Lines Matching +full:- +full:- +full:timeout +full:- +full:overall +full:- +full:minutes
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
898 ** [sqlite3_open_v2()] has historically be a no-op and might become an
954 ** read-only media and cannot be changed even by processes with
1016 ** (Third-party VFS implementations might also make the distinction
1051 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1075 ** requested lock, then the call to xLock() is a no-op.
1078 ** to xUnlock() is a no-op.
1091 ** locking strategy (for example to use dot-file locks), to inquire
1138 ** fails to zero-fill short reads might seem to work. However,
1139 ** failure to zero-fill short reads will eventually lead to
1195 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1197 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1207 ** point to an integer (type int) containing the new chunk-size to use
1209 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1220 ** the [rollback journal] or the [write-ahead log]) for a particular database
1233 ** this file-control is NULL. However, if the database file is being synced
1234 ** as part of a multi-database commit, the argument points to a nul-terminated
1235 ** string containing the transactions super-journal file name. VFSes that
1252 ** anti-virus programs. By default, the windows VFS will retry file read,
1278 ** WAL mode. If the integer is -1, then it is overwritten with the current
1283 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1287 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1288 ** mode. If the integer is -1, then it is overwritten with the current
1289 ** zero-damage mode setting.
1300 ** final bottom-level VFS are written into memory obtained from
1304 ** all file-control actions, there is no guarantee that this will actually
1306 ** pointer in case this file-control is not implemented. This file-control
1310 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1314 ** to a pointer to the top-level VFS.)^
1316 ** upper-most shim only.
1333 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1335 ** of the result string if the string is non-NULL.
1341 ** it is able to override built-in [PRAGMA] statements.
1345 ** file-control may be invoked by SQLite on the database file handle
1347 ** to the connection's busy-handler callback. The argument is of type (void**)
1348 ** - an array of two (void *) values. The first (void *) actually points
1350 ** busy-handler, this function should be invoked with the second (void *) in
1351 ** the array as the only argument. If it returns non-zero, then the operation
1356 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1366 ** maximum number of bytes that will be used for memory-mapped I/O.
1372 ** file-control is used internally to implement [PRAGMA mmap_size].
1378 ** The argument is a zero-terminated string. Higher layers in the
1380 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1405 ** Applications should <em>not</em> use this file-control.
1454 ** The parameter is a pointer to a 32-bit signed integer that contains
1455 ** the value that M is to be set to. Before returning, the 32-bit signed
1460 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1486 ** file to the database file, but before the *-shm file is updated to
1492 ** whether or not there is a database client in another process with a wal-mode
1494 ** (void*) argument passed with this file-control should be a pointer to a
1498 ** the database is not a wal-mode db, or if there is no such connection in any
1508 ** database is not a temp db, then this file-control purges the contents
1509 ** of the in-memory page cache. If there is an open transaction, or if
1510 ** the db is a temp-db, it is a no-op, not an error.
1588 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1631 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1649 ** consist of a single "-" character followed by no more than
1650 ** 11 alphanumeric and/or "-" characters.
1665 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1685 ** the open of a journal file a no-op. Writes to this journal would
1686 ** also be no-ops, and any attempt to read the journal would return
1688 ** file will be doing page-aligned sector reads and writes in a random
1727 ** flag is never actually used and is not implemented in the built-in
1730 ** non-zero error code if there is an I/O error or if the name of
1732 ** is returned, then non-zero or zero is written into *pResOut to indicate
1746 ** of good-quality randomness into zOut. The return value is
1754 ** a 24-hour day).
1780 void *pAppData; /* Pointer to application-specific data */
1890 ** are harmless no-ops.)^
1895 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1918 ** compile-time option, then the automatic calls to sqlite3_initialize()
1927 ** The sqlite3_os_init() routine does operating-system specific
1943 ** (using the [SQLITE_OS_OTHER=1] compile-time
1945 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1974 ** implementation of an application-defined [sqlite3_os_init()].
1984 ** then this routine returns a non-zero [error code].
1998 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2011 ** and low-level memory allocation routines.
2023 ** Note that SQLite comes with several [built-in memory allocators]
2028 ** memory allocator that simulates memory out-of-memory conditions in
2093 ** non-zero [error code] if a discontinued or unsupported configuration option
2099 ** [threading mode] to Single-thread. In other words, it disables
2102 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2104 ** value of Single-thread and so [sqlite3_config()] will return
2110 ** [threading mode] to Multi-thread. In other words, it disables
2114 ** are enabled so that SQLite will be safe to use in a multi-threaded
2117 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2118 ** it is not possible to set the Multi-thread [threading mode] and
2133 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2142 ** alternative low-level memory allocation routines to be used in place of
2170 ** disabled, the following SQLite interfaces become non-operational:
2191 ** This configuration option is a no-op if an application-defined page
2194 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2202 ** argument must be either a NULL pointer or a pointer to an 8-byte
2209 ** ^If pMem is NULL and N is non-zero, then each database connection
2212 ** of -1024*N bytes if N is negative, . ^If additional
2225 ** An 8-byte aligned pointer to the memory,
2232 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2240 ** The argument specifies alternative low-level mutex routines to be used
2244 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2257 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2290 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2292 ** passed through as the first parameter to the application-defined logger
2300 ** In a multi-threaded application, the application-defined logger
2305 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2322 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2323 ** if that compile-time option is omitted.
2333 ** They are retained for backwards compatibility but are now no-ops.
2339 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2355 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2362 ** compile-time maximum mmap size set by the
2363 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2365 ** changed to its compile-time default.
2370 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2371 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2387 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2396 ** becomes the [statement journal] spill-to-disk threshold.
2399 ** Or if the threshold is -1, statement journals are always held
2405 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2410 ** of type (int) - the new value of the sorter-reference size threshold.
2414 ** of a table column that its values are likely to be very large - larger
2415 ** than the configured sorter-reference size threshold - then a reference
2421 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2426 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2429 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2431 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2432 ** compile-time option is not set, then the default maximum is 1073741824.
2448 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2449 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2475 ** non-zero [error code] if a discontinued or unsupported configuration option
2491 ** must be aligned to an 8-byte boundary. ^If the second argument to
2527 ** triggers in the main database schema or in the schemas of ATTACH-ed
2544 ** views in the main database schema or in the schemas of ATTACH-ed
2551 ** [FTS3] full-text search engine extension.
2566 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2568 ** When the first argument to this interface is 1, then only the C-API is
2570 ** this interface is 0, then both the C-API and the SQL function are disabled.
2571 ** If the first argument is -1, then no changes are made to state of either the
2572 ** C-API or the SQL function.
2595 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2598 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2599 ** have been disabled - 0 if they are not disabled, 1 if they are.
2622 ** behavior. The first parameter passed to this operation is an integer -
2626 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2680 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2689 ** the legacy [double-quoted string literal] misfeature for DML statements
2691 ** default value of this setting is determined by the [-DSQLITE_DQS]
2692 ** compile-time option.
2698 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2700 ** default value of this setting is determined by the [-DSQLITE_DQS]
2701 ** compile-time option.
2728 ** created database file to have a schema format version number (the 4-byte
2785 ** has a unique 64-bit signed
2866 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2888 ** any changes performed by sub-triggers, as the sqlite3_changes()
2889 ** value will be saved and restored after each sub-trigger has run.)^
2957 ** CAPI3REF: Interrupt A Long-Running Query
2963 ** or Ctrl-C where the user wants a long query operation to halt
2988 ** SQL statements is a no-op and has no effect on SQL statements
2996 ** These routines are useful during command-line input to determine if the
3002 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3017 ** then the return value from sqlite3_complete16() will be non-zero
3020 ** The input to [sqlite3_complete()] must be a zero-terminated
3021 ** UTF-8 string.
3023 ** The input to [sqlite3_complete16()] must be a zero-terminated
3024 ** UTF-16 string in native byte order.
3031 ** KEYWORDS: {busy-handler callback} {busy handler}
3053 ** ^If the callback returns non-zero, then another attempt
3091 ** CAPI3REF: Set A Busy Timeout
3129 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3131 ** to zero-terminated strings that contain the names of the columns.
3133 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3145 ** -----------------------
3167 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3199 ** These routines are work-alikes of the "printf()" family of functions
3203 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3204 ** See the [built-in printf()] documentation for details.
3227 ** guarantees that the buffer is always zero-terminated. ^The first
3230 ** written will be n-1 characters.
3234 ** See also: [built-in printf()], [printf() SQL function]
3246 ** does not include operating-system specific [VFS] implementation. The
3257 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3258 ** of a signed 32-bit integer.
3263 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3268 ** might result if sqlite3_free() is called with a non-NULL pointer that
3288 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3289 ** of a 32-bit signed integer.
3304 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3328 ** routines, which form the built-in memory allocation subsystem.
3333 ** value of [sqlite3_memory_used()] since the high-water mark
3340 ** ^The memory high-water mark is reset to the current value of
3343 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3350 ** CAPI3REF: Pseudo-Random Number Generator
3352 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3355 ** the built-in random() and randomblob() SQL functions. This interface allows
3366 ** non-NULL P then the pseudo-randomness is generated
3373 ** CAPI3REF: Compile-Time Authorization Callbacks
3404 ** to the callback are either NULL pointers or zero-terminated strings
3432 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3451 ** statement might be re-prepared during [sqlite3_step()] due to a
3497 ** is the name of the inner-most trigger or view that is responsible for
3499 ** top-level SQL code.
3549 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3553 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3555 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3560 ** the original statement text and an estimate of wall-clock time
3580 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3602 ** interface by using the X argument when X begins with "--" and invoking
3609 ** X argument points to a 64-bit integer which is the estimated of
3639 ** M argument should be the bitwise OR-ed combination of
3689 ** ^If the progress callback returns non-zero, the operation is
3706 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3707 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3718 ** ^The default encoding will be UTF-8 for databases created using
3720 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3734 ** <dd>The database is opened in read-only mode. If the database does not
3756 ** <dd>The database will be opened as an in-memory database. The database
3757 ** is named by the "filename" argument for the purposes of cache-sharing,
3762 ** <dd>The new database connection will use the "multi-thread"
3780 ** this option is a no-op.
3806 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3817 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3818 ** is created for the connection. ^This in-memory database will vanish when
3826 ** on-disk database will be created. ^This private database will be
3836 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3859 ** SQLite and its built-in [VFSes] interpret the
3874 ** ^If "ro" is specified, then the database is opened for read-only
3877 ** "rw", then the database is opened for read-write (but not create)
3881 ** set to "memory" then a pure [in-memory database] that never reads
3908 ** read-only media. ^When immutable is set, SQLite assumes that the
3910 ** privilege, and so the database is opened read-only and all locking
3935 ** <tr><td style="white-space:nowrap">
3939 ** necessary - space characters can be used literally
3942 ** Open file "data.db" in the current directory for read-only access.
3943 ** Regardless of whether or not shared-cache mode is enabled by
3945 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3946 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3947 ** that uses dot-files in place of posix advisory locking.
3955 ** percent sign - "%" - followed by exactly two hexadecimal digits
3957 ** URI filename are interpreted, they are encoded using UTF-8 and all
3959 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
3963 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3965 ** characters must be converted to UTF-8 prior to passing them into
3975 const char *filename, /* Database filename (UTF-8) */
3979 const void *filename, /* Database filename (UTF-16) */
3983 const char *filename, /* Database filename (UTF-8) */
4020 ** case or if the value begins with a non-zero number. The
4028 ** 64-bit signed integer and returns that integer, or D if P does not
4033 ** the value) of the N-th query parameter for filename F, or a NULL
4035 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4094 ** ^If X is the name of a rollback or WAL-mode journal file that is
4100 ** only. It is not a general-purpose interface.
4144 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4180 ** change the value of the error code. The error-code preserving
4191 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4192 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4198 ** ^The sqlite3_errstr() interface returns the English-language text
4199 ** that describes the [result code], as UTF-8.
4208 ** SQL, then the sqlite3_error_offset() function returns -1.
4243 ** The life-cycle of a prepared statement object usually goes like this:
4258 ** CAPI3REF: Run-time Limits
4271 ** set at compile-time by a C preprocessor macro called
4280 ** simply invoke this interface with the third parameter set to -1.
4282 ** Run-time limits are intended for use in applications that manage
4295 ** New run-time limit categories may be added in future releases.
4300 ** CAPI3REF: Run-Time Limit Categories
4304 ** that can be lowered at run-time using [sqlite3_limit()].
4390 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4413 ** To execute an SQL statement, it must first be compiled into a byte-code
4422 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4423 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4424 ** as a convenience. The UTF-16 interfaces work by converting the
4425 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4432 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4434 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4435 ** and sqlite3_prepare16_v3() use UTF-16.
4441 ** If the caller knows that the supplied string is nul-terminated, then
4444 ** the nul-terminator.
4494 ** ^The specific value of a WHERE-clause [parameter] might influence the
4495 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4497 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4509 const char *zSql, /* SQL statement, UTF-8 encoded */
4516 const char *zSql, /* SQL statement, UTF-8 encoded */
4523 const char *zSql, /* SQL statement, UTF-8 encoded */
4531 const void *zSql, /* SQL statement, UTF-16 encoded */
4538 const void *zSql, /* SQL statement, UTF-16 encoded */
4545 const void *zSql, /* SQL statement, UTF-16 encoded */
4556 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4560 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4563 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4579 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4580 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4591 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4603 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4607 ** Note that [application-defined SQL functions] or
4611 ** change the database file through side-effects:
4637 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4639 ** read-only no-op if the table already exists, but
4664 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4701 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4712 ** implementation of [application-defined SQL functions] are protected.
4730 ** is always first parameter to [application-defined SQL functions].
4731 ** The application-defined SQL function implementation will pass this
4781 ** it should be a pointer to well-formed UTF8 text.
4783 ** it should be a pointer to well-formed UTF16 text.
4785 ** it should be a pointer to a well-formed unicode string that is
4789 ** [[byte-order determination rules]] ^The byte-order of
4790 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4807 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4847 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4849 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4921 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
4932 ** ^If the value N is out of range or if the N-th parameter is
4934 ** always in UTF-8 encoding even if the named parameter was
4935 ** originally specified as UTF-16 in [sqlite3_prepare16()],
4952 ** name must be given in UTF-8 even if the original statement
4953 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
4994 ** interface returns a pointer to a zero-terminated UTF-8 string
4995 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
4996 ** UTF-16 string. ^The first parameter is the [prepared statement]
5007 ** (for example during a conversion from UTF-8 to UTF-16) then a
5026 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5035 ** ^The names returned are the original un-aliased names of the
5041 ** ^The left-most column is column 0 for these routines.
5050 ** UTF-16 encoded strings and the other functions return UTF-8.
5053 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5077 ** ^The returned string is always UTF-8 encoded.
5090 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5140 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5166 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5199 ** will return non-zero if previous call to [sqlite3_step](P) returned
5201 ** where it always returns zero since each step of that multi-step
5215 ** <li> 64-bit signed integer
5216 ** <li> 64-bit IEEE floating point number
5249 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5250 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5251 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5252 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5257 ** or a UTF-8 TEXT result in bytes
5259 ** <td>→ <td>Size of UTF-16
5311 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5313 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5314 ** the string to UTF-8 and then returns the number of bytes.
5316 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5320 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5322 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5323 ** the string to UTF-16 and then returns the number of bytes.
5325 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5336 ** even empty strings, are always zero-terminated. ^The return
5337 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5353 ** [application-defined SQL functions] or [virtual tables], not within
5354 ** top-level application code.
5372 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5393 ** sqlite3_column_text16() is called. A zero-terminator might
5395 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5397 ** to UTF-16.</li>
5398 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5400 ** to UTF-8.</li>
5403 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5434 ** fail if an out-of-memory error occurs during a format conversion.
5435 ** Only the following subset of interfaces are subject to out-of-memory
5446 ** If an out-of-memory error occurs, then the return value from these
5448 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5482 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5497 ** object back to its initial state, ready to be re-executed.
5536 ** connection then application-defined SQL functions must be added
5540 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5541 ** representation, exclusive of the zero-terminator. ^Note that the name
5542 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5548 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5551 ** parameter is less than -1 or greater than 127 then the behavior is
5570 ** deterministic. The built-in [random()] SQL function is an example of a
5581 ** all application-defined SQL functions that do not need to be
5595 ** pointers to C-language functions that implement the SQL function or
5605 ** C-language callbacks that implement the new function. xStep and xFinal
5606 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5608 ** non-NULL, in which case the new function may be used as either an aggregate
5611 ** [user-defined window functions|available here].
5626 ** SQL function is used. ^A function implementation with a non-negative
5635 ** ^Built-in functions may be overloaded by new application-defined functions.
5637 ** ^An application-defined function is permitted to call other
5692 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5693 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5694 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5721 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5725 ** for all [application-defined SQL functions], and especially for functions
5726 ** that have side-effects or that could potentially leak sensitive
5746 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5748 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5750 ** security-adverse side-effects and information-leaks.
5755 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5757 ** functions. However, if it is not specified for a user-defined window
5758 ** function, then any sub-types belonging to arguments passed to the window
5797 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5798 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5800 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5801 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5803 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5804 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5807 ** or a UTF-8 TEXT in bytes
5809 ** <td>→ <td>Size of UTF-16
5827 ** implement [application-defined SQL functions] and [virtual tables].
5837 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5838 ** in the native byte-order of the host machine. ^The
5840 ** extract UTF-16 strings as big-endian and little-endian respectively.
5891 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5906 ** fail if an out-of-memory error occurs during a format conversion.
5907 ** Only the following subset of interfaces are subject to out-of-memory
5920 ** If an out-of-memory error occurs, then the return value from these
5922 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5949 ** an [application-defined SQL function] argument V. The subtype
5969 ** then sqlite3_value_free(V) is a harmless no-op.
6030 ** the application-defined function is running.
6050 ** These functions may be used by (non-aggregate) SQL functions to
6054 ** of where this might be useful is in a regular-expression matching
6063 ** value to the application-defined function. ^N is zero for the left-most
6068 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6069 ** argument of the application-defined function. ^Subsequent
6093 ** function parameters that are compile-time constants, including literal
6096 ** The value of the N parameter to these interfaces should be non-negative.
6123 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6139 ** an application-defined function to be the BLOB whose content is pointed
6144 ** interfaces set the result of the application-defined function to be
6148 ** an application-defined function to be a floating point value specified
6156 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6157 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6158 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6163 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6181 ** of the application-defined function to be the 32-bit signed integer
6184 ** of the application-defined function to be the 64-bit signed integer
6188 ** of the application-defined function to be NULL.
6192 ** set the return value of the application-defined function to be
6193 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6194 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6196 ** application-defined function to be a text string in an encoding
6206 ** is non-negative, then as many bytes (not characters) of the text
6207 ** pointed to by the 2nd parameter are taken as the application-defined
6208 ** function result. If the 3rd parameter is non-negative, then it
6215 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6231 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6233 ** byte-order specified by the BOM. ^The byte-order specified by
6234 ** the BOM at the beginning of the text overrides the byte-order
6237 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6248 ** the application-defined function to be a copy of the
6259 ** also associates the host-language pointer P or type T with that
6261 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6269 ** than the one containing the application-defined function that received
6301 ** the result from the [application-defined SQL function] with
6317 ** ^The name of the collation is a UTF-8 string
6319 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6382 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6425 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6426 ** the names are passed as UTF-16 in machine native byte order.
6427 ** ^A call to either function replaces the existing collation-needed callback.
6524 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6566 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6567 ** TemporaryFolder->Path->Data();
6570 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6583 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6622 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6627 ** the current directory on the sub-platforms of Win32 where that concept is
6631 ** UTF-8 or UTF-16, respectively.
6650 ** CAPI3REF: Test For Auto-Commit Mode
6654 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6658 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6660 ** If certain kinds of errors occur on a statement within a multi-statement
6691 ** for the N-th database on database connection D, or a NULL pointer of N is
6693 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6701 ** remember the string long-term should make their own copy. Applications that
6703 ** threads should mutex-protect calls to this API and should make their own
6715 ** connection D, or if database N is a temporary or in-memory database, then
6720 ** is [DETACH]-ed or until the database connection closes.
6741 ** CAPI3REF: Determine if a database is read-only
6745 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6764 ** a valid schema, then -1 is returned.
6830 ** ^If the callback on a commit hook function returns non-zero,
6852 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6854 ** hook returning non-zero, just as it would be with any other rollback.
6874 ** the schema-name of the attached database that is being autovacuumed,
6882 ** <p>^If there are multiple ATTACH-ed database files that are being
6997 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
6998 ** compile-time option is recommended because the
7024 ** shared cache mode should be enabled per-database connection via
7028 ** 32-bit integer is atomic.
7030 ** See Also: [SQLite Shared-Cache Mode]
7038 ** of heap memory by deallocating non-essential memory allocations
7040 ** pages to improve performance is an example of non-essential memory.
7043 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7057 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7092 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7116 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7117 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7150 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7154 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7221 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7233 ** with various operating-system specific extensions added.
7242 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7281 ** [extension loading] while evaluating user-entered SQL, the following API
7289 ** ^This interface enables or disables both the C-API
7292 ** to enable or disable only the C-API.)^
7332 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7361 ** The interface to the virtual-table mechanism is currently considered
7365 ** When the virtual-table mechanism stabilizes, we will declare the
7439 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7451 ** expr on the right-hand side can be evaluated (and thus the constraint
7466 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7472 ** non-zero.
7476 ** the right-hand side of the corresponding aConstraint[] is evaluated
7477 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7506 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7537 int iColumn; /* Column constrained. -1 for ROWID */
7540 int iTermOffset; /* Used internally - xBestIndex should ignore */
7582 ** ^The left-hand operand of the operator is given by the corresponding
7583 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7586 ** operators have no left-hand operand, and so for those operators the
7595 ** The right-hand operands for each constraint might be accessible using
7596 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7598 ** in the input SQL. If the right-hand operand is another column or an
7602 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7607 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7770 ** The interface to the virtual-table mechanism defined above (back up
7775 ** When the virtual-table mechanism stabilizes, we will declare the
7812 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7814 ** read-only access.
7850 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7866 ** and the built-in [zeroblob] SQL function may be used to create a
7867 ** zero-filled blob to read or write using the incremental-blob interface.
7897 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7919 ** ^If the blob handle being closed was opened for read-write access, and if
7920 ** the database is in auto-commit mode and there are no other open read-write
7928 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7955 ** caller-supplied buffer. N bytes of data are copied into buffer Z
7984 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8033 ** ^Names are zero-terminated UTF-8 strings.
8064 ** is selected automatically at compile-time. The following
8075 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8080 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8117 ** cases where it really needs one. If a faster non-recursive mutex
8164 ** behave as no-ops.
8177 ** An instance of this structure defines the low-level routines
8227 ** xMutexInit() must be no-ops.
8273 ** the routine should return 1. This seems counter-intuitive since
8277 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8293 ** next. Applications that override the built-in mutex logic must be
8309 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8324 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8330 ** CAPI3REF: Low-Level Control Of Database Files
8442 ** by enclosing in double-quotes) so as not to confuse the parser.
8447 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8450 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8456 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8468 ** <li> Put all identifier names inside double-quotes. This is the official
8479 ** compile-time options. For example, "VACUUM" is not a keyword if
8480 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8491 ** An instance of the sqlite3_str object contains a dynamically-sized
8515 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8555 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8560 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8561 ** S must contain at least N non-zero bytes of content. To append a
8562 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8566 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8569 ** single-byte character C onto the end of [sqlite3_str] object X.
8595 ** [SQLITE_NOMEM] following any out-of-memory error, or
8602 ** zero-termination byte.
8637 ** SQLITE_OK on success and a non-zero [error code] on failure.
8640 ** be represented by a 32-bit integer, then the values returned by
8658 ** These integer constants designate various run-time status parameters
8666 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8751 ** non-zero [error code] on failure.
8767 ** The [sqlite3_db_status()] interface will return a non-zero error code
8777 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8785 ** Only the high-water value is meaningful;
8793 ** Only the high-water value is meaningful;
8816 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8856 ** to disk all at once. When pages spill mid-transaction, that introduces
8927 ** A non-zero value in this counter may indicate an opportunity to
8933 ** A non-zero value in this counter may indicate an opportunity to
8962 ** step was bypassed because a Bloom filter returned not-found. The
9032 ** The built-in page cache is recommended for most uses.
9047 ** built-in default page cache is used instead of the application defined
9079 ** false if it is used for an in-memory database. The cache implementation
9090 ** suggested maximum cache-size (number of pages stored by) the cache
9136 ** as its second argument. If the third parameter, discard, is non-zero,
9170 ** is not obligated to free any memory, but well-behaved implementations should
9229 ** for copying in-memory databases to or from persistent files.
9235 ** ^The source database is read-locked only while it is being read;
9269 ** there is already a read or read-write transaction open on the
9301 ** <li> the destination database was opened read-only, or
9302 ** <li> the destination database is using write-ahead-log journaling
9304 ** <li> the destination database is an in-memory database and the
9308 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9309 ** the [sqlite3_busy_handler | busy-handler function]
9311 ** busy-handler returns non-zero before the lock is available, then
9332 ** sqlite3_backup_step(), the source database may be modified mid-way
9349 ** active write-transaction on the destination database is rolled back.
9356 ** ^If an out-of-memory condition or IO error occurred during any prior
9425 ** ^When running in shared-cache mode, a database operation may fail with
9426 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9427 ** individual tables within the shared-cache cannot be obtained. See
9428 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9432 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9436 ** ^Shared-cache locks are released when a database connection concludes
9440 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9450 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9456 ** ^If the blocked connection is attempting to obtain a write-lock on a
9457 ** shared-cache table, and more than one other connection currently holds
9458 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9461 ** ^(There may be at most one unlock-notify callback registered by a
9463 ** blocked connection already has a registered unlock-notify callback,
9466 ** unlock-notify callback is canceled. ^The blocked connections
9467 ** unlock-notify callback may also be canceled by closing the blocked
9470 ** The unlock-notify callback is not reentrant. If an application invokes
9471 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9479 ** When an unlock-notify callback is registered, the application provides a
9483 ** an unlock-notify callback is a pointer to an array of void* pointers,
9487 ** more than one blocked connection that has registered for an unlock-notify
9497 ** Assuming that after registering for an unlock-notify callback a
9508 ** unlock-notify callback is registered. The system is said to be in
9509 ** a deadlocked state if connection A has registered for an unlock-notify
9511 ** B has itself registered for an unlock-notify callback when connection
9514 ** registered for an unlock-notify callback on the conclusion of connection
9526 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9527 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9547 ** and extensions to compare the contents of two buffers containing UTF-8
9548 ** strings in a case-independent fashion, using the same definition of "case
9564 ** Note that this routine returns zero on a match and non-zero if the strings
9581 ** insensitive - equivalent upper and lower case ASCII characters match
9587 ** Note that this routine returns zero on a match and non-zero if the strings
9611 ** a fixed-length buffer on the stack. If the log message is longer than
9618 ** CAPI3REF: Write-Ahead Log Commit Hook
9625 ** the associated write-lock on the database released)^, so the implementation
9631 ** ^The third parameter is the name of the database that was written to -
9632 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9633 ** is the number of pages currently in the write-ahead log file,
9644 ** A single database handle may have at most a single write-ahead log callback
9646 ** previously registered write-ahead log callback. ^The return value is
9659 ** CAPI3REF: Configure an auto-checkpoint
9666 ** more frames in the [write-ahead log] file. ^Passing zero or
9681 ** ^Every new [database connection] defaults to having the auto-checkpoint
9697 ** [write-ahead log] for database X on [database connection] D to be
9698 ** transferred into the database file and for the write-ahead log to
9724 ** in the log were checkpointed. ^The [busy-handler callback]
9731 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9740 ** [busy-handler callback])
9753 ** the log file or to -1 if the checkpoint could not run because
9757 ** was called) or to -1 if the checkpoint could not run due to an error or
9765 ** busy-handler configured, it will not be invoked in this case.
9769 ** obtained immediately, and a busy-handler is configured, it is invoked and
9770 ** the writer lock retried until either the busy-handler returns 0 or the lock
9771 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9772 ** database readers as described above. ^If the busy-handler returns 0 before
9775 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9791 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9867 ** If X is non-zero, then the virtual table implementation guarantees
9953 ** [xUpdate] method understands as a "no-change" value.
10094 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10095 ** only sees a single value from the right-hand side of the IN operator
10099 ** table to see all values on the right-hand of the IN operator all at
10104 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10105 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10107 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10108 ** by which the virtual table can ask SQLite if all-at-once processing
10114 ** the IN operator all-at-once, respectively. ^Thus when the third
10115 ** parameter (F) is non-negative, this interface is the mechanism by
10124 ** (non-zero), that means that the constraint is an IN operator
10125 ** that can be processed all-at-once. ^If the constraint is not an IN
10126 ** operator or cannot be processed all-at-once, then the interface returns
10129 ** ^(All-at-once processing of the IN operator is selected if both of the
10133 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10135 ** use the N-th constraint.
10138 ** non-negative had F>=1.
10142 ** the traditional one-at-a-time processing strategy for the IN constraint.
10143 ** ^If both conditions are true, then the argvIndex-th parameter to the
10146 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10152 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10162 ** a parameter that was previously selected for all-at-once IN constraint
10165 ** an xFilter argument that was selected for all-at-once IN constraint
10169 ** ^(Use these routines to access all values on the right-hand side
10211 ** J being a 0-based index into P->aConstraint[], then this routine
10212 ** attempts to set *V to the value of the right-hand operand of
10213 ** that constraint if the right-hand operand is known. ^If the
10214 ** right-hand operand is not known, then *V is set to a NULL pointer.
10217 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10223 ** the right-hand operand of a constraint is a literal value in the original
10224 ** SQL statement. If the right-hand operand is an expression or a reference
10229 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10238 ** "Right-Hand Side".
10275 ** set to the total number of times that the X-th loop has run.</dd>
10279 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10284 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10291 ** to a zero-terminated UTF-8 string containing the name of the index or table
10292 ** used for the X-th loop.
10296 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10297 ** description for the X-th loop.
10301 ** "select-id" for the X-th loop. The select-id identifies which query or
10302 ** subquery the loop is part of. The main query has a select-id of zero.
10303 ** The select-id is the same value as is output in the first column
10325 ** compile-time option.
10333 ** Loops are numbered starting from zero. ^If idx is out of range - less than
10335 ** the statement - a non-zero value is returned and the variable that pOut
10340 ** as if the loop did not exist - it returns non-zero and leave the variable
10353 ** CAPI3REF: Zero Scan-Status Counters
10358 ** This API is only available if the library is built with pre-processor
10364 ** CAPI3REF: Flush caches to disk mid-transaction
10367 ** ^If a write-transaction is open on [database connection] D when the
10369 ** pages in the pager-cache that are not currently in use are written out
10373 ** interface flushes caches for all schemas - "main", "temp", and
10378 ** immediately and there is a busy-handler callback configured, it is invoked
10386 ** example an IO error or out-of-memory condition), then processing is
10397 ** CAPI3REF: The pre-update hook.
10401 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10473 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10474 ** triggers; or 2 for changes resulting from triggers called by top-level
10478 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10483 ** pre-update hook is being invoked for some other reason, including a
10484 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10510 ** CAPI3REF: Low-level system error code
10515 ** The return value is OS-dependent. For example, on unix systems, after
10555 ** If there is not already a read-transaction open on schema S when
10587 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10621 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10622 ** SQLITE_IOERR error code - is returned, then the final state of the
10636 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10653 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10720 ** For an ordinary on-disk database file, the serialization is just a
10721 ** copy of the disk file. For an in-memory database or a "TEMP" database,
10758 ** Zero or more of the following constants can be OR-ed together for
10762 ** a pointer to contiguous in-memory database that it is currently using,
10764 ** a contiguous in-memory database, then this option causes
10766 ** using a contiguous in-memory database if it has been initialized by a
10776 ** reopen S as an in-memory database based on the serialization contained
10780 ** permitted to add content to the in-memory database as long as the total
10832 ** should be treated as read-only.
10836 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10876 /* The double-precision datatype used by RTree depends on the
10877 ** SQLITE_RTREE_INT_ONLY compile-time option.
10887 ** R-Tree geometry query as follows:
10912 ** Register a 2nd-generation geometry callback named zScore that can be
10913 ** used as part of an R-Tree geometry query as follows:
11019 ** is not possible for an application to register a pre-update hook on a
11022 ** which a pre-update hook is already defined. The results of attempting
11090 ** disabled - it does not. A newly created session object is enabled.
11097 ** no-op, and may be used to query the current state of the session.
11174 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11180 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11184 ** conflict-handler callback) then the X'' value is returned. The application
11232 ** database row along with the updated values for each updated non-primary-key
11241 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11243 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11256 ** are sorted in the same order in which they were attached (or auto-attached)
11270 ** recorded once - the first time a row with said primary key is inserted,
11277 ** The session object therefore accumulates two types of records - those
11295 ** found and one or more of the non-primary key fields have been
11312 ** This may appear to have some counter-intuitive effects if a single row
11328 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11350 ** does not have a primary key, this function is a no-op (but does not return
11370 ** used to update the table in database zFrom (call this the "from-table")
11372 ** object (call this the "to-table"). Specifically:
11375 ** <li> For each row (primary key) that exists in the to-table but not in
11376 ** the from-table, an INSERT record is added to the session object.
11378 ** <li> For each row (primary key) that exists in the to-table but not in
11379 ** the from-table, a DELETE record is added to the session object.
11382 ** different non-PK values in each, an UPDATE record is added to the
11427 ** Because the non-primary key "old.*" fields are omitted, no
11446 ** Return non-zero if no changes to attached tables have been recorded by
11454 ** are restored. However, if this function returns non-zero, then it is
11541 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11565 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11578 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11581 ** or until the conflict-handler function returns.
11628 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11637 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11668 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11702 ** conflict-handler callback by [sqlite3changeset_apply()] with either
11713 ** "conflicting row" associated with the current conflict-handler callback
11751 ** function with an iterator passed to a conflict-handler by
11759 ** returned. This is to allow the following pattern (pseudo-code):
11912 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
11913 ** <tr><th style="white-space:pre">Existing Change </th>
11914 ** <th style="white-space:pre">New Change </th>
11959 ** returned. Or, if an out-of-memory condition occurs during processing, this
12017 ** Otherwise, if the return value is non-zero or the xFilter argument to
12053 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12057 ** returned by each invocation of the conflict-handler function. Refer to
12066 ** stored in all non-primary key columns also match the values stored in
12070 ** the non-primary key fields contains a value different from the original
12071 ** row value stored in the changeset, the conflict-handler function is
12074 ** only the values of those non-primary key fields are compared against
12075 ** the current database contents - any trailing database table columns
12079 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12084 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12103 ** This includes the case where the INSERT operation is re-attempted because
12111 ** stored in all modified non-primary key columns also match the values
12115 ** the modified non-primary key fields contains a value different from an
12116 ** original row value stored in the changeset, the conflict-handler function
12118 ** UPDATE changes only contain values for non-primary key fields that are
12120 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12123 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12127 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12145 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12222 ** Values that may be passed as the second argument to a conflict-handler.
12229 ** (non primary-key) fields modified by the update do not contain the
12390 ** combined on a per-field basis, not per-row. This means that in the
12474 ** corresponding non-streaming API functions:
12476 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12477 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12487 ** Non-streaming functions that accept changesets (or patchsets) as input
12492 ** low-memory environment is required to handle very large changesets, the
12756 ** non-negative but less than the number of columns in the table, return
12771 ** non-negative but less than the number of columns in the table, set
12785 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12883 ** If the bClear argument is non-zero, then the auxiliary data is cleared
12905 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12907 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12913 ** modify this structure directly - it should only be used as shown above
12921 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12935 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12937 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12946 ** xPhraseFirstColumn() set iCol to -1).
13015 ** The second and third arguments are an array of nul-terminated strings
13033 ** by argument pText. pText may or may not be nul-terminated. The first
13041 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13046 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13050 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13055 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13088 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13122 ** still appears to contain just two phrases - "(first OR 1st)"
13133 ** when tokenizing query text (it should not - to do so would be
13180 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13281 ** autoconf-based build
13309 ** The hard limit is the ability of a 32-bit signed integer
13310 ** to count the size: 2^31-1 or 2147483647.
13328 ** tell you that in a well-normalized database, you usually should
13386 ** The suggested maximum number of in-memory pages to use for
13389 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13391 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13392 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13395 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13409 ** counted using a signed 8-bit integer which has a maximum value of 127
13421 ** as a signed 32-bit integer can hold.
13428 ** imposed by the use of 16-bit offsets within each page.
13434 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13435 ** compiled with the default page-size limit will not be able to rollback
13458 ** device characteristics (sector-size and atomic write() support),
13475 ** This value can be lowered (or raised) at run-time using that the
13506 #pragma warn -rch /* unreachable code */
13507 #pragma warn -ccc /* Condition is always true or false */
13508 #pragma warn -aus /* Assigned value is never used */
13509 #pragma warn -csu /* Comparing signed and unsigned */
13510 #pragma warn -spa /* Suspicious pointer arithmetic */
13514 ** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13518 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13551 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13564 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13565 #else /* Generates a warning - but it always works */
13605 ** level of threadsafety. 2 means the library is multithreaded - multiple
13613 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13621 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13627 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13634 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13663 # error "Two or more of the following compile-time configuration options\
13701 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13766 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13778 ** of SQLite to unexpected behavior - to make the code "self-healing"
13784 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
13870 ** Return true (non-zero) if the input is an integer that is too large
13871 ** to fit in 32-bits. This macro is used inside of various testcase()
13872 ** macros to verify that we have tested SQLite for large-file support.
13899 ** This is the header file for the generic hash-table implementation
13910 ** The internals of this structure are intended to be opaque -- client
13917 ** All elements of the hash table are on a single doubly-linked list.
13921 ** the global doubly-linked list. The contents of the bucket are the
13922 ** element pointed to plus the next _ht.count-1 elements in the list.
13941 ** structure. All elements are stored on a single doubly-linked list.
13972 #define sqliteHashFirst(H) ((H)->first)
13973 #define sqliteHashNext(E) ((E)->next)
13974 #define sqliteHashData(E) ((E)->data)
13975 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13976 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13981 #define sqliteHashCount(H) ((H)->count)
14191 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14196 ** substitute integer for floating-point
14227 ** the VDBE-level file format changes. The following macros define the
14238 ** changed at run-time using a pragma.
14246 ** on the command-line
14276 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14278 ** The default value of "20" was chosen to minimize the run-time of the
14279 ** speedtest1 test program with options: --shrink-memory --reprepare
14293 ** The compile-time options SQLITE_MMAP_READWRITE and
14306 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14337 ** types can be conveniently redefined at compile-type. Like this:
14339 ** cc '-DUINTPTR_TYPE=long long int' ...
14379 typedef sqlite_int64 i64; /* 8-byte signed integer */
14380 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14381 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14382 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14383 typedef INT16_TYPE i16; /* 2-byte signed integer */
14384 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14385 typedef INT8_TYPE i8; /* 1-byte signed integer */
14393 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14398 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
14399 ** can be used at compile-time if desired.
14402 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14404 typedef u32 tRowcnt; /* 32-bit is the default */
14408 ** Estimated quantities used for query planning are stored as 16-bit
14410 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14419 ** 1 -> 0 20 -> 43 10000 -> 132
14420 ** 2 -> 10 25 -> 46 25000 -> 146
14421 ** 3 -> 16 100 -> 66 1000000 -> 199
14422 ** 4 -> 20 1000 -> 99 1048576 -> 200
14423 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14428 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14471 ** and whether or not that determination is run-time or compile-time.
14473 ** For best performance, an attempt is made to guess at the byte-order
14474 ** using C-preprocessor macros. If that is unsuccessful, or if
14475 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14476 ** at run-time.
14511 ** Constants for the largest and smallest possible 64-bit signed integers.
14512 ** These macros are designed to work correctly on both 32-bit and 64-bit
14517 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14521 ** to force 8-byte alignment on 64-bit architectures.
14526 ** pointers in size, and so it is a no-op on systems where the pointer
14542 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14547 ** underlying malloc() implementation might return us 4-byte aligned
14548 ** pointers. In that case, only verify 4-byte alignment.
14551 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14553 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14565 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14585 ** default MMAP_SIZE is specified at compile-time, make sure that it does
14609 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14630 ** An instance of the following structure is used to store the busy-handler
14635 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14669 ** The root-page of the schema table.
14688 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14707 ** directly, we use its constant as a key to lookup the run-time allocated
14709 ** for the run-time allocated buffer.
14712 ** macros become no-ops and have zero performance impact.
14810 ** Changing this from a 64-bit to a 32-bit type limits the number of
14832 #define ALLBITS ((Bitmask)-1)
14833 #define TOPBIT (((Bitmask)1)<<(BMS-1))
14862 ** This header file (together with is companion C source-code file
14866 ** This header file is #include-ed by sqliteInt.h and thus ends up
14874 ** necessary pre-processor macros for it.
14890 ** This file contains pre-processor directives related to operating system
14911 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
14974 ** a no-op
15000 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
15002 ** 2006-10-31: The default prefix used to be "sqlite_". But then
15003 ** Mcafee started using SQLite in their anti-virus product and it
15049 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
15157 ** sqlite3_malloc() to obtain space for the file-handle structure.
15193 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
15216 ** is devoted to storing a super-journal name - there are no more pages to
15220 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
15221 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
15229 #define PAGER_MEMORY 0x0002 /* In-memory database */
15234 #define PAGER_LOCKINGMODE_QUERY -1
15245 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
15250 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
15251 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
15257 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
15279 ** that make up the Pager sub-system API. See source code comments for
15434 ** This header file defines the interface that the sqlite B-Tree file
15447 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15454 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15455 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15468 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15483 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15484 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15538 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15541 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15544 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15545 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15565 ** For example, the free-page-count field is located at byte offset 36 of
15566 ** the database file header. The incr-vacuum-flag field is located at
15570 ** It is a read-only number computed by the pager. But we merge it with
15583 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15597 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15598 ** column of the b-tree of the cursor. The Expr tree will not contain
15599 ** any function calls nor subqueries nor references to b-trees other than
15602 ** The design of the _RANGE hint is aid b-tree implementations that try
15603 ** to prefetch content from remote machines - to provide those
15628 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15633 ** For read-only cursors the wrFlag argument is always zero. For read-write
15644 ** by this, the native b-tree engine of SQLite, but it is available to
15646 ** b-tree system. For alternative storage engines in which a delete of
15650 ** and DELETE operations as no-ops, and any READ operation against a
15653 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
15659 int wrFlag, /* 1 for writing. 0 for read-only */
15798 ** Enter and Leave procedures no-ops.
15885 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15916 u32 iSrcLine; /* Source-code line that generated this opcode
15924 ** A sub-routine used to implement a trigger program.
15927 VdbeOp *aOp; /* Array of opcodes for sub-program */
15933 SubProgram *pNext; /* Next sub-program already visited */
15953 #define P4_STATIC (-1) /* Pointer to a static string */
15954 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15955 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15956 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15957 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
15959 #define P4_FREE_IF_LE (-6)
15960 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
15961 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
15962 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
15963 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
15964 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
15965 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
15966 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
15967 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
15968 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
15969 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16070 #define OP_IfPos 59 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
16071 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
16072 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
16101 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
16118 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
16135 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
16171 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
16247 ** Additional non-public SQLITE_PREPARE_* flags
16296 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16301 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16378 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16415 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16420 ** // in distingishing equal and not-equal.
16423 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16424 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16432 ** is responsible for keeping track of this and reporting byte-code branches
16562 /* Modify the page-size after the cache has been created. */
16584 /* Change a page number. Used by incr-vacuum. */
16626 /* Set and get the suggested cache-size for the specified pager-cache.
16629 ** the total number of pages cached by purgeable pager-caches to the sum
16630 ** of the suggested cache-sizes.
16637 /* Set or get the suggested spill-size for the specified pager-cache.
16639 ** The spill-size is the minimum number of pages in cache before the cache
16703 ** at start-time.
16705 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
16708 ** start-time.
16710 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16712 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16729 ** If this is a no-op implementation, implement everything as macros.
16750 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16770 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16771 ** In other words, the zero-based numbers are used for all external interfaces
16772 ** and the one-based values are used internally.
16800 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16829 ** Db.pSchema->flags field.
16831 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16832 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16833 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16834 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16837 ** Allowed values for the DB.pSchema->flags field.
16857 ** Lookaside malloc is a set of fixed-size buffers that can be used
16879 ** in a performance-critical path. sz should be set by to szTrue whenever
16884 ** come off of pFree first, then pInit as a fallback. This dual-list
16885 ** allows use to compute a high-water mark - the maximum number of allocations
16886 ** outstanding at any point in the past - by subtracting the number of
16889 ** Enhancement on 2019-12-12: Two-size-lookaside
16893 ** The two-size-lookaside enhancement breaks up the lookaside allocation
16894 ** into two pools: One of 128-byte slots and the other of the default size
16895 ** (1200-byte) slots. Allocations are filled from the small-pool first,
16896 ** failing over to the full-size pool if that does not work. Thus more
16913 void *pMiddle; /* First byte past end of full-size buffers and
16918 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
16924 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16925 #define EnableLookaside db->lookaside.bDisable--;\
16926 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16928 /* Size of the smaller allocations in two-size lookside */
16936 ** A hash table for built-in function definitions. (Application-defined
17021 u32 nSchemaLock; /* Do not reset the schema when non-zero */
17029 u8 autoCommit; /* The auto-commit flag. */
17033 u8 dfltLockMode; /* Default locking-mode for attached dbs */
17039 u8 noSharedCache; /* True if no shared-cache backends */
17087 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17124 int busyTimeout; /* Busy handler timeout, in msec */
17125 int nSavepoint; /* Number of non-transaction savepoints */
17126 int nStatement; /* Number of nested statement-transactions */
17161 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17162 #define ENC(db) ((db)->enc)
17167 ** C-compilers still do not accept LL integer literals.
17191 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
17210 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17211 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17232 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17253 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17257 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17259 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17267 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17277 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17278 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17284 #define ConstFactorOk(P) ((P)->okConstFactor)
17299 ** structure. For global built-in functions (ex: substr(), max(), count())
17301 ** For per-connection application-defined functions, a pointer to this
17302 ** structure is held in the db->aHash hash table.
17304 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17305 ** field is used by per-connection app-def functions.
17308 i8 nArg; /* Number of arguments. -1 means unlimited */
17312 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17315 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17324 ** This structure encapsulates a user-function destructor callback (as
17361 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17364 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17365 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17366 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17367 /* 0x0200 -- available for reuse */
17368 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17372 ** single query - might change over time */
17373 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17374 /* 0x8000 -- available for reuse */
17375 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17378 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17380 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17381 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17384 /* Identifier numbers for each in-line function */
17402 ** as the user-data (sqlite3_user_data()) for the function. If
17413 ** zName is the name of a function that is implemented by in-line
17419 ** zName is the name of a test-only function implemented by in-line
17428 ** a single query. The iArg is ignored. The user-data is always set
17432 ** For math-library functions. xPtr is an arbitrary pointer.
17437 ** ignored and the user-data for these functions is set to an
17438 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17456 ** available as the function user-data (sqlite3_user_data()). The
17520 char *zName; /* Savepoint name (nul-terminated) */
17581 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17612 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17632 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17644 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17697 ** database connections, even when the rest of the in-memory database
17707 ** database schema are initially stored in a linked-list pointed to by
17714 ** When an in-memory Table object is deleted (for example when the
17764 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17795 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17796 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17806 #define TF_Readonly 0x00000001 /* Read-only system table */
17817 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
17818 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
17833 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
17834 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
17842 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17844 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
17852 ** only works for non-virtual tables (ordinary tables and views) and is
17857 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17858 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17860 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17869 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17870 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17885 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17888 ** from-table == child-table
17889 ** to-table == parent-table
17892 ** which is attached to the from-table. The to-table need not exist when
17893 ** the from-table is created. The existence of the to-table is not checked.
17907 /* EV: R-30323-21917 */
17973 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
18001 ** pKeyInfo->nField.
18005 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
18009 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
18010 ** multiple entries in the b-tree with the same key (when only looking
18011 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
18016 ** get and equal results when comparing this structure to a b-tree record.
18020 ** b-tree.
18023 KeyInfo *pKeyInfo; /* Collation and sort-order information */
18062 ** algorithm to employ when an attempt is made to insert a non-unique
18069 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
18071 ** be true (note-20221022-a). If a column beyond the 63rd column of the
18072 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
18082 ** number (it cannot - the database page is not allocated until the VDBE
18107 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18118 IndexSample *aSample; /* Samples of the left-most key */
18119 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
18120 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
18134 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18137 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18142 #define XN_ROWID (-1) /* Indexed column is the rowid */
18143 #define XN_EXPR (-2) /* Indexed column is an expression */
18175 const char *z; /* Text of the token. Not NULL-terminated! */
18198 int sortingIdxPTab; /* Cursor number of pseudo-table */
18226 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18227 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18228 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18233 ** the option is available (at compile-time).
18248 ** to represent the greater-than-or-equal-to operator in the expression
18262 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18281 ** subquery gives a constant result, then iTable is -1. If the subquery
18286 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18317 int iValue; /* Non-negative integer value if EP_IntValue */
18342 ** TK_TRIGGER: 1 -> new, 0 -> old
18347 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18350 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18414 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18415 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18416 #define ExprSetProperty(E,P) (E)->flags|=(P)
18417 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18418 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18419 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18424 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18425 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18426 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18427 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18428 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18429 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18430 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18439 ** processes but is a no-op for delivery.
18442 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18443 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18444 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18464 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18474 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18490 ** ---------- -------------------------
18549 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18558 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18569 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18580 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18581 ** contains more than 63 columns and the 64-th or later column is used.
18599 int regResult; /* Registers holding results of a co-routine */
18604 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18605 unsigned isCorrelated :1; /* True if sub-query is correlated */
18607 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18613 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18625 ExprList *pFuncArg; /* Arguments to table-valued-function */
18675 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18678 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18681 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18710 ** NameContexts can be nested. When resolving names, the inner-most
18726 ExprList *pEList; /* Optional list of result-set columns */
18767 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
18775 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18776 ** conflict-target clause.) The pUpsertTargetWhere is the optional
18880 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
18886 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
18894 ** identified by pDest->iSDParm.
18896 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
18898 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18903 ** the side-effects of functions.
18910 ** in register pDest->iSDParm then abandon the rest
18914 ** row of result as the key in table pDest->iSDParm.
18915 ** Apply the affinity pDest->affSdst before storing
18918 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18924 ** SRT_Coroutine Generate a co-routine that returns a new row of
18926 ** of the co-routine is stored in register pDest->iSDParm
18927 ** and the result row is stored in pDest->nDest registers
18928 ** starting with pDest->iSdst.
18930 ** SRT_Table Store results in temporary table pDest->iSDParm.
18936 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18937 ** But also use temporary table pDest->iSDParm+1 as
18941 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
18945 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18947 ** index at pDest->iSDParm+1 hold all prior stores.
18950 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18951 ** table is an intkey table - in this case the first
18953 ** key. If (pDest->iSDParm>0), then the table is an index
18954 ** table. (pDest->iSDParm) is the number of key columns in
18966 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
18972 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
19000 ** the code generator needs. We have to keep per-table autoincrement
19019 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
19100 ** The nTableLock and aTableLock variables are only used if the shared-cache
19101 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
19102 ** used to store the set of table-locks required by the statement being
19132 ** of the base register during check-constraint eval */
19143 int nMaxArg; /* Max args passed to user function by sub-program */
19147 TableLock *aTableLock; /* Required table locks for shared-cache mode */
19192 int nHeight; /* Expression tree height of current sub-select */
19204 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19226 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19228 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19237 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19243 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19249 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19273 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19277 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19281 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19316 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19317 the <column-list> is stored here */
19336 ** that is a part of a trigger-program.
19341 ** the first step of the trigger-program.
19348 ** orconf -> stores the ON CONFLICT algorithm
19349 ** pSelect -> The content to be inserted - either a SELECT statement or
19351 ** zTarget -> Dequoted name of the table to insert into.
19352 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19353 ** statement, then this stores the column-names to be
19355 ** pUpsert -> The ON CONFLICT clauses for an Upsert
19358 ** zTarget -> Dequoted name of the table to delete from.
19359 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19363 ** zTarget -> Dequoted name of the table to update.
19364 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19366 ** pExprList -> A list of the columns to update and the expressions to update
19371 ** pSelect -> The SELECT statement
19374 ** pExprList -> The list of expressions that follow the RETURNING keyword.
19390 TriggerStep *pNext; /* Next in the link-list */
19391 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19420 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19424 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19450 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19453 ** be used on trunk check-ins. They are a temporary mechanism available
19458 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
19460 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
19475 u8 bUseCis; /* Use covering indices for full-scans */
19479 int neverCorrupt; /* Database is always well-formed */
19482 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19483 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19484 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19485 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19495 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19497 /* The above might be initialized to non-zero. The following need to always
19528 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19530 /* vvvv--- must be last ---vvv */
19538 ** the assert is only valid on a well-formed database. Instead of:
19550 ** things that are always true for well-formed databases.
19555 ** Context pointer passed down through the tree-walk.
19596 const char *zType; /* Type of the container - used for error messages */
19597 const Token *pName; /* Name of the container - used for error messages */
19624 ** Return code from the parse-tree walking primitives and their
19685 ** data structures on sqlite3DebugPrintf() using a tree-like view.
19702 ** (2) All window functions in a single SELECT form a linked-list
19772 ** Assuming zIn points to the first byte of a UTF-8 character,
19773 ** advance zIn to point to the first byte of the next UTF-8 character.
19784 ** routines that report the line-number on which the error originated
19829 ** The ctype.h header is needed for non-ASCII systems. It is also
19963 # define MAN754 ((((u64)1)<<52)-1)
20103 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
20104 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
20113 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20377 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20378 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20440 ** Routines to read and write variable-length integers. These used to
20655 ** The interface to the LEMON-generated parser
20716 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20795 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20797 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20799 ** provided (enforcement of FK constraints requires the triggers sub-system).
20850 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
20903 ** sqlite3IoTrace is a pointer to a printf-like routine used to
20936 ** passed back to non-lookaside free() routines. Asserts such as the
20937 ** example above are placed on the non-lookaside free() routines to verify
20940 ** All of this is no-op for a production build. It only comes into
20941 ** play when the SQLITE_MEMDEBUG compile-time option is used.
20948 # define sqlite3MemdebugSetType(X,Y) /* no-op */
21002 ** all of the platform-specific files (os_*.c) and is #included into those
21014 ** switch. The following code should catch this problem at compile-time.
21028 ** high-performance timing routines.
21044 ** This file contains inline asm code for retrieving "high-performance"
21051 ** The following routine only works on pentium-class (or newer) processors.
21053 ** processor and returns that value. This can be used for high-res
21124 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
21148 || sqlite3_io_error_pending-- == 1 ) \
21163 sqlite3_diskfull_pending--; \
21210 ** This file implements routines used to report what compile-time options
21213 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
21217 ** autoconf-based build
21231 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21237 ** An array of names of all compile-time options. This array should
21238 ** be sorted A-Z.
21241 ** only a handful of compile-time options, so most times this array is usually
21279 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21283 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21285 "COMPILER=gcc-" __VERSION__,
22002 /* An array to map all upper-case characters into their corresponding
22003 ** lower-case character.
22005 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
22045 /* All of the upper-to-lower conversion data is above. The following
22062 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
22063 ** be undefined behavior. That's goofy, but the C-standards people thought
22071 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
22072 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
22073 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
22076 ** The following 256 byte lookup table is used to support SQLites built-in
22089 ** case. i.e. if the character is a lower-case ASCII character.
22090 ** If x is a lower-case ASCII character, then its upper-case equivalent
22091 ** is (x - 0x20). Therefore toupper() can be implemented as:
22098 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
22100 ** non-ASCII UTF character. Hence the test for whether or not a character is
22109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
22141 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
22145 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
22146 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
22148 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
22163 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
22164 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
22165 ** that compile-time option is omitted.
22171 # error "Compile-time disabling of covering index scan using the\
22172 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
22188 ** before 3.12.0). -1 means always keep the entire statement journal in
22198 ** The default lookaside-configuration, the format "SZ,N". SZ is the
22200 ** and N is the number of slots. The lookaside-configuration can be
22201 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
22202 ** or at run-time for an individual database connection using
22205 ** With the two-size-lookaside enhancement, less lookaside is required.
22206 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
22207 ** and 93 128-byte slots, which is more lookaside than is available
22208 ** using the older 1200,100 configuration without two-size-lookaside.
22219 /* The default maximum size of an in-memory database created using
22291 ** Hash table for global functions - functions common to all
22293 ** read-only.
22312 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22319 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
22476 ** * A b-tree cursor
22477 ** - In the main database or in an ephemeral database
22478 ** - On either an index or a table
22481 ** * A one-row "pseudotable" stored in a single register
22486 i8 iDb; /* Index of cursor database in db->aDb[] */
22495 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22545 /* Return true if P is a null-only cursor
22548 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
22557 ** When a sub-program is executed (OP_Program), a structure of this type
22560 ** values stored in the Vdbe struct. When the sub-program is finished,
22562 ** restoring the state of the VM to as it was before the sub-program
22599 i64 nDbChange; /* Value of db->nChange */
22634 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22653 ** UPDATE no-change flag set
22659 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22663 ** length Mem.n. Zero-terminated if
22719 /* Return TRUE if Mem X contains dynamically allocated content - anything
22723 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
22729 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
22732 ** True if Mem X is a NULL-nochng type.
22735 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
22736 && (X)->n==0 && (X)->u.nZero==0)
22747 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
22804 int iSelectID; /* The "Select-ID" for this loop */
22809 /* The DblquoteStr object holds the text of a double-quoted
22813 ** list is consulted for each double-quoted identifier to see if the
22861 i64 startTime; /* Time when query started - used for profiling */
22874 bft changeCntOn:1; /* True to update the change-counter */
22878 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
22884 DblquoteStr *pDblStr; /* List of double-quoted string literals */
22891 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
22939 ** set of values on the right-hand side of an IN constraint.
22955 ** single-byte varint.
23110 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
23155 ** we have to locate the state vector at run-time. In the more common
23185 ** The value of N is added to the current status value and the high-water
23189 ** mark is unchanged. N must be non-negative for StatusDown().
23209 wsdStat.nowValue[op] -= N;
23281 p = p->pNext;
23291 u32 nInit = countLookasideSlots(db->lookaside.pInit);
23292 u32 nFree = countLookasideSlots(db->lookaside.pFree);
23294 nInit += countLookasideSlots(db->lookaside.pSmallInit);
23295 nFree += countLookasideSlots(db->lookaside.pSmallFree);
23297 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
23298 return db->lookaside.nSlot - (nInit+nFree);
23308 int *pHighwater, /* Write high-water mark here */
23309 int resetFlag /* Reset high-water mark if true */
23317 sqlite3_mutex_enter(db->mutex);
23322 LookasideSlot *p = db->lookaside.pFree;
23324 while( p->pNext ) p = p->pNext;
23325 p->pNext = db->lookaside.pInit;
23326 db->lookaside.pInit = db->lookaside.pFree;
23327 db->lookaside.pFree = 0;
23330 p = db->lookaside.pSmallFree;
23332 while( p->pNext ) p = p->pNext;
23333 p->pNext = db->lookaside.pSmallInit;
23334 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
23335 db->lookaside.pSmallFree = 0;
23348 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
23349 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
23351 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
23353 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
23368 for(i=0; i<db->nDb; i++){
23369 Btree *pBt = db->aDb[i].pBt;
23395 db->pnBytesFreed = &nByte;
23396 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23397 db->lookaside.pEnd = db->lookaside.pStart;
23398 for(i=0; i<db->nDb; i++){
23399 Schema *pSchema = db->aDb[i].pSchema;
23404 pSchema->tblHash.count
23405 + pSchema->trigHash.count
23406 + pSchema->idxHash.count
23407 + pSchema->fkeyHash.count
23409 nByte += sqlite3_msize(pSchema->tblHash.ht);
23410 nByte += sqlite3_msize(pSchema->trigHash.ht);
23411 nByte += sqlite3_msize(pSchema->idxHash.ht);
23412 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
23414 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
23417 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
23422 db->pnBytesFreed = 0;
23423 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23440 db->pnBytesFreed = &nByte;
23441 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
23442 db->lookaside.pEnd = db->lookaside.pStart;
23443 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
23446 db->lookaside.pEnd = db->lookaside.pTrueEnd;
23447 db->pnBytesFreed = 0;
23449 *pHighwater = 0; /* IMP: R-64479-57858 */
23471 for(i=0; i<db->nDb; i++){
23472 if( db->aDb[i].pBt ){
23473 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
23477 *pHighwater = 0; /* IMP: R-42420-56072 */
23478 /* IMP: R-54100-20147 */
23479 /* IMP: R-29431-39229 */
23484 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
23489 *pHighwater = 0; /* IMP: R-11967-56545 */
23490 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
23498 sqlite3_mutex_leave(db->mutex);
23518 ** There is only one exported symbol in this file - the function
23527 ** 1970-01-01 00:00:00 is JD 2440587.5
23528 ** 2000-01-01 00:00:00 is JD 2451544.5
23530 ** This implementation requires years to be expressed as a 4-digit number
23531 ** which means that only dates between 0000-01-01 and 9999-12-31 can
23537 ** use the julian calendar for dates prior to 1582-10-15 and for some
23545 ** ISBN 0-943396-61-1
23546 ** Willmann-Bell, Inc
23573 int h, m; /* Hour and minutes */
23574 int tz; /* Timezone offset in minutes */
23592 ** of a four-character format specifiers ABCD is:
23606 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23607 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23608 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23609 ** the 2-digit day which is the last integer in the set.
23622 char N = zFormat[0] - '0';
23623 char min = zFormat[1] - '0';
23628 max = aMx[zFormat[2] - 'a'];
23631 while( N-- ){
23635 val = val*10 + *zDate - '0';
23652 ** Parse a timezone extension on the end of a date-time.
23655 ** (+/-)HH:MM
23661 ** If the parse is successful, write the number of minutes
23662 ** of change in p->tz and return 0. If a parser error occurs,
23663 ** return non-zero.
23672 p->tz = 0;
23674 if( c=='-' ){
23675 sgn = -1;
23689 p->tz = sgn*(nMn + nHr*60);
23692 p->tzSet = 1;
23720 ms = ms*10.0 + *zDate - '0';
23729 p->validJD = 0;
23730 p->rawS = 0;
23731 p->validHMS = 1;
23732 p->h = h;
23733 p->m = m;
23734 p->s = s + ms;
23736 p->validTZ = (p->tz!=0)?1:0;
23745 p->isError = 1;
23749 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
23750 ** that the YYYY-MM-DD is according to the Gregorian calendar.
23757 if( p->validJD ) return;
23758 if( p->validYMD ){
23759 Y = p->Y;
23760 M = p->M;
23761 D = p->D;
23763 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
23767 if( Y<-4713 || Y>9999 || p->rawS ){
23772 Y--;
23776 B = 2 - A + (A/4);
23779 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23780 p->validJD = 1;
23781 if( p->validHMS ){
23782 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5);
23783 if( p->validTZ ){
23784 p->iJD -= p->tz*60000;
23785 p->validYMD = 0;
23786 p->validHMS = 0;
23787 p->validTZ = 0;
23795 ** YYYY-MM-DD HH:MM:SS.FFF
23796 ** YYYY-MM-DD HH:MM:SS
23797 ** YYYY-MM-DD HH:MM
23798 ** YYYY-MM-DD
23801 ** on success and 1 if the input string is not a well-formed
23807 if( zDate[0]=='-' ){
23813 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
23821 p->validHMS = 0;
23825 p->validJD = 0;
23826 p->validYMD = 1;
23827 p->Y = neg ? -Y : Y;
23828 p->M = M;
23829 p->D = D;
23830 if( p->validTZ ){
23842 p->iJD = sqlite3StmtCurrentTime(context);
23843 if( p->iJD>0 ){
23844 p->validJD = 1;
23855 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23858 p->s = r;
23859 p->rawS = 1;
23861 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
23862 p->validJD = 1;
23872 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23876 ** In the first form, the +/-HH:MM is always optional. The fractional
23901 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
23924 if( p->validYMD ) return;
23925 if( !p->validJD ){
23926 p->Y = 2000;
23927 p->M = 1;
23928 p->D = 1;
23929 }else if( !validJulianDay(p->iJD) ){
23933 Z = (int)((p->iJD + 43200000)/86400000);
23934 A = (int)((Z - 1867216.25)/36524.25);
23935 A = Z + 1 + A - (A/4);
23937 C = (int)((B - 122.1)/365.25);
23939 E = (int)((B-D)/30.6001);
23941 p->D = B - D - X1;
23942 p->M = E<14 ? E-1 : E-13;
23943 p->Y = p->M>2 ? C - 4716 : C - 4715;
23945 p->validYMD = 1;
23953 if( p->validHMS ) return;
23955 s = (int)((p->iJD + 43200000) % 86400000);
23956 p->s = s/1000.0;
23957 s = (int)p->s;
23958 p->s -= s;
23959 p->h = s/3600;
23960 s -= p->h*3600;
23961 p->m = s/60;
23962 p->s += s - p->m*60;
23963 p->rawS = 0;
23964 p->validHMS = 1;
23979 p->validYMD = 0;
23980 p->validHMS = 0;
23981 p->validTZ = 0;
23991 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
24005 ** using whatever operating-system specific localtime facility that
24007 ** non-zero on any kind of error.
24009 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
24012 ** invoked in place of the OS-defined localtime() function.
24014 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
24080 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
24081 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
24083 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
24090 iYearDiff = (2000 + x.Y%4) - x.Y;
24094 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
24097 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
24100 sqlite3_result_error(pCtx, "local time unavailable", -1);
24103 p->Y = sLocal.tm_year + 1900 - iYearDiff;
24104 p->M = sLocal.tm_mon + 1;
24105 p->D = sLocal.tm_mday;
24106 p->h = sLocal.tm_hour;
24107 p->m = sLocal.tm_min;
24108 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
24109 p->validYMD = 1;
24110 p->validHMS = 1;
24111 p->validJD = 0;
24112 p->rawS = 0;
24113 p->validTZ = 0;
24114 p->isError = 0;
24124 ** Where NNN is an arbitrary floating-point number and "days" can be one
24142 ** Process a modifier to a date-time stamp. The modifiers are
24147 ** NNN minutes
24183 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
24184 if( !p->rawS || p->validJD ){
24186 p->rawS = 0;
24187 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
24188 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
24190 r = p->s*1000.0 + 210866760000000.0;
24192 p->iJD = (sqlite3_int64)(r + 0.5);
24193 p->validJD = 1;
24194 p->rawS = 0;
24204 ** Always interpret the prior number as a julian-day value. If this
24210 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
24211 if( p->validJD && p->rawS ){
24213 p->rawS = 0;
24235 ** Treat the current value of p->s as the number of
24238 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
24239 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
24240 r = p->s*1000.0 + 210866760000000.0;
24243 p->iJD = (sqlite3_int64)(r + 0.5);
24244 p->validJD = 1;
24245 p->rawS = 0;
24251 if( p->tzSet==0 ){
24258 iGuess = iOrigJD = p->iJD;
24263 iGuess -= iErr;
24269 iErr = new.iJD - iOrigJD;
24272 p->iJD = iGuess;
24273 p->validJD = 1;
24274 p->tzSet = 1;
24287 ** date is already on the appropriate weekday, this is a no-op.
24294 p->validTZ = 0;
24295 p->validJD = 0;
24297 Z = ((p->iJD + 129600000)/86400000) % 7;
24298 if( Z>n ) Z -= 7;
24299 p->iJD += (n - Z)*86400000;
24313 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
24316 p->validHMS = 1;
24317 p->h = p->m = 0;
24318 p->s = 0.0;
24319 p->rawS = 0;
24320 p->validTZ = 0;
24321 p->validJD = 0;
24323 p->D = 1;
24326 p->M = 1;
24327 p->D = 1;
24335 case '-':
24354 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
24355 ** specified number of hours, minutes, seconds, and fractional seconds
24366 tx.iJD -= 43200000;
24368 tx.iJD -= day*86400000;
24369 if( z[0]=='-' ) tx.iJD = -tx.iJD;
24372 p->iJD += tx.iJD;
24383 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
24386 rRounder = r<0 ? -0.5 : +0.5;
24390 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
24397 p->M += (int)r;
24398 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24399 p->Y += x;
24400 p->M -= x*12;
24401 p->validJD = 0;
24402 r -= (int)r;
24409 p->Y += y;
24410 p->validJD = 0;
24411 r -= (int)r;
24416 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
24432 ** Process time function arguments. argv[0] is a date-time stamp.
24469 if( p->isError || !validJulianDay(p->iJD) ) return 1;
24500 ** the unix epoch of 1970-01-01 00:00:00 GMT.
24510 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
24517 ** Return YYYY-MM-DD HH:MM:SS
24530 if( Y<0 ) Y = -Y;
24535 zBuf[5] = '-';
24538 zBuf[8] = '-';
24553 zBuf[0] = '-';
24593 ** Return YYYY-MM-DD
24606 if( Y<0 ) Y = -Y;
24611 zBuf[5] = '-';
24614 zBuf[8] = '-';
24619 zBuf[0] = '-';
24634 ** %H hour 00-24
24635 ** %j day of year 000-366
24637 ** %m month 01-12
24638 ** %M minute 00-59
24639 ** %s seconds since 1970-01-01
24640 ** %S seconds 00-59
24641 ** %w day of week 0-6 sunday==0
24642 ** %W week of year 00-53
24643 ** %Y year 0000-9999
24660 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
24662 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
24668 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24694 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
24698 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
24717 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
24744 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24793 ** If the library is compiled to omit the full-scale date and time
24799 ** This function uses the C-library functions time(), gmtime()
24801 ** as the user-data for the function.
24820 t = iT/1000 - 10000*(sqlite3_int64)21086676;
24831 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
24844 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
24845 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ),
24846 PURE_DATE(date, -1, 0, 0, dateFunc ),
24847 PURE_DATE(time, -1, 0, 0, timeFunc ),
24848 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
24849 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
24855 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
24856 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
24888 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
24906 ** from within OsOpen()), but some third-party implementations may.
24946 if( pId->pMethods ){
24947 pId->pMethods->xClose(pId);
24948 pId->pMethods = 0;
24953 return id->pMethods->xRead(id, pBuf, amt, offset);
24957 return id->pMethods->xWrite(id, pBuf, amt, offset);
24960 return id->pMethods->xTruncate(id, size);
24964 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
24968 return id->pMethods->xFileSize(id, pSize);
24973 return id->pMethods->xLock(id, lockType);
24977 return id->pMethods->xUnlock(id, lockType);
24981 return id->pMethods->xCheckReservedLock(id, pResOut);
24993 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
25003 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
25011 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
25018 return id->pMethods->xFileControl(id, op, pArg);
25021 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
25025 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
25029 if( NEVER(id->pMethods==0) ) return 0;
25030 return id->pMethods->xDeviceCharacteristics(id);
25034 return id->pMethods->xShmLock(id, offset, n, flags);
25037 id->pMethods->xShmBarrier(id);
25040 return id->pMethods->xShmUnmap(id, deleteFlag);
25050 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
25058 return id->pMethods->xFetch(id, iOff, iAmt, pp);
25061 return id->pMethods->xUnfetch(id, iOff, p);
25064 /* No-op stubs to use when memory-mapped I/O is disabled */
25092 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
25093 assert( rc==SQLITE_OK || pFile->pMethods==0 );
25099 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK;
25108 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
25118 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
25123 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */
25124 return pVfs->xDlOpen(pVfs, zPath);
25127 pVfs->xDlError(pVfs, nByte, zBufOut);
25130 return pVfs->xDlSym(pVfs, pHdle, zSym);
25133 pVfs->xDlClose(pVfs, pHandle);
25143 return pVfs->xRandomness(pVfs, nByte, zBufOut);
25148 return pVfs->xSleep(pVfs, nMicro);
25151 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
25155 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
25161 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
25162 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
25165 rc = pVfs->xCurrentTime(pVfs, &r);
25180 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
25238 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
25240 if( strcmp(zVfs, pVfs->zName)==0 ) break;
25252 /* No-op */
25254 vfsList = pVfs->pNext;
25257 while( p->pNext && p->pNext!=pVfs ){
25258 p = p->pNext;
25260 if( p->pNext==pVfs ){
25261 p->pNext = pVfs->pNext;
25285 pVfs->pNext = vfsList;
25288 pVfs->pNext = vfsList->pNext;
25289 vfsList->pNext = pVfs;
25331 ** Most malloc failures are non-benign. After they occur, SQLite
25355 ** we have to locate the state vector at run-time. In the more common
25385 ** indicates that subsequent malloc failures are non-benign.
25416 ** This file contains a no-op memory allocation drivers for use when
25426 ** used when no other memory allocator is specified using compile-time
25432 ** No-op versions of all memory allocation routines
25445 ** Populate the low-level memory allocation function pointers in
25478 ** This file contains low-level memory allocation drivers for when
25479 ** SQLite will use the standard C-library malloc/realloc/free interface
25482 ** This file contains implementations of the low-level memory allocation
25490 ** C-preprocessor macro summary:
25497 ** a different name, using a separate -D
25513 ** used when no other memory allocator is specified using compile-time
25533 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
25538 ** Use standard C library malloc and free on non-Apple systems.
25555 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
25589 ** For this low-level routine, we are guaranteed that nByte>0 because
25623 ** For this low-level routine, we already know that pPrior!=0 since
25625 ** by higher-level routines.
25633 p--;
25650 p--;
25659 ** For this low-level interface, we know that pPrior!=0. Cases where
25660 ** pPrior==0 while have been intercepted by higher-level routine and
25662 ** cases where nByte<=0 will have been intercepted by higher-level
25678 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
25679 p--;
25739 ** Populate the low-level memory allocation function pointers in
25772 ** This file contains low-level memory allocation drivers for when
25773 ** SQLite will use the standard C-library malloc/realloc/free interface
25778 ** This file contains implementations of the low-level memory allocation
25804 ** ------------------------------------------------------------------------
25806 ** ------------------------------------------------------------------------
25890 if( i>NCSIZE-1 ){
25891 i = NCSIZE - 1;
25900 mem.nCurrent[i]--;
25918 p--;
25919 assert( p->iForeGuard==(int)FOREGUARD );
25920 nReserve = ROUND8(p->iSize);
25928 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
25941 return (int)pHdr->iSize;
25974 ** Fill a buffer with pseudo-random bytes. This is used to preset
25983 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25988 nByte -= 4;
25990 while( nByte-- > 0 ){
25991 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
26019 pHdr->pNext = 0;
26020 pHdr->pPrev = mem.pLast;
26022 mem.pLast->pNext = pHdr;
26027 pHdr->iForeGuard = FOREGUARD;
26028 pHdr->eType = MEMTYPE_HEAP;
26029 pHdr->nBacktraceSlots = mem.nBacktrace;
26030 pHdr->nTitle = mem.nTitle;
26033 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
26034 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
26037 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
26040 pHdr->nBacktrace = 0;
26045 pHdr->iSize = nByte;
26050 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
26068 pBt -= pHdr->nBacktraceSlots;
26070 if( pHdr->pPrev ){
26071 assert( pHdr->pPrev->pNext==pHdr );
26072 pHdr->pPrev->pNext = pHdr->pNext;
26075 mem.pFirst = pHdr->pNext;
26077 if( pHdr->pNext ){
26078 assert( pHdr->pNext->pPrev==pHdr );
26079 pHdr->pNext->pPrev = pHdr->pPrev;
26082 mem.pLast = pHdr->pPrev;
26085 z -= pHdr->nTitle;
26086 adjustStats((int)pHdr->iSize, -1);
26087 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
26088 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
26106 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
26110 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
26111 if( nByte>pOldHdr->iSize ){
26112 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
26120 ** Populate the low-level memory allocation function pointers in
26144 assert( pHdr->iForeGuard==FOREGUARD );
26145 pHdr->eType = eType;
26163 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26164 if( (pHdr->eType&eType)==0 ){
26185 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
26186 if( (pHdr->eType&eType)!=0 ){
26215 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
26224 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26226 pBt -= pHdr->nBacktraceSlots;
26227 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
26246 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
26248 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
26250 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
26251 if( pHdr->nBacktrace ){
26254 pBt -= pHdr->nBacktraceSlots;
26255 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
26260 for(i=0; i<NCSIZE-1; i++){
26266 if( mem.nAlloc[NCSIZE-1] ){
26268 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
26269 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
26321 ** mean that the library will use a memory-pool by default, just that
26362 ** two fields form a double-linked list of chunks of related sizes.
26399 ** True if we are evaluating an out-of-memory callback.
26427 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26460 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26462 size = mem3.aPool[i-1].u.hdr.size4x/4;
26463 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26466 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
26495 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26496 size = mem3.aPool[i-1].u.hdr.size4x/4;
26497 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26500 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
26539 ** size parameters for check-out and return a pointer to the
26546 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
26547 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
26548 x = mem3.aPool[i-1].u.hdr.size4x;
26549 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
26550 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
26551 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
26563 if( nBlock>=mem3.szKeyBlk-1 ){
26573 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
26575 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
26576 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
26577 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
26578 mem3.szKeyBlk -= nBlock;
26579 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
26580 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26581 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26611 size = mem3.aPool[i-1].u.hdr.size4x;
26615 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
26616 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
26621 size = i + size/4 - prev;
26622 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
26623 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
26624 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
26664 i = mem3.aiSmall[nBlock-2];
26666 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
26672 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
26705 for(i=0; i<MX_SMALL-1; i++){
26732 i = p - mem3.aPool;
26733 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
26734 size = mem3.aPool[i-1].u.hdr.size4x/4;
26736 mem3.aPool[i-1].u.hdr.size4x &= ~1;
26737 mem3.aPool[i+size-1].u.hdr.prevSize = size;
26738 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
26743 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
26744 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
26745 mem3.iKeyBlk -= size;
26748 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26749 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26750 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26752 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26753 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
26755 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
26756 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26757 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26764 ** size returned omits the 8-byte header overhead. This only
26771 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
26772 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
26782 return ((n+11)&~7) - 4;
26822 if( nBytes<=nOld && nBytes>=nOld-128 ){
26851 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
26897 size = mem3.aPool[i-1].u.hdr.size4x;
26903 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
26908 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
26914 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
26916 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
26920 for(i=0; i<MX_SMALL-1; i++){
26925 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26934 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26939 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
26940 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
26956 ** Populate the low-level memory allocation function pointers in
27017 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
27026 ** N >= M*(1 + log2(n)/2) - n + 1
27055 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
27090 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
27121 ** structures, return a pointer to the idx-th such link.
27135 next = MEM5LINK(i)->next;
27136 prev = MEM5LINK(i)->prev;
27140 MEM5LINK(prev)->next = next;
27143 MEM5LINK(next)->prev = prev;
27158 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
27159 MEM5LINK(i)->prev = -1;
27162 MEM5LINK(x)->prev = i;
27184 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
27239 iBin--;
27250 mem5.totalExcess += iFullSz - nByte;
27277 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
27279 /* Check that the pointer pOld points to a valid, non-free block. */
27281 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
27286 assert( iBlock+size-1<(u32)mem5.nBlock );
27289 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
27294 mem5.currentCount--;
27295 mem5.currentOut -= size*mem5.szAtom;
27304 iBuddy = iBlock - size;
27366 ** memsys5Round(). Hence nBytes is always a non-negative power
27375 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
27398 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
27420 ** Examples: memsys5Log(1) -> 0
27421 ** memsys5Log(2) -> 1
27422 ** memsys5Log(4) -> 2
27423 ** memsys5Log(5) -> 3
27424 ** memsys5Log(8) -> 3
27425 ** memsys5Log(9) -> 4
27429 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
27454 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
27472 mem5.aiFreelist[ii] = -1;
27476 for(ii=LOGMAX; ii>=0; ii--){
27526 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
27617 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
27628 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
27638 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
27641 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
27672 p->iType = iType;
27675 if( iType-2>=ArraySize(staticMutexes) ){
27680 p = &staticMutexes[iType-2];
27683 if( p->mutex==0 ){
27684 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
27685 if( p->mutex==0 ){
27705 if( ((CheckMutex*)p)->iType<2 )
27709 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
27724 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
27725 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
27729 "illegal multi-threaded access to database connection"
27732 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
27740 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
27748 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
27778 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
27779 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
27807 pTo->xMutexInit = pFrom->xMutexInit;
27808 pTo->xMutexEnd = pFrom->xMutexEnd;
27809 pTo->xMutexFree = pFrom->xMutexFree;
27810 pTo->xMutexEnter = pFrom->xMutexEnter;
27811 pTo->xMutexTry = pFrom->xMutexTry;
27812 pTo->xMutexLeave = pFrom->xMutexLeave;
27813 pTo->xMutexHeld = pFrom->xMutexHeld;
27814 pTo->xMutexNotheld = pFrom->xMutexNotheld;
27816 pTo->xMutexAlloc = pFrom->xMutexAlloc;
27905 ** this function is a no-op.
27949 ** here are place-holders. Applications can substitute working
27950 ** mutex routines at start-time using the
28023 return p==0 || p->cnt>0;
28027 return p==0 || p->cnt==0;
28042 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
28049 pNew->id = id;
28050 pNew->cnt = 0;
28056 if( id-2<0 || id-2>=ArraySize(aStatic) ){
28061 pNew = &aStatic[id-2];
28062 pNew->id = id;
28074 assert( p->cnt==0 );
28075 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
28097 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28098 p->cnt++;
28102 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28103 p->cnt++;
28116 p->cnt--;
28117 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
28139 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
28140 ** is used regardless of the run-time threadsafety setting.
28180 ** home-grown mutexes. Encapsulate these conditions into a single #define.
28223 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
28229 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
28232 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
28285 ** cases where it really needs one. If a faster non-recursive mutex
28326 pthread_mutex_init(&p->mutex, 0);
28332 pthread_mutex_init(&p->mutex, &recursiveAttr);
28336 p->id = SQLITE_MUTEX_RECURSIVE;
28344 pthread_mutex_init(&p->mutex, 0);
28346 p->id = SQLITE_MUTEX_FAST;
28353 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
28358 p = &staticMutexes[iType-2];
28363 assert( p==0 || p->id==iType );
28375 assert( p->nRef==0 );
28377 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
28380 pthread_mutex_destroy(&p->mutex);
28402 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28407 ** is atomic - that it cannot be deceived into thinking self
28408 ** and p->owner are equal if p->owner changes between two values
28410 ** This implementation also assumes a coherent cache - that
28417 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28418 p->nRef++;
28420 pthread_mutex_lock(&p->mutex);
28421 assert( p->nRef==0 );
28422 p->owner = self;
28423 p->nRef = 1;
28427 /* Use the built-in recursive mutexes if they are available.
28429 pthread_mutex_lock(&p->mutex);
28431 assert( p->nRef>0 || p->owner==0 );
28432 p->owner = pthread_self();
28433 p->nRef++;
28438 if( p->trace ){
28439 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28445 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28450 ** is atomic - that it cannot be deceived into thinking self
28451 ** and p->owner are equal if p->owner changes between two values
28453 ** This implementation also assumes a coherent cache - that
28460 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28461 p->nRef++;
28463 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
28464 assert( p->nRef==0 );
28465 p->owner = self;
28466 p->nRef = 1;
28473 /* Use the built-in recursive mutexes if they are available.
28475 if( pthread_mutex_trylock(&p->mutex)==0 ){
28477 p->owner = pthread_self();
28478 p->nRef++;
28487 if( rc==SQLITE_OK && p->trace ){
28488 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28503 p->nRef--;
28504 if( p->nRef==0 ) p->owner = 0;
28506 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
28509 if( p->nRef==0 ){
28510 pthread_mutex_unlock(&p->mutex);
28513 pthread_mutex_unlock(&p->mutex);
28517 if( p->trace ){
28518 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28623 ** Determine if we are dealing with Windows CE - which has a much reduced
28651 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
28706 return p->nRef!=0 && p->owner==GetCurrentThreadId();
28710 return p->nRef==0 || p->owner!=tid;
28755 static int winMutex_isNt = -1; /* <0 means "need to query" */
28833 ** cases where it really needs one. If a faster non-recursive mutex
28859 p->id = iType;
28862 p->trace = 1;
28866 InitializeCriticalSectionEx(&p->mutex, 0, 0);
28868 InitializeCriticalSection(&p->mutex);
28875 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
28880 p = &winMutex_staticMutexes[iType-2];
28883 InterlockedCompareExchange(&p->trace, 1, 0);
28889 assert( p==0 || p->id==iType );
28901 assert( p->nRef==0 && p->owner==0 );
28902 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
28903 DeleteCriticalSection(&p->mutex);
28929 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
28934 EnterCriticalSection(&p->mutex);
28936 assert( p->nRef>0 || p->owner==0 );
28937 p->owner = tid;
28938 p->nRef++;
28939 if( p->trace ){
28940 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28941 tid, p->id, p, p->trace, p->nRef));
28952 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
28966 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
28971 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
28973 p->owner = tid;
28974 p->nRef++;
28982 if( p->trace ){
28983 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
28984 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
29002 assert( p->nRef>0 );
29003 assert( p->owner==tid );
29004 p->nRef--;
29005 if( p->nRef==0 ) p->owner = 0;
29006 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
29009 LeaveCriticalSection(&p->mutex);
29011 if( p->trace ){
29012 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
29013 tid, p->id, p, p->trace, p->nRef));
29060 ** Attempt to release up to n bytes of non-essential memory currently
29061 ** held by SQLite. An example of non-essential memory is memory used to
29068 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
29069 ** is a no-op returning zero if SQLite is not compiled with
29111 ** no-op.
29126 ** Set the soft heap-size limit for the library. An argument of
29127 ** zero disables the limit. A negative argument is a no-op used to
29142 if( rc ) return -1;
29157 excess = sqlite3_memory_used() - n;
29167 ** Set the hard heap-size limit for the library. An argument of zero
29168 ** disables the hard heap limit. A negative argument is a no-op used
29182 if( rc ) return -1;
29217 ** Return true if the heap is currently under memory pressure - in other
29275 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
29285 if( nUsed >= mem0.alarmThreshold - nFull ){
29290 if( nUsed >= mem0.hardLimit - nFull ){
29321 ** This provides a 256-byte safety margin for defense against 32-bit
29350 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
29377 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd);
29393 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
29395 return db->lookaside.szTrue;
29410 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){
29412 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29413 assert( sqlite3_mutex_held(db->mutex) );
29417 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29418 assert( sqlite3_mutex_held(db->mutex) );
29419 return db->lookaside.szTrue;
29435 if( p==0 ) return; /* IMP: R-49053-54554 */
29451 ** *db->pnBytesFreed.
29454 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
29459 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29460 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29463 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29466 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29468 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29470 assert( db->pnBytesFreed==0 );
29474 pBuf->pNext = db->lookaside.pSmallFree;
29475 db->lookaside.pSmallFree = pBuf;
29479 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29481 assert( db->pnBytesFreed==0 );
29483 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29485 pBuf->pNext = db->lookaside.pFree;
29486 db->lookaside.pFree = pBuf;
29490 if( db->pnBytesFreed ){
29503 assert( sqlite3_mutex_held(db->mutex) );
29505 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29507 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29509 assert( db->pnBytesFreed==0 );
29513 pBuf->pNext = db->lookaside.pSmallFree;
29514 db->lookaside.pSmallFree = pBuf;
29518 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29520 assert( db->pnBytesFreed==0 );
29522 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29524 pBuf->pNext = db->lookaside.pFree;
29525 db->lookaside.pFree = pBuf;
29529 if( db->pnBytesFreed ){
29539 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29552 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
29555 sqlite3_free(pOld); /* IMP: R-26507-47431 */
29563 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
29573 nDiff = nNew - nOld;
29575 mem0.alarmThreshold-nDiff ){
29577 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
29591 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
29597 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
29609 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
29653 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
29662 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
29690 assert( sqlite3_mutex_held(db->mutex) );
29691 assert( db->pnBytesFreed==0 );
29692 if( n>db->lookaside.sz ){
29693 if( !db->lookaside.bDisable ){
29694 db->lookaside.anStat[1]++;
29695 }else if( db->mallocFailed ){
29702 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
29703 db->lookaside.pSmallFree = pBuf->pNext;
29704 db->lookaside.anStat[0]++;
29706 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
29707 db->lookaside.pSmallInit = pBuf->pNext;
29708 db->lookaside.anStat[0]++;
29713 if( (pBuf = db->lookaside.pFree)!=0 ){
29714 db->lookaside.pFree = pBuf->pNext;
29715 db->lookaside.anStat[0]++;
29717 }else if( (pBuf = db->lookaside.pInit)!=0 ){
29718 db->lookaside.pInit = pBuf->pNext;
29719 db->lookaside.anStat[0]++;
29722 db->lookaside.anStat[2]++;
29726 assert( sqlite3_mutex_held(db->mutex) );
29727 assert( db->pnBytesFreed==0 );
29728 if( db->mallocFailed ){
29745 assert( sqlite3_mutex_held(db->mutex) );
29746 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
29748 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
29752 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
29753 if( n<=db->lookaside.szTrue ) return p;
29762 if( db->mallocFailed==0 ){
29778 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
29838 n = (int)(zEnd - zStart);
29839 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
29853 ** Call this routine to record the fact that an OOM (out-of-memory) error
29854 ** has happened. This routine will set db->mallocFailed, and also
29866 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
29867 db->mallocFailed = 1;
29868 if( db->nVdbeExec>0 ){
29869 AtomicStore(&db->u1.isInterrupted, 1);
29872 if( db->pParse ){
29874 sqlite3ErrorMsg(db->pParse, "out of memory");
29875 db->pParse->rc = SQLITE_NOMEM_BKPT;
29876 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){
29877 pParse->nErr++;
29878 pParse->rc = SQLITE_NOMEM;
29887 ** db->mallocFailed flag as necessary.
29893 if( db->mallocFailed && db->nVdbeExec==0 ){
29894 db->mallocFailed = 0;
29895 AtomicStore(&db->u1.isInterrupted, 0);
29896 assert( db->lookaside.bDisable>0 );
29905 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
29910 return rc & db->errMask;
29922 ** If an OOM as occurred, then the connection error-code (the value
29927 ** Otherwise the read (and possible write) of db->mallocFailed
29931 assert( sqlite3_mutex_held(db->mutex) );
29932 if( db->mallocFailed || rc ){
29935 return rc & db->errMask;
29946 ** This file contains code for a set of "printf"-like routines. These
29957 #define etRADIX 0 /* non-decimal integer types. %x %o */
29973 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
29974 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
29981 ** An "etByte" is an 8-bit unsigned value.
30010 static const char aPrefix[] = "-x0\000X0";
30049 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
30050 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
30068 ** 16 (the number of significant digits in a 64-bit float) '0' is
30075 (*cnt)--;
30079 *val = (*val - d)*10.0;
30089 p->accError = eError;
30090 if( p->mxAlloc ) sqlite3_str_reset(p);
30091 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
30098 if( p->nArg<=p->nUsed ) return 0;
30099 return sqlite3_value_int64(p->apArg[p->nUsed++]);
30102 if( p->nArg<=p->nUsed ) return 0.0;
30103 return sqlite3_value_double(p->apArg[p->nUsed++]);
30106 if( p->nArg<=p->nUsed ) return 0;
30107 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
30121 if( pAccum->accError ) return 0;
30122 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
30126 z = sqlite3DbMallocRaw(pAccum->db, n);
30143 ** Hard limit on the precision of floating-point conversions.
30163 etByte flag_leftjustify; /* True if "-" flag is present */
30173 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
30193 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30196 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
30210 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
30223 precision = -1;
30226 case '-': flag_leftjustify = 1; break;
30246 unsigned wx = c - '0';
30248 wx = wx*10 + c - '0';
30260 fmt--;
30272 width = width >= -2147483647 ? -width : 0;
30294 precision = precision >= -2147483647 ? -precision : -1;
30300 px = px*10 + c - '0';
30312 --fmt;
30327 xtype = infop->type;
30338 ** flag_leftjustify TRUE if a '-' is present or if the
30343 ** always non-negative. Zero is the default.
30345 ** is -1.
30350 assert( precision>=(-1) );
30361 if( infop->flags & FLAG_SIGNED ){
30376 testcase( v==(-1) );
30379 prefix = '-';
30399 if( flag_zeropad && precision<width-(prefix!=0) ){
30400 precision = width-(prefix!=0);
30402 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
30413 bufpt = &zOut[nOut-1];
30420 *(--bufpt) = zOrd[x*2+1];
30421 *(--bufpt) = zOrd[x*2];
30424 const char *cset = &aDigits[infop->charset];
30425 u8 base = infop->base;
30427 *(--bufpt) = cset[longvalue%base];
30431 length = (int)(&zOut[nOut-1]-bufpt);
30433 *(--bufpt) = '0'; /* Zero pad */
30437 int nn = (length - 1)/3; /* Number of "," to insert */
30438 int ix = (length - 1)%3 + 1;
30439 bufpt -= nn;
30442 ix--;
30445 nn--;
30450 if( prefix ) *(--bufpt) = prefix; /* Add sign */
30451 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
30454 pre = &aPrefix[infop->prefix];
30455 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
30457 length = (int)(&zOut[nOut-1]-bufpt);
30477 realvalue = -realvalue;
30478 prefix = '-';
30482 if( xtype==etGENERIC && precision>0 ) precision--;
30486 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
30492 ex = -1023 + (int)((u>>52)&0x7ff);
30493 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
30509 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
30510 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
30530 if( exp<-4 || exp>precision ){
30533 precision = precision - exp;
30563 for(; e2>=0; e2--){
30573 for(e2++; e2<0; precision--, e2++){
30578 while( (precision--)>0 ){
30583 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
30585 if( bufpt[-1]=='.' ){
30589 *(--bufpt) = 0;
30595 *(bufpt++) = aDigits[infop->charset];
30597 *(bufpt++) = '-'; exp = -exp;
30613 length = (int)(bufpt-zOut);
30620 int nPad = width - length;
30621 for(i=width; i>=nPad; i--){
30622 bufpt[i] = bufpt[i-nPad];
30625 while( nPad-- ) bufpt[i++] = '0';
30632 *(va_arg(ap,int*)) = pAccum->nChar;
30678 width -= precision-1;
30680 sqlite3_str_appendchar(pAccum, width-1, ' ');
30683 while( precision-- > 1 ){
30701 if( pAccum->nChar==0
30702 && pAccum->mxAlloc
30705 && pAccum->accError==0
30710 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30711 pAccum->zText = bufpt;
30712 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
30713 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
30714 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
30725 while( precision-- > 0 && z[0] ){
30728 length = (int)(z - (unsigned char*)bufpt);
30737 /* Adjust width to account for extra bytes in UTF-8 characters */
30738 int ii = length - 1;
30739 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
30764 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
30791 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30796 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
30797 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
30803 if( pToken && pToken->n ){
30804 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
30805 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
30813 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30816 if( pItem->zAlias && !flag_altform2 ){
30817 sqlite3_str_appendall(pAccum, pItem->zAlias);
30818 }else if( pItem->zName ){
30819 if( pItem->zDatabase ){
30820 sqlite3_str_appendall(pAccum, pItem->zDatabase);
30823 sqlite3_str_appendall(pAccum, pItem->zName);
30824 }else if( pItem->zAlias ){
30825 sqlite3_str_appendall(pAccum, pItem->zAlias);
30827 Select *pSel = pItem->pSelect;
30829 if( pSel->selFlags & SF_NestedFrom ){
30830 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
30832 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
30851 width -= length;
30861 sqlite3DbFree(pAccum->db, zExtra);
30880 if( db->errByteOffset!=(-2) ) return;
30881 pParse = db->pParse;
30883 zText =pParse->zTail;
30887 db->errByteOffset = (int)(z-zText);
30897 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
30899 pExpr = pExpr->pLeft;
30902 db->errByteOffset = pExpr->w.iOfst;
30914 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
30915 if( p->accError ){
30916 testcase(p->accError==SQLITE_TOOBIG);
30917 testcase(p->accError==SQLITE_NOMEM);
30920 if( p->mxAlloc==0 ){
30922 return p->nAlloc - p->nChar - 1;
30924 char *zOld = isMalloced(p) ? p->zText : 0;
30925 i64 szNew = p->nChar;
30927 if( szNew+p->nChar<=p->mxAlloc ){
30930 szNew += p->nChar;
30932 if( szNew > p->mxAlloc ){
30937 p->nAlloc = (int)szNew;
30939 if( p->db ){
30940 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
30942 zNew = sqlite3Realloc(zOld, p->nAlloc);
30945 assert( p->zText!=0 || p->nChar==0 );
30946 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
30947 p->zText = zNew;
30948 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
30949 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
30963 testcase( p->nChar + (i64)N > 0x7fffffff );
30964 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
30967 while( (N--)>0 ) p->zText[p->nChar++] = c;
30974 ** This is a helper routine to sqlite3_str_append() that does special-case
30981 memcpy(&p->zText[p->nChar], z, N);
30982 p->nChar += N;
30992 assert( p->zText!=0 || p->nChar==0 || p->accError );
30994 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
30995 if( p->nChar+N >= p->nAlloc ){
30998 assert( p->zText );
30999 p->nChar += N;
31000 memcpy(&p->zText[p->nChar-N], z, N);
31005 ** Append the complete text of zero-terminated string z[] to the p string.
31013 ** Finish off a string by making sure it is zero-terminated.
31019 assert( p->mxAlloc>0 && !isMalloced(p) );
31020 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
31022 memcpy(zText, p->zText, p->nChar+1);
31023 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
31027 p->zText = zText;
31031 if( p->zText ){
31032 p->zText[p->nChar] = 0;
31033 if( p->mxAlloc>0 && !isMalloced(p) ){
31037 return p->zText;
31045 if( p->accError ){
31046 sqlite3_result_error_code(pCtx, p->accError);
31049 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
31081 return p ? p->accError : SQLITE_NOMEM;
31086 return p ? p->nChar : 0;
31091 if( p==0 || p->nChar==0 ) return 0;
31092 p->zText[p->nChar] = 0;
31093 return p->zText;
31101 sqlite3DbFree(p->db, p->zText);
31102 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
31104 p->nAlloc = 0;
31105 p->nChar = 0;
31106 p->zText = 0;
31114 ** memory is used if not NULL. db->mallocFailed is set appropriately
31124 p->zText = zBase;
31125 p->db = db;
31126 p->nAlloc = n;
31127 p->mxAlloc = mx;
31128 p->nChar = 0;
31129 p->accError = 0;
31130 p->printfFlags = 0;
31138 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
31147 ** %-conversion extensions.
31155 db->aLimit[SQLITE_LIMIT_LENGTH]);
31167 ** %-conversion extensions.
31180 ** %-conversion extensions.
31204 ** %-conversion extensions.
31258 ** stack space on small-stack systems when logging is disabled.
31320 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31333 ** 2015-06-08
31365 p->iLevel++;
31368 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
31377 p->iLevel--;
31378 if( p->iLevel<0 ){
31395 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
31396 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
31398 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
31434 int colMoreToFollow = i<(nCol - 1);
31449 printf(" X-%s", z);
31457 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND");
31468 ** Generate a human-readable description of a WITH clause.
31473 if( pWith->nCte==0 ) return;
31474 if( pWith->pOuter ){
31475 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
31479 if( pWith->nCte>0 ){
31481 for(i=0; i<pWith->nCte; i++){
31484 const struct Cte *pCte = &pWith->a[i];
31486 sqlite3_str_appendf(&x, "%s", pCte->zName);
31487 if( pCte->pCols && pCte->pCols->nExpr>0 ){
31490 for(j=0; j<pCte->pCols->nExpr; j++){
31491 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
31496 if( pCte->eM10d!=M10d_Any ){
31498 pCte->eM10d==M10d_No ? "NOT " : "");
31500 if( pCte->pUse ){
31501 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
31502 pCte->pUse->nUse);
31505 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
31506 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
31514 ** Generate a human-readable description of a SrcList object.
31519 for(i=0; i<pSrc->nSrc; i++){
31520 const SrcItem *pItem = &pSrc->a[i];
31526 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
31527 if( pItem->pTab ){
31529 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
31531 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
31532 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
31533 }else if( pItem->fg.jointype & JT_LEFT ){
31534 sqlite3_str_appendf(&x, " LEFT-JOIN");
31535 }else if( pItem->fg.jointype & JT_RIGHT ){
31536 sqlite3_str_appendf(&x, " RIGHT-JOIN");
31537 }else if( pItem->fg.jointype & JT_CROSS ){
31538 sqlite3_str_appendf(&x, " CROSS-JOIN");
31540 if( pItem->fg.jointype & JT_LTORJ ){
31543 if( pItem->fg.fromDDL ){
31546 if( pItem->fg.isCte ){
31547 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
31549 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
31553 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
31555 if( pItem->pSelect ) n++;
31556 if( pItem->fg.isTabFunc ) n++;
31557 if( pItem->fg.isUsing ) n++;
31558 if( pItem->fg.isUsing ){
31559 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
31561 if( pItem->pSelect ){
31562 if( pItem->pTab ){
31563 Table *pTab = pItem->pTab;
31564 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
31566 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
31567 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
31569 if( pItem->fg.isTabFunc ){
31570 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
31577 ** Generate a human-readable description of a Select object.
31583 sqlite3TreeViewLine(pView, "nil-SELECT");
31587 if( p->pWith ){
31588 sqlite3TreeViewWith(pView, p->pWith, 1);
31593 if( p->selFlags & SF_WhereBegin ){
31598 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
31599 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
31600 p->selId, p, p->selFlags,
31601 (int)p->nSelectRow
31605 if( p->pPrior ){
31609 if( p->pSrc && p->pSrc->nSrc ) n++;
31610 if( p->pWhere ) n++;
31611 if( p->pGroupBy ) n++;
31612 if( p->pHaving ) n++;
31613 if( p->pOrderBy ) n++;
31614 if( p->pLimit ) n++;
31616 if( p->pWin ) n++;
31617 if( p->pWinDefn ) n++;
31620 if( p->pEList ){
31621 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
31623 n--;
31625 if( p->pWin ){
31627 sqlite3TreeViewPush(&pView, (n--)>0);
31628 sqlite3TreeViewLine(pView, "window-functions");
31629 for(pX=p->pWin; pX; pX=pX->pNextWin){
31630 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
31635 if( p->pSrc && p->pSrc->nSrc ){
31636 sqlite3TreeViewPush(&pView, (n--)>0);
31638 sqlite3TreeViewSrcList(pView, p->pSrc);
31641 if( p->pWhere ){
31642 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
31643 sqlite3TreeViewExpr(pView, p->pWhere, 0);
31646 if( p->pGroupBy ){
31647 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
31649 if( p->pHaving ){
31650 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
31651 sqlite3TreeViewExpr(pView, p->pHaving, 0);
31655 if( p->pWinDefn ){
31657 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
31658 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
31659 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
31664 if( p->pOrderBy ){
31665 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
31667 if( p->pLimit ){
31668 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
31669 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
31670 if( p->pLimit->pRight ){
31671 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
31672 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
31677 if( p->pPrior ){
31679 switch( p->op ){
31686 p = p->pPrior;
31730 ** Generate a human-readable explanation for a Window object
31735 if( pWin->pFilter ){
31737 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
31741 if( pWin->zName ){
31742 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
31746 if( pWin->zBase ) nElement++;
31747 if( pWin->pOrderBy ) nElement++;
31748 if( pWin->eFrmType ) nElement++;
31749 if( pWin->eExclude ) nElement++;
31750 if( pWin->zBase ){
31751 sqlite3TreeViewPush(&pView, (--nElement)>0);
31752 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
31755 if( pWin->pPartition ){
31756 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
31758 if( pWin->pOrderBy ){
31759 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
31761 if( pWin->eFrmType ){
31764 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
31765 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
31767 pWin->bImplicitFrame ? " (implied)" : "");
31768 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
31769 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
31770 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
31773 if( pWin->eExclude ){
31776 switch( pWin->eExclude ){
31782 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
31796 ** Generate a human-readable explanation for a Window Function object
31802 pWin->pWFunc->zName, pWin->pWFunc->nArg);
31809 ** Generate a human-readable explanation of an expression tree.
31821 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31825 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31827 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
31830 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
31842 switch( pExpr->op ){
31845 pExpr->iTable, pExpr->iColumn, zFlgs);
31849 if( pExpr->iTable<0 ){
31852 if( pExpr->op2 ){
31853 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
31858 pExpr->iColumn, zFlgs, zOp2);
31862 pExpr->iTable, pExpr->iColumn,
31863 pExpr->y.pTab, zFlgs);
31866 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31871 if( pExpr->flags & EP_IntValue ){
31872 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
31874 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
31881 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31887 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
31902 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31909 pExpr->u.zToken, pExpr->iColumn);
31913 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
31918 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
31925 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
31926 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31963 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
31965 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
31966 assert( pExpr->pRight );
31967 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
31968 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
31975 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
31976 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31983 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
31988 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
31989 pExpr->u.zToken, zFlgs);
31990 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32003 pFarg = pExpr->x.pList;
32005 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
32011 if( pExpr->op==TK_AGG_FUNCTION ){
32013 pExpr->op2, pExpr->u.zToken, zFlgs,
32014 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
32015 pExpr->iAgg, pExpr->pAggInfo);
32016 }else if( pExpr->op2!=0 ){
32019 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
32021 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
32022 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
32023 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
32024 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
32026 pExpr->u.zToken, zFlgs, zOp2);
32028 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
32043 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
32044 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32049 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
32050 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32056 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
32057 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
32060 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
32065 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32067 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
32069 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32082 ** X is stored in pExpr->pLeft.
32083 ** Y is stored in pExpr->pList->a[0].pExpr.
32084 ** Z is stored in pExpr->pList->a[1].pExpr.
32088 pX = pExpr->pLeft;
32090 assert( pExpr->x.pList->nExpr==2 );
32091 pY = pExpr->x.pList->a[0].pExpr;
32092 pZ = pExpr->x.pList->a[1].pExpr;
32101 ** to a column in the new.* or old.* pseudo-tables available to
32103 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
32104 ** is set to the column of the pseudo-table to read, or to -1 to
32108 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
32113 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32115 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
32121 switch( pExpr->affExpr ){
32128 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
32134 pExpr->iTable, pExpr->iColumn, zFlgs);
32135 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32141 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
32146 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
32147 pExpr->iColumn, pExpr->iTable-1,
32148 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
32149 assert( ExprUseXSelect(pExpr->pLeft) );
32150 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
32154 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
32155 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32162 tmp.op = pExpr->op2;
32167 if( pExpr->iColumn<=0 ){
32171 pExpr->iColumn-1);
32176 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
32182 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
32183 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
32186 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
32193 ** Generate a human-readable explanation of an expression list.
32206 for(i=0; i<pList->nExpr; i++){
32207 int j = pList->a[i].u.x.iOrderByCol;
32208 char *zName = pList->a[i].zEName;
32209 int moreToFollow = i<pList->nExpr - 1;
32215 switch( pList->a[i].fg.eEName ){
32220 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
32221 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
32222 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
32223 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
32236 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
32255 ** Generate a human-readable explanation of an id-list.
32268 for(i=0; i<pList->nId; i++){
32269 char *zName = pList->a[i].zName;
32270 int moreToFollow = i<pList->nId - 1;
32274 if( pList->eU4==EU4_NONE ){
32276 }else if( pList->eU4==EU4_IDX ){
32277 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
32279 assert( pList->eU4==EU4_EXPR );
32280 if( pList->a[i].u4.pExpr==0 ){
32284 sqlite3TreeViewPush(&pView, i<pList->nId-1);
32285 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
32305 ** Generate a human-readable explanation of a list of Upsert objects
32316 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
32318 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
32319 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
32320 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
32321 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
32322 if( pUpsert->pUpsertWhere ){
32323 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32324 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
32328 pUpsert = pUpsert->pNextUpsert;
32335 ** Generate a human-readable diagram of the data structure that go
32357 sqlite3TreeViewPush(&pView, (--n)>0);
32362 sqlite3TreeViewPush(&pView, (--n)>0);
32368 sqlite3TreeViewPush(&pView, (--n)>0);
32374 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32377 sqlite3TreeViewPush(&pView, (--n)>0);
32383 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32391 ** Generate a human-readable diagram of the data structure that go
32424 sqlite3TreeViewPush(&pView, (--n)>0);
32429 sqlite3TreeViewPush(&pView, (--n)>0);
32435 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
32438 sqlite3TreeViewPush(&pView, (--n)>0);
32439 sqlite3TreeViewLine(pView, "DATA-SOURCE");
32444 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
32447 sqlite3TreeViewPush(&pView, (--n)>0);
32453 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32461 ** Generate a human-readable diagram of the data structure that go
32496 sqlite3TreeViewPush(&pView, (--n)>0);
32501 sqlite3TreeViewPush(&pView, (--n)>0);
32507 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
32510 sqlite3TreeViewPush(&pView, (--n)>0);
32516 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32519 sqlite3TreeViewPush(&pView, (--n)>0);
32525 sqlite3TreeViewPush(&pView, (--n)>0);
32531 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32539 ** Show a human-readable graph of a TriggerStep
32550 moreToFollow || (showFullList && pStep->pNext!=0));
32552 if( cnt++ && pStep->pNext==0 ){
32556 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
32557 }while( showFullList && (pStep = pStep->pNext)!=0 );
32562 ** Show a human-readable graph of a Trigger
32573 moreToFollow || (showFullList && pTrigger->pNext!=0));
32575 if( cnt++ && pTrigger->pNext==0 ){
32579 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
32581 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
32583 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
32590 ** These simplified versions of the tree-view routines omit unnecessary
32636 ** This file contains code to implement a pseudo-random number
32655 /* The RFC-7539 ChaCha20 block function
32657 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
32686 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32688 ** we have to locate the state vector at run-time. In the more common
32740 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N);
32741 wsdPrng.n -= N;
32746 N -= wsdPrng.n;
32797 ** This file presents a simple cross-platform threading interface for
32810 ** single-threaded if desired.
32822 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
32845 /* This routine is never used in single-threaded mode */
32852 p->xTask = xTask;
32853 p->pIn = pIn;
32861 rc = pthread_create(&p->tid, 0, xTask, pIn);
32864 p->done = 1;
32865 p->pOut = xTask(pIn);
32877 if( p->done ){
32878 *ppOut = p->pOut;
32881 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
32894 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
32920 assert( p->id==GetCurrentThreadId() );
32922 assert( p->xTask!=0 );
32923 p->pResult = p->xTask(p->pIn);
32950 p->xTask = xTask;
32951 p->pIn = pIn;
32952 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
32953 if( p->tid==0 ){
32957 if( p->xTask==0 ){
32958 p->id = GetCurrentThreadId();
32959 p->pResult = xTask(pIn);
32974 if( p->xTask==0 ){
32975 /* assert( p->id==GetCurrentThreadId() ); */
32977 assert( p->tid==0 );
32979 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
32980 rc = sqlite3Win32Wait((HANDLE)p->tid);
32982 bRc = CloseHandle((HANDLE)p->tid);
32985 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
32994 /********************************* Single-Threaded **************************/
33023 p->xTask = xTask;
33024 p->pIn = pIn;
33026 p->xTask = 0;
33027 p->pResult = xTask(pIn);
33038 if( p->xTask ){
33039 *ppOut = p->xTask(p->pIn);
33041 *ppOut = p->pResult;
33057 /****************************** End Single-Threaded *************************/
33073 ** This file contains routines used to translate between UTF-8,
33074 ** UTF-16, UTF-16BE, and UTF-16LE.
33076 ** Notes on UTF-8:
33078 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
33085 ** Notes on UTF-16: (with wwww+1==uuuuu)
33087 ** Word-0 Word-1 Value
33093 ** 0xff 0xfe little-endian utf-16 follows
33094 ** 0xfe 0xff big-endian utf-16 follows
33111 ** a multi-byte UTF8 character.
33150 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33151 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33162 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
33163 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
33170 ** Translate a single UTF-8 character. Return the unicode value.
33177 ** Notes On Invalid UTF-8:
33179 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
33180 ** be encoded as a multi-byte character. Any multi-byte character that
33184 ** If a multi-byte character attempts to encode a value between
33188 ** byte of a character are interpreted as single-byte characters
33192 ** * This routine accepts over-length UTF8 encodings
33193 ** for unicode values 0x80 and greater. It does not change over-length
33199 c = sqlite3Utf8Trans1[c-0xc0]; \
33213 ** For this routine, we assume the UTF8 string is always zero-terminated.
33217 c = sqlite3Utf8Trans1[c-0xc0];
33251 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
33252 assert( pMem->flags&MEM_Str );
33253 assert( pMem->enc!=desiredEnc );
33254 assert( pMem->enc!=0 );
33255 assert( pMem->n>=0 );
33267 /* If the translation is between UTF-16 little and big endian, then
33271 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
33279 zIn = (u8*)pMem->z;
33280 zTerm = &zIn[pMem->n&~1];
33287 pMem->enc = desiredEnc;
33293 /* When converting from UTF-16, the maximum growth results from
33294 ** translating a 2-byte character to a 4-byte UTF-8 character.
33296 ** nul-terminator.
33298 pMem->n &= ~1;
33299 len = 2 * (sqlite3_int64)pMem->n + 1;
33301 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
33302 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
33304 ** nul-terminator.
33306 len = 2 * (sqlite3_int64)pMem->n + 2;
33315 zIn = (u8*)pMem->z;
33316 zTerm = &zIn[pMem->n];
33317 zOut = sqlite3DbMallocRaw(pMem->db, len);
33323 if( pMem->enc==SQLITE_UTF8 ){
33325 /* UTF-8 -> UTF-16 Little-endian */
33332 /* UTF-8 -> UTF-16 Big-endian */
33338 pMem->n = (int)(z - zOut);
33342 if( pMem->enc==SQLITE_UTF16LE ){
33343 /* UTF-16 Little-endian -> UTF-8 */
33355 zIn -= 2;
33372 /* UTF-16 Big-endian -> UTF-8 */
33384 zIn -= 2;
33401 pMem->n = (int)(z - zOut);
33404 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
33406 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
33408 pMem->flags = c;
33409 pMem->enc = desiredEnc;
33410 pMem->z = (char*)zOut;
33411 pMem->zMalloc = pMem->z;
33412 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
33430 ** This routine checks for a byte-order mark at the beginning of the
33431 ** UTF-16 string stored in *pMem. If one is present, it is removed and
33433 ** byte-swapping, it just sets Mem.enc appropriately.
33442 assert( pMem->n>=0 );
33443 if( pMem->n>1 ){
33444 u8 b1 = *(u8 *)pMem->z;
33445 u8 b2 = *(((u8 *)pMem->z) + 1);
33457 pMem->n -= 2;
33458 memmove(pMem->z, &pMem->z[2], pMem->n);
33459 pMem->z[pMem->n] = '\0';
33460 pMem->z[pMem->n+1] = '\0';
33461 pMem->flags |= MEM_Term;
33462 pMem->enc = bom;
33470 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
33483 zTerm = (const u8*)(-1);
33493 /* This test function is not currently used by the automated test-suite.
33498 ** Translate UTF-8 to UTF-8.
33500 ** This has the effect of making sure that the string is well-formed
33501 ** UTF-8. Miscoded characters are removed.
33503 ** The translation is done in-place and aborted if the output
33518 return (int)(zOut - zStart);
33524 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
33525 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
33536 if( db->mallocFailed ){
33540 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
33541 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
33542 assert( m.z || db->mallocFailed );
33547 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
33549 ** in pZ. nChar must be non-negative.
33563 return (int)(z-(unsigned char const *)zIn)
33564 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
33583 n = (int)(z-zBuf);
33592 assert( (z-zBuf)==n );
33629 ** sqlite3FaultSim() function only returns non-zero during testing.
33631 ** During testing, if the test harness has set a fault-sim callback using
33633 ** each call to sqlite3FaultSim() is relayed to that application-supplied
33634 ** callback and the integer return value form the application-supplied
33672 ** lower 30 bits of a 32-bit signed integer.
33687 ** The column type is an extra string stored after the zero-terminator on
33691 if( pCol->colFlags & COLFLAG_HASTYPE ){
33692 return pCol->zCnName + strlen(pCol->zCnName) + 1;
33693 }else if( pCol->eCType ){
33694 assert( pCol->eCType<=SQLITE_N_STDTYPE );
33695 return (char*)sqlite3StdType[pCol->eCType-1];
33702 ** Helper function for sqlite3Error() - called rarely. Broken out into
33707 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33718 db->errCode = err_code;
33719 if( err_code || db->pErr ){
33722 db->errByteOffset = -1;
33732 db->errCode = SQLITE_OK;
33733 db->errByteOffset = -1;
33734 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33745 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
33755 ** assumed to be encoded in UTF-8.
33763 db->errCode = err_code;
33767 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
33773 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
33778 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
33790 sqlite3 *db = pParse->db;
33792 assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
33793 db->errByteOffset = -2;
33797 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
33798 if( db->suppressErr ){
33800 if( db->mallocFailed ){
33801 pParse->nErr++;
33802 pParse->rc = SQLITE_NOMEM;
33805 pParse->nErr++;
33806 sqlite3DbFree(db, pParse->zErrMsg);
33807 pParse->zErrMsg = zMsg;
33808 pParse->rc = SQLITE_ERROR;
33809 pParse->pWith = 0;
33820 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
33821 pParse->rc = errCode;
33822 pParse->nErr++;
33827 ** Convert an SQL-style quoted string into a normal string by removing
33828 ** the quote characters. The conversion is done in-place. If the
33830 ** is a no-op.
33832 ** The input string must be zero-terminated. A new zero-terminator
33835 ** The return value is -1 if no dequoting occurs or the length of the
33839 ** 2002-02-14: This routine is extended to remove MS-Access style
33840 ** brackets from around identifiers. For example: "[a-b-c]" becomes
33841 ** "a-b-c".
33867 assert( sqlite3Isquote(p->u.zToken[0]) );
33868 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
33869 sqlite3Dequote(p->u.zToken);
33876 ** "abc" -> abc
33877 ** "ab""cd" -> (not possible because of the interior "")
33879 ** Remove the quotes if possible. This is a optimization. The overall
33881 ** is always a no-op.
33885 if( p->n<2 ) return;
33886 if( !sqlite3Isquote(p->z[0]) ) return;
33887 for(i=1; i<p->n-1; i++){
33888 if( sqlite3Isquote(p->z[i]) ) return;
33890 p->n -= 2;
33891 p->z++;
33898 p->z = z;
33899 p->n = sqlite3Strlen30(z);
33902 /* Convenient short-hand */
33909 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
33911 ** the contents of two buffers containing UTF-8 strings in a
33912 ** case-independent fashion, using the same definition of "case
33917 return zRight ? -1 : 0;
33934 c = (int)UpperToLower[c] - (int)UpperToLower[x];
33945 return zRight ? -1 : 0;
33951 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
33952 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
33956 ** Compute an 8-bit hash on a string that is insensitive to case differences
33969 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
34012 ** uses the encoding enc. The string is not necessarily zero-terminated.
34020 ** -1 => Not a valid number, but has a valid prefix which
34025 ** [+-]digits[E[+-]digits]
34026 ** [+-]digits.[digits][E[+-]digits]
34027 ** [+-].digits[E[+-]digits]
34049 int eValid = 1; /* True exponent is either not used or is well-formed */
34052 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
34068 for(i=3-enc; i<length && z[i]==0; i+=2){}
34069 if( i<length ) eType = -100;
34079 if( *z=='-' ){
34080 sign = -1;
34088 s = s*10 + (*z - '0');
34090 if( s>=((LARGEST_INT64-9)/10) ){
34091 /* skip non-significant significand digits
34105 if( s<((LARGEST_INT64-9)/10) ){
34106 s = s*10 + (*z - '0');
34107 d--;
34124 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
34127 if( *z=='-' ){
34128 esign = -1;
34135 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
34148 esign = -1;
34149 e *= -1;
34156 result = sign<0 ? -(double)0 : (double)0;
34164 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
34166 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
34169 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
34172 e--;
34176 s = sign<0 ? -s : s;
34178 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
34182 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
34183 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
34184 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
34217 /* return true if number and no extra non-whitespace chracters after */
34221 return -1;
34234 ** Render an signed 64-bit integer as text. Store the result in zOut[].
34243 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
34247 i = sizeof(zTemp)-2;
34248 zTemp[sizeof(zTemp)-1] = 0;
34250 zTemp[i--] = (x%10) + '0';
34253 if( v<0 ) zTemp[i--] = '-';
34254 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
34258 ** Compare the 19-character string zNum against the text representation
34269 ** will return -8.
34277 c = (zNum[i*incr]-pow63[i])*10;
34280 c = zNum[18*incr] - '8';
34281 testcase( c==(-1) );
34289 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
34294 ** -1 Not even a prefix of the input text looks like an integer
34295 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34296 ** 1 Excess non-space text after the integer value
34297 ** 2 Integer too large for a 64-bit signed integer or is malformed
34301 ** The string is not necessarily zero-terminated. The encoding is
34310 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
34321 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
34328 if( *zNum=='-' ){
34338 u = u*10 + c - '0';
34345 ** from clang and -fsanitize=undefined. This test and assignment make
34347 ** them, but we must appaise the undefined-behavior pharisees. */
34350 *pNum = -(i64)u;
34356 rc = -1;
34357 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
34363 rc = 1; /* Extra non-space text after the integer */
34374 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
34388 assert( u-1==LARGEST_INT64 );
34396 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
34397 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
34402 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34404 ** 2 Integer too large for a 64-bit signed integer or is malformed
34419 return (z[k]==0 && k-i<=16) ? 0 : 2;
34428 ** If zNum represents an integer that will fit in 32-bits, then set
34433 ** Any non-numeric characters that following zNum are ignored.
34435 ** input number to be zero-terminated.
34441 if( zNum[0]=='-' ){
34468 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
34475 ** 2^31 -> 2147483648
34481 testcase( v-neg==2147483647 );
34482 if( v-neg>2147483647 ){
34486 v = -v;
34493 ** Return a 32-bit integer value extracted from a string. If the
34503 ** Try to convert z into an unsigned 32-bit integer. Return true on
34512 v = v*10 + z[i] - '0';
34521 ** The variable-length integer encoding is as follows:
34528 ** 7 bits - A
34529 ** 14 bits - BA
34530 ** 21 bits - BBA
34531 ** 28 bits - BBBA
34532 ** 35 bits - BBBBA
34533 ** 42 bits - BBBBBA
34534 ** 49 bits - BBBBBBA
34535 ** 56 bits - BBBBBBBA
34536 ** 64 bits - BBBBBBBBC
34540 ** Write a 64-bit variable-length integer to memory starting at p[0].
34544 ** A variable-length integer consists of the lower 7 bits of each byte
34555 for(i=7; i>=0; i--){
34568 for(i=0, j=n-1; j>=0; j--, i++){
34600 ** Read a 64-bit variable-length integer from memory starting at p[0].
34743 b = p[-4];
34754 ** Read a 32-bit variable-length integer from memory starting at p[0].
34757 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
34761 ** single-byte case. All code should use the MACRO version as
34762 ** this function assumes the single-byte case has already been handled.
34767 /* The 1-byte case. Overwhelmingly the most common. Handled inline
34780 /* The 2-byte case */
34793 /* The 3-byte case */
34808 /* A 32-bit varint is used to store size information in btrees.
34809 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
34810 ** A 3-byte varint is sufficient, for example, to record the size
34811 ** of a 1048569-byte BLOB or string.
34813 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
34814 ** rare larger cases can be handled by the slower 64-bit varint
34822 n = sqlite3GetVarint(p-2, &v64);
34834 ** unrolling for the 3- and 4-byte varint cases. This code is
34867 ** slow) general-purpose sqlite3GetVarint() routine to extract the
34873 p -= 4;
34884 ** 64-bit integer.
34894 ** Read or write a four-byte big-endian integer value.
34961 n--;
35004 eOpenState = db->eOpenState;
35017 eOpenState = db->eOpenState;
35030 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
35031 ** the other 64-bit signed integer at *pA and store the result in *pA.
35041 testcase( iB==-1 ); testcase( iB==0 );
35043 testcase( iA>0 && LARGEST_INT64 - iA == iB );
35044 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
35045 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
35047 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
35048 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
35049 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
35061 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
35063 *pA -= iB;
35066 return sqlite3AddInt64(pA, -iB);
35084 if( -iA>LARGEST_INT64/-iB ) return 1;
35093 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
35094 ** if the integer has a value of -2147483648, return +2147483647
35099 return -x;
35104 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
35110 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
35115 ** test.db-journal => test.nal
35116 ** test.db-wal => test.wal
35117 ** test.db-shm => test.shm
35118 ** test.db-mj7f3319fa => test.9fa
35127 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
35128 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
35146 5, 5, 5, /* 12-14 */
35147 4, 4, 4, 4, /* 15-18 */
35148 3, 3, 3, 3, 3, 3, /* 19-24 */
35149 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
35154 return a+x[a-b];
35158 return b+x[b-a];
35171 while( x<8 ){ y -= 10; x <<= 1; }
35174 int i = 60 - __builtin_clzll(x);
35178 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
35182 return a[x&7] + y - 10;
35196 e = (a>>52) - 1022;
35207 if( n>=5 ) n -= 2;
35208 else if( n>=1 ) n -= 1;
35210 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
35217 ** db->mallocFailed flag is set.
35230 ** is always zero-terminated.
35329 ** This is the implementation of generic hash-tables
35342 pNew->first = 0;
35343 pNew->count = 0;
35344 pNew->htsize = 0;
35345 pNew->ht = 0;
35356 elem = pH->first;
35357 pH->first = 0;
35358 sqlite3_free(pH->ht);
35359 pH->ht = 0;
35360 pH->htsize = 0;
35362 HashElem *next_elem = elem->next;
35366 pH->count = 0;
35375 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
35378 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
35396 pHead = pEntry->count ? pEntry->chain : 0;
35397 pEntry->count++;
35398 pEntry->chain = pNew;
35403 pNew->next = pHead;
35404 pNew->prev = pHead->prev;
35405 if( pHead->prev ){ pHead->prev->next = pNew; }
35406 else { pH->first = pNew; }
35407 pHead->prev = pNew;
35409 pNew->next = pH->first;
35410 if( pH->first ){ pH->first->prev = pNew; }
35411 pNew->prev = 0;
35412 pH->first = pNew;
35431 if( new_size==pH->htsize ) return 0;
35447 sqlite3_free(pH->ht);
35448 pH->ht = new_ht;
35449 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
35451 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
35452 unsigned int h = strHash(elem->pKey) % new_size;
35453 next_elem = elem->next;
35474 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
35476 h = strHash(pKey) % pH->htsize;
35477 pEntry = &pH->ht[h];
35478 elem = pEntry->chain;
35479 count = pEntry->count;
35482 elem = pH->first;
35483 count = pH->count;
35486 while( count-- ){
35488 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
35491 elem = elem->next;
35505 if( elem->prev ){
35506 elem->prev->next = elem->next;
35508 pH->first = elem->next;
35510 if( elem->next ){
35511 elem->next->prev = elem->prev;
35513 if( pH->ht ){
35514 pEntry = &pH->ht[h];
35515 if( pEntry->chain==elem ){
35516 pEntry->chain = elem->next;
35518 assert( pEntry->count>0 );
35519 pEntry->count--;
35522 pH->count--;
35523 if( pH->count==0 ){
35524 assert( pH->first==0 );
35525 assert( pH->count==0 );
35537 return findElementWithHash(pH, pKey, 0)->data;
35562 if( elem->data ){
35563 void *old_data = elem->data;
35567 elem->data = data;
35568 elem->pKey = pKey;
35575 new_elem->pKey = pKey;
35576 new_elem->data = data;
35577 pH->count++;
35578 if( pH->count>=10 && pH->count > 2*pH->htsize ){
35579 if( rehash(pH, pH->count*2) ){
35580 assert( pH->htsize>0 );
35581 h = strHash(pKey) % pH->htsize;
35584 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
35661 /* 59 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35662 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35663 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35692 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
35709 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
35726 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
35762 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
35797 ** 2022-09-06
35840 ** VFS - the database and the rollback journal.
35849 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
35962 /* Forward declarations for the low-level storage engine
35978 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn);
35982 ** underlying key/value store to use - either "local" or "session".
35984 ** Both zKey and zData are zero-terminated pure text strings.
35998 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey,
36011 ** this routine is a no-op.
36017 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey));
36028 ** not counting the final zero terminator. Return -1 if the key does
36048 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36049 return -1;
36055 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey,
36064 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey));
36065 return -1;
36067 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
36070 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey,
36097 ** for JavaScript-side implementations in WASM builds. In such builds
36126 ** ---------------
36128 ** * Non-zero bytes are encoded as upper-case hexadecimal
36130 ** * A sequence of one or more zero-bytes that are not at the
36131 ** beginning of the buffer are encoded as a little-endian
36132 ** base-26 number using a..z. "a" means 0. "b" means 1,
36136 ** of hexadecimal and base-26 numbers, it is always clear where
36148 /* A sequence of 1 or more zeros is stored as a little-endian
36149 ** base-26 number using a..z as the digits. So one zero is "b".
36155 i += k-1;
36167 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36168 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36169 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36170 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
36171 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36172 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36173 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36174 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36176 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36177 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36178 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36179 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36180 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36181 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36182 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36183 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36206 n += (c - 'a')*mult;
36210 if( j+n>nOut ) return -1;
36227 ** Decode a complete journal file. Allocate space in pFile->aJrnl
36228 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
36231 ** The first few characters of the text encoding will be a little-endian
36232 ** base-26 number (digits a..z) that is the total number of bytes
36233 ** in the decoded journal file image. This base-26 number is followed
36235 ** separator is required to act as a terminator for the base-26 number.
36238 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */
36239 const char *zTxt, /* Text encoding. Zero-terminated */
36247 n += (zTxt[i] - 'a')*mult;
36250 sqlite3_free(pFile->aJrnl);
36251 pFile->aJrnl = sqlite3_malloc64( n );
36252 if( pFile->aJrnl==0 ){
36253 pFile->nJrnl = 0;
36256 pFile->nJrnl = n;
36257 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl);
36258 if( n<pFile->nJrnl ){
36259 sqlite3_free(pFile->aJrnl);
36260 pFile->aJrnl = 0;
36261 pFile->nJrnl = 0;
36271 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1);
36277 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData);
36283 ** Close an kvvfs-file.
36288 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass,
36289 pFile->isJournal ? "journal" : "db"));
36290 sqlite3_free(pFile->aJrnl);
36295 ** Read from the -journal file.
36304 assert( pFile->isJournal );
36305 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36306 if( pFile->aJrnl==0 ){
36307 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0);
36314 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1);
36317 if( pFile->aJrnl==0 ) return SQLITE_IOERR;
36319 if( iOfst+iAmt>pFile->nJrnl ){
36322 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt);
36342 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36347 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
36350 pFile->szPage = iAmt;
36356 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, aData, sizeof(aData)-1);
36364 n = kvvfsDecode(aData, &aData[2000], sizeof(aData)-2000);
36376 memset(zBuf+n, 0, iAmt-n);
36384 ** Write into the -journal file.
36394 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36396 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
36397 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
36401 pFile->aJrnl = aNew;
36402 if( pFile->nJrnl<iOfst ){
36403 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl);
36405 pFile->nJrnl = iEnd;
36407 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt);
36424 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst));
36426 assert( (iAmt & (iAmt-1))==0 );
36427 assert( pFile->szPage<0 || pFile->szPage==iAmt );
36428 pFile->szPage = iAmt;
36432 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){
36435 if( iOfst+iAmt > pFile->szDb ){
36436 pFile->szDb = iOfst + iAmt;
36442 ** Truncate an kvvfs-file.
36446 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size));
36448 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl");
36449 sqlite3_free(pFile->aJrnl);
36450 pFile->aJrnl = 0;
36451 pFile->nJrnl = 0;
36456 if( pFile->szDb>size
36457 && pFile->szPage>0
36458 && (size % pFile->szPage)==0
36462 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size));
36463 pgno = 1 + size/pFile->szPage;
36464 pgnoMax = 2 + pFile->szDb/pFile->szPage;
36467 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey);
36470 pFile->szDb = size;
36477 ** Sync an kvvfs-file.
36483 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass));
36484 if( pFile->nJrnl<=0 ){
36487 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 );
36491 n = pFile->nJrnl;
36498 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]);
36499 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut);
36508 ** Return the current file-size of an kvvfs-file.
36512 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass));
36513 *pSize = pFile->nJrnl;
36518 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass));
36519 if( pFile->szDb>=0 ){
36520 *pSize = pFile->szDb;
36528 ** Lock an kvvfs-file.
36532 assert( !pFile->isJournal );
36533 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock));
36536 pFile->szDb = kvvfsReadFileSize(pFile);
36542 ** Unlock an kvvfs-file.
36546 assert( !pFile->isJournal );
36547 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock));
36549 pFile->szDb = -1;
36555 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
36564 ** File control method. For custom operations on an kvvfs-file.
36575 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass));
36576 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){
36585 ** Return the sector-size in bytes for an kvvfs-file.
36592 ** Return the device characteristic flags supported by an kvvfs-file.
36616 pFile->isJournal = 0;
36617 pFile->base.pMethods = &kvvfs_db_io_methods;
36619 if( strcmp(zName, "local-journal")==0
36620 || strcmp(zName, "session-journal")==0
36622 pFile->isJournal = 1;
36623 pFile->base.pMethods = &kvvfs_jrnl_io_methods;
36628 pFile->zClass = "session";
36630 pFile->zClass = "local";
36632 pFile->aJrnl = 0;
36633 pFile->nJrnl = 0;
36634 pFile->szPage = -1;
36635 pFile->szDb = -1;
36641 ** ensure the file-system modifications are synced to disk before
36645 if( strcmp(zPath, "local-journal")==0 ){
36648 if( strcmp(zPath, "session-journal")==0 ){
36665 if( strcmp(zPath, "local-journal")==0 ){
36668 if( strcmp(zPath, "session-journal")==0 ){
36701 if( nOut<nPath+1 ) nPath = nOut - 1;
36753 ** This routine is called initialize the KV-vfs as the default VFS.
36783 ** This file contains the VFS implementation for unix-like operating systems
36789 ** use flock(), dot-files, various proprietary locking schemas, or simply
36799 ** * General-purpose declarations and utility functions.
36803 ** + for no-op locks
36804 ** + for dot-file locks
36812 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
36825 ** 3. Dot-file locking,
36884 ** -DHAVE_GETHOSTUUID=0
36885 ** -DHAVE_GETHOSTUUID=1
36888 ** -DSQLITE_ENABLE_LOCKING_STYLE.
36926 ** If we are to be thread-safe, include the pthreads header.
36947 ** Maximum supported path-length.
36969 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
36998 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
37025 ** transaction counter in bytes 24-27 of database files are updated
37090 ** The threadid macro resolves to the thread-id or to 0. Used for
37111 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
37112 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
37120 ** Linux-specific IOCTL magic numbers used for controlling F2FS
37161 ** which always has the same well-defined interface.
37194 ** Many system calls are accessed through pointer-to-functions so that
37223 ** DJGPP. But it is DOS - what did you expect?
37370 ** log if they come from non-root processes. So avoid calling fchown() if
37450 int i = -1;
37454 for(i=0; i<ArraySize(aSyscall)-1; i++){
37482 ** The m parameter will be non-zero only when creating -wal, -journal,
37483 ** and -shm files. We want those files to have *exactly* the same
37485 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
37510 fd = -1;
37575 ** integer lock-type.
37594 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
37595 ** command-line option on the compiler. This code is normally
37611 if( p->l_type==F_RDLCK ){
37613 }else if( p->l_type==F_WRLCK ){
37615 }else if( p->l_type==F_UNLCK ){
37620 assert( p->l_whence==SEEK_SET );
37624 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
37625 (int)p->l_pid, s);
37626 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
37639 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
37659 /* On Android, ftruncate() always uses 32-bit offsets, even if
37720 ** a performance-critical path, so it is sufficient to put these
37745 ** Changes are made in-place. Return the new name length.
37747 ** The original filename is in z[0..n-1]. Return the number of
37752 while( n>1 && z[n-1]=='/' ){ n--; }
37761 while( j>0 && z[j-1]!='/' ){ j--; }
37762 if( j>0 ){ j--; }
37793 pNew->zCanonicalName = (char*)&pNew[1];
37794 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
37795 n = vxworksSimplifyName(pNew->zCanonicalName, n);
37802 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
37803 if( pCandidate->nName==n
37804 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
37807 pCandidate->nRef++;
37814 pNew->nRef = 1;
37815 pNew->nName = n;
37816 pNew->pNext = vxworksFileList;
37828 assert( pId->nRef>0 );
37829 pId->nRef--;
37830 if( pId->nRef==0 ){
37832 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
37834 *pp = pId->pNext;
37892 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
37920 ** on linux - with NPTL a lock created by thread A can override locks
37921 ** in thread B. But there is no way to know at compile-time which
37923 ** compile-time whether or not thread A can override locks on thread B.
37924 ** One has to do a run-time check to discover the behavior of the
37944 ** sizes ino_t at only 32-bits instead of 64-bits. (See
37945 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
37946 ** To work around this, always allocate 64-bits for the inode number.
37947 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
38014 assert( pFile->pInode );
38015 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
38018 assert( pFile->pInode );
38019 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
38025 ** This function - unixLogErrorAtLine(), is only ever called via the macro
38030 ** errno and, if possible, the human-readable equivalent from strerror() or
38036 ** failed (e.g. "unlink", "open") and the associated file-system path,
38050 ** the strerror() function to obtain the human-readable error message
38073 strerror_r(iErrno, aErr, sizeof(aErr)-1);
38079 /* Non-threadsafe build, use strerror(). */
38085 "os_unix.c:%d: (%d) %s(%s) - %s",
38108 pFile ? pFile->zPath : 0, lineno);
38113 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
38117 pFile->lastErrno = error;
38121 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
38124 unixInodeInfo *pInode = pFile->pInode;
38128 for(p=pInode->pUnused; p; p=pNext){
38129 pNext = p->pNext;
38130 robust_close(pFile, p->fd, __LINE__);
38133 pInode->pUnused = 0;
38143 unixInodeInfo *pInode = pFile->pInode;
38147 pInode->nRef--;
38148 if( pInode->nRef==0 ){
38149 assert( pInode->pShmNode==0 );
38150 sqlite3_mutex_enter(pInode->pLockMutex);
38152 sqlite3_mutex_leave(pInode->pLockMutex);
38153 if( pInode->pPrev ){
38154 assert( pInode->pPrev->pNext==pInode );
38155 pInode->pPrev->pNext = pInode->pNext;
38158 inodeList = pInode->pNext;
38160 if( pInode->pNext ){
38161 assert( pInode->pNext->pPrev==pInode );
38162 pInode->pNext->pPrev = pInode->pPrev;
38164 sqlite3_mutex_free(pInode->pLockMutex);
38186 struct stat statbuf; /* Low-level file information */
38191 /* Get low-level information about the file that we can used to
38194 fd = pFile->h;
38199 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
38202 … sqlite3_log(SQLITE_IOERR, "findInodeInfo-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc);
38209 ** incorrectly for zero-size files. See ticket #3260. To work
38218 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
38223 … sqlite3_log(SQLITE_IOERR, "findInodeInfo-osWrite, fd[%d], errno[%d], osFstat[%d]", fd, errno, rc);
38231 …sqlite3_log(SQLITE_IOERR, "findInodeInfo-msdos-osFstat, fd[%d], errno[%d], osFstat[%d]", fd, errno…
38241 fileId.pId = pFile->pId;
38247 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
38248 pInode = pInode->pNext;
38256 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
38258 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38259 if( pInode->pLockMutex==0 ){
38264 pInode->nRef = 1;
38266 pInode->pNext = inodeList;
38267 pInode->pPrev = 0;
38268 if( inodeList ) inodeList->pPrev = pInode;
38271 pInode->nRef++;
38282 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
38285 return pFile->pInode!=0 &&
38286 (osStat(pFile->zPath, &buf)!=0
38287 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
38306 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
38308 rc = osFstat(pFile->h, &buf);
38310 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
38314 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
38318 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
38322 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
38331 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38342 assert( pFile->eFileLock<=SHARED_LOCK );
38343 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38346 if( pFile->pInode->eFileLock>SHARED_LOCK ){
38353 if( !reserved && !pFile->pInode->bProcessLock ){
38359 if( osFcntl(pFile->h, F_GETLK, &lock) ){
38368 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38369 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
38379 ** Set a posix-advisory-lock.
38383 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
38388 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
38397 unixFile *pFile /* Structure holding timeout value */
38399 int tm = pFile->iBusyTimeout;
38402 /* On systems that support some kind of blocking file lock with a timeout,
38405 ** lock once every millisecond until either the timeout expires, or until
38409 tm--;
38417 ** Attempt to set a system-lock on the file pFile. The lock is
38420 ** If the pFile was opened read/write from unix-excl, then the only lock
38423 ** operations become no-ops. Locking operations still happen internally,
38428 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
38429 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
38430 ** and is read-only.
38432 ** Zero is returned if the call completes successfully, or -1 if a call
38437 unixInodeInfo *pInode = pFile->pInode;
38439 assert( sqlite3_mutex_held(pInode->pLockMutex) );
38440 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
38441 if( pInode->bProcessLock==0 ){
38443 assert( pInode->nLock==0 );
38448 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
38450 pInode->bProcessLock = 1;
38451 pInode->nLock++;
38456 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
38462 ** Lock the file with the lock specified by parameter eFileLock - one
38476 ** UNLOCKED -> SHARED
38477 ** SHARED -> RESERVED
38478 ** SHARED -> (PENDING) -> EXCLUSIVE
38479 ** RESERVED -> (PENDING) -> EXCLUSIVE
38480 ** PENDING -> EXCLUSIVE
38488 ** lock primitives (called read-locks and write-locks below, to avoid
38497 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
38498 ** byte'. If this is successful, 'shared byte range' is read-locked
38501 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
38503 ** Windows95 is now pretty much extinct, but this work-around for the
38504 ** lack of shared-locks on Windows95 lives on, for backwards
38508 ** A RESERVED lock is implemented by grabbing a write-lock on the
38512 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
38520 ** implemented by obtaining a write-lock on the entire 'shared byte
38521 ** range'. Since all other locks require a read-lock on one of the bytes
38532 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
38533 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38534 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
38541 if( pFile->eFileLock>=eFileLock ){
38542 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
38552 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
38554 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
38556 /* This mutex is needed because pFile->pInode is shared across threads
38558 pInode = pFile->pInode;
38559 sqlite3_mutex_enter(pInode->pLockMutex);
38564 if( (pFile->eFileLock!=pInode->eFileLock &&
38565 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
38576 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
38578 assert( pFile->eFileLock==0 );
38579 assert( pInode->nShared>0 );
38580 pFile->eFileLock = SHARED_LOCK;
38581 pInode->nShared++;
38582 pInode->nLock++;
38594 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
38613 assert( pInode->nShared==0 );
38614 assert( pInode->eFileLock==0 );
38617 /* Now get the read-lock */
38641 pFile->eFileLock = SHARED_LOCK;
38642 pInode->nLock++;
38643 pInode->nShared = 1;
38645 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
38654 assert( 0!=pFile->eFileLock );
38677 /* Set up the transaction-counter change checking flags when
38683 && pFile->eFileLock<=SHARED_LOCK
38686 pFile->transCntrChng = 0;
38687 pFile->dbUpdate = 0;
38688 pFile->inNormalWrite = 1;
38694 pFile->eFileLock = eFileLock;
38695 pInode->eFileLock = eFileLock;
38697 pFile->eFileLock = PENDING_LOCK;
38698 pInode->eFileLock = PENDING_LOCK;
38702 sqlite3_mutex_leave(pInode->pLockMutex);
38703 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
38713 unixInodeInfo *pInode = pFile->pInode;
38714 UnixUnusedFd *p = pFile->pPreallocatedUnused;
38716 p->pNext = pInode->pUnused;
38717 pInode->pUnused = p;
38718 pFile->h = -1;
38719 pFile->pPreallocatedUnused = 0;
38727 ** the requested locking level, this routine is a no-op.
38742 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
38743 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
38747 if( pFile->eFileLock<=eFileLock ){
38750 pInode = pFile->pInode;
38751 sqlite3_mutex_enter(pInode->pLockMutex);
38752 assert( pInode->nShared!=0 );
38753 if( pFile->eFileLock>SHARED_LOCK ){
38754 assert( pInode->eFileLock==pFile->eFileLock );
38765 pFile->inNormalWrite = 0;
38769 ** before establishing the readlock - to avoid a race condition we downgrade
38785 off_t divSize = SHARED_SIZE - 1;
38791 if( unixFileLock(pFile, &lock)==(-1) ){
38801 if( unixFileLock(pFile, &lock)==(-1) ){
38812 lock.l_len = SHARED_SIZE-divSize;
38813 if( unixFileLock(pFile, &lock)==(-1) ){
38844 pInode->eFileLock = SHARED_LOCK;
38856 pInode->nShared--;
38857 if( pInode->nShared==0 ){
38862 pInode->eFileLock = NO_LOCK;
38866 pInode->eFileLock = NO_LOCK;
38867 pFile->eFileLock = NO_LOCK;
38875 pInode->nLock--;
38876 assert( pInode->nLock>=0 );
38877 if( pInode->nLock==0 ) closePendingFds(pFile);
38881 sqlite3_mutex_leave(pInode->pLockMutex);
38883 pFile->eFileLock = eFileLock;
38893 ** the requested locking level, this routine is a no-op.
38897 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
38922 if( pFile->h>=0 ){
38923 robust_close(pFile, pFile->h, __LINE__);
38924 pFile->h = -1;
38927 if( pFile->pId ){
38928 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
38929 osUnlink(pFile->pId->zCanonicalName);
38931 vxworksReleaseFileId(pFile->pId);
38932 pFile->pId = 0;
38936 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
38937 osUnlink(pFile->zPath);
38938 sqlite3_free(*(char**)&pFile->zPath);
38939 pFile->zPath = 0;
38942 OSTRACE(("CLOSE %-3d\n", pFile->h));
38943 OpenCounter(-1);
38944 sqlite3_free(pFile->pPreallocatedUnused);
38955 unixInodeInfo *pInode = pFile->pInode;
38966 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
38967 sqlite3_mutex_enter(pInode->pLockMutex);
38968 if( pInode->nLock ){
38971 ** descriptor to pInode->pUnused list. It will be automatically closed
38976 sqlite3_mutex_leave(pInode->pLockMutex);
38978 assert( pFile->pShm==0 );
38988 ****************************** No-op Locking **********************************
38994 ** This locking mode is appropriate for use on read-only databases
38995 ** (ex: databases that are burned into CD-ROM, for example.) It can
39025 /******************* End of the no-op lock implementation *********************
39029 ************************* Begin dot-file Locking ******************************
39059 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39074 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
39075 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
39081 ** Lock the file with the lock specified by parameter eFileLock - one
39095 ** UNLOCKED -> SHARED
39096 ** SHARED -> RESERVED
39097 ** SHARED -> (PENDING) -> EXCLUSIVE
39098 ** RESERVED -> (PENDING) -> EXCLUSIVE
39099 ** PENDING -> EXCLUSIVE
39109 char *zLockFile = (char *)pFile->lockingContext;
39116 if( pFile->eFileLock > NO_LOCK ){
39117 pFile->eFileLock = eFileLock;
39144 pFile->eFileLock = eFileLock;
39153 ** the requested locking level, this routine is a no-op.
39159 char *zLockFile = (char *)pFile->lockingContext;
39163 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
39164 pFile->eFileLock, osGetpid(0)));
39167 /* no-op if possible */
39168 if( pFile->eFileLock==eFileLock ){
39176 pFile->eFileLock = SHARED_LOCK;
39193 pFile->eFileLock = NO_LOCK;
39204 sqlite3_free(pFile->lockingContext);
39207 /****************** End of the dot-file lock implementation *******************
39215 ** flock() locking is like dot-file locking in that the various
39216 ** fine-grain locking levels supported by SQLite are collapsed into
39243 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39256 if( pFile->eFileLock>SHARED_LOCK ){
39263 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
39266 lrc = robust_flock(pFile->h, LOCK_UN);
39285 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
39298 ** Lock the file with the lock specified by parameter eFileLock - one
39312 ** UNLOCKED -> SHARED
39313 ** SHARED -> RESERVED
39314 ** SHARED -> (PENDING) -> EXCLUSIVE
39315 ** RESERVED -> (PENDING) -> EXCLUSIVE
39316 ** PENDING -> EXCLUSIVE
39334 if (pFile->eFileLock > NO_LOCK) {
39335 pFile->eFileLock = eFileLock;
39341 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
39350 pFile->eFileLock = eFileLock;
39352 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
39368 ** the requested locking level, this routine is a no-op.
39374 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
39375 pFile->eFileLock, osGetpid(0)));
39378 /* no-op if possible */
39379 if( pFile->eFileLock==eFileLock ){
39385 pFile->eFileLock = eFileLock;
39390 if( robust_flock(pFile->h, LOCK_UN) ){
39395 sqlite3_log(SQLITE_IOERR_UNLOCK, "IOERR_UNLOCK fd[%d], eFileLock[%d]", pFile->h, eFileLock);
39399 pFile->eFileLock = NO_LOCK;
39423 ** Semaphore locking is like dot-lock and flock in that it really only
39433 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39446 if( pFile->eFileLock>SHARED_LOCK ){
39452 sem_t *pSem = pFile->pInode->pSem;
39454 if( sem_trywait(pSem)==-1 ){
39461 reserved = (pFile->eFileLock < SHARED_LOCK);
39468 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
39475 ** Lock the file with the lock specified by parameter eFileLock - one
39489 ** UNLOCKED -> SHARED
39490 ** SHARED -> RESERVED
39491 ** SHARED -> (PENDING) -> EXCLUSIVE
39492 ** RESERVED -> (PENDING) -> EXCLUSIVE
39493 ** PENDING -> EXCLUSIVE
39505 sem_t *pSem = pFile->pInode->pSem;
39510 if (pFile->eFileLock > NO_LOCK) {
39511 pFile->eFileLock = eFileLock;
39517 if( sem_trywait(pSem)==-1 ){
39523 pFile->eFileLock = eFileLock;
39534 ** the requested locking level, this routine is a no-op.
39538 sem_t *pSem = pFile->pInode->pSem;
39542 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
39543 pFile->eFileLock, osGetpid(0)));
39546 /* no-op if possible */
39547 if( pFile->eFileLock==eFileLock ){
39553 pFile->eFileLock = eFileLock;
39558 if ( sem_post(pSem)==-1 ) {
39566 pFile->eFileLock = NO_LOCK;
39599 ** on Apple Macintosh computers - both OS9 and OSX.
39601 ** Third-party implementations of AFP are available. But this code here
39628 ** This is a utility for setting or clearing a bit-range lock on an
39647 pb.fd = pFile->h;
39650 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
39653 if ( err==-1 ) {
39676 ** to a non-zero value otherwise *pResOut is set to zero. The return value
39688 context = (afpLockingContext *) pFile->lockingContext;
39689 if( context->reserved ){
39693 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
39695 if( pFile->pInode->eFileLock>SHARED_LOCK ){
39703 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
39707 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
39717 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
39718 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
39725 ** Lock the file with the lock specified by parameter eFileLock - one
39739 ** UNLOCKED -> SHARED
39740 ** SHARED -> RESERVED
39741 ** SHARED -> (PENDING) -> EXCLUSIVE
39742 ** RESERVED -> (PENDING) -> EXCLUSIVE
39743 ** PENDING -> EXCLUSIVE
39751 unixInodeInfo *pInode = pFile->pInode;
39752 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
39755 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
39756 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
39757 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
39763 if( pFile->eFileLock>=eFileLock ){
39764 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
39774 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
39776 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
39778 /* This mutex is needed because pFile->pInode is shared across threads
39780 pInode = pFile->pInode;
39781 sqlite3_mutex_enter(pInode->pLockMutex);
39786 if( (pFile->eFileLock!=pInode->eFileLock &&
39787 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
39798 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
39800 assert( pFile->eFileLock==0 );
39801 assert( pInode->nShared>0 );
39802 pFile->eFileLock = SHARED_LOCK;
39803 pInode->nShared++;
39804 pInode->nLock++;
39813 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
39816 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
39830 assert( pInode->nShared==0 );
39831 assert( pInode->eFileLock==0 );
39834 /* Now get the read-lock SHARED_LOCK */
39837 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
39838 lrc1 = afpSetLock(context->dbPath, pFile,
39839 SHARED_FIRST+pInode->sharedByte, 1, 1);
39841 lrc1Errno = pFile->lastErrno;
39844 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
39856 pFile->eFileLock = SHARED_LOCK;
39857 pInode->nLock++;
39858 pInode->nShared = 1;
39860 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
39870 assert( 0!=pFile->eFileLock );
39871 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
39873 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
39875 context->reserved = 1;
39884 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
39885 pInode->sharedByte, 1, 0)) ){
39888 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
39890 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
39891 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
39909 pFile->eFileLock = eFileLock;
39910 pInode->eFileLock = eFileLock;
39912 pFile->eFileLock = PENDING_LOCK;
39913 pInode->eFileLock = PENDING_LOCK;
39917 sqlite3_mutex_leave(pInode->pLockMutex);
39918 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
39928 ** the requested locking level, this routine is a no-op.
39934 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
39937 int h = pFile->h;
39941 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
39942 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
39946 if( pFile->eFileLock<=eFileLock ){
39949 pInode = pFile->pInode;
39950 sqlite3_mutex_enter(pInode->pLockMutex);
39951 assert( pInode->nShared!=0 );
39952 if( pFile->eFileLock>SHARED_LOCK ){
39953 assert( pInode->eFileLock==pFile->eFileLock );
39955 SimulateIOError( h=(-1) )
39967 assert( pFile->inNormalWrite==0
39968 || pFile->dbUpdate==0
39969 || pFile->transCntrChng==1 );
39970 pFile->inNormalWrite = 0;
39973 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
39974 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
39975 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
39976 /* only re-establish the shared lock if necessary */
39977 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
39978 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
39983 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
39984 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
39986 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
39987 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
39989 context->reserved = 0;
39992 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
39993 pInode->eFileLock = SHARED_LOCK;
40002 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
40003 pInode->nShared--;
40004 if( pInode->nShared==0 ){
40006 SimulateIOError( h=(-1) )
40009 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
40012 pInode->eFileLock = NO_LOCK;
40013 pFile->eFileLock = NO_LOCK;
40017 pInode->nLock--;
40018 assert( pInode->nLock>=0 );
40019 if( pInode->nLock==0 ) closePendingFds(pFile);
40023 sqlite3_mutex_leave(pInode->pLockMutex);
40025 pFile->eFileLock = eFileLock;
40040 if( pFile->pInode ){
40041 unixInodeInfo *pInode = pFile->pInode;
40042 sqlite3_mutex_enter(pInode->pLockMutex);
40043 if( pInode->nLock ){
40046 ** descriptor to pInode->aPending. It will be automatically closed when
40051 sqlite3_mutex_leave(pInode->pLockMutex);
40054 sqlite3_free(pFile->lockingContext);
40064 ** is available. If you don't compile for a mac, then the "unix-afp"
40079 ** the requested locking level, this routine is a no-op.
40095 **************** Non-locking sqlite3_file methods *****************************
40125 assert( id->h>2 );
40128 got = osPread(id->h, pBuf, cnt, offset);
40129 SimulateIOError( got = -1 );
40131 got = osPread64(id->h, pBuf, cnt, offset);
40132 SimulateIOError( got = -1 );
40134 newOffset = lseek(id->h, offset, SEEK_SET);
40135 SimulateIOError( newOffset = -1 );
40138 return -1;
40140 got = osRead(id->h, pBuf, cnt);
40149 cnt -= got;
40156 OSTRACE(("READ %-3d %5d %7lld %llu\n",
40157 id->h, got+prior, offset-prior, TIMER_ELAPSED));
40178 /* If this is a database file (not a journal, super-journal or temp
40181 assert( pFile->pPreallocatedUnused==0
40190 if( offset<pFile->mmapSize ){
40191 if( offset+amt <= pFile->mmapSize ){
40192 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
40195 int nCopy = pFile->mmapSize - offset;
40196 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
40198 amt -= nCopy;
40208 /* pFile->lastErrno has been set by seekAndRead().
40215 switch( pFile->lastErrno ){
40225 …sqlite3_log(SQLITE_IOERR_CORRUPTFS, "unixRead-EDEVERR, fd:[%d], amt[%d], got[%d], offset[%lld]", p…
40230 …sqlite3_log(SQLITE_IOERR_READ, "unixRead-got<0, fd: [%d], amt[%d], got[%d], offset[%lld]", pFile->…
40235 /* Unread parts of the buffer must be zero-filled */
40236 memset(&((char*)pBuf)[got], 0, amt-got);
40242 ** Attempt to seek the file-descriptor passed as the first argument to
40244 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
40270 SimulateIOError( iSeek = -1 );
40272 rc = -1;
40280 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
40288 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
40295 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
40314 /* If this is a database file (not a journal, super-journal or temp
40317 assert( pFile->pPreallocatedUnused==0
40325 ** doing a hot-journal rollback or a write to some file other than a
40330 if( pFile->inNormalWrite ){
40331 pFile->dbUpdate = 1; /* The database has been modified */
40338 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
40339 pFile->transCntrChng = 1; /* The transaction counter has changed */
40348 if( offset<pFile->mmapSize ){
40349 if( offset+amt <= pFile->mmapSize ){
40350 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
40353 int nCopy = pFile->mmapSize - offset;
40354 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
40356 amt -= nCopy;
40363 amt -= wrote;
40367 SimulateIOError(( wrote=(-1), amt=1 ));
40371 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
40376 pFile->h,
40380 pFile->lastErrno);
40388 pFile->h,
40392 pFile->lastErrno);
40414 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
40484 ** no-op. But go ahead and call fstat() to validate the file
40517 if( rc==-1 && errno==ENOTSUP ){
40523 if( OS_VXWORKS && rc!= -1 ){
40536 ** The directory file descriptor is used for only one thing - to
40545 ** replace this routine with a harmless no-op. To make this routine
40546 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
40554 int fd = -1;
40558 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
40567 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
40582 ** has been created by fsync-ing the directory that contains the file.
40587 ** will not roll back - possibly leading to database corruption.
40607 OSTRACE(("SYNC %-3d\n", pFile->h));
40608 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
40612 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
40616 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
40619 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
40621 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
40623 rc = osOpenDirectory(pFile->zPath, &dirfd);
40631 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
40645 /* If the user has configured a chunk-size for this file, truncate the
40650 if( pFile->szChunk>0 ){
40651 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
40654 rc = robust_ftruncate(pFile->h, nByte);
40657 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
40661 ** doing a hot-journal rollback or a write to some file other than a
40664 ** when restoring a database using the backup API from a zero-length
40667 if( pFile->inNormalWrite && nByte==0 ){
40668 pFile->transCntrChng = 1;
40677 if( nByte<pFile->mmapSize ){
40678 pFile->mmapSize = nByte;
40693 rc = osFstat(((unixFile*)id)->h, &buf);
40701 /* When opening a zero-size database, the findInodeInfo() procedure
40703 ** in the OS-X msdos filesystem. In order to avoid problems with upper
40715 ** Handler for proxy-locking file-control verbs. Defined below in the
40723 ** file-control operation. Enlarge the database to nBytes in size
40724 ** (rounded up to the next chunk-size). If the database is already
40725 ** nBytes or larger, this routine is a no-op.
40728 if( pFile->szChunk>0 ){
40732 if( osFstat(pFile->h, &buf) ){
40736 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
40745 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
40750 "fcntlSizeHint-fallocate, fd[%d], bufSize[%lld], nSize[%lld] err[%d]",
40751 pFile->h,
40762 ** at offset (nSize-1), to set the size of the file correctly.
40766 int nBlk = buf.st_blksize; /* File-system block size */
40770 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
40773 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
40774 if( iWrite>=nSize ) iWrite = nSize - 1;
40779 "fcntlSizeHint-seekAndWrite, fd[%d], nWrite[%d], nSize[%d], nBlk[%d], iWrite[%lld]",
40780 pFile->h,
40794 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
40796 if( pFile->szChunk<=0 ){
40797 if( robust_ftruncate(pFile->h, nByte) ){
40799 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
40813 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
40815 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
40819 *pArg = (pFile->ctrlFlags & mask)!=0;
40821 pFile->ctrlFlags &= ~mask;
40823 pFile->ctrlFlags |= mask;
40841 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
40844 … sqlite3_log(SQLITE_IOERR_BEGIN_ATOMIC, "unixFileControl-begin, fd[%d], op[%d]", pFile->h, op);
40850 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
40853 … sqlite3_log(SQLITE_IOERR_COMMIT_ATOMIC, "unixFileControl-commit, fd[%d], op[%d]", pFile->h, op);
40859 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
40862 …sqlite3_log(SQLITE_IOERR_ROLLBACK_ATOMIC, "unixFileControl-rollback, fd[%d], op[%d]", pFile->h, op…
40870 *(int*)pArg = pFile->eFileLock;
40874 *(int*)pArg = pFile->lastErrno;
40878 pFile->szChunk = *(int *)pArg;
40897 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
40901 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
40903 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
40914 int iOld = pFile->iBusyTimeout;
40915 pFile->iBusyTimeout = *(int*)pArg;
40930 ** 64-bit type. */
40935 *(i64*)pArg = pFile->mmapSizeMax;
40936 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
40937 pFile->mmapSizeMax = newLimit;
40938 if( pFile->mmapSize>0 ){
40940 rc = unixMapfile(pFile, -1);
40953 ((unixFile*)id)->dbUpdate = 0;
40977 ** If pFd->sectorSize is non-zero when this function is called, it is a
40978 ** no-op. Otherwise, the values of pFd->sectorSize and
40979 ** pFd->deviceCharacteristics are set according to the file-system
40987 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
40988 if( pFd->sectorSize==0 ){
40994 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
40996 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
41001 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
41002 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
41005 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41012 if( pFile->sectorSize == 0 ){
41015 /* Set defaults for non-supported filesystems */
41016 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41017 pFile->deviceCharacteristics = 0;
41018 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
41023 pFile->sectorSize = fsInfo.f_bsize;
41024 pFile->deviceCharacteristics =
41032 pFile->sectorSize = fsInfo.f_bsize;
41033 pFile->deviceCharacteristics =
41035 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
41042 pFile->sectorSize = fsInfo.f_bsize;
41043 pFile->deviceCharacteristics =
41051 pFile->sectorSize = fsInfo.f_bsize;
41052 pFile->deviceCharacteristics =
41054 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41059 pFile->sectorSize = fsInfo.f_bsize;
41060 pFile->deviceCharacteristics =
41062 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
41067 pFile->deviceCharacteristics =
41076 if( pFile->sectorSize % 512 != 0 ){
41077 pFile->deviceCharacteristics = 0;
41078 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
41089 ** if two files are created in the same file-system directory (i.e.
41096 return pFd->sectorSize;
41105 ** words, after a power-loss event, parts of the file that were never
41106 ** written might end up being altered.) However, non-PSOW behavior is very,
41109 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
41115 return pFd->deviceCharacteristics;
41143 ** When multiple threads all reference the same wal-index, each thread
41145 ** of this unixShmNode object. In other words, each wal-index is opened
41160 ** The following fields are read-only after the object is created:
41174 int szRegion; /* Size of shared-memory regions */
41176 u8 isReadonly; /* True if read-only */
41178 char **apRegion; /* Array of mapped shared-memory regions */
41181 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
41194 ** are read-only thereafter:
41199 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
41205 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
41219 ** wal-mode transactions in other processes on database file pFile. If
41228 if( pFile->pShm){
41229 unixShmNode *pShmNode = pFile->pShm->pShmNode;
41236 f.l_len = SQLITE_SHM_NLOCK - 3;
41238 sqlite3_mutex_enter(pShmNode->pShmMutex);
41239 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
41244 sqlite3_mutex_leave(pShmNode->pShmMutex);
41252 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
41254 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
41263 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
41268 pShmNode = pFile->pInode->pShmNode;
41269 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
41270 assert( pShmNode->nRef>0 || unixMutexHeld() );
41278 if( pShmNode->hShm>=0 ){
41285 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
41286 if( res==-1 ){
41288 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
41298 OSTRACE(("SHM-LOCK "));
41299 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
41303 pShmNode->exclMask &= ~mask;
41304 pShmNode->sharedMask &= ~mask;
41306 OSTRACE(("read-lock %d ok", ofst));
41307 pShmNode->exclMask &= ~mask;
41308 pShmNode->sharedMask |= mask;
41311 OSTRACE(("write-lock %d ok", ofst));
41312 pShmNode->exclMask |= mask;
41313 pShmNode->sharedMask &= ~mask;
41319 OSTRACE(("read-lock failed"));
41322 OSTRACE(("write-lock %d failed", ofst));
41325 OSTRACE((" - afterwards %03x,%03x\n",
41326 pShmNode->sharedMask, pShmNode->exclMask));
41336 ** current system page-size.
41339 ** to use 64KB pages - in this case each mapping must cover at least two
41345 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
41353 ** This is not a VFS shared-memory method; it is a utility function called
41354 ** by VFS shared-memory methods.
41357 unixShmNode *p = pFd->pInode->pShmNode;
41359 if( p && ALWAYS(p->nRef==0) ){
41362 assert( p->pInode==pFd->pInode );
41363 sqlite3_mutex_free(p->pShmMutex);
41364 for(i=0; i<p->nRegion; i+=nShmPerMap){
41365 if( p->hShm>=0 ){
41366 osMunmap(p->apRegion[i], p->szRegion);
41368 sqlite3_free(p->apRegion[i]);
41371 sqlite3_free(p->apRegion);
41372 if( p->hShm>=0 ){
41373 robust_close(pFd, p->hShm, __LINE__);
41374 p->hShm = -1;
41376 p->pInode->pShmNode = 0;
41388 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
41397 ** and proceed with opening the *-shm file.
41401 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
41408 ** EXCLUSIVE failed just before truncating the *-shm file, then this
41409 ** process might open and use the *-shm file without truncating it.
41410 ** And if the *-shm file has been corrupted by a power failure or
41416 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
41419 if( pShmNode->isReadonly ){
41420 pShmNode->isUnlocked = 1;
41424 /* The first connection to attach must truncate the -shm file. We
41426 ** -shm header size) rather than 0 as a system debugging aid, to
41427 ** help detect if a -shm file truncation is legitimate or is the work
41429 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
41430 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
41445 ** Open a shared-memory area associated with open database file pDbFd.
41448 ** The file used to implement shared-memory is in the same directory
41450 ** file with the "-shm" suffix added. For example, if the database file
41452 ** for shared memory will be called "/home/user1/config.db-shm".
41458 ** database to end up using different files for shared memory -
41459 ** meaning that their memory would not really be shared - resulting
41461 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
41462 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
41466 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
41469 ** When opening a new shared-memory file, if no other instances of that
41473 ** If the original database file (pDbFd) is using the "unix-excl" VFS
41491 assert( pDbFd->pShm==0 );
41498 pInode = pDbFd->pInode;
41499 pShmNode = pInode->pShmNode;
41503 const char *zBasePath = pDbFd->zPath;
41507 ** a new *-shm file is created, an attempt will be made to create it
41510 if( osFstat(pDbFd->h, &sStat) ){
41526 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
41529 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
41532 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
41533 sqlite3FileSuffix3(pDbFd->zPath, zShm);
41535 pShmNode->hShm = -1;
41536 pDbFd->pInode->pShmNode = pShmNode;
41537 pShmNode->pInode = pDbFd->pInode;
41539 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
41540 if( pShmNode->pShmMutex==0 ){
41546 if( pInode->bProcessLock==0 ){
41547 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
41548 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
41551 if( pShmNode->hShm<0 ){
41552 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
41554 if( pShmNode->hShm<0 ){
41558 pShmNode->isReadonly = 1;
41565 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
41573 p->pShmNode = pShmNode;
41575 p->id = pShmNode->nextShmId++;
41577 pShmNode->nRef++;
41578 pDbFd->pShm = p;
41585 ** at pShmNode->pFirst. This must be done while holding the
41586 ** pShmNode->pShmMutex.
41588 sqlite3_mutex_enter(pShmNode->pShmMutex);
41589 p->pNext = pShmNode->pFirst;
41590 pShmNode->pFirst = p;
41591 sqlite3_mutex_leave(pShmNode->pShmMutex);
41604 ** shared-memory associated with the database file fd. Shared-memory regions
41605 ** are numbered starting from zero. Each shared-memory region is szRegion
41610 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
41613 ** bExtend is non-zero and the requested shared-memory region has not yet
41616 ** If the shared-memory region has already been allocated or is allocated by
41635 /* If the shared-memory file has not yet been opened, open it now. */
41636 if( pDbFd->pShm==0 ){
41641 p = pDbFd->pShm;
41642 pShmNode = p->pShmNode;
41643 sqlite3_mutex_enter(pShmNode->pShmMutex);
41644 if( pShmNode->isUnlocked ){
41647 pShmNode->isUnlocked = 0;
41649 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
41650 assert( pShmNode->pInode==pDbFd->pInode );
41651 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
41652 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
41657 if( pShmNode->nRegion<nReqRegion ){
41662 pShmNode->szRegion = szRegion;
41664 if( pShmNode->hShm>=0 ){
41666 ** Check to see if it has been allocated (i.e. if the wal-index file is
41669 if( osFstat(pShmNode->hShm, &sStat) ){
41697 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
41698 const char *zFile = pShmNode->zFilename;
41709 pShmNode->apRegion, nReqRegion*sizeof(char *)
41715 pShmNode->apRegion = apNew;
41716 while( pShmNode->nRegion<nReqRegion ){
41720 if( pShmNode->hShm>=0 ){
41722 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
41723 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
41726 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
41739 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
41741 pShmNode->nRegion += nShmPerMap;
41746 if( pShmNode->nRegion>iRegion ){
41747 *pp = pShmNode->apRegion[iRegion];
41751 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
41752 sqlite3_mutex_leave(pShmNode->pShmMutex);
41757 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
41767 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
41770 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
41773 if( pX->exclMask & (1<<i) ){
41775 aLock[i] = -1;
41776 }else if( pX->sharedMask & (1<<i) ){
41783 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
41784 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
41789 ** Change the lock state for a shared-memory segment.
41809 p = pDbFd->pShm;
41812 …sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShm, fd[%d], ofst[%d], n[%d], flags[%d]", pDbFd->h…
41816 pShmNode = p->pShmNode;
41819 …sqlite3_log(SQLITE_IOERR_SHMLOCK, "unixShmLock-pShmNode, fd[%d], ofst[%d], n[%d], flags[%d]", pDbF…
41823 aLock = pShmNode->aLock;
41825 assert( pShmNode==pDbFd->pInode->pShmNode );
41826 assert( pShmNode->pInode==pDbFd->pInode );
41834 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
41835 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
41851 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
41853 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
41854 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
41855 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
41859 mask = (1<<(ofst+n)) - (1<<ofst);
41861 sqlite3_mutex_enter(pShmNode->pShmMutex);
41864 if( (p->exclMask|p->sharedMask) & mask ){
41869 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
41879 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
41881 aLock[ofst]--;
41886 p->exclMask &= ~mask;
41887 p->sharedMask &= ~mask;
41892 assert( (p->exclMask & (1<<ofst))==0 );
41893 if( (p->sharedMask & mask)==0 ){
41902 p->sharedMask |= mask;
41911 assert( (p->sharedMask & mask)==0 );
41912 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
41919 ** also update the in-memory values. */
41923 assert( (p->sharedMask & mask)==0 );
41924 p->exclMask |= mask;
41926 aLock[ii] = -1;
41932 sqlite3_mutex_leave(pShmNode->pShmMutex);
41933 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
41934 p->id, osGetpid(0), p->sharedMask, p->exclMask));
41948 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
41949 assert( fd->pMethods->xLock==nolockLock
41957 ** Close a connection to shared-memory. Delete the underlying
41961 ** routine is a harmless no-op.
41965 int deleteFlag /* Delete shared-memory if true */
41968 unixShmNode *pShmNode; /* The underlying shared-memory file */
41973 p = pDbFd->pShm;
41975 pShmNode = p->pShmNode;
41977 assert( pShmNode==pDbFd->pInode->pShmNode );
41978 assert( pShmNode->pInode==pDbFd->pInode );
41982 sqlite3_mutex_enter(pShmNode->pShmMutex);
41983 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
41984 *pp = p->pNext;
41988 pDbFd->pShm = 0;
41989 sqlite3_mutex_leave(pShmNode->pShmMutex);
41991 /* If pShmNode->nRef has reached 0, then close the underlying
41992 ** shared-memory file, too */
41995 assert( pShmNode->nRef>0 );
41996 pShmNode->nRef--;
41997 if( pShmNode->nRef==0 ){
41998 if( deleteFlag && pShmNode->hShm>=0 ){
41999 osUnlink(pShmNode->zFilename);
42021 assert( pFd->nFetchOut==0 );
42022 if( pFd->pMapRegion ){
42023 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
42024 pFd->pMapRegion = 0;
42025 pFd->mmapSize = 0;
42026 pFd->mmapSizeActual = 0;
42050 int h = pFd->h; /* File descriptor open on db file */
42051 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
42052 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
42056 assert( pFd->nFetchOut==0 );
42057 assert( nNew>pFd->mmapSize );
42058 assert( nNew<=pFd->mmapSizeMax );
42060 assert( pFd->mmapSizeActual>=pFd->mmapSize );
42064 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
42069 i64 nReuse = pFd->mmapSize;
42072 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
42078 osMunmap(pReq, nOrig-nReuse);
42085 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
42088 osMunmap(pNew, nNew - nReuse);
42110 unixLogError(SQLITE_OK, zErr, pFd->zPath);
42115 pFd->mmapSizeMax = 0;
42117 pFd->pMapRegion = (void *)pNew;
42118 pFd->mmapSize = pFd->mmapSizeActual = nNew;
42122 ** Memory map or remap the file opened by file-descriptor pFd (if the file
42125 ** outstanding xFetch() references to it, this function is a no-op.
42127 ** If parameter nByte is non-negative, then it is the requested size of
42138 assert( nMap>=0 || pFd->nFetchOut==0 );
42139 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42140 if( pFd->nFetchOut>0 ) return SQLITE_OK;
42143 struct stat statbuf; /* Low-level file information */
42144 if( osFstat(pFd->h, &statbuf) ){
42149 if( nMap>pFd->mmapSizeMax ){
42150 nMap = pFd->mmapSizeMax;
42153 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
42154 if( nMap!=pFd->mmapSize ){
42181 if( pFd->mmapSizeMax>0 ){
42182 if( pFd->pMapRegion==0 ){
42183 int rc = unixMapfile(pFd, -1);
42186 if( pFd->mmapSize >= iOff+nAmt ){
42187 *pp = &((u8 *)pFd->pMapRegion)[iOff];
42188 pFd->nFetchOut++;
42196 ** If the third argument is non-NULL, then this function releases a
42213 assert( (p==0)==(pFd->nFetchOut==0) );
42216 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
42219 pFd->nFetchOut--;
42224 assert( pFd->nFetchOut>=0 );
42242 ** of "finder" functions. A finder-function is used to locate the appropriate
42245 ** the correct finder-function for that VFS.
42248 ** object. The only interesting finder-function is autolockIoFinder, which
42252 ** For finder-function F, two objects are created:
42254 ** (1) The real finder-function named "FImpt()".
42261 ** directly at the finder-function since C90 rules prevent a void*
42378 ** The proxy locking method is a "super-method" in the sense that it
42380 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
42449 if( statfs(filePath, &fsInfo) != -1 ){
42461 ** Test byte-range lock using fcntl(). If the call succeeds,
42462 ** assume that the file-system supports POSIX style locks.
42468 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42508 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
42546 assert( pNew->pInode==NULL );
42551 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
42552 pNew->h = h;
42553 pNew->pVfs = pVfs;
42554 pNew->zPath = zFilename;
42555 pNew->ctrlFlags = (u8)ctrlFlags;
42557 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
42561 pNew->ctrlFlags |= UNIXFILE_PSOW;
42563 if( strcmp(pVfs->zName,"unix-excl")==0 ){
42564 pNew->ctrlFlags |= UNIXFILE_EXCL;
42568 pNew->pId = vxworksFindFileId(zFilename);
42569 if( pNew->pId==0 ){
42578 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
42583 pNew->lockingContext = (void*)zFilename;
42593 rc = findInodeInfo(pNew, &pNew->pInode);
42606 ** handle h - as it is guaranteed that no posix locks will be released
42614 h = -1;
42625 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
42632 pCtx->dbPath = zFilename;
42633 pCtx->reserved = 0;
42636 rc = findInodeInfo(pNew, &pNew->pInode);
42638 sqlite3_free(pNew->lockingContext);
42640 h = -1;
42661 pNew->lockingContext = zLockFile;
42670 rc = findInodeInfo(pNew, &pNew->pInode);
42671 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
42672 char *zSemName = pNew->pInode->aSemName;
42675 pNew->pId->zCanonicalName);
42678 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
42679 if( pNew->pInode->pSem == SEM_FAILED ){
42681 pNew->pInode->aSemName[0] = '\0';
42692 h = -1;
42694 pNew->ctrlFlags |= UNIXFILE_DELETE;
42700 pId->pMethods = pLockingStyle;
42753 ** pVfs->mxPathname bytes.
42760 /* It's odd to simulate an io-error here, but really this is just
42761 ** using the io-error infrastructure to test that SQLite handles this
42776 zBuf[nBuf-2] = 0;
42779 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){
42791 ** Routine to transform a unixFile into a proxy-locking unixFile.
42792 ** Implementation in the proxy-lock division, but used by unixOpen()
42800 ** file (not a journal or super-journal file) identified by pathname
42806 ** other file descriptor open on the same file is holding a file-lock.
42812 ** such file descriptor is located, -1 is returned.
42830 ** ignored and -1 is returned. The caller will try to open a new file
42839 while( pInode && (pInode->fileId.dev!=sStat.st_dev
42840 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
42841 pInode = pInode->pNext;
42845 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
42846 sqlite3_mutex_enter(pInode->pLockMutex);
42848 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
42851 *pp = pUnused->pNext;
42853 sqlite3_mutex_leave(pInode->pLockMutex);
42893 ** this function queries the file-system for the permissions on the
42922 ** "<path to db>-journal"
42923 ** "<path to db>-wal"
42924 ** "<path to db>-journalNN"
42925 ** "<path to db>-walNN"
42931 ** a '-' character. However in 8+3 filename mode, or if a corrupt
42932 ** rollback journal specifies a super-journal with a goofy name, then
42933 ** the '-' might be missing or the '-' might be the first character in
42936 nDb = sqlite3Strlen30(zPath) - 1;
42938 if( zPath[nDb]=='-' ){
42944 nDb--;
42973 ** ReadWrite() -> (READWRITE | CREATE)
42974 ** ReadOnly() -> (READONLY)
42975 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
42977 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
42991 int fd = -1; /* File descriptor returned by open() */
43010 /* If creating a super- or main-file journal, this function will open
43011 ** a file-descriptor on the directory too. The first time unixSync()
43038 /* The main DB, main journal, WAL file and super-journal are never
43045 /* Assert that the upper layer has set one of the "file-type" flags. */
43072 fd = pUnused->fd;
43079 p->pPreallocatedUnused = pUnused;
43081 /* Database filenames are double-zero terminated if they are not
43089 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
43095 /* Generated temporary filenames are always double-zero terminated
43116 assert( !p->pPreallocatedUnused );
43121 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
43129 /* Failed to open the file for read/write access. Try read-only. */
43146 ** the case. The chown() system call will be a no-op if the current
43165 if( p->pPreallocatedUnused ){
43166 p->pPreallocatedUnused->fd = fd;
43167 p->pPreallocatedUnused->flags =
43186 p->openFlags = openFlags;
43191 if( fstatfs(fd, &fsInfo) == -1 ){
43200 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43203 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
43219 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
43224 ** never use proxy, NULL means use proxy for non-local files only. */
43237 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
43255 sqlite3_free(p->pPreallocatedUnused);
43273 if( osUnlink(zPath)==(-1) ){
43338 ** If the last component of the pathname in z[0]..z[j-1] is something
43346 for(i=j-1; i>0 && z[i-1]!='/'; i--){}
43348 if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0;
43349 *pJ = i-1;
43369 if( osGetcwd(zOut, nOut-2)==0 ){
43376 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
43381 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
43422 ** is stored as a nul-terminated string in the buffer pointed to by
43426 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
43444 assert( pVfs->mxPathname==MAX_PATHNAME );
43447 /* It's odd to simulate an io-error here, but really this is just
43448 ** using the io-error infrastructure to test that SQLite handles this
43478 nByte = osReadlink(zIn, zDel, nOut-1);
43484 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
43544 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
43547 ** use dlsym() with -pedantic-errors?
43555 ** This work-around is unlikely to work correctly on any system where
43584 ** errors. The reports issued by valgrind are incorrect - we would
43586 ** uninitialized space in zBuf - but valgrind errors tend to worry
43649 ** The following variable, if set to a non-zero value, is interpreted as
43713 ** low-level error message when operating-system problems come up
43732 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
43734 ** meta-layer over top of the primitive locking implemented above. For
43737 ** been defined - so that the primitive locking methods are available
43742 ** The default locking schemes in SQLite use byte-range locks on the
43747 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
43755 ** SHARED_RANGE 0x40000002 -> 0x40000200
43759 ** the read cache when byte-range locks are present. Enabling the read
43762 ** close-to-open semantics for ensuring cache coherency
43766 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
43775 ** -----------------
43807 ** -----------------------
43817 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
43818 ** by taking an sqlite-style shared lock on the conch file, reading the
43822 ** is patterned after the database file name as ".<databasename>-conch".
43830 ** The proxy file - a single-byte file used for all advisory file locks
43846 ** ---------------------
43850 ** Database files accessed on non-local file systems are
43895 int conchHeld; /* 1 if the conch is held, -1 if lockless */
43931 if( lPath[len-1]!='/' ){
43960 if( lockPath[i] == '/' && (i - start > 0) ){
43962 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
43963 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
43995 int fd = -1;
44004 ** 2. if that fails, and this is a lock file (not-conch), try creating
44006 ** 3. if that fails, try to open the file read-only
44011 fd = pUnused->fd;
44040 sqlite3_log(SQLITE_IOERR_LOCK, "proxyCreateUnixFile-EIO, fd[%d]", fd);
44053 pNew->openFlags = openFlags;
44057 pUnused->fd = fd;
44058 pUnused->flags = openFlags;
44059 pNew->pPreallocatedUnused = pUnused;
44093 struct timespec timeout = {1, 0}; /* 1 sec timeout */
44094 if( gethostuuid(pHostID, &timeout) ){
44117 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
44129 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44130 unixFile *conchFile = pCtx->conchFile;
44133 char *cPath = pCtx->conchFilePath;
44137 int fd = -1;
44138 int rc = -1;
44141 /* create a new path by replace the trailing '-conch' with '-break' */
44144 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
44149 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
44170 robust_close(pFile, conchFile->h, __LINE__);
44171 conchFile->h = fd;
44172 conchFile->openFlags = O_RDWR | O_CREAT;
44189 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44190 unixFile *conchFile = pCtx->conchFile;
44197 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44207 if( osFstat(conchFile->h, &buf) ){
44210 …CK, "proxyConchLock pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno);
44229 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
44233 …yConchLock tries 2, pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, errno);
44254 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
44257 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
44267 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
44273 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44275 if( pCtx->conchHeld!=0 ){
44278 unixFile *conchFile = pCtx->conchFile;
44291 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
44292 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44308 storeLastErrno(pFile, conchFile->lastErrno);
44310 …akeConch pFile fd[%d], conchFile fd[%d], lastErrno[%d]", pFile->h, conchFile->h, conchFile->lastEr…
44323 ** retry with a new auto-generated path
44331 if( !pCtx->lockProxyPath ){
44332 /* for auto-named local lock file, just check the host ID and we'll
44336 size_t pathLen = (readLen - PROXY_PATHINDEX);
44339 pathLen=MAXPATHLEN-1;
44349 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
44350 readLen-PROXY_PATHINDEX)
44358 if( (conchFile->openFlags&O_RDWR) == 0 ){
44364 if( !pCtx->lockProxyPath ){
44365 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
44374 futimes(conchFile->h, NULL);
44376 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
44392 if( pCtx->lockProxyPath!=NULL ){
44393 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
44399 robust_ftruncate(conchFile->h, writeSize);
44401 full_fsync(conchFile->h,0,0);
44407 int err = osFstat(pFile->h, &buf);
44413 osFchmod(conchFile->h, cmode);
44416 rc = osFchmod(conchFile->h, cmode);
44417 }while( rc==(-1) && errno==EINTR );
44433 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
44436 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
44437 if( rc==SQLITE_OK && pFile->openFlags ){
44439 if( pFile->h>=0 ){
44440 robust_close(pFile, pFile->h, __LINE__);
44442 pFile->h = -1;
44443 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
44446 pFile->h = fd;
44452 if( rc==SQLITE_OK && !pCtx->lockProxy ){
44453 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
44454 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
44457 ** so try again via auto-naming
44469 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
44470 if( !pCtx->lockProxyPath ){
44476 pCtx->conchHeld = 1;
44478 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
44480 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
44481 afpCtx->dbPath = pCtx->lockProxyPath;
44484 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44486 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
44489 } while (1); /* in case we need to retry the :auto: lock file -
44502 pCtx = (proxyLockingContext *)pFile->lockingContext;
44503 conchFile = pCtx->conchFile;
44504 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
44505 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
44507 if( pCtx->conchHeld>0 ){
44508 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
44510 pCtx->conchHeld = 0;
44511 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
44529 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
44541 for( i=(len-1); i>=0; i-- ){
44553 /* append the "-conch" suffix to the file */
44554 memcpy(&conchPath[i+1], "-conch", 7);
44561 /* Takes a fully configured proxy locking-style unix file and switches
44565 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44566 char *oldPath = pCtx->lockProxyPath;
44569 if( pFile->eFileLock!=NO_LOCK ){
44578 unixFile *lockProxy = pCtx->lockProxy;
44579 pCtx->lockProxy=NULL;
44580 pCtx->conchHeld = 0;
44582 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
44587 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
44602 if( pFile->pMethod == &afpIoMethods ){
44605 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44606 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
44610 if( pFile->pMethod == &dotlockIoMethods ){
44613 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
44614 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
44617 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
44618 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
44628 ** ->lockingContext
44629 ** ->pMethod
44637 if( pFile->eFileLock!=NO_LOCK ){
44647 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
44656 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
44658 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
44659 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
44661 ** (c) the file system is read-only, then enable no-locking access.
44669 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
44671 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
44676 pCtx->conchHeld = -1; /* read only FS/ lockless */
44682 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
44686 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
44687 if( pCtx->dbPath==NULL ){
44695 pCtx->oldLockingContext = pFile->lockingContext;
44696 pFile->lockingContext = pCtx;
44697 pCtx->pOldMethod = pFile->pMethod;
44698 pFile->pMethod = &proxyIoMethods;
44700 if( pCtx->conchFile ){
44701 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
44702 sqlite3_free(pCtx->conchFile);
44704 sqlite3DbFree(0, pCtx->lockProxyPath);
44705 sqlite3_free(pCtx->conchFilePath);
44708 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
44722 if( pFile->pMethod == &proxyIoMethods ){
44723 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
44725 if( pCtx->lockProxyPath ){
44726 *(const char **)pArg = pCtx->lockProxyPath;
44738 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
44741 /* turn off proxy locking - not supported. If support is added for
44747 /* turn off proxy locking - already off - NOOP */
44754 (proxyLockingContext*)pFile->lockingContext;
44756 || (pCtx->lockProxyPath &&
44757 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
44780 ** above this point are all utilities. The lock-related methods of the
44781 ** proxy-locking sqlite3_io_method object follow.
44788 ** to a non-zero value otherwise *pResOut is set to zero. The return value
44795 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44796 if( pCtx->conchHeld>0 ){
44797 unixFile *proxy = pCtx->lockProxy;
44798 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
44807 ** Lock the file with the lock specified by parameter eFileLock - one
44821 ** UNLOCKED -> SHARED
44822 ** SHARED -> RESERVED
44823 ** SHARED -> (PENDING) -> EXCLUSIVE
44824 ** RESERVED -> (PENDING) -> EXCLUSIVE
44825 ** PENDING -> EXCLUSIVE
44834 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44835 if( pCtx->conchHeld>0 ){
44836 unixFile *proxy = pCtx->lockProxy;
44837 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
44838 pFile->eFileLock = proxy->eFileLock;
44852 ** the requested locking level, this routine is a no-op.
44858 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44859 if( pCtx->conchHeld>0 ){
44860 unixFile *proxy = pCtx->lockProxy;
44861 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
44862 pFile->eFileLock = proxy->eFileLock;
44876 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
44877 unixFile *lockProxy = pCtx->lockProxy;
44878 unixFile *conchFile = pCtx->conchFile;
44882 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
44884 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
44887 pCtx->lockProxy = 0;
44890 if( pCtx->conchHeld ){
44894 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
44898 sqlite3DbFree(0, pCtx->lockProxyPath);
44899 sqlite3_free(pCtx->conchFilePath);
44900 sqlite3DbFree(0, pCtx->dbPath);
44902 pFile->lockingContext = pCtx->oldLockingContext;
44903 pFile->pMethod = pCtx->pOldMethod;
44905 return pFile->pMethod->xClose(id);
44925 ** This routine registers all VFS implementations for unix-like operating
44942 ** when compiling with -pedantic-errors on GCC.)
44945 ** finder-function. The finder-function returns a pointer to the
44948 ** macro for addition information on finder-functions.
44996 UNIXVFS("unix-none", nolockIoFinder ),
44997 UNIXVFS("unix-dotfile", dotlockIoFinder ),
44998 UNIXVFS("unix-excl", posixIoFinder ),
45000 UNIXVFS("unix-namedsem", semIoFinder ),
45003 UNIXVFS("unix-posix", posixIoFinder ),
45006 UNIXVFS("unix-flock", flockIoFinder ),
45009 UNIXVFS("unix-afp", afpIoFinder ),
45010 UNIXVFS("unix-nfs", nfsIoFinder ),
45011 UNIXVFS("unix-proxy", proxyIoFinder ),
45016 /* Double-check that the aSyscall[] array has been constructed
45042 ** READ-0 UNIX_SHM_BASE+3 123
45043 ** READ-1 UNIX_SHM_BASE+4 124
45044 ** READ-2 UNIX_SHM_BASE+5 125
45045 ** READ-3 UNIX_SHM_BASE+6 126
45046 ** READ-4 UNIX_SHM_BASE+7 127
45049 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
45063 ** This routine is a no-op for unix.
45117 ** based on the sub-platform)?
45125 ** based on the sub-platform)?
45213 ** characters, so we allocate 4 bytes per character assuming worst-case of
45214 ** 4-bytes-per-character for UTF8.
45237 ** Returns non-zero if the character should be treated as a directory
45301 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
45314 typedef struct winShm winShm; /* A connection to shared-memory */
45315 typedef struct winShmNode winShmNode; /* A region of shared-memory */
45373 BOOL bNoLock; /* Non-zero if locking is disabled. */
45387 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
45397 * If this is non-zero, an isolated heap will be created by the native Win32
45403 * WARNING: It is important to note that when this setting is non-zero and the
45415 * This is the maximum possible initial size of the Win32-specific heap, in
45423 * This is the extra space for the initial size of the Win32-specific heap,
45436 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
45443 * Win32-specific heap. It cannot be negative.
45449 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
45455 * initial size of the Win32-specific heap to exceed the maximum amount
45464 * The initial size of the Win32-specific heap. This value may be zero.
45473 * The maximum size of the Win32-specific heap. This value may be zero.
45489 ** The winMemData structure stores information required by the Win32-specific
45573 ** Many system calls are accessed through pointer-to-functions so that
46190 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
46302 int i = -1;
46306 for(i=0; i<ArraySize(aSyscall)-1; i++){
46321 ** "pnLargest" argument, if non-zero, will be used to return the size of the
46411 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
46412 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
46413 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
46479 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
46507 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
46532 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
46627 if( n==(SIZE_T)-1 ){
46649 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46650 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46653 if( !pWinMemData->hHeap ){
46661 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
46663 if( !pWinMemData->hHeap ){
46670 pWinMemData->bOwned = TRUE;
46671 assert( pWinMemData->bOwned );
46674 pWinMemData->hHeap = osGetProcessHeap();
46675 if( !pWinMemData->hHeap ){
46680 pWinMemData->bOwned = FALSE;
46681 assert( !pWinMemData->bOwned );
46683 assert( pWinMemData->hHeap!=0 );
46684 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46686 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46698 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
46699 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
46701 if( pWinMemData->hHeap ){
46702 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
46704 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
46706 if( pWinMemData->bOwned ){
46707 if( !osHeapDestroy(pWinMemData->hHeap) ){
46709 osGetLastError(), (void*)pWinMemData->hHeap);
46711 pWinMemData->bOwned = FALSE;
46713 pWinMemData->hHeap = NULL;
46718 ** Populate the low-level memory allocation function pointers in
46745 ** Convert a UTF-8 string to Microsoft Unicode.
46753 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
46761 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
46771 ** Convert a Microsoft Unicode string to UTF-8.
46779 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
46787 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
46807 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
46816 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
46826 ** Convert a Microsoft Unicode string to a multi-byte character string,
46836 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
46844 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
46854 ** Convert a multi-byte character string to UTF-8.
46872 ** Convert a UTF-8 string to a multi-byte character string.
46987 ** it accepts a UTF-8 string.
47031 ** it accepts a UTF-16 string.
47064 ** is zero if the error message fits in the buffer, or non-zero
47144 ** This function - winLogErrorAtLine() - is only ever called via the macro
47149 ** error code and, if possible, the human-readable equivalent from
47155 ** failed and the associated file-system path, if any.
47175 "os_win.c:%d: (%lu) %s(%s) - %s",
47184 ** will be retried following a locking error - probably caused by
47201 ** non-zero if the error code is transient in nature and the operation
47289 y.tm_year = pTm.wYear - 1900;
47290 y.tm_mon = pTm.wMonth - 1;
47304 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
47338 memset(&pFile->local, 0, sizeof(pFile->local));
47348 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
47349 if (!pFile->hMutex){
47350 pFile->lastErrno = osGetLastError();
47352 return winLogError(SQLITE_IOERR, pFile->lastErrno,
47357 winceMutexAcquire(pFile->hMutex);
47360 ** case-sensitive, take advantage of that by uppercasing the mutex name
47364 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
47369 ** must be zero-initialized */
47378 if( pFile->hShared ){
47379 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
47382 if( !pFile->shared ){
47383 pFile->lastErrno = osGetLastError();
47384 winLogError(SQLITE_IOERR, pFile->lastErrno,
47387 osCloseHandle(pFile->hShared);
47388 pFile->hShared = NULL;
47393 if( pFile->hShared==NULL ){
47395 pFile->lastErrno = lastErrno;
47396 winLogError(SQLITE_IOERR, pFile->lastErrno,
47400 winceMutexRelease(pFile->hMutex);
47401 osCloseHandle(pFile->hMutex);
47402 pFile->hMutex = NULL;
47408 memset(pFile->shared, 0, sizeof(winceLock));
47411 winceMutexRelease(pFile->hMutex);
47419 if (pFile->hMutex){
47421 winceMutexAcquire(pFile->hMutex);
47425 if (pFile->local.nReaders){
47426 pFile->shared->nReaders --;
47428 if (pFile->local.bReserved){
47429 pFile->shared->bReserved = FALSE;
47431 if (pFile->local.bPending){
47432 pFile->shared->bPending = FALSE;
47434 if (pFile->local.bExclusive){
47435 pFile->shared->bExclusive = FALSE;
47438 /* De-reference and close our copy of the shared memory handle */
47439 osUnmapViewOfFile(pFile->shared);
47440 osCloseHandle(pFile->hShared);
47443 winceMutexRelease(pFile->hMutex);
47444 osCloseHandle(pFile->hMutex);
47445 pFile->hMutex = NULL;
47465 if (!pFile->hMutex) return TRUE;
47466 winceMutexAcquire(pFile->hMutex);
47471 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
47472 pFile->shared->bExclusive = TRUE;
47473 pFile->local.bExclusive = TRUE;
47478 /* Want a read-only lock? */
47481 if (pFile->shared->bExclusive == 0){
47482 pFile->local.nReaders ++;
47483 if (pFile->local.nReaders == 1){
47484 pFile->shared->nReaders ++;
47494 if (pFile->shared->bPending == 0) {
47495 pFile->shared->bPending = TRUE;
47496 pFile->local.bPending = TRUE;
47504 if (pFile->shared->bReserved == 0) {
47505 pFile->shared->bReserved = TRUE;
47506 pFile->local.bReserved = TRUE;
47511 winceMutexRelease(pFile->hMutex);
47531 if (!pFile->hMutex) return TRUE;
47532 winceMutexAcquire(pFile->hMutex);
47537 if (pFile->local.bExclusive){
47539 pFile->local.bExclusive = FALSE;
47540 pFile->shared->bExclusive = FALSE;
47545 else if (pFile->local.nReaders){
47548 pFile->local.nReaders --;
47549 if (pFile->local.nReaders == 0)
47551 pFile->shared->nReaders --;
47560 if (pFile->local.bPending){
47561 pFile->local.bPending = FALSE;
47562 pFile->shared->bPending = FALSE;
47569 if (pFile->local.bReserved) {
47570 pFile->local.bReserved = FALSE;
47571 pFile->shared->bReserved = FALSE;
47576 winceMutexRelease(pFile->hMutex);
47656 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
47662 ** Otherwise, set pFile->lastErrno and return non-zero.
47671 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
47677 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
47683 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
47687 pFile->lastErrno = lastErrno;
47688 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47689 "winSeekFile", pFile->zPath);
47690 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47694 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
47705 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
47708 pFile->lastErrno = osGetLastError();
47709 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
47710 "winSeekFile", pFile->zPath);
47711 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
47715 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
47743 assert( pFile->pShm==0 );
47745 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
47747 osGetCurrentProcessId(), pFile, pFile->h));
47754 rc = osCloseHandle(pFile->h);
47760 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
47761 if( pAppData==NULL || !pAppData->bNoLock ){
47765 if( pFile->zDeleteOnClose ){
47768 osDeleteFileW(pFile->zDeleteOnClose)==0
47769 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
47774 sqlite3_free(pFile->zDeleteOnClose);
47778 pFile->h = NULL;
47780 OpenCounter(-1);
47782 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
47785 "winClose", pFile->zPath);
47812 pFile->h, pBuf, amt, offset, pFile->locktype));
47817 if( offset<pFile->mmapSize ){
47818 if( offset+amt <= pFile->mmapSize ){
47819 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
47820 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
47821 osGetCurrentProcessId(), pFile, pFile->h));
47824 int nCopy = (int)(pFile->mmapSize - offset);
47825 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
47827 amt -= nCopy;
47836 osGetCurrentProcessId(), pFile, pFile->h));
47839 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
47844 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
47849 pFile->lastErrno = lastErrno;
47851 osGetCurrentProcessId(), pFile, pFile->h));
47852 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
47853 "winRead", pFile->zPath);
47857 /* Unread parts of the buffer must be zero-filled */
47858 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
47860 osGetCurrentProcessId(), pFile, pFile->h));
47865 osGetCurrentProcessId(), pFile, pFile->h));
47890 pFile->h, pBuf, amt, offset, pFile->locktype));
47895 if( offset<pFile->mmapSize ){
47896 if( offset+amt <= pFile->mmapSize ){
47897 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
47898 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
47899 osGetCurrentProcessId(), pFile, pFile->h));
47902 int nCopy = (int)(pFile->mmapSize - offset);
47903 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
47905 amt -= nCopy;
47933 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
47935 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
47951 nRem -= nWrite;
47954 pFile->lastErrno = lastErrno;
47960 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
47961 || ( pFile->lastErrno==ERROR_DISK_FULL )){
47963 osGetCurrentProcessId(), pFile, pFile->h));
47964 return winLogError(SQLITE_FULL, pFile->lastErrno,
47965 "winWrite1", pFile->zPath);
47968 osGetCurrentProcessId(), pFile, pFile->h));
47969 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
47970 "winWrite2", pFile->zPath);
47975 osGetCurrentProcessId(), pFile, pFile->h));
47988 if( pFile->nFetchOut>0 ){
47989 /* File truncation is a no-op if there are outstanding memory mapped
47995 ** No real harm comes of this - the database file is not corrupted,
47999 ** The only feasible work-around is to defer the truncation until after
48000 ** all references to memory-mapped content are closed. That is doable,
48003 ** now to simply make trancations a no-op if there are pending reads. We
48013 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
48015 /* If the user has configured a chunk-size for this file, truncate the
48020 if( pFile->szChunk>0 ){
48021 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
48025 if( pFile->pMapRegion ){
48026 oldMmapSize = pFile->mmapSize;
48033 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
48035 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48036 "winTruncate1", pFile->zPath);
48037 }else if( 0==osSetEndOfFile(pFile->h) &&
48039 pFile->lastErrno = lastErrno;
48040 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
48041 "winTruncate2", pFile->zPath);
48047 winMapfile(pFile, -1);
48055 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
48101 osGetCurrentProcessId(), pFile, pFile->h, flags,
48102 pFile->locktype));
48114 ** no-op
48117 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
48118 osGetCurrentProcessId(), pFile, pFile->h));
48122 if( pFile->pMapRegion ){
48123 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
48124 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48126 pFile, pFile->pMapRegion));
48128 pFile->lastErrno = osGetLastError();
48129 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
48131 pFile, pFile->pMapRegion));
48132 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
48133 "winSync1", pFile->zPath);
48137 rc = osFlushFileBuffers(pFile->h);
48141 osGetCurrentProcessId(), pFile, pFile->h));
48144 pFile->lastErrno = osGetLastError();
48146 osGetCurrentProcessId(), pFile, pFile->h));
48147 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
48148 "winSync2", pFile->zPath);
48163 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
48168 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
48172 pFile->lastErrno = osGetLastError();
48173 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48174 "winFileSize", pFile->zPath);
48183 lowerBits = osGetFileSize(pFile->h, &upperBits);
48187 pFile->lastErrno = lastErrno;
48188 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
48189 "winFileSize", pFile->zPath);
48194 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
48236 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48243 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
48245 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
48253 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
48254 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48255 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48259 pFile->lastErrno = osGetLastError();
48262 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
48272 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
48274 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48278 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
48282 pFile->lastErrno = lastErrno;
48283 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
48284 "winUnlockReadLock", pFile->zPath);
48286 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
48291 ** Lock the file with the lock specified by parameter locktype - one
48305 ** UNLOCKED -> SHARED
48306 ** SHARED -> RESERVED
48307 ** SHARED -> (PENDING) -> EXCLUSIVE
48308 ** RESERVED -> (PENDING) -> EXCLUSIVE
48309 ** PENDING -> EXCLUSIVE
48319 int newLocktype; /* Set pFile->locktype to this value before exiting */
48326 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48332 if( pFile->locktype>=locktype ){
48333 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
48337 /* Do not allow any kind of write-lock on a read-only database
48339 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
48345 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
48347 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
48353 newLocktype = pFile->locktype;
48354 if( pFile->locktype==NO_LOCK
48355 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
48358 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
48361 ** around problems caused by indexing and/or anti-virus software on
48367 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
48368 pFile->h, cnt, res));
48370 pFile->lastErrno = lastErrno;
48372 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
48373 pFile->h, cnt, sqlite3ErrName(rc)));
48387 assert( pFile->locktype==NO_LOCK );
48399 assert( pFile->locktype==SHARED_LOCK );
48400 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
48418 assert( pFile->locktype>=SHARED_LOCK );
48420 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
48434 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48443 pFile->lastErrno = lastErrno;
48445 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
48446 pFile->h, locktype, newLocktype));
48448 pFile->locktype = (u8)newLocktype;
48450 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48457 ** non-zero, otherwise zero.
48464 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
48467 if( pFile->locktype>=RESERVED_LOCK ){
48469 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
48471 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
48473 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48476 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
48479 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
48480 pFile->h, pResOut, *pResOut));
48489 ** the requested locking level, this routine is a no-op.
48502 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
48503 type = pFile->locktype;
48505 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
48510 "winUnlock", pFile->zPath);
48514 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
48520 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
48522 pFile->locktype = (u8)locktype;
48524 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
48529 ****************************** No-op Locking **********************************
48535 ** This locking mode is appropriate for use on read-only databases
48536 ** (ex: databases that are burned into CD-ROM, for example.) It can
48563 /******************* End of the no-op lock implementation *********************
48568 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
48570 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
48574 *pArg = (pFile->ctrlFlags & mask)!=0;
48576 pFile->ctrlFlags &= ~mask;
48578 pFile->ctrlFlags |= mask;
48593 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
48596 *(int*)pArg = pFile->locktype;
48597 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48601 *(int*)pArg = (int)pFile->lastErrno;
48602 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48606 pFile->szChunk = *(int *)pArg;
48607 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48611 if( pFile->szChunk>0 ){
48622 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48625 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48630 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48635 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48639 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
48640 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48655 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48660 *phFile = pFile->h;
48661 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
48667 HANDLE hOldFile = pFile->h;
48668 pFile->h = *phFile;
48671 hOldFile, pFile->h));
48677 int rc = winGetTempname(pFile->pVfs, &zTFile);
48681 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48694 ** least a 64-bit type. */
48699 *(i64*)pArg = pFile->mmapSizeMax;
48700 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
48701 pFile->mmapSizeMax = newLimit;
48702 if( pFile->mmapSize>0 ){
48704 rc = winMapfile(pFile, -1);
48707 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
48712 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
48722 ** if two files are created in the same file-system directory (i.e.
48737 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
48779 ** log-summary, each thread has its own winFile object, but they all
48781 ** log-summary is opened only once per process.
48789 ** The following fields are read-only after the object is created:
48804 int szRegion; /* Size of shared-memory regions */
48806 u8 isReadonly; /* True if read-only */
48835 ** are read-only thereafter:
48840 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
48867 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
48875 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
48877 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
48878 pFile->hFile.h, lockType, ofst, nByte));
48880 /* Release/Acquire the system-level lock */
48882 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
48887 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
48893 pFile->lastErrno = osGetLastError();
48897 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
48898 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
48899 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
48911 ** This is not a VFS shared-memory method; it is a utility function called
48912 ** by VFS shared-memory methods.
48918 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
48922 if( p->nRef==0 ){
48924 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
48925 for(i=0; i<p->nRegion; i++){
48926 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
48927 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
48930 bRc = osCloseHandle(p->aRegion[i].hMap);
48931 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
48935 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
48937 winClose((sqlite3_file *)&p->hFile);
48943 winDelete(pVfs, p->zFilename, 0);
48947 *pp = p->pNext;
48948 sqlite3_free(p->aRegion);
48951 pp = &p->pNext;
48963 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
48969 if( pShmNode->isReadonly ){
48970 pShmNode->isUnlocked = 1;
48973 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
48976 "winLockSharedMemory", pShmNode->zFilename);
48988 ** Open the shared-memory area associated with database file pDbFd.
48990 ** When opening a new shared-memory file, if no other instances of that
49001 assert( pDbFd->pShm==0 ); /* Not previously opened */
49008 nName = sqlite3Strlen30(pDbFd->zPath);
49014 pNew->zFilename = (char*)&pNew[1];
49015 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
49016 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
49022 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
49026 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
49036 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
49037 pShmNode->pNext = winShmNodeList;
49041 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
49042 if( pShmNode->mutex==0 ){
49048 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
49053 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
49054 (sqlite3_file*)&pShmNode->hFile,
49058 pShmNode->zFilename);
49061 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
49068 p->pShmNode = pShmNode;
49070 p->id = pShmNode->nextShmId++;
49072 pShmNode->nRef++;
49073 pDbFd->pShm = p;
49080 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
49083 sqlite3_mutex_enter(pShmNode->mutex);
49084 p->pNext = pShmNode->pFirst;
49085 pShmNode->pFirst = p;
49086 sqlite3_mutex_leave(pShmNode->mutex);
49092 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
49100 ** Close a connection to shared-memory. Delete the underlying
49107 winFile *pDbFd; /* Database holding shared-memory */
49109 winShmNode *pShmNode; /* The underlying shared-memory file */
49113 p = pDbFd->pShm;
49115 pShmNode = p->pShmNode;
49119 sqlite3_mutex_enter(pShmNode->mutex);
49120 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
49121 *pp = p->pNext;
49125 pDbFd->pShm = 0;
49126 sqlite3_mutex_leave(pShmNode->mutex);
49128 /* If pShmNode->nRef has reached 0, then close the underlying
49129 ** shared-memory file, too */
49131 assert( pShmNode->nRef>0 );
49132 pShmNode->nRef--;
49133 if( pShmNode->nRef==0 ){
49134 winShmPurge(pDbFd->pVfs, deleteFlag);
49142 ** Change the lock state for a shared-memory segment.
49151 winShm *p = pDbFd->pShm; /* The shared memory being locked */
49158 pShmNode = p->pShmNode;
49169 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
49171 sqlite3_mutex_enter(pShmNode->mutex);
49176 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49178 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
49179 allMask |= pX->sharedMask;
49182 /* Unlock the system-level locks */
49191 p->exclMask &= ~mask;
49192 p->sharedMask &= ~mask;
49201 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49202 if( (pX->exclMask & mask)!=0 ){
49206 allShared |= pX->sharedMask;
49220 p->sharedMask |= mask;
49226 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
49227 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
49239 assert( (p->sharedMask & mask)==0 );
49240 p->exclMask |= mask;
49244 sqlite3_mutex_leave(pShmNode->mutex);
49245 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
49246 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
49261 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
49268 ** shared-memory associated with the database file fd. Shared-memory regions
49269 ** are numbered starting from zero. Each shared-memory region is szRegion
49274 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
49277 ** isWrite is non-zero and the requested shared-memory region has not yet
49280 ** If the shared-memory region has already been allocated or is allocated by
49293 winShm *pShm = pDbFd->pShm;
49302 pShm = pDbFd->pShm;
49305 pShmNode = pShm->pShmNode;
49307 sqlite3_mutex_enter(pShmNode->mutex);
49308 if( pShmNode->isUnlocked ){
49311 pShmNode->isUnlocked = 0;
49313 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
49315 if( pShmNode->nRegion<=iRegion ){
49318 sqlite3_int64 sz; /* Current size of wal-index file */
49320 pShmNode->szRegion = szRegion;
49323 ** Check to see if it has been allocated (i.e. if the wal-index file is
49326 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
49329 "winShmMap1", pDbFd->zPath);
49337 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
49341 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
49344 "winShmMap2", pDbFd->zPath);
49351 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
49357 pShmNode->aRegion = apNew;
49359 if( pShmNode->isReadonly ){
49364 while( pShmNode->nRegion<=iRegion ){
49365 HANDLE hMap = NULL; /* file-mapping handle */
49369 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
49373 hMap = osCreateFileMappingW(pShmNode->hFile.h,
49377 hMap = osCreateFileMappingA(pShmNode->hFile.h,
49381 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
49382 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
49385 int iOffset = pShmNode->nRegion*szRegion;
49389 iOffset - iOffsetShift, szRegion + iOffsetShift
49393 0, iOffset - iOffsetShift, szRegion + iOffsetShift
49396 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
49397 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
49401 pShmNode->lastErrno = osGetLastError();
49402 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
49403 "winShmMap3", pDbFd->zPath);
49408 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
49409 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
49410 pShmNode->nRegion++;
49415 if( pShmNode->nRegion>iRegion ){
49418 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
49423 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
49424 sqlite3_mutex_leave(pShmNode->mutex);
49441 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
49443 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
49444 pFile->mmapSize, pFile->mmapSizeMax));
49445 if( pFile->pMapRegion ){
49446 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
49447 pFile->lastErrno = osGetLastError();
49448 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
49450 pFile->pMapRegion));
49451 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49452 "winUnmapfile1", pFile->zPath);
49454 pFile->pMapRegion = 0;
49455 pFile->mmapSize = 0;
49457 if( pFile->hMap!=NULL ){
49458 if( !osCloseHandle(pFile->hMap) ){
49459 pFile->lastErrno = osGetLastError();
49460 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
49461 osGetCurrentProcessId(), pFile, pFile->hMap));
49462 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
49463 "winUnmapfile2", pFile->zPath);
49465 pFile->hMap = NULL;
49467 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49473 ** Memory map or remap the file opened by file-descriptor pFd (if the file
49476 ** outstanding xFetch() references to it, this function is a no-op.
49478 ** If parameter nByte is non-negative, then it is the requested size of
49492 assert( nMap>=0 || pFd->nFetchOut==0 );
49493 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
49496 if( pFd->nFetchOut>0 ) return SQLITE_OK;
49501 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
49506 if( nMap>pFd->mmapSizeMax ){
49507 nMap = pFd->mmapSizeMax;
49509 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
49511 if( nMap==0 && pFd->mmapSize>0 ){
49514 if( nMap!=pFd->mmapSize ){
49521 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
49527 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
49529 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
49533 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
49537 if( pFd->hMap==NULL ){
49538 pFd->lastErrno = osGetLastError();
49539 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49540 "winMapfile1", pFd->zPath);
49542 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
49549 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
49551 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
49554 osCloseHandle(pFd->hMap);
49555 pFd->hMap = NULL;
49556 pFd->lastErrno = osGetLastError();
49557 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
49558 "winMapfile2", pFd->zPath);
49560 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
49564 pFd->pMapRegion = pNew;
49565 pFd->mmapSize = nMap;
49568 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
49596 if( pFd->mmapSizeMax>0 ){
49597 if( pFd->pMapRegion==0 ){
49598 int rc = winMapfile(pFd, -1);
49605 if( pFd->mmapSize >= iOff+nAmt ){
49606 assert( pFd->pMapRegion!=0 );
49607 *pp = &((u8 *)pFd->pMapRegion)[iOff];
49608 pFd->nFetchOut++;
49619 ** If the third argument is non-NULL, then this function releases a
49635 assert( (p==0)==(pFd->nFetchOut==0) );
49638 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
49644 pFd->nFetchOut--;
49648 ** is unnecessary can be omitted - potentially improving
49653 assert( pFd->nFetchOut>=0 );
49741 ** supports for filenames into UTF-8. Space to hold the result is
49760 ** Convert a UTF-8 filename into whatever form the underlying
49780 ** This function returns non-zero if the specified UTF-8 string buffer
49788 if( winIsDirSep(zBuf[nLen-1]) ){
49827 /* It's odd to simulate an io-error here, but really this is just
49828 ** using the io-error infrastructure to test that SQLite handles this
49836 nMax = pVfs->mxPathname; nBuf = nMax + 2;
49839 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49847 nDir = nMax - (nPre + 15);
49852 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
49858 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
49900 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49913 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49921 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
49928 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
49934 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49952 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49958 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
49970 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49980 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
49985 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
49995 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
50009 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50027 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
50031 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
50036 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
50042 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
50088 const char *zName, /* Name of the file (UTF-8) */
50105 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
50147 /* The main DB, main journal, WAL file and super-journal are never
50154 /* Assert that the upper layer has set one of the "file-type" flags. */
50163 pFile->h = INVALID_HANDLE_VALUE;
50185 /* Database filenames are double-zero terminated if they are not
50331 pFile->lastErrno = lastErrno;
50332 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
50349 pAppData = (winVfsAppData*)pVfs->pAppData;
50354 && ((pAppData==NULL) || !pAppData->bNoLock)
50360 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
50365 pFile->zDeleteOnClose = zConverted;
50373 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
50374 pFile->pVfs = pVfs;
50375 pFile->h = h;
50377 pFile->ctrlFlags |= WINFILE_RDONLY;
50382 pFile->ctrlFlags |= WINFILE_PSOW;
50384 pFile->lastErrno = NO_ERROR;
50385 pFile->zPath = zName;
50387 pFile->hMap = NULL;
50388 pFile->pMapRegion = 0;
50389 pFile->mmapSize = 0;
50390 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
50546 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
50592 ** Returns non-zero if the specified path name starts with the "long path"
50603 ** Returns non-zero if the specified path name starts with a drive letter
50613 ** Returns non-zero if the specified path name should be used verbatim. If
50614 ** non-zero is returned from this function, the calling function must simply
50615 ** use the provided path name verbatim -OR- resolve it into a full path name
50651 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
50677 assert( nFull>=pVfs->mxPathname );
50685 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50691 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
50701 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50707 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
50713 zRelative, zOut, pVfs->mxPathname+1)<0 ){
50723 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
50742 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50745 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
50751 /* It's odd to simulate an io-error here, but really this is just
50752 ** using the io-error infrastructure to test that SQLite handles this
50764 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
50825 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
50856 int nFull = pVfs->mxPathname+1;
50931 for(j=0, k=p->i; j<sz; j++){
50932 p->a[k++] ^= x[j];
50933 if( k>=p->na ) k = 0;
50935 p->i = k;
50936 p->nXor += sz;
51007 ** The following variable, if set to a non-zero value, is interpreted as
51026 /* FILETIME structure is a 64-bit value representing the number of
51027 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
51034 /* 2^32 - to avoid use of LL and warnings in gcc */
51084 ** buffer with a nul-terminated UTF-8 encoded error message
51090 ** is zero if the error message fits in the buffer, or non-zero
51091 ** otherwise (if the message was truncated). If non-zero is returned,
51092 ** then it is not necessary to include the nul-terminator character
51149 "win32-longpath", /* zName */
51174 "win32-none", /* zName */
51199 "win32-longpath-none", /* zName */
51220 /* Double-check that the aSyscall[] array has been constructed
51273 ** 2016-09-07
51284 ** This file implements an in-memory VFS. A database is held as a contiguous
51300 /* Access to a lower-level VFS that (might) implement dynamic loading,
51303 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
51363 ** File-scope variables for holding the memdb files that are accessible
51467 sqlite3_mutex_enter(p->pMutex);
51470 sqlite3_mutex_leave(p->pMutex);
51477 ** Close an memdb-file.
51482 MemStore *p = ((MemFile*)pFile)->pStore;
51483 if( p->zFName ){
51492 if( p->nRef==1 ){
51493 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
51506 p->nRef--;
51507 if( p->nRef<=0 ){
51508 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
51509 sqlite3_free(p->aData);
51512 sqlite3_mutex_free(p->pMutex);
51521 ** Read data from an memdb-file.
51529 MemStore *p = ((MemFile*)pFile)->pStore;
51531 if( iOfst+iAmt>p->sz ){
51533 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
51537 memcpy(zBuf, p->aData+iOfst, iAmt);
51547 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
51550 if( newSz>p->szMax ){
51554 if( newSz>p->szMax ) newSz = p->szMax;
51555 pNew = sqlite3Realloc(p->aData, newSz);
51557 p->aData = pNew;
51558 p->szAlloc = newSz;
51563 ** Write data to an memdb-file.
51571 MemStore *p = ((MemFile*)pFile)->pStore;
51573 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51579 if( iOfst+iAmt>p->sz ){
51581 if( iOfst+iAmt>p->szAlloc
51587 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
51588 p->sz = iOfst+iAmt;
51590 memcpy(p->aData+iOfst, z, iAmt);
51596 ** Truncate an memdb-file.
51603 MemStore *p = ((MemFile*)pFile)->pStore;
51606 if( size>p->sz ){
51610 p->sz = size;
51617 ** Sync an memdb-file.
51626 ** Return the current file-size of an memdb-file.
51629 MemStore *p = ((MemFile*)pFile)->pStore;
51631 *pSize = p->sz;
51637 ** Lock an memdb-file.
51641 MemStore *p = pThis->pStore;
51643 if( eLock<=pThis->eLock ) return SQLITE_OK;
51646 assert( p->nWrLock==0 || p->nWrLock==1 );
51647 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
51648 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
51650 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
51655 assert( pThis->eLock==SQLITE_LOCK_NONE );
51656 if( p->nWrLock>0 ){
51659 p->nRdLock++;
51666 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51667 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
51668 if( p->nWrLock>0 ){
51671 p->nWrLock = 1;
51679 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
51680 if( p->nRdLock>1 ){
51682 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
51683 p->nWrLock = 1;
51689 if( rc==SQLITE_OK ) pThis->eLock = eLock;
51695 ** Unlock an memdb-file.
51699 MemStore *p = pThis->pStore;
51700 if( eLock>=pThis->eLock ) return SQLITE_OK;
51705 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
51706 p->nWrLock--;
51709 if( pThis->eLock>SQLITE_LOCK_SHARED ){
51710 p->nWrLock--;
51712 p->nRdLock--;
51715 pThis->eLock = eLock;
51723 ** occur on an in-memory database.
51733 ** File control method. For custom operations on an memdb-file.
51736 MemStore *p = ((MemFile*)pFile)->pStore;
51740 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
51745 if( iLimit<p->sz ){
51747 iLimit = p->szMax;
51749 iLimit = p->sz;
51752 p->szMax = iLimit;
51762 ** Return the sector-size in bytes for an memdb-file.
51770 ** Return the device characteristic flags supported by an memdb-file.
51780 /* Fetch a page of a memory-mapped file */
51787 MemStore *p = ((MemFile*)pFile)->pStore;
51789 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
51792 p->nMmap++;
51793 *pp = (void*)(p->aData + iOfst);
51799 /* Release a memory-mapped page */
51801 MemStore *p = ((MemFile*)pFile)->pStore;
51805 p->nMmap--;
51834 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
51856 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
51857 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
51858 p->zFName = (char*)&p[1];
51859 memcpy(p->zFName, zName, szName+1);
51860 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
51861 if( p->pMutex==0 ){
51862 memdb_g.nMemStore--;
51867 p->nRef = 1;
51871 p->nRef++;
51880 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
51881 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
51883 pFile->pStore = p;
51887 pFd->pMethods = &memdb_io_methods;
51892 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
51896 ** ensure the file-system modifications are synced to disk before
51943 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
51948 ** utf-8 string describing the most recent error encountered associated
51952 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
51959 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
51966 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
51974 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
51982 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
51990 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
51995 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
51998 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
52010 if( p->base.pMethods!=&memdb_io_methods ) return 0;
52011 pStore = p->pStore;
52013 if( pStore->zFName!=0 ) p = 0;
52044 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52047 if( piSize ) *piSize = -1;
52050 MemStore *pStore = p->pStore;
52051 assert( pStore->pMutex==0 );
52052 if( piSize ) *piSize = pStore->sz;
52054 pOut = pStore->aData;
52056 pOut = sqlite3_malloc64( pStore->sz );
52057 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
52061 pBt = db->aDb[iDb].pBt;
52065 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
52084 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
52124 sqlite3_mutex_enter(db->mutex);
52125 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
52136 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
52140 db->init.iDb = (u8)iDb;
52141 db->init.reopenMemdb = 1;
52143 db->init.reopenMemdb = 0;
52152 MemStore *pStore = p->pStore;
52153 pStore->aData = pData;
52155 pStore->sz = szDb;
52156 pStore->szAlloc = szBuf;
52157 pStore->szMax = szBuf;
52158 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
52159 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
52161 pStore->mFlags = mFlags;
52170 sqlite3_mutex_leave(db->mutex);
52182 sz = pLower->szOsFile;
52207 ** This file implements an object that represents a fixed-length
52211 ** journalled during a transaction, or which pages have the "dont-write"
52240 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
52257 ** sub-dividing and re-hashing. */
52262 ** no fewer collisions than the no-op *1. */
52282 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
52291 u32 nSet; /* Number of bits that are set - only valid for aHash
52315 p->iSize = iSize;
52321 ** Check to see if the i-th bit is set. Return true or false.
52327 i--;
52328 if( i>=p->iSize ) return 0;
52329 while( p->iDivisor ){
52330 u32 bin = i/p->iDivisor;
52331 i = i%p->iDivisor;
52332 p = p->u.apSub[bin];
52337 if( p->iSize<=BITVEC_NBIT ){
52338 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
52341 while( p->u.aHash[h] ){
52342 if( p->u.aHash[h]==i ) return 1;
52353 ** Set the i-th bit. Return 0 on success and an error code if
52356 ** This routine might cause sub-bitmaps to be allocated. Failing
52357 ** to get the memory needed to hold the sub-bitmap is the only
52368 assert( i<=p->iSize );
52369 i--;
52370 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
52371 u32 bin = i/p->iDivisor;
52372 i = i%p->iDivisor;
52373 if( p->u.apSub[bin]==0 ){
52374 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
52375 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
52377 p = p->u.apSub[bin];
52379 if( p->iSize<=BITVEC_NBIT ){
52380 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
52386 /* worring about sub-dividing and re-hashing. */
52387 if( !p->u.aHash[h] ){
52388 if (p->nSet<(BITVEC_NINT-1)) {
52397 if( p->u.aHash[h]==i ) return SQLITE_OK;
52400 } while( p->u.aHash[h] );
52405 if( p->nSet>=BITVEC_MXHASH ){
52408 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
52412 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52413 memset(p->u.apSub, 0, sizeof(p->u.apSub));
52414 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
52424 p->nSet++;
52425 p->u.aHash[h] = i;
52430 ** Clear the i-th bit.
52438 i--;
52439 while( p->iDivisor ){
52440 u32 bin = i/p->iDivisor;
52441 i = i%p->iDivisor;
52442 p = p->u.apSub[bin];
52447 if( p->iSize<=BITVEC_NBIT ){
52448 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
52452 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
52453 memset(p->u.aHash, 0, sizeof(p->u.aHash));
52454 p->nSet = 0;
52457 u32 h = BITVEC_HASH(aiValues[j]-1);
52458 p->nSet++;
52459 while( p->u.aHash[h] ){
52463 p->u.aHash[h] = aiValues[j];
52474 if( p->iDivisor ){
52477 sqlite3BitvecDestroy(p->u.apSub[i]);
52488 return p->iSize;
52530 ** If a memory allocation error occurs, return -1.
52535 int rc = -1;
52558 i = aOp[pc+2] - 1;
52570 if( (--aOp[pc+1]) > 0 ) nx = 0;
52586 ** match (rc==0). Change rc to non-zero if a discrepancy
52591 + (sqlite3BitvecSize(pBitvec) - sz);
52637 ** such that p was added to the list more recently than p->pDirtyNext.
52642 ** page to eject from the cache mid-transaction. It is better to eject
52682 pPg = (PgHdr*)pLower->pExtra;
52683 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
52684 a = (unsigned char *)pLower->pBuf;
52694 if( pCache->pCache==0 ) return;
52698 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
52701 if( ((PgHdr*)pLower)->pPage==0 ){
52702 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
52719 for(p=pCache->pDirty; p; p=p->pDirtyNext){
52739 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
52740 pCache = pPg->pCache;
52742 if( pPg->flags & PGHDR_CLEAN ){
52743 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
52746 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
52747 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
52748 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
52749 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
52753 if( pPg->flags & PGHDR_WRITEABLE ){
52754 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
52792 PCache *p = pPage->pCache;
52796 pPage->pgno));
52798 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
52799 assert( pPage->pDirtyPrev || pPage==p->pDirty );
52802 if( p->pSynced==pPage ){
52803 p->pSynced = pPage->pDirtyPrev;
52806 if( pPage->pDirtyNext ){
52807 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
52809 assert( pPage==p->pDirtyTail );
52810 p->pDirtyTail = pPage->pDirtyPrev;
52812 if( pPage->pDirtyPrev ){
52813 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
52819 assert( pPage==p->pDirty );
52820 p->pDirty = pPage->pDirtyNext;
52821 assert( p->bPurgeable || p->eCreate==2 );
52822 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
52823 assert( p->bPurgeable==0 || p->eCreate==1 );
52824 p->eCreate = 2;
52829 pPage->pDirtyPrev = 0;
52830 pPage->pDirtyNext = p->pDirty;
52831 if( pPage->pDirtyNext ){
52832 assert( pPage->pDirtyNext->pDirtyPrev==0 );
52833 pPage->pDirtyNext->pDirtyPrev = pPage;
52835 p->pDirtyTail = pPage;
52836 if( p->bPurgeable ){
52837 assert( p->eCreate==2 );
52838 p->eCreate = 1;
52841 p->pDirty = pPage;
52847 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
52848 if( !p->pSynced
52849 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
52851 p->pSynced = pPage;
52859 ** being used for an in-memory database, this function is a no-op.
52862 if( p->pCache->bPurgeable ){
52863 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
52864 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
52865 pcacheDump(p->pCache);
52870 ** Compute the number of pages of cache requested. p->szCache is the
52874 if( p->szCache>=0 ){
52875 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
52877 return p->szCache;
52880 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
52884 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
52897 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
52898 ** built-in default page cache is used instead of the application defined
52907 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
52938 p->szPage = 1;
52939 p->szExtra = szExtra;
52941 p->bPurgeable = bPurgeable;
52942 p->eCreate = 2;
52943 p->xStress = xStress;
52944 p->pStress = pStress;
52945 p->szCache = 100;
52946 p->szSpill = 1;
52956 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
52957 if( pCache->szPage ){
52960 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
52961 pCache->bPurgeable
52965 if( pCache->pCache ){
52966 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
52968 pCache->pCache = pNew;
52969 pCache->szPage = szPage;
53008 assert( pCache->pCache!=0 );
53010 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
53019 eCreate = createFlag & pCache->eCreate;
53021 assert( createFlag==0 || pCache->eCreate==eCreate );
53022 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
53023 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
53047 if( pCache->eCreate==2 ) return 0;
53049 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
53050 /* Find a dirty page to write-out and recycle. First try to find a
53051 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
53059 for(pPg=pCache->pSynced;
53060 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
53061 pPg=pPg->pDirtyPrev
53063 pCache->pSynced = pPg;
53065 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
53071 "spill page %d making room for %d - cache used: %d/%d",
53072 pPg->pgno, pgno,
53073 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
53076 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
53077 rc = pCache->xStress(pCache->pStress, pPg);
53084 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
53104 pPgHdr = (PgHdr*)pPage->pExtra;
53105 assert( pPgHdr->pPage==0 );
53106 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
53107 pPgHdr->pPage = pPage;
53108 pPgHdr->pData = pPage->pBuf;
53109 pPgHdr->pExtra = (void *)&pPgHdr[1];
53110 memset(pPgHdr->pExtra, 0, 8);
53111 pPgHdr->pCache = pCache;
53112 pPgHdr->pgno = pgno;
53113 pPgHdr->flags = PGHDR_CLEAN;
53131 pPgHdr = (PgHdr *)pPage->pExtra;
53133 if( !pPgHdr->pPage ){
53136 pCache->nRefSum++;
53137 pPgHdr->nRef++;
53147 assert( p->nRef>0 );
53148 p->pCache->nRefSum--;
53149 if( (--p->nRef)==0 ){
53150 if( p->flags&PGHDR_CLEAN ){
53163 assert(p->nRef>0);
53165 p->nRef++;
53166 p->pCache->nRefSum++;
53175 assert( p->nRef==1 );
53177 if( p->flags&PGHDR_DIRTY ){
53180 p->pCache->nRefSum--;
53181 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
53189 assert( p->nRef>0 );
53191 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
53192 p->flags &= ~PGHDR_DONT_WRITE;
53193 if( p->flags & PGHDR_CLEAN ){
53194 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
53195 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
53196 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
53210 assert( (p->flags & PGHDR_DIRTY)!=0 );
53211 assert( (p->flags & PGHDR_CLEAN)==0 );
53213 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53214 p->flags |= PGHDR_CLEAN;
53215 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
53217 if( p->nRef==0 ){
53227 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
53228 while( (p = pCache->pDirty)!=0 ){
53238 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
53239 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53240 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
53242 pCache->pSynced = pCache->pDirtyTail;
53250 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53251 p->flags &= ~PGHDR_NEED_SYNC;
53253 pCache->pSynced = pCache->pDirtyTail;
53260 PCache *pCache = p->pCache;
53262 assert( p->nRef>0 );
53265 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
53266 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
53268 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
53269 assert( pXPage->nRef==0 );
53270 pXPage->nRef++;
53271 pCache->nRefSum++;
53274 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
53275 p->pgno = newPgno;
53276 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
53292 if( pCache->pCache ){
53296 for(p=pCache->pDirty; p; p=pNext){
53297 pNext = p->pDirtyNext;
53302 assert( p->pgno>0 );
53303 if( p->pgno>pgno ){
53304 assert( p->flags&PGHDR_DIRTY );
53308 if( pgno==0 && pCache->nRefSum ){
53310 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
53312 ** pCache->nRefSum>0 */
53313 memset(pPage1->pBuf, 0, pCache->szPage);
53317 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
53325 assert( pCache->pCache!=0 );
53327 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
53346 if( pA->pgno<pB->pgno ){
53347 pTail->pDirty = pA;
53349 pA = pA->pDirty;
53351 pTail->pDirty = pB;
53355 pTail->pDirty = pB;
53357 pB = pB->pDirty;
53359 pTail->pDirty = pA;
53384 pIn = p->pDirty;
53385 p->pDirty = 0;
53386 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
53395 if( NEVER(i==N_SORT_BUCKET-1) ){
53415 for(p=pCache->pDirty; p; p=p->pDirtyNext){
53416 p->pDirty = p->pDirtyNext;
53418 return pcacheSortDirtyList(pCache->pDirty);
53428 return pCache->nRefSum;
53435 return p->nRef;
53442 assert( pCache->pCache!=0 );
53443 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
53448 ** Get the suggested cache-size value.
53456 ** Set the suggested cache-size value.
53459 assert( pCache->pCache!=0 );
53460 pCache->szCache = mxPage;
53461 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
53466 ** Set the suggested cache-spill value. Make no changes if if the
53467 ** argument is zero. Return the effective cache-spill size, which will
53472 assert( p->pCache!=0 );
53475 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
53477 p->szSpill = mxPage;
53480 if( res<p->szSpill ) res = p->szSpill;
53488 assert( pCache->pCache!=0 );
53489 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
53494 ** in the page-cache hierarchy.
53506 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
53515 return (pCache->pDirty!=0);
53527 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
53555 ** -------------------------------------------------------------
53557 ** -------------------------------------------------------------
53573 ** size can vary according to architecture, compile-time options, and
53579 ** allocations. However, the btree layer needs a small (16-byte) overrun
53594 ** (1) The general-purpose memory allocator - sqlite3Malloc()
53595 ** (2) Global page-cache memory provided using sqlite3_config() with
53597 ** (3) PCache-local bulk allocation.
53607 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
53611 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
53627 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
53637 ** but causes a 2-byte gap in the structure for most architectures (since
53638 ** pointers must be either 4 or 8-byte aligned). As this structure is located
53640 ** corrupt, code at the b-tree layer may overread the page buffer and
53646 ** The pLruNext and pLruPrev pointers form a double-linked circular list
53667 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
53668 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
53696 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
53702 ** open database file (including each in-memory database and each
53717 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
53735 PgHdr1 *pFree; /* List of unused pcache-local pages */
53736 void *pBulk; /* Bulk memory used by pcache-local */
53770 ** reading because (1) most platforms read a 32-bit integer atomically and
53787 # define pcache1EnterMutex(X) assert((X)->mutex==0)
53788 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
53791 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
53792 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
53802 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
53821 while( n-- ){
53823 p->pNext = pcache1.pFree;
53832 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
53833 ** true if pCache->pFree ends up containing one or more free pages.
53840 if( pCache->nMax<3 ) return 0;
53843 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
53845 szBulk = -1024 * (i64)pcache1.nInitPage;
53847 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
53848 szBulk = pCache->szAlloc*(i64)pCache->nMax;
53850 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
53853 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
53855 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
53856 pX->page.pBuf = zBulk;
53857 pX->page.pExtra = &pX[1];
53858 pX->isBulkLocal = 1;
53859 pX->isAnchor = 0;
53860 pX->pNext = pCache->pFree;
53861 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
53862 pCache->pFree = pX;
53863 zBulk += pCache->szAlloc;
53864 }while( --nBulk );
53866 return pCache->pFree!=0;
53885 pcache1.pFree = pcache1.pFree->pNext;
53886 pcache1.nFreeSlot--;
53923 pSlot->pNext = pcache1.pFree;
53970 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
53971 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
53972 assert( pCache->pFree!=0 );
53973 p = pCache->pFree;
53974 pCache->pFree = p->pNext;
53975 p->pNext = 0;
53982 assert( pCache->pGroup==&pcache1.grp );
53983 pcache1LeaveMutex(pCache->pGroup);
53986 pPg = pcache1Alloc(pCache->szAlloc);
53989 pcache1EnterMutex(pCache->pGroup);
53992 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
53993 p->page.pBuf = pPg;
53994 p->page.pExtra = &p[1];
53995 p->isBulkLocal = 0;
53996 p->isAnchor = 0;
53997 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
53999 (*pCache->pnPurgeable)++;
54009 pCache = p->pCache;
54010 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
54011 if( p->isBulkLocal ){
54012 p->pNext = pCache->pFree;
54013 pCache->pFree = p;
54015 pcache1Free(p->page.pBuf);
54017 (*pCache->pnPurgeable)--;
54055 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
54076 assert( sqlite3_mutex_held(p->pGroup->mutex) );
54078 nNew = p->nHash*2;
54083 pcache1LeaveMutex(p->pGroup);
54084 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
54086 if( p->nHash ){ sqlite3EndBenignMalloc(); }
54087 pcache1EnterMutex(p->pGroup);
54089 for(i=0; i<p->nHash; i++){
54091 PgHdr1 *pNext = p->apHash[i];
54093 unsigned int h = pPage->iKey % nNew;
54094 pNext = pPage->pNext;
54095 pPage->pNext = apNew[h];
54099 sqlite3_free(p->apHash);
54100 p->apHash = apNew;
54101 p->nHash = nNew;
54108 ** LRU list, then this function is a no-op.
54115 assert( pPage->pLruNext );
54116 assert( pPage->pLruPrev );
54117 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
54118 pPage->pLruPrev->pLruNext = pPage->pLruNext;
54119 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
54120 pPage->pLruNext = 0;
54121 /* pPage->pLruPrev = 0;
54123 assert( pPage->isAnchor==0 );
54124 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
54125 pPage->pCache->nRecyclable--;
54139 PCache1 *pCache = pPage->pCache;
54142 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54143 h = pPage->iKey % pCache->nHash;
54144 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
54145 *pp = (*pp)->pNext;
54147 pCache->nPage--;
54156 PGroup *pGroup = pCache->pGroup;
54158 assert( sqlite3_mutex_held(pGroup->mutex) );
54159 while( pGroup->nPurgeable>pGroup->nMaxPage
54160 && (p=pGroup->lru.pLruPrev)->isAnchor==0
54162 assert( p->pCache->pGroup==pGroup );
54167 if( pCache->nPage==0 && pCache->pBulk ){
54168 sqlite3_free(pCache->pBulk);
54169 pCache->pBulk = pCache->pFree = 0;
54184 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
54186 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
54187 assert( pCache->iMaxKey >= iLimit );
54188 assert( pCache->nHash > 0 );
54189 if( pCache->iMaxKey - iLimit < pCache->nHash ){
54194 h = iLimit % pCache->nHash;
54195 iStop = pCache->iMaxKey % pCache->nHash;
54196 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
54200 h = pCache->nHash/2;
54201 iStop = h - 1;
54206 assert( h<pCache->nHash );
54207 pp = &pCache->apHash[h];
54209 if( pPage->iKey>=iLimit ){
54210 pCache->nPage--;
54211 *pp = pPage->pNext;
54215 pp = &pPage->pNext;
54220 h = (h+1) % pCache->nHash;
54222 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
54239 ** private PGroup (mode-1). pcache1.separateCache is false if the single
54240 ** PGroup in pcache1.grp is used for all page caches (mode-2).
54242 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
54244 ** * Use a unified cache in single-threaded applications that have
54245 ** configured a start-time buffer for use as page-cache memory using
54246 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
54249 ** * Otherwise use separate caches (mode-1)
54303 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
54311 pGroup->mxPinned = 10;
54316 if( pGroup->lru.isAnchor==0 ){
54317 pGroup->lru.isAnchor = 1;
54318 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
54320 pCache->pGroup = pGroup;
54321 pCache->szPage = szPage;
54322 pCache->szExtra = szExtra;
54323 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
54324 pCache->bPurgeable = (bPurgeable ? 1 : 0);
54327 pCache->nMin = 10;
54328 pGroup->nMinPage += pCache->nMin;
54329 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54330 pCache->pnPurgeable = &pGroup->nPurgeable;
54332 pCache->pnPurgeable = &pCache->nPurgeableDummy;
54335 if( pCache->nHash==0 ){
54352 if( pCache->bPurgeable ){
54353 PGroup *pGroup = pCache->pGroup;
54356 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
54357 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
54359 pGroup->nMaxPage += (n - pCache->nMax);
54360 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54361 pCache->nMax = n;
54362 pCache->n90pct = pCache->nMax*9/10;
54375 if( pCache->bPurgeable ){
54376 PGroup *pGroup = pCache->pGroup;
54379 savedMaxPage = pGroup->nMaxPage;
54380 pGroup->nMaxPage = 0;
54382 pGroup->nMaxPage = savedMaxPage;
54393 pcache1EnterMutex(pCache->pGroup);
54394 n = pCache->nPage;
54395 pcache1LeaveMutex(pCache->pGroup);
54414 PGroup *pGroup = pCache->pGroup;
54418 assert( pCache->nPage >= pCache->nRecyclable );
54419 nPinned = pCache->nPage - pCache->nRecyclable;
54420 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
54421 assert( pCache->n90pct == pCache->nMax*9/10 );
54423 nPinned>=pGroup->mxPinned
54424 || nPinned>=pCache->n90pct
54425 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
54430 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
54431 assert( pCache->nHash>0 && pCache->apHash );
54434 if( pCache->bPurgeable
54435 && !pGroup->lru.pLruPrev->isAnchor
54436 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
54439 pPage = pGroup->lru.pLruPrev;
54443 pOther = pPage->pCache;
54444 if( pOther->szAlloc != pCache->szAlloc ){
54448 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
54460 unsigned int h = iKey % pCache->nHash;
54461 pCache->nPage++;
54462 pPage->iKey = iKey;
54463 pPage->pNext = pCache->apHash[h];
54464 pPage->pCache = pCache;
54465 pPage->pLruNext = 0;
54466 /* pPage->pLruPrev = 0;
54468 *(void **)pPage->page.pExtra = 0;
54469 pCache->apHash[h] = pPage;
54470 if( iKey>pCache->iMaxKey ){
54471 pCache->iMaxKey = iKey;
54487 ** For a non-purgeable cache (a cache used as the storage for an in-memory
54490 ** a non-purgeable cache.
54533 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
54545 pPage = pCache->apHash[iKey % pCache->nHash];
54546 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
54574 pcache1EnterMutex(pCache->pGroup);
54576 assert( pPage==0 || pCache->iMaxKey>=iKey );
54577 pcache1LeaveMutex(pCache->pGroup);
54591 assert( pCache->bPurgeable || createFlag!=1 );
54592 assert( pCache->bPurgeable || pCache->nMin==0 );
54593 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
54594 assert( pCache->nMin==0 || pCache->bPurgeable );
54595 assert( pCache->nHash>0 );
54597 if( pCache->pGroup->mutex ){
54619 PGroup *pGroup = pCache->pGroup;
54621 assert( pPage->pCache==pCache );
54627 assert( pPage->pLruNext==0 );
54630 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
54634 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
54635 pPage->pLruPrev = &pGroup->lru;
54636 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
54638 pCache->nRecyclable++;
54641 pcache1LeaveMutex(pCache->pGroup);
54657 assert( pPage->iKey==iOld );
54658 assert( pPage->pCache==pCache );
54661 pcache1EnterMutex(pCache->pGroup);
54664 hOld = iOld%pCache->nHash;
54665 pp = &pCache->apHash[hOld];
54667 pp = &(*pp)->pNext;
54669 *pp = pPage->pNext;
54672 hNew = iNew%pCache->nHash;
54673 pPage->iKey = iNew;
54674 pPage->pNext = pCache->apHash[hNew];
54675 pCache->apHash[hNew] = pPage;
54676 if( iNew>pCache->iMaxKey ){
54677 pCache->iMaxKey = iNew;
54680 pcache1LeaveMutex(pCache->pGroup);
54692 pcache1EnterMutex(pCache->pGroup);
54693 if( iLimit<=pCache->iMaxKey ){
54695 pCache->iMaxKey = iLimit-1;
54697 pcache1LeaveMutex(pCache->pGroup);
54707 PGroup *pGroup = pCache->pGroup;
54708 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
54710 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
54711 assert( pGroup->nMaxPage >= pCache->nMax );
54712 pGroup->nMaxPage -= pCache->nMax;
54713 assert( pGroup->nMinPage >= pCache->nMin );
54714 pGroup->nMinPage -= pCache->nMin;
54715 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
54718 sqlite3_free(pCache->pBulk);
54719 sqlite3_free(pCache->apHash);
54779 && p->isAnchor==0
54781 nFree += pcache1MemSize(p->page.pBuf);
54805 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
54865 ** a non-zero batch number, it will see all prior INSERTs.
54893 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
54951 p->pChunk = 0;
54952 p->db = db;
54953 p->pEntry = 0;
54954 p->pLast = 0;
54955 p->pForest = 0;
54956 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
54957 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
54958 p->rsFlags = ROWSET_SORTED;
54959 p->iBatch = 0;
54972 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
54973 pNextChunk = pChunk->pNextChunk;
54974 sqlite3DbFree(p->db, pChunk);
54976 p->pChunk = 0;
54977 p->nFresh = 0;
54978 p->pEntry = 0;
54979 p->pLast = 0;
54980 p->pForest = 0;
54981 p->rsFlags = ROWSET_SORTED;
54991 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
54999 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
55004 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
55008 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
55012 pNew->pNextChunk = p->pChunk;
55013 p->pChunk = pNew;
55014 p->pFresh = pNew->aEntry;
55015 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
55017 p->nFresh--;
55018 return p->pFresh++;
55032 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
55036 pEntry->v = rowid;
55037 pEntry->pRight = 0;
55038 pLast = p->pLast;
55040 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
55043 p->rsFlags &= ~ROWSET_SORTED;
55045 pLast->pRight = pEntry;
55047 p->pEntry = pEntry;
55049 p->pLast = pEntry;
55068 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
55069 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
55070 if( pA->v<=pB->v ){
55071 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
55072 pA = pA->pRight;
55074 pTail->pRight = pB;
55078 pTail = pTail->pRight = pB;
55079 pB = pB->pRight;
55081 pTail->pRight = pA;
55099 pNext = pIn->pRight;
55100 pIn->pRight = 0;
55128 if( pIn->pLeft ){
55130 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
55131 p->pRight = pIn;
55135 if( pIn->pRight ){
55136 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
55140 assert( (*ppLast)->pRight==0 );
55163 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
55167 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
55171 pLeft = rowSetNDeepTree(ppList, iDepth-1);
55173 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
55178 p->pLeft = pLeft;
55179 *ppList = p->pRight;
55180 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
55183 *ppList = p->pRight;
55184 p->pLeft = p->pRight = 0;
55200 pList = p->pRight;
55201 p->pLeft = p->pRight = 0;
55205 pList = p->pRight;
55206 p->pLeft = pLeft;
55207 p->pRight = rowSetNDeepTree(&pList, iDepth);
55227 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
55230 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55231 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55232 p->pEntry = rowSetEntrySort(p->pEntry);
55234 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
55238 if( p->pEntry ){
55239 *pRowid = p->pEntry->v;
55240 p->pEntry = p->pEntry->pRight;
55241 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
55256 ** on pRowSet->pEntry, then sort those entries into the forest at
55257 ** pRowSet->pForest so that they can be tested.
55263 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
55268 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
55269 p = pRowSet->pEntry;
55271 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
55272 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
55276 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55277 ppPrevTree = &pTree->pRight;
55278 if( pTree->pLeft==0 ){
55279 pTree->pLeft = rowSetListToTree(p);
55283 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
55284 pTree->pLeft = 0;
55291 pTree->v = 0;
55292 pTree->pRight = 0;
55293 pTree->pLeft = rowSetListToTree(p);
55296 pRowSet->pEntry = 0;
55297 pRowSet->pLast = 0;
55298 pRowSet->rsFlags |= ROWSET_SORTED;
55300 pRowSet->iBatch = iBatch;
55306 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
55307 p = pTree->pLeft;
55309 if( p->v<iRowid ){
55310 p = p->pRight;
55311 }else if( p->v>iRowid ){
55312 p = p->pLeft;
55358 ** This header file defines the interface to the write-ahead logging
55398 /* Connection to a write-ahead log (WAL) file.
55403 /* Open and close a connection to a write-ahead log. */
55411 ** snapshot is like a read-transaction. It is the state of the database
55420 /* Read a page from the write-ahead log, if it is present. */
55447 Wal *pWal, /* Write-ahead log connection */
55471 /* Return true if the argument is non-NULL and the WAL module is using
55472 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
55473 ** WAL module is using shared-memory, return false.
55487 ** stored in each frame (i.e. the db page-size when the WAL was created).
55554 ** (6) If a super-journal file is used, then all writes to the database file
55555 ** are synced prior to the super-journal being deleted.
55571 ** method is a no-op, but that does not change the fact the SQLite will
55582 ** (11) A database file is well-formed at the beginning and at the conclusion
55606 ** to print out file-descriptors.
55609 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
55612 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
55620 ** OPEN <------+------+
55623 ** +---------> READER-------+ |
55626 ** |<-------WRITER_LOCKED------> ERROR
55629 ** |<------WRITER_CACHEMOD-------->|
55632 ** |<-------WRITER_DBMOD---------->|
55635 ** +<------WRITER_FINISHED-------->+
55640 ** OPEN -> READER [sqlite3PagerSharedLock]
55641 ** READER -> OPEN [pager_unlock]
55643 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
55644 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
55645 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
55646 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
55647 ** WRITER_*** -> READER [pager_end_transaction]
55649 ** WRITER_*** -> ERROR [pager_error]
55650 ** ERROR -> OPEN [pager_unlock]
55656 ** state - the file may or may not be locked and the database size is
55666 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
55667 ** was) in exclusive-locking mode, a user-level read transaction is
55671 ** it opens a read-transaction on the database and returns to state
55672 ** OPEN after the read-transaction is completed. However a connection
55674 ** this state even after the read-transaction is closed. The only way
55678 ** * A read transaction may be active (but a write-transaction cannot).
55680 ** * The dbSize variable may be trusted (even if a user-level read
55684 ** * Even if a read-transaction is not open, it is guaranteed that
55685 ** there is no hot-journal in the file-system.
55689 ** The pager moves to this state from READER when a write-transaction
55691 ** required to start a write-transaction are held, but no actual
55706 ** * If the connection is open in rollback-mode, a RESERVED or greater
55708 ** * If the connection is open in WAL-mode, a WAL write transaction
55747 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
55763 ** The ERROR state is entered when an IO or disk-full error (including
55765 ** difficult to be sure that the in-memory pager state (cache contents,
55766 ** db size etc.) are consistent with the contents of the file-system.
55768 ** Temporary pager files may enter the ERROR state, but in-memory pagers
55772 ** the contents of the page-cache may be left in an inconsistent state.
55784 ** page-cache and any other in-memory state at the same time. Everything
55785 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
55786 ** when a read-transaction is next opened on the pager (transitioning
55802 ** In other cases, the error is returned to the b-tree layer. The b-tree
55806 ** Condition (3) is necessary because it can be triggered by a read-only
55808 ** code were simply returned to the user, the b-tree layer would not
55810 ** read-only statement cannot leave the pager in an internally inconsistent
55816 ** * The pager is not an in-memory pager.
55826 ** state. There are two exceptions: immediately after exclusive-mode has
55842 ** following locking-states, according to the lock currently held on
55850 ** pagerUnlockDb() take a conservative approach - eLock is always updated
55861 ** from ERROR to OPEN state. At this point there may be a hot-journal file
55862 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
55866 ** of hot-journal detection.
55872 ** a hot-journal may be mistaken for a journal being created by an active
55879 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
55880 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
55881 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
55895 if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
55897 if( P->xCodec==0 ){ O=(char*)D; }else \
55898 if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
55900 # define CODEC1(P,D,N,X,E) /* NO-OP */
55920 ** set to 0. If a journal-header is written into the main journal while
55923 ** journal before the journal-header. This is required during savepoint
55932 Pgno iSubRec; /* Index of first record in sub-journal */
55957 ** For a real on-disk database, the current lock held on the database file -
55960 ** For a temporary or in-memory database (neither of which require any
55972 ** This boolean variable is used to make sure that the change-counter
55973 ** (the 4-byte header field at byte offset 24 of the database file) is
55976 ** It is set to true when the change-counter field is updated, which
55981 ** updating the change-counter is omitted for the current transaction.
55984 ** need only update the change-counter once, for the first transaction
55990 ** (or may not) specify a super-journal name to be written into the
55993 ** Whether or not a journal file contains a super-journal pointer affects
55996 ** If a journal file does not contain a super-journal pointer, it is
55998 ** it does contain a super-journal pointer the journal file is finalized
56002 ** Journal files that contain super-journal pointers cannot be finalized
56003 ** simply by overwriting the first journal-header with zeroes, as the
56004 ** super-journal pointer could interfere with hot-journal rollback of any
56014 ** This variables control the behavior of cache-spills (calls made by
56016 ** to the file-system in order to free up memory).
56028 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
56029 ** is larger than the database page-size in order to prevent a journal sync
56034 ** This is a boolean variable. If true, then any required sub-journal
56035 ** is opened as an in-memory journal file. If false, then in-memory
56036 ** sub-journals are only used for in-memory pager files.
56039 ** write-transaction is opened.
56050 ** is not an integer multiple of the page-size, the value stored in
56051 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
56053 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
56056 ** During a write-transaction, if pages with page-numbers greater than
56067 ** Throughout a write-transaction, dbFileSize contains the size of
56069 ** write-transaction is first opened, and updated when VFS calls are made
56089 ** write-transaction is opened (at the same time as dbFileSize and
56092 ** size-hint passed to the method call. See pager_write_pagelist() for
56100 ** sub-codes.
56126 u8 readOnly; /* True for a read-only database */
56128 u8 memVfs; /* VFS-implemented memory database */
56141 u8 changeCountDone; /* Set after incrementing the change-counter */
56142 u8 setSuper; /* Super-jrnl name is written into jrnl */
56143 u8 doNotSpill; /* Do not spill the cache when non-zero */
56144 u8 subjInMemory; /* True to use in-memory sub-journals */
56152 int nRec; /* Pages journalled since last j-header written */
56153 u32 cksumInit; /* Quasi-random value added to every checksum */
56154 u32 nSubRec; /* Number of records written to sub-journal */
56158 sqlite3_file *sjfd; /* File descriptor for sub-journal */
56171 ** End of the routinely-changing class members
56174 u16 nExtra; /* Add this many bytes to each in-memory page */
56201 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
56202 char *zWal; /* File name for write-ahead log */
56219 ** a non-testing build. These variables are not thread-safe.
56238 ** written, semi-random garbage data might appear in the journal
56245 ** of a 32-bit checksum on each page of data. The checksum covers both
56246 ** the page number and the pPager->pageSize bytes of data for the page.
56247 ** This cksum is initialized to a 32-bit random value that appears in the
56263 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
56269 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
56272 ** The macro MEMDB is true if we are dealing with an in-memory database.
56280 # define MEMDB pPager->memDb
56285 ** interfaces to access the database using memory-mapped I/O.
56288 # define USEFETCH(x) ((x)->bUseFetch)
56295 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
56299 ** if( isOpen(pPager->jfd) ){ ...
56303 ** if( pPager->jfd->pMethods ){ ...
56305 #define isOpen(pFd) ((pFd)->pMethods!=0)
56310 ** by the b-tree layer. This is the case if:
56317 if( pPager->fd->pMethods==0 ) return 0;
56318 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
56320 if( pPager->xCodec!=0 ) return 0;
56323 if( pPager->pWal ){
56326 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
56335 # define pagerUseWal(x) ((x)->pWal!=0)
56357 assert( p->eState==PAGER_OPEN
56358 || p->eState==PAGER_READER
56359 || p->eState==PAGER_WRITER_LOCKED
56360 || p->eState==PAGER_WRITER_CACHEMOD
56361 || p->eState==PAGER_WRITER_DBMOD
56362 || p->eState==PAGER_WRITER_FINISHED
56363 || p->eState==PAGER_ERROR
56366 /* Regardless of the current state, a temp-file connection always behaves
56368 ** the change-counter field, so the changeCountDone flag is always set.
56370 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
56371 assert( p->tempFile==0 || pPager->changeCountDone );
56373 /* If the useJournal flag is clear, the journal-mode must be "OFF".
56374 ** And if the journal-mode is "OFF", the journal file must not be open.
56376 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
56377 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
56379 /* Check that MEMDB implies noSync. And an in-memory journal. Since
56380 ** this means an in-memory pager performs no IO at all, it cannot encounter
56382 ** a journal file. (although the in-memory journal implementation may
56384 ** is therefore not possible for an in-memory pager to enter the ERROR
56388 assert( !isOpen(p->fd) );
56389 assert( p->noSync );
56390 assert( p->journalMode==PAGER_JOURNALMODE_OFF
56391 || p->journalMode==PAGER_JOURNALMODE_MEMORY
56393 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
56400 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
56401 assert( p->eLock!=PENDING_LOCK );
56403 switch( p->eState ){
56406 assert( pPager->errCode==SQLITE_OK );
56407 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
56411 assert( pPager->errCode==SQLITE_OK );
56412 assert( p->eLock!=UNKNOWN_LOCK );
56413 assert( p->eLock>=SHARED_LOCK );
56417 assert( p->eLock!=UNKNOWN_LOCK );
56418 assert( pPager->errCode==SQLITE_OK );
56420 assert( p->eLock>=RESERVED_LOCK );
56422 assert( pPager->dbSize==pPager->dbOrigSize );
56423 assert( pPager->dbOrigSize==pPager->dbFileSize );
56424 assert( pPager->dbOrigSize==pPager->dbHintSize );
56425 assert( pPager->setSuper==0 );
56429 assert( p->eLock!=UNKNOWN_LOCK );
56430 assert( pPager->errCode==SQLITE_OK );
56437 assert( p->eLock>=RESERVED_LOCK );
56438 assert( isOpen(p->jfd)
56439 || p->journalMode==PAGER_JOURNALMODE_OFF
56440 || p->journalMode==PAGER_JOURNALMODE_WAL
56443 assert( pPager->dbOrigSize==pPager->dbFileSize );
56444 assert( pPager->dbOrigSize==pPager->dbHintSize );
56448 assert( p->eLock==EXCLUSIVE_LOCK );
56449 assert( pPager->errCode==SQLITE_OK );
56451 assert( p->eLock>=EXCLUSIVE_LOCK );
56452 assert( isOpen(p->jfd)
56453 || p->journalMode==PAGER_JOURNALMODE_OFF
56454 || p->journalMode==PAGER_JOURNALMODE_WAL
56455 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56457 assert( pPager->dbOrigSize<=pPager->dbHintSize );
56461 assert( p->eLock==EXCLUSIVE_LOCK );
56462 assert( pPager->errCode==SQLITE_OK );
56464 assert( isOpen(p->jfd)
56465 || p->journalMode==PAGER_JOURNALMODE_OFF
56466 || p->journalMode==PAGER_JOURNALMODE_WAL
56467 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
56476 assert( pPager->errCode!=SQLITE_OK );
56477 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
56510 , p->zFilename
56511 , p->eState==PAGER_OPEN ? "OPEN" :
56512 p->eState==PAGER_READER ? "READER" :
56513 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
56514 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
56515 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
56516 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
56517 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
56518 , (int)p->errCode
56519 , p->eLock==NO_LOCK ? "NO_LOCK" :
56520 p->eLock==RESERVED_LOCK ? "RESERVED" :
56521 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
56522 p->eLock==SHARED_LOCK ? "SHARED" :
56523 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
56524 , p->exclusiveMode ? "exclusive" : "normal"
56525 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
56526 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
56527 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
56528 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
56529 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
56530 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
56531 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
56532 , p->journalOff, p->journalHdr
56533 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
56552 if( pPager->errCode ){
56553 pPager->xGet = getPageError;
56557 && pPager->xCodec==0
56560 pPager->xGet = getPageMMap;
56563 pPager->xGet = getPageNormal;
56568 ** Return true if it is necessary to write page *pPg into the sub-journal.
56569 ** A page needs to be written into the sub-journal if there exists one
56572 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
56573 ** * The bit corresponding to the page-number is not set in
56577 Pager *pPager = pPg->pPager;
56579 Pgno pgno = pPg->pgno;
56581 for(i=0; i<pPager->nSavepoint; i++){
56582 p = &pPager->aSavepoint[i];
56583 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
56584 for(i=i+1; i<pPager->nSavepoint; i++){
56585 pPager->aSavepoint[i].bTruncateOnRelease = 0;
56598 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
56603 ** Read a 32-bit integer from the given file descriptor. Store the integer
56607 ** All values are stored on disk as big-endian.
56619 ** Write a 32-bit integer into a string buffer in big-endian byte order.
56625 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
56646 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
56649 if( isOpen(pPager->fd) ){
56650 assert( pPager->eLock>=eLock );
56651 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
56652 if( pPager->eLock!=UNKNOWN_LOCK ){
56653 pPager->eLock = (u8)eLock;
56657 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
56675 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
56676 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
56677 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
56678 pPager->eLock = (u8)eLock;
56686 ** This function determines whether or not the atomic-write or
56687 ** atomic-batch-write optimizations can be used with this pager. The
56688 ** atomic-write optimization can be used if:
56698 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
56699 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
56711 assert( isOpen(pPager->fd) );
56712 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
56718 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
56719 return -1;
56725 int nSector = pPager->sectorSize;
56726 int szPage = pPager->pageSize;
56748 ** Return a 32-bit hash of the page data for pPage.
56759 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
56762 pPage->pageHash = pager_pagehash(pPage);
56768 ** that the page is either dirty or still matches the calculated page-hash.
56772 Pager *pPager = pPg->pPager;
56773 assert( pPager->eState!=PAGER_ERROR );
56774 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
56786 ** This function attempts to read a super-journal file name from the
56789 ** used to store a super-journal file name at the end of a journal file.
56793 ** enough space to write the super-journal name). If the super-journal
56795 ** nul-terminator), then this is handled as if no super-journal name
56798 ** If a super-journal file name is present at the end of the journal
56800 ** nul-terminator byte is appended to the buffer following the
56801 ** super-journal file name.
56803 ** If it is determined that no super-journal file name is present
56811 u32 len; /* Length in bytes of super-journal name */
56820 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
56822 || len>szJ-16
56824 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
56825 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
56827 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
56832 /* See if the checksum matches the super-journal name */
56834 cksum -= zSuper[u];
56838 ** containing the super-journal filename is corrupted. This means
56840 ** super-journal filename.
56852 ** following the value in pPager->journalOff, assuming a sector
56853 ** size of pPager->sectorSize bytes.
56858 ** ---------------------------------------
56867 i64 c = pPager->journalOff;
56869 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
56873 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
56880 ** This function is a no-op if the journal file has not been written to
56883 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
56885 ** zero the 28-byte header at the start of the journal file. In either case,
56886 ** if the pager is not in no-sync mode, sync the journal file immediately
56889 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
56900 assert( isOpen(pPager->jfd) );
56901 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
56902 if( pPager->journalOff ){
56903 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
56907 rc = sqlite3OsTruncate(pPager->jfd, 0);
56910 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
56912 if( rc==SQLITE_OK && !pPager->noSync ){
56913 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
56924 rc = sqlite3OsFileSize(pPager->jfd, &sz);
56926 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
56939 ** - 8 bytes: Magic identifying journal format.
56940 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
56941 ** - 4 bytes: Random number used for page hash.
56942 ** - 4 bytes: Initial database page count.
56943 ** - 4 bytes: Sector size used by the process that wrote this journal.
56944 ** - 4 bytes: Database page size.
56946 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
56950 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
56951 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
56955 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
56965 for(ii=0; ii<pPager->nSavepoint; ii++){
56966 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
56967 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
56971 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
56974 ** Write the nRec Field - the number of page records that follow this
56977 ** if in full-sync mode), the zero is overwritten with the true number
56987 ** * When the pager is in no-sync mode. Corruption can follow a
56993 assert( isOpen(pPager->fd) || pPager->noSync );
56994 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
56995 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
57003 /* The random check-hash initializer */
57004 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
57005 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
57007 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
57009 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
57012 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
57020 nHeader-(sizeof(aJournalMagic)+20));
57031 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
57034 ** The loop is required here in case the sector-size is larger than the
57040 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
57041 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
57042 assert( pPager->journalHdr <= pPager->journalOff );
57043 pPager->journalOff += nHeader;
57053 ** pPager->journalOff. See comments above function writeJournalHdr() for
57058 ** database before the transaction began, in pages. Also, pPager->cksumInit
57077 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
57083 pPager->journalOff = journalHdrOffset(pPager);
57084 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
57087 iHdrOff = pPager->journalOff;
57094 if( isHot || iHdrOff!=pPager->journalHdr ){
57095 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
57104 /* Read the first three 32-bit fields of the journal header: The nRec
57105 ** field, the checksum-initializer and the database size at the start
57108 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
57109 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
57110 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
57115 if( pPager->journalOff==0 ){
57116 u32 iPageSize; /* Page-size field of journal header */
57117 u32 iSectorSize; /* Sector-size field of journal header */
57119 /* Read the page-size and sector-size journal header fields. */
57120 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
57121 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
57126 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
57131 iPageSize = pPager->pageSize;
57134 /* Check that the values read from the page-size and sector-size fields
57141 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
57143 /* If the either the page-size or sector-size in the journal-header is
57144 ** invalid, then the process that wrote the journal-header must have
57151 /* Update the page-size to match the value read from the journal.
57155 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
57158 /* Update the assumed sector-size to match the value used by
57164 pPager->sectorSize = iSectorSize;
57167 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
57173 ** Write the supplied super-journal name into the journal file for pager
57174 ** pPager at the current location. The super-journal name must be the last
57175 ** thing written to a journal file. If the pager is in full-sync mode, the
57180 ** + N bytes: super-journal filename in utf-8.
57181 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
57182 ** + 4 bytes: super-journal name checksum.
57185 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
57186 ** name, where each byte is interpreted as a signed 8-bit integer.
57189 ** this call is a no-op.
57198 assert( pPager->setSuper==0 );
57202 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57203 || !isOpen(pPager->jfd)
57207 pPager->setSuper = 1;
57208 assert( pPager->journalHdr <= pPager->journalOff );
57215 /* If in full-sync mode, advance to the next disk sector before writing
57216 ** the super-journal name. This is in case the previous page written to
57219 if( pPager->fullSync ){
57220 pPager->journalOff = journalHdrOffset(pPager);
57222 iHdrOff = pPager->journalOff;
57224 /* Write the super-journal data to the end of the journal file. If
57227 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
57228 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
57229 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
57230 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
57231 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
57236 pPager->journalOff += (nSuper+20);
57238 /* If the pager is in peristent-journal mode, then the physical
57239 ** journal-file may extend past the end of the super-journal name
57241 ** dangerous because the code to rollback a hot-journal file
57242 ** will not be able to find the super-journal name to determine
57248 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
57249 && jrnlSize>pPager->journalOff
57251 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
57257 ** Discard the entire contents of the in-memory page-cache.
57260 pPager->iDataVersion++;
57261 sqlite3BackupRestart(pPager->pBackup);
57262 sqlite3PcacheClear(pPager->pPCache);
57266 ** Return the pPager->iDataVersion value
57269 return pPager->iDataVersion;
57274 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
57279 for(ii=0; ii<pPager->nSavepoint; ii++){
57280 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
57282 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
57283 sqlite3OsClose(pPager->sjfd);
57285 sqlite3_free(pPager->aSavepoint);
57286 pPager->aSavepoint = 0;
57287 pPager->nSavepoint = 0;
57288 pPager->nSubRec = 0;
57300 for(ii=0; ii<pPager->nSavepoint; ii++){
57301 PagerSavepoint *p = &pPager->aSavepoint[ii];
57302 if( pgno<=p->nOrig ){
57303 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
57312 ** This function is a no-op if the pager is in exclusive mode and not
57316 ** If the pager is not in exclusive-access mode, the database file is
57317 ** completely unlocked. If the file is unlocked and the file-system does
57323 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
57324 ** or not, any journal file left in the file-system will be treated
57325 ** as a hot-journal and rolled back the next time a read-transaction
57330 assert( pPager->eState==PAGER_READER
57331 || pPager->eState==PAGER_OPEN
57332 || pPager->eState==PAGER_ERROR
57335 sqlite3BitvecDestroy(pPager->pInJournal);
57336 pPager->pInJournal = 0;
57340 assert( !isOpen(pPager->jfd) );
57341 sqlite3WalEndReadTransaction(pPager->pWal);
57342 pPager->eState = PAGER_OPEN;
57343 }else if( !pPager->exclusiveMode ){
57345 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
57359 || 1!=(pPager->journalMode & 5)
57361 sqlite3OsClose(pPager->jfd);
57370 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
57371 pPager->eLock = UNKNOWN_LOCK;
57375 ** without clearing the error code. This is intentional - the error
57378 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
57379 pPager->eState = PAGER_OPEN;
57385 ** normal and exclusive-locking mode.
57387 assert( pPager->errCode==SQLITE_OK || !MEMDB );
57388 if( pPager->errCode ){
57389 if( pPager->tempFile==0 ){
57391 pPager->changeCountDone = 0;
57392 pPager->eState = PAGER_OPEN;
57394 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
57396 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57397 pPager->errCode = SQLITE_OK;
57401 pPager->journalOff = 0;
57402 pPager->journalHdr = 0;
57403 pPager->setSuper = 0;
57410 ** the error-code about to be returned by a pager API function. The
57414 ** IOERR sub-codes, the pager enters the ERROR state and the error code
57418 ** The ERROR state indicates that the contents of the pager-cache
57420 ** the contents of the pager-cache. If a transaction was active when
57423 ** it were a hot-journal).
57429 pPager->errCode==SQLITE_FULL ||
57430 pPager->errCode==SQLITE_OK ||
57431 (pPager->errCode & 0xff)==SQLITE_IOERR
57434 pPager->errCode = rc;
57435 pPager->eState = PAGER_ERROR;
57451 ** * For non-TEMP databases, always sync to disk. This is necessary
57460 if( pPager->tempFile==0 ) return 1;
57462 if( !isOpen(pPager->fd) ) return 0;
57463 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
57469 ** after rollback of a hot-journal, or if an error occurs while opening
57470 ** the journal file or writing the very first journal-header of a
57475 ** exclusive than a RESERVED lock, it is a no-op.
57481 ** transaction. Nor will it be considered to be a hot-journal by this
57484 ** the current journal-mode (Pager.journalMode value), as follows:
57488 ** in-memory journal.
57507 ** If running in non-exclusive rollback mode, the lock on the file is
57525 ** is no write-transaction active but a RESERVED or greater lock is
57528 ** 1. After a successful hot-journal rollback, it is called with
57533 ** read-transaction, this function is called with eState==PAGER_READER
57534 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
57537 assert( pPager->eState!=PAGER_ERROR );
57538 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
57543 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
57544 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
57546 if( isOpen(pPager->jfd) ){
57550 if( sqlite3JournalIsInMemory(pPager->jfd) ){
57551 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
57552 sqlite3OsClose(pPager->jfd);
57553 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
57554 if( pPager->journalOff==0 ){
57557 rc = sqlite3OsTruncate(pPager->jfd, 0);
57558 if( rc==SQLITE_OK && pPager->fullSync ){
57564 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
57567 pPager->journalOff = 0;
57568 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
57569 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
57571 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
57572 pPager->journalOff = 0;
57575 ** a hot-journal was just rolled back. In this case the journal
57577 ** the database file, it will do so using an in-memory journal.
57579 int bDelete = !pPager->tempFile;
57580 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
57581 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
57582 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
57583 || pPager->journalMode==PAGER_JOURNALMODE_WAL
57585 sqlite3OsClose(pPager->jfd);
57587 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
57593 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
57594 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
57597 p->pageHash = 0;
57603 sqlite3BitvecDestroy(pPager->pInJournal);
57604 pPager->pInJournal = 0;
57605 pPager->nRec = 0;
57608 sqlite3PcacheCleanAll(pPager->pPCache);
57610 sqlite3PcacheClearWritable(pPager->pPCache);
57612 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
57616 /* Drop the WAL write-lock, if any. Also, if the connection was in
57620 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
57622 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
57623 /* This branch is taken when committing a transaction in rollback-journal
57629 assert( pPager->eLock==EXCLUSIVE_LOCK );
57630 rc = pager_truncate(pPager, pPager->dbSize);
57634 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
57638 if( !pPager->exclusiveMode
57639 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
57643 pPager->eState = PAGER_READER;
57644 pPager->setSuper = 0;
57655 ** call to pager_unlock() will discard all in-memory pages, unlock
57657 ** means that there is a hot-journal left in the file-system, the next
57667 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
57669 if( pPager->eState>=PAGER_WRITER_LOCKED ){
57673 }else if( !pPager->exclusiveMode ){
57674 assert( pPager->eState==PAGER_READER );
57682 ** Parameter aData must point to a buffer of pPager->pageSize bytes
57684 ** page of data and the current value of pPager->cksumInit.
57687 ** random initial value (pPager->cksumInit) and every 200th byte
57688 ** of the page data, starting with byte offset (pPager->pageSize%200).
57689 ** Each byte is interpreted as an 8-bit unsigned integer.
57701 u32 cksum = pPager->cksumInit; /* Checksum value to return */
57702 int i = pPager->pageSize-200; /* Loop counter */
57705 i -= 200;
57716 if( pPager->xCodecSizeChng ){
57717 pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
57718 (int)pPager->nReserve);
57722 # define pagerReportSize(X) /* No-op if we do not support a codec */
57732 if( pDest->nReserve!=pSrc->nReserve ){
57733 pDest->nReserve = pSrc->nReserve;
57741 ** from the sub-journal (if isMainJrnl==0) and playback that page.
57745 ** The main rollback journal uses checksums - the statement journal does
57748 ** If the page number of the page record read from the (sub-)journal file
57758 ** If the page record is successfully read from the (sub-)journal file
57760 ** while reading the record from the (sub-)journal file or while writing
57762 ** is successfully read from the (sub-)journal file but appears to be
57766 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
57780 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
57792 ** the codec. It is false for pure in-memory journals. */
57793 const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0);
57798 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
57799 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
57801 aData = pPager->pTmpSpace;
57807 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
57808 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
57809 ** only reads from the main journal, not the sub-journal.
57811 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
57812 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
57814 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
57816 /* Read the page number and page data from the journal or sub-journal
57819 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
57822 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
57824 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
57835 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
57839 rc = read32bits(jfd, (*pOffset)-4, &cksum);
57855 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
57856 pPager->nReserve = ((u8*)aData)[20];
57863 ** An exception to the above rule: If the database is in no-sync mode
57873 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
57890 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
57900 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
57902 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
57903 (isMainJrnl?"main-journal":"sub-journal")
57906 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
57908 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
57910 if( isOpen(pPager->fd)
57911 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
57914 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
57915 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
57919 ** This is usually safe even for an encrypted database - as the data
57921 ** is if the data was just read from an in-memory sub-journal. In that
57927 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
57931 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
57933 if( pgno>pPager->dbFileSize ){
57934 pPager->dbFileSize = pgno;
57936 if( pPager->pBackup ){
57940 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
57944 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
57948 ** the database and the page is not in-memory, there is a potential
57949 ** problem. When the page is next fetched by the b-tree layer, it
57955 ** if the page is on the free-list at the start of the transaction, then
57958 ** The solution is to add an in-memory page to the cache containing
57959 ** the data just read from the sub-journal. Mark the page as dirty
57960 ** and if the pager requires a journal-sync, then mark the page as
57961 ** requiring a journal-sync before it is written.
57964 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
57965 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
57967 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
57968 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
57980 pData = pPg->pData;
57981 memcpy(pData, (u8*)aData, pPager->pageSize);
57982 pPager->xReiniter(pPg);
57992 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
57997 if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); }
58005 ** Parameter zSuper is the name of a super-journal file. A single journal
58006 ** file that referred to the super-journal file has just been rolled back.
58007 ** This routine checks if it is possible to delete the super-journal file,
58013 ** When a super-journal file is created, it is populated with the names
58014 ** of all of its child journals, one after another, formatted as utf-8
58016 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
58019 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
58021 ** A super-journal file may only be deleted once all of its child
58024 ** This function reads the contents of the super-journal file into
58029 ** * if the child journal contains a reference to super-journal
58035 ** the file-system using sqlite3OsDelete().
58043 ** the entire contents of the super-journal file. This could be
58044 ** a couple of kilobytes or so - potentially larger than the page
58048 sqlite3_vfs *pVfs = pPager->pVfs;
58050 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
58051 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
58052 char *zSuperJournal = 0; /* Contents of super-journal file */
58053 i64 nSuperJournal; /* Size of super-journal file */
58055 char *zSuperPtr; /* Space to hold super-journal filename */
58060 ** If successful, open the super-journal file for reading.
58062 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
58069 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
58073 /* Load the entire super-journal file into space obtained from
58075 ** sufficient space (in zSuperPtr) to hold the names of super-journal
58076 ** files extracted from regular rollback-journals.
58080 nSuperPtr = pVfs->mxPathname+1;
58095 while( (zJournal-zSuperJournal)<nSuperJournal ){
58102 /* One of the journals pointed to by the super-journal exists.
58103 ** Open it and check if it points at the super-journal. If
58104 ** so, return without deleting the super-journal file.
58124 /* We have a match. Do not delete the super-journal file. */
58147 ** file in the file-system. This only happens when committing a transaction,
58148 ** or rolling back a transaction (including rolling back a hot-journal).
58151 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
58152 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
58167 assert( pPager->eState!=PAGER_ERROR );
58168 assert( pPager->eState!=PAGER_READER );
58170 if( isOpen(pPager->fd)
58171 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58174 int szPage = pPager->pageSize;
58175 assert( pPager->eLock==EXCLUSIVE_LOCK );
58177 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
58181 rc = sqlite3OsTruncate(pPager->fd, newSize);
58183 char *pTmp = pPager->pTmpSpace;
58185 testcase( (newSize-szPage) == currentSize );
58186 testcase( (newSize-szPage) > currentSize );
58187 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
58188 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
58191 pPager->dbFileSize = nPage;
58199 ** Return a sanitized version of the sector-size of OS file pFile. The
58218 ** super-journal pointers within created journal files.
58222 ** Otherwise, for non-temporary files, the effective sector size is
58229 ** pPager->sectorSize is to define the "blast radius" of bytes that
58237 assert( isOpen(pPager->fd) || pPager->tempFile );
58239 if( pPager->tempFile
58240 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
58246 pPager->sectorSize = 512;
58248 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
58259 ** (2) 4 byte big-endian integer which is the number of valid page records
58262 ** (3) 4 byte big-endian integer which is the initial value for the
58266 ** (5) 4 byte big-endian integer which is the sector size. The header
58268 ** (6) 4 byte big-endian integer which is the page size.
58272 ** + pPager->pageSize bytes of data.
58289 ** no-sync option for the journal. A power failure could lead to corruption
58293 ** If the file opened as the journal file is not a well-formed
58299 ** If an I/O or malloc() error occurs, the journal-file is not deleted
58310 sqlite3_vfs *pVfs = pPager->pVfs;
58317 char *zSuper = 0; /* Name of super-journal file if any */
58320 u32 savedPageSize = pPager->pageSize;
58325 assert( isOpen(pPager->jfd) );
58326 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
58331 /* Read the super-journal name from the journal, if it is present.
58332 ** If a super-journal file name is specified, but the file is not
58338 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
58342 zSuper = pPager->pTmpSpace;
58343 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58351 pPager->journalOff = 0;
58373 ** working in no-sync mode. This means that the rest of the journal
58378 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
58379 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
58397 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
58398 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
58404 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
58409 pPager->dbSize = mxPg;
58410 if( pPager->mxPgno<mxPg ){
58411 pPager->mxPgno = mxPg;
58423 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
58428 pPager->journalOff = szJ;
58454 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
58458 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
58462 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
58466 ** malloc error that occurred after the change-counter was updated but
58467 ** before the transaction was committed, then the change-counter
58470 ** update the change-counter at all. This may lead to cache inconsistency
58474 pPager->changeCountDone = pPager->tempFile;
58477 /* Leave 4 bytes of space before the super-journal filename in memory.
58481 zSuper = &pPager->pTmpSpace[4];
58482 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
58486 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
58495 /* If there was a super-journal and this routine will return success,
58496 ** see if it is possible to delete the super-journal.
58498 assert( zSuper==&pPager->pTmpSpace[4] );
58499 memset(&zSuper[-4], 0, 4);
58505 nPlayback, pPager->zJournal);
58520 ** pPg->pData. A shared lock or greater must be held on the database
58530 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
58536 assert( pPager->eState>=PAGER_READER && !MEMDB );
58537 assert( isOpen(pPager->fd) );
58540 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
58544 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
58548 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
58549 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
58555 if( pPg->pgno==1 ){
58562 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
58569 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
58571 u8 *dbFileVers = &((u8*)pPg->pData)[24];
58572 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
58575 CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT);
58578 PAGER_INCR(pPager->nRead);
58579 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
58581 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
58587 ** Update the value of the change-counter at offsets 24 and 92 in
58591 ** routine which only updates the change-counter if the update is actually
58592 ** needed, as determined by the pPager->changeCountDone state variable.
58599 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
58600 put32bits(((char*)pPg->pData)+24, change_counter);
58605 put32bits(((char*)pPg->pData)+92, change_counter);
58606 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
58635 pPager->xReiniter(pPg);
58649 sqlite3BackupRestart(pPager->pBackup);
58668 pPager->dbSize = pPager->dbOrigSize;
58669 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
58670 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58672 PgHdr *pNext = pList->pDirty;
58673 rc = pagerUndoCallback((void *)pPager, pList->pgno);
58699 assert( pPager->pWal );
58703 for(p=pList; p && p->pDirty; p=p->pDirty){
58704 assert( p->pgno < p->pDirty->pgno );
58708 assert( pList->pDirty==0 || isCommit );
58716 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
58717 if( p->pgno<=nTruncate ){
58718 ppNext = &p->pDirty;
58726 pPager->aStat[PAGER_STAT_WRITE] += nList;
58728 if( pList->pgno==1 ) pager_write_changecounter(pList);
58729 rc = sqlite3WalFrames(pPager->pWal,
58730 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
58732 if( rc==SQLITE_OK && pPager->pBackup ){
58733 for(p=pList; p; p=p->pDirty){
58734 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
58739 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58740 for(p=pList; p; p=p->pDirty){
58761 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
58768 sqlite3WalEndReadTransaction(pPager->pWal);
58770 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
58773 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
58792 /* Query the WAL sub-system for the database size. The WalDbsize()
58795 ** available from the WAL sub-system if the log file is empty or
58798 assert( pPager->eState==PAGER_OPEN );
58799 assert( pPager->eLock>=SHARED_LOCK );
58800 assert( isOpen(pPager->fd) );
58801 assert( pPager->tempFile==0 );
58802 nPage = sqlite3WalDbsize(pPager->pWal);
58805 ** WAL sub-system, determine the page count based on the size of
58807 ** integer multiple of the page-size, round up the result.
58809 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
58811 int rc = sqlite3OsFileSize(pPager->fd, &n);
58815 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
58822 if( nPage>pPager->mxPgno ){
58823 pPager->mxPgno = (Pgno)nPage;
58832 ** Check if the *-wal file that corresponds to the database opened by pPager
58833 ** exists if the database is not empy, or verify that the *-wal file does
58836 ** If the database is not empty and the *-wal file exists, open the pager
58837 ** in WAL mode. If the database is empty or if no *-wal file exists and
58845 ** a WAL on a none-empty database, this ensures there is no race condition
58851 assert( pPager->eState==PAGER_OPEN );
58852 assert( pPager->eLock>=SHARED_LOCK );
58854 if( !pPager->tempFile ){
58857 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
58866 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
58868 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
58871 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
58872 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
58882 ** the entire super-journal file. The case pSavepoint==NULL occurs when
58886 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
58899 ** * Pages are then played back from the sub-journal file, starting
58913 ** (or transaction). No page with a page-number greater than this value
58918 i64 iHdrOff; /* End of first segment of main-journal records */
58922 assert( pPager->eState!=PAGER_ERROR );
58923 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58927 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
58936 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
58937 pPager->changeCountDone = pPager->tempFile;
58943 /* Use pPager->journalOff as the effective size of the main rollback
58946 ** past pPager->journalOff is off-limits to us.
58948 szJ = pPager->journalOff;
58954 ** greater than the current database size (pPager->dbSize) but those
58959 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
58960 pPager->journalOff = pSavepoint->iOffset;
58961 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
58962 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
58966 pPager->journalOff = 0;
58971 ** of the main journal file. Continue to skip out-of-range pages and
58974 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
58982 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
58987 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
58989 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
58991 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
58992 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
58996 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
58998 /* Finally, rollback pages from the sub-journal. Page that were
59000 ** will be skipped. Out-of-range pages are also skipped.
59004 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
59007 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
59009 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
59010 assert( offset==(i64)ii*(4+pPager->pageSize) );
59018 pPager->journalOff = szJ;
59025 ** Change the maximum number of in-memory pages that are allowed
59029 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
59033 ** Change the maximum number of in-memory pages that are allowed
59037 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
59045 sqlite3_file *fd = pPager->fd;
59046 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
59048 sz = pPager->szMmap;
59049 pPager->bUseFetch = (sz>0);
59051 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
59060 pPager->szMmap = szMmap;
59068 sqlite3PcacheShrink(pPager->pPCache);
59090 ** database (with some additional information - the nRec field
59091 ** of the journal header - being written in between the two
59101 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
59112 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
59128 if( pPager->tempFile ){
59129 pPager->noSync = 1;
59130 pPager->fullSync = 0;
59131 pPager->extraSync = 0;
59133 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
59134 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
59135 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
59137 if( pPager->noSync ){
59138 pPager->syncFlags = 0;
59140 pPager->syncFlags = SQLITE_SYNC_FULL;
59142 pPager->syncFlags = SQLITE_SYNC_NORMAL;
59144 pPager->walSyncFlags = (pPager->syncFlags<<2);
59145 if( pPager->fullSync ){
59146 pPager->walSyncFlags |= pPager->syncFlags;
59148 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
59149 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
59152 pPager->doNotSpill &= ~SPILLFLAG_OFF;
59154 pPager->doNotSpill |= SPILLFLAG_OFF;
59196 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
59204 ** The pager invokes the busy-handler if sqlite3OsLock() returns
59205 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
59209 ** (which occurs during hot-journal rollback). Summary:
59212 ** --------------------------------------------------------
59213 ** NO_LOCK -> SHARED_LOCK | Yes
59214 ** SHARED_LOCK -> RESERVED_LOCK | No
59215 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
59216 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
59218 ** If the busy-handler callback returns non-zero, the lock is
59224 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
59228 pPager->xBusyHandler = xBusyHandler;
59229 pPager->pBusyHandlerArg = pBusyHandlerArg;
59230 ap = (void **)&pPager->xBusyHandler;
59233 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
59241 ** is a no-op. The value returned is the error state error code (i.e.
59242 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
59251 ** * the database is either not an in-memory database or it is
59252 ** an in-memory database that currently consists of zero pages.
59276 ** is a no-op for that case anyhow.
59281 if( (pPager->memDb==0 || pPager->dbSize==0)
59282 && sqlite3PcacheRefCount(pPager->pPCache)==0
59283 && pageSize && pageSize!=(u32)pPager->pageSize
59288 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
59289 rc = sqlite3OsFileSize(pPager->fd, &nByte);
59292 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
59304 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
59307 sqlite3PageFree(pPager->pTmpSpace);
59308 pPager->pTmpSpace = pNew;
59309 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
59310 pPager->pageSize = pageSize;
59311 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
59317 *pPageSize = pPager->pageSize;
59319 if( nReserve<0 ) nReserve = pPager->nReserve;
59321 pPager->nReserve = (i16)nReserve;
59336 return pPager->pTmpSpace;
59348 pPager->mxPgno = mxPage;
59350 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
59351 /* assert( pPager->mxPgno>=pPager->dbSize ); */
59355 return pPager->mxPgno;
59363 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
59372 sqlite3_io_error_pending = -1;
59399 assert( isOpen(pPager->fd) || pPager->tempFile );
59407 if( isOpen(pPager->fd) ){
59409 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
59418 ** This function may only be called when a read-transaction is open on
59421 ** However, if the file is between 1 and <page-size> bytes in size, then
59425 assert( pPager->eState>=PAGER_READER );
59426 assert( pPager->eState!=PAGER_WRITER_FINISHED );
59427 *pnPage = (int)pPager->dbSize;
59433 ** a similar or greater lock is already held, this function is a no-op
59448 /* Check that this is either a no-op (because the requested lock is
59449 ** already held), or one of the transitions that the busy-handler
59453 assert( (pPager->eLock>=locktype)
59454 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
59455 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
59460 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
59466 ** following is true for all dirty pages currently in the page-cache:
59472 ** be necessary to write the current content out to the sub-journal.
59481 ** sub-journal rolled back the content could not be restored and the
59487 Pager *pPager = pPg->pPager;
59488 assert( pPg->flags&PGHDR_DIRTY );
59489 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
59490 Pgno pgno = pPg->pgno;
59492 for(i=0; i<pPg->pPager->nSavepoint; i++){
59493 PagerSavepoint *p = &pPager->aSavepoint[i];
59494 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
59499 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
59506 ** Truncate the in-memory database file image to nPage pages. This
59517 assert( pPager->dbSize>=nPage || CORRUPT_DB );
59518 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
59519 pPager->dbSize = nPage;
59534 ** This function is called before attempting a hot-journal rollback. It
59535 ** syncs the journal file to disk, then sets pPager->journalHdr to the
59539 ** Syncing a hot-journal to disk before attempting to roll it back ensures
59540 ** that if a power-failure occurs during the rollback, the process that
59549 if( !pPager->noSync ){
59550 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
59553 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
59577 if( pPager->pMmapFreelist ){
59578 *ppPage = p = pPager->pMmapFreelist;
59579 pPager->pMmapFreelist = p->pDirty;
59580 p->pDirty = 0;
59581 assert( pPager->nExtra>=8 );
59582 memset(p->pExtra, 0, 8);
59584 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
59586 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
59589 p->pExtra = (void *)&p[1];
59590 p->flags = PGHDR_MMAP;
59591 p->nRef = 1;
59592 p->pPager = pPager;
59595 assert( p->pExtra==(void *)&p[1] );
59596 assert( p->pPage==0 );
59597 assert( p->flags==PGHDR_MMAP );
59598 assert( p->pPager==pPager );
59599 assert( p->nRef==1 );
59601 p->pgno = pgno;
59602 p->pData = pData;
59603 pPager->nMmapOut++;
59614 Pager *pPager = pPg->pPager;
59615 pPager->nMmapOut--;
59616 pPg->pDirty = pPager->pMmapFreelist;
59617 pPager->pMmapFreelist = pPg;
59619 assert( pPager->fd->pMethods->iVersion>=3 );
59620 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
59629 for(p=pPager->pMmapFreelist; p; p=pNext){
59630 pNext = p->pDirty;
59637 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
59644 if( pPager->tempFile ) return SQLITE_OK;
59645 if( pPager->dbSize==0 ) return SQLITE_OK;
59646 assert( pPager->zFilename && pPager->zFilename[0] );
59647 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
59649 /* If the HAS_MOVED file-control is unimplemented, assume that the file
59675 u8 *pTmp = (u8*)pPager->pTmpSpace;
59681 /* pPager->errCode = 0; */
59682 pPager->exclusiveMode = 0;
59686 assert( db || pPager->pWal==0 );
59687 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
59692 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
59693 pPager->pWal = 0;
59708 ** back or finalize it. The next database user will have to do hot-journal
59711 if( isOpen(pPager->jfd) ){
59720 sqlite3OsClose(pPager->jfd);
59721 sqlite3OsClose(pPager->fd);
59723 sqlite3PcacheClose(pPager->pPCache);
59726 if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
59729 assert( !pPager->aSavepoint && !pPager->pInJournal );
59730 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
59741 return pPg->pgno;
59755 ** disk and can be restored in the event of a hot-journal rollback.
59757 ** If the Pager.noSync flag is set, then this function is a no-op.
59758 ** Otherwise, the actions required depend on the journal-mode and the
59759 ** device characteristics of the file-system, as follows:
59761 ** * If the journal file is an in-memory journal file, no action need
59767 ** been written following it. If the pager is operating in full-sync
59773 ** Or, in pseudo-code:
59775 ** if( NOT <in-memory journal> ){
59777 ** if( <full-sync mode> ) xSync(<journal file>);
59790 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59791 || pPager->eState==PAGER_WRITER_DBMOD
59799 if( !pPager->noSync ){
59800 assert( !pPager->tempFile );
59801 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
59802 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
59803 assert( isOpen(pPager->jfd) );
59807 ** that wrote to this database was operating in persistent-journal
59810 ** file happens to be a journal-header (written as part of the
59811 ** previous connection's transaction), and a crash or power-failure
59815 ** hot-journal rollback following recovery. It may roll back all
59817 ** out-of-date data that follows it. Database corruption.
59833 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
59836 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
59839 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
59846 ** full-synchronous mode, sync the journal first. This ensures that
59856 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
59859 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
59862 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
59864 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
59871 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
59872 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
59877 pPager->journalHdr = pPager->journalOff;
59879 pPager->nRec = 0;
59884 pPager->journalHdr = pPager->journalOff;
59892 sqlite3PcacheClearSyncFlags(pPager->pPCache);
59893 pPager->eState = PAGER_WRITER_DBMOD;
59901 ** in-memory pages in the list to the database file. The argument may
59903 ** a no-op.
59910 ** If the pager is a temp-file pager and the actual file-system file
59935 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
59936 assert( pPager->eLock==EXCLUSIVE_LOCK );
59937 assert( isOpen(pPager->fd) || pList->pDirty==0 );
59939 /* If the file is a temp-file has not yet been opened, open it now. It
59941 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
59943 if( !isOpen(pPager->fd) ){
59944 assert( pPager->tempFile && rc==SQLITE_OK );
59945 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
59951 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
59953 && pPager->dbHintSize<pPager->dbSize
59954 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
59956 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
59957 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
59958 pPager->dbHintSize = pPager->dbSize;
59962 Pgno pgno = pList->pgno;
59966 ** make the file smaller (presumably by auto-vacuum code). Do not write
59972 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
59973 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
59976 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
59977 if( pList->pgno==1 ) pager_write_changecounter(pList);
59980 CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData);
59983 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
59990 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
59992 if( pgno>pPager->dbFileSize ){
59993 pPager->dbFileSize = pgno;
59995 pPager->aStat[PAGER_STAT_WRITE]++;
59998 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
60008 pList = pList->pDirty;
60015 ** Ensure that the sub-journal file is open. If it is already open, this
60016 ** function is a no-op.
60024 if( !isOpen(pPager->sjfd) ){
60029 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
60030 nStmtSpill = -1;
60032 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
60038 ** Append a record of the current state of page pPg to the sub-journal.
60040 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
60044 ** error code if the attempt to write to the sub-journal fails, or
60050 Pager *pPager = pPg->pPager;
60051 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
60053 /* Open the sub-journal, if it has not already been opened */
60054 assert( pPager->useJournal );
60055 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
60056 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
60059 || pPg->pgno>pPager->dbOrigSize
60063 /* If the sub-journal was opened successfully (or was already open),
60066 void *pData = pPg->pData;
60067 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
60071 if( !pPager->subjInMemory ){
60072 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
60076 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
60077 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
60079 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
60084 pPager->nSubRec++;
60085 assert( pPager->nSavepoint>0 );
60086 rc = addToSavepointBitvecs(pPager, pPg->pgno);
60101 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
60121 assert( pPg->pPager==pPager );
60122 assert( pPg->flags&PGHDR_DIRTY );
60140 if( NEVER(pPager->errCode) ) return SQLITE_OK;
60141 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
60142 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
60143 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
60144 if( pPager->doNotSpill
60145 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
60146 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
60151 pPager->aStat[PAGER_STAT_SPILL]++;
60152 pPg->pDirty = 0;
60162 if( pPager->tempFile==0 ){
60163 rc = sqlite3JournalCreate(pPager->jfd);
60169 if( pPg->flags&PGHDR_NEED_SYNC
60170 || pPager->eState==PAGER_WRITER_CACHEMOD
60177 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
60184 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
60195 int rc = pPager->errCode;
60197 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
60200 PgHdr *pNext = pList->pDirty;
60201 if( pList->nRef==0 ){
60217 ** If zFilename is NULL then a randomly-named temporary file is created
60221 ** This can be used to implement an in-memory database.
60247 int nExtra, /* Extra bytes append to each in-memory page */
60255 int tempFile = 0; /* True for temp files (incl. in-memory files) */
60256 int memDb = 0; /* True if this is an in-memory file */
60262 int readOnly = 0; /* True if this is a read-only file */
60273 /* Figure out how much space is required for each journal file-handle
60274 ** (there are two of them, the main journal and the sub-journal). */
60298 nPathname = pVfs->mxPathname+1;
60321 nUriByte = (int)(&z[1] - zUri);
60323 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
60325 ** the database being opened will be more than pVfs->mxPathname
60328 ** check for a hot-journal before reading.
60344 ** Database file handle (pVfs->szOsFile bytes)
60345 ** Sub-journal file handle (journalFileSize bytes)
60355 ** Some 3rd-party software, over which we have no control, depends on
60359 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
60362 ** filename format expected by 3rd-party software should be as follows:
60364 ** - Main Database Path
60365 ** - \0
60366 ** - Multiple URI components consisting of:
60367 ** - Key
60368 ** - \0
60369 ** - Value
60370 ** - \0
60371 ** - \0
60372 ** - Journal Path
60373 ** - \0
60374 ** - WAL Path (zWALName)
60375 ** - \0
60385 ROUND8(pVfs->szOsFile) + /* The main db file */
60403 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
60404 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
60405 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60406 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
60407 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
60412 pPager->zFilename = (char*)pPtr;
60425 pPager->zJournal = (char*)pPtr;
60427 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
60429 sqlite3FileSuffix3(zFilename,pPager->zJournal);
60430 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
60433 pPager->zJournal = 0;
60439 pPager->zWal = (char*)pPtr;
60441 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
60443 sqlite3FileSuffix3(zFilename, pPager->zWal);
60444 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
60447 pPager->zWal = 0;
60453 pPager->pVfs = pVfs;
60454 pPager->vfsFlags = vfsFlags;
60460 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
60463 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
60476 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
60480 if( szPageDflt<pPager->sectorSize ){
60481 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
60484 szPageDflt = (u32)pPager->sectorSize;
60501 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
60503 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
60513 ** This branch is also run for an in-memory database. An in-memory
60514 ** database is the same as a temp-file that is never written out to
60515 ** disk and uses an in-memory rollback journal.
60521 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
60522 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
60523 pPager->noLock = 1; /* Do no locking */
60531 assert( pPager->memDb==0 );
60532 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
60541 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
60547 sqlite3OsClose(pPager->fd);
60548 sqlite3PageFree(pPager->pTmpSpace);
60553 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
60554 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
60556 pPager->useJournal = (u8)useJournal;
60557 /* pPager->stmtOpen = 0; */
60558 /* pPager->stmtInUse = 0; */
60559 /* pPager->nRef = 0; */
60560 /* pPager->stmtSize = 0; */
60561 /* pPager->stmtJSize = 0; */
60562 /* pPager->nPage = 0; */
60563 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
60564 /* pPager->state = PAGER_UNLOCK; */
60565 /* pPager->errMask = 0; */
60566 pPager->tempFile = (u8)tempFile;
60570 pPager->exclusiveMode = (u8)tempFile;
60571 pPager->changeCountDone = pPager->tempFile;
60572 pPager->memDb = (u8)memDb;
60573 pPager->readOnly = (u8)readOnly;
60574 assert( useJournal || pPager->tempFile );
60575 pPager->noSync = pPager->tempFile;
60576 if( pPager->noSync ){
60577 assert( pPager->fullSync==0 );
60578 assert( pPager->extraSync==0 );
60579 assert( pPager->syncFlags==0 );
60580 assert( pPager->walSyncFlags==0 );
60582 pPager->fullSync = 1;
60583 pPager->extraSync = 0;
60584 pPager->syncFlags = SQLITE_SYNC_NORMAL;
60585 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
60587 /* pPager->pFirst = 0; */
60588 /* pPager->pFirstSynced = 0; */
60589 /* pPager->pLast = 0; */
60590 pPager->nExtra = (u16)nExtra;
60591 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
60592 assert( isOpen(pPager->fd) || tempFile );
60595 pPager->journalMode = PAGER_JOURNALMODE_OFF;
60597 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
60599 /* pPager->xBusyHandler = 0; */
60600 /* pPager->pBusyHandlerArg = 0; */
60601 pPager->xReiniter = xReinit;
60603 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
60604 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
60617 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
60618 zName--;
60620 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
60621 return pPager->fd;
60628 ** the file-system for the given pager. A hot journal is one that
60629 ** needs to be played back. According to this function, a hot-journal
60643 ** This routine does not check if there is a super-journal filename
60644 ** at the end of the file. If there is, and that super-journal file
60646 ** case this routine will return a false-positive. The pager_playback()
60650 ** If a hot-journal file is found to exist, *pExists is set to 1 and
60651 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
60653 ** to determine whether or not a hot-journal file exists, the IO error
60657 sqlite3_vfs * const pVfs = pPager->pVfs;
60660 int jrnlOpen = !!isOpen(pPager->jfd);
60662 assert( pPager->useJournal );
60663 assert( isOpen(pPager->fd) );
60664 assert( pPager->eState==PAGER_OPEN );
60666 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
60672 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
60682 ** in fact there is none. This results in a false-positive which will
60685 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
60689 assert( pPager->tempFile==0 );
60703 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
60704 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
60710 ** at least one non-zero bytes at the start of the journal file.
60716 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
60720 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
60725 sqlite3OsClose(pPager->jfd);
60752 ** has been successfully called. If a shared-lock is already held when
60753 ** this function is called, it is a no-op.
60760 ** the SHARED lock, the file-system is checked for a hot-journal,
60761 ** which is played back if present. Following any hot-journal
60763 ** the 'change-counter' field of the database file header and
60766 ** 2) If the pager is running in exclusive-mode, and there are currently
60773 ** occurs while locking the database, checking for a hot-journal file or
60779 /* This routine is only called from b-tree and only when there are no
60783 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
60785 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
60786 assert( pPager->errCode==SQLITE_OK );
60788 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
60789 int bHotJournal = 1; /* True if there exists a hot journal-file */
60792 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
60796 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
60803 if( pPager->eLock<=SHARED_LOCK ){
60810 if( pPager->readOnly ){
60820 ** hot-journal back.
60837 ** in exclusive-access mode the file descriptor will be kept open
60838 ** and possibly used for a transaction later on. Also, write-access
60845 ** may mean that the pager was in the error-state when this
60848 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
60849 sqlite3_vfs * const pVfs = pPager->pVfs;
60852 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
60856 assert( !pPager->tempFile );
60857 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
60858 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
60861 sqlite3OsClose(pPager->jfd);
60868 ** playing back the hot-journal so that we don't end up with
60874 if( isOpen(pPager->jfd) ){
60878 rc = pager_playback(pPager, !pPager->tempFile);
60879 pPager->eState = PAGER_OPEN;
60881 }else if( !pPager->exclusiveMode ){
60887 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
60906 assert( pPager->eState==PAGER_OPEN );
60907 assert( (pPager->eLock==SHARED_LOCK)
60908 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
60912 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
60913 /* The shared-lock has just been acquired then check to
60917 ** single unnecessary sqlite3OsRead() call at the start-up.
60921 ** a 32-bit counter that is incremented with each change. The
60929 char dbFileVers[sizeof(pPager->dbFileVers)];
60932 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
60940 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
60950 sqlite3OsUnfetch(pPager->fd, 0, 0);
60955 /* If there is a WAL file in the file-system, open this database in WAL
60956 ** mode. Otherwise, the following function call is a no-op.
60960 assert( pPager->pWal==0 || rc==SQLITE_OK );
60969 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
60970 rc = pagerPagecount(pPager, &pPager->dbSize);
60977 assert( pPager->eState==PAGER_OPEN );
60979 pPager->eState = PAGER_READER;
60980 pPager->hasHeldSharedLock = 1;
60991 ** nothing to rollback, so this routine is a no-op.
60994 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
60995 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
61008 ** getPageNormal() -- The normal getter
61009 ** getPageError() -- Used if the pager is in an error state
61010 ** getPageMmap() -- Used if memory-mapped I/O is enabled
61032 ** a) When reading a free-list leaf page from the database, and
61050 ** to find a page in the in-memory cache first. If the page is not already
61052 ** just returns 0. This routine acquires a read-lock the first time it
61068 assert( pPager->errCode==SQLITE_OK );
61069 assert( pPager->eState>=PAGER_READER );
61071 assert( pPager->hasHeldSharedLock==1 );
61074 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
61077 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
61084 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
61086 assert( pPg->pgno==pgno );
61087 assert( pPg->pPager==pPager || pPg->pPager==0 );
61090 if( pPg->pPager && !noContent ){
61094 pPager->aStat[PAGER_STAT_HIT]++;
61109 pPg->pPager = pPager;
61111 assert( !isOpen(pPager->fd) || !MEMDB );
61112 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
61113 if( pgno>pPager->mxPgno ){
61118 /* Failure to set the bits in the InJournal bit-vectors is benign.
61125 if( pgno<=pPager->dbOrigSize ){
61126 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
61133 memset(pPg->pData, 0, pPager->pageSize);
61136 assert( pPg->pPager==pPager );
61137 pPager->aStat[PAGER_STAT_MISS]++;
61158 /* The page getter for when memory-mapped I/O is enabled */
61169 /* It is acceptable to use a read-only (mmap) page for any page except
61170 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
61172 ** temporary or in-memory database. */
61174 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
61179 assert( pPager->xCodec==0 );
61189 assert( pPager->eState>=PAGER_READER );
61191 assert( pPager->hasHeldSharedLock==1 );
61192 assert( pPager->errCode==SQLITE_OK );
61195 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
61203 rc = sqlite3OsFetch(pPager->fd,
61204 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
61207 if( pPager->eState>PAGER_READER || pPager->tempFile ){
61213 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
61239 assert( pPager->errCode!=SQLITE_OK );
61241 return pPager->errCode;
61254 return pPager->xGet(pPager, pgno, ppPage, flags);
61258 ** Acquire a page if it is already in the in-memory cache. Do
61272 assert( pPager->pPCache!=0 );
61273 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
61274 assert( pPage==0 || pPager->hasHeldSharedLock );
61276 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
61292 TESTONLY( Pager *pPager = pPg->pPager; )
61294 if( pPg->flags & PGHDR_MMAP ){
61295 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
61301 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
61309 assert( pPg->pgno==1 );
61310 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
61311 pPager = pPg->pPager;
61322 ** to the start of it. If there are active savepoints, open the sub-journal
61340 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
61342 assert( pPager->eState==PAGER_WRITER_LOCKED );
61344 assert( pPager->pInJournal==0 );
61346 /* If already in the error state, this function is a no-op. But on
61349 if( NEVER(pPager->errCode) ) return pPager->errCode;
61351 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
61352 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
61353 if( pPager->pInJournal==0 ){
61358 if( !isOpen(pPager->jfd) ){
61359 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
61360 sqlite3MemJournalOpen(pPager->jfd);
61365 if( pPager->tempFile ){
61379 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
61383 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
61388 ** the sub-journal if necessary.
61392 pPager->nRec = 0;
61393 pPager->journalOff = 0;
61394 pPager->setSuper = 0;
61395 pPager->journalHdr = 0;
61401 sqlite3BitvecDestroy(pPager->pInJournal);
61402 pPager->pInJournal = 0;
61403 pPager->journalOff = 0;
61405 assert( pPager->eState==PAGER_WRITER_LOCKED );
61406 pPager->eState = PAGER_WRITER_CACHEMOD;
61413 ** Begin a write-transaction on the specified pager object. If a
61414 ** write-transaction has already been opened, this function is a no-op.
61421 ** If the subjInMemory argument is non-zero, then any sub-journal opened
61422 ** within this transaction will be opened as an in-memory file. This
61423 ** has no effect if the sub-journal is already opened (as it may be when
61425 ** sub-journal. If the subjInMemory argument is zero, then any required
61426 ** sub-journal is implemented in-memory if pPager is an in-memory database,
61432 if( pPager->errCode ) return pPager->errCode;
61433 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
61434 pPager->subjInMemory = (u8)subjInMemory;
61436 if( pPager->eState==PAGER_READER ){
61437 assert( pPager->pInJournal==0 );
61443 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
61448 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
61453 ** The busy-handler is not invoked if another connection already
61454 ** holds the write-lock. If possible, the upper layer will call it.
61456 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
61460 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
61475 ** transactions may copy data from the sub-journal into the database
61479 pPager->eState = PAGER_WRITER_LOCKED;
61480 pPager->dbHintSize = pPager->dbSize;
61481 pPager->dbFileSize = pPager->dbSize;
61482 pPager->dbOrigSize = pPager->dbSize;
61483 pPager->journalOff = 0;
61486 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
61487 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
61499 Pager *pPager = pPg->pPager;
61503 i64 iOff = pPager->journalOff;
61508 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
61510 assert( pPager->journalHdr<=pPager->journalOff );
61511 CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2);
61515 ** page in the block above, set the need-sync flag for the page.
61521 pPg->flags |= PGHDR_NEED_SYNC;
61523 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
61525 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
61527 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
61530 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
61531 pPager->journalOff, pPager->pageSize));
61534 PAGERID(pPager), pPg->pgno,
61535 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
61537 pPager->journalOff += 8 + pPager->pageSize;
61538 pPager->nRec++;
61539 assert( pPager->pInJournal!=0 );
61540 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
61543 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
61550 ** main journal or sub-journal as required. If the page is written into
61556 Pager *pPager = pPg->pPager;
61559 /* This routine is not called unless a write-transaction has already
61563 assert( pPager->eState==PAGER_WRITER_LOCKED
61564 || pPager->eState==PAGER_WRITER_CACHEMOD
61565 || pPager->eState==PAGER_WRITER_DBMOD
61568 assert( pPager->errCode==0 );
61569 assert( pPager->readOnly==0 );
61573 ** obtained the necessary locks to begin the write-transaction, but the
61581 if( pPager->eState==PAGER_WRITER_LOCKED ){
61585 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
61595 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
61596 if( pPager->pInJournal!=0
61597 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
61600 if( pPg->pgno<=pPager->dbOrigSize ){
61606 if( pPager->eState!=PAGER_WRITER_DBMOD ){
61607 pPg->flags |= PGHDR_NEED_SYNC;
61610 PAGERID(pPager), pPg->pgno,
61611 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
61615 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
61620 pPg->flags |= PGHDR_WRITEABLE;
61625 if( pPager->nSavepoint>0 ){
61630 if( pPager->dbSize<pPg->pgno ){
61631 pPager->dbSize = pPg->pgno;
61654 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
61655 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
61662 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
61663 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
61665 /* This trick assumes that both the page-size and sector-size are
61669 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
61671 nPageCount = pPager->dbSize;
61672 if( pPg->pgno>nPageCount ){
61673 nPage = (pPg->pgno - pg1)+1;
61674 }else if( (pg1+nPagePerSector-1)>nPageCount ){
61675 nPage = nPageCount+1-pg1;
61680 assert(pg1<=pPg->pgno);
61681 assert((pg1+nPage)>pPg->pgno);
61686 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
61691 if( pPage->flags&PGHDR_NEED_SYNC ){
61698 if( pPage->flags&PGHDR_NEED_SYNC ){
61716 pPage->flags |= PGHDR_NEED_SYNC;
61722 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
61723 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
61735 ** fit on a single disk sector. In this case all co-resident pages
61742 Pager *pPager = pPg->pPager;
61743 assert( (pPg->flags & PGHDR_MMAP)==0 );
61744 assert( pPager->eState>=PAGER_WRITER_LOCKED );
61746 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
61747 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
61749 }else if( pPager->errCode ){
61750 return pPager->errCode;
61751 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
61752 assert( pPager->tempFile==0 );
61766 return pPg->flags & PGHDR_WRITEABLE;
61784 ** This optimization cannot be used with a temp-file, as the page may
61791 Pager *pPager = pPg->pPager;
61792 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
61793 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
61794 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
61795 pPg->flags |= PGHDR_DONT_WRITE;
61796 pPg->flags &= ~PGHDR_WRITEABLE;
61797 testcase( pPg->flags & PGHDR_NEED_SYNC );
61804 ** change-counter, stored as a 4-byte big-endian integer starting at
61808 ** But this only happens if the pPager->changeCountDone flag is false.
61818 ** The isDirectMode flag may only be non-zero if the library was compiled
61820 ** if isDirect is non-zero, then the database file is updated directly
61827 assert( pPager->eState==PAGER_WRITER_CACHEMOD
61828 || pPager->eState==PAGER_WRITER_DBMOD
61833 ** atomic-write optimization is enabled in this build, then isDirect
61837 ** The idea is that if the atomic-write optimization is not
61850 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
61853 assert( !pPager->tempFile && isOpen(pPager->fd) );
61860 ** operating in direct-mode, make page 1 writable. When not in
61862 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
61875 assert( pPager->dbFileSize>0 );
61876 CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf);
61878 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
61879 pPager->aStat[PAGER_STAT_WRITE]++;
61882 /* Update the pager's copy of the change-counter. Otherwise, the
61884 ** flushed (as the change-counter values will not match). */
61886 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
61887 pPager->changeCountDone = 1;
61890 pPager->changeCountDone = 1;
61901 ** Sync the database file to disk. This is a no-op for in-memory databases
61904 ** If successful, or if called on a pager for which it is a no-op, this
61910 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
61912 if( rc==SQLITE_OK && !pPager->noSync ){
61914 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
61920 ** This function may only be called while a write-transaction is active in
61921 ** rollback. If the connection is in WAL mode, this call is a no-op.
61931 int rc = pPager->errCode;
61934 assert( pPager->eState==PAGER_WRITER_CACHEMOD
61935 || pPager->eState==PAGER_WRITER_DBMOD
61936 || pPager->eState==PAGER_WRITER_LOCKED
61948 ** of a super-journal file that should be written into the individual
61950 ** super-journal (a single database transaction).
61954 ** * The database file change-counter is updated,
61955 ** * the journal is synced (unless the atomic-write optimization is used),
61962 ** delete the super-journal file if specified).
61967 ** If the final parameter - noSync - is true, then the database file itself
61974 const char *zSuper, /* If not NULL, the super-journal name */
61979 assert( pPager->eState==PAGER_WRITER_LOCKED
61980 || pPager->eState==PAGER_WRITER_CACHEMOD
61981 || pPager->eState==PAGER_WRITER_DBMOD
61982 || pPager->eState==PAGER_ERROR
61987 if( NEVER(pPager->errCode) ) return pPager->errCode;
61993 pPager->zFilename, zSuper, pPager->dbSize));
61996 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
61998 assert( MEMDB==0 || pPager->tempFile );
61999 assert( isOpen(pPager->fd) || pPager->tempFile );
62001 /* If this is an in-memory db, or no pages have been written to, or this
62002 ** function has already been called, it is mostly a no-op. However, any
62004 sqlite3BackupRestart(pPager->pBackup);
62009 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62012 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
62015 pList->pDirty = 0;
62019 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
62023 sqlite3PcacheCleanAll(pPager->pPCache);
62026 /* The bBatch boolean is true if the batch-atomic-write commit method
62027 ** should be used. No rollback journal is created if batch-atomic-write
62031 sqlite3_file *fd = pPager->fd;
62034 && !pPager->noSync
62035 && sqlite3JournalIsInMemory(pPager->jfd);
62041 /* The following block updates the change-counter. Exactly how it
62042 ** does this depends on whether or not the atomic-update optimization
62046 ** * The file-system supports the atomic-write property for
62047 ** blocks of size page-size, and
62048 ** * This commit is not part of a multi-file transaction, and
62053 ** counter in 'indirect-mode'. If the optimization is compiled in but
62056 ** pager_incr_changecounter() to update the change-counter in indirect
62060 ** then call pager_incr_changecounter() to update the change-counter
62066 assert( isOpen(pPager->jfd)
62067 || pPager->journalMode==PAGER_JOURNALMODE_OFF
62068 || pPager->journalMode==PAGER_JOURNALMODE_WAL
62070 if( !zSuper && isOpen(pPager->jfd)
62071 && pPager->journalOff==jrnlBufferSize(pPager)
62072 && pPager->dbSize>=pPager->dbOrigSize
62073 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
62075 /* Update the db file change counter via the direct-write method. The
62076 ** following call will modify the in-memory representation of page 1
62078 ** directly to the database file. Because of the atomic-write
62079 ** property of the host file-system, this is safe.
62083 rc = sqlite3JournalCreate(pPager->jfd);
62092 rc = sqlite3JournalCreate(pPager->jfd);
62101 /* Write the super-journal name into the journal file. If a
62102 ** super-journal file name has already been written to the journal file,
62103 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
62109 ** If the atomic-update optimization is being used, this sync will not
62112 ** Because the change-counter page was just modified, unless the
62113 ** atomic-update optimization is used it is almost certain that the
62117 ** xSync() call will be changed to a no-op by the OS anyhow.
62122 pList = sqlite3PcacheDirtyList(pPager->pPCache);
62137 rc = sqlite3JournalCreate(pPager->jfd);
62139 sqlite3OsClose(pPager->jfd);
62144 sqlite3OsClose(pPager->jfd);
62156 sqlite3PcacheCleanAll(pPager->pPCache);
62161 ** last page in the db image moved to the free-list. In this case the
62164 if( pPager->dbSize>pPager->dbFileSize ){
62165 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
62166 assert( pPager->eState==PAGER_WRITER_DBMOD );
62181 pPager->eState = PAGER_WRITER_FINISHED;
62190 ** synced to disk. The journal file still exists in the file-system
62192 ** be used as a hot-journal and the current transaction rolled back.
62196 ** for hot-journal rollback. Once this is done the transaction is
62208 if( NEVER(pPager->errCode) ) return pPager->errCode;
62209 pPager->iDataVersion++;
62211 assert( pPager->eState==PAGER_WRITER_LOCKED
62212 || pPager->eState==PAGER_WRITER_FINISHED
62213 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
62218 ** this transaction, the pager is running in exclusive-mode and is
62219 ** using persistent journals, then this function is a no-op.
62223 ** a hot-journal during hot-journal rollback, 0 changes will be made
62228 if( pPager->eState==PAGER_WRITER_LOCKED
62229 && pPager->exclusiveMode
62230 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
62232 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
62233 pPager->eState = PAGER_READER;
62238 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
62244 ** transaction are reverted and the current write-transaction is closed.
62254 ** in-memory cache pages to the state they were in when the transaction
62263 ** In WAL mode, all cache-entries containing data modified within the
62265 ** their pre-transaction state by re-reading data from the database or
62272 /* PagerRollback() is a no-op if called in READER or OPEN state. If
62277 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
62278 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
62282 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
62283 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
62285 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
62286 int eState = pPager->eState;
62293 pPager->errCode = SQLITE_ABORT;
62294 pPager->eState = PAGER_ERROR;
62302 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
62315 ** Return TRUE if the database file is opened read-only. Return FALSE
62319 return pPager->readOnly;
62327 return sqlite3PcacheRefCount(pPager->pPCache);
62336 int perPageSize = pPager->pageSize + pPager->nExtra
62338 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
62340 + pPager->pageSize;
62356 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
62357 a[1] = sqlite3PcachePagecount(pPager->pPCache);
62358 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
62359 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
62360 a[4] = pPager->eState;
62361 a[5] = pPager->errCode;
62362 a[6] = pPager->aStat[PAGER_STAT_HIT];
62363 a[7] = pPager->aStat[PAGER_STAT_MISS];
62364 a[8] = 0; /* Used to be pPager->nOvfl */
62365 a[9] = pPager->nRead;
62366 a[10] = pPager->aStat[PAGER_STAT_WRITE];
62379 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
62395 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
62396 *pnVal += pPager->aStat[eStat];
62398 pPager->aStat[eStat] = 0;
62403 ** Return true if this is an in-memory or temp-file backed pager.
62406 return pPager->tempFile || pPager->memVfs;
62413 ** equal to nSavepoint, then this function is a no-op.
62416 ** occurs while opening the sub-journal file, then an IO error code is
62421 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
62425 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62427 assert( nSavepoint>nCurrent && pPager->useJournal );
62434 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
62439 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
62440 pPager->aSavepoint = aNew;
62444 aNew[ii].nOrig = pPager->dbSize;
62445 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
62446 aNew[ii].iOffset = pPager->journalOff;
62450 aNew[ii].iSubRec = pPager->nSubRec;
62451 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
62457 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
62459 pPager->nSavepoint = ii+1;
62461 assert( pPager->nSavepoint==nSavepoint );
62466 assert( pPager->eState>=PAGER_WRITER_LOCKED );
62469 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
62489 ** (the first created). A value of (Pager.nSavepoint-1) means operate
62491 ** (Pager.nSavepoint-1), then this function is a no-op.
62508 int rc = pPager->errCode;
62517 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
62526 for(ii=nNew; ii<pPager->nSavepoint; ii++){
62527 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
62529 pPager->nSavepoint = nNew;
62531 /* Truncate the sub-journal so that it only includes the parts
62534 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
62535 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
62536 /* Only truncate if it is an in-memory sub-journal. */
62537 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
62538 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
62539 rc = sqlite3OsTruncate(pPager->sjfd, sz);
62542 pPager->nSubRec = pRel->iSubRec;
62546 ** If this is a temp-file, it is possible that the journal file has
62550 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
62551 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
62562 pPager->journalMode==PAGER_JOURNALMODE_OFF
62563 && pPager->eState>=PAGER_WRITER_CACHEMOD
62565 pPager->errCode = SQLITE_ABORT;
62566 pPager->eState = PAGER_ERROR;
62578 ** Except, if the pager is in-memory only, then return an empty string if
62582 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
62583 ** participate in shared-cache.
62590 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
62597 return pPager->pVfs;
62606 return pPager->fd;
62615 return pPager->jfd;
62617 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
62625 return pPager->zJournal;
62639 if( pPager->xCodecFree ){
62640 pPager->xCodecFree(pPager->pCodec);
62644 pPager->xCodec = pPager->memDb ? 0 : xCodec;
62645 pPager->xCodecSizeChng = xCodecSizeChng;
62646 pPager->xCodecFree = xCodecFree;
62647 pPager->pCodec = pCodec;
62652 return pPager->pCodec;
62664 CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
62680 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
62688 ** If the fourth argument, isCommit, is non-zero, then this page is being
62698 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
62702 assert( pPg->nRef>0 );
62703 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62704 || pPager->eState==PAGER_WRITER_DBMOD
62708 /* In order to be able to rollback, an in-memory database must journal
62711 assert( pPager->tempFile || !MEMDB );
62712 if( pPager->tempFile ){
62719 ** sub-journal now. This is required to handle the following scenario:
62727 ** If page X were not written to the sub-journal here, it would not
62731 ** subjournalPage() may need to allocate space to store pPg->pgno into
62735 if( (pPg->flags & PGHDR_DIRTY)!=0
62742 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
62743 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
62745 /* If the journal needs to be sync()ed before page pPg->pgno can
62746 ** be written to, store pPg->pgno in local variable needSyncPgno.
62749 ** the journal needs to be sync()ed before database page pPg->pgno
62752 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
62753 needSyncPgno = pPg->pgno;
62754 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
62755 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
62756 assert( pPg->flags&PGHDR_DIRTY );
62759 /* If the cache contains a page with page-number pgno, remove it
62764 pPg->flags &= ~PGHDR_NEED_SYNC;
62766 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
62768 if( NEVER(pPgOld->nRef>1) ){
62772 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
62773 if( pPager->tempFile ){
62774 /* Do not discard pages from an in-memory database since we might
62776 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
62782 origPgno = pPg->pgno;
62786 /* For an in-memory database, make sure the original page continues
62790 if( pPager->tempFile && pPgOld ){
62796 /* If needSyncPgno is non-zero, then the journal file needs to be
62798 ** Currently, no such page exists in the page-cache and the
62800 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
62803 ** If the attempt to load the page into the page-cache fails, (due
62813 if( needSyncPgno<=pPager->dbOrigSize ){
62814 assert( pPager->pTmpSpace!=0 );
62815 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
62819 pPgHdr->flags |= PGHDR_NEED_SYNC;
62835 assert( pPg->pgno!=iNew );
62836 pPg->flags = flags;
62844 assert( pPg->nRef>0 || pPg->pPager->memDb );
62845 return pPg->pData;
62853 return pPg->pExtra;
62857 ** Get/set the locking-mode for this pager. Parameter eMode must be one
62860 ** the locking-mode is set to the value specified.
62864 ** locking-mode.
62872 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
62873 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
62874 pPager->exclusiveMode = (u8)eMode;
62876 return (int)pPager->exclusiveMode;
62880 ** Set the journal-mode for this pager. Parameter eMode must be one of:
62892 ** * An in-memory database can only have its journal_mode set to _OFF
62897 ** The returned indicate the current (possibly updated) journal-mode.
62900 u8 eOld = pPager->journalMode; /* Prior journalmode */
62914 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
62916 /* Do allow the journalmode of an in-memory database to be set to
62929 assert( pPager->eState!=PAGER_ERROR );
62930 pPager->journalMode = (u8)eMode;
62943 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
62944 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
62953 sqlite3OsClose(pPager->jfd);
62954 if( pPager->eLock>=RESERVED_LOCK ){
62955 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
62958 int state = pPager->eState;
62963 if( pPager->eState==PAGER_READER ){
62968 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
62975 assert( state==pPager->eState );
62978 sqlite3OsClose(pPager->jfd);
62983 return (int)pPager->journalMode;
62990 return (int)pPager->journalMode;
63000 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
63001 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
63006 ** Get/set the size-limit used for persistent journal files.
63008 ** Setting the size limit to -1 means no limit is enforced.
63009 ** An attempt to set a limit smaller than -1 is a no-op.
63012 if( iLimit>=-1 ){
63013 pPager->journalSizeLimit = iLimit;
63014 sqlite3WalLimit(pPager->pWal, iLimit);
63016 return pPager->journalSizeLimit;
63020 ** Return a pointer to the pPager->pBackup variable. The backup module
63026 return &pPager->pBackup;
63031 ** Unless this is an in-memory or temporary database, clear the pager cache.
63034 assert( MEMDB==0 || pPager->tempFile );
63035 if( pPager->tempFile==0 ) pager_reset(pPager);
63056 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
63068 if( pPager->pWal ){
63069 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
63070 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
63071 pPager->pBusyHandlerArg,
63072 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
63080 return sqlite3WalCallback(pPager->pWal);
63085 ** primitives necessary for write-ahead logging.
63088 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
63089 if( pPager->noLock ) return 0;
63090 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
63100 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63113 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
63114 ** lock on the database file and use heap-memory to store the wal-index
63115 ** in. Otherwise, use the normal shared-memory.
63120 assert( pPager->pWal==0 && pPager->tempFile==0 );
63121 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
63123 /* If the pager is already in exclusive-mode, the WAL module will use
63124 ** heap-memory for the wal-index instead of the VFS shared-memory
63128 if( pPager->exclusiveMode ){
63136 rc = sqlite3WalOpen(pPager->pVfs,
63137 pPager->fd, pPager->zWal, pPager->exclusiveMode,
63138 pPager->journalSizeLimit, &pPager->pWal
63152 ** file (not a temp file or an in-memory database), and the WAL file
63158 ** If the pager is open on a temp-file (or in-memory database), or if
63164 int *pbOpen /* OUT: Set to true if call is a no-op */
63169 assert( pPager->eState==PAGER_OPEN || pbOpen );
63170 assert( pPager->eState==PAGER_READER || !pbOpen );
63172 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
63174 if( !pPager->tempFile && !pPager->pWal ){
63178 sqlite3OsClose(pPager->jfd);
63182 pPager->journalMode = PAGER_JOURNALMODE_WAL;
63183 pPager->eState = PAGER_OPEN;
63204 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
63206 /* If the log file is not already open, but does exist in the file-system,
63210 if( !pPager->pWal ){
63215 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
63224 ** the database file, the log and log-summary files will be deleted.
63226 if( rc==SQLITE_OK && pPager->pWal ){
63229 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
63230 pPager->pageSize, (u8*)pPager->pTmpSpace);
63231 pPager->pWal = 0;
63233 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
63241 ** If pager pPager is a wal-mode database not in exclusive locking mode,
63248 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
63249 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
63260 sqlite3WalDb(pPager->pWal, db);
63272 if( pPager->pWal ){
63273 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
63288 if( pPager->pWal ){
63289 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
63302 if( pPager->pWal ){
63303 rc = sqlite3WalSnapshotRecover(pPager->pWal);
63324 if( pPager->pWal ){
63325 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
63337 assert( pPager->pWal );
63338 sqlite3WalSnapshotUnlock(pPager->pWal);
63346 ** A read-lock must be held on the pager when this function is called. If
63353 assert( pPager->eState>=PAGER_READER );
63354 return sqlite3WalFramesize(pPager->pWal);
63374 ** This file contains the implementation of a write-ahead log (WAL) used in
63377 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
63396 ** big-endian 32-bit unsigned integer values:
63402 ** 16: Salt-1, random integer incremented with each checkpoint
63403 ** 20: Salt-2, a different random integer changing with each ckpt
63404 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
63405 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
63407 ** Immediately following the wal-header are zero or more frames. Each
63408 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
63409 ** of page data. The frame-header is six big-endian 32-bit unsigned
63415 ** 8: Salt-1 (copied from the header)
63416 ** 12: Salt-2 (copied from the header)
63417 ** 16: Checksum-1.
63418 ** 20: Checksum-2.
63423 ** (1) The salt-1 and salt-2 values in the frame-header match
63424 ** salt values in the wal-header
63426 ** (2) The checksum values in the final 8 bytes of the frame-header
63431 ** The checksum is computed using 32-bit big-endian integers if the
63433 ** is computed using little-endian if the magic number is 0x377f0682.
63435 ** big-endian format regardless of which byte order is used to compute
63437 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
63440 ** for i from 0 to n-1 step 2:
63447 ** of the sequence being summed.) The s1 value spans all 32-bit
63450 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
63451 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
63452 ** The VFS.xSync operations serve as write barriers - all writes launched
63456 ** After each checkpoint, the salt-1 value is incremented and the salt-2
63485 ** data structure called the wal-index is maintained to expedite the
63488 ** WAL-INDEX FORMAT
63490 ** Conceptually, the wal-index is shared memory, though VFS implementations
63491 ** might choose to implement the wal-index using a mmapped file. Because
63492 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
63496 ** In the default unix and windows implementation, the wal-index is a mmapped
63497 ** file whose name is the database name with a "-shm" suffix added. For that
63498 ** reason, the wal-index is sometimes called the "shm" file.
63500 ** The wal-index is transient. After a crash, the wal-index can (and should
63502 ** to either truncate or zero the header of the wal-index when the last
63503 ** connection to it closes. Because the wal-index is transient, it can
63504 ** use an architecture-specific format; it does not have to be cross-platform.
63506 ** as big endian, the wal-index can store multi-byte values in the native
63509 ** The purpose of the wal-index is to answer this question quickly: Given
63514 ** The wal-index consists of a header region, followed by an one or
63517 ** The wal-index header contains the total number of frames within the WAL
63523 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
63525 ** wal-index. The values are:
63530 ** Each index block contains two sections, a page-mapping that contains the
63531 ** database page number associated with each wal frame, and a hash-table
63533 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
63534 ** for the first index block) 32-bit page numbers. The first entry in the
63535 ** first index-block contains the database page number corresponding to the
63540 ** The last index block in a wal-index usually contains less than the full
63541 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
63543 ** allocated size of the page-mapping array - the page-mapping array merely
63547 ** can be found by scanning the page-mapping sections of each index block
63553 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
63558 ** 1-based index of an entry in the mapping section of the same
63559 ** index block. Let K be the 1-based index of the largest entry in
63574 ** wrap-around.) Because the hash table is never more than half full,
63579 ** current index block. Otherwise the iMax-th mapping entry of the
63597 ** and to the wal-index) might be using a different value K1, where K1>K0.
63603 ** in the first place - which is what reader one wants. Meanwhile, the
63626 ** The maximum (and only) versions of the wal and wal-index formats
63630 ** values in the wal-header are correct and (b) the version field is not
63633 ** Similarly, if a client successfully reads a wal-index header (i.e. the
63635 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
63650 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
63660 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
63670 ** The following object holds a copy of the wal-index header content.
63672 ** The actual header in the wal-index consists of two copies of this
63679 ** Or it can be 1 to represent a 65536-byte page. The latter case was
63683 u32 iVersion; /* Wal-index version */
63687 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
63697 ** A copy of the following object occurs in the wal-index immediately
63720 ** holds read-lock K, then the value in aReadMark[K] is no greater than
63723 ** a special case; its value is never used and it exists as a place-holder
63752 ** We assume that 32-bit loads are atomic and so no locks are needed in
63765 ** This is a schematic view of the complete 136-byte header of the
63766 ** wal-index file (also known as the -shm file):
63768 ** +-----------------------------+
63770 ** +-----------------------------+ |
63772 ** +-----------------------------+ |
63774 ** +-------+-------+-------------+ |
63776 ** +-------+-------+-------------+ |
63778 ** +-----------------------------+ | WalIndexHdr object
63780 ** +-----------------------------+ |
63783 ** +-----------------------------+ |
63786 ** +-----------------------------+ |
63789 ** +-----------------------------+
63791 ** +-----------------------------+ |
63793 ** +-----------------------------+ |
63795 ** +-------+-------+-------------+ |
63797 ** +-------+-------+-------------+ | Second copy of the
63799 ** +-----------------------------+ |
63801 ** +-----------------------------+ |
63804 ** +-----------------------------+ |
63807 ** +-----------------------------+ |
63810 ** +-----------------------------+
63812 ** +-----------------------------+
63818 ** +-------+-------+------+------+
63820 ** +-------+-------+------+------+ ) 8 lock bytes
63822 ** +-------+-------+------+------+
63824 ** +-----------------------------+
63826 ** +-----------------------------+
63831 ** only support mandatory file-locks, we do not read or write data
63844 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
63845 ** big-endian format in the first 4 bytes of a WAL file.
63848 ** file are calculated by treating all data as an array of 32-bit
63849 ** big-endian words. Otherwise, they are calculated by interpreting
63850 ** all data as 32-bit little-endian words.
63855 ** Return the offset of frame iFrame in the write-ahead log file,
63857 ** is to the start of the write-ahead log frame-header.
63860 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
63864 ** An open write-ahead log file is represented by an instance of the
63875 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
63877 i16 readLock; /* Which read lock is being held. -1 for none */
63879 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
63886 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
63887 WalIndexHdr hdr; /* Wal-index header for current transaction */
63891 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
63918 ** Each page of the wal-index mapping contains a hash-table made up of
63932 ** walIteratorInit() - Create a new iterator,
63933 ** walIteratorNext() - Step an iterator,
63934 ** walIteratorFree() - Free an iterator.
63947 } aSegment[1]; /* One for every 32KB page in the wal-index */
63951 ** Define the parameters of the hash tables in the wal-index file. There
63952 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
63953 ** wal-index.
63955 ** Changing any of these constants will alter the wal-index format and
63963 ** The block of page numbers associated with the first hash-table in a
63964 ** wal-index is smaller than usual. This is so that there is a complete
63965 ** hash-table on each aligned 32KB page of the wal-index.
63967 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
63969 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
63975 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
63976 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
63979 ** If the wal-index is currently smaller the iPage pages then the size
63980 ** of the wal-index might be increased, but only if it is safe to do
63981 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
63982 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
63986 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
63990 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
63999 /* Enlarge the pWal->apWiData[] array if required */
64000 if( pWal->nWiData<=iPage ){
64003 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
64008 memset((void*)&apNew[pWal->nWiData], 0,
64009 sizeof(u32*)*(iPage+1-pWal->nWiData));
64010 pWal->apWiData = apNew;
64011 pWal->nWiData = iPage+1;
64015 assert( pWal->apWiData[iPage]==0 );
64016 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
64017 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
64018 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
64020 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
64021 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
64023 assert( pWal->apWiData[iPage]!=0
64025 || (pWal->writeLock==0 && iPage==0) );
64026 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
64030 pWal->readOnly |= WAL_SHM_RDONLY;
64037 *ppPage = pWal->apWiData[iPage];
64046 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
64053 ** Return a pointer to the WalCkptInfo structure in the wal-index.
64056 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64057 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
64061 ** Return a pointer to the WalIndexHdr structure in the wal-index.
64064 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64065 return (volatile WalIndexHdr*)pWal->apWiData[0];
64069 ** The argument to this macro must be of type u32. On a little-endian
64071 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
64073 ** of the input value as a little-endian integer.
64090 int nativeCksum, /* True for native byte-order, false for non-native */
64133 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64134 sqlite3OsShmBarrier(pWal->pDbFd);
64139 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
64141 ** might give false-positive TSAN warnings.
64143 ** See tag-20200519-1.
64152 ** Write the header information in pWal->hdr into the wal-index.
64154 ** The checksum on pWal->hdr is updated before it is written.
64160 assert( pWal->writeLock );
64161 pWal->hdr.isInit = 1;
64162 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
64163 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
64164 /* Possible TSAN false-positive. See tag-20200519-1 */
64165 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64167 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
64172 ** supplied by the caller. A frame-header is made up of a series of
64173 ** 4-byte big-endian integers, as follows:
64178 ** 8: Salt-1 (copied from the wal-header)
64179 ** 12: Salt-2 (copied from the wal-header)
64180 ** 16: Checksum-1.
64181 ** 20: Checksum-2.
64184 Wal *pWal, /* The write-ahead log */
64186 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
64190 int nativeCksum; /* True for native byte-order checksums */
64191 u32 *aCksum = pWal->hdr.aFrameCksum;
64195 if( pWal->iReCksum==0 ){
64196 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
64198 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64200 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64215 Wal *pWal, /* The write-ahead log */
64221 int nativeCksum; /* True for native byte-order checksums */
64222 u32 *aCksum = pWal->hdr.aFrameCksum;
64226 /* A frame is only valid if the salt values in the frame-header
64227 ** match the salt values in the wal-header.
64229 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
64241 ** all prior frams, the first 16 bytes of this frame-header,
64242 ** and the frame-data matches the checksum in the last 8
64243 ** bytes of this frame-header.
64245 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
64247 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
64271 return "WRITE-LOCK";
64273 return "CKPT-LOCK";
64275 return "RECOVER-LOCK";
64278 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
64279 lockIdx-WAL_READ_LOCK(0));
64288 ** A lock cannot be moved directly between shared and exclusive - it must go
64291 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
64295 if( pWal->exclusiveMode ) return SQLITE_OK;
64296 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64298 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
64300 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64304 if( pWal->exclusiveMode ) return;
64305 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
64307 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
64311 if( pWal->exclusiveMode ) return SQLITE_OK;
64312 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64314 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
64316 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
64320 if( pWal->exclusiveMode ) return;
64321 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
64323 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
64329 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
64334 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
64335 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
64338 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
64343 ** of a page hash table in the wal-index. This becomes the return value
64348 volatile ht_slot *aHash; /* Start of the wal-index hash table */
64355 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
64358 ** Set output variable pLoc->aHash to point to the start of the hash table
64359 ** in the wal-index file. Set pLoc->iZero to one less than the frame
64362 ** (pLoc->iZero+N) in the log.
64364 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
64365 ** first frame indexed by the hash table, frame (pLoc->iZero).
64374 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
64377 if( pLoc->aPgno ){
64378 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
64380 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
64381 pLoc->iZero = 0;
64383 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
64392 ** Return the number of the wal-index page that contains the hash-table
64393 ** and page-number array that contain entries corresponding to WAL frame
64394 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
64398 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
64415 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
64417 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
64422 ** than pWal->hdr.mxFrame.
64424 ** This function is called whenever pWal->hdr.mxFrame is decreased due
64427 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
64429 ** pWal->hdr.mxFrame advances to the point where those hash tables are
64438 assert( pWal->writeLock );
64439 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
64440 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
64441 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
64443 if( pWal->hdr.mxFrame==0 ) return;
64445 /* Obtain pointers to the hash-table and page-number array containing
64446 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
64447 ** that the page said hash-table and array reside on is already mapped.(1)
64449 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
64450 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
64451 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
64452 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
64454 /* Zero all hash-table entries that correspond to frame numbers greater
64455 ** than pWal->hdr.mxFrame.
64457 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
64466 ** frame numbers greater than pWal->hdr.mxFrame.
64468 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
64491 ** Set an entry in the wal-index that will map database page number
64496 WalHashLoc sLoc; /* Wal-index hash table location */
64500 /* Assuming the wal-index file was successfully mapped, populate the
64505 int idx; /* Value to write to hash-table slot */
64508 idx = iFrame - sLoc.iZero;
64511 /* If this is the first entry to be added to this hash-table, zero the
64515 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
64524 ** the hash-table before writing any new entries.
64526 if( sLoc.aPgno[idx-1] ){
64528 assert( !sLoc.aPgno[idx-1] );
64531 /* Write the aPgno[] array entry and the hash-table slot. */
64534 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
64536 sLoc.aPgno[idx-1] = iPage;
64552 ** thing to check, so only do this occasionally - not on every
64574 ** Recover the wal-index by reading the write-ahead log file.
64577 ** wal-index to prevent other threads/processes from doing anything
64578 ** with the WAL or wal-index while recovery is running. The
64595 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
64598 assert( pWal->writeLock );
64599 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
64600 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64607 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
64609 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
64616 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
64624 u32 iPg; /* Current 32KB wal-index page */
64628 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
64641 || szPage&(szPage-1)
64647 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
64648 pWal->szPage = szPage;
64649 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
64650 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
64653 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
64654 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
64656 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
64657 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
64681 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
64686 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
64691 pWal->apWiData[iPg] = aPrivate;
64699 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64706 /* If nTruncate is non-zero, this is a commit record. */
64708 pWal->hdr.mxFrame = iFrame;
64709 pWal->hdr.nPage = nTruncate;
64710 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
64713 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
64714 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
64717 pWal->apWiData[iPg] = aShare;
64729 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
64758 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
64759 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
64762 /* Reset the checkpoint-header. This is safe because this thread is
64764 ** checkpointers. Then set the values of read-mark slots 1 through N.
64767 pInfo->nBackfill = 0;
64768 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
64769 pInfo->aReadMark[0] = 0;
64773 if( i==1 && pWal->hdr.mxFrame ){
64774 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
64776 pInfo->aReadMark[i] = READMARK_NOT_USED;
64789 if( pWal->hdr.nPage ){
64792 pWal->hdr.mxFrame, pWal->zWalName
64799 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
64804 ** Close an open wal-index.
64807 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
64809 for(i=0; i<pWal->nWiData; i++){
64810 sqlite3_free((void *)pWal->apWiData[i]);
64811 pWal->apWiData[i] = 0;
64814 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
64815 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
64826 ** client from unlinking the WAL or wal-index file. If another process
64835 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
64838 int bNoShm, /* True to run in heap-memory mode */
64850 ** of these constants would result in an incompatible on-disk format
64851 ** for the -shm file. Any change that causes one of these asserts to
64855 ** This table also serves as a helpful cross-reference when trying to
64856 ** interpret hex dumps of the -shm file.
64896 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
64901 pRet->pVfs = pVfs;
64902 pRet->pWalFd = (sqlite3_file *)&pRet[1];
64903 pRet->pDbFd = pDbFd;
64904 pRet->readLock = -1;
64905 pRet->mxWalSize = mxWalSize;
64906 pRet->zWalName = zWalName;
64907 pRet->syncHeader = 1;
64908 pRet->padToSectorBoundary = 1;
64909 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
64911 /* Open file handle on the write-ahead log file. */
64913 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
64915 pRet->readOnly = WAL_RDONLY;
64920 sqlite3OsClose(pRet->pWalFd);
64924 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
64926 pRet->padToSectorBoundary = 0;
64938 if( pWal ) pWal->mxWalSize = iLimit;
64960 iMin = p->iPrior;
64962 for(i=p->nSegment-1; i>=0; i--){
64963 struct WalSegment *pSegment = &p->aSegment[i];
64964 while( pSegment->iNext<pSegment->nEntry ){
64965 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
64969 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
64973 pSegment->iNext++;
64977 *piPage = p->iPrior = iRet;
65005 const u32 *aContent, /* Pages in wal - keys for the sort */
65069 ht_slot *aList; /* Pointer to sub-list content */
65077 struct Sublist aSub[13]; /* Array of sub-lists */
65081 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
65090 assert( p->aList && p->nList<=(1<<iSub) );
65091 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
65092 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65103 assert( p->nList<=(1<<iSub) );
65104 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
65105 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
65115 assert( aContent[aList[i]] > aContent[aList[i-1]] );
65131 ** nBackfill or earlier may be included - excluding them is an optimization
65147 ht_slot *aTmp; /* Temp space used by merge-sort */
65153 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
65154 iLast = pWal->hdr.mxFrame;
65159 + (nSegment-1)*sizeof(struct WalSegment)
65166 p->nSegment = nSegment;
65168 /* Allocate temporary space used by the merge-sort routine. This block
65188 nEntry = (int)(iLast - sLoc.iZero);
65190 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
65192 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
65199 p->aSegment[i].iZero = sLoc.iZero;
65200 p->aSegment[i].nEntry = nEntry;
65201 p->aSegment[i].aIndex = aIndex;
65202 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
65219 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
65224 if( pWal->db ){
65225 int tmout = pWal->db->busyTimeout;
65229 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
65242 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
65256 assert( pWal->readLock<0 || bLock==0 );
65258 assert( pWal->db );
65262 pWal->writeLock = 1;
65266 }else if( pWal->writeLock ){
65268 pWal->writeLock = 0;
65277 pWal->db = db;
65301 ** busy-handler function. Invoke it and retry the lock until either the
65302 ** lock is successfully obtained or the busy-handler returns 0.
65325 ** The cache of the wal-index header must be valid to call this function.
65326 ** Return the page-size in bytes used by the database.
65329 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65338 ** file - there are no readers that may attempt to read a frame from
65341 ** This function updates the shared-memory structures so that the next
65346 ** new wal-index header. It should be passed a pseudo-random value (i.e.
65352 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
65353 pWal->nCkpt++;
65354 pWal->hdr.mxFrame = 0;
65356 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
65358 AtomicStore(&pInfo->nBackfill, 0);
65359 pInfo->nBackfillAttempted = 0;
65360 pInfo->aReadMark[1] = 0;
65361 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
65362 assert( pInfo->aReadMark[0]==0 );
65376 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
65382 ** in the WAL and can be recovered following a power-loss or hard reset.
65389 ** This routine uses and updates the nBackfill field of the wal-index header.
65408 int szPage; /* Database page-size */
65421 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65423 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65432 mxSafeFrame = pWal->hdr.mxFrame;
65433 mxPage = pWal->hdr.nPage;
65435 u32 y = AtomicLoad(pInfo->aReadMark+i);
65437 assert( y<=pWal->hdr.mxFrame );
65441 AtomicStore(pInfo->aReadMark+i, iMark);
65453 if( pInfo->nBackfill<mxSafeFrame ){
65454 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
65461 u32 nBackfill = pInfo->nBackfill;
65463 pInfo->nBackfillAttempted = mxSafeFrame;
65466 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
65474 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
65475 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
65477 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
65480 ** maximum size of the pending-byte page (65536 bytes), then
65484 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
65494 if( AtomicLoad(&db->u1.isInterrupted) ){
65495 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
65503 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
65505 iOffset = (iDbpage-1)*(i64)szPage;
65509 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
65512 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
65516 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
65517 i64 szDb = pWal->hdr.nPage*(i64)szPage;
65519 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
65521 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
65525 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
65546 assert( pWal->writeLock );
65547 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
65552 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
65553 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
65556 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
65562 ** wal-index header in shared memory, as all subsequent reader or
65566 ** the wal-index header do not match the contents of the
65567 ** file-system. To avoid this, update the wal-index header to
65570 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
65572 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65590 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
65592 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
65596 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
65612 int isDelete = 0; /* True to unlink wal and wal-index files */
65615 ** ordinary, rollback-mode locking methods, this guarantees that the
65618 ** the wal and wal-index files.
65623 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
65625 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
65626 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65632 int bPersist = -1;
65634 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
65638 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
65641 }else if( pWal->mxWalSize>=0 ){
65645 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
65654 sqlite3OsClose(pWal->pWalFd);
65657 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
65661 sqlite3_free((void *)pWal->apWiData);
65668 ** Try to read the wal-index header. Return 0 on success and 1 if
65671 ** The wal-index is in shared memory. Another thread or process might
65678 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
65681 ** If the checksum cannot be verified return non-zero. If the header
65689 /* The first page of the wal-index must be mapped at this point. */
65690 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65695 ** from the file. If this happens, return non-zero.
65697 ** tag-20200519-1:
65698 ** There are two copies of the header at the beginning of the wal-index.
65702 ** give false-positive warnings about these accesses because the tools do not
65703 ** account for the double-read and the memory barrier. The use of mutexes
65709 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
65717 return 1; /* Malformed header - probably all zeros */
65719 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
65724 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
65726 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
65727 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
65728 testcase( pWal->szPage<=32768 );
65729 testcase( pWal->szPage>=65536 );
65740 #define WAL_RETRY (-1)
65743 ** Read the wal-index header from the wal-index and into pWal->hdr.
65744 ** If the wal-header appears to be corrupt, try to reconstruct the
65745 ** wal-index from the WAL before returning.
65747 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
65748 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
65751 ** If the wal-index header is successfully read, return SQLITE_OK.
65757 volatile u32 *page0; /* Chunk of wal-index containing header */
65759 /* Ensure that page 0 of the wal-index (the page that contains the
65760 ** wal-index header) is mapped. Return early if an error occurs here.
65767 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
65769 ** confirm that another write-capable connection has the shared-memory
65770 ** open, and hence the content of the shared-memory is unreliable,
65771 ** since the shared-memory might be inconsistent with the WAL file
65774 assert( pWal->writeLock==0 );
65775 assert( pWal->readOnly & WAL_SHM_RDONLY );
65776 pWal->bShmUnreliable = 1;
65777 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
65780 return rc; /* Any other non-OK return is just an error */
65783 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
65787 assert( page0!=0 || pWal->writeLock==0 );
65789 /* If the first page of the wal-index has been mapped, try to read the
65790 ** wal-index header immediately, without holding any lock. This usually
65791 ** works, but may fail if the wal-index header is corrupt or currently
65800 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
65806 int bWriteLock = pWal->writeLock;
65808 pWal->writeLock = 1;
65812 /* If the wal-index header is still malformed even while holding
65821 pWal->writeLock = 0;
65829 ** sure the wal-index was not constructed with some future format that
65832 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
65835 if( pWal->bShmUnreliable ){
65838 pWal->bShmUnreliable = 0;
65839 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
65845 pWal->exclusiveMode = WAL_NORMAL_MODE;
65852 ** Open a transaction in a connection where the shared-memory is read-only
65853 ** and where we cannot verify that there is a separate write-capable connection
65854 ** on hand to keep the shared-memory up-to-date with the WAL file.
65856 ** This can happen, for example, when the shared-memory is implemented by
65857 ** memory-mapping a *-shm file, where a prior writer has shut down and
65858 ** left the *-shm file on disk, and now the present connection is trying
65859 ** to use that database but lacks write permission on the *-shm file.
65864 ** The *-wal file has been read and an appropriate wal-index has been
65865 ** constructed in pWal->apWiData[] using heap memory instead of shared
65872 ** WAL_RETRY, then the heap memory wal-index has been discarded and
65874 ** beginning (including attempting to map the *-shm file).
65887 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
65889 assert( pWal->bShmUnreliable );
65890 assert( pWal->readOnly & WAL_SHM_RDONLY );
65891 assert( pWal->nWiData>0 && pWal->apWiData[0] );
65901 pWal->readLock = 0;
65903 /* Check to see if a separate writer has attached to the shared-memory area,
65904 ** thus making the shared-memory "reliable" again. Do this by invoking
65908 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
65909 ** cause the heap-memory WAL-index to be discarded and the actual
65921 ** even if some external agent does a "chmod" to make the shared-memory
65925 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
65926 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
65932 /* We reach this point only if the real shared-memory is still unreliable.
65933 ** Assume the in-memory WAL-index substitute is correct and load it
65934 ** into pWal->hdr.
65936 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
65941 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
65946 /* If the wal file is too small to contain a wal-header and the
65947 ** wal-index header has mxFrame==0, then it must be safe to proceed
65953 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
65958 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
65962 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
65964 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
65971 assert( (pWal->szPage & (pWal->szPage-1))==0 );
65972 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
65973 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
65982 ** wal file since the heap-memory wal-index was created. If so, the
65983 ** heap-memory wal-index is discarded and WAL_RETRY returned to
65985 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
65986 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
65987 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
65995 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
65999 /* If nTruncate is non-zero, then a complete transaction has been
66007 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
66008 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
66014 for(i=0; i<pWal->nWiData; i++){
66015 sqlite3_free((void*)pWal->apWiData[i]);
66016 pWal->apWiData[i] = 0;
66018 pWal->bShmUnreliable = 0;
66037 ** to make a copy of the wal-index header into pWal->hdr. If the
66038 ** wal-index header has changed, *pChanged is set to 1 (as an indication
66040 ** flushed.) When useWal==1, the wal-index header is assumed to already
66051 ** bad luck when there is lots of contention for the wal-index, but that
66055 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
66056 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
66059 ** including frame number aReadMark[pWal->readLock]. The reader will
66060 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
66061 ** Or if pWal->readLock==0, then the reader will ignore the WAL
66064 ** this routine will always set pWal->readLock>0 on success.
66066 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
66077 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
66084 assert( pWal->readLock<0 ); /* Not currently locked */
66087 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
66095 ** paged out or take a page-fault that is time-consuming to resolve,
66109 VVA_ONLY( pWal->lockError = 1; )
66112 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
66116 sqlite3OsSleep(pWal->pVfs, nDelay);
66121 if( pWal->bShmUnreliable==0 ){
66133 if( pWal->apWiData[0]==0 ){
66138 ** code that determines whether or not the shared-memory region
66152 else if( pWal->bShmUnreliable ){
66157 assert( pWal->nWiData>0 );
66158 assert( pWal->apWiData[0]!=0 );
66160 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
66162 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
66171 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
66188 pWal->readLock = 0;
66198 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
66202 mxFrame = pWal->hdr.mxFrame;
66204 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
66205 mxFrame = pWal->pSnapshot->mxFrame;
66209 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
66216 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
66222 AtomicStore(pInfo->aReadMark+i,mxFrame);
66233 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
66241 /* Now that the read-lock has been obtained, check that neither the
66242 ** value in the aReadMark[] array or the contents of the wal-index
66245 ** It is necessary to check that the wal-index header did not change
66246 ** between the time it was read and when the shared-lock was obtained
66249 ** that occur later in the log than pWal->hdr.mxFrame may have been
66252 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
66255 ** Before checking that the live wal-index header has not changed
66258 ** to read any frames earlier than minFrame from the wal file - they
66262 ** nBackfill and checking that the wal-header in shared-memory still
66263 ** matches the one cached in pWal->hdr, it is guaranteed that the
66264 ** checkpointer that set nBackfill was not working with a wal-index
66265 ** header newer than that cached in pWal->hdr. If it were, that could
66267 ** a version of page X that lies before pWal->minFrame (call that version
66270 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
66273 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
66275 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
66277 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
66278 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
66283 assert( mxReadMark<=pWal->hdr.mxFrame );
66284 pWal->readLock = (i16)mxI;
66298 ** This is only really safe if the file-system is such that any page
66312 assert( pWal->readLock>=0 );
66316 int szPage = (int)pWal->szPage;
66319 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
66326 u32 i = pInfo->nBackfillAttempted;
66327 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
66335 assert( i - sLoc.iZero - 1 >=0 );
66336 pgno = sLoc.aPgno[i-sLoc.iZero-1];
66337 iDbOff = (i64)(pgno-1) * szPage;
66341 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
66344 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
66352 pInfo->nBackfillAttempted = i-1;
66370 ** it takes a snapshot of the state of the WAL and wal-index for the current
66372 ** Other threads might append new content to the WAL and wal-index but
66385 WalIndexHdr *pSnapshot = pWal->pSnapshot;
66388 assert( pWal->ckptLock==0 );
66392 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66400 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
66403 ** before checking pInfo->nBackfillAttempted. */
66411 pWal->ckptLock = 1;
66425 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
66427 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
66428 ** is populated with the wal-index header corresponding to the head
66436 ** pSnapshot->mxFrame into the database file. Note that the
66441 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
66442 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
66446 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
66447 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
66450 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
66451 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
66453 assert( pWal->readLock>0 );
66454 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
66460 /* A client using a non-current snapshot may not ignore any frames
66466 pWal->minFrame = 1;
66475 if( pWal->ckptLock ){
66478 pWal->ckptLock = 0;
66486 ** read-lock.
66490 if( pWal->readLock>=0 ){
66491 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
66492 pWal->readLock = -1;
66510 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
66515 assert( pWal->readLock>=0 || pWal->lockError );
66517 /* If the "last page" field of the wal-index header snapshot is 0, then
66519 ** in this case as an optimization. Likewise, if pWal->readLock==0,
66523 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
66533 ** that adds entries to the wal-index (and possibly to this hash
66537 ** read transaction was opened may have been written incorrectly -
66544 ** if we had exclusive access to the hash-table:
66547 ** This condition filters out normal hash-table collisions.
66551 ** table after the current read-transaction had started.
66553 iMinHash = walFramePage(pWal->minFrame);
66554 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
66569 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
66573 if( (nCollide--)==0 ){
66583 ** of the wal-index file content. Make sure the results agree with the
66588 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
66589 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
66616 sz = pWal->hdr.szPage;
66622 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
66629 if( pWal && ALWAYS(pWal->readLock>=0) ){
66630 return pWal->hdr.nPage;
66653 /* If the write-lock is already held, then it was obtained before the
66654 ** read-transaction was even opened, making this call a no-op.
66656 if( pWal->writeLock ){
66657 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
66664 assert( pWal->readLock>=0 );
66665 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
66667 if( pWal->readOnly ){
66678 pWal->writeLock = 1;
66684 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
66686 pWal->writeLock = 0;
66698 if( pWal->writeLock ){
66700 pWal->writeLock = 0;
66701 pWal->iReCksum = 0;
66702 pWal->truncateOnCommit = 0;
66709 ** function moves the write-pointer back to the start of the transaction.
66721 if( ALWAYS(pWal->writeLock) ){
66722 Pgno iMax = pWal->hdr.mxFrame;
66725 /* Restore the clients cache of the wal-index header to the state it
66728 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
66730 for(iFrame=pWal->hdr.mxFrame+1;
66736 ** (b) has an outstanding reference, then xUndo is either a no-op
66748 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
66760 assert( pWal->writeLock );
66761 aWalData[0] = pWal->hdr.mxFrame;
66762 aWalData[1] = pWal->hdr.aFrameCksum[0];
66763 aWalData[2] = pWal->hdr.aFrameCksum[1];
66764 aWalData[3] = pWal->nCkpt;
66776 assert( pWal->writeLock );
66777 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
66779 if( aWalData[3]!=pWal->nCkpt ){
66780 /* This savepoint was opened immediately after the write-transaction
66785 aWalData[3] = pWal->nCkpt;
66788 if( aWalData[0]<pWal->hdr.mxFrame ){
66789 pWal->hdr.mxFrame = aWalData[0];
66790 pWal->hdr.aFrameCksum[0] = aWalData[1];
66791 pWal->hdr.aFrameCksum[1] = aWalData[2];
66803 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
66807 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
66814 if( pWal->readLock==0 ){
66816 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
66817 if( pInfo->nBackfill>0 ){
66820 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66825 ** wal-index header to reflect this.
66828 ** at this point. But updating the actual wal-index header is also
66832 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
66838 pWal->readLock = -1;
66854 ** the next fsync should occur - passed from sqlite3WalFrames() into
66867 ** Do a sync when crossing the p->iSyncPoint boundary.
66880 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
66881 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
66882 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
66885 iAmt -= iFirstAmt;
66887 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
66888 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
66891 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
66906 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
66907 if( pPage->pgno==1 ){
66908 rc = checkHeaderValid(pPage->pPager, pPage->pData, "walWrite");
66914 pData = pPage->pData;
66916 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
66920 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
66933 const int szPage = pWal->szPage;/* Database page size */
66936 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
66946 ** wal-file header. Otherwise, read them from the frame header of the
66948 assert( pWal->iReCksum>0 );
66949 if( pWal->iReCksum==1 ){
66952 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
66954 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
66955 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
66956 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
66958 iRead = pWal->iReCksum;
66959 pWal->iReCksum = 0;
66962 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
66969 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
66978 ** Write a set of frames to the log. The caller must hold the write-lock
66983 int szPage, /* Database page-size in bytes */
67001 assert( pWal->writeLock );
67008 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
67010 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
67015 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
67016 iFirst = pLive->mxFrame+1;
67020 ** log file, instead of appending to it at pWal->hdr.mxFrame.
67030 iFrame = pWal->hdr.mxFrame;
67032 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
67033 u32 aCksum[2]; /* Checksum for wal-header */
67038 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
67039 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
67040 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
67041 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
67045 pWal->szPage = szPage;
67046 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
67047 pWal->hdr.aFrameCksum[0] = aCksum[0];
67048 pWal->hdr.aFrameCksum[1] = aCksum[1];
67049 pWal->truncateOnCommit = 1;
67051 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
67052 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
67059 ** an out-of-order write following a WAL restart could result in
67064 if( pWal->syncHeader ){
67065 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
67069 assert( (int)pWal->szPage==szPage );
67073 w.pFd = pWal->pWalFd;
67081 for(p=pList; p; p=p->pDirty){
67086 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
67088 if( iFirst && (p->pDirty || isCommit==0) ){
67090 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
67095 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
67096 pWal->iReCksum = iWrite;
67101 pData = p->pData;
67103 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
67105 p->flags &= ~PGHDR_WAL_APPEND;
67112 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
67117 p->flags |= PGHDR_WAL_APPEND;
67121 if( isCommit && pWal->iReCksum ){
67142 if( pWal->padToSectorBoundary ){
67143 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
67144 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
67165 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
67166 i64 sz = pWal->mxWalSize;
67167 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
67171 pWal->truncateOnCommit = 0;
67174 /* Append data to the wal-index. It is not necessary to lock the
67175 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
67179 iFrame = pWal->hdr.mxFrame;
67180 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
67181 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
67183 rc = walIndexAppend(pWal, iFrame, p->pgno);
67188 nExtra--;
67189 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
67194 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
67197 pWal->hdr.mxFrame = iFrame;
67199 pWal->hdr.iChange++;
67200 pWal->hdr.nPage = nTruncate;
67202 /* If this is a commit, update the wal-index header too. */
67205 pWal->iCallback = iFrame;
67222 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
67238 int isChanged = 0; /* True if a new wal-index header is loaded */
67242 assert( pWal->ckptLock==0 );
67243 assert( pWal->writeLock==0 );
67245 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
67249 if( pWal->readOnly ) return SQLITE_READONLY;
67253 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
67257 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
67259 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
67262 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
67269 pWal->ckptLock = 1;
67271 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
67275 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
67276 ** immediately, and a busy-handler is configured, it is invoked and the
67277 ** writer lock retried until either the busy-handler returns 0 or the
67283 pWal->writeLock = 1;
67293 /* Read the wal-index header. */
67298 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
67299 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
67306 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
67314 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
67315 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
67320 /* If a new wal-index header was loaded before the checkpoint was
67321 ** performed, then the pager-cache associated with pWal is now
67322 ** out of date. So zero the cached wal-index header to ensure that
67326 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
67334 if( pWal->ckptLock ){
67336 pWal->ckptLock = 0;
67342 sqlite3OsCurrentTimeInt64(db->pVfs, &g_lastCkptTime);
67359 ret = pWal->iCallback;
67360 pWal->iCallback = 0;
67371 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
67373 ** transition out of exclusive-mode is successful, return 1. This
67378 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
67380 ** WAL is already in exclusive-locking mode - meaning that this
67381 ** routine is a no-op. The pager must already hold the exclusive lock
67384 ** If op is negative, then do a dry-run of the op==1 case but do
67391 assert( pWal->writeLock==0 );
67392 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
67394 /* pWal->readLock is usually set, but might be -1 if there was a
67395 ** prior error while attempting to acquire are read-lock. This cannot
67398 ** upgrade to exclusive-mode following such an error.
67400 assert( pWal->readLock>=0 || pWal->lockError );
67401 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
67404 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
67405 pWal->exclusiveMode = WAL_NORMAL_MODE;
67406 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
67407 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67409 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67415 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
67416 assert( pWal->readLock>=0 );
67417 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
67418 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
67421 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
67427 ** Return true if the argument is non-NULL and the WAL module is using
67428 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
67429 ** WAL module is using shared-memory, return false.
67432 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
67445 assert( pWal->readLock>=0 && pWal->writeLock==0 );
67447 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
67455 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
67462 /* Try to open on pSnapshot when the next read-transaction starts
67468 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
67472 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
67481 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
67482 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
67483 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
67484 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
67504 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
67505 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
67529 ** read-lock. This function returns the database page-size if it is known,
67533 assert( pWal==0 || pWal->readLock>=0 );
67534 return (pWal ? pWal->szPage : 0);
67541 return pWal->pWalFd;
67578 ** This file implements an external (disk-based) database using BTrees.
67582 ** "Sorting And Searching", pages 473-480. Addison-Wesley
67588 ** ----------------------------------------------------------------
67589 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
67590 ** ----------------------------------------------------------------
67595 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
67617 ** page, or a pointer-map page.
67636 ** 40 60 15 4-byte meta values passed to higher layers
67641 ** 52 4 Largest root-page (auto/incr_vacuum)
67642 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
67645 ** 68 4 Application-ID
67647 ** 92 4 The version-valid-for number
67650 ** All of the integer values are big-endian (most significant byte first).
67658 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
67673 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
67676 ** |----------------|
67678 ** |----------------|
67680 ** |----------------|
67684 ** |----------------|
67687 ** |----------------| ^ Grows upwards
67690 ** |----------------|
67709 ** The cell pointer array contains zero or more 2-byte numbers which are
67740 ** appears first. A variable-length integer may not be more than 9 bytes long.
67742 ** allows a 64-bit integer to be encoded in 9 bytes.
67765 ** filled with data (pagesize - 4 bytes). The last page can have as little
67788 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
67795 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
67806 ** You can change this value at compile-time by specifying a
67807 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
67808 ** header must be exactly 16 bytes including the zero-terminator so
67820 ** first byte of on-disk image of every BTree page.
67830 ** is derived from the raw on-disk page content.
67837 ** stored in MemPage.pBt->mutex.
67841 u8 intKey; /* True if table b-trees. False for index b-trees */
67854 int nFree; /* Number of free bytes on the page. -1 for unknown */
67857 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
67858 ** non-overflow cell */
67862 u8 *aDataEnd; /* One byte past the end of the entire page - not just
67864 ** corruption-induced buffer overflow. */
67920 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
67934 ** If the shared-data extension is enabled, there may be multiple users
67946 # error wrong numeric code for no-transaction
67949 # error wrong numeric code for read-transaction
67952 # error wrong numeric code for write-transaction
67975 ** If a BtShared client fails to obtain a write-lock on a database
67976 ** table (because there exists one or more read-locks on the table),
67977 ** the shared-cache enters 'pending-lock' state and isPending is
67980 ** The shared-cache leaves the 'pending lock' state when either of
67986 ** while in the 'pending-lock' state, no connection may start a new
67989 ** This feature is included to help prevent writer-starvation.
67998 u8 autoVacuum; /* True if auto-vacuum is enabled */
67999 u8 incrVacuum; /* True if incr-vacuum is enabled */
68003 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
68006 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
68007 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
68016 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
68017 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
68021 BtLock *pLock; /* List of locks held on this shared-btree struct */
68037 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
68039 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
68051 u16 nSize; /* Size of the cell content on the main b-tree page */
68055 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
68058 ** root-node and 3 for all other internal nodes.
68067 ** b-tree within a database file.
68077 ** found at self->pBt->mutex.
68085 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
68086 ** sqlite3BtreePrevious() is no-op if skipNext<0.
68111 u8 curIntKey; /* Value of apPage[0]->intKey */
68113 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
68116 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
68142 ** Cursor is valid except that the Cursor.skipNext field is non-zero
68144 ** operation should be a no-op.
68169 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
68172 ** These macros define the location of the pointer-map entry for a
68177 ** PTRMAP_PAGENO returns the database page number of the pointer-map
68181 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
68183 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
68187 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
68204 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
68207 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
68215 ** overflow pages. The page-number identifies the previous
68218 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
68231 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
68232 assert( p->pBt->inTransaction>=p->inTrans );
68237 ** if the database supports auto-vacuum or not. Because it is used
68243 #define ISAUTOVACUUM (pBt->autoVacuum)
68254 ** the database. As the integrity-check proceeds, for each page used in
68255 ** the database the corresponding bit is set. This allows integrity-check to
68262 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
68272 u32 *heap; /* Min-heap used for analyzing cell coverage */
68277 ** Routines to read or write a two- and four-byte big-endian integer values.
68286 ** two-byte aligned address. get2bytea() is only used for accessing the
68305 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
68307 ** p->locked boolean to true.
68310 assert( p->locked==0 );
68311 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
68312 assert( sqlite3_mutex_held(p->db->mutex) );
68314 sqlite3_mutex_enter(p->pBt->mutex);
68315 p->pBt->db = p->db;
68316 p->locked = 1;
68320 ** Release the BtShared mutex associated with B-Tree handle p and
68321 ** clear the p->locked boolean.
68324 BtShared *pBt = p->pBt;
68325 assert( p->locked==1 );
68326 assert( sqlite3_mutex_held(pBt->mutex) );
68327 assert( sqlite3_mutex_held(p->db->mutex) );
68328 assert( p->db==pBt->db );
68330 sqlite3_mutex_leave(pBt->mutex);
68331 p->locked = 0;
68341 ** and this routine is a no-op. The underlying mutex is non-recursive.
68346 ** by all database connections. The p->pNext is a list of other
68349 ** p, then first unlock all of the others on p->pNext, then wait
68358 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
68359 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
68360 assert( p->pNext==0 || p->pNext->db==p->db );
68361 assert( p->pPrev==0 || p->pPrev->db==p->db );
68362 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
68365 assert( !p->locked || p->wantToLock>0 );
68366 assert( p->sharable || p->wantToLock==0 );
68369 assert( sqlite3_mutex_held(p->db->mutex) );
68373 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
68375 if( !p->sharable ) return;
68376 p->wantToLock++;
68377 if( p->locked ) return;
68394 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
68395 p->pBt->db = p->db;
68396 p->locked = 1;
68405 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68406 assert( pLater->sharable );
68407 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
68408 assert( !pLater->locked || pLater->wantToLock>0 );
68409 if( pLater->locked ){
68414 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
68415 if( pLater->wantToLock ){
68426 assert( sqlite3_mutex_held(p->db->mutex) );
68427 if( p->sharable ){
68428 assert( p->wantToLock>0 );
68429 p->wantToLock--;
68430 if( p->wantToLock==0 ){
68439 ** B-Tree is not marked as sharable.
68444 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
68445 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
68446 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
68447 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
68449 return (p->sharable==0 || p->locked);
68461 ** There is a corresponding leave-all procedures.
68472 assert( sqlite3_mutex_held(db->mutex) );
68473 for(i=0; i<db->nDb; i++){
68474 p = db->aDb[i].pBt;
68475 if( p && p->sharable ){
68480 db->noSharedCache = skipOk;
68483 if( db->noSharedCache==0 ) btreeEnterAll(db);
68488 assert( sqlite3_mutex_held(db->mutex) );
68489 for(i=0; i<db->nDb; i++){
68490 p = db->aDb[i].pBt;
68495 if( db->noSharedCache==0 ) btreeLeaveAll(db);
68507 if( !sqlite3_mutex_held(db->mutex) ){
68510 for(i=0; i<db->nDb; i++){
68512 p = db->aDb[i].pBt;
68513 if( p && p->sharable &&
68514 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
68525 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
68529 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
68537 if( db->pVfs==0 && db->nDb==0 ) return 1;
68539 assert( iDb>=0 && iDb<db->nDb );
68540 if( !sqlite3_mutex_held(db->mutex) ) return 0;
68542 p = db->aDb[iDb].pBt;
68544 return p->sharable==0 || p->locked==1;
68552 ** these two routines, all mutex operations are no-ops in that case and
68556 ** the ones below, are no-ops and are null #defines in btree.h.
68560 p->pBt->db = p->db;
68564 for(i=0; i<db->nDb; i++){
68565 Btree *p = db->aDb[i].pBt;
68567 p->pBt->db = p->db;
68582 sqlite3BtreeEnter(pCur->pBtree);
68586 sqlite3BtreeLeave(pCur->pBtree);
68606 ** This file implements an external (disk-based) database using BTrees.
68630 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
68635 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
68638 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
68651 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
68696 ** shared-cache table level locks. If the library is compiled with the
68697 ** shared-cache feature disabled, then there is only ever one user
68699 ** So define the lock related functions as no-ops.
68714 u64 n = pBt->nSeek;
68715 pBt->nSeek = 0;
68722 if( pager==NULL || pager->pCodec ){
68734 if( iDbpage==1 && db->aDb ){
68735 Btree *p = db->aDb[0].pBt;
68736 if( p && p->pBt ){
68750 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
68758 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
68769 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
68781 ** For example, when writing to a table with root-page iRoot via
68794 ** hold a write-lock on the schema table (root page 1). This is also
68799 Pgno iRoot, /* Root page of b-tree */
68800 int isIndex, /* True if iRoot is the root of an index b-tree */
68803 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
68808 ** and has the read-uncommitted flag set, then no lock is required.
68811 if( (pBtree->sharable==0)
68812 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
68819 ** the correct locks are held. So do not bother - just return true.
68822 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
68826 /* Figure out the root-page that the lock should be held on. For table
68827 ** b-trees, this is just the root page of the b-tree being read or
68828 ** written. For index b-trees, it is the root page of the associated
68833 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
68835 if( pIdx->tnum==iRoot ){
68842 iTab = pIdx->pTable->tnum;
68850 /* Search for the required lock. Either a write-lock on root-page iTab, a
68851 ** write-lock on the schema table, or (if the client is reading) a
68852 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
68853 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
68854 if( pLock->pBtree==pBtree
68855 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
68856 && pLock->eLock>=eLockType
68878 ** read-uncommitted flag set, then it is OK for the other object to
68888 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
68889 if( p->pgnoRoot==iRoot
68890 && p->pBtree!=pBtree
68891 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
68902 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
68907 BtShared *pBt = p->pBt;
68912 assert( p->db!=0 );
68913 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
68915 /* If requesting a write-lock, then the Btree must have an open write
68919 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
68920 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
68922 /* This routine is a no-op if the shared-cache is not enabled */
68923 if( !p->sharable ){
68930 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
68931 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
68935 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
68936 /* The condition (pIter->eLock!=eLock) in the following if(...)
68939 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
68945 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
68946 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
68947 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
68948 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
68950 assert( p==pBt->pWriter );
68951 pBt->btsFlags |= BTS_PENDING;
68962 ** Add a lock on the table with root-page iTable to the shared-btree used
68979 BtShared *pBt = p->pBt;
68985 assert( p->db!=0 );
68987 /* A connection with the read-uncommitted flag set will never try to
68988 ** obtain a read-lock using this function. The only read-lock obtained
68989 ** by a connection in read-uncommitted mode is on the sqlite_schema
68991 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
68993 /* This function should only be called on a sharable b-tree after it
68994 ** has been determined that no other b-tree holds a conflicting lock. */
68995 assert( p->sharable );
68999 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
69000 if( pIter->iTable==iTable && pIter->pBtree==p ){
69014 pLock->iTable = iTable;
69015 pLock->pBtree = p;
69016 pLock->pNext = pBt->pLock;
69017 pBt->pLock = pLock;
69021 ** and the requested lock. This means if a write-lock was already held
69022 ** and a read-lock requested, we don't incorrectly downgrade the lock.
69025 if( eLock>pLock->eLock ){
69026 pLock->eLock = eLock;
69043 BtShared *pBt = p->pBt;
69044 BtLock **ppIter = &pBt->pLock;
69047 assert( p->sharable || 0==*ppIter );
69048 assert( p->inTrans>0 );
69052 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
69053 assert( pLock->pBtree->inTrans>=pLock->eLock );
69054 if( pLock->pBtree==p ){
69055 *ppIter = pLock->pNext;
69056 assert( pLock->iTable!=1 || pLock==&p->lock );
69057 if( pLock->iTable!=1 ){
69061 ppIter = &pLock->pNext;
69065 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
69066 if( pBt->pWriter==p ){
69067 pBt->pWriter = 0;
69068 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69069 }else if( pBt->nTransaction==2 ){
69079 pBt->btsFlags &= ~BTS_PENDING;
69084 ** This function changes all write-locks held by Btree p into read-locks.
69087 BtShared *pBt = p->pBt;
69088 if( pBt->pWriter==p ){
69090 pBt->pWriter = 0;
69091 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
69092 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
69093 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
69094 pLock->eLock = READ_LOCK;
69112 return sqlite3_mutex_held(p->pBt->mutex);
69116 ** database connetion. This is important in shared-cache mode. If the database
69117 ** connection pointers get out-of-sync, it is possible for routines like
69120 ** statements only and for the purpose of double-checking that the btree code
69121 ** does keep the database connection pointers up-to-date.
69125 return (p->pBtree->db==p->pBt->db);
69133 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
69136 ** Invalidate the overflow page-list cache for all cursors opened
69141 assert( sqlite3_mutex_held(pBt->mutex) );
69142 for(p=pBt->pCursor; p; p=p->pNext){
69155 ** cursors open on any row within the table with root-page pgnoRoot.
69168 assert( pBtree->hasIncrblobCur );
69170 pBtree->hasIncrblobCur = 0;
69171 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69172 if( (p->curFlags & BTCF_Incrblob)!=0 ){
69173 pBtree->hasIncrblobCur = 1;
69174 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
69175 p->eState = CURSOR_INVALID;
69188 ** when a page that previously contained data becomes a free-list leaf
69193 ** free-list leaf pages:
69196 ** a free-list leaf page, the page is not written to the database
69197 ** (as free-list leaf pages contain no meaningful data). Sometimes
69201 ** 2) When a free-list leaf page is reused, its content is not read
69207 ** a page is moved to the free-list and then reused within the same
69209 ** it is moved to the free-list and it is also not journalled when it
69210 ** is extracted from the free-list and reused, then the original data
69215 ** moved to become a free-list leaf page, the corresponding bit is
69216 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
69223 if( !pBt->pHasContent ){
69224 assert( pgno<=pBt->nPage );
69225 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
69226 if( !pBt->pHasContent ){
69230 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
69231 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
69239 ** This function is called when a free-list leaf page is removed from the
69240 ** free-list for reuse. It returns false if it is safe to retrieve the
69241 ** page from the pager layer with the 'no-content' flag set. True otherwise.
69244 Bitvec *p = pBt->pHasContent;
69250 ** invoked at the conclusion of each write-transaction.
69253 sqlite3BitvecDestroy(pBt->pHasContent);
69254 pBt->pHasContent = 0;
69262 if( pCur->iPage>=0 ){
69263 for(i=0; i<pCur->iPage; i++){
69264 releasePageNotNull(pCur->apPage[i]);
69266 releasePageNotNull(pCur->pPage);
69267 pCur->iPage = -1;
69274 ** function saves the current cursor key in variables pCur->nKey and
69275 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
69279 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
69280 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
69281 ** set to point to a malloced buffer pCur->nKey bytes in size containing
69286 assert( CURSOR_VALID==pCur->eState );
69287 assert( 0==pCur->pKey );
69290 if( pCur->curIntKey ){
69292 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
69297 ** up to the size of 1 varint plus 1 8-byte value when the cursor
69301 pCur->nKey = sqlite3BtreePayloadSize(pCur);
69302 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
69304 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
69306 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
69307 pCur->pKey = pKey;
69315 assert( !pCur->curIntKey || !pCur->pKey );
69329 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
69330 assert( 0==pCur->pKey );
69333 if( pCur->curFlags & BTCF_Pinned ){
69336 if( pCur->eState==CURSOR_SKIPNEXT ){
69337 pCur->eState = CURSOR_VALID;
69339 pCur->skipNext = 0;
69345 pCur->eState = CURSOR_REQUIRESEEK;
69348 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
69357 ** the table with root-page iRoot. "Saving the cursor position" means that
69368 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
69378 assert( sqlite3_mutex_held(pBt->mutex) );
69379 assert( pExcept==0 || pExcept->pBt==pBt );
69380 for(p=pBt->pCursor; p; p=p->pNext){
69381 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
69384 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
69399 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
69400 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
69406 testcase( p->iPage>=0 );
69410 p = p->pNext;
69420 sqlite3_free(pCur->pKey);
69421 pCur->pKey = 0;
69422 pCur->eState = CURSOR_INVALID;
69441 KeyInfo *pKeyInfo = pCur->pKeyInfo;
69446 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
69451 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
69470 assert( pCur->eState>=CURSOR_REQUIRESEEK );
69471 if( pCur->eState==CURSOR_FAULT ){
69472 return pCur->skipNext;
69474 pCur->eState = CURSOR_INVALID;
69478 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
69481 sqlite3_free(pCur->pKey);
69482 pCur->pKey = 0;
69483 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
69484 if( skipNext ) pCur->skipNext = skipNext;
69485 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
69486 pCur->eState = CURSOR_SKIPNEXT;
69493 (p->eState>=CURSOR_REQUIRESEEK ? \
69513 assert( sizeof(pCur->eState)==1 );
69545 assert( pCur->eState!=CURSOR_VALID );
69551 if( pCur->eState!=CURSOR_VALID ){
69575 pCur->hints = x;
69582 ** number for the pointer-map page that contains the entry for the
69592 assert( sqlite3_mutex_held(pBt->mutex) );
69594 nPagesPerMapPage = (pBt->usableSize/5)+1;
69595 iPtrMap = (pgno-2)/nPagesPerMapPage;
69609 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
69610 ** a no-op. If an error occurs, the appropriate error code is written
69622 assert( sqlite3_mutex_held(pBt->mutex) );
69623 /* The super-journal page number must never be used as a pointer map page */
69626 assert( pBt->autoVacuum );
69632 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69649 assert( offset <= (int)pBt->usableSize-5 );
69653 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
69679 assert( sqlite3_mutex_held(pBt->mutex) );
69682 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
69693 assert( offset <= (int)pBt->usableSize-5 );
69715 ** 4-byte child pointer found on interior pages, if there is one.
69720 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69722 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
69728 ** on a single B-tree page. Make necessary adjustments to the CellInfo
69749 minLocal = pPage->minLocal;
69750 maxLocal = pPage->maxLocal;
69751 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
69755 pInfo->nLocal = (u16)surplus;
69757 pInfo->nLocal = (u16)minLocal;
69759 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
69768 maxLocal = pPage->maxLocal;
69774 minLocal = pPage->minLocal;
69775 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
69799 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69800 assert( pPage->leaf==0 );
69801 assert( pPage->childPtrSize==4 );
69805 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
69806 pInfo->nPayload = 0;
69807 pInfo->nLocal = 0;
69808 pInfo->pPayload = 0;
69820 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69821 assert( pPage->leaf==0 || pPage->leaf==1 );
69822 assert( pPage->intKeyLeaf );
69823 assert( pPage->childPtrSize==0 );
69844 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
69847 ** This routine is a high-runner.
69877 pInfo->nKey = *(i64*)&iKey;
69878 pInfo->nPayload = nPayload;
69879 pInfo->pPayload = pIter;
69880 testcase( nPayload==pPage->maxLocal );
69881 testcase( nPayload==(u32)pPage->maxLocal+1 );
69882 if( nPayload<=pPage->maxLocal ){
69886 pInfo->nSize = nPayload + (u16)(pIter - pCell);
69887 if( pInfo->nSize<4 ) pInfo->nSize = 4;
69888 pInfo->nLocal = (u16)nPayload;
69901 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69902 assert( pPage->leaf==0 || pPage->leaf==1 );
69903 assert( pPage->intKeyLeaf==0 );
69904 pIter = pCell + pPage->childPtrSize;
69914 pInfo->nKey = nPayload;
69915 pInfo->nPayload = nPayload;
69916 pInfo->pPayload = pIter;
69917 testcase( nPayload==pPage->maxLocal );
69918 testcase( nPayload==(u32)pPage->maxLocal+1 );
69919 if( nPayload<=pPage->maxLocal ){
69923 pInfo->nSize = nPayload + (u16)(pIter - pCell);
69924 if( pInfo->nSize<4 ) pInfo->nSize = 4;
69925 pInfo->nLocal = (u16)nPayload;
69935 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
69943 ** data area of the btree-page. The return number includes the cell
69952 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
69962 pPage->xParseCell(pPage, pCell, &debuginfo);
69974 testcase( nSize==pPage->maxLocal );
69975 testcase( nSize==(u32)pPage->maxLocal+1 );
69976 if( nSize<=pPage->maxLocal ){
69977 nSize += (u32)(pIter - pCell);
69980 int minLocal = pPage->minLocal;
69981 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
69982 testcase( nSize==pPage->maxLocal );
69983 testcase( nSize==(u32)pPage->maxLocal+1 );
69984 if( nSize>pPage->maxLocal ){
69987 nSize += 4 + (u16)(pIter - pCell);
70002 pPage->xParseCell(pPage, pCell, &debuginfo);
70007 assert( pPage->childPtrSize==4 );
70010 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
70011 return (u16)(pIter - pCell);
70024 pPage->xParseCell(pPage, pCell, &debuginfo);
70036 /* pIter now points at the 64-bit integer key value, a variable length
70047 testcase( nSize==pPage->maxLocal );
70048 testcase( nSize==(u32)pPage->maxLocal+1 );
70049 if( nSize<=pPage->maxLocal ){
70050 nSize += (u32)(pIter - pCell);
70053 int minLocal = pPage->minLocal;
70054 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
70055 testcase( nSize==pPage->maxLocal );
70056 testcase( nSize==(u32)pPage->maxLocal+1 );
70057 if( nSize>pPage->maxLocal ){
70060 nSize += 4 + (u16)(pIter - pCell);
70071 return pPage->xCellSize(pPage, findCell(pPage, iCell));
70079 ** pointer to an overflow page, insert an entry into the pointer-map for
70086 pPage->xParseCell(pPage, pCell, &info);
70089 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
70094 ovfl = get4byte(&pCell[info.nSize-4]);
70095 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
70103 ** page so that there are no free-blocks on the free-block list.
70108 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
70109 ** b-tree page so that there are no freeblocks or fragment bytes, all
70115 int pc; /* Address of the i-th cell */
70129 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70130 assert( pPage->pBt!=0 );
70131 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
70132 assert( pPage->nOverflow==0 );
70133 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70134 data = pPage->aData;
70135 hdr = pPage->hdrOffset;
70136 cellOffset = pPage->cellOffset;
70137 nCell = pPage->nCell;
70140 usableSize = pPage->pBt->usableSize;
70145 ** offsets to each pointer in the cell-pointer array than it is to
70149 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
70152 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
70166 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
70173 assert( cbrk+(iFree-top) <= usableSize );
70174 memmove(&data[cbrk], &data[top], iFree-top);
70186 iCellLast = usableSize - 4;
70189 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
70190 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
70193 u8 *pAddr; /* The i-th cell pointer */
70205 size = pPage->xCellSize(pPage, &src[pc]);
70206 cbrk -= size;
70220 assert( pPage->nFree>=0 );
70221 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
70228 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
70229 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70234 ** Search the free-list on page pPg for space to store a cell nByte bytes in
70236 ** from the free-list.
70238 ** If no suitable space can be found on the free-list, return NULL.
70248 const int hdr = pPg->hdrOffset; /* Offset to page header */
70249 u8 * const aData = pPg->aData; /* Page data */
70254 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
70259 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
70260 ** freeblock form a big-endian integer which is the size of the freeblock
70261 ** in bytes, including the 4-byte header. */
70264 if( (x = size - nByte)>=0 ){
70268 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
70272 /* Remove the slot from the free-list. Update the number of
70282 /* The slot remains on the free-list. Reduce its size to account
70299 if( pc>maxPC+nByte-4 ){
70307 ** Allocate nByte bytes of space from within the B-Tree page passed
70308 ** as the first argument. Write into *pIdx the index into pPage->aData[]
70320 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
70321 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
70327 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70328 assert( pPage->pBt );
70329 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70331 assert( pPage->nFree>=nByte );
70332 assert( pPage->nOverflow==0 );
70333 assert( nByte < (int)(pPage->pBt->usableSize-8) );
70335 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
70336 gap = pPage->cellOffset + 2*pPage->nCell;
70338 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
70341 ** However, that integer is too large to be stored in a 2-byte unsigned
70345 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
70347 if( top==0 && pPage->pBt->usableSize==65536 ){
70365 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
70366 *pIdx = g2 = (int)(pSpace-data);
70382 assert( pPage->nCell>0 || CORRUPT_DB );
70383 assert( pPage->nFree>=0 );
70384 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
70397 top -= nByte;
70399 assert( top+nByte <= (int)pPage->pBt->usableSize );
70405 ** Return a section of the pPage->aData to the freelist.
70406 ** The first byte of the new free block is pPage->aData[iStart]
70425 unsigned char *data = pPage->aData; /* Page content */
70428 assert( pPage->pBt!=0 );
70429 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70430 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
70431 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
70432 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70434 assert( iStart<=pPage->pBt->usableSize-4 );
70439 hdr = pPage->hdrOffset;
70451 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
70463 nFrag = iFreeBlk - iEnd;
70466 if( iEnd > pPage->pBt->usableSize ){
70469 iSize = iEnd - iStart;
70481 nFrag += iStart - iPtrEnd;
70482 iSize = iEnd - iPtr;
70487 data[hdr+7] -= nFrag;
70503 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
70510 pPage->nFree += iOrigSize;
70527 BtShared *pBt; /* A copy of pPage->pBt */
70529 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
70530 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70531 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
70533 pPage->childPtrSize = 4-4*pPage->leaf;
70534 pBt = pPage->pBt;
70536 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
70537 ** interior table b-tree page. */
70539 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
70540 ** leaf table b-tree page. */
70542 pPage->intKey = 1;
70543 if( pPage->leaf ){
70544 pPage->intKeyLeaf = 1;
70545 pPage->xCellSize = cellSizePtrTableLeaf;
70546 pPage->xParseCell = btreeParseCellPtr;
70548 pPage->intKeyLeaf = 0;
70549 pPage->xCellSize = cellSizePtrNoPayload;
70550 pPage->xParseCell = btreeParseCellPtrNoPayload;
70552 pPage->maxLocal = pBt->maxLeaf;
70553 pPage->minLocal = pBt->minLeaf;
70555 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
70556 ** interior index b-tree page. */
70558 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
70559 ** leaf index b-tree page. */
70561 pPage->intKey = 0;
70562 pPage->intKeyLeaf = 0;
70563 pPage->xCellSize = cellSizePtr;
70564 pPage->xParseCell = btreeParseCellPtrIndex;
70565 pPage->maxLocal = pBt->maxLocal;
70566 pPage->minLocal = pBt->minLocal;
70568 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
70570 pPage->intKey = 0;
70571 pPage->intKeyLeaf = 0;
70572 pPage->xCellSize = cellSizePtr;
70573 pPage->xParseCell = btreeParseCellPtrIndex;
70578 pPage->pgno,
70580 pPage->isInit,
70581 pPage->intKey,
70582 pPage->intKeyLeaf,
70583 pPage->leaf,
70584 pPage->childPtrSize,
70585 pPage->cellOffset,
70586 pPage->nCell,
70587 pPage->hdrOffset,
70588 pPage->minLocal,
70589 pPage->maxLocal,
70594 pPage->max1bytePayload = pBt->max1bytePayload;
70600 ** in the pPage->nFree field.
70603 int pc; /* Address of a freeblock within pPage->aData[] */
70605 u8 *data; /* Equal to pPage->aData */
70612 assert( pPage->pBt!=0 );
70613 assert( pPage->pBt->db!=0 );
70614 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70615 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
70616 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
70617 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
70618 assert( pPage->isInit==1 );
70619 assert( pPage->nFree<0 );
70621 usableSize = pPage->pBt->usableSize;
70622 hdr = pPage->hdrOffset;
70623 data = pPage->aData;
70624 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
70628 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
70629 iCellLast = usableSize - 4;
70632 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
70636 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
70640 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
70667 ** of the cell-content area plus the number of free bytes within
70668 ** the cell-content area. If this is greater than the usable-size
70670 ** serves to verify that the offset to the start of the cell-content
70676 pPage->nFree = (u16)(nFree - iCellFirst);
70689 int pc; /* Address of a freeblock within pPage->aData[] */
70690 u8 *data; /* Equal to pPage->aData */
70694 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
70695 usableSize = pPage->pBt->usableSize;
70696 iCellLast = usableSize - 4;
70697 data = pPage->aData;
70698 cellOffset = pPage->cellOffset;
70699 if( !pPage->leaf ) iCellLast--;
70700 for(i=0; i<pPage->nCell; i++){
70707 sz = pPage->xCellSize(pPage, &data[pc]);
70720 ** not contain a well-formed database page, then return
70722 ** guarantee that the page is well-formed. It only shows that
70726 u8 *data; /* Equal to pPage->aData */
70729 assert( pPage->pBt!=0 );
70730 assert( pPage->pBt->db!=0 );
70731 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70732 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
70733 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
70734 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
70735 assert( pPage->isInit==0 );
70737 pBt = pPage->pBt;
70738 data = pPage->aData + pPage->hdrOffset;
70739 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
70740 ** the b-tree page type. */
70744 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
70745 pPage->maskPage = (u16)(pBt->pageSize - 1);
70746 pPage->nOverflow = 0;
70747 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
70748 pPage->aCellIdx = data + pPage->childPtrSize + 8;
70749 pPage->aDataEnd = pPage->aData + pBt->pageSize;
70750 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
70751 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
70753 pPage->nCell = get2byte(&data[3]);
70754 if( pPage->nCell>MX_CELL(pBt) ){
70758 testcase( pPage->nCell==MX_CELL(pBt) );
70759 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
70763 assert( pPage->nCell>0
70764 || get2byteNotZero(&data[5])==(int)pBt->usableSize
70766 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
70767 pPage->isInit = 1;
70768 if( pBt->db->flags & SQLITE_CellSizeCk ){
70779 unsigned char *data = pPage->aData;
70780 BtShared *pBt = pPage->pBt;
70781 u8 hdr = pPage->hdrOffset;
70784 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
70785 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
70786 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
70787 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70788 assert( sqlite3_mutex_held(pBt->mutex) );
70789 if( pBt->btsFlags & BTS_FAST_SECURE ){
70790 memset(&data[hdr], 0, pBt->usableSize - hdr);
70796 put2byte(&data[hdr+5], pBt->usableSize);
70797 pPage->nFree = (u16)(pBt->usableSize - first);
70799 pPage->cellOffset = first;
70800 pPage->aDataEnd = &data[pBt->pageSize];
70801 pPage->aCellIdx = &data[first];
70802 pPage->aDataOfst = &data[pPage->childPtrSize];
70803 pPage->nOverflow = 0;
70804 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
70805 pPage->maskPage = (u16)(pBt->pageSize - 1);
70806 pPage->nCell = 0;
70807 pPage->isInit = 1;
70817 if( pgno!=pPage->pgno ){
70818 pPage->aData = sqlite3PagerGetData(pDbPage);
70819 pPage->pDbPage = pDbPage;
70820 pPage->pBt = pBt;
70821 pPage->pgno = pgno;
70822 pPage->hdrOffset = pgno==1 ? 100 : 0;
70824 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
70849 assert( sqlite3_mutex_held(pBt->mutex) );
70850 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
70863 assert( sqlite3_mutex_held(pBt->mutex) );
70864 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
70873 ** error, return ((unsigned int)-1).
70876 return pBt->nPage;
70880 return btreePagecount(p->pBt);
70888 ** And if the fetch fails, this routine must decrement pCur->iPage.
70890 ** The page is fetched as read-write unless pCur is not NULL and is
70891 ** a read-only cursor.
70901 int bReadOnly /* True for a read-only page */
70905 assert( sqlite3_mutex_held(pBt->mutex) );
70906 assert( pCur==0 || ppPage==&pCur->pPage );
70907 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
70908 assert( pCur==0 || pCur->iPage>0 );
70914 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
70919 if( (*ppPage)->isInit==0 ){
70926 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
70927 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
70931 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
70941 pCur->iPage--;
70942 pCur->pPage = pCur->apPage[pCur->iPage];
70956 assert( pPage->aData );
70957 assert( pPage->pBt );
70958 assert( pPage->pDbPage!=0 );
70959 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
70960 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
70961 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70962 sqlite3PagerUnrefNotNull(pPage->pDbPage);
70969 assert( pPage->aData );
70970 assert( pPage->pBt );
70971 assert( pPage->pDbPage!=0 );
70972 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
70973 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
70974 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70975 sqlite3PagerUnrefPageOne(pPage->pDbPage);
70995 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
71000 (*ppPage)->isInit = 0;
71020 if( pPage->isInit ){
71021 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71022 pPage->isInit = 0;
71029 ** the call for every page that comes in for re-initing. */
71041 assert( pBt->db );
71042 assert( sqlite3_mutex_held(pBt->db->mutex) );
71043 int rc = sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
71046 printLockInfoUsingPager( pBt->pPager );
71057 ** be exclusively in memory, or it might use a disk-based memory cache.
71061 ** If zFilename is ":memory:" then an in-memory database is created
71074 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
71091 /* Set the variable isMemdb to true for an in-memory database, or
71092 ** false for a file-based database.
71104 assert( sqlite3_mutex_held(db->mutex) );
71123 p->inTrans = TRANS_NONE;
71124 p->db = db;
71126 p->lock.pBtree = p;
71127 p->lock.iTable = 1;
71138 int nFullPathname = pVfs->mxPathname+1;
71142 p->sharable = 1;
71168 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
71169 assert( pBt->nRef>0 );
71170 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
71171 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
71173 for(iDb=db->nDb-1; iDb>=0; iDb--){
71174 Btree *pExisting = db->aDb[iDb].pBt;
71175 if( pExisting && pExisting->pBt==pBt ){
71183 p->pBt = pBt;
71184 pBt->nRef++;
71198 p->sharable = 1;
71220 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
71223 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
71224 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
71229 pBt->openFlags = (u8)flags;
71230 pBt->db = db;
71231 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
71232 p->pBt = pBt;
71234 pBt->pCursor = 0;
71235 pBt->pPage1 = 0;
71236 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
71238 pBt->btsFlags |= BTS_SECURE_DELETE;
71240 pBt->btsFlags |= BTS_OVERWRITE;
71242 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71243 ** determined by the 2-byte integer located at an offset of 16 bytes from
71245 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
71246 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
71247 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
71248 pBt->pageSize = 0;
71250 /* If the magic name ":memory:" will create an in-memory database, then
71251 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
71254 ** regular file-name. In this case the auto-vacuum applies as per normal.
71257 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
71258 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
71263 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
71264 ** determined by the one-byte unsigned integer found at an offset of 20
71267 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71269 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
71270 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
71273 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
71275 pBt->usableSize = pBt->pageSize - nReserve;
71276 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
71281 pBt->nRef = 1;
71282 if( p->sharable ){
71286 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
71287 if( pBt->mutex==0 ){
71293 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
71305 if( p->sharable ){
71308 for(i=0; i<db->nDb; i++){
71309 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
71310 while( pSib->pPrev ){ pSib = pSib->pPrev; }
71311 if( (uptr)p->pBt<(uptr)pSib->pBt ){
71312 p->pNext = pSib;
71313 p->pPrev = 0;
71314 pSib->pPrev = p;
71316 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
71317 pSib = pSib->pNext;
71319 p->pNext = pSib->pNext;
71320 p->pPrev = pSib;
71321 if( p->pNext ){
71322 p->pNext->pPrev = p;
71324 pSib->pNext = p;
71335 if( pBt && pBt->pPager ){
71336 sqlite3PagerClose(pBt->pPager, 0);
71344 /* If the B-Tree was successfully opened, set the pager-cache size to the
71345 ** default value. Except, when opening on an existing shared pager-cache,
71346 ** do not change the pager-cache size.
71352 pFile = sqlite3PagerFile(pBt->pPager);
71353 if( pFile->pMethods ){
71354 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
71377 assert( sqlite3_mutex_notheld(pBt->mutex) );
71380 pBt->nRef--;
71381 if( pBt->nRef<=0 ){
71383 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
71386 while( ALWAYS(pList) && pList->pNext!=pBt ){
71387 pList=pList->pNext;
71390 pList->pNext = pBt->pNext;
71394 sqlite3_mutex_free(pBt->mutex);
71406 ** Make sure pBt->pTmpSpace points to an allocation of
71407 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
71412 assert( pBt->pTmpSpace==0 );
71415 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
71416 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
71417 if( pBt->pTmpSpace==0 ){
71418 BtCursor *pCur = pBt->pCursor;
71419 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
71424 /* One of the uses of pBt->pTmpSpace is to format cells before
71437 ** left-child pointer to the beginning of a cell.
71439 memset(pBt->pTmpSpace, 0, 8);
71440 pBt->pTmpSpace += 4;
71445 ** Free the pBt->pTmpSpace allocation
71448 if( pBt->pTmpSpace ){
71449 pBt->pTmpSpace -= 4;
71450 sqlite3PageFree(pBt->pTmpSpace);
71451 pBt->pTmpSpace = 0;
71459 BtShared *pBt = p->pBt;
71462 assert( sqlite3_mutex_held(p->db->mutex) );
71468 BtCursor *pCur = pBt->pCursor;
71471 pCur = pCur->pNext;
71472 assert( pTmp->pBtree!=p );
71479 ** The call to sqlite3BtreeRollback() drops any table-locks held by
71485 /* If there are still other outstanding references to the shared-btree
71487 ** up the shared-btree.
71489 assert( p->wantToLock==0 && p->locked==0 );
71490 if( !p->sharable || removeFromSharingList(pBt) ){
71496 assert( !pBt->pCursor );
71497 sqlite3PagerClose(pBt->pPager, p->db);
71498 if( pBt->xFreeSchema && pBt->pSchema ){
71499 pBt->xFreeSchema(pBt->pSchema);
71501 sqlite3DbFree(0, pBt->pSchema);
71507 assert( p->wantToLock==0 );
71508 assert( p->locked==0 );
71509 if( p->pPrev ) p->pPrev->pNext = p->pNext;
71510 if( p->pNext ) p->pNext->pPrev = p->pPrev;
71525 BtShared *pBt = p->pBt;
71526 assert( sqlite3_mutex_held(p->db->mutex) );
71528 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
71544 BtShared *pBt = p->pBt;
71546 assert( sqlite3_mutex_held(p->db->mutex) );
71548 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
71559 BtShared *pBt = p->pBt;
71560 assert( sqlite3_mutex_held(p->db->mutex) );
71562 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
71573 ** is a very low but non-zero probability of damage. Level 3 reduces the
71581 BtShared *pBt = p->pBt;
71582 assert( sqlite3_mutex_held(p->db->mutex) );
71584 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
71613 BtShared *pBt = p->pBt;
71616 pBt->nReserveWanted = nReserve;
71617 x = pBt->pageSize - pBt->usableSize;
71619 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
71625 ((pageSize-1)&pageSize)==0 ){
71627 assert( !pBt->pCursor );
71629 pBt->pageSize = (u32)pageSize;
71632 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
71633 pBt->usableSize = pBt->pageSize - (u16)nReserve;
71634 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71643 return p->pBt->pageSize;
71648 ** may only be called if it is guaranteed that the b-tree mutex is already
71652 ** known that the shared b-tree mutex is held, but the mutex on the
71659 assert( sqlite3_mutex_held(p->pBt->mutex) );
71660 n = p->pBt->pageSize - p->pBt->usableSize;
71671 ** The amount of reserve can only grow - never shrink.
71676 n1 = (int)p->pBt->nReserveWanted;
71691 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
71702 ** newFlag==(-1) No changes
71707 ** freelist leaf pages are not written back to the database. Thus in-page
71721 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
71722 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
71724 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
71730 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
71731 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
71732 ** is disabled. The default value for the auto-vacuum property is
71739 BtShared *pBt = p->pBt;
71744 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
71747 pBt->autoVacuum = av ?1:0;
71748 pBt->incrVacuum = av==2 ?1:0;
71756 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
71766 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
71767 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
71776 ** If the user has not set the safety-level for this database connection
71777 ** using "PRAGMA synchronous", and if the safety-level is not already
71786 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
71787 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
71788 if( pDb->bSyncSet==0
71789 && pDb->safety_level!=safety_level
71790 && pDb!=&db->aDb[1]
71792 pDb->safety_level = safety_level;
71793 sqlite3PagerSetFlags(pBt->pPager,
71794 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
71811 ** well-formed database file, then SQLITE_CORRUPT is returned.
71821 assert( sqlite3_mutex_held(pBt->mutex) );
71822 assert( pBt->pPage1==0 );
71823 rc = sqlite3PagerSharedLock(pBt->pPager);
71831 nPage = get4byte(28+(u8*)pPage1->aData);
71832 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
71833 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
71836 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
71842 u8 *page1 = pPage1->aData;
71844 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
71853 pBt->btsFlags |= BTS_READ_ONLY;
71860 pBt->btsFlags |= BTS_READ_ONLY;
71871 ** may not be the latest version - there may be a newer one in the log
71874 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
71876 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
71892 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
71901 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
71902 ** determined by the 2-byte integer located at an offset of 16 bytes from
71905 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
71907 if( ((pageSize-1)&pageSize)!=0
71913 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
71915 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
71919 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
71920 ** determined by the one-byte unsigned integer found at an offset of 20
71922 usableSize = pageSize - page1[20];
71923 if( (u32)pageSize!=pBt->pageSize ){
71925 ** of BtShared.pageSize, we have discovered that the page-size is
71926 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
71928 ** again with the correct page-size.
71931 pBt->usableSize = usableSize;
71932 pBt->pageSize = pageSize;
71934 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
71935 pageSize-usableSize);
71939 if( sqlite3WritableSchema(pBt->db)==0 ){
71946 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
71952 pBt->pageSize = pageSize;
71953 pBt->usableSize = usableSize;
71955 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
71956 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
71962 ** cells can will fit on one page. We assume a 10-byte page header.
71964 ** 2-byte pointer to the cell
71965 ** 4-byte child pointer
71966 ** 9-byte nKey value
71967 ** 4-byte nData value
71968 ** 4-byte overflow page pointer
71969 ** So a cell consists of a 2-byte pointer, a header which is as much as
71973 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
71974 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
71975 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
71976 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
71977 if( pBt->maxLocal>127 ){
71978 pBt->max1bytePayload = 127;
71980 pBt->max1bytePayload = (u8)pBt->maxLocal;
71982 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
71983 pBt->pPage1 = pPage1;
71984 pBt->nPage = nPage;
71989 pBt->pPage1 = 0;
72009 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
72010 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
72011 && pCur->eState!=CURSOR_FAULT ) r++;
72023 ** If there is a transaction in progress, this routine is a no-op.
72026 assert( sqlite3_mutex_held(pBt->mutex) );
72027 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
72028 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
72029 MemPage *pPage1 = pBt->pPage1;
72030 assert( pPage1->aData );
72031 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
72032 pBt->pPage1 = 0;
72047 assert( sqlite3_mutex_held(pBt->mutex) );
72048 if( pBt->nPage>0 ){
72051 pP1 = pBt->pPage1;
72053 data = pP1->aData;
72054 rc = sqlite3PagerWrite(pP1->pDbPage);
72058 data[16] = (u8)((pBt->pageSize>>8)&0xff);
72059 data[17] = (u8)((pBt->pageSize>>16)&0xff);
72062 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
72063 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
72067 memset(&data[24], 0, 100-24);
72069 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
72071 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
72072 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
72073 put4byte(&data[36 + 4*4], pBt->autoVacuum);
72074 put4byte(&data[36 + 7*4], pBt->incrVacuum);
72076 pBt->nPage = 1;
72089 p->pBt->nPage = 0;
72090 rc = newDatabase(p->pBt);
72096 ** Attempt to start a new transaction. A write-transaction
72097 ** is started if the second argument is nonzero, otherwise a read-
72101 ** upgraded to exclusive by calling this routine a second time - the
72104 ** A write-transaction must be started before attempting any
72118 ** if there is one. But if there was previously a read-lock, do not
72119 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
72120 ** returned when there is already a read-lock in order to avoid a deadlock.
72131 BtShared *pBt = p->pBt;
72132 Pager *pPager = pBt->pPager;
72138 /* If the btree is already in a write-transaction, or it
72139 ** is already in a read-transaction and a read-transaction
72140 ** is requested, this is a no-op.
72142 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
72145 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
72147 if( (p->db->flags & SQLITE_ResetDatabase)
72150 pBt->btsFlags &= ~BTS_READ_ONLY;
72153 /* Write transactions are not possible on a read-only database */
72154 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
72163 ** on this shared-btree structure and a second write transaction is
72166 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
72167 || (pBt->btsFlags & BTS_PENDING)!=0
72169 pBlock = pBt->pWriter->db;
72172 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
72173 if( pIter->pBtree!=p ){
72174 pBlock = pIter->pBtree->db;
72180 sqlite3ConnectionBlocked(p->db, pBlock);
72187 /* Any read-only or read-write transaction implies a read-lock on
72188 ** page 1. So if some other shared-cache client already has a write-lock
72193 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
72194 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
72196 sqlite3PagerWalDb(pPager, p->db);
72201 if( pBt->pPage1==0 && wrflag ){
72202 assert( pBt->inTransaction==TRANS_NONE );
72208 /* Call lockBtree() until either pBt->pPage1 is populated or
72210 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
72211 ** reading page 1 it discovers that the page-size of the database
72212 ** file is not pBt->pageSize. In this case lockBtree() will update
72213 ** pBt->pageSize to the page-size of the file on disk.
72215 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
72218 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
72221 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
72224 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
72237 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
72245 if( p->inTrans==TRANS_NONE ){
72246 pBt->nTransaction++;
72248 if( p->sharable ){
72249 assert( p->lock.pBtree==p && p->lock.iTable==1 );
72250 p->lock.eLock = READ_LOCK;
72251 p->lock.pNext = pBt->pLock;
72252 pBt->pLock = &p->lock;
72256 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
72257 if( p->inTrans>pBt->inTransaction ){
72258 pBt->inTransaction = p->inTrans;
72261 MemPage *pPage1 = pBt->pPage1;
72263 assert( !pBt->pWriter );
72264 pBt->pWriter = p;
72265 pBt->btsFlags &= ~BTS_EXCLUSIVE;
72266 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
72269 /* If the db-size header field is incorrect (as it may be if an old
72272 ** re-read the database size from page 1 if a savepoint or transaction
72275 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
72276 rc = sqlite3PagerWrite(pPage1->pDbPage);
72278 put4byte(&pPage1->aData[28], pBt->nPage);
72287 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
72292 ** the sub-journal is not already open, then it will be opened here.
72294 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
72306 ** Set the pointer-map entries for all children of page pPage. Also, if
72314 BtShared *pBt = pPage->pBt;
72315 Pgno pgno = pPage->pgno;
72317 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72318 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72320 nCell = pPage->nCell;
72327 if( !pPage->leaf ){
72333 if( !pPage->leaf ){
72334 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72346 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
72349 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
72352 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
72356 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72357 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
72360 if( get4byte(pPage->aData)!=iFrom ){
72363 put4byte(pPage->aData, iTo);
72369 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
72371 nCell = pPage->nCell;
72377 pPage->xParseCell(pPage, pCell, &info);
72379 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
72382 if( iFrom==get4byte(pCell+info.nSize-4) ){
72383 put4byte(pCell+info.nSize-4, iTo);
72388 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
72400 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
72403 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
72415 ** the journal needs to be sync()ed before database page pDbPage->pgno
72423 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
72428 Pgno iDbPage = pDbPage->pgno;
72429 Pager *pPager = pBt->pPager;
72434 assert( sqlite3_mutex_held(pBt->mutex) );
72435 assert( pDbPage->pBt==pBt );
72441 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
72445 pDbPage->pgno = iFreePage;
72447 /* If pDbPage was a btree-page, then it may have child pages and/or cells
72461 Pgno nextOvfl = get4byte(pDbPage->aData);
72479 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
72497 ** Perform a single step of an incremental-vacuum. If successful, return
72502 ** More specifically, this function attempts to re-organize the database so
72508 ** If the bCommit parameter is non-zero, this function assumes that the
72510 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
72514 Pgno nFreeList; /* Number of pages still on the free-list */
72517 assert( sqlite3_mutex_held(pBt->mutex) );
72524 nFreeList = get4byte(&pBt->pPage1->aData[36]);
72539 /* Remove the page from the files free-list. This is not required
72540 ** if bCommit is non-zero. In that case, the free-list will be
72568 ** looping until a free-page located within the first nFin pages
72601 iLastPg--;
72603 pBt->bDoTruncate = 1;
72604 pBt->nPage = iLastPg;
72610 ** The database opened by the first argument is an auto-vacuum database
72612 ** size of the database in pages following an auto-vacuum operation.
72619 nEntry = pBt->usableSize/5;
72620 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
72621 nFin = nOrig - nFree - nPtrmap;
72623 nFin--;
72626 nFin--;
72633 ** A write-transaction must be opened before calling this function.
72642 BtShared *pBt = p->pBt;
72645 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
72646 if( !pBt->autoVacuum ){
72650 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
72662 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72663 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
72675 ** is committed for an auto-vacuum database.
72685 pBt = p->pBt;
72686 pPager = pBt->pPager;
72689 assert( sqlite3_mutex_held(pBt->mutex) );
72691 assert(pBt->autoVacuum);
72692 if( !pBt->incrVacuum ){
72702 ** is either a pointer-map page or the pending-byte page. If one
72708 nFree = get4byte(&pBt->pPage1->aData[36]);
72709 db = p->db;
72710 if( db->xAutovacPages ){
72712 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
72713 if( db->aDb[iDb].pBt==p ) break;
72715 nVac = db->xAutovacPages(
72716 db->pAutovacPagesArg,
72717 db->aDb[iDb].zDbSName,
72720 pBt->pageSize
72736 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
72740 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
72742 put4byte(&pBt->pPage1->aData[32], 0);
72743 put4byte(&pBt->pPage1->aData[36], 0);
72745 put4byte(&pBt->pPage1->aData[28], nFin);
72746 pBt->bDoTruncate = 1;
72747 pBt->nPage = nFin;
72763 ** This routine does the first phase of a two-phase commit. This routine
72775 ** This call is a no-op if no write-transaction is currently active on pBt.
72778 ** the name of a super-journal file that should be written into the
72779 ** individual journal file, or is NULL, indicating no super-journal file
72782 ** When this is called, the super-journal should already have been
72786 ** the write-transaction for this database file is to delete the journal.
72790 if( p->inTrans==TRANS_WRITE ){
72791 BtShared *pBt = p->pBt;
72794 if( pBt->autoVacuum ){
72801 if( pBt->bDoTruncate ){
72802 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
72805 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
72816 BtShared *pBt = p->pBt;
72817 sqlite3 *db = p->db;
72821 pBt->bDoTruncate = 0;
72823 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
72825 ** handle, downgrade to a read-only transaction. The other statements
72828 p->inTrans = TRANS_READ;
72834 if( p->inTrans!=TRANS_NONE ){
72836 pBt->nTransaction--;
72837 if( 0==pBt->nTransaction ){
72838 pBt->inTransaction = TRANS_NONE;
72844 p->inTrans = TRANS_NONE;
72854 ** This routine implements the second phase of a 2-phase commit. The
72866 ** is non-zero then this b-tree transaction is part of a multi-file
72868 ** (by deleting a super-journal file) and the caller will ignore this
72870 ** reset the b-tree objects internal state to indicate that the write
72879 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
72883 /* If the handle has a write-transaction open, commit the shared-btrees
72886 if( p->inTrans==TRANS_WRITE ){
72888 BtShared *pBt = p->pBt;
72889 assert( pBt->inTransaction==TRANS_WRITE );
72890 assert( pBt->nTransaction>0 );
72891 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
72896 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
72897 pBt->inTransaction = TRANS_READ;
72931 ** flag is true, then only write-cursors need be tripped - read-only
72935 ** rolled back modified the database schema. In this case b-tree root
72940 ** saving the current position of a read-only cursor, all cursors,
72941 ** including all read-cursors are tripped.
72953 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
72954 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
72955 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
72964 p->eState = CURSOR_FAULT;
72965 p->skipNext = errCode;
72975 ** Set the pBt->nPage field correctly, according to the current
72976 ** state of the database. Assume pBt->pPage1 is valid.
72979 int nPage = get4byte(&pPage1->aData[28]);
72981 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
72982 testcase( pBt->nPage!=(u32)nPage );
72983 pBt->nPage = nPage;
72999 BtShared *pBt = p->pBt;
73018 if( p->inTrans==TRANS_WRITE ){
73021 assert( TRANS_WRITE==pBt->inTransaction );
73022 rc2 = sqlite3PagerRollback(pBt->pPager);
73027 /* The rollback may have destroyed the pPage1->aData value. So
73029 ** sure pPage1->aData is set correctly. */
73035 pBt->inTransaction = TRANS_READ;
73055 ** A statement sub-transaction is implemented as an anonymous savepoint. The
73057 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
73058 ** are no active savepoints and no other statement-transactions open,
73064 BtShared *pBt = p->pBt;
73066 assert( p->inTrans==TRANS_WRITE );
73067 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
73069 assert( iStatement>p->db->nSavepoint );
73070 assert( pBt->inTransaction==TRANS_WRITE );
73076 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
73088 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
73095 if( p && p->inTrans==TRANS_WRITE ){
73096 BtShared *pBt = p->pBt;
73098 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
73104 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
73107 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
73108 pBt->nPage = 0;
73111 btreeSetNPage(pBt, pBt->pPage1);
73113 /* pBt->nPage might be zero if the database was corrupt when
73115 assert( CORRUPT_DB || pBt->nPage>0 );
73124 ** iTable. If a read-only cursor is requested, it is assumed that
73125 ** the caller already has at least a read-only transaction open
73126 ** on the database already. If a write-cursor is requested, then
73143 ** 3: The database must be writable (not on read-only media)
73154 ** operations on this cursor can be no-ops and all READ operations can
73155 ** return a null row (2-bytes: 0x01 0x00).
73158 ** root page of a b-tree. If it is not, then the cursor acquired
73167 int wrFlag, /* 1 to write. 0 read-only */
73171 BtShared *pBt = p->pBt; /* Shared b-tree handle */
73181 ** b-tree database, the connection is holding the required table locks,
73189 assert( p->inTrans>TRANS_NONE );
73190 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
73191 assert( pBt->pPage1 && pBt->pPage1->aData );
73192 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
73205 pCur->pgnoRoot = iTable;
73206 pCur->iPage = -1;
73207 pCur->pKeyInfo = pKeyInfo;
73208 pCur->pBtree = p;
73209 pCur->pBt = pBt;
73210 pCur->curFlags = 0;
73213 for(pX=pBt->pCursor; pX; pX=pX->pNext){
73214 if( pX->pgnoRoot==iTable ){
73215 pX->curFlags |= BTCF_Multiple;
73216 pCur->curFlags = BTCF_Multiple;
73219 pCur->eState = CURSOR_INVALID;
73220 pCur->pNext = pBt->pCursor;
73221 pBt->pCursor = pCur;
73223 pCur->curFlags |= BTCF_WriteFlag;
73224 pCur->curPagerFlags = 0;
73225 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
73227 pCur->curPagerFlags = PAGER_GET_READONLY;
73234 int wrFlag, /* 1 to write. 0 read-only */
73247 int wrFlag, /* 1 to write. 0 read-only */
73251 if( p->sharable ){
73263 ** to users so they cannot do the sizeof() themselves - they must call
73276 ** of run-time by skipping the initialization of those elements.
73287 Btree *pBtree = pCur->pBtree;
73289 BtShared *pBt = pCur->pBt;
73291 assert( pBt->pCursor!=0 );
73292 if( pBt->pCursor==pCur ){
73293 pBt->pCursor = pCur->pNext;
73295 BtCursor *pPrev = pBt->pCursor;
73297 if( pPrev->pNext==pCur ){
73298 pPrev->pNext = pCur->pNext;
73301 pPrev = pPrev->pNext;
73306 sqlite3_free(pCur->aOverflow);
73307 sqlite3_free(pCur->pKey);
73308 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
73311 assert( pBtree->sharable==0 );
73316 pCur->pBtree = 0;
73331 if( a->nKey!=b->nKey ) return 0;
73332 if( a->pPayload!=b->pPayload ) return 0;
73333 if( a->nPayload!=b->nPayload ) return 0;
73334 if( a->nLocal!=b->nLocal ) return 0;
73335 if( a->nSize!=b->nSize ) return 0;
73341 btreeParseCell(pCur->pPage, pCur->ix, &info);
73342 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
73348 if( pCur->info.nSize==0 ){
73349 pCur->curFlags |= BTCF_ValidNKey;
73350 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
73359 ** that is currently pointing to a row in a (non-empty) table.
73363 return pCur && pCur->eState==CURSOR_VALID;
73368 return pCur->eState==CURSOR_VALID;
73379 assert( pCur->eState==CURSOR_VALID );
73380 assert( pCur->curIntKey );
73382 return pCur->info.nKey;
73389 assert( (pCur->curFlags & BTCF_Pinned)==0 );
73390 pCur->curFlags |= BTCF_Pinned;
73393 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
73394 pCur->curFlags &= ~BTCF_Pinned;
73404 assert( pCur->eState==CURSOR_VALID );
73406 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
73407 (i64)(pCur->info.pPayload - pCur->pPage->aData);
73416 ** The caller must guarantee that the cursor is pointing to a non-NULL
73422 assert( pCur->eState==CURSOR_VALID );
73424 return pCur->info.nPayload;
73442 assert( pCur->eState==CURSOR_VALID );
73443 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
73449 ** linked list of overflow pages. If possible, it uses the auto-vacuum
73450 ** pointer-map data instead of reading the content of page ovfl to do so.
73462 ** the pointer-map was used to obtain the value for *pPgnoNext), then
73475 assert( sqlite3_mutex_held(pBt->mutex) );
73480 ** autovacuum pointer-map pages. Guess that the next page in
73485 if( pBt->autoVacuum ){
73509 next = get4byte(pPage->aData);
73537 int eOp, /* 0 -> copy from page, 1 -> copy to page */
73570 ** the overflow page-list cache array (BtCursor.aOverflow).
73574 ** Once an overflow page-list cache has been allocated, it must be
73576 ** the cursor is moved to a different row. Additionally, in auto-vacuum
73577 ** mode, the following events may invalidate an overflow page-list cache.
73588 int eOp /* zero to read. non-zero to write. */
73593 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
73594 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
73601 assert( pCur->eState==CURSOR_VALID );
73602 if( pCur->ix>=pPage->nCell ){
73608 aPayload = pCur->info.pPayload;
73609 assert( offset+amt <= pCur->info.nPayload );
73611 assert( aPayload > pPage->aData );
73612 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
73615 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
73622 if( offset<pCur->info.nLocal ){
73624 if( a+offset>pCur->info.nLocal ){
73625 a = pCur->info.nLocal - offset;
73627 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
73630 amt -= a;
73632 offset -= pCur->info.nLocal;
73637 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
73640 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
73649 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
73650 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
73651 if( pCur->aOverflow==0
73652 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
73655 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
73660 pCur->aOverflow = aNew;
73663 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
73664 pCur->curFlags |= BTCF_ValidOvfl;
73666 /* If the overflow page-list cache has been allocated and the
73670 if( pCur->aOverflow[offset/ovflSize] ){
73672 nextPage = pCur->aOverflow[iIdx];
73679 /* If required, populate the overflow page-list cache. */
73680 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
73681 assert( pCur->aOverflow[iIdx]==0
73682 || pCur->aOverflow[iIdx]==nextPage
73684 pCur->aOverflow[iIdx] = nextPage;
73690 ** page-list cache, if any, then fall back to the getOverflowPage()
73693 assert( pCur->curFlags & BTCF_ValidOvfl );
73694 assert( pCur->pBtree->db==pBt->db );
73695 if( pCur->aOverflow[iIdx+1] ){
73696 nextPage = pCur->aOverflow[iIdx+1];
73700 offset -= ovflSize;
73707 a = ovflSize - offset;
73715 ** 3) there are no dirty pages in the page-cache
73716 ** 4) the database is file-backed, and
73721 ** output buffer, bypassing the page-cache altogether. This speeds
73726 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
73727 && &pBuf[-4]>=pBufStart /* (6) */
73729 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
73731 u8 *aWrite = &pBuf[-4];
73734 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
73735 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
73743 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
73754 amt -= a;
73775 ** pCur can be pointing to either a table or an index b-tree.
73777 ** pCur is pointing to an index b-tree then the key section is read.
73789 assert( pCur->eState==CURSOR_VALID );
73790 assert( pCur->iPage>=0 && pCur->pPage );
73807 if ( pCur->eState==CURSOR_INVALID ){
73815 if( pCur->eState==CURSOR_VALID ){
73827 ** the key if index btrees (pPage->intKey==0) and is the data for
73828 ** table btrees (pPage->intKey==1). The number of bytes of available
73848 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
73849 assert( pCur->eState==CURSOR_VALID );
73850 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
73852 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
73853 assert( pCur->info.nSize>0 );
73854 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
73855 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
73856 amt = pCur->info.nLocal;
73857 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
73861 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
73864 return (void*)pCur->info.pPayload;
73871 ** b-tree page. Write the number of available bytes into *pAmt.
73891 ** This function returns SQLITE_CORRUPT if the page-header flags field of
73893 ** if an intkey page appears to be the parent of a non-intkey page, or
73894 ** vice-versa).
73898 assert( pCur->eState==CURSOR_VALID );
73899 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
73900 assert( pCur->iPage>=0 );
73901 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
73904 pCur->info.nSize = 0;
73905 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
73906 pCur->aiIdx[pCur->iPage] = pCur->ix;
73907 pCur->apPage[pCur->iPage] = pCur->pPage;
73908 pCur->ix = 0;
73909 pCur->iPage++;
73910 return getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur,
73911 pCur->curPagerFlags);
73916 ** Page pParent is an internal (non-leaf) tree page. This function
73917 ** asserts that page number iChild is the left-child if the iIdx'th
73919 ** cells in pParent, that page number iChild is the right-child of
73925 assert( iIdx<=pParent->nCell );
73926 if( iIdx==pParent->nCell ){
73927 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
73939 ** pCur->idx is set to the cell index that contains the pointer
73941 ** right-most child page then pCur->idx is set to one more than
73947 assert( pCur->eState==CURSOR_VALID );
73948 assert( pCur->iPage>0 );
73949 assert( pCur->pPage );
73951 pCur->apPage[pCur->iPage-1],
73952 pCur->aiIdx[pCur->iPage-1],
73953 pCur->pPage->pgno
73955 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
73956 pCur->info.nSize = 0;
73957 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
73958 pCur->ix = pCur->aiIdx[pCur->iPage-1];
73959 pLeaf = pCur->pPage;
73960 pCur->pPage = pCur->apPage[--pCur->iPage];
73965 ** Move the cursor to point to the root page of its b-tree structure.
73972 ** If the b-tree structure is empty, the cursor state is set to
73978 ** page-header flags indicate that the [virtual] root-page is the expected
73979 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
73981 ** indicating a table b-tree, or if the caller did specify a KeyInfo
73983 ** b-tree).
73993 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
73994 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
73996 if( pCur->iPage>=0 ){
73997 if( pCur->iPage ){
73998 releasePageNotNull(pCur->pPage);
73999 while( --pCur->iPage ){
74000 releasePageNotNull(pCur->apPage[pCur->iPage]);
74002 pRoot = pCur->pPage = pCur->apPage[0];
74005 }else if( pCur->pgnoRoot==0 ){
74006 pCur->eState = CURSOR_INVALID;
74009 assert( pCur->iPage==(-1) );
74010 if( pCur->eState>=CURSOR_REQUIRESEEK ){
74011 if( pCur->eState==CURSOR_FAULT ){
74012 assert( pCur->skipNext!=SQLITE_OK );
74013 return pCur->skipNext;
74017 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
74018 0, pCur->curPagerFlags);
74020 pCur->eState = CURSOR_INVALID;
74023 pCur->iPage = 0;
74024 pCur->curIntKey = pCur->pPage->intKey;
74026 pRoot = pCur->pPage;
74027 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
74029 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
74030 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
74031 ** NULL, the caller expects a table b-tree. If this is not the case,
74036 ** if pCur->iPage>=0). But this is not so if the database is corrupted
74037 ** in such a way that page pRoot is linked into a second b-tree table
74039 assert( pRoot->intKey==1 || pRoot->intKey==0 );
74040 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
74041 return SQLITE_CORRUPT_PAGE(pCur->pPage);
74045 pCur->ix = 0;
74046 pCur->info.nSize = 0;
74047 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
74049 if( pRoot->nCell>0 ){
74050 pCur->eState = CURSOR_VALID;
74051 }else if( !pRoot->leaf ){
74053 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
74054 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
74055 pCur->eState = CURSOR_VALID;
74058 pCur->eState = CURSOR_INVALID;
74065 ** Move the cursor down to the left-most leaf entry beneath the
74068 ** The left-most leaf is the one with the smallest key - the first
74077 assert( pCur->eState==CURSOR_VALID );
74078 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
74079 assert( pCur->ix<pPage->nCell );
74080 pgno = get4byte(findCell(pPage, pCur->ix));
74087 ** Move the cursor down to the right-most leaf entry beneath the
74090 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
74091 ** finds the right-most entry beneath the *page*.
74093 ** The right-most entry is the one with the largest key - the last
74102 assert( pCur->eState==CURSOR_VALID );
74103 while( !(pPage = pCur->pPage)->leaf ){
74104 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74105 pCur->ix = pPage->nCell;
74109 pCur->ix = pPage->nCell-1;
74110 assert( pCur->info.nSize==0 );
74111 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
74123 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74126 assert( pCur->pPage->nCell>0 );
74130 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74145 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74147 /* If the cursor already points to the last entry, this is a no-op. */
74148 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
74151 ** to the last entry in the b-tree. */
74153 for(ii=0; ii<pCur->iPage; ii++){
74154 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
74156 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
74157 testcase( pCur->ix!=pCur->pPage->nCell-1 );
74158 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
74159 assert( pCur->pPage->leaf );
74167 assert( pCur->eState==CURSOR_VALID );
74171 pCur->curFlags |= BTCF_AtLast;
74173 pCur->curFlags &= ~BTCF_AtLast;
74176 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74215 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74217 assert( pCur->pKeyInfo==0 );
74218 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
74222 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
74223 if( pCur->info.nKey==intKey ){
74227 if( pCur->info.nKey<intKey ){
74228 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
74229 *pRes = -1;
74236 if( pCur->info.nKey+1==intKey ){
74241 if( pCur->info.nKey==intKey ){
74252 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74258 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74259 *pRes = -1;
74264 assert( pCur->pPage );
74265 assert( pCur->pPage->isInit );
74266 assert( pCur->eState==CURSOR_VALID );
74267 assert( pCur->pPage->nCell > 0 );
74268 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
74269 assert( pCur->curIntKey );
74274 MemPage *pPage = pCur->pPage;
74277 /* pPage->nCell must be greater than zero. If this is the root-page
74279 ** not run. If this is not the root-page, then the moveToChild() routine
74281 ** be the right kind (index or table) of b-tree page. Otherwise
74283 assert( pPage->nCell>0 );
74284 assert( pPage->intKey );
74286 upr = pPage->nCell-1;
74288 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
74292 if( pPage->intKeyLeaf ){
74294 if( pCell>=pPage->aDataEnd ){
74302 if( lwr>upr ){ c = -1; break; }
74304 upr = idx-1;
74308 pCur->ix = (u16)idx;
74309 if( !pPage->leaf ){
74313 pCur->curFlags |= BTCF_ValidNKey;
74314 pCur->info.nKey = nCellKey;
74315 pCur->info.nSize = 0;
74323 assert( lwr==upr+1 || !pPage->leaf );
74324 assert( pPage->isInit );
74325 if( pPage->leaf ){
74326 assert( pCur->ix<pCur->pPage->nCell );
74327 pCur->ix = (u16)idx;
74333 if( lwr>=pPage->nCell ){
74334 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74338 pCur->ix = (u16)lwr;
74343 pCur->info.nSize = 0;
74344 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74349 ** Compare the "idx"-th cell on the page the cursor pCur is currently
74370 MemPage *pPage = pCur->pPage;
74376 if( nCell<=pPage->max1bytePayload ){
74377 /* This branch runs if the record-size field of the cell is a
74379 ** b-tree page. */
74380 testcase( pCell+nCell+1==pPage->aDataEnd );
74383 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
74385 /* The record-size field is a 2 byte varint and the record
74386 ** fits entirely on the main b-tree page. */
74387 testcase( pCell+nCell+2==pPage->aDataEnd );
74398 ** Return true (non-zero) if pCur is current pointing to the last
74403 assert( pCur->eState==CURSOR_VALID );
74404 for(i=0; i<pCur->iPage; i++){
74405 MemPage *pPage = pCur->apPage[i];
74406 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
74434 ** The pIdxKey->eqSeen field is set to 1 if there
74446 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74448 assert( pCur->pKeyInfo!=0 );
74451 pCur->pBtree->nSeek++; /* Performance measurement during testing */
74455 pIdxKey->errCode = 0;
74456 assert( pIdxKey->default_rc==1
74457 || pIdxKey->default_rc==0
74458 || pIdxKey->default_rc==-1
74473 if( pCur->eState==CURSOR_VALID
74474 && pCur->pPage->leaf
74478 if( pCur->ix==pCur->pPage->nCell-1
74479 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
74480 && pIdxKey->errCode==SQLITE_OK
74485 if( pCur->iPage>0
74487 && pIdxKey->errCode==SQLITE_OK
74489 pCur->curFlags &= ~BTCF_ValidOvfl;
74490 if( !pCur->pPage->isInit ){
74495 pIdxKey->errCode = SQLITE_OK;
74501 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
74502 *pRes = -1;
74509 assert( pCur->pPage );
74510 assert( pCur->pPage->isInit );
74511 assert( pCur->eState==CURSOR_VALID );
74512 assert( pCur->pPage->nCell > 0 );
74513 assert( pCur->curIntKey==0 );
74518 MemPage *pPage = pCur->pPage;
74521 /* pPage->nCell must be greater than zero. If this is the root-page
74523 ** not run. If this is not the root-page, then the moveToChild() routine
74525 ** be the right kind (index or table) of b-tree page. Otherwise
74527 assert( pPage->nCell>0 );
74528 assert( pPage->intKey==0 );
74530 upr = pPage->nCell-1;
74536 /* The maximum supported page-size is 65536 bytes. This means that
74537 ** the maximum number of record bytes stored on an index B-Tree
74538 ** page is less than 16384 bytes and may be stored as a 2-byte
74541 ** stored entirely within the b-tree page by inspecting the first
74545 if( nCell<=pPage->max1bytePayload ){
74546 /* This branch runs if the record-size field of the cell is a
74548 ** b-tree page. */
74549 testcase( pCell+nCell+1==pPage->aDataEnd );
74552 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
74554 /* The record-size field is a 2 byte varint and the record
74555 ** fits entirely on the main b-tree page. */
74556 testcase( pCell+nCell+2==pPage->aDataEnd );
74569 u8 * const pCellBody = pCell - pPage->childPtrSize;
74571 pPage->xParseCell(pPage, pCellBody, &pCur->info);
74572 nCell = (int)pCur->info.nKey;
74577 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
74586 pCur->ix = (u16)idx;
74589 pCur->curFlags &= ~BTCF_ValidOvfl;
74598 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
74599 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
74604 upr = idx-1;
74609 pCur->ix = (u16)idx;
74610 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
74617 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
74618 assert( pPage->isInit );
74619 if( pPage->leaf ){
74620 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
74621 pCur->ix = (u16)idx;
74626 if( lwr>=pPage->nCell ){
74627 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74631 pCur->ix = (u16)lwr;
74636 pCur->info.nSize = 0;
74637 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74654 return (CURSOR_VALID!=pCur->eState);
74667 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
74672 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
74673 if( NEVER(pCur->pPage->leaf==0) ) return -1;
74675 n = pCur->pPage->nCell;
74676 for(i=0; i<pCur->iPage; i++){
74677 n *= pCur->apPage[i]->nCell;
74708 if( pCur->eState!=CURSOR_VALID ){
74709 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
74714 if( CURSOR_INVALID==pCur->eState ){
74717 if( pCur->eState==CURSOR_SKIPNEXT ){
74718 pCur->eState = CURSOR_VALID;
74719 if( pCur->skipNext>0 ) return SQLITE_OK;
74723 pPage = pCur->pPage;
74724 idx = ++pCur->ix;
74725 if( NEVER(!pPage->isInit) || sqlite3FaultSim(412) ){
74729 if( idx>=pPage->nCell ){
74730 if( !pPage->leaf ){
74731 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
74736 if( pCur->iPage==0 ){
74737 pCur->eState = CURSOR_INVALID;
74741 pPage = pCur->pPage;
74742 }while( pCur->ix>=pPage->nCell );
74743 if( pPage->intKey ){
74749 if( pPage->leaf ){
74760 pCur->info.nSize = 0;
74761 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
74762 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
74763 pPage = pCur->pPage;
74764 if( (++pCur->ix)>=pPage->nCell ){
74765 pCur->ix--;
74768 if( pPage->leaf ){
74800 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
74801 assert( pCur->info.nSize==0 );
74802 if( pCur->eState!=CURSOR_VALID ){
74807 if( CURSOR_INVALID==pCur->eState ){
74810 if( CURSOR_SKIPNEXT==pCur->eState ){
74811 pCur->eState = CURSOR_VALID;
74812 if( pCur->skipNext<0 ) return SQLITE_OK;
74816 pPage = pCur->pPage;
74817 assert( pPage->isInit );
74818 if( !pPage->leaf ){
74819 int idx = pCur->ix;
74824 while( pCur->ix==0 ){
74825 if( pCur->iPage==0 ){
74826 pCur->eState = CURSOR_INVALID;
74831 assert( pCur->info.nSize==0 );
74832 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
74834 pCur->ix--;
74835 pPage = pCur->pPage;
74836 if( pPage->intKey && !pPage->leaf ){
74848 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
74849 pCur->info.nSize = 0;
74850 if( pCur->eState!=CURSOR_VALID
74851 || pCur->ix==0
74852 || pCur->pPage->leaf==0
74856 pCur->ix--;
74877 ** anywhere on the free-list, then it is guaranteed to be returned. If
74897 assert( sqlite3_mutex_held(pBt->mutex) );
74898 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
74899 pPage1 = pBt->pPage1;
74901 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
74903 n = get4byte(&pPage1->aData[36]);
74904 testcase( n==mxPage-1 );
74911 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
74914 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
74915 ** shows that the page 'nearby' is somewhere on the free-list, then
74916 ** the entire-list will be searched for that page.
74923 assert( pBt->autoVacuum );
74935 /* Decrement the free-list count by 1. Set iTrunk to the index of the
74936 ** first free-list trunk page. iPrevTrunk is initially 1.
74938 rc = sqlite3PagerWrite(pPage1->pDbPage);
74940 put4byte(&pPage1->aData[36], n-1);
74943 ** is not true. Otherwise, it runs once for each trunk-page on the
74944 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
74950 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
74953 iTrunk = get4byte(&pPrevTrunk->aData[0]);
74955 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
74958 iTrunk = get4byte(&pPage1->aData[32]);
74962 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
74971 assert( pTrunk->aData!=0 );
74972 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
74974 k = get4byte(&pTrunk->aData[4]);
74980 rc = sqlite3PagerWrite(pTrunk->pDbPage);
74985 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
74988 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
74989 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
75003 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75009 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
75011 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75015 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
75019 ** pointers to free-list leaves. The first leaf becomes a trunk
75023 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
75033 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
75038 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
75039 put4byte(&pNewTrunk->aData[4], k-1);
75040 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
75043 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
75044 put4byte(&pPage1->aData[32], iNewTrunk);
75046 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
75050 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
75054 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
75060 unsigned char *aData = pTrunk->aData;
75074 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
75076 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
75101 *pPgno, closest+1, k, pTrunk->pgno, n-1));
75102 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75104 if( closest<k-1 ){
75107 put4byte(&aData[4], k-1);
75111 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75128 ** pager layer with the 'no-content' flag set. This prevents the pager
75130 ** current transaction has already run one or more incremental-vacuum
75133 ** not set the no-content flag. This causes the pager to load and journal
75138 ** file on disk. So the effects of disabling the no-content optimization
75142 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
75144 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75146 pBt->nPage++;
75147 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
75150 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
75151 /* If *pPgno refers to a pointer-map page, allocate two new pages
75153 ** becomes a new pointer-map page, the second is used by the caller.
75156 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
75157 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
75158 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
75160 rc = sqlite3PagerWrite(pPg->pDbPage);
75164 pBt->nPage++;
75165 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
75168 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
75169 *pPgno = pBt->nPage;
75174 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
75187 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
75188 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
75193 ** This function is used to add page iPage to the database file free-list.
75194 ** It is assumed that the page is not already a part of the free-list.
75205 MemPage *pTrunk = 0; /* Free-list trunk page */
75206 Pgno iTrunk = 0; /* Page number of free-list trunk page */
75207 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
75210 u32 nFree; /* Initial number of pages on free-list */
75212 assert( sqlite3_mutex_held(pBt->mutex) );
75214 assert( !pMemPage || pMemPage->pgno==iPage );
75216 if( iPage<2 || iPage>pBt->nPage ){
75221 sqlite3PagerRef(pPage->pDbPage);
75227 rc = sqlite3PagerWrite(pPage1->pDbPage);
75229 nFree = get4byte(&pPage1->aData[36]);
75230 put4byte(&pPage1->aData[36], nFree+1);
75232 if( pBt->btsFlags & BTS_SECURE_DELETE ){
75237 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
75241 memset(pPage->aData, 0, pPage->pBt->pageSize);
75244 /* If the database supports auto-vacuum, write an entry in the pointer-map
75252 /* Now manipulate the actual database free-list structure. There are two
75253 ** possibilities. If the free-list is currently empty, or if the first
75254 ** trunk page in the free-list is full, then this page will become a
75255 ** new free-list trunk page. Otherwise, it will become a leaf of the
75256 ** first trunk page in the current free-list. This block tests if it
75257 ** is possible to add the page as a new free-list leaf.
75262 iTrunk = get4byte(&pPage1->aData[32]);
75272 nLeaf = get4byte(&pTrunk->aData[4]);
75273 assert( pBt->usableSize>32 );
75274 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
75278 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
75283 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
75286 ** usableSize/4 - 8 entries will be reported as corrupt. In order
75288 ** we will continue to restrict the number of entries to usableSize/4 - 8
75291 ** to read "usableSize/4-2" instead of "usableSize/4-8".
75293 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
75298 rc = sqlite3PagerWrite(pTrunk->pDbPage);
75300 put4byte(&pTrunk->aData[4], nLeaf+1);
75301 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
75302 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
75303 sqlite3PagerDontWrite(pPage->pDbPage);
75307 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
75313 ** the page being freed as a leaf page of the first trunk in the free-list.
75314 ** Possibly because the free-list is empty, or possibly because the
75315 ** first trunk in the free-list is full. Either way, the page being freed
75316 ** will become the new first trunk page in the free-list.
75321 rc = sqlite3PagerWrite(pPage->pDbPage);
75325 put4byte(pPage->aData, iTrunk);
75326 put4byte(&pPage->aData[4], 0);
75327 put4byte(&pPage1->aData[32], iPage);
75328 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
75332 pPage->isInit = 0;
75340 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
75358 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75359 assert( pInfo->nLocal!=pInfo->nPayload );
75360 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
75361 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
75362 if( pCell + pInfo->nSize > pPage->aDataEnd ){
75366 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
75367 pBt = pPage->pBt;
75368 assert( pBt->usableSize > 4 );
75369 ovflPageSize = pBt->usableSize - 4;
75370 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
75372 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
75374 while( nOvfl-- ){
75389 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
75396 ** freePage2() may zero the page contents if secure-delete mode is
75407 sqlite3PagerUnref(pOvfl->pDbPage);
75422 pPage->xParseCell(pPage, pCell, &sInfo); \
75437 ** Note that pCell does not necessary need to point to the pPage->aData
75439 ** be constructed in this temporary area then copied into pPage->aData
75459 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75463 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
75464 || sqlite3PagerIswriteable(pPage->pDbPage) );
75467 nHeader = pPage->childPtrSize;
75468 if( pPage->intKey ){
75469 nPayload = pX->nData + pX->nZero;
75470 pSrc = pX->pData;
75471 nSrc = pX->nData;
75472 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
75474 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
75476 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
75477 nSrc = nPayload = (int)pX->nKey;
75478 pSrc = pX->pKey;
75484 if( nPayload<=pPage->maxLocal ){
75495 memset(pPayload+nSrc, 0, nPayload-nSrc);
75502 mn = pPage->minLocal;
75503 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
75504 testcase( n==pPage->maxLocal );
75505 testcase( n==pPage->maxLocal+1 );
75506 if( n > pPage->maxLocal ) n = mn;
75512 pBt = pPage->pBt;
75529 pPage->xParseCell(pPage, pCell, &info);
75530 assert( nHeader==(int)(info.pPayload - pCell) );
75531 assert( info.nKey==pX->nKey );
75544 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
75548 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
75549 || sqlite3PagerIswriteable(pPage->pDbPage) );
75559 nPayload -= n;
75563 nSrc -= n;
75564 spaceLeft -= n;
75568 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
75569 if( pBt->autoVacuum ){
75579 /* If the database supports auto-vacuum, and the second or subsequent
75580 ** overflow page is being allocated, add an entry to the pointer-map
75584 ** to the pointer-map. If we write nothing to this pointer-map slot,
75589 if( pBt->autoVacuum && rc==SQLITE_OK ){
75604 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
75608 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
75609 || sqlite3PagerIswriteable(pPage->pDbPage) );
75614 pPrior = pOvfl->aData;
75616 pPayload = &pOvfl->aData[4];
75617 spaceLeft = pBt->usableSize - 4;
75625 ** Remove the i-th cell from pPage. This routine effects pPage only.
75634 u8 *data; /* pPage->aData */
75641 assert( idx<pPage->nCell );
75643 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75644 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75645 assert( pPage->nFree>=0 );
75646 data = pPage->aData;
75647 ptr = &pPage->aCellIdx[2*idx];
75648 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
75650 hdr = pPage->hdrOffset;
75652 testcase( pc+sz==pPage->pBt->usableSize );
75653 if( pc+sz > pPage->pBt->usableSize ){
75662 pPage->nCell--;
75663 if( pPage->nCell==0 ){
75666 put2byte(&data[hdr+5], pPage->pBt->usableSize);
75667 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
75668 - pPage->childPtrSize - 8;
75670 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
75671 put2byte(&data[hdr+3], pPage->nCell);
75672 pPage->nFree += 2;
75683 ** in pPage->apOvfl[] and make it point to the cell content (either
75685 ** Allocating a new entry in pPage->aCell[] implies that
75686 ** pPage->nOverflow is incremented.
75692 int i, /* New cell becomes the i-th cell of the page */
75696 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
75702 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
75705 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
75706 assert( MX_CELL(pPage->pBt)<=10921 );
75707 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
75708 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
75709 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
75710 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75711 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
75712 assert( pPage->nFree>=0 );
75713 if( pPage->nOverflow || sz+2>pPage->nFree ){
75721 j = pPage->nOverflow++;
75722 /* Comparison against ArraySize-1 since we hold back one extra slot
75725 assert( j < ArraySize(pPage->apOvfl)-1 );
75726 pPage->apOvfl[j] = pCell;
75727 pPage->aiOvfl[j] = (u16)i;
75734 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
75735 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
75737 int rc = sqlite3PagerWrite(pPage->pDbPage);
75742 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75743 data = pPage->aData;
75744 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
75750 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
75751 assert( idx+sz <= (int)pPage->pBt->usableSize );
75752 pPage->nFree -= (u16)(2 + sz);
75759 memcpy(&data[idx+4], pCell+4, sz-4);
75764 pIns = pPage->aCellIdx + i*2;
75765 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
75767 pPage->nCell++;
75769 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
75770 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
75772 if( pPage->pBt->autoVacuum ){
75792 ** The value of NN appears to give the best results overall.
75795 ** tunable - as if you could change them and recompile and it would all work.
75810 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
75822 ** -----------
75824 ** -----------
75827 ** --------- --------- ---------
75828 ** |Child-1| |Child-2| |Child-3|
75829 ** --------- --------- ---------
75833 ** 1. All cells from Child-1 in order
75835 ** 3. All cells from Child-2 in order
75837 ** 5. All cells from Child-3 in order
75839 ** For a table-btree (with rowids) the items 2 and 4 are empty because
75843 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
75847 ** ixNx[0] = Number of cells in Child-1.
75848 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
75849 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
75850 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
75853 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
75856 ** ixNx[0] = Number of cells in Child-1.
75857 ** ixNx[1] = Number of cells in Child-1 and Child-2.
75876 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
75880 assert( idx>=0 && idx+N<=p->nCell );
75882 assert( p->apCell[idx]!=0 );
75883 if( p->szCell[idx]==0 ){
75884 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
75887 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
75890 N--;
75898 assert( N>=0 && N<p->nCell );
75899 assert( p->szCell[N]==0 );
75900 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
75901 return p->szCell[N];
75904 assert( N>=0 && N<p->nCell );
75905 if( p->szCell[N] ) return p->szCell[N];
75910 ** Array apCell[] contains pointers to nCell b-tree page cells. The
75928 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
75929 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
75930 const int usableSize = pPg->pBt->usableSize;
75935 u8 *pCellptr = pPg->aCellIdx;
75936 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
75938 int k; /* Current slot in pCArray->apEnd[] */
75939 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
75944 memcpy(&pTmp[j], &aData[j], usableSize - j);
75946 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
75947 pSrcEnd = pCArray->apEnd[k];
75951 u8 *pCell = pCArray->apCell[i];
75952 u16 sz = pCArray->szCell[i];
75956 pCell = &pTmp[pCell - aData];
75963 pData -= sz;
75964 put2byte(pCellptr, (pData - aData));
75968 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
75971 if( pCArray->ixNx[k]<=i ){
75973 pSrcEnd = pCArray->apEnd[k];
75977 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
75978 pPg->nCell = nCell;
75979 pPg->nOverflow = 0;
75982 put2byte(&aData[hdr+3], pPg->nCell);
75983 put2byte(&aData[hdr+5], pData - aData);
75989 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
75992 ** will fit), non-zero is returned. Otherwise, if the cells are added
75995 ** Argument pCellptr points to the first entry in the cell-pointer array
75997 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
75999 ** that it is safe to overwrite this part of the cell-pointer array.
76007 ** end of the space required by this page for the cell-pointer area (for
76008 ** all cells - not just those inserted by the current call). If the content
76010 ** cells in apCell[], then the cells do not fit and non-zero is returned.
76014 u8 *pBegin, /* End of cell-pointer array */
76015 u8 **ppData, /* IN/OUT: Page content-area pointer */
76016 u8 *pCellptr, /* Pointer to cell-pointer area */
76021 int i = iFirst; /* Loop counter - cell index to insert */
76022 u8 *aData = pPg->aData; /* Complete page */
76025 int k; /* Current slot in pCArray->apEnd[] */
76027 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
76029 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
76030 pEnd = pCArray->apEnd[k];
76034 assert( pCArray->szCell[i]!=0 );
76035 sz = pCArray->szCell[i];
76037 if( (pData - pBegin)<sz ) return 1;
76038 pData -= sz;
76041 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
76044 assert( (pSlot+sz)<=pCArray->apCell[i]
76045 || pSlot>=(pCArray->apCell[i]+sz)
76047 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
76048 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
76054 memmove(pSlot, pCArray->apCell[i], sz);
76055 put2byte(pCellptr, (pSlot - aData));
76059 if( pCArray->ixNx[k]<=i ){
76061 pEnd = pCArray->apEnd[k];
76069 ** The pCArray object contains pointers to b-tree cells and their sizes.
76072 ** that is currently stored within the body of pPg to the pPg free-list.
76073 ** The cell-pointers and other fields of the page are not updated.
76075 ** This function returns the total number of cells added to the free-list.
76083 u8 * const aData = pPg->aData;
76084 u8 * const pEnd = &aData[pPg->pBt->usableSize];
76085 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
76093 u8 *pCell = pCArray->apCell[i];
76099 sz = pCArray->szCell[i]; assert( sz>0 );
76102 assert( pFree>aData && (pFree - aData)<65536 );
76103 freeSpace(pPg, (u16)(pFree - aData), szFree);
76118 assert( pFree>aData && (pFree - aData)<65536 );
76119 freeSpace(pPg, (u16)(pFree - aData), szFree);
76126 ** balanced. The current page, pPg, has pPg->nCell cells starting with
76127 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
76133 ** The pPg->nFree field is invalid when this function returns. It is the
76143 u8 * const aData = pPg->aData;
76144 const int hdr = pPg->hdrOffset;
76145 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
76146 int nCell = pPg->nCell; /* Cells stored on pPg */
76150 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
76154 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
76155 memcpy(pTmp, aData, pPg->pBt->usableSize);
76161 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
76163 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
76164 nCell -= nShift;
76167 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
76169 nCell -= nTail;
76174 if( pData>pPg->aDataEnd ) goto editpage_fail;
76178 int nAdd = MIN(nNew,iOld-iNew);
76179 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
76181 pCellptr = pPg->aCellIdx;
76191 for(i=0; i<pPg->nOverflow; i++){
76192 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
76194 pCellptr = &pPg->aCellIdx[iCell * 2];
76196 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
76209 pCellptr = &pPg->aCellIdx[nCell*2];
76212 iNew+nCell, nNew-nCell, pCArray
76215 pPg->nCell = nNew;
76216 pPg->nOverflow = 0;
76218 put2byte(&aData[hdr+3], pPg->nCell);
76219 put2byte(&aData[hdr+5], pData - aData);
76223 u8 *pCell = pCArray->apCell[i+iNew];
76224 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
76225 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
76226 pCell = &pTmp[pCell - aData];
76229 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
76244 ** a new entry is being inserted on the extreme right-end of the
76248 ** Instead of trying to balance the 3 right-most leaf pages, just add
76249 ** a new page to the right-hand side and put the one new entry in
76255 ** pPage is the leaf page which is the right-most page in the tree.
76257 ** which is also the right-most entry on the page.
76266 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
76271 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
76272 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76273 assert( pPage->nOverflow==1 );
76275 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
76276 assert( pPage->nFree>=0 );
76277 assert( pParent->nFree>=0 );
76279 /* Allocate a new page. This page will become the right-sibling of
76288 u8 *pCell = pPage->apOvfl[0];
76289 u16 szCell = pPage->xCellSize(pPage, pCell);
76293 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
76294 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
76300 b.apEnd[0] = pPage->aDataEnd;
76307 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
76309 /* If this is an auto-vacuum database, update the pointer map
76319 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
76320 if( szCell>pNew->minLocal ){
76326 ** consists of a 4-byte page number (the page number of pPage) and
76330 ** To find the largest key value on pPage, first find the right-most
76332 ** record-length (a variable length integer at most 32-bits in size)
76334 ** The first of the while(...) loops below skips over the record-length
76338 pCell = findCell(pPage, pPage->nCell-1);
76346 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
76347 0, pPage->pgno, &rc);
76350 /* Set the right-child pointer of pParent to point to the new page. */
76351 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
76365 ** for setting pointer-map entries.
76373 BtShared *pBt = pPage->pBt;
76374 assert( pPage->isInit );
76376 for(j=0; j<pPage->nCell; j++){
76381 pPage->xParseCell(pPage, z, &info);
76383 Pgno ovfl = get4byte(&z[info.nSize-4]);
76385 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
76387 if( !pPage->leaf ){
76390 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76393 if( !pPage->leaf ){
76394 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
76396 assert( n==pPage->pgno && e==PTRMAP_BTREE );
76404 ** This function is used to copy the contents of the b-tree node stored
76406 ** the pointer-map entries for each child page are updated so that the
76422 BtShared * const pBt = pFrom->pBt;
76423 u8 * const aFrom = pFrom->aData;
76424 u8 * const aTo = pTo->aData;
76425 int const iFromHdr = pFrom->hdrOffset;
76426 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
76431 assert( pFrom->isInit );
76432 assert( pFrom->nFree>=iToHdr );
76433 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
76435 /* Copy the b-tree node content from page pFrom to page pTo. */
76437 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
76438 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
76445 pTo->isInit = 0;
76453 /* If this is an auto-vacuum database, update the pointer-map entries
76454 ** for any b-tree or overflow pages that pTo now contains the pointers to.
76496 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
76505 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
76506 int isRoot, /* True if pParent is a root-page */
76514 int nxDiv; /* Next divider slot in pParent->aCell[] */
76519 int pageFlags; /* Value of pPage->aData[0] */
76525 u8 *pRight; /* Location in parent of right-sibling pointer */
76526 u8 *apDiv[NB-1]; /* Divider cells in pParent */
76527 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
76529 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
76538 pBt = pParent->pBt;
76539 assert( sqlite3_mutex_held(pBt->mutex) );
76540 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
76547 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
76548 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
76553 assert( pParent->nFree>=0 );
76566 i = pParent->nOverflow + pParent->nCell;
76574 nxDiv = i-2+bBulk;
76576 nxDiv = iParentIdx-1;
76578 i = 2-bBulk;
76581 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
76582 pRight = &pParent->aData[pParent->hdrOffset+8];
76584 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
76595 if( apOld[i]->nFree<0 ){
76602 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
76603 if( (i--)==0 ) break;
76605 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
76606 apDiv[i] = pParent->apOvfl[0];
76608 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
76609 pParent->nOverflow = 0;
76611 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
76613 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
76622 ** But not if we are in secure-delete mode. In secure-delete mode,
76627 if( pBt->btsFlags & BTS_FAST_SECURE ){
76632 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
76633 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
76635 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
76638 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
76642 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
76652 + pBt->pageSize; /* aSpace1 */
76654 assert( szScratch<=7*(int)pBt->pageSize );
76681 leafCorrection = b.pRef->leaf*4;
76682 leafData = b.pRef->intKeyLeaf;
76685 int limit = pOld->nCell;
76686 u8 *aData = pOld->aData;
76687 u16 maskPage = pOld->maskPage;
76688 u8 *piCell = aData + pOld->cellOffset;
76692 /* Verify that all sibling pages are of the same "type" (table-leaf,
76693 ** table-interior, index-leaf, or index-interior).
76695 if( pOld->aData[0]!=apOld[0]->aData[0] ){
76717 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
76718 if( pOld->nOverflow>0 ){
76719 if( NEVER(limit<pOld->aiOvfl[0]) ){
76723 limit = pOld->aiOvfl[0];
76729 for(k=0; k<pOld->nOverflow; k++){
76730 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
76731 b.apCell[b.nCell] = pOld->apOvfl[k];
76735 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
76742 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
76745 if( i<nOld-1 && !leafData){
76752 assert( sz<=pBt->maxLocal+23 );
76753 assert( iSpace1 <= (int)pBt->pageSize );
76757 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
76758 if( !pOld->leaf ){
76760 assert( pOld->hdrOffset==0 || CORRUPT_DB );
76763 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
76770 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
76782 ** size of all cells on the i-th page and cntNew[] which is the index
76789 ** szNew[i]: Spaced used on the i-th sibling page.
76791 ** the right of the i-th sibling page.
76795 usableSpace = pBt->usableSize - 12 + leafCorrection;
76798 b.apEnd[k] = p->aDataEnd;
76800 if( k && b.ixNx[k]==b.ixNx[k-1] ){
76801 k--; /* Omit b.ixNx[] entry for child pages with no cells */
76805 b.apEnd[k] = pParent->aDataEnd;
76808 assert( p->nFree>=0 );
76809 szNew[i] = usableSpace - p->nFree;
76810 for(j=0; j<p->nOverflow; j++){
76811 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
76822 szNew[k-1] = 0;
76823 cntNew[k-1] = b.nCell;
76825 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
76826 szNew[i] -= sz;
76835 cntNew[i]--;
76849 szNew[i+1] -= sz;
76853 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
76862 ** always nearly full, while the right-most sibling might be nearly empty.
76867 ** be so out of balance as to be illegal. For example, the right-most
76870 for(i=k-1; i>0; i--){
76872 int szLeft = szNew[i-1]; /* Size of sibling on the left */
76873 int r; /* Index of right-most cell in left sibling */
76876 r = cntNew[i-1] - 1;
76877 d = r + 1 - leafData;
76884 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
76888 szLeft -= b.szCell[r] + 2;
76889 cntNew[i-1] = r;
76890 r--;
76891 d--;
76894 szNew[i-1] = szLeft;
76895 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
76901 /* Sanity check: For a non-corrupt database file one of the follwing
76908 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
76910 apOld[0]->pgno, apOld[0]->nCell,
76911 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
76912 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
76918 pageFlags = apOld[0]->aData[0];
76924 rc = sqlite3PagerWrite(pNew->pDbPage);
76926 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
76941 /* Set the pointer-map entry for the new sibling page. */
76943 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
76964 aPgno[i] = apNew[i]->pgno;
76965 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
76966 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
76968 for(i=0; i<nNew-1; i++){
76971 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
76980 Pgno pgnoA = apNew[i]->pgno;
76981 Pgno pgnoB = apNew[iB]->pgno;
76982 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
76983 u16 fgA = apNew[i]->pDbPage->flags;
76984 u16 fgB = apNew[iB]->pDbPage->flags;
76985 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
76986 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
76987 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
76988 apNew[i]->pgno = pgnoB;
76989 apNew[iB]->pgno = pgnoA;
76995 apNew[0]->pgno, szNew[0], cntNew[0],
76996 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
76997 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
76998 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
76999 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
77000 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
77001 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
77002 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
77003 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
77006 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77008 assert( apNew[nNew-1]!=0 );
77009 put4byte(pRight, apNew[nNew-1]->pgno);
77011 /* If the sibling pages are not leaves, ensure that the right-child pointer
77012 ** of the right-most new sibling page is set to the value that was
77013 ** originally in the same field of the right-most old sibling page. */
77015 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
77016 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
77031 ** associated with the right-child of each sibling may also need to be
77038 int cntOldNext = pNew->nCell + pNew->nOverflow;
77049 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
77063 || pNew->pgno!=aPgno[iOld]
77064 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
77067 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
77069 if( cachedCellSize(&b,i)>pNew->minLocal ){
77078 for(i=0; i<nNew-1; i++){
77091 if( !pNew->leaf ){
77092 memcpy(&pNew->aData[8], pCell, 4);
77094 /* If the tree is a leaf-data tree, and the siblings are leaves,
77096 ** cell consists of the integer key for the right-most cell of
77097 ** the sibling-page assembled above only.
77100 j--;
77101 pNew->xParseCell(pNew, b.apCell[j], &info);
77106 pCell -= 4;
77107 /* Obscure case for non-leaf-data trees: If the cell at pCell was
77114 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
77120 sz = pParent->xCellSize(pParent, pCell);
77124 assert( sz<=pBt->maxLocal+23 );
77125 assert( iOvflSpace <= (int)pBt->pageSize );
77132 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
77134 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
77141 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
77143 ** the left-hand sibling apNew[iPg-1] has been updated.
77147 ** the right-hand sibling apNew[iPg+1] has been updated.
77151 ** The iPg value in the following loop starts at nNew-1 goes down
77152 ** to 0, then back up to nNew-1 again, thus making two passes over
77159 for(i=1-nNew; i<nNew; i++){
77160 int iPg = i<0 ? -i : i;
77164 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
77171 ** only after iPg-1 has already been updated. */
77172 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
77182 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
77183 iNew = cntNew[iPg-1] + !leafData;
77184 nNewCell = cntNew[iPg] - iNew;
77190 apNew[iPg]->nFree = usableSpace-szNew[iPg];
77191 assert( apNew[iPg]->nOverflow==0 );
77192 assert( apNew[iPg]->nCell==nNewCell );
77202 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
77203 /* The root page of the b-tree now contains no cells. The only sibling
77204 ** page is the right-child of the parent. Copy the contents of the
77205 ** child page into the parent, decreasing the overall height of the
77206 ** b-tree structure by one. This is described as the "balance-shallower"
77207 ** sub-algorithm in some documentation.
77209 ** If this is an auto-vacuum database, the call to copyNodeContent()
77210 ** sets all pointer-map entries corresponding to database image pages
77219 rc = defragmentPage(apNew[0], -1);
77221 assert( apNew[0]->nFree ==
77222 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
77223 - apNew[0]->nCell*2)
77229 /* Fix the pointer map entries associated with the right-child of each
77233 u32 key = get4byte(&apNew[i]->aData[8]);
77234 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
77238 assert( pParent->isInit );
77249 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
77276 ** This function is called when the root page of a b-tree structure is
77281 ** page is then overwritten to make it an empty page with the right-child
77284 ** Before returning, all pointer-map entries corresponding to pages
77285 ** that the new child-page now contains pointers to are updated. The
77286 ** entry corresponding to the new right-child pointer of the root
77298 BtShared *pBt = pRoot->pBt; /* The BTree */
77300 assert( pRoot->nOverflow>0 );
77301 assert( sqlite3_mutex_held(pBt->mutex) );
77303 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
77304 ** page that will become the new right-child of pPage. Copy the contents
77307 rc = sqlite3PagerWrite(pRoot->pDbPage);
77309 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
77312 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
77320 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
77321 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
77322 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
77324 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
77327 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
77328 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
77329 memcpy(pChild->apOvfl, pRoot->apOvfl,
77330 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
77331 pChild->nOverflow = pRoot->nOverflow;
77333 /* Zero the contents of pRoot. Then install pChild as the right-child. */
77334 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
77335 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
77343 ** on the same B-tree as pCur.
77346 ** pointing to the same b-tree. If an insert occurs on one SQL table
77348 ** table linked to the same b-tree. If the secondary insert causes a
77354 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
77356 && pOther->eState==CURSOR_VALID
77357 && pOther->pPage==pCur->pPage
77385 MemPage *pPage = pCur->pPage;
77387 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
77388 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
77394 }else if( (iPage = pCur->iPage)==0 ){
77395 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
77396 /* The root page of the b-tree is overfull. In this case call the
77397 ** balance_deeper() function to create a new child for the root-page
77398 ** and copy the current contents of the root-page to it. The
77399 ** next iteration of the do-loop will balance the child page.
77403 rc = balance_deeper(pPage, &pCur->apPage[1]);
77405 pCur->iPage = 1;
77406 pCur->ix = 0;
77407 pCur->aiIdx[0] = 0;
77408 pCur->apPage[0] = pPage;
77409 pCur->pPage = pCur->apPage[1];
77410 assert( pCur->pPage->nOverflow );
77415 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
77421 MemPage * const pParent = pCur->apPage[iPage-1];
77422 int const iIdx = pCur->aiIdx[iPage-1];
77424 rc = sqlite3PagerWrite(pParent->pDbPage);
77425 if( rc==SQLITE_OK && pParent->nFree<0 ){
77430 if( pPage->intKeyLeaf
77431 && pPage->nOverflow==1
77432 && pPage->aiOvfl[0]==pPage->nCell
77433 && pParent->pgno!=1
77434 && pParent->nCell==iIdx
77439 ** happens, the next iteration of the do-loop will balance pParent
77458 ** become overfull or underfull. The next iteration of the do-loop
77463 ** A subsequent iteration of the do-loop will deal with this by
77465 ** but it doesn't deal with overflow cells - just moves them to a
77472 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
77474 pCur->hints&BTREE_BULKLOAD);
77490 pPage->nOverflow = 0;
77492 /* The next iteration of the do-loop balances the parent page. */
77494 pCur->iPage--;
77495 assert( pCur->iPage>=0 );
77496 pCur->pPage = pCur->apPage[pCur->iPage];
77516 int nData = pX->nData - iOffset;
77522 int rc = sqlite3PagerWrite(pPage->pDbPage);
77524 memset(pDest + i, 0, iAmt - i);
77531 iAmt-nData);
77535 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
77536 int rc = sqlite3PagerWrite(pPage->pDbPage);
77542 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
77553 int iOffset; /* Next byte of pX->pData to write */
77554 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
77556 MemPage *pPage = pCur->pPage; /* Page being written */
77561 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
77562 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
77567 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
77568 0, pCur->info.nLocal);
77570 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
77573 iOffset = pCur->info.nLocal;
77576 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
77577 pBt = pPage->pBt;
77578 ovflPageSize = pBt->usableSize - 4;
77582 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
77586 ovflPgno = get4byte(pPage->aData);
77588 ovflPageSize = nTotal - iOffset;
77590 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
77593 sqlite3PagerUnref(pPage->pDbPage);
77616 ** If the seekResult parameter is non-zero, then a successful call to
77627 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
77628 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
77638 int loc = seekResult; /* -1: before desired location +1: after */
77642 Btree *p = pCur->pBtree;
77643 BtShared *pBt = p->pBt;
77648 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
77652 ** In some cases, the call to btreeMoveto() below is a no-op. For
77653 ** example, when inserting data into a table with auto-generated integer
77656 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
77661 if( pCur->curFlags & BTCF_Multiple ){
77662 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
77664 if( loc && pCur->iPage<0 ){
77677 if( pCur->eState>=CURSOR_REQUIRESEEK ){
77678 testcase( pCur->eState==CURSOR_REQUIRESEEK );
77679 testcase( pCur->eState==CURSOR_FAULT );
77685 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
77686 && pBt->inTransaction==TRANS_WRITE
77687 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
77688 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
77691 ** expecting an index b-tree, then the caller should be inserting blob
77695 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
77697 if( pCur->pKeyInfo==0 ){
77698 assert( pX->pKey==0 );
77699 /* If this is an insert into a table b-tree, invalidate any incrblob
77701 if( p->hasIncrblobCur ){
77702 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
77710 assert( pCur->curFlags & BTCF_ValidNKey );
77711 assert( pX->nKey==pCur->info.nKey );
77720 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
77723 assert( pX->nData>=0 && pX->nZero>=0 );
77724 if( pCur->info.nSize!=0
77725 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
77736 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
77754 if( pX->nMem ){
77756 r.pKeyInfo = pCur->pKeyInfo;
77757 r.aMem = pX->aMem;
77758 r.nField = pX->nMem;
77763 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
77775 if( pCur->info.nKey==pX->nKey ){
77777 x2.pData = pX->pKey;
77778 x2.nData = pX->nKey;
77784 assert( pCur->eState==CURSOR_VALID
77785 || (pCur->eState==CURSOR_INVALID && loc) );
77787 pPage = pCur->pPage;
77788 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
77789 assert( pPage->leaf || !pPage->intKey );
77790 if( pPage->nFree<0 ){
77791 if( NEVER(pCur->eState>CURSOR_INVALID) ){
77792 /* ^^^^^--- due to the moveToRoot() call above */
77801 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
77803 assert( pPage->isInit || CORRUPT_DB );
77804 newCell = pBt->pTmpSpace;
77808 szNew = pBt->nPreformatSize;
77810 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
77812 pPage->xParseCell(pPage, newCell, &info);
77814 Pgno ovfl = get4byte(&newCell[szNew-4]);
77815 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
77822 assert( szNew==pPage->xCellSize(pPage, newCell) );
77824 idx = pCur->ix;
77828 if( idx>=pPage->nCell ){
77831 rc = sqlite3PagerWrite(pPage->pDbPage);
77836 if( !pPage->leaf ){
77840 testcase( pCur->curFlags & BTCF_ValidOvfl );
77843 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
77853 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
77855 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
77858 if( oldCell+szNew > pPage->aDataEnd ){
77866 }else if( loc<0 && pPage->nCell>0 ){
77867 assert( pPage->leaf );
77868 idx = ++pCur->ix;
77869 pCur->curFlags &= ~BTCF_ValidNKey;
77871 assert( pPage->leaf );
77874 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
77875 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
77889 ** multiple records into an intkey b-tree using a single cursor (as can
77892 ** the b-tree if possible. If the cursor is left pointing to the last
77897 pCur->info.nSize = 0;
77898 if( pPage->nOverflow ){
77900 pCur->curFlags &= ~(BTCF_ValidNKey);
77907 pCur->pPage->nOverflow = 0;
77908 pCur->eState = CURSOR_INVALID;
77911 if( pCur->pKeyInfo ){
77912 assert( pCur->pKey==0 );
77913 pCur->pKey = sqlite3Malloc( pX->nKey );
77914 if( pCur->pKey==0 ){
77917 memcpy(pCur->pKey, pX->pKey, pX->nKey);
77920 pCur->eState = CURSOR_REQUIRESEEK;
77921 pCur->nKey = pX->nKey;
77924 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
77947 BtShared *pBt = pDest->pBt;
77948 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
77954 if( pSrc->info.nPayload<0x80 ){
77955 *(aOut++) = pSrc->info.nPayload;
77957 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
77959 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
77960 nIn = pSrc->info.nLocal;
77961 aIn = pSrc->info.pPayload;
77962 if( aIn+nIn>pSrc->pPage->aDataEnd ){
77965 nRem = pSrc->info.nPayload;
77966 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
77968 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
77970 Pager *pSrcPager = pSrc->pBt->pPager;
77977 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
77978 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
77979 if( nOut<pSrc->info.nPayload ){
77981 pBt->nPreformatSize += 4;
77985 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
77988 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
77992 nRem -= nOut;
77998 nOut -= nCopy;
77999 nIn -= nCopy;
78011 nIn = pSrc->pBt->usableSize - 4;
78022 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
78027 pPgnoOut = pPageOut->aData;
78030 nOut = MIN(pBt->usableSize - 4, nRem);
78055 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
78060 Btree *p = pCur->pBtree;
78061 BtShared *pBt = p->pBt;
78071 assert( pBt->inTransaction==TRANS_WRITE );
78072 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78073 assert( pCur->curFlags & BTCF_WriteFlag );
78074 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
78075 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
78077 if( pCur->eState!=CURSOR_VALID ){
78078 if( pCur->eState>=CURSOR_REQUIRESEEK ){
78080 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
78081 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
78086 assert( pCur->eState==CURSOR_VALID );
78088 iCellDepth = pCur->iPage;
78089 iCellIdx = pCur->ix;
78090 pPage = pCur->pPage;
78091 if( pPage->nCell<=iCellIdx ){
78095 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
78101 ** will cause a b-tree rebalance, then this is done by saving the cursor
78117 if( !pPage->leaf
78118 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
78119 (int)(pBt->usableSize*2/3)
78120 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
78122 /* A b-tree rebalance will be required after deleting this entry.
78136 ** sub-tree headed by the child page of the cell being deleted. This makes
78138 if( !pPage->leaf ){
78146 if( pCur->curFlags & BTCF_Multiple ){
78147 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
78151 /* If this is a delete operation to remove a row from a table b-tree,
78153 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
78154 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
78160 rc = sqlite3PagerWrite(pPage->pDbPage);
78167 ** is currently pointing to the largest entry in the sub-tree headed
78168 ** by the child-page of the cell that was just deleted from an internal
78171 if( !pPage->leaf ){
78172 MemPage *pLeaf = pCur->pPage;
78177 if( pLeaf->nFree<0 ){
78181 if( iCellDepth<pCur->iPage-1 ){
78182 n = pCur->apPage[iCellDepth+1]->pgno;
78184 n = pCur->pPage->pgno;
78186 pCell = findCell(pLeaf, pLeaf->nCell-1);
78187 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
78188 nCell = pLeaf->xCellSize(pLeaf, pCell);
78190 pTmp = pBt->pTmpSpace;
78192 rc = sqlite3PagerWrite(pLeaf->pDbPage);
78194 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
78196 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
78215 assert( pCur->pPage->nOverflow==0 );
78216 assert( pCur->pPage->nFree>=0 );
78217 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
78219 ** then balance() will always be a no-op. No need to invoke it. */
78224 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
78225 releasePageNotNull(pCur->pPage);
78226 pCur->iPage--;
78227 while( pCur->iPage>iCellDepth ){
78228 releasePage(pCur->apPage[pCur->iPage--]);
78230 pCur->pPage = pCur->apPage[pCur->iPage];
78236 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
78237 assert( pPage==pCur->pPage || CORRUPT_DB );
78238 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
78239 pCur->eState = CURSOR_SKIPNEXT;
78240 if( iCellIdx>=pPage->nCell ){
78241 pCur->skipNext = -1;
78242 pCur->ix = pPage->nCell-1;
78244 pCur->skipNext = 1;
78250 pCur->eState = CURSOR_REQUIRESEEK;
78270 BtShared *pBt = p->pBt;
78274 int ptfFlags; /* Page-type flage for the root page of new table */
78277 assert( pBt->inTransaction==TRANS_WRITE );
78278 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
78286 if( pBt->autoVacuum ){
78287 Pgno pgnoMove; /* Move a page here to make room for the root-page */
78292 ** out to be an overflow page, delete all overflow page-map caches
78298 ** root page of the new table should go. meta[3] is the largest root-page
78299 ** created so far, so the new root-page is (meta[3]+1).
78307 /* The new root-page may not be allocated on a pointer-map page, or the
78326 /* pgnoRoot is the page that will be used for the root-page of
78370 rc = sqlite3PagerWrite(pRoot->pDbPage);
78379 /* Update the pointer-map and meta-data with the new root-page number. */
78390 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
78402 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
78409 sqlite3PagerUnref(pRoot->pDbPage);
78410 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
78439 assert( sqlite3_mutex_held(pBt->mutex) );
78445 if( (pBt->openFlags & BTREE_SINGLE)==0
78446 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
78451 hdr = pPage->hdrOffset;
78452 for(i=0; i<pPage->nCell; i++){
78454 if( !pPage->leaf ){
78461 if( !pPage->leaf ){
78462 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
78464 if( pPage->intKey ) pnChange = 0;
78467 testcase( !pPage->intKey );
78468 *pnChange += pPage->nCell;
78472 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
78473 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
78495 BtShared *pBt = p->pBt;
78497 assert( p->inTrans==TRANS_WRITE );
78503 ** is the root of a table b-tree - if it is not, the following call is
78504 ** a no-op). */
78505 if( p->hasIncrblobCur ){
78520 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
78546 BtShared *pBt = p->pBt;
78549 assert( p->inTrans==TRANS_WRITE );
78569 if( pBt->autoVacuum ){
78574 /* If the table being dropped is the table with the largest root-page
78583 /* The table being dropped does not have the largest root-page
78585 ** gap left by the deleted root-page.
78608 /* Set the new 'max-root-page' value in the database header. This
78610 ** be a root-page number, less one again if that is the
78613 maxRootPgno--;
78616 maxRootPgno--;
78638 ** This function may only be called if the b-tree connection already
78641 ** Read the meta-information out of a database file. Meta[0]
78644 ** is read-only, the others are read/write.
78658 BtShared *pBt = p->pBt;
78661 assert( p->inTrans>TRANS_NONE );
78663 assert( pBt->pPage1 );
78667 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
78669 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
78672 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
78673 ** database, mark the database as read-only. */
78676 pBt->btsFlags |= BTS_READ_ONLY;
78684 ** Write meta-information back into the database. Meta[0] is
78685 ** read-only and may not be written.
78688 BtShared *pBt = p->pBt;
78693 assert( p->inTrans==TRANS_WRITE );
78694 assert( pBt->pPage1!=0 );
78695 pP1 = pBt->pPage1->aData;
78696 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
78701 assert( pBt->autoVacuum || iMeta==0 );
78703 pBt->incrVacuum = (u8)iMeta;
78712 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
78713 ** number of entries in the b-tree and write the result to *pnEntry.
78730 ** page in the B-Tree structure (not including overflow pages).
78732 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
78734 MemPage *pPage; /* Current page of the b-tree */
78736 /* If this is a leaf page or the tree is not an int-key tree, then
78740 pPage = pCur->pPage;
78741 if( pPage->leaf || !pPage->intKey ){
78742 nEntry += pPage->nCell;
78748 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
78750 ** to visit is the right-child of its parent.
78755 if( pPage->leaf ){
78757 if( pCur->iPage==0 ){
78758 /* All pages of the b-tree have been visited. Return successfully. */
78763 }while ( pCur->ix>=pCur->pPage->nCell );
78765 pCur->ix++;
78766 pPage = pCur->pPage;
78770 ** points at. This is the right-child if (iIdx==pPage->nCell).
78772 iIdx = pCur->ix;
78773 if( iIdx==pPage->nCell ){
78774 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
78789 return p->pBt->pPager;
78802 if( !pCheck->mxErr ) return;
78803 pCheck->mxErr--;
78804 pCheck->nErr++;
78806 if( pCheck->errMsg.nChar ){
78807 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
78809 if( pCheck->zPfx ){
78810 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
78812 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
78814 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
78815 pCheck->bOomFault = 1;
78823 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
78827 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
78828 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
78835 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
78836 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
78842 ** reference to the page, add an error message to pCheck->zErrMsg.
78849 if( iPage>pCheck->nPage || iPage==0 ){
78857 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
78864 ** Check that the entry in the pointer-map for page iChild maps to
78878 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
78880 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
78905 int nErrAtStart = pCheck->nErr;
78906 while( iPage!=0 && pCheck->mxErr ){
78910 N--;
78911 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
78919 if( pCheck->pBt->autoVacuum ){
78923 if( n>pCheck->pBt->usableSize/4-2 ){
78926 N--;
78931 if( pCheck->pBt->autoVacuum ){
78937 N -= n;
78942 /* If this database supports auto-vacuum and iPage is not the last
78943 ** page in this overflow list, check that the pointer-map entry for
78946 if( pCheck->pBt->autoVacuum && N>0 ){
78955 if( N && nErrAtStart==pCheck->nErr ){
78959 expected-N, expected);
78965 ** An implementation of a min-heap.
78975 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
79002 aHeap[0]--;
79039 int depth = -1, d2; /* Depth of a subtree */
79055 u32 *heap = 0; /* Min-heap used for checking cell coverage */
79056 u32 x, prev = 0; /* Next and previous entry on the min-heap */
79057 const char *saved_zPfx = pCheck->zPfx;
79058 int saved_v1 = pCheck->v1;
79059 int saved_v2 = pCheck->v2;
79064 pBt = pCheck->pBt;
79065 usableSize = pBt->usableSize;
79068 pCheck->zPfx = "Page %u: ";
79069 pCheck->v1 = iPage;
79078 savedIsInit = pPage->isInit;
79079 pPage->isInit = 0;
79091 data = pPage->aData;
79092 hdr = pPage->hdrOffset;
79095 pCheck->zPfx = "On tree page %u cell %d: ";
79099 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
79102 assert( pPage->nCell==nCell );
79104 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
79105 ** immediately follows the b-tree page header. */
79106 cellStart = hdr + 12 - 4*pPage->leaf;
79107 assert( pPage->aCellIdx==&data[cellStart] );
79108 pCellIdx = &data[cellStart + 2*(nCell-1)];
79110 if( !pPage->leaf ){
79111 /* Analyze the right-child page of internal pages */
79114 if( pBt->autoVacuum ){
79115 pCheck->zPfx = "On page %u at right child: ";
79124 heap = pCheck->heap;
79128 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
79130 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
79134 pCheck->v2 = i;
79137 pCellIdx -= 2;
79138 if( pc<contentOffset || pc>usableSize-4 ){
79140 pc, contentOffset, usableSize-4);
79145 pPage->xParseCell(pPage, pCell, &info);
79153 if( pPage->intKey ){
79165 assert( pc + info.nSize - 4 <= usableSize );
79166 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
79167 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
79169 if( pBt->autoVacuum ){
79176 if( !pPage->leaf ){
79180 if( pBt->autoVacuum ){
79191 /* Populate the coverage-checking heap for leaf pages */
79192 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
79199 pCheck->zPfx = 0;
79200 if( doCoverageCheck && pCheck->mxErr>0 ){
79201 /* For leaf pages, the min-heap has already been initialized and the
79204 if( !pPage->leaf ){
79205 heap = pCheck->heap;
79207 for(i=nCell-1; i>=0; i--){
79210 size = pPage->xCellSize(pPage, &data[pc]);
79211 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
79214 /* Add the freeblocks to the min-heap
79216 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
79223 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79226 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
79227 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
79228 ** big-endian integer which is the offset in the b-tree page of the next
79232 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
79235 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
79238 /* Analyze the min-heap looking for overlap between cells and/or
79241 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
79246 ** The loop below pulls entries from the min-heap in order and compares
79252 prev = contentOffset - 1; /* Implied first min-heap entry */
79259 nFrag += (x>>16) - (prev&0xffff) - 1;
79263 nFrag += usableSize - (prev&0xffff) - 1;
79264 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
79265 ** is stored in the fifth field of the b-tree page header.
79266 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
79277 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
79279 pCheck->zPfx = saved_zPfx;
79280 pCheck->v1 = saved_v1;
79281 pCheck->v2 = saved_v2;
79292 ** A read-only or read-write transaction must be opened before calling
79297 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
79301 ** root pages is incomplete. This is a "partial integrity-check". This
79319 BtShared *pBt = p->pBt;
79320 u64 savedDbFlags = pBt->db->flags;
79335 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
79336 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
79340 sCheck.pPager = pBt->pPager;
79361 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
79374 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
79375 get4byte(&pBt->pPage1->aData[36]));
79383 if( pBt->autoVacuum ){
79387 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
79394 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
79401 testcase( pBt->db->flags & SQLITE_CellSizeCk );
79402 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
79407 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
79413 pBt->db->flags = savedDbFlags;
79424 /* If the database supports auto-vacuum, make sure no tables contain
79425 ** references to pointer-map pages.
79428 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
79432 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
79451 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
79459 ** an empty string if the database is in-memory or a TEMP database.
79465 assert( p->pBt->pPager!=0 );
79466 return sqlite3PagerFilename(p->pBt->pPager, 1);
79478 assert( p->pBt->pPager!=0 );
79479 return sqlite3PagerJournalname(p->pBt->pPager);
79487 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
79488 return p ? p->inTrans : 0;
79496 ** transaction on the shared-cache the argument Btree is connected to.
79503 BtShared *pBt = p->pBt;
79505 if( pBt->inTransaction!=TRANS_NONE ){
79508 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
79521 assert( sqlite3_mutex_held(p->db->mutex) );
79522 return p->nBackup!=0;
79527 ** a single shared-btree. The memory is used by client code for its own
79528 ** purposes (for example, to store a high-level schema associated with
79529 ** the shared-btree). The btree layer manages reference counting issues.
79531 ** The first time this is called on a shared-btree, nBytes bytes of memory
79540 ** Just before the shared-btree is closed, the function passed as the
79546 BtShared *pBt = p->pBt;
79548 if( !pBt->pSchema && nBytes ){
79549 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
79550 pBt->xFreeSchema = xFree;
79553 return pBt->pSchema;
79563 assert( sqlite3_mutex_held(p->db->mutex) );
79580 assert( p->inTrans!=TRANS_NONE );
79581 if( p->sharable ){
79611 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
79612 assert( pCsr->curFlags & BTCF_Incrblob );
79618 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
79619 if( pCsr->eState!=CURSOR_VALID ){
79625 ** version of the b-tree page modified by the accessPayload call below.
79631 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
79637 ** (c) the connection holds a write-lock on the table (if required),
79638 ** (d) there are no conflicting read-locks, and
79641 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
79644 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
79645 && pCsr->pBt->inTransaction==TRANS_WRITE );
79646 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
79647 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
79648 assert( pCsr->pPage->intKey );
79657 pCur->curFlags |= BTCF_Incrblob;
79658 pCur->pBtree->hasIncrblobCur = 1;
79668 BtShared *pBt = pBtree->pBt;
79676 pBt->btsFlags &= ~BTS_NO_WAL;
79677 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
79681 u8 *aData = pBt->pPage1->aData;
79685 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
79694 pBt->btsFlags &= ~BTS_NO_WAL;
79703 return (pCsr->hints & mask)!=0;
79707 ** Return true if the given Btree is read-only.
79710 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
79719 ** If no transaction is active and the database is not a temp-db, clear
79720 ** the in-memory pager cache.
79723 BtShared *pBt = p->pBt;
79724 if( pBt->inTransaction==TRANS_NONE ){
79725 sqlite3PagerClearCache(pBt->pPager);
79734 return p->sharable;
79743 testcase( p->sharable );
79744 return p->pBt->nRef;
79772 Btree *pDest; /* Destination b-tree file */
79774 int bDestLocked; /* True once a write-transaction is open on pDest */
79778 Btree *pSrc; /* Source b-tree file */
79796 ** structure may be accessed via two groups of thread-safe entry points:
79810 ** backup_pagecount() are not thread-safe functions. If they are called
79818 ** Non-sharable Btrees (in-memory databases for example), do not have
79854 return pDb->aDb[i].pBt;
79863 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
79868 ** Check that there is no open read-transaction on the b-tree passed as the
79870 ** is an open read-transaction, return SQLITE_ERROR and leave an error
79912 sqlite3_mutex_enter(pSrcDb->mutex);
79913 sqlite3_mutex_enter(pDestDb->mutex);
79922 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
79933 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
79934 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
79935 p->pDestDb = pDestDb;
79936 p->pSrcDb = pSrcDb;
79937 p->iNext = 1;
79938 p->isAttached = 0;
79940 if( 0==p->pSrc || 0==p->pDest
79941 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
79953 p->pSrc->nBackup++;
79956 sqlite3_mutex_leave(pDestDb->mutex);
79957 sqlite3_mutex_leave(pSrcDb->mutex);
79981 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
79982 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
79983 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
79987 /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
79988 ** guaranteed that the shared-mutex is held by this thread, handle
79989 ** p->pSrc may not actually be the owner. */
79990 int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
79991 int nDestReserve = sqlite3BtreeGetRequestedReserve(p->pDest);
79996 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
79997 assert( p->bDestLocked );
79998 assert( !isFatalError(p->rc) );
79999 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
80002 /* Catch the case where the destination is an in-memory database and the
80031 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
80034 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
80052 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
80064 ** this function is a no-op.
80084 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
80085 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80086 p->pNext = *pp;
80088 p->isAttached = 1;
80092 ** Copy nPage pages from the source b-tree to the destination.
80103 sqlite3_mutex_enter(p->pSrcDb->mutex);
80104 sqlite3BtreeEnter(p->pSrc);
80105 if( p->pDestDb ){
80106 sqlite3_mutex_enter(p->pDestDb->mutex);
80109 rc = p->rc;
80111 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
80112 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
80114 int nSrcPage = -1; /* Size of source db in pages */
80117 /* If the source pager is currently in a write-transaction, return
80120 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
80126 /* If there is no open read-transaction on the source database, open
80130 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
80131 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
80141 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
80146 if( SQLITE_OK==rc && p->bDestLocked==0
80147 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
80148 (int*)&p->iDestSchema))
80150 p->bDestLocked = 1;
80155 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
80156 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
80157 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
80162 /* Now that there is a read-lock on the source database, query the
80165 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
80167 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
80168 const Pgno iSrcPg = p->iNext; /* Source page number */
80169 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
80177 p->iNext++;
80180 p->nPagecount = nSrcPage;
80181 p->nRemaining = nSrcPage+1-p->iNext;
80182 if( p->iNext>(Pgno)nSrcPage ){
80184 }else if( !p->isAttached ){
80190 ** is to make sure that the schema-version really does change in
80196 rc = sqlite3BtreeNewDb(p->pDest);
80200 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
80203 if( p->pDestDb ){
80204 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
80207 rc = sqlite3BtreeSetVersion(p->pDest, 2);
80224 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
80225 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
80228 nDestTruncate = (nSrcPage+ratio-1)/ratio;
80229 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
80230 nDestTruncate--;
80238 /* If the source page-size is smaller than the destination page-size,
80244 ** pending-byte page in the source database may need to be
80257 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
80269 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
80313 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
80323 ** "committing" a read-only transaction cannot fail.
80327 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
80328 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
80335 p->rc = rc;
80337 if( p->pDestDb ){
80338 sqlite3_mutex_leave(p->pDestDb->mutex);
80340 sqlite3BtreeLeave(p->pSrc);
80341 sqlite3_mutex_leave(p->pSrcDb->mutex);
80355 pSrcDb = p->pSrcDb;
80356 sqlite3_mutex_enter(pSrcDb->mutex);
80357 sqlite3BtreeEnter(p->pSrc);
80358 if( p->pDestDb ){
80359 sqlite3_mutex_enter(p->pDestDb->mutex);
80363 if( p->pDestDb ){
80364 p->pSrc->nBackup--;
80366 if( p->isAttached ){
80367 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
80370 pp = &(*pp)->pNext;
80373 *pp = p->pNext;
80377 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
80380 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
80381 if( p->pDestDb ){
80382 sqlite3Error(p->pDestDb, rc);
80385 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
80387 sqlite3BtreeLeave(p->pSrc);
80388 if( p->pDestDb ){
80389 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
80409 return p->nRemaining;
80423 return p->nPagecount;
80445 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
80446 if( !isFatalError(p->rc) && iPage<p->iNext ){
80452 assert( p->pDestDb );
80453 sqlite3_mutex_enter(p->pDestDb->mutex);
80455 sqlite3_mutex_leave(p->pDestDb->mutex);
80458 p->rc = rc;
80461 }while( (p = p->pNext)!=0 );
80480 for(p=pBackup; p; p=p->pNext){
80481 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
80482 p->iNext = 1;
80504 if( pFd->pMethods ){
80517 b.pSrcDb = pFrom->db;
80530 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
80537 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
80575 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
80588 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
80594 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
80597 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
80599 if( p->flags & MEM_Null ){
80601 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
80608 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
80611 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
80612 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
80613 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
80616 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
80624 assert( (p->flags & MEM_Cleared)==0 );
80628 assert( p->szMalloc==0
80629 || (p->flags==MEM_Undefined
80630 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
80631 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
80641 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
80643 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
80644 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
80645 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
80646 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
80659 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
80661 if( p->flags & MEM_Int ){
80663 /* Work-around for GCC bug
80666 assert( (p->flags&MEM_Int)*2==sizeof(x) );
80667 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
80670 sqlite3Int64ToText(p->u.i, zBuf);
80675 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
80694 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
80708 if( (p->flags & MEM_Str)==0 ) return 1;
80709 if( p->flags & MEM_Term ){
80710 /* Insure that the string is properly zero-terminated. Pay particular
80711 ** attention to the case where p->n is odd */
80712 if( p->szMalloc>0 && p->z==p->zMalloc ){
80713 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
80714 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
80716 assert( p->z[p->n]==0 );
80717 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
80718 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
80720 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
80722 z = p->z;
80725 if( p->enc!=SQLITE_UTF8 ){
80727 if( p->enc==SQLITE_UTF16BE ) z++;
80744 ** routine is a no-op.
80758 if( !(pMem->flags&MEM_Str) ){
80759 pMem->enc = desiredEnc;
80762 if( pMem->enc==desiredEnc ){
80765 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
80775 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
80776 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
80782 ** Make sure pMem->z points to a writable allocation of at least n bytes.
80785 ** pMem->z into the new allocation. pMem must be either a string or
80787 ** in pMem->z is discarded.
80792 testcase( pMem->db==0 );
80796 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
80797 testcase( bPreserve && pMem->z==0 );
80799 assert( pMem->szMalloc==0
80800 || (pMem->flags==MEM_Undefined
80801 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
80802 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
80803 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
80804 if( pMem->db ){
80805 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
80807 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
80808 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
80809 pMem->z = pMem->zMalloc;
80813 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
80814 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
80816 if( pMem->zMalloc==0 ){
80818 pMem->z = 0;
80819 pMem->szMalloc = 0;
80822 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
80825 if( bPreserve && pMem->z ){
80826 assert( pMem->z!=pMem->zMalloc );
80827 memcpy(pMem->zMalloc, pMem->z, pMem->n);
80829 if( (pMem->flags&MEM_Dyn)!=0 ){
80830 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
80831 pMem->xDel((void *)(pMem->z));
80834 pMem->z = pMem->zMalloc;
80835 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
80840 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
80841 ** If pMem->zMalloc already meets or exceeds the requested size, this
80842 ** routine is a no-op.
80845 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
80854 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
80855 if( pMem->szMalloc<szNew ){
80858 assert( (pMem->flags & MEM_Dyn)==0 );
80859 pMem->z = pMem->zMalloc;
80860 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
80869 ** to be a double-zero byte at an even byte boundary in order to
80874 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
80877 pMem->z[pMem->n] = 0;
80878 pMem->z[pMem->n+1] = 0;
80879 pMem->z[pMem->n+2] = 0;
80880 pMem->flags |= MEM_Term;
80892 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
80894 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
80896 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
80901 pMem->flags &= ~MEM_Ephem;
80903 pMem->pScopyFrom = 0;
80910 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
80917 assert( pMem->flags & MEM_Zero );
80918 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
80921 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
80924 nByte = pMem->n + pMem->u.nZero;
80926 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
80932 assert( pMem->z!=0 );
80933 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
80935 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
80936 pMem->n += pMem->u.nZero;
80937 pMem->flags &= ~(MEM_Zero|MEM_Term);
80947 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
80948 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
80949 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
80950 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
80975 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
80976 assert( !(pMem->flags&MEM_Zero) );
80977 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
80978 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
80984 pMem->enc = 0;
80988 vdbeMemRenderNum(nByte, pMem->z, pMem);
80989 assert( pMem->z!=0 );
80990 pMem->n = sqlite3Strlen30NN(pMem->z);
80991 pMem->enc = SQLITE_UTF8;
80992 pMem->flags |= MEM_Str|MEM_Term;
80993 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
81011 assert( pMem->db!=0 );
81012 assert( pFunc->xFinalize!=0 );
81013 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
81014 assert( sqlite3_mutex_held(pMem->db->mutex) );
81018 t.db = pMem->db;
81023 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
81024 assert( (pMem->flags & MEM_Dyn)==0 );
81025 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
81042 assert( pFunc->xValue!=0 );
81043 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
81044 assert( pAccum->db!=0 );
81045 assert( sqlite3_mutex_held(pAccum->db->mutex) );
81051 ctx.enc = ENC(pAccum->db);
81052 pFunc->xValue(&ctx);
81067 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
81069 if( p->flags&MEM_Agg ){
81070 sqlite3VdbeMemFinalize(p, p->u.pDef);
81071 assert( (p->flags & MEM_Agg)==0 );
81072 testcase( p->flags & MEM_Dyn );
81074 if( p->flags&MEM_Dyn ){
81075 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
81076 p->xDel((void *)p->z);
81078 p->flags = MEM_Null;
81083 ** by p->xDel and memory in p->zMalloc.
81093 if( p->szMalloc ){
81094 sqlite3DbFreeNN(p->db, p->zMalloc);
81095 p->szMalloc = 0;
81097 p->z = 0;
81112 if( VdbeMemDynamic(p) || p->szMalloc ){
81122 if( p->szMalloc ) vdbeMemClear(p);
81126 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
81127 ** If the double is out of range of a 64-bit signed integer then
81128 ** return the closest available 64-bit signed integer.
81132 /* When floating-point is omitted, double and int64 are the same thing */
81137 ** minimum and maximum 64-bit integers, or they define them
81140 ** larger than a 32-bit integer constant.
81159 ** a floating-point then the value returned is the integer part.
81162 ** an SQL-NULL value, return 0.
81168 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
81174 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81176 flags = pMem->flags;
81179 return pMem->u.i;
81181 return doubleToInt64(pMem->u.r);
81182 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
81198 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
81203 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81205 if( pMem->flags & MEM_Real ){
81206 return pMem->u.r;
81207 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
81208 testcase( pMem->flags & MEM_IntReal );
81209 return (double)pMem->u.i;
81210 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
81223 testcase( pMem->flags & MEM_IntReal );
81224 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
81225 if( pMem->flags & MEM_Null ) return ifNull;
81236 assert( pMem->flags & MEM_Real );
81238 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81241 ix = doubleToInt64(pMem->u.r);
81245 ** (1) the round-trip conversion real->int->real is a no-op, and
81253 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
81254 pMem->u.i = ix;
81264 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81268 pMem->u.i = sqlite3VdbeIntValue(pMem);
81279 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81282 pMem->u.r = sqlite3VdbeRealValue(pMem);
81292 ** For some versions of GCC on 32-bit machines, if you do the more obvious
81294 ** though the r1 and (double)i values are bit-for-bit the same.
81300 && i >= -2251799813685248LL && i < 2251799813685248LL);
81323 testcase( pMem->flags & MEM_Int );
81324 testcase( pMem->flags & MEM_Real );
81325 testcase( pMem->flags & MEM_IntReal );
81326 testcase( pMem->flags & MEM_Null );
81327 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
81330 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
81331 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81332 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
81333 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
81334 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
81336 pMem->u.i = ix;
81342 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
81343 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
81355 if( pMem->flags & MEM_Null ) return SQLITE_OK;
81358 if( (pMem->flags & MEM_Blob)==0 ){
81360 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81361 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
81363 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
81382 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
81384 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
81385 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
81386 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
81400 pMem->flags = flags;
81401 pMem->db = db;
81402 pMem->szMalloc = 0;
81422 pMem->flags = MEM_Null;
81436 pMem->flags = MEM_Blob|MEM_Zero;
81437 pMem->n = 0;
81439 pMem->u.nZero = n;
81440 pMem->enc = SQLITE_UTF8;
81441 pMem->z = 0;
81449 assert( pMem->z!=0 );
81450 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
81451 memset(pMem->z, 0, nByte);
81452 pMem->n = n>0?n:0;
81453 pMem->flags = MEM_Blob;
81454 pMem->enc = SQLITE_UTF8;
81462 ** a 64-bit integer.
81466 pMem->u.i = val;
81467 pMem->flags = MEM_Int;
81478 pMem->u.i = val;
81479 pMem->flags = MEM_Int;
81483 /* A no-op destructor */
81496 assert( pMem->flags==MEM_Null );
81498 pMem->u.zPType = zPType ? zPType : "";
81499 pMem->z = pPtr;
81500 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
81501 pMem->eSubtype = 'p';
81502 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
81513 pMem->u.r = val;
81514 pMem->flags = MEM_Real;
81525 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
81526 && pMem->xDel==sqlite3RowSetDelete;
81538 sqlite3 *db = pMem->db;
81545 pMem->z = (char*)p;
81546 pMem->flags = MEM_Blob|MEM_Dyn;
81547 pMem->xDel = sqlite3RowSetDelete;
81553 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
81556 assert( p->db!=0 );
81557 if( p->flags & (MEM_Str|MEM_Blob) ){
81558 int n = p->n;
81559 if( p->flags & MEM_Zero ){
81560 n += p->u.nZero;
81562 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
81573 ** This is used for testing and debugging only - to help ensure that shallow
81579 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
81580 if( pX->pScopyFrom==pMem ){
81582 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
81584 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
81592 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
81593 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
81596 ** undefined so that we can quickly detect the shallow-copy error */
81597 pX->flags = MEM_Undefined;
81598 pX->pScopyFrom = 0;
81601 pMem->pScopyFrom = 0;
81607 ** pTo are freed. The pFrom->z field is not duplicated. If
81608 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
81618 assert( pTo->db==pFrom->db );
81621 if( (pFrom->flags&MEM_Static)==0 ){
81622 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
81624 pTo->flags |= srcType;
81638 pTo->flags &= ~MEM_Dyn;
81639 if( pTo->flags&(MEM_Str|MEM_Blob) ){
81640 if( 0==(pFrom->flags&MEM_Static) ){
81641 pTo->flags |= MEM_Ephem;
81656 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
81657 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
81658 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
81662 pFrom->flags = MEM_Null;
81663 pFrom->szMalloc = 0;
81686 ** non-negative for blobs.
81695 i64 nByte = n; /* New value for pMem->n */
81697 u16 flags; /* New value for pMem->flags */
81700 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
81710 if( pMem->db ){
81711 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
81732 sqlite3DbFree(pMem->db, (void*)z);
81738 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
81756 memcpy(pMem->z, z, nAlloc);
81759 pMem->z = (char *)z;
81761 pMem->zMalloc = pMem->z;
81762 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
81764 pMem->xDel = xDel;
81769 pMem->n = (int)(nByte & 0x7fffffff);
81770 pMem->flags = flags;
81771 pMem->enc = enc;
81790 ** pMem->zMalloc to hold the content from the btree, if possible. New
81791 ** pMem->zMalloc space will be allocated if necessary. The calling routine
81805 pMem->flags = MEM_Null;
81810 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
81812 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
81813 pMem->flags = MEM_Blob;
81814 pMem->n = (int)amt;
81835 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
81836 assert( pMem->z!=0 );
81839 pMem->flags = MEM_Blob|MEM_Ephem;
81840 pMem->n = (int)amt;
81851 ** to a zero-terminated version of that string.
81855 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
81858 assert( (pVal->flags & (MEM_Null))==0 );
81859 if( pVal->flags & (MEM_Blob|MEM_Str) ){
81861 pVal->flags |= MEM_Str;
81862 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
81865 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
81866 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
81871 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
81874 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
81876 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
81877 || pVal->db->mallocFailed );
81878 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
81880 return pVal->z;
81892 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
81898 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
81901 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
81903 return pVal->z;
81905 if( pVal->flags&MEM_Null ){
81917 p->flags = MEM_Null;
81918 p->db = db;
81939 ** Otherwise, if the second argument is non-zero, then this function is
81948 UnpackedRecord *pRec = p->ppRec[0];
81951 Index *pIdx = p->pIdx; /* Index being probed */
81954 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
81959 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
81960 if( pRec->pKeyInfo ){
81961 assert( pRec->pKeyInfo->nAllField==nCol );
81962 assert( pRec->pKeyInfo->enc==ENC(db) );
81963 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
81965 pRec->aMem[i].flags = MEM_Null;
81966 pRec->aMem[i].db = db;
81974 p->ppRec[0] = pRec;
81977 pRec->nField = p->iVal+1;
81978 return &pRec->aMem[p->iVal];
82025 assert( (p->flags & EP_TokenOnly)==0 );
82027 pList = p->x.pList;
82028 if( pList ) nVal = pList->nExpr;
82030 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
82032 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
82033 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
82045 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
82056 testcase( pCtx->pParse->rc==SQLITE_ERROR );
82057 testcase( pCtx->pParse->rc==SQLITE_OK );
82062 pFunc->xSFunc(&ctx, nVal, apVal);
82065 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
82072 pCtx->pParse->nErr++;
82075 pCtx->pParse->rc = rc;
82121 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
82122 if( op==TK_REGISTER ) op = pExpr->op2;
82128 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
82133 aff = sqlite3AffinityType(pExpr->u.zToken,0);
82134 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
82144 ** case when the value is -9223372036854775808.
82147 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
82148 pExpr = pExpr->pLeft;
82149 op = pExpr->op;
82150 negInt = -1;
82151 zNeg = "-";
82158 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
82160 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
82162 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
82169 assert( (pVal->flags & MEM_IntReal)==0 );
82170 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
82171 testcase( pVal->flags & MEM_Int );
82172 testcase( pVal->flags & MEM_Real );
82173 pVal->flags &= ~MEM_Str;
82179 /* This branch happens for multiple negative signs. Ex: -(-5) */
82180 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
82184 if( pVal->flags & MEM_Real ){
82185 pVal->u.r = -pVal->u.r;
82186 }else if( pVal->u.i==SMALLEST_INT64 ){
82188 pVal->u.r = -(double)SMALLEST_INT64;
82190 pVal->u.r = LARGEST_INT64;
82194 pVal->u.i = -pVal->u.i;
82207 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
82208 assert( pExpr->u.zToken[1]=='\'' );
82211 zVal = &pExpr->u.zToken[2];
82212 nVal = sqlite3Strlen30(zVal)-1;
82227 pVal->flags = MEM_Int;
82228 pVal->u.i = pExpr->u.zToken[4]==0;
82237 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
82298 sqlite3 *db = pParse->db;
82303 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
82309 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
82311 int iBindVar = pExpr->iColumn;
82312 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
82313 if( (v = pParse->pReprepare)!=0 ){
82316 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
82318 pVal->db = pParse->db;
82325 assert( pVal==0 || pVal->db==db );
82376 if( pExpr==0 || pExpr->op!=TK_SELECT ){
82387 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
82419 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
82461 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
82462 pMem->enc = ENC(db);
82474 int nCol = pRec->pKeyInfo->nAllField;
82475 Mem *aMem = pRec->aMem;
82480 sqlite3KeyInfoUnref(pRec->pKeyInfo);
82505 sqlite3DbFreeNN(((Mem*)v)->db, v);
82514 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
82518 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
82519 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
82520 return p->n;
82522 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
82523 return p->n;
82525 if( (p->flags & MEM_Blob)!=0 ){
82526 if( p->flags & MEM_Zero ){
82527 return p->n + p->u.nZero;
82529 return p->n;
82532 if( p->flags & MEM_Null ) return 0;
82563 sqlite3 *db = pParse->db;
82567 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
82568 p->db = db;
82569 if( db->pVdbe ){
82570 db->pVdbe->ppVPrev = &p->pVNext;
82572 p->pVNext = db->pVdbe;
82573 p->ppVPrev = &db->pVdbe;
82574 db->pVdbe = p;
82575 assert( p->eVdbeState==VDBE_INIT_STATE );
82576 p->pParse = pParse;
82577 pParse->pVdbe = p;
82578 assert( pParse->aLabel==0 );
82579 assert( pParse->nLabel==0 );
82580 assert( p->nOpAlloc==0 );
82581 assert( pParse->szOpAlloc==0 );
82590 return p->pParse;
82598 sqlite3DbFree(p->db, p->zErrMsg);
82600 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
82609 p->prepFlags = prepFlags;
82611 p->expmask = 0;
82613 assert( p->zSql==0 );
82614 p->zSql = sqlite3DbStrNDup(p->db, z, n);
82619 ** Add a new element to the Vdbe->pDblStr list.
82625 sizeof(*pStr)+n+1-sizeof(pStr->z));
82627 pStr->pNextStr = p->pDblStr;
82628 p->pDblStr = pStr;
82629 memcpy(pStr->z, z, n+1);
82637 ** zId of length nId is a double-quoted identifier. Check to see if
82642 const char *zId /* The double-quoted identifier, already dequoted */
82646 if( pVdbe->pDblStr==0 ) return 0;
82647 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
82648 if( strcmp(zId, pStr->z)==0 ) return 1;
82655 ** Swap byte-code between two VDBE structures.
82667 assert( pA->db==pB->db );
82671 pTmp = pA->pVNext;
82672 pA->pVNext = pB->pVNext;
82673 pB->pVNext = pTmp;
82674 ppTmp = pA->ppVPrev;
82675 pA->ppVPrev = pB->ppVPrev;
82676 pB->ppVPrev = ppTmp;
82677 zTmp = pA->zSql;
82678 pA->zSql = pB->zSql;
82679 pB->zSql = zTmp;
82681 zTmp = pA->zNormSql;
82682 pA->zNormSql = pB->zNormSql;
82683 pB->zNormSql = zTmp;
82685 pB->expmask = pA->expmask;
82686 pB->prepFlags = pA->prepFlags;
82687 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
82688 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
82696 ** If an out-of-memory error occurs while resizing the array, return
82703 Parse *p = v->pParse;
82705 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
82713 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
82714 : (sqlite3_int64)v->nOpAlloc+nOp);
82716 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
82722 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
82723 sqlite3OomFault(p->db);
82728 assert( nNew>=(v->nOpAlloc+nOp) );
82729 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
82731 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
82732 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
82733 v->aOp = pNew;
82773 assert( p->nOpAlloc<=p->nOp );
82775 assert( p->nOpAlloc>p->nOp );
82782 i = p->nOp;
82783 assert( p->eVdbeState==VDBE_INIT_STATE );
82785 if( p->nOpAlloc<=i ){
82788 assert( p->aOp!=0 );
82789 p->nOp++;
82790 pOp = &p->aOp[i];
82792 pOp->opcode = (u8)op;
82793 pOp->p5 = 0;
82794 pOp->p1 = p1;
82795 pOp->p2 = p2;
82796 pOp->p3 = p3;
82797 pOp->p4.p = 0;
82798 pOp->p4type = P4_NOTUSED;
82800 pOp->zComment = 0;
82803 if( p->db->flags & SQLITE_VdbeAddopTrace ){
82804 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
82805 test_addop_breakpoint(i, &p->aOp[i]);
82809 pOp->cycles = 0;
82810 pOp->cnt = 0;
82813 pOp->iSrcLine = 0;
82907 Vdbe *v = pParse->pVdbe;
82912 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
82913 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
82915 assert( pParse->db->mallocFailed );
82916 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
82919 pCtx->pOut = 0;
82920 pCtx->pFunc = (FuncDef*)pFunc;
82921 pCtx->pVdbe = 0;
82922 pCtx->isError = 0;
82923 pCtx->argc = nArg;
82924 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
82957 if( pParse->addrExplain==0 ) return 0;
82958 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
82959 return pOp->p2;
82983 if( pParse->explain==2 )
82991 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
82993 v = pParse->pVdbe;
82994 iThis = v->nOp;
82995 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
82997 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
82999 pParse->addrExplain = iThis;
83009 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
83025 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
83026 sqlite3MayAbort(p->pParse);
83041 if( p->db->mallocFailed==0 ){
83042 VdbeOp *pOp = &p->aOp[addr];
83043 pOp->p4type = P4_INT32;
83044 pOp->p4.i = p4;
83049 /* Insert the end of a co-routine
83055 ** co-routine has its own independent set of registers, because co-routines
83057 ** that could cause problems if two or more co-routines are using the same
83060 v->pParse->nTempReg = 0;
83061 v->pParse->nRangeReg = 0;
83073 ** always negative and P2 values are suppose to be non-negative.
83080 ** Parse.aLabel[x] Stores the address that the x-th label resolves
83082 ** labels stores -1, but that is not required.
83090 return --pParse->nLabel;
83099 int nNewSize = 10 - p->nLabel;
83100 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
83101 nNewSize*sizeof(p->aLabel[0]));
83102 if( p->aLabel==0 ){
83103 p->nLabelAlloc = 0;
83107 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
83109 p->nLabelAlloc = nNewSize;
83110 p->aLabel[j] = v->nOp;
83114 Parse *p = v->pParse;
83116 assert( v->eVdbeState==VDBE_INIT_STATE );
83117 assert( j<-p->nLabel );
83120 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83121 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
83124 if( p->nLabelAlloc + p->nLabel < 0 ){
83127 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
83128 p->aLabel[j] = v->nOp;
83144 for(i=1; ALWAYS(i<p->nOp); i++){
83145 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
83146 p->aOp[1].opcode = OP_Noop;
83156 ** in a Vdbe main program and each of the sub-programs (triggers) it may
83167 ** sqlite3DbFree(v->db, sIter.apSub);
83176 int iSub; /* 0 = main program, 1 = first sub-program etc. */
83179 Vdbe *v = p->v;
83184 if( p->iSub<=p->nSub ){
83186 if( p->iSub==0 ){
83187 aOp = v->aOp;
83188 nOp = v->nOp;
83190 aOp = p->apSub[p->iSub-1]->aOp;
83191 nOp = p->apSub[p->iSub-1]->nOp;
83193 assert( p->iAddr<nOp );
83195 pRet = &aOp[p->iAddr];
83196 p->iAddr++;
83197 if( p->iAddr==nOp ){
83198 p->iSub++;
83199 p->iAddr = 0;
83202 if( pRet->p4type==P4_SUBPROGRAM ){
83203 int nByte = (p->nSub+1)*sizeof(SubProgram*);
83205 for(j=0; j<p->nSub; j++){
83206 if( p->apSub[j]==pRet->p4.pProgram ) break;
83208 if( j==p->nSub ){
83209 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
83210 if( !p->apSub ){
83213 p->apSub[p->nSub++] = pRet->p4.pProgram;
83226 ** sub-programs contains any of the following:
83243 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
83259 int opcode = pOp->opcode;
83266 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
83271 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
83275 ** where a "DELETE FROM tbl" has a statement-journal but does not
83276 ** require one. This is not so bad - it is an inefficiency, not a bug. */
83277 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
83282 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
83287 sqlite3DbFree(v->db, sIter.apSub);
83294 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
83298 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
83307 || (pC->eCurType!=CURTYPE_SORTER
83308 && pC->eCurType!=CURTYPE_PSEUDO
83309 && !pC->isEphemeral)
83311 p->nWrite++;
83322 assert( p->nWrite==0 || p->usesStmtJournal );
83350 Parse *pParse = p->pParse;
83351 int *aLabel = pParse->aLabel;
83352 p->readOnly = 1;
83353 p->bIsReader = 0;
83354 pOp = &p->aOp[p->nOp-1];
83355 assert( p->aOp[0].opcode==OP_Init );
83363 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
83366 switch( pOp->opcode ){
83368 if( pOp->p2!=0 ) p->readOnly = 0;
83373 p->bIsReader = 1;
83381 p->readOnly = 0;
83382 p->bIsReader = 1;
83386 assert( pOp->p2>=0 );
83391 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
83396 assert( (pOp - p->aOp) >= 3 );
83397 assert( pOp[-1].opcode==OP_Integer );
83398 n = pOp[-1].p1;
83405 if( pOp->p2<0 ){
83407 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
83408 ** have non-negative values for P2. */
83409 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
83410 assert( ADDR(pOp->p2)<-pParse->nLabel );
83411 pOp->p2 = aLabel[ADDR(pOp->p2)];
83417 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
83418 ** have non-negative values for P2. */
83419 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
83421 assert( pOp>p->aOp );
83422 pOp--;
83426 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
83427 pParse->aLabel = 0;
83429 pParse->nLabel = 0;
83431 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
83456 Vdbe *v, /* The byte-code program under construction */
83466 pParse = v->pParse;
83468 if( pParse->nErr ) return;
83470 assert( iLast<v->nOp );
83471 pOp = &v->aOp[iFirst];
83473 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
83474 int iDest = pOp->p2; /* Jump destination */
83476 if( pOp->opcode==OP_Gosub ) continue;
83480 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
83483 iDest = pParse->aLabel[j];
83487 for(; j<v->nOp; j++){
83488 VdbeOp *pX = &v->aOp[j];
83489 if( pX->opcode==OP_Return ){
83490 if( pX->p1==iRetReg ) break;
83493 if( pX->opcode==OP_Noop ) continue;
83494 if( pX->opcode==OP_Explain ) continue;
83522 assert( p->eVdbeState==VDBE_INIT_STATE );
83523 return p->nOp;
83532 ** sqlite3VdbeAddOpList() will always be non-NULL.
83536 assert( p->nOp + N <= p->nOpAlloc );
83550 for(i=0; i<p->nOp; i++){
83551 assert( p->aOp[i].opcode!=OP_ResultRow );
83579 VdbeOp *aOp = p->aOp;
83580 assert( aOp && !p->db->mallocFailed );
83583 assert( DbMaskAllZero(p->btreeMask) );
83586 *pnOp = p->nOp;
83587 p->aOp = 0;
83595 ** Non-zero P2 arguments to jump instructions are automatically adjusted
83602 int iLineno /* Source-file line number of first opcode */
83607 assert( p->eVdbeState==VDBE_INIT_STATE );
83608 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
83611 pFirst = pOut = &p->aOp[p->nOp];
83613 pOut->opcode = aOp->opcode;
83614 pOut->p1 = aOp->p1;
83615 pOut->p2 = aOp->p2;
83616 assert( aOp->p2>=0 );
83617 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
83618 pOut->p2 += p->nOp;
83620 pOut->p3 = aOp->p3;
83621 pOut->p4type = P4_NOTUSED;
83622 pOut->p4.p = 0;
83623 pOut->p5 = 0;
83625 pOut->zComment = 0;
83628 pOut->iSrcLine = iLineno+i;
83633 if( p->db->flags & SQLITE_VdbeAddopTrace ){
83634 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
83638 p->nOp += nOp;
83654 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
83656 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
83658 ScanStatus *pNew = &aNew[p->nScan++];
83659 pNew->addrExplain = addrExplain;
83660 pNew->addrLoop = addrLoop;
83661 pNew->addrVisit = addrVisit;
83662 pNew->nEst = nEst;
83663 pNew->zName = sqlite3DbStrDup(p->db, zName);
83664 p->aScan = aNew;
83676 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
83680 sqlite3VdbeGetOp(p,addr)->p1 = val;
83683 assert( addr>=0 || p->db->mallocFailed );
83684 sqlite3VdbeGetOp(p,addr)->p2 = val;
83688 sqlite3VdbeGetOp(p,addr)->p3 = val;
83691 assert( p->nOp>0 || p->db->mallocFailed );
83692 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
83702 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
83703 pOp->p5 |= OPFLAG_TYPEOFARG;
83712 sqlite3VdbeChangeP2(p, addr, p->nOp);
83718 ** the previous opcode (and is thus a no-op) then simply back up
83723 ** strives to omit useless byte-code like this:
83729 if( addr==p->nOp-1 ){
83730 assert( p->aOp[addr].opcode==OP_Once
83731 || p->aOp[addr].opcode==OP_If
83732 || p->aOp[addr].opcode==OP_FkIfZero );
83733 assert( p->aOp[addr].p4type==0 );
83735 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
83737 p->nOp--;
83739 sqlite3VdbeChangeP2(p, addr, p->nOp);
83750 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
83759 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
83764 freeEphemeralFunction(db, p->pFunc);
83782 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
83796 if( db->pnBytesFreed==0 ){
83804 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
83819 Op *pOp = &aOp[nOp-1];
83821 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
83823 sqlite3DbFree(db, pOp->zComment);
83826 pOp--;
83834 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
83838 p->pNext = pVdbe->pProgram;
83839 pVdbe->pProgram = p;
83846 return pVdbe->pProgram!=0;
83854 if( p->db->mallocFailed ) return 0;
83855 assert( addr>=0 && addr<p->nOp );
83856 pOp = &p->aOp[addr];
83857 freeP4(p->db, pOp->p4type, pOp->p4.p);
83858 pOp->p4type = P4_NOTUSED;
83859 pOp->p4.z = 0;
83860 pOp->opcode = OP_Noop;
83869 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
83870 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
83888 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
83889 assert( pParse->pVdbe );
83891 assert( iFirst+N-1<=pParse->nMem );
83896 N--;
83898 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
83899 mask &= ~MASKBIT32(N-1);
83900 N--;
83904 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
83905 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
83934 if( pOp->p4type ){
83935 freeP4(p->db, pOp->p4type, pOp->p4.p);
83936 pOp->p4type = 0;
83937 pOp->p4.p = 0;
83940 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
83943 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
83944 pOp->p4type = P4_DYNAMIC;
83951 db = p->db;
83952 assert( p->eVdbeState==VDBE_INIT_STATE );
83953 assert( p->aOp!=0 || db->mallocFailed );
83954 if( db->mallocFailed ){
83958 assert( p->nOp>0 );
83959 assert( addr<p->nOp );
83961 addr = p->nOp - 1;
83963 pOp = &p->aOp[addr];
83964 if( n>=0 || pOp->p4type ){
83971 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
83972 pOp->p4type = P4_INT32;
83975 pOp->p4.p = (void*)zP4;
83976 pOp->p4type = (signed char)n;
83983 ** to the value defined by the arguments. This is a high-speed
83994 if( p->db->mallocFailed ){
83995 freeP4(p->db, n, pP4);
83998 assert( p->nOp>0 );
83999 pOp = &p->aOp[p->nOp-1];
84000 assert( pOp->p4type==P4_NOTUSED );
84001 pOp->p4type = n;
84002 pOp->p4.p = pP4;
84011 Vdbe *v = pParse->pVdbe;
84022 ** insert a No-op and add the comment to that new instruction. This
84027 assert( p->nOp>0 || p->aOp==0 );
84028 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
84029 if( p->nOp ){
84030 assert( p->aOp );
84031 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
84032 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
84059 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
84064 ** Return the opcode for a given address. The address must be non-negative.
84080 assert( p->eVdbeState==VDBE_INIT_STATE );
84081 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
84082 if( p->db->mallocFailed ){
84085 return &p->aOp[addr];
84092 return sqlite3VdbeGetOp(p, p->nOp - 1);
84101 if( c=='1' ) return pOp->p1;
84102 if( c=='2' ) return pOp->p2;
84103 if( c=='3' ) return pOp->p3;
84104 if( c=='4' ) return pOp->p4.i;
84105 return pOp->p5;
84116 ** "PX" -> "r[X]"
84117 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
84118 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
84119 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
84122 sqlite3 *db, /* Optional - Oom error reporting only */
84134 zOpName = sqlite3OpcodeName(pOp->opcode);
84150 if( pOp->zComment && pOp->zComment[0] ){
84151 sqlite3_str_appendall(&x, pOp->zComment);
84168 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
84171 sqlite3_context *pCtx = pOp->p4.pCtx;
84172 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
84174 }else if( pCtx->argc>1 ){
84175 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
84178 x.nChar -= 2;
84184 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
84193 if( !seenCom && pOp->zComment ){
84194 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
84196 }else if( pOp->zComment ){
84197 sqlite3_str_appendall(&x, pOp->zComment);
84213 switch( pExpr->op ){
84216 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
84219 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
84225 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
84229 if( pExpr->iColumn<0 ){
84232 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
84270 displayP4Expr(p, pExpr->pLeft);
84271 if( pExpr->pRight ){
84273 displayP4Expr(p, pExpr->pRight);
84291 switch( pOp->p4type ){
84294 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
84295 assert( pKeyInfo->aSortFlags!=0 );
84296 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
84297 for(j=0; j<pKeyInfo->nKeyField; j++){
84298 CollSeq *pColl = pKeyInfo->aColl[j];
84299 const char *zColl = pColl ? pColl->zName : "";
84302 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
84303 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
84311 displayP4Expr(&x, pOp->p4.pExpr);
84317 CollSeq *pColl = pOp->p4.pColl;
84318 assert( pColl->enc<4 );
84319 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
84320 encnames[pColl->enc]);
84324 FuncDef *pDef = pOp->p4.pFunc;
84325 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84329 FuncDef *pDef = pOp->p4.pCtx->pFunc;
84330 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
84334 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
84338 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
84342 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
84346 Mem *pMem = pOp->p4.pMem;
84347 if( pMem->flags & MEM_Str ){
84348 zP4 = pMem->z;
84349 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84350 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
84351 }else if( pMem->flags & MEM_Real ){
84352 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
84353 }else if( pMem->flags & MEM_Null ){
84356 assert( pMem->flags & MEM_Blob );
84363 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
84370 u32 *ai = pOp->p4.ai;
84384 zP4 = pOp->p4.pTab->zName;
84388 zP4 = pOp->p4.z;
84400 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
84404 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
84405 ** p->btreeMask of databases that will require a lock.
84408 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
84409 assert( i<(int)sizeof(p->btreeMask)*8 );
84410 DbMaskSet(p->btreeMask, i);
84411 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
84412 DbMaskSet(p->lockMask, i);
84418 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
84422 ** that the correct busy-handler callback is invoked if required.
84424 ** If SQLite is not threadsafe but does support shared-cache mode, then
84429 ** If SQLite is not threadsafe and does not support shared-cache mode, this
84430 ** function is a no-op.
84432 ** The p->btreeMask field is a bitmask of all btrees that the prepared
84433 ** statement p will ever use. Let N be the number of bits in p->btreeMask
84443 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
84444 db = p->db;
84445 aDb = db->aDb;
84446 nDb = db->nDb;
84448 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
84464 db = p->db;
84465 aDb = db->aDb;
84466 nDb = db->nDb;
84468 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
84474 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
84487 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
84501 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
84502 zP4 ? zP4 : "", pOp->p5,
84515 ** This is a high-runner, so only those fields that really do need to
84530 p->flags = flags;
84531 p->db = db;
84532 p->szMalloc = 0;
84534 p->pScopyFrom = 0;
84537 }while( (--N)>0 );
84552 sqlite3 *db = p->db;
84553 if( db->pnBytesFreed ){
84555 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
84569 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
84570 ** sqlite3MemRelease() were called from here. With -O2, this jumps
84575 testcase( p->flags & MEM_Agg );
84576 testcase( p->flags & MEM_Dyn );
84577 if( p->flags&(MEM_Agg|MEM_Dyn) ){
84578 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
84580 p->flags = MEM_Undefined;
84581 }else if( p->szMalloc ){
84582 sqlite3DbNNFreeNN(db, p->zMalloc);
84583 p->szMalloc = 0;
84584 p->flags = MEM_Undefined;
84588 p->flags = MEM_Undefined;
84603 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
84619 pFrame->pParent = pFrame->v->pDelFrame;
84620 pFrame->v->pDelFrame = pFrame;
84640 int nSub = 0; /* Number of sub-vdbes seen so far */
84641 SubProgram **apSub = 0; /* Array of sub-vdbes */
84652 ** encountered, but p->pc will eventually catch up to nRow.
84654 nRow = p->nOp;
84656 if( pSub->flags&MEM_Blob ){
84659 nSub = pSub->n/sizeof(Vdbe*);
84660 apSub = (SubProgram **)pSub->z;
84663 nRow += apSub[i]->nOp;
84670 p->rc = SQLITE_OK;
84674 if( i<p->nOp ){
84677 aOp = p->aOp;
84682 i -= p->nOp;
84685 for(j=0; i>=apSub[j]->nOp; j++){
84686 i -= apSub[j]->nOp;
84687 assert( i<apSub[j]->nOp || j+1<nSub );
84689 aOp = apSub[j]->aOp;
84694 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
84704 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
84705 if( p->rc!=SQLITE_OK ){
84709 apSub = (SubProgram **)pSub->z;
84712 pSub->n = nSub*sizeof(SubProgram*);
84713 nRow += aOp[i].p4.pProgram->nOp;
84720 if( pOp->opcode==OP_OpenRead ) break;
84721 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
84722 if( pOp->opcode==OP_ReopenIdx ) break;
84746 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
84748 for(i=0; i<p->nChildCsr; i++){
84749 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
84751 releaseMemArray(aMem, p->nChildMem);
84752 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
84753 sqlite3DbFree(p->v->db, p);
84764 ** When p->explain==1, each instruction is listed. When
84765 ** p->explain==2, only OP_Explain instructions are listed and these
84766 ** are shown in a different format. p->explain==2 is used to implement
84768 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
84772 ** When p->explain==1, first the main program is listed, then each of
84779 sqlite3 *db = p->db; /* The database connection */
84782 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
84783 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
84787 assert( p->explain );
84788 assert( p->eVdbeState==VDBE_RUN_STATE );
84789 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
84793 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
84796 p->pResultSet = 0;
84798 if( p->rc==SQLITE_NOMEM ){
84810 assert( p->nMem>9 );
84811 pSub = &p->aMem[9];
84817 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
84821 if( AtomicLoad(&db->u1.isInterrupted) ){
84822 p->rc = SQLITE_INTERRUPT;
84824 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
84827 if( p->explain==2 ){
84828 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
84829 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
84830 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
84831 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
84832 p->nResColumn = 4;
84835 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
84836 -1, SQLITE_UTF8, SQLITE_STATIC);
84837 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
84838 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
84839 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
84841 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
84845 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
84850 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
84851 p->nResColumn = 8;
84853 p->pResultSet = pMem;
84854 if( db->mallocFailed ){
84855 p->rc = SQLITE_NOMEM;
84858 p->rc = SQLITE_OK;
84873 if( p->zSql ){
84874 z = p->zSql;
84875 }else if( p->nOp>=1 ){
84876 const VdbeOp *pOp = &p->aOp[0];
84877 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
84878 z = pOp->p4.z;
84891 int nOp = p->nOp;
84895 pOp = &p->aOp[0];
84896 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
84899 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
84903 if( z[i-1]!=' ' ){
84926 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
84945 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
84948 if( nByte <= p->nFree ){
84949 p->nFree -= nByte;
84950 pBuf = &p->pSpace[p->nFree];
84952 p->nNeeded += nByte;
84968 assert( p->eVdbeState==VDBE_INIT_STATE
84969 || p->eVdbeState==VDBE_READY_STATE
84970 || p->eVdbeState==VDBE_HALT_STATE );
84974 assert( p->nOp>0 );
84976 p->eVdbeState = VDBE_READY_STATE;
84979 for(i=0; i<p->nMem; i++){
84980 assert( p->aMem[i].db==p->db );
84983 p->pc = -1;
84984 p->rc = SQLITE_OK;
84985 p->errorAction = OE_Abort;
84986 p->nChange = 0;
84987 p->cacheCtr = 1;
84988 p->minWriteFileFormat = 255;
84989 p->iStatement = 0;
84990 p->nFkConstraint = 0;
84992 for(i=0; i<p->nOp; i++){
84993 p->aOp[i].cnt = 0;
84994 p->aOp[i].cycles = 0;
85030 assert( p->nOp>0 );
85032 assert( p->eVdbeState==VDBE_INIT_STATE );
85033 assert( pParse==p->pParse );
85034 p->pVList = pParse->pVList;
85035 pParse->pVList = 0;
85036 db = p->db;
85037 assert( db->mallocFailed==0 );
85038 nVar = pParse->nVar;
85039 nMem = pParse->nMem;
85040 nCursor = pParse->nTab;
85041 nArg = pParse->nMaxArg;
85055 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
85056 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
85058 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
85063 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
85064 if( pParse->explain ){
85071 p->explain = pParse->explain;
85072 if( pParse->explain==2 ){
85082 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
85086 p->expired = 0;
85094 ** This two-pass approach that reuses as much memory as possible from
85099 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
85100 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
85101 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
85102 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
85104 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
85107 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
85109 if( !db->mallocFailed ){
85110 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
85111 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
85112 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
85113 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
85115 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
85120 if( db->mallocFailed ){
85121 p->nVar = 0;
85122 p->nCursor = 0;
85123 p->nMem = 0;
85125 p->nCursor = nCursor;
85126 p->nVar = (ynVar)nVar;
85127 initMemArray(p->aVar, nVar, db, MEM_Null);
85128 p->nMem = nMem;
85129 initMemArray(p->aMem, nMem, db, MEM_Undefined);
85130 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
85132 memset(p->anExec, 0, p->nOp*sizeof(i64));
85146 switch( pCx->eCurType ){
85148 sqlite3VdbeSorterClose(p->db, pCx);
85152 assert( pCx->uc.pCursor!=0 );
85153 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
85158 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
85159 const sqlite3_module *pModule = pVCur->pVtab->pModule;
85160 assert( pVCur->pVtab->nRef>0 );
85161 pVCur->pVtab->nRef--;
85162 pModule->xClose(pVCur);
85174 for(i=0; i<p->nCursor; i++){
85175 VdbeCursor *pC = p->apCsr[i];
85178 p->apCsr[i] = 0;
85185 ** is used, for example, when a trigger sub-program is halted to restore
85189 Vdbe *v = pFrame->v;
85192 v->anExec = pFrame->anExec;
85194 v->aOp = pFrame->aOp;
85195 v->nOp = pFrame->nOp;
85196 v->aMem = pFrame->aMem;
85197 v->nMem = pFrame->nMem;
85198 v->apCsr = pFrame->apCsr;
85199 v->nCursor = pFrame->nCursor;
85200 v->db->lastRowid = pFrame->lastRowid;
85201 v->nChange = pFrame->nChange;
85202 v->db->nChange = pFrame->nDbChange;
85203 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
85204 v->pAuxData = pFrame->pAuxData;
85205 pFrame->pAuxData = 0;
85206 return pFrame->pc;
85218 if( p->pFrame ){
85220 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
85222 p->pFrame = 0;
85223 p->nFrame = 0;
85225 assert( p->nFrame==0 );
85227 releaseMemArray(p->aMem, p->nMem);
85228 while( p->pDelFrame ){
85229 VdbeFrame *pDel = p->pDelFrame;
85230 p->pDelFrame = pDel->pParent;
85235 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
85236 assert( p->pAuxData==0 );
85247 sqlite3 *db = p->db;
85249 if( p->nResColumn ){
85250 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
85251 sqlite3DbFree(db, p->aColName);
85254 p->nResColumn = (u16)nResColumn;
85255 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
85256 if( p->aColName==0 ) return;
85257 initMemArray(p->aColName, n, db, MEM_Null);
85279 assert( idx<p->nResColumn );
85281 if( p->db->mallocFailed ){
85285 assert( p->aColName!=0 );
85286 pColName = &(p->aColName[idx+var*p->nResColumn]);
85287 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
85288 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
85295 ** write-transaction spanning more than one database file, this routine
85296 ** takes care of the super-journal trickery.
85300 int nTrans = 0; /* Number of databases with an active write-transaction
85301 ** that are candidates for a two-phase commit using a
85302 ** super-journal */
85315 ** be done before determining whether a super-journal file is
85324 ** one database file has an open write transaction, a super-journal
85327 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85328 Btree *pBt = db->aDb[i].pBt;
85330 /* Whether or not a database might need a super-journal depends upon
85332 ** journal modes use a super-journal and which do not */
85345 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
85360 /* If there are any write-transactions at all, invoke the commit hook */
85361 if( needXcommit && db->xCommitCallback ){
85362 rc = db->xCommitCallback(db->pCommitArg);
85368 /* The simple case - no more than one database file (not counting the
85370 ** super-journal.
85374 ** that case we do not support atomic multi-file commits, so use the
85377 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
85380 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85381 Btree *pBt = db->aDb[i].pBt;
85392 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85393 Btree *pBt = db->aDb[i].pBt;
85403 /* The complex case - There is a multi-file write-transaction active.
85404 ** This requires a super-journal file to ensure the transaction is
85409 sqlite3_vfs *pVfs = db->pVfs;
85410 char *zSuper = 0; /* File-name for the super-journal */
85411 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
85418 /* Select a super-journal file name */
85436 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
85438 /* The antipenultimate character of the super-journal name must
85440 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
85445 /* Open the super-journal. */
85452 sqlite3DbFree(db, zSuper-4);
85457 ** super-journal file. If an error occurs at this point close
85458 ** and delete the super-journal file. All the individual journal files
85459 ** still have 'null' as the super-journal pointer, so they will roll
85462 for(i=0; i<db->nDb; i++){
85463 Btree *pBt = db->aDb[i].pBt;
85475 sqlite3DbFree(db, zSuper-4);
85481 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
85489 sqlite3DbFree(db, zSuper-4);
85494 ** sets the super-journal pointer in each individual journal. If
85495 ** an error occurs here, do not delete the super-journal file.
85499 ** super-journal file will be orphaned. But we cannot delete it,
85500 ** in case the super-journal file name was written into the journal
85503 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
85504 Btree *pBt = db->aDb[i].pBt;
85512 sqlite3DbFree(db, zSuper-4);
85516 /* Delete the super-journal file. This commits the transaction. After
85521 sqlite3DbFree(db, zSuper-4);
85536 for(i=0; i<db->nDb; i++){
85537 Btree *pBt = db->aDb[i].pBt;
85556 ** This is an internal self-check only - it is not an essential processing
85559 ** This is a no-op if NDEBUG is defined.
85567 p = db->pVdbe;
85571 if( p->readOnly==0 ) nWrite++;
85572 if( p->bIsReader ) nRead++;
85574 p = p->pVNext;
85576 assert( cnt==db->nVdbeActive );
85577 assert( nWrite==db->nVdbeWrite );
85578 assert( nRead==db->nVdbeRead );
85585 ** If the Vdbe passed as the first argument opened a statement-transaction,
85595 sqlite3 *const db = p->db;
85598 const int iSavepoint = p->iStatement-1;
85601 assert( db->nStatement>0 );
85602 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
85604 for(i=0; i<db->nDb; i++){
85606 Btree *pBt = db->aDb[i].pBt;
85619 db->nStatement--;
85620 p->iStatement = 0;
85635 db->nDeferredCons = p->nStmtDefCons;
85636 db->nDeferredImmCons = p->nStmtDefImmCons;
85641 if( p->db->nStatement && p->iStatement ){
85660 sqlite3 *db = p->db;
85661 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
85662 || (!deferred && p->nFkConstraint>0)
85664 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
85665 p->errorAction = OE_Abort;
85667 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
85689 sqlite3 *db = p->db;
85707 assert( p->eVdbeState==VDBE_RUN_STATE );
85708 if( db->mallocFailed ){
85709 p->rc = SQLITE_NOMEM_BKPT;
85716 if( p->bIsReader ){
85717 int mrc; /* Primary error code from p->rc */
85725 if( p->rc ){
85726 mrc = p->rc & 0xff;
85735 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
85740 ** Even if the statement is read-only, it is important to perform
85742 ** occurred while writing to the journal, sub-journal or database
85747 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
85748 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
85756 db->autoCommit = 1;
85757 p->nChange = 0;
85763 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
85767 /* If the auto-commit flag is set and this is the only active writer
85774 && db->autoCommit
85775 && db->nVdbeWrite==(p->readOnly==0)
85777 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
85780 if( NEVER(p->readOnly) ){
85785 }else if( db->flags & SQLITE_CorruptRdOnly ){
85787 db->flags &= ~SQLITE_CorruptRdOnly;
85789 /* The auto-commit flag is true, the vdbe program was successful
85795 if( rc==SQLITE_BUSY && p->readOnly ){
85799 p->rc = rc;
85801 p->nChange = 0;
85803 db->nDeferredCons = 0;
85804 db->nDeferredImmCons = 0;
85805 db->flags &= ~(u64)SQLITE_DeferFKs;
85810 p->nChange = 0;
85812 db->nStatement = 0;
85814 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
85816 }else if( p->errorAction==OE_Abort ){
85821 db->autoCommit = 1;
85822 p->nChange = 0;
85826 /* If eStatementOp is non-zero, then a statement transaction needs to
85835 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
85836 p->rc = rc;
85837 sqlite3DbFree(db, p->zErrMsg);
85838 p->zErrMsg = 0;
85842 db->autoCommit = 1;
85843 p->nChange = 0;
85848 ** has been rolled back, update the database connection change-counter.
85850 if( p->changeCntOn ){
85852 sqlite3VdbeSetChanges(db, p->nChange);
85856 p->nChange = 0;
85864 db->nVdbeActive--;
85865 if( !p->readOnly ) db->nVdbeWrite--;
85866 if( p->bIsReader ) db->nVdbeRead--;
85867 assert( db->nVdbeActive>=db->nVdbeRead );
85868 assert( db->nVdbeRead>=db->nVdbeWrite );
85869 assert( db->nVdbeWrite>=0 );
85870 p->eVdbeState = VDBE_HALT_STATE;
85872 if( db->mallocFailed ){
85873 p->rc = SQLITE_NOMEM_BKPT;
85876 /* If the auto-commit flag is set to true, then any locks that were held
85878 ** to invoke any required unlock-notify callbacks.
85880 if( db->autoCommit ){
85884 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
85885 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
85891 ** in p->rc. This routine sets that result back to SQLITE_OK.
85894 p->rc = SQLITE_OK;
85906 sqlite3 *db = p->db;
85907 int rc = p->rc;
85908 if( p->zErrMsg ){
85909 db->bBenignMalloc++;
85911 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
85912 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
85914 db->bBenignMalloc--;
85915 }else if( db->pErr ){
85916 sqlite3ValueSetNull(db->pErr);
85918 db->errCode = rc;
85919 db->errByteOffset = -1;
85929 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
85930 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
85931 assert( v->db->init.busy==0 );
85934 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
85936 sqlite3DbFree(v->db, zExpanded);
85961 db = p->db;
85967 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
85974 if( p->pc>=0 ){
85976 if( db->pErr || p->zErrMsg ){
85979 db->errCode = p->rc;
85988 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
85989 if( p->aMem ){
85990 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
85993 if( p->zErrMsg ){
85994 sqlite3DbFree(db, p->zErrMsg);
85995 p->zErrMsg = 0;
85997 p->pResultSet = 0;
85999 p->nWrite = 0;
86008 fprintf(out, "---- ");
86009 for(i=0; i<p->nOp; i++){
86010 fprintf(out, "%02x", p->aOp[i].opcode);
86013 if( p->zSql ){
86015 fprintf(out, "-- ");
86016 for(i=0; (c = p->zSql[i])!=0; i++){
86017 if( pc=='\n' ) fprintf(out, "-- ");
86023 for(i=0; i<p->nOp; i++){
86026 p->aOp[i].cnt,
86027 p->aOp[i].cycles,
86028 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
86031 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
86037 return p->rc & db->errMask;
86049 if( p->eVdbeState>=VDBE_READY_STATE ){
86051 assert( (rc & p->db->errMask)==rc );
86077 || (pAux->iAuxOp==iOp
86078 && pAux->iAuxArg>=0
86079 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
86081 testcase( pAux->iAuxArg==31 );
86082 if( pAux->xDeleteAux ){
86083 pAux->xDeleteAux(pAux->pAux);
86085 *pp = pAux->pNextAux;
86088 pp= &pAux->pNextAux;
86104 assert( p->db==0 || p->db==db );
86105 if( p->aColName ){
86106 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
86107 sqlite3DbNNFreeNN(db, p->aColName);
86109 for(pSub=p->pProgram; pSub; pSub=pNext){
86110 pNext = pSub->pNext;
86111 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
86114 if( p->eVdbeState!=VDBE_INIT_STATE ){
86115 releaseMemArray(p->aVar, p->nVar);
86116 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
86117 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
86119 vdbeFreeOpArray(db, p->aOp, p->nOp);
86120 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
86122 sqlite3DbFree(db, p->zNormSql);
86125 for(pThis=p->pDblStr; pThis; pThis=pNext){
86126 pNext = pThis->pNextStr;
86134 for(i=0; i<p->nScan; i++){
86135 sqlite3DbFree(db, p->aScan[i].zName);
86137 sqlite3DbFree(db, p->aScan);
86149 db = p->db;
86151 assert( sqlite3_mutex_held(db->mutex) );
86153 if( db->pnBytesFreed==0 ){
86154 assert( p->ppVPrev!=0 );
86155 *p->ppVPrev = p->pVNext;
86156 if( p->pVNext ){
86157 p->pVNext->ppVPrev = p->ppVPrev;
86173 assert( p->deferredMoveto );
86174 assert( p->isTable );
86175 assert( p->eCurType==CURTYPE_BTREE );
86176 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
86182 p->deferredMoveto = 0;
86183 p->cacheStatus = CACHE_STALE;
86196 assert( p->eCurType==CURTYPE_BTREE );
86197 assert( p->uc.pCursor!=0 );
86198 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
86199 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
86200 p->cacheStatus = CACHE_STALE;
86201 if( isDifferentRow ) p->nullRow = 1;
86210 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
86211 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
86223 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
86228 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
86235 ** serial-type and data blob separately.
86240 ** -------------- --------------- ---------------
86252 ** N>=12 and even (N-12)/2 BLOB
86253 ** N>=13 and odd (N-13)/2 text
86261 ** Return the serial-type for the value stored in pMem.
86265 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
86266 ** opcode in the byte-code engine. But by moving this routine in-line, we
86272 int flags = pMem->flags;
86282 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
86283 i64 i = pMem->u.i;
86308 ** as an integer, then we might as well make it an 8-byte floating
86310 pMem->u.r = (double)pMem->u.i;
86311 pMem->flags &= ~MEM_IntReal;
86312 pMem->flags |= MEM_Real;
86321 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
86322 assert( pMem->n>=0 );
86323 n = (u32)pMem->n;
86325 n += pMem->u.nZero;
86353 ** Return the length of the data corresponding to the supplied serial-type.
86357 return (serial_type-12)/2;
86360 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
86370 ** If we are on an architecture with mixed-endian floating
86374 ** For most architectures, this is a no-op.
86376 ** (later): It is reported to me that the mixed-endian problem
86378 ** that early versions of GCC stored the two words of a 64-bit
86386 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
86390 ** (2007-08-30) Frank van Vugt has studied this problem closely
86393 ** emulation that uses only 32-bit mantissas instead of a full
86394 ** 48-bits as required by the IEEE standard. (This is the
86397 ** the necessary byte swapping is carried out using a 64-bit integer
86398 ** rather than a 64-bit float. Frank assures us that the code here
86421 ** big-endian integer. Return the equivalent native integer
86447 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
86448 ** twos-complement integer. */
86449 pMem->u.i = *(i64*)&x;
86450 pMem->flags = MEM_Int;
86451 testcase( pMem->u.i<0 );
86453 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
86458 ** defined that 64-bit floating point values really are mixed
86467 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
86469 memcpy(&pMem->u.r, &x, sizeof(x));
86470 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
86480 ** UPDATE no-change flag set */
86481 pMem->flags = MEM_Null|MEM_Zero;
86482 pMem->n = 0;
86483 pMem->u.nZero = 0;
86488 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
86489 pMem->flags = MEM_Null;
86493 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
86495 pMem->u.i = ONE_BYTE_INT(buf);
86496 pMem->flags = MEM_Int;
86497 testcase( pMem->u.i<0 );
86500 case 2: { /* 2-byte signed integer */
86501 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
86502 ** twos-complement integer. */
86503 pMem->u.i = TWO_BYTE_INT(buf);
86504 pMem->flags = MEM_Int;
86505 testcase( pMem->u.i<0 );
86508 case 3: { /* 3-byte signed integer */
86509 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
86510 ** twos-complement integer. */
86511 pMem->u.i = THREE_BYTE_INT(buf);
86512 pMem->flags = MEM_Int;
86513 testcase( pMem->u.i<0 );
86516 case 4: { /* 4-byte signed integer */
86517 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
86518 ** twos-complement integer. */
86519 pMem->u.i = FOUR_BYTE_INT(buf);
86521 /* Work around a sign-extension bug in the HP compiler for HP/UX */
86522 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
86524 pMem->flags = MEM_Int;
86525 testcase( pMem->u.i<0 );
86528 case 5: { /* 6-byte signed integer */
86529 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
86530 ** twos-complement integer. */
86531 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
86532 pMem->flags = MEM_Int;
86533 testcase( pMem->u.i<0 );
86536 case 6: /* 8-byte signed integer */
86545 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
86546 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
86547 pMem->u.i = serial_type-8;
86548 pMem->flags = MEM_Int;
86552 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
86554 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
86555 ** (N-13)/2 bytes in length. */
86557 pMem->z = (char *)buf;
86558 pMem->n = (serial_type-12)/2;
86559 pMem->flags = aFlag[serial_type&1];
86584 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
86585 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
86587 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
86588 assert( pKeyInfo->aSortFlags!=0 );
86589 p->pKeyInfo = pKeyInfo;
86590 p->nField = pKeyInfo->nKeyField + 1;
86595 ** Given the nKey-byte encoding of a record in pKey[], populate the
86610 Mem *pMem = p->aMem;
86612 p->default_rc = 0;
86621 pMem->enc = pKeyInfo->enc;
86622 pMem->db = pKeyInfo->db;
86623 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
86624 pMem->szMalloc = 0;
86625 pMem->z = 0;
86629 if( (++u)>=p->nField ) break;
86636 sqlite3VdbeMemSetNull(pMem-1);
86638 assert( u<=pKeyInfo->nKeyField + 1 );
86639 p->nField = u;
86668 pKeyInfo = pPKey2->pKeyInfo;
86669 if( pKeyInfo->db==0 ) return 1;
86670 mem1.enc = pKeyInfo->enc;
86671 mem1.db = pKeyInfo->db;
86687 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
86688 assert( pKeyInfo->aSortFlags!=0 );
86689 assert( pKeyInfo->nKeyField>0 );
86716 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
86717 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
86720 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
86721 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
86723 rc = -rc;
86725 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
86726 rc = -rc; /* Invert the result for DESC sort order. */
86731 }while( idx1<szHdr1 && i<pPKey2->nField );
86742 rc = pPKey2->default_rc;
86749 if( pKeyInfo->db->mallocFailed ) return 1;
86758 ** limit given by pKeyInfo->nAllField.
86760 ** If this constraint is not satisfied, it means that the high-speed
86784 assert( nField <= pKeyInfo->nAllField );
86794 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
86802 if( pMem1->enc==pColl->enc ){
86805 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
86811 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
86812 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
86815 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
86816 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
86821 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
86831 ** with MEM_Zero. Return true if it could be a zero-blob.
86848 int n1 = pB1->n;
86849 int n2 = pB2->n;
86851 /* It is possible to have a Blob value that has some non-zero content
86855 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
86856 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
86858 if( (pB1->flags|pB2->flags) & MEM_Zero ){
86859 if( pB1->flags & pB2->flags & MEM_Zero ){
86860 return pB1->u.nZero - pB2->u.nZero;
86861 }else if( pB1->flags & MEM_Zero ){
86862 if( !isAllZero(pB2->z, pB2->n) ) return -1;
86863 return pB1->u.nZero - n2;
86865 if( !isAllZero(pB1->z, pB1->n) ) return +1;
86866 return n1 - pB2->u.nZero;
86869 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
86871 return n1 - n2;
86875 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
86885 if( x<r ) return -1;
86891 if( r<-9223372036854775808.0 ) return +1;
86892 if( r>=9223372036854775808.0 ) return -1;
86894 if( i<y ) return -1;
86897 if( s<r ) return -1;
86916 f1 = pMem1->flags;
86917 f2 = pMem2->flags;
86925 return (f2&MEM_Null) - (f1&MEM_Null);
86937 if( pMem1->u.i < pMem2->u.i ) return -1;
86938 if( pMem1->u.i > pMem2->u.i ) return +1;
86942 if( pMem1->u.r < pMem2->u.r ) return -1;
86943 if( pMem1->u.r > pMem2->u.r ) return +1;
86950 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
86952 if( pMem1->u.i < pMem2->u.i ) return -1;
86953 if( pMem1->u.i > pMem2->u.i ) return +1;
86956 return -1;
86963 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
86965 return -1;
86979 return -1;
86982 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
86983 assert( pMem1->enc==SQLITE_UTF8 ||
86984 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
86990 assert( !pColl || pColl->xCmp );
87005 ** The first argument passed to this function is a serial-type that
87006 ** corresponds to an integer - all values between 1 and 9 inclusive
87042 return (serial_type - 8);
87054 ** If argument bSkip is non-zero, it is assumed that the caller has already
87058 ** fields that appear in both keys are equal, then pPKey2->default_rc is
87061 ** If database corruption is discovered, set pPKey2->errCode to
87063 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
87064 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
87076 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
87105 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87110 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
87112 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
87113 assert( pPKey2->pKeyInfo->nKeyField>0 );
87115 while( 1 /*exit-by-break*/ ){
87119 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
87120 testcase( pRhs->flags & MEM_Int );
87121 testcase( pRhs->flags & MEM_IntReal );
87125 rc = serial_type==10 ? -1 : +1;
87127 rc = -1;
87130 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
87133 i64 rhs = pRhs->u.i;
87135 rc = -1;
87143 else if( pRhs->flags & MEM_Real ){
87150 rc = serial_type==10 ? -1 : +1;
87152 rc = -1;
87156 if( mem1.u.r<pRhs->u.r ){
87157 rc = -1;
87158 }else if( mem1.u.r>pRhs->u.r ){
87162 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
87168 else if( pRhs->flags & MEM_Str ){
87172 rc = -1;
87176 mem1.n = (serial_type - 12) / 2;
87180 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
87182 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87184 }else if( pKeyInfo->aColl[i] ){
87185 mem1.enc = pKeyInfo->enc;
87186 mem1.db = pKeyInfo->db;
87190 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
87193 int nCmp = MIN(mem1.n, pRhs->n);
87194 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87195 if( rc==0 ) rc = mem1.n - pRhs->n;
87201 else if( pRhs->flags & MEM_Blob ){
87202 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
87206 rc = -1;
87208 int nStr = (serial_type - 12) / 2;
87212 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87214 }else if( pRhs->flags & MEM_Zero ){
87218 rc = nStr - pRhs->u.nZero;
87221 int nCmp = MIN(nStr, pRhs->n);
87222 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
87223 if( rc==0 ) rc = nStr - pRhs->n;
87235 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
87239 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
87241 rc = -rc;
87250 if( i==pPKey2->nField ) break;
87256 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87270 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
87271 || pPKey2->pKeyInfo->db->mallocFailed
87273 pPKey2->eqSeen = 1;
87274 return pPKey2->default_rc;
87287 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
87305 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
87308 case 1: { /* 1-byte signed integer */
87313 case 2: { /* 2-byte signed integer */
87318 case 3: { /* 3-byte signed integer */
87323 case 4: { /* 4-byte signed integer */
87329 case 5: { /* 6-byte signed integer */
87334 case 6: { /* 8-byte signed integer */
87361 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
87362 v = pPKey2->u.i;
87364 res = pPKey2->r1;
87366 res = pPKey2->r2;
87367 }else if( pPKey2->nField>1 ){
87373 ** fields. Return pPKey2->default_rc in this case. */
87374 res = pPKey2->default_rc;
87375 pPKey2->eqSeen = 1;
87385 ** uses the collation sequence BINARY and (c) that the size-of-header varint
87396 assert( pPKey2->aMem[0].flags & MEM_Str );
87397 assert( pPKey2->aMem[0].n == pPKey2->n );
87398 assert( pPKey2->aMem[0].z == pPKey2->u.z );
87399 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
87409 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
87411 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
87417 nStr = (serial_type-12) / 2;
87419 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
87422 nCmp = MIN( pPKey2->n, nStr );
87423 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
87426 res = pPKey2->r2;
87428 res = pPKey2->r1;
87430 res = nStr - pPKey2->n;
87432 if( pPKey2->nField>1 ){
87435 res = pPKey2->default_rc;
87436 pPKey2->eqSeen = 1;
87439 res = pPKey2->r2;
87441 res = pPKey2->r1;
87448 || pPKey2->pKeyInfo->db->mallocFailed
87460 ** that the size-of-header varint that occurs at the start of each record
87472 if( p->pKeyInfo->nAllField<=13 ){
87473 int flags = p->aMem[0].flags;
87474 if( p->pKeyInfo->aSortFlags[0] ){
87475 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
87478 p->r1 = 1;
87479 p->r2 = -1;
87481 p->r1 = -1;
87482 p->r2 = 1;
87485 p->u.i = p->aMem[0].u.i;
87492 && p->pKeyInfo->aColl[0]==0
87495 p->u.z = p->aMem[0].z;
87496 p->n = p->aMem[0].n;
87521 ** than 2GiB are support - anything large must be database corruption.
87523 ** this code can safely assume that nCellKey is 32-bits
87546 /* The last field of the index should be an integer - the ROWID.
87548 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
87567 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
87602 assert( pC->eCurType==CURTYPE_BTREE );
87603 pCur = pC->uc.pCursor;
87627 assert( sqlite3_mutex_held(db->mutex) );
87628 db->nChange = nChange;
87629 db->nTotalChange += nChange;
87637 v->changeCntOn = 1;
87646 ** programs obsolete. Removing user-defined functions or collating
87660 for(p = db->pVdbe; p; p=p->pVNext){
87661 p->expired = iCode+1;
87669 return v->db;
87676 return v->prepFlags;
87690 Mem *pMem = &v->aVar[iVar-1];
87691 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
87692 if( 0==(pMem->flags & MEM_Null) ){
87693 sqlite3_value *pRet = sqlite3ValueNew(v->db);
87706 ** to sqlite3_reoptimize() that re-preparing the statement may result
87711 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
87713 v->expmask |= 0x80000000;
87715 v->expmask |= ((u32)1 << (iVar-1));
87724 ** throw an error if it is given inputs that would make it non-deterministic.
87725 ** This routine is invoked by date/time functions that use non-deterministic
87731 if( pCtx->pVdbe==0 ) return 1;
87733 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
87734 if( pOp->opcode==OP_PureFunc ){
87737 if( pOp->p5 & NC_IsCheck ){
87739 }else if( pOp->p5 & NC_GenCol ){
87744 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
87745 pCtx->pFunc->zName, zContext);
87746 sqlite3_result_error(pCtx, zMsg, -1);
87760 if( pVtab->zErrMsg ){
87761 sqlite3 *db = p->db;
87762 sqlite3DbFree(db, p->zErrMsg);
87763 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
87764 sqlite3_free(pVtab->zErrMsg);
87765 pVtab->zErrMsg = 0;
87774 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
87785 Mem *pMem = &p->aMem[i];
87786 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
87795 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
87801 Vdbe *v, /* Vdbe pre-update hook is invoked by */
87810 sqlite3 *db = v->db;
87813 const char *zTbl = pTab->zName;
87816 assert( db->pPreUpdate==0 );
87823 iKey2 = v->aMem[iReg].u.i;
87830 assert( pCsr->eCurType==CURTYPE_BTREE );
87831 assert( pCsr->nField==pTab->nCol
87832 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
87841 preupdate.keyinfo.nKeyField = pTab->nCol;
87848 db->pPreUpdate = &preupdate;
87849 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
87850 db->pPreUpdate = 0;
87856 for(i=0; i<pCsr->nField; i++){
87886 ** Return TRUE (non-zero) of the statement supplied as an argument needs
87895 return p==0 || p->expired;
87905 if( p->db==0 ){
87929 assert( p->startTime>0 );
87930 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
87931 assert( db->init.busy==0 );
87932 assert( p->zSql!=0 );
87933 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
87934 iElapse = (iNow - p->startTime)*1000000;
87936 if( db->xProfile ){
87937 db->xProfile(db->pProfileArg, p->zSql, iElapse);
87940 if( db->mTrace & SQLITE_TRACE_PROFILE ){
87941 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
87943 p->startTime = 0;
87950 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
87952 # define checkProfileCallback(DB,P) /*no-op*/
87967 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
87968 ** pointer is a harmless no-op. */
87972 sqlite3 *db = v->db;
87974 sqlite3_mutex_enter(db->mutex);
87976 assert( v->eVdbeState>=VDBE_READY_STATE );
87999 sqlite3 *db = v->db;
88000 sqlite3_mutex_enter(db->mutex);
88004 assert( (rc & (db->errMask))==rc );
88006 sqlite3_mutex_leave(db->mutex);
88019 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
88022 for(i=0; i<p->nVar; i++){
88023 sqlite3VdbeMemRelease(&p->aVar[i]);
88024 p->aVar[i].flags = MEM_Null;
88026 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
88027 if( p->expmask ){
88028 p->expired = 1;
88041 if( p->flags & (MEM_Blob|MEM_Str) ){
88043 assert( p->flags==MEM_Null && p->z==0 );
88046 p->flags |= MEM_Blob;
88047 return p->n ? p->z : 0;
88069 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
88073 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
88076 && p->eSubtype=='p'
88077 && strcmp(p->u.zPType, zPType)==0
88079 return (void*)p->z;
88098 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
88099 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
88172 if( pVal->flags & MEM_Null ){
88174 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
88176 }else if( pVal->flags & MEM_Int ){
88178 }else if( pVal->flags & MEM_Str ){
88181 assert( eType == aType[pVal->flags&MEM_AffMask] );
88184 return aType[pVal->flags&MEM_AffMask];
88187 return pVal->enc;
88192 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
88197 return (pVal->flags&MEM_FromBind)!=0;
88209 pNew->flags &= ~MEM_Dyn;
88210 pNew->db = 0;
88211 if( pNew->flags&(MEM_Str|MEM_Blob) ){
88212 pNew->flags &= ~(MEM_Static|MEM_Dyn);
88213 pNew->flags |= MEM_Ephem;
88218 }else if( pNew->flags & MEM_Null ){
88220 pNew->flags &= ~(MEM_Term|MEM_Subtype);
88234 ** The following routines are used by user-defined functions to specify
88251 Mem *pOut = pCtx->pOut;
88264 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
88292 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88301 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88310 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88311 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
88314 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88315 pCtx->isError = SQLITE_ERROR;
88316 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
88320 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88321 pCtx->isError = SQLITE_ERROR;
88322 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
88326 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88327 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
88330 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88331 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
88334 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88335 sqlite3VdbeMemSetNull(pCtx->pOut);
88343 Mem *pOut = pCtx->pOut;
88344 assert( sqlite3_mutex_held(pOut->db->mutex) );
88346 pOut->flags = MEM_Null;
88350 Mem *pOut = pCtx->pOut;
88351 assert( sqlite3_mutex_held(pOut->db->mutex) );
88352 pOut->eSubtype = eSubtype & 0xff;
88353 pOut->flags |= MEM_Subtype;
88361 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88371 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88387 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88396 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88405 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88410 Mem *pOut = pCtx->pOut;
88411 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88413 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
88422 Mem *pOut = pCtx->pOut;
88423 assert( sqlite3_mutex_held(pOut->db->mutex) );
88424 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
88429 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
88432 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
88436 pCtx->isError = errCode ? errCode : -1;
88438 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
88440 if( pCtx->pOut->flags & MEM_Null ){
88441 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
88448 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88449 pCtx->isError = SQLITE_TOOBIG;
88450 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
88456 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88457 sqlite3VdbeMemSetNull(pCtx->pOut);
88458 pCtx->isError = SQLITE_NOMEM_BKPT;
88459 sqlite3OomFault(pCtx->pOut->db);
88465 ** test-control.
88468 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88469 if( pCtx->pOut->flags & MEM_Int ){
88470 pCtx->pOut->flags &= ~MEM_Int;
88471 pCtx->pOut->flags |= MEM_IntReal;
88485 for(i=0; i<db->nDb; i++){
88486 Btree *pBt = db->aDb[i].pBt;
88492 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
88493 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
88516 db = p->db;
88517 if( p->eVdbeState!=VDBE_RUN_STATE ){
88519 if( p->eVdbeState==VDBE_READY_STATE ){
88520 if( p->expired ){
88521 p->rc = SQLITE_SCHEMA;
88523 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
88525 ** error has occurred, then return the error code in p->rc to the
88538 if( db->nVdbeActive==0 ){
88539 AtomicStore(&db->u1.isInterrupted, 0);
88542 assert( db->nVdbeWrite>0 || db->autoCommit==0
88543 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
88547 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
88548 && !db->init.busy && p->zSql ){
88549 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
88551 assert( p->startTime==0 );
88555 db->nVdbeActive++;
88556 if( p->readOnly==0 ) db->nVdbeWrite++;
88557 if( p->bIsReader ) db->nVdbeRead++;
88558 p->pc = 0;
88559 p->eVdbeState = VDBE_RUN_STATE;
88562 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
88567 ** This "automatic-reset" change is not technically an incompatibility,
88573 ** returns, and those were broken by the automatic-reset change. As a
88574 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
88580 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
88588 assert( p->eVdbeState==VDBE_READY_STATE );
88594 p->rcApp = SQLITE_OK;
88597 if( p->explain ){
88602 db->nVdbeExec++;
88604 db->nVdbeExec--;
88608 assert( p->rc==SQLITE_OK );
88609 assert( db->mallocFailed==0 );
88610 db->errCode = SQLITE_ROW;
88618 if( rc==SQLITE_DONE && db->autoCommit ){
88619 assert( p->rc==SQLITE_OK );
88620 p->rc = doWalCallbacks(db);
88621 if( p->rc!=SQLITE_OK ){
88624 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
88626 ** error has occurred, then return the error code in p->rc to the
88633 db->errCode = rc;
88634 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
88635 p->rc = SQLITE_NOMEM_BKPT;
88636 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
88641 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
88645 return (rc&db->errMask);
88650 sqlite3_mutex_enter(db->mutex);
88651 db->xDropTableHandle = xFunc;
88652 sqlite3_mutex_leave(db->mutex);
88658 ** This is the top-level implementation of sqlite3_step(). Call
88671 db = v->db;
88672 sqlite3_mutex_enter(db->mutex);
88675 int savedPc = v->pc;
88677 Sqlite3SharedBlockMethods *pSharedBlock = v->pSharedBlock;
88678 int totalRows = v->totalRows;
88679 int blockFull = v->blockFull;
88680 int startPos = v->startPos;
88681 int addedRows = v->addedRows;
88693 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
88694 sqlite3DbFree(db, v->zErrMsg);
88695 if( !db->mallocFailed ){
88696 v->zErrMsg = sqlite3DbStrDup(db, zErr);
88697 v->rc = rc = sqlite3ApiExit(db, rc);
88699 v->zErrMsg = 0;
88700 v->rc = rc = SQLITE_NOMEM_BKPT;
88706 v->pSharedBlock = pSharedBlock;
88707 v->totalRows = totalRows;
88708 v->blockFull = blockFull;
88709 v->startPos = startPos;
88710 v->addedRows = addedRows;
88718 ** SQLITE_SCHEMA. tag-20220401a */
88719 v->minWriteFileFormat = 254;
88721 assert( v->expired==0 );
88724 if( rc==SQLITE_DONE && db->xDropTableHandle!=NULL && db->isDropTable==1 ){
88725 db->isDropTable = 0;
88726 db->xDropTableHandle(db, db->mDropTableName, db->mDropSchemaName);
88728 if( db->mDropTableName!=NULL ){
88729 sqlite3_free(db->mDropTableName);
88730 db->mDropTableName = NULL;
88732 if( db->mDropSchemaName!=NULL ){
88733 sqlite3_free(db->mDropSchemaName);
88734 db->mDropSchemaName = NULL;
88737 sqlite3_mutex_leave(db->mutex);
88747 assert( p && p->pFunc );
88748 return p->pFunc->pUserData;
88755 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
88762 assert( p && p->pOut );
88763 return p->pOut->db;
88777 ** performance by substituting a NULL result, or some other light-weight
88782 return sqlite3_value_nochange(p->pOut);
88802 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
88805 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
88806 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
88807 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
88813 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
88814 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
88818 sqlite3_value *pOut = pRhs->pOut;
88821 pOut->enc = ENC(pOut->db);
88822 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
88859 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
88860 assert( p->pVdbe!=0 );
88863 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
88866 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
88874 ** its pMem->z element.
88877 Mem *pMem = p->pMem;
88878 assert( (pMem->flags & MEM_Agg)==0 );
88881 pMem->z = 0;
88884 pMem->flags = MEM_Agg;
88885 pMem->u.pDef = p->pFunc;
88886 if( pMem->z ){
88887 memset(pMem->z, 0, nByte);
88890 return (void*)pMem->z;
88899 assert( p && p->pFunc && p->pFunc->xFinalize );
88900 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
88902 if( (p->pMem->flags & MEM_Agg)==0 ){
88905 return (void*)p->pMem->z;
88911 ** the user-function defined by pCtx.
88913 ** The left-most argument is 0.
88922 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88924 if( pCtx->pVdbe==0 ) return 0;
88926 assert( pCtx->pVdbe!=0 );
88928 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
88929 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
88930 return pAuxData->pAux;
88938 ** argument to the user-function defined by pCtx. Any previous value is
88941 ** The left-most argument is 0.
88954 Vdbe *pVdbe = pCtx->pVdbe;
88956 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
88963 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
88964 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
88969 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
88971 pAuxData->iAuxOp = pCtx->iOp;
88972 pAuxData->iAuxArg = iArg;
88973 pAuxData->pNextAux = pVdbe->pAuxData;
88974 pVdbe->pAuxData = pAuxData;
88975 if( pCtx->isError==0 ) pCtx->isError = -1;
88976 }else if( pAuxData->xDeleteAux ){
88977 pAuxData->xDeleteAux(pAuxData->pAux);
88980 pAuxData->pAux = pAux;
88981 pAuxData->xDeleteAux = xDelete;
89001 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
89002 return p->pMem->n;
89011 return pVm ? pVm->nResColumn : 0;
89020 if( pVm==0 || pVm->pResultSet==0 ) return 0;
89021 return pVm->nResColumn;
89030 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
89031 ** instead of an 8-byte one. This all works fine, except that when
89033 ** that a Mem structure is located on an 8-byte boundary. To prevent
89035 ** using gcc, we force nullMem to be 8-byte aligned using the magical
89073 assert( pVm->db );
89074 sqlite3_mutex_enter(pVm->db->mutex);
89075 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
89076 pOut = &pVm->pResultSet[i];
89078 sqlite3Error(pVm->db, SQLITE_RANGE);
89111 assert( p->db!=0 );
89112 assert( sqlite3_mutex_held(p->db->mutex) );
89113 p->rc = sqlite3ApiExit(p->db, p->rc);
89114 sqlite3_mutex_leave(p->db->mutex);
89164 if( pOut->flags&MEM_Static ){
89165 pOut->flags &= ~MEM_Static;
89166 pOut->flags |= MEM_Ephem;
89185 ** Convert the N-th element of pStmt->pColName[] into a string using
89218 db = p->db;
89223 sqlite3_mutex_enter(db->mutex);
89224 assert( db->mallocFailed==0 );
89227 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
89231 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
89236 if( db->mallocFailed ){
89240 sqlite3_mutex_leave(db->mutex);
89339 ** The error code stored in database p->db is overwritten with the return
89347 sqlite3_mutex_enter(p->db->mutex);
89348 if( p->eVdbeState!=VDBE_READY_STATE ){
89349 sqlite3Error(p->db, SQLITE_MISUSE);
89350 sqlite3_mutex_leave(p->db->mutex);
89352 "bind on a busy prepared statement: [%s]", p->zSql);
89355 if( i>=(unsigned int)p->nVar ){
89356 sqlite3Error(p->db, SQLITE_RANGE);
89357 sqlite3_mutex_leave(p->db->mutex);
89360 pVar = &p->aVar[i];
89362 pVar->flags = MEM_Null;
89363 p->db->errCode = SQLITE_OK;
89368 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
89374 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
89375 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
89376 p->expired = 1;
89396 rc = vdbeUnbind(p, (u32)(i-1));
89399 pVar = &p->aVar[i-1];
89402 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
89405 sqlite3Error(p->db, rc);
89406 rc = sqlite3ApiExit(p->db, rc);
89409 sqlite3_mutex_leave(p->db->mutex);
89445 rc = vdbeUnbind(p, (u32)(i-1));
89447 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
89448 sqlite3_mutex_leave(p->db->mutex);
89458 rc = vdbeUnbind(p, (u32)(i-1));
89460 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
89461 sqlite3_mutex_leave(p->db->mutex);
89468 rc = vdbeUnbind(p, (u32)(i-1));
89470 sqlite3_mutex_leave(p->db->mutex);
89483 rc = vdbeUnbind(p, (u32)(i-1));
89485 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
89486 sqlite3_mutex_leave(p->db->mutex);
89528 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
89532 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
89534 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
89539 if( pValue->flags & MEM_Zero ){
89540 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
89542 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
89547 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
89548 pValue->enc);
89561 rc = vdbeUnbind(p, (u32)(i-1));
89564 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
89566 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
89568 sqlite3_mutex_leave(p->db->mutex);
89575 sqlite3_mutex_enter(p->db->mutex);
89576 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
89582 rc = sqlite3ApiExit(p->db, rc);
89583 sqlite3_mutex_leave(p->db->mutex);
89593 return p ? p->nVar : 0;
89600 ** The result is always UTF-8.
89605 return sqlite3VListNumToName(p->pVList, i);
89615 return sqlite3VListNameToNum(p->pVList, zName, nName);
89628 assert( pTo->db==pFrom->db );
89629 assert( pTo->nVar==pFrom->nVar );
89630 sqlite3_mutex_enter(pTo->db->mutex);
89631 for(i=0; i<pFrom->nVar; i++){
89632 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
89634 sqlite3_mutex_leave(pTo->db->mutex);
89654 if( pFrom->nVar!=pTo->nVar ){
89657 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
89658 if( pTo->expmask ){
89659 pTo->expired = 1;
89661 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
89662 if( pFrom->expmask ){
89663 pFrom->expired = 1;
89676 return pStmt ? ((Vdbe*)pStmt)->db : 0;
89684 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
89692 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
89700 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
89717 sqlite3_mutex_enter(pDb->mutex);
89719 pNext = (sqlite3_stmt*)pDb->pVdbe;
89721 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
89723 sqlite3_mutex_leave(pDb->mutex);
89735 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
89742 sqlite3 *db = pVdbe->db;
89743 sqlite3_mutex_enter(db->mutex);
89745 db->pnBytesFreed = (int*)&v;
89746 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
89747 db->lookaside.pEnd = db->lookaside.pStart;
89749 db->pnBytesFreed = 0;
89750 db->lookaside.pEnd = db->lookaside.pTrueEnd;
89751 sqlite3_mutex_leave(db->mutex);
89753 v = pVdbe->aCounter[op];
89754 if( resetFlag ) pVdbe->aCounter[op] = 0;
89764 return p ? p->zSql : 0;
89784 sqlite3_mutex_enter(p->db->mutex);
89786 sqlite3_mutex_leave(p->db->mutex);
89799 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
89800 sqlite3_mutex_enter(p->db->mutex);
89801 p->zNormSql = sqlite3Normalize(p, p->zSql);
89802 sqlite3_mutex_leave(p->db->mutex);
89804 return p->zNormSql;
89823 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
89830 ** This function is called from within a pre-update callback to retrieve
89834 PreUpdate *p = db->pPreUpdate;
89839 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
89840 if( !p || p->op==SQLITE_INSERT ){
89844 if( p->pPk ){
89845 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
89847 if( iIdx>=p->pCsr->nField || iIdx<0 ){
89853 if( p->pUnpacked==0 ){
89857 assert( p->pCsr->eCurType==CURTYPE_BTREE );
89858 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
89861 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
89863 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
89864 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
89870 p->aRecord = aRec;
89873 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
89874 if( iIdx==p->pTab->iPKey ){
89875 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
89876 }else if( iIdx>=p->pUnpacked->nField ){
89878 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
89879 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
89880 testcase( pMem->flags & MEM_Int );
89881 testcase( pMem->flags & MEM_IntReal );
89894 ** This function is called from within a pre-update callback to retrieve
89898 PreUpdate *p = db->pPreUpdate;
89899 return (p ? p->keyinfo.nKeyField : 0);
89905 ** This function is designed to be called from within a pre-update callback
89909 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
89910 ** top-level trigger etc.).
89916 PreUpdate *p = db->pPreUpdate;
89917 return (p ? p->v->nFrame : 0);
89923 ** This function is designed to be called from within a pre-update callback
89927 PreUpdate *p = db->pPreUpdate;
89928 return (p ? p->iBlobWrite : -1);
89934 ** This function is called from within a pre-update callback to retrieve
89938 PreUpdate *p = db->pPreUpdate;
89942 if( !p || p->op==SQLITE_DELETE ){
89946 if( p->pPk && p->op!=SQLITE_UPDATE ){
89947 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
89949 if( iIdx>=p->pCsr->nField || iIdx<0 ){
89954 if( p->op==SQLITE_INSERT ){
89955 /* For an INSERT, memory cell p->iNewReg contains the serialized record
89957 UnpackedRecord *pUnpack = p->pNewUnpacked;
89959 Mem *pData = &p->v->aMem[p->iNewReg];
89962 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
89967 p->pNewUnpacked = pUnpack;
89969 pMem = &pUnpack->aMem[iIdx];
89970 if( iIdx==p->pTab->iPKey ){
89971 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
89972 }else if( iIdx>=pUnpack->nField ){
89976 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
89981 assert( p->op==SQLITE_UPDATE );
89982 if( !p->aNew ){
89983 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
89984 if( !p->aNew ){
89989 assert( iIdx>=0 && iIdx<p->pCsr->nField );
89990 pMem = &p->aNew[iIdx];
89991 if( pMem->flags==0 ){
89992 if( iIdx==p->pTab->iPKey ){
89993 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
89995 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
90020 if( idx<0 || idx>=p->nScan ) return 1;
90021 pScan = &p->aScan[idx];
90024 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
90028 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
90033 LogEst x = pScan->nEst;
90042 *(const char**)pOut = pScan->zName;
90046 if( pScan->addrExplain ){
90047 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
90054 if( pScan->addrExplain ){
90055 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
90057 *(int*)pOut = -1;
90073 memset(p->anExec, 0, p->nOp * sizeof(i64));
90094 ** The Vdbe parse-tree explainer is also found here.
90102 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
90127 ** This function returns a pointer to a nul-terminated string in memory
90131 ** then the returned string holds a copy of zRawSql with "-- " prepended
90137 ** with large (multi-megabyte) strings and blobs.
90147 ** parameter index is known, locate the value in p->aVar[]. Then render
90166 db = p->db;
90167 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
90168 if( db->nVdbeExec>1 ){
90172 sqlite3_str_append(&out, "-- ", 3);
90173 assert( (zRawSql - zStart) > 0 );
90174 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
90176 }else if( p->nVar==0 ){
90205 assert( idx>0 && idx<=p->nVar );
90206 pVar = &p->aVar[idx-1];
90207 if( pVar->flags & MEM_Null ){
90209 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
90210 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
90211 }else if( pVar->flags & MEM_Real ){
90212 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
90213 }else if( pVar->flags & MEM_Str ){
90220 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
90228 nOut = pVar->n;
90232 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
90235 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
90237 if( nOut<pVar->n ){
90238 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90244 }else if( pVar->flags & MEM_Zero ){
90245 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
90248 assert( pVar->flags & MEM_Blob );
90250 nOut = pVar->n;
90255 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
90259 if( nOut<pVar->n ){
90260 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
90349 ** use this information to make sure that the zero-blob functionality
90356 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
90357 sqlite3_max_blobsize = p->n;
90367 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
90369 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
90421 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
90422 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
90423 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
90425 ** In other words, if M is 2, then I is either 0 (for fall-through) or
90427 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
90438 ** always taken, the flags should be 0x05 since the fall-through and
90440 ** flags should be 0x06 since only the fall-through approach is allowed.
90443 ** interested in equal or not-equal. In other words, I==0 and I==2
90448 ** should be no-ops except for special builds used to measure test coverage.
90453 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
90457 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
90469 ** iSrcLine - the line number of the VdbeCoverage() macro, with
90471 ** I - Mask of bits 0x07 indicating which cases are are
90473 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
90478 ** M - Type of jump. Same as M argument above
90503 if( ((P)->flags&MEM_Ephem)!=0 \
90507 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
90534 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
90535 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
90537 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
90545 assert( iCur>=0 && iCur<p->nCursor );
90546 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
90547 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
90548 p->apCsr[iCur] = 0;
90553 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
90554 ** to hold cursors, it is faster to in-line the logic. */
90555 assert( pMem->flags==MEM_Undefined );
90556 assert( (pMem->flags & MEM_Dyn)==0 );
90557 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
90558 if( pMem->szMalloc<nByte ){
90559 if( pMem->szMalloc>0 ){
90560 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
90562 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
90563 if( pMem->zMalloc==0 ){
90564 pMem->szMalloc = 0;
90567 pMem->szMalloc = nByte;
90570 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
90572 pCx->eCurType = eCurType;
90573 pCx->nField = nField;
90574 pCx->aOffset = &pCx->aType[nField];
90576 pCx->uc.pCursor = (BtCursor*)
90577 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
90578 sqlite3BtreeCursorZero(pCx->uc.pCursor);
90596 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
90616 u8 enc = pRec->enc;
90618 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
90619 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
90621 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
90622 pRec->flags |= MEM_Int;
90624 pRec->u.r = rValue;
90625 pRec->flags |= MEM_Real;
90628 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
90631 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
90632 pRec->flags &= ~MEM_Str;
90642 ** floating-point representation if an integer representation
90652 ** No-op. pRec is unchanged.
90662 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
90663 if( (pRec->flags & MEM_Real)==0 ){
90664 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
90675 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
90676 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
90677 testcase( pRec->flags & MEM_Int );
90678 testcase( pRec->flags & MEM_Real );
90679 testcase( pRec->flags & MEM_IntReal );
90683 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
90718 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
90724 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
90725 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
90727 pMem->u.i = 0;
90730 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
90732 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
90733 pMem->u.i = ix;
90738 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
90739 pMem->u.i = ix;
90749 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
90750 ** But it does set pMem->u.r and pMem->u.i appropriately.
90753 assert( (pMem->flags & MEM_Null)==0
90754 || pMem->db==0 || pMem->db->mallocFailed );
90755 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
90756 testcase( pMem->flags & MEM_Int );
90757 testcase( pMem->flags & MEM_Real );
90758 testcase( pMem->flags & MEM_IntReal );
90759 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
90761 assert( pMem->flags & (MEM_Str|MEM_Blob) );
90762 testcase( pMem->flags & MEM_Str );
90763 testcase( pMem->flags & MEM_Blob );
90773 int f = pMem->flags;
90791 for(i=0; i<25 && i<pMem->n; i++){
90792 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
90795 for(i=0; i<25 && i<pMem->n; i++){
90796 char z = pMem->z[i];
90801 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
90818 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
90819 for(j=0; j<25 && j<pMem->n; j++){
90820 c = pMem->z[j];
90823 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
90833 if( p->flags & MEM_Undefined ){
90835 }else if( p->flags & MEM_Null ){
90836 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
90837 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
90838 printf(" si:%lld", p->u.i);
90839 }else if( (p->flags & (MEM_IntReal))!=0 ){
90840 printf(" ir:%lld", p->u.i);
90841 }else if( p->flags & MEM_Int ){
90842 printf(" i:%lld", p->u.i);
90844 }else if( p->flags & MEM_Real ){
90845 printf(" r:%.17g", p->u.r);
90856 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
90861 if( p->pScopyFrom ){
90862 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
90881 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
90887 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
90897 ** high-performance timing routines.
90907 ** the number of non-transaction savepoints currently in the
90917 for(p=db->pSavepoint; p; p=p->pNext) n++;
90918 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
90932 int rc = p->pSharedBlock->xAddRow(pCtx, p->addedRows);
90937 for(i=0; i<pOp->p2; i++){
90940 rc = p->pSharedBlock->xPutLong(pCtx, p->addedRows, i, (sqlite3_int64)pMem[i].u.i);
90944 rc = p->pSharedBlock->xPutDouble(pCtx, p->addedRows, i, pMem[i].u.r);
90951 rc = p->pSharedBlock->xPutString(pCtx, p->addedRows, i, pMem[i].z, pMem[i].n+1);
90955 rc = p->pSharedBlock->xPutBlob(pCtx, p->addedRows, i, pMem[i].z, pMem[i].n);
90959 rc = p->pSharedBlock->xPutNull(pCtx, p->addedRows, i);
90963 rc = p->pSharedBlock->xPutOther(pCtx, p->addedRows, i);
90979 ** Return the register of pOp->p2 after first preparing it to be
90984 pOut->flags = MEM_Int;
90989 assert( pOp->p2>0 );
90990 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
90991 pOut = &p->aMem[pOp->p2];
90993 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
90996 pOut->flags = MEM_Int;
91002 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
91003 ** with pOp->p3. Return the hash.
91009 assert( pOp->p4type==P4_INT32 );
91010 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
91012 if( p->flags & (MEM_Int|MEM_IntReal) ){
91013 h += p->u.i;
91014 }else if( p->flags & MEM_Real ){
91016 }else if( p->flags & (MEM_Str|MEM_Blob) ){
91017 h += p->n;
91018 if( p->flags & MEM_Zero ) h += p->u.nZero;
91035 return azTypes[sqlite3_value_type(pMem)-1];
91045 Op *aOp = p->aOp; /* Copy of p->aOp */
91054 sqlite3 *db = p->db; /* The database */
91062 Mem *aMem = p->aMem; /* Copy of p->aMem */
91072 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
91075 if( db->xProgress ){
91076 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
91077 assert( 0 < db->nProgressOps );
91078 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
91083 if( p->rc==SQLITE_NOMEM ){
91088 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
91089 testcase( p->rc!=SQLITE_OK );
91090 p->rc = SQLITE_OK;
91091 assert( p->bIsReader || p->readOnly!=0 );
91092 p->iCurrentTime = 0;
91093 assert( p->explain==0 );
91094 p->pResultSet = 0;
91095 db->busyHandler.nBusy = 0;
91096 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91100 if( p->pc==0
91101 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
91106 if( p->db->flags & SQLITE_VdbeListing ){
91108 for(i=0; i<p->nOp; i++){
91112 if( p->db->flags & SQLITE_VdbeEQP ){
91113 for(i=0; i<p->nOp; i++){
91121 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
91125 for(pOp=&aOp[p->pc]; 1; pOp++){
91130 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
91136 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
91142 if( db->flags & SQLITE_VdbeTrace ){
91143 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
91144 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
91154 sqlite3_interrupt_count--;
91164 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
91166 assert( pOp->p1>0 );
91167 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
91168 assert( memIsValid(&aMem[pOp->p1]) );
91169 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
91170 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
91173 assert( pOp->p2>0 );
91174 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91175 assert( memIsValid(&aMem[pOp->p2]) );
91176 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
91177 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
91180 assert( pOp->p3>0 );
91181 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91182 assert( memIsValid(&aMem[pOp->p3]) );
91183 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
91184 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
91187 assert( pOp->p2>0 );
91188 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
91189 memAboutToChange(p, &aMem[pOp->p2]);
91192 assert( pOp->p3>0 );
91193 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91194 memAboutToChange(p, &aMem[pOp->p3]);
91202 switch( pOp->opcode ){
91209 ** the switch statement will break with convention and be flush-left. Another
91247 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
91257 if( pOp->p5 ){
91258 assert( pOp->p2 < (int)(pOp - aOp) );
91259 assert( pOp->p2 > 1 );
91260 pOp = &aOp[pOp->p2 - 2];
91267 pOp = &aOp[pOp->p2 - 1];
91278 ** faster according to "valgrind --tool=cachegrind" */
91280 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
91285 ** If the progress callback returns non-zero, exit the virtual machine with
91288 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
91289 assert( db->nProgressOps!=0 );
91290 nProgressLimit += db->nProgressOps;
91291 if( db->xProgress(db->pProgressArg) ){
91308 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91309 pIn1 = &aMem[pOp->p1];
91312 pIn1->flags = MEM_Int;
91313 pIn1->u.i = (int)(pOp-aOp);
91314 REGISTER_TRACE(pOp->p1, pIn1);
91325 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
91332 ** P2 is not used by the byte-code engine. However, if P2 is positive
91337 ** value is a byte-code indentation hint. See tag-20220407a in
91341 pIn1 = &aMem[pOp->p1];
91342 if( pIn1->flags & MEM_Int ){
91343 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
91344 pOp = &aOp[pIn1->u.i];
91345 }else if( ALWAYS(pOp->p3) ){
91363 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91364 assert( pOp->p2>=0 && pOp->p2<p->nOp );
91365 assert( pOp->p3>=0 && pOp->p3<p->nOp );
91366 pOut = &aMem[pOp->p1];
91368 pOut->u.i = pOp->p3 - 1;
91369 pOut->flags = MEM_Int;
91370 if( pOp->p2==0 ) break;
91375 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
91376 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
91377 pOp = &aOp[pOp->p2 - 1];
91391 pIn1 = &aMem[pOp->p1];
91392 assert( pIn1->flags==MEM_Int );
91393 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
91394 pCaller = &aOp[pIn1->u.i];
91395 assert( pCaller->opcode==OP_Yield );
91396 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
91397 pOp = &aOp[pCaller->p2 - 1];
91398 pIn1->flags = MEM_Undefined;
91417 pIn1 = &aMem[pOp->p1];
91419 pIn1->flags = MEM_Int;
91420 pcDest = (int)pIn1->u.i;
91421 pIn1->u.i = (int)(pOp - aOp);
91422 REGISTER_TRACE(pOp->p1, pIn1);
91432 ** value in register P3 is not NULL, then this routine is a no-op.
91436 pIn3 = &aMem[pOp->p3];
91438 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
91440 if( (pIn3->flags & MEM_Null)==0 ) break;
91480 if( p->pSharedBlock!=NULL ){
91481 p->pSharedBlock->xFinish(p->pSharedBlock->pContext, p->addedRows, p->totalRows);
91486 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
91488 if( p->pFrame && pOp->p1==SQLITE_OK ){
91489 /* Halt the sub-program. Return control to the parent frame. */
91490 pFrame = p->pFrame;
91491 p->pFrame = pFrame->pParent;
91492 p->nFrame--;
91493 sqlite3VdbeSetChanges(db, p->nChange);
91495 if( pOp->p2==OE_Ignore ){
91496 /* Instruction pcx is the OP_Program that invoked the sub-program
91498 ** instruction is set to OE_Ignore, then the sub-program is throwing
91501 pcx = p->aOp[pcx].p2-1;
91503 aOp = p->aOp;
91504 aMem = p->aMem;
91508 p->rc = pOp->p1;
91509 p->errorAction = (u8)pOp->p2;
91510 assert( pOp->p5<=4 );
91511 if( p->rc ){
91512 if( pOp->p5 ){
91515 testcase( pOp->p5==1 );
91516 testcase( pOp->p5==2 );
91517 testcase( pOp->p5==3 );
91518 testcase( pOp->p5==4 );
91519 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
91520 if( pOp->p4.z ){
91521 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
91524 sqlite3VdbeError(p, "%s", pOp->p4.z);
91526 pcx = (int)(pOp - aOp);
91527 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
91532 p->rc = SQLITE_BUSY;
91534 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
91535 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
91536 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
91544 ** The 32-bit integer value P1 is written into register P2.
91548 pOut->u.i = pOp->p1;
91555 ** P4 is a pointer to a 64-bit integer value.
91560 assert( pOp->p4.pI64!=0 );
91561 pOut->u.i = *pOp->p4.pI64;
91569 ** P4 is a pointer to a 64-bit floating point value.
91574 pOut->flags = MEM_Real;
91575 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
91576 pOut->u.r = *pOp->p4.pReal;
91584 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
91590 assert( pOp->p4.z!=0 );
91592 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
91596 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
91600 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
91602 pOut->szMalloc = 0;
91603 pOut->flags |= MEM_Static;
91604 if( pOp->p4type==P4_DYNAMIC ){
91605 sqlite3DbFree(db, pOp->p4.z);
91607 pOp->p4type = P4_DYNAMIC;
91608 pOp->p4.z = pOut->z;
91609 pOp->p1 = pOut->n;
91612 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
91615 pOp->opcode = OP_String;
91634 assert( pOp->p4.z!=0 );
91636 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
91637 pOut->z = pOp->p4.z;
91638 pOut->n = pOp->p1;
91639 pOut->enc = encoding;
91642 if( pOp->p3>0 ){
91643 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91644 pIn3 = &aMem[pOp->p3];
91645 assert( pIn3->flags & MEM_Int );
91646 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
91655 ** Mark the beginning of a subroutine that can be entered in-line
91659 ** If the subroutine is entered in-line, then the OP_Return will simply
91665 ** a no-op that simply falls through to the next instruction (assuming that
91667 ** entered in-line, then the OP_Return will cause in-line execution to
91682 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
91691 cnt = pOp->p3-pOp->p2;
91692 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
91693 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
91694 pOut->n = 0;
91696 pOut->uTemp = 0;
91702 pOut->flags = nullFlag;
91703 pOut->n = 0;
91704 cnt--;
91718 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
91719 pOut = &aMem[pOp->p1];
91720 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
91729 ** a zero-filled blob that is P1 bytes long in P2.
91732 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
91734 if( pOp->p4.z==0 ){
91735 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
91738 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
91740 pOut->enc = encoding;
91756 assert( pOp->p1>0 && pOp->p1<=p->nVar );
91757 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
91758 pVar = &p->aVar[pOp->p1 - 1];
91762 pOut = &aMem[pOp->p2];
91765 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
91766 pOut->flags |= MEM_Static|MEM_FromBind;
91774 ** Move the P3 values in register P1..P1+P3-1 over into
91775 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
91777 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
91785 n = pOp->p3;
91786 p1 = pOp->p1;
91787 p2 = pOp->p2;
91794 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
91795 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
91800 pIn1->pScopyFrom = 0;
91802 for(i=1; i<p->nMem; i++){
91813 }while( --n );
91833 n = pOp->p3;
91834 pIn1 = &aMem[pOp->p1];
91835 pOut = &aMem[pOp->p2];
91841 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
91842 pOut->flags &= ~MEM_Subtype;
91845 pOut->pScopyFrom = 0;
91847 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
91848 if( (n--)==0 ) break;
91869 pIn1 = &aMem[pOp->p1];
91870 pOut = &aMem[pOp->p2];
91874 pOut->pScopyFrom = pIn1;
91875 pOut->mScopyFlags = pIn1->flags;
91889 pIn1 = &aMem[pOp->p1];
91890 assert( (pIn1->flags & MEM_Int)!=0 );
91891 pOut = &aMem[pOp->p2];
91892 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
91900 ** constraint violations, this is a no-op.
91917 ** The registers P1 through P1+P2-1 contain a single row of
91920 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
91924 assert( p->nResColumn==pOp->p2 );
91925 assert( pOp->p1>0 || CORRUPT_DB );
91926 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
91929 if( p->pSharedBlock!=NULL ){
91930 void *pCtx = p->pSharedBlock->pContext;
91931 p->totalRows++;
91932 if( p->totalRows<=p->startPos || p->blockFull ){
91935 Mem *pMem = &aMem[pOp->p1];
91937 …if( rc==SQLITE_FULL && p->addedRows && (p->startPos + p->addedRows) <= p->pSharedBlock->requiredPo…
91938 p->startPos += p->addedRows;
91939 p->addedRows = 0;
91940 p->pSharedBlock->xReset(pCtx,p->startPos);
91941 p->blockFull = 0;
91946 p->addedRows++;
91948 p->blockFull = 1;
91954 if( p->blockFull && p->pSharedBlock->countAllRows==0 ){
91955 p->pSharedBlock->xFinish(pCtx, p->addedRows, p->totalRows);
91958 }else if( p->blockFull && p->pSharedBlock->countAllRows==1 ){
91966 p->cacheCtr = (p->cacheCtr + 2)|1;
91967 p->pResultSet = &aMem[pOp->p1];
91970 Mem *pMem = p->pResultSet;
91972 for(i=0; i<pOp->p2; i++){
91974 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
91984 if( db->mallocFailed ) goto no_mem;
91985 if( db->mTrace & SQLITE_TRACE_ROW ){
91986 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
91988 p->pc = (int)(pOp - aOp) + 1;
92011 pIn1 = &aMem[pOp->p1];
92012 pIn2 = &aMem[pOp->p2];
92013 pOut = &aMem[pOp->p3];
92016 flags1 = pIn1->flags;
92018 testcase( pIn2->flags & MEM_Null );
92019 if( (flags1 | pIn2->flags) & MEM_Null ){
92025 flags1 = pIn1->flags & ~MEM_Str;
92028 flags1 = pIn1->flags & ~MEM_Str;
92030 flags2 = pIn2->flags;
92033 flags2 = pIn2->flags & ~MEM_Str;
92036 flags2 = pIn2->flags & ~MEM_Str;
92038 nByte = pIn1->n + pIn2->n;
92039 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
92047 memcpy(pOut->z, pIn2->z, pIn2->n);
92048 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
92049 pIn2->flags = flags2;
92051 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
92052 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
92053 pIn1->flags = flags1;
92055 pOut->z[nByte]=0;
92056 pOut->z[nByte+1] = 0;
92057 pOut->flags |= MEM_Term;
92058 pOut->n = (int)nByte;
92059 pOut->enc = encoding;
92080 ** Synopsis: r[P3]=r[P2]-r[P1]
92114 pIn1 = &aMem[pOp->p1];
92115 type1 = pIn1->flags;
92116 pIn2 = &aMem[pOp->p2];
92117 type2 = pIn2->flags;
92118 pOut = &aMem[pOp->p3];
92121 iA = pIn1->u.i;
92122 iB = pIn2->u.i;
92123 switch( pOp->opcode ){
92129 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
92135 if( iA==-1 ) iA = 1;
92140 pOut->u.i = iB;
92151 switch( pOp->opcode ){
92153 case OP_Subtract: rB -= rA; break;
92165 if( iA==-1 ) iA = 1;
92171 pOut->u.i = rB;
92177 pOut->u.r = rB;
92192 ** be returned. This is used by the built-in min(), max() and nullif()
92201 ** publicly. Only built-in functions have access to this feature.
92204 assert( pOp->p4type==P4_COLLSEQ );
92205 if( pOp->p1 ){
92206 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
92214 ** Take the bit-wise AND of the values in register P1 and P2 and
92221 ** Take the bit-wise OR of the values in register P1 and P2 and
92250 pIn1 = &aMem[pOp->p1];
92251 pIn2 = &aMem[pOp->p2];
92252 pOut = &aMem[pOp->p3];
92253 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
92259 op = pOp->opcode;
92270 op = 2*OP_ShiftLeft + 1 - op;
92271 iB = iB>(-64) ? -iB : 64;
92275 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
92282 /* Sign-extend on a right shift of a negative number */
92283 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
92288 pOut->u.i = iA;
92302 pIn1 = &aMem[pOp->p1];
92305 pIn1->u.i += pOp->p2;
92317 pIn1 = &aMem[pOp->p1];
92318 if( (pIn1->flags & MEM_Int)==0 ){
92320 if( (pIn1->flags & MEM_Int)==0 ){
92322 if( pOp->p2==0 ){
92346 pIn1 = &aMem[pOp->p1];
92347 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
92348 testcase( pIn1->flags & MEM_Int );
92349 testcase( pIn1->flags & MEM_IntReal );
92351 REGISTER_TRACE(pOp->p1, pIn1);
92374 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
92375 testcase( pOp->p2==SQLITE_AFF_TEXT );
92376 testcase( pOp->p2==SQLITE_AFF_BLOB );
92377 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
92378 testcase( pOp->p2==SQLITE_AFF_INTEGER );
92379 testcase( pOp->p2==SQLITE_AFF_REAL );
92380 pIn1 = &aMem[pOp->p1];
92384 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
92387 REGISTER_TRACE(pOp->p1, pIn1);
92398 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
92442 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
92492 u16 flags1; /* Copy of initial value of pIn1->flags */
92493 u16 flags3; /* Copy of initial value of pIn3->flags */
92495 pIn1 = &aMem[pOp->p1];
92496 pIn3 = &aMem[pOp->p3];
92497 flags1 = pIn1->flags;
92498 flags3 = pIn3->flags;
92500 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
92502 if( pIn3->u.i > pIn1->u.i ){
92503 if( sqlite3aGTb[pOp->opcode] ){
92504 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92508 }else if( pIn3->u.i < pIn1->u.i ){
92509 if( sqlite3aLTb[pOp->opcode] ){
92510 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92513 iCompare = -1;
92515 if( sqlite3aEQb[pOp->opcode] ){
92516 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92521 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92526 if( pOp->p5 & SQLITE_NULLEQ ){
92532 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
92533 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
92539 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
92547 if( pOp->p5 & SQLITE_JUMPIFNULL ){
92554 /* Neither operand is NULL and we couldn't do the special high-speed
92555 ** integer comparison case. So do a general-case comparison. */
92556 affinity = pOp->p5 & SQLITE_AFF_MASK;
92561 testcase( flags3==pIn3->flags );
92562 flags3 = pIn3->flags;
92570 testcase( pIn1->flags & MEM_Int );
92571 testcase( pIn1->flags & MEM_Real );
92572 testcase( pIn1->flags & MEM_IntReal );
92574 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
92575 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
92579 testcase( pIn3->flags & MEM_Int );
92580 testcase( pIn3->flags & MEM_Real );
92581 testcase( pIn3->flags & MEM_IntReal );
92583 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
92584 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
92587 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
92588 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
92600 res2 = sqlite3aLTb[pOp->opcode];
92602 res2 = sqlite3aEQb[pOp->opcode];
92604 res2 = sqlite3aGTb[pOp->opcode];
92609 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
92610 pIn3->flags = flags3;
92611 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
92612 pIn1->flags = flags1;
92614 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
92636 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
92639 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
92663 assert( pOp->p4type==P4_INTARRAY );
92664 assert( pOp->p4.ai );
92671 ** Synopsis: r[P1@P3] <-> r[P2@P3]
92673 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
92674 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
92703 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
92707 assert( pOp[-1].opcode==OP_Permutation );
92708 assert( pOp[-1].p4type==P4_INTARRAY );
92709 aPermute = pOp[-1].p4.ai + 1;
92712 n = pOp->p3;
92713 pKeyInfo = pOp->p4.pKeyInfo;
92716 p1 = pOp->p1;
92717 p2 = pOp->p2;
92722 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
92723 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
92725 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
92726 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
92735 assert( i<pKeyInfo->nKeyField );
92736 pColl = pKeyInfo->aColl[i];
92737 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
92740 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
92743 iCompare = -iCompare;
92745 if( bRev ) iCompare = -iCompare;
92763 if( pOp->p5&0x80 ){
92764 if( p->pSharedBlock!=NULL ){
92765 if( p->totalRows < p->startPos || p->blockFull ){
92766 p->totalRows++;
92767 pOp = &aOp[pOp->p2 - 1];
92773 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
92775 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
92777 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
92779 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
92809 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
92810 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
92811 if( pOp->opcode==OP_And ){
92818 pOut = &aMem[pOp->p3];
92822 pOut->u.i = v1;
92849 assert( pOp->p4type==P4_INT32 );
92850 assert( pOp->p4.i==0 || pOp->p4.i==1 );
92851 assert( pOp->p3==0 || pOp->p3==1 );
92852 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
92853 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
92865 pIn1 = &aMem[pOp->p1];
92866 pOut = &aMem[pOp->p2];
92867 if( (pIn1->flags & MEM_Null)==0 ){
92879 ** ones-complement of the P1 value into register P2. If P1 holds
92883 pIn1 = &aMem[pOp->p1];
92884 pOut = &aMem[pOp->p2];
92886 if( (pIn1->flags & MEM_Null)==0 ){
92887 pOut->flags = MEM_Int;
92888 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
92896 ** encountered on each invocation of the byte-code program. Jump to P2
92899 ** Top-level programs determine first invocation by comparing the P1
92907 ** because the self-altering code trick does not work for recursive
92912 assert( p->aOp[0].opcode==OP_Init );
92913 if( p->pFrame ){
92914 iAddr = (int)(pOp - p->aOp);
92915 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
92919 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
92921 if( p->aOp[0].p1==pOp->p1 ){
92927 pOp->p1 = p->aOp[0].p1;
92934 ** is considered true if it is numeric and non-zero. If the value
92935 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
92939 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
92949 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
92953 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
92965 pIn1 = &aMem[pOp->p1];
92966 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
92967 if( (pIn1->flags & MEM_Null)!=0 ){
92986 ** If P1 is -1, then P3 is a register number and the datatype is taken
93003 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
93004 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
93005 if( pOp->p1>=0 ){
93006 pC = p->apCsr[pOp->p1];
93008 assert( pOp->p3>=0 );
93009 if( pOp->p3<pC->nHdrParsed ){
93010 serialType = pC->aType[pOp->p3];
93037 typeMask = 1 << (pOp->p4.i - 1);
93045 assert( memIsValid(&aMem[pOp->p3]) );
93046 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
93053 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
93054 if( typeMask & pOp->p5 ){
93068 if( (aMem[pOp->p1].flags & MEM_Null)!=0
93069 || (aMem[pOp->p3].flags & MEM_Null)!=0
93071 sqlite3VdbeMemSetNull(aMem + pOp->p2);
93073 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
93084 pIn1 = &aMem[pOp->p1];
93085 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
93086 if( (pIn1->flags & MEM_Null)==0 ){
93100 ** If P1 is not an open cursor, then this opcode is a no-op.
93104 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93105 pC = p->apCsr[pOp->p1];
93106 if( ALWAYS(pC) && pC->nullRow ){
93107 sqlite3VdbeMemSetNull(aMem + pOp->p3);
93127 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
93131 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93132 pC = p->apCsr[pOp->p1];
93133 pOut = &p->aMem[pOp->p3];
93134 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
93137 if( pC->deferredMoveto ){
93141 if( sqlite3BtreeEof(pC->uc.pCursor) ){
93144 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
93156 ** information about the format of the data.) Extract the P2-th column
93176 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
93177 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
93185 u64 offset64; /* 64-bit offset */
93189 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93190 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93191 pC = p->apCsr[pOp->p1];
93192 p2 = (u32)pOp->p2;
93196 assert( p2<(u32)pC->nField
93197 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
93198 aOffset = pC->aOffset;
93199 assert( aOffset==pC->aType+pC->nField );
93200 assert( pC->eCurType!=CURTYPE_VTAB );
93201 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
93202 assert( pC->eCurType!=CURTYPE_SORTER );
93204 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
93205 if( pC->nullRow ){
93206 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
93207 /* For the special case of as pseudo-cursor, the seekResult field
93209 pReg = &aMem[pC->seekResult];
93210 assert( pReg->flags & MEM_Blob );
93212 pC->payloadSize = pC->szRow = pReg->n;
93213 pC->aRow = (u8*)pReg->z;
93215 pDest = &aMem[pOp->p3];
93221 pCrsr = pC->uc.pCursor;
93222 if( pC->deferredMoveto ){
93224 assert( !pC->isEphemeral );
93225 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
93226 pC = pC->pAltCursor;
93227 p2 = iMap - 1;
93237 assert( pC->eCurType==CURTYPE_BTREE );
93240 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
93241 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
93242 assert( pC->szRow<=pC->payloadSize );
93243 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
93245 pC->cacheStatus = p->cacheCtr;
93246 if( (aOffset[0] = pC->aRow[0])<0x80 ){
93247 pC->iHdrOffset = 1;
93249 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
93251 pC->nHdrParsed = 0;
93253 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
93254 /* pC->aRow does not have to hold the entire row, but it does at least
93255 ** need to cover the header of the record. If pC->aRow does not contain
93258 pC->aRow = 0;
93259 pC->szRow = 0;
93267 ** 3-byte type for each of the maximum of 32768 columns plus three
93270 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
93275 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
93287 zData = pC->aRow;
93288 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
93292 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
93299 ** parsed and valid information is in aOffset[] and pC->aType[].
93301 if( pC->nHdrParsed<=p2 ){
93303 ** to extract additional fields up through the p2+1-th field
93305 if( pC->iHdrOffset<aOffset[0] ){
93307 if( pC->aRow==0 ){
93309 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
93313 zData = pC->aRow;
93316 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
93318 i = pC->nHdrParsed;
93320 zHdr = zData + pC->iHdrOffset;
93324 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
93329 pC->aType[i] = t;
93340 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
93341 || (offset64 > pC->payloadSize)
93347 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93352 pC->nHdrParsed = i;
93353 pC->iHdrOffset = (u32)(zHdr - zData);
93354 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
93363 if( pC->nHdrParsed<=p2 ){
93364 pDest = &aMem[pOp->p3];
93366 if( pOp->p4type==P4_MEM ){
93367 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
93374 t = pC->aType[p2];
93377 /* Extract the content for the p2+1-th column. Control can only
93378 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
93381 assert( p2<pC->nHdrParsed );
93383 pDest = &aMem[pOp->p3];
93389 assert( t==pC->aType[p2] );
93390 if( pC->szRow>=aOffset[p2+1] ){
93392 ** page - where the content is not on an overflow page */
93393 zData = pC->aRow + aOffset[p2];
93398 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
93402 pDest->n = len = (t-12)/2;
93403 pDest->enc = encoding;
93404 if( pDest->szMalloc < len+2 ){
93405 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
93406 pDest->flags = MEM_Null;
93409 pDest->z = pDest->zMalloc;
93411 memcpy(pDest->z, zData, len);
93412 pDest->z[len] = 0;
93413 pDest->z[len+1] = 0;
93414 pDest->flags = aFlag[t&1];
93417 pDest->enc = encoding;
93419 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
93420 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
93438 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
93439 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
93441 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
93442 pDest->flags &= ~MEM_Ephem;
93448 REGISTER_TRACE(pOp->p3, pDest);
93453 pOp = &aOp[aOp[0].p3-1];
93473 ** is zero. When P3 is non-zero, no type checking occurs for
93480 ** <li> P2 should be the number of non-virtual columns in the
93492 assert( pOp->p4type==P4_TABLE );
93493 pTab = pOp->p4.pTab;
93494 assert( pTab->tabFlags & TF_Strict );
93495 assert( pTab->nNVCol==pOp->p2 );
93496 aCol = pTab->aCol;
93497 pIn1 = &aMem[pOp->p1];
93498 for(i=0; i<pTab->nCol; i++){
93501 if( pOp->p3 ){ pIn1++; continue; }
93503 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
93505 if( (pIn1->flags & MEM_Null)==0 ){
93508 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
93513 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
93517 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
93521 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
93522 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
93523 if( pIn1->flags & MEM_Int ){
93526 ** so that we keep the high-resolution integer value but know that
93528 testcase( pIn1->u.i==140737488355328LL );
93529 testcase( pIn1->u.i==140737488355327LL );
93530 testcase( pIn1->u.i==-140737488355328LL );
93531 testcase( pIn1->u.i==-140737488355329LL );
93532 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
93533 pIn1->flags |= MEM_IntReal;
93534 pIn1->flags &= ~MEM_Int;
93536 pIn1->u.r = (double)pIn1->u.i;
93537 pIn1->flags |= MEM_Real;
93538 pIn1->flags &= ~MEM_Int;
93540 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
93551 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
93554 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
93559 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
93560 pTab->zName, aCol[i].zCnName);
93570 ** P4 is a string that is P2 characters long. The N-th character of the
93571 ** string indicates the column affinity that should be used for the N-th
93577 zAffinity = pOp->p4.z;
93579 assert( pOp->p2>0 );
93580 assert( zAffinity[pOp->p2]==0 );
93581 pIn1 = &aMem[pOp->p1];
93582 while( 1 /*exit-by-break*/ ){
93583 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
93586 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
93589 ** so that we keep the high-resolution integer value but know that
93591 testcase( pIn1->u.i==140737488355328LL );
93592 testcase( pIn1->u.i==140737488355327LL );
93593 testcase( pIn1->u.i==-140737488355328LL );
93594 testcase( pIn1->u.i==-140737488355329LL );
93595 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
93596 pIn1->flags |= MEM_IntReal;
93597 pIn1->flags &= ~MEM_Int;
93599 pIn1->u.r = (double)pIn1->u.i;
93600 pIn1->flags |= MEM_Real;
93601 pIn1->flags &= ~MEM_Int;
93604 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
93619 ** P4 may be a string that is P2 characters long. The N-th character of the
93620 ** string indicates the column affinity that should be used for the N-th
93629 ** compile-time option is enabled:
93632 ** of the right-most table that can be null-trimmed.
93636 ** accept no-change records with serial_type 10. This value is
93658 ** ------------------------------------------------------------------------
93659 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
93660 ** ------------------------------------------------------------------------
93667 ** hdr-size field is also a varint which is the offset from the beginning
93673 nField = pOp->p1;
93674 zAffinity = pOp->p4.z;
93675 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
93677 nField = pOp->p2;
93678 pLast = &pData0[nField-1];
93681 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
93682 pOut = &aMem[pOp->p3];
93692 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
93693 pRec->flags |= MEM_IntReal;
93694 pRec->flags &= ~(MEM_Int);
93696 REGISTER_TRACE((int)(pRec-aMem), pRec);
93706 ** have a non-NULL default value. Also, the record must be left with
93708 ** index of the right-most column with a non-NULL default value */
93709 if( pOp->p5 ){
93710 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
93711 pLast--;
93712 nField--;
93719 ** the Mem.uTemp field of each term should hold the serial-type that will
93723 ** --------------- ---------------
93725 ** 1 1-byte signed integer
93726 ** 2 2-byte signed integer
93727 ** 3 3-byte signed integer
93728 ** 4 4-byte signed integer
93729 ** 5 6-byte signed integer
93730 ** 6 8-byte signed integer
93746 if( pRec->flags & MEM_Null ){
93747 if( pRec->flags & MEM_Zero ){
93751 ** Give such values a special internal-use-only serial-type of 10
93755 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
93757 pRec->uTemp = 10;
93759 pRec->uTemp = 0;
93762 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
93764 i64 i = pRec->u.i;
93766 testcase( pRec->flags & MEM_Int );
93767 testcase( pRec->flags & MEM_IntReal );
93780 if( (i&1)==i && p->minWriteFileFormat>=4 ){
93781 pRec->uTemp = 8+(u32)uu;
93784 pRec->uTemp = 1;
93788 pRec->uTemp = 2;
93791 pRec->uTemp = 3;
93794 pRec->uTemp = 4;
93797 pRec->uTemp = 5;
93800 if( pRec->flags & MEM_IntReal ){
93802 ** as an integer, then we might as well make it an 8-byte floating
93804 pRec->u.r = (double)pRec->u.i;
93805 pRec->flags &= ~MEM_IntReal;
93806 pRec->flags |= MEM_Real;
93807 pRec->uTemp = 7;
93809 pRec->uTemp = 6;
93812 }else if( pRec->flags & MEM_Real ){
93815 pRec->uTemp = 7;
93817 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
93818 assert( pRec->n>=0 );
93819 len = (u32)pRec->n;
93820 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
93821 if( pRec->flags & MEM_Zero ){
93822 serial_type += pRec->u.nZero*2;
93825 len += pRec->u.nZero;
93827 nZero += pRec->u.nZero;
93832 pRec->uTemp = serial_type;
93835 pRec--;
93838 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
93856 ** the new record. The output register (pOp->p3) is not allowed to
93860 if( nByte+nZero<=pOut->szMalloc ){
93863 pOut->z = pOut->zMalloc;
93867 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
93874 pOut->n = (int)nByte;
93875 pOut->flags = MEM_Blob;
93877 pOut->u.nZero = nZero;
93878 pOut->flags |= MEM_Zero;
93881 zHdr = (u8 *)pOut->z;
93892 while( 1 /*exit-by-break*/ ){
93893 serial_type = pRec->uTemp;
93894 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
93896 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
93906 assert( sizeof(v)==sizeof(pRec->u.r) );
93907 memcpy(&v, &pRec->u.r, sizeof(v));
93910 v = pRec->u.i;
93914 while( 1 /*exit-by-break*/ ){
93915 zPayload[--i] = (u8)(v&0xFF);
93923 if( serial_type>=14 && pRec->n>0 ){
93924 assert( pRec->z!=0 );
93925 memcpy(zPayload, pRec->z, pRec->n);
93926 zPayload += pRec->n;
93930 if( pRec->n ){
93931 assert( pRec->z!=0 );
93932 memcpy(zPayload, pRec->z, pRec->n);
93933 zPayload += pRec->n;
93939 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
93940 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
93942 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93943 REGISTER_TRACE(pOp->p3, pOut);
93954 ** every btree page of the table. But if P3 is non-zero, an estimate
93961 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
93962 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
93964 if( pOp->p3 ){
93972 pOut->u.i = nEntry;
93993 p1 = pOp->p1;
93994 zName = pOp->p4.z;
93999 assert( db->pSavepoint==0 || db->autoCommit==0 );
94001 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
94003 assert( p->bIsReader );
94006 if( db->nVdbeWrite>0 ){
94010 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
94019 ** that the db->aVTrans[] array is empty. */
94020 assert( db->autoCommit==0 || db->nVTrans==0 );
94022 db->nStatement+db->nSavepoint);
94029 pNew->zName = (char *)&pNew[1];
94030 memcpy(pNew->zName, zName, nName+1);
94034 if( db->autoCommit ){
94035 db->autoCommit = 0;
94036 db->isTransactionSavepoint = 1;
94038 db->nSavepoint++;
94042 pNew->pNext = db->pSavepoint;
94043 db->pSavepoint = pNew;
94044 pNew->nDeferredCons = db->nDeferredCons;
94045 pNew->nDeferredImmCons = db->nDeferredImmCons;
94055 pSavepoint = db->pSavepoint;
94056 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
94057 pSavepoint = pSavepoint->pNext
94064 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
94068 sqlite3VdbeError(p, "cannot release savepoint - "
94077 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
94082 db->autoCommit = 1;
94084 p->pc = (int)(pOp - aOp);
94085 db->autoCommit = 0;
94086 p->rc = rc = SQLITE_BUSY;
94089 rc = p->rc;
94091 db->autoCommit = 0;
94093 db->isTransactionSavepoint = 0;
94097 iSavepoint = db->nSavepoint - iSavepoint - 1;
94099 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
94100 for(ii=0; ii<db->nDb; ii++){
94101 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
94110 for(ii=0; ii<db->nDb; ii++){
94111 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
94119 db->mDbFlags |= DBFLAG_SchemaChange;
94126 while( db->pSavepoint!=pSavepoint ){
94127 pTmp = db->pSavepoint;
94128 db->pSavepoint = pTmp->pNext;
94130 db->nSavepoint--;
94138 assert( pSavepoint==db->pSavepoint );
94139 db->pSavepoint = pSavepoint->pNext;
94142 db->nSavepoint--;
94146 db->nDeferredCons = pSavepoint->nDeferredCons;
94147 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
94157 if( p->eVdbeState==VDBE_HALT_STATE ){
94166 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
94177 desiredAutoCommit = pOp->p1;
94178 iRollback = pOp->p2;
94181 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
94182 assert( p->bIsReader );
94184 if( desiredAutoCommit!=db->autoCommit ){
94188 db->autoCommit = 1;
94189 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
94193 sqlite3VdbeError(p, "cannot commit transaction - "
94200 db->autoCommit = (u8)desiredAutoCommit;
94203 p->pc = (int)(pOp - aOp);
94204 db->autoCommit = (u8)(1-desiredAutoCommit);
94205 p->rc = rc = SQLITE_BUSY;
94209 if( p->rc==SQLITE_OK ){
94218 (iRollback)?"cannot rollback - no transaction is active":
94219 "cannot commit - no transaction is active"));
94231 ** If P2 is non-zero, then a write-transaction is started, or if a
94232 ** read-transaction is already active, it is upgraded to a write-transaction.
94233 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
94241 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
94267 assert( p->bIsReader );
94268 assert( p->readOnly==0 || pOp->p2==0 );
94269 assert( pOp->p2>=0 && pOp->p2<=2 );
94270 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94271 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94273 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
94274 if( db->flags & SQLITE_QueryOnly ){
94284 pDb = &db->aDb[pOp->p1];
94285 pBt = pDb->pBt;
94288 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
94293 p->pc = (int)(pOp - aOp);
94294 p->rc = rc;
94300 if( p->usesStmtJournal
94301 && pOp->p2
94302 && (db->autoCommit==0 || db->nVdbeRead>1)
94305 if( p->iStatement==0 ){
94306 assert( db->nStatement>=0 && db->nSavepoint>=0 );
94307 db->nStatement++;
94308 p->iStatement = db->nSavepoint + db->nStatement;
94311 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
94313 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
94319 p->nStmtDefCons = db->nDeferredCons;
94320 p->nStmtDefImmCons = db->nDeferredImmCons;
94323 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
94325 && pOp->p5
94326 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
94329 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
94333 sqlite3DbFree(db, p->zErrMsg);
94334 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
94335 /* If the schema-cookie from the database file matches the cookie
94336 ** stored with the in-memory representation of the schema, do
94339 ** If virtual-tables are in use, this is not just an optimization.
94340 ** Often, v-tables store their data in other SQLite tables, which
94341 ** are queried from within xNext() and other v-table methods using
94342 ** prepared queries. If such a query is out-of-date, we do not want to
94344 ** v-table would have to be ready for the sqlite3_vtab structure itself
94346 ** a v-table method.
94348 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
94349 sqlite3ResetOneSchema(db, pOp->p1);
94351 p->expired = 1;
94357 p->changeCntOn = 0;
94371 ** There must be a read-lock on the database (either a transaction
94380 assert( p->bIsReader );
94381 iDb = pOp->p1;
94382 iCookie = pOp->p3;
94383 assert( pOp->p3<SQLITE_N_BTREE_META );
94384 assert( iDb>=0 && iDb<db->nDb );
94385 assert( db->aDb[iDb].pBt!=0 );
94386 assert( DbMaskTest(p->btreeMask, iDb) );
94388 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
94390 pOut->u.i = iMeta;
94405 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
94413 assert( pOp->p2<SQLITE_N_BTREE_META );
94414 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94415 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94416 assert( p->readOnly==0 );
94417 pDb = &db->aDb[pOp->p1];
94418 assert( pDb->pBt!=0 );
94419 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
94421 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
94422 if( pOp->p2==BTREE_SCHEMA_VERSION ){
94424 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
94425 db->mDbFlags |= DBFLAG_SchemaChange;
94426 sqlite3FkClearTriggerCache(db, pOp->p1);
94427 }else if( pOp->p2==BTREE_FILE_FORMAT ){
94429 pDb->pSchema->file_format = pOp->p3;
94431 if( pOp->p1==1 ){
94435 p->expired = 0;
94444 ** Open a read-only cursor for the database table whose root page is
94461 ** object, then table being opened must be an [index b-tree] where the
94463 ** sequence of that index b-tree. Otherwise, if P4 is an integer
94464 ** value, then the table being opened must be a [table b-tree] with a
94474 ** b-tree and if it is this opcode becomes a no-op. In other words,
94496 ** OPFLAG_P2ISREG bit is set in P5 - see below).
94500 ** object, then table being opened must be an [index b-tree] where the
94502 ** sequence of that index b-tree. Otherwise, if P4 is an integer
94503 ** value, then the table being opened must be a [table b-tree] with a
94535 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
94536 assert( pOp->p4type==P4_KEYINFO );
94537 pCur = p->apCsr[pOp->p1];
94538 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
94539 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
94540 assert( pCur->eCurType==CURTYPE_BTREE );
94541 sqlite3BtreeClearCursor(pCur->uc.pCursor);
94549 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
94550 assert( p->bIsReader );
94551 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
94552 || p->readOnly==0 );
94554 if( p->expired==1 ){
94561 p2 = (u32)pOp->p2;
94562 iDb = pOp->p3;
94563 assert( iDb>=0 && iDb<db->nDb );
94564 assert( DbMaskTest(p->btreeMask, iDb) );
94565 pDb = &db->aDb[iDb];
94566 pX = pDb->pBt;
94568 if( pOp->opcode==OP_OpenWrite ){
94570 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
94572 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
94573 p->minWriteFileFormat = pDb->pSchema->file_format;
94578 if( pOp->p5 & OPFLAG_P2ISREG ){
94580 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
94581 assert( pOp->opcode==OP_OpenWrite );
94584 assert( (pIn2->flags & MEM_Int)!=0 );
94586 p2 = (int)pIn2->u.i;
94593 if( pOp->p4type==P4_KEYINFO ){
94594 pKeyInfo = pOp->p4.pKeyInfo;
94595 assert( pKeyInfo->enc==ENC(db) );
94596 assert( pKeyInfo->db==db );
94597 nField = pKeyInfo->nAllField;
94598 }else if( pOp->p4type==P4_INT32 ){
94599 nField = pOp->p4.i;
94601 assert( pOp->p1>=0 );
94604 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
94606 pCur->iDb = iDb;
94607 pCur->nullRow = 1;
94608 pCur->isOrdered = 1;
94609 pCur->pgnoRoot = p2;
94611 pCur->wrFlag = wrFlag;
94613 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
94614 pCur->pKeyInfo = pKeyInfo;
94616 ** SQLite used to check if the root-page flags were sane at this point
94619 pCur->isTable = pOp->p4type!=P4_KEYINFO;
94624 testcase( pOp->p5 & OPFLAG_BULKCSR );
94625 testcase( pOp->p2 & OPFLAG_SEEKEQ );
94626 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
94627 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
94638 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
94644 pOrig = p->apCsr[pOp->p2];
94646 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
94648 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
94650 pCx->nullRow = 1;
94651 pCx->isEphemeral = 1;
94652 pCx->pKeyInfo = pOrig->pKeyInfo;
94653 pCx->isTable = pOrig->isTable;
94654 pCx->pgnoRoot = pOrig->pgnoRoot;
94655 pCx->isOrdered = pOrig->isOrdered;
94656 pCx->ub.pBtx = pOrig->ub.pBtx;
94657 pCx->noReuse = 1;
94658 pOrig->noReuse = 1;
94659 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
94660 pCx->pKeyInfo, pCx->uc.pCursor);
94674 ** the main database is read-only. The ephemeral
94691 ** can be used as zero-length data for OP_Insert. This is an optimization
94713 assert( pOp->p1>=0 );
94714 assert( pOp->p2>=0 );
94715 if( pOp->p3>0 ){
94718 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
94719 assert( pOp->opcode==OP_OpenEphemeral );
94720 assert( aMem[pOp->p3].flags & MEM_Null );
94721 aMem[pOp->p3].n = 0;
94722 aMem[pOp->p3].z = "";
94724 pCx = p->apCsr[pOp->p1];
94725 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
94729 assert( pCx->isEphemeral );
94730 pCx->seqCount = 0;
94731 pCx->cacheStatus = CACHE_STALE;
94732 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
94734 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
94736 pCx->isEphemeral = 1;
94737 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
94738 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
94741 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
94746 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
94748 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
94749 assert( pOp->p4type==P4_KEYINFO );
94750 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
94751 BTREE_BLOBKEY | pOp->p5);
94753 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
94754 assert( pKeyInfo->db==db );
94755 assert( pKeyInfo->enc==ENC(db) );
94756 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
94757 pKeyInfo, pCx->uc.pCursor);
94759 pCx->isTable = 0;
94761 pCx->pgnoRoot = SCHEMA_ROOT;
94762 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
94763 0, pCx->uc.pCursor);
94764 pCx->isTable = 1;
94767 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
94769 sqlite3BtreeClose(pCx->ub.pBtx);
94774 pCx->nullRow = 1;
94782 ** tables using an external merge-sort algorithm.
94784 ** If argument P3 is non-zero, then it indicates that the sorter may
94791 assert( pOp->p1>=0 );
94792 assert( pOp->p2>=0 );
94793 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
94795 pCx->pKeyInfo = pOp->p4.pKeyInfo;
94796 assert( pCx->pKeyInfo->db==db );
94797 assert( pCx->pKeyInfo->enc==ENC(db) );
94798 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
94812 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94813 pC = p->apCsr[pOp->p1];
94815 if( (pC->seqCount++)==0 ){
94829 ** A pseudo-table created by this opcode is used to hold a single
94832 ** is the only cursor opcode that works with a pseudo-table.
94835 ** the pseudo-table.
94840 assert( pOp->p1>=0 );
94841 assert( pOp->p3>=0 );
94842 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
94844 pCx->nullRow = 1;
94845 pCx->seekResult = pOp->p2;
94846 pCx->isTable = 1;
94847 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
94849 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
94851 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
94852 assert( pOp->p5==0 );
94859 ** currently open, this instruction is a no-op.
94862 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94863 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
94864 p->apCsr[pOp->p1] = 0;
94873 ** table or index for cursor P1 are used. P4 is a 64-bit integer
94876 ** by the cursor. The high-order bit is set if any column after
94881 pC = p->apCsr[pOp->p1];
94882 assert( pC->eCurType==CURTYPE_BTREE );
94883 pC->maskUsed = *(u64*)pOp->p4.pI64;
94891 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
94918 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
94936 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
94954 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
94990 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94991 assert( pOp->p2!=0 );
94992 pC = p->apCsr[pOp->p1];
94994 assert( pC->eCurType==CURTYPE_BTREE );
94998 assert( pC->isOrdered );
94999 assert( pC->uc.pCursor!=0 );
95000 oc = pOp->opcode;
95002 pC->nullRow = 0;
95004 pC->seekOp = pOp->opcode;
95007 pC->deferredMoveto = 0;
95008 pC->cacheStatus = CACHE_STALE;
95009 if( pC->isTable ){
95012 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
95018 pIn3 = &aMem[pOp->p3];
95019 flags3 = pIn3->flags;
95024 newType = pIn3->flags; /* Record the type after applying numeric affinity */
95025 pIn3->flags = flags3; /* But convert the type back to its original */
95036 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
95041 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
95047 ** (x > 4.9) -> (x >= 5)
95048 ** (x <= 4.9) -> (x < 5)
95051 assert( OP_SeekGE==(OP_SeekGT-1) );
95052 assert( OP_SeekLT==(OP_SeekLE-1) );
95054 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
95066 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
95067 pC->movetoTarget = iKey; /* Used by OP_Delete */
95077 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
95079 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
95081 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
95082 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
95089 nField = pOp->p4.i;
95090 assert( pOp->p4type==P4_INT32 );
95092 r.pKeyInfo = pC->pKeyInfo;
95097 ** r.default_rc = -1;
95102 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
95103 assert( oc!=OP_SeekGT || r.default_rc==-1 );
95104 assert( oc!=OP_SeekLE || r.default_rc==-1 );
95108 r.aMem = &aMem[pOp->p3];
95114 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
95119 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
95134 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95150 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
95163 res = sqlite3BtreeEof(pC->uc.pCursor);
95167 assert( pOp->p2>0 );
95180 ** Synopsis: Scan-ahead up to P1 rows
95192 ** This opcode helps to optimize IN operators on a multi-column index
95195 ** of the b-tree instead. A correct answer is obtained if this opcode
95196 ** is omitted or is a no-op.
95203 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
95215 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
95255 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
95258 assert( pOp->p2>=(int)(pOp-aOp)+2 );
95260 if( pOp->p5==0 ){
95262 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
95263 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
95264 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
95265 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
95266 || aOp[pOp->p2-1].opcode==OP_IdxGE );
95267 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
95270 assert( pOp->p2==(int)(pOp-aOp)+2 );
95271 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
95275 assert( pOp->p1>0 );
95276 pC = p->apCsr[pOp[1].p1];
95278 assert( pC->eCurType==CURTYPE_BTREE );
95279 assert( !pC->isTable );
95280 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
95282 if( db->flags&SQLITE_VdbeTrace ){
95283 printf("... cursor not valid - fall through\n");
95288 nStep = pOp->p1;
95290 r.pKeyInfo = pC->pKeyInfo;
95307 if( res>0 && pOp->p5==0 ){
95311 if( db->flags&SQLITE_VdbeTrace ){
95312 printf("... %d steps and then skip\n", pOp->p1 - nStep);
95322 if( db->flags&SQLITE_VdbeTrace ){
95323 printf("... %d steps and then success\n", pOp->p1 - nStep);
95332 if( db->flags&SQLITE_VdbeTrace ){
95333 printf("... fall through after %d steps\n", pOp->p1);
95339 nStep--;
95340 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
95365 ** early, thus saving work. This is part of the IN-early-out optimization.
95367 ** P1 must be a valid b-tree cursor.
95371 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95372 pC = p->apCsr[pOp->p1];
95374 assert( pOp->p3>=pOp->p2 );
95375 if( pC->seekHit<pOp->p2 ){
95377 if( db->flags&SQLITE_VdbeTrace ){
95378 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
95381 pC->seekHit = pOp->p2;
95382 }else if( pC->seekHit>pOp->p3 ){
95384 if( db->flags&SQLITE_VdbeTrace ){
95385 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
95388 pC->seekHit = pOp->p3;
95402 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95403 pCur = p->apCsr[pOp->p1];
95404 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
95405 if( pCur==0 || pCur->nullRow ){
95466 ** This opcode is used in IN clause processing for a multi-column key.
95468 ** left-most element, and if there are no matches on the most recent
95487 ** record are not-NULL then a check is done to determine if any row in the
95503 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95504 pC = p->apCsr[pOp->p1];
95507 if( db->flags&SQLITE_VdbeTrace ){
95508 printf("seekHit is %d\n", pC->seekHit);
95511 if( pC->seekHit>=pOp->p4.i ) break;
95525 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
95528 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95529 assert( pOp->p4type==P4_INT32 );
95530 pC = p->apCsr[pOp->p1];
95533 pC->seekOp = pOp->opcode;
95535 r.aMem = &aMem[pOp->p3];
95536 assert( pC->eCurType==CURTYPE_BTREE );
95537 assert( pC->uc.pCursor!=0 );
95538 assert( pC->isTable==0 );
95539 r.nField = (u16)pOp->p4.i;
95542 r.pKeyInfo = pC->pKeyInfo;
95548 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
95551 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
95554 assert( r.aMem->flags & MEM_Blob );
95555 assert( pOp->opcode!=OP_NoConflict );
95559 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
95561 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
95562 pIdxKey->default_rc = 0;
95563 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
95569 alreadyExists = (pC->seekResult==0);
95570 pC->nullRow = 1-alreadyExists;
95571 pC->deferredMoveto = 0;
95572 pC->cacheStatus = CACHE_STALE;
95573 if( pOp->opcode==OP_Found ){
95581 if( pOp->opcode==OP_NoConflict ){
95593 if( pOp->opcode==OP_IfNoHope ){
95594 pC->seekHit = pOp->p4.i;
95616 ** (with arbitrary multi-value keys).
95635 ** P3 register to contain a non-integer value, in which case the jump is
95639 ** (with arbitrary multi-value keys).
95653 pIn3 = &aMem[pOp->p3];
95654 testcase( pIn3->flags & MEM_Int );
95655 testcase( pIn3->flags & MEM_IntReal );
95656 testcase( pIn3->flags & MEM_Real );
95657 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
95658 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
95659 /* If pIn3->u.i does not contain an integer, compute iKey as the
95673 pIn3 = &aMem[pOp->p3];
95674 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
95675 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95676 iKey = pIn3->u.i;
95678 pC = p->apCsr[pOp->p1];
95681 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
95683 assert( pC->isTable );
95684 assert( pC->eCurType==CURTYPE_BTREE );
95685 pCrsr = pC->uc.pCursor;
95690 pC->movetoTarget = iKey; /* Used by OP_Delete */
95691 pC->nullRow = 0;
95692 pC->cacheStatus = CACHE_STALE;
95693 pC->deferredMoveto = 0;
95695 pC->seekResult = res;
95698 if( pOp->p2==0 ){
95717 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95718 assert( p->apCsr[pOp->p1]!=0 );
95719 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
95721 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
95754 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95755 pC = p->apCsr[pOp->p1];
95757 assert( pC->isTable );
95758 assert( pC->eCurType==CURTYPE_BTREE );
95759 assert( pC->uc.pCursor!=0 );
95762 ** thing) is obtained in a two-step algorithm.
95774 assert( pC->isTable );
95786 if( !pC->useRandomRowid ){
95787 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
95792 v = 1; /* IMP: R-61914-48074 */
95794 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
95795 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
95797 pC->useRandomRowid = 1;
95799 v++; /* IMP: R-29538-34987 */
95805 if( pOp->p3 ){
95807 assert( pOp->p3>0 );
95808 if( p->pFrame ){
95809 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
95811 assert( pOp->p3<=pFrame->nMem );
95812 pMem = &pFrame->aMem[pOp->p3];
95815 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
95816 pMem = &aMem[pOp->p3];
95821 REGISTER_TRACE(pOp->p3, pMem);
95823 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
95824 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
95825 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
95828 if( v<pMem->u.i+1 ){
95829 v = pMem->u.i + 1;
95831 pMem->u.i = v;
95834 if( pC->useRandomRowid ){
95835 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
95839 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
95845 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
95851 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
95854 assert( v>0 ); /* EV: R-40812-03570 */
95856 pC->deferredMoveto = 0;
95857 pC->cacheStatus = CACHE_STALE;
95859 pOut->u.i = v;
95888 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
95891 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
95892 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
95905 const char *zDb; /* database name - used by the update hook */
95906 Table *pTab; /* Table structure - used by update and pre-update hooks */
95909 pData = &aMem[pOp->p2];
95910 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
95912 pC = p->apCsr[pOp->p1];
95914 assert( pC->eCurType==CURTYPE_BTREE );
95915 assert( pC->deferredMoveto==0 );
95916 assert( pC->uc.pCursor!=0 );
95917 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
95918 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
95919 REGISTER_TRACE(pOp->p2, pData);
95922 pKey = &aMem[pOp->p3];
95923 assert( pKey->flags & MEM_Int );
95925 REGISTER_TRACE(pOp->p3, pKey);
95926 x.nKey = pKey->u.i;
95928 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
95929 assert( pC->iDb>=0 );
95930 zDb = db->aDb[pC->iDb].zDbSName;
95931 pTab = pOp->p4.pTab;
95932 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
95939 /* Invoke the pre-update hook, if any */
95941 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
95942 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
95944 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
95945 /* Prevent post-update hook from running in cases when it should not */
95949 if( pOp->p5 & OPFLAG_ISNOOP ) break;
95952 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
95953 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
95954 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
95955 x.pData = pData->z;
95956 x.nData = pData->n;
95957 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
95958 if( pData->flags & MEM_Zero ){
95959 x.nZero = pData->u.nZero;
95964 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
95965 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
95968 pC->deferredMoveto = 0;
95969 pC->cacheStatus = CACHE_STALE;
95971 /* Invoke the update-hook if required. */
95974 assert( db->xUpdateCallback!=0 );
95975 assert( pTab->aCol!=0 );
95976 db->xUpdateCallback(db->pUpdateArg,
95977 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
95978 zDb, pTab->zName, x.nKey);
95986 ** of table - intkey or index. This opcode is used as part of copying
95999 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
96000 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
96002 pDest = p->apCsr[pOp->p1];
96003 pSrc = p->apCsr[pOp->p2];
96004 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
96005 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
96017 ** the next Next instruction will be a no-op. As a result, in this case
96031 ** P1 must not be pseudo-table. It has to be a real table with
96035 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
96037 ** this case. Specifically, if one is configured, the pre-update hook is
96038 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
96051 opflags = pOp->p2;
96052 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96053 pC = p->apCsr[pOp->p1];
96055 assert( pC->eCurType==CURTYPE_BTREE );
96056 assert( pC->uc.pCursor!=0 );
96057 assert( pC->deferredMoveto==0 );
96061 if( pOp->p4type==P4_TABLE
96062 && HasRowid(pOp->p4.pTab)
96063 && pOp->p5==0
96064 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
96067 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
96069 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96070 assert( CORRUPT_DB || pC->movetoTarget==iKey );
96074 /* If the update-hook or pre-update-hook will be invoked, set zDb to
96079 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
96080 assert( pC->iDb>=0 );
96081 assert( pOp->p4.pTab!=0 );
96082 zDb = db->aDb[pC->iDb].zDbSName;
96083 pTab = pOp->p4.pTab;
96084 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
96085 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96093 /* Invoke the pre-update-hook if required. */
96094 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
96095 if( db->xPreUpdateCallback && pTab ){
96098 || (aMem[pOp->p3].flags & MEM_Int)
96102 zDb, pTab, pC->movetoTarget,
96103 pOp->p3, -1
96110 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
96115 if( p->pFrame==0 ){
96116 if( pC->isEphemeral==0
96117 && (pOp->p5 & OPFLAG_AUXDELETE)==0
96118 && (pC->wrFlag & OPFLAG_FORDELETE)==0
96122 if( pOp->p2 & OPFLAG_NCHANGE ){
96123 nExtraDelete--;
96128 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
96129 pC->cacheStatus = CACHE_STALE;
96130 pC->seekResult = 0;
96133 /* Invoke the update-hook if required. */
96135 p->nChange++;
96136 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
96137 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
96138 pC->movetoTarget);
96139 assert( pC->iDb>=0 );
96153 sqlite3VdbeSetChanges(db, p->nChange);
96154 p->nChange = 0;
96178 pC = p->apCsr[pOp->p1];
96180 assert( pOp->p4type==P4_INT32 );
96181 pIn3 = &aMem[pOp->p3];
96182 nKeyCol = pOp->p4.i;
96198 ** a register that is the source for a pseudo-table cursor created using
96199 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
96206 pOut = &aMem[pOp->p2];
96207 pC = p->apCsr[pOp->p1];
96210 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
96211 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96213 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
96230 ** of a real table, not a pseudo-table.
96234 ** register will be invalidated as soon as the cursor moves - including
96252 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96253 pC = p->apCsr[pOp->p1];
96255 assert( pC->eCurType==CURTYPE_BTREE );
96257 assert( pC->nullRow==0 );
96258 assert( pC->uc.pCursor!=0 );
96259 pCrsr = pC->uc.pCursor;
96269 assert( pC->deferredMoveto==0 );
96273 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
96279 if( !pOp->p3 ) Deephemeralize(pOut);
96281 REGISTER_TRACE(pOp->p2, pOut);
96302 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96303 pC = p->apCsr[pOp->p1];
96305 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
96306 if( pC->nullRow ){
96307 pOut->flags = MEM_Null;
96309 }else if( pC->deferredMoveto ){
96310 v = pC->movetoTarget;
96312 }else if( pC->eCurType==CURTYPE_VTAB ){
96313 assert( pC->uc.pVCur!=0 );
96314 pVtab = pC->uc.pVCur->pVtab;
96315 pModule = pVtab->pModule;
96316 assert( pModule->xRowid );
96317 rc = pModule->xRowid(pC->uc.pVCur, &v);
96322 assert( pC->eCurType==CURTYPE_BTREE );
96323 assert( pC->uc.pCursor!=0 );
96326 if( pC->nullRow ){
96327 pOut->flags = MEM_Null;
96330 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
96332 pOut->u.i = v;
96343 ** pseudo-cursor that always returns NULL for every column.
96348 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96349 pC = p->apCsr[pOp->p1];
96352 ** pseudo-cursor that always gives null rows. */
96353 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
96355 pC->seekResult = 0;
96356 pC->isTable = 1;
96357 pC->noReuse = 1;
96358 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
96360 pC->nullRow = 1;
96361 pC->cacheStatus = CACHE_STALE;
96362 if( pC->eCurType==CURTYPE_BTREE ){
96363 assert( pC->uc.pCursor!=0 );
96364 sqlite3BtreeClearCursor(pC->uc.pCursor);
96367 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
96400 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96401 pC = p->apCsr[pOp->p1];
96403 assert( pC->eCurType==CURTYPE_BTREE );
96404 pCrsr = pC->uc.pCursor;
96408 pC->seekOp = pOp->opcode;
96410 if( pOp->opcode==OP_SeekEnd ){
96411 assert( pOp->p2==0 );
96412 pC->seekResult = -1;
96418 pC->nullRow = (u8)res;
96419 pC->deferredMoveto = 0;
96420 pC->cacheStatus = CACHE_STALE;
96422 if( pOp->p2>0 ){
96440 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96441 pC = p->apCsr[pOp->p1];
96443 pCrsr = pC->uc.pCursor;
96449 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
96482 sqlite3_search_count--;
96484 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
96505 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96506 assert( pOp->p5==0 );
96507 pC = p->apCsr[pOp->p1];
96509 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
96512 pC->seekOp = OP_Rewind;
96517 assert( pC->eCurType==CURTYPE_BTREE );
96518 pCrsr = pC->uc.pCursor;
96521 pC->deferredMoveto = 0;
96522 pC->cacheStatus = CACHE_STALE;
96525 pC->nullRow = (u8)res;
96526 assert( pOp->p2>0 && pOp->p2<p->nOp );
96543 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
96552 ** number P5-1 in the prepared statement is incremented.
96568 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
96577 ** number P5-1 in the prepared statement is incremented.
96589 pC = p->apCsr[pOp->p1];
96595 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96596 assert( pOp->p5==0
96597 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
96598 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
96599 pC = p->apCsr[pOp->p1];
96601 assert( pC->deferredMoveto==0 );
96602 assert( pC->eCurType==CURTYPE_BTREE );
96603 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
96604 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
96605 || pC->seekOp==OP_NullRow);
96606 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
96610 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96611 assert( pOp->p5==0
96612 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
96613 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
96614 pC = p->apCsr[pOp->p1];
96616 assert( pC->deferredMoveto==0 );
96617 assert( pC->eCurType==CURTYPE_BTREE );
96618 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
96619 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
96620 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
96621 || pC->seekOp==OP_IfNoHope);
96622 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
96625 pC->cacheStatus = CACHE_STALE;
96628 pC->nullRow = 0;
96629 p->aCounter[pOp->p5]++;
96637 pC->nullRow = 1;
96653 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
96673 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96674 pC = p->apCsr[pOp->p1];
96678 pIn2 = &aMem[pOp->p2];
96679 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
96680 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
96681 assert( pC->eCurType==CURTYPE_BTREE );
96682 assert( pC->isTable==0 );
96685 x.nKey = pIn2->n;
96686 x.pKey = pIn2->z;
96687 x.aMem = aMem + pOp->p3;
96688 x.nMem = (u16)pOp->p4.i;
96689 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
96690 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
96691 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
96693 assert( pC->deferredMoveto==0 );
96694 pC->cacheStatus = CACHE_STALE;
96709 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96710 pC = p->apCsr[pOp->p1];
96714 pIn2 = &aMem[pOp->p2];
96715 assert( pIn2->flags & MEM_Blob );
96716 assert( pC->isTable==0 );
96737 ** this (self-correcting and non-critical) error if in writable_schema mode.
96745 assert( pOp->p3>0 );
96746 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
96747 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96748 pC = p->apCsr[pOp->p1];
96750 assert( pC->eCurType==CURTYPE_BTREE );
96752 pCrsr = pC->uc.pCursor;
96754 r.pKeyInfo = pC->pKeyInfo;
96755 r.nField = (u16)pOp->p3;
96757 r.aMem = &aMem[pOp->p2];
96763 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
96767 assert( pC->deferredMoveto==0 );
96768 pC->cacheStatus = CACHE_STALE;
96769 pC->seekResult = 0;
96786 ** is non-zero, then reading column a(i)-1 from cursor P3 is
96807 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96808 pC = p->apCsr[pOp->p1];
96810 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
96811 assert( pC->uc.pCursor!=0 );
96812 assert( pC->isTable==0 || IsNullCursor(pC) );
96813 assert( pC->deferredMoveto==0 );
96814 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
96825 if( !pC->nullRow ){
96827 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
96831 if( pOp->opcode==OP_DeferredSeek ){
96832 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
96833 pTabCur = p->apCsr[pOp->p3];
96835 assert( pTabCur->eCurType==CURTYPE_BTREE );
96836 assert( pTabCur->uc.pCursor!=0 );
96837 assert( pTabCur->isTable );
96838 pTabCur->nullRow = 0;
96839 pTabCur->movetoTarget = rowid;
96840 pTabCur->deferredMoveto = 1;
96841 pTabCur->cacheStatus = CACHE_STALE;
96842 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
96843 assert( !pTabCur->isEphemeral );
96844 pTabCur->ub.aAltMap = pOp->p4.ai;
96845 assert( !pC->isEphemeral );
96846 pTabCur->pAltCursor = pC;
96849 pOut->u.i = rowid;
96852 assert( pOp->opcode==OP_IdxRowid );
96853 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
96862 ** already occurred, this instruction is a no-op.
96867 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96868 pC = p->apCsr[pOp->p1];
96869 if( pC->deferredMoveto ){
96928 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96929 pC = p->apCsr[pOp->p1];
96931 assert( pC->isOrdered );
96932 assert( pC->eCurType==CURTYPE_BTREE );
96933 assert( pC->uc.pCursor!=0);
96934 assert( pC->deferredMoveto==0 );
96935 assert( pOp->p4type==P4_INT32 );
96936 r.pKeyInfo = pC->pKeyInfo;
96937 r.nField = (u16)pOp->p4.i;
96938 if( pOp->opcode<OP_IdxLT ){
96939 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
96940 r.default_rc = -1;
96942 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
96945 r.aMem = &aMem[pOp->p3];
96951 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
96962 assert( pC->eCurType==CURTYPE_BTREE );
96963 pCur = pC->uc.pCursor;
96981 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
96982 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
96983 res = -res;
96985 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
97006 ** value of the root page that moved - its value before the move occurred -
97017 ** and non-autovacuum modes.
97026 assert( p->readOnly==0 );
97027 assert( pOp->p1>1 );
97029 pOut->flags = MEM_Null;
97030 if( db->nVdbeRead > db->nVDestroy+1 ){
97032 p->errorAction = OE_Abort;
97035 iDb = pOp->p3;
97036 assert( DbMaskTest(p->btreeMask, iDb) );
97038 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
97039 pOut->flags = MEM_Int;
97040 pOut->u.i = iMoved;
97044 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
97064 ** If the P3 value is non-zero, then the row change count is incremented
97076 assert( p->readOnly==0 );
97077 assert( DbMaskTest(p->btreeMask, pOp->p2) );
97078 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
97079 if( pOp->p3 ){
97080 p->nChange += nChange;
97081 if( pOp->p3>0 ){
97082 assert( memIsValid(&aMem[pOp->p3]) );
97083 memAboutToChange(p, &aMem[pOp->p3]);
97084 aMem[pOp->p3].u.i += nChange;
97102 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97103 pC = p->apCsr[pOp->p1];
97106 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
97108 assert( pC->eCurType==CURTYPE_BTREE );
97109 assert( pC->isEphemeral );
97110 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
97119 ** Allocate a new b-tree in the main database file if P1==0 or in the
97123 ** The root page number of the new b-tree is stored in register P2.
97132 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
97133 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97134 assert( DbMaskTest(p->btreeMask, pOp->p1) );
97135 assert( p->readOnly==0 );
97136 pDb = &db->aDb[pOp->p1];
97137 assert( pDb->pBt!=0 );
97138 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
97140 pOut->u.i = pgno;
97150 db->nSqlExec++;
97151 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
97152 db->nSqlExec--;
97164 ** then runs the new virtual machine. It is thus a re-entrant opcode.
97177 for(iDb=0; iDb<db->nDb; iDb++){
97178 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
97182 iDb = pOp->p1;
97183 assert( iDb>=0 && iDb<db->nDb );
97185 || db->mallocFailed
97186 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
97189 if( pOp->p4.z==0 ){
97190 sqlite3SchemaClear(db->aDb[iDb].pSchema);
97191 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
97192 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
97193 db->mDbFlags |= DBFLAG_SchemaChange;
97194 p->expired = 0;
97201 initData.pzErrMsg = &p->zErrMsg;
97203 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
97206 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
97210 assert( db->init.busy==0 );
97211 db->init.busy = 1;
97214 assert( !db->mallocFailed );
97218 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
97224 db->init.busy = 0;
97245 assert( pOp->p1>=0 && pOp->p1<db->nDb );
97246 rc = sqlite3AnalysisLoad(db, pOp->p1);
97254 ** Remove the internal (in-memory) data structures that describe
97262 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
97268 ** Remove the internal (in-memory) data structures that describe
97276 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
97282 ** Remove the internal (in-memory) data structures that describe
97290 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
97322 assert( p->bIsReader );
97323 nRoot = pOp->p2;
97324 aRoot = pOp->p4.ai;
97327 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
97328 pnErr = &aMem[pOp->p3];
97329 assert( (pnErr->flags & MEM_Int)!=0 );
97330 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
97331 pIn1 = &aMem[pOp->p1];
97332 assert( pOp->p5<db->nDb );
97333 assert( DbMaskTest(p->btreeMask, pOp->p5) );
97334 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
97335 (int)pnErr->u.i+1, &nErr);
97342 pnErr->u.i -= nErr-1;
97343 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
97360 pIn1 = &aMem[pOp->p1];
97361 pIn2 = &aMem[pOp->p2];
97362 assert( (pIn2->flags & MEM_Int)!=0 );
97363 if( (pIn1->flags & MEM_Blob)==0 ){
97367 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
97382 pIn1 = &aMem[pOp->p1];
97383 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
97384 if( (pIn1->flags & MEM_Blob)==0
97385 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
97394 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
97402 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
97411 ** must have P4==0, the final set must have P4==-1, and for all other sets
97416 ** (b) when P4==-1 there is no need to insert the value, as it will
97426 pIn1 = &aMem[pOp->p1];
97427 pIn3 = &aMem[pOp->p3];
97428 iSet = pOp->p4.i;
97429 assert( pIn3->flags&MEM_Int );
97434 if( (pIn1->flags & MEM_Blob)==0 ){
97438 assert( pOp->p4type==P4_INT32 );
97439 assert( iSet==-1 || iSet>=0 );
97441 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
97446 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
97459 ** cell in an array of values used as arguments to the sub-program. P2
97460 ** contains the address to jump to if the sub-program throws an IGNORE
97463 ** memory required by the sub-vdbe at runtime.
97467 ** If P5 is non-zero, then recursive program invocation is enabled.
97470 int nMem; /* Number of memory registers for sub-program */
97471 int nByte; /* Bytes of runtime space required for sub-program */
97476 SubProgram *pProgram; /* Sub-program to execute */
97479 pProgram = pOp->p4.pProgram;
97480 pRt = &aMem[pOp->p3];
97481 assert( pProgram->nOp>0 );
97484 ** disabled for backwards compatibility (p5 is set if this sub-program
97494 if( pOp->p5 ){
97495 t = pProgram->token;
97496 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
97500 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
97510 if( (pRt->flags&MEM_Blob)==0 ){
97516 nMem = pProgram->nMem + pProgram->nCsr;
97518 if( pProgram->nCsr==0 ) nMem++;
97521 + pProgram->nCsr * sizeof(VdbeCursor*)
97522 + (pProgram->nOp + 7)/8;
97528 pRt->flags = MEM_Blob|MEM_Dyn;
97529 pRt->z = (char*)pFrame;
97530 pRt->n = nByte;
97531 pRt->xDel = sqlite3VdbeFrameMemDel;
97533 pFrame->v = p;
97534 pFrame->nChildMem = nMem;
97535 pFrame->nChildCsr = pProgram->nCsr;
97536 pFrame->pc = (int)(pOp - aOp);
97537 pFrame->aMem = p->aMem;
97538 pFrame->nMem = p->nMem;
97539 pFrame->apCsr = p->apCsr;
97540 pFrame->nCursor = p->nCursor;
97541 pFrame->aOp = p->aOp;
97542 pFrame->nOp = p->nOp;
97543 pFrame->token = pProgram->token;
97545 pFrame->anExec = p->anExec;
97548 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
97551 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
97553 pMem->flags = MEM_Undefined;
97554 pMem->db = db;
97557 pFrame = (VdbeFrame*)pRt->z;
97558 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
97559 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
97560 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
97561 assert( pProgram->nCsr==pFrame->nChildCsr );
97562 assert( (int)(pOp - aOp)==pFrame->pc );
97565 p->nFrame++;
97566 pFrame->pParent = p->pFrame;
97567 pFrame->lastRowid = db->lastRowid;
97568 pFrame->nChange = p->nChange;
97569 pFrame->nDbChange = p->db->nChange;
97570 assert( pFrame->pAuxData==0 );
97571 pFrame->pAuxData = p->pAuxData;
97572 p->pAuxData = 0;
97573 p->nChange = 0;
97574 p->pFrame = pFrame;
97575 p->aMem = aMem = VdbeFrameMem(pFrame);
97576 p->nMem = pFrame->nChildMem;
97577 p->nCursor = (u16)pFrame->nChildCsr;
97578 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
97579 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
97580 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
97581 p->aOp = aOp = pProgram->aOp;
97582 p->nOp = pProgram->nOp;
97584 p->anExec = 0;
97591 for(i=0; i<p->nMem; i++){
97592 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
97597 pOp = &aOp[-1];
97603 ** This opcode is only ever present in sub-programs called via the
97617 pFrame = p->pFrame;
97618 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
97630 ** If P1 is non-zero, the database constraint counter is incremented
97635 if( db->flags & SQLITE_DeferFKs ){
97636 db->nDeferredImmCons += pOp->p2;
97637 }else if( pOp->p1 ){
97638 db->nDeferredCons += pOp->p2;
97640 p->nFkConstraint += pOp->p2;
97648 ** This opcode tests if a foreign key constraint-counter is currently zero.
97652 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
97654 ** zero, the jump is taken if the statement constraint-counter is zero
97658 if( pOp->p1 ){
97659 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
97660 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
97662 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
97663 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
97675 ** within a sub-program). Set the value of register P1 to the maximum of
97683 if( p->pFrame ){
97684 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
97685 pIn1 = &pFrame->aMem[pOp->p1];
97687 pIn1 = &aMem[pOp->p1];
97691 pIn2 = &aMem[pOp->p2];
97693 if( pIn1->u.i<pIn2->u.i){
97694 pIn1->u.i = pIn2->u.i;
97701 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
97711 pIn1 = &aMem[pOp->p1];
97712 assert( pIn1->flags&MEM_Int );
97713 VdbeBranchTaken( pIn1->u.i>0, 2);
97714 if( pIn1->u.i>0 ){
97715 pIn1->u.i -= pOp->p3;
97722 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
97735 ** and r[P2] is set to -1.
97741 pIn1 = &aMem[pOp->p1];
97742 pIn3 = &aMem[pOp->p3];
97744 assert( pIn1->flags & MEM_Int );
97745 assert( pIn3->flags & MEM_Int );
97746 x = pIn1->u.i;
97747 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
97755 pOut->u.i = -1;
97757 pOut->u.i = x;
97763 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
97767 ** If it is non-zero (negative or positive) and then also jump to P2.
97771 pIn1 = &aMem[pOp->p1];
97772 assert( pIn1->flags&MEM_Int );
97773 VdbeBranchTaken(pIn1->u.i<0, 2);
97774 if( pIn1->u.i ){
97775 if( pIn1->u.i>0 ) pIn1->u.i--;
97782 ** Synopsis: if (--r[P1])==0 goto P2
97789 if( pOp->p5&0x80 ){
97790 if( p->pSharedBlock!=NULL ){
97791 if( p->totalRows < p->startPos || p->blockFull ){
97792 pIn1 = &aMem[pOp->p1];
97793 assert( pIn1->flags&MEM_Int );
97794 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
97795 VdbeBranchTaken(pIn1->u.i==-1, 2);
97796 if( pIn1->u.i==-1 ) goto jump_to_p2;
97802 pIn1 = &aMem[pOp->p1];
97803 assert( pIn1->flags&MEM_Int );
97804 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
97805 VdbeBranchTaken(pIn1->u.i==0, 2);
97806 if( pIn1->u.i==0 ) goto jump_to_p2;
97855 assert( pOp->p4type==P4_FUNCDEF );
97856 n = pOp->p5;
97857 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
97858 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
97859 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
97861 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
97863 pCtx->pMem = 0;
97864 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
97865 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
97866 pCtx->pFunc = pOp->p4.pFunc;
97867 pCtx->iOp = (int)(pOp - aOp);
97868 pCtx->pVdbe = p;
97869 pCtx->skipFlag = 0;
97870 pCtx->isError = 0;
97871 pCtx->enc = encoding;
97872 pCtx->argc = n;
97873 pOp->p4type = P4_FUNCCTX;
97874 pOp->p4.pCtx = pCtx;
97877 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
97879 pOp->opcode = OP_AggStep1;
97888 assert( pOp->p4type==P4_FUNCCTX );
97889 pCtx = pOp->p4.pCtx;
97890 pMem = &aMem[pOp->p3];
97893 if( pOp->p1 ){
97896 assert( pMem->uTemp==0x1122e0e3 );
97899 pMem->uTemp = 0x1122e0e3;
97907 if( pCtx->pMem != pMem ){
97908 pCtx->pMem = pMem;
97909 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
97913 for(i=0; i<pCtx->argc; i++){
97914 assert( memIsValid(pCtx->argv[i]) );
97915 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
97919 pMem->n++;
97920 assert( pCtx->pOut->flags==MEM_Null );
97921 assert( pCtx->isError==0 );
97922 assert( pCtx->skipFlag==0 );
97924 if( pOp->p1 ){
97925 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
97928 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
97930 if( pCtx->isError ){
97931 if( pCtx->isError>0 ){
97932 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
97933 rc = pCtx->isError;
97935 if( pCtx->skipFlag ){
97936 assert( pOp[-1].opcode==OP_CollSeq );
97937 i = pOp[-1].p1;
97939 pCtx->skipFlag = 0;
97941 sqlite3VdbeMemRelease(pCtx->pOut);
97942 pCtx->pOut->flags = MEM_Null;
97943 pCtx->isError = 0;
97946 assert( pCtx->pOut->flags==MEM_Null );
97947 assert( pCtx->skipFlag==0 );
97980 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
97981 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
97982 pMem = &aMem[pOp->p1];
97983 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
97985 if( pOp->p3 ){
97986 memAboutToChange(p, &aMem[pOp->p3]);
97987 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
97988 pMem = &aMem[pOp->p3];
97992 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
98007 ** Checkpoint database P1. This is a no-op if P1 is not currently in
98014 ** mem[P3+2] are initialized to -1.
98021 assert( p->readOnly==0 );
98023 aRes[1] = aRes[2] = -1;
98024 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
98025 || pOp->p2==SQLITE_CHECKPOINT_FULL
98026 || pOp->p2==SQLITE_CHECKPOINT_RESTART
98027 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
98029 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
98035 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
98052 ** Write a string containing the final journal-mode to register P2.
98064 eNew = pOp->p3;
98073 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98074 assert( p->readOnly==0 );
98076 pBt = db->aDb[pOp->p1].pBt;
98091 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
98099 if( !db->autoCommit || db->nVdbeRead>1 ){
98110 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
98138 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
98139 pOut->z = (char *)sqlite3JournalModename(eNew);
98140 pOut->n = sqlite3Strlen30(pOut->z);
98141 pOut->enc = SQLITE_UTF8;
98159 assert( p->readOnly==0 );
98160 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
98161 pOp->p2 ? &aMem[pOp->p2] : 0);
98177 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98178 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98179 assert( p->readOnly==0 );
98180 pBt = db->aDb[pOp->p1].pBt;
98199 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
98209 assert( pOp->p2==0 || pOp->p2==1 );
98210 if( !pOp->p1 ){
98211 sqlite3ExpirePreparedStatements(db, pOp->p2);
98213 p->expired = pOp->p2+1;
98225 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98226 pC = p->apCsr[pOp->p1];
98228 assert( pC->eCurType==CURTYPE_BTREE );
98229 sqlite3BtreeCursorPin(pC->uc.pCursor);
98240 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98241 pC = p->apCsr[pOp->p1];
98243 assert( pC->eCurType==CURTYPE_BTREE );
98244 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
98253 ** the shared-cache feature is enabled.
98259 ** P2 contains the root-page of the table to lock.
98265 u8 isWriteLock = (u8)pOp->p3;
98266 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
98267 int p1 = pOp->p1;
98268 assert( p1>=0 && p1<db->nDb );
98269 assert( DbMaskTest(p->btreeMask, p1) );
98271 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
98274 const char *z = pOp->p4.z;
98296 pVTab = pOp->p4.pVtab;
98298 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
98318 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
98319 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
98320 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
98323 assert( zTab || db->mallocFailed );
98325 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
98340 db->nVDestroy++;
98341 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
98342 db->nVDestroy--;
98343 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
98362 assert( p->bIsReader );
98365 pVtab = pOp->p4.pVtab->pVtab;
98366 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
98370 pModule = pVtab->pModule;
98371 rc = pModule->xOpen(pVtab, &pVCur);
98376 pVCur->pVtab = pVtab;
98379 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
98381 pCur->uc.pVCur = pVCur;
98382 pVtab->nRef++;
98384 assert( db->mallocFailed );
98385 pModule->xClose(pVCur);
98407 pC = p->apCsr[pOp->p1];
98410 pRhs->pCsr = pC->uc.pCursor;
98411 pRhs->pOut = &aMem[pOp->p3];
98413 pOut->flags = MEM_Null;
98453 pQuery = &aMem[pOp->p3];
98455 pCur = p->apCsr[pOp->p1];
98457 REGISTER_TRACE(pOp->p3, pQuery);
98459 assert( pCur->eCurType==CURTYPE_VTAB );
98460 pVCur = pCur->uc.pVCur;
98461 pVtab = pVCur->pVtab;
98462 pModule = pVtab->pModule;
98465 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
98466 nArg = (int)pArgc->u.i;
98467 iQuery = (int)pQuery->u.i;
98470 apArg = p->apArg;
98474 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
98477 res = pModule->xEof(pVCur);
98478 pCur->nullRow = 0;
98489 ** Store in register P3 the value of the P2-th column of
98490 ** the current row of the virtual-table of cursor P1.
98506 VdbeCursor *pCur = p->apCsr[pOp->p1];
98508 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98509 pDest = &aMem[pOp->p3];
98511 if( pCur->nullRow ){
98515 assert( pCur->eCurType==CURTYPE_VTAB );
98516 pVtab = pCur->uc.pVCur->pVtab;
98517 pModule = pVtab->pModule;
98518 assert( pModule->xColumn );
98522 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
98523 if( pOp->p5 & OPFLAG_NOCHNG ){
98525 pDest->flags = MEM_Null|MEM_Zero;
98526 pDest->u.nZero = 0;
98530 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
98537 REGISTER_TRACE(pOp->p3, pDest);
98558 pCur = p->apCsr[pOp->p1];
98560 assert( pCur->eCurType==CURTYPE_VTAB );
98561 if( pCur->nullRow ){
98564 pVtab = pCur->uc.pVCur->pVtab;
98565 pModule = pVtab->pModule;
98566 assert( pModule->xNext );
98574 rc = pModule->xNext(pCur->uc.pVCur);
98577 res = pModule->xEof(pCur->uc.pVCur);
98599 isLegacy = (db->flags & SQLITE_LegacyAlter);
98600 db->flags |= SQLITE_LegacyAlter;
98601 pVtab = pOp->p4.pVtab->pVtab;
98602 pName = &aMem[pOp->p1];
98603 assert( pVtab->pModule->xRename );
98605 assert( p->readOnly==0 );
98606 REGISTER_TRACE(pOp->p1, pName);
98607 assert( pName->flags & MEM_Str );
98608 testcase( pName->enc==SQLITE_UTF8 );
98609 testcase( pName->enc==SQLITE_UTF16BE );
98610 testcase( pName->enc==SQLITE_UTF16LE );
98613 rc = pVtab->pModule->xRename(pVtab, pName->z);
98614 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
98616 p->expired = 0;
98629 ** invocation. The value in register (P3+P2-1) corresponds to the
98659 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
98660 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
98662 assert( p->readOnly==0 );
98663 if( db->mallocFailed ) goto no_mem;
98665 pVtab = pOp->p4.pVtab->pVtab;
98666 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
98670 pModule = pVtab->pModule;
98671 nArg = pOp->p2;
98672 assert( pOp->p4type==P4_VTAB );
98673 if( ALWAYS(pModule->xUpdate) ){
98674 u8 vtabOnConflict = db->vtabOnConflict;
98675 apArg = p->apArg;
98676 pX = &aMem[pOp->p3];
98683 db->vtabOnConflict = pOp->p5;
98684 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
98685 db->vtabOnConflict = vtabOnConflict;
98687 if( rc==SQLITE_OK && pOp->p1 ){
98688 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
98689 db->lastRowid = rowid;
98691 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
98692 if( pOp->p5==OE_Ignore ){
98695 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
98698 p->nChange++;
98713 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
98733 pBt = db->aDb[pOp->p1].pBt;
98735 if( pOp->p3 ){
98737 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
98739 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
98754 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
98773 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
98782 ** purely non-deterministic. Some built-in date/time functions can be
98783 ** either determinitic of non-deterministic, depending on their arguments.
98784 ** When those function are used in a non-deterministic way, they will check
98795 assert( pOp->p4type==P4_FUNCCTX );
98796 pCtx = pOp->p4.pCtx;
98802 pOut = &aMem[pOp->p3];
98803 if( pCtx->pOut != pOut ){
98804 pCtx->pVdbe = p;
98805 pCtx->pOut = pOut;
98806 pCtx->enc = encoding;
98807 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
98809 assert( pCtx->pVdbe==p );
98813 for(i=0; i<pCtx->argc; i++){
98814 assert( memIsValid(pCtx->argv[i]) );
98815 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
98819 assert( pCtx->isError==0 );
98820 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
98823 if( pCtx->isError ){
98824 if( pCtx->isError>0 ){
98826 rc = pCtx->isError;
98828 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
98829 pCtx->isError = 0;
98833 assert( (pOut->flags&MEM_Str)==0
98834 || pOut->enc==encoding
98835 || db->mallocFailed );
98838 REGISTER_TRACE(pOp->p3, pOut);
98849 pIn1 = &aMem[pOp->p1];
98850 pIn1->flags &= ~MEM_Subtype;
98863 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
98864 pIn1 = &aMem[pOp->p1];
98865 assert( pIn1->flags & MEM_Blob );
98866 assert( pIn1->n>0 );
98869 if( db->flags&SQLITE_VdbeTrace ){
98871 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
98874 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
98877 h %= pIn1->n;
98878 pIn1->z[h/8] |= 1<<(h&7);
98894 ** false positive - if the jump is taken when it should fall through.
98899 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
98900 pIn1 = &aMem[pOp->p1];
98901 assert( (pIn1->flags & MEM_Blob)!=0 );
98902 assert( pIn1->n >= 1 );
98905 if( db->flags&SQLITE_VdbeTrace ){
98907 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
98910 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
98913 h %= pIn1->n;
98914 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
98916 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
98919 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
98939 ** the UTF-8 string contained in P4 is emitted on the trace callback.
98958 ** The "--" string is broken up to prevent false-positives with srcck1.c.
98961 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
98963 ** using the X argument when X begins with "--" and invoking
98966 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
98969 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
98972 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
98973 && p->minWriteFileFormat!=254 /* tag-20220401a */
98974 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
98977 if( db->mTrace & SQLITE_TRACE_LEGACY ){
98979 db->trace.xLegacy(db->pTraceArg, z);
98983 if( db->nVdbeExec>1 ){
98984 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
98985 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
98988 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
98992 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
98995 for(j=0; j<db->nDb; j++){
98996 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
98997 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
99002 if( (db->flags & SQLITE_SqlTrace)!=0
99003 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
99005 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
99009 assert( pOp->p2>0 );
99010 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
99011 if( pOp->opcode==OP_Trace ) break;
99012 for(i=1; i<p->nOp; i++){
99013 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
99015 pOp->p1 = 0;
99017 pOp->p1++;
99018 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
99028 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
99033 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99034 assert( pOp->p4type==P4_EXPR );
99035 pC = p->apCsr[pOp->p1];
99037 assert( pC->eCurType==CURTYPE_BTREE );
99038 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
99039 pOp->p4.pExpr, aMem);
99096 assert( pOp->p1>0 );
99097 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
99098 pMem = &aMem[pOp->p1];
99099 constMask = pOp->p3;
99100 for(i=0; i<pOp->p2; i++, pMem++){
99102 pMem->pScopyFrom = 0;
99103 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
99119 ** the same as a no-op. This opcodesnever appears in a real VM program.
99122 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
99129 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
99138 if( endTime>start ) pOrigOp->cycles += endTime - start;
99139 pOrigOp->cnt++;
99149 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
99152 if( db->flags & SQLITE_VdbeTrace ){
99153 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
99156 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
99159 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
99176 if( db->mallocFailed ){
99183 if( db->flags & SQLITE_VdbeTrace ){
99184 const char *zTrace = p->zSql;
99191 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
99194 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
99197 p->rc = rc;
99201 (int)(pOp - aOp), p->zSql, p->zErrMsg);
99202 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
99204 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
99205 db->flags |= SQLITE_CorruptRdOnly;
99209 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
99217 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
99218 nProgressLimit += db->nProgressOps;
99219 if( db->xProgress(db->pProgressArg) ){
99226 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
99229 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
99253 assert( AtomicLoad(&db->u1.isInterrupted) );
99299 ** the b-tree cursor associated with blob handle p to point to row iRow.
99310 ** If an error does occur, then the b-tree cursor is closed. All subsequent
99317 Vdbe *v = (Vdbe *)p->pStmt;
99322 v->aMem[1].flags = MEM_Int;
99323 v->aMem[1].u.i = iRow;
99329 if( v->pc>4 ){
99330 v->pc = 4;
99331 assert( v->aOp[v->pc].opcode==OP_NotExists );
99334 rc = sqlite3_step(p->pStmt);
99337 VdbeCursor *pC = v->apCsr[0];
99340 assert( pC->eCurType==CURTYPE_BTREE );
99341 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
99342 testcase( pC->nHdrParsed==p->iCol );
99343 testcase( pC->nHdrParsed==p->iCol+1 );
99345 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
99349 sqlite3_finalize(p->pStmt);
99350 p->pStmt = 0;
99352 p->iOffset = pC->aType[p->iCol + pC->nField];
99353 p->nByte = sqlite3VdbeSerialTypeLen(type);
99354 p->pCsr = pC->uc.pCursor;
99355 sqlite3BtreeIncrblobCursor(p->pCsr);
99361 }else if( p->pStmt ){
99362 rc = sqlite3_finalize(p->pStmt);
99363 p->pStmt = 0;
99365 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
99368 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
99388 int wrFlag, /* True -> read/write access, false -> read-only */
99392 int iCol; /* Index of zColumn in row-record */
99412 sqlite3_mutex_enter(db->mutex);
99447 pBlob->pTab = pTab;
99448 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
99451 for(iCol=0; iCol<pTab->nCol; iCol++) {
99452 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
99456 if( iCol==pTab->nCol ){
99471 if( db->flags&SQLITE_ForeignKeys ){
99478 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
99480 for(j=0; j<pFKey->nCol; j++){
99481 if( pFKey->aCol[j].iFrom==iCol ){
99488 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
99490 for(j=0; j<pIdx->nKeyCol; j++){
99492 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
99506 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
99507 assert( pBlob->pStmt || db->mallocFailed );
99508 if( pBlob->pStmt ){
99512 ** of writing code to use the b-tree layer directly is that the
99517 ** Code external to the Vdbe then "borrows" the b-tree cursor and
99522 ** which closes the b-tree cursor and (possibly) commits the
99535 Vdbe *v = (Vdbe *)pBlob->pStmt;
99536 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
99540 pTab->pSchema->schema_cookie,
99541 pTab->pSchema->iGeneration);
99543 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
99549 if( db->mallocFailed==0 ){
99556 aOp[0].p2 = pTab->tnum;
99558 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
99560 if( db->mallocFailed==0 ){
99564 ** parameter of the other to pTab->tnum. */
99566 aOp[1].p2 = pTab->tnum;
99577 aOp[1].p4.i = pTab->nCol+1;
99578 aOp[3].p2 = pTab->nCol;
99587 pBlob->iCol = iCol;
99588 pBlob->db = db;
99590 if( db->mallocFailed ){
99599 if( rc==SQLITE_OK && db->mallocFailed==0 ){
99602 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
99609 sqlite3_mutex_leave(db->mutex);
99623 sqlite3_stmt *pStmt = p->pStmt;
99624 db = p->db;
99625 sqlite3_mutex_enter(db->mutex);
99627 sqlite3_mutex_leave(db->mutex);
99651 db = p->db;
99652 sqlite3_mutex_enter(db->mutex);
99653 v = (Vdbe*)p->pStmt;
99655 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
99659 /* If there is no statement handle, then the blob-handle has
99665 ** returned, clean-up the statement handle.
99667 assert( db == v->db );
99668 sqlite3BtreeEnterCursor(p->pCsr);
99671 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
99672 /* If a pre-update hook is registered and this is a write cursor,
99675 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
99682 ** using the incremental-blob API, this works. For the sessions module
99686 iKey = sqlite3BtreeIntegerKey(p->pCsr);
99687 assert( v->apCsr[0]!=0 );
99688 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
99690 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
99695 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
99696 sqlite3BtreeLeaveCursor(p->pCsr);
99699 p->pStmt = 0;
99701 v->rc = rc;
99706 sqlite3_mutex_leave(db->mutex);
99732 return (p && p->pStmt) ? p->nByte : 0;
99751 db = p->db;
99752 sqlite3_mutex_enter(db->mutex);
99754 if( p->pStmt==0 ){
99755 /* If there is no statement handle, then the blob-handle has
99761 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
99771 assert( rc==SQLITE_OK || p->pStmt==0 );
99772 sqlite3_mutex_leave(db->mutex);
99781 ** 2011-07-09
99796 ** The VdbeSorter object implements a multi-threaded external merge sort
99800 ** Here is the (internal, non-API) interface between this module and the
99853 ** an in-memory merge sort. In this case, no temporary files are required
99860 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
99861 ** of PMAs may be created by merging existing PMAs together - for example
99862 ** merging two or more level-0 PMAs together creates a level-1 PMA.
99866 ** page-cache of the main database. Specifically, the threshold is set to
99870 ** If the sorter is running in single-threaded mode, then all PMAs generated
99872 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
99879 ** The sorter is running in multi-threaded mode if (a) the library was built
99880 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
99889 ** sorter is running in single-threaded mode, then these PMAs are merged
99894 ** Or, if running in multi-threaded mode, then a background thread is
99904 ** Rewind() is called, then a hierarchy of incremental-merges is used.
99910 ** If running in multi-threaded mode and there are more than
99923 ** characteristics of the sorter in multi-threaded mode.
99930 ** Hard-coded maximum amount of data to accumulate in memory before flushing
99943 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
99945 typedef struct SorterList SorterList; /* In-memory list of records */
99958 ** An in-memory list of objects to be sorted.
99967 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
99990 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
100004 ** aReadr[0] -> Banana
100005 ** aReadr[1] -> Feijoa
100006 ** aReadr[2] -> Elderberry
100007 ** aReadr[3] -> Currant
100008 ** aReadr[4] -> Grapefruit
100009 ** aReadr[5] -> Apple
100010 ** aReadr[6] -> Durian
100011 ** aReadr[7] -> EOF
100020 ** aReadr[5] -> Eggplant
100025 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
100048 ** single-threaded operation, there is exactly one instance of this object
100049 ** and for multi-threaded operation there are two or more instances.
100060 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
100077 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
100082 SorterFile file; /* Temp file for level-0 PMAs */
100106 SorterList list; /* List of in-memory records */
100149 ** There are two types of IncrMerger object - single (bUseThread==0) and
100150 ** multi-threaded (bUseThread==1).
100152 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
100167 ** A single-threaded IncrMerger does not open any temporary files of its
100169 ** at offset iStartOff of file pTask->file2. And instead of using a
100171 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
100189 ** size. But I/O is more efficient if it occurs in page-sized blocks where
100191 ** the PMA so that aligned, page-size blocks are written.
100194 int eFWErr; /* Non-zero if in an error state */
100208 ** by this module. If using a separate allocation for each in-memory record
100216 ** has finished passing records to the sorter, or when the in-memory buffer
100249 sqlite3_free(pReadr->aAlloc);
100250 sqlite3_free(pReadr->aBuffer);
100251 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
100252 vdbeIncrFree(pReadr->pIncr);
100273 if( p->aMap ){
100274 *ppOut = &p->aMap[p->iReadOff];
100275 p->iReadOff += nByte;
100279 assert( p->aBuffer );
100282 ** p->nBuffer bytes of data from the file into it. Or, if there are less
100283 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
100284 iBuf = p->iReadOff % p->nBuffer;
100290 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
100291 nRead = p->nBuffer;
100293 nRead = (int)(p->iEof - p->iReadOff);
100298 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
100302 nAvail = p->nBuffer - iBuf;
100305 /* The requested data is available in the in-memory buffer. In this
100308 *ppOut = &p->aBuffer[iBuf];
100309 p->iReadOff += nByte;
100311 /* The requested data is not all available in the in-memory buffer.
100312 ** In this case, allocate space at p->aAlloc[] to copy the requested
100313 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
100316 /* Extend the p->aAlloc[] allocation if required. */
100317 if( p->nAlloc<nByte ){
100319 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
100321 aNew = sqlite3Realloc(p->aAlloc, nNew);
100323 p->nAlloc = nNew;
100324 p->aAlloc = aNew;
100328 ** p->aAlloc[]. */
100329 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
100330 p->iReadOff += nAvail;
100331 nRem = nByte - nAvail;
100333 /* The following loop copies up to p->nBuffer bytes per iteration into
100334 ** the p->aAlloc[] buffer. */
100341 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
100344 assert( aNext!=p->aAlloc );
100345 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
100346 nRem -= nCopy;
100349 *ppOut = p->aAlloc;
100362 if( p->aMap ){
100363 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
100365 iBuf = p->iReadOff % p->nBuffer;
100366 if( iBuf && (p->nBuffer-iBuf)>=9 ){
100367 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
100394 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
100395 sqlite3_file *pFd = pFile->pFd;
100396 if( pFd->pMethods->iVersion>=3 ){
100397 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
100417 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
100420 if( pReadr->aMap ){
100421 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
100422 pReadr->aMap = 0;
100424 pReadr->iReadOff = iOff;
100425 pReadr->iEof = pFile->iEof;
100426 pReadr->pFd = pFile->pFd;
100428 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
100429 if( rc==SQLITE_OK && pReadr->aMap==0 ){
100430 int pgsz = pTask->pSorter->pgsz;
100431 int iBuf = pReadr->iReadOff % pgsz;
100432 if( pReadr->aBuffer==0 ){
100433 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
100434 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
100435 pReadr->nBuffer = pgsz;
100438 int nRead = pgsz - iBuf;
100439 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
100440 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
100443 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
100461 if( pReadr->iReadOff>=pReadr->iEof ){
100462 IncrMerger *pIncr = pReadr->pIncr;
100466 if( rc==SQLITE_OK && pIncr->bEof==0 ){
100468 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
100486 pReadr->nKey = (int)nRec;
100487 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
100496 ** starting at offset iStart and ending at offset iEof-1. This function
100512 assert( pFile->iEof>iStart );
100513 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
100514 assert( pReadr->aBuffer==0 );
100515 assert( pReadr->aMap==0 );
100521 pReadr->iEof = pReadr->iReadOff + nByte;
100538 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100542 UnpackedRecord *r2 = pTask->pUnpacked;
100544 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
100552 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
100556 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
100557 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
100560 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
100565 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100569 UnpackedRecord *r2 = pTask->pUnpacked;
100571 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
100584 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100599 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
100601 res = n1 - n2;
100605 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
100611 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
100612 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
100613 res = res * -1;
100626 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
100648 if( (res = v1[i] - v2[i])!=0 ){
100650 res = v1[0] & 0x80 ? -1 : +1;
100656 res = s1 - s2;
100661 res = -1;
100663 res = s1 - s2;
100668 if( *v1 & 0x80 ) res = -1;
100675 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
100680 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
100681 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
100682 res = res * -1;
100691 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
100694 ** is non-zero and the sorter is able to guarantee a stable sort, nField
100702 ** The sorter can guarantee a stable sort when running in single-threaded
100703 ** mode, but not in multi-threaded mode.
100715 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
100716 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
100730 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
100738 nWorker = SORTER_MAX_MERGE_COUNT-1;
100742 assert( pCsr->pKeyInfo );
100743 assert( !pCsr->isEphemeral );
100744 assert( pCsr->eCurType==CURTYPE_SORTER );
100745 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
100749 pCsr->uc.pSorter = pSorter;
100753 Btree *pBt = db->aDb[0].pBt;
100754 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
100755 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
100756 pKeyInfo->db = 0;
100758 pKeyInfo->nKeyField = nField;
100761 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
100763 pSorter->nTask = nWorker + 1;
100764 pSorter->iPrev = (u8)(nWorker - 1);
100765 pSorter->bUseThreads = (pSorter->nTask>1);
100766 pSorter->db = db;
100767 for(i=0; i<pSorter->nTask; i++){
100768 SortSubtask *pTask = &pSorter->aTask[i];
100769 pTask->pSorter = pSorter;
100775 pSorter->mnPmaSize = szPma * pgsz;
100777 mxCache = db->aDb[0].pSchema->cache_size;
100779 /* A negative cache-size value C indicates that the cache is abs(C)
100781 mxCache = mxCache * -1024;
100786 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
100791 assert( pSorter->iMemory==0 );
100792 pSorter->nMemory = pgsz;
100793 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
100794 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
100798 if( pKeyInfo->nAllField<13
100799 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
100800 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
100802 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
100817 pNext = p->u.pNext;
100827 sqlite3DbFree(db, pTask->pUnpacked);
100829 /* pTask->list.aMemory can only be non-zero if it was handed memory
100831 if( pTask->list.aMemory ){
100832 sqlite3_free(pTask->list.aMemory);
100836 assert( pTask->list.aMemory==0 );
100837 vdbeSorterRecordFree(0, pTask->list.pList);
100839 if( pTask->file.pFd ){
100840 sqlite3OsCloseFree(pTask->file.pFd);
100842 if( pTask->file2.pFd ){
100843 sqlite3OsCloseFree(pTask->file2.pFd);
100851 int iTask = (pTask - pTask->pSorter->aTask);
100852 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
100866 int iTask = (pTask - pTask->pSorter->aTask);
100867 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
100877 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
100890 ** Join thread pTask->thread.
100894 if( pTask->pThread ){
100896 int bDone = pTask->bDone;
100900 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
100903 assert( pTask->bDone==1 );
100904 pTask->bDone = 0;
100905 pTask->pThread = 0;
100918 assert( pTask->pThread==0 && pTask->bDone==0 );
100919 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
100924 ** level-0 PMAs.
100933 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
100936 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
100937 for(i=pSorter->nTask-1; i>=0; i--){
100938 SortSubtask *pTask = &pSorter->aTask[i];
100968 pNew->nTree = N;
100969 pNew->pTask = 0;
100970 pNew->aReadr = (PmaReader*)&pNew[1];
100971 pNew->aTree = (int*)&pNew->aReadr[N];
100982 for(i=0; i<pMerger->nTree; i++){
100983 vdbePmaReaderClear(&pMerger->aReadr[i]);
100996 if( pIncr->bUseThread ){
100997 vdbeSorterJoinThread(pIncr->pTask);
100998 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
100999 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
101002 vdbeMergeEngineFree(pIncr->pMerger);
101013 assert( pSorter->bUseThreads || pSorter->pReader==0 );
101015 if( pSorter->pReader ){
101016 vdbePmaReaderClear(pSorter->pReader);
101017 sqlite3DbFree(db, pSorter->pReader);
101018 pSorter->pReader = 0;
101021 vdbeMergeEngineFree(pSorter->pMerger);
101022 pSorter->pMerger = 0;
101023 for(i=0; i<pSorter->nTask; i++){
101024 SortSubtask *pTask = &pSorter->aTask[i];
101026 pTask->pSorter = pSorter;
101028 if( pSorter->list.aMemory==0 ){
101029 vdbeSorterRecordFree(0, pSorter->list.pList);
101031 pSorter->list.pList = 0;
101032 pSorter->list.szPMA = 0;
101033 pSorter->bUsePMA = 0;
101034 pSorter->iMemory = 0;
101035 pSorter->mxKeysize = 0;
101036 sqlite3DbFree(db, pSorter->pUnpacked);
101037 pSorter->pUnpacked = 0;
101045 assert( pCsr->eCurType==CURTYPE_SORTER );
101046 pSorter = pCsr->uc.pSorter;
101049 sqlite3_free(pSorter->list.aMemory);
101051 pCsr->uc.pSorter = 0;
101057 ** The first argument is a file-handle open on a temporary file. The file
101066 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
101080 ** Allocate space for a file-handle and open a temporary file. If successful,
101081 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
101091 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
101108 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
101112 if( pTask->pUnpacked==0 ){
101113 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
101114 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
101115 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
101116 pTask->pUnpacked->errCode = 0;
101137 res = pTask->xCompare(
101138 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
101143 pp = &p1->u.pNext;
101144 p1 = p1->u.pNext;
101151 pp = &p2->u.pNext;
101152 p2 = p2->u.pNext;
101168 if( p->typeMask==SORTER_TYPE_INTEGER ){
101170 }else if( p->typeMask==SORTER_TYPE_TEXT ){
101177 ** Sort the linked list of records headed at pTask->pList. Return
101190 p = pList->pList;
101191 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
101196 if( pList->aMemory ){
101197 if( (u8*)p==pList->aMemory ){
101200 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
101201 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
101204 pNext = p->u.pNext;
101207 p->u.pNext = 0;
101221 pList->pList = p;
101223 assert( pTask->pUnpacked->errCode==SQLITE_OK
101224 || pTask->pUnpacked->errCode==SQLITE_NOMEM
101226 return pTask->pUnpacked->errCode;
101230 ** Initialize a PMA-writer object.
101239 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
101240 if( !p->aBuffer ){
101241 p->eFWErr = SQLITE_NOMEM_BKPT;
101243 p->iBufEnd = p->iBufStart = (iStart % nBuf);
101244 p->iWriteOff = iStart - p->iBufStart;
101245 p->nBuffer = nBuf;
101246 p->pFd = pFd;
101256 while( nRem>0 && p->eFWErr==0 ){
101258 if( nCopy>(p->nBuffer - p->iBufEnd) ){
101259 nCopy = p->nBuffer - p->iBufEnd;
101262 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
101263 p->iBufEnd += nCopy;
101264 if( p->iBufEnd==p->nBuffer ){
101265 p->eFWErr = sqlite3OsWrite(p->pFd,
101266 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101267 p->iWriteOff + p->iBufStart
101269 p->iBufStart = p->iBufEnd = 0;
101270 p->iWriteOff += p->nBuffer;
101272 assert( p->iBufEnd<p->nBuffer );
101274 nRem -= nCopy;
101279 ** Flush any buffered data to disk and clean up the PMA-writer object.
101280 ** The results of using the PMA-writer after this call are undefined.
101289 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
101290 p->eFWErr = sqlite3OsWrite(p->pFd,
101291 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
101292 p->iWriteOff + p->iBufStart
101295 *piEof = (p->iWriteOff + p->iBufEnd);
101296 sqlite3_free(p->aBuffer);
101297 rc = p->eFWErr;
101314 ** Write the current contents of in-memory linked-list pList to a level-0
101315 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
101323 ** * One or more records packed end-to-end in order of ascending keys.
101328 sqlite3 *db = pTask->pSorter->db;
101333 /* Set iSz to the expected size of file pTask->file after writing the PMA.
101335 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
101340 assert( pList->szPMA>0 );
101343 if( pTask->file.pFd==0 ){
101344 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
101345 assert( rc!=SQLITE_OK || pTask->file.pFd );
101346 assert( pTask->file.iEof==0 );
101347 assert( pTask->nPMA==0 );
101352 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
101364 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
101365 pTask->file.iEof);
101366 pTask->nPMA++;
101367 vdbePmaWriteVarint(&writer, pList->szPMA);
101368 for(p=pList->pList; p; p=pNext){
101369 pNext = p->u.pNext;
101370 vdbePmaWriteVarint(&writer, p->nVal);
101371 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
101372 if( pList->aMemory==0 ) sqlite3_free(p);
101374 pList->pList = p;
101375 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
101379 assert( rc!=SQLITE_OK || pList->pList==0 );
101380 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
101396 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
101397 SortSubtask *pTask = pMerger->pTask;
101400 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
101411 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
101412 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
101414 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
101417 if( pReadr1->pFd==0 ){
101419 }else if( pReadr2->pFd==0 ){
101420 iRes = -1;
101422 iRes = pTask->xCompare(pTask, &bCached,
101423 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
101429 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
101434 ** was actually called above, then pTask->pUnpacked now contains
101443 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
101444 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
101447 if( pReadr1->pFd ) bCached = 0;
101448 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
101449 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
101452 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
101455 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
101460 ** The main routine for background threads that write level-0 PMAs.
101465 assert( pTask->bDone==0 );
101466 rc = vdbeSorterListToPMA(pTask, &pTask->list);
101467 pTask->bDone = 1;
101478 pSorter->bUsePMA = 1;
101479 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
101484 int nWorker = (pSorter->nTask-1);
101488 pSorter->bUsePMA = 1;
101490 /* Select a sub-task to sort and flush the current list of in-memory
101491 ** records to disk. If the sorter is running in multi-threaded mode,
101492 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
101493 ** the background thread from a sub-tasks previous turn is still running,
101494 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
101495 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
101496 ** sub-tasks are prefered as they use background threads - the final
101497 ** sub-task uses the main thread. */
101499 int iTest = (pSorter->iPrev + i + 1) % nWorker;
101500 pTask = &pSorter->aTask[iTest];
101501 if( pTask->bDone ){
101504 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
101510 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
101517 assert( pTask->pThread==0 && pTask->bDone==0 );
101518 assert( pTask->list.pList==0 );
101519 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
101521 aMem = pTask->list.aMemory;
101523 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
101524 pTask->list = pSorter->list;
101525 pSorter->list.pList = 0;
101526 pSorter->list.szPMA = 0;
101528 pSorter->list.aMemory = aMem;
101529 pSorter->nMemory = sqlite3MallocSize(aMem);
101530 }else if( pSorter->list.aMemory ){
101531 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
101532 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
101558 assert( pCsr->eCurType==CURTYPE_SORTER );
101559 pSorter = pCsr->uc.pSorter;
101560 getVarint32NR((const u8*)&pVal->z[1], t);
101562 pSorter->typeMask &= SORTER_TYPE_INTEGER;
101564 pSorter->typeMask &= SORTER_TYPE_TEXT;
101566 pSorter->typeMask = 0;
101574 ** If using the single large allocation mode (pSorter->aMemory!=0), then
101581 ** * The total memory allocated for the in-memory list is greater
101582 ** than (page-size * cache-size), or
101584 ** * The total memory allocated for the in-memory list is greater
101585 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
101587 nReq = pVal->n + sizeof(SorterRecord);
101588 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
101589 if( pSorter->mxPmaSize ){
101590 if( pSorter->list.aMemory ){
101591 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
101594 (pSorter->list.szPMA > pSorter->mxPmaSize)
101595 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
101600 pSorter->list.szPMA = 0;
101601 pSorter->iMemory = 0;
101602 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
101606 pSorter->list.szPMA += nPMA;
101607 if( nPMA>pSorter->mxKeysize ){
101608 pSorter->mxKeysize = nPMA;
101611 if( pSorter->list.aMemory ){
101612 int nMin = pSorter->iMemory + nReq;
101614 if( nMin>pSorter->nMemory ){
101616 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
101617 int iListOff = -1;
101618 if( pSorter->list.pList ){
101619 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
101622 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
101624 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
101627 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
101629 pSorter->list.aMemory = aNew;
101630 pSorter->nMemory = nNew;
101633 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
101634 pSorter->iMemory += ROUND8(nReq);
101635 if( pSorter->list.pList ){
101636 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
101643 pNew->u.pNext = pSorter->list.pList;
101646 memcpy(SRVAL(pNew), pVal->z, pVal->n);
101647 pNew->nVal = pVal->n;
101648 pSorter->list.pList = pNew;
101654 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
101656 ** except that the number-of-bytes varint is omitted from the start.
101661 i64 iStart = pIncr->iStartOff;
101662 SorterFile *pOut = &pIncr->aFile[1];
101663 SortSubtask *pTask = pIncr->pTask;
101664 MergeEngine *pMerger = pIncr->pMerger;
101666 assert( pIncr->bEof==0 );
101670 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
101673 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
101674 int nKey = pReader->nKey;
101679 if( pReader->pFd==0 ) break;
101680 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
101684 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
101685 assert( pIncr->pMerger->pTask==pTask );
101686 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
101689 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
101698 ** multi-threaded IncrMerger objects.
101703 pIncr->pTask->bDone = 1;
101712 assert( pIncr->bUseThread );
101713 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
101723 ** For single-threaded objects, this is accomplished by literally reading
101724 ** keys from pIncr->pMerger and repopulating aFile[0].
101726 ** For multi-threaded objects, all that is required is to wait until the
101738 if( pIncr->bUseThread ){
101739 rc = vdbeSorterJoinThread(pIncr->pTask);
101742 SorterFile f0 = pIncr->aFile[0];
101743 pIncr->aFile[0] = pIncr->aFile[1];
101744 pIncr->aFile[1] = f0;
101748 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
101749 pIncr->bEof = 1;
101758 pIncr->aFile[0] = pIncr->aFile[1];
101759 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
101760 pIncr->bEof = 1;
101782 pIncr->pMerger = pMerger;
101783 pIncr->pTask = pTask;
101784 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
101785 pTask->file2.iEof += pIncr->mxSz;
101796 ** Set the "use-threads" flag on object pIncr.
101799 pIncr->bUseThread = 1;
101800 pIncr->pTask->file2.iEof -= pIncr->mxSz;
101807 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
101813 int iOut /* Store the result in pMerger->aTree[iOut] */
101821 assert( iOut<pMerger->nTree && iOut>0 );
101823 if( iOut>=(pMerger->nTree/2) ){
101824 i1 = (iOut - pMerger->nTree/2) * 2;
101827 i1 = pMerger->aTree[iOut*2];
101828 i2 = pMerger->aTree[iOut*2+1];
101831 p1 = &pMerger->aReadr[i1];
101832 p2 = &pMerger->aReadr[i2];
101834 if( p1->pFd==0 ){
101836 }else if( p2->pFd==0 ){
101839 SortSubtask *pTask = pMerger->pTask;
101842 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
101843 res = pTask->xCompare(
101844 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
101853 pMerger->aTree[iOut] = iRes;
101860 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
101906 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
101910 assert( pMerger->pTask==0 );
101911 pMerger->pTask = pTask;
101913 nTree = pMerger->nTree;
101918 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
101921 ** on this PmaReader before any of the multi-threaded PmaReaders takes
101922 ** better advantage of multi-processor hardware. */
101923 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
101925 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
101930 for(i=pMerger->nTree-1; i>0; i--){
101933 return pTask->pUnpacked->errCode;
101938 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
101940 ** object at (pReadr->pIncr).
101943 ** in the sub-tree headed by pReadr are also initialized. Data is then
101948 ** to be a multi-threaded PmaReader and this function is being called in a
101949 ** background thread. In this case all PmaReaders in the sub-tree are
101957 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
101958 ** this entire function is being run by thread (pTask->thread), that will
101962 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
101963 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
101971 IncrMerger *pIncr = pReadr->pIncr;
101972 SortSubtask *pTask = pIncr->pTask;
101973 sqlite3 *db = pTask->pSorter->db;
101975 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
101978 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
101980 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
101981 ** requires two temp files to itself, whereas a single-threaded object
101982 ** only requires a region of pTask->file2. */
101984 int mxSz = pIncr->mxSz;
101986 if( pIncr->bUseThread ){
101987 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
101989 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
101993 /*if( !pIncr->bUseThread )*/{
101994 if( pTask->file2.pFd==0 ){
101995 assert( pTask->file2.iEof>0 );
101996 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
101997 pTask->file2.iEof = 0;
102000 pIncr->aFile[1].pFd = pTask->file2.pFd;
102001 pIncr->iStartOff = pTask->file2.iEof;
102002 pTask->file2.iEof += mxSz;
102008 if( rc==SQLITE_OK && pIncr->bUseThread ){
102010 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
102012 ** pIncr->pTask->thread.
102041 pReader->pIncr->pTask->bDone = 1;
102047 ** If the PmaReader passed as the first argument is not an incremental-reader
102048 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
102052 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
102058 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
102062 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
102063 if( pIncr->bUseThread ){
102065 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
102076 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
102077 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
102082 ** first PMA to read from pTask->file. Assuming no error occurs, it is
102090 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
102091 MergeEngine **ppOut /* OUT: New merge-engine */
102103 PmaReader *pReadr = &pNew->aReadr[i];
102104 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
102105 iOff = pReadr->iEof;
102122 ** nPMA<=16 -> TreeDepth() == 0
102123 ** nPMA<=256 -> TreeDepth() == 1
102124 ** nPMA<=65536 -> TreeDepth() == 2
102137 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
102165 PmaReader *pReadr = &p->aReadr[iIter];
102167 if( pReadr->pIncr==0 ){
102172 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
102176 p = pReadr->pIncr->pMerger;
102182 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
102191 ** that has already written two or more level-0 PMAs to one or more temp
102209 /* If the sorter uses more than one task, then create the top-level
102211 ** one PmaReader per sub-task. */
102212 assert( pSorter->bUseThreads || pSorter->nTask==1 );
102213 if( pSorter->nTask>1 ){
102214 pMain = vdbeMergeEngineNew(pSorter->nTask);
102219 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102220 SortSubtask *pTask = &pSorter->aTask[iTask];
102221 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
102222 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
102224 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
102227 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
102228 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
102234 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
102235 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
102236 int nReader; /* Number of level-0 PMAs to merge */
102238 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
102249 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
102274 ** (for multi-threaded sorters) so that it can be used to iterate through
102281 SortSubtask *pTask0 = &pSorter->aTask[0];
102284 sqlite3 *db = pTask0->pSorter->db;
102287 for(i=0; i<pSorter->nTask; i++){
102288 pSorter->aTask[i].xCompare = xCompare;
102295 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
102296 if( pSorter->bUseThreads ){
102299 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
102303 pSorter->pReader = pReadr;
102307 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
102309 vdbeIncrMergerSetThreads(pReadr->pIncr);
102310 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
102312 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
102314 assert( pIncr->pTask!=pLast );
102317 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
102322 ** b) If it is using task (nTask-1), it is configured to run
102323 ** in single-threaded mode. This is important, as the
102327 PmaReader *p = &pMain->aReadr[iTask];
102328 assert( p->pIncr==0 || (
102329 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
102330 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
102344 pSorter->pMerger = pMain;
102365 assert( pCsr->eCurType==CURTYPE_SORTER );
102366 pSorter = pCsr->uc.pSorter;
102371 ** from the in-memory list. */
102372 if( pSorter->bUsePMA==0 ){
102373 if( pSorter->list.pList ){
102375 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
102382 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
102386 assert( pSorter->list.pList );
102396 assert( pSorter->pReader==0 );
102417 assert( pCsr->eCurType==CURTYPE_SORTER );
102418 pSorter = pCsr->uc.pSorter;
102419 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
102420 if( pSorter->bUsePMA ){
102421 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
102422 assert( pSorter->bUseThreads==0 || pSorter->pReader );
102423 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
102425 if( pSorter->bUseThreads ){
102426 rc = vdbePmaReaderNext(pSorter->pReader);
102427 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
102430 /*if( !pSorter->bUseThreads )*/ {
102432 assert( pSorter->pMerger!=0 );
102433 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
102434 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
102438 SorterRecord *pFree = pSorter->list.pList;
102439 pSorter->list.pList = pFree->u.pNext;
102440 pFree->u.pNext = 0;
102441 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
102442 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
102456 if( pSorter->bUsePMA ){
102459 if( pSorter->bUseThreads ){
102460 pReader = pSorter->pReader;
102463 /*if( !pSorter->bUseThreads )*/{
102464 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
102466 *pnKey = pReader->nKey;
102467 pKey = pReader->aKey;
102469 *pnKey = pSorter->list.pList->nVal;
102470 pKey = SRVAL(pSorter->list.pList);
102482 assert( pCsr->eCurType==CURTYPE_SORTER );
102483 pSorter = pCsr->uc.pSorter;
102488 pOut->n = nKey;
102490 memcpy(pOut->z, pKey, nKey);
102523 assert( pCsr->eCurType==CURTYPE_SORTER );
102524 pSorter = pCsr->uc.pSorter;
102525 r2 = pSorter->pUnpacked;
102526 pKeyInfo = pCsr->pKeyInfo;
102528 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
102530 r2->nField = nKeyCol;
102532 assert( r2->nField==nKeyCol );
102537 if( r2->aMem[i].flags & MEM_Null ){
102538 *pRes = -1;
102543 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
102550 ** 2020-03-23
102561 ** This file implements virtual-tables for examining the bytecode content
102568 /* An instance of the bytecode() table-valued function.
102572 sqlite3_vtab base; /* Base class - must be first */
102581 sqlite3_vtab_cursor base; /* Base class - must be first */
102596 ** Create a new bytecode() table-valued function.
102640 pNew->db = db;
102641 pNew->bTablesUsed = isTabUsed*2;
102664 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
102665 *ppCursor = &pCur->base;
102673 sqlite3_free(pCur->zP4);
102674 pCur->zP4 = 0;
102675 sqlite3VdbeMemRelease(&pCur->sub);
102676 sqlite3VdbeMemSetNull(&pCur->sub);
102677 if( pCur->needFinalize ){
102678 sqlite3_finalize(pCur->pStmt);
102680 pCur->pStmt = 0;
102681 pCur->needFinalize = 0;
102682 pCur->zType = 0;
102683 pCur->zSchema = 0;
102684 pCur->zName = 0;
102703 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
102705 if( pCur->zP4 ){
102706 sqlite3_free(pCur->zP4);
102707 pCur->zP4 = 0;
102709 if( pCur->zName ){
102710 pCur->zName = 0;
102711 pCur->zType = 0;
102712 pCur->zSchema = 0;
102715 (Vdbe*)pCur->pStmt,
102716 pCur->showSubprograms ? &pCur->sub : 0,
102717 pTab->bTablesUsed,
102718 &pCur->iRowid,
102719 &pCur->iAddr,
102720 &pCur->aOp);
102722 sqlite3VdbeMemSetNull(&pCur->sub);
102723 pCur->aOp = 0;
102734 return pCur->aOp==0;
102747 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
102748 Op *pOp = pCur->aOp + pCur->iAddr;
102749 if( pVTab->bTablesUsed ){
102753 if( i<=2 && pCur->zType==0 ){
102756 int iDb = pOp->p3;
102757 Pgno iRoot = (Pgno)pOp->p2;
102758 sqlite3 *db = pVTab->db;
102759 pSchema = db->aDb[iDb].pSchema;
102760 pCur->zSchema = db->aDb[iDb].zDbSName;
102761 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
102763 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
102764 pCur->zName = pTab->zName;
102765 pCur->zType = "table";
102769 if( pCur->zName==0 ){
102770 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
102772 if( pIdx->tnum==iRoot ){
102773 pCur->zName = pIdx->zName;
102774 pCur->zType = "index";
102784 sqlite3_result_int(ctx, pCur->iAddr);
102787 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
102788 -1, SQLITE_STATIC);
102791 sqlite3_result_int(ctx, pOp->p1);
102794 sqlite3_result_int(ctx, pOp->p2);
102797 sqlite3_result_int(ctx, pOp->p3);
102801 if( pCur->zP4==0 ){
102802 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
102805 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
102808 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
102809 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
102814 sqlite3_result_int(ctx, pOp->p5);
102817 Op *aOp = pCur->aOp;
102819 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
102820 if( pCur->iRowid==pCur->iAddr+1 ){
102823 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
102830 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
102833 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
102836 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
102839 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
102851 *pRowid = pCur->iRowid;
102867 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
102871 pCur->iRowid = 0;
102872 pCur->iAddr = 0;
102873 pCur->showSubprograms = idxNum==0;
102880 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
102881 pCur->needFinalize = 1;
102884 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
102886 if( pCur->pStmt==0 ){
102887 pVTab->base.zErrMsg = sqlite3_mprintf(
102889 pVTab->bTablesUsed ? "tables_used" : "bytecode"
102911 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
102912 pIdxInfo->estimatedCost = (double)100;
102913 pIdxInfo->estimatedRows = 100;
102914 pIdxInfo->idxNum = 0;
102915 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
102916 if( p->usable==0 ) continue;
102917 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
102919 pIdxInfo->aConstraintUsage[i].omit = 1;
102920 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
102922 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
102923 pIdxInfo->aConstraintUsage[i].omit = 1;
102924 pIdxInfo->idxNum = 1;
102988 ** This file contains code use to implement an in-memory rollback journal.
102989 ** The in-memory rollback journal is used to journal transactions for
102992 ** Update: The in-memory journal is also used to temporarily cache
102993 ** smaller journals that are not critical for power-loss recovery.
103011 ** The zChunk array is always at least 8 bytes in size - usually much more.
103028 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
103040 ** This structure is a subclass of sqlite3_file. Each open memory-journal
103045 int nChunkSize; /* In-memory chunk-size */
103048 FileChunk *pFirst; /* Head of in-memory chunk-list */
103058 ** Read data from the in-memory journal file. This is the implementation
103073 if( (iAmt+iOfst)>p->endpoint.iOffset ){
103076 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
103077 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
103079 for(pChunk=p->pFirst;
103080 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
103081 pChunk=pChunk->pNext
103083 iOff += p->nChunkSize;
103086 pChunk = p->readpoint.pChunk;
103090 iChunkOffset = (int)(iOfst%p->nChunkSize);
103092 int iSpace = p->nChunkSize - iChunkOffset;
103093 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
103094 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
103096 nRead -= iSpace;
103098 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
103099 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
103100 p->readpoint.pChunk = pChunk;
103112 pNext = pIter->pNext;
103131 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
103133 nChunk = copy.endpoint.iOffset - iOff;
103135 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
103140 /* No error has occurred. Free the in-memory buffers. */
103146 ** the original before returning. This way, SQLite uses the in-memory
103147 ** journal data to roll back changes made to the internal page-cache
103174 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
103184 /* An in-memory journal file should only ever be appended to. Random
103186 ** the in-memory journal is being used by a connection using the
103187 ** atomic-write optimization. In this case the first 28 bytes of the
103189 assert( iOfst<=p->endpoint.iOffset );
103190 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
103193 if( iOfst==0 && p->pFirst ){
103194 assert( p->nChunkSize>iAmt );
103195 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
103198 FileChunk *pChunk = p->endpoint.pChunk;
103199 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
103200 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
103205 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
103209 pNew->pNext = 0;
103211 assert( p->pFirst );
103212 pChunk->pNext = pNew;
103214 assert( !p->pFirst );
103215 p->pFirst = pNew;
103217 pChunk = p->endpoint.pChunk = pNew;
103221 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
103223 nWrite -= iSpace;
103224 p->endpoint.iOffset += iSpace;
103233 ** Truncate the in-memory file.
103237 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
103238 if( size<p->endpoint.iOffset ){
103241 memjrnlFreeChunks(p->pFirst);
103242 p->pFirst = 0;
103244 i64 iOff = p->nChunkSize;
103245 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
103246 iOff += p->nChunkSize;
103249 memjrnlFreeChunks(pIter->pNext);
103250 pIter->pNext = 0;
103254 p->endpoint.pChunk = pIter;
103255 p->endpoint.iOffset = size;
103256 p->readpoint.pChunk = 0;
103257 p->readpoint.iOffset = 0;
103267 memjrnlFreeChunks(p->pFirst);
103275 ** syncing an in-memory journal is a no-op.
103287 *pSize = (sqlite_int64) p->endpoint.iOffset;
103322 ** all content is always stored in main-memory. Finally, if nSpill is a
103323 ** positive value, then the journal file is initially created in-memory
103339 /* Zero the file-handle object. If nSpill was passed zero, initialize
103342 ** made on the journal file-handle. */
103349 p->nChunkSize = nSpill;
103351 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
103352 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
103355 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
103356 p->nSpill = nSpill;
103357 p->flags = flags;
103358 p->zJournal = zName;
103359 p->pVfs = pVfs;
103364 ** Open an in-memory journal file.
103367 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
103374 ** in-memory-only journal file (i.e. is one that was opened with a +ve
103381 if( pJfd->pMethods==&MemJournalMethods && (
103383 p->nSpill>0
103388 NEVER(p->nSpill>0)
103391 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
103401 ** The file-handle passed as the only argument is open on a journal file.
103406 return p->pMethods==&MemJournalMethods;
103411 ** pVfs to create the underlying on-disk files.
103414 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
103445 for(pWin=pList; pWin; pWin=pWin->pNextWin){
103447 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
103449 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
103451 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
103453 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
103455 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
103477 ** return from the top-level walk call.
103487 rc = pWalker->xExprCallback(pWalker, pExpr);
103490 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
103491 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
103492 if( pExpr->pRight ){
103494 pExpr = pExpr->pRight;
103498 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
103500 if( pExpr->x.pList ){
103501 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
103505 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
103526 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
103527 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
103534 ** This is a no-op callback for Walker->xSelectCallback2. If this
103535 ** callback is set, then the Select->pWinDefn list is traversed.
103540 /* No-op */
103550 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
103551 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
103552 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
103553 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
103554 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
103555 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
103557 if( p->pWinDefn ){
103559 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
103560 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
103562 || pWalker->xSelectCallback2==sqlite3SelectPopWith
103567 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
103587 pSrc = p->pSrc;
103589 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
103590 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
103593 if( pItem->fg.isTabFunc
103594 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
103606 ** on the compound select chain, p->pPrior.
103611 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
103618 ** is a no-op returning WRC_Continue.
103623 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
103625 rc = pWalker->xSelectCallback(pWalker, p);
103632 if( pWalker->xSelectCallback2 ){
103633 pWalker->xSelectCallback2(pWalker, p);
103635 p = p->pPrior;
103645 pWalker->walkerDepth++;
103650 pWalker->walkerDepth--;
103655 ** No-op routine for the parse-tree walker.
103669 ** No-op routine for the parse-tree walker for SELECT statements.
103709 ** is a helper function - a callback for the tree walker.
103714 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
103728 ** Turn the pExpr expression into an alias for the iCol-th column of the
103749 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
103753 Expr *pOrig; /* The iCol-th column of the result set */
103757 assert( iCol>=0 && iCol<pEList->nExpr );
103758 pOrig = pEList->a[iCol].pExpr;
103760 db = pParse->db;
103762 if( db->mallocFailed ){
103768 if( pExpr->op==TK_COLLATE ){
103770 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
103776 if( ALWAYS(pExpr->y.pWin!=0) ){
103777 pExpr->y.pWin->pOwner = pExpr;
103799 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
103800 zSpan = pItem->zEName;
103818 ** Return TRUE if the double-quoted string mis-feature should be supported.
103821 if( db->init.busy ) return 1; /* Always support for legacy schemas */
103822 if( pTopNC->ncFlags & NC_IsDDL ){
103824 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
103827 return (db->flags & SQLITE_DqsDDL)!=0;
103830 return (db->flags & SQLITE_DqsDML)!=0;
103835 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
103842 n = pExpr->iColumn;
103844 pExTab = pExpr->y.pTab;
103846 if( (pExTab->tabFlags & TF_HasGenerated)!=0
103847 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
103849 testcase( pExTab->nCol==BMS-1 );
103850 testcase( pExTab->nCol==BMS );
103851 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
103853 testcase( n==BMS-1 );
103855 if( n>=BMS ) n = BMS-1;
103872 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
103874 pNew->iTable = pMatch->iCursor;
103875 pNew->iColumn = iColumn;
103876 pNew->y.pTab = pMatch->pTab;
103877 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
103889 ** pExpr->iDb Set the index in db->aDb[] of the database X
103891 ** pExpr->iTable Set to the cursor number for the table obtained
103893 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
103895 ** pExpr->iColumn Set to the column number within the table.
103896 ** pExpr->op Set to TK_COLUMN.
103897 ** pExpr->pLeft Any expression this points to is deleted
103898 ** pExpr->pRight Any expression this points to is deleted.
103922 sqlite3 *db = pParse->db; /* The database connection */
103927 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
103937 /* Initialize the node to no-match */
103938 pExpr->iTable = -1;
103946 testcase( pNC->ncFlags & NC_PartIdx );
103947 testcase( pNC->ncFlags & NC_IsCheck );
103948 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
103955 for(i=0; i<db->nDb; i++){
103956 assert( db->aDb[i].zDbSName );
103957 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
103958 pSchema = db->aDb[i].pSchema;
103962 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
103965 pSchema = db->aDb[0].pSchema;
103966 zDb = db->aDb[0].zDbSName;
103971 /* Start at the inner-most context and move outward until a match is found */
103975 SrcList *pSrcList = pNC->pSrcList;
103978 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
103980 pTab = pItem->pTab;
103981 assert( pTab!=0 && pTab->zName!=0 );
103982 assert( pTab->nCol>0 || pParse->nErr );
103983 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
103984 if( pItem->fg.isNestedFrom ){
103989 ** This pItem -------------^
103992 assert( pItem->pSelect!=0 );
103993 pEList = pItem->pSelect->pEList;
103995 assert( pEList->nExpr==pTab->nCol );
103996 for(j=0; j<pEList->nExpr; j++){
103997 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
104001 if( pItem->fg.isUsing==0
104002 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104010 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104011 /* An INNER or LEFT JOIN. Use the left-most table */
104014 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104015 /* A RIGHT JOIN. Use the right-most table */
104021 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104027 pExpr->iColumn = j;
104028 pEList->a[j].fg.bUsed = 1;
104030 if( pEList->a[j].fg.bUsingTerm ) break;
104038 if( pTab->pSchema!=pSchema ) continue;
104041 zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
104047 if( IN_RENAME_OBJECT && pItem->zAlias ){
104048 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
104052 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
104053 if( pCol->hName==hCol
104054 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104057 if( pItem->fg.isUsing==0
104058 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
104066 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
104067 /* An INNER or LEFT JOIN. Use the left-most table */
104070 if( (pItem->fg.jointype & JT_LEFT)==0 ){
104071 /* A RIGHT JOIN. Use the right-most table */
104077 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104082 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
104083 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
104084 if( pItem->fg.isNestedFrom ){
104096 pExpr->iTable = pMatch->iCursor;
104098 pExpr->y.pTab = pMatch->pTab;
104099 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
104102 pSchema = pExpr->y.pTab->pSchema;
104115 if( pParse->pTriggerTab!=0 ){
104116 int op = pParse->eTriggerOp;
104118 if( pParse->bReturning ){
104119 if( (pNC->ncFlags & NC_UBaseReg)!=0
104120 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
104122 pExpr->iTable = op!=TK_DELETE;
104123 pTab = pParse->pTriggerTab;
104126 pExpr->iTable = 1;
104127 pTab = pParse->pTriggerTab;
104129 pExpr->iTable = 0;
104130 pTab = pParse->pTriggerTab;
104135 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
104136 Upsert *pUpsert = pNC->uNC.pUpsert;
104138 pTab = pUpsert->pUpsertSrc->a[0].pTab;
104139 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
104147 pSchema = pTab->pSchema;
104149 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
104150 if( pCol->hName==hCol
104151 && sqlite3StrICmp(pCol->zCnName, zCol)==0
104153 if( iCol==pTab->iPKey ){
104154 iCol = -1;
104159 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
104160 /* IMP: R-51414-32910 */
104161 iCol = -1;
104163 if( iCol<pTab->nCol ){
104167 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
104168 testcase( iCol==(-1) );
104171 pExpr->iColumn = iCol;
104172 pExpr->y.pTab = pTab;
104175 pExpr->iTable = pNC->uNC.pUpsert->regData +
104183 pExpr->y.pTab = pTab;
104184 if( pParse->bReturning ){
104186 pExpr->op2 = TK_COLUMN;
104187 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
104190 pExpr->iColumn = (i16)iCol;
104194 pExpr->affExpr = SQLITE_AFF_INTEGER;
104195 }else if( pExpr->iTable==0 ){
104198 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104202 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
104218 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
104220 && ALWAYS(VisibleRowid(pMatch->pTab))
104223 pExpr->iColumn = -1;
104224 pExpr->affExpr = SQLITE_AFF_INTEGER;
104229 ** might refer to an result-set alias. This happens, for example, when
104239 ** The ability to use an output result-set column in the WHERE, GROUP BY,
104246 && (pNC->ncFlags & NC_UEList)!=0
104249 pEList = pNC->uNC.pEList;
104251 for(j=0; j<pEList->nExpr; j++){
104252 char *zAs = pEList->a[j].zEName;
104253 if( pEList->a[j].fg.eEName==ENAME_NAME
104257 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
104258 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
104259 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
104260 pOrig = pEList->a[j].pExpr;
104261 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
104266 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
104291 pNC = pNC->pNext;
104298 ** supplied) and the value of Z is enclosed in double-quotes, then
104303 ** Because no reference was made to outer contexts, the pNC->nRef
104307 assert( pExpr->op==TK_ID );
104311 /* If a double-quoted identifier does not match any known column name,
104315 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
104326 "double-quoted string literal: \"%w\"", zCol);
104328 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
104330 pExpr->op = TK_STRING;
104331 memset(&pExpr->y, 0, sizeof(pExpr->y));
104351 if( pFJMatch->nExpr==cnt-1 ){
104355 sqlite3ExprDelete(db, pExpr->pLeft);
104356 pExpr->pLeft = 0;
104357 sqlite3ExprDelete(db, pExpr->pRight);
104358 pExpr->pRight = 0;
104360 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
104361 pExpr->op = TK_FUNCTION;
104362 pExpr->u.zToken = "coalesce";
104363 pExpr->x.pList = pFJMatch;
104379 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104380 pParse->checkSchema = 1;
104381 pTopNC->nNcErr++;
104387 sqlite3ExprDelete(db, pExpr->pLeft);
104388 pExpr->pLeft = 0;
104389 sqlite3ExprDelete(db, pExpr->pRight);
104390 pExpr->pRight = 0;
104408 if( pExpr->iColumn>=0 && pMatch!=0 ){
104409 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
104412 pExpr->op = eNewExprOp;
104417 if( pParse->db->xAuth
104418 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
104420 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
104427 pTopNC->nRef++;
104429 pTopNC = pTopNC->pNext;
104444 SrcItem *pItem = &pSrc->a[iSrc];
104447 pTab = p->y.pTab = pItem->pTab;
104448 p->iTable = pItem->iCursor;
104449 if( p->y.pTab->iPKey==iCol ){
104450 p->iColumn = -1;
104452 p->iColumn = (ynVar)iCol;
104453 if( (pTab->tabFlags & TF_HasGenerated)!=0
104454 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
104456 testcase( pTab->nCol==63 );
104457 testcase( pTab->nCol==64 );
104458 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
104461 testcase( iCol==BMS-1 );
104462 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
104471 ** pNC->ncFlags values determined by validMask.
104493 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
104495 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
104498 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
104501 if( pExpr ) pExpr->op = TK_NULL;
104502 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
104506 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
104510 ** Return 1024 times this value. Or return -1 if p is not a floating point
104514 double r = -1.0;
104515 if( p->op!=TK_FLOAT ) return -1;
104517 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
104519 if( r>1.0 ) return -1;
104538 pNC = pWalker->u.pNC;
104540 pParse = pNC->pParse;
104541 assert( pParse==pWalker->pParse );
104544 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
104545 SrcList *pSrcList = pNC->pSrcList;
104547 for(i=0; i<pNC->pSrcList->nSrc; i++){
104548 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
104552 switch( pExpr->op ){
104560 SrcList *pSrcList = pNC->pSrcList;
104562 assert( pSrcList && pSrcList->nSrc>=1 );
104563 pItem = pSrcList->a;
104564 pExpr->op = TK_COLUMN;
104566 pExpr->y.pTab = pItem->pTab;
104567 pExpr->iTable = pItem->iCursor;
104568 pExpr->iColumn--;
104569 pExpr->affExpr = SQLITE_AFF_INTEGER;
104575 ** "expr IS NOT NULL" --> "TRUE"
104576 ** "expr IS NULL" --> "FALSE"
104581 ** If this optimization occurs, also restore the NameContext ref-counts
104592 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
104593 anRef[i] = p->nRef;
104595 sqlite3WalkExpr(pWalker, pExpr->pLeft);
104596 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
104599 if( pExpr->op==TK_NOTNULL ){
104600 pExpr->u.zToken = "true";
104603 pExpr->u.zToken = "false";
104606 pExpr->op = TK_TRUEFALSE;
104607 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
104608 p->nRef = anRef[i];
104610 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
104611 pExpr->pLeft = 0;
104621 ** be one call to lookupName(). Then the compiler will in-line
104631 if( pExpr->op==TK_ID ){
104635 zColumn = pExpr->u.zToken;
104637 Expr *pLeft = pExpr->pLeft;
104638 testcase( pNC->ncFlags & NC_IdxExpr );
104639 testcase( pNC->ncFlags & NC_GenCol );
104642 pRight = pExpr->pRight;
104643 if( pRight->op==TK_ID ){
104646 assert( pRight->op==TK_DOT );
104648 zDb = pLeft->u.zToken;
104649 pLeft = pRight->pLeft;
104650 pRight = pRight->pRight;
104653 zTable = pLeft->u.zToken;
104654 zColumn = pRight->u.zToken;
104658 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
104667 ExprList *pList = pExpr->x.pList; /* The argument list */
104668 int n = pList ? pList->nExpr : 0; /* Number of arguments */
104674 u8 enc = ENC(pParse->db); /* The database encoding */
104675 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
104677 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
104680 zId = pExpr->u.zToken;
104681 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
104683 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
104690 is_agg = pDef->xFinalize!=0;
104691 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
104694 pExpr->iTable = exprProbability(pList->a[1].pExpr);
104695 if( pExpr->iTable<0 ){
104699 pNC->nNcErr++;
104702 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
104704 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
104705 ** short-hand for likelihood(X,0.0625).
104706 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
104708 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
104711 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
104716 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
104721 pNC->nNcErr++;
104723 pExpr->op = TK_NULL;
104728 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
104735 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
104736 /* Clearly non-deterministic functions like random(), but also
104742 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
104746 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
104747 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
104749 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
104750 && pParse->nested==0
104751 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
104753 /* Internal-use-only functions are disallowed unless the
104755 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
104760 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
104769 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
104770 || (pDef->xValue==0 && pDef->xInverse==0)
104771 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
104773 if( pDef && pDef->xValue==0 && pWin ){
104777 pNC->nNcErr++;
104779 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
104780 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
104781 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
104784 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
104790 pNC->nNcErr++;
104794 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
104796 pNC->nNcErr++;
104800 else if( no_such_func && pParse->db->init.busy==0
104802 && pParse->explain==0
104806 pNC->nNcErr++;
104810 pNC->nNcErr++;
104815 "FILTER may not be used with non-aggregate %#T()",
104818 pNC->nNcErr++;
104826 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
104828 pNC->ncFlags &= ~NC_AllowAgg;
104841 Select *pSel = pNC->pWinSelect;
104842 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
104844 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
104845 if( pParse->db->mallocFailed ) break;
104847 sqlite3WalkExprList(pWalker, pWin->pPartition);
104848 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
104849 sqlite3WalkExpr(pWalker, pWin->pFilter);
104851 pNC->ncFlags |= NC_HasWin;
104856 pExpr->op = TK_AGG_FUNCTION;
104857 pExpr->op2 = 0;
104860 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
104865 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
104867 pExpr->op2++;
104868 pNC2 = pNC2->pNext;
104874 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
104875 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
104876 pNC2->ncFlags |= NC_HasAgg
104877 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
104881 pNC->ncFlags |= savedAllowFlags;
104883 /* FIX ME: Compute pExpr->affinity based on the expected return
104890 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
104893 testcase( pExpr->op==TK_IN );
104895 int nRef = pNC->nRef;
104896 testcase( pNC->ncFlags & NC_IsCheck );
104897 testcase( pNC->ncFlags & NC_PartIdx );
104898 testcase( pNC->ncFlags & NC_IdxExpr );
104899 testcase( pNC->ncFlags & NC_GenCol );
104900 if( pNC->ncFlags & NC_SelfRef ){
104903 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
104905 assert( pNC->nRef>=nRef );
104906 if( nRef!=pNC->nRef ){
104908 pNC->ncFlags |= NC_VarSelect;
104914 testcase( pNC->ncFlags & NC_IsCheck );
104915 testcase( pNC->ncFlags & NC_PartIdx );
104916 testcase( pNC->ncFlags & NC_IdxExpr );
104917 testcase( pNC->ncFlags & NC_GenCol );
104924 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
104928 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
104931 if( pRight->op==TK_TRUEFALSE ){
104932 pExpr->op2 = pExpr->op;
104933 pExpr->op = TK_TRUTH;
104947 if( pParse->db->mallocFailed ) break;
104948 assert( pExpr->pLeft!=0 );
104949 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
104950 if( pExpr->op==TK_BETWEEN ){
104952 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
104954 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
104957 assert( pExpr->pRight!=0 );
104958 nRight = sqlite3ExprVectorSize(pExpr->pRight);
104961 testcase( pExpr->op==TK_EQ );
104962 testcase( pExpr->op==TK_NE );
104963 testcase( pExpr->op==TK_LT );
104964 testcase( pExpr->op==TK_LE );
104965 testcase( pExpr->op==TK_GT );
104966 testcase( pExpr->op==TK_GE );
104967 testcase( pExpr->op==TK_IS );
104968 testcase( pExpr->op==TK_ISNOT );
104969 testcase( pExpr->op==TK_BETWEEN );
104971 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104976 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
104977 return pParse->nErr ? WRC_Abort : WRC_Continue;
104984 ** to the AS-name of one of the terms of the expression list. If it is,
105001 if( pE->op==TK_ID ){
105004 zCol = pE->u.zToken;
105005 for(i=0; i<pEList->nExpr; i++){
105006 if( pEList->a[i].fg.eEName==ENAME_NAME
105007 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
105025 ** Attempt to match pE against result set columns in the left-most
105027 ** as an indication to the caller that it should sort by the i-th column.
105028 ** The left-most column is 1. In other words, the value returned is the
105032 ** If there is no match, return 0. Return -1 if an error occurs.
105044 u8 savedSuppErr; /* Saved value of db->suppressErr */
105047 pEList = pSelect->pEList;
105053 nc.pSrcList = pSelect->pSrc;
105057 db = pParse->db;
105058 savedSuppErr = db->suppressErr;
105059 db->suppressErr = 1;
105061 db->suppressErr = savedSuppErr;
105065 ** in the result set. Return an 1-based index of the matching
105066 ** result-set entry.
105068 for(i=0; i<pEList->nExpr; i++){
105069 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
105079 ** Generate an ORDER BY or GROUP BY term out-of-range error.
105084 int i, /* The index (1-based) of the term out of range */
105089 "%r %s BY term out of range - should be "
105091 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
105103 ** beginning with the left-most SELECT and working toward the right.
105119 pOrderBy = pSelect->pOrderBy;
105121 db = pParse->db;
105122 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105126 for(i=0; i<pOrderBy->nExpr; i++){
105127 pOrderBy->a[i].fg.done = 0;
105129 pSelect->pNext = 0;
105130 while( pSelect->pPrior ){
105131 pSelect->pPrior->pNext = pSelect;
105132 pSelect = pSelect->pPrior;
105137 pEList = pSelect->pEList;
105139 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105140 int iCol = -1;
105142 if( pItem->fg.done ) continue;
105143 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
105146 if( iCol<=0 || iCol>pEList->nExpr ){
105147 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
105165 if( !db->mallocFailed ){
105181 pNew->flags |= EP_IntValue;
105182 pNew->u.iValue = iCol;
105183 if( pItem->pExpr==pE ){
105184 pItem->pExpr = pNew;
105186 Expr *pParent = pItem->pExpr;
105187 assert( pParent->op==TK_COLLATE );
105188 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
105189 assert( pParent->pLeft==pE );
105190 pParent->pLeft = pNew;
105193 pItem->u.x.iOrderByCol = (u16)iCol;
105195 pItem->fg.done = 1;
105200 pSelect = pSelect->pNext;
105202 for(i=0; i<pOrderBy->nExpr; i++){
105203 if( pOrderBy->a[i].fg.done==0 ){
105220 ** return non-zero. Return zero if no errors are seen.
105229 sqlite3 *db = pParse->db;
105233 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
105234 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
105238 pEList = pSelect->pEList;
105240 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105241 if( pItem->u.x.iOrderByCol ){
105242 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
105243 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
105246 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
105259 Window *pWin = pExpr->y.pWin;
105270 if( pSelect->pWin ){
105288 ** If the order-by term is an integer I between 1 and N (where N is the
105290 ** in the resolution is a copy of the I-th result-set expression. If
105291 ** the order-by term is an identifier that corresponds to the AS-name of
105292 ** a result-set expression, then the term resolves to a copy of the
105293 ** result-set expression. Otherwise, the expression is resolved in
105294 ** the usual way - using sqlite3ResolveExprNames().
105313 nResult = pSelect->pEList->nExpr;
105314 pParse = pNC->pParse;
105315 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
105316 Expr *pE = pItem->pExpr;
105320 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
105322 /* If an AS-name match is found, mark this ORDER BY column as being
105323 ** a copy of the iCol-th result-set column. The subsequent call to
105325 ** copy of the iCol-th result-set expression. */
105326 pItem->u.x.iOrderByCol = (u16)iCol;
105333 ** order-by term to a copy of the result-set expression */
105338 pItem->u.x.iOrderByCol = (u16)iCol;
105343 pItem->u.x.iOrderByCol = 0;
105347 for(j=0; j<pSelect->pEList->nExpr; j++){
105348 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
105353 pItem->u.x.iOrderByCol = j+1;
105371 Select *pLeftmost; /* Left-most of SELECT of a compound */
105376 if( p->selFlags & SF_Resolved ){
105379 pOuterNC = pWalker->u.pNC;
105380 pParse = pWalker->pParse;
105381 db = pParse->db;
105391 if( (p->selFlags & SF_Expanded)==0 ){
105393 return pParse->nErr ? WRC_Abort : WRC_Prune;
105396 isCompound = p->pPrior!=0;
105400 assert( (p->selFlags & SF_Expanded)!=0 );
105401 assert( (p->selFlags & SF_Resolved)==0 );
105402 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
105403 p->selFlags |= SF_Resolved;
105412 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
105418 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
105419 ** as if it were part of the sub-query, not the parent. This block
105420 ** moves the pOrderBy down to the sub-query. It will be moved back
105422 if( p->selFlags & SF_Converted ){
105423 Select *pSub = p->pSrc->a[0].pSelect;
105424 assert( p->pSrc->nSrc==1 && p->pOrderBy );
105425 assert( pSub->pPrior && pSub->pOrderBy==0 );
105426 pSub->pOrderBy = p->pOrderBy;
105427 p->pOrderBy = 0;
105432 for(i=0; i<p->pSrc->nSrc; i++){
105433 SrcItem *pItem = &p->pSrc->a[i];
105434 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
105435 int nRef = pOuterNC ? pOuterNC->nRef : 0;
105436 const char *zSavedContext = pParse->zAuthContext;
105438 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
105439 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
105440 pParse->zAuthContext = zSavedContext;
105441 if( pParse->nErr ) return WRC_Abort;
105442 assert( db->mallocFailed==0 );
105445 ** expressions in the sub-select were resolved, the sub-select
105451 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
105452 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
105457 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
105458 ** resolve the result-set expression list.
105461 sNC.pSrcList = p->pSrc;
105465 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
105468 /* If there are no aggregate functions in the result-set, and no GROUP BY
105471 assert( (p->selFlags & SF_Aggregate)==0 );
105472 pGroupBy = p->pGroupBy;
105476 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
105481 /* Add the output column list to the name-context before parsing the
105487 ** re-evaluated for each reference to it.
105490 sNC.uNC.pEList = p->pEList;
105492 if( p->pHaving ){
105493 if( (p->selFlags & SF_Aggregate)==0 ){
105494 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
105497 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
105499 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
105501 /* Resolve names in table-valued-function arguments */
105502 for(i=0; i<p->pSrc->nSrc; i++){
105503 SrcItem *pItem = &p->pSrc->a[i];
105504 if( pItem->fg.isTabFunc
105505 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
105514 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
105515 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
105516 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
105531 ** the sub-query back to the parent query. At this point each term
105535 if( p->selFlags & SF_Converted ){
105536 Select *pSub = p->pSrc->a[0].pSelect;
105537 p->pOrderBy = pSub->pOrderBy;
105538 pSub->pOrderBy = 0;
105543 ** below, after all of the result-sets for all of the elements of
105546 ** If there is an ORDER BY clause on a term of a compound-select other
105547 ** than the right-most term, then that is a syntax error. But the error
105551 if( p->pOrderBy!=0
105552 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
105553 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
105557 if( db->mallocFailed ){
105568 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
105571 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
105572 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
105582 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
105583 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
105589 p = p->pPrior;
105605 ** table columns and result-set columns. At the same time, do error
105613 ** the symbolic name assigned to an ATTACH-ed database.
105624 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
105628 ** To resolve result-set references, look for expression nodes of the
105629 ** form Z (with no X and Y prefix) where the Z matches the right-hand
105630 ** size of an AS clause in the result-set of a SELECT. The Z expression
105631 ** is replaced by a copy of the left-hand side of the result-set expression.
105632 ** Table-name and function resolution occurs on the substituted expression
105659 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
105660 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
105661 w.pParse = pNC->pParse;
105663 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
105667 w.pParse->nHeight += pExpr->nHeight;
105668 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
105674 w.pParse->nHeight -= pExpr->nHeight;
105678 testcase( pNC->ncFlags & NC_HasAgg );
105679 testcase( pNC->ncFlags & NC_HasWin );
105680 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
105681 pNC->ncFlags |= savedHasAgg;
105682 return pNC->nNcErr>0 || w.pParse->nErr>0;
105698 w.pParse = pNC->pParse;
105703 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
105704 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
105705 for(i=0; i<pList->nExpr; i++){
105706 Expr *pExpr = pList->a[i].pExpr;
105709 w.pParse->nHeight += pExpr->nHeight;
105710 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
105716 w.pParse->nHeight -= pExpr->nHeight;
105720 testcase( pNC->ncFlags & NC_HasAgg );
105721 testcase( pNC->ncFlags & NC_HasWin );
105722 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
105723 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
105724 savedHasAgg |= pNC->ncFlags &
105726 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
105728 if( w.pParse->nErr>0 ) return WRC_Abort;
105730 pNC->ncFlags |= savedHasAgg;
105736 ** decendents of the SELECT, including compounds off of p->pPrior,
105767 ** ------------
105775 ** nodes of the expression is set to -1 and the Expr.iColumn value is
105787 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
105788 NameContext sNC; /* Name context for pParse->pNewTable */
105798 sSrc.a[0].zName = pTab->zName;
105800 sSrc.a[0].iCursor = -1;
105801 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
105802 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
105841 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
105842 return pTab->aCol[iCol].affinity;
105849 ** or a sub-select with a column as the return value, then the
105864 assert( pExpr->op==TK_COLLATE
105865 || pExpr->op==TK_IF_NULL_ROW
105866 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
105867 pExpr = pExpr->pLeft;
105870 op = pExpr->op;
105871 if( op==TK_REGISTER ) op = pExpr->op2;
105874 assert( pExpr->y.pTab!=0 );
105875 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
105879 assert( pExpr->x.pSelect!=0 );
105880 assert( pExpr->x.pSelect->pEList!=0 );
105881 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
105882 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
105887 return sqlite3AffinityType(pExpr->u.zToken, 0);
105891 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
105892 assert( pExpr->iColumn < pExpr->iTable );
105893 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
105895 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
105900 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
105902 return pExpr->affExpr;
105910 ** If a memory allocation error occurs, that fact is recorded in pParse->db
105919 if( pCollName->n>0 ){
105920 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
105922 pNew->pLeft = pExpr;
105923 pNew->flags |= EP_Collate|EP_Skip;
105945 assert( pExpr->op==TK_COLLATE );
105946 pExpr = pExpr->pLeft;
105960 assert( pExpr->x.pList->nExpr>0 );
105961 assert( pExpr->op==TK_FUNCTION );
105962 pExpr = pExpr->x.pList->a[0].pExpr;
105964 assert( pExpr->op==TK_COLLATE );
105965 pExpr = pExpr->pLeft;
105986 sqlite3 *db = pParse->db;
105990 int op = p->op;
105991 if( op==TK_REGISTER ) op = p->op2;
105995 assert( p->y.pTab!=0 );
105996 if( (j = p->iColumn)>=0 ){
105997 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
106003 p = p->pLeft;
106008 p = p->x.pList->a[0].pExpr;
106013 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
106016 if( p->flags & EP_Collate ){
106017 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
106018 p = p->pLeft;
106020 Expr *pNext = p->pRight;
106023 assert( p->x.pList==0 || p->pRight==0 );
106024 if( p->x.pList!=0 && !db->mallocFailed ){
106026 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
106027 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
106028 pNext = p->x.pList->a[i].pExpr;
106057 if( p==0 ) p = pParse->db->pDfltColl;
106068 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
106100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
106101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
106102 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
106103 assert( pExpr->pLeft );
106104 aff = sqlite3ExprAffinity(pExpr->pLeft);
106105 if( pExpr->pRight ){
106106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
106108 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
106165 if( pLeft->flags & EP_Collate ){
106167 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
106188 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
106190 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
106211 if( pParse->nErr ) return 0;
106218 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
106220 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
106240 ** is a sub-select, return the number of columns in the sub-select. For
106244 u8 op = pExpr->op;
106245 if( op==TK_REGISTER ) op = pExpr->op2;
106248 return pExpr->x.pList->nExpr;
106251 return pExpr->x.pSelect->pEList->nExpr;
106258 ** Return a pointer to a subexpression of pVector that is the i-th
106268 ** just the expression for the i-th term of the result set, and may
106273 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
106275 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
106276 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
106278 return pVector->x.pSelect->pEList->a[i].pExpr;
106281 return pVector->x.pList->a[i].pExpr;
106290 ** the iField-th column of the vector expression pVector.
106310 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
106315 if( pVector->op==TK_SELECT ){
106323 ** pLeft->iTable: First in an array of register holding result, or 0
106335 pRet->iTable = nField;
106336 pRet->iColumn = iField;
106337 pRet->pLeft = pVector;
106340 if( pVector->op==TK_VECTOR ){
106343 ppVector = &pVector->x.pList->a[iField].pExpr;
106351 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
106359 ** sub-select returns more than one column, the first in an array
106367 if( pExpr->op==TK_SELECT ){
106375 ** Argument pVector points to a vector expression - either a TK_VECTOR
106383 ** containing the results of the sub-select.
106400 u8 op = pVector->op;
106404 return pVector->iTable+iField;
106408 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
106413 *ppExpr = pVector->x.pList->a[iField].pExpr;
106426 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
106427 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
106428 ** otherwise: op==pExpr->op and p5==0
106437 Vdbe *v = pParse->pVdbe;
106438 Expr *pLeft = pExpr->pLeft;
106439 Expr *pRight = pExpr->pRight;
106450 if( pParse->nErr ) return;
106455 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
106456 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
106457 || pExpr->op==TK_LT || pExpr->op==TK_GT
106458 || pExpr->op==TK_LE || pExpr->op==TK_GE
106460 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
106461 || (pExpr->op==TK_ISNOT && op==TK_NE) );
106462 assert( p5==0 || pExpr->op!=op );
106463 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
106491 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
106501 if( i==nLeft-1 ){
106509 if( i==nLeft-2 ) opx = op;
106527 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
106548 if( p->nHeight>*pnHeight ){
106549 *pnHeight = p->nHeight;
106556 for(i=0; i<p->nExpr; i++){
106557 heightOfExpr(p->a[i].pExpr, pnHeight);
106563 for(p=pSelect; p; p=p->pPrior){
106564 heightOfExpr(p->pWhere, pnHeight);
106565 heightOfExpr(p->pHaving, pnHeight);
106566 heightOfExpr(p->pLimit, pnHeight);
106567 heightOfExprList(p->pEList, pnHeight);
106568 heightOfExprList(p->pGroupBy, pnHeight);
106569 heightOfExprList(p->pOrderBy, pnHeight);
106584 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
106585 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
106586 nHeight = p->pRight->nHeight;
106589 heightOfSelect(p->x.pSelect, &nHeight);
106590 }else if( p->x.pList ){
106591 heightOfExprList(p->x.pList, &nHeight);
106592 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
106594 p->nHeight = nHeight + 1;
106606 if( pParse->nErr ) return;
106608 sqlite3ExprCheckHeight(pParse, p->nHeight);
106626 if( pParse->nErr ) return;
106627 if( p && ExprUseXList(p) && p->x.pList ){
106628 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
106645 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
106649 ** can be translated into a 32-bit integer, then the token is not
106666 if( op!=TK_INTEGER || pToken->z==0
106667 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
106668 nExtra = pToken->n+1;
106675 pNew->op = (u8)op;
106676 pNew->iAgg = -1;
106679 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
106680 pNew->u.iValue = iValue;
106682 pNew->u.zToken = (char*)&pNew[1];
106683 assert( pToken->z!=0 || pToken->n==0 );
106684 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
106685 pNew->u.zToken[pToken->n] = 0;
106686 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
106692 pNew->nHeight = 1;
106699 ** Allocate a new expression node from a zero-terminated token that has
106726 assert( db->mallocFailed );
106731 assert( pRoot->x.pSelect==0 );
106733 pRoot->pRight = pRight;
106734 pRoot->flags |= EP_Propagate & pRight->flags;
106736 pRoot->nHeight = pRight->nHeight+1;
106738 pRoot->nHeight = 1;
106742 pRoot->pLeft = pLeft;
106743 pRoot->flags |= EP_Propagate & pLeft->flags;
106745 if( pLeft->nHeight>=pRoot->nHeight ){
106746 pRoot->nHeight = pLeft->nHeight+1;
106757 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
106767 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
106770 p->op = op & 0xff;
106771 p->iAgg = -1;
106772 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
106773 sqlite3ExprCheckHeight(pParse, p->nHeight);
106775 sqlite3ExprDelete(pParse->db, pLeft);
106776 sqlite3ExprDelete(pParse->db, pRight);
106787 pExpr->x.pSelect = pSelect;
106791 assert( pParse->db->mallocFailed );
106792 sqlite3SelectDelete(pParse->db, pSelect);
106819 for(ii=0; ii<pEList->nExpr; ii++){
106821 Expr *pExpr = pEList->a[ii].pExpr;
106823 if( pExpr->op==TK_VECTOR ){
106825 nExprElem = pExpr->x.pList->nExpr;
106830 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
106836 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
106837 pExpr->x.pList = 0;
106840 pSel->op = TK_ALL;
106841 pSel->pPrior = pRet;
106847 if( pRet && pRet->pPrior ){
106848 pRet->selFlags |= SF_MultiValue;
106850 sqlite3ExprListDelete(pParse->db, pEList);
106863 sqlite3 *db = pParse->db;
106890 sqlite3 *db = pParse->db;
106898 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
106900 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
106901 && !pParse->nested
106905 pNew->x.pList = pList;
106917 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
106919 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
106920 ** top-level SQL
106930 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
106932 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
106933 || (pParse->db->flags & SQLITE_TrustedSchema)==0
106964 sqlite3 *db = pParse->db;
106970 z = pExpr->u.zToken;
106977 x = (ynVar)(++pParse->nVar);
106985 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
106986 i = z[1]-'0'; /* The common case of ?N for a single digit N */
106989 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
106993 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
106994 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
106995 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
106997 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
106998 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107002 if( x>pParse->nVar ){
107003 pParse->nVar = (int)x;
107005 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
107013 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
107015 x = (ynVar)(++pParse->nVar);
107020 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
107023 pExpr->iColumn = x;
107024 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
107026 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
107036 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
107038 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
107039 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
107042 assert( p->pLeft==0 );
107043 assert( p->pRight==0 );
107044 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
107045 assert( !ExprUseXList(p) || p->x.pList==0 );
107050 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
107051 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
107052 if( p->pRight ){
107054 sqlite3ExprDeleteNN(db, p->pRight);
107057 sqlite3SelectDelete(db, p->x.pSelect);
107059 sqlite3ExprListDelete(db, p->x.pList);
107062 sqlite3WindowDelete(db, p->y.pWin);
107081 }else if( p->pOn ){
107082 sqlite3ExprDeleteNN(db, p->pOn);
107083 }else if( p->pUsing ){
107084 sqlite3IdListDelete(db, p->pUsing);
107096 ** pExpr to the pParse->pConstExpr list with a register number of 0.
107112 sqlite3ExprDeleteNN(pParse->db, p);
107138 ** The dupedExprStructSize() function returns two values OR-ed together:
107151 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
107166 if( 0==flags || p->op==TK_SELECT_COLUMN
107176 if( p->pLeft || p->x.pList ){
107179 assert( p->pRight==0 );
107193 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107194 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
107217 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
107226 ** to store the copy of expression p, the copies of p->u.zToken
107227 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
107256 ** by the copy of the p->u.zToken string (if any).
107261 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
107262 nToken = sqlite3Strlen30(p->u.zToken) + 1;
107273 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
107278 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
107279 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
107280 pNew->flags |= staticFlag;
107286 /* Copy the p->u.zToken string, if any. */
107288 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
107289 memcpy(zToken, p->u.zToken, nToken);
107292 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
107293 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
107295 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
107297 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
107301 /* Fill in pNew->pLeft and pNew->pRight. */
107305 pNew->pLeft = p->pLeft ?
107306 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
107307 pNew->pRight = p->pRight ?
107308 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
107312 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
107321 if( pNew->op==TK_SELECT_COLUMN ){
107322 pNew->pLeft = p->pLeft;
107323 assert( p->pRight==0 || p->pRight==p->pLeft
107324 || ExprHasProperty(p->pLeft, EP_Subquery) );
107326 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
107328 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
107338 ** and the db->mallocFailed flag set.
107344 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
107348 pRet->nCte = p->nCte;
107349 for(i=0; i<p->nCte; i++){
107350 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
107351 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
107352 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
107353 pRet->a[i].eM10d = p->a[i].eM10d;
107368 ** objects found there, assembling them onto the linked list at Select->pWin.
107371 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
107372 Select *pSelect = pWalker->u.pSelect;
107373 Window *pWin = pExpr->y.pWin;
107376 assert( pWin->ppThis==0 );
107382 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
107411 ** part of the in-memory representation of the database schema.
107428 pNew->nExpr = p->nExpr;
107429 pNew->nAlloc = p->nAlloc;
107430 pItem = pNew->a;
107431 pOldItem = p->a;
107432 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
107433 Expr *pOldExpr = pOldItem->pExpr;
107435 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
107437 && pOldExpr->op==TK_SELECT_COLUMN
107438 && (pNewExpr = pItem->pExpr)!=0
107440 if( pNewExpr->pRight ){
107441 pPriorSelectColOld = pOldExpr->pRight;
107442 pPriorSelectColNew = pNewExpr->pRight;
107443 pNewExpr->pLeft = pNewExpr->pRight;
107445 if( pOldExpr->pLeft!=pPriorSelectColOld ){
107446 pPriorSelectColOld = pOldExpr->pLeft;
107448 pNewExpr->pRight = pPriorSelectColNew;
107450 pNewExpr->pLeft = pPriorSelectColNew;
107453 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
107454 pItem->fg = pOldItem->fg;
107455 pItem->fg.done = 0;
107456 pItem->u = pOldItem->u;
107475 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
107478 pNew->nSrc = pNew->nAlloc = p->nSrc;
107479 for(i=0; i<p->nSrc; i++){
107480 SrcItem *pNewItem = &pNew->a[i];
107481 const SrcItem *pOldItem = &p->a[i];
107483 pNewItem->pSchema = pOldItem->pSchema;
107484 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
107485 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
107486 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
107487 pNewItem->fg = pOldItem->fg;
107488 pNewItem->iCursor = pOldItem->iCursor;
107489 pNewItem->addrFillSub = pOldItem->addrFillSub;
107490 pNewItem->regReturn = pOldItem->regReturn;
107491 if( pNewItem->fg.isIndexedBy ){
107492 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
107494 pNewItem->u2 = pOldItem->u2;
107495 if( pNewItem->fg.isCte ){
107496 pNewItem->u2.pCteUse->nUse++;
107498 if( pNewItem->fg.isTabFunc ){
107499 pNewItem->u1.pFuncArg =
107500 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
107502 pTab = pNewItem->pTab = pOldItem->pTab;
107504 pTab->nTabRef++;
107506 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
107507 if( pOldItem->fg.isUsing ){
107508 assert( pNewItem->fg.isUsing );
107509 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
107511 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
107513 pNewItem->colUsed = pOldItem->colUsed;
107522 assert( p->eU4!=EU4_EXPR );
107523 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
107525 pNew->nId = p->nId;
107526 pNew->eU4 = p->eU4;
107527 for(i=0; i<p->nId; i++){
107528 struct IdList_item *pNewItem = &pNew->a[i];
107529 const struct IdList_item *pOldItem = &p->a[i];
107530 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
107531 pNewItem->u4 = pOldItem->u4;
107542 for(p=pDup; p; p=p->pPrior){
107545 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
107546 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
107547 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
107548 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
107549 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
107550 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
107551 pNew->op = p->op;
107552 pNew->pNext = pNext;
107553 pNew->pPrior = 0;
107554 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
107555 pNew->iLimit = 0;
107556 pNew->iOffset = 0;
107557 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
107558 pNew->addrOpenEphm[0] = -1;
107559 pNew->addrOpenEphm[1] = -1;
107560 pNew->nSelectRow = p->nSelectRow;
107561 pNew->pWith = sqlite3WithDup(db, p->pWith);
107563 pNew->pWin = 0;
107564 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
107565 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
107567 pNew->selId = p->selId;
107568 if( db->mallocFailed ){
107572 pNew->pNext = 0;
107577 pp = &pNew->pPrior;
107598 ** Reason: This routine assumes that the number of slots in pList->a[]
107603 ** NULL is returned. If non-NULL is returned, then it is guaranteed
107614 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
107619 pList->nAlloc = 4;
107620 pList->nExpr = 1;
107621 pItem = &pList->a[0];
107623 pItem->pExpr = pExpr;
107633 pList->nAlloc *= 2;
107635 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
107643 pItem = &pList->a[pList->nExpr++];
107645 pItem->pExpr = pExpr;
107655 return sqlite3ExprListAppendNew(pParse->db,pExpr);
107657 if( pList->nAlloc<pList->nExpr+1 ){
107658 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
107660 pItem = &pList->a[pList->nExpr++];
107662 pItem->pExpr = pExpr;
107683 sqlite3 *db = pParse->db;
107686 int iFirst = pList ? pList->nExpr : 0;
107697 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
107699 pColumns->nId, n);
107703 for(i=0; i<pColumns->nId; i++){
107704 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
107705 assert( pSubExpr!=0 || db->mallocFailed );
107709 assert( pList->nExpr==iFirst+i+1 );
107710 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
107711 pColumns->a[i].zName = 0;
107715 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
107716 Expr *pFirst = pList->a[iFirst].pExpr;
107718 assert( pFirst->op==TK_SELECT_COLUMN );
107722 pFirst->pRight = pExpr;
107727 pFirst->iTable = pColumns->nId;
107742 assert( p->nExpr>0 );
107754 pItem = &p->a[p->nExpr-1];
107755 assert( pItem->fg.bNulls==0 );
107759 pItem->fg.sortFlags = (u8)iSortOrder;
107762 pItem->fg.bNulls = 1;
107764 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
107774 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
107783 assert( pList!=0 || pParse->db->mallocFailed!=0 );
107784 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
107787 assert( pList->nExpr>0 );
107788 pItem = &pList->a[pList->nExpr-1];
107789 assert( pItem->zEName==0 );
107790 assert( pItem->fg.eEName==ENAME_NAME );
107791 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
107793 /* If dequote==0, then pName->z does not point to part of a DDL
107795 ** to the token-map. */
107796 sqlite3Dequote(pItem->zEName);
107798 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
107809 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
107818 sqlite3 *db = pParse->db;
107819 assert( pList!=0 || db->mallocFailed!=0 );
107821 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
107822 assert( pList->nExpr>0 );
107823 if( pItem->zEName==0 ){
107824 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
107825 pItem->fg.eEName = ENAME_SPAN;
107839 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
107840 testcase( pEList && pEList->nExpr==mx );
107841 testcase( pEList && pEList->nExpr==mx+1 );
107842 if( pEList && pEList->nExpr>mx ){
107851 int i = pList->nExpr;
107852 struct ExprList_item *pItem = pList->a;
107853 assert( pList->nExpr>0 );
107856 sqlite3ExprDelete(db, pItem->pExpr);
107857 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
107859 }while( --i>0 );
107867 ** Return the bitwise-OR of all Expr.flags fields in the given
107874 for(i=0; i<pList->nExpr; i++){
107875 Expr *pExpr = pList->a[i].pExpr;
107877 m |= pExpr->flags;
107883 ** This is a SELECT-node callback for the expression walker that
107885 ** pWalker->eCode to zero and abort.
107891 pWalker->eCode = 0;
107912 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
107917 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
107919 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
107921 pExpr->op = TK_TRUEFALSE;
107934 assert( pExpr->op==TK_TRUEFALSE );
107936 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
107937 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
107938 return pExpr->u.zToken[4]==0;
107956 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
107957 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
107958 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
107960 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
107962 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
107977 ** sqlite3ExprIsConstant() pWalker->eCode==1
107978 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
107979 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
107980 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
107997 /* If pWalker->eCode is 2 then any term of the expression that comes from
108000 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
108001 pWalker->eCode = 0;
108005 switch( pExpr->op ){
108007 ** and either pWalker->eCode==4 or 5 or the function has the
108010 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
108013 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
108016 pWalker->eCode = 0;
108029 testcase( pExpr->op==TK_ID );
108030 testcase( pExpr->op==TK_COLUMN );
108031 testcase( pExpr->op==TK_AGG_FUNCTION );
108032 testcase( pExpr->op==TK_AGG_COLUMN );
108033 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
108036 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
108043 testcase( pExpr->op==TK_REGISTER );
108044 testcase( pExpr->op==TK_IF_NULL_ROW );
108045 testcase( pExpr->op==TK_DOT );
108046 pWalker->eCode = 0;
108049 if( pWalker->eCode==5 ){
108053 pExpr->op = TK_NULL;
108054 }else if( pWalker->eCode==4 ){
108057 pWalker->eCode = 0;
108062 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
108063 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
108081 ** Walk an expression tree. Return non-zero if the expression is constant
108084 ** For the purposes of this function, a double-quoted string (ex: "abc")
108085 ** is considered a variable but a single-quoted string (ex: 'abc') is
108093 ** Walk an expression tree. Return non-zero if
108102 ** can be added to the pParse->pConstExpr list and evaluated once when
108110 ** Walk an expression tree. Return non-zero if the expression is constant
108112 ** expression must not refer to any non-deterministic function nor any
108127 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
108129 ** (2) pExpr cannot use subqueries or non-deterministic functions.
108143 if( pSrc->fg.jointype & JT_LTORJ ){
108146 if( pSrc->fg.jointype & JT_LEFT ){
108148 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
108152 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
108160 ExprList *pGroupBy = pWalker->u.pGroupBy;
108165 for(i=0; i<pGroupBy->nExpr; i++){
108166 Expr *p = pGroupBy->a[i].pExpr;
108167 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
108168 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
108175 /* Check if pExpr is a sub-select. If so, consider it variable. */
108177 pWalker->eCode = 0;
108185 ** Walk the expression tree passed as the first argument. Return non-zero
108193 ** assumes that no other collating sequence will have a finer-grained
108216 ** in a CREATE TABLE statement. Return non-zero if the expression is
108217 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
108231 ** For the purposes of this function, a double-quoted string (ex: "abc")
108232 ** is considered a variable but a single-quoted string (ex: 'abc') is
108260 ** to fit in a 32-bit integer, return 1 and put the value of the integer
108262 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
108268 /* If an expression is an integer literal that fits in a signed 32-bit
108270 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
108271 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
108273 if( p->flags & EP_IntValue ){
108274 *pValue = p->u.iValue;
108277 switch( p->op ){
108279 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
108284 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
108286 *pValue = -v;
108313 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108314 p = p->pLeft;
108317 op = p->op;
108318 if( op==TK_REGISTER ) op = p->op2;
108328 p->y.pTab==0 || /* Reference to column of index on expression */
108329 (p->iColumn>=0
108330 && p->y.pTab->aCol!=0 /* Possible due to prior error */
108331 && p->y.pTab->aCol[p->iColumn].notNull==0);
108351 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
108352 if( p->op==TK_UMINUS ) unaryMinus = 1;
108353 p = p->pLeft;
108355 op = p->op;
108356 if( op==TK_REGISTER ) op = p->op2;
108371 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
108372 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
108381 ** Return TRUE if the given string is a row-id column name.
108406 p = pX->x.pSelect;
108407 if( p->pPrior ) return 0; /* Not a compound SELECT */
108408 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
108409 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
108410 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
108413 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
108414 if( p->pLimit ) return 0; /* Has no LIMIT clause */
108415 if( p->pWhere ) return 0; /* Has no WHERE clause */
108416 pSrc = p->pSrc;
108418 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
108419 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
108420 pTab = pSrc->a[0].pTab;
108424 pEList = p->pEList;
108427 for(i=0; i<pEList->nExpr; i++){
108428 Expr *pRes = pEList->a[i].pExpr;
108429 if( pRes->op!=TK_COLUMN ) return 0;
108430 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
108438 ** Generate code that checks the left-most column of index table iCur to see if
108440 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
108458 ** right-hand side. Return TRUE if that list is constant.
108464 pLHS = pIn->pLeft;
108465 pIn->pLeft = 0;
108467 pIn->pLeft = pLHS;
108477 ** The job of this routine is to find or create a b-tree object that can
108481 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
108484 ** The returned value of this function indicates the b-tree type, as follows:
108486 ** IN_INDEX_ROWID - The cursor was opened on a database table.
108487 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
108488 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
108489 ** IN_INDEX_EPH - The cursor was opened on a specially created and
108491 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
108494 ** An existing b-tree might be used if the RHS expression pX is a simple
108501 ** pX->iTable made to point to the ephemeral table instead of an
108504 ** will be set on pX and the pX->y.sub fields will be set to show where
108513 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
108514 ** through the set members) then the b-tree must not contain duplicates.
108516 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
108519 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
108522 ** index can be found with the specified <columns> as its left-most.
108526 ** routine might decide that creating an ephemeral b-tree for membership
108531 ** When the b-tree is being used for membership tests, the calling function
108540 ** the value in that register will be NULL if the b-tree contains one or more
108541 ** NULL values, and it will be some non-NULL value if the b-tree contains no
108570 assert( pX->op==TK_IN );
108572 iTab = pParse->nTab++;
108581 ExprList *pEList = pX->x.pSelect->pEList;
108582 for(i=0; i<pEList->nExpr; i++){
108583 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
108585 if( i==pEList->nExpr ){
108593 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
108594 sqlite3 *db = pParse->db; /* Database connection */
108597 ExprList *pEList = p->pEList;
108598 int nExpr = pEList->nExpr;
108600 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
108601 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
108602 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
108603 pTab = p->pSrc->a[0].pTab;
108606 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108609 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
108612 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
108620 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
108632 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
108633 int iCol = pEList->a[i].pExpr->iColumn;
108655 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
108658 if( pIdx->nColumn<nExpr ) continue;
108659 if( pIdx->pPartIdxWhere!=0 ) continue;
108660 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
108662 testcase( pIdx->nColumn==BMS-2 );
108663 testcase( pIdx->nColumn==BMS-1 );
108664 if( pIdx->nColumn>=BMS-1 ) continue;
108666 if( pIdx->nKeyCol>nExpr
108667 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
108675 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
108676 Expr *pRhs = pEList->a[i].pExpr;
108680 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
108682 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
108683 assert( pIdx->azColl[j] );
108684 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
108696 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
108697 if( colUsed==(MASKBIT(nExpr)-1) ){
108701 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
108702 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
108704 VdbeComment((v, "%s", pIdx->zName));
108706 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
108710 i64 mask = (1<<nExpr)-1;
108714 *prRhsHasNull = ++pParse->nMem;
108736 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
108738 pParse->nTab--; /* Back out the allocation of the unused cursor */
108739 iTab = -1; /* Cursor is not allocated */
108744 /* Could not find an existing table or index to use as the RHS b-tree.
108747 u32 savedNQueryLoop = pParse->nQueryLoop;
108751 pParse->nQueryLoop = 0;
108753 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
108755 assert( pX->op==TK_IN );
108760 pParse->nQueryLoop = savedNQueryLoop;
108765 n = sqlite3ExprVectorSize(pX->pLeft);
108776 ** function allocates and returns a nul-terminated string containing
108783 Expr *pLeft = pExpr->pLeft;
108785 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
108788 assert( pExpr->op==TK_IN );
108789 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
108796 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
108812 ** "sub-select returns N columns - expected M"
108815 if( pParse->nErr==0 ){
108816 const char *zFmt = "sub-select returns %d columns - expected %d";
108824 ** it is not permitted. If pExpr is a sub-select vector, this routine
108827 ** "sub-select returns N columns - expected 1"
108836 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
108850 ** x IN (4,5,11) -- IN operator with list on right-hand side
108851 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
108855 ** table is computed, the cursor number is also stored in pExpr->iTable,
108879 v = pParse->pVdbe;
108885 ** * The right-hand side is a correlated subquery
108886 ** * The right-hand side is an expression list containing variables
108892 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
108901 pExpr->x.pSelect->selId));
108904 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
108905 pExpr->y.sub.iAddr);
108906 assert( iTab!=pExpr->iTable );
108907 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
108916 pExpr->y.sub.regReturn = ++pParse->nMem;
108917 pExpr->y.sub.iAddr =
108918 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
108924 pLeft = pExpr->pLeft;
108930 pExpr->iTable = iTab;
108931 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
108934 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
108939 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
108947 Select *pSelect = pExpr->x.pSelect;
108948 ExprList *pEList = pSelect->pEList;
108951 addrOnce?"":"CORRELATED ", pSelect->selId
108955 if( ALWAYS(pEList->nExpr==nVal) ){
108962 pSelect->iLimit = 0;
108963 testcase( pSelect->selFlags & SF_Distinct );
108965 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
108966 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
108967 sqlite3SelectDelete(pParse->db, pCopy);
108968 sqlite3DbFree(pParse->db, dest.zAffSdst);
108975 assert( pEList->nExpr>0 );
108979 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
108980 pParse, p, pEList->a[i].pExpr
108984 }else if( ALWAYS(pExpr->x.pList!=0) ){
108994 ExprList *pList = pExpr->x.pList;
109005 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109011 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
109012 Expr *pE2 = pItem->pExpr;
109016 ** this code only executes once. Because for a non-constant
109020 sqlite3VdbeChangeToNoop(v, addrOnce-1);
109042 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109043 || pParse->nErr );
109044 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109045 pExpr->y.sub.iAddr, 1);
109056 ** (SELECT a FROM b) -- subquery
109057 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
109061 ** Return the register that holds the result. For a multi-column SELECT,
109063 ** return value is the register of the left-most result column.
109075 Vdbe *v = pParse->pVdbe;
109077 if( pParse->nErr ) return 0;
109078 testcase( pExpr->op==TK_EXISTS );
109079 testcase( pExpr->op==TK_SELECT );
109080 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
109082 pSel = pExpr->x.pSelect;
109087 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
109089 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
109090 pExpr->y.sub.iAddr);
109091 return pExpr->iTable;
109098 pExpr->y.sub.regReturn = ++pParse->nMem;
109099 pExpr->y.sub.iAddr =
109100 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
109105 ** * The right-hand side is a correlated subquery
109106 ** * The right-hand side is an expression list containing variables
109127 addrOnce?"":"CORRELATED ", pSel->selId));
109128 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
109129 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
109130 pParse->nMem += nReg;
109131 if( pExpr->op==TK_SELECT ){
109135 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
109142 if( pSel->pLimit ){
109143 /* The subquery already has a limit. If the pre-existing limit is X
109145 sqlite3 *db = pParse->db;
109148 pLimit->affExpr = SQLITE_AFF_NUMERIC;
109150 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
109152 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
109153 pSel->pLimit->pLeft = pLimit;
109155 /* If there is no pre-existing limit add a limit of 1 */
109156 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
109157 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
109159 pSel->iLimit = 0;
109161 pExpr->op2 = pExpr->op;
109162 pExpr->op = TK_ERROR;
109165 pExpr->iTable = rReg = dest.iSDParm;
109173 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
109174 || pParse->nErr );
109175 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
109176 pExpr->y.sub.iAddr, 1);
109186 ** sub-select on the RHS of the IN() operator has the same number of
109188 ** a sub-query, that the LHS is a vector of size 1.
109191 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
109192 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
109193 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
109194 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
109198 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
109212 ** The left-hand side (LHS) is a scalar or vector expression. The
109213 ** right-hand side (RHS) is an array of zero or more scalar values, or a
109228 ** See the separate in-operator.md documentation file in the canonical
109252 int addrTop; /* Top of the step-6 loop */
109254 u8 okConstFactor = pParse->okConstFactor;
109257 pLeft = pExpr->pLeft;
109260 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
109262 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
109264 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
109270 v = pParse->pVdbe;
109278 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
109283 ** nVector-1. */
109303 assert( pParse->okConstFactor==okConstFactor );
109304 pParse->okConstFactor = 0;
109306 pParse->okConstFactor = okConstFactor;
109323 ** This is step (1) in the in-operator.md optimized algorithm.
109333 pList = pExpr->x.pList;
109334 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
109339 for(ii=0; ii<pList->nExpr; ii++){
109340 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
109341 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
109345 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
109349 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
109350 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
109351 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
109352 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
109383 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
109384 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
109391 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
109396 /* In this case, the RHS is the ROWID of table b-tree and so we also
109397 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
109415 /* Step 4. If the RHS is known to be non-NULL and we did not find
109475 sqlite3DbFree(pParse->db, aiMap);
109476 sqlite3DbFree(pParse->db, zAff);
109483 ** value described by z[0..n-1] into register iMem.
109485 ** The z[] string will probably not be zero-terminated. But the
109494 if( negateFlag ) value = -value;
109503 ** text z[0..n-1] into register iMem.
109505 ** Expr.u.zToken is always UTF8 and zero-terminated.
109508 Vdbe *v = pParse->pVdbe;
109509 if( pExpr->flags & EP_IntValue ){
109510 int i = pExpr->u.iValue;
109512 if( negFlag ) i = -i;
109517 const char *z = pExpr->u.zToken;
109522 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
109527 negFlag?"-":"",pExpr);
109535 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
109543 ** appropriate for the iIdxCol-th column of index pIdx.
109552 i16 iTabCol = pIdx->aiColumn[iIdxCol];
109554 assert( pIdx->aColExpr );
109555 assert( pIdx->aColExpr->nExpr>iIdxCol );
109556 pParse->iSelfTab = iTabCur + 1;
109557 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
109558 pParse->iSelfTab = 0;
109560 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
109577 Vdbe *v = pParse->pVdbe;
109579 assert( pParse->iSelfTab!=0 );
109580 if( pParse->iSelfTab>0 ){
109581 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
109586 if( pCol->affinity>=SQLITE_AFF_TEXT ){
109587 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
109594 ** Generate code to extract the value of the iCol-th column of a table.
109606 if( iCol<0 || iCol==pTab->iPKey ){
109608 VdbeComment((v, "%s.rowid", pTab->zName));
109616 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
109618 if( pCol->colFlags & COLFLAG_BUSY ){
109620 pCol->zCnName);
109622 int savedSelfTab = pParse->iSelfTab;
109623 pCol->colFlags |= COLFLAG_BUSY;
109624 pParse->iSelfTab = iTabCur+1;
109626 pParse->iSelfTab = savedSelfTab;
109627 pCol->colFlags &= ~COLFLAG_BUSY;
109646 ** Generate code that will extract the iColumn-th column from
109660 assert( pParse->pVdbe!=0 );
109661 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
109663 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
109664 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
109670 ** Generate code to move content from registers iFrom...iFrom+nReg-1
109671 ** over to iTo..iTo+nReg-1.
109674 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
109685 p->op2 = p->op;
109686 p->op = TK_REGISTER;
109687 p->iTable = iReg;
109708 if( p->op==TK_SELECT ){
109716 iResult = pParse->nMem+1;
109717 pParse->nMem += nResult;
109720 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
109728 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
109732 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
109739 ** in-line rather than by using the usual callbacks.
109748 Vdbe *v = pParse->pVdbe;
109751 nFarg = pFarg->nExpr;
109752 assert( nFarg>0 ); /* All in-line functions have at least one argument */
109755 /* Attempt a direct implementation of the built-in COALESCE() and
109757 ** arguments past the first non-NULL argument.
109762 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
109766 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
109781 Expr *pArg = pFarg->a[0].pExpr;
109782 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
109783 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
109791 /* The UNLIKELY() function is a no-op. The result is the value
109795 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
109800 ** Test-only SQL functions that are only usable if enabled
109808 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
109817 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
109826 pA1 = pFarg->a[1].pExpr;
109827 if( pA1->op==TK_COLUMN ){
109829 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
109845 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
109847 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
109856 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxExpr.
109868 for(p=pParse->pIdxExpr; p; p=p->pIENext){
109869 int iDataCur = p->iDataCur;
109871 if( pParse->iSelfTab ){
109872 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
109873 iDataCur = -1;
109875 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
109876 v = pParse->pVdbe;
109878 if( p->bMaybeNullRow ){
109883 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
109885 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109886 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109888 p = pParse->pIdxExpr;
109889 pParse->pIdxExpr = 0;
109891 pParse->pIdxExpr = p;
109894 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
109895 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
109899 return -1; /* Not found */
109915 Vdbe *v = pParse->pVdbe; /* The VM under construction */
109918 int regFree1 = 0; /* If non-zero free this temporary register */
109919 int regFree2 = 0; /* If non-zero free this temporary register */
109924 assert( target>0 && target<=pParse->nMem );
109930 }else if( pParse->pIdxExpr!=0
109937 op = pExpr->op;
109941 AggInfo *pAggInfo = pExpr->pAggInfo;
109944 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
109945 pCol = &pAggInfo->aCol[pExpr->iAgg];
109946 if( !pAggInfo->directMode ){
109947 assert( pCol->iMem>0 );
109948 return pCol->iMem;
109949 }else if( pAggInfo->useSortingIdx ){
109950 Table *pTab = pCol->pTab;
109951 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
109952 pCol->iSorterColumn, target);
109953 if( pCol->iColumn<0 ){
109954 VdbeComment((v,"%s.rowid",pTab->zName));
109957 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
109958 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
109968 int iTab = pExpr->iTable;
109972 ** constraints, and that constant is coded by the pExpr->pLeft
109978 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
109980 assert( pExpr->y.pTab!=0 );
109981 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
109987 &zAff[(aff-'B')*2], P4_STATIC);
109992 if( pParse->iSelfTab<0 ){
109996 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
110002 int iCol = pExpr->iColumn;
110004 pTab = pExpr->y.pTab;
110007 assert( iCol<pTab->nCol );
110009 return -1-pParse->iSelfTab;
110011 pCol = pTab->aCol + iCol;
110013 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
110015 if( pCol->colFlags & COLFLAG_GENERATED ){
110016 if( pCol->colFlags & COLFLAG_BUSY ){
110018 pCol->zCnName);
110021 pCol->colFlags |= COLFLAG_BUSY;
110022 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
110025 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
110029 if( pCol->affinity==SQLITE_AFF_REAL ){
110039 iTab = pParse->iSelfTab - 1;
110043 assert( pExpr->y.pTab!=0 );
110044 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
110045 pExpr->iColumn, iTab, target,
110046 pExpr->op2);
110060 codeReal(v, pExpr->u.zToken, 0, target);
110066 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
110074 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
110084 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
110085 assert( pExpr->u.zToken[1]=='\'' );
110086 z = &pExpr->u.zToken[2];
110087 n = sqlite3Strlen30(z) - 1;
110096 assert( pExpr->u.zToken!=0 );
110097 assert( pExpr->u.zToken[0]!=0 );
110098 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
110099 if( pExpr->u.zToken[1]!=0 ){
110100 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
110101 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
110102 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
110108 return pExpr->iTable;
110113 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
110120 sqlite3AffinityType(pExpr->u.zToken, 0));
110128 /* fall-through */
110135 Expr *pLeft = pExpr->pLeft;
110140 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
110142 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
110184 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
110185 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
110192 Expr *pLeft = pExpr->pLeft;
110194 if( pLeft->op==TK_INTEGER ){
110198 }else if( pLeft->op==TK_FLOAT ){
110200 codeReal(v, pLeft->u.zToken, 1, target);
110209 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
110219 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
110227 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
110229 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
110230 bNormal = pExpr->op2==TK_IS;
110242 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
110252 AggInfo *pInfo = pExpr->pAggInfo;
110254 || NEVER(pExpr->iAgg<0)
110255 || NEVER(pExpr->iAgg>=pInfo->nFunc)
110260 return pInfo->aFunc[pExpr->iAgg].iMem;
110271 sqlite3 *db = pParse->db; /* The database connection */
110277 return pExpr->y.pWin->regResult;
110284 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
110288 pFarg = pExpr->x.pList;
110289 nFarg = pFarg ? pFarg->nExpr : 0;
110291 zId = pExpr->u.zToken;
110294 if( pDef==0 && pParse->explain ){
110298 if( pDef==0 || pDef->xFinalize!=0 ){
110302 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
110303 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
110304 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
110306 SQLITE_PTR_TO_INT(pDef->pUserData), target);
110307 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
110312 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
110316 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
110317 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
110322 r1 = pParse->nMem+1;
110323 pParse->nMem += nFarg;
110333 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
110336 assert( pFarg->a[0].pExpr!=0 );
110337 exprOp = pFarg->a[0].pExpr->op;
110341 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
110342 pFarg->a[0].pExpr->op2 =
110343 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
110366 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
110368 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
110371 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
110372 if( !pColl ) pColl = db->pDfltColl;
110376 pDef, pExpr->op2);
110392 if( pParse->db->mallocFailed ){
110396 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
110406 Expr *pLeft = pExpr->pLeft;
110407 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
110408 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
110409 pLeft->op2 = pParse->withinRJSubrtn;
110411 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
110413 if( pExpr->iTable!=n ){
110415 pExpr->iTable, n);
110417 return pLeft->iTable + pExpr->iColumn;
110440 ** X is stored in pExpr->pLeft.
110441 ** Y is stored in pExpr->pList->a[0].pExpr.
110442 ** Z is stored in pExpr->pList->a[1].pExpr.
110450 && ALWAYS(pExpr->pLeft)
110451 && pExpr->pLeft->op==TK_FUNCTION
110453 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
110461 pExpr = pExpr->pLeft;
110462 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
110467 pExpr = pExpr->pLeft;
110468 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
110473 ** to a column in the new.* or old.* pseudo-tables available to
110475 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
110476 ** is set to the column of the pseudo-table to read, or to -1 to
110481 ** to reference another column of the old.* pseudo-table, where
110483 ** set to (n+1), where n is the number of columns in each pseudo-table.
110484 ** For a reference to any other column in the new.* pseudo-table, p1
110493 ** p1==0 -> old.rowid p1==3 -> new.rowid
110494 ** p1==1 -> old.a p1==4 -> new.a
110495 ** p1==2 -> old.b p1==5 -> new.b
110502 pTab = pExpr->y.pTab;
110503 iCol = pExpr->iColumn;
110504 p1 = pExpr->iTable * (pTab->nCol+1) + 1
110507 assert( pExpr->iTable==0 || pExpr->iTable==1 );
110508 assert( iCol>=-1 && iCol<pTab->nCol );
110509 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
110510 assert( p1>=0 && p1<(pTab->nCol*2+2) );
110514 (pExpr->iTable ? "new" : "old"),
110515 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
110522 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
110524 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
110537 ** that derive from the right-hand table of a LEFT JOIN. The
110538 ** Expr.iTable value is the table number for the right-hand table.
110544 u8 okConstFactor = pParse->okConstFactor;
110545 AggInfo *pAggInfo = pExpr->pAggInfo;
110547 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
110548 if( !pAggInfo->directMode ){
110549 inReg = pAggInfo->aCol[pExpr->iAgg].iMem;
110552 if( pExpr->pAggInfo->useSortingIdx ){
110553 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
110554 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
110560 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
110563 ** really constant because they originate from the right-hand side
110565 pParse->okConstFactor = 0;
110566 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
110567 pParse->okConstFactor = okConstFactor;
110584 ** X (if it exists) is in pExpr->pLeft.
110585 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
110588 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
110605 sqlite3 *db = pParse->db;
110607 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
110608 assert(pExpr->x.pList->nExpr > 0);
110609 pEList = pExpr->x.pList;
110610 aListelem = pEList->a;
110611 nExpr = pEList->nExpr;
110613 if( (pX = pExpr->pLeft)!=0 ){
110615 if( db->mallocFailed ){
110619 testcase( pX->op==TK_COLUMN );
110627 ** The value in regFree1 might get SCopy-ed into the file result.
110632 for(i=0; i<nExpr-1; i=i+2){
110640 testcase( pTest->op==TK_COLUMN );
110642 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
110648 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
110659 assert( pExpr->affExpr==OE_Rollback
110660 || pExpr->affExpr==OE_Abort
110661 || pExpr->affExpr==OE_Fail
110662 || pExpr->affExpr==OE_Ignore
110664 if( !pParse->pTriggerTab && !pParse->nested ){
110666 "RAISE() may only be used within a trigger-program");
110669 if( pExpr->affExpr==OE_Abort ){
110673 if( pExpr->affExpr==OE_Ignore ){
110675 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
110679 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
110680 pExpr->affExpr, pExpr->u.zToken, 0, 0);
110716 p = pParse->pConstExpr;
110720 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
110721 if( pItem->fg.reusable
110722 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
110724 return pItem->u.iConstExprReg;
110728 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
110730 Vdbe *v = pParse->pVdbe;
110734 pParse->okConstFactor = 0;
110735 if( !pParse->db->mallocFailed ){
110736 if( regDest<0 ) regDest = ++pParse->nMem;
110739 pParse->okConstFactor = 1;
110740 sqlite3ExprDelete(pParse->db, pExpr);
110745 struct ExprList_item *pItem = &p->a[p->nExpr-1];
110746 pItem->fg.reusable = regDest<0;
110747 if( regDest<0 ) regDest = ++pParse->nMem;
110748 pItem->u.iConstExprReg = regDest;
110750 pParse->pConstExpr = p;
110773 && pExpr->op!=TK_REGISTER
110777 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
110800 assert( target>0 && target<=pParse->nMem );
110801 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
110802 if( pParse->pVdbe==0 ) return;
110811 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
110821 sqlite3 *db = pParse->db;
110823 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
110834 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
110846 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
110871 Vdbe *v = pParse->pVdbe;
110874 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
110875 n = pList->nExpr;
110877 for(pItem=pList->a, i=0; i<n; i++, pItem++){
110878 Expr *pExpr = pItem->pExpr;
110880 if( pItem->fg.bSorterRef ){
110881 i--;
110882 n--;
110885 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
110887 i--;
110888 n--;
110890 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
110901 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
110902 && pOp->p1+pOp->p3+1==inReg
110903 && pOp->p2+pOp->p3+1==target+i
110904 && pOp->p5==0 /* The do-not-merge flag must be clear */
110906 pOp->p3++;
110948 sqlite3 *db = pParse->db;
110955 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
110956 if( db->mallocFailed==0 ){
110962 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
110965 compRight.pRight = pExpr->x.pList->a[1].pExpr;
110975 pDel->flags |= EP_OuterON;
111009 Vdbe *v = pParse->pVdbe;
111019 op = pExpr->op;
111029 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
111031 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111035 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111036 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
111042 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111049 isNot = pExpr->op2==TK_ISNOT;
111050 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111054 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111057 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111075 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111077 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
111078 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
111079 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111099 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
111127 /* No-op */
111152 Vdbe *v = pParse->pVdbe;
111163 /* The value of pExpr->op and op are related as follows:
111165 ** pExpr->op op
111166 ** --------- ----------
111176 ** For other values of pExpr->op, op is undefined and unused.
111181 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
111185 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
111186 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
111187 assert( pExpr->op!=TK_NE || op==OP_Eq );
111188 assert( pExpr->op!=TK_EQ || op==OP_Ne );
111189 assert( pExpr->op!=TK_LT || op==OP_Ge );
111190 assert( pExpr->op!=TK_LE || op==OP_Gt );
111191 assert( pExpr->op!=TK_GT || op==OP_Le );
111192 assert( pExpr->op!=TK_GE || op==OP_Lt );
111194 switch( pExpr->op ){
111200 }else if( pExpr->op==TK_AND ){
111202 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
111203 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111207 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
111209 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
111216 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
111223 isNot = pExpr->op2==TK_ISNOT;
111224 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
111229 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
111234 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
111241 testcase( pExpr->op==TK_IS );
111242 testcase( pExpr->op==TK_ISNOT );
111243 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
111252 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
111254 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
111255 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
111256 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
111274 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
111304 /* no-op */
111325 sqlite3 *db = pParse->db;
111327 if( db->mallocFailed==0 ){
111337 ** If pExpr is a simple SQL value - an integer, real, string, blob
111338 ** or NULL value - then the VDBE currently being prepared is configured
111339 ** to re-prepare each time a new value is bound to variable pVar.
111342 ** same as that currently bound to variable pVar, non-zero is returned.
111355 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
111357 iVar = pVar->iColumn;
111358 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
111359 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
111362 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
111377 ** other than the top-level COLLATE operator.
111379 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
111391 ** this routine is used, it does not hurt to get an extra 2 - that
111396 ** pParse->pReprepare can be matched against literals in pB. The
111397 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
111412 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
111415 combinedFlags = pA->flags | pB->flags;
111417 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
111422 if( pA->op!=pB->op || pA->op==TK_RAISE ){
111423 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
111426 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
111429 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
111430 && pB->iTable<0 && pA->iTable==iTab
111439 if( pA->u.zToken ){
111440 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
111441 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
111443 assert( pA->op==pB->op );
111448 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
111453 }else if( pA->op==TK_NULL ){
111455 }else if( pA->op==TK_COLLATE ){
111456 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
111458 if( pB->u.zToken!=0
111459 && pA->op!=TK_COLUMN
111460 && pA->op!=TK_AGG_COLUMN
111461 && strcmp(pA->u.zToken,pB->u.zToken)!=0
111466 if( (pA->flags & (EP_Distinct|EP_Commuted))
111467 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
111471 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
111472 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
111473 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
111474 if( pA->op!=TK_STRING
111475 && pA->op!=TK_TRUEFALSE
111478 if( pA->iColumn!=pB->iColumn ) return 2;
111479 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
111480 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
111493 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
111496 ** This routine might return non-zero for equivalent ExprLists. The
111502 ** always differs from a non-NULL pointer.
111508 if( pA->nExpr!=pB->nExpr ) return 1;
111509 for(i=0; i<pA->nExpr; i++){
111511 Expr *pExprA = pA->a[i].pExpr;
111512 Expr *pExprB = pB->a[i].pExpr;
111513 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
111520 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
111531 ** Return non-zero if Expr p can only be true if pNN is not NULL.
111533 ** Or if seenNot is true, return non-zero if Expr p can only be
111534 ** non-NULL if pNN is not NULL
111541 int seenNot /* Return true only if p can be any non-NULL value */
111546 return pNN->op!=TK_NULL;
111548 switch( p->op ){
111551 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
111552 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
111557 pList = p->x.pList;
111559 assert( pList->nExpr==2 );
111561 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
111562 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
111566 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
111586 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
111593 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
111597 if( p->op2!=TK_IS ) return 0;
111598 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
111602 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
111625 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
111642 if( pE2->op==TK_OR
111643 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
111644 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
111648 if( pE2->op==TK_NOTNULL
111649 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
111658 ** If the expression node requires that the table at pWalker->iCur
111659 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
111662 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
111663 ** (never setting pWalker->eCode) is a harmless missed optimization.
111666 testcase( pExpr->op==TK_AGG_COLUMN );
111667 testcase( pExpr->op==TK_AGG_FUNCTION );
111669 switch( pExpr->op ){
111680 testcase( pExpr->op==TK_ISNOT );
111681 testcase( pExpr->op==TK_ISNULL );
111682 testcase( pExpr->op==TK_NOTNULL );
111683 testcase( pExpr->op==TK_IS );
111684 testcase( pExpr->op==TK_OR );
111685 testcase( pExpr->op==TK_VECTOR );
111686 testcase( pExpr->op==TK_CASE );
111687 testcase( pExpr->op==TK_IN );
111688 testcase( pExpr->op==TK_FUNCTION );
111689 testcase( pExpr->op==TK_TRUTH );
111692 if( pWalker->u.iCur==pExpr->iTable ){
111693 pWalker->eCode = 1;
111699 if( pWalker->eCode==0 ){
111700 sqlite3WalkExpr(pWalker, pExpr->pLeft);
111701 if( pWalker->eCode ){
111702 pWalker->eCode = 0;
111703 sqlite3WalkExpr(pWalker, pExpr->pRight);
111709 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
111710 assert( pWalker->eCode );
111724 Expr *pLeft = pExpr->pLeft;
111725 Expr *pRight = pExpr->pRight;
111726 testcase( pExpr->op==TK_EQ );
111727 testcase( pExpr->op==TK_NE );
111728 testcase( pExpr->op==TK_LT );
111729 testcase( pExpr->op==TK_LE );
111730 testcase( pExpr->op==TK_GT );
111731 testcase( pExpr->op==TK_GE );
111734 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
111735 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
111736 if( (pLeft->op==TK_COLUMN
111737 && ALWAYS(pLeft->y.pTab!=0)
111738 && IsVirtual(pLeft->y.pTab))
111739 || (pRight->op==TK_COLUMN
111740 && ALWAYS(pRight->y.pTab!=0)
111741 && IsVirtual(pRight->y.pTab))
111753 ** Return true (non-zero) if expression p can only be true if at least
111754 ** one column of table iTab is non-null. In other words, return true
111771 ** be non-NULL, then the LEFT JOIN can be safely converted into an
111778 if( p->op==TK_NOTNULL ){
111779 p = p->pLeft;
111781 while( p->op==TK_AND ){
111782 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
111783 p = p->pRight;
111809 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
111810 ** pWalker->u.pIdxCover->pIdx.
111813 if( pExpr->op==TK_COLUMN
111814 && pExpr->iTable==pWalker->u.pIdxCover->iCur
111815 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
111817 pWalker->eCode = 1;
111869 struct RefSrcList *p = pWalker->u.pRefSrcList;
111870 SrcList *pSrc = pSelect->pSrc;
111873 if( pSrc->nSrc==0 ) return WRC_Continue;
111874 j = p->nExclude;
111875 p->nExclude += pSrc->nSrc;
111876 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
111878 p->nExclude = 0;
111881 p->aiExclude = piNew;
111883 for(i=0; i<pSrc->nSrc; i++, j++){
111884 p->aiExclude[j] = pSrc->a[i].iCursor;
111889 struct RefSrcList *p = pWalker->u.pRefSrcList;
111890 SrcList *pSrc = pSelect->pSrc;
111891 if( p->nExclude ){
111892 assert( p->nExclude>=pSrc->nSrc );
111893 p->nExclude -= pSrc->nSrc;
111899 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
111902 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
111906 if( pExpr->op==TK_COLUMN
111907 || pExpr->op==TK_AGG_COLUMN
111910 struct RefSrcList *p = pWalker->u.pRefSrcList;
111911 SrcList *pSrc = p->pRef;
111912 int nSrc = pSrc ? pSrc->nSrc : 0;
111914 if( pExpr->iTable==pSrc->a[i].iCursor ){
111915 pWalker->eCode |= 1;
111919 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
111920 if( i>=p->nExclude ){
111921 pWalker->eCode |= 2;
111936 ** -1 pExpr only references no tables at all, or it only
111945 assert( pParse->db!=0 );
111952 x.db = pParse->db;
111954 assert( pExpr->op==TK_AGG_FUNCTION );
111956 sqlite3WalkExprList(&w, pExpr->x.pList);
111959 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
111962 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
111968 return -1;
111980 ** The copy is stored on pParse->pConstExpr with a register number of 0.
111987 && pExpr->pAggInfo!=0
111989 AggInfo *pAggInfo = pExpr->pAggInfo;
111990 int iAgg = pExpr->iAgg;
111991 Parse *pParse = pWalker->pParse;
111992 sqlite3 *db = pParse->db;
111993 if( pExpr->op!=TK_AGG_FUNCTION ){
111994 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_IF_NULL_ROW );
111995 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
111996 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
111999 pAggInfo->aCol[iAgg].pCExpr = pExpr;
112004 assert( pExpr->op==TK_AGG_FUNCTION );
112005 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
112006 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
112009 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
112024 pWalker->pParse = pParse;
112025 pWalker->xExprCallback = agginfoPersistExprCb;
112026 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
112030 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
112035 pInfo->aCol = sqlite3ArrayAllocate(
112037 pInfo->aCol,
112038 sizeof(pInfo->aCol[0]),
112039 &pInfo->nColumn,
112046 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
112051 pInfo->aFunc = sqlite3ArrayAllocate(
112053 pInfo->aFunc,
112054 sizeof(pInfo->aFunc[0]),
112055 &pInfo->nFunc,
112068 NameContext *pNC = pWalker->u.pNC;
112069 Parse *pParse = pNC->pParse;
112070 SrcList *pSrcList = pNC->pSrcList;
112071 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
112073 assert( pNC->ncFlags & NC_UAggInfo );
112074 switch( pExpr->op ){
112078 testcase( pExpr->op==TK_AGG_COLUMN );
112079 testcase( pExpr->op==TK_COLUMN );
112080 testcase( pExpr->op==TK_IF_NULL_ROW );
112084 SrcItem *pItem = pSrcList->a;
112085 for(i=0; i<pSrcList->nSrc; i++, pItem++){
112088 if( pExpr->iTable==pItem->iCursor ){
112092 ** Make an entry for the column in pAggInfo->aCol[] if there
112096 pCol = pAggInfo->aCol;
112097 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
112098 if( pCol->iTable==pExpr->iTable
112099 && pCol->iColumn==pExpr->iColumn
112100 && pExpr->op!=TK_IF_NULL_ROW
112105 if( (k>=pAggInfo->nColumn)
112106 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
112108 pCol = &pAggInfo->aCol[k];
112110 pCol->pTab = pExpr->y.pTab;
112111 pCol->iTable = pExpr->iTable;
112112 pCol->iColumn = pExpr->iColumn;
112113 pCol->iMem = ++pParse->nMem;
112114 pCol->iSorterColumn = -1;
112115 pCol->pCExpr = pExpr;
112116 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
112118 ExprList *pGB = pAggInfo->pGroupBy;
112119 struct ExprList_item *pTerm = pGB->a;
112120 n = pGB->nExpr;
112122 Expr *pE = pTerm->pExpr;
112123 if( pE->op==TK_COLUMN
112124 && pE->iTable==pExpr->iTable
112125 && pE->iColumn==pExpr->iColumn
112127 pCol->iSorterColumn = j;
112132 if( pCol->iSorterColumn<0 ){
112133 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
112136 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
112139 ** pAggInfo->aCol[] entry.
112142 pExpr->pAggInfo = pAggInfo;
112143 if( pExpr->op==TK_COLUMN ){
112144 pExpr->op = TK_AGG_COLUMN;
112146 pExpr->iAgg = (i16)k;
112148 } /* endif pExpr->iTable==pItem->iCursor */
112154 if( (pNC->ncFlags & NC_InAggFunc)==0
112155 && pWalker->walkerDepth==pExpr->op2
112160 struct AggInfo_func *pItem = pAggInfo->aFunc;
112161 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
112162 if( pItem->pFExpr==pExpr ) break;
112163 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
112167 if( i>=pAggInfo->nFunc ){
112168 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
112170 u8 enc = ENC(pParse->db);
112171 i = addAggInfoFunc(pParse->db, pAggInfo);
112174 pItem = &pAggInfo->aFunc[i];
112175 pItem->pFExpr = pExpr;
112176 pItem->iMem = ++pParse->nMem;
112178 pItem->pFunc = sqlite3FindFunction(pParse->db,
112179 pExpr->u.zToken,
112180 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
112181 if( pExpr->flags & EP_Distinct ){
112182 pItem->iDistinct = pParse->nTab++;
112184 pItem->iDistinct = -1;
112188 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
112192 pExpr->iAgg = (i16)i;
112193 pExpr->pAggInfo = pAggInfo;
112205 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
112220 assert( pNC->pSrcList!=0 );
112234 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
112235 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
112244 if( pParse->nTempReg==0 ){
112245 return ++pParse->nMem;
112247 return pParse->aTempReg[--pParse->nTempReg];
112257 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
112258 pParse->aTempReg[pParse->nTempReg++] = iReg;
112269 i = pParse->iRangeReg;
112270 n = pParse->nRangeReg;
112272 pParse->iRangeReg += nReg;
112273 pParse->nRangeReg -= nReg;
112275 i = pParse->nMem+1;
112276 pParse->nMem += nReg;
112286 if( nReg>pParse->nRangeReg ){
112287 pParse->nRangeReg = nReg;
112288 pParse->iRangeReg = iReg;
112295 ** Always invoke this procedure after coding a subroutine or co-routine
112297 ** the sub/co-routine does not use registers in common with the code that
112298 ** invokes the sub/co-routine.
112301 pParse->nTempReg = 0;
112302 pParse->nRangeReg = 0;
112313 if( pParse->nRangeReg>0
112314 && pParse->iRangeReg+pParse->nRangeReg > iFirst
112315 && pParse->iRangeReg <= iLast
112319 for(i=0; i<pParse->nTempReg; i++){
112320 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
112356 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
112361 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
112363 || (pTab->tabFlags & TF_Eponymous)!=0
112364 || ( (pTab->tabFlags & TF_Shadow)!=0
112365 && sqlite3ReadOnlyShadowTables(pParse->db)
112369 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
112389 pParse->colNamesSet = 1;
112413 ** Generate VM code to replace any double-quoted strings (but not double-quoted
112415 ** database zDb with their single-quoted equivalents. If argument bTemp is
112441 Vdbe *v = pParse->pVdbe;
112444 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
112445 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
112461 char *zName = 0; /* NULL-terminated version of pName */
112462 sqlite3 *db = pParse->db; /* Database connection */
112463 int nTabName; /* Number of UTF-8 characters in zTabName */
112466 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
112468 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
112469 assert( pSrc->nSrc==1 );
112470 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
112472 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112474 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
112475 zDb = db->aDb[iDb].zDbSName;
112505 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
112512 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
112523 if( pVTab->pVtab->pModule->xRename==0 ){
112539 /* figure out how many UTF-8 characters are in zName */
112540 zTabName = pTab->zName;
112541 nTabName = sqlite3Utf8CharLen(zTabName, -1);
112578 zDb, zName, pTab->zName);
112599 ** of any resources used by the v-table implementation (including other
112604 int i = ++pParse->nMem;
112639 ** The Table structure pParse->pNewTable was extended to include
112643 Table *pNew; /* Copy of pParse->pNewTable */
112648 char *zCol; /* Null-terminated column definition */
112655 db = pParse->db;
112656 assert( db->pParse==pParse );
112657 if( pParse->nErr ) return;
112658 assert( db->mallocFailed==0 );
112659 pNew = pParse->pNewTable;
112663 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
112664 zDb = db->aDb[iDb].zDbSName;
112665 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
112666 pCol = &pNew->aCol[pNew->nCol-1];
112673 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
112683 if( pCol->colFlags & COLFLAG_PRIMKEY ){
112687 if( pNew->pIndex ){
112692 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
112697 assert( pDflt==0 || pDflt->op==TK_SPAN );
112698 if( pDflt && pDflt->pLeft->op==TK_NULL ){
112702 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
112704 "Cannot add a REFERENCES column with non-NULL default value");
112706 if( pCol->notNull && !pDflt ){
112721 assert( db->mallocFailed == 1 );
112726 "Cannot add a column with non-constant default");
112730 }else if( pCol->colFlags & COLFLAG_STORED ){
112736 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
112738 char *zEnd = &zCol[pColDef->n-1];
112740 *zEnd-- = '\0';
112751 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
112766 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
112776 if( pNew->pCheck!=0
112777 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
112793 ** This function is called by the parser after the table-name in
112794 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
112795 ** pSrc is the full-name of the table being altered.
112813 sqlite3 *db = pParse->db;
112816 assert( pParse->pNewTable==0 );
112818 if( db->mallocFailed ) goto exit_begin_add_column;
112819 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112840 assert( pTab->u.tab.addColOffset>0 );
112841 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112852 pParse->pNewTable = pNew;
112853 pNew->nTabRef = 1;
112854 pNew->nCol = pTab->nCol;
112855 assert( pNew->nCol>0 );
112856 nAlloc = (((pNew->nCol-1)/8)*8)+8;
112857 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
112858 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
112859 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
112860 if( !pNew->aCol || !pNew->zName ){
112861 assert( db->mallocFailed );
112864 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
112865 for(i=0; i<pNew->nCol; i++){
112866 Column *pCol = &pNew->aCol[i];
112867 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
112868 pCol->hName = sqlite3StrIHash(pCol->zCnName);
112871 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
112872 pNew->pSchema = db->aDb[iDb].pSchema;
112873 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
112874 pNew->nTabRef = 1;
112885 ** it loads an error message into pParse and returns non-zero.
112905 zType, pTab->zName
112922 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
112926 sqlite3 *db = pParse->db; /* Database connection */
112936 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112944 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
112946 zDb = db->aDb[iSchema].zDbSName;
112950 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
112959 for(iCol=0; iCol<pTab->nCol; iCol++){
112960 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
112962 if( iCol==pTab->nCol ){
112967 /* Ensure the schema contains no double-quoted strings */
112978 assert( pNew->n>0 );
112979 bQuote = sqlite3Isquote(pNew->z[0]);
112986 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
112987 pTab->zName
112994 zDb, pTab->zName, iCol, zNew, bQuote
113049 ** rename-token list.
113051 ** 2. Dereferences each pointer in the rename-token list.
113054 ** address-sanitizer or similar. If any of these pointers no longer
113055 ** point to valid objects, an exception is raised by the memory-checking
113069 assert( pParse==pParse->db->pParse );
113070 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
113071 if( pParse->nErr==0 ){
113074 for(p=pParse->pRename; p; p=p->pNext){
113075 if( p->p ){
113076 assert( p->p!=pPtr );
113077 i += *(u8*)(p->p);
113092 ** in pParse->pRename.
113104 assert( pPtr || pParse->db->mallocFailed );
113106 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
113107 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
113109 pNew->p = pPtr;
113110 pNew->t = *pToken;
113111 pNew->pNext = pParse->pRename;
113112 pParse->pRename = pNew;
113127 for(p=pParse->pRename; p; p=p->pNext){
113128 if( p->p==pFrom ){
113129 p->p = pTo;
113139 Parse *pParse = pWalker->pParse;
113142 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
113152 With *pWith = pSelect->pWith;
113154 Parse *pParse = pWalker->pParse;
113157 assert( pWith->nCte>0 );
113158 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
113159 /* Push a copy of the With object onto the with-stack. We use a copy
113161 ** and SF_Resolved) below. And the parser code that uses the with-stack
113164 pCopy = sqlite3WithDup(pParse->db, pWith);
113167 for(i=0; i<pWith->nCte; i++){
113168 Select *p = pWith->a[i].pSelect;
113173 if( sNC.pParse->db->mallocFailed ) return;
113175 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
113177 if( pCopy && pParse->pWith==pCopy ){
113178 pParse->pWith = pCopy->pOuter;
113192 for(ii=0; ii<pIdList->nId; ii++){
113193 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
113201 Parse *pParse = pWalker->pParse;
113203 if( pParse->nErr ) return WRC_Abort;
113204 testcase( p->selFlags & SF_View );
113205 testcase( p->selFlags & SF_CopyCte );
113206 if( p->selFlags & (SF_View|SF_CopyCte) ){
113209 if( ALWAYS(p->pEList) ){
113210 ExprList *pList = p->pEList;
113211 for(i=0; i<pList->nExpr; i++){
113212 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
113213 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
113217 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
113218 SrcList *pSrc = p->pSrc;
113219 for(i=0; i<pSrc->nSrc; i++){
113220 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
113221 if( pSrc->a[i].fg.isUsing==0 ){
113222 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
113224 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
113237 u8 eMode = pParse->eParseMode;
113243 pParse->eParseMode = PARSE_MODE_UNMAP;
113245 pParse->eParseMode = eMode;
113249 ** Remove all nodes that are part of expression-list pEList from the
113260 for(i=0; i<pEList->nExpr; i++){
113261 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
113262 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
113275 pNext = p->pNext;
113298 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
113299 if( (*pp)->p==pPtr ){
113302 *pp = pToken->pNext;
113303 pToken->pNext = pCtx->pList;
113304 pCtx->pList = pToken;
113305 pCtx->nList++;
113316 ** descend into sub-select statements.
113319 if( p->selFlags & (SF_View|SF_CopyCte) ){
113320 testcase( p->selFlags & SF_View );
113321 testcase( p->selFlags & SF_CopyCte );
113335 ** constructed in RenameCtx object at pWalker->u.pRename.
113338 RenameCtx *p = pWalker->u.pRename;
113339 if( pExpr->op==TK_TRIGGER
113340 && pExpr->iColumn==p->iCol
113341 && pWalker->pParse->pTriggerTab==p->pTab
113343 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
113344 }else if( pExpr->op==TK_COLUMN
113345 && pExpr->iColumn==p->iCol
113347 && p->pTab==pExpr->y.pTab
113349 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
113364 RenameToken *pBest = pCtx->pList;
113368 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
113369 if( pToken->t.z>pBest->t.z ) pBest = pToken;
113371 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
113372 *pp = pBest->pNext;
113379 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
113381 ** sub-routine is currently stored in pParse->zErrMsg. This function
113395 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
113397 pParse->zErrMsg
113399 sqlite3_result_error(pCtx, zErr, -1);
113400 sqlite3DbFree(pParse->db, zErr);
113404 ** For each name in the the expression-list pEList (i.e. each
113405 ** pEList->a[i].zName) that matches the string in zOld, extract the
113406 ** corresponding rename-token from Parse object pParse and add it
113417 for(i=0; i<pEList->nExpr; i++){
113418 const char *zName = pEList->a[i].zEName;
113419 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
113430 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
113431 ** that matches the string in zOld, extract the corresponding rename-token
113442 for(i=0; i<pIdList->nId; i++){
113443 const char *zName = pIdList->a[i].zName;
113472 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
113473 p->eParseMode = PARSE_MODE_RENAME;
113474 p->db = db;
113475 p->nQueryLoop = 1;
113477 if( db->mallocFailed ) rc = SQLITE_NOMEM;
113479 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
113490 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
113491 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
113496 db->init.iDb = 0;
113536 nQuot = sqlite3Strlen30(zQuot)-1;
113540 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
113549 /* At this point pRename->pList contains a list of RenameToken objects
113551 ** with the new column name, or with single-quoted versions of themselves.
113556 while( pRename->pList ){
113563 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
113569 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
113572 /* Dequote the double-quoted token. Then requote it again, this time
113575 ** add another space after the new, single-quoted version of the
113578 memcpy(zBuf1, pBest->t.z, pBest->t.n);
113579 zBuf1[pBest->t.n] = 0;
113582 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
113588 iOff = pBest->t.z - zSql;
113589 if( pBest->t.n!=nReplace ){
113590 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
113591 nOut - (iOff + pBest->t.n)
113593 nOut += nReplace - pBest->t.n;
113600 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
113611 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
113617 sqlite3 *db = pParse->db;
113618 Trigger *pNew = pParse->pNewTrigger;
113625 assert( pNew->pTabSchema );
113626 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
113627 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
113629 pParse->eTriggerOp = pNew->op;
113632 if( ALWAYS(pParse->pTriggerTab) ){
113633 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
113637 if( rc==SQLITE_OK && pNew->pWhen ){
113638 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
113641 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
113642 if( pStep->pSelect ){
113643 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
113644 if( pParse->nErr ) rc = pParse->rc;
113646 if( rc==SQLITE_OK && pStep->zTarget ){
113650 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
113653 pStep->pExprList = 0;
113658 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
113659 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
113660 assert( pSrc==pSel->pSrc );
113661 if( pStep->pExprList ) pSel->pEList = 0;
113662 pSel->pSrc = 0;
113665 if( pStep->pFrom ){
113667 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
113668 SrcItem *p = &pStep->pFrom->a[i];
113669 if( p->pSelect ){
113670 sqlite3SelectPrep(pParse, p->pSelect, 0);
113675 if( db->mallocFailed ){
113679 if( rc==SQLITE_OK && pStep->pWhere ){
113680 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
113683 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
113685 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
113686 if( pStep->pUpsert && rc==SQLITE_OK ){
113687 Upsert *pUpsert = pStep->pUpsert;
113688 pUpsert->pUpsertSrc = pSrc;
113691 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
113693 ExprList *pUpsertSet = pUpsert->pUpsertSet;
113697 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
113700 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
113722 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
113725 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
113726 sqlite3WalkSelect(pWalker, pStep->pSelect);
113727 sqlite3WalkExpr(pWalker, pStep->pWhere);
113728 sqlite3WalkExprList(pWalker, pStep->pExprList);
113729 if( pStep->pUpsert ){
113730 Upsert *pUpsert = pStep->pUpsert;
113731 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
113732 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
113733 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
113734 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
113736 if( pStep->pFrom ){
113738 for(i=0; i<pStep->pFrom->nSrc; i++){
113739 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
113750 sqlite3 *db = pParse->db;
113752 if( pParse->pVdbe ){
113753 sqlite3VdbeFinalize(pParse->pVdbe);
113755 sqlite3DeleteTable(db, pParse->pNewTable);
113756 while( (pIdx = pParse->pNewIndex)!=0 ){
113757 pParse->pNewIndex = pIdx->pNext;
113760 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
113761 sqlite3DbFree(db, pParse->zErrMsg);
113762 renameTokenFree(db, pParse->pRename);
113778 ** 7. bQuote: Non-zero if the new column name should be quoted.
113782 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
113812 sqlite3_xauth xAuth = db->xAuth;
113822 if( pTab==0 || iCol>=pTab->nCol ){
113826 zOld = pTab->aCol[iCol].zCnName;
113828 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
113831 db->xAuth = 0;
113846 Select *pSelect = sParse.pNewTable->u.view.pSelect;
113847 pSelect->selFlags &= ~SF_View;
113850 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
113857 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
113861 if( iCol<sParse.pNewTable->nCol ){
113863 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
113867 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
113869 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
113870 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
113871 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
113873 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
113874 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
113877 for(i=0; i<sParse.pNewTable->nCol; i++){
113879 &sParse.pNewTable->aCol[i]);
113886 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
113887 for(i=0; i<pFKey->nCol; i++){
113888 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
113889 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
113891 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
113892 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
113894 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
113900 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
113901 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
113908 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
113909 if( pStep->zTarget ){
113910 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
113912 if( pStep->pUpsert ){
113913 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
113916 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
113917 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
113925 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
113949 db->xAuth = xAuth;
113958 RenameCtx *p = pWalker->u.pRename;
113959 if( pExpr->op==TK_COLUMN
113961 && p->pTab==pExpr->y.pTab
113963 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
113973 RenameCtx *p = pWalker->u.pRename;
113974 SrcList *pSrc = pSelect->pSrc;
113975 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
113976 testcase( pSelect->selFlags & SF_View );
113977 testcase( pSelect->selFlags & SF_CopyCte );
113981 assert( pWalker->pParse->db->mallocFailed );
113984 for(i=0; i<pSrc->nSrc; i++){
113985 SrcItem *pItem = &pSrc->a[i];
113986 if( pItem->pTab==p->pTab ){
113987 renameTokenFind(pWalker->pParse, p, pItem->zName);
114013 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
114036 sqlite3_xauth xAuth = db->xAuth;
114037 db->xAuth = 0;
114053 int isLegacy = (db->flags & SQLITE_LegacyAlter);
114059 Select *pSelect = pTab->u.view.pSelect;
114064 assert( pSelect->selFlags & SF_View );
114065 pSelect->selFlags &= ~SF_View;
114066 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
114070 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
114076 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
114081 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
114082 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
114083 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
114092 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
114095 sqlite3WalkExprList(&sWalker, pTab->pCheck);
114097 renameTokenFind(&sParse, &sCtx, pTab->zName);
114103 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
114105 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114113 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
114114 && sCtx.pTab->pSchema==pTrigger->pTabSchema
114116 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
114123 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
114124 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
114125 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
114127 if( pStep->pFrom ){
114129 for(i=0; i<pStep->pFrom->nSrc; i++){
114130 SrcItem *pItem = &pStep->pFrom->a[i];
114131 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
114132 renameTokenFind(&sParse, &sCtx, pItem->zName);
114160 db->xAuth = xAuth;
114168 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
114169 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
114177 ** double-quotes use single quotes instead.
114211 sqlite3_xauth xAuth = db->xAuth;
114212 db->xAuth = 0;
114237 Select *pSelect = sParse.pNewTable->u.view.pSelect;
114238 pSelect->selFlags &= ~SF_View;
114241 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
114247 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
114249 for(i=0; i<sParse.pNewTable->nCol; i++){
114252 &sParse.pNewTable->aCol[i]));
114257 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
114258 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
114284 db->xAuth = xAuth;
114323 int isLegacy = (db->flags & SQLITE_LegacyAlter);
114328 sqlite3_xauth xAuth = db->xAuth;
114329 db->xAuth = 0;
114337 int flags = db->flags;
114338 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
114340 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
114346 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
114355 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
114373 db->xAuth = xAuth;
114382 ** argv[0]: An integer - the index of the schema containing the table
114384 ** argv[2]: An integer - the index of the column to remove.
114398 const char *zDb = db->aDb[iSchema].zDbSName;
114407 sqlite3_xauth xAuth = db->xAuth;
114408 db->xAuth = 0;
114415 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
114421 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
114422 if( iCol<pTab->nCol-1 ){
114424 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
114425 zEnd = (const char*)pEnd->t.z;
114428 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
114429 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
114432 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
114433 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
114439 db->xAuth = xAuth;
114455 sqlite3 *db = pParse->db; /* Database handle */
114460 int iCol; /* Index of column zCol in pTab->aCol[] */
114463 assert( pParse->pNewTable==0 );
114465 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
114466 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
114477 assert( db->mallocFailed );
114488 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
114490 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
114497 if( pTab->nCol<=1 ){
114503 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114505 zDb = db->aDb[iDb].zDbSName;
114508 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
114518 , zDb, iDb, iCol, pTab->zName
114526 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
114532 int nField = 0; /* Number of non-virtual columns after drop */
114535 iCur = pParse->nTab++;
114538 reg = ++pParse->nMem;
114541 pParse->nMem += pTab->nCol;
114544 pParse->nMem += pPk->nColumn;
114545 for(i=0; i<pPk->nKeyCol; i++){
114548 nField = pPk->nKeyCol;
114550 regRec = ++pParse->nMem;
114551 for(i=0; i<pTab->nCol; i++){
114552 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
114557 if( iPos<pPk->nKeyCol ) continue;
114558 regOut = reg+1+iPos-(iPos>iColPos);
114562 if( i==pTab->iPKey ){
114572 pParse->nMem++;
114578 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
114594 ** Register built-in functions used to help implement ALTER TABLE
114611 ** 2005-07-08
114661 ** columns. The N-th integer (for N>1) is the average number of rows in
114662 ** the index which have the same value for the first N-1 columns. For
114663 ** a K-column index, there will be K+1 integers in the stat column. If
114687 ** inclusive are samples of the left-most key value in the index taken at
114694 ** For i between 0 and S-1. Conceptually, the index space is divided into
114721 ** of entries in the index whose left-most column exactly matches
114722 ** the left-most column of the sample. The second integer in nEq
114728 ** left-most column is less than the left-most column of the sample.
114729 ** The K-th integer in the nLt entry is the number of index entries
114744 ** looks at the left-most column of the index. The sqlite_stat3.sample
114745 ** column contains the actual value of the left-most column instead
114766 ** appropriate compile-time options are provided.
114795 sqlite3 *db = pParse->db;
114809 pDb = &db->aDb[iDb];
114818 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
114821 ** side-effect of the CREATE TABLE statement is to leave the rootpage
114822 ** of the new table in register pParse->regRoot. This is important
114825 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
114827 aRoot[i] = (u32)pParse->regRoot;
114834 aRoot[i] = pStat->tnum;
114839 pDb->zDbSName, zTab, zWhereType, zWhere
114842 }else if( db->xPreUpdateCallback ){
114843 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
114869 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
114894 int nLimit; /* Analysis row-scan limit */
114897 u8 nSkipAhead; /* Number of times of skip-ahead */
114902 u32 iPrn; /* Pseudo-random number used for sampling */
114917 if( p->nRowid ){
114918 sqlite3DbFree(db, p->u.aRowid);
114919 p->nRowid = 0;
114929 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
114930 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
114931 if( p->u.aRowid ){
114932 p->nRowid = n;
114933 memcpy(p->u.aRowid, pData, n);
114935 p->nRowid = 0;
114945 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
114946 p->nRowid = 0;
114947 p->u.iRowid = iRowid;
114957 pTo->isPSample = pFrom->isPSample;
114958 pTo->iCol = pFrom->iCol;
114959 pTo->iHash = pFrom->iHash;
114960 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
114961 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
114962 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
114963 if( pFrom->nRowid ){
114964 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
114966 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
114977 if( p->mxSample ){
114979 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
114980 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
114981 sampleClear(p->db, &p->current);
114984 sqlite3DbFree(p->db, p);
114993 ** L: A limit on the number of rows to scan, or 0 for no-limit
115051 p->db = db;
115052 p->nEst = sqlite3_value_int64(argv[2]);
115053 p->nRow = 0;
115054 p->nLimit = sqlite3_value_int64(argv[3]);
115055 p->nCol = nCol;
115056 p->nKeyCol = nKeyCol;
115057 p->nSkipAhead = 0;
115058 p->current.anDLt = (tRowcnt*)&p[1];
115059 p->current.anEq = &p->current.anDLt[nColUp];
115062 p->mxSample = p->nLimit==0 ? mxSample : 0;
115065 int i; /* Used to iterate through p->aSample[] */
115067 p->iGet = -1;
115068 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
115069 p->current.anLt = &p->current.anEq[nColUp];
115070 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
115073 p->a = (struct StatSample*)&p->current.anLt[nColUp];
115074 p->aBest = &p->a[mxSample];
115075 pSpace = (u8*)(&p->a[mxSample+nCol]);
115077 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115078 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115079 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
115081 assert( (pSpace - (u8*)p)==n );
115084 p->aBest[i].iCol = i;
115109 ** pNew and pOld are both candidate non-periodic samples selected for
115110 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
115117 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
115124 int nCol = pAccum->nCol;
115126 assert( pNew->iCol==pOld->iCol );
115127 for(i=pNew->iCol+1; i<nCol; i++){
115128 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
115129 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
115131 if( pNew->iHash>pOld->iHash ) return 1;
115141 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
115148 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
115149 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
115151 assert( pOld->isPSample==0 && pNew->isPSample==0 );
115152 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
115156 if( pNew->iCol<pOld->iCol ) return 1;
115157 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
115163 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
115164 ** remove the least desirable sample from p->a[] to make room.
115176 if( nEqZero>p->nMaxEqZero ){
115177 p->nMaxEqZero = nEqZero;
115179 if( pNew->isPSample==0 ){
115181 assert( pNew->anEq[pNew->iCol]>0 );
115188 for(i=p->nSample-1; i>=0; i--){
115189 StatSample *pOld = &p->a[i];
115190 if( pOld->anEq[pNew->iCol]==0 ){
115191 if( pOld->isPSample ) return;
115192 assert( pOld->iCol>pNew->iCol );
115200 pUpgrade->iCol = pNew->iCol;
115201 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
115207 if( p->nSample>=p->mxSample ){
115208 StatSample *pMin = &p->a[p->iMin];
115209 tRowcnt *anEq = pMin->anEq;
115210 tRowcnt *anLt = pMin->anLt;
115211 tRowcnt *anDLt = pMin->anDLt;
115212 sampleClear(p->db, pMin);
115213 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
115214 pSample = &p->a[p->nSample-1];
115215 pSample->nRowid = 0;
115216 pSample->anEq = anEq;
115217 pSample->anDLt = anDLt;
115218 pSample->anLt = anLt;
115219 p->nSample = p->mxSample-1;
115222 /* The "rows less-than" for the rowid column must be greater than that
115223 ** for the last sample in the p->a[] array. Otherwise, the samples would
115225 assert( p->nSample==0
115226 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
115229 pSample = &p->a[p->nSample];
115231 p->nSample++;
115234 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
115237 if( p->nSample>=p->mxSample ){
115238 int iMin = -1;
115239 for(i=0; i<p->mxSample; i++){
115240 if( p->a[i].isPSample ) continue;
115241 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
115246 p->iMin = iMin;
115254 ** p->current contains a sample that reflects the previous row of the
115263 for(i=(p->nCol-2); i>=iChng; i--){
115264 StatSample *pBest = &p->aBest[i];
115265 pBest->anEq[i] = p->current.anEq[i];
115266 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
115272 ** p->nMaxEqZero or greater set to zero. */
115273 for(i=p->nSample-1; i>=0; i--){
115275 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
115279 if( iChng<p->nMaxEqZero ){
115280 for(i=p->nSample-1; i>=0; i--){
115282 for(j=iChng; j<p->nCol; j++){
115283 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
115286 p->nMaxEqZero = iChng;
115296 ** C Index of left-most column to differ from previous row
115306 ** if it wants the byte-code to do special processing.
115323 assert( p->nCol>0 );
115324 assert( iChng<p->nCol );
115326 if( p->nRow==0 ){
115328 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
115332 if( p->mxSample ) samplePushPrevious(p, iChng);
115338 p->current.anEq[i]++;
115340 for(i=iChng; i<p->nCol; i++){
115341 p->current.anDLt[i]++;
115343 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
115345 p->current.anEq[i] = 1;
115349 p->nRow++;
115351 if( p->mxSample ){
115354 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
115356 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
115359 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
115361 nLt = p->current.anLt[p->nCol-1];
115363 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
115364 p->current.isPSample = 1;
115365 p->current.iCol = 0;
115366 sampleInsert(p, &p->current, p->nCol-1);
115367 p->current.isPSample = 0;
115371 for(i=0; i<(p->nCol-1); i++){
115372 p->current.iCol = i;
115373 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
115374 sampleCopy(p, &p->aBest[i], &p->current);
115379 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
115380 p->nSkipAhead++;
115381 sqlite3_result_int(context, p->current.anDLt[0]>0);
115419 ** a one-parameter function, stat_get(P), that always returns the
115436 assert( eCall==STAT_GET_STAT1 || p->mxSample );
115461 ** I = (K+D-1)/D
115471 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
115473 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
115474 for(i=0; i<p->nKeyCol; i++){
115475 u64 nDistinct = p->current.anDLt[i] + 1;
115476 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
115477 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
115479 assert( p->current.anEq[i] );
115485 if( p->iGet<0 ){
115487 p->iGet = 0;
115489 if( p->iGet<p->nSample ){
115490 StatSample *pS = p->a + p->iGet;
115491 if( pS->nRowid==0 ){
115492 sqlite3_result_int64(context, pS->u.iRowid);
115494 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
115503 assert( p->iGet<p->nSample );
115505 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
115506 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
115508 aCnt = p->a[p->iGet].anDLt;
115509 p->iGet++;
115513 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
115514 for(i=0; i<p->nCol; i++){
115517 if( sStat.nChar ) sStat.nChar--;
115539 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
115552 ** of the k-th column of the pIdx index.
115560 assert( k>=0 && k<pIdx->nColumn );
115561 i = pIdx->aiColumn[k];
115563 VdbeComment((v,"%s.rowid",pIdx->zName));
115565 assert( pIdx->bHasExpr );
115566 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
115568 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
115587 sqlite3 *db = pParse->db; /* Database handle */
115593 int jZeroRows = -1; /* Jump from here if number of rows is zero */
115610 pParse->nMem = MAX(pParse->nMem, iMem);
115619 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
115624 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115628 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
115629 db->aDb[iDb].zDbSName ) ){
115635 if( db->xPreUpdateCallback ){
115638 pStat1->zName = (char*)&pStat1[1];
115639 memcpy(pStat1->zName, "sqlite_stat1", 13);
115640 pStat1->nCol = 3;
115641 pStat1->iPKey = -1;
115642 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
115646 /* Establish a read-lock on the table at the shared-cache level.
115647 ** Open a read-only cursor on the table. Also allocate a cursor number
115650 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
115653 pParse->nTab = MAX(pParse->nTab, iTab);
115655 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
115657 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115665 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
115667 nCol = pIdx->nKeyCol;
115668 zIdxName = pTab->zName;
115669 nColTest = nCol - 1;
115671 nCol = pIdx->nColumn;
115672 zIdxName = pIdx->zName;
115673 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
115678 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
115681 ** Pseudo-code for loop that calls stat_push():
115716 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
115718 /* Open a read-only cursor on the index being analyzed. */
115719 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
115720 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
115722 VdbeComment((v, "%s", pIdx->zName));
115733 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
115747 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
115780 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
115781 /* For a single-column UNIQUE index, once we have found a non-NULL
115788 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
115808 sqlite3VdbeJumpHere(v, addrNextRow-1);
115831 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
115833 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
115834 for(j=0; j<pPk->nKeyCol; j++){
115835 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
115836 assert( k>=0 && k<pIdx->nColumn );
115840 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
115841 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
115849 if( db->nAnalysisLimit ){
115871 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
115877 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
115888 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
115906 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
115920 VdbeComment((v, "%s", pTab->zName));
115930 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
115952 sqlite3 *db = pParse->db;
115953 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
115960 iStatCur = pParse->nTab;
115961 pParse->nTab += 3;
115963 iMem = pParse->nMem+1;
115964 iTab = pParse->nTab;
115966 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
115983 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
115984 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
115986 iStatCur = pParse->nTab;
115987 pParse->nTab += 3;
115989 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
115991 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
115993 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
116001 ** ANALYZE -- 1
116002 ** ANALYZE <database> -- 2
116003 ** ANALYZE ?<database>.?<tablename> -- 3
116010 sqlite3 *db = pParse->db;
116021 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
116029 for(i=0; i<db->nDb; i++){
116033 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
116040 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
116044 analyzeTable(pParse, pIdx->pTable, pIdx);
116052 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
116068 ** The first argument points to a nul-terminated string containing a
116092 v = v*10 + c - '0';
116111 pIndex->bUnordered = 0;
116112 pIndex->noSkipScan = 0;
116115 pIndex->bUnordered = 1;
116116 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
116119 pIndex->szIdxRow = sqlite3LogEst(sz);
116121 pIndex->noSkipScan = 1;
116124 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
116125 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
116140 ** argv[2] = results of analysis - on integer for each column
116157 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
116166 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
116172 int nCol = pIndex->nKeyCol+1;
116177 if( pIndex->aiRowEst==0 ){
116178 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
116179 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
116181 aiRowEst = pIndex->aiRowEst;
116183 pIndex->bUnordered = 0;
116184 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
116185 pIndex->hasStat1 = 1;
116186 if( pIndex->pPartIdxWhere==0 ){
116187 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
116188 pTable->tabFlags |= TF_HasStat1;
116192 fakeIdx.szIdxRow = pTable->szTabRow;
116196 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
116197 pTable->szTabRow = fakeIdx.szIdxRow;
116198 pTable->tabFlags |= TF_HasStat1;
116210 if( pIdx->aSample ){
116212 for(j=0; j<pIdx->nSample; j++){
116213 IndexSample *p = &pIdx->aSample[j];
116214 sqlite3DbFree(db, p->p);
116216 sqlite3DbFree(db, pIdx->aSample);
116218 if( db && db->pnBytesFreed==0 ){
116219 pIdx->nSample = 0;
116220 pIdx->aSample = 0;
116230 ** Populate the pIdx->aAvgEq[] array based on the samples currently
116231 ** stored in pIdx->aSample[].
116235 IndexSample *aSample = pIdx->aSample;
116236 IndexSample *pFinal = &aSample[pIdx->nSample-1];
116239 if( pIdx->nSampleCol>1 ){
116244 nCol = pIdx->nSampleCol-1;
116245 pIdx->aAvgEq[nCol] = 1;
116248 int nSample = pIdx->nSample;
116256 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
116257 nRow = pFinal->anLt[iCol];
116258 nDist100 = (i64)100 * pFinal->anDLt[iCol];
116259 nSample--;
116261 nRow = pIdx->aiRowEst[0];
116262 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
116264 pIdx->nRowEst0 = nRow;
116271 if( i==(pIdx->nSample-1)
116280 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
116283 pIdx->aAvgEq[iCol] = avgEq;
116327 IndexSample *pSample; /* A slot in pIdx->aSample[] */
116329 assert( db->lookaside.bDisable );
116334 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
116352 assert( pIdx==0 || pIdx->nSample==0 );
116354 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
116355 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
116356 nIdxCol = pIdx->nKeyCol;
116358 nIdxCol = pIdx->nColumn;
116360 pIdx->nSampleCol = nIdxCol;
116365 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
116366 if( pIdx->aSample==0 ){
116370 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
116371 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
116372 pIdx->pTable->tabFlags |= TF_HasStat4;
116374 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
116375 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
116376 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
116378 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
116387 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
116402 nCol = pIdx->nSampleCol;
116407 pSample = &pIdx->aSample[pIdx->nSample];
116408 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
116409 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
116410 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
116418 pSample->n = sqlite3_column_bytes(pStmt, 4);
116419 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
116420 if( pSample->p==0 ){
116424 if( pSample->n ){
116425 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
116427 pIdx->nSample++;
116442 assert( db->lookaside.bDisable );
116472 ** If an OOM error occurs, this function always sets db->mallocFailed.
116481 Schema *pSchema = db->aDb[iDb].pSchema;
116484 assert( iDb>=0 && iDb<db->nDb );
116485 assert( db->aDb[iDb].pBt!=0 );
116489 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
116491 pTab->tabFlags &= ~TF_HasStat1;
116493 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
116495 pIdx->hasStat1 = 0;
116498 pIdx->aSample = 0;
116504 sInfo.zDatabase = db->aDb[iDb].zDbSName;
116520 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
116522 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
116532 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
116534 sqlite3_free(pIdx->aiRowEst);
116535 pIdx->aiRowEst = 0;
116588 if( pExpr->op!=TK_ID ){
116591 pExpr->op = TK_STRING;
116603 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
116609 ** An SQL user-function registered to do the work of an ATTACH statement. The
116619 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
116620 ** new database, close the database on db->init.iDb and reopen it as an
116648 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
116655 ** from sqlite3_deserialize() to close database db->init.iDb and
116659 pNew = &db->aDb[db->init.iDb];
116660 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
116661 pNew->pBt = 0;
116662 pNew->pSchema = 0;
116663 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
116673 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
116674 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
116675 db->aLimit[SQLITE_LIMIT_ATTACHED]
116679 for(i=0; i<db->nDb; i++){
116687 /* Allocate the new entry in the db->aDb[] array and initialize the schema
116690 if( db->aDb==db->aDbStatic ){
116691 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
116693 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
116695 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
116698 db->aDb = aNew;
116699 pNew = &db->aDb[db->nDb];
116706 flags = db->openFlags;
116707 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
116710 sqlite3_result_error(context, zErr, -1);
116716 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
116717 db->nDb++;
116718 pNew->zDbSName = sqlite3DbStrDup(db, zName);
116720 db->noSharedCache = 0;
116726 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
116727 if( !pNew->pSchema ){
116729 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
116734 sqlite3BtreeEnter(pNew->pBt);
116735 pPager = sqlite3BtreePager(pNew->pBt);
116736 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
116737 sqlite3BtreeSecureDelete(pNew->pBt,
116738 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
116740 sqlite3BtreeSetPagerFlags(pNew->pBt,
116741 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
116743 sqlite3BtreeLeave(pNew->pBt);
116745 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
116746 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
116768 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
116776 if( nKey || sqlite3BtreeGetRequestedReserve(db->aDb[0].pBt)>0 ){
116777 rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
116788 ** remove the entry from the db->aDb[] array. i.e. put everything back the
116793 db->init.iDb = 0;
116794 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
116805 if( newAuth<db->auth.authLevel ){
116812 int iDb = db->nDb - 1;
116814 if( db->aDb[iDb].pBt ){
116815 sqlite3BtreeClose(db->aDb[iDb].pBt);
116816 db->aDb[iDb].pBt = 0;
116817 db->aDb[iDb].pSchema = 0;
116820 db->nDb = iDb;
116837 sqlite3_result_error(context, zErrDyn, -1);
116844 ** An SQL user-function registered to do the work of an DETACH statement. The
116866 for(i=0; i<db->nDb; i++){
116867 pDb = &db->aDb[i];
116868 if( pDb->pBt==0 ) continue;
116872 if( i>=db->nDb ){
116880 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
116881 || sqlite3BtreeIsInBackup(pDb->pBt)
116889 assert( db->aDb[1].pSchema );
116890 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
116893 if( pTrig->pTabSchema==pDb->pSchema ){
116894 pTrig->pTabSchema = pTrig->pSchema;
116899 sqlite3BtreeClose(pDb->pBt);
116900 pDb->pBt = 0;
116901 pDb->pSchema = 0;
116906 sqlite3_result_error(context, zErr, -1);
116925 sqlite3* db = pParse->db;
116928 if( pParse->nErr ) goto attach_end;
116943 if( pAuthArg->op==TK_STRING ){
116945 zAuthArg = pAuthArg->u.zToken;
116963 assert( v || db->mallocFailed );
116965 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
116966 pFunc->nArg, pFunc, 0);
117025 DbFixer *pFix = p->u.pFix;
117026 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
117027 if( pExpr->op==TK_VARIABLE ){
117028 if( pFix->pParse->db->init.busy ){
117029 pExpr->op = TK_NULL;
117031 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
117042 DbFixer *pFix = p->u.pFix;
117045 sqlite3 *db = pFix->pParse->db;
117046 int iDb = sqlite3FindDbName(db, pFix->zDb);
117047 SrcList *pList = pSelect->pSrc;
117050 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
117051 if( pFix->bTemp==0 ){
117052 if( pItem->zDatabase ){
117053 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
117054 sqlite3ErrorMsg(pFix->pParse,
117056 pFix->zType, pFix->pName, pItem->zDatabase);
117059 sqlite3DbFree(db, pItem->zDatabase);
117060 pItem->zDatabase = 0;
117061 pItem->fg.notCte = 1;
117063 pItem->pSchema = pFix->pSchema;
117064 pItem->fg.fromDDL = 1;
117067 if( pList->a[i].fg.isUsing==0
117068 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
117074 if( pSelect->pWith ){
117075 for(i=0; i<pSelect->pWith->nCte; i++){
117076 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
117095 sqlite3 *db = pParse->db;
117096 assert( db->nDb>iDb );
117097 pFix->pParse = pParse;
117098 pFix->zDb = db->aDb[iDb].zDbSName;
117099 pFix->pSchema = db->aDb[iDb].pSchema;
117100 pFix->zType = zType;
117101 pFix->pName = pName;
117102 pFix->bTemp = (iDb==1);
117103 pFix->w.pParse = pParse;
117104 pFix->w.xExprCallback = fixExprCb;
117105 pFix->w.xSelectCallback = fixSelectCb;
117106 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
117107 pFix->w.walkerDepth = 0;
117108 pFix->w.eCode = 0;
117109 pFix->w.u.pFix = pFix;
117123 ** pParse->zErrMsg and these routines return non-zero. If everything
117135 res = sqlite3WalkSelect(&pFix->w, &s);
117144 return sqlite3WalkSelect(&pFix->w, pSelect);
117150 return sqlite3WalkExpr(&pFix->w, pExpr);
117160 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
117161 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
117162 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
117163 || sqlite3FixSrcList(pFix, pStep->pFrom)
117170 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
117171 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
117172 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
117173 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
117174 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
117181 pStep = pStep->pNext;
117204 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
117251 ** means that the SQL statement will never-run - the sqlite3_exec() call
117267 sqlite3_mutex_enter(db->mutex);
117268 db->xAuth = (sqlite3_xauth)xAuth;
117269 db->pAuthArg = pArg;
117270 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
117271 sqlite3_mutex_leave(db->mutex);
117276 ** Write an error message into pParse->zErrMsg that explains that the
117277 ** user-supplied authorization function returned an illegal value.
117281 pParse->rc = SQLITE_ERROR;
117299 sqlite3 *db = pParse->db; /* Database handle */
117300 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
117303 if( db->init.busy ) return SQLITE_OK;
117304 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
117306 ,db->auth.zAuthUser
117311 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
117313 pParse->rc = SQLITE_AUTH;
117337 int iSrc; /* Index in pTabList->a[] of table being read */
117341 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
117343 assert( pParse->db->xAuth!=0 );
117344 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
117351 if( pExpr->op==TK_TRIGGER ){
117352 pTab = pParse->pTriggerTab;
117355 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
117356 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
117357 pTab = pTabList->a[iSrc].pTab;
117362 iCol = pExpr->iColumn;
117366 assert( iCol<pTab->nCol );
117367 zCol = pTab->aCol[iCol].zCnName;
117368 }else if( pTab->iPKey>=0 ){
117369 assert( pTab->iPKey<pTab->nCol );
117370 zCol = pTab->aCol[pTab->iPKey].zCnName;
117374 assert( iDb>=0 && iDb<pParse->db->nDb );
117375 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
117376 pExpr->op = TK_NULL;
117393 sqlite3 *db = pParse->db;
117399 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
117400 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
117404 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
117405 ** callback are either NULL pointers or zero-terminated strings that
117413 testcase( pParse->zAuthContext==0 );
117415 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
117417 ,db->auth.zAuthUser
117422 pParse->rc = SQLITE_AUTH;
117433 ** popped. Or if pParse==0, this routine is a no-op.
117441 pContext->pParse = pParse;
117442 pContext->zAuthContext = pParse->zAuthContext;
117443 pParse->zAuthContext = zContext;
117451 if( pContext->pParse ){
117452 pContext->pParse->zAuthContext = pContext->zAuthContext;
117453 pContext->pParse = 0;
117500 ** Record the fact that we want to lock a table at run-time.
117523 for(i=0; i<pToplevel->nTableLock; i++){
117524 p = &pToplevel->aTableLock[i];
117525 if( p->iDb==iDb && p->iTab==iTab ){
117526 p->isWriteLock = (p->isWriteLock || isWriteLock);
117531 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
117532 pToplevel->aTableLock =
117533 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
117534 if( pToplevel->aTableLock ){
117535 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
117536 p->iDb = iDb;
117537 p->iTab = iTab;
117538 p->isWriteLock = isWriteLock;
117539 p->zLockName = zName;
117541 pToplevel->nTableLock = 0;
117542 sqlite3OomFault(pToplevel->db);
117553 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
117563 Vdbe *pVdbe = pParse->pVdbe;
117566 for(i=0; i<pParse->nTableLock; i++){
117567 TableLock *p = &pParse->aTableLock[i];
117568 int p1 = p->iDb;
117569 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
117570 p->zLockName, P4_STATIC);
117578 ** Return TRUE if the given yDbMask object is empty - if it contains no
117605 assert( pParse->pToplevel==0 );
117606 db = pParse->db;
117607 assert( db->pParse==pParse );
117608 if( pParse->nested ) return;
117609 if( pParse->nErr ){
117610 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
117613 assert( db->mallocFailed==0 );
117618 v = pParse->pVdbe;
117620 if( db->init.busy ){
117621 pParse->rc = SQLITE_DONE;
117625 if( v==0 ) pParse->rc = SQLITE_ERROR;
117627 assert( !pParse->isMultiWrite
117628 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
117630 if( pParse->bReturning ){
117631 Returning *pReturning = pParse->u1.pReturning;
117635 if( pReturning->nRetCol ){
117638 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
117640 reg = pReturning->iRetReg;
117641 for(i=0; i<pReturning->nRetCol; i++){
117642 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
117645 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
117653 if( pParse->nTableLock>0 && db->init.busy==0 ){
117655 if( db->auth.authLevel<UAUTH_User ){
117657 pParse->rc = SQLITE_AUTH_USER;
117669 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
117671 assert( db->nDb>0 );
117675 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
117677 pSchema = db->aDb[iDb].pSchema;
117681 DbMaskTest(pParse->writeMask,iDb), /* P2 */
117682 pSchema->schema_cookie, /* P3 */
117683 pSchema->iGeneration /* P4 */
117685 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
117687 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
117688 }while( ++iDb<db->nDb );
117690 for(i=0; i<pParse->nVtabLock; i++){
117691 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
117694 pParse->nVtabLock = 0;
117698 ** obtain the required table-locks. This is a no-op unless the
117699 ** shared-cache feature is enabled.
117714 if( pParse->pConstExpr ){
117715 ExprList *pEL = pParse->pConstExpr;
117716 pParse->okConstFactor = 0;
117717 for(i=0; i<pEL->nExpr; i++){
117718 int iReg = pEL->a[i].u.iConstExprReg;
117719 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
117723 if( pParse->bReturning ){
117724 Returning *pRet = pParse->u1.pReturning;
117725 if( pRet->nRetCol ){
117726 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
117736 assert( v!=0 || pParse->nErr );
117737 assert( db->mallocFailed==0 || pParse->nErr );
117738 if( pParse->nErr==0 ){
117741 assert( pParse->pAinc==0 || pParse->nTab>0 );
117743 pParse->rc = SQLITE_DONE;
117745 pParse->rc = SQLITE_ERROR;
117758 ** * Built-in SQL functions always take precedence over application-defined
117760 ** built-in function.
117765 sqlite3 *db = pParse->db;
117766 u32 savedDbFlags = db->mDbFlags;
117769 if( pParse->nErr ) return;
117770 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
117778 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
117779 pParse->nErr++;
117782 pParse->nested++;
117785 db->mDbFlags |= DBFLAG_PreferBuiltin;
117787 db->mDbFlags = savedDbFlags;
117790 pParse->nested--;
117804 ** Locate the in-memory structure that describes a particular database
117824 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
117829 for(i=0; i<db->nDb; i++){
117830 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
117832 if( i>=db->nDb ){
117841 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
117848 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
117853 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
117860 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
117863 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
117866 for(i=2; i<db->nDb; i++){
117868 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
117873 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
117875 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
117884 ** Locate the in-memory structure that describes a particular database
117887 ** error message in pParse->zErrMsg.
117890 ** routine leaves an error message in pParse->zErrMsg where
117900 sqlite3 *db = pParse->db;
117904 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
117916 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
117917 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
117922 testcase( pMod->pEpoTab==0 );
117923 return pMod->pEpoTab;
117928 pParse->checkSchema = 1;
117929 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
117941 assert( HasRowid(p) || p->iPKey<0 );
117952 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
117953 ** non-NULL if it is part of a view or trigger program definition. See
117962 assert( p->pSchema==0 || p->zDatabase==0 );
117963 if( p->pSchema ){
117964 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
117965 zDb = pParse->db->aDb[iDb].zDbSName;
117967 zDb = p->zDatabase;
117969 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
117989 ** Locate the in-memory structure that describes
118005 for(i=OMIT_TEMPDB; i<db->nDb; i++){
118007 Schema *pSchema = db->aDb[j].pSchema;
118011 p = sqlite3HashFind(&pSchema->idxHash, zName);
118024 sqlite3ExprDelete(db, p->pPartIdxWhere);
118025 sqlite3ExprListDelete(db, p->aColExpr);
118026 sqlite3DbFree(db, p->zColAff);
118027 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
118029 sqlite3_free(p->aiRowEst);
118045 pHash = &db->aDb[iDb].pSchema->idxHash;
118048 if( pIndex->pTable->pIndex==pIndex ){
118049 pIndex->pTable->pIndex = pIndex->pNext;
118054 p = pIndex->pTable->pIndex;
118055 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
118056 if( ALWAYS(p && p->pNext==pIndex) ){
118057 p->pNext = pIndex->pNext;
118062 db->mDbFlags |= DBFLAG_SchemaChange;
118066 ** Look through the list of open database files in db->aDb[] and if
118068 ** db->aDb[] structure to a smaller size, if possible.
118075 for(i=j=2; i<db->nDb; i++){
118076 struct Db *pDb = &db->aDb[i];
118077 if( pDb->pBt==0 ){
118078 sqlite3DbFree(db, pDb->zDbSName);
118079 pDb->zDbSName = 0;
118083 db->aDb[j] = db->aDb[i];
118087 db->nDb = j;
118088 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
118089 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
118090 sqlite3DbFree(db, db->aDb);
118091 db->aDb = db->aDbStatic;
118097 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
118102 assert( iDb<db->nDb );
118108 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
118111 if( db->nSchemaLock==0 ){
118112 for(i=0; i<db->nDb; i++){
118114 sqlite3SchemaClear(db->aDb[i].pSchema);
118127 for(i=0; i<db->nDb; i++){
118128 Db *pDb = &db->aDb[i];
118129 if( pDb->pSchema ){
118130 if( db->nSchemaLock==0 ){
118131 sqlite3SchemaClear(pDb->pSchema);
118137 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
118140 if( db->nSchemaLock==0 ){
118149 db->mDbFlags &= ~DBFLAG_SchemaChange;
118165 pList = pTab->u.tab.pDfltList;
118166 if( pCol->iDflt==0
118168 || NEVER(pList->nExpr<pCol->iDflt)
118170 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
118171 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
118173 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
118174 pList->a[pCol->iDflt-1].pExpr = pExpr;
118184 if( pCol->iDflt==0 ) return 0;
118186 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
118187 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
118188 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
118203 n = sqlite3Strlen30(pCol->zCnName) + 1;
118204 if( pCol->colFlags & COLFLAG_HASTYPE ){
118205 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
118208 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
118210 pCol->zCnName = zNew;
118211 memcpy(pCol->zCnName + n, zColl, nColl);
118212 pCol->colFlags |= COLFLAG_HASCOLL;
118221 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
118222 z = pCol->zCnName;
118224 if( pCol->colFlags & COLFLAG_HASTYPE ){
118239 if( (pCol = pTable->aCol)!=0 ){
118240 for(i=0; i<pTable->nCol; i++, pCol++){
118241 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
118242 sqlite3DbFree(db, pCol->zCnName);
118244 sqlite3DbNNFreeNN(db, pTable->aCol);
118246 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
118248 if( db->pnBytesFreed==0 ){
118249 pTable->aCol = 0;
118250 pTable->nCol = 0;
118252 pTable->u.tab.pDfltList = 0;
118270 ** db parameter can be used with db->pnBytesFreed to measure the memory
118286 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
118292 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
118293 pNext = pIndex->pNext;
118294 assert( pIndex->pSchema==pTable->pSchema
118295 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
118296 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
118297 char *zName = pIndex->zName;
118299 &pIndex->pSchema->idxHash, zName, 0
118301 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
118317 sqlite3SelectDelete(db, pTable->u.view.pSelect);
118323 sqlite3DbFree(db, pTable->zName);
118324 sqlite3DbFree(db, pTable->zColAff);
118325 sqlite3ExprListDelete(db, pTable->pCheck);
118335 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
118349 assert( iDb>=0 && iDb<db->nDb );
118352 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
118353 pDb = &db->aDb[iDb];
118354 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
118356 u8 tableType = p->eTabType;
118359 db->mDbFlags |= DBFLAG_SchemaChange;
118362 db->isDropTable = 1;
118363 db->mDropTableName = sqlite3_malloc(strlen(zTabName) + 1);
118364 if( db->mDropTableName!=NULL ){
118365 memcpy(db->mDropTableName, zTabName, strlen(zTabName) + 1);
118367 db->mDropSchemaName = sqlite3_malloc(strlen(db->aDb[iDb].zDbSName) + 1);
118368 if( db->mDropSchemaName!=NULL ){
118369 memcpy(db->mDropSchemaName, db->aDb[iDb].zDbSName, strlen(db->aDb[iDb].zDbSName) + 1);
118391 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
118407 if( p->nTab==0 ){
118408 p->nTab = 1;
118413 ** Parameter zName points to a nul-terminated buffer containing the name
118415 ** function returns the index of the named database in db->aDb[], or
118416 ** -1 if the named db cannot be found.
118419 int i = -1; /* Database number */
118422 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
118423 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
118435 ** index of the named database in db->aDb[], or -1 if the named db
118470 sqlite3 *db = pParse->db;
118473 if( pName2->n>0 ){
118474 if( db->init.busy ) {
118476 return -1;
118482 return -1;
118485 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
118486 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
118487 iDb = db->init.iDb;
118497 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
118498 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
118500 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
118502 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
118504 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
118508 ** This routine is used to check if the UTF-8 string zName is a legal
118524 sqlite3 *db = pParse->db;
118526 || db->init.imposterTable
118532 if( db->init.busy ){
118533 if( sqlite3_stricmp(zType, db->init.azInit[0])
118534 || sqlite3_stricmp(zName, db->init.azInit[1])
118535 || sqlite3_stricmp(zTblName, db->init.azInit[2])
118541 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
118558 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
118565 ** find the (first) offset of that column in index pIdx. Or return -1
118570 for(i=0; i<pIdx->nColumn; i++){
118571 if( iCol==pIdx->aiColumn[i] ) return i;
118573 return -1;
118586 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
118589 if( pTab->tabFlags & TF_HasVirtual ){
118592 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
118604 ** the N-th virtual column (zero-based) then the storage number is
118605 ** the number of non-virtual columns in the table plus N.
118621 ** -- 0 1 2 3 4 5 6 7 8
118632 ** this routine is a no-op macro. If the pTab does not have any virtual
118633 ** columns, then this routine is no-op that always return iCol. If iCol
118639 assert( iCol<pTab->nCol );
118640 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
118642 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
118644 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
118646 return pTab->nNVCol + i - n;
118660 ** is a read-only no-op.
118663 int iReg = ++pParse->nMem;
118681 ** The new table record is initialized and put in pParse->pNewTable.
118698 sqlite3 *db = pParse->db;
118703 if( db->init.busy && db->init.newTnum==1 ){
118705 iDb = db->init.iDb;
118712 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
118724 pParse->sNameToken = *pName;
118729 if( db->init.iDb==1 ) isTemp = 1;
118740 char *zDb = db->aDb[iDb].zDbSName;
118759 char *zDb = db->aDb[iDb].zDbSName;
118769 assert( !db->init.busy || CORRUPT_DB );
118783 assert( db->mallocFailed );
118784 pParse->rc = SQLITE_NOMEM_BKPT;
118785 pParse->nErr++;
118788 pTable->zName = zName;
118789 pTable->iPKey = -1;
118790 pTable->pSchema = db->aDb[iDb].pSchema;
118791 pTable->nTabRef = 1;
118793 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
118795 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
118797 assert( pParse->pNewTable==0 );
118798 pParse->pNewTable = pTable;
118808 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
118825 reg1 = pParse->regRowid = ++pParse->nMem;
118826 reg2 = pParse->regRoot = ++pParse->nMem;
118827 reg3 = ++pParse->nMem;
118831 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
118837 /* This just creates a place-holder record in the sqlite_schema table.
118841 ** The rowid for the new entry is left in register pParse->regRowid.
118842 ** The root page number of the new table is left in reg pParse->regRoot.
118852 assert( !pParse->bReturning );
118853 pParse->u1.addrCrTab =
118864 /* Normal (non-error) return. */
118869 pParse->checkSchema = 1;
118879 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
118880 pCol->colFlags |= COLFLAG_HIDDEN;
118881 if( pTab ) pTab->tabFlags |= TF_HasHidden;
118882 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
118883 pTab->tabFlags |= TF_OOOHidden;
118891 ** with any application-generated triggers.
118900 pHash = &(db->aDb[1].pSchema->trigHash);
118902 sqlite3ExprListDelete(db, pRet->pReturnEL);
118925 sqlite3 *db = pParse->db;
118926 if( pParse->pNewTrigger ){
118929 assert( pParse->bReturning==0 );
118931 pParse->bReturning = 1;
118937 pParse->u1.pReturning = pRet;
118938 pRet->pParse = pParse;
118939 pRet->pReturnEL = pList;
118942 testcase( pParse->earlyCleanup );
118943 if( db->mallocFailed ) return;
118944 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
118945 pRet->retTrig.op = TK_RETURNING;
118946 pRet->retTrig.tr_tm = TRIGGER_AFTER;
118947 pRet->retTrig.bReturning = 1;
118948 pRet->retTrig.pSchema = db->aDb[1].pSchema;
118949 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
118950 pRet->retTrig.step_list = &pRet->retTStep;
118951 pRet->retTStep.op = TK_RETURNING;
118952 pRet->retTStep.pTrig = &pRet->retTrig;
118953 pRet->retTStep.pExprList = pList;
118954 pHash = &(db->aDb[1].pSchema->trigHash);
118955 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
118956 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
118957 ==&pRet->retTrig ){
118976 sqlite3 *db = pParse->db;
118983 if( (p = pParse->pNewTable)==0 ) return;
118984 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
118985 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
118995 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
118997 sType.n -= 6;
118998 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119000 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
119002 sType.n -= 9;
119003 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
119032 for(i=0; i<p->nCol; i++){
119033 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
119039 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
119044 p->aCol = aNew;
119045 pCol = &p->aCol[p->nCol];
119046 memset(pCol, 0, sizeof(p->aCol[0]));
119047 pCol->zCnName = z;
119048 pCol->hName = hName;
119054 pCol->affinity = affinity;
119055 pCol->eCType = eType;
119056 pCol->szEst = szEst;
119060 pCol->colFlags |= COLFLAG_SORTERREF;
119069 pCol->affinity = sqlite3AffinityType(zType, pCol);
119070 pCol->colFlags |= COLFLAG_HASTYPE;
119072 p->nCol++;
119073 p->nNVCol++;
119074 pParse->constraintName.n = 0;
119086 p = pParse->pNewTable;
119087 if( p==0 || NEVER(p->nCol<1) ) return;
119088 pCol = &p->aCol[p->nCol-1];
119089 pCol->notNull = (u8)onError;
119090 p->tabFlags |= TF_HasNotNull;
119094 if( pCol->colFlags & COLFLAG_UNIQUE ){
119096 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
119097 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
119098 if( pIdx->aiColumn[0]==p->nCol-1 ){
119099 pIdx->uniqNotNull = 1;
119109 ** This routine does a case-independent search of zType for the
119117 ** --------------------------------
119175 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
119182 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
119187 pCol->colFlags |= COLFLAG_SORTERREF;
119192 pCol->szEst = v;
119215 sqlite3 *db = pParse->db;
119216 p = pParse->pNewTable;
119218 int isInit = db->init.busy && db->init.iDb!=1;
119219 pCol = &(p->aCol[p->nCol-1]);
119222 pCol->zCnName);
119224 }else if( pCol->colFlags & COLFLAG_GENERATED ){
119225 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119226 testcase( pCol->colFlags & COLFLAG_STORED );
119267 if( p->op==TK_STRING ){
119268 p->op = TK_ID;
119269 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
119270 p->pLeft->op = TK_ID;
119278 pCol->colFlags |= COLFLAG_PRIMKEY;
119280 if( pCol->colFlags & COLFLAG_GENERATED ){
119281 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
119282 testcase( pCol->colFlags & COLFLAG_STORED );
119301 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
119314 Table *pTab = pParse->pNewTable;
119316 int iCol = -1, i;
119319 if( pTab->tabFlags & TF_HasPrimaryKey ){
119321 "table \"%s\" has more than one primary key", pTab->zName);
119324 pTab->tabFlags |= TF_HasPrimaryKey;
119326 iCol = pTab->nCol - 1;
119327 pCol = &pTab->aCol[iCol];
119331 nTerm = pList->nExpr;
119333 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
119336 if( pCExpr->op==TK_ID ){
119339 zCName = pCExpr->u.zToken;
119340 for(iCol=0; iCol<pTab->nCol; iCol++){
119341 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
119342 pCol = &pTab->aCol[iCol];
119352 && pCol->eCType==COLTYPE_INTEGER
119356 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
119357 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
119359 pTab->iPKey = iCol;
119360 pTab->keyConf = (u8)onError;
119362 pTab->tabFlags |= autoInc*TF_Autoincrement;
119363 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
119377 sqlite3ExprListDelete(pParse->db, pList);
119391 Table *pTab = pParse->pNewTable;
119392 sqlite3 *db = pParse->db;
119394 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
119396 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
119397 if( pParse->constraintName.n ){
119398 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
119402 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
119404 t.n = (int)(zEnd - t.z);
119405 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
119410 sqlite3ExprDelete(pParse->db, pCheckExpr);
119424 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
119425 i = p->nCol-1;
119426 db = pParse->db;
119432 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
119438 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
119439 assert( pIdx->nKeyCol==1 );
119440 if( pIdx->aiColumn[0]==i ){
119441 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
119454 Table *pTab = pParse->pNewTable;
119460 pCol = &(pTab->aCol[pTab->nCol-1]);
119465 if( pCol->iDflt>0 ) goto generated_error;
119467 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
119468 /* no-op */
119469 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
119475 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
119476 pCol->colFlags |= eType;
119479 pTab->tabFlags |= eType;
119480 if( pCol->colFlags & COLFLAG_PRIMKEY ){
119489 pCol->zCnName);
119491 sqlite3ExprDelete(pParse->db, pExpr);
119494 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
119496 sqlite3ExprDelete(pParse->db, pExpr);
119510 ** This plan is not completely bullet-proof. It is possible for
119516 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
119517 ** the schema-version whenever the schema changes.
119520 sqlite3 *db = pParse->db;
119521 Vdbe *v = pParse->pVdbe;
119524 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
119547 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
119551 ** If the string zSignedIdent consists entirely of alpha-numeric
119554 ** it is quoted using double-quotes.
119590 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
119591 n += identLength(pCol->zCnName) + 5;
119593 n += identLength(p->zName);
119603 n += 35 + 6*p->nCol;
119611 identPut(zStmt, &k, p->zName);
119613 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
119624 sqlite3_snprintf(n-k, &zStmt[k], zSep);
119627 identPut(zStmt, &k, pCol->zCnName);
119628 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
119629 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
119630 testcase( pCol->affinity==SQLITE_AFF_BLOB );
119631 testcase( pCol->affinity==SQLITE_AFF_TEXT );
119632 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
119633 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
119634 testcase( pCol->affinity==SQLITE_AFF_REAL );
119636 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
119638 assert( pCol->affinity==SQLITE_AFF_BLOB
119639 || pCol->affinity==sqlite3AffinityType(zType, 0) );
119644 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
119655 if( pIdx->nColumn>=N ) return SQLITE_OK;
119656 assert( pIdx->isResized==0 );
119660 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
119661 pIdx->azColl = (const char**)zExtra;
119663 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
119664 pIdx->aiRowLogEst = (LogEst*)zExtra;
119666 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
119667 pIdx->aiColumn = (i16*)zExtra;
119669 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
119670 pIdx->aSortOrder = (u8*)zExtra;
119671 pIdx->nColumn = N;
119672 pIdx->isResized = 1;
119683 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
119684 wTable += pTabCol->szEst;
119686 if( pTab->iPKey<0 ) wTable++;
119687 pTab->szTabRow = sqlite3LogEst(wTable*4);
119696 const Column *aCol = pIdx->pTable->aCol;
119697 for(i=0; i<pIdx->nColumn; i++){
119698 i16 x = pIdx->aiColumn[i];
119699 assert( x<pIdx->pTable->nCol );
119700 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
119702 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
119710 while( nCol-- > 0 ){
119720 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
119733 assert( nKey<=pIdx->nColumn );
119734 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
119735 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
119736 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
119737 assert( pPk->pTable==pIdx->pTable );
119739 j = pPk->aiColumn[iCol];
119742 assert( pIdx->aiColumn[i]>=0 || j>=0 );
119743 if( pIdx->aiColumn[i]==j
119744 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
119757 ** high-order bit of colNotIdxed is always 1. All unindexed columns
119760 ** 2019-10-24: For the purpose of this computation, virtual columns are
119768 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
119774 Table *pTab = pIdx->pTable;
119775 for(j=pIdx->nColumn-1; j>=0; j--){
119776 int x = pIdx->aiColumn[j];
119777 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
119778 testcase( x==BMS-1 );
119779 testcase( x==BMS-2 );
119780 if( x<BMS-1 ) m |= MASKBIT(x);
119783 pIdx->colNotIdxed = ~m;
119784 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
119817 sqlite3 *db = pParse->db;
119818 Vdbe *v = pParse->pVdbe;
119822 if( !db->init.imposterTable ){
119823 for(i=0; i<pTab->nCol; i++){
119824 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
119825 && (pTab->aCol[i].notNull==OE_None)
119827 pTab->aCol[i].notNull = OE_Abort;
119830 pTab->tabFlags |= TF_HasNotNull;
119836 assert( !pParse->bReturning );
119837 if( pParse->u1.addrCrTab ){
119839 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
119845 if( pTab->iPKey>=0 ){
119848 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
119852 pTab->tabFlags &= ~TF_WithoutRowid;
119856 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
119858 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
119859 assert( pParse->pNewTable==pTab );
119860 pTab->iPKey = -1;
119861 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
119863 if( pParse->nErr ){
119864 pTab->tabFlags &= ~TF_WithoutRowid;
119867 assert( db->mallocFailed==0 );
119869 assert( pPk->nKeyCol==1 );
119879 for(i=j=1; i<pPk->nKeyCol; i++){
119881 pPk->nColumn--;
119883 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
119884 pPk->azColl[j] = pPk->azColl[i];
119885 pPk->aSortOrder[j] = pPk->aSortOrder[i];
119886 pPk->aiColumn[j++] = pPk->aiColumn[i];
119889 pPk->nKeyCol = j;
119892 pPk->isCovering = 1;
119893 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
119894 nPk = pPk->nColumn = pPk->nKeyCol;
119900 if( v && pPk->tnum>0 ){
119901 assert( db->init.busy==0 );
119902 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
119906 pPk->tnum = pTab->tnum;
119908 /* Update the in-memory representation of all UNIQUE indices by converting
119911 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119915 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
119916 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
119922 pIdx->nColumn = pIdx->nKeyCol;
119925 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
119926 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
119927 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
119928 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
119929 pIdx->aiColumn[j] = pPk->aiColumn[i];
119930 pIdx->azColl[j] = pPk->azColl[i];
119931 if( pPk->aSortOrder[i] ){
119933 pIdx->bAscKeyBug = 1;
119938 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
119939 assert( pIdx->nColumn>=j );
119945 for(i=0; i<pTab->nCol; i++){
119946 if( !hasColumn(pPk->aiColumn, nPk, i)
119947 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
119950 for(i=0, j=nPk; i<pTab->nCol; i++){
119951 if( !hasColumn(pPk->aiColumn, j, i)
119952 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
119954 assert( j<pPk->nColumn );
119955 pPk->aiColumn[j] = i;
119956 pPk->azColl[j] = sqlite3StrBINARY;
119960 assert( pPk->nColumn==j );
119961 assert( pTab->nNVCol<=j );
119976 nName = sqlite3Strlen30(pTab->zName);
119977 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
119979 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
119981 if( pMod->pModule->iVersion<3 ) return 0;
119982 if( pMod->pModule->xShadowName==0 ) return 0;
119983 return pMod->pModule->xShadowName(zName+nName+1);
119995 int nName; /* Length of pTab->zName */
120000 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
120002 if( NEVER(pMod->pModule==0) ) return;
120003 if( pMod->pModule->iVersion<3 ) return;
120004 if( pMod->pModule->xShadowName==0 ) return;
120005 assert( pTab->zName!=0 );
120006 nName = sqlite3Strlen30(pTab->zName);
120007 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
120009 assert( pOther->zName!=0 );
120011 if( pOther->tabFlags & TF_Shadow ) continue;
120012 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
120013 && pOther->zName[nName]=='_'
120014 && pMod->pModule->xShadowName(pOther->zName+nName+1)
120016 pOther->tabFlags |= TF_Shadow;
120067 #define markExprListImmutable(X) /* no-op */
120080 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
120099 sqlite3 *db = pParse->db; /* The database connection */
120106 p = pParse->pNewTable;
120109 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
120110 p->tabFlags |= TF_Shadow;
120113 /* If the db->init.busy is 1 it means we are reading the SQL off the
120116 ** for the table from the db->init.newTnum field. (The page number
120120 ** table itself. So mark it read-only.
120122 if( db->init.busy ){
120123 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
120127 p->tnum = db->init.newTnum;
120128 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
120142 p->tabFlags |= TF_Strict;
120143 for(ii=0; ii<p->nCol; ii++){
120144 Column *pCol = &p->aCol[ii];
120145 if( pCol->eCType==COLTYPE_CUSTOM ){
120146 if( pCol->colFlags & COLFLAG_HASTYPE ){
120149 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
120153 p->zName, pCol->zCnName);
120156 }else if( pCol->eCType==COLTYPE_ANY ){
120157 pCol->affinity = SQLITE_AFF_BLOB;
120159 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
120160 && p->iPKey!=ii
120161 && pCol->notNull == OE_None
120163 pCol->notNull = OE_Abort;
120164 p->tabFlags |= TF_HasNotNull;
120169 assert( (p->tabFlags & TF_HasPrimaryKey)==0
120170 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
120171 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
120172 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
120176 if( (p->tabFlags & TF_Autoincrement) ){
120181 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
120182 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
120185 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
120188 iDb = sqlite3SchemaToIndex(db, p->pSchema);
120193 if( p->pCheck ){
120194 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
120195 if( pParse->nErr ){
120198 sqlite3ExprListDelete(db, p->pCheck);
120199 p->pCheck = 0;
120201 markExprListImmutable(p->pCheck);
120206 if( p->tabFlags & TF_HasGenerated ){
120208 testcase( p->tabFlags & TF_HasVirtual );
120209 testcase( p->tabFlags & TF_HasStored );
120210 for(ii=0; ii<p->nCol; ii++){
120211 u32 colFlags = p->aCol[ii].colFlags;
120213 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
120223 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
120231 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
120239 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
120249 if( !db->init.busy ){
120277 ** statement to populate the new table. The root-page number for the
120278 ** new table is in register pParse->regRoot.
120284 ** A shared-cache write-lock is not required to write to the new table,
120285 ** as a schema-lock must have already been obtained to create it. Since
120286 ** a schema-lock excludes all other database users, the write-lock would
120291 int regYield; /* Register holding co-routine entry-point */
120292 int addrTop; /* Top of the co-routine */
120299 pParse->rc = SQLITE_ERROR;
120300 pParse->nErr++;
120303 regYield = ++pParse->nMem;
120304 regRec = ++pParse->nMem;
120305 regRowid = ++pParse->nMem;
120306 assert(pParse->nTab==1);
120308 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
120310 pParse->nTab = 2;
120313 if( pParse->nErr ) return;
120316 assert( p->aCol==0 );
120317 p->nCol = p->nNVCol = pSelTab->nCol;
120318 p->aCol = pSelTab->aCol;
120319 pSelTab->nCol = 0;
120320 pSelTab->aCol = 0;
120324 if( pParse->nErr ) return;
120326 sqlite3VdbeJumpHere(v, addrTop - 1);
120342 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
120343 n = (int)(pEnd2->z - pParse->sNameToken.z);
120344 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
120346 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
120358 db->aDb[iDb].zDbSName,
120360 p->zName,
120361 p->zName,
120362 pParse->regRoot,
120364 pParse->regRowid
120373 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
120374 Db *pDb = &db->aDb[iDb];
120376 if( pDb->pSchema->pSeqTab==0 ){
120379 pDb->zDbSName
120387 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
120390 /* Add the table to the in-memory representation of the database.
120392 if( db->init.busy ){
120394 Schema *pSchema = p->pSchema;
120396 assert( HasRowid(p) || p->iPKey<0 );
120397 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
120403 pParse->pNewTable = 0;
120404 db->mDbFlags |= DBFLAG_SchemaChange;
120409 assert( !pParse->nested );
120411 if( strcmp(p->zName, "sqlite_sequence")==0 ){
120413 p->pSchema->pSeqTab = p;
120421 if( pCons->z==0 ){
120424 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
120450 sqlite3 *db = pParse->db;
120452 if( pParse->nVar>0 ){
120457 p = pParse->pNewTable;
120458 if( p==0 || pParse->nErr ) goto create_view_fail;
120463 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
120466 p->tabFlags |= TF_NoVisibleRowid;
120470 iDb = sqlite3SchemaToIndex(db, p->pSchema);
120476 ** allocated rather than point to the input string - which means that
120479 pSelect->selFlags |= SF_View;
120481 p->u.view.pSelect = pSelect;
120484 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
120486 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
120487 p->eTabType = TABTYP_VIEW;
120488 if( db->mallocFailed ) goto create_view_fail;
120493 sEnd = pParse->sLastToken;
120499 n = (int)(sEnd.z - pBegin->z);
120501 z = pBegin->z;
120502 while( sqlite3Isspace(z[n-1]) ){ n--; }
120503 sEnd.z = &z[n-1];
120523 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
120529 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
120541 db->nSchemaLock++;
120543 db->nSchemaLock--;
120553 assert( pTable->nCol<=0 );
120570 if( pTable->nCol<0 ){
120571 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
120574 assert( pTable->nCol>=0 );
120584 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
120586 u8 eParseMode = pParse->eParseMode;
120587 int nTab = pParse->nTab;
120588 int nSelect = pParse->nSelect;
120589 pParse->eParseMode = PARSE_MODE_NORMAL;
120590 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
120591 pTable->nCol = -1;
120594 xAuth = db->xAuth;
120595 db->xAuth = 0;
120597 db->xAuth = xAuth;
120601 pParse->nTab = nTab;
120602 pParse->nSelect = nSelect;
120604 pTable->nCol = 0;
120606 }else if( pTable->pCheck ){
120609 ** arglist which is stored in pTable->pCheck. The pCheck field
120613 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
120614 &pTable->nCol, &pTable->aCol);
120615 if( pParse->nErr==0
120616 && pTable->nCol==pSel->pEList->nExpr
120618 assert( db->mallocFailed==0 );
120626 assert( pTable->aCol==0 );
120627 pTable->nCol = pSelTab->nCol;
120628 pTable->aCol = pSelTab->aCol;
120629 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
120630 pSelTab->nCol = 0;
120631 pSelTab->aCol = 0;
120632 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
120634 pTable->nNVCol = pTable->nCol;
120638 pParse->eParseMode = eParseMode;
120642 pTable->pSchema->schemaFlags |= DB_UnresetViews;
120643 if( db->mallocFailed ){
120651 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
120664 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
120679 ** root-page of a table or index in database iDb has changed from iFrom
120700 pDb = &db->aDb[iDb];
120701 pHash = &pDb->pSchema->tblHash;
120704 if( pTab->tnum==iFrom ){
120705 pTab->tnum = iTo;
120708 pHash = &pDb->pSchema->idxHash;
120711 if( pIdx->tnum==iFrom ){
120712 pIdx->tnum = iTo;
120719 ** Write code to erase the table with root-page iTable from database iDb.
120721 ** if a root-page of another table is moved by the btree-layer whilst
120722 ** erasing iTable (this can happen with an auto-vacuum database).
120732 ** is non-zero, then it is the root page number of a table moved to
120743 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
120751 ** in case a root-page belonging to another table is moved by the btree layer
120752 ** is also added (this can happen with an auto-vacuum database).
120755 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
120757 ** table and index root-pages in order, starting with the numerically
120758 ** largest root-page number. This guarantees that none of the root-pages
120766 ** and root page 5 happened to be the largest root-page number in the
120769 ** a free-list page.
120771 Pgno iTab = pTab->tnum;
120781 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
120782 Pgno iIdx = pIdx->tnum;
120783 assert( pIdx->pSchema==pTab->pSchema );
120791 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
120792 assert( iDb>=0 && iDb<pParse->db->nDb );
120810 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
120814 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
120828 sqlite3 *db = pParse->db;
120830 Db *pDb = &db->aDb[iDb];
120848 assert( pTrigger->pSchema==pTab->pSchema ||
120849 pTrigger->pSchema==db->aDb[1].pSchema );
120851 pTrigger = pTrigger->pNext;
120858 ** move as a result of the drop (can happen in auto-vacuum mode).
120860 if( pTab->tabFlags & TF_Autoincrement ){
120863 pDb->zDbSName, pTab->zName
120878 pDb->zDbSName, pTab->zName);
120887 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
120890 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
120896 ** Return TRUE if shadow tables should be read-only in the current
120901 if( (db->flags & SQLITE_Defensive)!=0
120902 && db->pVtabCtx==0
120903 && db->nVdbeExec==0
120916 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
120917 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
120918 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
120921 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
120924 if( pTab->tabFlags & TF_Eponymous ){
120937 sqlite3 *db = pParse->db;
120940 if( db->mallocFailed ){
120943 assert( pParse->nErr==0 );
120944 assert( pName->nSrc==1 );
120946 if( noErr ) db->suppressErr++;
120948 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
120949 if( noErr ) db->suppressErr--;
120953 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
120958 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
120959 assert( iDb>=0 && iDb<db->nDb );
120971 const char *zDb = db->aDb[iDb].zDbSName;
120985 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
120994 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
120997 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
121003 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
121012 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
121016 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
121028 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
121049 ** under construction in the pParse->pNewTable field.
121061 sqlite3 *db = pParse->db;
121065 Table *p = pParse->pNewTable;
121074 int iCol = p->nCol-1;
121076 if( pToCol && pToCol->nExpr!=1 ){
121079 p->aCol[iCol].zCnName, pTo);
121083 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
121089 nCol = pFromCol->nExpr;
121091 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
121093 for(i=0; i<pToCol->nExpr; i++){
121094 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
121101 pFKey->pFrom = p;
121103 pFKey->pNextFrom = p->u.tab.pFKey;
121104 z = (char*)&pFKey->aCol[nCol];
121105 pFKey->zTo = z;
121109 memcpy(z, pTo->z, pTo->n);
121110 z[pTo->n] = 0;
121112 z += pTo->n+1;
121113 pFKey->nCol = nCol;
121115 pFKey->aCol[0].iFrom = p->nCol-1;
121119 for(j=0; j<p->nCol; j++){
121120 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
121121 pFKey->aCol[i].iFrom = j;
121125 if( j>=p->nCol ){
121128 pFromCol->a[i].zEName);
121132 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
121138 int n = sqlite3Strlen30(pToCol->a[i].zEName);
121139 pFKey->aCol[i].zCol = z;
121141 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
121143 memcpy(z, pToCol->a[i].zEName, n);
121148 pFKey->isDeferred = 0;
121149 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
121150 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
121152 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
121153 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
121154 pFKey->zTo, (void *)pFKey
121161 assert( pNextTo->pPrevTo==0 );
121162 pFKey->pNextTo = pNextTo;
121163 pNextTo->pPrevTo = pFKey;
121169 p->u.tab.pFKey = pFKey;
121190 if( (pTab = pParse->pNewTable)==0 ) return;
121192 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
121193 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
121194 pFKey->isDeferred = (u8)isDeferred;
121207 ** the root page number of the index is taken from pIndex->tnum.
121210 Table *pTab = pIndex->pTable; /* The table that is indexed */
121211 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
121212 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
121221 sqlite3 *db = pParse->db; /* The database connection */
121222 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
121225 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
121226 db->aDb[iDb].zDbSName ) ){
121231 /* Require a write-lock on the table to perform this operation */
121232 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121239 tnum = pIndex->tnum;
121242 assert( pKey!=0 || pParse->nErr );
121245 iSorter = pParse->nTab++;
121246 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
121272 pIndex->nKeyCol); VdbeCoverage(v);
121287 if( !pIndex->bAscKeyBug ){
121312 ** of 8-byte aligned space after the Index object and return a
121332 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
121333 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
121334 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
121335 p->aSortOrder = (u8*)pExtra;
121336 p->nColumn = nCol;
121337 p->nKeyCol = nCol - 1;
121346 ** pParse and return non-zero. Otherwise, return zero.
121351 for(i=0; i<pList->nExpr; i++){
121352 if( pList->a[i].fg.bNulls ){
121353 u8 sf = pList->a[i].fg.sortFlags;
121368 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
121369 ** as the table to be indexed. pParse->pNewTable is a table that is
121373 ** is a primary key or unique-constraint on the most recent column added
121380 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
121396 sqlite3 *db = pParse->db;
121406 assert( db->pParse==pParse );
121407 if( pParse->nErr ){
121410 assert( db->mallocFailed==0 );
121426 /* Use the two-part index name to determine the database
121433 assert( pName && pName->z );
121440 if( !db->init.busy ){
121442 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
121454 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
121455 assert( db->mallocFailed==0 || pTab==0 );
121457 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
121459 "cannot create a TEMP index on non-TEMP table \"%s\"",
121460 pTab->zName);
121467 pTab = pParse->pNewTable;
121469 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121471 pDb = &db->aDb[iDb];
121474 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
121475 && db->init.busy==0
121478 && sqlite3UserAuthTable(pTab->zName)==0
121481 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
121513 assert( pName->z!=0 );
121514 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
121518 if( !db->init.busy ){
121519 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
121524 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
121528 assert( !db->init.busy );
121538 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
121539 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
121556 const char *zDb = pDb->zDbSName;
121562 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
121574 Column *pCol = &pTab->aCol[pTab->nCol-1];
121575 pCol->colFlags |= COLFLAG_UNIQUE;
121576 sqlite3TokenInit(&prevCol, pCol->zCnName);
121580 assert( pList->nExpr==1 );
121584 if( pParse->nErr ) goto exit_create_index;
121590 for(i=0; i<pList->nExpr; i++){
121591 Expr *pExpr = pList->a[i].pExpr;
121593 if( pExpr->op==TK_COLLATE ){
121595 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
121603 nExtraCol = pPk ? pPk->nKeyCol : 1;
121604 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
121605 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
121607 if( db->mallocFailed ){
121610 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
121611 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
121612 pIndex->zName = zExtra;
121614 memcpy(pIndex->zName, zName, nName+1);
121615 pIndex->pTable = pTab;
121616 pIndex->onError = (u8)onError;
121617 pIndex->uniqNotNull = onError!=OE_None;
121618 pIndex->idxType = idxType;
121619 pIndex->pSchema = db->aDb[iDb].pSchema;
121620 pIndex->nKeyCol = pList->nExpr;
121623 pIndex->pPartIdxWhere = pPIWhere;
121630 if( pDb->pSchema->file_format>=4 ){
121631 sortOrderMask = -1; /* Honor DESC */
121639 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
121645 pListItem = pList->a;
121647 pIndex->aColExpr = pList;
121650 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
121651 Expr *pCExpr; /* The i-th index expression */
121652 int requestedSortOrder; /* ASC or DESC on the i-th expression */
121655 sqlite3StringToId(pListItem->pExpr);
121656 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
121657 if( pParse->nErr ) goto exit_create_index;
121658 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
121659 if( pCExpr->op!=TK_COLUMN ){
121660 if( pTab==pParse->pNewTable ){
121665 if( pIndex->aColExpr==0 ){
121666 pIndex->aColExpr = pList;
121670 pIndex->aiColumn[i] = XN_EXPR;
121671 pIndex->uniqNotNull = 0;
121672 pIndex->bHasExpr = 1;
121674 j = pCExpr->iColumn;
121677 j = pTab->iPKey;
121679 if( pTab->aCol[j].notNull==0 ){
121680 pIndex->uniqNotNull = 0;
121682 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
121683 pIndex->bHasVCol = 1;
121684 pIndex->bHasExpr = 1;
121687 pIndex->aiColumn[i] = (i16)j;
121690 if( pListItem->pExpr->op==TK_COLLATE ){
121692 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
121693 zColl = pListItem->pExpr->u.zToken;
121699 nExtra -= nColl;
121701 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
121704 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
121707 pIndex->azColl[i] = zColl;
121708 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
121709 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
121717 for(j=0; j<pPk->nKeyCol; j++){
121718 int x = pPk->aiColumn[j];
121720 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
121721 pIndex->nColumn--;
121723 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
121724 pIndex->aiColumn[i] = x;
121725 pIndex->azColl[i] = pPk->azColl[j];
121726 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
121730 assert( i==pIndex->nColumn );
121732 pIndex->aiColumn[i] = XN_ROWID;
121733 pIndex->azColl[i] = sqlite3StrBINARY;
121736 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
121741 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
121743 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
121744 pIndex->isCovering = 1;
121745 for(j=0; j<pTab->nCol; j++){
121746 if( j==pTab->iPKey ) continue;
121748 pIndex->isCovering = 0;
121753 if( pTab==pParse->pNewTable ){
121776 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121779 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
121782 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
121783 for(k=0; k<pIdx->nKeyCol; k++){
121786 assert( pIdx->aiColumn[k]>=0 );
121787 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
121788 z1 = pIdx->azColl[k];
121789 z2 = pIndex->azColl[k];
121792 if( k==pIdx->nKeyCol ){
121793 if( pIdx->onError!=pIndex->onError ){
121801 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
121805 if( pIdx->onError==OE_Default ){
121806 pIdx->onError = pIndex->onError;
121809 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
121811 pIndex->pNext = pParse->pNewIndex;
121812 pParse->pNewIndex = pIndex;
121823 ** in-memory database structures.
121825 assert( pParse->nErr==0 );
121826 if( db->init.busy ){
121829 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
121831 pIndex->tnum = db->init.newTnum;
121834 pParse->rc = SQLITE_CORRUPT_BKPT;
121838 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
121839 pIndex->zName, pIndex);
121845 db->mDbFlags |= DBFLAG_SchemaChange;
121864 int iMem = ++pParse->nMem;
121877 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
121885 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
121886 if( pName->z[n-1]==';' ) n--;
121889 onError==OE_None ? "" : " UNIQUE", n, pName->z);
121900 db->aDb[iDb].zDbSName,
121901 pIndex->zName,
121902 pTab->zName,
121909 ** to invalidate all pre-compiled statements.
121915 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
121919 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
121922 if( db->init.busy || pTblName==0 ){
121923 pIndex->pNext = pTab->pIndex;
121924 pTab->pIndex = pIndex;
121928 assert( pParse->pNewIndex==0 );
121929 pParse->pNewIndex = pIndex;
121943 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
121945 if( pThis->onError!=OE_Replace ) continue;
121946 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
121948 pThis->pNext = pNext->pNext;
121949 pNext->pNext = pThis;
121950 ppFrom = &pNext->pNext;
121958 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
121959 assert( pThis->onError!=OE_Replace
121960 || pThis->pNext==0
121961 || pThis->pNext->onError==OE_Replace );
121972 ** Fill the Index.aiRowEst[] array with default information - information
121982 ** aiRowEst[N]<=aiRowEst[N-1]
121992 LogEst *a = pIdx->aiRowLogEst;
121994 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
121998 assert( !pIdx->hasStat1 );
122004 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
122010 x = pIdx->pTable->nRowLogEst;
122013 pIdx->pTable->nRowLogEst = x = 99;
122015 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
122021 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
122026 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
122036 sqlite3 *db = pParse->db;
122039 if( db->mallocFailed ){
122042 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
122043 assert( pName->nSrc==1 );
122047 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
122050 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
122052 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
122055 pParse->checkSchema = 1;
122058 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
122063 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
122067 Table *pTab = pIndex->pTable;
122068 const char *zDb = db->aDb[iDb].zDbSName;
122074 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
122086 db->aDb[iDb].zDbSName, pIndex->zName
122088 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
122090 destroyRootPage(pParse, pIndex->tnum, iDb);
122091 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
122104 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
122112 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
122124 if( (n & (n-1))==0 ){
122128 *pIdx = -1;
122146 sqlite3 *db = pParse->db;
122154 sizeof(IdList) + pList->nId*sizeof(pList->a));
122161 i = pList->nId++;
122162 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
122163 if( IN_RENAME_OBJECT && pList->a[i].zName ){
122164 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
122176 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
122177 for(i=0; i<pList->nId; i++){
122178 sqlite3DbFree(db, pList->a[i].zName);
122184 ** Return the index in pList of the identifier named zId. Return -1
122190 for(i=0; i<pList->nId; i++){
122191 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
122193 return -1;
122202 ** appropriate for small and memory-limited applications.
122231 int nExtra, /* Number of new slots to add to pSrc->a[] */
122232 int iStart /* Index in pSrc->a[] of first new slot */
122240 assert( iStart<=pSrc->nSrc );
122243 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
122245 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
122246 sqlite3 *db = pParse->db;
122248 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
122255 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
122257 assert( db->mallocFailed );
122261 pSrc->nAlloc = nAlloc;
122266 for(i=pSrc->nSrc-1; i>=iStart; i--){
122267 pSrc->a[i+nExtra] = pSrc->a[i];
122269 pSrc->nSrc += nExtra;
122272 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
122274 pSrc->a[i].iCursor = -1;
122327 assert( pParse->db!=0 );
122328 db = pParse->db;
122330 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
122332 pList->nAlloc = 1;
122333 pList->nSrc = 1;
122334 memset(&pList->a[0], 0, sizeof(pList->a[0]));
122335 pList->a[0].iCursor = -1;
122337 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
122345 pItem = &pList->a[pList->nSrc-1];
122346 if( pDatabase && pDatabase->z==0 ){
122350 pItem->zName = sqlite3NameFromToken(db, pDatabase);
122351 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
122353 pItem->zName = sqlite3NameFromToken(db, pTable);
122354 pItem->zDatabase = 0;
122365 assert( pList || pParse->db->mallocFailed );
122367 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
122368 if( pItem->iCursor>=0 ) continue;
122369 pItem->iCursor = pParse->nTab++;
122370 if( pItem->pSelect ){
122371 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
122385 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
122386 if( pItem->zDatabase ) sqlite3DbNNFreeNN(db, pItem->zDatabase);
122387 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
122388 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
122389 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
122390 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
122391 sqlite3DeleteTable(db, pItem->pTab);
122392 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
122393 if( pItem->fg.isUsing ){
122394 sqlite3IdListDelete(db, pItem->u3.pUsing);
122395 }else if( pItem->u3.pOn ){
122396 sqlite3ExprDelete(db, pItem->u3.pOn);
122408 ** pDatabase is NULL if the database name qualifier is missing - the
122423 Token *pAlias, /* The right-hand side of the AS subexpression */
122428 sqlite3 *db = pParse->db;
122429 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
122431 (pOnUsing->pOn ? "ON" : "USING")
122439 assert( p->nSrc>0 );
122440 pItem = &p->a[p->nSrc-1];
122442 assert( pItem->zName==0 || pDatabase!=0 );
122443 if( IN_RENAME_OBJECT && pItem->zName ){
122444 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
122445 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
122448 if( pAlias->n ){
122449 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
122452 pItem->pSelect = pSubquery;
122453 if( pSubquery->selFlags & SF_NestedFrom ){
122454 pItem->fg.isNestedFrom = 1;
122457 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
122458 assert( pItem->fg.isUsing==0 );
122460 pItem->u3.pOn = 0;
122461 }else if( pOnUsing->pUsing ){
122462 pItem->fg.isUsing = 1;
122463 pItem->u3.pUsing = pOnUsing->pUsing;
122465 pItem->u3.pOn = pOnUsing->pOn;
122478 ** element of the source-list passed as the second argument.
122482 if( p && pIndexedBy->n>0 ){
122484 assert( p->nSrc>0 );
122485 pItem = &p->a[p->nSrc-1];
122486 assert( pItem->fg.notIndexed==0 );
122487 assert( pItem->fg.isIndexedBy==0 );
122488 assert( pItem->fg.isTabFunc==0 );
122489 if( pIndexedBy->n==1 && !pIndexedBy->z ){
122492 pItem->fg.notIndexed = 1;
122494 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
122495 pItem->fg.isIndexedBy = 1;
122496 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
122507 assert( p1 && p1->nSrc==1 );
122509 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
122511 sqlite3SrcListDelete(pParse->db, p2);
122514 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
122515 sqlite3DbFree(pParse->db, p2);
122516 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
122524 ** table-valued-function.
122528 SrcItem *pItem = &p->a[p->nSrc-1];
122529 assert( pItem->fg.notIndexed==0 );
122530 assert( pItem->fg.isIndexedBy==0 );
122531 assert( pItem->fg.isTabFunc==0 );
122532 pItem->u1.pFuncArg = pList;
122533 pItem->fg.isTabFunc = 1;
122535 sqlite3ExprListDelete(pParse->db, pList);
122551 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
122556 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
122563 if( p && p->nSrc>1 ){
122564 int i = p->nSrc-1;
122567 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
122568 }while( (--i)>0 );
122569 p->a[0].fg.jointype = 0;
122574 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
122575 i--;
122578 p->a[i].fg.jointype |= JT_LTORJ;
122579 }while( (--i)>=0 );
122593 db = pParse->db;
122601 for(i=0; i<db->nDb; i++){
122603 Btree *pBt = db->aDb[i].pBt;
122628 assert( pParse->db!=0 );
122646 char *zName = sqlite3NameFromToken(pParse->db, pName);
122654 sqlite3DbFree(pParse->db, zName);
122666 sqlite3 *db = pParse->db;
122667 if( db->aDb[1].pBt==0 && !pParse->explain ){
122677 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
122681 pParse->rc = rc;
122684 db->aDb[1].pBt = pBt;
122685 assert( db->aDb[1].pSchema );
122686 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
122697 ** will occur at the end of the top-level VDBE and will be generated
122701 assert( iDb>=0 && iDb<pToplevel->db->nDb );
122702 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
122704 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
122705 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
122706 DbMaskSet(pToplevel->cookieMask, iDb);
122722 sqlite3 *db = pParse->db;
122724 for(i=0; i<db->nDb; i++){
122725 Db *pDb = &db->aDb[i];
122726 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
122748 DbMaskSet(pToplevel->writeMask, iDb);
122749 pToplevel->isMultiWrite |= setStatement;
122761 pToplevel->isMultiWrite = 1;
122782 pToplevel->mayAbort = 1;
122799 assert( pParse->pVdbe!=0 );
122801 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
122820 Table *pTab = pIdx->pTable;
122822 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
122823 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
122824 if( pIdx->aColExpr ){
122825 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
122827 for(j=0; j<pIdx->nKeyCol; j++){
122829 assert( pIdx->aiColumn[j]>=0 );
122830 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
122832 sqlite3_str_appendall(&errMsg, pTab->zName);
122846 ** Code an OP_Halt due to non-unique rowid.
122851 Table *pTab /* The table with the non-unique rowid */
122855 if( pTab->iPKey>=0 ){
122856 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
122857 pTab->aCol[pTab->iPKey].zCnName);
122860 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
122875 for(i=0; i<pIndex->nColumn; i++){
122876 const char *z = pIndex->azColl[i];
122877 assert( z!=0 || pIndex->aiColumn[i]<0 );
122878 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
122895 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
122897 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
122899 sqlite3RefillIndex(pParse, pIndex, -1);
122915 sqlite3 *db = pParse->db; /* The database connection */
122920 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
122922 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
122933 ** REINDEX -- 1
122934 ** REINDEX <collation> -- 2
122935 ** REINDEX ?<database>.?<tablename> -- 3
122936 ** REINDEX ?<database>.?<indexname> -- 4
122951 sqlite3 *db = pParse->db; /* The database connection */
122963 }else if( NEVER(pName2==0) || pName2->z==0 ){
122965 assert( pName1->z );
122966 zColl = sqlite3NameFromToken(pParse->db, pName1);
122980 zDb = db->aDb[iDb].zDbSName;
122991 sqlite3RefillIndex(pParse, pIndex, -1);
123006 int nCol = pIdx->nColumn;
123007 int nKey = pIdx->nKeyCol;
123009 if( pParse->nErr ) return 0;
123010 if( pIdx->uniqNotNull ){
123011 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
123013 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
123018 const char *zColl = pIdx->azColl[i];
123019 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
123021 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
123022 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
123024 if( pParse->nErr ){
123025 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
123026 if( pIdx->bNoQuery==0 ){
123031 ** the missing index using the collation-needed callback. For
123034 pIdx->bNoQuery = 1;
123035 pParse->rc = SQLITE_ERROR_RETRY;
123050 Token *pName, /* Name of the common-table */
123056 sqlite3 *db = pParse->db;
123059 assert( pNew!=0 || db->mallocFailed );
123061 if( db->mallocFailed ){
123065 pNew->pSelect = pQuery;
123066 pNew->pCols = pArglist;
123067 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
123068 pNew->eM10d = eM10d;
123079 sqlite3ExprListDelete(db, pCte->pCols);
123080 sqlite3SelectDelete(db, pCte->pSelect);
123081 sqlite3DbFree(db, pCte->zName);
123104 sqlite3 *db = pParse->db;
123114 zName = pCte->zName;
123117 for(i=0; i<pWith->nCte; i++){
123118 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
123125 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
123130 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
123132 if( db->mallocFailed ){
123136 pNew->a[pNew->nCte++] = *pCte;
123149 for(i=0; i<pWith->nCte; i++){
123150 cteClear(db, &pWith->a[i]);
123182 assert( !db->xCollNeeded || !db->xCollNeeded16 );
123183 if( db->xCollNeeded ){
123186 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
123190 if( db->xCollNeeded16 ){
123193 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
123196 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
123207 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
123212 char *z = pColl->zName;
123217 if( pColl2->xCmp!=0 ){
123219 pColl->xDel = 0; /* Do not copy the destructor */
123238 if( pColl && pColl->xCmp==0 ){
123239 const char *zName = pColl->zName;
123240 sqlite3 *db = pParse->db;
123259 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
123271 pColl = sqlite3HashFind(&db->aCollSeq, zName);
123285 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
123303 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
123328 if( pColl ) pColl += enc-1;
123330 pColl = db->pDfltColl;
123341 db->enc = enc;
123342 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
123345 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
123369 sqlite3 *db = pParse->db;
123375 if( !p || !p->xCmp ){
123382 if( p && !p->xCmp && synthCollSeq(db, p) ){
123385 assert( !p || p->xCmp );
123388 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
123414 sqlite3 *db = pParse->db;
123416 u8 initbusy = db->init.busy;
123420 if( !initbusy && (!pColl || !pColl->xCmp) ){
123433 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
123434 ** is also -1. In other words, we are searching for a function that
123437 ** If nArg is -2 that means that we are searching for any function
123447 ** 4: UTF8/16 conversion required - argument count matches exactly
123448 ** 5: UTF16 byte order conversion required - argument count matches exactly
123451 ** If nArg==(-2) then any function with a non-null xSFunc is
123453 ** a non-match.
123458 int nArg, /* Desired number of arguments. (-1)==any */
123462 assert( p->nArg>=-1 );
123465 if( p->nArg!=nArg ){
123466 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
123467 if( p->nArg>=0 ) return 0;
123472 if( p->nArg==nArg ){
123479 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
123481 }else if( (enc & p->funcFlags & 2)!=0 ){
123497 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
123498 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
123499 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
123522 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
123523 aDef[i].pNext = pOther->pNext;
123524 pOther->pNext = &aDef[i];
123537 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
123545 ** If nArg is -2, then the first valid function found is returned. A
123546 ** function is valid if xSFunc is non-zero. The nArg==(-2)
123548 ** of arguments. If nArg is -2, then createFlag must be 0.
123556 const char *zName, /* Name of the function. zero-terminated */
123557 int nArg, /* Number of arguments. -1 means any number */
123567 assert( nArg>=(-2) );
123568 assert( nArg>=(-1) || createFlag==0 );
123571 /* First search for a match amongst the application-defined functions.
123573 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
123580 p = p->pNext;
123583 /* If no match is found, search the built-in functions.
123585 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
123586 ** functions even if a prior app-defined function was found. And give
123587 ** priority to built-in functions.
123592 ** new function. But the FuncDefs for built-in functions are read-only.
123593 ** So we must not search for built-ins when creating a new function.
123595 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
123605 p = p->pNext;
123617 pBest->zName = (const char*)&pBest[1];
123618 pBest->nArg = (u16)nArg;
123619 pBest->funcFlags = enc;
123621 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
123622 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
123628 pBest->pNext = pOther;
123632 if( pBest && (pBest->xSFunc || createFlag) ){
123654 temp1 = pSchema->tblHash;
123655 temp2 = pSchema->trigHash;
123656 sqlite3HashInit(&pSchema->trigHash);
123657 sqlite3HashClear(&pSchema->idxHash);
123662 sqlite3HashInit(&pSchema->tblHash);
123668 sqlite3HashClear(&pSchema->fkeyHash);
123669 pSchema->pSeqTab = 0;
123670 if( pSchema->schemaFlags & DB_SchemaLoaded ){
123671 pSchema->iGeneration++;
123673 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
123689 }else if ( 0==p->file_format ){
123690 sqlite3HashInit(&p->tblHash);
123691 sqlite3HashInit(&p->idxHash);
123692 sqlite3HashInit(&p->trigHash);
123693 sqlite3HashInit(&p->fkeyHash);
123694 p->enc = SQLITE_UTF8;
123727 ** pSrc->a[0].pTab Pointer to the Table object
123728 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
123732 SrcItem *pItem = pSrc->a;
123734 assert( pItem && pSrc->nSrc>=1 );
123736 sqlite3DeleteTable(pParse->db, pItem->pTab);
123737 pItem->pTab = pTab;
123739 pTab->nTabRef++;
123740 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
123747 /* Generate byte-code that will report the number of rows modified
123757 /* Return true if table pTab is read-only.
123759 ** A table is read-only if any of the following are true:
123774 ** is for a top-level SQL statement.
123777 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
123784 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
123787 if( pParse->pToplevel!=0
123788 && pTab->u.vtab.p->eVtabRisk >
123789 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
123792 pTab->zName);
123801 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
123802 db = pParse->db;
123803 if( (pTab->tabFlags & TF_Readonly)!=0 ){
123804 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
123806 assert( pTab->tabFlags & TF_Shadow );
123813 ** If pTab is not writable -> generate an error message and return 1.
123814 ** If pTab is writable but other errors have occurred -> return 1.
123815 ** If pTab is writable and no prior errors -> return 0;
123819 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
123824 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
123849 sqlite3 *db = pParse->db;
123850 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
123854 assert( pFrom->nSrc==1 );
123855 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
123856 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
123857 assert( pFrom->a[0].fg.isUsing==0 );
123858 assert( pFrom->a[0].u3.pOn==0 );
123879 SrcList *pSrc, /* the FROM clause -- which tables to scan */
123885 sqlite3 *db = pParse->db;
123897 sqlite3ExprDelete(pParse->db, pWhere);
123898 sqlite3ExprListDelete(pParse->db, pOrderBy);
123918 pTab = pSrc->a[0].pTab;
123926 if( pPk->nKeyCol==1 ){
123927 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
123932 for(i=0; i<pPk->nKeyCol; i++){
123933 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
123938 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
123945 pSrc->a[0].pTab = 0;
123947 pSrc->a[0].pTab = pTab;
123948 if( pSrc->a[0].fg.isIndexedBy ){
123949 assert( pSrc->a[0].fg.isCte==0 );
123950 pSrc->a[0].u2.pIBIndex = 0;
123951 pSrc->a[0].fg.isIndexedBy = 0;
123952 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
123953 }else if( pSrc->a[0].fg.isCte ){
123954 pSrc->a[0].u2.pCteUse->nUse++;
124021 db = pParse->db;
124022 assert( db->pParse==pParse );
124023 if( pParse->nErr ){
124026 assert( db->mallocFailed==0 );
124027 assert( pTabList->nSrc==1 );
124056 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
124080 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
124081 assert( iDb<db->nDb );
124082 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
124083 db->aDb[iDb].zDbSName);
124092 assert( pTabList->nSrc==1 );
124093 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
124094 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
124095 pParse->nTab++;
124101 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
124110 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
124139 if( (db->flags & SQLITE_CountRows)!=0
124140 && !pParse->nested
124141 && !pParse->pTriggerTab
124142 && !pParse->bReturning
124144 memCnt = ++pParse->nMem;
124155 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
124165 && db->xPreUpdateCallback==0
124169 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
124171 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
124172 pTab->zName, P4_STATIC);
124174 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
124175 assert( pIdx->pSchema==pTab->pSchema );
124177 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
124179 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
124192 iRowSet = ++pParse->nMem;
124199 nPk = pPk->nKeyCol;
124200 iPk = pParse->nMem+1;
124201 pParse->nMem += nPk;
124202 iEphCur = pParse->nTab++;
124211 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
124212 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
124213 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
124233 assert( pPk->aiColumn[i]>=0 );
124235 pPk->aiColumn[i], iPk+i);
124239 iKey = ++pParse->nMem;
124240 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
124244 /* For ONEPASS, no need to store the rowid/primary-key. There is only
124255 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
124256 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
124262 iKey = ++pParse->nMem;
124265 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
124295 /* Set up a loop over the rowids/primary-keys that were found in the
124296 ** where-clause loop above.
124300 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
124329 pParse->isMultiWrite = 0;
124337 int count = (pParse->nested==0); /* True to count changes */
124342 /* End of the loop over all rowids/primary-keys. */
124353 } /* End non-truncate path */
124359 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
124405 ** cursor number iIdxCur+i for the i-th index.
124441 u8 count, /* If non-zero, increment the row change counter */
124446 Vdbe *v = pParse->pVdbe; /* Vdbe */
124480 iOld = pParse->nMem+1;
124481 pParse->nMem += (1 + pTab->nCol);
124483 /* Populate the OLD.* pseudo-table register array. These values will be
124486 for(iCol=0; iCol<pTab->nCol; iCol++){
124514 iIdxNoSeek = -1;
124527 ** If variable 'count' is non-zero, then this OP_Delete instruction should
124528 ** invoke the update-hook. The pre-update-hook, on the other hand should
124530 ** the update-hook is not invoked for rows removed by REPLACE, but the
124531 ** pre-update-hook is.
124537 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
124579 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
124580 ** index is the 0-th index.)
124594 int r1 = -1; /* Register holding an index key */
124601 v = pParse->pVdbe;
124603 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
124608 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
124612 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
124660 Vdbe *v = pParse->pVdbe;
124666 if( pIdx->pPartIdxWhere ){
124668 pParse->iSelfTab = iDataCur + 1;
124669 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
124671 pParse->iSelfTab = 0;
124672 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
124678 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
124680 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
124683 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
124684 && pPrior->aiColumn[j]!=XN_EXPR
124690 if( pIdx->aiColumn[j]>=0 ){
124708 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
124714 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
124731 ** This file contains the C-language implementations for many of the SQL
124748 assert( context->pVdbe!=0 );
124749 pOp = &context->pVdbe->aOp[context->iOp-1];
124750 assert( pOp->opcode==OP_CollSeq );
124751 assert( pOp->p4type==P4_COLLSEQ );
124752 return pOp->p4.pColl;
124760 assert( context->isError<=0 );
124761 context->isError = -1;
124762 context->skipFlag = 1;
124766 ** Implementation of the non-aggregate min() and max() functions
124779 mask = sqlite3_user_data(context)==0 ? 0 : -1;
124782 assert( mask==-1 || mask==0 );
124804 int i = sqlite3_value_type(argv[0]) - 1;
124812 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
124816 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
124861 sqlite3_result_int(context, (int)(z-z0));
124874 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
124885 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
124887 ** equivalent positive 64-bit two complement value. */
124888 sqlite3_result_error(context, "integer overflow", -1);
124891 iVal = -iVal;
124897 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
124904 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
124908 if( rVal<0 ) rVal = -rVal;
124975 nHaystack--;
125006 x.nArg = argc-1;
125009 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
125022 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
125023 ** of x. If x is text, then we actually count UTF-8 characters.
125067 ** as substr(X,1,N) - it returns the first N characters of X. This
125068 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
125069 ** from 2009-02-02 for compatibility of applications that exploited the
125076 p2 = -p2;
125080 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
125090 p1--;
125092 p2--;
125095 p1 -= p2;
125105 p1--;
125107 for(z2=z; *z2 && p2; p2--){
125110 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
125114 p2 = len-p1;
125138 /* If Y==0 and X will fit in a 64-bit int,
125142 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
125145 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
125170 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
125171 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
125172 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
125234 #define noopFunc versionFunc /* Substitute function - never called */
125249 ** (or -9223372036854775808) since when you do abs() of that
125254 ** therefore be no less than -9223372036854775807.
125256 r = -(r & LARGEST_INT64);
125296 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
125305 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
125330 /* IMP: R-11217-42568 This function is a wrapper around the
125336 ** A structure defining how to do GLOB-style comparisons.
125359 /* The correct SQL-92 behavior is for the LIKE operator to ignore
125374 ** Compare two UTF-8 strings for equality where the first string is
125394 ** range of characters can be specified using '-'. Example:
125395 ** "[a-z]" matches any single lower-case letter. To match a '-', make
125418 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
125419 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
125420 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
125437 if( pInfo->matchSet==0 ){
125443 assert( matchOther<0x80 ); /* '[' is a single-byte character */
125445 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
125458 ** For a case-insensitive search, set variable cx to be the same as
125491 if( pInfo->matchSet==0 ){
125511 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
125542 ** non-zero if there is no match.
125555 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
125556 ** a miss - like strcmp().
125580 ** the build-in LIKE operator. The first argument to the function is the
125618 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
125619 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
125620 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
125621 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
125625 /* The escape character string must consist of a single UTF-8 character.
125630 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
125632 "ESCAPE expression must be a single character", -1);
125636 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
125639 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
125640 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
125643 escape = pInfo->matchSet;
125683 /* IMP: R-48699-48617 This function is an SQL wrapper around the
125684 ** sqlite3_libversion() C-interface. */
125685 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
125699 /* IMP: R-24470-31136 This function is an SQL wrapper around the
125701 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
125707 ** its side-effects.
125733 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
125757 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
125761 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
125765 /* Array for converting from half-bytes (nybbles) into ASCII hex
125780 assert( pStr!=0 && pStr->nChar==0 );
125790 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
125807 if( pStr->accError==0 ){
125808 char *zText = pStr->zText;
125818 pStr->nChar = nBlob*2 + 3;
125840 ** Strings are surrounded by single-quotes with escapes on interior quotes
125850 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
125861 ** The unicode() function. Return the integer unicode code-point value
125913 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
125946 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
125959 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
125998 || sqlite3_context_db_handle(context)->mallocFailed );
126018 loopLimit = nStr - nPattern;
126025 nOut += nRep - nPattern;
126026 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
126027 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
126028 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
126034 if( (cntExpand&(cntExpand-1))==0 ){
126039 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
126049 i += nPattern-1;
126052 assert( j+nStr-i+1<=nOut );
126053 memcpy(&zOut[j], &zStr[i], nStr-i);
126054 j += nStr - i;
126109 aLen[nChar] = (unsigned)(z - azChar[nChar]);
126124 nIn -= len;
126132 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
126135 nIn -= len;
126150 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
126151 ** When the "sqlite3" command-line shell is built using this functionality,
126153 ** involving application-defined functions to be examined in a generic
126161 /* no-op */
126166 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
126167 ** is only available if the SQLITE_SOUNDEX compile-time option is used
126174 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
126219 /* IMP: R-64894-50321 The string "?000" is returned if the argument
126228 ** A function that loads a shared-library extension then returns NULL.
126239 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
126240 sqlite3_result_error(context, "not authorized", -1);
126250 sqlite3_result_error(context, zErrMsg, -1);
126267 u8 approx; /* True if non-integer value was input to the sum */
126288 p->cnt++;
126291 p->rSum += v;
126292 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
126293 p->approx = p->overflow = 1;
126296 p->rSum += sqlite3_value_double(argv[0]);
126297 p->approx = 1;
126309 /* p is always non-NULL because sumStep() will have been called first
126312 assert( p->cnt>0 );
126313 p->cnt--;
126314 assert( type==SQLITE_INTEGER || p->approx );
126315 if( type==SQLITE_INTEGER && p->approx==0 ){
126317 p->rSum -= v;
126318 p->iSum -= v;
126320 p->rSum -= sqlite3_value_double(argv[0]);
126330 if( p && p->cnt>0 ){
126331 if( p->overflow ){
126332 sqlite3_result_error(context,"integer overflow",-1);
126333 }else if( p->approx ){
126334 sqlite3_result_double(context, p->rSum);
126336 sqlite3_result_int64(context, p->iSum);
126343 if( p && p->cnt>0 ){
126344 sqlite3_result_double(context, p->rSum/(double)p->cnt);
126351 sqlite3_result_double(context, p ? p->rSum : (double)0);
126373 p->n++;
126380 ** expressed as a 32-bit integer. */
126381 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
126382 || p->n==sqlite3_aggregate_count(context) );
126388 sqlite3_result_int64(context, p ? p->n : 0);
126394 /* p is always non-NULL since countStep() will have been called first */
126396 p->n--;
126398 p->bInverse = 1;
126422 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
126423 }else if( pBest->flags ){
126430 ** sqlite3_user_data() function returns (void *)-1. For min() it
126443 pBest->db = sqlite3_context_db_handle(context);
126451 if( pRes->flags ){
126482 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
126484 ** (Hence, its slots in use number nAccum-1 between method calls.)
126505 int firstTerm = pGCC->str.mxAlloc==0;
126506 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
126509 sqlite3_str_appendchar(&pGCC->str, 1, ',');
126513 pGCC->nFirstSepLength = 1;
126520 sqlite3_str_append(&pGCC->str, zSep, nSep);
126526 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
126527 int *pnsl = pGCC->pnSepLengths;
126530 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
126532 int i = 0, nA = pGCC->nAccum-1;
126533 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
126536 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
126539 if( ALWAYS(pGCC->nAccum>0) ){
126540 pnsl[pGCC->nAccum-1] = nSep;
126542 pGCC->pnSepLengths = pnsl;
126544 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
126551 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
126553 pGCC->nAccum += 1;
126557 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
126572 /* pGCC is always non-NULL since groupConcatStep() will have always
126580 pGCC->nAccum -= 1;
126581 if( pGCC->pnSepLengths!=0 ){
126582 assert(pGCC->nAccum >= 0);
126583 if( pGCC->nAccum>0 ){
126584 nVS += *pGCC->pnSepLengths;
126585 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
126586 (pGCC->nAccum-1)*sizeof(int));
126589 /* If removing single accumulated string, harmlessly over-do. */
126590 nVS += pGCC->nFirstSepLength;
126592 if( nVS>=(int)pGCC->str.nChar ){
126593 pGCC->str.nChar = 0;
126595 pGCC->str.nChar -= nVS;
126596 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
126598 if( pGCC->str.nChar==0 ){
126599 pGCC->str.mxAlloc = 0;
126600 sqlite3_free(pGCC->pnSepLengths);
126601 pGCC->pnSepLengths = 0;
126612 sqlite3ResultStrAccum(context, &pGCC->str);
126614 sqlite3_free(pGCC->pnSepLengths);
126623 StrAccum *pAccum = &pGCC->str;
126624 if( pAccum->accError==SQLITE_TOOBIG ){
126626 }else if( pAccum->accError==SQLITE_NOMEM ){
126630 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
126639 ** This routine does per-connection function registration. Most
126640 ** of the built-in functions above are part of the global function set.
126662 ** Re-register the built-in LIKE functions. The caseSensitive
126678 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
126679 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
126687 ** LIKE-style function then return FALSE.
126703 assert( pExpr->op==TK_FUNCTION );
126705 if( !pExpr->x.pList ){
126708 nExpr = pExpr->x.pList->nExpr;
126710 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
126714 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
126722 memcpy(aWc, pDef->pUserData, 3);
126730 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
126732 if( pEscape->op!=TK_STRING ) return 0;
126734 zEscape = pEscape->u.zToken;
126741 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
126757 /* Extra math functions that require linking with -lm
126803 ** ln(X) - natural logarithm
126804 ** log(X) - log X base 10
126805 ** log10(X) - log X base 10
126806 ** log(B,X) - log X base B
126862 ** Implementation of 1-argument SQL math functions:
126864 ** exp(X) - Compute e to the X-th power
126884 ** Implementation of 2-argument SQL math functions:
126886 ** power(X,Y) - Compute X to the Y-th power
126909 ** Implementation of 0-argument pi() function.
126937 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
126942 ** to the global function hash table. This occurs at start-time (as
126953 ** FuncDef.pHash elements at start-time. The elements of this array
126954 ** are read-only after initialization is complete.
126993 FUNCTION(min, -1, 0, 1, minmaxFunc ),
126997 FUNCTION(max, -1, 1, 1, minmaxFunc ),
127005 FUNCTION(printf, -1, 0, 0, printfFunc ),
127006 FUNCTION(format, -1, 0, 0, printfFunc ),
127008 FUNCTION(char, -1, 0, 0, charFunc ),
127056 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
127097 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
127108 #if 0 /* Enable to print out how the built-in functions are hashed */
127113 printf("FUNC-HASH %02d:", i);
127114 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
127115 int n = sqlite3Strlen30(p->zName);
127116 int h = p->zName[0] + n;
127117 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
127118 printf(" %s(%d)", p->zName, h);
127148 ** --------------------------
127230 ** ---------------
127232 ** Before coding an UPDATE or DELETE row operation, the code-generator
127237 ** accessed). No information is required by the code-generator before
127241 ** sqlite3FkRequired() - Test to see if FK processing is required.
127242 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
127246 ** --------------------------------------
127248 ** sqlite3FkCheck() - Check for foreign key violations.
127249 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
127250 ** sqlite3FkDelete() - Delete an FKey structure.
127255 ** -----------------------
127285 ** constraint to the parent table column stored in the left-most column
127287 ** child table column that corresponds to the second left-most column of
127306 ** then non-zero is returned, and a "foreign key mismatch" error loaded
127307 ** into pParse. If an OOM error occurs, non-zero is returned and the
127308 ** pParse->db->mallocFailed flag is set.
127319 int nCol = pFKey->nCol; /* Number of columns in parent key */
127320 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
127327 /* If this is a non-composite (single column) foreign key, check if it
127333 ** Non-composite foreign keys do not require the aiCol array.
127343 if( pParent->iPKey>=0 ){
127345 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
127351 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
127356 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
127357 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
127369 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
127374 /* If zKey is non-NULL, then this foreign key was declared to
127380 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
127389 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
127391 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
127393 zIdxCol = pParent->aCol[iCol].zCnName;
127395 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
127396 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
127408 if( !pParse->disableTriggers ){
127410 "foreign key mismatch - \"%w\" referencing \"%w\"",
127411 pFKey->pFrom->zName, pFKey->zTo);
127413 sqlite3DbFree(pParse->db, aiCol);
127425 ** affected - once to "delete" the old row, and then again to "insert" the
127435 ** --------------------------------------------------------------------------
127460 int iCur = pParse->nTab - 1; /* Cursor number to use */
127464 (!pFKey->isDeferred
127465 && !(pParse->db->flags & SQLITE_DeferFKs)
127466 && !pParse->pToplevel
127467 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
127477 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
127480 for(i=0; i<pFKey->nCol; i++){
127481 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
127498 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
127503 ** to increment the constraint-counter (i.e. this is an INSERT operation),
127505 ** increment the constraint-counter. */
127506 if( pTab==pFKey->pFrom && nIncr==1 ){
127514 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
127518 int nCol = pFKey->nCol;
127521 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
127525 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
127530 ** to increment the constraint-counter (i.e. this is an INSERT operation),
127532 ** increment the constraint-counter.
127534 ** If any of the parent-key values are NULL, then the row cannot match
127536 ** of the parent-key values are NULL (at this point it is known that
127539 if( pTab==pFKey->pFrom && nIncr==1 ){
127542 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
127545 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
127546 pIdx->aiColumn[i]);
127547 assert( pIdx->aiColumn[i]>=0 );
127548 assert( aiCol[i]!=pTab->iPKey );
127549 if( pIdx->aiColumn[i]==pTab->iPKey ){
127560 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
127567 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
127568 && !pParse->pToplevel
127569 && !pParse->isMultiWrite
127579 if( nIncr>0 && pFKey->isDeferred==0 ){
127582 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
127607 sqlite3 *db = pParse->db;
127611 if( iCol>=0 && iCol!=pTab->iPKey ){
127612 pCol = &pTab->aCol[iCol];
127613 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
127614 pExpr->affExpr = pCol->affinity;
127616 if( zColl==0 ) zColl = db->pDfltColl->zName;
127619 pExpr->iTable = regBase;
127620 pExpr->affExpr = SQLITE_AFF_INTEGER;
127639 pExpr->y.pTab = pTab;
127640 pExpr->iTable = iCursor;
127641 pExpr->iColumn = iCol;
127650 ** code for an SQL UPDATE operation, this function may be called twice -
127653 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
127662 ** --------------------------------------------------------------------------
127684 sqlite3 *db = pParse->db; /* Database handle */
127692 assert( pIdx==0 || pIdx->pTable==pTab );
127693 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
127694 assert( pIdx!=0 || pFKey->nCol==1 );
127698 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
127704 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
127710 for(i=0; i<pFKey->nCol; i++){
127717 iCol = pIdx ? pIdx->aiColumn[i] : -1;
127719 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
127721 zCol = pFKey->pFrom->aCol[iCol].zCnName;
127741 if( pTab==pFKey->pFrom && nIncr>0 ){
127746 pLeft = exprTableRegister(pParse, pTab, regData, -1);
127747 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
127752 for(i=0; i<pIdx->nKeyCol; i++){
127753 i16 iCol = pIdx->aiColumn[i];
127756 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
127774 if( pParse->nErr==0 ){
127776 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
127804 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
127810 ** and all of its sub-components.
127812 ** The Trigger structure or any of its sub-components may be allocated from
127817 TriggerStep *pStep = p->step_list;
127818 sqlite3ExprDelete(dbMem, pStep->pWhere);
127819 sqlite3ExprListDelete(dbMem, pStep->pExprList);
127820 sqlite3SelectDelete(dbMem, pStep->pSelect);
127821 sqlite3ExprDelete(dbMem, p->pWhen);
127833 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
127838 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
127839 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
127840 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
127863 sqlite3 *db = pParse->db;
127864 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
127877 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
127878 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
127885 pParse->disableTriggers = 1;
127887 pParse->disableTriggers = 0;
127898 if( (db->flags & SQLITE_DeferFKs)==0 ){
127919 ** is set to -1). If the rowid column is modified by the UPDATE statement
127920 ** the bChngRowid argument is non-zero.
127932 for(i=0; i<p->nCol; i++){
127933 int iChildKey = p->aCol[i].iFrom;
127935 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
127946 ** is set to -1). If the rowid column is modified by the UPDATE statement
127947 ** the bChngRowid argument is non-zero.
127959 for(i=0; i<p->nCol; i++){
127960 char *zKey = p->aCol[i].zCol;
127962 for(iKey=0; iKey<pTab->nCol; iKey++){
127963 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
127964 Column *pCol = &pTab->aCol[iKey];
127966 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
127967 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
127983 if( pTop->pTriggerPrg ){
127984 Trigger *p = pTop->pTriggerPrg->pTrigger;
127985 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
127986 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
128000 ** first register in an array of (pTab->nCol+1) registers containing the
128006 ** first register of an array of (pTab->nCol+1) registers containing the new
128022 sqlite3 *db = pParse->db; /* Database handle */
128026 int isIgnoreErrors = pParse->disableTriggers;
128028 /* Exactly one of regOld and regNew should be non-zero. */
128031 /* If foreign-keys are disabled, this function is a no-op. */
128032 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
128035 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128036 zDb = db->aDb[iDb].zDbSName;
128040 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
128050 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
128060 if( pParse->disableTriggers ){
128061 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
128063 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
128067 if( !isIgnoreErrors || db->mallocFailed ) return;
128074 ** FK counter for each row of the current table with non-NULL keys.
128077 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
128078 for(i=0; i<pFKey->nCol; i++){
128080 iFromCol = pFKey->aCol[i].iFrom;
128081 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
128084 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
128088 assert( pFKey->nCol==1 || (aiFree && pIdx) );
128093 iCol = pFKey->aCol[0].iFrom;
128096 for(i=0; i<pFKey->nCol; i++){
128097 if( aiCol[i]==pTab->iPKey ){
128098 aiCol[i] = -1;
128100 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
128105 if( db->xAuth ){
128107 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
128108 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
128114 /* Take a shared-cache advisory read-lock on the parent table. Allocate
128117 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
128118 pParse->nTab++;
128124 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
128143 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
128152 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
128153 && !pParse->pToplevel && !pParse->isMultiWrite
128162 if( !isIgnoreErrors || db->mallocFailed ) return;
128165 assert( aiCol || pFKey->nCol==1 );
128171 SrcItem *pItem = pSrc->a;
128172 pItem->pTab = pFKey->pFrom;
128173 pItem->zName = pFKey->pFrom->zName;
128174 pItem->pTab->nTabRef++;
128175 pItem->iCursor = pParse->nTab++;
128178 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
128181 int eAction = pFKey->aAction[aChange!=0];
128186 ** So do not set the "may-abort" flag in this case.
128189 ** may-abort flag will eventually be set on this statement anyway
128200 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
128204 pItem->zName = 0;
128222 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
128225 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128226 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
128228 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
128232 for(i=0; i<pIdx->nKeyCol; i++){
128233 assert( pIdx->aiColumn[i]>=0 );
128234 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
128249 ** entry in the aChange[] array is set to -1. If the column is modified,
128254 ** non-zero. If there is no foreign key related processing, this function
128270 int *aChange, /* Non-NULL for UPDATE operations */
128275 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
128280 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
128287 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
128289 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
128295 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
128297 if( p->aAction[1]!=OE_None ) return 2;
128308 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
128316 ** require no special handling by the triggers sub-system, code for them is
128339 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
128341 sqlite3 *db = pParse->db; /* Database handle */
128346 action = pFKey->aAction[iAction];
128347 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
128350 pTrigger = pFKey->apTrigger[iAction];
128356 int *aiCol = 0; /* child table cols -> parent key cols */
128365 assert( aiCol || pFKey->nCol==1 );
128367 for(i=0; i<pFKey->nCol; i++){
128375 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
128377 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
128378 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
128380 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
128381 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
128419 Column *pCol = pFKey->pFrom->aCol + iFromCol;
128421 if( pCol->colFlags & COLFLAG_GENERATED ){
128422 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
128423 testcase( pCol->colFlags & COLFLAG_STORED );
128426 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
128442 zFrom = pFKey->pFrom->zName;
128446 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128453 tDb.z = db->aDb[iDb].zDbSName;
128458 pRaise->affExpr = OE_Abort;
128475 nFrom + 1 /* Space for pStep->zTarget */
128478 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
128479 pStep->zTarget = (char *)&pStep[1];
128480 memcpy((char *)pStep->zTarget, zFrom, nFrom);
128482 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
128483 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
128484 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
128487 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
128491 /* Re-enable the lookaside buffer, if it was disabled earlier. */
128498 if( db->mallocFailed==1 ){
128507 pStep->op = TK_SELECT;
128511 pStep->op = TK_DELETE;
128516 pStep->op = TK_UPDATE;
128518 pStep->pTrig = pTrigger;
128519 pTrigger->pSchema = pTab->pSchema;
128520 pTrigger->pTabSchema = pTab->pSchema;
128521 pFKey->apTrigger[iAction] = pTrigger;
128522 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
128535 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
128540 /* If foreign-key support is enabled, iterate through all FKs that
128543 ** trigger sub-program. */
128544 if( pParse->db->flags&SQLITE_ForeignKeys ){
128546 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
128566 FKey *pNext; /* Copy of pFKey->pNextFrom */
128570 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
128571 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
128574 if( db->pnBytesFreed==0 ){
128575 if( pFKey->pPrevTo ){
128576 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
128578 void *p = (void *)pFKey->pNextTo;
128579 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
128580 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
128582 if( pFKey->pNextTo ){
128583 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
128587 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
128590 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
128594 fkTriggerDelete(db, pFKey->apTrigger[0]);
128595 fkTriggerDelete(db, pFKey->apTrigger[1]);
128598 pNext = pFKey->pNextFrom;
128640 assert( pParse->pVdbe!=0 );
128641 v = pParse->pVdbe;
128643 sqlite3TableLock(pParse, iDb, pTab->tnum,
128644 (opcode==OP_OpenWrite)?1:0, pTab->zName);
128646 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
128647 VdbeComment((v, "%s", pTab->zName));
128651 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
128652 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
128654 VdbeComment((v, "%s", pTab->zName));
128664 ** ------------------------------
128679 if( !pIdx->zColAff ){
128689 Table *pTab = pIdx->pTable;
128690 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
128691 if( !pIdx->zColAff ){
128695 for(n=0; n<pIdx->nColumn; n++){
128696 i16 x = pIdx->aiColumn[n];
128699 aff = pTab->aCol[x].affinity;
128704 assert( pIdx->bHasExpr );
128705 assert( pIdx->aColExpr!=0 );
128706 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
128710 pIdx->zColAff[n] = aff;
128712 pIdx->zColAff[n] = 0;
128715 return pIdx->zColAff;
128725 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
128728 for(i=j=0; i<pTab->nCol; i++){
128729 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
128730 zColAff[j++] = pTab->aCol[i].affinity;
128734 zColAff[j--] = 0;
128744 ** For ordinary (legacy, non-strict) tables:
128745 ** -----------------------------------------
128751 ** which were then optimized out) then this routine becomes a no-op.
128761 ** --------- ---------------
128769 ** ------------------
128783 if( pTab->tabFlags & TF_Strict ){
128792 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
128793 pPrev->opcode = OP_TypeCheck;
128794 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
128797 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
128802 zColAff = pTab->zColAff;
128809 pTab->zColAff = zColAff;
128817 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
128818 || sqlite3VdbeDb(v)->mallocFailed );
128819 sqlite3VdbeChangeP4(v, -1, zColAff, i);
128825 ** Return non-zero if the table pTab in database iDb or any of its indices
128835 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
128841 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
128843 Pgno tnum = pOp->p2;
128844 if( tnum==pTab->tnum ){
128847 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
128848 if( tnum==pIndex->tnum ){
128854 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
128855 assert( pOp->p4.pVtab!=0 );
128856 assert( pOp->p4type==P4_VTAB );
128868 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
128869 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
128870 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
128894 assert( pTab->tabFlags & TF_HasGenerated );
128895 testcase( pTab->tabFlags & TF_HasVirtual );
128896 testcase( pTab->tabFlags & TF_HasStored );
128901 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
128902 if( (pTab->tabFlags & TF_HasStored)!=0 ){
128903 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
128904 if( pOp->opcode==OP_Affinity ){
128906 ** columns. '@' is the no-op affinity and those columns have not
128909 char *zP4 = pOp->p4.z;
128911 assert( pOp->p4type==P4_DYNAMIC );
128913 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
128916 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
128921 }else if( pOp->opcode==OP_TypeCheck ){
128925 pOp->p3 = 1;
128930 ** this is a two-pass algorithm. On the first pass, mark all generated
128933 for(i=0; i<pTab->nCol; i++){
128934 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
128935 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
128936 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
128937 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
128946 /* On the second pass, compute the value of each NOT-AVAILABLE column.
128951 pParse->iSelfTab = -iRegStore;
128955 for(i=0; i<pTab->nCol; i++){
128956 Column *pCol = pTab->aCol + i;
128957 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
128959 pCol->colFlags |= COLFLAG_BUSY;
128962 pCol->colFlags &= ~COLFLAG_BUSY;
128968 assert( pCol->colFlags & COLFLAG_GENERATED );
128971 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
128976 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
128978 pParse->iSelfTab = 0;
129013 assert( pParse->db->aDb[iDb].pSchema!=0 );
129014 if( (pTab->tabFlags & TF_Autoincrement)!=0
129015 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
129019 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
129023 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
129027 || pSeqTab->nCol!=2
129029 pParse->nErr++;
129030 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
129034 pInfo = pToplevel->pAinc;
129035 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
129037 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
129039 testcase( pParse->earlyCleanup );
129040 if( pParse->db->mallocFailed ) return 0;
129041 pInfo->pNext = pToplevel->pAinc;
129042 pToplevel->pAinc = pInfo;
129043 pInfo->pTab = pTab;
129044 pInfo->iDb = iDb;
129045 pToplevel->nMem++; /* Register to hold name of table */
129046 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
129047 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
129049 memId = pInfo->regCtr;
129060 sqlite3 *db = pParse->db; /* The database connection */
129063 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
129065 /* This routine is never called during trigger-generation. It is
129066 ** only called from the top-level */
129067 assert( pParse->pTriggerTab==0 );
129071 for(p = pParse->pAinc; p; p = p->pNext){
129088 pDb = &db->aDb[p->iDb];
129089 memId = p->regCtr;
129090 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129091 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
129092 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
129098 aOp[3].p1 = memId-1;
129107 if( pParse->nTab==0 ) pParse->nTab = 1;
129121 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
129134 Vdbe *v = pParse->pVdbe;
129135 sqlite3 *db = pParse->db;
129138 for(p = pParse->pAinc; p; p = p->pNext){
129148 Db *pDb = &db->aDb[p->iDb];
129150 int memId = p->regCtr;
129153 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
129156 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
129161 aOp[2].p1 = memId-1;
129170 if( pParse->pAinc ) autoIncrementEnd(pParse);
129175 ** above are all no-ops
129199 ** then a list of all (non-hidden) columns for the table is substituted.
129204 ** first two forms shown above. A VALUES clause is really just short-hand
129210 ** insert with data coming from a single-row VALUES clause, the code executes
129211 ** once straight down through. Pseudo-code follows (we call this
129223 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
129247 ** X <- A
129255 ** end-coroutine X
129268 ** X <- A
129276 ** end co-routine R
129305 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
129334 db = pParse->db;
129335 assert( db->pParse==pParse );
129336 if( pParse->nErr ){
129339 assert( db->mallocFailed==0 );
129346 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
129347 pList = pSelect->pEList;
129348 pSelect->pEList = 0;
129355 assert( pTabList->nSrc==1 );
129360 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129361 assert( iDb<db->nDb );
129362 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
129363 db->aDb[iDb].zDbSName) ){
129388 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
129394 ** ViewGetColumnNames() is a no-op if pTab is not a view.
129400 /* Cannot insert into a read-only table.
129410 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
129442 regRowid = regIns = pParse->nMem+1;
129443 pParse->nMem += pTab->nCol + 1;
129446 pParse->nMem++;
129459 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
129460 ** loop, if ipkColumn==(-1), that means that integer primary key
129469 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
129471 assert( pColumn->eU4!=EU4_EXPR );
129472 pColumn->eU4 = EU4_IDX;
129473 for(i=0; i<pColumn->nId; i++){
129474 pColumn->a[i].u4.idx = -1;
129476 for(i=0; i<pColumn->nId; i++){
129477 for(j=0; j<pTab->nCol; j++){
129478 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
129479 pColumn->a[i].u4.idx = j;
129481 if( j==pTab->iPKey ){
129485 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
129488 pTab->aCol[j].zCnName);
129495 if( j>=pTab->nCol ){
129496 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
129501 pTabList->a, pColumn->a[i].zName);
129502 pParse->checkSchema = 1;
129510 ** is coming from a SELECT statement, then generate a co-routine that
129512 ** co-routine is the common header to the 3rd and 4th templates.
129515 /* Data is coming from a SELECT or from a multi-row VALUES clause.
129516 ** Generate a co-routine to run the SELECT. */
129517 int regYield; /* Register holding co-routine entry-point */
129518 int addrTop; /* Top of the co-routine */
129521 regYield = ++pParse->nMem;
129526 dest.nSdst = pTab->nCol;
129529 assert( db->pParse==pParse );
129530 if( rc || pParse->nErr ) goto insert_cleanup;
129531 assert( db->mallocFailed==0 );
129533 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
129534 assert( pSelect->pEList );
129535 nColumn = pSelect->pEList->nExpr;
129565 srcTab = pParse->nTab++;
129580 ** single-row VALUES clause
129585 srcTab = -1;
129588 nColumn = pList->nExpr;
129602 ipkColumn = pTab->iPKey;
129604 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
129605 testcase( pTab->tabFlags & TF_HasVirtual );
129606 testcase( pTab->tabFlags & TF_HasStored );
129607 for(i=ipkColumn-1; i>=0; i--){
129608 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
129609 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
129610 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
129611 ipkColumn--;
129623 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
129624 for(i=0; i<pTab->nCol; i++){
129625 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
129628 if( nColumn!=(pTab->nCol-nHidden) ){
129631 pTabList->a, pTab->nCol-nHidden, nColumn);
129635 if( pColumn!=0 && nColumn!=pColumn->nId ){
129636 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
129642 if( (db->flags & SQLITE_CountRows)!=0
129643 && !pParse->nested
129644 && !pParse->pTriggerTab
129645 && !pParse->bReturning
129647 regRowCount = ++pParse->nMem;
129654 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
129660 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
129662 aRegIdx[i] = ++pParse->nMem;
129663 pParse->nMem += pIdx->nColumn;
129665 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
129672 pTab->zName);
129679 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
129682 pTabList->a[0].iCursor = iDataCur;
129685 pNx->pUpsertSrc = pTabList;
129686 pNx->regData = regData;
129687 pNx->iDataCur = iDataCur;
129688 pNx->iIdxCur = iIdxCur;
129689 if( pNx->pUpsertTarget ){
129694 pNx = pNx->pNextUpsert;
129722 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
129726 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
129728 ** the value does not get overwritten by a NULL at tag-20191021-002. */
129742 for(i=0; i<pTab->nCol; i++, iRegStore++){
129746 if( i==pTab->iPKey ){
129747 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
129750 ** NULL - we cannot optimize further by skipping the column completely */
129754 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
129760 iRegStore--;
129764 ** triggers, the slots used for stored columns will be OP_Copy-ed
129775 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
129781 assert( pColumn->eU4==EU4_IDX );
129782 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
129783 if( j>=pColumn->nId ){
129787 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
129795 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
129799 k = i - nHidden;
129809 Expr *pX = pList->a[k].pExpr;
129823 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
129829 ** not happened yet) so we substitute a rowid of -1
129832 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
129840 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
129843 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
129849 assert( pTab->nNVCol>0 );
129850 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
129857 if( pTab->tabFlags & TF_HasGenerated ){
129858 testcase( pTab->tabFlags & TF_HasVirtual );
129859 testcase( pTab->tabFlags & TF_HasStored );
129875 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
129877 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
129890 /* Rowid already initialized at tag-20191021-001 */
129892 Expr *pIpk = pList->a[ipkColumn].pExpr;
129893 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
129897 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
129928 if( pTab->tabFlags & TF_HasGenerated ){
129940 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
129951 if( db->flags & SQLITE_ForeignKeys ){
129969 }else if( pParse->bReturning ){
129971 ** constant value -1, in case one or more of the returned expressions
129973 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
129986 pTab, regData-2-pTab->nCol, onError, endOfLoop);
130003 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
130004 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
130018 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
130054 ** Meanings of bits in of pWalker->eCode for
130061 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
130066 if( pExpr->op==TK_COLUMN ){
130067 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
130068 if( pExpr->iColumn>=0 ){
130069 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
130070 pWalker->eCode |= CKCNSTRNT_COLUMN;
130073 pWalker->eCode |= CKCNSTRNT_ROWID;
130080 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
130089 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
130090 ** The operation of this routine is the same - return true if an only if
130118 ** the indexes of a table in the order provided in the Table->pIndex list.
130119 ** However, sometimes (rarely - when there is an upsert) it wants to visit
130153 assert( pIter->i==0 );
130154 if( pIter->eType ){
130155 *pIx = pIter->u.ax.aIdx[0].ix;
130156 return pIter->u.ax.aIdx[0].p;
130159 return pIter->u.lx.pIdx;
130165 if( pIter->eType ){
130166 int i = ++pIter->i;
130167 if( i>=pIter->u.ax.nIdx ){
130171 *pIx = pIter->u.ax.aIdx[i].ix;
130172 return pIter->u.ax.aIdx[i].p;
130175 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
130176 return pIter->u.lx.pIdx;
130186 ** pTab->nCol+1 registers in this range. The first register (the one
130213 ** at pTab->pIndex.
130215 ** (2019-05-07) The generated code also creates a new record for the
130217 ** register identified by aRegIdx[nIdx] - in other words in the first
130228 ** for the first index in the pTab->pIndex list. Cursors for other indices
130229 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
130237 ** --------------- ---------- ----------------------------------------
130267 ** Or if overrideError==OE_Default, then the pParse->onError parameter
130268 ** is used. Or if pParse->onError==OE_Default then the onError value
130279 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
130313 db = pParse->db;
130314 v = pParse->pVdbe;
130317 nCol = pTab->nCol;
130328 nPkField = pPk->nKeyCol;
130337 if( pTab->tabFlags & TF_HasNotNull ){
130344 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
130345 int isGenerated; /* non-zero if column is generated */
130346 onError = pCol->notNull;
130348 if( i==pTab->iPKey ){
130351 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
130367 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
130369 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
130370 testcase( pCol->colFlags & COLFLAG_STORED );
130371 testcase( pCol->colFlags & COLFLAG_GENERATED );
130387 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
130399 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
130400 pCol->zCnName);
130425 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
130433 } /* end of 2-pass loop */
130434 } /* end if( has-not-null-constraints ) */
130439 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
130440 ExprList *pCheck = pTab->pCheck;
130441 pParse->iSelfTab = -(regNewData+1);
130443 for(i=0; i<pCheck->nExpr; i++){
130446 Expr *pExpr = pCheck->a[i].pExpr;
130461 if( !db->mallocFailed ){
130468 char *zName = pCheck->a[i].zEName;
130469 assert( zName!=0 || pParse->db->mallocFailed );
130470 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
130477 pParse->iSelfTab = 0;
130494 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
130512 sIdxIter.u.lx.pIdx = pTab->pIndex;
130514 if( pUpsert->pUpsertTarget==0 ){
130515 /* There is just on ON CONFLICT clause and it has no constraint-target */
130516 assert( pUpsert->pNextUpsert==0 );
130517 if( pUpsert->isDoUpdate==0 ){
130518 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
130526 }else if( pTab->pIndex!=0 ){
130534 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
130543 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
130544 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
130545 if( pTerm->pUpsertTarget==0 ) break;
130546 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
130548 pIdx = pTab->pIndex;
130549 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
130550 pIdx = pIdx->pNext;
130559 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
130589 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
130595 if( db->flags&SQLITE_RecTriggers ){
130605 regTrigCnt = ++pParse->nMem;
130620 onError = pTab->keyConf;
130631 if( pUpsertClause->isDoUpdate==0 ){
130652 && pTab->pIndex /* There exist other constraints */
130691 ** recursive-triggers flag is set, call GenerateRowDelete() to
130693 ** the triggers and remove both the table and index b-tree entries.
130695 ** Otherwise, if there are no triggers or the recursive-triggers
130697 ** GenerateRowIndexDelete(). This removes the index b-tree entries
130698 ** only. The table b-tree entry will be replaced by the new entry
130715 regNewData, 1, 0, OE_Replace, 1, -1);
130721 /* This OP_Delete opcode fires the pre-update-hook only. It does
130722 ** not modify the b-tree. It is more efficient to let the coming
130728 if( pTab->pIndex ){
130730 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
130753 sqlite3VdbeJumpHere(v, ipkTop-1);
130786 VdbeNoopComment((v, "prep index %s", pIdx->zName));
130791 if( pIdx->pPartIdxWhere ){
130793 pParse->iSelfTab = -(regNewData+1);
130794 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
130796 pParse->iSelfTab = 0;
130803 for(i=0; i<pIdx->nColumn; i++){
130804 int iField = pIdx->aiColumn[i];
130807 pParse->iSelfTab = -(regNewData+1);
130808 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
130809 pParse->iSelfTab = 0;
130810 VdbeComment((v, "%s column %d", pIdx->zName, i));
130811 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
130819 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
130822 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
130823 VdbeComment((v, "for %s", pIdx->zName));
130825 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
130826 sqlite3SetMakeRecordP5(v, pIdx->pTable);
130829 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
130841 onError = pIdx->onError;
130854 if( pUpsertClause->isDoUpdate==0 ){
130869 ** must be explicitly deleted in order to ensure any pre-update hook
130873 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
130876 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
130878 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
130879 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
130890 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
130898 ** is different from old-rowid */
130907 ** store it in registers regR..regR+nPk-1 */
130909 for(i=0; i<pPk->nKeyCol; i++){
130910 assert( pPk->aiColumn[i]>=0 );
130911 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
130913 VdbeComment((v, "%s.%s", pTab->zName,
130914 pTab->aCol[pPk->aiColumn[i]].zCnName));
130925 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
130929 for(i=0; i<pPk->nKeyCol; i++){
130930 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
130931 x = pPk->aiColumn[i];
130933 if( i==(pPk->nKeyCol-1) ){
130977 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
130978 assert( nConflictCk>0 || db->mallocFailed );
131006 if( pIdx->pPartIdxWhere ){
131009 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
131013 ** the constraint-ok jump destination to be the address of
131034 nConflictCk--;
131089 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
131103 ** to be the number of columns in table pTab that must not be NULL-trimmed.
131105 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
131111 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
131112 if( pTab->pSchema->file_format<2 ) return;
131114 for(i=pTab->nCol-1; i>0; i--){
131115 if( pTab->aCol[i].iDflt!=0 ) break;
131116 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
131125 ** PK index. This function adds code to invoke the pre-update hook,
131135 Vdbe *v = pParse->pVdbe;
131138 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
131178 v = pParse->pVdbe;
131181 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
131183 assert( pIdx->onError!=OE_Replace
131184 || pIdx->pNext==0
131185 || pIdx->pNext->onError==OE_Replace );
131187 if( pIdx->pPartIdxWhere ){
131201 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
131205 if( pParse->nested ){
131218 if( !pParse->nested ){
131234 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
131240 ** pTab->pIndex list.
131242 ** If pTab is a virtual table, then this routine is a no-op and the
131264 /* This routine is a no-op for virtual tables. Leave the output
131267 *piDataCur = *piIdxCur = -999;
131270 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
131271 v = pParse->pVdbe;
131273 if( iBase<0 ) iBase = pParse->nTab;
131279 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
131282 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
131284 assert( pIdx->pSchema==pTab->pSchema );
131290 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
131293 VdbeComment((v, "%s", pIdx->zName));
131296 if( iBase>pParse->nTab ) pParse->nTab = iBase;
131305 ** purposes only - to make sure the transfer optimization really
131327 assert( pDest->pTable!=pSrc->pTable );
131328 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
131331 if( pDest->onError!=pSrc->onError ){
131334 for(i=0; i<pSrc->nKeyCol; i++){
131335 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
131338 if( pSrc->aiColumn[i]==XN_EXPR ){
131339 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
131340 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
131341 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
131345 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
131348 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
131352 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
131370 ** There are lots of rules for determining compatibility - see comments
131375 ** is empty - a factor that can only be determined at run-time. In that
131392 sqlite3 *db = pParse->db;
131396 SrcItem *pItem; /* An element of pSelect->pSrc */
131409 if( pParse->pWith || pSelect->pWith ){
131421 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
131424 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
131425 if( pSelect->pSrc->nSrc!=1 ){
131428 if( pSelect->pSrc->a[0].pSelect ){
131431 if( pSelect->pWhere ){
131434 if( pSelect->pOrderBy ){
131439 if( pSelect->pGroupBy ){
131442 if( pSelect->pLimit ){
131445 if( pSelect->pPrior ){
131448 if( pSelect->selFlags & SF_Distinct ){
131451 pEList = pSelect->pEList;
131453 if( pEList->nExpr!=1 ){
131456 assert( pEList->a[0].pExpr );
131457 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
131465 pItem = pSelect->pSrc->a;
131470 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
131480 if( pDest->nCol!=pSrc->nCol ){
131483 if( pDest->iPKey!=pSrc->iPKey ){
131486 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
131487 return 0; /* Cannot feed from a non-strict into a strict table */
131489 for(i=0; i<pDest->nCol; i++){
131490 Column *pDestCol = &pDest->aCol[i];
131491 Column *pSrcCol = &pSrc->aCol[i];
131493 if( (db->mDbFlags & DBFLAG_Vacuum)==0
131494 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
131516 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
131517 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
131518 return 0; /* Both columns have the same generated-column type */
131524 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
131527 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
131528 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
131529 testcase( pDestCol->colFlags & COLFLAG_STORED );
131534 if( pDestCol->affinity!=pSrcCol->affinity ){
131541 if( pDestCol->notNull && !pSrcCol->notNull ){
131545 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
131548 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
131550 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
131553 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
131554 pSrcExpr->u.zToken)!=0)
131560 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
131564 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
131570 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
131575 return 0; /* Corrupt schema - two indexes on the same btree */
131579 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
131592 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
131596 if( (db->flags & SQLITE_CountRows)!=0 ){
131607 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
131610 iSrc = pParse->nTab++;
131611 iDest = pParse->nTab++;
131618 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
131619 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
131648 if( pDest->iPKey>=0 ){
131650 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
131658 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
131662 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
131665 if( db->mDbFlags & DBFLAG_Vacuum ){
131672 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
131681 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
131682 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
131690 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
131691 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
131693 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
131695 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
131699 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
131701 VdbeComment((v, "%s", pSrcIdx->zName));
131702 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
131705 VdbeComment((v, "%s", pDestIdx->zName));
131707 if( db->mDbFlags & DBFLAG_Vacuum ){
131712 ** order. In this case, instead of seeking within the b-tree as part
131714 ** OP_IdxInsert to seek to the point within the b-tree where each key
131722 for(i=0; i<pSrcIdx->nColumn; i++){
131723 const char *zColl = pSrcIdx->azColl[i];
131726 if( i==pSrcIdx->nColumn ){
131731 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
131736 if( (db->mDbFlags & DBFLAG_Vacuum)==0
131812 sqlite3_mutex_enter(db->mutex);
131819 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
131825 /* this happens for a comment or white-space */
131838 && db->flags&SQLITE_NullCallback)) ){
131865 /* EVIDENCE-OF: R-38229-40159 If the callback function to
131866 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
131904 assert( (rc&db->errMask)==rc );
131905 sqlite3_mutex_leave(db->mutex);
132311 ** (part of the main SQLite library - not an extension) so that
132318 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
132320 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
132322 #define sqlite3_bind_blob sqlite3_api->bind_blob
132323 #define sqlite3_bind_double sqlite3_api->bind_double
132324 #define sqlite3_bind_int sqlite3_api->bind_int
132325 #define sqlite3_bind_int64 sqlite3_api->bind_int64
132326 #define sqlite3_bind_null sqlite3_api->bind_null
132327 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
132328 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
132329 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
132330 #define sqlite3_bind_text sqlite3_api->bind_text
132331 #define sqlite3_bind_text16 sqlite3_api->bind_text16
132332 #define sqlite3_bind_value sqlite3_api->bind_value
132333 #define sqlite3_busy_handler sqlite3_api->busy_handler
132334 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
132335 #define sqlite3_changes sqlite3_api->changes
132336 #define sqlite3_close sqlite3_api->close
132337 #define sqlite3_collation_needed sqlite3_api->collation_needed
132338 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
132339 #define sqlite3_column_blob sqlite3_api->column_blob
132340 #define sqlite3_column_bytes sqlite3_api->column_bytes
132341 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
132342 #define sqlite3_column_count sqlite3_api->column_count
132343 #define sqlite3_column_database_name sqlite3_api->column_database_name
132344 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
132345 #define sqlite3_column_decltype sqlite3_api->column_decltype
132346 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
132347 #define sqlite3_column_double sqlite3_api->column_double
132348 #define sqlite3_column_int sqlite3_api->column_int
132349 #define sqlite3_column_int64 sqlite3_api->column_int64
132350 #define sqlite3_column_name sqlite3_api->column_name
132351 #define sqlite3_column_name16 sqlite3_api->column_name16
132352 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
132353 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
132354 #define sqlite3_column_table_name sqlite3_api->column_table_name
132355 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
132356 #define sqlite3_column_text sqlite3_api->column_text
132357 #define sqlite3_column_text16 sqlite3_api->column_text16
132358 #define sqlite3_column_type sqlite3_api->column_type
132359 #define sqlite3_column_value sqlite3_api->column_value
132360 #define sqlite3_commit_hook sqlite3_api->commit_hook
132361 #define sqlite3_complete sqlite3_api->complete
132362 #define sqlite3_complete16 sqlite3_api->complete16
132363 #define sqlite3_create_collation sqlite3_api->create_collation
132364 #define sqlite3_create_collation16 sqlite3_api->create_collation16
132365 #define sqlite3_create_function sqlite3_api->create_function
132366 #define sqlite3_create_function16 sqlite3_api->create_function16
132367 #define sqlite3_create_module sqlite3_api->create_module
132368 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
132369 #define sqlite3_data_count sqlite3_api->data_count
132370 #define sqlite3_db_handle sqlite3_api->db_handle
132371 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
132372 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
132373 #define sqlite3_errcode sqlite3_api->errcode
132374 #define sqlite3_errmsg sqlite3_api->errmsg
132375 #define sqlite3_errmsg16 sqlite3_api->errmsg16
132376 #define sqlite3_exec sqlite3_api->exec
132378 #define sqlite3_expired sqlite3_api->expired
132380 #define sqlite3_finalize sqlite3_api->finalize
132381 #define sqlite3_free sqlite3_api->free
132382 #define sqlite3_free_table sqlite3_api->free_table
132383 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
132384 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
132385 #define sqlite3_get_table sqlite3_api->get_table
132387 #define sqlite3_global_recover sqlite3_api->global_recover
132389 #define sqlite3_interrupt sqlite3_api->interruptx
132390 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
132391 #define sqlite3_libversion sqlite3_api->libversion
132392 #define sqlite3_libversion_number sqlite3_api->libversion_number
132393 #define sqlite3_malloc sqlite3_api->malloc
132394 #define sqlite3_mprintf sqlite3_api->mprintf
132395 #define sqlite3_open sqlite3_api->open
132396 #define sqlite3_open16 sqlite3_api->open16
132397 #define sqlite3_prepare sqlite3_api->prepare
132398 #define sqlite3_prepare16 sqlite3_api->prepare16
132399 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
132400 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
132401 #define sqlite3_profile sqlite3_api->profile
132402 #define sqlite3_progress_handler sqlite3_api->progress_handler
132403 #define sqlite3_realloc sqlite3_api->realloc
132404 #define sqlite3_reset sqlite3_api->reset
132405 #define sqlite3_result_blob sqlite3_api->result_blob
132406 #define sqlite3_result_double sqlite3_api->result_double
132407 #define sqlite3_result_error sqlite3_api->result_error
132408 #define sqlite3_result_error16 sqlite3_api->result_error16
132409 #define sqlite3_result_int sqlite3_api->result_int
132410 #define sqlite3_result_int64 sqlite3_api->result_int64
132411 #define sqlite3_result_null sqlite3_api->result_null
132412 #define sqlite3_result_text sqlite3_api->result_text
132413 #define sqlite3_result_text16 sqlite3_api->result_text16
132414 #define sqlite3_result_text16be sqlite3_api->result_text16be
132415 #define sqlite3_result_text16le sqlite3_api->result_text16le
132416 #define sqlite3_result_value sqlite3_api->result_value
132417 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
132418 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
132419 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
132420 #define sqlite3_snprintf sqlite3_api->xsnprintf
132421 #define sqlite3_step sqlite3_api->step
132422 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
132423 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
132424 #define sqlite3_total_changes sqlite3_api->total_changes
132425 #define sqlite3_trace sqlite3_api->trace
132427 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
132429 #define sqlite3_update_hook sqlite3_api->update_hook
132430 #define sqlite3_user_data sqlite3_api->user_data
132431 #define sqlite3_value_blob sqlite3_api->value_blob
132432 #define sqlite3_value_bytes sqlite3_api->value_bytes
132433 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
132434 #define sqlite3_value_double sqlite3_api->value_double
132435 #define sqlite3_value_int sqlite3_api->value_int
132436 #define sqlite3_value_int64 sqlite3_api->value_int64
132437 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
132438 #define sqlite3_value_text sqlite3_api->value_text
132439 #define sqlite3_value_text16 sqlite3_api->value_text16
132440 #define sqlite3_value_text16be sqlite3_api->value_text16be
132441 #define sqlite3_value_text16le sqlite3_api->value_text16le
132442 #define sqlite3_value_type sqlite3_api->value_type
132443 #define sqlite3_vmprintf sqlite3_api->vmprintf
132444 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
132445 #define sqlite3_overload_function sqlite3_api->overload_function
132446 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
132447 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
132448 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
132449 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
132450 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
132451 #define sqlite3_blob_close sqlite3_api->blob_close
132452 #define sqlite3_blob_open sqlite3_api->blob_open
132453 #define sqlite3_blob_read sqlite3_api->blob_read
132454 #define sqlite3_blob_write sqlite3_api->blob_write
132455 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
132456 #define sqlite3_file_control sqlite3_api->file_control
132457 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
132458 #define sqlite3_memory_used sqlite3_api->memory_used
132459 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
132460 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
132461 #define sqlite3_mutex_free sqlite3_api->mutex_free
132462 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
132463 #define sqlite3_mutex_try sqlite3_api->mutex_try
132464 #define sqlite3_open_v2 sqlite3_api->open_v2
132465 #define sqlite3_release_memory sqlite3_api->release_memory
132466 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
132467 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
132468 #define sqlite3_sleep sqlite3_api->sleep
132469 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
132470 #define sqlite3_vfs_find sqlite3_api->vfs_find
132471 #define sqlite3_vfs_register sqlite3_api->vfs_register
132472 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
132473 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
132474 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
132475 #define sqlite3_result_error_code sqlite3_api->result_error_code
132476 #define sqlite3_test_control sqlite3_api->test_control
132477 #define sqlite3_randomness sqlite3_api->randomness
132478 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
132479 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
132480 #define sqlite3_limit sqlite3_api->limit
132481 #define sqlite3_next_stmt sqlite3_api->next_stmt
132482 #define sqlite3_sql sqlite3_api->sql
132483 #define sqlite3_status sqlite3_api->status
132484 #define sqlite3_backup_finish sqlite3_api->backup_finish
132485 #define sqlite3_backup_init sqlite3_api->backup_init
132486 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
132487 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
132488 #define sqlite3_backup_step sqlite3_api->backup_step
132489 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
132490 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
132491 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
132492 #define sqlite3_db_config sqlite3_api->db_config
132493 #define sqlite3_db_mutex sqlite3_api->db_mutex
132494 #define sqlite3_db_status sqlite3_api->db_status
132495 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
132496 #define sqlite3_log sqlite3_api->log
132497 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
132498 #define sqlite3_sourceid sqlite3_api->sourceid
132499 #define sqlite3_stmt_status sqlite3_api->stmt_status
132500 #define sqlite3_strnicmp sqlite3_api->strnicmp
132501 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
132502 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
132503 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
132504 #define sqlite3_wal_hook sqlite3_api->wal_hook
132505 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
132506 #define sqlite3_vtab_config sqlite3_api->vtab_config
132507 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
132509 #define sqlite3_close_v2 sqlite3_api->close_v2
132510 #define sqlite3_db_filename sqlite3_api->db_filename
132511 #define sqlite3_db_readonly sqlite3_api->db_readonly
132512 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
132513 #define sqlite3_errstr sqlite3_api->errstr
132514 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
132515 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
132516 #define sqlite3_stricmp sqlite3_api->stricmp
132517 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
132518 #define sqlite3_uri_int64 sqlite3_api->uri_int64
132519 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
132520 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
132521 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
132523 #define sqlite3_auto_extension sqlite3_api->auto_extension
132524 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
132525 #define sqlite3_bind_text64 sqlite3_api->bind_text64
132526 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
132527 #define sqlite3_load_extension sqlite3_api->load_extension
132528 #define sqlite3_malloc64 sqlite3_api->malloc64
132529 #define sqlite3_msize sqlite3_api->msize
132530 #define sqlite3_realloc64 sqlite3_api->realloc64
132531 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
132532 #define sqlite3_result_blob64 sqlite3_api->result_blob64
132533 #define sqlite3_result_text64 sqlite3_api->result_text64
132534 #define sqlite3_strglob sqlite3_api->strglob
132536 #define sqlite3_value_dup sqlite3_api->value_dup
132537 #define sqlite3_value_free sqlite3_api->value_free
132538 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
132539 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
132541 #define sqlite3_value_subtype sqlite3_api->value_subtype
132542 #define sqlite3_result_subtype sqlite3_api->result_subtype
132544 #define sqlite3_status64 sqlite3_api->status64
132545 #define sqlite3_strlike sqlite3_api->strlike
132546 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
132548 #define sqlite3_system_errno sqlite3_api->system_errno
132550 #define sqlite3_trace_v2 sqlite3_api->trace_v2
132551 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
132553 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
132555 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
132556 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
132557 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
132558 #define sqlite3_result_pointer sqlite3_api->result_pointer
132559 #define sqlite3_value_pointer sqlite3_api->value_pointer
132561 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
132562 #define sqlite3_value_nochange sqlite3_api->value_nochange
132563 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
132565 #define sqlite3_keyword_count sqlite3_api->keyword_count
132566 #define sqlite3_keyword_name sqlite3_api->keyword_name
132567 #define sqlite3_keyword_check sqlite3_api->keyword_check
132568 #define sqlite3_str_new sqlite3_api->str_new
132569 #define sqlite3_str_finish sqlite3_api->str_finish
132570 #define sqlite3_str_appendf sqlite3_api->str_appendf
132571 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
132572 #define sqlite3_str_append sqlite3_api->str_append
132573 #define sqlite3_str_appendall sqlite3_api->str_appendall
132574 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
132575 #define sqlite3_str_reset sqlite3_api->str_reset
132576 #define sqlite3_str_errcode sqlite3_api->str_errcode
132577 #define sqlite3_str_length sqlite3_api->str_length
132578 #define sqlite3_str_value sqlite3_api->str_value
132580 #define sqlite3_create_window_function sqlite3_api->create_window_function
132582 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
132584 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
132585 #define sqlite3_value_frombind sqlite3_api->value_frombind
132587 #define sqlite3_drop_modules sqlite3_api->drop_modules
132589 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
132590 #define sqlite3_uri_key sqlite3_api->uri_key
132591 #define sqlite3_filename_database sqlite3_api->filename_database
132592 #define sqlite3_filename_journal sqlite3_api->filename_journal
132593 #define sqlite3_filename_wal sqlite3_api->filename_wal
132595 #define sqlite3_create_filename sqlite3_api->create_filename
132596 #define sqlite3_free_filename sqlite3_api->free_filename
132597 #define sqlite3_database_file_object sqlite3_api->database_file_object
132599 #define sqlite3_txn_state sqlite3_api->txn_state
132601 #define sqlite3_changes64 sqlite3_api->changes64
132602 #define sqlite3_total_changes64 sqlite3_api->total_changes64
132604 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
132606 #define sqlite3_error_offset sqlite3_api->error_offset
132607 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
132608 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
132609 #define sqlite3_vtab_in sqlite3_api->vtab_in
132610 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
132611 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
132614 #define sqlite3_deserialize sqlite3_api->deserialize
132615 #define sqlite3_serialize sqlite3_api->serialize
132617 #define sqlite3_db_name sqlite3_api->db_name
132619 #define sqlite3_value_encoding sqlite3_api->value_encoding
132621 #define sqlite3_set_droptable_handle sqlite3_api->set_droptable_handle
132634 # define SQLITE_EXTENSION_INIT1 /*no-op*/
132636 # define SQLITE_EXTENSION_INIT3 /*no-op*/
133178 sqlite3_vfs *pVfs = db->pVfs;
133210 if( (db->flags & SQLITE_LoadExtension)==0 ){
133219 /* tag-20210611-1. Some dlopen() implementations will segfault if given
133257 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
133277 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
133296 /* Append the new shared library handle to the db->aExtension array. */
133297 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
133301 if( db->nExtension>0 ){
133302 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
133304 sqlite3DbFree(db, db->aExtension);
133305 db->aExtension = aHandle;
133307 db->aExtension[db->nExtension++] = handle;
133318 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
133330 sqlite3_mutex_enter(db->mutex);
133333 sqlite3_mutex_leave(db->mutex);
133343 assert( sqlite3_mutex_held(db->mutex) );
133344 for(i=0; i<db->nExtension; i++){
133345 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
133347 sqlite3DbFree(db, db->aExtension);
133355 sqlite3_mutex_enter(db->mutex);
133357 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
133359 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
133361 sqlite3_mutex_leave(db->mutex);
133382 ** we have to locate the state vector at run-time. In the more common
133442 ** routine is a no-op.
133457 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
133459 wsdAutoext.nExt--;
133628 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
133632 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
133634 /* Names of columns for pragmas that return multi-column result
133635 ** or that return single-column results where the name of the
133699 /* 56 */ "timeout", /* Used by: busy_timeout */
133702 /* Definitions of all built-in pragmas */
134333 ** Interpret the given string as an auto-vacuum mode value.
134351 ** backed temporary databases, 2 for the Red-Black tree in memory database
134352 ** and 0 to use the compile-time default.
134356 return z[0] - '0';
134373 sqlite3 *db = pParse->db;
134374 if( db->aDb[1].pBt!=0 ){
134375 if( !db->autoCommit
134376 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
134382 sqlite3BtreeClose(db->aDb[1].pBt);
134383 db->aDb[1].pBt = 0;
134398 sqlite3 *db = pParse->db;
134399 if( db->temp_store==ts ) return SQLITE_OK;
134403 db->temp_store = (u8)ts;
134415 u8 n = pPragma->nPragCName;
134418 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
134421 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
134455 if( db->autoCommit ){
134456 Db *pDb = db->aDb;
134457 int n = db->nDb;
134463 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
134464 while( (n--) > 0 ){
134465 if( pDb->pBt ){
134466 sqlite3BtreeSetPagerFlags(pDb->pBt,
134467 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
134474 # define setAllPagerFlags(X) /* no-op */
134479 ** Return a human-readable name for a constraint resolution action.
134500 ** journal-mode name.
134527 upr = ArraySize(aPragmaName)-1;
134533 upr = mid - 1;
134548 int isBuiltin, /* True if this is a built-in function */
134559 for(; p; p=p->pNext){
134568 if( p->xSFunc==0 ) continue;
134569 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
134574 if( p->xValue!=0 ){
134576 }else if( p->xFinalize!=0 ){
134582 p->zName, isBuiltin,
134583 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
134584 p->nArg,
134585 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
134594 ** Generate code to output a single-column result row with a value of the
134627 int minusFlag /* True if a '-' sign preceded <value> */
134629 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
134630 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
134636 sqlite3 *db = pParse->db; /* The database connection */
134649 pParse->nMem = 2;
134655 pDb = &db->aDb[iDb];
134667 zRight = sqlite3MPrintf(db, "-%T", pValue);
134673 zDb = pId2->n>0 ? pDb->zDbSName : 0;
134678 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
134680 ** handled the pragma and generate a no-op prepared statement.
134682 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
134687 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
134697 db->busyHandler.nBusy = 0;
134711 pParse->nErr++;
134712 pParse->rc = rc;
134728 /* IMP: R-43042-22504 No error messages are generated if an
134734 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
134739 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
134740 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
134746 switch( pPragma->ePragTyp ){
134781 pParse->nMem += 2;
134793 pDb->pSchema->cache_size = size;
134794 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
134811 Btree *pBt = pDb->pBt;
134817 /* Malloc may fail when setting the page-size, as there is an internal
134820 db->nextPagesize = sqlite3Atoi(zRight);
134821 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
134837 Btree *pBt = pDb->pBt;
134838 int b = -1;
134847 if( pId2->n==0 && b>=0 ){
134849 for(ii=0; ii<db->nDb; ii++){
134850 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
134879 iReg = ++pParse->nMem;
134903 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
134906 ** the locking-mode of the main database).
134908 eMode = db->dfltLockMode;
134911 if( pId2->n==0 ){
134913 ** of the PRAGMA command. In this case the locking-mode must be
134921 assert(pDb==&db->aDb[0]);
134922 for(ii=2; ii<db->nDb; ii++){
134923 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
134926 db->dfltLockMode = (u8)eMode;
134928 pPager = sqlite3BtreePager(pDb->pBt);
134965 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
134966 /* Do not allow journal-mode "OFF" in defensive since the database
134971 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
134974 pId2->n = 1;
134976 for(ii=db->nDb-1; ii>=0; ii--){
134977 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
134993 Pager *pPager = sqlite3BtreePager(pDb->pBt);
134994 i64 iLimit = -2;
134997 if( iLimit<-1 ) iLimit = -1;
135010 ** Get or set the value of the database 'auto-vacuum' parameter.
135015 Btree *pBt = pDb->pBt;
135022 db->nextAutovac = (u8)eAuto;
135024 ** incr-vacuum flags. This is required in case this connection
135026 ** as an auto-vacuum capable db.
135033 ** that this really is an auto-vacuum capable database.
135052 aOp[4].p3 = eAuto - 1;
135075 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
135091 ** number of pages is adjusted so that the cache uses -N kibibytes
135097 returnSingleInt(v, pDb->pSchema->cache_size);
135100 pDb->pSchema->cache_size = size;
135101 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
135118 ** number of pages is adjusted so that the cache uses -N kibibytes
135132 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
135133 sqlite3BtreeSetSpillSize(pDb->pBt,0));
135137 sqlite3BtreeSetSpillSize(pDb->pBt, size);
135140 db->flags |= SQLITE_CacheSpill;
135142 db->flags &= ~(u64)SQLITE_CacheSpill;
135171 if( pId2->n==0 ) db->szMmap = sz;
135172 for(ii=db->nDb-1; ii>=0; ii--){
135173 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
135174 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
135178 sz = -1;
135187 pParse->nErr++;
135188 pParse->rc = rc;
135201 ** Note that it is possible for the library compile-time options to
135206 returnSingleInt(v, db->temp_store);
135231 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
135239 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
135240 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
135278 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
135309 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135316 Pager *pPager = sqlite3BtreePager(pDb->pBt);
135346 returnSingleInt(v, pDb->safety_level-1);
135348 if( !db->autoCommit ){
135354 pDb->safety_level = iLevel;
135355 pDb->bSyncSet = 1;
135367 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
135369 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
135370 if( db->autoCommit==0 ){
135372 ** in auto-commit mode. */
135376 if( db->auth.authLevel==UAUTH_User ){
135377 /* Do not allow non-admin users to modify the schema arbitrarily */
135383 db->flags |= mask;
135385 db->flags &= ~mask;
135386 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
135390 /* IMP: R-60817-01178 If the argument is "RESET" then schema
135397 /* Many of the flag-pragmas modify the code generated by the SQL
135420 ** pk: Non-zero for PK fields.
135431 pParse->nMem = 7;
135433 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
135436 if( pCol->colFlags & COLFLAG_NOINSERT ){
135437 if( pPragma->iArg==0 ){
135441 if( pCol->colFlags & COLFLAG_VIRTUAL ){
135443 }else if( pCol->colFlags & COLFLAG_STORED ){
135445 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
135449 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
135454 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
135457 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
135460 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
135461 i-nHidden,
135462 pCol->zCnName,
135464 pCol->notNull ? 1 : 0,
135465 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
135488 pParse->nMem = 6;
135490 for(ii=0; ii<db->nDb; ii++){
135494 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
135501 pHash = &db->aDb[ii].pSchema->tblHash;
135503 while( initNCol-- ){
135508 if( pTab->nCol==0 ){
135509 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
135512 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
135516 if( db->mallocFailed ){
135517 sqlite3ErrorMsg(db->pParse, "out of memory");
135518 db->pParse->rc = SQLITE_NOMEM_BKPT;
135520 pHash = &db->aDb[ii].pSchema->tblHash;
135529 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
135534 }else if( pTab->tabFlags & TF_Shadow ){
135540 db->aDb[ii].zDbSName,
135541 sqlite3PreferredTableName(pTab->zName),
135543 pTab->nCol,
135544 (pTab->tabFlags & TF_WithoutRowid)!=0,
135545 (pTab->tabFlags & TF_Strict)!=0
135556 pParse->nMem = 5;
135558 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
135561 sqlite3PreferredTableName(pTab->zName),
135563 pTab->szTabRow,
135564 pTab->nRowLogEst,
135565 pTab->tabFlags);
135566 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
135568 pIdx->zName,
135569 pIdx->szIdxRow,
135570 pIdx->aiRowLogEst[0],
135571 pIdx->hasStat1);
135593 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
135596 if( pPragma->iArg ){
135598 mx = pIdx->nColumn;
135599 pParse->nMem = 6;
135602 mx = pIdx->nKeyCol;
135603 pParse->nMem = 3;
135605 pTab = pIdx->pTable;
135607 assert( pParse->nMem<=pPragma->nPragCName );
135609 i16 cnum = pIdx->aiColumn[i];
135611 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
135612 if( pPragma->iArg ){
135614 pIdx->aSortOrder[i],
135615 pIdx->azColl[i],
135616 i<pIdx->nKeyCol);
135618 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
135630 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135631 pParse->nMem = 5;
135633 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
135637 pIdx->zName,
135639 azOrigin[pIdx->idxType],
135640 pIdx->pPartIdxWhere!=0);
135648 pParse->nMem = 3;
135649 for(i=0; i<db->nDb; i++){
135650 if( db->aDb[i].pBt==0 ) continue;
135651 assert( db->aDb[i].zDbSName!=0 );
135654 db->aDb[i].zDbSName,
135655 sqlite3BtreeGetFilename(db->aDb[i].pBt));
135663 pParse->nMem = 2;
135664 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
135666 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
135676 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
135677 pParse->nMem = 6;
135679 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
135680 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
135684 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
135686 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
135695 pParse->nMem = 1;
135696 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
135698 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
135721 pFK = pTab->u.tab.pFKey;
135723 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135725 pParse->nMem = 8;
135729 for(j=0; j<pFK->nCol; j++){
135733 pFK->zTo,
135734 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
135735 pFK->aCol[j].zCol,
135736 actionName(pFK->aAction[1]), /* ON UPDATE */
135737 actionName(pFK->aAction[0]), /* ON DELETE */
135741 pFK = pFK->pNextFrom;
135766 regResult = pParse->nMem+1;
135767 pParse->nMem += 4;
135768 regRow = ++pParse->nMem;
135769 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
135778 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
135779 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135780 zDb = db->aDb[iDb].zDbSName;
135782 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
135783 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
135785 sqlite3VdbeLoadString(v, regResult, pTab->zName);
135787 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
135788 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
135791 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
135797 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
135805 assert( pParse->nErr>0 || pFK==0 );
135807 if( pParse->nTab<i ) pParse->nTab = i;
135810 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
135811 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
135816 assert( x==0 || db->mallocFailed );
135824 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
135825 for(j=0; j<pFK->nCol; j++){
135826 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
135834 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
135835 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
135836 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
135842 assert( pFK->nCol==1 || db->mallocFailed );
135851 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
135890 ** without the overhead of cross-checking indexes. Quick_check
135918 ** to -1 here, to indicate that the VDBE should verify the integrity
135921 assert( iDb==0 || pId2->z );
135922 if( pId2->z==0 ) iDb = -1;
135925 pParse->nMem = 6;
135936 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
135939 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
135942 for(i=0; i<db->nDb; i++){
135954 /* Do an integrity check of the B-Tree
135960 pTbls = &db->aDb[i].pSchema->tblHash;
135967 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
135980 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
135981 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
135982 aRoot[++cnt] = pIdx->tnum;
135988 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
135991 /* Do the b-tree integrity checks */
135996 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
136010 int r1 = -1;
136013 int mxCol; /* Maximum non-virtual column number */
136022 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
136023 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
136028 ** reg[8+i] counts the number of entries in the i-th index
136031 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136034 assert( pParse->nMem>=8+j );
136039 /* Fetch the right-most column from the table. This will cause
136044 mxCol = pTab->nCol-1;
136046 && ((pTab->aCol[mxCol].colFlags & COLFLAG_VIRTUAL)!=0
136047 || pTab->iPKey==mxCol) ) mxCol--;
136058 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
136063 pTab->zName);
136068 for(j=0; j<pPk->nKeyCol; j++){
136076 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
136077 ** (3) Datatype for TEXT columns in non-STRICT tables must be
136079 ** (4) Datatype for numeric columns in non-STRICT tables must not
136082 bStrict = (pTab->tabFlags & TF_Strict)!=0;
136083 for(j=0; j<pTab->nCol; j++){
136085 Column *pCol = pTab->aCol + j; /* The column to be checked */
136091 if( j==pTab->iPKey ) continue;
136093 doTypeCheck = pCol->eCType>COLTYPE_ANY;
136095 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
136097 if( pCol->notNull==0 && !doTypeCheck ) continue;
136101 if( pCol->colFlags & COLFLAG_VIRTUAL ){
136103 p1 = -1;
136106 if( pCol->iDflt ){
136109 pCol->affinity, &pDfltValue);
136127 if( pCol->notNull ){
136132 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
136133 pCol->zCnName);
136143 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
136153 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
136154 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
136156 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
136157 sqlite3StdType[pCol->eCType-1],
136158 pTab->zName, pTab->aCol[j].zCnName);
136160 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
136161 /* (3) Datatype for TEXT columns in non-STRICT tables must be
136167 pTab->zName, pTab->aCol[j].zCnName);
136169 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
136170 /* (4) Datatype for numeric columns in non-STRICT tables must not
136179 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
136183 pTab->zName, pTab->aCol[j].zCnName);
136191 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
136192 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
136193 if( db->mallocFailed==0 ){
136198 pParse->iSelfTab = iDataCur + 1;
136199 for(k=pCheck->nExpr-1; k>0; k--){
136200 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
136202 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
136205 pParse->iSelfTab = 0;
136207 pTab->zName);
136216 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136226 pIdx->nColumn); VdbeCoverage(v);
136231 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
136242 for(kk=0; kk<pIdx->nKeyCol; kk++){
136243 int iCol = pIdx->aiColumn[kk];
136244 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
136245 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
136253 pIdx->nKeyCol); VdbeCoverage(v);
136254 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
136263 sqlite3VdbeJumpHere(v, loopTop-1);
136266 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
136271 sqlite3VdbeLoadString(v, 4, pIdx->zName);
136277 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
136297 aOp[0].p2 = 1-mxErr;
136303 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
136312 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
136317 ** The second form of this pragma is a no-op if the main database file
136338 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
136339 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
136340 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
136343 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
136353 returnSingleText(v, encnames[ENC(pParse->db)].zName);
136360 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
136361 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
136362 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
136363 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
136369 if( !pEnc->zName ){
136394 ** the value of the schema-version and user-version, respectively. Both
136395 ** the schema-version and the user-version are 32-bit signed integers
136398 ** The schema-cookie is usually only manipulated internally by SQLite. It
136405 ** the schema-version is potentially dangerous and may lead to program
136408 ** The user-version is not used internally by SQLite. It may be used by
136412 int iCookie = pPragma->iArg; /* Which cookie to read or write */
136414 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
136429 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
136431 ** mode. Change the OP_SetCookie opcode into a no-op. */
136458 ** Return the names of all compile-time options used in this build,
136464 pParse->nMem = 1;
136481 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
136492 pParse->nMem = 3;
136511 db->xWalCallback==sqlite3WalDefaultHook ?
136512 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
136520 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
136562 ** ever added that should be off by default, those off-by-default
136572 ** (2) The query planner used sqlite_stat1-style statistics for one or
136600 iTabCur = pParse->nTab++;
136601 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
136604 pSchema = db->aDb[iDb].pSchema;
136605 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
136611 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
136614 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
136615 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
136616 if( !pIdx->hasStat1 ){
136628 db->aDb[iDb].zDbSName, pTab->zName);
136646 ** Call sqlite3_busy_timeout(db, N). Return the current timeout value
136649 ** disables the timeout.
136652 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
136656 returnSingleInt(v, db->busyTimeout);
136664 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
136666 ** specified and is a non-negative integer.
136667 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
136669 ** sqlite3_soft_heap_limit64(-1) C-language function.
136676 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
136687 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
136694 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
136697 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
136716 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
136730 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
136733 db->nAnalysisLimit = (int)(N&0x7fffffff);
136735 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
136748 pParse->nMem = 2;
136749 for(i=0; i<db->nDb; i++){
136753 if( db->aDb[i].zDbSName==0 ) continue;
136754 pBt = db->aDb[i].pBt;
136757 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
136761 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
136769 ** ---------- ------
136782 if( pPragma->iArg==2 || pPragma->iArg==3 ){
136792 n = pPragma->iArg<4 ? sqlite3Strlen30(zRight) : -1;
136794 if( (pPragma->iArg & 1)==0 ){
136810 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
136819 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
136824 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
136875 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
136880 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
136884 if( pPragma->mPragFlg & PragFlg_Result1 ){
136888 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
136894 assert( strlen(zBuf) < sizeof(zBuf)-1 );
136902 pTab->pName = pPragma;
136903 pTab->db = db;
136904 pTab->iHidden = i;
136905 pTab->nHidden = j;
136937 pIdxInfo->estimatedCost = (double)1;
136938 if( pTab->nHidden==0 ){ return SQLITE_OK; }
136939 pConstraint = pIdxInfo->aConstraint;
136942 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
136943 if( pConstraint->usable==0 ) continue;
136944 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
136945 if( pConstraint->iColumn < pTab->iHidden ) continue;
136946 j = pConstraint->iColumn - pTab->iHidden;
136951 pIdxInfo->estimatedCost = (double)2147483647;
136952 pIdxInfo->estimatedRows = 2147483647;
136955 j = seen[0]-1;
136956 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
136957 pIdxInfo->aConstraintUsage[j].omit = 1;
136959 pIdxInfo->estimatedCost = (double)20;
136960 pIdxInfo->estimatedRows = 20;
136961 j = seen[1]-1;
136962 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
136963 pIdxInfo->aConstraintUsage[j].omit = 1;
136973 pCsr->base.pVtab = pVtab;
136974 *ppCursor = &pCsr->base;
136981 sqlite3_finalize(pCsr->pPragma);
136982 pCsr->pPragma = 0;
136983 for(i=0; i<ArraySize(pCsr->azArg); i++){
136984 sqlite3_free(pCsr->azArg[i]);
136985 pCsr->azArg[i] = 0;
137003 pCsr->iRowid++;
137004 assert( pCsr->pPragma );
137005 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
137006 rc = sqlite3_finalize(pCsr->pPragma);
137007 pCsr->pPragma = 0;
137022 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137031 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
137034 assert( j<ArraySize(pCsr->azArg) );
137035 assert( pCsr->azArg[j]==0 );
137037 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
137038 if( pCsr->azArg[j]==0 ){
137043 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
137045 if( pCsr->azArg[1] ){
137046 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
137048 sqlite3_str_appendall(&acc, pTab->pName->zName);
137049 if( pCsr->azArg[0] ){
137050 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
137054 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
137057 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
137068 return (pCsr->pPragma==0);
137080 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
137081 if( i<pTab->iHidden ){
137082 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
137084 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
137094 *p = pCsr->iRowid;
137101 0, /* xCreate - create a table */
137102 pragmaVtabConnect, /* xConnect - connect to an existing table */
137103 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
137104 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
137105 0, /* xDestroy - Drop a table */
137106 pragmaVtabOpen, /* xOpen - open a cursor */
137107 pragmaVtabClose, /* xClose - close a cursor */
137108 pragmaVtabFilter, /* xFilter - configure scan constraints */
137109 pragmaVtabNext, /* xNext - advance a cursor */
137111 pragmaVtabColumn, /* xColumn - read data */
137112 pragmaVtabRowid, /* xRowid - read data */
137113 0, /* xUpdate - write data */
137114 0, /* xBegin - begin transaction */
137115 0, /* xSync - sync transaction */
137116 0, /* xCommit - commit transaction */
137117 0, /* xRollback - rollback transaction */
137118 0, /* xFindFunction - function overloading */
137119 0, /* xRename - rename the table */
137136 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
137137 assert( sqlite3HashFind(&db->aModule, zName)==0 );
137173 sqlite3 *db = pData->db;
137174 if( db->mallocFailed ){
137175 pData->rc = SQLITE_NOMEM_BKPT;
137176 }else if( pData->pzErrMsg[0]!=0 ){
137178 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
137184 *pData->pzErrMsg = sqlite3MPrintf(db,
137186 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
137189 pData->rc = SQLITE_ERROR;
137190 }else if( db->flags & SQLITE_WriteSchema ){
137191 pData->rc = SQLITE_CORRUPT_BKPT;
137196 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
137197 *pData->pzErrMsg = z;
137198 pData->rc = SQLITE_CORRUPT_BKPT;
137209 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
137210 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
137218 const char *zSql, /* UTF-8 encoded SQL statement. */
137243 sqlite3 *db = pData->db;
137244 int iDb = pData->iDb;
137248 assert( sqlite3_mutex_held(db->mutex) );
137249 db->mDbFlags |= DBFLAG_EncodingFixed;
137251 pData->nInitRow++;
137252 if( db->mallocFailed ){
137257 assert( iDb>=0 && iDb<db->nDb );
137264 ** But because db->init.busy is set to 1, no VDBE code is generated
137274 u8 saved_iDb = db->init.iDb;
137278 assert( db->init.busy );
137279 db->init.iDb = iDb;
137280 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
137281 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
137287 db->init.orphanTrigger = 0;
137288 db->init.azInit = (const char**)argv;
137290 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
137291 rc = db->errCode;
137293 db->init.iDb = saved_iDb;
137294 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
137296 if( db->init.orphanTrigger ){
137299 if( rc > pData->rc ) pData->rc = rc;
137307 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
137319 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
137323 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
137324 || pIndex->tnum<2
137325 || pIndex->tnum>pData->mxPage
137356 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
137358 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
137359 assert( iDb>=0 && iDb<db->nDb );
137360 assert( db->aDb[iDb].pSchema );
137361 assert( sqlite3_mutex_held(db->mutex) );
137362 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
137364 db->init.busy = 1;
137366 /* Construct the in-memory representation schema tables (sqlite_schema or
137370 ** the schema table as read-only. */
137386 db->mDbFlags &= mask;
137394 pDb = &db->aDb[iDb];
137395 if( pDb->pBt==0 ){
137402 /* If there is not already a read-only (or read-write) transaction opened
137403 ** on the b-tree database, open one now. If a transaction is opened, it
137405 sqlite3BtreeEnter(pDb->pBt);
137406 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
137407 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
137422 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
137433 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
137435 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
137438 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
137440 /* If opening a non-empty database, check the text encoding. For the
137445 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
137446 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
137450 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
137458 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
137466 pDb->pSchema->enc = ENC(db);
137468 if( pDb->pSchema->cache_size==0 ){
137470 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
137472 pDb->pSchema->cache_size = size;
137474 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
137476 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
137482 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
137485 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
137486 if( pDb->pSchema->file_format==0 ){
137487 pDb->pSchema->file_format = 1;
137489 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
137500 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
137501 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
137506 assert( db->init.busy );
137507 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
137512 db->aDb[iDb].zDbSName, zSchemaTabName);
137516 xAuth = db->xAuth;
137517 db->xAuth = 0;
137521 db->xAuth = xAuth;
137532 assert( pDb == &(db->aDb[iDb]) );
137533 if( db->mallocFailed ){
137536 pDb = &db->aDb[iDb];
137538 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
137559 sqlite3BtreeCommit(pDb->pBt);
137561 sqlite3BtreeLeave(pDb->pBt);
137570 db->init.busy = 0;
137575 ** Initialize all database files - the main database file, the file
137585 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
137587 assert( sqlite3_mutex_held(db->mutex) );
137588 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
137589 assert( db->init.busy==0 );
137591 assert( db->nDb>0 );
137598 for(i=db->nDb-1; i>0; i--){
137599 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
137612 ** This routine is a no-op if the database schema is already initialized.
137617 sqlite3 *db = pParse->db;
137618 assert( sqlite3_mutex_held(db->mutex) );
137619 if( !db->init.busy ){
137620 rc = sqlite3Init(db, &pParse->zErrMsg);
137622 pParse->rc = rc;
137623 pParse->nErr++;
137624 }else if( db->noSharedCache ){
137625 db->mDbFlags |= DBFLAG_SchemaKnownOk;
137634 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
137635 ** make no changes to pParse->rc.
137638 sqlite3 *db = pParse->db;
137643 assert( pParse->checkSchema );
137644 assert( sqlite3_mutex_held(db->mutex) );
137645 for(iDb=0; iDb<db->nDb; iDb++){
137647 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
137650 /* If there is not already a read-only (or read-write) transaction opened
137651 ** on the b-tree database, open one now. If a transaction is opened, it
137652 ** will be closed immediately after reading the meta-value. */
137657 pParse->rc = SQLITE_NOMEM;
137664 ** value stored as part of the in-memory schema representation,
137668 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
137670 pParse->rc = SQLITE_SCHEMA;
137682 ** which database file in db->aDb[] the schema refers to.
137688 int i = -32768;
137690 /* If pSchema is NULL, then return -32768. This happens when code in
137692 ** created by a sub-select). In this case the return value of this
137695 ** We return -32768 instead of the more usual -1 simply because using
137696 ** -32768 as the incorrect index into db->aDb[] is much
137697 ** more likely to cause a segfault than -1 (of course there are assert()
137699 ** -32768 will still fit into a 16-bit signed integer.
137701 assert( sqlite3_mutex_held(db->mutex) );
137704 assert( i<db->nDb );
137705 if( db->aDb[i].pSchema==pSchema ){
137709 assert( i>=0 && i<db->nDb );
137718 sqlite3 *db = pParse->db;
137720 assert( db->pParse==pParse );
137721 assert( pParse->nested==0 );
137723 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
137725 while( pParse->pCleanup ){
137726 ParseCleanup *pCleanup = pParse->pCleanup;
137727 pParse->pCleanup = pCleanup->pNext;
137728 pCleanup->xCleanup(db, pCleanup->pPtr);
137731 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
137732 if( pParse->pConstExpr ){
137733 sqlite3ExprListDelete(db, pParse->pConstExpr);
137735 assert( db->lookaside.bDisable >= pParse->disableLookaside );
137736 db->lookaside.bDisable -= pParse->disableLookaside;
137737 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
137738 assert( pParse->db->pParse==pParse );
137739 db->pParse = pParse->pOuterParse;
137740 pParse->db = 0;
137741 pParse->disableLookaside = 0;
137752 ** common cleanups, we save a single NULL-pointer comparison in
137757 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
137759 ** use-after-free errors following an OOM. The preferred way to do this is
137762 ** testcase( pParse->earlyCleanup );
137777 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
137779 pCleanup->pNext = pParse->pCleanup;
137780 pParse->pCleanup = pCleanup;
137781 pCleanup->pPtr = pPtr;
137782 pCleanup->xCleanup = xCleanup;
137784 xCleanup(pParse->db, pPtr);
137787 pParse->earlyCleanup = 1;
137805 assert( db->pParse!=pParse );
137806 pParse->pOuterParse = db->pParse;
137807 db->pParse = pParse;
137808 pParse->db = db;
137809 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
137821 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
137825 const char *zSql, /* UTF-8 encoded SQL statement. */
137839 sParse.pOuterParse = db->pParse;
137840 db->pParse = &sParse;
137844 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
137845 assert( sqlite3_mutex_held(db->mutex) );
137847 /* For a long-term use prepared statement avoid the use of
137858 ** some other database connection is holding a write-lock, which in
137879 if( !db->noSharedCache ){
137880 for(i=0; i<db->nDb; i++) {
137881 Btree *pBt = db->aDb[i].pBt;
137886 const char *zDb = db->aDb[i].zDbSName;
137888 testcase( db->flags & SQLITE_ReadUncommit );
137896 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
137899 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
137901 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
137912 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
137926 if( db->init.busy==0 ){
137927 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
137929 if( db->mallocFailed ){
137934 if( sParse.checkSchema && db->init.busy==0 ){
137959 sParse.pTriggerPrg = pT->pNext;
137970 const char *zSql, /* UTF-8 encoded SQL statement. */
137987 sqlite3_mutex_enter(db->mutex);
137995 if( rc==SQLITE_OK || db->mallocFailed ) break;
137997 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
138000 assert( (rc&db->errMask)==rc );
138001 db->busyHandler.nBusy = 0;
138002 sqlite3_mutex_leave(db->mutex);
138022 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
138026 assert( sqlite3_mutex_held(db->mutex) );
138028 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
138056 const char *zSql, /* UTF-8 encoded SQL statement. */
138068 const char *zSql, /* UTF-8 encoded SQL statement. */
138074 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
138086 const char *zSql, /* UTF-8 encoded SQL statement. */
138093 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
138110 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
138114 const void *zSql, /* UTF-16 encoded SQL statement. */
138120 /* This function currently works by first transforming the UTF-16
138121 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
138141 sqlite3_mutex_enter(db->mutex);
138144 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
138149 ** equivalent pointer into the UTF-16 string by counting the unicode
138151 ** the same number of characters into the UTF-16 string.
138153 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
138158 sqlite3_mutex_leave(db->mutex);
138172 const void *zSql, /* UTF-16 encoded SQL statement. */
138184 const void *zSql, /* UTF-16 encoded SQL statement. */
138196 const void *zSql, /* UTF-16 encoded SQL statement. */
138247 ** The aDefer[] array is used by the sorter-references optimization. For
138257 ** When the sorter-reference optimization is used, there is one entry in the
138266 int regReturn; /* Register holding block-output return address */
138267 int labelBkOut; /* Start label for the block-output subroutine */
138294 Select *pPrior = p->pPrior;
138295 sqlite3ExprListDelete(db, p->pEList);
138296 sqlite3SrcListDelete(db, p->pSrc);
138297 sqlite3ExprDelete(db, p->pWhere);
138298 sqlite3ExprListDelete(db, p->pGroupBy);
138299 sqlite3ExprDelete(db, p->pHaving);
138300 sqlite3ExprListDelete(db, p->pOrderBy);
138301 sqlite3ExprDelete(db, p->pLimit);
138302 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
138304 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
138305 sqlite3WindowListDelete(db, p->pWinDefn);
138307 while( p->pWin ){
138308 assert( p->pWin->ppThis==&p->pWin );
138309 sqlite3WindowUnlinkFromSelect(p->pWin);
138322 pDest->eDest = (u8)eDest;
138323 pDest->iSDParm = iParm;
138324 pDest->iSDParm2 = 0;
138325 pDest->zAffSdst = 0;
138326 pDest->iSdst = 0;
138327 pDest->nSdst = 0;
138338 SrcList *pSrc, /* the FROM clause -- which tables to scan */
138348 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
138350 assert( pParse->db->mallocFailed );
138355 sqlite3Expr(pParse->db,TK_ASTERISK,0));
138357 pNew->pEList = pEList;
138358 pNew->op = TK_SELECT;
138359 pNew->selFlags = selFlags;
138360 pNew->iLimit = 0;
138361 pNew->iOffset = 0;
138362 pNew->selId = ++pParse->nSelect;
138363 pNew->addrOpenEphm[0] = -1;
138364 pNew->addrOpenEphm[1] = -1;
138365 pNew->nSelectRow = 0;
138366 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
138367 pNew->pSrc = pSrc;
138368 pNew->pWhere = pWhere;
138369 pNew->pGroupBy = pGroupBy;
138370 pNew->pHaving = pHaving;
138371 pNew->pOrderBy = pOrderBy;
138372 pNew->pPrior = 0;
138373 pNew->pNext = 0;
138374 pNew->pLimit = pLimit;
138375 pNew->pWith = 0;
138377 pNew->pWin = 0;
138378 pNew->pWinDefn = 0;
138380 if( pParse->db->mallocFailed ) {
138381 clearSelect(pParse->db, pNew, pNew!=&standin);
138384 assert( pNew->pSrc!=0 || pParse->nErr>0 );
138398 ** Return a pointer to the right-most SELECT statement in a compound.
138401 while( p->pNext ) p = p->pNext;
138426 ** ------- ----- ----- ------------
138427 ** CROSS - - JT_CROSS
138428 ** INNER - - JT_INNER
138429 ** LEFT - - JT_LEFT|JT_OUTER
138430 ** LEFT OUTER - JT_LEFT|JT_OUTER
138431 ** RIGHT - - JT_RIGHT|JT_OUTER
138432 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
138433 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
138434 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
138435 ** NATURAL INNER - JT_NATURAL|JT_INNER
138436 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
138438 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
138440 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
138444 ** of other non-standard and in many cases non-sensical join types.
138449 ** INNER CROSS JOIN -> same as JOIN
138450 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
138451 ** OUTER LEFT JOIN -> same as LEFT JOIN
138452 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
138453 ** LEFT RIGHT JOIN -> same as FULL JOIN
138454 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
138455 ** CROSS CROSS CROSS JOIN -> same as JOIN
138494 if( p->n==aKeyword[j].nChar
138495 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
138523 ** Return the index of a column in a table. Return -1 if the column
138530 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
138531 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
138533 return -1;
138541 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
138542 if( pItem->fg.isNestedFrom ){
138544 assert( pItem->pSelect!=0 );
138545 pResults = pItem->pSelect->pEList;
138547 assert( iCol>=0 && iCol<pResults->nExpr );
138548 pResults->a[iCol].fg.bUsed = 1;
138554 ** table that has a column named zCol. The search is left-to-right.
138564 int iStart, /* First member of pSrc->a[] to check */
138565 int iEnd, /* Last member of pSrc->a[] to check */
138567 int *piTab, /* Write index of pSrc->a[] here */
138568 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
138574 assert( iEnd<pSrc->nSrc );
138579 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
138581 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
138584 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
138626 p->w.iJoin = iTable;
138627 if( p->op==TK_FUNCTION ){
138629 if( p->x.pList ){
138631 for(i=0; i<p->x.pList->nExpr; i++){
138632 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
138636 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
138637 p = p->pRight;
138657 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
138661 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
138664 if( p->op==TK_FUNCTION ){
138666 if( p->x.pList ){
138668 for(i=0; i<p->x.pList->nExpr; i++){
138669 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
138673 unsetJoinExpr(p->pLeft, iTable, nullable);
138674 p = p->pRight;
138691 ** The left most table is the first entry in Select.pSrc. The right-most
138705 pSrc = p->pSrc;
138706 pLeft = &pSrc->a[0];
138708 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
138709 Table *pRightTab = pRight->pTab;
138712 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
138713 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
138718 if( pRight->fg.jointype & JT_NATURAL ){
138720 if( pRight->fg.isUsing || pRight->u3.pOn ){
138725 for(j=0; j<pRightTab->nCol; j++){
138728 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
138729 zName = pRightTab->aCol[j].zCnName;
138733 assert( pUsing->nId>0 );
138734 assert( pUsing->a[pUsing->nId-1].zName==0 );
138735 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
138740 pRight->fg.isUsing = 1;
138741 pRight->fg.isSynthUsing = 1;
138742 pRight->u3.pUsing = pUsing;
138744 if( pParse->nErr ) return 1;
138754 if( pRight->fg.isUsing ){
138755 IdList *pList = pRight->u3.pUsing;
138756 sqlite3 *db = pParse->db;
138758 for(j=0; j<pList->nId; j++){
138767 zName = pList->a[j].zName;
138771 pRight->fg.isSynthUsing)==0
138773 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
138778 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
138779 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
138782 ** contains the zName column, then this branch is a no-op.
138789 ** non-USING references to zName on the left of an INNER or LEFT
138796 pRight->fg.isSynthUsing)!=0 ){
138797 if( pSrc->a[iLeft].fg.isUsing==0
138798 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
138806 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
138821 pEq->w.iJoin = pE2->iTable;
138823 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
138830 else if( pRight->u3.pOn ){
138831 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
138832 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
138833 pRight->u3.pOn = 0;
138834 pRight->fg.isOn = 1;
138863 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
138864 0, pInfo->ecelFlags);
138866 if( pInfo->pExtra ){
138867 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
138868 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
138886 int nOBSat = pSort->nOBSat;
138887 Vdbe *v = pParse->pVdbe;
138888 int regOut = ++pParse->nMem;
138889 if( pSort->pDeferredRowLoad ){
138890 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
138892 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
138898 ** through regData+nData-1 onto the sorter.
138909 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
138910 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
138911 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
138915 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
138939 regBase = regData - nPrefixReg;
138941 regBase = pParse->nMem + 1;
138942 pParse->nMem += nBase;
138944 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
138945 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
138946 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
138947 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
138950 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
138964 regPrevKey = pParse->nMem+1;
138965 pParse->nMem += pSort->nOBSat;
138966 nKey = nExpr - pSort->nOBSat + bSeq;
138970 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
138973 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
138974 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
138975 if( pParse->db->mallocFailed ) return;
138976 pOp->p2 = nKey + nData;
138977 pKI = pOp->p4.pKeyInfo;
138978 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
138979 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
138980 testcase( pKI->nAllField > pKI->nKeyField+2 );
138981 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
138982 pKI->nAllField-pKI->nKeyField-1);
138986 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
138987 pSort->regReturn = ++pParse->nMem;
138988 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
138989 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
138991 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
138995 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
139009 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
139014 int iCsr = pSort->iECursor;
139019 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
139026 if( pSort->sortFlags & SORTFLAG_UseSorter ){
139031 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
139032 regBase+nOBSat, nBase-nOBSat);
139035 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
139105 int nResultCol = pEList->nExpr;
139106 Vdbe *v = pParse->pVdbe;
139115 iRet = regPrev = pParse->nMem+1;
139116 pParse->nMem += nResultCol;
139120 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
139121 if( i<nResultCol-1 ){
139128 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
139131 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
139132 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
139167 ** No adjustments necessary. This function is a no-op.
139186 if( pParse->nErr==0
139189 Vdbe *v = pParse->pVdbe;
139191 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
139200 pOp->opcode = OP_Null;
139201 pOp->p1 = 1;
139202 pOp->p2 = iVal;
139209 ** This function is called as part of inner-loop generation for a SELECT
139211 ** determines the expressions, if any, that the sorter-reference
139212 ** optimization should be used for. The sorter-reference optimization
139225 ** for which the sorter-reference optimization should be enabled.
139226 ** Additionally, the pSort->aDefer[] array is populated with entries
139241 for(i=0; i<pEList->nExpr; i++){
139242 struct ExprList_item *pItem = &pEList->a[i];
139243 if( pItem->u.x.iOrderByCol==0 ){
139244 Expr *pExpr = pItem->pExpr;
139246 if( pExpr->op==TK_COLUMN
139247 && pExpr->iColumn>=0
139249 && (pTab = pExpr->y.pTab)!=0
139251 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
139255 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
139258 if( nDefer==ArraySize(pSort->aDefer) ){
139266 nKey = pPk->nKeyCol;
139271 pNew->iTable = pExpr->iTable;
139273 pNew->y.pTab = pExpr->y.pTab;
139274 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
139278 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
139279 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
139280 pSort->aDefer[nDefer].nKey = nKey;
139284 pItem->fg.bSorterRef = 1;
139288 pSort->nDefer = (u8)nDefer;
139297 ** If srcTab is negative, then the p->pEList expressions
139299 ** zero or more, then data is pulled from srcTab and p->pEList is used only
139305 int srcTab, /* Pull data from this table if non-negative */
139312 Vdbe *v = pParse->pVdbe;
139315 int eDest = pDest->eDest; /* How to dispose of results */
139316 int iParm = pDest->iSDParm; /* First argument to disposal method */
139324 ** values for any expressions that are also part of the sort-key are omitted
139330 assert( p->pEList!=0 );
139331 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
139332 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
139335 if( hasDistinct && (pDistinct->eTnctType==WHERE_DISTINCT_UNIQUE) ){
139337 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
139343 codeOffset(v, p->iOffset, iContinue);
139346 if( p->iLimit ){
139347 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak);
139358 nResultCol = p->pEList->nExpr;
139360 if( pDest->iSdst==0 ){
139362 nPrefixReg = pSort->pOrderBy->nExpr;
139363 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
139364 pParse->nMem += nPrefixReg;
139366 pDest->iSdst = pParse->nMem+1;
139367 pParse->nMem += nResultCol;
139368 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
139370 ** on the right-hand side of an INSERT contains more result columns than
139374 pParse->nMem += nResultCol;
139376 pDest->nSdst = nResultCol;
139377 regOrig = regResult = pDest->iSdst;
139381 VdbeComment((v, "%s", p->pEList->a[i].zEName));
139398 /* For each expression in p->pEList that is a copy of an expression in
139399 ** the ORDER BY clause (pSort->pOrderBy), set the associated
139401 ** expression within the sort-key that pushOntoSorter() will generate.
139402 ** This allows the p->pEList field to be omitted from the sorted record,
139406 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
139408 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
139409 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
139413 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
139414 if( pExtra && pParse->db->mallocFailed==0 ){
139420 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
139421 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
139422 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
139423 pParse->nMem += pExtra->nExpr;
139429 pEList = p->pEList;
139430 for(i=0; i<pEList->nExpr; i++){
139431 if( pEList->a[i].u.x.iOrderByCol>0
139433 || pEList->a[i].fg.bSorterRef
139436 nResultCol--;
139455 if( pExtra ) nResultCol += pExtra->nExpr;
139457 if( p->iLimit
139463 pSort->pDeferredRowLoad = &sRowLoadInfo;
139475 int eType = pDistinct->eTnctType;
139476 int iTab = pDistinct->tabTnct;
139477 assert( nResultCol==p->pEList->nExpr );
139478 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
139479 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
139481 codeOffset(v, p->iOffset, iContinue);
139484 if( p->iLimit ){
139485 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak);
139531 if( pDest->zAffSdst ){
139532 sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol);
139567 int i2 = pDest->iSDParm2;
139576 regResult+(i2<0), nResultCol-(i2<0), r1);
139601 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
139603 r1, pDest->zAffSdst, nResultCol);
139625 assert( nResultCol<=pDest->nSdst );
139629 assert( nResultCol==pDest->nSdst );
139637 case SRT_Coroutine: /* Send data to a co-routine */
139645 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
139654 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
139655 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
139656 ** pSO->nExpr columns, then make sure all keys are unique by adding a
139665 pSO = pDest->pOrderBy;
139667 nKey = pSO->nExpr;
139686 regResult + pSO->a[i].u.x.iOrderByCol - 1,
139704 ** user-defined functions that have side effects. We do not care
139718 if( pSort==0 && p->iLimit ){
139719 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
139728 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
139731 p->aSortFlags = (u8*)&p->aColl[N+X];
139732 p->nKeyField = (u16)N;
139733 p->nAllField = (u16)(N+X);
139734 p->enc = ENC(db);
139735 p->db = db;
139736 p->nRef = 1;
139749 assert( p->db!=0 );
139750 assert( p->nRef>0 );
139751 p->nRef--;
139752 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
139761 assert( p->nRef>0 );
139762 p->nRef++;
139774 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
139800 sqlite3 *db = pParse->db;
139803 nExpr = pList->nExpr;
139804 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
139807 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
139808 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
139809 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
139832 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
139835 ** "USE TEMP B-TREE FOR xxx"
139841 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
139845 ** Assign expression b to lvalue a. A second, no-op, version of this macro
139854 /* No-op versions of the explainXXX() functions and macros. */
139861 ** If the inner loop was generated using a non-null pOrderBy argument,
139873 Vdbe *v = pParse->pVdbe; /* The prepared statement */
139874 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
139879 ExprList *pOrderBy = pSort->pOrderBy;
139880 int eDest = pDest->eDest;
139881 int iParm = pDest->iSDParm;
139890 struct ExprList_item *aOutEx = p->pEList->a;
139893 if( pSort->labelBkOut ){
139894 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
139896 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
139900 /* Open any cursors needed for sorter-reference expressions */
139901 for(i=0; i<pSort->nDefer; i++){
139902 Table *pTab = pSort->aDefer[i].pTab;
139903 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
139904 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
139905 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
139909 iTab = pSort->iECursor;
139911 if( eDest==SRT_Mem && p->iOffset ){
139912 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
139915 regRow = pDest->iSdst;
139925 nKey = pOrderBy->nExpr - pSort->nOBSat;
139926 if( pSort->sortFlags & SORTFLAG_UseSorter ){
139927 int regSortOut = ++pParse->nMem;
139928 iSortTab = pParse->nTab++;
139929 if( pSort->labelBkOut ){
139937 assert( p->iLimit==0 && p->iOffset==0 );
139948 codeOffset(v, p->iOffset, addrContinue);
139957 if( p->iOffset>0 ){
139958 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
139961 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
139968 if( pSort->nDefer ){
139972 for(i=0; i<pSort->nDefer; i++){
139973 int iCsr = pSort->aDefer[i].iCsr;
139974 Table *pTab = pSort->aDefer[i].pTab;
139975 int nKey = pSort->aDefer[i].nKey;
139985 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
139998 for(i=nColumn-1; i>=0; i--){
140007 iRead = aOutEx[i].u.x.iOrderByCol-1;
140009 iRead = iCol--;
140026 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
140028 pDest->zAffSdst, nColumn);
140038 int i2 = pDest->iSDParm2;
140040 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
140053 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
140055 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
140071 if( pSort->sortFlags & SORTFLAG_UseSorter ){
140076 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
140088 ** result-set expression in all of the following SELECT statements is
140099 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
140126 assert( pNC->pSrcList!=0 );
140127 switch( pExpr->op ){
140135 int iCol = pExpr->iColumn; /* Index of column in pTab */
140137 SrcList *pTabList = pNC->pSrcList;
140138 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
140139 if( j<pTabList->nSrc ){
140140 pTab = pTabList->a[j].pTab;
140141 pS = pTabList->a[j].pSelect;
140143 pNC = pNC->pNext;
140158 ** sub-select. In this case, set the column type to NULL, even
140168 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
140170 /* The "table" is actually a sub-select or a view in the FROM clause
140172 ** data for the result-set column of the sub-select.
140174 if( iCol<pS->pEList->nExpr
140182 ** rowid of the sub-select or view. This expression is legal (see
140183 ** test case misc2.2.2) - it always evaluates to NULL.
140186 Expr *p = pS->pEList->a[iCol].pExpr;
140187 sNC.pSrcList = pS->pSrc;
140189 sNC.pParse = pNC->pParse;
140196 if( iCol<0 ) iCol = pTab->iPKey;
140197 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140202 zOrigCol = pTab->aCol[iCol].zCnName;
140203 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
140205 zOrigTab = pTab->zName;
140206 if( pNC->pParse && pTab->pSchema ){
140207 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
140208 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
140211 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
140215 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
140223 /* The expression is a sub-select. Return the declaration type and
140231 pS = pExpr->x.pSelect;
140232 p = pS->pEList->a[0].pExpr;
140233 sNC.pSrcList = pS->pSrc;
140235 sNC.pParse = pNC->pParse;
140263 Vdbe *v = pParse->pVdbe;
140269 for(i=0; i<pEList->nExpr; i++){
140270 Expr *p = pEList->a[i].pExpr;
140278 /* The vdbe must make its own copy of the column-type and other
140328 Vdbe *v = pParse->pVdbe;
140333 sqlite3 *db = pParse->db;
140339 if( pParse->explain ){
140344 if( pParse->colNamesSet ) return;
140345 /* Column names are determined by the left-most term of a compound select */
140346 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
140348 pTabList = pSelect->pSrc;
140349 pEList = pSelect->pEList;
140352 pParse->colNamesSet = 1;
140353 fullName = (db->flags & SQLITE_FullColNames)!=0;
140354 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
140355 sqlite3VdbeSetNumCols(v, pEList->nExpr);
140356 for(i=0; i<pEList->nExpr; i++){
140357 Expr *p = pEList->a[i].pExpr;
140360 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
140361 assert( p->op!=TK_COLUMN
140362 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
140363 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
140365 char *zName = pEList->a[i].zEName;
140367 }else if( srcName && p->op==TK_COLUMN ){
140369 int iCol = p->iColumn;
140370 pTab = p->y.pTab;
140372 if( iCol<0 ) iCol = pTab->iPKey;
140373 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
140377 zCol = pTab->aCol[iCol].zCnName;
140381 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
140387 const char *z = pEList->a[i].zEName;
140423 sqlite3 *db = pParse->db; /* Database connection */
140435 nCol = pEList->nExpr;
140447 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
140448 struct ExprList_item *pX = &pEList->a[i];
140452 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
140455 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
140456 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
140457 pColExpr = pColExpr->pRight;
140460 if( pColExpr->op==TK_COLUMN
140462 && ALWAYS( pColExpr->y.pTab!=0 )
140465 int iCol = pColExpr->iColumn;
140466 pTab = pColExpr->y.pTab;
140467 if( iCol<0 ) iCol = pTab->iPKey;
140468 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
140469 }else if( pColExpr->op==TK_ID ){
140471 zName = pColExpr->u.zToken;
140474 assert( zName==pX->zEName ); /* pointer comparison intended */
140488 if( pCollide->fg.bUsingTerm ){
140489 pCol->colFlags |= COLFLAG_NOEXPAND;
140493 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
140499 pCol->zCnName = zName;
140500 pCol->hName = sqlite3StrIHash(zName);
140501 if( pX->fg.bNoExpand ){
140502 pCol->colFlags |= COLFLAG_NOEXPAND;
140510 if( db->mallocFailed ){
140539 sqlite3 *db = pParse->db;
140548 assert( (pSelect->selFlags & SF_Resolved)!=0 );
140549 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
140550 if( db->mallocFailed ) return;
140552 sNC.pSrcList = pSelect->pSrc;
140553 a = pSelect->pEList->a;
140554 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
140557 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
140560 /* pCol->szEst = ... // Column size est for SELECT tables never used */
140561 pCol->affinity = sqlite3ExprAffinity(p);
140564 n = sqlite3Strlen30(pCol->zCnName);
140565 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
140566 if( pCol->zCnName ){
140567 memcpy(&pCol->zCnName[n+1], zType, m+1);
140568 pCol->colFlags |= COLFLAG_HASTYPE;
140570 testcase( pCol->colFlags & COLFLAG_HASTYPE );
140571 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
140574 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
140577 assert( pTab->pIndex==0 );
140578 sqlite3ColumnSetColl(db, pCol, pColl->zName);
140581 pTab->szTabRow = 1; /* Any non-zero value works */
140590 sqlite3 *db = pParse->db;
140593 savedFlags = db->flags;
140594 db->flags &= ~(u64)SQLITE_FullColNames;
140595 db->flags |= SQLITE_ShortColNames;
140597 db->flags = savedFlags;
140598 if( pParse->nErr ) return 0;
140599 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
140604 pTab->nTabRef = 1;
140605 pTab->zName = 0;
140606 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
140607 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
140609 pTab->iPKey = -1;
140610 if( db->mallocFailed ){
140622 if( pParse->pVdbe ){
140623 return pParse->pVdbe;
140625 if( pParse->pToplevel==0
140626 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
140628 pParse->okConstFactor = 1;
140636 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
140644 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
140652 ** Only if pLimit->pLeft!=0 do the limit registers get
140662 Expr *pLimit = p->pLimit;
140664 if( p->iLimit ) return;
140667 ** "LIMIT -1" always shows all rows. There is some
140673 assert( pLimit->op==TK_LIMIT );
140674 assert( pLimit->pLeft!=0 );
140675 p->iLimit = iLimit = ++pParse->nMem;
140678 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
140683 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
140684 p->nSelectRow = sqlite3LogEst((u64)n);
140685 p->selFlags |= SF_FixedLimit;
140688 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
140693 if( pLimit->pRight ){
140694 p->iOffset = iOffset = ++pParse->nMem;
140695 pParse->nMem++; /* Allocate an extra register for limit+offset */
140696 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
140707 ** Return the appropriate collating sequence for the iCol-th column of
140708 ** the result set for the compound-select statement "p". Return NULL if
140712 ** left-most term of the select that has a collating sequence.
140716 if( p->pPrior ){
140717 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
140722 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
140725 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
140726 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
140741 ExprList *pOrderBy = p->pOrderBy;
140742 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
140743 sqlite3 *db = pParse->db;
140748 struct ExprList_item *pItem = &pOrderBy->a[i];
140749 Expr *pTerm = pItem->pExpr;
140752 if( pTerm->flags & EP_Collate ){
140755 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
140756 if( pColl==0 ) pColl = db->pDfltColl;
140757 pOrderBy->a[i].pExpr =
140758 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
140761 pRet->aColl[i] = pColl;
140762 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
140774 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
140776 ** p->pPrior p
140779 ** There is exactly one reference to the recursive-table in the FROM clause
140780 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
140782 ** The setup-query runs once to generate an initial set of rows that go
140786 ** recursive-table for a recursive-query run. The output of the recursive-query
140811 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
140812 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
140813 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
140815 Select *pFirstRec; /* Left-most recursive term */
140831 if( p->pWin ){
140842 p->nSelectRow = 320; /* 4 billion rows */
140844 pLimit = p->pLimit;
140845 regLimit = p->iLimit;
140846 regOffset = p->iOffset;
140847 p->pLimit = 0;
140848 p->iLimit = p->iOffset = 0;
140849 pOrderBy = p->pOrderBy;
140852 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
140853 if( pSrc->a[i].fg.isRecursive ){
140854 iCurrent = pSrc->a[i].iCursor;
140862 iQueue = pParse->nTab++;
140863 if( p->op==TK_UNION ){
140865 iDistinct = pParse->nTab++;
140872 regCurrent = ++pParse->nMem;
140876 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
140884 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
140885 p->selFlags |= SF_UsesEphemeral;
140889 p->pOrderBy = 0;
140895 ** iDistinct table. pFirstRec is left pointing to the left-most
140898 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
140899 if( pFirstRec->selFlags & SF_Aggregate ){
140903 pFirstRec->op = TK_ALL;
140904 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
140907 /* Store the results of the setup-query in Queue. */
140908 pSetup = pFirstRec->pPrior;
140909 pSetup->pNext = 0;
140912 pSetup->pNext = p;
140921 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
140939 ** the value for the recursive-table. Store the results in the Queue.
140941 pFirstRec->pPrior = 0;
140944 assert( pFirstRec->pPrior==0 );
140945 pFirstRec->pPrior = pSetup;
140952 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
140953 p->pOrderBy = pOrderBy;
140954 p->pLimit = pLimit;
140962 Select *p, /* The right-most of SELECTs to be coded */
140967 ** Handle the special case of a compound-select that originates from a
140980 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
140984 Select *p, /* The right-most of SELECTs to be coded */
140989 int bShowAll = p->pLimit==0;
140990 assert( p->selFlags & SF_MultiValue );
140992 assert( p->selFlags & SF_Values );
140993 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
140994 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
140996 if( p->pWin ) return -1;
140998 if( p->pPrior==0 ) break;
140999 assert( p->pPrior->pNext==p );
141000 p = p->pPrior;
141006 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
141008 p->nSelectRow = nRow;
141009 p = p->pNext;
141020 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
141029 ** "p" points to the right-most of the two queries. the query on the
141030 ** left is p->pPrior. The left query could also be a compound query
141036 ** Example 1: Consider a three-way compound SQL statement.
141044 ** `-----> SELECT b FROM t2
141046 ** `------> SELECT a FROM t1
141050 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
141057 Select *p, /* The right-most of SELECTs to be coded */
141068 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
141070 assert( p && p->pPrior ); /* Calling function guarantees this much */
141071 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
141072 assert( p->selFlags & SF_Compound );
141073 db = pParse->db;
141074 pPrior = p->pPrior;
141076 assert( pPrior->pOrderBy==0 );
141077 assert( pPrior->pLimit==0 );
141085 assert( p->pEList );
141086 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
141090 /* Special handling for a compound-select that originates as a VALUES clause.
141092 if( p->selFlags & SF_MultiValue ){
141101 assert( p->pEList && pPrior->pEList );
141102 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
141105 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
141112 if( p->pOrderBy ){
141117 if( pPrior->pPrior==0 ){
141119 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
141125 switch( p->op ){
141129 assert( !pPrior->pLimit );
141130 pPrior->iLimit = p->iLimit;
141131 pPrior->iOffset = p->iOffset;
141132 pPrior->pLimit = p->pLimit;
141135 pPrior->pLimit = 0;
141139 p->pPrior = 0;
141140 p->iLimit = pPrior->iLimit;
141141 p->iOffset = pPrior->iOffset;
141142 if( p->iLimit ){
141143 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
141145 if( p->iOffset ){
141147 p->iLimit, p->iOffset+1, p->iOffset);
141154 pDelete = p->pPrior;
141155 p->pPrior = pPrior;
141156 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141157 if( p->pLimit
141158 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
141159 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
141161 p->nSelectRow = sqlite3LogEst((u64)nLimit);
141173 Expr *pLimit; /* Saved values of p->nLimit */
141177 testcase( p->op==TK_EXCEPT );
141178 testcase( p->op==TK_UNION );
141184 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
141190 unionTab = pParse->nTab++;
141191 assert( p->pOrderBy==0 );
141193 assert( p->addrOpenEphm[0] == -1 );
141194 p->addrOpenEphm[0] = addr;
141195 findRightmost(p)->selFlags |= SF_UsesEphemeral;
141196 assert( p->pEList );
141202 assert( !pPrior->pOrderBy );
141212 if( p->op==TK_EXCEPT ){
141215 assert( p->op==TK_UNION );
141218 p->pPrior = 0;
141219 pLimit = p->pLimit;
141220 p->pLimit = 0;
141222 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
141223 sqlite3SelectOpName(p->op)));
141227 assert( p->pOrderBy==0 );
141228 pDelete = p->pPrior;
141229 p->pPrior = pPrior;
141230 p->pOrderBy = 0;
141231 if( p->op==TK_UNION ){
141232 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141234 sqlite3ExprDelete(db, p->pLimit);
141235 p->pLimit = pLimit;
141236 p->iLimit = 0;
141237 p->iOffset = 0;
141243 assert( p->pEList || db->mallocFailed );
141244 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
141260 default: assert( p->op==TK_INTERSECT ); {
141272 tab1 = pParse->nTab++;
141273 tab2 = pParse->nTab++;
141274 assert( p->pOrderBy==0 );
141277 assert( p->addrOpenEphm[0] == -1 );
141278 p->addrOpenEphm[0] = addr;
141279 findRightmost(p)->selFlags |= SF_UsesEphemeral;
141280 assert( p->pEList );
141294 assert( p->addrOpenEphm[1] == -1 );
141295 p->addrOpenEphm[1] = addr;
141296 p->pPrior = 0;
141297 pLimit = p->pLimit;
141298 p->pLimit = 0;
141300 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
141301 sqlite3SelectOpName(p->op)));
141305 pDelete = p->pPrior;
141306 p->pPrior = pPrior;
141307 if( p->nSelectRow>pPrior->nSelectRow ){
141308 p->nSelectRow = pPrior->nSelectRow;
141310 sqlite3ExprDelete(db, p->pLimit);
141311 p->pLimit = pLimit;
141317 assert( p->pEList );
141339 if( p->pNext==0 ){
141344 if( pParse->nErr ) goto multi_select_end;
141350 ** This section is run by the right-most SELECT statement only.
141351 ** SELECT statements to the left always skip this part. The right-most
141355 if( p->selFlags & SF_UsesEphemeral ){
141359 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
141362 assert( p->pNext==0 );
141363 assert( p->pEList!=0 );
141364 nCol = p->pEList->nExpr;
141370 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
141373 *apColl = db->pDfltColl;
141377 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
141379 int addr = pLoop->addrOpenEphm[i];
141383 assert( pLoop->addrOpenEphm[1]<0 );
141389 pLoop->addrOpenEphm[i] = -1;
141396 pDest->iSdst = dest.iSdst;
141397 pDest->nSdst = dest.nSdst;
141412 if( p->selFlags & SF_Values ){
141417 sqlite3SelectOpName(p->op));
141425 ** The data to be output is contained in pIn->iSdst. There are
141426 ** pIn->nSdst columns to be output. pDest is where the output should
141438 ** If the LIMIT found in p->iLimit is reached, jump immediately to
141451 Vdbe *v = pParse->pVdbe;
141463 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
141467 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
141470 if( pParse->db->mallocFailed ) return 0;
141474 codeOffset(v, p->iOffset, iContinue);
141476 assert( pDest->eDest!=SRT_Exists );
141477 assert( pDest->eDest!=SRT_Table );
141478 switch( pDest->eDest ){
141484 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
141485 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
141486 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
141498 testcase( pIn->nSdst>1 );
141500 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
141501 r1, pDest->zAffSdst, pIn->nSdst);
141502 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
141503 pIn->iSdst, pIn->nSdst);
141511 ** if it is the RHS of a row-value IN operator.
141514 testcase( pIn->nSdst>1 );
141515 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
141522 ** starting at pDest->iSdst. Then the co-routine yields.
141525 if( pDest->iSdst==0 ){
141526 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
141527 pDest->nSdst = pIn->nSdst;
141529 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
141530 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
141543 assert( pDest->eDest==SRT_Output );
141544 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
141551 if( p->iLimit ){
141552 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
141573 ** co-routines. Then run the co-routines in parallel and merge the results
141600 ** ------------- ----------------- -------------- -----------------
141651 Select *p, /* The right-most of SELECTs to be coded */
141656 Select *pSplit; /* Left-most SELECT in the right-hand group */
141661 int regAddrA; /* Address register for select-A coroutine */
141662 int regAddrB; /* Address register for select-B coroutine */
141663 int addrSelectA; /* Address of the select-A coroutine */
141664 int addrSelectB; /* Address of the select-B coroutine */
141665 int regOutA; /* Address register for the output-A subroutine */
141666 int regOutB; /* Address register for the output-B subroutine */
141667 int addrOutA; /* Address of the output-A subroutine */
141668 int addrOutB = 0; /* Address of the output-B subroutine */
141669 int addrEofA; /* Address of the select-A-exhausted subroutine */
141671 int addrEofB; /* Address of the select-B-exhausted subroutine */
141675 int regLimitA; /* Limit register for select-A */
141676 int regLimitB; /* Limit register for select-A */
141678 int savedLimit; /* Saved value of p->iLimit */
141679 int savedOffset; /* Saved value of p->iOffset */
141681 int labelEnd; /* Label for the end of the overall SELECT stmt */
141691 assert( p->pOrderBy!=0 );
141693 db = pParse->db;
141694 v = pParse->pVdbe;
141702 op = p->op;
141703 assert( p->pPrior->pOrderBy==0 );
141704 pOrderBy = p->pOrderBy;
141706 nOrderBy = pOrderBy->nExpr;
141713 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
141715 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
141717 assert( pItem->u.x.iOrderByCol>0 );
141718 if( pItem->u.x.iOrderByCol==i ) break;
141723 pNew->flags |= EP_IntValue;
141724 pNew->u.iValue = i;
141725 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
141726 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
141742 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
141744 assert( pItem->u.x.iOrderByCol>0 );
141745 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
141746 aPermute[i] = pItem->u.x.iOrderByCol - 1;
141760 int nExpr = p->pEList->nExpr;
141761 assert( nOrderBy>=nExpr || db->mallocFailed );
141762 regPrev = pParse->nMem+1;
141763 pParse->nMem += nExpr+1;
141769 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
141770 pKeyDup->aSortFlags[i] = 0;
141781 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
141783 assert( pSplit->pPrior->pNext==pSplit );
141790 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
141792 pPrior = pSplit->pPrior;
141794 pSplit->pPrior = 0;
141795 pPrior->pNext = 0;
141796 assert( p->pOrderBy == pOrderBy );
141797 assert( pOrderBy!=0 || db->mallocFailed );
141798 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
141799 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
141800 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
141804 if( p->iLimit && op==TK_ALL ){
141805 regLimitA = ++pParse->nMem;
141806 regLimitB = ++pParse->nMem;
141807 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
141813 sqlite3ExprDelete(db, p->pLimit);
141814 p->pLimit = 0;
141816 regAddrA = ++pParse->nMem;
141817 regAddrB = ++pParse->nMem;
141818 regOutA = ++pParse->nMem;
141819 regOutB = ++pParse->nMem;
141823 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
141826 ** left of the compound operator - the "A" select.
141831 pPrior->iLimit = regLimitA;
141838 ** the right - the "B" select
141843 savedLimit = p->iLimit;
141844 savedOffset = p->iOffset;
141845 p->iLimit = regLimitB;
141846 p->iOffset = 0;
141849 p->iLimit = savedLimit;
141850 p->iOffset = savedOffset;
141878 VdbeNoopComment((v, "eof-A subroutine"));
141883 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
141891 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
141893 VdbeNoopComment((v, "eof-B subroutine"));
141901 VdbeNoopComment((v, "A-lt-B subroutine"));
141914 VdbeNoopComment((v, "A-eq-B subroutine"));
141922 VdbeNoopComment((v, "A-gt-B subroutine"));
141951 if( pSplit->pPrior ){
141953 (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior);
141955 pSplit->pPrior = pPrior;
141956 pPrior->pNext = pSplit;
141957 sqlite3ExprListDelete(db, pPrior->pOrderBy);
141958 pPrior->pOrderBy = 0;
141963 return pParse->nErr!=0;
141978 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
142001 ** when processing a non-matched row of the left.
142018 ** a column in table number iTable with a copy of the iColumn-th
142035 && pExpr->w.iJoin==pSubst->iTable
142038 pExpr->w.iJoin = pSubst->iNewTable;
142040 if( pExpr->op==TK_COLUMN
142041 && pExpr->iTable==pSubst->iTable
142045 if( pExpr->iColumn<0 ){
142046 pExpr->op = TK_NULL;
142051 int iColumn = pExpr->iColumn;
142052 Expr *pCopy = pSubst->pEList->a[iColumn].pExpr;
142054 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
142055 assert( pExpr->pRight==0 );
142057 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
142059 sqlite3 *db = pSubst->pParse->db;
142060 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
142064 ifNullRow.iTable = pSubst->iNewTable;
142065 ifNullRow.iColumn = -99;
142071 if( db->mallocFailed ){
142075 if( pSubst->isOuterJoin ){
142079 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
142080 pExpr->flags & (EP_OuterON|EP_InnerON));
142084 if( pExpr->op==TK_TRUEFALSE ){
142085 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
142086 pExpr->op = TK_INTEGER;
142091 ** just as it did when it was a column of a view or sub-query. */
142093 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
142094 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
142095 pSubst->pCList->a[iColumn].pExpr
142097 if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){
142098 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
142099 (pColl ? pColl->zName : "BINARY")
142107 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
142108 pExpr->iTable = pSubst->iNewTable;
142110 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
142111 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
142113 substSelect(pSubst, pExpr->x.pSelect, 1);
142115 substExprList(pSubst, pExpr->x.pList);
142119 Window *pWin = pExpr->y.pWin;
142120 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
142121 substExprList(pSubst, pWin->pPartition);
142122 substExprList(pSubst, pWin->pOrderBy);
142134 for(i=0; i<pList->nExpr; i++){
142135 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
142141 int doPrior /* Do substitutes on p->pPrior too */
142148 substExprList(pSubst, p->pEList);
142149 substExprList(pSubst, p->pGroupBy);
142150 substExprList(pSubst, p->pOrderBy);
142151 p->pHaving = substExpr(pSubst, p->pHaving);
142152 p->pWhere = substExpr(pSubst, p->pWhere);
142153 pSrc = p->pSrc;
142155 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
142156 substSelect(pSubst, pItem->pSelect, 1);
142157 if( pItem->fg.isTabFunc ){
142158 substExprList(pSubst, pItem->u1.pFuncArg);
142161 }while( doPrior && (p = p->pPrior)!=0 );
142171 ** pSrcItem->colUsed mask.
142175 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
142176 pItem = pWalker->u.pSrcItem;
142177 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
142178 if( pExpr->iColumn<0 ) return WRC_Continue;
142179 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
142187 if( NEVER(pSrcItem->pTab==0) ) return;
142192 pSrcItem->colUsed = 0;
142208 ** If pSrc contains any sub-selects, call this routine recursively
142209 ** on the FROM clause of each such sub-select, with iExcept set to -1.
142219 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
142222 assert( pItem->iCursor < aCsrMap[0] );
142223 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
142224 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
142226 pItem->iCursor = aCsrMap[pItem->iCursor+1];
142227 for(p=pItem->pSelect; p; p=p->pPrior){
142228 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
142238 int *aCsrMap = pWalker->u.aiCol;
142250 int op = pExpr->op;
142252 renumberCursorDoMapping(pWalker, &pExpr->iTable);
142255 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
142263 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
142285 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
142300 while( pSel->pPrior ){
142301 pSel = pSel->pPrior;
142303 return pSel->pEList;
142342 ** other than the one FROM-clause subquery that is a candidate
142344 ** from 2015-02-09.)
142358 ** sub-queries that were excluded from this optimization. Restriction
142373 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
142374 ** accidently carried the comment forward until 2014-09-15. Original
142411 ** The parent and sub-query may contain WHERE clauses. Subject to
142418 ** Also, each component of the sub-query must return the same number
142421 ** such (illegal) sub-query is flattened. The caller will detect the
142424 ** (18) If the sub-query is a compound select, then all terms of the
142431 ** (20) If the sub-query is a compound select, then it must not use
142442 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
142448 ** The subquery may not be an aggregate that uses the built-in min() or
142470 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
142473 ** If flattening is not attempted, this routine is a no-op and returns 0.
142482 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
142485 const char *zSavedAuthContext = pParse->zAuthContext;
142488 Select *pSub1; /* Pointer to the rightmost select in sub-query */
142492 int iNewParent = -1;/* Replacement table for iParent */
142497 sqlite3 *db = pParse->db;
142504 assert( p->pPrior==0 );
142506 pSrc = p->pSrc;
142507 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
142508 pSubitem = &pSrc->a[iFrom];
142509 iParent = pSubitem->iCursor;
142510 pSub = pSubitem->pSelect;
142514 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
142517 pSubSrc = pSub->pSrc;
142521 ** because they could be computed at compile-time. But when LIMIT and OFFSET
142524 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
142525 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
142526 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
142529 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
142530 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
142531 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
142534 if( p->pOrderBy && pSub->pOrderBy ){
142537 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
142538 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
142539 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
142542 if( pSub->selFlags & (SF_Recursive) ){
142561 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
142562 if( pSubSrc->nSrc>1 /* (3a) */
142563 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
142564 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
142565 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
142572 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
142573 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
142576 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
142580 /* Restriction (17): If the sub-query is a compound SELECT, then it must
142585 if( pSub->pPrior ){
142587 if( pSub->pOrderBy ){
142590 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
142593 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
142594 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
142595 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
142596 assert( pSub->pSrc!=0 );
142597 assert( (pSub->selFlags & SF_Recursive)==0 );
142598 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
142599 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
142600 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
142601 || pSub1->pSrc->nSrc<1 /* (17c) */
142603 || pSub1->pWin /* (17e) */
142608 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
142610 ** omitted on left-hand tables of the right join that is being
142614 testcase( pSub1->pSrc->nSrc>1 );
142618 if( p->pOrderBy ){
142619 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
142620 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
142625 if( (p->selFlags & SF_Recursive) ) return 0;
142628 for(ii=0; ii<pSub->pEList->nExpr; ii++){
142630 assert( pSub->pEList->a[ii].pExpr!=0 );
142631 aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr);
142632 for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){
142633 assert( pSub1->pEList!=0 );
142634 assert( pSub1->pEList->nExpr>ii );
142635 assert( pSub1->pEList->a[ii].pExpr!=0 );
142636 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
142642 if( pSrc->nSrc>1 ){
142643 if( pParse->nSelect>500 ) return 0;
142645 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
142646 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
142652 pSub->selId, pSub, iFrom));
142655 pParse->zAuthContext = pSubitem->zName;
142658 pParse->zAuthContext = zSavedAuthContext;
142661 pSub1 = pSubitem->pSelect;
142662 sqlite3DbFree(db, pSubitem->zDatabase);
142663 sqlite3DbFree(db, pSubitem->zName);
142664 sqlite3DbFree(db, pSubitem->zAlias);
142665 pSubitem->zDatabase = 0;
142666 pSubitem->zName = 0;
142667 pSubitem->zAlias = 0;
142668 pSubitem->pSelect = 0;
142669 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
142671 /* If the sub-query is a compound SELECT statement, then (by restrictions
142675 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
142678 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
142679 ** OFFSET clauses and joins them to the left-hand-side of the original
142681 ** select statements in the compound sub-query.
142702 ** We call this the "compound-subquery flattening".
142704 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
142706 ExprList *pOrderBy = p->pOrderBy;
142707 Expr *pLimit = p->pLimit;
142708 Select *pPrior = p->pPrior;
142709 Table *pItemTab = pSubitem->pTab;
142710 pSubitem->pTab = 0;
142711 p->pOrderBy = 0;
142712 p->pPrior = 0;
142713 p->pLimit = 0;
142715 p->pLimit = pLimit;
142716 p->pOrderBy = pOrderBy;
142717 p->op = TK_ALL;
142718 pSubitem->pTab = pItemTab;
142720 p->pPrior = pPrior;
142722 pNew->selId = ++pParse->nSelect;
142723 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
142726 pNew->pPrior = pPrior;
142727 if( pPrior ) pPrior->pNext = pNew;
142728 pNew->pNext = p;
142729 p->pPrior = pNew;
142730 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
142731 " creates %u as peer\n",pNew->selId));
142733 assert( pSubitem->pSelect==0 );
142736 if( db->mallocFailed ){
142737 pSubitem->pSelect = pSub1;
142746 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
142748 if( ALWAYS(pSubitem->pTab!=0) ){
142749 Table *pTabToDel = pSubitem->pTab;
142750 if( pTabToDel->nTabRef==1 ){
142755 testcase( pToplevel->earlyCleanup );
142757 pTabToDel->nTabRef--;
142759 pSubitem->pTab = 0;
142762 /* The following loop runs once for each term in a compound-subquery
142764 ** of flattening - a flattening other than a compound-subquery flattening -
142776 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
142779 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
142781 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
142782 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
142783 pSrc = pParent->pSrc; /* FROM clause of the outer query */
142786 jointype = pSubitem->fg.jointype; /* First time through the loop */
142805 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
142807 pParent->pSrc = pSrc;
142814 SrcItem *pItem = &pSrc->a[i+iFrom];
142815 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
142816 assert( pItem->fg.isTabFunc==0 );
142817 *pItem = pSubSrc->a[i];
142818 pItem->fg.jointype |= ltorj;
142819 iNewParent = pSubSrc->a[i].iCursor;
142820 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
142822 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
142823 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
142837 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
142838 /* At this point, any non-zero iOrderByCol values indicate that the
142845 ** function attempts to flatten a compound sub-query into pParent
142846 ** (the only way this can happen is if the compound sub-query is
142847 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
142848 ExprList *pOrderBy = pSub->pOrderBy;
142849 for(i=0; i<pOrderBy->nExpr; i++){
142850 pOrderBy->a[i].u.x.iOrderByCol = 0;
142852 assert( pParent->pOrderBy==0 );
142853 pParent->pOrderBy = pOrderBy;
142854 pSub->pOrderBy = 0;
142856 pWhere = pSub->pWhere;
142857 pSub->pWhere = 0;
142862 if( pParent->pWhere ){
142863 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
142865 pParent->pWhere = pWhere;
142868 if( db->mallocFailed==0 ){
142874 x.pEList = pSub->pEList;
142881 pParent->selFlags |= pSub->selFlags & SF_Compound;
142882 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
142890 if( pSub->pLimit ){
142891 pParent->pLimit = pSub->pLimit;
142892 pSub->pLimit = 0;
142898 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
142927 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
142948 Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */
142951 assert( pColumn->op==TK_COLUMN );
142956 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
142960 /* 2018-10-25 ticket [cf5ed20f]
142962 for(i=0; i<pConst->nConst; i++){
142963 const Expr *pE2 = pConst->apExpr[i*2];
142964 assert( pE2->op==TK_COLUMN );
142965 if( pE2->iTable==pColumn->iTable
142966 && pE2->iColumn==pColumn->iColumn
142972 pConst->bHasAffBlob = 1;
142975 pConst->nConst++;
142976 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
142977 pConst->nConst*2*sizeof(Expr*));
142978 if( pConst->apExpr==0 ){
142979 pConst->nConst = 0;
142981 pConst->apExpr[pConst->nConst*2-2] = pColumn;
142982 pConst->apExpr[pConst->nConst*2-1] = pValue;
142989 ** is part of the AND-connected terms of the expression. For each term
142995 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
143000 if( pExpr->op==TK_AND ){
143001 findConstInWhere(pConst, pExpr->pRight);
143002 findConstInWhere(pConst, pExpr->pLeft);
143005 if( pExpr->op!=TK_EQ ) return;
143006 pRight = pExpr->pRight;
143007 pLeft = pExpr->pLeft;
143010 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
143013 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
143022 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
143024 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
143033 if( pConst->pOomFault[0] ) return WRC_Prune;
143034 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
143035 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
143041 for(i=0; i<pConst->nConst; i++){
143042 Expr *pColumn = pConst->apExpr[i*2];
143044 if( pColumn->iTable!=pExpr->iTable ) continue;
143045 if( pColumn->iColumn!=pExpr->iColumn ) continue;
143050 pConst->nChng++;
143053 assert( pExpr->pLeft==0 );
143054 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
143055 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
143064 ** any substitutions based on the contents of pWalker->u.pConst should
143070 ** one of the columns in pWalker->u.pConst, or
143075 ** pWalker->u.pConst.
143078 WhereConst *pConst = pWalker->u.pConst;
143083 if( pConst->bHasAffBlob ){
143084 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
143085 || pExpr->op==TK_IS
143087 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
143088 if( pConst->pOomFault[0] ) return WRC_Prune;
143089 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
143090 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
143094 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
143098 ** The WHERE-clause constant propagation optimization.
143101 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
143133 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
143156 x.pOomFault = &pParse->db->mallocFailed;
143162 if( ALWAYS(p->pSrc!=0)
143163 && p->pSrc->nSrc>0
143164 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
143173 findConstInWhere(&x, p->pWhere);
143182 sqlite3WalkExpr(&w, p->pWhere);
143183 sqlite3DbFree(x.pParse->db, x.apExpr);
143194 ** push WHERE clause expression pExpr down to FROM clause sub-query
143201 ** BY clause of all window function used by the sub-query. It is safe
143207 ** * the sub-query uses only one distinct window frame, and
143211 assert( pSubq->pWin->pPartition );
143212 assert( (pSubq->selFlags & SF_MultiPart)==0 );
143213 assert( pSubq->pPrior==0 );
143214 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
143224 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
143228 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
143236 ** (1) (** This restriction was removed on 2017-09-29. We used to
143254 ** of a LEFT JOIN where iCursor is not the right-hand table of that
143273 ** (6b) The inner query is a compound and uses window-functions.
143277 ** all window-functions used by the sub-query. It is safe to
143279 ** window over which any window-function is calculated.
143292 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
143304 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
143305 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
143308 if( pSubq->pPrior ){
143310 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
143311 u8 op = pSel->op;
143315 if( pSel->pWin ) return 0; /* restriction (6b) */
143318 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
143329 for(pX=pSubq; pX; pX=pX->pPrior){
143330 assert( (pX->selFlags & (SF_Recursive))==0 );
143335 if( pSubq->pLimit!=0 ){
143338 while( pWhere->op==TK_AND ){
143339 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
143340 pWhere = pWhere->pLeft;
143346 || pWhere->w.iJoin!=iCursor)
143351 && pWhere->w.iJoin!=iCursor
143359 pSubq->selFlags |= SF_PushDown;
143362 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
143363 unsetJoinExpr(pNew, -1, 1);
143365 x.iTable = pSrc->iCursor;
143366 x.iNewTable = pSrc->iCursor;
143368 x.pEList = pSubq->pEList;
143372 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
143373 /* Restriction 6c has prevented push-down in this case */
143374 sqlite3ExprDelete(pParse->db, pNew);
143375 nChng--;
143379 if( pSubq->selFlags & SF_Aggregate ){
143380 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
143382 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
143384 pSubq = pSubq->pPrior;
143415 assert( pFunc->op==TK_AGG_FUNCTION );
143418 pEList = pFunc->x.pList;
143420 || pEList->nExpr!=1
143427 zFunc = pFunc->u.zToken;
143430 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
143440 assert( pOrderBy!=0 || db->mallocFailed );
143441 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
143447 ** The second argument is the associated aggregate-info object. This
143452 ** where table is a database table, not a sub-select or view. If the query
143466 assert( !p->pGroupBy );
143468 if( p->pWhere
143469 || p->pEList->nExpr!=1
143470 || p->pSrc->nSrc!=1
143471 || p->pSrc->a[0].pSelect
143472 || pAggInfo->nFunc!=1
143473 || p->pHaving
143477 pTab = p->pSrc->a[0].pTab;
143481 pExpr = p->pEList->a[0].pExpr;
143483 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
143484 if( pExpr->pAggInfo!=pAggInfo ) return 0;
143485 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
143486 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
143495 ** If the source-list item passed as an argument was augmented with an
143499 ** pFrom->pIndex and return SQLITE_OK.
143502 Table *pTab = pFrom->pTab;
143503 char *zIndexedBy = pFrom->u1.zIndexedBy;
143506 assert( pFrom->fg.isIndexedBy!=0 );
143508 for(pIdx=pTab->pIndex;
143509 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
143510 pIdx=pIdx->pNext
143514 pParse->checkSchema = 1;
143517 assert( pFrom->fg.isCte==0 );
143518 pFrom->u2.pIBIndex = pIdx;
143553 if( p->pPrior==0 ) return WRC_Continue;
143554 if( p->pOrderBy==0 ) return WRC_Continue;
143555 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
143557 a = p->pOrderBy->a;
143559 /* If iOrderByCol is already non-zero, then it has already been matched
143561 ** SELECT is rewritten for window-functions processing and then passed
143566 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
143567 if( a[i].pExpr->flags & EP_Collate ) break;
143573 pParse = pWalker->pParse;
143574 db = pParse->db;
143581 p->pSrc = pNewSrc;
143582 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
143583 p->op = TK_SELECT;
143584 p->pWhere = 0;
143585 pNew->pGroupBy = 0;
143586 pNew->pHaving = 0;
143587 pNew->pOrderBy = 0;
143588 p->pPrior = 0;
143589 p->pNext = 0;
143590 p->pWith = 0;
143592 p->pWinDefn = 0;
143594 p->selFlags &= ~SF_Compound;
143595 assert( (p->selFlags & SF_Converted)==0 );
143596 p->selFlags |= SF_Converted;
143597 assert( pNew->pPrior!=0 );
143598 pNew->pPrior->pNext = pNew;
143599 pNew->pLimit = 0;
143604 ** Check to see if the FROM clause term pFrom has table-valued function
143606 ** non-zero, since pFrom is not allowed to be a table-valued function.
143609 if( pFrom->fg.isTabFunc ){
143610 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
143620 ** FROM clause element pItem is really a common-table-expression (CTE)
143624 ** If a non-NULL value is returned, set *ppContext to point to the With
143632 const char *zName = pItem->zName;
143634 assert( pItem->zDatabase==0 );
143636 for(p=pWith; p; p=p->pOuter){
143638 for(i=0; i<p->nCte; i++){
143639 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
143641 return &p->a[i];
143644 if( p->bView ) break;
143650 ** with the inner-most WITH clause being at the top of the stack.
143674 if( pParse->nErr==0 ){
143675 assert( pParse->pWith!=pWith );
143676 pWith->pOuter = pParse->pWith;
143677 pParse->pWith = pWith;
143686 ** pParse->pWith linked list). And if currently processing a CTE
143690 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
143705 assert( pFrom->pTab==0 );
143706 if( pParse->pWith==0 ){
143710 if( pParse->nErr ){
143711 /* Prior errors might have left pParse->pWith in a goofy state, so
143715 if( pFrom->zDatabase!=0 ){
143720 if( pFrom->fg.notCte ){
143728 pCte = searchWith(pParse->pWith, pFrom, &pWith);
143730 sqlite3 *db = pParse->db;
143734 Select *pLeft; /* Left-most SELECT statement */
143735 Select *pRecTerm; /* Left-most recursive term */
143737 With *pSavedWith; /* Initial value of pParse->pWith */
143738 int iRecTab = -1; /* Cursor for recursive table */
143741 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
143743 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
143745 if( pCte->zCteErr ){
143746 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
143751 assert( pFrom->pTab==0 );
143754 pCteUse = pCte->pUse;
143756 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
143763 pCteUse->eM10d = pCte->eM10d;
143765 pFrom->pTab = pTab;
143766 pTab->nTabRef = 1;
143767 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
143768 pTab->iPKey = -1;
143769 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
143770 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
143771 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
143772 if( db->mallocFailed ) return 2;
143773 pFrom->pSelect->selFlags |= SF_CopyCte;
143774 assert( pFrom->pSelect );
143775 if( pFrom->fg.isIndexedBy ){
143776 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
143779 pFrom->fg.isCte = 1;
143780 pFrom->u2.pCteUse = pCteUse;
143781 pCteUse->nUse++;
143782 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
143783 pCteUse->eM10d = M10d_Yes;
143787 pRecTerm = pSel = pFrom->pSelect;
143788 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
143789 while( bMayRecursive && pRecTerm->op==pSel->op ){
143791 SrcList *pSrc = pRecTerm->pSrc;
143792 assert( pRecTerm->pPrior!=0 );
143793 for(i=0; i<pSrc->nSrc; i++){
143794 SrcItem *pItem = &pSrc->a[i];
143795 if( pItem->zDatabase==0
143796 && pItem->zName!=0
143797 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
143799 pItem->pTab = pTab;
143800 pTab->nTabRef++;
143801 pItem->fg.isRecursive = 1;
143802 if( pRecTerm->selFlags & SF_Recursive ){
143804 "multiple references to recursive table: %s", pCte->zName
143808 pRecTerm->selFlags |= SF_Recursive;
143809 if( iRecTab<0 ) iRecTab = pParse->nTab++;
143810 pItem->iCursor = iRecTab;
143813 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
143814 pRecTerm = pRecTerm->pPrior;
143817 pCte->zCteErr = "circular reference: %s";
143818 pSavedWith = pParse->pWith;
143819 pParse->pWith = pWith;
143820 if( pSel->selFlags & SF_Recursive ){
143823 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
143824 assert( pRecTerm->pNext!=0 );
143825 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
143826 assert( pRecTerm->pWith==0 );
143827 pRecTerm->pWith = pSel->pWith;
143829 pRecTerm->pWith = 0;
143831 pParse->pWith = pSavedWith;
143836 pParse->pWith = pSavedWith;
143840 pParse->pWith = pWith;
143842 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
143843 pEList = pLeft->pEList;
143844 if( pCte->pCols ){
143845 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
143847 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
143849 pParse->pWith = pSavedWith;
143852 pEList = pCte->pCols;
143855 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
143857 if( pSel->selFlags & SF_Recursive ){
143858 pCte->zCteErr = "multiple recursive references: %s";
143860 pCte->zCteErr = "recursive reference in a subquery: %s";
143864 pCte->zCteErr = 0;
143865 pParse->pWith = pSavedWith;
143882 Parse *pParse = pWalker->pParse;
143883 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
143884 With *pWith = findRightmost(p)->pWith;
143886 assert( pParse->pWith==pWith || pParse->nErr );
143887 pParse->pWith = pWith->pOuter;
143895 ** sub-query in the FROM clause of a SELECT statement. This function
143901 Select *pSel = pFrom->pSelect;
143905 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
143907 pTab->nTabRef = 1;
143908 if( pFrom->zAlias ){
143909 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
143911 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
143913 while( pSel->pPrior ){ pSel = pSel->pPrior; }
143914 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
143915 pTab->iPKey = -1;
143916 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
143918 /* The usual case - do not allow ROWID on a subquery */
143919 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
143921 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
143923 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
143942 N--;
143944 if( pBase->fg.isUsing==0 ) continue;
143945 if( NEVER(pBase->u3.pUsing==0) ) continue;
143946 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
143959 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
143961 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
143977 Parse *pParse = pWalker->pParse;
143982 sqlite3 *db = pParse->db;
143984 u16 selFlags = p->selFlags;
143987 p->selFlags |= SF_Expanded;
143988 if( db->mallocFailed ){
143991 assert( p->pSrc!=0 );
143995 if( pWalker->eCode ){
143997 p->selId = ++pParse->nSelect;
143999 pTabList = p->pSrc;
144000 pEList = p->pEList;
144001 if( pParse->pWith && (p->selFlags & SF_View) ){
144002 if( p->pWith==0 ){
144003 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
144004 if( p->pWith==0 ){
144008 p->pWith->bView = 1;
144010 sqlite3WithPush(pParse, p->pWith, 0);
144021 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144023 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
144024 if( pFrom->pTab ) continue;
144025 assert( pFrom->fg.isRecursive==0 );
144026 if( pFrom->zName==0 ){
144028 Select *pSel = pFrom->pSelect;
144029 /* A sub-query in the FROM clause of a SELECT */
144031 assert( pFrom->pTab==0 );
144038 pTab = pFrom->pTab;
144043 assert( pFrom->pTab==0 );
144044 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
144046 if( pTab->nTabRef>=0xffff ){
144048 pTab->zName);
144049 pFrom->pTab = 0;
144052 pTab->nTabRef++;
144059 u8 eCodeOrig = pWalker->eCode;
144061 assert( pFrom->pSelect==0 );
144063 if( (db->flags & SQLITE_EnableView)==0
144064 && pTab->pSchema!=db->aDb[1].pSchema
144067 pTab->zName);
144069 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
144073 && pFrom->fg.fromDDL
144074 && ALWAYS(pTab->u.vtab.p!=0)
144075 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
144078 pTab->zName);
144082 nCol = pTab->nCol;
144083 pTab->nCol = -1;
144084 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
144085 sqlite3WalkSelect(pWalker, pFrom->pSelect);
144086 pWalker->eCode = eCodeOrig;
144087 pTab->nCol = nCol;
144093 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
144100 assert( db->mallocFailed==0 || pParse->nErr!=0 );
144101 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
144116 for(k=0; k<pEList->nExpr; k++){
144117 pE = pEList->a[k].pExpr;
144118 if( pE->op==TK_ASTERISK ) break;
144119 assert( pE->op!=TK_DOT || pE->pRight!=0 );
144120 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
144121 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
144122 elistFlags |= pE->flags;
144124 if( k<pEList->nExpr ){
144130 struct ExprList_item *a = pEList->a;
144132 int flags = pParse->db->flags;
144136 for(k=0; k<pEList->nExpr; k++){
144138 elistFlags |= pE->flags;
144139 pRight = pE->pRight;
144140 assert( pE->op!=TK_DOT || pRight!=0 );
144141 if( pE->op!=TK_ASTERISK
144142 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
144148 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
144149 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
144158 if( pE->op==TK_DOT ){
144159 assert( pE->pLeft!=0 );
144160 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
144161 zTName = pE->pLeft->u.zToken;
144163 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144164 Table *pTab = pFrom->pTab; /* Table for this data source */
144165 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
144171 if( (zTabName = pFrom->zAlias)==0 ){
144172 zTabName = pTab->zName;
144174 if( db->mallocFailed ) break;
144175 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
144176 if( pFrom->fg.isNestedFrom ){
144177 assert( pFrom->pSelect!=0 );
144178 pNestedFrom = pFrom->pSelect->pEList;
144180 assert( pNestedFrom->nExpr==pTab->nCol );
144186 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
144187 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
144189 if( i+1<pTabList->nSrc
144195 for(ii=0; ii<pUsing->nId; ii++){
144196 const char *zUName = pUsing->a[ii].zName;
144200 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
144201 assert( pX->zEName==0 );
144202 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
144203 pX->fg.eEName = ENAME_TAB;
144204 pX->fg.bUsingTerm = 1;
144210 for(j=0; j<pTab->nCol; j++){
144211 char *zName = pTab->aCol[j].zCnName;
144217 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
144223 ** result-set list unless the SELECT has the SF_IncludeHidden
144226 if( (p->selFlags & SF_IncludeHidden)==0
144227 && IsHiddenColumn(&pTab->aCol[j])
144231 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
144240 if( pFrom->fg.isUsing
144241 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
144249 if( (pTabList->nSrc>1
144250 && ( (pFrom->fg.jointype & JT_LTORJ)==0
144252 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
144260 if( IN_RENAME_OBJECT && pE->pLeft ){
144261 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
144274 pX = &pNew->a[pNew->nExpr-1];
144275 assert( pX->zEName==0 );
144278 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
144279 testcase( pX->zEName==0 );
144281 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
144283 testcase( pX->zEName==0 );
144285 pX->fg.eEName = ENAME_TAB;
144286 if( (pFrom->fg.isUsing
144287 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
144289 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
144291 pX->fg.bNoExpand = 1;
144294 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
144295 pX->fg.eEName = ENAME_NAME;
144297 pX->zEName = sqlite3DbStrDup(db, zName);
144298 pX->fg.eEName = ENAME_NAME;
144312 p->pEList = pNew;
144314 if( p->pEList ){
144315 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
144320 p->selFlags |= SF_ComplexResult;
144325 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
144352 ** The calling function can detect the problem by looking at pParse->nErr
144353 ** and/or pParse->db->mallocFailed.
144359 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
144376 ** For each FROM-clause subquery, add Column.zType and Column.zColl
144391 assert( p->selFlags & SF_Resolved );
144392 if( p->selFlags & SF_HasTypeInfo ) return;
144393 p->selFlags |= SF_HasTypeInfo;
144394 pParse = pWalker->pParse;
144395 pTabList = p->pSrc;
144396 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
144397 Table *pTab = pFrom->pTab;
144399 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
144400 /* A sub-query in the FROM clause of a SELECT */
144401 Select *pSel = pFrom->pSelect;
144403 while( pSel->pPrior ) pSel = pSel->pPrior;
144415 ** the Table structures of all FROM-clause subqueries in a
144436 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
144437 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
144449 assert( p!=0 || pParse->db->mallocFailed );
144450 assert( pParse->db->pParse==pParse );
144451 if( pParse->db->mallocFailed ) return;
144452 if( p->selFlags & SF_HasTypeInfo ) return;
144454 if( pParse->nErr ) return;
144456 if( pParse->nErr ) return;
144469 Vdbe *v = pParse->pVdbe;
144472 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
144473 assert( pParse->db->pParse==pParse );
144474 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
144476 if( pParse->nErr ) return;
144480 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
144481 for(i=0; i<pAggInfo->nColumn; i++){
144482 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
144483 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
144485 for(i=0; i<pAggInfo->nFunc; i++){
144486 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
144487 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
144490 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
144491 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
144492 if( pFunc->iDistinct>=0 ){
144493 Expr *pE = pFunc->pFExpr;
144495 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
144498 pFunc->iDistinct = -1;
144500 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
144501 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
144502 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
144503 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
144504 pFunc->pFunc->zName));
144515 Vdbe *v = pParse->pVdbe;
144518 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
144520 assert( ExprUseXList(pF->pFExpr) );
144521 pList = pF->pFExpr->x.pList;
144522 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
144523 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
144532 ** If regAcc is non-zero and there are no min() or max() aggregates
144533 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
144543 Vdbe *v = pParse->pVdbe;
144550 pAggInfo->directMode = 1;
144551 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
144556 assert( ExprUseXList(pF->pFExpr) );
144557 assert( !IsWindowFunc(pF->pFExpr) );
144558 pList = pF->pFExpr->x.pList;
144559 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
144560 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
144561 if( pAggInfo->nAccumulator
144562 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
144568 if( regHit==0 ) regHit = ++pParse->nMem;
144582 nArg = pList->nExpr;
144589 if( pF->iDistinct>=0 && pList ){
144593 pF->iDistinct = codeDistinct(pParse, eDistinctType,
144594 pF->iDistinct, addrNext, pList, regAgg);
144596 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
144600 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
144601 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
144602 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
144605 pColl = pParse->db->pDfltColl;
144607 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
144610 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
144611 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
144618 if( regHit==0 && pAggInfo->nAccumulator ){
144624 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
144625 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
144628 pAggInfo->directMode = 0;
144644 if( pParse->explain==2 ){
144647 pTab->zName,
144649 bCover ? pIdx->zName : ""
144664 ** sub-expression matches the criteria for being moved to the WHERE
144665 ** clause. If so, add it to the WHERE clause and replace the sub-expression
144669 if( pExpr->op!=TK_AND ){
144670 Select *pS = pWalker->u.pSelect;
144672 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
144678 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
144680 && pExpr->pAggInfo==0
144682 sqlite3 *db = pWalker->pParse->db;
144685 Expr *pWhere = pS->pWhere;
144687 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
144688 pS->pWhere = pNew;
144689 pWalker->eCode = 1;
144718 sqlite3WalkExpr(&sWalker, p->pHaving);
144728 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
144733 SrcList *pTabList, /* Search for self-joins in this FROM clause */
144737 assert( pThis->pSelect!=0 );
144738 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
144739 for(pItem = pTabList->a; pItem<pThis; pItem++){
144741 if( pItem->pSelect==0 ) continue;
144742 if( pItem->fg.viaCoroutine ) continue;
144743 if( pItem->zName==0 ) continue;
144744 assert( pItem->pTab!=0 );
144745 assert( pThis->pTab!=0 );
144746 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
144747 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
144748 pS1 = pItem->pSelect;
144749 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
144754 if( pItem->pSelect->selFlags & SF_PushDown ){
144768 sqlite3DbFree(db, p->aCol);
144769 sqlite3DbFree(db, p->aFunc);
144798 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
144799 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
144800 if( p->pWhere ) return 0;
144801 if( p->pGroupBy ) return 0;
144802 pExpr = p->pEList->a[0].pExpr;
144803 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
144805 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
144807 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
144808 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
144809 pSub = p->pSrc->a[0].pSelect;
144811 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
144813 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
144814 if( pSub->pWhere ) return 0; /* No WHERE clause */
144815 if( pSub->pLimit ) return 0; /* No LIMIT clause */
144816 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
144817 pSub = pSub->pPrior; /* Repeat over compound */
144822 db = pParse->db;
144825 pSub = p->pSrc->a[0].pSelect;
144826 p->pSrc->a[0].pSelect = 0;
144827 sqlite3SrcListDelete(db, p->pSrc);
144828 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
144831 pPrior = pSub->pPrior;
144832 pSub->pPrior = 0;
144833 pSub->pNext = 0;
144834 pSub->selFlags |= SF_Aggregate;
144835 pSub->selFlags &= ~SF_Compound;
144836 pSub->nSelectRow = 0;
144837 sqlite3ExprListDelete(db, pSub->pEList);
144839 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
144849 p->pEList->a[0].pExpr = pExpr;
144850 p->selFlags &= ~SF_Aggregate;
144854 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
144863 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
144869 for(i=0; i<pSrc->nSrc; i++){
144870 SrcItem *p1 = &pSrc->a[i];
144872 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
144875 if( p1->pSelect
144876 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
144877 && sameSrcAlias(p0, p1->pSelect->pSrc)
144893 ** pParse->zErrMsg.
144921 db = pParse->db;
144922 assert( pParse==db->pParse );
144924 if( p==0 || pParse->nErr ){
144927 assert( db->mallocFailed==0 );
144930 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
144940 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
144941 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
144942 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
144943 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
144945 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
144946 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
144947 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
144949 if( p->pOrderBy ){
144953 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
144958 p->pOrderBy);
144959 testcase( pParse->earlyCleanup );
144960 p->pOrderBy = 0;
144962 p->selFlags &= ~SF_Distinct;
144963 p->selFlags |= SF_NoopOrderBy;
144966 if( pParse->nErr ){
144969 assert( db->mallocFailed==0 );
144970 assert( p->pEList!=0 );
144980 ** In this case, it is an error if the target object (pSrc->a[0]) name
144981 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
144987 if( p->selFlags & SF_UFSrcCheck ){
144988 SrcItem *p0 = &p->pSrc->a[0];
144989 if( sameSrcAlias(p0, p->pSrc) ){
144992 p0->zAlias ? p0->zAlias : p0->pTab->zName
144998 ** and leaving this flag set can cause errors if a compound sub-query
144999 ** in p->pSrc is flattened into this query and this function called
145001 p->selFlags &= ~SF_UFSrcCheck;
145004 if( pDest->eDest==SRT_Output ){
145010 assert( pParse->nErr );
145014 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
145020 pTabList = p->pSrc;
145021 isAgg = (p->selFlags & SF_Aggregate)!=0;
145023 sSort.pOrderBy = p->pOrderBy;
145029 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
145030 SrcItem *pItem = &pTabList->a[i];
145031 Select *pSub = pItem->pSelect;
145032 Table *pTab = pItem->pTab;
145042 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
145043 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
145047 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
145048 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
145049 assert( pItem->iCursor>=0 );
145050 unsetJoinExpr(p->pWhere, pItem->iCursor,
145051 pTabList->a[0].fg.jointype & JT_LTORJ);
145059 if( pTab->nCol!=pSub->pEList->nExpr ){
145061 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
145069 ** will be implemented as a co-routine and there is no advantage to
145072 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
145073 assert( pSub->pGroupBy==0 );
145075 /* If a FROM-clause subquery has an ORDER BY clause that is not
145084 ** (2) The subquery was added to help with window-function
145088 ** the built-in count(), min(), or max().
145095 if( pSub->pOrderBy!=0
145096 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
145097 && pSub->pLimit==0 /* Condition (1) */
145098 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
145099 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
145103 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
145106 pSub->pOrderBy);
145107 pSub->pOrderBy = 0;
145113 ** it will be implemented as a co-routine, then do not flatten. This
145128 if( pSub->pOrderBy!=0
145130 && (p->selFlags & SF_ComplexResult)!=0
145131 && (pTabList->nSrc==1
145132 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
145138 if( pParse->nErr ) goto select_end;
145140 i = -1;
145142 pTabList = p->pSrc;
145143 if( db->mallocFailed ) goto select_end;
145145 sSort.pOrderBy = p->pOrderBy;
145154 if( p->pPrior ){
145157 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
145162 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
145167 /* Do the WHERE-clause constant propagation optimization if this is
145168 ** a join. No need to speed time on this operation for non-join queries
145172 if( p->pWhere!=0
145173 && p->pWhere->op==TK_AND
145191 if( db->mallocFailed ) goto select_end;
145192 pEList = p->pEList;
145193 pTabList = p->pSrc;
145199 ** (2) Generate code for all sub-queries
145201 for(i=0; i<pTabList->nSrc; i++){
145202 SrcItem *pItem = &pTabList->a[i];
145215 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
145216 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
145223 ** assume the column name is non-NULL and segfault. The use of an empty
145226 if( pItem->colUsed==0 && pItem->zName!=0 ){
145227 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
145231 /* Generate code for all sub-queries in the FROM clause
145233 pSub = pItem->pSelect;
145237 assert( pItem->addrFillSub==0 );
145242 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
145246 pParse->nHeight += sqlite3SelectExprHeight(p);
145248 /* Make copies of constant WHERE-clause terms in the outer query down
145252 && (pItem->fg.isCte==0
145253 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
145254 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
145259 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
145263 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
145265 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
145268 zSavedAuthContext = pParse->zAuthContext;
145269 pParse->zAuthContext = pItem->zName;
145273 ** The subquery is implemented as a co-routine if all of the following are
145282 && (pTabList->nSrc==1
145283 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) /* (1) */
145284 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
145285 && (pTabList->a[0].fg.jointype & JT_LTORJ)==0 /* (3) */
145287 /* Implement a co-routine that will return a single row of the result
145292 pItem->regReturn = ++pParse->nMem;
145293 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
145295 pItem->addrFillSub = addrTop;
145296 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
145297 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
145299 pItem->pTab->nRowLogEst = pSub->nSelectRow;
145300 pItem->fg.viaCoroutine = 1;
145301 pItem->regResult = dest.iSdst;
145302 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
145303 sqlite3VdbeJumpHere(v, addrTop-1);
145305 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
145308 ** the make the pItem->iCursor be a copy of the ephemerial table that
145310 CteUse *pCteUse = pItem->u2.pCteUse;
145311 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
145312 if( pItem->iCursor!=pCteUse->iCur ){
145313 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
145316 pSub->nSelectRow = pCteUse->nRowEst;
145320 if( pPrior->addrFillSub ){
145321 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
145323 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
145324 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
145332 pItem->regReturn = ++pParse->nMem;
145334 pItem->addrFillSub = topAddr+1;
145335 pItem->fg.isMaterialized = 1;
145336 if( pItem->fg.isCorrelated==0 ){
145345 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
145347 dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab);
145351 pItem->pTab->nRowLogEst = pSub->nSelectRow;
145353 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
145357 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
145358 CteUse *pCteUse = pItem->u2.pCteUse;
145359 pCteUse->addrM9e = pItem->addrFillSub;
145360 pCteUse->regRtn = pItem->regReturn;
145361 pCteUse->iCur = pItem->iCursor;
145362 pCteUse->nRowEst = pSub->nSelectRow;
145365 if( db->mallocFailed ) goto select_end;
145366 pParse->nHeight -= sqlite3SelectExprHeight(p);
145367 pParse->zAuthContext = zSavedAuthContext;
145373 pEList = p->pEList;
145374 pWhere = p->pWhere;
145375 pGroupBy = p->pGroupBy;
145376 pHaving = p->pHaving;
145377 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
145381 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
145387 ** if the select-list is the same as the ORDER BY list, then this query
145396 ** The second form is preferred as a single index (or temp-table) may be
145398 ** written the query must use a temp-table for at least one of the ORDER
145399 ** BY and DISTINCT, and an index or separate temp-table for the other.
145401 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
145402 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
145404 && p->pWin==0
145407 p->selFlags &= ~SF_Distinct;
145408 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
145409 p->selFlags |= SF_Aggregate;
145410 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
145426 ** being unused if the data can be extracted in pre-sorted order.
145435 pParse, sSort.pOrderBy, 0, pEList->nExpr);
145436 sSort.iECursor = pParse->nTab++;
145439 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
145443 sSort.addrSortIndex = -1;
145448 if( pDest->eDest==SRT_EphemTab ){
145449 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
145450 if( p->selFlags & SF_NestedFrom ){
145451 /* Delete or NULL-out result columns that will never be used */
145453 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
145454 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
145455 sqlite3DbFree(db, pEList->a[ii].zEName);
145456 pEList->nExpr--;
145458 for(ii=0; ii<pEList->nExpr; ii++){
145459 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
145467 if( (p->selFlags & SF_FixedLimit)==0 ){
145468 p->nSelectRow = 320; /* 4 billion rows */
145470 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
145471 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
145478 if( p->selFlags & SF_Distinct ){
145479 sDistinct.tabTnct = pParse->nTab++;
145482 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
145493 | (p->selFlags & SF_FixedLimit);
145495 Window *pWin = p->pWin; /* Main window object (or NULL) */
145506 p->pEList, p, wctrlFlags, p->nSelectRow);
145508 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
145509 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
145517 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
145531 assert( p->pEList==pEList );
145537 int regGosub = ++pParse->nMem;
145543 VdbeNoopComment((v, "inner-loop subroutine"));
145545 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
145548 VdbeComment((v, "end inner-loop subroutine"));
145554 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
145586 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
145587 pItem->u.x.iAlias = 0;
145589 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
145590 pItem->u.x.iAlias = 0;
145593 if( p->nSelectRow>66 ) p->nSelectRow = 66;
145598 ** in the correct order. It also may not - the GROUP BY might use a
145603 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
145606 ** ASC or DESC order - only that each group is returned contiguously.
145610 for(ii=0; ii<pGroupBy->nExpr; ii++){
145612 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
145613 pGroupBy->a[ii].fg.sortFlags = sortFlags;
145615 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
145621 p->nSelectRow = 0;
145635 testcase( pParse->earlyCleanup );
145637 if( db->mallocFailed ){
145640 pAggInfo->selId = p->selId;
145646 pAggInfo->mnReg = pParse->nMem+1;
145647 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
145648 pAggInfo->pGroupBy = pGroupBy;
145653 assert( pWhere==p->pWhere );
145654 assert( pHaving==p->pHaving );
145655 assert( pGroupBy==p->pGroupBy );
145657 pWhere = p->pWhere;
145661 pAggInfo->nAccumulator = pAggInfo->nColumn;
145662 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
145663 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
145667 for(i=0; i<pAggInfo->nFunc; i++){
145668 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
145671 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
145675 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
145680 pAggInfo->mxReg = pParse->nMem;
145681 if( db->mallocFailed ) goto select_end;
145691 for(ii=0; ii<pAggInfo->nColumn; ii++){
145692 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
145694 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
145696 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
145697 pCol->iTable, pCol->iColumn, pCol->iMem,
145698 pCol->iSorterColumn);
145699 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
145701 for(ii=0; ii<pAggInfo->nFunc; ii++){
145702 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
145703 ii, pAggInfo->aFunc[ii].iMem);
145704 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
145715 int addr1; /* A-vs-B comparision jump */
145727 if( pAggInfo->nFunc==1
145728 && pAggInfo->aFunc[0].iDistinct>=0
145729 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
145730 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
145731 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
145733 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
145745 pAggInfo->sortingIdx = pParse->nTab++;
145747 0, pAggInfo->nColumn);
145749 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
145754 iUseFlag = ++pParse->nMem;
145755 iAbortFlag = ++pParse->nMem;
145756 regOutputRow = ++pParse->nMem;
145758 regReset = ++pParse->nMem;
145760 iAMem = pParse->nMem + 1;
145761 pParse->nMem += pGroupBy->nExpr;
145762 iBMem = pParse->nMem + 1;
145763 pParse->nMem += pGroupBy->nExpr;
145766 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
145785 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
145803 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
145807 nGroupBy = pGroupBy->nExpr;
145810 for(i=0; i<pAggInfo->nColumn; i++){
145811 if( pAggInfo->aCol[i].iSorterColumn>=j ){
145819 pAggInfo->directMode = 1;
145820 for(i=0; i<pAggInfo->nColumn; i++){
145821 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
145822 if( pCol->iSorterColumn>=j ){
145823 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
145827 pAggInfo->directMode = 0;
145830 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
145835 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
145838 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
145840 pAggInfo->useSortingIdx = 1;
145847 ** This is an optimization - the correct answer should result regardless.
145864 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
145867 for(j=0; j<pGroupBy->nExpr; j++){
145871 pAggInfo->directMode = 1;
145872 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
145875 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
145889 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
145908 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
145928 ** is less than or equal to zero, the subroutine is a no-op. If
145945 selectInnerLoop(pParse, p, -1, &sSort,
145951 /* Generate a subroutine that will reset the group-by accumulator
145960 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
145961 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
145980 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
145981 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
145985 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
145988 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
145992 ** (2011-04-15) Do not do a full scan of an unordered index.
145994 ** (2013-10-03) Do not count the entries in a partial index.
146000 if( !p->pSrc->a[0].fg.notIndexed ){
146001 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146002 if( pIdx->bUnordered==0
146003 && pIdx->szIdxRow<pTab->szTabRow
146004 && pIdx->pPartIdxWhere==0
146005 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
146012 iRoot = pBest->tnum;
146016 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
146019 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
146021 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
146039 if( pAggInfo->nAccumulator ){
146040 for(i=0; i<pAggInfo->nFunc; i++){
146041 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
146044 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
146048 if( i==pAggInfo->nFunc ){
146049 regAcc = ++pParse->nMem;
146052 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
146053 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
146054 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
146062 assert( p->pGroupBy==0 );
146071 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
146083 struct AggInfo_func *pF = pAggInfo->aFunc;
146085 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
146100 selectInnerLoop(pParse, p, -1, 0, 0,
146117 assert( p->pEList==pEList );
146118 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
146127 rc = (pParse->nErr>0);
146133 assert( db->mallocFailed==0 || db->mallocFailed==1 );
146134 assert( db->mallocFailed==0 || pParse->nErr!=0 );
146137 if( pAggInfo && !db->mallocFailed ){
146138 for(i=0; i<pAggInfo->nColumn; i++){
146139 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
146141 assert( pExpr->pAggInfo==pAggInfo );
146142 assert( pExpr->iAgg==i );
146144 for(i=0; i<pAggInfo->nFunc; i++){
146145 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
146147 assert( pExpr->pAggInfo==pAggInfo );
146148 assert( pExpr->iAgg==i );
146208 int need; /* Slots needed in p->azResult[] */
146212 /* Make sure there is enough space in p->azResult to hold everything
146215 if( p->nRow==0 && argv!=0 ){
146220 if( p->nData + need > p->nAlloc ){
146222 p->nAlloc = p->nAlloc*2 + need;
146223 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
146225 p->azResult = azNew;
146231 if( p->nRow==0 ){
146232 p->nColumn = nCol;
146236 p->azResult[p->nData++] = z;
146238 }else if( (int)p->nColumn!=nCol ){
146239 sqlite3_free(p->zErrMsg);
146240 p->zErrMsg = sqlite3_mprintf(
146243 p->rc = SQLITE_ERROR;
146259 p->azResult[p->nData++] = z;
146261 p->nRow++;
146266 p->rc = SQLITE_NOMEM_BKPT;
146306 db->errCode = SQLITE_NOMEM;
146322 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
146335 db->errCode = SQLITE_NOMEM;
146354 azResult--;
146387 pTriggerStep = pTriggerStep->pNext;
146389 sqlite3ExprDelete(db, pTmp->pWhere);
146390 sqlite3ExprListDelete(db, pTmp->pExprList);
146391 sqlite3SelectDelete(db, pTmp->pSelect);
146392 sqlite3IdListDelete(db, pTmp->pIdList);
146393 sqlite3UpsertDelete(db, pTmp->pUpsert);
146394 sqlite3SrcListDelete(db, pTmp->pFrom);
146395 sqlite3DbFree(db, pTmp->zSpan);
146406 ** are already attached to pTab->pTrigger. But there might be additional
146408 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
146413 ** pTab as well as the triggers lised in pTab->pTrigger.
146420 assert( pParse->disableTriggers==0 );
146421 pTmpSchema = pParse->db->aDb[1].pSchema;
146422 p = sqliteHashFirst(&pTmpSchema->trigHash);
146423 pList = pTab->pTrigger;
146426 if( pTrig->pTabSchema==pTab->pSchema
146427 && pTrig->table
146428 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
146429 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
146431 pTrig->pNext = pList;
146433 }else if( pTrig->op==TK_RETURNING ){
146435 assert( pParse->db->pVtabCtx==0 );
146437 assert( pParse->bReturning );
146438 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
146439 pTrig->table = pTab->zName;
146440 pTrig->pTabSchema = pTab->pSchema;
146441 pTrig->pNext = pList;
146449 printf("Triggers for %s:", pTab->zName);
146450 for(pX=pList; pX; pX=pX->pNext){
146451 printf(" %s", pX->zName);
146464 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
146483 sqlite3 *db = pParse->db; /* The database connection */
146488 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
146494 if( pName2->n>0 ){
146507 if( !pTableName || db->mallocFailed ){
146511 /* A long-standing parser bug is that this syntax was allowed:
146519 if( db->init.busy && iDb!=1 ){
146520 sqlite3DbFree(db, pTableName->a[0].zDatabase);
146521 pTableName->a[0].zDatabase = 0;
146530 if( db->init.busy==0 && pName2->n==0 && pTab
146531 && pTab->pSchema==db->aDb[1].pSchema ){
146536 if( db->mallocFailed ) goto trigger_cleanup;
146537 assert( pTableName->nSrc==1 );
146556 assert( db->mallocFailed );
146559 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
146564 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
146568 assert( !db->init.busy );
146576 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
146586 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
146591 " trigger on table: %S", pTableName->a);
146597 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
146599 const char *zDb = db->aDb[iTabDb].zDbSName;
146600 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
146602 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
146623 pTrigger->zName = zName;
146625 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
146626 pTrigger->pSchema = db->aDb[iDb].pSchema;
146627 pTrigger->pTabSchema = pTab->pSchema;
146628 pTrigger->op = (u8)op;
146629 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
146631 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
146632 pTrigger->pWhen = pWhen;
146635 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
146637 pTrigger->pColumns = pColumns;
146639 assert( pParse->pNewTrigger==0 );
146640 pParse->pNewTrigger = pTrigger;
146647 if( !pParse->pNewTrigger ){
146650 assert( pParse->pNewTrigger==pTrigger );
146655 if( db->init.iDb==1 ){
146658 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
146662 ** "orphaned trigger" - a trigger whose associated table is missing.
146664 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
146666 db->init.orphanTrigger = 1;
146680 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
146682 sqlite3 *db = pParse->db; /* The database */
146687 pParse->pNewTrigger = 0;
146688 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
146689 zName = pTrig->zName;
146690 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
146691 pTrig->step_list = pStepList;
146693 pStepList->pTrig = pTrig;
146694 pStepList = pStepList->pNext;
146696 sqlite3TokenInit(&nameToken, pTrig->zName);
146698 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
146699 || sqlite3FixExpr(&sFix, pTrig->pWhen)
146706 assert( !db->init.busy );
146707 pParse->pNewTrigger = pTrig;
146715 if( !db->init.busy ){
146720 ** are read-only, and the trigger makes a change to a shadow table,
146721 ** then raise an error - do not allow the trigger to be created. */
146724 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
146725 if( pStep->zTarget!=0
146726 && sqlite3ShadowTableName(db, pStep->zTarget)
146730 pTrig->zName, pStep->zTarget);
146740 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
146745 db->aDb[iDb].zDbSName, zName,
146746 pTrig->table, z);
146753 if( db->init.busy ){
146755 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
146761 }else if( pLink->pSchema==pLink->pTabSchema ){
146763 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
146765 pLink->pNext = pTab->pTrigger;
146766 pTab->pTrigger = pLink;
146772 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
146805 pTriggerStep->op = TK_SELECT;
146806 pTriggerStep->pSelect = pSelect;
146807 pTriggerStep->orconf = OE_Default;
146808 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
146816 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
146825 sqlite3 *db = pParse->db;
146828 if( pParse->nErr ) return 0;
146829 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
146832 memcpy(z, pName->z, pName->n);
146834 pTriggerStep->zTarget = z;
146835 pTriggerStep->op = op;
146836 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
146838 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
146861 sqlite3 *db = pParse->db;
146864 assert(pSelect != 0 || db->mallocFailed);
146869 pTriggerStep->pSelect = pSelect;
146872 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
146874 pTriggerStep->pIdList = pColumn;
146875 pTriggerStep->pUpsert = pUpsert;
146876 pTriggerStep->orconf = orconf;
146878 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
146899 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
146906 sqlite3 *db = pParse->db;
146912 pTriggerStep->pExprList = pEList;
146913 pTriggerStep->pWhere = pWhere;
146914 pTriggerStep->pFrom = pFrom;
146919 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
146920 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
146921 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
146923 pTriggerStep->orconf = orconf;
146943 sqlite3 *db = pParse->db;
146949 pTriggerStep->pWhere = pWhere;
146952 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
146954 pTriggerStep->orconf = OE_Default;
146964 if( pTrigger==0 || pTrigger->bReturning ) return;
146965 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
146966 sqlite3DbFree(db, pTrigger->zName);
146967 sqlite3DbFree(db, pTrigger->table);
146968 sqlite3ExprDelete(db, pTrigger->pWhen);
146969 sqlite3IdListDelete(db, pTrigger->pColumns);
146986 sqlite3 *db = pParse->db;
146988 if( db->mallocFailed ) goto drop_trigger_cleanup;
146993 assert( pName->nSrc==1 );
146994 zDb = pName->a[0].zDatabase;
146995 zName = pName->a[0].zName;
146997 for(i=OMIT_TEMPDB; i<db->nDb; i++){
147001 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
147006 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
147010 pParse->checkSchema = 1;
147024 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
147034 sqlite3 *db = pParse->db;
147037 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
147038 assert( iDb>=0 && iDb<db->nDb );
147040 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
147044 const char *zDb = db->aDb[iDb].zDbSName;
147047 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
147059 db->aDb[iDb].zDbSName, pTrigger->zName
147062 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
147074 pHash = &(db->aDb[iDb].pSchema->trigHash);
147077 if( pTrigger->pSchema==pTrigger->pTabSchema ){
147081 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
147083 *pp = (*pp)->pNext;
147090 db->mDbFlags |= DBFLAG_SchemaChange;
147106 for(e=0; e<pEList->nExpr; e++){
147107 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
147116 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
147117 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
147140 || (pList->bReturning && pList->pNext==0) );
147143 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
147144 && pTab->pTrigger!=0
147149 if( pList==pTab->pTrigger ){
147153 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
147154 p->pNext = 0;
147158 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
147159 mask |= p->tr_tm;
147160 }else if( p->op==TK_RETURNING ){
147164 p->op = op;
147171 p->tr_tm = TRIGGER_BEFORE;
147173 p->tr_tm = TRIGGER_AFTER;
147175 mask |= p->tr_tm;
147176 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
147179 mask |= p->tr_tm;
147181 p = p->pNext;
147198 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
147199 || pParse->disableTriggers
147208 ** Convert the pStep->zTarget string into a SrcList and return a pointer
147221 sqlite3 *db = pParse->db;
147223 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
147225 assert( pSrc==0 || pSrc->nSrc==1 );
147228 Schema *pSchema = pStep->pTrig->pSchema;
147229 pSrc->a[0].zName = zName;
147230 if( pSchema!=db->aDb[1].pSchema ){
147231 pSrc->a[0].pSchema = pSchema;
147233 if( pStep->pFrom ){
147234 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
147235 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
147261 if( pTerm->op==TK_ASTERISK ) return 1;
147262 if( pTerm->op!=TK_DOT ) return 0;
147263 assert( pTerm->pRight!=0 );
147264 assert( pTerm->pLeft!=0 );
147265 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
147282 sqlite3 *db = pParse->db;
147285 for(i=0; i<pList->nExpr; i++){
147286 Expr *pOldExpr = pList->a[i].pExpr;
147290 for(jj=0; jj<pTab->nCol; jj++){
147292 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
147293 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
147295 if( !db->mallocFailed ){
147296 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
147297 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
147298 pItem->fg.eEName = ENAME_NAME;
147304 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
147305 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
147306 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
147307 pItem->fg.eEName = pList->a[i].fg.eEName;
147317 ** is generated in-line.
147325 Vdbe *v = pParse->pVdbe;
147326 sqlite3 *db = pParse->db;
147333 assert( pParse->bReturning );
147334 assert( db->pParse==pParse );
147335 pReturning = pParse->u1.pReturning;
147336 assert( pTrigger == &(pReturning->retTrig) );
147339 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
147343 sFrom.a[0].iCursor = -1;
147345 if( pParse->nErr==0 ){
147346 assert( db->mallocFailed==0 );
147350 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
147351 if( !db->mallocFailed ){
147354 if( pReturning->nRetCol==0 ){
147355 pReturning->nRetCol = pNew->nExpr;
147356 pReturning->iRetCur = pParse->nTab++;
147361 pParse->eTriggerOp = pTrigger->op;
147362 pParse->pTriggerTab = pTab;
147364 && ALWAYS(!db->mallocFailed)
147367 int nCol = pNew->nExpr;
147368 int reg = pParse->nMem+1;
147369 pParse->nMem += nCol+2;
147370 pReturning->iRetReg = reg;
147372 Expr *pCol = pNew->a[i].pExpr;
147373 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
147380 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
147381 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
147385 pParse->eTriggerOp = 0;
147386 pParse->pTriggerTab = 0;
147401 Vdbe *v = pParse->pVdbe;
147402 sqlite3 *db = pParse->db;
147404 assert( pParse->pTriggerTab && pParse->pToplevel );
147407 for(pStep=pStepList; pStep; pStep=pStep->pNext){
147418 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
147419 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
147421 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
147422 assert( pParse->okConstFactor==0 );
147425 if( pStep->zSpan ){
147427 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
147432 switch( pStep->op ){
147436 sqlite3ExprListDup(db, pStep->pExprList, 0),
147437 sqlite3ExprDup(db, pStep->pWhere, 0),
147438 pParse->eOrconf, 0, 0, 0
147446 sqlite3SelectDup(db, pStep->pSelect, 0),
147447 sqlite3IdListDup(db, pStep->pIdList),
147448 pParse->eOrconf,
147449 sqlite3UpsertDup(db, pStep->pUpsert)
147457 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
147462 default: assert( pStep->op==TK_SELECT ); {
147464 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
147495 ** Parse context structure pFrom has just been used to create a sub-vdbe
147500 assert( pFrom->zErrMsg==0 || pFrom->nErr );
147501 assert( pTo->zErrMsg==0 || pTo->nErr );
147502 if( pTo->nErr==0 ){
147503 pTo->zErrMsg = pFrom->zErrMsg;
147504 pTo->nErr = pFrom->nErr;
147505 pTo->rc = pFrom->rc;
147507 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
147512 ** Create and populate a new TriggerPrg object with a sub-program
147522 sqlite3 *db = pParse->db; /* Database handle */
147526 NameContext sNC; /* Name context for sub-vdbe */
147527 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
147529 Parse sSubParse; /* Parse context for sub-vdbe */
147531 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
147532 assert( pTop->pVdbe );
147536 ** list of the top-level Parse object sooner rather than later. */
147539 pPrg->pNext = pTop->pTriggerPrg;
147540 pTop->pTriggerPrg = pPrg;
147541 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
147543 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
147544 pPrg->pTrigger = pTrigger;
147545 pPrg->orconf = orconf;
147546 pPrg->aColmask[0] = 0xffffffff;
147547 pPrg->aColmask[1] = 0xffffffff;
147550 ** trigger sub-program. */
147556 sSubParse.zAuthContext = pTrigger->zName;
147557 sSubParse.eTriggerOp = pTrigger->op;
147558 sSubParse.nQueryLoop = pParse->nQueryLoop;
147559 sSubParse.prepFlags = pParse->prepFlags;
147564 pTrigger->zName, onErrorText(orconf),
147565 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
147566 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
147567 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
147568 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
147569 pTab->zName
147572 if( pTrigger->zName ){
147573 sqlite3VdbeChangeP4(v, -1,
147574 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
147580 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
147582 if( pTrigger->pWhen ){
147583 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
147584 if( db->mallocFailed==0
147593 /* Code the trigger program into the sub-vdbe. */
147594 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
147596 /* Insert an OP_Halt at the end of the sub-program. */
147601 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
147604 if( pParse->nErr==0 ){
147605 assert( db->mallocFailed==0 );
147606 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
147608 pProgram->nMem = sSubParse.nMem;
147609 pProgram->nCsr = sSubParse.nTab;
147610 pProgram->token = (void *)pTrigger;
147611 pPrg->aColmask[0] = sSubParse.oldmask;
147612 pPrg->aColmask[1] = sSubParse.newmask;
147624 ** Return a pointer to a TriggerPrg object containing the sub-program for
147638 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
147644 for(pPrg=pRoot->pTriggerPrg;
147645 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
147646 pPrg=pPrg->pNext
147652 pParse->db->errByteOffset = -1;
147675 assert( pPrg || pParse->nErr );
147678 ** is a pointer to the sub-vdbe containing the trigger program. */
147680 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
147682 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
147683 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
147685 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
147687 /* Set the P5 operand of the OP_Program instruction to non-zero if
147689 ** invocation is disallowed if (a) the sub-program is really a trigger,
147704 ** operation on pTab, this function is a no-op.
147709 ** (a copy of pTab->nCol), then registers are populated as follows:
147712 ** ------------------------------------------------------
147714 ** reg+1 OLD.* value of left-most column of pTab
147716 ** reg+N OLD.* value of right-most column of pTab
147718 ** reg+N+2 NEW.* value of left-most column of pTab
147720 ** reg+N+N+1 NEW.* value of right-most column of pTab
147753 for(p=pTrigger; p; p=p->pNext){
147758 assert( p->pSchema!=0 );
147759 assert( p->pTabSchema!=0 );
147760 assert( p->pSchema==p->pTabSchema
147761 || p->pSchema==pParse->db->aDb[1].pSchema );
147768 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
147769 && p->tr_tm==tr_tm
147770 && checkColumnOverlap(p->pColumns, pChanges)
147772 if( !p->bReturning ){
147782 ** Triggers may access values stored in the old.* or new.* pseudo-table.
147783 ** This function returns a 32-bit bitmask indicating which columns of the
147788 ** Bit 0 of the returned mask is set if the left-most column of the
147820 for(p=pTrigger; p; p=p->pNext){
147821 if( p->op==op
147822 && (tr_tm&p->tr_tm)
147823 && checkColumnOverlap(p->pColumns,pChanges)
147825 if( p->bReturning ){
147831 mask |= pPrg->aColmask[isNew];
147876 ** i-th column of table pTab. This routine sets the P4 parameter of the
147882 ** command. If the latter, then the row-records in the table btree on disk
147885 ** If the former, then all row-records are guaranteed to include a value
147899 ** If column as REAL affinity and the table is an ordinary b-tree table
147907 assert( pTab->nCol>i );
147908 pCol = &pTab->aCol[i];
147909 if( pCol->iDflt ){
147913 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
147914 assert( i<pTab->nCol );
147917 pCol->affinity, &pValue);
147923 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
147932 ** and false if not. This is an optimization. False-positives are a
147933 ** performance degradation, but false-negatives can result in a corrupt
147936 ** aXRef[j] will be non-negative if column j of the original table is
147946 i16 iIdxCol = pIdx->aiColumn[iCol];
147952 assert( pIdx->aColExpr!=0 );
147953 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
147954 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
147962 ** to be unchanged. This is an optimization. False-positives are a
147963 ** performance degradation, but false-negatives can result in a corrupt
147966 ** aXRef[j] will be non-negative if column j of the original table is
147975 if( pIdx->pPartIdxWhere==0 ) return 0;
147976 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
147984 ** table in the source-list (pSrc->a[0]).
147988 if( pRet ) pRet->iColumn = iCol+1;
147996 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
148004 ** SELECT <other-columns>, pChanges FROM pTabList
148006 ** GROUP BY <other-columns>
148012 ** the <other-columns> in the query above are is determined by the type
148013 ** of table pTabList->a[0].pTab.
148016 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
148020 ** If the table is actually a view, then <other-columns> are all columns of
148024 ** If the table is a virtual or ordinary intkey table, then <other-columns>
148027 ** rowid value in <other-columns> is used as the integer key, and the
148047 sqlite3 *db = pParse->db;
148048 Table *pTab = pTabList->a[0].pTab;
148068 assert( pTabList->nSrc>1 );
148070 pSrc->a[0].fg.notCte = 1;
148071 pSrc->a[0].iCursor = -1;
148072 pSrc->a[0].pTab->nTabRef--;
148073 pSrc->a[0].pTab = 0;
148076 for(i=0; i<pPk->nKeyCol; i++){
148077 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
148087 for(i=0; i<pTab->nCol; i++){
148100 assert( pChanges!=0 || pParse->db->mallocFailed );
148102 for(i=0; i<pChanges->nExpr; i++){
148104 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
148111 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
148113 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
148151 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
148152 ** an expression for the i-th column of the table.
148153 ** aXRef[i]==-1 if the i-th column is not changed. */
148159 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
148161 NameContext sNC; /* The name-context to resolve expressions in */
148183 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
148195 db = pParse->db;
148196 assert( db->pParse==pParse );
148197 if( pParse->nErr ){
148200 assert( db->mallocFailed==0 );
148206 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
148228 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
148234 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
148237 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
148262 iBaseCur = iDataCur = pParse->nTab++;
148265 testcase( pPk!=0 && pPk!=pTab->pIndex );
148266 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
148268 iDataCur = pParse->nTab;
148270 pParse->nTab++;
148274 iDataCur = pUpsert->iDataCur;
148275 iIdxCur = pUpsert->iIdxCur;
148276 pParse->nTab = iBaseCur;
148278 pTabList->a[0].iCursor = iDataCur;
148283 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
148285 aRegIdx = aXRef+pTab->nCol;
148289 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
148291 /* Initialize the name-context */
148309 for(i=0; i<pChanges->nExpr; i++){
148310 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
148313 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
148316 for(j=0; j<pTab->nCol; j++){
148317 if( pTab->aCol[j].hName==hCol
148318 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
148320 if( j==pTab->iPKey ){
148322 pRowidExpr = pChanges->a[i].pExpr;
148324 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
148328 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
148329 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
148330 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
148333 pTab->aCol[j].zCnName);
148341 if( j>=pTab->nCol ){
148342 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
148343 j = -1;
148345 pRowidExpr = pChanges->a[i].pExpr;
148348 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
148349 pParse->checkSchema = 1;
148356 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
148357 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
148358 db->aDb[iDb].zDbSName);
148362 aXRef[j] = -1;
148376 ** is non-negative, so the value of aXRef[] for generated columns can be
148377 ** set to any non-negative number. We use 99999 so that the value is
148380 if( pTab->tabFlags & TF_HasGenerated ){
148382 testcase( pTab->tabFlags & TF_HasVirtual );
148383 testcase( pTab->tabFlags & TF_HasStored );
148386 for(i=0; i<pTab->nCol; i++){
148388 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
148390 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
148406 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
148415 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
148420 reg = ++pParse->nMem;
148421 pParse->nMem += pIdx->nColumn;
148424 for(i=0; i<pIdx->nKeyCol; i++){
148426 reg = ++pParse->nMem;
148427 pParse->nMem += pIdx->nColumn;
148428 if( onError==OE_Default && pIdx->onError==OE_Replace ){
148438 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
148445 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
148454 ** two-pass update algorithm. */
148455 assert( aRegIdx[nAllIdx]==pParse->nMem );
148457 regOldRowid = regNewRowid = ++pParse->nMem;
148459 regOld = pParse->nMem + 1;
148460 pParse->nMem += pTab->nCol;
148463 regNewRowid = ++pParse->nMem;
148465 regNew = pParse->nMem + 1;
148466 pParse->nMem += pTab->nCol;
148471 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
148508 if( (db->flags&SQLITE_CountRows)!=0
148509 && !pParse->pTriggerTab
148510 && !pParse->nested
148511 && !pParse->bReturning
148514 regRowCount = ++pParse->nMem;
148520 iEph = pParse->nTab++;
148524 nPk = pPk ? pPk->nKeyCol : 0;
148525 iPk = pParse->nMem+1;
148526 pParse->nMem += nPk;
148527 pParse->nMem += nChangeFrom;
148528 regKey = ++pParse->nMem;
148530 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
148531 iEph = pParse->nTab++;
148532 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
148537 pKeyInfo->nAllField = nEphCol;
148571 ** Do not consider a single-pass strategy for a multi-row update if
148575 ** or index, causing a single-pass approach to malfunction. */
148577 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
148583 /* A one-pass strategy that might update more than one row may not
148599 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
148609 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
148613 aRegIdx[nAllIdx] = ++pParse->nMem;
148625 assert( pPk->aiColumn[i]>=0 );
148627 pPk->aiColumn[i], iPk+i);
148651 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
148652 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
148655 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
148733 /* Compute the old pre-UPDATE content of the row being changed, if that
148740 for(i=0; i<pTab->nCol; i++){
148741 u32 colFlags = pTab->aCol[i].colFlags;
148774 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
148775 if( i==pTab->iPKey ){
148777 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
148778 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
148783 int nOff = (isView ? pTab->nCol : nPk);
148787 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
148805 if( pTab->tabFlags & TF_HasGenerated ){
148806 testcase( pTab->tabFlags & TF_HasVirtual );
148807 testcase( pTab->tabFlags & TF_HasStored );
148821 /* The row-trigger may have deleted the row being updated. In this
148823 ** required. This behavior - what happens when the row being updated
148824 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
148835 /* After-BEFORE-trigger-reload-loop:
148841 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
148844 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
148845 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
148846 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
148847 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
148852 if( pTab->tabFlags & TF_HasGenerated ){
148853 testcase( pTab->tabFlags & TF_HasVirtual );
148854 testcase( pTab->tabFlags & TF_HasStored );
148886 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
148899 ** to invoke the pre-update hook.
148902 ** pre-update hook. If the caller invokes preupdate_new(), the returned
148916 if( !pParse->nested ){
148957 /* Nothing to do at end-of-loop for a single-pass */
148971 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
149009 ** There are two possible strategies - the default and the special
149037 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
149040 sqlite3 *db = pParse->db; /* Database connection */
149043 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
149047 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
149056 ephemTab = pParse->nTab++;
149058 regArg = pParse->nMem + 1;
149059 pParse->nMem += nArg;
149060 if( pSrc->nSrc>1 ){
149074 assert( pPk->nKeyCol==1 );
149075 iPk = pPk->aiColumn[0];
149077 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
149084 for(i=0; i<pTab->nCol; i++){
149087 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
149098 regRec = ++pParse->nMem;
149099 regRowid = ++pParse->nMem;
149108 for(i=0; i<pTab->nCol; i++){
149109 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
149111 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
149129 assert( pPk->nKeyCol==1 );
149130 iPk = pPk->aiColumn[0];
149141 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
149152 ** accept no-change records with serial_type 10 */
149163 if( pSrc->nSrc==1 ){
149196 ** 2018-04-12
149217 Upsert *pNext = p->pNextUpsert;
149218 sqlite3ExprListDelete(db, p->pUpsertTarget);
149219 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
149220 sqlite3ExprListDelete(db, p->pUpsertSet);
149221 sqlite3ExprDelete(db, p->pUpsertWhere);
149222 sqlite3DbFree(db, p->pToFree);
149238 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
149239 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
149240 sqlite3ExprListDup(db, p->pUpsertSet, 0),
149241 sqlite3ExprDup(db, p->pUpsertWhere, 0),
149242 sqlite3UpsertDup(db, p->pNextUpsert)
149267 pNew->pUpsertTarget = pTarget;
149268 pNew->pUpsertTargetWhere = pTargetWhere;
149269 pNew->pUpsertSet = pSet;
149270 pNew->pUpsertWhere = pWhere;
149271 pNew->isDoUpdate = pSet!=0;
149272 pNew->pNextUpsert = pNext;
149279 ** symbols in the conflict-target.
149293 ExprList *pTarget; /* The conflict-target clause */
149294 Expr *pTerm; /* One term of the conflict-target clause */
149299 assert( pTabList->nSrc==1 );
149300 assert( pTabList->a[0].pTab!=0 );
149302 assert( pUpsert->pUpsertTarget!=0 );
149304 /* Resolve all symbolic names in the conflict-target clause, which
149305 ** includes both the list of columns and the optional partial-index
149311 for(; pUpsert && pUpsert->pUpsertTarget;
149312 pUpsert=pUpsert->pNextUpsert, nClause++){
149313 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
149315 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
149319 pTab = pTabList->a[0].pTab;
149320 pTarget = pUpsert->pUpsertTarget;
149321 iCursor = pTabList->a[0].iCursor;
149323 && pTarget->nExpr==1
149324 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
149325 && pTerm->iColumn==XN_ROWID
149327 /* The conflict-target is the rowid of the primary table */
149328 assert( pUpsert->pUpsertIdx==0 );
149336 ** prior to comparing against the conflict-target expression.
149342 sCol[1].iTable = pTabList->a[0].iCursor;
149345 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
149348 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
149349 if( pIdx->pPartIdxWhere ){
149350 if( pUpsert->pUpsertTargetWhere==0 ) continue;
149351 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
149352 pIdx->pPartIdxWhere, iCursor)!=0 ){
149356 nn = pIdx->nKeyCol;
149359 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
149360 if( pIdx->aiColumn[ii]==XN_EXPR ){
149361 assert( pIdx->aColExpr!=0 );
149362 assert( pIdx->aColExpr->nExpr>ii );
149363 assert( pIdx->bHasExpr );
149364 pExpr = pIdx->aColExpr->a[ii].pExpr;
149365 if( pExpr->op!=TK_COLLATE ){
149371 sCol[1].iColumn = pIdx->aiColumn[ii];
149375 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
149389 pUpsert->pUpsertIdx = pIdx;
149392 if( pUpsert->pUpsertIdx==0 ){
149394 if( nClause==0 && pUpsert->pNextUpsert==0 ){
149415 pNext = pUpsert->pNextUpsert;
149417 if( pNext->pUpsertTarget==0 ) return 1;
149418 if( pNext->pUpsertIdx==0 ) return 1;
149431 && pUpsert->pUpsertTarget!=0
149432 && pUpsert->pUpsertIdx!=pIdx
149434 pUpsert = pUpsert->pNextUpsert;
149443 ** In this case parameter iCur is a cursor open on the table b-tree that
149449 Parse *pParse, /* The parsing and code-generating context */
149455 Vdbe *v = pParse->pVdbe;
149456 sqlite3 *db = pParse->db;
149464 iDataCur = pUpsert->iDataCur;
149476 int nPk = pPk->nKeyCol;
149477 int iPk = pParse->nMem+1;
149478 pParse->nMem += nPk;
149481 assert( pPk->aiColumn[i]>=0 );
149482 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
149484 VdbeComment((v, "%s.%s", pIdx->zName,
149485 pTab->aCol[pPk->aiColumn[i]].zCnName));
149496 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
149498 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
149500 for(i=0; i<pTab->nCol; i++){
149501 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
149502 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
149505 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
149506 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
149550 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
149622 if( pParse->nErr ) goto build_vacuum_end;
149631 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
149632 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
149635 iDb = sqlite3FindDb(pParse->db, pNm);
149642 iIntoReg = ++pParse->nMem;
149649 sqlite3ExprDelete(pParse->db, pInto);
149665 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
149666 u64 saved_flags; /* Saved value of db->flags */
149667 i64 saved_nChange; /* Saved value of db->nChange */
149668 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
149669 u32 saved_openFlags; /* Saved value of db->openFlags */
149679 if( !db->autoCommit ){
149681 return SQLITE_ERROR; /* IMP: R-12218-18073 */
149683 if( db->nVdbeActive>1 ){
149684 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
149685 return SQLITE_ERROR; /* IMP: R-15610-35227 */
149687 saved_openFlags = db->openFlags;
149690 sqlite3SetString(pzErrMsg, db, "non-text filename");
149694 db->openFlags &= ~SQLITE_OPEN_READONLY;
149695 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
149701 ** restored before returning. Then set the writable-schema flag, and
149703 saved_flags = db->flags;
149704 saved_mDbFlags = db->mDbFlags;
149705 saved_nChange = db->nChange;
149706 saved_nTotalChange = db->nTotalChange;
149707 saved_mTrace = db->mTrace;
149708 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
149709 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
149710 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
149712 db->mTrace = 0;
149714 zDbMain = db->aDb[iDb].zDbSName;
149715 pMain = db->aDb[iDb].pBt;
149724 ** An optimisation would be to use a non-journaled pager.
149732 nDb = db->nDb;
149734 db->openFlags = saved_openFlags;
149736 assert( (db->nDb-1)==nDb );
149737 pDb = &db->aDb[nDb];
149738 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
149739 pTemp = pDb->pBt;
149743 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
149748 db->mDbFlags |= DBFLAG_VacuumInto;
149750 /* For a VACUUM INTO, the pager-flags are set to the same values as
149753 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
149758 if( db->nextPagesize ){
149763 if( nKey ) db->nextPagesize = 0;
149767 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
149773 ** to ensure that we do not try to change the page-size on a WAL database.
149785 db->nextPagesize = 0;
149789 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
149790 || NEVER(db->mallocFailed)
149797 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
149804 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
149818 db->init.iDb = 0;
149831 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
149832 db->mDbFlags &= ~DBFLAG_Vacuum;
149851 ** both transactions are closed by this block - the main database
149905 /* Restore the original value of db->flags */
149906 db->init.iDb = 0;
149907 db->mDbFlags = saved_mDbFlags;
149908 db->flags = saved_flags;
149909 db->nChange = saved_nChange;
149910 db->nTotalChange = saved_nTotalChange;
149911 db->mTrace = saved_mTrace;
149912 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
149921 db->autoCommit = 1;
149924 sqlite3BtreeClose(pDb->pBt);
149925 pDb->pBt = 0;
149926 pDb->pSchema = 0;
149929 /* This both clears the schemas and reduces the size of the db->aDb[]
150000 pMod->zName = zCopy;
150001 pMod->pModule = pModule;
150002 pMod->pAux = pAux;
150003 pMod->xDestroy = xDestroy;
150004 pMod->pEpoTab = 0;
150005 pMod->nRefModule = 1;
150007 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
150035 sqlite3_mutex_enter(db->mutex);
150039 sqlite3_mutex_leave(db->mutex);
150045 ** External API function used to create a new virtual-table module.
150060 ** External API function used to create a new virtual-table module.
150076 ** External API to drop all virtual-table modules, except those named
150084 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
150089 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
150092 createModule(db, pMod->zName, 0, 0, 0);
150102 assert( pMod->nRefModule>0 );
150103 pMod->nRefModule--;
150104 if( pMod->nRefModule==0 ){
150105 if( pMod->xDestroy ){
150106 pMod->xDestroy(pMod->pAux);
150108 assert( pMod->pEpoTab==0 );
150122 pVTab->nRef++;
150127 ** pTab is a pointer to a Table structure representing a virtual-table.
150129 ** this virtual-table, if one has been created, or NULL otherwise.
150134 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
150139 ** Decrement the ref-count on a virtual table object. When the ref-count
150143 sqlite3 *db = pVTab->db;
150146 assert( pVTab->nRef>0 );
150147 assert( db->eOpenState==SQLITE_STATE_OPEN
150148 || db->eOpenState==SQLITE_STATE_ZOMBIE );
150150 pVTab->nRef--;
150151 if( pVTab->nRef==0 ){
150152 sqlite3_vtab *p = pVTab->pVtab;
150153 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
150155 p->pModule->xDisconnect(p);
150163 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
150166 ** connection db is left in the p->u.vtab.p list.
150173 pVTable = p->u.vtab.p;
150174 p->u.vtab.p = 0;
150180 ** database connection that may have an entry in the p->u.vtab.p list.
150182 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
150185 sqlite3 *db2 = pVTable->db;
150186 VTable *pNext = pVTable->pNext;
150190 p->u.vtab.p = pRet;
150191 pRet->pNext = 0;
150193 pVTable->pNext = db2->pDisconnect;
150194 db2->pDisconnect = pVTable;
150206 ** list in p->pVTab. It also decrements the VTable ref count. This is
150209 ** be being used by other shared-cache connections).
150216 assert( sqlite3_mutex_held(db->mutex) );
150218 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
150219 if( (*ppVTab)->db==db ){
150221 *ppVTab = pVTab->pNext;
150233 ** shared b-tree databases opened using connection db are held by the
150243 ** or, if the virtual table is stored in a non-sharable database, then
150247 ** by multiple threads. It is thread-safe.
150250 VTable *p = db->pDisconnect;
150253 assert( sqlite3_mutex_held(db->mutex) );
150256 db->pDisconnect = 0;
150259 VTable *pNext = p->pNext;
150267 ** Clear any and all virtual-table information from the Table record.
150271 ** Since it is a virtual-table, the Table structure contains a pointer
150283 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
150284 if( p->u.vtab.azArg ){
150286 for(i=0; i<p->u.vtab.nArg; i++){
150287 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
150289 sqlite3DbFree(db, p->u.vtab.azArg);
150294 ** Add a new module argument to pTable->u.vtab.azArg[].
150295 ** The string is not copied - the pointer is stored. The
150302 sqlite3 *db = pParse->db;
150305 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
150306 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
150307 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
150309 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
150313 int i = pTable->u.vtab.nArg++;
150316 pTable->u.vtab.azArg = azModuleArg;
150336 pTable = pParse->pNewTable;
150338 assert( 0==pTable->pIndex );
150339 pTable->eTabType = TABTYP_VTAB;
150341 db = pParse->db;
150343 assert( pTable->u.vtab.nArg==0 );
150346 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
150347 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
150348 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
150350 pParse->sNameToken.n = (int)(
150351 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
150360 if( pTable->u.vtab.azArg ){
150361 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
150363 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
150364 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
150371 ** in pParse->zArg[] and appends it to the list of arguments on the
150372 ** virtual table currently under construction in pParse->pTable.
150375 if( pParse->sArg.z && pParse->pNewTable ){
150376 const char *z = (const char*)pParse->sArg.z;
150377 int n = pParse->sArg.n;
150378 sqlite3 *db = pParse->db;
150379 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
150388 Table *pTab = pParse->pNewTable; /* The table being constructed */
150389 sqlite3 *db = pParse->db; /* The database connection */
150394 pParse->sArg.z = 0;
150395 if( pTab->u.vtab.nArg<1 ) return;
150403 if( !db->init.busy ){
150414 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
150416 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
150422 ** The VM register number pParse->regRowid holds the rowid of an
150426 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
150431 db->aDb[iDb].zDbSName,
150432 pTab->zName,
150433 pTab->zName,
150435 pParse->regRowid
150441 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
150445 iReg = ++pParse->nMem;
150446 sqlite3VdbeLoadString(v, iReg, pTab->zName);
150449 /* If we are rereading the sqlite_schema table create the in-memory
150452 Schema *pSchema = pTab->pSchema;
150453 const char *zName = pTab->zName;
150456 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
150462 pParse->pNewTable = 0;
150472 pParse->sArg.z = 0;
150473 pParse->sArg.n = 0;
150481 Token *pArg = &pParse->sArg;
150482 if( pArg->z==0 ){
150483 pArg->z = p->z;
150484 pArg->n = p->n;
150486 assert(pArg->z <= p->z);
150487 pArg->n = (int)(&p->z[p->n] - pArg->z);
150507 int nArg = pTab->u.vtab.nArg;
150514 azArg = (const char *const*)pTab->u.vtab.azArg;
150516 /* Check that the virtual-table is not already being initialized */
150517 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
150518 if( pCtx->pTab==pTab ){
150520 "vtable constructor called recursively: %s", pTab->zName
150526 zModuleName = sqlite3DbStrDup(db, pTab->zName);
150537 pVTable->db = db;
150538 pVTable->pMod = pMod;
150539 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
150541 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
150542 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
150545 assert( &db->pVtabCtx );
150549 sCtx.pPrior = db->pVtabCtx;
150551 db->pVtabCtx = &sCtx;
150552 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
150553 db->pVtabCtx = sCtx.pPrior;
150565 }else if( ALWAYS(pVTable->pVtab) ){
150568 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
150569 pVTable->pVtab->pModule = pMod->pModule;
150570 pMod->nRefModule++;
150571 pVTable->nRef = 1;
150574 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
150581 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
150585 pVTable->pNext = pTab->u.vtab.p;
150586 pTab->u.vtab.p = pVTable;
150588 for(iCol=0; iCol<pTab->nCol; iCol++){
150589 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
150595 && (i==0 || zType[i-1]==' ')
150608 assert(zType[i-1]==' ');
150609 zType[i-1] = '\0';
150611 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
150612 pTab->tabFlags |= TF_HasHidden;
150615 pTab->tabFlags |= oooHidden;
150630 ** This call is a no-op if table pTab is not a virtual table.
150633 sqlite3 *db = pParse->db;
150645 zMod = pTab->u.vtab.azArg[0];
150646 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
150649 const char *zModule = pTab->u.vtab.azArg[0];
150654 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
150657 pParse->rc = rc;
150665 ** Grow the db->aVTrans[] array so that there is room for at least one
150666 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
150672 if( (db->nVTrans%ARRAY_INCR)==0 ){
150675 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
150676 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
150680 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
150681 db->aVTrans = aVTrans;
150693 db->aVTrans[db->nVTrans++] = pVTab;
150711 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
150712 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
150715 zMod = pTab->u.vtab.azArg[0];
150716 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
150722 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
150726 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
150758 sqlite3_mutex_enter(db->mutex);
150759 pCtx = db->pVtabCtx;
150760 if( !pCtx || pCtx->bDeclared ){
150762 sqlite3_mutex_leave(db->mutex);
150765 pTab = pCtx->pTab;
150772 ** schema. Nevertheless, defend against that (turn off db->init.busy)
150774 assert( db->init.busy==0 );
150775 initBusy = db->init.busy;
150776 db->init.busy = 0;
150780 && ALWAYS(!db->mallocFailed)
150784 if( !pTab->aCol ){
150787 pTab->aCol = pNew->aCol;
150788 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
150789 pTab->nNVCol = pTab->nCol = pNew->nCol;
150790 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
150791 pNew->nCol = 0;
150792 pNew->aCol = 0;
150793 assert( pTab->pIndex==0 );
150796 && pCtx->pVTable->pMod->pModule->xUpdate!=0
150797 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
150799 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
150800 ** or else must have a single-column PRIMARY KEY */
150803 pIdx = pNew->pIndex;
150805 assert( pIdx->pNext==0 );
150806 pTab->pIndex = pIdx;
150807 pNew->pIndex = 0;
150808 pIdx->pTable = pTab;
150811 pCtx->bDeclared = 1;
150825 db->init.busy = initBusy;
150829 sqlite3_mutex_leave(db->mutex);
150838 ** This call is a no-op if zTab is not a virtual table.
150844 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
150847 && ALWAYS(pTab->u.vtab.p!=0)
150851 for(p=pTab->u.vtab.p; p; p=p->pNext){
150852 assert( p->pVtab );
150853 if( p->pVtab->nRef>0 ){
150858 xDestroy = p->pMod->pModule->xDestroy;
150859 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
150861 pTab->nTabRef++;
150862 rc = xDestroy(p->pVtab);
150865 assert( pTab->u.vtab.p==p && p->pNext==0 );
150866 p->pVtab = 0;
150867 pTab->u.vtab.p = 0;
150886 if( db->aVTrans ){
150887 VTable **aVTrans = db->aVTrans;
150888 db->aVTrans = 0;
150889 for(i=0; i<db->nVTrans; i++){
150891 sqlite3_vtab *p = pVTab->pVtab;
150894 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
150897 pVTab->iSavepoint = 0;
150901 db->nVTrans = 0;
150910 ** If an error message is available, leave it in p->zErrMsg.
150915 VTable **aVTrans = db->aVTrans;
150917 db->aVTrans = 0;
150918 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
150920 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
150921 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
150926 db->aVTrans = aVTrans;
150960 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
150971 pModule = pVTab->pVtab->pModule;
150973 if( pModule->xBegin ){
150977 for(i=0; i<db->nVTrans; i++){
150978 if( db->aVTrans[i]==pVTab ){
150987 rc = pModule->xBegin(pVTab->pVtab);
150989 int iSvpt = db->nStatement + db->nSavepoint;
150991 if( iSvpt && pModule->xSavepoint ){
150992 pVTab->iSavepoint = iSvpt;
150993 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
151020 assert( iSavepoint>=-1 );
151021 if( db->aVTrans ){
151023 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
151024 VTable *pVTab = db->aVTrans[i];
151025 const sqlite3_module *pMod = pVTab->pMod->pModule;
151026 if( pVTab->pVtab && pMod->iVersion>=2 ){
151031 xMethod = pMod->xSavepoint;
151032 pVTab->iSavepoint = iSavepoint+1;
151035 xMethod = pMod->xRollbackTo;
151038 xMethod = pMod->xRelease;
151041 if( xMethod && pVTab->iSavepoint>iSavepoint ){
151042 rc = xMethod(pVTab->pVtab, iSavepoint);
151080 if( pExpr->op!=TK_COLUMN ) return pDef;
151082 pTab = pExpr->y.pTab;
151085 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
151087 assert( pVtab->pModule!=0 );
151088 pMod = (sqlite3_module *)pVtab->pModule;
151089 if( pMod->xFindFunction==0 ) return pDef;
151095 ** with an all lower-case function name. Continue in this tradition to
151101 for(i=0; pDef->zName[i]; i++){
151102 unsigned char x = (unsigned char)pDef->zName[i];
151107 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
151115 + sqlite3Strlen30(pDef->zName) + 1);
151120 pNew->zName = (const char*)&pNew[1];
151121 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
151122 pNew->xSFunc = xSFunc;
151123 pNew->pUserData = pArg;
151124 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
151129 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
151132 ** is a no-op.
151140 for(i=0; i<pToplevel->nVtabLock; i++){
151141 if( pTab==pToplevel->apVtabLock[i] ) return;
151143 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
151144 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
151146 pToplevel->apVtabLock = apVtabLock;
151147 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
151149 sqlite3OomFault(pToplevel->db);
151156 ** exist. Return non-zero if either the eponymous virtual table instance
151163 ** instances always exist. They cannot be DROP-ed.
151169 const sqlite3_module *pModule = pMod->pModule;
151173 sqlite3 *db = pParse->db;
151174 if( pMod->pEpoTab ) return 1;
151175 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
151178 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
151179 if( pTab->zName==0 ){
151183 pMod->pEpoTab = pTab;
151184 pTab->nTabRef = 1;
151185 pTab->eTabType = TABTYP_VTAB;
151186 pTab->pSchema = db->aDb[0].pSchema;
151187 assert( pTab->u.vtab.nArg==0 );
151188 pTab->iPKey = -1;
151189 pTab->tabFlags |= TF_Eponymous;
151190 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
151192 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
151193 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
151207 Table *pTab = pMod->pEpoTab;
151212 pTab->tabFlags |= TF_Ephemeral;
151214 pMod->pEpoTab = 0;
151234 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
151235 return (int)aMap[db->vtabOnConflict-1];
151251 sqlite3_mutex_enter(db->mutex);
151252 p = db->pVtabCtx;
151256 assert( p->pTab==0 || IsVirtual(p->pTab) );
151260 p->pVTable->bConstraint = (u8)va_arg(ap, int);
151264 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
151268 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
151280 sqlite3_mutex_leave(db->mutex);
151289 ** 2015-06-06
151302 ** This file was split off from where.c on 2015-06-06 in order to reduce the
151311 ** 2013-11-12
151380 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
151388 int addrSkip; /* Jump here for next iteration of skip-scan */
151392 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
151393 int addrBignull; /* Jump here for next part of big-null scan */
151403 union { /* Information that depends on pWLoop->wsFlags */
151409 int iBase; /* Base register of multi-key index record */
151413 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
151427 ** prevent a query solution - which is an error) and many terms of the
151429 ** potential way of implementing that FROM-clause term, together with
151435 ** and that minimize the overall cost.
151445 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
151488 ** correspond to the subquery(s) of OR-clause processing. Only the
151504 ** vector, not a scalar, and because dependencies are many-to-one, not
151505 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
151521 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
151534 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
151565 ** spread out over the non-negative integers. For example, the cursor
151584 int iParent; /* Disable pWC->a[iParent] when this term disabled */
151594 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
151607 #define TERM_OK 0x0040 /* Used during OR-clause processing */
151613 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
151620 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
151631 int k; /* Resume scanning at this->pWC->a[this->k] */
151637 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
151659 int nBase; /* Number of terms through the last non-Virtual */
151697 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
151698 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
151705 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
151706 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
151745 ** routine. The limit is high enough that is should not impact real-world
151783 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
151792 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
151806 ** Private interfaces - callable only by other where.c routines.
151858 int iLevel, /* Which level of pWInfo->a[] should be coded */
151886 ** OR-ed combination of these values can be used when searching for
151898 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
151899 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
151900 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
151901 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
151905 #define WO_OR 0x0200 /* Two or more OR-connected terms */
151906 #define WO_AND 0x0400 /* Two or more AND-connected terms */
151909 #define WO_ROWVAL 0x2000 /* A row-value term */
151912 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
151927 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
151935 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
151940 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
151942 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
151945 #define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
151955 ** Return the name of the i-th column of the pIdx index.
151958 i = pIdx->aiColumn[i];
151961 return pIdx->pTable->aCol[i].zCnName;
151976 int iTerm, /* Zero-based index of first term. */
151977 int bAnd, /* Non-zero to append " AND " */
152017 Index *pIndex = pLoop->u.btree.pIndex;
152018 u16 nEq = pLoop->u.btree.nEq;
152019 u16 nSkip = pLoop->nSkip;
152022 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
152031 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
152032 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
152035 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
152036 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
152042 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
152044 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
152058 if( sqlite3ParseToplevel(pParse)->explain==2 )
152061 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
152062 Vdbe *v = pParse->pVdbe; /* VM being constructed */
152063 sqlite3 *db = pParse->db; /* Database handle */
152071 pLoop = pLevel->pWLoop;
152072 flags = pLoop->wsFlags;
152076 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
152086 assert( pLoop->u.btree.pIndex!=0 );
152087 pIdx = pLoop->u.btree.pIndex;
152089 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
152104 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
152110 const Table *pTab = pItem->pTab;
152111 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
152133 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
152136 if( pItem->fg.jointype & JT_LEFT ){
152137 sqlite3_str_appendf(&str, " LEFT-JOIN");
152140 if( pLoop->nOut>=10 ){
152142 sqlite3LogEstToInt(pLoop->nOut));
152150 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
152160 ** required and this routine is a no-op.
152171 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
152172 Vdbe *v = pParse->pVdbe; /* VM being constructed */
152173 sqlite3 *db = pParse->db; /* Database handle */
152183 pLoop = pLevel->pWLoop;
152184 if( pLoop->wsFlags & WHERE_IPK ){
152185 const Table *pTab = pItem->pTab;
152186 if( pTab->iPKey>=0 ){
152187 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
152192 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
152193 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
152194 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
152201 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
152223 WhereLoop *pLoop = pLvl->pWLoop;
152224 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
152225 zObj = pLoop->u.btree.pIndex->zName;
152227 zObj = pSrclist->a[pLvl->iFrom].zName;
152230 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
152276 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
152281 while( (pTerm->wtFlags & TERM_CODED)==0
152282 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
152283 && (pLevel->notReady & pTerm->prereqAll)==0
152285 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
152286 pTerm->wtFlags |= TERM_LIKECOND;
152288 pTerm->wtFlags |= TERM_CODED;
152292 sqlite3DebugPrintf("DISABLE-");
152293 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
152296 if( pTerm->iParent<0 ) break;
152297 pTerm = &pTerm->pWC->a[pTerm->iParent];
152299 pTerm->nChild--;
152300 if( pTerm->nChild!=0 ) break;
152310 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
152317 Vdbe *v = pParse->pVdbe;
152319 assert( pParse->db->mallocFailed );
152329 n--;
152333 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
152334 n--;
152405 sqlite3 *db = pParse->db;
152408 if( db->mallocFailed==0 ){
152417 pOrigRhs = pNew->x.pSelect->pEList;
152418 assert( pNew->pLeft!=0 );
152419 assert( ExprUseXList(pNew->pLeft) );
152420 pOrigLhs = pNew->pLeft->x.pList;
152421 for(i=iEq; i<pLoop->nLTerm; i++){
152422 if( pLoop->aLTerm[i]->pExpr==pX ){
152424 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
152425 iField = pLoop->aLTerm[i]->u.x.iField - 1;
152426 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
152427 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
152428 pOrigRhs->a[iField].pExpr = 0;
152429 assert( pOrigLhs->a[iField].pExpr!=0 );
152430 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
152431 pOrigLhs->a[iField].pExpr = 0;
152436 pNew->pLeft->x.pList = pLhs;
152437 pNew->x.pSelect->pEList = pRhs;
152438 if( pLhs && pLhs->nExpr==1 ){
152442 Expr *p = pLhs->a[0].pExpr;
152443 pLhs->a[0].pExpr = 0;
152444 sqlite3ExprDelete(db, pNew->pLeft);
152445 pNew->pLeft = p;
152447 pSelect = pNew->x.pSelect;
152448 if( pSelect->pOrderBy ){
152450 ** iOrderByCol variables. These are set to non-zero when an
152452 ** result-set. Since the result-set of the SELECT statement may
152456 ExprList *pOrderBy = pSelect->pOrderBy;
152457 for(i=0; i<pOrderBy->nExpr; i++){
152458 pOrderBy->a[i].u.x.iOrderByCol = 0;
152485 ** straight-line code. For constraints of the form X IN (...)
152493 int bRev, /* True for reverse-order IN operations */
152496 Expr *pX = pTerm->pExpr;
152497 Vdbe *v = pParse->pVdbe;
152500 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
152502 if( pX->op==TK_EQ || pX->op==TK_IS ){
152503 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
152504 }else if( pX->op==TK_ISNULL ){
152512 WhereLoop *pLoop = pLevel->pWLoop;
152517 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
152518 && pLoop->u.btree.pIndex!=0
152519 && pLoop->u.btree.pIndex->aSortOrder[iEq]
152525 assert( pX->op==TK_IN );
152529 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
152534 for(i=iEq;i<pLoop->nLTerm; i++){
152535 assert( pLoop->aLTerm[i]!=0 );
152536 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
152540 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
152543 Expr *pExpr = pTerm->pExpr;
152544 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
152545 sqlite3 *db = pParse->db;
152547 if( !db->mallocFailed ){
152548 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
152550 pExpr->iTable = iTab;
152554 int n = sqlite3ExprVectorSize(pX->pLeft);
152555 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
152569 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
152570 pLoop->wsFlags |= WHERE_IN_ABLE;
152571 if( pLevel->u.in.nIn==0 ){
152572 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
152574 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
152575 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
152578 i = pLevel->u.in.nIn;
152579 pLevel->u.in.nIn += nEq;
152580 pLevel->u.in.aInLoop =
152581 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
152582 pLevel->u.in.aInLoop,
152583 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
152584 pIn = pLevel->u.in.aInLoop;
152588 for(i=iEq;i<pLoop->nLTerm; i++){
152589 if( pLoop->aLTerm[i]->pExpr==pX ){
152590 int iOut = iReg + i - iEq;
152592 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
152595 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
152599 pIn->iCur = iTab;
152600 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
152602 pIn->iBase = iReg - i;
152603 pIn->nPrefix = i;
152605 pIn->nPrefix = 0;
152608 pIn->eEndLoopOp = OP_Noop;
152614 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
152615 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
152617 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
152619 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
152622 pLevel->u.in.nIn = 0;
152624 sqlite3DbFree(pParse->db, aiMap);
152635 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
152637 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
152638 || (pTerm->eOperator & WO_EQUIV)==0
152659 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
152660 ** The only thing it does is allocate the pLevel->iMem memory cell and
152699 u16 nSkip; /* Number of left-most columns to skip */
152700 Vdbe *v = pParse->pVdbe; /* The vm under construction */
152710 pLoop = pLevel->pWLoop;
152711 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
152712 nEq = pLoop->u.btree.nEq;
152713 nSkip = pLoop->nSkip;
152714 pIdx = pLoop->u.btree.pIndex;
152719 regBase = pParse->nMem + 1;
152720 nReg = pLoop->u.btree.nEq + nExtraReg;
152721 pParse->nMem += nReg;
152723 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
152724 assert( zAff!=0 || pParse->db->mallocFailed );
152727 int iIdxCur = pLevel->iIdxCur;
152728 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
152732 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
152734 assert( pLevel->addrSkip==0 );
152735 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
152742 testcase( pIdx->aiColumn[j]==XN_EXPR );
152752 pTerm = pLoop->aLTerm[j];
152756 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
152757 testcase( pTerm->wtFlags & TERM_VIRTUAL );
152769 pTerm = pLoop->aLTerm[j];
152770 if( pTerm->eOperator & WO_IN ){
152771 if( pTerm->pExpr->flags & EP_xIsSelect ){
152778 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
152779 Expr *pRight = pTerm->pExpr->pRight;
152780 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
152781 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
152784 if( pParse->nErr==0 ){
152785 assert( pParse->db->mallocFailed==0 );
152815 ** becomes a no-op.
152822 if( pTerm->wtFlags & TERM_LIKEOPT ){
152824 assert( pLevel->iLikeRepCntr>0 );
152827 assert( pOp->opcode==OP_String8
152828 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
152829 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
152830 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
152853 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
152856 struct CCurHint *pHint = pWalker->u.pCCurHint;
152857 assert( pHint->pIdx!=0 );
152858 if( pExpr->op==TK_COLUMN
152859 && pExpr->iTable==pHint->iTabCur
152860 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
152862 pWalker->eCode = 1;
152869 ** should be included in the cursor-hint for a table that is on the rhs
152870 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
152883 if( pExpr->op==TK_IS
152884 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
152885 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
152887 pWalker->eCode = 1;
152888 }else if( pExpr->op==TK_FUNCTION ){
152891 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
152892 pWalker->eCode = 1;
152919 struct CCurHint *pHint = pWalker->u.pCCurHint;
152920 if( pExpr->op==TK_COLUMN ){
152921 if( pExpr->iTable!=pHint->iTabCur ){
152922 int reg = ++pWalker->pParse->nMem; /* Register for column value */
152923 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
152924 pExpr->op = TK_REGISTER;
152925 pExpr->iTable = reg;
152926 }else if( pHint->pIdx!=0 ){
152927 pExpr->iTable = pHint->iIdxCur;
152928 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
152929 assert( pExpr->iColumn>=0 );
152931 }else if( pExpr->op==TK_AGG_FUNCTION ){
152933 ** be a correlated sub-query, and expression pExpr is an aggregate from
152951 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
152953 Parse *pParse = pWInfo->pParse;
152954 sqlite3 *db = pParse->db;
152955 Vdbe *v = pParse->pVdbe;
152957 WhereLoop *pLoop = pLevel->pWLoop;
152966 iCur = pLevel->iTabCur;
152967 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
152969 sHint.iIdxCur = pLevel->iIdxCur;
152970 sHint.pIdx = pLoop->u.btree.pIndex;
152974 pWC = &pWInfo->sWC;
152975 for(i=0; i<pWC->nBase; i++){
152976 pTerm = &pWC->a[i];
152977 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
152978 if( pTerm->prereqAll & pLevel->notReady ) continue;
152982 ** from the cursor-hint.
153002 if( pTabItem->fg.jointype & JT_LEFT ){
153003 Expr *pExpr = pTerm->pExpr;
153005 || pExpr->w.iJoin!=pTabItem->iCursor
153009 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
153013 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
153016 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
153019 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
153020 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
153021 if( j<pLoop->nLTerm ) continue;
153024 /* No subqueries or non-deterministic functions allowed */
153025 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
153032 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
153037 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
153048 # define codeCursorHint(A,B,C,D) /* No-op */
153052 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
153063 ** However, if the scan currently being coded is a branch of an OR-loop and
153080 int iCur, /* Cursor for IPK b-tree */
153083 Parse *pParse = pWInfo->pParse; /* Parse context */
153084 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
153087 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
153089 pWInfo->bDeferredSeek = 1;
153091 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
153092 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
153095 Table *pTab = pIdx->pTable;
153096 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
153098 ai[0] = pTab->nCol;
153099 for(i=0; i<pIdx->nColumn-1; i++){
153101 assert( pIdx->aiColumn[i]<pTab->nCol );
153102 x1 = pIdx->aiColumn[i];
153107 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
153126 Vdbe *v = pParse->pVdbe;
153128 assert( p->op==TK_SELECT );
153130 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
153137 pList = p->x.pList;
153138 assert( nReg<=pList->nExpr );
153140 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
153144 assert( nReg==1 || pParse->nErr );
153163 while( pTruth->op==TK_AND ){
153164 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
153165 pTruth = pTruth->pRight;
153167 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
153169 if( pTerm->wtFlags & TERM_CODED ) continue;
153170 pExpr = pTerm->pExpr;
153172 pTerm->wtFlags |= TERM_CODED;
153182 ** inner-loop Bloom filters, then evaluate those filters now, before the
153188 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
153195 int iLevel, /* Which level of pWInfo->a[] should be coded */
153199 while( ++iLevel < pWInfo->nLevel ){
153200 WhereLevel *pLevel = &pWInfo->a[iLevel];
153201 WhereLoop *pLoop = pLevel->pWLoop;
153202 if( pLevel->regFilter==0 ) continue;
153203 if( pLevel->pWLoop->nSkip ) continue;
153204 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
153205 ** vvvvv--' pLevel->regFilter if this were true. */
153206 if( NEVER(pLoop->prereq & notReady) ) continue;
153207 assert( pLevel->addrBrk==0 );
153208 pLevel->addrBrk = addrNxt;
153209 if( pLoop->wsFlags & WHERE_IPK ){
153210 WhereTerm *pTerm = pLoop->aLTerm[0];
153213 assert( pTerm->pExpr!=0 );
153214 testcase( pTerm->wtFlags & TERM_VIRTUAL );
153217 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
153218 VdbeCoverage(pParse->pVdbe);
153219 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
153221 VdbeCoverage(pParse->pVdbe);
153223 u16 nEq = pLoop->u.btree.nEq;
153227 assert( pLoop->wsFlags & WHERE_INDEXED );
153228 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
153231 sqlite3DbFree(pParse->db, zStartAff);
153232 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
153234 VdbeCoverage(pParse->pVdbe);
153236 pLevel->regFilter = 0;
153237 pLevel->addrBrk = 0;
153242 ** Generate code for the start of the iLevel-th loop in the WHERE clause
153249 int iLevel, /* Which level of pWInfo->a[] should be coded */
153270 pWC = &pWInfo->sWC;
153271 db = pParse->db;
153272 pLoop = pLevel->pWLoop;
153273 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
153274 iCur = pTabItem->iCursor;
153275 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
153276 bRev = (pWInfo->revMask>>iLevel)&1;
153277 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
153281 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
153287 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
153289 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
153304 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
153305 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
153311 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
153312 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
153314 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
153315 pLevel->iLeftJoin = ++pParse->nMem;
153316 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
153317 VdbeComment((v, "init LEFT JOIN no-match flag"));
153322 for(j=iLevel; j>0; j--){
153323 if( pWInfo->a[j].iLeftJoin ) break;
153324 if( pWInfo->a[j].pRJ ) break;
153326 addrHalt = pWInfo->a[j].addrBrk;
153328 /* Special case of a FROM clause subquery implemented as a co-routine */
153329 if( pTabItem->fg.viaCoroutine ){
153330 int regYield = pTabItem->regReturn;
153331 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
153332 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
153334 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
153335 pLevel->op = OP_Goto;
153339 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
153340 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
153345 int nConstraint = pLoop->nLTerm;
153348 addrNotFound = pLevel->addrBrk;
153351 pTerm = pLoop->aLTerm[j];
153353 if( pTerm->eOperator & WO_IN ){
153354 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
153355 int iTab = pParse->nTab++;
153356 int iCache = ++pParse->nMem;
153357 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
153361 addrNotFound = pLevel->addrNxt;
153364 Expr *pRight = pTerm->pExpr->pRight;
153366 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
153367 && pLoop->u.vtab.bOmitOffset
153369 assert( pTerm->eOperator==WO_AUX );
153370 assert( pWInfo->pSelect!=0 );
153371 assert( pWInfo->pSelect->iOffset>0 );
153372 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
153377 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
153380 pLoop->u.vtab.idxStr,
153381 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
153383 pLoop->u.vtab.needFree = 0;
153385 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
153386 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
153387 pLevel->p1 = iCur;
153388 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
153389 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153390 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
153393 pTerm = pLoop->aLTerm[j];
153394 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
153398 if( (pTerm->eOperator & WO_IN)!=0
153399 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
153400 && !db->mallocFailed
153405 int iIn; /* IN loop corresponding to the j-th constraint */
153412 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
153413 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
153414 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
153415 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
153417 testcase( pOp->opcode==OP_Rowid );
153418 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
153427 if( !db->mallocFailed ){
153428 int iFld = pTerm->u.x.iField;
153429 Expr *pLeft = pTerm->pExpr->pLeft;
153432 assert( pLeft->op==TK_VECTOR );
153434 assert( iFld<=pLeft->x.pList->nExpr );
153435 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
153437 pCompare->pLeft = pLeft;
153439 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
153441 pRight->iTable = iReg+j+2;
153443 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
153446 pCompare->pLeft = 0;
153454 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
153462 if( (pLoop->wsFlags & WHERE_IPK)!=0
153463 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
153470 assert( pLoop->u.btree.nEq==1 );
153471 pTerm = pLoop->aLTerm[0];
153473 assert( pTerm->pExpr!=0 );
153474 testcase( pTerm->wtFlags & TERM_VIRTUAL );
153475 iReleaseReg = ++pParse->nMem;
153478 addrNxt = pLevel->addrNxt;
153479 if( pLevel->regFilter ){
153482 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
153489 pLevel->op = OP_Noop;
153490 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
153491 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
153502 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
153503 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
153529 assert( (pStart->wtFlags & TERM_VNULL)==0 );
153530 testcase( pStart->wtFlags & TERM_VIRTUAL );
153531 pX = pStart->pExpr;
153533 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
153534 if( sqlite3ExprIsVector(pX->pRight) ){
153536 codeExprOrVector(pParse, pX->pRight, r1, 1);
153537 testcase( pX->op==TK_GT );
153538 testcase( pX->op==TK_GE );
153539 testcase( pX->op==TK_LT );
153540 testcase( pX->op==TK_LE );
153541 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
153542 assert( pX->op!=TK_GT || op==OP_SeekGE );
153543 assert( pX->op!=TK_GE || op==OP_SeekGE );
153544 assert( pX->op!=TK_LT || op==OP_SeekLE );
153545 assert( pX->op!=TK_LE || op==OP_SeekLE );
153547 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
153549 op = aMoveOp[(pX->op - TK_GT)];
153553 VdbeCoverageIf(v, pX->op==TK_GT);
153554 VdbeCoverageIf(v, pX->op==TK_LE);
153555 VdbeCoverageIf(v, pX->op==TK_LT);
153556 VdbeCoverageIf(v, pX->op==TK_GE);
153565 pX = pEnd->pExpr;
153567 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
153568 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
153569 testcase( pEnd->wtFlags & TERM_VIRTUAL );
153570 memEndValue = ++pParse->nMem;
153571 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
153572 if( 0==sqlite3ExprIsVector(pX->pRight)
153573 && (pX->op==TK_LT || pX->op==TK_GT)
153579 if( 0==sqlite3ExprIsVector(pX->pRight) ){
153584 pLevel->op = bRev ? OP_Prev : OP_Next;
153585 pLevel->p1 = iCur;
153586 pLevel->p2 = start;
153587 assert( pLevel->p5==0 );
153589 iRowidReg = ++pParse->nMem;
153598 }else if( pLoop->wsFlags & WHERE_INDEXED ){
153603 ** left-most columns of the index. It may also contain
153606 ** the right-most column can be an inequality - the rest must
153646 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
153647 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
153648 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
153664 int regBignull = 0; /* big-null flag register */
153667 pIdx = pLoop->u.btree.pIndex;
153668 iIdxCur = pLevel->iIdxCur;
153669 assert( nEq>=pLoop->nSkip );
153675 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
153676 pRangeStart = pLoop->aLTerm[j++];
153677 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
153679 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
153680 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
153682 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
153683 pRangeEnd = pLoop->aLTerm[j++];
153684 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
153686 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
153688 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
153689 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
153690 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
153692 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
153696 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
153698 pLevel->iLikeRepCntr <<=1;
153699 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
153703 j = pIdx->aiColumn[nEq];
153704 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
153709 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
153712 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
153715 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
153718 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
153719 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
153723 testcase( pLoop->nSkip>0 );
153726 pLevel->regBignull = regBignull = ++pParse->nMem;
153727 if( pLevel->iLeftJoin ){
153730 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
153737 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
153743 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
153759 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
153761 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
153762 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
153763 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
153764 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
153765 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
153766 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
153772 Expr *pRight = pRangeStart->pExpr->pRight;
153775 if( (pRangeStart->wtFlags & TERM_VNULL)==0
153785 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
153802 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
153803 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
153804 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
153810 VdbeComment((v, "NULL-scan pass ctr"));
153812 if( pLevel->regFilter ){
153813 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
153821 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
153832 (pIdx->aiRowLogEst[0]+9)/10);
153857 nConstraint-startEq);
153871 assert( pLevel->p2==0 );
153873 Expr *pRight = pRangeEnd->pExpr->pRight;
153875 /* For a seek-scan that has a range on the lowest term of the index,
153878 ** over that initialization, leaving the range-end value set to the
153879 ** range-start value, resulting in a wrong answer.
153880 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
153882 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153886 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
153896 assert( pParse->db->mallocFailed );
153899 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
153917 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
153922 /* Except, skip the end-of-range check while doing the NULL-scan */
153924 VdbeComment((v, "If NULL-scan 2nd pass"));
153936 /* During a NULL-scan, check to see if we have reached the end of
153942 VdbeComment((v, "If NULL-scan 1st pass"));
153953 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
153958 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
153959 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
153962 }else if( HasRowid(pIdx->pTable) ){
153965 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
153966 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
153967 for(j=0; j<pPk->nKeyCol; j++){
153968 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
153972 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
153975 if( pLevel->iLeftJoin==0 ){
153980 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
153983 if( pIdx->pPartIdxWhere ){
153984 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
153987 testcase( pIdx->pPartIdxWhere );
153989 ** The OR-optimization doesn't work for the right hand table of
153991 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
153995 if( pLoop->wsFlags & WHERE_ONEROW ){
153996 pLevel->op = OP_Noop;
153998 pLevel->op = OP_Prev;
154000 pLevel->op = OP_Next;
154002 pLevel->p1 = iIdxCur;
154003 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
154004 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
154005 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
154007 assert( pLevel->p5==0 );
154013 if( pLoop->wsFlags & WHERE_MULTI_OR ){
154052 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
154057 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
154058 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
154060 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
154062 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
154070 Table *pTab = pTabItem->pTab;
154072 pTerm = pLoop->aLTerm[0];
154074 assert( pTerm->eOperator & WO_OR );
154075 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
154076 pOrWc = &pTerm->u.pOrInfo->wc;
154077 pLevel->op = OP_Return;
154078 pLevel->p1 = regReturn;
154084 if( pWInfo->nLevel>1 ){
154087 nNotReady = pWInfo->nLevel - iLevel - 1;
154089 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
154091 pOrTab->nAlloc = (u8)(nNotReady + 1);
154092 pOrTab->nSrc = pOrTab->nAlloc;
154093 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
154094 origSrc = pWInfo->pTabList->a;
154096 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
154099 pOrTab = pWInfo->pTabList;
154110 ** correct response for the end-of-loop code (the OP_Return) is to
154114 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
154116 regRowset = ++pParse->nMem;
154120 regRowset = pParse->nTab++;
154121 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
154124 regRowid = ++pParse->nMem;
154134 ** the "interesting" terms of z - terms that did not originate in the
154142 ** 2022-02-04: Do not push down slices of a row-value comparison.
154144 ** the initialization of the right-hand operand of the vector comparison
154148 ** 2022-03-03: Do not push down expressions that involve subqueries.
154149 ** The subquery might get coded as a subroutine. Any table-references
154150 ** in the subquery might be resolved to index-references for the index on
154153 ** index-references will not work. tag-20220303a
154156 if( pWC->nTerm>1 ){
154158 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
154159 Expr *pExpr = pWC->a[iTerm].pExpr;
154160 if( &pWC->a[iTerm] == pTerm ) continue;
154161 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
154162 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
154163 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
154164 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
154167 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
154168 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
154176 ** prevents sqlite3PExpr() from applying the AND short-circuit
154184 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
154186 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
154187 for(ii=0; ii<pOrWc->nTerm; ii++){
154188 WhereTerm *pOrTerm = &pOrWc->a[ii];
154189 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
154190 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
154191 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
154198 if( db->mallocFailed ){
154203 pAndExpr->pLeft = pOrExpr;
154208 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
154211 assert( pSubWInfo || pParse->nErr );
154215 pParse, pOrTab, &pSubWInfo->a[0], 0
154217 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
154219 /* This is the sub-WHERE clause body. First skip over
154220 ** duplicate rows from prior sub-WHERE clauses, and record the
154222 ** row will be skipped in subsequent sub-WHERE clauses.
154224 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
154225 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
154227 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
154233 int nPk = pPk->nKeyCol;
154240 int iCol = pPk->aiColumn[iPk];
154252 ** the temp table. And if iSet is -1, assume that there is no
154275 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
154278 /* The pSubWInfo->untestedTerms flag means that this OR term
154283 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
154285 /* If all of the OR-connected terms are optimized using the same
154291 ** uses an index, and this is either the first OR-connected term
154297 pSubLoop = pSubWInfo->a[0].pWLoop;
154298 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
154299 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
154300 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
154301 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
154303 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
154304 pCov = pSubLoop->u.btree.pIndex;
154309 pWInfo->bDeferredSeek = 1;
154320 assert( pLevel->pWLoop==pLoop );
154321 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
154322 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
154323 pLevel->u.pCoveringIdx = pCov;
154324 if( pCov ) pLevel->iIdxCur = iCovCur;
154326 pAndExpr->pLeft = 0;
154330 sqlite3VdbeGoto(v, pLevel->addrBrk);
154335 ** loop. The byte-code formatter will use that P2 value as a hint to
154337 ** See tag-20220407a in vdbe.c and shell.c */
154338 assert( pLevel->op==OP_Return );
154339 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
154341 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
154353 if( pTabItem->fg.isRecursive ){
154355 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
154356 pLevel->op = OP_Noop;
154359 pLevel->op = aStep[bRev];
154360 pLevel->p1 = iCur;
154361 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
154364 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
154369 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
154381 ** sub-queries.
154389 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
154392 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154393 testcase( pTerm->wtFlags & TERM_CODED );
154394 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
154395 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
154396 testcase( pWInfo->untestedTerms==0
154397 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
154398 pWInfo->untestedTerms = 1;
154401 pE = pTerm->pExpr;
154403 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
154406 ** join processing. tag-20220513a */
154408 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
154412 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
154413 if( m & pLevel->notReady ){
154419 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
154423 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
154428 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
154437 u32 x = pLevel->iLikeRepCntr;
154448 pWC->nTerm-j, pTerm, iLoop));
154452 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
154457 pTerm->wtFlags |= TERM_CODED;
154470 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
154473 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
154474 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
154475 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
154476 if( pTerm->leftCursor!=iCur ) continue;
154477 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
154478 pE = pTerm->pExpr;
154482 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
154486 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
154487 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154488 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
154491 if( pAlt->wtFlags & (TERM_CODED) ) continue;
154492 if( (pAlt->eOperator & WO_IN)
154493 && ExprUseXSelect(pAlt->pExpr)
154494 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
154498 testcase( pAlt->eOperator & WO_EQ );
154499 testcase( pAlt->eOperator & WO_IS );
154500 testcase( pAlt->eOperator & WO_IN );
154502 sEAlt = *pAlt->pExpr;
154503 sEAlt.pLeft = pE->pLeft;
154505 pAlt->wtFlags |= TERM_CODED;
154511 if( pLevel->pRJ ){
154516 WhereRightJoin *pRJ = pLevel->pRJ;
154518 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
154523 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
154526 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
154531 nPk = pPk->nKeyCol;
154534 int iCol = pPk->aiColumn[iPk];
154538 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
154540 VdbeComment((v, "match against %s", pTab->zName));
154542 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
154543 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
154552 if( pLevel->iLeftJoin ){
154553 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
154554 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
154556 if( pLevel->pRJ==0 ){
154561 if( pLevel->pRJ ){
154564 ** be in-line with the rest of the code. But at the end, a separate
154568 WhereRightJoin *pRJ = pLevel->pRJ;
154569 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
154570 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
154571 assert( pParse->withinRJSubrtn < 255 );
154572 pParse->withinRJSubrtn++;
154577 ** appropriate WHERE clause constraint checks. tag-20220513a.
154580 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
154581 testcase( pTerm->wtFlags & TERM_VIRTUAL );
154582 testcase( pTerm->wtFlags & TERM_CODED );
154583 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
154584 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
154585 assert( pWInfo->untestedTerms );
154588 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
154589 assert( pTerm->pExpr );
154590 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
154591 pTerm->wtFlags |= TERM_CODED;
154597 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
154603 iLevel, (u64)pLevel->notReady);
154606 return pLevel->notReady;
154610 ** Generate the code for the loop that finds all non-matched terms
154618 Parse *pParse = pWInfo->pParse;
154619 Vdbe *v = pParse->pVdbe;
154620 WhereRightJoin *pRJ = pLevel->pRJ;
154622 WhereClause *pWC = &pWInfo->sWC;
154624 WhereLoop *pLoop = pLevel->pWLoop;
154625 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
154630 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
154631 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
154632 pRJ->regReturn);
154635 mAll |= pWInfo->a[k].pWLoop->maskSelf;
154636 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
154637 iIdxCur = pWInfo->a[k].iIdxCur;
154642 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
154643 mAll |= pLoop->maskSelf;
154644 for(k=0; k<pWC->nTerm; k++){
154645 WhereTerm *pTerm = &pWC->a[k];
154646 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
154647 && pTerm->eOperator!=WO_ROWVAL
154651 if( pTerm->prereqAll & ~mAll ) continue;
154652 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
154654 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
154661 assert( pParse->withinRJSubrtn < 100 );
154662 pParse->withinRJSubrtn++;
154666 int iCur = pLevel->iTabCur;
154667 int r = ++pParse->nMem;
154671 Table *pTab = pTabItem->pTab;
154673 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
154678 nPk = pPk->nKeyCol;
154679 pParse->nMem += nPk - 1;
154681 int iCol = pPk->aiColumn[iPk];
154685 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
154687 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
154690 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
154693 sqlite3ExprDelete(pParse->db, pSubWhere);
154695 assert( pParse->withinRJSubrtn>0 );
154696 pParse->withinRJSubrtn--;
154702 ** 2015-06-08
154729 sqlite3WhereClauseClear(&p->wc);
154737 sqlite3WhereClauseClear(&p->wc);
154744 ** The index in pWC->a[] of the new WhereTerm is returned on success.
154747 ** the db->mallocFailed flag so that higher-level functions can detect it.
154749 ** This routine will increase the size of the pWC->a[] array as necessary.
154758 ** the pWC->a[] array.
154764 if( pWC->nTerm>=pWC->nSlot ){
154765 WhereTerm *pOld = pWC->a;
154766 sqlite3 *db = pWC->pWInfo->pParse->db;
154767 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
154768 if( pWC->a==0 ){
154772 pWC->a = pOld;
154775 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
154776 pWC->nSlot = pWC->nSlot*2;
154778 pTerm = &pWC->a[idx = pWC->nTerm++];
154779 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
154781 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
154783 pTerm->truthProb = 1;
154785 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
154786 pTerm->wtFlags = wtFlags;
154787 pTerm->pWC = pWC;
154788 pTerm->iParent = -1;
154789 memset(&pTerm->eOperator, 0,
154790 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
154812 if( pExpr->pLeft->op==TK_VECTOR
154813 || pExpr->pRight->op==TK_VECTOR
154814 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
154815 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
154817 pExpr->flags ^= EP_Commuted;
154819 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
154820 if( pExpr->op>=TK_GT ){
154825 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
154826 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
154844 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
154845 c = (u16)(WO_EQ<<(op-TK_EQ));
154883 int cnt; /* Number of non-wildcard prefix characters */
154885 sqlite3 *db = pParse->db; /* Database connection */
154897 pList = pExpr->x.pList;
154898 pLeft = pList->a[1].pExpr;
154900 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
154901 op = pRight->op;
154902 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
154903 Vdbe *pReprepare = pParse->pReprepare;
154904 int iCol = pRight->iColumn;
154909 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
154910 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
154913 z = (u8*)pRight->u.zToken;
154925 ** with a wildcard and if (2) the non-wildcard prefix does not end with
154932 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
154944 zNew = pPrefix->u.zToken;
154960 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
154961 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
154962 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
154963 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
154964 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
154966 if( pLeft->op!=TK_COLUMN
154969 && ALWAYS(pLeft->y.pTab)
154970 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
154976 if( iTo==1 && zNew[0]=='-' ){
154979 zNew[iTo-1]++;
154981 zNew[iTo-1]--;
154996 Vdbe *v = pParse->pVdbe;
154997 sqlite3VdbeSetVarmask(v, pRight->iColumn);
154999 if( *pisComplete && pRight->u.zToken[1] ){
155008 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
155030 ** ----------------------- ---------------------------------
155058 if( pExpr->op==TK_FUNCTION ){
155073 pList = pExpr->x.pList;
155074 if( pList==0 || pList->nExpr!=2 ){
155078 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
155080 ** the left-hand side operand in their in-fix form.
155085 pCol = pList->a[1].pExpr;
155086 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
155090 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
155092 *ppRight = pList->a[0].pExpr;
155105 ** Historically, xFindFunction expected to see lower-case function
155109 pCol = pList->a[0].pExpr;
155110 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
155111 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
155117 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
155119 assert( pVtab->pModule!=0 );
155121 pMod = (sqlite3_module *)pVtab->pModule;
155122 if( pMod->xFindFunction!=0 ){
155123 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
155126 *ppRight = pList->a[1].pExpr;
155132 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
155134 Expr *pLeft = pExpr->pLeft;
155135 Expr *pRight = pExpr->pRight;
155136 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
155140 assert( pRight==0 || pRight->op!=TK_COLUMN
155141 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
155148 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
155149 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
155150 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
155163 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
155164 pDerived->w.iJoin = pBase->w.iJoin;
155172 pWC->a[iChild].iParent = iParent;
155173 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
155174 pWC->a[iParent].nChild++;
155178 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
155183 if( pTerm->eOperator!=WO_AND ){
155186 if( N<pTerm->u.pAndInfo->wc.nTerm ){
155187 return &pTerm->u.pAndInfo->wc.a[N];
155194 ** two subterms are in disjunction - they are OR-ed together.
155204 ** x<y OR x=y --> x<=y
155205 ** x=y OR x=y --> x=y
155206 ** x<=y OR x<y --> x<=y
155210 ** x<y OR x>y --> x!=y
155218 u16 eOp = pOne->eOperator | pTwo->eOperator;
155224 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
155225 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
155226 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
155229 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
155230 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
155231 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
155232 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
155234 if( (eOp & (eOp-1))!=0 ){
155242 db = pWC->pWInfo->pParse->db;
155243 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
155245 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
155246 pNew->op = op;
155253 ** Analyze a term that consists of two or more OR-connected
155266 ** The term being analyzed must have two or more of OR-connected subterms.
155267 ** A single subterm might be a set of AND-connected sub-subterms.
155305 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
155342 int idxTerm /* Index of the OR-term to be analyzed */
155344 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
155345 Parse *pParse = pWInfo->pParse; /* Parser context */
155346 sqlite3 *db = pParse->db; /* Database connection */
155347 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
155348 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
155351 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
155361 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
155362 assert( pExpr->op==TK_OR );
155363 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
155365 pTerm->wtFlags |= TERM_ORINFO;
155366 pOrWc = &pOrInfo->wc;
155367 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
155371 if( db->mallocFailed ) return;
155372 assert( pOrWc->nTerm>=2 );
155379 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
155380 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
155382 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
155390 pOrTerm->u.pAndInfo = pAndInfo;
155391 pOrTerm->wtFlags |= TERM_ANDINFO;
155392 pOrTerm->eOperator = WO_AND;
155393 pOrTerm->leftCursor = -1;
155394 pAndWC = &pAndInfo->wc;
155395 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
155396 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
155397 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
155399 pAndWC->pOuter = pWC;
155400 if( !db->mallocFailed ){
155401 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
155402 assert( pAndTerm->pExpr );
155403 if( allowedOp(pAndTerm->pExpr->op)
155404 || pAndTerm->eOperator==WO_AUX
155406 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
155412 }else if( pOrTerm->wtFlags & TERM_COPIED ){
155417 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
155418 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
155419 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
155420 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
155423 if( (pOrTerm->eOperator & WO_EQ)==0 ){
155435 pOrInfo->indexable = indexable;
155436 pTerm->eOperator = WO_OR;
155437 pTerm->leftCursor = -1;
155439 pWC->hasOr = 1;
155442 /* For a two-way OR, attempt to implementation case 2.
155444 if( indexable && pOrWc->nTerm==2 ){
155447 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
155450 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
155461 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
155464 ** something other than == on a column in the single table. The 1-bit
155468 ** sure the same column is used on all terms. The 2-bit case is when
155479 int iColumn = -1; /* Column index on lhs of IN operator */
155480 int iCursor = -1; /* Table cursor common to all terms */
155491 pOrTerm = pOrWc->a;
155492 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
155493 assert( pOrTerm->eOperator & WO_EQ );
155494 pOrTerm->wtFlags &= ~TERM_OK;
155495 if( pOrTerm->leftCursor==iCursor ){
155496 /* This is the 2-bit case and we are on the second iteration and
155501 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
155502 pOrTerm->leftCursor))==0 ){
155507 testcase( pOrTerm->wtFlags & TERM_COPIED );
155508 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
155509 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
155512 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
155513 iColumn = pOrTerm->u.x.leftColumn;
155514 iCursor = pOrTerm->leftCursor;
155515 pLeft = pOrTerm->pExpr->pLeft;
155523 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
155531 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
155532 assert( pOrTerm->eOperator & WO_EQ );
155533 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
155534 if( pOrTerm->leftCursor!=iCursor ){
155535 pOrTerm->wtFlags &= ~TERM_OK;
155536 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
155537 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
155542 /* If the right-hand side is also a column, then the affinities
155546 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
155547 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
155551 pOrTerm->wtFlags |= TERM_OK;
155567 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
155568 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
155569 assert( pOrTerm->eOperator & WO_EQ );
155570 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
155571 assert( pOrTerm->leftCursor==iCursor );
155572 assert( pOrTerm->u.x.leftColumn==iColumn );
155573 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
155574 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
155575 pLeft = pOrTerm->pExpr->pLeft;
155584 pNew->x.pList = pList;
155588 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
155607 ** 5b. The overall collating sequence is BINARY
155616 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
155617 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
155619 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
155620 aff2 = sqlite3ExprAffinity(pExpr->pRight);
155628 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
155639 SrcList *pSrc = pS->pSrc;
155640 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
155641 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
155642 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
155643 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
155644 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
155647 for(i=0; i<pSrc->nSrc; i++){
155648 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
155649 if( pSrc->a[i].fg.isUsing==0 ){
155650 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
155652 if( pSrc->a[i].fg.isTabFunc ){
155653 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
155657 pS = pS->pPrior;
155668 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
155685 iCur = pFrom->a[i].iCursor;
155686 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
155687 if( pIdx->aColExpr==0 ) continue;
155688 for(i=0; i<pIdx->nKeyCol; i++){
155689 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
155690 assert( pIdx->bHasExpr );
155691 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
155713 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
155715 pExpr = pExpr->x.pList->a[0].pExpr;
155719 if( pExpr->op==TK_COLUMN ){
155720 aiCurCol[0] = pExpr->iTable;
155721 aiCurCol[1] = pExpr->iColumn;
155725 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
155753 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
155757 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
155763 int op; /* Top-level operator. pExpr->op */
155764 Parse *pParse = pWInfo->pParse; /* Parsing context */
155765 sqlite3 *db = pParse->db; /* Database connection */
155769 if( db->mallocFailed ){
155772 assert( pWC->nTerm > idxTerm );
155773 pTerm = &pWC->a[idxTerm];
155774 pMaskSet = &pWInfo->sMaskSet;
155775 pExpr = pTerm->pExpr;
155777 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
155778 pMaskSet->bVarSelect = 0;
155779 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
155780 op = pExpr->op;
155782 assert( pExpr->pRight==0 );
155785 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
155787 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
155789 prereqAll = prereqLeft | pTerm->prereqRight;
155791 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
155792 if( pExpr->pLeft==0
155794 || pExpr->x.pList!=0
155798 prereqAll = prereqLeft | pTerm->prereqRight;
155801 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
155812 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
155815 extraRight = x-1; /* ON clause terms may not be used with an index
155828 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
155835 pTerm->prereqAll = prereqAll;
155836 pTerm->leftCursor = -1;
155837 pTerm->iParent = -1;
155838 pTerm->eOperator = 0;
155841 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
155842 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
155843 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
155845 if( pTerm->u.x.iField>0 ){
155847 assert( pLeft->op==TK_VECTOR );
155849 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
155853 pTerm->leftCursor = aiCurCol[0];
155854 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155855 pTerm->u.x.leftColumn = aiCurCol[1];
155856 pTerm->eOperator = operatorMask(op) & opMask;
155858 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
155860 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
155865 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
155866 assert( pTerm->u.x.iField==0 );
155867 if( pTerm->leftCursor>=0 ){
155870 if( db->mallocFailed ){
155876 pNew = &pWC->a[idxNew];
155878 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
155879 pTerm = &pWC->a[idxTerm];
155880 pTerm->wtFlags |= TERM_COPIED;
155883 pTerm->eOperator |= WO_EQUIV;
155890 pNew->wtFlags |= exprCommute(pParse, pDup);
155891 pNew->leftCursor = aiCurCol[0];
155892 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155893 pNew->u.x.leftColumn = aiCurCol[1];
155895 pNew->prereqRight = prereqLeft | extraRight;
155896 pNew->prereqAll = prereqAll;
155897 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
155904 pExpr->op = TK_TRUEFALSE;
155905 pExpr->u.zToken = "false";
155907 pTerm->prereqAll = 0;
155908 pTerm->eOperator = 0;
155928 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
155933 pList = pExpr->x.pList;
155935 assert( pList->nExpr==2 );
155940 sqlite3ExprDup(db, pExpr->pLeft, 0),
155941 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
155946 pTerm = &pWC->a[idxTerm];
155956 else if( pExpr->op==TK_OR ){
155957 assert( pWC->op==TK_AND );
155959 pTerm = &pWC->a[idxTerm];
155968 else if( pExpr->op==TK_NOTNULL ){
155969 if( pExpr->pLeft->op==TK_COLUMN
155970 && pExpr->pLeft->iColumn>=0
155974 Expr *pLeft = pExpr->pLeft;
155985 pNewTerm = &pWC->a[idxNew];
155986 pNewTerm->prereqRight = 0;
155987 pNewTerm->leftCursor = pLeft->iTable;
155988 pNewTerm->u.x.leftColumn = pLeft->iColumn;
155989 pNewTerm->eOperator = WO_GT;
155991 pTerm = &pWC->a[idxTerm];
155992 pTerm->wtFlags |= TERM_COPIED;
155993 pNewTerm->prereqAll = pTerm->prereqAll;
156009 ** for LIKE) then the lower-bound is made all uppercase and the upper-
156013 else if( pExpr->op==TK_FUNCTION
156014 && pWC->op==TK_AND
156018 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
156027 pLeft = pExpr->x.pList->a[1].pExpr;
156033 /* Convert the lower bound to upper-case and the upper bound to
156034 ** lower-case (upper-case is less than lower-case in ASCII) so that
156037 if( noCase && !pParse->db->mallocFailed ){
156040 pTerm->wtFlags |= TERM_LIKE;
156041 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
156042 pStr1->u.zToken[i] = sqlite3Toupper(c);
156043 pStr2->u.zToken[i] = sqlite3Tolower(c);
156047 if( !db->mallocFailed ){
156049 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
156058 if( c=='A'-1 ) isComplete = 0;
156080 pTerm = &pWC->a[idxTerm];
156088 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
156089 ** new terms for each component comparison - "a = ?" and "b = ?". The
156094 ** is not a sub-select.
156096 ** tag-20220128a
156098 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
156099 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
156100 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
156101 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
156102 || (pExpr->pRight->flags & EP_xIsSelect)==0)
156103 && pWC->op==TK_AND
156109 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
156110 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
156112 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
156117 pTerm = &pWC->a[idxTerm];
156118 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
156119 pTerm->eOperator = WO_ROWVAL;
156122 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
156131 else if( pExpr->op==TK_IN
156132 && pTerm->u.x.iField==0
156133 && pExpr->pLeft->op==TK_VECTOR
156135 && pExpr->x.pSelect->pPrior==0
156137 && pExpr->x.pSelect->pWin==0
156139 && pWC->op==TK_AND
156142 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
156145 pWC->a[idxNew].u.x.iField = i+1;
156161 else if( pWC->op==TK_AND ){
156164 while( res-- > 0 ){
156177 pNewExpr->w.iJoin = pExpr->w.iJoin;
156181 pNewTerm = &pWC->a[idxNew];
156182 pNewTerm->prereqRight = prereqExpr;
156183 pNewTerm->leftCursor = pLeft->iTable;
156184 pNewTerm->u.x.leftColumn = pLeft->iColumn;
156185 pNewTerm->eOperator = WO_AUX;
156186 pNewTerm->eMatchOp = eOp2;
156188 pTerm = &pWC->a[idxTerm];
156189 pTerm->wtFlags |= TERM_COPIED;
156190 pNewTerm->prereqAll = pTerm->prereqAll;
156200 testcase( pTerm!=&pWC->a[idxTerm] );
156201 pTerm = &pWC->a[idxTerm];
156202 pTerm->prereqRight |= extraRight;
156229 pWC->op = op;
156232 if( pE2->op!=op ){
156235 sqlite3WhereSplit(pWC, pE2->pLeft, op);
156236 sqlite3WhereSplit(pWC, pE2->pRight, op);
156243 ** where-clause passed as the first argument. The value for the term
156258 Parse *pParse = pWC->pWInfo->pParse;
156259 sqlite3 *db = pParse->db;
156267 pVal->u.iValue = iVal;
156272 pVal->iTable = iReg;
156279 pTerm = &pWC->a[idx];
156280 pTerm->leftCursor = iCsr;
156281 pTerm->eOperator = WO_AUX;
156282 pTerm->eMatchOp = eMatchOp;
156305 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
156306 if( p->pGroupBy==0
156307 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
156308 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
156310 ExprList *pOrderBy = p->pOrderBy;
156311 int iCsr = p->pSrc->a[0].iCursor;
156315 for(ii=0; ii<pWC->nTerm; ii++){
156316 if( pWC->a[ii].wtFlags & TERM_CODED ){
156318 ** other, subsequent terms. It can be ignored. See tag-20220128a */
156319 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
156320 assert( pWC->a[ii].eOperator==WO_ROWVAL );
156323 if( pWC->a[ii].leftCursor!=iCsr ) return;
156328 for(ii=0; ii<pOrderBy->nExpr; ii++){
156329 Expr *pExpr = pOrderBy->a[ii].pExpr;
156330 if( pExpr->op!=TK_COLUMN ) return;
156331 if( pExpr->iTable!=iCsr ) return;
156332 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
156336 /* All conditions are met. Add the terms to the where-clause object. */
156337 assert( p->pLimit->op==TK_LIMIT );
156338 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
156340 if( p->iOffset>0 ){
156341 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
156354 pWC->pWInfo = pWInfo;
156355 pWC->hasOr = 0;
156356 pWC->pOuter = 0;
156357 pWC->nTerm = 0;
156358 pWC->nBase = 0;
156359 pWC->nSlot = ArraySize(pWC->aStatic);
156360 pWC->a = pWC->aStatic;
156369 sqlite3 *db = pWC->pWInfo->pParse->db;
156370 assert( pWC->nTerm>=pWC->nBase );
156371 if( pWC->nTerm>0 ){
156372 WhereTerm *a = pWC->a;
156373 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
156376 /* Verify that every term past pWC->nBase is virtual */
156377 for(i=pWC->nBase; i<pWC->nTerm; i++){
156378 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
156382 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
156383 if( a->wtFlags & TERM_DYNAMIC ){
156384 sqlite3ExprDelete(db, a->pExpr);
156386 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
156387 if( a->wtFlags & TERM_ORINFO ){
156388 assert( (a->wtFlags & TERM_ANDINFO)==0 );
156389 whereOrInfoDelete(db, a->u.pOrInfo);
156391 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
156392 whereAndInfoDelete(db, a->u.pAndInfo);
156407 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
156412 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
156417 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
156423 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
156430 ** "no-inline" function to avoid the stack push overhead in the
156438 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
156439 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
156440 if( p->pRight ){
156441 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
156442 assert( p->x.pList==0 );
156444 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
156445 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
156446 }else if( p->x.pList ){
156447 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
156450 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
156451 assert( p->y.pWin!=0 );
156452 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
156453 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
156454 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
156460 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
156461 return sqlite3WhereGetMask(pMaskSet, p->iTable);
156463 assert( p->op!=TK_IF_NULL_ROW );
156475 for(i=0; i<pList->nExpr; i++){
156476 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
156496 for(i=pWC->nTerm-1; i>=0; i--){
156502 ** For table-valued-functions, transform the function arguments into
156518 if( pItem->fg.isTabFunc==0 ) return;
156519 pTab = pItem->pTab;
156521 pArgs = pItem->u1.pFuncArg;
156523 for(j=k=0; j<pArgs->nExpr; j++){
156526 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
156527 if( k>=pTab->nCol ){
156528 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
156529 pTab->zName, j);
156532 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
156534 pColRef->iTable = pItem->iCursor;
156535 pColRef->iColumn = k++;
156537 pColRef->y.pTab = pTab;
156538 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
156540 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
156542 if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ) ){
156547 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
156603 return pWInfo->nRowOut;
156611 return pWInfo->eDistinct;
156623 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
156627 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
156629 ** inner-most loop did not fit within the sorter, then we can skip all
156632 ** loop - the loop immediately outside the inner-most.
156639 ** be the continuation for the second-inner-most loop. If the ORDER BY
156641 ** be the continuation for the inner-most loop.
156644 ** inner-most loop, in the sense that a correct answer will result.
156651 if( !pWInfo->bOrderedInnerLoop ){
156653 ** continuation of the inner-most loop. */
156654 return pWInfo->iContinue;
156656 pInner = &pWInfo->a[pWInfo->nLevel-1];
156657 assert( pInner->addrNxt!=0 );
156658 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
156663 ** the aggregate-step call to min() or max(), check to see if any
156675 if( !pWInfo->bOrderedInnerLoop ) return;
156676 if( pWInfo->nOBSat==0 ) return;
156677 for(i=pWInfo->nLevel-1; i>=0; i--){
156678 pInner = &pWInfo->a[i];
156679 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
156680 sqlite3VdbeGoto(v, pInner->addrNxt);
156684 sqlite3VdbeGoto(v, pWInfo->iBreak);
156692 assert( pWInfo->iContinue!=0 );
156693 return pWInfo->iContinue;
156701 return pWInfo->iBreak;
156708 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
156715 ** Either value may be -1, indicating that cursor is not used.
156718 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
156722 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
156724 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
156726 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
156730 return pWInfo->eOnePass;
156738 return pWInfo->bDeferredSeek;
156745 pDest->n = pSrc->n;
156746 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
156759 LogEst rRun, /* Run-cost of the new entry */
156764 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
156765 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
156768 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
156772 if( pSet->n<N_OR_COST ){
156773 p = &pSet->a[pSet->n++];
156774 p->nOut = nOut;
156776 p = pSet->a;
156777 for(i=1; i<pSet->n; i++){
156778 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
156780 if( p->rRun<=rRun ) return 0;
156783 p->prereq = prereq;
156784 p->rRun = rRun;
156785 if( p->nOut>nOut ) p->nOut = nOut;
156795 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
156796 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
156797 assert( iCursor>=-1 );
156798 if( pMaskSet->ix[0]==iCursor ){
156801 for(i=1; i<pMaskSet->n; i++){
156802 if( pMaskSet->ix[i]==iCursor ){
156813 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
156815 pBlock->pNext = pWInfo->pMemToFree;
156816 pBlock->sz = nByte;
156817 pWInfo->pMemToFree = pBlock;
156826 pOldBlk--;
156827 assert( pOldBlk->sz<nByte );
156828 memcpy(pNew, pOld, pOldBlk->sz);
156838 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
156842 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
156843 pMaskSet->ix[pMaskSet->n++] = iCursor;
156847 ** If the right-hand branch of the expression is a TK_COLUMN, then return
156848 ** a pointer to the right-hand branch. Otherwise, return NULL.
156851 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
156852 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
156865 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
156867 WhereClause *pWC; /* Shorthand for pScan->pWC */
156869 int k = pScan->k; /* Where to start scanning */
156871 assert( pScan->iEquiv<=pScan->nEquiv );
156872 pWC = pScan->pWC;
156874 iColumn = pScan->aiColumn[pScan->iEquiv-1];
156875 iCur = pScan->aiCur[pScan->iEquiv-1];
156879 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
156880 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
156881 if( pTerm->leftCursor==iCur
156882 && pTerm->u.x.leftColumn==iColumn
156884 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
156885 pScan->pIdxExpr,iCur)==0)
156886 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
156888 if( (pTerm->eOperator & WO_EQUIV)!=0
156889 && pScan->nEquiv<ArraySize(pScan->aiCur)
156890 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
156893 for(j=0; j<pScan->nEquiv; j++){
156894 if( pScan->aiCur[j]==pX->iTable
156895 && pScan->aiColumn[j]==pX->iColumn ){
156899 if( j==pScan->nEquiv ){
156900 pScan->aiCur[j] = pX->iTable;
156901 pScan->aiColumn[j] = pX->iColumn;
156902 pScan->nEquiv++;
156905 if( (pTerm->eOperator & pScan->opMask)!=0 ){
156907 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
156909 Parse *pParse = pWC->pWInfo->pParse;
156910 pX = pTerm->pExpr;
156911 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
156914 assert(pX->pLeft);
156916 if( pColl==0 ) pColl = pParse->db->pDfltColl;
156917 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
156921 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
156922 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
156923 && pX->op==TK_COLUMN
156924 && pX->iTable==pScan->aiCur[0]
156925 && pX->iColumn==pScan->aiColumn[0]
156927 testcase( pTerm->eOperator & WO_IS );
156930 pScan->pWC = pWC;
156931 pScan->k = k+1;
156935 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
156936 pTerm, pScan->nEquiv);
156937 for(ii=0; ii<pScan->nEquiv; ii++){
156939 pScan->aiCur[ii], pScan->aiColumn[ii]);
156948 pWC = pWC->pOuter;
156951 if( pScan->iEquiv>=pScan->nEquiv ) break;
156952 pWC = pScan->pOrigWC;
156954 pScan->iEquiv++;
156961 ** It is factored out into a separate tail-recursion subroutine so that
156962 ** the normal whereScanInit() routine, which is a high-runner, does not
156966 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
156997 pScan->pOrigWC = pWC;
156998 pScan->pWC = pWC;
156999 pScan->pIdxExpr = 0;
157000 pScan->idxaff = 0;
157001 pScan->zCollName = 0;
157002 pScan->opMask = opMask;
157003 pScan->k = 0;
157004 pScan->aiCur[0] = iCur;
157005 pScan->nEquiv = 1;
157006 pScan->iEquiv = 1;
157009 iColumn = pIdx->aiColumn[j];
157010 if( iColumn==pIdx->pTable->iPKey ){
157013 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
157014 pScan->zCollName = pIdx->azColl[j];
157016 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
157017 pScan->zCollName = pIdx->azColl[j];
157018 pScan->aiColumn[0] = XN_EXPR;
157024 pScan->aiColumn[0] = iColumn;
157035 ** Search for terms matching the iColumn-th column of pIdx
157036 ** rather than the iColumn-th column of table iCur.
157040 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
157047 ** then try for the one with no dependencies on <expr> - in other words where
157050 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
157068 if( (p->prereqRight & notReady)==0 ){
157069 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
157070 testcase( p->eOperator & WO_IS );
157081 ** This function searches pList for an entry that matches the iCol-th column
157084 ** If such an expression is found, its index in pList->a[] is returned. If
157085 ** no expression is found, -1 is returned.
157095 const char *zColl = pIdx->azColl[iCol];
157097 for(i=0; i<pList->nExpr; i++){
157098 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
157100 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
157101 && p->iColumn==pIdx->aiColumn[iCol]
157102 && p->iTable==iBase
157104 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
157105 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
157111 return -1;
157115 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
157120 assert( iCol>=0 && iCol<pIdx->nColumn );
157121 j = pIdx->aiColumn[iCol];
157123 return pIdx->pTable->aCol[j].notNull;
157124 }else if( j==(-1) ){
157127 assert( j==(-2) );
157134 ** Return true if the DISTINCT expression-list passed as the third argument
157138 ** DISTINCT list are collectively unique and individually non-null.
157151 /* If there is more than one table or sub-select in the FROM clause of
157154 if( pTabList->nSrc!=1 ) return 0;
157155 iBase = pTabList->a[0].iCursor;
157156 pTab = pTabList->a[0].pTab;
157159 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
157160 ** current SELECT is a correlated sub-query.
157162 for(i=0; i<pDistinct->nExpr; i++){
157163 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
157165 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
157166 if( p->iTable==iBase && p->iColumn<0 ) return 1;
157177 ** comparison and select-list expressions must match those of the index.
157182 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
157184 if( pIdx->pPartIdxWhere ) continue;
157185 for(i=0; i<pIdx->nKeyCol; i++){
157191 if( i==pIdx->nKeyCol ){
157205 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
157212 ** opcodes into OP_Copy when the table is being accessed via co-routine
157225 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
157227 Vdbe *v = pParse->pVdbe;
157230 if( pParse->db->mallocFailed ) return;
157232 if( pOp->p1!=iTabCur ) continue;
157233 if( pOp->opcode==OP_Column ){
157234 pOp->opcode = OP_Copy;
157235 pOp->p1 = pOp->p2 + iRegister;
157236 pOp->p2 = pOp->p3;
157237 pOp->p3 = 0;
157238 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
157239 }else if( pOp->opcode==OP_Rowid ){
157240 pOp->opcode = OP_Sequence;
157241 pOp->p1 = iAutoidxCur;
157244 pOp->opcode = OP_Null;
157245 pOp->p3 = 0;
157256 ** are no-ops.
157262 for(i=0; i<p->nConstraint; i++){
157266 p->aConstraint[i].iColumn,
157267 p->aConstraint[i].iTermOffset,
157268 p->aConstraint[i].op,
157269 p->aConstraint[i].usable,
157272 for(i=0; i<p->nOrderBy; i++){
157275 p->aOrderBy[i].iColumn,
157276 p->aOrderBy[i].desc);
157282 for(i=0; i<p->nConstraint; i++){
157285 p->aConstraintUsage[i].argvIndex,
157286 p->aConstraintUsage[i].omit);
157288 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
157289 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
157290 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
157291 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
157292 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
157310 ** not-NULL condition on the left table of the RIGHT JOIN.
157316 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
157317 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
157318 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
157319 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
157320 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
157321 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
157322 || pTerm->pExpr->w.iJoin != pSrc->iCursor
157326 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
157327 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
157348 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
157349 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
157350 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
157351 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
157356 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
157357 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157358 if( pTerm->u.x.leftColumn<0 ) return 0;
157359 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
157360 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
157361 testcase( pTerm->pExpr->op==TK_IS );
157382 WhereTerm *pWCEnd; /* End of pWC->a[] */
157391 int mxBitCol; /* Maximum column in pSrc->colUsed */
157406 v = pParse->pVdbe;
157413 pTable = pSrc->pTab;
157414 pWCEnd = &pWC->a[pWC->nTerm];
157415 pLoop = pLevel->pWLoop;
157417 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
157418 Expr *pExpr = pTerm->pExpr;
157422 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
157426 sqlite3ExprDup(pParse->db, pExpr, 0));
157431 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157432 iCol = pTerm->u.x.leftColumn;
157433 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
157435 testcase( iCol==BMS-1 );
157438 "automatic index on %s(%s)", pTable->zName,
157439 pTable->aCol[iCol].zCnName);
157443 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
157446 pLoop->aLTerm[nKeyCol++] = pTerm;
157451 assert( nKeyCol>0 || pParse->db->mallocFailed );
157452 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
157453 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
157464 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
157465 mxBitCol = MIN(BMS-1,pTable->nCol);
157466 testcase( pTable->nCol==BMS-1 );
157467 testcase( pTable->nCol==BMS-2 );
157471 if( pSrc->colUsed & MASKBIT(BMS-1) ){
157472 nKeyCol += pTable->nCol - BMS + 1;
157476 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
157478 pLoop->u.btree.pIndex = pIdx;
157479 pIdx->zName = "auto-index";
157480 pIdx->pTable = pTable;
157483 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
157487 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157488 iCol = pTerm->u.x.leftColumn;
157489 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
157490 testcase( iCol==BMS-1 );
157493 Expr *pX = pTerm->pExpr;
157495 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
157497 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
157498 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
157503 assert( (u32)n==pLoop->u.btree.nEq );
157509 pIdx->aiColumn[n] = i;
157510 pIdx->azColl[n] = sqlite3StrBINARY;
157514 if( pSrc->colUsed & MASKBIT(BMS-1) ){
157515 for(i=BMS-1; i<pTable->nCol; i++){
157516 pIdx->aiColumn[n] = i;
157517 pIdx->azColl[n] = sqlite3StrBINARY;
157522 pIdx->aiColumn[n] = XN_ROWID;
157523 pIdx->azColl[n] = sqlite3StrBINARY;
157526 assert( pLevel->iIdxCur>=0 );
157527 pLevel->iIdxCur = pParse->nTab++;
157528 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
157530 VdbeComment((v, "for %s", pTable->zName));
157531 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){
157532 pLevel->regFilter = ++pParse->nMem;
157533 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
157537 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
157538 if( pTabItem->fg.viaCoroutine ){
157539 int regYield = pTabItem->regReturn;
157541 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
157544 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
157546 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
157551 pLoop->wsFlags |= WHERE_PARTIALIDX;
157555 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
157557 if( pLevel->regFilter ){
157558 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
157559 regBase, pLoop->u.btree.nEq);
157561 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
157564 if( pTabItem->fg.viaCoroutine ){
157566 testcase( pParse->db->mallocFailed );
157567 assert( pLevel->iIdxCur>0 );
157568 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
157569 pTabItem->regResult, pLevel->iIdxCur);
157571 pTabItem->fg.viaCoroutine = 0;
157573 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
157583 sqlite3ExprDelete(pParse->db, pPartial);
157600 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
157609 int iLevel, /* Index in pWInfo->a[] that is pLevel */
157618 Parse *pParse = pWInfo->pParse; /* Parsing context */
157619 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
157620 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
157625 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
157634 iCur = pLevel->iTabCur;
157635 pLevel->regFilter = ++pParse->nMem;
157638 ** to zero-filled blob of at least 80K bits, but maybe more if the
157640 ** measure the size of the table at run-time using OP_Count with
157645 pItem = &pWInfo->pTabList->a[pLevel->iFrom];
157647 pTab = pItem->pTab;
157649 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
157655 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
157658 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
157659 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
157660 Expr *pExpr = pTerm->pExpr;
157661 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
157664 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
157667 if( pLoop->wsFlags & WHERE_IPK ){
157670 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
157673 Index *pIdx = pLoop->u.btree.pIndex;
157674 int n = pLoop->u.btree.nEq;
157678 int iCol = pIdx->aiColumn[jj];
157679 assert( pIdx->pTable==pItem->pTab );
157680 sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
157682 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
157686 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
157689 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
157690 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
157691 while( ++iLevel < pWInfo->nLevel ){
157693 pLevel = &pWInfo->a[iLevel];
157694 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
157695 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
157696 pLoop = pLevel->pWLoop;
157698 if( pLoop->prereq & notReady ) continue;
157699 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
157702 /* This is a candidate for bloom-filter pull-down (early evaluation).
157709 }while( iLevel < pWInfo->nLevel );
157729 Parse *pParse = pWInfo->pParse;
157740 ExprList *pOrderBy = pWInfo->pOrderBy;
157743 pTab = pSrc->pTab;
157751 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157752 pTerm->wtFlags &= ~TERM_OK;
157753 if( pTerm->leftCursor != pSrc->iCursor ) continue;
157754 if( pTerm->prereqRight & mUnusable ) continue;
157755 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
157756 testcase( pTerm->eOperator & WO_IN );
157757 testcase( pTerm->eOperator & WO_ISNULL );
157758 testcase( pTerm->eOperator & WO_IS );
157759 testcase( pTerm->eOperator & WO_ALL );
157760 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
157761 if( pTerm->wtFlags & TERM_VNULL ) continue;
157763 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
157764 assert( pTerm->u.x.leftColumn>=XN_ROWID );
157765 assert( pTerm->u.x.leftColumn<pTab->nCol );
157766 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
157772 pTerm->wtFlags |= TERM_OK;
157781 int n = pOrderBy->nExpr;
157783 Expr *pExpr = pOrderBy->a[i].pExpr;
157792 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
157794 /* First case - a direct column references without a COLLATE operator */
157795 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
157796 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
157800 /* 2nd case - a column reference with a COLLATE operator. Only match
157802 if( pExpr->op==TK_COLLATE
157803 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
157804 && pE2->iTable==pSrc->iCursor
157808 assert( pExpr->u.zToken!=0 );
157809 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
157810 pExpr->iColumn = pE2->iColumn;
157811 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
157812 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
157814 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
157822 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
157823 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
157824 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
157832 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
157841 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
157844 pIdxInfo->aConstraint = pIdxCons;
157845 pIdxInfo->aOrderBy = pIdxOrderBy;
157846 pIdxInfo->aConstraintUsage = pUsage;
157847 pHidden->pWC = pWC;
157848 pHidden->pParse = pParse;
157849 pHidden->eDistinct = eDistinct;
157850 pHidden->mIn = 0;
157851 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157853 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
157854 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
157856 op = pTerm->eOperator & WO_ALL;
157858 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
157859 pHidden->mIn |= SMASKBIT32(j);
157864 pIdxCons[j].op = pTerm->eMatchOp;
157881 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
157884 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
157896 pIdxInfo->nConstraint = j;
157898 Expr *pExpr = pOrderBy->a[i].pExpr;
157900 assert( pExpr->op==TK_COLUMN
157901 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
157902 && pExpr->iColumn==pExpr->pLeft->iColumn) );
157903 pIdxOrderBy[j].iColumn = pExpr->iColumn;
157904 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
157907 pIdxInfo->nOrderBy = j;
157922 assert( pHidden->pParse!=0 );
157923 assert( pHidden->pParse->db==db );
157924 for(i=0; i<pIdxInfo->nConstraint; i++){
157925 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
157926 pHidden->aRhs[i] = 0;
157944 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
157948 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
157952 pParse->db->nSchemaLock++;
157953 rc = pVtab->pModule->xBestIndex(pVtab, p);
157954 pParse->db->nSchemaLock--;
157959 sqlite3OomFault(pParse->db);
157960 }else if( !pVtab->zErrMsg ){
157963 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
157966 sqlite3_free(pVtab->zErrMsg);
157967 pVtab->zErrMsg = 0;
157982 ** into the aSample[] array - it is an index into a virtual set of samples
157993 IndexSample *aSample = pIdx->aSample;
158007 assert( pIdx->nSample>0 );
158008 assert( pRec->nField>0 );
158054 nField = MIN(pRec->nField, pIdx->nSample);
158056 iSample = pIdx->nSample * nField;
158068 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
158074 pRec->nField = n;
158077 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
158080 iLower = aSample[iSamp].anLt[n-1];
158082 res = -1;
158085 iCol = n-1;
158094 if( pParse->db->mallocFailed==0 ){
158097 assert( i<pIdx->nSample );
158098 assert( iCol==nField-1 );
158099 pRec->nField = nField;
158101 || pParse->db->mallocFailed
158104 /* Unless i==pIdx->nSample, indicating that pRec is larger than
158107 assert( i<=pIdx->nSample && i>=0 );
158108 pRec->nField = iCol+1;
158109 assert( i==pIdx->nSample
158111 || pParse->db->mallocFailed );
158116 ** If (i>0), then pRec must also be greater than sample (i-1). */
158118 pRec->nField = iCol;
158120 || pParse->db->mallocFailed );
158123 pRec->nField = nField;
158124 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
158125 || pParse->db->mallocFailed );
158133 assert( iCol==nField-1 );
158138 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
158141 if( i>=pIdx->nSample ){
158142 iUpper = pIdx->nRowEst0;
158150 iGap = iUpper - iLower;
158158 aStat[1] = pIdx->aAvgEq[nField-1];
158161 /* Restore the pRec->nField value before returning. */
158162 pRec->nField = nField;
158181 if( pTerm->truthProb<=0 ){
158182 nRet += pTerm->truthProb;
158183 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
158184 nRet -= 20; assert( 20==sqlite3LogEst(4) );
158196 assert( iCol>=0 && iCol<pIdx->nColumn );
158197 if( !pIdx->zColAff ){
158200 assert( pIdx->zColAff[iCol]!=0 );
158201 return pIdx->zColAff[iCol];
158209 ** range-scan on a skip-scan index. For example:
158214 ** Value pLoop->nOut is currently set to the estimated number of rows
158225 ** N is the total number of samples, the pLoop->nOut value is adjusted
158228 ** nOut = nOut * ( min(U - L, 1) / N )
158249 Index *p = pLoop->u.btree.pIndex;
158250 int nEq = pLoop->u.btree.nEq;
158251 sqlite3 *db = pParse->db;
158252 int nLower = -1;
158253 int nUpper = p->nSample+1;
158262 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
158264 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
158268 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
158269 nUpper = p2 ? 0 : p->nSample;
158275 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
158276 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
158286 nDiff = (nUpper - nLower);
158295 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
158296 pLoop->nOut -= nAdjust;
158298 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
158299 nLower, nUpper, nAdjust*-1, pLoop->nOut));
158329 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
158337 ** left-most column of the index). Or, if the query is:
158362 int nOut = pLoop->nOut;
158366 Index *p = pLoop->u.btree.pIndex;
158367 int nEq = pLoop->u.btree.nEq;
158369 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
158370 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
158372 if( nEq==pBuilder->nRecValid ){
158373 UnpackedRecord *pRec = pBuilder->pRec;
158375 int nBtm = pLoop->u.btree.nBtm;
158376 int nTop = pLoop->u.btree.nTop;
158381 ** key-prefix formed by the nEq values matched against the nEq left-most
158395 ** The number of rows between the two bounds is then just iUpper-iLower.
158399 int iLwrIdx = -2; /* aSample[] for the lower bound */
158400 int iUprIdx = -1; /* aSample[] for the upper bound */
158403 testcase( pRec->nField!=pBuilder->nRecValid );
158404 pRec->nField = pBuilder->nRecValid;
158409 iUpper = p->nRowEst0;
158411 /* Note: this call could be optimized away - since the same values must
158418 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
158419 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
158420 assert( p->aSortOrder!=0 );
158421 if( p->aSortOrder[nEq] ){
158430 Expr *pExpr = pLower->pExpr->pRight;
158437 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
158439 nOut--;
158447 Expr *pExpr = pUpper->pExpr->pRight;
158454 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
158456 nOut--;
158461 pBuilder->pRec = pRec;
158464 nNew = sqlite3LogEst(iUpper - iLower);
158469 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
158490 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
158495 ** has an application-defined likelihood(), assume the range is
158496 ** reduced by an additional 75%. This means that, by default, an open-ended
158500 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
158501 nNew -= 20;
158504 nOut -= (pLower!=0) + (pUpper!=0);
158508 if( pLoop->nOut>nOut ){
158510 pLoop->nOut, nOut));
158513 pLoop->nOut = (LogEst)nOut;
158521 ** the histogram data. This only works when x is the left-most
158528 ** non-zero.
158541 Index *p = pBuilder->pNew->u.btree.pIndex;
158542 int nEq = pBuilder->pNew->u.btree.nEq;
158543 UnpackedRecord *pRec = pBuilder->pRec;
158549 assert( nEq<=p->nColumn );
158550 assert( p->aSample!=0 );
158551 assert( p->nSample>0 );
158552 assert( pBuilder->nRecValid<nEq );
158556 if( pBuilder->nRecValid<(nEq-1) ){
158562 if( nEq>=p->nColumn ){
158567 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
158568 pBuilder->pRec = pRec;
158571 pBuilder->nRecValid = nEq;
158575 p->zName, nEq-1, (int)a[1]));
158585 ** an IN constraint where the right-hand side of the IN operator
158592 ** non-zero.
158605 Index *p = pBuilder->pNew->u.btree.pIndex;
158606 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
158607 int nRecValid = pBuilder->nRecValid;
158613 assert( p->aSample!=0 );
158614 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
158616 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
158618 pBuilder->nRecValid = nRecValid;
158626 assert( pBuilder->nRecValid==nRecValid );
158638 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
158643 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
158644 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
158645 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
158646 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
158647 if( pTerm->eOperator & WO_SINGLE ){
158648 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
158650 pTerm->leftCursor, pTerm->u.x.leftColumn);
158651 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
158653 pTerm->u.pOrInfo->indexable);
158655 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
158658 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
158659 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
158663 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
158664 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
158666 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
158667 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
158669 if( pTerm->iParent>=0 ){
158670 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
158673 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
158684 for(i=0; i<pWC->nTerm; i++){
158685 sqlite3WhereTermPrint(&pWC->a[i], i);
158695 WhereInfo *pWInfo = pWC->pWInfo;
158696 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
158697 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
158698 Table *pTab = pItem->pTab;
158699 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
158700 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
158701 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
158703 pItem->zAlias ? pItem->zAlias : pTab->zName);
158704 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
158706 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
158708 int i = sqlite3Strlen30(zName) - 1;
158709 while( zName[i]!='_' ) i--;
158712 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
158718 if( p->u.vtab.idxStr ){
158720 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
158722 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
158724 sqlite3DebugPrintf(" %-19s", z);
158727 if( p->wsFlags & WHERE_SKIPSCAN ){
158728 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
158730 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
158732 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
158733 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
158735 for(i=0; i<p->nLTerm; i++){
158736 sqlite3WhereTermPrint(p->aLTerm[i], i);
158747 p->aLTerm = p->aLTermSpace;
158748 p->nLTerm = 0;
158749 p->nLSlot = ArraySize(p->aLTermSpace);
158750 p->wsFlags = 0;
158757 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
158758 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
158759 sqlite3_free(p->u.vtab.idxStr);
158760 p->u.vtab.needFree = 0;
158761 p->u.vtab.idxStr = 0;
158762 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
158763 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
158764 sqlite3DbFreeNN(db, p->u.btree.pIndex);
158765 p->u.btree.pIndex = 0;
158775 if( p->aLTerm!=p->aLTermSpace ){
158776 sqlite3DbFreeNN(db, p->aLTerm);
158777 p->aLTerm = p->aLTermSpace;
158778 p->nLSlot = ArraySize(p->aLTermSpace);
158781 p->nLTerm = 0;
158782 p->wsFlags = 0;
158786 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
158790 if( p->nLSlot>=n ) return SQLITE_OK;
158792 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
158794 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
158795 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
158796 p->aLTerm = paNew;
158797 p->nLSlot = n;
158806 if( pFrom->nLTerm > pTo->nLSlot
158807 && whereLoopResize(db, pTo, pFrom->nLTerm)
158813 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
158814 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
158815 pFrom->u.vtab.needFree = 0;
158816 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
158817 pFrom->u.btree.pIndex = 0;
158837 sqlite3WhereClauseClear(&pWInfo->sWC);
158838 while( pWInfo->pLoops ){
158839 WhereLoop *p = pWInfo->pLoops;
158840 pWInfo->pLoops = p->pNextLoop;
158843 while( pWInfo->pMemToFree ){
158844 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
158845 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
158846 pWInfo->pMemToFree = pNext;
158865 ** was added because if X uses skip-scan less than Y it still might
158868 ** than a non-covering index even if it is a proper subset.
158875 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
158878 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
158879 if( pY->nSkip > pX->nSkip ) return 0;
158880 for(i=pX->nLTerm-1; i>=0; i--){
158881 if( pX->aLTerm[i]==0 ) continue;
158882 for(j=pY->nLTerm-1; j>=0; j--){
158883 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
158887 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
158888 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
158909 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
158910 for(; p; p=p->pNextLoop){
158911 if( p->iTab!=pTemplate->iTab ) continue;
158912 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
158917 pTemplate->rRun, pTemplate->nOut,
158918 MIN(p->rRun, pTemplate->rRun),
158919 MIN(p->nOut - 1, pTemplate->nOut)));
158920 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
158921 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
158926 pTemplate->rRun, pTemplate->nOut,
158927 MAX(p->rRun, pTemplate->rRun),
158928 MAX(p->nOut + 1, pTemplate->nOut)));
158929 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
158930 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
158954 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
158955 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
158964 assert( p->rSetup==0 || pTemplate->rSetup==0
158965 || p->rSetup==pTemplate->rSetup );
158969 ** rSetup. Call this SETUP-INVARIANT */
158970 assert( p->rSetup>=pTemplate->rSetup );
158972 /* Any loop using an appliation-defined index (or PRIMARY KEY or
158974 ** than an automatic index. Unless it is a skip-scan. */
158975 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
158976 && (pTemplate->nSkip)==0
158977 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
158978 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
158979 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
158989 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
158990 && p->rSetup<=pTemplate->rSetup /* (2a) */
158991 && p->rRun<=pTemplate->rRun /* (2b) */
158992 && p->nOut<=pTemplate->nOut /* (2c) */
159002 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
159003 && p->rRun>=pTemplate->rRun /* (2a) */
159004 && p->nOut>=pTemplate->nOut /* (2b) */
159006 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
159022 ** If pBuilder->pOrSet is not NULL then we care about only the
159024 ** information is gathered in the pBuilder->pOrSet object. This special
159027 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
159039 WhereInfo *pWInfo = pBuilder->pWInfo;
159040 sqlite3 *db = pWInfo->pParse->db;
159044 if( pBuilder->iPlanLimit==0 ){
159046 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
159049 pBuilder->iPlanLimit--;
159051 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
159053 /* If pBuilder->pOrSet is defined, then only keep track of the costs
159056 if( pBuilder->pOrSet!=0 ){
159057 if( pTemplate->nLTerm ){
159059 u16 n = pBuilder->pOrSet->n;
159062 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
159063 pTemplate->nOut);
159066 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
159067 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159076 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
159084 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159100 sqlite3WhereLoopPrint(p, pBuilder->pWC);
159105 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
159113 p->pNextLoop = 0;
159118 WhereLoop **ppTail = &p->pNextLoop;
159125 *ppTail = pToDel->pNextLoop;
159129 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
159136 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
159137 Index *pIndex = p->u.btree.pIndex;
159138 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
159139 p->u.btree.pIndex = 0;
159158 ** TODO --> Perhaps this is something that could be improved by better
159162 ** value corresponds to -1 in LogEst notation, so this means decrement
159169 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
159170 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
159180 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
159182 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
159184 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
159185 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
159187 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
159188 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
159189 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
159190 for(j=pLoop->nLTerm-1; j>=0; j--){
159191 pX = pLoop->aLTerm[j];
159194 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
159197 if( pLoop->maskSelf==pTerm->prereqAll ){
159201 ** "self-culling".
159203 ** 2022-03-24: Self-culling only applies if either the extra terms
159204 ** are straight comparison operators that are non-true with NULL
159207 if( (pTerm->eOperator & 0x3f)!=0
159208 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
159211 pLoop->wsFlags |= WHERE_SELFCULL;
159214 if( pTerm->truthProb<=0 ){
159217 pLoop->nOut += pTerm->truthProb;
159221 pLoop->nOut--;
159222 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
159223 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
159225 Expr *pRight = pTerm->pExpr->pRight;
159227 testcase( pTerm->pExpr->op==TK_IS );
159228 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
159234 pTerm->wtFlags |= TERM_HEURTRUTH;
159241 if( pLoop->nOut > nRow-iReduce ){
159242 pLoop->nOut = nRow - iReduce;
159270 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
159273 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
159282 assert( ExprUseXList(pTerm->pExpr->pLeft) );
159283 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
159284 pRhs = pTerm->pExpr->pRight;
159286 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
159288 pRhs = pRhs->x.pList->a[i].pExpr;
159295 if( pLhs->op!=TK_COLUMN
159296 || pLhs->iTable!=iCur
159297 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
159298 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
159303 testcase( pLhs->iColumn==XN_ROWID );
159305 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
159310 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
159317 ** compiled with -DSQLITE_ENABLE_COSTMULT
159326 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
159329 ** When this function is called, pBuilder->pNew->nOut contains the
159334 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
159343 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
159344 Parse *pParse = pWInfo->pParse; /* Parsing context */
159345 sqlite3 *db = pParse->db; /* Database connection malloc context */
159350 Bitmask saved_prereq; /* Original value of pNew->prereq */
159351 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
159352 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
159353 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
159354 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
159355 u16 saved_nSkip; /* Original value of pNew->nSkip */
159356 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
159357 LogEst saved_nOut; /* Original value of pNew->nOut */
159363 pNew = pBuilder->pNew;
159364 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
159366 pProbe->pTable->zName,pProbe->zName,
159367 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
159369 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
159370 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
159371 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
159374 assert( pNew->u.btree.nBtm==0 );
159377 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
159379 assert( pNew->u.btree.nEq<pProbe->nColumn );
159380 assert( pNew->u.btree.nEq<pProbe->nKeyCol
159381 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
159383 saved_nEq = pNew->u.btree.nEq;
159384 saved_nBtm = pNew->u.btree.nBtm;
159385 saved_nTop = pNew->u.btree.nTop;
159386 saved_nSkip = pNew->nSkip;
159387 saved_nLTerm = pNew->nLTerm;
159388 saved_wsFlags = pNew->wsFlags;
159389 saved_prereq = pNew->prereq;
159390 saved_nOut = pNew->nOut;
159391 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
159393 pNew->rSetup = 0;
159394 rSize = pProbe->aiRowLogEst[0];
159397 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
159402 int nRecValid = pBuilder->nRecValid;
159404 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
159409 if( pTerm->prereqRight & pNew->maskSelf ) continue;
159413 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
159415 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
159420 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
159421 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
159423 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
159425 pNew->wsFlags = saved_wsFlags;
159426 pNew->u.btree.nEq = saved_nEq;
159427 pNew->u.btree.nBtm = saved_nBtm;
159428 pNew->u.btree.nTop = saved_nTop;
159429 pNew->nLTerm = saved_nLTerm;
159430 if( pNew->nLTerm>=pNew->nLSlot
159431 && whereLoopResize(db, pNew, pNew->nLTerm+1)
159433 break; /* OOM while trying to enlarge the pNew->aLTerm array */
159435 pNew->aLTerm[pNew->nLTerm++] = pTerm;
159436 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
159439 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
159440 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
159441 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
159445 Expr *pExpr = pTerm->pExpr;
159456 for(i=0; i<pNew->nLTerm-1; i++){
159457 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
159459 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
159461 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
159463 if( pProbe->hasStat1 && rLogSize>=10 ){
159470 ** the left-most index column, M==N.
159480 ** with the index, as using an index has better worst-case behavior.
159485 M = pProbe->aiRowLogEst[saved_nEq];
159487 /* TUNING v----- 10 to bias toward indexed IN */
159488 x = M + logK + 10 - (nIn + rLogSize);
159497 " nInMul=%d) prefers skip-scan\n",
159499 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
159508 pNew->wsFlags |= WHERE_COLUMN_IN;
159510 int iCol = pProbe->aiColumn[saved_nEq];
159511 pNew->wsFlags |= WHERE_COLUMN_EQ;
159512 assert( saved_nEq==pNew->u.btree.nEq );
159514 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
159516 if( iCol==XN_ROWID || pProbe->uniqNotNull
159517 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
159519 pNew->wsFlags |= WHERE_ONEROW;
159521 pNew->wsFlags |= WHERE_UNQ_WANTED;
159524 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
159526 pNew->wsFlags |= WHERE_COLUMN_NULL;
159529 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
159534 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
159535 pNew->u.btree.nBtm = nVecLen;
159538 if( pTerm->wtFlags & TERM_LIKEOPT ){
159542 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
159543 assert( pTop->wtFlags & TERM_LIKEOPT );
159544 assert( pTop->eOperator==WO_LT );
159545 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
159546 pNew->aLTerm[pNew->nLTerm++] = pTop;
159547 pNew->wsFlags |= WHERE_TOP_LIMIT;
159548 pNew->u.btree.nTop = 1;
159554 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
159555 pNew->u.btree.nTop = nVecLen;
159557 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
159558 pNew->aLTerm[pNew->nLTerm-2] : 0;
159562 /* At this point pNew->nOut is set to the number of rows expected to
159566 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
159567 assert( pNew->nOut==saved_nOut );
159568 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
159573 int nEq = ++pNew->u.btree.nEq;
159576 assert( pNew->nOut==saved_nOut );
159577 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
159580 pNew->nOut += pTerm->truthProb;
159581 pNew->nOut -= nIn;
159586 && pProbe->nSample
159587 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
159588 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
159591 Expr *pExpr = pTerm->pExpr;
159596 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
159598 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
159603 pNew->nOut = sqlite3LogEst(nOut);
159607 ** See tag-202002240-1 */
159608 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
159617 pTerm->wtFlags |= TERM_HIGHTRUTH;
159618 if( pTerm->wtFlags & TERM_HEURTRUTH ){
159622 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
159625 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
159626 pNew->nOut -= nIn;
159632 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
159637 pNew->nOut += 10;
159644 ** it to pNew->rRun, which is currently set to the cost of the index
159645 ** seek only. Then, if this is a non-covering index, add the cost of
159647 assert( pSrc->pTab->szTabRow>0 );
159648 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
159649 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
159650 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
159651 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
159653 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
159655 nOutUnadjusted = pNew->nOut;
159656 pNew->rRun += nInMul + nIn;
159657 pNew->nOut += nInMul + nIn;
159658 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
159661 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
159662 pNew->nOut = saved_nOut;
159664 pNew->nOut = nOutUnadjusted;
159667 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
159668 && pNew->u.btree.nEq<pProbe->nColumn
159669 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
159670 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
159674 pNew->nOut = saved_nOut;
159676 pBuilder->nRecValid = nRecValid;
159679 pNew->prereq = saved_prereq;
159680 pNew->u.btree.nEq = saved_nEq;
159681 pNew->u.btree.nBtm = saved_nBtm;
159682 pNew->u.btree.nTop = saved_nTop;
159683 pNew->nSkip = saved_nSkip;
159684 pNew->wsFlags = saved_wsFlags;
159685 pNew->nOut = saved_nOut;
159686 pNew->nLTerm = saved_nLTerm;
159688 /* Consider using a skip-scan if there are no WHERE clause constraints
159689 ** available for the left-most terms of the index, and if the average
159690 ** number of repeats in the left-most terms is at least 18.
159700 && saved_nEq+1<pProbe->nKeyCol
159701 && saved_nEq==pNew->nLTerm
159702 && pProbe->noSkipScan==0
159703 && pProbe->hasStat1!=0
159705 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
159706 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
159709 pNew->u.btree.nEq++;
159710 pNew->nSkip++;
159711 pNew->aLTerm[pNew->nLTerm++] = 0;
159712 pNew->wsFlags |= WHERE_SKIPSCAN;
159713 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
159714 pNew->nOut -= nIter;
159715 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
159716 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
159719 pNew->nOut = saved_nOut;
159720 pNew->u.btree.nEq = saved_nEq;
159721 pNew->nSkip = saved_nSkip;
159722 pNew->wsFlags = saved_wsFlags;
159726 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
159747 if( pIndex->bUnordered ) return 0;
159748 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
159749 for(ii=0; ii<pOB->nExpr; ii++){
159750 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
159752 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
159753 if( pExpr->iColumn<0 ) return 1;
159754 for(jj=0; jj<pIndex->nKeyCol; jj++){
159755 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
159757 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
159758 for(jj=0; jj<pIndex->nKeyCol; jj++){
159759 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
159760 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
159783 pParse = pWC->pWInfo->pParse;
159784 while( pWhere->op==TK_AND ){
159785 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
159786 pWhere = pWhere->pRight;
159788 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
159789 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
159791 pExpr = pTerm->pExpr;
159792 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
159795 && (pTerm->wtFlags & TERM_VNULL)==0
159812 ** Information passed in is pWalk->u.pCovIdxCk. Call is pCk.
159814 ** If the Expr node references the table with cursor pCk->iTabCur, then
159815 ** make sure that column is covered by the index pCk->pIdx. We know that
159816 ** all columns less than 63 (really BMS-1) are covered, so we don't need
159819 ** If the index does not cover the column, then set pWalk->eCode to
159820 ** non-zero and return WRC_Abort to stop the search.
159830 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_AGG_COLUMN ) return WRC_Continue;
159831 if( pExpr->iColumn<(BMS-1) ) return WRC_Continue;
159832 if( pExpr->iTable!=pWalk->u.pCovIdxCk->iTabCur ) return WRC_Continue;
159833 pIdx = pWalk->u.pCovIdxCk->pIdx;
159834 aiColumn = pIdx->aiColumn;
159835 nColumn = pIdx->nColumn;
159837 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
159839 pWalk->eCode = 1;
159845 ** pIdx is an index that covers all of the low-number columns used by
159846 ** pWInfo->pSelect (columns from 0 through 62). But there are columns
159847 ** in pWInfo->pSelect beyond 62. This routine tries to answer the question
159850 ** Return 0 if pIdx is a covering index. Return non-zero if pIdx is
159854 ** This routine is an optimization. It is always safe to return non-zero.
159855 ** But returning zero when non-zero should have been returned can lead to
159866 if( pWInfo->pSelect==0 ){
159871 for(i=0; i<pIdx->nColumn; i++){
159872 if( pIdx->aiColumn[i]>=BMS-1 ) break;
159874 if( i>=pIdx->nColumn ){
159887 sqlite3WalkSelect(&w, pWInfo->pSelect);
159893 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
159894 ** a b-tree table, not a virtual table.
159896 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
159901 ** cost = nRow * 3.0 // full-table scan
159903 ** cost = nRow * (K+3.0) // scan of non-covering index
159910 ** the index b-tree:
159913 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
159917 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
159922 ** log(nRow) factor is omitted from a non-covering index scan in order to
159923 ** bias the scoring in favor of using an index, since the worst-case
159924 ** performance of using an index is far better than the worst-case performance
159935 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
159946 pNew = pBuilder->pNew;
159947 pWInfo = pBuilder->pWInfo;
159948 pTabList = pWInfo->pTabList;
159949 pSrc = pTabList->a + pNew->iTab;
159950 pTab = pSrc->pTab;
159951 pWC = pBuilder->pWC;
159952 assert( !IsVirtual(pSrc->pTab) );
159954 if( pSrc->fg.isIndexedBy ){
159955 assert( pSrc->fg.isCte==0 );
159957 pProbe = pSrc->u2.pIBIndex;
159959 pProbe = pTab->pIndex;
159973 sPk.szIdxRow = pTab->szTabRow;
159975 aiRowEstPk[0] = pTab->nRowLogEst;
159977 pFirst = pSrc->pTab->pIndex;
159978 if( pSrc->fg.notIndexed==0 ){
159985 rSize = pTab->nRowLogEst;
159989 if( !pBuilder->pOrSet /* Not part of an OR optimization */
159990 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
159991 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
159992 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
159993 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
159995 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
159996 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
159997 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
159999 /* Generate auto-index WhereLoops */
160002 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
160004 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
160005 if( pTerm->prereqRight & pNew->maskSelf ) continue;
160007 pNew->u.btree.nEq = 1;
160008 pNew->nSkip = 0;
160009 pNew->u.btree.pIndex = 0;
160010 pNew->nLTerm = 1;
160011 pNew->aLTerm[0] = pTerm;
160012 /* TUNING: One-time cost for computing the automatic index is
160015 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
160020 pNew->rSetup = rLogSize + rSize;
160021 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
160022 pNew->rSetup += 28;
160024 pNew->rSetup -= 10;
160026 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
160027 if( pNew->rSetup<0 ) pNew->rSetup = 0;
160032 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
160033 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
160034 pNew->wsFlags = WHERE_AUTO_INDEX;
160035 pNew->prereq = mPrereq | pTerm->prereqRight;
160045 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
160047 if( pProbe->pPartIdxWhere!=0
160048 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
160049 pProbe->pPartIdxWhere)
160051 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
160054 if( pProbe->bNoQuery ) continue;
160055 rSize = pProbe->aiRowLogEst[0];
160056 pNew->u.btree.nEq = 0;
160057 pNew->u.btree.nBtm = 0;
160058 pNew->u.btree.nTop = 0;
160059 pNew->nSkip = 0;
160060 pNew->nLTerm = 0;
160061 pNew->iSortIdx = 0;
160062 pNew->rSetup = 0;
160063 pNew->prereq = mPrereq;
160064 pNew->nOut = rSize;
160065 pNew->u.btree.pIndex = pProbe;
160066 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
160069 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
160070 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
160072 pNew->wsFlags = WHERE_IPK;
160075 pNew->iSortIdx = b ? iSortIdx : 0;
160078 ** since index lookups have better worst-case performance if our
160089 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
160091 pNew->rRun = rSize + 16;
160093 if( IsView(pTab) || (pTab->tabFlags & TF_Ephemeral)!=0 ){
160094 pNew->wsFlags |= WHERE_VIEWSCAN;
160096 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
160099 pNew->nOut = rSize;
160103 if( pProbe->isCovering ){
160104 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
160107 m = pSrc->colUsed & pProbe->colNotIdxed;
160109 m = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
160111 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
160117 || pProbe->pPartIdxWhere!=0
160118 || pSrc->fg.isIndexedBy
160120 && pProbe->bUnordered==0
160121 && (pProbe->szIdxRow<pTab->szTabRow)
160122 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
160124 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
160127 pNew->iSortIdx = b ? iSortIdx : 0;
160132 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
160134 /* If this is a non-covering index scan, add in the cost of
160141 int iCur = pSrc->iCursor;
160142 WhereClause *pWC2 = &pWInfo->sWC;
160143 for(ii=0; ii<pWC2->nTerm; ii++){
160144 WhereTerm *pTerm = &pWC2->a[ii];
160145 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
160150 if( pTerm->truthProb<=0 ){
160151 nLookup += pTerm->truthProb;
160153 nLookup--;
160154 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
160158 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
160160 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
160162 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
160163 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
160165 ** positioned to the correct row during the right-join no-match
160170 pNew->nOut = rSize;
160175 pBuilder->bldFlags1 = 0;
160177 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
160178 /* If a non-unique index is used, or if a prefix of the key for
160179 ** unique index is used (making the index functionally non-unique)
160182 pTab->tabFlags |= TF_StatsUsed;
160185 sqlite3Stat4ProbeFree(pBuilder->pRec);
160186 pBuilder->nRecValid = 0;
160187 pBuilder->pRec = 0;
160199 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
160200 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
160201 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
160206 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
160234 WhereClause *pWC = pBuilder->pWC;
160237 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
160241 WhereLoop *pNew = pBuilder->pNew;
160242 Parse *pParse = pBuilder->pWInfo->pParse;
160243 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
160244 int nConstraint = pIdxInfo->nConstraint;
160248 pNew->prereq = mPrereq;
160252 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
160254 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
160255 pIdxCons->usable = 0;
160256 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
160257 && (pTerm->eOperator & mExclude)==0
160260 pIdxCons->usable = 1;
160266 assert( pIdxInfo->needToFreeIdxStr==0 );
160267 pIdxInfo->idxStr = 0;
160268 pIdxInfo->idxNum = 0;
160269 pIdxInfo->orderByConsumed = 0;
160270 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
160271 pIdxInfo->estimatedRows = 25;
160272 pIdxInfo->idxFlags = 0;
160273 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
160274 pHidden->mHandleIn = 0;
160277 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
160284 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
160290 mxTerm = -1;
160291 assert( pNew->nLSlot>=nConstraint );
160292 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
160293 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
160294 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
160297 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
160299 int j = pIdxCons->iTermOffset;
160302 || j>=pWC->nTerm
160303 || pNew->aLTerm[iTerm]!=0
160304 || pIdxCons->usable==0
160306 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
160307 testcase( pIdxInfo->needToFreeIdxStr );
160310 testcase( iTerm==nConstraint-1 );
160312 testcase( j==pWC->nTerm-1 );
160313 pTerm = &pWC->a[j];
160314 pNew->prereq |= pTerm->prereqRight;
160315 assert( iTerm<pNew->nLSlot );
160316 pNew->aLTerm[iTerm] = pTerm;
160323 pNew->u.vtab.omitMask |= 1<<iTerm;
160327 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
160328 pNew->u.vtab.bOmitOffset = 1;
160331 if( SMASKBIT32(i) & pHidden->mHandleIn ){
160332 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
160333 }else if( (pTerm->eOperator & WO_IN)!=0 ){
160339 pIdxInfo->orderByConsumed = 0;
160340 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
160351 if( pIdxInfo->needToFreeIdxStr ){
160352 sqlite3_free(pIdxInfo->idxStr);
160353 pIdxInfo->idxStr = 0;
160354 pIdxInfo->needToFreeIdxStr = 0;
160362 pNew->nLTerm = mxTerm+1;
160364 if( pNew->aLTerm[i]==0 ){
160365 /* The non-zero argvIdx values must be contiguous. Raise an
160367 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
160368 testcase( pIdxInfo->needToFreeIdxStr );
160372 assert( pNew->nLTerm<=pNew->nLSlot );
160373 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
160374 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
160375 pIdxInfo->needToFreeIdxStr = 0;
160376 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
160377 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
160378 pIdxInfo->nOrderBy : 0);
160379 pNew->rSetup = 0;
160380 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
160381 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
160385 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
160386 pNew->wsFlags |= WHERE_ONEROW;
160388 pNew->wsFlags &= ~WHERE_ONEROW;
160391 if( pNew->u.vtab.needFree ){
160392 sqlite3_free(pNew->u.vtab.idxStr);
160393 pNew->u.vtab.needFree = 0;
160397 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
160420 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
160422 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
160423 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
160424 if( pX->pLeft ){
160425 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
160427 zRet = (pC ? pC->zName : sqlite3StrBINARY);
160440 if( m & pHidden->mIn ){
160442 pHidden->mHandleIn &= ~m;
160444 pHidden->mHandleIn |= m;
160455 ** on the right-hand-side of constraint iCons.
160465 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
160466 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
160468 if( pH->aRhs[iCons]==0 ){
160469 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
160471 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
160472 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
160476 pVal = pH->aRhs[iCons];
160480 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
160481 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
160492 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
160493 return pHidden->eDistinct;
160501 ** prepared is read-only, then just start read transactions on all
160505 ** This is used by the (built-in) sqlite_dbpage virtual table.
160509 Parse *pParse = pHidden->pParse;
160510 int nDb = pParse->db->nDb;
160515 if( pParse->writeMask ){
160525 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
160546 ** mUnusable should always be configured as "not-usable" for xBestIndex.
160567 pWInfo = pBuilder->pWInfo;
160568 pParse = pWInfo->pParse;
160569 pWC = pBuilder->pWC;
160570 pNew = pBuilder->pNew;
160571 pSrc = &pWInfo->pTabList->a[pNew->iTab];
160572 assert( IsVirtual(pSrc->pTab) );
160575 pNew->rSetup = 0;
160576 pNew->wsFlags = WHERE_VIRTUALTABLE;
160577 pNew->nLTerm = 0;
160578 pNew->u.vtab.needFree = 0;
160579 nConstraint = p->nConstraint;
160580 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
160581 freeIndexInfo(pParse->db, p);
160586 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
160603 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
160616 mBestNoIn = pNew->prereq & ~mPrereq;
160631 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
160642 if( pNew->prereq==mPrereq ){
160668 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
160669 freeIndexInfo(pParse->db, p);
160670 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
160684 WhereInfo *pWInfo = pBuilder->pWInfo;
160695 pWC = pBuilder->pWC;
160696 pWCEnd = pWC->a + pWC->nTerm;
160697 pNew = pBuilder->pNew;
160699 pItem = pWInfo->pTabList->a + pNew->iTab;
160700 iCur = pItem->iCursor;
160702 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
160703 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
160705 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
160706 if( (pTerm->eOperator & WO_OR)!=0
160707 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
160709 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
160710 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
160718 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
160719 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
160720 if( (pOrTerm->eOperator & WO_AND)!=0 ){
160721 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
160722 }else if( pOrTerm->leftCursor==iCur ){
160723 tempWC.pWInfo = pWC->pWInfo;
160735 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
160736 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
160742 if( IsVirtual(pItem->pTab) ){
160775 pNew->nLTerm = 1;
160776 pNew->aLTerm[0] = pTerm;
160777 pNew->wsFlags = WHERE_MULTI_OR;
160778 pNew->rSetup = 0;
160779 pNew->iSortIdx = 0;
160780 memset(&pNew->u, 0, sizeof(pNew->u));
160783 ** of all sub-scans required by the OR-scan. However, due to rounding
160784 ** errors, it may be that the cost of the OR-scan is equal to its
160785 ** most expensive sub-scan. Add the smallest possible penalty
160792 ** the planner may elect to "OR" together a full-table scan and an
160794 pNew->rRun = sSum.a[i].rRun + 1;
160795 pNew->nOut = sSum.a[i].nOut;
160796 pNew->prereq = sSum.a[i].prereq;
160799 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
160809 WhereInfo *pWInfo = pBuilder->pWInfo;
160813 SrcList *pTabList = pWInfo->pTabList;
160815 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
160816 sqlite3 *db = pWInfo->pParse->db;
160824 pNew = pBuilder->pNew;
160827 assert( pNew->nLTerm==0 );
160828 assert( pNew->wsFlags==0 );
160829 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
160830 assert( pNew->aLTerm!=0 );
160832 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
160833 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
160835 pNew->iTab = iTab;
160836 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
160837 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
160839 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
160847 ** prevent FROM-clause terms from moving from the right side of
160851 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
160853 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
160858 if( IsVirtual(pItem->pTab) ){
160861 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
160862 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
160871 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
160874 mPrior |= pNew->maskSelf;
160875 if( rc || db->mallocFailed ){
160905 ** pOrderBy terms must be matched in strict left-to-right order.
160912 u16 nLoop, /* Number of entries in pPath->aLoop[] */
160913 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
160919 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
160935 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
160938 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
160942 ** We say the WhereLoop is "one-row" if it generates no more than one
160943 ** row of output. A WhereLoop is one-row if all of the following are true:
160946 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
160947 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
160949 ** We say the WhereLoop is "order-distinct" if the set of columns from
160951 ** row of the WhereLoop. Every one-row WhereLoop is automatically
160952 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
160953 ** is not order-distinct. To be order-distinct is not quite the same as being
160955 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
160956 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
160960 ** automatically order-distinct.
160966 nOrderBy = pOrderBy->nExpr;
160967 testcase( nOrderBy==BMS-1 );
160968 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
160970 obDone = MASKBIT(nOrderBy)-1;
160978 if( iLoop>0 ) ready |= pLoop->maskSelf;
160980 pLoop = pPath->aLoop[iLoop];
160985 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
160986 if( pLoop->u.vtab.isOrdered
160993 pLoop->u.btree.nDistinctCol = 0;
160995 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
161004 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
161006 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
161007 if( pOBExpr->iTable!=iCur ) continue;
161008 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
161011 if( pTerm->eOperator==WO_IN ){
161017 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
161018 if( j>=pLoop->nLTerm ) continue;
161020 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
161021 Parse *pParse = pWInfo->pParse;
161022 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
161023 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
161025 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
161028 testcase( pTerm->pExpr->op==TK_IS );
161033 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
161034 if( pLoop->wsFlags & WHERE_IPK ){
161038 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
161041 nKeyCol = pIndex->nKeyCol;
161042 nColumn = pIndex->nColumn;
161043 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
161044 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
161045 || !HasRowid(pIndex->pTable));
161046 /* All relevant terms of the index must also be non-NULL in order
161049 ** made at tag-20210426-1 below */
161051 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
161062 assert( j>=pLoop->u.btree.nEq
161063 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
161065 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
161066 u16 eOp = pLoop->aLTerm[j]->eOperator;
161071 ** the loop need to be marked as not order-distinct because it can
161090 ** j<pLoop->u.btree.nEq constraint above. Any equality other
161093 Expr *pX = pLoop->aLTerm[j]->pExpr;
161094 for(i=j+1; i<pLoop->u.btree.nEq; i++){
161095 if( pLoop->aLTerm[i]->pExpr==pX ){
161096 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
161105 ** (revIdx) for the j-th column of the index.
161108 iColumn = pIndex->aiColumn[j];
161109 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
161110 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
161117 ** WhereLoop is not well-ordered. tag-20210426-1
161121 && j>=pLoop->u.btree.nEq
161122 && pIndex->pTable->aCol[iColumn].notNull==0
161131 /* Find the ORDER BY term that corresponds to the j-th column
161137 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
161143 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
161144 if( pOBExpr->iTable!=iCur ) continue;
161145 if( pOBExpr->iColumn!=iColumn ) continue;
161147 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
161153 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
161154 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
161157 pLoop->u.btree.nDistinctCol = j+1;
161167 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
161172 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
161177 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
161178 if( j==pLoop->u.btree.nEq ){
161179 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
161203 } /* end-if not one-row */
161207 orderDistinctMask |= pLoop->maskSelf;
161212 p = pOrderBy->a[i].pExpr;
161213 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
161220 } /* End the loop over all WhereLoops from outer-most down to inner-most */
161223 for(i=nOrderBy-1; i>0; i--){
161224 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
161229 return -1;
161236 ** BY clause - and so any order that groups rows as required satisfies the
161253 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
161254 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
161257 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
161258 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
161259 return pWInfo->sorted;
161267 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
161268 if( pLast ) zName[i++] = pLast->cId;
161290 ** Or, if the order-by clause has X terms but only the last Y
161291 ** terms are out of order, then block-sorting will reduce the
161301 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
161309 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
161310 nRow = pWInfo->iLimit;
161311 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
161314 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
161321 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
161323 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
161353 pParse = pWInfo->pParse;
161354 nLoop = pWInfo->nLevel;
161356 ** For 2-way joins, the 5 best paths are followed.
161359 assert( nLoop<=pWInfo->pTabList->nSrc );
161360 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
161364 ** by the overall query. Once this estimate has been obtained, the caller
161367 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
161370 nOrderBy = pWInfo->pOrderBy->nExpr;
161376 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
161382 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
161383 pFrom->aLoop = pX;
161388 ** is either zero - meaning it has not yet been initialized - or the
161403 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
161411 ** -1, indicating that the result set may or may not be ordered,
161413 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
161422 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
161428 Bitmask revMask; /* Mask of rev-order loops for (..) */
161430 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
161431 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
161432 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
161443 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
161444 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
161445 nOut = pFrom->nRow + pWLoop->nOut;
161446 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
161447 isOrdered = pFrom->isOrdered;
161451 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
161454 revMask = pFrom->revLoop;
161469 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
161470 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
161474 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
161477 /* TUNING: A full-scan of a VIEW or subquery in the outer loop
161479 if( iLoop==0 && (pWLoop->wsFlags & WHERE_VIEWSCAN)!=0 ){
161480 rCost += -10;
161481 nOut += -30;
161485 ** mxChoice best-so-far paths.
161487 ** First look for an existing path among best-so-far paths
161491 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
161492 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
161493 ** of legal values for isOrdered, -1..64.
161496 if( pTo->maskLoop==maskNew
161497 && ((pTo->isOrdered^isOrdered)&0x80)==0
161499 testcase( jj==nTo-1 );
161504 /* None of the existing best-so-far paths match the candidate. */
161509 ** paths currently in the best-so-far buffer. So discard
161513 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
161521 ** needs to be added to the set of best-so-far paths. */
161532 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
161538 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
161544 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
161546 if( pTo->rCost<rCost
161547 || (pTo->rCost==rCost
161548 && (pTo->nRow<nOut
161549 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
161556 "Skip %s cost=%-3d,%3d,%3d order=%c",
161559 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
161560 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
161561 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
161565 testcase( pTo->rCost==rCost );
161568 testcase( pTo->rCost==rCost+1 );
161574 "Update %s cost=%-3d,%3d,%3d order=%c",
161577 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
161578 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
161579 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
161584 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
161585 pTo->revLoop = revMask;
161586 pTo->nRow = nOut;
161587 pTo->rCost = rCost;
161588 pTo->rUnsorted = rUnsorted;
161589 pTo->isOrdered = isOrdered;
161590 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
161591 pTo->aLoop[iLoop] = pWLoop;
161597 if( pTo->rCost>mxCost
161598 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
161600 mxCost = pTo->rCost;
161601 mxUnsorted = pTo->rUnsorted;
161611 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
161613 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
161614 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
161615 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
161616 if( pTo->isOrdered>0 ){
161617 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
161634 sqlite3StackFreeNN(pParse->db, pSpace);
161641 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
161643 assert( pWInfo->nLevel==nLoop );
161646 WhereLevel *pLevel = pWInfo->a + iLoop;
161647 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
161648 pLevel->iFrom = pWLoop->iTab;
161649 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
161651 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
161652 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
161653 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
161657 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
161658 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
161659 if( rc==pWInfo->pResultSet->nExpr ){
161660 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
161663 pWInfo->bOrderedInnerLoop = 0;
161664 if( pWInfo->pOrderBy ){
161665 pWInfo->nOBSat = pFrom->isOrdered;
161666 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
161667 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
161668 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
161671 pWInfo->revMask = pFrom->revLoop;
161672 if( pWInfo->nOBSat<=0 ){
161673 pWInfo->nOBSat = 0;
161675 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
161680 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
161681 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
161684 if( rc==pWInfo->pOrderBy->nExpr ){
161685 pWInfo->bOrderedInnerLoop = 1;
161686 pWInfo->revMask = m;
161691 && pWInfo->nOBSat==1
161692 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
161694 pWInfo->bOrderedInnerLoop = 1;
161697 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
161698 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
161701 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
161702 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
161704 assert( pWInfo->sorted==0 );
161705 if( nOrder==pWInfo->pOrderBy->nExpr ){
161706 pWInfo->sorted = 1;
161707 pWInfo->revMask = revMask;
161713 pWInfo->nRowOut = pFrom->nRow;
161716 sqlite3StackFreeNN(pParse->db, pSpace);
161724 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
161727 ** Return non-zero on success, if this query can be handled by this
161728 ** no-frills query planner. Return zero if this query needs the
161729 ** general-purpose query planner.
161743 pWInfo = pBuilder->pWInfo;
161744 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
161745 assert( pWInfo->pTabList->nSrc>=1 );
161746 pItem = pWInfo->pTabList->a;
161747 pTab = pItem->pTab;
161749 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
161750 testcase( pItem->fg.isIndexedBy );
161751 testcase( pItem->fg.notIndexed );
161754 iCur = pItem->iCursor;
161755 pWC = &pWInfo->sWC;
161756 pLoop = pBuilder->pNew;
161757 pLoop->wsFlags = 0;
161758 pLoop->nSkip = 0;
161759 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
161760 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
161762 testcase( pTerm->eOperator & WO_IS );
161763 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
161764 pLoop->aLTerm[0] = pTerm;
161765 pLoop->nLTerm = 1;
161766 pLoop->u.btree.nEq = 1;
161768 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
161770 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
161772 assert( pLoop->aLTermSpace==pLoop->aLTerm );
161774 || pIdx->pPartIdxWhere!=0
161775 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
161777 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
161778 for(j=0; j<pIdx->nKeyCol; j++){
161780 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
161782 testcase( pTerm->eOperator & WO_IS );
161783 pLoop->aLTerm[j] = pTerm;
161785 if( j!=pIdx->nKeyCol ) continue;
161786 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
161787 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
161788 pLoop->wsFlags |= WHERE_IDX_ONLY;
161790 pLoop->nLTerm = j;
161791 pLoop->u.btree.nEq = j;
161792 pLoop->u.btree.pIndex = pIdx;
161794 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
161798 if( pLoop->wsFlags ){
161799 pLoop->nOut = (LogEst)1;
161800 pWInfo->a[0].pWLoop = pLoop;
161801 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
161802 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
161803 pWInfo->a[0].iTabCur = iCur;
161804 pWInfo->nRowOut = 1;
161805 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
161806 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
161807 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
161809 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
161811 pLoop->cId = '0';
161827 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
161828 pWalker->eCode = 0;
161835 ** Return true if the expression contains no non-deterministic SQL
161836 ** functions. Do not consider non-deterministic SQL functions that are
161837 ** part of sub-select statements.
161860 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
161861 p->cId = zLabel[i%(sizeof(zLabel)-1)];
161908 assert( pWInfo->nLevel>=2 );
161909 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
161913 assert( pWInfo->pResultSet!=0 );
161914 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
161916 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
161917 if( pWInfo->pOrderBy ){
161918 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
161920 for(i=pWInfo->nLevel-1; i>=1; i--){
161924 pLoop = pWInfo->a[i].pWLoop;
161925 pItem = &pWInfo->pTabList->a[pLoop->iTab];
161926 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
161927 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
161928 && (pLoop->wsFlags & WHERE_ONEROW)==0
161932 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
161933 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
161934 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
161935 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
161936 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
161937 || pTerm->pExpr->w.iJoin!=pItem->iCursor
161944 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
161945 notReady &= ~pLoop->maskSelf;
161946 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
161947 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
161948 pTerm->wtFlags |= TERM_CODED;
161951 if( i!=pWInfo->nLevel-1 ){
161952 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
161953 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
161955 pWInfo->nLevel--;
161956 assert( pWInfo->nLevel>0 );
161970 ** (3) Bloom-filter processing is not disabled. (Checked by the
161985 assert( pWInfo->nLevel>=2 );
161986 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
161987 nSearch = pWInfo->a[0].pWLoop->nOut;
161988 for(i=1; i<pWInfo->nLevel; i++){
161989 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
161991 if( (pLoop->wsFlags & reqFlags)==reqFlags
161992 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
161993 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
161995 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
161996 Table *pTab = pItem->pTab;
161997 pTab->tabFlags |= TF_StatsUsed;
161998 if( nSearch > pTab->nRowLogEst
161999 && (pTab->tabFlags & TF_HasStat1)!=0
162001 testcase( pItem->fg.jointype & JT_LEFT );
162002 pLoop->wsFlags |= WHERE_BLOOMFILTER;
162003 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
162005 "-> use Bloom-filter on loop %c because there are ~%.1e "
162007 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
162008 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
162011 nSearch += pLoop->nOut;
162017 ** free the Parse->pIdxExpr list when the Parse object is destroyed.
162021 while( pParse->pIdxExpr!=0 ){
162022 IndexedExpr *p = pParse->pIdxExpr;
162023 pParse->pIdxExpr = p->pIENext;
162024 sqlite3ExprDelete(db, p->pExpr);
162035 ** This routine adds IndexedExpr entries to the Parse->pIdxExpr field for
162041 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxExpr */
162042 Index *pIdx, /* The index-on-expression that contains the expressions */
162049 assert( pIdx->bHasExpr );
162050 pTab = pIdx->pTable;
162051 for(i=0; i<pIdx->nColumn; i++){
162053 int j = pIdx->aiColumn[i];
162056 pExpr = pIdx->aColExpr->a[i].pExpr;
162057 testcase( pTabItem->fg.jointype & JT_LEFT );
162058 testcase( pTabItem->fg.jointype & JT_RIGHT );
162059 testcase( pTabItem->fg.jointype & JT_LTORJ );
162060 bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
162061 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
162062 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
162068 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
162070 p->pIENext = pParse->pIdxExpr;
162071 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
162072 p->iDataCur = pTabItem->iCursor;
162073 p->iIdxCur = iIdxCur;
162074 p->iIdxCol = i;
162075 p->bMaybeNullRow = bMaybeNullRow;
162077 p->zIdxName = pIdx->zName;
162079 pParse->pIdxExpr = p;
162080 if( p->pIENext==0 ){
162105 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
162109 ** end |-- by sqlite3WhereEnd()
162116 ** scanning through all values on the right-hand side of the IN.
162119 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
162129 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
162139 ** inner loops (or around the "..." if the test occurs within the inner-
162188 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
162192 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
162209 db = pParse->db;
162213 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
162214 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
162219 testcase( pTabList->nSrc==BMS );
162220 if( pTabList->nSrc>BMS ){
162230 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
162235 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
162236 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
162239 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
162241 if( db->mallocFailed ){
162246 pWInfo->pParse = pParse;
162247 pWInfo->pTabList = pTabList;
162248 pWInfo->pOrderBy = pOrderBy;
162250 pWInfo->pWhere = pWhere;
162252 pWInfo->pResultSet = pResultSet;
162253 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
162254 pWInfo->nLevel = nTabList;
162255 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
162256 pWInfo->wctrlFlags = wctrlFlags;
162257 pWInfo->iLimit = iAuxArg;
162258 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
162259 pWInfo->pSelect = pSelect;
162260 memset(&pWInfo->nOBSat, 0,
162261 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
162262 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
162263 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
162264 pMaskSet = &pWInfo->sMaskSet;
162265 pMaskSet->n = 0;
162266 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
162268 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
162270 sWLB.pWC = &pWInfo->sWC;
162275 sWLB.pNew->cId = '*';
162281 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
162282 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
162287 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
162291 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
162297 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
162300 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
162304 ** Note that bitmasks are created for all pTabList->nSrc tables in
162306 ** equal to pTabList->nSrc but might be shortened to 1 if the
162311 createMask(pMaskSet, pTabList->a[ii].iCursor);
162312 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
162313 }while( (++ii)<pTabList->nSrc );
162317 for(ii=0; ii<pTabList->nSrc; ii++){
162318 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
162327 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
162328 if( pSelect && pSelect->pLimit ){
162329 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
162331 if( pParse->nErr ) goto whereBeginError;
162337 ** Do not do this if the expression contains non-deterministic functions
162338 ** that are not within a sub-select. This is not strictly required, but
162341 ** FROM ... WHERE random()>0; -- eval random() once per row
162342 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
162344 for(ii=0; ii<sWLB.pWC->nBase; ii++){
162345 WhereTerm *pT = &sWLB.pWC->a[ii];
162346 if( pT->wtFlags & TERM_VIRTUAL ) continue;
162347 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
162348 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
162349 pT->wtFlags |= TERM_CODED;
162358 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
162359 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
162361 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
162364 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
162365 pWInfo->pOrderBy = pResultSet;
162389 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
162409 while( pWInfo->pLoops ){
162410 WhereLoop *p = pWInfo->pLoops;
162411 pWInfo->pLoops = p->pNextLoop;
162421 if( db->mallocFailed ) goto whereBeginError;
162422 if( pWInfo->pOrderBy ){
162423 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
162424 if( db->mallocFailed ) goto whereBeginError;
162427 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
162428 pWInfo->revMask = ALLBITS;
162430 if( pParse->nErr ){
162433 assert( db->mallocFailed==0 );
162436 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
162437 if( pWInfo->nOBSat>0 ){
162438 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
162440 switch( pWInfo->eDistinct ){
162455 for(ii=0; ii<pWInfo->nLevel; ii++){
162456 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
162464 ** This query optimization is factored out into a separate "no-inline"
162467 ** some C-compiler optimizers from in-lining the
162469 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
162472 if( pWInfo->nLevel>=2
162478 nTabList = pWInfo->nLevel;
162485 if( pWInfo->nLevel>=2
162493 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
162498 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
162501 ** to use a one-pass algorithm, determine if this is appropriate.
162503 ** A one-pass approach can be used if the caller has requested one
162507 ** * the caller has indicated that a one-pass approach can be used
162516 ** use a one-pass approach, and this is not set accurately for scans
162519 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
162521 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
162523 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
162526 && !IsVirtual(pTabList->a[0].pTab)
162529 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
162530 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
162534 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
162542 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
162547 pTabItem = &pTabList->a[pLevel->iFrom];
162548 pTab = pTabItem->pTab;
162549 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
162550 pLoop = pLevel->pWLoop;
162551 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
162555 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
162557 int iCur = pTabItem->iCursor;
162563 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
162565 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
162568 if( pWInfo->eOnePass!=ONEPASS_OFF ){
162570 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
162572 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
162573 assert( pTabItem->iCursor==pLevel->iTabCur );
162574 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
162575 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
162576 if( pWInfo->eOnePass==ONEPASS_OFF
162577 && pTab->nCol<BMS
162578 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
162579 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
162584 Bitmask b = pTabItem->colUsed;
162587 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
162588 assert( n<=pTab->nCol );
162591 if( pLoop->u.btree.pIndex!=0 ){
162599 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
162600 (const u8*)&pTabItem->colUsed, P4_INT64);
162603 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
162605 if( pLoop->wsFlags & WHERE_INDEXED ){
162606 Index *pIx = pLoop->u.btree.pIndex;
162610 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
162614 /* This is one term of an OR-optimization using the PRIMARY KEY of a
162616 iIndexCur = pLevel->iTabCur;
162618 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
162619 Index *pJ = pTabItem->pTab->pIndex;
162624 pJ = pJ->pNext;
162627 pWInfo->aiCurOnePass[1] = iIndexCur;
162632 iIndexCur = pParse->nTab++;
162633 if( pIx->bHasExpr && OptimizationEnabled(db, SQLITE_IndexedExpr) ){
162637 pLevel->iIdxCur = iIndexCur;
162639 assert( pIx->pSchema==pTab->pSchema );
162642 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
162644 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
162645 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
162646 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
162647 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
162648 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
162649 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
162653 VdbeComment((v, "%s", pIx->zName));
162658 for(ii=0; ii<pIx->nColumn; ii++){
162659 jj = pIx->aiColumn[ii];
162662 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
162672 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
162673 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
162675 WhereRightJoin *pRJ = pLevel->pRJ;
162676 pRJ->iMatch = pParse->nTab++;
162677 pRJ->regBloom = ++pParse->nMem;
162678 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
162679 pRJ->regReturn = ++pParse->nMem;
162680 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
162681 assert( pTab==pTabItem->pTab );
162684 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
162685 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
162687 pInfo->aColl[0] = 0;
162688 pInfo->aSortFlags[0] = 0;
162693 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
162696 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
162700 pWInfo->nOBSat = 0;
162701 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
162704 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
162705 if( db->mallocFailed ) goto whereBeginError;
162715 if( pParse->nErr ) goto whereBeginError;
162716 pLevel = &pWInfo->a[ii];
162717 wsFlags = pLevel->pWLoop->wsFlags;
162718 pSrc = &pTabList->a[pLevel->iFrom];
162719 if( pSrc->fg.isMaterialized ){
162720 if( pSrc->fg.isCorrelated ){
162721 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
162724 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
162731 constructAutomaticIndex(pParse, &pWInfo->sWC,
162732 &pTabList->a[pLevel->iFrom], notReady, pLevel);
162737 if( db->mallocFailed ) goto whereBeginError;
162742 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
162744 pWInfo->iContinue = pLevel->addrCont;
162751 VdbeModuleComment((v, "Begin WHERE-core"));
162752 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
162758 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
162771 # define OpcodeRewriteTrace(D,K,P) /* no-op */
162779 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
162791 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
162792 if( pOp->p1!=iCur ) continue;
162793 if( pOp->opcode==OP_Close ) return 0;
162794 if( pOp->opcode==OP_OpenRead ) return 1;
162795 if( pOp->opcode==OP_OpenWrite ) return 1;
162796 if( pOp->opcode==OP_OpenDup ) return 1;
162797 if( pOp->opcode==OP_OpenAutoindex ) return 1;
162798 if( pOp->opcode==OP_OpenEphemeral ) return 1;
162809 Parse *pParse = pWInfo->pParse;
162810 Vdbe *v = pParse->pVdbe;
162814 SrcList *pTabList = pWInfo->pTabList;
162815 sqlite3 *db = pParse->db;
162821 VdbeModuleComment((v, "End WHERE-core"));
162822 for(i=pWInfo->nLevel-1; i>=0; i--){
162824 pLevel = &pWInfo->a[i];
162825 if( pLevel->pRJ ){
162828 WhereRightJoin *pRJ = pLevel->pRJ;
162829 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
162830 pLevel->addrCont = 0;
162831 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
162832 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
162836 pLoop = pLevel->pWLoop;
162837 if( pLevel->op!=OP_Noop ){
162842 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
162843 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
162844 && (pLoop->wsFlags & WHERE_INDEXED)!=0
162845 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
162846 && (n = pLoop->u.btree.nDistinctCol)>0
162847 && pIdx->aiRowLogEst[n]>=36
162849 int r1 = pParse->nMem+1;
162852 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
162854 pParse->nMem += n+1;
162855 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
162856 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
162859 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
162863 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
162864 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
162865 sqlite3VdbeChangeP5(v, pLevel->p5);
162867 VdbeCoverageIf(v, pLevel->op==OP_Next);
162868 VdbeCoverageIf(v, pLevel->op==OP_Prev);
162869 VdbeCoverageIf(v, pLevel->op==OP_VNext);
162870 if( pLevel->regBignull ){
162871 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
162872 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
162878 }else if( pLevel->addrCont ){
162879 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
162881 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
162884 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
162885 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
162886 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
162887 || pParse->db->mallocFailed );
162888 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
162889 if( pIn->eEndLoopOp!=OP_Noop ){
162890 if( pIn->nPrefix ){
162892 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
162893 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
162894 if( pLevel->iLeftJoin ){
162895 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
162900 ** return the null-row. So, if the cursor is not open yet,
162903 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
162908 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
162910 pIn->iBase, pIn->nPrefix);
162916 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
162919 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
162921 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
162922 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
162924 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
162927 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
162928 if( pLevel->pRJ ){
162929 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
162932 if( pLevel->addrSkip ){
162933 sqlite3VdbeGoto(v, pLevel->addrSkip);
162934 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
162935 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
162936 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
162939 if( pLevel->addrLikeRep ){
162940 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
162941 pLevel->addrLikeRep);
162945 if( pLevel->iLeftJoin ){
162946 int ws = pLoop->wsFlags;
162947 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
162950 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
162951 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
162954 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
162957 Index *pIx = pLevel->u.pCoveringIdx;
162958 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
162959 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
162962 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
162964 if( pLevel->op==OP_Return ){
162965 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
162967 sqlite3VdbeGoto(v, pLevel->addrFirst);
162971 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
162972 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
162975 assert( pWInfo->nLevel<=pTabList->nSrc );
162976 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
162980 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
162981 Table *pTab = pTabItem->pTab;
162983 pLoop = pLevel->pWLoop;
162989 if( pLevel->pRJ ){
162994 /* For a co-routine, change all OP_Column references to the table of
162995 ** the co-routine into OP_Copy of result contained in a register.
162998 if( pTabItem->fg.viaCoroutine ){
162999 testcase( pParse->db->mallocFailed );
163000 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
163001 pTabItem->regResult, 0);
163016 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
163017 pIdx = pLoop->u.btree.pIndex;
163018 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
163019 pIdx = pLevel->u.pCoveringIdx;
163022 && !db->mallocFailed
163024 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
163027 last = pWInfo->iEndWhere;
163029 if( pIdx->bHasExpr ){
163030 IndexedExpr *p = pParse->pIdxExpr;
163032 if( p->iIdxCur==pLevel->iIdxCur ){
163033 p->iDataCur = -1;
163034 p->iIdxCur = -1;
163036 p = p->pIENext;
163039 k = pLevel->addrBody + 1;
163041 if( db->flags & SQLITE_VdbeAddopTrace ){
163042 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
163045 pOp = sqlite3VdbeGetOp(v, k - 1);
163046 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
163047 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
163048 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
163051 pLastOp = pOp + (last - k);
163054 if( pOp->p1!=pLevel->iTabCur ){
163055 /* no-op */
163056 }else if( pOp->opcode==OP_Column
163058 || pOp->opcode==OP_Offset
163061 int x = pOp->p2;
163062 assert( pIdx->pTable==pTab );
163064 if( pOp->opcode==OP_Offset ){
163070 x = pPk->aiColumn[x];
163078 pOp->p2 = x;
163079 pOp->p1 = pLevel->iIdxCur;
163083 ** reference. Verify that this is harmless - that the
163087 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
163088 || cursorIsOpen(v,pOp->p1,k)
163089 || pOp->opcode==OP_Offset
163092 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
163093 || cursorIsOpen(v,pOp->p1,k)
163097 }else if( pOp->opcode==OP_Rowid ){
163098 pOp->p1 = pLevel->iIdxCur;
163099 pOp->opcode = OP_IdxRowid;
163101 }else if( pOp->opcode==OP_IfNullRow ){
163102 pOp->p1 = pLevel->iIdxCur;
163110 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
163118 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
163122 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
163124 pParse->withinRJSubrtn -= nRJ;
163173 ** the sub-query.
163178 ** select-list and ORDER BY expressions in the parent query are
163179 ** selected by the sub-query. For the purposes of the transformation,
163211 ** sub-query, which is always implemented as a co-routine. It then calls
163216 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
163217 ** When the sub-routine is invoked:
163219 ** * The results of all window-functions for the row are stored
163231 ** BUILT-IN WINDOW FUNCTIONS
163233 ** This implementation features the following built-in window functions:
163247 ** These are the same built-in window functions supported by Postgres.
163250 ** be implemented using an API, built-in window functions are much more
163253 ** As such, some built-in window functions use the same API as aggregate
163264 ** As well as some of the built-in window functions, aggregate window
163271 ** Implementation of built-in window function row_number(). Assumes that the
163302 ** Implementation of built-in window function dense_rank(). Assumes that
163314 if( p ) p->nStep = 1;
163322 if( p->nStep ){
163323 p->nValue++;
163324 p->nStep = 0;
163326 sqlite3_result_int64(pCtx, p->nValue);
163331 ** Implementation of built-in window function nth_value(). This
163332 ** implementation is used in "slow mode" only - when the EXCLUDE clause
163363 p->nStep++;
163364 if( iVal==p->nStep ){
163365 p->pValue = sqlite3_value_dup(apArg[0]);
163366 if( !p->pValue ){
163377 pCtx, "second argument to nth_value must be a positive integer", -1
163383 if( p && p->pValue ){
163384 sqlite3_result_value(pCtx, p->pValue);
163385 sqlite3_value_free(p->pValue);
163386 p->pValue = 0;
163399 if( p && p->pValue==0 ){
163400 p->pValue = sqlite3_value_dup(apArg[0]);
163401 if( !p->pValue ){
163411 if( p && p->pValue ){
163412 sqlite3_result_value(pCtx, p->pValue);
163413 sqlite3_value_free(p->pValue);
163414 p->pValue = 0;
163421 ** Implementation of built-in window function rank(). Assumes that
163434 p->nStep++;
163435 if( p->nValue==0 ){
163436 p->nValue = p->nStep;
163446 sqlite3_result_int64(pCtx, p->nValue);
163447 p->nValue = 0;
163452 ** Implementation of built-in window function percent_rank(). Assumes that
163467 p->nTotal++;
163479 p->nStep++;
163485 p->nValue = p->nStep;
163486 if( p->nTotal>1 ){
163487 double r = (double)p->nValue / (double)(p->nTotal-1);
163497 ** Implementation of built-in window function cume_dist(). Assumes that
163512 p->nTotal++;
163524 p->nStep++;
163530 double r = (double)(p->nStep) / (double)(p->nTotal);
163560 if( p->nTotal==0 ){
163561 p->nParam = sqlite3_value_int64(apArg[0]);
163562 if( p->nParam<=0 ){
163564 pCtx, "argument of ntile must be a positive integer", -1
163568 p->nTotal++;
163580 p->iRow++;
163585 if( p && p->nParam>0 ){
163586 int nSize = (p->nTotal / p->nParam);
163588 sqlite3_result_int64(pCtx, p->iRow+1);
163590 i64 nLarge = p->nTotal - p->nParam*nSize;
163592 i64 iRow = p->iRow;
163594 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
163599 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
163626 sqlite3_value_free(p->pVal);
163627 p->pVal = sqlite3_value_dup(apArg[0]);
163628 if( p->pVal==0 ){
163631 p->nVal++;
163645 p->nVal--;
163646 if( p->nVal==0 ){
163647 sqlite3_value_free(p->pVal);
163648 p->pVal = 0;
163655 if( p && p->pVal ){
163656 sqlite3_result_value(pCtx, p->pVal);
163662 if( p && p->pVal ){
163663 sqlite3_result_value(pCtx, p->pVal);
163664 sqlite3_value_free(p->pVal);
163665 p->pVal = 0;
163670 ** Static names for the built-in window function names. These static
163675 ** if( pFuncDef->zName==row_valueName ){ ... }
163690 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
163691 ** for built-in window functions that never call those interfaces.
163708 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
163719 ** no-op routines for their methods */
163737 ** Register those built-in window functions that are not also aggregates.
163762 for(p=pList; p; p=p->pNextWin){
163763 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
163784 ** * If the function is a built-in window function that requires the
163785 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
163794 if( pWin->zName && pWin->eFrmType==0 ){
163795 Window *p = windowFind(pParse, pList, pWin->zName);
163797 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
163798 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
163799 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
163800 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
163801 pWin->eStart = p->eStart;
163802 pWin->eEnd = p->eEnd;
163803 pWin->eFrmType = p->eFrmType;
163804 pWin->eExclude = p->eExclude;
163808 if( (pWin->eFrmType==TK_RANGE)
163809 && (pWin->pStart || pWin->pEnd)
163810 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
163816 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
163817 sqlite3 *db = pParse->db;
163818 if( pWin->pFilter ){
163840 if( pFunc->zName==aUp[i].zFunc ){
163841 sqlite3ExprDelete(db, pWin->pStart);
163842 sqlite3ExprDelete(db, pWin->pEnd);
163843 pWin->pEnd = pWin->pStart = 0;
163844 pWin->eFrmType = aUp[i].eFrmType;
163845 pWin->eStart = aUp[i].eStart;
163846 pWin->eEnd = aUp[i].eEnd;
163847 pWin->eExclude = 0;
163848 if( pWin->eStart==TK_FOLLOWING ){
163849 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
163856 pWin->pWFunc = pFunc;
163869 Select *pSubSelect; /* Current sub-select, if any */
163874 ** this function appends to the output expression-list and updates
163878 struct WindowRewrite *p = pWalker->u.pRewrite;
163879 Parse *pParse = pWalker->pParse;
163881 assert( p->pWin!=0 );
163883 /* If this function is being called from within a scalar sub-select
163887 ** to the scalar sub-select. */
163888 if( p->pSubSelect ){
163889 if( pExpr->op!=TK_COLUMN ){
163892 int nSrc = p->pSrc->nSrc;
163895 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
163901 switch( pExpr->op ){
163908 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
163909 if( pExpr->y.pWin==pWin ){
163910 assert( pWin->pOwner==pExpr );
163919 int iCol = -1;
163920 if( pParse->db->mallocFailed ) return WRC_Abort;
163921 if( p->pSub ){
163923 for(i=0; i<p->pSub->nExpr; i++){
163924 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
163931 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
163932 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
163933 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
163935 if( p->pSub ){
163936 int f = pExpr->flags & EP_Collate;
163939 sqlite3ExprDelete(pParse->db, pExpr);
163943 pExpr->op = TK_COLUMN;
163944 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
163945 pExpr->iTable = p->pWin->iEphCsr;
163946 pExpr->y.pTab = p->pTab;
163947 pExpr->flags = f;
163949 if( pParse->db->mallocFailed ) return WRC_Abort;
163953 default: /* no-op */
163960 struct WindowRewrite *p = pWalker->u.pRewrite;
163961 Select *pSave = p->pSubSelect;
163965 p->pSubSelect = pSelect;
163967 p->pSubSelect = pSave;
163974 ** Iterate through each expression in expression-list pEList. For each:
163981 ** Append the node to output expression-list (*ppSub). And replace it
163982 ** with a TK_COLUMN that reads the (N-1)th element of table
163983 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
163992 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
164017 ** Append a copy of each expression in expression-list pAppend to
164028 int nInit = pList ? pList->nExpr : 0;
164029 for(i=0; i<pAppend->nExpr; i++){
164030 sqlite3 *db = pParse->db;
164031 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
164032 if( db->mallocFailed ){
164041 pSub->op = TK_NULL;
164042 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
164043 pSub->u.zToken = 0;
164047 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
164056 ** then we have to increase the Expr->op2 values of those nodes
164062 if( pExpr->op==TK_AGG_FUNCTION
164063 && pExpr->op2>=pWalker->walkerDepth
164065 pExpr->op2++;
164071 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
164073 sqlite3ErrorMsg(pWalker->pParse,
164074 "misuse of aggregate: %s()", pExpr->u.zToken);
164081 ** any SQL window functions, this function is a no-op. Otherwise, it
164088 if( p->pWin
164089 && p->pPrior==0
164090 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
164094 sqlite3 *db = pParse->db;
164096 SrcList *pSrc = p->pSrc;
164097 Expr *pWhere = p->pWhere;
164098 ExprList *pGroupBy = p->pGroupBy;
164099 Expr *pHaving = p->pHaving;
164102 ExprList *pSublist = 0; /* Expression list for sub-query */
164103 Window *pMWin = p->pWin; /* Main window object */
164108 u32 selFlags = p->selFlags;
164116 if( (p->selFlags & SF_Aggregate)==0 ){
164119 sqlite3WalkExprList(&w, p->pOrderBy);
164122 p->pSrc = 0;
164123 p->pWhere = 0;
164124 p->pGroupBy = 0;
164125 p->pHaving = 0;
164126 p->selFlags &= ~SF_Aggregate;
164127 p->selFlags |= SF_WinRewrite;
164129 /* Create the ORDER BY clause for the sub-select. This is the concatenation
164132 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
164133 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
164134 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
164135 int nSave = pSort->nExpr;
164136 pSort->nExpr = p->pOrderBy->nExpr;
164137 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
164138 sqlite3ExprListDelete(db, p->pOrderBy);
164139 p->pOrderBy = 0;
164141 pSort->nExpr = nSave;
164147 pMWin->iEphCsr = pParse->nTab++;
164148 pParse->nTab += 3;
164150 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
164151 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
164152 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
164155 ** sub-select expression list. They are required to figure out where
164157 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
164158 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
164161 ** sub-select expression list. Also allocate two registers for each
164162 ** window function - one for the accumulator, another for interim
164164 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164166 assert( ExprUseXList(pWin->pOwner) );
164167 assert( pWin->pWFunc!=0 );
164168 pArgs = pWin->pOwner->x.pList;
164169 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
164171 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
164172 pWin->bExprArgs = 1;
164174 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
164177 if( pWin->pFilter ){
164178 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
164181 pWin->regAccum = ++pParse->nMem;
164182 pWin->regResult = ++pParse->nMem;
164183 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
164202 ("New window-function subquery in FROM clause of (%u/%p)\n",
164203 p->selId, p));
164204 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
164205 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
164208 if( p->pSrc ){
164210 p->pSrc->a[0].pSelect = pSub;
164211 sqlite3SrcListAssignCursors(pParse, p->pSrc);
164212 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
164214 pSub->selFlags |= (selFlags & SF_Aggregate);
164217 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
164222 pTab->tabFlags |= TF_Ephemeral;
164223 p->pSrc->a[0].pTab = pTab;
164234 if( db->mallocFailed ) rc = SQLITE_NOMEM;
164238 ** result-set or ORDER BY clause of the SELECT statement p. */
164242 assert( rc==SQLITE_OK || pParse->nErr!=0 );
164251 if( p->ppThis ){
164252 *p->ppThis = p->pNextWin;
164253 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
164254 p->ppThis = 0;
164264 sqlite3ExprDelete(db, p->pFilter);
164265 sqlite3ExprListDelete(db, p->pPartition);
164266 sqlite3ExprListDelete(db, p->pOrderBy);
164267 sqlite3ExprDelete(db, p->pEnd);
164268 sqlite3ExprDelete(db, p->pStart);
164269 sqlite3DbFree(db, p->zName);
164270 sqlite3DbFree(db, p->zBase);
164280 Window *pNext = p->pNextWin;
164288 ** value should be a non-negative integer. If the value is not a
164289 ** constant, change it to NULL. The fact that it is then a non-negative
164296 sqlite3ExprDelete(pParse->db, pExpr);
164297 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
164352 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
164354 pWin->eFrmType = eType;
164355 pWin->eStart = eStart;
164356 pWin->eEnd = eEnd;
164357 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
164360 pWin->eExclude = eExclude;
164361 pWin->bImplicitFrame = bImplicitFrame;
164362 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
164363 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
164367 sqlite3ExprDelete(pParse->db, pEnd);
164368 sqlite3ExprDelete(pParse->db, pStart);
164374 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
164375 ** equivalent nul-terminated string.
164385 pWin->pPartition = pPartition;
164386 pWin->pOrderBy = pOrderBy;
164388 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
164391 sqlite3ExprListDelete(pParse->db, pPartition);
164392 sqlite3ExprListDelete(pParse->db, pOrderBy);
164400 ** stored in the linked list starting at pWin->pNextWin. This function
164405 if( pWin->zBase ){
164406 sqlite3 *db = pParse->db;
164407 Window *pExist = windowFind(pParse, pList, pWin->zBase);
164411 if( pWin->pPartition ){
164413 }else if( pExist->pOrderBy && pWin->pOrderBy ){
164415 }else if( pExist->bImplicitFrame==0 ){
164420 "cannot override %s of window: %s", zErr, pWin->zBase
164423 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
164424 if( pExist->pOrderBy ){
164425 assert( pWin->pOrderBy==0 );
164426 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
164428 sqlite3DbFree(db, pWin->zBase);
164429 pWin->zBase = 0;
164440 assert( p->op==TK_FUNCTION );
164442 p->y.pWin = pWin;
164444 pWin->pOwner = p;
164445 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
164451 sqlite3WindowDelete(pParse->db, pWin);
164456 ** Possibly link window pWin into the list at pSel->pWin (window functions
164463 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
164464 pWin->pNextWin = pSel->pWin;
164465 if( pSel->pWin ){
164466 pSel->pWin->ppThis = &pWin->pNextWin;
164468 pSel->pWin = pWin;
164469 pWin->ppThis = &pSel->pWin;
164471 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
164472 pSel->selFlags |= SF_MultiPart;
164491 if( p1->eFrmType!=p2->eFrmType ) return 1;
164492 if( p1->eStart!=p2->eStart ) return 1;
164493 if( p1->eEnd!=p2->eEnd ) return 1;
164494 if( p1->eExclude!=p2->eExclude ) return 1;
164495 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
164496 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
164497 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
164500 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
164504 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
164514 ** to begin iterating through the sub-query results. It is used to allocate
164518 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
164519 Window *pMWin = pSelect->pWin;
164523 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
164524 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
164525 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
164526 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
164530 if( pMWin->pPartition ){
164531 int nExpr = pMWin->pPartition->nExpr;
164532 pMWin->regPart = pParse->nMem+1;
164533 pParse->nMem += nExpr;
164534 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
164537 pMWin->regOne = ++pParse->nMem;
164538 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
164540 if( pMWin->eExclude ){
164541 pMWin->regStartRowid = ++pParse->nMem;
164542 pMWin->regEndRowid = ++pParse->nMem;
164543 pMWin->csrApp = pParse->nTab++;
164544 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
164545 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
164546 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
164550 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164551 FuncDef *p = pWin->pWFunc;
164552 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
164562 assert( ExprUseXList(pWin->pOwner) );
164563 pList = pWin->pOwner->x.pList;
164565 pWin->csrApp = pParse->nTab++;
164566 pWin->regApp = pParse->nMem+1;
164567 pParse->nMem += 3;
164568 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
164569 assert( pKeyInfo->aSortFlags[0]==0 );
164570 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
164572 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
164574 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
164576 else if( p->zName==nth_valueName || p->zName==first_valueName ){
164577 /* Allocate two registers at pWin->regApp. These will be used to
164579 pWin->regApp = pParse->nMem+1;
164580 pWin->csrApp = pParse->nTab++;
164581 pParse->nMem += 2;
164582 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
164584 else if( p->zName==leadName || p->zName==lagName ){
164585 pWin->csrApp = pParse->nTab++;
164586 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
164601 ** code to check that the value is a non-negative integer and throws an
164606 "frame starting offset must be a non-negative integer",
164607 "frame ending offset must be a non-negative integer",
164609 "frame starting offset must be a non-negative number",
164610 "frame ending offset must be a non-negative number",
164648 ** Return the number of arguments passed to the window-function associated
164653 assert( ExprUseXList(pWin->pOwner) );
164654 pList = pWin->pOwner->x.pList;
164655 return (pList ? pList->nExpr : 0);
164678 ** in an array of accumulator registers - one for each window function
164685 ** order to reduce memory requirements - it would always be safe just
164700 ** Consider a window-frame similar to the following:
164720 ** Depending on the window-frame in question, all three cursors may not
164748 Window *pMWin = p->pMWin;
164749 ExprList *pOrderBy = pMWin->pOrderBy;
164751 Vdbe *v = sqlite3GetVdbe(p->pParse);
164752 ExprList *pPart = pMWin->pPartition;
164753 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
164755 for(i=0; i<pOrderBy->nExpr; i++){
164763 ** xInverse (if bInverse is non-zero) for each window function in the
164764 ** linked list starting at pMWin. Or, for built-in window functions
164775 ** already populated with all columns from the current row of the sub-query.
164777 ** If argument regPartSize is non-zero, then it is a register containing the
164787 Parse *pParse = p->pParse;
164790 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164791 FuncDef *pFunc = pWin->pWFunc;
164793 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
164796 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
164803 if( i!=1 || pFunc->zName!=nth_valueName ){
164804 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
164806 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
164811 if( pMWin->regStartRowid==0
164812 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
164813 && (pWin->eStart!=TK_UNBOUNDED)
164818 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
164819 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
164820 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
164821 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
164823 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
164825 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
164826 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
164829 }else if( pWin->regApp ){
164830 assert( pFunc->zName==nth_valueName
164831 || pFunc->zName==first_valueName
164834 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
164835 }else if( pFunc->xSFunc!=noopStepFunc ){
164837 if( pWin->pFilter ){
164839 assert( ExprUseXList(pWin->pOwner) );
164840 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
164841 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
164843 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
164849 if( pWin->bExprArgs ){
164853 assert( ExprUseXList(pWin->pOwner) );
164854 nArg = pWin->pOwner->x.pList->nExpr;
164856 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
164860 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
164861 pOp->p1 = csr;
164865 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
164868 assert( ExprUseXList(pWin->pOwner) );
164869 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
164873 bInverse, regArg, pWin->regAccum);
164876 if( pWin->bExprArgs ){
164894 ** pMWin. Or, for built-in window-functions that do not use the standard
164898 Parse *pParse = p->pParse;
164899 Window *pMWin = p->pMWin;
164903 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164904 if( pMWin->regStartRowid==0
164905 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
164906 && (pWin->eStart!=TK_UNBOUNDED)
164908 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
164909 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
164911 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
164912 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
164913 }else if( pWin->regApp ){
164914 assert( pMWin->regStartRowid==0 );
164918 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
164919 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
164920 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
164921 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
164923 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
164924 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
164932 ** p->pMWin list by doing a full scan of the current window frame. Store the
164938 Parse *pParse = p->pParse;
164939 Window *pMWin = p->pMWin;
164940 Vdbe *v = p->pVdbe;
164956 csr = pMWin->csrApp;
164957 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
164969 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
164970 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
164972 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
164973 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
164976 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
164980 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
164983 if( pMWin->eExclude==TK_CURRENT ){
164986 }else if( pMWin->eExclude!=TK_NO ){
164991 if( pMWin->pOrderBy ){
164992 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
164994 if( pMWin->eExclude==TK_TIES ){
165011 windowAggStep(p, pMWin, csr, 0, p->regArg);
165016 sqlite3VdbeJumpHere(v, addrNext-1);
165030 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
165034 ** for per-row processing is only generated for the following built-in window
165043 Window *pMWin = p->pMWin;
165044 Vdbe *v = p->pVdbe;
165046 if( pMWin->regStartRowid ){
165049 Parse *pParse = p->pParse;
165052 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165053 FuncDef *pFunc = pWin->pWFunc;
165054 assert( ExprUseXList(pWin->pOwner) );
165055 if( pFunc->zName==nth_valueName
165056 || pFunc->zName==first_valueName
165058 int csr = pWin->csrApp;
165061 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
165063 if( pFunc->zName==nth_valueName ){
165064 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
165069 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
165070 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
165074 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
165078 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
165079 int nArg = pWin->pOwner->x.pList->nExpr;
165080 int csr = pWin->csrApp;
165083 int iEph = pMWin->iEphCsr;
165086 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
165088 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
165092 int val = (pFunc->zName==leadName ? 1 : -1);
165095 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
165097 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
165104 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
165110 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
165116 ** any equivalent initialization required by any built-in window functions
165124 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165125 FuncDef *pFunc = pWin->pWFunc;
165126 assert( pWin->regAccum );
165127 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
165129 if( pMWin->regStartRowid==0 ){
165130 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
165131 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
165132 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
165135 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
165136 assert( pWin->eStart!=TK_UNBOUNDED );
165137 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
165138 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
165142 regArg = pParse->nMem+1;
165143 pParse->nMem += nArg;
165153 if( pMWin->regStartRowid ) return 1;
165154 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
165155 FuncDef *pFunc = pWin->pWFunc;
165156 if( (pFunc->zName==nth_valueName)
165157 || (pFunc->zName==first_valueName)
165158 || (pFunc->zName==leadName)
165159 || (pFunc->zName==lagName)
165169 ** pOrderBy->nExpr. This function generates code to compare the two
165186 int nVal = pOrderBy->nExpr;
165194 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
165209 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
165211 ** If the sort-order for the ORDER BY term in the window is DESC, then the
165213 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
165217 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
165227 int regVal, /* Register containing non-negative number */
165231 Parse *pParse = p->pParse;
165233 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
165236 int regString = ++pParse->nMem; /* Reg. for constant value '' */
165242 /* Read the peer-value from each cursor into a register */
165247 assert( pOrderBy && pOrderBy->nExpr==1 );
165248 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
165258 reg1, (arith==OP_Add ? "+" : "-"), regVal,
165281 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
165296 default: assert( op==OP_Lt ); /* no-op */ break;
165307 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
165310 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
165313 ** reg1 = reg1 +/- regVal
165316 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
165333 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
165362 Parse *pParse = p->pParse;
165363 Window *pMWin = p->pMWin;
165365 Vdbe *v = p->pVdbe;
165367 int bPeer = (pMWin->eFrmType!=TK_ROWS);
165372 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
165374 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
165380 if( pMWin->eFrmType==TK_RANGE ){
165384 if( pMWin->eStart==TK_FOLLOWING ){
165386 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
165390 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
165395 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
165404 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
165415 if( pMWin->eStart==pMWin->eEnd && regCountdown
165416 && pMWin->eFrmType==TK_RANGE
165421 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
165422 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
165425 }else if( p->regRowid ){
165426 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
165427 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
165432 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
165437 csr = p->current.csr;
165438 reg = p->current.reg;
165443 csr = p->start.csr;
165444 reg = p->start.reg;
165445 if( pMWin->regStartRowid ){
165446 assert( pMWin->regEndRowid );
165447 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
165449 windowAggStep(p, pMWin, csr, 1, p->regArg);
165455 csr = p->end.csr;
165456 reg = p->end.reg;
165457 if( pMWin->regStartRowid ){
165458 assert( pMWin->regEndRowid );
165459 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
165461 windowAggStep(p, pMWin, csr, 0, p->regArg);
165466 if( op==p->eDelete ){
165484 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
165487 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
165509 pNew->zName = sqlite3DbStrDup(db, p->zName);
165510 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
165511 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
165512 pNew->pWFunc = p->pWFunc;
165513 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
165514 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
165515 pNew->eFrmType = p->eFrmType;
165516 pNew->eEnd = p->eEnd;
165517 pNew->eStart = p->eStart;
165518 pNew->eExclude = p->eExclude;
165519 pNew->regResult = p->regResult;
165520 pNew->regAccum = p->regAccum;
165521 pNew->iArgCol = p->iArgCol;
165522 pNew->iEphCsr = p->iEphCsr;
165523 pNew->bExprArgs = p->bExprArgs;
165524 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
165525 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
165526 pNew->pOwner = pOwner;
165527 pNew->bImplicitFrame = p->bImplicitFrame;
165542 for(pWin=p; pWin; pWin=pWin->pNextWin){
165545 pp = &((*pp)->pNextWin);
165561 sqlite3 *db = pParse->db;
165563 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
165575 ** and invoke the sub-routine at instruction addrGosub once for each row.
165605 ** if( (regEnd--)<=0 ){
165607 ** if( (regStart--)<=0 ){
165618 ** if( (regStart--)<=0 ){
165624 ** The pseudo-code above uses the following shorthand:
165639 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
165658 ** if( (regEnd--)<=0 ){
165662 ** if( (regStart--)<=0 ){
165668 ** if( (regEnd--)<=0 ){
165684 ** regStart = regEnd - <expr1>
165687 ** if( (regEnd--)<=0 ){
165690 ** if( (regStart--)<=0 ){
165698 ** if( (regEnd--)<=0 ){
165702 ** if( (regStart--)<=0 ){
165714 ** This is optimized of course - branches that will never be taken and
165735 ** if( (regStart--)<=0 ){
165751 ** To handle this case, the pseudo-code programs depicted above are modified
165771 ** of the outer loop - the one started by sqlite3WhereBegin().
165913 Window *pMWin = p->pWin;
165914 ExprList *pOrderBy = pMWin->pOrderBy;
165917 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
165918 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
165929 WindowCodeArg s; /* Context object for sub-routines */
165934 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
165935 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
165937 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
165938 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
165940 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
165941 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
165942 || pMWin->eExclude==TK_NO
165954 s.current.csr = pMWin->iEphCsr;
165960 ** are four options - they may never be deleted (eDelete==0), they may
165965 switch( pMWin->eStart ){
165967 if( pMWin->eFrmType!=TK_RANGE
165968 && windowExprGtZero(pParse, pMWin->pStart)
165975 if( pMWin->eEnd==TK_PRECEDING ){
165976 if( pMWin->eFrmType!=TK_RANGE
165977 && windowExprGtZero(pParse, pMWin->pEnd)
165991 /* Allocate registers for the array of values from the sub-query, the
165994 regNew = pParse->nMem+1;
165995 pParse->nMem += nInput;
165996 regRecord = ++pParse->nMem;
165997 s.regRowid = ++pParse->nMem;
166002 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
166003 regStart = ++pParse->nMem;
166005 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
166006 regEnd = ++pParse->nMem;
166012 if( pMWin->eFrmType!=TK_ROWS ){
166013 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
166014 regNewPeer = regNew + pMWin->nBufferCol;
166015 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
166016 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
166017 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
166018 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
166019 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
166022 /* Load the column values for the row returned by the sub-select
166035 if( pMWin->pPartition ){
166037 ExprList *pPart = pMWin->pPartition;
166038 int nPart = pPart->nExpr;
166039 int regNewPart = regNew + pMWin->nBufferCol;
166042 regFlushPart = ++pParse->nMem;
166043 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
166049 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
166055 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
166062 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
166063 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
166066 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
166067 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
166070 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
166071 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
166083 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
166084 assert( pMWin->eEnd==TK_FOLLOWING );
166088 if( pMWin->eStart!=TK_UNBOUNDED ){
166097 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
166098 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
166099 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
166100 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
166111 if( pMWin->eStart==TK_FOLLOWING ){
166113 if( pMWin->eEnd!=TK_UNBOUNDED ){
166114 if( pMWin->eFrmType==TK_RANGE ){
166128 if( pMWin->eEnd==TK_PRECEDING ){
166129 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
166137 if( pMWin->eEnd!=TK_UNBOUNDED ){
166138 if( pMWin->eFrmType==TK_RANGE ){
166168 if( pMWin->pPartition ){
166176 if( pMWin->eEnd==TK_PRECEDING ){
166177 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
166181 }else if( pMWin->eStart==TK_FOLLOWING ){
166187 if( pMWin->eFrmType==TK_RANGE ){
166192 if( pMWin->eEnd==TK_UNBOUNDED ){
166197 assert( pMWin->eEnd==TK_FOLLOWING );
166222 if( pMWin->pPartition ){
166223 if( pMWin->regStartRowid ){
166224 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
166225 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
166239 ** 2001-09-15
166253 ** That input file is processed by Lemon to generate a C-language
166255 ** this comment as part of the translated C-code. Edits should be made
166262 ** Disable all error recovery processing in the parser push-down
166315 sqlite3 *db = pParse->db;
166316 pParse->disableLookaside++;
166336 sqlite3ExprListDelete(pParse->db, pOrderBy);
166337 sqlite3ExprDelete(pParse->db, pLimit);
166343 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
166349 if( p->pPrior ){
166353 pLoop->pNext = pNext;
166354 pLoop->selFlags |= SF_Compound;
166356 pLoop = pLoop->pPrior;
166359 if( pLoop->pOrderBy || pLoop->pLimit ){
166361 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
166362 sqlite3SelectOpName(pNext->op));
166366 if( (p->selFlags & SF_MultiValue)==0 &&
166367 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
166380 pSelect->pWith = pWith;
166383 sqlite3WithDelete(pParse->db, pWith);
166391 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
166394 p->op = (u8)op;
166395 p->affExpr = 0;
166396 p->flags = EP_Leaf;
166398 /* p->iAgg = -1; // Not required */
166399 p->pLeft = p->pRight = 0;
166400 p->pAggInfo = 0;
166401 memset(&p->x, 0, sizeof(p->x));
166402 memset(&p->y, 0, sizeof(p->y));
166403 p->op2 = 0;
166404 p->iTable = 0;
166405 p->iColumn = 0;
166406 p->u.zToken = (char*)&p[1];
166407 memcpy(p->u.zToken, t.z, t.n);
166408 p->u.zToken[t.n] = 0;
166409 p->w.iOfst = (int)(t.z - pParse->zTail);
166410 if( sqlite3Isquote(p->u.zToken[0]) ){
166414 p->nHeight = 1;
166427 sqlite3 *db = pParse->db;
166428 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
166429 pA->op = (u8)op;
166430 sqlite3ExprDelete(db, pA->pRight);
166431 pA->pRight = 0;
166449 && pParse->db->init.busy==0
166452 pIdToken->n, pIdToken->z);
166655 ** that represent terminal and non-terminal symbols.
166661 ** (also known as: "terminal symbols") have fall-back
166665 ** YYACTIONTYPE is the data type used for "action codes" - numbers
166670 ** value associated with a terminal or non-terminal
166673 ** Each non-terminal can have a different minor type.
166695 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
166696 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
166699 ** YY_NO_ACTION The yy_action[] code for no-op
166745 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
166746 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
166763 /* Define the yytestcase() macro to be a no-op if is not already defined
166788 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
166797 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
166810 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
166822 ** shifting non-terminals after a reduce.
167334 #define YY_REDUCE_MIN (-271)
167337 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
167338 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
167339 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
167340 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
167341 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167342 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167343 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167344 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
167350 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
167351 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
167352 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
167353 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
167354 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
167355 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
167358 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
167359 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
167439 /********** End of lemon-generated parsing tables *****************************/
167662 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
167665 YYMINORTYPE minor; /* The user-supplied minor token value. This
167675 int yyhwm; /* High-water mark of the stack */
168475 newSize = p->yystksz*2 + 100;
168476 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
168477 if( p->yystack==&p->yystk0 ){
168479 if( pNew ) pNew[0] = p->yystk0;
168481 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
168484 p->yystack = pNew;
168485 p->yytos = &p->yystack[idx];
168489 yyTracePrompt, p->yystksz, newSize);
168492 p->yystksz = newSize;
168513 yypParser->yyhwm = 0;
168516 yypParser->yytos = NULL;
168517 yypParser->yystack = NULL;
168518 yypParser->yystksz = 0;
168520 yypParser->yystack = &yypParser->yystk0;
168521 yypParser->yystksz = 1;
168525 yypParser->yyerrcnt = -1;
168527 yypParser->yytos = yypParser->yystack;
168528 yypParser->yystack[0].stateno = 0;
168529 yypParser->yystack[0].major = 0;
168531 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
168576 ** terminal or non-terminal is destroyed. This can happen
168591 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
168606 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
168623 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
168632 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
168637 sqlite3WithDelete(pParse->db, (yypminor->yy521));
168643 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
168649 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
168658 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
168664 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
168669 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
168676 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
168692 assert( pParser->yytos!=0 );
168693 assert( pParser->yytos > pParser->yystack );
168694 yytos = pParser->yytos--;
168699 yyTokenName[yytos->major]);
168702 yy_destructor(pParser, yytos->major, &yytos->minor);
168710 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
168712 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
168743 return pParser->yyhwm;
168749 ** is in state X and has a lookahead token Y. In a well-tested
168786 ** look-ahead token iLookAhead.
168789 YYCODETYPE iLookAhead, /* The look-ahead token */
168827 int j = i - iLookAhead + YYWILDCARD;
168850 ** Find the appropriate action for a parser given the non-terminal
168851 ** look-ahead token iLookAhead.
168855 YYCODETYPE iLookAhead /* The look-ahead token */
168890 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
168909 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
168913 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
168914 yyNewState - YY_MIN_REDUCE);
168932 yypParser->yytos++;
168934 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
168935 yypParser->yyhwm++;
168936 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
168940 if( yypParser->yytos>yypParser->yystackEnd ){
168941 yypParser->yytos--;
168946 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
168948 yypParser->yytos--;
168955 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
168957 yytos = yypParser->yytos;
168958 yytos->stateno = yyNewState;
168959 yytos->major = yyMajor;
168960 yytos->minor.yy0 = yyMinor;
168964 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
169375 ** of symbols on the right-hand side of that rule. */
169377 -1, /* (0) explain ::= EXPLAIN */
169378 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
169379 -1, /* (2) cmdx ::= cmd */
169380 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
169382 -1, /* (5) transtype ::= DEFERRED */
169383 -1, /* (6) transtype ::= IMMEDIATE */
169384 -1, /* (7) transtype ::= EXCLUSIVE */
169385 -2, /* (8) cmd ::= COMMIT|END trans_opt */
169386 -2, /* (9) cmd ::= ROLLBACK trans_opt */
169387 -2, /* (10) cmd ::= SAVEPOINT nm */
169388 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
169389 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
169390 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
169391 -1, /* (14) createkw ::= CREATE */
169393 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
169394 -1, /* (17) temp ::= TEMP */
169396 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
169397 -2, /* (20) create_table_args ::= AS select */
169399 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
169400 -2, /* (23) table_option ::= WITHOUT nm */
169401 -1, /* (24) table_option ::= nm */
169402 -2, /* (25) columnname ::= nm typetoken */
169404 -4, /* (27) typetoken ::= typename LP signed RP */
169405 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
169406 -2, /* (29) typename ::= typename ID|STRING */
169409 -2, /* (32) ccons ::= CONSTRAINT nm */
169410 -3, /* (33) ccons ::= DEFAULT scantok term */
169411 -4, /* (34) ccons ::= DEFAULT LP expr RP */
169412 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
169413 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
169414 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
169415 -3, /* (38) ccons ::= NOT NULL onconf */
169416 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
169417 -2, /* (40) ccons ::= UNIQUE onconf */
169418 -4, /* (41) ccons ::= CHECK LP expr RP */
169419 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
169420 -1, /* (43) ccons ::= defer_subclause */
169421 -2, /* (44) ccons ::= COLLATE ID|STRING */
169422 -3, /* (45) generated ::= LP expr RP */
169423 -4, /* (46) generated ::= LP expr RP ID */
169425 -1, /* (48) autoinc ::= AUTOINCR */
169427 -2, /* (50) refargs ::= refargs refarg */
169428 -2, /* (51) refarg ::= MATCH nm */
169429 -3, /* (52) refarg ::= ON INSERT refact */
169430 -3, /* (53) refarg ::= ON DELETE refact */
169431 -3, /* (54) refarg ::= ON UPDATE refact */
169432 -2, /* (55) refact ::= SET NULL */
169433 -2, /* (56) refact ::= SET DEFAULT */
169434 -1, /* (57) refact ::= CASCADE */
169435 -1, /* (58) refact ::= RESTRICT */
169436 -2, /* (59) refact ::= NO ACTION */
169437 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
169438 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
169440 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
169441 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
169443 -1, /* (66) tconscomma ::= COMMA */
169444 -2, /* (67) tcons ::= CONSTRAINT nm */
169445 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
169446 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
169447 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
169448 …-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
169451 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
169453 -2, /* (76) orconf ::= OR resolvetype */
169454 -1, /* (77) resolvetype ::= IGNORE */
169455 -1, /* (78) resolvetype ::= REPLACE */
169456 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
169457 -2, /* (80) ifexists ::= IF EXISTS */
169459 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
169460 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
169461 -1, /* (84) cmd ::= select */
169462 -3, /* (85) select ::= WITH wqlist selectnowith */
169463 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
169464 -1, /* (87) select ::= selectnowith */
169465 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
169466 -1, /* (89) multiselect_op ::= UNION */
169467 -2, /* (90) multiselect_op ::= UNION ALL */
169468 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
169469 …-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
169470 …-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
169471 -4, /* (94) values ::= VALUES LP nexprlist RP */
169472 -5, /* (95) values ::= values COMMA LP nexprlist RP */
169473 -1, /* (96) distinct ::= DISTINCT */
169474 -1, /* (97) distinct ::= ALL */
169477 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
169478 -3, /* (101) selcollist ::= sclp scanpt STAR */
169479 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
169480 -2, /* (103) as ::= AS nm */
169483 -2, /* (106) from ::= FROM seltablist */
169484 -2, /* (107) stl_prefix ::= seltablist joinop */
169486 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
169487 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
169488 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
169489 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
169490 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
169492 -2, /* (115) dbnm ::= DOT nm */
169493 -1, /* (116) fullname ::= nm */
169494 -3, /* (117) fullname ::= nm DOT nm */
169495 -1, /* (118) xfullname ::= nm */
169496 -3, /* (119) xfullname ::= nm DOT nm */
169497 -5, /* (120) xfullname ::= nm DOT nm AS nm */
169498 -3, /* (121) xfullname ::= nm AS nm */
169499 -1, /* (122) joinop ::= COMMA|JOIN */
169500 -2, /* (123) joinop ::= JOIN_KW JOIN */
169501 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
169502 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
169503 -2, /* (126) on_using ::= ON expr */
169504 -4, /* (127) on_using ::= USING LP idlist RP */
169507 -3, /* (130) indexed_by ::= INDEXED BY nm */
169508 -2, /* (131) indexed_by ::= NOT INDEXED */
169510 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
169511 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
169512 -3, /* (135) sortlist ::= expr sortorder nulls */
169513 -1, /* (136) sortorder ::= ASC */
169514 -1, /* (137) sortorder ::= DESC */
169516 -2, /* (139) nulls ::= NULLS FIRST */
169517 -2, /* (140) nulls ::= NULLS LAST */
169520 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
169522 -2, /* (145) having_opt ::= HAVING expr */
169524 -2, /* (147) limit_opt ::= LIMIT expr */
169525 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
169526 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
169527 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
169529 -2, /* (152) where_opt ::= WHERE expr */
169531 -2, /* (154) where_opt_ret ::= WHERE expr */
169532 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
169533 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
169534 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
169535 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
169536 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
169537 -3, /* (160) setlist ::= nm EQ expr */
169538 -5, /* (161) setlist ::= LP idlist RP EQ expr */
169539 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
169540 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
169542 -2, /* (165) upsert ::= RETURNING selcollist */
169543 …-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
169544 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
169545 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
169546 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
169547 -2, /* (170) returning ::= RETURNING selcollist */
169548 -2, /* (171) insert_cmd ::= INSERT orconf */
169549 -1, /* (172) insert_cmd ::= REPLACE */
169551 -3, /* (174) idlist_opt ::= LP idlist RP */
169552 -3, /* (175) idlist ::= idlist COMMA nm */
169553 -1, /* (176) idlist ::= nm */
169554 -3, /* (177) expr ::= LP expr RP */
169555 -1, /* (178) expr ::= ID|INDEXED */
169556 -1, /* (179) expr ::= JOIN_KW */
169557 -3, /* (180) expr ::= nm DOT nm */
169558 -5, /* (181) expr ::= nm DOT nm DOT nm */
169559 -1, /* (182) term ::= NULL|FLOAT|BLOB */
169560 -1, /* (183) term ::= STRING */
169561 -1, /* (184) term ::= INTEGER */
169562 -1, /* (185) expr ::= VARIABLE */
169563 -3, /* (186) expr ::= expr COLLATE ID|STRING */
169564 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
169565 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
169566 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
169567 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
169568 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
169569 -1, /* (192) term ::= CTIME_KW */
169570 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
169571 -3, /* (194) expr ::= expr AND expr */
169572 -3, /* (195) expr ::= expr OR expr */
169573 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
169574 -3, /* (197) expr ::= expr EQ|NE expr */
169575 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
169576 -3, /* (199) expr ::= expr PLUS|MINUS expr */
169577 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
169578 -3, /* (201) expr ::= expr CONCAT expr */
169579 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
169580 -3, /* (203) expr ::= expr likeop expr */
169581 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
169582 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
169583 -3, /* (206) expr ::= expr NOT NULL */
169584 -3, /* (207) expr ::= expr IS expr */
169585 -4, /* (208) expr ::= expr IS NOT expr */
169586 -6, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
169587 -5, /* (210) expr ::= expr IS DISTINCT FROM expr */
169588 -2, /* (211) expr ::= NOT expr */
169589 -2, /* (212) expr ::= BITNOT expr */
169590 -2, /* (213) expr ::= PLUS|MINUS expr */
169591 -3, /* (214) expr ::= expr PTR expr */
169592 -1, /* (215) between_op ::= BETWEEN */
169593 -2, /* (216) between_op ::= NOT BETWEEN */
169594 -5, /* (217) expr ::= expr between_op expr AND expr */
169595 -1, /* (218) in_op ::= IN */
169596 -2, /* (219) in_op ::= NOT IN */
169597 -5, /* (220) expr ::= expr in_op LP exprlist RP */
169598 -3, /* (221) expr ::= LP select RP */
169599 -5, /* (222) expr ::= expr in_op LP select RP */
169600 -5, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
169601 -4, /* (224) expr ::= EXISTS LP select RP */
169602 -5, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
169603 -5, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
169604 -4, /* (227) case_exprlist ::= WHEN expr THEN expr */
169605 -2, /* (228) case_else ::= ELSE expr */
169607 -1, /* (230) case_operand ::= expr */
169610 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
169611 -1, /* (234) nexprlist ::= expr */
169613 -3, /* (236) paren_exprlist ::= LP exprlist RP */
169614 …-12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
169615 -1, /* (238) uniqueflag ::= UNIQUE */
169618 -3, /* (241) eidlist_opt ::= LP eidlist RP */
169619 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
169620 -3, /* (243) eidlist ::= nm collate sortorder */
169622 -2, /* (245) collate ::= COLLATE ID|STRING */
169623 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
169624 -2, /* (247) cmd ::= VACUUM vinto */
169625 -3, /* (248) cmd ::= VACUUM nm vinto */
169626 -2, /* (249) vinto ::= INTO expr */
169628 -3, /* (251) cmd ::= PRAGMA nm dbnm */
169629 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
169630 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
169631 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
169632 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
169633 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
169634 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
169635 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
169636 …-11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
169637 -1, /* (260) trigger_time ::= BEFORE|AFTER */
169638 -2, /* (261) trigger_time ::= INSTEAD OF */
169640 -1, /* (263) trigger_event ::= DELETE|INSERT */
169641 -1, /* (264) trigger_event ::= UPDATE */
169642 -3, /* (265) trigger_event ::= UPDATE OF idlist */
169644 -2, /* (267) when_clause ::= WHEN expr */
169645 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
169646 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
169647 -3, /* (270) trnm ::= nm DOT nm */
169648 -3, /* (271) tridxby ::= INDEXED BY nm */
169649 -2, /* (272) tridxby ::= NOT INDEXED */
169650 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
169651 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
169652 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
169653 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
169654 -4, /* (277) expr ::= RAISE LP IGNORE RP */
169655 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
169656 -1, /* (279) raisetype ::= ROLLBACK */
169657 -1, /* (280) raisetype ::= ABORT */
169658 -1, /* (281) raisetype ::= FAIL */
169659 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
169660 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
169661 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
169663 -2, /* (286) key_opt ::= KEY expr */
169664 -1, /* (287) cmd ::= REINDEX */
169665 -3, /* (288) cmd ::= REINDEX nm dbnm */
169666 -1, /* (289) cmd ::= ANALYZE */
169667 -3, /* (290) cmd ::= ANALYZE nm dbnm */
169668 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
169669 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
169670 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
169671 -1, /* (294) add_column_fullname ::= fullname */
169672 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
169673 -1, /* (296) cmd ::= create_vtab */
169674 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
169675 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
169677 -1, /* (300) vtabargtoken ::= ANY */
169678 -3, /* (301) vtabargtoken ::= lp anylist RP */
169679 -1, /* (302) lp ::= LP */
169680 -2, /* (303) with ::= WITH wqlist */
169681 -3, /* (304) with ::= WITH RECURSIVE wqlist */
169682 -1, /* (305) wqas ::= AS */
169683 -2, /* (306) wqas ::= AS MATERIALIZED */
169684 -3, /* (307) wqas ::= AS NOT MATERIALIZED */
169685 -6, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
169686 -1, /* (309) wqlist ::= wqitem */
169687 -3, /* (310) wqlist ::= wqlist COMMA wqitem */
169688 -1, /* (311) windowdefn_list ::= windowdefn */
169689 -3, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
169690 -5, /* (313) windowdefn ::= nm AS LP window RP */
169691 -5, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
169692 -6, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
169693 -4, /* (316) window ::= ORDER BY sortlist frame_opt */
169694 -5, /* (317) window ::= nm ORDER BY sortlist frame_opt */
169695 -1, /* (318) window ::= frame_opt */
169696 -2, /* (319) window ::= nm frame_opt */
169698 -3, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
169699 …-6, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
169700 -1, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
169701 -1, /* (324) frame_bound_s ::= frame_bound */
169702 -2, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
169703 -1, /* (326) frame_bound_e ::= frame_bound */
169704 -2, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
169705 -2, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
169706 -2, /* (329) frame_bound ::= CURRENT ROW */
169708 -2, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
169709 -2, /* (332) frame_exclude ::= NO OTHERS */
169710 -2, /* (333) frame_exclude ::= CURRENT ROW */
169711 -1, /* (334) frame_exclude ::= GROUP|TIES */
169712 -2, /* (335) window_clause ::= WINDOW windowdefn_list */
169713 -2, /* (336) filter_over ::= filter_clause over_clause */
169714 -1, /* (337) filter_over ::= over_clause */
169715 -1, /* (338) filter_over ::= filter_clause */
169716 -4, /* (339) over_clause ::= OVER LP window RP */
169717 -2, /* (340) over_clause ::= OVER nm */
169718 -5, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
169719 -1, /* (342) input ::= cmdlist */
169720 -2, /* (343) cmdlist ::= cmdlist ecmd */
169721 -1, /* (344) cmdlist ::= ecmd */
169722 -1, /* (345) ecmd ::= SEMI */
169723 -2, /* (346) ecmd ::= cmdx SEMI */
169724 -3, /* (347) ecmd ::= explain cmdx SEMI */
169726 -1, /* (349) trans_opt ::= TRANSACTION */
169727 -2, /* (350) trans_opt ::= TRANSACTION nm */
169728 -1, /* (351) savepoint_opt ::= SAVEPOINT */
169730 -2, /* (353) cmd ::= create_table create_table_args */
169731 -1, /* (354) table_option_set ::= table_option */
169732 -4, /* (355) columnlist ::= columnlist COMMA columnname carglist */
169733 -2, /* (356) columnlist ::= columnname carglist */
169734 -1, /* (357) nm ::= ID|INDEXED */
169735 -1, /* (358) nm ::= STRING */
169736 -1, /* (359) nm ::= JOIN_KW */
169737 -1, /* (360) typetoken ::= typename */
169738 -1, /* (361) typename ::= ID|STRING */
169739 -1, /* (362) signed ::= plus_num */
169740 -1, /* (363) signed ::= minus_num */
169741 -2, /* (364) carglist ::= carglist ccons */
169743 -2, /* (366) ccons ::= NULL onconf */
169744 -4, /* (367) ccons ::= GENERATED ALWAYS AS generated */
169745 -2, /* (368) ccons ::= AS generated */
169746 -2, /* (369) conslist_opt ::= COMMA conslist */
169747 -3, /* (370) conslist ::= conslist tconscomma tcons */
169748 -1, /* (371) conslist ::= tcons */
169750 -1, /* (373) defer_subclause_opt ::= defer_subclause */
169751 -1, /* (374) resolvetype ::= raisetype */
169752 -1, /* (375) selectnowith ::= oneselect */
169753 -1, /* (376) oneselect ::= values */
169754 -2, /* (377) sclp ::= selcollist COMMA */
169755 -1, /* (378) as ::= ID|STRING */
169756 -1, /* (379) indexed_opt ::= indexed_by */
169758 -1, /* (381) expr ::= term */
169759 -1, /* (382) likeop ::= LIKE_KW|MATCH */
169760 -1, /* (383) exprlist ::= nexprlist */
169761 -1, /* (384) nmnum ::= plus_num */
169762 -1, /* (385) nmnum ::= nm */
169763 -1, /* (386) nmnum ::= ON */
169764 -1, /* (387) nmnum ::= DELETE */
169765 -1, /* (388) nmnum ::= DEFAULT */
169766 -1, /* (389) plus_num ::= INTEGER|FLOAT */
169768 -3, /* (391) foreach_clause ::= FOR EACH ROW */
169769 -1, /* (392) trnm ::= nm */
169771 -1, /* (394) database_kw_opt ::= DATABASE */
169774 -1, /* (397) kwcolumn_opt ::= COLUMNKW */
169775 -1, /* (398) vtabarglist ::= vtabarg */
169776 -3, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
169777 -2, /* (400) vtabarg ::= vtabarg vtabargtoken */
169779 -4, /* (402) anylist ::= anylist LP anylist RP */
169780 -2, /* (403) anylist ::= anylist ANY */
169793 ** only called from one place, optimizing compilers will in-line it, which
169810 yymsp = yypParser->yytos;
169824 { pParse->explain = 1; }
169827 { pParse->explain = 2; }
169833 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
169842 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
169846 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
169865 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[…
169882 {yymsp[-2].minor.yy394 = 1;}
169885 {yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
169889 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
169895 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
169902 {yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
169903 yymsp[-2].minor.yy285 = yylhsminor.yy285;
169908 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
169910 yymsp[-1].minor.yy285 = 0;
169927 {sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
169936 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
169941 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
169945 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
169961 {pParse->constraintName = yymsp[0].minor.yy0;}
169964 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
169967 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
169970 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
169975 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
169983 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
169992 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
169999 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
170002 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
170011 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
170014 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
170020 { yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
170023 { yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy2…
170026 { yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
170029 { yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
170032 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
170035 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
170038 { yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
170041 { yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
170044 { yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
170047 { yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
170050 { yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
170053 {yymsp[-2].minor.yy394 = 0;}
170058 {yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
170065 {yymsp[-1].minor.yy394 = 1;}
170068 {yymsp[-1].minor.yy394 = 0;}
170071 {pParse->constraintName.n = 0;}
170074 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
170077 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
170081 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
170085 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
170094 {yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
170105 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
170110 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
170115 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
170122 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
170126 {yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
170129 {yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
170137 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
170143 Select *pLhs = yymsp[-2].minor.yy47;
170144 if( pRhs && pRhs->pPrior ){
170153 pRhs->op = (u8)yymsp[-1].minor.yy394;
170154 pRhs->pPrior = pLhs;
170155 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
170156 pRhs->selFlags &= ~SF_MultiValue;
170157 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
170159 sqlite3SelectDelete(pParse->db, pLhs);
170161 yymsp[-2].minor.yy47 = pRhs;
170166 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
170169 {yymsp[-1].minor.yy394 = TK_ALL;}
170173 …-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].min…
170178 …-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].min…
170179 if( yymsp[-9].minor.yy47 ){
170180 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
170182 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
170188 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
170193 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
170194 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
170195 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
170197 pRight->op = TK_ALL;
170198 pRight->pPrior = pLeft;
170199 yymsp[-4].minor.yy47 = pRight;
170201 yymsp[-4].minor.yy47 = pLeft;
170221 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528…
170222 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
170223 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
170228 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
170229 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
170235 Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0);
170237 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
170244 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
170252 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
170253 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
170258 …if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0) ) yymsp[-1].minor.yy131->a[yyms…
170263 …yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor…
170268 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor…
170269 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
170274 …yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor…
170275 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
170280 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170285 …if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0…
170286 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
170287 }else if( yymsp[-3].minor.yy131->nSrc==1 ){
170288 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170289 if( yymsp[-5].minor.yy131 ){
170290 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
170291 SrcItem *pOld = yymsp[-3].minor.yy131->a;
170292 pNew->zName = pOld->zName;
170293 pNew->zDatabase = pOld->zDatabase;
170294 pNew->pSelect = pOld->pSelect;
170295 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
170296 pNew->fg.isNestedFrom = 1;
170298 if( pOld->fg.isTabFunc ){
170299 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
170300 pOld->u1.pFuncArg = 0;
170301 pOld->fg.isTabFunc = 0;
170302 pNew->fg.isTabFunc = 1;
170304 pOld->zName = pOld->zDatabase = 0;
170305 pOld->pSelect = 0;
170307 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
170310 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
170311 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom,0);
170312 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
170323 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
170329 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
170330 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
170332 yymsp[-2].minor.yy131 = yylhsminor.yy131;
170335 {yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
170338 {yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
170342 …yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
170343 …if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
170348 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
170349 …if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
170356 {yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
170359 {yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
170362 {yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
170365 {yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
170368 {yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
170374 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
170377 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
170381 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
170385 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
170386 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
170391 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
170392 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
170406 {yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
170409 {yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
170425 {yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
170428 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,0);}
170431 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
170434 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
170438 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
170439 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
170443 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
170446 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
170450 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
170451 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
170452 if( yymsp[-1].minor.yy131 ){
170453 SrcList *pFromClause = yymsp[-1].minor.yy131;
170454 if( pFromClause->nSrc>1 ){
170462 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
170464 …sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].mi…
170469 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
170470 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
170475 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
170481 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
170483 yymsp[-2].minor.yy322 = yylhsminor.yy322;
170487 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
170492 …sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5…
170497 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
170504 { yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
170507 { yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,…
170510 { yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0…
170513 { yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
170516 { yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy5…
170525 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
170528 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
170531 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
170534 {yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
170538 {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
170542 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
170546 yymsp[-2].minor.yy528 = yylhsminor.yy528;
170550 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0);
170551 Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
170559 yymsp[-4].minor.yy528 = yylhsminor.yy528;
170563 {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
170567 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
170568 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
170581 ** in the virtual machine. #N is the N-th register. */
170582 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
170584 if( pParse->nested==0 ){
170589 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
170596 …yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.…
170601 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
170602 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
170607 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[…
170609 yymsp[-4].minor.yy528 = yylhsminor.yy528;
170613 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
170615 yymsp[-3].minor.yy528 = yylhsminor.yy528;
170619 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[…
170622 yymsp[-5].minor.yy528 = yylhsminor.yy528;
170626 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
170629 yymsp[-4].minor.yy528 = yylhsminor.yy528;
170639 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
170640 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
170641 if( yymsp[-4].minor.yy528 ){
170642 yymsp[-4].minor.yy528->x.pList = pList;
170643 if( ALWAYS(pList->nExpr) ){
170644 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
170647 sqlite3ExprListDelete(pParse->db, pList);
170652 {yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
170661 {yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy5…
170664 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
170669 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
170670 yymsp[-1].minor.yy0.n &= 0x7fffffff;
170672 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
170673 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
170674 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy528, 0);
170675 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
170681 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
170682 yymsp[-3].minor.yy0.n &= 0x7fffffff;
170683 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
170684 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
170686 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
170687 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy528, 0);
170688 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
170692 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
170695 {yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy528,0);}
170699 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
170700 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL);
170705 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
170706 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL);
170711 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
170712 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL);
170717 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
170718 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL);
170723 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwri…
170727 …yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
170728 /*A-overwrites-B*/
170733 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
170735 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
170737 yymsp[-2].minor.yy528 = yylhsminor.yy528;
170745 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
170747 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy528, 0);
170748 if( yymsp[-4].minor.yy528 ){
170749 yymsp[-4].minor.yy528->x.pList = pList;
170751 sqlite3ExprListDelete(pParse->db, pList);
170753 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
170758 if( yymsp[-1].minor.yy322==0 ){
170767 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
170768 …yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false…
170769 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
170771 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
170772 …if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=T…
170773 yymsp[-1].minor.yy322->a[0].pExpr = 0;
170774 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
170776 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy528, pRHS);
170778 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
170779 if( yymsp[-4].minor.yy528==0 ){
170780 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
170781 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
170782 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
170783 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
170786 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
170789 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
170790 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
170793 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
170799 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
170800 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
170805 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
170806 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
170807 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
170812 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
170815 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
170816 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
170817 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
170823 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
170824 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
170829 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy528, 0);
170830 if( yymsp[-4].minor.yy528 ){
170831 …yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
170832 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
170834 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
170835 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
170841 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
170842 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
170847 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
170848 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
170852 {yymsp[0].minor.yy528 = yymsp[0].minor.yy528; /*A-overwrites-X*/}
170855 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
170858 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
170862 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
170866 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
170867 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy3…
170868 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_…
170869 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
170870 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
170883 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
170888 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
170892 {sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
170898 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
170901 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
170904 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
170907 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
170910 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
170913 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
170918 all.z = yymsp[-3].minor.yy0.z;
170919 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
170920 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
170925 …-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].min…
170926 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
170930 { yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
170933 { yymsp[-1].minor.yy394 = TK_INSTEAD;}
170940 {yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
170943 {yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
170951 { yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
170955 assert( yymsp[-2].minor.yy33!=0 );
170956 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
170957 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
170962 assert( yymsp[-1].minor.yy33!=0 );
170963 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
170968 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
170989 …tep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy…
170990 yymsp[-8].minor.yy33 = yylhsminor.yy33;
170994 …-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy4…
170996 yymsp[-7].minor.yy33 = yylhsminor.yy33;
170999 …sminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[…
171000 yymsp[-5].minor.yy33 = yylhsminor.yy33;
171003 …ggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*y…
171004 yymsp[-2].minor.yy33 = yylhsminor.yy33;
171008 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
171009 if( yymsp[-3].minor.yy528 ){
171010 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
171016 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
171017 if( yymsp[-5].minor.yy528 ) {
171018 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
171030 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
171035 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
171047 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
171053 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
171057 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
171062 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
171063 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
171068 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
171079 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
171090 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
171109 {yymsp[-1].minor.yy516 = M10d_Yes;}
171112 {yymsp[-2].minor.yy516 = M10d_No;}
171116 …sp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].…
171121 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
171126 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
171136 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
171137 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
171140 yymsp[-2].minor.yy41 = yylhsminor.yy41;
171144 if( ALWAYS(yymsp[-1].minor.yy41) ){
171145 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
171147 yylhsminor.yy41 = yymsp[-1].minor.yy41;
171149 yymsp[-4].minor.yy41 = yylhsminor.yy41;
171153 …yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, y…
171158 …e3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp…
171160 yymsp[-5].minor.yy41 = yylhsminor.yy41;
171164 …yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322…
171169 …1 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.…
171171 yymsp[-4].minor.yy41 = yylhsminor.yy41;
171182 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
171184 yymsp[-1].minor.yy41 = yylhsminor.yy41;
171193 …ylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yy…
171195 yymsp[-2].minor.yy41 = yylhsminor.yy41;
171199 …wAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yy…
171201 yymsp[-5].minor.yy41 = yylhsminor.yy41;
171211 {yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
171212 yymsp[-1].minor.yy595 = yylhsminor.yy595;
171215 {yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
171216 yymsp[-1].minor.yy595 = yylhsminor.yy595;
171222 {yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
171226 {yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
171229 {yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
171232 { yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
171237 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
171239 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
171243 yymsp[-1].minor.yy41 = yylhsminor.yy41;
171247 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
171249 yylhsminor.yy41->eFrmType = TK_FILTER;
171250 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
171252 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
171259 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
171260 assert( yymsp[-3].minor.yy41!=0 );
171265 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
171266 if( yymsp[-1].minor.yy41 ){
171267 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
171272 { yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
171354 yypParser->yytos = yymsp;
171355 yymsp->stateno = (YYACTIONTYPE)yyact;
171356 yymsp->major = (YYCODETYPE)yygoto;
171375 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
171423 yypParser->yyerrcnt = -1;
171425 assert( yypParser->yytos==yypParser->yystack );
171447 ** <li> An option argument of a grammar-specified type.
171471 assert( yypParser->yytos!=0 );
171476 yyact = yypParser->yytos->stateno;
171484 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
171490 assert( yypParser->yytos>=yypParser->yystack );
171491 assert( yyact==yypParser->yytos->stateno );
171494 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
171504 yypParser->yytos[yysize].stateno);
171518 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
171519 yypParser->yyhwm++;
171520 assert( yypParser->yyhwm ==
171521 (int)(yypParser->yytos - yypParser->yystack));
171525 if( yypParser->yytos>=yypParser->yystackEnd ){
171530 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
171542 yypParser->yyerrcnt--;
171546 yypParser->yytos--;
171580 if( yypParser->yyerrcnt<0 ){
171583 yymx = yypParser->yytos->major;
171594 while( yypParser->yytos > yypParser->yystack ){
171595 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
171600 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
171604 yypParser->yyerrcnt = -1;
171611 yypParser->yyerrcnt = 3;
171614 yyact = yypParser->yytos->stateno;
171636 if( yypParser->yyerrcnt<=0 ){
171639 yypParser->yyerrcnt = 3;
171644 yypParser->yyerrcnt = -1;
171656 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
171657 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
171696 ** individual tokens and sends those tokens one-by-one over to the
171721 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
171726 #define CC_SLASH 16 /* '/'. / or c-style comment */
171785 ** lower-case ASCII equivalent. On ASCII machines, this is just
171786 ** an upper-to-lower case map. On EBCDIC machines we also need
171839 ** might be implemented more directly using a hand-written hash table.
171897 /* aKWHash[i] is the hash value for the i-th keyword */
171911 ** then the i-th keyword has no more hash collisions. Otherwise,
171912 ** the next keyword with the same hash is aKWHash[i]-1. */
171927 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
171943 ** the text for the i-th keyword. */
171958 /* aKWCode[i] is the parser symbol code for the i-th keyword */
172120 /* Check to see if z[0..n-1] is a keyword. If it is, write the
172127 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
172128 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
172322 ** For ASCII, any character with the high-order bit set is
172323 ** allowed in an identifier. For 7-bit characters,
172353 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
172395 ** impossible to call a window-function without a FILTER clause.
172442 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
172456 if( z[1]=='-' ){
172458 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
172494 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
172620 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
172736 void *pEngine; /* The LEMON-generated LALR(1) parser */
172739 int lastTokenParsed = -1; /* type of the previous token */
172740 sqlite3 *db = pParse->db; /* The database connection */
172744 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
172746 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
172749 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
172750 if( db->nVdbeActive==0 ){
172751 AtomicStore(&db->u1.isInterrupted, 0);
172753 pParse->rc = SQLITE_OK;
172754 pParse->zTail = zSql;
172756 if( db->flags & SQLITE_ParserTrace ){
172773 assert( pParse->pNewTable==0 );
172774 assert( pParse->pNewTrigger==0 );
172775 assert( pParse->nVar==0 );
172776 assert( pParse->pVList==0 );
172777 pParentParse = db->pParse;
172778 db->pParse = pParse;
172781 mxSqlLen -= n;
172783 pParse->rc = SQLITE_TOOBIG;
172784 pParse->nErr++;
172796 if( AtomicLoad(&db->u1.isInterrupted) ){
172797 pParse->rc = SQLITE_INTERRUPT;
172798 pParse->nErr++;
172835 pParse->sLastToken.z = zSql;
172836 pParse->sLastToken.n = n;
172837 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
172840 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
172841 if( pParse->rc!=SQLITE_OK ) break;
172856 if( db->mallocFailed ){
172857 pParse->rc = SQLITE_NOMEM_BKPT;
172859 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
172860 if( pParse->zErrMsg==0 ){
172861 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
172863 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
172866 pParse->zTail = zSql;
172868 sqlite3_free(pParse->apVtabLock);
172871 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
172872 /* If the pParse->declareVtab flag is set, do not delete any table
172873 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
172876 sqlite3DeleteTable(db, pParse->pNewTable);
172878 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
172879 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
172881 if( pParse->pVList ) sqlite3DbNNFreeNN(db, pParse->pVList);
172882 db->pParse = pParentParse;
172883 assert( nErr==0 || pParse->rc!=SQLITE_OK );
172894 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
172900 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
172912 int prevType = 0; /* Previous non-whitespace token */
172920 tokenType = -1;
172924 for(i=0; zSql[i] && pStr->accError==0; i+=n){
172952 iStartIN = pStr->nChar;
172960 assert( pStr->nChar>=(u32)iStartIN );
172961 pStr->nChar = iStartIN+1;
172965 nParen--;
172971 j = pStr->nChar;
172995 while( j<pStr->nChar ){
172996 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
173007 j = pStr->nChar;
173009 while( j<pStr->nChar ){
173010 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
173054 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
173083 ** (0) INVALID We have not yet seen a non-whitespace character.
173181 case '/': { /* C-style comments */
173193 case '-': { /* SQL-style comments from "--" to end of line */
173194 if( zSql[1]!='-' ){
173203 case '[': { /* Microsoft-style identifiers in [...] */
173210 case '`': /* Grave-accent quoted symbols used by MySQL */
173211 case '"': /* single- and double-quoted strings */
173272 zSql += nId-1;
173289 ** above, except that the parameter is required to be UTF-16 encoded, not
173290 ** UTF-8.
173302 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
173440 ** This is an extension initializer that is a no-op and always
173441 ** succeeds, except that it fails if the fault-simulation is set
173469 ** built-in extensions.
173509 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
173515 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
173520 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
173528 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
173533 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
173535 ** the SQLITE_THREADSAFE compile-time option being set to 0.
173541 ** this variable being set to non-zero will cause OSTRACE macros to emit
173588 ** This routine is a no-op except on its very first call for the process,
173604 ** * Calls to this routine from Y must block until the outer-most
173625 ** combination, the work-around is to set the correct pointer
173626 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
173630 ** to sqlite3_initialize() should be a no-op. But the initialization
173653 ** malloc subsystem - this implies that the allocation of a static
173690 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
173737 sqlite3GlobalConfig.nRefInitMutex--;
173754 u64 x = (((u64)1)<<63)-1;
173765 ** compile-time option.
173783 ** when this routine is invoked, then this routine is a harmless no-op.
173832 ** the SQLite library at run-time.
173853 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
173855 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
173856 ** Single-thread. */
173862 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
173864 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
173865 ** Multi-thread. */
173871 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
173873 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
173880 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
173887 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
173896 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
173899 ** low-level memory allocation routines to be used in place of the memory
173905 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
173914 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
173925 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
173926 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
173935 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
173947 /* no-op */
173957 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
173965 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
173976 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
173981 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
173982 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
173997 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
174007 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
174042 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
174043 ** can be changed at start-time using the
174048 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
174049 ** argument of type int. If non-zero, then URI handling is globally
174057 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
174075 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
174081 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
174082 ** negative, then that argument is changed to its compile-time default.
174084 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
174086 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
174087 ** compile-time option.
174099 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
174101 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
174161 int nBig; /* Number of full-size slots */
174162 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
174171 if( db->lookaside.bMalloced ){
174172 sqlite3_free(db->lookaside.pStart);
174177 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
174185 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
174194 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
174197 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
174206 db->lookaside.pStart = pStart;
174207 db->lookaside.pInit = 0;
174208 db->lookaside.pFree = 0;
174209 db->lookaside.sz = (u16)sz;
174210 db->lookaside.szTrue = (u16)sz;
174217 p->pNext = db->lookaside.pInit;
174218 db->lookaside.pInit = p;
174222 db->lookaside.pSmallInit = 0;
174223 db->lookaside.pSmallFree = 0;
174224 db->lookaside.pMiddle = p;
174226 p->pNext = db->lookaside.pSmallInit;
174227 db->lookaside.pSmallInit = p;
174232 db->lookaside.pEnd = p;
174233 db->lookaside.bDisable = 0;
174234 db->lookaside.bMalloced = pBuf==0 ?1:0;
174235 db->lookaside.nSlot = nBig+nSm;
174237 db->lookaside.pStart = 0;
174239 db->lookaside.pSmallInit = 0;
174240 db->lookaside.pSmallFree = 0;
174241 db->lookaside.pMiddle = 0;
174243 db->lookaside.pEnd = 0;
174244 db->lookaside.bDisable = 1;
174245 db->lookaside.sz = 0;
174246 db->lookaside.bMalloced = 0;
174247 db->lookaside.nSlot = 0;
174249 db->lookaside.pTrueEnd = db->lookaside.pEnd;
174265 return db->mutex;
174278 sqlite3_mutex_enter(db->mutex);
174280 for(i=0; i<db->nDb; i++){
174281 Btree *pBt = db->aDb[i].pBt;
174288 sqlite3_mutex_leave(db->mutex);
174293 ** Flush any dirty pages in the pager-cache for any attached database
174304 sqlite3_mutex_enter(db->mutex);
174306 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
174307 Btree *pBt = db->aDb[i].pBt;
174318 sqlite3_mutex_leave(db->mutex);
174328 sqlite3_mutex_enter(db->mutex);
174332 /* IMP: R-06824-28531 */
174333 /* IMP: R-36257-52125 */
174334 db->aDb[0].zDbSName = va_arg(ap,char*);
174339 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
174340 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
174341 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
174353 pVdbe->pSharedBlock = pSharedBlock;
174355 pVdbe->startPos = pSharedBlock->startPos;
174357 pVdbe->totalRows = 0;
174358 pVdbe->blockFull = 0;
174359 pVdbe->addedRows = 0;
174392 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
174397 u64 oldFlags = db->flags;
174399 db->flags |= aFlagOp[i].mask;
174401 db->flags &= ~(u64)aFlagOp[i].mask;
174403 if( oldFlags!=db->flags ){
174407 *pRes = (db->flags & aFlagOp[i].mask)!=0;
174417 sqlite3_mutex_leave(db->mutex);
174433 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
174439 rc = nKey1 - nKey2;
174455 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
174456 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
174461 ** Return true if CollSeq is the default built-in BINARY.
174464 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
174465 return p==0 || p->xCmp==binCollFunc;
174469 ** Another built-in collating sequence: NOCASE.
174475 ** At the moment there is only a UTF-8 implementation.
174486 r = nKey1-nKey2;
174501 return db->lastRowid;
174514 sqlite3_mutex_enter(db->mutex);
174515 db->lastRowid = iRowid;
174516 sqlite3_mutex_leave(db->mutex);
174529 return db->nChange;
174545 return db->nTotalChange;
174554 ** at the b-tree/pager level.
174557 while( db->pSavepoint ){
174558 Savepoint *pTmp = db->pSavepoint;
174559 db->pSavepoint = pTmp->pNext;
174562 db->nSavepoint = 0;
174563 db->nStatement = 0;
174564 db->isTransactionSavepoint = 0;
174575 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
174576 pDestructor = p->u.pDestructor;
174578 pDestructor->nRef--;
174579 if( pDestructor->nRef==0 ){
174580 pDestructor->xDestroy(pDestructor->pUserData);
174595 for(i=0; i<db->nDb; i++){
174596 Schema *pSchema = db->aDb[i].pSchema;
174598 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
174604 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
174606 if( pMod->pEpoTab ){
174607 sqlite3VtabDisconnect(db, pMod->pEpoTab);
174623 assert( sqlite3_mutex_held(db->mutex) );
174624 if( db->pVdbe ) return 1;
174625 for(j=0; j<db->nDb; j++){
174626 Btree *pBt = db->aDb[j].pBt;
174637 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
174638 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
174644 sqlite3_mutex_enter(db->mutex);
174645 if( db->mTrace & SQLITE_TRACE_CLOSE ){
174646 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
174654 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
174656 ** SQL statements below, as the v-table implementation may be storing
174667 sqlite3_mutex_leave(db->mutex);
174680 db->eOpenState = SQLITE_STATE_ZOMBIE;
174691 int iTxn = -1;
174695 return -1;
174698 sqlite3_mutex_enter(db->mutex);
174701 if( iDb<0 ) nDb--;
174704 nDb = db->nDb-1;
174707 Btree *pBt = db->aDb[iDb].pBt;
174711 sqlite3_mutex_leave(db->mutex);
174744 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
174745 sqlite3_mutex_leave(db->mutex);
174757 ** they are reset. And that the required b-tree mutex is held to make
174765 for(j=0; j<db->nDb; j++){
174766 struct Db *pDb = &db->aDb[j];
174767 if( pDb->pBt ){
174768 sqlite3BtreeClose(pDb->pBt);
174769 pDb->pBt = 0;
174771 pDb->pSchema = 0;
174776 if( db->aDb[1].pSchema ){
174777 sqlite3SchemaClear(db->aDb[1].pSchema);
174783 assert( db->nDb<=2 );
174784 assert( db->aDb==db->aDbStatic );
174787 ** locks and does not require any further unlock-notify callbacks.
174791 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
174796 pNext = p->pNext;
174801 sqlite3HashClear(&db->aFunc);
174802 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
174812 sqlite3HashClear(&db->aCollSeq);
174814 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
174819 sqlite3HashClear(&db->aModule);
174823 sqlite3ValueFree(db->pErr);
174826 sqlite3_free(db->auth.zAuthUser);
174827 sqlite3_free(db->auth.zAuthPW);
174830 db->eOpenState = SQLITE_STATE_ERROR;
174832 /* The temp-database schema is allocated differently from the other schema
174838 sqlite3DbFree(db, db->aDb[1].pSchema);
174839 if( db->xAutovacDestr ){
174840 db->xAutovacDestr(db->pAutovacPagesArg);
174842 sqlite3_mutex_leave(db->mutex);
174843 db->eOpenState = SQLITE_STATE_CLOSED;
174844 sqlite3_mutex_free(db->mutex);
174846 if( db->lookaside.bMalloced ){
174847 sqlite3_free(db->lookaside.pStart);
174854 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
174863 assert( sqlite3_mutex_held(db->mutex) );
174866 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
174868 ** modified the database schema. If the b-tree mutexes are not taken
174869 ** here, then another shared-cache connection might sneak in between
174873 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
174875 for(i=0; i<db->nDb; i++){
174876 Btree *p = db->aDb[i].pBt;
174894 db->nDeferredCons = 0;
174895 db->nDeferredImmCons = 0;
174896 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
174898 /* If one has been configured, invoke the rollback-hook callback */
174899 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
174900 db->xRollbackCallback(db->pRollbackArg);
175083 ** again until a timeout value is reached. The timeout value is
175087 ** Return non-zero to retry the lock. Return zero to stop trying
175103 int tmout = db->busyTimeout;
175111 delay = delays[NDELAY-1];
175112 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
175115 delay = tmout - prior;
175118 sqlite3OsSleep(db->pVfs, delay*1000);
175124 int tmout = ((sqlite3 *)ptr)->busyTimeout;
175128 sqlite3OsSleep(db->pVfs, 1000000);
175139 ** If this routine returns non-zero, the lock is retried. If it
175144 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
175145 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
175147 p->nBusy = -1;
175149 p->nBusy++;
175166 sqlite3_mutex_enter(db->mutex);
175167 db->busyHandler.xBusyHandler = xBusy;
175168 db->busyHandler.pBusyArg = pArg;
175169 db->busyHandler.nBusy = 0;
175170 db->busyTimeout = 0;
175171 sqlite3_mutex_leave(db->mutex);
175193 sqlite3_mutex_enter(db->mutex);
175195 db->xProgress = xProgress;
175196 db->nProgressOps = (unsigned)nOps;
175197 db->pProgressArg = pArg;
175199 db->xProgress = 0;
175200 db->nProgressOps = 0;
175201 db->pProgressArg = 0;
175203 sqlite3_mutex_leave(db->mutex);
175219 db->busyTimeout = ms;
175231 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) ){
175236 AtomicStore(&db->u1.isInterrupted, 1);
175262 assert( sqlite3_mutex_held(db->mutex) );
175268 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
175332 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
175333 if( db->nVdbeActive ){
175335 "unable to delete/modify user-function due to active statements");
175336 assert( !db->mallocFailed );
175342 /* Trying to delete a function that does not exist. This is a no-op.
175348 assert(p || db->mallocFailed);
175358 pDestructor->nRef++;
175360 p->u.pDestructor = pDestructor;
175361 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
175362 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
175363 testcase( p->funcFlags & SQLITE_DIRECTONLY );
175364 p->xSFunc = xSFunc ? xSFunc : xStep;
175365 p->xFinalize = xFinal;
175366 p->xValue = xValue;
175367 p->xInverse = xInverse;
175368 p->pUserData = pUserData;
175369 p->nArg = (u16)nArg;
175374 ** Worker function used by utf-8 APIs that create new functions:
175401 sqlite3_mutex_enter(db->mutex);
175409 pArg->nRef = 0;
175410 pArg->xDestroy = xDestroy;
175411 pArg->pUserData = p;
175416 if( pArg && pArg->nRef==0 ){
175424 sqlite3_mutex_leave(db->mutex);
175491 sqlite3_mutex_enter(db->mutex);
175492 assert( !db->mallocFailed );
175493 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
175497 sqlite3_mutex_leave(db->mutex);
175521 sqlite3_result_error(context, zErr, -1);
175529 ** this routine is a no-op. If the function does not exist, then create
175530 ** a new one that always throws a run-time error.
175546 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
175550 sqlite3_mutex_enter(db->mutex);
175552 sqlite3_mutex_leave(db->mutex);
175565 ** A NULL trace function means that no tracing is executes. A non-NULL
175579 sqlite3_mutex_enter(db->mutex);
175580 pOld = db->pTraceArg;
175581 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
175582 db->trace.xLegacy = xTrace;
175583 db->pTraceArg = pArg;
175584 sqlite3_mutex_leave(db->mutex);
175589 /* Register a trace callback using the version-2 interface.
175602 sqlite3_mutex_enter(db->mutex);
175605 db->mTrace = mTrace;
175606 db->trace.xV2 = xTrace;
175607 db->pTraceArg = pArg;
175608 sqlite3_mutex_leave(db->mutex);
175617 ** A NULL profile function means that no profiling is executes. A non-NULL
175634 sqlite3_mutex_enter(db->mutex);
175635 pOld = db->pProfileArg;
175636 db->xProfile = xProfile;
175637 db->pProfileArg = pArg;
175638 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
175639 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
175640 sqlite3_mutex_leave(db->mutex);
175648 ** If the invoked function returns non-zero, then the commit becomes a
175664 sqlite3_mutex_enter(db->mutex);
175665 pOld = db->pCommitArg;
175666 db->xCommitCallback = xCallback;
175667 db->pCommitArg = pArg;
175668 sqlite3_mutex_leave(db->mutex);
175689 sqlite3_mutex_enter(db->mutex);
175690 pRet = db->pUpdateArg;
175691 db->xUpdateCallback = xCallback;
175692 db->pUpdateArg = pArg;
175693 sqlite3_mutex_leave(db->mutex);
175714 sqlite3_mutex_enter(db->mutex);
175715 pRet = db->pRollbackArg;
175716 db->xRollbackCallback = xCallback;
175717 db->pRollbackArg = pArg;
175718 sqlite3_mutex_leave(db->mutex);
175734 sqlite3_mutex_enter(db->mutex);
175735 pRet = db->pPreUpdateArg;
175736 db->xPreUpdateCallback = xCallback;
175737 db->pPreUpdateArg = pArg;
175738 sqlite3_mutex_leave(db->mutex);
175759 sqlite3_mutex_enter(db->mutex);
175760 if( db->xAutovacDestr ){
175761 db->xAutovacDestr(db->pAutovacPagesArg);
175763 db->xAutovacPages = xCallback;
175764 db->pAutovacPagesArg = pArg;
175765 db->xAutovacDestr = xDestructor;
175766 sqlite3_mutex_leave(db->mutex);
175823 ** into the write-ahead-log by this database connection.
175838 sqlite3_mutex_enter(db->mutex);
175839 pRet = db->pWalArg;
175840 db->xWalCallback = xCallback;
175841 db->pWalArg = pArg;
175842 sqlite3_mutex_leave(db->mutex);
175869 /* Initialize the output variables to -1 in case an error occurs. */
175870 if( pnLog ) *pnLog = -1;
175871 if( pnCkpt ) *pnCkpt = -1;
175878 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
175883 sqlite3_mutex_enter(db->mutex);
175893 db->busyHandler.nBusy = 0;
175901 if( db->nVdbeActive==0 ){
175902 AtomicStore(&db->u1.isInterrupted, 0);
175905 sqlite3_mutex_leave(db->mutex);
175913 ** to contains a zero-length string, all attached databases are
175917 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
175924 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
175933 ** associated with the specific b-tree being checkpointed is taken by
175937 ** checkpointed. If an error is encountered it is returned immediately -
175948 assert( sqlite3_mutex_held(db->mutex) );
175949 assert( !pnLog || *pnLog==-1 );
175950 assert( !pnCkpt || *pnCkpt==-1 );
175954 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
175956 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
175971 ** This function returns true if main-memory should be used instead of
175973 ** The value returned depends on the value of db->temp_store (runtime
175978 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
175979 ** ----------------- -------------- ------------------------------
175991 return ( db->temp_store==2 );
175994 return ( db->temp_store!=1 );
176007 ** Return UTF-8 encoded English language explanation of the most recent
176018 sqlite3_mutex_enter(db->mutex);
176019 if( db->mallocFailed ){
176022 testcase( db->pErr==0 );
176023 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
176024 assert( !db->mallocFailed );
176026 z = sqlite3ErrStr(db->errCode);
176029 sqlite3_mutex_leave(db->mutex);
176037 int iOffset = -1;
176038 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
176039 sqlite3_mutex_enter(db->mutex);
176040 iOffset = db->errByteOffset;
176041 sqlite3_mutex_leave(db->mutex);
176048 ** Return UTF-16 encoded English language explanation of the most recent
176068 sqlite3_mutex_enter(db->mutex);
176069 if( db->mallocFailed ){
176072 z = sqlite3_value_text16(db->pErr);
176074 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
176075 z = sqlite3_value_text16(db->pErr);
176078 ** above. If this is the case, then the db->mallocFailed flag needs to
176084 sqlite3_mutex_leave(db->mutex);
176097 if( !db || db->mallocFailed ){
176100 return db->errCode & db->errMask;
176106 if( !db || db->mallocFailed ){
176109 return db->errCode;
176112 return db ? db->iSysErrno : 0;
176139 assert( sqlite3_mutex_held(db->mutex) );
176157 ** are no active VMs, invalidate any pre-compiled statements.
176160 if( pColl && pColl->xCmp ){
176161 if( db->nVdbeActive ){
176171 ** Also, collation destructor - CollSeq.xDel() - function may need
176174 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
176175 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
176179 if( p->enc==pColl->enc ){
176180 if( p->xDel ){
176181 p->xDel(p->pUser);
176183 p->xCmp = 0;
176191 pColl->xCmp = xCompare;
176192 pColl->pUser = pCtx;
176193 pColl->xDel = xDel;
176194 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
176215 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
176260 ** If an invalid limit index is supplied, report -1.
176274 return -1;
176278 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
176279 ** there is a hard upper bound set at compile-time by a C preprocessor
176296 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
176300 return -1;
176302 oldLimit = db->aLimit[limitId];
176303 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
176305 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
176309 db->aLimit[limitId] = newLimit;
176311 return oldLimit; /* IMP: R-53341-35419 */
176315 ** This function is used to parse both URIs and non-URI filenames passed by the
176321 ** query parameter. The second argument contains the URI (or non-URI filename)
176342 const char *zUri, /* Nul-terminated URI to parse */
176357 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
176358 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
176359 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
176368 ** method that there may be extra parameters following the file-name. */
176375 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
176398 iIn-7, &zUri[7]);
176411 ** 0: Parsing file-name.
176449 if( zFile[iOut-1]==0 ){
176451 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
176467 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
176616 sqlite3_key_v2(db, zDb, zKey, -1);
176628 ** is UTF-8 encoded.
176631 const char *zFilename, /* Database filename UTF-8 encoded */
176699 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
176700 if( db->mutex==0 ){
176706 sqlite3MutexWarnOnContention(db->mutex);
176709 sqlite3_mutex_enter(db->mutex);