Lines Matching +full:stale +full:- +full:pr +full:- +full:label
17 ** language. The code for the "sqlite3" command-line shell is also in a
48 ** NO_TEST - The branches on this line are not
53 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
57 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
61 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
66 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
137 ** 2015-03-02
175 ** large file support, or if the OS is windows, these should be no-ops.
181 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
190 ** on 2008-11-28.) These days, all Linux kernels support large files, so
207 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
214 ** so the GCC_VERSION macro will be set to a correct non-zero value even
267 ** some MinGW-specific macros). When compiling for MinGW, either the
290 /* Optionally #include a user-defined header, whereby compilation options
308 ** 2001-09-15
319 ** presents to client programs. If a C-function, structure, datatype,
330 ** The official C-language API documentation for SQLite is derived
375 ** that require non-default calling conventions.
400 ** These no-op macros are used in front of interfaces to mark those
402 ** should not use deprecated interfaces - they are supported for backwards
426 ** CAPI3REF: Compile-Time Library Version Numbers
442 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
444 ** a string which identifies a particular check-in of SQLite
446 ** string contains the date and time of the check-in (UTC) and a SHA1
447 ** or SHA3-256 hash of the entire source tree. If the source code has
457 #define SQLITE_SOURCE_ID "2022-07-21 15:24:47 698edb77537b67c41adc68f9b892db56bcf9a55e00371a61…
460 ** CAPI3REF: Run-Time Library Version Numbers
497 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
506 ** returning the N-th compile time option string. ^If N is out of range,
531 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
549 ** This interface only reports on the compile-time mutex setting
555 ** sqlite3_threadsafe() function shows only the compile-time setting of
556 ** thread safety, not any run-time changes to that setting made by
581 ** CAPI3REF: 64-Bit Integer Types
584 ** Because there is no cross-platform way to specify 64-bit integer types
585 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
592 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
615 ** substitute integer for floating-point.
657 ** argument is a harmless no-op.
670 ** CAPI3REF: One-Step Query Execution Interface
678 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
679 ** semicolon-separate SQL statements passed into its 2nd argument,
701 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
751 /* beginning-of-error-codes */
782 /* end-of-error-codes */
790 ** these result codes are too coarse-grained. They do not provide as
894 ** [sqlite3_open_v2()] has historically be a no-op and might become an
950 ** read-only media and cannot be changed even by processes with
1008 ** (Third-party VFS implementations might also make the distinction
1043 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1076 ** locking strategy (for example to use dot-file locks), to inquire
1077 ** about the status of a lock, or to break stale locks. The SQLite
1123 ** fails to zero-fill short reads might seem to work. However,
1124 ** failure to zero-fill short reads will eventually lead to
1170 ** compile-time option is used.
1181 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1183 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1193 ** point to an integer (type int) containing the new chunk-size to use
1195 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1206 ** the [rollback journal] or the [write-ahead log]) for a particular database
1219 ** this file-control is NULL. However, if the database file is being synced
1220 ** as part of a multi-database commit, the argument points to a nul-terminated
1221 ** string containing the transactions super-journal file name. VFSes that
1238 ** anti-virus programs. By default, the windows VFS will retry file read,
1264 ** WAL mode. If the integer is -1, then it is overwritten with the current
1269 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1273 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1274 ** mode. If the integer is -1, then it is overwritten with the current
1275 ** zero-damage mode setting.
1286 ** final bottom-level VFS are written into memory obtained from
1290 ** all file-control actions, there is no guarantee that this will actually
1292 ** pointer in case this file-control is not implemented. This file-control
1296 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1300 ** to a pointer to the top-level VFS.)^
1302 ** upper-most shim only.
1319 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1321 ** of the result string if the string is non-NULL.
1327 ** it is able to override built-in [PRAGMA] statements.
1331 ** file-control may be invoked by SQLite on the database file handle
1333 ** to the connection's busy-handler callback. The argument is of type (void**)
1334 ** - an array of two (void *) values. The first (void *) actually points
1336 ** busy-handler, this function should be invoked with the second (void *) in
1337 ** the array as the only argument. If it returns non-zero, then the operation
1342 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1352 ** maximum number of bytes that will be used for memory-mapped I/O.
1358 ** file-control is used internally to implement [PRAGMA mmap_size].
1364 ** The argument is a zero-terminated string. Higher layers in the
1366 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1391 ** Applications should <em>not</em> use this file-control.
1440 ** The parameter is a pointer to a 32-bit signed integer that contains
1441 ** the value that M is to be set to. Before returning, the 32-bit signed
1446 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1472 ** file to the database file, but before the *-shm file is updated to
1478 ** whether or not there is a database client in another process with a wal-mode
1480 ** (void*) argument passed with this file-control should be a pointer to a
1484 ** the database is not a wal-mode db, or if there is no such connection in any
1590 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1608 ** consist of a single "-" character followed by no more than
1609 ** 11 alphanumeric and/or "-" characters.
1624 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1644 ** the open of a journal file a no-op. Writes to this journal would
1645 ** also be no-ops, and any attempt to read the journal would return
1647 ** file will be doing page-aligned sector reads and writes in a random
1686 ** flag is never actually used and is not implemented in the built-in
1689 ** non-zero error code if there is an I/O error or if the name of
1691 ** is returned, then non-zero or zero is written into *pResOut to indicate
1705 ** of good-quality randomness into zOut. The return value is
1713 ** a 24-hour day).
1739 void *pAppData; /* Pointer to application-specific data */
1849 ** are harmless no-ops.)^
1854 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1877 ** compile-time option, then the automatic calls to sqlite3_initialize()
1886 ** The sqlite3_os_init() routine does operating-system specific
1902 ** (using the [SQLITE_OS_OTHER=1] compile-time
1904 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
1933 ** implementation of an application-defined [sqlite3_os_init()].
1943 ** then this routine returns a non-zero [error code].
1957 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1970 ** and low-level memory allocation routines.
1982 ** Note that SQLite comes with several [built-in memory allocators]
1987 ** memory allocator that simulates memory out-of-memory conditions in
2052 ** non-zero [error code] if a discontinued or unsupported configuration option
2058 ** [threading mode] to Single-thread. In other words, it disables
2061 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2063 ** value of Single-thread and so [sqlite3_config()] will return
2069 ** [threading mode] to Multi-thread. In other words, it disables
2073 ** are enabled so that SQLite will be safe to use in a multi-threaded
2076 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2077 ** it is not possible to set the Multi-thread [threading mode] and
2092 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2101 ** alternative low-level memory allocation routines to be used in place of
2129 ** disabled, the following SQLite interfaces become non-operational:
2150 ** This configuration option is a no-op if an application-defined page
2153 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2161 ** argument must be either a NULL pointer or a pointer to an 8-byte
2168 ** ^If pMem is NULL and N is non-zero, then each database connection
2171 ** of -1024*N bytes if N is negative, . ^If additional
2184 ** An 8-byte aligned pointer to the memory,
2191 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2199 ** The argument specifies alternative low-level mutex routines to be used
2203 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2216 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2249 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2251 ** passed through as the first parameter to the application-defined logger
2259 ** In a multi-threaded application, the application-defined logger
2264 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2281 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2282 ** if that compile-time option is omitted.
2292 ** They are retained for backwards compatibility but are now no-ops.
2298 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2314 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2321 ** compile-time maximum mmap size set by the
2322 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2324 ** changed to its compile-time default.
2329 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2330 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2346 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2355 ** becomes the [statement journal] spill-to-disk threshold.
2358 ** Or if the threshold is -1, statement journals are always held
2364 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2369 ** of type (int) - the new value of the sorter-reference size threshold.
2373 ** of a table column that its values are likely to be very large - larger
2374 ** than the configured sorter-reference size threshold - then a reference
2380 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2385 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2388 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2390 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2391 ** compile-time option is not set, then the default maximum is 1073741824.
2407 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2408 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2434 ** non-zero [error code] if a discontinued or unsupported configuration option
2450 ** must be aligned to an 8-byte boundary. ^If the second argument to
2486 ** triggers in the main database schema or in the schemas of ATTACH-ed
2503 ** views in the main database schema or in the schemas of ATTACH-ed
2510 ** [FTS3] full-text search engine extension.
2525 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2527 ** When the first argument to this interface is 1, then only the C-API is
2529 ** this interface is 0, then both the C-API and the SQL function are disabled.
2530 ** If the first argument is -1, then no changes are made to state of either the
2531 ** C-API or the SQL function.
2554 ** is an integer - positive to disable checkpoints-on-close, or zero (the
2557 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2558 ** have been disabled - 0 if they are not disabled, 1 if they are.
2581 ** behavior. The first parameter passed to this operation is an integer -
2585 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2638 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2647 ** the legacy [double-quoted string literal] misfeature for DML statements
2649 ** default value of this setting is determined by the [-DSQLITE_DQS]
2650 ** compile-time option.
2656 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2658 ** default value of this setting is determined by the [-DSQLITE_DQS]
2659 ** compile-time option.
2686 ** created database file to have a schema format version number (the 4-byte
2740 ** has a unique 64-bit signed
2821 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2843 ** any changes performed by sub-triggers, as the sqlite3_changes()
2844 ** value will be saved and restored after each sub-trigger has run.)^
2912 ** CAPI3REF: Interrupt A Long-Running Query
2918 ** or Ctrl-C where the user wants a long query operation to halt
2943 ** SQL statements is a no-op and has no effect on SQL statements
2951 ** These routines are useful during command-line input to determine if the
2957 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
2972 ** then the return value from sqlite3_complete16() will be non-zero
2975 ** The input to [sqlite3_complete()] must be a zero-terminated
2976 ** UTF-8 string.
2978 ** The input to [sqlite3_complete16()] must be a zero-terminated
2979 ** UTF-16 string in native byte order.
2986 ** KEYWORDS: {busy-handler callback} {busy handler}
3008 ** ^If the callback returns non-zero, then another attempt
3084 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3086 ** to zero-terminated strings that contain the names of the columns.
3088 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3100 ** -----------------------
3122 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3154 ** These routines are work-alikes of the "printf()" family of functions
3158 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3159 ** See the [built-in printf()] documentation for details.
3182 ** guarantees that the buffer is always zero-terminated. ^The first
3185 ** written will be n-1 characters.
3189 ** See also: [built-in printf()], [printf() SQL function]
3201 ** does not include operating-system specific [VFS] implementation. The
3212 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3213 ** of a signed 32-bit integer.
3218 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3223 ** might result if sqlite3_free() is called with a non-NULL pointer that
3243 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3244 ** of a 32-bit signed integer.
3259 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3283 ** routines, which form the built-in memory allocation subsystem.
3288 ** value of [sqlite3_memory_used()] since the high-water mark
3295 ** ^The memory high-water mark is reset to the current value of
3298 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3305 ** CAPI3REF: Pseudo-Random Number Generator
3307 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3310 ** the built-in random() and randomblob() SQL functions. This interface allows
3321 ** non-NULL P then the pseudo-randomness is generated
3328 ** CAPI3REF: Compile-Time Authorization Callbacks
3359 ** to the callback are either NULL pointers or zero-terminated strings
3387 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3406 ** statement might be re-prepared during [sqlite3_step()] due to a
3452 ** is the name of the inner-most trigger or view that is responsible for
3454 ** top-level SQL code.
3504 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3508 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3510 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3515 ** the original statement text and an estimate of wall-clock time
3535 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3557 ** interface by using the X argument when X begins with "--" and invoking
3564 ** X argument points to a 64-bit integer which is the estimated of
3594 ** M argument should be the bitwise OR-ed combination of
3644 ** ^If the progress callback returns non-zero, the operation is
3661 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3662 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3673 ** ^The default encoding will be UTF-8 for databases created using
3675 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
3689 ** <dd>The database is opened in read-only mode. If the database does not
3711 ** <dd>The database will be opened as an in-memory database. The database
3712 ** is named by the "filename" argument for the purposes of cache-sharing,
3717 ** <dd>The new database connection will use the "multi-thread"
3758 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
3769 ** ^If the filename is ":memory:", then a private, temporary in-memory database
3770 ** is created for the connection. ^This in-memory database will vanish when
3778 ** on-disk database will be created. ^This private database will be
3788 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3811 ** SQLite and its built-in [VFSes] interpret the
3826 ** ^If "ro" is specified, then the database is opened for read-only
3829 ** "rw", then the database is opened for read-write (but not create)
3833 ** set to "memory" then a pure [in-memory database] that never reads
3860 ** read-only media. ^When immutable is set, SQLite assumes that the
3862 ** privilege, and so the database is opened read-only and all locking
3887 ** <tr><td style="white-space:nowrap">
3891 ** necessary - space characters can be used literally
3894 ** Open file "data.db" in the current directory for read-only access.
3895 ** Regardless of whether or not shared-cache mode is enabled by
3897 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3898 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3899 ** that uses dot-files in place of posix advisory locking.
3907 ** percent sign - "%" - followed by exactly two hexadecimal digits
3909 ** URI filename are interpreted, they are encoded using UTF-8 and all
3911 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
3915 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3917 ** characters must be converted to UTF-8 prior to passing them into
3927 const char *filename, /* Database filename (UTF-8) */
3931 const void *filename, /* Database filename (UTF-16) */
3935 const char *filename, /* Database filename (UTF-8) */
3972 ** case or if the value begins with a non-zero number. The
3980 ** 64-bit signed integer and returns that integer, or D if P does not
3985 ** the value) of the N-th query parameter for filename F, or a NULL
3987 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4046 ** ^If X is the name of a rollback or WAL-mode journal file that is
4052 ** only. It is not a general-purpose interface.
4096 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4132 ** change the value of the error code. The error-code preserving
4143 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4144 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4150 ** ^The sqlite3_errstr() interface returns the English-language text
4151 ** that describes the [result code], as UTF-8.
4160 ** SQL, then the sqlite3_error_offset() function returns -1.
4195 ** The life-cycle of a prepared statement object usually goes like this:
4210 ** CAPI3REF: Run-time Limits
4223 ** set at compile-time by a C preprocessor macro called
4232 ** simply invoke this interface with the third parameter set to -1.
4234 ** Run-time limits are intended for use in applications that manage
4247 ** New run-time limit categories may be added in future releases.
4252 ** CAPI3REF: Run-Time Limit Categories
4256 ** that can be lowered at run-time using [sqlite3_limit()].
4342 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4365 ** To execute an SQL statement, it must first be compiled into a byte-code
4374 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4375 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4376 ** as a convenience. The UTF-16 interfaces work by converting the
4377 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4384 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4386 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4387 ** and sqlite3_prepare16_v3() use UTF-16.
4393 ** If the caller knows that the supplied string is nul-terminated, then
4396 ** the nul-terminator.
4446 ** ^The specific value of a WHERE-clause [parameter] might influence the
4447 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4449 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4461 const char *zSql, /* SQL statement, UTF-8 encoded */
4468 const char *zSql, /* SQL statement, UTF-8 encoded */
4475 const char *zSql, /* SQL statement, UTF-8 encoded */
4483 const void *zSql, /* SQL statement, UTF-16 encoded */
4490 const void *zSql, /* SQL statement, UTF-16 encoded */
4497 const void *zSql, /* SQL statement, UTF-16 encoded */
4508 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4512 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4515 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4531 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4532 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4543 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4555 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4559 ** Note that [application-defined SQL functions] or
4563 ** change the database file through side-effects:
4589 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4591 ** read-only no-op if the table already exists, but
4616 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4653 ** sqlite3_value object. If SQLite is compiled to be single-threaded
4664 ** implementation of [application-defined SQL functions] are protected.
4682 ** is always first parameter to [application-defined SQL functions].
4683 ** The application-defined SQL function implementation will pass this
4733 ** it should be a pointer to well-formed UTF8 text.
4735 ** it should be a pointer to well-formed UTF16 text.
4737 ** it should be a pointer to a well-formed unicode string that is
4741 ** [[byte-order determination rules]] ^The byte-order of
4742 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4759 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4799 ** ^A negative value for the zeroblob results in a zero-length BLOB.
4801 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4873 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
4884 ** ^If the value N is out of range or if the N-th parameter is
4886 ** always in UTF-8 encoding even if the named parameter was
4887 ** originally specified as UTF-16 in [sqlite3_prepare16()],
4904 ** name must be given in UTF-8 even if the original statement
4905 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
4946 ** interface returns a pointer to a zero-terminated UTF-8 string
4947 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
4948 ** UTF-16 string. ^The first parameter is the [prepared statement]
4959 ** (for example during a conversion from UTF-8 to UTF-16) then a
4978 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
4987 ** ^The names returned are the original un-aliased names of the
4993 ** ^The left-most column is column 0 for these routines.
5002 ** UTF-16 encoded strings and the other functions return UTF-8.
5005 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5029 ** ^The returned string is always UTF-8 encoded.
5042 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5092 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5118 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5148 ** will return non-zero if previous call to [sqlite3_step](P) returned
5150 ** where it always returns zero since each step of that multi-step
5164 ** <li> 64-bit signed integer
5165 ** <li> 64-bit IEEE floating point number
5198 ** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result
5199 ** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result
5200 ** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result
5201 ** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result
5206 ** or a UTF-8 TEXT result in bytes
5208 ** <td>→ <td>Size of UTF-16
5260 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5262 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5263 ** the string to UTF-8 and then returns the number of bytes.
5265 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5269 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5271 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5272 ** the string to UTF-16 and then returns the number of bytes.
5274 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5285 ** even empty strings, are always zero-terminated. ^The return
5286 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5302 ** [application-defined SQL functions] or [virtual tables], not within
5303 ** top-level application code.
5321 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5342 ** sqlite3_column_text16() is called. A zero-terminator might
5344 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5346 ** to UTF-16.</li>
5347 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5349 ** to UTF-8.</li>
5352 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5383 ** fail if an out-of-memory error occurs during a format conversion.
5384 ** Only the following subset of interfaces are subject to out-of-memory
5395 ** If an out-of-memory error occurs, then the return value from these
5397 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5431 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5446 ** object back to its initial state, ready to be re-executed.
5485 ** connection then application-defined SQL functions must be added
5489 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5490 ** representation, exclusive of the zero-terminator. ^Note that the name
5491 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5497 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5500 ** parameter is less than -1 or greater than 127 then the behavior is
5519 ** deterministic. The built-in [random()] SQL function is an example of a
5530 ** all application-defined SQL functions that do not need to be
5544 ** pointers to C-language functions that implement the SQL function or
5554 ** C-language callbacks that implement the new function. xStep and xFinal
5555 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5557 ** non-NULL, in which case the new function may be used as either an aggregate
5560 ** [user-defined window functions|available here].
5575 ** SQL function is used. ^A function implementation with a non-negative
5584 ** ^Built-in functions may be overloaded by new application-defined functions.
5586 ** ^An application-defined function is permitted to call other
5641 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
5642 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
5643 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
5670 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5674 ** for all [application-defined SQL functions], and especially for functions
5675 ** that have side-effects or that could potentially leak sensitive
5695 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
5697 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
5699 ** security-adverse side-effects and information-leaks.
5704 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5706 ** functions. However, if it is not specified for a user-defined window
5707 ** function, then any sub-types belonging to arguments passed to the window
5746 ** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value
5747 ** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value
5749 ** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value
5750 ** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in
5752 ** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value
5753 ** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value
5756 ** or a UTF-8 TEXT in bytes
5758 ** <td>→ <td>Size of UTF-16
5776 ** implement [application-defined SQL functions] and [virtual tables].
5786 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5787 ** in the native byte-order of the host machine. ^The
5789 ** extract UTF-16 strings as big-endian and little-endian respectively.
5830 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5845 ** fail if an out-of-memory error occurs during a format conversion.
5846 ** Only the following subset of interfaces are subject to out-of-memory
5859 ** If an out-of-memory error occurs, then the return value from these
5861 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5887 ** an [application-defined SQL function] argument V. The subtype
5907 ** then sqlite3_value_free(V) is a harmless no-op.
5968 ** the application-defined function is running.
5988 ** These functions may be used by (non-aggregate) SQL functions to
5992 ** of where this might be useful is in a regular-expression matching
6001 ** value to the application-defined function. ^N is zero for the left-most
6006 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6007 ** argument of the application-defined function. ^Subsequent
6031 ** function parameters that are compile-time constants, including literal
6034 ** The value of the N parameter to these interfaces should be non-negative.
6061 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6077 ** an application-defined function to be the BLOB whose content is pointed
6082 ** interfaces set the result of the application-defined function to be
6086 ** an application-defined function to be a floating point value specified
6094 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6095 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6096 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6101 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6119 ** of the application-defined function to be the 32-bit signed integer
6122 ** of the application-defined function to be the 64-bit signed integer
6126 ** of the application-defined function to be NULL.
6130 ** set the return value of the application-defined function to be
6131 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6132 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6134 ** application-defined function to be a text string in an encoding
6143 ** is non-negative, then as many bytes (not characters) of the text
6144 ** pointed to by the 2nd parameter are taken as the application-defined
6145 ** function result. If the 3rd parameter is non-negative, then it
6152 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6168 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6170 ** byte-order specified by the BOM. ^The byte-order specified by
6171 ** the BOM at the beginning of the text overrides the byte-order
6174 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6185 ** the application-defined function to be a copy of the
6196 ** also associates the host-language pointer P or type T with that
6198 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6206 ** than the one containing the application-defined function that received
6238 ** the result from the [application-defined SQL function] with
6254 ** ^The name of the collation is a UTF-8 string
6256 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6319 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6362 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6363 ** the names are passed as UTF-16 in machine native byte order.
6364 ** ^A call to either function replaces the existing collation-needed callback.
6423 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6465 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6466 ** TemporaryFolder->Path->Data();
6469 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6482 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6521 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6526 ** the current directory on the sub-platforms of Win32 where that concept is
6530 ** UTF-8 or UTF-16, respectively.
6549 ** CAPI3REF: Test For Auto-Commit Mode
6553 ** ^The sqlite3_get_autocommit() interface returns non-zero or
6557 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6559 ** If certain kinds of errors occur on a statement within a multi-statement
6590 ** for the N-th database on database connection D, or a NULL pointer of N is
6592 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
6600 ** remember the string long-term should make their own copy. Applications that
6602 ** threads should mutex-protect calls to this API and should make their own
6614 ** connection D, or if database N is a temporary or in-memory database, then
6619 ** is [DETACH]-ed or until the database connection closes.
6640 ** CAPI3REF: Determine if a database is read-only
6644 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6663 ** a valid schema, then -1 is returned.
6729 ** ^If the callback on a commit hook function returns non-zero,
6751 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6753 ** hook returning non-zero, just as it would be with any other rollback.
6773 ** the schema-name of the attached database that is being autovacuumed,
6781 ** <p>^If there are multiple ATTACH-ed database files that are being
6918 ** shared cache mode should be enabled per-database connection via
6922 ** 32-bit integer is atomic.
6924 ** See Also: [SQLite Shared-Cache Mode]
6932 ** of heap memory by deallocating non-essential memory allocations
6934 ** pages to improve performance is an example of non-essential memory.
6937 ** ^The sqlite3_release_memory() routine is a no-op returning zero
6951 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6986 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7010 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7011 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7044 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7048 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7115 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7127 ** with various operating-system specific extensions added.
7136 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7175 ** [extension loading] while evaluating user-entered SQL, the following API
7183 ** ^This interface enables or disables both the C-API
7186 ** to enable or disable only the C-API.)^
7226 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7255 ** The interface to the virtual-table mechanism is currently considered
7259 ** When the virtual-table mechanism stabilizes, we will declare the
7333 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7345 ** expr on the right-hand side can be evaluated (and thus the constraint
7360 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7366 ** non-zero.
7370 ** the right-hand side of the corresponding aConstraint[] is evaluated
7371 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7400 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7431 int iColumn; /* Column constrained. -1 for ROWID */
7434 int iTermOffset; /* Used internally - xBestIndex should ignore */
7476 ** ^The left-hand operand of the operator is given by the corresponding
7477 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
7480 ** operators have no left-hand operand, and so for those operators the
7489 ** The right-hand operands for each constraint might be accessible using
7490 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
7492 ** in the input SQL. If the right-hand operand is another column or an
7496 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
7501 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
7664 ** The interface to the virtual-table mechanism defined above (back up
7669 ** When the virtual-table mechanism stabilizes, we will declare the
7706 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
7708 ** read-only access.
7744 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7760 ** and the built-in [zeroblob] SQL function may be used to create a
7761 ** zero-filled blob to read or write using the incremental-blob interface.
7791 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7813 ** ^If the blob handle being closed was opened for read-write access, and if
7814 ** the database is in auto-commit mode and there are no other open read-write
7822 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7849 ** caller-supplied buffer. N bytes of data are copied into buffer Z
7878 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
7927 ** ^Names are zero-terminated UTF-8 strings.
7958 ** is selected automatically at compile-time. The following
7969 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
7974 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8011 ** cases where it really needs one. If a faster non-recursive mutex
8058 ** behave as no-ops.
8071 ** An instance of this structure defines the low-level routines
8121 ** xMutexInit() must be no-ops.
8167 ** the routine should return 1. This seems counter-intuitive since
8171 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8187 ** next. Applications that override the built-in mutex logic must be
8203 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8218 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8224 ** CAPI3REF: Low-Level Control Of Database Files
8336 ** by enclosing in double-quotes) so as not to confuse the parser.
8341 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8344 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8350 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8362 ** <li> Put all identifier names inside double-quotes. This is the official
8373 ** compile-time options. For example, "VACUUM" is not a keyword if
8374 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8385 ** An instance of the sqlite3_str object contains a dynamically-sized
8409 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8449 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8454 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8455 ** S must contain at least N non-zero bytes of content. To append a
8456 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8460 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8463 ** single-byte character C onto the end of [sqlite3_str] object X.
8489 ** [SQLITE_NOMEM] following any out-of-memory error, or
8496 ** zero-termination byte.
8531 ** SQLITE_OK on success and a non-zero [error code] on failure.
8534 ** be represented by a 32-bit integer, then the values returned by
8552 ** These integer constants designate various run-time status parameters
8560 ** and internal memory usage by the SQLite library. Auxiliary page-cache
8645 ** non-zero [error code] on failure.
8661 ** The [sqlite3_db_status()] interface will return a non-zero error code
8671 ** satisfied using lookaside memory. Only the high-water value is meaningful;
8679 ** Only the high-water value is meaningful;
8687 ** Only the high-water value is meaningful;
8710 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8750 ** to disk all at once. When pages spill mid-transaction, that introduces
8821 ** A non-zero value in this counter may indicate an opportunity to
8827 ** A non-zero value in this counter may indicate an opportunity to
8856 ** step was bypassed because a Bloom filter returned not-found. The
8926 ** The built-in page cache is recommended for most uses.
8941 ** built-in default page cache is used instead of the application defined
8973 ** false if it is used for an in-memory database. The cache implementation
8984 ** suggested maximum cache-size (number of pages stored by) the cache
9030 ** as its second argument. If the third parameter, discard, is non-zero,
9064 ** is not obligated to free any memory, but well-behaved implementations should
9123 ** for copying in-memory databases to or from persistent files.
9129 ** ^The source database is read-locked only while it is being read;
9163 ** there is already a read or read-write transaction open on the
9195 ** <li> the destination database was opened read-only, or
9196 ** <li> the destination database is using write-ahead-log journaling
9198 ** <li> the destination database is an in-memory database and the
9202 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9203 ** the [sqlite3_busy_handler | busy-handler function]
9205 ** busy-handler returns non-zero before the lock is available, then
9226 ** sqlite3_backup_step(), the source database may be modified mid-way
9243 ** active write-transaction on the destination database is rolled back.
9250 ** ^If an out-of-memory condition or IO error occurred during any prior
9319 ** ^When running in shared-cache mode, a database operation may fail with
9320 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9321 ** individual tables within the shared-cache cannot be obtained. See
9322 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9326 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9330 ** ^Shared-cache locks are released when a database connection concludes
9334 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9344 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9350 ** ^If the blocked connection is attempting to obtain a write-lock on a
9351 ** shared-cache table, and more than one other connection currently holds
9352 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9355 ** ^(There may be at most one unlock-notify callback registered by a
9357 ** blocked connection already has a registered unlock-notify callback,
9360 ** unlock-notify callback is canceled. ^The blocked connections
9361 ** unlock-notify callback may also be canceled by closing the blocked
9364 ** The unlock-notify callback is not reentrant. If an application invokes
9365 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9373 ** When an unlock-notify callback is registered, the application provides a
9377 ** an unlock-notify callback is a pointer to an array of void* pointers,
9381 ** more than one blocked connection that has registered for an unlock-notify
9391 ** Assuming that after registering for an unlock-notify callback a
9402 ** unlock-notify callback is registered. The system is said to be in
9403 ** a deadlocked state if connection A has registered for an unlock-notify
9405 ** B has itself registered for an unlock-notify callback when connection
9408 ** registered for an unlock-notify callback on the conclusion of connection
9420 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9421 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9441 ** and extensions to compare the contents of two buffers containing UTF-8
9442 ** strings in a case-independent fashion, using the same definition of "case
9458 ** Note that this routine returns zero on a match and non-zero if the strings
9475 ** insensitive - equivalent upper and lower case ASCII characters match
9481 ** Note that this routine returns zero on a match and non-zero if the strings
9505 ** a fixed-length buffer on the stack. If the log message is longer than
9512 ** CAPI3REF: Write-Ahead Log Commit Hook
9519 ** the associated write-lock on the database released)^, so the implementation
9525 ** ^The third parameter is the name of the database that was written to -
9526 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
9527 ** is the number of pages currently in the write-ahead log file,
9538 ** A single database handle may have at most a single write-ahead log callback
9540 ** previously registered write-ahead log callback. ^The return value is
9553 ** CAPI3REF: Configure an auto-checkpoint
9560 ** more frames in the [write-ahead log] file. ^Passing zero or
9575 ** ^Every new [database connection] defaults to having the auto-checkpoint
9591 ** [write-ahead log] for database X on [database connection] D to be
9592 ** transferred into the database file and for the write-ahead log to
9618 ** in the log were checkpointed. ^The [busy-handler callback]
9625 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
9634 ** [busy-handler callback])
9647 ** the log file or to -1 if the checkpoint could not run because
9651 ** was called) or to -1 if the checkpoint could not run due to an error or
9659 ** busy-handler configured, it will not be invoked in this case.
9663 ** obtained immediately, and a busy-handler is configured, it is invoked and
9664 ** the writer lock retried until either the busy-handler returns 0 or the lock
9665 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
9666 ** database readers as described above. ^If the busy-handler returns 0 before
9669 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9685 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9761 ** If X is non-zero, then the virtual table implementation guarantees
9826 ** [xUpdate] method understands as a "no-change" value.
9967 ** on the right-hand side of the IN operator.)^ Thus the virtual table
9968 ** only sees a single value from the right-hand side of the IN operator
9972 ** table to see all values on the right-hand of the IN operator all at
9977 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
9978 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
9980 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
9981 ** by which the virtual table can ask SQLite if all-at-once processing
9987 ** the IN operator all-at-once, respectively. ^Thus when the third
9988 ** parameter (F) is non-negative, this interface is the mechanism by
9997 ** (non-zero), that means that the constraint is an IN operator
9998 ** that can be processed all-at-once. ^If the constraint is not an IN
9999 ** operator or cannot be processed all-at-once, then the interface returns
10002 ** ^(All-at-once processing of the IN operator is selected if both of the
10006 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10008 ** use the N-th constraint.
10011 ** non-negative had F>=1.
10015 ** the traditional one-at-a-time processing strategy for the IN constraint.
10016 ** ^If both conditions are true, then the argvIndex-th parameter to the
10019 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10025 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10035 ** a parameter that was previously selected for all-at-once IN constraint
10038 ** an xFilter argument that was selected for all-at-once IN constraint
10042 ** ^(Use these routines to access all values on the right-hand side
10084 ** J being a 0-based index into P->aConstraint[], then this routine
10085 ** attempts to set *V to the value of the right-hand operand of
10086 ** that constraint if the right-hand operand is known. ^If the
10087 ** right-hand operand is not known, then *V is set to a NULL pointer.
10090 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10096 ** the right-hand operand of a constraint is a literal value in the original
10097 ** SQL statement. If the right-hand operand is an expression or a reference
10102 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10111 ** "Right-Hand Side".
10148 ** set to the total number of times that the X-th loop has run.</dd>
10152 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10157 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10164 ** to a zero-terminated UTF-8 string containing the name of the index or table
10165 ** used for the X-th loop.
10169 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10170 ** description for the X-th loop.
10174 ** "select-id" for the X-th loop. The select-id identifies which query or
10175 ** subquery the loop is part of. The main query has a select-id of zero.
10176 ** The select-id is the same value as is output in the first column
10198 ** compile-time option.
10206 ** Loops are numbered starting from zero. ^If idx is out of range - less than
10208 ** the statement - a non-zero value is returned and the variable that pOut
10213 ** as if the loop did not exist - it returns non-zero and leave the variable
10226 ** CAPI3REF: Zero Scan-Status Counters
10231 ** This API is only available if the library is built with pre-processor
10237 ** CAPI3REF: Flush caches to disk mid-transaction
10240 ** ^If a write-transaction is open on [database connection] D when the
10242 ** pages in the pager-cache that are not currently in use are written out
10246 ** interface flushes caches for all schemas - "main", "temp", and
10251 ** immediately and there is a busy-handler callback configured, it is invoked
10259 ** example an IO error or out-of-memory condition), then processing is
10270 ** CAPI3REF: The pre-update hook.
10274 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10346 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10347 ** triggers; or 2 for changes resulting from triggers called by top-level
10351 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10356 ** pre-update hook is being invoked for some other reason, including a
10357 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
10383 ** CAPI3REF: Low-level system error code
10388 ** The return value is OS-dependent. For example, on unix systems, after
10428 ** If there is not already a read-transaction open on schema S when
10460 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10494 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10495 ** SQLITE_IOERR error code - is returned, then the final state of the
10509 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10526 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10593 ** For an ordinary on-disk database file, the serialization is just a
10594 ** copy of the disk file. For an in-memory database or a "TEMP" database,
10631 ** Zero or more of the following constants can be OR-ed together for
10635 ** a pointer to contiguous in-memory database that it is currently using,
10637 ** a contiguous in-memory database, then this option causes
10639 ** using a contiguous in-memory database if it has been initialized by a
10649 ** reopen S as an in-memory database based on the serialization contained
10653 ** permitted to add content to the in-memory database as long as the total
10705 ** should be treated as read-only.
10709 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10749 /* The double-precision datatype used by RTree depends on the
10750 ** SQLITE_RTREE_INT_ONLY compile-time option.
10760 ** R-Tree geometry query as follows:
10785 ** Register a 2nd-generation geometry callback named zScore that can be
10786 ** used as part of an R-Tree geometry query as follows:
10892 ** is not possible for an application to register a pre-update hook on a
10895 ** which a pre-update hook is already defined. The results of attempting
10963 ** disabled - it does not. A newly created session object is enabled.
10970 ** no-op, and may be used to query the current state of the session.
11047 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11053 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11057 ** conflict-handler callback) then the X'' value is returned. The application
11105 ** database row along with the updated values for each updated non-primary-key
11114 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11116 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11129 ** are sorted in the same order in which they were attached (or auto-attached)
11143 ** recorded once - the first time a row with said primary key is inserted,
11150 ** The session object therefore accumulates two types of records - those
11168 ** found and one or more of the non-primary key fields have been
11185 ** This may appear to have some counter-intuitive effects if a single row
11201 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11223 ** does not have a primary key, this function is a no-op (but does not return
11243 ** used to update the table in database zFrom (call this the "from-table")
11245 ** object (call this the "to-table"). Specifically:
11248 ** <li> For each row (primary key) that exists in the to-table but not in
11249 ** the from-table, an INSERT record is added to the session object.
11251 ** <li> For each row (primary key) that exists in the to-table but not in
11252 ** the from-table, a DELETE record is added to the session object.
11255 ** different non-PK values in each, an UPDATE record is added to the
11300 ** Because the non-primary key "old.*" fields are omitted, no
11319 ** Return non-zero if no changes to attached tables have been recorded by
11327 ** are restored. However, if this function returns non-zero, then it is
11414 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11438 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11451 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11454 ** or until the conflict-handler function returns.
11501 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11510 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11541 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11575 ** conflict-handler callback by [sqlite3changeset_apply()] with either
11586 ** "conflicting row" associated with the current conflict-handler callback
11624 ** function with an iterator passed to a conflict-handler by
11632 ** returned. This is to allow the following pattern (pseudo-code):
11785 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
11786 ** <tr><th style="white-space:pre">Existing Change </th>
11787 ** <th style="white-space:pre">New Change </th>
11832 ** returned. Or, if an out-of-memory condition occurs during processing, this
11890 ** Otherwise, if the return value is non-zero or the xFilter argument to
11926 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11930 ** returned by each invocation of the conflict-handler function. Refer to
11939 ** stored in all non-primary key columns also match the values stored in
11943 ** the non-primary key fields contains a value different from the original
11944 ** row value stored in the changeset, the conflict-handler function is
11947 ** only the values of those non-primary key fields are compared against
11948 ** the current database contents - any trailing database table columns
11952 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11957 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11976 ** This includes the case where the INSERT operation is re-attempted because
11984 ** stored in all modified non-primary key columns also match the values
11988 ** the modified non-primary key fields contains a value different from an
11989 ** original row value stored in the changeset, the conflict-handler function
11991 ** UPDATE changes only contain values for non-primary key fields that are
11993 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11996 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12000 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12018 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12095 ** Values that may be passed as the second argument to a conflict-handler.
12102 ** (non primary-key) fields modified by the update do not contain the
12263 ** combined on a per-field basis, not per-row. This means that in the
12347 ** corresponding non-streaming API functions:
12349 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12350 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12360 ** Non-streaming functions that accept changesets (or patchsets) as input
12365 ** low-memory environment is required to handle very large changesets, the
12629 ** non-negative but less than the number of columns in the table, return
12644 ** non-negative but less than the number of columns in the table, set
12658 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12756 ** If the bClear argument is non-zero, then the auxiliary data is cleared
12778 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12780 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12786 ** modify this structure directly - it should only be used as shown above
12794 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12808 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12810 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
12819 ** xPhraseFirstColumn() set iCol to -1).
12888 ** The second and third arguments are an array of nul-terminated strings
12906 ** by argument pText. pText may or may not be nul-terminated. The first
12914 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12919 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12923 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12928 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12961 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
12995 ** still appears to contain just two phrases - "(first OR 1st)"
13006 ** when tokenizing query text (it should not - to do so would be
13053 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13149 ** autoconf-based build
13177 ** The hard limit is the ability of a 32-bit signed integer
13178 ** to count the size: 2^31-1 or 2147483647.
13196 ** tell you that in a well-normalized database, you usually should
13254 ** The suggested maximum number of in-memory pages to use for
13257 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13259 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13260 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13263 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13277 ** counted using a signed 8-bit integer which has a maximum value of 127
13289 ** as a signed 32-bit integer can hold.
13296 ** imposed by the use of 16-bit offsets within each page.
13302 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13303 ** compiled with the default page-size limit will not be able to rollback
13326 ** device characteristics (sector-size and atomic write() support),
13343 ** This value can be lowered (or raised) at run-time using that the
13374 #pragma warn -rch /* unreachable code */
13375 #pragma warn -ccc /* Condition is always true or false */
13376 #pragma warn -aus /* Assigned value is never used */
13377 #pragma warn -csu /* Comparing signed and unsigned */
13378 #pragma warn -spa /* Suspicious pointer arithmetic */
13382 ** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13386 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13419 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13432 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13433 #else /* Generates a warning - but it always works */
13473 ** level of threadsafety. 2 means the library is multithreaded - multiple
13481 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13489 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13495 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13502 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13531 # error "Two or more of the following compile-time configuration options\
13569 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13634 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
13646 ** of SQLite to unexpected behavior - to make the code "self-healing"
13652 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
13738 ** Return true (non-zero) if the input is an integer that is too large
13739 ** to fit in 32-bits. This macro is used inside of various testcase()
13740 ** macros to verify that we have tested SQLite for large-file support.
13767 ** This is the header file for the generic hash-table implementation
13778 ** The internals of this structure are intended to be opaque -- client
13785 ** All elements of the hash table are on a single doubly-linked list.
13789 ** the global doubly-linked list. The contents of the bucket are the
13790 ** element pointed to plus the next _ht.count-1 elements in the list.
13809 ** structure. All elements are stored on a single doubly-linked list.
13840 #define sqliteHashFirst(H) ((H)->first)
13841 #define sqliteHashNext(E) ((E)->next)
13842 #define sqliteHashData(E) ((E)->data)
13843 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
13844 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
13849 #define sqliteHashCount(H) ((H)->count)
14059 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14064 ** substitute integer for floating-point
14095 ** the VDBE-level file format changes. The following macros define the
14106 ** changed at run-time using a pragma.
14114 ** on the command-line
14144 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14146 ** The default value of "20" was chosen to minimize the run-time of the
14147 ** speedtest1 test program with options: --shrink-memory --reprepare
14161 ** The compile-time options SQLITE_MMAP_READWRITE and
14174 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14205 ** types can be conveniently redefined at compile-type. Like this:
14207 ** cc '-DUINTPTR_TYPE=long long int' ...
14247 typedef sqlite_int64 i64; /* 8-byte signed integer */
14248 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14249 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14250 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14251 typedef INT16_TYPE i16; /* 2-byte signed integer */
14252 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14253 typedef INT8_TYPE i8; /* 1-byte signed integer */
14261 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14266 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
14267 ** can be used at compile-time if desired.
14270 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14272 typedef u32 tRowcnt; /* 32-bit is the default */
14276 ** Estimated quantities used for query planning are stored as 16-bit
14278 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14287 ** 1 -> 0 20 -> 43 10000 -> 132
14288 ** 2 -> 10 25 -> 46 25000 -> 146
14289 ** 3 -> 16 100 -> 66 1000000 -> 199
14290 ** 4 -> 20 1000 -> 99 1048576 -> 200
14291 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14296 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14339 ** and whether or not that determination is run-time or compile-time.
14341 ** For best performance, an attempt is made to guess at the byte-order
14342 ** using C-preprocessor macros. If that is unsuccessful, or if
14343 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14344 ** at run-time.
14379 ** Constants for the largest and smallest possible 64-bit signed integers.
14380 ** These macros are designed to work correctly on both 32-bit and 64-bit
14385 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14389 ** to force 8-byte alignment on 64-bit architectures.
14394 ** pointers in size, and so it is a no-op on systems where the pointer
14410 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14415 ** underlying malloc() implementation might return us 4-byte aligned
14416 ** pointers. In that case, only verify 4-byte alignment.
14419 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14421 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14433 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14453 ** default MMAP_SIZE is specified at compile-time, make sure that it does
14477 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14498 ** An instance of the following structure is used to store the busy-handler
14503 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14537 ** The root-page of the schema table.
14556 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14575 ** directly, we use its constant as a key to lookup the run-time allocated
14577 ** for the run-time allocated buffer.
14580 ** macros become no-ops and have zero performance impact.
14677 ** Changing this from a 64-bit to a 32-bit type limits the number of
14699 #define ALLBITS ((Bitmask)-1)
14741 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14764 ** is devoted to storing a super-journal name - there are no more pages to
14768 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14769 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
14777 #define PAGER_MEMORY 0x0002 /* In-memory database */
14782 #define PAGER_LOCKINGMODE_QUERY -1
14793 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14798 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14799 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14805 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14827 ** that make up the Pager sub-system API. See source code comments for
14975 ** This header file defines the interface that the sqlite B-Tree file
14988 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
14995 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
14996 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15009 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15024 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15025 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15079 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15082 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15085 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15086 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15106 ** For example, the free-page-count field is located at byte offset 36 of
15107 ** the database file header. The incr-vacuum-flag field is located at
15111 ** It is a read-only number computed by the pager. But we merge it with
15124 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15138 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15139 ** column of the b-tree of the cursor. The Expr tree will not contain
15140 ** any function calls nor subqueries nor references to b-trees other than
15143 ** The design of the _RANGE hint is aid b-tree implementations that try
15144 ** to prefetch content from remote machines - to provide those
15169 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15174 ** For read-only cursors the wrFlag argument is always zero. For read-write
15185 ** by this, the native b-tree engine of SQLite, but it is available to
15187 ** b-tree system. For alternative storage engines in which a delete of
15191 ** and DELETE operations as no-ops, and any READ operation against a
15194 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
15200 int wrFlag, /* 1 for writing. 0 for read-only */
15337 ** Enter and Leave procedures no-ops.
15424 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15455 u32 iSrcLine; /* Source-code line that generated this opcode
15463 ** A sub-routine used to implement a trigger program.
15466 VdbeOp *aOp; /* Array of opcodes for sub-program */
15472 SubProgram *pNext; /* Next sub-program already visited */
15492 #define P4_STATIC (-1) /* Pointer to a static string */
15493 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15494 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15495 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15496 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
15498 #define P4_FREE_IF_LE (-6)
15499 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
15500 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
15501 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
15502 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
15503 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
15504 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
15505 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
15506 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
15507 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
15508 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
15536 ** The following macro converts a label returned by sqlite3VdbeMakeLabel()
15538 ** address of that label.
15599 #define OP_IfPos 49 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15609 #define OP_IfNotZero 59 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15610 #define OP_DecrJumpZero 60 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15640 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
15657 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15674 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
15710 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
15786 ** Additional non-public SQLITE_PREPARE_* flags
15835 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15840 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15915 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
15952 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
15957 ** // in distingishing equal and not-equal.
15960 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
15961 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
15969 ** is responsible for keeping track of this and reporting byte-code branches
16099 /* Modify the page-size after the cache has been created. */
16121 /* Change a page number. Used by incr-vacuum. */
16163 /* Set and get the suggested cache-size for the specified pager-cache.
16166 ** the total number of pages cached by purgeable pager-caches to the sum
16167 ** of the suggested cache-sizes.
16174 /* Set or get the suggested spill-size for the specified pager-cache.
16176 ** The spill-size is the minimum number of pages in cache before the cache
16224 ** This header file (together with is companion C source-code file
16228 ** This header file is #include-ed by sqliteInt.h and thus ends up
16236 ** necessary pre-processor macros for it.
16252 ** This file contains pre-processor directives related to operating system
16302 ** a no-op
16335 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16337 ** 2006-10-31: The default prefix used to be "sqlite_". But then
16338 ** Mcafee started using SQLite in their anti-virus product and it
16384 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
16492 ** sqlite3_malloc() to obtain space for the file-handle structure.
16531 ** at start-time.
16533 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
16536 ** start-time.
16538 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16540 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16557 ** If this is a no-op implementation, implement everything as macros.
16578 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16598 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16599 ** In other words, the zero-based numbers are used for all external interfaces
16600 ** and the one-based values are used internally.
16628 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16657 ** Db.pSchema->flags field.
16659 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16660 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16661 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16662 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16665 ** Allowed values for the DB.pSchema->flags field.
16685 ** Lookaside malloc is a set of fixed-size buffers that can be used
16707 ** in a performance-critical path. sz should be set by to szTrue whenever
16712 ** come off of pFree first, then pInit as a fallback. This dual-list
16713 ** allows use to compute a high-water mark - the maximum number of allocations
16714 ** outstanding at any point in the past - by subtracting the number of
16717 ** Enhancement on 2019-12-12: Two-size-lookaside
16721 ** The two-size-lookaside enhancement breaks up the lookaside allocation
16722 ** into two pools: One of 128-byte slots and the other of the default size
16723 ** (1200-byte) slots. Allocations are filled from the small-pool first,
16724 ** failing over to the full-size pool if that does not work. Thus more
16741 void *pMiddle; /* First byte past end of full-size buffers and
16751 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16752 #define EnableLookaside db->lookaside.bDisable--;\
16753 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16755 /* Size of the smaller allocations in two-size lookside */
16763 ** A hash table for built-in function definitions. (Application-defined
16846 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16854 u8 autoCommit; /* The auto-commit flag. */
16858 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16864 u8 noSharedCache; /* True if no shared-cache backends */
16912 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
16950 int nSavepoint; /* Number of non-transaction savepoints */
16951 int nStatement; /* Number of nested statement-transactions */
16980 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
16981 #define ENC(db) ((db)->enc)
16986 ** C-compilers still do not accept LL integer literals.
17010 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
17029 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17030 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17051 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17072 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17076 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17078 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17086 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
17095 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17096 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17102 #define ConstFactorOk(P) ((P)->okConstFactor)
17117 ** structure. For global built-in functions (ex: substr(), max(), count())
17119 ** For per-connection application-defined functions, a pointer to this
17120 ** structure is held in the db->aHash hash table.
17122 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17123 ** field is used by per-connection app-def functions.
17126 i8 nArg; /* Number of arguments. -1 means unlimited */
17130 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17133 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17142 ** This structure encapsulates a user-function destructor callback (as
17179 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17182 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17183 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17184 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17185 /* 0x0200 -- available for reuse */
17186 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17190 ** single query - might change over time */
17191 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17192 /* 0x8000 -- available for reuse */
17193 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17196 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17198 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17199 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17202 /* Identifier numbers for each in-line function */
17220 ** as the user-data (sqlite3_user_data()) for the function. If
17231 ** zName is the name of a function that is implemented by in-line
17237 ** zName is the name of a test-only function implemented by in-line
17246 ** a single query. The iArg is ignored. The user-data is always set
17250 ** For math-library functions. xPtr is an arbitrary pointer.
17255 ** ignored and the user-data for these functions is set to an
17256 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17274 ** available as the function user-data (sqlite3_user_data()). The
17338 char *zName; /* Savepoint name (nul-terminated) */
17399 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
17430 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17450 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17462 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17515 ** database connections, even when the rest of the in-memory database
17525 ** database schema are initially stored in a linked-list pointed to by
17532 ** When an in-memory Table object is deleted (for example when the
17582 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17613 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17614 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17624 #define TF_Readonly 0x00000001 /* Read-only system table */
17635 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
17636 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
17651 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
17652 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
17660 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
17662 ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->eTabType==TABTYP_VTAB)
17670 ** only works for non-virtual tables (ordinary tables and views) and is
17675 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17676 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17678 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17687 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17688 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17703 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17706 ** from-table == child-table
17707 ** to-table == parent-table
17710 ** which is attached to the from-table. The to-table need not exist when
17711 ** the from-table is created. The existence of the to-table is not checked.
17725 /* EV: R-30323-21917 */
17791 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17819 ** pKeyInfo->nField.
17823 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17827 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
17828 ** multiple entries in the b-tree with the same key (when only looking
17829 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17834 ** get and equal results when comparing this structure to a b-tree record.
17838 ** b-tree.
17841 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17880 ** algorithm to employ whenever an attempt is made to insert a non-unique
17888 ** number (it cannot - the database page is not allocated until the VDBE
17913 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17922 IndexSample *aSample; /* Samples of the left-most key */
17923 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17924 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17938 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
17941 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
17946 #define XN_ROWID (-1) /* Indexed column is the rowid */
17947 #define XN_EXPR (-2) /* Indexed column is an expression */
17979 const char *z; /* Text of the token. Not NULL-terminated! */
18002 int sortingIdxPTab; /* Cursor number of pseudo-table */
18030 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18031 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18032 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18037 ** the option is available (at compile-time).
18052 ** to represent the greater-than-or-equal-to operator in the expression
18066 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18085 ** subquery gives a constant result, then iTable is -1. If the subquery
18090 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18121 int iValue; /* Non-negative integer value if EP_IntValue */
18146 ** TK_TRIGGER: 1 -> new, 0 -> old
18151 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18154 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18218 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18219 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18220 #define ExprSetProperty(E,P) (E)->flags|=(P)
18221 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18222 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
18223 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
18228 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
18229 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
18230 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
18231 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
18232 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
18233 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
18234 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
18243 ** processes but is a no-op for delivery.
18246 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18247 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18248 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18268 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18278 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18294 ** ---------- -------------------------
18353 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18362 Expr *pExpr; /* Expr to implement a USING variable -- NOT USED */
18373 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
18395 int regResult; /* Registers holding results of a co-routine */
18400 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18401 unsigned isCorrelated :1; /* True if sub-query is correlated */
18403 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18409 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18421 ExprList *pFuncArg; /* Arguments to table-valued-function */
18454 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18455 ** contains more than 63 columns and the 64-th or later column is used.
18483 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18486 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18489 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18518 ** NameContexts can be nested. When resolving names, the inner-most
18534 ExprList *pEList; /* Optional list of result-set columns */
18575 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
18583 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18584 ** conflict-target clause.) The pUpsertTargetWhere is the optional
18688 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
18694 #define IsNestedFrom(S) ((S)!=0 && ((S)->selFlags&SF_NestedFrom)!=0)
18702 ** identified by pDest->iSDParm.
18704 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
18706 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18711 ** the side-effects of functions.
18718 ** in register pDest->iSDParm then abandon the rest
18722 ** row of result as the key in table pDest->iSDParm.
18723 ** Apply the affinity pDest->affSdst before storing
18726 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18732 ** SRT_Coroutine Generate a co-routine that returns a new row of
18734 ** of the co-routine is stored in register pDest->iSDParm
18735 ** and the result row is stored in pDest->nDest registers
18736 ** starting with pDest->iSdst.
18738 ** SRT_Table Store results in temporary table pDest->iSDParm.
18744 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18745 ** But also use temporary table pDest->iSDParm+1 as
18749 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
18753 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18755 ** index at pDest->iSDParm+1 hold all prior stores.
18758 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18759 ** table is an intkey table - in this case the first
18761 ** key. If (pDest->iSDParm>0), then the table is an index
18762 ** table. (pDest->iSDParm) is the number of key columns in
18774 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
18780 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
18808 ** the code generator needs. We have to keep per-table autoincrement
18827 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
18886 ** The nTableLock and aTableLock variables are only used if the shared-cache
18887 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18888 ** used to store the set of table-locks required by the statement being
18918 ** of the base register during check-constraint eval */
18928 int nMaxArg; /* Max args passed to user function by sub-program */
18932 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18977 int nHeight; /* Expression tree height of current sub-select */
18989 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19011 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
19013 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19022 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19028 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19034 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19058 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19062 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19066 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19101 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19102 the <column-list> is stored here */
19121 ** that is a part of a trigger-program.
19126 ** the first step of the trigger-program.
19133 ** orconf -> stores the ON CONFLICT algorithm
19134 ** pSelect -> The content to be inserted - either a SELECT statement or
19136 ** zTarget -> Dequoted name of the table to insert into.
19137 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19138 ** statement, then this stores the column-names to be
19140 ** pUpsert -> The ON CONFLICT clauses for an Upsert
19143 ** zTarget -> Dequoted name of the table to delete from.
19144 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
19148 ** zTarget -> Dequoted name of the table to update.
19149 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19151 ** pExprList -> A list of the columns to update and the expressions to update
19156 ** pSelect -> The SELECT statement
19159 ** pExprList -> The list of expressions that follow the RETURNING keyword.
19175 TriggerStep *pNext; /* Next in the link-list */
19176 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19205 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19209 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19235 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
19238 ** be used on trunk check-ins. They are a temporary mechanism available
19243 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
19245 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
19260 u8 bUseCis; /* Use covering indices for full-scans */
19264 int neverCorrupt; /* Database is always well-formed */
19267 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19268 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19269 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19270 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19280 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19282 /* The above might be initialized to non-zero. The following need to always
19313 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19315 /* vvvv--- must be last ---vvv */
19323 ** the assert is only valid on a well-formed database. Instead of:
19335 ** things that are always true for well-formed databases.
19340 ** Context pointer passed down through the tree-walk.
19381 const char *zType; /* Type of the container - used for error messages */
19382 const Token *pName; /* Name of the container - used for error messages */
19409 ** Return code from the parse-tree walking primitives and their
19470 ** data structures on sqlite3DebugPrintf() using a tree-like view.
19487 ** (2) All window functions in a single SELECT form a linked-list
19557 ** Assuming zIn points to the first byte of a UTF-8 character,
19558 ** advance zIn to point to the first byte of the next UTF-8 character.
19569 ** routines that report the line-number on which the error originated
19614 ** The ctype.h header is needed for non-ASCII systems. It is also
19743 # define MAN754 ((((u64)1)<<52)-1)
19883 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
19884 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
19893 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20153 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20154 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20215 ** Routines to read and write variable-length integers. These used to
20430 ** The interface to the LEMON-generated parser
20491 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20570 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20572 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20574 ** provided (enforcement of FK constraints requires the triggers sub-system).
20625 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
20678 ** sqlite3IoTrace is a pointer to a printf-like routine used to
20711 ** passed back to non-lookaside free() routines. Asserts such as the
20712 ** example above are placed on the non-lookaside free() routines to verify
20715 ** All of this is no-op for a production build. It only comes into
20716 ** play when the SQLITE_MEMDEBUG compile-time option is used.
20723 # define sqlite3MemdebugSetType(X,Y) /* no-op */
20773 ** all of the platform-specific files (os_*.c) and is #included into those
20785 ** switch. The following code should catch this problem at compile-time.
20799 ** high-performance timing routines.
20815 ** This file contains inline asm code for retrieving "high-performance"
20822 ** The following routine only works on pentium-class (or newer) processors.
20824 ** processor and returns that value. This can be used for high-res
20895 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
20919 || sqlite3_io_error_pending-- == 1 ) \
20934 sqlite3_diskfull_pending--; \
20977 ** This file implements routines used to report what compile-time options
20980 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
20984 ** autoconf-based build
20998 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
21004 ** An array of names of all compile-time options. This array should
21005 ** be sorted A-Z.
21008 ** only a handful of compile-time options, so most times this array is usually
21046 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
21050 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
21052 "COMPILER=gcc-" __VERSION__,
21763 /* An array to map all upper-case characters into their corresponding
21764 ** lower-case character.
21766 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
21806 /* All of the upper-to-lower conversion data is above. The following
21823 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
21824 ** be undefined behavior. That's goofy, but the C-standards people thought
21832 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
21833 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
21834 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
21837 ** The following 256 byte lookup table is used to support SQLites built-in
21850 ** case. i.e. if the character is a lower-case ASCII character.
21851 ** If x is a lower-case ASCII character, then its upper-case equivalent
21852 ** is (x - 0x20). Therefore toupper() can be implemented as:
21859 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
21861 ** non-ASCII UTF character. Hence the test for whether or not a character is
21870 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
21902 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
21906 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
21907 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
21909 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
21917 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
21918 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
21919 ** that compile-time option is omitted.
21925 # error "Compile-time disabling of covering index scan using the\
21926 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
21942 ** before 3.12.0). -1 means always keep the entire statement journal in
21952 ** The default lookaside-configuration, the format "SZ,N". SZ is the
21954 ** and N is the number of slots. The lookaside-configuration can be
21955 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
21956 ** or at run-time for an individual database connection using
21959 ** With the two-size-lookaside enhancement, less lookaside is required.
21960 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
21961 ** and 93 128-byte slots, which is more lookaside than is available
21962 ** using the older 1200,100 configuration without two-size-lookaside.
21973 /* The default maximum size of an in-memory database created using
22045 ** Hash table for global functions - functions common to all
22047 ** read-only.
22066 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
22073 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
22242 ** * A b-tree cursor
22243 ** - In the main database or in an ephemeral database
22244 ** - On either an index or a table
22247 ** * A one-row "pseudotable" stored in a single register
22252 i8 iDb; /* Index of cursor database in db->aDb[] */
22261 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
22311 /* Return true if P is a null-only cursor
22314 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
22323 ** When a sub-program is executed (OP_Program), a structure of this type
22326 ** values stored in the Vdbe struct. When the sub-program is finished,
22328 ** restoring the state of the VM to as it was before the sub-program
22365 i64 nDbChange; /* Value of db->nChange */
22400 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
22419 ** UPDATE no-change flag set
22425 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
22429 ** length Mem.n. Zero-terminated if
22485 /* Return TRUE if Mem X contains dynamically allocated content - anything
22489 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
22495 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
22498 ** True if Mem X is a NULL-nochng type.
22501 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
22502 && (X)->n==0 && (X)->u.nZero==0)
22513 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
22570 int iSelectID; /* The "Select-ID" for this loop */
22575 /* The DblquoteStr object holds the text of a double-quoted
22579 ** list is consulted for each double-quoted identifier to see if the
22627 i64 startTime; /* Time when query started - used for profiling */
22640 bft changeCntOn:1; /* True to update the change-counter */
22644 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
22650 DblquoteStr *pDblStr; /* List of double-quoted string literals */
22657 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
22698 ** set of values on the right-hand side of an IN constraint.
22714 ** single-byte varint.
22869 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
22914 ** we have to locate the state vector at run-time. In the more common
22944 ** The value of N is added to the current status value and the high-water
22948 ** mark is unchanged. N must be non-negative for StatusDown().
22968 wsdStat.nowValue[op] -= N;
23040 p = p->pNext;
23050 u32 nInit = countLookasideSlots(db->lookaside.pInit);
23051 u32 nFree = countLookasideSlots(db->lookaside.pFree);
23053 nInit += countLookasideSlots(db->lookaside.pSmallInit);
23054 nFree += countLookasideSlots(db->lookaside.pSmallFree);
23056 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
23057 return db->lookaside.nSlot - (nInit+nFree);
23067 int *pHighwater, /* Write high-water mark here */
23068 int resetFlag /* Reset high-water mark if true */
23076 sqlite3_mutex_enter(db->mutex);
23081 LookasideSlot *p = db->lookaside.pFree;
23083 while( p->pNext ) p = p->pNext;
23084 p->pNext = db->lookaside.pInit;
23085 db->lookaside.pInit = db->lookaside.pFree;
23086 db->lookaside.pFree = 0;
23089 p = db->lookaside.pSmallFree;
23091 while( p->pNext ) p = p->pNext;
23092 p->pNext = db->lookaside.pSmallInit;
23093 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
23094 db->lookaside.pSmallFree = 0;
23107 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
23108 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
23110 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
23112 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
23127 for(i=0; i<db->nDb; i++){
23128 Btree *pBt = db->aDb[i].pBt;
23154 db->pnBytesFreed = &nByte;
23155 for(i=0; i<db->nDb; i++){
23156 Schema *pSchema = db->aDb[i].pSchema;
23161 pSchema->tblHash.count
23162 + pSchema->trigHash.count
23163 + pSchema->idxHash.count
23164 + pSchema->fkeyHash.count
23166 nByte += sqlite3_msize(pSchema->tblHash.ht);
23167 nByte += sqlite3_msize(pSchema->trigHash.ht);
23168 nByte += sqlite3_msize(pSchema->idxHash.ht);
23169 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
23171 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
23174 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
23179 db->pnBytesFreed = 0;
23196 db->pnBytesFreed = &nByte;
23197 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
23200 db->pnBytesFreed = 0;
23202 *pHighwater = 0; /* IMP: R-64479-57858 */
23224 for(i=0; i<db->nDb; i++){
23225 if( db->aDb[i].pBt ){
23226 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
23230 *pHighwater = 0; /* IMP: R-42420-56072 */
23231 /* IMP: R-54100-20147 */
23232 /* IMP: R-29431-39229 */
23237 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
23242 *pHighwater = 0; /* IMP: R-11967-56545 */
23243 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
23251 sqlite3_mutex_leave(db->mutex);
23271 ** There is only one exported symbol in this file - the function
23280 ** 1970-01-01 00:00:00 is JD 2440587.5
23281 ** 2000-01-01 00:00:00 is JD 2451544.5
23283 ** This implementation requires years to be expressed as a 4-digit number
23284 ** which means that only dates between 0000-01-01 and 9999-12-31 can
23290 ** use the julian calendar for dates prior to 1582-10-15 and for some
23298 ** ISBN 0-943396-61-1
23299 ** Willmann-Bell, Inc
23345 ** of a four-character format specifiers ABCD is:
23359 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
23360 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
23361 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
23362 ** the 2-digit day which is the last integer in the set.
23375 char N = zFormat[0] - '0';
23376 char min = zFormat[1] - '0';
23381 max = aMx[zFormat[2] - 'a'];
23384 while( N-- ){
23388 val = val*10 + *zDate - '0';
23405 ** Parse a timezone extension on the end of a date-time.
23408 ** (+/-)HH:MM
23415 ** of change in p->tz and return 0. If a parser error occurs,
23416 ** return non-zero.
23425 p->tz = 0;
23427 if( c=='-' ){
23428 sgn = -1;
23442 p->tz = sgn*(nMn + nHr*60);
23445 p->tzSet = 1;
23473 ms = ms*10.0 + *zDate - '0';
23482 p->validJD = 0;
23483 p->rawS = 0;
23484 p->validHMS = 1;
23485 p->h = h;
23486 p->m = m;
23487 p->s = s + ms;
23489 p->validTZ = (p->tz!=0)?1:0;
23498 p->isError = 1;
23502 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
23503 ** that the YYYY-MM-DD is according to the Gregorian calendar.
23510 if( p->validJD ) return;
23511 if( p->validYMD ){
23512 Y = p->Y;
23513 M = p->M;
23514 D = p->D;
23516 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */
23520 if( Y<-4713 || Y>9999 || p->rawS ){
23525 Y--;
23529 B = 2 - A + (A/4);
23532 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
23533 p->validJD = 1;
23534 if( p->validHMS ){
23535 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
23536 if( p->validTZ ){
23537 p->iJD -= p->tz*60000;
23538 p->validYMD = 0;
23539 p->validHMS = 0;
23540 p->validTZ = 0;
23548 ** YYYY-MM-DD HH:MM:SS.FFF
23549 ** YYYY-MM-DD HH:MM:SS
23550 ** YYYY-MM-DD HH:MM
23551 ** YYYY-MM-DD
23554 ** on success and 1 if the input string is not a well-formed
23560 if( zDate[0]=='-' ){
23566 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){
23574 p->validHMS = 0;
23578 p->validJD = 0;
23579 p->validYMD = 1;
23580 p->Y = neg ? -Y : Y;
23581 p->M = M;
23582 p->D = D;
23583 if( p->validTZ ){
23595 p->iJD = sqlite3StmtCurrentTime(context);
23596 if( p->iJD>0 ){
23597 p->validJD = 1;
23608 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
23611 p->s = r;
23612 p->rawS = 1;
23614 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
23615 p->validJD = 1;
23625 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
23629 ** In the first form, the +/-HH:MM is always optional. The fractional
23654 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
23677 if( p->validYMD ) return;
23678 if( !p->validJD ){
23679 p->Y = 2000;
23680 p->M = 1;
23681 p->D = 1;
23682 }else if( !validJulianDay(p->iJD) ){
23686 Z = (int)((p->iJD + 43200000)/86400000);
23687 A = (int)((Z - 1867216.25)/36524.25);
23688 A = Z + 1 + A - (A/4);
23690 C = (int)((B - 122.1)/365.25);
23692 E = (int)((B-D)/30.6001);
23694 p->D = B - D - X1;
23695 p->M = E<14 ? E-1 : E-13;
23696 p->Y = p->M>2 ? C - 4716 : C - 4715;
23698 p->validYMD = 1;
23706 if( p->validHMS ) return;
23708 s = (int)((p->iJD + 43200000) % 86400000);
23709 p->s = s/1000.0;
23710 s = (int)p->s;
23711 p->s -= s;
23712 p->h = s/3600;
23713 s -= p->h*3600;
23714 p->m = s/60;
23715 p->s += s - p->m*60;
23716 p->rawS = 0;
23717 p->validHMS = 1;
23732 p->validYMD = 0;
23733 p->validHMS = 0;
23734 p->validTZ = 0;
23744 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
23758 ** using whatever operating-system specific localtime facility that
23760 ** non-zero on any kind of error.
23762 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
23765 ** invoked in place of the OS-defined localtime() function.
23767 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
23833 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */
23834 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */
23836 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
23843 iYearDiff = (2000 + x.Y%4) - x.Y;
23847 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
23850 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000);
23853 sqlite3_result_error(pCtx, "local time unavailable", -1);
23856 p->Y = sLocal.tm_year + 1900 - iYearDiff;
23857 p->M = sLocal.tm_mon + 1;
23858 p->D = sLocal.tm_mday;
23859 p->h = sLocal.tm_hour;
23860 p->m = sLocal.tm_min;
23861 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001;
23862 p->validYMD = 1;
23863 p->validHMS = 1;
23864 p->validJD = 0;
23865 p->rawS = 0;
23866 p->validTZ = 0;
23867 p->isError = 0;
23877 ** Where NNN is an arbitrary floating-point number and "days" can be one
23895 ** Process a modifier to a date-time stamp. The modifiers are
23936 if( idx>1 ) return 1; /* IMP: R-33611-57934 */
23937 if( !p->rawS || p->validJD ){
23939 p->rawS = 0;
23940 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
23941 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
23943 r = p->s*1000.0 + 210866760000000.0;
23945 p->iJD = (sqlite3_int64)(r + 0.5);
23946 p->validJD = 1;
23947 p->rawS = 0;
23957 ** Always interpret the prior number as a julian-day value. If this
23963 if( idx>1 ) return 1; /* IMP: R-31176-64601 */
23964 if( p->validJD && p->rawS ){
23966 p->rawS = 0;
23988 ** Treat the current value of p->s as the number of
23991 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
23992 if( idx>1 ) return 1; /* IMP: R-49255-55373 */
23993 r = p->s*1000.0 + 210866760000000.0;
23996 p->iJD = (sqlite3_int64)(r + 0.5);
23997 p->validJD = 1;
23998 p->rawS = 0;
24004 if( p->tzSet==0 ){
24011 iGuess = iOrigJD = p->iJD;
24016 iGuess -= iErr;
24022 iErr = new.iJD - iOrigJD;
24025 p->iJD = iGuess;
24026 p->validJD = 1;
24027 p->tzSet = 1;
24040 ** date is already on the appropriate weekday, this is a no-op.
24047 p->validTZ = 0;
24048 p->validJD = 0;
24050 Z = ((p->iJD + 129600000)/86400000) % 7;
24051 if( Z>n ) Z -= 7;
24052 p->iJD += (n - Z)*86400000;
24066 if( !p->validJD && !p->validYMD && !p->validHMS ) break;
24069 p->validHMS = 1;
24070 p->h = p->m = 0;
24071 p->s = 0.0;
24072 p->rawS = 0;
24073 p->validTZ = 0;
24074 p->validJD = 0;
24076 p->D = 1;
24079 p->M = 1;
24080 p->D = 1;
24088 case '-':
24107 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
24119 tx.iJD -= 43200000;
24121 tx.iJD -= day*86400000;
24122 if( z[0]=='-' ) tx.iJD = -tx.iJD;
24125 p->iJD += tx.iJD;
24136 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
24139 rRounder = r<0 ? -0.5 : +0.5;
24143 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
24150 p->M += (int)r;
24151 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
24152 p->Y += x;
24153 p->M -= x*12;
24154 p->validJD = 0;
24155 r -= (int)r;
24162 p->Y += y;
24163 p->validJD = 0;
24164 r -= (int)r;
24169 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder);
24185 ** Process time function arguments. argv[0] is a date-time stamp.
24222 if( p->isError || !validJulianDay(p->iJD) ) return 1;
24253 ** the unix epoch of 1970-01-01 00:00:00 GMT.
24263 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000);
24270 ** Return YYYY-MM-DD HH:MM:SS
24283 if( Y<0 ) Y = -Y;
24288 zBuf[5] = '-';
24291 zBuf[8] = '-';
24306 zBuf[0] = '-';
24346 ** Return YYYY-MM-DD
24359 if( Y<0 ) Y = -Y;
24364 zBuf[5] = '-';
24367 zBuf[8] = '-';
24372 zBuf[0] = '-';
24387 ** %H hour 00-24
24388 ** %j day of year 000-366
24390 ** %m month 01-12
24391 ** %M minute 00-59
24392 ** %s seconds since 1970-01-01
24393 ** %S seconds 00-59
24394 ** %w day of week 0-6 sunday==0
24395 ** %W week of year 00-53
24396 ** %Y year 0000-9999
24413 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
24415 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
24421 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24447 nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
24451 sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
24470 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000);
24497 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
24546 ** If the library is compiled to omit the full-scale date and time
24552 ** This function uses the C-library functions time(), gmtime()
24554 ** as the user-data for the function.
24573 t = iT/1000 - 10000*(sqlite3_int64)21086676;
24584 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
24597 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ),
24598 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ),
24599 PURE_DATE(date, -1, 0, 0, dateFunc ),
24600 PURE_DATE(time, -1, 0, 0, timeFunc ),
24601 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
24602 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
24608 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc),
24609 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
24641 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
24659 ** from within OsOpen()), but some third-party implementations may.
24699 if( pId->pMethods ){
24700 pId->pMethods->xClose(pId);
24701 pId->pMethods = 0;
24706 return id->pMethods->xRead(id, pBuf, amt, offset);
24710 return id->pMethods->xWrite(id, pBuf, amt, offset);
24713 return id->pMethods->xTruncate(id, size);
24717 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
24721 return id->pMethods->xFileSize(id, pSize);
24725 return id->pMethods->xLock(id, lockType);
24728 return id->pMethods->xUnlock(id, lockType);
24732 return id->pMethods->xCheckReservedLock(id, pResOut);
24744 if( id->pMethods==0 ) return SQLITE_NOTFOUND;
24754 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
24762 ** The CKPT_DONE and CKPT_START file-controls are write-only signals
24769 return id->pMethods->xFileControl(id, op, pArg);
24772 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
24776 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
24780 if( NEVER(id->pMethods==0) ) return 0;
24781 return id->pMethods->xDeviceCharacteristics(id);
24785 return id->pMethods->xShmLock(id, offset, n, flags);
24788 id->pMethods->xShmBarrier(id);
24791 return id->pMethods->xShmUnmap(id, deleteFlag);
24801 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
24809 return id->pMethods->xFetch(id, iOff, iAmt, pp);
24812 return id->pMethods->xUnfetch(id, iOff, p);
24815 /* No-op stubs to use when memory-mapped I/O is disabled */
24842 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut);
24843 assert( rc==SQLITE_OK || pFile->pMethods==0 );
24849 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK;
24858 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
24868 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
24873 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */
24874 return pVfs->xDlOpen(pVfs, zPath);
24877 pVfs->xDlError(pVfs, nByte, zBufOut);
24880 return pVfs->xDlSym(pVfs, pHdle, zSym);
24883 pVfs->xDlClose(pVfs, pHandle);
24893 return pVfs->xRandomness(pVfs, nByte, zBufOut);
24898 return pVfs->xSleep(pVfs, nMicro);
24901 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
24905 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
24911 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
24912 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
24915 rc = pVfs->xCurrentTime(pVfs, &r);
24930 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
24988 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
24990 if( strcmp(zVfs, pVfs->zName)==0 ) break;
25002 /* No-op */
25004 vfsList = pVfs->pNext;
25007 while( p->pNext && p->pNext!=pVfs ){
25008 p = p->pNext;
25010 if( p->pNext==pVfs ){
25011 p->pNext = pVfs->pNext;
25035 pVfs->pNext = vfsList;
25038 pVfs->pNext = vfsList->pNext;
25039 vfsList->pNext = pVfs;
25081 ** Most malloc failures are non-benign. After they occur, SQLite
25105 ** we have to locate the state vector at run-time. In the more common
25135 ** indicates that subsequent malloc failures are non-benign.
25166 ** This file contains a no-op memory allocation drivers for use when
25176 ** used when no other memory allocator is specified using compile-time
25182 ** No-op versions of all memory allocation routines
25195 ** Populate the low-level memory allocation function pointers in
25228 ** This file contains low-level memory allocation drivers for when
25229 ** SQLite will use the standard C-library malloc/realloc/free interface
25232 ** This file contains implementations of the low-level memory allocation
25240 ** C-preprocessor macro summary:
25247 ** a different name, using a separate -D
25263 ** used when no other memory allocator is specified using compile-time
25283 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
25288 ** Use standard C library malloc and free on non-Apple systems.
25305 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
25339 ** For this low-level routine, we are guaranteed that nByte>0 because
25373 ** For this low-level routine, we already know that pPrior!=0 since
25375 ** by higher-level routines.
25383 p--;
25400 p--;
25409 ** For this low-level interface, we know that pPrior!=0. Cases where
25410 ** pPrior==0 while have been intercepted by higher-level routine and
25412 ** cases where nByte<=0 will have been intercepted by higher-level
25428 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
25429 p--;
25489 ** Populate the low-level memory allocation function pointers in
25522 ** This file contains low-level memory allocation drivers for when
25523 ** SQLite will use the standard C-library malloc/realloc/free interface
25528 ** This file contains implementations of the low-level memory allocation
25554 ** ------------------------------------------------------------------------
25556 ** ------------------------------------------------------------------------
25640 if( i>NCSIZE-1 ){
25641 i = NCSIZE - 1;
25650 mem.nCurrent[i]--;
25668 p--;
25669 assert( p->iForeGuard==(int)FOREGUARD );
25670 nReserve = ROUND8(p->iSize);
25678 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
25691 return (int)pHdr->iSize;
25724 ** Fill a buffer with pseudo-random bytes. This is used to preset
25733 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25738 nByte -= 4;
25740 while( nByte-- > 0 ){
25741 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
25769 pHdr->pNext = 0;
25770 pHdr->pPrev = mem.pLast;
25772 mem.pLast->pNext = pHdr;
25777 pHdr->iForeGuard = FOREGUARD;
25778 pHdr->eType = MEMTYPE_HEAP;
25779 pHdr->nBacktraceSlots = mem.nBacktrace;
25780 pHdr->nTitle = mem.nTitle;
25783 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
25784 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
25787 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
25790 pHdr->nBacktrace = 0;
25795 pHdr->iSize = nByte;
25800 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
25818 pBt -= pHdr->nBacktraceSlots;
25820 if( pHdr->pPrev ){
25821 assert( pHdr->pPrev->pNext==pHdr );
25822 pHdr->pPrev->pNext = pHdr->pNext;
25825 mem.pFirst = pHdr->pNext;
25827 if( pHdr->pNext ){
25828 assert( pHdr->pNext->pPrev==pHdr );
25829 pHdr->pNext->pPrev = pHdr->pPrev;
25832 mem.pLast = pHdr->pPrev;
25835 z -= pHdr->nTitle;
25836 adjustStats((int)pHdr->iSize, -1);
25837 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
25838 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
25856 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
25860 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
25861 if( nByte>pOldHdr->iSize ){
25862 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
25870 ** Populate the low-level memory allocation function pointers in
25894 assert( pHdr->iForeGuard==FOREGUARD );
25895 pHdr->eType = eType;
25913 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
25914 if( (pHdr->eType&eType)==0 ){
25935 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */
25936 if( (pHdr->eType&eType)!=0 ){
25965 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
25974 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25976 pBt -= pHdr->nBacktraceSlots;
25977 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
25996 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
25998 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
26000 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
26001 if( pHdr->nBacktrace ){
26004 pBt -= pHdr->nBacktraceSlots;
26005 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
26010 for(i=0; i<NCSIZE-1; i++){
26016 if( mem.nAlloc[NCSIZE-1] ){
26018 NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
26019 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
26071 ** mean that the library will use a memory-pool by default, just that
26112 ** two fields form a double-linked list of chunks of related sizes.
26149 ** True if we are evaluating an out-of-memory callback.
26177 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
26210 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26212 size = mem3.aPool[i-1].u.hdr.size4x/4;
26213 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26216 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
26245 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
26246 size = mem3.aPool[i-1].u.hdr.size4x/4;
26247 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
26250 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
26289 ** size parameters for check-out and return a pointer to the
26296 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
26297 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
26298 x = mem3.aPool[i-1].u.hdr.size4x;
26299 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
26300 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
26301 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
26313 if( nBlock>=mem3.szKeyBlk-1 ){
26323 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock;
26325 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock;
26326 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2;
26327 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
26328 mem3.szKeyBlk -= nBlock;
26329 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk;
26330 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26331 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26361 size = mem3.aPool[i-1].u.hdr.size4x;
26365 assert( i > mem3.aPool[i-1].u.hdr.prevSize );
26366 prev = i - mem3.aPool[i-1].u.hdr.prevSize;
26371 size = i + size/4 - prev;
26372 x = mem3.aPool[prev-1].u.hdr.size4x & 2;
26373 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
26374 mem3.aPool[prev+size-1].u.hdr.prevSize = size;
26414 i = mem3.aiSmall[nBlock-2];
26416 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
26422 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
26455 for(i=0; i<MX_SMALL-1; i++){
26482 i = p - mem3.aPool;
26483 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
26484 size = mem3.aPool[i-1].u.hdr.size4x/4;
26486 mem3.aPool[i-1].u.hdr.size4x &= ~1;
26487 mem3.aPool[i+size-1].u.hdr.prevSize = size;
26488 mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
26493 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){
26494 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize;
26495 mem3.iKeyBlk -= size;
26498 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26499 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26500 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26502 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2;
26503 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){
26505 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4;
26506 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x;
26507 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk;
26514 ** size returned omits the 8-byte header overhead. This only
26521 assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
26522 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
26532 return ((n+11)&~7) - 4;
26572 if( nBytes<=nOld && nBytes>=nOld-128 ){
26601 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
26647 size = mem3.aPool[i-1].u.hdr.size4x;
26653 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
26658 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
26664 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
26666 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
26670 for(i=0; i<MX_SMALL-1; i++){
26675 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26684 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
26689 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8);
26690 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8);
26706 ** Populate the low-level memory allocation function pointers in
26767 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
26776 ** N >= M*(1 + log2(n)/2) - n + 1
26805 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
26840 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
26871 ** structures, return a pointer to the idx-th such link.
26885 next = MEM5LINK(i)->next;
26886 prev = MEM5LINK(i)->prev;
26890 MEM5LINK(prev)->next = next;
26893 MEM5LINK(next)->prev = prev;
26908 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
26909 MEM5LINK(i)->prev = -1;
26912 MEM5LINK(x)->prev = i;
26934 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
26989 iBin--;
27000 mem5.totalExcess += iFullSz - nByte;
27027 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
27029 /* Check that the pointer pOld points to a valid, non-free block. */
27031 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
27036 assert( iBlock+size-1<(u32)mem5.nBlock );
27039 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
27044 mem5.currentCount--;
27045 mem5.currentOut -= size*mem5.szAtom;
27054 iBuddy = iBlock - size;
27116 ** memsys5Round(). Hence nBytes is always a non-negative power
27125 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */
27148 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
27166 ** Examples: memsys5Log(1) -> 0
27167 ** memsys5Log(2) -> 1
27168 ** memsys5Log(4) -> 2
27169 ** memsys5Log(5) -> 3
27170 ** memsys5Log(8) -> 3
27171 ** memsys5Log(9) -> 4
27175 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
27200 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
27218 mem5.aiFreelist[ii] = -1;
27222 for(ii=LOGMAX; ii>=0; ii--){
27272 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
27363 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
27374 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
27384 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
27387 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
27418 p->iType = iType;
27421 if( iType-2>=ArraySize(staticMutexes) ){
27426 p = &staticMutexes[iType-2];
27429 if( p->mutex==0 ){
27430 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
27431 if( p->mutex==0 ){
27451 if( ((CheckMutex*)p)->iType<2 )
27455 pGlobalMutexMethods->xMutexFree(pCheck->mutex);
27470 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
27471 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
27475 "illegal multi-threaded access to database connection"
27478 pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
27486 return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
27494 pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
27524 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
27525 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
27553 pTo->xMutexInit = pFrom->xMutexInit;
27554 pTo->xMutexEnd = pFrom->xMutexEnd;
27555 pTo->xMutexFree = pFrom->xMutexFree;
27556 pTo->xMutexEnter = pFrom->xMutexEnter;
27557 pTo->xMutexTry = pFrom->xMutexTry;
27558 pTo->xMutexLeave = pFrom->xMutexLeave;
27559 pTo->xMutexHeld = pFrom->xMutexHeld;
27560 pTo->xMutexNotheld = pFrom->xMutexNotheld;
27562 pTo->xMutexAlloc = pFrom->xMutexAlloc;
27651 ** this function is a no-op.
27695 ** here are place-holders. Applications can substitute working
27696 ** mutex routines at start-time using the
27769 return p==0 || p->cnt>0;
27773 return p==0 || p->cnt==0;
27788 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1];
27795 pNew->id = id;
27796 pNew->cnt = 0;
27802 if( id-2<0 || id-2>=ArraySize(aStatic) ){
27807 pNew = &aStatic[id-2];
27808 pNew->id = id;
27820 assert( p->cnt==0 );
27821 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){
27843 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27844 p->cnt++;
27848 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27849 p->cnt++;
27862 p->cnt--;
27863 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
27885 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
27886 ** is used regardless of the run-time threadsafety setting.
27926 ** home-grown mutexes. Encapsulate these conditions into a single #define.
27969 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
27975 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
27978 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
28031 ** cases where it really needs one. If a faster non-recursive mutex
28072 pthread_mutex_init(&p->mutex, 0);
28078 pthread_mutex_init(&p->mutex, &recursiveAttr);
28082 p->id = SQLITE_MUTEX_RECURSIVE;
28090 pthread_mutex_init(&p->mutex, 0);
28092 p->id = SQLITE_MUTEX_FAST;
28099 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
28104 p = &staticMutexes[iType-2];
28109 assert( p==0 || p->id==iType );
28121 assert( p->nRef==0 );
28123 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
28126 pthread_mutex_destroy(&p->mutex);
28148 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28153 ** is atomic - that it cannot be deceived into thinking self
28154 ** and p->owner are equal if p->owner changes between two values
28156 ** This implementation also assumes a coherent cache - that
28163 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28164 p->nRef++;
28166 pthread_mutex_lock(&p->mutex);
28167 assert( p->nRef==0 );
28168 p->owner = self;
28169 p->nRef = 1;
28173 /* Use the built-in recursive mutexes if they are available.
28175 pthread_mutex_lock(&p->mutex);
28177 assert( p->nRef>0 || p->owner==0 );
28178 p->owner = pthread_self();
28179 p->nRef++;
28184 if( p->trace ){
28185 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28191 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
28196 ** is atomic - that it cannot be deceived into thinking self
28197 ** and p->owner are equal if p->owner changes between two values
28199 ** This implementation also assumes a coherent cache - that
28206 if( p->nRef>0 && pthread_equal(p->owner, self) ){
28207 p->nRef++;
28209 }else if( pthread_mutex_trylock(&p->mutex)==0 ){
28210 assert( p->nRef==0 );
28211 p->owner = self;
28212 p->nRef = 1;
28219 /* Use the built-in recursive mutexes if they are available.
28221 if( pthread_mutex_trylock(&p->mutex)==0 ){
28223 p->owner = pthread_self();
28224 p->nRef++;
28233 if( rc==SQLITE_OK && p->trace ){
28234 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28249 p->nRef--;
28250 if( p->nRef==0 ) p->owner = 0;
28252 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
28255 if( p->nRef==0 ){
28256 pthread_mutex_unlock(&p->mutex);
28259 pthread_mutex_unlock(&p->mutex);
28263 if( p->trace ){
28264 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
28369 ** Determine if we are dealing with Windows CE - which has a much reduced
28397 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
28452 return p->nRef!=0 && p->owner==GetCurrentThreadId();
28456 return p->nRef==0 || p->owner!=tid;
28501 static int winMutex_isNt = -1; /* <0 means "need to query" */
28579 ** cases where it really needs one. If a faster non-recursive mutex
28605 p->id = iType;
28608 p->trace = 1;
28612 InitializeCriticalSectionEx(&p->mutex, 0, 0);
28614 InitializeCriticalSection(&p->mutex);
28621 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
28626 p = &winMutex_staticMutexes[iType-2];
28629 InterlockedCompareExchange(&p->trace, 1, 0);
28635 assert( p==0 || p->id==iType );
28647 assert( p->nRef==0 && p->owner==0 );
28648 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
28649 DeleteCriticalSection(&p->mutex);
28675 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
28680 EnterCriticalSection(&p->mutex);
28682 assert( p->nRef>0 || p->owner==0 );
28683 p->owner = tid;
28684 p->nRef++;
28685 if( p->trace ){
28686 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28687 tid, p->id, p, p->trace, p->nRef));
28698 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
28712 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
28717 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
28719 p->owner = tid;
28720 p->nRef++;
28728 if( p->trace ){
28729 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
28730 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
28748 assert( p->nRef>0 );
28749 assert( p->owner==tid );
28750 p->nRef--;
28751 if( p->nRef==0 ) p->owner = 0;
28752 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
28755 LeaveCriticalSection(&p->mutex);
28757 if( p->trace ){
28758 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
28759 tid, p->id, p, p->trace, p->nRef));
28806 ** Attempt to release up to n bytes of non-essential memory currently
28807 ** held by SQLite. An example of non-essential memory is memory used to
28814 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
28815 ** is a no-op returning zero if SQLite is not compiled with
28857 ** no-op.
28872 ** Set the soft heap-size limit for the library. An argument of
28873 ** zero disables the limit. A negative argument is a no-op used to
28888 if( rc ) return -1;
28903 excess = sqlite3_memory_used() - n;
28913 ** Set the hard heap-size limit for the library. An argument of zero
28914 ** disables the hard heap limit. A negative argument is a no-op used
28928 if( rc ) return -1;
28963 ** Return true if the heap is currently under memory pressure - in other
29021 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
29031 if( nUsed >= mem0.alarmThreshold - nFull ){
29036 if( nUsed >= mem0.hardLimit - nFull ){
29080 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
29107 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
29123 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
29125 return db->lookaside.szTrue;
29142 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29144 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29145 assert( sqlite3_mutex_held(db->mutex) );
29149 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29150 assert( sqlite3_mutex_held(db->mutex) );
29151 return db->lookaside.szTrue;
29167 if( p==0 ) return; /* IMP: R-49053-54554 */
29183 ** *db->pnBytesFreed.
29186 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
29191 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
29192 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
29195 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29198 if( db->pnBytesFreed ){
29202 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
29204 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
29209 pBuf->pNext = db->lookaside.pSmallFree;
29210 db->lookaside.pSmallFree = pBuf;
29214 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
29217 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
29219 pBuf->pNext = db->lookaside.pFree;
29220 db->lookaside.pFree = pBuf;
29232 assert( db==0 || sqlite3_mutex_held(db->mutex) );
29245 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
29248 sqlite3_free(pOld); /* IMP: R-26507-47431 */
29256 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
29266 nDiff = nNew - nOld;
29268 mem0.alarmThreshold-nDiff ){
29270 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
29284 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
29290 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
29302 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
29346 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
29355 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
29383 assert( sqlite3_mutex_held(db->mutex) );
29384 assert( db->pnBytesFreed==0 );
29385 if( n>db->lookaside.sz ){
29386 if( !db->lookaside.bDisable ){
29387 db->lookaside.anStat[1]++;
29388 }else if( db->mallocFailed ){
29395 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
29396 db->lookaside.pSmallFree = pBuf->pNext;
29397 db->lookaside.anStat[0]++;
29399 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
29400 db->lookaside.pSmallInit = pBuf->pNext;
29401 db->lookaside.anStat[0]++;
29406 if( (pBuf = db->lookaside.pFree)!=0 ){
29407 db->lookaside.pFree = pBuf->pNext;
29408 db->lookaside.anStat[0]++;
29410 }else if( (pBuf = db->lookaside.pInit)!=0 ){
29411 db->lookaside.pInit = pBuf->pNext;
29412 db->lookaside.anStat[0]++;
29415 db->lookaside.anStat[2]++;
29419 assert( sqlite3_mutex_held(db->mutex) );
29420 assert( db->pnBytesFreed==0 );
29421 if( db->mallocFailed ){
29438 assert( sqlite3_mutex_held(db->mutex) );
29439 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
29441 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
29445 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
29446 if( n<=db->lookaside.szTrue ) return p;
29455 if( db->mallocFailed==0 ){
29471 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
29531 n = (int)(zEnd - zStart);
29532 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
29546 ** Call this routine to record the fact that an OOM (out-of-memory) error
29547 ** has happened. This routine will set db->mallocFailed, and also
29559 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
29560 db->mallocFailed = 1;
29561 if( db->nVdbeExec>0 ){
29562 AtomicStore(&db->u1.isInterrupted, 1);
29565 if( db->pParse ){
29566 sqlite3ErrorMsg(db->pParse, "out of memory");
29567 db->pParse->rc = SQLITE_NOMEM_BKPT;
29575 ** db->mallocFailed flag as necessary.
29581 if( db->mallocFailed && db->nVdbeExec==0 ){
29582 db->mallocFailed = 0;
29583 AtomicStore(&db->u1.isInterrupted, 0);
29584 assert( db->lookaside.bDisable>0 );
29593 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
29598 return rc & db->errMask;
29610 ** If an OOM as occurred, then the connection error-code (the value
29615 ** Otherwise the read (and possible write) of db->mallocFailed
29619 assert( sqlite3_mutex_held(db->mutex) );
29620 if( db->mallocFailed || rc ){
29623 return rc & db->errMask;
29634 ** This file contains code for a set of "printf"-like routines. These
29645 #define etRADIX 0 /* non-decimal integer types. %x %o */
29661 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
29662 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
29669 ** An "etByte" is an 8-bit unsigned value.
29698 static const char aPrefix[] = "-x0\000X0";
29737 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
29738 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
29756 ** 16 (the number of significant digits in a 64-bit float) '0' is
29763 (*cnt)--;
29767 *val = (*val - d)*10.0;
29777 p->accError = eError;
29778 if( p->mxAlloc ) sqlite3_str_reset(p);
29779 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
29786 if( p->nArg<=p->nUsed ) return 0;
29787 return sqlite3_value_int64(p->apArg[p->nUsed++]);
29790 if( p->nArg<=p->nUsed ) return 0.0;
29791 return sqlite3_value_double(p->apArg[p->nUsed++]);
29794 if( p->nArg<=p->nUsed ) return 0;
29795 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
29809 if( pAccum->accError ) return 0;
29810 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
29814 z = sqlite3DbMallocRaw(pAccum->db, n);
29831 ** Hard limit on the precision of floating-point conversions.
29851 etByte flag_leftjustify; /* True if "-" flag is present */
29861 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
29881 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
29884 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
29898 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
29911 precision = -1;
29914 case '-': flag_leftjustify = 1; break;
29934 unsigned wx = c - '0';
29936 wx = wx*10 + c - '0';
29948 fmt--;
29960 width = width >= -2147483647 ? -width : 0;
29982 precision = precision >= -2147483647 ? -precision : -1;
29988 px = px*10 + c - '0';
30000 --fmt;
30015 xtype = infop->type;
30026 ** flag_leftjustify TRUE if a '-' is present or if the
30031 ** always non-negative. Zero is the default.
30033 ** is -1.
30038 assert( precision>=(-1) );
30049 if( infop->flags & FLAG_SIGNED ){
30064 testcase( v==(-1) );
30067 prefix = '-';
30087 if( flag_zeropad && precision<width-(prefix!=0) ){
30088 precision = width-(prefix!=0);
30090 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
30101 bufpt = &zOut[nOut-1];
30108 *(--bufpt) = zOrd[x*2+1];
30109 *(--bufpt) = zOrd[x*2];
30112 const char *cset = &aDigits[infop->charset];
30113 u8 base = infop->base;
30115 *(--bufpt) = cset[longvalue%base];
30119 length = (int)(&zOut[nOut-1]-bufpt);
30121 *(--bufpt) = '0'; /* Zero pad */
30125 int nn = (length - 1)/3; /* Number of "," to insert */
30126 int ix = (length - 1)%3 + 1;
30127 bufpt -= nn;
30130 ix--;
30133 nn--;
30138 if( prefix ) *(--bufpt) = prefix; /* Add sign */
30139 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
30142 pre = &aPrefix[infop->prefix];
30143 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
30145 length = (int)(&zOut[nOut-1]-bufpt);
30165 realvalue = -realvalue;
30166 prefix = '-';
30170 if( xtype==etGENERIC && precision>0 ) precision--;
30174 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
30180 ex = -1023 + (int)((u>>52)&0x7ff);
30181 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
30197 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
30198 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
30218 if( exp<-4 || exp>precision ){
30221 precision = precision - exp;
30251 for(; e2>=0; e2--){
30261 for(e2++; e2<0; precision--, e2++){
30266 while( (precision--)>0 ){
30271 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
30273 if( bufpt[-1]=='.' ){
30277 *(--bufpt) = 0;
30283 *(bufpt++) = aDigits[infop->charset];
30285 *(bufpt++) = '-'; exp = -exp;
30301 length = (int)(bufpt-zOut);
30308 int nPad = width - length;
30309 for(i=width; i>=nPad; i--){
30310 bufpt[i] = bufpt[i-nPad];
30313 while( nPad-- ) bufpt[i++] = '0';
30320 *(va_arg(ap,int*)) = pAccum->nChar;
30366 width -= precision-1;
30368 sqlite3_str_appendchar(pAccum, width-1, ' ');
30371 while( precision-- > 1 ){
30389 if( pAccum->nChar==0
30390 && pAccum->mxAlloc
30393 && pAccum->accError==0
30398 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
30399 pAccum->zText = bufpt;
30400 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
30401 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
30402 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
30413 while( precision-- > 0 && z[0] ){
30416 length = (int)(z - (unsigned char*)bufpt);
30425 /* Adjust width to account for extra bytes in UTF-8 characters */
30426 int ii = length - 1;
30427 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
30452 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
30479 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30484 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken);
30485 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr);
30491 if( pToken && pToken->n ){
30492 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
30493 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z);
30501 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
30504 if( pItem->zAlias && !flag_altform2 ){
30505 sqlite3_str_appendall(pAccum, pItem->zAlias);
30506 }else if( pItem->zName ){
30507 if( pItem->zDatabase ){
30508 sqlite3_str_appendall(pAccum, pItem->zDatabase);
30511 sqlite3_str_appendall(pAccum, pItem->zName);
30512 }else if( pItem->zAlias ){
30513 sqlite3_str_appendall(pAccum, pItem->zAlias);
30515 Select *pSel = pItem->pSelect;
30517 if( pSel->selFlags & SF_NestedFrom ){
30518 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId);
30520 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId);
30539 width -= length;
30549 sqlite3DbFree(pAccum->db, zExtra);
30568 if( db->errByteOffset!=(-2) ) return;
30569 pParse = db->pParse;
30571 zText =pParse->zTail;
30575 db->errByteOffset = (int)(z-zText);
30585 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0)
30587 pExpr = pExpr->pLeft;
30590 db->errByteOffset = pExpr->w.iOfst;
30602 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
30603 if( p->accError ){
30604 testcase(p->accError==SQLITE_TOOBIG);
30605 testcase(p->accError==SQLITE_NOMEM);
30608 if( p->mxAlloc==0 ){
30610 return p->nAlloc - p->nChar - 1;
30612 char *zOld = isMalloced(p) ? p->zText : 0;
30613 i64 szNew = p->nChar;
30615 if( szNew+p->nChar<=p->mxAlloc ){
30618 szNew += p->nChar;
30620 if( szNew > p->mxAlloc ){
30625 p->nAlloc = (int)szNew;
30627 if( p->db ){
30628 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
30630 zNew = sqlite3Realloc(zOld, p->nAlloc);
30633 assert( p->zText!=0 || p->nChar==0 );
30634 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
30635 p->zText = zNew;
30636 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
30637 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
30651 testcase( p->nChar + (i64)N > 0x7fffffff );
30652 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
30655 while( (N--)>0 ) p->zText[p->nChar++] = c;
30662 ** This is a helper routine to sqlite3_str_append() that does special-case
30669 memcpy(&p->zText[p->nChar], z, N);
30670 p->nChar += N;
30680 assert( p->zText!=0 || p->nChar==0 || p->accError );
30682 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
30683 if( p->nChar+N >= p->nAlloc ){
30686 assert( p->zText );
30687 p->nChar += N;
30688 memcpy(&p->zText[p->nChar-N], z, N);
30693 ** Append the complete text of zero-terminated string z[] to the p string.
30701 ** Finish off a string by making sure it is zero-terminated.
30707 assert( p->mxAlloc>0 && !isMalloced(p) );
30708 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
30710 memcpy(zText, p->zText, p->nChar+1);
30711 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
30715 p->zText = zText;
30719 if( p->zText ){
30720 p->zText[p->nChar] = 0;
30721 if( p->mxAlloc>0 && !isMalloced(p) ){
30725 return p->zText;
30733 if( p->accError ){
30734 sqlite3_result_error_code(pCtx, p->accError);
30737 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC);
30769 return p ? p->accError : SQLITE_NOMEM;
30774 return p ? p->nChar : 0;
30779 if( p==0 || p->nChar==0 ) return 0;
30780 p->zText[p->nChar] = 0;
30781 return p->zText;
30789 sqlite3DbFree(p->db, p->zText);
30790 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
30792 p->nAlloc = 0;
30793 p->nChar = 0;
30794 p->zText = 0;
30802 ** memory is used if not NULL. db->mallocFailed is set appropriately
30812 p->zText = zBase;
30813 p->db = db;
30814 p->nAlloc = n;
30815 p->mxAlloc = mx;
30816 p->nChar = 0;
30817 p->accError = 0;
30818 p->printfFlags = 0;
30826 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
30835 ** %-conversion extensions.
30843 db->aLimit[SQLITE_LIMIT_LENGTH]);
30855 ** %-conversion extensions.
30868 ** %-conversion extensions.
30892 ** %-conversion extensions.
30946 ** stack space on small-stack systems when logging is disabled.
31008 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
31021 ** 2015-06-08
31053 p->iLevel++;
31056 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
31065 p->iLevel--;
31066 if( p->iLevel<0 ){
31083 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){
31084 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
31086 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
31101 ** Shorthand for starting a new tree item that consists of a single label
31122 int colMoreToFollow = i<(nCol - 1);
31137 printf(" X-%s", z);
31145 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND");
31156 ** Generate a human-readable description of a WITH clause.
31161 if( pWith->nCte==0 ) return;
31162 if( pWith->pOuter ){
31163 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
31167 if( pWith->nCte>0 ){
31169 for(i=0; i<pWith->nCte; i++){
31172 const struct Cte *pCte = &pWith->a[i];
31174 sqlite3_str_appendf(&x, "%s", pCte->zName);
31175 if( pCte->pCols && pCte->pCols->nExpr>0 ){
31178 for(j=0; j<pCte->pCols->nExpr; j++){
31179 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
31184 if( pCte->eM10d!=M10d_Any ){
31186 pCte->eM10d==M10d_No ? "NOT " : "");
31188 if( pCte->pUse ){
31189 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
31190 pCte->pUse->nUse);
31193 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
31194 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
31202 ** Generate a human-readable description of a SrcList object.
31207 for(i=0; i<pSrc->nSrc; i++){
31208 const SrcItem *pItem = &pSrc->a[i];
31214 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem);
31215 if( pItem->pTab ){
31217 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
31219 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){
31220 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN");
31221 }else if( pItem->fg.jointype & JT_LEFT ){
31222 sqlite3_str_appendf(&x, " LEFT-JOIN");
31223 }else if( pItem->fg.jointype & JT_RIGHT ){
31224 sqlite3_str_appendf(&x, " RIGHT-JOIN");
31225 }else if( pItem->fg.jointype & JT_CROSS ){
31226 sqlite3_str_appendf(&x, " CROSS-JOIN");
31228 if( pItem->fg.jointype & JT_LTORJ ){
31231 if( pItem->fg.fromDDL ){
31234 if( pItem->fg.isCte ){
31235 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
31237 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){
31241 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
31243 if( pItem->pSelect ) n++;
31244 if( pItem->fg.isTabFunc ) n++;
31245 if( pItem->fg.isUsing ) n++;
31246 if( pItem->fg.isUsing ){
31247 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING");
31249 if( pItem->pSelect ){
31250 if( pItem->pTab ){
31251 Table *pTab = pItem->pTab;
31252 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1);
31254 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
31255 sqlite3TreeViewSelect(pView, pItem->pSelect, (--n)>0);
31257 if( pItem->fg.isTabFunc ){
31258 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
31265 ** Generate a human-readable description of a Select object.
31271 sqlite3TreeViewLine(pView, "nil-SELECT");
31275 if( p->pWith ){
31276 sqlite3TreeViewWith(pView, p->pWith, 1);
31281 if( p->selFlags & SF_WhereBegin ){
31286 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
31287 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
31288 p->selId, p, p->selFlags,
31289 (int)p->nSelectRow
31293 if( p->pPrior ){
31297 if( p->pSrc && p->pSrc->nSrc ) n++;
31298 if( p->pWhere ) n++;
31299 if( p->pGroupBy ) n++;
31300 if( p->pHaving ) n++;
31301 if( p->pOrderBy ) n++;
31302 if( p->pLimit ) n++;
31304 if( p->pWin ) n++;
31305 if( p->pWinDefn ) n++;
31308 if( p->pEList ){
31309 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
31311 n--;
31313 if( p->pWin ){
31315 sqlite3TreeViewPush(&pView, (n--)>0);
31316 sqlite3TreeViewLine(pView, "window-functions");
31317 for(pX=p->pWin; pX; pX=pX->pNextWin){
31318 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
31323 if( p->pSrc && p->pSrc->nSrc ){
31324 sqlite3TreeViewPush(&pView, (n--)>0);
31326 sqlite3TreeViewSrcList(pView, p->pSrc);
31329 if( p->pWhere ){
31330 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
31331 sqlite3TreeViewExpr(pView, p->pWhere, 0);
31334 if( p->pGroupBy ){
31335 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
31337 if( p->pHaving ){
31338 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
31339 sqlite3TreeViewExpr(pView, p->pHaving, 0);
31343 if( p->pWinDefn ){
31345 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
31346 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
31347 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
31352 if( p->pOrderBy ){
31353 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
31355 if( p->pLimit ){
31356 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
31357 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
31358 if( p->pLimit->pRight ){
31359 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
31360 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
31365 if( p->pPrior ){
31367 switch( p->op ){
31374 p = p->pPrior;
31418 ** Generate a human-readable explanation for a Window object
31423 if( pWin->pFilter ){
31425 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
31429 if( pWin->zName ){
31430 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
31434 if( pWin->zBase ) nElement++;
31435 if( pWin->pOrderBy ) nElement++;
31436 if( pWin->eFrmType ) nElement++;
31437 if( pWin->eExclude ) nElement++;
31438 if( pWin->zBase ){
31439 sqlite3TreeViewPush(&pView, (--nElement)>0);
31440 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
31443 if( pWin->pPartition ){
31444 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
31446 if( pWin->pOrderBy ){
31447 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
31449 if( pWin->eFrmType ){
31452 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
31453 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
31455 pWin->bImplicitFrame ? " (implied)" : "");
31456 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
31457 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
31458 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
31461 if( pWin->eExclude ){
31464 switch( pWin->eExclude ){
31470 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
31484 ** Generate a human-readable explanation for a Window Function object
31490 pWin->pWFunc->zName, pWin->pWFunc->nArg);
31497 ** Generate a human-readable explanation of an expression tree.
31509 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
31513 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
31515 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin);
31518 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin);
31530 switch( pExpr->op ){
31533 pExpr->iTable, pExpr->iColumn, zFlgs);
31537 if( pExpr->iTable<0 ){
31540 if( pExpr->op2 ){
31541 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
31546 pExpr->iColumn, zFlgs, zOp2);
31550 pExpr->iTable, pExpr->iColumn,
31551 pExpr->y.pTab, zFlgs);
31554 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31559 if( pExpr->flags & EP_IntValue ){
31560 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
31562 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
31569 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31575 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
31590 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
31597 pExpr->u.zToken, pExpr->iColumn);
31601 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
31606 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
31613 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
31614 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31651 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
31653 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
31654 assert( pExpr->pRight );
31655 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
31656 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
31663 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
31664 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31671 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
31676 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
31677 pExpr->u.zToken, zFlgs);
31678 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31691 pFarg = pExpr->x.pList;
31693 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
31699 if( pExpr->op==TK_AGG_FUNCTION ){
31701 pExpr->op2, pExpr->u.zToken, zFlgs,
31702 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
31703 pExpr->iAgg, pExpr->pAggInfo);
31704 }else if( pExpr->op2!=0 ){
31707 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
31709 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
31710 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
31711 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
31712 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
31714 pExpr->u.zToken, zFlgs, zOp2);
31716 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
31731 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
31732 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31737 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
31738 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31744 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags);
31745 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable);
31748 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr);
31753 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31755 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
31757 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
31770 ** X is stored in pExpr->pLeft.
31771 ** Y is stored in pExpr->pList->a[0].pExpr.
31772 ** Z is stored in pExpr->pList->a[1].pExpr.
31776 pX = pExpr->pLeft;
31778 assert( pExpr->x.pList->nExpr==2 );
31779 pY = pExpr->x.pList->a[0].pExpr;
31780 pZ = pExpr->x.pList->a[1].pExpr;
31789 ** to a column in the new.* or old.* pseudo-tables available to
31791 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
31792 ** is set to the column of the pseudo-table to read, or to -1 to
31796 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
31801 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31803 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
31809 switch( pExpr->affExpr ){
31816 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
31822 pExpr->iTable, pExpr->iColumn, zFlgs);
31823 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
31829 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
31834 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s",
31835 pExpr->iColumn, pExpr->iTable-1,
31836 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : "");
31837 assert( ExprUseXSelect(pExpr->pLeft) );
31838 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
31842 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
31843 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31850 tmp.op = pExpr->op2;
31855 if( pExpr->iColumn<=0 ){
31859 pExpr->iColumn-1);
31864 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
31870 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
31871 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
31874 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
31881 ** Generate a human-readable explanation of an expression list.
31894 for(i=0; i<pList->nExpr; i++){
31895 int j = pList->a[i].u.x.iOrderByCol;
31896 char *zName = pList->a[i].zEName;
31897 int moreToFollow = i<pList->nExpr - 1;
31903 switch( pList->a[i].fg.eEName ){
31908 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName);
31909 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) ");
31910 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) ");
31911 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) ");
31924 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
31943 ** Generate a human-readable explanation of an id-list.
31956 for(i=0; i<pList->nId; i++){
31957 char *zName = pList->a[i].zName;
31958 int moreToFollow = i<pList->nId - 1;
31962 if( pList->eU4==EU4_NONE ){
31964 }else if( pList->eU4==EU4_IDX ){
31965 fprintf(stdout, "%s (%d)\n", zName, pList->a[i].u4.idx);
31967 assert( pList->eU4==EU4_EXPR );
31968 if( pList->a[i].u4.pExpr==0 ){
31972 sqlite3TreeViewPush(&pView, i<pList->nId-1);
31973 sqlite3TreeViewExpr(pView, pList->a[i].u4.pExpr, 0);
31993 ** Generate a human-readable explanation of a list of Upsert objects
32004 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow);
32006 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING");
32007 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0);
32008 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET");
32009 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET");
32010 if( pUpsert->pUpsertWhere ){
32011 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
32012 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0);
32016 pUpsert = pUpsert->pNextUpsert;
32023 ** Generate a human-readable diagram of the data structure that go
32045 sqlite3TreeViewPush(&pView, (--n)>0);
32050 sqlite3TreeViewPush(&pView, (--n)>0);
32056 sqlite3TreeViewPush(&pView, (--n)>0);
32062 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32065 sqlite3TreeViewPush(&pView, (--n)>0);
32071 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32079 ** Generate a human-readable diagram of the data structure that go
32112 sqlite3TreeViewPush(&pView, (--n)>0);
32117 sqlite3TreeViewPush(&pView, (--n)>0);
32123 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS");
32126 sqlite3TreeViewPush(&pView, (--n)>0);
32127 sqlite3TreeViewLine(pView, "DATA-SOURCE");
32132 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES");
32135 sqlite3TreeViewPush(&pView, (--n)>0);
32141 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32149 ** Generate a human-readable diagram of the data structure that go
32184 sqlite3TreeViewPush(&pView, (--n)>0);
32189 sqlite3TreeViewPush(&pView, (--n)>0);
32195 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET");
32198 sqlite3TreeViewPush(&pView, (--n)>0);
32204 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY");
32207 sqlite3TreeViewPush(&pView, (--n)>0);
32213 sqlite3TreeViewPush(&pView, (--n)>0);
32219 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1);
32227 ** Show a human-readable graph of a TriggerStep
32238 moreToFollow || (showFullList && pStep->pNext!=0));
32240 if( cnt++ && pStep->pNext==0 ){
32244 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING");
32245 }while( showFullList && (pStep = pStep->pNext)!=0 );
32250 ** Show a human-readable graph of a Trigger
32261 moreToFollow || (showFullList && pTrigger->pNext!=0));
32263 if( cnt++ && pTrigger->pNext==0 ){
32267 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName);
32269 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1);
32271 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 );
32278 ** These simplified versions of the tree-view routines omit unnecessary
32324 ** This file contains code to implement a pseudo-random number
32349 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
32351 ** we have to locate the state vector at run-time. In the more common
32387 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
32422 }while( --N );
32467 ** This file presents a simple cross-platform threading interface for
32480 ** single-threaded if desired.
32492 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
32515 /* This routine is never used in single-threaded mode */
32522 p->xTask = xTask;
32523 p->pIn = pIn;
32531 rc = pthread_create(&p->tid, 0, xTask, pIn);
32534 p->done = 1;
32535 p->pOut = xTask(pIn);
32547 if( p->done ){
32548 *ppOut = p->pOut;
32551 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
32564 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
32590 assert( p->id==GetCurrentThreadId() );
32592 assert( p->xTask!=0 );
32593 p->pResult = p->xTask(p->pIn);
32620 p->xTask = xTask;
32621 p->pIn = pIn;
32622 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
32623 if( p->tid==0 ){
32627 if( p->xTask==0 ){
32628 p->id = GetCurrentThreadId();
32629 p->pResult = xTask(pIn);
32644 if( p->xTask==0 ){
32645 /* assert( p->id==GetCurrentThreadId() ); */
32647 assert( p->tid==0 );
32649 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
32650 rc = sqlite3Win32Wait((HANDLE)p->tid);
32652 bRc = CloseHandle((HANDLE)p->tid);
32655 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
32664 /********************************* Single-Threaded **************************/
32693 p->xTask = xTask;
32694 p->pIn = pIn;
32696 p->xTask = 0;
32697 p->pResult = xTask(pIn);
32708 if( p->xTask ){
32709 *ppOut = p->xTask(p->pIn);
32711 *ppOut = p->pResult;
32727 /****************************** End Single-Threaded *************************/
32743 ** This file contains routines used to translate between UTF-8,
32744 ** UTF-16, UTF-16BE, and UTF-16LE.
32746 ** Notes on UTF-8:
32748 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
32755 ** Notes on UTF-16: (with wwww+1==uuuuu)
32757 ** Word-0 Word-1 Value
32763 ** 0xff 0xfe little-endian utf-16 follows
32764 ** 0xfe 0xff big-endian utf-16 follows
32781 ** a multi-byte UTF8 character.
32820 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
32821 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
32832 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
32833 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
32840 ** Translate a single UTF-8 character. Return the unicode value.
32847 ** Notes On Invalid UTF-8:
32849 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
32850 ** be encoded as a multi-byte character. Any multi-byte character that
32854 ** If a multi-byte character attempts to encode a value between
32858 ** byte of a character are interpreted as single-byte characters
32862 ** * This routine accepts over-length UTF8 encodings
32863 ** for unicode values 0x80 and greater. It does not change over-length
32869 c = sqlite3Utf8Trans1[c-0xc0]; \
32883 ** For this routine, we assume the UTF8 string is always zero-terminated.
32887 c = sqlite3Utf8Trans1[c-0xc0];
32921 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
32922 assert( pMem->flags&MEM_Str );
32923 assert( pMem->enc!=desiredEnc );
32924 assert( pMem->enc!=0 );
32925 assert( pMem->n>=0 );
32937 /* If the translation is between UTF-16 little and big endian, then
32941 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
32949 zIn = (u8*)pMem->z;
32950 zTerm = &zIn[pMem->n&~1];
32957 pMem->enc = desiredEnc;
32963 /* When converting from UTF-16, the maximum growth results from
32964 ** translating a 2-byte character to a 4-byte UTF-8 character.
32966 ** nul-terminator.
32968 pMem->n &= ~1;
32969 len = 2 * (sqlite3_int64)pMem->n + 1;
32971 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
32972 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
32974 ** nul-terminator.
32976 len = 2 * (sqlite3_int64)pMem->n + 2;
32985 zIn = (u8*)pMem->z;
32986 zTerm = &zIn[pMem->n];
32987 zOut = sqlite3DbMallocRaw(pMem->db, len);
32993 if( pMem->enc==SQLITE_UTF8 ){
32995 /* UTF-8 -> UTF-16 Little-endian */
33002 /* UTF-8 -> UTF-16 Big-endian */
33008 pMem->n = (int)(z - zOut);
33012 if( pMem->enc==SQLITE_UTF16LE ){
33013 /* UTF-16 Little-endian -> UTF-8 */
33025 zIn -= 2;
33042 /* UTF-16 Big-endian -> UTF-8 */
33054 zIn -= 2;
33071 pMem->n = (int)(z - zOut);
33074 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
33076 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
33078 pMem->flags = c;
33079 pMem->enc = desiredEnc;
33080 pMem->z = (char*)zOut;
33081 pMem->zMalloc = pMem->z;
33082 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
33100 ** This routine checks for a byte-order mark at the beginning of the
33101 ** UTF-16 string stored in *pMem. If one is present, it is removed and
33103 ** byte-swapping, it just sets Mem.enc appropriately.
33112 assert( pMem->n>=0 );
33113 if( pMem->n>1 ){
33114 u8 b1 = *(u8 *)pMem->z;
33115 u8 b2 = *(((u8 *)pMem->z) + 1);
33127 pMem->n -= 2;
33128 memmove(pMem->z, &pMem->z[2], pMem->n);
33129 pMem->z[pMem->n] = '\0';
33130 pMem->z[pMem->n+1] = '\0';
33131 pMem->flags |= MEM_Term;
33132 pMem->enc = bom;
33140 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
33153 zTerm = (const u8*)(-1);
33163 /* This test function is not currently used by the automated test-suite.
33168 ** Translate UTF-8 to UTF-8.
33170 ** This has the effect of making sure that the string is well-formed
33171 ** UTF-8. Miscoded characters are removed.
33173 ** The translation is done in-place and aborted if the output
33188 return (int)(zOut - zStart);
33194 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
33195 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
33206 if( db->mallocFailed ){
33210 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
33211 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
33212 assert( m.z || db->mallocFailed );
33217 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
33219 ** in pZ. nChar must be non-negative.
33233 return (int)(z-(unsigned char const *)zIn)
33234 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
33253 n = (int)(z-zBuf);
33262 assert( (z-zBuf)==n );
33299 ** sqlite3FaultSim() function only returns non-zero during testing.
33301 ** During testing, if the test harness has set a fault-sim callback using
33303 ** each call to sqlite3FaultSim() is relayed to that application-supplied
33304 ** callback and the integer return value form the application-supplied
33342 ** lower 30 bits of a 32-bit signed integer.
33357 ** The column type is an extra string stored after the zero-terminator on
33361 if( pCol->colFlags & COLFLAG_HASTYPE ){
33362 return pCol->zCnName + strlen(pCol->zCnName) + 1;
33363 }else if( pCol->eCType ){
33364 assert( pCol->eCType<=SQLITE_N_STDTYPE );
33365 return (char*)sqlite3StdType[pCol->eCType-1];
33372 ** Helper function for sqlite3Error() - called rarely. Broken out into
33377 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33388 db->errCode = err_code;
33389 if( err_code || db->pErr ){
33392 db->errByteOffset = -1;
33402 db->errCode = SQLITE_OK;
33403 db->errByteOffset = -1;
33404 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
33415 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
33425 ** assumed to be encoded in UTF-8.
33433 db->errCode = err_code;
33437 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
33443 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
33448 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
33460 sqlite3 *db = pParse->db;
33462 assert( db->pParse==pParse );
33463 db->errByteOffset = -2;
33467 if( db->errByteOffset<-1 ) db->errByteOffset = -1;
33468 if( db->suppressErr ){
33470 if( db->mallocFailed ){
33471 pParse->nErr++;
33472 pParse->rc = SQLITE_NOMEM;
33475 pParse->nErr++;
33476 sqlite3DbFree(db, pParse->zErrMsg);
33477 pParse->zErrMsg = zMsg;
33478 pParse->rc = SQLITE_ERROR;
33479 pParse->pWith = 0;
33490 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
33491 pParse->rc = errCode;
33492 pParse->nErr++;
33497 ** Convert an SQL-style quoted string into a normal string by removing
33498 ** the quote characters. The conversion is done in-place. If the
33500 ** is a no-op.
33502 ** The input string must be zero-terminated. A new zero-terminator
33505 ** The return value is -1 if no dequoting occurs or the length of the
33509 ** 2002-02-14: This routine is extended to remove MS-Access style
33510 ** brackets from around identifiers. For example: "[a-b-c]" becomes
33511 ** "a-b-c".
33537 assert( sqlite3Isquote(p->u.zToken[0]) );
33538 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
33539 sqlite3Dequote(p->u.zToken);
33546 ** "abc" -> abc
33547 ** "ab""cd" -> (not possible because of the interior "")
33551 ** is always a no-op.
33555 if( p->n<2 ) return;
33556 if( !sqlite3Isquote(p->z[0]) ) return;
33557 for(i=1; i<p->n-1; i++){
33558 if( sqlite3Isquote(p->z[i]) ) return;
33560 p->n -= 2;
33561 p->z++;
33568 p->z = z;
33569 p->n = sqlite3Strlen30(z);
33572 /* Convenient short-hand */
33579 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
33581 ** the contents of two buffers containing UTF-8 strings in a
33582 ** case-independent fashion, using the same definition of "case
33587 return zRight ? -1 : 0;
33604 c = (int)UpperToLower[c] - (int)UpperToLower[x];
33615 return zRight ? -1 : 0;
33621 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
33622 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
33626 ** Compute an 8-bit hash on a string that is insensitive to case differences
33639 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
33682 ** uses the encoding enc. The string is not necessarily zero-terminated.
33690 ** -1 => Not a valid number, but has a valid prefix which
33695 ** [+-]digits[E[+-]digits]
33696 ** [+-]digits.[digits][E[+-]digits]
33697 ** [+-].digits[E[+-]digits]
33719 int eValid = 1; /* True exponent is either not used or is well-formed */
33722 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
33738 for(i=3-enc; i<length && z[i]==0; i+=2){}
33739 if( i<length ) eType = -100;
33749 if( *z=='-' ){
33750 sign = -1;
33758 s = s*10 + (*z - '0');
33760 if( s>=((LARGEST_INT64-9)/10) ){
33761 /* skip non-significant significand digits
33775 if( s<((LARGEST_INT64-9)/10) ){
33776 s = s*10 + (*z - '0');
33777 d--;
33794 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
33797 if( *z=='-' ){
33798 esign = -1;
33805 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
33818 esign = -1;
33819 e *= -1;
33826 result = sign<0 ? -(double)0 : (double)0;
33834 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
33836 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
33839 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
33842 e--;
33846 s = sign<0 ? -s : s;
33848 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
33852 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
33853 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
33854 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
33887 /* return true if number and no extra non-whitespace chracters after */
33891 return -1;
33904 ** Render an signed 64-bit integer as text. Store the result in zOut[].
33913 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
33917 i = sizeof(zTemp)-2;
33918 zTemp[sizeof(zTemp)-1] = 0;
33920 zTemp[i--] = (x%10) + '0';
33923 if( v<0 ) zTemp[i--] = '-';
33924 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
33928 ** Compare the 19-character string zNum against the text representation
33939 ** will return -8.
33947 c = (zNum[i*incr]-pow63[i])*10;
33950 c = zNum[18*incr] - '8';
33951 testcase( c==(-1) );
33959 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
33964 ** -1 Not even a prefix of the input text looks like an integer
33965 ** 0 Successful transformation. Fits in a 64-bit signed integer.
33966 ** 1 Excess non-space text after the integer value
33967 ** 2 Integer too large for a 64-bit signed integer or is malformed
33971 ** The string is not necessarily zero-terminated. The encoding is
33980 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
33991 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
33998 if( *zNum=='-' ){
34008 u = u*10 + c - '0';
34015 ** from clang and -fsanitize=undefined. This test and assignment make
34017 ** them, but we must appaise the undefined-behavior pharisees. */
34020 *pNum = -(i64)u;
34026 rc = -1;
34027 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
34033 rc = 1; /* Extra non-space text after the integer */
34044 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
34058 assert( u-1==LARGEST_INT64 );
34066 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
34067 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
34072 ** 0 Successful transformation. Fits in a 64-bit signed integer.
34074 ** 2 Integer too large for a 64-bit signed integer or is malformed
34089 return (z[k]==0 && k-i<=16) ? 0 : 2;
34098 ** If zNum represents an integer that will fit in 32-bits, then set
34103 ** Any non-numeric characters that following zNum are ignored.
34105 ** input number to be zero-terminated.
34111 if( zNum[0]=='-' ){
34138 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
34145 ** 2^31 -> 2147483648
34151 testcase( v-neg==2147483647 );
34152 if( v-neg>2147483647 ){
34156 v = -v;
34163 ** Return a 32-bit integer value extracted from a string. If the
34173 ** Try to convert z into an unsigned 32-bit integer. Return true on
34182 v = v*10 + z[i] - '0';
34191 ** The variable-length integer encoding is as follows:
34198 ** 7 bits - A
34199 ** 14 bits - BA
34200 ** 21 bits - BBA
34201 ** 28 bits - BBBA
34202 ** 35 bits - BBBBA
34203 ** 42 bits - BBBBBA
34204 ** 49 bits - BBBBBBA
34205 ** 56 bits - BBBBBBBA
34206 ** 64 bits - BBBBBBBBC
34210 ** Write a 64-bit variable-length integer to memory starting at p[0].
34214 ** A variable-length integer consists of the lower 7 bits of each byte
34225 for(i=7; i>=0; i--){
34238 for(i=0, j=n-1; j>=0; j--, i++){
34270 ** Read a 64-bit variable-length integer from memory starting at p[0].
34413 b = p[-4];
34424 ** Read a 32-bit variable-length integer from memory starting at p[0].
34427 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
34431 ** single-byte case. All code should use the MACRO version as
34432 ** this function assumes the single-byte case has already been handled.
34437 /* The 1-byte case. Overwhelmingly the most common. Handled inline
34450 /* The 2-byte case */
34463 /* The 3-byte case */
34478 /* A 32-bit varint is used to store size information in btrees.
34479 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
34480 ** A 3-byte varint is sufficient, for example, to record the size
34481 ** of a 1048569-byte BLOB or string.
34483 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
34484 ** rare larger cases can be handled by the slower 64-bit varint
34492 n = sqlite3GetVarint(p-2, &v64);
34504 ** unrolling for the 3- and 4-byte varint cases. This code is
34537 ** slow) general-purpose sqlite3GetVarint() routine to extract the
34543 p -= 4;
34554 ** 64-bit integer.
34564 ** Read or write a four-byte big-endian integer value.
34631 n--;
34674 eOpenState = db->eOpenState;
34687 eOpenState = db->eOpenState;
34700 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
34701 ** the other 64-bit signed integer at *pA and store the result in *pA.
34711 testcase( iB==-1 ); testcase( iB==0 );
34713 testcase( iA>0 && LARGEST_INT64 - iA == iB );
34714 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
34715 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
34717 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
34718 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
34719 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
34731 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
34733 *pA -= iB;
34736 return sqlite3AddInt64(pA, -iB);
34754 if( -iA>LARGEST_INT64/-iB ) return 1;
34763 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
34764 ** if the integer has a value of -2147483648, return +2147483647
34769 return -x;
34774 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
34780 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
34785 ** test.db-journal => test.nal
34786 ** test.db-wal => test.wal
34787 ** test.db-shm => test.shm
34788 ** test.db-mj7f3319fa => test.9fa
34797 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
34798 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
34816 5, 5, 5, /* 12-14 */
34817 4, 4, 4, 4, /* 15-18 */
34818 3, 3, 3, 3, 3, 3, /* 19-24 */
34819 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
34824 return a+x[a-b];
34828 return b+x[b-a];
34841 while( x<8 ){ y -= 10; x <<= 1; }
34844 int i = 60 - __builtin_clzll(x);
34848 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
34852 return a[x&7] + y - 10;
34866 e = (a>>52) - 1022;
34877 if( n>=5 ) n -= 2;
34878 else if( n>=1 ) n -= 1;
34880 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
34887 ** db->mallocFailed flag is set.
34900 ** is always zero-terminated.
34999 ** This is the implementation of generic hash-tables
35012 pNew->first = 0;
35013 pNew->count = 0;
35014 pNew->htsize = 0;
35015 pNew->ht = 0;
35026 elem = pH->first;
35027 pH->first = 0;
35028 sqlite3_free(pH->ht);
35029 pH->ht = 0;
35030 pH->htsize = 0;
35032 HashElem *next_elem = elem->next;
35036 pH->count = 0;
35045 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
35048 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
35066 pHead = pEntry->count ? pEntry->chain : 0;
35067 pEntry->count++;
35068 pEntry->chain = pNew;
35073 pNew->next = pHead;
35074 pNew->prev = pHead->prev;
35075 if( pHead->prev ){ pHead->prev->next = pNew; }
35076 else { pH->first = pNew; }
35077 pHead->prev = pNew;
35079 pNew->next = pH->first;
35080 if( pH->first ){ pH->first->prev = pNew; }
35081 pNew->prev = 0;
35082 pH->first = pNew;
35101 if( new_size==pH->htsize ) return 0;
35117 sqlite3_free(pH->ht);
35118 pH->ht = new_ht;
35119 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
35121 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
35122 unsigned int h = strHash(elem->pKey) % new_size;
35123 next_elem = elem->next;
35144 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
35146 h = strHash(pKey) % pH->htsize;
35147 pEntry = &pH->ht[h];
35148 elem = pEntry->chain;
35149 count = pEntry->count;
35152 elem = pH->first;
35153 count = pH->count;
35156 while( count-- ){
35158 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
35161 elem = elem->next;
35175 if( elem->prev ){
35176 elem->prev->next = elem->next;
35178 pH->first = elem->next;
35180 if( elem->next ){
35181 elem->next->prev = elem->prev;
35183 if( pH->ht ){
35184 pEntry = &pH->ht[h];
35185 if( pEntry->chain==elem ){
35186 pEntry->chain = elem->next;
35188 assert( pEntry->count>0 );
35189 pEntry->count--;
35192 pH->count--;
35193 if( pH->count==0 ){
35194 assert( pH->first==0 );
35195 assert( pH->count==0 );
35207 return findElementWithHash(pH, pKey, 0)->data;
35232 if( elem->data ){
35233 void *old_data = elem->data;
35237 elem->data = data;
35238 elem->pKey = pKey;
35245 new_elem->pKey = pKey;
35246 new_elem->data = data;
35247 pH->count++;
35248 if( pH->count>=10 && pH->count > 2*pH->htsize ){
35249 if( rehash(pH, pH->count*2) ){
35250 assert( pH->htsize>0 );
35251 h = strHash(pKey) % pH->htsize;
35254 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
35321 /* 49 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
35331 /* 59 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
35332 /* 60 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
35362 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
35379 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
35396 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
35432 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
35478 ** This file contains the VFS implementation for unix-like operating systems
35484 ** use flock(), dot-files, various proprietary locking schemas, or simply
35494 ** * General-purpose declarations and utility functions.
35498 ** + for no-op locks
35499 ** + for dot-file locks
35507 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
35520 ** 3. Dot-file locking,
35579 ** -DHAVE_GETHOSTUUID=0
35580 ** -DHAVE_GETHOSTUUID=1
35583 ** -DSQLITE_ENABLE_LOCKING_STYLE.
35621 ** If we are to be thread-safe, include the pthreads header.
35642 ** Maximum supported path-length.
35664 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
35693 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
35720 ** transaction counter in bytes 24-27 of database files are updated
35785 ** The threadid macro resolves to the thread-id or to 0. Used for
35806 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
35807 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
35815 ** Linux-specific IOCTL magic numbers used for controlling F2FS
35833 ** which always has the same well-defined interface.
35844 ** Many system calls are accessed through pointer-to-functions so that
35873 ** DJGPP. But it is DOS - what did you expect?
36020 ** log if they come from non-root processes. So avoid calling fchown() if
36100 int i = -1;
36104 for(i=0; i<ArraySize(aSyscall)-1; i++){
36132 ** The m parameter will be non-zero only when creating -wal, -journal,
36133 ** and -shm files. We want those files to have *exactly* the same
36135 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
36157 fd = -1;
36222 ** integer lock-type.
36241 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
36242 ** command-line option on the compiler. This code is normally
36258 if( p->l_type==F_RDLCK ){
36260 }else if( p->l_type==F_WRLCK ){
36262 }else if( p->l_type==F_UNLCK ){
36267 assert( p->l_whence==SEEK_SET );
36271 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
36272 (int)p->l_pid, s);
36273 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
36286 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
36306 /* On Android, ftruncate() always uses 32-bit offsets, even if
36367 ** a performance-critical path, so it is sufficient to put these
36392 ** Changes are made in-place. Return the new name length.
36394 ** The original filename is in z[0..n-1]. Return the number of
36399 while( n>1 && z[n-1]=='/' ){ n--; }
36408 while( j>0 && z[j-1]!='/' ){ j--; }
36409 if( j>0 ){ j--; }
36440 pNew->zCanonicalName = (char*)&pNew[1];
36441 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
36442 n = vxworksSimplifyName(pNew->zCanonicalName, n);
36449 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
36450 if( pCandidate->nName==n
36451 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
36454 pCandidate->nRef++;
36461 pNew->nRef = 1;
36462 pNew->nName = n;
36463 pNew->pNext = vxworksFileList;
36475 assert( pId->nRef>0 );
36476 pId->nRef--;
36477 if( pId->nRef==0 ){
36479 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
36481 *pp = pId->pNext;
36539 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
36567 ** on linux - with NPTL a lock created by thread A can override locks
36568 ** in thread B. But there is no way to know at compile-time which
36570 ** compile-time whether or not thread A can override locks on thread B.
36571 ** One has to do a run-time check to discover the behavior of the
36591 ** sizes ino_t at only 32-bits instead of 64-bits. (See
36592 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
36593 ** To work around this, always allocate 64-bits for the inode number.
36594 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
36661 assert( pFile->pInode );
36662 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
36665 assert( pFile->pInode );
36666 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
36672 ** This function - unixLogErrorAtLine(), is only ever called via the macro
36677 ** errno and, if possible, the human-readable equivalent from strerror() or
36683 ** failed (e.g. "unlink", "open") and the associated file-system path,
36697 ** the strerror() function to obtain the human-readable error message
36720 strerror_r(iErrno, aErr, sizeof(aErr)-1);
36726 /* Non-threadsafe build, use strerror(). */
36732 "os_unix.c:%d: (%d) %s(%s) - %s",
36755 pFile ? pFile->zPath : 0, lineno);
36760 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
36764 pFile->lastErrno = error;
36768 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
36771 unixInodeInfo *pInode = pFile->pInode;
36775 for(p=pInode->pUnused; p; p=pNext){
36776 pNext = p->pNext;
36777 robust_close(pFile, p->fd, __LINE__);
36780 pInode->pUnused = 0;
36790 unixInodeInfo *pInode = pFile->pInode;
36794 pInode->nRef--;
36795 if( pInode->nRef==0 ){
36796 assert( pInode->pShmNode==0 );
36797 sqlite3_mutex_enter(pInode->pLockMutex);
36799 sqlite3_mutex_leave(pInode->pLockMutex);
36800 if( pInode->pPrev ){
36801 assert( pInode->pPrev->pNext==pInode );
36802 pInode->pPrev->pNext = pInode->pNext;
36805 inodeList = pInode->pNext;
36807 if( pInode->pNext ){
36808 assert( pInode->pNext->pPrev==pInode );
36809 pInode->pNext->pPrev = pInode->pPrev;
36811 sqlite3_mutex_free(pInode->pLockMutex);
36833 struct stat statbuf; /* Low-level file information */
36838 /* Get low-level information about the file that we can used to
36841 fd = pFile->h;
36846 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
36853 ** incorrectly for zero-size files. See ticket #3260. To work
36862 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
36879 fileId.pId = pFile->pId;
36885 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
36886 pInode = pInode->pNext;
36894 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
36896 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
36897 if( pInode->pLockMutex==0 ){
36902 pInode->nRef = 1;
36904 pInode->pNext = inodeList;
36905 pInode->pPrev = 0;
36906 if( inodeList ) inodeList->pPrev = pInode;
36909 pInode->nRef++;
36920 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
36923 return pFile->pInode!=0 &&
36924 (osStat(pFile->zPath, &buf)!=0
36925 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
36944 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
36946 rc = osFstat(pFile->h, &buf);
36948 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
36952 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
36956 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
36960 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
36969 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36980 assert( pFile->eFileLock<=SHARED_LOCK );
36981 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
36984 if( pFile->pInode->eFileLock>SHARED_LOCK ){
36991 if( !reserved && !pFile->pInode->bProcessLock ){
36997 if( osFcntl(pFile->h, F_GETLK, &lock) ){
37006 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
37007 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
37017 ** Set a posix-advisory-lock.
37021 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
37026 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
37037 int tm = pFile->iBusyTimeout;
37047 tm--;
37055 ** Attempt to set a system-lock on the file pFile. The lock is
37058 ** If the pFile was opened read/write from unix-excl, then the only lock
37061 ** operations become no-ops. Locking operations still happen internally,
37066 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
37067 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
37068 ** and is read-only.
37070 ** Zero is returned if the call completes successfully, or -1 if a call
37075 unixInodeInfo *pInode = pFile->pInode;
37077 assert( sqlite3_mutex_held(pInode->pLockMutex) );
37078 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
37079 if( pInode->bProcessLock==0 ){
37081 assert( pInode->nLock==0 );
37086 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
37088 pInode->bProcessLock = 1;
37089 pInode->nLock++;
37094 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
37100 ** Lock the file with the lock specified by parameter eFileLock - one
37114 ** UNLOCKED -> SHARED
37115 ** SHARED -> RESERVED
37116 ** SHARED -> (PENDING) -> EXCLUSIVE
37117 ** RESERVED -> (PENDING) -> EXCLUSIVE
37118 ** PENDING -> EXCLUSIVE
37126 ** lock primitives (called read-locks and write-locks below, to avoid
37135 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
37136 ** byte'. If this is successful, 'shared byte range' is read-locked
37139 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
37141 ** Windows95 is now pretty much extinct, but this work-around for the
37142 ** lack of shared-locks on Windows95 lives on, for backwards
37146 ** A RESERVED lock is implemented by grabbing a write-lock on the
37150 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
37158 ** implemented by obtaining a write-lock on the entire 'shared byte
37159 ** range'. Since all other locks require a read-lock on one of the bytes
37170 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
37171 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
37172 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
37179 if( pFile->eFileLock>=eFileLock ){
37180 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
37190 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
37192 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
37194 /* This mutex is needed because pFile->pInode is shared across threads
37196 pInode = pFile->pInode;
37197 sqlite3_mutex_enter(pInode->pLockMutex);
37202 if( (pFile->eFileLock!=pInode->eFileLock &&
37203 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
37214 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
37216 assert( pFile->eFileLock==0 );
37217 assert( pInode->nShared>0 );
37218 pFile->eFileLock = SHARED_LOCK;
37219 pInode->nShared++;
37220 pInode->nLock++;
37232 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
37251 assert( pInode->nShared==0 );
37252 assert( pInode->eFileLock==0 );
37255 /* Now get the read-lock */
37279 pFile->eFileLock = SHARED_LOCK;
37280 pInode->nLock++;
37281 pInode->nShared = 1;
37283 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
37292 assert( 0!=pFile->eFileLock );
37315 /* Set up the transaction-counter change checking flags when
37321 && pFile->eFileLock<=SHARED_LOCK
37324 pFile->transCntrChng = 0;
37325 pFile->dbUpdate = 0;
37326 pFile->inNormalWrite = 1;
37332 pFile->eFileLock = eFileLock;
37333 pInode->eFileLock = eFileLock;
37335 pFile->eFileLock = PENDING_LOCK;
37336 pInode->eFileLock = PENDING_LOCK;
37340 sqlite3_mutex_leave(pInode->pLockMutex);
37341 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
37351 unixInodeInfo *pInode = pFile->pInode;
37352 UnixUnusedFd *p = pFile->pPreallocatedUnused;
37354 p->pNext = pInode->pUnused;
37355 pInode->pUnused = p;
37356 pFile->h = -1;
37357 pFile->pPreallocatedUnused = 0;
37365 ** the requested locking level, this routine is a no-op.
37380 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
37381 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
37385 if( pFile->eFileLock<=eFileLock ){
37388 pInode = pFile->pInode;
37389 sqlite3_mutex_enter(pInode->pLockMutex);
37390 assert( pInode->nShared!=0 );
37391 if( pFile->eFileLock>SHARED_LOCK ){
37392 assert( pInode->eFileLock==pFile->eFileLock );
37401 ** cache. The use of a stale cache can lead to database corruption.
37403 pFile->inNormalWrite = 0;
37407 ** before establishing the readlock - to avoid a race condition we downgrade
37423 off_t divSize = SHARED_SIZE - 1;
37429 if( unixFileLock(pFile, &lock)==(-1) ){
37439 if( unixFileLock(pFile, &lock)==(-1) ){
37450 lock.l_len = SHARED_SIZE-divSize;
37451 if( unixFileLock(pFile, &lock)==(-1) ){
37482 pInode->eFileLock = SHARED_LOCK;
37494 pInode->nShared--;
37495 if( pInode->nShared==0 ){
37500 pInode->eFileLock = NO_LOCK;
37504 pInode->eFileLock = NO_LOCK;
37505 pFile->eFileLock = NO_LOCK;
37513 pInode->nLock--;
37514 assert( pInode->nLock>=0 );
37515 if( pInode->nLock==0 ) closePendingFds(pFile);
37519 sqlite3_mutex_leave(pInode->pLockMutex);
37521 pFile->eFileLock = eFileLock;
37531 ** the requested locking level, this routine is a no-op.
37535 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
37560 if( pFile->h>=0 ){
37561 robust_close(pFile, pFile->h, __LINE__);
37562 pFile->h = -1;
37565 if( pFile->pId ){
37566 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
37567 osUnlink(pFile->pId->zCanonicalName);
37569 vxworksReleaseFileId(pFile->pId);
37570 pFile->pId = 0;
37574 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
37575 osUnlink(pFile->zPath);
37576 sqlite3_free(*(char**)&pFile->zPath);
37577 pFile->zPath = 0;
37580 OSTRACE(("CLOSE %-3d\n", pFile->h));
37581 OpenCounter(-1);
37582 sqlite3_free(pFile->pPreallocatedUnused);
37593 unixInodeInfo *pInode = pFile->pInode;
37604 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
37605 sqlite3_mutex_enter(pInode->pLockMutex);
37606 if( pInode->nLock ){
37609 ** descriptor to pInode->pUnused list. It will be automatically closed
37614 sqlite3_mutex_leave(pInode->pLockMutex);
37616 assert( pFile->pShm==0 );
37626 ****************************** No-op Locking **********************************
37632 ** This locking mode is appropriate for use on read-only databases
37633 ** (ex: databases that are burned into CD-ROM, for example.) It can
37663 /******************* End of the no-op lock implementation *********************
37667 ************************* Begin dot-file Locking ******************************
37676 ** (2) An application crash or power loss can leave stale lock files
37697 ** to a non-zero value otherwise *pResOut is set to zero. The return value
37712 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
37713 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
37719 ** Lock the file with the lock specified by parameter eFileLock - one
37733 ** UNLOCKED -> SHARED
37734 ** SHARED -> RESERVED
37735 ** SHARED -> (PENDING) -> EXCLUSIVE
37736 ** RESERVED -> (PENDING) -> EXCLUSIVE
37737 ** PENDING -> EXCLUSIVE
37747 char *zLockFile = (char *)pFile->lockingContext;
37754 if( pFile->eFileLock > NO_LOCK ){
37755 pFile->eFileLock = eFileLock;
37782 pFile->eFileLock = eFileLock;
37791 ** the requested locking level, this routine is a no-op.
37797 char *zLockFile = (char *)pFile->lockingContext;
37801 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
37802 pFile->eFileLock, osGetpid(0)));
37805 /* no-op if possible */
37806 if( pFile->eFileLock==eFileLock ){
37814 pFile->eFileLock = SHARED_LOCK;
37831 pFile->eFileLock = NO_LOCK;
37842 sqlite3_free(pFile->lockingContext);
37845 /****************** End of the dot-file lock implementation *******************
37853 ** flock() locking is like dot-file locking in that the various
37854 ** fine-grain locking levels supported by SQLite are collapsed into
37881 ** to a non-zero value otherwise *pResOut is set to zero. The return value
37894 if( pFile->eFileLock>SHARED_LOCK ){
37901 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
37904 lrc = robust_flock(pFile->h, LOCK_UN);
37923 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
37936 ** Lock the file with the lock specified by parameter eFileLock - one
37950 ** UNLOCKED -> SHARED
37951 ** SHARED -> RESERVED
37952 ** SHARED -> (PENDING) -> EXCLUSIVE
37953 ** RESERVED -> (PENDING) -> EXCLUSIVE
37954 ** PENDING -> EXCLUSIVE
37972 if (pFile->eFileLock > NO_LOCK) {
37973 pFile->eFileLock = eFileLock;
37979 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
37988 pFile->eFileLock = eFileLock;
37990 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
38006 ** the requested locking level, this routine is a no-op.
38012 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
38013 pFile->eFileLock, osGetpid(0)));
38016 /* no-op if possible */
38017 if( pFile->eFileLock==eFileLock ){
38023 pFile->eFileLock = eFileLock;
38028 if( robust_flock(pFile->h, LOCK_UN) ){
38034 pFile->eFileLock = NO_LOCK;
38058 ** Semaphore locking is like dot-lock and flock in that it really only
38068 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38081 if( pFile->eFileLock>SHARED_LOCK ){
38087 sem_t *pSem = pFile->pInode->pSem;
38089 if( sem_trywait(pSem)==-1 ){
38096 reserved = (pFile->eFileLock < SHARED_LOCK);
38103 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
38110 ** Lock the file with the lock specified by parameter eFileLock - one
38124 ** UNLOCKED -> SHARED
38125 ** SHARED -> RESERVED
38126 ** SHARED -> (PENDING) -> EXCLUSIVE
38127 ** RESERVED -> (PENDING) -> EXCLUSIVE
38128 ** PENDING -> EXCLUSIVE
38140 sem_t *pSem = pFile->pInode->pSem;
38145 if (pFile->eFileLock > NO_LOCK) {
38146 pFile->eFileLock = eFileLock;
38152 if( sem_trywait(pSem)==-1 ){
38158 pFile->eFileLock = eFileLock;
38169 ** the requested locking level, this routine is a no-op.
38173 sem_t *pSem = pFile->pInode->pSem;
38177 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
38178 pFile->eFileLock, osGetpid(0)));
38181 /* no-op if possible */
38182 if( pFile->eFileLock==eFileLock ){
38188 pFile->eFileLock = eFileLock;
38193 if ( sem_post(pSem)==-1 ) {
38201 pFile->eFileLock = NO_LOCK;
38234 ** on Apple Macintosh computers - both OS9 and OSX.
38236 ** Third-party implementations of AFP are available. But this code here
38263 ** This is a utility for setting or clearing a bit-range lock on an
38282 pb.fd = pFile->h;
38285 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
38288 if ( err==-1 ) {
38311 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38323 context = (afpLockingContext *) pFile->lockingContext;
38324 if( context->reserved ){
38328 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38330 if( pFile->pInode->eFileLock>SHARED_LOCK ){
38338 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
38342 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
38352 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38353 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
38360 ** Lock the file with the lock specified by parameter eFileLock - one
38374 ** UNLOCKED -> SHARED
38375 ** SHARED -> RESERVED
38376 ** SHARED -> (PENDING) -> EXCLUSIVE
38377 ** RESERVED -> (PENDING) -> EXCLUSIVE
38378 ** PENDING -> EXCLUSIVE
38386 unixInodeInfo *pInode = pFile->pInode;
38387 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38390 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
38391 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38392 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
38398 if( pFile->eFileLock>=eFileLock ){
38399 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
38409 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
38411 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
38413 /* This mutex is needed because pFile->pInode is shared across threads
38415 pInode = pFile->pInode;
38416 sqlite3_mutex_enter(pInode->pLockMutex);
38421 if( (pFile->eFileLock!=pInode->eFileLock &&
38422 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
38433 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
38435 assert( pFile->eFileLock==0 );
38436 assert( pInode->nShared>0 );
38437 pFile->eFileLock = SHARED_LOCK;
38438 pInode->nShared++;
38439 pInode->nLock++;
38448 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
38451 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
38465 assert( pInode->nShared==0 );
38466 assert( pInode->eFileLock==0 );
38469 /* Now get the read-lock SHARED_LOCK */
38472 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
38473 lrc1 = afpSetLock(context->dbPath, pFile,
38474 SHARED_FIRST+pInode->sharedByte, 1, 1);
38476 lrc1Errno = pFile->lastErrno;
38479 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
38491 pFile->eFileLock = SHARED_LOCK;
38492 pInode->nLock++;
38493 pInode->nShared = 1;
38495 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
38505 assert( 0!=pFile->eFileLock );
38506 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
38508 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
38510 context->reserved = 1;
38519 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
38520 pInode->sharedByte, 1, 0)) ){
38523 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
38525 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
38526 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
38544 pFile->eFileLock = eFileLock;
38545 pInode->eFileLock = eFileLock;
38547 pFile->eFileLock = PENDING_LOCK;
38548 pInode->eFileLock = PENDING_LOCK;
38552 sqlite3_mutex_leave(pInode->pLockMutex);
38553 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
38563 ** the requested locking level, this routine is a no-op.
38569 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38572 int h = pFile->h;
38576 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
38577 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
38581 if( pFile->eFileLock<=eFileLock ){
38584 pInode = pFile->pInode;
38585 sqlite3_mutex_enter(pInode->pLockMutex);
38586 assert( pInode->nShared!=0 );
38587 if( pFile->eFileLock>SHARED_LOCK ){
38588 assert( pInode->eFileLock==pFile->eFileLock );
38590 SimulateIOError( h=(-1) )
38600 ** cache. The use of a stale cache can lead to database corruption.
38602 assert( pFile->inNormalWrite==0
38603 || pFile->dbUpdate==0
38604 || pFile->transCntrChng==1 );
38605 pFile->inNormalWrite = 0;
38608 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
38609 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
38610 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
38611 /* only re-establish the shared lock if necessary */
38612 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
38613 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
38618 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
38619 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
38621 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
38622 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
38624 context->reserved = 0;
38627 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
38628 pInode->eFileLock = SHARED_LOCK;
38637 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
38638 pInode->nShared--;
38639 if( pInode->nShared==0 ){
38641 SimulateIOError( h=(-1) )
38644 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
38647 pInode->eFileLock = NO_LOCK;
38648 pFile->eFileLock = NO_LOCK;
38652 pInode->nLock--;
38653 assert( pInode->nLock>=0 );
38654 if( pInode->nLock==0 ) closePendingFds(pFile);
38658 sqlite3_mutex_leave(pInode->pLockMutex);
38660 pFile->eFileLock = eFileLock;
38675 if( pFile->pInode ){
38676 unixInodeInfo *pInode = pFile->pInode;
38677 sqlite3_mutex_enter(pInode->pLockMutex);
38678 if( pInode->nLock ){
38681 ** descriptor to pInode->aPending. It will be automatically closed when
38686 sqlite3_mutex_leave(pInode->pLockMutex);
38689 sqlite3_free(pFile->lockingContext);
38699 ** is available. If you don't compile for a mac, then the "unix-afp"
38714 ** the requested locking level, this routine is a no-op.
38730 **************** Non-locking sqlite3_file methods *****************************
38760 assert( id->h>2 );
38763 got = osPread(id->h, pBuf, cnt, offset);
38764 SimulateIOError( got = -1 );
38766 got = osPread64(id->h, pBuf, cnt, offset);
38767 SimulateIOError( got = -1 );
38769 newOffset = lseek(id->h, offset, SEEK_SET);
38770 SimulateIOError( newOffset = -1 );
38773 return -1;
38775 got = osRead(id->h, pBuf, cnt);
38784 cnt -= got;
38791 OSTRACE(("READ %-3d %5d %7lld %llu\n",
38792 id->h, got+prior, offset-prior, TIMER_ELAPSED));
38813 /* If this is a database file (not a journal, super-journal or temp
38816 assert( pFile->pPreallocatedUnused==0
38825 if( offset<pFile->mmapSize ){
38826 if( offset+amt <= pFile->mmapSize ){
38827 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
38830 int nCopy = pFile->mmapSize - offset;
38831 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
38833 amt -= nCopy;
38843 /* pFile->lastErrno has been set by seekAndRead().
38850 switch( pFile->lastErrno ){
38864 /* Unread parts of the buffer must be zero-filled */
38865 memset(&((char*)pBuf)[got], 0, amt-got);
38871 ** Attempt to seek the file-descriptor passed as the first argument to
38873 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
38899 SimulateIOError( iSeek = -1 );
38901 rc = -1;
38909 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
38917 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
38924 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
38943 /* If this is a database file (not a journal, super-journal or temp
38946 assert( pFile->pPreallocatedUnused==0
38954 ** doing a hot-journal rollback or a write to some file other than a
38959 if( pFile->inNormalWrite ){
38960 pFile->dbUpdate = 1; /* The database has been modified */
38967 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
38968 pFile->transCntrChng = 1; /* The transaction counter has changed */
38977 if( offset<pFile->mmapSize ){
38978 if( offset+amt <= pFile->mmapSize ){
38979 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
38982 int nCopy = pFile->mmapSize - offset;
38983 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
38985 amt -= nCopy;
38992 amt -= wrote;
38996 SimulateIOError(( wrote=(-1), amt=1 ));
39000 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
39025 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
39095 ** no-op. But go ahead and call fstat() to validate the file
39128 if( rc==-1 && errno==ENOTSUP ){
39134 if( OS_VXWORKS && rc!= -1 ){
39147 ** The directory file descriptor is used for only one thing - to
39156 ** replace this routine with a harmless no-op. To make this routine
39157 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
39165 int fd = -1;
39169 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
39178 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
39193 ** has been created by fsync-ing the directory that contains the file.
39198 ** will not roll back - possibly leading to database corruption.
39218 OSTRACE(("SYNC %-3d\n", pFile->h));
39219 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
39223 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
39227 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
39230 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
39232 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
39234 rc = osOpenDirectory(pFile->zPath, &dirfd);
39242 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
39256 /* If the user has configured a chunk-size for this file, truncate the
39261 if( pFile->szChunk>0 ){
39262 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
39265 rc = robust_ftruncate(pFile->h, nByte);
39268 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
39272 ** doing a hot-journal rollback or a write to some file other than a
39275 ** when restoring a database using the backup API from a zero-length
39278 if( pFile->inNormalWrite && nByte==0 ){
39279 pFile->transCntrChng = 1;
39288 if( nByte<pFile->mmapSize ){
39289 pFile->mmapSize = nByte;
39304 rc = osFstat(((unixFile*)id)->h, &buf);
39312 /* When opening a zero-size database, the findInodeInfo() procedure
39314 ** in the OS-X msdos filesystem. In order to avoid problems with upper
39326 ** Handler for proxy-locking file-control verbs. Defined below in the
39334 ** file-control operation. Enlarge the database to nBytes in size
39335 ** (rounded up to the next chunk-size). If the database is already
39336 ** nBytes or larger, this routine is a no-op.
39339 if( pFile->szChunk>0 ){
39343 if( osFstat(pFile->h, &buf) ){
39347 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
39356 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
39363 ** at offset (nSize-1), to set the size of the file correctly.
39367 int nBlk = buf.st_blksize; /* File-system block size */
39371 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
39374 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
39375 if( iWrite>=nSize ) iWrite = nSize - 1;
39384 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
39386 if( pFile->szChunk<=0 ){
39387 if( robust_ftruncate(pFile->h, nByte) ){
39389 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
39403 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
39405 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
39409 *pArg = (pFile->ctrlFlags & mask)!=0;
39411 pFile->ctrlFlags &= ~mask;
39413 pFile->ctrlFlags |= mask;
39431 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
39435 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
39439 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
39445 *(int*)pArg = pFile->eFileLock;
39449 *(int*)pArg = pFile->lastErrno;
39453 pFile->szChunk = *(int *)pArg;
39472 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
39476 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
39478 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
39489 int iOld = pFile->iBusyTimeout;
39490 pFile->iBusyTimeout = *(int*)pArg;
39505 ** 64-bit type. */
39510 *(i64*)pArg = pFile->mmapSizeMax;
39511 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
39512 pFile->mmapSizeMax = newLimit;
39513 if( pFile->mmapSize>0 ){
39515 rc = unixMapfile(pFile, -1);
39528 ((unixFile*)id)->dbUpdate = 0;
39552 ** If pFd->sectorSize is non-zero when this function is called, it is a
39553 ** no-op. Otherwise, the values of pFd->sectorSize and
39554 ** pFd->deviceCharacteristics are set according to the file-system
39562 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
39563 if( pFd->sectorSize==0 ){
39569 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
39571 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
39576 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
39577 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
39580 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39587 if( pFile->sectorSize == 0 ){
39590 /* Set defaults for non-supported filesystems */
39591 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39592 pFile->deviceCharacteristics = 0;
39593 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
39598 pFile->sectorSize = fsInfo.f_bsize;
39599 pFile->deviceCharacteristics =
39607 pFile->sectorSize = fsInfo.f_bsize;
39608 pFile->deviceCharacteristics =
39610 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
39617 pFile->sectorSize = fsInfo.f_bsize;
39618 pFile->deviceCharacteristics =
39626 pFile->sectorSize = fsInfo.f_bsize;
39627 pFile->deviceCharacteristics =
39629 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
39634 pFile->sectorSize = fsInfo.f_bsize;
39635 pFile->deviceCharacteristics =
39637 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
39642 pFile->deviceCharacteristics =
39651 if( pFile->sectorSize % 512 != 0 ){
39652 pFile->deviceCharacteristics = 0;
39653 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39664 ** if two files are created in the same file-system directory (i.e.
39671 return pFd->sectorSize;
39680 ** words, after a power-loss event, parts of the file that were never
39681 ** written might end up being altered.) However, non-PSOW behavior is very,
39684 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
39690 return pFd->deviceCharacteristics;
39718 ** When multiple threads all reference the same wal-index, each thread
39720 ** of this unixShmNode object. In other words, each wal-index is opened
39735 ** The following fields are read-only after the object is created:
39749 int szRegion; /* Size of shared-memory regions */
39751 u8 isReadonly; /* True if read-only */
39753 char **apRegion; /* Array of mapped shared-memory regions */
39756 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
39769 ** are read-only thereafter:
39774 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
39780 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
39794 ** wal-mode transactions in other processes on database file pFile. If
39803 if( pFile->pShm){
39804 unixShmNode *pShmNode = pFile->pShm->pShmNode;
39811 f.l_len = SQLITE_SHM_NLOCK - 3;
39813 sqlite3_mutex_enter(pShmNode->pShmMutex);
39814 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
39819 sqlite3_mutex_leave(pShmNode->pShmMutex);
39827 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
39829 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
39838 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
39843 pShmNode = pFile->pInode->pShmNode;
39844 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
39845 assert( pShmNode->nRef>0 || unixMutexHeld() );
39853 if( pShmNode->hShm>=0 ){
39860 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
39861 if( res==-1 ){
39863 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
39873 OSTRACE(("SHM-LOCK "));
39874 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
39878 pShmNode->exclMask &= ~mask;
39879 pShmNode->sharedMask &= ~mask;
39881 OSTRACE(("read-lock %d ok", ofst));
39882 pShmNode->exclMask &= ~mask;
39883 pShmNode->sharedMask |= mask;
39886 OSTRACE(("write-lock %d ok", ofst));
39887 pShmNode->exclMask |= mask;
39888 pShmNode->sharedMask &= ~mask;
39894 OSTRACE(("read-lock failed"));
39897 OSTRACE(("write-lock %d failed", ofst));
39900 OSTRACE((" - afterwards %03x,%03x\n",
39901 pShmNode->sharedMask, pShmNode->exclMask));
39911 ** current system page-size.
39914 ** to use 64KB pages - in this case each mapping must cover at least two
39920 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
39928 ** This is not a VFS shared-memory method; it is a utility function called
39929 ** by VFS shared-memory methods.
39932 unixShmNode *p = pFd->pInode->pShmNode;
39934 if( p && ALWAYS(p->nRef==0) ){
39937 assert( p->pInode==pFd->pInode );
39938 sqlite3_mutex_free(p->pShmMutex);
39939 for(i=0; i<p->nRegion; i+=nShmPerMap){
39940 if( p->hShm>=0 ){
39941 osMunmap(p->apRegion[i], p->szRegion);
39943 sqlite3_free(p->apRegion[i]);
39946 sqlite3_free(p->apRegion);
39947 if( p->hShm>=0 ){
39948 robust_close(pFd, p->hShm, __LINE__);
39949 p->hShm = -1;
39951 p->pInode->pShmNode = 0;
39963 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
39972 ** and proceed with opening the *-shm file.
39976 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
39983 ** EXCLUSIVE failed just before truncating the *-shm file, then this
39984 ** process might open and use the *-shm file without truncating it.
39985 ** And if the *-shm file has been corrupted by a power failure or
39991 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
39994 if( pShmNode->isReadonly ){
39995 pShmNode->isUnlocked = 1;
39999 /* The first connection to attach must truncate the -shm file. We
40001 ** -shm header size) rather than 0 as a system debugging aid, to
40002 ** help detect if a -shm file truncation is legitimate or is the work
40004 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
40005 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
40020 ** Open a shared-memory area associated with open database file pDbFd.
40023 ** The file used to implement shared-memory is in the same directory
40025 ** file with the "-shm" suffix added. For example, if the database file
40027 ** for shared memory will be called "/home/user1/config.db-shm".
40033 ** database to end up using different files for shared memory -
40034 ** meaning that their memory would not really be shared - resulting
40036 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
40037 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
40041 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
40044 ** When opening a new shared-memory file, if no other instances of that
40048 ** If the original database file (pDbFd) is using the "unix-excl" VFS
40066 assert( pDbFd->pShm==0 );
40073 pInode = pDbFd->pInode;
40074 pShmNode = pInode->pShmNode;
40078 const char *zBasePath = pDbFd->zPath;
40082 ** a new *-shm file is created, an attempt will be made to create it
40085 if( osFstat(pDbFd->h, &sStat) ){
40101 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
40104 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
40107 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
40108 sqlite3FileSuffix3(pDbFd->zPath, zShm);
40110 pShmNode->hShm = -1;
40111 pDbFd->pInode->pShmNode = pShmNode;
40112 pShmNode->pInode = pDbFd->pInode;
40114 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
40115 if( pShmNode->pShmMutex==0 ){
40121 if( pInode->bProcessLock==0 ){
40122 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
40123 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
40126 if( pShmNode->hShm<0 ){
40127 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
40129 if( pShmNode->hShm<0 ){
40133 pShmNode->isReadonly = 1;
40140 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
40148 p->pShmNode = pShmNode;
40150 p->id = pShmNode->nextShmId++;
40152 pShmNode->nRef++;
40153 pDbFd->pShm = p;
40160 ** at pShmNode->pFirst. This must be done while holding the
40161 ** pShmNode->pShmMutex.
40163 sqlite3_mutex_enter(pShmNode->pShmMutex);
40164 p->pNext = pShmNode->pFirst;
40165 pShmNode->pFirst = p;
40166 sqlite3_mutex_leave(pShmNode->pShmMutex);
40179 ** shared-memory associated with the database file fd. Shared-memory regions
40180 ** are numbered starting from zero. Each shared-memory region is szRegion
40185 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
40188 ** bExtend is non-zero and the requested shared-memory region has not yet
40191 ** If the shared-memory region has already been allocated or is allocated by
40210 /* If the shared-memory file has not yet been opened, open it now. */
40211 if( pDbFd->pShm==0 ){
40216 p = pDbFd->pShm;
40217 pShmNode = p->pShmNode;
40218 sqlite3_mutex_enter(pShmNode->pShmMutex);
40219 if( pShmNode->isUnlocked ){
40222 pShmNode->isUnlocked = 0;
40224 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
40225 assert( pShmNode->pInode==pDbFd->pInode );
40226 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
40227 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
40232 if( pShmNode->nRegion<nReqRegion ){
40237 pShmNode->szRegion = szRegion;
40239 if( pShmNode->hShm>=0 ){
40241 ** Check to see if it has been allocated (i.e. if the wal-index file is
40244 if( osFstat(pShmNode->hShm, &sStat) ){
40272 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
40273 const char *zFile = pShmNode->zFilename;
40284 pShmNode->apRegion, nReqRegion*sizeof(char *)
40290 pShmNode->apRegion = apNew;
40291 while( pShmNode->nRegion<nReqRegion ){
40295 if( pShmNode->hShm>=0 ){
40297 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
40298 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
40301 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
40314 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
40316 pShmNode->nRegion += nShmPerMap;
40321 if( pShmNode->nRegion>iRegion ){
40322 *pp = pShmNode->apRegion[iRegion];
40326 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
40327 sqlite3_mutex_leave(pShmNode->pShmMutex);
40332 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
40342 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
40345 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
40348 if( pX->exclMask & (1<<i) ){
40350 aLock[i] = -1;
40351 }else if( pX->sharedMask & (1<<i) ){
40358 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
40359 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
40364 ** Change the lock state for a shared-memory segment.
40384 p = pDbFd->pShm;
40386 pShmNode = p->pShmNode;
40388 aLock = pShmNode->aLock;
40390 assert( pShmNode==pDbFd->pInode->pShmNode );
40391 assert( pShmNode->pInode==pDbFd->pInode );
40399 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
40400 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
40416 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
40418 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
40419 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
40420 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
40424 mask = (1<<(ofst+n)) - (1<<ofst);
40426 sqlite3_mutex_enter(pShmNode->pShmMutex);
40429 if( (p->exclMask|p->sharedMask) & mask ){
40434 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
40444 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
40446 aLock[ofst]--;
40451 p->exclMask &= ~mask;
40452 p->sharedMask &= ~mask;
40457 assert( (p->exclMask & (1<<ofst))==0 );
40458 if( (p->sharedMask & mask)==0 ){
40467 p->sharedMask |= mask;
40476 assert( (p->sharedMask & mask)==0 );
40477 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
40484 ** also update the in-memory values. */
40488 assert( (p->sharedMask & mask)==0 );
40489 p->exclMask |= mask;
40491 aLock[ii] = -1;
40497 sqlite3_mutex_leave(pShmNode->pShmMutex);
40498 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
40499 p->id, osGetpid(0), p->sharedMask, p->exclMask));
40513 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
40514 assert( fd->pMethods->xLock==nolockLock
40522 ** Close a connection to shared-memory. Delete the underlying
40526 ** routine is a harmless no-op.
40530 int deleteFlag /* Delete shared-memory if true */
40533 unixShmNode *pShmNode; /* The underlying shared-memory file */
40538 p = pDbFd->pShm;
40540 pShmNode = p->pShmNode;
40542 assert( pShmNode==pDbFd->pInode->pShmNode );
40543 assert( pShmNode->pInode==pDbFd->pInode );
40547 sqlite3_mutex_enter(pShmNode->pShmMutex);
40548 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
40549 *pp = p->pNext;
40553 pDbFd->pShm = 0;
40554 sqlite3_mutex_leave(pShmNode->pShmMutex);
40556 /* If pShmNode->nRef has reached 0, then close the underlying
40557 ** shared-memory file, too */
40560 assert( pShmNode->nRef>0 );
40561 pShmNode->nRef--;
40562 if( pShmNode->nRef==0 ){
40563 if( deleteFlag && pShmNode->hShm>=0 ){
40564 osUnlink(pShmNode->zFilename);
40586 assert( pFd->nFetchOut==0 );
40587 if( pFd->pMapRegion ){
40588 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
40589 pFd->pMapRegion = 0;
40590 pFd->mmapSize = 0;
40591 pFd->mmapSizeActual = 0;
40615 int h = pFd->h; /* File descriptor open on db file */
40616 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
40617 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
40621 assert( pFd->nFetchOut==0 );
40622 assert( nNew>pFd->mmapSize );
40623 assert( nNew<=pFd->mmapSizeMax );
40625 assert( pFd->mmapSizeActual>=pFd->mmapSize );
40629 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
40634 i64 nReuse = pFd->mmapSize;
40637 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
40643 osMunmap(pReq, nOrig-nReuse);
40650 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
40653 osMunmap(pNew, nNew - nReuse);
40675 unixLogError(SQLITE_OK, zErr, pFd->zPath);
40680 pFd->mmapSizeMax = 0;
40682 pFd->pMapRegion = (void *)pNew;
40683 pFd->mmapSize = pFd->mmapSizeActual = nNew;
40687 ** Memory map or remap the file opened by file-descriptor pFd (if the file
40690 ** outstanding xFetch() references to it, this function is a no-op.
40692 ** If parameter nByte is non-negative, then it is the requested size of
40703 assert( nMap>=0 || pFd->nFetchOut==0 );
40704 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
40705 if( pFd->nFetchOut>0 ) return SQLITE_OK;
40708 struct stat statbuf; /* Low-level file information */
40709 if( osFstat(pFd->h, &statbuf) ){
40714 if( nMap>pFd->mmapSizeMax ){
40715 nMap = pFd->mmapSizeMax;
40718 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
40719 if( nMap!=pFd->mmapSize ){
40746 if( pFd->mmapSizeMax>0 ){
40747 if( pFd->pMapRegion==0 ){
40748 int rc = unixMapfile(pFd, -1);
40751 if( pFd->mmapSize >= iOff+nAmt ){
40752 *pp = &((u8 *)pFd->pMapRegion)[iOff];
40753 pFd->nFetchOut++;
40761 ** If the third argument is non-NULL, then this function releases a
40778 assert( (p==0)==(pFd->nFetchOut==0) );
40781 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
40784 pFd->nFetchOut--;
40789 assert( pFd->nFetchOut>=0 );
40807 ** of "finder" functions. A finder-function is used to locate the appropriate
40810 ** the correct finder-function for that VFS.
40813 ** object. The only interesting finder-function is autolockIoFinder, which
40817 ** For finder-function F, two objects are created:
40819 ** (1) The real finder-function named "FImpt()".
40826 ** directly at the finder-function since C90 rules prevent a void*
40943 ** The proxy locking method is a "super-method" in the sense that it
40945 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
41014 if( statfs(filePath, &fsInfo) != -1 ){
41026 ** Test byte-range lock using fcntl(). If the call succeeds,
41027 ** assume that the file-system supports POSIX style locks.
41033 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
41073 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
41111 assert( pNew->pInode==NULL );
41116 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
41117 pNew->h = h;
41118 pNew->pVfs = pVfs;
41119 pNew->zPath = zFilename;
41120 pNew->ctrlFlags = (u8)ctrlFlags;
41122 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
41126 pNew->ctrlFlags |= UNIXFILE_PSOW;
41128 if( strcmp(pVfs->zName,"unix-excl")==0 ){
41129 pNew->ctrlFlags |= UNIXFILE_EXCL;
41133 pNew->pId = vxworksFindFileId(zFilename);
41134 if( pNew->pId==0 ){
41143 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
41148 pNew->lockingContext = (void*)zFilename;
41158 rc = findInodeInfo(pNew, &pNew->pInode);
41171 ** handle h - as it is guaranteed that no posix locks will be released
41179 h = -1;
41190 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41197 pCtx->dbPath = zFilename;
41198 pCtx->reserved = 0;
41201 rc = findInodeInfo(pNew, &pNew->pInode);
41203 sqlite3_free(pNew->lockingContext);
41205 h = -1;
41226 pNew->lockingContext = zLockFile;
41235 rc = findInodeInfo(pNew, &pNew->pInode);
41236 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
41237 char *zSemName = pNew->pInode->aSemName;
41240 pNew->pId->zCanonicalName);
41243 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
41244 if( pNew->pInode->pSem == SEM_FAILED ){
41246 pNew->pInode->aSemName[0] = '\0';
41257 h = -1;
41259 pNew->ctrlFlags |= UNIXFILE_DELETE;
41265 pId->pMethods = pLockingStyle;
41318 ** pVfs->mxPathname bytes.
41324 /* It's odd to simulate an io-error here, but really this is just
41325 ** using the io-error infrastructure to test that SQLite handles this
41337 zBuf[nBuf-2] = 0;
41340 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
41347 ** Routine to transform a unixFile into a proxy-locking unixFile.
41348 ** Implementation in the proxy-lock division, but used by unixOpen()
41356 ** file (not a journal or super-journal file) identified by pathname
41362 ** other file descriptor open on the same file is holding a file-lock.
41368 ** such file descriptor is located, -1 is returned.
41386 ** ignored and -1 is returned. The caller will try to open a new file
41395 while( pInode && (pInode->fileId.dev!=sStat.st_dev
41396 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
41397 pInode = pInode->pNext;
41401 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
41402 sqlite3_mutex_enter(pInode->pLockMutex);
41404 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
41407 *pp = pUnused->pNext;
41409 sqlite3_mutex_leave(pInode->pLockMutex);
41449 ** this function queries the file-system for the permissions on the
41478 ** "<path to db>-journal"
41479 ** "<path to db>-wal"
41480 ** "<path to db>-journalNN"
41481 ** "<path to db>-walNN"
41487 ** a '-' character. However in 8+3 filename mode, or if a corrupt
41488 ** rollback journal specifies a super-journal with a goofy name, then
41489 ** the '-' might be missing or the '-' might be the first character in
41492 nDb = sqlite3Strlen30(zPath) - 1;
41494 if( zPath[nDb]=='-' ){
41500 nDb--;
41529 ** ReadWrite() -> (READWRITE | CREATE)
41530 ** ReadOnly() -> (READONLY)
41531 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
41533 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
41547 int fd = -1; /* File descriptor returned by open() */
41566 /* If creating a super- or main-file journal, this function will open
41567 ** a file-descriptor on the directory too. The first time unixSync()
41594 /* The main DB, main journal, WAL file and super-journal are never
41601 /* Assert that the upper layer has set one of the "file-type" flags. */
41628 fd = pUnused->fd;
41635 p->pPreallocatedUnused = pUnused;
41637 /* Database filenames are double-zero terminated if they are not
41645 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
41651 /* Generated temporary filenames are always double-zero terminated
41672 assert( !p->pPreallocatedUnused );
41677 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
41685 /* Failed to open the file for read/write access. Try read-only. */
41702 ** the case. The chown() system call will be a no-op if the current
41721 if( p->pPreallocatedUnused ){
41722 p->pPreallocatedUnused->fd = fd;
41723 p->pPreallocatedUnused->flags =
41742 p->openFlags = openFlags;
41747 if( fstatfs(fd, &fsInfo) == -1 ){
41753 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
41756 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
41772 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
41777 ** never use proxy, NULL means use proxy for non-local files only. */
41790 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
41808 sqlite3_free(p->pPreallocatedUnused);
41826 if( osUnlink(zPath)==(-1) ){
41895 int rc; /* Non-zero following any error */
41910 const char *zName, /* Name to append to pPath. Not zero-terminated */
41918 if( pPath->nUsed<=1 ){
41919 pPath->rc = SQLITE_ERROR;
41922 assert( pPath->zOut[0]=='/' );
41923 while( pPath->zOut[--pPath->nUsed]!='/' ){}
41927 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
41928 pPath->rc = SQLITE_ERROR;
41931 pPath->zOut[pPath->nUsed++] = '/';
41932 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
41933 pPath->nUsed += nName;
41935 if( pPath->rc==SQLITE_OK ){
41938 pPath->zOut[pPath->nUsed] = 0;
41939 zIn = pPath->zOut;
41942 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
41947 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
41948 pPath->rc = SQLITE_CANTOPEN_BKPT;
41951 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
41952 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
41953 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
41958 pPath->nUsed = 0;
41960 pPath->nUsed -= nName + 1;
41973 const char *zPath /* Path to append to pPath. Is zero-terminated */
41980 appendOnePathElement(pPath, &zPath[j], i-j);
41988 ** is stored as a nul-terminated string in the buffer pointed to by
41992 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
42010 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
42052 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
42055 ** use dlsym() with -pedantic-errors?
42063 ** This work-around is unlikely to work correctly on any system where
42092 ** errors. The reports issued by valgrind are incorrect - we would
42094 ** uninitialized space in zBuf - but valgrind errors tend to worry
42157 ** The following variable, if set to a non-zero value, is interpreted as
42221 ** low-level error message when operating-system problems come up
42240 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
42242 ** meta-layer over top of the primitive locking implemented above. For
42245 ** been defined - so that the primitive locking methods are available
42250 ** The default locking schemes in SQLite use byte-range locks on the
42255 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
42263 ** SHARED_RANGE 0x40000002 -> 0x40000200
42267 ** the read cache when byte-range locks are present. Enabling the read
42270 ** close-to-open semantics for ensuring cache coherency
42274 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
42283 ** -----------------
42315 ** -----------------------
42325 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
42326 ** by taking an sqlite-style shared lock on the conch file, reading the
42330 ** is patterned after the database file name as ".<databasename>-conch".
42338 ** The proxy file - a single-byte file used for all advisory file locks
42354 ** ---------------------
42358 ** Database files accessed on non-local file systems are
42403 int conchHeld; /* 1 if the conch is held, -1 if lockless */
42436 if( lPath[len-1]!='/' ){
42465 if( lockPath[i] == '/' && (i - start > 0) ){
42467 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
42468 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
42500 int fd = -1;
42509 ** 2. if that fails, and this is a lock file (not-conch), try creating
42511 ** 3. if that fails, try to open the file read-only
42516 fd = pUnused->fd;
42557 pNew->openFlags = openFlags;
42561 pUnused->fd = fd;
42562 pUnused->flags = openFlags;
42563 pNew->pPreallocatedUnused = pUnused;
42621 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
42633 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42634 unixFile *conchFile = pCtx->conchFile;
42637 char *cPath = pCtx->conchFilePath;
42641 int fd = -1;
42642 int rc = -1;
42645 /* create a new path by replace the trailing '-conch' with '-break' */
42648 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
42653 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
42673 fprintf(stderr, "broke stale lock on %s\n", cPath);
42674 robust_close(pFile, conchFile->h, __LINE__);
42675 conchFile->h = fd;
42676 conchFile->openFlags = O_RDWR | O_CREAT;
42684 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
42689 /* Take the requested lock on the conch file and break a stale lock if the
42693 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42694 unixFile *conchFile = pCtx->conchFile;
42701 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42711 if( osFstat(conchFile->h, &buf) ){
42730 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
42752 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
42755 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42765 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
42771 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42773 if( pCtx->conchHeld!=0 ){
42776 unixFile *conchFile = pCtx->conchFile;
42789 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
42790 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
42806 storeLastErrno(pFile, conchFile->lastErrno);
42818 ** retry with a new auto-generated path
42826 if( !pCtx->lockProxyPath ){
42827 /* for auto-named local lock file, just check the host ID and we'll
42831 size_t pathLen = (readLen - PROXY_PATHINDEX);
42834 pathLen=MAXPATHLEN-1;
42844 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
42845 readLen-PROXY_PATHINDEX)
42853 if( (conchFile->openFlags&O_RDWR) == 0 ){
42859 if( !pCtx->lockProxyPath ){
42860 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
42869 futimes(conchFile->h, NULL);
42871 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
42887 if( pCtx->lockProxyPath!=NULL ){
42888 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
42894 robust_ftruncate(conchFile->h, writeSize);
42896 full_fsync(conchFile->h,0,0);
42902 int err = osFstat(pFile->h, &buf);
42908 osFchmod(conchFile->h, cmode);
42911 rc = osFchmod(conchFile->h, cmode);
42912 }while( rc==(-1) && errno==EINTR );
42928 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
42931 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
42932 if( rc==SQLITE_OK && pFile->openFlags ){
42934 if( pFile->h>=0 ){
42935 robust_close(pFile, pFile->h, __LINE__);
42937 pFile->h = -1;
42938 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
42941 pFile->h = fd;
42947 if( rc==SQLITE_OK && !pCtx->lockProxy ){
42948 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
42949 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
42952 ** so try again via auto-naming
42964 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
42965 if( !pCtx->lockProxyPath ){
42971 pCtx->conchHeld = 1;
42973 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
42975 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
42976 afpCtx->dbPath = pCtx->lockProxyPath;
42979 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
42981 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
42984 } while (1); /* in case we need to retry the :auto: lock file -
42997 pCtx = (proxyLockingContext *)pFile->lockingContext;
42998 conchFile = pCtx->conchFile;
42999 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
43000 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
43002 if( pCtx->conchHeld>0 ){
43003 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
43005 pCtx->conchHeld = 0;
43006 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
43024 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
43036 for( i=(len-1); i>=0; i-- ){
43048 /* append the "-conch" suffix to the file */
43049 memcpy(&conchPath[i+1], "-conch", 7);
43056 /* Takes a fully configured proxy locking-style unix file and switches
43060 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43061 char *oldPath = pCtx->lockProxyPath;
43064 if( pFile->eFileLock!=NO_LOCK ){
43073 unixFile *lockProxy = pCtx->lockProxy;
43074 pCtx->lockProxy=NULL;
43075 pCtx->conchHeld = 0;
43077 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
43082 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
43097 if( pFile->pMethod == &afpIoMethods ){
43100 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
43101 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
43105 if( pFile->pMethod == &dotlockIoMethods ){
43108 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
43109 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
43112 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
43113 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
43123 ** ->lockingContext
43124 ** ->pMethod
43132 if( pFile->eFileLock!=NO_LOCK ){
43142 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
43151 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
43153 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
43154 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
43156 ** (c) the file system is read-only, then enable no-locking access.
43164 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
43166 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
43171 pCtx->conchHeld = -1; /* read only FS/ lockless */
43177 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
43181 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
43182 if( pCtx->dbPath==NULL ){
43190 pCtx->oldLockingContext = pFile->lockingContext;
43191 pFile->lockingContext = pCtx;
43192 pCtx->pOldMethod = pFile->pMethod;
43193 pFile->pMethod = &proxyIoMethods;
43195 if( pCtx->conchFile ){
43196 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
43197 sqlite3_free(pCtx->conchFile);
43199 sqlite3DbFree(0, pCtx->lockProxyPath);
43200 sqlite3_free(pCtx->conchFilePath);
43203 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
43217 if( pFile->pMethod == &proxyIoMethods ){
43218 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43220 if( pCtx->lockProxyPath ){
43221 *(const char **)pArg = pCtx->lockProxyPath;
43233 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
43236 /* turn off proxy locking - not supported. If support is added for
43242 /* turn off proxy locking - already off - NOOP */
43249 (proxyLockingContext*)pFile->lockingContext;
43251 || (pCtx->lockProxyPath &&
43252 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
43275 ** above this point are all utilities. The lock-related methods of the
43276 ** proxy-locking sqlite3_io_method object follow.
43283 ** to a non-zero value otherwise *pResOut is set to zero. The return value
43290 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43291 if( pCtx->conchHeld>0 ){
43292 unixFile *proxy = pCtx->lockProxy;
43293 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
43302 ** Lock the file with the lock specified by parameter eFileLock - one
43316 ** UNLOCKED -> SHARED
43317 ** SHARED -> RESERVED
43318 ** SHARED -> (PENDING) -> EXCLUSIVE
43319 ** RESERVED -> (PENDING) -> EXCLUSIVE
43320 ** PENDING -> EXCLUSIVE
43329 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43330 if( pCtx->conchHeld>0 ){
43331 unixFile *proxy = pCtx->lockProxy;
43332 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
43333 pFile->eFileLock = proxy->eFileLock;
43347 ** the requested locking level, this routine is a no-op.
43353 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43354 if( pCtx->conchHeld>0 ){
43355 unixFile *proxy = pCtx->lockProxy;
43356 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
43357 pFile->eFileLock = proxy->eFileLock;
43371 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43372 unixFile *lockProxy = pCtx->lockProxy;
43373 unixFile *conchFile = pCtx->conchFile;
43377 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
43379 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
43382 pCtx->lockProxy = 0;
43385 if( pCtx->conchHeld ){
43389 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
43393 sqlite3DbFree(0, pCtx->lockProxyPath);
43394 sqlite3_free(pCtx->conchFilePath);
43395 sqlite3DbFree(0, pCtx->dbPath);
43397 pFile->lockingContext = pCtx->oldLockingContext;
43398 pFile->pMethod = pCtx->pOldMethod;
43400 return pFile->pMethod->xClose(id);
43420 ** This routine registers all VFS implementations for unix-like operating
43437 ** when compiling with -pedantic-errors on GCC.)
43440 ** finder-function. The finder-function returns a pointer to the
43443 ** macro for addition information on finder-functions.
43491 UNIXVFS("unix-none", nolockIoFinder ),
43492 UNIXVFS("unix-dotfile", dotlockIoFinder ),
43493 UNIXVFS("unix-excl", posixIoFinder ),
43495 UNIXVFS("unix-namedsem", semIoFinder ),
43498 UNIXVFS("unix-posix", posixIoFinder ),
43501 UNIXVFS("unix-flock", flockIoFinder ),
43504 UNIXVFS("unix-afp", afpIoFinder ),
43505 UNIXVFS("unix-nfs", nfsIoFinder ),
43506 UNIXVFS("unix-proxy", proxyIoFinder ),
43511 /* Double-check that the aSyscall[] array has been constructed
43529 ** READ-0 UNIX_SHM_BASE+3 123
43530 ** READ-1 UNIX_SHM_BASE+4 124
43531 ** READ-2 UNIX_SHM_BASE+5 125
43532 ** READ-3 UNIX_SHM_BASE+6 126
43533 ** READ-4 UNIX_SHM_BASE+7 127
43536 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
43550 ** This routine is a no-op for unix.
43604 ** based on the sub-platform)?
43612 ** based on the sub-platform)?
43700 ** characters, so we allocate 4 bytes per character assuming worst-case of
43701 ** 4-bytes-per-character for UTF8.
43724 ** Returns non-zero if the character should be treated as a directory
43788 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
43801 typedef struct winShm winShm; /* A connection to shared-memory */
43802 typedef struct winShmNode winShmNode; /* A region of shared-memory */
43860 BOOL bNoLock; /* Non-zero if locking is disabled. */
43874 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
43884 * If this is non-zero, an isolated heap will be created by the native Win32
43890 * WARNING: It is important to note that when this setting is non-zero and the
43902 * This is the maximum possible initial size of the Win32-specific heap, in
43910 * This is the extra space for the initial size of the Win32-specific heap,
43923 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
43930 * Win32-specific heap. It cannot be negative.
43936 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
43942 * initial size of the Win32-specific heap to exceed the maximum amount
43951 * The initial size of the Win32-specific heap. This value may be zero.
43960 * The maximum size of the Win32-specific heap. This value may be zero.
43976 ** The winMemData structure stores information required by the Win32-specific
44060 ** Many system calls are accessed through pointer-to-functions so that
44677 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
44789 int i = -1;
44793 for(i=0; i<ArraySize(aSyscall)-1; i++){
44808 ** "pnLargest" argument, if non-zero, will be used to return the size of the
44898 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
44899 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
44900 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
44966 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
44994 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
45019 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
45114 if( n==(SIZE_T)-1 ){
45136 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
45137 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
45140 if( !pWinMemData->hHeap ){
45148 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
45150 if( !pWinMemData->hHeap ){
45157 pWinMemData->bOwned = TRUE;
45158 assert( pWinMemData->bOwned );
45161 pWinMemData->hHeap = osGetProcessHeap();
45162 if( !pWinMemData->hHeap ){
45167 pWinMemData->bOwned = FALSE;
45168 assert( !pWinMemData->bOwned );
45170 assert( pWinMemData->hHeap!=0 );
45171 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
45173 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
45185 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
45186 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
45188 if( pWinMemData->hHeap ){
45189 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
45191 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
45193 if( pWinMemData->bOwned ){
45194 if( !osHeapDestroy(pWinMemData->hHeap) ){
45196 osGetLastError(), (void*)pWinMemData->hHeap);
45198 pWinMemData->bOwned = FALSE;
45200 pWinMemData->hHeap = NULL;
45205 ** Populate the low-level memory allocation function pointers in
45232 ** Convert a UTF-8 string to Microsoft Unicode.
45240 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
45248 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
45258 ** Convert a Microsoft Unicode string to UTF-8.
45266 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
45274 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
45294 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
45303 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
45313 ** Convert a Microsoft Unicode string to a multi-byte character string,
45323 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
45331 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
45341 ** Convert a multi-byte character string to UTF-8.
45359 ** Convert a UTF-8 string to a multi-byte character string.
45474 ** it accepts a UTF-8 string.
45511 ** it accepts a UTF-16 string.
45544 ** is zero if the error message fits in the buffer, or non-zero
45624 ** This function - winLogErrorAtLine() - is only ever called via the macro
45629 ** error code and, if possible, the human-readable equivalent from
45635 ** failed and the associated file-system path, if any.
45655 "os_win.c:%d: (%lu) %s(%s) - %s",
45664 ** will be retried following a locking error - probably caused by
45681 ** non-zero if the error code is transient in nature and the operation
45769 y.tm_year = pTm.wYear - 1900;
45770 y.tm_mon = pTm.wMonth - 1;
45784 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
45818 memset(&pFile->local, 0, sizeof(pFile->local));
45828 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
45829 if (!pFile->hMutex){
45830 pFile->lastErrno = osGetLastError();
45832 return winLogError(SQLITE_IOERR, pFile->lastErrno,
45837 winceMutexAcquire(pFile->hMutex);
45840 ** case-sensitive, take advantage of that by uppercasing the mutex name
45844 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
45849 ** must be zero-initialized */
45858 if( pFile->hShared ){
45859 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
45862 if( !pFile->shared ){
45863 pFile->lastErrno = osGetLastError();
45864 winLogError(SQLITE_IOERR, pFile->lastErrno,
45867 osCloseHandle(pFile->hShared);
45868 pFile->hShared = NULL;
45873 if( pFile->hShared==NULL ){
45875 pFile->lastErrno = lastErrno;
45876 winLogError(SQLITE_IOERR, pFile->lastErrno,
45880 winceMutexRelease(pFile->hMutex);
45881 osCloseHandle(pFile->hMutex);
45882 pFile->hMutex = NULL;
45888 memset(pFile->shared, 0, sizeof(winceLock));
45891 winceMutexRelease(pFile->hMutex);
45899 if (pFile->hMutex){
45901 winceMutexAcquire(pFile->hMutex);
45905 if (pFile->local.nReaders){
45906 pFile->shared->nReaders --;
45908 if (pFile->local.bReserved){
45909 pFile->shared->bReserved = FALSE;
45911 if (pFile->local.bPending){
45912 pFile->shared->bPending = FALSE;
45914 if (pFile->local.bExclusive){
45915 pFile->shared->bExclusive = FALSE;
45918 /* De-reference and close our copy of the shared memory handle */
45919 osUnmapViewOfFile(pFile->shared);
45920 osCloseHandle(pFile->hShared);
45923 winceMutexRelease(pFile->hMutex);
45924 osCloseHandle(pFile->hMutex);
45925 pFile->hMutex = NULL;
45945 if (!pFile->hMutex) return TRUE;
45946 winceMutexAcquire(pFile->hMutex);
45951 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
45952 pFile->shared->bExclusive = TRUE;
45953 pFile->local.bExclusive = TRUE;
45958 /* Want a read-only lock? */
45961 if (pFile->shared->bExclusive == 0){
45962 pFile->local.nReaders ++;
45963 if (pFile->local.nReaders == 1){
45964 pFile->shared->nReaders ++;
45974 if (pFile->shared->bPending == 0) {
45975 pFile->shared->bPending = TRUE;
45976 pFile->local.bPending = TRUE;
45984 if (pFile->shared->bReserved == 0) {
45985 pFile->shared->bReserved = TRUE;
45986 pFile->local.bReserved = TRUE;
45991 winceMutexRelease(pFile->hMutex);
46011 if (!pFile->hMutex) return TRUE;
46012 winceMutexAcquire(pFile->hMutex);
46017 if (pFile->local.bExclusive){
46019 pFile->local.bExclusive = FALSE;
46020 pFile->shared->bExclusive = FALSE;
46025 else if (pFile->local.nReaders){
46028 pFile->local.nReaders --;
46029 if (pFile->local.nReaders == 0)
46031 pFile->shared->nReaders --;
46040 if (pFile->local.bPending){
46041 pFile->local.bPending = FALSE;
46042 pFile->shared->bPending = FALSE;
46049 if (pFile->local.bReserved) {
46050 pFile->local.bReserved = FALSE;
46051 pFile->shared->bReserved = FALSE;
46056 winceMutexRelease(pFile->hMutex);
46136 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
46142 ** Otherwise, set pFile->lastErrno and return non-zero.
46151 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
46157 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
46163 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
46167 pFile->lastErrno = lastErrno;
46168 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
46169 "winSeekFile", pFile->zPath);
46170 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46174 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46185 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
46188 pFile->lastErrno = osGetLastError();
46189 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
46190 "winSeekFile", pFile->zPath);
46191 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46195 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46223 assert( pFile->pShm==0 );
46225 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
46227 osGetCurrentProcessId(), pFile, pFile->h));
46234 rc = osCloseHandle(pFile->h);
46240 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
46241 if( pAppData==NULL || !pAppData->bNoLock ){
46245 if( pFile->zDeleteOnClose ){
46248 osDeleteFileW(pFile->zDeleteOnClose)==0
46249 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
46254 sqlite3_free(pFile->zDeleteOnClose);
46258 pFile->h = NULL;
46260 OpenCounter(-1);
46262 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
46265 "winClose", pFile->zPath);
46292 pFile->h, pBuf, amt, offset, pFile->locktype));
46297 if( offset<pFile->mmapSize ){
46298 if( offset+amt <= pFile->mmapSize ){
46299 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
46300 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46301 osGetCurrentProcessId(), pFile, pFile->h));
46304 int nCopy = (int)(pFile->mmapSize - offset);
46305 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
46307 amt -= nCopy;
46316 osGetCurrentProcessId(), pFile, pFile->h));
46319 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
46324 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
46329 pFile->lastErrno = lastErrno;
46331 osGetCurrentProcessId(), pFile, pFile->h));
46332 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
46333 "winRead", pFile->zPath);
46337 /* Unread parts of the buffer must be zero-filled */
46338 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
46340 osGetCurrentProcessId(), pFile, pFile->h));
46345 osGetCurrentProcessId(), pFile, pFile->h));
46370 pFile->h, pBuf, amt, offset, pFile->locktype));
46375 if( offset<pFile->mmapSize ){
46376 if( offset+amt <= pFile->mmapSize ){
46377 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
46378 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46379 osGetCurrentProcessId(), pFile, pFile->h));
46382 int nCopy = (int)(pFile->mmapSize - offset);
46383 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
46385 amt -= nCopy;
46413 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
46415 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
46431 nRem -= nWrite;
46434 pFile->lastErrno = lastErrno;
46440 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
46441 || ( pFile->lastErrno==ERROR_DISK_FULL )){
46443 osGetCurrentProcessId(), pFile, pFile->h));
46444 return winLogError(SQLITE_FULL, pFile->lastErrno,
46445 "winWrite1", pFile->zPath);
46448 osGetCurrentProcessId(), pFile, pFile->h));
46449 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
46450 "winWrite2", pFile->zPath);
46455 osGetCurrentProcessId(), pFile, pFile->h));
46468 if( pFile->nFetchOut>0 ){
46469 /* File truncation is a no-op if there are outstanding memory mapped
46475 ** No real harm comes of this - the database file is not corrupted,
46479 ** The only feasible work-around is to defer the truncation until after
46480 ** all references to memory-mapped content are closed. That is doable,
46483 ** now to simply make trancations a no-op if there are pending reads. We
46493 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
46495 /* If the user has configured a chunk-size for this file, truncate the
46500 if( pFile->szChunk>0 ){
46501 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
46505 if( pFile->pMapRegion ){
46506 oldMmapSize = pFile->mmapSize;
46513 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
46515 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
46516 "winTruncate1", pFile->zPath);
46517 }else if( 0==osSetEndOfFile(pFile->h) &&
46519 pFile->lastErrno = lastErrno;
46520 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
46521 "winTruncate2", pFile->zPath);
46527 winMapfile(pFile, -1);
46535 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
46581 osGetCurrentProcessId(), pFile, pFile->h, flags,
46582 pFile->locktype));
46594 ** no-op
46597 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46598 osGetCurrentProcessId(), pFile, pFile->h));
46602 if( pFile->pMapRegion ){
46603 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
46604 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46606 pFile, pFile->pMapRegion));
46608 pFile->lastErrno = osGetLastError();
46609 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46611 pFile, pFile->pMapRegion));
46612 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46613 "winSync1", pFile->zPath);
46617 rc = osFlushFileBuffers(pFile->h);
46621 osGetCurrentProcessId(), pFile, pFile->h));
46624 pFile->lastErrno = osGetLastError();
46626 osGetCurrentProcessId(), pFile, pFile->h));
46627 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
46628 "winSync2", pFile->zPath);
46643 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
46648 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
46652 pFile->lastErrno = osGetLastError();
46653 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
46654 "winFileSize", pFile->zPath);
46663 lowerBits = osGetFileSize(pFile->h, &upperBits);
46667 pFile->lastErrno = lastErrno;
46668 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
46669 "winFileSize", pFile->zPath);
46674 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
46716 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46723 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
46725 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
46733 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
46734 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46735 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
46739 pFile->lastErrno = osGetLastError();
46742 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
46752 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46754 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
46758 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
46762 pFile->lastErrno = lastErrno;
46763 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
46764 "winUnlockReadLock", pFile->zPath);
46766 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
46771 ** Lock the file with the lock specified by parameter locktype - one
46785 ** UNLOCKED -> SHARED
46786 ** SHARED -> RESERVED
46787 ** SHARED -> (PENDING) -> EXCLUSIVE
46788 ** RESERVED -> (PENDING) -> EXCLUSIVE
46789 ** PENDING -> EXCLUSIVE
46799 int newLocktype; /* Set pFile->locktype to this value before exiting */
46806 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46812 if( pFile->locktype>=locktype ){
46813 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
46817 /* Do not allow any kind of write-lock on a read-only database
46819 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
46825 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
46827 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
46833 newLocktype = pFile->locktype;
46834 if( pFile->locktype==NO_LOCK
46835 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
46838 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46841 ** around problems caused by indexing and/or anti-virus software on
46847 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
46848 pFile->h, cnt, res));
46850 pFile->lastErrno = lastErrno;
46852 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
46853 pFile->h, cnt, sqlite3ErrName(rc)));
46867 assert( pFile->locktype==NO_LOCK );
46879 assert( pFile->locktype==SHARED_LOCK );
46880 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
46898 assert( pFile->locktype>=SHARED_LOCK );
46900 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
46914 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
46923 pFile->lastErrno = lastErrno;
46925 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
46926 pFile->h, locktype, newLocktype));
46928 pFile->locktype = (u8)newLocktype;
46930 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
46937 ** non-zero, otherwise zero.
46944 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
46947 if( pFile->locktype>=RESERVED_LOCK ){
46949 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
46951 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
46953 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
46956 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
46959 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
46960 pFile->h, pResOut, *pResOut));
46969 ** the requested locking level, this routine is a no-op.
46982 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46983 type = pFile->locktype;
46985 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
46990 "winUnlock", pFile->zPath);
46994 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
47000 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
47002 pFile->locktype = (u8)locktype;
47004 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
47009 ****************************** No-op Locking **********************************
47015 ** This locking mode is appropriate for use on read-only databases
47016 ** (ex: databases that are burned into CD-ROM, for example.) It can
47043 /******************* End of the no-op lock implementation *********************
47048 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
47050 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
47054 *pArg = (pFile->ctrlFlags & mask)!=0;
47056 pFile->ctrlFlags &= ~mask;
47058 pFile->ctrlFlags |= mask;
47073 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
47076 *(int*)pArg = pFile->locktype;
47077 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47081 *(int*)pArg = (int)pFile->lastErrno;
47082 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47086 pFile->szChunk = *(int *)pArg;
47087 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47091 if( pFile->szChunk>0 ){
47102 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47105 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47110 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47115 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47119 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
47120 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47135 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47140 *phFile = pFile->h;
47141 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47147 HANDLE hOldFile = pFile->h;
47148 pFile->h = *phFile;
47151 hOldFile, pFile->h));
47157 int rc = winGetTempname(pFile->pVfs, &zTFile);
47161 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47174 ** least a 64-bit type. */
47179 *(i64*)pArg = pFile->mmapSizeMax;
47180 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
47181 pFile->mmapSizeMax = newLimit;
47182 if( pFile->mmapSize>0 ){
47184 rc = winMapfile(pFile, -1);
47187 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47192 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
47202 ** if two files are created in the same file-system directory (i.e.
47217 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
47259 ** log-summary, each thread has its own winFile object, but they all
47261 ** log-summary is opened only once per process.
47269 ** The following fields are read-only after the object is created:
47284 int szRegion; /* Size of shared-memory regions */
47286 u8 isReadonly; /* True if read-only */
47315 ** are read-only thereafter:
47320 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
47347 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
47355 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
47357 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
47358 pFile->hFile.h, lockType, ofst, nByte));
47360 /* Release/Acquire the system-level lock */
47362 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
47367 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
47373 pFile->lastErrno = osGetLastError();
47377 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
47378 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
47379 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
47391 ** This is not a VFS shared-memory method; it is a utility function called
47392 ** by VFS shared-memory methods.
47398 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
47402 if( p->nRef==0 ){
47404 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
47405 for(i=0; i<p->nRegion; i++){
47406 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
47407 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
47410 bRc = osCloseHandle(p->aRegion[i].hMap);
47411 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
47415 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
47417 winClose((sqlite3_file *)&p->hFile);
47423 winDelete(pVfs, p->zFilename, 0);
47427 *pp = p->pNext;
47428 sqlite3_free(p->aRegion);
47431 pp = &p->pNext;
47443 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
47449 if( pShmNode->isReadonly ){
47450 pShmNode->isUnlocked = 1;
47453 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
47456 "winLockSharedMemory", pShmNode->zFilename);
47468 ** Open the shared-memory area associated with database file pDbFd.
47470 ** When opening a new shared-memory file, if no other instances of that
47481 assert( pDbFd->pShm==0 ); /* Not previously opened */
47488 nName = sqlite3Strlen30(pDbFd->zPath);
47494 pNew->zFilename = (char*)&pNew[1];
47495 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
47496 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
47502 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
47506 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
47516 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
47517 pShmNode->pNext = winShmNodeList;
47521 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
47522 if( pShmNode->mutex==0 ){
47528 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
47533 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
47534 (sqlite3_file*)&pShmNode->hFile,
47538 pShmNode->zFilename);
47541 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
47548 p->pShmNode = pShmNode;
47550 p->id = pShmNode->nextShmId++;
47552 pShmNode->nRef++;
47553 pDbFd->pShm = p;
47560 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
47563 sqlite3_mutex_enter(pShmNode->mutex);
47564 p->pNext = pShmNode->pFirst;
47565 pShmNode->pFirst = p;
47566 sqlite3_mutex_leave(pShmNode->mutex);
47572 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
47580 ** Close a connection to shared-memory. Delete the underlying
47587 winFile *pDbFd; /* Database holding shared-memory */
47589 winShmNode *pShmNode; /* The underlying shared-memory file */
47593 p = pDbFd->pShm;
47595 pShmNode = p->pShmNode;
47599 sqlite3_mutex_enter(pShmNode->mutex);
47600 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
47601 *pp = p->pNext;
47605 pDbFd->pShm = 0;
47606 sqlite3_mutex_leave(pShmNode->mutex);
47608 /* If pShmNode->nRef has reached 0, then close the underlying
47609 ** shared-memory file, too */
47611 assert( pShmNode->nRef>0 );
47612 pShmNode->nRef--;
47613 if( pShmNode->nRef==0 ){
47614 winShmPurge(pDbFd->pVfs, deleteFlag);
47622 ** Change the lock state for a shared-memory segment.
47631 winShm *p = pDbFd->pShm; /* The shared memory being locked */
47638 pShmNode = p->pShmNode;
47649 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
47651 sqlite3_mutex_enter(pShmNode->mutex);
47656 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47658 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
47659 allMask |= pX->sharedMask;
47662 /* Unlock the system-level locks */
47671 p->exclMask &= ~mask;
47672 p->sharedMask &= ~mask;
47681 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47682 if( (pX->exclMask & mask)!=0 ){
47686 allShared |= pX->sharedMask;
47700 p->sharedMask |= mask;
47706 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47707 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
47719 assert( (p->sharedMask & mask)==0 );
47720 p->exclMask |= mask;
47724 sqlite3_mutex_leave(pShmNode->mutex);
47725 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
47726 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
47741 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
47748 ** shared-memory associated with the database file fd. Shared-memory regions
47749 ** are numbered starting from zero. Each shared-memory region is szRegion
47754 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
47757 ** isWrite is non-zero and the requested shared-memory region has not yet
47760 ** If the shared-memory region has already been allocated or is allocated by
47773 winShm *pShm = pDbFd->pShm;
47782 pShm = pDbFd->pShm;
47785 pShmNode = pShm->pShmNode;
47787 sqlite3_mutex_enter(pShmNode->mutex);
47788 if( pShmNode->isUnlocked ){
47791 pShmNode->isUnlocked = 0;
47793 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
47795 if( pShmNode->nRegion<=iRegion ){
47798 sqlite3_int64 sz; /* Current size of wal-index file */
47800 pShmNode->szRegion = szRegion;
47803 ** Check to see if it has been allocated (i.e. if the wal-index file is
47806 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
47809 "winShmMap1", pDbFd->zPath);
47817 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
47821 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
47824 "winShmMap2", pDbFd->zPath);
47831 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
47837 pShmNode->aRegion = apNew;
47839 if( pShmNode->isReadonly ){
47844 while( pShmNode->nRegion<=iRegion ){
47845 HANDLE hMap = NULL; /* file-mapping handle */
47849 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
47853 hMap = osCreateFileMappingW(pShmNode->hFile.h,
47857 hMap = osCreateFileMappingA(pShmNode->hFile.h,
47861 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
47862 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
47865 int iOffset = pShmNode->nRegion*szRegion;
47869 iOffset - iOffsetShift, szRegion + iOffsetShift
47873 0, iOffset - iOffsetShift, szRegion + iOffsetShift
47876 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
47877 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
47881 pShmNode->lastErrno = osGetLastError();
47882 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
47883 "winShmMap3", pDbFd->zPath);
47888 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
47889 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
47890 pShmNode->nRegion++;
47895 if( pShmNode->nRegion>iRegion ){
47898 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
47903 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
47904 sqlite3_mutex_leave(pShmNode->mutex);
47921 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
47923 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
47924 pFile->mmapSize, pFile->mmapSizeMax));
47925 if( pFile->pMapRegion ){
47926 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
47927 pFile->lastErrno = osGetLastError();
47928 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
47930 pFile->pMapRegion));
47931 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
47932 "winUnmapfile1", pFile->zPath);
47934 pFile->pMapRegion = 0;
47935 pFile->mmapSize = 0;
47937 if( pFile->hMap!=NULL ){
47938 if( !osCloseHandle(pFile->hMap) ){
47939 pFile->lastErrno = osGetLastError();
47940 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
47941 osGetCurrentProcessId(), pFile, pFile->hMap));
47942 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
47943 "winUnmapfile2", pFile->zPath);
47945 pFile->hMap = NULL;
47947 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
47953 ** Memory map or remap the file opened by file-descriptor pFd (if the file
47956 ** outstanding xFetch() references to it, this function is a no-op.
47958 ** If parameter nByte is non-negative, then it is the requested size of
47972 assert( nMap>=0 || pFd->nFetchOut==0 );
47973 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
47976 if( pFd->nFetchOut>0 ) return SQLITE_OK;
47981 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
47986 if( nMap>pFd->mmapSizeMax ){
47987 nMap = pFd->mmapSizeMax;
47989 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
47991 if( nMap==0 && pFd->mmapSize>0 ){
47994 if( nMap!=pFd->mmapSize ){
48001 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
48007 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
48009 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
48013 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
48017 if( pFd->hMap==NULL ){
48018 pFd->lastErrno = osGetLastError();
48019 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
48020 "winMapfile1", pFd->zPath);
48022 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
48029 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
48031 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
48034 osCloseHandle(pFd->hMap);
48035 pFd->hMap = NULL;
48036 pFd->lastErrno = osGetLastError();
48037 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
48038 "winMapfile2", pFd->zPath);
48040 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
48044 pFd->pMapRegion = pNew;
48045 pFd->mmapSize = nMap;
48048 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
48076 if( pFd->mmapSizeMax>0 ){
48077 if( pFd->pMapRegion==0 ){
48078 int rc = winMapfile(pFd, -1);
48085 if( pFd->mmapSize >= iOff+nAmt ){
48086 assert( pFd->pMapRegion!=0 );
48087 *pp = &((u8 *)pFd->pMapRegion)[iOff];
48088 pFd->nFetchOut++;
48099 ** If the third argument is non-NULL, then this function releases a
48115 assert( (p==0)==(pFd->nFetchOut==0) );
48118 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
48124 pFd->nFetchOut--;
48128 ** is unnecessary can be omitted - potentially improving
48133 assert( pFd->nFetchOut>=0 );
48221 ** supports for filenames into UTF-8. Space to hold the result is
48240 ** Convert a UTF-8 filename into whatever form the underlying
48260 ** This function returns non-zero if the specified UTF-8 string buffer
48268 if( winIsDirSep(zBuf[nLen-1]) ){
48294 /* It's odd to simulate an io-error here, but really this is just
48295 ** using the io-error infrastructure to test that SQLite handles this
48303 nMax = pVfs->mxPathname; nBuf = nMax + 2;
48306 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48314 nDir = nMax - (nPre + 15);
48319 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48324 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48364 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48377 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48385 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
48392 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
48398 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48416 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48422 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48434 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48444 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48449 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48459 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48473 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48491 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48495 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
48500 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
48506 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
48552 const char *zName, /* Name of the file (UTF-8) */
48569 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
48611 /* The main DB, main journal, WAL file and super-journal are never
48618 /* Assert that the upper layer has set one of the "file-type" flags. */
48627 pFile->h = INVALID_HANDLE_VALUE;
48649 /* Database filenames are double-zero terminated if they are not
48795 pFile->lastErrno = lastErrno;
48796 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
48813 pAppData = (winVfsAppData*)pVfs->pAppData;
48818 && ((pAppData==NULL) || !pAppData->bNoLock)
48824 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
48829 pFile->zDeleteOnClose = zConverted;
48837 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
48838 pFile->pVfs = pVfs;
48839 pFile->h = h;
48841 pFile->ctrlFlags |= WINFILE_RDONLY;
48846 pFile->ctrlFlags |= WINFILE_PSOW;
48848 pFile->lastErrno = NO_ERROR;
48849 pFile->zPath = zName;
48851 pFile->hMap = NULL;
48852 pFile->pMapRegion = 0;
48853 pFile->mmapSize = 0;
48854 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
49010 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
49056 ** Returns non-zero if the specified path name starts with the "long path"
49067 ** Returns non-zero if the specified path name starts with a drive letter
49077 ** Returns non-zero if the specified path name should be used verbatim. If
49078 ** non-zero is returned from this function, the calling function must simply
49079 ** use the provided path name verbatim -OR- resolve it into a full path name
49115 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49141 assert( nFull>=pVfs->mxPathname );
49149 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49155 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
49165 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49171 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49177 zRelative, zOut, pVfs->mxPathname+1)<0 ){
49187 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
49206 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49209 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
49215 /* It's odd to simulate an io-error here, but really this is just
49216 ** using the io-error infrastructure to test that SQLite handles this
49228 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49289 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
49306 int nFull = pVfs->mxPathname+1;
49381 for(j=0, k=p->i; j<sz; j++){
49382 p->a[k++] ^= x[j];
49383 if( k>=p->na ) k = 0;
49385 p->i = k;
49386 p->nXor += sz;
49457 ** The following variable, if set to a non-zero value, is interpreted as
49476 /* FILETIME structure is a 64-bit value representing the number of
49477 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
49484 /* 2^32 - to avoid use of LL and warnings in gcc */
49534 ** buffer with a nul-terminated UTF-8 encoded error message
49540 ** is zero if the error message fits in the buffer, or non-zero
49541 ** otherwise (if the message was truncated). If non-zero is returned,
49542 ** then it is not necessary to include the nul-terminator character
49599 "win32-longpath", /* zName */
49624 "win32-none", /* zName */
49649 "win32-longpath-none", /* zName */
49670 /* Double-check that the aSyscall[] array has been constructed
49723 ** 2016-09-07
49734 ** This file implements an in-memory VFS. A database is held as a contiguous
49750 /* Access to a lower-level VFS that (might) implement dynamic loading,
49753 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
49813 ** File-scope variables for holding the memdb files that are accessible
49891 memdbLock, /* xUnlock - same as xLock in this case */
49916 sqlite3_mutex_enter(p->pMutex);
49919 sqlite3_mutex_leave(p->pMutex);
49926 ** Close an memdb-file.
49931 MemStore *p = ((MemFile*)pFile)->pStore;
49932 if( p->zFName ){
49941 if( p->nRef==1 ){
49942 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
49955 p->nRef--;
49956 if( p->nRef<=0 ){
49957 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
49958 sqlite3_free(p->aData);
49961 sqlite3_mutex_free(p->pMutex);
49970 ** Read data from an memdb-file.
49978 MemStore *p = ((MemFile*)pFile)->pStore;
49980 if( iOfst+iAmt>p->sz ){
49982 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
49986 memcpy(zBuf, p->aData+iOfst, iAmt);
49996 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
49999 if( newSz>p->szMax ){
50003 if( newSz>p->szMax ) newSz = p->szMax;
50004 pNew = sqlite3Realloc(p->aData, newSz);
50006 p->aData = pNew;
50007 p->szAlloc = newSz;
50012 ** Write data to an memdb-file.
50020 MemStore *p = ((MemFile*)pFile)->pStore;
50022 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
50028 if( iOfst+iAmt>p->sz ){
50030 if( iOfst+iAmt>p->szAlloc
50036 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
50037 p->sz = iOfst+iAmt;
50039 memcpy(p->aData+iOfst, z, iAmt);
50045 ** Truncate an memdb-file.
50052 MemStore *p = ((MemFile*)pFile)->pStore;
50055 if( size>p->sz ){
50059 p->sz = size;
50066 ** Sync an memdb-file.
50075 ** Return the current file-size of an memdb-file.
50078 MemStore *p = ((MemFile*)pFile)->pStore;
50080 *pSize = p->sz;
50086 ** Lock an memdb-file.
50090 MemStore *p = pThis->pStore;
50092 if( eLock==pThis->eLock ) return SQLITE_OK;
50095 if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){
50097 }else if( pThis->eLock<=SQLITE_LOCK_SHARED ){
50098 if( p->nWrLock ){
50101 p->nWrLock = 1;
50105 if( pThis->eLock > SQLITE_LOCK_SHARED ){
50106 assert( p->nWrLock==1 );
50107 p->nWrLock = 0;
50108 }else if( p->nWrLock ){
50111 p->nRdLock++;
50115 if( pThis->eLock>SQLITE_LOCK_SHARED ){
50116 assert( p->nWrLock==1 );
50117 p->nWrLock = 0;
50119 assert( p->nRdLock>0 );
50120 p->nRdLock--;
50122 if( rc==SQLITE_OK ) pThis->eLock = eLock;
50130 ** occur on an in-memory database.
50140 ** File control method. For custom operations on an memdb-file.
50143 MemStore *p = ((MemFile*)pFile)->pStore;
50147 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
50152 if( iLimit<p->sz ){
50154 iLimit = p->szMax;
50156 iLimit = p->sz;
50159 p->szMax = iLimit;
50169 ** Return the sector-size in bytes for an memdb-file.
50177 ** Return the device characteristic flags supported by an memdb-file.
50187 /* Fetch a page of a memory-mapped file */
50194 MemStore *p = ((MemFile*)pFile)->pStore;
50196 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
50199 p->nMmap++;
50200 *pp = (void*)(p->aData + iOfst);
50206 /* Release a memory-mapped page */
50208 MemStore *p = ((MemFile*)pFile)->pStore;
50212 p->nMmap--;
50241 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
50263 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
50264 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
50265 p->zFName = (char*)&p[1];
50266 memcpy(p->zFName, zName, szName+1);
50267 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50268 if( p->pMutex==0 ){
50269 memdb_g.nMemStore--;
50274 p->nRef = 1;
50278 p->nRef++;
50287 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
50288 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
50290 pFile->pStore = p;
50294 pFd->pMethods = &memdb_io_methods;
50299 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
50303 ** ensure the file-system modifications are synced to disk before
50350 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
50355 ** utf-8 string describing the most recent error encountered associated
50359 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
50366 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
50373 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
50381 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
50389 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
50397 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
50402 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
50405 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
50417 if( p->base.pMethods!=&memdb_io_methods ) return 0;
50418 pStore = p->pStore;
50420 if( pStore->zFName!=0 ) p = 0;
50451 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50454 if( piSize ) *piSize = -1;
50457 MemStore *pStore = p->pStore;
50458 assert( pStore->pMutex==0 );
50459 if( piSize ) *piSize = pStore->sz;
50461 pOut = pStore->aData;
50463 pOut = sqlite3_malloc64( pStore->sz );
50464 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
50468 pBt = db->aDb[iDb].pBt;
50472 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
50491 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
50531 sqlite3_mutex_enter(db->mutex);
50532 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50543 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
50547 db->init.iDb = (u8)iDb;
50548 db->init.reopenMemdb = 1;
50550 db->init.reopenMemdb = 0;
50559 MemStore *pStore = p->pStore;
50560 pStore->aData = pData;
50562 pStore->sz = szDb;
50563 pStore->szAlloc = szBuf;
50564 pStore->szMax = szBuf;
50565 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
50566 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
50568 pStore->mFlags = mFlags;
50577 sqlite3_mutex_leave(db->mutex);
50589 sz = pLower->szOsFile;
50614 ** This file implements an object that represents a fixed-length
50618 ** journalled during a transaction, or which pages have the "dont-write"
50647 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
50664 ** sub-dividing and re-hashing. */
50669 ** no fewer collisions than the no-op *1. */
50689 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
50698 u32 nSet; /* Number of bits that are set - only valid for aHash
50722 p->iSize = iSize;
50728 ** Check to see if the i-th bit is set. Return true or false.
50734 i--;
50735 if( i>=p->iSize ) return 0;
50736 while( p->iDivisor ){
50737 u32 bin = i/p->iDivisor;
50738 i = i%p->iDivisor;
50739 p = p->u.apSub[bin];
50744 if( p->iSize<=BITVEC_NBIT ){
50745 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
50748 while( p->u.aHash[h] ){
50749 if( p->u.aHash[h]==i ) return 1;
50760 ** Set the i-th bit. Return 0 on success and an error code if
50763 ** This routine might cause sub-bitmaps to be allocated. Failing
50764 ** to get the memory needed to hold the sub-bitmap is the only
50775 assert( i<=p->iSize );
50776 i--;
50777 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
50778 u32 bin = i/p->iDivisor;
50779 i = i%p->iDivisor;
50780 if( p->u.apSub[bin]==0 ){
50781 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
50782 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
50784 p = p->u.apSub[bin];
50786 if( p->iSize<=BITVEC_NBIT ){
50787 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
50793 /* worring about sub-dividing and re-hashing. */
50794 if( !p->u.aHash[h] ){
50795 if (p->nSet<(BITVEC_NINT-1)) {
50804 if( p->u.aHash[h]==i ) return SQLITE_OK;
50807 } while( p->u.aHash[h] );
50812 if( p->nSet>=BITVEC_MXHASH ){
50815 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
50819 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50820 memset(p->u.apSub, 0, sizeof(p->u.apSub));
50821 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
50831 p->nSet++;
50832 p->u.aHash[h] = i;
50837 ** Clear the i-th bit.
50845 i--;
50846 while( p->iDivisor ){
50847 u32 bin = i/p->iDivisor;
50848 i = i%p->iDivisor;
50849 p = p->u.apSub[bin];
50854 if( p->iSize<=BITVEC_NBIT ){
50855 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
50859 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50860 memset(p->u.aHash, 0, sizeof(p->u.aHash));
50861 p->nSet = 0;
50864 u32 h = BITVEC_HASH(aiValues[j]-1);
50865 p->nSet++;
50866 while( p->u.aHash[h] ){
50870 p->u.aHash[h] = aiValues[j];
50881 if( p->iDivisor ){
50884 sqlite3BitvecDestroy(p->u.apSub[i]);
50895 return p->iSize;
50937 ** If a memory allocation error occurs, return -1.
50942 int rc = -1;
50965 i = aOp[pc+2] - 1;
50977 if( (--aOp[pc+1]) > 0 ) nx = 0;
50993 ** match (rc==0). Change rc to non-zero if a discrepancy
50998 + (sqlite3BitvecSize(pBitvec) - sz);
51044 ** such that p was added to the list more recently than p->pDirtyNext.
51049 ** page to eject from the cache mid-transaction. It is better to eject
51093 if( pCache->pCache==0 ) return;
51097 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
51099 pPg = (PgHdr*)pLower->pExtra;
51100 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51101 a = (unsigned char *)pLower->pBuf;
51104 if( pPg->pPage==0 ){
51105 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
51127 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
51128 pCache = pPg->pCache;
51130 if( pPg->flags & PGHDR_CLEAN ){
51131 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
51132 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
51133 assert( pCache->pDirtyTail!=pPg );
51136 if( pPg->flags & PGHDR_WRITEABLE ){
51137 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
51175 PCache *p = pPage->pCache;
51179 pPage->pgno));
51181 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
51182 assert( pPage->pDirtyPrev || pPage==p->pDirty );
51185 if( p->pSynced==pPage ){
51186 p->pSynced = pPage->pDirtyPrev;
51189 if( pPage->pDirtyNext ){
51190 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
51192 assert( pPage==p->pDirtyTail );
51193 p->pDirtyTail = pPage->pDirtyPrev;
51195 if( pPage->pDirtyPrev ){
51196 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
51202 assert( pPage==p->pDirty );
51203 p->pDirty = pPage->pDirtyNext;
51204 assert( p->bPurgeable || p->eCreate==2 );
51205 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
51206 assert( p->bPurgeable==0 || p->eCreate==1 );
51207 p->eCreate = 2;
51212 pPage->pDirtyPrev = 0;
51213 pPage->pDirtyNext = p->pDirty;
51214 if( pPage->pDirtyNext ){
51215 assert( pPage->pDirtyNext->pDirtyPrev==0 );
51216 pPage->pDirtyNext->pDirtyPrev = pPage;
51218 p->pDirtyTail = pPage;
51219 if( p->bPurgeable ){
51220 assert( p->eCreate==2 );
51221 p->eCreate = 1;
51224 p->pDirty = pPage;
51230 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
51231 if( !p->pSynced
51232 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
51234 p->pSynced = pPage;
51242 ** being used for an in-memory database, this function is a no-op.
51245 if( p->pCache->bPurgeable ){
51246 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
51247 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
51248 pcacheDump(p->pCache);
51253 ** Compute the number of pages of cache requested. p->szCache is the
51257 if( p->szCache>=0 ){
51258 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
51260 return p->szCache;
51263 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
51267 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
51280 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
51281 ** built-in default page cache is used instead of the application defined
51290 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
51321 p->szPage = 1;
51322 p->szExtra = szExtra;
51324 p->bPurgeable = bPurgeable;
51325 p->eCreate = 2;
51326 p->xStress = xStress;
51327 p->pStress = pStress;
51328 p->szCache = 100;
51329 p->szSpill = 1;
51339 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
51340 if( pCache->szPage ){
51343 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
51344 pCache->bPurgeable
51348 if( pCache->pCache ){
51349 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
51351 pCache->pCache = pNew;
51352 pCache->szPage = szPage;
51391 assert( pCache->pCache!=0 );
51393 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
51402 eCreate = createFlag & pCache->eCreate;
51404 assert( createFlag==0 || pCache->eCreate==eCreate );
51405 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
51406 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51429 if( pCache->eCreate==2 ) return 0;
51431 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
51432 /* Find a dirty page to write-out and recycle. First try to find a
51433 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
51441 for(pPg=pCache->pSynced;
51442 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
51443 pPg=pPg->pDirtyPrev
51445 pCache->pSynced = pPg;
51447 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
51453 "spill page %d making room for %d - cache used: %d/%d",
51454 pPg->pgno, pgno,
51455 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
51458 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
51459 rc = pCache->xStress(pCache->pStress, pPg);
51466 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
51486 pPgHdr = (PgHdr*)pPage->pExtra;
51487 assert( pPgHdr->pPage==0 );
51488 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
51489 pPgHdr->pPage = pPage;
51490 pPgHdr->pData = pPage->pBuf;
51491 pPgHdr->pExtra = (void *)&pPgHdr[1];
51492 memset(pPgHdr->pExtra, 0, 8);
51493 pPgHdr->pCache = pCache;
51494 pPgHdr->pgno = pgno;
51495 pPgHdr->flags = PGHDR_CLEAN;
51513 pPgHdr = (PgHdr *)pPage->pExtra;
51515 if( !pPgHdr->pPage ){
51518 pCache->nRefSum++;
51519 pPgHdr->nRef++;
51529 assert( p->nRef>0 );
51530 p->pCache->nRefSum--;
51531 if( (--p->nRef)==0 ){
51532 if( p->flags&PGHDR_CLEAN ){
51544 assert(p->nRef>0);
51546 p->nRef++;
51547 p->pCache->nRefSum++;
51556 assert( p->nRef==1 );
51558 if( p->flags&PGHDR_DIRTY ){
51561 p->pCache->nRefSum--;
51562 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
51570 assert( p->nRef>0 );
51572 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
51573 p->flags &= ~PGHDR_DONT_WRITE;
51574 if( p->flags & PGHDR_CLEAN ){
51575 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
51576 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
51577 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
51590 assert( (p->flags & PGHDR_DIRTY)!=0 );
51591 assert( (p->flags & PGHDR_CLEAN)==0 );
51593 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
51594 p->flags |= PGHDR_CLEAN;
51595 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
51597 if( p->nRef==0 ){
51607 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
51608 while( (p = pCache->pDirty)!=0 ){
51618 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
51619 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51620 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
51622 pCache->pSynced = pCache->pDirtyTail;
51630 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51631 p->flags &= ~PGHDR_NEED_SYNC;
51633 pCache->pSynced = pCache->pDirtyTail;
51640 PCache *pCache = p->pCache;
51641 assert( p->nRef>0 );
51644 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51645 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51646 p->pgno = newPgno;
51647 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
51662 if( pCache->pCache ){
51666 for(p=pCache->pDirty; p; p=pNext){
51667 pNext = p->pDirtyNext;
51672 assert( p->pgno>0 );
51673 if( p->pgno>pgno ){
51674 assert( p->flags&PGHDR_DIRTY );
51678 if( pgno==0 && pCache->nRefSum ){
51680 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
51682 ** pCache->nRefSum>0 */
51683 memset(pPage1->pBuf, 0, pCache->szPage);
51687 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
51695 assert( pCache->pCache!=0 );
51697 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
51716 if( pA->pgno<pB->pgno ){
51717 pTail->pDirty = pA;
51719 pA = pA->pDirty;
51721 pTail->pDirty = pB;
51725 pTail->pDirty = pB;
51727 pB = pB->pDirty;
51729 pTail->pDirty = pA;
51754 pIn = p->pDirty;
51755 p->pDirty = 0;
51756 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
51765 if( NEVER(i==N_SORT_BUCKET-1) ){
51785 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51786 p->pDirty = p->pDirtyNext;
51788 return pcacheSortDirtyList(pCache->pDirty);
51798 return pCache->nRefSum;
51805 return p->nRef;
51812 assert( pCache->pCache!=0 );
51813 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
51818 ** Get the suggested cache-size value.
51826 ** Set the suggested cache-size value.
51829 assert( pCache->pCache!=0 );
51830 pCache->szCache = mxPage;
51831 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
51836 ** Set the suggested cache-spill value. Make no changes if if the
51837 ** argument is zero. Return the effective cache-spill size, which will
51842 assert( p->pCache!=0 );
51845 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
51847 p->szSpill = mxPage;
51850 if( res<p->szSpill ) res = p->szSpill;
51858 assert( pCache->pCache!=0 );
51859 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
51864 ** in the page-cache hierarchy.
51876 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
51885 return (pCache->pDirty!=0);
51897 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
51925 ** -------------------------------------------------------------
51927 ** -------------------------------------------------------------
51943 ** size can vary according to architecture, compile-time options, and
51951 ** power of two, and leaving the rounded-up space unused.
51963 ** (1) The general-purpose memory allocator - sqlite3Malloc()
51964 ** (2) Global page-cache memory provided using sqlite3_config() with
51966 ** (3) PCache-local bulk allocation.
51976 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
51980 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
51997 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
52001 ** but causes a 2-byte gap in the structure for most architectures (since
52002 ** pointers must be either 4 or 8-byte aligned). As this structure is located
52004 ** corrupt, code at the b-tree layer may overread the page buffer and
52026 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
52027 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
52055 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
52061 ** open database file (including each in-memory database and each
52076 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
52094 PgHdr1 *pFree; /* List of unused pcache-local pages */
52095 void *pBulk; /* Bulk memory used by pcache-local */
52129 ** reading because (1) most platforms read a 32-bit integer atomically and
52146 # define pcache1EnterMutex(X) assert((X)->mutex==0)
52147 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
52150 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
52151 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
52161 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
52180 while( n-- ){
52182 p->pNext = pcache1.pFree;
52191 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
52192 ** true if pCache->pFree ends up containing one or more free pages.
52199 if( pCache->nMax<3 ) return 0;
52202 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
52204 szBulk = -1024 * (i64)pcache1.nInitPage;
52206 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
52207 szBulk = pCache->szAlloc*(i64)pCache->nMax;
52209 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
52212 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
52214 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
52215 pX->page.pBuf = zBulk;
52216 pX->page.pExtra = &pX[1];
52217 pX->isBulkLocal = 1;
52218 pX->isAnchor = 0;
52219 pX->pNext = pCache->pFree;
52220 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
52221 pCache->pFree = pX;
52222 zBulk += pCache->szAlloc;
52223 }while( --nBulk );
52225 return pCache->pFree!=0;
52244 pcache1.pFree = pcache1.pFree->pNext;
52245 pcache1.nFreeSlot--;
52282 pSlot->pNext = pcache1.pFree;
52329 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52330 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
52331 assert( pCache->pFree!=0 );
52332 p = pCache->pFree;
52333 pCache->pFree = p->pNext;
52334 p->pNext = 0;
52341 assert( pCache->pGroup==&pcache1.grp );
52342 pcache1LeaveMutex(pCache->pGroup);
52346 pPg = pcache1Alloc(pCache->szPage);
52347 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
52354 pPg = pcache1Alloc(pCache->szAlloc);
52358 pcache1EnterMutex(pCache->pGroup);
52362 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
52364 p->page.pBuf = pPg;
52365 p->page.pExtra = &p[1];
52366 p->isBulkLocal = 0;
52367 p->isAnchor = 0;
52368 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
52370 (*pCache->pnPurgeable)++;
52380 pCache = p->pCache;
52381 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
52382 if( p->isBulkLocal ){
52383 p->pNext = pCache->pFree;
52384 pCache->pFree = p;
52386 pcache1Free(p->page.pBuf);
52391 (*pCache->pnPurgeable)--;
52429 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
52450 assert( sqlite3_mutex_held(p->pGroup->mutex) );
52452 nNew = p->nHash*2;
52457 pcache1LeaveMutex(p->pGroup);
52458 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
52460 if( p->nHash ){ sqlite3EndBenignMalloc(); }
52461 pcache1EnterMutex(p->pGroup);
52463 for(i=0; i<p->nHash; i++){
52465 PgHdr1 *pNext = p->apHash[i];
52467 unsigned int h = pPage->iKey % nNew;
52468 pNext = pPage->pNext;
52469 pPage->pNext = apNew[h];
52473 sqlite3_free(p->apHash);
52474 p->apHash = apNew;
52475 p->nHash = nNew;
52482 ** LRU list, then this function is a no-op.
52489 assert( pPage->pLruNext );
52490 assert( pPage->pLruPrev );
52491 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
52492 pPage->pLruPrev->pLruNext = pPage->pLruNext;
52493 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
52494 pPage->pLruNext = 0;
52495 /* pPage->pLruPrev = 0;
52497 assert( pPage->isAnchor==0 );
52498 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
52499 pPage->pCache->nRecyclable--;
52513 PCache1 *pCache = pPage->pCache;
52516 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52517 h = pPage->iKey % pCache->nHash;
52518 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
52519 *pp = (*pp)->pNext;
52521 pCache->nPage--;
52530 PGroup *pGroup = pCache->pGroup;
52532 assert( sqlite3_mutex_held(pGroup->mutex) );
52533 while( pGroup->nPurgeable>pGroup->nMaxPage
52534 && (p=pGroup->lru.pLruPrev)->isAnchor==0
52536 assert( p->pCache->pGroup==pGroup );
52541 if( pCache->nPage==0 && pCache->pBulk ){
52542 sqlite3_free(pCache->pBulk);
52543 pCache->pBulk = pCache->pFree = 0;
52558 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
52560 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52561 assert( pCache->iMaxKey >= iLimit );
52562 assert( pCache->nHash > 0 );
52563 if( pCache->iMaxKey - iLimit < pCache->nHash ){
52568 h = iLimit % pCache->nHash;
52569 iStop = pCache->iMaxKey % pCache->nHash;
52570 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
52574 h = pCache->nHash/2;
52575 iStop = h - 1;
52580 assert( h<pCache->nHash );
52581 pp = &pCache->apHash[h];
52583 if( pPage->iKey>=iLimit ){
52584 pCache->nPage--;
52585 *pp = pPage->pNext;
52589 pp = &pPage->pNext;
52594 h = (h+1) % pCache->nHash;
52596 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
52613 ** private PGroup (mode-1). pcache1.separateCache is false if the single
52614 ** PGroup in pcache1.grp is used for all page caches (mode-2).
52616 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
52618 ** * Use a unified cache in single-threaded applications that have
52619 ** configured a start-time buffer for use as page-cache memory using
52620 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
52623 ** * Otherwise use separate caches (mode-1)
52677 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
52685 pGroup->mxPinned = 10;
52690 if( pGroup->lru.isAnchor==0 ){
52691 pGroup->lru.isAnchor = 1;
52692 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
52694 pCache->pGroup = pGroup;
52695 pCache->szPage = szPage;
52696 pCache->szExtra = szExtra;
52697 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
52698 pCache->bPurgeable = (bPurgeable ? 1 : 0);
52701 pCache->nMin = 10;
52702 pGroup->nMinPage += pCache->nMin;
52703 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52704 pCache->pnPurgeable = &pGroup->nPurgeable;
52706 pCache->pnPurgeable = &pCache->nPurgeableDummy;
52709 if( pCache->nHash==0 ){
52726 if( pCache->bPurgeable ){
52727 PGroup *pGroup = pCache->pGroup;
52730 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
52731 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
52733 pGroup->nMaxPage += (n - pCache->nMax);
52734 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52735 pCache->nMax = n;
52736 pCache->n90pct = pCache->nMax*9/10;
52749 if( pCache->bPurgeable ){
52750 PGroup *pGroup = pCache->pGroup;
52753 savedMaxPage = pGroup->nMaxPage;
52754 pGroup->nMaxPage = 0;
52756 pGroup->nMaxPage = savedMaxPage;
52767 pcache1EnterMutex(pCache->pGroup);
52768 n = pCache->nPage;
52769 pcache1LeaveMutex(pCache->pGroup);
52788 PGroup *pGroup = pCache->pGroup;
52792 assert( pCache->nPage >= pCache->nRecyclable );
52793 nPinned = pCache->nPage - pCache->nRecyclable;
52794 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
52795 assert( pCache->n90pct == pCache->nMax*9/10 );
52797 nPinned>=pGroup->mxPinned
52798 || nPinned>=pCache->n90pct
52799 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
52804 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
52805 assert( pCache->nHash>0 && pCache->apHash );
52808 if( pCache->bPurgeable
52809 && !pGroup->lru.pLruPrev->isAnchor
52810 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
52813 pPage = pGroup->lru.pLruPrev;
52817 pOther = pPage->pCache;
52818 if( pOther->szAlloc != pCache->szAlloc ){
52822 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
52834 unsigned int h = iKey % pCache->nHash;
52835 pCache->nPage++;
52836 pPage->iKey = iKey;
52837 pPage->pNext = pCache->apHash[h];
52838 pPage->pCache = pCache;
52839 pPage->pLruNext = 0;
52840 /* pPage->pLruPrev = 0;
52842 *(void **)pPage->page.pExtra = 0;
52843 pCache->apHash[h] = pPage;
52844 if( iKey>pCache->iMaxKey ){
52845 pCache->iMaxKey = iKey;
52861 ** For a non-purgeable cache (a cache used as the storage for an in-memory
52864 ** a non-purgeable cache.
52907 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
52919 pPage = pCache->apHash[iKey % pCache->nHash];
52920 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
52948 pcache1EnterMutex(pCache->pGroup);
52950 assert( pPage==0 || pCache->iMaxKey>=iKey );
52951 pcache1LeaveMutex(pCache->pGroup);
52965 assert( pCache->bPurgeable || createFlag!=1 );
52966 assert( pCache->bPurgeable || pCache->nMin==0 );
52967 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
52968 assert( pCache->nMin==0 || pCache->bPurgeable );
52969 assert( pCache->nHash>0 );
52971 if( pCache->pGroup->mutex ){
52993 PGroup *pGroup = pCache->pGroup;
52995 assert( pPage->pCache==pCache );
53001 assert( pPage->pLruNext==0 );
53004 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
53008 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
53009 pPage->pLruPrev = &pGroup->lru;
53010 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
53012 pCache->nRecyclable++;
53015 pcache1LeaveMutex(pCache->pGroup);
53031 assert( pPage->iKey==iOld );
53032 assert( pPage->pCache==pCache );
53034 pcache1EnterMutex(pCache->pGroup);
53036 h = iOld%pCache->nHash;
53037 pp = &pCache->apHash[h];
53039 pp = &(*pp)->pNext;
53041 *pp = pPage->pNext;
53043 h = iNew%pCache->nHash;
53044 pPage->iKey = iNew;
53045 pPage->pNext = pCache->apHash[h];
53046 pCache->apHash[h] = pPage;
53047 if( iNew>pCache->iMaxKey ){
53048 pCache->iMaxKey = iNew;
53051 pcache1LeaveMutex(pCache->pGroup);
53063 pcache1EnterMutex(pCache->pGroup);
53064 if( iLimit<=pCache->iMaxKey ){
53066 pCache->iMaxKey = iLimit-1;
53068 pcache1LeaveMutex(pCache->pGroup);
53078 PGroup *pGroup = pCache->pGroup;
53079 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
53081 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
53082 assert( pGroup->nMaxPage >= pCache->nMax );
53083 pGroup->nMaxPage -= pCache->nMax;
53084 assert( pGroup->nMinPage >= pCache->nMin );
53085 pGroup->nMinPage -= pCache->nMin;
53086 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
53089 sqlite3_free(pCache->pBulk);
53090 sqlite3_free(pCache->apHash);
53150 && p->isAnchor==0
53152 nFree += pcache1MemSize(p->page.pBuf);
53179 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
53239 ** a non-zero batch number, it will see all prior INSERTs.
53267 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
53325 p->pChunk = 0;
53326 p->db = db;
53327 p->pEntry = 0;
53328 p->pLast = 0;
53329 p->pForest = 0;
53330 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
53331 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
53332 p->rsFlags = ROWSET_SORTED;
53333 p->iBatch = 0;
53346 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
53347 pNextChunk = pChunk->pNextChunk;
53348 sqlite3DbFree(p->db, pChunk);
53350 p->pChunk = 0;
53351 p->nFresh = 0;
53352 p->pEntry = 0;
53353 p->pLast = 0;
53354 p->pForest = 0;
53355 p->rsFlags = ROWSET_SORTED;
53365 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
53373 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
53378 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
53382 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
53386 pNew->pNextChunk = p->pChunk;
53387 p->pChunk = pNew;
53388 p->pFresh = pNew->aEntry;
53389 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
53391 p->nFresh--;
53392 return p->pFresh++;
53406 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
53410 pEntry->v = rowid;
53411 pEntry->pRight = 0;
53412 pLast = p->pLast;
53414 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
53417 p->rsFlags &= ~ROWSET_SORTED;
53419 pLast->pRight = pEntry;
53421 p->pEntry = pEntry;
53423 p->pLast = pEntry;
53442 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
53443 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
53444 if( pA->v<=pB->v ){
53445 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
53446 pA = pA->pRight;
53448 pTail->pRight = pB;
53452 pTail = pTail->pRight = pB;
53453 pB = pB->pRight;
53455 pTail->pRight = pA;
53473 pNext = pIn->pRight;
53474 pIn->pRight = 0;
53502 if( pIn->pLeft ){
53504 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
53505 p->pRight = pIn;
53509 if( pIn->pRight ){
53510 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
53514 assert( (*ppLast)->pRight==0 );
53537 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
53541 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
53545 pLeft = rowSetNDeepTree(ppList, iDepth-1);
53547 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
53552 p->pLeft = pLeft;
53553 *ppList = p->pRight;
53554 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
53557 *ppList = p->pRight;
53558 p->pLeft = p->pRight = 0;
53574 pList = p->pRight;
53575 p->pLeft = p->pRight = 0;
53579 pList = p->pRight;
53580 p->pLeft = pLeft;
53581 p->pRight = rowSetNDeepTree(&pList, iDepth);
53601 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
53604 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53605 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53606 p->pEntry = rowSetEntrySort(p->pEntry);
53608 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
53612 if( p->pEntry ){
53613 *pRowid = p->pEntry->v;
53614 p->pEntry = p->pEntry->pRight;
53615 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
53630 ** on pRowSet->pEntry, then sort those entries into the forest at
53631 ** pRowSet->pForest so that they can be tested.
53637 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
53642 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
53643 p = pRowSet->pEntry;
53645 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
53646 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53650 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53651 ppPrevTree = &pTree->pRight;
53652 if( pTree->pLeft==0 ){
53653 pTree->pLeft = rowSetListToTree(p);
53657 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
53658 pTree->pLeft = 0;
53665 pTree->v = 0;
53666 pTree->pRight = 0;
53667 pTree->pLeft = rowSetListToTree(p);
53670 pRowSet->pEntry = 0;
53671 pRowSet->pLast = 0;
53672 pRowSet->rsFlags |= ROWSET_SORTED;
53674 pRowSet->iBatch = iBatch;
53680 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53681 p = pTree->pLeft;
53683 if( p->v<iRowid ){
53684 p = p->pRight;
53685 }else if( p->v>iRowid ){
53686 p = p->pLeft;
53732 ** This header file defines the interface to the write-ahead logging
53772 /* Connection to a write-ahead log (WAL) file.
53777 /* Open and close a connection to a write-ahead log. */
53785 ** snapshot is like a read-transaction. It is the state of the database
53794 /* Read a page from the write-ahead log, if it is present. */
53821 Wal *pWal, /* Write-ahead log connection */
53845 /* Return true if the argument is non-NULL and the WAL module is using
53846 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
53847 ** WAL module is using shared-memory, return false.
53861 ** stored in each frame (i.e. the db page-size when the WAL was created).
53928 ** (6) If a super-journal file is used, then all writes to the database file
53929 ** are synced prior to the super-journal being deleted.
53945 ** method is a no-op, but that does not change the fact the SQLite will
53956 ** (11) A database file is well-formed at the beginning and at the conclusion
53980 ** to print out file-descriptors.
53983 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
53986 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
53994 ** OPEN <------+------+
53997 ** +---------> READER-------+ |
54000 ** |<-------WRITER_LOCKED------> ERROR
54003 ** |<------WRITER_CACHEMOD-------->|
54006 ** |<-------WRITER_DBMOD---------->|
54009 ** +<------WRITER_FINISHED-------->+
54014 ** OPEN -> READER [sqlite3PagerSharedLock]
54015 ** READER -> OPEN [pager_unlock]
54017 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
54018 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
54019 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
54020 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
54021 ** WRITER_*** -> READER [pager_end_transaction]
54023 ** WRITER_*** -> ERROR [pager_error]
54024 ** ERROR -> OPEN [pager_unlock]
54030 ** state - the file may or may not be locked and the database size is
54040 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
54041 ** was) in exclusive-locking mode, a user-level read transaction is
54045 ** it opens a read-transaction on the database and returns to state
54046 ** OPEN after the read-transaction is completed. However a connection
54048 ** this state even after the read-transaction is closed. The only way
54052 ** * A read transaction may be active (but a write-transaction cannot).
54054 ** * The dbSize variable may be trusted (even if a user-level read
54058 ** * Even if a read-transaction is not open, it is guaranteed that
54059 ** there is no hot-journal in the file-system.
54063 ** The pager moves to this state from READER when a write-transaction
54065 ** required to start a write-transaction are held, but no actual
54080 ** * If the connection is open in rollback-mode, a RESERVED or greater
54082 ** * If the connection is open in WAL-mode, a WAL write transaction
54121 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
54137 ** The ERROR state is entered when an IO or disk-full error (including
54139 ** difficult to be sure that the in-memory pager state (cache contents,
54140 ** db size etc.) are consistent with the contents of the file-system.
54142 ** Temporary pager files may enter the ERROR state, but in-memory pagers
54146 ** the contents of the page-cache may be left in an inconsistent state.
54158 ** page-cache and any other in-memory state at the same time. Everything
54159 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
54160 ** when a read-transaction is next opened on the pager (transitioning
54176 ** In other cases, the error is returned to the b-tree layer. The b-tree
54180 ** Condition (3) is necessary because it can be triggered by a read-only
54182 ** code were simply returned to the user, the b-tree layer would not
54184 ** read-only statement cannot leave the pager in an internally inconsistent
54190 ** * The pager is not an in-memory pager.
54200 ** state. There are two exceptions: immediately after exclusive-mode has
54216 ** following locking-states, according to the lock currently held on
54224 ** pagerUnlockDb() take a conservative approach - eLock is always updated
54235 ** from ERROR to OPEN state. At this point there may be a hot-journal file
54236 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
54240 ** of hot-journal detection.
54246 ** a hot-journal may be mistaken for a journal being created by an active
54253 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
54254 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
54255 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
54280 ** set to 0. If a journal-header is written into the main journal while
54283 ** journal before the journal-header. This is required during savepoint
54292 Pgno iSubRec; /* Index of first record in sub-journal */
54317 ** For a real on-disk database, the current lock held on the database file -
54320 ** For a temporary or in-memory database (neither of which require any
54332 ** This boolean variable is used to make sure that the change-counter
54333 ** (the 4-byte header field at byte offset 24 of the database file) is
54336 ** It is set to true when the change-counter field is updated, which
54341 ** updating the change-counter is omitted for the current transaction.
54344 ** need only update the change-counter once, for the first transaction
54350 ** (or may not) specify a super-journal name to be written into the
54353 ** Whether or not a journal file contains a super-journal pointer affects
54356 ** If a journal file does not contain a super-journal pointer, it is
54358 ** it does contain a super-journal pointer the journal file is finalized
54362 ** Journal files that contain super-journal pointers cannot be finalized
54363 ** simply by overwriting the first journal-header with zeroes, as the
54364 ** super-journal pointer could interfere with hot-journal rollback of any
54374 ** This variables control the behavior of cache-spills (calls made by
54376 ** to the file-system in order to free up memory).
54388 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
54389 ** is larger than the database page-size in order to prevent a journal sync
54394 ** This is a boolean variable. If true, then any required sub-journal
54395 ** is opened as an in-memory journal file. If false, then in-memory
54396 ** sub-journals are only used for in-memory pager files.
54399 ** write-transaction is opened.
54410 ** is not an integer multiple of the page-size, the value stored in
54411 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
54413 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
54416 ** During a write-transaction, if pages with page-numbers greater than
54427 ** Throughout a write-transaction, dbFileSize contains the size of
54429 ** write-transaction is first opened, and updated when VFS calls are made
54449 ** write-transaction is opened (at the same time as dbFileSize and
54452 ** size-hint passed to the method call. See pager_write_pagelist() for
54460 ** sub-codes.
54486 u8 readOnly; /* True for a read-only database */
54488 u8 memVfs; /* VFS-implemented memory database */
54501 u8 changeCountDone; /* Set after incrementing the change-counter */
54502 u8 setSuper; /* Super-jrnl name is written into jrnl */
54503 u8 doNotSpill; /* Do not spill the cache when non-zero */
54504 u8 subjInMemory; /* True to use in-memory sub-journals */
54512 int nRec; /* Pages journalled since last j-header written */
54513 u32 cksumInit; /* Quasi-random value added to every checksum */
54514 u32 nSubRec; /* Number of records written to sub-journal */
54518 sqlite3_file *sjfd; /* File descriptor for sub-journal */
54531 ** End of the routinely-changing class members
54534 u16 nExtra; /* Add this many bytes to each in-memory page */
54555 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
54556 char *zWal; /* File name for write-ahead log */
54573 ** a non-testing build. These variables are not thread-safe.
54592 ** written, semi-random garbage data might appear in the journal
54599 ** of a 32-bit checksum on each page of data. The checksum covers both
54600 ** the page number and the pPager->pageSize bytes of data for the page.
54601 ** This cksum is initialized to a 32-bit random value that appears in the
54617 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
54623 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
54626 ** The macro MEMDB is true if we are dealing with an in-memory database.
54634 # define MEMDB pPager->memDb
54639 ** interfaces to access the database using memory-mapped I/O.
54642 # define USEFETCH(x) ((x)->bUseFetch)
54649 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
54653 ** if( isOpen(pPager->jfd) ){ ...
54657 ** if( pPager->jfd->pMethods ){ ...
54659 #define isOpen(pFd) ((pFd)->pMethods!=0)
54664 ** by the b-tree layer. This is the case if:
54671 if( pPager->fd->pMethods==0 ) return 0;
54672 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
54674 if( pPager->pWal ){
54677 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
54686 # define pagerUseWal(x) ((x)->pWal!=0)
54708 assert( p->eState==PAGER_OPEN
54709 || p->eState==PAGER_READER
54710 || p->eState==PAGER_WRITER_LOCKED
54711 || p->eState==PAGER_WRITER_CACHEMOD
54712 || p->eState==PAGER_WRITER_DBMOD
54713 || p->eState==PAGER_WRITER_FINISHED
54714 || p->eState==PAGER_ERROR
54717 /* Regardless of the current state, a temp-file connection always behaves
54719 ** the change-counter field, so the changeCountDone flag is always set.
54721 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
54722 assert( p->tempFile==0 || pPager->changeCountDone );
54724 /* If the useJournal flag is clear, the journal-mode must be "OFF".
54725 ** And if the journal-mode is "OFF", the journal file must not be open.
54727 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
54728 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
54730 /* Check that MEMDB implies noSync. And an in-memory journal. Since
54731 ** this means an in-memory pager performs no IO at all, it cannot encounter
54733 ** a journal file. (although the in-memory journal implementation may
54735 ** is therefore not possible for an in-memory pager to enter the ERROR
54739 assert( !isOpen(p->fd) );
54740 assert( p->noSync );
54741 assert( p->journalMode==PAGER_JOURNALMODE_OFF
54742 || p->journalMode==PAGER_JOURNALMODE_MEMORY
54744 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
54751 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
54752 assert( p->eLock!=PENDING_LOCK );
54754 switch( p->eState ){
54757 assert( pPager->errCode==SQLITE_OK );
54758 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
54762 assert( pPager->errCode==SQLITE_OK );
54763 assert( p->eLock!=UNKNOWN_LOCK );
54764 assert( p->eLock>=SHARED_LOCK );
54768 assert( p->eLock!=UNKNOWN_LOCK );
54769 assert( pPager->errCode==SQLITE_OK );
54771 assert( p->eLock>=RESERVED_LOCK );
54773 assert( pPager->dbSize==pPager->dbOrigSize );
54774 assert( pPager->dbOrigSize==pPager->dbFileSize );
54775 assert( pPager->dbOrigSize==pPager->dbHintSize );
54776 assert( pPager->setSuper==0 );
54780 assert( p->eLock!=UNKNOWN_LOCK );
54781 assert( pPager->errCode==SQLITE_OK );
54788 assert( p->eLock>=RESERVED_LOCK );
54789 assert( isOpen(p->jfd)
54790 || p->journalMode==PAGER_JOURNALMODE_OFF
54791 || p->journalMode==PAGER_JOURNALMODE_WAL
54794 assert( pPager->dbOrigSize==pPager->dbFileSize );
54795 assert( pPager->dbOrigSize==pPager->dbHintSize );
54799 assert( p->eLock==EXCLUSIVE_LOCK );
54800 assert( pPager->errCode==SQLITE_OK );
54802 assert( p->eLock>=EXCLUSIVE_LOCK );
54803 assert( isOpen(p->jfd)
54804 || p->journalMode==PAGER_JOURNALMODE_OFF
54805 || p->journalMode==PAGER_JOURNALMODE_WAL
54806 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54808 assert( pPager->dbOrigSize<=pPager->dbHintSize );
54812 assert( p->eLock==EXCLUSIVE_LOCK );
54813 assert( pPager->errCode==SQLITE_OK );
54815 assert( isOpen(p->jfd)
54816 || p->journalMode==PAGER_JOURNALMODE_OFF
54817 || p->journalMode==PAGER_JOURNALMODE_WAL
54818 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54827 assert( pPager->errCode!=SQLITE_OK );
54828 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
54861 , p->zFilename
54862 , p->eState==PAGER_OPEN ? "OPEN" :
54863 p->eState==PAGER_READER ? "READER" :
54864 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
54865 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
54866 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
54867 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
54868 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
54869 , (int)p->errCode
54870 , p->eLock==NO_LOCK ? "NO_LOCK" :
54871 p->eLock==RESERVED_LOCK ? "RESERVED" :
54872 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
54873 p->eLock==SHARED_LOCK ? "SHARED" :
54874 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
54875 , p->exclusiveMode ? "exclusive" : "normal"
54876 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
54877 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
54878 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
54879 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
54880 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
54881 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
54882 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
54883 , p->journalOff, p->journalHdr
54884 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
54903 if( pPager->errCode ){
54904 pPager->xGet = getPageError;
54907 pPager->xGet = getPageMMap;
54910 pPager->xGet = getPageNormal;
54915 ** Return true if it is necessary to write page *pPg into the sub-journal.
54916 ** A page needs to be written into the sub-journal if there exists one
54919 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
54920 ** * The bit corresponding to the page-number is not set in
54924 Pager *pPager = pPg->pPager;
54926 Pgno pgno = pPg->pgno;
54928 for(i=0; i<pPager->nSavepoint; i++){
54929 p = &pPager->aSavepoint[i];
54930 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
54931 for(i=i+1; i<pPager->nSavepoint; i++){
54932 pPager->aSavepoint[i].bTruncateOnRelease = 0;
54945 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
54950 ** Read a 32-bit integer from the given file descriptor. Store the integer
54954 ** All values are stored on disk as big-endian.
54966 ** Write a 32-bit integer into a string buffer in big-endian byte order.
54972 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
54993 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
54996 if( isOpen(pPager->fd) ){
54997 assert( pPager->eLock>=eLock );
54998 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
54999 if( pPager->eLock!=UNKNOWN_LOCK ){
55000 pPager->eLock = (u8)eLock;
55004 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
55022 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
55023 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
55024 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
55025 pPager->eLock = (u8)eLock;
55033 ** This function determines whether or not the atomic-write or
55034 ** atomic-batch-write optimizations can be used with this pager. The
55035 ** atomic-write optimization can be used if:
55045 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
55046 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
55058 assert( isOpen(pPager->fd) );
55059 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
55065 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
55066 return -1;
55072 int nSector = pPager->sectorSize;
55073 int szPage = pPager->pageSize;
55095 ** Return a 32-bit hash of the page data for pPage.
55106 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
55109 pPage->pageHash = pager_pagehash(pPage);
55115 ** that the page is either dirty or still matches the calculated page-hash.
55119 Pager *pPager = pPg->pPager;
55120 assert( pPager->eState!=PAGER_ERROR );
55121 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
55133 ** This function attempts to read a super-journal file name from the
55136 ** used to store a super-journal file name at the end of a journal file.
55140 ** enough space to write the super-journal name). If the super-journal
55142 ** nul-terminator), then this is handled as if no super-journal name
55145 ** If a super-journal file name is present at the end of the journal
55147 ** nul-terminator byte is appended to the buffer following the
55148 ** super-journal file name.
55150 ** If it is determined that no super-journal file name is present
55158 u32 len; /* Length in bytes of super-journal name */
55167 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
55169 || len>szJ-16
55171 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
55172 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
55174 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
55179 /* See if the checksum matches the super-journal name */
55181 cksum -= zSuper[u];
55185 ** containing the super-journal filename is corrupted. This means
55187 ** super-journal filename.
55199 ** following the value in pPager->journalOff, assuming a sector
55200 ** size of pPager->sectorSize bytes.
55205 ** ---------------------------------------
55214 i64 c = pPager->journalOff;
55216 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
55220 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
55227 ** This function is a no-op if the journal file has not been written to
55230 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
55232 ** zero the 28-byte header at the start of the journal file. In either case,
55233 ** if the pager is not in no-sync mode, sync the journal file immediately
55236 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
55247 assert( isOpen(pPager->jfd) );
55248 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
55249 if( pPager->journalOff ){
55250 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
55254 rc = sqlite3OsTruncate(pPager->jfd, 0);
55257 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
55259 if( rc==SQLITE_OK && !pPager->noSync ){
55260 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
55271 rc = sqlite3OsFileSize(pPager->jfd, &sz);
55273 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
55286 ** - 8 bytes: Magic identifying journal format.
55287 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
55288 ** - 4 bytes: Random number used for page hash.
55289 ** - 4 bytes: Initial database page count.
55290 ** - 4 bytes: Sector size used by the process that wrote this journal.
55291 ** - 4 bytes: Database page size.
55293 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
55297 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
55298 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
55302 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
55312 for(ii=0; ii<pPager->nSavepoint; ii++){
55313 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
55314 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
55318 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
55321 ** Write the nRec Field - the number of page records that follow this
55324 ** if in full-sync mode), the zero is overwritten with the true number
55334 ** * When the pager is in no-sync mode. Corruption can follow a
55340 assert( isOpen(pPager->fd) || pPager->noSync );
55341 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
55342 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
55350 /* The random check-hash initializer */
55351 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
55352 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
55354 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
55356 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
55359 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
55367 nHeader-(sizeof(aJournalMagic)+20));
55378 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
55381 ** The loop is required here in case the sector-size is larger than the
55387 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
55388 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
55389 assert( pPager->journalHdr <= pPager->journalOff );
55390 pPager->journalOff += nHeader;
55400 ** pPager->journalOff. See comments above function writeJournalHdr() for
55405 ** database before the transaction began, in pages. Also, pPager->cksumInit
55424 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
55430 pPager->journalOff = journalHdrOffset(pPager);
55431 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
55434 iHdrOff = pPager->journalOff;
55441 if( isHot || iHdrOff!=pPager->journalHdr ){
55442 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
55451 /* Read the first three 32-bit fields of the journal header: The nRec
55452 ** field, the checksum-initializer and the database size at the start
55455 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
55456 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
55457 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
55462 if( pPager->journalOff==0 ){
55463 u32 iPageSize; /* Page-size field of journal header */
55464 u32 iSectorSize; /* Sector-size field of journal header */
55466 /* Read the page-size and sector-size journal header fields. */
55467 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
55468 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
55473 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
55478 iPageSize = pPager->pageSize;
55481 /* Check that the values read from the page-size and sector-size fields
55488 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
55490 /* If the either the page-size or sector-size in the journal-header is
55491 ** invalid, then the process that wrote the journal-header must have
55498 /* Update the page-size to match the value read from the journal.
55502 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
55505 /* Update the assumed sector-size to match the value used by
55511 pPager->sectorSize = iSectorSize;
55514 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
55520 ** Write the supplied super-journal name into the journal file for pager
55521 ** pPager at the current location. The super-journal name must be the last
55522 ** thing written to a journal file. If the pager is in full-sync mode, the
55527 ** + N bytes: super-journal filename in utf-8.
55528 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
55529 ** + 4 bytes: super-journal name checksum.
55532 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
55533 ** name, where each byte is interpreted as a signed 8-bit integer.
55536 ** this call is a no-op.
55545 assert( pPager->setSuper==0 );
55549 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
55550 || !isOpen(pPager->jfd)
55554 pPager->setSuper = 1;
55555 assert( pPager->journalHdr <= pPager->journalOff );
55562 /* If in full-sync mode, advance to the next disk sector before writing
55563 ** the super-journal name. This is in case the previous page written to
55566 if( pPager->fullSync ){
55567 pPager->journalOff = journalHdrOffset(pPager);
55569 iHdrOff = pPager->journalOff;
55571 /* Write the super-journal data to the end of the journal file. If
55574 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
55575 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
55576 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
55577 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
55578 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
55583 pPager->journalOff += (nSuper+20);
55585 /* If the pager is in peristent-journal mode, then the physical
55586 ** journal-file may extend past the end of the super-journal name
55588 ** dangerous because the code to rollback a hot-journal file
55589 ** will not be able to find the super-journal name to determine
55595 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
55596 && jrnlSize>pPager->journalOff
55598 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
55604 ** Discard the entire contents of the in-memory page-cache.
55607 pPager->iDataVersion++;
55608 sqlite3BackupRestart(pPager->pBackup);
55609 sqlite3PcacheClear(pPager->pPCache);
55613 ** Return the pPager->iDataVersion value
55616 return pPager->iDataVersion;
55621 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
55626 for(ii=0; ii<pPager->nSavepoint; ii++){
55627 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
55629 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
55630 sqlite3OsClose(pPager->sjfd);
55632 sqlite3_free(pPager->aSavepoint);
55633 pPager->aSavepoint = 0;
55634 pPager->nSavepoint = 0;
55635 pPager->nSubRec = 0;
55647 for(ii=0; ii<pPager->nSavepoint; ii++){
55648 PagerSavepoint *p = &pPager->aSavepoint[ii];
55649 if( pgno<=p->nOrig ){
55650 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
55659 ** This function is a no-op if the pager is in exclusive mode and not
55663 ** If the pager is not in exclusive-access mode, the database file is
55664 ** completely unlocked. If the file is unlocked and the file-system does
55670 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
55671 ** or not, any journal file left in the file-system will be treated
55672 ** as a hot-journal and rolled back the next time a read-transaction
55677 assert( pPager->eState==PAGER_READER
55678 || pPager->eState==PAGER_OPEN
55679 || pPager->eState==PAGER_ERROR
55682 sqlite3BitvecDestroy(pPager->pInJournal);
55683 pPager->pInJournal = 0;
55687 assert( !isOpen(pPager->jfd) );
55688 sqlite3WalEndReadTransaction(pPager->pWal);
55689 pPager->eState = PAGER_OPEN;
55690 }else if( !pPager->exclusiveMode ){
55692 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
55706 || 1!=(pPager->journalMode & 5)
55708 sqlite3OsClose(pPager->jfd);
55717 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
55718 pPager->eLock = UNKNOWN_LOCK;
55722 ** without clearing the error code. This is intentional - the error
55725 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
55726 pPager->eState = PAGER_OPEN;
55732 ** normal and exclusive-locking mode.
55734 assert( pPager->errCode==SQLITE_OK || !MEMDB );
55735 if( pPager->errCode ){
55736 if( pPager->tempFile==0 ){
55738 pPager->changeCountDone = 0;
55739 pPager->eState = PAGER_OPEN;
55741 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
55743 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
55744 pPager->errCode = SQLITE_OK;
55748 pPager->journalOff = 0;
55749 pPager->journalHdr = 0;
55750 pPager->setSuper = 0;
55757 ** the error-code about to be returned by a pager API function. The
55761 ** IOERR sub-codes, the pager enters the ERROR state and the error code
55765 ** The ERROR state indicates that the contents of the pager-cache
55767 ** the contents of the pager-cache. If a transaction was active when
55770 ** it were a hot-journal).
55776 pPager->errCode==SQLITE_FULL ||
55777 pPager->errCode==SQLITE_OK ||
55778 (pPager->errCode & 0xff)==SQLITE_IOERR
55781 pPager->errCode = rc;
55782 pPager->eState = PAGER_ERROR;
55798 ** * For non-TEMP databases, always sync to disk. This is necessary
55807 if( pPager->tempFile==0 ) return 1;
55809 if( !isOpen(pPager->fd) ) return 0;
55810 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
55816 ** after rollback of a hot-journal, or if an error occurs while opening
55817 ** the journal file or writing the very first journal-header of a
55822 ** exclusive than a RESERVED lock, it is a no-op.
55828 ** transaction. Nor will it be considered to be a hot-journal by this
55831 ** the current journal-mode (Pager.journalMode value), as follows:
55835 ** in-memory journal.
55854 ** If running in non-exclusive rollback mode, the lock on the file is
55872 ** is no write-transaction active but a RESERVED or greater lock is
55875 ** 1. After a successful hot-journal rollback, it is called with
55880 ** read-transaction, this function is called with eState==PAGER_READER
55881 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
55884 assert( pPager->eState!=PAGER_ERROR );
55885 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
55890 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
55891 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
55893 if( isOpen(pPager->jfd) ){
55897 if( sqlite3JournalIsInMemory(pPager->jfd) ){
55898 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
55899 sqlite3OsClose(pPager->jfd);
55900 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
55901 if( pPager->journalOff==0 ){
55904 rc = sqlite3OsTruncate(pPager->jfd, 0);
55905 if( rc==SQLITE_OK && pPager->fullSync ){
55911 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
55914 pPager->journalOff = 0;
55915 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
55916 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
55918 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
55919 pPager->journalOff = 0;
55922 ** a hot-journal was just rolled back. In this case the journal
55924 ** the database file, it will do so using an in-memory journal.
55926 int bDelete = !pPager->tempFile;
55927 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
55928 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
55929 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
55930 || pPager->journalMode==PAGER_JOURNALMODE_WAL
55932 sqlite3OsClose(pPager->jfd);
55934 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
55940 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
55941 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
55944 p->pageHash = 0;
55950 sqlite3BitvecDestroy(pPager->pInJournal);
55951 pPager->pInJournal = 0;
55952 pPager->nRec = 0;
55955 sqlite3PcacheCleanAll(pPager->pPCache);
55957 sqlite3PcacheClearWritable(pPager->pPCache);
55959 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
55963 /* Drop the WAL write-lock, if any. Also, if the connection was in
55967 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
55969 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
55970 /* This branch is taken when committing a transaction in rollback-journal
55976 assert( pPager->eLock==EXCLUSIVE_LOCK );
55977 rc = pager_truncate(pPager, pPager->dbSize);
55981 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
55985 if( !pPager->exclusiveMode
55986 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
55990 pPager->eState = PAGER_READER;
55991 pPager->setSuper = 0;
56002 ** call to pager_unlock() will discard all in-memory pages, unlock
56004 ** means that there is a hot-journal left in the file-system, the next
56014 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
56016 if( pPager->eState>=PAGER_WRITER_LOCKED ){
56020 }else if( !pPager->exclusiveMode ){
56021 assert( pPager->eState==PAGER_READER );
56029 ** Parameter aData must point to a buffer of pPager->pageSize bytes
56031 ** page of data and the current value of pPager->cksumInit.
56034 ** random initial value (pPager->cksumInit) and every 200th byte
56035 ** of the page data, starting with byte offset (pPager->pageSize%200).
56036 ** Each byte is interpreted as an 8-bit unsigned integer.
56048 u32 cksum = pPager->cksumInit; /* Checksum value to return */
56049 int i = pPager->pageSize-200; /* Loop counter */
56052 i -= 200;
56059 ** from the sub-journal (if isMainJrnl==0) and playback that page.
56063 ** The main rollback journal uses checksums - the statement journal does
56066 ** If the page number of the page record read from the (sub-)journal file
56076 ** If the page record is successfully read from the (sub-)journal file
56078 ** while reading the record from the (sub-)journal file or while writing
56080 ** is successfully read from the (sub-)journal file but appears to be
56084 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
56098 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
56111 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
56112 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
56114 aData = pPager->pTmpSpace;
56120 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
56121 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
56122 ** only reads from the main journal, not the sub-journal.
56124 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
56125 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
56127 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
56129 /* Read the page number and page data from the journal or sub-journal
56132 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
56135 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
56137 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
56148 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
56152 rc = read32bits(jfd, (*pOffset)-4, &cksum);
56168 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
56169 pPager->nReserve = ((u8*)aData)[20];
56176 ** An exception to the above rule: If the database is in no-sync mode
56186 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
56203 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
56213 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
56215 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
56216 (isMainJrnl?"main-journal":"sub-journal")
56219 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
56221 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
56223 if( isOpen(pPager->fd)
56224 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56227 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
56228 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
56232 ** This is usually safe even for an encrypted database - as the data
56234 ** is if the data was just read from an in-memory sub-journal. In that
56237 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
56239 if( pgno>pPager->dbFileSize ){
56240 pPager->dbFileSize = pgno;
56242 if( pPager->pBackup ){
56243 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
56247 ** the database and the page is not in-memory, there is a potential
56248 ** problem. When the page is next fetched by the b-tree layer, it
56254 ** if the page is on the free-list at the start of the transaction, then
56257 ** The solution is to add an in-memory page to the cache containing
56258 ** the data just read from the sub-journal. Mark the page as dirty
56259 ** and if the pager requires a journal-sync, then mark the page as
56260 ** requiring a journal-sync before it is written.
56263 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
56264 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
56266 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
56267 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
56279 pData = pPg->pData;
56280 memcpy(pData, (u8*)aData, pPager->pageSize);
56281 pPager->xReiniter(pPg);
56291 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
56299 ** Parameter zSuper is the name of a super-journal file. A single journal
56300 ** file that referred to the super-journal file has just been rolled back.
56301 ** This routine checks if it is possible to delete the super-journal file,
56307 ** When a super-journal file is created, it is populated with the names
56308 ** of all of its child journals, one after another, formatted as utf-8
56310 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
56313 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
56315 ** A super-journal file may only be deleted once all of its child
56318 ** This function reads the contents of the super-journal file into
56323 ** * if the child journal contains a reference to super-journal
56329 ** the file-system using sqlite3OsDelete().
56337 ** the entire contents of the super-journal file. This could be
56338 ** a couple of kilobytes or so - potentially larger than the page
56342 sqlite3_vfs *pVfs = pPager->pVfs;
56344 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
56345 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
56346 char *zSuperJournal = 0; /* Contents of super-journal file */
56347 i64 nSuperJournal; /* Size of super-journal file */
56349 char *zSuperPtr; /* Space to hold super-journal filename */
56354 ** If successful, open the super-journal file for reading.
56356 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
56363 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
56367 /* Load the entire super-journal file into space obtained from
56369 ** sufficient space (in zSuperPtr) to hold the names of super-journal
56370 ** files extracted from regular rollback-journals.
56374 nSuperPtr = pVfs->mxPathname+1;
56389 while( (zJournal-zSuperJournal)<nSuperJournal ){
56396 /* One of the journals pointed to by the super-journal exists.
56397 ** Open it and check if it points at the super-journal. If
56398 ** so, return without deleting the super-journal file.
56418 /* We have a match. Do not delete the super-journal file. */
56441 ** file in the file-system. This only happens when committing a transaction,
56442 ** or rolling back a transaction (including rolling back a hot-journal).
56445 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
56446 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
56461 assert( pPager->eState!=PAGER_ERROR );
56462 assert( pPager->eState!=PAGER_READER );
56464 if( isOpen(pPager->fd)
56465 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56468 int szPage = pPager->pageSize;
56469 assert( pPager->eLock==EXCLUSIVE_LOCK );
56471 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
56475 rc = sqlite3OsTruncate(pPager->fd, newSize);
56477 char *pTmp = pPager->pTmpSpace;
56479 testcase( (newSize-szPage) == currentSize );
56480 testcase( (newSize-szPage) > currentSize );
56481 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
56482 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
56485 pPager->dbFileSize = nPage;
56493 ** Return a sanitized version of the sector-size of OS file pFile. The
56512 ** super-journal pointers within created journal files.
56516 ** Otherwise, for non-temporary files, the effective sector size is
56523 ** pPager->sectorSize is to define the "blast radius" of bytes that
56531 assert( isOpen(pPager->fd) || pPager->tempFile );
56533 if( pPager->tempFile
56534 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
56540 pPager->sectorSize = 512;
56542 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
56553 ** (2) 4 byte big-endian integer which is the number of valid page records
56556 ** (3) 4 byte big-endian integer which is the initial value for the
56560 ** (5) 4 byte big-endian integer which is the sector size. The header
56562 ** (6) 4 byte big-endian integer which is the page size.
56566 ** + pPager->pageSize bytes of data.
56583 ** no-sync option for the journal. A power failure could lead to corruption
56587 ** If the file opened as the journal file is not a well-formed
56593 ** If an I/O or malloc() error occurs, the journal-file is not deleted
56604 sqlite3_vfs *pVfs = pPager->pVfs;
56611 char *zSuper = 0; /* Name of super-journal file if any */
56614 u32 savedPageSize = pPager->pageSize;
56619 assert( isOpen(pPager->jfd) );
56620 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
56625 /* Read the super-journal name from the journal, if it is present.
56626 ** If a super-journal file name is specified, but the file is not
56632 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
56636 zSuper = pPager->pTmpSpace;
56637 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56645 pPager->journalOff = 0;
56667 ** working in no-sync mode. This means that the rest of the journal
56672 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
56673 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
56691 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
56692 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
56698 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
56703 pPager->dbSize = mxPg;
56704 if( pPager->mxPgno<mxPg ){
56705 pPager->mxPgno = mxPg;
56717 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
56722 pPager->journalOff = szJ;
56748 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
56752 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
56756 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
56760 ** malloc error that occurred after the change-counter was updated but
56761 ** before the transaction was committed, then the change-counter
56764 ** update the change-counter at all. This may lead to cache inconsistency
56768 pPager->changeCountDone = pPager->tempFile;
56771 /* Leave 4 bytes of space before the super-journal filename in memory.
56775 zSuper = &pPager->pTmpSpace[4];
56776 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56780 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56789 /* If there was a super-journal and this routine will return success,
56790 ** see if it is possible to delete the super-journal.
56792 assert( zSuper==&pPager->pTmpSpace[4] );
56793 memset(&zSuper[-4], 0, 4);
56799 nPlayback, pPager->zJournal);
56814 ** pPg->pData. A shared lock or greater must be held on the database
56824 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
56830 assert( pPager->eState>=PAGER_READER && !MEMDB );
56831 assert( isOpen(pPager->fd) );
56834 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
56838 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
56842 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
56843 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
56849 if( pPg->pgno==1 ){
56856 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
56863 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
56865 u8 *dbFileVers = &((u8*)pPg->pData)[24];
56866 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
56870 PAGER_INCR(pPager->nRead);
56871 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
56873 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
56879 ** Update the value of the change-counter at offsets 24 and 92 in
56883 ** routine which only updates the change-counter if the update is actually
56884 ** needed, as determined by the pPager->changeCountDone state variable.
56891 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
56892 put32bits(((char*)pPg->pData)+24, change_counter);
56897 put32bits(((char*)pPg->pData)+92, change_counter);
56898 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
56927 pPager->xReiniter(pPg);
56941 sqlite3BackupRestart(pPager->pBackup);
56960 pPager->dbSize = pPager->dbOrigSize;
56961 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
56962 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56964 PgHdr *pNext = pList->pDirty;
56965 rc = pagerUndoCallback((void *)pPager, pList->pgno);
56991 assert( pPager->pWal );
56995 for(p=pList; p && p->pDirty; p=p->pDirty){
56996 assert( p->pgno < p->pDirty->pgno );
57000 assert( pList->pDirty==0 || isCommit );
57008 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
57009 if( p->pgno<=nTruncate ){
57010 ppNext = &p->pDirty;
57018 pPager->aStat[PAGER_STAT_WRITE] += nList;
57020 if( pList->pgno==1 ) pager_write_changecounter(pList);
57021 rc = sqlite3WalFrames(pPager->pWal,
57022 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
57024 if( rc==SQLITE_OK && pPager->pBackup ){
57025 for(p=pList; p; p=p->pDirty){
57026 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
57031 pList = sqlite3PcacheDirtyList(pPager->pPCache);
57032 for(p=pList; p; p=p->pDirty){
57053 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
57060 sqlite3WalEndReadTransaction(pPager->pWal);
57062 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
57065 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57084 /* Query the WAL sub-system for the database size. The WalDbsize()
57087 ** available from the WAL sub-system if the log file is empty or
57090 assert( pPager->eState==PAGER_OPEN );
57091 assert( pPager->eLock>=SHARED_LOCK );
57092 assert( isOpen(pPager->fd) );
57093 assert( pPager->tempFile==0 );
57094 nPage = sqlite3WalDbsize(pPager->pWal);
57097 ** WAL sub-system, determine the page count based on the size of
57099 ** integer multiple of the page-size, round up the result.
57101 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
57103 int rc = sqlite3OsFileSize(pPager->fd, &n);
57107 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
57114 if( nPage>pPager->mxPgno ){
57115 pPager->mxPgno = (Pgno)nPage;
57124 ** Check if the *-wal file that corresponds to the database opened by pPager
57125 ** exists if the database is not empy, or verify that the *-wal file does
57128 ** If the database is not empty and the *-wal file exists, open the pager
57129 ** in WAL mode. If the database is empty or if no *-wal file exists and
57137 ** a WAL on a none-empty database, this ensures there is no race condition
57143 assert( pPager->eState==PAGER_OPEN );
57144 assert( pPager->eLock>=SHARED_LOCK );
57146 if( !pPager->tempFile ){
57149 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
57158 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
57160 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
57163 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
57164 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
57174 ** the entire super-journal file. The case pSavepoint==NULL occurs when
57178 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
57191 ** * Pages are then played back from the sub-journal file, starting
57205 ** (or transaction). No page with a page-number greater than this value
57210 i64 iHdrOff; /* End of first segment of main-journal records */
57214 assert( pPager->eState!=PAGER_ERROR );
57215 assert( pPager->eState>=PAGER_WRITER_LOCKED );
57219 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
57228 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
57229 pPager->changeCountDone = pPager->tempFile;
57235 /* Use pPager->journalOff as the effective size of the main rollback
57238 ** past pPager->journalOff is off-limits to us.
57240 szJ = pPager->journalOff;
57246 ** greater than the current database size (pPager->dbSize) but those
57251 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
57252 pPager->journalOff = pSavepoint->iOffset;
57253 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
57254 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57258 pPager->journalOff = 0;
57263 ** of the main journal file. Continue to skip out-of-range pages and
57266 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
57274 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
57279 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
57281 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
57283 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
57284 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57288 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
57290 /* Finally, rollback pages from the sub-journal. Page that were
57292 ** will be skipped. Out-of-range pages are also skipped.
57296 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
57299 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
57301 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
57302 assert( offset==(i64)ii*(4+pPager->pageSize) );
57310 pPager->journalOff = szJ;
57317 ** Change the maximum number of in-memory pages that are allowed
57321 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
57325 ** Change the maximum number of in-memory pages that are allowed
57329 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
57337 sqlite3_file *fd = pPager->fd;
57338 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
57340 sz = pPager->szMmap;
57341 pPager->bUseFetch = (sz>0);
57343 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
57352 pPager->szMmap = szMmap;
57360 sqlite3PcacheShrink(pPager->pPCache);
57382 ** database (with some additional information - the nRec field
57383 ** of the journal header - being written in between the two
57393 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
57404 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
57420 if( pPager->tempFile ){
57421 pPager->noSync = 1;
57422 pPager->fullSync = 0;
57423 pPager->extraSync = 0;
57425 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
57426 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
57427 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
57429 if( pPager->noSync ){
57430 pPager->syncFlags = 0;
57432 pPager->syncFlags = SQLITE_SYNC_FULL;
57434 pPager->syncFlags = SQLITE_SYNC_NORMAL;
57436 pPager->walSyncFlags = (pPager->syncFlags<<2);
57437 if( pPager->fullSync ){
57438 pPager->walSyncFlags |= pPager->syncFlags;
57440 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
57441 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
57444 pPager->doNotSpill &= ~SPILLFLAG_OFF;
57446 pPager->doNotSpill |= SPILLFLAG_OFF;
57488 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
57496 ** The pager invokes the busy-handler if sqlite3OsLock() returns
57497 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
57501 ** (which occurs during hot-journal rollback). Summary:
57504 ** --------------------------------------------------------
57505 ** NO_LOCK -> SHARED_LOCK | Yes
57506 ** SHARED_LOCK -> RESERVED_LOCK | No
57507 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
57508 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
57510 ** If the busy-handler callback returns non-zero, the lock is
57516 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
57520 pPager->xBusyHandler = xBusyHandler;
57521 pPager->pBusyHandlerArg = pBusyHandlerArg;
57522 ap = (void **)&pPager->xBusyHandler;
57525 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
57533 ** is a no-op. The value returned is the error state error code (i.e.
57534 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
57543 ** * the database is either not an in-memory database or it is
57544 ** an in-memory database that currently consists of zero pages.
57568 ** is a no-op for that case anyhow.
57573 if( (pPager->memDb==0 || pPager->dbSize==0)
57574 && sqlite3PcacheRefCount(pPager->pPCache)==0
57575 && pageSize && pageSize!=(u32)pPager->pageSize
57580 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
57581 rc = sqlite3OsFileSize(pPager->fd, &nByte);
57584 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
57596 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
57599 sqlite3PageFree(pPager->pTmpSpace);
57600 pPager->pTmpSpace = pNew;
57601 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
57602 pPager->pageSize = pageSize;
57603 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
57609 *pPageSize = pPager->pageSize;
57611 if( nReserve<0 ) nReserve = pPager->nReserve;
57613 pPager->nReserve = (i16)nReserve;
57628 return pPager->pTmpSpace;
57640 pPager->mxPgno = mxPage;
57642 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
57643 /* assert( pPager->mxPgno>=pPager->dbSize ); */
57647 return pPager->mxPgno;
57655 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
57664 sqlite3_io_error_pending = -1;
57691 assert( isOpen(pPager->fd) || pPager->tempFile );
57699 if( isOpen(pPager->fd) ){
57701 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
57710 ** This function may only be called when a read-transaction is open on
57713 ** However, if the file is between 1 and <page-size> bytes in size, then
57717 assert( pPager->eState>=PAGER_READER );
57718 assert( pPager->eState!=PAGER_WRITER_FINISHED );
57719 *pnPage = (int)pPager->dbSize;
57725 ** a similar or greater lock is already held, this function is a no-op
57740 /* Check that this is either a no-op (because the requested lock is
57741 ** already held), or one of the transitions that the busy-handler
57745 assert( (pPager->eLock>=locktype)
57746 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
57747 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
57752 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
57758 ** following is true for all dirty pages currently in the page-cache:
57764 ** be necessary to write the current content out to the sub-journal.
57773 ** sub-journal rolled back the content could not be restored and the
57779 Pager *pPager = pPg->pPager;
57780 assert( pPg->flags&PGHDR_DIRTY );
57781 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
57782 Pgno pgno = pPg->pgno;
57784 for(i=0; i<pPg->pPager->nSavepoint; i++){
57785 PagerSavepoint *p = &pPager->aSavepoint[i];
57786 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
57791 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
57798 ** Truncate the in-memory database file image to nPage pages. This
57809 assert( pPager->dbSize>=nPage || CORRUPT_DB );
57810 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
57811 pPager->dbSize = nPage;
57826 ** This function is called before attempting a hot-journal rollback. It
57827 ** syncs the journal file to disk, then sets pPager->journalHdr to the
57831 ** Syncing a hot-journal to disk before attempting to roll it back ensures
57832 ** that if a power-failure occurs during the rollback, the process that
57841 if( !pPager->noSync ){
57842 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
57845 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
57869 if( pPager->pMmapFreelist ){
57870 *ppPage = p = pPager->pMmapFreelist;
57871 pPager->pMmapFreelist = p->pDirty;
57872 p->pDirty = 0;
57873 assert( pPager->nExtra>=8 );
57874 memset(p->pExtra, 0, 8);
57876 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
57878 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
57881 p->pExtra = (void *)&p[1];
57882 p->flags = PGHDR_MMAP;
57883 p->nRef = 1;
57884 p->pPager = pPager;
57887 assert( p->pExtra==(void *)&p[1] );
57888 assert( p->pPage==0 );
57889 assert( p->flags==PGHDR_MMAP );
57890 assert( p->pPager==pPager );
57891 assert( p->nRef==1 );
57893 p->pgno = pgno;
57894 p->pData = pData;
57895 pPager->nMmapOut++;
57906 Pager *pPager = pPg->pPager;
57907 pPager->nMmapOut--;
57908 pPg->pDirty = pPager->pMmapFreelist;
57909 pPager->pMmapFreelist = pPg;
57911 assert( pPager->fd->pMethods->iVersion>=3 );
57912 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
57921 for(p=pPager->pMmapFreelist; p; p=pNext){
57922 pNext = p->pDirty;
57929 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
57936 if( pPager->tempFile ) return SQLITE_OK;
57937 if( pPager->dbSize==0 ) return SQLITE_OK;
57938 assert( pPager->zFilename && pPager->zFilename[0] );
57939 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
57941 /* If the HAS_MOVED file-control is unimplemented, assume that the file
57967 u8 *pTmp = (u8*)pPager->pTmpSpace;
57973 /* pPager->errCode = 0; */
57974 pPager->exclusiveMode = 0;
57978 assert( db || pPager->pWal==0 );
57979 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
57984 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
57985 pPager->pWal = 0;
58000 ** back or finalize it. The next database user will have to do hot-journal
58003 if( isOpen(pPager->jfd) ){
58012 sqlite3OsClose(pPager->jfd);
58013 sqlite3OsClose(pPager->fd);
58015 sqlite3PcacheClose(pPager->pPCache);
58016 assert( !pPager->aSavepoint && !pPager->pInJournal );
58017 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
58028 return pPg->pgno;
58042 ** disk and can be restored in the event of a hot-journal rollback.
58044 ** If the Pager.noSync flag is set, then this function is a no-op.
58045 ** Otherwise, the actions required depend on the journal-mode and the
58046 ** device characteristics of the file-system, as follows:
58048 ** * If the journal file is an in-memory journal file, no action need
58054 ** been written following it. If the pager is operating in full-sync
58060 ** Or, in pseudo-code:
58062 ** if( NOT <in-memory journal> ){
58064 ** if( <full-sync mode> ) xSync(<journal file>);
58077 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58078 || pPager->eState==PAGER_WRITER_DBMOD
58086 if( !pPager->noSync ){
58087 assert( !pPager->tempFile );
58088 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58089 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58090 assert( isOpen(pPager->jfd) );
58094 ** that wrote to this database was operating in persistent-journal
58097 ** file happens to be a journal-header (written as part of the
58098 ** previous connection's transaction), and a crash or power-failure
58102 ** hot-journal rollback following recovery. It may roll back all
58104 ** out-of-date data that follows it. Database corruption.
58120 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
58123 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
58126 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
58133 ** full-synchronous mode, sync the journal first. This ensures that
58143 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
58146 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
58149 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
58151 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
58158 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
58159 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
58164 pPager->journalHdr = pPager->journalOff;
58166 pPager->nRec = 0;
58171 pPager->journalHdr = pPager->journalOff;
58179 sqlite3PcacheClearSyncFlags(pPager->pPCache);
58180 pPager->eState = PAGER_WRITER_DBMOD;
58188 ** in-memory pages in the list to the database file. The argument may
58190 ** a no-op.
58197 ** If the pager is a temp-file pager and the actual file-system file
58222 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
58223 assert( pPager->eLock==EXCLUSIVE_LOCK );
58224 assert( isOpen(pPager->fd) || pList->pDirty==0 );
58226 /* If the file is a temp-file has not yet been opened, open it now. It
58228 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
58230 if( !isOpen(pPager->fd) ){
58231 assert( pPager->tempFile && rc==SQLITE_OK );
58232 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
58238 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
58240 && pPager->dbHintSize<pPager->dbSize
58241 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
58243 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
58244 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
58245 pPager->dbHintSize = pPager->dbSize;
58249 Pgno pgno = pList->pgno;
58253 ** make the file smaller (presumably by auto-vacuum code). Do not write
58259 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
58260 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
58263 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
58264 if( pList->pgno==1 ) pager_write_changecounter(pList);
58266 pData = pList->pData;
58269 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
58276 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
58278 if( pgno>pPager->dbFileSize ){
58279 pPager->dbFileSize = pgno;
58281 pPager->aStat[PAGER_STAT_WRITE]++;
58284 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
58294 pList = pList->pDirty;
58301 ** Ensure that the sub-journal file is open. If it is already open, this
58302 ** function is a no-op.
58310 if( !isOpen(pPager->sjfd) ){
58315 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
58316 nStmtSpill = -1;
58318 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
58324 ** Append a record of the current state of page pPg to the sub-journal.
58326 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
58330 ** error code if the attempt to write to the sub-journal fails, or
58336 Pager *pPager = pPg->pPager;
58337 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
58339 /* Open the sub-journal, if it has not already been opened */
58340 assert( pPager->useJournal );
58341 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
58342 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
58345 || pPg->pgno>pPager->dbOrigSize
58349 /* If the sub-journal was opened successfully (or was already open),
58352 void *pData = pPg->pData;
58353 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
58356 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
58357 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
58359 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
58364 pPager->nSubRec++;
58365 assert( pPager->nSavepoint>0 );
58366 rc = addToSavepointBitvecs(pPager, pPg->pgno);
58381 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
58401 assert( pPg->pPager==pPager );
58402 assert( pPg->flags&PGHDR_DIRTY );
58420 if( NEVER(pPager->errCode) ) return SQLITE_OK;
58421 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
58422 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
58423 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
58424 if( pPager->doNotSpill
58425 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
58426 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
58431 pPager->aStat[PAGER_STAT_SPILL]++;
58432 pPg->pDirty = 0;
58442 if( pPager->tempFile==0 ){
58443 rc = sqlite3JournalCreate(pPager->jfd);
58449 if( pPg->flags&PGHDR_NEED_SYNC
58450 || pPager->eState==PAGER_WRITER_CACHEMOD
58457 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
58464 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
58475 int rc = pPager->errCode;
58477 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
58480 PgHdr *pNext = pList->pDirty;
58481 if( pList->nRef==0 ){
58497 ** If zFilename is NULL then a randomly-named temporary file is created
58501 ** This can be used to implement an in-memory database.
58527 int nExtra, /* Extra bytes append to each in-memory page */
58535 int tempFile = 0; /* True for temp files (incl. in-memory files) */
58536 int memDb = 0; /* True if this is an in-memory file */
58542 int readOnly = 0; /* True if this is a read-only file */
58553 /* Figure out how much space is required for each journal file-handle
58554 ** (there are two of them, the main journal and the sub-journal). */
58578 nPathname = pVfs->mxPathname+1;
58601 nUriByte = (int)(&z[1] - zUri);
58603 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
58605 ** the database being opened will be more than pVfs->mxPathname
58608 ** check for a hot-journal before reading.
58624 ** Database file handle (pVfs->szOsFile bytes)
58625 ** Sub-journal file handle (journalFileSize bytes)
58635 ** Some 3rd-party software, over which we have no control, depends on
58639 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
58642 ** filename format expected by 3rd-party software should be as follows:
58644 ** - Main Database Path
58645 ** - \0
58646 ** - Multiple URI components consisting of:
58647 ** - Key
58648 ** - \0
58649 ** - Value
58650 ** - \0
58651 ** - \0
58652 ** - Journal Path
58653 ** - \0
58654 ** - WAL Path (zWALName)
58655 ** - \0
58665 ROUND8(pVfs->szOsFile) + /* The main db file */
58683 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
58684 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
58685 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58686 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58687 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
58692 pPager->zFilename = (char*)pPtr;
58705 pPager->zJournal = (char*)pPtr;
58707 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
58709 sqlite3FileSuffix3(zFilename,pPager->zJournal);
58710 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
58713 pPager->zJournal = 0;
58719 pPager->zWal = (char*)pPtr;
58721 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
58723 sqlite3FileSuffix3(zFilename, pPager->zWal);
58724 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
58727 pPager->zWal = 0;
58733 pPager->pVfs = pVfs;
58734 pPager->vfsFlags = vfsFlags;
58740 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
58743 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
58756 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58760 if( szPageDflt<pPager->sectorSize ){
58761 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
58764 szPageDflt = (u32)pPager->sectorSize;
58781 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
58783 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
58793 ** This branch is also run for an in-memory database. An in-memory
58794 ** database is the same as a temp-file that is never written out to
58795 ** disk and uses an in-memory rollback journal.
58801 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
58802 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
58803 pPager->noLock = 1; /* Do no locking */
58811 assert( pPager->memDb==0 );
58812 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
58821 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
58827 sqlite3OsClose(pPager->fd);
58828 sqlite3PageFree(pPager->pTmpSpace);
58833 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
58834 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
58836 pPager->useJournal = (u8)useJournal;
58837 /* pPager->stmtOpen = 0; */
58838 /* pPager->stmtInUse = 0; */
58839 /* pPager->nRef = 0; */
58840 /* pPager->stmtSize = 0; */
58841 /* pPager->stmtJSize = 0; */
58842 /* pPager->nPage = 0; */
58843 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
58844 /* pPager->state = PAGER_UNLOCK; */
58845 /* pPager->errMask = 0; */
58846 pPager->tempFile = (u8)tempFile;
58850 pPager->exclusiveMode = (u8)tempFile;
58851 pPager->changeCountDone = pPager->tempFile;
58852 pPager->memDb = (u8)memDb;
58853 pPager->readOnly = (u8)readOnly;
58854 assert( useJournal || pPager->tempFile );
58855 pPager->noSync = pPager->tempFile;
58856 if( pPager->noSync ){
58857 assert( pPager->fullSync==0 );
58858 assert( pPager->extraSync==0 );
58859 assert( pPager->syncFlags==0 );
58860 assert( pPager->walSyncFlags==0 );
58862 pPager->fullSync = 1;
58863 pPager->extraSync = 0;
58864 pPager->syncFlags = SQLITE_SYNC_NORMAL;
58865 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
58867 /* pPager->pFirst = 0; */
58868 /* pPager->pFirstSynced = 0; */
58869 /* pPager->pLast = 0; */
58870 pPager->nExtra = (u16)nExtra;
58871 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
58872 assert( isOpen(pPager->fd) || tempFile );
58875 pPager->journalMode = PAGER_JOURNALMODE_OFF;
58877 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
58879 /* pPager->xBusyHandler = 0; */
58880 /* pPager->pBusyHandlerArg = 0; */
58881 pPager->xReiniter = xReinit;
58883 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
58884 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
58897 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
58898 zName--;
58900 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
58901 return pPager->fd;
58908 ** the file-system for the given pager. A hot journal is one that
58909 ** needs to be played back. According to this function, a hot-journal
58923 ** This routine does not check if there is a super-journal filename
58924 ** at the end of the file. If there is, and that super-journal file
58926 ** case this routine will return a false-positive. The pager_playback()
58930 ** If a hot-journal file is found to exist, *pExists is set to 1 and
58931 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
58933 ** to determine whether or not a hot-journal file exists, the IO error
58937 sqlite3_vfs * const pVfs = pPager->pVfs;
58940 int jrnlOpen = !!isOpen(pPager->jfd);
58942 assert( pPager->useJournal );
58943 assert( isOpen(pPager->fd) );
58944 assert( pPager->eState==PAGER_OPEN );
58946 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
58952 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
58962 ** in fact there is none. This results in a false-positive which will
58965 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
58969 assert( pPager->tempFile==0 );
58983 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
58984 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
58990 ** at least one non-zero bytes at the start of the journal file.
58996 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
59000 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
59005 sqlite3OsClose(pPager->jfd);
59032 ** has been successfully called. If a shared-lock is already held when
59033 ** this function is called, it is a no-op.
59040 ** the SHARED lock, the file-system is checked for a hot-journal,
59041 ** which is played back if present. Following any hot-journal
59043 ** the 'change-counter' field of the database file header and
59046 ** 2) If the pager is running in exclusive-mode, and there are currently
59053 ** occurs while locking the database, checking for a hot-journal file or
59059 /* This routine is only called from b-tree and only when there are no
59063 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
59065 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
59066 assert( pPager->errCode==SQLITE_OK );
59068 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
59069 int bHotJournal = 1; /* True if there exists a hot journal-file */
59072 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
59076 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
59083 if( pPager->eLock<=SHARED_LOCK ){
59090 if( pPager->readOnly ){
59100 ** hot-journal back.
59117 ** in exclusive-access mode the file descriptor will be kept open
59118 ** and possibly used for a transaction later on. Also, write-access
59125 ** may mean that the pager was in the error-state when this
59128 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
59129 sqlite3_vfs * const pVfs = pPager->pVfs;
59132 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
59136 assert( !pPager->tempFile );
59137 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
59138 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
59141 sqlite3OsClose(pPager->jfd);
59148 ** playing back the hot-journal so that we don't end up with
59154 if( isOpen(pPager->jfd) ){
59158 rc = pager_playback(pPager, !pPager->tempFile);
59159 pPager->eState = PAGER_OPEN;
59161 }else if( !pPager->exclusiveMode ){
59167 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
59186 assert( pPager->eState==PAGER_OPEN );
59187 assert( (pPager->eLock==SHARED_LOCK)
59188 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
59192 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
59193 /* The shared-lock has just been acquired then check to
59197 ** single unnecessary sqlite3OsRead() call at the start-up.
59201 ** a 32-bit counter that is incremented with each change. The
59209 char dbFileVers[sizeof(pPager->dbFileVers)];
59212 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
59220 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
59230 sqlite3OsUnfetch(pPager->fd, 0, 0);
59235 /* If there is a WAL file in the file-system, open this database in WAL
59236 ** mode. Otherwise, the following function call is a no-op.
59240 assert( pPager->pWal==0 || rc==SQLITE_OK );
59249 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
59250 rc = pagerPagecount(pPager, &pPager->dbSize);
59257 assert( pPager->eState==PAGER_OPEN );
59259 pPager->eState = PAGER_READER;
59260 pPager->hasHeldSharedLock = 1;
59271 ** nothing to rollback, so this routine is a no-op.
59274 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
59275 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
59288 ** getPageNormal() -- The normal getter
59289 ** getPageError() -- Used if the pager is in an error state
59290 ** getPageMmap() -- Used if memory-mapped I/O is enabled
59312 ** a) When reading a free-list leaf page from the database, and
59330 ** to find a page in the in-memory cache first. If the page is not already
59332 ** just returns 0. This routine acquires a read-lock the first time it
59348 assert( pPager->errCode==SQLITE_OK );
59349 assert( pPager->eState>=PAGER_READER );
59351 assert( pPager->hasHeldSharedLock==1 );
59354 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
59357 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
59364 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
59366 assert( pPg->pgno==pgno );
59367 assert( pPg->pPager==pPager || pPg->pPager==0 );
59370 if( pPg->pPager && !noContent ){
59374 pPager->aStat[PAGER_STAT_HIT]++;
59389 pPg->pPager = pPager;
59391 assert( !isOpen(pPager->fd) || !MEMDB );
59392 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
59393 if( pgno>pPager->mxPgno ){
59398 /* Failure to set the bits in the InJournal bit-vectors is benign.
59405 if( pgno<=pPager->dbOrigSize ){
59406 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
59413 memset(pPg->pData, 0, pPager->pageSize);
59416 assert( pPg->pPager==pPager );
59417 pPager->aStat[PAGER_STAT_MISS]++;
59438 /* The page getter for when memory-mapped I/O is enabled */
59449 /* It is acceptable to use a read-only (mmap) page for any page except
59450 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
59452 ** temporary or in-memory database. */
59454 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
59466 assert( pPager->eState>=PAGER_READER );
59468 assert( pPager->hasHeldSharedLock==1 );
59469 assert( pPager->errCode==SQLITE_OK );
59472 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
59480 rc = sqlite3OsFetch(pPager->fd,
59481 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
59484 if( pPager->eState>PAGER_READER || pPager->tempFile ){
59490 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
59516 assert( pPager->errCode!=SQLITE_OK );
59518 return pPager->errCode;
59531 return pPager->xGet(pPager, pgno, ppPage, flags);
59535 ** Acquire a page if it is already in the in-memory cache. Do
59549 assert( pPager->pPCache!=0 );
59550 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
59551 assert( pPage==0 || pPager->hasHeldSharedLock );
59553 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
59569 TESTONLY( Pager *pPager = pPg->pPager; )
59571 if( pPg->flags & PGHDR_MMAP ){
59572 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
59578 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
59586 assert( pPg->pgno==1 );
59587 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
59588 pPager = pPg->pPager;
59599 ** to the start of it. If there are active savepoints, open the sub-journal
59617 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
59619 assert( pPager->eState==PAGER_WRITER_LOCKED );
59621 assert( pPager->pInJournal==0 );
59623 /* If already in the error state, this function is a no-op. But on
59626 if( NEVER(pPager->errCode) ) return pPager->errCode;
59628 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
59629 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
59630 if( pPager->pInJournal==0 ){
59635 if( !isOpen(pPager->jfd) ){
59636 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
59637 sqlite3MemJournalOpen(pPager->jfd);
59642 if( pPager->tempFile ){
59655 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
59659 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
59664 ** the sub-journal if necessary.
59668 pPager->nRec = 0;
59669 pPager->journalOff = 0;
59670 pPager->setSuper = 0;
59671 pPager->journalHdr = 0;
59677 sqlite3BitvecDestroy(pPager->pInJournal);
59678 pPager->pInJournal = 0;
59680 assert( pPager->eState==PAGER_WRITER_LOCKED );
59681 pPager->eState = PAGER_WRITER_CACHEMOD;
59688 ** Begin a write-transaction on the specified pager object. If a
59689 ** write-transaction has already been opened, this function is a no-op.
59696 ** If the subjInMemory argument is non-zero, then any sub-journal opened
59697 ** within this transaction will be opened as an in-memory file. This
59698 ** has no effect if the sub-journal is already opened (as it may be when
59700 ** sub-journal. If the subjInMemory argument is zero, then any required
59701 ** sub-journal is implemented in-memory if pPager is an in-memory database,
59707 if( pPager->errCode ) return pPager->errCode;
59708 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
59709 pPager->subjInMemory = (u8)subjInMemory;
59711 if( pPager->eState==PAGER_READER ){
59712 assert( pPager->pInJournal==0 );
59718 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
59723 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
59728 ** The busy-handler is not invoked if another connection already
59729 ** holds the write-lock. If possible, the upper layer will call it.
59731 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
59735 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
59750 ** transactions may copy data from the sub-journal into the database
59754 pPager->eState = PAGER_WRITER_LOCKED;
59755 pPager->dbHintSize = pPager->dbSize;
59756 pPager->dbFileSize = pPager->dbSize;
59757 pPager->dbOrigSize = pPager->dbSize;
59758 pPager->journalOff = 0;
59761 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
59762 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
59774 Pager *pPager = pPg->pPager;
59778 i64 iOff = pPager->journalOff;
59783 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
59785 assert( pPager->journalHdr<=pPager->journalOff );
59786 pData2 = pPg->pData;
59790 ** page in the block above, set the need-sync flag for the page.
59796 pPg->flags |= PGHDR_NEED_SYNC;
59798 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
59800 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
59802 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
59805 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
59806 pPager->journalOff, pPager->pageSize));
59809 PAGERID(pPager), pPg->pgno,
59810 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
59812 pPager->journalOff += 8 + pPager->pageSize;
59813 pPager->nRec++;
59814 assert( pPager->pInJournal!=0 );
59815 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
59818 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
59825 ** main journal or sub-journal as required. If the page is written into
59831 Pager *pPager = pPg->pPager;
59834 /* This routine is not called unless a write-transaction has already
59838 assert( pPager->eState==PAGER_WRITER_LOCKED
59839 || pPager->eState==PAGER_WRITER_CACHEMOD
59840 || pPager->eState==PAGER_WRITER_DBMOD
59843 assert( pPager->errCode==0 );
59844 assert( pPager->readOnly==0 );
59848 ** obtained the necessary locks to begin the write-transaction, but the
59856 if( pPager->eState==PAGER_WRITER_LOCKED ){
59860 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
59870 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
59871 if( pPager->pInJournal!=0
59872 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
59875 if( pPg->pgno<=pPager->dbOrigSize ){
59881 if( pPager->eState!=PAGER_WRITER_DBMOD ){
59882 pPg->flags |= PGHDR_NEED_SYNC;
59885 PAGERID(pPager), pPg->pgno,
59886 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
59890 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
59895 pPg->flags |= PGHDR_WRITEABLE;
59900 if( pPager->nSavepoint>0 ){
59905 if( pPager->dbSize<pPg->pgno ){
59906 pPager->dbSize = pPg->pgno;
59929 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
59930 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
59937 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
59938 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
59940 /* This trick assumes that both the page-size and sector-size are
59944 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
59946 nPageCount = pPager->dbSize;
59947 if( pPg->pgno>nPageCount ){
59948 nPage = (pPg->pgno - pg1)+1;
59949 }else if( (pg1+nPagePerSector-1)>nPageCount ){
59950 nPage = nPageCount+1-pg1;
59955 assert(pg1<=pPg->pgno);
59956 assert((pg1+nPage)>pPg->pgno);
59961 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
59966 if( pPage->flags&PGHDR_NEED_SYNC ){
59973 if( pPage->flags&PGHDR_NEED_SYNC ){
59991 pPage->flags |= PGHDR_NEED_SYNC;
59997 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
59998 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
60010 ** fit on a single disk sector. In this case all co-resident pages
60017 Pager *pPager = pPg->pPager;
60018 assert( (pPg->flags & PGHDR_MMAP)==0 );
60019 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60021 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
60022 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
60024 }else if( pPager->errCode ){
60025 return pPager->errCode;
60026 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
60027 assert( pPager->tempFile==0 );
60041 return pPg->flags & PGHDR_WRITEABLE;
60059 ** This optimization cannot be used with a temp-file, as the page may
60066 Pager *pPager = pPg->pPager;
60067 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
60068 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
60069 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
60070 pPg->flags |= PGHDR_DONT_WRITE;
60071 pPg->flags &= ~PGHDR_WRITEABLE;
60072 testcase( pPg->flags & PGHDR_NEED_SYNC );
60079 ** change-counter, stored as a 4-byte big-endian integer starting at
60083 ** But this only happens if the pPager->changeCountDone flag is false.
60093 ** The isDirectMode flag may only be non-zero if the library was compiled
60095 ** if isDirect is non-zero, then the database file is updated directly
60102 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60103 || pPager->eState==PAGER_WRITER_DBMOD
60108 ** atomic-write optimization is enabled in this build, then isDirect
60112 ** The idea is that if the atomic-write optimization is not
60125 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
60128 assert( !pPager->tempFile && isOpen(pPager->fd) );
60135 ** operating in direct-mode, make page 1 writable. When not in
60137 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
60150 assert( pPager->dbFileSize>0 );
60151 zBuf = pPgHdr->pData;
60153 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
60154 pPager->aStat[PAGER_STAT_WRITE]++;
60157 /* Update the pager's copy of the change-counter. Otherwise, the
60159 ** flushed (as the change-counter values will not match). */
60161 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
60162 pPager->changeCountDone = 1;
60165 pPager->changeCountDone = 1;
60176 ** Sync the database file to disk. This is a no-op for in-memory databases
60179 ** If successful, or if called on a pager for which it is a no-op, this
60185 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
60187 if( rc==SQLITE_OK && !pPager->noSync ){
60189 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
60195 ** This function may only be called while a write-transaction is active in
60196 ** rollback. If the connection is in WAL mode, this call is a no-op.
60206 int rc = pPager->errCode;
60209 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60210 || pPager->eState==PAGER_WRITER_DBMOD
60211 || pPager->eState==PAGER_WRITER_LOCKED
60223 ** of a super-journal file that should be written into the individual
60225 ** super-journal (a single database transaction).
60229 ** * The database file change-counter is updated,
60230 ** * the journal is synced (unless the atomic-write optimization is used),
60237 ** delete the super-journal file if specified).
60242 ** If the final parameter - noSync - is true, then the database file itself
60249 const char *zSuper, /* If not NULL, the super-journal name */
60254 assert( pPager->eState==PAGER_WRITER_LOCKED
60255 || pPager->eState==PAGER_WRITER_CACHEMOD
60256 || pPager->eState==PAGER_WRITER_DBMOD
60257 || pPager->eState==PAGER_ERROR
60262 if( NEVER(pPager->errCode) ) return pPager->errCode;
60268 pPager->zFilename, zSuper, pPager->dbSize));
60271 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
60273 assert( MEMDB==0 || pPager->tempFile );
60274 assert( isOpen(pPager->fd) || pPager->tempFile );
60276 /* If this is an in-memory db, or no pages have been written to, or this
60277 ** function has already been called, it is mostly a no-op. However, any
60279 sqlite3BackupRestart(pPager->pBackup);
60284 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60287 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
60290 pList->pDirty = 0;
60294 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
60298 sqlite3PcacheCleanAll(pPager->pPCache);
60301 /* The bBatch boolean is true if the batch-atomic-write commit method
60302 ** should be used. No rollback journal is created if batch-atomic-write
60306 sqlite3_file *fd = pPager->fd;
60309 && !pPager->noSync
60310 && sqlite3JournalIsInMemory(pPager->jfd);
60316 /* The following block updates the change-counter. Exactly how it
60317 ** does this depends on whether or not the atomic-update optimization
60321 ** * The file-system supports the atomic-write property for
60322 ** blocks of size page-size, and
60323 ** * This commit is not part of a multi-file transaction, and
60328 ** counter in 'indirect-mode'. If the optimization is compiled in but
60331 ** pager_incr_changecounter() to update the change-counter in indirect
60335 ** then call pager_incr_changecounter() to update the change-counter
60341 assert( isOpen(pPager->jfd)
60342 || pPager->journalMode==PAGER_JOURNALMODE_OFF
60343 || pPager->journalMode==PAGER_JOURNALMODE_WAL
60345 if( !zSuper && isOpen(pPager->jfd)
60346 && pPager->journalOff==jrnlBufferSize(pPager)
60347 && pPager->dbSize>=pPager->dbOrigSize
60348 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
60350 /* Update the db file change counter via the direct-write method. The
60351 ** following call will modify the in-memory representation of page 1
60353 ** directly to the database file. Because of the atomic-write
60354 ** property of the host file-system, this is safe.
60358 rc = sqlite3JournalCreate(pPager->jfd);
60367 rc = sqlite3JournalCreate(pPager->jfd);
60376 /* Write the super-journal name into the journal file. If a
60377 ** super-journal file name has already been written to the journal file,
60378 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
60384 ** If the atomic-update optimization is being used, this sync will not
60387 ** Because the change-counter page was just modified, unless the
60388 ** atomic-update optimization is used it is almost certain that the
60392 ** xSync() call will be changed to a no-op by the OS anyhow.
60397 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60412 rc = sqlite3JournalCreate(pPager->jfd);
60414 sqlite3OsClose(pPager->jfd);
60419 sqlite3OsClose(pPager->jfd);
60431 sqlite3PcacheCleanAll(pPager->pPCache);
60436 ** last page in the db image moved to the free-list. In this case the
60439 if( pPager->dbSize>pPager->dbFileSize ){
60440 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
60441 assert( pPager->eState==PAGER_WRITER_DBMOD );
60456 pPager->eState = PAGER_WRITER_FINISHED;
60465 ** synced to disk. The journal file still exists in the file-system
60467 ** be used as a hot-journal and the current transaction rolled back.
60471 ** for hot-journal rollback. Once this is done the transaction is
60483 if( NEVER(pPager->errCode) ) return pPager->errCode;
60484 pPager->iDataVersion++;
60486 assert( pPager->eState==PAGER_WRITER_LOCKED
60487 || pPager->eState==PAGER_WRITER_FINISHED
60488 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
60493 ** this transaction, the pager is running in exclusive-mode and is
60494 ** using persistent journals, then this function is a no-op.
60498 ** a hot-journal during hot-journal rollback, 0 changes will be made
60503 if( pPager->eState==PAGER_WRITER_LOCKED
60504 && pPager->exclusiveMode
60505 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
60507 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
60508 pPager->eState = PAGER_READER;
60513 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
60519 ** transaction are reverted and the current write-transaction is closed.
60529 ** in-memory cache pages to the state they were in when the transaction
60538 ** In WAL mode, all cache-entries containing data modified within the
60540 ** their pre-transaction state by re-reading data from the database or
60547 /* PagerRollback() is a no-op if called in READER or OPEN state. If
60552 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
60553 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
60557 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
60558 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
60560 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
60561 int eState = pPager->eState;
60568 pPager->errCode = SQLITE_ABORT;
60569 pPager->eState = PAGER_ERROR;
60577 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
60590 ** Return TRUE if the database file is opened read-only. Return FALSE
60594 return pPager->readOnly;
60602 return sqlite3PcacheRefCount(pPager->pPCache);
60611 int perPageSize = pPager->pageSize + pPager->nExtra
60613 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
60615 + pPager->pageSize;
60631 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
60632 a[1] = sqlite3PcachePagecount(pPager->pPCache);
60633 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
60634 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
60635 a[4] = pPager->eState;
60636 a[5] = pPager->errCode;
60637 a[6] = pPager->aStat[PAGER_STAT_HIT];
60638 a[7] = pPager->aStat[PAGER_STAT_MISS];
60639 a[8] = 0; /* Used to be pPager->nOvfl */
60640 a[9] = pPager->nRead;
60641 a[10] = pPager->aStat[PAGER_STAT_WRITE];
60654 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
60670 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
60671 *pnVal += pPager->aStat[eStat];
60673 pPager->aStat[eStat] = 0;
60678 ** Return true if this is an in-memory or temp-file backed pager.
60681 return pPager->tempFile || pPager->memVfs;
60688 ** equal to nSavepoint, then this function is a no-op.
60691 ** occurs while opening the sub-journal file, then an IO error code is
60696 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
60700 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60702 assert( nSavepoint>nCurrent && pPager->useJournal );
60709 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
60714 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
60715 pPager->aSavepoint = aNew;
60719 aNew[ii].nOrig = pPager->dbSize;
60720 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
60721 aNew[ii].iOffset = pPager->journalOff;
60725 aNew[ii].iSubRec = pPager->nSubRec;
60726 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
60732 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
60734 pPager->nSavepoint = ii+1;
60736 assert( pPager->nSavepoint==nSavepoint );
60741 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60744 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
60764 ** (the first created). A value of (Pager.nSavepoint-1) means operate
60766 ** (Pager.nSavepoint-1), then this function is a no-op.
60783 int rc = pPager->errCode;
60792 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
60801 for(ii=nNew; ii<pPager->nSavepoint; ii++){
60802 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
60804 pPager->nSavepoint = nNew;
60806 /* Truncate the sub-journal so that it only includes the parts
60809 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
60810 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
60811 /* Only truncate if it is an in-memory sub-journal. */
60812 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
60813 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
60814 rc = sqlite3OsTruncate(pPager->sjfd, sz);
60817 pPager->nSubRec = pRel->iSubRec;
60821 ** If this is a temp-file, it is possible that the journal file has
60825 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
60826 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
60837 pPager->journalMode==PAGER_JOURNALMODE_OFF
60838 && pPager->eState>=PAGER_WRITER_CACHEMOD
60840 pPager->errCode = SQLITE_ABORT;
60841 pPager->eState = PAGER_ERROR;
60853 ** Except, if the pager is in-memory only, then return an empty string if
60857 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
60858 ** participate in shared-cache.
60865 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
60872 return pPager->pVfs;
60881 return pPager->fd;
60890 return pPager->jfd;
60892 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
60900 return pPager->zJournal;
60913 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
60921 ** If the fourth argument, isCommit, is non-zero, then this page is being
60931 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
60935 assert( pPg->nRef>0 );
60936 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60937 || pPager->eState==PAGER_WRITER_DBMOD
60941 /* In order to be able to rollback, an in-memory database must journal
60944 assert( pPager->tempFile || !MEMDB );
60945 if( pPager->tempFile ){
60952 ** sub-journal now. This is required to handle the following scenario:
60960 ** If page X were not written to the sub-journal here, it would not
60964 ** subjournalPage() may need to allocate space to store pPg->pgno into
60968 if( (pPg->flags & PGHDR_DIRTY)!=0
60975 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
60976 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
60978 /* If the journal needs to be sync()ed before page pPg->pgno can
60979 ** be written to, store pPg->pgno in local variable needSyncPgno.
60982 ** the journal needs to be sync()ed before database page pPg->pgno
60985 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
60986 needSyncPgno = pPg->pgno;
60987 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
60988 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
60989 assert( pPg->flags&PGHDR_DIRTY );
60992 /* If the cache contains a page with page-number pgno, remove it
60997 pPg->flags &= ~PGHDR_NEED_SYNC;
60999 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
61001 if( NEVER(pPgOld->nRef>1) ){
61005 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
61006 if( pPager->tempFile ){
61007 /* Do not discard pages from an in-memory database since we might
61009 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
61015 origPgno = pPg->pgno;
61019 /* For an in-memory database, make sure the original page continues
61023 if( pPager->tempFile && pPgOld ){
61029 /* If needSyncPgno is non-zero, then the journal file needs to be
61031 ** Currently, no such page exists in the page-cache and the
61033 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
61036 ** If the attempt to load the page into the page-cache fails, (due
61046 if( needSyncPgno<=pPager->dbOrigSize ){
61047 assert( pPager->pTmpSpace!=0 );
61048 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
61052 pPgHdr->flags |= PGHDR_NEED_SYNC;
61068 assert( pPg->pgno!=iNew );
61069 pPg->flags = flags;
61077 assert( pPg->nRef>0 || pPg->pPager->memDb );
61078 return pPg->pData;
61086 return pPg->pExtra;
61090 ** Get/set the locking-mode for this pager. Parameter eMode must be one
61093 ** the locking-mode is set to the value specified.
61097 ** locking-mode.
61105 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
61106 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
61107 pPager->exclusiveMode = (u8)eMode;
61109 return (int)pPager->exclusiveMode;
61113 ** Set the journal-mode for this pager. Parameter eMode must be one of:
61125 ** * An in-memory database can only have its journal_mode set to _OFF
61130 ** The returned indicate the current (possibly updated) journal-mode.
61133 u8 eOld = pPager->journalMode; /* Prior journalmode */
61147 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
61149 /* Do allow the journalmode of an in-memory database to be set to
61162 assert( pPager->eState!=PAGER_ERROR );
61163 pPager->journalMode = (u8)eMode;
61176 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
61177 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
61186 sqlite3OsClose(pPager->jfd);
61187 if( pPager->eLock>=RESERVED_LOCK ){
61188 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61191 int state = pPager->eState;
61196 if( pPager->eState==PAGER_READER ){
61201 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61208 assert( state==pPager->eState );
61211 sqlite3OsClose(pPager->jfd);
61216 return (int)pPager->journalMode;
61223 return (int)pPager->journalMode;
61233 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
61234 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
61239 ** Get/set the size-limit used for persistent journal files.
61241 ** Setting the size limit to -1 means no limit is enforced.
61242 ** An attempt to set a limit smaller than -1 is a no-op.
61245 if( iLimit>=-1 ){
61246 pPager->journalSizeLimit = iLimit;
61247 sqlite3WalLimit(pPager->pWal, iLimit);
61249 return pPager->journalSizeLimit;
61253 ** Return a pointer to the pPager->pBackup variable. The backup module
61259 return &pPager->pBackup;
61264 ** Unless this is an in-memory or temporary database, clear the pager cache.
61267 assert( MEMDB==0 || pPager->tempFile );
61268 if( pPager->tempFile==0 ) pager_reset(pPager);
61289 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
61301 if( pPager->pWal ){
61302 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
61303 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
61304 pPager->pBusyHandlerArg,
61305 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
61313 return sqlite3WalCallback(pPager->pWal);
61318 ** primitives necessary for write-ahead logging.
61321 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
61322 if( pPager->noLock ) return 0;
61323 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
61333 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
61346 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
61347 ** lock on the database file and use heap-memory to store the wal-index
61348 ** in. Otherwise, use the normal shared-memory.
61353 assert( pPager->pWal==0 && pPager->tempFile==0 );
61354 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
61356 /* If the pager is already in exclusive-mode, the WAL module will use
61357 ** heap-memory for the wal-index instead of the VFS shared-memory
61361 if( pPager->exclusiveMode ){
61369 rc = sqlite3WalOpen(pPager->pVfs,
61370 pPager->fd, pPager->zWal, pPager->exclusiveMode,
61371 pPager->journalSizeLimit, &pPager->pWal
61385 ** file (not a temp file or an in-memory database), and the WAL file
61391 ** If the pager is open on a temp-file (or in-memory database), or if
61397 int *pbOpen /* OUT: Set to true if call is a no-op */
61402 assert( pPager->eState==PAGER_OPEN || pbOpen );
61403 assert( pPager->eState==PAGER_READER || !pbOpen );
61405 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
61407 if( !pPager->tempFile && !pPager->pWal ){
61411 sqlite3OsClose(pPager->jfd);
61415 pPager->journalMode = PAGER_JOURNALMODE_WAL;
61416 pPager->eState = PAGER_OPEN;
61437 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
61439 /* If the log file is not already open, but does exist in the file-system,
61443 if( !pPager->pWal ){
61448 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
61457 ** the database file, the log and log-summary files will be deleted.
61459 if( rc==SQLITE_OK && pPager->pWal ){
61462 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
61463 pPager->pageSize, (u8*)pPager->pTmpSpace);
61464 pPager->pWal = 0;
61466 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
61474 ** If pager pPager is a wal-mode database not in exclusive locking mode,
61481 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
61482 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
61493 sqlite3WalDb(pPager->pWal, db);
61505 if( pPager->pWal ){
61506 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
61521 if( pPager->pWal ){
61522 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
61535 if( pPager->pWal ){
61536 rc = sqlite3WalSnapshotRecover(pPager->pWal);
61557 if( pPager->pWal ){
61558 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
61570 assert( pPager->pWal );
61571 sqlite3WalSnapshotUnlock(pPager->pWal);
61579 ** A read-lock must be held on the pager when this function is called. If
61586 assert( pPager->eState>=PAGER_READER );
61587 return sqlite3WalFramesize(pPager->pWal);
61607 ** This file contains the implementation of a write-ahead log (WAL) used in
61610 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
61629 ** big-endian 32-bit unsigned integer values:
61635 ** 16: Salt-1, random integer incremented with each checkpoint
61636 ** 20: Salt-2, a different random integer changing with each ckpt
61637 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
61638 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
61640 ** Immediately following the wal-header are zero or more frames. Each
61641 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
61642 ** of page data. The frame-header is six big-endian 32-bit unsigned
61648 ** 8: Salt-1 (copied from the header)
61649 ** 12: Salt-2 (copied from the header)
61650 ** 16: Checksum-1.
61651 ** 20: Checksum-2.
61656 ** (1) The salt-1 and salt-2 values in the frame-header match
61657 ** salt values in the wal-header
61659 ** (2) The checksum values in the final 8 bytes of the frame-header
61664 ** The checksum is computed using 32-bit big-endian integers if the
61666 ** is computed using little-endian if the magic number is 0x377f0682.
61668 ** big-endian format regardless of which byte order is used to compute
61670 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
61673 ** for i from 0 to n-1 step 2:
61680 ** of the sequence being summed.) The s1 value spans all 32-bit
61683 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
61684 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
61685 ** The VFS.xSync operations serve as write barriers - all writes launched
61689 ** After each checkpoint, the salt-1 value is incremented and the salt-2
61718 ** data structure called the wal-index is maintained to expedite the
61721 ** WAL-INDEX FORMAT
61723 ** Conceptually, the wal-index is shared memory, though VFS implementations
61724 ** might choose to implement the wal-index using a mmapped file. Because
61725 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
61729 ** In the default unix and windows implementation, the wal-index is a mmapped
61730 ** file whose name is the database name with a "-shm" suffix added. For that
61731 ** reason, the wal-index is sometimes called the "shm" file.
61733 ** The wal-index is transient. After a crash, the wal-index can (and should
61735 ** to either truncate or zero the header of the wal-index when the last
61736 ** connection to it closes. Because the wal-index is transient, it can
61737 ** use an architecture-specific format; it does not have to be cross-platform.
61739 ** as big endian, the wal-index can store multi-byte values in the native
61742 ** The purpose of the wal-index is to answer this question quickly: Given
61747 ** The wal-index consists of a header region, followed by an one or
61750 ** The wal-index header contains the total number of frames within the WAL
61756 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
61758 ** wal-index. The values are:
61763 ** Each index block contains two sections, a page-mapping that contains the
61764 ** database page number associated with each wal frame, and a hash-table
61766 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
61767 ** for the first index block) 32-bit page numbers. The first entry in the
61768 ** first index-block contains the database page number corresponding to the
61773 ** The last index block in a wal-index usually contains less than the full
61774 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
61776 ** allocated size of the page-mapping array - the page-mapping array merely
61780 ** can be found by scanning the page-mapping sections of each index block
61786 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
61791 ** 1-based index of an entry in the mapping section of the same
61792 ** index block. Let K be the 1-based index of the largest entry in
61807 ** wrap-around.) Because the hash table is never more than half full,
61812 ** current index block. Otherwise the iMax-th mapping entry of the
61830 ** and to the wal-index) might be using a different value K1, where K1>K0.
61836 ** in the first place - which is what reader one wants. Meanwhile, the
61859 ** The maximum (and only) versions of the wal and wal-index formats
61863 ** values in the wal-header are correct and (b) the version field is not
61866 ** Similarly, if a client successfully reads a wal-index header (i.e. the
61868 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
61883 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
61893 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
61903 ** The following object holds a copy of the wal-index header content.
61905 ** The actual header in the wal-index consists of two copies of this
61912 ** Or it can be 1 to represent a 65536-byte page. The latter case was
61916 u32 iVersion; /* Wal-index version */
61920 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
61930 ** A copy of the following object occurs in the wal-index immediately
61953 ** holds read-lock K, then the value in aReadMark[K] is no greater than
61956 ** a special case; its value is never used and it exists as a place-holder
61985 ** We assume that 32-bit loads are atomic and so no locks are needed in
61998 ** This is a schematic view of the complete 136-byte header of the
61999 ** wal-index file (also known as the -shm file):
62001 ** +-----------------------------+
62003 ** +-----------------------------+ |
62005 ** +-----------------------------+ |
62007 ** +-------+-------+-------------+ |
62009 ** +-------+-------+-------------+ |
62011 ** +-----------------------------+ | WalIndexHdr object
62013 ** +-----------------------------+ |
62016 ** +-----------------------------+ |
62019 ** +-----------------------------+ |
62022 ** +-----------------------------+
62024 ** +-----------------------------+ |
62026 ** +-----------------------------+ |
62028 ** +-------+-------+-------------+ |
62030 ** +-------+-------+-------------+ | Second copy of the
62032 ** +-----------------------------+ |
62034 ** +-----------------------------+ |
62037 ** +-----------------------------+ |
62040 ** +-----------------------------+ |
62043 ** +-----------------------------+
62045 ** +-----------------------------+
62051 ** +-------+-------+------+------+
62053 ** +-------+-------+------+------+ ) 8 lock bytes
62055 ** +-------+-------+------+------+
62057 ** +-----------------------------+
62059 ** +-----------------------------+
62064 ** only support mandatory file-locks, we do not read or write data
62077 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
62078 ** big-endian format in the first 4 bytes of a WAL file.
62081 ** file are calculated by treating all data as an array of 32-bit
62082 ** big-endian words. Otherwise, they are calculated by interpreting
62083 ** all data as 32-bit little-endian words.
62088 ** Return the offset of frame iFrame in the write-ahead log file,
62090 ** is to the start of the write-ahead log frame-header.
62093 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
62097 ** An open write-ahead log file is represented by an instance of the
62108 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
62110 i16 readLock; /* Which read lock is being held. -1 for none */
62112 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
62119 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
62120 WalIndexHdr hdr; /* Wal-index header for current transaction */
62124 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
62151 ** Each page of the wal-index mapping contains a hash-table made up of
62165 ** walIteratorInit() - Create a new iterator,
62166 ** walIteratorNext() - Step an iterator,
62167 ** walIteratorFree() - Free an iterator.
62180 } aSegment[1]; /* One for every 32KB page in the wal-index */
62184 ** Define the parameters of the hash tables in the wal-index file. There
62185 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
62186 ** wal-index.
62188 ** Changing any of these constants will alter the wal-index format and
62196 ** The block of page numbers associated with the first hash-table in a
62197 ** wal-index is smaller than usual. This is so that there is a complete
62198 ** hash-table on each aligned 32KB page of the wal-index.
62200 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
62202 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
62208 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
62209 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
62212 ** If the wal-index is currently smaller the iPage pages then the size
62213 ** of the wal-index might be increased, but only if it is safe to do
62214 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
62215 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
62219 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
62223 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
62232 /* Enlarge the pWal->apWiData[] array if required */
62233 if( pWal->nWiData<=iPage ){
62236 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
62241 memset((void*)&apNew[pWal->nWiData], 0,
62242 sizeof(u32*)*(iPage+1-pWal->nWiData));
62243 pWal->apWiData = apNew;
62244 pWal->nWiData = iPage+1;
62248 assert( pWal->apWiData[iPage]==0 );
62249 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
62250 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
62251 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
62253 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
62254 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
62256 assert( pWal->apWiData[iPage]!=0
62258 || (pWal->writeLock==0 && iPage==0) );
62259 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
62263 pWal->readOnly |= WAL_SHM_RDONLY;
62270 *ppPage = pWal->apWiData[iPage];
62279 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
62286 ** Return a pointer to the WalCkptInfo structure in the wal-index.
62289 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62290 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
62294 ** Return a pointer to the WalIndexHdr structure in the wal-index.
62297 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62298 return (volatile WalIndexHdr*)pWal->apWiData[0];
62302 ** The argument to this macro must be of type u32. On a little-endian
62304 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
62306 ** of the input value as a little-endian integer.
62323 int nativeCksum, /* True for native byte-order, false for non-native */
62366 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
62367 sqlite3OsShmBarrier(pWal->pDbFd);
62372 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
62374 ** might give false-positive TSAN warnings.
62376 ** See tag-20200519-1.
62385 ** Write the header information in pWal->hdr into the wal-index.
62387 ** The checksum on pWal->hdr is updated before it is written.
62393 assert( pWal->writeLock );
62394 pWal->hdr.isInit = 1;
62395 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
62396 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
62397 /* Possible TSAN false-positive. See tag-20200519-1 */
62398 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62400 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62405 ** supplied by the caller. A frame-header is made up of a series of
62406 ** 4-byte big-endian integers, as follows:
62411 ** 8: Salt-1 (copied from the wal-header)
62412 ** 12: Salt-2 (copied from the wal-header)
62413 ** 16: Checksum-1.
62414 ** 20: Checksum-2.
62417 Wal *pWal, /* The write-ahead log */
62419 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
62423 int nativeCksum; /* True for native byte-order checksums */
62424 u32 *aCksum = pWal->hdr.aFrameCksum;
62428 if( pWal->iReCksum==0 ){
62429 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
62431 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
62433 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62448 Wal *pWal, /* The write-ahead log */
62454 int nativeCksum; /* True for native byte-order checksums */
62455 u32 *aCksum = pWal->hdr.aFrameCksum;
62459 /* A frame is only valid if the salt values in the frame-header
62460 ** match the salt values in the wal-header.
62462 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
62474 ** all prior frams, the first 16 bytes of this frame-header,
62475 ** and the frame-data matches the checksum in the last 8
62476 ** bytes of this frame-header.
62478 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
62480 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62504 return "WRITE-LOCK";
62506 return "CKPT-LOCK";
62508 return "RECOVER-LOCK";
62511 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
62512 lockIdx-WAL_READ_LOCK(0));
62521 ** A lock cannot be moved directly between shared and exclusive - it must go
62524 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
62528 if( pWal->exclusiveMode ) return SQLITE_OK;
62529 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
62531 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
62533 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
62537 if( pWal->exclusiveMode ) return;
62538 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
62540 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
62544 if( pWal->exclusiveMode ) return SQLITE_OK;
62545 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62547 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
62549 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
62553 if( pWal->exclusiveMode ) return;
62554 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62556 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
62562 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
62567 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
62568 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
62571 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
62576 ** of a page hash table in the wal-index. This becomes the return value
62581 volatile ht_slot *aHash; /* Start of the wal-index hash table */
62588 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
62591 ** Set output variable pLoc->aHash to point to the start of the hash table
62592 ** in the wal-index file. Set pLoc->iZero to one less than the frame
62595 ** (pLoc->iZero+N) in the log.
62597 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
62598 ** first frame indexed by the hash table, frame (pLoc->iZero).
62607 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
62610 if( pLoc->aPgno ){
62611 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
62613 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
62614 pLoc->iZero = 0;
62616 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
62625 ** Return the number of the wal-index page that contains the hash-table
62626 ** and page-number array that contain entries corresponding to WAL frame
62627 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
62631 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
62648 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
62650 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
62655 ** than pWal->hdr.mxFrame.
62657 ** This function is called whenever pWal->hdr.mxFrame is decreased due
62660 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
62662 ** pWal->hdr.mxFrame advances to the point where those hash tables are
62671 assert( pWal->writeLock );
62672 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
62673 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
62674 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
62676 if( pWal->hdr.mxFrame==0 ) return;
62678 /* Obtain pointers to the hash-table and page-number array containing
62679 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
62680 ** that the page said hash-table and array reside on is already mapped.(1)
62682 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
62683 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
62684 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
62685 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
62687 /* Zero all hash-table entries that correspond to frame numbers greater
62688 ** than pWal->hdr.mxFrame.
62690 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
62699 ** frame numbers greater than pWal->hdr.mxFrame.
62701 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
62724 ** Set an entry in the wal-index that will map database page number
62729 WalHashLoc sLoc; /* Wal-index hash table location */
62733 /* Assuming the wal-index file was successfully mapped, populate the
62738 int idx; /* Value to write to hash-table slot */
62741 idx = iFrame - sLoc.iZero;
62744 /* If this is the first entry to be added to this hash-table, zero the
62748 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
62757 ** the hash-table before writing any new entries.
62759 if( sLoc.aPgno[idx-1] ){
62761 assert( !sLoc.aPgno[idx-1] );
62764 /* Write the aPgno[] array entry and the hash-table slot. */
62767 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
62769 sLoc.aPgno[idx-1] = iPage;
62785 ** thing to check, so only do this occasionally - not on every
62807 ** Recover the wal-index by reading the write-ahead log file.
62810 ** wal-index to prevent other threads/processes from doing anything
62811 ** with the WAL or wal-index while recovery is running. The
62828 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
62831 assert( pWal->writeLock );
62832 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
62833 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
62840 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
62842 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
62849 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
62857 u32 iPg; /* Current 32KB wal-index page */
62861 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
62874 || szPage&(szPage-1)
62880 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
62881 pWal->szPage = szPage;
62882 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
62883 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
62886 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
62887 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
62889 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
62890 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
62914 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
62919 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
62924 pWal->apWiData[iPg] = aPrivate;
62932 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
62939 /* If nTruncate is non-zero, this is a commit record. */
62941 pWal->hdr.mxFrame = iFrame;
62942 pWal->hdr.nPage = nTruncate;
62943 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
62946 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
62947 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
62950 pWal->apWiData[iPg] = aShare;
62962 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
62991 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
62992 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
62995 /* Reset the checkpoint-header. This is safe because this thread is
62997 ** checkpointers. Then set the values of read-mark slots 1 through N.
63000 pInfo->nBackfill = 0;
63001 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
63002 pInfo->aReadMark[0] = 0;
63006 if( i==1 && pWal->hdr.mxFrame ){
63007 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
63009 pInfo->aReadMark[i] = READMARK_NOT_USED;
63022 if( pWal->hdr.nPage ){
63025 pWal->hdr.mxFrame, pWal->zWalName
63032 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
63037 ** Close an open wal-index.
63040 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
63042 for(i=0; i<pWal->nWiData; i++){
63043 sqlite3_free((void *)pWal->apWiData[i]);
63044 pWal->apWiData[i] = 0;
63047 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
63048 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
63059 ** client from unlinking the WAL or wal-index file. If another process
63068 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
63071 int bNoShm, /* True to run in heap-memory mode */
63083 ** of these constants would result in an incompatible on-disk format
63084 ** for the -shm file. Any change that causes one of these asserts to
63088 ** This table also serves as a helpful cross-reference when trying to
63089 ** interpret hex dumps of the -shm file.
63129 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
63134 pRet->pVfs = pVfs;
63135 pRet->pWalFd = (sqlite3_file *)&pRet[1];
63136 pRet->pDbFd = pDbFd;
63137 pRet->readLock = -1;
63138 pRet->mxWalSize = mxWalSize;
63139 pRet->zWalName = zWalName;
63140 pRet->syncHeader = 1;
63141 pRet->padToSectorBoundary = 1;
63142 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
63144 /* Open file handle on the write-ahead log file. */
63146 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
63148 pRet->readOnly = WAL_RDONLY;
63153 sqlite3OsClose(pRet->pWalFd);
63157 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
63159 pRet->padToSectorBoundary = 0;
63171 if( pWal ) pWal->mxWalSize = iLimit;
63193 iMin = p->iPrior;
63195 for(i=p->nSegment-1; i>=0; i--){
63196 struct WalSegment *pSegment = &p->aSegment[i];
63197 while( pSegment->iNext<pSegment->nEntry ){
63198 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
63202 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
63206 pSegment->iNext++;
63210 *piPage = p->iPrior = iRet;
63238 const u32 *aContent, /* Pages in wal - keys for the sort */
63302 ht_slot *aList; /* Pointer to sub-list content */
63310 struct Sublist aSub[13]; /* Array of sub-lists */
63314 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
63323 assert( p->aList && p->nList<=(1<<iSub) );
63324 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
63325 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63336 assert( p->nList<=(1<<iSub) );
63337 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
63338 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63348 assert( aContent[aList[i]] > aContent[aList[i-1]] );
63364 ** nBackfill or earlier may be included - excluding them is an optimization
63380 ht_slot *aTmp; /* Temp space used by merge-sort */
63386 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
63387 iLast = pWal->hdr.mxFrame;
63392 + (nSegment-1)*sizeof(struct WalSegment)
63399 p->nSegment = nSegment;
63401 /* Allocate temporary space used by the merge-sort routine. This block
63421 nEntry = (int)(iLast - sLoc.iZero);
63423 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
63425 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
63432 p->aSegment[i].iZero = sLoc.iZero;
63433 p->aSegment[i].nEntry = nEntry;
63434 p->aSegment[i].aIndex = aIndex;
63435 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
63452 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
63457 if( pWal->db ){
63458 int tmout = pWal->db->busyTimeout;
63462 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
63475 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
63489 assert( pWal->readLock<0 || bLock==0 );
63491 assert( pWal->db );
63495 pWal->writeLock = 1;
63499 }else if( pWal->writeLock ){
63501 pWal->writeLock = 0;
63510 pWal->db = db;
63534 ** busy-handler function. Invoke it and retry the lock until either the
63535 ** lock is successfully obtained or the busy-handler returns 0.
63558 ** The cache of the wal-index header must be valid to call this function.
63559 ** Return the page-size in bytes used by the database.
63562 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63571 ** file - there are no readers that may attempt to read a frame from
63574 ** This function updates the shared-memory structures so that the next
63579 ** new wal-index header. It should be passed a pseudo-random value (i.e.
63585 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
63586 pWal->nCkpt++;
63587 pWal->hdr.mxFrame = 0;
63589 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
63591 AtomicStore(&pInfo->nBackfill, 0);
63592 pInfo->nBackfillAttempted = 0;
63593 pInfo->aReadMark[1] = 0;
63594 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
63595 assert( pInfo->aReadMark[0]==0 );
63607 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
63613 ** in the WAL and can be recovered following a power-loss or hard reset.
63620 ** This routine uses and updates the nBackfill field of the wal-index header.
63639 int szPage; /* Database page-size */
63652 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63654 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63663 mxSafeFrame = pWal->hdr.mxFrame;
63664 mxPage = pWal->hdr.nPage;
63666 u32 y = AtomicLoad(pInfo->aReadMark+i);
63668 assert( y<=pWal->hdr.mxFrame );
63672 AtomicStore(pInfo->aReadMark+i, iMark);
63684 if( pInfo->nBackfill<mxSafeFrame ){
63685 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
63692 u32 nBackfill = pInfo->nBackfill;
63694 pInfo->nBackfillAttempted = mxSafeFrame;
63697 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
63705 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
63706 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
63708 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
63711 ** maximum size of the pending-byte page (65536 bytes), then
63715 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
63725 if( AtomicLoad(&db->u1.isInterrupted) ){
63726 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
63734 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
63736 iOffset = (iDbpage-1)*(i64)szPage;
63738 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
63741 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
63745 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
63746 i64 szDb = pWal->hdr.nPage*(i64)szPage;
63748 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
63750 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
63754 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
63775 assert( pWal->writeLock );
63776 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63781 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
63782 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
63785 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
63791 ** wal-index header in shared memory, as all subsequent reader or
63795 ** the wal-index header do not match the contents of the
63796 ** file-system. To avoid this, update the wal-index header to
63799 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
63801 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63819 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
63821 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
63825 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
63841 int isDelete = 0; /* True to unlink wal and wal-index files */
63844 ** ordinary, rollback-mode locking methods, this guarantees that the
63847 ** the wal and wal-index files.
63852 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
63854 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
63855 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63861 int bPersist = -1;
63863 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
63867 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
63870 }else if( pWal->mxWalSize>=0 ){
63874 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
63883 sqlite3OsClose(pWal->pWalFd);
63886 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
63890 sqlite3_free((void *)pWal->apWiData);
63897 ** Try to read the wal-index header. Return 0 on success and 1 if
63900 ** The wal-index is in shared memory. Another thread or process might
63907 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
63910 ** If the checksum cannot be verified return non-zero. If the header
63918 /* The first page of the wal-index must be mapped at this point. */
63919 assert( pWal->nWiData>0 && pWal->apWiData[0] );
63924 ** from the file. If this happens, return non-zero.
63926 ** tag-20200519-1:
63927 ** There are two copies of the header at the beginning of the wal-index.
63931 ** give false-positive warnings about these accesses because the tools do not
63932 ** account for the double-read and the memory barrier. The use of mutexes
63938 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
63946 return 1; /* Malformed header - probably all zeros */
63948 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
63953 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
63955 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
63956 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63957 testcase( pWal->szPage<=32768 );
63958 testcase( pWal->szPage>=65536 );
63969 #define WAL_RETRY (-1)
63972 ** Read the wal-index header from the wal-index and into pWal->hdr.
63973 ** If the wal-header appears to be corrupt, try to reconstruct the
63974 ** wal-index from the WAL before returning.
63976 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
63977 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
63980 ** If the wal-index header is successfully read, return SQLITE_OK.
63986 volatile u32 *page0; /* Chunk of wal-index containing header */
63988 /* Ensure that page 0 of the wal-index (the page that contains the
63989 ** wal-index header) is mapped. Return early if an error occurs here.
63996 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
63998 ** confirm that another write-capable connection has the shared-memory
63999 ** open, and hence the content of the shared-memory is unreliable,
64000 ** since the shared-memory might be inconsistent with the WAL file
64003 assert( pWal->writeLock==0 );
64004 assert( pWal->readOnly & WAL_SHM_RDONLY );
64005 pWal->bShmUnreliable = 1;
64006 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
64009 return rc; /* Any other non-OK return is just an error */
64012 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
64016 assert( page0!=0 || pWal->writeLock==0 );
64018 /* If the first page of the wal-index has been mapped, try to read the
64019 ** wal-index header immediately, without holding any lock. This usually
64020 ** works, but may fail if the wal-index header is corrupt or currently
64029 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
64035 int bWriteLock = pWal->writeLock;
64037 pWal->writeLock = 1;
64041 /* If the wal-index header is still malformed even while holding
64050 pWal->writeLock = 0;
64058 ** sure the wal-index was not constructed with some future format that
64061 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
64064 if( pWal->bShmUnreliable ){
64067 pWal->bShmUnreliable = 0;
64068 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
64074 pWal->exclusiveMode = WAL_NORMAL_MODE;
64081 ** Open a transaction in a connection where the shared-memory is read-only
64082 ** and where we cannot verify that there is a separate write-capable connection
64083 ** on hand to keep the shared-memory up-to-date with the WAL file.
64085 ** This can happen, for example, when the shared-memory is implemented by
64086 ** memory-mapping a *-shm file, where a prior writer has shut down and
64087 ** left the *-shm file on disk, and now the present connection is trying
64088 ** to use that database but lacks write permission on the *-shm file.
64093 ** The *-wal file has been read and an appropriate wal-index has been
64094 ** constructed in pWal->apWiData[] using heap memory instead of shared
64101 ** WAL_RETRY, then the heap memory wal-index has been discarded and
64103 ** beginning (including attempting to map the *-shm file).
64116 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
64118 assert( pWal->bShmUnreliable );
64119 assert( pWal->readOnly & WAL_SHM_RDONLY );
64120 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64130 pWal->readLock = 0;
64132 /* Check to see if a separate writer has attached to the shared-memory area,
64133 ** thus making the shared-memory "reliable" again. Do this by invoking
64137 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
64138 ** cause the heap-memory WAL-index to be discarded and the actual
64150 ** even if some external agent does a "chmod" to make the shared-memory
64154 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
64155 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
64161 /* We reach this point only if the real shared-memory is still unreliable.
64162 ** Assume the in-memory WAL-index substitute is correct and load it
64163 ** into pWal->hdr.
64165 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
64170 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
64175 /* If the wal file is too small to contain a wal-header and the
64176 ** wal-index header has mxFrame==0, then it must be safe to proceed
64182 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
64187 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
64191 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
64193 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
64200 assert( (pWal->szPage & (pWal->szPage-1))==0 );
64201 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
64202 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
64211 ** wal file since the heap-memory wal-index was created. If so, the
64212 ** heap-memory wal-index is discarded and WAL_RETRY returned to
64214 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
64215 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
64216 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
64224 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64228 /* If nTruncate is non-zero, then a complete transaction has been
64236 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
64237 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
64243 for(i=0; i<pWal->nWiData; i++){
64244 sqlite3_free((void*)pWal->apWiData[i]);
64245 pWal->apWiData[i] = 0;
64247 pWal->bShmUnreliable = 0;
64266 ** to make a copy of the wal-index header into pWal->hdr. If the
64267 ** wal-index header has changed, *pChanged is set to 1 (as an indication
64269 ** flushed.) When useWal==1, the wal-index header is assumed to already
64280 ** bad luck when there is lots of contention for the wal-index, but that
64284 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
64285 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
64288 ** including frame number aReadMark[pWal->readLock]. The reader will
64289 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
64290 ** Or if pWal->readLock==0, then the reader will ignore the WAL
64293 ** this routine will always set pWal->readLock>0 on success.
64295 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
64305 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
64312 assert( pWal->readLock<0 ); /* Not currently locked */
64315 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
64323 ** paged out or take a page-fault that is time-consuming to resolve,
64337 VVA_ONLY( pWal->lockError = 1; )
64340 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
64341 sqlite3OsSleep(pWal->pVfs, nDelay);
64346 if( pWal->bShmUnreliable==0 ){
64358 if( pWal->apWiData[0]==0 ){
64363 ** code that determines whether or not the shared-memory region
64377 else if( pWal->bShmUnreliable ){
64382 assert( pWal->nWiData>0 );
64383 assert( pWal->apWiData[0]!=0 );
64385 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
64387 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
64396 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
64413 pWal->readLock = 0;
64423 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
64427 mxFrame = pWal->hdr.mxFrame;
64429 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
64430 mxFrame = pWal->pSnapshot->mxFrame;
64434 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
64441 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
64447 AtomicStore(pInfo->aReadMark+i,mxFrame);
64458 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
64466 /* Now that the read-lock has been obtained, check that neither the
64467 ** value in the aReadMark[] array or the contents of the wal-index
64470 ** It is necessary to check that the wal-index header did not change
64471 ** between the time it was read and when the shared-lock was obtained
64474 ** that occur later in the log than pWal->hdr.mxFrame may have been
64477 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
64480 ** Before checking that the live wal-index header has not changed
64483 ** to read any frames earlier than minFrame from the wal file - they
64487 ** nBackfill and checking that the wal-header in shared-memory still
64488 ** matches the one cached in pWal->hdr, it is guaranteed that the
64489 ** checkpointer that set nBackfill was not working with a wal-index
64490 ** header newer than that cached in pWal->hdr. If it were, that could
64492 ** a version of page X that lies before pWal->minFrame (call that version
64495 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
64498 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
64500 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
64502 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
64503 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
64508 assert( mxReadMark<=pWal->hdr.mxFrame );
64509 pWal->readLock = (i16)mxI;
64523 ** This is only really safe if the file-system is such that any page
64537 assert( pWal->readLock>=0 );
64541 int szPage = (int)pWal->szPage;
64544 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
64551 u32 i = pInfo->nBackfillAttempted;
64552 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
64560 assert( i - sLoc.iZero - 1 >=0 );
64561 pgno = sLoc.aPgno[i-sLoc.iZero-1];
64562 iDbOff = (i64)(pgno-1) * szPage;
64566 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
64569 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
64577 pInfo->nBackfillAttempted = i-1;
64595 ** it takes a snapshot of the state of the WAL and wal-index for the current
64597 ** Other threads might append new content to the WAL and wal-index but
64602 ** Pager layer will use this to know that its cache is stale and
64610 WalIndexHdr *pSnapshot = pWal->pSnapshot;
64613 assert( pWal->ckptLock==0 );
64617 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64625 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
64628 ** before checking pInfo->nBackfillAttempted. */
64636 pWal->ckptLock = 1;
64650 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64652 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
64653 ** is populated with the wal-index header corresponding to the head
64661 ** pSnapshot->mxFrame into the database file. Note that the
64666 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
64667 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
64671 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
64672 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
64675 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
64676 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
64678 assert( pWal->readLock>0 );
64679 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
64685 /* A client using a non-current snapshot may not ignore any frames
64691 pWal->minFrame = 1;
64700 if( pWal->ckptLock ){
64703 pWal->ckptLock = 0;
64711 ** read-lock.
64715 if( pWal->readLock>=0 ){
64716 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
64717 pWal->readLock = -1;
64735 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
64740 assert( pWal->readLock>=0 || pWal->lockError );
64742 /* If the "last page" field of the wal-index header snapshot is 0, then
64744 ** in this case as an optimization. Likewise, if pWal->readLock==0,
64748 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
64758 ** that adds entries to the wal-index (and possibly to this hash
64762 ** read transaction was opened may have been written incorrectly -
64769 ** if we had exclusive access to the hash-table:
64772 ** This condition filters out normal hash-table collisions.
64776 ** table after the current read-transaction had started.
64778 iMinHash = walFramePage(pWal->minFrame);
64779 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
64794 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
64798 if( (nCollide--)==0 ){
64808 ** of the wal-index file content. Make sure the results agree with the
64813 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
64814 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
64841 sz = pWal->hdr.szPage;
64847 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
64854 if( pWal && ALWAYS(pWal->readLock>=0) ){
64855 return pWal->hdr.nPage;
64878 /* If the write-lock is already held, then it was obtained before the
64879 ** read-transaction was even opened, making this call a no-op.
64881 if( pWal->writeLock ){
64882 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
64889 assert( pWal->readLock>=0 );
64890 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
64892 if( pWal->readOnly ){
64903 pWal->writeLock = 1;
64909 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
64911 pWal->writeLock = 0;
64923 if( pWal->writeLock ){
64925 pWal->writeLock = 0;
64926 pWal->iReCksum = 0;
64927 pWal->truncateOnCommit = 0;
64934 ** function moves the write-pointer back to the start of the transaction.
64946 if( ALWAYS(pWal->writeLock) ){
64947 Pgno iMax = pWal->hdr.mxFrame;
64950 /* Restore the clients cache of the wal-index header to the state it
64953 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
64955 for(iFrame=pWal->hdr.mxFrame+1;
64961 ** (b) has an outstanding reference, then xUndo is either a no-op
64973 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
64985 assert( pWal->writeLock );
64986 aWalData[0] = pWal->hdr.mxFrame;
64987 aWalData[1] = pWal->hdr.aFrameCksum[0];
64988 aWalData[2] = pWal->hdr.aFrameCksum[1];
64989 aWalData[3] = pWal->nCkpt;
65001 assert( pWal->writeLock );
65002 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
65004 if( aWalData[3]!=pWal->nCkpt ){
65005 /* This savepoint was opened immediately after the write-transaction
65010 aWalData[3] = pWal->nCkpt;
65013 if( aWalData[0]<pWal->hdr.mxFrame ){
65014 pWal->hdr.mxFrame = aWalData[0];
65015 pWal->hdr.aFrameCksum[0] = aWalData[1];
65016 pWal->hdr.aFrameCksum[1] = aWalData[2];
65028 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
65032 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
65039 if( pWal->readLock==0 ){
65041 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
65042 if( pInfo->nBackfill>0 ){
65045 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65050 ** wal-index header to reflect this.
65053 ** at this point. But updating the actual wal-index header is also
65057 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65063 pWal->readLock = -1;
65079 ** the next fsync should occur - passed from sqlite3WalFrames() into
65092 ** Do a sync when crossing the p->iSyncPoint boundary.
65105 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
65106 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
65107 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
65110 iAmt -= iFirstAmt;
65112 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
65113 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
65116 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
65131 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
65132 pData = pPage->pData;
65133 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
65137 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
65150 const int szPage = pWal->szPage;/* Database page size */
65153 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
65163 ** wal-file header. Otherwise, read them from the frame header of the
65165 assert( pWal->iReCksum>0 );
65166 if( pWal->iReCksum==1 ){
65169 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
65171 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
65172 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
65173 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
65175 iRead = pWal->iReCksum;
65176 pWal->iReCksum = 0;
65179 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
65186 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
65195 ** Write a set of frames to the log. The caller must hold the write-lock
65200 int szPage, /* Database page-size in bytes */
65218 assert( pWal->writeLock );
65225 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
65227 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
65232 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
65233 iFirst = pLive->mxFrame+1;
65237 ** log file, instead of appending to it at pWal->hdr.mxFrame.
65247 iFrame = pWal->hdr.mxFrame;
65249 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
65250 u32 aCksum[2]; /* Checksum for wal-header */
65255 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
65256 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
65257 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
65258 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
65262 pWal->szPage = szPage;
65263 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
65264 pWal->hdr.aFrameCksum[0] = aCksum[0];
65265 pWal->hdr.aFrameCksum[1] = aCksum[1];
65266 pWal->truncateOnCommit = 1;
65268 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
65269 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
65276 ** an out-of-order write following a WAL restart could result in
65281 if( pWal->syncHeader ){
65282 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
65286 assert( (int)pWal->szPage==szPage );
65290 w.pFd = pWal->pWalFd;
65298 for(p=pList; p; p=p->pDirty){
65303 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
65305 if( iFirst && (p->pDirty || isCommit==0) ){
65307 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
65312 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
65313 pWal->iReCksum = iWrite;
65315 pData = p->pData;
65316 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
65318 p->flags &= ~PGHDR_WAL_APPEND;
65325 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
65330 p->flags |= PGHDR_WAL_APPEND;
65334 if( isCommit && pWal->iReCksum ){
65355 if( pWal->padToSectorBoundary ){
65356 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
65357 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
65378 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
65379 i64 sz = pWal->mxWalSize;
65380 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
65384 pWal->truncateOnCommit = 0;
65387 /* Append data to the wal-index. It is not necessary to lock the
65388 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
65392 iFrame = pWal->hdr.mxFrame;
65393 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
65394 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
65396 rc = walIndexAppend(pWal, iFrame, p->pgno);
65401 nExtra--;
65402 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
65407 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
65410 pWal->hdr.mxFrame = iFrame;
65412 pWal->hdr.iChange++;
65413 pWal->hdr.nPage = nTruncate;
65415 /* If this is a commit, update the wal-index header too. */
65418 pWal->iCallback = iFrame;
65433 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
65449 int isChanged = 0; /* True if a new wal-index header is loaded */
65453 assert( pWal->ckptLock==0 );
65454 assert( pWal->writeLock==0 );
65456 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65460 if( pWal->readOnly ) return SQLITE_READONLY;
65464 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
65468 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
65470 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
65473 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
65480 pWal->ckptLock = 1;
65482 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
65486 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
65487 ** immediately, and a busy-handler is configured, it is invoked and the
65488 ** writer lock retried until either the busy-handler returns 0 or the
65494 pWal->writeLock = 1;
65504 /* Read the wal-index header. */
65509 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
65510 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
65517 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
65525 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
65526 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
65531 /* If a new wal-index header was loaded before the checkpoint was
65532 ** performed, then the pager-cache associated with pWal is now
65533 ** out of date. So zero the cached wal-index header to ensure that
65537 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
65545 if( pWal->ckptLock ){
65547 pWal->ckptLock = 0;
65564 ret = pWal->iCallback;
65565 pWal->iCallback = 0;
65576 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
65578 ** transition out of exclusive-mode is successful, return 1. This
65583 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
65585 ** WAL is already in exclusive-locking mode - meaning that this
65586 ** routine is a no-op. The pager must already hold the exclusive lock
65589 ** If op is negative, then do a dry-run of the op==1 case but do
65596 assert( pWal->writeLock==0 );
65597 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
65599 /* pWal->readLock is usually set, but might be -1 if there was a
65600 ** prior error while attempting to acquire are read-lock. This cannot
65603 ** upgrade to exclusive-mode following such an error.
65605 assert( pWal->readLock>=0 || pWal->lockError );
65606 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
65609 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
65610 pWal->exclusiveMode = WAL_NORMAL_MODE;
65611 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
65612 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65614 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
65620 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
65621 assert( pWal->readLock>=0 );
65622 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
65623 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65626 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
65632 ** Return true if the argument is non-NULL and the WAL module is using
65633 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
65634 ** WAL module is using shared-memory, return false.
65637 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
65650 assert( pWal->readLock>=0 && pWal->writeLock==0 );
65652 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
65660 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
65667 /* Try to open on pSnapshot when the next read-transaction starts
65673 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
65677 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
65686 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
65687 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
65688 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
65689 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
65709 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
65710 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
65734 ** read-lock. This function returns the database page-size if it is known,
65738 assert( pWal==0 || pWal->readLock>=0 );
65739 return (pWal ? pWal->szPage : 0);
65746 return pWal->pWalFd;
65783 ** This file implements an external (disk-based) database using BTrees.
65787 ** "Sorting And Searching", pages 473-480. Addison-Wesley
65793 ** ----------------------------------------------------------------
65794 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
65795 ** ----------------------------------------------------------------
65800 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
65822 ** page, or a pointer-map page.
65841 ** 40 60 15 4-byte meta values passed to higher layers
65846 ** 52 4 Largest root-page (auto/incr_vacuum)
65847 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
65850 ** 68 4 Application-ID
65852 ** 92 4 The version-valid-for number
65855 ** All of the integer values are big-endian (most significant byte first).
65863 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
65878 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
65881 ** |----------------|
65883 ** |----------------|
65885 ** |----------------|
65889 ** |----------------|
65892 ** |----------------| ^ Grows upwards
65895 ** |----------------|
65914 ** The cell pointer array contains zero or more 2-byte numbers which are
65945 ** appears first. A variable-length integer may not be more than 9 bytes long.
65947 ** allows a 64-bit integer to be encoded in 9 bytes.
65970 ** filled with data (pagesize - 4 bytes). The last page can have as little
65993 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
66000 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
66011 ** You can change this value at compile-time by specifying a
66012 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
66013 ** header must be exactly 16 bytes including the zero-terminator so
66025 ** first byte of on-disk image of every BTree page.
66035 ** is derived from the raw on-disk page content.
66042 ** stored in MemPage.pBt->mutex.
66046 u8 intKey; /* True if table b-trees. False for index b-trees */
66059 int nFree; /* Number of free bytes on the page. -1 for unknown */
66062 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
66063 ** non-overflow cell */
66067 u8 *aDataEnd; /* One byte past the end of the entire page - not just
66069 ** corruption-induced buffer overflow. */
66125 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
66139 ** If the shared-data extension is enabled, there may be multiple users
66151 # error wrong numeric code for no-transaction
66154 # error wrong numeric code for read-transaction
66157 # error wrong numeric code for write-transaction
66180 ** If a BtShared client fails to obtain a write-lock on a database
66181 ** table (because there exists one or more read-locks on the table),
66182 ** the shared-cache enters 'pending-lock' state and isPending is
66185 ** The shared-cache leaves the 'pending lock' state when either of
66191 ** while in the 'pending-lock' state, no connection may start a new
66194 ** This feature is included to help prevent writer-starvation.
66203 u8 autoVacuum; /* True if auto-vacuum is enabled */
66204 u8 incrVacuum; /* True if incr-vacuum is enabled */
66208 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
66211 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
66212 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
66221 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
66222 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
66226 BtLock *pLock; /* List of locks held on this shared-btree struct */
66242 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
66244 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
66256 u16 nSize; /* Size of the cell content on the main b-tree page */
66260 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
66263 ** root-node and 3 for all other internal nodes.
66272 ** b-tree within a database file.
66282 ** found at self->pBt->mutex.
66290 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
66291 ** sqlite3BtreePrevious() is no-op if skipNext<0.
66316 u8 curIntKey; /* Value of apPage[0]->intKey */
66318 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
66321 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
66347 ** Cursor is valid except that the Cursor.skipNext field is non-zero
66349 ** operation should be a no-op.
66374 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
66377 ** These macros define the location of the pointer-map entry for a
66382 ** PTRMAP_PAGENO returns the database page number of the pointer-map
66386 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
66388 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
66392 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
66409 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
66412 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
66420 ** overflow pages. The page-number identifies the previous
66423 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
66436 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
66437 assert( p->pBt->inTransaction>=p->inTrans );
66442 ** if the database supports auto-vacuum or not. Because it is used
66448 #define ISAUTOVACUUM (pBt->autoVacuum)
66459 ** the database. As the integrity-check proceeds, for each page used in
66460 ** the database the corresponding bit is set. This allows integrity-check to
66467 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
66477 u32 *heap; /* Min-heap used for analyzing cell coverage */
66482 ** Routines to read or write a two- and four-byte big-endian integer values.
66491 ** two-byte aligned address. get2bytea() is only used for accessing the
66510 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
66512 ** p->locked boolean to true.
66515 assert( p->locked==0 );
66516 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
66517 assert( sqlite3_mutex_held(p->db->mutex) );
66519 sqlite3_mutex_enter(p->pBt->mutex);
66520 p->pBt->db = p->db;
66521 p->locked = 1;
66525 ** Release the BtShared mutex associated with B-Tree handle p and
66526 ** clear the p->locked boolean.
66529 BtShared *pBt = p->pBt;
66530 assert( p->locked==1 );
66531 assert( sqlite3_mutex_held(pBt->mutex) );
66532 assert( sqlite3_mutex_held(p->db->mutex) );
66533 assert( p->db==pBt->db );
66535 sqlite3_mutex_leave(pBt->mutex);
66536 p->locked = 0;
66546 ** and this routine is a no-op. The underlying mutex is non-recursive.
66551 ** by all database connections. The p->pNext is a list of other
66554 ** p, then first unlock all of the others on p->pNext, then wait
66563 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
66564 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
66565 assert( p->pNext==0 || p->pNext->db==p->db );
66566 assert( p->pPrev==0 || p->pPrev->db==p->db );
66567 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
66570 assert( !p->locked || p->wantToLock>0 );
66571 assert( p->sharable || p->wantToLock==0 );
66574 assert( sqlite3_mutex_held(p->db->mutex) );
66578 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
66580 if( !p->sharable ) return;
66581 p->wantToLock++;
66582 if( p->locked ) return;
66599 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
66600 p->pBt->db = p->db;
66601 p->locked = 1;
66610 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66611 assert( pLater->sharable );
66612 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
66613 assert( !pLater->locked || pLater->wantToLock>0 );
66614 if( pLater->locked ){
66619 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66620 if( pLater->wantToLock ){
66631 assert( sqlite3_mutex_held(p->db->mutex) );
66632 if( p->sharable ){
66633 assert( p->wantToLock>0 );
66634 p->wantToLock--;
66635 if( p->wantToLock==0 ){
66644 ** B-Tree is not marked as sharable.
66649 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
66650 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
66651 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
66652 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
66654 return (p->sharable==0 || p->locked);
66666 ** There is a corresponding leave-all procedures.
66677 assert( sqlite3_mutex_held(db->mutex) );
66678 for(i=0; i<db->nDb; i++){
66679 p = db->aDb[i].pBt;
66680 if( p && p->sharable ){
66685 db->noSharedCache = skipOk;
66688 if( db->noSharedCache==0 ) btreeEnterAll(db);
66693 assert( sqlite3_mutex_held(db->mutex) );
66694 for(i=0; i<db->nDb; i++){
66695 p = db->aDb[i].pBt;
66700 if( db->noSharedCache==0 ) btreeLeaveAll(db);
66712 if( !sqlite3_mutex_held(db->mutex) ){
66715 for(i=0; i<db->nDb; i++){
66717 p = db->aDb[i].pBt;
66718 if( p && p->sharable &&
66719 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
66730 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
66734 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
66743 assert( iDb>=0 && iDb<db->nDb );
66744 if( !sqlite3_mutex_held(db->mutex) ) return 0;
66746 p = db->aDb[iDb].pBt;
66748 return p->sharable==0 || p->locked==1;
66756 ** these two routines, all mutex operations are no-ops in that case and
66760 ** the ones below, are no-ops and are null #defines in btree.h.
66764 p->pBt->db = p->db;
66768 for(i=0; i<db->nDb; i++){
66769 Btree *p = db->aDb[i].pBt;
66771 p->pBt->db = p->db;
66786 sqlite3BtreeEnter(pCur->pBtree);
66790 sqlite3BtreeLeave(pCur->pBtree);
66810 ** This file implements an external (disk-based) database using BTrees.
66834 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
66839 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
66842 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
66855 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
66900 ** shared-cache table level locks. If the library is compiled with the
66901 ** shared-cache feature disabled, then there is only ever one user
66903 ** So define the lock related functions as no-ops.
66918 u64 n = pBt->nSeek;
66919 pBt->nSeek = 0;
66930 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
66938 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
66949 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
66961 ** For example, when writing to a table with root-page iRoot via
66974 ** hold a write-lock on the schema table (root page 1). This is also
66979 Pgno iRoot, /* Root page of b-tree */
66980 int isIndex, /* True if iRoot is the root of an index b-tree */
66983 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
66988 ** and has the read-uncommitted flag set, then no lock is required.
66991 if( (pBtree->sharable==0)
66992 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
66999 ** the correct locks are held. So do not bother - just return true.
67002 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
67006 /* Figure out the root-page that the lock should be held on. For table
67007 ** b-trees, this is just the root page of the b-tree being read or
67008 ** written. For index b-trees, it is the root page of the associated
67013 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
67015 if( pIdx->tnum==iRoot ){
67022 iTab = pIdx->pTable->tnum;
67030 /* Search for the required lock. Either a write-lock on root-page iTab, a
67031 ** write-lock on the schema table, or (if the client is reading) a
67032 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
67033 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
67034 if( pLock->pBtree==pBtree
67035 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
67036 && pLock->eLock>=eLockType
67058 ** read-uncommitted flag set, then it is OK for the other object to
67068 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67069 if( p->pgnoRoot==iRoot
67070 && p->pBtree!=pBtree
67071 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
67082 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
67087 BtShared *pBt = p->pBt;
67092 assert( p->db!=0 );
67093 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
67095 /* If requesting a write-lock, then the Btree must have an open write
67099 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
67100 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
67102 /* This routine is a no-op if the shared-cache is not enabled */
67103 if( !p->sharable ){
67110 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
67111 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
67115 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67116 /* The condition (pIter->eLock!=eLock) in the following if(...)
67119 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
67125 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
67126 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
67127 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
67128 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
67130 assert( p==pBt->pWriter );
67131 pBt->btsFlags |= BTS_PENDING;
67142 ** Add a lock on the table with root-page iTable to the shared-btree used
67159 BtShared *pBt = p->pBt;
67165 assert( p->db!=0 );
67167 /* A connection with the read-uncommitted flag set will never try to
67168 ** obtain a read-lock using this function. The only read-lock obtained
67169 ** by a connection in read-uncommitted mode is on the sqlite_schema
67171 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
67173 /* This function should only be called on a sharable b-tree after it
67174 ** has been determined that no other b-tree holds a conflicting lock. */
67175 assert( p->sharable );
67179 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67180 if( pIter->iTable==iTable && pIter->pBtree==p ){
67194 pLock->iTable = iTable;
67195 pLock->pBtree = p;
67196 pLock->pNext = pBt->pLock;
67197 pBt->pLock = pLock;
67201 ** and the requested lock. This means if a write-lock was already held
67202 ** and a read-lock requested, we don't incorrectly downgrade the lock.
67205 if( eLock>pLock->eLock ){
67206 pLock->eLock = eLock;
67223 BtShared *pBt = p->pBt;
67224 BtLock **ppIter = &pBt->pLock;
67227 assert( p->sharable || 0==*ppIter );
67228 assert( p->inTrans>0 );
67232 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
67233 assert( pLock->pBtree->inTrans>=pLock->eLock );
67234 if( pLock->pBtree==p ){
67235 *ppIter = pLock->pNext;
67236 assert( pLock->iTable!=1 || pLock==&p->lock );
67237 if( pLock->iTable!=1 ){
67241 ppIter = &pLock->pNext;
67245 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
67246 if( pBt->pWriter==p ){
67247 pBt->pWriter = 0;
67248 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
67249 }else if( pBt->nTransaction==2 ){
67259 pBt->btsFlags &= ~BTS_PENDING;
67264 ** This function changes all write-locks held by Btree p into read-locks.
67267 BtShared *pBt = p->pBt;
67268 if( pBt->pWriter==p ){
67270 pBt->pWriter = 0;
67271 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
67272 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
67273 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
67274 pLock->eLock = READ_LOCK;
67292 return sqlite3_mutex_held(p->pBt->mutex);
67296 ** database connetion. This is important in shared-cache mode. If the database
67297 ** connection pointers get out-of-sync, it is possible for routines like
67298 ** btreeInitPage() to reference an stale connection pointer that references a
67300 ** statements only and for the purpose of double-checking that the btree code
67301 ** does keep the database connection pointers up-to-date.
67305 return (p->pBtree->db==p->pBt->db);
67313 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
67316 ** Invalidate the overflow page-list cache for all cursors opened
67321 assert( sqlite3_mutex_held(pBt->mutex) );
67322 for(p=pBt->pCursor; p; p=p->pNext){
67335 ** cursors open on any row within the table with root-page pgnoRoot.
67348 assert( pBtree->hasIncrblobCur );
67350 pBtree->hasIncrblobCur = 0;
67351 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67352 if( (p->curFlags & BTCF_Incrblob)!=0 ){
67353 pBtree->hasIncrblobCur = 1;
67354 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
67355 p->eState = CURSOR_INVALID;
67368 ** when a page that previously contained data becomes a free-list leaf
67373 ** free-list leaf pages:
67376 ** a free-list leaf page, the page is not written to the database
67377 ** (as free-list leaf pages contain no meaningful data). Sometimes
67381 ** 2) When a free-list leaf page is reused, its content is not read
67387 ** a page is moved to the free-list and then reused within the same
67389 ** it is moved to the free-list and it is also not journalled when it
67390 ** is extracted from the free-list and reused, then the original data
67395 ** moved to become a free-list leaf page, the corresponding bit is
67396 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
67403 if( !pBt->pHasContent ){
67404 assert( pgno<=pBt->nPage );
67405 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
67406 if( !pBt->pHasContent ){
67410 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
67411 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
67419 ** This function is called when a free-list leaf page is removed from the
67420 ** free-list for reuse. It returns false if it is safe to retrieve the
67421 ** page from the pager layer with the 'no-content' flag set. True otherwise.
67424 Bitvec *p = pBt->pHasContent;
67430 ** invoked at the conclusion of each write-transaction.
67433 sqlite3BitvecDestroy(pBt->pHasContent);
67434 pBt->pHasContent = 0;
67442 if( pCur->iPage>=0 ){
67443 for(i=0; i<pCur->iPage; i++){
67444 releasePageNotNull(pCur->apPage[i]);
67446 releasePageNotNull(pCur->pPage);
67447 pCur->iPage = -1;
67454 ** function saves the current cursor key in variables pCur->nKey and
67455 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
67459 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
67460 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
67461 ** set to point to a malloced buffer pCur->nKey bytes in size containing
67466 assert( CURSOR_VALID==pCur->eState );
67467 assert( 0==pCur->pKey );
67470 if( pCur->curIntKey ){
67472 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
67477 ** up to the size of 1 varint plus 1 8-byte value when the cursor
67481 pCur->nKey = sqlite3BtreePayloadSize(pCur);
67482 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
67484 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
67486 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
67487 pCur->pKey = pKey;
67495 assert( !pCur->curIntKey || !pCur->pKey );
67509 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
67510 assert( 0==pCur->pKey );
67513 if( pCur->curFlags & BTCF_Pinned ){
67516 if( pCur->eState==CURSOR_SKIPNEXT ){
67517 pCur->eState = CURSOR_VALID;
67519 pCur->skipNext = 0;
67525 pCur->eState = CURSOR_REQUIRESEEK;
67528 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
67537 ** the table with root-page iRoot. "Saving the cursor position" means that
67548 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
67558 assert( sqlite3_mutex_held(pBt->mutex) );
67559 assert( pExcept==0 || pExcept->pBt==pBt );
67560 for(p=pBt->pCursor; p; p=p->pNext){
67561 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
67564 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
67579 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
67580 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
67586 testcase( p->iPage>=0 );
67590 p = p->pNext;
67600 sqlite3_free(pCur->pKey);
67601 pCur->pKey = 0;
67602 pCur->eState = CURSOR_INVALID;
67621 KeyInfo *pKeyInfo = pCur->pKeyInfo;
67626 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
67631 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
67650 assert( pCur->eState>=CURSOR_REQUIRESEEK );
67651 if( pCur->eState==CURSOR_FAULT ){
67652 return pCur->skipNext;
67654 pCur->eState = CURSOR_INVALID;
67658 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
67661 sqlite3_free(pCur->pKey);
67662 pCur->pKey = 0;
67663 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
67664 if( skipNext ) pCur->skipNext = skipNext;
67665 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
67666 pCur->eState = CURSOR_SKIPNEXT;
67673 (p->eState>=CURSOR_REQUIRESEEK ? \
67693 assert( sizeof(pCur->eState)==1 );
67725 assert( pCur->eState!=CURSOR_VALID );
67731 if( pCur->eState!=CURSOR_VALID ){
67755 pCur->hints = x;
67762 ** number for the pointer-map page that contains the entry for the
67772 assert( sqlite3_mutex_held(pBt->mutex) );
67774 nPagesPerMapPage = (pBt->usableSize/5)+1;
67775 iPtrMap = (pgno-2)/nPagesPerMapPage;
67789 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
67790 ** a no-op. If an error occurs, the appropriate error code is written
67802 assert( sqlite3_mutex_held(pBt->mutex) );
67803 /* The super-journal page number must never be used as a pointer map page */
67806 assert( pBt->autoVacuum );
67812 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67829 assert( offset <= (int)pBt->usableSize-5 );
67833 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
67859 assert( sqlite3_mutex_held(pBt->mutex) );
67862 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67873 assert( offset <= (int)pBt->usableSize-5 );
67895 ** 4-byte child pointer found on interior pages, if there is one.
67900 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
67902 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
67908 ** on a single B-tree page. Make necessary adjustments to the CellInfo
67929 minLocal = pPage->minLocal;
67930 maxLocal = pPage->maxLocal;
67931 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
67935 pInfo->nLocal = (u16)surplus;
67937 pInfo->nLocal = (u16)minLocal;
67939 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
67948 maxLocal = pPage->maxLocal;
67954 minLocal = pPage->minLocal;
67955 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
67979 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67980 assert( pPage->leaf==0 );
67981 assert( pPage->childPtrSize==4 );
67985 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
67986 pInfo->nPayload = 0;
67987 pInfo->nLocal = 0;
67988 pInfo->pPayload = 0;
68000 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68001 assert( pPage->leaf==0 || pPage->leaf==1 );
68002 assert( pPage->intKeyLeaf );
68003 assert( pPage->childPtrSize==0 );
68024 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
68027 ** This routine is a high-runner.
68057 pInfo->nKey = *(i64*)&iKey;
68058 pInfo->nPayload = nPayload;
68059 pInfo->pPayload = pIter;
68060 testcase( nPayload==pPage->maxLocal );
68061 testcase( nPayload==(u32)pPage->maxLocal+1 );
68062 if( nPayload<=pPage->maxLocal ){
68066 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68067 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68068 pInfo->nLocal = (u16)nPayload;
68081 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68082 assert( pPage->leaf==0 || pPage->leaf==1 );
68083 assert( pPage->intKeyLeaf==0 );
68084 pIter = pCell + pPage->childPtrSize;
68094 pInfo->nKey = nPayload;
68095 pInfo->nPayload = nPayload;
68096 pInfo->pPayload = pIter;
68097 testcase( nPayload==pPage->maxLocal );
68098 testcase( nPayload==(u32)pPage->maxLocal+1 );
68099 if( nPayload<=pPage->maxLocal ){
68103 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68104 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68105 pInfo->nLocal = (u16)nPayload;
68115 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
68123 ** data area of the btree-page. The return number includes the cell
68132 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
68142 pPage->xParseCell(pPage, pCell, &debuginfo);
68154 testcase( nSize==pPage->maxLocal );
68155 testcase( nSize==(u32)pPage->maxLocal+1 );
68156 if( nSize<=pPage->maxLocal ){
68157 nSize += (u32)(pIter - pCell);
68160 int minLocal = pPage->minLocal;
68161 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68162 testcase( nSize==pPage->maxLocal );
68163 testcase( nSize==(u32)pPage->maxLocal+1 );
68164 if( nSize>pPage->maxLocal ){
68167 nSize += 4 + (u16)(pIter - pCell);
68182 pPage->xParseCell(pPage, pCell, &debuginfo);
68187 assert( pPage->childPtrSize==4 );
68190 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
68191 return (u16)(pIter - pCell);
68204 pPage->xParseCell(pPage, pCell, &debuginfo);
68216 /* pIter now points at the 64-bit integer key value, a variable length
68227 testcase( nSize==pPage->maxLocal );
68228 testcase( nSize==(u32)pPage->maxLocal+1 );
68229 if( nSize<=pPage->maxLocal ){
68230 nSize += (u32)(pIter - pCell);
68233 int minLocal = pPage->minLocal;
68234 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68235 testcase( nSize==pPage->maxLocal );
68236 testcase( nSize==(u32)pPage->maxLocal+1 );
68237 if( nSize>pPage->maxLocal ){
68240 nSize += 4 + (u16)(pIter - pCell);
68251 return pPage->xCellSize(pPage, findCell(pPage, iCell));
68259 ** pointer to an overflow page, insert an entry into the pointer-map for
68266 pPage->xParseCell(pPage, pCell, &info);
68269 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
68274 ovfl = get4byte(&pCell[info.nSize-4]);
68275 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
68283 ** page so that there are no free-blocks on the free-block list.
68288 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
68289 ** b-tree page so that there are no freeblocks or fragment bytes, all
68295 int pc; /* Address of the i-th cell */
68309 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68310 assert( pPage->pBt!=0 );
68311 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
68312 assert( pPage->nOverflow==0 );
68313 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68315 src = data = pPage->aData;
68316 hdr = pPage->hdrOffset;
68317 cellOffset = pPage->cellOffset;
68318 nCell = pPage->nCell;
68321 usableSize = pPage->pBt->usableSize;
68326 ** offsets to each pointer in the cell-pointer array than it is to
68330 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
68333 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
68347 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
68354 assert( cbrk+(iFree-top) <= usableSize );
68355 memmove(&data[cbrk], &data[top], iFree-top);
68367 iCellLast = usableSize - 4;
68370 u8 *pAddr; /* The i-th cell pointer */
68382 size = pPage->xCellSize(pPage, &src[pc]);
68383 cbrk -= size;
68393 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68394 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68402 assert( pPage->nFree>=0 );
68403 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
68410 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
68411 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68416 ** Search the free-list on page pPg for space to store a cell nByte bytes in
68418 ** from the free-list.
68420 ** If no suitable space can be found on the free-list, return NULL.
68430 const int hdr = pPg->hdrOffset; /* Offset to page header */
68431 u8 * const aData = pPg->aData; /* Page data */
68436 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
68441 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
68442 ** freeblock form a big-endian integer which is the size of the freeblock
68443 ** in bytes, including the 4-byte header. */
68446 if( (x = size - nByte)>=0 ){
68450 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
68454 /* Remove the slot from the free-list. Update the number of
68465 /* The slot remains on the free-list. Reduce its size to account
68482 if( pc>maxPC+nByte-4 ){
68490 ** Allocate nByte bytes of space from within the B-Tree page passed
68491 ** as the first argument. Write into *pIdx the index into pPage->aData[]
68503 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
68504 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
68510 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68511 assert( pPage->pBt );
68512 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68514 assert( pPage->nFree>=nByte );
68515 assert( pPage->nOverflow==0 );
68516 assert( nByte < (int)(pPage->pBt->usableSize-8) );
68518 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
68519 gap = pPage->cellOffset + 2*pPage->nCell;
68521 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
68524 ** However, that integer is too large to be stored in a 2-byte unsigned
68528 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
68530 if( top==0 && pPage->pBt->usableSize==65536 ){
68548 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
68549 *pIdx = g2 = (int)(pSpace-data);
68565 assert( pPage->nCell>0 || CORRUPT_DB );
68566 assert( pPage->nFree>=0 );
68567 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
68580 top -= nByte;
68582 assert( top+nByte <= (int)pPage->pBt->usableSize );
68588 ** Return a section of the pPage->aData to the freelist.
68589 ** The first byte of the new free block is pPage->aData[iStart]
68608 unsigned char *data = pPage->aData; /* Page content */
68611 assert( pPage->pBt!=0 );
68612 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68613 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
68614 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
68615 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68617 assert( iStart<=pPage->pBt->usableSize-4 );
68622 hdr = pPage->hdrOffset;
68634 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
68646 nFrag = iFreeBlk - iEnd;
68649 if( iEnd > pPage->pBt->usableSize ){
68652 iSize = iEnd - iStart;
68664 nFrag += iStart - iPtrEnd;
68665 iSize = iEnd - iPtr;
68670 data[hdr+7] -= nFrag;
68686 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
68693 pPage->nFree += iOrigSize;
68710 BtShared *pBt; /* A copy of pPage->pBt */
68712 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
68713 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68714 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
68716 pPage->childPtrSize = 4-4*pPage->leaf;
68717 pBt = pPage->pBt;
68719 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
68720 ** interior table b-tree page. */
68722 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
68723 ** leaf table b-tree page. */
68725 pPage->intKey = 1;
68726 if( pPage->leaf ){
68727 pPage->intKeyLeaf = 1;
68728 pPage->xCellSize = cellSizePtrTableLeaf;
68729 pPage->xParseCell = btreeParseCellPtr;
68731 pPage->intKeyLeaf = 0;
68732 pPage->xCellSize = cellSizePtrNoPayload;
68733 pPage->xParseCell = btreeParseCellPtrNoPayload;
68735 pPage->maxLocal = pBt->maxLeaf;
68736 pPage->minLocal = pBt->minLeaf;
68738 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
68739 ** interior index b-tree page. */
68741 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
68742 ** leaf index b-tree page. */
68744 pPage->intKey = 0;
68745 pPage->intKeyLeaf = 0;
68746 pPage->xCellSize = cellSizePtr;
68747 pPage->xParseCell = btreeParseCellPtrIndex;
68748 pPage->maxLocal = pBt->maxLocal;
68749 pPage->minLocal = pBt->minLocal;
68751 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
68753 pPage->intKey = 0;
68754 pPage->intKeyLeaf = 0;
68755 pPage->xCellSize = cellSizePtr;
68756 pPage->xParseCell = btreeParseCellPtrIndex;
68759 pPage->max1bytePayload = pBt->max1bytePayload;
68765 ** in the pPage->nFree field.
68768 int pc; /* Address of a freeblock within pPage->aData[] */
68770 u8 *data; /* Equal to pPage->aData */
68777 assert( pPage->pBt!=0 );
68778 assert( pPage->pBt->db!=0 );
68779 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68780 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
68781 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
68782 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
68783 assert( pPage->isInit==1 );
68784 assert( pPage->nFree<0 );
68786 usableSize = pPage->pBt->usableSize;
68787 hdr = pPage->hdrOffset;
68788 data = pPage->aData;
68789 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
68793 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
68794 iCellLast = usableSize - 4;
68797 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
68801 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
68805 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
68832 ** of the cell-content area plus the number of free bytes within
68833 ** the cell-content area. If this is greater than the usable-size
68835 ** serves to verify that the offset to the start of the cell-content
68841 pPage->nFree = (u16)(nFree - iCellFirst);
68854 int pc; /* Address of a freeblock within pPage->aData[] */
68855 u8 *data; /* Equal to pPage->aData */
68859 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
68860 usableSize = pPage->pBt->usableSize;
68861 iCellLast = usableSize - 4;
68862 data = pPage->aData;
68863 cellOffset = pPage->cellOffset;
68864 if( !pPage->leaf ) iCellLast--;
68865 for(i=0; i<pPage->nCell; i++){
68872 sz = pPage->xCellSize(pPage, &data[pc]);
68885 ** not contain a well-formed database page, then return
68887 ** guarantee that the page is well-formed. It only shows that
68891 u8 *data; /* Equal to pPage->aData */
68894 assert( pPage->pBt!=0 );
68895 assert( pPage->pBt->db!=0 );
68896 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68897 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
68898 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
68899 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
68900 assert( pPage->isInit==0 );
68902 pBt = pPage->pBt;
68903 data = pPage->aData + pPage->hdrOffset;
68904 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
68905 ** the b-tree page type. */
68909 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
68910 pPage->maskPage = (u16)(pBt->pageSize - 1);
68911 pPage->nOverflow = 0;
68912 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
68913 pPage->aCellIdx = data + pPage->childPtrSize + 8;
68914 pPage->aDataEnd = pPage->aData + pBt->pageSize;
68915 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
68916 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68918 pPage->nCell = get2byte(&data[3]);
68919 if( pPage->nCell>MX_CELL(pBt) ){
68923 testcase( pPage->nCell==MX_CELL(pBt) );
68924 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
68928 assert( pPage->nCell>0
68929 || get2byteNotZero(&data[5])==(int)pBt->usableSize
68931 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
68932 pPage->isInit = 1;
68933 if( pBt->db->flags & SQLITE_CellSizeCk ){
68944 unsigned char *data = pPage->aData;
68945 BtShared *pBt = pPage->pBt;
68946 u8 hdr = pPage->hdrOffset;
68949 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
68950 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
68951 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
68952 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68953 assert( sqlite3_mutex_held(pBt->mutex) );
68954 if( pBt->btsFlags & BTS_FAST_SECURE ){
68955 memset(&data[hdr], 0, pBt->usableSize - hdr);
68961 put2byte(&data[hdr+5], pBt->usableSize);
68962 pPage->nFree = (u16)(pBt->usableSize - first);
68964 pPage->cellOffset = first;
68965 pPage->aDataEnd = &data[pBt->pageSize];
68966 pPage->aCellIdx = &data[first];
68967 pPage->aDataOfst = &data[pPage->childPtrSize];
68968 pPage->nOverflow = 0;
68969 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
68970 pPage->maskPage = (u16)(pBt->pageSize - 1);
68971 pPage->nCell = 0;
68972 pPage->isInit = 1;
68982 if( pgno!=pPage->pgno ){
68983 pPage->aData = sqlite3PagerGetData(pDbPage);
68984 pPage->pDbPage = pDbPage;
68985 pPage->pBt = pBt;
68986 pPage->pgno = pgno;
68987 pPage->hdrOffset = pgno==1 ? 100 : 0;
68989 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
69014 assert( sqlite3_mutex_held(pBt->mutex) );
69015 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
69028 assert( sqlite3_mutex_held(pBt->mutex) );
69029 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
69038 ** error, return ((unsigned int)-1).
69041 return pBt->nPage;
69045 return btreePagecount(p->pBt);
69053 ** And if the fetch fails, this routine must decrement pCur->iPage.
69055 ** The page is fetched as read-write unless pCur is not NULL and is
69056 ** a read-only cursor.
69066 int bReadOnly /* True for a read-only page */
69070 assert( sqlite3_mutex_held(pBt->mutex) );
69071 assert( pCur==0 || ppPage==&pCur->pPage );
69072 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
69073 assert( pCur==0 || pCur->iPage>0 );
69079 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
69084 if( (*ppPage)->isInit==0 ){
69091 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
69092 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
69096 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
69106 pCur->iPage--;
69107 pCur->pPage = pCur->apPage[pCur->iPage];
69123 assert( pPage->aData );
69124 assert( pPage->pBt );
69125 assert( pPage->pDbPage!=0 );
69126 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
69127 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
69128 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69129 sqlite3PagerUnrefNotNull(pPage->pDbPage);
69136 assert( pPage->aData );
69137 assert( pPage->pBt );
69138 assert( pPage->pDbPage!=0 );
69139 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
69140 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
69141 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69142 sqlite3PagerUnrefPageOne(pPage->pDbPage);
69162 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
69167 (*ppPage)->isInit = 0;
69187 if( pPage->isInit ){
69188 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69189 pPage->isInit = 0;
69196 ** the call for every page that comes in for re-initing. */
69207 assert( pBt->db );
69208 assert( sqlite3_mutex_held(pBt->db->mutex) );
69209 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
69217 ** be exclusively in memory, or it might use a disk-based memory cache.
69221 ** If zFilename is ":memory:" then an in-memory database is created
69234 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
69251 /* Set the variable isMemdb to true for an in-memory database, or
69252 ** false for a file-based database.
69264 assert( sqlite3_mutex_held(db->mutex) );
69283 p->inTrans = TRANS_NONE;
69284 p->db = db;
69286 p->lock.pBtree = p;
69287 p->lock.iTable = 1;
69298 int nFullPathname = pVfs->mxPathname+1;
69302 p->sharable = 1;
69328 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
69329 assert( pBt->nRef>0 );
69330 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
69331 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
69333 for(iDb=db->nDb-1; iDb>=0; iDb--){
69334 Btree *pExisting = db->aDb[iDb].pBt;
69335 if( pExisting && pExisting->pBt==pBt ){
69343 p->pBt = pBt;
69344 pBt->nRef++;
69358 p->sharable = 1;
69380 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
69383 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
69384 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
69389 pBt->openFlags = (u8)flags;
69390 pBt->db = db;
69391 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
69392 p->pBt = pBt;
69394 pBt->pCursor = 0;
69395 pBt->pPage1 = 0;
69396 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
69398 pBt->btsFlags |= BTS_SECURE_DELETE;
69400 pBt->btsFlags |= BTS_OVERWRITE;
69402 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
69403 ** determined by the 2-byte integer located at an offset of 16 bytes from
69405 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
69406 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
69407 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
69408 pBt->pageSize = 0;
69410 /* If the magic name ":memory:" will create an in-memory database, then
69411 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
69414 ** regular file-name. In this case the auto-vacuum applies as per normal.
69417 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
69418 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
69423 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
69424 ** determined by the one-byte unsigned integer found at an offset of 20
69427 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
69429 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
69430 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
69433 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69435 pBt->usableSize = pBt->pageSize - nReserve;
69436 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
69441 pBt->nRef = 1;
69442 if( p->sharable ){
69446 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
69447 if( pBt->mutex==0 ){
69453 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
69465 if( p->sharable ){
69468 for(i=0; i<db->nDb; i++){
69469 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
69470 while( pSib->pPrev ){ pSib = pSib->pPrev; }
69471 if( (uptr)p->pBt<(uptr)pSib->pBt ){
69472 p->pNext = pSib;
69473 p->pPrev = 0;
69474 pSib->pPrev = p;
69476 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
69477 pSib = pSib->pNext;
69479 p->pNext = pSib->pNext;
69480 p->pPrev = pSib;
69481 if( p->pNext ){
69482 p->pNext->pPrev = p;
69484 pSib->pNext = p;
69495 if( pBt && pBt->pPager ){
69496 sqlite3PagerClose(pBt->pPager, 0);
69504 /* If the B-Tree was successfully opened, set the pager-cache size to the
69505 ** default value. Except, when opening on an existing shared pager-cache,
69506 ** do not change the pager-cache size.
69512 pFile = sqlite3PagerFile(pBt->pPager);
69513 if( pFile->pMethods ){
69514 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
69537 assert( sqlite3_mutex_notheld(pBt->mutex) );
69540 pBt->nRef--;
69541 if( pBt->nRef<=0 ){
69543 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
69546 while( ALWAYS(pList) && pList->pNext!=pBt ){
69547 pList=pList->pNext;
69550 pList->pNext = pBt->pNext;
69554 sqlite3_mutex_free(pBt->mutex);
69566 ** Make sure pBt->pTmpSpace points to an allocation of
69567 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
69572 assert( pBt->pTmpSpace==0 );
69575 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
69576 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
69577 if( pBt->pTmpSpace==0 ){
69578 BtCursor *pCur = pBt->pCursor;
69579 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
69584 /* One of the uses of pBt->pTmpSpace is to format cells before
69597 ** left-child pointer to the beginning of a cell.
69599 memset(pBt->pTmpSpace, 0, 8);
69600 pBt->pTmpSpace += 4;
69605 ** Free the pBt->pTmpSpace allocation
69608 if( pBt->pTmpSpace ){
69609 pBt->pTmpSpace -= 4;
69610 sqlite3PageFree(pBt->pTmpSpace);
69611 pBt->pTmpSpace = 0;
69619 BtShared *pBt = p->pBt;
69622 assert( sqlite3_mutex_held(p->db->mutex) );
69628 BtCursor *pCur = pBt->pCursor;
69631 pCur = pCur->pNext;
69632 assert( pTmp->pBtree!=p );
69639 ** The call to sqlite3BtreeRollback() drops any table-locks held by
69645 /* If there are still other outstanding references to the shared-btree
69647 ** up the shared-btree.
69649 assert( p->wantToLock==0 && p->locked==0 );
69650 if( !p->sharable || removeFromSharingList(pBt) ){
69656 assert( !pBt->pCursor );
69657 sqlite3PagerClose(pBt->pPager, p->db);
69658 if( pBt->xFreeSchema && pBt->pSchema ){
69659 pBt->xFreeSchema(pBt->pSchema);
69661 sqlite3DbFree(0, pBt->pSchema);
69667 assert( p->wantToLock==0 );
69668 assert( p->locked==0 );
69669 if( p->pPrev ) p->pPrev->pNext = p->pNext;
69670 if( p->pNext ) p->pNext->pPrev = p->pPrev;
69685 BtShared *pBt = p->pBt;
69686 assert( sqlite3_mutex_held(p->db->mutex) );
69688 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
69704 BtShared *pBt = p->pBt;
69706 assert( sqlite3_mutex_held(p->db->mutex) );
69708 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
69719 BtShared *pBt = p->pBt;
69720 assert( sqlite3_mutex_held(p->db->mutex) );
69722 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
69733 ** is a very low but non-zero probability of damage. Level 3 reduces the
69741 BtShared *pBt = p->pBt;
69742 assert( sqlite3_mutex_held(p->db->mutex) );
69744 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
69773 BtShared *pBt = p->pBt;
69776 pBt->nReserveWanted = nReserve;
69777 x = pBt->pageSize - pBt->usableSize;
69779 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
69785 ((pageSize-1)&pageSize)==0 ){
69787 assert( !pBt->pCursor );
69789 pBt->pageSize = (u32)pageSize;
69792 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69793 pBt->usableSize = pBt->pageSize - (u16)nReserve;
69794 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
69803 return p->pBt->pageSize;
69808 ** may only be called if it is guaranteed that the b-tree mutex is already
69812 ** known that the shared b-tree mutex is held, but the mutex on the
69819 assert( sqlite3_mutex_held(p->pBt->mutex) );
69820 n = p->pBt->pageSize - p->pBt->usableSize;
69831 ** The amount of reserve can only grow - never shrink.
69836 n1 = (int)p->pBt->nReserveWanted;
69851 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
69862 ** newFlag==(-1) No changes
69867 ** freelist leaf pages are not written back to the database. Thus in-page
69881 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
69882 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
69884 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
69890 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
69891 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
69892 ** is disabled. The default value for the auto-vacuum property is
69899 BtShared *pBt = p->pBt;
69904 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
69907 pBt->autoVacuum = av ?1:0;
69908 pBt->incrVacuum = av==2 ?1:0;
69916 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
69926 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
69927 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
69936 ** If the user has not set the safety-level for this database connection
69937 ** using "PRAGMA synchronous", and if the safety-level is not already
69946 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
69947 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
69948 if( pDb->bSyncSet==0
69949 && pDb->safety_level!=safety_level
69950 && pDb!=&db->aDb[1]
69952 pDb->safety_level = safety_level;
69953 sqlite3PagerSetFlags(pBt->pPager,
69954 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
69971 ** well-formed database file, then SQLITE_CORRUPT is returned.
69981 assert( sqlite3_mutex_held(pBt->mutex) );
69982 assert( pBt->pPage1==0 );
69983 rc = sqlite3PagerSharedLock(pBt->pPager);
69991 nPage = get4byte(28+(u8*)pPage1->aData);
69992 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
69993 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
69996 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
70002 u8 *page1 = pPage1->aData;
70004 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
70013 pBt->btsFlags |= BTS_READ_ONLY;
70020 pBt->btsFlags |= BTS_READ_ONLY;
70031 ** may not be the latest version - there may be a newer one in the log
70034 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
70036 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
70052 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
70061 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
70062 ** determined by the 2-byte integer located at an offset of 16 bytes from
70065 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
70067 if( ((pageSize-1)&pageSize)!=0
70073 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
70075 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
70079 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
70080 ** determined by the one-byte unsigned integer found at an offset of 20
70082 usableSize = pageSize - page1[20];
70083 if( (u32)pageSize!=pBt->pageSize ){
70085 ** of BtShared.pageSize, we have discovered that the page-size is
70086 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
70088 ** again with the correct page-size.
70091 pBt->usableSize = usableSize;
70092 pBt->pageSize = pageSize;
70094 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
70095 pageSize-usableSize);
70099 if( sqlite3WritableSchema(pBt->db)==0 ){
70106 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
70112 pBt->pageSize = pageSize;
70113 pBt->usableSize = usableSize;
70115 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
70116 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
70122 ** cells can will fit on one page. We assume a 10-byte page header.
70124 ** 2-byte pointer to the cell
70125 ** 4-byte child pointer
70126 ** 9-byte nKey value
70127 ** 4-byte nData value
70128 ** 4-byte overflow page pointer
70129 ** So a cell consists of a 2-byte pointer, a header which is as much as
70133 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
70134 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
70135 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
70136 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
70137 if( pBt->maxLocal>127 ){
70138 pBt->max1bytePayload = 127;
70140 pBt->max1bytePayload = (u8)pBt->maxLocal;
70142 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
70143 pBt->pPage1 = pPage1;
70144 pBt->nPage = nPage;
70149 pBt->pPage1 = 0;
70169 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
70170 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
70171 && pCur->eState!=CURSOR_FAULT ) r++;
70183 ** If there is a transaction in progress, this routine is a no-op.
70186 assert( sqlite3_mutex_held(pBt->mutex) );
70187 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
70188 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
70189 MemPage *pPage1 = pBt->pPage1;
70190 assert( pPage1->aData );
70191 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
70192 pBt->pPage1 = 0;
70207 assert( sqlite3_mutex_held(pBt->mutex) );
70208 if( pBt->nPage>0 ){
70211 pP1 = pBt->pPage1;
70213 data = pP1->aData;
70214 rc = sqlite3PagerWrite(pP1->pDbPage);
70218 data[16] = (u8)((pBt->pageSize>>8)&0xff);
70219 data[17] = (u8)((pBt->pageSize>>16)&0xff);
70222 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
70223 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
70227 memset(&data[24], 0, 100-24);
70229 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
70231 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
70232 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
70233 put4byte(&data[36 + 4*4], pBt->autoVacuum);
70234 put4byte(&data[36 + 7*4], pBt->incrVacuum);
70236 pBt->nPage = 1;
70249 p->pBt->nPage = 0;
70250 rc = newDatabase(p->pBt);
70256 ** Attempt to start a new transaction. A write-transaction
70257 ** is started if the second argument is nonzero, otherwise a read-
70261 ** upgraded to exclusive by calling this routine a second time - the
70264 ** A write-transaction must be started before attempting any
70278 ** if there is one. But if there was previously a read-lock, do not
70279 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
70280 ** returned when there is already a read-lock in order to avoid a deadlock.
70291 BtShared *pBt = p->pBt;
70292 Pager *pPager = pBt->pPager;
70298 /* If the btree is already in a write-transaction, or it
70299 ** is already in a read-transaction and a read-transaction
70300 ** is requested, this is a no-op.
70302 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
70305 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
70307 if( (p->db->flags & SQLITE_ResetDatabase)
70310 pBt->btsFlags &= ~BTS_READ_ONLY;
70313 /* Write transactions are not possible on a read-only database */
70314 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
70323 ** on this shared-btree structure and a second write transaction is
70326 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
70327 || (pBt->btsFlags & BTS_PENDING)!=0
70329 pBlock = pBt->pWriter->db;
70332 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70333 if( pIter->pBtree!=p ){
70334 pBlock = pIter->pBtree->db;
70340 sqlite3ConnectionBlocked(p->db, pBlock);
70347 /* Any read-only or read-write transaction implies a read-lock on
70348 ** page 1. So if some other shared-cache client already has a write-lock
70353 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
70354 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
70356 sqlite3PagerWalDb(pPager, p->db);
70361 if( pBt->pPage1==0 && wrflag ){
70362 assert( pBt->inTransaction==TRANS_NONE );
70368 /* Call lockBtree() until either pBt->pPage1 is populated or
70370 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
70371 ** reading page 1 it discovers that the page-size of the database
70372 ** file is not pBt->pageSize. In this case lockBtree() will update
70373 ** pBt->pageSize to the page-size of the file on disk.
70375 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
70378 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
70381 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
70384 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
70397 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
70405 if( p->inTrans==TRANS_NONE ){
70406 pBt->nTransaction++;
70408 if( p->sharable ){
70409 assert( p->lock.pBtree==p && p->lock.iTable==1 );
70410 p->lock.eLock = READ_LOCK;
70411 p->lock.pNext = pBt->pLock;
70412 pBt->pLock = &p->lock;
70416 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
70417 if( p->inTrans>pBt->inTransaction ){
70418 pBt->inTransaction = p->inTrans;
70421 MemPage *pPage1 = pBt->pPage1;
70423 assert( !pBt->pWriter );
70424 pBt->pWriter = p;
70425 pBt->btsFlags &= ~BTS_EXCLUSIVE;
70426 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
70429 /* If the db-size header field is incorrect (as it may be if an old
70432 ** re-read the database size from page 1 if a savepoint or transaction
70435 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
70436 rc = sqlite3PagerWrite(pPage1->pDbPage);
70438 put4byte(&pPage1->aData[28], pBt->nPage);
70447 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
70452 ** the sub-journal is not already open, then it will be opened here.
70454 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
70466 ** Set the pointer-map entries for all children of page pPage. Also, if
70474 BtShared *pBt = pPage->pBt;
70475 Pgno pgno = pPage->pgno;
70477 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70478 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
70480 nCell = pPage->nCell;
70487 if( !pPage->leaf ){
70493 if( !pPage->leaf ){
70494 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70506 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
70509 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
70512 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
70516 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70517 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70520 if( get4byte(pPage->aData)!=iFrom ){
70523 put4byte(pPage->aData, iTo);
70529 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
70531 nCell = pPage->nCell;
70537 pPage->xParseCell(pPage, pCell, &info);
70539 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
70542 if( iFrom==get4byte(pCell+info.nSize-4) ){
70543 put4byte(pCell+info.nSize-4, iTo);
70557 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
70560 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
70572 ** the journal needs to be sync()ed before database page pDbPage->pgno
70580 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
70585 Pgno iDbPage = pDbPage->pgno;
70586 Pager *pPager = pBt->pPager;
70591 assert( sqlite3_mutex_held(pBt->mutex) );
70592 assert( pDbPage->pBt==pBt );
70598 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
70602 pDbPage->pgno = iFreePage;
70604 /* If pDbPage was a btree-page, then it may have child pages and/or cells
70618 Pgno nextOvfl = get4byte(pDbPage->aData);
70636 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
70654 ** Perform a single step of an incremental-vacuum. If successful, return
70659 ** More specifically, this function attempts to re-organize the database so
70665 ** If the bCommit parameter is non-zero, this function assumes that the
70667 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
70671 Pgno nFreeList; /* Number of pages still on the free-list */
70674 assert( sqlite3_mutex_held(pBt->mutex) );
70681 nFreeList = get4byte(&pBt->pPage1->aData[36]);
70696 /* Remove the page from the files free-list. This is not required
70697 ** if bCommit is non-zero. In that case, the free-list will be
70725 ** looping until a free-page located within the first nFin pages
70758 iLastPg--;
70760 pBt->bDoTruncate = 1;
70761 pBt->nPage = iLastPg;
70767 ** The database opened by the first argument is an auto-vacuum database
70769 ** size of the database in pages following an auto-vacuum operation.
70776 nEntry = pBt->usableSize/5;
70777 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
70778 nFin = nOrig - nFree - nPtrmap;
70780 nFin--;
70783 nFin--;
70790 ** A write-transaction must be opened before calling this function.
70799 BtShared *pBt = p->pBt;
70802 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
70803 if( !pBt->autoVacuum ){
70807 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
70819 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70820 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
70832 ** is committed for an auto-vacuum database.
70842 pBt = p->pBt;
70843 pPager = pBt->pPager;
70846 assert( sqlite3_mutex_held(pBt->mutex) );
70848 assert(pBt->autoVacuum);
70849 if( !pBt->incrVacuum ){
70859 ** is either a pointer-map page or the pending-byte page. If one
70865 nFree = get4byte(&pBt->pPage1->aData[36]);
70866 db = p->db;
70867 if( db->xAutovacPages ){
70869 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
70870 if( db->aDb[iDb].pBt==p ) break;
70872 nVac = db->xAutovacPages(
70873 db->pAutovacPagesArg,
70874 db->aDb[iDb].zDbSName,
70877 pBt->pageSize
70893 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
70897 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70899 put4byte(&pBt->pPage1->aData[32], 0);
70900 put4byte(&pBt->pPage1->aData[36], 0);
70902 put4byte(&pBt->pPage1->aData[28], nFin);
70903 pBt->bDoTruncate = 1;
70904 pBt->nPage = nFin;
70920 ** This routine does the first phase of a two-phase commit. This routine
70932 ** This call is a no-op if no write-transaction is currently active on pBt.
70935 ** the name of a super-journal file that should be written into the
70936 ** individual journal file, or is NULL, indicating no super-journal file
70939 ** When this is called, the super-journal should already have been
70943 ** the write-transaction for this database file is to delete the journal.
70947 if( p->inTrans==TRANS_WRITE ){
70948 BtShared *pBt = p->pBt;
70951 if( pBt->autoVacuum ){
70958 if( pBt->bDoTruncate ){
70959 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
70962 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
70973 BtShared *pBt = p->pBt;
70974 sqlite3 *db = p->db;
70978 pBt->bDoTruncate = 0;
70980 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
70982 ** handle, downgrade to a read-only transaction. The other statements
70985 p->inTrans = TRANS_READ;
70991 if( p->inTrans!=TRANS_NONE ){
70993 pBt->nTransaction--;
70994 if( 0==pBt->nTransaction ){
70995 pBt->inTransaction = TRANS_NONE;
71001 p->inTrans = TRANS_NONE;
71011 ** This routine implements the second phase of a 2-phase commit. The
71023 ** is non-zero then this b-tree transaction is part of a multi-file
71025 ** (by deleting a super-journal file) and the caller will ignore this
71027 ** reset the b-tree objects internal state to indicate that the write
71036 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
71040 /* If the handle has a write-transaction open, commit the shared-btrees
71043 if( p->inTrans==TRANS_WRITE ){
71045 BtShared *pBt = p->pBt;
71046 assert( pBt->inTransaction==TRANS_WRITE );
71047 assert( pBt->nTransaction>0 );
71048 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
71053 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
71054 pBt->inTransaction = TRANS_READ;
71088 ** flag is true, then only write-cursors need be tripped - read-only
71092 ** rolled back modified the database schema. In this case b-tree root
71097 ** saving the current position of a read-only cursor, all cursors,
71098 ** including all read-cursors are tripped.
71110 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71111 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
71112 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
71121 p->eState = CURSOR_FAULT;
71122 p->skipNext = errCode;
71132 ** Set the pBt->nPage field correctly, according to the current
71133 ** state of the database. Assume pBt->pPage1 is valid.
71136 int nPage = get4byte(&pPage1->aData[28]);
71138 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
71139 testcase( pBt->nPage!=(u32)nPage );
71140 pBt->nPage = nPage;
71156 BtShared *pBt = p->pBt;
71175 if( p->inTrans==TRANS_WRITE ){
71178 assert( TRANS_WRITE==pBt->inTransaction );
71179 rc2 = sqlite3PagerRollback(pBt->pPager);
71184 /* The rollback may have destroyed the pPage1->aData value. So
71186 ** sure pPage1->aData is set correctly. */
71192 pBt->inTransaction = TRANS_READ;
71212 ** A statement sub-transaction is implemented as an anonymous savepoint. The
71214 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
71215 ** are no active savepoints and no other statement-transactions open,
71221 BtShared *pBt = p->pBt;
71223 assert( p->inTrans==TRANS_WRITE );
71224 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71226 assert( iStatement>p->db->nSavepoint );
71227 assert( pBt->inTransaction==TRANS_WRITE );
71233 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
71245 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
71252 if( p && p->inTrans==TRANS_WRITE ){
71253 BtShared *pBt = p->pBt;
71255 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
71261 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
71264 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
71265 pBt->nPage = 0;
71268 btreeSetNPage(pBt, pBt->pPage1);
71270 /* pBt->nPage might be zero if the database was corrupt when
71272 assert( CORRUPT_DB || pBt->nPage>0 );
71281 ** iTable. If a read-only cursor is requested, it is assumed that
71282 ** the caller already has at least a read-only transaction open
71283 ** on the database already. If a write-cursor is requested, then
71300 ** 3: The database must be writable (not on read-only media)
71311 ** operations on this cursor can be no-ops and all READ operations can
71312 ** return a null row (2-bytes: 0x01 0x00).
71315 ** root page of a b-tree. If it is not, then the cursor acquired
71324 int wrFlag, /* 1 to write. 0 read-only */
71328 BtShared *pBt = p->pBt; /* Shared b-tree handle */
71338 ** b-tree database, the connection is holding the required table locks,
71346 assert( p->inTrans>TRANS_NONE );
71347 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
71348 assert( pBt->pPage1 && pBt->pPage1->aData );
71349 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
71362 pCur->pgnoRoot = iTable;
71363 pCur->iPage = -1;
71364 pCur->pKeyInfo = pKeyInfo;
71365 pCur->pBtree = p;
71366 pCur->pBt = pBt;
71367 pCur->curFlags = 0;
71370 for(pX=pBt->pCursor; pX; pX=pX->pNext){
71371 if( pX->pgnoRoot==iTable ){
71372 pX->curFlags |= BTCF_Multiple;
71373 pCur->curFlags = BTCF_Multiple;
71376 pCur->eState = CURSOR_INVALID;
71377 pCur->pNext = pBt->pCursor;
71378 pBt->pCursor = pCur;
71380 pCur->curFlags |= BTCF_WriteFlag;
71381 pCur->curPagerFlags = 0;
71382 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
71384 pCur->curPagerFlags = PAGER_GET_READONLY;
71391 int wrFlag, /* 1 to write. 0 read-only */
71404 int wrFlag, /* 1 to write. 0 read-only */
71408 if( p->sharable ){
71420 ** to users so they cannot do the sizeof() themselves - they must call
71433 ** of run-time by skipping the initialization of those elements.
71444 Btree *pBtree = pCur->pBtree;
71446 BtShared *pBt = pCur->pBt;
71448 assert( pBt->pCursor!=0 );
71449 if( pBt->pCursor==pCur ){
71450 pBt->pCursor = pCur->pNext;
71452 BtCursor *pPrev = pBt->pCursor;
71454 if( pPrev->pNext==pCur ){
71455 pPrev->pNext = pCur->pNext;
71458 pPrev = pPrev->pNext;
71463 sqlite3_free(pCur->aOverflow);
71464 sqlite3_free(pCur->pKey);
71465 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
71468 assert( pBtree->sharable==0 );
71473 pCur->pBtree = 0;
71488 if( a->nKey!=b->nKey ) return 0;
71489 if( a->pPayload!=b->pPayload ) return 0;
71490 if( a->nPayload!=b->nPayload ) return 0;
71491 if( a->nLocal!=b->nLocal ) return 0;
71492 if( a->nSize!=b->nSize ) return 0;
71498 btreeParseCell(pCur->pPage, pCur->ix, &info);
71499 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
71505 if( pCur->info.nSize==0 ){
71506 pCur->curFlags |= BTCF_ValidNKey;
71507 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
71516 ** that is currently pointing to a row in a (non-empty) table.
71520 return pCur && pCur->eState==CURSOR_VALID;
71525 return pCur->eState==CURSOR_VALID;
71536 assert( pCur->eState==CURSOR_VALID );
71537 assert( pCur->curIntKey );
71539 return pCur->info.nKey;
71546 assert( (pCur->curFlags & BTCF_Pinned)==0 );
71547 pCur->curFlags |= BTCF_Pinned;
71550 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
71551 pCur->curFlags &= ~BTCF_Pinned;
71561 assert( pCur->eState==CURSOR_VALID );
71563 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
71564 (i64)(pCur->info.pPayload - pCur->pPage->aData);
71573 ** The caller must guarantee that the cursor is pointing to a non-NULL
71579 assert( pCur->eState==CURSOR_VALID );
71581 return pCur->info.nPayload;
71599 assert( pCur->eState==CURSOR_VALID );
71600 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
71606 ** linked list of overflow pages. If possible, it uses the auto-vacuum
71607 ** pointer-map data instead of reading the content of page ovfl to do so.
71619 ** the pointer-map was used to obtain the value for *pPgnoNext), then
71632 assert( sqlite3_mutex_held(pBt->mutex) );
71637 ** autovacuum pointer-map pages. Guess that the next page in
71642 if( pBt->autoVacuum ){
71666 next = get4byte(pPage->aData);
71694 int eOp, /* 0 -> copy from page, 1 -> copy to page */
71727 ** the overflow page-list cache array (BtCursor.aOverflow).
71731 ** Once an overflow page-list cache has been allocated, it must be
71733 ** the cursor is moved to a different row. Additionally, in auto-vacuum
71734 ** mode, the following events may invalidate an overflow page-list cache.
71745 int eOp /* zero to read. non-zero to write. */
71750 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
71751 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
71758 assert( pCur->eState==CURSOR_VALID );
71759 if( pCur->ix>=pPage->nCell ){
71765 aPayload = pCur->info.pPayload;
71766 assert( offset+amt <= pCur->info.nPayload );
71768 assert( aPayload > pPage->aData );
71769 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
71772 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
71779 if( offset<pCur->info.nLocal ){
71781 if( a+offset>pCur->info.nLocal ){
71782 a = pCur->info.nLocal - offset;
71784 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
71787 amt -= a;
71789 offset -= pCur->info.nLocal;
71794 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
71797 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
71806 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
71807 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
71808 if( pCur->aOverflow==0
71809 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
71812 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
71817 pCur->aOverflow = aNew;
71820 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
71821 pCur->curFlags |= BTCF_ValidOvfl;
71823 /* If the overflow page-list cache has been allocated and the
71827 if( pCur->aOverflow[offset/ovflSize] ){
71829 nextPage = pCur->aOverflow[iIdx];
71836 /* If required, populate the overflow page-list cache. */
71837 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
71838 assert( pCur->aOverflow[iIdx]==0
71839 || pCur->aOverflow[iIdx]==nextPage
71841 pCur->aOverflow[iIdx] = nextPage;
71847 ** page-list cache, if any, then fall back to the getOverflowPage()
71850 assert( pCur->curFlags & BTCF_ValidOvfl );
71851 assert( pCur->pBtree->db==pBt->db );
71852 if( pCur->aOverflow[iIdx+1] ){
71853 nextPage = pCur->aOverflow[iIdx+1];
71857 offset -= ovflSize;
71864 a = ovflSize - offset;
71872 ** 3) there are no dirty pages in the page-cache
71873 ** 4) the database is file-backed, and
71878 ** output buffer, bypassing the page-cache altogether. This speeds
71883 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
71884 && &pBuf[-4]>=pBufStart /* (6) */
71886 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
71888 u8 *aWrite = &pBuf[-4];
71891 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
71892 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
71900 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
71911 amt -= a;
71932 ** pCur can be pointing to either a table or an index b-tree.
71934 ** pCur is pointing to an index b-tree then the key section is read.
71946 assert( pCur->eState==CURSOR_VALID );
71947 assert( pCur->iPage>=0 && pCur->pPage );
71964 if ( pCur->eState==CURSOR_INVALID ){
71972 if( pCur->eState==CURSOR_VALID ){
71984 ** the key if index btrees (pPage->intKey==0) and is the data for
71985 ** table btrees (pPage->intKey==1). The number of bytes of available
72005 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
72006 assert( pCur->eState==CURSOR_VALID );
72007 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72009 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
72010 assert( pCur->info.nSize>0 );
72011 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
72012 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
72013 amt = pCur->info.nLocal;
72014 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
72018 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
72021 return (void*)pCur->info.pPayload;
72028 ** b-tree page. Write the number of available bytes into *pAmt.
72048 ** This function returns SQLITE_CORRUPT if the page-header flags field of
72050 ** if an intkey page appears to be the parent of a non-intkey page, or
72051 ** vice-versa).
72054 BtShared *pBt = pCur->pBt;
72057 assert( pCur->eState==CURSOR_VALID );
72058 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
72059 assert( pCur->iPage>=0 );
72060 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
72063 pCur->info.nSize = 0;
72064 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72065 pCur->aiIdx[pCur->iPage] = pCur->ix;
72066 pCur->apPage[pCur->iPage] = pCur->pPage;
72067 pCur->ix = 0;
72068 pCur->iPage++;
72069 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
72074 ** Page pParent is an internal (non-leaf) tree page. This function
72075 ** asserts that page number iChild is the left-child if the iIdx'th
72077 ** cells in pParent, that page number iChild is the right-child of
72083 assert( iIdx<=pParent->nCell );
72084 if( iIdx==pParent->nCell ){
72085 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
72097 ** pCur->idx is set to the cell index that contains the pointer
72099 ** right-most child page then pCur->idx is set to one more than
72105 assert( pCur->eState==CURSOR_VALID );
72106 assert( pCur->iPage>0 );
72107 assert( pCur->pPage );
72109 pCur->apPage[pCur->iPage-1],
72110 pCur->aiIdx[pCur->iPage-1],
72111 pCur->pPage->pgno
72113 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
72114 pCur->info.nSize = 0;
72115 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72116 pCur->ix = pCur->aiIdx[pCur->iPage-1];
72117 pLeaf = pCur->pPage;
72118 pCur->pPage = pCur->apPage[--pCur->iPage];
72123 ** Move the cursor to point to the root page of its b-tree structure.
72130 ** If the b-tree structure is empty, the cursor state is set to
72136 ** page-header flags indicate that the [virtual] root-page is the expected
72137 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
72139 ** indicating a table b-tree, or if the caller did specify a KeyInfo
72141 ** b-tree).
72151 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
72152 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
72154 if( pCur->iPage>=0 ){
72155 if( pCur->iPage ){
72156 releasePageNotNull(pCur->pPage);
72157 while( --pCur->iPage ){
72158 releasePageNotNull(pCur->apPage[pCur->iPage]);
72160 pRoot = pCur->pPage = pCur->apPage[0];
72163 }else if( pCur->pgnoRoot==0 ){
72164 pCur->eState = CURSOR_INVALID;
72167 assert( pCur->iPage==(-1) );
72168 if( pCur->eState>=CURSOR_REQUIRESEEK ){
72169 if( pCur->eState==CURSOR_FAULT ){
72170 assert( pCur->skipNext!=SQLITE_OK );
72171 return pCur->skipNext;
72175 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
72176 0, pCur->curPagerFlags);
72178 pCur->eState = CURSOR_INVALID;
72181 pCur->iPage = 0;
72182 pCur->curIntKey = pCur->pPage->intKey;
72184 pRoot = pCur->pPage;
72185 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
72187 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
72188 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
72189 ** NULL, the caller expects a table b-tree. If this is not the case,
72194 ** if pCur->iPage>=0). But this is not so if the database is corrupted
72195 ** in such a way that page pRoot is linked into a second b-tree table
72197 assert( pRoot->intKey==1 || pRoot->intKey==0 );
72198 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
72199 return SQLITE_CORRUPT_PAGE(pCur->pPage);
72203 pCur->ix = 0;
72204 pCur->info.nSize = 0;
72205 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
72207 if( pRoot->nCell>0 ){
72208 pCur->eState = CURSOR_VALID;
72209 }else if( !pRoot->leaf ){
72211 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
72212 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
72213 pCur->eState = CURSOR_VALID;
72216 pCur->eState = CURSOR_INVALID;
72223 ** Move the cursor down to the left-most leaf entry beneath the
72226 ** The left-most leaf is the one with the smallest key - the first
72235 assert( pCur->eState==CURSOR_VALID );
72236 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
72237 assert( pCur->ix<pPage->nCell );
72238 pgno = get4byte(findCell(pPage, pCur->ix));
72245 ** Move the cursor down to the right-most leaf entry beneath the
72248 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
72249 ** finds the right-most entry beneath the *page*.
72251 ** The right-most entry is the one with the largest key - the last
72260 assert( pCur->eState==CURSOR_VALID );
72261 while( !(pPage = pCur->pPage)->leaf ){
72262 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72263 pCur->ix = pPage->nCell;
72267 pCur->ix = pPage->nCell-1;
72268 assert( pCur->info.nSize==0 );
72269 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
72281 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72284 assert( pCur->pPage->nCell>0 );
72288 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72303 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72305 /* If the cursor already points to the last entry, this is a no-op. */
72306 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
72309 ** to the last entry in the b-tree. */
72311 for(ii=0; ii<pCur->iPage; ii++){
72312 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
72314 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
72315 testcase( pCur->ix!=pCur->pPage->nCell-1 );
72316 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
72317 assert( pCur->pPage->leaf );
72325 assert( pCur->eState==CURSOR_VALID );
72329 pCur->curFlags |= BTCF_AtLast;
72331 pCur->curFlags &= ~BTCF_AtLast;
72334 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72373 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72375 assert( pCur->pKeyInfo==0 );
72376 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
72380 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
72381 if( pCur->info.nKey==intKey ){
72385 if( pCur->info.nKey<intKey ){
72386 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
72387 *pRes = -1;
72394 if( pCur->info.nKey+1==intKey ){
72399 if( pCur->info.nKey==intKey ){
72410 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72416 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72417 *pRes = -1;
72422 assert( pCur->pPage );
72423 assert( pCur->pPage->isInit );
72424 assert( pCur->eState==CURSOR_VALID );
72425 assert( pCur->pPage->nCell > 0 );
72426 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
72427 assert( pCur->curIntKey );
72432 MemPage *pPage = pCur->pPage;
72435 /* pPage->nCell must be greater than zero. If this is the root-page
72437 ** not run. If this is not the root-page, then the moveToChild() routine
72439 ** be the right kind (index or table) of b-tree page. Otherwise
72441 assert( pPage->nCell>0 );
72442 assert( pPage->intKey );
72444 upr = pPage->nCell-1;
72446 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
72450 if( pPage->intKeyLeaf ){
72452 if( pCell>=pPage->aDataEnd ){
72460 if( lwr>upr ){ c = -1; break; }
72462 upr = idx-1;
72466 pCur->ix = (u16)idx;
72467 if( !pPage->leaf ){
72471 pCur->curFlags |= BTCF_ValidNKey;
72472 pCur->info.nKey = nCellKey;
72473 pCur->info.nSize = 0;
72481 assert( lwr==upr+1 || !pPage->leaf );
72482 assert( pPage->isInit );
72483 if( pPage->leaf ){
72484 assert( pCur->ix<pCur->pPage->nCell );
72485 pCur->ix = (u16)idx;
72491 if( lwr>=pPage->nCell ){
72492 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72496 pCur->ix = (u16)lwr;
72501 pCur->info.nSize = 0;
72502 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72507 ** Compare the "idx"-th cell on the page the cursor pCur is currently
72528 MemPage *pPage = pCur->pPage;
72534 if( nCell<=pPage->max1bytePayload ){
72535 /* This branch runs if the record-size field of the cell is a
72537 ** b-tree page. */
72538 testcase( pCell+nCell+1==pPage->aDataEnd );
72541 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72543 /* The record-size field is a 2 byte varint and the record
72544 ** fits entirely on the main b-tree page. */
72545 testcase( pCell+nCell+2==pPage->aDataEnd );
72556 ** Return true (non-zero) if pCur is current pointing to the last
72561 assert( pCur->eState==CURSOR_VALID );
72562 for(i=0; i<pCur->iPage; i++){
72563 MemPage *pPage = pCur->apPage[i];
72564 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
72592 ** The pIdxKey->eqSeen field is set to 1 if there
72604 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72606 assert( pCur->pKeyInfo!=0 );
72609 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72613 pIdxKey->errCode = 0;
72614 assert( pIdxKey->default_rc==1
72615 || pIdxKey->default_rc==0
72616 || pIdxKey->default_rc==-1
72631 if( pCur->eState==CURSOR_VALID
72632 && pCur->pPage->leaf
72636 if( pCur->ix==pCur->pPage->nCell-1
72637 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
72638 && pIdxKey->errCode==SQLITE_OK
72643 if( pCur->iPage>0
72645 && pIdxKey->errCode==SQLITE_OK
72647 pCur->curFlags &= ~BTCF_ValidOvfl;
72648 if( !pCur->pPage->isInit ){
72653 pIdxKey->errCode = SQLITE_OK;
72659 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72660 *pRes = -1;
72667 assert( pCur->pPage );
72668 assert( pCur->pPage->isInit );
72669 assert( pCur->eState==CURSOR_VALID );
72670 assert( pCur->pPage->nCell > 0 );
72671 assert( pCur->curIntKey==0 );
72676 MemPage *pPage = pCur->pPage;
72679 /* pPage->nCell must be greater than zero. If this is the root-page
72681 ** not run. If this is not the root-page, then the moveToChild() routine
72683 ** be the right kind (index or table) of b-tree page. Otherwise
72685 assert( pPage->nCell>0 );
72686 assert( pPage->intKey==0 );
72688 upr = pPage->nCell-1;
72694 /* The maximum supported page-size is 65536 bytes. This means that
72695 ** the maximum number of record bytes stored on an index B-Tree
72696 ** page is less than 16384 bytes and may be stored as a 2-byte
72699 ** stored entirely within the b-tree page by inspecting the first
72703 if( nCell<=pPage->max1bytePayload ){
72704 /* This branch runs if the record-size field of the cell is a
72706 ** b-tree page. */
72707 testcase( pCell+nCell+1==pPage->aDataEnd );
72710 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72712 /* The record-size field is a 2 byte varint and the record
72713 ** fits entirely on the main b-tree page. */
72714 testcase( pCell+nCell+2==pPage->aDataEnd );
72727 u8 * const pCellBody = pCell - pPage->childPtrSize;
72729 pPage->xParseCell(pPage, pCellBody, &pCur->info);
72730 nCell = (int)pCur->info.nKey;
72735 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
72744 pCur->ix = (u16)idx;
72747 pCur->curFlags &= ~BTCF_ValidOvfl;
72756 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
72757 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
72762 upr = idx-1;
72767 pCur->ix = (u16)idx;
72768 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
72775 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
72776 assert( pPage->isInit );
72777 if( pPage->leaf ){
72778 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
72779 pCur->ix = (u16)idx;
72784 if( lwr>=pPage->nCell ){
72785 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72789 pCur->ix = (u16)lwr;
72794 pCur->info.nSize = 0;
72795 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72812 return (CURSOR_VALID!=pCur->eState);
72825 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72830 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
72831 if( NEVER(pCur->pPage->leaf==0) ) return -1;
72833 n = pCur->pPage->nCell;
72834 for(i=0; i<pCur->iPage; i++){
72835 n *= pCur->apPage[i]->nCell;
72866 if( pCur->eState!=CURSOR_VALID ){
72867 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72872 if( CURSOR_INVALID==pCur->eState ){
72875 if( pCur->eState==CURSOR_SKIPNEXT ){
72876 pCur->eState = CURSOR_VALID;
72877 if( pCur->skipNext>0 ) return SQLITE_OK;
72881 pPage = pCur->pPage;
72882 idx = ++pCur->ix;
72883 if( !pPage->isInit || sqlite3FaultSim(412) ){
72887 ** in a corrupt database file where the table being DELETE-ed from
72889 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
72894 if( idx>=pPage->nCell ){
72895 if( !pPage->leaf ){
72896 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
72901 if( pCur->iPage==0 ){
72902 pCur->eState = CURSOR_INVALID;
72906 pPage = pCur->pPage;
72907 }while( pCur->ix>=pPage->nCell );
72908 if( pPage->intKey ){
72914 if( pPage->leaf ){
72925 pCur->info.nSize = 0;
72926 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72927 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
72928 pPage = pCur->pPage;
72929 if( (++pCur->ix)>=pPage->nCell ){
72930 pCur->ix--;
72933 if( pPage->leaf ){
72965 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
72966 assert( pCur->info.nSize==0 );
72967 if( pCur->eState!=CURSOR_VALID ){
72972 if( CURSOR_INVALID==pCur->eState ){
72975 if( CURSOR_SKIPNEXT==pCur->eState ){
72976 pCur->eState = CURSOR_VALID;
72977 if( pCur->skipNext<0 ) return SQLITE_OK;
72981 pPage = pCur->pPage;
72982 assert( pPage->isInit );
72983 if( !pPage->leaf ){
72984 int idx = pCur->ix;
72989 while( pCur->ix==0 ){
72990 if( pCur->iPage==0 ){
72991 pCur->eState = CURSOR_INVALID;
72996 assert( pCur->info.nSize==0 );
72997 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
72999 pCur->ix--;
73000 pPage = pCur->pPage;
73001 if( pPage->intKey && !pPage->leaf ){
73013 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
73014 pCur->info.nSize = 0;
73015 if( pCur->eState!=CURSOR_VALID
73016 || pCur->ix==0
73017 || pCur->pPage->leaf==0
73021 pCur->ix--;
73042 ** anywhere on the free-list, then it is guaranteed to be returned. If
73062 assert( sqlite3_mutex_held(pBt->mutex) );
73063 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
73064 pPage1 = pBt->pPage1;
73066 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
73068 n = get4byte(&pPage1->aData[36]);
73069 testcase( n==mxPage-1 );
73076 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
73079 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
73080 ** shows that the page 'nearby' is somewhere on the free-list, then
73081 ** the entire-list will be searched for that page.
73088 assert( pBt->autoVacuum );
73100 /* Decrement the free-list count by 1. Set iTrunk to the index of the
73101 ** first free-list trunk page. iPrevTrunk is initially 1.
73103 rc = sqlite3PagerWrite(pPage1->pDbPage);
73105 put4byte(&pPage1->aData[36], n-1);
73108 ** is not true. Otherwise, it runs once for each trunk-page on the
73109 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
73115 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
73118 iTrunk = get4byte(&pPrevTrunk->aData[0]);
73120 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
73123 iTrunk = get4byte(&pPage1->aData[32]);
73127 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
73136 assert( pTrunk->aData!=0 );
73137 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
73139 k = get4byte(&pTrunk->aData[4]);
73145 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73150 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73153 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73154 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
73168 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73174 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73176 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73180 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
73184 ** pointers to free-list leaves. The first leaf becomes a trunk
73188 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
73198 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
73203 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
73204 put4byte(&pNewTrunk->aData[4], k-1);
73205 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
73208 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
73209 put4byte(&pPage1->aData[32], iNewTrunk);
73211 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73215 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
73219 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73225 unsigned char *aData = pTrunk->aData;
73239 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
73241 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
73266 *pPgno, closest+1, k, pTrunk->pgno, n-1));
73267 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73269 if( closest<k-1 ){
73272 put4byte(&aData[4], k-1);
73276 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73293 ** pager layer with the 'no-content' flag set. This prevents the pager
73295 ** current transaction has already run one or more incremental-vacuum
73298 ** not set the no-content flag. This causes the pager to load and journal
73303 ** file on disk. So the effects of disabling the no-content optimization
73307 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
73309 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73311 pBt->nPage++;
73312 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
73315 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
73316 /* If *pPgno refers to a pointer-map page, allocate two new pages
73318 ** becomes a new pointer-map page, the second is used by the caller.
73321 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
73322 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
73323 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
73325 rc = sqlite3PagerWrite(pPg->pDbPage);
73329 pBt->nPage++;
73330 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
73333 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
73334 *pPgno = pBt->nPage;
73339 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73352 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
73353 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
73358 ** This function is used to add page iPage to the database file free-list.
73359 ** It is assumed that the page is not already a part of the free-list.
73370 MemPage *pTrunk = 0; /* Free-list trunk page */
73371 Pgno iTrunk = 0; /* Page number of free-list trunk page */
73372 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
73375 u32 nFree; /* Initial number of pages on free-list */
73377 assert( sqlite3_mutex_held(pBt->mutex) );
73379 assert( !pMemPage || pMemPage->pgno==iPage );
73381 if( iPage<2 || iPage>pBt->nPage ){
73386 sqlite3PagerRef(pPage->pDbPage);
73392 rc = sqlite3PagerWrite(pPage1->pDbPage);
73394 nFree = get4byte(&pPage1->aData[36]);
73395 put4byte(&pPage1->aData[36], nFree+1);
73397 if( pBt->btsFlags & BTS_SECURE_DELETE ){
73402 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
73406 memset(pPage->aData, 0, pPage->pBt->pageSize);
73409 /* If the database supports auto-vacuum, write an entry in the pointer-map
73417 /* Now manipulate the actual database free-list structure. There are two
73418 ** possibilities. If the free-list is currently empty, or if the first
73419 ** trunk page in the free-list is full, then this page will become a
73420 ** new free-list trunk page. Otherwise, it will become a leaf of the
73421 ** first trunk page in the current free-list. This block tests if it
73422 ** is possible to add the page as a new free-list leaf.
73427 iTrunk = get4byte(&pPage1->aData[32]);
73437 nLeaf = get4byte(&pTrunk->aData[4]);
73438 assert( pBt->usableSize>32 );
73439 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
73443 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
73448 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
73451 ** usableSize/4 - 8 entries will be reported as corrupt. In order
73453 ** we will continue to restrict the number of entries to usableSize/4 - 8
73456 ** to read "usableSize/4-2" instead of "usableSize/4-8".
73458 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
73463 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73465 put4byte(&pTrunk->aData[4], nLeaf+1);
73466 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
73467 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
73468 sqlite3PagerDontWrite(pPage->pDbPage);
73472 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
73478 ** the page being freed as a leaf page of the first trunk in the free-list.
73479 ** Possibly because the free-list is empty, or possibly because the
73480 ** first trunk in the free-list is full. Either way, the page being freed
73481 ** will become the new first trunk page in the free-list.
73486 rc = sqlite3PagerWrite(pPage->pDbPage);
73490 put4byte(pPage->aData, iTrunk);
73491 put4byte(&pPage->aData[4], 0);
73492 put4byte(&pPage1->aData[32], iPage);
73493 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
73497 pPage->isInit = 0;
73505 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
73523 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73524 assert( pInfo->nLocal!=pInfo->nPayload );
73525 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
73526 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
73527 if( pCell + pInfo->nSize > pPage->aDataEnd ){
73531 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
73532 pBt = pPage->pBt;
73533 assert( pBt->usableSize > 4 );
73534 ovflPageSize = pBt->usableSize - 4;
73535 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
73537 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
73539 while( nOvfl-- ){
73554 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
73561 ** freePage2() may zero the page contents if secure-delete mode is
73572 sqlite3PagerUnref(pOvfl->pDbPage);
73587 pPage->xParseCell(pPage, pCell, &sInfo); \
73602 ** Note that pCell does not necessary need to point to the pPage->aData
73604 ** be constructed in this temporary area then copied into pPage->aData
73624 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73628 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
73629 || sqlite3PagerIswriteable(pPage->pDbPage) );
73632 nHeader = pPage->childPtrSize;
73633 if( pPage->intKey ){
73634 nPayload = pX->nData + pX->nZero;
73635 pSrc = pX->pData;
73636 nSrc = pX->nData;
73637 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
73639 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
73641 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
73642 nSrc = nPayload = (int)pX->nKey;
73643 pSrc = pX->pKey;
73649 if( nPayload<=pPage->maxLocal ){
73660 memset(pPayload+nSrc, 0, nPayload-nSrc);
73667 mn = pPage->minLocal;
73668 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
73669 testcase( n==pPage->maxLocal );
73670 testcase( n==pPage->maxLocal+1 );
73671 if( n > pPage->maxLocal ) n = mn;
73677 pBt = pPage->pBt;
73694 pPage->xParseCell(pPage, pCell, &info);
73695 assert( nHeader==(int)(info.pPayload - pCell) );
73696 assert( info.nKey==pX->nKey );
73709 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
73713 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
73714 || sqlite3PagerIswriteable(pPage->pDbPage) );
73724 nPayload -= n;
73728 nSrc -= n;
73729 spaceLeft -= n;
73733 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
73734 if( pBt->autoVacuum ){
73744 /* If the database supports auto-vacuum, and the second or subsequent
73745 ** overflow page is being allocated, add an entry to the pointer-map
73749 ** to the pointer-map. If we write nothing to this pointer-map slot,
73754 if( pBt->autoVacuum && rc==SQLITE_OK ){
73769 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
73773 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
73774 || sqlite3PagerIswriteable(pPage->pDbPage) );
73779 pPrior = pOvfl->aData;
73781 pPayload = &pOvfl->aData[4];
73782 spaceLeft = pBt->usableSize - 4;
73790 ** Remove the i-th cell from pPage. This routine effects pPage only.
73799 u8 *data; /* pPage->aData */
73806 assert( idx<pPage->nCell );
73808 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73809 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73810 assert( pPage->nFree>=0 );
73811 data = pPage->aData;
73812 ptr = &pPage->aCellIdx[2*idx];
73813 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
73815 hdr = pPage->hdrOffset;
73817 if( pc<hdr+pPage->nCell*2 ){
73823 testcase( pc+sz==pPage->pBt->usableSize );
73824 if( pc+sz > pPage->pBt->usableSize ){
73833 pPage->nCell--;
73834 if( pPage->nCell==0 ){
73837 put2byte(&data[hdr+5], pPage->pBt->usableSize);
73838 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
73839 - pPage->childPtrSize - 8;
73841 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
73842 put2byte(&data[hdr+3], pPage->nCell);
73843 pPage->nFree += 2;
73854 ** in pPage->apOvfl[] and make it point to the cell content (either
73856 ** Allocating a new entry in pPage->aCell[] implies that
73857 ** pPage->nOverflow is incremented.
73863 int i, /* New cell becomes the i-th cell of the page */
73867 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
73873 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
73876 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
73877 assert( MX_CELL(pPage->pBt)<=10921 );
73878 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
73879 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
73880 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
73881 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73882 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
73883 assert( pPage->nFree>=0 );
73884 if( pPage->nOverflow || sz+2>pPage->nFree ){
73892 j = pPage->nOverflow++;
73893 /* Comparison against ArraySize-1 since we hold back one extra slot
73896 assert( j < ArraySize(pPage->apOvfl)-1 );
73897 pPage->apOvfl[j] = pCell;
73898 pPage->aiOvfl[j] = (u16)i;
73905 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
73906 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
73908 int rc = sqlite3PagerWrite(pPage->pDbPage);
73913 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73914 data = pPage->aData;
73915 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
73921 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
73922 assert( idx+sz <= (int)pPage->pBt->usableSize );
73923 pPage->nFree -= (u16)(2 + sz);
73930 memcpy(&data[idx+4], pCell+4, sz-4);
73935 pIns = pPage->aCellIdx + i*2;
73936 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
73938 pPage->nCell++;
73940 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
73941 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
73943 if( pPage->pBt->autoVacuum ){
73966 ** tunable - as if you could change them and recompile and it would all work.
73981 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
73993 ** -----------
73995 ** -----------
73998 ** --------- --------- ---------
73999 ** |Child-1| |Child-2| |Child-3|
74000 ** --------- --------- ---------
74004 ** 1. All cells from Child-1 in order
74006 ** 3. All cells from Child-2 in order
74008 ** 5. All cells from Child-3 in order
74010 ** For a table-btree (with rowids) the items 2 and 4 are empty because
74014 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
74018 ** ixNx[0] = Number of cells in Child-1.
74019 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
74020 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
74021 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
74024 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
74027 ** ixNx[0] = Number of cells in Child-1.
74028 ** ixNx[1] = Number of cells in Child-1 and Child-2.
74047 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
74051 assert( idx>=0 && idx+N<=p->nCell );
74053 assert( p->apCell[idx]!=0 );
74054 if( p->szCell[idx]==0 ){
74055 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
74058 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
74061 N--;
74069 assert( N>=0 && N<p->nCell );
74070 assert( p->szCell[N]==0 );
74071 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
74072 return p->szCell[N];
74075 assert( N>=0 && N<p->nCell );
74076 if( p->szCell[N] ) return p->szCell[N];
74081 ** Array apCell[] contains pointers to nCell b-tree page cells. The
74099 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
74100 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
74101 const int usableSize = pPg->pBt->usableSize;
74106 u8 *pCellptr = pPg->aCellIdx;
74107 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74109 int k; /* Current slot in pCArray->apEnd[] */
74110 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
74115 memcpy(&pTmp[j], &aData[j], usableSize - j);
74117 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
74118 pSrcEnd = pCArray->apEnd[k];
74122 u8 *pCell = pCArray->apCell[i];
74123 u16 sz = pCArray->szCell[i];
74127 pCell = &pTmp[pCell - aData];
74134 pData -= sz;
74135 put2byte(pCellptr, (pData - aData));
74139 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
74142 if( pCArray->ixNx[k]<=i ){
74144 pSrcEnd = pCArray->apEnd[k];
74148 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
74149 pPg->nCell = nCell;
74150 pPg->nOverflow = 0;
74153 put2byte(&aData[hdr+3], pPg->nCell);
74154 put2byte(&aData[hdr+5], pData - aData);
74160 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
74163 ** will fit), non-zero is returned. Otherwise, if the cells are added
74166 ** Argument pCellptr points to the first entry in the cell-pointer array
74168 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
74170 ** that it is safe to overwrite this part of the cell-pointer array.
74178 ** end of the space required by this page for the cell-pointer area (for
74179 ** all cells - not just those inserted by the current call). If the content
74181 ** cells in apCell[], then the cells do not fit and non-zero is returned.
74185 u8 *pBegin, /* End of cell-pointer array */
74186 u8 **ppData, /* IN/OUT: Page content-area pointer */
74187 u8 *pCellptr, /* Pointer to cell-pointer area */
74192 int i = iFirst; /* Loop counter - cell index to insert */
74193 u8 *aData = pPg->aData; /* Complete page */
74196 int k; /* Current slot in pCArray->apEnd[] */
74198 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
74200 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
74201 pEnd = pCArray->apEnd[k];
74205 assert( pCArray->szCell[i]!=0 );
74206 sz = pCArray->szCell[i];
74208 if( (pData - pBegin)<sz ) return 1;
74209 pData -= sz;
74212 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
74215 assert( (pSlot+sz)<=pCArray->apCell[i]
74216 || pSlot>=(pCArray->apCell[i]+sz)
74218 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
74219 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
74225 memmove(pSlot, pCArray->apCell[i], sz);
74226 put2byte(pCellptr, (pSlot - aData));
74230 if( pCArray->ixNx[k]<=i ){
74232 pEnd = pCArray->apEnd[k];
74240 ** The pCArray object contains pointers to b-tree cells and their sizes.
74243 ** that is currently stored within the body of pPg to the pPg free-list.
74244 ** The cell-pointers and other fields of the page are not updated.
74246 ** This function returns the total number of cells added to the free-list.
74254 u8 * const aData = pPg->aData;
74255 u8 * const pEnd = &aData[pPg->pBt->usableSize];
74256 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
74264 u8 *pCell = pCArray->apCell[i];
74270 sz = pCArray->szCell[i]; assert( sz>0 );
74273 assert( pFree>aData && (pFree - aData)<65536 );
74274 freeSpace(pPg, (u16)(pFree - aData), szFree);
74289 assert( pFree>aData && (pFree - aData)<65536 );
74290 freeSpace(pPg, (u16)(pFree - aData), szFree);
74297 ** balanced. The current page, pPg, has pPg->nCell cells starting with
74298 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
74304 ** The pPg->nFree field is invalid when this function returns. It is the
74314 u8 * const aData = pPg->aData;
74315 const int hdr = pPg->hdrOffset;
74316 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
74317 int nCell = pPg->nCell; /* Cells stored on pPg */
74321 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
74325 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74326 memcpy(pTmp, aData, pPg->pBt->usableSize);
74332 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
74334 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
74335 nCell -= nShift;
74338 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
74340 nCell -= nTail;
74345 if( pData>pPg->aDataEnd ) goto editpage_fail;
74349 int nAdd = MIN(nNew,iOld-iNew);
74350 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
74352 pCellptr = pPg->aCellIdx;
74362 for(i=0; i<pPg->nOverflow; i++){
74363 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
74365 pCellptr = &pPg->aCellIdx[iCell * 2];
74367 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
74380 pCellptr = &pPg->aCellIdx[nCell*2];
74383 iNew+nCell, nNew-nCell, pCArray
74386 pPg->nCell = nNew;
74387 pPg->nOverflow = 0;
74389 put2byte(&aData[hdr+3], pPg->nCell);
74390 put2byte(&aData[hdr+5], pData - aData);
74394 u8 *pCell = pCArray->apCell[i+iNew];
74395 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
74396 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
74397 pCell = &pTmp[pCell - aData];
74400 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
74415 ** a new entry is being inserted on the extreme right-end of the
74419 ** Instead of trying to balance the 3 right-most leaf pages, just add
74420 ** a new page to the right-hand side and put the one new entry in
74426 ** pPage is the leaf page which is the right-most page in the tree.
74428 ** which is also the right-most entry on the page.
74437 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
74442 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74443 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
74444 assert( pPage->nOverflow==1 );
74446 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
74447 assert( pPage->nFree>=0 );
74448 assert( pParent->nFree>=0 );
74450 /* Allocate a new page. This page will become the right-sibling of
74459 u8 *pCell = pPage->apOvfl[0];
74460 u16 szCell = pPage->xCellSize(pPage, pCell);
74464 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
74465 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
74471 b.apEnd[0] = pPage->aDataEnd;
74478 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
74480 /* If this is an auto-vacuum database, update the pointer map
74490 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
74491 if( szCell>pNew->minLocal ){
74497 ** consists of a 4-byte page number (the page number of pPage) and
74501 ** To find the largest key value on pPage, first find the right-most
74503 ** record-length (a variable length integer at most 32-bits in size)
74505 ** The first of the while(...) loops below skips over the record-length
74509 pCell = findCell(pPage, pPage->nCell-1);
74517 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
74518 0, pPage->pgno, &rc);
74521 /* Set the right-child pointer of pParent to point to the new page. */
74522 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
74536 ** for setting pointer-map entries.
74544 BtShared *pBt = pPage->pBt;
74545 assert( pPage->isInit );
74547 for(j=0; j<pPage->nCell; j++){
74552 pPage->xParseCell(pPage, z, &info);
74554 Pgno ovfl = get4byte(&z[info.nSize-4]);
74556 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
74558 if( !pPage->leaf ){
74561 assert( n==pPage->pgno && e==PTRMAP_BTREE );
74564 if( !pPage->leaf ){
74565 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74567 assert( n==pPage->pgno && e==PTRMAP_BTREE );
74575 ** This function is used to copy the contents of the b-tree node stored
74577 ** the pointer-map entries for each child page are updated so that the
74593 BtShared * const pBt = pFrom->pBt;
74594 u8 * const aFrom = pFrom->aData;
74595 u8 * const aTo = pTo->aData;
74596 int const iFromHdr = pFrom->hdrOffset;
74597 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
74602 assert( pFrom->isInit );
74603 assert( pFrom->nFree>=iToHdr );
74604 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
74606 /* Copy the b-tree node content from page pFrom to page pTo. */
74608 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
74609 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
74616 pTo->isInit = 0;
74624 /* If this is an auto-vacuum database, update the pointer-map entries
74625 ** for any b-tree or overflow pages that pTo now contains the pointers to.
74667 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
74676 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
74677 int isRoot, /* True if pParent is a root-page */
74685 int nxDiv; /* Next divider slot in pParent->aCell[] */
74690 int pageFlags; /* Value of pPage->aData[0] */
74696 u8 *pRight; /* Location in parent of right-sibling pointer */
74697 u8 *apDiv[NB-1]; /* Divider cells in pParent */
74698 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
74700 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
74711 pBt = pParent->pBt;
74712 assert( sqlite3_mutex_held(pBt->mutex) );
74713 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
74720 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
74721 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
74726 assert( pParent->nFree>=0 );
74739 i = pParent->nOverflow + pParent->nCell;
74747 nxDiv = i-2+bBulk;
74749 nxDiv = iParentIdx-1;
74751 i = 2-bBulk;
74754 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
74755 pRight = &pParent->aData[pParent->hdrOffset+8];
74757 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
74768 if( apOld[i]->nFree<0 ){
74775 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
74776 if( (i--)==0 ) break;
74778 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
74779 apDiv[i] = pParent->apOvfl[0];
74781 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74782 pParent->nOverflow = 0;
74784 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
74786 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74795 ** But not if we are in secure-delete mode. In secure-delete mode,
74800 if( pBt->btsFlags & BTS_FAST_SECURE ){
74805 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
74806 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
74808 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
74811 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
74815 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
74825 + pBt->pageSize; /* aSpace1 */
74827 assert( szScratch<=7*(int)pBt->pageSize );
74854 leafCorrection = b.pRef->leaf*4;
74855 leafData = b.pRef->intKeyLeaf;
74858 int limit = pOld->nCell;
74859 u8 *aData = pOld->aData;
74860 u16 maskPage = pOld->maskPage;
74861 u8 *piCell = aData + pOld->cellOffset;
74865 /* Verify that all sibling pages are of the same "type" (table-leaf,
74866 ** table-interior, index-leaf, or index-interior).
74868 if( pOld->aData[0]!=apOld[0]->aData[0] ){
74890 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
74891 if( pOld->nOverflow>0 ){
74892 if( NEVER(limit<pOld->aiOvfl[0]) ){
74896 limit = pOld->aiOvfl[0];
74902 for(k=0; k<pOld->nOverflow; k++){
74903 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
74904 b.apCell[b.nCell] = pOld->apOvfl[k];
74908 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
74915 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
74918 if( i<nOld-1 && !leafData){
74925 assert( sz<=pBt->maxLocal+23 );
74926 assert( iSpace1 <= (int)pBt->pageSize );
74930 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
74931 if( !pOld->leaf ){
74933 assert( pOld->hdrOffset==0 || CORRUPT_DB );
74936 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
74943 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
74955 ** size of all cells on the i-th page and cntNew[] which is the index
74962 ** szNew[i]: Spaced used on the i-th sibling page.
74964 ** the right of the i-th sibling page.
74968 usableSpace = pBt->usableSize - 12 + leafCorrection;
74971 b.apEnd[k] = p->aDataEnd;
74973 if( k && b.ixNx[k]==b.ixNx[k-1] ){
74974 k--; /* Omit b.ixNx[] entry for child pages with no cells */
74978 b.apEnd[k] = pParent->aDataEnd;
74981 assert( p->nFree>=0 );
74982 szNew[i] = usableSpace - p->nFree;
74983 for(j=0; j<p->nOverflow; j++){
74984 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
74995 szNew[k-1] = 0;
74996 cntNew[k-1] = b.nCell;
74998 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
74999 szNew[i] -= sz;
75008 cntNew[i]--;
75022 szNew[i+1] -= sz;
75026 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
75035 ** always nearly full, while the right-most sibling might be nearly empty.
75040 ** be so out of balance as to be illegal. For example, the right-most
75043 for(i=k-1; i>0; i--){
75045 int szLeft = szNew[i-1]; /* Size of sibling on the left */
75046 int r; /* Index of right-most cell in left sibling */
75049 r = cntNew[i-1] - 1;
75050 d = r + 1 - leafData;
75057 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
75061 szLeft -= b.szCell[r] + 2;
75062 cntNew[i-1] = r;
75063 r--;
75064 d--;
75067 szNew[i-1] = szLeft;
75068 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
75074 /* Sanity check: For a non-corrupt database file one of the follwing
75081 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
75083 apOld[0]->pgno, apOld[0]->nCell,
75084 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
75085 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
75091 pageFlags = apOld[0]->aData[0];
75097 rc = sqlite3PagerWrite(pNew->pDbPage);
75099 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
75114 /* Set the pointer-map entry for the new sibling page. */
75116 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
75137 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75138 aPgFlags[i] = apNew[i]->pDbPage->flags;
75162 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75164 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75165 apNew[i]->pgno = pgno;
75171 apNew[0]->pgno, szNew[0], cntNew[0],
75172 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
75173 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
75174 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
75175 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
75176 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
75177 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
75178 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
75179 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
75182 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
75184 assert( apNew[nNew-1]!=0 );
75185 put4byte(pRight, apNew[nNew-1]->pgno);
75187 /* If the sibling pages are not leaves, ensure that the right-child pointer
75188 ** of the right-most new sibling page is set to the value that was
75189 ** originally in the same field of the right-most old sibling page. */
75191 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
75192 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
75207 ** associated with the right-child of each sibling may also need to be
75214 int cntOldNext = pNew->nCell + pNew->nOverflow;
75225 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
75239 || pNew->pgno!=aPgno[iOld]
75240 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
75243 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
75245 if( cachedCellSize(&b,i)>pNew->minLocal ){
75254 for(i=0; i<nNew-1; i++){
75267 if( !pNew->leaf ){
75268 memcpy(&pNew->aData[8], pCell, 4);
75270 /* If the tree is a leaf-data tree, and the siblings are leaves,
75272 ** cell consists of the integer key for the right-most cell of
75273 ** the sibling-page assembled above only.
75276 j--;
75277 pNew->xParseCell(pNew, b.apCell[j], &info);
75282 pCell -= 4;
75283 /* Obscure case for non-leaf-data trees: If the cell at pCell was
75290 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
75296 sz = pParent->xCellSize(pParent, pCell);
75300 assert( sz<=pBt->maxLocal+23 );
75301 assert( iOvflSpace <= (int)pBt->pageSize );
75308 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
75310 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
75317 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
75319 ** the left-hand sibling apNew[iPg-1] has been updated.
75323 ** the right-hand sibling apNew[iPg+1] has been updated.
75327 ** The iPg value in the following loop starts at nNew-1 goes down
75328 ** to 0, then back up to nNew-1 again, thus making two passes over
75335 for(i=1-nNew; i<nNew; i++){
75336 int iPg = i<0 ? -i : i;
75340 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
75347 ** only after iPg-1 has already been updated. */
75348 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
75358 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
75359 iNew = cntNew[iPg-1] + !leafData;
75360 nNewCell = cntNew[iPg] - iNew;
75366 apNew[iPg]->nFree = usableSpace-szNew[iPg];
75367 assert( apNew[iPg]->nOverflow==0 );
75368 assert( apNew[iPg]->nCell==nNewCell );
75378 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
75379 /* The root page of the b-tree now contains no cells. The only sibling
75380 ** page is the right-child of the parent. Copy the contents of the
75382 ** b-tree structure by one. This is described as the "balance-shallower"
75383 ** sub-algorithm in some documentation.
75385 ** If this is an auto-vacuum database, the call to copyNodeContent()
75386 ** sets all pointer-map entries corresponding to database image pages
75395 rc = defragmentPage(apNew[0], -1);
75397 assert( apNew[0]->nFree ==
75398 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
75399 - apNew[0]->nCell*2)
75405 /* Fix the pointer map entries associated with the right-child of each
75409 u32 key = get4byte(&apNew[i]->aData[8]);
75410 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
75414 assert( pParent->isInit );
75425 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
75452 ** This function is called when the root page of a b-tree structure is
75457 ** page is then overwritten to make it an empty page with the right-child
75460 ** Before returning, all pointer-map entries corresponding to pages
75461 ** that the new child-page now contains pointers to are updated. The
75462 ** entry corresponding to the new right-child pointer of the root
75474 BtShared *pBt = pRoot->pBt; /* The BTree */
75476 assert( pRoot->nOverflow>0 );
75477 assert( sqlite3_mutex_held(pBt->mutex) );
75479 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
75480 ** page that will become the new right-child of pPage. Copy the contents
75483 rc = sqlite3PagerWrite(pRoot->pDbPage);
75485 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
75488 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
75496 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
75497 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
75498 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
75500 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
75503 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
75504 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
75505 memcpy(pChild->apOvfl, pRoot->apOvfl,
75506 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
75507 pChild->nOverflow = pRoot->nOverflow;
75509 /* Zero the contents of pRoot. Then install pChild as the right-child. */
75510 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
75511 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
75519 ** on the same B-tree as pCur.
75522 ** pointing to the same b-tree. If an insert occurs on one SQL table
75524 ** table linked to the same b-tree. If the secondary insert causes a
75530 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
75532 && pOther->eState==CURSOR_VALID
75533 && pOther->pPage==pCur->pPage
75561 MemPage *pPage = pCur->pPage;
75563 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
75564 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
75570 }else if( (iPage = pCur->iPage)==0 ){
75571 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
75572 /* The root page of the b-tree is overfull. In this case call the
75573 ** balance_deeper() function to create a new child for the root-page
75574 ** and copy the current contents of the root-page to it. The
75575 ** next iteration of the do-loop will balance the child page.
75579 rc = balance_deeper(pPage, &pCur->apPage[1]);
75581 pCur->iPage = 1;
75582 pCur->ix = 0;
75583 pCur->aiIdx[0] = 0;
75584 pCur->apPage[0] = pPage;
75585 pCur->pPage = pCur->apPage[1];
75586 assert( pCur->pPage->nOverflow );
75592 MemPage * const pParent = pCur->apPage[iPage-1];
75593 int const iIdx = pCur->aiIdx[iPage-1];
75595 rc = sqlite3PagerWrite(pParent->pDbPage);
75596 if( rc==SQLITE_OK && pParent->nFree<0 ){
75601 if( pPage->intKeyLeaf
75602 && pPage->nOverflow==1
75603 && pPage->aiOvfl[0]==pPage->nCell
75604 && pParent->pgno!=1
75605 && pParent->nCell==iIdx
75610 ** happens, the next iteration of the do-loop will balance pParent
75629 ** become overfull or underfull. The next iteration of the do-loop
75634 ** A subsequent iteration of the do-loop will deal with this by
75636 ** but it doesn't deal with overflow cells - just moves them to a
75643 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
75645 pCur->hints&BTREE_BULKLOAD);
75661 pPage->nOverflow = 0;
75663 /* The next iteration of the do-loop balances the parent page. */
75665 pCur->iPage--;
75666 assert( pCur->iPage>=0 );
75667 pCur->pPage = pCur->apPage[pCur->iPage];
75687 int nData = pX->nData - iOffset;
75693 int rc = sqlite3PagerWrite(pPage->pDbPage);
75695 memset(pDest + i, 0, iAmt - i);
75702 iAmt-nData);
75706 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
75707 int rc = sqlite3PagerWrite(pPage->pDbPage);
75713 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
75724 int iOffset; /* Next byte of pX->pData to write */
75725 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
75727 MemPage *pPage = pCur->pPage; /* Page being written */
75732 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
75733 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
75738 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
75739 0, pCur->info.nLocal);
75741 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
75744 iOffset = pCur->info.nLocal;
75747 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
75748 pBt = pPage->pBt;
75749 ovflPageSize = pBt->usableSize - 4;
75753 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
75757 ovflPgno = get4byte(pPage->aData);
75759 ovflPageSize = nTotal - iOffset;
75761 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
75764 sqlite3PagerUnref(pPage->pDbPage);
75787 ** If the seekResult parameter is non-zero, then a successful call to
75798 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
75799 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
75809 int loc = seekResult; /* -1: before desired location +1: after */
75813 Btree *p = pCur->pBtree;
75814 BtShared *pBt = p->pBt;
75819 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
75823 ** In some cases, the call to btreeMoveto() below is a no-op. For
75824 ** example, when inserting data into a table with auto-generated integer
75827 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
75832 if( pCur->curFlags & BTCF_Multiple ){
75833 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
75835 if( loc && pCur->iPage<0 ){
75848 if( pCur->eState>=CURSOR_REQUIRESEEK ){
75849 testcase( pCur->eState==CURSOR_REQUIRESEEK );
75850 testcase( pCur->eState==CURSOR_FAULT );
75856 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
75857 && pBt->inTransaction==TRANS_WRITE
75858 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
75859 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75862 ** expecting an index b-tree, then the caller should be inserting blob
75866 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
75868 if( pCur->pKeyInfo==0 ){
75869 assert( pX->pKey==0 );
75870 /* If this is an insert into a table b-tree, invalidate any incrblob
75872 if( p->hasIncrblobCur ){
75873 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
75881 assert( pCur->curFlags & BTCF_ValidNKey );
75882 assert( pX->nKey==pCur->info.nKey );
75891 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
75894 assert( pX->nData>=0 && pX->nZero>=0 );
75895 if( pCur->info.nSize!=0
75896 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
75907 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
75925 if( pX->nMem ){
75927 r.pKeyInfo = pCur->pKeyInfo;
75928 r.aMem = pX->aMem;
75929 r.nField = pX->nMem;
75934 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
75946 if( pCur->info.nKey==pX->nKey ){
75948 x2.pData = pX->pKey;
75949 x2.nData = pX->nKey;
75955 assert( pCur->eState==CURSOR_VALID
75956 || (pCur->eState==CURSOR_INVALID && loc) );
75958 pPage = pCur->pPage;
75959 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
75960 assert( pPage->leaf || !pPage->intKey );
75961 if( pPage->nFree<0 ){
75962 if( NEVER(pCur->eState>CURSOR_INVALID) ){
75963 /* ^^^^^--- due to the moveToRoot() call above */
75972 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
75974 assert( pPage->isInit || CORRUPT_DB );
75975 newCell = pBt->pTmpSpace;
75979 szNew = pBt->nPreformatSize;
75981 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
75983 pPage->xParseCell(pPage, newCell, &info);
75985 Pgno ovfl = get4byte(&newCell[szNew-4]);
75986 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
75993 assert( szNew==pPage->xCellSize(pPage, newCell) );
75995 idx = pCur->ix;
75999 if( idx>=pPage->nCell ){
76002 rc = sqlite3PagerWrite(pPage->pDbPage);
76007 if( !pPage->leaf ){
76011 testcase( pCur->curFlags & BTCF_ValidOvfl );
76014 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
76024 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
76026 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
76029 if( oldCell+szNew > pPage->aDataEnd ){
76037 }else if( loc<0 && pPage->nCell>0 ){
76038 assert( pPage->leaf );
76039 idx = ++pCur->ix;
76040 pCur->curFlags &= ~BTCF_ValidNKey;
76042 assert( pPage->leaf );
76045 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
76046 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
76060 ** multiple records into an intkey b-tree using a single cursor (as can
76063 ** the b-tree if possible. If the cursor is left pointing to the last
76068 pCur->info.nSize = 0;
76069 if( pPage->nOverflow ){
76071 pCur->curFlags &= ~(BTCF_ValidNKey);
76078 pCur->pPage->nOverflow = 0;
76079 pCur->eState = CURSOR_INVALID;
76082 if( pCur->pKeyInfo ){
76083 assert( pCur->pKey==0 );
76084 pCur->pKey = sqlite3Malloc( pX->nKey );
76085 if( pCur->pKey==0 ){
76088 memcpy(pCur->pKey, pX->pKey, pX->nKey);
76091 pCur->eState = CURSOR_REQUIRESEEK;
76092 pCur->nKey = pX->nKey;
76095 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
76118 BtShared *pBt = pDest->pBt;
76119 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
76125 if( pSrc->info.nPayload<0x80 ){
76126 *(aOut++) = pSrc->info.nPayload;
76128 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
76130 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
76131 nIn = pSrc->info.nLocal;
76132 aIn = pSrc->info.pPayload;
76133 if( aIn+nIn>pSrc->pPage->aDataEnd ){
76136 nRem = pSrc->info.nPayload;
76137 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
76139 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
76141 Pager *pSrcPager = pSrc->pBt->pPager;
76148 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
76149 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
76150 if( nOut<pSrc->info.nPayload ){
76152 pBt->nPreformatSize += 4;
76156 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
76159 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
76163 nRem -= nOut;
76169 nOut -= nCopy;
76170 nIn -= nCopy;
76182 nIn = pSrc->pBt->usableSize - 4;
76193 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
76198 pPgnoOut = pPageOut->aData;
76201 nOut = MIN(pBt->usableSize - 4, nRem);
76226 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
76231 Btree *p = pCur->pBtree;
76232 BtShared *pBt = p->pBt;
76242 assert( pBt->inTransaction==TRANS_WRITE );
76243 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
76244 assert( pCur->curFlags & BTCF_WriteFlag );
76245 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
76246 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
76248 if( pCur->eState!=CURSOR_VALID ){
76249 if( pCur->eState>=CURSOR_REQUIRESEEK ){
76251 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
76252 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
76257 assert( pCur->eState==CURSOR_VALID );
76259 iCellDepth = pCur->iPage;
76260 iCellIdx = pCur->ix;
76261 pPage = pCur->pPage;
76262 if( pPage->nCell<=iCellIdx ){
76266 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
76272 ** will cause a b-tree rebalance, then this is done by saving the cursor
76288 if( !pPage->leaf
76289 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
76290 (int)(pBt->usableSize*2/3)
76291 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
76293 /* A b-tree rebalance will be required after deleting this entry.
76307 ** sub-tree headed by the child page of the cell being deleted. This makes
76309 if( !pPage->leaf ){
76317 if( pCur->curFlags & BTCF_Multiple ){
76318 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
76322 /* If this is a delete operation to remove a row from a table b-tree,
76324 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
76325 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
76331 rc = sqlite3PagerWrite(pPage->pDbPage);
76338 ** is currently pointing to the largest entry in the sub-tree headed
76339 ** by the child-page of the cell that was just deleted from an internal
76342 if( !pPage->leaf ){
76343 MemPage *pLeaf = pCur->pPage;
76348 if( pLeaf->nFree<0 ){
76352 if( iCellDepth<pCur->iPage-1 ){
76353 n = pCur->apPage[iCellDepth+1]->pgno;
76355 n = pCur->pPage->pgno;
76357 pCell = findCell(pLeaf, pLeaf->nCell-1);
76358 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
76359 nCell = pLeaf->xCellSize(pLeaf, pCell);
76361 pTmp = pBt->pTmpSpace;
76363 rc = sqlite3PagerWrite(pLeaf->pDbPage);
76365 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
76367 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
76386 assert( pCur->pPage->nOverflow==0 );
76387 assert( pCur->pPage->nFree>=0 );
76388 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
76390 ** then balance() will always be a no-op. No need to invoke it. */
76395 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
76396 releasePageNotNull(pCur->pPage);
76397 pCur->iPage--;
76398 while( pCur->iPage>iCellDepth ){
76399 releasePage(pCur->apPage[pCur->iPage--]);
76401 pCur->pPage = pCur->apPage[pCur->iPage];
76407 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
76408 assert( pPage==pCur->pPage || CORRUPT_DB );
76409 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
76410 pCur->eState = CURSOR_SKIPNEXT;
76411 if( iCellIdx>=pPage->nCell ){
76412 pCur->skipNext = -1;
76413 pCur->ix = pPage->nCell-1;
76415 pCur->skipNext = 1;
76421 pCur->eState = CURSOR_REQUIRESEEK;
76441 BtShared *pBt = p->pBt;
76445 int ptfFlags; /* Page-type flage for the root page of new table */
76448 assert( pBt->inTransaction==TRANS_WRITE );
76449 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
76457 if( pBt->autoVacuum ){
76458 Pgno pgnoMove; /* Move a page here to make room for the root-page */
76463 ** out to be an overflow page, delete all overflow page-map caches
76469 ** root page of the new table should go. meta[3] is the largest root-page
76470 ** created so far, so the new root-page is (meta[3]+1).
76478 /* The new root-page may not be allocated on a pointer-map page, or the
76497 /* pgnoRoot is the page that will be used for the root-page of
76541 rc = sqlite3PagerWrite(pRoot->pDbPage);
76550 /* Update the pointer-map and meta-data with the new root-page number. */
76561 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
76573 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
76580 sqlite3PagerUnref(pRoot->pDbPage);
76581 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
76610 assert( sqlite3_mutex_held(pBt->mutex) );
76616 if( (pBt->openFlags & BTREE_SINGLE)==0
76617 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
76622 hdr = pPage->hdrOffset;
76623 for(i=0; i<pPage->nCell; i++){
76625 if( !pPage->leaf ){
76632 if( !pPage->leaf ){
76633 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
76635 if( pPage->intKey ) pnChange = 0;
76638 testcase( !pPage->intKey );
76639 *pnChange += pPage->nCell;
76643 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
76644 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
76666 BtShared *pBt = p->pBt;
76668 assert( p->inTrans==TRANS_WRITE );
76674 ** is the root of a table b-tree - if it is not, the following call is
76675 ** a no-op). */
76676 if( p->hasIncrblobCur ){
76691 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
76717 BtShared *pBt = p->pBt;
76720 assert( p->inTrans==TRANS_WRITE );
76740 if( pBt->autoVacuum ){
76745 /* If the table being dropped is the table with the largest root-page
76754 /* The table being dropped does not have the largest root-page
76756 ** gap left by the deleted root-page.
76779 /* Set the new 'max-root-page' value in the database header. This
76781 ** be a root-page number, less one again if that is the
76784 maxRootPgno--;
76787 maxRootPgno--;
76809 ** This function may only be called if the b-tree connection already
76812 ** Read the meta-information out of a database file. Meta[0]
76815 ** is read-only, the others are read/write.
76829 BtShared *pBt = p->pBt;
76832 assert( p->inTrans>TRANS_NONE );
76834 assert( pBt->pPage1 );
76838 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
76840 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
76843 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
76844 ** database, mark the database as read-only. */
76847 pBt->btsFlags |= BTS_READ_ONLY;
76855 ** Write meta-information back into the database. Meta[0] is
76856 ** read-only and may not be written.
76859 BtShared *pBt = p->pBt;
76864 assert( p->inTrans==TRANS_WRITE );
76865 assert( pBt->pPage1!=0 );
76866 pP1 = pBt->pPage1->aData;
76867 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
76872 assert( pBt->autoVacuum || iMeta==0 );
76874 pBt->incrVacuum = (u8)iMeta;
76883 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
76884 ** number of entries in the b-tree and write the result to *pnEntry.
76901 ** page in the B-Tree structure (not including overflow pages).
76903 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
76905 MemPage *pPage; /* Current page of the b-tree */
76907 /* If this is a leaf page or the tree is not an int-key tree, then
76911 pPage = pCur->pPage;
76912 if( pPage->leaf || !pPage->intKey ){
76913 nEntry += pPage->nCell;
76919 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
76921 ** to visit is the right-child of its parent.
76926 if( pPage->leaf ){
76928 if( pCur->iPage==0 ){
76929 /* All pages of the b-tree have been visited. Return successfully. */
76934 }while ( pCur->ix>=pCur->pPage->nCell );
76936 pCur->ix++;
76937 pPage = pCur->pPage;
76941 ** points at. This is the right-child if (iIdx==pPage->nCell).
76943 iIdx = pCur->ix;
76944 if( iIdx==pPage->nCell ){
76945 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76960 return p->pBt->pPager;
76973 if( !pCheck->mxErr ) return;
76974 pCheck->mxErr--;
76975 pCheck->nErr++;
76977 if( pCheck->errMsg.nChar ){
76978 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
76980 if( pCheck->zPfx ){
76981 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
76983 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
76985 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
76986 pCheck->bOomFault = 1;
76994 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
76998 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
76999 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
77006 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
77007 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
77013 ** reference to the page, add an error message to pCheck->zErrMsg.
77020 if( iPage>pCheck->nPage || iPage==0 ){
77028 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
77035 ** Check that the entry in the pointer-map for page iChild maps to
77049 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
77051 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
77076 int nErrAtStart = pCheck->nErr;
77077 while( iPage!=0 && pCheck->mxErr ){
77081 N--;
77082 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
77090 if( pCheck->pBt->autoVacuum ){
77094 if( n>pCheck->pBt->usableSize/4-2 ){
77097 N--;
77102 if( pCheck->pBt->autoVacuum ){
77108 N -= n;
77113 /* If this database supports auto-vacuum and iPage is not the last
77114 ** page in this overflow list, check that the pointer-map entry for
77117 if( pCheck->pBt->autoVacuum && N>0 ){
77126 if( N && nErrAtStart==pCheck->nErr ){
77130 expected-N, expected);
77136 ** An implementation of a min-heap.
77146 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
77173 aHeap[0]--;
77210 int depth = -1, d2; /* Depth of a subtree */
77226 u32 *heap = 0; /* Min-heap used for checking cell coverage */
77227 u32 x, prev = 0; /* Next and previous entry on the min-heap */
77228 const char *saved_zPfx = pCheck->zPfx;
77229 int saved_v1 = pCheck->v1;
77230 int saved_v2 = pCheck->v2;
77235 pBt = pCheck->pBt;
77236 usableSize = pBt->usableSize;
77239 pCheck->zPfx = "Page %u: ";
77240 pCheck->v1 = iPage;
77249 savedIsInit = pPage->isInit;
77250 pPage->isInit = 0;
77262 data = pPage->aData;
77263 hdr = pPage->hdrOffset;
77266 pCheck->zPfx = "On tree page %u cell %d: ";
77270 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
77273 assert( pPage->nCell==nCell );
77275 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
77276 ** immediately follows the b-tree page header. */
77277 cellStart = hdr + 12 - 4*pPage->leaf;
77278 assert( pPage->aCellIdx==&data[cellStart] );
77279 pCellIdx = &data[cellStart + 2*(nCell-1)];
77281 if( !pPage->leaf ){
77282 /* Analyze the right-child page of internal pages */
77285 if( pBt->autoVacuum ){
77286 pCheck->zPfx = "On page %u at right child: ";
77295 heap = pCheck->heap;
77299 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
77301 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
77305 pCheck->v2 = i;
77308 pCellIdx -= 2;
77309 if( pc<contentOffset || pc>usableSize-4 ){
77311 pc, contentOffset, usableSize-4);
77316 pPage->xParseCell(pPage, pCell, &info);
77324 if( pPage->intKey ){
77336 assert( pc + info.nSize - 4 <= usableSize );
77337 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
77338 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
77340 if( pBt->autoVacuum ){
77347 if( !pPage->leaf ){
77351 if( pBt->autoVacuum ){
77362 /* Populate the coverage-checking heap for leaf pages */
77363 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
77370 pCheck->zPfx = 0;
77371 if( doCoverageCheck && pCheck->mxErr>0 ){
77372 /* For leaf pages, the min-heap has already been initialized and the
77375 if( !pPage->leaf ){
77376 heap = pCheck->heap;
77378 for(i=nCell-1; i>=0; i--){
77381 size = pPage->xCellSize(pPage, &data[pc]);
77382 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
77385 /* Add the freeblocks to the min-heap
77387 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
77394 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
77397 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
77398 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
77399 ** big-endian integer which is the offset in the b-tree page of the next
77403 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
77406 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
77409 /* Analyze the min-heap looking for overlap between cells and/or
77412 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
77417 ** The loop below pulls entries from the min-heap in order and compares
77423 prev = contentOffset - 1; /* Implied first min-heap entry */
77430 nFrag += (x>>16) - (prev&0xffff) - 1;
77434 nFrag += usableSize - (prev&0xffff) - 1;
77435 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
77436 ** is stored in the fifth field of the b-tree page header.
77437 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
77448 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
77450 pCheck->zPfx = saved_zPfx;
77451 pCheck->v1 = saved_v1;
77452 pCheck->v2 = saved_v2;
77463 ** A read-only or read-write transaction must be opened before calling
77468 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
77472 ** root pages is incomplete. This is a "partial integrity-check". This
77490 BtShared *pBt = p->pBt;
77491 u64 savedDbFlags = pBt->db->flags;
77506 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
77507 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
77511 sCheck.pPager = pBt->pPager;
77532 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
77545 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
77546 get4byte(&pBt->pPage1->aData[36]));
77554 if( pBt->autoVacuum ){
77558 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
77565 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
77572 testcase( pBt->db->flags & SQLITE_CellSizeCk );
77573 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
77578 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
77584 pBt->db->flags = savedDbFlags;
77595 /* If the database supports auto-vacuum, make sure no tables contain
77596 ** references to pointer-map pages.
77599 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
77603 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
77622 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
77630 ** an empty string if the database is in-memory or a TEMP database.
77636 assert( p->pBt->pPager!=0 );
77637 return sqlite3PagerFilename(p->pBt->pPager, 1);
77649 assert( p->pBt->pPager!=0 );
77650 return sqlite3PagerJournalname(p->pBt->pPager);
77658 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
77659 return p ? p->inTrans : 0;
77667 ** transaction on the shared-cache the argument Btree is connected to.
77674 BtShared *pBt = p->pBt;
77676 if( pBt->inTransaction!=TRANS_NONE ){
77679 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
77692 assert( sqlite3_mutex_held(p->db->mutex) );
77693 return p->nBackup!=0;
77698 ** a single shared-btree. The memory is used by client code for its own
77699 ** purposes (for example, to store a high-level schema associated with
77700 ** the shared-btree). The btree layer manages reference counting issues.
77702 ** The first time this is called on a shared-btree, nBytes bytes of memory
77711 ** Just before the shared-btree is closed, the function passed as the
77717 BtShared *pBt = p->pBt;
77719 if( !pBt->pSchema && nBytes ){
77720 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
77721 pBt->xFreeSchema = xFree;
77724 return pBt->pSchema;
77734 assert( sqlite3_mutex_held(p->db->mutex) );
77751 assert( p->inTrans!=TRANS_NONE );
77752 if( p->sharable ){
77782 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
77783 assert( pCsr->curFlags & BTCF_Incrblob );
77789 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
77790 if( pCsr->eState!=CURSOR_VALID ){
77796 ** version of the b-tree page modified by the accessPayload call below.
77802 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
77808 ** (c) the connection holds a write-lock on the table (if required),
77809 ** (d) there are no conflicting read-locks, and
77812 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
77815 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
77816 && pCsr->pBt->inTransaction==TRANS_WRITE );
77817 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
77818 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
77819 assert( pCsr->pPage->intKey );
77828 pCur->curFlags |= BTCF_Incrblob;
77829 pCur->pBtree->hasIncrblobCur = 1;
77839 BtShared *pBt = pBtree->pBt;
77847 pBt->btsFlags &= ~BTS_NO_WAL;
77848 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
77852 u8 *aData = pBt->pPage1->aData;
77856 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77865 pBt->btsFlags &= ~BTS_NO_WAL;
77874 return (pCsr->hints & mask)!=0;
77878 ** Return true if the given Btree is read-only.
77881 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
77894 return p->sharable;
77903 testcase( p->sharable );
77904 return p->pBt->nRef;
77932 Btree *pDest; /* Destination b-tree file */
77934 int bDestLocked; /* True once a write-transaction is open on pDest */
77938 Btree *pSrc; /* Source b-tree file */
77956 ** structure may be accessed via two groups of thread-safe entry points:
77970 ** backup_pagecount() are not thread-safe functions. If they are called
77978 ** Non-sharable Btrees (in-memory databases for example), do not have
78014 return pDb->aDb[i].pBt;
78023 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
78028 ** Check that there is no open read-transaction on the b-tree passed as the
78030 ** is an open read-transaction, return SQLITE_ERROR and leave an error
78072 sqlite3_mutex_enter(pSrcDb->mutex);
78073 sqlite3_mutex_enter(pDestDb->mutex);
78082 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78093 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
78094 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
78095 p->pDestDb = pDestDb;
78096 p->pSrcDb = pSrcDb;
78097 p->iNext = 1;
78098 p->isAttached = 0;
78100 if( 0==p->pSrc || 0==p->pDest
78101 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
78113 p->pSrc->nBackup++;
78116 sqlite3_mutex_leave(pDestDb->mutex);
78117 sqlite3_mutex_leave(pSrcDb->mutex);
78141 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
78142 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
78143 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
78149 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
78150 assert( p->bDestLocked );
78151 assert( !isFatalError(p->rc) );
78152 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
78155 /* Catch the case where the destination is an in-memory database and the
78166 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
78169 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
78187 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
78199 ** this function is a no-op.
78219 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
78220 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78221 p->pNext = *pp;
78223 p->isAttached = 1;
78227 ** Copy nPage pages from the source b-tree to the destination.
78238 sqlite3_mutex_enter(p->pSrcDb->mutex);
78239 sqlite3BtreeEnter(p->pSrc);
78240 if( p->pDestDb ){
78241 sqlite3_mutex_enter(p->pDestDb->mutex);
78244 rc = p->rc;
78246 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
78247 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
78249 int nSrcPage = -1; /* Size of source db in pages */
78252 /* If the source pager is currently in a write-transaction, return
78255 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
78261 /* If there is no open read-transaction on the source database, open
78265 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
78266 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
78276 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
78281 if( SQLITE_OK==rc && p->bDestLocked==0
78282 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
78283 (int*)&p->iDestSchema))
78285 p->bDestLocked = 1;
78290 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
78291 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
78292 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
78297 /* Now that there is a read-lock on the source database, query the
78300 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
78302 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
78303 const Pgno iSrcPg = p->iNext; /* Source page number */
78304 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
78312 p->iNext++;
78315 p->nPagecount = nSrcPage;
78316 p->nRemaining = nSrcPage+1-p->iNext;
78317 if( p->iNext>(Pgno)nSrcPage ){
78319 }else if( !p->isAttached ){
78325 ** is to make sure that the schema-version really does change in
78331 rc = sqlite3BtreeNewDb(p->pDest);
78335 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
78338 if( p->pDestDb ){
78339 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
78342 rc = sqlite3BtreeSetVersion(p->pDest, 2);
78359 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
78360 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
78363 nDestTruncate = (nSrcPage+ratio-1)/ratio;
78364 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
78365 nDestTruncate--;
78373 /* If the source page-size is smaller than the destination page-size,
78379 ** pending-byte page in the source database may need to be
78392 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
78404 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
78448 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
78458 ** "committing" a read-only transaction cannot fail.
78462 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
78463 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
78470 p->rc = rc;
78472 if( p->pDestDb ){
78473 sqlite3_mutex_leave(p->pDestDb->mutex);
78475 sqlite3BtreeLeave(p->pSrc);
78476 sqlite3_mutex_leave(p->pSrcDb->mutex);
78490 pSrcDb = p->pSrcDb;
78491 sqlite3_mutex_enter(pSrcDb->mutex);
78492 sqlite3BtreeEnter(p->pSrc);
78493 if( p->pDestDb ){
78494 sqlite3_mutex_enter(p->pDestDb->mutex);
78498 if( p->pDestDb ){
78499 p->pSrc->nBackup--;
78501 if( p->isAttached ){
78502 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78505 pp = &(*pp)->pNext;
78508 *pp = p->pNext;
78512 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
78515 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
78516 if( p->pDestDb ){
78517 sqlite3Error(p->pDestDb, rc);
78520 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
78522 sqlite3BtreeLeave(p->pSrc);
78523 if( p->pDestDb ){
78524 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78544 return p->nRemaining;
78558 return p->nPagecount;
78580 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
78581 if( !isFatalError(p->rc) && iPage<p->iNext ){
78587 assert( p->pDestDb );
78588 sqlite3_mutex_enter(p->pDestDb->mutex);
78590 sqlite3_mutex_leave(p->pDestDb->mutex);
78593 p->rc = rc;
78596 }while( (p = p->pNext)!=0 );
78615 for(p=pBackup; p; p=p->pNext){
78616 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
78617 p->iNext = 1;
78639 if( pFd->pMethods ){
78652 b.pSrcDb = pFrom->db;
78661 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
78668 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
78706 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
78719 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
78725 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
78728 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
78730 if( p->flags & MEM_Null ){
78732 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
78739 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
78742 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
78743 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
78744 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
78747 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
78755 assert( (p->flags & MEM_Cleared)==0 );
78759 assert( p->szMalloc==0
78760 || (p->flags==MEM_Undefined
78761 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
78762 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
78772 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
78774 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
78775 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
78776 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
78777 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
78790 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
78792 if( p->flags & MEM_Int ){
78794 /* Work-around for GCC bug
78797 assert( (p->flags&MEM_Int)*2==sizeof(x) );
78798 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
78801 sqlite3Int64ToText(p->u.i, zBuf);
78806 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
78825 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
78839 if( (p->flags & MEM_Str)==0 ) return 1;
78840 if( p->flags & MEM_Term ){
78841 /* Insure that the string is properly zero-terminated. Pay particular
78842 ** attention to the case where p->n is odd */
78843 if( p->szMalloc>0 && p->z==p->zMalloc ){
78844 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
78845 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
78847 assert( p->z[p->n]==0 );
78848 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
78849 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
78851 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
78853 z = p->z;
78856 if( p->enc!=SQLITE_UTF8 ){
78858 if( p->enc==SQLITE_UTF16BE ) z++;
78875 ** routine is a no-op.
78889 if( !(pMem->flags&MEM_Str) ){
78890 pMem->enc = desiredEnc;
78893 if( pMem->enc==desiredEnc ){
78896 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
78906 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
78907 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
78913 ** Make sure pMem->z points to a writable allocation of at least n bytes.
78916 ** pMem->z into the new allocation. pMem must be either a string or
78918 ** in pMem->z is discarded.
78923 testcase( pMem->db==0 );
78927 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
78928 testcase( bPreserve && pMem->z==0 );
78930 assert( pMem->szMalloc==0
78931 || (pMem->flags==MEM_Undefined
78932 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
78933 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
78934 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
78935 if( pMem->db ){
78936 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
78938 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
78939 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
78940 pMem->z = pMem->zMalloc;
78944 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
78945 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
78947 if( pMem->zMalloc==0 ){
78949 pMem->z = 0;
78950 pMem->szMalloc = 0;
78953 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
78956 if( bPreserve && pMem->z ){
78957 assert( pMem->z!=pMem->zMalloc );
78958 memcpy(pMem->zMalloc, pMem->z, pMem->n);
78960 if( (pMem->flags&MEM_Dyn)!=0 ){
78961 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
78962 pMem->xDel((void *)(pMem->z));
78965 pMem->z = pMem->zMalloc;
78966 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
78971 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
78972 ** If pMem->zMalloc already meets or exceeds the requested size, this
78973 ** routine is a no-op.
78976 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
78985 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
78986 if( pMem->szMalloc<szNew ){
78989 assert( (pMem->flags & MEM_Dyn)==0 );
78990 pMem->z = pMem->zMalloc;
78991 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
79000 ** to be a double-zero byte at an even byte boundary in order to
79005 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
79008 pMem->z[pMem->n] = 0;
79009 pMem->z[pMem->n+1] = 0;
79010 pMem->z[pMem->n+2] = 0;
79011 pMem->flags |= MEM_Term;
79023 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79025 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
79027 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
79032 pMem->flags &= ~MEM_Ephem;
79034 pMem->pScopyFrom = 0;
79041 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
79048 assert( pMem->flags & MEM_Zero );
79049 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
79052 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79055 nByte = pMem->n + pMem->u.nZero;
79057 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
79063 assert( pMem->z!=0 );
79064 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
79066 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
79067 pMem->n += pMem->u.nZero;
79068 pMem->flags &= ~(MEM_Zero|MEM_Term);
79078 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79079 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
79080 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
79081 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
79106 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79107 assert( !(pMem->flags&MEM_Zero) );
79108 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
79109 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
79115 pMem->enc = 0;
79119 vdbeMemRenderNum(nByte, pMem->z, pMem);
79120 assert( pMem->z!=0 );
79121 pMem->n = sqlite3Strlen30NN(pMem->z);
79122 pMem->enc = SQLITE_UTF8;
79123 pMem->flags |= MEM_Str|MEM_Term;
79124 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
79142 assert( pMem->db!=0 );
79143 assert( pFunc->xFinalize!=0 );
79144 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
79145 assert( sqlite3_mutex_held(pMem->db->mutex) );
79149 t.db = pMem->db;
79154 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
79155 assert( (pMem->flags & MEM_Dyn)==0 );
79156 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
79173 assert( pFunc->xValue!=0 );
79174 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
79175 assert( pAccum->db!=0 );
79176 assert( sqlite3_mutex_held(pAccum->db->mutex) );
79182 ctx.enc = ENC(pAccum->db);
79183 pFunc->xValue(&ctx);
79198 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
79200 if( p->flags&MEM_Agg ){
79201 sqlite3VdbeMemFinalize(p, p->u.pDef);
79202 assert( (p->flags & MEM_Agg)==0 );
79203 testcase( p->flags & MEM_Dyn );
79205 if( p->flags&MEM_Dyn ){
79206 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
79207 p->xDel((void *)p->z);
79209 p->flags = MEM_Null;
79214 ** by p->xDel and memory in p->zMalloc.
79224 if( p->szMalloc ){
79225 sqlite3DbFreeNN(p->db, p->zMalloc);
79226 p->szMalloc = 0;
79228 p->z = 0;
79243 if( VdbeMemDynamic(p) || p->szMalloc ){
79253 if( p->szMalloc ) vdbeMemClear(p);
79257 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
79258 ** If the double is out of range of a 64-bit signed integer then
79259 ** return the closest available 64-bit signed integer.
79263 /* When floating-point is omitted, double and int64 are the same thing */
79268 ** minimum and maximum 64-bit integers, or they define them
79271 ** larger than a 32-bit integer constant.
79290 ** a floating-point then the value returned is the integer part.
79293 ** an SQL-NULL value, return 0.
79299 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
79305 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79307 flags = pMem->flags;
79310 return pMem->u.i;
79312 return doubleToInt64(pMem->u.r);
79313 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
79329 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
79334 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79336 if( pMem->flags & MEM_Real ){
79337 return pMem->u.r;
79338 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
79339 testcase( pMem->flags & MEM_IntReal );
79340 return (double)pMem->u.i;
79341 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
79354 testcase( pMem->flags & MEM_IntReal );
79355 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
79356 if( pMem->flags & MEM_Null ) return ifNull;
79367 assert( pMem->flags & MEM_Real );
79369 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79372 ix = doubleToInt64(pMem->u.r);
79376 ** (1) the round-trip conversion real->int->real is a no-op, and
79384 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
79385 pMem->u.i = ix;
79395 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79399 pMem->u.i = sqlite3VdbeIntValue(pMem);
79410 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79413 pMem->u.r = sqlite3VdbeRealValue(pMem);
79423 ** For some versions of GCC on 32-bit machines, if you do the more obvious
79425 ** though the r1 and (double)i values are bit-for-bit the same.
79431 && i >= -2251799813685248LL && i < 2251799813685248LL);
79444 testcase( pMem->flags & MEM_Int );
79445 testcase( pMem->flags & MEM_Real );
79446 testcase( pMem->flags & MEM_IntReal );
79447 testcase( pMem->flags & MEM_Null );
79448 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
79451 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
79452 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79453 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
79454 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79455 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
79457 pMem->u.i = ix;
79463 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
79464 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
79476 if( pMem->flags & MEM_Null ) return SQLITE_OK;
79479 if( (pMem->flags & MEM_Blob)==0 ){
79481 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
79482 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
79484 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
79503 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
79505 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
79506 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
79520 pMem->flags = flags;
79521 pMem->db = db;
79522 pMem->szMalloc = 0;
79542 pMem->flags = MEM_Null;
79556 pMem->flags = MEM_Blob|MEM_Zero;
79557 pMem->n = 0;
79559 pMem->u.nZero = n;
79560 pMem->enc = SQLITE_UTF8;
79561 pMem->z = 0;
79569 assert( pMem->z!=0 );
79570 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
79571 memset(pMem->z, 0, nByte);
79572 pMem->n = n>0?n:0;
79573 pMem->flags = MEM_Blob;
79574 pMem->enc = SQLITE_UTF8;
79582 ** a 64-bit integer.
79586 pMem->u.i = val;
79587 pMem->flags = MEM_Int;
79598 pMem->u.i = val;
79599 pMem->flags = MEM_Int;
79603 /* A no-op destructor */
79616 assert( pMem->flags==MEM_Null );
79618 pMem->u.zPType = zPType ? zPType : "";
79619 pMem->z = pPtr;
79620 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
79621 pMem->eSubtype = 'p';
79622 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
79633 pMem->u.r = val;
79634 pMem->flags = MEM_Real;
79645 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
79646 && pMem->xDel==sqlite3RowSetDelete;
79658 sqlite3 *db = pMem->db;
79665 pMem->z = (char*)p;
79666 pMem->flags = MEM_Blob|MEM_Dyn;
79667 pMem->xDel = sqlite3RowSetDelete;
79673 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
79676 assert( p->db!=0 );
79677 if( p->flags & (MEM_Str|MEM_Blob) ){
79678 int n = p->n;
79679 if( p->flags & MEM_Zero ){
79680 n += p->u.nZero;
79682 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
79693 ** This is used for testing and debugging only - to help ensure that shallow
79699 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
79700 if( pX->pScopyFrom==pMem ){
79702 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
79704 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
79712 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
79713 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
79716 ** undefined so that we can quickly detect the shallow-copy error */
79717 pX->flags = MEM_Undefined;
79718 pX->pScopyFrom = 0;
79721 pMem->pScopyFrom = 0;
79727 ** pTo are freed. The pFrom->z field is not duplicated. If
79728 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
79738 assert( pTo->db==pFrom->db );
79741 if( (pFrom->flags&MEM_Static)==0 ){
79742 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
79744 pTo->flags |= srcType;
79758 pTo->flags &= ~MEM_Dyn;
79759 if( pTo->flags&(MEM_Str|MEM_Blob) ){
79760 if( 0==(pFrom->flags&MEM_Static) ){
79761 pTo->flags |= MEM_Ephem;
79776 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
79777 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
79778 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
79782 pFrom->flags = MEM_Null;
79783 pFrom->szMalloc = 0;
79806 ** non-negative for blobs.
79815 i64 nByte = n; /* New value for pMem->n */
79817 u16 flags; /* New value for pMem->flags */
79820 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79830 if( pMem->db ){
79831 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
79852 sqlite3DbFree(pMem->db, (void*)z);
79858 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
79876 memcpy(pMem->z, z, nAlloc);
79879 pMem->z = (char *)z;
79881 pMem->zMalloc = pMem->z;
79882 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
79884 pMem->xDel = xDel;
79889 pMem->n = (int)(nByte & 0x7fffffff);
79890 pMem->flags = flags;
79891 pMem->enc = enc;
79910 ** pMem->zMalloc to hold the content from the btree, if possible. New
79911 ** pMem->zMalloc space will be allocated if necessary. The calling routine
79925 pMem->flags = MEM_Null;
79930 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
79932 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
79933 pMem->flags = MEM_Blob;
79934 pMem->n = (int)amt;
79955 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
79956 assert( pMem->z!=0 );
79959 pMem->flags = MEM_Blob|MEM_Ephem;
79960 pMem->n = (int)amt;
79971 ** to a zero-terminated version of that string.
79975 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
79978 assert( (pVal->flags & (MEM_Null))==0 );
79979 if( pVal->flags & (MEM_Blob|MEM_Str) ){
79981 pVal->flags |= MEM_Str;
79982 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
79985 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
79986 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
79991 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
79994 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
79996 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
79997 || pVal->db->mallocFailed );
79998 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
80000 return pVal->z;
80012 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
80018 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
80021 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
80023 return pVal->z;
80025 if( pVal->flags&MEM_Null ){
80037 p->flags = MEM_Null;
80038 p->db = db;
80059 ** Otherwise, if the second argument is non-zero, then this function is
80068 UnpackedRecord *pRec = p->ppRec[0];
80071 Index *pIdx = p->pIdx; /* Index being probed */
80074 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
80079 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
80080 if( pRec->pKeyInfo ){
80081 assert( pRec->pKeyInfo->nAllField==nCol );
80082 assert( pRec->pKeyInfo->enc==ENC(db) );
80083 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
80085 pRec->aMem[i].flags = MEM_Null;
80086 pRec->aMem[i].db = db;
80094 p->ppRec[0] = pRec;
80097 pRec->nField = p->iVal+1;
80098 return &pRec->aMem[p->iVal];
80145 assert( (p->flags & EP_TokenOnly)==0 );
80147 pList = p->x.pList;
80148 if( pList ) nVal = pList->nExpr;
80150 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
80152 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
80153 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
80165 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
80176 testcase( pCtx->pParse->rc==SQLITE_ERROR );
80177 testcase( pCtx->pParse->rc==SQLITE_OK );
80182 pFunc->xSFunc(&ctx, nVal, apVal);
80185 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
80192 pCtx->pParse->nErr++;
80195 pCtx->pParse->rc = rc;
80241 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
80242 if( op==TK_REGISTER ) op = pExpr->op2;
80248 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
80253 aff = sqlite3AffinityType(pExpr->u.zToken,0);
80254 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
80264 ** case when the value is -9223372036854775808.
80267 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
80268 pExpr = pExpr->pLeft;
80269 op = pExpr->op;
80270 negInt = -1;
80271 zNeg = "-";
80278 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
80280 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
80282 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
80289 assert( (pVal->flags & MEM_IntReal)==0 );
80290 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
80291 testcase( pVal->flags & MEM_Int );
80292 testcase( pVal->flags & MEM_Real );
80293 pVal->flags &= ~MEM_Str;
80299 /* This branch happens for multiple negative signs. Ex: -(-5) */
80300 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
80304 if( pVal->flags & MEM_Real ){
80305 pVal->u.r = -pVal->u.r;
80306 }else if( pVal->u.i==SMALLEST_INT64 ){
80308 pVal->u.r = -(double)SMALLEST_INT64;
80310 pVal->u.r = LARGEST_INT64;
80314 pVal->u.i = -pVal->u.i;
80327 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
80328 assert( pExpr->u.zToken[1]=='\'' );
80331 zVal = &pExpr->u.zToken[2];
80332 nVal = sqlite3Strlen30(zVal)-1;
80347 pVal->flags = MEM_Int;
80348 pVal->u.i = pExpr->u.zToken[4]==0;
80357 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
80418 sqlite3 *db = pParse->db;
80423 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
80429 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
80431 int iBindVar = pExpr->iColumn;
80432 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
80433 if( (v = pParse->pReprepare)!=0 ){
80436 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
80438 pVal->db = pParse->db;
80445 assert( pVal==0 || pVal->db==db );
80496 if( pExpr==0 || pExpr->op!=TK_SELECT ){
80507 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
80539 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
80581 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
80582 pMem->enc = ENC(db);
80594 int nCol = pRec->pKeyInfo->nAllField;
80595 Mem *aMem = pRec->aMem;
80600 sqlite3KeyInfoUnref(pRec->pKeyInfo);
80625 sqlite3DbFreeNN(((Mem*)v)->db, v);
80634 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
80638 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
80639 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
80640 return p->n;
80642 if( (p->flags & MEM_Blob)!=0 ){
80643 if( p->flags & MEM_Zero ){
80644 return p->n + p->u.nZero;
80646 return p->n;
80649 if( p->flags & MEM_Null ) return 0;
80680 sqlite3 *db = pParse->db;
80684 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
80685 p->db = db;
80686 if( db->pVdbe ){
80687 db->pVdbe->pPrev = p;
80689 p->pNext = db->pVdbe;
80690 p->pPrev = 0;
80691 db->pVdbe = p;
80692 assert( p->eVdbeState==VDBE_INIT_STATE );
80693 p->pParse = pParse;
80694 pParse->pVdbe = p;
80695 assert( pParse->aLabel==0 );
80696 assert( pParse->nLabel==0 );
80697 assert( p->nOpAlloc==0 );
80698 assert( pParse->szOpAlloc==0 );
80707 return p->pParse;
80715 sqlite3DbFree(p->db, p->zErrMsg);
80717 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
80726 p->prepFlags = prepFlags;
80728 p->expmask = 0;
80730 assert( p->zSql==0 );
80731 p->zSql = sqlite3DbStrNDup(p->db, z, n);
80736 ** Add a new element to the Vdbe->pDblStr list.
80742 sizeof(*pStr)+n+1-sizeof(pStr->z));
80744 pStr->pNextStr = p->pDblStr;
80745 p->pDblStr = pStr;
80746 memcpy(pStr->z, z, n+1);
80754 ** zId of length nId is a double-quoted identifier. Check to see if
80759 const char *zId /* The double-quoted identifier, already dequoted */
80763 if( pVdbe->pDblStr==0 ) return 0;
80764 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
80765 if( strcmp(zId, pStr->z)==0 ) return 1;
80777 assert( pA->db==pB->db );
80781 pTmp = pA->pNext;
80782 pA->pNext = pB->pNext;
80783 pB->pNext = pTmp;
80784 pTmp = pA->pPrev;
80785 pA->pPrev = pB->pPrev;
80786 pB->pPrev = pTmp;
80787 zTmp = pA->zSql;
80788 pA->zSql = pB->zSql;
80789 pB->zSql = zTmp;
80791 zTmp = pA->zNormSql;
80792 pA->zNormSql = pB->zNormSql;
80793 pB->zNormSql = zTmp;
80795 pB->expmask = pA->expmask;
80796 pB->prepFlags = pA->prepFlags;
80797 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
80798 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
80806 ** If an out-of-memory error occurs while resizing the array, return
80813 Parse *p = v->pParse;
80815 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
80823 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
80824 : (sqlite3_int64)v->nOpAlloc+nOp);
80826 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
80832 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
80833 sqlite3OomFault(p->db);
80838 assert( nNew>=(v->nOpAlloc+nOp) );
80839 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
80841 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
80842 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
80843 v->aOp = pNew;
80883 assert( p->nOpAlloc<=p->nOp );
80885 assert( p->nOpAlloc>p->nOp );
80892 i = p->nOp;
80893 assert( p->eVdbeState==VDBE_INIT_STATE );
80895 if( p->nOpAlloc<=i ){
80898 assert( p->aOp!=0 );
80899 p->nOp++;
80900 pOp = &p->aOp[i];
80902 pOp->opcode = (u8)op;
80903 pOp->p5 = 0;
80904 pOp->p1 = p1;
80905 pOp->p2 = p2;
80906 pOp->p3 = p3;
80907 pOp->p4.p = 0;
80908 pOp->p4type = P4_NOTUSED;
80910 pOp->zComment = 0;
80913 if( p->db->flags & SQLITE_VdbeAddopTrace ){
80914 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
80915 test_addop_breakpoint(i, &p->aOp[i]);
80919 pOp->cycles = 0;
80920 pOp->cnt = 0;
80923 pOp->iSrcLine = 0;
81017 Vdbe *v = pParse->pVdbe;
81022 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
81023 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
81025 assert( pParse->db->mallocFailed );
81026 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
81029 pCtx->pOut = 0;
81030 pCtx->pFunc = (FuncDef*)pFunc;
81031 pCtx->pVdbe = 0;
81032 pCtx->isError = 0;
81033 pCtx->argc = nArg;
81034 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
81066 if( pParse->addrExplain==0 ) return 0;
81067 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
81068 return pOp->p2;
81092 if( pParse->explain==2 )
81100 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
81102 v = pParse->pVdbe;
81103 iThis = v->nOp;
81104 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
81106 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
81108 pParse->addrExplain = iThis;
81118 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
81134 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
81135 sqlite3MayAbort(p->pParse);
81150 if( p->db->mallocFailed==0 ){
81151 VdbeOp *pOp = &p->aOp[addr];
81152 pOp->p4type = P4_INT32;
81153 pOp->p4.i = p4;
81158 /* Insert the end of a co-routine
81164 ** co-routine has its own independent set of registers, because co-routines
81166 ** that could cause problems if two or more co-routines are using the same
81169 v->pParse->nTempReg = 0;
81170 v->pParse->nRangeReg = 0;
81174 ** Create a new symbolic label for an instruction that has yet to be
81175 ** coded. The symbolic label is really just a negative number. The
81176 ** label can be used as the P2 value of an operation. Later, when
81177 ** the label is resolved to a specific address, the VDBE will scan
81179 ** the label into the resolved address.
81181 ** The VDBE knows that a P2 value is a label because labels are
81182 ** always negative and P2 values are suppose to be non-negative.
81183 ** Hence, a negative P2 value is a label that has yet to be resolved.
81189 ** Parse.aLabel[x] Stores the address that the x-th label resolves
81191 ** labels stores -1, but that is not required.
81199 return --pParse->nLabel;
81203 ** Resolve label "x" to be the address of the next instruction to
81208 int nNewSize = 10 - p->nLabel;
81209 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
81210 nNewSize*sizeof(p->aLabel[0]));
81211 if( p->aLabel==0 ){
81212 p->nLabelAlloc = 0;
81216 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
81218 p->nLabelAlloc = nNewSize;
81219 p->aLabel[j] = v->nOp;
81223 Parse *p = v->pParse;
81225 assert( v->eVdbeState==VDBE_INIT_STATE );
81226 assert( j<-p->nLabel );
81229 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81230 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
81233 if( p->nLabelAlloc + p->nLabel < 0 ){
81236 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
81237 p->aLabel[j] = v->nOp;
81253 for(i=1; ALWAYS(i<p->nOp); i++){
81254 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
81255 p->aOp[1].opcode = OP_Noop;
81265 ** in a Vdbe main program and each of the sub-programs (triggers) it may
81276 ** sqlite3DbFree(v->db, sIter.apSub);
81285 int iSub; /* 0 = main program, 1 = first sub-program etc. */
81288 Vdbe *v = p->v;
81293 if( p->iSub<=p->nSub ){
81295 if( p->iSub==0 ){
81296 aOp = v->aOp;
81297 nOp = v->nOp;
81299 aOp = p->apSub[p->iSub-1]->aOp;
81300 nOp = p->apSub[p->iSub-1]->nOp;
81302 assert( p->iAddr<nOp );
81304 pRet = &aOp[p->iAddr];
81305 p->iAddr++;
81306 if( p->iAddr==nOp ){
81307 p->iSub++;
81308 p->iAddr = 0;
81311 if( pRet->p4type==P4_SUBPROGRAM ){
81312 int nByte = (p->nSub+1)*sizeof(SubProgram*);
81314 for(j=0; j<p->nSub; j++){
81315 if( p->apSub[j]==pRet->p4.pProgram ) break;
81317 if( j==p->nSub ){
81318 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
81319 if( !p->apSub ){
81322 p->apSub[p->nSub++] = pRet->p4.pProgram;
81335 ** sub-programs contains any of the following:
81352 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
81368 int opcode = pOp->opcode;
81374 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
81379 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
81383 ** where a "DELETE FROM tbl" has a statement-journal but does not
81384 ** require one. This is not so bad - it is an inefficiency, not a bug. */
81385 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
81390 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
81395 sqlite3DbFree(v->db, sIter.apSub);
81402 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
81406 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
81415 || (pC->eCurType!=CURTYPE_SORTER
81416 && pC->eCurType!=CURTYPE_PSEUDO
81417 && !pC->isEphemeral)
81419 p->nWrite++;
81430 assert( p->nWrite==0 || p->usesStmtJournal );
81438 ** (1) For each jump instruction with a negative P2 value (a label)
81458 Parse *pParse = p->pParse;
81459 int *aLabel = pParse->aLabel;
81460 p->readOnly = 1;
81461 p->bIsReader = 0;
81462 pOp = &p->aOp[p->nOp-1];
81471 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
81474 switch( pOp->opcode ){
81476 if( pOp->p2!=0 ) p->readOnly = 0;
81481 p->bIsReader = 1;
81489 p->readOnly = 0;
81490 p->bIsReader = 1;
81495 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
81500 assert( (pOp - p->aOp) >= 3 );
81501 assert( pOp[-1].opcode==OP_Integer );
81502 n = pOp[-1].p1;
81509 if( pOp->p2<0 ){
81511 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81512 ** have non-negative values for P2. */
81513 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
81514 assert( ADDR(pOp->p2)<-pParse->nLabel );
81515 pOp->p2 = aLabel[ADDR(pOp->p2)];
81521 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81522 ** have non-negative values for P2. */
81523 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
81525 if( pOp==p->aOp ) break;
81526 pOp--;
81529 sqlite3DbFreeNN(p->db, pParse->aLabel);
81530 pParse->aLabel = 0;
81532 pParse->nLabel = 0;
81534 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
81549 ** A jump to an unresolved label means that the jump destination will be
81559 Vdbe *v, /* The byte-code program under construction */
81569 pParse = v->pParse;
81571 if( pParse->nErr ) return;
81573 assert( iLast<v->nOp );
81574 pOp = &v->aOp[iFirst];
81576 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
81577 int iDest = pOp->p2; /* Jump destination */
81579 if( pOp->opcode==OP_Gosub ) continue;
81583 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
81586 iDest = pParse->aLabel[j];
81590 for(; j<v->nOp; j++){
81591 VdbeOp *pX = &v->aOp[j];
81592 if( pX->opcode==OP_Return ){
81593 if( pX->p1==iRetReg ) break;
81596 if( pX->opcode==OP_Noop ) continue;
81597 if( pX->opcode==OP_Explain ) continue;
81625 assert( p->eVdbeState==VDBE_INIT_STATE );
81626 return p->nOp;
81635 ** sqlite3VdbeAddOpList() will always be non-NULL.
81639 assert( p->nOp + N <= p->nOpAlloc );
81653 for(i=0; i<p->nOp; i++){
81654 assert( p->aOp[i].opcode!=OP_ResultRow );
81682 VdbeOp *aOp = p->aOp;
81683 assert( aOp && !p->db->mallocFailed );
81686 assert( DbMaskAllZero(p->btreeMask) );
81689 *pnOp = p->nOp;
81690 p->aOp = 0;
81698 ** Non-zero P2 arguments to jump instructions are automatically adjusted
81705 int iLineno /* Source-file line number of first opcode */
81710 assert( p->eVdbeState==VDBE_INIT_STATE );
81711 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
81714 pFirst = pOut = &p->aOp[p->nOp];
81716 pOut->opcode = aOp->opcode;
81717 pOut->p1 = aOp->p1;
81718 pOut->p2 = aOp->p2;
81719 assert( aOp->p2>=0 );
81720 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
81721 pOut->p2 += p->nOp;
81723 pOut->p3 = aOp->p3;
81724 pOut->p4type = P4_NOTUSED;
81725 pOut->p4.p = 0;
81726 pOut->p5 = 0;
81728 pOut->zComment = 0;
81731 pOut->iSrcLine = iLineno+i;
81736 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81737 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
81741 p->nOp += nOp;
81757 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
81759 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
81761 ScanStatus *pNew = &aNew[p->nScan++];
81762 pNew->addrExplain = addrExplain;
81763 pNew->addrLoop = addrLoop;
81764 pNew->addrVisit = addrVisit;
81765 pNew->nEst = nEst;
81766 pNew->zName = sqlite3DbStrDup(p->db, zName);
81767 p->aScan = aNew;
81778 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
81781 sqlite3VdbeGetOp(p,addr)->p1 = val;
81784 sqlite3VdbeGetOp(p,addr)->p2 = val;
81787 sqlite3VdbeGetOp(p,addr)->p3 = val;
81790 assert( p->nOp>0 || p->db->mallocFailed );
81791 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
81799 sqlite3VdbeChangeP2(p, addr, p->nOp);
81805 ** the previous opcode (and is thus a no-op) then simply back up
81810 ** strives to omit useless byte-code like this:
81816 if( addr==p->nOp-1 ){
81817 assert( p->aOp[addr].opcode==OP_Once
81818 || p->aOp[addr].opcode==OP_If
81819 || p->aOp[addr].opcode==OP_FkIfZero );
81820 assert( p->aOp[addr].p4type==0 );
81822 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
81824 p->nOp--;
81826 sqlite3VdbeChangeP2(p, addr, p->nOp);
81836 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
81845 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
81849 freeEphemeralFunction(db, p->pFunc);
81867 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
81881 if( db->pnBytesFreed==0 ){
81889 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
81903 Op *pOp = &aOp[nOp-1];
81905 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
81907 sqlite3DbFree(db, pOp->zComment);
81910 pOp--;
81918 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
81922 p->pNext = pVdbe->pProgram;
81923 pVdbe->pProgram = p;
81930 return pVdbe->pProgram!=0;
81938 if( p->db->mallocFailed ) return 0;
81939 assert( addr>=0 && addr<p->nOp );
81940 pOp = &p->aOp[addr];
81941 freeP4(p->db, pOp->p4type, pOp->p4.p);
81942 pOp->p4type = P4_NOTUSED;
81943 pOp->p4.z = 0;
81944 pOp->opcode = OP_Noop;
81953 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
81954 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
81972 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
81973 assert( pParse->pVdbe );
81975 assert( iFirst+N-1<=pParse->nMem );
81980 N--;
81982 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
81983 mask &= ~MASKBIT32(N-1);
81984 N--;
81988 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
81989 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
82018 if( pOp->p4type ){
82019 freeP4(p->db, pOp->p4type, pOp->p4.p);
82020 pOp->p4type = 0;
82021 pOp->p4.p = 0;
82024 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
82027 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
82028 pOp->p4type = P4_DYNAMIC;
82035 db = p->db;
82036 assert( p->eVdbeState==VDBE_INIT_STATE );
82037 assert( p->aOp!=0 || db->mallocFailed );
82038 if( db->mallocFailed ){
82042 assert( p->nOp>0 );
82043 assert( addr<p->nOp );
82045 addr = p->nOp - 1;
82047 pOp = &p->aOp[addr];
82048 if( n>=0 || pOp->p4type ){
82055 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
82056 pOp->p4type = P4_INT32;
82059 pOp->p4.p = (void*)zP4;
82060 pOp->p4type = (signed char)n;
82067 ** to the value defined by the arguments. This is a high-speed
82078 if( p->db->mallocFailed ){
82079 freeP4(p->db, n, pP4);
82082 assert( p->nOp>0 );
82083 pOp = &p->aOp[p->nOp-1];
82084 assert( pOp->p4type==P4_NOTUSED );
82085 pOp->p4type = n;
82086 pOp->p4.p = pP4;
82095 Vdbe *v = pParse->pVdbe;
82106 ** insert a No-op and add the comment to that new instruction. This
82111 assert( p->nOp>0 || p->aOp==0 );
82112 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
82113 if( p->nOp ){
82114 assert( p->aOp );
82115 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
82116 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
82143 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
82148 ** Return the opcode for a given address. If the address is -1, then
82164 assert( p->eVdbeState==VDBE_INIT_STATE );
82166 addr = p->nOp - 1;
82168 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
82169 if( p->db->mallocFailed ){
82172 return &p->aOp[addr];
82182 if( c=='1' ) return pOp->p1;
82183 if( c=='2' ) return pOp->p2;
82184 if( c=='3' ) return pOp->p3;
82185 if( c=='4' ) return pOp->p4.i;
82186 return pOp->p5;
82197 ** "PX" -> "r[X]"
82198 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
82199 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
82200 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
82203 sqlite3 *db, /* Optional - Oom error reporting only */
82215 zOpName = sqlite3OpcodeName(pOp->opcode);
82231 if( pOp->zComment && pOp->zComment[0] ){
82232 sqlite3_str_appendall(&x, pOp->zComment);
82249 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
82252 sqlite3_context *pCtx = pOp->p4.pCtx;
82253 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
82255 }else if( pCtx->argc>1 ){
82256 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
82259 x.nChar -= 2;
82265 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
82274 if( !seenCom && pOp->zComment ){
82275 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
82277 }else if( pOp->zComment ){
82278 sqlite3_str_appendall(&x, pOp->zComment);
82294 switch( pExpr->op ){
82297 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
82300 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
82306 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
82310 if( pExpr->iColumn<0 ){
82313 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
82351 displayP4Expr(p, pExpr->pLeft);
82352 if( pExpr->pRight ){
82354 displayP4Expr(p, pExpr->pRight);
82372 switch( pOp->p4type ){
82375 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
82376 assert( pKeyInfo->aSortFlags!=0 );
82377 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
82378 for(j=0; j<pKeyInfo->nKeyField; j++){
82379 CollSeq *pColl = pKeyInfo->aColl[j];
82380 const char *zColl = pColl ? pColl->zName : "";
82383 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
82384 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
82392 displayP4Expr(&x, pOp->p4.pExpr);
82398 CollSeq *pColl = pOp->p4.pColl;
82399 assert( pColl->enc<4 );
82400 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
82401 encnames[pColl->enc]);
82405 FuncDef *pDef = pOp->p4.pFunc;
82406 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82410 FuncDef *pDef = pOp->p4.pCtx->pFunc;
82411 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82415 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
82419 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
82423 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
82427 Mem *pMem = pOp->p4.pMem;
82428 if( pMem->flags & MEM_Str ){
82429 zP4 = pMem->z;
82430 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
82431 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
82432 }else if( pMem->flags & MEM_Real ){
82433 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
82434 }else if( pMem->flags & MEM_Null ){
82437 assert( pMem->flags & MEM_Blob );
82444 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
82451 u32 *ai = pOp->p4.ai;
82465 zP4 = pOp->p4.pTab->zName;
82469 zP4 = pOp->p4.z;
82481 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
82485 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
82486 ** p->btreeMask of databases that will require a lock.
82489 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
82490 assert( i<(int)sizeof(p->btreeMask)*8 );
82491 DbMaskSet(p->btreeMask, i);
82492 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
82493 DbMaskSet(p->lockMask, i);
82499 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
82503 ** that the correct busy-handler callback is invoked if required.
82505 ** If SQLite is not threadsafe but does support shared-cache mode, then
82510 ** If SQLite is not threadsafe and does not support shared-cache mode, this
82511 ** function is a no-op.
82513 ** The p->btreeMask field is a bitmask of all btrees that the prepared
82514 ** statement p will ever use. Let N be the number of bits in p->btreeMask
82524 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
82525 db = p->db;
82526 aDb = db->aDb;
82527 nDb = db->nDb;
82529 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
82545 db = p->db;
82546 aDb = db->aDb;
82547 nDb = db->nDb;
82549 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
82555 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
82568 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
82582 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
82583 zP4 ? zP4 : "", pOp->p5,
82596 ** This is a high-runner, so only those fields that really do need to
82611 p->flags = flags;
82612 p->db = db;
82613 p->szMalloc = 0;
82615 p->pScopyFrom = 0;
82618 }while( (--N)>0 );
82633 sqlite3 *db = p->db;
82634 if( db->pnBytesFreed ){
82636 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
82650 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
82651 ** sqlite3MemRelease() were called from here. With -O2, this jumps
82656 testcase( p->flags & MEM_Agg );
82657 testcase( p->flags & MEM_Dyn );
82658 if( p->flags&(MEM_Agg|MEM_Dyn) ){
82659 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
82661 p->flags = MEM_Undefined;
82662 }else if( p->szMalloc ){
82663 sqlite3DbFreeNN(db, p->zMalloc);
82664 p->szMalloc = 0;
82665 p->flags = MEM_Undefined;
82669 p->flags = MEM_Undefined;
82684 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
82700 pFrame->pParent = pFrame->v->pDelFrame;
82701 pFrame->v->pDelFrame = pFrame;
82721 int nSub = 0; /* Number of sub-vdbes seen so far */
82722 SubProgram **apSub = 0; /* Array of sub-vdbes */
82733 ** encountered, but p->pc will eventually catch up to nRow.
82735 nRow = p->nOp;
82737 if( pSub->flags&MEM_Blob ){
82740 nSub = pSub->n/sizeof(Vdbe*);
82741 apSub = (SubProgram **)pSub->z;
82744 nRow += apSub[i]->nOp;
82751 p->rc = SQLITE_OK;
82755 if( i<p->nOp ){
82758 aOp = p->aOp;
82763 i -= p->nOp;
82766 for(j=0; i>=apSub[j]->nOp; j++){
82767 i -= apSub[j]->nOp;
82768 assert( i<apSub[j]->nOp || j+1<nSub );
82770 aOp = apSub[j]->aOp;
82775 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
82785 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
82786 if( p->rc!=SQLITE_OK ){
82790 apSub = (SubProgram **)pSub->z;
82793 pSub->n = nSub*sizeof(SubProgram*);
82794 nRow += aOp[i].p4.pProgram->nOp;
82801 if( pOp->opcode==OP_OpenRead ) break;
82802 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
82803 if( pOp->opcode==OP_ReopenIdx ) break;
82827 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
82829 for(i=0; i<p->nChildCsr; i++){
82830 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
82832 releaseMemArray(aMem, p->nChildMem);
82833 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
82834 sqlite3DbFree(p->v->db, p);
82845 ** When p->explain==1, each instruction is listed. When
82846 ** p->explain==2, only OP_Explain instructions are listed and these
82847 ** are shown in a different format. p->explain==2 is used to implement
82849 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
82853 ** When p->explain==1, first the main program is listed, then each of
82860 sqlite3 *db = p->db; /* The database connection */
82863 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
82864 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
82868 assert( p->explain );
82869 assert( p->eVdbeState==VDBE_RUN_STATE );
82870 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
82874 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
82877 p->pResultSet = 0;
82879 if( p->rc==SQLITE_NOMEM ){
82891 assert( p->nMem>9 );
82892 pSub = &p->aMem[9];
82898 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
82902 if( AtomicLoad(&db->u1.isInterrupted) ){
82903 p->rc = SQLITE_INTERRUPT;
82905 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
82908 if( p->explain==2 ){
82909 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
82910 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
82911 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
82912 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
82913 p->nResColumn = 4;
82916 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
82917 -1, SQLITE_UTF8, SQLITE_STATIC);
82918 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
82919 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
82920 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
82922 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
82926 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
82931 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
82932 p->nResColumn = 8;
82934 p->pResultSet = pMem;
82935 if( db->mallocFailed ){
82936 p->rc = SQLITE_NOMEM;
82939 p->rc = SQLITE_OK;
82954 if( p->zSql ){
82955 z = p->zSql;
82956 }else if( p->nOp>=1 ){
82957 const VdbeOp *pOp = &p->aOp[0];
82958 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
82959 z = pOp->p4.z;
82972 int nOp = p->nOp;
82976 pOp = &p->aOp[0];
82977 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
82980 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
82984 if( z[i-1]!=' ' ){
83007 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
83026 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
83029 if( nByte <= p->nFree ){
83030 p->nFree -= nByte;
83031 pBuf = &p->pSpace[p->nFree];
83033 p->nNeeded += nByte;
83049 assert( p->eVdbeState==VDBE_INIT_STATE
83050 || p->eVdbeState==VDBE_READY_STATE
83051 || p->eVdbeState==VDBE_HALT_STATE );
83055 assert( p->nOp>0 );
83057 p->eVdbeState = VDBE_READY_STATE;
83060 for(i=0; i<p->nMem; i++){
83061 assert( p->aMem[i].db==p->db );
83064 p->pc = -1;
83065 p->rc = SQLITE_OK;
83066 p->errorAction = OE_Abort;
83067 p->nChange = 0;
83068 p->cacheCtr = 1;
83069 p->minWriteFileFormat = 255;
83070 p->iStatement = 0;
83071 p->nFkConstraint = 0;
83073 for(i=0; i<p->nOp; i++){
83074 p->aOp[i].cnt = 0;
83075 p->aOp[i].cycles = 0;
83111 assert( p->nOp>0 );
83113 assert( p->eVdbeState==VDBE_INIT_STATE );
83114 assert( pParse==p->pParse );
83115 p->pVList = pParse->pVList;
83116 pParse->pVList = 0;
83117 db = p->db;
83118 assert( db->mallocFailed==0 );
83119 nVar = pParse->nVar;
83120 nMem = pParse->nMem;
83121 nCursor = pParse->nTab;
83122 nArg = pParse->nMaxArg;
83136 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
83137 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
83139 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
83144 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
83145 if( pParse->explain ){
83152 p->explain = pParse->explain;
83153 if( pParse->explain==2 ){
83163 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
83167 p->expired = 0;
83175 ** This two-pass approach that reuses as much memory as possible from
83180 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
83181 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
83182 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
83183 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
83185 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
83188 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
83190 if( !db->mallocFailed ){
83191 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
83192 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
83193 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
83194 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
83196 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
83201 if( db->mallocFailed ){
83202 p->nVar = 0;
83203 p->nCursor = 0;
83204 p->nMem = 0;
83206 p->nCursor = nCursor;
83207 p->nVar = (ynVar)nVar;
83208 initMemArray(p->aVar, nVar, db, MEM_Null);
83209 p->nMem = nMem;
83210 initMemArray(p->aMem, nMem, db, MEM_Undefined);
83211 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
83213 memset(p->anExec, 0, p->nOp*sizeof(i64));
83227 switch( pCx->eCurType ){
83229 sqlite3VdbeSorterClose(p->db, pCx);
83233 assert( pCx->uc.pCursor!=0 );
83234 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
83239 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
83240 const sqlite3_module *pModule = pVCur->pVtab->pModule;
83241 assert( pVCur->pVtab->nRef>0 );
83242 pVCur->pVtab->nRef--;
83243 pModule->xClose(pVCur);
83255 for(i=0; i<p->nCursor; i++){
83256 VdbeCursor *pC = p->apCsr[i];
83259 p->apCsr[i] = 0;
83266 ** is used, for example, when a trigger sub-program is halted to restore
83270 Vdbe *v = pFrame->v;
83273 v->anExec = pFrame->anExec;
83275 v->aOp = pFrame->aOp;
83276 v->nOp = pFrame->nOp;
83277 v->aMem = pFrame->aMem;
83278 v->nMem = pFrame->nMem;
83279 v->apCsr = pFrame->apCsr;
83280 v->nCursor = pFrame->nCursor;
83281 v->db->lastRowid = pFrame->lastRowid;
83282 v->nChange = pFrame->nChange;
83283 v->db->nChange = pFrame->nDbChange;
83284 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
83285 v->pAuxData = pFrame->pAuxData;
83286 pFrame->pAuxData = 0;
83287 return pFrame->pc;
83299 if( p->pFrame ){
83301 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
83303 p->pFrame = 0;
83304 p->nFrame = 0;
83306 assert( p->nFrame==0 );
83308 releaseMemArray(p->aMem, p->nMem);
83309 while( p->pDelFrame ){
83310 VdbeFrame *pDel = p->pDelFrame;
83311 p->pDelFrame = pDel->pParent;
83316 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
83317 assert( p->pAuxData==0 );
83328 sqlite3 *db = p->db;
83330 if( p->nResColumn ){
83331 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
83332 sqlite3DbFree(db, p->aColName);
83335 p->nResColumn = (u16)nResColumn;
83336 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
83337 if( p->aColName==0 ) return;
83338 initMemArray(p->aColName, n, db, MEM_Null);
83360 assert( idx<p->nResColumn );
83362 if( p->db->mallocFailed ){
83366 assert( p->aColName!=0 );
83367 pColName = &(p->aColName[idx+var*p->nResColumn]);
83368 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
83369 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
83376 ** write-transaction spanning more than one database file, this routine
83377 ** takes care of the super-journal trickery.
83381 int nTrans = 0; /* Number of databases with an active write-transaction
83382 ** that are candidates for a two-phase commit using a
83383 ** super-journal */
83396 ** be done before determining whether a super-journal file is
83405 ** one database file has an open write transaction, a super-journal
83408 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83409 Btree *pBt = db->aDb[i].pBt;
83411 /* Whether or not a database might need a super-journal depends upon
83413 ** journal modes use a super-journal and which do not */
83426 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
83441 /* If there are any write-transactions at all, invoke the commit hook */
83442 if( needXcommit && db->xCommitCallback ){
83443 rc = db->xCommitCallback(db->pCommitArg);
83449 /* The simple case - no more than one database file (not counting the
83451 ** super-journal.
83455 ** that case we do not support atomic multi-file commits, so use the
83458 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
83461 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83462 Btree *pBt = db->aDb[i].pBt;
83473 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83474 Btree *pBt = db->aDb[i].pBt;
83484 /* The complex case - There is a multi-file write-transaction active.
83485 ** This requires a super-journal file to ensure the transaction is
83490 sqlite3_vfs *pVfs = db->pVfs;
83491 char *zSuper = 0; /* File-name for the super-journal */
83492 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
83499 /* Select a super-journal file name */
83517 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
83519 /* The antipenultimate character of the super-journal name must
83521 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
83526 /* Open the super-journal. */
83533 sqlite3DbFree(db, zSuper-4);
83538 ** super-journal file. If an error occurs at this point close
83539 ** and delete the super-journal file. All the individual journal files
83540 ** still have 'null' as the super-journal pointer, so they will roll
83543 for(i=0; i<db->nDb; i++){
83544 Btree *pBt = db->aDb[i].pBt;
83556 sqlite3DbFree(db, zSuper-4);
83562 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
83570 sqlite3DbFree(db, zSuper-4);
83575 ** sets the super-journal pointer in each individual journal. If
83576 ** an error occurs here, do not delete the super-journal file.
83580 ** super-journal file will be orphaned. But we cannot delete it,
83581 ** in case the super-journal file name was written into the journal
83584 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83585 Btree *pBt = db->aDb[i].pBt;
83593 sqlite3DbFree(db, zSuper-4);
83597 /* Delete the super-journal file. This commits the transaction. After
83602 sqlite3DbFree(db, zSuper-4);
83617 for(i=0; i<db->nDb; i++){
83618 Btree *pBt = db->aDb[i].pBt;
83637 ** This is an internal self-check only - it is not an essential processing
83640 ** This is a no-op if NDEBUG is defined.
83648 p = db->pVdbe;
83652 if( p->readOnly==0 ) nWrite++;
83653 if( p->bIsReader ) nRead++;
83655 p = p->pNext;
83657 assert( cnt==db->nVdbeActive );
83658 assert( nWrite==db->nVdbeWrite );
83659 assert( nRead==db->nVdbeRead );
83666 ** If the Vdbe passed as the first argument opened a statement-transaction,
83676 sqlite3 *const db = p->db;
83679 const int iSavepoint = p->iStatement-1;
83682 assert( db->nStatement>0 );
83683 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
83685 for(i=0; i<db->nDb; i++){
83687 Btree *pBt = db->aDb[i].pBt;
83700 db->nStatement--;
83701 p->iStatement = 0;
83716 db->nDeferredCons = p->nStmtDefCons;
83717 db->nDeferredImmCons = p->nStmtDefImmCons;
83722 if( p->db->nStatement && p->iStatement ){
83741 sqlite3 *db = p->db;
83742 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
83743 || (!deferred && p->nFkConstraint>0)
83745 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
83746 p->errorAction = OE_Abort;
83748 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
83770 sqlite3 *db = p->db;
83788 assert( p->eVdbeState==VDBE_RUN_STATE );
83789 if( db->mallocFailed ){
83790 p->rc = SQLITE_NOMEM_BKPT;
83797 if( p->bIsReader ){
83798 int mrc; /* Primary error code from p->rc */
83806 if( p->rc ){
83807 mrc = p->rc & 0xff;
83816 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
83821 ** Even if the statement is read-only, it is important to perform
83823 ** occurred while writing to the journal, sub-journal or database
83828 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
83829 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
83837 db->autoCommit = 1;
83838 p->nChange = 0;
83844 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
83848 /* If the auto-commit flag is set and this is the only active writer
83855 && db->autoCommit
83856 && db->nVdbeWrite==(p->readOnly==0)
83858 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
83861 if( NEVER(p->readOnly) ){
83866 }else if( db->flags & SQLITE_CorruptRdOnly ){
83868 db->flags &= ~SQLITE_CorruptRdOnly;
83870 /* The auto-commit flag is true, the vdbe program was successful
83876 if( rc==SQLITE_BUSY && p->readOnly ){
83880 p->rc = rc;
83882 p->nChange = 0;
83884 db->nDeferredCons = 0;
83885 db->nDeferredImmCons = 0;
83886 db->flags &= ~(u64)SQLITE_DeferFKs;
83891 p->nChange = 0;
83893 db->nStatement = 0;
83895 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
83897 }else if( p->errorAction==OE_Abort ){
83902 db->autoCommit = 1;
83903 p->nChange = 0;
83907 /* If eStatementOp is non-zero, then a statement transaction needs to
83916 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
83917 p->rc = rc;
83918 sqlite3DbFree(db, p->zErrMsg);
83919 p->zErrMsg = 0;
83923 db->autoCommit = 1;
83924 p->nChange = 0;
83929 ** has been rolled back, update the database connection change-counter.
83931 if( p->changeCntOn ){
83933 sqlite3VdbeSetChanges(db, p->nChange);
83937 p->nChange = 0;
83945 db->nVdbeActive--;
83946 if( !p->readOnly ) db->nVdbeWrite--;
83947 if( p->bIsReader ) db->nVdbeRead--;
83948 assert( db->nVdbeActive>=db->nVdbeRead );
83949 assert( db->nVdbeRead>=db->nVdbeWrite );
83950 assert( db->nVdbeWrite>=0 );
83951 p->eVdbeState = VDBE_HALT_STATE;
83953 if( db->mallocFailed ){
83954 p->rc = SQLITE_NOMEM_BKPT;
83957 /* If the auto-commit flag is set to true, then any locks that were held
83959 ** to invoke any required unlock-notify callbacks.
83961 if( db->autoCommit ){
83965 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
83966 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
83972 ** in p->rc. This routine sets that result back to SQLITE_OK.
83975 p->rc = SQLITE_OK;
83987 sqlite3 *db = p->db;
83988 int rc = p->rc;
83989 if( p->zErrMsg ){
83990 db->bBenignMalloc++;
83992 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
83993 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
83995 db->bBenignMalloc--;
83996 }else if( db->pErr ){
83997 sqlite3ValueSetNull(db->pErr);
83999 db->errCode = rc;
84000 db->errByteOffset = -1;
84010 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
84011 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
84012 assert( v->db->init.busy==0 );
84015 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
84017 sqlite3DbFree(v->db, zExpanded);
84042 db = p->db;
84048 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
84055 if( p->pc>=0 ){
84057 if( db->pErr || p->zErrMsg ){
84060 db->errCode = p->rc;
84069 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
84070 if( p->aMem ){
84071 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
84074 if( p->zErrMsg ){
84075 sqlite3DbFree(db, p->zErrMsg);
84076 p->zErrMsg = 0;
84078 p->pResultSet = 0;
84080 p->nWrite = 0;
84089 fprintf(out, "---- ");
84090 for(i=0; i<p->nOp; i++){
84091 fprintf(out, "%02x", p->aOp[i].opcode);
84094 if( p->zSql ){
84096 fprintf(out, "-- ");
84097 for(i=0; (c = p->zSql[i])!=0; i++){
84098 if( pc=='\n' ) fprintf(out, "-- ");
84104 for(i=0; i<p->nOp; i++){
84107 p->aOp[i].cnt,
84108 p->aOp[i].cycles,
84109 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
84112 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
84118 return p->rc & db->errMask;
84130 if( p->eVdbeState>=VDBE_READY_STATE ){
84132 assert( (rc & p->db->errMask)==rc );
84158 || (pAux->iAuxOp==iOp
84159 && pAux->iAuxArg>=0
84160 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
84162 testcase( pAux->iAuxArg==31 );
84163 if( pAux->xDeleteAux ){
84164 pAux->xDeleteAux(pAux->pAux);
84166 *pp = pAux->pNextAux;
84169 pp= &pAux->pNextAux;
84184 assert( p->db==0 || p->db==db );
84185 if( p->aColName ){
84186 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
84187 sqlite3DbFreeNN(db, p->aColName);
84189 for(pSub=p->pProgram; pSub; pSub=pNext){
84190 pNext = pSub->pNext;
84191 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
84194 if( p->eVdbeState!=VDBE_INIT_STATE ){
84195 releaseMemArray(p->aVar, p->nVar);
84196 if( p->pVList ) sqlite3DbFreeNN(db, p->pVList);
84197 if( p->pFree ) sqlite3DbFreeNN(db, p->pFree);
84199 vdbeFreeOpArray(db, p->aOp, p->nOp);
84200 sqlite3DbFree(db, p->zSql);
84202 sqlite3DbFree(db, p->zNormSql);
84205 for(pThis=p->pDblStr; pThis; pThis=pNext){
84206 pNext = pThis->pNextStr;
84214 for(i=0; i<p->nScan; i++){
84215 sqlite3DbFree(db, p->aScan[i].zName);
84217 sqlite3DbFree(db, p->aScan);
84229 db = p->db;
84230 assert( sqlite3_mutex_held(db->mutex) );
84232 if( db->pnBytesFreed==0 ){
84233 if( p->pPrev ){
84234 p->pPrev->pNext = p->pNext;
84236 assert( db->pVdbe==p );
84237 db->pVdbe = p->pNext;
84239 if( p->pNext ){
84240 p->pNext->pPrev = p->pPrev;
84256 assert( p->deferredMoveto );
84257 assert( p->isTable );
84258 assert( p->eCurType==CURTYPE_BTREE );
84259 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
84265 p->deferredMoveto = 0;
84266 p->cacheStatus = CACHE_STALE;
84279 assert( p->eCurType==CURTYPE_BTREE );
84280 assert( p->uc.pCursor!=0 );
84281 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
84282 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
84283 p->cacheStatus = CACHE_STALE;
84284 if( isDifferentRow ) p->nullRow = 1;
84293 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
84294 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
84306 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
84311 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
84318 ** serial-type and data blob separately.
84323 ** -------------- --------------- ---------------
84335 ** N>=12 and even (N-12)/2 BLOB
84336 ** N>=13 and odd (N-13)/2 text
84344 ** Return the serial-type for the value stored in pMem.
84348 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
84349 ** opcode in the byte-code engine. But by moving this routine in-line, we
84355 int flags = pMem->flags;
84365 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
84366 i64 i = pMem->u.i;
84391 ** as an integer, then we might as well make it an 8-byte floating
84393 pMem->u.r = (double)pMem->u.i;
84394 pMem->flags &= ~MEM_IntReal;
84395 pMem->flags |= MEM_Real;
84404 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
84405 assert( pMem->n>=0 );
84406 n = (u32)pMem->n;
84408 n += pMem->u.nZero;
84436 ** Return the length of the data corresponding to the supplied serial-type.
84440 return (serial_type-12)/2;
84443 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
84453 ** If we are on an architecture with mixed-endian floating
84457 ** For most architectures, this is a no-op.
84459 ** (later): It is reported to me that the mixed-endian problem
84461 ** that early versions of GCC stored the two words of a 64-bit
84469 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
84473 ** (2007-08-30) Frank van Vugt has studied this problem closely
84476 ** emulation that uses only 32-bit mantissas instead of a full
84477 ** 48-bits as required by the IEEE standard. (This is the
84480 ** the necessary byte swapping is carried out using a 64-bit integer
84481 ** rather than a 64-bit float. Frank assures us that the code here
84504 ** big-endian integer. Return the equivalent native integer
84530 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
84531 ** twos-complement integer. */
84532 pMem->u.i = *(i64*)&x;
84533 pMem->flags = MEM_Int;
84534 testcase( pMem->u.i<0 );
84536 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
84541 ** defined that 64-bit floating point values really are mixed
84550 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
84552 memcpy(&pMem->u.r, &x, sizeof(x));
84553 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
84563 ** UPDATE no-change flag set */
84564 pMem->flags = MEM_Null|MEM_Zero;
84565 pMem->n = 0;
84566 pMem->u.nZero = 0;
84571 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
84572 pMem->flags = MEM_Null;
84576 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
84578 pMem->u.i = ONE_BYTE_INT(buf);
84579 pMem->flags = MEM_Int;
84580 testcase( pMem->u.i<0 );
84583 case 2: { /* 2-byte signed integer */
84584 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
84585 ** twos-complement integer. */
84586 pMem->u.i = TWO_BYTE_INT(buf);
84587 pMem->flags = MEM_Int;
84588 testcase( pMem->u.i<0 );
84591 case 3: { /* 3-byte signed integer */
84592 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
84593 ** twos-complement integer. */
84594 pMem->u.i = THREE_BYTE_INT(buf);
84595 pMem->flags = MEM_Int;
84596 testcase( pMem->u.i<0 );
84599 case 4: { /* 4-byte signed integer */
84600 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
84601 ** twos-complement integer. */
84602 pMem->u.i = FOUR_BYTE_INT(buf);
84604 /* Work around a sign-extension bug in the HP compiler for HP/UX */
84605 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
84607 pMem->flags = MEM_Int;
84608 testcase( pMem->u.i<0 );
84611 case 5: { /* 6-byte signed integer */
84612 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
84613 ** twos-complement integer. */
84614 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
84615 pMem->flags = MEM_Int;
84616 testcase( pMem->u.i<0 );
84619 case 6: /* 8-byte signed integer */
84628 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
84629 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
84630 pMem->u.i = serial_type-8;
84631 pMem->flags = MEM_Int;
84635 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
84637 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
84638 ** (N-13)/2 bytes in length. */
84640 pMem->z = (char *)buf;
84641 pMem->n = (serial_type-12)/2;
84642 pMem->flags = aFlag[serial_type&1];
84667 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
84668 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
84670 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
84671 assert( pKeyInfo->aSortFlags!=0 );
84672 p->pKeyInfo = pKeyInfo;
84673 p->nField = pKeyInfo->nKeyField + 1;
84678 ** Given the nKey-byte encoding of a record in pKey[], populate the
84693 Mem *pMem = p->aMem;
84695 p->default_rc = 0;
84704 pMem->enc = pKeyInfo->enc;
84705 pMem->db = pKeyInfo->db;
84706 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
84707 pMem->szMalloc = 0;
84708 pMem->z = 0;
84712 if( (++u)>=p->nField ) break;
84719 sqlite3VdbeMemSetNull(pMem-1);
84721 assert( u<=pKeyInfo->nKeyField + 1 );
84722 p->nField = u;
84751 pKeyInfo = pPKey2->pKeyInfo;
84752 if( pKeyInfo->db==0 ) return 1;
84753 mem1.enc = pKeyInfo->enc;
84754 mem1.db = pKeyInfo->db;
84770 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
84771 assert( pKeyInfo->aSortFlags!=0 );
84772 assert( pKeyInfo->nKeyField>0 );
84799 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
84800 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
84803 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
84804 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
84806 rc = -rc;
84808 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
84809 rc = -rc; /* Invert the result for DESC sort order. */
84814 }while( idx1<szHdr1 && i<pPKey2->nField );
84825 rc = pPKey2->default_rc;
84832 if( pKeyInfo->db->mallocFailed ) return 1;
84841 ** limit given by pKeyInfo->nAllField.
84843 ** If this constraint is not satisfied, it means that the high-speed
84867 assert( nField <= pKeyInfo->nAllField );
84877 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
84885 if( pMem1->enc==pColl->enc ){
84888 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
84894 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
84895 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
84898 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
84899 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
84904 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
84914 ** with MEM_Zero. Return true if it could be a zero-blob.
84931 int n1 = pB1->n;
84932 int n2 = pB2->n;
84934 /* It is possible to have a Blob value that has some non-zero content
84938 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
84939 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
84941 if( (pB1->flags|pB2->flags) & MEM_Zero ){
84942 if( pB1->flags & pB2->flags & MEM_Zero ){
84943 return pB1->u.nZero - pB2->u.nZero;
84944 }else if( pB1->flags & MEM_Zero ){
84945 if( !isAllZero(pB2->z, pB2->n) ) return -1;
84946 return pB1->u.nZero - n2;
84948 if( !isAllZero(pB1->z, pB1->n) ) return +1;
84949 return n1 - pB2->u.nZero;
84952 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
84954 return n1 - n2;
84958 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
84968 if( x<r ) return -1;
84974 if( r<-9223372036854775808.0 ) return +1;
84975 if( r>=9223372036854775808.0 ) return -1;
84977 if( i<y ) return -1;
84980 if( s<r ) return -1;
84999 f1 = pMem1->flags;
85000 f2 = pMem2->flags;
85008 return (f2&MEM_Null) - (f1&MEM_Null);
85020 if( pMem1->u.i < pMem2->u.i ) return -1;
85021 if( pMem1->u.i > pMem2->u.i ) return +1;
85025 if( pMem1->u.r < pMem2->u.r ) return -1;
85026 if( pMem1->u.r > pMem2->u.r ) return +1;
85033 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
85035 if( pMem1->u.i < pMem2->u.i ) return -1;
85036 if( pMem1->u.i > pMem2->u.i ) return +1;
85039 return -1;
85046 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
85048 return -1;
85062 return -1;
85065 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
85066 assert( pMem1->enc==SQLITE_UTF8 ||
85067 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
85073 assert( !pColl || pColl->xCmp );
85088 ** The first argument passed to this function is a serial-type that
85089 ** corresponds to an integer - all values between 1 and 9 inclusive
85125 return (serial_type - 8);
85137 ** If argument bSkip is non-zero, it is assumed that the caller has already
85141 ** fields that appear in both keys are equal, then pPKey2->default_rc is
85144 ** If database corruption is discovered, set pPKey2->errCode to
85146 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
85147 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
85159 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
85188 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85193 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
85195 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
85196 assert( pPKey2->pKeyInfo->nKeyField>0 );
85202 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
85203 testcase( pRhs->flags & MEM_Int );
85204 testcase( pRhs->flags & MEM_IntReal );
85210 rc = -1;
85213 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
85216 i64 rhs = pRhs->u.i;
85218 rc = -1;
85226 else if( pRhs->flags & MEM_Real ){
85235 rc = -1;
85239 if( mem1.u.r<pRhs->u.r ){
85240 rc = -1;
85241 }else if( mem1.u.r>pRhs->u.r ){
85245 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
85251 else if( pRhs->flags & MEM_Str ){
85255 rc = -1;
85259 mem1.n = (serial_type - 12) / 2;
85263 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
85265 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85267 }else if( pKeyInfo->aColl[i] ){
85268 mem1.enc = pKeyInfo->enc;
85269 mem1.db = pKeyInfo->db;
85273 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
85276 int nCmp = MIN(mem1.n, pRhs->n);
85277 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85278 if( rc==0 ) rc = mem1.n - pRhs->n;
85284 else if( pRhs->flags & MEM_Blob ){
85285 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
85289 rc = -1;
85291 int nStr = (serial_type - 12) / 2;
85295 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85297 }else if( pRhs->flags & MEM_Zero ){
85301 rc = nStr - pRhs->u.nZero;
85304 int nCmp = MIN(nStr, pRhs->n);
85305 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85306 if( rc==0 ) rc = nStr - pRhs->n;
85318 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
85322 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
85324 rc = -rc;
85333 if( i==pPKey2->nField ) break;
85348 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
85349 || pPKey2->pKeyInfo->db->mallocFailed
85351 pPKey2->eqSeen = 1;
85352 return pPKey2->default_rc;
85365 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
85383 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85386 case 1: { /* 1-byte signed integer */
85391 case 2: { /* 2-byte signed integer */
85396 case 3: { /* 3-byte signed integer */
85401 case 4: { /* 4-byte signed integer */
85407 case 5: { /* 6-byte signed integer */
85412 case 6: { /* 8-byte signed integer */
85439 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
85440 v = pPKey2->u.i;
85442 res = pPKey2->r1;
85444 res = pPKey2->r2;
85445 }else if( pPKey2->nField>1 ){
85451 ** fields. Return pPKey2->default_rc in this case. */
85452 res = pPKey2->default_rc;
85453 pPKey2->eqSeen = 1;
85463 ** uses the collation sequence BINARY and (c) that the size-of-header varint
85474 assert( pPKey2->aMem[0].flags & MEM_Str );
85475 assert( pPKey2->aMem[0].n == pPKey2->n );
85476 assert( pPKey2->aMem[0].z == pPKey2->u.z );
85477 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85487 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
85489 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
85495 nStr = (serial_type-12) / 2;
85497 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85500 nCmp = MIN( pPKey2->n, nStr );
85501 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
85504 res = pPKey2->r2;
85506 res = pPKey2->r1;
85508 res = nStr - pPKey2->n;
85510 if( pPKey2->nField>1 ){
85513 res = pPKey2->default_rc;
85514 pPKey2->eqSeen = 1;
85517 res = pPKey2->r2;
85519 res = pPKey2->r1;
85526 || pPKey2->pKeyInfo->db->mallocFailed
85538 ** that the size-of-header varint that occurs at the start of each record
85550 if( p->pKeyInfo->nAllField<=13 ){
85551 int flags = p->aMem[0].flags;
85552 if( p->pKeyInfo->aSortFlags[0] ){
85553 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
85556 p->r1 = 1;
85557 p->r2 = -1;
85559 p->r1 = -1;
85560 p->r2 = 1;
85563 p->u.i = p->aMem[0].u.i;
85570 && p->pKeyInfo->aColl[0]==0
85573 p->u.z = p->aMem[0].z;
85574 p->n = p->aMem[0].n;
85599 ** than 2GiB are support - anything large must be database corruption.
85601 ** this code can safely assume that nCellKey is 32-bits
85624 /* The last field of the index should be an integer - the ROWID.
85626 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
85645 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
85680 assert( pC->eCurType==CURTYPE_BTREE );
85681 pCur = pC->uc.pCursor;
85705 assert( sqlite3_mutex_held(db->mutex) );
85706 db->nChange = nChange;
85707 db->nTotalChange += nChange;
85715 v->changeCntOn = 1;
85724 ** programs obsolete. Removing user-defined functions or collating
85738 for(p = db->pVdbe; p; p=p->pNext){
85739 p->expired = iCode+1;
85747 return v->db;
85754 return v->prepFlags;
85768 Mem *pMem = &v->aVar[iVar-1];
85769 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
85770 if( 0==(pMem->flags & MEM_Null) ){
85771 sqlite3_value *pRet = sqlite3ValueNew(v->db);
85784 ** to sqlite3_reoptimize() that re-preparing the statement may result
85789 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
85791 v->expmask |= 0x80000000;
85793 v->expmask |= ((u32)1 << (iVar-1));
85802 ** throw an error if it is given inputs that would make it non-deterministic.
85803 ** This routine is invoked by date/time functions that use non-deterministic
85809 if( pCtx->pVdbe==0 ) return 1;
85811 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
85812 if( pOp->opcode==OP_PureFunc ){
85815 if( pOp->p5 & NC_IsCheck ){
85817 }else if( pOp->p5 & NC_GenCol ){
85822 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
85823 pCtx->pFunc->zName, zContext);
85824 sqlite3_result_error(pCtx, zMsg, -1);
85838 if( pVtab->zErrMsg ){
85839 sqlite3 *db = p->db;
85840 sqlite3DbFree(db, p->zErrMsg);
85841 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
85842 sqlite3_free(pVtab->zErrMsg);
85843 pVtab->zErrMsg = 0;
85852 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
85862 Mem *pMem = &p->aMem[i];
85863 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
85872 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
85878 Vdbe *v, /* Vdbe pre-update hook is invoked by */
85887 sqlite3 *db = v->db;
85890 const char *zTbl = pTab->zName;
85893 assert( db->pPreUpdate==0 );
85900 iKey2 = v->aMem[iReg].u.i;
85907 assert( pCsr->eCurType==CURTYPE_BTREE );
85908 assert( pCsr->nField==pTab->nCol
85909 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
85918 preupdate.keyinfo.nKeyField = pTab->nCol;
85925 db->pPreUpdate = &preupdate;
85926 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
85927 db->pPreUpdate = 0;
85933 for(i=0; i<pCsr->nField; i++){
85963 ** Return TRUE (non-zero) of the statement supplied as an argument needs
85972 return p==0 || p->expired;
85982 if( p->db==0 ){
86006 assert( p->startTime>0 );
86007 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
86008 assert( db->init.busy==0 );
86009 assert( p->zSql!=0 );
86010 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
86011 iElapse = (iNow - p->startTime)*1000000;
86013 if( db->xProfile ){
86014 db->xProfile(db->pProfileArg, p->zSql, iElapse);
86017 if( db->mTrace & SQLITE_TRACE_PROFILE ){
86018 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
86020 p->startTime = 0;
86027 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
86029 # define checkProfileCallback(DB,P) /*no-op*/
86044 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
86045 ** pointer is a harmless no-op. */
86049 sqlite3 *db = v->db;
86051 sqlite3_mutex_enter(db->mutex);
86074 sqlite3 *db = v->db;
86075 sqlite3_mutex_enter(db->mutex);
86079 assert( (rc & (db->errMask))==rc );
86081 sqlite3_mutex_leave(db->mutex);
86094 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
86097 for(i=0; i<p->nVar; i++){
86098 sqlite3VdbeMemRelease(&p->aVar[i]);
86099 p->aVar[i].flags = MEM_Null;
86101 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
86102 if( p->expmask ){
86103 p->expired = 1;
86116 if( p->flags & (MEM_Blob|MEM_Str) ){
86118 assert( p->flags==MEM_Null && p->z==0 );
86121 p->flags |= MEM_Blob;
86122 return p->n ? p->z : 0;
86144 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
86148 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
86151 && p->eSubtype=='p'
86152 && strcmp(p->u.zPType, zPType)==0
86154 return (void*)p->z;
86173 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
86174 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
86247 if( pVal->flags & MEM_Null ){
86249 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
86251 }else if( pVal->flags & MEM_Int ){
86253 }else if( pVal->flags & MEM_Str ){
86256 assert( eType == aType[pVal->flags&MEM_AffMask] );
86259 return aType[pVal->flags&MEM_AffMask];
86264 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
86269 return (pVal->flags&MEM_FromBind)!=0;
86281 pNew->flags &= ~MEM_Dyn;
86282 pNew->db = 0;
86283 if( pNew->flags&(MEM_Str|MEM_Blob) ){
86284 pNew->flags &= ~(MEM_Static|MEM_Dyn);
86285 pNew->flags |= MEM_Ephem;
86290 }else if( pNew->flags & MEM_Null ){
86292 pNew->flags &= ~(MEM_Term|MEM_Subtype);
86306 ** The following routines are used by user-defined functions to specify
86323 Mem *pOut = pCtx->pOut;
86336 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86364 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86373 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86382 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86383 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
86386 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86387 pCtx->isError = SQLITE_ERROR;
86388 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
86392 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86393 pCtx->isError = SQLITE_ERROR;
86394 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
86398 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86399 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
86402 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86403 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
86406 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86407 sqlite3VdbeMemSetNull(pCtx->pOut);
86415 Mem *pOut = pCtx->pOut;
86416 assert( sqlite3_mutex_held(pOut->db->mutex) );
86418 pOut->flags = MEM_Null;
86422 Mem *pOut = pCtx->pOut;
86423 assert( sqlite3_mutex_held(pOut->db->mutex) );
86424 pOut->eSubtype = eSubtype & 0xff;
86425 pOut->flags |= MEM_Subtype;
86433 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86443 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86459 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86468 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86477 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86482 Mem *pOut = pCtx->pOut;
86483 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86485 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86494 Mem *pOut = pCtx->pOut;
86495 assert( sqlite3_mutex_held(pOut->db->mutex) );
86496 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
86501 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86504 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86508 pCtx->isError = errCode ? errCode : -1;
86510 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
86512 if( pCtx->pOut->flags & MEM_Null ){
86513 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
86520 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86521 pCtx->isError = SQLITE_TOOBIG;
86522 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
86528 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86529 sqlite3VdbeMemSetNull(pCtx->pOut);
86530 pCtx->isError = SQLITE_NOMEM_BKPT;
86531 sqlite3OomFault(pCtx->pOut->db);
86537 ** test-control.
86540 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86541 if( pCtx->pOut->flags & MEM_Int ){
86542 pCtx->pOut->flags &= ~MEM_Int;
86543 pCtx->pOut->flags |= MEM_IntReal;
86557 for(i=0; i<db->nDb; i++){
86558 Btree *pBt = db->aDb[i].pBt;
86564 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
86565 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
86588 db = p->db;
86589 if( p->eVdbeState!=VDBE_RUN_STATE ){
86591 if( p->eVdbeState==VDBE_READY_STATE ){
86592 if( p->expired ){
86593 p->rc = SQLITE_SCHEMA;
86595 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
86597 ** error has occurred, then return the error code in p->rc to the
86610 if( db->nVdbeActive==0 ){
86611 AtomicStore(&db->u1.isInterrupted, 0);
86614 assert( db->nVdbeWrite>0 || db->autoCommit==0
86615 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
86619 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
86620 && !db->init.busy && p->zSql ){
86621 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
86623 assert( p->startTime==0 );
86627 db->nVdbeActive++;
86628 if( p->readOnly==0 ) db->nVdbeWrite++;
86629 if( p->bIsReader ) db->nVdbeRead++;
86630 p->pc = 0;
86631 p->eVdbeState = VDBE_RUN_STATE;
86634 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
86639 ** This "automatic-reset" change is not technically an incompatibility,
86645 ** returns, and those were broken by the automatic-reset change. As a
86646 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
86652 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
86660 assert( p->eVdbeState==VDBE_READY_STATE );
86666 p->rcApp = SQLITE_OK;
86669 if( p->explain ){
86674 db->nVdbeExec++;
86676 db->nVdbeExec--;
86680 assert( p->rc==SQLITE_OK );
86681 assert( db->mallocFailed==0 );
86682 db->errCode = SQLITE_ROW;
86690 if( rc==SQLITE_DONE && db->autoCommit ){
86691 assert( p->rc==SQLITE_OK );
86692 p->rc = doWalCallbacks(db);
86693 if( p->rc!=SQLITE_OK ){
86696 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
86698 ** error has occurred, then return the error code in p->rc to the
86705 db->errCode = rc;
86706 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
86707 p->rc = SQLITE_NOMEM_BKPT;
86708 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
86713 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
86717 return (rc&db->errMask);
86721 ** This is the top-level implementation of sqlite3_step(). Call
86734 db = v->db;
86735 sqlite3_mutex_enter(db->mutex);
86738 int savedPc = v->pc;
86749 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
86750 sqlite3DbFree(db, v->zErrMsg);
86751 if( !db->mallocFailed ){
86752 v->zErrMsg = sqlite3DbStrDup(db, zErr);
86753 v->rc = rc = sqlite3ApiExit(db, rc);
86755 v->zErrMsg = 0;
86756 v->rc = rc = SQLITE_NOMEM_BKPT;
86765 ** SQLITE_SCHEMA. tag-20220401a */
86766 v->minWriteFileFormat = 254;
86768 assert( v->expired==0 );
86770 sqlite3_mutex_leave(db->mutex);
86780 assert( p && p->pFunc );
86781 return p->pFunc->pUserData;
86788 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
86795 assert( p && p->pOut );
86796 return p->pOut->db;
86810 ** performance by substituting a NULL result, or some other light-weight
86815 return sqlite3_value_nochange(p->pOut);
86835 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
86838 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
86839 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
86840 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
86846 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
86847 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
86851 sqlite3_value *pOut = pRhs->pOut;
86854 pOut->enc = ENC(pOut->db);
86855 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
86892 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
86893 assert( p->pVdbe!=0 );
86896 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
86899 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
86907 ** its pMem->z element.
86910 Mem *pMem = p->pMem;
86911 assert( (pMem->flags & MEM_Agg)==0 );
86914 pMem->z = 0;
86917 pMem->flags = MEM_Agg;
86918 pMem->u.pDef = p->pFunc;
86919 if( pMem->z ){
86920 memset(pMem->z, 0, nByte);
86923 return (void*)pMem->z;
86932 assert( p && p->pFunc && p->pFunc->xFinalize );
86933 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
86935 if( (p->pMem->flags & MEM_Agg)==0 ){
86938 return (void*)p->pMem->z;
86944 ** the user-function defined by pCtx.
86946 ** The left-most argument is 0.
86955 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86957 if( pCtx->pVdbe==0 ) return 0;
86959 assert( pCtx->pVdbe!=0 );
86961 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
86962 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
86963 return pAuxData->pAux;
86971 ** argument to the user-function defined by pCtx. Any previous value is
86974 ** The left-most argument is 0.
86987 Vdbe *pVdbe = pCtx->pVdbe;
86989 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86996 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
86997 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
87002 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
87004 pAuxData->iAuxOp = pCtx->iOp;
87005 pAuxData->iAuxArg = iArg;
87006 pAuxData->pNextAux = pVdbe->pAuxData;
87007 pVdbe->pAuxData = pAuxData;
87008 if( pCtx->isError==0 ) pCtx->isError = -1;
87009 }else if( pAuxData->xDeleteAux ){
87010 pAuxData->xDeleteAux(pAuxData->pAux);
87013 pAuxData->pAux = pAux;
87014 pAuxData->xDeleteAux = xDelete;
87034 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
87035 return p->pMem->n;
87044 return pVm ? pVm->nResColumn : 0;
87053 if( pVm==0 || pVm->pResultSet==0 ) return 0;
87054 return pVm->nResColumn;
87063 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
87064 ** instead of an 8-byte one. This all works fine, except that when
87066 ** that a Mem structure is located on an 8-byte boundary. To prevent
87068 ** using gcc, we force nullMem to be 8-byte aligned using the magical
87106 assert( pVm->db );
87107 sqlite3_mutex_enter(pVm->db->mutex);
87108 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
87109 pOut = &pVm->pResultSet[i];
87111 sqlite3Error(pVm->db, SQLITE_RANGE);
87144 assert( p->db!=0 );
87145 assert( sqlite3_mutex_held(p->db->mutex) );
87146 p->rc = sqlite3ApiExit(p->db, p->rc);
87147 sqlite3_mutex_leave(p->db->mutex);
87197 if( pOut->flags&MEM_Static ){
87198 pOut->flags &= ~MEM_Static;
87199 pOut->flags |= MEM_Ephem;
87218 ** Convert the N-th element of pStmt->pColName[] into a string using
87251 db = p->db;
87256 sqlite3_mutex_enter(db->mutex);
87257 assert( db->mallocFailed==0 );
87260 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
87264 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
87269 if( db->mallocFailed ){
87273 sqlite3_mutex_leave(db->mutex);
87372 ** The error code stored in database p->db is overwritten with the return
87380 sqlite3_mutex_enter(p->db->mutex);
87381 if( p->eVdbeState!=VDBE_READY_STATE ){
87382 sqlite3Error(p->db, SQLITE_MISUSE);
87383 sqlite3_mutex_leave(p->db->mutex);
87385 "bind on a busy prepared statement: [%s]", p->zSql);
87388 if( i<1 || i>p->nVar ){
87389 sqlite3Error(p->db, SQLITE_RANGE);
87390 sqlite3_mutex_leave(p->db->mutex);
87393 i--;
87394 pVar = &p->aVar[i];
87396 pVar->flags = MEM_Null;
87397 p->db->errCode = SQLITE_OK;
87402 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
87408 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
87409 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
87410 p->expired = 1;
87433 pVar = &p->aVar[i-1];
87436 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
87439 sqlite3Error(p->db, rc);
87440 rc = sqlite3ApiExit(p->db, rc);
87443 sqlite3_mutex_leave(p->db->mutex);
87481 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
87482 sqlite3_mutex_leave(p->db->mutex);
87494 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
87495 sqlite3_mutex_leave(p->db->mutex);
87504 sqlite3_mutex_leave(p->db->mutex);
87519 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
87520 sqlite3_mutex_leave(p->db->mutex);
87562 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
87566 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
87568 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
87573 if( pValue->flags & MEM_Zero ){
87574 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
87576 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
87581 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
87582 pValue->enc);
87598 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87600 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87602 sqlite3_mutex_leave(p->db->mutex);
87609 sqlite3_mutex_enter(p->db->mutex);
87610 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
87616 rc = sqlite3ApiExit(p->db, rc);
87617 sqlite3_mutex_leave(p->db->mutex);
87627 return p ? p->nVar : 0;
87634 ** The result is always UTF-8.
87639 return sqlite3VListNumToName(p->pVList, i);
87649 return sqlite3VListNameToNum(p->pVList, zName, nName);
87662 assert( pTo->db==pFrom->db );
87663 assert( pTo->nVar==pFrom->nVar );
87664 sqlite3_mutex_enter(pTo->db->mutex);
87665 for(i=0; i<pFrom->nVar; i++){
87666 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
87668 sqlite3_mutex_leave(pTo->db->mutex);
87688 if( pFrom->nVar!=pTo->nVar ){
87691 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
87692 if( pTo->expmask ){
87693 pTo->expired = 1;
87695 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
87696 if( pFrom->expmask ){
87697 pFrom->expired = 1;
87710 return pStmt ? ((Vdbe*)pStmt)->db : 0;
87718 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
87726 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
87734 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
87751 sqlite3_mutex_enter(pDb->mutex);
87753 pNext = (sqlite3_stmt*)pDb->pVdbe;
87755 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
87757 sqlite3_mutex_leave(pDb->mutex);
87769 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
87776 sqlite3 *db = pVdbe->db;
87777 sqlite3_mutex_enter(db->mutex);
87779 db->pnBytesFreed = (int*)&v;
87781 db->pnBytesFreed = 0;
87782 sqlite3_mutex_leave(db->mutex);
87784 v = pVdbe->aCounter[op];
87785 if( resetFlag ) pVdbe->aCounter[op] = 0;
87795 return p ? p->zSql : 0;
87815 sqlite3_mutex_enter(p->db->mutex);
87817 sqlite3_mutex_leave(p->db->mutex);
87830 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
87831 sqlite3_mutex_enter(p->db->mutex);
87832 p->zNormSql = sqlite3Normalize(p, p->zSql);
87833 sqlite3_mutex_leave(p->db->mutex);
87835 return p->zNormSql;
87854 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
87861 ** This function is called from within a pre-update callback to retrieve
87865 PreUpdate *p = db->pPreUpdate;
87870 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
87871 if( !p || p->op==SQLITE_INSERT ){
87875 if( p->pPk ){
87876 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87878 if( iIdx>=p->pCsr->nField || iIdx<0 ){
87884 if( p->pUnpacked==0 ){
87888 assert( p->pCsr->eCurType==CURTYPE_BTREE );
87889 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
87892 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
87894 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
87895 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
87901 p->aRecord = aRec;
87904 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
87905 if( iIdx==p->pTab->iPKey ){
87906 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
87907 }else if( iIdx>=p->pUnpacked->nField ){
87909 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
87910 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
87911 testcase( pMem->flags & MEM_Int );
87912 testcase( pMem->flags & MEM_IntReal );
87925 ** This function is called from within a pre-update callback to retrieve
87929 PreUpdate *p = db->pPreUpdate;
87930 return (p ? p->keyinfo.nKeyField : 0);
87936 ** This function is designed to be called from within a pre-update callback
87940 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
87941 ** top-level trigger etc.).
87947 PreUpdate *p = db->pPreUpdate;
87948 return (p ? p->v->nFrame : 0);
87954 ** This function is designed to be called from within a pre-update callback
87958 PreUpdate *p = db->pPreUpdate;
87959 return (p ? p->iBlobWrite : -1);
87965 ** This function is called from within a pre-update callback to retrieve
87969 PreUpdate *p = db->pPreUpdate;
87973 if( !p || p->op==SQLITE_DELETE ){
87977 if( p->pPk && p->op!=SQLITE_UPDATE ){
87978 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87980 if( iIdx>=p->pCsr->nField || iIdx<0 ){
87985 if( p->op==SQLITE_INSERT ){
87986 /* For an INSERT, memory cell p->iNewReg contains the serialized record
87988 UnpackedRecord *pUnpack = p->pNewUnpacked;
87990 Mem *pData = &p->v->aMem[p->iNewReg];
87993 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
87998 p->pNewUnpacked = pUnpack;
88000 pMem = &pUnpack->aMem[iIdx];
88001 if( iIdx==p->pTab->iPKey ){
88002 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88003 }else if( iIdx>=pUnpack->nField ){
88007 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
88012 assert( p->op==SQLITE_UPDATE );
88013 if( !p->aNew ){
88014 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
88015 if( !p->aNew ){
88020 assert( iIdx>=0 && iIdx<p->pCsr->nField );
88021 pMem = &p->aNew[iIdx];
88022 if( pMem->flags==0 ){
88023 if( iIdx==p->pTab->iPKey ){
88024 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88026 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
88051 if( idx<0 || idx>=p->nScan ) return 1;
88052 pScan = &p->aScan[idx];
88055 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
88059 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
88064 LogEst x = pScan->nEst;
88073 *(const char**)pOut = pScan->zName;
88077 if( pScan->addrExplain ){
88078 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
88085 if( pScan->addrExplain ){
88086 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
88088 *(int*)pOut = -1;
88104 memset(p->anExec, 0, p->nOp * sizeof(i64));
88125 ** The Vdbe parse-tree explainer is also found here.
88133 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
88158 ** This function returns a pointer to a nul-terminated string in memory
88162 ** then the returned string holds a copy of zRawSql with "-- " prepended
88168 ** with large (multi-megabyte) strings and blobs.
88178 ** parameter index is known, locate the value in p->aVar[]. Then render
88197 db = p->db;
88198 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
88199 if( db->nVdbeExec>1 ){
88203 sqlite3_str_append(&out, "-- ", 3);
88204 assert( (zRawSql - zStart) > 0 );
88205 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
88207 }else if( p->nVar==0 ){
88236 assert( idx>0 && idx<=p->nVar );
88237 pVar = &p->aVar[idx-1];
88238 if( pVar->flags & MEM_Null ){
88240 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
88241 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
88242 }else if( pVar->flags & MEM_Real ){
88243 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
88244 }else if( pVar->flags & MEM_Str ){
88251 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
88259 nOut = pVar->n;
88263 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
88266 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
88268 if( nOut<pVar->n ){
88269 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88275 }else if( pVar->flags & MEM_Zero ){
88276 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
88279 assert( pVar->flags & MEM_Blob );
88281 nOut = pVar->n;
88286 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
88290 if( nOut<pVar->n ){
88291 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88380 ** use this information to make sure that the zero-blob functionality
88387 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
88388 sqlite3_max_blobsize = p->n;
88398 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
88400 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
88452 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
88453 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
88454 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
88456 ** In other words, if M is 2, then I is either 0 (for fall-through) or
88458 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
88469 ** always taken, the flags should be 0x05 since the fall-through and
88471 ** flags should be 0x06 since only the fall-through approach is allowed.
88474 ** interested in equal or not-equal. In other words, I==0 and I==2
88479 ** should be no-ops except for special builds used to measure test coverage.
88484 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
88488 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
88500 ** iSrcLine - the line number of the VdbeCoverage() macro, with
88502 ** I - Mask of bits 0x07 indicating which cases are are
88504 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
88509 ** M - Type of jump. Same as M argument above
88534 if( ((P)->flags&MEM_Ephem)!=0 \
88538 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
88565 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
88566 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
88568 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
88576 assert( iCur>=0 && iCur<p->nCursor );
88577 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
88578 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
88579 p->apCsr[iCur] = 0;
88584 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
88585 ** to hold cursors, it is faster to in-line the logic. */
88586 assert( pMem->flags==MEM_Undefined );
88587 assert( (pMem->flags & MEM_Dyn)==0 );
88588 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
88589 if( pMem->szMalloc<nByte ){
88590 if( pMem->szMalloc>0 ){
88591 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
88593 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
88594 if( pMem->zMalloc==0 ){
88595 pMem->szMalloc = 0;
88598 pMem->szMalloc = nByte;
88601 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
88603 pCx->eCurType = eCurType;
88604 pCx->nField = nField;
88605 pCx->aOffset = &pCx->aType[nField];
88607 pCx->uc.pCursor = (BtCursor*)
88608 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
88609 sqlite3BtreeCursorZero(pCx->uc.pCursor);
88626 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
88646 u8 enc = pRec->enc;
88648 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
88649 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
88651 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
88652 pRec->flags |= MEM_Int;
88654 pRec->u.r = rValue;
88655 pRec->flags |= MEM_Real;
88658 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
88661 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
88662 pRec->flags &= ~MEM_Str;
88672 ** floating-point representation if an integer representation
88682 ** No-op. pRec is unchanged.
88692 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
88693 if( (pRec->flags & MEM_Real)==0 ){
88694 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
88705 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
88706 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
88707 testcase( pRec->flags & MEM_Int );
88708 testcase( pRec->flags & MEM_Real );
88709 testcase( pRec->flags & MEM_IntReal );
88713 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
88748 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
88754 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
88755 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
88757 pMem->u.i = 0;
88760 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
88762 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
88763 pMem->u.i = ix;
88768 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
88769 pMem->u.i = ix;
88779 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
88780 ** But it does set pMem->u.r and pMem->u.i appropriately.
88783 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
88784 testcase( pMem->flags & MEM_Int );
88785 testcase( pMem->flags & MEM_Real );
88786 testcase( pMem->flags & MEM_IntReal );
88787 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
88789 if( pMem->flags & (MEM_Str|MEM_Blob) ){
88790 testcase( pMem->flags & MEM_Str );
88791 testcase( pMem->flags & MEM_Blob );
88803 int f = pMem->flags;
88821 for(i=0; i<25 && i<pMem->n; i++){
88822 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
88825 for(i=0; i<25 && i<pMem->n; i++){
88826 char z = pMem->z[i];
88831 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
88848 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
88849 for(j=0; j<25 && j<pMem->n; j++){
88850 c = pMem->z[j];
88853 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
88863 if( p->flags & MEM_Undefined ){
88865 }else if( p->flags & MEM_Null ){
88866 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
88867 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
88868 printf(" si:%lld", p->u.i);
88869 }else if( (p->flags & (MEM_IntReal))!=0 ){
88870 printf(" ir:%lld", p->u.i);
88871 }else if( p->flags & MEM_Int ){
88872 printf(" i:%lld", p->u.i);
88874 }else if( p->flags & MEM_Real ){
88875 printf(" r:%.17g", p->u.r);
88886 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
88891 if( p->pScopyFrom ){
88892 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
88911 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
88917 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
88927 ** high-performance timing routines.
88937 ** the number of non-transaction savepoints currently in the
88947 for(p=db->pSavepoint; p; p=p->pNext) n++;
88948 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
88954 ** Return the register of pOp->p2 after first preparing it to be
88959 pOut->flags = MEM_Int;
88964 assert( pOp->p2>0 );
88965 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
88966 pOut = &p->aMem[pOp->p2];
88968 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
88971 pOut->flags = MEM_Int;
88977 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
88978 ** with pOp->p3. Return the hash.
88984 assert( pOp->p4type==P4_INT32 );
88985 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
88987 if( p->flags & (MEM_Int|MEM_IntReal) ){
88988 h += p->u.i;
88989 }else if( p->flags & MEM_Real ){
88991 }else if( p->flags & (MEM_Str|MEM_Blob) ){
88992 h += p->n;
88993 if( p->flags & MEM_Zero ) h += p->u.nZero;
89010 return azTypes[sqlite3_value_type(pMem)-1];
89020 Op *aOp = p->aOp; /* Copy of p->aOp */
89029 sqlite3 *db = p->db; /* The database */
89037 Mem *aMem = p->aMem; /* Copy of p->aMem */
89047 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
89050 if( db->xProgress ){
89051 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
89052 assert( 0 < db->nProgressOps );
89053 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
89058 if( p->rc==SQLITE_NOMEM ){
89063 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
89064 testcase( p->rc!=SQLITE_OK );
89065 p->rc = SQLITE_OK;
89066 assert( p->bIsReader || p->readOnly!=0 );
89067 p->iCurrentTime = 0;
89068 assert( p->explain==0 );
89069 p->pResultSet = 0;
89070 db->busyHandler.nBusy = 0;
89071 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
89075 if( p->pc==0
89076 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
89081 if( p->db->flags & SQLITE_VdbeListing ){
89083 for(i=0; i<p->nOp; i++){
89087 if( p->db->flags & SQLITE_VdbeEQP ){
89088 for(i=0; i<p->nOp; i++){
89096 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
89100 for(pOp=&aOp[p->pc]; 1; pOp++){
89105 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
89111 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
89117 if( db->flags & SQLITE_VdbeTrace ){
89118 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
89119 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
89129 sqlite3_interrupt_count--;
89139 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
89141 assert( pOp->p1>0 );
89142 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
89143 assert( memIsValid(&aMem[pOp->p1]) );
89144 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
89145 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
89148 assert( pOp->p2>0 );
89149 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
89150 assert( memIsValid(&aMem[pOp->p2]) );
89151 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
89152 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
89155 assert( pOp->p3>0 );
89156 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89157 assert( memIsValid(&aMem[pOp->p3]) );
89158 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
89159 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
89162 assert( pOp->p2>0 );
89163 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
89164 memAboutToChange(p, &aMem[pOp->p2]);
89167 assert( pOp->p3>0 );
89168 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89169 memAboutToChange(p, &aMem[pOp->p3]);
89177 switch( pOp->opcode ){
89184 ** the switch statement will break with convention and be flush-left. Another
89222 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
89232 if( pOp->p5 ){
89233 assert( pOp->p2 < (int)(pOp - aOp) );
89234 assert( pOp->p2 > 1 );
89235 pOp = &aOp[pOp->p2 - 2];
89242 pOp = &aOp[pOp->p2 - 1];
89253 ** faster according to "valgrind --tool=cachegrind" */
89255 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
89260 ** If the progress callback returns non-zero, exit the virtual machine with
89263 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
89264 assert( db->nProgressOps!=0 );
89265 nProgressLimit += db->nProgressOps;
89266 if( db->xProgress(db->pProgressArg) ){
89283 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89284 pIn1 = &aMem[pOp->p1];
89287 pIn1->flags = MEM_Int;
89288 pIn1->u.i = (int)(pOp-aOp);
89289 REGISTER_TRACE(pOp->p1, pIn1);
89300 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
89307 ** P2 is not used by the byte-code engine. However, if P2 is positive
89312 ** value is a byte-code indentation hint. See tag-20220407a in
89316 pIn1 = &aMem[pOp->p1];
89317 if( pIn1->flags & MEM_Int ){
89318 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
89319 pOp = &aOp[pIn1->u.i];
89320 }else if( ALWAYS(pOp->p3) ){
89338 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89339 assert( pOp->p2>=0 && pOp->p2<p->nOp );
89340 assert( pOp->p3>=0 && pOp->p3<p->nOp );
89341 pOut = &aMem[pOp->p1];
89343 pOut->u.i = pOp->p3 - 1;
89344 pOut->flags = MEM_Int;
89345 if( pOp->p2==0 ) break;
89350 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
89351 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
89352 pOp = &aOp[pOp->p2 - 1];
89366 pIn1 = &aMem[pOp->p1];
89367 assert( pIn1->flags==MEM_Int );
89368 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
89369 pCaller = &aOp[pIn1->u.i];
89370 assert( pCaller->opcode==OP_Yield );
89371 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
89372 pOp = &aOp[pCaller->p2 - 1];
89373 pIn1->flags = MEM_Undefined;
89392 pIn1 = &aMem[pOp->p1];
89394 pIn1->flags = MEM_Int;
89395 pcDest = (int)pIn1->u.i;
89396 pIn1->u.i = (int)(pOp - aOp);
89397 REGISTER_TRACE(pOp->p1, pIn1);
89407 ** value in register P3 is not NULL, then this routine is a no-op.
89411 pIn3 = &aMem[pOp->p3];
89413 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
89415 if( (pIn3->flags & MEM_Null)==0 ) break;
89455 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
89457 if( p->pFrame && pOp->p1==SQLITE_OK ){
89458 /* Halt the sub-program. Return control to the parent frame. */
89459 pFrame = p->pFrame;
89460 p->pFrame = pFrame->pParent;
89461 p->nFrame--;
89462 sqlite3VdbeSetChanges(db, p->nChange);
89464 if( pOp->p2==OE_Ignore ){
89465 /* Instruction pcx is the OP_Program that invoked the sub-program
89467 ** instruction is set to OE_Ignore, then the sub-program is throwing
89470 pcx = p->aOp[pcx].p2-1;
89472 aOp = p->aOp;
89473 aMem = p->aMem;
89477 p->rc = pOp->p1;
89478 p->errorAction = (u8)pOp->p2;
89479 assert( pOp->p5<=4 );
89480 if( p->rc ){
89481 if( pOp->p5 ){
89484 testcase( pOp->p5==1 );
89485 testcase( pOp->p5==2 );
89486 testcase( pOp->p5==3 );
89487 testcase( pOp->p5==4 );
89488 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
89489 if( pOp->p4.z ){
89490 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
89493 sqlite3VdbeError(p, "%s", pOp->p4.z);
89495 pcx = (int)(pOp - aOp);
89496 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
89501 p->rc = SQLITE_BUSY;
89503 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
89504 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
89505 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
89513 ** The 32-bit integer value P1 is written into register P2.
89517 pOut->u.i = pOp->p1;
89524 ** P4 is a pointer to a 64-bit integer value.
89529 assert( pOp->p4.pI64!=0 );
89530 pOut->u.i = *pOp->p4.pI64;
89538 ** P4 is a pointer to a 64-bit floating point value.
89543 pOut->flags = MEM_Real;
89544 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
89545 pOut->u.r = *pOp->p4.pReal;
89553 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
89559 assert( pOp->p4.z!=0 );
89561 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
89565 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
89569 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
89571 pOut->szMalloc = 0;
89572 pOut->flags |= MEM_Static;
89573 if( pOp->p4type==P4_DYNAMIC ){
89574 sqlite3DbFree(db, pOp->p4.z);
89576 pOp->p4type = P4_DYNAMIC;
89577 pOp->p4.z = pOut->z;
89578 pOp->p1 = pOut->n;
89581 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
89584 pOp->opcode = OP_String;
89603 assert( pOp->p4.z!=0 );
89605 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
89606 pOut->z = pOp->p4.z;
89607 pOut->n = pOp->p1;
89608 pOut->enc = encoding;
89611 if( pOp->p3>0 ){
89612 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89613 pIn3 = &aMem[pOp->p3];
89614 assert( pIn3->flags & MEM_Int );
89615 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
89624 ** Mark the beginning of a subroutine that can be entered in-line
89628 ** If the subroutine is entered in-line, then the OP_Return will simply
89634 ** a no-op that simply falls through to the next instruction (assuming that
89636 ** entered in-line, then the OP_Return will cause in-line execution to
89651 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
89660 cnt = pOp->p3-pOp->p2;
89661 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89662 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
89663 pOut->n = 0;
89665 pOut->uTemp = 0;
89671 pOut->flags = nullFlag;
89672 pOut->n = 0;
89673 cnt--;
89687 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89688 pOut = &aMem[pOp->p1];
89689 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
89698 ** a zero-filled blob that is P1 bytes long in P2.
89701 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
89703 if( pOp->p4.z==0 ){
89704 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
89707 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
89709 pOut->enc = encoding;
89725 assert( pOp->p1>0 && pOp->p1<=p->nVar );
89726 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
89727 pVar = &p->aVar[pOp->p1 - 1];
89731 pOut = &aMem[pOp->p2];
89734 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
89735 pOut->flags |= MEM_Static|MEM_FromBind;
89743 ** Move the P3 values in register P1..P1+P3-1 over into
89744 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
89746 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
89754 n = pOp->p3;
89755 p1 = pOp->p1;
89756 p2 = pOp->p2;
89763 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
89764 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
89769 pIn1->pScopyFrom = 0;
89771 for(i=1; i<p->nMem; i++){
89782 }while( --n );
89802 n = pOp->p3;
89803 pIn1 = &aMem[pOp->p1];
89804 pOut = &aMem[pOp->p2];
89810 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
89811 pOut->flags &= ~MEM_Subtype;
89814 pOut->pScopyFrom = 0;
89816 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
89817 if( (n--)==0 ) break;
89838 pIn1 = &aMem[pOp->p1];
89839 pOut = &aMem[pOp->p2];
89843 pOut->pScopyFrom = pIn1;
89844 pOut->mScopyFlags = pIn1->flags;
89858 pIn1 = &aMem[pOp->p1];
89859 assert( (pIn1->flags & MEM_Int)!=0 );
89860 pOut = &aMem[pOp->p2];
89861 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
89869 ** constraint violations, this is a no-op.
89886 ** The registers P1 through P1+P2-1 contain a single row of
89889 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
89893 assert( p->nResColumn==pOp->p2 );
89894 assert( pOp->p1>0 || CORRUPT_DB );
89895 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
89897 p->cacheCtr = (p->cacheCtr + 2)|1;
89898 p->pResultSet = &aMem[pOp->p1];
89901 Mem *pMem = p->pResultSet;
89903 for(i=0; i<pOp->p2; i++){
89905 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
89915 if( db->mallocFailed ) goto no_mem;
89916 if( db->mTrace & SQLITE_TRACE_ROW ){
89917 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
89919 p->pc = (int)(pOp - aOp) + 1;
89942 pIn1 = &aMem[pOp->p1];
89943 pIn2 = &aMem[pOp->p2];
89944 pOut = &aMem[pOp->p3];
89947 flags1 = pIn1->flags;
89949 testcase( pIn2->flags & MEM_Null );
89950 if( (flags1 | pIn2->flags) & MEM_Null ){
89956 flags1 = pIn1->flags & ~MEM_Str;
89959 flags1 = pIn1->flags & ~MEM_Str;
89961 flags2 = pIn2->flags;
89964 flags2 = pIn2->flags & ~MEM_Str;
89967 flags2 = pIn2->flags & ~MEM_Str;
89969 nByte = pIn1->n + pIn2->n;
89970 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
89978 memcpy(pOut->z, pIn2->z, pIn2->n);
89979 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
89980 pIn2->flags = flags2;
89982 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
89983 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
89984 pIn1->flags = flags1;
89986 pOut->z[nByte]=0;
89987 pOut->z[nByte+1] = 0;
89988 pOut->flags |= MEM_Term;
89989 pOut->n = (int)nByte;
89990 pOut->enc = encoding;
90011 ** Synopsis: r[P3]=r[P2]-r[P1]
90046 pIn1 = &aMem[pOp->p1];
90048 pIn2 = &aMem[pOp->p2];
90050 pOut = &aMem[pOp->p3];
90051 flags = pIn1->flags | pIn2->flags;
90053 iA = pIn1->u.i;
90054 iB = pIn2->u.i;
90055 switch( pOp->opcode ){
90061 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
90067 if( iA==-1 ) iA = 1;
90072 pOut->u.i = iB;
90080 switch( pOp->opcode ){
90082 case OP_Subtract: rB -= rA; break;
90094 if( iA==-1 ) iA = 1;
90100 pOut->u.i = rB;
90106 pOut->u.r = rB;
90121 ** be returned. This is used by the built-in min(), max() and nullif()
90130 ** publicly. Only built-in functions have access to this feature.
90133 assert( pOp->p4type==P4_COLLSEQ );
90134 if( pOp->p1 ){
90135 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
90143 ** Take the bit-wise AND of the values in register P1 and P2 and
90150 ** Take the bit-wise OR of the values in register P1 and P2 and
90179 pIn1 = &aMem[pOp->p1];
90180 pIn2 = &aMem[pOp->p2];
90181 pOut = &aMem[pOp->p3];
90182 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
90188 op = pOp->opcode;
90199 op = 2*OP_ShiftLeft + 1 - op;
90200 iB = iB>(-64) ? -iB : 64;
90204 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
90211 /* Sign-extend on a right shift of a negative number */
90212 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
90217 pOut->u.i = iA;
90231 pIn1 = &aMem[pOp->p1];
90234 pIn1->u.i += pOp->p2;
90246 pIn1 = &aMem[pOp->p1];
90247 if( (pIn1->flags & MEM_Int)==0 ){
90249 if( (pIn1->flags & MEM_Int)==0 ){
90251 if( pOp->p2==0 ){
90275 pIn1 = &aMem[pOp->p1];
90276 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
90277 testcase( pIn1->flags & MEM_Int );
90278 testcase( pIn1->flags & MEM_IntReal );
90280 REGISTER_TRACE(pOp->p1, pIn1);
90303 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
90304 testcase( pOp->p2==SQLITE_AFF_TEXT );
90305 testcase( pOp->p2==SQLITE_AFF_BLOB );
90306 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
90307 testcase( pOp->p2==SQLITE_AFF_INTEGER );
90308 testcase( pOp->p2==SQLITE_AFF_REAL );
90309 pIn1 = &aMem[pOp->p1];
90313 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
90316 REGISTER_TRACE(pOp->p1, pIn1);
90327 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90371 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90421 u16 flags1; /* Copy of initial value of pIn1->flags */
90422 u16 flags3; /* Copy of initial value of pIn3->flags */
90424 pIn1 = &aMem[pOp->p1];
90425 pIn3 = &aMem[pOp->p3];
90426 flags1 = pIn1->flags;
90427 flags3 = pIn3->flags;
90429 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
90431 if( pIn3->u.i > pIn1->u.i ){
90432 if( sqlite3aGTb[pOp->opcode] ){
90433 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90437 }else if( pIn3->u.i < pIn1->u.i ){
90438 if( sqlite3aLTb[pOp->opcode] ){
90439 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90442 iCompare = -1;
90444 if( sqlite3aEQb[pOp->opcode] ){
90445 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90450 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90455 if( pOp->p5 & SQLITE_NULLEQ ){
90461 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
90462 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
90468 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
90476 if( pOp->p5 & SQLITE_JUMPIFNULL ){
90483 /* Neither operand is NULL and we couldn't do the special high-speed
90484 ** integer comparison case. So do a general-case comparison. */
90485 affinity = pOp->p5 & SQLITE_AFF_MASK;
90490 testcase( flags3==pIn3->flags );
90491 flags3 = pIn3->flags;
90499 testcase( pIn1->flags & MEM_Int );
90500 testcase( pIn1->flags & MEM_Real );
90501 testcase( pIn1->flags & MEM_IntReal );
90503 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
90504 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
90508 testcase( pIn3->flags & MEM_Int );
90509 testcase( pIn3->flags & MEM_Real );
90510 testcase( pIn3->flags & MEM_IntReal );
90512 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
90513 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
90516 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
90517 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
90529 res2 = sqlite3aLTb[pOp->opcode];
90531 res2 = sqlite3aEQb[pOp->opcode];
90533 res2 = sqlite3aGTb[pOp->opcode];
90538 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
90539 pIn3->flags = flags3;
90540 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
90541 pIn1->flags = flags1;
90543 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90565 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
90568 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
90592 assert( pOp->p4type==P4_INTARRAY );
90593 assert( pOp->p4.ai );
90600 ** Synopsis: r[P1@P3] <-> r[P2@P3]
90602 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
90603 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
90632 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
90636 assert( pOp[-1].opcode==OP_Permutation );
90637 assert( pOp[-1].p4type==P4_INTARRAY );
90638 aPermute = pOp[-1].p4.ai + 1;
90641 n = pOp->p3;
90642 pKeyInfo = pOp->p4.pKeyInfo;
90645 p1 = pOp->p1;
90646 p2 = pOp->p2;
90651 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
90652 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
90654 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
90655 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
90664 assert( i<pKeyInfo->nKeyField );
90665 pColl = pKeyInfo->aColl[i];
90666 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
90669 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
90672 iCompare = -iCompare;
90674 if( bRev ) iCompare = -iCompare;
90691 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
90693 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
90695 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
90697 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
90727 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
90728 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
90729 if( pOp->opcode==OP_And ){
90736 pOut = &aMem[pOp->p3];
90740 pOut->u.i = v1;
90767 assert( pOp->p4type==P4_INT32 );
90768 assert( pOp->p4.i==0 || pOp->p4.i==1 );
90769 assert( pOp->p3==0 || pOp->p3==1 );
90770 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
90771 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
90783 pIn1 = &aMem[pOp->p1];
90784 pOut = &aMem[pOp->p2];
90785 if( (pIn1->flags & MEM_Null)==0 ){
90797 ** ones-complement of the P1 value into register P2. If P1 holds
90801 pIn1 = &aMem[pOp->p1];
90802 pOut = &aMem[pOp->p2];
90804 if( (pIn1->flags & MEM_Null)==0 ){
90805 pOut->flags = MEM_Int;
90806 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
90814 ** encountered on each invocation of the byte-code program. Jump to P2
90817 ** Top-level programs determine first invocation by comparing the P1
90825 ** because the self-altering code trick does not work for recursive
90830 assert( p->aOp[0].opcode==OP_Init );
90831 if( p->pFrame ){
90832 iAddr = (int)(pOp - p->aOp);
90833 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
90837 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
90839 if( p->aOp[0].p1==pOp->p1 ){
90845 pOp->p1 = p->aOp[0].p1;
90852 ** is considered true if it is numeric and non-zero. If the value
90853 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
90857 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
90867 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
90871 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
90883 pIn1 = &aMem[pOp->p1];
90884 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
90885 if( (pIn1->flags & MEM_Null)!=0 ){
90900 pIn1 = &aMem[pOp->p1];
90901 doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
90915 if( (aMem[pOp->p1].flags & MEM_Null)!=0
90916 || (aMem[pOp->p3].flags & MEM_Null)!=0
90918 sqlite3VdbeMemSetNull(aMem + pOp->p2);
90920 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
90931 pIn1 = &aMem[pOp->p1];
90932 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
90933 if( (pIn1->flags & MEM_Null)==0 ){
90948 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90949 assert( p->apCsr[pOp->p1]!=0 );
90950 if( p->apCsr[pOp->p1]->nullRow ){
90951 sqlite3VdbeMemSetNull(aMem + pOp->p3);
90971 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
90975 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90976 pC = p->apCsr[pOp->p1];
90977 pOut = &p->aMem[pOp->p3];
90978 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
90981 if( pC->deferredMoveto ){
90985 if( sqlite3BtreeEof(pC->uc.pCursor) ){
90988 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
91000 ** information about the format of the data.) Extract the P2-th column
91018 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
91019 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
91027 u64 offset64; /* 64-bit offset */
91031 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91032 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91033 pC = p->apCsr[pOp->p1];
91034 p2 = (u32)pOp->p2;
91038 assert( p2<(u32)pC->nField
91039 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
91040 aOffset = pC->aOffset;
91041 assert( aOffset==pC->aType+pC->nField );
91042 assert( pC->eCurType!=CURTYPE_VTAB );
91043 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
91044 assert( pC->eCurType!=CURTYPE_SORTER );
91046 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
91047 if( pC->nullRow ){
91048 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
91049 /* For the special case of as pseudo-cursor, the seekResult field
91051 pReg = &aMem[pC->seekResult];
91052 assert( pReg->flags & MEM_Blob );
91054 pC->payloadSize = pC->szRow = pReg->n;
91055 pC->aRow = (u8*)pReg->z;
91057 pDest = &aMem[pOp->p3];
91063 pCrsr = pC->uc.pCursor;
91064 if( pC->deferredMoveto ){
91066 assert( !pC->isEphemeral );
91067 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
91068 pC = pC->pAltCursor;
91069 p2 = iMap - 1;
91079 assert( pC->eCurType==CURTYPE_BTREE );
91082 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
91083 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
91084 assert( pC->szRow<=pC->payloadSize );
91085 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
91087 pC->cacheStatus = p->cacheCtr;
91088 if( (aOffset[0] = pC->aRow[0])<0x80 ){
91089 pC->iHdrOffset = 1;
91091 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
91093 pC->nHdrParsed = 0;
91095 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
91096 /* pC->aRow does not have to hold the entire row, but it does at least
91097 ** need to cover the header of the record. If pC->aRow does not contain
91100 pC->aRow = 0;
91101 pC->szRow = 0;
91109 ** 3-byte type for each of the maximum of 32768 columns plus three
91112 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
91117 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
91129 zData = pC->aRow;
91130 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
91134 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
91141 ** parsed and valid information is in aOffset[] and pC->aType[].
91143 if( pC->nHdrParsed<=p2 ){
91145 ** to extract additional fields up through the p2+1-th field
91147 if( pC->iHdrOffset<aOffset[0] ){
91149 if( pC->aRow==0 ){
91151 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
91155 zData = pC->aRow;
91158 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
91160 i = pC->nHdrParsed;
91162 zHdr = zData + pC->iHdrOffset;
91166 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
91171 pC->aType[i] = t;
91182 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
91183 || (offset64 > pC->payloadSize)
91189 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91194 pC->nHdrParsed = i;
91195 pC->iHdrOffset = (u32)(zHdr - zData);
91196 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91205 if( pC->nHdrParsed<=p2 ){
91206 pDest = &aMem[pOp->p3];
91208 if( pOp->p4type==P4_MEM ){
91209 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
91216 t = pC->aType[p2];
91219 /* Extract the content for the p2+1-th column. Control can only
91220 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
91223 assert( p2<pC->nHdrParsed );
91225 pDest = &aMem[pOp->p3];
91231 assert( t==pC->aType[p2] );
91232 if( pC->szRow>=aOffset[p2+1] ){
91234 ** page - where the content is not on an overflow page */
91235 zData = pC->aRow + aOffset[p2];
91240 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
91244 pDest->n = len = (t-12)/2;
91245 pDest->enc = encoding;
91246 if( pDest->szMalloc < len+2 ){
91247 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
91248 pDest->flags = MEM_Null;
91251 pDest->z = pDest->zMalloc;
91253 memcpy(pDest->z, zData, len);
91254 pDest->z[len] = 0;
91255 pDest->z[len+1] = 0;
91256 pDest->flags = aFlag[t&1];
91259 pDest->enc = encoding;
91261 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
91262 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
91280 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
91281 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
91283 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
91284 pDest->flags &= ~MEM_Ephem;
91290 REGISTER_TRACE(pOp->p3, pDest);
91295 pOp = &aOp[aOp[0].p3-1];
91315 ** is zero. When P3 is non-zero, no type checking occurs for
91322 ** <li> P2 should be the number of non-virtual columns in the
91334 assert( pOp->p4type==P4_TABLE );
91335 pTab = pOp->p4.pTab;
91336 assert( pTab->tabFlags & TF_Strict );
91337 assert( pTab->nNVCol==pOp->p2 );
91338 aCol = pTab->aCol;
91339 pIn1 = &aMem[pOp->p1];
91340 for(i=0; i<pTab->nCol; i++){
91343 if( pOp->p3 ){ pIn1++; continue; }
91345 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
91347 if( (pIn1->flags & MEM_Null)==0 ){
91350 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
91355 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
91359 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
91363 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
91364 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
91365 if( pIn1->flags & MEM_Int ){
91368 ** so that we keep the high-resolution integer value but know that
91370 testcase( pIn1->u.i==140737488355328LL );
91371 testcase( pIn1->u.i==140737488355327LL );
91372 testcase( pIn1->u.i==-140737488355328LL );
91373 testcase( pIn1->u.i==-140737488355329LL );
91374 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
91375 pIn1->flags |= MEM_IntReal;
91376 pIn1->flags &= ~MEM_Int;
91378 pIn1->u.r = (double)pIn1->u.i;
91379 pIn1->flags |= MEM_Real;
91380 pIn1->flags &= ~MEM_Int;
91382 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
91393 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91396 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
91401 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
91402 pTab->zName, aCol[i].zCnName);
91412 ** P4 is a string that is P2 characters long. The N-th character of the
91413 ** string indicates the column affinity that should be used for the N-th
91419 zAffinity = pOp->p4.z;
91421 assert( pOp->p2>0 );
91422 assert( zAffinity[pOp->p2]==0 );
91423 pIn1 = &aMem[pOp->p1];
91424 while( 1 /*exit-by-break*/ ){
91425 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
91428 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
91431 ** so that we keep the high-resolution integer value but know that
91433 testcase( pIn1->u.i==140737488355328LL );
91434 testcase( pIn1->u.i==140737488355327LL );
91435 testcase( pIn1->u.i==-140737488355328LL );
91436 testcase( pIn1->u.i==-140737488355329LL );
91437 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
91438 pIn1->flags |= MEM_IntReal;
91439 pIn1->flags &= ~MEM_Int;
91441 pIn1->u.r = (double)pIn1->u.i;
91442 pIn1->flags |= MEM_Real;
91443 pIn1->flags &= ~MEM_Int;
91446 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91461 ** P4 may be a string that is P2 characters long. The N-th character of the
91462 ** string indicates the column affinity that should be used for the N-th
91471 ** compile-time option is enabled:
91474 ** of the right-most table that can be null-trimmed.
91478 ** accept no-change records with serial_type 10. This value is
91500 ** ------------------------------------------------------------------------
91501 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
91502 ** ------------------------------------------------------------------------
91509 ** hdr-size field is also a varint which is the offset from the beginning
91515 nField = pOp->p1;
91516 zAffinity = pOp->p4.z;
91517 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
91519 nField = pOp->p2;
91520 pLast = &pData0[nField-1];
91523 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
91524 pOut = &aMem[pOp->p3];
91534 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
91535 pRec->flags |= MEM_IntReal;
91536 pRec->flags &= ~(MEM_Int);
91538 REGISTER_TRACE((int)(pRec-aMem), pRec);
91548 ** have a non-NULL default value. Also, the record must be left with
91550 ** index of the right-most column with a non-NULL default value */
91551 if( pOp->p5 ){
91552 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
91553 pLast--;
91554 nField--;
91561 ** the Mem.uTemp field of each term should hold the serial-type that will
91565 ** --------------- ---------------
91567 ** 1 1-byte signed integer
91568 ** 2 2-byte signed integer
91569 ** 3 3-byte signed integer
91570 ** 4 4-byte signed integer
91571 ** 5 6-byte signed integer
91572 ** 6 8-byte signed integer
91588 if( pRec->flags & MEM_Null ){
91589 if( pRec->flags & MEM_Zero ){
91593 ** Give such values a special internal-use-only serial-type of 10
91597 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
91599 pRec->uTemp = 10;
91601 pRec->uTemp = 0;
91604 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
91606 i64 i = pRec->u.i;
91608 testcase( pRec->flags & MEM_Int );
91609 testcase( pRec->flags & MEM_IntReal );
91622 if( (i&1)==i && p->minWriteFileFormat>=4 ){
91623 pRec->uTemp = 8+(u32)uu;
91626 pRec->uTemp = 1;
91630 pRec->uTemp = 2;
91633 pRec->uTemp = 3;
91636 pRec->uTemp = 4;
91639 pRec->uTemp = 5;
91642 if( pRec->flags & MEM_IntReal ){
91644 ** as an integer, then we might as well make it an 8-byte floating
91646 pRec->u.r = (double)pRec->u.i;
91647 pRec->flags &= ~MEM_IntReal;
91648 pRec->flags |= MEM_Real;
91649 pRec->uTemp = 7;
91651 pRec->uTemp = 6;
91654 }else if( pRec->flags & MEM_Real ){
91657 pRec->uTemp = 7;
91659 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
91660 assert( pRec->n>=0 );
91661 len = (u32)pRec->n;
91662 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
91663 if( pRec->flags & MEM_Zero ){
91664 serial_type += pRec->u.nZero*2;
91667 len += pRec->u.nZero;
91669 nZero += pRec->u.nZero;
91674 pRec->uTemp = serial_type;
91677 pRec--;
91680 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
91698 ** the new record. The output register (pOp->p3) is not allowed to
91702 if( nByte+nZero<=pOut->szMalloc ){
91705 pOut->z = pOut->zMalloc;
91709 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
91716 pOut->n = (int)nByte;
91717 pOut->flags = MEM_Blob;
91719 pOut->u.nZero = nZero;
91720 pOut->flags |= MEM_Zero;
91723 zHdr = (u8 *)pOut->z;
91734 while( 1 /*exit-by-break*/ ){
91735 serial_type = pRec->uTemp;
91736 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
91738 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
91748 assert( sizeof(v)==sizeof(pRec->u.r) );
91749 memcpy(&v, &pRec->u.r, sizeof(v));
91752 v = pRec->u.i;
91756 while( 1 /*exit-by-break*/ ){
91757 zPayload[--i] = (u8)(v&0xFF);
91765 if( serial_type>=14 && pRec->n>0 ){
91766 assert( pRec->z!=0 );
91767 memcpy(zPayload, pRec->z, pRec->n);
91768 zPayload += pRec->n;
91772 if( pRec->n ){
91773 assert( pRec->z!=0 );
91774 memcpy(zPayload, pRec->z, pRec->n);
91775 zPayload += pRec->n;
91781 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
91782 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
91784 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91785 REGISTER_TRACE(pOp->p3, pOut);
91796 ** every btree page of the table. But if P3 is non-zero, an estimate
91803 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
91804 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
91806 if( pOp->p3 ){
91814 pOut->u.i = nEntry;
91835 p1 = pOp->p1;
91836 zName = pOp->p4.z;
91841 assert( db->pSavepoint==0 || db->autoCommit==0 );
91843 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
91845 assert( p->bIsReader );
91848 if( db->nVdbeWrite>0 ){
91852 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
91861 ** that the db->aVTrans[] array is empty. */
91862 assert( db->autoCommit==0 || db->nVTrans==0 );
91864 db->nStatement+db->nSavepoint);
91871 pNew->zName = (char *)&pNew[1];
91872 memcpy(pNew->zName, zName, nName+1);
91876 if( db->autoCommit ){
91877 db->autoCommit = 0;
91878 db->isTransactionSavepoint = 1;
91880 db->nSavepoint++;
91884 pNew->pNext = db->pSavepoint;
91885 db->pSavepoint = pNew;
91886 pNew->nDeferredCons = db->nDeferredCons;
91887 pNew->nDeferredImmCons = db->nDeferredImmCons;
91897 pSavepoint = db->pSavepoint;
91898 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
91899 pSavepoint = pSavepoint->pNext
91906 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
91910 sqlite3VdbeError(p, "cannot release savepoint - "
91919 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
91924 db->autoCommit = 1;
91926 p->pc = (int)(pOp - aOp);
91927 db->autoCommit = 0;
91928 p->rc = rc = SQLITE_BUSY;
91931 rc = p->rc;
91933 db->autoCommit = 0;
91935 db->isTransactionSavepoint = 0;
91939 iSavepoint = db->nSavepoint - iSavepoint - 1;
91941 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
91942 for(ii=0; ii<db->nDb; ii++){
91943 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
91952 for(ii=0; ii<db->nDb; ii++){
91953 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
91961 db->mDbFlags |= DBFLAG_SchemaChange;
91968 while( db->pSavepoint!=pSavepoint ){
91969 pTmp = db->pSavepoint;
91970 db->pSavepoint = pTmp->pNext;
91972 db->nSavepoint--;
91980 assert( pSavepoint==db->pSavepoint );
91981 db->pSavepoint = pSavepoint->pNext;
91984 db->nSavepoint--;
91988 db->nDeferredCons = pSavepoint->nDeferredCons;
91989 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
91999 if( p->eVdbeState==VDBE_HALT_STATE ){
92008 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
92019 desiredAutoCommit = pOp->p1;
92020 iRollback = pOp->p2;
92023 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
92024 assert( p->bIsReader );
92026 if( desiredAutoCommit!=db->autoCommit ){
92030 db->autoCommit = 1;
92031 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
92035 sqlite3VdbeError(p, "cannot commit transaction - "
92042 db->autoCommit = (u8)desiredAutoCommit;
92045 p->pc = (int)(pOp - aOp);
92046 db->autoCommit = (u8)(1-desiredAutoCommit);
92047 p->rc = rc = SQLITE_BUSY;
92051 if( p->rc==SQLITE_OK ){
92060 (iRollback)?"cannot rollback - no transaction is active":
92061 "cannot commit - no transaction is active"));
92073 ** If P2 is non-zero, then a write-transaction is started, or if a
92074 ** read-transaction is already active, it is upgraded to a write-transaction.
92075 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
92083 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
92109 assert( p->bIsReader );
92110 assert( p->readOnly==0 || pOp->p2==0 );
92111 assert( pOp->p2>=0 && pOp->p2<=2 );
92112 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92113 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92115 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
92116 if( db->flags & SQLITE_QueryOnly ){
92126 pDb = &db->aDb[pOp->p1];
92127 pBt = pDb->pBt;
92130 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
92135 p->pc = (int)(pOp - aOp);
92136 p->rc = rc;
92142 if( p->usesStmtJournal
92143 && pOp->p2
92144 && (db->autoCommit==0 || db->nVdbeRead>1)
92147 if( p->iStatement==0 ){
92148 assert( db->nStatement>=0 && db->nSavepoint>=0 );
92149 db->nStatement++;
92150 p->iStatement = db->nSavepoint + db->nStatement;
92153 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
92155 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
92161 p->nStmtDefCons = db->nDeferredCons;
92162 p->nStmtDefImmCons = db->nDeferredImmCons;
92165 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
92167 && pOp->p5
92168 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
92171 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
92175 sqlite3DbFree(db, p->zErrMsg);
92176 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
92177 /* If the schema-cookie from the database file matches the cookie
92178 ** stored with the in-memory representation of the schema, do
92181 ** If virtual-tables are in use, this is not just an optimization.
92182 ** Often, v-tables store their data in other SQLite tables, which
92183 ** are queried from within xNext() and other v-table methods using
92184 ** prepared queries. If such a query is out-of-date, we do not want to
92186 ** v-table would have to be ready for the sqlite3_vtab structure itself
92188 ** a v-table method.
92190 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
92191 sqlite3ResetOneSchema(db, pOp->p1);
92193 p->expired = 1;
92199 p->changeCntOn = 0;
92213 ** There must be a read-lock on the database (either a transaction
92222 assert( p->bIsReader );
92223 iDb = pOp->p1;
92224 iCookie = pOp->p3;
92225 assert( pOp->p3<SQLITE_N_BTREE_META );
92226 assert( iDb>=0 && iDb<db->nDb );
92227 assert( db->aDb[iDb].pBt!=0 );
92228 assert( DbMaskTest(p->btreeMask, iDb) );
92230 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
92232 pOut->u.i = iMeta;
92247 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
92255 assert( pOp->p2<SQLITE_N_BTREE_META );
92256 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92257 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92258 assert( p->readOnly==0 );
92259 pDb = &db->aDb[pOp->p1];
92260 assert( pDb->pBt!=0 );
92261 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
92263 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
92264 if( pOp->p2==BTREE_SCHEMA_VERSION ){
92266 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
92267 db->mDbFlags |= DBFLAG_SchemaChange;
92268 sqlite3FkClearTriggerCache(db, pOp->p1);
92269 }else if( pOp->p2==BTREE_FILE_FORMAT ){
92271 pDb->pSchema->file_format = pOp->p3;
92273 if( pOp->p1==1 ){
92277 p->expired = 0;
92286 ** Open a read-only cursor for the database table whose root page is
92303 ** object, then table being opened must be an [index b-tree] where the
92305 ** sequence of that index b-tree. Otherwise, if P4 is an integer
92306 ** value, then the table being opened must be a [table b-tree] with a
92316 ** b-tree and if it is this opcode becomes a no-op. In other words,
92338 ** OPFLAG_P2ISREG bit is set in P5 - see below).
92342 ** object, then table being opened must be an [index b-tree] where the
92344 ** sequence of that index b-tree. Otherwise, if P4 is an integer
92345 ** value, then the table being opened must be a [table b-tree] with a
92377 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
92378 assert( pOp->p4type==P4_KEYINFO );
92379 pCur = p->apCsr[pOp->p1];
92380 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
92381 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
92382 assert( pCur->eCurType==CURTYPE_BTREE );
92383 sqlite3BtreeClearCursor(pCur->uc.pCursor);
92391 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
92392 assert( p->bIsReader );
92393 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
92394 || p->readOnly==0 );
92396 if( p->expired==1 ){
92403 p2 = (u32)pOp->p2;
92404 iDb = pOp->p3;
92405 assert( iDb>=0 && iDb<db->nDb );
92406 assert( DbMaskTest(p->btreeMask, iDb) );
92407 pDb = &db->aDb[iDb];
92408 pX = pDb->pBt;
92410 if( pOp->opcode==OP_OpenWrite ){
92412 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
92414 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
92415 p->minWriteFileFormat = pDb->pSchema->file_format;
92420 if( pOp->p5 & OPFLAG_P2ISREG ){
92422 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
92423 assert( pOp->opcode==OP_OpenWrite );
92426 assert( (pIn2->flags & MEM_Int)!=0 );
92428 p2 = (int)pIn2->u.i;
92435 if( pOp->p4type==P4_KEYINFO ){
92436 pKeyInfo = pOp->p4.pKeyInfo;
92437 assert( pKeyInfo->enc==ENC(db) );
92438 assert( pKeyInfo->db==db );
92439 nField = pKeyInfo->nAllField;
92440 }else if( pOp->p4type==P4_INT32 ){
92441 nField = pOp->p4.i;
92443 assert( pOp->p1>=0 );
92446 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
92448 pCur->iDb = iDb;
92449 pCur->nullRow = 1;
92450 pCur->isOrdered = 1;
92451 pCur->pgnoRoot = p2;
92453 pCur->wrFlag = wrFlag;
92455 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
92456 pCur->pKeyInfo = pKeyInfo;
92458 ** SQLite used to check if the root-page flags were sane at this point
92461 pCur->isTable = pOp->p4type!=P4_KEYINFO;
92466 testcase( pOp->p5 & OPFLAG_BULKCSR );
92467 testcase( pOp->p2 & OPFLAG_SEEKEQ );
92468 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
92469 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
92480 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
92486 pOrig = p->apCsr[pOp->p2];
92488 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
92490 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
92492 pCx->nullRow = 1;
92493 pCx->isEphemeral = 1;
92494 pCx->pKeyInfo = pOrig->pKeyInfo;
92495 pCx->isTable = pOrig->isTable;
92496 pCx->pgnoRoot = pOrig->pgnoRoot;
92497 pCx->isOrdered = pOrig->isOrdered;
92498 pCx->ub.pBtx = pOrig->ub.pBtx;
92499 pCx->noReuse = 1;
92500 pOrig->noReuse = 1;
92501 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
92502 pCx->pKeyInfo, pCx->uc.pCursor);
92516 ** the main database is read-only. The ephemeral
92533 ** can be used as zero-length data for OP_Insert. This is an optimization
92555 assert( pOp->p1>=0 );
92556 assert( pOp->p2>=0 );
92557 if( pOp->p3>0 ){
92560 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
92561 assert( pOp->opcode==OP_OpenEphemeral );
92562 assert( aMem[pOp->p3].flags & MEM_Null );
92563 aMem[pOp->p3].n = 0;
92564 aMem[pOp->p3].z = "";
92566 pCx = p->apCsr[pOp->p1];
92567 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
92571 assert( pCx->isEphemeral );
92572 pCx->seqCount = 0;
92573 pCx->cacheStatus = CACHE_STALE;
92574 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
92576 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
92578 pCx->isEphemeral = 1;
92579 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
92580 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
92583 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
92588 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
92590 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
92591 assert( pOp->p4type==P4_KEYINFO );
92592 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
92593 BTREE_BLOBKEY | pOp->p5);
92595 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
92596 assert( pKeyInfo->db==db );
92597 assert( pKeyInfo->enc==ENC(db) );
92598 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
92599 pKeyInfo, pCx->uc.pCursor);
92601 pCx->isTable = 0;
92603 pCx->pgnoRoot = SCHEMA_ROOT;
92604 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
92605 0, pCx->uc.pCursor);
92606 pCx->isTable = 1;
92609 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
92611 sqlite3BtreeClose(pCx->ub.pBtx);
92616 pCx->nullRow = 1;
92624 ** tables using an external merge-sort algorithm.
92626 ** If argument P3 is non-zero, then it indicates that the sorter may
92633 assert( pOp->p1>=0 );
92634 assert( pOp->p2>=0 );
92635 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
92637 pCx->pKeyInfo = pOp->p4.pKeyInfo;
92638 assert( pCx->pKeyInfo->db==db );
92639 assert( pCx->pKeyInfo->enc==ENC(db) );
92640 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
92654 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92655 pC = p->apCsr[pOp->p1];
92657 if( (pC->seqCount++)==0 ){
92671 ** A pseudo-table created by this opcode is used to hold a single
92674 ** is the only cursor opcode that works with a pseudo-table.
92677 ** the pseudo-table.
92682 assert( pOp->p1>=0 );
92683 assert( pOp->p3>=0 );
92684 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
92686 pCx->nullRow = 1;
92687 pCx->seekResult = pOp->p2;
92688 pCx->isTable = 1;
92689 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
92691 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
92693 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
92694 assert( pOp->p5==0 );
92701 ** currently open, this instruction is a no-op.
92704 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92705 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
92706 p->apCsr[pOp->p1] = 0;
92715 ** table or index for cursor P1 are used. P4 is a 64-bit integer
92718 ** by the cursor. The high-order bit is set if any column after
92723 pC = p->apCsr[pOp->p1];
92724 assert( pC->eCurType==CURTYPE_BTREE );
92725 pC->maskUsed = *(u64*)pOp->p4.pI64;
92733 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92760 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92778 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92796 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92832 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92833 assert( pOp->p2!=0 );
92834 pC = p->apCsr[pOp->p1];
92836 assert( pC->eCurType==CURTYPE_BTREE );
92840 assert( pC->isOrdered );
92841 assert( pC->uc.pCursor!=0 );
92842 oc = pOp->opcode;
92844 pC->nullRow = 0;
92846 pC->seekOp = pOp->opcode;
92849 pC->deferredMoveto = 0;
92850 pC->cacheStatus = CACHE_STALE;
92851 if( pC->isTable ){
92854 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
92860 pIn3 = &aMem[pOp->p3];
92861 flags3 = pIn3->flags;
92866 newType = pIn3->flags; /* Record the type after applying numeric affinity */
92867 pIn3->flags = flags3; /* But convert the type back to its original */
92878 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
92883 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
92889 ** (x > 4.9) -> (x >= 5)
92890 ** (x <= 4.9) -> (x < 5)
92893 assert( OP_SeekGE==(OP_SeekGT-1) );
92894 assert( OP_SeekLT==(OP_SeekLE-1) );
92896 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
92908 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
92909 pC->movetoTarget = iKey; /* Used by OP_Delete */
92919 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
92921 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
92923 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
92924 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
92931 nField = pOp->p4.i;
92932 assert( pOp->p4type==P4_INT32 );
92934 r.pKeyInfo = pC->pKeyInfo;
92939 ** r.default_rc = -1;
92944 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
92945 assert( oc!=OP_SeekGT || r.default_rc==-1 );
92946 assert( oc!=OP_SeekLE || r.default_rc==-1 );
92950 r.aMem = &aMem[pOp->p3];
92955 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
92970 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
92986 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
92999 res = sqlite3BtreeEof(pC->uc.pCursor);
93003 assert( pOp->p2>0 );
93016 ** Synopsis: Scan-ahead up to P1 rows
93028 ** This opcode helps to optimize IN operators on a multi-column index
93031 ** of the b-tree instead. A correct answer is obtained if this opcode
93032 ** is omitted or is a no-op.
93039 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
93069 /* pOp->p2 points to the first instruction past the OP_IdxGT that
93071 assert( pOp->p2>=(int)(pOp-aOp)+2 );
93072 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
93073 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
93074 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
93075 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
93076 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
93078 assert( pOp->p1>0 );
93079 pC = p->apCsr[pOp[1].p1];
93081 assert( pC->eCurType==CURTYPE_BTREE );
93082 assert( !pC->isTable );
93083 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
93085 if( db->flags&SQLITE_VdbeTrace ){
93086 printf("... cursor not valid - fall through\n");
93091 nStep = pOp->p1;
93093 r.pKeyInfo = pC->pKeyInfo;
93113 if( db->flags&SQLITE_VdbeTrace ){
93114 printf("... %d steps and then skip\n", pOp->p1 - nStep);
93123 if( db->flags&SQLITE_VdbeTrace ){
93124 printf("... %d steps and then success\n", pOp->p1 - nStep);
93133 if( db->flags&SQLITE_VdbeTrace ){
93134 printf("... fall through after %d steps\n", pOp->p1);
93140 nStep--;
93141 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
93166 ** early, thus saving work. This is part of the IN-early-out optimization.
93168 ** P1 must be a valid b-tree cursor.
93172 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93173 pC = p->apCsr[pOp->p1];
93175 assert( pOp->p3>=pOp->p2 );
93176 if( pC->seekHit<pOp->p2 ){
93178 if( db->flags&SQLITE_VdbeTrace ){
93179 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
93182 pC->seekHit = pOp->p2;
93183 }else if( pC->seekHit>pOp->p3 ){
93185 if( db->flags&SQLITE_VdbeTrace ){
93186 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
93189 pC->seekHit = pOp->p3;
93200 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93201 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
93202 if( !p->apCsr[pOp->p1] ){
93263 ** This opcode is used in IN clause processing for a multi-column key.
93265 ** left-most element, and if there are no matches on the most recent
93284 ** record are not-NULL then a check is done to determine if any row in the
93300 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93301 pC = p->apCsr[pOp->p1];
93304 if( db->flags&SQLITE_VdbeTrace ){
93305 printf("seekHit is %d\n", pC->seekHit);
93308 if( pC->seekHit>=pOp->p4.i ) break;
93322 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
93325 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93326 assert( pOp->p4type==P4_INT32 );
93327 pC = p->apCsr[pOp->p1];
93330 pC->seekOp = pOp->opcode;
93332 r.aMem = &aMem[pOp->p3];
93333 assert( pC->eCurType==CURTYPE_BTREE );
93334 assert( pC->uc.pCursor!=0 );
93335 assert( pC->isTable==0 );
93336 r.nField = (u16)pOp->p4.i;
93339 r.pKeyInfo = pC->pKeyInfo;
93345 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
93348 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
93351 assert( r.aMem->flags & MEM_Blob );
93352 assert( pOp->opcode!=OP_NoConflict );
93356 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
93358 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
93359 pIdxKey->default_rc = 0;
93360 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
93366 alreadyExists = (pC->seekResult==0);
93367 pC->nullRow = 1-alreadyExists;
93368 pC->deferredMoveto = 0;
93369 pC->cacheStatus = CACHE_STALE;
93370 if( pOp->opcode==OP_Found ){
93378 if( pOp->opcode==OP_NoConflict ){
93390 if( pOp->opcode==OP_IfNoHope ){
93391 pC->seekHit = pOp->p4.i;
93413 ** (with arbitrary multi-value keys).
93432 ** P3 register to contain a non-integer value, in which case the jump is
93436 ** (with arbitrary multi-value keys).
93450 pIn3 = &aMem[pOp->p3];
93451 testcase( pIn3->flags & MEM_Int );
93452 testcase( pIn3->flags & MEM_IntReal );
93453 testcase( pIn3->flags & MEM_Real );
93454 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
93455 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
93456 /* If pIn3->u.i does not contain an integer, compute iKey as the
93470 pIn3 = &aMem[pOp->p3];
93471 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
93472 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93473 iKey = pIn3->u.i;
93475 pC = p->apCsr[pOp->p1];
93478 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
93480 assert( pC->isTable );
93481 assert( pC->eCurType==CURTYPE_BTREE );
93482 pCrsr = pC->uc.pCursor;
93487 pC->movetoTarget = iKey; /* Used by OP_Delete */
93488 pC->nullRow = 0;
93489 pC->cacheStatus = CACHE_STALE;
93490 pC->deferredMoveto = 0;
93492 pC->seekResult = res;
93495 if( pOp->p2==0 ){
93514 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93515 assert( p->apCsr[pOp->p1]!=0 );
93516 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
93518 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
93551 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93552 pC = p->apCsr[pOp->p1];
93554 assert( pC->isTable );
93555 assert( pC->eCurType==CURTYPE_BTREE );
93556 assert( pC->uc.pCursor!=0 );
93559 ** thing) is obtained in a two-step algorithm.
93571 assert( pC->isTable );
93583 if( !pC->useRandomRowid ){
93584 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
93589 v = 1; /* IMP: R-61914-48074 */
93591 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
93592 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93594 pC->useRandomRowid = 1;
93596 v++; /* IMP: R-29538-34987 */
93602 if( pOp->p3 ){
93604 assert( pOp->p3>0 );
93605 if( p->pFrame ){
93606 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93608 assert( pOp->p3<=pFrame->nMem );
93609 pMem = &pFrame->aMem[pOp->p3];
93612 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93613 pMem = &aMem[pOp->p3];
93618 REGISTER_TRACE(pOp->p3, pMem);
93620 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
93621 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
93622 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
93625 if( v<pMem->u.i+1 ){
93626 v = pMem->u.i + 1;
93628 pMem->u.i = v;
93631 if( pC->useRandomRowid ){
93632 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
93636 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
93642 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
93648 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
93651 assert( v>0 ); /* EV: R-40812-03570 */
93653 pC->deferredMoveto = 0;
93654 pC->cacheStatus = CACHE_STALE;
93656 pOut->u.i = v;
93685 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
93688 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
93689 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
93702 const char *zDb; /* database name - used by the update hook */
93703 Table *pTab; /* Table structure - used by update and pre-update hooks */
93706 pData = &aMem[pOp->p2];
93707 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93709 pC = p->apCsr[pOp->p1];
93711 assert( pC->eCurType==CURTYPE_BTREE );
93712 assert( pC->deferredMoveto==0 );
93713 assert( pC->uc.pCursor!=0 );
93714 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
93715 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
93716 REGISTER_TRACE(pOp->p2, pData);
93719 pKey = &aMem[pOp->p3];
93720 assert( pKey->flags & MEM_Int );
93722 REGISTER_TRACE(pOp->p3, pKey);
93723 x.nKey = pKey->u.i;
93725 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
93726 assert( pC->iDb>=0 );
93727 zDb = db->aDb[pC->iDb].zDbSName;
93728 pTab = pOp->p4.pTab;
93729 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
93736 /* Invoke the pre-update hook, if any */
93738 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
93739 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
93741 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
93742 /* Prevent post-update hook from running in cases when it should not */
93746 if( pOp->p5 & OPFLAG_ISNOOP ) break;
93749 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
93750 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
93751 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
93752 x.pData = pData->z;
93753 x.nData = pData->n;
93754 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
93755 if( pData->flags & MEM_Zero ){
93756 x.nZero = pData->u.nZero;
93761 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
93762 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
93765 pC->deferredMoveto = 0;
93766 pC->cacheStatus = CACHE_STALE;
93768 /* Invoke the update-hook if required. */
93771 assert( db->xUpdateCallback!=0 );
93772 assert( pTab->aCol!=0 );
93773 db->xUpdateCallback(db->pUpdateArg,
93774 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
93775 zDb, pTab->zName, x.nKey);
93783 ** of table - intkey or index. This opcode is used as part of copying
93796 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
93797 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
93799 pDest = p->apCsr[pOp->p1];
93800 pSrc = p->apCsr[pOp->p2];
93801 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
93802 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
93814 ** the next Next instruction will be a no-op. As a result, in this case
93828 ** P1 must not be pseudo-table. It has to be a real table with
93832 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
93834 ** this case. Specifically, if one is configured, the pre-update hook is
93835 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
93848 opflags = pOp->p2;
93849 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93850 pC = p->apCsr[pOp->p1];
93852 assert( pC->eCurType==CURTYPE_BTREE );
93853 assert( pC->uc.pCursor!=0 );
93854 assert( pC->deferredMoveto==0 );
93858 if( pOp->p4type==P4_TABLE
93859 && HasRowid(pOp->p4.pTab)
93860 && pOp->p5==0
93861 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
93864 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
93866 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93867 assert( CORRUPT_DB || pC->movetoTarget==iKey );
93871 /* If the update-hook or pre-update-hook will be invoked, set zDb to
93876 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
93877 assert( pC->iDb>=0 );
93878 assert( pOp->p4.pTab!=0 );
93879 zDb = db->aDb[pC->iDb].zDbSName;
93880 pTab = pOp->p4.pTab;
93881 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
93882 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93890 /* Invoke the pre-update-hook if required. */
93891 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
93892 if( db->xPreUpdateCallback && pTab ){
93895 || (aMem[pOp->p3].flags & MEM_Int)
93899 zDb, pTab, pC->movetoTarget,
93900 pOp->p3, -1
93907 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
93912 if( p->pFrame==0 ){
93913 if( pC->isEphemeral==0
93914 && (pOp->p5 & OPFLAG_AUXDELETE)==0
93915 && (pC->wrFlag & OPFLAG_FORDELETE)==0
93919 if( pOp->p2 & OPFLAG_NCHANGE ){
93920 nExtraDelete--;
93925 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
93926 pC->cacheStatus = CACHE_STALE;
93927 pC->seekResult = 0;
93930 /* Invoke the update-hook if required. */
93932 p->nChange++;
93933 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
93934 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
93935 pC->movetoTarget);
93936 assert( pC->iDb>=0 );
93950 sqlite3VdbeSetChanges(db, p->nChange);
93951 p->nChange = 0;
93975 pC = p->apCsr[pOp->p1];
93977 assert( pOp->p4type==P4_INT32 );
93978 pIn3 = &aMem[pOp->p3];
93979 nKeyCol = pOp->p4.i;
93995 ** a register that is the source for a pseudo-table cursor created using
93996 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
94003 pOut = &aMem[pOp->p2];
94004 pC = p->apCsr[pOp->p1];
94007 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
94008 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94010 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
94027 ** of a real table, not a pseudo-table.
94031 ** register will be invalidated as soon as the cursor moves - including
94049 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94050 pC = p->apCsr[pOp->p1];
94052 assert( pC->eCurType==CURTYPE_BTREE );
94054 assert( pC->nullRow==0 );
94055 assert( pC->uc.pCursor!=0 );
94056 pCrsr = pC->uc.pCursor;
94066 assert( pC->deferredMoveto==0 );
94070 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
94076 if( !pOp->p3 ) Deephemeralize(pOut);
94078 REGISTER_TRACE(pOp->p2, pOut);
94099 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94100 pC = p->apCsr[pOp->p1];
94102 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
94103 if( pC->nullRow ){
94104 pOut->flags = MEM_Null;
94106 }else if( pC->deferredMoveto ){
94107 v = pC->movetoTarget;
94109 }else if( pC->eCurType==CURTYPE_VTAB ){
94110 assert( pC->uc.pVCur!=0 );
94111 pVtab = pC->uc.pVCur->pVtab;
94112 pModule = pVtab->pModule;
94113 assert( pModule->xRowid );
94114 rc = pModule->xRowid(pC->uc.pVCur, &v);
94119 assert( pC->eCurType==CURTYPE_BTREE );
94120 assert( pC->uc.pCursor!=0 );
94123 if( pC->nullRow ){
94124 pOut->flags = MEM_Null;
94127 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
94129 pOut->u.i = v;
94140 ** pseudo-cursor that always returns NULL for every column.
94145 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94146 pC = p->apCsr[pOp->p1];
94149 ** pseudo-cursor that always gives null rows. */
94150 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
94152 pC->seekResult = 0;
94153 pC->isTable = 1;
94154 pC->noReuse = 1;
94155 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
94157 pC->nullRow = 1;
94158 pC->cacheStatus = CACHE_STALE;
94159 if( pC->eCurType==CURTYPE_BTREE ){
94160 assert( pC->uc.pCursor!=0 );
94161 sqlite3BtreeClearCursor(pC->uc.pCursor);
94164 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
94197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94198 pC = p->apCsr[pOp->p1];
94200 assert( pC->eCurType==CURTYPE_BTREE );
94201 pCrsr = pC->uc.pCursor;
94205 pC->seekOp = pOp->opcode;
94207 if( pOp->opcode==OP_SeekEnd ){
94208 assert( pOp->p2==0 );
94209 pC->seekResult = -1;
94215 pC->nullRow = (u8)res;
94216 pC->deferredMoveto = 0;
94217 pC->cacheStatus = CACHE_STALE;
94219 if( pOp->p2>0 ){
94237 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94238 pC = p->apCsr[pOp->p1];
94240 pCrsr = pC->uc.pCursor;
94246 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
94279 sqlite3_search_count--;
94281 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
94302 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94303 assert( pOp->p5==0 );
94304 pC = p->apCsr[pOp->p1];
94306 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
94309 pC->seekOp = OP_Rewind;
94314 assert( pC->eCurType==CURTYPE_BTREE );
94315 pCrsr = pC->uc.pCursor;
94318 pC->deferredMoveto = 0;
94319 pC->cacheStatus = CACHE_STALE;
94322 pC->nullRow = (u8)res;
94323 assert( pOp->p2>0 && pOp->p2<p->nOp );
94340 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
94349 ** number P5-1 in the prepared statement is incremented.
94365 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
94374 ** number P5-1 in the prepared statement is incremented.
94386 pC = p->apCsr[pOp->p1];
94392 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94393 assert( pOp->p5<ArraySize(p->aCounter) );
94394 pC = p->apCsr[pOp->p1];
94396 assert( pC->deferredMoveto==0 );
94397 assert( pC->eCurType==CURTYPE_BTREE );
94398 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
94399 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
94400 || pC->seekOp==OP_NullRow);
94401 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
94405 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94406 assert( pOp->p5<ArraySize(p->aCounter) );
94407 pC = p->apCsr[pOp->p1];
94409 assert( pC->deferredMoveto==0 );
94410 assert( pC->eCurType==CURTYPE_BTREE );
94411 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
94412 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
94413 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
94414 || pC->seekOp==OP_IfNoHope);
94415 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
94418 pC->cacheStatus = CACHE_STALE;
94421 pC->nullRow = 0;
94422 p->aCounter[pOp->p5]++;
94430 pC->nullRow = 1;
94446 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
94466 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94467 pC = p->apCsr[pOp->p1];
94471 pIn2 = &aMem[pOp->p2];
94472 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
94473 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
94474 assert( pC->eCurType==CURTYPE_BTREE );
94475 assert( pC->isTable==0 );
94478 x.nKey = pIn2->n;
94479 x.pKey = pIn2->z;
94480 x.aMem = aMem + pOp->p3;
94481 x.nMem = (u16)pOp->p4.i;
94482 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
94483 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
94484 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
94486 assert( pC->deferredMoveto==0 );
94487 pC->cacheStatus = CACHE_STALE;
94502 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94503 pC = p->apCsr[pOp->p1];
94507 pIn2 = &aMem[pOp->p2];
94508 assert( pIn2->flags & MEM_Blob );
94509 assert( pC->isTable==0 );
94530 ** this (self-correcting and non-critical) error if in writable_schema mode.
94538 assert( pOp->p3>0 );
94539 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
94540 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94541 pC = p->apCsr[pOp->p1];
94543 assert( pC->eCurType==CURTYPE_BTREE );
94545 pCrsr = pC->uc.pCursor;
94547 r.pKeyInfo = pC->pKeyInfo;
94548 r.nField = (u16)pOp->p3;
94550 r.aMem = &aMem[pOp->p2];
94556 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
94560 assert( pC->deferredMoveto==0 );
94561 pC->cacheStatus = CACHE_STALE;
94562 pC->seekResult = 0;
94579 ** is non-zero, then reading column a(i)-1 from cursor P3 is
94600 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94601 pC = p->apCsr[pOp->p1];
94603 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
94604 assert( pC->uc.pCursor!=0 );
94605 assert( pC->isTable==0 || IsNullCursor(pC) );
94606 assert( pC->deferredMoveto==0 );
94607 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
94618 if( !pC->nullRow ){
94620 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
94624 if( pOp->opcode==OP_DeferredSeek ){
94625 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
94626 pTabCur = p->apCsr[pOp->p3];
94628 assert( pTabCur->eCurType==CURTYPE_BTREE );
94629 assert( pTabCur->uc.pCursor!=0 );
94630 assert( pTabCur->isTable );
94631 pTabCur->nullRow = 0;
94632 pTabCur->movetoTarget = rowid;
94633 pTabCur->deferredMoveto = 1;
94634 pTabCur->cacheStatus = CACHE_STALE;
94635 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
94636 assert( !pTabCur->isEphemeral );
94637 pTabCur->ub.aAltMap = pOp->p4.ai;
94638 assert( !pC->isEphemeral );
94639 pTabCur->pAltCursor = pC;
94642 pOut->u.i = rowid;
94645 assert( pOp->opcode==OP_IdxRowid );
94646 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
94655 ** already occurred, this instruction is a no-op.
94660 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94661 pC = p->apCsr[pOp->p1];
94662 if( pC->deferredMoveto ){
94721 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94722 pC = p->apCsr[pOp->p1];
94724 assert( pC->isOrdered );
94725 assert( pC->eCurType==CURTYPE_BTREE );
94726 assert( pC->uc.pCursor!=0);
94727 assert( pC->deferredMoveto==0 );
94728 assert( pOp->p4type==P4_INT32 );
94729 r.pKeyInfo = pC->pKeyInfo;
94730 r.nField = (u16)pOp->p4.i;
94731 if( pOp->opcode<OP_IdxLT ){
94732 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
94733 r.default_rc = -1;
94735 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
94738 r.aMem = &aMem[pOp->p3];
94744 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
94755 assert( pC->eCurType==CURTYPE_BTREE );
94756 pCur = pC->uc.pCursor;
94774 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
94775 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
94776 res = -res;
94778 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
94799 ** value of the root page that moved - its value before the move occurred -
94810 ** and non-autovacuum modes.
94819 assert( p->readOnly==0 );
94820 assert( pOp->p1>1 );
94822 pOut->flags = MEM_Null;
94823 if( db->nVdbeRead > db->nVDestroy+1 ){
94825 p->errorAction = OE_Abort;
94828 iDb = pOp->p3;
94829 assert( DbMaskTest(p->btreeMask, iDb) );
94831 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
94832 pOut->flags = MEM_Int;
94833 pOut->u.i = iMoved;
94837 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
94857 ** If the P3 value is non-zero, then the row change count is incremented
94869 assert( p->readOnly==0 );
94870 assert( DbMaskTest(p->btreeMask, pOp->p2) );
94871 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
94872 if( pOp->p3 ){
94873 p->nChange += nChange;
94874 if( pOp->p3>0 ){
94875 assert( memIsValid(&aMem[pOp->p3]) );
94876 memAboutToChange(p, &aMem[pOp->p3]);
94877 aMem[pOp->p3].u.i += nChange;
94895 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94896 pC = p->apCsr[pOp->p1];
94899 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
94901 assert( pC->eCurType==CURTYPE_BTREE );
94902 assert( pC->isEphemeral );
94903 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
94912 ** Allocate a new b-tree in the main database file if P1==0 or in the
94916 ** The root page number of the new b-tree is stored in register P2.
94925 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
94926 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94927 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94928 assert( p->readOnly==0 );
94929 pDb = &db->aDb[pOp->p1];
94930 assert( pDb->pBt!=0 );
94931 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
94933 pOut->u.i = pgno;
94943 db->nSqlExec++;
94944 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
94945 db->nSqlExec--;
94957 ** then runs the new virtual machine. It is thus a re-entrant opcode.
94970 for(iDb=0; iDb<db->nDb; iDb++){
94971 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
94975 iDb = pOp->p1;
94976 assert( iDb>=0 && iDb<db->nDb );
94978 || db->mallocFailed
94979 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
94982 if( pOp->p4.z==0 ){
94983 sqlite3SchemaClear(db->aDb[iDb].pSchema);
94984 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
94985 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
94986 db->mDbFlags |= DBFLAG_SchemaChange;
94987 p->expired = 0;
94994 initData.pzErrMsg = &p->zErrMsg;
94996 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
94999 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
95003 assert( db->init.busy==0 );
95004 db->init.busy = 1;
95007 assert( !db->mallocFailed );
95011 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
95017 db->init.busy = 0;
95038 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95039 rc = sqlite3AnalysisLoad(db, pOp->p1);
95047 ** Remove the internal (in-memory) data structures that describe
95055 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
95061 ** Remove the internal (in-memory) data structures that describe
95069 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
95075 ** Remove the internal (in-memory) data structures that describe
95083 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
95115 assert( p->bIsReader );
95116 nRoot = pOp->p2;
95117 aRoot = pOp->p4.ai;
95120 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95121 pnErr = &aMem[pOp->p3];
95122 assert( (pnErr->flags & MEM_Int)!=0 );
95123 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
95124 pIn1 = &aMem[pOp->p1];
95125 assert( pOp->p5<db->nDb );
95126 assert( DbMaskTest(p->btreeMask, pOp->p5) );
95127 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
95128 (int)pnErr->u.i+1, &nErr);
95135 pnErr->u.i -= nErr-1;
95136 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
95153 pIn1 = &aMem[pOp->p1];
95154 pIn2 = &aMem[pOp->p2];
95155 assert( (pIn2->flags & MEM_Int)!=0 );
95156 if( (pIn1->flags & MEM_Blob)==0 ){
95160 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
95175 pIn1 = &aMem[pOp->p1];
95176 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
95177 if( (pIn1->flags & MEM_Blob)==0
95178 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
95187 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
95195 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
95204 ** must have P4==0, the final set must have P4==-1, and for all other sets
95209 ** (b) when P4==-1 there is no need to insert the value, as it will
95219 pIn1 = &aMem[pOp->p1];
95220 pIn3 = &aMem[pOp->p3];
95221 iSet = pOp->p4.i;
95222 assert( pIn3->flags&MEM_Int );
95227 if( (pIn1->flags & MEM_Blob)==0 ){
95231 assert( pOp->p4type==P4_INT32 );
95232 assert( iSet==-1 || iSet>=0 );
95234 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
95239 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
95252 ** cell in an array of values used as arguments to the sub-program. P2
95253 ** contains the address to jump to if the sub-program throws an IGNORE
95256 ** memory required by the sub-vdbe at runtime.
95260 ** If P5 is non-zero, then recursive program invocation is enabled.
95263 int nMem; /* Number of memory registers for sub-program */
95264 int nByte; /* Bytes of runtime space required for sub-program */
95269 SubProgram *pProgram; /* Sub-program to execute */
95272 pProgram = pOp->p4.pProgram;
95273 pRt = &aMem[pOp->p3];
95274 assert( pProgram->nOp>0 );
95277 ** disabled for backwards compatibility (p5 is set if this sub-program
95287 if( pOp->p5 ){
95288 t = pProgram->token;
95289 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
95293 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
95303 if( (pRt->flags&MEM_Blob)==0 ){
95309 nMem = pProgram->nMem + pProgram->nCsr;
95311 if( pProgram->nCsr==0 ) nMem++;
95314 + pProgram->nCsr * sizeof(VdbeCursor*)
95315 + (pProgram->nOp + 7)/8;
95321 pRt->flags = MEM_Blob|MEM_Dyn;
95322 pRt->z = (char*)pFrame;
95323 pRt->n = nByte;
95324 pRt->xDel = sqlite3VdbeFrameMemDel;
95326 pFrame->v = p;
95327 pFrame->nChildMem = nMem;
95328 pFrame->nChildCsr = pProgram->nCsr;
95329 pFrame->pc = (int)(pOp - aOp);
95330 pFrame->aMem = p->aMem;
95331 pFrame->nMem = p->nMem;
95332 pFrame->apCsr = p->apCsr;
95333 pFrame->nCursor = p->nCursor;
95334 pFrame->aOp = p->aOp;
95335 pFrame->nOp = p->nOp;
95336 pFrame->token = pProgram->token;
95338 pFrame->anExec = p->anExec;
95341 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
95344 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
95346 pMem->flags = MEM_Undefined;
95347 pMem->db = db;
95350 pFrame = (VdbeFrame*)pRt->z;
95351 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
95352 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
95353 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
95354 assert( pProgram->nCsr==pFrame->nChildCsr );
95355 assert( (int)(pOp - aOp)==pFrame->pc );
95358 p->nFrame++;
95359 pFrame->pParent = p->pFrame;
95360 pFrame->lastRowid = db->lastRowid;
95361 pFrame->nChange = p->nChange;
95362 pFrame->nDbChange = p->db->nChange;
95363 assert( pFrame->pAuxData==0 );
95364 pFrame->pAuxData = p->pAuxData;
95365 p->pAuxData = 0;
95366 p->nChange = 0;
95367 p->pFrame = pFrame;
95368 p->aMem = aMem = VdbeFrameMem(pFrame);
95369 p->nMem = pFrame->nChildMem;
95370 p->nCursor = (u16)pFrame->nChildCsr;
95371 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
95372 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
95373 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
95374 p->aOp = aOp = pProgram->aOp;
95375 p->nOp = pProgram->nOp;
95377 p->anExec = 0;
95384 for(i=0; i<p->nMem; i++){
95385 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
95390 pOp = &aOp[-1];
95396 ** This opcode is only ever present in sub-programs called via the
95410 pFrame = p->pFrame;
95411 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
95423 ** If P1 is non-zero, the database constraint counter is incremented
95428 if( db->flags & SQLITE_DeferFKs ){
95429 db->nDeferredImmCons += pOp->p2;
95430 }else if( pOp->p1 ){
95431 db->nDeferredCons += pOp->p2;
95433 p->nFkConstraint += pOp->p2;
95441 ** This opcode tests if a foreign key constraint-counter is currently zero.
95445 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
95447 ** zero, the jump is taken if the statement constraint-counter is zero
95451 if( pOp->p1 ){
95452 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
95453 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95455 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
95456 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95468 ** within a sub-program). Set the value of register P1 to the maximum of
95476 if( p->pFrame ){
95477 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
95478 pIn1 = &pFrame->aMem[pOp->p1];
95480 pIn1 = &aMem[pOp->p1];
95484 pIn2 = &aMem[pOp->p2];
95486 if( pIn1->u.i<pIn2->u.i){
95487 pIn1->u.i = pIn2->u.i;
95494 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
95504 pIn1 = &aMem[pOp->p1];
95505 assert( pIn1->flags&MEM_Int );
95506 VdbeBranchTaken( pIn1->u.i>0, 2);
95507 if( pIn1->u.i>0 ){
95508 pIn1->u.i -= pOp->p3;
95515 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
95528 ** and r[P2] is set to -1.
95534 pIn1 = &aMem[pOp->p1];
95535 pIn3 = &aMem[pOp->p3];
95537 assert( pIn1->flags & MEM_Int );
95538 assert( pIn3->flags & MEM_Int );
95539 x = pIn1->u.i;
95540 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
95548 pOut->u.i = -1;
95550 pOut->u.i = x;
95556 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
95560 ** If it is non-zero (negative or positive) and then also jump to P2.
95564 pIn1 = &aMem[pOp->p1];
95565 assert( pIn1->flags&MEM_Int );
95566 VdbeBranchTaken(pIn1->u.i<0, 2);
95567 if( pIn1->u.i ){
95568 if( pIn1->u.i>0 ) pIn1->u.i--;
95575 ** Synopsis: if (--r[P1])==0 goto P2
95581 pIn1 = &aMem[pOp->p1];
95582 assert( pIn1->flags&MEM_Int );
95583 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
95584 VdbeBranchTaken(pIn1->u.i==0, 2);
95585 if( pIn1->u.i==0 ) goto jump_to_p2;
95634 assert( pOp->p4type==P4_FUNCDEF );
95635 n = pOp->p5;
95636 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95637 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
95638 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
95640 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
95642 pCtx->pMem = 0;
95643 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
95644 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
95645 pCtx->pFunc = pOp->p4.pFunc;
95646 pCtx->iOp = (int)(pOp - aOp);
95647 pCtx->pVdbe = p;
95648 pCtx->skipFlag = 0;
95649 pCtx->isError = 0;
95650 pCtx->enc = encoding;
95651 pCtx->argc = n;
95652 pOp->p4type = P4_FUNCCTX;
95653 pOp->p4.pCtx = pCtx;
95656 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
95658 pOp->opcode = OP_AggStep1;
95667 assert( pOp->p4type==P4_FUNCCTX );
95668 pCtx = pOp->p4.pCtx;
95669 pMem = &aMem[pOp->p3];
95672 if( pOp->p1 ){
95675 assert( pMem->uTemp==0x1122e0e3 );
95678 pMem->uTemp = 0x1122e0e3;
95686 if( pCtx->pMem != pMem ){
95687 pCtx->pMem = pMem;
95688 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
95692 for(i=0; i<pCtx->argc; i++){
95693 assert( memIsValid(pCtx->argv[i]) );
95694 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
95698 pMem->n++;
95699 assert( pCtx->pOut->flags==MEM_Null );
95700 assert( pCtx->isError==0 );
95701 assert( pCtx->skipFlag==0 );
95703 if( pOp->p1 ){
95704 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
95707 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
95709 if( pCtx->isError ){
95710 if( pCtx->isError>0 ){
95711 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
95712 rc = pCtx->isError;
95714 if( pCtx->skipFlag ){
95715 assert( pOp[-1].opcode==OP_CollSeq );
95716 i = pOp[-1].p1;
95718 pCtx->skipFlag = 0;
95720 sqlite3VdbeMemRelease(pCtx->pOut);
95721 pCtx->pOut->flags = MEM_Null;
95722 pCtx->isError = 0;
95725 assert( pCtx->pOut->flags==MEM_Null );
95726 assert( pCtx->skipFlag==0 );
95759 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
95760 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
95761 pMem = &aMem[pOp->p1];
95762 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
95764 if( pOp->p3 ){
95765 memAboutToChange(p, &aMem[pOp->p3]);
95766 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
95767 pMem = &aMem[pOp->p3];
95771 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
95786 ** Checkpoint database P1. This is a no-op if P1 is not currently in
95793 ** mem[P3+2] are initialized to -1.
95800 assert( p->readOnly==0 );
95802 aRes[1] = aRes[2] = -1;
95803 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
95804 || pOp->p2==SQLITE_CHECKPOINT_FULL
95805 || pOp->p2==SQLITE_CHECKPOINT_RESTART
95806 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
95808 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
95814 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
95831 ** Write a string containing the final journal-mode to register P2.
95843 eNew = pOp->p3;
95852 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95853 assert( p->readOnly==0 );
95855 pBt = db->aDb[pOp->p1].pBt;
95870 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
95878 if( !db->autoCommit || db->nVdbeRead>1 ){
95889 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
95917 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
95918 pOut->z = (char *)sqlite3JournalModename(eNew);
95919 pOut->n = sqlite3Strlen30(pOut->z);
95920 pOut->enc = SQLITE_UTF8;
95938 assert( p->readOnly==0 );
95939 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
95940 pOp->p2 ? &aMem[pOp->p2] : 0);
95956 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95957 assert( DbMaskTest(p->btreeMask, pOp->p1) );
95958 assert( p->readOnly==0 );
95959 pBt = db->aDb[pOp->p1].pBt;
95978 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
95988 assert( pOp->p2==0 || pOp->p2==1 );
95989 if( !pOp->p1 ){
95990 sqlite3ExpirePreparedStatements(db, pOp->p2);
95992 p->expired = pOp->p2+1;
96004 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96005 pC = p->apCsr[pOp->p1];
96007 assert( pC->eCurType==CURTYPE_BTREE );
96008 sqlite3BtreeCursorPin(pC->uc.pCursor);
96019 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96020 pC = p->apCsr[pOp->p1];
96022 assert( pC->eCurType==CURTYPE_BTREE );
96023 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
96032 ** the shared-cache feature is enabled.
96038 ** P2 contains the root-page of the table to lock.
96044 u8 isWriteLock = (u8)pOp->p3;
96045 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
96046 int p1 = pOp->p1;
96047 assert( p1>=0 && p1<db->nDb );
96048 assert( DbMaskTest(p->btreeMask, p1) );
96050 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
96053 const char *z = pOp->p4.z;
96075 pVTab = pOp->p4.pVtab;
96077 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
96097 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
96098 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
96099 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
96102 assert( zTab || db->mallocFailed );
96104 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
96119 db->nVDestroy++;
96120 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
96121 db->nVDestroy--;
96122 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
96141 assert( p->bIsReader );
96144 pVtab = pOp->p4.pVtab->pVtab;
96145 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
96149 pModule = pVtab->pModule;
96150 rc = pModule->xOpen(pVtab, &pVCur);
96155 pVCur->pVtab = pVtab;
96158 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
96160 pCur->uc.pVCur = pVCur;
96161 pVtab->nRef++;
96163 assert( db->mallocFailed );
96164 pModule->xClose(pVCur);
96186 pC = p->apCsr[pOp->p1];
96189 pRhs->pCsr = pC->uc.pCursor;
96190 pRhs->pOut = &aMem[pOp->p3];
96192 pOut->flags = MEM_Null;
96232 pQuery = &aMem[pOp->p3];
96234 pCur = p->apCsr[pOp->p1];
96236 REGISTER_TRACE(pOp->p3, pQuery);
96238 assert( pCur->eCurType==CURTYPE_VTAB );
96239 pVCur = pCur->uc.pVCur;
96240 pVtab = pVCur->pVtab;
96241 pModule = pVtab->pModule;
96244 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
96245 nArg = (int)pArgc->u.i;
96246 iQuery = (int)pQuery->u.i;
96249 apArg = p->apArg;
96253 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
96256 res = pModule->xEof(pVCur);
96257 pCur->nullRow = 0;
96268 ** Store in register P3 the value of the P2-th column of
96269 ** the current row of the virtual-table of cursor P1.
96285 VdbeCursor *pCur = p->apCsr[pOp->p1];
96287 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96288 pDest = &aMem[pOp->p3];
96290 if( pCur->nullRow ){
96294 assert( pCur->eCurType==CURTYPE_VTAB );
96295 pVtab = pCur->uc.pVCur->pVtab;
96296 pModule = pVtab->pModule;
96297 assert( pModule->xColumn );
96301 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
96302 if( pOp->p5 & OPFLAG_NOCHNG ){
96304 pDest->flags = MEM_Null|MEM_Zero;
96305 pDest->u.nZero = 0;
96309 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
96316 REGISTER_TRACE(pOp->p3, pDest);
96337 pCur = p->apCsr[pOp->p1];
96339 assert( pCur->eCurType==CURTYPE_VTAB );
96340 if( pCur->nullRow ){
96343 pVtab = pCur->uc.pVCur->pVtab;
96344 pModule = pVtab->pModule;
96345 assert( pModule->xNext );
96353 rc = pModule->xNext(pCur->uc.pVCur);
96356 res = pModule->xEof(pCur->uc.pVCur);
96378 isLegacy = (db->flags & SQLITE_LegacyAlter);
96379 db->flags |= SQLITE_LegacyAlter;
96380 pVtab = pOp->p4.pVtab->pVtab;
96381 pName = &aMem[pOp->p1];
96382 assert( pVtab->pModule->xRename );
96384 assert( p->readOnly==0 );
96385 REGISTER_TRACE(pOp->p1, pName);
96386 assert( pName->flags & MEM_Str );
96387 testcase( pName->enc==SQLITE_UTF8 );
96388 testcase( pName->enc==SQLITE_UTF16BE );
96389 testcase( pName->enc==SQLITE_UTF16LE );
96392 rc = pVtab->pModule->xRename(pVtab, pName->z);
96393 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
96395 p->expired = 0;
96408 ** invocation. The value in register (P3+P2-1) corresponds to the
96438 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
96439 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
96441 assert( p->readOnly==0 );
96442 if( db->mallocFailed ) goto no_mem;
96444 pVtab = pOp->p4.pVtab->pVtab;
96445 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
96449 pModule = pVtab->pModule;
96450 nArg = pOp->p2;
96451 assert( pOp->p4type==P4_VTAB );
96452 if( ALWAYS(pModule->xUpdate) ){
96453 u8 vtabOnConflict = db->vtabOnConflict;
96454 apArg = p->apArg;
96455 pX = &aMem[pOp->p3];
96462 db->vtabOnConflict = pOp->p5;
96463 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
96464 db->vtabOnConflict = vtabOnConflict;
96466 if( rc==SQLITE_OK && pOp->p1 ){
96467 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
96468 db->lastRowid = rowid;
96470 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
96471 if( pOp->p5==OE_Ignore ){
96474 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
96477 p->nChange++;
96492 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
96512 pBt = db->aDb[pOp->p1].pBt;
96514 if( pOp->p3 ){
96516 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
96518 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
96533 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
96552 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
96561 ** purely non-deterministic. Some built-in date/time functions can be
96562 ** either determinitic of non-deterministic, depending on their arguments.
96563 ** When those function are used in a non-deterministic way, they will check
96574 assert( pOp->p4type==P4_FUNCCTX );
96575 pCtx = pOp->p4.pCtx;
96581 pOut = &aMem[pOp->p3];
96582 if( pCtx->pOut != pOut ){
96583 pCtx->pVdbe = p;
96584 pCtx->pOut = pOut;
96585 pCtx->enc = encoding;
96586 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
96588 assert( pCtx->pVdbe==p );
96592 for(i=0; i<pCtx->argc; i++){
96593 assert( memIsValid(pCtx->argv[i]) );
96594 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
96598 assert( pCtx->isError==0 );
96599 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
96602 if( pCtx->isError ){
96603 if( pCtx->isError>0 ){
96605 rc = pCtx->isError;
96607 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
96608 pCtx->isError = 0;
96612 assert( (pOut->flags&MEM_Str)==0
96613 || pOut->enc==encoding
96614 || db->mallocFailed );
96617 REGISTER_TRACE(pOp->p3, pOut);
96628 pIn1 = &aMem[pOp->p1];
96629 pIn1->flags &= ~MEM_Subtype;
96642 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
96643 pIn1 = &aMem[pOp->p1];
96644 assert( pIn1->flags & MEM_Blob );
96645 assert( pIn1->n>0 );
96648 if( db->flags&SQLITE_VdbeTrace ){
96650 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96653 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96656 h %= pIn1->n;
96657 pIn1->z[h/8] |= 1<<(h&7);
96673 ** false positive - if the jump is taken when it should fall through.
96678 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
96679 pIn1 = &aMem[pOp->p1];
96680 assert( (pIn1->flags & MEM_Blob)!=0 );
96681 assert( pIn1->n >= 1 );
96684 if( db->flags&SQLITE_VdbeTrace ){
96686 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96689 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96692 h %= pIn1->n;
96693 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
96695 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
96698 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
96718 ** the UTF-8 string contained in P4 is emitted on the trace callback.
96737 ** The "--" string is broken up to prevent false-positives with srcck1.c.
96740 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
96742 ** using the X argument when X begins with "--" and invoking
96745 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
96748 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
96751 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
96752 && p->minWriteFileFormat!=254 /* tag-20220401a */
96753 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96756 if( db->mTrace & SQLITE_TRACE_LEGACY ){
96758 db->trace.xLegacy(db->pTraceArg, z);
96762 if( db->nVdbeExec>1 ){
96763 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
96764 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
96767 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
96771 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
96774 for(j=0; j<db->nDb; j++){
96775 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
96776 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
96781 if( (db->flags & SQLITE_SqlTrace)!=0
96782 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96784 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
96788 assert( pOp->p2>0 );
96789 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
96790 if( pOp->opcode==OP_Trace ) break;
96791 for(i=1; i<p->nOp; i++){
96792 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
96794 pOp->p1 = 0;
96796 pOp->p1++;
96797 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
96807 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
96812 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96813 assert( pOp->p4type==P4_EXPR );
96814 pC = p->apCsr[pOp->p1];
96816 assert( pC->eCurType==CURTYPE_BTREE );
96817 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
96818 pOp->p4.pExpr, aMem);
96875 assert( pOp->p1>0 );
96876 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
96877 pMem = &aMem[pOp->p1];
96878 constMask = pOp->p3;
96879 for(i=0; i<pOp->p2; i++, pMem++){
96881 pMem->pScopyFrom = 0;
96882 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
96898 ** the same as a no-op. This opcodesnever appears in a real VM program.
96901 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
96908 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
96917 if( endTime>start ) pOrigOp->cycles += endTime - start;
96918 pOrigOp->cnt++;
96928 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
96931 if( db->flags & SQLITE_VdbeTrace ){
96932 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
96935 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
96938 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
96955 if( db->mallocFailed ){
96962 if( db->flags & SQLITE_VdbeTrace ){
96963 const char *zTrace = p->zSql;
96970 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
96973 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
96976 p->rc = rc;
96980 (int)(pOp - aOp), p->zSql, p->zErrMsg);
96981 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
96983 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
96984 db->flags |= SQLITE_CorruptRdOnly;
96988 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
96996 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
96997 nProgressLimit += db->nProgressOps;
96998 if( db->xProgress(db->pProgressArg) ){
97005 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
97008 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
97032 assert( AtomicLoad(&db->u1.isInterrupted) );
97078 ** the b-tree cursor associated with blob handle p to point to row iRow.
97089 ** If an error does occur, then the b-tree cursor is closed. All subsequent
97096 Vdbe *v = (Vdbe *)p->pStmt;
97101 v->aMem[1].flags = MEM_Int;
97102 v->aMem[1].u.i = iRow;
97108 if( v->pc>4 ){
97109 v->pc = 4;
97110 assert( v->aOp[v->pc].opcode==OP_NotExists );
97113 rc = sqlite3_step(p->pStmt);
97116 VdbeCursor *pC = v->apCsr[0];
97119 assert( pC->eCurType==CURTYPE_BTREE );
97120 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
97121 testcase( pC->nHdrParsed==p->iCol );
97122 testcase( pC->nHdrParsed==p->iCol+1 );
97124 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
97128 sqlite3_finalize(p->pStmt);
97129 p->pStmt = 0;
97131 p->iOffset = pC->aType[p->iCol + pC->nField];
97132 p->nByte = sqlite3VdbeSerialTypeLen(type);
97133 p->pCsr = pC->uc.pCursor;
97134 sqlite3BtreeIncrblobCursor(p->pCsr);
97140 }else if( p->pStmt ){
97141 rc = sqlite3_finalize(p->pStmt);
97142 p->pStmt = 0;
97144 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
97147 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
97167 int wrFlag, /* True -> read/write access, false -> read-only */
97171 int iCol; /* Index of zColumn in row-record */
97191 sqlite3_mutex_enter(db->mutex);
97226 pBlob->pTab = pTab;
97227 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
97230 for(iCol=0; iCol<pTab->nCol; iCol++) {
97231 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
97235 if( iCol==pTab->nCol ){
97250 if( db->flags&SQLITE_ForeignKeys ){
97257 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
97259 for(j=0; j<pFKey->nCol; j++){
97260 if( pFKey->aCol[j].iFrom==iCol ){
97267 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
97269 for(j=0; j<pIdx->nKeyCol; j++){
97271 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
97285 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
97286 assert( pBlob->pStmt || db->mallocFailed );
97287 if( pBlob->pStmt ){
97291 ** of writing code to use the b-tree layer directly is that the
97296 ** Code external to the Vdbe then "borrows" the b-tree cursor and
97301 ** which closes the b-tree cursor and (possibly) commits the
97314 Vdbe *v = (Vdbe *)pBlob->pStmt;
97315 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97319 pTab->pSchema->schema_cookie,
97320 pTab->pSchema->iGeneration);
97322 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
97328 if( db->mallocFailed==0 ){
97335 aOp[0].p2 = pTab->tnum;
97337 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
97339 if( db->mallocFailed==0 ){
97343 ** parameter of the other to pTab->tnum. */
97345 aOp[1].p2 = pTab->tnum;
97356 aOp[1].p4.i = pTab->nCol+1;
97357 aOp[3].p2 = pTab->nCol;
97366 pBlob->iCol = iCol;
97367 pBlob->db = db;
97369 if( db->mallocFailed ){
97378 if( rc==SQLITE_OK && db->mallocFailed==0 ){
97381 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
97388 sqlite3_mutex_leave(db->mutex);
97402 sqlite3_stmt *pStmt = p->pStmt;
97403 db = p->db;
97404 sqlite3_mutex_enter(db->mutex);
97406 sqlite3_mutex_leave(db->mutex);
97430 db = p->db;
97431 sqlite3_mutex_enter(db->mutex);
97432 v = (Vdbe*)p->pStmt;
97434 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
97438 /* If there is no statement handle, then the blob-handle has
97444 ** returned, clean-up the statement handle.
97446 assert( db == v->db );
97447 sqlite3BtreeEnterCursor(p->pCsr);
97450 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
97451 /* If a pre-update hook is registered and this is a write cursor,
97454 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
97461 ** using the incremental-blob API, this works. For the sessions module
97465 iKey = sqlite3BtreeIntegerKey(p->pCsr);
97466 assert( v->apCsr[0]!=0 );
97467 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
97469 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
97474 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
97475 sqlite3BtreeLeaveCursor(p->pCsr);
97478 p->pStmt = 0;
97480 v->rc = rc;
97485 sqlite3_mutex_leave(db->mutex);
97511 return (p && p->pStmt) ? p->nByte : 0;
97530 db = p->db;
97531 sqlite3_mutex_enter(db->mutex);
97533 if( p->pStmt==0 ){
97534 /* If there is no statement handle, then the blob-handle has
97540 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
97550 assert( rc==SQLITE_OK || p->pStmt==0 );
97551 sqlite3_mutex_leave(db->mutex);
97560 ** 2011-07-09
97575 ** The VdbeSorter object implements a multi-threaded external merge sort
97579 ** Here is the (internal, non-API) interface between this module and the
97632 ** an in-memory merge sort. In this case, no temporary files are required
97639 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
97640 ** of PMAs may be created by merging existing PMAs together - for example
97641 ** merging two or more level-0 PMAs together creates a level-1 PMA.
97645 ** page-cache of the main database. Specifically, the threshold is set to
97649 ** If the sorter is running in single-threaded mode, then all PMAs generated
97651 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
97658 ** The sorter is running in multi-threaded mode if (a) the library was built
97659 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
97668 ** sorter is running in single-threaded mode, then these PMAs are merged
97673 ** Or, if running in multi-threaded mode, then a background thread is
97683 ** Rewind() is called, then a hierarchy of incremental-merges is used.
97689 ** If running in multi-threaded mode and there are more than
97702 ** characteristics of the sorter in multi-threaded mode.
97709 ** Hard-coded maximum amount of data to accumulate in memory before flushing
97722 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
97724 typedef struct SorterList SorterList; /* In-memory list of records */
97737 ** An in-memory list of objects to be sorted.
97746 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
97769 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
97783 ** aReadr[0] -> Banana
97784 ** aReadr[1] -> Feijoa
97785 ** aReadr[2] -> Elderberry
97786 ** aReadr[3] -> Currant
97787 ** aReadr[4] -> Grapefruit
97788 ** aReadr[5] -> Apple
97789 ** aReadr[6] -> Durian
97790 ** aReadr[7] -> EOF
97799 ** aReadr[5] -> Eggplant
97804 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
97827 ** single-threaded operation, there is exactly one instance of this object
97828 ** and for multi-threaded operation there are two or more instances.
97839 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
97856 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
97861 SorterFile file; /* Temp file for level-0 PMAs */
97885 SorterList list; /* List of in-memory records */
97928 ** There are two types of IncrMerger object - single (bUseThread==0) and
97929 ** multi-threaded (bUseThread==1).
97931 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
97946 ** A single-threaded IncrMerger does not open any temporary files of its
97948 ** at offset iStartOff of file pTask->file2. And instead of using a
97950 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
97968 ** size. But I/O is more efficient if it occurs in page-sized blocks where
97970 ** the PMA so that aligned, page-size blocks are written.
97973 int eFWErr; /* Non-zero if in an error state */
97987 ** by this module. If using a separate allocation for each in-memory record
97995 ** has finished passing records to the sorter, or when the in-memory buffer
98028 sqlite3_free(pReadr->aAlloc);
98029 sqlite3_free(pReadr->aBuffer);
98030 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98031 vdbeIncrFree(pReadr->pIncr);
98052 if( p->aMap ){
98053 *ppOut = &p->aMap[p->iReadOff];
98054 p->iReadOff += nByte;
98058 assert( p->aBuffer );
98061 ** p->nBuffer bytes of data from the file into it. Or, if there are less
98062 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
98063 iBuf = p->iReadOff % p->nBuffer;
98069 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
98070 nRead = p->nBuffer;
98072 nRead = (int)(p->iEof - p->iReadOff);
98077 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
98081 nAvail = p->nBuffer - iBuf;
98084 /* The requested data is available in the in-memory buffer. In this
98087 *ppOut = &p->aBuffer[iBuf];
98088 p->iReadOff += nByte;
98090 /* The requested data is not all available in the in-memory buffer.
98091 ** In this case, allocate space at p->aAlloc[] to copy the requested
98092 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
98095 /* Extend the p->aAlloc[] allocation if required. */
98096 if( p->nAlloc<nByte ){
98098 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
98100 aNew = sqlite3Realloc(p->aAlloc, nNew);
98102 p->nAlloc = nNew;
98103 p->aAlloc = aNew;
98107 ** p->aAlloc[]. */
98108 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
98109 p->iReadOff += nAvail;
98110 nRem = nByte - nAvail;
98112 /* The following loop copies up to p->nBuffer bytes per iteration into
98113 ** the p->aAlloc[] buffer. */
98120 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
98123 assert( aNext!=p->aAlloc );
98124 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
98125 nRem -= nCopy;
98128 *ppOut = p->aAlloc;
98141 if( p->aMap ){
98142 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
98144 iBuf = p->iReadOff % p->nBuffer;
98145 if( iBuf && (p->nBuffer-iBuf)>=9 ){
98146 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
98173 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
98174 sqlite3_file *pFd = pFile->pFd;
98175 if( pFd->pMethods->iVersion>=3 ){
98176 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
98196 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
98199 if( pReadr->aMap ){
98200 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98201 pReadr->aMap = 0;
98203 pReadr->iReadOff = iOff;
98204 pReadr->iEof = pFile->iEof;
98205 pReadr->pFd = pFile->pFd;
98207 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
98208 if( rc==SQLITE_OK && pReadr->aMap==0 ){
98209 int pgsz = pTask->pSorter->pgsz;
98210 int iBuf = pReadr->iReadOff % pgsz;
98211 if( pReadr->aBuffer==0 ){
98212 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
98213 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
98214 pReadr->nBuffer = pgsz;
98217 int nRead = pgsz - iBuf;
98218 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
98219 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
98222 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
98240 if( pReadr->iReadOff>=pReadr->iEof ){
98241 IncrMerger *pIncr = pReadr->pIncr;
98245 if( rc==SQLITE_OK && pIncr->bEof==0 ){
98247 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
98265 pReadr->nKey = (int)nRec;
98266 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
98275 ** starting at offset iStart and ending at offset iEof-1. This function
98291 assert( pFile->iEof>iStart );
98292 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
98293 assert( pReadr->aBuffer==0 );
98294 assert( pReadr->aMap==0 );
98300 pReadr->iEof = pReadr->iReadOff + nByte;
98317 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98321 UnpackedRecord *r2 = pTask->pUnpacked;
98323 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98331 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
98335 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
98336 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
98339 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
98344 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98348 UnpackedRecord *r2 = pTask->pUnpacked;
98350 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98363 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98378 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
98380 res = n1 - n2;
98384 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98390 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
98391 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98392 res = res * -1;
98405 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98427 if( (res = v1[i] - v2[i])!=0 ){
98429 res = v1[0] & 0x80 ? -1 : +1;
98435 res = s1 - s2;
98440 res = -1;
98442 res = s1 - s2;
98447 if( *v1 & 0x80 ) res = -1;
98454 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98459 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98460 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
98461 res = res * -1;
98470 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
98473 ** is non-zero and the sorter is able to guarantee a stable sort, nField
98481 ** The sorter can guarantee a stable sort when running in single-threaded
98482 ** mode, but not in multi-threaded mode.
98494 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
98495 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
98509 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
98517 nWorker = SORTER_MAX_MERGE_COUNT-1;
98521 assert( pCsr->pKeyInfo );
98522 assert( !pCsr->isEphemeral );
98523 assert( pCsr->eCurType==CURTYPE_SORTER );
98524 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
98528 pCsr->uc.pSorter = pSorter;
98532 Btree *pBt = db->aDb[0].pBt;
98533 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
98534 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
98535 pKeyInfo->db = 0;
98537 pKeyInfo->nKeyField = nField;
98540 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
98542 pSorter->nTask = nWorker + 1;
98543 pSorter->iPrev = (u8)(nWorker - 1);
98544 pSorter->bUseThreads = (pSorter->nTask>1);
98545 pSorter->db = db;
98546 for(i=0; i<pSorter->nTask; i++){
98547 SortSubtask *pTask = &pSorter->aTask[i];
98548 pTask->pSorter = pSorter;
98554 pSorter->mnPmaSize = szPma * pgsz;
98556 mxCache = db->aDb[0].pSchema->cache_size;
98558 /* A negative cache-size value C indicates that the cache is abs(C)
98560 mxCache = mxCache * -1024;
98565 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
98570 assert( pSorter->iMemory==0 );
98571 pSorter->nMemory = pgsz;
98572 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
98573 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
98577 if( pKeyInfo->nAllField<13
98578 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
98579 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
98581 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
98596 pNext = p->u.pNext;
98606 sqlite3DbFree(db, pTask->pUnpacked);
98608 /* pTask->list.aMemory can only be non-zero if it was handed memory
98610 if( pTask->list.aMemory ){
98611 sqlite3_free(pTask->list.aMemory);
98615 assert( pTask->list.aMemory==0 );
98616 vdbeSorterRecordFree(0, pTask->list.pList);
98618 if( pTask->file.pFd ){
98619 sqlite3OsCloseFree(pTask->file.pFd);
98621 if( pTask->file2.pFd ){
98622 sqlite3OsCloseFree(pTask->file2.pFd);
98630 int iTask = (pTask - pTask->pSorter->aTask);
98631 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98645 int iTask = (pTask - pTask->pSorter->aTask);
98646 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98656 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98669 ** Join thread pTask->thread.
98673 if( pTask->pThread ){
98675 int bDone = pTask->bDone;
98679 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
98682 assert( pTask->bDone==1 );
98683 pTask->bDone = 0;
98684 pTask->pThread = 0;
98697 assert( pTask->pThread==0 && pTask->bDone==0 );
98698 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
98703 ** level-0 PMAs.
98712 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
98715 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
98716 for(i=pSorter->nTask-1; i>=0; i--){
98717 SortSubtask *pTask = &pSorter->aTask[i];
98747 pNew->nTree = N;
98748 pNew->pTask = 0;
98749 pNew->aReadr = (PmaReader*)&pNew[1];
98750 pNew->aTree = (int*)&pNew->aReadr[N];
98761 for(i=0; i<pMerger->nTree; i++){
98762 vdbePmaReaderClear(&pMerger->aReadr[i]);
98775 if( pIncr->bUseThread ){
98776 vdbeSorterJoinThread(pIncr->pTask);
98777 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
98778 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
98781 vdbeMergeEngineFree(pIncr->pMerger);
98792 assert( pSorter->bUseThreads || pSorter->pReader==0 );
98794 if( pSorter->pReader ){
98795 vdbePmaReaderClear(pSorter->pReader);
98796 sqlite3DbFree(db, pSorter->pReader);
98797 pSorter->pReader = 0;
98800 vdbeMergeEngineFree(pSorter->pMerger);
98801 pSorter->pMerger = 0;
98802 for(i=0; i<pSorter->nTask; i++){
98803 SortSubtask *pTask = &pSorter->aTask[i];
98805 pTask->pSorter = pSorter;
98807 if( pSorter->list.aMemory==0 ){
98808 vdbeSorterRecordFree(0, pSorter->list.pList);
98810 pSorter->list.pList = 0;
98811 pSorter->list.szPMA = 0;
98812 pSorter->bUsePMA = 0;
98813 pSorter->iMemory = 0;
98814 pSorter->mxKeysize = 0;
98815 sqlite3DbFree(db, pSorter->pUnpacked);
98816 pSorter->pUnpacked = 0;
98824 assert( pCsr->eCurType==CURTYPE_SORTER );
98825 pSorter = pCsr->uc.pSorter;
98828 sqlite3_free(pSorter->list.aMemory);
98830 pCsr->uc.pSorter = 0;
98836 ** The first argument is a file-handle open on a temporary file. The file
98845 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
98859 ** Allocate space for a file-handle and open a temporary file. If successful,
98860 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
98870 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
98887 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
98891 if( pTask->pUnpacked==0 ){
98892 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
98893 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
98894 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
98895 pTask->pUnpacked->errCode = 0;
98916 res = pTask->xCompare(
98917 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
98922 pp = &p1->u.pNext;
98923 p1 = p1->u.pNext;
98930 pp = &p2->u.pNext;
98931 p2 = p2->u.pNext;
98947 if( p->typeMask==SORTER_TYPE_INTEGER ){
98949 }else if( p->typeMask==SORTER_TYPE_TEXT ){
98956 ** Sort the linked list of records headed at pTask->pList. Return
98969 p = pList->pList;
98970 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
98975 if( pList->aMemory ){
98976 if( (u8*)p==pList->aMemory ){
98979 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
98980 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
98983 pNext = p->u.pNext;
98986 p->u.pNext = 0;
99000 pList->pList = p;
99002 assert( pTask->pUnpacked->errCode==SQLITE_OK
99003 || pTask->pUnpacked->errCode==SQLITE_NOMEM
99005 return pTask->pUnpacked->errCode;
99009 ** Initialize a PMA-writer object.
99018 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
99019 if( !p->aBuffer ){
99020 p->eFWErr = SQLITE_NOMEM_BKPT;
99022 p->iBufEnd = p->iBufStart = (iStart % nBuf);
99023 p->iWriteOff = iStart - p->iBufStart;
99024 p->nBuffer = nBuf;
99025 p->pFd = pFd;
99035 while( nRem>0 && p->eFWErr==0 ){
99037 if( nCopy>(p->nBuffer - p->iBufEnd) ){
99038 nCopy = p->nBuffer - p->iBufEnd;
99041 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
99042 p->iBufEnd += nCopy;
99043 if( p->iBufEnd==p->nBuffer ){
99044 p->eFWErr = sqlite3OsWrite(p->pFd,
99045 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99046 p->iWriteOff + p->iBufStart
99048 p->iBufStart = p->iBufEnd = 0;
99049 p->iWriteOff += p->nBuffer;
99051 assert( p->iBufEnd<p->nBuffer );
99053 nRem -= nCopy;
99058 ** Flush any buffered data to disk and clean up the PMA-writer object.
99059 ** The results of using the PMA-writer after this call are undefined.
99068 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
99069 p->eFWErr = sqlite3OsWrite(p->pFd,
99070 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99071 p->iWriteOff + p->iBufStart
99074 *piEof = (p->iWriteOff + p->iBufEnd);
99075 sqlite3_free(p->aBuffer);
99076 rc = p->eFWErr;
99093 ** Write the current contents of in-memory linked-list pList to a level-0
99094 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
99102 ** * One or more records packed end-to-end in order of ascending keys.
99107 sqlite3 *db = pTask->pSorter->db;
99112 /* Set iSz to the expected size of file pTask->file after writing the PMA.
99114 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
99119 assert( pList->szPMA>0 );
99122 if( pTask->file.pFd==0 ){
99123 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
99124 assert( rc!=SQLITE_OK || pTask->file.pFd );
99125 assert( pTask->file.iEof==0 );
99126 assert( pTask->nPMA==0 );
99131 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
99143 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
99144 pTask->file.iEof);
99145 pTask->nPMA++;
99146 vdbePmaWriteVarint(&writer, pList->szPMA);
99147 for(p=pList->pList; p; p=pNext){
99148 pNext = p->u.pNext;
99149 vdbePmaWriteVarint(&writer, p->nVal);
99150 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
99151 if( pList->aMemory==0 ) sqlite3_free(p);
99153 pList->pList = p;
99154 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
99158 assert( rc!=SQLITE_OK || pList->pList==0 );
99159 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
99175 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
99176 SortSubtask *pTask = pMerger->pTask;
99179 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
99190 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
99191 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
99193 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
99196 if( pReadr1->pFd==0 ){
99198 }else if( pReadr2->pFd==0 ){
99199 iRes = -1;
99201 iRes = pTask->xCompare(pTask, &bCached,
99202 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
99208 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
99213 ** was actually called above, then pTask->pUnpacked now contains
99222 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
99223 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99226 if( pReadr1->pFd ) bCached = 0;
99227 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
99228 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99231 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
99234 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
99239 ** The main routine for background threads that write level-0 PMAs.
99244 assert( pTask->bDone==0 );
99245 rc = vdbeSorterListToPMA(pTask, &pTask->list);
99246 pTask->bDone = 1;
99257 pSorter->bUsePMA = 1;
99258 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
99263 int nWorker = (pSorter->nTask-1);
99267 pSorter->bUsePMA = 1;
99269 /* Select a sub-task to sort and flush the current list of in-memory
99270 ** records to disk. If the sorter is running in multi-threaded mode,
99271 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
99272 ** the background thread from a sub-tasks previous turn is still running,
99273 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
99274 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
99275 ** sub-tasks are prefered as they use background threads - the final
99276 ** sub-task uses the main thread. */
99278 int iTest = (pSorter->iPrev + i + 1) % nWorker;
99279 pTask = &pSorter->aTask[iTest];
99280 if( pTask->bDone ){
99283 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
99289 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
99296 assert( pTask->pThread==0 && pTask->bDone==0 );
99297 assert( pTask->list.pList==0 );
99298 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
99300 aMem = pTask->list.aMemory;
99302 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
99303 pTask->list = pSorter->list;
99304 pSorter->list.pList = 0;
99305 pSorter->list.szPMA = 0;
99307 pSorter->list.aMemory = aMem;
99308 pSorter->nMemory = sqlite3MallocSize(aMem);
99309 }else if( pSorter->list.aMemory ){
99310 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
99311 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
99337 assert( pCsr->eCurType==CURTYPE_SORTER );
99338 pSorter = pCsr->uc.pSorter;
99339 getVarint32NR((const u8*)&pVal->z[1], t);
99341 pSorter->typeMask &= SORTER_TYPE_INTEGER;
99343 pSorter->typeMask &= SORTER_TYPE_TEXT;
99345 pSorter->typeMask = 0;
99353 ** If using the single large allocation mode (pSorter->aMemory!=0), then
99360 ** * The total memory allocated for the in-memory list is greater
99361 ** than (page-size * cache-size), or
99363 ** * The total memory allocated for the in-memory list is greater
99364 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
99366 nReq = pVal->n + sizeof(SorterRecord);
99367 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
99368 if( pSorter->mxPmaSize ){
99369 if( pSorter->list.aMemory ){
99370 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
99373 (pSorter->list.szPMA > pSorter->mxPmaSize)
99374 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
99379 pSorter->list.szPMA = 0;
99380 pSorter->iMemory = 0;
99381 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
99385 pSorter->list.szPMA += nPMA;
99386 if( nPMA>pSorter->mxKeysize ){
99387 pSorter->mxKeysize = nPMA;
99390 if( pSorter->list.aMemory ){
99391 int nMin = pSorter->iMemory + nReq;
99393 if( nMin>pSorter->nMemory ){
99395 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
99396 int iListOff = -1;
99397 if( pSorter->list.pList ){
99398 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
99401 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
99403 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
99406 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
99408 pSorter->list.aMemory = aNew;
99409 pSorter->nMemory = nNew;
99412 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
99413 pSorter->iMemory += ROUND8(nReq);
99414 if( pSorter->list.pList ){
99415 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
99422 pNew->u.pNext = pSorter->list.pList;
99425 memcpy(SRVAL(pNew), pVal->z, pVal->n);
99426 pNew->nVal = pVal->n;
99427 pSorter->list.pList = pNew;
99433 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
99435 ** except that the number-of-bytes varint is omitted from the start.
99440 i64 iStart = pIncr->iStartOff;
99441 SorterFile *pOut = &pIncr->aFile[1];
99442 SortSubtask *pTask = pIncr->pTask;
99443 MergeEngine *pMerger = pIncr->pMerger;
99445 assert( pIncr->bEof==0 );
99449 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
99452 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
99453 int nKey = pReader->nKey;
99458 if( pReader->pFd==0 ) break;
99459 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
99463 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
99464 assert( pIncr->pMerger->pTask==pTask );
99465 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
99468 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
99477 ** multi-threaded IncrMerger objects.
99482 pIncr->pTask->bDone = 1;
99491 assert( pIncr->bUseThread );
99492 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
99502 ** For single-threaded objects, this is accomplished by literally reading
99503 ** keys from pIncr->pMerger and repopulating aFile[0].
99505 ** For multi-threaded objects, all that is required is to wait until the
99517 if( pIncr->bUseThread ){
99518 rc = vdbeSorterJoinThread(pIncr->pTask);
99521 SorterFile f0 = pIncr->aFile[0];
99522 pIncr->aFile[0] = pIncr->aFile[1];
99523 pIncr->aFile[1] = f0;
99527 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99528 pIncr->bEof = 1;
99537 pIncr->aFile[0] = pIncr->aFile[1];
99538 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99539 pIncr->bEof = 1;
99561 pIncr->pMerger = pMerger;
99562 pIncr->pTask = pTask;
99563 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
99564 pTask->file2.iEof += pIncr->mxSz;
99575 ** Set the "use-threads" flag on object pIncr.
99578 pIncr->bUseThread = 1;
99579 pIncr->pTask->file2.iEof -= pIncr->mxSz;
99586 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
99592 int iOut /* Store the result in pMerger->aTree[iOut] */
99600 assert( iOut<pMerger->nTree && iOut>0 );
99602 if( iOut>=(pMerger->nTree/2) ){
99603 i1 = (iOut - pMerger->nTree/2) * 2;
99606 i1 = pMerger->aTree[iOut*2];
99607 i2 = pMerger->aTree[iOut*2+1];
99610 p1 = &pMerger->aReadr[i1];
99611 p2 = &pMerger->aReadr[i2];
99613 if( p1->pFd==0 ){
99615 }else if( p2->pFd==0 ){
99618 SortSubtask *pTask = pMerger->pTask;
99621 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
99622 res = pTask->xCompare(
99623 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
99632 pMerger->aTree[iOut] = iRes;
99639 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
99685 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99689 assert( pMerger->pTask==0 );
99690 pMerger->pTask = pTask;
99692 nTree = pMerger->nTree;
99697 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
99700 ** on this PmaReader before any of the multi-threaded PmaReaders takes
99701 ** better advantage of multi-processor hardware. */
99702 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
99704 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
99709 for(i=pMerger->nTree-1; i>0; i--){
99712 return pTask->pUnpacked->errCode;
99717 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
99719 ** object at (pReadr->pIncr).
99722 ** in the sub-tree headed by pReadr are also initialized. Data is then
99727 ** to be a multi-threaded PmaReader and this function is being called in a
99728 ** background thread. In this case all PmaReaders in the sub-tree are
99736 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
99737 ** this entire function is being run by thread (pTask->thread), that will
99741 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
99742 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
99750 IncrMerger *pIncr = pReadr->pIncr;
99751 SortSubtask *pTask = pIncr->pTask;
99752 sqlite3 *db = pTask->pSorter->db;
99754 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99757 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
99759 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
99760 ** requires two temp files to itself, whereas a single-threaded object
99761 ** only requires a region of pTask->file2. */
99763 int mxSz = pIncr->mxSz;
99765 if( pIncr->bUseThread ){
99766 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
99768 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
99772 /*if( !pIncr->bUseThread )*/{
99773 if( pTask->file2.pFd==0 ){
99774 assert( pTask->file2.iEof>0 );
99775 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
99776 pTask->file2.iEof = 0;
99779 pIncr->aFile[1].pFd = pTask->file2.pFd;
99780 pIncr->iStartOff = pTask->file2.iEof;
99781 pTask->file2.iEof += mxSz;
99787 if( rc==SQLITE_OK && pIncr->bUseThread ){
99789 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
99791 ** pIncr->pTask->thread.
99820 pReader->pIncr->pTask->bDone = 1;
99826 ** If the PmaReader passed as the first argument is not an incremental-reader
99827 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
99831 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
99837 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
99841 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
99842 if( pIncr->bUseThread ){
99844 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
99855 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
99856 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
99861 ** first PMA to read from pTask->file. Assuming no error occurs, it is
99869 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
99870 MergeEngine **ppOut /* OUT: New merge-engine */
99882 PmaReader *pReadr = &pNew->aReadr[i];
99883 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
99884 iOff = pReadr->iEof;
99901 ** nPMA<=16 -> TreeDepth() == 0
99902 ** nPMA<=256 -> TreeDepth() == 1
99903 ** nPMA<=65536 -> TreeDepth() == 2
99916 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
99944 PmaReader *pReadr = &p->aReadr[iIter];
99946 if( pReadr->pIncr==0 ){
99951 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
99955 p = pReadr->pIncr->pMerger;
99961 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
99970 ** that has already written two or more level-0 PMAs to one or more temp
99988 /* If the sorter uses more than one task, then create the top-level
99990 ** one PmaReader per sub-task. */
99991 assert( pSorter->bUseThreads || pSorter->nTask==1 );
99992 if( pSorter->nTask>1 ){
99993 pMain = vdbeMergeEngineNew(pSorter->nTask);
99998 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
99999 SortSubtask *pTask = &pSorter->aTask[iTask];
100000 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
100001 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
100003 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
100006 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
100007 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
100013 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
100014 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
100015 int nReader; /* Number of level-0 PMAs to merge */
100017 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
100028 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
100053 ** (for multi-threaded sorters) so that it can be used to iterate through
100060 SortSubtask *pTask0 = &pSorter->aTask[0];
100063 sqlite3 *db = pTask0->pSorter->db;
100066 for(i=0; i<pSorter->nTask; i++){
100067 pSorter->aTask[i].xCompare = xCompare;
100074 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
100075 if( pSorter->bUseThreads ){
100078 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
100082 pSorter->pReader = pReadr;
100086 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
100088 vdbeIncrMergerSetThreads(pReadr->pIncr);
100089 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
100091 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
100093 assert( pIncr->pTask!=pLast );
100096 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
100101 ** b) If it is using task (nTask-1), it is configured to run
100102 ** in single-threaded mode. This is important, as the
100106 PmaReader *p = &pMain->aReadr[iTask];
100107 assert( p->pIncr==0 || (
100108 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
100109 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
100123 pSorter->pMerger = pMain;
100144 assert( pCsr->eCurType==CURTYPE_SORTER );
100145 pSorter = pCsr->uc.pSorter;
100150 ** from the in-memory list. */
100151 if( pSorter->bUsePMA==0 ){
100152 if( pSorter->list.pList ){
100154 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
100161 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
100165 assert( pSorter->list.pList );
100175 assert( pSorter->pReader==0 );
100196 assert( pCsr->eCurType==CURTYPE_SORTER );
100197 pSorter = pCsr->uc.pSorter;
100198 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
100199 if( pSorter->bUsePMA ){
100200 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
100201 assert( pSorter->bUseThreads==0 || pSorter->pReader );
100202 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
100204 if( pSorter->bUseThreads ){
100205 rc = vdbePmaReaderNext(pSorter->pReader);
100206 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
100209 /*if( !pSorter->bUseThreads )*/ {
100211 assert( pSorter->pMerger!=0 );
100212 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
100213 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
100217 SorterRecord *pFree = pSorter->list.pList;
100218 pSorter->list.pList = pFree->u.pNext;
100219 pFree->u.pNext = 0;
100220 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
100221 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
100235 if( pSorter->bUsePMA ){
100238 if( pSorter->bUseThreads ){
100239 pReader = pSorter->pReader;
100242 /*if( !pSorter->bUseThreads )*/{
100243 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
100245 *pnKey = pReader->nKey;
100246 pKey = pReader->aKey;
100248 *pnKey = pSorter->list.pList->nVal;
100249 pKey = SRVAL(pSorter->list.pList);
100261 assert( pCsr->eCurType==CURTYPE_SORTER );
100262 pSorter = pCsr->uc.pSorter;
100267 pOut->n = nKey;
100269 memcpy(pOut->z, pKey, nKey);
100302 assert( pCsr->eCurType==CURTYPE_SORTER );
100303 pSorter = pCsr->uc.pSorter;
100304 r2 = pSorter->pUnpacked;
100305 pKeyInfo = pCsr->pKeyInfo;
100307 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
100309 r2->nField = nKeyCol;
100311 assert( r2->nField==nKeyCol );
100316 if( r2->aMem[i].flags & MEM_Null ){
100317 *pRes = -1;
100322 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
100329 ** 2020-03-23
100340 ** This file implements virtual-tables for examining the bytecode content
100347 /* An instance of the bytecode() table-valued function.
100351 sqlite3_vtab base; /* Base class - must be first */
100360 sqlite3_vtab_cursor base; /* Base class - must be first */
100375 ** Create a new bytecode() table-valued function.
100419 pNew->db = db;
100420 pNew->bTablesUsed = isTabUsed*2;
100443 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
100444 *ppCursor = &pCur->base;
100452 sqlite3_free(pCur->zP4);
100453 pCur->zP4 = 0;
100454 sqlite3VdbeMemRelease(&pCur->sub);
100455 sqlite3VdbeMemSetNull(&pCur->sub);
100456 if( pCur->needFinalize ){
100457 sqlite3_finalize(pCur->pStmt);
100459 pCur->pStmt = 0;
100460 pCur->needFinalize = 0;
100461 pCur->zType = 0;
100462 pCur->zSchema = 0;
100463 pCur->zName = 0;
100482 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
100484 if( pCur->zP4 ){
100485 sqlite3_free(pCur->zP4);
100486 pCur->zP4 = 0;
100488 if( pCur->zName ){
100489 pCur->zName = 0;
100490 pCur->zType = 0;
100491 pCur->zSchema = 0;
100494 (Vdbe*)pCur->pStmt,
100495 pCur->showSubprograms ? &pCur->sub : 0,
100496 pTab->bTablesUsed,
100497 &pCur->iRowid,
100498 &pCur->iAddr,
100499 &pCur->aOp);
100501 sqlite3VdbeMemSetNull(&pCur->sub);
100502 pCur->aOp = 0;
100513 return pCur->aOp==0;
100526 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
100527 Op *pOp = pCur->aOp + pCur->iAddr;
100528 if( pVTab->bTablesUsed ){
100532 if( i<=2 && pCur->zType==0 ){
100535 int iDb = pOp->p3;
100536 Pgno iRoot = (Pgno)pOp->p2;
100537 sqlite3 *db = pVTab->db;
100538 pSchema = db->aDb[iDb].pSchema;
100539 pCur->zSchema = db->aDb[iDb].zDbSName;
100540 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
100542 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
100543 pCur->zName = pTab->zName;
100544 pCur->zType = "table";
100548 if( pCur->zName==0 ){
100549 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
100551 if( pIdx->tnum==iRoot ){
100552 pCur->zName = pIdx->zName;
100553 pCur->zType = "index";
100563 sqlite3_result_int(ctx, pCur->iAddr);
100566 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
100567 -1, SQLITE_STATIC);
100570 sqlite3_result_int(ctx, pOp->p1);
100573 sqlite3_result_int(ctx, pOp->p2);
100576 sqlite3_result_int(ctx, pOp->p3);
100580 if( pCur->zP4==0 ){
100581 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
100584 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
100587 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
100588 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
100593 sqlite3_result_int(ctx, pOp->p5);
100596 Op *aOp = pCur->aOp;
100598 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
100599 if( pCur->iRowid==pCur->iAddr+1 ){
100602 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
100609 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
100612 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
100615 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
100618 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
100630 *pRowid = pCur->iRowid;
100646 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
100650 pCur->iRowid = 0;
100651 pCur->iAddr = 0;
100652 pCur->showSubprograms = idxNum==0;
100659 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
100660 pCur->needFinalize = 1;
100663 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
100665 if( pCur->pStmt==0 ){
100666 pVTab->base.zErrMsg = sqlite3_mprintf(
100668 pVTab->bTablesUsed ? "tables_used" : "bytecode"
100690 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
100691 pIdxInfo->estimatedCost = (double)100;
100692 pIdxInfo->estimatedRows = 100;
100693 pIdxInfo->idxNum = 0;
100694 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
100695 if( p->usable==0 ) continue;
100696 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
100698 pIdxInfo->aConstraintUsage[i].omit = 1;
100699 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
100701 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
100702 pIdxInfo->aConstraintUsage[i].omit = 1;
100703 pIdxInfo->idxNum = 1;
100767 ** This file contains code use to implement an in-memory rollback journal.
100768 ** The in-memory rollback journal is used to journal transactions for
100771 ** Update: The in-memory journal is also used to temporarily cache
100772 ** smaller journals that are not critical for power-loss recovery.
100790 ** The zChunk array is always at least 8 bytes in size - usually much more.
100807 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
100819 ** This structure is a subclass of sqlite3_file. Each open memory-journal
100824 int nChunkSize; /* In-memory chunk-size */
100827 FileChunk *pFirst; /* Head of in-memory chunk-list */
100837 ** Read data from the in-memory journal file. This is the implementation
100852 if( (iAmt+iOfst)>p->endpoint.iOffset ){
100855 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
100856 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
100858 for(pChunk=p->pFirst;
100859 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
100860 pChunk=pChunk->pNext
100862 iOff += p->nChunkSize;
100865 pChunk = p->readpoint.pChunk;
100869 iChunkOffset = (int)(iOfst%p->nChunkSize);
100871 int iSpace = p->nChunkSize - iChunkOffset;
100872 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
100873 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
100875 nRead -= iSpace;
100877 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
100878 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
100879 p->readpoint.pChunk = pChunk;
100891 pNext = pIter->pNext;
100910 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
100912 nChunk = copy.endpoint.iOffset - iOff;
100914 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
100919 /* No error has occurred. Free the in-memory buffers. */
100925 ** the original before returning. This way, SQLite uses the in-memory
100926 ** journal data to roll back changes made to the internal page-cache
100953 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
100963 /* An in-memory journal file should only ever be appended to. Random
100965 ** the in-memory journal is being used by a connection using the
100966 ** atomic-write optimization. In this case the first 28 bytes of the
100968 assert( iOfst<=p->endpoint.iOffset );
100969 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
100972 if( iOfst==0 && p->pFirst ){
100973 assert( p->nChunkSize>iAmt );
100974 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
100977 FileChunk *pChunk = p->endpoint.pChunk;
100978 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
100979 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
100984 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
100988 pNew->pNext = 0;
100990 assert( p->pFirst );
100991 pChunk->pNext = pNew;
100993 assert( !p->pFirst );
100994 p->pFirst = pNew;
100996 pChunk = p->endpoint.pChunk = pNew;
101000 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
101002 nWrite -= iSpace;
101003 p->endpoint.iOffset += iSpace;
101012 ** Truncate the in-memory file.
101016 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
101017 if( size<p->endpoint.iOffset ){
101020 memjrnlFreeChunks(p->pFirst);
101021 p->pFirst = 0;
101023 i64 iOff = p->nChunkSize;
101024 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
101025 iOff += p->nChunkSize;
101028 memjrnlFreeChunks(pIter->pNext);
101029 pIter->pNext = 0;
101033 p->endpoint.pChunk = pIter;
101034 p->endpoint.iOffset = size;
101035 p->readpoint.pChunk = 0;
101036 p->readpoint.iOffset = 0;
101046 memjrnlFreeChunks(p->pFirst);
101054 ** syncing an in-memory journal is a no-op.
101066 *pSize = (sqlite_int64) p->endpoint.iOffset;
101101 ** all content is always stored in main-memory. Finally, if nSpill is a
101102 ** positive value, then the journal file is initially created in-memory
101116 /* Zero the file-handle object. If nSpill was passed zero, initialize
101119 ** made on the journal file-handle. */
101126 p->nChunkSize = nSpill;
101128 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
101129 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
101132 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
101133 p->nSpill = nSpill;
101134 p->flags = flags;
101135 p->zJournal = zName;
101136 p->pVfs = pVfs;
101141 ** Open an in-memory journal file.
101144 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
101151 ** in-memory-only journal file (i.e. is one that was opened with a +ve
101158 if( pJfd->pMethods==&MemJournalMethods && (
101160 p->nSpill>0
101165 NEVER(p->nSpill>0)
101168 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
101178 ** The file-handle passed as the only argument is open on a journal file.
101183 return p->pMethods==&MemJournalMethods;
101188 ** pVfs to create the underlying on-disk files.
101191 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
101222 for(pWin=pList; pWin; pWin=pWin->pNextWin){
101224 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
101226 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
101228 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
101230 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
101232 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
101254 ** return from the top-level walk call.
101264 rc = pWalker->xExprCallback(pWalker, pExpr);
101267 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
101268 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
101269 if( pExpr->pRight ){
101271 pExpr = pExpr->pRight;
101275 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
101277 if( pExpr->x.pList ){
101278 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
101282 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
101303 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
101304 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
101311 ** This is a no-op callback for Walker->xSelectCallback2. If this
101312 ** callback is set, then the Select->pWinDefn list is traversed.
101317 /* No-op */
101327 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
101328 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
101329 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
101330 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
101331 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
101332 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
101334 if( p->pWinDefn ){
101336 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
101337 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
101339 || pWalker->xSelectCallback2==sqlite3SelectPopWith
101344 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
101364 pSrc = p->pSrc;
101366 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
101367 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
101370 if( pItem->fg.isTabFunc
101371 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
101383 ** on the compound select chain, p->pPrior.
101388 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
101395 ** is a no-op returning WRC_Continue.
101400 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
101402 rc = pWalker->xSelectCallback(pWalker, p);
101409 if( pWalker->xSelectCallback2 ){
101410 pWalker->xSelectCallback2(pWalker, p);
101412 p = p->pPrior;
101422 pWalker->walkerDepth++;
101427 pWalker->walkerDepth--;
101432 ** No-op routine for the parse-tree walker.
101446 ** No-op routine for the parse-tree walker for SELECT statements.
101486 ** is a helper function - a callback for the tree walker.
101491 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
101505 ** Turn the pExpr expression into an alias for the iCol-th column of the
101526 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
101528 int nSubquery /* Number of subqueries that the label is moving */
101530 Expr *pOrig; /* The iCol-th column of the result set */
101534 assert( iCol>=0 && iCol<pEList->nExpr );
101535 pOrig = pEList->a[iCol].pExpr;
101537 db = pParse->db;
101539 if( db->mallocFailed ){
101545 if( pExpr->op==TK_COLLATE ){
101547 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
101553 if( ALWAYS(pExpr->y.pWin!=0) ){
101554 pExpr->y.pWin->pOwner = pExpr;
101578 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
101579 zSpan = pItem->zEName;
101597 ** Return TRUE if the double-quoted string mis-feature should be supported.
101600 if( db->init.busy ) return 1; /* Always support for legacy schemas */
101601 if( pTopNC->ncFlags & NC_IsDDL ){
101603 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
101606 return (db->flags & SQLITE_DqsDDL)!=0;
101609 return (db->flags & SQLITE_DqsDML)!=0;
101614 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
101621 n = pExpr->iColumn;
101623 pExTab = pExpr->y.pTab;
101625 if( (pExTab->tabFlags & TF_HasGenerated)!=0
101626 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
101628 testcase( pExTab->nCol==BMS-1 );
101629 testcase( pExTab->nCol==BMS );
101630 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
101632 testcase( n==BMS-1 );
101634 if( n>=BMS ) n = BMS-1;
101651 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
101653 pNew->iTable = pMatch->iCursor;
101654 pNew->iColumn = iColumn;
101655 pNew->y.pTab = pMatch->pTab;
101656 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
101668 ** pExpr->iDb Set the index in db->aDb[] of the database X
101670 ** pExpr->iTable Set to the cursor number for the table obtained
101672 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
101674 ** pExpr->iColumn Set to the column number within the table.
101675 ** pExpr->op Set to TK_COLUMN.
101676 ** pExpr->pLeft Any expression this points to is deleted
101677 ** pExpr->pRight Any expression this points to is deleted.
101701 sqlite3 *db = pParse->db; /* The database connection */
101706 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
101716 /* Initialize the node to no-match */
101717 pExpr->iTable = -1;
101725 testcase( pNC->ncFlags & NC_PartIdx );
101726 testcase( pNC->ncFlags & NC_IsCheck );
101727 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
101734 for(i=0; i<db->nDb; i++){
101735 assert( db->aDb[i].zDbSName );
101736 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
101737 pSchema = db->aDb[i].pSchema;
101741 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
101744 pSchema = db->aDb[0].pSchema;
101745 zDb = db->aDb[0].zDbSName;
101750 /* Start at the inner-most context and move outward until a match is found */
101754 SrcList *pSrcList = pNC->pSrcList;
101757 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
101759 pTab = pItem->pTab;
101760 assert( pTab!=0 && pTab->zName!=0 );
101761 assert( pTab->nCol>0 || pParse->nErr );
101762 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
101763 if( pItem->fg.isNestedFrom ){
101768 ** This pItem -------------^
101771 assert( pItem->pSelect!=0 );
101772 pEList = pItem->pSelect->pEList;
101774 assert( pEList->nExpr==pTab->nCol );
101775 for(j=0; j<pEList->nExpr; j++){
101776 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
101780 if( pItem->fg.isUsing==0
101781 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101789 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
101790 /* An INNER or LEFT JOIN. Use the left-most table */
101793 if( (pItem->fg.jointype & JT_LEFT)==0 ){
101794 /* A RIGHT JOIN. Use the right-most table */
101800 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101806 pExpr->iColumn = j;
101807 pEList->a[j].fg.bUsed = 1;
101809 if( pEList->a[j].fg.bUsingTerm ) break;
101817 if( pTab->pSchema!=pSchema ) continue;
101820 zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
101826 if( IN_RENAME_OBJECT && pItem->zAlias ){
101827 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
101831 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
101832 if( pCol->hName==hCol
101833 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101836 if( pItem->fg.isUsing==0
101837 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101845 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
101846 /* An INNER or LEFT JOIN. Use the left-most table */
101849 if( (pItem->fg.jointype & JT_LEFT)==0 ){
101850 /* A RIGHT JOIN. Use the right-most table */
101856 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101861 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
101862 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
101863 if( pItem->fg.isNestedFrom ){
101875 pExpr->iTable = pMatch->iCursor;
101877 pExpr->y.pTab = pMatch->pTab;
101878 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
101881 pSchema = pExpr->y.pTab->pSchema;
101894 if( pParse->pTriggerTab!=0 ){
101895 int op = pParse->eTriggerOp;
101897 if( pParse->bReturning ){
101898 if( (pNC->ncFlags & NC_UBaseReg)!=0
101899 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
101901 pExpr->iTable = op!=TK_DELETE;
101902 pTab = pParse->pTriggerTab;
101905 pExpr->iTable = 1;
101906 pTab = pParse->pTriggerTab;
101908 pExpr->iTable = 0;
101909 pTab = pParse->pTriggerTab;
101914 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
101915 Upsert *pUpsert = pNC->uNC.pUpsert;
101917 pTab = pUpsert->pUpsertSrc->a[0].pTab;
101918 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
101926 pSchema = pTab->pSchema;
101928 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
101929 if( pCol->hName==hCol
101930 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101932 if( iCol==pTab->iPKey ){
101933 iCol = -1;
101938 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
101939 /* IMP: R-51414-32910 */
101940 iCol = -1;
101942 if( iCol<pTab->nCol ){
101946 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
101947 testcase( iCol==(-1) );
101950 pExpr->iColumn = iCol;
101951 pExpr->y.pTab = pTab;
101954 pExpr->iTable = pNC->uNC.pUpsert->regData +
101962 pExpr->y.pTab = pTab;
101963 if( pParse->bReturning ){
101965 pExpr->op2 = TK_COLUMN;
101966 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
101969 pExpr->iColumn = (i16)iCol;
101973 pExpr->affExpr = SQLITE_AFF_INTEGER;
101974 }else if( pExpr->iTable==0 ){
101977 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
101981 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
101997 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
101999 && ALWAYS(VisibleRowid(pMatch->pTab))
102002 pExpr->iColumn = -1;
102003 pExpr->affExpr = SQLITE_AFF_INTEGER;
102008 ** might refer to an result-set alias. This happens, for example, when
102018 ** The ability to use an output result-set column in the WHERE, GROUP BY,
102025 && (pNC->ncFlags & NC_UEList)!=0
102028 pEList = pNC->uNC.pEList;
102030 for(j=0; j<pEList->nExpr; j++){
102031 char *zAs = pEList->a[j].zEName;
102032 if( pEList->a[j].fg.eEName==ENAME_NAME
102036 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
102037 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
102038 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
102039 pOrig = pEList->a[j].pExpr;
102040 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
102045 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
102070 pNC = pNC->pNext;
102077 ** supplied) and the value of Z is enclosed in double-quotes, then
102082 ** Because no reference was made to outer contexts, the pNC->nRef
102086 assert( pExpr->op==TK_ID );
102090 /* If a double-quoted identifier does not match any known column name,
102094 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
102105 "double-quoted string literal: \"%w\"", zCol);
102107 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
102109 pExpr->op = TK_STRING;
102110 memset(&pExpr->y, 0, sizeof(pExpr->y));
102130 if( pFJMatch->nExpr==cnt-1 ){
102134 sqlite3ExprDelete(db, pExpr->pLeft);
102135 pExpr->pLeft = 0;
102136 sqlite3ExprDelete(db, pExpr->pRight);
102137 pExpr->pRight = 0;
102139 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
102140 pExpr->op = TK_FUNCTION;
102141 pExpr->u.zToken = "coalesce";
102142 pExpr->x.pList = pFJMatch;
102158 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102159 pParse->checkSchema = 1;
102160 pTopNC->nNcErr++;
102166 sqlite3ExprDelete(db, pExpr->pLeft);
102167 pExpr->pLeft = 0;
102168 sqlite3ExprDelete(db, pExpr->pRight);
102169 pExpr->pRight = 0;
102187 if( pExpr->iColumn>=0 && pMatch!=0 ){
102188 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
102191 pExpr->op = eNewExprOp;
102196 if( pParse->db->xAuth
102197 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
102199 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
102206 pTopNC->nRef++;
102208 pTopNC = pTopNC->pNext;
102223 SrcItem *pItem = &pSrc->a[iSrc];
102226 pTab = p->y.pTab = pItem->pTab;
102227 p->iTable = pItem->iCursor;
102228 if( p->y.pTab->iPKey==iCol ){
102229 p->iColumn = -1;
102231 p->iColumn = (ynVar)iCol;
102232 if( (pTab->tabFlags & TF_HasGenerated)!=0
102233 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
102235 testcase( pTab->nCol==63 );
102236 testcase( pTab->nCol==64 );
102237 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
102240 testcase( iCol==BMS-1 );
102241 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
102250 ** pNC->ncFlags values determined by validMask.
102272 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
102274 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
102277 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
102280 if( pExpr ) pExpr->op = TK_NULL;
102281 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102285 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
102289 ** Return 1024 times this value. Or return -1 if p is not a floating point
102293 double r = -1.0;
102294 if( p->op!=TK_FLOAT ) return -1;
102296 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
102298 if( r>1.0 ) return -1;
102317 pNC = pWalker->u.pNC;
102319 pParse = pNC->pParse;
102320 assert( pParse==pWalker->pParse );
102323 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
102324 SrcList *pSrcList = pNC->pSrcList;
102326 for(i=0; i<pNC->pSrcList->nSrc; i++){
102327 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
102331 switch( pExpr->op ){
102339 SrcList *pSrcList = pNC->pSrcList;
102341 assert( pSrcList && pSrcList->nSrc>=1 );
102342 pItem = pSrcList->a;
102343 pExpr->op = TK_COLUMN;
102345 pExpr->y.pTab = pItem->pTab;
102346 pExpr->iTable = pItem->iCursor;
102347 pExpr->iColumn--;
102348 pExpr->affExpr = SQLITE_AFF_INTEGER;
102354 ** "expr IS NOT NULL" --> "TRUE"
102355 ** "expr IS NULL" --> "FALSE"
102360 ** If this optimization occurs, also restore the NameContext ref-counts
102371 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
102372 anRef[i] = p->nRef;
102374 sqlite3WalkExpr(pWalker, pExpr->pLeft);
102375 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
102378 if( pExpr->op==TK_NOTNULL ){
102379 pExpr->u.zToken = "true";
102382 pExpr->u.zToken = "false";
102385 pExpr->op = TK_TRUEFALSE;
102386 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
102387 p->nRef = anRef[i];
102389 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
102390 pExpr->pLeft = 0;
102400 ** be one call to lookupName(). Then the compiler will in-line
102410 if( pExpr->op==TK_ID ){
102414 zColumn = pExpr->u.zToken;
102416 Expr *pLeft = pExpr->pLeft;
102417 testcase( pNC->ncFlags & NC_IdxExpr );
102418 testcase( pNC->ncFlags & NC_GenCol );
102421 pRight = pExpr->pRight;
102422 if( pRight->op==TK_ID ){
102425 assert( pRight->op==TK_DOT );
102427 zDb = pLeft->u.zToken;
102428 pLeft = pRight->pLeft;
102429 pRight = pRight->pRight;
102432 zTable = pLeft->u.zToken;
102433 zColumn = pRight->u.zToken;
102437 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
102446 ExprList *pList = pExpr->x.pList; /* The argument list */
102447 int n = pList ? pList->nExpr : 0; /* Number of arguments */
102453 u8 enc = ENC(pParse->db); /* The database encoding */
102454 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
102456 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
102459 zId = pExpr->u.zToken;
102460 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
102462 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
102469 is_agg = pDef->xFinalize!=0;
102470 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
102473 pExpr->iTable = exprProbability(pList->a[1].pExpr);
102474 if( pExpr->iTable<0 ){
102478 pNC->nNcErr++;
102481 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
102483 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
102484 ** short-hand for likelihood(X,0.0625).
102485 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
102487 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
102490 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
102495 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
102500 pNC->nNcErr++;
102502 pExpr->op = TK_NULL;
102507 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
102514 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
102515 /* Clearly non-deterministic functions like random(), but also
102521 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
102525 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
102526 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
102528 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
102529 && pParse->nested==0
102530 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
102532 /* Internal-use-only functions are disallowed unless the
102534 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
102539 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
102548 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
102549 || (pDef->xValue==0 && pDef->xInverse==0)
102550 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
102552 if( pDef && pDef->xValue==0 && pWin ){
102556 pNC->nNcErr++;
102558 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
102559 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
102560 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
102563 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
102569 pNC->nNcErr++;
102573 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
102575 pNC->nNcErr++;
102579 else if( no_such_func && pParse->db->init.busy==0
102581 && pParse->explain==0
102585 pNC->nNcErr++;
102589 pNC->nNcErr++;
102594 "FILTER may not be used with non-aggregate %#T()",
102597 pNC->nNcErr++;
102605 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
102607 pNC->ncFlags &= ~NC_AllowAgg;
102620 Select *pSel = pNC->pWinSelect;
102621 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
102623 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
102624 if( pParse->db->mallocFailed ) break;
102626 sqlite3WalkExprList(pWalker, pWin->pPartition);
102627 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
102628 sqlite3WalkExpr(pWalker, pWin->pFilter);
102630 pNC->ncFlags |= NC_HasWin;
102635 pExpr->op = TK_AGG_FUNCTION;
102636 pExpr->op2 = 0;
102639 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
102644 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
102646 pExpr->op2++;
102647 pNC2 = pNC2->pNext;
102653 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
102654 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
102655 pNC2->ncFlags |= NC_HasAgg
102656 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
102660 pNC->ncFlags |= savedAllowFlags;
102662 /* FIX ME: Compute pExpr->affinity based on the expected return
102669 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
102672 testcase( pExpr->op==TK_IN );
102674 int nRef = pNC->nRef;
102675 testcase( pNC->ncFlags & NC_IsCheck );
102676 testcase( pNC->ncFlags & NC_PartIdx );
102677 testcase( pNC->ncFlags & NC_IdxExpr );
102678 testcase( pNC->ncFlags & NC_GenCol );
102679 if( pNC->ncFlags & NC_SelfRef ){
102682 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
102684 assert( pNC->nRef>=nRef );
102685 if( nRef!=pNC->nRef ){
102687 pNC->ncFlags |= NC_VarSelect;
102693 testcase( pNC->ncFlags & NC_IsCheck );
102694 testcase( pNC->ncFlags & NC_PartIdx );
102695 testcase( pNC->ncFlags & NC_IdxExpr );
102696 testcase( pNC->ncFlags & NC_GenCol );
102703 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
102707 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
102710 if( pRight->op==TK_TRUEFALSE ){
102711 pExpr->op2 = pExpr->op;
102712 pExpr->op = TK_TRUTH;
102726 if( pParse->db->mallocFailed ) break;
102727 assert( pExpr->pLeft!=0 );
102728 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
102729 if( pExpr->op==TK_BETWEEN ){
102731 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
102733 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
102736 assert( pExpr->pRight!=0 );
102737 nRight = sqlite3ExprVectorSize(pExpr->pRight);
102740 testcase( pExpr->op==TK_EQ );
102741 testcase( pExpr->op==TK_NE );
102742 testcase( pExpr->op==TK_LT );
102743 testcase( pExpr->op==TK_LE );
102744 testcase( pExpr->op==TK_GT );
102745 testcase( pExpr->op==TK_GE );
102746 testcase( pExpr->op==TK_IS );
102747 testcase( pExpr->op==TK_ISNOT );
102748 testcase( pExpr->op==TK_BETWEEN );
102750 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102755 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
102756 return pParse->nErr ? WRC_Abort : WRC_Continue;
102763 ** to the AS-name of one of the terms of the expression list. If it is,
102780 if( pE->op==TK_ID ){
102783 zCol = pE->u.zToken;
102784 for(i=0; i<pEList->nExpr; i++){
102785 if( pEList->a[i].fg.eEName==ENAME_NAME
102786 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
102804 ** Attempt to match pE against result set columns in the left-most
102806 ** as an indication to the caller that it should sort by the i-th column.
102807 ** The left-most column is 1. In other words, the value returned is the
102811 ** If there is no match, return 0. Return -1 if an error occurs.
102823 u8 savedSuppErr; /* Saved value of db->suppressErr */
102826 pEList = pSelect->pEList;
102832 nc.pSrcList = pSelect->pSrc;
102836 db = pParse->db;
102837 savedSuppErr = db->suppressErr;
102838 db->suppressErr = 1;
102840 db->suppressErr = savedSuppErr;
102844 ** in the result set. Return an 1-based index of the matching
102845 ** result-set entry.
102847 for(i=0; i<pEList->nExpr; i++){
102848 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
102858 ** Generate an ORDER BY or GROUP BY term out-of-range error.
102863 int i, /* The index (1-based) of the term out of range */
102868 "%r %s BY term out of range - should be "
102870 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102882 ** beginning with the left-most SELECT and working toward the right.
102898 pOrderBy = pSelect->pOrderBy;
102900 db = pParse->db;
102901 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
102905 for(i=0; i<pOrderBy->nExpr; i++){
102906 pOrderBy->a[i].fg.done = 0;
102908 pSelect->pNext = 0;
102909 while( pSelect->pPrior ){
102910 pSelect->pPrior->pNext = pSelect;
102911 pSelect = pSelect->pPrior;
102916 pEList = pSelect->pEList;
102918 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
102919 int iCol = -1;
102921 if( pItem->fg.done ) continue;
102922 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
102925 if( iCol<=0 || iCol>pEList->nExpr ){
102926 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
102944 if( !db->mallocFailed ){
102960 pNew->flags |= EP_IntValue;
102961 pNew->u.iValue = iCol;
102962 if( pItem->pExpr==pE ){
102963 pItem->pExpr = pNew;
102965 Expr *pParent = pItem->pExpr;
102966 assert( pParent->op==TK_COLLATE );
102967 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
102968 assert( pParent->pLeft==pE );
102969 pParent->pLeft = pNew;
102972 pItem->u.x.iOrderByCol = (u16)iCol;
102974 pItem->fg.done = 1;
102979 pSelect = pSelect->pNext;
102981 for(i=0; i<pOrderBy->nExpr; i++){
102982 if( pOrderBy->a[i].fg.done==0 ){
102999 ** return non-zero. Return zero if no errors are seen.
103008 sqlite3 *db = pParse->db;
103012 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
103013 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
103017 pEList = pSelect->pEList;
103019 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103020 if( pItem->u.x.iOrderByCol ){
103021 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
103022 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
103025 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
103038 Window *pWin = pExpr->y.pWin;
103049 if( pSelect->pWin ){
103067 ** If the order-by term is an integer I between 1 and N (where N is the
103069 ** in the resolution is a copy of the I-th result-set expression. If
103070 ** the order-by term is an identifier that corresponds to the AS-name of
103071 ** a result-set expression, then the term resolves to a copy of the
103072 ** result-set expression. Otherwise, the expression is resolved in
103073 ** the usual way - using sqlite3ResolveExprNames().
103092 nResult = pSelect->pEList->nExpr;
103093 pParse = pNC->pParse;
103094 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103095 Expr *pE = pItem->pExpr;
103099 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
103101 /* If an AS-name match is found, mark this ORDER BY column as being
103102 ** a copy of the iCol-th result-set column. The subsequent call to
103104 ** copy of the iCol-th result-set expression. */
103105 pItem->u.x.iOrderByCol = (u16)iCol;
103112 ** order-by term to a copy of the result-set expression */
103117 pItem->u.x.iOrderByCol = (u16)iCol;
103122 pItem->u.x.iOrderByCol = 0;
103126 for(j=0; j<pSelect->pEList->nExpr; j++){
103127 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
103132 pItem->u.x.iOrderByCol = j+1;
103150 Select *pLeftmost; /* Left-most of SELECT of a compound */
103155 if( p->selFlags & SF_Resolved ){
103158 pOuterNC = pWalker->u.pNC;
103159 pParse = pWalker->pParse;
103160 db = pParse->db;
103170 if( (p->selFlags & SF_Expanded)==0 ){
103172 return pParse->nErr ? WRC_Abort : WRC_Prune;
103175 isCompound = p->pPrior!=0;
103179 assert( (p->selFlags & SF_Expanded)!=0 );
103180 assert( (p->selFlags & SF_Resolved)==0 );
103181 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
103182 p->selFlags |= SF_Resolved;
103191 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
103197 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
103198 ** as if it were part of the sub-query, not the parent. This block
103199 ** moves the pOrderBy down to the sub-query. It will be moved back
103201 if( p->selFlags & SF_Converted ){
103202 Select *pSub = p->pSrc->a[0].pSelect;
103203 assert( p->pSrc->nSrc==1 && p->pOrderBy );
103204 assert( pSub->pPrior && pSub->pOrderBy==0 );
103205 pSub->pOrderBy = p->pOrderBy;
103206 p->pOrderBy = 0;
103211 for(i=0; i<p->pSrc->nSrc; i++){
103212 SrcItem *pItem = &p->pSrc->a[i];
103213 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
103214 int nRef = pOuterNC ? pOuterNC->nRef : 0;
103215 const char *zSavedContext = pParse->zAuthContext;
103217 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
103218 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
103219 pParse->zAuthContext = zSavedContext;
103220 if( pParse->nErr ) return WRC_Abort;
103221 assert( db->mallocFailed==0 );
103224 ** expressions in the sub-select were resolved, the sub-select
103230 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
103231 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
103236 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
103237 ** resolve the result-set expression list.
103240 sNC.pSrcList = p->pSrc;
103244 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
103247 /* If there are no aggregate functions in the result-set, and no GROUP BY
103250 assert( (p->selFlags & SF_Aggregate)==0 );
103251 pGroupBy = p->pGroupBy;
103255 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
103260 /* Add the output column list to the name-context before parsing the
103266 ** re-evaluated for each reference to it.
103269 sNC.uNC.pEList = p->pEList;
103271 if( p->pHaving ){
103272 if( (p->selFlags & SF_Aggregate)==0 ){
103273 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
103276 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
103278 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
103280 /* Resolve names in table-valued-function arguments */
103281 for(i=0; i<p->pSrc->nSrc; i++){
103282 SrcItem *pItem = &p->pSrc->a[i];
103283 if( pItem->fg.isTabFunc
103284 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
103293 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
103294 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
103295 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
103310 ** the sub-query back to the parent query. At this point each term
103314 if( p->selFlags & SF_Converted ){
103315 Select *pSub = p->pSrc->a[0].pSelect;
103316 p->pOrderBy = pSub->pOrderBy;
103317 pSub->pOrderBy = 0;
103322 ** below, after all of the result-sets for all of the elements of
103325 ** If there is an ORDER BY clause on a term of a compound-select other
103326 ** than the right-most term, then that is a syntax error. But the error
103330 if( p->pOrderBy!=0
103331 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
103332 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
103336 if( db->mallocFailed ){
103347 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
103350 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
103351 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
103361 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
103362 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
103368 p = p->pPrior;
103384 ** table columns and result-set columns. At the same time, do error
103392 ** the symbolic name assigned to an ATTACH-ed database.
103403 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
103407 ** To resolve result-set references, look for expression nodes of the
103408 ** form Z (with no X and Y prefix) where the Z matches the right-hand
103409 ** size of an AS clause in the result-set of a SELECT. The Z expression
103410 ** is replaced by a copy of the left-hand side of the result-set expression.
103411 ** Table-name and function resolution occurs on the substituted expression
103438 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103439 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103440 w.pParse = pNC->pParse;
103442 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
103446 w.pParse->nHeight += pExpr->nHeight;
103447 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103453 w.pParse->nHeight -= pExpr->nHeight;
103457 testcase( pNC->ncFlags & NC_HasAgg );
103458 testcase( pNC->ncFlags & NC_HasWin );
103459 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
103460 pNC->ncFlags |= savedHasAgg;
103461 return pNC->nNcErr>0 || w.pParse->nErr>0;
103477 w.pParse = pNC->pParse;
103482 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103483 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103484 for(i=0; i<pList->nExpr; i++){
103485 Expr *pExpr = pList->a[i].pExpr;
103488 w.pParse->nHeight += pExpr->nHeight;
103489 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103495 w.pParse->nHeight -= pExpr->nHeight;
103499 testcase( pNC->ncFlags & NC_HasAgg );
103500 testcase( pNC->ncFlags & NC_HasWin );
103501 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
103502 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
103503 savedHasAgg |= pNC->ncFlags &
103505 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103507 if( w.pParse->nErr>0 ) return WRC_Abort;
103509 pNC->ncFlags |= savedHasAgg;
103515 ** decendents of the SELECT, including compounds off of p->pPrior,
103546 ** ------------
103554 ** nodes of the expression is set to -1 and the Expr.iColumn value is
103566 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
103567 NameContext sNC; /* Name context for pParse->pNewTable */
103577 sSrc.a[0].zName = pTab->zName;
103579 sSrc.a[0].iCursor = -1;
103580 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
103581 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
103620 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
103621 return pTab->aCol[iCol].affinity;
103628 ** or a sub-select with a column as the return value, then the
103643 assert( pExpr->op==TK_COLLATE
103644 || pExpr->op==TK_IF_NULL_ROW
103645 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
103646 pExpr = pExpr->pLeft;
103649 op = pExpr->op;
103650 if( op==TK_REGISTER ) op = pExpr->op2;
103653 if( pExpr->y.pTab ){
103654 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103659 assert( pExpr->x.pSelect!=0 );
103660 assert( pExpr->x.pSelect->pEList!=0 );
103661 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
103662 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
103667 return sqlite3AffinityType(pExpr->u.zToken, 0);
103671 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
103672 assert( pExpr->iColumn < pExpr->iTable );
103673 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
103675 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
103680 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
103682 return pExpr->affExpr;
103690 ** If a memory allocation error occurs, that fact is recorded in pParse->db
103699 if( pCollName->n>0 ){
103700 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
103702 pNew->pLeft = pExpr;
103703 pNew->flags |= EP_Collate|EP_Skip;
103725 assert( pExpr->op==TK_COLLATE );
103726 pExpr = pExpr->pLeft;
103740 assert( pExpr->x.pList->nExpr>0 );
103741 assert( pExpr->op==TK_FUNCTION );
103742 pExpr = pExpr->x.pList->a[0].pExpr;
103744 assert( pExpr->op==TK_COLLATE );
103745 pExpr = pExpr->pLeft;
103766 sqlite3 *db = pParse->db;
103770 int op = p->op;
103771 if( op==TK_REGISTER ) op = p->op2;
103774 if( p->y.pTab!=0 ){
103775 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
103777 int j = p->iColumn;
103779 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
103786 p = p->pLeft;
103791 p = p->x.pList->a[0].pExpr;
103796 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
103799 if( p->flags & EP_Collate ){
103800 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
103801 p = p->pLeft;
103803 Expr *pNext = p->pRight;
103806 assert( p->x.pList==0 || p->pRight==0 );
103807 if( p->x.pList!=0 && !db->mallocFailed ){
103809 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
103810 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
103811 pNext = p->x.pList->a[i].pExpr;
103840 if( p==0 ) p = pParse->db->pDfltColl;
103851 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
103883 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
103884 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
103885 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
103886 assert( pExpr->pLeft );
103887 aff = sqlite3ExprAffinity(pExpr->pLeft);
103888 if( pExpr->pRight ){
103889 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
103891 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
103948 if( pLeft->flags & EP_Collate ){
103950 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
103971 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
103973 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
103994 if( pParse->nErr ) return 0;
104001 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
104003 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
104023 ** is a sub-select, return the number of columns in the sub-select. For
104027 u8 op = pExpr->op;
104028 if( op==TK_REGISTER ) op = pExpr->op2;
104031 return pExpr->x.pList->nExpr;
104034 return pExpr->x.pSelect->pEList->nExpr;
104041 ** Return a pointer to a subexpression of pVector that is the i-th
104051 ** just the expression for the i-th term of the result set, and may
104056 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
104058 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
104059 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
104061 return pVector->x.pSelect->pEList->a[i].pExpr;
104064 return pVector->x.pList->a[i].pExpr;
104073 ** the iField-th column of the vector expression pVector.
104093 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
104098 if( pVector->op==TK_SELECT ){
104106 ** pLeft->iTable: First in an array of register holding result, or 0
104118 pRet->iTable = nField;
104119 pRet->iColumn = iField;
104120 pRet->pLeft = pVector;
104123 if( pVector->op==TK_VECTOR ){
104126 ppVector = &pVector->x.pList->a[iField].pExpr;
104134 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
104142 ** sub-select returns more than one column, the first in an array
104150 if( pExpr->op==TK_SELECT ){
104158 ** Argument pVector points to a vector expression - either a TK_VECTOR
104166 ** containing the results of the sub-select.
104183 u8 op = pVector->op;
104187 return pVector->iTable+iField;
104191 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
104196 *ppExpr = pVector->x.pList->a[iField].pExpr;
104209 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
104210 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
104211 ** otherwise: op==pExpr->op and p5==0
104220 Vdbe *v = pParse->pVdbe;
104221 Expr *pLeft = pExpr->pLeft;
104222 Expr *pRight = pExpr->pRight;
104233 if( pParse->nErr ) return;
104238 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
104239 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
104240 || pExpr->op==TK_LT || pExpr->op==TK_GT
104241 || pExpr->op==TK_LE || pExpr->op==TK_GE
104243 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
104244 || (pExpr->op==TK_ISNOT && op==TK_NE) );
104245 assert( p5==0 || pExpr->op!=op );
104246 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
104258 Expr *pL = 0, *pR = 0;
104263 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2);
104265 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted);
104274 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
104284 if( i==nLeft-1 ){
104292 if( i==nLeft-2 ) opx = op;
104310 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
104331 if( p->nHeight>*pnHeight ){
104332 *pnHeight = p->nHeight;
104339 for(i=0; i<p->nExpr; i++){
104340 heightOfExpr(p->a[i].pExpr, pnHeight);
104346 for(p=pSelect; p; p=p->pPrior){
104347 heightOfExpr(p->pWhere, pnHeight);
104348 heightOfExpr(p->pHaving, pnHeight);
104349 heightOfExpr(p->pLimit, pnHeight);
104350 heightOfExprList(p->pEList, pnHeight);
104351 heightOfExprList(p->pGroupBy, pnHeight);
104352 heightOfExprList(p->pOrderBy, pnHeight);
104367 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104368 if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight;
104370 heightOfSelect(p->x.pSelect, &nHeight);
104371 }else if( p->x.pList ){
104372 heightOfExprList(p->x.pList, &nHeight);
104373 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
104375 p->nHeight = nHeight + 1;
104387 if( pParse->nErr ) return;
104389 sqlite3ExprCheckHeight(pParse, p->nHeight);
104407 if( pParse->nErr ) return;
104408 if( p && ExprUseXList(p) && p->x.pList ){
104409 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
104426 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
104430 ** can be translated into a 32-bit integer, then the token is not
104447 if( op!=TK_INTEGER || pToken->z==0
104448 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
104449 nExtra = pToken->n+1;
104456 pNew->op = (u8)op;
104457 pNew->iAgg = -1;
104460 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
104461 pNew->u.iValue = iValue;
104463 pNew->u.zToken = (char*)&pNew[1];
104464 assert( pToken->z!=0 || pToken->n==0 );
104465 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
104466 pNew->u.zToken[pToken->n] = 0;
104467 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
104473 pNew->nHeight = 1;
104480 ** Allocate a new expression node from a zero-terminated token that has
104507 assert( db->mallocFailed );
104512 pRoot->pRight = pRight;
104513 pRoot->flags |= EP_Propagate & pRight->flags;
104516 pRoot->pLeft = pLeft;
104517 pRoot->flags |= EP_Propagate & pLeft->flags;
104527 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
104537 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
104540 p->op = op & 0xff;
104541 p->iAgg = -1;
104542 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
104543 sqlite3ExprCheckHeight(pParse, p->nHeight);
104545 sqlite3ExprDelete(pParse->db, pLeft);
104546 sqlite3ExprDelete(pParse->db, pRight);
104557 pExpr->x.pSelect = pSelect;
104561 assert( pParse->db->mallocFailed );
104562 sqlite3SelectDelete(pParse->db, pSelect);
104589 for(ii=0; ii<pEList->nExpr; ii++){
104591 Expr *pExpr = pEList->a[ii].pExpr;
104593 if( pExpr->op==TK_VECTOR ){
104595 nExprElem = pExpr->x.pList->nExpr;
104600 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
104606 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
104607 pExpr->x.pList = 0;
104610 pSel->op = TK_ALL;
104611 pSel->pPrior = pRet;
104617 if( pRet && pRet->pPrior ){
104618 pRet->selFlags |= SF_MultiValue;
104620 sqlite3ExprListDelete(pParse->db, pEList);
104633 sqlite3 *db = pParse->db;
104660 sqlite3 *db = pParse->db;
104668 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
104670 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
104671 && !pParse->nested
104675 pNew->x.pList = pList;
104687 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
104689 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
104690 ** top-level SQL
104700 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
104702 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
104703 || (pParse->db->flags & SQLITE_TrustedSchema)==0
104734 sqlite3 *db = pParse->db;
104740 z = pExpr->u.zToken;
104747 x = (ynVar)(++pParse->nVar);
104755 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
104756 i = z[1]-'0'; /* The common case of ?N for a single digit N */
104759 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
104763 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
104764 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
104765 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
104767 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
104768 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104772 if( x>pParse->nVar ){
104773 pParse->nVar = (int)x;
104775 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
104783 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
104785 x = (ynVar)(++pParse->nVar);
104790 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
104793 pExpr->iColumn = x;
104794 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
104796 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104805 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
104807 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
104808 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
104811 assert( p->pLeft==0 );
104812 assert( p->pRight==0 );
104813 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
104814 assert( !ExprUseXList(p) || p->x.pList==0 );
104819 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
104820 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
104821 if( p->pRight ){
104823 sqlite3ExprDeleteNN(db, p->pRight);
104826 sqlite3SelectDelete(db, p->x.pSelect);
104828 sqlite3ExprListDelete(db, p->x.pList);
104831 sqlite3WindowDelete(db, p->y.pWin);
104838 sqlite3DbFree(db, p->u.zToken);
104854 }else if( p->pOn ){
104855 sqlite3ExprDeleteNN(db, p->pOn);
104856 }else if( p->pUsing ){
104857 sqlite3IdListDelete(db, p->pUsing);
104869 ** pExpr to the pParse->pConstExpr list with a register number of 0.
104872 pParse->pConstExpr =
104873 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
104884 sqlite3ExprDeleteNN(pParse->db, p);
104910 ** The dupedExprStructSize() function returns two values OR-ed together:
104923 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
104938 if( 0==flags || p->op==TK_SELECT_COLUMN
104949 if( p->pLeft || p->x.pList ){
104952 assert( p->pRight==0 );
104966 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
104967 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
104990 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
104999 ** to store the copy of expression p, the copies of p->u.zToken
105000 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
105029 ** by the copy of the p->u.zToken string (if any).
105034 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
105035 nToken = sqlite3Strlen30(p->u.zToken) + 1;
105046 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
105051 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
105052 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
105053 pNew->flags |= staticFlag;
105059 /* Copy the p->u.zToken string, if any. */
105061 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
105062 memcpy(zToken, p->u.zToken, nToken);
105065 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
105066 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
105068 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
105070 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
105074 /* Fill in pNew->pLeft and pNew->pRight. */
105078 pNew->pLeft = p->pLeft ?
105079 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
105080 pNew->pRight = p->pRight ?
105081 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
105085 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
105094 if( pNew->op==TK_SELECT_COLUMN ){
105095 pNew->pLeft = p->pLeft;
105096 assert( p->pRight==0 || p->pRight==p->pLeft
105097 || ExprHasProperty(p->pLeft, EP_Subquery) );
105099 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
105101 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
105111 ** and the db->mallocFailed flag set.
105117 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
105121 pRet->nCte = p->nCte;
105122 for(i=0; i<p->nCte; i++){
105123 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
105124 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
105125 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
105126 pRet->a[i].eM10d = p->a[i].eM10d;
105141 ** objects found there, assembling them onto the linked list at Select->pWin.
105144 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
105145 Select *pSelect = pWalker->u.pSelect;
105146 Window *pWin = pExpr->y.pWin;
105149 assert( pWin->ppThis==0 );
105155 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
105184 ** part of the in-memory representation of the database schema.
105201 pNew->nExpr = p->nExpr;
105202 pNew->nAlloc = p->nAlloc;
105203 pItem = pNew->a;
105204 pOldItem = p->a;
105205 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
105206 Expr *pOldExpr = pOldItem->pExpr;
105208 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
105210 && pOldExpr->op==TK_SELECT_COLUMN
105211 && (pNewExpr = pItem->pExpr)!=0
105213 if( pNewExpr->pRight ){
105214 pPriorSelectColOld = pOldExpr->pRight;
105215 pPriorSelectColNew = pNewExpr->pRight;
105216 pNewExpr->pLeft = pNewExpr->pRight;
105218 if( pOldExpr->pLeft!=pPriorSelectColOld ){
105219 pPriorSelectColOld = pOldExpr->pLeft;
105221 pNewExpr->pRight = pPriorSelectColNew;
105223 pNewExpr->pLeft = pPriorSelectColNew;
105226 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
105227 pItem->fg = pOldItem->fg;
105228 pItem->fg.done = 0;
105229 pItem->u = pOldItem->u;
105248 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
105251 pNew->nSrc = pNew->nAlloc = p->nSrc;
105252 for(i=0; i<p->nSrc; i++){
105253 SrcItem *pNewItem = &pNew->a[i];
105254 const SrcItem *pOldItem = &p->a[i];
105256 pNewItem->pSchema = pOldItem->pSchema;
105257 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
105258 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105259 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
105260 pNewItem->fg = pOldItem->fg;
105261 pNewItem->iCursor = pOldItem->iCursor;
105262 pNewItem->addrFillSub = pOldItem->addrFillSub;
105263 pNewItem->regReturn = pOldItem->regReturn;
105264 if( pNewItem->fg.isIndexedBy ){
105265 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
105267 pNewItem->u2 = pOldItem->u2;
105268 if( pNewItem->fg.isCte ){
105269 pNewItem->u2.pCteUse->nUse++;
105271 if( pNewItem->fg.isTabFunc ){
105272 pNewItem->u1.pFuncArg =
105273 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
105275 pTab = pNewItem->pTab = pOldItem->pTab;
105277 pTab->nTabRef++;
105279 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
105280 if( pOldItem->fg.isUsing ){
105281 assert( pNewItem->fg.isUsing );
105282 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
105284 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
105286 pNewItem->colUsed = pOldItem->colUsed;
105295 assert( p->eU4!=EU4_EXPR );
105296 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
105298 pNew->nId = p->nId;
105299 pNew->eU4 = p->eU4;
105300 for(i=0; i<p->nId; i++){
105301 struct IdList_item *pNewItem = &pNew->a[i];
105302 const struct IdList_item *pOldItem = &p->a[i];
105303 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105304 pNewItem->u4 = pOldItem->u4;
105315 for(p=pDup; p; p=p->pPrior){
105318 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
105319 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
105320 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
105321 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
105322 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
105323 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
105324 pNew->op = p->op;
105325 pNew->pNext = pNext;
105326 pNew->pPrior = 0;
105327 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
105328 pNew->iLimit = 0;
105329 pNew->iOffset = 0;
105330 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
105331 pNew->addrOpenEphm[0] = -1;
105332 pNew->addrOpenEphm[1] = -1;
105333 pNew->nSelectRow = p->nSelectRow;
105334 pNew->pWith = sqlite3WithDup(db, p->pWith);
105336 pNew->pWin = 0;
105337 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
105338 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
105340 pNew->selId = p->selId;
105341 if( db->mallocFailed ){
105345 pNew->pNext = 0;
105350 pp = &pNew->pPrior;
105371 ** Reason: This routine assumes that the number of slots in pList->a[]
105376 ** NULL is returned. If non-NULL is returned, then it is guaranteed
105387 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
105392 pList->nAlloc = 4;
105393 pList->nExpr = 1;
105394 pItem = &pList->a[0];
105396 pItem->pExpr = pExpr;
105406 pList->nAlloc *= 2;
105408 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
105416 pItem = &pList->a[pList->nExpr++];
105418 pItem->pExpr = pExpr;
105428 return sqlite3ExprListAppendNew(pParse->db,pExpr);
105430 if( pList->nAlloc<pList->nExpr+1 ){
105431 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
105433 pItem = &pList->a[pList->nExpr++];
105435 pItem->pExpr = pExpr;
105456 sqlite3 *db = pParse->db;
105459 int iFirst = pList ? pList->nExpr : 0;
105470 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
105472 pColumns->nId, n);
105476 for(i=0; i<pColumns->nId; i++){
105477 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
105478 assert( pSubExpr!=0 || db->mallocFailed );
105482 assert( pList->nExpr==iFirst+i+1 );
105483 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
105484 pColumns->a[i].zName = 0;
105488 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
105489 Expr *pFirst = pList->a[iFirst].pExpr;
105491 assert( pFirst->op==TK_SELECT_COLUMN );
105495 pFirst->pRight = pExpr;
105500 pFirst->iTable = pColumns->nId;
105515 assert( p->nExpr>0 );
105527 pItem = &p->a[p->nExpr-1];
105528 assert( pItem->fg.bNulls==0 );
105532 pItem->fg.sortFlags = (u8)iSortOrder;
105535 pItem->fg.bNulls = 1;
105537 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
105547 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105556 assert( pList!=0 || pParse->db->mallocFailed!=0 );
105557 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
105560 assert( pList->nExpr>0 );
105561 pItem = &pList->a[pList->nExpr-1];
105562 assert( pItem->zEName==0 );
105563 assert( pItem->fg.eEName==ENAME_NAME );
105564 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
105566 /* If dequote==0, then pName->z does not point to part of a DDL
105568 ** to the token-map. */
105569 sqlite3Dequote(pItem->zEName);
105571 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
105582 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105591 sqlite3 *db = pParse->db;
105592 assert( pList!=0 || db->mallocFailed!=0 );
105594 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
105595 assert( pList->nExpr>0 );
105596 if( pItem->zEName==0 ){
105597 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
105598 pItem->fg.eEName = ENAME_SPAN;
105612 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
105613 testcase( pEList && pEList->nExpr==mx );
105614 testcase( pEList && pEList->nExpr==mx+1 );
105615 if( pEList && pEList->nExpr>mx ){
105624 int i = pList->nExpr;
105625 struct ExprList_item *pItem = pList->a;
105626 assert( pList->nExpr>0 );
105628 sqlite3ExprDelete(db, pItem->pExpr);
105629 sqlite3DbFree(db, pItem->zEName);
105631 }while( --i>0 );
105639 ** Return the bitwise-OR of all Expr.flags fields in the given
105646 for(i=0; i<pList->nExpr; i++){
105647 Expr *pExpr = pList->a[i].pExpr;
105649 m |= pExpr->flags;
105655 ** This is a SELECT-node callback for the expression walker that
105657 ** pWalker->eCode to zero and abort.
105663 pWalker->eCode = 0;
105684 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
105689 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
105691 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
105693 pExpr->op = TK_TRUEFALSE;
105706 assert( pExpr->op==TK_TRUEFALSE );
105708 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
105709 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
105710 return pExpr->u.zToken[4]==0;
105728 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
105729 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
105730 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
105732 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
105734 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
105749 ** sqlite3ExprIsConstant() pWalker->eCode==1
105750 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
105751 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
105752 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
105769 /* If pWalker->eCode is 2 then any term of the expression that comes from
105772 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
105773 pWalker->eCode = 0;
105777 switch( pExpr->op ){
105779 ** and either pWalker->eCode==4 or 5 or the function has the
105782 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
105785 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
105788 pWalker->eCode = 0;
105801 testcase( pExpr->op==TK_ID );
105802 testcase( pExpr->op==TK_COLUMN );
105803 testcase( pExpr->op==TK_AGG_FUNCTION );
105804 testcase( pExpr->op==TK_AGG_COLUMN );
105805 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
105808 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
105815 testcase( pExpr->op==TK_REGISTER );
105816 testcase( pExpr->op==TK_IF_NULL_ROW );
105817 testcase( pExpr->op==TK_DOT );
105818 pWalker->eCode = 0;
105821 if( pWalker->eCode==5 ){
105825 pExpr->op = TK_NULL;
105826 }else if( pWalker->eCode==4 ){
105829 pWalker->eCode = 0;
105834 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
105835 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
105853 ** Walk an expression tree. Return non-zero if the expression is constant
105856 ** For the purposes of this function, a double-quoted string (ex: "abc")
105857 ** is considered a variable but a single-quoted string (ex: 'abc') is
105865 ** Walk an expression tree. Return non-zero if
105874 ** can be added to the pParse->pConstExpr list and evaluated once when
105882 ** Walk an expression tree. Return non-zero if the expression is constant
105884 ** expression must not refer to any non-deterministic function nor any
105899 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
105901 ** (2) pExpr cannot use subqueries or non-deterministic functions.
105915 if( pSrc->fg.jointype & JT_LTORJ ){
105918 if( pSrc->fg.jointype & JT_LEFT ){
105920 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
105924 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
105932 ExprList *pGroupBy = pWalker->u.pGroupBy;
105937 for(i=0; i<pGroupBy->nExpr; i++){
105938 Expr *p = pGroupBy->a[i].pExpr;
105939 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
105940 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
105947 /* Check if pExpr is a sub-select. If so, consider it variable. */
105949 pWalker->eCode = 0;
105957 ** Walk the expression tree passed as the first argument. Return non-zero
105965 ** assumes that no other collating sequence will have a finer-grained
105988 ** in a CREATE TABLE statement. Return non-zero if the expression is
105989 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
106003 ** For the purposes of this function, a double-quoted string (ex: "abc")
106004 ** is considered a variable but a single-quoted string (ex: 'abc') is
106032 ** to fit in a 32-bit integer, return 1 and put the value of the integer
106034 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
106040 /* If an expression is an integer literal that fits in a signed 32-bit
106042 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
106043 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
106045 if( p->flags & EP_IntValue ){
106046 *pValue = p->u.iValue;
106049 switch( p->op ){
106051 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
106056 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
106058 *pValue = -v;
106085 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
106086 p = p->pLeft;
106089 op = p->op;
106090 if( op==TK_REGISTER ) op = p->op2;
106100 p->y.pTab==0 || /* Reference to column of index on expression */
106101 (p->iColumn>=0
106102 && p->y.pTab->aCol!=0 /* Possible due to prior error */
106103 && p->y.pTab->aCol[p->iColumn].notNull==0);
106123 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
106124 if( p->op==TK_UMINUS ) unaryMinus = 1;
106125 p = p->pLeft;
106127 op = p->op;
106128 if( op==TK_REGISTER ) op = p->op2;
106143 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
106144 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
106153 ** Return TRUE if the given string is a row-id column name.
106178 p = pX->x.pSelect;
106179 if( p->pPrior ) return 0; /* Not a compound SELECT */
106180 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
106181 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
106182 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
106185 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
106186 if( p->pLimit ) return 0; /* Has no LIMIT clause */
106187 if( p->pWhere ) return 0; /* Has no WHERE clause */
106188 pSrc = p->pSrc;
106190 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
106191 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
106192 pTab = pSrc->a[0].pTab;
106196 pEList = p->pEList;
106199 for(i=0; i<pEList->nExpr; i++){
106200 Expr *pRes = pEList->a[i].pExpr;
106201 if( pRes->op!=TK_COLUMN ) return 0;
106202 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
106210 ** Generate code that checks the left-most column of index table iCur to see if
106212 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
106230 ** right-hand side. Return TRUE if that list is constant.
106236 pLHS = pIn->pLeft;
106237 pIn->pLeft = 0;
106239 pIn->pLeft = pLHS;
106249 ** The job of this routine is to find or create a b-tree object that can
106253 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
106256 ** The returned value of this function indicates the b-tree type, as follows:
106258 ** IN_INDEX_ROWID - The cursor was opened on a database table.
106259 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
106260 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
106261 ** IN_INDEX_EPH - The cursor was opened on a specially created and
106263 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
106266 ** An existing b-tree might be used if the RHS expression pX is a simple
106273 ** pX->iTable made to point to the ephemeral table instead of an
106276 ** will be set on pX and the pX->y.sub fields will be set to show where
106285 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
106286 ** through the set members) then the b-tree must not contain duplicates.
106288 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
106291 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
106294 ** index can be found with the specified <columns> as its left-most.
106298 ** routine might decide that creating an ephemeral b-tree for membership
106303 ** When the b-tree is being used for membership tests, the calling function
106312 ** the value in that register will be NULL if the b-tree contains one or more
106313 ** NULL values, and it will be some non-NULL value if the b-tree contains no
106342 assert( pX->op==TK_IN );
106344 iTab = pParse->nTab++;
106353 ExprList *pEList = pX->x.pSelect->pEList;
106354 for(i=0; i<pEList->nExpr; i++){
106355 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
106357 if( i==pEList->nExpr ){
106365 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
106366 sqlite3 *db = pParse->db; /* Database connection */
106369 ExprList *pEList = p->pEList;
106370 int nExpr = pEList->nExpr;
106372 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
106373 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
106374 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
106375 pTab = p->pSrc->a[0].pTab;
106378 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106381 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106384 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
106392 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
106404 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106405 int iCol = pEList->a[i].pExpr->iColumn;
106427 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
106430 if( pIdx->nColumn<nExpr ) continue;
106431 if( pIdx->pPartIdxWhere!=0 ) continue;
106432 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
106434 testcase( pIdx->nColumn==BMS-2 );
106435 testcase( pIdx->nColumn==BMS-1 );
106436 if( pIdx->nColumn>=BMS-1 ) continue;
106438 if( pIdx->nKeyCol>nExpr
106439 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
106447 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106448 Expr *pRhs = pEList->a[i].pExpr;
106452 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
106454 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
106455 assert( pIdx->azColl[j] );
106456 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
106468 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
106469 if( colUsed==(MASKBIT(nExpr)-1) ){
106473 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
106474 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
106476 VdbeComment((v, "%s", pIdx->zName));
106478 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
106482 i64 mask = (1<<nExpr)-1;
106486 *prRhsHasNull = ++pParse->nMem;
106508 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
106510 pParse->nTab--; /* Back out the allocation of the unused cursor */
106511 iTab = -1; /* Cursor is not allocated */
106516 /* Could not find an existing table or index to use as the RHS b-tree.
106519 u32 savedNQueryLoop = pParse->nQueryLoop;
106523 pParse->nQueryLoop = 0;
106525 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
106527 assert( pX->op==TK_IN );
106532 pParse->nQueryLoop = savedNQueryLoop;
106537 n = sqlite3ExprVectorSize(pX->pLeft);
106548 ** function allocates and returns a nul-terminated string containing
106555 Expr *pLeft = pExpr->pLeft;
106557 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
106560 assert( pExpr->op==TK_IN );
106561 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
106568 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
106584 ** "sub-select returns N columns - expected M"
106587 if( pParse->nErr==0 ){
106588 const char *zFmt = "sub-select returns %d columns - expected %d";
106596 ** it is not permitted. If pExpr is a sub-select vector, this routine
106599 ** "sub-select returns N columns - expected 1"
106608 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
106622 ** x IN (4,5,11) -- IN operator with list on right-hand side
106623 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
106627 ** table is computed, the cursor number is also stored in pExpr->iTable,
106651 v = pParse->pVdbe;
106657 ** * The right-hand side is a correlated subquery
106658 ** * The right-hand side is an expression list containing variables
106664 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
106673 pExpr->x.pSelect->selId));
106676 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
106677 pExpr->y.sub.iAddr);
106678 assert( iTab!=pExpr->iTable );
106679 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
106688 pExpr->y.sub.regReturn = ++pParse->nMem;
106689 pExpr->y.sub.iAddr =
106690 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
106696 pLeft = pExpr->pLeft;
106702 pExpr->iTable = iTab;
106703 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
106706 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
106711 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
106719 Select *pSelect = pExpr->x.pSelect;
106720 ExprList *pEList = pSelect->pEList;
106723 addrOnce?"":"CORRELATED ", pSelect->selId
106727 if( ALWAYS(pEList->nExpr==nVal) ){
106734 pSelect->iLimit = 0;
106735 testcase( pSelect->selFlags & SF_Distinct );
106737 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
106738 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
106739 sqlite3SelectDelete(pParse->db, pCopy);
106740 sqlite3DbFree(pParse->db, dest.zAffSdst);
106747 assert( pEList->nExpr>0 );
106751 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
106752 pParse, p, pEList->a[i].pExpr
106756 }else if( ALWAYS(pExpr->x.pList!=0) ){
106766 ExprList *pList = pExpr->x.pList;
106777 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
106783 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
106784 Expr *pE2 = pItem->pExpr;
106788 ** this code only executes once. Because for a non-constant
106792 sqlite3VdbeChangeToNoop(v, addrOnce-1);
106813 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
106814 || pParse->nErr );
106815 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
106816 pExpr->y.sub.iAddr, 1);
106827 ** (SELECT a FROM b) -- subquery
106828 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
106832 ** Return the register that holds the result. For a multi-column SELECT,
106834 ** return value is the register of the left-most result column.
106846 Vdbe *v = pParse->pVdbe;
106848 if( pParse->nErr ) return 0;
106849 testcase( pExpr->op==TK_EXISTS );
106850 testcase( pExpr->op==TK_SELECT );
106851 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
106853 pSel = pExpr->x.pSelect;
106858 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
106860 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
106861 pExpr->y.sub.iAddr);
106862 return pExpr->iTable;
106869 pExpr->y.sub.regReturn = ++pParse->nMem;
106870 pExpr->y.sub.iAddr =
106871 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
106876 ** * The right-hand side is a correlated subquery
106877 ** * The right-hand side is an expression list containing variables
106898 addrOnce?"":"CORRELATED ", pSel->selId));
106899 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
106900 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
106901 pParse->nMem += nReg;
106902 if( pExpr->op==TK_SELECT ){
106906 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
106913 if( pSel->pLimit ){
106914 /* The subquery already has a limit. If the pre-existing limit is X
106916 sqlite3 *db = pParse->db;
106919 pLimit->affExpr = SQLITE_AFF_NUMERIC;
106921 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
106923 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
106924 pSel->pLimit->pLeft = pLimit;
106926 /* If there is no pre-existing limit add a limit of 1 */
106927 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
106928 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
106930 pSel->iLimit = 0;
106932 pExpr->op2 = pExpr->op;
106933 pExpr->op = TK_ERROR;
106936 pExpr->iTable = rReg = dest.iSDParm;
106944 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
106945 || pParse->nErr );
106946 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
106947 pExpr->y.sub.iAddr, 1);
106957 ** sub-select on the RHS of the IN() operator has the same number of
106959 ** a sub-query, that the LHS is a vector of size 1.
106962 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
106963 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
106964 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
106965 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
106969 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
106983 ** The left-hand side (LHS) is a scalar or vector expression. The
106984 ** right-hand side (RHS) is an array of zero or more scalar values, or a
106999 ** See the separate in-operator.md documentation file in the canonical
107023 int addrTop; /* Top of the step-6 loop */
107025 u8 okConstFactor = pParse->okConstFactor;
107028 pLeft = pExpr->pLeft;
107031 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
107033 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
107035 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
107041 v = pParse->pVdbe;
107049 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
107054 ** nVector-1. */
107074 assert( pParse->okConstFactor==okConstFactor );
107075 pParse->okConstFactor = 0;
107077 pParse->okConstFactor = okConstFactor;
107094 ** This is step (1) in the in-operator.md optimized algorithm.
107104 pList = pExpr->x.pList;
107105 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
107110 for(ii=0; ii<pList->nExpr; ii++){
107111 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
107112 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
107116 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
107120 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
107121 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
107122 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
107123 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
107154 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
107155 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
107162 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
107167 /* In this case, the RHS is the ROWID of table b-tree and so we also
107168 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
107186 /* Step 4. If the RHS is known to be non-NULL and we did not find
107246 sqlite3DbFree(pParse->db, aiMap);
107247 sqlite3DbFree(pParse->db, zAff);
107254 ** value described by z[0..n-1] into register iMem.
107256 ** The z[] string will probably not be zero-terminated. But the
107265 if( negateFlag ) value = -value;
107274 ** text z[0..n-1] into register iMem.
107276 ** Expr.u.zToken is always UTF8 and zero-terminated.
107279 Vdbe *v = pParse->pVdbe;
107280 if( pExpr->flags & EP_IntValue ){
107281 int i = pExpr->u.iValue;
107283 if( negFlag ) i = -i;
107288 const char *z = pExpr->u.zToken;
107293 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
107298 negFlag?"-":"",pExpr);
107306 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
107314 ** appropriate for the iIdxCol-th column of index pIdx.
107323 i16 iTabCol = pIdx->aiColumn[iIdxCol];
107325 assert( pIdx->aColExpr );
107326 assert( pIdx->aColExpr->nExpr>iIdxCol );
107327 pParse->iSelfTab = iTabCur + 1;
107328 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
107329 pParse->iSelfTab = 0;
107331 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
107348 Vdbe *v = pParse->pVdbe;
107350 assert( pParse->iSelfTab!=0 );
107351 if( pParse->iSelfTab>0 ){
107352 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
107357 if( pCol->affinity>=SQLITE_AFF_TEXT ){
107358 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
107365 ** Generate code to extract the value of the iCol-th column of a table.
107380 if( iCol<0 || iCol==pTab->iPKey ){
107382 VdbeComment((v, "%s.rowid", pTab->zName));
107390 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
107392 if( pCol->colFlags & COLFLAG_BUSY ){
107394 pCol->zCnName);
107396 int savedSelfTab = pParse->iSelfTab;
107397 pCol->colFlags |= COLFLAG_BUSY;
107398 pParse->iSelfTab = iTabCur+1;
107400 pParse->iSelfTab = savedSelfTab;
107401 pCol->colFlags &= ~COLFLAG_BUSY;
107420 ** Generate code that will extract the iColumn-th column from
107434 assert( pParse->pVdbe!=0 );
107435 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107437 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
107438 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
107444 ** Generate code to move content from registers iFrom...iFrom+nReg-1
107445 ** over to iTo..iTo+nReg-1.
107448 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
107459 p->op2 = p->op;
107460 p->op = TK_REGISTER;
107461 p->iTable = iReg;
107482 if( p->op==TK_SELECT ){
107490 iResult = pParse->nMem+1;
107491 pParse->nMem += nResult;
107494 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
107502 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107506 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
107513 ** in-line rather than by using the usual callbacks.
107522 Vdbe *v = pParse->pVdbe;
107525 nFarg = pFarg->nExpr;
107526 assert( nFarg>0 ); /* All in-line functions have at least one argument */
107529 /* Attempt a direct implementation of the built-in COALESCE() and
107531 ** arguments past the first non-NULL argument.
107536 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
107540 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
107555 Expr *pArg = pFarg->a[0].pExpr;
107556 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
107557 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
107565 /* The UNLIKELY() function is a no-op. The result is the value
107569 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
107574 ** Test-only SQL functions that are only usable if enabled
107582 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107591 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107600 pA1 = pFarg->a[1].pExpr;
107601 if( pA1->op==TK_COLUMN ){
107603 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
107619 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
107621 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
107642 Vdbe *v = pParse->pVdbe; /* The VM under construction */
107645 int regFree1 = 0; /* If non-zero free this temporary register */
107646 int regFree2 = 0; /* If non-zero free this temporary register */
107651 assert( target>0 && target<=pParse->nMem );
107659 op = pExpr->op;
107663 AggInfo *pAggInfo = pExpr->pAggInfo;
107666 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
107667 pCol = &pAggInfo->aCol[pExpr->iAgg];
107668 if( !pAggInfo->directMode ){
107669 assert( pCol->iMem>0 );
107670 return pCol->iMem;
107671 }else if( pAggInfo->useSortingIdx ){
107672 Table *pTab = pCol->pTab;
107673 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
107674 pCol->iSorterColumn, target);
107675 if( pCol->iColumn<0 ){
107676 VdbeComment((v,"%s.rowid",pTab->zName));
107679 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107680 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
107690 int iTab = pExpr->iTable;
107694 ** constraints, and that constant is coded by the pExpr->pLeft
107700 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107702 if( pExpr->y.pTab ){
107703 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107705 aff = pExpr->affExpr;
107712 &zAff[(aff-'B')*2], P4_STATIC);
107717 if( pParse->iSelfTab<0 ){
107721 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
107727 int iCol = pExpr->iColumn;
107729 pTab = pExpr->y.pTab;
107732 assert( iCol<pTab->nCol );
107734 return -1-pParse->iSelfTab;
107736 pCol = pTab->aCol + iCol;
107738 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
107740 if( pCol->colFlags & COLFLAG_GENERATED ){
107741 if( pCol->colFlags & COLFLAG_BUSY ){
107743 pCol->zCnName);
107746 pCol->colFlags |= COLFLAG_BUSY;
107747 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
107750 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
107754 if( pCol->affinity==SQLITE_AFF_REAL ){
107764 iTab = pParse->iSelfTab - 1;
107768 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
107769 pExpr->iColumn, iTab, target,
107770 pExpr->op2);
107771 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
107787 codeReal(v, pExpr->u.zToken, 0, target);
107793 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
107801 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
107811 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
107812 assert( pExpr->u.zToken[1]=='\'' );
107813 z = &pExpr->u.zToken[2];
107814 n = sqlite3Strlen30(z) - 1;
107823 assert( pExpr->u.zToken!=0 );
107824 assert( pExpr->u.zToken[0]!=0 );
107825 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
107826 if( pExpr->u.zToken[1]!=0 ){
107827 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
107828 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
107829 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
107835 return pExpr->iTable;
107840 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
107847 sqlite3AffinityType(pExpr->u.zToken, 0));
107855 /* fall-through */
107862 Expr *pLeft = pExpr->pLeft;
107867 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
107869 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
107911 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107912 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
107919 Expr *pLeft = pExpr->pLeft;
107921 if( pLeft->op==TK_INTEGER ){
107925 }else if( pLeft->op==TK_FLOAT ){
107927 codeReal(v, pLeft->u.zToken, 1, target);
107936 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
107946 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107954 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107956 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
107957 bNormal = pExpr->op2==TK_IS;
107969 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107979 AggInfo *pInfo = pExpr->pAggInfo;
107981 || NEVER(pExpr->iAgg<0)
107982 || NEVER(pExpr->iAgg>=pInfo->nFunc)
107987 return pInfo->aFunc[pExpr->iAgg].iMem;
107998 sqlite3 *db = pParse->db; /* The database connection */
108004 return pExpr->y.pWin->regResult;
108011 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108015 pFarg = pExpr->x.pList;
108016 nFarg = pFarg ? pFarg->nExpr : 0;
108018 zId = pExpr->u.zToken;
108021 if( pDef==0 && pParse->explain ){
108025 if( pDef==0 || pDef->xFinalize!=0 ){
108029 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
108030 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
108031 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
108033 SQLITE_PTR_TO_INT(pDef->pUserData), target);
108034 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
108039 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
108043 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
108044 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
108049 r1 = pParse->nMem+1;
108050 pParse->nMem += nFarg;
108060 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
108063 assert( pFarg->a[0].pExpr!=0 );
108064 exprOp = pFarg->a[0].pExpr->op;
108068 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
108069 pFarg->a[0].pExpr->op2 =
108070 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
108093 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
108095 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
108098 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
108099 if( !pColl ) pColl = db->pDfltColl;
108103 pDef, pExpr->op2);
108119 if( pParse->db->mallocFailed ){
108123 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
108133 Expr *pLeft = pExpr->pLeft;
108134 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
108135 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
108136 pLeft->op2 = pParse->withinRJSubrtn;
108138 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
108140 if( pExpr->iTable!=n ){
108142 pExpr->iTable, n);
108144 return pLeft->iTable + pExpr->iColumn;
108167 ** X is stored in pExpr->pLeft.
108168 ** Y is stored in pExpr->pList->a[0].pExpr.
108169 ** Z is stored in pExpr->pList->a[1].pExpr.
108177 && ALWAYS(pExpr->pLeft)
108178 && pExpr->pLeft->op==TK_FUNCTION
108180 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108188 pExpr = pExpr->pLeft;
108189 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
108194 pExpr = pExpr->pLeft;
108195 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
108200 ** to a column in the new.* or old.* pseudo-tables available to
108202 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
108203 ** is set to the column of the pseudo-table to read, or to -1 to
108208 ** to reference another column of the old.* pseudo-table, where
108210 ** set to (n+1), where n is the number of columns in each pseudo-table.
108211 ** For a reference to any other column in the new.* pseudo-table, p1
108220 ** p1==0 -> old.rowid p1==3 -> new.rowid
108221 ** p1==1 -> old.a p1==4 -> new.a
108222 ** p1==2 -> old.b p1==5 -> new.b
108229 pTab = pExpr->y.pTab;
108230 iCol = pExpr->iColumn;
108231 p1 = pExpr->iTable * (pTab->nCol+1) + 1
108234 assert( pExpr->iTable==0 || pExpr->iTable==1 );
108235 assert( iCol>=-1 && iCol<pTab->nCol );
108236 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
108237 assert( p1>=0 && p1<(pTab->nCol*2+2) );
108241 (pExpr->iTable ? "new" : "old"),
108242 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
108249 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
108251 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
108264 ** that derive from the right-hand table of a LEFT JOIN. The
108265 ** Expr.iTable value is the table number for the right-hand table.
108271 u8 okConstFactor = pParse->okConstFactor;
108272 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108275 ** really constant because they originate from the right-hand side
108277 pParse->okConstFactor = 0;
108278 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108279 pParse->okConstFactor = okConstFactor;
108296 ** X (if it exists) is in pExpr->pLeft.
108297 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
108300 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
108307 int endLabel; /* GOTO label for end of CASE stmt */
108308 int nextCase; /* GOTO label for next WHEN clause */
108317 sqlite3 *db = pParse->db;
108319 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
108320 assert(pExpr->x.pList->nExpr > 0);
108321 pEList = pExpr->x.pList;
108322 aListelem = pEList->a;
108323 nExpr = pEList->nExpr;
108325 if( (pX = pExpr->pLeft)!=0 ){
108327 if( db->mallocFailed ){
108331 testcase( pX->op==TK_COLUMN );
108339 ** The value in regFree1 might get SCopy-ed into the file result.
108344 for(i=0; i<nExpr-1; i=i+2){
108352 testcase( pTest->op==TK_COLUMN );
108354 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
108360 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
108371 assert( pExpr->affExpr==OE_Rollback
108372 || pExpr->affExpr==OE_Abort
108373 || pExpr->affExpr==OE_Fail
108374 || pExpr->affExpr==OE_Ignore
108376 if( !pParse->pTriggerTab && !pParse->nested ){
108378 "RAISE() may only be used within a trigger-program");
108381 if( pExpr->affExpr==OE_Abort ){
108385 if( pExpr->affExpr==OE_Ignore ){
108387 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
108391 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
108392 pExpr->affExpr, pExpr->u.zToken, 0, 0);
108428 p = pParse->pConstExpr;
108432 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
108433 if( pItem->fg.reusable
108434 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
108436 return pItem->u.iConstExprReg;
108440 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
108442 Vdbe *v = pParse->pVdbe;
108446 pParse->okConstFactor = 0;
108447 if( !pParse->db->mallocFailed ){
108448 if( regDest<0 ) regDest = ++pParse->nMem;
108451 pParse->okConstFactor = 1;
108452 sqlite3ExprDelete(pParse->db, pExpr);
108457 struct ExprList_item *pItem = &p->a[p->nExpr-1];
108458 pItem->fg.reusable = regDest<0;
108459 if( regDest<0 ) regDest = ++pParse->nMem;
108460 pItem->u.iConstExprReg = regDest;
108462 pParse->pConstExpr = p;
108485 && pExpr->op!=TK_REGISTER
108489 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108512 assert( target>0 && target<=pParse->nMem );
108513 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
108514 if( pParse->pVdbe==0 ) return;
108523 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
108533 sqlite3 *db = pParse->db;
108535 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
108546 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
108558 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
108583 Vdbe *v = pParse->pVdbe;
108586 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
108587 n = pList->nExpr;
108589 for(pItem=pList->a, i=0; i<n; i++, pItem++){
108590 Expr *pExpr = pItem->pExpr;
108592 if( pItem->fg.bSorterRef ){
108593 i--;
108594 n--;
108597 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
108599 i--;
108600 n--;
108602 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
108613 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
108614 && pOp->p1+pOp->p3+1==inReg
108615 && pOp->p2+pOp->p3+1==target+i
108616 && pOp->p5==0 /* The do-not-merge flag must be clear */
108618 pOp->p3++;
108660 sqlite3 *db = pParse->db;
108667 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
108668 if( db->mallocFailed==0 ){
108674 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
108677 compRight.pRight = pExpr->x.pList->a[1].pExpr;
108687 pDel->flags |= EP_OuterON;
108708 ** to the label "dest" if the expression is true but execution
108721 Vdbe *v = pParse->pVdbe;
108731 op = pExpr->op;
108741 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
108743 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108747 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108748 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108754 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108761 isNot = pExpr->op2==TK_ISNOT;
108762 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108766 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108769 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108787 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108789 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108790 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
108791 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108811 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108838 /* No-op */
108855 ** to the label "dest" if the expression is false but execution
108863 Vdbe *v = pParse->pVdbe;
108874 /* The value of pExpr->op and op are related as follows:
108876 ** pExpr->op op
108877 ** --------- ----------
108887 ** For other values of pExpr->op, op is undefined and unused.
108892 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
108896 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
108897 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
108898 assert( pExpr->op!=TK_NE || op==OP_Eq );
108899 assert( pExpr->op!=TK_EQ || op==OP_Ne );
108900 assert( pExpr->op!=TK_LT || op==OP_Ge );
108901 assert( pExpr->op!=TK_LE || op==OP_Gt );
108902 assert( pExpr->op!=TK_GT || op==OP_Le );
108903 assert( pExpr->op!=TK_GE || op==OP_Lt );
108905 switch( pExpr->op ){
108911 }else if( pExpr->op==TK_AND ){
108913 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108914 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108918 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
108920 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108927 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108934 isNot = pExpr->op2==TK_ISNOT;
108935 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108940 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108945 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108952 testcase( pExpr->op==TK_IS );
108953 testcase( pExpr->op==TK_ISNOT );
108954 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
108963 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108965 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108966 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
108967 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108985 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
109014 /* no-op */
109035 sqlite3 *db = pParse->db;
109037 if( db->mallocFailed==0 ){
109047 ** If pExpr is a simple SQL value - an integer, real, string, blob
109048 ** or NULL value - then the VDBE currently being prepared is configured
109049 ** to re-prepare each time a new value is bound to variable pVar.
109052 ** same as that currently bound to variable pVar, non-zero is returned.
109063 sqlite3_value *pL, *pR = 0;
109065 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
109066 if( pR ){
109067 iVar = pVar->iColumn;
109068 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
109069 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
109072 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
109074 res = 0==sqlite3MemCompare(pL, pR, 0);
109076 sqlite3ValueFree(pR);
109087 ** other than the top-level COLLATE operator.
109089 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109101 ** this routine is used, it does not hurt to get an extra 2 - that
109106 ** pParse->pReprepare can be matched against literals in pB. The
109107 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
109122 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
109125 combinedFlags = pA->flags | pB->flags;
109127 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
109132 if( pA->op!=pB->op || pA->op==TK_RAISE ){
109133 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
109136 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109143 if( pA->u.zToken ){
109144 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
109145 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109147 assert( pA->op==pB->op );
109152 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
109157 }else if( pA->op==TK_NULL ){
109159 }else if( pA->op==TK_COLLATE ){
109160 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109162 if( pB->u.zToken!=0
109163 && pA->op!=TK_COLUMN
109164 && pA->op!=TK_AGG_COLUMN
109165 && strcmp(pA->u.zToken,pB->u.zToken)!=0
109170 if( (pA->flags & (EP_Distinct|EP_Commuted))
109171 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
109175 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
109176 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
109177 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
109178 if( pA->op!=TK_STRING
109179 && pA->op!=TK_TRUEFALSE
109182 if( pA->iColumn!=pB->iColumn ) return 2;
109183 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
109184 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
109197 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109200 ** This routine might return non-zero for equivalent ExprLists. The
109206 ** always differs from a non-NULL pointer.
109212 if( pA->nExpr!=pB->nExpr ) return 1;
109213 for(i=0; i<pA->nExpr; i++){
109215 Expr *pExprA = pA->a[i].pExpr;
109216 Expr *pExprB = pB->a[i].pExpr;
109217 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
109224 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
109235 ** Return non-zero if Expr p can only be true if pNN is not NULL.
109237 ** Or if seenNot is true, return non-zero if Expr p can only be
109238 ** non-NULL if pNN is not NULL
109245 int seenNot /* Return true only if p can be any non-NULL value */
109250 return pNN->op!=TK_NULL;
109252 switch( p->op ){
109255 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
109256 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109261 pList = p->x.pList;
109263 assert( pList->nExpr==2 );
109265 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
109266 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
109270 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109290 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
109297 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
109301 if( p->op2!=TK_IS ) return 0;
109302 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109306 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109329 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
109346 if( pE2->op==TK_OR
109347 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
109348 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
109352 if( pE2->op==TK_NOTNULL
109353 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
109362 ** If the expression node requires that the table at pWalker->iCur
109363 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
109366 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
109367 ** (never setting pWalker->eCode) is a harmless missed optimization.
109370 testcase( pExpr->op==TK_AGG_COLUMN );
109371 testcase( pExpr->op==TK_AGG_FUNCTION );
109373 switch( pExpr->op ){
109384 testcase( pExpr->op==TK_ISNOT );
109385 testcase( pExpr->op==TK_ISNULL );
109386 testcase( pExpr->op==TK_NOTNULL );
109387 testcase( pExpr->op==TK_IS );
109388 testcase( pExpr->op==TK_OR );
109389 testcase( pExpr->op==TK_VECTOR );
109390 testcase( pExpr->op==TK_CASE );
109391 testcase( pExpr->op==TK_IN );
109392 testcase( pExpr->op==TK_FUNCTION );
109393 testcase( pExpr->op==TK_TRUTH );
109396 if( pWalker->u.iCur==pExpr->iTable ){
109397 pWalker->eCode = 1;
109403 if( pWalker->eCode==0 ){
109404 sqlite3WalkExpr(pWalker, pExpr->pLeft);
109405 if( pWalker->eCode ){
109406 pWalker->eCode = 0;
109407 sqlite3WalkExpr(pWalker, pExpr->pRight);
109413 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
109414 assert( pWalker->eCode );
109428 Expr *pLeft = pExpr->pLeft;
109429 Expr *pRight = pExpr->pRight;
109430 testcase( pExpr->op==TK_EQ );
109431 testcase( pExpr->op==TK_NE );
109432 testcase( pExpr->op==TK_LT );
109433 testcase( pExpr->op==TK_LE );
109434 testcase( pExpr->op==TK_GT );
109435 testcase( pExpr->op==TK_GE );
109438 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
109439 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
109440 if( (pLeft->op==TK_COLUMN
109441 && pLeft->y.pTab!=0
109442 && IsVirtual(pLeft->y.pTab))
109443 || (pRight->op==TK_COLUMN
109444 && pRight->y.pTab!=0
109445 && IsVirtual(pRight->y.pTab))
109457 ** Return true (non-zero) if expression p can only be true if at least
109458 ** one column of table iTab is non-null. In other words, return true
109475 ** be non-NULL, then the LEFT JOIN can be safely converted into an
109482 if( p->op==TK_NOTNULL ){
109483 p = p->pLeft;
109485 while( p->op==TK_AND ){
109486 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
109487 p = p->pRight;
109513 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
109514 ** pWalker->u.pIdxCover->pIdx.
109517 if( pExpr->op==TK_COLUMN
109518 && pExpr->iTable==pWalker->u.pIdxCover->iCur
109519 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
109521 pWalker->eCode = 1;
109573 struct RefSrcList *p = pWalker->u.pRefSrcList;
109574 SrcList *pSrc = pSelect->pSrc;
109577 if( pSrc->nSrc==0 ) return WRC_Continue;
109578 j = p->nExclude;
109579 p->nExclude += pSrc->nSrc;
109580 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
109582 p->nExclude = 0;
109585 p->aiExclude = piNew;
109587 for(i=0; i<pSrc->nSrc; i++, j++){
109588 p->aiExclude[j] = pSrc->a[i].iCursor;
109593 struct RefSrcList *p = pWalker->u.pRefSrcList;
109594 SrcList *pSrc = pSelect->pSrc;
109595 if( p->nExclude ){
109596 assert( p->nExclude>=pSrc->nSrc );
109597 p->nExclude -= pSrc->nSrc;
109603 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
109606 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
109610 if( pExpr->op==TK_COLUMN
109611 || pExpr->op==TK_AGG_COLUMN
109614 struct RefSrcList *p = pWalker->u.pRefSrcList;
109615 SrcList *pSrc = p->pRef;
109616 int nSrc = pSrc ? pSrc->nSrc : 0;
109618 if( pExpr->iTable==pSrc->a[i].iCursor ){
109619 pWalker->eCode |= 1;
109623 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
109624 if( i>=p->nExclude ){
109625 pWalker->eCode |= 2;
109640 ** -1 pExpr only references no tables at all, or it only
109655 x.db = pParse->db;
109657 assert( pExpr->op==TK_AGG_FUNCTION );
109659 sqlite3WalkExprList(&w, pExpr->x.pList);
109662 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109665 sqlite3DbFree(pParse->db, x.aiExclude);
109671 return -1;
109683 ** The copy is stored on pParse->pConstExpr with a register number of 0.
109690 && pExpr->pAggInfo!=0
109692 AggInfo *pAggInfo = pExpr->pAggInfo;
109693 int iAgg = pExpr->iAgg;
109694 Parse *pParse = pWalker->pParse;
109695 sqlite3 *db = pParse->db;
109696 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
109697 if( pExpr->op==TK_AGG_COLUMN ){
109698 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
109699 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109702 pAggInfo->aCol[iAgg].pCExpr = pExpr;
109707 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
109708 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109711 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
109726 pWalker->pParse = pParse;
109727 pWalker->xExprCallback = agginfoPersistExprCb;
109728 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
109732 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
109737 pInfo->aCol = sqlite3ArrayAllocate(
109739 pInfo->aCol,
109740 sizeof(pInfo->aCol[0]),
109741 &pInfo->nColumn,
109748 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
109753 pInfo->aFunc = sqlite3ArrayAllocate(
109755 pInfo->aFunc,
109756 sizeof(pInfo->aFunc[0]),
109757 &pInfo->nFunc,
109770 NameContext *pNC = pWalker->u.pNC;
109771 Parse *pParse = pNC->pParse;
109772 SrcList *pSrcList = pNC->pSrcList;
109773 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
109775 assert( pNC->ncFlags & NC_UAggInfo );
109776 switch( pExpr->op ){
109779 testcase( pExpr->op==TK_AGG_COLUMN );
109780 testcase( pExpr->op==TK_COLUMN );
109784 SrcItem *pItem = pSrcList->a;
109785 for(i=0; i<pSrcList->nSrc; i++, pItem++){
109788 if( pExpr->iTable==pItem->iCursor ){
109792 ** Make an entry for the column in pAggInfo->aCol[] if there
109796 pCol = pAggInfo->aCol;
109797 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
109798 if( pCol->iTable==pExpr->iTable &&
109799 pCol->iColumn==pExpr->iColumn ){
109803 if( (k>=pAggInfo->nColumn)
109804 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
109806 pCol = &pAggInfo->aCol[k];
109808 pCol->pTab = pExpr->y.pTab;
109809 pCol->iTable = pExpr->iTable;
109810 pCol->iColumn = pExpr->iColumn;
109811 pCol->iMem = ++pParse->nMem;
109812 pCol->iSorterColumn = -1;
109813 pCol->pCExpr = pExpr;
109814 if( pAggInfo->pGroupBy ){
109816 ExprList *pGB = pAggInfo->pGroupBy;
109817 struct ExprList_item *pTerm = pGB->a;
109818 n = pGB->nExpr;
109820 Expr *pE = pTerm->pExpr;
109821 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
109822 pE->iColumn==pExpr->iColumn ){
109823 pCol->iSorterColumn = j;
109828 if( pCol->iSorterColumn<0 ){
109829 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
109832 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
109835 ** pAggInfo->aCol[] entry.
109838 pExpr->pAggInfo = pAggInfo;
109839 pExpr->op = TK_AGG_COLUMN;
109840 pExpr->iAgg = (i16)k;
109842 } /* endif pExpr->iTable==pItem->iCursor */
109848 if( (pNC->ncFlags & NC_InAggFunc)==0
109849 && pWalker->walkerDepth==pExpr->op2
109854 struct AggInfo_func *pItem = pAggInfo->aFunc;
109855 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
109856 if( pItem->pFExpr==pExpr ) break;
109857 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
109861 if( i>=pAggInfo->nFunc ){
109862 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
109864 u8 enc = ENC(pParse->db);
109865 i = addAggInfoFunc(pParse->db, pAggInfo);
109868 pItem = &pAggInfo->aFunc[i];
109869 pItem->pFExpr = pExpr;
109870 pItem->iMem = ++pParse->nMem;
109872 pItem->pFunc = sqlite3FindFunction(pParse->db,
109873 pExpr->u.zToken,
109874 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
109875 if( pExpr->flags & EP_Distinct ){
109876 pItem->iDistinct = pParse->nTab++;
109878 pItem->iDistinct = -1;
109882 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
109886 pExpr->iAgg = (i16)i;
109887 pExpr->pAggInfo = pAggInfo;
109899 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
109914 assert( pNC->pSrcList!=0 );
109928 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
109929 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
109938 if( pParse->nTempReg==0 ){
109939 return ++pParse->nMem;
109941 return pParse->aTempReg[--pParse->nTempReg];
109951 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
109952 pParse->aTempReg[pParse->nTempReg++] = iReg;
109963 i = pParse->iRangeReg;
109964 n = pParse->nRangeReg;
109966 pParse->iRangeReg += nReg;
109967 pParse->nRangeReg -= nReg;
109969 i = pParse->nMem+1;
109970 pParse->nMem += nReg;
109980 if( nReg>pParse->nRangeReg ){
109981 pParse->nRangeReg = nReg;
109982 pParse->iRangeReg = iReg;
109989 ** Always invoke this procedure after coding a subroutine or co-routine
109991 ** the sub/co-routine does not use registers in common with the code that
109992 ** invokes the sub/co-routine.
109995 pParse->nTempReg = 0;
109996 pParse->nRangeReg = 0;
110007 if( pParse->nRangeReg>0
110008 && pParse->iRangeReg+pParse->nRangeReg > iFirst
110009 && pParse->iRangeReg <= iLast
110013 for(i=0; i<pParse->nTempReg; i++){
110014 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
110050 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
110055 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
110057 || (pTab->tabFlags & TF_Eponymous)!=0
110058 || ( (pTab->tabFlags & TF_Shadow)!=0
110059 && sqlite3ReadOnlyShadowTables(pParse->db)
110063 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
110083 pParse->colNamesSet = 1;
110107 ** Generate VM code to replace any double-quoted strings (but not double-quoted
110109 ** database zDb with their single-quoted equivalents. If argument bTemp is
110135 Vdbe *v = pParse->pVdbe;
110138 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
110139 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
110155 char *zName = 0; /* NULL-terminated version of pName */
110156 sqlite3 *db = pParse->db; /* Database connection */
110157 int nTabName; /* Number of UTF-8 characters in zTabName */
110160 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
110162 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
110163 assert( pSrc->nSrc==1 );
110164 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
110166 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110168 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
110169 zDb = db->aDb[iDb].zDbSName;
110199 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
110206 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110217 if( pVTab->pVtab->pModule->xRename==0 ){
110233 /* figure out how many UTF-8 characters are in zName */
110234 zTabName = pTab->zName;
110235 nTabName = sqlite3Utf8CharLen(zTabName, -1);
110272 zDb, zName, pTab->zName);
110293 ** of any resources used by the v-table implementation (including other
110298 int i = ++pParse->nMem;
110333 ** The Table structure pParse->pNewTable was extended to include
110337 Table *pNew; /* Copy of pParse->pNewTable */
110342 char *zCol; /* Null-terminated column definition */
110349 db = pParse->db;
110350 assert( db->pParse==pParse );
110351 if( pParse->nErr ) return;
110352 assert( db->mallocFailed==0 );
110353 pNew = pParse->pNewTable;
110357 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
110358 zDb = db->aDb[iDb].zDbSName;
110359 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
110360 pCol = &pNew->aCol[pNew->nCol-1];
110367 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110377 if( pCol->colFlags & COLFLAG_PRIMKEY ){
110381 if( pNew->pIndex ){
110386 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
110391 assert( pDflt==0 || pDflt->op==TK_SPAN );
110392 if( pDflt && pDflt->pLeft->op==TK_NULL ){
110396 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
110398 "Cannot add a REFERENCES column with non-NULL default value");
110400 if( pCol->notNull && !pDflt ){
110415 assert( db->mallocFailed == 1 );
110420 "Cannot add a column with non-constant default");
110424 }else if( pCol->colFlags & COLFLAG_STORED ){
110430 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
110432 char *zEnd = &zCol[pColDef->n-1];
110434 *zEnd-- = '\0';
110445 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
110460 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
110470 if( pNew->pCheck!=0
110471 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
110487 ** This function is called by the parser after the table-name in
110488 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
110489 ** pSrc is the full-name of the table being altered.
110507 sqlite3 *db = pParse->db;
110510 assert( pParse->pNewTable==0 );
110512 if( db->mallocFailed ) goto exit_begin_add_column;
110513 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110534 assert( pTab->u.tab.addColOffset>0 );
110535 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110546 pParse->pNewTable = pNew;
110547 pNew->nTabRef = 1;
110548 pNew->nCol = pTab->nCol;
110549 assert( pNew->nCol>0 );
110550 nAlloc = (((pNew->nCol-1)/8)*8)+8;
110551 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
110552 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
110553 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
110554 if( !pNew->aCol || !pNew->zName ){
110555 assert( db->mallocFailed );
110558 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
110559 for(i=0; i<pNew->nCol; i++){
110560 Column *pCol = &pNew->aCol[i];
110561 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
110562 pCol->hName = sqlite3StrIHash(pCol->zCnName);
110565 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
110566 pNew->pSchema = db->aDb[iDb].pSchema;
110567 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
110568 pNew->nTabRef = 1;
110579 ** it loads an error message into pParse and returns non-zero.
110599 zType, pTab->zName
110616 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
110620 sqlite3 *db = pParse->db; /* Database connection */
110630 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110638 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
110640 zDb = db->aDb[iSchema].zDbSName;
110644 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110653 for(iCol=0; iCol<pTab->nCol; iCol++){
110654 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
110656 if( iCol==pTab->nCol ){
110661 /* Ensure the schema contains no double-quoted strings */
110672 assert( pNew->n>0 );
110673 bQuote = sqlite3Isquote(pNew->z[0]);
110680 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
110681 pTab->zName
110688 zDb, pTab->zName, iCol, zNew, bQuote
110743 ** rename-token list.
110745 ** 2. Dereferences each pointer in the rename-token list.
110748 ** address-sanitizer or similar. If any of these pointers no longer
110749 ** point to valid objects, an exception is raised by the memory-checking
110763 assert( pParse==pParse->db->pParse );
110764 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
110765 if( pParse->nErr==0 ){
110768 for(p=pParse->pRename; p; p=p->pNext){
110769 if( p->p ){
110770 assert( p->p!=pPtr );
110771 i += *(u8*)(p->p);
110786 ** in pParse->pRename.
110798 assert( pPtr || pParse->db->mallocFailed );
110800 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
110801 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
110803 pNew->p = pPtr;
110804 pNew->t = *pToken;
110805 pNew->pNext = pParse->pRename;
110806 pParse->pRename = pNew;
110821 for(p=pParse->pRename; p; p=p->pNext){
110822 if( p->p==pFrom ){
110823 p->p = pTo;
110833 Parse *pParse = pWalker->pParse;
110836 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
110846 With *pWith = pSelect->pWith;
110848 Parse *pParse = pWalker->pParse;
110851 assert( pWith->nCte>0 );
110852 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
110853 /* Push a copy of the With object onto the with-stack. We use a copy
110855 ** and SF_Resolved) below. And the parser code that uses the with-stack
110858 pCopy = sqlite3WithDup(pParse->db, pWith);
110861 for(i=0; i<pWith->nCte; i++){
110862 Select *p = pWith->a[i].pSelect;
110867 if( sNC.pParse->db->mallocFailed ) return;
110869 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
110871 if( pCopy && pParse->pWith==pCopy ){
110872 pParse->pWith = pCopy->pOuter;
110886 for(ii=0; ii<pIdList->nId; ii++){
110887 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
110895 Parse *pParse = pWalker->pParse;
110897 if( pParse->nErr ) return WRC_Abort;
110898 testcase( p->selFlags & SF_View );
110899 testcase( p->selFlags & SF_CopyCte );
110900 if( p->selFlags & (SF_View|SF_CopyCte) ){
110903 if( ALWAYS(p->pEList) ){
110904 ExprList *pList = p->pEList;
110905 for(i=0; i<pList->nExpr; i++){
110906 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
110907 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
110911 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
110912 SrcList *pSrc = p->pSrc;
110913 for(i=0; i<pSrc->nSrc; i++){
110914 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
110915 if( pSrc->a[i].fg.isUsing==0 ){
110916 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
110918 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
110931 u8 eMode = pParse->eParseMode;
110937 pParse->eParseMode = PARSE_MODE_UNMAP;
110939 pParse->eParseMode = eMode;
110943 ** Remove all nodes that are part of expression-list pEList from the
110954 for(i=0; i<pEList->nExpr; i++){
110955 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
110956 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
110969 pNext = p->pNext;
110992 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
110993 if( (*pp)->p==pPtr ){
110996 *pp = pToken->pNext;
110997 pToken->pNext = pCtx->pList;
110998 pCtx->pList = pToken;
110999 pCtx->nList++;
111010 ** descend into sub-select statements.
111013 if( p->selFlags & (SF_View|SF_CopyCte) ){
111014 testcase( p->selFlags & SF_View );
111015 testcase( p->selFlags & SF_CopyCte );
111029 ** constructed in RenameCtx object at pWalker->u.pRename.
111032 RenameCtx *p = pWalker->u.pRename;
111033 if( pExpr->op==TK_TRIGGER
111034 && pExpr->iColumn==p->iCol
111035 && pWalker->pParse->pTriggerTab==p->pTab
111037 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111038 }else if( pExpr->op==TK_COLUMN
111039 && pExpr->iColumn==p->iCol
111041 && p->pTab==pExpr->y.pTab
111043 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111058 RenameToken *pBest = pCtx->pList;
111062 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
111063 if( pToken->t.z>pBest->t.z ) pBest = pToken;
111065 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
111066 *pp = pBest->pNext;
111073 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
111075 ** sub-routine is currently stored in pParse->zErrMsg. This function
111089 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
111091 pParse->zErrMsg
111093 sqlite3_result_error(pCtx, zErr, -1);
111094 sqlite3DbFree(pParse->db, zErr);
111098 ** For each name in the the expression-list pEList (i.e. each
111099 ** pEList->a[i].zName) that matches the string in zOld, extract the
111100 ** corresponding rename-token from Parse object pParse and add it
111111 for(i=0; i<pEList->nExpr; i++){
111112 const char *zName = pEList->a[i].zEName;
111113 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
111124 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
111125 ** that matches the string in zOld, extract the corresponding rename-token
111136 for(i=0; i<pIdList->nId; i++){
111137 const char *zName = pIdList->a[i].zName;
111166 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
111167 p->eParseMode = PARSE_MODE_RENAME;
111168 p->db = db;
111169 p->nQueryLoop = 1;
111171 if( db->mallocFailed ) rc = SQLITE_NOMEM;
111173 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
111184 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
111185 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
111190 db->init.iDb = 0;
111230 nQuot = sqlite3Strlen30(zQuot)-1;
111234 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
111243 /* At this point pRename->pList contains a list of RenameToken objects
111245 ** with the new column name, or with single-quoted versions of themselves.
111250 while( pRename->pList ){
111257 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
111263 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
111266 /* Dequote the double-quoted token. Then requote it again, this time
111269 ** add another space after the new, single-quoted version of the
111272 memcpy(zBuf1, pBest->t.z, pBest->t.n);
111273 zBuf1[pBest->t.n] = 0;
111276 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
111282 iOff = pBest->t.z - zSql;
111283 if( pBest->t.n!=nReplace ){
111284 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
111285 nOut - (iOff + pBest->t.n)
111287 nOut += nReplace - pBest->t.n;
111294 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
111305 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
111311 sqlite3 *db = pParse->db;
111312 Trigger *pNew = pParse->pNewTrigger;
111319 assert( pNew->pTabSchema );
111320 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
111321 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
111323 pParse->eTriggerOp = pNew->op;
111326 if( ALWAYS(pParse->pTriggerTab) ){
111327 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
111331 if( rc==SQLITE_OK && pNew->pWhen ){
111332 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
111335 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
111336 if( pStep->pSelect ){
111337 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
111338 if( pParse->nErr ) rc = pParse->rc;
111340 if( rc==SQLITE_OK && pStep->zTarget ){
111344 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
111347 pStep->pExprList = 0;
111352 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
111353 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
111354 assert( pSrc==pSel->pSrc );
111355 if( pStep->pExprList ) pSel->pEList = 0;
111356 pSel->pSrc = 0;
111359 if( pStep->pFrom ){
111361 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
111362 SrcItem *p = &pStep->pFrom->a[i];
111363 if( p->pSelect ){
111364 sqlite3SelectPrep(pParse, p->pSelect, 0);
111369 if( db->mallocFailed ){
111373 if( rc==SQLITE_OK && pStep->pWhere ){
111374 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
111377 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
111379 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
111380 if( pStep->pUpsert && rc==SQLITE_OK ){
111381 Upsert *pUpsert = pStep->pUpsert;
111382 pUpsert->pUpsertSrc = pSrc;
111385 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
111387 ExprList *pUpsertSet = pUpsert->pUpsertSet;
111391 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
111394 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
111416 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
111419 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111420 sqlite3WalkSelect(pWalker, pStep->pSelect);
111421 sqlite3WalkExpr(pWalker, pStep->pWhere);
111422 sqlite3WalkExprList(pWalker, pStep->pExprList);
111423 if( pStep->pUpsert ){
111424 Upsert *pUpsert = pStep->pUpsert;
111425 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
111426 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
111427 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
111428 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
111430 if( pStep->pFrom ){
111432 for(i=0; i<pStep->pFrom->nSrc; i++){
111433 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
111444 sqlite3 *db = pParse->db;
111446 if( pParse->pVdbe ){
111447 sqlite3VdbeFinalize(pParse->pVdbe);
111449 sqlite3DeleteTable(db, pParse->pNewTable);
111450 while( (pIdx = pParse->pNewIndex)!=0 ){
111451 pParse->pNewIndex = pIdx->pNext;
111454 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
111455 sqlite3DbFree(db, pParse->zErrMsg);
111456 renameTokenFree(db, pParse->pRename);
111472 ** 7. bQuote: Non-zero if the new column name should be quoted.
111476 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
111506 sqlite3_xauth xAuth = db->xAuth;
111516 if( pTab==0 || iCol>=pTab->nCol ){
111520 zOld = pTab->aCol[iCol].zCnName;
111522 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
111525 db->xAuth = 0;
111540 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111541 pSelect->selFlags &= ~SF_View;
111544 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
111551 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
111555 if( iCol<sParse.pNewTable->nCol ){
111557 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
111561 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
111563 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111564 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
111565 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111567 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
111568 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111571 for(i=0; i<sParse.pNewTable->nCol; i++){
111573 &sParse.pNewTable->aCol[i]);
111580 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111581 for(i=0; i<pFKey->nCol; i++){
111582 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
111583 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
111585 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
111586 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
111588 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
111594 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111595 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111602 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
111603 if( pStep->zTarget ){
111604 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
111606 if( pStep->pUpsert ){
111607 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
111610 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
111611 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
111619 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
111643 db->xAuth = xAuth;
111652 RenameCtx *p = pWalker->u.pRename;
111653 if( pExpr->op==TK_COLUMN
111655 && p->pTab==pExpr->y.pTab
111657 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
111667 RenameCtx *p = pWalker->u.pRename;
111668 SrcList *pSrc = pSelect->pSrc;
111669 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
111670 testcase( pSelect->selFlags & SF_View );
111671 testcase( pSelect->selFlags & SF_CopyCte );
111675 assert( pWalker->pParse->db->mallocFailed );
111678 for(i=0; i<pSrc->nSrc; i++){
111679 SrcItem *pItem = &pSrc->a[i];
111680 if( pItem->pTab==p->pTab ){
111681 renameTokenFind(pWalker->pParse, p, pItem->zName);
111707 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
111730 sqlite3_xauth xAuth = db->xAuth;
111731 db->xAuth = 0;
111747 int isLegacy = (db->flags & SQLITE_LegacyAlter);
111753 Select *pSelect = pTab->u.view.pSelect;
111758 assert( pSelect->selFlags & SF_View );
111759 pSelect->selFlags &= ~SF_View;
111760 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
111764 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
111770 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
111775 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111776 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
111777 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
111786 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
111789 sqlite3WalkExprList(&sWalker, pTab->pCheck);
111791 renameTokenFind(&sParse, &sCtx, pTab->zName);
111797 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
111799 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111807 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
111808 && sCtx.pTab->pSchema==pTrigger->pTabSchema
111810 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
111817 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111818 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
111819 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
111821 if( pStep->pFrom ){
111823 for(i=0; i<pStep->pFrom->nSrc; i++){
111824 SrcItem *pItem = &pStep->pFrom->a[i];
111825 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
111826 renameTokenFind(&sParse, &sCtx, pItem->zName);
111854 db->xAuth = xAuth;
111862 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
111863 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
111871 ** double-quotes use single quotes instead.
111905 sqlite3_xauth xAuth = db->xAuth;
111906 db->xAuth = 0;
111931 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111932 pSelect->selFlags &= ~SF_View;
111935 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
111941 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111943 for(i=0; i<sParse.pNewTable->nCol; i++){
111946 &sParse.pNewTable->aCol[i]));
111951 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111952 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111978 db->xAuth = xAuth;
112017 int isLegacy = (db->flags & SQLITE_LegacyAlter);
112022 sqlite3_xauth xAuth = db->xAuth;
112023 db->xAuth = 0;
112031 int flags = db->flags;
112032 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
112034 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
112040 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
112049 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
112067 db->xAuth = xAuth;
112076 ** argv[0]: An integer - the index of the schema containing the table
112078 ** argv[2]: An integer - the index of the column to remove.
112092 const char *zDb = db->aDb[iSchema].zDbSName;
112101 sqlite3_xauth xAuth = db->xAuth;
112102 db->xAuth = 0;
112109 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
112115 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
112116 if( iCol<pTab->nCol-1 ){
112118 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
112119 zEnd = (const char*)pEnd->t.z;
112122 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
112123 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
112126 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
112127 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
112133 db->xAuth = xAuth;
112149 sqlite3 *db = pParse->db; /* Database handle */
112154 int iCol; /* Index of column zCol in pTab->aCol[] */
112157 assert( pParse->pNewTable==0 );
112159 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
112160 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112171 assert( db->mallocFailed );
112182 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
112184 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
112191 if( pTab->nCol<=1 ){
112197 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112199 zDb = db->aDb[iDb].zDbSName;
112202 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
112212 , zDb, iDb, iCol, pTab->zName
112220 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
112226 int nField = 0; /* Number of non-virtual columns after drop */
112229 iCur = pParse->nTab++;
112232 reg = ++pParse->nMem;
112235 pParse->nMem += pTab->nCol;
112238 pParse->nMem += pPk->nColumn;
112239 for(i=0; i<pPk->nKeyCol; i++){
112242 nField = pPk->nKeyCol;
112244 regRec = ++pParse->nMem;
112245 for(i=0; i<pTab->nCol; i++){
112246 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
112251 if( iPos<pPk->nKeyCol ) continue;
112252 regOut = reg+1+iPos-(iPos>iColPos);
112256 if( i==pTab->iPKey ){
112266 pParse->nMem++;
112272 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
112288 ** Register built-in functions used to help implement ALTER TABLE
112305 ** 2005-07-08
112355 ** columns. The N-th integer (for N>1) is the average number of rows in
112356 ** the index which have the same value for the first N-1 columns. For
112357 ** a K-column index, there will be K+1 integers in the stat column. If
112381 ** inclusive are samples of the left-most key value in the index taken at
112388 ** For i between 0 and S-1. Conceptually, the index space is divided into
112415 ** of entries in the index whose left-most column exactly matches
112416 ** the left-most column of the sample. The second integer in nEq
112422 ** left-most column is less than the left-most column of the sample.
112423 ** The K-th integer in the nLt entry is the number of index entries
112438 ** looks at the left-most column of the index. The sqlite_stat3.sample
112439 ** column contains the actual value of the left-most column instead
112460 ** appropriate compile-time options are provided.
112489 sqlite3 *db = pParse->db;
112503 pDb = &db->aDb[iDb];
112512 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
112515 ** side-effect of the CREATE TABLE statement is to leave the rootpage
112516 ** of the new table in register pParse->regRoot. This is important
112519 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
112521 aRoot[i] = (u32)pParse->regRoot;
112528 aRoot[i] = pStat->tnum;
112533 pDb->zDbSName, zTab, zWhereType, zWhere
112536 }else if( db->xPreUpdateCallback ){
112537 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
112563 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
112588 int nLimit; /* Analysis row-scan limit */
112591 u8 nSkipAhead; /* Number of times of skip-ahead */
112596 u32 iPrn; /* Pseudo-random number used for sampling */
112611 if( p->nRowid ){
112612 sqlite3DbFree(db, p->u.aRowid);
112613 p->nRowid = 0;
112623 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112624 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
112625 if( p->u.aRowid ){
112626 p->nRowid = n;
112627 memcpy(p->u.aRowid, pData, n);
112629 p->nRowid = 0;
112639 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112640 p->nRowid = 0;
112641 p->u.iRowid = iRowid;
112651 pTo->isPSample = pFrom->isPSample;
112652 pTo->iCol = pFrom->iCol;
112653 pTo->iHash = pFrom->iHash;
112654 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
112655 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
112656 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
112657 if( pFrom->nRowid ){
112658 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
112660 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
112671 if( p->mxSample ){
112673 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
112674 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
112675 sampleClear(p->db, &p->current);
112678 sqlite3DbFree(p->db, p);
112687 ** L: A limit on the number of rows to scan, or 0 for no-limit
112745 p->db = db;
112746 p->nEst = sqlite3_value_int64(argv[2]);
112747 p->nRow = 0;
112748 p->nLimit = sqlite3_value_int64(argv[3]);
112749 p->nCol = nCol;
112750 p->nKeyCol = nKeyCol;
112751 p->nSkipAhead = 0;
112752 p->current.anDLt = (tRowcnt*)&p[1];
112753 p->current.anEq = &p->current.anDLt[nColUp];
112756 p->mxSample = p->nLimit==0 ? mxSample : 0;
112759 int i; /* Used to iterate through p->aSample[] */
112761 p->iGet = -1;
112762 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
112763 p->current.anLt = &p->current.anEq[nColUp];
112764 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
112767 p->a = (struct StatSample*)&p->current.anLt[nColUp];
112768 p->aBest = &p->a[mxSample];
112769 pSpace = (u8*)(&p->a[mxSample+nCol]);
112771 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112772 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112773 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112775 assert( (pSpace - (u8*)p)==n );
112778 p->aBest[i].iCol = i;
112803 ** pNew and pOld are both candidate non-periodic samples selected for
112804 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
112811 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
112818 int nCol = pAccum->nCol;
112820 assert( pNew->iCol==pOld->iCol );
112821 for(i=pNew->iCol+1; i<nCol; i++){
112822 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
112823 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
112825 if( pNew->iHash>pOld->iHash ) return 1;
112835 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
112842 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
112843 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
112845 assert( pOld->isPSample==0 && pNew->isPSample==0 );
112846 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
112850 if( pNew->iCol<pOld->iCol ) return 1;
112851 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
112857 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
112858 ** remove the least desirable sample from p->a[] to make room.
112870 if( nEqZero>p->nMaxEqZero ){
112871 p->nMaxEqZero = nEqZero;
112873 if( pNew->isPSample==0 ){
112875 assert( pNew->anEq[pNew->iCol]>0 );
112882 for(i=p->nSample-1; i>=0; i--){
112883 StatSample *pOld = &p->a[i];
112884 if( pOld->anEq[pNew->iCol]==0 ){
112885 if( pOld->isPSample ) return;
112886 assert( pOld->iCol>pNew->iCol );
112894 pUpgrade->iCol = pNew->iCol;
112895 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
112901 if( p->nSample>=p->mxSample ){
112902 StatSample *pMin = &p->a[p->iMin];
112903 tRowcnt *anEq = pMin->anEq;
112904 tRowcnt *anLt = pMin->anLt;
112905 tRowcnt *anDLt = pMin->anDLt;
112906 sampleClear(p->db, pMin);
112907 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
112908 pSample = &p->a[p->nSample-1];
112909 pSample->nRowid = 0;
112910 pSample->anEq = anEq;
112911 pSample->anDLt = anDLt;
112912 pSample->anLt = anLt;
112913 p->nSample = p->mxSample-1;
112916 /* The "rows less-than" for the rowid column must be greater than that
112917 ** for the last sample in the p->a[] array. Otherwise, the samples would
112919 assert( p->nSample==0
112920 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
112923 pSample = &p->a[p->nSample];
112925 p->nSample++;
112928 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
112931 if( p->nSample>=p->mxSample ){
112932 int iMin = -1;
112933 for(i=0; i<p->mxSample; i++){
112934 if( p->a[i].isPSample ) continue;
112935 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
112940 p->iMin = iMin;
112948 ** p->current contains a sample that reflects the previous row of the
112957 for(i=(p->nCol-2); i>=iChng; i--){
112958 StatSample *pBest = &p->aBest[i];
112959 pBest->anEq[i] = p->current.anEq[i];
112960 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
112966 ** p->nMaxEqZero or greater set to zero. */
112967 for(i=p->nSample-1; i>=0; i--){
112969 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
112973 if( iChng<p->nMaxEqZero ){
112974 for(i=p->nSample-1; i>=0; i--){
112976 for(j=iChng; j<p->nCol; j++){
112977 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
112980 p->nMaxEqZero = iChng;
112990 ** C Index of left-most column to differ from previous row
113000 ** if it wants the byte-code to do special processing.
113017 assert( p->nCol>0 );
113018 assert( iChng<p->nCol );
113020 if( p->nRow==0 ){
113022 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
113026 if( p->mxSample ) samplePushPrevious(p, iChng);
113032 p->current.anEq[i]++;
113034 for(i=iChng; i<p->nCol; i++){
113035 p->current.anDLt[i]++;
113037 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
113039 p->current.anEq[i] = 1;
113043 p->nRow++;
113045 if( p->mxSample ){
113048 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
113050 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
113053 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
113055 nLt = p->current.anLt[p->nCol-1];
113057 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
113058 p->current.isPSample = 1;
113059 p->current.iCol = 0;
113060 sampleInsert(p, &p->current, p->nCol-1);
113061 p->current.isPSample = 0;
113065 for(i=0; i<(p->nCol-1); i++){
113066 p->current.iCol = i;
113067 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
113068 sampleCopy(p, &p->aBest[i], &p->current);
113073 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
113074 p->nSkipAhead++;
113075 sqlite3_result_int(context, p->current.anDLt[0]>0);
113113 ** a one-parameter function, stat_get(P), that always returns the
113130 assert( eCall==STAT_GET_STAT1 || p->mxSample );
113155 ** I = (K+D-1)/D
113165 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
113167 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
113168 for(i=0; i<p->nKeyCol; i++){
113169 u64 nDistinct = p->current.anDLt[i] + 1;
113170 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
113171 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
113173 assert( p->current.anEq[i] );
113179 if( p->iGet<0 ){
113181 p->iGet = 0;
113183 if( p->iGet<p->nSample ){
113184 StatSample *pS = p->a + p->iGet;
113185 if( pS->nRowid==0 ){
113186 sqlite3_result_int64(context, pS->u.iRowid);
113188 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
113197 assert( p->iGet<p->nSample );
113199 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
113200 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
113202 aCnt = p->a[p->iGet].anDLt;
113203 p->iGet++;
113207 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
113208 for(i=0; i<p->nCol; i++){
113211 if( sStat.nChar ) sStat.nChar--;
113233 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
113246 ** of the k-th column of the pIdx index.
113254 assert( k>=0 && k<pIdx->nColumn );
113255 i = pIdx->aiColumn[k];
113257 VdbeComment((v,"%s.rowid",pIdx->zName));
113259 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113261 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113280 sqlite3 *db = pParse->db; /* Database handle */
113286 int jZeroRows = -1; /* Jump from here if number of rows is zero */
113303 pParse->nMem = MAX(pParse->nMem, iMem);
113312 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
113317 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113321 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
113322 db->aDb[iDb].zDbSName ) ){
113328 if( db->xPreUpdateCallback ){
113331 pStat1->zName = (char*)&pStat1[1];
113332 memcpy(pStat1->zName, "sqlite_stat1", 13);
113333 pStat1->nCol = 3;
113334 pStat1->iPKey = -1;
113335 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
113339 /* Establish a read-lock on the table at the shared-cache level.
113340 ** Open a read-only cursor on the table. Also allocate a cursor number
113343 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113346 pParse->nTab = MAX(pParse->nTab, iTab);
113348 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
113350 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113358 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
113360 nCol = pIdx->nKeyCol;
113361 zIdxName = pTab->zName;
113362 nColTest = nCol - 1;
113364 nCol = pIdx->nColumn;
113365 zIdxName = pIdx->zName;
113366 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
113371 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
113374 ** Pseudo-code for loop that calls stat_push():
113409 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
113411 /* Open a read-only cursor on the index being analyzed. */
113412 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
113413 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
113415 VdbeComment((v, "%s", pIdx->zName));
113426 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
113440 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
113473 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
113474 /* For a single-column UNIQUE index, once we have found a non-NULL
113481 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
113501 sqlite3VdbeJumpHere(v, addrNextRow-1);
113524 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
113526 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
113527 for(j=0; j<pPk->nKeyCol; j++){
113528 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
113529 assert( k>=0 && k<pIdx->nColumn );
113533 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
113534 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
113542 if( db->nAnalysisLimit ){
113564 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
113570 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
113581 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
113599 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
113613 VdbeComment((v, "%s", pTab->zName));
113623 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
113645 sqlite3 *db = pParse->db;
113646 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
113653 iStatCur = pParse->nTab;
113654 pParse->nTab += 3;
113656 iMem = pParse->nMem+1;
113657 iTab = pParse->nTab;
113659 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
113676 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113677 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113679 iStatCur = pParse->nTab;
113680 pParse->nTab += 3;
113682 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
113684 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
113686 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
113694 ** ANALYZE -- 1
113695 ** ANALYZE <database> -- 2
113696 ** ANALYZE ?<database>.?<tablename> -- 3
113703 sqlite3 *db = pParse->db;
113714 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113722 for(i=0; i<db->nDb; i++){
113726 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
113733 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
113737 analyzeTable(pParse, pIdx->pTable, pIdx);
113745 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
113761 ** The first argument points to a nul-terminated string containing a
113785 v = v*10 + c - '0';
113804 pIndex->bUnordered = 0;
113805 pIndex->noSkipScan = 0;
113808 pIndex->bUnordered = 1;
113809 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
113812 pIndex->szIdxRow = sqlite3LogEst(sz);
113814 pIndex->noSkipScan = 1;
113817 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
113818 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
113833 ** argv[2] = results of analysis - on integer for each column
113850 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
113859 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
113865 int nCol = pIndex->nKeyCol+1;
113870 if( pIndex->aiRowEst==0 ){
113871 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
113872 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
113874 aiRowEst = pIndex->aiRowEst;
113876 pIndex->bUnordered = 0;
113877 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
113878 pIndex->hasStat1 = 1;
113879 if( pIndex->pPartIdxWhere==0 ){
113880 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
113881 pTable->tabFlags |= TF_HasStat1;
113885 fakeIdx.szIdxRow = pTable->szTabRow;
113889 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
113890 pTable->szTabRow = fakeIdx.szIdxRow;
113891 pTable->tabFlags |= TF_HasStat1;
113903 if( pIdx->aSample ){
113905 for(j=0; j<pIdx->nSample; j++){
113906 IndexSample *p = &pIdx->aSample[j];
113907 sqlite3DbFree(db, p->p);
113909 sqlite3DbFree(db, pIdx->aSample);
113911 if( db && db->pnBytesFreed==0 ){
113912 pIdx->nSample = 0;
113913 pIdx->aSample = 0;
113923 ** Populate the pIdx->aAvgEq[] array based on the samples currently
113924 ** stored in pIdx->aSample[].
113928 IndexSample *aSample = pIdx->aSample;
113929 IndexSample *pFinal = &aSample[pIdx->nSample-1];
113932 if( pIdx->nSampleCol>1 ){
113937 nCol = pIdx->nSampleCol-1;
113938 pIdx->aAvgEq[nCol] = 1;
113941 int nSample = pIdx->nSample;
113949 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
113950 nRow = pFinal->anLt[iCol];
113951 nDist100 = (i64)100 * pFinal->anDLt[iCol];
113952 nSample--;
113954 nRow = pIdx->aiRowEst[0];
113955 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
113957 pIdx->nRowEst0 = nRow;
113964 if( i==(pIdx->nSample-1)
113973 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
113976 pIdx->aAvgEq[iCol] = avgEq;
114020 IndexSample *pSample; /* A slot in pIdx->aSample[] */
114022 assert( db->lookaside.bDisable );
114027 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114045 assert( pIdx==0 || pIdx->nSample==0 );
114047 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
114048 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
114049 nIdxCol = pIdx->nKeyCol;
114051 nIdxCol = pIdx->nColumn;
114053 pIdx->nSampleCol = nIdxCol;
114058 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
114059 if( pIdx->aSample==0 ){
114063 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
114064 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
114065 pIdx->pTable->tabFlags |= TF_HasStat4;
114067 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
114068 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
114069 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
114071 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
114080 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114095 nCol = pIdx->nSampleCol;
114100 pSample = &pIdx->aSample[pIdx->nSample];
114101 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
114102 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
114103 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
114111 pSample->n = sqlite3_column_bytes(pStmt, 4);
114112 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
114113 if( pSample->p==0 ){
114117 if( pSample->n ){
114118 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
114120 pIdx->nSample++;
114135 assert( db->lookaside.bDisable );
114165 ** If an OOM error occurs, this function always sets db->mallocFailed.
114174 Schema *pSchema = db->aDb[iDb].pSchema;
114177 assert( iDb>=0 && iDb<db->nDb );
114178 assert( db->aDb[iDb].pBt!=0 );
114182 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
114184 pTab->tabFlags &= ~TF_HasStat1;
114186 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114188 pIdx->hasStat1 = 0;
114191 pIdx->aSample = 0;
114197 sInfo.zDatabase = db->aDb[iDb].zDbSName;
114213 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114215 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
114225 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114227 sqlite3_free(pIdx->aiRowEst);
114228 pIdx->aiRowEst = 0;
114281 if( pExpr->op!=TK_ID ){
114284 pExpr->op = TK_STRING;
114296 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
114302 ** An SQL user-function registered to do the work of an ATTACH statement. The
114312 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
114313 ** new database, close the database on db->init.iDb and reopen it as an
114341 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
114348 ** from sqlite3_deserialize() to close database db->init.iDb and
114352 pNew = &db->aDb[db->init.iDb];
114353 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
114354 pNew->pBt = 0;
114355 pNew->pSchema = 0;
114356 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
114366 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
114367 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
114368 db->aLimit[SQLITE_LIMIT_ATTACHED]
114372 for(i=0; i<db->nDb; i++){
114380 /* Allocate the new entry in the db->aDb[] array and initialize the schema
114383 if( db->aDb==db->aDbStatic ){
114384 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
114386 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
114388 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
114391 db->aDb = aNew;
114392 pNew = &db->aDb[db->nDb];
114399 flags = db->openFlags;
114400 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
114403 sqlite3_result_error(context, zErr, -1);
114409 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
114410 db->nDb++;
114411 pNew->zDbSName = sqlite3DbStrDup(db, zName);
114413 db->noSharedCache = 0;
114419 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
114420 if( !pNew->pSchema ){
114422 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
114427 sqlite3BtreeEnter(pNew->pBt);
114428 pPager = sqlite3BtreePager(pNew->pBt);
114429 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
114430 sqlite3BtreeSecureDelete(pNew->pBt,
114431 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
114433 sqlite3BtreeSetPagerFlags(pNew->pBt,
114434 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
114436 sqlite3BtreeLeave(pNew->pBt);
114438 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
114439 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
114446 ** remove the entry from the db->aDb[] array. i.e. put everything back the
114451 db->init.iDb = 0;
114452 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
114463 if( newAuth<db->auth.authLevel ){
114470 int iDb = db->nDb - 1;
114472 if( db->aDb[iDb].pBt ){
114473 sqlite3BtreeClose(db->aDb[iDb].pBt);
114474 db->aDb[iDb].pBt = 0;
114475 db->aDb[iDb].pSchema = 0;
114478 db->nDb = iDb;
114495 sqlite3_result_error(context, zErrDyn, -1);
114502 ** An SQL user-function registered to do the work of an DETACH statement. The
114524 for(i=0; i<db->nDb; i++){
114525 pDb = &db->aDb[i];
114526 if( pDb->pBt==0 ) continue;
114530 if( i>=db->nDb ){
114538 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
114539 || sqlite3BtreeIsInBackup(pDb->pBt)
114547 assert( db->aDb[1].pSchema );
114548 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
114551 if( pTrig->pTabSchema==pDb->pSchema ){
114552 pTrig->pTabSchema = pTrig->pSchema;
114557 sqlite3BtreeClose(pDb->pBt);
114558 pDb->pBt = 0;
114559 pDb->pSchema = 0;
114564 sqlite3_result_error(context, zErr, -1);
114583 sqlite3* db = pParse->db;
114586 if( pParse->nErr ) goto attach_end;
114601 if( pAuthArg->op==TK_STRING ){
114603 zAuthArg = pAuthArg->u.zToken;
114621 assert( v || db->mallocFailed );
114623 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
114624 pFunc->nArg, pFunc, 0);
114683 DbFixer *pFix = p->u.pFix;
114684 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
114685 if( pExpr->op==TK_VARIABLE ){
114686 if( pFix->pParse->db->init.busy ){
114687 pExpr->op = TK_NULL;
114689 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
114700 DbFixer *pFix = p->u.pFix;
114703 sqlite3 *db = pFix->pParse->db;
114704 int iDb = sqlite3FindDbName(db, pFix->zDb);
114705 SrcList *pList = pSelect->pSrc;
114708 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
114709 if( pFix->bTemp==0 ){
114710 if( pItem->zDatabase ){
114711 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
114712 sqlite3ErrorMsg(pFix->pParse,
114714 pFix->zType, pFix->pName, pItem->zDatabase);
114717 sqlite3DbFree(db, pItem->zDatabase);
114718 pItem->zDatabase = 0;
114719 pItem->fg.notCte = 1;
114721 pItem->pSchema = pFix->pSchema;
114722 pItem->fg.fromDDL = 1;
114725 if( pList->a[i].fg.isUsing==0
114726 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
114732 if( pSelect->pWith ){
114733 for(i=0; i<pSelect->pWith->nCte; i++){
114734 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
114753 sqlite3 *db = pParse->db;
114754 assert( db->nDb>iDb );
114755 pFix->pParse = pParse;
114756 pFix->zDb = db->aDb[iDb].zDbSName;
114757 pFix->pSchema = db->aDb[iDb].pSchema;
114758 pFix->zType = zType;
114759 pFix->pName = pName;
114760 pFix->bTemp = (iDb==1);
114761 pFix->w.pParse = pParse;
114762 pFix->w.xExprCallback = fixExprCb;
114763 pFix->w.xSelectCallback = fixSelectCb;
114764 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
114765 pFix->w.walkerDepth = 0;
114766 pFix->w.eCode = 0;
114767 pFix->w.u.pFix = pFix;
114781 ** pParse->zErrMsg and these routines return non-zero. If everything
114793 res = sqlite3WalkSelect(&pFix->w, &s);
114802 return sqlite3WalkSelect(&pFix->w, pSelect);
114808 return sqlite3WalkExpr(&pFix->w, pExpr);
114818 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
114819 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
114820 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
114821 || sqlite3FixSrcList(pFix, pStep->pFrom)
114828 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
114829 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
114830 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
114831 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
114832 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
114839 pStep = pStep->pNext;
114862 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
114909 ** means that the SQL statement will never-run - the sqlite3_exec() call
114925 sqlite3_mutex_enter(db->mutex);
114926 db->xAuth = (sqlite3_xauth)xAuth;
114927 db->pAuthArg = pArg;
114928 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
114929 sqlite3_mutex_leave(db->mutex);
114934 ** Write an error message into pParse->zErrMsg that explains that the
114935 ** user-supplied authorization function returned an illegal value.
114939 pParse->rc = SQLITE_ERROR;
114957 sqlite3 *db = pParse->db; /* Database handle */
114958 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
114961 if( db->init.busy ) return SQLITE_OK;
114962 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
114964 ,db->auth.zAuthUser
114969 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
114971 pParse->rc = SQLITE_AUTH;
114995 int iSrc; /* Index in pTabList->a[] of table being read */
114999 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
115001 assert( pParse->db->xAuth!=0 );
115002 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
115009 if( pExpr->op==TK_TRIGGER ){
115010 pTab = pParse->pTriggerTab;
115013 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
115014 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
115015 pTab = pTabList->a[iSrc].pTab;
115020 iCol = pExpr->iColumn;
115024 assert( iCol<pTab->nCol );
115025 zCol = pTab->aCol[iCol].zCnName;
115026 }else if( pTab->iPKey>=0 ){
115027 assert( pTab->iPKey<pTab->nCol );
115028 zCol = pTab->aCol[pTab->iPKey].zCnName;
115032 assert( iDb>=0 && iDb<pParse->db->nDb );
115033 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
115034 pExpr->op = TK_NULL;
115051 sqlite3 *db = pParse->db;
115057 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
115058 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
115062 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
115063 ** callback are either NULL pointers or zero-terminated strings that
115071 testcase( pParse->zAuthContext==0 );
115073 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
115075 ,db->auth.zAuthUser
115080 pParse->rc = SQLITE_AUTH;
115091 ** popped. Or if pParse==0, this routine is a no-op.
115099 pContext->pParse = pParse;
115100 pContext->zAuthContext = pParse->zAuthContext;
115101 pParse->zAuthContext = zContext;
115109 if( pContext->pParse ){
115110 pContext->pParse->zAuthContext = pContext->zAuthContext;
115111 pContext->pParse = 0;
115158 ** Record the fact that we want to lock a table at run-time.
115181 for(i=0; i<pToplevel->nTableLock; i++){
115182 p = &pToplevel->aTableLock[i];
115183 if( p->iDb==iDb && p->iTab==iTab ){
115184 p->isWriteLock = (p->isWriteLock || isWriteLock);
115189 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
115190 pToplevel->aTableLock =
115191 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
115192 if( pToplevel->aTableLock ){
115193 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
115194 p->iDb = iDb;
115195 p->iTab = iTab;
115196 p->isWriteLock = isWriteLock;
115197 p->zLockName = zName;
115199 pToplevel->nTableLock = 0;
115200 sqlite3OomFault(pToplevel->db);
115211 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
115221 Vdbe *pVdbe = pParse->pVdbe;
115224 for(i=0; i<pParse->nTableLock; i++){
115225 TableLock *p = &pParse->aTableLock[i];
115226 int p1 = p->iDb;
115227 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
115228 p->zLockName, P4_STATIC);
115236 ** Return TRUE if the given yDbMask object is empty - if it contains no
115262 assert( pParse->pToplevel==0 );
115263 db = pParse->db;
115264 assert( db->pParse==pParse );
115265 if( pParse->nested ) return;
115266 if( pParse->nErr ){
115267 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
115270 assert( db->mallocFailed==0 );
115275 v = pParse->pVdbe;
115277 if( db->init.busy ){
115278 pParse->rc = SQLITE_DONE;
115282 if( v==0 ) pParse->rc = SQLITE_ERROR;
115284 assert( !pParse->isMultiWrite
115285 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
115287 if( pParse->bReturning ){
115288 Returning *pReturning = pParse->u1.pReturning;
115293 if( pReturning->nRetCol ){
115296 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
115298 reg = pReturning->iRetReg;
115299 for(i=0; i<pReturning->nRetCol; i++){
115300 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
115303 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
115311 if( pParse->nTableLock>0 && db->init.busy==0 ){
115313 if( db->auth.authLevel<UAUTH_User ){
115315 pParse->rc = SQLITE_AUTH_USER;
115327 if( db->mallocFailed==0
115328 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
115331 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115333 assert( db->nDb>0 );
115337 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115339 pSchema = db->aDb[iDb].pSchema;
115343 DbMaskTest(pParse->writeMask,iDb), /* P2 */
115344 pSchema->schema_cookie, /* P3 */
115345 pSchema->iGeneration /* P4 */
115347 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115349 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115350 }while( ++iDb<db->nDb );
115352 for(i=0; i<pParse->nVtabLock; i++){
115353 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115356 pParse->nVtabLock = 0;
115360 ** obtain the required table-locks. This is a no-op unless the
115361 ** shared-cache feature is enabled.
115376 if( pParse->pConstExpr ){
115377 ExprList *pEL = pParse->pConstExpr;
115378 pParse->okConstFactor = 0;
115379 for(i=0; i<pEL->nExpr; i++){
115380 int iReg = pEL->a[i].u.iConstExprReg;
115382 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115387 if( pParse->bReturning ){
115388 Returning *pRet = pParse->u1.pReturning;
115389 if( pRet->nRetCol ){
115390 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115401 assert( v!=0 || pParse->nErr );
115402 assert( db->mallocFailed==0 || pParse->nErr );
115403 if( pParse->nErr==0 ){
115406 assert( pParse->pAinc==0 || pParse->nTab>0 );
115408 pParse->rc = SQLITE_DONE;
115410 pParse->rc = SQLITE_ERROR;
115423 ** * Built-in SQL functions always take precedence over application-defined
115425 ** built-in function.
115430 sqlite3 *db = pParse->db;
115431 u32 savedDbFlags = db->mDbFlags;
115434 if( pParse->nErr ) return;
115435 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
115443 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
115444 pParse->nErr++;
115447 pParse->nested++;
115450 db->mDbFlags |= DBFLAG_PreferBuiltin;
115452 db->mDbFlags = savedDbFlags;
115455 pParse->nested--;
115469 ** Locate the in-memory structure that describes a particular database
115489 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
115494 for(i=0; i<db->nDb; i++){
115495 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
115497 if( i>=db->nDb ){
115506 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115513 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115518 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
115525 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
115528 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
115531 for(i=2; i<db->nDb; i++){
115533 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115538 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
115540 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115549 ** Locate the in-memory structure that describes a particular database
115552 ** error message in pParse->zErrMsg.
115555 ** routine leaves an error message in pParse->zErrMsg where
115565 sqlite3 *db = pParse->db;
115569 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
115581 if( pParse->disableVtab==0 && db->init.busy==0 ){
115582 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115587 testcase( pMod->pEpoTab==0 );
115588 return pMod->pEpoTab;
115593 pParse->checkSchema = 1;
115594 }else if( IsVirtual(p) && pParse->disableVtab ){
115606 assert( HasRowid(p) || p->iPKey<0 );
115617 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
115618 ** non-NULL if it is part of a view or trigger program definition. See
115627 assert( p->pSchema==0 || p->zDatabase==0 );
115628 if( p->pSchema ){
115629 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
115630 zDb = pParse->db->aDb[iDb].zDbSName;
115632 zDb = p->zDatabase;
115634 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
115654 ** Locate the in-memory structure that describes
115670 for(i=OMIT_TEMPDB; i<db->nDb; i++){
115672 Schema *pSchema = db->aDb[j].pSchema;
115676 p = sqlite3HashFind(&pSchema->idxHash, zName);
115689 sqlite3ExprDelete(db, p->pPartIdxWhere);
115690 sqlite3ExprListDelete(db, p->aColExpr);
115691 sqlite3DbFree(db, p->zColAff);
115692 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
115694 sqlite3_free(p->aiRowEst);
115710 pHash = &db->aDb[iDb].pSchema->idxHash;
115713 if( pIndex->pTable->pIndex==pIndex ){
115714 pIndex->pTable->pIndex = pIndex->pNext;
115719 p = pIndex->pTable->pIndex;
115720 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
115721 if( ALWAYS(p && p->pNext==pIndex) ){
115722 p->pNext = pIndex->pNext;
115727 db->mDbFlags |= DBFLAG_SchemaChange;
115731 ** Look through the list of open database files in db->aDb[] and if
115733 ** db->aDb[] structure to a smaller size, if possible.
115740 for(i=j=2; i<db->nDb; i++){
115741 struct Db *pDb = &db->aDb[i];
115742 if( pDb->pBt==0 ){
115743 sqlite3DbFree(db, pDb->zDbSName);
115744 pDb->zDbSName = 0;
115748 db->aDb[j] = db->aDb[i];
115752 db->nDb = j;
115753 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
115754 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
115755 sqlite3DbFree(db, db->aDb);
115756 db->aDb = db->aDbStatic;
115762 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
115767 assert( iDb<db->nDb );
115773 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
115776 if( db->nSchemaLock==0 ){
115777 for(i=0; i<db->nDb; i++){
115779 sqlite3SchemaClear(db->aDb[i].pSchema);
115792 for(i=0; i<db->nDb; i++){
115793 Db *pDb = &db->aDb[i];
115794 if( pDb->pSchema ){
115795 if( db->nSchemaLock==0 ){
115796 sqlite3SchemaClear(pDb->pSchema);
115802 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
115805 if( db->nSchemaLock==0 ){
115814 db->mDbFlags &= ~DBFLAG_SchemaChange;
115830 pList = pTab->u.tab.pDfltList;
115831 if( pCol->iDflt==0
115833 || NEVER(pList->nExpr<pCol->iDflt)
115835 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
115836 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
115838 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
115839 pList->a[pCol->iDflt-1].pExpr = pExpr;
115849 if( pCol->iDflt==0 ) return 0;
115851 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
115852 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
115853 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
115868 n = sqlite3Strlen30(pCol->zCnName) + 1;
115869 if( pCol->colFlags & COLFLAG_HASTYPE ){
115870 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
115873 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
115875 pCol->zCnName = zNew;
115876 memcpy(pCol->zCnName + n, zColl, nColl);
115877 pCol->colFlags |= COLFLAG_HASCOLL;
115886 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
115887 z = pCol->zCnName;
115889 if( pCol->colFlags & COLFLAG_HASTYPE ){
115903 if( (pCol = pTable->aCol)!=0 ){
115904 for(i=0; i<pTable->nCol; i++, pCol++){
115905 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
115906 sqlite3DbFree(db, pCol->zCnName);
115908 sqlite3DbFree(db, pTable->aCol);
115910 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
115912 if( db==0 || db->pnBytesFreed==0 ){
115913 pTable->aCol = 0;
115914 pTable->nCol = 0;
115916 pTable->u.tab.pDfltList = 0;
115934 ** db parameter can be used with db->pnBytesFreed to measure the memory
115949 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
115955 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
115956 pNext = pIndex->pNext;
115957 assert( pIndex->pSchema==pTable->pSchema
115958 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
115959 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
115960 char *zName = pIndex->zName;
115962 &pIndex->pSchema->idxHash, zName, 0
115964 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
115980 sqlite3SelectDelete(db, pTable->u.view.pSelect);
115986 sqlite3DbFree(db, pTable->zName);
115987 sqlite3DbFree(db, pTable->zColAff);
115988 sqlite3ExprListDelete(db, pTable->pCheck);
115997 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
116011 assert( iDb>=0 && iDb<db->nDb );
116014 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
116015 pDb = &db->aDb[iDb];
116016 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
116018 db->mDbFlags |= DBFLAG_SchemaChange;
116037 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
116053 if( p->nTab==0 ){
116054 p->nTab = 1;
116059 ** Parameter zName points to a nul-terminated buffer containing the name
116061 ** function returns the index of the named database in db->aDb[], or
116062 ** -1 if the named db cannot be found.
116065 int i = -1; /* Database number */
116068 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
116069 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
116081 ** index of the named database in db->aDb[], or -1 if the named db
116116 sqlite3 *db = pParse->db;
116119 if( pName2->n>0 ){
116120 if( db->init.busy ) {
116122 return -1;
116128 return -1;
116131 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
116132 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
116133 iDb = db->init.iDb;
116143 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
116144 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116146 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116148 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116150 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
116154 ** This routine is used to check if the UTF-8 string zName is a legal
116170 sqlite3 *db = pParse->db;
116172 || db->init.imposterTable
116178 if( db->init.busy ){
116179 if( sqlite3_stricmp(zType, db->init.azInit[0])
116180 || sqlite3_stricmp(zName, db->init.azInit[1])
116181 || sqlite3_stricmp(zTblName, db->init.azInit[2])
116187 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
116204 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
116211 ** find the (first) offset of that column in index pIdx. Or return -1
116216 for(i=0; i<pIdx->nColumn; i++){
116217 if( iCol==pIdx->aiColumn[i] ) return i;
116219 return -1;
116232 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
116235 if( pTab->tabFlags & TF_HasVirtual ){
116238 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
116250 ** the N-th virtual column (zero-based) then the storage number is
116251 ** the number of non-virtual columns in the table plus N.
116267 ** -- 0 1 2 3 4 5 6 7 8
116278 ** this routine is a no-op macro. If the pTab does not have any virtual
116279 ** columns, then this routine is no-op that always return iCol. If iCol
116285 assert( iCol<pTab->nCol );
116286 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
116288 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
116290 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
116292 return pTab->nNVCol + i - n;
116306 ** is a read-only no-op.
116309 int iReg = ++pParse->nMem;
116327 ** The new table record is initialized and put in pParse->pNewTable.
116344 sqlite3 *db = pParse->db;
116349 if( db->init.busy && db->init.newTnum==1 ){
116351 iDb = db->init.iDb;
116358 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
116370 pParse->sNameToken = *pName;
116375 if( db->init.iDb==1 ) isTemp = 1;
116386 char *zDb = db->aDb[iDb].zDbSName;
116405 char *zDb = db->aDb[iDb].zDbSName;
116415 assert( !db->init.busy || CORRUPT_DB );
116429 assert( db->mallocFailed );
116430 pParse->rc = SQLITE_NOMEM_BKPT;
116431 pParse->nErr++;
116434 pTable->zName = zName;
116435 pTable->iPKey = -1;
116436 pTable->pSchema = db->aDb[iDb].pSchema;
116437 pTable->nTabRef = 1;
116439 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
116441 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
116443 assert( pParse->pNewTable==0 );
116444 pParse->pNewTable = pTable;
116454 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
116471 reg1 = pParse->regRowid = ++pParse->nMem;
116472 reg2 = pParse->regRoot = ++pParse->nMem;
116473 reg3 = ++pParse->nMem;
116477 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
116483 /* This just creates a place-holder record in the sqlite_schema table.
116487 ** The rowid for the new entry is left in register pParse->regRowid.
116488 ** The root page number of the new table is left in reg pParse->regRoot.
116498 assert( !pParse->bReturning );
116499 pParse->u1.addrCrTab =
116510 /* Normal (non-error) return. */
116515 pParse->checkSchema = 1;
116525 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
116526 pCol->colFlags |= COLFLAG_HIDDEN;
116527 if( pTab ) pTab->tabFlags |= TF_HasHidden;
116528 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
116529 pTab->tabFlags |= TF_OOOHidden;
116537 ** with any application-generated triggers.
116546 pHash = &(db->aDb[1].pSchema->trigHash);
116548 sqlite3ExprListDelete(db, pRet->pReturnEL);
116571 sqlite3 *db = pParse->db;
116572 if( pParse->pNewTrigger ){
116575 assert( pParse->bReturning==0 );
116577 pParse->bReturning = 1;
116583 pParse->u1.pReturning = pRet;
116584 pRet->pParse = pParse;
116585 pRet->pReturnEL = pList;
116588 testcase( pParse->earlyCleanup );
116589 if( db->mallocFailed ) return;
116590 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
116591 pRet->retTrig.op = TK_RETURNING;
116592 pRet->retTrig.tr_tm = TRIGGER_AFTER;
116593 pRet->retTrig.bReturning = 1;
116594 pRet->retTrig.pSchema = db->aDb[1].pSchema;
116595 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
116596 pRet->retTrig.step_list = &pRet->retTStep;
116597 pRet->retTStep.op = TK_RETURNING;
116598 pRet->retTStep.pTrig = &pRet->retTrig;
116599 pRet->retTStep.pExprList = pList;
116600 pHash = &(db->aDb[1].pSchema->trigHash);
116601 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
116602 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
116603 ==&pRet->retTrig ){
116622 sqlite3 *db = pParse->db;
116629 if( (p = pParse->pNewTable)==0 ) return;
116630 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
116631 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
116641 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
116643 sType.n -= 6;
116644 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
116646 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
116648 sType.n -= 9;
116649 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
116678 for(i=0; i<p->nCol; i++){
116679 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
116685 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
116690 p->aCol = aNew;
116691 pCol = &p->aCol[p->nCol];
116692 memset(pCol, 0, sizeof(p->aCol[0]));
116693 pCol->zCnName = z;
116694 pCol->hName = hName;
116700 pCol->affinity = affinity;
116701 pCol->eCType = eType;
116702 pCol->szEst = szEst;
116706 pCol->colFlags |= COLFLAG_SORTERREF;
116715 pCol->affinity = sqlite3AffinityType(zType, pCol);
116716 pCol->colFlags |= COLFLAG_HASTYPE;
116718 p->nCol++;
116719 p->nNVCol++;
116720 pParse->constraintName.n = 0;
116732 p = pParse->pNewTable;
116733 if( p==0 || NEVER(p->nCol<1) ) return;
116734 pCol = &p->aCol[p->nCol-1];
116735 pCol->notNull = (u8)onError;
116736 p->tabFlags |= TF_HasNotNull;
116740 if( pCol->colFlags & COLFLAG_UNIQUE ){
116742 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
116743 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
116744 if( pIdx->aiColumn[0]==p->nCol-1 ){
116745 pIdx->uniqNotNull = 1;
116755 ** This routine does a case-independent search of zType for the
116763 ** --------------------------------
116821 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
116828 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
116833 pCol->colFlags |= COLFLAG_SORTERREF;
116838 pCol->szEst = v;
116861 sqlite3 *db = pParse->db;
116862 p = pParse->pNewTable;
116864 int isInit = db->init.busy && db->init.iDb!=1;
116865 pCol = &(p->aCol[p->nCol-1]);
116868 pCol->zCnName);
116870 }else if( pCol->colFlags & COLFLAG_GENERATED ){
116871 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116872 testcase( pCol->colFlags & COLFLAG_STORED );
116913 if( p->op==TK_STRING ){
116914 p->op = TK_ID;
116915 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
116916 p->pLeft->op = TK_ID;
116924 pCol->colFlags |= COLFLAG_PRIMKEY;
116926 if( pCol->colFlags & COLFLAG_GENERATED ){
116927 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116928 testcase( pCol->colFlags & COLFLAG_STORED );
116947 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
116960 Table *pTab = pParse->pNewTable;
116962 int iCol = -1, i;
116965 if( pTab->tabFlags & TF_HasPrimaryKey ){
116967 "table \"%s\" has more than one primary key", pTab->zName);
116970 pTab->tabFlags |= TF_HasPrimaryKey;
116972 iCol = pTab->nCol - 1;
116973 pCol = &pTab->aCol[iCol];
116977 nTerm = pList->nExpr;
116979 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
116982 if( pCExpr->op==TK_ID ){
116985 zCName = pCExpr->u.zToken;
116986 for(iCol=0; iCol<pTab->nCol; iCol++){
116987 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
116988 pCol = &pTab->aCol[iCol];
116998 && pCol->eCType==COLTYPE_INTEGER
117002 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
117003 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
117005 pTab->iPKey = iCol;
117006 pTab->keyConf = (u8)onError;
117008 pTab->tabFlags |= autoInc*TF_Autoincrement;
117009 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
117023 sqlite3ExprListDelete(pParse->db, pList);
117037 Table *pTab = pParse->pNewTable;
117038 sqlite3 *db = pParse->db;
117040 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
117042 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
117043 if( pParse->constraintName.n ){
117044 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
117048 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
117050 t.n = (int)(zEnd - t.z);
117051 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
117056 sqlite3ExprDelete(pParse->db, pCheckExpr);
117070 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
117071 i = p->nCol-1;
117072 db = pParse->db;
117078 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
117084 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117085 assert( pIdx->nKeyCol==1 );
117086 if( pIdx->aiColumn[0]==i ){
117087 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
117100 Table *pTab = pParse->pNewTable;
117106 pCol = &(pTab->aCol[pTab->nCol-1]);
117111 if( pCol->iDflt>0 ) goto generated_error;
117113 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
117114 /* no-op */
117115 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
117121 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
117122 pCol->colFlags |= eType;
117125 pTab->tabFlags |= eType;
117126 if( pCol->colFlags & COLFLAG_PRIMKEY ){
117135 pCol->zCnName);
117137 sqlite3ExprDelete(pParse->db, pExpr);
117140 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
117142 sqlite3ExprDelete(pParse->db, pExpr);
117156 ** This plan is not completely bullet-proof. It is possible for
117162 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
117163 ** the schema-version whenever the schema changes.
117166 sqlite3 *db = pParse->db;
117167 Vdbe *v = pParse->pVdbe;
117170 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
117193 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
117197 ** If the string zSignedIdent consists entirely of alpha-numeric
117200 ** it is quoted using double-quotes.
117236 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
117237 n += identLength(pCol->zCnName) + 5;
117239 n += identLength(p->zName);
117249 n += 35 + 6*p->nCol;
117257 identPut(zStmt, &k, p->zName);
117259 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
117270 sqlite3_snprintf(n-k, &zStmt[k], zSep);
117273 identPut(zStmt, &k, pCol->zCnName);
117274 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
117275 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
117276 testcase( pCol->affinity==SQLITE_AFF_BLOB );
117277 testcase( pCol->affinity==SQLITE_AFF_TEXT );
117278 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
117279 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
117280 testcase( pCol->affinity==SQLITE_AFF_REAL );
117282 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
117284 assert( pCol->affinity==SQLITE_AFF_BLOB
117285 || pCol->affinity==sqlite3AffinityType(zType, 0) );
117290 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
117301 if( pIdx->nColumn>=N ) return SQLITE_OK;
117302 assert( pIdx->isResized==0 );
117306 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
117307 pIdx->azColl = (const char**)zExtra;
117309 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
117310 pIdx->aiRowLogEst = (LogEst*)zExtra;
117312 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
117313 pIdx->aiColumn = (i16*)zExtra;
117315 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
117316 pIdx->aSortOrder = (u8*)zExtra;
117317 pIdx->nColumn = N;
117318 pIdx->isResized = 1;
117329 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
117330 wTable += pTabCol->szEst;
117332 if( pTab->iPKey<0 ) wTable++;
117333 pTab->szTabRow = sqlite3LogEst(wTable*4);
117342 const Column *aCol = pIdx->pTable->aCol;
117343 for(i=0; i<pIdx->nColumn; i++){
117344 i16 x = pIdx->aiColumn[i];
117345 assert( x<pIdx->pTable->nCol );
117346 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
117348 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
117356 while( nCol-- > 0 ){
117366 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
117379 assert( nKey<=pIdx->nColumn );
117380 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
117381 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
117382 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
117383 assert( pPk->pTable==pIdx->pTable );
117385 j = pPk->aiColumn[iCol];
117388 assert( pIdx->aiColumn[i]>=0 || j>=0 );
117389 if( pIdx->aiColumn[i]==j
117390 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
117402 ** high-order bit of colNotIdxed is always 1. All unindexed columns
117405 ** 2019-10-24: For the purpose of this computation, virtual columns are
117413 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
117419 Table *pTab = pIdx->pTable;
117420 for(j=pIdx->nColumn-1; j>=0; j--){
117421 int x = pIdx->aiColumn[j];
117422 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
117423 testcase( x==BMS-1 );
117424 testcase( x==BMS-2 );
117425 if( x<BMS-1 ) m |= MASKBIT(x);
117428 pIdx->colNotIdxed = ~m;
117429 assert( (pIdx->colNotIdxed>>63)==1 );
117462 sqlite3 *db = pParse->db;
117463 Vdbe *v = pParse->pVdbe;
117467 if( !db->init.imposterTable ){
117468 for(i=0; i<pTab->nCol; i++){
117469 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
117470 && (pTab->aCol[i].notNull==OE_None)
117472 pTab->aCol[i].notNull = OE_Abort;
117475 pTab->tabFlags |= TF_HasNotNull;
117481 assert( !pParse->bReturning );
117482 if( pParse->u1.addrCrTab ){
117484 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
117490 if( pTab->iPKey>=0 ){
117493 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
117497 pTab->tabFlags &= ~TF_WithoutRowid;
117501 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
117503 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
117504 assert( pParse->pNewTable==pTab );
117505 pTab->iPKey = -1;
117506 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
117508 if( pParse->nErr ){
117509 pTab->tabFlags &= ~TF_WithoutRowid;
117512 assert( db->mallocFailed==0 );
117514 assert( pPk->nKeyCol==1 );
117524 for(i=j=1; i<pPk->nKeyCol; i++){
117526 pPk->nColumn--;
117528 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
117529 pPk->azColl[j] = pPk->azColl[i];
117530 pPk->aSortOrder[j] = pPk->aSortOrder[i];
117531 pPk->aiColumn[j++] = pPk->aiColumn[i];
117534 pPk->nKeyCol = j;
117537 pPk->isCovering = 1;
117538 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
117539 nPk = pPk->nColumn = pPk->nKeyCol;
117545 if( v && pPk->tnum>0 ){
117546 assert( db->init.busy==0 );
117547 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
117551 pPk->tnum = pTab->tnum;
117553 /* Update the in-memory representation of all UNIQUE indices by converting
117556 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117560 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117561 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117567 pIdx->nColumn = pIdx->nKeyCol;
117570 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
117571 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
117572 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117573 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117574 pIdx->aiColumn[j] = pPk->aiColumn[i];
117575 pIdx->azColl[j] = pPk->azColl[i];
117576 if( pPk->aSortOrder[i] ){
117578 pIdx->bAscKeyBug = 1;
117583 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
117584 assert( pIdx->nColumn>=j );
117590 for(i=0; i<pTab->nCol; i++){
117591 if( !hasColumn(pPk->aiColumn, nPk, i)
117592 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
117595 for(i=0, j=nPk; i<pTab->nCol; i++){
117596 if( !hasColumn(pPk->aiColumn, j, i)
117597 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
117599 assert( j<pPk->nColumn );
117600 pPk->aiColumn[j] = i;
117601 pPk->azColl[j] = sqlite3StrBINARY;
117605 assert( pPk->nColumn==j );
117606 assert( pTab->nNVCol<=j );
117621 nName = sqlite3Strlen30(pTab->zName);
117622 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
117624 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117626 if( pMod->pModule->iVersion<3 ) return 0;
117627 if( pMod->pModule->xShadowName==0 ) return 0;
117628 return pMod->pModule->xShadowName(zName+nName+1);
117640 int nName; /* Length of pTab->zName */
117645 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117647 if( NEVER(pMod->pModule==0) ) return;
117648 if( pMod->pModule->iVersion<3 ) return;
117649 if( pMod->pModule->xShadowName==0 ) return;
117650 assert( pTab->zName!=0 );
117651 nName = sqlite3Strlen30(pTab->zName);
117652 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
117654 assert( pOther->zName!=0 );
117656 if( pOther->tabFlags & TF_Shadow ) continue;
117657 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
117658 && pOther->zName[nName]=='_'
117659 && pMod->pModule->xShadowName(pOther->zName+nName+1)
117661 pOther->tabFlags |= TF_Shadow;
117712 #define markExprListImmutable(X) /* no-op */
117725 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
117744 sqlite3 *db = pParse->db; /* The database connection */
117751 p = pParse->pNewTable;
117754 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
117755 p->tabFlags |= TF_Shadow;
117758 /* If the db->init.busy is 1 it means we are reading the SQL off the
117761 ** for the table from the db->init.newTnum field. (The page number
117765 ** table itself. So mark it read-only.
117767 if( db->init.busy ){
117768 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
117772 p->tnum = db->init.newTnum;
117773 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
117787 p->tabFlags |= TF_Strict;
117788 for(ii=0; ii<p->nCol; ii++){
117789 Column *pCol = &p->aCol[ii];
117790 if( pCol->eCType==COLTYPE_CUSTOM ){
117791 if( pCol->colFlags & COLFLAG_HASTYPE ){
117794 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
117798 p->zName, pCol->zCnName);
117801 }else if( pCol->eCType==COLTYPE_ANY ){
117802 pCol->affinity = SQLITE_AFF_BLOB;
117804 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
117805 && p->iPKey!=ii
117806 && pCol->notNull == OE_None
117808 pCol->notNull = OE_Abort;
117809 p->tabFlags |= TF_HasNotNull;
117814 assert( (p->tabFlags & TF_HasPrimaryKey)==0
117815 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
117816 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
117817 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
117821 if( (p->tabFlags & TF_Autoincrement) ){
117826 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
117827 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
117830 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
117833 iDb = sqlite3SchemaToIndex(db, p->pSchema);
117838 if( p->pCheck ){
117839 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
117840 if( pParse->nErr ){
117843 sqlite3ExprListDelete(db, p->pCheck);
117844 p->pCheck = 0;
117846 markExprListImmutable(p->pCheck);
117851 if( p->tabFlags & TF_HasGenerated ){
117853 testcase( p->tabFlags & TF_HasVirtual );
117854 testcase( p->tabFlags & TF_HasStored );
117855 for(ii=0; ii<p->nCol; ii++){
117856 u32 colFlags = p->aCol[ii].colFlags;
117858 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
117868 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
117876 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
117884 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117894 if( !db->init.busy ){
117922 ** statement to populate the new table. The root-page number for the
117923 ** new table is in register pParse->regRoot.
117929 ** A shared-cache write-lock is not required to write to the new table,
117930 ** as a schema-lock must have already been obtained to create it. Since
117931 ** a schema-lock excludes all other database users, the write-lock would
117936 int regYield; /* Register holding co-routine entry-point */
117937 int addrTop; /* Top of the co-routine */
117944 pParse->rc = SQLITE_ERROR;
117945 pParse->nErr++;
117948 regYield = ++pParse->nMem;
117949 regRec = ++pParse->nMem;
117950 regRowid = ++pParse->nMem;
117951 assert(pParse->nTab==1);
117953 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
117955 pParse->nTab = 2;
117958 if( pParse->nErr ) return;
117961 assert( p->aCol==0 );
117962 p->nCol = p->nNVCol = pSelTab->nCol;
117963 p->aCol = pSelTab->aCol;
117964 pSelTab->nCol = 0;
117965 pSelTab->aCol = 0;
117969 if( pParse->nErr ) return;
117971 sqlite3VdbeJumpHere(v, addrTop - 1);
117987 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
117988 n = (int)(pEnd2->z - pParse->sNameToken.z);
117989 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
117991 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
118003 db->aDb[iDb].zDbSName,
118005 p->zName,
118006 p->zName,
118007 pParse->regRoot,
118009 pParse->regRowid
118018 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
118019 Db *pDb = &db->aDb[iDb];
118021 if( pDb->pSchema->pSeqTab==0 ){
118024 pDb->zDbSName
118032 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
118035 /* Add the table to the in-memory representation of the database.
118037 if( db->init.busy ){
118039 Schema *pSchema = p->pSchema;
118041 assert( HasRowid(p) || p->iPKey<0 );
118042 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
118048 pParse->pNewTable = 0;
118049 db->mDbFlags |= DBFLAG_SchemaChange;
118054 assert( !pParse->nested );
118056 if( strcmp(p->zName, "sqlite_sequence")==0 ){
118058 p->pSchema->pSeqTab = p;
118066 if( pCons->z==0 ){
118069 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
118095 sqlite3 *db = pParse->db;
118097 if( pParse->nVar>0 ){
118102 p = pParse->pNewTable;
118103 if( p==0 || pParse->nErr ) goto create_view_fail;
118108 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
118111 p->tabFlags |= TF_NoVisibleRowid;
118115 iDb = sqlite3SchemaToIndex(db, p->pSchema);
118121 ** allocated rather than point to the input string - which means that
118124 pSelect->selFlags |= SF_View;
118126 p->u.view.pSelect = pSelect;
118129 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
118131 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
118132 p->eTabType = TABTYP_VIEW;
118133 if( db->mallocFailed ) goto create_view_fail;
118138 sEnd = pParse->sLastToken;
118144 n = (int)(sEnd.z - pBegin->z);
118146 z = pBegin->z;
118147 while( sqlite3Isspace(z[n-1]) ){ n--; }
118148 sEnd.z = &z[n-1];
118168 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118174 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118186 db->nSchemaLock++;
118188 db->nSchemaLock--;
118197 if( pTable->nCol>0 ) return 0;
118214 if( pTable->nCol<0 ){
118215 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
118218 assert( pTable->nCol>=0 );
118228 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
118230 u8 eParseMode = pParse->eParseMode;
118231 int nTab = pParse->nTab;
118232 int nSelect = pParse->nSelect;
118233 pParse->eParseMode = PARSE_MODE_NORMAL;
118234 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
118235 pTable->nCol = -1;
118238 xAuth = db->xAuth;
118239 db->xAuth = 0;
118241 db->xAuth = xAuth;
118245 pParse->nTab = nTab;
118246 pParse->nSelect = nSelect;
118248 pTable->nCol = 0;
118250 }else if( pTable->pCheck ){
118253 ** arglist which is stored in pTable->pCheck. The pCheck field
118257 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
118258 &pTable->nCol, &pTable->aCol);
118259 if( pParse->nErr==0
118260 && pTable->nCol==pSel->pEList->nExpr
118262 assert( db->mallocFailed==0 );
118270 assert( pTable->aCol==0 );
118271 pTable->nCol = pSelTab->nCol;
118272 pTable->aCol = pSelTab->aCol;
118273 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
118274 pSelTab->nCol = 0;
118275 pSelTab->aCol = 0;
118276 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
118278 pTable->nNVCol = pTable->nCol;
118282 pParse->eParseMode = eParseMode;
118286 pTable->pSchema->schemaFlags |= DB_UnresetViews;
118287 if( db->mallocFailed ){
118303 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
118318 ** root-page of a table or index in database iDb has changed from iFrom
118339 pDb = &db->aDb[iDb];
118340 pHash = &pDb->pSchema->tblHash;
118343 if( pTab->tnum==iFrom ){
118344 pTab->tnum = iTo;
118347 pHash = &pDb->pSchema->idxHash;
118350 if( pIdx->tnum==iFrom ){
118351 pIdx->tnum = iTo;
118358 ** Write code to erase the table with root-page iTable from database iDb.
118360 ** if a root-page of another table is moved by the btree-layer whilst
118361 ** erasing iTable (this can happen with an auto-vacuum database).
118371 ** is non-zero, then it is the root page number of a table moved to
118382 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
118390 ** in case a root-page belonging to another table is moved by the btree layer
118391 ** is also added (this can happen with an auto-vacuum database).
118394 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
118396 ** table and index root-pages in order, starting with the numerically
118397 ** largest root-page number. This guarantees that none of the root-pages
118405 ** and root page 5 happened to be the largest root-page number in the
118408 ** a free-list page.
118410 Pgno iTab = pTab->tnum;
118420 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
118421 Pgno iIdx = pIdx->tnum;
118422 assert( pIdx->pSchema==pTab->pSchema );
118430 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
118431 assert( iDb>=0 && iDb<pParse->db->nDb );
118449 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
118453 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
118467 sqlite3 *db = pParse->db;
118469 Db *pDb = &db->aDb[iDb];
118487 assert( pTrigger->pSchema==pTab->pSchema ||
118488 pTrigger->pSchema==db->aDb[1].pSchema );
118490 pTrigger = pTrigger->pNext;
118497 ** move as a result of the drop (can happen in auto-vacuum mode).
118499 if( pTab->tabFlags & TF_Autoincrement ){
118502 pDb->zDbSName, pTab->zName
118517 pDb->zDbSName, pTab->zName);
118526 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
118529 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
118535 ** Return TRUE if shadow tables should be read-only in the current
118540 if( (db->flags & SQLITE_Defensive)!=0
118541 && db->pVtabCtx==0
118542 && db->nVdbeExec==0
118555 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
118556 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
118557 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
118560 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
118563 if( pTab->tabFlags & TF_Eponymous ){
118576 sqlite3 *db = pParse->db;
118579 if( db->mallocFailed ){
118582 assert( pParse->nErr==0 );
118583 assert( pName->nSrc==1 );
118585 if( noErr ) db->suppressErr++;
118587 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
118588 if( noErr ) db->suppressErr--;
118592 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
118597 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118598 assert( iDb>=0 && iDb<db->nDb );
118610 const char *zDb = db->aDb[iDb].zDbSName;
118624 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
118633 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
118636 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
118642 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
118651 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
118655 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
118667 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
118688 ** under construction in the pParse->pNewTable field.
118700 sqlite3 *db = pParse->db;
118704 Table *p = pParse->pNewTable;
118713 int iCol = p->nCol-1;
118715 if( pToCol && pToCol->nExpr!=1 ){
118718 p->aCol[iCol].zCnName, pTo);
118722 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
118728 nCol = pFromCol->nExpr;
118730 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
118732 for(i=0; i<pToCol->nExpr; i++){
118733 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
118740 pFKey->pFrom = p;
118742 pFKey->pNextFrom = p->u.tab.pFKey;
118743 z = (char*)&pFKey->aCol[nCol];
118744 pFKey->zTo = z;
118748 memcpy(z, pTo->z, pTo->n);
118749 z[pTo->n] = 0;
118751 z += pTo->n+1;
118752 pFKey->nCol = nCol;
118754 pFKey->aCol[0].iFrom = p->nCol-1;
118758 for(j=0; j<p->nCol; j++){
118759 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
118760 pFKey->aCol[i].iFrom = j;
118764 if( j>=p->nCol ){
118767 pFromCol->a[i].zEName);
118771 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
118777 int n = sqlite3Strlen30(pToCol->a[i].zEName);
118778 pFKey->aCol[i].zCol = z;
118780 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
118782 memcpy(z, pToCol->a[i].zEName, n);
118787 pFKey->isDeferred = 0;
118788 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
118789 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
118791 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
118792 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
118793 pFKey->zTo, (void *)pFKey
118800 assert( pNextTo->pPrevTo==0 );
118801 pFKey->pNextTo = pNextTo;
118802 pNextTo->pPrevTo = pFKey;
118808 p->u.tab.pFKey = pFKey;
118829 if( (pTab = pParse->pNewTable)==0 ) return;
118831 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
118832 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
118833 pFKey->isDeferred = (u8)isDeferred;
118846 ** the root page number of the index is taken from pIndex->tnum.
118849 Table *pTab = pIndex->pTable; /* The table that is indexed */
118850 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
118851 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
118856 int iPartIdxLabel; /* Jump to this label to skip a row */
118860 sqlite3 *db = pParse->db; /* The database connection */
118861 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
118864 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
118865 db->aDb[iDb].zDbSName ) ){
118870 /* Require a write-lock on the table to perform this operation */
118871 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
118878 tnum = pIndex->tnum;
118881 assert( pKey!=0 || pParse->nErr );
118884 iSorter = pParse->nTab++;
118885 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
118911 pIndex->nKeyCol); VdbeCoverage(v);
118926 if( !pIndex->bAscKeyBug ){
118951 ** of 8-byte aligned space after the Index object and return a
118971 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
118972 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
118973 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
118974 p->aSortOrder = (u8*)pExtra;
118975 p->nColumn = nCol;
118976 p->nKeyCol = nCol - 1;
118985 ** pParse and return non-zero. Otherwise, return zero.
118990 for(i=0; i<pList->nExpr; i++){
118991 if( pList->a[i].fg.bNulls ){
118992 u8 sf = pList->a[i].fg.sortFlags;
119007 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
119008 ** as the table to be indexed. pParse->pNewTable is a table that is
119012 ** is a primary key or unique-constraint on the most recent column added
119019 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
119035 sqlite3 *db = pParse->db;
119045 assert( db->pParse==pParse );
119046 if( pParse->nErr ){
119049 assert( db->mallocFailed==0 );
119065 /* Use the two-part index name to determine the database
119072 assert( pName && pName->z );
119079 if( !db->init.busy ){
119081 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
119093 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
119094 assert( db->mallocFailed==0 || pTab==0 );
119096 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
119098 "cannot create a TEMP index on non-TEMP table \"%s\"",
119099 pTab->zName);
119106 pTab = pParse->pNewTable;
119108 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119110 pDb = &db->aDb[iDb];
119113 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
119114 && db->init.busy==0
119117 && sqlite3UserAuthTable(pTab->zName)==0
119120 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
119152 assert( pName->z!=0 );
119153 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119157 if( !db->init.busy ){
119163 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
119167 assert( !db->init.busy );
119177 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
119178 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
119195 const char *zDb = pDb->zDbSName;
119201 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
119213 Column *pCol = &pTab->aCol[pTab->nCol-1];
119214 pCol->colFlags |= COLFLAG_UNIQUE;
119215 sqlite3TokenInit(&prevCol, pCol->zCnName);
119219 assert( pList->nExpr==1 );
119223 if( pParse->nErr ) goto exit_create_index;
119229 for(i=0; i<pList->nExpr; i++){
119230 Expr *pExpr = pList->a[i].pExpr;
119232 if( pExpr->op==TK_COLLATE ){
119234 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
119242 nExtraCol = pPk ? pPk->nKeyCol : 1;
119243 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
119244 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
119246 if( db->mallocFailed ){
119249 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
119250 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
119251 pIndex->zName = zExtra;
119253 memcpy(pIndex->zName, zName, nName+1);
119254 pIndex->pTable = pTab;
119255 pIndex->onError = (u8)onError;
119256 pIndex->uniqNotNull = onError!=OE_None;
119257 pIndex->idxType = idxType;
119258 pIndex->pSchema = db->aDb[iDb].pSchema;
119259 pIndex->nKeyCol = pList->nExpr;
119262 pIndex->pPartIdxWhere = pPIWhere;
119269 if( pDb->pSchema->file_format>=4 ){
119270 sortOrderMask = -1; /* Honor DESC */
119278 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
119284 pListItem = pList->a;
119286 pIndex->aColExpr = pList;
119289 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
119290 Expr *pCExpr; /* The i-th index expression */
119291 int requestedSortOrder; /* ASC or DESC on the i-th expression */
119294 sqlite3StringToId(pListItem->pExpr);
119295 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
119296 if( pParse->nErr ) goto exit_create_index;
119297 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
119298 if( pCExpr->op!=TK_COLUMN ){
119299 if( pTab==pParse->pNewTable ){
119304 if( pIndex->aColExpr==0 ){
119305 pIndex->aColExpr = pList;
119309 pIndex->aiColumn[i] = XN_EXPR;
119310 pIndex->uniqNotNull = 0;
119312 j = pCExpr->iColumn;
119315 j = pTab->iPKey;
119317 if( pTab->aCol[j].notNull==0 ){
119318 pIndex->uniqNotNull = 0;
119320 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
119321 pIndex->bHasVCol = 1;
119324 pIndex->aiColumn[i] = (i16)j;
119327 if( pListItem->pExpr->op==TK_COLLATE ){
119329 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
119330 zColl = pListItem->pExpr->u.zToken;
119336 nExtra -= nColl;
119338 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
119341 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
119344 pIndex->azColl[i] = zColl;
119345 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
119346 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
119354 for(j=0; j<pPk->nKeyCol; j++){
119355 int x = pPk->aiColumn[j];
119357 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
119358 pIndex->nColumn--;
119360 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
119361 pIndex->aiColumn[i] = x;
119362 pIndex->azColl[i] = pPk->azColl[j];
119363 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
119367 assert( i==pIndex->nColumn );
119369 pIndex->aiColumn[i] = XN_ROWID;
119370 pIndex->azColl[i] = sqlite3StrBINARY;
119373 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
119378 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
119380 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
119381 pIndex->isCovering = 1;
119382 for(j=0; j<pTab->nCol; j++){
119383 if( j==pTab->iPKey ) continue;
119385 pIndex->isCovering = 0;
119390 if( pTab==pParse->pNewTable ){
119413 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119416 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
119419 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
119420 for(k=0; k<pIdx->nKeyCol; k++){
119423 assert( pIdx->aiColumn[k]>=0 );
119424 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
119425 z1 = pIdx->azColl[k];
119426 z2 = pIndex->azColl[k];
119429 if( k==pIdx->nKeyCol ){
119430 if( pIdx->onError!=pIndex->onError ){
119438 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
119442 if( pIdx->onError==OE_Default ){
119443 pIdx->onError = pIndex->onError;
119446 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
119448 pIndex->pNext = pParse->pNewIndex;
119449 pParse->pNewIndex = pIndex;
119460 ** in-memory database structures.
119462 assert( pParse->nErr==0 );
119463 if( db->init.busy ){
119466 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
119468 pIndex->tnum = db->init.newTnum;
119471 pParse->rc = SQLITE_CORRUPT_BKPT;
119475 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
119476 pIndex->zName, pIndex);
119482 db->mDbFlags |= DBFLAG_SchemaChange;
119501 int iMem = ++pParse->nMem;
119514 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
119522 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
119523 if( pName->z[n-1]==';' ) n--;
119526 onError==OE_None ? "" : " UNIQUE", n, pName->z);
119537 db->aDb[iDb].zDbSName,
119538 pIndex->zName,
119539 pTab->zName,
119546 ** to invalidate all pre-compiled statements.
119552 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
119556 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
119559 if( db->init.busy || pTblName==0 ){
119560 pIndex->pNext = pTab->pIndex;
119561 pTab->pIndex = pIndex;
119565 assert( pParse->pNewIndex==0 );
119566 pParse->pNewIndex = pIndex;
119580 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
119582 if( pThis->onError!=OE_Replace ) continue;
119583 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
119585 pThis->pNext = pNext->pNext;
119586 pNext->pNext = pThis;
119587 ppFrom = &pNext->pNext;
119595 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
119596 assert( pThis->onError!=OE_Replace
119597 || pThis->pNext==0
119598 || pThis->pNext->onError==OE_Replace );
119609 ** Fill the Index.aiRowEst[] array with default information - information
119619 ** aiRowEst[N]<=aiRowEst[N-1]
119629 LogEst *a = pIdx->aiRowLogEst;
119631 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
119635 assert( !pIdx->hasStat1 );
119641 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
119647 x = pIdx->pTable->nRowLogEst;
119650 pIdx->pTable->nRowLogEst = x = 99;
119652 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
119658 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
119663 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
119673 sqlite3 *db = pParse->db;
119676 if( db->mallocFailed ){
119679 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
119680 assert( pName->nSrc==1 );
119684 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
119687 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
119689 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
119692 pParse->checkSchema = 1;
119695 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
119700 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
119704 Table *pTab = pIndex->pTable;
119705 const char *zDb = db->aDb[iDb].zDbSName;
119711 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
119723 db->aDb[iDb].zDbSName, pIndex->zName
119725 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
119727 destroyRootPage(pParse, pIndex->tnum, iDb);
119728 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
119741 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
119749 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
119761 if( (n & (n-1))==0 ){
119765 *pIdx = -1;
119783 sqlite3 *db = pParse->db;
119791 sizeof(IdList) + pList->nId*sizeof(pList->a));
119798 i = pList->nId++;
119799 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
119800 if( IN_RENAME_OBJECT && pList->a[i].zName ){
119801 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
119812 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
119813 for(i=0; i<pList->nId; i++){
119814 sqlite3DbFree(db, pList->a[i].zName);
119820 ** Return the index in pList of the identifier named zId. Return -1
119826 for(i=0; i<pList->nId; i++){
119827 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
119829 return -1;
119838 ** appropriate for small and memory-limited applications.
119867 int nExtra, /* Number of new slots to add to pSrc->a[] */
119868 int iStart /* Index in pSrc->a[] of first new slot */
119876 assert( iStart<=pSrc->nSrc );
119879 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
119881 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
119882 sqlite3 *db = pParse->db;
119884 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
119891 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
119893 assert( db->mallocFailed );
119897 pSrc->nAlloc = nAlloc;
119902 for(i=pSrc->nSrc-1; i>=iStart; i--){
119903 pSrc->a[i+nExtra] = pSrc->a[i];
119905 pSrc->nSrc += nExtra;
119908 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
119910 pSrc->a[i].iCursor = -1;
119963 assert( pParse->db!=0 );
119964 db = pParse->db;
119966 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
119968 pList->nAlloc = 1;
119969 pList->nSrc = 1;
119970 memset(&pList->a[0], 0, sizeof(pList->a[0]));
119971 pList->a[0].iCursor = -1;
119973 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
119981 pItem = &pList->a[pList->nSrc-1];
119982 if( pDatabase && pDatabase->z==0 ){
119986 pItem->zName = sqlite3NameFromToken(db, pDatabase);
119987 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
119989 pItem->zName = sqlite3NameFromToken(db, pTable);
119990 pItem->zDatabase = 0;
120001 assert( pList || pParse->db->mallocFailed );
120003 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
120004 if( pItem->iCursor>=0 ) continue;
120005 pItem->iCursor = pParse->nTab++;
120006 if( pItem->pSelect ){
120007 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
120020 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120021 if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
120022 sqlite3DbFree(db, pItem->zName);
120023 if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
120024 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120025 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120026 sqlite3DeleteTable(db, pItem->pTab);
120027 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120028 if( pItem->fg.isUsing ){
120029 sqlite3IdListDelete(db, pItem->u3.pUsing);
120030 }else if( pItem->u3.pOn ){
120031 sqlite3ExprDelete(db, pItem->u3.pOn);
120043 ** pDatabase is NULL if the database name qualifier is missing - the
120058 Token *pAlias, /* The right-hand side of the AS subexpression */
120063 sqlite3 *db = pParse->db;
120064 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
120066 (pOnUsing->pOn ? "ON" : "USING")
120074 assert( p->nSrc>0 );
120075 pItem = &p->a[p->nSrc-1];
120077 assert( pItem->zName==0 || pDatabase!=0 );
120078 if( IN_RENAME_OBJECT && pItem->zName ){
120079 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
120080 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
120083 if( pAlias->n ){
120084 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
120087 pItem->pSelect = pSubquery;
120088 if( pSubquery->selFlags & SF_NestedFrom ){
120089 pItem->fg.isNestedFrom = 1;
120092 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
120093 assert( pItem->fg.isUsing==0 );
120095 pItem->u3.pOn = 0;
120096 }else if( pOnUsing->pUsing ){
120097 pItem->fg.isUsing = 1;
120098 pItem->u3.pUsing = pOnUsing->pUsing;
120100 pItem->u3.pOn = pOnUsing->pOn;
120113 ** element of the source-list passed as the second argument.
120117 if( p && pIndexedBy->n>0 ){
120119 assert( p->nSrc>0 );
120120 pItem = &p->a[p->nSrc-1];
120121 assert( pItem->fg.notIndexed==0 );
120122 assert( pItem->fg.isIndexedBy==0 );
120123 assert( pItem->fg.isTabFunc==0 );
120124 if( pIndexedBy->n==1 && !pIndexedBy->z ){
120127 pItem->fg.notIndexed = 1;
120129 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
120130 pItem->fg.isIndexedBy = 1;
120131 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
120142 assert( p1 && p1->nSrc==1 );
120144 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
120146 sqlite3SrcListDelete(pParse->db, p2);
120149 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
120150 sqlite3DbFree(pParse->db, p2);
120151 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
120159 ** table-valued-function.
120163 SrcItem *pItem = &p->a[p->nSrc-1];
120164 assert( pItem->fg.notIndexed==0 );
120165 assert( pItem->fg.isIndexedBy==0 );
120166 assert( pItem->fg.isTabFunc==0 );
120167 pItem->u1.pFuncArg = pList;
120168 pItem->fg.isTabFunc = 1;
120170 sqlite3ExprListDelete(pParse->db, pList);
120186 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
120191 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
120198 if( p && p->nSrc>1 ){
120199 int i = p->nSrc-1;
120202 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
120203 }while( (--i)>0 );
120204 p->a[0].fg.jointype = 0;
120209 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
120210 i--;
120213 p->a[i].fg.jointype |= JT_LTORJ;
120214 }while( (--i)>=0 );
120228 db = pParse->db;
120236 for(i=0; i<db->nDb; i++){
120238 Btree *pBt = db->aDb[i].pBt;
120263 assert( pParse->db!=0 );
120281 char *zName = sqlite3NameFromToken(pParse->db, pName);
120289 sqlite3DbFree(pParse->db, zName);
120301 sqlite3 *db = pParse->db;
120302 if( db->aDb[1].pBt==0 && !pParse->explain ){
120312 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
120316 pParse->rc = rc;
120319 db->aDb[1].pBt = pBt;
120320 assert( db->aDb[1].pSchema );
120321 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
120332 ** will occur at the end of the top-level VDBE and will be generated
120336 assert( iDb>=0 && iDb<pToplevel->db->nDb );
120337 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
120339 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
120340 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
120341 DbMaskSet(pToplevel->cookieMask, iDb);
120357 sqlite3 *db = pParse->db;
120359 for(i=0; i<db->nDb; i++){
120360 Db *pDb = &db->aDb[i];
120361 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
120383 DbMaskSet(pToplevel->writeMask, iDb);
120384 pToplevel->isMultiWrite |= setStatement;
120396 pToplevel->isMultiWrite = 1;
120417 pToplevel->mayAbort = 1;
120434 assert( pParse->pVdbe!=0 );
120436 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
120455 Table *pTab = pIdx->pTable;
120457 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
120458 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
120459 if( pIdx->aColExpr ){
120460 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
120462 for(j=0; j<pIdx->nKeyCol; j++){
120464 assert( pIdx->aiColumn[j]>=0 );
120465 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
120467 sqlite3_str_appendall(&errMsg, pTab->zName);
120481 ** Code an OP_Halt due to non-unique rowid.
120486 Table *pTab /* The table with the non-unique rowid */
120490 if( pTab->iPKey>=0 ){
120491 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
120492 pTab->aCol[pTab->iPKey].zCnName);
120495 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
120510 for(i=0; i<pIndex->nColumn; i++){
120511 const char *z = pIndex->azColl[i];
120512 assert( z!=0 || pIndex->aiColumn[i]<0 );
120513 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
120530 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
120532 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
120534 sqlite3RefillIndex(pParse, pIndex, -1);
120550 sqlite3 *db = pParse->db; /* The database connection */
120555 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
120557 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
120568 ** REINDEX -- 1
120569 ** REINDEX <collation> -- 2
120570 ** REINDEX ?<database>.?<tablename> -- 3
120571 ** REINDEX ?<database>.?<indexname> -- 4
120586 sqlite3 *db = pParse->db; /* The database connection */
120598 }else if( NEVER(pName2==0) || pName2->z==0 ){
120600 assert( pName1->z );
120601 zColl = sqlite3NameFromToken(pParse->db, pName1);
120615 zDb = db->aDb[iDb].zDbSName;
120626 sqlite3RefillIndex(pParse, pIndex, -1);
120641 int nCol = pIdx->nColumn;
120642 int nKey = pIdx->nKeyCol;
120644 if( pParse->nErr ) return 0;
120645 if( pIdx->uniqNotNull ){
120646 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
120648 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
120653 const char *zColl = pIdx->azColl[i];
120654 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
120656 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
120657 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
120659 if( pParse->nErr ){
120660 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
120661 if( pIdx->bNoQuery==0 ){
120666 ** the missing index using the collation-needed callback. For
120669 pIdx->bNoQuery = 1;
120670 pParse->rc = SQLITE_ERROR_RETRY;
120685 Token *pName, /* Name of the common-table */
120691 sqlite3 *db = pParse->db;
120694 assert( pNew!=0 || db->mallocFailed );
120696 if( db->mallocFailed ){
120700 pNew->pSelect = pQuery;
120701 pNew->pCols = pArglist;
120702 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
120703 pNew->eM10d = eM10d;
120714 sqlite3ExprListDelete(db, pCte->pCols);
120715 sqlite3SelectDelete(db, pCte->pSelect);
120716 sqlite3DbFree(db, pCte->zName);
120739 sqlite3 *db = pParse->db;
120749 zName = pCte->zName;
120752 for(i=0; i<pWith->nCte; i++){
120753 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
120760 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
120765 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
120767 if( db->mallocFailed ){
120771 pNew->a[pNew->nCte++] = *pCte;
120784 for(i=0; i<pWith->nCte; i++){
120785 cteClear(db, &pWith->a[i]);
120817 assert( !db->xCollNeeded || !db->xCollNeeded16 );
120818 if( db->xCollNeeded ){
120821 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
120825 if( db->xCollNeeded16 ){
120828 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
120831 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
120842 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
120847 char *z = pColl->zName;
120852 if( pColl2->xCmp!=0 ){
120854 pColl->xDel = 0; /* Do not copy the destructor */
120873 if( pColl && pColl->xCmp==0 ){
120874 const char *zName = pColl->zName;
120875 sqlite3 *db = pParse->db;
120894 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
120906 pColl = sqlite3HashFind(&db->aCollSeq, zName);
120920 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
120938 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
120963 if( pColl ) pColl += enc-1;
120965 pColl = db->pDfltColl;
120976 db->enc = enc;
120977 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
120980 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
121004 sqlite3 *db = pParse->db;
121010 if( !p || !p->xCmp ){
121017 if( p && !p->xCmp && synthCollSeq(db, p) ){
121020 assert( !p || p->xCmp );
121023 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
121049 sqlite3 *db = pParse->db;
121051 u8 initbusy = db->init.busy;
121055 if( !initbusy && (!pColl || !pColl->xCmp) ){
121068 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
121069 ** is also -1. In other words, we are searching for a function that
121072 ** If nArg is -2 that means that we are searching for any function
121082 ** 4: UTF8/16 conversion required - argument count matches exactly
121083 ** 5: UTF16 byte order conversion required - argument count matches exactly
121086 ** If nArg==(-2) then any function with a non-null xSFunc is
121088 ** a non-match.
121093 int nArg, /* Desired number of arguments. (-1)==any */
121097 assert( p->nArg>=-1 );
121100 if( p->nArg!=nArg ){
121101 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
121102 if( p->nArg>=0 ) return 0;
121107 if( p->nArg==nArg ){
121114 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
121116 }else if( (enc & p->funcFlags & 2)!=0 ){
121132 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
121133 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
121134 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
121157 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
121158 aDef[i].pNext = pOther->pNext;
121159 pOther->pNext = &aDef[i];
121172 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
121180 ** If nArg is -2, then the first valid function found is returned. A
121181 ** function is valid if xSFunc is non-zero. The nArg==(-2)
121183 ** of arguments. If nArg is -2, then createFlag must be 0.
121191 const char *zName, /* Name of the function. zero-terminated */
121192 int nArg, /* Number of arguments. -1 means any number */
121202 assert( nArg>=(-2) );
121203 assert( nArg>=(-1) || createFlag==0 );
121206 /* First search for a match amongst the application-defined functions.
121208 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
121215 p = p->pNext;
121218 /* If no match is found, search the built-in functions.
121220 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
121221 ** functions even if a prior app-defined function was found. And give
121222 ** priority to built-in functions.
121227 ** new function. But the FuncDefs for built-in functions are read-only.
121228 ** So we must not search for built-ins when creating a new function.
121230 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
121240 p = p->pNext;
121252 pBest->zName = (const char*)&pBest[1];
121253 pBest->nArg = (u16)nArg;
121254 pBest->funcFlags = enc;
121256 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
121257 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
121263 pBest->pNext = pOther;
121267 if( pBest && (pBest->xSFunc || createFlag) ){
121287 temp1 = pSchema->tblHash;
121288 temp2 = pSchema->trigHash;
121289 sqlite3HashInit(&pSchema->trigHash);
121290 sqlite3HashClear(&pSchema->idxHash);
121295 sqlite3HashInit(&pSchema->tblHash);
121301 sqlite3HashClear(&pSchema->fkeyHash);
121302 pSchema->pSeqTab = 0;
121303 if( pSchema->schemaFlags & DB_SchemaLoaded ){
121304 pSchema->iGeneration++;
121306 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
121322 }else if ( 0==p->file_format ){
121323 sqlite3HashInit(&p->tblHash);
121324 sqlite3HashInit(&p->idxHash);
121325 sqlite3HashInit(&p->trigHash);
121326 sqlite3HashInit(&p->fkeyHash);
121327 p->enc = SQLITE_UTF8;
121360 ** pSrc->a[0].pTab Pointer to the Table object
121361 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
121365 SrcItem *pItem = pSrc->a;
121367 assert( pItem && pSrc->nSrc>=1 );
121369 sqlite3DeleteTable(pParse->db, pItem->pTab);
121370 pItem->pTab = pTab;
121372 pTab->nTabRef++;
121373 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
121380 /* Generate byte-code that will report the number of rows modified
121390 /* Return true if table pTab is read-only.
121392 ** A table is read-only if any of the following are true:
121403 ** is for a top-level SQL statement.
121408 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
121410 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
121411 db = pParse->db;
121412 if( (pTab->tabFlags & TF_Readonly)!=0 ){
121413 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
121415 assert( pTab->tabFlags & TF_Shadow );
121426 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121431 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
121456 sqlite3 *db = pParse->db;
121457 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
121461 assert( pFrom->nSrc==1 );
121462 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
121463 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
121464 assert( pFrom->a[0].fg.isUsing==0 );
121465 assert( pFrom->a[0].u3.pOn==0 );
121486 SrcList *pSrc, /* the FROM clause -- which tables to scan */
121492 sqlite3 *db = pParse->db;
121504 sqlite3ExprDelete(pParse->db, pWhere);
121505 sqlite3ExprListDelete(pParse->db, pOrderBy);
121525 pTab = pSrc->a[0].pTab;
121533 if( pPk->nKeyCol==1 ){
121534 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
121539 for(i=0; i<pPk->nKeyCol; i++){
121540 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
121545 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
121552 pSrc->a[0].pTab = 0;
121554 pSrc->a[0].pTab = pTab;
121555 if( pSrc->a[0].fg.isIndexedBy ){
121556 assert( pSrc->a[0].fg.isCte==0 );
121557 pSrc->a[0].u2.pIBIndex = 0;
121558 pSrc->a[0].fg.isIndexedBy = 0;
121559 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
121560 }else if( pSrc->a[0].fg.isCte ){
121561 pSrc->a[0].u2.pCteUse->nUse++;
121628 db = pParse->db;
121629 assert( db->pParse==pParse );
121630 if( pParse->nErr ){
121633 assert( db->mallocFailed==0 );
121634 assert( pTabList->nSrc==1 );
121663 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
121687 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121688 assert( iDb<db->nDb );
121689 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
121690 db->aDb[iDb].zDbSName);
121699 assert( pTabList->nSrc==1 );
121700 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
121701 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
121702 pParse->nTab++;
121708 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
121717 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
121746 if( (db->flags & SQLITE_CountRows)!=0
121747 && !pParse->nested
121748 && !pParse->pTriggerTab
121749 && !pParse->bReturning
121751 memCnt = ++pParse->nMem;
121762 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
121772 && db->xPreUpdateCallback==0
121776 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121778 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
121779 pTab->zName, P4_STATIC);
121781 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121782 assert( pIdx->pSchema==pTab->pSchema );
121783 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
121785 sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
121798 iRowSet = ++pParse->nMem;
121805 nPk = pPk->nKeyCol;
121806 iPk = pParse->nMem+1;
121807 pParse->nMem += nPk;
121808 iEphCur = pParse->nTab++;
121817 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
121818 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
121819 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
121839 assert( pPk->aiColumn[i]>=0 );
121841 pPk->aiColumn[i], iPk+i);
121845 iKey = ++pParse->nMem;
121846 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
121850 /* For ONEPASS, no need to store the rowid/primary-key. There is only
121861 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
121862 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
121868 iKey = ++pParse->nMem;
121871 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
121901 /* Set up a loop over the rowids/primary-keys that were found in the
121902 ** where-clause loop above.
121906 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
121935 pParse->isMultiWrite = 0;
121943 int count = (pParse->nested==0); /* True to count changes */
121948 /* End of the loop over all rowids/primary-keys. */
121959 } /* End non-truncate path */
121965 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
122011 ** cursor number iIdxCur+i for the i-th index.
122047 u8 count, /* If non-zero, increment the row change counter */
122052 Vdbe *v = pParse->pVdbe; /* Vdbe */
122054 int iLabel; /* Label resolved to end of generated code */
122086 iOld = pParse->nMem+1;
122087 pParse->nMem += (1 + pTab->nCol);
122089 /* Populate the OLD.* pseudo-table register array. These values will be
122092 for(iCol=0; iCol<pTab->nCol; iCol++){
122120 iIdxNoSeek = -1;
122133 ** If variable 'count' is non-zero, then this OP_Delete instruction should
122134 ** invoke the update-hook. The pre-update-hook, on the other hand should
122136 ** the update-hook is not invoked for rows removed by REPLACE, but the
122137 ** pre-update-hook is.
122143 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
122185 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
122186 ** index is the 0-th index.)
122200 int r1 = -1; /* Register holding an index key */
122207 v = pParse->pVdbe;
122209 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
122214 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
122218 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
122237 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
122238 ** to that label if pIdx is a partial index that should be skipped.
122239 ** The label should be resolved using sqlite3ResolvePartIdxLabel().
122242 ** will be set to zero which is an empty label that is ignored by
122262 int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
122266 Vdbe *v = pParse->pVdbe;
122272 if( pIdx->pPartIdxWhere ){
122274 pParse->iSelfTab = iDataCur + 1;
122275 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
122277 pParse->iSelfTab = 0;
122278 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
122284 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
122286 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
122289 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
122290 && pPrior->aiColumn[j]!=XN_EXPR
122296 if( pIdx->aiColumn[j]>=0 ){
122314 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
122316 ** resolve that label.
122320 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
122337 ** This file contains the C-language implementations for many of the SQL
122354 assert( context->pVdbe!=0 );
122355 pOp = &context->pVdbe->aOp[context->iOp-1];
122356 assert( pOp->opcode==OP_CollSeq );
122357 assert( pOp->p4type==P4_COLLSEQ );
122358 return pOp->p4.pColl;
122366 assert( context->isError<=0 );
122367 context->isError = -1;
122368 context->skipFlag = 1;
122372 ** Implementation of the non-aggregate min() and max() functions
122385 mask = sqlite3_user_data(context)==0 ? 0 : -1;
122388 assert( mask==-1 || mask==0 );
122410 int i = sqlite3_value_type(argv[0]) - 1;
122418 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
122422 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
122467 sqlite3_result_int(context, (int)(z-z0));
122480 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
122491 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
122493 ** equivalent positive 64-bit two complement value. */
122494 sqlite3_result_error(context, "integer overflow", -1);
122497 iVal = -iVal;
122503 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
122510 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
122514 if( rVal<0 ) rVal = -rVal;
122581 nHaystack--;
122612 x.nArg = argc-1;
122615 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
122628 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
122629 ** of x. If x is text, then we actually count UTF-8 characters.
122673 ** as substr(X,1,N) - it returns the first N characters of X. This
122674 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
122675 ** from 2009-02-02 for compatibility of applications that exploited the
122682 p2 = -p2;
122686 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
122696 p1--;
122698 p2--;
122701 p1 -= p2;
122711 p1--;
122713 for(z2=z; *z2 && p2; p2--){
122716 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
122720 p2 = len-p1;
122744 /* If Y==0 and X will fit in a 64-bit int,
122748 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
122751 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
122776 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
122777 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
122778 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
122840 #define noopFunc versionFunc /* Substitute function - never called */
122855 ** (or -9223372036854775808) since when you do abs() of that
122860 ** therefore be no less than -9223372036854775807.
122862 r = -(r & LARGEST_INT64);
122902 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
122911 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
122936 /* IMP: R-11217-42568 This function is a wrapper around the
122942 ** A structure defining how to do GLOB-style comparisons.
122965 /* The correct SQL-92 behavior is for the LIKE operator to ignore
122980 ** Compare two UTF-8 strings for equality where the first string is
123000 ** range of characters can be specified using '-'. Example:
123001 ** "[a-z]" matches any single lower-case letter. To match a '-', make
123024 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
123025 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
123026 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
123043 if( pInfo->matchSet==0 ){
123049 assert( matchOther<0x80 ); /* '[' is a single-byte character */
123051 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
123064 ** For a case-insensitive search, set variable cx to be the same as
123097 if( pInfo->matchSet==0 ){
123117 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
123148 ** non-zero if there is no match.
123155 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123156 ** a miss - like strcmp().
123174 ** the build-in LIKE operator. The first argument to the function is the
123212 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
123213 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
123214 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
123215 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
123219 /* The escape character string must consist of a single UTF-8 character.
123224 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
123226 "ESCAPE expression must be a single character", -1);
123230 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
123233 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
123234 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
123237 escape = pInfo->matchSet;
123277 /* IMP: R-48699-48617 This function is an SQL wrapper around the
123278 ** sqlite3_libversion() C-interface. */
123279 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
123293 /* IMP: R-24470-31136 This function is an SQL wrapper around the
123295 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
123301 ** its side-effects.
123327 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
123351 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
123355 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
123359 /* Array for converting from half-bytes (nybbles) into ASCII hex
123374 assert( pStr!=0 && pStr->nChar==0 );
123384 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
123401 if( pStr->accError==0 ){
123402 char *zText = pStr->zText;
123412 pStr->nChar = nBlob*2 + 3;
123434 ** Strings are surrounded by single-quotes with escapes on interior quotes
123444 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
123455 ** The unicode() function. Return the integer unicode code-point value
123507 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
123540 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
123553 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
123592 || sqlite3_context_db_handle(context)->mallocFailed );
123612 loopLimit = nStr - nPattern;
123619 nOut += nRep - nPattern;
123620 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
123621 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
123622 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
123628 if( (cntExpand&(cntExpand-1))==0 ){
123633 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
123643 i += nPattern-1;
123646 assert( j+nStr-i+1<=nOut );
123647 memcpy(&zOut[j], &zStr[i], nStr-i);
123648 j += nStr - i;
123703 aLen[nChar] = (unsigned)(z - azChar[nChar]);
123718 nIn -= len;
123726 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
123729 nIn -= len;
123744 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
123745 ** When the "sqlite3" command-line shell is built using this functionality,
123747 ** involving application-defined functions to be examined in a generic
123755 /* no-op */
123760 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
123761 ** is only available if the SQLITE_SOUNDEX compile-time option is used
123768 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
123813 /* IMP: R-64894-50321 The string "?000" is returned if the argument
123822 ** A function that loads a shared-library extension then returns NULL.
123833 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
123834 sqlite3_result_error(context, "not authorized", -1);
123844 sqlite3_result_error(context, zErrMsg, -1);
123861 u8 approx; /* True if non-integer value was input to the sum */
123882 p->cnt++;
123885 p->rSum += v;
123886 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
123887 p->approx = p->overflow = 1;
123890 p->rSum += sqlite3_value_double(argv[0]);
123891 p->approx = 1;
123903 /* p is always non-NULL because sumStep() will have been called first
123906 assert( p->cnt>0 );
123907 p->cnt--;
123908 assert( type==SQLITE_INTEGER || p->approx );
123909 if( type==SQLITE_INTEGER && p->approx==0 ){
123911 p->rSum -= v;
123912 p->iSum -= v;
123914 p->rSum -= sqlite3_value_double(argv[0]);
123924 if( p && p->cnt>0 ){
123925 if( p->overflow ){
123926 sqlite3_result_error(context,"integer overflow",-1);
123927 }else if( p->approx ){
123928 sqlite3_result_double(context, p->rSum);
123930 sqlite3_result_int64(context, p->iSum);
123937 if( p && p->cnt>0 ){
123938 sqlite3_result_double(context, p->rSum/(double)p->cnt);
123945 sqlite3_result_double(context, p ? p->rSum : (double)0);
123967 p->n++;
123974 ** expressed as a 32-bit integer. */
123975 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
123976 || p->n==sqlite3_aggregate_count(context) );
123982 sqlite3_result_int64(context, p ? p->n : 0);
123988 /* p is always non-NULL since countStep() will have been called first */
123990 p->n--;
123992 p->bInverse = 1;
124016 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
124017 }else if( pBest->flags ){
124024 ** sqlite3_user_data() function returns (void *)-1. For min() it
124037 pBest->db = sqlite3_context_db_handle(context);
124045 if( pRes->flags ){
124076 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
124078 ** (Hence, its slots in use number nAccum-1 between method calls.)
124099 int firstTerm = pGCC->str.mxAlloc==0;
124100 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
124103 sqlite3_str_appendchar(&pGCC->str, 1, ',');
124107 pGCC->nFirstSepLength = 1;
124114 sqlite3_str_append(&pGCC->str, zSep, nSep);
124120 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
124121 int *pnsl = pGCC->pnSepLengths;
124124 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
124126 int i = 0, nA = pGCC->nAccum-1;
124127 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
124130 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
124133 if( ALWAYS(pGCC->nAccum>0) ){
124134 pnsl[pGCC->nAccum-1] = nSep;
124136 pGCC->pnSepLengths = pnsl;
124138 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
124145 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
124147 pGCC->nAccum += 1;
124151 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
124166 /* pGCC is always non-NULL since groupConcatStep() will have always
124174 pGCC->nAccum -= 1;
124175 if( pGCC->pnSepLengths!=0 ){
124176 assert(pGCC->nAccum >= 0);
124177 if( pGCC->nAccum>0 ){
124178 nVS += *pGCC->pnSepLengths;
124179 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
124180 (pGCC->nAccum-1)*sizeof(int));
124183 /* If removing single accumulated string, harmlessly over-do. */
124184 nVS += pGCC->nFirstSepLength;
124186 if( nVS>=(int)pGCC->str.nChar ){
124187 pGCC->str.nChar = 0;
124189 pGCC->str.nChar -= nVS;
124190 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
124192 if( pGCC->str.nChar==0 ){
124193 pGCC->str.mxAlloc = 0;
124194 sqlite3_free(pGCC->pnSepLengths);
124195 pGCC->pnSepLengths = 0;
124206 sqlite3ResultStrAccum(context, &pGCC->str);
124208 sqlite3_free(pGCC->pnSepLengths);
124217 StrAccum *pAccum = &pGCC->str;
124218 if( pAccum->accError==SQLITE_TOOBIG ){
124220 }else if( pAccum->accError==SQLITE_NOMEM ){
124224 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
124233 ** This routine does per-connection function registration. Most
124234 ** of the built-in functions above are part of the global function set.
124246 ** Re-register the built-in LIKE functions. The caseSensitive
124262 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
124263 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
124271 ** LIKE-style function then return FALSE.
124287 assert( pExpr->op==TK_FUNCTION );
124289 if( !pExpr->x.pList ){
124292 nExpr = pExpr->x.pList->nExpr;
124294 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
124298 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
124306 memcpy(aWc, pDef->pUserData, 3);
124314 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
124316 if( pEscape->op!=TK_STRING ) return 0;
124318 zEscape = pEscape->u.zToken;
124325 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
124341 /* Extra math functions that require linking with -lm
124387 ** ln(X) - natural logarithm
124388 ** log(X) - log X base 10
124389 ** log10(X) - log X base 10
124390 ** log(B,X) - log X base B
124446 ** Implementation of 1-argument SQL math functions:
124448 ** exp(X) - Compute e to the X-th power
124468 ** Implementation of 2-argument SQL math functions:
124470 ** power(X,Y) - Compute X to the Y-th power
124493 ** Implementation of 0-argument pi() function.
124521 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
124526 ** to the global function hash table. This occurs at start-time (as
124537 ** FuncDef.pHash elements at start-time. The elements of this array
124538 ** are read-only after initialization is complete.
124577 FUNCTION(min, -1, 0, 1, minmaxFunc ),
124581 FUNCTION(max, -1, 1, 1, minmaxFunc ),
124589 FUNCTION(printf, -1, 0, 0, printfFunc ),
124590 FUNCTION(format, -1, 0, 0, printfFunc ),
124592 FUNCTION(char, -1, 0, 0, charFunc ),
124640 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
124681 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
124692 #if 0 /* Enable to print out how the built-in functions are hashed */
124697 printf("FUNC-HASH %02d:", i);
124698 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
124699 int n = sqlite3Strlen30(p->zName);
124700 int h = p->zName[0] + n;
124701 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
124702 printf(" %s(%d)", p->zName, h);
124732 ** --------------------------
124814 ** ---------------
124816 ** Before coding an UPDATE or DELETE row operation, the code-generator
124821 ** accessed). No information is required by the code-generator before
124825 ** sqlite3FkRequired() - Test to see if FK processing is required.
124826 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
124830 ** --------------------------------------
124832 ** sqlite3FkCheck() - Check for foreign key violations.
124833 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
124834 ** sqlite3FkDelete() - Delete an FKey structure.
124839 ** -----------------------
124869 ** constraint to the parent table column stored in the left-most column
124871 ** child table column that corresponds to the second left-most column of
124890 ** then non-zero is returned, and a "foreign key mismatch" error loaded
124891 ** into pParse. If an OOM error occurs, non-zero is returned and the
124892 ** pParse->db->mallocFailed flag is set.
124903 int nCol = pFKey->nCol; /* Number of columns in parent key */
124904 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
124911 /* If this is a non-composite (single column) foreign key, check if it
124917 ** Non-composite foreign keys do not require the aiCol array.
124927 if( pParent->iPKey>=0 ){
124929 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
124935 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
124940 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
124941 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
124953 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
124958 /* If zKey is non-NULL, then this foreign key was declared to
124964 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
124973 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
124975 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
124977 zIdxCol = pParent->aCol[iCol].zCnName;
124979 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
124980 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
124992 if( !pParse->disableTriggers ){
124994 "foreign key mismatch - \"%w\" referencing \"%w\"",
124995 pFKey->pFrom->zName, pFKey->zTo);
124997 sqlite3DbFree(pParse->db, aiCol);
125009 ** affected - once to "delete" the old row, and then again to "insert" the
125019 ** --------------------------------------------------------------------------
125044 int iCur = pParse->nTab - 1; /* Cursor number to use */
125048 (!pFKey->isDeferred
125049 && !(pParse->db->flags & SQLITE_DeferFKs)
125050 && !pParse->pToplevel
125051 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
125061 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
125064 for(i=0; i<pFKey->nCol; i++){
125065 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
125082 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
125087 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125089 ** increment the constraint-counter. */
125090 if( pTab==pFKey->pFrom && nIncr==1 ){
125098 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
125102 int nCol = pFKey->nCol;
125105 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
125109 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
125114 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125116 ** increment the constraint-counter.
125118 ** If any of the parent-key values are NULL, then the row cannot match
125120 ** of the parent-key values are NULL (at this point it is known that
125123 if( pTab==pFKey->pFrom && nIncr==1 ){
125126 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
125129 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
125130 pIdx->aiColumn[i]);
125131 assert( pIdx->aiColumn[i]>=0 );
125132 assert( aiCol[i]!=pTab->iPKey );
125133 if( pIdx->aiColumn[i]==pTab->iPKey ){
125144 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
125151 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
125152 && !pParse->pToplevel
125153 && !pParse->isMultiWrite
125163 if( nIncr>0 && pFKey->isDeferred==0 ){
125166 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
125191 sqlite3 *db = pParse->db;
125195 if( iCol>=0 && iCol!=pTab->iPKey ){
125196 pCol = &pTab->aCol[iCol];
125197 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
125198 pExpr->affExpr = pCol->affinity;
125200 if( zColl==0 ) zColl = db->pDfltColl->zName;
125203 pExpr->iTable = regBase;
125204 pExpr->affExpr = SQLITE_AFF_INTEGER;
125223 pExpr->y.pTab = pTab;
125224 pExpr->iTable = iCursor;
125225 pExpr->iColumn = iCol;
125234 ** code for an SQL UPDATE operation, this function may be called twice -
125237 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
125246 ** --------------------------------------------------------------------------
125268 sqlite3 *db = pParse->db; /* Database handle */
125276 assert( pIdx==0 || pIdx->pTable==pTab );
125277 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
125278 assert( pIdx!=0 || pFKey->nCol==1 );
125282 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
125288 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
125294 for(i=0; i<pFKey->nCol; i++){
125301 iCol = pIdx ? pIdx->aiColumn[i] : -1;
125303 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125305 zCol = pFKey->pFrom->aCol[iCol].zCnName;
125325 if( pTab==pFKey->pFrom && nIncr>0 ){
125330 pLeft = exprTableRegister(pParse, pTab, regData, -1);
125331 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
125336 for(i=0; i<pIdx->nKeyCol; i++){
125337 i16 iCol = pIdx->aiColumn[i];
125340 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
125358 if( pParse->nErr==0 ){
125360 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
125388 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
125394 ** and all of its sub-components.
125396 ** The Trigger structure or any of its sub-components may be allocated from
125401 TriggerStep *pStep = p->step_list;
125402 sqlite3ExprDelete(dbMem, pStep->pWhere);
125403 sqlite3ExprListDelete(dbMem, pStep->pExprList);
125404 sqlite3SelectDelete(dbMem, pStep->pSelect);
125405 sqlite3ExprDelete(dbMem, p->pWhen);
125417 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
125422 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125423 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
125424 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
125447 sqlite3 *db = pParse->db;
125448 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
125461 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125462 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
125469 pParse->disableTriggers = 1;
125471 pParse->disableTriggers = 0;
125482 if( (db->flags & SQLITE_DeferFKs)==0 ){
125503 ** is set to -1). If the rowid column is modified by the UPDATE statement
125504 ** the bChngRowid argument is non-zero.
125516 for(i=0; i<p->nCol; i++){
125517 int iChildKey = p->aCol[i].iFrom;
125519 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
125530 ** is set to -1). If the rowid column is modified by the UPDATE statement
125531 ** the bChngRowid argument is non-zero.
125543 for(i=0; i<p->nCol; i++){
125544 char *zKey = p->aCol[i].zCol;
125546 for(iKey=0; iKey<pTab->nCol; iKey++){
125547 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
125548 Column *pCol = &pTab->aCol[iKey];
125550 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
125551 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
125567 if( pTop->pTriggerPrg ){
125568 Trigger *p = pTop->pTriggerPrg->pTrigger;
125569 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
125570 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
125584 ** first register in an array of (pTab->nCol+1) registers containing the
125590 ** first register of an array of (pTab->nCol+1) registers containing the new
125606 sqlite3 *db = pParse->db; /* Database handle */
125610 int isIgnoreErrors = pParse->disableTriggers;
125612 /* Exactly one of regOld and regNew should be non-zero. */
125615 /* If foreign-keys are disabled, this function is a no-op. */
125616 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
125619 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125620 zDb = db->aDb[iDb].zDbSName;
125624 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125634 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
125644 if( pParse->disableTriggers ){
125645 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
125647 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
125651 if( !isIgnoreErrors || db->mallocFailed ) return;
125658 ** FK counter for each row of the current table with non-NULL keys.
125661 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
125662 for(i=0; i<pFKey->nCol; i++){
125664 iFromCol = pFKey->aCol[i].iFrom;
125665 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
125668 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
125672 assert( pFKey->nCol==1 || (aiFree && pIdx) );
125677 iCol = pFKey->aCol[0].iFrom;
125680 for(i=0; i<pFKey->nCol; i++){
125681 if( aiCol[i]==pTab->iPKey ){
125682 aiCol[i] = -1;
125684 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
125689 if( db->xAuth ){
125691 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
125692 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
125698 /* Take a shared-cache advisory read-lock on the parent table. Allocate
125701 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
125702 pParse->nTab++;
125708 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
125727 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
125736 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
125737 && !pParse->pToplevel && !pParse->isMultiWrite
125746 if( !isIgnoreErrors || db->mallocFailed ) return;
125749 assert( aiCol || pFKey->nCol==1 );
125755 SrcItem *pItem = pSrc->a;
125756 pItem->pTab = pFKey->pFrom;
125757 pItem->zName = pFKey->pFrom->zName;
125758 pItem->pTab->nTabRef++;
125759 pItem->iCursor = pParse->nTab++;
125762 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
125765 int eAction = pFKey->aAction[aChange!=0];
125770 ** So do not set the "may-abort" flag in this case.
125773 ** may-abort flag will eventually be set on this statement anyway
125784 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
125788 pItem->zName = 0;
125806 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
125809 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125810 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
125812 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125816 for(i=0; i<pIdx->nKeyCol; i++){
125817 assert( pIdx->aiColumn[i]>=0 );
125818 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
125833 ** entry in the aChange[] array is set to -1. If the column is modified,
125838 ** non-zero. If there is no foreign key related processing, this function
125854 int *aChange, /* Non-NULL for UPDATE operations */
125859 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
125864 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
125871 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125873 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
125879 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125881 if( p->aAction[1]!=OE_None ) return 2;
125892 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
125900 ** require no special handling by the triggers sub-system, code for them is
125923 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
125925 sqlite3 *db = pParse->db; /* Database handle */
125930 action = pFKey->aAction[iAction];
125931 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
125934 pTrigger = pFKey->apTrigger[iAction];
125940 int *aiCol = 0; /* child table cols -> parent key cols */
125949 assert( aiCol || pFKey->nCol==1 );
125951 for(i=0; i<pFKey->nCol; i++){
125959 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125961 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
125962 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
125964 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
125965 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
126003 Column *pCol = pFKey->pFrom->aCol + iFromCol;
126005 if( pCol->colFlags & COLFLAG_GENERATED ){
126006 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
126007 testcase( pCol->colFlags & COLFLAG_STORED );
126010 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
126026 zFrom = pFKey->pFrom->zName;
126030 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126037 tDb.z = db->aDb[iDb].zDbSName;
126042 pRaise->affExpr = OE_Abort;
126059 nFrom + 1 /* Space for pStep->zTarget */
126062 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
126063 pStep->zTarget = (char *)&pStep[1];
126064 memcpy((char *)pStep->zTarget, zFrom, nFrom);
126066 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
126067 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
126068 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
126071 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
126075 /* Re-enable the lookaside buffer, if it was disabled earlier. */
126082 if( db->mallocFailed==1 ){
126091 pStep->op = TK_SELECT;
126095 pStep->op = TK_DELETE;
126100 pStep->op = TK_UPDATE;
126102 pStep->pTrig = pTrigger;
126103 pTrigger->pSchema = pTab->pSchema;
126104 pTrigger->pTabSchema = pTab->pSchema;
126105 pFKey->apTrigger[iAction] = pTrigger;
126106 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
126119 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
126124 /* If foreign-key support is enabled, iterate through all FKs that
126127 ** trigger sub-program. */
126128 if( pParse->db->flags&SQLITE_ForeignKeys ){
126130 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
126150 FKey *pNext; /* Copy of pFKey->pNextFrom */
126153 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126154 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
126157 if( !db || db->pnBytesFreed==0 ){
126158 if( pFKey->pPrevTo ){
126159 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126161 void *p = (void *)pFKey->pNextTo;
126162 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
126163 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
126165 if( pFKey->pNextTo ){
126166 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
126170 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
126173 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
126177 fkTriggerDelete(db, pFKey->apTrigger[0]);
126178 fkTriggerDelete(db, pFKey->apTrigger[1]);
126181 pNext = pFKey->pNextFrom;
126223 assert( pParse->pVdbe!=0 );
126224 v = pParse->pVdbe;
126226 sqlite3TableLock(pParse, iDb, pTab->tnum,
126227 (opcode==OP_OpenWrite)?1:0, pTab->zName);
126229 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
126230 VdbeComment((v, "%s", pTab->zName));
126234 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
126235 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
126237 VdbeComment((v, "%s", pTab->zName));
126247 ** ------------------------------
126262 if( !pIdx->zColAff ){
126272 Table *pTab = pIdx->pTable;
126273 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
126274 if( !pIdx->zColAff ){
126278 for(n=0; n<pIdx->nColumn; n++){
126279 i16 x = pIdx->aiColumn[n];
126282 aff = pTab->aCol[x].affinity;
126287 assert( pIdx->aColExpr!=0 );
126288 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126292 pIdx->zColAff[n] = aff;
126294 pIdx->zColAff[n] = 0;
126297 return pIdx->zColAff;
126304 ** For ordinary (legacy, non-strict) tables:
126305 ** -----------------------------------------
126311 ** which were then optimized out) then this routine becomes a no-op.
126321 ** --------- ---------------
126329 ** ------------------
126343 if( pTab->tabFlags & TF_Strict ){
126350 pPrev = sqlite3VdbeGetOp(v, -1);
126352 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
126353 pPrev->opcode = OP_TypeCheck;
126354 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
126357 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
126362 zColAff = pTab->zColAff;
126365 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
126371 for(i=j=0; i<pTab->nCol; i++){
126372 assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
126373 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
126374 zColAff[j++] = pTab->aCol[i].affinity;
126378 zColAff[j--] = 0;
126380 pTab->zColAff = zColAff;
126388 assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord
126389 || sqlite3VdbeDb(v)->mallocFailed );
126390 sqlite3VdbeChangeP4(v, -1, zColAff, i);
126396 ** Return non-zero if the table pTab in database iDb or any of its indices
126406 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
126412 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
126414 Pgno tnum = pOp->p2;
126415 if( tnum==pTab->tnum ){
126418 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
126419 if( tnum==pIndex->tnum ){
126425 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
126426 assert( pOp->p4.pVtab!=0 );
126427 assert( pOp->p4type==P4_VTAB );
126439 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
126440 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
126441 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
126465 assert( pTab->tabFlags & TF_HasGenerated );
126466 testcase( pTab->tabFlags & TF_HasVirtual );
126467 testcase( pTab->tabFlags & TF_HasStored );
126472 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126473 if( (pTab->tabFlags & TF_HasStored)!=0 ){
126474 pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
126475 if( pOp->opcode==OP_Affinity ){
126477 ** columns. '@' is the no-op affinity and those columns have not
126480 char *zP4 = pOp->p4.z;
126482 assert( pOp->p4type==P4_DYNAMIC );
126484 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
126487 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
126492 }else if( pOp->opcode==OP_TypeCheck ){
126496 pOp->p3 = 1;
126501 ** this is a two-pass algorithm. On the first pass, mark all generated
126504 for(i=0; i<pTab->nCol; i++){
126505 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
126506 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
126507 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
126508 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
126517 /* On the second pass, compute the value of each NOT-AVAILABLE column.
126522 pParse->iSelfTab = -iRegStore;
126526 for(i=0; i<pTab->nCol; i++){
126527 Column *pCol = pTab->aCol + i;
126528 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
126530 pCol->colFlags |= COLFLAG_BUSY;
126533 pCol->colFlags &= ~COLFLAG_BUSY;
126539 assert( pCol->colFlags & COLFLAG_GENERATED );
126542 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
126547 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
126549 pParse->iSelfTab = 0;
126584 assert( pParse->db->aDb[iDb].pSchema!=0 );
126585 if( (pTab->tabFlags & TF_Autoincrement)!=0
126586 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
126590 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
126594 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
126598 || pSeqTab->nCol!=2
126600 pParse->nErr++;
126601 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
126605 pInfo = pToplevel->pAinc;
126606 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
126608 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
126610 testcase( pParse->earlyCleanup );
126611 if( pParse->db->mallocFailed ) return 0;
126612 pInfo->pNext = pToplevel->pAinc;
126613 pToplevel->pAinc = pInfo;
126614 pInfo->pTab = pTab;
126615 pInfo->iDb = iDb;
126616 pToplevel->nMem++; /* Register to hold name of table */
126617 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
126618 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
126620 memId = pInfo->regCtr;
126631 sqlite3 *db = pParse->db; /* The database connection */
126634 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
126636 /* This routine is never called during trigger-generation. It is
126637 ** only called from the top-level */
126638 assert( pParse->pTriggerTab==0 );
126642 for(p = pParse->pAinc; p; p = p->pNext){
126659 pDb = &db->aDb[p->iDb];
126660 memId = p->regCtr;
126661 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
126662 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
126663 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
126669 aOp[3].p1 = memId-1;
126678 if( pParse->nTab==0 ) pParse->nTab = 1;
126692 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
126705 Vdbe *v = pParse->pVdbe;
126706 sqlite3 *db = pParse->db;
126709 for(p = pParse->pAinc; p; p = p->pNext){
126719 Db *pDb = &db->aDb[p->iDb];
126721 int memId = p->regCtr;
126724 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
126727 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
126732 aOp[2].p1 = memId-1;
126741 if( pParse->pAinc ) autoIncrementEnd(pParse);
126746 ** above are all no-ops
126770 ** then a list of all (non-hidden) columns for the table is substituted.
126775 ** first two forms shown above. A VALUES clause is really just short-hand
126781 ** insert with data coming from a single-row VALUES clause, the code executes
126782 ** once straight down through. Pseudo-code follows (we call this
126794 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
126818 ** X <- A
126826 ** end-coroutine X
126839 ** X <- A
126847 ** end co-routine R
126876 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
126877 int endOfLoop; /* Label for the end of the insertion loop */
126879 int addrInsTop = 0; /* Jump to label "D" */
126880 int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
126905 db = pParse->db;
126906 assert( db->pParse==pParse );
126907 if( pParse->nErr ){
126910 assert( db->mallocFailed==0 );
126917 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
126918 pList = pSelect->pEList;
126919 pSelect->pEList = 0;
126926 assert( pTabList->nSrc==1 );
126931 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126932 assert( iDb<db->nDb );
126933 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
126934 db->aDb[iDb].zDbSName) ){
126959 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
126965 ** ViewGetColumnNames() is a no-op if pTab is not a view.
126971 /* Cannot insert into a read-only table.
126981 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
127013 regRowid = regIns = pParse->nMem+1;
127014 pParse->nMem += pTab->nCol + 1;
127017 pParse->nMem++;
127030 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
127031 ** loop, if ipkColumn==(-1), that means that integer primary key
127040 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
127042 assert( pColumn->eU4!=EU4_EXPR );
127043 pColumn->eU4 = EU4_IDX;
127044 for(i=0; i<pColumn->nId; i++){
127045 pColumn->a[i].u4.idx = -1;
127047 for(i=0; i<pColumn->nId; i++){
127048 for(j=0; j<pTab->nCol; j++){
127049 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
127050 pColumn->a[i].u4.idx = j;
127052 if( j==pTab->iPKey ){
127056 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
127059 pTab->aCol[j].zCnName);
127066 if( j>=pTab->nCol ){
127067 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
127072 pTabList->a, pColumn->a[i].zName);
127073 pParse->checkSchema = 1;
127081 ** is coming from a SELECT statement, then generate a co-routine that
127083 ** co-routine is the common header to the 3rd and 4th templates.
127086 /* Data is coming from a SELECT or from a multi-row VALUES clause.
127087 ** Generate a co-routine to run the SELECT. */
127088 int regYield; /* Register holding co-routine entry-point */
127089 int addrTop; /* Top of the co-routine */
127092 regYield = ++pParse->nMem;
127097 dest.nSdst = pTab->nCol;
127100 assert( db->pParse==pParse );
127101 if( rc || pParse->nErr ) goto insert_cleanup;
127102 assert( db->mallocFailed==0 );
127104 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
127105 assert( pSelect->pEList );
127106 nColumn = pSelect->pEList->nExpr;
127134 int addrL; /* Label "L" */
127136 srcTab = pParse->nTab++;
127151 ** single-row VALUES clause
127156 srcTab = -1;
127159 nColumn = pList->nExpr;
127173 ipkColumn = pTab->iPKey;
127175 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
127176 testcase( pTab->tabFlags & TF_HasVirtual );
127177 testcase( pTab->tabFlags & TF_HasStored );
127178 for(i=ipkColumn-1; i>=0; i--){
127179 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
127180 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
127181 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
127182 ipkColumn--;
127194 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
127195 for(i=0; i<pTab->nCol; i++){
127196 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
127199 if( nColumn!=(pTab->nCol-nHidden) ){
127202 pTabList->a, pTab->nCol-nHidden, nColumn);
127206 if( pColumn!=0 && nColumn!=pColumn->nId ){
127207 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
127213 if( (db->flags & SQLITE_CountRows)!=0
127214 && !pParse->nested
127215 && !pParse->pTriggerTab
127216 && !pParse->bReturning
127218 regRowCount = ++pParse->nMem;
127225 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
127231 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
127233 aRegIdx[i] = ++pParse->nMem;
127234 pParse->nMem += pIdx->nColumn;
127236 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
127243 pTab->zName);
127250 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
127253 pTabList->a[0].iCursor = iDataCur;
127256 pNx->pUpsertSrc = pTabList;
127257 pNx->regData = regData;
127258 pNx->iDataCur = iDataCur;
127259 pNx->iIdxCur = iIdxCur;
127260 if( pNx->pUpsertTarget ){
127265 pNx = pNx->pNextUpsert;
127293 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
127297 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
127299 ** the value does not get overwritten by a NULL at tag-20191021-002. */
127313 for(i=0; i<pTab->nCol; i++, iRegStore++){
127317 if( i==pTab->iPKey ){
127318 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
127321 ** NULL - we cannot optimize further by skipping the column completely */
127325 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
127331 iRegStore--;
127335 ** triggers, the slots used for stored columns will be OP_Copy-ed
127346 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127352 assert( pColumn->eU4==EU4_IDX );
127353 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
127354 if( j>=pColumn->nId ){
127358 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127366 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127370 k = i - nHidden;
127380 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
127389 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
127395 ** not happened yet) so we substitute a rowid of -1
127398 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
127406 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
127409 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
127415 assert( pTab->nNVCol>0 );
127416 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
127423 if( pTab->tabFlags & TF_HasGenerated ){
127424 testcase( pTab->tabFlags & TF_HasVirtual );
127425 testcase( pTab->tabFlags & TF_HasStored );
127441 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
127443 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
127456 /* Rowid already initialized at tag-20191021-001 */
127458 Expr *pIpk = pList->a[ipkColumn].pExpr;
127459 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
127463 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
127494 if( pTab->tabFlags & TF_HasGenerated ){
127506 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
127533 }else if( pParse->bReturning ){
127535 ** constant value -1, in case one or more of the returned expressions
127537 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
127550 pTab, regData-2-pTab->nCol, onError, endOfLoop);
127567 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
127568 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
127582 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
127618 ** Meanings of bits in of pWalker->eCode for
127625 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
127630 if( pExpr->op==TK_COLUMN ){
127631 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
127632 if( pExpr->iColumn>=0 ){
127633 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
127634 pWalker->eCode |= CKCNSTRNT_COLUMN;
127637 pWalker->eCode |= CKCNSTRNT_ROWID;
127644 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
127653 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
127654 ** The operation of this routine is the same - return true if an only if
127682 ** the indexes of a table in the order provided in the Table->pIndex list.
127683 ** However, sometimes (rarely - when there is an upsert) it wants to visit
127717 assert( pIter->i==0 );
127718 if( pIter->eType ){
127719 *pIx = pIter->u.ax.aIdx[0].ix;
127720 return pIter->u.ax.aIdx[0].p;
127723 return pIter->u.lx.pIdx;
127729 if( pIter->eType ){
127730 int i = ++pIter->i;
127731 if( i>=pIter->u.ax.nIdx ){
127735 *pIx = pIter->u.ax.aIdx[i].ix;
127736 return pIter->u.ax.aIdx[i].p;
127739 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
127740 return pIter->u.lx.pIdx;
127750 ** pTab->nCol+1 registers in this range. The first register (the one
127777 ** at pTab->pIndex.
127779 ** (2019-05-07) The generated code also creates a new record for the
127781 ** register identified by aRegIdx[nIdx] - in other words in the first
127792 ** for the first index in the pTab->pIndex list. Cursors for other indices
127793 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
127801 ** --------------- ---------- ----------------------------------------
127831 ** Or if overrideError==OE_Default, then the pParse->onError parameter
127832 ** is used. Or if pParse->onError==OE_Default then the onError value
127843 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
127845 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
127871 int lblRecheckOk = 0; /* Each recheck jumps to this label if it passes */
127877 db = pParse->db;
127878 v = pParse->pVdbe;
127881 nCol = pTab->nCol;
127892 nPkField = pPk->nKeyCol;
127901 if( pTab->tabFlags & TF_HasNotNull ){
127908 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
127909 int isGenerated; /* non-zero if column is generated */
127910 onError = pCol->notNull;
127912 if( i==pTab->iPKey ){
127915 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
127931 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
127933 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
127934 testcase( pCol->colFlags & COLFLAG_STORED );
127935 testcase( pCol->colFlags & COLFLAG_GENERATED );
127951 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
127963 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
127964 pCol->zCnName);
127989 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
127997 } /* end of 2-pass loop */
127998 } /* end if( has-not-null-constraints ) */
128003 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
128004 ExprList *pCheck = pTab->pCheck;
128005 pParse->iSelfTab = -(regNewData+1);
128007 for(i=0; i<pCheck->nExpr; i++){
128010 Expr *pExpr = pCheck->a[i].pExpr;
128025 if( !db->mallocFailed ){
128032 char *zName = pCheck->a[i].zEName;
128033 assert( zName!=0 || pParse->db->mallocFailed );
128034 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
128041 pParse->iSelfTab = 0;
128058 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
128076 sIdxIter.u.lx.pIdx = pTab->pIndex;
128078 if( pUpsert->pUpsertTarget==0 ){
128079 /* There is just on ON CONFLICT clause and it has no constraint-target */
128080 assert( pUpsert->pNextUpsert==0 );
128081 if( pUpsert->isDoUpdate==0 ){
128082 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
128090 }else if( pTab->pIndex!=0 ){
128098 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128107 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
128108 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
128109 if( pTerm->pUpsertTarget==0 ) break;
128110 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
128112 pIdx = pTab->pIndex;
128113 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
128114 pIdx = pIdx->pNext;
128123 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
128153 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
128159 if( db->flags&SQLITE_RecTriggers ){
128169 regTrigCnt = ++pParse->nMem;
128184 onError = pTab->keyConf;
128195 if( pUpsertClause->isDoUpdate==0 ){
128216 && pTab->pIndex /* There exist other constraints */
128255 ** recursive-triggers flag is set, call GenerateRowDelete() to
128257 ** the triggers and remove both the table and index b-tree entries.
128259 ** Otherwise, if there are no triggers or the recursive-triggers
128261 ** GenerateRowIndexDelete(). This removes the index b-tree entries
128262 ** only. The table b-tree entry will be replaced by the new entry
128279 regNewData, 1, 0, OE_Replace, 1, -1);
128285 /* This OP_Delete opcode fires the pre-update-hook only. It does
128286 ** not modify the b-tree. It is more efficient to let the coming
128292 if( pTab->pIndex ){
128294 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
128317 sqlite3VdbeJumpHere(v, ipkTop-1);
128350 VdbeNoopComment((v, "prep index %s", pIdx->zName));
128355 if( pIdx->pPartIdxWhere ){
128357 pParse->iSelfTab = -(regNewData+1);
128358 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
128360 pParse->iSelfTab = 0;
128367 for(i=0; i<pIdx->nColumn; i++){
128368 int iField = pIdx->aiColumn[i];
128371 pParse->iSelfTab = -(regNewData+1);
128372 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
128373 pParse->iSelfTab = 0;
128374 VdbeComment((v, "%s column %d", pIdx->zName, i));
128375 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
128383 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
128386 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
128387 VdbeComment((v, "for %s", pIdx->zName));
128389 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
128390 sqlite3SetMakeRecordP5(v, pIdx->pTable);
128393 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
128405 onError = pIdx->onError;
128418 if( pUpsertClause->isDoUpdate==0 ){
128433 ** must be explicitly deleted in order to ensure any pre-update hook
128437 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
128440 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
128442 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
128443 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
128454 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
128462 ** is different from old-rowid */
128471 ** store it in registers regR..regR+nPk-1 */
128473 for(i=0; i<pPk->nKeyCol; i++){
128474 assert( pPk->aiColumn[i]>=0 );
128475 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
128477 VdbeComment((v, "%s.%s", pTab->zName,
128478 pTab->aCol[pPk->aiColumn[i]].zCnName));
128489 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
128493 for(i=0; i<pPk->nKeyCol; i++){
128494 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
128495 x = pPk->aiColumn[i];
128497 if( i==(pPk->nKeyCol-1) ){
128541 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
128542 assert( nConflictCk>0 || db->mallocFailed );
128570 if( pIdx->pPartIdxWhere ){
128573 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
128577 ** the constraint-ok jump destination to be the address of
128598 nConflictCk--;
128653 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
128667 ** to be the number of columns in table pTab that must not be NULL-trimmed.
128669 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
128675 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
128676 if( pTab->pSchema->file_format<2 ) return;
128678 for(i=pTab->nCol-1; i>0; i--){
128679 if( pTab->aCol[i].iDflt!=0 ) break;
128680 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
128689 ** PK index. This function adds code to invoke the pre-update hook,
128699 Vdbe *v = pParse->pVdbe;
128702 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
128742 v = pParse->pVdbe;
128745 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128747 assert( pIdx->onError!=OE_Replace
128748 || pIdx->pNext==0
128749 || pIdx->pNext->onError==OE_Replace );
128751 if( pIdx->pPartIdxWhere ){
128765 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
128769 if( pParse->nested ){
128782 if( !pParse->nested ){
128798 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
128804 ** pTab->pIndex list.
128806 ** If pTab is a virtual table, then this routine is a no-op and the
128828 /* This routine is a no-op for virtual tables. Leave the output
128831 *piDataCur = *piIdxCur = -999;
128834 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128835 v = pParse->pVdbe;
128837 if( iBase<0 ) iBase = pParse->nTab;
128843 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
128846 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128848 assert( pIdx->pSchema==pTab->pSchema );
128854 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
128857 VdbeComment((v, "%s", pIdx->zName));
128860 if( iBase>pParse->nTab ) pParse->nTab = iBase;
128869 ** purposes only - to make sure the transfer optimization really
128891 assert( pDest->pTable!=pSrc->pTable );
128892 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
128895 if( pDest->onError!=pSrc->onError ){
128898 for(i=0; i<pSrc->nKeyCol; i++){
128899 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
128902 if( pSrc->aiColumn[i]==XN_EXPR ){
128903 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
128904 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
128905 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
128909 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
128912 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
128916 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
128934 ** There are lots of rules for determining compatibility - see comments
128939 ** is empty - a factor that can only be determined at run-time. In that
128956 sqlite3 *db = pParse->db;
128960 SrcItem *pItem; /* An element of pSelect->pSrc */
128973 if( pParse->pWith || pSelect->pWith ){
128985 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
128988 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
128989 if( pSelect->pSrc->nSrc!=1 ){
128992 if( pSelect->pSrc->a[0].pSelect ){
128995 if( pSelect->pWhere ){
128998 if( pSelect->pOrderBy ){
129003 if( pSelect->pGroupBy ){
129006 if( pSelect->pLimit ){
129009 if( pSelect->pPrior ){
129012 if( pSelect->selFlags & SF_Distinct ){
129015 pEList = pSelect->pEList;
129017 if( pEList->nExpr!=1 ){
129020 assert( pEList->a[0].pExpr );
129021 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
129029 pItem = pSelect->pSrc->a;
129034 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
129044 if( pDest->nCol!=pSrc->nCol ){
129047 if( pDest->iPKey!=pSrc->iPKey ){
129050 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
129051 return 0; /* Cannot feed from a non-strict into a strict table */
129053 for(i=0; i<pDest->nCol; i++){
129054 Column *pDestCol = &pDest->aCol[i];
129055 Column *pSrcCol = &pSrc->aCol[i];
129057 if( (db->mDbFlags & DBFLAG_Vacuum)==0
129058 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
129080 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
129081 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
129082 return 0; /* Both columns have the same generated-column type */
129088 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
129091 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
129092 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
129093 testcase( pDestCol->colFlags & COLFLAG_STORED );
129098 if( pDestCol->affinity!=pSrcCol->affinity ){
129105 if( pDestCol->notNull && !pSrcCol->notNull ){
129109 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
129112 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
129114 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
129117 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
129118 pSrcExpr->u.zToken)!=0)
129124 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129128 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
129134 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
129139 return 0; /* Corrupt schema - two indexes on the same btree */
129143 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
129156 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
129160 if( (db->flags & SQLITE_CountRows)!=0 ){
129171 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
129174 iSrc = pParse->nTab++;
129175 iDest = pParse->nTab++;
129182 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
129183 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
129212 if( pDest->iPKey>=0 ){
129214 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129222 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
129226 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
129229 if( db->mDbFlags & DBFLAG_Vacuum ){
129236 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129245 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129246 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
129254 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
129255 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
129257 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129259 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
129263 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
129265 VdbeComment((v, "%s", pSrcIdx->zName));
129266 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
129269 VdbeComment((v, "%s", pDestIdx->zName));
129271 if( db->mDbFlags & DBFLAG_Vacuum ){
129276 ** order. In this case, instead of seeking within the b-tree as part
129278 ** OP_IdxInsert to seek to the point within the b-tree where each key
129286 for(i=0; i<pSrcIdx->nColumn; i++){
129287 const char *zColl = pSrcIdx->azColl[i];
129290 if( i==pSrcIdx->nColumn ){
129295 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
129300 if( (db->mDbFlags & DBFLAG_Vacuum)==0
129376 sqlite3_mutex_enter(db->mutex);
129383 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
129389 /* this happens for a comment or white-space */
129402 && db->flags&SQLITE_NullCallback)) ){
129429 /* EVIDENCE-OF: R-38229-40159 If the callback function to
129430 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
129468 assert( (rc&db->errMask)==rc );
129469 sqlite3_mutex_leave(db->mutex);
129871 ** (part of the main SQLite library - not an extension) so that
129878 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
129880 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
129882 #define sqlite3_bind_blob sqlite3_api->bind_blob
129883 #define sqlite3_bind_double sqlite3_api->bind_double
129884 #define sqlite3_bind_int sqlite3_api->bind_int
129885 #define sqlite3_bind_int64 sqlite3_api->bind_int64
129886 #define sqlite3_bind_null sqlite3_api->bind_null
129887 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
129888 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
129889 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
129890 #define sqlite3_bind_text sqlite3_api->bind_text
129891 #define sqlite3_bind_text16 sqlite3_api->bind_text16
129892 #define sqlite3_bind_value sqlite3_api->bind_value
129893 #define sqlite3_busy_handler sqlite3_api->busy_handler
129894 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
129895 #define sqlite3_changes sqlite3_api->changes
129896 #define sqlite3_close sqlite3_api->close
129897 #define sqlite3_collation_needed sqlite3_api->collation_needed
129898 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
129899 #define sqlite3_column_blob sqlite3_api->column_blob
129900 #define sqlite3_column_bytes sqlite3_api->column_bytes
129901 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
129902 #define sqlite3_column_count sqlite3_api->column_count
129903 #define sqlite3_column_database_name sqlite3_api->column_database_name
129904 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
129905 #define sqlite3_column_decltype sqlite3_api->column_decltype
129906 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
129907 #define sqlite3_column_double sqlite3_api->column_double
129908 #define sqlite3_column_int sqlite3_api->column_int
129909 #define sqlite3_column_int64 sqlite3_api->column_int64
129910 #define sqlite3_column_name sqlite3_api->column_name
129911 #define sqlite3_column_name16 sqlite3_api->column_name16
129912 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
129913 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
129914 #define sqlite3_column_table_name sqlite3_api->column_table_name
129915 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
129916 #define sqlite3_column_text sqlite3_api->column_text
129917 #define sqlite3_column_text16 sqlite3_api->column_text16
129918 #define sqlite3_column_type sqlite3_api->column_type
129919 #define sqlite3_column_value sqlite3_api->column_value
129920 #define sqlite3_commit_hook sqlite3_api->commit_hook
129921 #define sqlite3_complete sqlite3_api->complete
129922 #define sqlite3_complete16 sqlite3_api->complete16
129923 #define sqlite3_create_collation sqlite3_api->create_collation
129924 #define sqlite3_create_collation16 sqlite3_api->create_collation16
129925 #define sqlite3_create_function sqlite3_api->create_function
129926 #define sqlite3_create_function16 sqlite3_api->create_function16
129927 #define sqlite3_create_module sqlite3_api->create_module
129928 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
129929 #define sqlite3_data_count sqlite3_api->data_count
129930 #define sqlite3_db_handle sqlite3_api->db_handle
129931 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
129932 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
129933 #define sqlite3_errcode sqlite3_api->errcode
129934 #define sqlite3_errmsg sqlite3_api->errmsg
129935 #define sqlite3_errmsg16 sqlite3_api->errmsg16
129936 #define sqlite3_exec sqlite3_api->exec
129938 #define sqlite3_expired sqlite3_api->expired
129940 #define sqlite3_finalize sqlite3_api->finalize
129941 #define sqlite3_free sqlite3_api->free
129942 #define sqlite3_free_table sqlite3_api->free_table
129943 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
129944 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
129945 #define sqlite3_get_table sqlite3_api->get_table
129947 #define sqlite3_global_recover sqlite3_api->global_recover
129949 #define sqlite3_interrupt sqlite3_api->interruptx
129950 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
129951 #define sqlite3_libversion sqlite3_api->libversion
129952 #define sqlite3_libversion_number sqlite3_api->libversion_number
129953 #define sqlite3_malloc sqlite3_api->malloc
129954 #define sqlite3_mprintf sqlite3_api->mprintf
129955 #define sqlite3_open sqlite3_api->open
129956 #define sqlite3_open16 sqlite3_api->open16
129957 #define sqlite3_prepare sqlite3_api->prepare
129958 #define sqlite3_prepare16 sqlite3_api->prepare16
129959 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
129960 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
129961 #define sqlite3_profile sqlite3_api->profile
129962 #define sqlite3_progress_handler sqlite3_api->progress_handler
129963 #define sqlite3_realloc sqlite3_api->realloc
129964 #define sqlite3_reset sqlite3_api->reset
129965 #define sqlite3_result_blob sqlite3_api->result_blob
129966 #define sqlite3_result_double sqlite3_api->result_double
129967 #define sqlite3_result_error sqlite3_api->result_error
129968 #define sqlite3_result_error16 sqlite3_api->result_error16
129969 #define sqlite3_result_int sqlite3_api->result_int
129970 #define sqlite3_result_int64 sqlite3_api->result_int64
129971 #define sqlite3_result_null sqlite3_api->result_null
129972 #define sqlite3_result_text sqlite3_api->result_text
129973 #define sqlite3_result_text16 sqlite3_api->result_text16
129974 #define sqlite3_result_text16be sqlite3_api->result_text16be
129975 #define sqlite3_result_text16le sqlite3_api->result_text16le
129976 #define sqlite3_result_value sqlite3_api->result_value
129977 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
129978 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
129979 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
129980 #define sqlite3_snprintf sqlite3_api->xsnprintf
129981 #define sqlite3_step sqlite3_api->step
129982 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
129983 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
129984 #define sqlite3_total_changes sqlite3_api->total_changes
129985 #define sqlite3_trace sqlite3_api->trace
129987 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
129989 #define sqlite3_update_hook sqlite3_api->update_hook
129990 #define sqlite3_user_data sqlite3_api->user_data
129991 #define sqlite3_value_blob sqlite3_api->value_blob
129992 #define sqlite3_value_bytes sqlite3_api->value_bytes
129993 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
129994 #define sqlite3_value_double sqlite3_api->value_double
129995 #define sqlite3_value_int sqlite3_api->value_int
129996 #define sqlite3_value_int64 sqlite3_api->value_int64
129997 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
129998 #define sqlite3_value_text sqlite3_api->value_text
129999 #define sqlite3_value_text16 sqlite3_api->value_text16
130000 #define sqlite3_value_text16be sqlite3_api->value_text16be
130001 #define sqlite3_value_text16le sqlite3_api->value_text16le
130002 #define sqlite3_value_type sqlite3_api->value_type
130003 #define sqlite3_vmprintf sqlite3_api->vmprintf
130004 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
130005 #define sqlite3_overload_function sqlite3_api->overload_function
130006 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
130007 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
130008 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
130009 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
130010 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
130011 #define sqlite3_blob_close sqlite3_api->blob_close
130012 #define sqlite3_blob_open sqlite3_api->blob_open
130013 #define sqlite3_blob_read sqlite3_api->blob_read
130014 #define sqlite3_blob_write sqlite3_api->blob_write
130015 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
130016 #define sqlite3_file_control sqlite3_api->file_control
130017 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
130018 #define sqlite3_memory_used sqlite3_api->memory_used
130019 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
130020 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
130021 #define sqlite3_mutex_free sqlite3_api->mutex_free
130022 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
130023 #define sqlite3_mutex_try sqlite3_api->mutex_try
130024 #define sqlite3_open_v2 sqlite3_api->open_v2
130025 #define sqlite3_release_memory sqlite3_api->release_memory
130026 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
130027 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
130028 #define sqlite3_sleep sqlite3_api->sleep
130029 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
130030 #define sqlite3_vfs_find sqlite3_api->vfs_find
130031 #define sqlite3_vfs_register sqlite3_api->vfs_register
130032 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
130033 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
130034 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
130035 #define sqlite3_result_error_code sqlite3_api->result_error_code
130036 #define sqlite3_test_control sqlite3_api->test_control
130037 #define sqlite3_randomness sqlite3_api->randomness
130038 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
130039 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
130040 #define sqlite3_limit sqlite3_api->limit
130041 #define sqlite3_next_stmt sqlite3_api->next_stmt
130042 #define sqlite3_sql sqlite3_api->sql
130043 #define sqlite3_status sqlite3_api->status
130044 #define sqlite3_backup_finish sqlite3_api->backup_finish
130045 #define sqlite3_backup_init sqlite3_api->backup_init
130046 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
130047 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
130048 #define sqlite3_backup_step sqlite3_api->backup_step
130049 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
130050 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
130051 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
130052 #define sqlite3_db_config sqlite3_api->db_config
130053 #define sqlite3_db_mutex sqlite3_api->db_mutex
130054 #define sqlite3_db_status sqlite3_api->db_status
130055 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
130056 #define sqlite3_log sqlite3_api->log
130057 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
130058 #define sqlite3_sourceid sqlite3_api->sourceid
130059 #define sqlite3_stmt_status sqlite3_api->stmt_status
130060 #define sqlite3_strnicmp sqlite3_api->strnicmp
130061 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
130062 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
130063 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
130064 #define sqlite3_wal_hook sqlite3_api->wal_hook
130065 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
130066 #define sqlite3_vtab_config sqlite3_api->vtab_config
130067 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
130069 #define sqlite3_close_v2 sqlite3_api->close_v2
130070 #define sqlite3_db_filename sqlite3_api->db_filename
130071 #define sqlite3_db_readonly sqlite3_api->db_readonly
130072 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
130073 #define sqlite3_errstr sqlite3_api->errstr
130074 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
130075 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
130076 #define sqlite3_stricmp sqlite3_api->stricmp
130077 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
130078 #define sqlite3_uri_int64 sqlite3_api->uri_int64
130079 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
130080 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
130081 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
130083 #define sqlite3_auto_extension sqlite3_api->auto_extension
130084 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
130085 #define sqlite3_bind_text64 sqlite3_api->bind_text64
130086 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
130087 #define sqlite3_load_extension sqlite3_api->load_extension
130088 #define sqlite3_malloc64 sqlite3_api->malloc64
130089 #define sqlite3_msize sqlite3_api->msize
130090 #define sqlite3_realloc64 sqlite3_api->realloc64
130091 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
130092 #define sqlite3_result_blob64 sqlite3_api->result_blob64
130093 #define sqlite3_result_text64 sqlite3_api->result_text64
130094 #define sqlite3_strglob sqlite3_api->strglob
130096 #define sqlite3_value_dup sqlite3_api->value_dup
130097 #define sqlite3_value_free sqlite3_api->value_free
130098 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
130099 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
130101 #define sqlite3_value_subtype sqlite3_api->value_subtype
130102 #define sqlite3_result_subtype sqlite3_api->result_subtype
130104 #define sqlite3_status64 sqlite3_api->status64
130105 #define sqlite3_strlike sqlite3_api->strlike
130106 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
130108 #define sqlite3_system_errno sqlite3_api->system_errno
130110 #define sqlite3_trace_v2 sqlite3_api->trace_v2
130111 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
130113 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
130115 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
130116 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
130117 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
130118 #define sqlite3_result_pointer sqlite3_api->result_pointer
130119 #define sqlite3_value_pointer sqlite3_api->value_pointer
130121 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
130122 #define sqlite3_value_nochange sqlite3_api->value_nochange
130123 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
130125 #define sqlite3_keyword_count sqlite3_api->keyword_count
130126 #define sqlite3_keyword_name sqlite3_api->keyword_name
130127 #define sqlite3_keyword_check sqlite3_api->keyword_check
130128 #define sqlite3_str_new sqlite3_api->str_new
130129 #define sqlite3_str_finish sqlite3_api->str_finish
130130 #define sqlite3_str_appendf sqlite3_api->str_appendf
130131 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
130132 #define sqlite3_str_append sqlite3_api->str_append
130133 #define sqlite3_str_appendall sqlite3_api->str_appendall
130134 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
130135 #define sqlite3_str_reset sqlite3_api->str_reset
130136 #define sqlite3_str_errcode sqlite3_api->str_errcode
130137 #define sqlite3_str_length sqlite3_api->str_length
130138 #define sqlite3_str_value sqlite3_api->str_value
130140 #define sqlite3_create_window_function sqlite3_api->create_window_function
130142 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
130144 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
130145 #define sqlite3_value_frombind sqlite3_api->value_frombind
130147 #define sqlite3_drop_modules sqlite3_api->drop_modules
130149 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
130150 #define sqlite3_uri_key sqlite3_api->uri_key
130151 #define sqlite3_filename_database sqlite3_api->filename_database
130152 #define sqlite3_filename_journal sqlite3_api->filename_journal
130153 #define sqlite3_filename_wal sqlite3_api->filename_wal
130155 #define sqlite3_create_filename sqlite3_api->create_filename
130156 #define sqlite3_free_filename sqlite3_api->free_filename
130157 #define sqlite3_database_file_object sqlite3_api->database_file_object
130159 #define sqlite3_txn_state sqlite3_api->txn_state
130161 #define sqlite3_changes64 sqlite3_api->changes64
130162 #define sqlite3_total_changes64 sqlite3_api->total_changes64
130164 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
130166 #define sqlite3_error_offset sqlite3_api->error_offset
130167 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130168 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130169 #define sqlite3_vtab_in sqlite3_api->vtab_in
130170 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130171 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
130174 #define sqlite3_deserialize sqlite3_api->deserialize
130175 #define sqlite3_serialize sqlite3_api->serialize
130177 #define sqlite3_db_name sqlite3_api->db_name
130190 # define SQLITE_EXTENSION_INIT1 /*no-op*/
130192 # define SQLITE_EXTENSION_INIT3 /*no-op*/
130719 sqlite3_vfs *pVfs = db->pVfs;
130751 if( (db->flags & SQLITE_LoadExtension)==0 ){
130760 /* tag-20210611-1. Some dlopen() implementations will segfault if given
130798 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
130818 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130837 /* Append the new shared library handle to the db->aExtension array. */
130838 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
130842 if( db->nExtension>0 ){
130843 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
130845 sqlite3DbFree(db, db->aExtension);
130846 db->aExtension = aHandle;
130848 db->aExtension[db->nExtension++] = handle;
130859 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130871 sqlite3_mutex_enter(db->mutex);
130874 sqlite3_mutex_leave(db->mutex);
130884 assert( sqlite3_mutex_held(db->mutex) );
130885 for(i=0; i<db->nExtension; i++){
130886 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
130888 sqlite3DbFree(db, db->aExtension);
130896 sqlite3_mutex_enter(db->mutex);
130898 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
130900 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
130902 sqlite3_mutex_leave(db->mutex);
130923 ** we have to locate the state vector at run-time. In the more common
130983 ** routine is a no-op.
130998 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
131000 wsdAutoext.nExt--;
131166 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
131170 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
131172 /* Names of columns for pragmas that return multi-column result
131173 ** or that return single-column results where the name of the
131240 /* Definitions of all built-in pragmas */
131831 ** Interpret the given string as an auto-vacuum mode value.
131849 ** backed temporary databases, 2 for the Red-Black tree in memory database
131850 ** and 0 to use the compile-time default.
131854 return z[0] - '0';
131871 sqlite3 *db = pParse->db;
131872 if( db->aDb[1].pBt!=0 ){
131873 if( !db->autoCommit
131874 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
131880 sqlite3BtreeClose(db->aDb[1].pBt);
131881 db->aDb[1].pBt = 0;
131896 sqlite3 *db = pParse->db;
131897 if( db->temp_store==ts ) return SQLITE_OK;
131901 db->temp_store = (u8)ts;
131913 u8 n = pPragma->nPragCName;
131916 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
131919 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
131953 if( db->autoCommit ){
131954 Db *pDb = db->aDb;
131955 int n = db->nDb;
131961 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
131962 while( (n--) > 0 ){
131963 if( pDb->pBt ){
131964 sqlite3BtreeSetPagerFlags(pDb->pBt,
131965 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
131972 # define setAllPagerFlags(X) /* no-op */
131977 ** Return a human-readable name for a constraint resolution action.
131998 ** journal-mode name.
132025 upr = ArraySize(aPragmaName)-1;
132031 upr = mid - 1;
132046 int isBuiltin, /* True if this is a built-in function */
132057 for(; p; p=p->pNext){
132066 if( p->xSFunc==0 ) continue;
132067 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
132072 if( p->xValue!=0 ){
132074 }else if( p->xFinalize!=0 ){
132080 p->zName, isBuiltin,
132081 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
132082 p->nArg,
132083 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
132092 ** Generate code to output a single-column result row with a value of the
132125 int minusFlag /* True if a '-' sign preceded <value> */
132127 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
132128 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
132134 sqlite3 *db = pParse->db; /* The database connection */
132141 pParse->nMem = 2;
132147 pDb = &db->aDb[iDb];
132159 zRight = sqlite3MPrintf(db, "-%T", pValue);
132165 zDb = pId2->n>0 ? pDb->zDbSName : 0;
132170 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
132172 ** handled the pragma and generate a no-op prepared statement.
132174 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
132179 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
132189 db->busyHandler.nBusy = 0;
132203 pParse->nErr++;
132204 pParse->rc = rc;
132211 /* IMP: R-43042-22504 No error messages are generated if an
132217 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
132222 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
132223 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
132229 switch( pPragma->ePragTyp ){
132264 pParse->nMem += 2;
132276 pDb->pSchema->cache_size = size;
132277 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132294 Btree *pBt = pDb->pBt;
132300 /* Malloc may fail when setting the page-size, as there is an internal
132303 db->nextPagesize = sqlite3Atoi(zRight);
132304 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
132320 Btree *pBt = pDb->pBt;
132321 int b = -1;
132330 if( pId2->n==0 && b>=0 ){
132332 for(ii=0; ii<db->nDb; ii++){
132333 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
132362 iReg = ++pParse->nMem;
132386 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
132389 ** the locking-mode of the main database).
132391 eMode = db->dfltLockMode;
132394 if( pId2->n==0 ){
132396 ** of the PRAGMA command. In this case the locking-mode must be
132404 assert(pDb==&db->aDb[0]);
132405 for(ii=2; ii<db->nDb; ii++){
132406 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
132409 db->dfltLockMode = (u8)eMode;
132411 pPager = sqlite3BtreePager(pDb->pBt);
132448 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
132449 /* Do not allow journal-mode "OFF" in defensive since the database
132454 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
132457 pId2->n = 1;
132459 for(ii=db->nDb-1; ii>=0; ii--){
132460 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132476 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132477 i64 iLimit = -2;
132480 if( iLimit<-1 ) iLimit = -1;
132493 ** Get or set the value of the database 'auto-vacuum' parameter.
132498 Btree *pBt = pDb->pBt;
132505 db->nextAutovac = (u8)eAuto;
132507 ** incr-vacuum flags. This is required in case this connection
132509 ** as an auto-vacuum capable db.
132516 ** that this really is an auto-vacuum capable database.
132535 aOp[4].p3 = eAuto - 1;
132558 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
132574 ** number of pages is adjusted so that the cache uses -N kibibytes
132580 returnSingleInt(v, pDb->pSchema->cache_size);
132583 pDb->pSchema->cache_size = size;
132584 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132601 ** number of pages is adjusted so that the cache uses -N kibibytes
132615 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
132616 sqlite3BtreeSetSpillSize(pDb->pBt,0));
132620 sqlite3BtreeSetSpillSize(pDb->pBt, size);
132623 db->flags |= SQLITE_CacheSpill;
132625 db->flags &= ~(u64)SQLITE_CacheSpill;
132654 if( pId2->n==0 ) db->szMmap = sz;
132655 for(ii=db->nDb-1; ii>=0; ii--){
132656 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132657 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
132661 sz = -1;
132670 pParse->nErr++;
132671 pParse->rc = rc;
132684 ** Note that it is possible for the library compile-time options to
132689 returnSingleInt(v, db->temp_store);
132713 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132720 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
132721 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
132757 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132786 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132793 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132823 returnSingleInt(v, pDb->safety_level-1);
132825 if( !db->autoCommit ){
132831 pDb->safety_level = iLevel;
132832 pDb->bSyncSet = 1;
132844 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
132846 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
132847 if( db->autoCommit==0 ){
132849 ** in auto-commit mode. */
132853 if( db->auth.authLevel==UAUTH_User ){
132854 /* Do not allow non-admin users to modify the schema arbitrarily */
132860 db->flags |= mask;
132862 db->flags &= ~mask;
132863 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
132867 /* IMP: R-60817-01178 If the argument is "RESET" then schema
132874 /* Many of the flag-pragmas modify the code generated by the SQL
132897 ** pk: Non-zero for PK fields.
132908 pParse->nMem = 7;
132910 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
132913 if( pCol->colFlags & COLFLAG_NOINSERT ){
132914 if( pPragma->iArg==0 ){
132918 if( pCol->colFlags & COLFLAG_VIRTUAL ){
132920 }else if( pCol->colFlags & COLFLAG_STORED ){
132922 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
132926 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
132931 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
132934 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
132937 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
132938 i-nHidden,
132939 pCol->zCnName,
132941 pCol->notNull ? 1 : 0,
132942 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
132965 pParse->nMem = 6;
132967 for(ii=0; ii<db->nDb; ii++){
132971 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
132978 pHash = &db->aDb[ii].pSchema->tblHash;
132980 while( initNCol-- ){
132985 if( pTab->nCol==0 ){
132986 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
132989 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
132993 if( db->mallocFailed ){
132994 sqlite3ErrorMsg(db->pParse, "out of memory");
132995 db->pParse->rc = SQLITE_NOMEM_BKPT;
132997 pHash = &db->aDb[ii].pSchema->tblHash;
133006 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
133011 }else if( pTab->tabFlags & TF_Shadow ){
133017 db->aDb[ii].zDbSName,
133018 sqlite3PreferredTableName(pTab->zName),
133020 pTab->nCol,
133021 (pTab->tabFlags & TF_WithoutRowid)!=0,
133022 (pTab->tabFlags & TF_Strict)!=0
133033 pParse->nMem = 5;
133035 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
133038 sqlite3PreferredTableName(pTab->zName),
133040 pTab->szTabRow,
133041 pTab->nRowLogEst,
133042 pTab->tabFlags);
133043 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133045 pIdx->zName,
133046 pIdx->szIdxRow,
133047 pIdx->aiRowLogEst[0],
133048 pIdx->hasStat1);
133070 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
133073 if( pPragma->iArg ){
133075 mx = pIdx->nColumn;
133076 pParse->nMem = 6;
133079 mx = pIdx->nKeyCol;
133080 pParse->nMem = 3;
133082 pTab = pIdx->pTable;
133084 assert( pParse->nMem<=pPragma->nPragCName );
133086 i16 cnum = pIdx->aiColumn[i];
133088 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
133089 if( pPragma->iArg ){
133091 pIdx->aSortOrder[i],
133092 pIdx->azColl[i],
133093 i<pIdx->nKeyCol);
133095 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
133107 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133108 pParse->nMem = 5;
133110 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
133114 pIdx->zName,
133116 azOrigin[pIdx->idxType],
133117 pIdx->pPartIdxWhere!=0);
133125 pParse->nMem = 3;
133126 for(i=0; i<db->nDb; i++){
133127 if( db->aDb[i].pBt==0 ) continue;
133128 assert( db->aDb[i].zDbSName!=0 );
133131 db->aDb[i].zDbSName,
133132 sqlite3BtreeGetFilename(db->aDb[i].pBt));
133140 pParse->nMem = 2;
133141 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
133143 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
133153 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
133154 pParse->nMem = 6;
133156 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
133157 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
133161 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
133163 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
133172 pParse->nMem = 1;
133173 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
133175 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
133198 pFK = pTab->u.tab.pFKey;
133200 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133202 pParse->nMem = 8;
133206 for(j=0; j<pFK->nCol; j++){
133210 pFK->zTo,
133211 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
133212 pFK->aCol[j].zCol,
133213 actionName(pFK->aAction[1]), /* ON UPDATE */
133214 actionName(pFK->aAction[0]), /* ON DELETE */
133218 pFK = pFK->pNextFrom;
133243 regResult = pParse->nMem+1;
133244 pParse->nMem += 4;
133245 regRow = ++pParse->nMem;
133246 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
133255 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
133256 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133257 zDb = db->aDb[iDb].zDbSName;
133259 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
133260 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
133262 sqlite3VdbeLoadString(v, regResult, pTab->zName);
133264 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133265 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133268 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
133274 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
133282 assert( pParse->nErr>0 || pFK==0 );
133284 if( pParse->nTab<i ) pParse->nTab = i;
133287 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133288 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133293 assert( x==0 || db->mallocFailed );
133301 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
133302 for(j=0; j<pFK->nCol; j++){
133303 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
133311 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
133312 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
133313 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
133319 assert( pFK->nCol==1 || db->mallocFailed );
133328 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
133367 ** without the overhead of cross-checking indexes. Quick_check
133395 ** to -1 here, to indicate that the VDBE should verify the integrity
133398 assert( iDb==0 || pId2->z );
133399 if( pId2->z==0 ) iDb = -1;
133402 pParse->nMem = 6;
133413 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
133416 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
133419 for(i=0; i<db->nDb; i++){
133431 /* Do an integrity check of the B-Tree
133437 pTbls = &db->aDb[i].pSchema->tblHash;
133444 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
133457 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
133458 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133459 aRoot[++cnt] = pIdx->tnum;
133465 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
133468 /* Do the b-tree integrity checks */
133473 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
133487 int r1 = -1;
133496 ** reg[8+i] counts the number of entries in the i-th index
133499 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133502 assert( pParse->nMem>=8+j );
133508 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133510 VdbeComment((v, "(right-most column)"));
133514 bStrict = (pTab->tabFlags & TF_Strict)!=0;
133515 for(j=0; j<pTab->nCol; j++){
133517 Column *pCol = pTab->aCol + j;
133519 if( j==pTab->iPKey ) continue;
133520 if( pCol->notNull==0 && !bStrict ) continue;
133523 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
133526 if( pCol->notNull ){
133528 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133529 pCol->zCnName);
133531 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133538 if( (pTab->tabFlags & TF_Strict)!=0
133539 && pCol->eCType!=COLTYPE_ANY
133542 sqlite3StdTypeMap[pCol->eCType-1]);
133544 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133545 sqlite3StdType[pCol->eCType-1],
133546 pTab->zName, pTab->aCol[j].zCnName);
133554 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133555 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133556 if( db->mallocFailed==0 ){
133561 pParse->iSelfTab = iDataCur + 1;
133562 for(k=pCheck->nExpr-1; k>0; k--){
133563 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
133565 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
133568 pParse->iSelfTab = 0;
133570 pTab->zName);
133579 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133589 pIdx->nColumn); VdbeCoverage(v);
133594 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
133605 for(kk=0; kk<pIdx->nKeyCol; kk++){
133606 int iCol = pIdx->aiColumn[kk];
133607 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
133608 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
133616 pIdx->nKeyCol); VdbeCoverage(v);
133617 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
133626 sqlite3VdbeJumpHere(v, loopTop-1);
133629 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133634 sqlite3VdbeLoadString(v, 4, pIdx->zName);
133657 aOp[0].p2 = 1-mxErr;
133663 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
133672 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
133677 ** The second form of this pragma is a no-op if the main database file
133698 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
133699 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
133700 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
133703 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
133713 returnSingleText(v, encnames[ENC(pParse->db)].zName);
133720 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
133721 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
133722 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
133723 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
133729 if( !pEnc->zName ){
133754 ** the value of the schema-version and user-version, respectively. Both
133755 ** the schema-version and the user-version are 32-bit signed integers
133758 ** The schema-cookie is usually only manipulated internally by SQLite. It
133765 ** the schema-version is potentially dangerous and may lead to program
133768 ** The user-version is not used internally by SQLite. It may be used by
133772 int iCookie = pPragma->iArg; /* Which cookie to read or write */
133774 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
133813 ** Return the names of all compile-time options used in this build,
133819 pParse->nMem = 1;
133836 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
133847 pParse->nMem = 3;
133866 db->xWalCallback==sqlite3WalDefaultHook ?
133867 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
133875 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
133917 ** ever added that should be off by default, those off-by-default
133927 ** (2) The query planner used sqlite_stat1-style statistics for one or
133955 iTabCur = pParse->nTab++;
133956 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
133959 pSchema = db->aDb[iDb].pSchema;
133960 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
133966 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
133969 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
133970 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133971 if( !pIdx->hasStat1 ){
133983 db->aDb[iDb].zDbSName, pTab->zName);
134007 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
134011 returnSingleInt(v, db->busyTimeout);
134019 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
134021 ** specified and is a non-negative integer.
134022 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
134024 ** sqlite3_soft_heap_limit64(-1) C-language function.
134031 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
134042 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
134049 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
134052 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
134071 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
134085 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
134088 db->nAnalysisLimit = (int)(N&0x7fffffff);
134090 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
134103 pParse->nMem = 2;
134104 for(i=0; i<db->nDb; i++){
134108 if( db->aDb[i].zDbSName==0 ) continue;
134109 pBt = db->aDb[i].pBt;
134112 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
134116 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
134124 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
134133 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
134138 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
134189 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
134194 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
134198 if( pPragma->mPragFlg & PragFlg_Result1 ){
134202 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
134208 assert( strlen(zBuf) < sizeof(zBuf)-1 );
134216 pTab->pName = pPragma;
134217 pTab->db = db;
134218 pTab->iHidden = i;
134219 pTab->nHidden = j;
134251 pIdxInfo->estimatedCost = (double)1;
134252 if( pTab->nHidden==0 ){ return SQLITE_OK; }
134253 pConstraint = pIdxInfo->aConstraint;
134256 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
134257 if( pConstraint->usable==0 ) continue;
134258 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
134259 if( pConstraint->iColumn < pTab->iHidden ) continue;
134260 j = pConstraint->iColumn - pTab->iHidden;
134265 pIdxInfo->estimatedCost = (double)2147483647;
134266 pIdxInfo->estimatedRows = 2147483647;
134269 j = seen[0]-1;
134270 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
134271 pIdxInfo->aConstraintUsage[j].omit = 1;
134273 pIdxInfo->estimatedCost = (double)20;
134274 pIdxInfo->estimatedRows = 20;
134275 j = seen[1]-1;
134276 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
134277 pIdxInfo->aConstraintUsage[j].omit = 1;
134287 pCsr->base.pVtab = pVtab;
134288 *ppCursor = &pCsr->base;
134295 sqlite3_finalize(pCsr->pPragma);
134296 pCsr->pPragma = 0;
134297 for(i=0; i<ArraySize(pCsr->azArg); i++){
134298 sqlite3_free(pCsr->azArg[i]);
134299 pCsr->azArg[i] = 0;
134317 pCsr->iRowid++;
134318 assert( pCsr->pPragma );
134319 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
134320 rc = sqlite3_finalize(pCsr->pPragma);
134321 pCsr->pPragma = 0;
134336 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134345 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
134348 assert( j<ArraySize(pCsr->azArg) );
134349 assert( pCsr->azArg[j]==0 );
134351 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
134352 if( pCsr->azArg[j]==0 ){
134357 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
134359 if( pCsr->azArg[1] ){
134360 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
134362 sqlite3_str_appendall(&acc, pTab->pName->zName);
134363 if( pCsr->azArg[0] ){
134364 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
134368 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
134371 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
134382 return (pCsr->pPragma==0);
134394 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134395 if( i<pTab->iHidden ){
134396 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
134398 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
134408 *p = pCsr->iRowid;
134415 0, /* xCreate - create a table */
134416 pragmaVtabConnect, /* xConnect - connect to an existing table */
134417 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
134418 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
134419 0, /* xDestroy - Drop a table */
134420 pragmaVtabOpen, /* xOpen - open a cursor */
134421 pragmaVtabClose, /* xClose - close a cursor */
134422 pragmaVtabFilter, /* xFilter - configure scan constraints */
134423 pragmaVtabNext, /* xNext - advance a cursor */
134425 pragmaVtabColumn, /* xColumn - read data */
134426 pragmaVtabRowid, /* xRowid - read data */
134427 0, /* xUpdate - write data */
134428 0, /* xBegin - begin transaction */
134429 0, /* xSync - sync transaction */
134430 0, /* xCommit - commit transaction */
134431 0, /* xRollback - rollback transaction */
134432 0, /* xFindFunction - function overloading */
134433 0, /* xRename - rename the table */
134450 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
134451 assert( sqlite3HashFind(&db->aModule, zName)==0 );
134487 sqlite3 *db = pData->db;
134488 if( db->mallocFailed ){
134489 pData->rc = SQLITE_NOMEM_BKPT;
134490 }else if( pData->pzErrMsg[0]!=0 ){
134492 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
134498 *pData->pzErrMsg = sqlite3MPrintf(db,
134500 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
134503 pData->rc = SQLITE_ERROR;
134504 }else if( db->flags & SQLITE_WriteSchema ){
134505 pData->rc = SQLITE_CORRUPT_BKPT;
134510 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
134511 *pData->pzErrMsg = z;
134512 pData->rc = SQLITE_CORRUPT_BKPT;
134523 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
134524 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
134532 const char *zSql, /* UTF-8 encoded SQL statement. */
134557 sqlite3 *db = pData->db;
134558 int iDb = pData->iDb;
134562 assert( sqlite3_mutex_held(db->mutex) );
134563 db->mDbFlags |= DBFLAG_EncodingFixed;
134565 pData->nInitRow++;
134566 if( db->mallocFailed ){
134571 assert( iDb>=0 && iDb<db->nDb );
134578 ** But because db->init.busy is set to 1, no VDBE code is generated
134588 u8 saved_iDb = db->init.iDb;
134592 assert( db->init.busy );
134593 db->init.iDb = iDb;
134594 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
134595 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
134601 db->init.orphanTrigger = 0;
134602 db->init.azInit = (const char**)argv;
134604 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
134605 rc = db->errCode;
134607 db->init.iDb = saved_iDb;
134608 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
134610 if( db->init.orphanTrigger ){
134613 if( rc > pData->rc ) pData->rc = rc;
134621 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
134633 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
134637 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
134638 || pIndex->tnum<2
134639 || pIndex->tnum>pData->mxPage
134670 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
134672 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
134673 assert( iDb>=0 && iDb<db->nDb );
134674 assert( db->aDb[iDb].pSchema );
134675 assert( sqlite3_mutex_held(db->mutex) );
134676 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
134678 db->init.busy = 1;
134680 /* Construct the in-memory representation schema tables (sqlite_schema or
134684 ** the schema table as read-only. */
134700 db->mDbFlags &= mask;
134708 pDb = &db->aDb[iDb];
134709 if( pDb->pBt==0 ){
134716 /* If there is not already a read-only (or read-write) transaction opened
134717 ** on the b-tree database, open one now. If a transaction is opened, it
134719 sqlite3BtreeEnter(pDb->pBt);
134720 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
134721 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
134736 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
134747 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
134749 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
134752 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
134754 /* If opening a non-empty database, check the text encoding. For the
134759 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
134760 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
134764 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
134772 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
134780 pDb->pSchema->enc = ENC(db);
134782 if( pDb->pSchema->cache_size==0 ){
134784 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
134786 pDb->pSchema->cache_size = size;
134788 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
134790 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
134796 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
134799 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
134800 if( pDb->pSchema->file_format==0 ){
134801 pDb->pSchema->file_format = 1;
134803 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
134814 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
134815 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
134820 assert( db->init.busy );
134821 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
134826 db->aDb[iDb].zDbSName, zSchemaTabName);
134830 xAuth = db->xAuth;
134831 db->xAuth = 0;
134835 db->xAuth = xAuth;
134846 assert( pDb == &(db->aDb[iDb]) );
134847 if( db->mallocFailed ){
134850 pDb = &db->aDb[iDb];
134852 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
134873 sqlite3BtreeCommit(pDb->pBt);
134875 sqlite3BtreeLeave(pDb->pBt);
134884 db->init.busy = 0;
134889 ** Initialize all database files - the main database file, the file
134899 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
134901 assert( sqlite3_mutex_held(db->mutex) );
134902 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
134903 assert( db->init.busy==0 );
134905 assert( db->nDb>0 );
134912 for(i=db->nDb-1; i>0; i--){
134913 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
134926 ** This routine is a no-op if the database schema is already initialized.
134931 sqlite3 *db = pParse->db;
134932 assert( sqlite3_mutex_held(db->mutex) );
134933 if( !db->init.busy ){
134934 rc = sqlite3Init(db, &pParse->zErrMsg);
134936 pParse->rc = rc;
134937 pParse->nErr++;
134938 }else if( db->noSharedCache ){
134939 db->mDbFlags |= DBFLAG_SchemaKnownOk;
134948 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
134949 ** make no changes to pParse->rc.
134952 sqlite3 *db = pParse->db;
134957 assert( pParse->checkSchema );
134958 assert( sqlite3_mutex_held(db->mutex) );
134959 for(iDb=0; iDb<db->nDb; iDb++){
134961 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
134964 /* If there is not already a read-only (or read-write) transaction opened
134965 ** on the b-tree database, open one now. If a transaction is opened, it
134966 ** will be closed immediately after reading the meta-value. */
134971 pParse->rc = SQLITE_NOMEM;
134978 ** value stored as part of the in-memory schema representation,
134982 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
134984 pParse->rc = SQLITE_SCHEMA;
134996 ** which database file in db->aDb[] the schema refers to.
135002 int i = -32768;
135004 /* If pSchema is NULL, then return -32768. This happens when code in
135006 ** created by a sub-select). In this case the return value of this
135009 ** We return -32768 instead of the more usual -1 simply because using
135010 ** -32768 as the incorrect index into db->aDb[] is much
135011 ** more likely to cause a segfault than -1 (of course there are assert()
135013 ** -32768 will still fit into a 16-bit signed integer.
135015 assert( sqlite3_mutex_held(db->mutex) );
135018 assert( i<db->nDb );
135019 if( db->aDb[i].pSchema==pSchema ){
135023 assert( i>=0 && i<db->nDb );
135032 sqlite3 *db = pParse->db;
135034 assert( db->pParse==pParse );
135035 assert( pParse->nested==0 );
135037 sqlite3DbFree(db, pParse->aTableLock);
135039 while( pParse->pCleanup ){
135040 ParseCleanup *pCleanup = pParse->pCleanup;
135041 pParse->pCleanup = pCleanup->pNext;
135042 pCleanup->xCleanup(db, pCleanup->pPtr);
135045 sqlite3DbFree(db, pParse->aLabel);
135046 if( pParse->pConstExpr ){
135047 sqlite3ExprListDelete(db, pParse->pConstExpr);
135049 assert( db->lookaside.bDisable >= pParse->disableLookaside );
135050 db->lookaside.bDisable -= pParse->disableLookaside;
135051 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
135052 assert( pParse->db->pParse==pParse );
135053 db->pParse = pParse->pOuterParse;
135054 pParse->db = 0;
135055 pParse->disableLookaside = 0;
135066 ** common cleanups, we save a single NULL-pointer comparison in
135071 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
135073 ** use-after-free errors following an OOM. The preferred way to do this is
135076 ** testcase( pParse->earlyCleanup );
135091 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
135093 pCleanup->pNext = pParse->pCleanup;
135094 pParse->pCleanup = pCleanup;
135095 pCleanup->pPtr = pPtr;
135096 pCleanup->xCleanup = xCleanup;
135098 xCleanup(pParse->db, pPtr);
135101 pParse->earlyCleanup = 1;
135119 assert( db->pParse!=pParse );
135120 pParse->pOuterParse = db->pParse;
135121 db->pParse = pParse;
135122 pParse->db = db;
135123 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
135135 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
135139 const char *zSql, /* UTF-8 encoded SQL statement. */
135153 sParse.pOuterParse = db->pParse;
135154 db->pParse = &sParse;
135158 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
135159 assert( sqlite3_mutex_held(db->mutex) );
135161 /* For a long-term use prepared statement avoid the use of
135172 ** some other database connection is holding a write-lock, which in
135193 if( !db->noSharedCache ){
135194 for(i=0; i<db->nDb; i++) {
135195 Btree *pBt = db->aDb[i].pBt;
135200 const char *zDb = db->aDb[i].zDbSName;
135202 testcase( db->flags & SQLITE_ReadUncommit );
135211 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135213 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
135224 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
135238 if( db->init.busy==0 ){
135239 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
135241 if( db->mallocFailed ){
135246 if( sParse.checkSchema && db->init.busy==0 ){
135271 sParse.pTriggerPrg = pT->pNext;
135282 const char *zSql, /* UTF-8 encoded SQL statement. */
135299 sqlite3_mutex_enter(db->mutex);
135307 if( rc==SQLITE_OK || db->mallocFailed ) break;
135309 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
135312 assert( (rc&db->errMask)==rc );
135313 db->busyHandler.nBusy = 0;
135314 sqlite3_mutex_leave(db->mutex);
135334 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
135338 assert( sqlite3_mutex_held(db->mutex) );
135340 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
135368 const char *zSql, /* UTF-8 encoded SQL statement. */
135380 const char *zSql, /* UTF-8 encoded SQL statement. */
135386 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
135398 const char *zSql, /* UTF-8 encoded SQL statement. */
135405 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
135422 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
135426 const void *zSql, /* UTF-16 encoded SQL statement. */
135432 /* This function currently works by first transforming the UTF-16
135433 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
135453 sqlite3_mutex_enter(db->mutex);
135456 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
135461 ** equivalent pointer into the UTF-16 string by counting the unicode
135463 ** the same number of characters into the UTF-16 string.
135465 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
135470 sqlite3_mutex_leave(db->mutex);
135484 const void *zSql, /* UTF-16 encoded SQL statement. */
135496 const void *zSql, /* UTF-16 encoded SQL statement. */
135508 const void *zSql, /* UTF-16 encoded SQL statement. */
135559 ** The aDefer[] array is used by the sorter-references optimization. For
135569 ** When the sorter-reference optimization is used, there is one entry in the
135578 int regReturn; /* Register holding block-output return address */
135579 int labelBkOut; /* Start label for the block-output subroutine */
135605 Select *pPrior = p->pPrior;
135606 sqlite3ExprListDelete(db, p->pEList);
135607 sqlite3SrcListDelete(db, p->pSrc);
135608 sqlite3ExprDelete(db, p->pWhere);
135609 sqlite3ExprListDelete(db, p->pGroupBy);
135610 sqlite3ExprDelete(db, p->pHaving);
135611 sqlite3ExprListDelete(db, p->pOrderBy);
135612 sqlite3ExprDelete(db, p->pLimit);
135613 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
135615 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
135616 sqlite3WindowListDelete(db, p->pWinDefn);
135618 while( p->pWin ){
135619 assert( p->pWin->ppThis==&p->pWin );
135620 sqlite3WindowUnlinkFromSelect(p->pWin);
135633 pDest->eDest = (u8)eDest;
135634 pDest->iSDParm = iParm;
135635 pDest->iSDParm2 = 0;
135636 pDest->zAffSdst = 0;
135637 pDest->iSdst = 0;
135638 pDest->nSdst = 0;
135649 SrcList *pSrc, /* the FROM clause -- which tables to scan */
135659 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
135661 assert( pParse->db->mallocFailed );
135666 sqlite3Expr(pParse->db,TK_ASTERISK,0));
135668 pNew->pEList = pEList;
135669 pNew->op = TK_SELECT;
135670 pNew->selFlags = selFlags;
135671 pNew->iLimit = 0;
135672 pNew->iOffset = 0;
135673 pNew->selId = ++pParse->nSelect;
135674 pNew->addrOpenEphm[0] = -1;
135675 pNew->addrOpenEphm[1] = -1;
135676 pNew->nSelectRow = 0;
135677 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
135678 pNew->pSrc = pSrc;
135679 pNew->pWhere = pWhere;
135680 pNew->pGroupBy = pGroupBy;
135681 pNew->pHaving = pHaving;
135682 pNew->pOrderBy = pOrderBy;
135683 pNew->pPrior = 0;
135684 pNew->pNext = 0;
135685 pNew->pLimit = pLimit;
135686 pNew->pWith = 0;
135688 pNew->pWin = 0;
135689 pNew->pWinDefn = 0;
135691 if( pParse->db->mallocFailed ) {
135692 clearSelect(pParse->db, pNew, pNew!=&standin);
135695 assert( pNew->pSrc!=0 || pParse->nErr>0 );
135709 ** Return a pointer to the right-most SELECT statement in a compound.
135712 while( p->pNext ) p = p->pNext;
135737 ** ------- ----- ----- ------------
135738 ** CROSS - - JT_CROSS
135739 ** INNER - - JT_INNER
135740 ** LEFT - - JT_LEFT|JT_OUTER
135741 ** LEFT OUTER - JT_LEFT|JT_OUTER
135742 ** RIGHT - - JT_RIGHT|JT_OUTER
135743 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
135744 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
135745 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
135746 ** NATURAL INNER - JT_NATURAL|JT_INNER
135747 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
135749 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
135751 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
135755 ** of other non-standard and in many cases non-sensical join types.
135760 ** INNER CROSS JOIN -> same as JOIN
135761 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
135762 ** OUTER LEFT JOIN -> same as LEFT JOIN
135763 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
135764 ** LEFT RIGHT JOIN -> same as FULL JOIN
135765 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
135766 ** CROSS CROSS CROSS JOIN -> same as JOIN
135805 if( p->n==aKeyword[j].nChar
135806 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
135834 ** Return the index of a column in a table. Return -1 if the column
135841 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
135842 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
135844 return -1;
135852 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
135853 if( pItem->fg.isNestedFrom ){
135855 assert( pItem->pSelect!=0 );
135856 pResults = pItem->pSelect->pEList;
135858 assert( iCol>=0 && iCol<pResults->nExpr );
135859 pResults->a[iCol].fg.bUsed = 1;
135865 ** table that has a column named zCol. The search is left-to-right.
135875 int iStart, /* First member of pSrc->a[] to check */
135876 int iEnd, /* Last member of pSrc->a[] to check */
135878 int *piTab, /* Write index of pSrc->a[] here */
135879 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
135885 assert( iEnd<pSrc->nSrc );
135890 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
135892 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
135895 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
135937 p->w.iJoin = iTable;
135938 if( p->op==TK_FUNCTION ){
135940 if( p->x.pList ){
135942 for(i=0; i<p->x.pList->nExpr; i++){
135943 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
135947 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
135948 p = p->pRight;
135968 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
135972 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
135975 if( p->op==TK_FUNCTION ){
135977 if( p->x.pList ){
135979 for(i=0; i<p->x.pList->nExpr; i++){
135980 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
135984 unsetJoinExpr(p->pLeft, iTable, nullable);
135985 p = p->pRight;
136002 ** The left most table is the first entry in Select.pSrc. The right-most
136016 pSrc = p->pSrc;
136017 pLeft = &pSrc->a[0];
136019 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
136020 Table *pRightTab = pRight->pTab;
136023 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
136024 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
136029 if( pRight->fg.jointype & JT_NATURAL ){
136031 if( pRight->fg.isUsing || pRight->u3.pOn ){
136036 for(j=0; j<pRightTab->nCol; j++){
136039 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
136040 zName = pRightTab->aCol[j].zCnName;
136044 assert( pUsing->nId>0 );
136045 assert( pUsing->a[pUsing->nId-1].zName==0 );
136046 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
136051 pRight->fg.isUsing = 1;
136052 pRight->fg.isSynthUsing = 1;
136053 pRight->u3.pUsing = pUsing;
136055 if( pParse->nErr ) return 1;
136065 if( pRight->fg.isUsing ){
136066 IdList *pList = pRight->u3.pUsing;
136067 sqlite3 *db = pParse->db;
136069 for(j=0; j<pList->nId; j++){
136078 zName = pList->a[j].zName;
136082 pRight->fg.isSynthUsing)==0
136084 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
136089 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136090 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
136093 ** contains the zName column, then this branch is a no-op.
136100 ** non-USING references to zName on the left of an INNER or LEFT
136107 pRight->fg.isSynthUsing)!=0 ){
136108 if( pSrc->a[iLeft].fg.isUsing==0
136109 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
136117 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136132 pEq->w.iJoin = pE2->iTable;
136134 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
136141 else if( pRight->u3.pOn ){
136142 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
136143 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
136144 pRight->u3.pOn = 0;
136145 pRight->fg.isOn = 1;
136174 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
136175 0, pInfo->ecelFlags);
136177 if( pInfo->pExtra ){
136178 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
136179 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
136197 int nOBSat = pSort->nOBSat;
136198 Vdbe *v = pParse->pVdbe;
136199 int regOut = ++pParse->nMem;
136200 if( pSort->pDeferredRowLoad ){
136201 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
136203 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
136209 ** through regData+nData-1 onto the sorter.
136220 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
136221 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
136222 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
136226 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
136250 regBase = regData - nPrefixReg;
136252 regBase = pParse->nMem + 1;
136253 pParse->nMem += nBase;
136255 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
136256 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
136257 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
136258 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
136261 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
136275 regPrevKey = pParse->nMem+1;
136276 pParse->nMem += pSort->nOBSat;
136277 nKey = nExpr - pSort->nOBSat + bSeq;
136281 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
136284 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
136285 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136286 if( pParse->db->mallocFailed ) return;
136287 pOp->p2 = nKey + nData;
136288 pKI = pOp->p4.pKeyInfo;
136289 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
136290 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
136291 testcase( pKI->nAllField > pKI->nKeyField+2 );
136292 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
136293 pKI->nAllField-pKI->nKeyField-1);
136297 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
136298 pSort->regReturn = ++pParse->nMem;
136299 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
136300 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
136302 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
136306 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
136320 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
136321 ** value is not zero, then it is a label of where to jump. Otherwise,
136325 int iCsr = pSort->iECursor;
136330 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
136337 if( pSort->sortFlags & SORTFLAG_UseSorter ){
136342 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
136343 regBase+nOBSat, nBase-nOBSat);
136346 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
136416 int nResultCol = pEList->nExpr;
136417 Vdbe *v = pParse->pVdbe;
136426 iRet = regPrev = pParse->nMem+1;
136427 pParse->nMem += nResultCol;
136431 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
136432 if( i<nResultCol-1 ){
136439 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
136442 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
136443 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
136478 ** No adjustments necessary. This function is a no-op.
136497 if( pParse->nErr==0
136500 Vdbe *v = pParse->pVdbe;
136502 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
136511 pOp->opcode = OP_Null;
136512 pOp->p1 = 1;
136513 pOp->p2 = iVal;
136520 ** This function is called as part of inner-loop generation for a SELECT
136522 ** determines the expressions, if any, that the sorter-reference
136523 ** optimization should be used for. The sorter-reference optimization
136536 ** for which the sorter-reference optimization should be enabled.
136537 ** Additionally, the pSort->aDefer[] array is populated with entries
136552 for(i=0; i<pEList->nExpr; i++){
136553 struct ExprList_item *pItem = &pEList->a[i];
136554 if( pItem->u.x.iOrderByCol==0 ){
136555 Expr *pExpr = pItem->pExpr;
136557 if( pExpr->op==TK_COLUMN
136558 && pExpr->iColumn>=0
136560 && (pTab = pExpr->y.pTab)!=0
136562 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
136566 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
136569 if( nDefer==ArraySize(pSort->aDefer) ){
136577 nKey = pPk->nKeyCol;
136582 pNew->iTable = pExpr->iTable;
136584 pNew->y.pTab = pExpr->y.pTab;
136585 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
136589 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
136590 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
136591 pSort->aDefer[nDefer].nKey = nKey;
136595 pItem->fg.bSorterRef = 1;
136599 pSort->nDefer = (u8)nDefer;
136608 ** If srcTab is negative, then the p->pEList expressions
136610 ** zero or more, then data is pulled from srcTab and p->pEList is used only
136616 int srcTab, /* Pull data from this table if non-negative */
136623 Vdbe *v = pParse->pVdbe;
136626 int eDest = pDest->eDest; /* How to dispose of results */
136627 int iParm = pDest->iSDParm; /* First argument to disposal method */
136635 ** values for any expressions that are also part of the sort-key are omitted
136641 assert( p->pEList!=0 );
136642 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
136643 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
136646 codeOffset(v, p->iOffset, iContinue);
136651 nResultCol = p->pEList->nExpr;
136653 if( pDest->iSdst==0 ){
136655 nPrefixReg = pSort->pOrderBy->nExpr;
136656 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
136657 pParse->nMem += nPrefixReg;
136659 pDest->iSdst = pParse->nMem+1;
136660 pParse->nMem += nResultCol;
136661 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
136663 ** on the right-hand side of an INSERT contains more result columns than
136667 pParse->nMem += nResultCol;
136669 pDest->nSdst = nResultCol;
136670 regOrig = regResult = pDest->iSdst;
136674 VdbeComment((v, "%s", p->pEList->a[i].zEName));
136691 /* For each expression in p->pEList that is a copy of an expression in
136692 ** the ORDER BY clause (pSort->pOrderBy), set the associated
136694 ** expression within the sort-key that pushOntoSorter() will generate.
136695 ** This allows the p->pEList field to be omitted from the sorted record,
136699 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
136701 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
136702 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
136706 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
136707 if( pExtra && pParse->db->mallocFailed==0 ){
136713 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136714 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
136715 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
136716 pParse->nMem += pExtra->nExpr;
136722 pEList = p->pEList;
136723 for(i=0; i<pEList->nExpr; i++){
136724 if( pEList->a[i].u.x.iOrderByCol>0
136726 || pEList->a[i].fg.bSorterRef
136729 nResultCol--;
136748 if( pExtra ) nResultCol += pExtra->nExpr;
136750 if( p->iLimit
136756 pSort->pDeferredRowLoad = &sRowLoadInfo;
136768 int eType = pDistinct->eTnctType;
136769 int iTab = pDistinct->tabTnct;
136770 assert( nResultCol==p->pEList->nExpr );
136771 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
136772 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
136774 codeOffset(v, p->iOffset, iContinue);
136847 int i2 = pDest->iSDParm2;
136856 regResult+(i2<0), nResultCol-(i2<0), r1);
136881 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
136883 r1, pDest->zAffSdst, nResultCol);
136905 assert( nResultCol<=pDest->nSdst );
136909 assert( nResultCol==pDest->nSdst );
136917 case SRT_Coroutine: /* Send data to a co-routine */
136925 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
136934 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
136935 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
136936 ** pSO->nExpr columns, then make sure all keys are unique by adding a
136945 pSO = pDest->pOrderBy;
136947 nKey = pSO->nExpr;
136966 regResult + pSO->a[i].u.x.iOrderByCol - 1,
136984 ** user-defined functions that have side effects. We do not care
136998 if( pSort==0 && p->iLimit ){
136999 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
137008 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
137011 p->aSortFlags = (u8*)&p->aColl[N+X];
137012 p->nKeyField = (u16)N;
137013 p->nAllField = (u16)(N+X);
137014 p->enc = ENC(db);
137015 p->db = db;
137016 p->nRef = 1;
137029 assert( p->nRef>0 );
137030 p->nRef--;
137031 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
137040 assert( p->nRef>0 );
137041 p->nRef++;
137053 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
137079 sqlite3 *db = pParse->db;
137082 nExpr = pList->nExpr;
137083 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
137086 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
137087 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
137088 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
137111 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
137114 ** "USE TEMP B-TREE FOR xxx"
137120 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
137124 ** Assign expression b to lvalue a. A second, no-op, version of this macro
137133 /* No-op versions of the explainXXX() functions and macros. */
137140 ** If the inner loop was generated using a non-null pOrderBy argument,
137152 Vdbe *v = pParse->pVdbe; /* The prepared statement */
137153 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
137158 ExprList *pOrderBy = pSort->pOrderBy;
137159 int eDest = pDest->eDest;
137160 int iParm = pDest->iSDParm;
137169 struct ExprList_item *aOutEx = p->pEList->a;
137172 if( pSort->labelBkOut ){
137173 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
137175 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
137179 /* Open any cursors needed for sorter-reference expressions */
137180 for(i=0; i<pSort->nDefer; i++){
137181 Table *pTab = pSort->aDefer[i].pTab;
137182 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137183 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
137184 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
137188 iTab = pSort->iECursor;
137190 if( eDest==SRT_Mem && p->iOffset ){
137191 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
137194 regRow = pDest->iSdst;
137204 nKey = pOrderBy->nExpr - pSort->nOBSat;
137205 if( pSort->sortFlags & SORTFLAG_UseSorter ){
137206 int regSortOut = ++pParse->nMem;
137207 iSortTab = pParse->nTab++;
137208 if( pSort->labelBkOut ){
137216 codeOffset(v, p->iOffset, addrContinue);
137221 codeOffset(v, p->iOffset, addrContinue);
137225 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137232 if( pSort->nDefer ){
137236 for(i=0; i<pSort->nDefer; i++){
137237 int iCsr = pSort->aDefer[i].iCsr;
137238 Table *pTab = pSort->aDefer[i].pTab;
137239 int nKey = pSort->aDefer[i].nKey;
137249 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
137262 for(i=nColumn-1; i>=0; i--){
137271 iRead = aOutEx[i].u.x.iOrderByCol-1;
137273 iRead = iCol--;
137290 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
137292 pDest->zAffSdst, nColumn);
137302 int i2 = pDest->iSDParm2;
137304 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
137317 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
137319 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
137335 if( pSort->sortFlags & SORTFLAG_UseSorter ){
137340 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
137355 ** result-set expression in all of the following SELECT statements is
137366 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
137393 assert( pNC->pSrcList!=0 );
137394 switch( pExpr->op ){
137402 int iCol = pExpr->iColumn; /* Index of column in pTab */
137404 SrcList *pTabList = pNC->pSrcList;
137405 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
137406 if( j<pTabList->nSrc ){
137407 pTab = pTabList->a[j].pTab;
137408 pS = pTabList->a[j].pSelect;
137410 pNC = pNC->pNext;
137425 ** sub-select. In this case, set the column type to NULL, even
137435 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
137437 /* The "table" is actually a sub-select or a view in the FROM clause
137439 ** data for the result-set column of the sub-select.
137441 if( iCol<pS->pEList->nExpr
137449 ** rowid of the sub-select or view. This expression is legal (see
137450 ** test case misc2.2.2) - it always evaluates to NULL.
137453 Expr *p = pS->pEList->a[iCol].pExpr;
137454 sNC.pSrcList = pS->pSrc;
137456 sNC.pParse = pNC->pParse;
137463 if( iCol<0 ) iCol = pTab->iPKey;
137464 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
137469 zOrigCol = pTab->aCol[iCol].zCnName;
137470 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137472 zOrigTab = pTab->zName;
137473 if( pNC->pParse && pTab->pSchema ){
137474 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
137475 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
137478 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
137482 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137490 /* The expression is a sub-select. Return the declaration type and
137498 pS = pExpr->x.pSelect;
137499 p = pS->pEList->a[0].pExpr;
137500 sNC.pSrcList = pS->pSrc;
137502 sNC.pParse = pNC->pParse;
137530 Vdbe *v = pParse->pVdbe;
137536 for(i=0; i<pEList->nExpr; i++){
137537 Expr *p = pEList->a[i].pExpr;
137545 /* The vdbe must make its own copy of the column-type and other
137595 Vdbe *v = pParse->pVdbe;
137600 sqlite3 *db = pParse->db;
137606 if( pParse->explain ){
137611 if( pParse->colNamesSet ) return;
137612 /* Column names are determined by the left-most term of a compound select */
137613 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137615 pTabList = pSelect->pSrc;
137616 pEList = pSelect->pEList;
137619 pParse->colNamesSet = 1;
137620 fullName = (db->flags & SQLITE_FullColNames)!=0;
137621 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
137622 sqlite3VdbeSetNumCols(v, pEList->nExpr);
137623 for(i=0; i<pEList->nExpr; i++){
137624 Expr *p = pEList->a[i].pExpr;
137627 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
137628 assert( p->op!=TK_COLUMN
137629 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
137630 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
137632 char *zName = pEList->a[i].zEName;
137634 }else if( srcName && p->op==TK_COLUMN ){
137636 int iCol = p->iColumn;
137637 pTab = p->y.pTab;
137639 if( iCol<0 ) iCol = pTab->iPKey;
137640 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
137644 zCol = pTab->aCol[iCol].zCnName;
137648 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
137654 const char *z = pEList->a[i].zEName;
137690 sqlite3 *db = pParse->db; /* Database connection */
137702 nCol = pEList->nExpr;
137714 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
137715 struct ExprList_item *pX = &pEList->a[i];
137719 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
137722 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
137723 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
137724 pColExpr = pColExpr->pRight;
137727 if( pColExpr->op==TK_COLUMN
137729 && ALWAYS( pColExpr->y.pTab!=0 )
137732 int iCol = pColExpr->iColumn;
137733 pTab = pColExpr->y.pTab;
137734 if( iCol<0 ) iCol = pTab->iPKey;
137735 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
137736 }else if( pColExpr->op==TK_ID ){
137738 zName = pColExpr->u.zToken;
137741 assert( zName==pX->zEName ); /* pointer comparison intended */
137755 if( pCollide->fg.bUsingTerm ){
137756 pCol->colFlags |= COLFLAG_NOEXPAND;
137760 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
137766 pCol->zCnName = zName;
137767 pCol->hName = sqlite3StrIHash(zName);
137768 if( pX->fg.bNoExpand ){
137769 pCol->colFlags |= COLFLAG_NOEXPAND;
137777 if( db->mallocFailed ){
137806 sqlite3 *db = pParse->db;
137815 assert( (pSelect->selFlags & SF_Resolved)!=0 );
137816 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
137817 if( db->mallocFailed ) return;
137819 sNC.pSrcList = pSelect->pSrc;
137820 a = pSelect->pEList->a;
137821 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
137824 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
137827 /* pCol->szEst = ... // Column size est for SELECT tables never used */
137828 pCol->affinity = sqlite3ExprAffinity(p);
137831 n = sqlite3Strlen30(pCol->zCnName);
137832 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
137833 if( pCol->zCnName ){
137834 memcpy(&pCol->zCnName[n+1], zType, m+1);
137835 pCol->colFlags |= COLFLAG_HASTYPE;
137837 testcase( pCol->colFlags & COLFLAG_HASTYPE );
137838 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
137841 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
137844 assert( pTab->pIndex==0 );
137845 sqlite3ColumnSetColl(db, pCol, pColl->zName);
137848 pTab->szTabRow = 1; /* Any non-zero value works */
137857 sqlite3 *db = pParse->db;
137860 savedFlags = db->flags;
137861 db->flags &= ~(u64)SQLITE_FullColNames;
137862 db->flags |= SQLITE_ShortColNames;
137864 db->flags = savedFlags;
137865 if( pParse->nErr ) return 0;
137866 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137871 pTab->nTabRef = 1;
137872 pTab->zName = 0;
137873 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
137874 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
137876 pTab->iPKey = -1;
137877 if( db->mallocFailed ){
137889 if( pParse->pVdbe ){
137890 return pParse->pVdbe;
137892 if( pParse->pToplevel==0
137893 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
137895 pParse->okConstFactor = 1;
137903 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
137911 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
137919 ** Only if pLimit->pLeft!=0 do the limit registers get
137929 Expr *pLimit = p->pLimit;
137931 if( p->iLimit ) return;
137934 ** "LIMIT -1" always shows all rows. There is some
137940 assert( pLimit->op==TK_LIMIT );
137941 assert( pLimit->pLeft!=0 );
137942 p->iLimit = iLimit = ++pParse->nMem;
137945 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
137950 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
137951 p->nSelectRow = sqlite3LogEst((u64)n);
137952 p->selFlags |= SF_FixedLimit;
137955 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
137960 if( pLimit->pRight ){
137961 p->iOffset = iOffset = ++pParse->nMem;
137962 pParse->nMem++; /* Allocate an extra register for limit+offset */
137963 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
137974 ** Return the appropriate collating sequence for the iCol-th column of
137975 ** the result set for the compound-select statement "p". Return NULL if
137979 ** left-most term of the select that has a collating sequence.
137983 if( p->pPrior ){
137984 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
137989 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
137992 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
137993 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
138008 ExprList *pOrderBy = p->pOrderBy;
138009 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
138010 sqlite3 *db = pParse->db;
138015 struct ExprList_item *pItem = &pOrderBy->a[i];
138016 Expr *pTerm = pItem->pExpr;
138019 if( pTerm->flags & EP_Collate ){
138022 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
138023 if( pColl==0 ) pColl = db->pDfltColl;
138024 pOrderBy->a[i].pExpr =
138025 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
138028 pRet->aColl[i] = pColl;
138029 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
138041 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
138043 ** p->pPrior p
138046 ** There is exactly one reference to the recursive-table in the FROM clause
138047 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
138049 ** The setup-query runs once to generate an initial set of rows that go
138053 ** recursive-table for a recursive-query run. The output of the recursive-query
138078 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
138079 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
138080 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
138082 Select *pFirstRec; /* Left-most recursive term */
138098 if( p->pWin ){
138109 p->nSelectRow = 320; /* 4 billion rows */
138111 pLimit = p->pLimit;
138112 regLimit = p->iLimit;
138113 regOffset = p->iOffset;
138114 p->pLimit = 0;
138115 p->iLimit = p->iOffset = 0;
138116 pOrderBy = p->pOrderBy;
138119 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
138120 if( pSrc->a[i].fg.isRecursive ){
138121 iCurrent = pSrc->a[i].iCursor;
138129 iQueue = pParse->nTab++;
138130 if( p->op==TK_UNION ){
138132 iDistinct = pParse->nTab++;
138139 regCurrent = ++pParse->nMem;
138143 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
138151 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
138152 p->selFlags |= SF_UsesEphemeral;
138156 p->pOrderBy = 0;
138162 ** iDistinct table. pFirstRec is left pointing to the left-most
138165 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
138166 if( pFirstRec->selFlags & SF_Aggregate ){
138170 pFirstRec->op = TK_ALL;
138171 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
138174 /* Store the results of the setup-query in Queue. */
138175 pSetup = pFirstRec->pPrior;
138176 pSetup->pNext = 0;
138179 pSetup->pNext = p;
138188 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
138206 ** the value for the recursive-table. Store the results in the Queue.
138208 pFirstRec->pPrior = 0;
138211 assert( pFirstRec->pPrior==0 );
138212 pFirstRec->pPrior = pSetup;
138219 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
138220 p->pOrderBy = pOrderBy;
138221 p->pLimit = pLimit;
138229 Select *p, /* The right-most of SELECTs to be coded */
138234 ** Handle the special case of a compound-select that originates from a
138247 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
138251 Select *p, /* The right-most of SELECTs to be coded */
138256 int bShowAll = p->pLimit==0;
138257 assert( p->selFlags & SF_MultiValue );
138259 assert( p->selFlags & SF_Values );
138260 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
138261 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
138263 if( p->pWin ) return -1;
138265 if( p->pPrior==0 ) break;
138266 assert( p->pPrior->pNext==p );
138267 p = p->pPrior;
138273 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
138275 p->nSelectRow = nRow;
138276 p = p->pNext;
138287 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
138296 ** "p" points to the right-most of the two queries. the query on the
138297 ** left is p->pPrior. The left query could also be a compound query
138303 ** Example 1: Consider a three-way compound SQL statement.
138311 ** `-----> SELECT b FROM t2
138313 ** `------> SELECT a FROM t1
138317 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
138324 Select *p, /* The right-most of SELECTs to be coded */
138335 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
138337 assert( p && p->pPrior ); /* Calling function guarantees this much */
138338 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
138339 assert( p->selFlags & SF_Compound );
138340 db = pParse->db;
138341 pPrior = p->pPrior;
138343 assert( pPrior->pOrderBy==0 );
138344 assert( pPrior->pLimit==0 );
138352 assert( p->pEList );
138353 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
138357 /* Special handling for a compound-select that originates as a VALUES clause.
138359 if( p->selFlags & SF_MultiValue ){
138368 assert( p->pEList && pPrior->pEList );
138369 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
138372 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
138379 if( p->pOrderBy ){
138384 if( pPrior->pPrior==0 ){
138386 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
138392 switch( p->op ){
138396 assert( !pPrior->pLimit );
138397 pPrior->iLimit = p->iLimit;
138398 pPrior->iOffset = p->iOffset;
138399 pPrior->pLimit = p->pLimit;
138402 pPrior->pLimit = 0;
138406 p->pPrior = 0;
138407 p->iLimit = pPrior->iLimit;
138408 p->iOffset = pPrior->iOffset;
138409 if( p->iLimit ){
138410 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
138412 if( p->iOffset ){
138414 p->iLimit, p->iOffset+1, p->iOffset);
138421 pDelete = p->pPrior;
138422 p->pPrior = pPrior;
138423 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138424 if( p->pLimit
138425 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
138426 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
138428 p->nSelectRow = sqlite3LogEst((u64)nLimit);
138440 Expr *pLimit; /* Saved values of p->nLimit */
138444 testcase( p->op==TK_EXCEPT );
138445 testcase( p->op==TK_UNION );
138451 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
138457 unionTab = pParse->nTab++;
138458 assert( p->pOrderBy==0 );
138460 assert( p->addrOpenEphm[0] == -1 );
138461 p->addrOpenEphm[0] = addr;
138462 findRightmost(p)->selFlags |= SF_UsesEphemeral;
138463 assert( p->pEList );
138469 assert( !pPrior->pOrderBy );
138479 if( p->op==TK_EXCEPT ){
138482 assert( p->op==TK_UNION );
138485 p->pPrior = 0;
138486 pLimit = p->pLimit;
138487 p->pLimit = 0;
138489 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
138490 sqlite3SelectOpName(p->op)));
138494 assert( p->pOrderBy==0 );
138495 pDelete = p->pPrior;
138496 p->pPrior = pPrior;
138497 p->pOrderBy = 0;
138498 if( p->op==TK_UNION ){
138499 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138501 sqlite3ExprDelete(db, p->pLimit);
138502 p->pLimit = pLimit;
138503 p->iLimit = 0;
138504 p->iOffset = 0;
138510 assert( p->pEList || db->mallocFailed );
138511 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
138527 default: assert( p->op==TK_INTERSECT ); {
138539 tab1 = pParse->nTab++;
138540 tab2 = pParse->nTab++;
138541 assert( p->pOrderBy==0 );
138544 assert( p->addrOpenEphm[0] == -1 );
138545 p->addrOpenEphm[0] = addr;
138546 findRightmost(p)->selFlags |= SF_UsesEphemeral;
138547 assert( p->pEList );
138561 assert( p->addrOpenEphm[1] == -1 );
138562 p->addrOpenEphm[1] = addr;
138563 p->pPrior = 0;
138564 pLimit = p->pLimit;
138565 p->pLimit = 0;
138567 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
138568 sqlite3SelectOpName(p->op)));
138572 pDelete = p->pPrior;
138573 p->pPrior = pPrior;
138574 if( p->nSelectRow>pPrior->nSelectRow ){
138575 p->nSelectRow = pPrior->nSelectRow;
138577 sqlite3ExprDelete(db, p->pLimit);
138578 p->pLimit = pLimit;
138584 assert( p->pEList );
138606 if( p->pNext==0 ){
138611 if( pParse->nErr ) goto multi_select_end;
138617 ** This section is run by the right-most SELECT statement only.
138618 ** SELECT statements to the left always skip this part. The right-most
138622 if( p->selFlags & SF_UsesEphemeral ){
138626 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
138629 assert( p->pNext==0 );
138630 assert( p->pEList!=0 );
138631 nCol = p->pEList->nExpr;
138637 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
138640 *apColl = db->pDfltColl;
138644 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
138646 int addr = pLoop->addrOpenEphm[i];
138650 assert( pLoop->addrOpenEphm[1]<0 );
138656 pLoop->addrOpenEphm[i] = -1;
138663 pDest->iSdst = dest.iSdst;
138664 pDest->nSdst = dest.nSdst;
138679 if( p->selFlags & SF_Values ){
138684 sqlite3SelectOpName(p->op));
138692 ** The data to be output is contained in pIn->iSdst. There are
138693 ** pIn->nSdst columns to be output. pDest is where the output should
138705 ** If the LIMIT found in p->iLimit is reached, jump immediately to
138718 Vdbe *v = pParse->pVdbe;
138730 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
138734 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
138737 if( pParse->db->mallocFailed ) return 0;
138741 codeOffset(v, p->iOffset, iContinue);
138743 assert( pDest->eDest!=SRT_Exists );
138744 assert( pDest->eDest!=SRT_Table );
138745 switch( pDest->eDest ){
138751 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
138752 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
138753 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
138765 testcase( pIn->nSdst>1 );
138767 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
138768 r1, pDest->zAffSdst, pIn->nSdst);
138769 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
138770 pIn->iSdst, pIn->nSdst);
138778 ** if it is the RHS of a row-value IN operator.
138781 testcase( pIn->nSdst>1 );
138782 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
138789 ** starting at pDest->iSdst. Then the co-routine yields.
138792 if( pDest->iSdst==0 ){
138793 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
138794 pDest->nSdst = pIn->nSdst;
138796 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
138797 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
138810 assert( pDest->eDest==SRT_Output );
138811 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
138818 if( p->iLimit ){
138819 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
138840 ** co-routines. Then run the co-routines in parallel and merge the results
138867 ** ------------- ----------------- -------------- -----------------
138918 Select *p, /* The right-most of SELECTs to be coded */
138923 Select *pSplit; /* Left-most SELECT in the right-hand group */
138928 int regAddrA; /* Address register for select-A coroutine */
138929 int regAddrB; /* Address register for select-B coroutine */
138930 int addrSelectA; /* Address of the select-A coroutine */
138931 int addrSelectB; /* Address of the select-B coroutine */
138932 int regOutA; /* Address register for the output-A subroutine */
138933 int regOutB; /* Address register for the output-B subroutine */
138934 int addrOutA; /* Address of the output-A subroutine */
138935 int addrOutB = 0; /* Address of the output-B subroutine */
138936 int addrEofA; /* Address of the select-A-exhausted subroutine */
138938 int addrEofB; /* Address of the select-B-exhausted subroutine */
138942 int regLimitA; /* Limit register for select-A */
138943 int regLimitB; /* Limit register for select-A */
138945 int savedLimit; /* Saved value of p->iLimit */
138946 int savedOffset; /* Saved value of p->iOffset */
138947 int labelCmpr; /* Label for the start of the merge algorithm */
138948 int labelEnd; /* Label for the end of the overall SELECT stmt */
138958 assert( p->pOrderBy!=0 );
138960 db = pParse->db;
138961 v = pParse->pVdbe;
138969 op = p->op;
138970 assert( p->pPrior->pOrderBy==0 );
138971 pOrderBy = p->pOrderBy;
138973 nOrderBy = pOrderBy->nExpr;
138980 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
138982 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
138984 assert( pItem->u.x.iOrderByCol>0 );
138985 if( pItem->u.x.iOrderByCol==i ) break;
138990 pNew->flags |= EP_IntValue;
138991 pNew->u.iValue = i;
138992 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
138993 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
139009 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
139011 assert( pItem->u.x.iOrderByCol>0 );
139012 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
139013 aPermute[i] = pItem->u.x.iOrderByCol - 1;
139027 int nExpr = p->pEList->nExpr;
139028 assert( nOrderBy>=nExpr || db->mallocFailed );
139029 regPrev = pParse->nMem+1;
139030 pParse->nMem += nExpr+1;
139036 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
139037 pKeyDup->aSortFlags[i] = 0;
139048 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
139050 assert( pSplit->pPrior->pNext==pSplit );
139057 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
139059 pPrior = pSplit->pPrior;
139061 pSplit->pPrior = 0;
139062 pPrior->pNext = 0;
139063 assert( p->pOrderBy == pOrderBy );
139064 assert( pOrderBy!=0 || db->mallocFailed );
139065 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
139066 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
139067 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
139071 if( p->iLimit && op==TK_ALL ){
139072 regLimitA = ++pParse->nMem;
139073 regLimitB = ++pParse->nMem;
139074 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
139080 sqlite3ExprDelete(db, p->pLimit);
139081 p->pLimit = 0;
139083 regAddrA = ++pParse->nMem;
139084 regAddrB = ++pParse->nMem;
139085 regOutA = ++pParse->nMem;
139086 regOutB = ++pParse->nMem;
139090 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
139093 ** left of the compound operator - the "A" select.
139098 pPrior->iLimit = regLimitA;
139105 ** the right - the "B" select
139110 savedLimit = p->iLimit;
139111 savedOffset = p->iOffset;
139112 p->iLimit = regLimitB;
139113 p->iOffset = 0;
139116 p->iLimit = savedLimit;
139117 p->iOffset = savedOffset;
139145 VdbeNoopComment((v, "eof-A subroutine"));
139150 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
139158 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
139160 VdbeNoopComment((v, "eof-B subroutine"));
139168 VdbeNoopComment((v, "A-lt-B subroutine"));
139181 VdbeNoopComment((v, "A-eq-B subroutine"));
139189 VdbeNoopComment((v, "A-gt-B subroutine"));
139218 if( pSplit->pPrior ){
139219 sqlite3SelectDelete(db, pSplit->pPrior);
139221 pSplit->pPrior = pPrior;
139222 pPrior->pNext = pSplit;
139223 sqlite3ExprListDelete(db, pPrior->pOrderBy);
139224 pPrior->pOrderBy = 0;
139229 return pParse->nErr!=0;
139244 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
139267 ** when processing a non-matched row of the left.
139283 ** a column in table number iTable with a copy of the iColumn-th
139300 && pExpr->w.iJoin==pSubst->iTable
139303 pExpr->w.iJoin = pSubst->iNewTable;
139305 if( pExpr->op==TK_COLUMN
139306 && pExpr->iTable==pSubst->iTable
139310 if( pExpr->iColumn<0 ){
139311 pExpr->op = TK_NULL;
139316 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
139318 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
139319 assert( pExpr->pRight==0 );
139321 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139323 sqlite3 *db = pSubst->pParse->db;
139324 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
139328 ifNullRow.iTable = pSubst->iNewTable;
139334 if( db->mallocFailed ){
139338 if( pSubst->isOuterJoin ){
139342 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
139343 pExpr->flags & (EP_OuterON|EP_InnerON));
139347 if( pExpr->op==TK_TRUEFALSE ){
139348 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
139349 pExpr->op = TK_INTEGER;
139354 ** just as it did when it was a column of a view or sub-query. */
139355 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
139356 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
139357 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
139358 (pColl ? pColl->zName : "BINARY")
139365 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
139366 pExpr->iTable = pSubst->iNewTable;
139368 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
139369 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
139371 substSelect(pSubst, pExpr->x.pSelect, 1);
139373 substExprList(pSubst, pExpr->x.pList);
139377 Window *pWin = pExpr->y.pWin;
139378 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
139379 substExprList(pSubst, pWin->pPartition);
139380 substExprList(pSubst, pWin->pOrderBy);
139392 for(i=0; i<pList->nExpr; i++){
139393 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
139399 int doPrior /* Do substitutes on p->pPrior too */
139406 substExprList(pSubst, p->pEList);
139407 substExprList(pSubst, p->pGroupBy);
139408 substExprList(pSubst, p->pOrderBy);
139409 p->pHaving = substExpr(pSubst, p->pHaving);
139410 p->pWhere = substExpr(pSubst, p->pWhere);
139411 pSrc = p->pSrc;
139413 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
139414 substSelect(pSubst, pItem->pSelect, 1);
139415 if( pItem->fg.isTabFunc ){
139416 substExprList(pSubst, pItem->u1.pFuncArg);
139419 }while( doPrior && (p = p->pPrior)!=0 );
139429 ** pSrcItem->colUsed mask.
139433 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
139434 pItem = pWalker->u.pSrcItem;
139435 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
139436 if( pExpr->iColumn<0 ) return WRC_Continue;
139437 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
139445 if( NEVER(pSrcItem->pTab==0) ) return;
139450 pSrcItem->colUsed = 0;
139466 ** If pSrc contains any sub-selects, call this routine recursively
139467 ** on the FROM clause of each such sub-select, with iExcept set to -1.
139477 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
139480 assert( pItem->iCursor < aCsrMap[0] );
139481 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
139482 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
139484 pItem->iCursor = aCsrMap[pItem->iCursor+1];
139485 for(p=pItem->pSelect; p; p=p->pPrior){
139486 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
139496 int *aCsrMap = pWalker->u.aiCol;
139508 int op = pExpr->op;
139510 renumberCursorDoMapping(pWalker, &pExpr->iTable);
139513 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
139521 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
139543 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
139588 ** other than the one FROM-clause subquery that is a candidate
139590 ** from 2015-02-09.)
139603 ** sub-queries that were excluded from this optimization. Restriction
139618 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
139619 ** accidently carried the comment forward until 2014-09-15. Original
139654 ** The parent and sub-query may contain WHERE clauses. Subject to
139661 ** Also, each component of the sub-query must return the same number
139664 ** such (illegal) sub-query is flattened. The caller will detect the
139667 ** (18) If the sub-query is a compound select, then all terms of the
139674 ** (20) If the sub-query is a compound select, then it must not use
139685 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
139691 ** The subquery may not be an aggregate that uses the built-in min() or
139712 ** (29) Either the subquery is not the right-hand operand of a join with an
139713 ** ON or USING clause nor the right-hand operand of a NATURAL JOIN, or
139714 ** the right-most table within the FROM clause of the subquery
139719 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
139722 ** If flattening is not attempted, this routine is a no-op and returns 0.
139731 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
139734 const char *zSavedAuthContext = pParse->zAuthContext;
139737 Select *pSub1; /* Pointer to the rightmost select in sub-query */
139741 int iNewParent = -1;/* Replacement table for iParent */
139746 sqlite3 *db = pParse->db;
139753 assert( p->pPrior==0 );
139755 pSrc = p->pSrc;
139756 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
139757 pSubitem = &pSrc->a[iFrom];
139758 iParent = pSubitem->iCursor;
139759 pSub = pSubitem->pSelect;
139763 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
139766 pSubSrc = pSub->pSrc;
139770 ** because they could be computed at compile-time. But when LIMIT and OFFSET
139773 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
139774 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
139775 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
139778 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
139779 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
139780 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
139783 if( p->pOrderBy && pSub->pOrderBy ){
139786 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
139787 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
139788 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
139791 if( pSub->selFlags & (SF_Recursive) ){
139810 ** aggregates are processed - there is no mechanism to determine if
139811 ** the LEFT JOIN table should be all-NULL.
139815 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
139816 if( pSubSrc->nSrc>1 /* (3a) */
139818 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
139819 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
139820 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
139828 /* Setting isOuterJoin to -1 causes OP_IfNullRow opcodes to be generated for
139830 ** though they are not necessary. This will stress-test the OP_IfNullRow
139832 isOuterJoin = -1;
139836 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
139837 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139840 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
139851 ** (29a) The right-most entry in the FROM clause of the subquery
139861 if( pSubSrc->nSrc>=2
139862 && (pSubSrc->a[pSubSrc->nSrc-1].fg.jointype & JT_OUTER)!=0
139864 if( (pSubitem->fg.jointype & JT_NATURAL)!=0
139865 || pSubitem->fg.isUsing
139866 || NEVER(pSubitem->u3.pOn!=0) /* ON clause already shifted into WHERE */
139867 || pSubitem->fg.isOn
139873 /* Restriction (17): If the sub-query is a compound SELECT, then it must
139878 if( pSub->pPrior ){
139879 if( pSub->pOrderBy ){
139882 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
139885 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
139886 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
139887 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
139888 assert( pSub->pSrc!=0 );
139889 assert( (pSub->selFlags & SF_Recursive)==0 );
139890 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
139891 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
139892 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
139893 || pSub1->pSrc->nSrc<1 /* (17c) */
139895 || pSub1->pWin /* (17e) */
139900 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139902 ** omitted on left-hand tables of the right join that is being
139906 testcase( pSub1->pSrc->nSrc>1 );
139910 if( p->pOrderBy ){
139912 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
139913 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
139918 if( (p->selFlags & SF_Recursive) ) return 0;
139920 if( pSrc->nSrc>1 ){
139921 if( pParse->nSelect>500 ) return 0;
139923 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
139924 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
139930 pSub->selId, pSub, iFrom));
139933 pParse->zAuthContext = pSubitem->zName;
139936 pParse->zAuthContext = zSavedAuthContext;
139939 pSub1 = pSubitem->pSelect;
139940 sqlite3DbFree(db, pSubitem->zDatabase);
139941 sqlite3DbFree(db, pSubitem->zName);
139942 sqlite3DbFree(db, pSubitem->zAlias);
139943 pSubitem->zDatabase = 0;
139944 pSubitem->zName = 0;
139945 pSubitem->zAlias = 0;
139946 pSubitem->pSelect = 0;
139947 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
139949 /* If the sub-query is a compound SELECT statement, then (by restrictions
139953 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
139956 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
139957 ** OFFSET clauses and joins them to the left-hand-side of the original
139959 ** select statements in the compound sub-query.
139980 ** We call this the "compound-subquery flattening".
139982 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
139984 ExprList *pOrderBy = p->pOrderBy;
139985 Expr *pLimit = p->pLimit;
139986 Select *pPrior = p->pPrior;
139987 Table *pItemTab = pSubitem->pTab;
139988 pSubitem->pTab = 0;
139989 p->pOrderBy = 0;
139990 p->pPrior = 0;
139991 p->pLimit = 0;
139993 p->pLimit = pLimit;
139994 p->pOrderBy = pOrderBy;
139995 p->op = TK_ALL;
139996 pSubitem->pTab = pItemTab;
139998 p->pPrior = pPrior;
140000 pNew->selId = ++pParse->nSelect;
140001 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
140004 pNew->pPrior = pPrior;
140005 if( pPrior ) pPrior->pNext = pNew;
140006 pNew->pNext = p;
140007 p->pPrior = pNew;
140008 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
140009 " creates %u as peer\n",pNew->selId));
140011 assert( pSubitem->pSelect==0 );
140014 if( db->mallocFailed ){
140015 pSubitem->pSelect = pSub1;
140024 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
140026 if( ALWAYS(pSubitem->pTab!=0) ){
140027 Table *pTabToDel = pSubitem->pTab;
140028 if( pTabToDel->nTabRef==1 ){
140033 testcase( pToplevel->earlyCleanup );
140035 pTabToDel->nTabRef--;
140037 pSubitem->pTab = 0;
140040 /* The following loop runs once for each term in a compound-subquery
140042 ** of flattening - a flattening other than a compound-subquery flattening -
140054 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
140057 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
140059 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
140060 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
140061 pSrc = pParent->pSrc; /* FROM clause of the outer query */
140064 jointype = pSubitem->fg.jointype; /* First time through the loop */
140083 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
140085 pParent->pSrc = pSrc;
140092 SrcItem *pItem = &pSrc->a[i+iFrom];
140093 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
140094 assert( pItem->fg.isTabFunc==0 );
140095 *pItem = pSubSrc->a[i];
140096 pItem->fg.jointype |= ltorj;
140097 iNewParent = pSubSrc->a[i].iCursor;
140098 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
140100 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
140101 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
140115 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
140116 /* At this point, any non-zero iOrderByCol values indicate that the
140123 ** function attempts to flatten a compound sub-query into pParent
140124 ** (the only way this can happen is if the compound sub-query is
140125 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
140126 ExprList *pOrderBy = pSub->pOrderBy;
140127 for(i=0; i<pOrderBy->nExpr; i++){
140128 pOrderBy->a[i].u.x.iOrderByCol = 0;
140130 assert( pParent->pOrderBy==0 );
140131 pParent->pOrderBy = pOrderBy;
140132 pSub->pOrderBy = 0;
140134 pWhere = pSub->pWhere;
140135 pSub->pWhere = 0;
140140 if( pParent->pWhere ){
140141 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
140143 pParent->pWhere = pWhere;
140146 if( db->mallocFailed==0 ){
140152 x.pEList = pSub->pEList;
140158 pParent->selFlags |= pSub->selFlags & SF_Compound;
140159 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
140167 if( pSub->pLimit ){
140168 pParent->pLimit = pSub->pLimit;
140169 pSub->pLimit = 0;
140175 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140204 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
140228 assert( pColumn->op==TK_COLUMN );
140233 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
140237 /* 2018-10-25 ticket [cf5ed20f]
140239 for(i=0; i<pConst->nConst; i++){
140240 const Expr *pE2 = pConst->apExpr[i*2];
140241 assert( pE2->op==TK_COLUMN );
140242 if( pE2->iTable==pColumn->iTable
140243 && pE2->iColumn==pColumn->iColumn
140249 pConst->bHasAffBlob = 1;
140252 pConst->nConst++;
140253 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
140254 pConst->nConst*2*sizeof(Expr*));
140255 if( pConst->apExpr==0 ){
140256 pConst->nConst = 0;
140258 pConst->apExpr[pConst->nConst*2-2] = pColumn;
140259 pConst->apExpr[pConst->nConst*2-1] = pValue;
140266 ** is part of the AND-connected terms of the expression. For each term
140272 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
140277 if( pExpr->op==TK_AND ){
140278 findConstInWhere(pConst, pExpr->pRight);
140279 findConstInWhere(pConst, pExpr->pLeft);
140282 if( pExpr->op!=TK_EQ ) return;
140283 pRight = pExpr->pRight;
140284 pLeft = pExpr->pLeft;
140287 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
140290 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
140299 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
140301 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
140310 if( pConst->pOomFault[0] ) return WRC_Prune;
140311 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
140312 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
140318 for(i=0; i<pConst->nConst; i++){
140319 Expr *pColumn = pConst->apExpr[i*2];
140321 if( pColumn->iTable!=pExpr->iTable ) continue;
140322 if( pColumn->iColumn!=pExpr->iColumn ) continue;
140327 pConst->nChng++;
140330 assert( pExpr->pLeft==0 );
140331 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
140332 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
140341 ** any substitutions based on the contents of pWalker->u.pConst should
140347 ** one of the columns in pWalker->u.pConst, or
140352 ** pWalker->u.pConst.
140355 WhereConst *pConst = pWalker->u.pConst;
140360 if( pConst->bHasAffBlob ){
140361 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
140362 || pExpr->op==TK_IS
140364 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
140365 if( pConst->pOomFault[0] ) return WRC_Prune;
140366 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
140367 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
140371 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
140375 ** The WHERE-clause constant propagation optimization.
140378 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
140410 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
140433 x.pOomFault = &pParse->db->mallocFailed;
140439 if( ALWAYS(p->pSrc!=0)
140440 && p->pSrc->nSrc>0
140441 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
140450 findConstInWhere(&x, p->pWhere);
140459 sqlite3WalkExpr(&w, p->pWhere);
140460 sqlite3DbFree(x.pParse->db, x.apExpr);
140471 ** push WHERE clause expression pExpr down to FROM clause sub-query
140478 ** BY clause of all window function used by the sub-query. It is safe
140484 ** * the sub-query uses only one distinct window frame, and
140488 assert( pSubq->pWin->pPartition );
140489 assert( (pSubq->selFlags & SF_MultiPart)==0 );
140490 assert( pSubq->pPrior==0 );
140491 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
140501 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
140505 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
140513 ** (1) (** This restriction was removed on 2017-09-29. We used to
140531 ** of a LEFT JOIN where iCursor is not the right-hand table of that
140550 ** (6b) The inner query is a compound and uses window-functions.
140554 ** all window-functions used by the sub-query. It is safe to
140556 ** window over which any window-function is calculated.
140562 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
140574 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
140575 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
140578 if( pSubq->pPrior ){
140580 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
140581 if( pSel->pWin ) return 0; /* restriction (6b) */
140584 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
140595 for(pX=pSubq; pX; pX=pX->pPrior){
140596 assert( (pX->selFlags & (SF_Recursive))==0 );
140601 if( pSubq->pLimit!=0 ){
140604 while( pWhere->op==TK_AND ){
140605 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
140606 pWhere = pWhere->pLeft;
140612 || pWhere->w.iJoin!=iCursor)
140617 && pWhere->w.iJoin!=iCursor
140625 pSubq->selFlags |= SF_PushDown;
140628 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
140629 unsetJoinExpr(pNew, -1, 1);
140631 x.iTable = pSrc->iCursor;
140632 x.iNewTable = pSrc->iCursor;
140634 x.pEList = pSubq->pEList;
140637 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140638 /* Restriction 6c has prevented push-down in this case */
140639 sqlite3ExprDelete(pParse->db, pNew);
140640 nChng--;
140644 if( pSubq->selFlags & SF_Aggregate ){
140645 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
140647 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
140649 pSubq = pSubq->pPrior;
140680 assert( pFunc->op==TK_AGG_FUNCTION );
140683 pEList = pFunc->x.pList;
140685 || pEList->nExpr!=1
140692 zFunc = pFunc->u.zToken;
140695 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
140705 assert( pOrderBy!=0 || db->mallocFailed );
140706 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
140712 ** The second argument is the associated aggregate-info object. This
140717 ** where table is a database table, not a sub-select or view. If the query
140731 assert( !p->pGroupBy );
140733 if( p->pWhere
140734 || p->pEList->nExpr!=1
140735 || p->pSrc->nSrc!=1
140736 || p->pSrc->a[0].pSelect
140737 || pAggInfo->nFunc!=1
140741 pTab = p->pSrc->a[0].pTab;
140745 pExpr = p->pEList->a[0].pExpr;
140747 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
140748 if( pExpr->pAggInfo!=pAggInfo ) return 0;
140749 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
140750 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
140759 ** If the source-list item passed as an argument was augmented with an
140763 ** pFrom->pIndex and return SQLITE_OK.
140766 Table *pTab = pFrom->pTab;
140767 char *zIndexedBy = pFrom->u1.zIndexedBy;
140770 assert( pFrom->fg.isIndexedBy!=0 );
140772 for(pIdx=pTab->pIndex;
140773 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
140774 pIdx=pIdx->pNext
140778 pParse->checkSchema = 1;
140781 assert( pFrom->fg.isCte==0 );
140782 pFrom->u2.pIBIndex = pIdx;
140817 if( p->pPrior==0 ) return WRC_Continue;
140818 if( p->pOrderBy==0 ) return WRC_Continue;
140819 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
140821 a = p->pOrderBy->a;
140823 /* If iOrderByCol is already non-zero, then it has already been matched
140825 ** SELECT is rewritten for window-functions processing and then passed
140830 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
140831 if( a[i].pExpr->flags & EP_Collate ) break;
140837 pParse = pWalker->pParse;
140838 db = pParse->db;
140845 p->pSrc = pNewSrc;
140846 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
140847 p->op = TK_SELECT;
140848 p->pWhere = 0;
140849 pNew->pGroupBy = 0;
140850 pNew->pHaving = 0;
140851 pNew->pOrderBy = 0;
140852 p->pPrior = 0;
140853 p->pNext = 0;
140854 p->pWith = 0;
140856 p->pWinDefn = 0;
140858 p->selFlags &= ~SF_Compound;
140859 assert( (p->selFlags & SF_Converted)==0 );
140860 p->selFlags |= SF_Converted;
140861 assert( pNew->pPrior!=0 );
140862 pNew->pPrior->pNext = pNew;
140863 pNew->pLimit = 0;
140868 ** Check to see if the FROM clause term pFrom has table-valued function
140870 ** non-zero, since pFrom is not allowed to be a table-valued function.
140873 if( pFrom->fg.isTabFunc ){
140874 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
140884 ** FROM clause element pItem is really a common-table-expression (CTE)
140888 ** If a non-NULL value is returned, set *ppContext to point to the With
140896 const char *zName = pItem->zName;
140898 assert( pItem->zDatabase==0 );
140900 for(p=pWith; p; p=p->pOuter){
140902 for(i=0; i<p->nCte; i++){
140903 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
140905 return &p->a[i];
140908 if( p->bView ) break;
140914 ** with the inner-most WITH clause being at the top of the stack.
140938 if( pParse->nErr==0 ){
140939 assert( pParse->pWith!=pWith );
140940 pWith->pOuter = pParse->pWith;
140941 pParse->pWith = pWith;
140950 ** pParse->pWith linked list). And if currently processing a CTE
140954 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
140969 assert( pFrom->pTab==0 );
140970 if( pParse->pWith==0 ){
140974 if( pParse->nErr ){
140975 /* Prior errors might have left pParse->pWith in a goofy state, so
140979 if( pFrom->zDatabase!=0 ){
140984 if( pFrom->fg.notCte ){
140992 pCte = searchWith(pParse->pWith, pFrom, &pWith);
140994 sqlite3 *db = pParse->db;
140998 Select *pLeft; /* Left-most SELECT statement */
140999 Select *pRecTerm; /* Left-most recursive term */
141001 With *pSavedWith; /* Initial value of pParse->pWith */
141002 int iRecTab = -1; /* Cursor for recursive table */
141005 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
141007 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
141009 if( pCte->zCteErr ){
141010 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
141015 assert( pFrom->pTab==0 );
141018 pCteUse = pCte->pUse;
141020 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
141027 pCteUse->eM10d = pCte->eM10d;
141029 pFrom->pTab = pTab;
141030 pTab->nTabRef = 1;
141031 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
141032 pTab->iPKey = -1;
141033 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141034 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
141035 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
141036 if( db->mallocFailed ) return 2;
141037 pFrom->pSelect->selFlags |= SF_CopyCte;
141038 assert( pFrom->pSelect );
141039 if( pFrom->fg.isIndexedBy ){
141040 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
141043 pFrom->fg.isCte = 1;
141044 pFrom->u2.pCteUse = pCteUse;
141045 pCteUse->nUse++;
141046 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
141047 pCteUse->eM10d = M10d_Yes;
141051 pRecTerm = pSel = pFrom->pSelect;
141052 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
141053 while( bMayRecursive && pRecTerm->op==pSel->op ){
141055 SrcList *pSrc = pRecTerm->pSrc;
141056 assert( pRecTerm->pPrior!=0 );
141057 for(i=0; i<pSrc->nSrc; i++){
141058 SrcItem *pItem = &pSrc->a[i];
141059 if( pItem->zDatabase==0
141060 && pItem->zName!=0
141061 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
141063 pItem->pTab = pTab;
141064 pTab->nTabRef++;
141065 pItem->fg.isRecursive = 1;
141066 if( pRecTerm->selFlags & SF_Recursive ){
141068 "multiple references to recursive table: %s", pCte->zName
141072 pRecTerm->selFlags |= SF_Recursive;
141073 if( iRecTab<0 ) iRecTab = pParse->nTab++;
141074 pItem->iCursor = iRecTab;
141077 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
141078 pRecTerm = pRecTerm->pPrior;
141081 pCte->zCteErr = "circular reference: %s";
141082 pSavedWith = pParse->pWith;
141083 pParse->pWith = pWith;
141084 if( pSel->selFlags & SF_Recursive ){
141087 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
141088 assert( pRecTerm->pNext!=0 );
141089 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
141090 assert( pRecTerm->pWith==0 );
141091 pRecTerm->pWith = pSel->pWith;
141093 pRecTerm->pWith = 0;
141095 pParse->pWith = pSavedWith;
141100 pParse->pWith = pSavedWith;
141104 pParse->pWith = pWith;
141106 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
141107 pEList = pLeft->pEList;
141108 if( pCte->pCols ){
141109 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
141111 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
141113 pParse->pWith = pSavedWith;
141116 pEList = pCte->pCols;
141119 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
141121 if( pSel->selFlags & SF_Recursive ){
141122 pCte->zCteErr = "multiple recursive references: %s";
141124 pCte->zCteErr = "recursive reference in a subquery: %s";
141128 pCte->zCteErr = 0;
141129 pParse->pWith = pSavedWith;
141146 Parse *pParse = pWalker->pParse;
141147 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
141148 With *pWith = findRightmost(p)->pWith;
141150 assert( pParse->pWith==pWith || pParse->nErr );
141151 pParse->pWith = pWith->pOuter;
141159 ** sub-query in the FROM clause of a SELECT statement. This function
141165 Select *pSel = pFrom->pSelect;
141169 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
141171 pTab->nTabRef = 1;
141172 if( pFrom->zAlias ){
141173 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
141175 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
141177 while( pSel->pPrior ){ pSel = pSel->pPrior; }
141178 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
141179 pTab->iPKey = -1;
141180 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141182 /* The usual case - do not allow ROWID on a subquery */
141183 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
141185 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
141187 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
141206 N--;
141208 if( pBase->fg.isUsing==0 ) continue;
141209 if( NEVER(pBase->u3.pUsing==0) ) continue;
141210 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
141223 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
141225 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
141241 Parse *pParse = pWalker->pParse;
141246 sqlite3 *db = pParse->db;
141248 u16 selFlags = p->selFlags;
141251 p->selFlags |= SF_Expanded;
141252 if( db->mallocFailed ){
141255 assert( p->pSrc!=0 );
141259 if( pWalker->eCode ){
141261 p->selId = ++pParse->nSelect;
141263 pTabList = p->pSrc;
141264 pEList = p->pEList;
141265 if( pParse->pWith && (p->selFlags & SF_View) ){
141266 if( p->pWith==0 ){
141267 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
141268 if( p->pWith==0 ){
141272 p->pWith->bView = 1;
141274 sqlite3WithPush(pParse, p->pWith, 0);
141285 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141287 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
141288 if( pFrom->pTab ) continue;
141289 assert( pFrom->fg.isRecursive==0 );
141290 if( pFrom->zName==0 ){
141292 Select *pSel = pFrom->pSelect;
141293 /* A sub-query in the FROM clause of a SELECT */
141295 assert( pFrom->pTab==0 );
141302 pTab = pFrom->pTab;
141307 assert( pFrom->pTab==0 );
141308 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
141310 if( pTab->nTabRef>=0xffff ){
141312 pTab->zName);
141313 pFrom->pTab = 0;
141316 pTab->nTabRef++;
141323 u8 eCodeOrig = pWalker->eCode;
141325 assert( pFrom->pSelect==0 );
141327 if( (db->flags & SQLITE_EnableView)==0
141328 && pTab->pSchema!=db->aDb[1].pSchema
141331 pTab->zName);
141333 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
141337 && pFrom->fg.fromDDL
141338 && ALWAYS(pTab->u.vtab.p!=0)
141339 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
141342 pTab->zName);
141346 nCol = pTab->nCol;
141347 pTab->nCol = -1;
141348 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
141349 sqlite3WalkSelect(pWalker, pFrom->pSelect);
141350 pWalker->eCode = eCodeOrig;
141351 pTab->nCol = nCol;
141357 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
141364 assert( db->mallocFailed==0 || pParse->nErr!=0 );
141365 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
141380 for(k=0; k<pEList->nExpr; k++){
141381 pE = pEList->a[k].pExpr;
141382 if( pE->op==TK_ASTERISK ) break;
141383 assert( pE->op!=TK_DOT || pE->pRight!=0 );
141384 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
141385 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
141386 elistFlags |= pE->flags;
141388 if( k<pEList->nExpr ){
141394 struct ExprList_item *a = pEList->a;
141396 int flags = pParse->db->flags;
141400 for(k=0; k<pEList->nExpr; k++){
141402 elistFlags |= pE->flags;
141403 pRight = pE->pRight;
141404 assert( pE->op!=TK_DOT || pRight!=0 );
141405 if( pE->op!=TK_ASTERISK
141406 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
141412 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
141413 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
141422 if( pE->op==TK_DOT ){
141423 assert( pE->pLeft!=0 );
141424 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
141425 zTName = pE->pLeft->u.zToken;
141427 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141428 Table *pTab = pFrom->pTab; /* Table for this data source */
141429 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
141435 if( (zTabName = pFrom->zAlias)==0 ){
141436 zTabName = pTab->zName;
141438 if( db->mallocFailed ) break;
141439 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
141440 if( pFrom->fg.isNestedFrom ){
141441 assert( pFrom->pSelect!=0 );
141442 pNestedFrom = pFrom->pSelect->pEList;
141444 assert( pNestedFrom->nExpr==pTab->nCol );
141450 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
141451 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
141453 if( i+1<pTabList->nSrc
141459 for(ii=0; ii<pUsing->nId; ii++){
141460 const char *zUName = pUsing->a[ii].zName;
141464 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
141465 assert( pX->zEName==0 );
141466 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
141467 pX->fg.eEName = ENAME_TAB;
141468 pX->fg.bUsingTerm = 1;
141474 for(j=0; j<pTab->nCol; j++){
141475 char *zName = pTab->aCol[j].zCnName;
141481 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
141487 ** result-set list unless the SELECT has the SF_IncludeHidden
141490 if( (p->selFlags & SF_IncludeHidden)==0
141491 && IsHiddenColumn(&pTab->aCol[j])
141495 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
141504 if( pFrom->fg.isUsing
141505 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
141513 if( (pTabList->nSrc>1
141514 && ( (pFrom->fg.jointype & JT_LTORJ)==0
141516 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
141524 if( IN_RENAME_OBJECT && pE->pLeft ){
141525 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
141538 pX = &pNew->a[pNew->nExpr-1];
141539 assert( pX->zEName==0 );
141542 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
141543 testcase( pX->zEName==0 );
141545 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
141547 testcase( pX->zEName==0 );
141549 pX->fg.eEName = ENAME_TAB;
141550 if( (pFrom->fg.isUsing
141551 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
141553 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
141555 pX->fg.bNoExpand = 1;
141558 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
141559 pX->fg.eEName = ENAME_NAME;
141561 pX->zEName = sqlite3DbStrDup(db, zName);
141562 pX->fg.eEName = ENAME_NAME;
141576 p->pEList = pNew;
141578 if( p->pEList ){
141579 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
141584 p->selFlags |= SF_ComplexResult;
141589 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
141616 ** The calling function can detect the problem by looking at pParse->nErr
141617 ** and/or pParse->db->mallocFailed.
141623 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
141640 ** For each FROM-clause subquery, add Column.zType and Column.zColl
141655 assert( p->selFlags & SF_Resolved );
141656 if( p->selFlags & SF_HasTypeInfo ) return;
141657 p->selFlags |= SF_HasTypeInfo;
141658 pParse = pWalker->pParse;
141659 pTabList = p->pSrc;
141660 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141661 Table *pTab = pFrom->pTab;
141663 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
141664 /* A sub-query in the FROM clause of a SELECT */
141665 Select *pSel = pFrom->pSelect;
141667 while( pSel->pPrior ) pSel = pSel->pPrior;
141679 ** the Table structures of all FROM-clause subqueries in a
141700 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
141701 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
141713 assert( p!=0 || pParse->db->mallocFailed );
141714 assert( pParse->db->pParse==pParse );
141715 if( pParse->db->mallocFailed ) return;
141716 if( p->selFlags & SF_HasTypeInfo ) return;
141718 if( pParse->nErr ) return;
141720 if( pParse->nErr ) return;
141733 Vdbe *v = pParse->pVdbe;
141736 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
141737 assert( pParse->db->pParse==pParse );
141738 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
141740 if( pParse->nErr ) return;
141744 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
141745 for(i=0; i<pAggInfo->nColumn; i++){
141746 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
141747 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
141749 for(i=0; i<pAggInfo->nFunc; i++){
141750 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
141751 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
141754 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
141755 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
141756 if( pFunc->iDistinct>=0 ){
141757 Expr *pE = pFunc->pFExpr;
141759 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
141762 pFunc->iDistinct = -1;
141764 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
141765 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
141766 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
141767 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
141768 pFunc->pFunc->zName));
141779 Vdbe *v = pParse->pVdbe;
141782 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141784 assert( ExprUseXList(pF->pFExpr) );
141785 pList = pF->pFExpr->x.pList;
141786 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
141787 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
141796 ** If regAcc is non-zero and there are no min() or max() aggregates
141797 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
141807 Vdbe *v = pParse->pVdbe;
141814 pAggInfo->directMode = 1;
141815 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141820 assert( ExprUseXList(pF->pFExpr) );
141821 assert( !IsWindowFunc(pF->pFExpr) );
141822 pList = pF->pFExpr->x.pList;
141823 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
141824 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
141825 if( pAggInfo->nAccumulator
141826 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
141832 if( regHit==0 ) regHit = ++pParse->nMem;
141846 nArg = pList->nExpr;
141853 if( pF->iDistinct>=0 && pList ){
141857 pF->iDistinct = codeDistinct(pParse, eDistinctType,
141858 pF->iDistinct, addrNext, pList, regAgg);
141860 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
141864 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
141865 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
141866 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
141869 pColl = pParse->db->pDfltColl;
141871 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
141874 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
141875 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
141882 if( regHit==0 && pAggInfo->nAccumulator ){
141888 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
141889 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
141892 pAggInfo->directMode = 0;
141908 if( pParse->explain==2 ){
141911 pTab->zName,
141913 bCover ? pIdx->zName : ""
141928 ** sub-expression matches the criteria for being moved to the WHERE
141929 ** clause. If so, add it to the WHERE clause and replace the sub-expression
141933 if( pExpr->op!=TK_AND ){
141934 Select *pS = pWalker->u.pSelect;
141936 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
141942 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
141944 && pExpr->pAggInfo==0
141946 sqlite3 *db = pWalker->pParse->db;
141949 Expr *pWhere = pS->pWhere;
141951 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
141952 pS->pWhere = pNew;
141953 pWalker->eCode = 1;
141982 sqlite3WalkExpr(&sWalker, p->pHaving);
141992 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
141997 SrcList *pTabList, /* Search for self-joins in this FROM clause */
142001 assert( pThis->pSelect!=0 );
142002 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
142003 for(pItem = pTabList->a; pItem<pThis; pItem++){
142005 if( pItem->pSelect==0 ) continue;
142006 if( pItem->fg.viaCoroutine ) continue;
142007 if( pItem->zName==0 ) continue;
142008 assert( pItem->pTab!=0 );
142009 assert( pThis->pTab!=0 );
142010 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
142011 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
142012 pS1 = pItem->pSelect;
142013 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
142018 if( pItem->pSelect->selFlags & SF_PushDown ){
142032 sqlite3DbFree(db, p->aCol);
142033 sqlite3DbFree(db, p->aFunc);
142062 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
142063 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
142064 if( p->pWhere ) return 0;
142065 if( p->pGroupBy ) return 0;
142066 pExpr = p->pEList->a[0].pExpr;
142067 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
142069 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
142071 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
142072 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
142073 pSub = p->pSrc->a[0].pSelect;
142075 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
142077 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
142078 if( pSub->pWhere ) return 0; /* No WHERE clause */
142079 if( pSub->pLimit ) return 0; /* No LIMIT clause */
142080 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
142081 pSub = pSub->pPrior; /* Repeat over compound */
142086 db = pParse->db;
142089 pSub = p->pSrc->a[0].pSelect;
142090 p->pSrc->a[0].pSelect = 0;
142091 sqlite3SrcListDelete(db, p->pSrc);
142092 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
142095 pPrior = pSub->pPrior;
142096 pSub->pPrior = 0;
142097 pSub->pNext = 0;
142098 pSub->selFlags |= SF_Aggregate;
142099 pSub->selFlags &= ~SF_Compound;
142100 pSub->nSelectRow = 0;
142101 sqlite3ExprListDelete(db, pSub->pEList);
142103 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
142113 p->pEList->a[0].pExpr = pExpr;
142114 p->selFlags &= ~SF_Aggregate;
142118 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
142127 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
142133 for(i=0; i<pSrc->nSrc; i++){
142134 SrcItem *p1 = &pSrc->a[i];
142136 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
142139 if( p1->pSelect
142140 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
142141 && sameSrcAlias(p0, p1->pSelect->pSrc)
142157 ** pParse->zErrMsg.
142185 db = pParse->db;
142186 assert( pParse==db->pParse );
142188 if( p==0 || pParse->nErr ){
142191 assert( db->mallocFailed==0 );
142194 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
142204 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
142205 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
142206 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
142207 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
142209 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
142210 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
142211 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
142213 if( p->pOrderBy ){
142217 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
142222 p->pOrderBy);
142223 testcase( pParse->earlyCleanup );
142224 p->pOrderBy = 0;
142226 p->selFlags &= ~SF_Distinct;
142227 p->selFlags |= SF_NoopOrderBy;
142230 if( pParse->nErr ){
142233 assert( db->mallocFailed==0 );
142234 assert( p->pEList!=0 );
142244 ** In this case, it is an error if the target object (pSrc->a[0]) name
142245 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
142251 if( p->selFlags & SF_UFSrcCheck ){
142252 SrcItem *p0 = &p->pSrc->a[0];
142253 if( sameSrcAlias(p0, p->pSrc) ){
142256 p0->zAlias ? p0->zAlias : p0->pTab->zName
142262 ** and leaving this flag set can cause errors if a compound sub-query
142263 ** in p->pSrc is flattened into this query and this function called
142265 p->selFlags &= ~SF_UFSrcCheck;
142268 if( pDest->eDest==SRT_Output ){
142274 assert( pParse->nErr );
142278 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
142284 pTabList = p->pSrc;
142285 isAgg = (p->selFlags & SF_Aggregate)!=0;
142287 sSort.pOrderBy = p->pOrderBy;
142293 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
142294 SrcItem *pItem = &pTabList->a[i];
142295 Select *pSub = pItem->pSelect;
142296 Table *pTab = pItem->pTab;
142306 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
142307 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
142311 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
142312 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
142313 assert( pItem->iCursor>=0 );
142314 unsetJoinExpr(p->pWhere, pItem->iCursor,
142315 pTabList->a[0].fg.jointype & JT_LTORJ);
142323 if( pTab->nCol!=pSub->pEList->nExpr ){
142325 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
142333 ** will be implemented as a co-routine and there is no advantage to
142336 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
142337 assert( pSub->pGroupBy==0 );
142339 /* If a FROM-clause subquery has an ORDER BY clause that is not
142348 ** (2) The subquery was added to help with window-function
142352 ** the built-in count(), min(), or max().
142359 if( pSub->pOrderBy!=0
142360 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
142361 && pSub->pLimit==0 /* Condition (1) */
142362 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
142363 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
142367 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
142370 pSub->pOrderBy);
142371 pSub->pOrderBy = 0;
142377 ** it will be implemented as a co-routine, then do not flatten. This
142392 if( pSub->pOrderBy!=0
142394 && (p->selFlags & SF_ComplexResult)!=0
142395 && (pTabList->nSrc==1
142396 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
142402 if( pParse->nErr ) goto select_end;
142404 i = -1;
142406 pTabList = p->pSrc;
142407 if( db->mallocFailed ) goto select_end;
142409 sSort.pOrderBy = p->pOrderBy;
142418 if( p->pPrior ){
142421 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
142426 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
142431 /* Do the WHERE-clause constant propagation optimization if this is
142432 ** a join. No need to speed time on this operation for non-join queries
142436 if( p->pWhere!=0
142437 && p->pWhere->op==TK_AND
142455 if( db->mallocFailed ) goto select_end;
142456 pEList = p->pEList;
142457 pTabList = p->pSrc;
142463 ** (2) Generate code for all sub-queries
142465 for(i=0; i<pTabList->nSrc; i++){
142466 SrcItem *pItem = &pTabList->a[i];
142479 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
142480 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
142487 ** assume the column name is non-NULL and segfault. The use of an empty
142490 if( pItem->colUsed==0 && pItem->zName!=0 ){
142491 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
142495 /* Generate code for all sub-queries in the FROM clause
142497 pSub = pItem->pSelect;
142501 assert( pItem->addrFillSub==0 );
142506 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
142510 pParse->nHeight += sqlite3SelectExprHeight(p);
142512 /* Make copies of constant WHERE-clause terms in the outer query down
142516 && (pItem->fg.isCte==0
142517 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
142518 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
142523 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
142527 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
142529 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
142532 zSavedAuthContext = pParse->zAuthContext;
142533 pParse->zAuthContext = pItem->zName;
142537 ** The subquery is implemented as a co-routine if all of the following are
142546 && (pTabList->nSrc==1
142547 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) /* (1) */
142548 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
142549 && (pTabList->a[0].fg.jointype & JT_LTORJ)==0 /* (3) */
142551 /* Implement a co-routine that will return a single row of the result
142556 pItem->regReturn = ++pParse->nMem;
142557 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
142559 pItem->addrFillSub = addrTop;
142560 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
142561 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
142563 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142564 pItem->fg.viaCoroutine = 1;
142565 pItem->regResult = dest.iSdst;
142566 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
142567 sqlite3VdbeJumpHere(v, addrTop-1);
142569 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
142572 ** the make the pItem->iCursor be a copy of the ephemerial table that
142574 CteUse *pCteUse = pItem->u2.pCteUse;
142575 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
142576 if( pItem->iCursor!=pCteUse->iCur ){
142577 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
142580 pSub->nSelectRow = pCteUse->nRowEst;
142584 if( pPrior->addrFillSub ){
142585 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
142587 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
142588 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
142596 pItem->regReturn = ++pParse->nMem;
142598 pItem->addrFillSub = topAddr+1;
142599 pItem->fg.isMaterialized = 1;
142600 if( pItem->fg.isCorrelated==0 ){
142609 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
142612 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142614 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
142618 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142619 CteUse *pCteUse = pItem->u2.pCteUse;
142620 pCteUse->addrM9e = pItem->addrFillSub;
142621 pCteUse->regRtn = pItem->regReturn;
142622 pCteUse->iCur = pItem->iCursor;
142623 pCteUse->nRowEst = pSub->nSelectRow;
142626 if( db->mallocFailed ) goto select_end;
142627 pParse->nHeight -= sqlite3SelectExprHeight(p);
142628 pParse->zAuthContext = zSavedAuthContext;
142634 pEList = p->pEList;
142635 pWhere = p->pWhere;
142636 pGroupBy = p->pGroupBy;
142637 pHaving = p->pHaving;
142638 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
142642 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
142648 ** if the select-list is the same as the ORDER BY list, then this query
142657 ** The second form is preferred as a single index (or temp-table) may be
142659 ** written the query must use a temp-table for at least one of the ORDER
142660 ** BY and DISTINCT, and an index or separate temp-table for the other.
142662 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
142663 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
142665 && p->pWin==0
142668 p->selFlags &= ~SF_Distinct;
142669 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
142670 p->selFlags |= SF_Aggregate;
142671 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
142687 ** being unused if the data can be extracted in pre-sorted order.
142696 pParse, sSort.pOrderBy, 0, pEList->nExpr);
142697 sSort.iECursor = pParse->nTab++;
142700 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
142704 sSort.addrSortIndex = -1;
142709 if( pDest->eDest==SRT_EphemTab ){
142710 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
142711 if( p->selFlags & SF_NestedFrom ){
142712 /* Delete or NULL-out result columns that will never be used */
142714 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
142715 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
142716 sqlite3DbFree(db, pEList->a[ii].zEName);
142717 pEList->nExpr--;
142719 for(ii=0; ii<pEList->nExpr; ii++){
142720 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
142728 if( (p->selFlags & SF_FixedLimit)==0 ){
142729 p->nSelectRow = 320; /* 4 billion rows */
142732 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
142739 if( p->selFlags & SF_Distinct ){
142740 sDistinct.tabTnct = pParse->nTab++;
142743 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
142754 | (p->selFlags & SF_FixedLimit);
142756 Window *pWin = p->pWin; /* Main window object (or NULL) */
142767 p->pEList, p, wctrlFlags, p->nSelectRow);
142769 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
142770 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
142778 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
142792 assert( p->pEList==pEList );
142798 int regGosub = ++pParse->nMem;
142804 VdbeNoopComment((v, "inner-loop subroutine"));
142806 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
142809 VdbeComment((v, "end inner-loop subroutine"));
142815 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
142847 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
142848 pItem->u.x.iAlias = 0;
142850 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
142851 pItem->u.x.iAlias = 0;
142854 if( p->nSelectRow>66 ) p->nSelectRow = 66;
142859 ** in the correct order. It also may not - the GROUP BY might use a
142864 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
142867 ** ASC or DESC order - only that each group is returned contiguously.
142871 for(ii=0; ii<pGroupBy->nExpr; ii++){
142873 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
142874 pGroupBy->a[ii].fg.sortFlags = sortFlags;
142876 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
142882 p->nSelectRow = 0;
142885 /* Create a label to jump to when we want to abort the query */
142896 testcase( pParse->earlyCleanup );
142898 if( db->mallocFailed ){
142901 pAggInfo->selId = p->selId;
142907 pAggInfo->mnReg = pParse->nMem+1;
142908 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
142909 pAggInfo->pGroupBy = pGroupBy;
142914 assert( pWhere==p->pWhere );
142915 assert( pHaving==p->pHaving );
142916 assert( pGroupBy==p->pGroupBy );
142918 pWhere = p->pWhere;
142922 pAggInfo->nAccumulator = pAggInfo->nColumn;
142923 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
142924 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
142928 for(i=0; i<pAggInfo->nFunc; i++){
142929 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
142932 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
142936 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
142941 pAggInfo->mxReg = pParse->nMem;
142942 if( db->mallocFailed ) goto select_end;
142952 for(ii=0; ii<pAggInfo->nColumn; ii++){
142953 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
142954 ii, pAggInfo->aCol[ii].iMem);
142955 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
142957 for(ii=0; ii<pAggInfo->nFunc; ii++){
142958 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
142959 ii, pAggInfo->aFunc[ii].iMem);
142960 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
142971 int addr1; /* A-vs-B comparision jump */
142983 if( pAggInfo->nFunc==1
142984 && pAggInfo->aFunc[0].iDistinct>=0
142985 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
142986 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
142987 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
142989 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
143001 pAggInfo->sortingIdx = pParse->nTab++;
143003 0, pAggInfo->nColumn);
143005 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
143010 iUseFlag = ++pParse->nMem;
143011 iAbortFlag = ++pParse->nMem;
143012 regOutputRow = ++pParse->nMem;
143014 regReset = ++pParse->nMem;
143016 iAMem = pParse->nMem + 1;
143017 pParse->nMem += pGroupBy->nExpr;
143018 iBMem = pParse->nMem + 1;
143019 pParse->nMem += pGroupBy->nExpr;
143022 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
143041 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
143059 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
143063 nGroupBy = pGroupBy->nExpr;
143066 for(i=0; i<pAggInfo->nColumn; i++){
143067 if( pAggInfo->aCol[i].iSorterColumn>=j ){
143075 for(i=0; i<pAggInfo->nColumn; i++){
143076 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143077 if( pCol->iSorterColumn>=j ){
143080 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
143086 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
143091 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
143094 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
143096 pAggInfo->useSortingIdx = 1;
143103 ** This is an optimization - the correct answer should result regardless.
143120 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
143123 for(j=0; j<pGroupBy->nExpr; j++){
143127 pAggInfo->directMode = 1;
143128 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
143131 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
143145 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
143164 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
143184 ** is less than or equal to zero, the subroutine is a no-op. If
143201 selectInnerLoop(pParse, p, -1, &sSort,
143207 /* Generate a subroutine that will reset the group-by accumulator
143216 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
143217 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143236 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
143237 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
143241 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
143244 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
143248 ** (2011-04-15) Do not do a full scan of an unordered index.
143250 ** (2013-10-03) Do not count the entries in a partial index.
143256 if( !p->pSrc->a[0].fg.notIndexed ){
143257 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143258 if( pIdx->bUnordered==0
143259 && pIdx->szIdxRow<pTab->szTabRow
143260 && pIdx->pPartIdxWhere==0
143261 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
143268 iRoot = pBest->tnum;
143272 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
143275 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
143277 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
143295 if( pAggInfo->nAccumulator ){
143296 for(i=0; i<pAggInfo->nFunc; i++){
143297 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
143300 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
143304 if( i==pAggInfo->nFunc ){
143305 regAcc = ++pParse->nMem;
143308 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
143309 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
143310 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
143318 assert( p->pGroupBy==0 );
143327 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
143339 struct AggInfo_func *pF = pAggInfo->aFunc;
143341 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143356 selectInnerLoop(pParse, p, -1, 0, 0,
143373 assert( p->pEList==pEList );
143374 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
143383 rc = (pParse->nErr>0);
143389 assert( db->mallocFailed==0 || db->mallocFailed==1 );
143390 assert( db->mallocFailed==0 || pParse->nErr!=0 );
143393 if( pAggInfo && !db->mallocFailed ){
143394 for(i=0; i<pAggInfo->nColumn; i++){
143395 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
143397 assert( pExpr->pAggInfo==pAggInfo );
143398 assert( pExpr->iAgg==i );
143400 for(i=0; i<pAggInfo->nFunc; i++){
143401 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
143403 assert( pExpr->pAggInfo==pAggInfo );
143404 assert( pExpr->iAgg==i );
143464 int need; /* Slots needed in p->azResult[] */
143468 /* Make sure there is enough space in p->azResult to hold everything
143471 if( p->nRow==0 && argv!=0 ){
143476 if( p->nData + need > p->nAlloc ){
143478 p->nAlloc = p->nAlloc*2 + need;
143479 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
143481 p->azResult = azNew;
143487 if( p->nRow==0 ){
143488 p->nColumn = nCol;
143492 p->azResult[p->nData++] = z;
143494 }else if( (int)p->nColumn!=nCol ){
143495 sqlite3_free(p->zErrMsg);
143496 p->zErrMsg = sqlite3_mprintf(
143499 p->rc = SQLITE_ERROR;
143515 p->azResult[p->nData++] = z;
143517 p->nRow++;
143522 p->rc = SQLITE_NOMEM_BKPT;
143562 db->errCode = SQLITE_NOMEM;
143578 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
143591 db->errCode = SQLITE_NOMEM;
143610 azResult--;
143643 pTriggerStep = pTriggerStep->pNext;
143645 sqlite3ExprDelete(db, pTmp->pWhere);
143646 sqlite3ExprListDelete(db, pTmp->pExprList);
143647 sqlite3SelectDelete(db, pTmp->pSelect);
143648 sqlite3IdListDelete(db, pTmp->pIdList);
143649 sqlite3UpsertDelete(db, pTmp->pUpsert);
143650 sqlite3SrcListDelete(db, pTmp->pFrom);
143651 sqlite3DbFree(db, pTmp->zSpan);
143662 ** are already attached to pTab->pTrigger. But there might be additional
143664 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
143669 ** pTab as well as the triggers lised in pTab->pTrigger.
143676 assert( pParse->disableTriggers==0 );
143677 pTmpSchema = pParse->db->aDb[1].pSchema;
143678 p = sqliteHashFirst(&pTmpSchema->trigHash);
143679 pList = pTab->pTrigger;
143682 if( pTrig->pTabSchema==pTab->pSchema
143683 && pTrig->table
143684 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
143685 && pTrig->pTabSchema!=pTmpSchema
143687 pTrig->pNext = pList;
143689 }else if( pTrig->op==TK_RETURNING ){
143691 assert( pParse->db->pVtabCtx==0 );
143693 assert( pParse->bReturning );
143694 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
143695 pTrig->table = pTab->zName;
143696 pTrig->pTabSchema = pTab->pSchema;
143697 pTrig->pNext = pList;
143705 printf("Triggers for %s:", pTab->zName);
143706 for(pX=pList; pX; pX=pX->pNext){
143707 printf(" %s", pX->zName);
143720 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
143739 sqlite3 *db = pParse->db; /* The database connection */
143744 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
143750 if( pName2->n>0 ){
143763 if( !pTableName || db->mallocFailed ){
143767 /* A long-standing parser bug is that this syntax was allowed:
143775 if( db->init.busy && iDb!=1 ){
143776 sqlite3DbFree(db, pTableName->a[0].zDatabase);
143777 pTableName->a[0].zDatabase = 0;
143786 if( db->init.busy==0 && pName2->n==0 && pTab
143787 && pTab->pSchema==db->aDb[1].pSchema ){
143792 if( db->mallocFailed ) goto trigger_cleanup;
143793 assert( pTableName->nSrc==1 );
143812 assert( db->mallocFailed );
143815 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
143820 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
143824 assert( !db->init.busy );
143832 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
143842 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
143847 " trigger on table: %S", pTableName->a);
143853 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
143855 const char *zDb = db->aDb[iTabDb].zDbSName;
143856 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
143858 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
143879 pTrigger->zName = zName;
143881 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
143882 pTrigger->pSchema = db->aDb[iDb].pSchema;
143883 pTrigger->pTabSchema = pTab->pSchema;
143884 pTrigger->op = (u8)op;
143885 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
143887 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
143888 pTrigger->pWhen = pWhen;
143891 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
143893 pTrigger->pColumns = pColumns;
143895 assert( pParse->pNewTrigger==0 );
143896 pParse->pNewTrigger = pTrigger;
143903 if( !pParse->pNewTrigger ){
143906 assert( pParse->pNewTrigger==pTrigger );
143911 if( db->init.iDb==1 ){
143914 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
143918 ** "orphaned trigger" - a trigger whose associated table is missing.
143920 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
143922 db->init.orphanTrigger = 1;
143936 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
143938 sqlite3 *db = pParse->db; /* The database */
143943 pParse->pNewTrigger = 0;
143944 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
143945 zName = pTrig->zName;
143946 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
143947 pTrig->step_list = pStepList;
143949 pStepList->pTrig = pTrig;
143950 pStepList = pStepList->pNext;
143952 sqlite3TokenInit(&nameToken, pTrig->zName);
143954 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
143955 || sqlite3FixExpr(&sFix, pTrig->pWhen)
143962 assert( !db->init.busy );
143963 pParse->pNewTrigger = pTrig;
143971 if( !db->init.busy ){
143979 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
143984 db->aDb[iDb].zDbSName, zName,
143985 pTrig->table, z);
143992 if( db->init.busy ){
143994 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
144000 }else if( pLink->pSchema==pLink->pTabSchema ){
144002 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
144004 pLink->pNext = pTab->pTrigger;
144005 pTab->pTrigger = pLink;
144011 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
144044 pTriggerStep->op = TK_SELECT;
144045 pTriggerStep->pSelect = pSelect;
144046 pTriggerStep->orconf = OE_Default;
144047 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144055 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
144064 sqlite3 *db = pParse->db;
144067 if( pParse->nErr ) return 0;
144068 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
144071 memcpy(z, pName->z, pName->n);
144073 pTriggerStep->zTarget = z;
144074 pTriggerStep->op = op;
144075 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144077 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
144100 sqlite3 *db = pParse->db;
144103 assert(pSelect != 0 || db->mallocFailed);
144108 pTriggerStep->pSelect = pSelect;
144111 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
144113 pTriggerStep->pIdList = pColumn;
144114 pTriggerStep->pUpsert = pUpsert;
144115 pTriggerStep->orconf = orconf;
144117 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
144138 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
144145 sqlite3 *db = pParse->db;
144151 pTriggerStep->pExprList = pEList;
144152 pTriggerStep->pWhere = pWhere;
144153 pTriggerStep->pFrom = pFrom;
144158 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
144159 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
144160 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
144162 pTriggerStep->orconf = orconf;
144182 sqlite3 *db = pParse->db;
144188 pTriggerStep->pWhere = pWhere;
144191 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
144193 pTriggerStep->orconf = OE_Default;
144203 if( pTrigger==0 || pTrigger->bReturning ) return;
144204 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
144205 sqlite3DbFree(db, pTrigger->zName);
144206 sqlite3DbFree(db, pTrigger->table);
144207 sqlite3ExprDelete(db, pTrigger->pWhen);
144208 sqlite3IdListDelete(db, pTrigger->pColumns);
144225 sqlite3 *db = pParse->db;
144227 if( db->mallocFailed ) goto drop_trigger_cleanup;
144232 assert( pName->nSrc==1 );
144233 zDb = pName->a[0].zDatabase;
144234 zName = pName->a[0].zName;
144236 for(i=OMIT_TEMPDB; i<db->nDb; i++){
144240 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
144245 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
144249 pParse->checkSchema = 1;
144263 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
144273 sqlite3 *db = pParse->db;
144276 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
144277 assert( iDb>=0 && iDb<db->nDb );
144279 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
144283 const char *zDb = db->aDb[iDb].zDbSName;
144286 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
144298 db->aDb[iDb].zDbSName, pTrigger->zName
144301 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
144313 pHash = &(db->aDb[iDb].pSchema->trigHash);
144316 if( pTrigger->pSchema==pTrigger->pTabSchema ){
144320 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
144322 *pp = (*pp)->pNext;
144329 db->mDbFlags |= DBFLAG_SchemaChange;
144345 for(e=0; e<pEList->nExpr; e++){
144346 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
144355 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
144356 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
144379 || (pList->bReturning && pList->pNext==0) );
144382 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
144383 && pTab->pTrigger!=0
144388 if( pList==pTab->pTrigger ){
144392 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
144393 p->pNext = 0;
144397 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
144398 mask |= p->tr_tm;
144399 }else if( p->op==TK_RETURNING ){
144403 p->op = op;
144410 p->tr_tm = TRIGGER_BEFORE;
144412 p->tr_tm = TRIGGER_AFTER;
144414 mask |= p->tr_tm;
144415 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
144418 mask |= p->tr_tm;
144420 p = p->pNext;
144437 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
144438 || pParse->disableTriggers
144447 ** Convert the pStep->zTarget string into a SrcList and return a pointer
144460 sqlite3 *db = pParse->db;
144462 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
144464 assert( pSrc==0 || pSrc->nSrc==1 );
144467 Schema *pSchema = pStep->pTrig->pSchema;
144468 pSrc->a[0].zName = zName;
144469 if( pSchema!=db->aDb[1].pSchema ){
144470 pSrc->a[0].pSchema = pSchema;
144472 if( pStep->pFrom ){
144473 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
144474 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
144500 if( pTerm->op==TK_ASTERISK ) return 1;
144501 if( pTerm->op!=TK_DOT ) return 0;
144502 assert( pTerm->pRight!=0 );
144503 assert( pTerm->pLeft!=0 );
144504 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
144521 sqlite3 *db = pParse->db;
144524 for(i=0; i<pList->nExpr; i++){
144525 Expr *pOldExpr = pList->a[i].pExpr;
144529 for(jj=0; jj<pTab->nCol; jj++){
144531 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
144532 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
144534 if( !db->mallocFailed ){
144535 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144536 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
144537 pItem->fg.eEName = ENAME_NAME;
144543 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
144544 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144545 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
144546 pItem->fg.eEName = pList->a[i].fg.eEName;
144556 ** is generated in-line.
144564 Vdbe *v = pParse->pVdbe;
144565 sqlite3 *db = pParse->db;
144572 assert( pParse->bReturning );
144573 assert( db->pParse==pParse );
144574 pReturning = pParse->u1.pReturning;
144575 assert( pTrigger == &(pReturning->retTrig) );
144578 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
144582 sFrom.a[0].iCursor = -1;
144584 if( pParse->nErr==0 ){
144585 assert( db->mallocFailed==0 );
144589 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
144590 if( !db->mallocFailed ){
144593 if( pReturning->nRetCol==0 ){
144594 pReturning->nRetCol = pNew->nExpr;
144595 pReturning->iRetCur = pParse->nTab++;
144600 pParse->eTriggerOp = pTrigger->op;
144601 pParse->pTriggerTab = pTab;
144603 && ALWAYS(!db->mallocFailed)
144606 int nCol = pNew->nExpr;
144607 int reg = pParse->nMem+1;
144608 pParse->nMem += nCol+2;
144609 pReturning->iRetReg = reg;
144611 Expr *pCol = pNew->a[i].pExpr;
144612 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
144619 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
144620 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
144624 pParse->eTriggerOp = 0;
144625 pParse->pTriggerTab = 0;
144640 Vdbe *v = pParse->pVdbe;
144641 sqlite3 *db = pParse->db;
144643 assert( pParse->pTriggerTab && pParse->pToplevel );
144646 for(pStep=pStepList; pStep; pStep=pStep->pNext){
144657 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
144658 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
144660 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
144661 assert( pParse->okConstFactor==0 );
144664 if( pStep->zSpan ){
144666 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
144671 switch( pStep->op ){
144675 sqlite3ExprListDup(db, pStep->pExprList, 0),
144676 sqlite3ExprDup(db, pStep->pWhere, 0),
144677 pParse->eOrconf, 0, 0, 0
144685 sqlite3SelectDup(db, pStep->pSelect, 0),
144686 sqlite3IdListDup(db, pStep->pIdList),
144687 pParse->eOrconf,
144688 sqlite3UpsertDup(db, pStep->pUpsert)
144696 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
144701 default: assert( pStep->op==TK_SELECT ); {
144703 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
144734 ** Parse context structure pFrom has just been used to create a sub-vdbe
144739 assert( pFrom->zErrMsg==0 || pFrom->nErr );
144740 assert( pTo->zErrMsg==0 || pTo->nErr );
144741 if( pTo->nErr==0 ){
144742 pTo->zErrMsg = pFrom->zErrMsg;
144743 pTo->nErr = pFrom->nErr;
144744 pTo->rc = pFrom->rc;
144746 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
144751 ** Create and populate a new TriggerPrg object with a sub-program
144761 sqlite3 *db = pParse->db; /* Database handle */
144765 NameContext sNC; /* Name context for sub-vdbe */
144766 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
144767 int iEndTrigger = 0; /* Label to jump to if WHEN is false */
144768 Parse sSubParse; /* Parse context for sub-vdbe */
144770 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
144771 assert( pTop->pVdbe );
144775 ** list of the top-level Parse object sooner rather than later. */
144778 pPrg->pNext = pTop->pTriggerPrg;
144779 pTop->pTriggerPrg = pPrg;
144780 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
144782 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
144783 pPrg->pTrigger = pTrigger;
144784 pPrg->orconf = orconf;
144785 pPrg->aColmask[0] = 0xffffffff;
144786 pPrg->aColmask[1] = 0xffffffff;
144789 ** trigger sub-program. */
144795 sSubParse.zAuthContext = pTrigger->zName;
144796 sSubParse.eTriggerOp = pTrigger->op;
144797 sSubParse.nQueryLoop = pParse->nQueryLoop;
144798 sSubParse.disableVtab = pParse->disableVtab;
144803 pTrigger->zName, onErrorText(orconf),
144804 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
144805 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
144806 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
144807 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
144808 pTab->zName
144811 if( pTrigger->zName ){
144812 sqlite3VdbeChangeP4(v, -1,
144813 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
144819 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
144821 if( pTrigger->pWhen ){
144822 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
144823 if( db->mallocFailed==0
144832 /* Code the trigger program into the sub-vdbe. */
144833 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
144835 /* Insert an OP_Halt at the end of the sub-program. */
144840 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
144843 if( pParse->nErr==0 ){
144844 assert( db->mallocFailed==0 );
144845 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
144847 pProgram->nMem = sSubParse.nMem;
144848 pProgram->nCsr = sSubParse.nTab;
144849 pProgram->token = (void *)pTrigger;
144850 pPrg->aColmask[0] = sSubParse.oldmask;
144851 pPrg->aColmask[1] = sSubParse.newmask;
144863 ** Return a pointer to a TriggerPrg object containing the sub-program for
144877 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
144883 for(pPrg=pRoot->pTriggerPrg;
144884 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
144885 pPrg=pPrg->pNext
144891 pParse->db->errByteOffset = -1;
144914 assert( pPrg || pParse->nErr );
144917 ** is a pointer to the sub-vdbe containing the trigger program. */
144919 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
144921 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
144922 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
144924 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
144926 /* Set the P5 operand of the OP_Program instruction to non-zero if
144928 ** invocation is disallowed if (a) the sub-program is really a trigger,
144943 ** operation on pTab, this function is a no-op.
144948 ** (a copy of pTab->nCol), then registers are populated as follows:
144951 ** ------------------------------------------------------
144953 ** reg+1 OLD.* value of left-most column of pTab
144955 ** reg+N OLD.* value of right-most column of pTab
144957 ** reg+N+2 NEW.* value of left-most column of pTab
144959 ** reg+N+N+1 NEW.* value of right-most column of pTab
144992 for(p=pTrigger; p; p=p->pNext){
144997 assert( p->pSchema!=0 );
144998 assert( p->pTabSchema!=0 );
144999 assert( p->pSchema==p->pTabSchema
145000 || p->pSchema==pParse->db->aDb[1].pSchema );
145007 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
145008 && p->tr_tm==tr_tm
145009 && checkColumnOverlap(p->pColumns, pChanges)
145011 if( !p->bReturning ){
145021 ** Triggers may access values stored in the old.* or new.* pseudo-table.
145022 ** This function returns a 32-bit bitmask indicating which columns of the
145027 ** Bit 0 of the returned mask is set if the left-most column of the
145059 for(p=pTrigger; p; p=p->pNext){
145060 if( p->op==op
145061 && (tr_tm&p->tr_tm)
145062 && checkColumnOverlap(p->pColumns,pChanges)
145064 if( p->bReturning ){
145070 mask |= pPrg->aColmask[isNew];
145115 ** i-th column of table pTab. This routine sets the P4 parameter of the
145121 ** command. If the latter, then the row-records in the table btree on disk
145124 ** If the former, then all row-records are guaranteed to include a value
145138 ** If column as REAL affinity and the table is an ordinary b-tree table
145148 Column *pCol = &pTab->aCol[i];
145149 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145150 assert( i<pTab->nCol );
145153 pCol->affinity, &pValue);
145159 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
145168 ** and false if not. This is an optimization. False-positives are a
145169 ** performance degradation, but false-negatives can result in a corrupt
145172 ** aXRef[j] will be non-negative if column j of the original table is
145182 i16 iIdxCol = pIdx->aiColumn[iCol];
145188 assert( pIdx->aColExpr!=0 );
145189 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
145190 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
145198 ** to be unchanged. This is an optimization. False-positives are a
145199 ** performance degradation, but false-negatives can result in a corrupt
145202 ** aXRef[j] will be non-negative if column j of the original table is
145211 if( pIdx->pPartIdxWhere==0 ) return 0;
145212 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
145220 ** table in the source-list (pSrc->a[0]).
145224 if( pRet ) pRet->iColumn = iCol+1;
145232 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
145240 ** SELECT <other-columns>, pChanges FROM pTabList
145242 ** GROUP BY <other-columns>
145248 ** the <other-columns> in the query above are is determined by the type
145249 ** of table pTabList->a[0].pTab.
145252 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
145256 ** If the table is actually a view, then <other-columns> are all columns of
145260 ** If the table is a virtual or ordinary intkey table, then <other-columns>
145263 ** rowid value in <other-columns> is used as the integer key, and the
145283 sqlite3 *db = pParse->db;
145284 Table *pTab = pTabList->a[0].pTab;
145304 assert( pTabList->nSrc>1 );
145306 pSrc->a[0].fg.notCte = 1;
145307 pSrc->a[0].iCursor = -1;
145308 pSrc->a[0].pTab->nTabRef--;
145309 pSrc->a[0].pTab = 0;
145312 for(i=0; i<pPk->nKeyCol; i++){
145313 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
145323 for(i=0; i<pTab->nCol; i++){
145336 assert( pChanges!=0 || pParse->db->mallocFailed );
145338 for(i=0; i<pChanges->nExpr; i++){
145340 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
145347 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
145349 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
145387 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
145388 ** an expression for the i-th column of the table.
145389 ** aXRef[i]==-1 if the i-th column is not changed. */
145395 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
145397 NameContext sNC; /* The name-context to resolve expressions in */
145419 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
145431 db = pParse->db;
145432 assert( db->pParse==pParse );
145433 if( pParse->nErr ){
145436 assert( db->mallocFailed==0 );
145442 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
145464 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
145470 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
145473 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
145498 iBaseCur = iDataCur = pParse->nTab++;
145501 testcase( pPk!=0 && pPk!=pTab->pIndex );
145502 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
145504 iDataCur = pParse->nTab;
145506 pParse->nTab++;
145510 iDataCur = pUpsert->iDataCur;
145511 iIdxCur = pUpsert->iIdxCur;
145512 pParse->nTab = iBaseCur;
145514 pTabList->a[0].iCursor = iDataCur;
145519 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
145521 aRegIdx = aXRef+pTab->nCol;
145525 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
145527 /* Initialize the name-context */
145545 for(i=0; i<pChanges->nExpr; i++){
145546 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
145549 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
145552 for(j=0; j<pTab->nCol; j++){
145553 if( pTab->aCol[j].hName==hCol
145554 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
145556 if( j==pTab->iPKey ){
145558 pRowidExpr = pChanges->a[i].pExpr;
145560 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
145564 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
145565 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
145566 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
145569 pTab->aCol[j].zCnName);
145577 if( j>=pTab->nCol ){
145578 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
145579 j = -1;
145581 pRowidExpr = pChanges->a[i].pExpr;
145584 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
145585 pParse->checkSchema = 1;
145592 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
145593 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
145594 db->aDb[iDb].zDbSName);
145598 aXRef[j] = -1;
145612 ** is non-negative, so the value of aXRef[] for generated columns can be
145613 ** set to any non-negative number. We use 99999 so that the value is
145616 if( pTab->tabFlags & TF_HasGenerated ){
145618 testcase( pTab->tabFlags & TF_HasVirtual );
145619 testcase( pTab->tabFlags & TF_HasStored );
145622 for(i=0; i<pTab->nCol; i++){
145624 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
145626 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
145642 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
145651 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
145656 reg = ++pParse->nMem;
145657 pParse->nMem += pIdx->nColumn;
145660 for(i=0; i<pIdx->nKeyCol; i++){
145662 reg = ++pParse->nMem;
145663 pParse->nMem += pIdx->nColumn;
145664 if( onError==OE_Default && pIdx->onError==OE_Replace ){
145674 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
145681 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
145690 ** two-pass update algorithm. */
145691 assert( aRegIdx[nAllIdx]==pParse->nMem );
145693 regOldRowid = regNewRowid = ++pParse->nMem;
145695 regOld = pParse->nMem + 1;
145696 pParse->nMem += pTab->nCol;
145699 regNewRowid = ++pParse->nMem;
145701 regNew = pParse->nMem + 1;
145702 pParse->nMem += pTab->nCol;
145707 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
145744 if( (db->flags&SQLITE_CountRows)!=0
145745 && !pParse->pTriggerTab
145746 && !pParse->nested
145747 && !pParse->bReturning
145750 regRowCount = ++pParse->nMem;
145756 iEph = pParse->nTab++;
145760 nPk = pPk ? pPk->nKeyCol : 0;
145761 iPk = pParse->nMem+1;
145762 pParse->nMem += nPk;
145763 pParse->nMem += nChangeFrom;
145764 regKey = ++pParse->nMem;
145766 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
145767 iEph = pParse->nTab++;
145768 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
145773 pKeyInfo->nAllField = nEphCol;
145807 ** Do not consider a single-pass strategy for a multi-row update if
145811 ** or index, causing a single-pass approach to malfunction. */
145813 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
145819 /* A one-pass strategy that might update more than one row may not
145835 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
145845 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
145849 aRegIdx[nAllIdx] = ++pParse->nMem;
145861 assert( pPk->aiColumn[i]>=0 );
145863 pPk->aiColumn[i], iPk+i);
145887 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
145888 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
145891 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
145969 /* Compute the old pre-UPDATE content of the row being changed, if that
145976 for(i=0; i<pTab->nCol; i++){
145977 u32 colFlags = pTab->aCol[i].colFlags;
146010 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146011 if( i==pTab->iPKey ){
146013 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
146014 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
146019 int nOff = (isView ? pTab->nCol : nPk);
146023 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
146041 if( pTab->tabFlags & TF_HasGenerated ){
146042 testcase( pTab->tabFlags & TF_HasVirtual );
146043 testcase( pTab->tabFlags & TF_HasStored );
146057 /* The row-trigger may have deleted the row being updated. In this
146059 ** required. This behavior - what happens when the row being updated
146060 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
146071 /* After-BEFORE-trigger-reload-loop:
146077 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
146080 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146081 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
146082 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
146083 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
146088 if( pTab->tabFlags & TF_HasGenerated ){
146089 testcase( pTab->tabFlags & TF_HasVirtual );
146090 testcase( pTab->tabFlags & TF_HasStored );
146122 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
146135 ** to invoke the pre-update hook.
146138 ** pre-update hook. If the caller invokes preupdate_new(), the returned
146152 if( !pParse->nested ){
146193 /* Nothing to do at end-of-loop for a single-pass */
146207 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
146245 ** There are two possible strategies - the default and the special
146273 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
146276 sqlite3 *db = pParse->db; /* Database connection */
146279 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
146283 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
146292 ephemTab = pParse->nTab++;
146294 regArg = pParse->nMem + 1;
146295 pParse->nMem += nArg;
146296 if( pSrc->nSrc>1 ){
146310 assert( pPk->nKeyCol==1 );
146311 iPk = pPk->aiColumn[0];
146313 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
146320 for(i=0; i<pTab->nCol; i++){
146323 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
146334 regRec = ++pParse->nMem;
146335 regRowid = ++pParse->nMem;
146344 for(i=0; i<pTab->nCol; i++){
146345 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
146347 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
146365 assert( pPk->nKeyCol==1 );
146366 iPk = pPk->aiColumn[0];
146377 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
146388 ** accept no-change records with serial_type 10 */
146399 if( pSrc->nSrc==1 ){
146432 ** 2018-04-12
146453 Upsert *pNext = p->pNextUpsert;
146454 sqlite3ExprListDelete(db, p->pUpsertTarget);
146455 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
146456 sqlite3ExprListDelete(db, p->pUpsertSet);
146457 sqlite3ExprDelete(db, p->pUpsertWhere);
146458 sqlite3DbFree(db, p->pToFree);
146474 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
146475 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
146476 sqlite3ExprListDup(db, p->pUpsertSet, 0),
146477 sqlite3ExprDup(db, p->pUpsertWhere, 0),
146478 sqlite3UpsertDup(db, p->pNextUpsert)
146503 pNew->pUpsertTarget = pTarget;
146504 pNew->pUpsertTargetWhere = pTargetWhere;
146505 pNew->pUpsertSet = pSet;
146506 pNew->pUpsertWhere = pWhere;
146507 pNew->isDoUpdate = pSet!=0;
146508 pNew->pNextUpsert = pNext;
146515 ** symbols in the conflict-target.
146529 ExprList *pTarget; /* The conflict-target clause */
146530 Expr *pTerm; /* One term of the conflict-target clause */
146535 assert( pTabList->nSrc==1 );
146536 assert( pTabList->a[0].pTab!=0 );
146538 assert( pUpsert->pUpsertTarget!=0 );
146540 /* Resolve all symbolic names in the conflict-target clause, which
146541 ** includes both the list of columns and the optional partial-index
146547 for(; pUpsert && pUpsert->pUpsertTarget;
146548 pUpsert=pUpsert->pNextUpsert, nClause++){
146549 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
146551 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
146555 pTab = pTabList->a[0].pTab;
146556 pTarget = pUpsert->pUpsertTarget;
146557 iCursor = pTabList->a[0].iCursor;
146559 && pTarget->nExpr==1
146560 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
146561 && pTerm->iColumn==XN_ROWID
146563 /* The conflict-target is the rowid of the primary table */
146564 assert( pUpsert->pUpsertIdx==0 );
146572 ** prior to comparing against the conflict-target expression.
146578 sCol[1].iTable = pTabList->a[0].iCursor;
146581 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146584 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
146585 if( pIdx->pPartIdxWhere ){
146586 if( pUpsert->pUpsertTargetWhere==0 ) continue;
146587 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
146588 pIdx->pPartIdxWhere, iCursor)!=0 ){
146592 nn = pIdx->nKeyCol;
146595 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146596 if( pIdx->aiColumn[ii]==XN_EXPR ){
146597 assert( pIdx->aColExpr!=0 );
146598 assert( pIdx->aColExpr->nExpr>ii );
146599 pExpr = pIdx->aColExpr->a[ii].pExpr;
146600 if( pExpr->op!=TK_COLLATE ){
146606 sCol[1].iColumn = pIdx->aiColumn[ii];
146610 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
146624 pUpsert->pUpsertIdx = pIdx;
146627 if( pUpsert->pUpsertIdx==0 ){
146629 if( nClause==0 && pUpsert->pNextUpsert==0 ){
146650 pNext = pUpsert->pNextUpsert;
146652 if( pNext->pUpsertTarget==0 ) return 1;
146653 if( pNext->pUpsertIdx==0 ) return 1;
146666 && pUpsert->pUpsertTarget!=0
146667 && pUpsert->pUpsertIdx!=pIdx
146669 pUpsert = pUpsert->pNextUpsert;
146678 ** In this case parameter iCur is a cursor open on the table b-tree that
146684 Parse *pParse, /* The parsing and code-generating context */
146690 Vdbe *v = pParse->pVdbe;
146691 sqlite3 *db = pParse->db;
146699 iDataCur = pUpsert->iDataCur;
146711 int nPk = pPk->nKeyCol;
146712 int iPk = pParse->nMem+1;
146713 pParse->nMem += nPk;
146716 assert( pPk->aiColumn[i]>=0 );
146717 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
146719 VdbeComment((v, "%s.%s", pIdx->zName,
146720 pTab->aCol[pPk->aiColumn[i]].zCnName));
146731 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
146733 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
146735 for(i=0; i<pTab->nCol; i++){
146736 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
146737 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
146740 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
146741 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
146785 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
146857 if( pParse->nErr ) goto build_vacuum_end;
146866 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
146867 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
146870 iDb = sqlite3FindDb(pParse->db, pNm);
146877 iIntoReg = ++pParse->nMem;
146884 sqlite3ExprDelete(pParse->db, pInto);
146900 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
146901 u64 saved_flags; /* Saved value of db->flags */
146902 i64 saved_nChange; /* Saved value of db->nChange */
146903 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
146904 u32 saved_openFlags; /* Saved value of db->openFlags */
146913 if( !db->autoCommit ){
146915 return SQLITE_ERROR; /* IMP: R-12218-18073 */
146917 if( db->nVdbeActive>1 ){
146918 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
146919 return SQLITE_ERROR; /* IMP: R-15610-35227 */
146921 saved_openFlags = db->openFlags;
146924 sqlite3SetString(pzErrMsg, db, "non-text filename");
146928 db->openFlags &= ~SQLITE_OPEN_READONLY;
146929 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
146935 ** restored before returning. Then set the writable-schema flag, and
146937 saved_flags = db->flags;
146938 saved_mDbFlags = db->mDbFlags;
146939 saved_nChange = db->nChange;
146940 saved_nTotalChange = db->nTotalChange;
146941 saved_mTrace = db->mTrace;
146942 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
146943 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
146944 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
146946 db->mTrace = 0;
146948 zDbMain = db->aDb[iDb].zDbSName;
146949 pMain = db->aDb[iDb].pBt;
146958 ** An optimisation would be to use a non-journaled pager.
146966 nDb = db->nDb;
146968 db->openFlags = saved_openFlags;
146970 assert( (db->nDb-1)==nDb );
146971 pDb = &db->aDb[nDb];
146972 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
146973 pTemp = pDb->pBt;
146977 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
146982 db->mDbFlags |= DBFLAG_VacuumInto;
146986 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
146992 ** to ensure that we do not try to change the page-size on a WAL database.
147004 db->nextPagesize = 0;
147008 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
147009 || NEVER(db->mallocFailed)
147016 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
147023 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
147037 db->init.iDb = 0;
147050 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
147051 db->mDbFlags &= ~DBFLAG_Vacuum;
147070 ** both transactions are closed by this block - the main database
147124 /* Restore the original value of db->flags */
147125 db->init.iDb = 0;
147126 db->mDbFlags = saved_mDbFlags;
147127 db->flags = saved_flags;
147128 db->nChange = saved_nChange;
147129 db->nTotalChange = saved_nTotalChange;
147130 db->mTrace = saved_mTrace;
147131 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
147140 db->autoCommit = 1;
147143 sqlite3BtreeClose(pDb->pBt);
147144 pDb->pBt = 0;
147145 pDb->pSchema = 0;
147148 /* This both clears the schemas and reduces the size of the db->aDb[]
147219 pMod->zName = zCopy;
147220 pMod->pModule = pModule;
147221 pMod->pAux = pAux;
147222 pMod->xDestroy = xDestroy;
147223 pMod->pEpoTab = 0;
147224 pMod->nRefModule = 1;
147226 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
147254 sqlite3_mutex_enter(db->mutex);
147258 sqlite3_mutex_leave(db->mutex);
147264 ** External API function used to create a new virtual-table module.
147279 ** External API function used to create a new virtual-table module.
147295 ** External API to drop all virtual-table modules, except those named
147303 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
147308 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
147311 createModule(db, pMod->zName, 0, 0, 0);
147321 assert( pMod->nRefModule>0 );
147322 pMod->nRefModule--;
147323 if( pMod->nRefModule==0 ){
147324 if( pMod->xDestroy ){
147325 pMod->xDestroy(pMod->pAux);
147327 assert( pMod->pEpoTab==0 );
147341 pVTab->nRef++;
147346 ** pTab is a pointer to a Table structure representing a virtual-table.
147348 ** this virtual-table, if one has been created, or NULL otherwise.
147353 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
147358 ** Decrement the ref-count on a virtual table object. When the ref-count
147362 sqlite3 *db = pVTab->db;
147365 assert( pVTab->nRef>0 );
147366 assert( db->eOpenState==SQLITE_STATE_OPEN
147367 || db->eOpenState==SQLITE_STATE_ZOMBIE );
147369 pVTab->nRef--;
147370 if( pVTab->nRef==0 ){
147371 sqlite3_vtab *p = pVTab->pVtab;
147372 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
147374 p->pModule->xDisconnect(p);
147382 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
147385 ** connection db is left in the p->u.vtab.p list.
147392 pVTable = p->u.vtab.p;
147393 p->u.vtab.p = 0;
147399 ** database connection that may have an entry in the p->u.vtab.p list.
147401 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
147404 sqlite3 *db2 = pVTable->db;
147405 VTable *pNext = pVTable->pNext;
147409 p->u.vtab.p = pRet;
147410 pRet->pNext = 0;
147412 pVTable->pNext = db2->pDisconnect;
147413 db2->pDisconnect = pVTable;
147425 ** list in p->pVTab. It also decrements the VTable ref count. This is
147428 ** be being used by other shared-cache connections).
147435 assert( sqlite3_mutex_held(db->mutex) );
147437 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
147438 if( (*ppVTab)->db==db ){
147440 *ppVTab = pVTab->pNext;
147452 ** shared b-tree databases opened using connection db are held by the
147462 ** or, if the virtual table is stored in a non-sharable database, then
147466 ** by multiple threads. It is thread-safe.
147469 VTable *p = db->pDisconnect;
147472 assert( sqlite3_mutex_held(db->mutex) );
147475 db->pDisconnect = 0;
147478 VTable *pNext = p->pNext;
147486 ** Clear any and all virtual-table information from the Table record.
147490 ** Since it is a virtual-table, the Table structure contains a pointer
147501 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147502 if( p->u.vtab.azArg ){
147504 for(i=0; i<p->u.vtab.nArg; i++){
147505 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147507 sqlite3DbFree(db, p->u.vtab.azArg);
147512 ** Add a new module argument to pTable->u.vtab.azArg[].
147513 ** The string is not copied - the pointer is stored. The
147520 sqlite3 *db = pParse->db;
147523 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
147524 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
147525 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
147527 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
147531 int i = pTable->u.vtab.nArg++;
147534 pTable->u.vtab.azArg = azModuleArg;
147554 pTable = pParse->pNewTable;
147556 assert( 0==pTable->pIndex );
147557 pTable->eTabType = TABTYP_VTAB;
147559 db = pParse->db;
147561 assert( pTable->u.vtab.nArg==0 );
147564 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
147565 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
147566 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
147568 pParse->sNameToken.n = (int)(
147569 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
147578 if( pTable->u.vtab.azArg ){
147579 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
147581 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
147582 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
147589 ** in pParse->zArg[] and appends it to the list of arguments on the
147590 ** virtual table currently under construction in pParse->pTable.
147593 if( pParse->sArg.z && pParse->pNewTable ){
147594 const char *z = (const char*)pParse->sArg.z;
147595 int n = pParse->sArg.n;
147596 sqlite3 *db = pParse->db;
147597 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
147606 Table *pTab = pParse->pNewTable; /* The table being constructed */
147607 sqlite3 *db = pParse->db; /* The database connection */
147612 pParse->sArg.z = 0;
147613 if( pTab->u.vtab.nArg<1 ) return;
147621 if( !db->init.busy ){
147632 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
147634 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
147640 ** The VM register number pParse->regRowid holds the rowid of an
147644 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147649 db->aDb[iDb].zDbSName,
147650 pTab->zName,
147651 pTab->zName,
147653 pParse->regRowid
147659 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
147663 iReg = ++pParse->nMem;
147664 sqlite3VdbeLoadString(v, iReg, pTab->zName);
147667 /* If we are rereading the sqlite_schema table create the in-memory
147670 Schema *pSchema = pTab->pSchema;
147671 const char *zName = pTab->zName;
147674 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
147680 pParse->pNewTable = 0;
147690 pParse->sArg.z = 0;
147691 pParse->sArg.n = 0;
147699 Token *pArg = &pParse->sArg;
147700 if( pArg->z==0 ){
147701 pArg->z = p->z;
147702 pArg->n = p->n;
147704 assert(pArg->z <= p->z);
147705 pArg->n = (int)(&p->z[p->n] - pArg->z);
147725 int nArg = pTab->u.vtab.nArg;
147732 azArg = (const char *const*)pTab->u.vtab.azArg;
147734 /* Check that the virtual-table is not already being initialized */
147735 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
147736 if( pCtx->pTab==pTab ){
147738 "vtable constructor called recursively: %s", pTab->zName
147744 zModuleName = sqlite3DbStrDup(db, pTab->zName);
147755 pVTable->db = db;
147756 pVTable->pMod = pMod;
147757 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
147759 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147760 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
147763 assert( &db->pVtabCtx );
147767 sCtx.pPrior = db->pVtabCtx;
147769 db->pVtabCtx = &sCtx;
147770 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
147771 db->pVtabCtx = sCtx.pPrior;
147783 }else if( ALWAYS(pVTable->pVtab) ){
147786 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
147787 pVTable->pVtab->pModule = pMod->pModule;
147788 pMod->nRefModule++;
147789 pVTable->nRef = 1;
147792 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
147799 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
147803 pVTable->pNext = pTab->u.vtab.p;
147804 pTab->u.vtab.p = pVTable;
147806 for(iCol=0; iCol<pTab->nCol; iCol++){
147807 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
147813 && (i==0 || zType[i-1]==' ')
147826 assert(zType[i-1]==' ');
147827 zType[i-1] = '\0';
147829 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
147830 pTab->tabFlags |= TF_HasHidden;
147833 pTab->tabFlags |= oooHidden;
147848 ** This call is a no-op if table pTab is not a virtual table.
147851 sqlite3 *db = pParse->db;
147863 zMod = pTab->u.vtab.azArg[0];
147864 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147867 const char *zModule = pTab->u.vtab.azArg[0];
147872 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
147875 pParse->rc = rc;
147883 ** Grow the db->aVTrans[] array so that there is room for at least one
147884 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
147890 if( (db->nVTrans%ARRAY_INCR)==0 ){
147893 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
147894 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
147898 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
147899 db->aVTrans = aVTrans;
147911 db->aVTrans[db->nVTrans++] = pVTab;
147929 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
147930 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
147933 zMod = pTab->u.vtab.azArg[0];
147934 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147940 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
147944 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
147976 sqlite3_mutex_enter(db->mutex);
147977 pCtx = db->pVtabCtx;
147978 if( !pCtx || pCtx->bDeclared ){
147980 sqlite3_mutex_leave(db->mutex);
147983 pTab = pCtx->pTab;
147990 ** schema. Nevertheless, defend against that (turn off db->init.busy)
147992 assert( db->init.busy==0 );
147993 initBusy = db->init.busy;
147994 db->init.busy = 0;
147998 && ALWAYS(!db->mallocFailed)
148002 if( !pTab->aCol ){
148005 pTab->aCol = pNew->aCol;
148006 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
148007 pTab->nNVCol = pTab->nCol = pNew->nCol;
148008 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
148009 pNew->nCol = 0;
148010 pNew->aCol = 0;
148011 assert( pTab->pIndex==0 );
148014 && pCtx->pVTable->pMod->pModule->xUpdate!=0
148015 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
148017 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
148018 ** or else must have a single-column PRIMARY KEY */
148021 pIdx = pNew->pIndex;
148023 assert( pIdx->pNext==0 );
148024 pTab->pIndex = pIdx;
148025 pNew->pIndex = 0;
148026 pIdx->pTable = pTab;
148029 pCtx->bDeclared = 1;
148043 db->init.busy = initBusy;
148047 sqlite3_mutex_leave(db->mutex);
148056 ** This call is a no-op if zTab is not a virtual table.
148062 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
148065 && ALWAYS(pTab->u.vtab.p!=0)
148069 for(p=pTab->u.vtab.p; p; p=p->pNext){
148070 assert( p->pVtab );
148071 if( p->pVtab->nRef>0 ){
148076 xDestroy = p->pMod->pModule->xDestroy;
148077 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
148079 pTab->nTabRef++;
148080 rc = xDestroy(p->pVtab);
148083 assert( pTab->u.vtab.p==p && p->pNext==0 );
148084 p->pVtab = 0;
148085 pTab->u.vtab.p = 0;
148104 if( db->aVTrans ){
148105 VTable **aVTrans = db->aVTrans;
148106 db->aVTrans = 0;
148107 for(i=0; i<db->nVTrans; i++){
148109 sqlite3_vtab *p = pVTab->pVtab;
148112 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
148115 pVTab->iSavepoint = 0;
148119 db->nVTrans = 0;
148128 ** If an error message is available, leave it in p->zErrMsg.
148133 VTable **aVTrans = db->aVTrans;
148135 db->aVTrans = 0;
148136 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
148138 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
148139 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
148144 db->aVTrans = aVTrans;
148178 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
148189 pModule = pVTab->pVtab->pModule;
148191 if( pModule->xBegin ){
148195 for(i=0; i<db->nVTrans; i++){
148196 if( db->aVTrans[i]==pVTab ){
148205 rc = pModule->xBegin(pVTab->pVtab);
148207 int iSvpt = db->nStatement + db->nSavepoint;
148209 if( iSvpt && pModule->xSavepoint ){
148210 pVTab->iSavepoint = iSvpt;
148211 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
148238 assert( iSavepoint>=-1 );
148239 if( db->aVTrans ){
148241 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
148242 VTable *pVTab = db->aVTrans[i];
148243 const sqlite3_module *pMod = pVTab->pMod->pModule;
148244 if( pVTab->pVtab && pMod->iVersion>=2 ){
148249 xMethod = pMod->xSavepoint;
148250 pVTab->iSavepoint = iSavepoint+1;
148253 xMethod = pMod->xRollbackTo;
148256 xMethod = pMod->xRelease;
148259 if( xMethod && pVTab->iSavepoint>iSavepoint ){
148260 rc = xMethod(pVTab->pVtab, iSavepoint);
148298 if( pExpr->op!=TK_COLUMN ) return pDef;
148300 pTab = pExpr->y.pTab;
148303 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148305 assert( pVtab->pModule!=0 );
148306 pMod = (sqlite3_module *)pVtab->pModule;
148307 if( pMod->xFindFunction==0 ) return pDef;
148313 ** with an all lower-case function name. Continue in this tradition to
148319 for(i=0; pDef->zName[i]; i++){
148320 unsigned char x = (unsigned char)pDef->zName[i];
148325 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
148333 + sqlite3Strlen30(pDef->zName) + 1);
148338 pNew->zName = (const char*)&pNew[1];
148339 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
148340 pNew->xSFunc = xSFunc;
148341 pNew->pUserData = pArg;
148342 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
148347 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
148350 ** is a no-op.
148358 for(i=0; i<pToplevel->nVtabLock; i++){
148359 if( pTab==pToplevel->apVtabLock[i] ) return;
148361 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
148362 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
148364 pToplevel->apVtabLock = apVtabLock;
148365 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
148367 sqlite3OomFault(pToplevel->db);
148374 ** exist. Return non-zero if either the eponymous virtual table instance
148381 ** instances always exist. They cannot be DROP-ed.
148387 const sqlite3_module *pModule = pMod->pModule;
148391 sqlite3 *db = pParse->db;
148392 if( pMod->pEpoTab ) return 1;
148393 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
148396 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
148397 if( pTab->zName==0 ){
148401 pMod->pEpoTab = pTab;
148402 pTab->nTabRef = 1;
148403 pTab->eTabType = TABTYP_VTAB;
148404 pTab->pSchema = db->aDb[0].pSchema;
148405 assert( pTab->u.vtab.nArg==0 );
148406 pTab->iPKey = -1;
148407 pTab->tabFlags |= TF_Eponymous;
148408 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148410 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148411 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
148425 Table *pTab = pMod->pEpoTab;
148430 pTab->tabFlags |= TF_Ephemeral;
148432 pMod->pEpoTab = 0;
148452 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
148453 return (int)aMap[db->vtabOnConflict-1];
148469 sqlite3_mutex_enter(db->mutex);
148470 p = db->pVtabCtx;
148474 assert( p->pTab==0 || IsVirtual(p->pTab) );
148478 p->pVTable->bConstraint = (u8)va_arg(ap, int);
148482 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
148486 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
148498 sqlite3_mutex_leave(db->mutex);
148507 ** 2015-06-06
148520 ** This file was split off from where.c on 2015-06-06 in order to reduce the
148529 ** 2013-11-12
148598 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
148606 int addrSkip; /* Jump here for next iteration of skip-scan */
148610 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
148611 int addrBignull; /* Jump here for next part of big-null scan */
148621 union { /* Information that depends on pWLoop->wsFlags */
148627 int iBase; /* Base register of multi-key index record */
148631 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
148645 ** prevent a query solution - which is an error) and many terms of the
148647 ** potential way of implementing that FROM-clause term, together with
148663 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
148706 ** correspond to the subquery(s) of OR-clause processing. Only the
148722 ** vector, not a scalar, and because dependencies are many-to-one, not
148723 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
148739 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
148752 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
148783 ** spread out over the non-negative integers. For example, the cursor
148802 int iParent; /* Disable pWC->a[iParent] when this term disabled */
148812 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
148825 #define TERM_OK 0x0040 /* Used during OR-clause processing */
148831 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
148838 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
148849 int k; /* Resume scanning at this->pWC->a[this->k] */
148855 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
148877 int nBase; /* Number of terms through the last non-Virtual */
148915 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
148916 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
148923 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
148924 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
148963 ** routine. The limit is high enough that is should not impact real-world
149015 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149024 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
149039 ** Private interfaces - callable only by other where.c routines.
149091 int iLevel, /* Which level of pWInfo->a[] should be coded */
149119 ** OR-ed combination of these values can be used when searching for
149131 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
149132 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
149133 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
149134 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
149138 #define WO_OR 0x0200 /* Two or more OR-connected terms */
149139 #define WO_AND 0x0400 /* Two or more AND-connected terms */
149142 #define WO_ROWVAL 0x2000 /* A row-value term */
149145 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
149160 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
149168 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
149173 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
149175 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
149187 ** Return the name of the i-th column of the pIdx index.
149190 i = pIdx->aiColumn[i];
149193 return pIdx->pTable->aCol[i].zCnName;
149208 int iTerm, /* Zero-based index of first term. */
149209 int bAnd, /* Non-zero to append " AND " */
149249 Index *pIndex = pLoop->u.btree.pIndex;
149250 u16 nEq = pLoop->u.btree.nEq;
149251 u16 nSkip = pLoop->nSkip;
149254 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
149263 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
149264 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
149267 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
149268 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
149274 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
149276 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
149290 if( sqlite3ParseToplevel(pParse)->explain==2 )
149293 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
149294 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149295 sqlite3 *db = pParse->db; /* Database handle */
149303 pLoop = pLevel->pWLoop;
149304 flags = pLoop->wsFlags;
149308 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
149318 assert( pLoop->u.btree.pIndex!=0 );
149319 pIdx = pLoop->u.btree.pIndex;
149321 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
149336 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
149342 const Table *pTab = pItem->pTab;
149343 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
149365 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
149368 if( pItem->fg.jointype & JT_LEFT ){
149369 sqlite3_str_appendf(&str, " LEFT-JOIN");
149372 if( pLoop->nOut>=10 ){
149374 sqlite3LogEstToInt(pLoop->nOut));
149382 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
149392 ** required and this routine is a no-op.
149403 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
149404 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149405 sqlite3 *db = pParse->db; /* Database handle */
149415 pLoop = pLevel->pWLoop;
149416 if( pLoop->wsFlags & WHERE_IPK ){
149417 const Table *pTab = pItem->pTab;
149418 if( pTab->iPKey>=0 ){
149419 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
149424 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
149425 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
149426 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
149433 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
149455 WhereLoop *pLoop = pLvl->pWLoop;
149456 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
149457 zObj = pLoop->u.btree.pIndex->zName;
149459 zObj = pSrclist->a[pLvl->iFrom].zName;
149462 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
149508 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
149513 while( (pTerm->wtFlags & TERM_CODED)==0
149514 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
149515 && (pLevel->notReady & pTerm->prereqAll)==0
149517 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
149518 pTerm->wtFlags |= TERM_LIKECOND;
149520 pTerm->wtFlags |= TERM_CODED;
149524 sqlite3DebugPrintf("DISABLE-");
149525 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
149528 if( pTerm->iParent<0 ) break;
149529 pTerm = &pTerm->pWC->a[pTerm->iParent];
149531 pTerm->nChild--;
149532 if( pTerm->nChild!=0 ) break;
149542 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
149549 Vdbe *v = pParse->pVdbe;
149551 assert( pParse->db->mallocFailed );
149561 n--;
149565 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
149566 n--;
149637 sqlite3 *db = pParse->db;
149640 if( db->mallocFailed==0 ){
149649 pOrigRhs = pNew->x.pSelect->pEList;
149650 assert( pNew->pLeft!=0 );
149651 assert( ExprUseXList(pNew->pLeft) );
149652 pOrigLhs = pNew->pLeft->x.pList;
149653 for(i=iEq; i<pLoop->nLTerm; i++){
149654 if( pLoop->aLTerm[i]->pExpr==pX ){
149656 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
149657 iField = pLoop->aLTerm[i]->u.x.iField - 1;
149658 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
149659 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
149660 pOrigRhs->a[iField].pExpr = 0;
149661 assert( pOrigLhs->a[iField].pExpr!=0 );
149662 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
149663 pOrigLhs->a[iField].pExpr = 0;
149668 pNew->pLeft->x.pList = pLhs;
149669 pNew->x.pSelect->pEList = pRhs;
149670 if( pLhs && pLhs->nExpr==1 ){
149674 Expr *p = pLhs->a[0].pExpr;
149675 pLhs->a[0].pExpr = 0;
149676 sqlite3ExprDelete(db, pNew->pLeft);
149677 pNew->pLeft = p;
149679 pSelect = pNew->x.pSelect;
149680 if( pSelect->pOrderBy ){
149682 ** iOrderByCol variables. These are set to non-zero when an
149684 ** result-set. Since the result-set of the SELECT statement may
149688 ExprList *pOrderBy = pSelect->pOrderBy;
149689 for(i=0; i<pOrderBy->nExpr; i++){
149690 pOrderBy->a[i].u.x.iOrderByCol = 0;
149717 ** straight-line code. For constraints of the form X IN (...)
149725 int bRev, /* True for reverse-order IN operations */
149728 Expr *pX = pTerm->pExpr;
149729 Vdbe *v = pParse->pVdbe;
149732 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
149734 if( pX->op==TK_EQ || pX->op==TK_IS ){
149735 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
149736 }else if( pX->op==TK_ISNULL ){
149744 WhereLoop *pLoop = pLevel->pWLoop;
149749 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
149750 && pLoop->u.btree.pIndex!=0
149751 && pLoop->u.btree.pIndex->aSortOrder[iEq]
149757 assert( pX->op==TK_IN );
149761 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
149766 for(i=iEq;i<pLoop->nLTerm; i++){
149767 assert( pLoop->aLTerm[i]!=0 );
149768 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
149772 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
149775 Expr *pExpr = pTerm->pExpr;
149776 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
149777 sqlite3 *db = pParse->db;
149779 if( !db->mallocFailed ){
149780 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149782 pExpr->iTable = iTab;
149786 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149800 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
149801 pLoop->wsFlags |= WHERE_IN_ABLE;
149802 if( pLevel->u.in.nIn==0 ){
149803 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
149805 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
149806 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
149809 i = pLevel->u.in.nIn;
149810 pLevel->u.in.nIn += nEq;
149811 pLevel->u.in.aInLoop =
149812 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
149813 pLevel->u.in.aInLoop,
149814 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
149815 pIn = pLevel->u.in.aInLoop;
149819 for(i=iEq;i<pLoop->nLTerm; i++){
149820 if( pLoop->aLTerm[i]->pExpr==pX ){
149821 int iOut = iReg + i - iEq;
149823 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
149826 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
149830 pIn->iCur = iTab;
149831 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
149833 pIn->iBase = iReg - i;
149834 pIn->nPrefix = i;
149836 pIn->nPrefix = 0;
149839 pIn->eEndLoopOp = OP_Noop;
149845 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
149846 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
149848 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
149850 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
149853 pLevel->u.in.nIn = 0;
149855 sqlite3DbFree(pParse->db, aiMap);
149866 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
149868 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
149869 || (pTerm->eOperator & WO_EQUIV)==0
149890 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
149891 ** The only thing it does is allocate the pLevel->iMem memory cell and
149930 u16 nSkip; /* Number of left-most columns to skip */
149931 Vdbe *v = pParse->pVdbe; /* The vm under construction */
149941 pLoop = pLevel->pWLoop;
149942 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
149943 nEq = pLoop->u.btree.nEq;
149944 nSkip = pLoop->nSkip;
149945 pIdx = pLoop->u.btree.pIndex;
149950 regBase = pParse->nMem + 1;
149951 nReg = pLoop->u.btree.nEq + nExtraReg;
149952 pParse->nMem += nReg;
149954 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
149955 assert( zAff!=0 || pParse->db->mallocFailed );
149958 int iIdxCur = pLevel->iIdxCur;
149959 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
149963 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
149965 assert( pLevel->addrSkip==0 );
149966 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
149973 testcase( pIdx->aiColumn[j]==XN_EXPR );
149983 pTerm = pLoop->aLTerm[j];
149987 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
149988 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150000 pTerm = pLoop->aLTerm[j];
150001 if( pTerm->eOperator & WO_IN ){
150002 if( pTerm->pExpr->flags & EP_xIsSelect ){
150009 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
150010 Expr *pRight = pTerm->pExpr->pRight;
150011 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
150012 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
150015 if( pParse->nErr==0 ){
150016 assert( pParse->db->mallocFailed==0 );
150046 ** becomes a no-op.
150053 if( pTerm->wtFlags & TERM_LIKEOPT ){
150055 assert( pLevel->iLikeRepCntr>0 );
150056 pOp = sqlite3VdbeGetOp(v, -1);
150058 assert( pOp->opcode==OP_String8
150059 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
150060 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150061 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
150084 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
150087 struct CCurHint *pHint = pWalker->u.pCCurHint;
150088 assert( pHint->pIdx!=0 );
150089 if( pExpr->op==TK_COLUMN
150090 && pExpr->iTable==pHint->iTabCur
150091 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
150093 pWalker->eCode = 1;
150100 ** should be included in the cursor-hint for a table that is on the rhs
150101 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
150114 if( pExpr->op==TK_IS
150115 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
150116 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
150118 pWalker->eCode = 1;
150119 }else if( pExpr->op==TK_FUNCTION ){
150122 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
150123 pWalker->eCode = 1;
150150 struct CCurHint *pHint = pWalker->u.pCCurHint;
150151 if( pExpr->op==TK_COLUMN ){
150152 if( pExpr->iTable!=pHint->iTabCur ){
150153 int reg = ++pWalker->pParse->nMem; /* Register for column value */
150154 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
150155 pExpr->op = TK_REGISTER;
150156 pExpr->iTable = reg;
150157 }else if( pHint->pIdx!=0 ){
150158 pExpr->iTable = pHint->iIdxCur;
150159 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
150160 assert( pExpr->iColumn>=0 );
150162 }else if( pExpr->op==TK_AGG_FUNCTION ){
150164 ** be a correlated sub-query, and expression pExpr is an aggregate from
150182 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
150184 Parse *pParse = pWInfo->pParse;
150185 sqlite3 *db = pParse->db;
150186 Vdbe *v = pParse->pVdbe;
150188 WhereLoop *pLoop = pLevel->pWLoop;
150197 iCur = pLevel->iTabCur;
150198 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
150200 sHint.iIdxCur = pLevel->iIdxCur;
150201 sHint.pIdx = pLoop->u.btree.pIndex;
150205 pWC = &pWInfo->sWC;
150206 for(i=0; i<pWC->nBase; i++){
150207 pTerm = &pWC->a[i];
150208 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
150209 if( pTerm->prereqAll & pLevel->notReady ) continue;
150213 ** from the cursor-hint.
150233 if( pTabItem->fg.jointype & JT_LEFT ){
150234 Expr *pExpr = pTerm->pExpr;
150236 || pExpr->w.iJoin!=pTabItem->iCursor
150240 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150244 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
150247 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
150250 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
150251 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
150252 if( j<pLoop->nLTerm ) continue;
150255 /* No subqueries or non-deterministic functions allowed */
150256 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
150263 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150268 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
150279 # define codeCursorHint(A,B,C,D) /* No-op */
150283 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
150294 ** However, if the scan currently being coded is a branch of an OR-loop and
150311 int iCur, /* Cursor for IPK b-tree */
150314 Parse *pParse = pWInfo->pParse; /* Parse context */
150315 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
150318 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
150320 pWInfo->bDeferredSeek = 1;
150322 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
150323 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
150326 Table *pTab = pIdx->pTable;
150327 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
150329 ai[0] = pTab->nCol;
150330 for(i=0; i<pIdx->nColumn-1; i++){
150332 assert( pIdx->aiColumn[i]<pTab->nCol );
150333 x1 = pIdx->aiColumn[i];
150338 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
150357 Vdbe *v = pParse->pVdbe;
150359 assert( p->op==TK_SELECT );
150361 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
150368 pList = p->x.pList;
150369 assert( nReg<=pList->nExpr );
150371 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
150375 assert( nReg==1 || pParse->nErr );
150399 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
150401 pNew->pNext = pTrans->pWInfo->pExprMods;
150402 pTrans->pWInfo->pExprMods = pNew;
150403 pNew->pExpr = pExpr;
150404 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
150414 IdxExprTrans *pX = p->u.pIdxTrans;
150415 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
150418 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
150419 pExpr->op = TK_COLUMN;
150420 pExpr->iTable = pX->iIdxCur;
150421 pExpr->iColumn = pX->iIdxCol;
150424 pExpr->y.pTab = 0;
150436 if( pExpr->op==TK_COLUMN ){
150437 IdxExprTrans *pX = p->u.pIdxTrans;
150438 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
150439 assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 );
150441 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
150442 pExpr->iTable = pX->iIdxCur;
150443 pExpr->iColumn = pX->iIdxCol;
150444 pExpr->y.pTab = 0;
150456 ** 2019-10-24: Updated to also translate references to a VIRTUAL column in
150471 aColExpr = pIdx->aColExpr;
150472 if( aColExpr==0 && !pIdx->bHasVCol ){
150477 pTab = pIdx->pTable;
150483 x.db = pWInfo->pParse->db;
150484 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
150485 i16 iRef = pIdx->aiColumn[iIdxCol];
150487 assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 );
150488 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
150493 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
150494 && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0
150495 || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]),
150500 ** out of the index is an optimization only - the main table is always
150503 ** the column is non-standard */
150511 sqlite3WalkExpr(&w, pWInfo->pWhere);
150512 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
150513 sqlite3WalkExprList(&w, pWInfo->pResultSet);
150531 while( pTruth->op==TK_AND ){
150532 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
150533 pTruth = pTruth->pRight;
150535 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
150537 if( pTerm->wtFlags & TERM_CODED ) continue;
150538 pExpr = pTerm->pExpr;
150540 pTerm->wtFlags |= TERM_CODED;
150550 ** inner-loop Bloom filters, then evaluate those filters now, before the
150556 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
150563 int iLevel, /* Which level of pWInfo->a[] should be coded */
150567 while( ++iLevel < pWInfo->nLevel ){
150568 WhereLevel *pLevel = &pWInfo->a[iLevel];
150569 WhereLoop *pLoop = pLevel->pWLoop;
150570 if( pLevel->regFilter==0 ) continue;
150571 if( pLevel->pWLoop->nSkip ) continue;
150572 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
150573 ** vvvvv--' pLevel->regFilter if this were true. */
150574 if( NEVER(pLoop->prereq & notReady) ) continue;
150575 assert( pLevel->addrBrk==0 );
150576 pLevel->addrBrk = addrNxt;
150577 if( pLoop->wsFlags & WHERE_IPK ){
150578 WhereTerm *pTerm = pLoop->aLTerm[0];
150581 assert( pTerm->pExpr!=0 );
150582 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150585 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150587 VdbeCoverage(pParse->pVdbe);
150589 u16 nEq = pLoop->u.btree.nEq;
150593 assert( pLoop->wsFlags & WHERE_INDEXED );
150594 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
150597 sqlite3DbFree(pParse->db, zStartAff);
150598 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150600 VdbeCoverage(pParse->pVdbe);
150602 pLevel->regFilter = 0;
150603 pLevel->addrBrk = 0;
150608 ** Generate code for the start of the iLevel-th loop in the WHERE clause
150615 int iLevel, /* Which level of pWInfo->a[] should be coded */
150636 pWC = &pWInfo->sWC;
150637 db = pParse->db;
150638 pLoop = pLevel->pWLoop;
150639 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
150640 iCur = pTabItem->iCursor;
150641 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
150642 bRev = (pWInfo->revMask>>iLevel)&1;
150643 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
150647 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
150653 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
150655 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
150665 ** When there is an IN operator, we also have a "addrNxt" label that
150667 ** there are no IN operators in the constraints, the "addrNxt" label
150670 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
150671 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
150677 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
150678 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
150680 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
150681 pLevel->iLeftJoin = ++pParse->nMem;
150682 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
150683 VdbeComment((v, "init LEFT JOIN no-match flag"));
150688 for(j=iLevel; j>0; j--){
150689 if( pWInfo->a[j].iLeftJoin ) break;
150690 if( pWInfo->a[j].pRJ ) break;
150692 addrHalt = pWInfo->a[j].addrBrk;
150694 /* Special case of a FROM clause subquery implemented as a co-routine */
150695 if( pTabItem->fg.viaCoroutine ){
150696 int regYield = pTabItem->regReturn;
150697 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
150698 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
150700 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
150701 pLevel->op = OP_Goto;
150705 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
150706 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
150711 int nConstraint = pLoop->nLTerm;
150714 addrNotFound = pLevel->addrBrk;
150717 pTerm = pLoop->aLTerm[j];
150719 if( pTerm->eOperator & WO_IN ){
150720 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
150721 int iTab = pParse->nTab++;
150722 int iCache = ++pParse->nMem;
150723 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
150727 addrNotFound = pLevel->addrNxt;
150730 Expr *pRight = pTerm->pExpr->pRight;
150732 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
150733 && pLoop->u.vtab.bOmitOffset
150735 assert( pTerm->eOperator==WO_AUX );
150736 assert( pWInfo->pLimit!=0 );
150737 assert( pWInfo->pLimit->iOffset>0 );
150738 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
150743 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
150746 pLoop->u.vtab.idxStr,
150747 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
150749 pLoop->u.vtab.needFree = 0;
150751 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
150752 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
150753 pLevel->p1 = iCur;
150754 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
150755 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
150756 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
150759 pTerm = pLoop->aLTerm[j];
150760 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
150764 if( (pTerm->eOperator & WO_IN)!=0
150765 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
150766 && !db->mallocFailed
150771 int iIn; /* IN loop corresponding to the j-th constraint */
150778 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
150779 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
150780 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
150781 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
150783 testcase( pOp->opcode==OP_Rowid );
150784 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
150793 if( !db->mallocFailed ){
150794 int iFld = pTerm->u.x.iField;
150795 Expr *pLeft = pTerm->pExpr->pLeft;
150798 assert( pLeft->op==TK_VECTOR );
150800 assert( iFld<=pLeft->x.pList->nExpr );
150801 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
150803 pCompare->pLeft = pLeft;
150805 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
150807 pRight->iTable = iReg+j+2;
150809 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
150812 pCompare->pLeft = 0;
150820 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
150828 if( (pLoop->wsFlags & WHERE_IPK)!=0
150829 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
150836 assert( pLoop->u.btree.nEq==1 );
150837 pTerm = pLoop->aLTerm[0];
150839 assert( pTerm->pExpr!=0 );
150840 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150841 iReleaseReg = ++pParse->nMem;
150844 addrNxt = pLevel->addrNxt;
150845 if( pLevel->regFilter ){
150846 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
150853 pLevel->op = OP_Noop;
150854 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
150855 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
150866 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
150867 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
150893 assert( (pStart->wtFlags & TERM_VNULL)==0 );
150894 testcase( pStart->wtFlags & TERM_VIRTUAL );
150895 pX = pStart->pExpr;
150897 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
150898 if( sqlite3ExprIsVector(pX->pRight) ){
150900 codeExprOrVector(pParse, pX->pRight, r1, 1);
150901 testcase( pX->op==TK_GT );
150902 testcase( pX->op==TK_GE );
150903 testcase( pX->op==TK_LT );
150904 testcase( pX->op==TK_LE );
150905 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
150906 assert( pX->op!=TK_GT || op==OP_SeekGE );
150907 assert( pX->op!=TK_GE || op==OP_SeekGE );
150908 assert( pX->op!=TK_LT || op==OP_SeekLE );
150909 assert( pX->op!=TK_LE || op==OP_SeekLE );
150911 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
150913 op = aMoveOp[(pX->op - TK_GT)];
150917 VdbeCoverageIf(v, pX->op==TK_GT);
150918 VdbeCoverageIf(v, pX->op==TK_LE);
150919 VdbeCoverageIf(v, pX->op==TK_LT);
150920 VdbeCoverageIf(v, pX->op==TK_GE);
150929 pX = pEnd->pExpr;
150931 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
150932 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
150933 testcase( pEnd->wtFlags & TERM_VIRTUAL );
150934 memEndValue = ++pParse->nMem;
150935 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
150936 if( 0==sqlite3ExprIsVector(pX->pRight)
150937 && (pX->op==TK_LT || pX->op==TK_GT)
150943 if( 0==sqlite3ExprIsVector(pX->pRight) ){
150948 pLevel->op = bRev ? OP_Prev : OP_Next;
150949 pLevel->p1 = iCur;
150950 pLevel->p2 = start;
150951 assert( pLevel->p5==0 );
150953 iRowidReg = ++pParse->nMem;
150962 }else if( pLoop->wsFlags & WHERE_INDEXED ){
150967 ** left-most columns of the index. It may also contain
150970 ** the right-most column can be an inequality - the rest must
151010 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
151011 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
151012 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
151028 int regBignull = 0; /* big-null flag register */
151031 pIdx = pLoop->u.btree.pIndex;
151032 iIdxCur = pLevel->iIdxCur;
151033 assert( nEq>=pLoop->nSkip );
151039 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
151040 pRangeStart = pLoop->aLTerm[j++];
151041 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
151043 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
151044 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
151046 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
151047 pRangeEnd = pLoop->aLTerm[j++];
151048 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
151050 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
151052 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
151053 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
151054 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
151056 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
151060 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
151062 pLevel->iLikeRepCntr <<=1;
151063 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
151067 j = pIdx->aiColumn[nEq];
151068 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
151073 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
151076 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
151079 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
151082 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
151083 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
151087 testcase( pLoop->nSkip>0 );
151090 pLevel->regBignull = regBignull = ++pParse->nMem;
151091 if( pLevel->iLeftJoin ){
151094 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
151101 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
151107 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
151123 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
151125 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
151126 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
151127 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
151128 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
151129 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
151130 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
151136 Expr *pRight = pRangeStart->pExpr->pRight;
151139 if( (pRangeStart->wtFlags & TERM_VNULL)==0
151149 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
151166 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
151167 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
151168 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
151174 VdbeComment((v, "NULL-scan pass ctr"));
151176 if( pLevel->regFilter ){
151177 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
151185 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
151196 (pIdx->aiRowLogEst[0]+9)/10);
151216 nConstraint-startEq);
151230 assert( pLevel->p2==0 );
151232 Expr *pRight = pRangeEnd->pExpr->pRight;
151234 /* For a seek-scan that has a range on the lowest term of the index,
151237 ** over that initialization, leaving the range-end value set to the
151238 ** range-start value, resulting in a wrong answer.
151239 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
151241 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151245 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
151255 assert( pParse->db->mallocFailed );
151258 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
151276 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151281 /* Except, skip the end-of-range check while doing the NULL-scan */
151283 VdbeComment((v, "If NULL-scan 2nd pass"));
151295 /* During a NULL-scan, check to see if we have reached the end of
151301 VdbeComment((v, "If NULL-scan 1st pass"));
151312 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
151317 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
151318 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
151321 }else if( HasRowid(pIdx->pTable) ){
151324 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
151325 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
151326 for(j=0; j<pPk->nKeyCol; j++){
151327 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
151331 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151334 if( pLevel->iLeftJoin==0 ){
151347 ** Also, do not do this when processing one index an a multi-index
151352 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ){
151360 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
151363 if( pIdx->pPartIdxWhere ){
151364 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
151367 testcase( pIdx->pPartIdxWhere );
151369 ** The OR-optimization doesn't work for the right hand table of
151371 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
151375 if( pLoop->wsFlags & WHERE_ONEROW ){
151376 pLevel->op = OP_Noop;
151378 pLevel->op = OP_Prev;
151380 pLevel->op = OP_Next;
151382 pLevel->p1 = iIdxCur;
151383 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
151384 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
151385 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
151387 assert( pLevel->p5==0 );
151393 if( pLoop->wsFlags & WHERE_MULTI_OR ){
151420 ** Following the above, code to terminate the loop. Label A, the target
151432 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
151437 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
151438 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
151440 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
151442 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
151450 Table *pTab = pTabItem->pTab;
151452 pTerm = pLoop->aLTerm[0];
151454 assert( pTerm->eOperator & WO_OR );
151455 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
151456 pOrWc = &pTerm->u.pOrInfo->wc;
151457 pLevel->op = OP_Return;
151458 pLevel->p1 = regReturn;
151464 if( pWInfo->nLevel>1 ){
151467 nNotReady = pWInfo->nLevel - iLevel - 1;
151469 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
151471 pOrTab->nAlloc = (u8)(nNotReady + 1);
151472 pOrTab->nSrc = pOrTab->nAlloc;
151473 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
151474 origSrc = pWInfo->pTabList->a;
151476 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
151479 pOrTab = pWInfo->pTabList;
151490 ** correct response for the end-of-loop code (the OP_Return) is to
151494 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
151496 regRowset = ++pParse->nMem;
151500 regRowset = pParse->nTab++;
151501 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
151504 regRowid = ++pParse->nMem;
151514 ** the "interesting" terms of z - terms that did not originate in the
151522 ** 2022-02-04: Do not push down slices of a row-value comparison.
151524 ** the initialization of the right-hand operand of the vector comparison
151528 ** 2022-03-03: Do not push down expressions that involve subqueries.
151529 ** The subquery might get coded as a subroutine. Any table-references
151530 ** in the subquery might be resolved to index-references for the index on
151533 ** index-references will not work. tag-20220303a
151536 if( pWC->nTerm>1 ){
151538 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
151539 Expr *pExpr = pWC->a[iTerm].pExpr;
151540 if( &pWC->a[iTerm] == pTerm ) continue;
151541 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
151542 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
151543 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
151544 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
151547 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
151548 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
151556 ** prevents sqlite3PExpr() from applying the AND short-circuit
151564 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
151566 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
151567 for(ii=0; ii<pOrWc->nTerm; ii++){
151568 WhereTerm *pOrTerm = &pOrWc->a[ii];
151569 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
151570 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
151571 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
151578 if( db->mallocFailed ){
151583 pAndExpr->pLeft = pOrExpr;
151588 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
151591 assert( pSubWInfo || pParse->nErr );
151595 pParse, pOrTab, &pSubWInfo->a[0], 0
151597 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
151599 /* This is the sub-WHERE clause body. First skip over
151600 ** duplicate rows from prior sub-WHERE clauses, and record the
151602 ** row will be skipped in subsequent sub-WHERE clauses.
151604 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
151605 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
151607 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
151613 int nPk = pPk->nKeyCol;
151620 int iCol = pPk->aiColumn[iPk];
151632 ** the temp table. And if iSet is -1, assume that there is no
151655 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
151658 /* The pSubWInfo->untestedTerms flag means that this OR term
151663 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
151665 /* If all of the OR-connected terms are optimized using the same
151671 ** uses an index, and this is either the first OR-connected term
151677 pSubLoop = pSubWInfo->a[0].pWLoop;
151678 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
151679 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
151680 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
151681 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
151683 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
151684 pCov = pSubLoop->u.btree.pIndex;
151689 pWInfo->bDeferredSeek = 1;
151700 assert( pLevel->pWLoop==pLoop );
151701 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
151702 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
151703 pLevel->u.pCoveringIdx = pCov;
151704 if( pCov ) pLevel->iIdxCur = iCovCur;
151706 pAndExpr->pLeft = 0;
151710 sqlite3VdbeGoto(v, pLevel->addrBrk);
151715 ** loop. The byte-code formatter will use that P2 value as a hint to
151717 ** See tag-20220407a in vdbe.c and shell.c */
151718 assert( pLevel->op==OP_Return );
151719 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151721 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
151733 if( pTabItem->fg.isRecursive ){
151735 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
151736 pLevel->op = OP_Noop;
151739 pLevel->op = aStep[bRev];
151740 pLevel->p1 = iCur;
151741 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
151744 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
151749 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
151761 ** sub-queries.
151769 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
151772 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151773 testcase( pTerm->wtFlags & TERM_CODED );
151774 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151775 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151776 testcase( pWInfo->untestedTerms==0
151777 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
151778 pWInfo->untestedTerms = 1;
151781 pE = pTerm->pExpr;
151783 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
151786 ** join processing. tag-20220513a */
151788 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
151792 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
151793 if( m & pLevel->notReady ){
151799 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
151803 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
151808 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
151817 u32 x = pLevel->iLikeRepCntr;
151828 pWC->nTerm-j, pTerm, iLoop));
151832 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151837 pTerm->wtFlags |= TERM_CODED;
151850 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
151853 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151854 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
151855 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
151856 if( pTerm->leftCursor!=iCur ) continue;
151857 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
151858 pE = pTerm->pExpr;
151862 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151866 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
151867 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151868 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
151871 if( pAlt->wtFlags & (TERM_CODED) ) continue;
151872 if( (pAlt->eOperator & WO_IN)
151873 && ExprUseXSelect(pAlt->pExpr)
151874 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
151878 testcase( pAlt->eOperator & WO_EQ );
151879 testcase( pAlt->eOperator & WO_IS );
151880 testcase( pAlt->eOperator & WO_IN );
151882 sEAlt = *pAlt->pExpr;
151883 sEAlt.pLeft = pE->pLeft;
151885 pAlt->wtFlags |= TERM_CODED;
151891 if( pLevel->pRJ ){
151896 WhereRightJoin *pRJ = pLevel->pRJ;
151898 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
151903 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
151906 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
151911 nPk = pPk->nKeyCol;
151914 int iCol = pPk->aiColumn[iPk];
151918 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
151920 VdbeComment((v, "match against %s", pTab->zName));
151922 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
151923 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
151932 if( pLevel->iLeftJoin ){
151933 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
151934 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
151936 if( pLevel->pRJ==0 ){
151941 if( pLevel->pRJ ){
151944 ** be in-line with the rest of the code. But at the end, a separate
151948 WhereRightJoin *pRJ = pLevel->pRJ;
151949 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
151950 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
151951 assert( pParse->withinRJSubrtn < 255 );
151952 pParse->withinRJSubrtn++;
151957 ** appropriate WHERE clause constraint checks. tag-20220513a.
151960 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
151961 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151962 testcase( pTerm->wtFlags & TERM_CODED );
151963 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151964 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151965 assert( pWInfo->untestedTerms );
151968 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
151969 assert( pTerm->pExpr );
151970 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
151971 pTerm->wtFlags |= TERM_CODED;
151977 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
151983 iLevel, (u64)pLevel->notReady);
151986 return pLevel->notReady;
151990 ** Generate the code for the loop that finds all non-matched terms
151998 Parse *pParse = pWInfo->pParse;
151999 Vdbe *v = pParse->pVdbe;
152000 WhereRightJoin *pRJ = pLevel->pRJ;
152002 WhereClause *pWC = &pWInfo->sWC;
152004 WhereLoop *pLoop = pLevel->pWLoop;
152005 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152010 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
152011 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
152012 pRJ->regReturn);
152015 mAll |= pWInfo->a[k].pWLoop->maskSelf;
152016 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
152017 iIdxCur = pWInfo->a[k].iIdxCur;
152022 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
152023 mAll |= pLoop->maskSelf;
152024 for(k=0; k<pWC->nTerm; k++){
152025 WhereTerm *pTerm = &pWC->a[k];
152026 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
152027 && pTerm->eOperator!=WO_ROWVAL
152031 if( pTerm->prereqAll & ~mAll ) continue;
152032 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
152034 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
152041 assert( pParse->withinRJSubrtn < 100 );
152042 pParse->withinRJSubrtn++;
152046 int iCur = pLevel->iTabCur;
152047 int r = ++pParse->nMem;
152051 Table *pTab = pTabItem->pTab;
152053 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
152058 nPk = pPk->nKeyCol;
152059 pParse->nMem += nPk - 1;
152061 int iCol = pPk->aiColumn[iPk];
152065 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
152067 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
152070 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
152073 sqlite3ExprDelete(pParse->db, pSubWhere);
152075 assert( pParse->withinRJSubrtn>0 );
152076 pParse->withinRJSubrtn--;
152082 ** 2015-06-08
152109 sqlite3WhereClauseClear(&p->wc);
152117 sqlite3WhereClauseClear(&p->wc);
152124 ** The index in pWC->a[] of the new WhereTerm is returned on success.
152127 ** the db->mallocFailed flag so that higher-level functions can detect it.
152129 ** This routine will increase the size of the pWC->a[] array as necessary.
152138 ** the pWC->a[] array.
152144 if( pWC->nTerm>=pWC->nSlot ){
152145 WhereTerm *pOld = pWC->a;
152146 sqlite3 *db = pWC->pWInfo->pParse->db;
152147 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
152148 if( pWC->a==0 ){
152152 pWC->a = pOld;
152155 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
152156 pWC->nSlot = pWC->nSlot*2;
152158 pTerm = &pWC->a[idx = pWC->nTerm++];
152159 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
152161 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
152163 pTerm->truthProb = 1;
152165 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
152166 pTerm->wtFlags = wtFlags;
152167 pTerm->pWC = pWC;
152168 pTerm->iParent = -1;
152169 memset(&pTerm->eOperator, 0,
152170 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
152192 if( pExpr->pLeft->op==TK_VECTOR
152193 || pExpr->pRight->op==TK_VECTOR
152194 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
152195 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
152197 pExpr->flags ^= EP_Commuted;
152199 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
152200 if( pExpr->op>=TK_GT ){
152205 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
152206 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
152224 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
152225 c = (u16)(WO_EQ<<(op-TK_EQ));
152263 int cnt; /* Number of non-wildcard prefix characters */
152265 sqlite3 *db = pParse->db; /* Database connection */
152277 pList = pExpr->x.pList;
152278 pLeft = pList->a[1].pExpr;
152280 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
152281 op = pRight->op;
152282 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
152283 Vdbe *pReprepare = pParse->pReprepare;
152284 int iCol = pRight->iColumn;
152289 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
152290 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
152293 z = (u8*)pRight->u.zToken;
152305 ** with a wildcard and if (2) the non-wildcard prefix does not end with
152312 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
152324 zNew = pPrefix->u.zToken;
152340 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
152341 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
152342 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
152343 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
152344 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152346 if( pLeft->op!=TK_COLUMN
152349 && pLeft->y.pTab
152350 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
152356 if( iTo==1 && zNew[0]=='-' ){
152359 zNew[iTo-1]++;
152361 zNew[iTo-1]--;
152376 Vdbe *v = pParse->pVdbe;
152377 sqlite3VdbeSetVarmask(v, pRight->iColumn);
152379 if( *pisComplete && pRight->u.zToken[1] ){
152388 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
152410 ** ----------------------- ---------------------------------
152438 if( pExpr->op==TK_FUNCTION ){
152453 pList = pExpr->x.pList;
152454 if( pList==0 || pList->nExpr!=2 ){
152458 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
152460 ** the left-hand side operand in their in-fix form.
152465 pCol = pList->a[1].pExpr;
152466 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152467 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152471 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152473 *ppRight = pList->a[0].pExpr;
152486 ** Historically, xFindFunction expected to see lower-case function
152490 pCol = pList->a[0].pExpr;
152491 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152492 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152498 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
152500 assert( pVtab->pModule!=0 );
152502 pMod = (sqlite3_module *)pVtab->pModule;
152503 if( pMod->xFindFunction!=0 ){
152504 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
152507 *ppRight = pList->a[1].pExpr;
152513 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
152515 Expr *pLeft = pExpr->pLeft;
152516 Expr *pRight = pExpr->pRight;
152517 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
152518 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
152522 assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
152523 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
152530 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
152531 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
152532 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
152545 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
152546 pDerived->w.iJoin = pBase->w.iJoin;
152554 pWC->a[iChild].iParent = iParent;
152555 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
152556 pWC->a[iParent].nChild++;
152560 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
152565 if( pTerm->eOperator!=WO_AND ){
152568 if( N<pTerm->u.pAndInfo->wc.nTerm ){
152569 return &pTerm->u.pAndInfo->wc.a[N];
152576 ** two subterms are in disjunction - they are OR-ed together.
152586 ** x<y OR x=y --> x<=y
152587 ** x=y OR x=y --> x=y
152588 ** x<=y OR x<y --> x<=y
152592 ** x<y OR x>y --> x!=y
152600 u16 eOp = pOne->eOperator | pTwo->eOperator;
152606 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
152607 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
152608 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
152611 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
152612 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
152613 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
152614 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
152616 if( (eOp & (eOp-1))!=0 ){
152624 db = pWC->pWInfo->pParse->db;
152625 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
152627 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
152628 pNew->op = op;
152635 ** Analyze a term that consists of two or more OR-connected
152648 ** The term being analyzed must have two or more of OR-connected subterms.
152649 ** A single subterm might be a set of AND-connected sub-subterms.
152687 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
152724 int idxTerm /* Index of the OR-term to be analyzed */
152726 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
152727 Parse *pParse = pWInfo->pParse; /* Parser context */
152728 sqlite3 *db = pParse->db; /* Database connection */
152729 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
152730 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
152733 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
152743 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
152744 assert( pExpr->op==TK_OR );
152745 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
152747 pTerm->wtFlags |= TERM_ORINFO;
152748 pOrWc = &pOrInfo->wc;
152749 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
152753 if( db->mallocFailed ) return;
152754 assert( pOrWc->nTerm>=2 );
152761 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
152762 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
152764 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
152772 pOrTerm->u.pAndInfo = pAndInfo;
152773 pOrTerm->wtFlags |= TERM_ANDINFO;
152774 pOrTerm->eOperator = WO_AND;
152775 pOrTerm->leftCursor = -1;
152776 pAndWC = &pAndInfo->wc;
152777 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
152778 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
152779 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
152781 pAndWC->pOuter = pWC;
152782 if( !db->mallocFailed ){
152783 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
152784 assert( pAndTerm->pExpr );
152785 if( allowedOp(pAndTerm->pExpr->op)
152786 || pAndTerm->eOperator==WO_AUX
152788 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
152794 }else if( pOrTerm->wtFlags & TERM_COPIED ){
152799 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
152800 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
152801 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
152802 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
152805 if( (pOrTerm->eOperator & WO_EQ)==0 ){
152817 pOrInfo->indexable = indexable;
152818 pTerm->eOperator = WO_OR;
152819 pTerm->leftCursor = -1;
152821 pWC->hasOr = 1;
152824 /* For a two-way OR, attempt to implementation case 2.
152826 if( indexable && pOrWc->nTerm==2 ){
152829 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
152832 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
152843 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
152846 ** something other than == on a column in the single table. The 1-bit
152850 ** sure the same column is used on all terms. The 2-bit case is when
152861 int iColumn = -1; /* Column index on lhs of IN operator */
152862 int iCursor = -1; /* Table cursor common to all terms */
152873 pOrTerm = pOrWc->a;
152874 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
152875 assert( pOrTerm->eOperator & WO_EQ );
152876 pOrTerm->wtFlags &= ~TERM_OK;
152877 if( pOrTerm->leftCursor==iCursor ){
152878 /* This is the 2-bit case and we are on the second iteration and
152883 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
152884 pOrTerm->leftCursor))==0 ){
152889 testcase( pOrTerm->wtFlags & TERM_COPIED );
152890 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
152891 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
152894 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152895 iColumn = pOrTerm->u.x.leftColumn;
152896 iCursor = pOrTerm->leftCursor;
152897 pLeft = pOrTerm->pExpr->pLeft;
152905 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
152913 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
152914 assert( pOrTerm->eOperator & WO_EQ );
152915 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152916 if( pOrTerm->leftCursor!=iCursor ){
152917 pOrTerm->wtFlags &= ~TERM_OK;
152918 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
152919 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
152924 /* If the right-hand side is also a column, then the affinities
152928 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
152929 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
152933 pOrTerm->wtFlags |= TERM_OK;
152949 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
152950 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
152951 assert( pOrTerm->eOperator & WO_EQ );
152952 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152953 assert( pOrTerm->leftCursor==iCursor );
152954 assert( pOrTerm->u.x.leftColumn==iColumn );
152955 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
152956 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
152957 pLeft = pOrTerm->pExpr->pLeft;
152966 pNew->x.pList = pList;
152970 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
152998 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
152999 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
153001 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
153002 aff2 = sqlite3ExprAffinity(pExpr->pRight);
153010 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
153021 SrcList *pSrc = pS->pSrc;
153022 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
153023 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
153024 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
153025 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
153026 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
153029 for(i=0; i<pSrc->nSrc; i++){
153030 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
153031 if( pSrc->a[i].fg.isUsing==0 ){
153032 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
153034 if( pSrc->a[i].fg.isTabFunc ){
153035 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
153039 pS = pS->pPrior;
153050 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
153067 iCur = pFrom->a[i].iCursor;
153068 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153069 if( pIdx->aColExpr==0 ) continue;
153070 for(i=0; i<pIdx->nKeyCol; i++){
153071 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
153072 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153094 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
153096 pExpr = pExpr->x.pList->a[0].pExpr;
153100 if( pExpr->op==TK_COLUMN ){
153101 aiCurCol[0] = pExpr->iTable;
153102 aiCurCol[1] = pExpr->iColumn;
153106 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
153134 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
153138 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
153144 int op; /* Top-level operator. pExpr->op */
153145 Parse *pParse = pWInfo->pParse; /* Parsing context */
153146 sqlite3 *db = pParse->db; /* Database connection */
153150 if( db->mallocFailed ){
153153 assert( pWC->nTerm > idxTerm );
153154 pTerm = &pWC->a[idxTerm];
153155 pMaskSet = &pWInfo->sMaskSet;
153156 pExpr = pTerm->pExpr;
153158 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
153159 pMaskSet->bVarSelect = 0;
153160 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
153161 op = pExpr->op;
153163 assert( pExpr->pRight==0 );
153166 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
153168 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
153170 prereqAll = prereqLeft | pTerm->prereqRight;
153172 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
153173 if( pExpr->pLeft==0
153175 || pExpr->x.pList!=0
153179 prereqAll = prereqLeft | pTerm->prereqRight;
153182 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
153193 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
153196 extraRight = x-1; /* ON clause terms may not be used with an index
153209 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
153216 pTerm->prereqAll = prereqAll;
153217 pTerm->leftCursor = -1;
153218 pTerm->iParent = -1;
153219 pTerm->eOperator = 0;
153222 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
153223 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
153224 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
153226 if( pTerm->u.x.iField>0 ){
153228 assert( pLeft->op==TK_VECTOR );
153230 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
153234 pTerm->leftCursor = aiCurCol[0];
153235 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
153236 pTerm->u.x.leftColumn = aiCurCol[1];
153237 pTerm->eOperator = operatorMask(op) & opMask;
153239 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
153241 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
153246 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
153247 assert( pTerm->u.x.iField==0 );
153248 if( pTerm->leftCursor>=0 ){
153251 if( db->mallocFailed ){
153257 pNew = &pWC->a[idxNew];
153259 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
153260 pTerm = &pWC->a[idxTerm];
153261 pTerm->wtFlags |= TERM_COPIED;
153264 pTerm->eOperator |= WO_EQUIV;
153271 pNew->wtFlags |= exprCommute(pParse, pDup);
153272 pNew->leftCursor = aiCurCol[0];
153273 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
153274 pNew->u.x.leftColumn = aiCurCol[1];
153276 pNew->prereqRight = prereqLeft | extraRight;
153277 pNew->prereqAll = prereqAll;
153278 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
153285 pExpr->op = TK_TRUEFALSE;
153286 pExpr->u.zToken = "false";
153288 pTerm->prereqAll = 0;
153289 pTerm->eOperator = 0;
153309 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
153314 pList = pExpr->x.pList;
153316 assert( pList->nExpr==2 );
153321 sqlite3ExprDup(db, pExpr->pLeft, 0),
153322 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
153327 pTerm = &pWC->a[idxTerm];
153337 else if( pExpr->op==TK_OR ){
153338 assert( pWC->op==TK_AND );
153340 pTerm = &pWC->a[idxTerm];
153349 else if( pExpr->op==TK_NOTNULL ){
153350 if( pExpr->pLeft->op==TK_COLUMN
153351 && pExpr->pLeft->iColumn>=0
153355 Expr *pLeft = pExpr->pLeft;
153366 pNewTerm = &pWC->a[idxNew];
153367 pNewTerm->prereqRight = 0;
153368 pNewTerm->leftCursor = pLeft->iTable;
153369 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153370 pNewTerm->eOperator = WO_GT;
153372 pTerm = &pWC->a[idxTerm];
153373 pTerm->wtFlags |= TERM_COPIED;
153374 pNewTerm->prereqAll = pTerm->prereqAll;
153390 ** for LIKE) then the lower-bound is made all uppercase and the upper-
153394 else if( pExpr->op==TK_FUNCTION
153395 && pWC->op==TK_AND
153399 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
153408 pLeft = pExpr->x.pList->a[1].pExpr;
153414 /* Convert the lower bound to upper-case and the upper bound to
153415 ** lower-case (upper-case is less than lower-case in ASCII) so that
153418 if( noCase && !pParse->db->mallocFailed ){
153421 pTerm->wtFlags |= TERM_LIKE;
153422 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
153423 pStr1->u.zToken[i] = sqlite3Toupper(c);
153424 pStr2->u.zToken[i] = sqlite3Tolower(c);
153428 if( !db->mallocFailed ){
153430 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
153439 if( c=='A'-1 ) isComplete = 0;
153461 pTerm = &pWC->a[idxTerm];
153469 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
153470 ** new terms for each component comparison - "a = ?" and "b = ?". The
153475 ** is not a sub-select.
153477 ** tag-20220128a
153479 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
153480 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
153481 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
153482 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
153483 || (pExpr->pRight->flags & EP_xIsSelect)==0)
153484 && pWC->op==TK_AND
153490 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
153491 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
153493 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
153498 pTerm = &pWC->a[idxTerm];
153499 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
153500 pTerm->eOperator = WO_ROWVAL;
153503 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
153512 else if( pExpr->op==TK_IN
153513 && pTerm->u.x.iField==0
153514 && pExpr->pLeft->op==TK_VECTOR
153516 && pExpr->x.pSelect->pPrior==0
153518 && pExpr->x.pSelect->pWin==0
153520 && pWC->op==TK_AND
153523 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
153526 pWC->a[idxNew].u.x.iField = i+1;
153542 else if( pWC->op==TK_AND ){
153545 while( res-- > 0 ){
153558 pNewExpr->w.iJoin = pExpr->w.iJoin;
153562 pNewTerm = &pWC->a[idxNew];
153563 pNewTerm->prereqRight = prereqExpr;
153564 pNewTerm->leftCursor = pLeft->iTable;
153565 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153566 pNewTerm->eOperator = WO_AUX;
153567 pNewTerm->eMatchOp = eOp2;
153569 pTerm = &pWC->a[idxTerm];
153570 pTerm->wtFlags |= TERM_COPIED;
153571 pNewTerm->prereqAll = pTerm->prereqAll;
153581 testcase( pTerm!=&pWC->a[idxTerm] );
153582 pTerm = &pWC->a[idxTerm];
153583 pTerm->prereqRight |= extraRight;
153610 pWC->op = op;
153613 if( pE2->op!=op ){
153616 sqlite3WhereSplit(pWC, pE2->pLeft, op);
153617 sqlite3WhereSplit(pWC, pE2->pRight, op);
153624 ** where-clause passed as the first argument. The value for the term
153639 Parse *pParse = pWC->pWInfo->pParse;
153640 sqlite3 *db = pParse->db;
153648 pVal->u.iValue = iVal;
153653 pVal->iTable = iReg;
153660 pTerm = &pWC->a[idx];
153661 pTerm->leftCursor = iCsr;
153662 pTerm->eOperator = WO_AUX;
153663 pTerm->eMatchOp = eMatchOp;
153686 assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
153687 if( (p && p->pLimit) /* 1 */
153688 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
153689 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
153691 ExprList *pOrderBy = p->pOrderBy;
153692 int iCsr = p->pSrc->a[0].iCursor;
153696 for(ii=0; ii<pWC->nTerm; ii++){
153697 if( pWC->a[ii].wtFlags & TERM_CODED ){
153699 ** other, subsequent terms. It can be ignored. See tag-20220128a */
153700 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
153701 assert( pWC->a[ii].eOperator==WO_ROWVAL );
153704 if( pWC->a[ii].leftCursor!=iCsr ) return;
153709 for(ii=0; ii<pOrderBy->nExpr; ii++){
153710 Expr *pExpr = pOrderBy->a[ii].pExpr;
153711 if( pExpr->op!=TK_COLUMN ) return;
153712 if( pExpr->iTable!=iCsr ) return;
153713 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
153717 /* All conditions are met. Add the terms to the where-clause object. */
153718 assert( p->pLimit->op==TK_LIMIT );
153719 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
153721 if( p->iOffset>0 ){
153722 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
153735 pWC->pWInfo = pWInfo;
153736 pWC->hasOr = 0;
153737 pWC->pOuter = 0;
153738 pWC->nTerm = 0;
153739 pWC->nBase = 0;
153740 pWC->nSlot = ArraySize(pWC->aStatic);
153741 pWC->a = pWC->aStatic;
153750 sqlite3 *db = pWC->pWInfo->pParse->db;
153751 assert( pWC->nTerm>=pWC->nBase );
153752 if( pWC->nTerm>0 ){
153753 WhereTerm *a = pWC->a;
153754 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
153757 /* Verify that every term past pWC->nBase is virtual */
153758 for(i=pWC->nBase; i<pWC->nTerm; i++){
153759 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
153763 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
153764 if( a->wtFlags & TERM_DYNAMIC ){
153765 sqlite3ExprDelete(db, a->pExpr);
153767 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
153768 if( a->wtFlags & TERM_ORINFO ){
153769 assert( (a->wtFlags & TERM_ANDINFO)==0 );
153770 whereOrInfoDelete(db, a->u.pOrInfo);
153772 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
153773 whereAndInfoDelete(db, a->u.pAndInfo);
153788 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
153793 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
153798 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
153804 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
153811 ** "no-inline" function to avoid the stack push overhead in the
153819 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
153820 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
153821 if( p->pRight ){
153822 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
153823 assert( p->x.pList==0 );
153825 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
153826 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
153827 }else if( p->x.pList ){
153828 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
153831 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
153832 assert( p->y.pWin!=0 );
153833 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
153834 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
153835 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
153841 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
153842 return sqlite3WhereGetMask(pMaskSet, p->iTable);
153844 assert( p->op!=TK_IF_NULL_ROW );
153856 for(i=0; i<pList->nExpr; i++){
153857 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
153877 for(i=pWC->nTerm-1; i>=0; i--){
153883 ** For table-valued-functions, transform the function arguments into
153899 if( pItem->fg.isTabFunc==0 ) return;
153900 pTab = pItem->pTab;
153902 pArgs = pItem->u1.pFuncArg;
153904 for(j=k=0; j<pArgs->nExpr; j++){
153907 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
153908 if( k>=pTab->nCol ){
153909 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
153910 pTab->zName, j);
153913 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
153915 pColRef->iTable = pItem->iCursor;
153916 pColRef->iColumn = k++;
153918 pColRef->y.pTab = pTab;
153919 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
153921 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
153923 if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ) ){
153928 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
153984 return pWInfo->nRowOut;
153992 return pWInfo->eDistinct;
154004 return pWInfo->nOBSat;
154008 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
154010 ** inner-most loop did not fit within the sorter, then we can skip all
154013 ** loop - the loop immediately outside the inner-most.
154017 ** label returned by this function.
154020 ** be the continuation for the second-inner-most loop. If the ORDER BY
154022 ** be the continuation for the inner-most loop.
154025 ** inner-most loop, in the sense that a correct answer will result.
154032 if( !pWInfo->bOrderedInnerLoop ){
154034 ** continuation of the inner-most loop. */
154035 return pWInfo->iContinue;
154037 pInner = &pWInfo->a[pWInfo->nLevel-1];
154038 assert( pInner->addrNxt!=0 );
154039 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
154044 ** the aggregate-step call to min() or max(), check to see if any
154056 if( !pWInfo->bOrderedInnerLoop ) return;
154057 if( pWInfo->nOBSat==0 ) return;
154058 for(i=pWInfo->nLevel-1; i>=0; i--){
154059 pInner = &pWInfo->a[i];
154060 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
154061 sqlite3VdbeGoto(v, pInner->addrNxt);
154065 sqlite3VdbeGoto(v, pWInfo->iBreak);
154069 ** Return the VDBE address or label to jump to in order to continue
154073 assert( pWInfo->iContinue!=0 );
154074 return pWInfo->iContinue;
154078 ** Return the VDBE address or label to jump to in order to break
154082 return pWInfo->iBreak;
154089 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
154096 ** Either value may be -1, indicating that cursor is not used.
154099 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
154103 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
154105 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
154107 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
154111 return pWInfo->eOnePass;
154119 return pWInfo->bDeferredSeek;
154126 pDest->n = pSrc->n;
154127 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
154140 LogEst rRun, /* Run-cost of the new entry */
154145 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
154146 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
154149 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
154153 if( pSet->n<N_OR_COST ){
154154 p = &pSet->a[pSet->n++];
154155 p->nOut = nOut;
154157 p = pSet->a;
154158 for(i=1; i<pSet->n; i++){
154159 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
154161 if( p->rRun<=rRun ) return 0;
154164 p->prereq = prereq;
154165 p->rRun = rRun;
154166 if( p->nOut>nOut ) p->nOut = nOut;
154176 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
154177 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
154178 assert( iCursor>=-1 );
154179 if( pMaskSet->ix[0]==iCursor ){
154182 for(i=1; i<pMaskSet->n; i++){
154183 if( pMaskSet->ix[i]==iCursor ){
154194 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
154196 pBlock->pNext = pWInfo->pMemToFree;
154197 pBlock->sz = nByte;
154198 pWInfo->pMemToFree = pBlock;
154207 pOldBlk--;
154208 assert( pOldBlk->sz<nByte );
154209 memcpy(pNew, pOld, pOldBlk->sz);
154219 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
154223 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
154224 pMaskSet->ix[pMaskSet->n++] = iCursor;
154228 ** If the right-hand branch of the expression is a TK_COLUMN, then return
154229 ** a pointer to the right-hand branch. Otherwise, return NULL.
154232 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
154233 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
154246 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
154248 WhereClause *pWC; /* Shorthand for pScan->pWC */
154250 int k = pScan->k; /* Where to start scanning */
154252 assert( pScan->iEquiv<=pScan->nEquiv );
154253 pWC = pScan->pWC;
154255 iColumn = pScan->aiColumn[pScan->iEquiv-1];
154256 iCur = pScan->aiCur[pScan->iEquiv-1];
154260 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
154261 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
154262 if( pTerm->leftCursor==iCur
154263 && pTerm->u.x.leftColumn==iColumn
154265 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
154266 pScan->pIdxExpr,iCur)==0)
154267 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
154269 if( (pTerm->eOperator & WO_EQUIV)!=0
154270 && pScan->nEquiv<ArraySize(pScan->aiCur)
154271 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
154274 for(j=0; j<pScan->nEquiv; j++){
154275 if( pScan->aiCur[j]==pX->iTable
154276 && pScan->aiColumn[j]==pX->iColumn ){
154280 if( j==pScan->nEquiv ){
154281 pScan->aiCur[j] = pX->iTable;
154282 pScan->aiColumn[j] = pX->iColumn;
154283 pScan->nEquiv++;
154286 if( (pTerm->eOperator & pScan->opMask)!=0 ){
154288 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
154290 Parse *pParse = pWC->pWInfo->pParse;
154291 pX = pTerm->pExpr;
154292 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
154295 assert(pX->pLeft);
154297 if( pColl==0 ) pColl = pParse->db->pDfltColl;
154298 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
154302 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
154303 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
154304 && pX->op==TK_COLUMN
154305 && pX->iTable==pScan->aiCur[0]
154306 && pX->iColumn==pScan->aiColumn[0]
154308 testcase( pTerm->eOperator & WO_IS );
154311 pScan->pWC = pWC;
154312 pScan->k = k+1;
154316 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
154317 pTerm, pScan->nEquiv);
154318 for(ii=0; ii<pScan->nEquiv; ii++){
154320 pScan->aiCur[ii], pScan->aiColumn[ii]);
154329 pWC = pWC->pOuter;
154332 if( pScan->iEquiv>=pScan->nEquiv ) break;
154333 pWC = pScan->pOrigWC;
154335 pScan->iEquiv++;
154342 ** It is factored out into a separate tail-recursion subroutine so that
154343 ** the normal whereScanInit() routine, which is a high-runner, does not
154347 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
154378 pScan->pOrigWC = pWC;
154379 pScan->pWC = pWC;
154380 pScan->pIdxExpr = 0;
154381 pScan->idxaff = 0;
154382 pScan->zCollName = 0;
154383 pScan->opMask = opMask;
154384 pScan->k = 0;
154385 pScan->aiCur[0] = iCur;
154386 pScan->nEquiv = 1;
154387 pScan->iEquiv = 1;
154390 iColumn = pIdx->aiColumn[j];
154391 if( iColumn==pIdx->pTable->iPKey ){
154394 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
154395 pScan->zCollName = pIdx->azColl[j];
154397 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
154398 pScan->zCollName = pIdx->azColl[j];
154399 pScan->aiColumn[0] = XN_EXPR;
154405 pScan->aiColumn[0] = iColumn;
154416 ** Search for terms matching the iColumn-th column of pIdx
154417 ** rather than the iColumn-th column of table iCur.
154421 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
154428 ** then try for the one with no dependencies on <expr> - in other words where
154431 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
154449 if( (p->prereqRight & notReady)==0 ){
154450 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
154451 testcase( p->eOperator & WO_IS );
154462 ** This function searches pList for an entry that matches the iCol-th column
154465 ** If such an expression is found, its index in pList->a[] is returned. If
154466 ** no expression is found, -1 is returned.
154476 const char *zColl = pIdx->azColl[iCol];
154478 for(i=0; i<pList->nExpr; i++){
154479 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
154481 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
154482 && p->iColumn==pIdx->aiColumn[iCol]
154483 && p->iTable==iBase
154485 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
154486 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
154492 return -1;
154496 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
154501 assert( iCol>=0 && iCol<pIdx->nColumn );
154502 j = pIdx->aiColumn[iCol];
154504 return pIdx->pTable->aCol[j].notNull;
154505 }else if( j==(-1) ){
154508 assert( j==(-2) );
154515 ** Return true if the DISTINCT expression-list passed as the third argument
154519 ** DISTINCT list are collectively unique and individually non-null.
154532 /* If there is more than one table or sub-select in the FROM clause of
154535 if( pTabList->nSrc!=1 ) return 0;
154536 iBase = pTabList->a[0].iCursor;
154537 pTab = pTabList->a[0].pTab;
154540 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
154541 ** current SELECT is a correlated sub-query.
154543 for(i=0; i<pDistinct->nExpr; i++){
154544 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
154546 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
154547 if( p->iTable==iBase && p->iColumn<0 ) return 1;
154558 ** comparison and select-list expressions must match those of the index.
154563 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154565 if( pIdx->pPartIdxWhere ) continue;
154566 for(i=0; i<pIdx->nKeyCol; i++){
154572 if( i==pIdx->nKeyCol ){
154586 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
154593 ** opcodes into OP_Copy when the table is being accessed via co-routine
154606 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
154608 Vdbe *v = pParse->pVdbe;
154611 if( pParse->db->mallocFailed ) return;
154613 if( pOp->p1!=iTabCur ) continue;
154614 if( pOp->opcode==OP_Column ){
154615 pOp->opcode = OP_Copy;
154616 pOp->p1 = pOp->p2 + iRegister;
154617 pOp->p2 = pOp->p3;
154618 pOp->p3 = 0;
154619 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
154620 }else if( pOp->opcode==OP_Rowid ){
154621 pOp->opcode = OP_Sequence;
154622 pOp->p1 = iAutoidxCur;
154625 pOp->opcode = OP_Null;
154626 pOp->p3 = 0;
154637 ** are no-ops.
154643 for(i=0; i<p->nConstraint; i++){
154647 p->aConstraint[i].iColumn,
154648 p->aConstraint[i].iTermOffset,
154649 p->aConstraint[i].op,
154650 p->aConstraint[i].usable,
154653 for(i=0; i<p->nOrderBy; i++){
154656 p->aOrderBy[i].iColumn,
154657 p->aOrderBy[i].desc);
154663 for(i=0; i<p->nConstraint; i++){
154666 p->aConstraintUsage[i].argvIndex,
154667 p->aConstraintUsage[i].omit);
154669 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
154670 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
154671 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
154672 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
154673 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
154692 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
154693 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
154694 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
154695 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
154696 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
154697 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
154698 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
154699 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
154700 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
154701 || pTerm->pExpr->w.iJoin != pSrc->iCursor
154703 return 0; /* See tag-20191211-001 */
154706 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
154707 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154708 if( pTerm->u.x.leftColumn<0 ) return 0;
154709 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
154710 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
154711 testcase( pTerm->pExpr->op==TK_IS );
154732 WhereTerm *pWCEnd; /* End of pWC->a[] */
154741 int mxBitCol; /* Maximum column in pSrc->colUsed */
154756 v = pParse->pVdbe;
154763 pTable = pSrc->pTab;
154764 pWCEnd = &pWC->a[pWC->nTerm];
154765 pLoop = pLevel->pWLoop;
154767 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154768 Expr *pExpr = pTerm->pExpr;
154772 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
154776 sqlite3ExprDup(pParse->db, pExpr, 0));
154781 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154782 iCol = pTerm->u.x.leftColumn;
154783 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
154785 testcase( iCol==BMS-1 );
154788 "automatic index on %s(%s)", pTable->zName,
154789 pTable->aCol[iCol].zCnName);
154793 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
154796 pLoop->aLTerm[nKeyCol++] = pTerm;
154801 assert( nKeyCol>0 || pParse->db->mallocFailed );
154802 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
154803 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
154814 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
154815 mxBitCol = MIN(BMS-1,pTable->nCol);
154816 testcase( pTable->nCol==BMS-1 );
154817 testcase( pTable->nCol==BMS-2 );
154821 if( pSrc->colUsed & MASKBIT(BMS-1) ){
154822 nKeyCol += pTable->nCol - BMS + 1;
154826 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
154828 pLoop->u.btree.pIndex = pIdx;
154829 pIdx->zName = "auto-index";
154830 pIdx->pTable = pTable;
154833 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154837 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154838 iCol = pTerm->u.x.leftColumn;
154839 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
154840 testcase( iCol==BMS-1 );
154843 Expr *pX = pTerm->pExpr;
154845 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
154847 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
154848 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
154853 assert( (u32)n==pLoop->u.btree.nEq );
154859 pIdx->aiColumn[n] = i;
154860 pIdx->azColl[n] = sqlite3StrBINARY;
154864 if( pSrc->colUsed & MASKBIT(BMS-1) ){
154865 for(i=BMS-1; i<pTable->nCol; i++){
154866 pIdx->aiColumn[n] = i;
154867 pIdx->azColl[n] = sqlite3StrBINARY;
154872 pIdx->aiColumn[n] = XN_ROWID;
154873 pIdx->azColl[n] = sqlite3StrBINARY;
154876 assert( pLevel->iIdxCur>=0 );
154877 pLevel->iIdxCur = pParse->nTab++;
154878 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
154880 VdbeComment((v, "for %s", pTable->zName));
154881 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){
154882 pLevel->regFilter = ++pParse->nMem;
154883 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
154887 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
154888 if( pTabItem->fg.viaCoroutine ){
154889 int regYield = pTabItem->regReturn;
154891 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
154894 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
154896 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
154901 pLoop->wsFlags |= WHERE_PARTIALIDX;
154905 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
154907 if( pLevel->regFilter ){
154908 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
154909 regBase, pLoop->u.btree.nEq);
154911 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
154914 if( pTabItem->fg.viaCoroutine ){
154916 testcase( pParse->db->mallocFailed );
154917 assert( pLevel->iIdxCur>0 );
154918 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
154919 pTabItem->regResult, pLevel->iIdxCur);
154921 pTabItem->fg.viaCoroutine = 0;
154923 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
154933 sqlite3ExprDelete(pParse->db, pPartial);
154950 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
154959 int iLevel, /* Index in pWInfo->a[] that is pLevel */
154968 Parse *pParse = pWInfo->pParse; /* Parsing context */
154969 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
154970 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
154975 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
154984 iCur = pLevel->iTabCur;
154985 pLevel->regFilter = ++pParse->nMem;
154988 ** to zero-filled blob of at least 80K bits, but maybe more if the
154990 ** measure the size of the table at run-time using OP_Count with
154995 pItem = &pWInfo->pTabList->a[pLevel->iFrom];
154997 pTab = pItem->pTab;
154999 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
155005 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
155008 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
155009 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
155010 Expr *pExpr = pTerm->pExpr;
155011 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
155014 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
155017 if( pLoop->wsFlags & WHERE_IPK ){
155020 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
155023 Index *pIdx = pLoop->u.btree.pIndex;
155024 int n = pLoop->u.btree.nEq;
155028 int iCol = pIdx->aiColumn[jj];
155029 assert( pIdx->pTable==pItem->pTab );
155030 sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
155032 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
155036 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
155039 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
155040 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
155041 while( ++iLevel < pWInfo->nLevel ){
155043 pLevel = &pWInfo->a[iLevel];
155044 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155045 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
155046 pLoop = pLevel->pWLoop;
155048 if( pLoop->prereq & notReady ) continue;
155049 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
155052 /* This is a candidate for bloom-filter pull-down (early evaluation).
155059 }while( iLevel < pWInfo->nLevel );
155079 Parse *pParse = pWInfo->pParse;
155090 ExprList *pOrderBy = pWInfo->pOrderBy;
155093 pTab = pSrc->pTab;
155101 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155102 pTerm->wtFlags &= ~TERM_OK;
155103 if( pTerm->leftCursor != pSrc->iCursor ) continue;
155104 if( pTerm->prereqRight & mUnusable ) continue;
155105 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
155106 testcase( pTerm->eOperator & WO_IN );
155107 testcase( pTerm->eOperator & WO_ISNULL );
155108 testcase( pTerm->eOperator & WO_IS );
155109 testcase( pTerm->eOperator & WO_ALL );
155110 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
155111 if( pTerm->wtFlags & TERM_VNULL ) continue;
155113 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155114 assert( pTerm->u.x.leftColumn>=XN_ROWID );
155115 assert( pTerm->u.x.leftColumn<pTab->nCol );
155117 /* tag-20191211-002: WHERE-clause constraints are not useful to the
155118 ** right-hand table of a LEFT JOIN nor to the either table of a
155119 ** RIGHT JOIN. See tag-20191211-001 for the
155121 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
155122 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
155123 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
155124 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
155125 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) );
155126 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
155127 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
155128 || pTerm->pExpr->w.iJoin != pSrc->iCursor
155134 pTerm->wtFlags |= TERM_OK;
155143 int n = pOrderBy->nExpr;
155145 Expr *pExpr = pOrderBy->a[i].pExpr;
155154 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
155156 /* First case - a direct column references without a COLLATE operator */
155157 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
155158 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
155162 /* 2nd case - a column reference with a COLLATE operator. Only match
155164 if( pExpr->op==TK_COLLATE
155165 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
155166 && pE2->iTable==pSrc->iCursor
155170 assert( pExpr->u.zToken!=0 );
155171 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
155172 pExpr->iColumn = pE2->iColumn;
155173 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
155174 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
155176 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
155184 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
155185 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
155186 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
155194 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
155203 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
155206 pIdxInfo->aConstraint = pIdxCons;
155207 pIdxInfo->aOrderBy = pIdxOrderBy;
155208 pIdxInfo->aConstraintUsage = pUsage;
155209 pHidden->pWC = pWC;
155210 pHidden->pParse = pParse;
155211 pHidden->eDistinct = eDistinct;
155212 pHidden->mIn = 0;
155213 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155215 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
155216 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
155218 op = pTerm->eOperator & WO_ALL;
155220 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
155221 pHidden->mIn |= SMASKBIT32(j);
155226 pIdxCons[j].op = pTerm->eMatchOp;
155243 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
155246 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
155258 pIdxInfo->nConstraint = j;
155260 Expr *pExpr = pOrderBy->a[i].pExpr;
155262 assert( pExpr->op==TK_COLUMN
155263 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
155264 && pExpr->iColumn==pExpr->pLeft->iColumn) );
155265 pIdxOrderBy[j].iColumn = pExpr->iColumn;
155266 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
155269 pIdxInfo->nOrderBy = j;
155284 assert( pHidden->pParse!=0 );
155285 assert( pHidden->pParse->db==db );
155286 for(i=0; i<pIdxInfo->nConstraint; i++){
155287 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
155288 pHidden->aRhs[i] = 0;
155306 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
155310 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
155314 pParse->db->nSchemaLock++;
155315 rc = pVtab->pModule->xBestIndex(pVtab, p);
155316 pParse->db->nSchemaLock--;
155321 sqlite3OomFault(pParse->db);
155322 }else if( !pVtab->zErrMsg ){
155325 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
155328 sqlite3_free(pVtab->zErrMsg);
155329 pVtab->zErrMsg = 0;
155344 ** into the aSample[] array - it is an index into a virtual set of samples
155355 IndexSample *aSample = pIdx->aSample;
155369 assert( pIdx->nSample>0 );
155370 assert( pRec->nField>0 );
155416 nField = MIN(pRec->nField, pIdx->nSample);
155418 iSample = pIdx->nSample * nField;
155430 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
155436 pRec->nField = n;
155439 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
155442 iLower = aSample[iSamp].anLt[n-1];
155444 res = -1;
155447 iCol = n-1;
155456 if( pParse->db->mallocFailed==0 ){
155459 assert( i<pIdx->nSample );
155460 assert( iCol==nField-1 );
155461 pRec->nField = nField;
155463 || pParse->db->mallocFailed
155466 /* Unless i==pIdx->nSample, indicating that pRec is larger than
155469 assert( i<=pIdx->nSample && i>=0 );
155470 pRec->nField = iCol+1;
155471 assert( i==pIdx->nSample
155473 || pParse->db->mallocFailed );
155478 ** If (i>0), then pRec must also be greater than sample (i-1). */
155480 pRec->nField = iCol;
155482 || pParse->db->mallocFailed );
155485 pRec->nField = nField;
155486 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
155487 || pParse->db->mallocFailed );
155495 assert( iCol==nField-1 );
155500 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155503 if( i>=pIdx->nSample ){
155504 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
155512 iGap = iUpper - iLower;
155520 aStat[1] = pIdx->aAvgEq[nField-1];
155523 /* Restore the pRec->nField value before returning. */
155524 pRec->nField = nField;
155543 if( pTerm->truthProb<=0 ){
155544 nRet += pTerm->truthProb;
155545 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
155546 nRet -= 20; assert( 20==sqlite3LogEst(4) );
155558 assert( iCol>=0 && iCol<pIdx->nColumn );
155559 if( !pIdx->zColAff ){
155562 assert( pIdx->zColAff[iCol]!=0 );
155563 return pIdx->zColAff[iCol];
155571 ** range-scan on a skip-scan index. For example:
155576 ** Value pLoop->nOut is currently set to the estimated number of rows
155587 ** N is the total number of samples, the pLoop->nOut value is adjusted
155590 ** nOut = nOut * ( min(U - L, 1) / N )
155611 Index *p = pLoop->u.btree.pIndex;
155612 int nEq = pLoop->u.btree.nEq;
155613 sqlite3 *db = pParse->db;
155614 int nLower = -1;
155615 int nUpper = p->nSample+1;
155624 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
155626 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
155630 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
155631 nUpper = p2 ? 0 : p->nSample;
155637 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
155638 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
155648 nDiff = (nUpper - nLower);
155657 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
155658 pLoop->nOut -= nAdjust;
155660 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
155661 nLower, nUpper, nAdjust*-1, pLoop->nOut));
155691 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
155699 ** left-most column of the index). Or, if the query is:
155724 int nOut = pLoop->nOut;
155728 Index *p = pLoop->u.btree.pIndex;
155729 int nEq = pLoop->u.btree.nEq;
155731 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
155732 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
155734 if( nEq==pBuilder->nRecValid ){
155735 UnpackedRecord *pRec = pBuilder->pRec;
155737 int nBtm = pLoop->u.btree.nBtm;
155738 int nTop = pLoop->u.btree.nTop;
155743 ** key-prefix formed by the nEq values matched against the nEq left-most
155757 ** The number of rows between the two bounds is then just iUpper-iLower.
155761 int iLwrIdx = -2; /* aSample[] for the lower bound */
155762 int iUprIdx = -1; /* aSample[] for the upper bound */
155765 testcase( pRec->nField!=pBuilder->nRecValid );
155766 pRec->nField = pBuilder->nRecValid;
155771 iUpper = p->nRowEst0;
155773 /* Note: this call could be optimized away - since the same values must
155780 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
155781 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
155782 assert( p->aSortOrder!=0 );
155783 if( p->aSortOrder[nEq] ){
155792 Expr *pExpr = pLower->pExpr->pRight;
155799 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
155801 nOut--;
155809 Expr *pExpr = pUpper->pExpr->pRight;
155816 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
155818 nOut--;
155823 pBuilder->pRec = pRec;
155826 nNew = sqlite3LogEst(iUpper - iLower);
155831 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
155852 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
155857 ** has an application-defined likelihood(), assume the range is
155858 ** reduced by an additional 75%. This means that, by default, an open-ended
155862 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
155863 nNew -= 20;
155866 nOut -= (pLower!=0) + (pUpper!=0);
155870 if( pLoop->nOut>nOut ){
155872 pLoop->nOut, nOut));
155875 pLoop->nOut = (LogEst)nOut;
155883 ** the histogram data. This only works when x is the left-most
155890 ** non-zero.
155903 Index *p = pBuilder->pNew->u.btree.pIndex;
155904 int nEq = pBuilder->pNew->u.btree.nEq;
155905 UnpackedRecord *pRec = pBuilder->pRec;
155911 assert( nEq<=p->nColumn );
155912 assert( p->aSample!=0 );
155913 assert( p->nSample>0 );
155914 assert( pBuilder->nRecValid<nEq );
155918 if( pBuilder->nRecValid<(nEq-1) ){
155924 if( nEq>=p->nColumn ){
155929 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
155930 pBuilder->pRec = pRec;
155933 pBuilder->nRecValid = nEq;
155937 p->zName, nEq-1, (int)a[1]));
155947 ** an IN constraint where the right-hand side of the IN operator
155954 ** non-zero.
155967 Index *p = pBuilder->pNew->u.btree.pIndex;
155968 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
155969 int nRecValid = pBuilder->nRecValid;
155975 assert( p->aSample!=0 );
155976 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
155978 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
155980 pBuilder->nRecValid = nRecValid;
155988 assert( pBuilder->nRecValid==nRecValid );
156000 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
156005 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
156006 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
156007 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
156008 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
156009 if( pTerm->eOperator & WO_SINGLE ){
156010 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
156012 pTerm->leftCursor, pTerm->u.x.leftColumn);
156013 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
156015 pTerm->u.pOrInfo->indexable);
156017 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
156020 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
156021 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
156025 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
156026 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
156028 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
156029 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
156031 if( pTerm->iParent>=0 ){
156032 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
156035 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
156046 for(i=0; i<pWC->nTerm; i++){
156047 sqlite3WhereTermPrint(&pWC->a[i], i);
156057 WhereInfo *pWInfo = pWC->pWInfo;
156058 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
156059 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
156060 Table *pTab = pItem->pTab;
156061 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
156062 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
156063 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
156065 pItem->zAlias ? pItem->zAlias : pTab->zName);
156066 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
156068 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
156070 int i = sqlite3Strlen30(zName) - 1;
156071 while( zName[i]!='_' ) i--;
156074 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
156080 if( p->u.vtab.idxStr ){
156082 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
156084 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
156086 sqlite3DebugPrintf(" %-19s", z);
156089 if( p->wsFlags & WHERE_SKIPSCAN ){
156090 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
156092 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
156094 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
156095 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
156097 for(i=0; i<p->nLTerm; i++){
156098 sqlite3WhereTermPrint(p->aLTerm[i], i);
156109 p->aLTerm = p->aLTermSpace;
156110 p->nLTerm = 0;
156111 p->nLSlot = ArraySize(p->aLTermSpace);
156112 p->wsFlags = 0;
156119 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
156120 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
156121 sqlite3_free(p->u.vtab.idxStr);
156122 p->u.vtab.needFree = 0;
156123 p->u.vtab.idxStr = 0;
156124 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
156125 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
156126 sqlite3DbFreeNN(db, p->u.btree.pIndex);
156127 p->u.btree.pIndex = 0;
156136 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156142 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156146 if( p->nLSlot>=n ) return SQLITE_OK;
156148 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
156150 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
156151 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156152 p->aLTerm = paNew;
156153 p->nLSlot = n;
156162 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
156167 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
156168 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
156169 pFrom->u.vtab.needFree = 0;
156170 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
156171 pFrom->u.btree.pIndex = 0;
156189 sqlite3WhereClauseClear(&pWInfo->sWC);
156190 while( pWInfo->pLoops ){
156191 WhereLoop *p = pWInfo->pLoops;
156192 pWInfo->pLoops = p->pNextLoop;
156195 assert( pWInfo->pExprMods==0 );
156196 while( pWInfo->pMemToFree ){
156197 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156198 sqlite3DbFreeNN(db, pWInfo->pMemToFree);
156199 pWInfo->pMemToFree = pNext;
156207 while( pWInfo->pExprMods ){
156208 WhereExprMod *p = pWInfo->pExprMods;
156209 pWInfo->pExprMods = p->pNext;
156210 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
156211 sqlite3DbFree(pWInfo->pParse->db, p);
156229 ** was added because if X uses skip-scan less than Y it still might
156232 ** than a non-covering index even if it is a proper subset.
156239 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
156242 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
156243 if( pY->nSkip > pX->nSkip ) return 0;
156244 for(i=pX->nLTerm-1; i>=0; i--){
156245 if( pX->aLTerm[i]==0 ) continue;
156246 for(j=pY->nLTerm-1; j>=0; j--){
156247 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
156251 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
156252 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
156273 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
156274 for(; p; p=p->pNextLoop){
156275 if( p->iTab!=pTemplate->iTab ) continue;
156276 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
156281 pTemplate->rRun, pTemplate->nOut,
156282 MIN(p->rRun, pTemplate->rRun),
156283 MIN(p->nOut - 1, pTemplate->nOut)));
156284 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
156285 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
156290 pTemplate->rRun, pTemplate->nOut,
156291 MAX(p->rRun, pTemplate->rRun),
156292 MAX(p->nOut + 1, pTemplate->nOut)));
156293 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
156294 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
156318 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
156319 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
156328 assert( p->rSetup==0 || pTemplate->rSetup==0
156329 || p->rSetup==pTemplate->rSetup );
156333 ** rSetup. Call this SETUP-INVARIANT */
156334 assert( p->rSetup>=pTemplate->rSetup );
156336 /* Any loop using an appliation-defined index (or PRIMARY KEY or
156338 ** than an automatic index. Unless it is a skip-scan. */
156339 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
156340 && (pTemplate->nSkip)==0
156341 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
156342 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
156343 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
156353 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
156354 && p->rSetup<=pTemplate->rSetup /* (2a) */
156355 && p->rRun<=pTemplate->rRun /* (2b) */
156356 && p->nOut<=pTemplate->nOut /* (2c) */
156366 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
156367 && p->rRun>=pTemplate->rRun /* (2a) */
156368 && p->nOut>=pTemplate->nOut /* (2b) */
156370 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
156386 ** If pBuilder->pOrSet is not NULL then we care about only the
156388 ** information is gathered in the pBuilder->pOrSet object. This special
156391 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
156403 WhereInfo *pWInfo = pBuilder->pWInfo;
156404 sqlite3 *db = pWInfo->pParse->db;
156408 if( pBuilder->iPlanLimit==0 ){
156410 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
156413 pBuilder->iPlanLimit--;
156415 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
156417 /* If pBuilder->pOrSet is defined, then only keep track of the costs
156420 if( pBuilder->pOrSet!=0 ){
156421 if( pTemplate->nLTerm ){
156423 u16 n = pBuilder->pOrSet->n;
156426 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
156427 pTemplate->nOut);
156430 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
156431 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156440 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
156448 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156464 sqlite3WhereLoopPrint(p, pBuilder->pWC);
156469 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156477 p->pNextLoop = 0;
156482 WhereLoop **ppTail = &p->pNextLoop;
156489 *ppTail = pToDel->pNextLoop;
156493 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
156500 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
156501 Index *pIndex = p->u.btree.pIndex;
156502 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
156503 p->u.btree.pIndex = 0;
156522 ** TODO --> Perhaps this is something that could be improved by better
156526 ** value corresponds to -1 in LogEst notation, so this means decrement
156533 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
156534 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
156544 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
156546 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
156548 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
156549 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
156551 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
156552 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
156553 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
156554 for(j=pLoop->nLTerm-1; j>=0; j--){
156555 pX = pLoop->aLTerm[j];
156558 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
156561 if( pLoop->maskSelf==pTerm->prereqAll ){
156565 ** "self-culling".
156567 ** 2022-03-24: Self-culling only applies if either the extra terms
156568 ** are straight comparison operators that are non-true with NULL
156571 if( (pTerm->eOperator & 0x3f)!=0
156572 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
156575 pLoop->wsFlags |= WHERE_SELFCULL;
156578 if( pTerm->truthProb<=0 ){
156581 pLoop->nOut += pTerm->truthProb;
156585 pLoop->nOut--;
156586 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
156587 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
156589 Expr *pRight = pTerm->pExpr->pRight;
156591 testcase( pTerm->pExpr->op==TK_IS );
156592 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
156598 pTerm->wtFlags |= TERM_HEURTRUTH;
156605 if( pLoop->nOut > nRow-iReduce ){
156606 pLoop->nOut = nRow - iReduce;
156634 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
156637 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
156646 assert( ExprUseXList(pTerm->pExpr->pLeft) );
156647 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
156648 pRhs = pTerm->pExpr->pRight;
156650 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
156652 pRhs = pRhs->x.pList->a[i].pExpr;
156659 if( pLhs->op!=TK_COLUMN
156660 || pLhs->iTable!=iCur
156661 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
156662 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
156667 testcase( pLhs->iColumn==XN_ROWID );
156669 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
156674 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
156681 ** compiled with -DSQLITE_ENABLE_COSTMULT
156690 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
156693 ** When this function is called, pBuilder->pNew->nOut contains the
156698 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
156707 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
156708 Parse *pParse = pWInfo->pParse; /* Parsing context */
156709 sqlite3 *db = pParse->db; /* Database connection malloc context */
156714 Bitmask saved_prereq; /* Original value of pNew->prereq */
156715 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
156716 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
156717 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
156718 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
156719 u16 saved_nSkip; /* Original value of pNew->nSkip */
156720 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
156721 LogEst saved_nOut; /* Original value of pNew->nOut */
156727 pNew = pBuilder->pNew;
156728 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
156730 pProbe->pTable->zName,pProbe->zName,
156731 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
156733 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
156734 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
156735 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
156738 assert( pNew->u.btree.nBtm==0 );
156741 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
156743 assert( pNew->u.btree.nEq<pProbe->nColumn );
156744 assert( pNew->u.btree.nEq<pProbe->nKeyCol
156745 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
156747 saved_nEq = pNew->u.btree.nEq;
156748 saved_nBtm = pNew->u.btree.nBtm;
156749 saved_nTop = pNew->u.btree.nTop;
156750 saved_nSkip = pNew->nSkip;
156751 saved_nLTerm = pNew->nLTerm;
156752 saved_wsFlags = pNew->wsFlags;
156753 saved_prereq = pNew->prereq;
156754 saved_nOut = pNew->nOut;
156755 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
156757 pNew->rSetup = 0;
156758 rSize = pProbe->aiRowLogEst[0];
156761 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
156766 int nRecValid = pBuilder->nRecValid;
156768 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
156773 if( pTerm->prereqRight & pNew->maskSelf ) continue;
156777 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
156779 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
156782 ** See tag-20191211-002 for the vtab equivalent.
156784 ** 2022-06-06: See https://sqlite.org/forum/forumpost/206d99a16dd9212f
156787 ** not-NULL condition on the left table of the RIGHT JOIN.
156789 ** 2022-06-10: The same condition applies to termCanDriveIndex() above.
156792 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
156793 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
156794 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
156795 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
156796 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
156797 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
156798 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
156799 || pTerm->pExpr->w.iJoin != pSrc->iCursor
156805 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
156806 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
156808 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
156810 pNew->wsFlags = saved_wsFlags;
156811 pNew->u.btree.nEq = saved_nEq;
156812 pNew->u.btree.nBtm = saved_nBtm;
156813 pNew->u.btree.nTop = saved_nTop;
156814 pNew->nLTerm = saved_nLTerm;
156815 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156816 pNew->aLTerm[pNew->nLTerm++] = pTerm;
156817 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
156820 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
156821 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
156822 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
156826 Expr *pExpr = pTerm->pExpr;
156837 for(i=0; i<pNew->nLTerm-1; i++){
156838 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
156840 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
156842 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
156844 if( pProbe->hasStat1 && rLogSize>=10 ){
156851 ** the left-most index column, M==N.
156861 ** with the index, as using an index has better worst-case behavior.
156866 M = pProbe->aiRowLogEst[saved_nEq];
156868 /* TUNING v----- 10 to bias toward indexed IN */
156869 x = M + logK + 10 - (nIn + rLogSize);
156878 " nInMul=%d) prefers skip-scan\n",
156880 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
156889 pNew->wsFlags |= WHERE_COLUMN_IN;
156891 int iCol = pProbe->aiColumn[saved_nEq];
156892 pNew->wsFlags |= WHERE_COLUMN_EQ;
156893 assert( saved_nEq==pNew->u.btree.nEq );
156895 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
156897 if( iCol==XN_ROWID || pProbe->uniqNotNull
156898 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
156900 pNew->wsFlags |= WHERE_ONEROW;
156902 pNew->wsFlags |= WHERE_UNQ_WANTED;
156905 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
156907 pNew->wsFlags |= WHERE_COLUMN_NULL;
156911 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
156912 pNew->u.btree.nBtm = whereRangeVectorLen(
156913 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156917 if( pTerm->wtFlags & TERM_LIKEOPT ){
156921 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
156922 assert( pTop->wtFlags & TERM_LIKEOPT );
156923 assert( pTop->eOperator==WO_LT );
156924 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156925 pNew->aLTerm[pNew->nLTerm++] = pTop;
156926 pNew->wsFlags |= WHERE_TOP_LIMIT;
156927 pNew->u.btree.nTop = 1;
156933 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
156934 pNew->u.btree.nTop = whereRangeVectorLen(
156935 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156938 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
156939 pNew->aLTerm[pNew->nLTerm-2] : 0;
156942 /* At this point pNew->nOut is set to the number of rows expected to
156946 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
156947 assert( pNew->nOut==saved_nOut );
156948 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
156953 int nEq = ++pNew->u.btree.nEq;
156956 assert( pNew->nOut==saved_nOut );
156957 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
156960 pNew->nOut += pTerm->truthProb;
156961 pNew->nOut -= nIn;
156966 && pProbe->nSample
156967 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
156968 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
156971 Expr *pExpr = pTerm->pExpr;
156976 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
156978 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
156983 pNew->nOut = sqlite3LogEst(nOut);
156987 ** See tag-202002240-1 */
156988 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
156997 pTerm->wtFlags |= TERM_HIGHTRUTH;
156998 if( pTerm->wtFlags & TERM_HEURTRUTH ){
157002 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
157005 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
157006 pNew->nOut -= nIn;
157012 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
157017 pNew->nOut += 10;
157024 ** it to pNew->rRun, which is currently set to the cost of the index
157025 ** seek only. Then, if this is a non-covering index, add the cost of
157027 assert( pSrc->pTab->szTabRow>0 );
157028 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
157029 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
157030 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
157031 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
157033 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
157035 nOutUnadjusted = pNew->nOut;
157036 pNew->rRun += nInMul + nIn;
157037 pNew->nOut += nInMul + nIn;
157038 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
157041 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
157042 pNew->nOut = saved_nOut;
157044 pNew->nOut = nOutUnadjusted;
157047 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
157048 && pNew->u.btree.nEq<pProbe->nColumn
157049 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
157050 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
157054 pNew->nOut = saved_nOut;
157056 pBuilder->nRecValid = nRecValid;
157059 pNew->prereq = saved_prereq;
157060 pNew->u.btree.nEq = saved_nEq;
157061 pNew->u.btree.nBtm = saved_nBtm;
157062 pNew->u.btree.nTop = saved_nTop;
157063 pNew->nSkip = saved_nSkip;
157064 pNew->wsFlags = saved_wsFlags;
157065 pNew->nOut = saved_nOut;
157066 pNew->nLTerm = saved_nLTerm;
157068 /* Consider using a skip-scan if there are no WHERE clause constraints
157069 ** available for the left-most terms of the index, and if the average
157070 ** number of repeats in the left-most terms is at least 18.
157080 && saved_nEq+1<pProbe->nKeyCol
157081 && saved_nEq==pNew->nLTerm
157082 && pProbe->noSkipScan==0
157083 && pProbe->hasStat1!=0
157085 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
157086 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
157089 pNew->u.btree.nEq++;
157090 pNew->nSkip++;
157091 pNew->aLTerm[pNew->nLTerm++] = 0;
157092 pNew->wsFlags |= WHERE_SKIPSCAN;
157093 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
157094 pNew->nOut -= nIter;
157095 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
157096 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
157099 pNew->nOut = saved_nOut;
157100 pNew->u.btree.nEq = saved_nEq;
157101 pNew->nSkip = saved_nSkip;
157102 pNew->wsFlags = saved_wsFlags;
157106 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
157127 if( pIndex->bUnordered ) return 0;
157128 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
157129 for(ii=0; ii<pOB->nExpr; ii++){
157130 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
157132 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
157133 if( pExpr->iColumn<0 ) return 1;
157134 for(jj=0; jj<pIndex->nKeyCol; jj++){
157135 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
157137 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
157138 for(jj=0; jj<pIndex->nKeyCol; jj++){
157139 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
157140 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
157163 pParse = pWC->pWInfo->pParse;
157164 while( pWhere->op==TK_AND ){
157165 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
157166 pWhere = pWhere->pRight;
157168 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
157169 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157171 pExpr = pTerm->pExpr;
157172 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
157175 && (pTerm->wtFlags & TERM_VNULL)==0
157185 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157186 ** a b-tree table, not a virtual table.
157188 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
157193 ** cost = nRow * 3.0 // full-table scan
157195 ** cost = nRow * (K+3.0) // scan of non-covering index
157202 ** the index b-tree:
157205 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
157209 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
157214 ** log(nRow) factor is omitted from a non-covering index scan in order to
157215 ** bias the scoring in favor of using an index, since the worst-case
157216 ** performance of using an index is far better than the worst-case performance
157227 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
157238 pNew = pBuilder->pNew;
157239 pWInfo = pBuilder->pWInfo;
157240 pTabList = pWInfo->pTabList;
157241 pSrc = pTabList->a + pNew->iTab;
157242 pTab = pSrc->pTab;
157243 pWC = pBuilder->pWC;
157244 assert( !IsVirtual(pSrc->pTab) );
157246 if( pSrc->fg.isIndexedBy ){
157247 assert( pSrc->fg.isCte==0 );
157249 pProbe = pSrc->u2.pIBIndex;
157251 pProbe = pTab->pIndex;
157265 sPk.szIdxRow = pTab->szTabRow;
157267 aiRowEstPk[0] = pTab->nRowLogEst;
157269 pFirst = pSrc->pTab->pIndex;
157270 if( pSrc->fg.notIndexed==0 ){
157277 rSize = pTab->nRowLogEst;
157281 if( !pBuilder->pOrSet /* Not part of an OR optimization */
157282 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
157283 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
157284 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
157285 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
157287 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
157288 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
157289 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
157291 /* Generate auto-index WhereLoops */
157294 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
157296 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
157297 if( pTerm->prereqRight & pNew->maskSelf ) continue;
157299 pNew->u.btree.nEq = 1;
157300 pNew->nSkip = 0;
157301 pNew->u.btree.pIndex = 0;
157302 pNew->nLTerm = 1;
157303 pNew->aLTerm[0] = pTerm;
157304 /* TUNING: One-time cost for computing the automatic index is
157307 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
157312 pNew->rSetup = rLogSize + rSize;
157313 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
157314 pNew->rSetup += 28;
157316 pNew->rSetup -= 10;
157318 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
157319 if( pNew->rSetup<0 ) pNew->rSetup = 0;
157324 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
157325 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
157326 pNew->wsFlags = WHERE_AUTO_INDEX;
157327 pNew->prereq = mPrereq | pTerm->prereqRight;
157337 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
157339 if( pProbe->pPartIdxWhere!=0
157340 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
157341 pProbe->pPartIdxWhere)
157343 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
157346 if( pProbe->bNoQuery ) continue;
157347 rSize = pProbe->aiRowLogEst[0];
157348 pNew->u.btree.nEq = 0;
157349 pNew->u.btree.nBtm = 0;
157350 pNew->u.btree.nTop = 0;
157351 pNew->nSkip = 0;
157352 pNew->nLTerm = 0;
157353 pNew->iSortIdx = 0;
157354 pNew->rSetup = 0;
157355 pNew->prereq = mPrereq;
157356 pNew->nOut = rSize;
157357 pNew->u.btree.pIndex = pProbe;
157358 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
157361 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
157362 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
157364 pNew->wsFlags = WHERE_IPK;
157367 pNew->iSortIdx = b ? iSortIdx : 0;
157370 ** since index lookups have better worst-case performance if our
157381 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
157383 pNew->rRun = rSize + 16;
157385 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157388 pNew->nOut = rSize;
157392 if( pProbe->isCovering ){
157393 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
157396 m = pSrc->colUsed & pProbe->colNotIdxed;
157397 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
157403 || pProbe->pPartIdxWhere!=0
157404 || pSrc->fg.isIndexedBy
157406 && pProbe->bUnordered==0
157407 && (pProbe->szIdxRow<pTab->szTabRow)
157408 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
157410 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
157413 pNew->iSortIdx = b ? iSortIdx : 0;
157418 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
157420 /* If this is a non-covering index scan, add in the cost of
157427 int iCur = pSrc->iCursor;
157428 WhereClause *pWC2 = &pWInfo->sWC;
157429 for(ii=0; ii<pWC2->nTerm; ii++){
157430 WhereTerm *pTerm = &pWC2->a[ii];
157431 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
157436 if( pTerm->truthProb<=0 ){
157437 nLookup += pTerm->truthProb;
157439 nLookup--;
157440 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
157444 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
157446 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157448 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
157449 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
157451 ** positioned to the correct row during the right-join no-match
157456 pNew->nOut = rSize;
157461 pBuilder->bldFlags1 = 0;
157463 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
157464 /* If a non-unique index is used, or if a prefix of the key for
157465 ** unique index is used (making the index functionally non-unique)
157468 pTab->tabFlags |= TF_StatsUsed;
157471 sqlite3Stat4ProbeFree(pBuilder->pRec);
157472 pBuilder->nRecValid = 0;
157473 pBuilder->pRec = 0;
157485 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
157486 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
157487 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
157492 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
157520 WhereClause *pWC = pBuilder->pWC;
157523 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
157527 WhereLoop *pNew = pBuilder->pNew;
157528 Parse *pParse = pBuilder->pWInfo->pParse;
157529 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
157530 int nConstraint = pIdxInfo->nConstraint;
157534 pNew->prereq = mPrereq;
157538 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157540 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
157541 pIdxCons->usable = 0;
157542 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
157543 && (pTerm->eOperator & mExclude)==0
157546 pIdxCons->usable = 1;
157552 assert( pIdxInfo->needToFreeIdxStr==0 );
157553 pIdxInfo->idxStr = 0;
157554 pIdxInfo->idxNum = 0;
157555 pIdxInfo->orderByConsumed = 0;
157556 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
157557 pIdxInfo->estimatedRows = 25;
157558 pIdxInfo->idxFlags = 0;
157559 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
157560 pHidden->mHandleIn = 0;
157563 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
157570 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
157576 mxTerm = -1;
157577 assert( pNew->nLSlot>=nConstraint );
157578 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
157579 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
157580 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157583 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
157585 int j = pIdxCons->iTermOffset;
157588 || j>=pWC->nTerm
157589 || pNew->aLTerm[iTerm]!=0
157590 || pIdxCons->usable==0
157592 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157593 testcase( pIdxInfo->needToFreeIdxStr );
157596 testcase( iTerm==nConstraint-1 );
157598 testcase( j==pWC->nTerm-1 );
157599 pTerm = &pWC->a[j];
157600 pNew->prereq |= pTerm->prereqRight;
157601 assert( iTerm<pNew->nLSlot );
157602 pNew->aLTerm[iTerm] = pTerm;
157609 pNew->u.vtab.omitMask |= 1<<iTerm;
157613 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
157614 pNew->u.vtab.bOmitOffset = 1;
157617 if( SMASKBIT32(i) & pHidden->mHandleIn ){
157618 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
157619 }else if( (pTerm->eOperator & WO_IN)!=0 ){
157625 pIdxInfo->orderByConsumed = 0;
157626 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
157637 if( pIdxInfo->needToFreeIdxStr ){
157638 sqlite3_free(pIdxInfo->idxStr);
157639 pIdxInfo->idxStr = 0;
157640 pIdxInfo->needToFreeIdxStr = 0;
157648 pNew->nLTerm = mxTerm+1;
157650 if( pNew->aLTerm[i]==0 ){
157651 /* The non-zero argvIdx values must be contiguous. Raise an
157653 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157654 testcase( pIdxInfo->needToFreeIdxStr );
157658 assert( pNew->nLTerm<=pNew->nLSlot );
157659 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
157660 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
157661 pIdxInfo->needToFreeIdxStr = 0;
157662 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
157663 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
157664 pIdxInfo->nOrderBy : 0);
157665 pNew->rSetup = 0;
157666 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
157667 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
157671 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
157672 pNew->wsFlags |= WHERE_ONEROW;
157674 pNew->wsFlags &= ~WHERE_ONEROW;
157677 if( pNew->u.vtab.needFree ){
157678 sqlite3_free(pNew->u.vtab.idxStr);
157679 pNew->u.vtab.needFree = 0;
157683 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
157706 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
157708 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
157709 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
157710 if( pX->pLeft ){
157711 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
157713 zRet = (pC ? pC->zName : sqlite3StrBINARY);
157726 if( m & pHidden->mIn ){
157728 pHidden->mHandleIn &= ~m;
157730 pHidden->mHandleIn |= m;
157741 ** on the right-hand-side of constraint iCons.
157751 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
157752 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
157754 if( pH->aRhs[iCons]==0 ){
157755 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
157757 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
157758 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
157762 pVal = pH->aRhs[iCons];
157766 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
157767 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
157778 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
157779 return pHidden->eDistinct;
157787 ** prepared is read-only, then just start read transactions on all
157791 ** This is used by the (built-in) sqlite_dbpage virtual table.
157795 Parse *pParse = pHidden->pParse;
157796 int nDb = pParse->db->nDb;
157801 if( pParse->writeMask ){
157811 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
157832 ** mUnusable should always be configured as "not-usable" for xBestIndex.
157853 pWInfo = pBuilder->pWInfo;
157854 pParse = pWInfo->pParse;
157855 pWC = pBuilder->pWC;
157856 pNew = pBuilder->pNew;
157857 pSrc = &pWInfo->pTabList->a[pNew->iTab];
157858 assert( IsVirtual(pSrc->pTab) );
157861 pNew->rSetup = 0;
157862 pNew->wsFlags = WHERE_VIRTUALTABLE;
157863 pNew->nLTerm = 0;
157864 pNew->u.vtab.needFree = 0;
157865 nConstraint = p->nConstraint;
157866 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
157867 freeIndexInfo(pParse->db, p);
157872 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
157889 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
157902 mBestNoIn = pNew->prereq & ~mPrereq;
157917 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
157928 if( pNew->prereq==mPrereq ){
157954 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
157955 freeIndexInfo(pParse->db, p);
157956 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
157970 WhereInfo *pWInfo = pBuilder->pWInfo;
157981 pWC = pBuilder->pWC;
157982 pWCEnd = pWC->a + pWC->nTerm;
157983 pNew = pBuilder->pNew;
157985 pItem = pWInfo->pTabList->a + pNew->iTab;
157986 iCur = pItem->iCursor;
157988 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
157989 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
157991 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
157992 if( (pTerm->eOperator & WO_OR)!=0
157993 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
157995 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
157996 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
158004 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
158005 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
158006 if( (pOrTerm->eOperator & WO_AND)!=0 ){
158007 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
158008 }else if( pOrTerm->leftCursor==iCur ){
158009 tempWC.pWInfo = pWC->pWInfo;
158021 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
158022 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
158028 if( IsVirtual(pItem->pTab) ){
158061 pNew->nLTerm = 1;
158062 pNew->aLTerm[0] = pTerm;
158063 pNew->wsFlags = WHERE_MULTI_OR;
158064 pNew->rSetup = 0;
158065 pNew->iSortIdx = 0;
158066 memset(&pNew->u, 0, sizeof(pNew->u));
158069 ** of all sub-scans required by the OR-scan. However, due to rounding
158070 ** errors, it may be that the cost of the OR-scan is equal to its
158071 ** most expensive sub-scan. Add the smallest possible penalty
158078 ** the planner may elect to "OR" together a full-table scan and an
158080 pNew->rRun = sSum.a[i].rRun + 1;
158081 pNew->nOut = sSum.a[i].nOut;
158082 pNew->prereq = sSum.a[i].prereq;
158085 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
158095 WhereInfo *pWInfo = pBuilder->pWInfo;
158099 SrcList *pTabList = pWInfo->pTabList;
158101 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
158102 sqlite3 *db = pWInfo->pParse->db;
158110 pNew = pBuilder->pNew;
158112 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
158113 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158115 pNew->iTab = iTab;
158116 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
158117 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
158119 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
158127 ** prevent FROM-clause terms from moving from the right side of
158131 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
158133 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
158138 if( IsVirtual(pItem->pTab) ){
158141 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
158142 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
158151 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
158154 mPrior |= pNew->maskSelf;
158155 if( rc || db->mallocFailed ){
158185 ** pOrderBy terms must be matched in strict left-to-right order.
158192 u16 nLoop, /* Number of entries in pPath->aLoop[] */
158193 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
158199 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
158215 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
158218 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
158222 ** We say the WhereLoop is "one-row" if it generates no more than one
158223 ** row of output. A WhereLoop is one-row if all of the following are true:
158226 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
158227 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
158229 ** We say the WhereLoop is "order-distinct" if the set of columns from
158231 ** row of the WhereLoop. Every one-row WhereLoop is automatically
158232 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
158233 ** is not order-distinct. To be order-distinct is not quite the same as being
158235 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
158236 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
158240 ** automatically order-distinct.
158246 nOrderBy = pOrderBy->nExpr;
158247 testcase( nOrderBy==BMS-1 );
158248 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
158250 obDone = MASKBIT(nOrderBy)-1;
158258 if( iLoop>0 ) ready |= pLoop->maskSelf;
158260 pLoop = pPath->aLoop[iLoop];
158265 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
158266 if( pLoop->u.vtab.isOrdered
158273 pLoop->u.btree.nDistinctCol = 0;
158275 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
158284 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158286 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
158287 if( pOBExpr->iTable!=iCur ) continue;
158288 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
158291 if( pTerm->eOperator==WO_IN ){
158297 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
158298 if( j>=pLoop->nLTerm ) continue;
158300 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
158301 Parse *pParse = pWInfo->pParse;
158302 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
158303 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
158305 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
158308 testcase( pTerm->pExpr->op==TK_IS );
158313 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
158314 if( pLoop->wsFlags & WHERE_IPK ){
158318 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
158321 nKeyCol = pIndex->nKeyCol;
158322 nColumn = pIndex->nColumn;
158323 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
158324 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
158325 || !HasRowid(pIndex->pTable));
158326 /* All relevant terms of the index must also be non-NULL in order
158329 ** made at tag-20210426-1 below */
158331 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
158342 assert( j>=pLoop->u.btree.nEq
158343 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
158345 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
158346 u16 eOp = pLoop->aLTerm[j]->eOperator;
158351 ** the loop need to be marked as not order-distinct because it can
158370 ** j<pLoop->u.btree.nEq constraint above. Any equality other
158373 Expr *pX = pLoop->aLTerm[j]->pExpr;
158374 for(i=j+1; i<pLoop->u.btree.nEq; i++){
158375 if( pLoop->aLTerm[i]->pExpr==pX ){
158376 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
158385 ** (revIdx) for the j-th column of the index.
158388 iColumn = pIndex->aiColumn[j];
158389 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
158390 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
158397 ** WhereLoop is not well-ordered. tag-20210426-1
158401 && j>=pLoop->u.btree.nEq
158402 && pIndex->pTable->aCol[iColumn].notNull==0
158411 /* Find the ORDER BY term that corresponds to the j-th column
158417 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158423 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
158424 if( pOBExpr->iTable!=iCur ) continue;
158425 if( pOBExpr->iColumn!=iColumn ) continue;
158427 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
158433 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
158434 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
158437 pLoop->u.btree.nDistinctCol = j+1;
158447 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
158452 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
158457 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
158458 if( j==pLoop->u.btree.nEq ){
158459 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
158483 } /* end-if not one-row */
158487 orderDistinctMask |= pLoop->maskSelf;
158492 p = pOrderBy->a[i].pExpr;
158493 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
158500 } /* End the loop over all WhereLoops from outer-most down to inner-most */
158503 for(i=nOrderBy-1; i>0; i--){
158504 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
158509 return -1;
158516 ** BY clause - and so any order that groups rows as required satisfies the
158533 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
158534 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
158537 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
158538 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
158539 return pWInfo->sorted;
158547 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
158548 if( pLast ) zName[i++] = pLast->cId;
158570 ** Or, if the order-by clause has X terms but only the last Y
158571 ** terms are out of order, then block-sorting will reduce the
158581 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
158589 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
158590 nRow = pWInfo->iLimit;
158591 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
158594 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
158601 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
158603 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
158634 pParse = pWInfo->pParse;
158635 db = pParse->db;
158636 nLoop = pWInfo->nLevel;
158638 ** For 2-way joins, the 5 best paths are followed.
158641 assert( nLoop<=pWInfo->pTabList->nSrc );
158642 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
158649 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
158652 nOrderBy = pWInfo->pOrderBy->nExpr;
158664 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
158665 pFrom->aLoop = pX;
158670 ** is either zero - meaning it has not yet been initialized - or the
158685 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
158693 ** -1, indicating that the result set may or may not be ordered,
158695 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
158704 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
158708 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
158710 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
158712 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
158713 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
158714 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
158725 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
158726 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
158727 nOut = pFrom->nRow + pWLoop->nOut;
158728 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
158731 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
158734 revMask = pFrom->revLoop;
158749 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
158750 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
158754 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
158758 ** mxChoice best-so-far paths.
158760 ** First look for an existing path among best-so-far paths
158764 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
158765 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
158766 ** of legal values for isOrdered, -1..64.
158769 if( pTo->maskLoop==maskNew
158770 && ((pTo->isOrdered^isOrdered)&0x80)==0
158772 testcase( jj==nTo-1 );
158777 /* None of the existing best-so-far paths match the candidate. */
158782 ** paths currently in the best-so-far buffer. So discard
158786 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
158794 ** needs to be added to the set of best-so-far paths. */
158805 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
158811 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
158817 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
158819 if( pTo->rCost<rCost
158820 || (pTo->rCost==rCost
158821 && (pTo->nRow<nOut
158822 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
158829 "Skip %s cost=%-3d,%3d,%3d order=%c",
158832 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
158833 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158834 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158838 testcase( pTo->rCost==rCost );
158841 testcase( pTo->rCost==rCost+1 );
158847 "Update %s cost=%-3d,%3d,%3d order=%c",
158850 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
158851 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158852 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158857 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
158858 pTo->revLoop = revMask;
158859 pTo->nRow = nOut;
158860 pTo->rCost = rCost;
158861 pTo->rUnsorted = rUnsorted;
158862 pTo->isOrdered = isOrdered;
158863 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
158864 pTo->aLoop[iLoop] = pWLoop;
158870 if( pTo->rCost>mxCost
158871 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
158873 mxCost = pTo->rCost;
158874 mxUnsorted = pTo->rUnsorted;
158884 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
158886 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
158887 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158888 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
158889 if( pTo->isOrdered>0 ){
158890 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
158914 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
158916 assert( pWInfo->nLevel==nLoop );
158919 WhereLevel *pLevel = pWInfo->a + iLoop;
158920 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
158921 pLevel->iFrom = pWLoop->iTab;
158922 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
158924 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
158925 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
158926 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
158930 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
158931 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
158932 if( rc==pWInfo->pResultSet->nExpr ){
158933 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
158936 pWInfo->bOrderedInnerLoop = 0;
158937 if( pWInfo->pOrderBy ){
158938 pWInfo->nOBSat = pFrom->isOrdered;
158939 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
158940 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
158941 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
158944 pWInfo->revMask = pFrom->revLoop;
158945 if( pWInfo->nOBSat<=0 ){
158946 pWInfo->nOBSat = 0;
158948 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
158953 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
158954 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
158957 if( rc==pWInfo->pOrderBy->nExpr ){
158958 pWInfo->bOrderedInnerLoop = 1;
158959 pWInfo->revMask = m;
158964 && pWInfo->nOBSat==1
158965 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
158967 pWInfo->bOrderedInnerLoop = 1;
158970 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
158971 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
158974 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
158975 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
158977 assert( pWInfo->sorted==0 );
158978 if( nOrder==pWInfo->pOrderBy->nExpr ){
158979 pWInfo->sorted = 1;
158980 pWInfo->revMask = revMask;
158986 pWInfo->nRowOut = pFrom->nRow;
158997 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
159000 ** Return non-zero on success, if this query can be handled by this
159001 ** no-frills query planner. Return zero if this query needs the
159002 ** general-purpose query planner.
159016 pWInfo = pBuilder->pWInfo;
159017 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
159018 assert( pWInfo->pTabList->nSrc>=1 );
159019 pItem = pWInfo->pTabList->a;
159020 pTab = pItem->pTab;
159022 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
159023 testcase( pItem->fg.isIndexedBy );
159024 testcase( pItem->fg.notIndexed );
159027 iCur = pItem->iCursor;
159028 pWC = &pWInfo->sWC;
159029 pLoop = pBuilder->pNew;
159030 pLoop->wsFlags = 0;
159031 pLoop->nSkip = 0;
159032 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
159033 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159035 testcase( pTerm->eOperator & WO_IS );
159036 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
159037 pLoop->aLTerm[0] = pTerm;
159038 pLoop->nLTerm = 1;
159039 pLoop->u.btree.nEq = 1;
159041 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
159043 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159045 assert( pLoop->aLTermSpace==pLoop->aLTerm );
159047 || pIdx->pPartIdxWhere!=0
159048 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
159050 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
159051 for(j=0; j<pIdx->nKeyCol; j++){
159053 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159055 testcase( pTerm->eOperator & WO_IS );
159056 pLoop->aLTerm[j] = pTerm;
159058 if( j!=pIdx->nKeyCol ) continue;
159059 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
159060 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
159061 pLoop->wsFlags |= WHERE_IDX_ONLY;
159063 pLoop->nLTerm = j;
159064 pLoop->u.btree.nEq = j;
159065 pLoop->u.btree.pIndex = pIdx;
159067 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
159071 if( pLoop->wsFlags ){
159072 pLoop->nOut = (LogEst)1;
159073 pWInfo->a[0].pWLoop = pLoop;
159074 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
159075 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
159076 pWInfo->a[0].iTabCur = iCur;
159077 pWInfo->nRowOut = 1;
159078 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
159079 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
159080 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159082 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
159084 pLoop->cId = '0';
159100 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
159101 pWalker->eCode = 0;
159108 ** Return true if the expression contains no non-deterministic SQL
159109 ** functions. Do not consider non-deterministic SQL functions that are
159110 ** part of sub-select statements.
159133 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
159134 p->cId = zLabel[i%(sizeof(zLabel)-1)];
159181 assert( pWInfo->nLevel>=2 );
159182 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
159186 assert( pWInfo->pResultSet!=0 );
159187 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
159189 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
159190 if( pWInfo->pOrderBy ){
159191 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
159193 for(i=pWInfo->nLevel-1; i>=1; i--){
159197 pLoop = pWInfo->a[i].pWLoop;
159198 pItem = &pWInfo->pTabList->a[pLoop->iTab];
159199 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
159200 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
159201 && (pLoop->wsFlags & WHERE_ONEROW)==0
159205 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
159206 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
159207 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159208 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159209 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
159210 || pTerm->pExpr->w.iJoin!=pItem->iCursor
159217 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
159218 notReady &= ~pLoop->maskSelf;
159219 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159220 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159221 pTerm->wtFlags |= TERM_CODED;
159224 if( i!=pWInfo->nLevel-1 ){
159225 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
159226 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
159228 pWInfo->nLevel--;
159229 assert( pWInfo->nLevel>0 );
159243 ** (3) Bloom-filter processing is not disabled. (Checked by the
159258 assert( pWInfo->nLevel>=2 );
159259 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
159260 nSearch = pWInfo->a[0].pWLoop->nOut;
159261 for(i=1; i<pWInfo->nLevel; i++){
159262 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
159264 if( (pLoop->wsFlags & reqFlags)==reqFlags
159265 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
159266 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
159268 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
159269 Table *pTab = pItem->pTab;
159270 pTab->tabFlags |= TF_StatsUsed;
159271 if( nSearch > pTab->nRowLogEst
159272 && (pTab->tabFlags & TF_HasStat1)!=0
159274 testcase( pItem->fg.jointype & JT_LEFT );
159275 pLoop->wsFlags |= WHERE_BLOOMFILTER;
159276 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
159278 "-> use Bloom-filter on loop %c because there are ~%.1e "
159280 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
159281 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
159284 nSearch += pLoop->nOut;
159307 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
159311 ** end |-- by sqlite3WhereEnd()
159318 ** scanning through all values on the right-hand side of the IN.
159321 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
159331 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
159341 ** inner loops (or around the "..." if the test occurs within the inner-
159390 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
159394 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
159411 db = pParse->db;
159415 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
159416 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
159421 testcase( pTabList->nSrc==BMS );
159422 if( pTabList->nSrc>BMS ){
159432 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
159437 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
159438 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
159441 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
159443 if( db->mallocFailed ){
159448 pWInfo->pParse = pParse;
159449 pWInfo->pTabList = pTabList;
159450 pWInfo->pOrderBy = pOrderBy;
159451 pWInfo->pWhere = pWhere;
159452 pWInfo->pResultSet = pResultSet;
159453 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159454 pWInfo->nLevel = nTabList;
159455 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159456 pWInfo->wctrlFlags = wctrlFlags;
159457 pWInfo->iLimit = iAuxArg;
159458 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159460 pWInfo->pLimit = pLimit;
159462 memset(&pWInfo->nOBSat, 0,
159463 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
159464 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159465 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
159466 pMaskSet = &pWInfo->sMaskSet;
159467 pMaskSet->n = 0;
159468 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
159470 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
159472 sWLB.pWC = &pWInfo->sWC;
159477 sWLB.pNew->cId = '*';
159483 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
159484 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
159489 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
159493 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159499 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
159502 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
159506 ** Note that bitmasks are created for all pTabList->nSrc tables in
159508 ** equal to pTabList->nSrc but might be shortened to 1 if the
159513 createMask(pMaskSet, pTabList->a[ii].iCursor);
159514 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
159515 }while( (++ii)<pTabList->nSrc );
159519 for(ii=0; ii<pTabList->nSrc; ii++){
159520 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
159529 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159530 sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
159531 if( pParse->nErr ) goto whereBeginError;
159537 ** Do not do this if the expression contains non-deterministic functions
159538 ** that are not within a sub-select. This is not strictly required, but
159541 ** FROM ... WHERE random()>0; -- eval random() once per row
159542 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
159544 for(ii=0; ii<sWLB.pWC->nBase; ii++){
159545 WhereTerm *pT = &sWLB.pWC->a[ii];
159546 if( pT->wtFlags & TERM_VIRTUAL ) continue;
159547 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
159548 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
159549 pT->wtFlags |= TERM_CODED;
159558 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
159559 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
159561 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159564 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
159565 pWInfo->pOrderBy = pResultSet;
159589 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
159609 while( pWInfo->pLoops ){
159610 WhereLoop *p = pWInfo->pLoops;
159611 pWInfo->pLoops = p->pNextLoop;
159621 if( db->mallocFailed ) goto whereBeginError;
159622 if( pWInfo->pOrderBy ){
159623 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
159624 if( db->mallocFailed ) goto whereBeginError;
159627 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
159628 pWInfo->revMask = ALLBITS;
159630 if( pParse->nErr ){
159633 assert( db->mallocFailed==0 );
159636 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
159637 if( pWInfo->nOBSat>0 ){
159638 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
159640 switch( pWInfo->eDistinct ){
159655 for(ii=0; ii<pWInfo->nLevel; ii++){
159656 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
159664 ** This query optimization is factored out into a separate "no-inline"
159667 ** some C-compiler optimizers from in-lining the
159669 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
159672 if( pWInfo->nLevel>=2
159678 nTabList = pWInfo->nLevel;
159685 if( pWInfo->nLevel>=2
159693 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
159698 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
159701 ** to use a one-pass algorithm, determine if this is appropriate.
159703 ** A one-pass approach can be used if the caller has requested one
159707 ** * the caller has indicated that a one-pass approach can be used
159716 ** use a one-pass approach, and this is not set accurately for scans
159719 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
159721 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
159723 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
159726 && !IsVirtual(pTabList->a[0].pTab)
159729 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
159730 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
159734 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
159742 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
159747 pTabItem = &pTabList->a[pLevel->iFrom];
159748 pTab = pTabItem->pTab;
159749 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
159750 pLoop = pLevel->pWLoop;
159751 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
159755 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
159757 int iCur = pTabItem->iCursor;
159763 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
159765 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
159768 if( pWInfo->eOnePass!=ONEPASS_OFF ){
159770 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
159772 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
159773 assert( pTabItem->iCursor==pLevel->iTabCur );
159774 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
159775 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
159776 if( pWInfo->eOnePass==ONEPASS_OFF
159777 && pTab->nCol<BMS
159778 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
159779 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
159784 Bitmask b = pTabItem->colUsed;
159787 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
159788 assert( n<=pTab->nCol );
159791 if( pLoop->u.btree.pIndex!=0 ){
159799 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
159800 (const u8*)&pTabItem->colUsed, P4_INT64);
159803 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
159805 if( pLoop->wsFlags & WHERE_INDEXED ){
159806 Index *pIx = pLoop->u.btree.pIndex;
159810 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
159814 /* This is one term of an OR-optimization using the PRIMARY KEY of a
159816 iIndexCur = pLevel->iTabCur;
159818 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
159819 Index *pJ = pTabItem->pTab->pIndex;
159824 pJ = pJ->pNext;
159827 pWInfo->aiCurOnePass[1] = iIndexCur;
159832 iIndexCur = pParse->nTab++;
159834 pLevel->iIdxCur = iIndexCur;
159836 assert( pIx->pSchema==pTab->pSchema );
159839 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159841 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
159842 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
159843 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
159844 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
159845 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
159846 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
159850 VdbeComment((v, "%s", pIx->zName));
159855 for(ii=0; ii<pIx->nColumn; ii++){
159856 jj = pIx->aiColumn[ii];
159859 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
159869 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
159870 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
159872 WhereRightJoin *pRJ = pLevel->pRJ;
159873 pRJ->iMatch = pParse->nTab++;
159874 pRJ->regBloom = ++pParse->nMem;
159875 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
159876 pRJ->regReturn = ++pParse->nMem;
159877 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
159878 assert( pTab==pTabItem->pTab );
159881 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
159882 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
159884 pInfo->aColl[0] = 0;
159885 pInfo->aSortFlags[0] = 0;
159890 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
159893 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
159897 pWInfo->nOBSat = 0;
159898 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
159901 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
159902 if( db->mallocFailed ) goto whereBeginError;
159912 if( pParse->nErr ) goto whereBeginError;
159913 pLevel = &pWInfo->a[ii];
159914 wsFlags = pLevel->pWLoop->wsFlags;
159915 pSrc = &pTabList->a[pLevel->iFrom];
159916 if( pSrc->fg.isMaterialized ){
159917 if( pSrc->fg.isCorrelated ){
159918 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159921 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159928 constructAutomaticIndex(pParse, &pWInfo->sWC,
159929 &pTabList->a[pLevel->iFrom], notReady, pLevel);
159934 if( db->mallocFailed ) goto whereBeginError;
159939 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
159941 pWInfo->iContinue = pLevel->addrCont;
159948 VdbeModuleComment((v, "Begin WHERE-core"));
159949 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
159955 testcase( pWInfo->pExprMods!=0 );
159957 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
159970 # define OpcodeRewriteTrace(D,K,P) /* no-op */
159978 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
159990 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
159991 if( pOp->p1!=iCur ) continue;
159992 if( pOp->opcode==OP_Close ) return 0;
159993 if( pOp->opcode==OP_OpenRead ) return 1;
159994 if( pOp->opcode==OP_OpenWrite ) return 1;
159995 if( pOp->opcode==OP_OpenDup ) return 1;
159996 if( pOp->opcode==OP_OpenAutoindex ) return 1;
159997 if( pOp->opcode==OP_OpenEphemeral ) return 1;
160008 Parse *pParse = pWInfo->pParse;
160009 Vdbe *v = pParse->pVdbe;
160013 SrcList *pTabList = pWInfo->pTabList;
160014 sqlite3 *db = pParse->db;
160020 VdbeModuleComment((v, "End WHERE-core"));
160021 for(i=pWInfo->nLevel-1; i>=0; i--){
160023 pLevel = &pWInfo->a[i];
160024 if( pLevel->pRJ ){
160027 WhereRightJoin *pRJ = pLevel->pRJ;
160028 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160029 pLevel->addrCont = 0;
160030 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
160031 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
160035 pLoop = pLevel->pWLoop;
160036 if( pLevel->op!=OP_Noop ){
160041 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
160042 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
160043 && (pLoop->wsFlags & WHERE_INDEXED)!=0
160044 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
160045 && (n = pLoop->u.btree.nDistinctCol)>0
160046 && pIdx->aiRowLogEst[n]>=36
160048 int r1 = pParse->nMem+1;
160051 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
160053 pParse->nMem += n+1;
160054 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
160055 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
160058 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
160062 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160063 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
160064 sqlite3VdbeChangeP5(v, pLevel->p5);
160066 VdbeCoverageIf(v, pLevel->op==OP_Next);
160067 VdbeCoverageIf(v, pLevel->op==OP_Prev);
160068 VdbeCoverageIf(v, pLevel->op==OP_VNext);
160069 if( pLevel->regBignull ){
160070 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
160071 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
160077 }else if( pLevel->addrCont ){
160078 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160080 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
160083 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
160084 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
160085 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
160086 || pParse->db->mallocFailed );
160087 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160088 if( pIn->eEndLoopOp!=OP_Noop ){
160089 if( pIn->nPrefix ){
160091 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
160092 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
160093 if( pLevel->iLeftJoin ){
160094 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
160099 ** return the null-row. So, if the cursor is not open yet,
160102 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
160107 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
160109 pIn->iBase, pIn->nPrefix);
160115 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160118 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
160120 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
160121 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
160123 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
160126 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
160127 if( pLevel->pRJ ){
160128 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
160131 if( pLevel->addrSkip ){
160132 sqlite3VdbeGoto(v, pLevel->addrSkip);
160133 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
160134 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
160135 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
160138 if( pLevel->addrLikeRep ){
160139 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
160140 pLevel->addrLikeRep);
160144 if( pLevel->iLeftJoin ){
160145 int ws = pLoop->wsFlags;
160146 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
160149 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
160150 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
160153 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
160156 Index *pIx = pLevel->u.pCoveringIdx;
160157 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
160158 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
160161 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
160163 if( pLevel->op==OP_Return ){
160164 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
160166 sqlite3VdbeGoto(v, pLevel->addrFirst);
160170 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160171 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160174 assert( pWInfo->nLevel<=pTabList->nSrc );
160175 if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160176 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160180 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
160181 Table *pTab = pTabItem->pTab;
160183 pLoop = pLevel->pWLoop;
160189 if( pLevel->pRJ ){
160194 /* For a co-routine, change all OP_Column references to the table of
160195 ** the co-routine into OP_Copy of result contained in a register.
160198 if( pTabItem->fg.viaCoroutine ){
160199 testcase( pParse->db->mallocFailed );
160200 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
160201 pTabItem->regResult, 0);
160216 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
160217 pIdx = pLoop->u.btree.pIndex;
160218 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
160219 pIdx = pLevel->u.pCoveringIdx;
160222 && !db->mallocFailed
160224 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
160227 last = pWInfo->iEndWhere;
160229 k = pLevel->addrBody + 1;
160231 if( db->flags & SQLITE_VdbeAddopTrace ){
160232 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
160235 pOp = sqlite3VdbeGetOp(v, k - 1);
160236 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
160237 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
160238 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
160241 pLastOp = pOp + (last - k);
160244 if( pOp->p1!=pLevel->iTabCur ){
160245 /* no-op */
160246 }else if( pOp->opcode==OP_Column
160248 || pOp->opcode==OP_Offset
160251 int x = pOp->p2;
160252 assert( pIdx->pTable==pTab );
160254 if( pOp->opcode==OP_Offset ){
160260 x = pPk->aiColumn[x];
160268 pOp->p2 = x;
160269 pOp->p1 = pLevel->iIdxCur;
160273 ** reference. Verify that this is harmless - that the
160277 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
160278 || cursorIsOpen(v,pOp->p1,k)
160279 || pOp->opcode==OP_Offset
160282 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
160283 || cursorIsOpen(v,pOp->p1,k)
160287 }else if( pOp->opcode==OP_Rowid ){
160288 pOp->p1 = pLevel->iIdxCur;
160289 pOp->opcode = OP_IdxRowid;
160291 }else if( pOp->opcode==OP_IfNullRow ){
160292 pOp->p1 = pLevel->iIdxCur;
160300 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
160308 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
160312 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160314 pParse->withinRJSubrtn -= nRJ;
160363 ** the sub-query.
160368 ** select-list and ORDER BY expressions in the parent query are
160369 ** selected by the sub-query. For the purposes of the transformation,
160401 ** sub-query, which is always implemented as a co-routine. It then calls
160406 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
160407 ** When the sub-routine is invoked:
160409 ** * The results of all window-functions for the row are stored
160421 ** BUILT-IN WINDOW FUNCTIONS
160423 ** This implementation features the following built-in window functions:
160437 ** These are the same built-in window functions supported by Postgres.
160440 ** be implemented using an API, built-in window functions are much more
160443 ** As such, some built-in window functions use the same API as aggregate
160454 ** As well as some of the built-in window functions, aggregate window
160461 ** Implementation of built-in window function row_number(). Assumes that the
160492 ** Implementation of built-in window function dense_rank(). Assumes that
160504 if( p ) p->nStep = 1;
160512 if( p->nStep ){
160513 p->nValue++;
160514 p->nStep = 0;
160516 sqlite3_result_int64(pCtx, p->nValue);
160521 ** Implementation of built-in window function nth_value(). This
160522 ** implementation is used in "slow mode" only - when the EXCLUDE clause
160553 p->nStep++;
160554 if( iVal==p->nStep ){
160555 p->pValue = sqlite3_value_dup(apArg[0]);
160556 if( !p->pValue ){
160567 pCtx, "second argument to nth_value must be a positive integer", -1
160573 if( p && p->pValue ){
160574 sqlite3_result_value(pCtx, p->pValue);
160575 sqlite3_value_free(p->pValue);
160576 p->pValue = 0;
160589 if( p && p->pValue==0 ){
160590 p->pValue = sqlite3_value_dup(apArg[0]);
160591 if( !p->pValue ){
160601 if( p && p->pValue ){
160602 sqlite3_result_value(pCtx, p->pValue);
160603 sqlite3_value_free(p->pValue);
160604 p->pValue = 0;
160611 ** Implementation of built-in window function rank(). Assumes that
160624 p->nStep++;
160625 if( p->nValue==0 ){
160626 p->nValue = p->nStep;
160636 sqlite3_result_int64(pCtx, p->nValue);
160637 p->nValue = 0;
160642 ** Implementation of built-in window function percent_rank(). Assumes that
160657 p->nTotal++;
160669 p->nStep++;
160675 p->nValue = p->nStep;
160676 if( p->nTotal>1 ){
160677 double r = (double)p->nValue / (double)(p->nTotal-1);
160687 ** Implementation of built-in window function cume_dist(). Assumes that
160702 p->nTotal++;
160714 p->nStep++;
160720 double r = (double)(p->nStep) / (double)(p->nTotal);
160750 if( p->nTotal==0 ){
160751 p->nParam = sqlite3_value_int64(apArg[0]);
160752 if( p->nParam<=0 ){
160754 pCtx, "argument of ntile must be a positive integer", -1
160758 p->nTotal++;
160770 p->iRow++;
160775 if( p && p->nParam>0 ){
160776 int nSize = (p->nTotal / p->nParam);
160778 sqlite3_result_int64(pCtx, p->iRow+1);
160780 i64 nLarge = p->nTotal - p->nParam*nSize;
160782 i64 iRow = p->iRow;
160784 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
160789 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
160816 sqlite3_value_free(p->pVal);
160817 p->pVal = sqlite3_value_dup(apArg[0]);
160818 if( p->pVal==0 ){
160821 p->nVal++;
160835 p->nVal--;
160836 if( p->nVal==0 ){
160837 sqlite3_value_free(p->pVal);
160838 p->pVal = 0;
160845 if( p && p->pVal ){
160846 sqlite3_result_value(pCtx, p->pVal);
160852 if( p && p->pVal ){
160853 sqlite3_result_value(pCtx, p->pVal);
160854 sqlite3_value_free(p->pVal);
160855 p->pVal = 0;
160860 ** Static names for the built-in window function names. These static
160865 ** if( pFuncDef->zName==row_valueName ){ ... }
160880 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
160881 ** for built-in window functions that never call those interfaces.
160898 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
160909 ** no-op routines for their methods */
160927 ** Register those built-in window functions that are not also aggregates.
160952 for(p=pList; p; p=p->pNextWin){
160953 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
160974 ** * If the function is a built-in window function that requires the
160975 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
160984 if( pWin->zName && pWin->eFrmType==0 ){
160985 Window *p = windowFind(pParse, pList, pWin->zName);
160987 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
160988 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
160989 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
160990 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
160991 pWin->eStart = p->eStart;
160992 pWin->eEnd = p->eEnd;
160993 pWin->eFrmType = p->eFrmType;
160994 pWin->eExclude = p->eExclude;
160998 if( (pWin->eFrmType==TK_RANGE)
160999 && (pWin->pStart || pWin->pEnd)
161000 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
161006 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
161007 sqlite3 *db = pParse->db;
161008 if( pWin->pFilter ){
161030 if( pFunc->zName==aUp[i].zFunc ){
161031 sqlite3ExprDelete(db, pWin->pStart);
161032 sqlite3ExprDelete(db, pWin->pEnd);
161033 pWin->pEnd = pWin->pStart = 0;
161034 pWin->eFrmType = aUp[i].eFrmType;
161035 pWin->eStart = aUp[i].eStart;
161036 pWin->eEnd = aUp[i].eEnd;
161037 pWin->eExclude = 0;
161038 if( pWin->eStart==TK_FOLLOWING ){
161039 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
161046 pWin->pWFunc = pFunc;
161059 Select *pSubSelect; /* Current sub-select, if any */
161064 ** this function appends to the output expression-list and updates
161068 struct WindowRewrite *p = pWalker->u.pRewrite;
161069 Parse *pParse = pWalker->pParse;
161071 assert( p->pWin!=0 );
161073 /* If this function is being called from within a scalar sub-select
161077 ** to the scalar sub-select. */
161078 if( p->pSubSelect ){
161079 if( pExpr->op!=TK_COLUMN ){
161082 int nSrc = p->pSrc->nSrc;
161085 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
161091 switch( pExpr->op ){
161098 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
161099 if( pExpr->y.pWin==pWin ){
161100 assert( pWin->pOwner==pExpr );
161109 int iCol = -1;
161110 if( pParse->db->mallocFailed ) return WRC_Abort;
161111 if( p->pSub ){
161113 for(i=0; i<p->pSub->nExpr; i++){
161114 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
161121 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
161122 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
161123 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
161125 if( p->pSub ){
161126 int f = pExpr->flags & EP_Collate;
161129 sqlite3ExprDelete(pParse->db, pExpr);
161133 pExpr->op = TK_COLUMN;
161134 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
161135 pExpr->iTable = p->pWin->iEphCsr;
161136 pExpr->y.pTab = p->pTab;
161137 pExpr->flags = f;
161139 if( pParse->db->mallocFailed ) return WRC_Abort;
161143 default: /* no-op */
161150 struct WindowRewrite *p = pWalker->u.pRewrite;
161151 Select *pSave = p->pSubSelect;
161155 p->pSubSelect = pSelect;
161157 p->pSubSelect = pSave;
161164 ** Iterate through each expression in expression-list pEList. For each:
161171 ** Append the node to output expression-list (*ppSub). And replace it
161172 ** with a TK_COLUMN that reads the (N-1)th element of table
161173 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
161182 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
161207 ** Append a copy of each expression in expression-list pAppend to
161218 int nInit = pList ? pList->nExpr : 0;
161219 for(i=0; i<pAppend->nExpr; i++){
161220 sqlite3 *db = pParse->db;
161221 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
161223 if( db->mallocFailed ){
161232 pSub->op = TK_NULL;
161233 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
161234 pSub->u.zToken = 0;
161238 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
161247 ** then we have to increase the Expr->op2 values of those nodes
161253 if( pExpr->op==TK_AGG_FUNCTION
161254 && pExpr->op2>=pWalker->walkerDepth
161256 pExpr->op2++;
161262 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
161264 sqlite3ErrorMsg(pWalker->pParse,
161265 "misuse of aggregate: %s()", pExpr->u.zToken);
161272 ** any SQL window functions, this function is a no-op. Otherwise, it
161279 if( p->pWin
161280 && p->pPrior==0
161281 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
161285 sqlite3 *db = pParse->db;
161287 SrcList *pSrc = p->pSrc;
161288 Expr *pWhere = p->pWhere;
161289 ExprList *pGroupBy = p->pGroupBy;
161290 Expr *pHaving = p->pHaving;
161293 ExprList *pSublist = 0; /* Expression list for sub-query */
161294 Window *pMWin = p->pWin; /* Main window object */
161299 u32 selFlags = p->selFlags;
161307 if( (p->selFlags & SF_Aggregate)==0 ){
161310 sqlite3WalkExprList(&w, p->pOrderBy);
161313 p->pSrc = 0;
161314 p->pWhere = 0;
161315 p->pGroupBy = 0;
161316 p->pHaving = 0;
161317 p->selFlags &= ~SF_Aggregate;
161318 p->selFlags |= SF_WinRewrite;
161320 /* Create the ORDER BY clause for the sub-select. This is the concatenation
161323 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
161324 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
161325 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
161326 int nSave = pSort->nExpr;
161327 pSort->nExpr = p->pOrderBy->nExpr;
161328 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
161329 sqlite3ExprListDelete(db, p->pOrderBy);
161330 p->pOrderBy = 0;
161332 pSort->nExpr = nSave;
161338 pMWin->iEphCsr = pParse->nTab++;
161339 pParse->nTab += 3;
161341 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
161342 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
161343 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
161346 ** sub-select expression list. They are required to figure out where
161348 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
161349 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
161352 ** sub-select expression list. Also allocate two registers for each
161353 ** window function - one for the accumulator, another for interim
161355 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161357 assert( ExprUseXList(pWin->pOwner) );
161358 assert( pWin->pWFunc!=0 );
161359 pArgs = pWin->pOwner->x.pList;
161360 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
161362 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161363 pWin->bExprArgs = 1;
161365 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161368 if( pWin->pFilter ){
161369 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
161372 pWin->regAccum = ++pParse->nMem;
161373 pWin->regResult = ++pParse->nMem;
161374 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
161393 ("New window-function subquery in FROM clause of (%u/%p)\n",
161394 p->selId, p));
161395 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
161396 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
161399 if( p->pSrc ){
161401 p->pSrc->a[0].pSelect = pSub;
161402 sqlite3SrcListAssignCursors(pParse, p->pSrc);
161403 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
161405 pSub->selFlags |= (selFlags & SF_Aggregate);
161408 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
161413 pTab->tabFlags |= TF_Ephemeral;
161414 p->pSrc->a[0].pTab = pTab;
161425 if( db->mallocFailed ) rc = SQLITE_NOMEM;
161429 ** result-set or ORDER BY clause of the SELECT statement p. */
161433 assert( rc==SQLITE_OK || pParse->nErr!=0 );
161442 if( p->ppThis ){
161443 *p->ppThis = p->pNextWin;
161444 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
161445 p->ppThis = 0;
161455 sqlite3ExprDelete(db, p->pFilter);
161456 sqlite3ExprListDelete(db, p->pPartition);
161457 sqlite3ExprListDelete(db, p->pOrderBy);
161458 sqlite3ExprDelete(db, p->pEnd);
161459 sqlite3ExprDelete(db, p->pStart);
161460 sqlite3DbFree(db, p->zName);
161461 sqlite3DbFree(db, p->zBase);
161471 Window *pNext = p->pNextWin;
161479 ** value should be a non-negative integer. If the value is not a
161480 ** constant, change it to NULL. The fact that it is then a non-negative
161487 sqlite3ExprDelete(pParse->db, pExpr);
161488 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
161543 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
161545 pWin->eFrmType = eType;
161546 pWin->eStart = eStart;
161547 pWin->eEnd = eEnd;
161548 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
161551 pWin->eExclude = eExclude;
161552 pWin->bImplicitFrame = bImplicitFrame;
161553 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
161554 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
161558 sqlite3ExprDelete(pParse->db, pEnd);
161559 sqlite3ExprDelete(pParse->db, pStart);
161565 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
161566 ** equivalent nul-terminated string.
161576 pWin->pPartition = pPartition;
161577 pWin->pOrderBy = pOrderBy;
161579 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
161582 sqlite3ExprListDelete(pParse->db, pPartition);
161583 sqlite3ExprListDelete(pParse->db, pOrderBy);
161591 ** stored in the linked list starting at pWin->pNextWin. This function
161596 if( pWin->zBase ){
161597 sqlite3 *db = pParse->db;
161598 Window *pExist = windowFind(pParse, pList, pWin->zBase);
161602 if( pWin->pPartition ){
161604 }else if( pExist->pOrderBy && pWin->pOrderBy ){
161606 }else if( pExist->bImplicitFrame==0 ){
161611 "cannot override %s of window: %s", zErr, pWin->zBase
161614 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
161615 if( pExist->pOrderBy ){
161616 assert( pWin->pOrderBy==0 );
161617 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
161619 sqlite3DbFree(db, pWin->zBase);
161620 pWin->zBase = 0;
161631 assert( p->op==TK_FUNCTION );
161633 p->y.pWin = pWin;
161635 pWin->pOwner = p;
161636 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
161642 sqlite3WindowDelete(pParse->db, pWin);
161647 ** Possibly link window pWin into the list at pSel->pWin (window functions
161654 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
161655 pWin->pNextWin = pSel->pWin;
161656 if( pSel->pWin ){
161657 pSel->pWin->ppThis = &pWin->pNextWin;
161659 pSel->pWin = pWin;
161660 pWin->ppThis = &pSel->pWin;
161662 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
161663 pSel->selFlags |= SF_MultiPart;
161682 if( p1->eFrmType!=p2->eFrmType ) return 1;
161683 if( p1->eStart!=p2->eStart ) return 1;
161684 if( p1->eEnd!=p2->eEnd ) return 1;
161685 if( p1->eExclude!=p2->eExclude ) return 1;
161686 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
161687 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
161688 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
161691 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
161695 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
161705 ** to begin iterating through the sub-query results. It is used to allocate
161709 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
161710 Window *pMWin = pSelect->pWin;
161714 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
161715 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
161716 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
161717 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
161721 if( pMWin->pPartition ){
161722 int nExpr = pMWin->pPartition->nExpr;
161723 pMWin->regPart = pParse->nMem+1;
161724 pParse->nMem += nExpr;
161725 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
161728 pMWin->regOne = ++pParse->nMem;
161729 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
161731 if( pMWin->eExclude ){
161732 pMWin->regStartRowid = ++pParse->nMem;
161733 pMWin->regEndRowid = ++pParse->nMem;
161734 pMWin->csrApp = pParse->nTab++;
161735 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
161736 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
161737 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
161741 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161742 FuncDef *p = pWin->pWFunc;
161743 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
161753 assert( ExprUseXList(pWin->pOwner) );
161754 pList = pWin->pOwner->x.pList;
161756 pWin->csrApp = pParse->nTab++;
161757 pWin->regApp = pParse->nMem+1;
161758 pParse->nMem += 3;
161759 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
161760 assert( pKeyInfo->aSortFlags[0]==0 );
161761 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
161763 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
161765 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
161767 else if( p->zName==nth_valueName || p->zName==first_valueName ){
161768 /* Allocate two registers at pWin->regApp. These will be used to
161770 pWin->regApp = pParse->nMem+1;
161771 pWin->csrApp = pParse->nTab++;
161772 pParse->nMem += 2;
161773 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
161775 else if( p->zName==leadName || p->zName==lagName ){
161776 pWin->csrApp = pParse->nTab++;
161777 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
161792 ** code to check that the value is a non-negative integer and throws an
161797 "frame starting offset must be a non-negative integer",
161798 "frame ending offset must be a non-negative integer",
161800 "frame starting offset must be a non-negative number",
161801 "frame ending offset must be a non-negative number",
161839 ** Return the number of arguments passed to the window-function associated
161844 assert( ExprUseXList(pWin->pOwner) );
161845 pList = pWin->pOwner->x.pList;
161846 return (pList ? pList->nExpr : 0);
161869 ** in an array of accumulator registers - one for each window function
161876 ** order to reduce memory requirements - it would always be safe just
161891 ** Consider a window-frame similar to the following:
161911 ** Depending on the window-frame in question, all three cursors may not
161939 Window *pMWin = p->pMWin;
161940 ExprList *pOrderBy = pMWin->pOrderBy;
161942 Vdbe *v = sqlite3GetVdbe(p->pParse);
161943 ExprList *pPart = pMWin->pPartition;
161944 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
161946 for(i=0; i<pOrderBy->nExpr; i++){
161954 ** xInverse (if bInverse is non-zero) for each window function in the
161955 ** linked list starting at pMWin. Or, for built-in window functions
161966 ** already populated with all columns from the current row of the sub-query.
161968 ** If argument regPartSize is non-zero, then it is a register containing the
161978 Parse *pParse = p->pParse;
161981 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161982 FuncDef *pFunc = pWin->pWFunc;
161984 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
161987 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
161994 if( i!=1 || pFunc->zName!=nth_valueName ){
161995 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
161997 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
162002 if( pMWin->regStartRowid==0
162003 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
162004 && (pWin->eStart!=TK_UNBOUNDED)
162009 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
162010 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
162011 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
162012 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
162014 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
162016 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
162017 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162020 }else if( pWin->regApp ){
162021 assert( pFunc->zName==nth_valueName
162022 || pFunc->zName==first_valueName
162025 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
162026 }else if( pFunc->xSFunc!=noopStepFunc ){
162028 if( pWin->pFilter ){
162030 assert( ExprUseXList(pWin->pOwner) );
162031 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
162032 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
162034 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
162040 if( pWin->bExprArgs ){
162044 assert( ExprUseXList(pWin->pOwner) );
162045 nArg = pWin->pOwner->x.pList->nExpr;
162047 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
162051 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
162052 pOp->p1 = csr;
162056 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
162059 assert( ExprUseXList(pWin->pOwner) );
162060 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
162064 bInverse, regArg, pWin->regAccum);
162067 if( pWin->bExprArgs ){
162085 ** pMWin. Or, for built-in window-functions that do not use the standard
162089 Parse *pParse = p->pParse;
162090 Window *pMWin = p->pMWin;
162094 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162095 if( pMWin->regStartRowid==0
162096 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
162097 && (pWin->eStart!=TK_UNBOUNDED)
162099 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162100 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
162102 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
162103 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162104 }else if( pWin->regApp ){
162105 assert( pMWin->regStartRowid==0 );
162109 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
162110 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
162111 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
162112 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162114 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
162115 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
162123 ** p->pMWin list by doing a full scan of the current window frame. Store the
162129 Parse *pParse = p->pParse;
162130 Window *pMWin = p->pMWin;
162131 Vdbe *v = p->pVdbe;
162147 csr = pMWin->csrApp;
162148 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162160 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
162161 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
162163 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162164 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162167 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
162171 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
162174 if( pMWin->eExclude==TK_CURRENT ){
162177 }else if( pMWin->eExclude!=TK_NO ){
162182 if( pMWin->pOrderBy ){
162183 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
162185 if( pMWin->eExclude==TK_TIES ){
162202 windowAggStep(p, pMWin, csr, 0, p->regArg);
162207 sqlite3VdbeJumpHere(v, addrNext-1);
162221 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
162225 ** for per-row processing is only generated for the following built-in window
162234 Window *pMWin = p->pMWin;
162235 Vdbe *v = p->pVdbe;
162237 if( pMWin->regStartRowid ){
162240 Parse *pParse = p->pParse;
162243 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162244 FuncDef *pFunc = pWin->pWFunc;
162245 assert( ExprUseXList(pWin->pOwner) );
162246 if( pFunc->zName==nth_valueName
162247 || pFunc->zName==first_valueName
162249 int csr = pWin->csrApp;
162252 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162254 if( pFunc->zName==nth_valueName ){
162255 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
162260 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
162261 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
162265 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
162269 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
162270 int nArg = pWin->pOwner->x.pList->nExpr;
162271 int csr = pWin->csrApp;
162274 int iEph = pMWin->iEphCsr;
162277 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162279 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
162283 int val = (pFunc->zName==leadName ? 1 : -1);
162286 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
162288 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
162295 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
162301 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
162307 ** any equivalent initialization required by any built-in window functions
162315 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162316 FuncDef *pFunc = pWin->pWFunc;
162317 assert( pWin->regAccum );
162318 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162320 if( pMWin->regStartRowid==0 ){
162321 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
162322 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
162323 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
162326 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
162327 assert( pWin->eStart!=TK_UNBOUNDED );
162328 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
162329 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
162333 regArg = pParse->nMem+1;
162334 pParse->nMem += nArg;
162344 if( pMWin->regStartRowid ) return 1;
162345 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162346 FuncDef *pFunc = pWin->pWFunc;
162347 if( (pFunc->zName==nth_valueName)
162348 || (pFunc->zName==first_valueName)
162349 || (pFunc->zName==leadName)
162350 || (pFunc->zName==lagName)
162360 ** pOrderBy->nExpr. This function generates code to compare the two
162377 int nVal = pOrderBy->nExpr;
162385 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
162400 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
162402 ** If the sort-order for the ORDER BY term in the window is DESC, then the
162404 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
162408 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
162418 int regVal, /* Register containing non-negative number */
162422 Parse *pParse = p->pParse;
162424 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
162427 int regString = ++pParse->nMem; /* Reg. for constant value '' */
162433 /* Read the peer-value from each cursor into a register */
162438 assert( pOrderBy && pOrderBy->nExpr==1 );
162439 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
162449 reg1, (arith==OP_Add ? "+" : "-"), regVal,
162472 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
162487 default: assert( op==OP_Lt ); /* no-op */ break;
162495 sqlite3VdbeChangeP2(v, -1, addrDone);
162499 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162502 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
162505 ** reg1 = reg1 +/- regVal
162508 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
162525 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
162554 Parse *pParse = p->pParse;
162555 Window *pMWin = p->pMWin;
162557 Vdbe *v = p->pVdbe;
162559 int bPeer = (pMWin->eFrmType!=TK_ROWS);
162564 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
162566 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
162572 if( pMWin->eFrmType==TK_RANGE ){
162576 if( pMWin->eStart==TK_FOLLOWING ){
162578 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
162582 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
162587 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
162596 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
162607 if( pMWin->eStart==pMWin->eEnd && regCountdown
162608 && pMWin->eFrmType==TK_RANGE
162613 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
162614 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
162617 }else if( p->regRowid ){
162618 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
162619 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
162624 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
162629 csr = p->current.csr;
162630 reg = p->current.reg;
162635 csr = p->start.csr;
162636 reg = p->start.reg;
162637 if( pMWin->regStartRowid ){
162638 assert( pMWin->regEndRowid );
162639 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
162641 windowAggStep(p, pMWin, csr, 1, p->regArg);
162647 csr = p->end.csr;
162648 reg = p->end.reg;
162649 if( pMWin->regStartRowid ){
162650 assert( pMWin->regEndRowid );
162651 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
162653 windowAggStep(p, pMWin, csr, 0, p->regArg);
162658 if( op==p->eDelete ){
162676 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162679 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
162701 pNew->zName = sqlite3DbStrDup(db, p->zName);
162702 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
162703 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
162704 pNew->pWFunc = p->pWFunc;
162705 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
162706 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
162707 pNew->eFrmType = p->eFrmType;
162708 pNew->eEnd = p->eEnd;
162709 pNew->eStart = p->eStart;
162710 pNew->eExclude = p->eExclude;
162711 pNew->regResult = p->regResult;
162712 pNew->regAccum = p->regAccum;
162713 pNew->iArgCol = p->iArgCol;
162714 pNew->iEphCsr = p->iEphCsr;
162715 pNew->bExprArgs = p->bExprArgs;
162716 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
162717 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
162718 pNew->pOwner = pOwner;
162719 pNew->bImplicitFrame = p->bImplicitFrame;
162734 for(pWin=p; pWin; pWin=pWin->pNextWin){
162737 pp = &((*pp)->pNextWin);
162753 sqlite3 *db = pParse->db;
162755 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
162767 ** and invoke the sub-routine at instruction addrGosub once for each row.
162797 ** if( (regEnd--)<=0 ){
162799 ** if( (regStart--)<=0 ){
162810 ** if( (regStart--)<=0 ){
162816 ** The pseudo-code above uses the following shorthand:
162831 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
162850 ** if( (regEnd--)<=0 ){
162854 ** if( (regStart--)<=0 ){
162860 ** if( (regEnd--)<=0 ){
162876 ** regStart = regEnd - <expr1>
162879 ** if( (regEnd--)<=0 ){
162882 ** if( (regStart--)<=0 ){
162890 ** if( (regEnd--)<=0 ){
162894 ** if( (regStart--)<=0 ){
162906 ** This is optimized of course - branches that will never be taken and
162927 ** if( (regStart--)<=0 ){
162943 ** To handle this case, the pseudo-code programs depicted above are modified
162963 ** of the outer loop - the one started by sqlite3WhereBegin().
163105 Window *pMWin = p->pWin;
163106 ExprList *pOrderBy = pMWin->pOrderBy;
163109 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
163110 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
163121 WindowCodeArg s; /* Context object for sub-routines */
163122 int lblWhereEnd; /* Label just before sqlite3WhereEnd() code */
163126 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
163127 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
163129 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
163130 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
163132 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
163133 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
163134 || pMWin->eExclude==TK_NO
163146 s.current.csr = pMWin->iEphCsr;
163152 ** are four options - they may never be deleted (eDelete==0), they may
163157 switch( pMWin->eStart ){
163159 if( pMWin->eFrmType!=TK_RANGE
163160 && windowExprGtZero(pParse, pMWin->pStart)
163167 if( pMWin->eEnd==TK_PRECEDING ){
163168 if( pMWin->eFrmType!=TK_RANGE
163169 && windowExprGtZero(pParse, pMWin->pEnd)
163183 /* Allocate registers for the array of values from the sub-query, the
163186 regNew = pParse->nMem+1;
163187 pParse->nMem += nInput;
163188 regRecord = ++pParse->nMem;
163189 s.regRowid = ++pParse->nMem;
163194 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
163195 regStart = ++pParse->nMem;
163197 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
163198 regEnd = ++pParse->nMem;
163204 if( pMWin->eFrmType!=TK_ROWS ){
163205 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
163206 regNewPeer = regNew + pMWin->nBufferCol;
163207 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
163208 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
163209 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
163210 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
163211 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
163214 /* Load the column values for the row returned by the sub-select
163227 if( pMWin->pPartition ){
163229 ExprList *pPart = pMWin->pPartition;
163230 int nPart = pPart->nExpr;
163231 int regNewPart = regNew + pMWin->nBufferCol;
163234 regFlushPart = ++pParse->nMem;
163235 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
163241 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
163247 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
163254 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
163255 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
163258 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
163259 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
163262 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
163263 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
163275 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
163276 assert( pMWin->eEnd==TK_FOLLOWING );
163280 if( pMWin->eStart!=TK_UNBOUNDED ){
163289 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
163290 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
163291 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
163292 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
163303 if( pMWin->eStart==TK_FOLLOWING ){
163305 if( pMWin->eEnd!=TK_UNBOUNDED ){
163306 if( pMWin->eFrmType==TK_RANGE ){
163320 if( pMWin->eEnd==TK_PRECEDING ){
163321 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
163329 if( pMWin->eEnd!=TK_UNBOUNDED ){
163330 if( pMWin->eFrmType==TK_RANGE ){
163360 if( pMWin->pPartition ){
163368 if( pMWin->eEnd==TK_PRECEDING ){
163369 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
163373 }else if( pMWin->eStart==TK_FOLLOWING ){
163379 if( pMWin->eFrmType==TK_RANGE ){
163384 if( pMWin->eEnd==TK_UNBOUNDED ){
163389 assert( pMWin->eEnd==TK_FOLLOWING );
163414 if( pMWin->pPartition ){
163415 if( pMWin->regStartRowid ){
163416 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
163417 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
163431 ** 2001-09-15
163445 ** That input file is processed by Lemon to generate a C-language
163447 ** this comment as part of the translated C-code. Edits should be made
163454 ** Disable all error recovery processing in the parser push-down
163507 sqlite3 *db = pParse->db;
163508 pParse->disableLookaside++;
163528 sqlite3ExprListDelete(pParse->db, pOrderBy);
163529 sqlite3ExprDelete(pParse->db, pLimit);
163535 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
163541 if( p->pPrior ){
163545 pLoop->pNext = pNext;
163546 pLoop->selFlags |= SF_Compound;
163548 pLoop = pLoop->pPrior;
163551 if( pLoop->pOrderBy || pLoop->pLimit ){
163553 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
163554 sqlite3SelectOpName(pNext->op));
163558 if( (p->selFlags & SF_MultiValue)==0 &&
163559 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
163572 pSelect->pWith = pWith;
163575 sqlite3WithDelete(pParse->db, pWith);
163583 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
163586 p->op = (u8)op;
163587 p->affExpr = 0;
163588 p->flags = EP_Leaf;
163590 /* p->iAgg = -1; // Not required */
163591 p->pLeft = p->pRight = 0;
163592 p->pAggInfo = 0;
163593 memset(&p->x, 0, sizeof(p->x));
163594 memset(&p->y, 0, sizeof(p->y));
163595 p->op2 = 0;
163596 p->iTable = 0;
163597 p->iColumn = 0;
163598 p->u.zToken = (char*)&p[1];
163599 memcpy(p->u.zToken, t.z, t.n);
163600 p->u.zToken[t.n] = 0;
163601 p->w.iOfst = (int)(t.z - pParse->zTail);
163602 if( sqlite3Isquote(p->u.zToken[0]) ){
163606 p->nHeight = 1;
163619 sqlite3 *db = pParse->db;
163620 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
163621 pA->op = (u8)op;
163622 sqlite3ExprDelete(db, pA->pRight);
163623 pA->pRight = 0;
163641 && pParse->db->init.busy==0
163644 pIdToken->n, pIdToken->z);
163847 ** that represent terminal and non-terminal symbols.
163853 ** (also known as: "terminal symbols") have fall-back
163857 ** YYACTIONTYPE is the data type used for "action codes" - numbers
163862 ** value associated with a terminal or non-terminal
163865 ** Each non-terminal can have a different minor type.
163887 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
163888 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
163891 ** YY_NO_ACTION The yy_action[] code for no-op
163937 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
163938 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
163955 /* Define the yytestcase() macro to be a no-op if is not already defined
163980 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
163989 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
164002 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
164014 ** shifting non-terminals after a reduce.
164526 #define YY_REDUCE_MIN (-271)
164529 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
164530 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
164531 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
164532 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
164533 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164534 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164535 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164536 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
164542 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164543 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
164544 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
164545 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
164546 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
164547 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
164550 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
164551 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
164631 /********** End of lemon-generated parsing tables *****************************/
164854 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
164857 YYMINORTYPE minor; /* The user-supplied minor token value. This
164867 int yyhwm; /* High-water mark of the stack */
165667 newSize = p->yystksz*2 + 100;
165668 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
165669 if( p->yystack==&p->yystk0 ){
165671 if( pNew ) pNew[0] = p->yystk0;
165673 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
165676 p->yystack = pNew;
165677 p->yytos = &p->yystack[idx];
165681 yyTracePrompt, p->yystksz, newSize);
165684 p->yystksz = newSize;
165705 yypParser->yyhwm = 0;
165708 yypParser->yytos = NULL;
165709 yypParser->yystack = NULL;
165710 yypParser->yystksz = 0;
165712 yypParser->yystack = &yypParser->yystk0;
165713 yypParser->yystksz = 1;
165717 yypParser->yyerrcnt = -1;
165719 yypParser->yytos = yypParser->yystack;
165720 yypParser->yystack[0].stateno = 0;
165721 yypParser->yystack[0].major = 0;
165723 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
165768 ** terminal or non-terminal is destroyed. This can happen
165783 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
165798 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
165815 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
165824 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
165829 sqlite3WithDelete(pParse->db, (yypminor->yy521));
165835 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
165841 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
165850 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
165856 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
165861 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
165868 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
165884 assert( pParser->yytos!=0 );
165885 assert( pParser->yytos > pParser->yystack );
165886 yytos = pParser->yytos--;
165891 yyTokenName[yytos->major]);
165894 yy_destructor(pParser, yytos->major, &yytos->minor);
165902 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
165904 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
165935 return pParser->yyhwm;
165941 ** is in state X and has a lookahead token Y. In a well-tested
165978 ** look-ahead token iLookAhead.
165981 YYCODETYPE iLookAhead, /* The look-ahead token */
166019 int j = i - iLookAhead + YYWILDCARD;
166042 ** Find the appropriate action for a parser given the non-terminal
166043 ** look-ahead token iLookAhead.
166047 YYCODETYPE iLookAhead /* The look-ahead token */
166082 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
166101 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166105 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166106 yyNewState - YY_MIN_REDUCE);
166124 yypParser->yytos++;
166126 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
166127 yypParser->yyhwm++;
166128 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
166132 if( yypParser->yytos>yypParser->yystackEnd ){
166133 yypParser->yytos--;
166138 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
166140 yypParser->yytos--;
166147 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
166149 yytos = yypParser->yytos;
166150 yytos->stateno = yyNewState;
166151 yytos->major = yyMajor;
166152 yytos->minor.yy0 = yyMinor;
166156 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
166567 ** of symbols on the right-hand side of that rule. */
166569 -1, /* (0) explain ::= EXPLAIN */
166570 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
166571 -1, /* (2) cmdx ::= cmd */
166572 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
166574 -1, /* (5) transtype ::= DEFERRED */
166575 -1, /* (6) transtype ::= IMMEDIATE */
166576 -1, /* (7) transtype ::= EXCLUSIVE */
166577 -2, /* (8) cmd ::= COMMIT|END trans_opt */
166578 -2, /* (9) cmd ::= ROLLBACK trans_opt */
166579 -2, /* (10) cmd ::= SAVEPOINT nm */
166580 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
166581 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
166582 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
166583 -1, /* (14) createkw ::= CREATE */
166585 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
166586 -1, /* (17) temp ::= TEMP */
166588 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
166589 -2, /* (20) create_table_args ::= AS select */
166591 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
166592 -2, /* (23) table_option ::= WITHOUT nm */
166593 -1, /* (24) table_option ::= nm */
166594 -2, /* (25) columnname ::= nm typetoken */
166596 -4, /* (27) typetoken ::= typename LP signed RP */
166597 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
166598 -2, /* (29) typename ::= typename ID|STRING */
166601 -2, /* (32) ccons ::= CONSTRAINT nm */
166602 -3, /* (33) ccons ::= DEFAULT scantok term */
166603 -4, /* (34) ccons ::= DEFAULT LP expr RP */
166604 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
166605 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
166606 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
166607 -3, /* (38) ccons ::= NOT NULL onconf */
166608 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
166609 -2, /* (40) ccons ::= UNIQUE onconf */
166610 -4, /* (41) ccons ::= CHECK LP expr RP */
166611 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
166612 -1, /* (43) ccons ::= defer_subclause */
166613 -2, /* (44) ccons ::= COLLATE ID|STRING */
166614 -3, /* (45) generated ::= LP expr RP */
166615 -4, /* (46) generated ::= LP expr RP ID */
166617 -1, /* (48) autoinc ::= AUTOINCR */
166619 -2, /* (50) refargs ::= refargs refarg */
166620 -2, /* (51) refarg ::= MATCH nm */
166621 -3, /* (52) refarg ::= ON INSERT refact */
166622 -3, /* (53) refarg ::= ON DELETE refact */
166623 -3, /* (54) refarg ::= ON UPDATE refact */
166624 -2, /* (55) refact ::= SET NULL */
166625 -2, /* (56) refact ::= SET DEFAULT */
166626 -1, /* (57) refact ::= CASCADE */
166627 -1, /* (58) refact ::= RESTRICT */
166628 -2, /* (59) refact ::= NO ACTION */
166629 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
166630 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
166632 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
166633 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
166635 -1, /* (66) tconscomma ::= COMMA */
166636 -2, /* (67) tcons ::= CONSTRAINT nm */
166637 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
166638 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
166639 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
166640 …-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
166643 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
166645 -2, /* (76) orconf ::= OR resolvetype */
166646 -1, /* (77) resolvetype ::= IGNORE */
166647 -1, /* (78) resolvetype ::= REPLACE */
166648 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
166649 -2, /* (80) ifexists ::= IF EXISTS */
166651 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
166652 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
166653 -1, /* (84) cmd ::= select */
166654 -3, /* (85) select ::= WITH wqlist selectnowith */
166655 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
166656 -1, /* (87) select ::= selectnowith */
166657 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
166658 -1, /* (89) multiselect_op ::= UNION */
166659 -2, /* (90) multiselect_op ::= UNION ALL */
166660 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
166661 …-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
166662 …-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
166663 -4, /* (94) values ::= VALUES LP nexprlist RP */
166664 -5, /* (95) values ::= values COMMA LP nexprlist RP */
166665 -1, /* (96) distinct ::= DISTINCT */
166666 -1, /* (97) distinct ::= ALL */
166669 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
166670 -3, /* (101) selcollist ::= sclp scanpt STAR */
166671 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
166672 -2, /* (103) as ::= AS nm */
166675 -2, /* (106) from ::= FROM seltablist */
166676 -2, /* (107) stl_prefix ::= seltablist joinop */
166678 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
166679 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
166680 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
166681 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
166682 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
166684 -2, /* (115) dbnm ::= DOT nm */
166685 -1, /* (116) fullname ::= nm */
166686 -3, /* (117) fullname ::= nm DOT nm */
166687 -1, /* (118) xfullname ::= nm */
166688 -3, /* (119) xfullname ::= nm DOT nm */
166689 -5, /* (120) xfullname ::= nm DOT nm AS nm */
166690 -3, /* (121) xfullname ::= nm AS nm */
166691 -1, /* (122) joinop ::= COMMA|JOIN */
166692 -2, /* (123) joinop ::= JOIN_KW JOIN */
166693 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
166694 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
166695 -2, /* (126) on_using ::= ON expr */
166696 -4, /* (127) on_using ::= USING LP idlist RP */
166699 -3, /* (130) indexed_by ::= INDEXED BY nm */
166700 -2, /* (131) indexed_by ::= NOT INDEXED */
166702 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
166703 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
166704 -3, /* (135) sortlist ::= expr sortorder nulls */
166705 -1, /* (136) sortorder ::= ASC */
166706 -1, /* (137) sortorder ::= DESC */
166708 -2, /* (139) nulls ::= NULLS FIRST */
166709 -2, /* (140) nulls ::= NULLS LAST */
166712 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
166714 -2, /* (145) having_opt ::= HAVING expr */
166716 -2, /* (147) limit_opt ::= LIMIT expr */
166717 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
166718 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
166719 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
166721 -2, /* (152) where_opt ::= WHERE expr */
166723 -2, /* (154) where_opt_ret ::= WHERE expr */
166724 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
166725 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
166726 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
166727 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
166728 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
166729 -3, /* (160) setlist ::= nm EQ expr */
166730 -5, /* (161) setlist ::= LP idlist RP EQ expr */
166731 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
166732 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
166734 -2, /* (165) upsert ::= RETURNING selcollist */
166735 …-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
166736 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
166737 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
166738 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
166739 -2, /* (170) returning ::= RETURNING selcollist */
166740 -2, /* (171) insert_cmd ::= INSERT orconf */
166741 -1, /* (172) insert_cmd ::= REPLACE */
166743 -3, /* (174) idlist_opt ::= LP idlist RP */
166744 -3, /* (175) idlist ::= idlist COMMA nm */
166745 -1, /* (176) idlist ::= nm */
166746 -3, /* (177) expr ::= LP expr RP */
166747 -1, /* (178) expr ::= ID|INDEXED */
166748 -1, /* (179) expr ::= JOIN_KW */
166749 -3, /* (180) expr ::= nm DOT nm */
166750 -5, /* (181) expr ::= nm DOT nm DOT nm */
166751 -1, /* (182) term ::= NULL|FLOAT|BLOB */
166752 -1, /* (183) term ::= STRING */
166753 -1, /* (184) term ::= INTEGER */
166754 -1, /* (185) expr ::= VARIABLE */
166755 -3, /* (186) expr ::= expr COLLATE ID|STRING */
166756 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
166757 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
166758 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
166759 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
166760 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
166761 -1, /* (192) term ::= CTIME_KW */
166762 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
166763 -3, /* (194) expr ::= expr AND expr */
166764 -3, /* (195) expr ::= expr OR expr */
166765 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
166766 -3, /* (197) expr ::= expr EQ|NE expr */
166767 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
166768 -3, /* (199) expr ::= expr PLUS|MINUS expr */
166769 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
166770 -3, /* (201) expr ::= expr CONCAT expr */
166771 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
166772 -3, /* (203) expr ::= expr likeop expr */
166773 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
166774 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
166775 -3, /* (206) expr ::= expr NOT NULL */
166776 -3, /* (207) expr ::= expr IS expr */
166777 -4, /* (208) expr ::= expr IS NOT expr */
166778 -6, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
166779 -5, /* (210) expr ::= expr IS DISTINCT FROM expr */
166780 -2, /* (211) expr ::= NOT expr */
166781 -2, /* (212) expr ::= BITNOT expr */
166782 -2, /* (213) expr ::= PLUS|MINUS expr */
166783 -3, /* (214) expr ::= expr PTR expr */
166784 -1, /* (215) between_op ::= BETWEEN */
166785 -2, /* (216) between_op ::= NOT BETWEEN */
166786 -5, /* (217) expr ::= expr between_op expr AND expr */
166787 -1, /* (218) in_op ::= IN */
166788 -2, /* (219) in_op ::= NOT IN */
166789 -5, /* (220) expr ::= expr in_op LP exprlist RP */
166790 -3, /* (221) expr ::= LP select RP */
166791 -5, /* (222) expr ::= expr in_op LP select RP */
166792 -5, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
166793 -4, /* (224) expr ::= EXISTS LP select RP */
166794 -5, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
166795 -5, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
166796 -4, /* (227) case_exprlist ::= WHEN expr THEN expr */
166797 -2, /* (228) case_else ::= ELSE expr */
166799 -1, /* (230) case_operand ::= expr */
166802 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
166803 -1, /* (234) nexprlist ::= expr */
166805 -3, /* (236) paren_exprlist ::= LP exprlist RP */
166806 …-12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
166807 -1, /* (238) uniqueflag ::= UNIQUE */
166810 -3, /* (241) eidlist_opt ::= LP eidlist RP */
166811 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
166812 -3, /* (243) eidlist ::= nm collate sortorder */
166814 -2, /* (245) collate ::= COLLATE ID|STRING */
166815 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
166816 -2, /* (247) cmd ::= VACUUM vinto */
166817 -3, /* (248) cmd ::= VACUUM nm vinto */
166818 -2, /* (249) vinto ::= INTO expr */
166820 -3, /* (251) cmd ::= PRAGMA nm dbnm */
166821 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
166822 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
166823 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
166824 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
166825 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
166826 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
166827 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
166828 …-11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
166829 -1, /* (260) trigger_time ::= BEFORE|AFTER */
166830 -2, /* (261) trigger_time ::= INSTEAD OF */
166832 -1, /* (263) trigger_event ::= DELETE|INSERT */
166833 -1, /* (264) trigger_event ::= UPDATE */
166834 -3, /* (265) trigger_event ::= UPDATE OF idlist */
166836 -2, /* (267) when_clause ::= WHEN expr */
166837 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
166838 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
166839 -3, /* (270) trnm ::= nm DOT nm */
166840 -3, /* (271) tridxby ::= INDEXED BY nm */
166841 -2, /* (272) tridxby ::= NOT INDEXED */
166842 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
166843 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
166844 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
166845 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
166846 -4, /* (277) expr ::= RAISE LP IGNORE RP */
166847 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
166848 -1, /* (279) raisetype ::= ROLLBACK */
166849 -1, /* (280) raisetype ::= ABORT */
166850 -1, /* (281) raisetype ::= FAIL */
166851 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
166852 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
166853 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
166855 -2, /* (286) key_opt ::= KEY expr */
166856 -1, /* (287) cmd ::= REINDEX */
166857 -3, /* (288) cmd ::= REINDEX nm dbnm */
166858 -1, /* (289) cmd ::= ANALYZE */
166859 -3, /* (290) cmd ::= ANALYZE nm dbnm */
166860 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
166861 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
166862 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
166863 -1, /* (294) add_column_fullname ::= fullname */
166864 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
166865 -1, /* (296) cmd ::= create_vtab */
166866 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
166867 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
166869 -1, /* (300) vtabargtoken ::= ANY */
166870 -3, /* (301) vtabargtoken ::= lp anylist RP */
166871 -1, /* (302) lp ::= LP */
166872 -2, /* (303) with ::= WITH wqlist */
166873 -3, /* (304) with ::= WITH RECURSIVE wqlist */
166874 -1, /* (305) wqas ::= AS */
166875 -2, /* (306) wqas ::= AS MATERIALIZED */
166876 -3, /* (307) wqas ::= AS NOT MATERIALIZED */
166877 -6, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
166878 -1, /* (309) wqlist ::= wqitem */
166879 -3, /* (310) wqlist ::= wqlist COMMA wqitem */
166880 -1, /* (311) windowdefn_list ::= windowdefn */
166881 -3, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
166882 -5, /* (313) windowdefn ::= nm AS LP window RP */
166883 -5, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
166884 -6, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
166885 -4, /* (316) window ::= ORDER BY sortlist frame_opt */
166886 -5, /* (317) window ::= nm ORDER BY sortlist frame_opt */
166887 -1, /* (318) window ::= frame_opt */
166888 -2, /* (319) window ::= nm frame_opt */
166890 -3, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
166891 …-6, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
166892 -1, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
166893 -1, /* (324) frame_bound_s ::= frame_bound */
166894 -2, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
166895 -1, /* (326) frame_bound_e ::= frame_bound */
166896 -2, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
166897 -2, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
166898 -2, /* (329) frame_bound ::= CURRENT ROW */
166900 -2, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
166901 -2, /* (332) frame_exclude ::= NO OTHERS */
166902 -2, /* (333) frame_exclude ::= CURRENT ROW */
166903 -1, /* (334) frame_exclude ::= GROUP|TIES */
166904 -2, /* (335) window_clause ::= WINDOW windowdefn_list */
166905 -2, /* (336) filter_over ::= filter_clause over_clause */
166906 -1, /* (337) filter_over ::= over_clause */
166907 -1, /* (338) filter_over ::= filter_clause */
166908 -4, /* (339) over_clause ::= OVER LP window RP */
166909 -2, /* (340) over_clause ::= OVER nm */
166910 -5, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
166911 -1, /* (342) input ::= cmdlist */
166912 -2, /* (343) cmdlist ::= cmdlist ecmd */
166913 -1, /* (344) cmdlist ::= ecmd */
166914 -1, /* (345) ecmd ::= SEMI */
166915 -2, /* (346) ecmd ::= cmdx SEMI */
166916 -3, /* (347) ecmd ::= explain cmdx SEMI */
166918 -1, /* (349) trans_opt ::= TRANSACTION */
166919 -2, /* (350) trans_opt ::= TRANSACTION nm */
166920 -1, /* (351) savepoint_opt ::= SAVEPOINT */
166922 -2, /* (353) cmd ::= create_table create_table_args */
166923 -1, /* (354) table_option_set ::= table_option */
166924 -4, /* (355) columnlist ::= columnlist COMMA columnname carglist */
166925 -2, /* (356) columnlist ::= columnname carglist */
166926 -1, /* (357) nm ::= ID|INDEXED */
166927 -1, /* (358) nm ::= STRING */
166928 -1, /* (359) nm ::= JOIN_KW */
166929 -1, /* (360) typetoken ::= typename */
166930 -1, /* (361) typename ::= ID|STRING */
166931 -1, /* (362) signed ::= plus_num */
166932 -1, /* (363) signed ::= minus_num */
166933 -2, /* (364) carglist ::= carglist ccons */
166935 -2, /* (366) ccons ::= NULL onconf */
166936 -4, /* (367) ccons ::= GENERATED ALWAYS AS generated */
166937 -2, /* (368) ccons ::= AS generated */
166938 -2, /* (369) conslist_opt ::= COMMA conslist */
166939 -3, /* (370) conslist ::= conslist tconscomma tcons */
166940 -1, /* (371) conslist ::= tcons */
166942 -1, /* (373) defer_subclause_opt ::= defer_subclause */
166943 -1, /* (374) resolvetype ::= raisetype */
166944 -1, /* (375) selectnowith ::= oneselect */
166945 -1, /* (376) oneselect ::= values */
166946 -2, /* (377) sclp ::= selcollist COMMA */
166947 -1, /* (378) as ::= ID|STRING */
166948 -1, /* (379) indexed_opt ::= indexed_by */
166950 -1, /* (381) expr ::= term */
166951 -1, /* (382) likeop ::= LIKE_KW|MATCH */
166952 -1, /* (383) exprlist ::= nexprlist */
166953 -1, /* (384) nmnum ::= plus_num */
166954 -1, /* (385) nmnum ::= nm */
166955 -1, /* (386) nmnum ::= ON */
166956 -1, /* (387) nmnum ::= DELETE */
166957 -1, /* (388) nmnum ::= DEFAULT */
166958 -1, /* (389) plus_num ::= INTEGER|FLOAT */
166960 -3, /* (391) foreach_clause ::= FOR EACH ROW */
166961 -1, /* (392) trnm ::= nm */
166963 -1, /* (394) database_kw_opt ::= DATABASE */
166966 -1, /* (397) kwcolumn_opt ::= COLUMNKW */
166967 -1, /* (398) vtabarglist ::= vtabarg */
166968 -3, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
166969 -2, /* (400) vtabarg ::= vtabarg vtabargtoken */
166971 -4, /* (402) anylist ::= anylist LP anylist RP */
166972 -2, /* (403) anylist ::= anylist ANY */
166985 ** only called from one place, optimizing compilers will in-line it, which
167002 yymsp = yypParser->yytos;
167016 { pParse->explain = 1; }
167019 { pParse->explain = 2; }
167025 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
167034 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
167038 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
167057 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[…
167074 {yymsp[-2].minor.yy394 = 1;}
167077 {yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
167081 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
167087 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167094 {yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
167095 yymsp[-2].minor.yy285 = yylhsminor.yy285;
167100 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
167102 yymsp[-1].minor.yy285 = 0;
167119 {sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
167128 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
167133 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
167137 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
167153 {pParse->constraintName = yymsp[0].minor.yy0;}
167156 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
167159 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
167162 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
167167 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
167175 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
167184 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
167191 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
167194 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
167203 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
167206 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
167212 { yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
167215 { yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy2…
167218 { yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
167221 { yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
167224 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
167227 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
167230 { yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
167233 { yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
167236 { yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
167239 { yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
167242 { yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
167245 {yymsp[-2].minor.yy394 = 0;}
167250 {yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
167257 {yymsp[-1].minor.yy394 = 1;}
167260 {yymsp[-1].minor.yy394 = 0;}
167263 {pParse->constraintName.n = 0;}
167266 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
167269 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
167273 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
167277 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
167286 {yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
167297 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
167302 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
167307 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
167314 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167318 {yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167321 {yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167329 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
167335 Select *pLhs = yymsp[-2].minor.yy47;
167336 if( pRhs && pRhs->pPrior ){
167345 pRhs->op = (u8)yymsp[-1].minor.yy394;
167346 pRhs->pPrior = pLhs;
167347 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
167348 pRhs->selFlags &= ~SF_MultiValue;
167349 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
167351 sqlite3SelectDelete(pParse->db, pLhs);
167353 yymsp[-2].minor.yy47 = pRhs;
167358 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
167361 {yymsp[-1].minor.yy394 = TK_ALL;}
167365 …-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].min…
167370 …-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].min…
167371 if( yymsp[-9].minor.yy47 ){
167372 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
167374 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
167380 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
167385 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
167386 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
167387 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
167389 pRight->op = TK_ALL;
167390 pRight->pPrior = pLeft;
167391 yymsp[-4].minor.yy47 = pRight;
167393 yymsp[-4].minor.yy47 = pLeft;
167413 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528…
167414 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
167415 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
167420 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
167421 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
167427 Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0);
167429 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
167436 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
167444 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
167445 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
167450 …if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0) ) yymsp[-1].minor.yy131->a[yyms…
167455 …yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor…
167460 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor…
167461 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
167466 …yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor…
167467 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
167472 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167477 …if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0…
167478 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
167479 }else if( yymsp[-3].minor.yy131->nSrc==1 ){
167480 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167481 if( yymsp[-5].minor.yy131 ){
167482 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
167483 SrcItem *pOld = yymsp[-3].minor.yy131->a;
167484 pNew->zName = pOld->zName;
167485 pNew->zDatabase = pOld->zDatabase;
167486 pNew->pSelect = pOld->pSelect;
167487 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
167488 pNew->fg.isNestedFrom = 1;
167490 if( pOld->fg.isTabFunc ){
167491 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
167492 pOld->u1.pFuncArg = 0;
167493 pOld->fg.isTabFunc = 0;
167494 pNew->fg.isTabFunc = 1;
167496 pOld->zName = pOld->zDatabase = 0;
167497 pOld->pSelect = 0;
167499 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
167502 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
167503 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom,0);
167504 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167515 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
167521 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
167522 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
167524 yymsp[-2].minor.yy131 = yylhsminor.yy131;
167527 {yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
167530 {yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
167534 …yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
167535 …if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
167540 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
167541 …if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
167548 {yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
167551 {yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
167554 {yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
167557 {yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
167560 {yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
167566 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
167569 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
167573 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
167577 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
167578 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167583 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
167584 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167598 {yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
167601 {yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
167617 {yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
167620 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,0);}
167623 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167626 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
167630 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
167631 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
167635 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
167638 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
167642 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167643 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167644 if( yymsp[-1].minor.yy131 ){
167645 SrcList *pFromClause = yymsp[-1].minor.yy131;
167646 if( pFromClause->nSrc>1 ){
167654 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
167656 …sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].mi…
167661 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
167662 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
167667 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
167673 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
167675 yymsp[-2].minor.yy322 = yylhsminor.yy322;
167679 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
167684 …sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5…
167689 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
167696 { yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
167699 { yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,…
167702 { yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0…
167705 { yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
167708 { yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy5…
167717 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
167720 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
167723 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
167726 {yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
167730 {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167734 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
167738 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167742 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0);
167743 Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
167751 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167755 {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167759 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
167760 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
167773 ** in the virtual machine. #N is the N-th register. */
167774 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
167776 if( pParse->nested==0 ){
167781 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
167788 …yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.…
167793 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
167794 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
167799 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[…
167801 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167805 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
167807 yymsp[-3].minor.yy528 = yylhsminor.yy528;
167811 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[…
167814 yymsp[-5].minor.yy528 = yylhsminor.yy528;
167818 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
167821 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167831 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
167832 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
167833 if( yymsp[-4].minor.yy528 ){
167834 yymsp[-4].minor.yy528->x.pList = pList;
167835 if( ALWAYS(pList->nExpr) ){
167836 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
167839 sqlite3ExprListDelete(pParse->db, pList);
167844 {yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167853 {yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy5…
167856 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
167861 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
167862 yymsp[-1].minor.yy0.n &= 0x7fffffff;
167864 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
167865 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167866 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy528, 0);
167867 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
167873 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
167874 yymsp[-3].minor.yy0.n &= 0x7fffffff;
167875 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167876 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
167878 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
167879 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy528, 0);
167880 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
167884 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
167887 {yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy528,0);}
167891 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
167892 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL);
167897 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
167898 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL);
167903 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
167904 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL);
167909 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
167910 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL);
167915 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwri…
167919 …yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
167920 /*A-overwrites-B*/
167925 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
167927 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167929 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167937 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167939 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy528, 0);
167940 if( yymsp[-4].minor.yy528 ){
167941 yymsp[-4].minor.yy528->x.pList = pList;
167943 sqlite3ExprListDelete(pParse->db, pList);
167945 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
167950 if( yymsp[-1].minor.yy322==0 ){
167959 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167960 …yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false…
167961 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
167963 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167964 …if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=T…
167965 yymsp[-1].minor.yy322->a[0].pExpr = 0;
167966 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167968 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy528, pRHS);
167970 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
167971 if( yymsp[-4].minor.yy528==0 ){
167972 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167973 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
167974 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
167975 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
167978 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
167981 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
167982 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
167985 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
167991 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
167992 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
167997 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
167998 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
167999 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
168004 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
168007 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
168008 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
168009 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
168015 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
168016 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
168021 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy528, 0);
168022 if( yymsp[-4].minor.yy528 ){
168023 …yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
168024 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
168026 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
168027 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168033 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
168034 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
168039 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
168040 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
168044 {yymsp[0].minor.yy528 = yymsp[0].minor.yy528; /*A-overwrites-X*/}
168047 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
168050 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
168054 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
168058 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
168059 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy3…
168060 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_…
168061 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
168062 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
168075 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
168080 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
168084 {sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
168090 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
168093 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
168096 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
168099 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
168102 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
168105 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
168110 all.z = yymsp[-3].minor.yy0.z;
168111 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
168112 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
168117 …-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].min…
168118 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
168122 { yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
168125 { yymsp[-1].minor.yy394 = TK_INSTEAD;}
168132 {yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
168135 {yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
168143 { yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
168147 assert( yymsp[-2].minor.yy33!=0 );
168148 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
168149 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
168154 assert( yymsp[-1].minor.yy33!=0 );
168155 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
168160 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
168181 …tep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy…
168182 yymsp[-8].minor.yy33 = yylhsminor.yy33;
168186 …-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy4…
168188 yymsp[-7].minor.yy33 = yylhsminor.yy33;
168191 …sminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[…
168192 yymsp[-5].minor.yy33 = yylhsminor.yy33;
168195 …ggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*y…
168196 yymsp[-2].minor.yy33 = yylhsminor.yy33;
168200 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
168201 if( yymsp[-3].minor.yy528 ){
168202 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
168208 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
168209 if( yymsp[-5].minor.yy528 ) {
168210 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
168222 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
168227 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
168239 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168245 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168249 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
168254 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
168255 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
168260 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
168271 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
168282 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
168301 {yymsp[-1].minor.yy516 = M10d_Yes;}
168304 {yymsp[-2].minor.yy516 = M10d_No;}
168308 …sp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].…
168313 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
168318 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
168328 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
168329 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
168332 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168336 if( ALWAYS(yymsp[-1].minor.yy41) ){
168337 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
168339 yylhsminor.yy41 = yymsp[-1].minor.yy41;
168341 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168345 …yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, y…
168350 …e3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp…
168352 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168356 …yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322…
168361 …1 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.…
168363 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168374 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
168376 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168385 …ylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yy…
168387 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168391 …wAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yy…
168393 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168403 {yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
168404 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168407 {yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
168408 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168414 {yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
168418 {yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
168421 {yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
168424 { yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
168429 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
168431 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168435 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168439 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168441 yylhsminor.yy41->eFrmType = TK_FILTER;
168442 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
168444 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
168451 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
168452 assert( yymsp[-3].minor.yy41!=0 );
168457 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168458 if( yymsp[-1].minor.yy41 ){
168459 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
168464 { yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
168546 yypParser->yytos = yymsp;
168547 yymsp->stateno = (YYACTIONTYPE)yyact;
168548 yymsp->major = (YYCODETYPE)yygoto;
168567 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
168615 yypParser->yyerrcnt = -1;
168617 assert( yypParser->yytos==yypParser->yystack );
168639 ** <li> An option argument of a grammar-specified type.
168663 assert( yypParser->yytos!=0 );
168668 yyact = yypParser->yytos->stateno;
168676 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
168682 assert( yypParser->yytos>=yypParser->yystack );
168683 assert( yyact==yypParser->yytos->stateno );
168686 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
168696 yypParser->yytos[yysize].stateno);
168710 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
168711 yypParser->yyhwm++;
168712 assert( yypParser->yyhwm ==
168713 (int)(yypParser->yytos - yypParser->yystack));
168717 if( yypParser->yytos>=yypParser->yystackEnd ){
168722 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
168734 yypParser->yyerrcnt--;
168738 yypParser->yytos--;
168772 if( yypParser->yyerrcnt<0 ){
168775 yymx = yypParser->yytos->major;
168786 while( yypParser->yytos > yypParser->yystack ){
168787 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
168792 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
168796 yypParser->yyerrcnt = -1;
168803 yypParser->yyerrcnt = 3;
168806 yyact = yypParser->yytos->stateno;
168828 if( yypParser->yyerrcnt<=0 ){
168831 yypParser->yyerrcnt = 3;
168836 yypParser->yyerrcnt = -1;
168848 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
168849 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
168888 ** individual tokens and sends those tokens one-by-one over to the
168913 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
168918 #define CC_SLASH 16 /* '/'. / or c-style comment */
168977 ** lower-case ASCII equivalent. On ASCII machines, this is just
168978 ** an upper-to-lower case map. On EBCDIC machines we also need
169031 ** might be implemented more directly using a hand-written hash table.
169089 /* aKWHash[i] is the hash value for the i-th keyword */
169103 ** then the i-th keyword has no more hash collisions. Otherwise,
169104 ** the next keyword with the same hash is aKWHash[i]-1. */
169119 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
169135 ** the text for the i-th keyword. */
169150 /* aKWCode[i] is the parser symbol code for the i-th keyword */
169312 /* Check to see if z[0..n-1] is a keyword. If it is, write the
169319 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
169320 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
169514 ** For ASCII, any character with the high-order bit set is
169515 ** allowed in an identifier. For 7-bit characters,
169545 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
169587 ** impossible to call a window-function without a FILTER clause.
169634 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
169648 if( z[1]=='-' ){
169650 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
169686 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
169812 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
169928 void *pEngine; /* The LEMON-generated LALR(1) parser */
169931 int lastTokenParsed = -1; /* type of the previous token */
169932 sqlite3 *db = pParse->db; /* The database connection */
169936 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
169938 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
169941 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
169942 if( db->nVdbeActive==0 ){
169943 AtomicStore(&db->u1.isInterrupted, 0);
169945 pParse->rc = SQLITE_OK;
169946 pParse->zTail = zSql;
169948 if( db->flags & SQLITE_ParserTrace ){
169965 assert( pParse->pNewTable==0 );
169966 assert( pParse->pNewTrigger==0 );
169967 assert( pParse->nVar==0 );
169968 assert( pParse->pVList==0 );
169969 pParentParse = db->pParse;
169970 db->pParse = pParse;
169973 mxSqlLen -= n;
169975 pParse->rc = SQLITE_TOOBIG;
169976 pParse->nErr++;
169988 if( AtomicLoad(&db->u1.isInterrupted) ){
169989 pParse->rc = SQLITE_INTERRUPT;
169990 pParse->nErr++;
170027 pParse->sLastToken.z = zSql;
170028 pParse->sLastToken.n = n;
170029 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
170032 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
170033 if( pParse->rc!=SQLITE_OK ) break;
170048 if( db->mallocFailed ){
170049 pParse->rc = SQLITE_NOMEM_BKPT;
170051 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
170052 if( pParse->zErrMsg==0 ){
170053 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
170055 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
170058 pParse->zTail = zSql;
170060 sqlite3_free(pParse->apVtabLock);
170063 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
170064 /* If the pParse->declareVtab flag is set, do not delete any table
170065 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
170068 sqlite3DeleteTable(db, pParse->pNewTable);
170070 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
170071 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170073 if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList);
170074 db->pParse = pParentParse;
170075 assert( nErr==0 || pParse->rc!=SQLITE_OK );
170086 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
170092 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
170104 int prevType = 0; /* Previous non-whitespace token */
170112 tokenType = -1;
170116 for(i=0; zSql[i] && pStr->accError==0; i+=n){
170144 iStartIN = pStr->nChar;
170152 assert( pStr->nChar>=(u32)iStartIN );
170153 pStr->nChar = iStartIN+1;
170157 nParen--;
170163 j = pStr->nChar;
170187 while( j<pStr->nChar ){
170188 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
170199 j = pStr->nChar;
170201 while( j<pStr->nChar ){
170202 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
170246 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
170275 ** (0) INVALID We have not yet seen a non-whitespace character.
170373 case '/': { /* C-style comments */
170385 case '-': { /* SQL-style comments from "--" to end of line */
170386 if( zSql[1]!='-' ){
170395 case '[': { /* Microsoft-style identifiers in [...] */
170402 case '`': /* Grave-accent quoted symbols used by MySQL */
170403 case '"': /* single- and double-quoted strings */
170464 zSql += nId-1;
170481 ** above, except that the parameter is required to be UTF-16 encoded, not
170482 ** UTF-8.
170494 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
170632 ** This is an extension initializer that is a no-op and always
170633 ** succeeds, except that it fails if the fault-simulation is set
170661 ** built-in extensions.
170701 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
170707 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
170712 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
170720 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
170725 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
170727 ** the SQLITE_THREADSAFE compile-time option being set to 0.
170733 ** this variable being set to non-zero will cause OSTRACE macros to emit
170780 ** This routine is a no-op except on its very first call for the process,
170796 ** * Calls to this routine from Y must block until the outer-most
170817 ** combination, the work-around is to set the correct pointer
170818 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
170822 ** to sqlite3_initialize() should be a no-op. But the initialization
170845 ** malloc subsystem - this implies that the allocation of a static
170882 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
170929 sqlite3GlobalConfig.nRefInitMutex--;
170946 u64 x = (((u64)1)<<63)-1;
170957 ** compile-time option.
170975 ** when this routine is invoked, then this routine is a harmless no-op.
171024 ** the SQLite library at run-time.
171045 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
171047 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
171048 ** Single-thread. */
171054 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
171056 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
171057 ** Multi-thread. */
171063 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
171065 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
171072 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
171079 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
171088 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
171091 ** low-level memory allocation routines to be used in place of the memory
171097 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
171106 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
171117 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
171118 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
171127 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
171139 /* no-op */
171149 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
171157 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
171168 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
171173 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
171174 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
171189 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
171199 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
171234 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
171235 ** can be changed at start-time using the
171240 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
171241 ** argument of type int. If non-zero, then URI handling is globally
171249 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
171267 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
171273 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
171274 ** negative, then that argument is changed to its compile-time default.
171276 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
171278 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
171279 ** compile-time option.
171291 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
171293 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
171353 int nBig; /* Number of full-size slots */
171354 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
171363 if( db->lookaside.bMalloced ){
171364 sqlite3_free(db->lookaside.pStart);
171369 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
171377 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
171386 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
171389 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
171398 db->lookaside.pStart = pStart;
171399 db->lookaside.pInit = 0;
171400 db->lookaside.pFree = 0;
171401 db->lookaside.sz = (u16)sz;
171402 db->lookaside.szTrue = (u16)sz;
171409 p->pNext = db->lookaside.pInit;
171410 db->lookaside.pInit = p;
171414 db->lookaside.pSmallInit = 0;
171415 db->lookaside.pSmallFree = 0;
171416 db->lookaside.pMiddle = p;
171418 p->pNext = db->lookaside.pSmallInit;
171419 db->lookaside.pSmallInit = p;
171424 db->lookaside.pEnd = p;
171425 db->lookaside.bDisable = 0;
171426 db->lookaside.bMalloced = pBuf==0 ?1:0;
171427 db->lookaside.nSlot = nBig+nSm;
171429 db->lookaside.pStart = db;
171431 db->lookaside.pSmallInit = 0;
171432 db->lookaside.pSmallFree = 0;
171433 db->lookaside.pMiddle = db;
171435 db->lookaside.pEnd = db;
171436 db->lookaside.bDisable = 1;
171437 db->lookaside.sz = 0;
171438 db->lookaside.bMalloced = 0;
171439 db->lookaside.nSlot = 0;
171456 return db->mutex;
171469 sqlite3_mutex_enter(db->mutex);
171471 for(i=0; i<db->nDb; i++){
171472 Btree *pBt = db->aDb[i].pBt;
171479 sqlite3_mutex_leave(db->mutex);
171484 ** Flush any dirty pages in the pager-cache for any attached database
171495 sqlite3_mutex_enter(db->mutex);
171497 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
171498 Btree *pBt = db->aDb[i].pBt;
171509 sqlite3_mutex_leave(db->mutex);
171522 /* IMP: R-06824-28531 */
171523 /* IMP: R-36257-52125 */
171524 db->aDb[0].zDbSName = va_arg(ap,char*);
171529 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
171530 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
171531 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
171559 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
171564 u64 oldFlags = db->flags;
171566 db->flags |= aFlagOp[i].mask;
171568 db->flags &= ~(u64)aFlagOp[i].mask;
171570 if( oldFlags!=db->flags ){
171574 *pRes = (db->flags & aFlagOp[i].mask)!=0;
171599 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
171605 rc = nKey1 - nKey2;
171621 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
171622 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
171627 ** Return true if CollSeq is the default built-in BINARY.
171630 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
171631 return p==0 || p->xCmp==binCollFunc;
171635 ** Another built-in collating sequence: NOCASE.
171641 ** At the moment there is only a UTF-8 implementation.
171652 r = nKey1-nKey2;
171667 return db->lastRowid;
171680 sqlite3_mutex_enter(db->mutex);
171681 db->lastRowid = iRowid;
171682 sqlite3_mutex_leave(db->mutex);
171695 return db->nChange;
171711 return db->nTotalChange;
171720 ** at the b-tree/pager level.
171723 while( db->pSavepoint ){
171724 Savepoint *pTmp = db->pSavepoint;
171725 db->pSavepoint = pTmp->pNext;
171728 db->nSavepoint = 0;
171729 db->nStatement = 0;
171730 db->isTransactionSavepoint = 0;
171741 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
171742 pDestructor = p->u.pDestructor;
171744 pDestructor->nRef--;
171745 if( pDestructor->nRef==0 ){
171746 pDestructor->xDestroy(pDestructor->pUserData);
171761 for(i=0; i<db->nDb; i++){
171762 Schema *pSchema = db->aDb[i].pSchema;
171764 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
171770 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
171772 if( pMod->pEpoTab ){
171773 sqlite3VtabDisconnect(db, pMod->pEpoTab);
171789 assert( sqlite3_mutex_held(db->mutex) );
171790 if( db->pVdbe ) return 1;
171791 for(j=0; j<db->nDb; j++){
171792 Btree *pBt = db->aDb[j].pBt;
171803 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
171804 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
171810 sqlite3_mutex_enter(db->mutex);
171811 if( db->mTrace & SQLITE_TRACE_CLOSE ){
171812 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
171820 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
171822 ** SQL statements below, as the v-table implementation may be storing
171833 sqlite3_mutex_leave(db->mutex);
171846 db->eOpenState = SQLITE_STATE_ZOMBIE;
171857 int iTxn = -1;
171861 return -1;
171864 sqlite3_mutex_enter(db->mutex);
171867 if( iDb<0 ) nDb--;
171870 nDb = db->nDb-1;
171873 Btree *pBt = db->aDb[iDb].pBt;
171877 sqlite3_mutex_leave(db->mutex);
171910 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
171911 sqlite3_mutex_leave(db->mutex);
171923 ** they are reset. And that the required b-tree mutex is held to make
171931 for(j=0; j<db->nDb; j++){
171932 struct Db *pDb = &db->aDb[j];
171933 if( pDb->pBt ){
171934 sqlite3BtreeClose(pDb->pBt);
171935 pDb->pBt = 0;
171937 pDb->pSchema = 0;
171942 if( db->aDb[1].pSchema ){
171943 sqlite3SchemaClear(db->aDb[1].pSchema);
171949 assert( db->nDb<=2 );
171950 assert( db->aDb==db->aDbStatic );
171953 ** locks and does not require any further unlock-notify callbacks.
171957 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
171962 pNext = p->pNext;
171967 sqlite3HashClear(&db->aFunc);
171968 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
171978 sqlite3HashClear(&db->aCollSeq);
171980 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
171985 sqlite3HashClear(&db->aModule);
171989 sqlite3ValueFree(db->pErr);
171992 sqlite3_free(db->auth.zAuthUser);
171993 sqlite3_free(db->auth.zAuthPW);
171996 db->eOpenState = SQLITE_STATE_ERROR;
171998 /* The temp-database schema is allocated differently from the other schema
172004 sqlite3DbFree(db, db->aDb[1].pSchema);
172005 if( db->xAutovacDestr ){
172006 db->xAutovacDestr(db->pAutovacPagesArg);
172008 sqlite3_mutex_leave(db->mutex);
172009 db->eOpenState = SQLITE_STATE_CLOSED;
172010 sqlite3_mutex_free(db->mutex);
172012 if( db->lookaside.bMalloced ){
172013 sqlite3_free(db->lookaside.pStart);
172020 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
172029 assert( sqlite3_mutex_held(db->mutex) );
172032 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
172034 ** modified the database schema. If the b-tree mutexes are not taken
172035 ** here, then another shared-cache connection might sneak in between
172039 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
172041 for(i=0; i<db->nDb; i++){
172042 Btree *p = db->aDb[i].pBt;
172060 db->nDeferredCons = 0;
172061 db->nDeferredImmCons = 0;
172062 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
172064 /* If one has been configured, invoke the rollback-hook callback */
172065 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
172066 db->xRollbackCallback(db->pRollbackArg);
172253 ** Return non-zero to retry the lock. Return zero to stop trying
172269 int tmout = db->busyTimeout;
172277 delay = delays[NDELAY-1];
172278 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
172281 delay = tmout - prior;
172284 sqlite3OsSleep(db->pVfs, delay*1000);
172290 int tmout = ((sqlite3 *)ptr)->busyTimeout;
172294 sqlite3OsSleep(db->pVfs, 1000000);
172305 ** If this routine returns non-zero, the lock is retried. If it
172310 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
172311 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
172313 p->nBusy = -1;
172315 p->nBusy++;
172332 sqlite3_mutex_enter(db->mutex);
172333 db->busyHandler.xBusyHandler = xBusy;
172334 db->busyHandler.pBusyArg = pArg;
172335 db->busyHandler.nBusy = 0;
172336 db->busyTimeout = 0;
172337 sqlite3_mutex_leave(db->mutex);
172359 sqlite3_mutex_enter(db->mutex);
172361 db->xProgress = xProgress;
172362 db->nProgressOps = (unsigned)nOps;
172363 db->pProgressArg = pArg;
172365 db->xProgress = 0;
172366 db->nProgressOps = 0;
172367 db->pProgressArg = 0;
172369 sqlite3_mutex_leave(db->mutex);
172385 db->busyTimeout = ms;
172397 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) ){
172402 AtomicStore(&db->u1.isInterrupted, 1);
172428 assert( sqlite3_mutex_held(db->mutex) );
172434 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
172498 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
172499 if( db->nVdbeActive ){
172501 "unable to delete/modify user-function due to active statements");
172502 assert( !db->mallocFailed );
172508 /* Trying to delete a function that does not exist. This is a no-op.
172514 assert(p || db->mallocFailed);
172524 pDestructor->nRef++;
172526 p->u.pDestructor = pDestructor;
172527 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
172528 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
172529 testcase( p->funcFlags & SQLITE_DIRECTONLY );
172530 p->xSFunc = xSFunc ? xSFunc : xStep;
172531 p->xFinalize = xFinal;
172532 p->xValue = xValue;
172533 p->xInverse = xInverse;
172534 p->pUserData = pUserData;
172535 p->nArg = (u16)nArg;
172540 ** Worker function used by utf-8 APIs that create new functions:
172567 sqlite3_mutex_enter(db->mutex);
172575 pArg->nRef = 0;
172576 pArg->xDestroy = xDestroy;
172577 pArg->pUserData = p;
172582 if( pArg && pArg->nRef==0 ){
172590 sqlite3_mutex_leave(db->mutex);
172657 sqlite3_mutex_enter(db->mutex);
172658 assert( !db->mallocFailed );
172659 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
172663 sqlite3_mutex_leave(db->mutex);
172687 sqlite3_result_error(context, zErr, -1);
172695 ** this routine is a no-op. If the function does not exist, then create
172696 ** a new one that always throws a run-time error.
172712 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
172716 sqlite3_mutex_enter(db->mutex);
172718 sqlite3_mutex_leave(db->mutex);
172731 ** A NULL trace function means that no tracing is executes. A non-NULL
172745 sqlite3_mutex_enter(db->mutex);
172746 pOld = db->pTraceArg;
172747 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
172748 db->trace.xLegacy = xTrace;
172749 db->pTraceArg = pArg;
172750 sqlite3_mutex_leave(db->mutex);
172755 /* Register a trace callback using the version-2 interface.
172768 sqlite3_mutex_enter(db->mutex);
172771 db->mTrace = mTrace;
172772 db->trace.xV2 = xTrace;
172773 db->pTraceArg = pArg;
172774 sqlite3_mutex_leave(db->mutex);
172783 ** A NULL profile function means that no profiling is executes. A non-NULL
172800 sqlite3_mutex_enter(db->mutex);
172801 pOld = db->pProfileArg;
172802 db->xProfile = xProfile;
172803 db->pProfileArg = pArg;
172804 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
172805 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
172806 sqlite3_mutex_leave(db->mutex);
172814 ** If the invoked function returns non-zero, then the commit becomes a
172830 sqlite3_mutex_enter(db->mutex);
172831 pOld = db->pCommitArg;
172832 db->xCommitCallback = xCallback;
172833 db->pCommitArg = pArg;
172834 sqlite3_mutex_leave(db->mutex);
172855 sqlite3_mutex_enter(db->mutex);
172856 pRet = db->pUpdateArg;
172857 db->xUpdateCallback = xCallback;
172858 db->pUpdateArg = pArg;
172859 sqlite3_mutex_leave(db->mutex);
172880 sqlite3_mutex_enter(db->mutex);
172881 pRet = db->pRollbackArg;
172882 db->xRollbackCallback = xCallback;
172883 db->pRollbackArg = pArg;
172884 sqlite3_mutex_leave(db->mutex);
172900 sqlite3_mutex_enter(db->mutex);
172901 pRet = db->pPreUpdateArg;
172902 db->xPreUpdateCallback = xCallback;
172903 db->pPreUpdateArg = pArg;
172904 sqlite3_mutex_leave(db->mutex);
172925 sqlite3_mutex_enter(db->mutex);
172926 if( db->xAutovacDestr ){
172927 db->xAutovacDestr(db->pAutovacPagesArg);
172929 db->xAutovacPages = xCallback;
172930 db->pAutovacPagesArg = pArg;
172931 db->xAutovacDestr = xDestructor;
172932 sqlite3_mutex_leave(db->mutex);
172989 ** into the write-ahead-log by this database connection.
173004 sqlite3_mutex_enter(db->mutex);
173005 pRet = db->pWalArg;
173006 db->xWalCallback = xCallback;
173007 db->pWalArg = pArg;
173008 sqlite3_mutex_leave(db->mutex);
173035 /* Initialize the output variables to -1 in case an error occurs. */
173036 if( pnLog ) *pnLog = -1;
173037 if( pnCkpt ) *pnCkpt = -1;
173044 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
173049 sqlite3_mutex_enter(db->mutex);
173059 db->busyHandler.nBusy = 0;
173067 if( db->nVdbeActive==0 ){
173068 AtomicStore(&db->u1.isInterrupted, 0);
173071 sqlite3_mutex_leave(db->mutex);
173079 ** to contains a zero-length string, all attached databases are
173083 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
173090 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
173099 ** associated with the specific b-tree being checkpointed is taken by
173103 ** checkpointed. If an error is encountered it is returned immediately -
173114 assert( sqlite3_mutex_held(db->mutex) );
173115 assert( !pnLog || *pnLog==-1 );
173116 assert( !pnCkpt || *pnCkpt==-1 );
173120 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
173122 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
173137 ** This function returns true if main-memory should be used instead of
173139 ** The value returned depends on the value of db->temp_store (runtime
173144 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
173145 ** ----------------- -------------- ------------------------------
173157 return ( db->temp_store==2 );
173160 return ( db->temp_store!=1 );
173173 ** Return UTF-8 encoded English language explanation of the most recent
173184 sqlite3_mutex_enter(db->mutex);
173185 if( db->mallocFailed ){
173188 testcase( db->pErr==0 );
173189 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
173190 assert( !db->mallocFailed );
173192 z = sqlite3ErrStr(db->errCode);
173195 sqlite3_mutex_leave(db->mutex);
173203 int iOffset = -1;
173204 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
173205 sqlite3_mutex_enter(db->mutex);
173206 iOffset = db->errByteOffset;
173207 sqlite3_mutex_leave(db->mutex);
173214 ** Return UTF-16 encoded English language explanation of the most recent
173234 sqlite3_mutex_enter(db->mutex);
173235 if( db->mallocFailed ){
173238 z = sqlite3_value_text16(db->pErr);
173240 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
173241 z = sqlite3_value_text16(db->pErr);
173244 ** above. If this is the case, then the db->mallocFailed flag needs to
173250 sqlite3_mutex_leave(db->mutex);
173263 if( !db || db->mallocFailed ){
173266 return db->errCode & db->errMask;
173272 if( !db || db->mallocFailed ){
173275 return db->errCode;
173278 return db ? db->iSysErrno : 0;
173305 assert( sqlite3_mutex_held(db->mutex) );
173323 ** are no active VMs, invalidate any pre-compiled statements.
173326 if( pColl && pColl->xCmp ){
173327 if( db->nVdbeActive ){
173337 ** Also, collation destructor - CollSeq.xDel() - function may need
173340 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
173341 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
173345 if( p->enc==pColl->enc ){
173346 if( p->xDel ){
173347 p->xDel(p->pUser);
173349 p->xCmp = 0;
173357 pColl->xCmp = xCompare;
173358 pColl->pUser = pCtx;
173359 pColl->xDel = xDel;
173360 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
173381 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
173426 ** If an invalid limit index is supplied, report -1.
173440 return -1;
173444 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
173445 ** there is a hard upper bound set at compile-time by a C preprocessor
173462 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
173466 return -1;
173468 oldLimit = db->aLimit[limitId];
173469 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
173471 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
173475 db->aLimit[limitId] = newLimit;
173477 return oldLimit; /* IMP: R-53341-35419 */
173481 ** This function is used to parse both URIs and non-URI filenames passed by the
173487 ** query parameter. The second argument contains the URI (or non-URI filename)
173508 const char *zUri, /* Nul-terminated URI to parse */
173523 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
173524 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
173525 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
173534 ** method that there may be extra parameters following the file-name. */
173541 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
173564 iIn-7, &zUri[7]);
173577 ** 0: Parsing file-name.
173615 if( zFile[iOut-1]==0 ){
173617 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
173633 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
173759 ** is UTF-8 encoded.
173762 const char *zFilename, /* Database filename UTF-8 encoded */
173830 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
173831 if( db->mutex==0 ){
173837 sqlite3MutexWarnOnContention(db->mutex);
173840 sqlite3_mutex_enter(db->mutex);
173841 db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff;
173842 db->nDb = 2;
173843 db->eOpenState = SQLITE_STATE_BUSY;
173844 db->aDb = db->aDbStatic;
173845 db->lookaside.bDisable = 1;
173846 db->lookaside.sz = 0;
173848 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
173849 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
173850 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
173851 db->autoCommit = 1;
173852 db->nextAutovac = -1;
173853 db->szMmap = sqlite3GlobalConfig.szMmap;
173854 db->nextPagesize = 0;
173855 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
173857 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
173861 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
173862 db->nMaxSorterMmap = 0x7FFFFFFF;
173864 db->flags |= SQLITE_ShortColNames
173871 /* The SQLITE_DQS compile-time option determines the default settings
173875 ** ---------- ----------------------- -----------------------
173882 ** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
173884 ** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
173933 sqlite3HashInit(&db->aCollSeq);
173935 sqlite3HashInit(&db->aModule);
173938 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
173939 ** and UTF-16, so add a version for each to avoid any unnecessary
173942 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
173950 if( db->mallocFailed ){
173957 ** Throw an error if any non-sense combination is used. If we
173966 db->openFlags = flags;
173974 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
173976 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
173986 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
173995 sqlite3BtreeEnter(db->aDb[0].pBt);
173996 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
173997 if( !db->mallocFailed ){
174000 sqlite3BtreeLeave(db->aDb[0].pBt);
174001 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
174006 db->aDb[0].zDbSName = "main";
174007 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
174008 db->aDb[1].zDbSName = "temp";
174009 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
174011 db->eOpenState = SQLITE_STATE_OPEN;
174012 if( db->mallocFailed ){
174016 /* Register all built-in functions, but do not attempt to read the
174025 /* Load compiled-in extensions */
174030 /* Load automatic extensions - extensions that have been registered
174042 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
174045 db->mDbFlags |= DBFLAG_InternalFunc;
174048 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
174049 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
174053 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
174054 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
174060 /* Enable the lookaside-malloc subsystem */
174068 assert( db->mutex!=0 || isThreadsafe==0
174070 sqlite3_mutex_leave(db->mutex);
174078 db->eOpenState = SQLITE_STATE_SICK;
174104 const char *filename, /* Database filename (UTF-8) */
174120 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
174134 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
174181 sqlite3_mutex_enter(db->mutex);
174182 assert( !db->mallocFailed );
174185 sqlite3_mutex_leave(db->mutex);
174206 sqlite3_mutex_enter(db->mutex);
174207 assert( !db->mallocFailed );
174208 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
174214 sqlite3_mutex_leave(db->mutex);
174231 sqlite3_mutex_enter(db->mutex);
174232 db->xCollNeeded = xCollNeeded;
174233 db->xCollNeeded16 = 0;
174234 db->pCollNeededArg = pCollNeededArg;
174235 sqlite3_mutex_leave(db->mutex);
174252 sqlite3_mutex_enter(db->mutex);
174253 db->xCollNeeded = 0;
174254 db->xCollNeeded16 = xCollNeeded16;
174255 db->pCollNeededArg = pCollNeededArg;
174256 sqlite3_mutex_leave(db->mutex);
174284 return db->autoCommit;
174296 ** a low-level error is first detected.
174336 ** This is a convenience routine that makes sure that all thread-specific
174339 ** SQLite no longer uses thread-specific data so this routine is now a
174340 ** no-op. It is retained for historical compatibility.
174359 int *pAutoinc /* OUTPUT: True if column is auto-increment */
174380 sqlite3_mutex_enter(db->mutex);
174398 for(iCol=0; iCol<pTab->nCol; iCol++){
174399 pCol = &pTab->aCol[iCol];
174400 if( 0==sqlite3StrICmp(pCol->zCnName, zColumnName) ){
174404 if( iCol==pTab->nCol ){
174406 iCol = pTab->iPKey;
174407 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
174428 notnull = pCol->notNull!=0;
174429 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
174430 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
174461 sqlite3_mutex_leave(db->mutex);
174488 sqlite3_mutex_enter(db->mutex);
174489 db->errMask = onoff ? 0xffffffff : 0xff;
174490 sqlite3_mutex_leave(db->mutex);
174504 sqlite3_mutex_enter(db->mutex);
174534 int nSave = db->busyHandler.nBusy;
174536 db->busyHandler.nBusy = nSave;
174540 sqlite3_mutex_leave(db->mutex);
174576 ** Control the seed for the pseudo-random number generator (PRNG) that
174591 ** This test-control also resets the PRNG so that the new seed will
174599 assert( db==0 || db->aDb[0].pSchema!=0 );
174600 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
174611 ** is an array of integers that defines the test. Return -1 on a
174612 ** memory allocation error, 0 on success, or non-zero for an error.
174637 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
174638 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
174690 ** This action provides a run-time test to see whether or not
174691 ** assert() was enabled at compile-time. If X is true and assert()
174700 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
174731 ** This action provides a run-time test to see how the ALWAYS and
174732 ** NEVER macros were defined at compile-time.
174737 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
174739 ** hard-coded to true or else it asserts if its argument is false.
174740 ** The first behavior (hard-coded to true) is the case if
174745 ** The run-time test procedure might look something like this:
174748 ** // ALWAYS() and NEVER() are no-op pass-through macros
174764 ** The integer returned reveals the byte-order of the computer on which
174767 ** 1 big-endian, determined at run-time
174768 ** 10 little-endian, determined at run-time
174769 ** 432101 big-endian, determined at compile-time
174770 ** 123410 little-endian, determined at compile-time
174788 db->dbOptFlags = va_arg(ap, u32);
174803 ** and return zero on success, or return non-zero on failure.
174823 db->mDbFlags ^= DBFLAG_InternalFunc;
174829 ** Set or clear a flag that indicates that the database file is always well-
174833 ** that demonstrat invariants on well-formed database files.
174846 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
174885 db->nMaxSorterMmap = va_arg(ap, int);
174904 ** or off. "tnum" is the root page of the b-tree to which the imposter
174918 sqlite3_mutex_enter(db->mutex);
174921 db->init.iDb = iDb;
174922 db->init.busy = db->init.imposterTable = va_arg(ap,int);
174923 db->init.newTnum = va_arg(ap,int);
174924 if( db->init.busy==0 && db->init.newTnum>0 ){
174928 sqlite3_mutex_leave(db->mutex);
174936 ** -DYYCOVERAGE) writes a report onto "out" that shows all
174950 ** This test-control causes the most recent sqlite3_result_int64() value
174954 ** test-control enables us to write an intreal() SQL function that can
174969 ** This test-control queries the seek-counter on the "main" database
174970 ** file. The seek-counter is written into *pnSeek and is then reset.
174971 ** The seek-count is only available if compiled with SQLITE_DEBUG.
174976 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
175028 ** of the id-th tuning parameter to *piValue. If "id" is between -1
175029 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
175038 ** checked-in code.
175045 }else if( id<0 && id>=-SQLITE_NTUNE ){
175046 *piValue = Tuning(-id);
175068 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
175069 zName--;
175120 assert( (sqlite3_int64)(p - pResult)==nByte );
175132 sqlite3_free(p - 4);
175160 while( ALWAYS(zFilename) && zFilename[0] && (N--)>0 ){
175177 ** Return a 64-bit integer value for a query parameter.
175198 ** passing free() a pointer that was not obtained from malloc() - it is
175231 return iDb<0 ? 0 : db->aDb[iDb].pBt;
175235 ** Return the name of the N-th database schema. Return NULL if N is out
175245 if( N<0 || N>=db->nDb ){
175248 return db->aDb[N].zDbSName;
175269 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
175277 return -1;
175281 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
175302 sqlite3_mutex_enter(db->mutex);
175304 if( db->autoCommit==0 ){
175307 Btree *pBt = db->aDb[iDb].pBt;
175317 sqlite3_mutex_leave(db->mutex);
175323 ** Open a read-transaction on the snapshot idendified by pSnapshot.
175338 sqlite3_mutex_enter(db->mutex);
175339 if( db->autoCommit==0 ){
175343 Btree *pBt = db->aDb[iDb].pBt;
175348 if( db->nVdbeActive==0 ){
175372 sqlite3_mutex_leave(db->mutex);
175392 sqlite3_mutex_enter(db->mutex);
175395 Btree *pBt = db->aDb[iDb].pBt;
175404 sqlite3_mutex_leave(db->mutex);
175419 ** Given the name of a compile-time option, return true if that option
175455 ** Return the N-th compile-time option string. If N is out of range,
175517 ** 1) Each entry in the list has a non-NULL value for either
175529 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
175534 assert( p->pUnlockConnection || p->pBlockingConnection );
175537 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
175538 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
175539 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
175540 assert( db==0 || p->pUnlockConnection!=db );
175541 assert( db==0 || p->pBlockingConnection!=db );
175551 ** db is not currently a part of the list, this function is a no-op.
175556 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
175558 *pp = (*pp)->pNextBlocked;
175573 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
175574 pp=&(*pp)->pNextBlocked
175576 db->pNextBlocked = *pp;
175598 ** Register an unlock-notify callback.
175603 ** cache. pOther is found by looking at db->pBlockingConnection.
175625 sqlite3_mutex_enter(db->mutex);
175630 db->pBlockingConnection = 0;
175631 db->pUnlockConnection = 0;
175632 db->xUnlockNotify = 0;
175633 db->pUnlockArg = 0;
175634 }else if( 0==db->pBlockingConnection ){
175643 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
175647 db->pUnlockConnection = db->pBlockingConnection;
175648 db->xUnlockNotify = xNotify;
175649 db->pUnlockArg = pArg;
175656 assert( !db->mallocFailed );
175658 sqlite3_mutex_leave(db->mutex);
175670 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
175673 db->pBlockingConnection = pBlocker;
175689 ** set to db, then invoke the configured unlock-notify callback and
175697 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
175707 /* This loop runs once for each entry in the blocked-connections list. */
175708 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
175712 if( p->pBlockingConnection==db ){
175713 p->pBlockingConnection = 0;
175717 if( p->pUnlockConnection==db ){
175718 assert( p->xUnlockNotify );
175719 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
175738 ** be passed to the unlock-notify callback is larger than the
175745 ** closed and the unlock-notify callbacks on blocked connections
175747 ** indefinitely for an unlock-notify callback that will never
175750 ** Instead, invoke the unlock-notify callback with the context
175753 ** requiring any dynamic allocation. This is sub-optimal because
175767 aArg[nArg++] = p->pUnlockArg;
175768 xUnlockNotify = p->xUnlockNotify;
175769 p->pUnlockConnection = 0;
175770 p->xUnlockNotify = 0;
175771 p->pUnlockArg = 0;
175775 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
175777 *pp = p->pNextBlocked;
175778 p->pNextBlocked = 0;
175780 pp = &p->pNextBlocked;
175818 ** This is an SQLite module implementing full-text search.
175831 /* The full-text index is stored in a series of b+tree (-like)
175840 ** The basic unit of encoding is a variable-length integer called a
175841 ** varint. We encode variable-length integers in little-endian order
175848 ** 7 bits - A
175849 ** 14 bits - BA
175850 ** 21 bits - BBA
175854 ** the encoding is not the same. SQLite varints are big-endian
175856 ** little-endian and can be up to 10 bytes in length (in theory).
175866 ** A doclist (document list) holds a docid-sorted list of hits for a
175872 ** FTS3 used to optionally store character offsets using a compile-time
175901 ** label: A B C D E F G H I J K
175905 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
175908 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
175910 ** (72-2) and then terminates with the 0 at K.
175912 ** A "position-list" is the list of positions for multiple columns for
175913 ** a single docid. A "column-list" is the set of positions for a single
175914 ** column. Hence, a position-list consists of one or more column-lists,
175915 ** a document record consists of a docid followed by a position-list and
175919 ** array of varint-encoded docids.
175934 ** (further terms are delta-encoded)
175980 ** (further terms are delta-encoded)
176010 ** The segment directory in table %_segdir stores meta-information for
176022 ** The meta-information in the segment directory is:
176023 ** level - segment level (see below)
176024 ** idx - index within level
176025 ** - (level,idx uniquely identify a segment)
176026 ** start_block - first leaf node
176027 ** leaves_end_block - last leaf node
176028 ** end_block - last block (including interior nodes)
176029 ** root - contents of root node
176058 ** a tiny bit slower (perhaps due to more overhead in merge-time
176074 ** costs), and infrequent and non-existent terms still seem to be fast
176077 ** TODO(shess) That said, it would be nice to have a better query-side
176085 ** Since we're using a segmented structure, with no docid-oriented
176150 ** Defines the interface to tokenizers used by fulltext-search. There
176208 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
176255 ** normalized version of the token (i.e. after any case-folding and/or
176269 ** nul-terminated. This should either be fixed, or pInput/nBytes
176321 ** This is the header file for the generic hash-table implementation
176323 ** hash table implementation for the full-text indexing module.
176334 ** The internals of this structure are intended to be opaque -- client
176354 ** structure. All elements are stored on a single doubly-linked list.
176369 ** (including the null-terminator, if any). Case
176410 #define fts3HashFirst(H) ((H)->first)
176411 #define fts3HashNext(E) ((E)->next)
176412 #define fts3HashData(E) ((E)->data)
176413 #define fts3HashKey(E) ((E)->pKey)
176414 #define fts3HashKeysize(E) ((E)->nKey)
176419 #define fts3HashCount(H) ((H)->count)
176458 ** a collision when building an amalgamation with built-in FTS3.
176479 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
176481 ** as one or more b+-trees in the %_segments and %_segdir tables.
176483 ** It is possible to determine which index a b+-tree belongs to based on the
176485 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
176491 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
176498 ** make it a no-op.
176505 ** Terminator values for position-lists and column-lists.
176507 #define POS_COLUMN (1) /* Column-list terminator */
176508 #define POS_END (0) /* Position-list terminator */
176549 typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
176550 typedef short int i16; /* 2-byte (or larger) signed integer */
176551 typedef unsigned int u32; /* 4-byte unsigned integer */
176552 typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
176553 typedef sqlite3_int64 i64; /* 8-byte signed integer */
176579 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
176650 ** time must share a common language-id (see the FTS4 langid= feature).
176653 ** A single FTS4 table may have multiple full-text indexes. For each index
176687 ** by special insert command 'test-no-incr-doclist'. */
176697 # define MergeCount(P) ((P)->nMergeCount)
176720 char *aDoclist; /* List of docids for full-text queries */
176739 ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
176753 #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
176756 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
176758 ** above. The upper 16-bits contain a combination of the following
176759 ** bits, used to describe extra constraints on full-text searches.
176779 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
176792 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
176838 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
176906 #define FTS3_SEGCURSOR_PENDING -1
176907 #define FTS3_SEGCURSOR_ALL -2
176936 int nAdvance; /* How many seg-readers to advance */
177069 ** assert() conditions in the fts3 code are activated - conditions that are
177077 ** Write a 64-bit variable-length integer to memory starting at p[0].
177088 q[-1] &= 0x7f; /* turn off high bit in final byte */
177089 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
177090 return (int) (q - (unsigned char *)p);
177119 return (int)(p - pStart);
177123 ** Read a 64-bit variable-length integer from memory starting at p[0].
177132 ** Read a 64-bit variable-length integer from memory starting at p[0] and
177133 ** not extending past pEnd[-1].
177154 return (int)(p - pStart);
177159 ** a non-negative 32-bit integer before it is returned.
177195 ** Convert an SQL-style quoted string into a normal string by removing
177196 ** the quote characters. The conversion is done in-place. If the
177198 ** is a no-op.
177216 /* If the first byte was a '[', then the close-quote character is a ']' */
177245 ** varint that is part of a doclist (or position-list, or any other list
177262 ** clear on character p[-1]. */
177263 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
177278 assert( p->nPendingData==0 );
177279 assert( p->pSegments==0 );
177282 sqlite3_finalize(p->pSeekStmt);
177283 for(i=0; i<SizeofArray(p->aStmt); i++){
177284 sqlite3_finalize(p->aStmt[i]);
177286 sqlite3_free(p->zSegmentsTbl);
177287 sqlite3_free(p->zReadExprlist);
177288 sqlite3_free(p->zWriteExprlist);
177289 sqlite3_free(p->zContentTbl);
177290 sqlite3_free(p->zLanguageid);
177293 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
177315 ** If *pRc is initially non-zero then this routine is a no-op.
177343 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
177344 sqlite3 *db = p->db; /* Database handle */
177353 zDb, p->zName,
177354 zDb, p->zName,
177355 zDb, p->zName,
177356 zDb, p->zName,
177357 (p->zContentTbl ? "--" : ""), zDb,p->zName
177373 ** If *pRc is non-zero when this function is called, it is a no-op.
177385 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
177386 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
177389 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
177390 for(i=1; zCols && i<p->nColumn; i++){
177391 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
177397 zCols, p->zName, zLanguageid
177402 rc = sqlite3_declare_vtab(p->db, zSql);
177415 fts3DbExec(pRc, p->db,
177418 p->zDb, p->zName
177420 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
177428 ** If the p->bHasDocsize boolean is true (indicating that this is an
177435 sqlite3 *db = p->db; /* The database connection */
177437 if( p->zContentTbl==0 ){
177438 const char *zLanguageid = p->zLanguageid;
177443 for(i=0; zContentCols && i<p->nColumn; i++){
177444 char *z = p->azColumn[i];
177455 p->zDb, p->zName, zContentCols
177463 p->zDb, p->zName
177475 p->zDb, p->zName
177477 if( p->bHasDocsize ){
177480 p->zDb, p->zName
177483 assert( p->bHasStat==p->bFts4 );
177484 if( p->bHasStat ){
177491 ** Store the current database page-size in bytes in p->nPgsz.
177493 ** If *pRc is non-zero when this function is called, it is a no-op.
177503 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
177507 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
177510 p->nPgsz = sqlite3_column_int(pStmt, 0);
177513 p->nPgsz = 1024;
177517 assert( p->nPgsz>0 || rc!=SQLITE_OK );
177544 *pnKey = (int)(zCsr-z);
177580 ** Return a copy of input string zInput enclosed in double-quotes (") and
177583 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
177618 ** table has the three user-defined columns "a", "b", and "c", the following
177626 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177637 if( p->zContentTbl==0 ){
177644 for(i=0; i<p->nColumn; i++){
177645 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
177647 if( p->zLanguageid ){
177653 for(i=0; i<p->nColumn; i++){
177654 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
177656 if( p->zLanguageid ){
177657 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
177661 p->zDb,
177662 (p->zContentTbl ? p->zContentTbl : p->zName),
177663 (p->zContentTbl ? "" : "_content")
177671 ** user-defined text column).
177676 ** user-defined text columns, the following string is returned:
177683 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177700 for(i=0; i<p->nColumn; i++){
177703 if( p->zLanguageid ){
177711 ** Buffer z contains a positive integer value encoded as utf-8 text.
177719 iVal = iVal*10 + (z[i] - '0');
177720 if( iVal>0x7FFFFFFF ) return -1;
177727 ** This function interprets the string at (*pp) as a non-negative integer
177803 nIndex--;
177804 i--;
177829 ** of each columns name, including the nul-terminator.
177858 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
177873 ** nul-terminator byte. */
177911 ** argv[0] -> module name ("fts3" or "fts4")
177912 ** argv[1] -> database name
177913 ** argv[2] -> table name
177914 ** argv[...] -> "column name" and other module argument fields.
177925 Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
177962 nByte = sizeof(const char *) * (argc-2);
178007 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
178008 { "prefix", 6 }, /* 1 -> PREFIX */
178009 { "compress", 8 }, /* 2 -> COMPRESS */
178010 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
178011 { "order", 5 }, /* 4 -> ORDER */
178012 { "content", 7 }, /* 5 -> CONTENT */
178013 { "languageid", 10 }, /* 6 -> LANGUAGEID */
178014 { "notindexed", 10 } /* 7 -> NOTINDEXED */
178023 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
178124 nCol--;
178167 p->db = db;
178168 p->nColumn = nCol;
178169 p->nPendingData = 0;
178170 p->azColumn = (char **)&p[1];
178171 p->pTokenizer = pTokenizer;
178172 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
178173 p->bHasDocsize = (isFts4 && bNoDocsize==0);
178174 p->bHasStat = (u8)isFts4;
178175 p->bFts4 = (u8)isFts4;
178176 p->bDescIdx = (u8)bDescIdx;
178177 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
178178 p->zContentTbl = zContent;
178179 p->zLanguageid = zLanguageid;
178182 TESTONLY( p->inTransaction = -1 );
178183 TESTONLY( p->mxSavepoint = -1 );
178185 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
178186 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
178187 p->nIndex = nIndex;
178189 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
178191 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
178194 zCsr = (char *)&p->abNotindexed[nCol];
178195 p->zName = zCsr;
178198 p->zDb = zCsr;
178212 p->azColumn[iCol] = zCsr;
178219 int n = (int)strlen(p->azColumn[iCol]);
178223 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
178225 p->abNotindexed[iCol] = 1;
178243 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
178244 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
178258 p->bHasStat = 2;
178261 /* Figure out the page-size for the database. This is required in order to
178264 p->nNodeSize = p->nPgsz-35;
178267 p->nMergeCount = FTS3_MERGE_COUNT;
178287 pTokenizer->pModule->xDestroy(pTokenizer);
178290 assert( p->pSegments==0 );
178291 *ppVTab = &p->base;
178322 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
178324 ** support estimatedRows. In that case this function is a no-op.
178329 pIdxInfo->estimatedRows = nRow;
178335 ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
178337 ** support index-info flags. In that case this function is a no-op.
178342 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
178352 ** 2. Full-text search using a MATCH operator on a non-docid column.
178358 int iCons = -1; /* Index of constraint to use */
178360 int iLangidCons = -1; /* Index of langid=x constraint, if present */
178361 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
178362 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
178365 if( p->bLock ){
178373 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
178374 pInfo->estimatedCost = 5000000;
178375 for(i=0; i<pInfo->nConstraint; i++){
178377 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
178378 if( pCons->usable==0 ){
178379 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
178385 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
178386 pInfo->estimatedCost = 1e50;
178393 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
178396 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
178397 pInfo->idxNum = FTS3_DOCID_SEARCH;
178398 pInfo->estimatedCost = 1.0;
178402 /* A MATCH constraint. Use a full-text search.
178411 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
178412 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
178414 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
178415 pInfo->estimatedCost = 2.0;
178420 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
178421 && pCons->iColumn==p->nColumn + 2
178427 switch( pCons->op ){
178442 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
178446 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
178447 pInfo->aConstraintUsage[iCons].omit = 1;
178450 pInfo->idxNum |= FTS3_HAVE_LANGID;
178451 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
178454 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
178455 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
178458 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
178459 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
178465 if( pInfo->nOrderBy==1 ){
178466 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
178467 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
178468 if( pOrder->desc ){
178469 pInfo->idxStr = "DESC";
178471 pInfo->idxStr = "ASC";
178473 pInfo->orderByConsumed = 1;
178477 assert( p->pSegments==0 );
178502 ** Finalize the statement handle at pCsr->pStmt.
178509 if( pCsr->bSeekStmt ){
178510 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
178511 if( p->pSeekStmt==0 ){
178512 p->pSeekStmt = pCsr->pStmt;
178513 sqlite3_reset(pCsr->pStmt);
178514 pCsr->pStmt = 0;