Lines Matching +full:stale +full:- +full:pr +full:- +full:message
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,
692 ** is not NULL then any error message is written into memory obtained
695 ** on error message strings returned through the 5th parameter of
696 ** sqlite3_exec() after the error message string is no longer needed.
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.
1315 ** the error message if the pragma fails. ^If the
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
2256 ** log message after formatting via [sqlite3_snprintf()].
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
3347 ** the authorizer will fail with an error message.
3352 ** authorizer will fail with an error message explaining that
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.
4145 ** ^(Memory to hold the error message string is managed internally.
4150 ** ^The sqlite3_errstr() interface returns the English-language text
4151 ** that describes the [result code], as UTF-8.
4152 ** ^(Memory to hold the error message string is managed internally
4160 ** SQL, then the sqlite3_error_offset() function returns -1.
4174 ** error code and message may or may not be set.
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
6093 ** as the text of an error message. ^SQLite interprets the error
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()].
6099 ** message all text up through the first zero character.
6101 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6102 ** bytes (not characters) from the 2nd parameter as the error message.
6104 ** routines make a private copy of the error message text before
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
7143 ** fill *pzErrMsg with error message text stored in memory
7166 char **pzErrMsg /* Put error message here if not 0 */
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.)^
7218 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
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
7598 ** ^Virtual tables methods can set an error message by assigning a
7601 ** prior to assigning a new string to zErrMsg. ^After the error message
7608 char *zErrMsg; /* Error message from sqlite3_mprintf() */
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.
7733 ** [database connection] error code and message accessible via
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()] -
7801 ** ^This function sets the database handle error code and message.
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
7884 ** [database connection] error code and message accessible via
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
9167 ** returned and an error code and error message are stored in the
9169 ** ^The error code and message for the failed call to sqlite3_backup_init()
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
9491 ** ^The [sqlite3_log()] interface writes a message into the [error log]
9504 ** will not use dynamically allocated memory. The log message is stored in
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
10264 ** ^This function does not set the database handle error code or message
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
11270 ** message. It is the responsibility of the caller to free this buffer using
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
11907 ** changes associated with the table are applied. A warning message is issued
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 */
15510 /* Error message codes for OP_Halt */
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 */
16921 sqlite3_value *pErr; /* Most recent error message */
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
18894 char *zErrMsg; /* An error message */
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)
19218 char **pzErrMsg; /* Error message stored here */
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
19423 const char *zCteErr; /* Error message for circular references */
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
22624 char *zErrMsg; /* Error message written here */
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
23914 ** is in a system call (i.e. localtime()), then an error message is written
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.
30944 ** This is the routine that actually formats the sqlite3_log() message.
30946 ** stack space on small-stack systems when logging is disabled.
30959 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
30968 ** Format and write a message to the log if logging is enabled.
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);
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);
33382 ** Set the current error code to err_code and clear any prior error message.
33388 db->errCode = err_code;
33389 if( err_code || db->pErr ){
33392 db->errByteOffset = -1;
33398 ** and error message.
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.
35625 ** If we are to be thread-safe, include the pthreads header.
35646 ** Maximum supported path-length.
35668 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
35697 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
35724 ** transaction counter in bytes 24-27 of database files are updated
35789 ** The threadid macro resolves to the thread-id or to 0. Used for
35810 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
35811 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
35819 ** Linux-specific IOCTL magic numbers used for controlling F2FS
35837 ** which always has the same well-defined interface.
35848 ** Many system calls are accessed through pointer-to-functions so that
35877 ** DJGPP. But it is DOS - what did you expect?
36023 ** On some systems, calls to fchown() will trigger a message in a security
36024 ** log if they come from non-root processes. So avoid calling fchown() if
36104 int i = -1;
36108 for(i=0; i<ArraySize(aSyscall)-1; i++){
36136 ** The m parameter will be non-zero only when creating -wal, -journal,
36137 ** and -shm files. We want those files to have *exactly* the same
36139 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
36161 fd = -1;
36232 ** integer lock-type.
36251 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
36252 ** command-line option on the compiler. This code is normally
36268 if( p->l_type==F_RDLCK ){
36270 }else if( p->l_type==F_WRLCK ){
36272 }else if( p->l_type==F_UNLCK ){
36277 assert( p->l_whence==SEEK_SET );
36281 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
36282 (int)p->l_pid, s);
36283 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
36296 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
36316 /* On Android, ftruncate() always uses 32-bit offsets, even if
36377 ** a performance-critical path, so it is sufficient to put these
36402 ** Changes are made in-place. Return the new name length.
36404 ** The original filename is in z[0..n-1]. Return the number of
36409 while( n>1 && z[n-1]=='/' ){ n--; }
36418 while( j>0 && z[j-1]!='/' ){ j--; }
36419 if( j>0 ){ j--; }
36450 pNew->zCanonicalName = (char*)&pNew[1];
36451 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
36452 n = vxworksSimplifyName(pNew->zCanonicalName, n);
36459 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
36460 if( pCandidate->nName==n
36461 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
36464 pCandidate->nRef++;
36471 pNew->nRef = 1;
36472 pNew->nName = n;
36473 pNew->pNext = vxworksFileList;
36485 assert( pId->nRef>0 );
36486 pId->nRef--;
36487 if( pId->nRef==0 ){
36489 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
36491 *pp = pId->pNext;
36549 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
36577 ** on linux - with NPTL a lock created by thread A can override locks
36578 ** in thread B. But there is no way to know at compile-time which
36580 ** compile-time whether or not thread A can override locks on thread B.
36581 ** One has to do a run-time check to discover the behavior of the
36601 ** sizes ino_t at only 32-bits instead of 64-bits. (See
36602 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
36603 ** To work around this, always allocate 64-bits for the inode number.
36604 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
36671 assert( pFile->pInode );
36672 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
36675 assert( pFile->pInode );
36676 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
36682 ** This function - unixLogErrorAtLine(), is only ever called via the macro
36686 ** set. It logs a message using sqlite3_log() containing the current value of
36687 ** errno and, if possible, the human-readable equivalent from strerror() or
36693 ** failed (e.g. "unlink", "open") and the associated file-system path,
36703 char *zErr; /* Message from strerror() or equivalent */
36707 ** the strerror() function to obtain the human-readable error message
36717 ** returns a pointer to a buffer containing the error message. That pointer
36720 ** strerror_r(), which always writes an error message into aErr[].
36723 ** available, the error message will often be an empty string. Not a
36730 strerror_r(iErrno, aErr, sizeof(aErr)-1);
36736 /* Non-threadsafe build, use strerror(). */
36742 "os_unix.c:%d: (%d) %s(%s) - %s",
36771 pFile ? pFile->zPath : 0, lineno);
36776 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
36780 pFile->lastErrno = error;
36784 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
36787 unixInodeInfo *pInode = pFile->pInode;
36791 for(p=pInode->pUnused; p; p=pNext){
36792 pNext = p->pNext;
36793 robust_close(pFile, p->fd, __LINE__);
36796 pInode->pUnused = 0;
36806 unixInodeInfo *pInode = pFile->pInode;
36810 pInode->nRef--;
36811 if( pInode->nRef==0 ){
36812 assert( pInode->pShmNode==0 );
36813 sqlite3_mutex_enter(pInode->pLockMutex);
36815 sqlite3_mutex_leave(pInode->pLockMutex);
36816 if( pInode->pPrev ){
36817 assert( pInode->pPrev->pNext==pInode );
36818 pInode->pPrev->pNext = pInode->pNext;
36821 inodeList = pInode->pNext;
36823 if( pInode->pNext ){
36824 assert( pInode->pNext->pPrev==pInode );
36825 pInode->pNext->pPrev = pInode->pPrev;
36827 sqlite3_mutex_free(pInode->pLockMutex);
36849 struct stat statbuf; /* Low-level file information */
36854 /* Get low-level information about the file that we can used to
36857 fd = pFile->h;
36862 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
36869 ** incorrectly for zero-size files. See ticket #3260. To work
36878 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
36895 fileId.pId = pFile->pId;
36901 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
36902 pInode = pInode->pNext;
36910 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
36912 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
36913 if( pInode->pLockMutex==0 ){
36918 pInode->nRef = 1;
36920 pInode->pNext = inodeList;
36921 pInode->pPrev = 0;
36922 if( inodeList ) inodeList->pPrev = pInode;
36925 pInode->nRef++;
36936 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
36939 return pFile->pInode!=0 &&
36940 (osStat(pFile->zPath, &buf)!=0
36941 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
36960 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
36962 rc = osFstat(pFile->h, &buf);
36964 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
36968 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
36972 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
36976 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
36985 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36996 assert( pFile->eFileLock<=SHARED_LOCK );
36997 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
37000 if( pFile->pInode->eFileLock>SHARED_LOCK ){
37007 if( !reserved && !pFile->pInode->bProcessLock ){
37013 if( osFcntl(pFile->h, F_GETLK, &lock) ){
37022 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
37023 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
37033 ** Set a posix-advisory-lock.
37037 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
37042 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
37053 int tm = pFile->iBusyTimeout;
37063 tm--;
37071 ** Attempt to set a system-lock on the file pFile. The lock is
37074 ** If the pFile was opened read/write from unix-excl, then the only lock
37077 ** operations become no-ops. Locking operations still happen internally,
37082 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
37083 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
37084 ** and is read-only.
37086 ** Zero is returned if the call completes successfully, or -1 if a call
37091 unixInodeInfo *pInode = pFile->pInode;
37093 assert( sqlite3_mutex_held(pInode->pLockMutex) );
37094 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
37095 if( pInode->bProcessLock==0 ){
37097 assert( pInode->nLock==0 );
37102 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
37104 pInode->bProcessLock = 1;
37105 pInode->nLock++;
37110 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
37116 ** Lock the file with the lock specified by parameter eFileLock - one
37130 ** UNLOCKED -> SHARED
37131 ** SHARED -> RESERVED
37132 ** SHARED -> (PENDING) -> EXCLUSIVE
37133 ** RESERVED -> (PENDING) -> EXCLUSIVE
37134 ** PENDING -> EXCLUSIVE
37142 ** lock primitives (called read-locks and write-locks below, to avoid
37151 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
37152 ** byte'. If this is successful, 'shared byte range' is read-locked
37155 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
37157 ** Windows95 is now pretty much extinct, but this work-around for the
37158 ** lack of shared-locks on Windows95 lives on, for backwards
37162 ** A RESERVED lock is implemented by grabbing a write-lock on the
37166 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
37174 ** implemented by obtaining a write-lock on the entire 'shared byte
37175 ** range'. Since all other locks require a read-lock on one of the bytes
37186 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
37187 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
37188 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
37195 if( pFile->eFileLock>=eFileLock ){
37196 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
37206 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
37208 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
37210 /* This mutex is needed because pFile->pInode is shared across threads
37212 pInode = pFile->pInode;
37213 sqlite3_mutex_enter(pInode->pLockMutex);
37218 if( (pFile->eFileLock!=pInode->eFileLock &&
37219 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
37230 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
37232 assert( pFile->eFileLock==0 );
37233 assert( pInode->nShared>0 );
37234 pFile->eFileLock = SHARED_LOCK;
37235 pInode->nShared++;
37236 pInode->nLock++;
37248 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
37267 assert( pInode->nShared==0 );
37268 assert( pInode->eFileLock==0 );
37271 /* Now get the read-lock */
37295 pFile->eFileLock = SHARED_LOCK;
37296 pInode->nLock++;
37297 pInode->nShared = 1;
37299 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
37308 assert( 0!=pFile->eFileLock );
37331 /* Set up the transaction-counter change checking flags when
37337 && pFile->eFileLock<=SHARED_LOCK
37340 pFile->transCntrChng = 0;
37341 pFile->dbUpdate = 0;
37342 pFile->inNormalWrite = 1;
37348 pFile->eFileLock = eFileLock;
37349 pInode->eFileLock = eFileLock;
37351 pFile->eFileLock = PENDING_LOCK;
37352 pInode->eFileLock = PENDING_LOCK;
37356 sqlite3_mutex_leave(pInode->pLockMutex);
37357 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
37367 unixInodeInfo *pInode = pFile->pInode;
37368 UnixUnusedFd *p = pFile->pPreallocatedUnused;
37370 p->pNext = pInode->pUnused;
37371 pInode->pUnused = p;
37372 pFile->h = -1;
37373 pFile->pPreallocatedUnused = 0;
37381 ** the requested locking level, this routine is a no-op.
37396 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
37397 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
37401 if( pFile->eFileLock<=eFileLock ){
37404 pInode = pFile->pInode;
37405 sqlite3_mutex_enter(pInode->pLockMutex);
37406 assert( pInode->nShared!=0 );
37407 if( pFile->eFileLock>SHARED_LOCK ){
37408 assert( pInode->eFileLock==pFile->eFileLock );
37417 ** cache. The use of a stale cache can lead to database corruption.
37419 pFile->inNormalWrite = 0;
37423 ** before establishing the readlock - to avoid a race condition we downgrade
37439 off_t divSize = SHARED_SIZE - 1;
37445 if( unixFileLock(pFile, &lock)==(-1) ){
37455 if( unixFileLock(pFile, &lock)==(-1) ){
37466 lock.l_len = SHARED_SIZE-divSize;
37467 if( unixFileLock(pFile, &lock)==(-1) ){
37498 pInode->eFileLock = SHARED_LOCK;
37510 pInode->nShared--;
37511 if( pInode->nShared==0 ){
37516 pInode->eFileLock = NO_LOCK;
37520 pInode->eFileLock = NO_LOCK;
37521 pFile->eFileLock = NO_LOCK;
37529 pInode->nLock--;
37530 assert( pInode->nLock>=0 );
37531 if( pInode->nLock==0 ) closePendingFds(pFile);
37535 sqlite3_mutex_leave(pInode->pLockMutex);
37537 pFile->eFileLock = eFileLock;
37547 ** the requested locking level, this routine is a no-op.
37551 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
37576 if( pFile->h>=0 ){
37577 robust_close(pFile, pFile->h, __LINE__);
37578 pFile->h = -1;
37581 if( pFile->pId ){
37582 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
37583 osUnlink(pFile->pId->zCanonicalName);
37585 vxworksReleaseFileId(pFile->pId);
37586 pFile->pId = 0;
37590 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
37591 osUnlink(pFile->zPath);
37592 sqlite3_free(*(char**)&pFile->zPath);
37593 pFile->zPath = 0;
37596 OSTRACE(("CLOSE %-3d\n", pFile->h));
37597 OpenCounter(-1);
37598 sqlite3_free(pFile->pPreallocatedUnused);
37609 unixInodeInfo *pInode = pFile->pInode;
37620 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
37621 sqlite3_mutex_enter(pInode->pLockMutex);
37622 if( pInode->nLock ){
37625 ** descriptor to pInode->pUnused list. It will be automatically closed
37630 sqlite3_mutex_leave(pInode->pLockMutex);
37632 assert( pFile->pShm==0 );
37642 ****************************** No-op Locking **********************************
37648 ** This locking mode is appropriate for use on read-only databases
37649 ** (ex: databases that are burned into CD-ROM, for example.) It can
37679 /******************* End of the no-op lock implementation *********************
37683 ************************* Begin dot-file Locking ******************************
37692 ** (2) An application crash or power loss can leave stale lock files
37713 ** to a non-zero value otherwise *pResOut is set to zero. The return value
37728 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
37729 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
37735 ** Lock the file with the lock specified by parameter eFileLock - one
37749 ** UNLOCKED -> SHARED
37750 ** SHARED -> RESERVED
37751 ** SHARED -> (PENDING) -> EXCLUSIVE
37752 ** RESERVED -> (PENDING) -> EXCLUSIVE
37753 ** PENDING -> EXCLUSIVE
37763 char *zLockFile = (char *)pFile->lockingContext;
37770 if( pFile->eFileLock > NO_LOCK ){
37771 pFile->eFileLock = eFileLock;
37798 pFile->eFileLock = eFileLock;
37807 ** the requested locking level, this routine is a no-op.
37813 char *zLockFile = (char *)pFile->lockingContext;
37817 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
37818 pFile->eFileLock, osGetpid(0)));
37821 /* no-op if possible */
37822 if( pFile->eFileLock==eFileLock ){
37830 pFile->eFileLock = SHARED_LOCK;
37847 pFile->eFileLock = NO_LOCK;
37858 sqlite3_free(pFile->lockingContext);
37861 /****************** End of the dot-file lock implementation *******************
37869 ** flock() locking is like dot-file locking in that the various
37870 ** fine-grain locking levels supported by SQLite are collapsed into
37897 ** to a non-zero value otherwise *pResOut is set to zero. The return value
37910 if( pFile->eFileLock>SHARED_LOCK ){
37917 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
37920 lrc = robust_flock(pFile->h, LOCK_UN);
37939 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
37952 ** Lock the file with the lock specified by parameter eFileLock - one
37966 ** UNLOCKED -> SHARED
37967 ** SHARED -> RESERVED
37968 ** SHARED -> (PENDING) -> EXCLUSIVE
37969 ** RESERVED -> (PENDING) -> EXCLUSIVE
37970 ** PENDING -> EXCLUSIVE
37988 if (pFile->eFileLock > NO_LOCK) {
37989 pFile->eFileLock = eFileLock;
37995 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
38004 pFile->eFileLock = eFileLock;
38006 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
38022 ** the requested locking level, this routine is a no-op.
38028 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
38029 pFile->eFileLock, osGetpid(0)));
38032 /* no-op if possible */
38033 if( pFile->eFileLock==eFileLock ){
38039 pFile->eFileLock = eFileLock;
38044 if( robust_flock(pFile->h, LOCK_UN) ){
38050 pFile->eFileLock = NO_LOCK;
38074 ** Semaphore locking is like dot-lock and flock in that it really only
38084 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38097 if( pFile->eFileLock>SHARED_LOCK ){
38103 sem_t *pSem = pFile->pInode->pSem;
38105 if( sem_trywait(pSem)==-1 ){
38112 reserved = (pFile->eFileLock < SHARED_LOCK);
38119 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
38126 ** Lock the file with the lock specified by parameter eFileLock - one
38140 ** UNLOCKED -> SHARED
38141 ** SHARED -> RESERVED
38142 ** SHARED -> (PENDING) -> EXCLUSIVE
38143 ** RESERVED -> (PENDING) -> EXCLUSIVE
38144 ** PENDING -> EXCLUSIVE
38156 sem_t *pSem = pFile->pInode->pSem;
38161 if (pFile->eFileLock > NO_LOCK) {
38162 pFile->eFileLock = eFileLock;
38168 if( sem_trywait(pSem)==-1 ){
38174 pFile->eFileLock = eFileLock;
38185 ** the requested locking level, this routine is a no-op.
38189 sem_t *pSem = pFile->pInode->pSem;
38193 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
38194 pFile->eFileLock, osGetpid(0)));
38197 /* no-op if possible */
38198 if( pFile->eFileLock==eFileLock ){
38204 pFile->eFileLock = eFileLock;
38209 if ( sem_post(pSem)==-1 ) {
38217 pFile->eFileLock = NO_LOCK;
38250 ** on Apple Macintosh computers - both OS9 and OSX.
38252 ** Third-party implementations of AFP are available. But this code here
38279 ** This is a utility for setting or clearing a bit-range lock on an
38298 pb.fd = pFile->h;
38301 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
38304 if ( err==-1 ) {
38327 ** to a non-zero value otherwise *pResOut is set to zero. The return value
38339 context = (afpLockingContext *) pFile->lockingContext;
38340 if( context->reserved ){
38344 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
38346 if( pFile->pInode->eFileLock>SHARED_LOCK ){
38354 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
38358 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
38368 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
38369 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
38376 ** Lock the file with the lock specified by parameter eFileLock - one
38390 ** UNLOCKED -> SHARED
38391 ** SHARED -> RESERVED
38392 ** SHARED -> (PENDING) -> EXCLUSIVE
38393 ** RESERVED -> (PENDING) -> EXCLUSIVE
38394 ** PENDING -> EXCLUSIVE
38402 unixInodeInfo *pInode = pFile->pInode;
38403 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38406 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
38407 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
38408 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
38414 if( pFile->eFileLock>=eFileLock ){
38415 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
38425 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
38427 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
38429 /* This mutex is needed because pFile->pInode is shared across threads
38431 pInode = pFile->pInode;
38432 sqlite3_mutex_enter(pInode->pLockMutex);
38437 if( (pFile->eFileLock!=pInode->eFileLock &&
38438 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
38449 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
38451 assert( pFile->eFileLock==0 );
38452 assert( pInode->nShared>0 );
38453 pFile->eFileLock = SHARED_LOCK;
38454 pInode->nShared++;
38455 pInode->nLock++;
38464 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
38467 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
38481 assert( pInode->nShared==0 );
38482 assert( pInode->eFileLock==0 );
38485 /* Now get the read-lock SHARED_LOCK */
38488 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
38489 lrc1 = afpSetLock(context->dbPath, pFile,
38490 SHARED_FIRST+pInode->sharedByte, 1, 1);
38492 lrc1Errno = pFile->lastErrno;
38495 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
38507 pFile->eFileLock = SHARED_LOCK;
38508 pInode->nLock++;
38509 pInode->nShared = 1;
38511 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
38521 assert( 0!=pFile->eFileLock );
38522 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
38524 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
38526 context->reserved = 1;
38535 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
38536 pInode->sharedByte, 1, 0)) ){
38539 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
38541 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
38542 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
38560 pFile->eFileLock = eFileLock;
38561 pInode->eFileLock = eFileLock;
38563 pFile->eFileLock = PENDING_LOCK;
38564 pInode->eFileLock = PENDING_LOCK;
38568 sqlite3_mutex_leave(pInode->pLockMutex);
38569 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
38579 ** the requested locking level, this routine is a no-op.
38585 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
38588 int h = pFile->h;
38592 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
38593 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
38597 if( pFile->eFileLock<=eFileLock ){
38600 pInode = pFile->pInode;
38601 sqlite3_mutex_enter(pInode->pLockMutex);
38602 assert( pInode->nShared!=0 );
38603 if( pFile->eFileLock>SHARED_LOCK ){
38604 assert( pInode->eFileLock==pFile->eFileLock );
38606 SimulateIOError( h=(-1) )
38616 ** cache. The use of a stale cache can lead to database corruption.
38618 assert( pFile->inNormalWrite==0
38619 || pFile->dbUpdate==0
38620 || pFile->transCntrChng==1 );
38621 pFile->inNormalWrite = 0;
38624 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
38625 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
38626 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
38627 /* only re-establish the shared lock if necessary */
38628 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
38629 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
38634 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
38635 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
38637 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
38638 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
38640 context->reserved = 0;
38643 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
38644 pInode->eFileLock = SHARED_LOCK;
38653 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
38654 pInode->nShared--;
38655 if( pInode->nShared==0 ){
38657 SimulateIOError( h=(-1) )
38660 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
38663 pInode->eFileLock = NO_LOCK;
38664 pFile->eFileLock = NO_LOCK;
38668 pInode->nLock--;
38669 assert( pInode->nLock>=0 );
38670 if( pInode->nLock==0 ) closePendingFds(pFile);
38674 sqlite3_mutex_leave(pInode->pLockMutex);
38676 pFile->eFileLock = eFileLock;
38691 if( pFile->pInode ){
38692 unixInodeInfo *pInode = pFile->pInode;
38693 sqlite3_mutex_enter(pInode->pLockMutex);
38694 if( pInode->nLock ){
38697 ** descriptor to pInode->aPending. It will be automatically closed when
38702 sqlite3_mutex_leave(pInode->pLockMutex);
38705 sqlite3_free(pFile->lockingContext);
38715 ** is available. If you don't compile for a mac, then the "unix-afp"
38730 ** the requested locking level, this routine is a no-op.
38746 **************** Non-locking sqlite3_file methods *****************************
38776 assert( id->h>2 );
38779 got = osPread(id->h, pBuf, cnt, offset);
38780 SimulateIOError( got = -1 );
38782 got = osPread64(id->h, pBuf, cnt, offset);
38783 SimulateIOError( got = -1 );
38785 newOffset = lseek(id->h, offset, SEEK_SET);
38786 SimulateIOError( newOffset = -1 );
38789 return -1;
38791 got = osRead(id->h, pBuf, cnt);
38800 cnt -= got;
38807 OSTRACE(("READ %-3d %5d %7lld %llu\n",
38808 id->h, got+prior, offset-prior, TIMER_ELAPSED));
38829 /* If this is a database file (not a journal, super-journal or temp
38832 assert( pFile->pPreallocatedUnused==0
38841 if( offset<pFile->mmapSize ){
38842 if( offset+amt <= pFile->mmapSize ){
38843 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
38846 int nCopy = pFile->mmapSize - offset;
38847 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
38849 amt -= nCopy;
38859 /* pFile->lastErrno has been set by seekAndRead().
38866 switch( pFile->lastErrno ){
38880 /* Unread parts of the buffer must be zero-filled */
38881 memset(&((char*)pBuf)[got], 0, amt-got);
38887 ** Attempt to seek the file-descriptor passed as the first argument to
38889 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
38915 SimulateIOError( iSeek = -1 );
38917 rc = -1;
38925 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
38933 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
38940 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
38959 /* If this is a database file (not a journal, super-journal or temp
38962 assert( pFile->pPreallocatedUnused==0
38970 ** doing a hot-journal rollback or a write to some file other than a
38975 if( pFile->inNormalWrite ){
38976 pFile->dbUpdate = 1; /* The database has been modified */
38983 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
38984 pFile->transCntrChng = 1; /* The transaction counter has changed */
38993 if( offset<pFile->mmapSize ){
38994 if( offset+amt <= pFile->mmapSize ){
38995 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
38998 int nCopy = pFile->mmapSize - offset;
38999 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
39001 amt -= nCopy;
39008 amt -= wrote;
39012 SimulateIOError(( wrote=(-1), amt=1 ));
39016 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
39041 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
39111 ** no-op. But go ahead and call fstat() to validate the file
39144 if( rc==-1 && errno==ENOTSUP ){
39150 if( OS_VXWORKS && rc!= -1 ){
39163 ** The directory file descriptor is used for only one thing - to
39172 ** replace this routine with a harmless no-op. To make this routine
39173 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
39181 int fd = -1;
39185 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
39194 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
39209 ** has been created by fsync-ing the directory that contains the file.
39214 ** will not roll back - possibly leading to database corruption.
39234 OSTRACE(("SYNC %-3d\n", pFile->h));
39235 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
39239 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
39243 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
39246 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
39248 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
39250 rc = osOpenDirectory(pFile->zPath, &dirfd);
39258 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
39272 /* If the user has configured a chunk-size for this file, truncate the
39277 if( pFile->szChunk>0 ){
39278 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
39281 rc = robust_ftruncate(pFile->h, nByte);
39284 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
39288 ** doing a hot-journal rollback or a write to some file other than a
39291 ** when restoring a database using the backup API from a zero-length
39294 if( pFile->inNormalWrite && nByte==0 ){
39295 pFile->transCntrChng = 1;
39304 if( nByte<pFile->mmapSize ){
39305 pFile->mmapSize = nByte;
39320 rc = osFstat(((unixFile*)id)->h, &buf);
39324 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", ((unixFile*)id)->zPath);
39328 /* When opening a zero-size database, the findInodeInfo() procedure
39330 ** in the OS-X msdos filesystem. In order to avoid problems with upper
39342 ** Handler for proxy-locking file-control verbs. Defined below in the
39350 ** file-control operation. Enlarge the database to nBytes in size
39351 ** (rounded up to the next chunk-size). If the database is already
39352 ** nBytes or larger, this routine is a no-op.
39355 if( pFile->szChunk>0 ){
39359 if( osFstat(pFile->h, &buf) ){
39360 return unixLogError(SQLITE_IOERR_FSTAT, "fstat", pFile->zPath);
39363 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
39372 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
39379 ** at offset (nSize-1), to set the size of the file correctly.
39383 int nBlk = buf.st_blksize; /* File-system block size */
39387 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
39390 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
39391 if( iWrite>=nSize ) iWrite = nSize - 1;
39400 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
39402 if( pFile->szChunk<=0 ){
39403 if( robust_ftruncate(pFile->h, nByte) ){
39405 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
39419 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
39421 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
39425 *pArg = (pFile->ctrlFlags & mask)!=0;
39427 pFile->ctrlFlags &= ~mask;
39429 pFile->ctrlFlags |= mask;
39447 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
39451 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
39455 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
39461 *(int*)pArg = pFile->eFileLock;
39465 *(int*)pArg = pFile->lastErrno;
39469 pFile->szChunk = *(int *)pArg;
39488 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
39492 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
39494 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
39505 int iOld = pFile->iBusyTimeout;
39506 pFile->iBusyTimeout = *(int*)pArg;
39521 ** 64-bit type. */
39526 *(i64*)pArg = pFile->mmapSizeMax;
39527 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
39528 pFile->mmapSizeMax = newLimit;
39529 if( pFile->mmapSize>0 ){
39531 rc = unixMapfile(pFile, -1);
39544 ((unixFile*)id)->dbUpdate = 0;
39568 ** If pFd->sectorSize is non-zero when this function is called, it is a
39569 ** no-op. Otherwise, the values of pFd->sectorSize and
39570 ** pFd->deviceCharacteristics are set according to the file-system
39578 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
39579 if( pFd->sectorSize==0 ){
39585 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
39587 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
39592 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
39593 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
39596 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39603 if( pFile->sectorSize == 0 ){
39606 /* Set defaults for non-supported filesystems */
39607 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39608 pFile->deviceCharacteristics = 0;
39609 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
39614 pFile->sectorSize = fsInfo.f_bsize;
39615 pFile->deviceCharacteristics =
39623 pFile->sectorSize = fsInfo.f_bsize;
39624 pFile->deviceCharacteristics =
39626 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
39633 pFile->sectorSize = fsInfo.f_bsize;
39634 pFile->deviceCharacteristics =
39642 pFile->sectorSize = fsInfo.f_bsize;
39643 pFile->deviceCharacteristics =
39645 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
39650 pFile->sectorSize = fsInfo.f_bsize;
39651 pFile->deviceCharacteristics =
39653 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
39658 pFile->deviceCharacteristics =
39667 if( pFile->sectorSize % 512 != 0 ){
39668 pFile->deviceCharacteristics = 0;
39669 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
39680 ** if two files are created in the same file-system directory (i.e.
39687 return pFd->sectorSize;
39696 ** words, after a power-loss event, parts of the file that were never
39697 ** written might end up being altered.) However, non-PSOW behavior is very,
39700 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
39706 return pFd->deviceCharacteristics;
39734 ** When multiple threads all reference the same wal-index, each thread
39736 ** of this unixShmNode object. In other words, each wal-index is opened
39751 ** The following fields are read-only after the object is created:
39765 int szRegion; /* Size of shared-memory regions */
39767 u8 isReadonly; /* True if read-only */
39769 char **apRegion; /* Array of mapped shared-memory regions */
39772 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
39785 ** are read-only thereafter:
39790 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
39796 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
39810 ** wal-mode transactions in other processes on database file pFile. If
39819 if( pFile->pShm){
39820 unixShmNode *pShmNode = pFile->pShm->pShmNode;
39827 f.l_len = SQLITE_SHM_NLOCK - 3;
39829 sqlite3_mutex_enter(pShmNode->pShmMutex);
39830 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){
39835 sqlite3_mutex_leave(pShmNode->pShmMutex);
39843 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
39845 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
39854 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
39859 pShmNode = pFile->pInode->pShmNode;
39860 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
39861 assert( pShmNode->nRef>0 || unixMutexHeld() );
39869 if( pShmNode->hShm>=0 ){
39876 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
39877 if( res==-1 ){
39879 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
39889 OSTRACE(("SHM-LOCK "));
39890 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
39894 pShmNode->exclMask &= ~mask;
39895 pShmNode->sharedMask &= ~mask;
39897 OSTRACE(("read-lock %d ok", ofst));
39898 pShmNode->exclMask &= ~mask;
39899 pShmNode->sharedMask |= mask;
39902 OSTRACE(("write-lock %d ok", ofst));
39903 pShmNode->exclMask |= mask;
39904 pShmNode->sharedMask &= ~mask;
39910 OSTRACE(("read-lock failed"));
39913 OSTRACE(("write-lock %d failed", ofst));
39916 OSTRACE((" - afterwards %03x,%03x\n",
39917 pShmNode->sharedMask, pShmNode->exclMask));
39927 ** current system page-size.
39930 ** to use 64KB pages - in this case each mapping must cover at least two
39936 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
39944 ** This is not a VFS shared-memory method; it is a utility function called
39945 ** by VFS shared-memory methods.
39948 unixShmNode *p = pFd->pInode->pShmNode;
39950 if( p && ALWAYS(p->nRef==0) ){
39953 assert( p->pInode==pFd->pInode );
39954 sqlite3_mutex_free(p->pShmMutex);
39955 for(i=0; i<p->nRegion; i+=nShmPerMap){
39956 if( p->hShm>=0 ){
39957 osMunmap(p->apRegion[i], p->szRegion);
39959 sqlite3_free(p->apRegion[i]);
39962 sqlite3_free(p->apRegion);
39963 if( p->hShm>=0 ){
39964 robust_close(pFd, p->hShm, __LINE__);
39965 p->hShm = -1;
39967 p->pInode->pShmNode = 0;
39979 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
39988 ** and proceed with opening the *-shm file.
39992 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
39999 ** EXCLUSIVE failed just before truncating the *-shm file, then this
40000 ** process might open and use the *-shm file without truncating it.
40001 ** And if the *-shm file has been corrupted by a power failure or
40007 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
40010 if( pShmNode->isReadonly ){
40011 pShmNode->isUnlocked = 1;
40015 /* The first connection to attach must truncate the -shm file. We
40017 ** -shm header size) rather than 0 as a system debugging aid, to
40018 ** help detect if a -shm file truncation is legitimate or is the work
40020 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
40021 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
40036 ** Open a shared-memory area associated with open database file pDbFd.
40039 ** The file used to implement shared-memory is in the same directory
40041 ** file with the "-shm" suffix added. For example, if the database file
40043 ** for shared memory will be called "/home/user1/config.db-shm".
40049 ** database to end up using different files for shared memory -
40050 ** meaning that their memory would not really be shared - resulting
40052 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
40053 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
40057 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
40060 ** When opening a new shared-memory file, if no other instances of that
40064 ** If the original database file (pDbFd) is using the "unix-excl" VFS
40082 assert( pDbFd->pShm==0 );
40089 pInode = pDbFd->pInode;
40090 pShmNode = pInode->pShmNode;
40094 const char *zBasePath = pDbFd->zPath;
40098 ** a new *-shm file is created, an attempt will be made to create it
40101 if( osFstat(pDbFd->h, &sStat) ){
40102 rc = unixLogError(SQLITE_IOERR_FSTAT, "fstat", pDbFd->zPath);
40117 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
40120 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
40123 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
40124 sqlite3FileSuffix3(pDbFd->zPath, zShm);
40126 pShmNode->hShm = -1;
40127 pDbFd->pInode->pShmNode = pShmNode;
40128 pShmNode->pInode = pDbFd->pInode;
40130 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
40131 if( pShmNode->pShmMutex==0 ){
40137 if( pInode->bProcessLock==0 ){
40138 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
40139 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
40142 if( pShmNode->hShm<0 ){
40143 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
40145 if( pShmNode->hShm<0 ){
40149 pShmNode->isReadonly = 1;
40156 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
40164 p->pShmNode = pShmNode;
40166 p->id = pShmNode->nextShmId++;
40168 pShmNode->nRef++;
40169 pDbFd->pShm = p;
40176 ** at pShmNode->pFirst. This must be done while holding the
40177 ** pShmNode->pShmMutex.
40179 sqlite3_mutex_enter(pShmNode->pShmMutex);
40180 p->pNext = pShmNode->pFirst;
40181 pShmNode->pFirst = p;
40182 sqlite3_mutex_leave(pShmNode->pShmMutex);
40195 ** shared-memory associated with the database file fd. Shared-memory regions
40196 ** are numbered starting from zero. Each shared-memory region is szRegion
40201 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
40204 ** bExtend is non-zero and the requested shared-memory region has not yet
40207 ** If the shared-memory region has already been allocated or is allocated by
40226 /* If the shared-memory file has not yet been opened, open it now. */
40227 if( pDbFd->pShm==0 ){
40232 p = pDbFd->pShm;
40233 pShmNode = p->pShmNode;
40234 sqlite3_mutex_enter(pShmNode->pShmMutex);
40235 if( pShmNode->isUnlocked ){
40238 pShmNode->isUnlocked = 0;
40240 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
40241 assert( pShmNode->pInode==pDbFd->pInode );
40242 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
40243 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
40248 if( pShmNode->nRegion<nReqRegion ){
40253 pShmNode->szRegion = szRegion;
40255 if( pShmNode->hShm>=0 ){
40257 ** Check to see if it has been allocated (i.e. if the wal-index file is
40260 if( osFstat(pShmNode->hShm, &sStat) ){
40288 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
40289 const char *zFile = pShmNode->zFilename;
40300 pShmNode->apRegion, nReqRegion*sizeof(char *)
40306 pShmNode->apRegion = apNew;
40307 while( pShmNode->nRegion<nReqRegion ){
40311 if( pShmNode->hShm>=0 ){
40313 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
40314 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
40317 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
40330 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
40332 pShmNode->nRegion += nShmPerMap;
40337 if( pShmNode->nRegion>iRegion ){
40338 *pp = pShmNode->apRegion[iRegion];
40342 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
40343 sqlite3_mutex_leave(pShmNode->pShmMutex);
40348 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
40358 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
40361 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
40364 if( pX->exclMask & (1<<i) ){
40366 aLock[i] = -1;
40367 }else if( pX->sharedMask & (1<<i) ){
40374 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
40375 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
40380 ** Change the lock state for a shared-memory segment.
40400 p = pDbFd->pShm;
40402 pShmNode = p->pShmNode;
40404 aLock = pShmNode->aLock;
40406 assert( pShmNode==pDbFd->pInode->pShmNode );
40407 assert( pShmNode->pInode==pDbFd->pInode );
40415 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
40416 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
40432 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
40434 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
40435 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
40436 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
40440 mask = (1<<(ofst+n)) - (1<<ofst);
40442 sqlite3_mutex_enter(pShmNode->pShmMutex);
40445 if( (p->exclMask|p->sharedMask) & mask ){
40450 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
40460 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
40462 aLock[ofst]--;
40467 p->exclMask &= ~mask;
40468 p->sharedMask &= ~mask;
40473 assert( (p->exclMask & (1<<ofst))==0 );
40474 if( (p->sharedMask & mask)==0 ){
40483 p->sharedMask |= mask;
40492 assert( (p->sharedMask & mask)==0 );
40493 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
40500 ** also update the in-memory values. */
40504 assert( (p->sharedMask & mask)==0 );
40505 p->exclMask |= mask;
40507 aLock[ii] = -1;
40513 sqlite3_mutex_leave(pShmNode->pShmMutex);
40514 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
40515 p->id, osGetpid(0), p->sharedMask, p->exclMask));
40529 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
40530 assert( fd->pMethods->xLock==nolockLock
40538 ** Close a connection to shared-memory. Delete the underlying
40542 ** routine is a harmless no-op.
40546 int deleteFlag /* Delete shared-memory if true */
40549 unixShmNode *pShmNode; /* The underlying shared-memory file */
40554 p = pDbFd->pShm;
40556 pShmNode = p->pShmNode;
40558 assert( pShmNode==pDbFd->pInode->pShmNode );
40559 assert( pShmNode->pInode==pDbFd->pInode );
40563 sqlite3_mutex_enter(pShmNode->pShmMutex);
40564 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
40565 *pp = p->pNext;
40569 pDbFd->pShm = 0;
40570 sqlite3_mutex_leave(pShmNode->pShmMutex);
40572 /* If pShmNode->nRef has reached 0, then close the underlying
40573 ** shared-memory file, too */
40576 assert( pShmNode->nRef>0 );
40577 pShmNode->nRef--;
40578 if( pShmNode->nRef==0 ){
40579 if( deleteFlag && pShmNode->hShm>=0 ){
40580 osUnlink(pShmNode->zFilename);
40602 assert( pFd->nFetchOut==0 );
40603 if( pFd->pMapRegion ){
40604 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
40605 pFd->pMapRegion = 0;
40606 pFd->mmapSize = 0;
40607 pFd->mmapSizeActual = 0;
40621 ** If unsuccessful, an error message is logged via sqlite3_log() and
40631 int h = pFd->h; /* File descriptor open on db file */
40632 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
40633 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
40637 assert( pFd->nFetchOut==0 );
40638 assert( nNew>pFd->mmapSize );
40639 assert( nNew<=pFd->mmapSizeMax );
40641 assert( pFd->mmapSizeActual>=pFd->mmapSize );
40645 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
40650 i64 nReuse = pFd->mmapSize;
40653 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
40659 osMunmap(pReq, nOrig-nReuse);
40666 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
40669 osMunmap(pNew, nNew - nReuse);
40691 unixLogError(SQLITE_OK, zErr, pFd->zPath);
40696 pFd->mmapSizeMax = 0;
40698 pFd->pMapRegion = (void *)pNew;
40699 pFd->mmapSize = pFd->mmapSizeActual = nNew;
40703 ** Memory map or remap the file opened by file-descriptor pFd (if the file
40706 ** outstanding xFetch() references to it, this function is a no-op.
40708 ** If parameter nByte is non-negative, then it is the requested size of
40719 assert( nMap>=0 || pFd->nFetchOut==0 );
40720 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
40721 if( pFd->nFetchOut>0 ) return SQLITE_OK;
40724 struct stat statbuf; /* Low-level file information */
40725 if( osFstat(pFd->h, &statbuf) ){
40730 if( nMap>pFd->mmapSizeMax ){
40731 nMap = pFd->mmapSizeMax;
40734 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
40735 if( nMap!=pFd->mmapSize ){
40762 if( pFd->mmapSizeMax>0 ){
40763 if( pFd->pMapRegion==0 ){
40764 int rc = unixMapfile(pFd, -1);
40767 if( pFd->mmapSize >= iOff+nAmt ){
40768 *pp = &((u8 *)pFd->pMapRegion)[iOff];
40769 pFd->nFetchOut++;
40777 ** If the third argument is non-NULL, then this function releases a
40794 assert( (p==0)==(pFd->nFetchOut==0) );
40797 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
40800 pFd->nFetchOut--;
40805 assert( pFd->nFetchOut>=0 );
40823 ** of "finder" functions. A finder-function is used to locate the appropriate
40826 ** the correct finder-function for that VFS.
40829 ** object. The only interesting finder-function is autolockIoFinder, which
40833 ** For finder-function F, two objects are created:
40835 ** (1) The real finder-function named "FImpt()".
40842 ** directly at the finder-function since C90 rules prevent a void*
40959 ** The proxy locking method is a "super-method" in the sense that it
40961 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
41030 if( statfs(filePath, &fsInfo) != -1 ){
41042 ** Test byte-range lock using fcntl(). If the call succeeds,
41043 ** assume that the file-system supports POSIX style locks.
41049 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
41089 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
41127 assert( pNew->pInode==NULL );
41132 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
41133 pNew->h = h;
41134 pNew->pVfs = pVfs;
41135 pNew->zPath = zFilename;
41136 pNew->ctrlFlags = (u8)ctrlFlags;
41138 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
41142 pNew->ctrlFlags |= UNIXFILE_PSOW;
41144 if( strcmp(pVfs->zName,"unix-excl")==0 ){
41145 pNew->ctrlFlags |= UNIXFILE_EXCL;
41149 pNew->pId = vxworksFindFileId(zFilename);
41150 if( pNew->pId==0 ){
41159 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
41164 pNew->lockingContext = (void*)zFilename;
41174 rc = findInodeInfo(pNew, &pNew->pInode);
41187 ** handle h - as it is guaranteed that no posix locks will be released
41195 h = -1;
41206 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
41213 pCtx->dbPath = zFilename;
41214 pCtx->reserved = 0;
41217 rc = findInodeInfo(pNew, &pNew->pInode);
41219 sqlite3_free(pNew->lockingContext);
41221 h = -1;
41242 pNew->lockingContext = zLockFile;
41251 rc = findInodeInfo(pNew, &pNew->pInode);
41252 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
41253 char *zSemName = pNew->pInode->aSemName;
41256 pNew->pId->zCanonicalName);
41259 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
41260 if( pNew->pInode->pSem == SEM_FAILED ){
41262 pNew->pInode->aSemName[0] = '\0';
41273 h = -1;
41275 pNew->ctrlFlags |= UNIXFILE_DELETE;
41281 pId->pMethods = pLockingStyle;
41334 ** pVfs->mxPathname bytes.
41340 /* It's odd to simulate an io-error here, but really this is just
41341 ** using the io-error infrastructure to test that SQLite handles this
41353 zBuf[nBuf-2] = 0;
41356 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
41363 ** Routine to transform a unixFile into a proxy-locking unixFile.
41364 ** Implementation in the proxy-lock division, but used by unixOpen()
41372 ** file (not a journal or super-journal file) identified by pathname
41378 ** other file descriptor open on the same file is holding a file-lock.
41384 ** such file descriptor is located, -1 is returned.
41402 ** ignored and -1 is returned. The caller will try to open a new file
41411 while( pInode && (pInode->fileId.dev!=sStat.st_dev
41412 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
41413 pInode = pInode->pNext;
41417 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
41418 sqlite3_mutex_enter(pInode->pLockMutex);
41420 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
41423 *pp = pUnused->pNext;
41425 sqlite3_mutex_leave(pInode->pLockMutex);
41465 ** this function queries the file-system for the permissions on the
41494 ** "<path to db>-journal"
41495 ** "<path to db>-wal"
41496 ** "<path to db>-journalNN"
41497 ** "<path to db>-walNN"
41503 ** a '-' character. However in 8+3 filename mode, or if a corrupt
41504 ** rollback journal specifies a super-journal with a goofy name, then
41505 ** the '-' might be missing or the '-' might be the first character in
41508 nDb = sqlite3Strlen30(zPath) - 1;
41510 if( zPath[nDb]=='-' ){
41516 nDb--;
41545 ** ReadWrite() -> (READWRITE | CREATE)
41546 ** ReadOnly() -> (READONLY)
41547 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
41549 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
41563 int fd = -1; /* File descriptor returned by open() */
41582 /* If creating a super- or main-file journal, this function will open
41583 ** a file-descriptor on the directory too. The first time unixSync()
41610 /* The main DB, main journal, WAL file and super-journal are never
41617 /* Assert that the upper layer has set one of the "file-type" flags. */
41644 fd = pUnused->fd;
41651 p->pPreallocatedUnused = pUnused;
41653 /* Database filenames are double-zero terminated if they are not
41661 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
41667 /* Generated temporary filenames are always double-zero terminated
41688 assert( !p->pPreallocatedUnused );
41693 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
41701 /* Failed to open the file for read/write access. Try read-only. */
41718 ** the case. The chown() system call will be a no-op if the current
41737 if( p->pPreallocatedUnused ){
41738 p->pPreallocatedUnused->fd = fd;
41739 p->pPreallocatedUnused->flags =
41758 p->openFlags = openFlags;
41763 if( fstatfs(fd, &fsInfo) == -1 ){
41769 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
41772 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
41788 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
41793 ** never use proxy, NULL means use proxy for non-local files only. */
41806 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
41824 sqlite3_free(p->pPreallocatedUnused);
41842 if( osUnlink(zPath)==(-1) ){
41911 int rc; /* Non-zero following any error */
41926 const char *zName, /* Name to append to pPath. Not zero-terminated */
41934 if( pPath->nUsed<=1 ){
41935 pPath->rc = SQLITE_ERROR;
41938 assert( pPath->zOut[0]=='/' );
41939 while( pPath->zOut[--pPath->nUsed]!='/' ){}
41943 if( pPath->nUsed + nName + 2 >= pPath->nOut ){
41944 pPath->rc = SQLITE_ERROR;
41947 pPath->zOut[pPath->nUsed++] = '/';
41948 memcpy(&pPath->zOut[pPath->nUsed], zName, nName);
41949 pPath->nUsed += nName;
41951 if( pPath->rc==SQLITE_OK ){
41954 pPath->zOut[pPath->nUsed] = 0;
41955 zIn = pPath->zOut;
41958 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
41963 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
41964 pPath->rc = SQLITE_CANTOPEN_BKPT;
41967 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
41968 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
41969 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
41974 pPath->nUsed = 0;
41976 pPath->nUsed -= nName + 1;
41989 const char *zPath /* Path to append to pPath. Is zero-terminated */
41996 appendOnePathElement(pPath, &zPath[j], i-j);
42004 ** is stored as a nul-terminated string in the buffer pointed to by
42008 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
42026 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){
42052 ** message is available, it is written to zBufOut. If no error message
42054 ** error message.
42068 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
42071 ** use dlsym() with -pedantic-errors?
42079 ** This work-around is unlikely to work correctly on any system where
42108 ** errors. The reports issued by valgrind are incorrect - we would
42110 ** uninitialized space in zBuf - but valgrind errors tend to worry
42173 ** The following variable, if set to a non-zero value, is interpreted as
42237 ** low-level error message when operating-system problems come up
42256 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
42258 ** meta-layer over top of the primitive locking implemented above. For
42261 ** been defined - so that the primitive locking methods are available
42266 ** The default locking schemes in SQLite use byte-range locks on the
42271 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
42279 ** SHARED_RANGE 0x40000002 -> 0x40000200
42283 ** the read cache when byte-range locks are present. Enabling the read
42286 ** close-to-open semantics for ensuring cache coherency
42290 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
42299 ** -----------------
42331 ** -----------------------
42341 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
42342 ** by taking an sqlite-style shared lock on the conch file, reading the
42346 ** is patterned after the database file name as ".<databasename>-conch".
42354 ** The proxy file - a single-byte file used for all advisory file locks
42370 ** ---------------------
42374 ** Database files accessed on non-local file systems are
42419 int conchHeld; /* 1 if the conch is held, -1 if lockless */
42452 if( lPath[len-1]!='/' ){
42481 if( lockPath[i] == '/' && (i - start > 0) ){
42483 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
42484 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
42516 int fd = -1;
42525 ** 2. if that fails, and this is a lock file (not-conch), try creating
42527 ** 3. if that fails, try to open the file read-only
42532 fd = pUnused->fd;
42573 pNew->openFlags = openFlags;
42577 pUnused->fd = fd;
42578 pUnused->flags = openFlags;
42579 pNew->pPreallocatedUnused = pUnused;
42637 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
42649 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42650 unixFile *conchFile = pCtx->conchFile;
42653 char *cPath = pCtx->conchFilePath;
42657 int fd = -1;
42658 int rc = -1;
42661 /* create a new path by replace the trailing '-conch' with '-break' */
42664 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
42669 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
42689 fprintf(stderr, "broke stale lock on %s\n", cPath);
42690 robust_close(pFile, conchFile->h, __LINE__);
42691 conchFile->h = fd;
42692 conchFile->openFlags = O_RDWR | O_CREAT;
42700 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
42705 /* Take the requested lock on the conch file and break a stale lock if the
42709 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42710 unixFile *conchFile = pCtx->conchFile;
42717 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42727 if( osFstat(conchFile->h, &buf) ){
42746 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
42768 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
42771 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
42781 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
42787 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
42789 if( pCtx->conchHeld!=0 ){
42792 unixFile *conchFile = pCtx->conchFile;
42805 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
42806 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
42822 storeLastErrno(pFile, conchFile->lastErrno);
42834 ** retry with a new auto-generated path
42842 if( !pCtx->lockProxyPath ){
42843 /* for auto-named local lock file, just check the host ID and we'll
42847 size_t pathLen = (readLen - PROXY_PATHINDEX);
42850 pathLen=MAXPATHLEN-1;
42860 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
42861 readLen-PROXY_PATHINDEX)
42869 if( (conchFile->openFlags&O_RDWR) == 0 ){
42875 if( !pCtx->lockProxyPath ){
42876 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
42885 futimes(conchFile->h, NULL);
42887 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
42903 if( pCtx->lockProxyPath!=NULL ){
42904 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
42910 robust_ftruncate(conchFile->h, writeSize);
42912 full_fsync(conchFile->h,0,0);
42918 int err = osFstat(pFile->h, &buf);
42924 osFchmod(conchFile->h, cmode);
42927 rc = osFchmod(conchFile->h, cmode);
42928 }while( rc==(-1) && errno==EINTR );
42944 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
42947 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
42948 if( rc==SQLITE_OK && pFile->openFlags ){
42950 if( pFile->h>=0 ){
42951 robust_close(pFile, pFile->h, __LINE__);
42953 pFile->h = -1;
42954 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
42957 pFile->h = fd;
42963 if( rc==SQLITE_OK && !pCtx->lockProxy ){
42964 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
42965 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
42968 ** so try again via auto-naming
42980 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
42981 if( !pCtx->lockProxyPath ){
42987 pCtx->conchHeld = 1;
42989 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
42991 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
42992 afpCtx->dbPath = pCtx->lockProxyPath;
42995 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
42997 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
43000 } while (1); /* in case we need to retry the :auto: lock file -
43013 pCtx = (proxyLockingContext *)pFile->lockingContext;
43014 conchFile = pCtx->conchFile;
43015 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
43016 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
43018 if( pCtx->conchHeld>0 ){
43019 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
43021 pCtx->conchHeld = 0;
43022 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
43040 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
43052 for( i=(len-1); i>=0; i-- ){
43064 /* append the "-conch" suffix to the file */
43065 memcpy(&conchPath[i+1], "-conch", 7);
43072 /* Takes a fully configured proxy locking-style unix file and switches
43076 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43077 char *oldPath = pCtx->lockProxyPath;
43080 if( pFile->eFileLock!=NO_LOCK ){
43089 unixFile *lockProxy = pCtx->lockProxy;
43090 pCtx->lockProxy=NULL;
43091 pCtx->conchHeld = 0;
43093 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
43098 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
43113 if( pFile->pMethod == &afpIoMethods ){
43116 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
43117 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
43121 if( pFile->pMethod == &dotlockIoMethods ){
43124 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
43125 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
43128 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
43129 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
43139 ** ->lockingContext
43140 ** ->pMethod
43148 if( pFile->eFileLock!=NO_LOCK ){
43158 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
43167 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
43169 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
43170 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
43172 ** (c) the file system is read-only, then enable no-locking access.
43180 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
43182 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
43187 pCtx->conchHeld = -1; /* read only FS/ lockless */
43193 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
43197 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
43198 if( pCtx->dbPath==NULL ){
43206 pCtx->oldLockingContext = pFile->lockingContext;
43207 pFile->lockingContext = pCtx;
43208 pCtx->pOldMethod = pFile->pMethod;
43209 pFile->pMethod = &proxyIoMethods;
43211 if( pCtx->conchFile ){
43212 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
43213 sqlite3_free(pCtx->conchFile);
43215 sqlite3DbFree(0, pCtx->lockProxyPath);
43216 sqlite3_free(pCtx->conchFilePath);
43219 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
43233 if( pFile->pMethod == &proxyIoMethods ){
43234 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
43236 if( pCtx->lockProxyPath ){
43237 *(const char **)pArg = pCtx->lockProxyPath;
43249 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
43252 /* turn off proxy locking - not supported. If support is added for
43258 /* turn off proxy locking - already off - NOOP */
43265 (proxyLockingContext*)pFile->lockingContext;
43267 || (pCtx->lockProxyPath &&
43268 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
43291 ** above this point are all utilities. The lock-related methods of the
43292 ** proxy-locking sqlite3_io_method object follow.
43299 ** to a non-zero value otherwise *pResOut is set to zero. The return value
43306 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43307 if( pCtx->conchHeld>0 ){
43308 unixFile *proxy = pCtx->lockProxy;
43309 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
43318 ** Lock the file with the lock specified by parameter eFileLock - one
43332 ** UNLOCKED -> SHARED
43333 ** SHARED -> RESERVED
43334 ** SHARED -> (PENDING) -> EXCLUSIVE
43335 ** RESERVED -> (PENDING) -> EXCLUSIVE
43336 ** PENDING -> EXCLUSIVE
43345 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43346 if( pCtx->conchHeld>0 ){
43347 unixFile *proxy = pCtx->lockProxy;
43348 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
43349 pFile->eFileLock = proxy->eFileLock;
43363 ** the requested locking level, this routine is a no-op.
43369 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43370 if( pCtx->conchHeld>0 ){
43371 unixFile *proxy = pCtx->lockProxy;
43372 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
43373 pFile->eFileLock = proxy->eFileLock;
43387 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
43388 unixFile *lockProxy = pCtx->lockProxy;
43389 unixFile *conchFile = pCtx->conchFile;
43393 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
43395 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
43398 pCtx->lockProxy = 0;
43401 if( pCtx->conchHeld ){
43405 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
43409 sqlite3DbFree(0, pCtx->lockProxyPath);
43410 sqlite3_free(pCtx->conchFilePath);
43411 sqlite3DbFree(0, pCtx->dbPath);
43413 pFile->lockingContext = pCtx->oldLockingContext;
43414 pFile->pMethod = pCtx->pOldMethod;
43416 return pFile->pMethod->xClose(id);
43436 ** This routine registers all VFS implementations for unix-like operating
43453 ** when compiling with -pedantic-errors on GCC.)
43456 ** finder-function. The finder-function returns a pointer to the
43459 ** macro for addition information on finder-functions.
43507 UNIXVFS("unix-none", nolockIoFinder ),
43508 UNIXVFS("unix-dotfile", dotlockIoFinder ),
43509 UNIXVFS("unix-excl", posixIoFinder ),
43511 UNIXVFS("unix-namedsem", semIoFinder ),
43514 UNIXVFS("unix-posix", posixIoFinder ),
43517 UNIXVFS("unix-flock", flockIoFinder ),
43520 UNIXVFS("unix-afp", afpIoFinder ),
43521 UNIXVFS("unix-nfs", nfsIoFinder ),
43522 UNIXVFS("unix-proxy", proxyIoFinder ),
43527 /* Double-check that the aSyscall[] array has been constructed
43545 ** READ-0 UNIX_SHM_BASE+3 123
43546 ** READ-1 UNIX_SHM_BASE+4 124
43547 ** READ-2 UNIX_SHM_BASE+5 125
43548 ** READ-3 UNIX_SHM_BASE+6 126
43549 ** READ-4 UNIX_SHM_BASE+7 127
43552 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */
43566 ** This routine is a no-op for unix.
43620 ** based on the sub-platform)?
43628 ** based on the sub-platform)?
43716 ** characters, so we allocate 4 bytes per character assuming worst-case of
43717 ** 4-bytes-per-character for UTF8.
43733 ** Maximum error message length (in chars) for WinRT.
43740 ** Returns non-zero if the character should be treated as a directory
43804 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
43817 typedef struct winShm winShm; /* A connection to shared-memory */
43818 typedef struct winShmNode winShmNode; /* A region of shared-memory */
43876 BOOL bNoLock; /* Non-zero if locking is disabled. */
43890 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
43900 * If this is non-zero, an isolated heap will be created by the native Win32
43906 * WARNING: It is important to note that when this setting is non-zero and the
43918 * This is the maximum possible initial size of the Win32-specific heap, in
43926 * This is the extra space for the initial size of the Win32-specific heap,
43939 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
43946 * Win32-specific heap. It cannot be negative.
43952 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
43958 * initial size of the Win32-specific heap to exceed the maximum amount
43967 * The initial size of the Win32-specific heap. This value may be zero.
43976 * The maximum size of the Win32-specific heap. This value may be zero.
43992 ** The winMemData structure stores information required by the Win32-specific
44076 ** Many system calls are accessed through pointer-to-functions so that
44693 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
44805 int i = -1;
44809 for(i=0; i<ArraySize(aSyscall)-1; i++){
44824 ** "pnLargest" argument, if non-zero, will be used to return the size of the
44914 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
44915 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
44916 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
44982 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
45010 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
45035 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
45130 if( n==(SIZE_T)-1 ){
45152 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
45153 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
45156 if( !pWinMemData->hHeap ){
45164 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
45166 if( !pWinMemData->hHeap ){
45173 pWinMemData->bOwned = TRUE;
45174 assert( pWinMemData->bOwned );
45177 pWinMemData->hHeap = osGetProcessHeap();
45178 if( !pWinMemData->hHeap ){
45183 pWinMemData->bOwned = FALSE;
45184 assert( !pWinMemData->bOwned );
45186 assert( pWinMemData->hHeap!=0 );
45187 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
45189 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
45201 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
45202 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
45204 if( pWinMemData->hHeap ){
45205 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
45207 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
45209 if( pWinMemData->bOwned ){
45210 if( !osHeapDestroy(pWinMemData->hHeap) ){
45212 osGetLastError(), (void*)pWinMemData->hHeap);
45214 pWinMemData->bOwned = FALSE;
45216 pWinMemData->hHeap = NULL;
45221 ** Populate the low-level memory allocation function pointers in
45248 ** Convert a UTF-8 string to Microsoft Unicode.
45256 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
45264 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
45274 ** Convert a Microsoft Unicode string to UTF-8.
45282 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
45290 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
45310 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
45319 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
45329 ** Convert a Microsoft Unicode string to a multi-byte character string,
45339 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
45347 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
45357 ** Convert a multi-byte character string to UTF-8.
45375 ** Convert a UTF-8 string to a multi-byte character string.
45490 ** it accepts a UTF-8 string.
45527 ** it accepts a UTF-16 string.
45560 ** is zero if the error message fits in the buffer, or non-zero
45561 ** otherwise (if the message was truncated).
45640 ** This function - winLogErrorAtLine() - is only ever called via the macro
45644 ** It logs a message using sqlite3_log() containing the current value of
45645 ** error code and, if possible, the human-readable equivalent from
45651 ** failed and the associated file-system path, if any.
45671 "os_win.c:%d: (%lu) %s(%s) - %s",
45680 ** will be retried following a locking error - probably caused by
45697 ** non-zero if the error code is transient in nature and the operation
45785 y.tm_year = pTm.wYear - 1900;
45786 y.tm_mon = pTm.wMonth - 1;
45800 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
45834 memset(&pFile->local, 0, sizeof(pFile->local));
45844 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
45845 if (!pFile->hMutex){
45846 pFile->lastErrno = osGetLastError();
45848 return winLogError(SQLITE_IOERR, pFile->lastErrno,
45853 winceMutexAcquire(pFile->hMutex);
45856 ** case-sensitive, take advantage of that by uppercasing the mutex name
45860 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
45865 ** must be zero-initialized */
45874 if( pFile->hShared ){
45875 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
45878 if( !pFile->shared ){
45879 pFile->lastErrno = osGetLastError();
45880 winLogError(SQLITE_IOERR, pFile->lastErrno,
45883 osCloseHandle(pFile->hShared);
45884 pFile->hShared = NULL;
45889 if( pFile->hShared==NULL ){
45891 pFile->lastErrno = lastErrno;
45892 winLogError(SQLITE_IOERR, pFile->lastErrno,
45896 winceMutexRelease(pFile->hMutex);
45897 osCloseHandle(pFile->hMutex);
45898 pFile->hMutex = NULL;
45904 memset(pFile->shared, 0, sizeof(winceLock));
45907 winceMutexRelease(pFile->hMutex);
45915 if (pFile->hMutex){
45917 winceMutexAcquire(pFile->hMutex);
45921 if (pFile->local.nReaders){
45922 pFile->shared->nReaders --;
45924 if (pFile->local.bReserved){
45925 pFile->shared->bReserved = FALSE;
45927 if (pFile->local.bPending){
45928 pFile->shared->bPending = FALSE;
45930 if (pFile->local.bExclusive){
45931 pFile->shared->bExclusive = FALSE;
45934 /* De-reference and close our copy of the shared memory handle */
45935 osUnmapViewOfFile(pFile->shared);
45936 osCloseHandle(pFile->hShared);
45939 winceMutexRelease(pFile->hMutex);
45940 osCloseHandle(pFile->hMutex);
45941 pFile->hMutex = NULL;
45961 if (!pFile->hMutex) return TRUE;
45962 winceMutexAcquire(pFile->hMutex);
45967 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
45968 pFile->shared->bExclusive = TRUE;
45969 pFile->local.bExclusive = TRUE;
45974 /* Want a read-only lock? */
45977 if (pFile->shared->bExclusive == 0){
45978 pFile->local.nReaders ++;
45979 if (pFile->local.nReaders == 1){
45980 pFile->shared->nReaders ++;
45990 if (pFile->shared->bPending == 0) {
45991 pFile->shared->bPending = TRUE;
45992 pFile->local.bPending = TRUE;
46000 if (pFile->shared->bReserved == 0) {
46001 pFile->shared->bReserved = TRUE;
46002 pFile->local.bReserved = TRUE;
46007 winceMutexRelease(pFile->hMutex);
46027 if (!pFile->hMutex) return TRUE;
46028 winceMutexAcquire(pFile->hMutex);
46033 if (pFile->local.bExclusive){
46035 pFile->local.bExclusive = FALSE;
46036 pFile->shared->bExclusive = FALSE;
46041 else if (pFile->local.nReaders){
46044 pFile->local.nReaders --;
46045 if (pFile->local.nReaders == 0)
46047 pFile->shared->nReaders --;
46056 if (pFile->local.bPending){
46057 pFile->local.bPending = FALSE;
46058 pFile->shared->bPending = FALSE;
46065 if (pFile->local.bReserved) {
46066 pFile->local.bReserved = FALSE;
46067 pFile->shared->bReserved = FALSE;
46072 winceMutexRelease(pFile->hMutex);
46152 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
46158 ** Otherwise, set pFile->lastErrno and return non-zero.
46167 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
46173 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
46179 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
46183 pFile->lastErrno = lastErrno;
46184 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
46185 "winSeekFile", pFile->zPath);
46186 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46190 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46201 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
46204 pFile->lastErrno = osGetLastError();
46205 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
46206 "winSeekFile", pFile->zPath);
46207 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
46211 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
46239 assert( pFile->pShm==0 );
46241 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
46243 osGetCurrentProcessId(), pFile, pFile->h));
46250 rc = osCloseHandle(pFile->h);
46256 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
46257 if( pAppData==NULL || !pAppData->bNoLock ){
46261 if( pFile->zDeleteOnClose ){
46264 osDeleteFileW(pFile->zDeleteOnClose)==0
46265 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
46270 sqlite3_free(pFile->zDeleteOnClose);
46274 pFile->h = NULL;
46276 OpenCounter(-1);
46278 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
46281 "winClose", pFile->zPath);
46308 pFile->h, pBuf, amt, offset, pFile->locktype));
46313 if( offset<pFile->mmapSize ){
46314 if( offset+amt <= pFile->mmapSize ){
46315 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
46316 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46317 osGetCurrentProcessId(), pFile, pFile->h));
46320 int nCopy = (int)(pFile->mmapSize - offset);
46321 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
46323 amt -= nCopy;
46332 osGetCurrentProcessId(), pFile, pFile->h));
46335 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
46340 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
46345 pFile->lastErrno = lastErrno;
46347 osGetCurrentProcessId(), pFile, pFile->h));
46348 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
46349 "winRead", pFile->zPath);
46353 /* Unread parts of the buffer must be zero-filled */
46354 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
46356 osGetCurrentProcessId(), pFile, pFile->h));
46361 osGetCurrentProcessId(), pFile, pFile->h));
46386 pFile->h, pBuf, amt, offset, pFile->locktype));
46391 if( offset<pFile->mmapSize ){
46392 if( offset+amt <= pFile->mmapSize ){
46393 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
46394 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46395 osGetCurrentProcessId(), pFile, pFile->h));
46398 int nCopy = (int)(pFile->mmapSize - offset);
46399 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
46401 amt -= nCopy;
46429 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
46431 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
46447 nRem -= nWrite;
46450 pFile->lastErrno = lastErrno;
46456 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
46457 || ( pFile->lastErrno==ERROR_DISK_FULL )){
46459 osGetCurrentProcessId(), pFile, pFile->h));
46460 return winLogError(SQLITE_FULL, pFile->lastErrno,
46461 "winWrite1", pFile->zPath);
46464 osGetCurrentProcessId(), pFile, pFile->h));
46465 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
46466 "winWrite2", pFile->zPath);
46471 osGetCurrentProcessId(), pFile, pFile->h));
46484 if( pFile->nFetchOut>0 ){
46485 /* File truncation is a no-op if there are outstanding memory mapped
46491 ** No real harm comes of this - the database file is not corrupted,
46495 ** The only feasible work-around is to defer the truncation until after
46496 ** all references to memory-mapped content are closed. That is doable,
46499 ** now to simply make trancations a no-op if there are pending reads. We
46509 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
46511 /* If the user has configured a chunk-size for this file, truncate the
46516 if( pFile->szChunk>0 ){
46517 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
46521 if( pFile->pMapRegion ){
46522 oldMmapSize = pFile->mmapSize;
46529 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
46531 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
46532 "winTruncate1", pFile->zPath);
46533 }else if( 0==osSetEndOfFile(pFile->h) &&
46535 pFile->lastErrno = lastErrno;
46536 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
46537 "winTruncate2", pFile->zPath);
46543 winMapfile(pFile, -1);
46551 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
46597 osGetCurrentProcessId(), pFile, pFile->h, flags,
46598 pFile->locktype));
46610 ** no-op
46613 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
46614 osGetCurrentProcessId(), pFile, pFile->h));
46618 if( pFile->pMapRegion ){
46619 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
46620 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46622 pFile, pFile->pMapRegion));
46624 pFile->lastErrno = osGetLastError();
46625 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
46627 pFile, pFile->pMapRegion));
46628 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46629 "winSync1", pFile->zPath);
46633 rc = osFlushFileBuffers(pFile->h);
46637 osGetCurrentProcessId(), pFile, pFile->h));
46640 pFile->lastErrno = osGetLastError();
46642 osGetCurrentProcessId(), pFile, pFile->h));
46643 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
46644 "winSync2", pFile->zPath);
46659 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
46664 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
46668 pFile->lastErrno = osGetLastError();
46669 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
46670 "winFileSize", pFile->zPath);
46679 lowerBits = osGetFileSize(pFile->h, &upperBits);
46683 pFile->lastErrno = lastErrno;
46684 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
46685 "winFileSize", pFile->zPath);
46690 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
46732 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46739 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
46741 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
46749 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
46750 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46751 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
46755 pFile->lastErrno = osGetLastError();
46758 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
46768 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
46770 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
46774 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
46778 pFile->lastErrno = lastErrno;
46779 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
46780 "winUnlockReadLock", pFile->zPath);
46782 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
46787 ** Lock the file with the lock specified by parameter locktype - one
46801 ** UNLOCKED -> SHARED
46802 ** SHARED -> RESERVED
46803 ** SHARED -> (PENDING) -> EXCLUSIVE
46804 ** RESERVED -> (PENDING) -> EXCLUSIVE
46805 ** PENDING -> EXCLUSIVE
46815 int newLocktype; /* Set pFile->locktype to this value before exiting */
46822 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46828 if( pFile->locktype>=locktype ){
46829 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
46833 /* Do not allow any kind of write-lock on a read-only database
46835 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
46841 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
46843 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
46849 newLocktype = pFile->locktype;
46850 if( pFile->locktype==NO_LOCK
46851 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
46854 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
46857 ** around problems caused by indexing and/or anti-virus software on
46863 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
46864 pFile->h, cnt, res));
46866 pFile->lastErrno = lastErrno;
46868 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
46869 pFile->h, cnt, sqlite3ErrName(rc)));
46883 assert( pFile->locktype==NO_LOCK );
46895 assert( pFile->locktype==SHARED_LOCK );
46896 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
46914 assert( pFile->locktype>=SHARED_LOCK );
46916 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
46930 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
46939 pFile->lastErrno = lastErrno;
46941 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
46942 pFile->h, locktype, newLocktype));
46944 pFile->locktype = (u8)newLocktype;
46946 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
46953 ** non-zero, otherwise zero.
46960 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
46963 if( pFile->locktype>=RESERVED_LOCK ){
46965 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
46967 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
46969 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
46972 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
46975 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
46976 pFile->h, pResOut, *pResOut));
46985 ** the requested locking level, this routine is a no-op.
46998 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
46999 type = pFile->locktype;
47001 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
47006 "winUnlock", pFile->zPath);
47010 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
47016 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
47018 pFile->locktype = (u8)locktype;
47020 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
47025 ****************************** No-op Locking **********************************
47031 ** This locking mode is appropriate for use on read-only databases
47032 ** (ex: databases that are burned into CD-ROM, for example.) It can
47059 /******************* End of the no-op lock implementation *********************
47064 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
47066 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
47070 *pArg = (pFile->ctrlFlags & mask)!=0;
47072 pFile->ctrlFlags &= ~mask;
47074 pFile->ctrlFlags |= mask;
47089 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
47092 *(int*)pArg = pFile->locktype;
47093 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47097 *(int*)pArg = (int)pFile->lastErrno;
47098 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47102 pFile->szChunk = *(int *)pArg;
47103 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47107 if( pFile->szChunk>0 ){
47118 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47121 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47126 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47131 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47135 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
47136 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47151 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47156 *phFile = pFile->h;
47157 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
47163 HANDLE hOldFile = pFile->h;
47164 pFile->h = *phFile;
47167 hOldFile, pFile->h));
47173 int rc = winGetTempname(pFile->pVfs, &zTFile);
47177 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47190 ** least a 64-bit type. */
47195 *(i64*)pArg = pFile->mmapSizeMax;
47196 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
47197 pFile->mmapSizeMax = newLimit;
47198 if( pFile->mmapSize>0 ){
47200 rc = winMapfile(pFile, -1);
47203 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
47208 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
47218 ** if two files are created in the same file-system directory (i.e.
47233 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
47275 ** log-summary, each thread has its own winFile object, but they all
47277 ** log-summary is opened only once per process.
47285 ** The following fields are read-only after the object is created:
47300 int szRegion; /* Size of shared-memory regions */
47302 u8 isReadonly; /* True if read-only */
47331 ** are read-only thereafter:
47336 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
47363 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
47371 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
47373 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
47374 pFile->hFile.h, lockType, ofst, nByte));
47376 /* Release/Acquire the system-level lock */
47378 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
47383 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
47389 pFile->lastErrno = osGetLastError();
47393 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
47394 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
47395 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
47407 ** This is not a VFS shared-memory method; it is a utility function called
47408 ** by VFS shared-memory methods.
47414 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
47418 if( p->nRef==0 ){
47420 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
47421 for(i=0; i<p->nRegion; i++){
47422 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
47423 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
47426 bRc = osCloseHandle(p->aRegion[i].hMap);
47427 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
47431 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
47433 winClose((sqlite3_file *)&p->hFile);
47439 winDelete(pVfs, p->zFilename, 0);
47443 *pp = p->pNext;
47444 sqlite3_free(p->aRegion);
47447 pp = &p->pNext;
47459 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
47465 if( pShmNode->isReadonly ){
47466 pShmNode->isUnlocked = 1;
47469 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
47472 "winLockSharedMemory", pShmNode->zFilename);
47484 ** Open the shared-memory area associated with database file pDbFd.
47486 ** When opening a new shared-memory file, if no other instances of that
47497 assert( pDbFd->pShm==0 ); /* Not previously opened */
47504 nName = sqlite3Strlen30(pDbFd->zPath);
47510 pNew->zFilename = (char*)&pNew[1];
47511 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
47512 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
47518 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
47522 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
47532 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
47533 pShmNode->pNext = winShmNodeList;
47537 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
47538 if( pShmNode->mutex==0 ){
47544 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
47549 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
47550 (sqlite3_file*)&pShmNode->hFile,
47554 pShmNode->zFilename);
47557 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
47564 p->pShmNode = pShmNode;
47566 p->id = pShmNode->nextShmId++;
47568 pShmNode->nRef++;
47569 pDbFd->pShm = p;
47576 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
47579 sqlite3_mutex_enter(pShmNode->mutex);
47580 p->pNext = pShmNode->pFirst;
47581 pShmNode->pFirst = p;
47582 sqlite3_mutex_leave(pShmNode->mutex);
47588 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
47596 ** Close a connection to shared-memory. Delete the underlying
47603 winFile *pDbFd; /* Database holding shared-memory */
47605 winShmNode *pShmNode; /* The underlying shared-memory file */
47609 p = pDbFd->pShm;
47611 pShmNode = p->pShmNode;
47615 sqlite3_mutex_enter(pShmNode->mutex);
47616 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
47617 *pp = p->pNext;
47621 pDbFd->pShm = 0;
47622 sqlite3_mutex_leave(pShmNode->mutex);
47624 /* If pShmNode->nRef has reached 0, then close the underlying
47625 ** shared-memory file, too */
47627 assert( pShmNode->nRef>0 );
47628 pShmNode->nRef--;
47629 if( pShmNode->nRef==0 ){
47630 winShmPurge(pDbFd->pVfs, deleteFlag);
47638 ** Change the lock state for a shared-memory segment.
47647 winShm *p = pDbFd->pShm; /* The shared memory being locked */
47654 pShmNode = p->pShmNode;
47665 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
47667 sqlite3_mutex_enter(pShmNode->mutex);
47672 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47674 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
47675 allMask |= pX->sharedMask;
47678 /* Unlock the system-level locks */
47687 p->exclMask &= ~mask;
47688 p->sharedMask &= ~mask;
47697 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47698 if( (pX->exclMask & mask)!=0 ){
47702 allShared |= pX->sharedMask;
47716 p->sharedMask |= mask;
47722 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
47723 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
47735 assert( (p->sharedMask & mask)==0 );
47736 p->exclMask |= mask;
47740 sqlite3_mutex_leave(pShmNode->mutex);
47741 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
47742 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
47757 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
47764 ** shared-memory associated with the database file fd. Shared-memory regions
47765 ** are numbered starting from zero. Each shared-memory region is szRegion
47770 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
47773 ** isWrite is non-zero and the requested shared-memory region has not yet
47776 ** If the shared-memory region has already been allocated or is allocated by
47789 winShm *pShm = pDbFd->pShm;
47798 pShm = pDbFd->pShm;
47801 pShmNode = pShm->pShmNode;
47803 sqlite3_mutex_enter(pShmNode->mutex);
47804 if( pShmNode->isUnlocked ){
47807 pShmNode->isUnlocked = 0;
47809 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
47811 if( pShmNode->nRegion<=iRegion ){
47814 sqlite3_int64 sz; /* Current size of wal-index file */
47816 pShmNode->szRegion = szRegion;
47819 ** Check to see if it has been allocated (i.e. if the wal-index file is
47822 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
47825 "winShmMap1", pDbFd->zPath);
47833 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
47837 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
47840 "winShmMap2", pDbFd->zPath);
47847 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
47853 pShmNode->aRegion = apNew;
47855 if( pShmNode->isReadonly ){
47860 while( pShmNode->nRegion<=iRegion ){
47861 HANDLE hMap = NULL; /* file-mapping handle */
47865 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
47869 hMap = osCreateFileMappingW(pShmNode->hFile.h,
47873 hMap = osCreateFileMappingA(pShmNode->hFile.h,
47877 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
47878 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
47881 int iOffset = pShmNode->nRegion*szRegion;
47885 iOffset - iOffsetShift, szRegion + iOffsetShift
47889 0, iOffset - iOffsetShift, szRegion + iOffsetShift
47892 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
47893 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
47897 pShmNode->lastErrno = osGetLastError();
47898 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
47899 "winShmMap3", pDbFd->zPath);
47904 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
47905 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
47906 pShmNode->nRegion++;
47911 if( pShmNode->nRegion>iRegion ){
47914 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
47919 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
47920 sqlite3_mutex_leave(pShmNode->mutex);
47937 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
47939 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
47940 pFile->mmapSize, pFile->mmapSizeMax));
47941 if( pFile->pMapRegion ){
47942 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
47943 pFile->lastErrno = osGetLastError();
47944 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
47946 pFile->pMapRegion));
47947 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
47948 "winUnmapfile1", pFile->zPath);
47950 pFile->pMapRegion = 0;
47951 pFile->mmapSize = 0;
47953 if( pFile->hMap!=NULL ){
47954 if( !osCloseHandle(pFile->hMap) ){
47955 pFile->lastErrno = osGetLastError();
47956 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
47957 osGetCurrentProcessId(), pFile, pFile->hMap));
47958 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
47959 "winUnmapfile2", pFile->zPath);
47961 pFile->hMap = NULL;
47963 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
47969 ** Memory map or remap the file opened by file-descriptor pFd (if the file
47972 ** outstanding xFetch() references to it, this function is a no-op.
47974 ** If parameter nByte is non-negative, then it is the requested size of
47988 assert( nMap>=0 || pFd->nFetchOut==0 );
47989 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
47992 if( pFd->nFetchOut>0 ) return SQLITE_OK;
47997 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
48002 if( nMap>pFd->mmapSizeMax ){
48003 nMap = pFd->mmapSizeMax;
48005 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
48007 if( nMap==0 && pFd->mmapSize>0 ){
48010 if( nMap!=pFd->mmapSize ){
48017 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
48023 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
48025 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
48029 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
48033 if( pFd->hMap==NULL ){
48034 pFd->lastErrno = osGetLastError();
48035 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
48036 "winMapfile1", pFd->zPath);
48038 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
48045 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
48047 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
48050 osCloseHandle(pFd->hMap);
48051 pFd->hMap = NULL;
48052 pFd->lastErrno = osGetLastError();
48053 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
48054 "winMapfile2", pFd->zPath);
48056 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
48060 pFd->pMapRegion = pNew;
48061 pFd->mmapSize = nMap;
48064 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
48092 if( pFd->mmapSizeMax>0 ){
48093 if( pFd->pMapRegion==0 ){
48094 int rc = winMapfile(pFd, -1);
48101 if( pFd->mmapSize >= iOff+nAmt ){
48102 assert( pFd->pMapRegion!=0 );
48103 *pp = &((u8 *)pFd->pMapRegion)[iOff];
48104 pFd->nFetchOut++;
48115 ** If the third argument is non-NULL, then this function releases a
48131 assert( (p==0)==(pFd->nFetchOut==0) );
48134 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
48140 pFd->nFetchOut--;
48144 ** is unnecessary can be omitted - potentially improving
48149 assert( pFd->nFetchOut>=0 );
48237 ** supports for filenames into UTF-8. Space to hold the result is
48256 ** Convert a UTF-8 filename into whatever form the underlying
48276 ** This function returns non-zero if the specified UTF-8 string buffer
48284 if( winIsDirSep(zBuf[nLen-1]) ){
48310 /* It's odd to simulate an io-error here, but really this is just
48311 ** using the io-error infrastructure to test that SQLite handles this
48319 nMax = pVfs->mxPathname; nBuf = nMax + 2;
48322 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48330 nDir = nMax - (nPre + 15);
48335 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
48340 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48380 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48393 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48401 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
48408 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
48414 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48432 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48438 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48450 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48460 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48465 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
48475 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
48489 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48507 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
48511 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
48516 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
48522 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
48568 const char *zName, /* Name of the file (UTF-8) */
48585 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
48627 /* The main DB, main journal, WAL file and super-journal are never
48634 /* Assert that the upper layer has set one of the "file-type" flags. */
48643 pFile->h = INVALID_HANDLE_VALUE;
48665 /* Database filenames are double-zero terminated if they are not
48811 pFile->lastErrno = lastErrno;
48812 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
48829 pAppData = (winVfsAppData*)pVfs->pAppData;
48834 && ((pAppData==NULL) || !pAppData->bNoLock)
48840 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
48845 pFile->zDeleteOnClose = zConverted;
48853 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
48854 pFile->pVfs = pVfs;
48855 pFile->h = h;
48857 pFile->ctrlFlags |= WINFILE_RDONLY;
48862 pFile->ctrlFlags |= WINFILE_PSOW;
48864 pFile->lastErrno = NO_ERROR;
48865 pFile->zPath = zName;
48867 pFile->hMap = NULL;
48868 pFile->pMapRegion = 0;
48869 pFile->mmapSize = 0;
48870 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
49026 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
49072 ** Returns non-zero if the specified path name starts with the "long path"
49083 ** Returns non-zero if the specified path name starts with a drive letter
49093 ** Returns non-zero if the specified path name should be used verbatim. If
49094 ** non-zero is returned from this function, the calling function must simply
49095 ** use the provided path name verbatim -OR- resolve it into a full path name
49131 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
49157 assert( nFull>=pVfs->mxPathname );
49165 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49171 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
49181 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49187 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
49193 zRelative, zOut, pVfs->mxPathname+1)<0 ){
49203 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
49222 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49225 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
49231 /* It's odd to simulate an io-error here, but really this is just
49232 ** using the io-error infrastructure to test that SQLite handles this
49244 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
49305 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
49322 int nFull = pVfs->mxPathname+1;
49397 for(j=0, k=p->i; j<sz; j++){
49398 p->a[k++] ^= x[j];
49399 if( k>=p->na ) k = 0;
49401 p->i = k;
49402 p->nXor += sz;
49473 ** The following variable, if set to a non-zero value, is interpreted as
49492 /* FILETIME structure is a 64-bit value representing the number of
49493 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
49500 /* 2^32 - to avoid use of LL and warnings in gcc */
49550 ** buffer with a nul-terminated UTF-8 encoded error message
49554 ** If the error message is too large for the supplied buffer,
49556 ** is zero if the error message fits in the buffer, or non-zero
49557 ** otherwise (if the message was truncated). If non-zero is returned,
49558 ** then it is not necessary to include the nul-terminator character
49561 ** Not supplying an error message will have no adverse effect
49563 ** returns an error message:
49570 ** However if an error message is supplied, it will be incorporated
49571 ** by sqlite into the error message available to the user using
49615 "win32-longpath", /* zName */
49640 "win32-none", /* zName */
49665 "win32-longpath-none", /* zName */
49686 /* Double-check that the aSyscall[] array has been constructed
49739 ** 2016-09-07
49750 ** This file implements an in-memory VFS. A database is held as a contiguous
49766 /* Access to a lower-level VFS that (might) implement dynamic loading,
49769 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
49829 ** File-scope variables for holding the memdb files that are accessible
49907 memdbLock, /* xUnlock - same as xLock in this case */
49932 sqlite3_mutex_enter(p->pMutex);
49935 sqlite3_mutex_leave(p->pMutex);
49942 ** Close an memdb-file.
49947 MemStore *p = ((MemFile*)pFile)->pStore;
49948 if( p->zFName ){
49957 if( p->nRef==1 ){
49958 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
49971 p->nRef--;
49972 if( p->nRef<=0 ){
49973 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
49974 sqlite3_free(p->aData);
49977 sqlite3_mutex_free(p->pMutex);
49986 ** Read data from an memdb-file.
49994 MemStore *p = ((MemFile*)pFile)->pStore;
49996 if( iOfst+iAmt>p->sz ){
49998 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
50002 memcpy(zBuf, p->aData+iOfst, iAmt);
50012 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
50015 if( newSz>p->szMax ){
50019 if( newSz>p->szMax ) newSz = p->szMax;
50020 pNew = sqlite3Realloc(p->aData, newSz);
50022 p->aData = pNew;
50023 p->szAlloc = newSz;
50028 ** Write data to an memdb-file.
50036 MemStore *p = ((MemFile*)pFile)->pStore;
50038 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
50044 if( iOfst+iAmt>p->sz ){
50046 if( iOfst+iAmt>p->szAlloc
50052 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
50053 p->sz = iOfst+iAmt;
50055 memcpy(p->aData+iOfst, z, iAmt);
50061 ** Truncate an memdb-file.
50068 MemStore *p = ((MemFile*)pFile)->pStore;
50071 if( size>p->sz ){
50075 p->sz = size;
50082 ** Sync an memdb-file.
50091 ** Return the current file-size of an memdb-file.
50094 MemStore *p = ((MemFile*)pFile)->pStore;
50096 *pSize = p->sz;
50102 ** Lock an memdb-file.
50106 MemStore *p = pThis->pStore;
50108 if( eLock==pThis->eLock ) return SQLITE_OK;
50111 if( p->mFlags & SQLITE_DESERIALIZE_READONLY ){
50113 }else if( pThis->eLock<=SQLITE_LOCK_SHARED ){
50114 if( p->nWrLock ){
50117 p->nWrLock = 1;
50121 if( pThis->eLock > SQLITE_LOCK_SHARED ){
50122 assert( p->nWrLock==1 );
50123 p->nWrLock = 0;
50124 }else if( p->nWrLock ){
50127 p->nRdLock++;
50131 if( pThis->eLock>SQLITE_LOCK_SHARED ){
50132 assert( p->nWrLock==1 );
50133 p->nWrLock = 0;
50135 assert( p->nRdLock>0 );
50136 p->nRdLock--;
50138 if( rc==SQLITE_OK ) pThis->eLock = eLock;
50146 ** occur on an in-memory database.
50156 ** File control method. For custom operations on an memdb-file.
50159 MemStore *p = ((MemFile*)pFile)->pStore;
50163 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
50168 if( iLimit<p->sz ){
50170 iLimit = p->szMax;
50172 iLimit = p->sz;
50175 p->szMax = iLimit;
50185 ** Return the sector-size in bytes for an memdb-file.
50193 ** Return the device characteristic flags supported by an memdb-file.
50203 /* Fetch a page of a memory-mapped file */
50210 MemStore *p = ((MemFile*)pFile)->pStore;
50212 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
50215 p->nMmap++;
50216 *pp = (void*)(p->aData + iOfst);
50222 /* Release a memory-mapped page */
50224 MemStore *p = ((MemFile*)pFile)->pStore;
50228 p->nMmap--;
50257 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
50279 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
50280 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
50281 p->zFName = (char*)&p[1];
50282 memcpy(p->zFName, zName, szName+1);
50283 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50284 if( p->pMutex==0 ){
50285 memdb_g.nMemStore--;
50290 p->nRef = 1;
50294 p->nRef++;
50303 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
50304 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
50306 pFile->pStore = p;
50310 pFd->pMethods = &memdb_io_methods;
50315 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
50319 ** ensure the file-system modifications are synced to disk before
50366 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
50371 ** utf-8 string describing the most recent error encountered associated
50375 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
50382 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
50389 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
50397 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
50405 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
50413 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
50418 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
50421 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
50433 if( p->base.pMethods!=&memdb_io_methods ) return 0;
50434 pStore = p->pStore;
50436 if( pStore->zFName!=0 ) p = 0;
50467 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50470 if( piSize ) *piSize = -1;
50473 MemStore *pStore = p->pStore;
50474 assert( pStore->pMutex==0 );
50475 if( piSize ) *piSize = pStore->sz;
50477 pOut = pStore->aData;
50479 pOut = sqlite3_malloc64( pStore->sz );
50480 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
50484 pBt = db->aDb[iDb].pBt;
50488 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
50507 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
50547 sqlite3_mutex_enter(db->mutex);
50548 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
50559 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
50563 db->init.iDb = (u8)iDb;
50564 db->init.reopenMemdb = 1;
50566 db->init.reopenMemdb = 0;
50575 MemStore *pStore = p->pStore;
50576 pStore->aData = pData;
50578 pStore->sz = szDb;
50579 pStore->szAlloc = szBuf;
50580 pStore->szMax = szBuf;
50581 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
50582 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
50584 pStore->mFlags = mFlags;
50593 sqlite3_mutex_leave(db->mutex);
50605 sz = pLower->szOsFile;
50630 ** This file implements an object that represents a fixed-length
50634 ** journalled during a transaction, or which pages have the "dont-write"
50663 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
50680 ** sub-dividing and re-hashing. */
50685 ** no fewer collisions than the no-op *1. */
50705 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
50714 u32 nSet; /* Number of bits that are set - only valid for aHash
50738 p->iSize = iSize;
50744 ** Check to see if the i-th bit is set. Return true or false.
50750 i--;
50751 if( i>=p->iSize ) return 0;
50752 while( p->iDivisor ){
50753 u32 bin = i/p->iDivisor;
50754 i = i%p->iDivisor;
50755 p = p->u.apSub[bin];
50760 if( p->iSize<=BITVEC_NBIT ){
50761 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
50764 while( p->u.aHash[h] ){
50765 if( p->u.aHash[h]==i ) return 1;
50776 ** Set the i-th bit. Return 0 on success and an error code if
50779 ** This routine might cause sub-bitmaps to be allocated. Failing
50780 ** to get the memory needed to hold the sub-bitmap is the only
50791 assert( i<=p->iSize );
50792 i--;
50793 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
50794 u32 bin = i/p->iDivisor;
50795 i = i%p->iDivisor;
50796 if( p->u.apSub[bin]==0 ){
50797 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
50798 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
50800 p = p->u.apSub[bin];
50802 if( p->iSize<=BITVEC_NBIT ){
50803 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
50809 /* worring about sub-dividing and re-hashing. */
50810 if( !p->u.aHash[h] ){
50811 if (p->nSet<(BITVEC_NINT-1)) {
50820 if( p->u.aHash[h]==i ) return SQLITE_OK;
50823 } while( p->u.aHash[h] );
50828 if( p->nSet>=BITVEC_MXHASH ){
50831 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
50835 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50836 memset(p->u.apSub, 0, sizeof(p->u.apSub));
50837 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
50847 p->nSet++;
50848 p->u.aHash[h] = i;
50853 ** Clear the i-th bit.
50861 i--;
50862 while( p->iDivisor ){
50863 u32 bin = i/p->iDivisor;
50864 i = i%p->iDivisor;
50865 p = p->u.apSub[bin];
50870 if( p->iSize<=BITVEC_NBIT ){
50871 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
50875 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
50876 memset(p->u.aHash, 0, sizeof(p->u.aHash));
50877 p->nSet = 0;
50880 u32 h = BITVEC_HASH(aiValues[j]-1);
50881 p->nSet++;
50882 while( p->u.aHash[h] ){
50886 p->u.aHash[h] = aiValues[j];
50897 if( p->iDivisor ){
50900 sqlite3BitvecDestroy(p->u.apSub[i]);
50911 return p->iSize;
50953 ** If a memory allocation error occurs, return -1.
50958 int rc = -1;
50981 i = aOp[pc+2] - 1;
50993 if( (--aOp[pc+1]) > 0 ) nx = 0;
51009 ** match (rc==0). Change rc to non-zero if a discrepancy
51014 + (sqlite3BitvecSize(pBitvec) - sz);
51060 ** such that p was added to the list more recently than p->pDirtyNext.
51065 ** page to eject from the cache mid-transaction. It is better to eject
51109 if( pCache->pCache==0 ) return;
51113 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
51115 pPg = (PgHdr*)pLower->pExtra;
51116 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
51117 a = (unsigned char *)pLower->pBuf;
51120 if( pPg->pPage==0 ){
51121 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
51143 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
51144 pCache = pPg->pCache;
51146 if( pPg->flags & PGHDR_CLEAN ){
51147 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
51148 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
51149 assert( pCache->pDirtyTail!=pPg );
51152 if( pPg->flags & PGHDR_WRITEABLE ){
51153 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
51191 PCache *p = pPage->pCache;
51195 pPage->pgno));
51197 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
51198 assert( pPage->pDirtyPrev || pPage==p->pDirty );
51201 if( p->pSynced==pPage ){
51202 p->pSynced = pPage->pDirtyPrev;
51205 if( pPage->pDirtyNext ){
51206 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
51208 assert( pPage==p->pDirtyTail );
51209 p->pDirtyTail = pPage->pDirtyPrev;
51211 if( pPage->pDirtyPrev ){
51212 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
51218 assert( pPage==p->pDirty );
51219 p->pDirty = pPage->pDirtyNext;
51220 assert( p->bPurgeable || p->eCreate==2 );
51221 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
51222 assert( p->bPurgeable==0 || p->eCreate==1 );
51223 p->eCreate = 2;
51228 pPage->pDirtyPrev = 0;
51229 pPage->pDirtyNext = p->pDirty;
51230 if( pPage->pDirtyNext ){
51231 assert( pPage->pDirtyNext->pDirtyPrev==0 );
51232 pPage->pDirtyNext->pDirtyPrev = pPage;
51234 p->pDirtyTail = pPage;
51235 if( p->bPurgeable ){
51236 assert( p->eCreate==2 );
51237 p->eCreate = 1;
51240 p->pDirty = pPage;
51246 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
51247 if( !p->pSynced
51248 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
51250 p->pSynced = pPage;
51258 ** being used for an in-memory database, this function is a no-op.
51261 if( p->pCache->bPurgeable ){
51262 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
51263 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
51264 pcacheDump(p->pCache);
51269 ** Compute the number of pages of cache requested. p->szCache is the
51273 if( p->szCache>=0 ){
51274 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
51276 return p->szCache;
51279 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
51283 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
51296 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
51297 ** built-in default page cache is used instead of the application defined
51306 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
51337 p->szPage = 1;
51338 p->szExtra = szExtra;
51340 p->bPurgeable = bPurgeable;
51341 p->eCreate = 2;
51342 p->xStress = xStress;
51343 p->pStress = pStress;
51344 p->szCache = 100;
51345 p->szSpill = 1;
51355 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
51356 if( pCache->szPage ){
51359 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
51360 pCache->bPurgeable
51364 if( pCache->pCache ){
51365 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
51367 pCache->pCache = pNew;
51368 pCache->szPage = szPage;
51407 assert( pCache->pCache!=0 );
51409 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
51418 eCreate = createFlag & pCache->eCreate;
51420 assert( createFlag==0 || pCache->eCreate==eCreate );
51421 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
51422 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
51445 if( pCache->eCreate==2 ) return 0;
51447 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
51448 /* Find a dirty page to write-out and recycle. First try to find a
51449 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
51457 for(pPg=pCache->pSynced;
51458 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
51459 pPg=pPg->pDirtyPrev
51461 pCache->pSynced = pPg;
51463 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
51469 "spill page %d making room for %d - cache used: %d/%d",
51470 pPg->pgno, pgno,
51471 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
51474 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
51475 rc = pCache->xStress(pCache->pStress, pPg);
51482 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
51502 pPgHdr = (PgHdr*)pPage->pExtra;
51503 assert( pPgHdr->pPage==0 );
51504 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
51505 pPgHdr->pPage = pPage;
51506 pPgHdr->pData = pPage->pBuf;
51507 pPgHdr->pExtra = (void *)&pPgHdr[1];
51508 memset(pPgHdr->pExtra, 0, 8);
51509 pPgHdr->pCache = pCache;
51510 pPgHdr->pgno = pgno;
51511 pPgHdr->flags = PGHDR_CLEAN;
51529 pPgHdr = (PgHdr *)pPage->pExtra;
51531 if( !pPgHdr->pPage ){
51534 pCache->nRefSum++;
51535 pPgHdr->nRef++;
51545 assert( p->nRef>0 );
51546 p->pCache->nRefSum--;
51547 if( (--p->nRef)==0 ){
51548 if( p->flags&PGHDR_CLEAN ){
51560 assert(p->nRef>0);
51562 p->nRef++;
51563 p->pCache->nRefSum++;
51572 assert( p->nRef==1 );
51574 if( p->flags&PGHDR_DIRTY ){
51577 p->pCache->nRefSum--;
51578 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
51586 assert( p->nRef>0 );
51588 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
51589 p->flags &= ~PGHDR_DONT_WRITE;
51590 if( p->flags & PGHDR_CLEAN ){
51591 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
51592 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
51593 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
51606 assert( (p->flags & PGHDR_DIRTY)!=0 );
51607 assert( (p->flags & PGHDR_CLEAN)==0 );
51609 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
51610 p->flags |= PGHDR_CLEAN;
51611 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
51613 if( p->nRef==0 ){
51623 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
51624 while( (p = pCache->pDirty)!=0 ){
51634 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
51635 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51636 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
51638 pCache->pSynced = pCache->pDirtyTail;
51646 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51647 p->flags &= ~PGHDR_NEED_SYNC;
51649 pCache->pSynced = pCache->pDirtyTail;
51656 PCache *pCache = p->pCache;
51657 assert( p->nRef>0 );
51660 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
51661 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
51662 p->pgno = newPgno;
51663 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
51678 if( pCache->pCache ){
51682 for(p=pCache->pDirty; p; p=pNext){
51683 pNext = p->pDirtyNext;
51688 assert( p->pgno>0 );
51689 if( p->pgno>pgno ){
51690 assert( p->flags&PGHDR_DIRTY );
51694 if( pgno==0 && pCache->nRefSum ){
51696 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
51698 ** pCache->nRefSum>0 */
51699 memset(pPage1->pBuf, 0, pCache->szPage);
51703 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
51711 assert( pCache->pCache!=0 );
51713 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
51732 if( pA->pgno<pB->pgno ){
51733 pTail->pDirty = pA;
51735 pA = pA->pDirty;
51737 pTail->pDirty = pB;
51741 pTail->pDirty = pB;
51743 pB = pB->pDirty;
51745 pTail->pDirty = pA;
51770 pIn = p->pDirty;
51771 p->pDirty = 0;
51772 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
51781 if( NEVER(i==N_SORT_BUCKET-1) ){
51801 for(p=pCache->pDirty; p; p=p->pDirtyNext){
51802 p->pDirty = p->pDirtyNext;
51804 return pcacheSortDirtyList(pCache->pDirty);
51814 return pCache->nRefSum;
51821 return p->nRef;
51828 assert( pCache->pCache!=0 );
51829 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
51834 ** Get the suggested cache-size value.
51842 ** Set the suggested cache-size value.
51845 assert( pCache->pCache!=0 );
51846 pCache->szCache = mxPage;
51847 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
51852 ** Set the suggested cache-spill value. Make no changes if if the
51853 ** argument is zero. Return the effective cache-spill size, which will
51858 assert( p->pCache!=0 );
51861 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
51863 p->szSpill = mxPage;
51866 if( res<p->szSpill ) res = p->szSpill;
51874 assert( pCache->pCache!=0 );
51875 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
51880 ** in the page-cache hierarchy.
51892 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
51901 return (pCache->pDirty!=0);
51913 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
51941 ** -------------------------------------------------------------
51943 ** -------------------------------------------------------------
51959 ** size can vary according to architecture, compile-time options, and
51967 ** power of two, and leaving the rounded-up space unused.
51979 ** (1) The general-purpose memory allocator - sqlite3Malloc()
51980 ** (2) Global page-cache memory provided using sqlite3_config() with
51982 ** (3) PCache-local bulk allocation.
51992 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
51996 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
52013 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
52017 ** but causes a 2-byte gap in the structure for most architectures (since
52018 ** pointers must be either 4 or 8-byte aligned). As this structure is located
52020 ** corrupt, code at the b-tree layer may overread the page buffer and
52042 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
52043 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
52071 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
52077 ** open database file (including each in-memory database and each
52092 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
52110 PgHdr1 *pFree; /* List of unused pcache-local pages */
52111 void *pBulk; /* Bulk memory used by pcache-local */
52145 ** reading because (1) most platforms read a 32-bit integer atomically and
52162 # define pcache1EnterMutex(X) assert((X)->mutex==0)
52163 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
52166 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
52167 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
52177 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
52196 while( n-- ){
52198 p->pNext = pcache1.pFree;
52207 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
52208 ** true if pCache->pFree ends up containing one or more free pages.
52215 if( pCache->nMax<3 ) return 0;
52218 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
52220 szBulk = -1024 * (i64)pcache1.nInitPage;
52222 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
52223 szBulk = pCache->szAlloc*(i64)pCache->nMax;
52225 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
52228 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
52230 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
52231 pX->page.pBuf = zBulk;
52232 pX->page.pExtra = &pX[1];
52233 pX->isBulkLocal = 1;
52234 pX->isAnchor = 0;
52235 pX->pNext = pCache->pFree;
52236 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
52237 pCache->pFree = pX;
52238 zBulk += pCache->szAlloc;
52239 }while( --nBulk );
52241 return pCache->pFree!=0;
52260 pcache1.pFree = pcache1.pFree->pNext;
52261 pcache1.nFreeSlot--;
52298 pSlot->pNext = pcache1.pFree;
52345 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52346 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
52347 assert( pCache->pFree!=0 );
52348 p = pCache->pFree;
52349 pCache->pFree = p->pNext;
52350 p->pNext = 0;
52357 assert( pCache->pGroup==&pcache1.grp );
52358 pcache1LeaveMutex(pCache->pGroup);
52362 pPg = pcache1Alloc(pCache->szPage);
52363 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
52370 pPg = pcache1Alloc(pCache->szAlloc);
52374 pcache1EnterMutex(pCache->pGroup);
52378 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
52380 p->page.pBuf = pPg;
52381 p->page.pExtra = &p[1];
52382 p->isBulkLocal = 0;
52383 p->isAnchor = 0;
52384 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
52386 (*pCache->pnPurgeable)++;
52396 pCache = p->pCache;
52397 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
52398 if( p->isBulkLocal ){
52399 p->pNext = pCache->pFree;
52400 pCache->pFree = p;
52402 pcache1Free(p->page.pBuf);
52407 (*pCache->pnPurgeable)--;
52445 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
52466 assert( sqlite3_mutex_held(p->pGroup->mutex) );
52468 nNew = p->nHash*2;
52473 pcache1LeaveMutex(p->pGroup);
52474 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
52476 if( p->nHash ){ sqlite3EndBenignMalloc(); }
52477 pcache1EnterMutex(p->pGroup);
52479 for(i=0; i<p->nHash; i++){
52481 PgHdr1 *pNext = p->apHash[i];
52483 unsigned int h = pPage->iKey % nNew;
52484 pNext = pPage->pNext;
52485 pPage->pNext = apNew[h];
52489 sqlite3_free(p->apHash);
52490 p->apHash = apNew;
52491 p->nHash = nNew;
52498 ** LRU list, then this function is a no-op.
52505 assert( pPage->pLruNext );
52506 assert( pPage->pLruPrev );
52507 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
52508 pPage->pLruPrev->pLruNext = pPage->pLruNext;
52509 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
52510 pPage->pLruNext = 0;
52511 /* pPage->pLruPrev = 0;
52513 assert( pPage->isAnchor==0 );
52514 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
52515 pPage->pCache->nRecyclable--;
52529 PCache1 *pCache = pPage->pCache;
52532 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52533 h = pPage->iKey % pCache->nHash;
52534 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
52535 *pp = (*pp)->pNext;
52537 pCache->nPage--;
52546 PGroup *pGroup = pCache->pGroup;
52548 assert( sqlite3_mutex_held(pGroup->mutex) );
52549 while( pGroup->nPurgeable>pGroup->nMaxPage
52550 && (p=pGroup->lru.pLruPrev)->isAnchor==0
52552 assert( p->pCache->pGroup==pGroup );
52557 if( pCache->nPage==0 && pCache->pBulk ){
52558 sqlite3_free(pCache->pBulk);
52559 pCache->pBulk = pCache->pFree = 0;
52574 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
52576 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
52577 assert( pCache->iMaxKey >= iLimit );
52578 assert( pCache->nHash > 0 );
52579 if( pCache->iMaxKey - iLimit < pCache->nHash ){
52584 h = iLimit % pCache->nHash;
52585 iStop = pCache->iMaxKey % pCache->nHash;
52586 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
52590 h = pCache->nHash/2;
52591 iStop = h - 1;
52596 assert( h<pCache->nHash );
52597 pp = &pCache->apHash[h];
52599 if( pPage->iKey>=iLimit ){
52600 pCache->nPage--;
52601 *pp = pPage->pNext;
52605 pp = &pPage->pNext;
52610 h = (h+1) % pCache->nHash;
52612 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
52629 ** private PGroup (mode-1). pcache1.separateCache is false if the single
52630 ** PGroup in pcache1.grp is used for all page caches (mode-2).
52632 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
52634 ** * Use a unified cache in single-threaded applications that have
52635 ** configured a start-time buffer for use as page-cache memory using
52636 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
52639 ** * Otherwise use separate caches (mode-1)
52693 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
52701 pGroup->mxPinned = 10;
52706 if( pGroup->lru.isAnchor==0 ){
52707 pGroup->lru.isAnchor = 1;
52708 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
52710 pCache->pGroup = pGroup;
52711 pCache->szPage = szPage;
52712 pCache->szExtra = szExtra;
52713 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
52714 pCache->bPurgeable = (bPurgeable ? 1 : 0);
52717 pCache->nMin = 10;
52718 pGroup->nMinPage += pCache->nMin;
52719 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52720 pCache->pnPurgeable = &pGroup->nPurgeable;
52722 pCache->pnPurgeable = &pCache->nPurgeableDummy;
52725 if( pCache->nHash==0 ){
52742 if( pCache->bPurgeable ){
52743 PGroup *pGroup = pCache->pGroup;
52746 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
52747 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
52749 pGroup->nMaxPage += (n - pCache->nMax);
52750 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
52751 pCache->nMax = n;
52752 pCache->n90pct = pCache->nMax*9/10;
52765 if( pCache->bPurgeable ){
52766 PGroup *pGroup = pCache->pGroup;
52769 savedMaxPage = pGroup->nMaxPage;
52770 pGroup->nMaxPage = 0;
52772 pGroup->nMaxPage = savedMaxPage;
52783 pcache1EnterMutex(pCache->pGroup);
52784 n = pCache->nPage;
52785 pcache1LeaveMutex(pCache->pGroup);
52804 PGroup *pGroup = pCache->pGroup;
52808 assert( pCache->nPage >= pCache->nRecyclable );
52809 nPinned = pCache->nPage - pCache->nRecyclable;
52810 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
52811 assert( pCache->n90pct == pCache->nMax*9/10 );
52813 nPinned>=pGroup->mxPinned
52814 || nPinned>=pCache->n90pct
52815 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
52820 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
52821 assert( pCache->nHash>0 && pCache->apHash );
52824 if( pCache->bPurgeable
52825 && !pGroup->lru.pLruPrev->isAnchor
52826 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
52829 pPage = pGroup->lru.pLruPrev;
52833 pOther = pPage->pCache;
52834 if( pOther->szAlloc != pCache->szAlloc ){
52838 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
52850 unsigned int h = iKey % pCache->nHash;
52851 pCache->nPage++;
52852 pPage->iKey = iKey;
52853 pPage->pNext = pCache->apHash[h];
52854 pPage->pCache = pCache;
52855 pPage->pLruNext = 0;
52856 /* pPage->pLruPrev = 0;
52858 *(void **)pPage->page.pExtra = 0;
52859 pCache->apHash[h] = pPage;
52860 if( iKey>pCache->iMaxKey ){
52861 pCache->iMaxKey = iKey;
52877 ** For a non-purgeable cache (a cache used as the storage for an in-memory
52880 ** a non-purgeable cache.
52923 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
52935 pPage = pCache->apHash[iKey % pCache->nHash];
52936 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
52964 pcache1EnterMutex(pCache->pGroup);
52966 assert( pPage==0 || pCache->iMaxKey>=iKey );
52967 pcache1LeaveMutex(pCache->pGroup);
52981 assert( pCache->bPurgeable || createFlag!=1 );
52982 assert( pCache->bPurgeable || pCache->nMin==0 );
52983 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
52984 assert( pCache->nMin==0 || pCache->bPurgeable );
52985 assert( pCache->nHash>0 );
52987 if( pCache->pGroup->mutex ){
53009 PGroup *pGroup = pCache->pGroup;
53011 assert( pPage->pCache==pCache );
53017 assert( pPage->pLruNext==0 );
53020 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
53024 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
53025 pPage->pLruPrev = &pGroup->lru;
53026 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
53028 pCache->nRecyclable++;
53031 pcache1LeaveMutex(pCache->pGroup);
53047 assert( pPage->iKey==iOld );
53048 assert( pPage->pCache==pCache );
53050 pcache1EnterMutex(pCache->pGroup);
53052 h = iOld%pCache->nHash;
53053 pp = &pCache->apHash[h];
53055 pp = &(*pp)->pNext;
53057 *pp = pPage->pNext;
53059 h = iNew%pCache->nHash;
53060 pPage->iKey = iNew;
53061 pPage->pNext = pCache->apHash[h];
53062 pCache->apHash[h] = pPage;
53063 if( iNew>pCache->iMaxKey ){
53064 pCache->iMaxKey = iNew;
53067 pcache1LeaveMutex(pCache->pGroup);
53079 pcache1EnterMutex(pCache->pGroup);
53080 if( iLimit<=pCache->iMaxKey ){
53082 pCache->iMaxKey = iLimit-1;
53084 pcache1LeaveMutex(pCache->pGroup);
53094 PGroup *pGroup = pCache->pGroup;
53095 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
53097 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
53098 assert( pGroup->nMaxPage >= pCache->nMax );
53099 pGroup->nMaxPage -= pCache->nMax;
53100 assert( pGroup->nMinPage >= pCache->nMin );
53101 pGroup->nMinPage -= pCache->nMin;
53102 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
53105 sqlite3_free(pCache->pBulk);
53106 sqlite3_free(pCache->apHash);
53166 && p->isAnchor==0
53168 nFree += pcache1MemSize(p->page.pBuf);
53195 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
53255 ** a non-zero batch number, it will see all prior INSERTs.
53283 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
53341 p->pChunk = 0;
53342 p->db = db;
53343 p->pEntry = 0;
53344 p->pLast = 0;
53345 p->pForest = 0;
53346 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
53347 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
53348 p->rsFlags = ROWSET_SORTED;
53349 p->iBatch = 0;
53362 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
53363 pNextChunk = pChunk->pNextChunk;
53364 sqlite3DbFree(p->db, pChunk);
53366 p->pChunk = 0;
53367 p->nFresh = 0;
53368 p->pEntry = 0;
53369 p->pLast = 0;
53370 p->pForest = 0;
53371 p->rsFlags = ROWSET_SORTED;
53381 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
53389 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
53394 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
53398 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
53402 pNew->pNextChunk = p->pChunk;
53403 p->pChunk = pNew;
53404 p->pFresh = pNew->aEntry;
53405 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
53407 p->nFresh--;
53408 return p->pFresh++;
53422 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
53426 pEntry->v = rowid;
53427 pEntry->pRight = 0;
53428 pLast = p->pLast;
53430 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
53433 p->rsFlags &= ~ROWSET_SORTED;
53435 pLast->pRight = pEntry;
53437 p->pEntry = pEntry;
53439 p->pLast = pEntry;
53458 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
53459 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
53460 if( pA->v<=pB->v ){
53461 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
53462 pA = pA->pRight;
53464 pTail->pRight = pB;
53468 pTail = pTail->pRight = pB;
53469 pB = pB->pRight;
53471 pTail->pRight = pA;
53489 pNext = pIn->pRight;
53490 pIn->pRight = 0;
53518 if( pIn->pLeft ){
53520 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
53521 p->pRight = pIn;
53525 if( pIn->pRight ){
53526 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
53530 assert( (*ppLast)->pRight==0 );
53553 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
53557 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
53561 pLeft = rowSetNDeepTree(ppList, iDepth-1);
53563 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
53568 p->pLeft = pLeft;
53569 *ppList = p->pRight;
53570 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
53573 *ppList = p->pRight;
53574 p->pLeft = p->pRight = 0;
53590 pList = p->pRight;
53591 p->pLeft = p->pRight = 0;
53595 pList = p->pRight;
53596 p->pLeft = pLeft;
53597 p->pRight = rowSetNDeepTree(&pList, iDepth);
53617 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
53620 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53621 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53622 p->pEntry = rowSetEntrySort(p->pEntry);
53624 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
53628 if( p->pEntry ){
53629 *pRowid = p->pEntry->v;
53630 p->pEntry = p->pEntry->pRight;
53631 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
53646 ** on pRowSet->pEntry, then sort those entries into the forest at
53647 ** pRowSet->pForest so that they can be tested.
53653 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
53658 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
53659 p = pRowSet->pEntry;
53661 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
53662 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
53666 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53667 ppPrevTree = &pTree->pRight;
53668 if( pTree->pLeft==0 ){
53669 pTree->pLeft = rowSetListToTree(p);
53673 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
53674 pTree->pLeft = 0;
53681 pTree->v = 0;
53682 pTree->pRight = 0;
53683 pTree->pLeft = rowSetListToTree(p);
53686 pRowSet->pEntry = 0;
53687 pRowSet->pLast = 0;
53688 pRowSet->rsFlags |= ROWSET_SORTED;
53690 pRowSet->iBatch = iBatch;
53696 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
53697 p = pTree->pLeft;
53699 if( p->v<iRowid ){
53700 p = p->pRight;
53701 }else if( p->v>iRowid ){
53702 p = p->pLeft;
53748 ** This header file defines the interface to the write-ahead logging
53788 /* Connection to a write-ahead log (WAL) file.
53793 /* Open and close a connection to a write-ahead log. */
53801 ** snapshot is like a read-transaction. It is the state of the database
53810 /* Read a page from the write-ahead log, if it is present. */
53837 Wal *pWal, /* Write-ahead log connection */
53861 /* Return true if the argument is non-NULL and the WAL module is using
53862 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
53863 ** WAL module is using shared-memory, return false.
53877 ** stored in each frame (i.e. the db page-size when the WAL was created).
53944 ** (6) If a super-journal file is used, then all writes to the database file
53945 ** are synced prior to the super-journal being deleted.
53961 ** method is a no-op, but that does not change the fact the SQLite will
53972 ** (11) A database file is well-formed at the beginning and at the conclusion
53996 ** to print out file-descriptors.
53999 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
54002 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
54010 ** OPEN <------+------+
54013 ** +---------> READER-------+ |
54016 ** |<-------WRITER_LOCKED------> ERROR
54019 ** |<------WRITER_CACHEMOD-------->|
54022 ** |<-------WRITER_DBMOD---------->|
54025 ** +<------WRITER_FINISHED-------->+
54030 ** OPEN -> READER [sqlite3PagerSharedLock]
54031 ** READER -> OPEN [pager_unlock]
54033 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
54034 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
54035 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
54036 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
54037 ** WRITER_*** -> READER [pager_end_transaction]
54039 ** WRITER_*** -> ERROR [pager_error]
54040 ** ERROR -> OPEN [pager_unlock]
54046 ** state - the file may or may not be locked and the database size is
54056 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
54057 ** was) in exclusive-locking mode, a user-level read transaction is
54061 ** it opens a read-transaction on the database and returns to state
54062 ** OPEN after the read-transaction is completed. However a connection
54064 ** this state even after the read-transaction is closed. The only way
54068 ** * A read transaction may be active (but a write-transaction cannot).
54070 ** * The dbSize variable may be trusted (even if a user-level read
54074 ** * Even if a read-transaction is not open, it is guaranteed that
54075 ** there is no hot-journal in the file-system.
54079 ** The pager moves to this state from READER when a write-transaction
54081 ** required to start a write-transaction are held, but no actual
54096 ** * If the connection is open in rollback-mode, a RESERVED or greater
54098 ** * If the connection is open in WAL-mode, a WAL write transaction
54137 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
54153 ** The ERROR state is entered when an IO or disk-full error (including
54155 ** difficult to be sure that the in-memory pager state (cache contents,
54156 ** db size etc.) are consistent with the contents of the file-system.
54158 ** Temporary pager files may enter the ERROR state, but in-memory pagers
54162 ** the contents of the page-cache may be left in an inconsistent state.
54174 ** page-cache and any other in-memory state at the same time. Everything
54175 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
54176 ** when a read-transaction is next opened on the pager (transitioning
54192 ** In other cases, the error is returned to the b-tree layer. The b-tree
54196 ** Condition (3) is necessary because it can be triggered by a read-only
54198 ** code were simply returned to the user, the b-tree layer would not
54200 ** read-only statement cannot leave the pager in an internally inconsistent
54206 ** * The pager is not an in-memory pager.
54216 ** state. There are two exceptions: immediately after exclusive-mode has
54232 ** following locking-states, according to the lock currently held on
54240 ** pagerUnlockDb() take a conservative approach - eLock is always updated
54251 ** from ERROR to OPEN state. At this point there may be a hot-journal file
54252 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
54256 ** of hot-journal detection.
54262 ** a hot-journal may be mistaken for a journal being created by an active
54269 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
54270 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
54271 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
54296 ** set to 0. If a journal-header is written into the main journal while
54299 ** journal before the journal-header. This is required during savepoint
54308 Pgno iSubRec; /* Index of first record in sub-journal */
54333 ** For a real on-disk database, the current lock held on the database file -
54336 ** For a temporary or in-memory database (neither of which require any
54348 ** This boolean variable is used to make sure that the change-counter
54349 ** (the 4-byte header field at byte offset 24 of the database file) is
54352 ** It is set to true when the change-counter field is updated, which
54357 ** updating the change-counter is omitted for the current transaction.
54360 ** need only update the change-counter once, for the first transaction
54366 ** (or may not) specify a super-journal name to be written into the
54369 ** Whether or not a journal file contains a super-journal pointer affects
54372 ** If a journal file does not contain a super-journal pointer, it is
54374 ** it does contain a super-journal pointer the journal file is finalized
54378 ** Journal files that contain super-journal pointers cannot be finalized
54379 ** simply by overwriting the first journal-header with zeroes, as the
54380 ** super-journal pointer could interfere with hot-journal rollback of any
54390 ** This variables control the behavior of cache-spills (calls made by
54392 ** to the file-system in order to free up memory).
54404 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
54405 ** is larger than the database page-size in order to prevent a journal sync
54410 ** This is a boolean variable. If true, then any required sub-journal
54411 ** is opened as an in-memory journal file. If false, then in-memory
54412 ** sub-journals are only used for in-memory pager files.
54415 ** write-transaction is opened.
54426 ** is not an integer multiple of the page-size, the value stored in
54427 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
54429 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
54432 ** During a write-transaction, if pages with page-numbers greater than
54443 ** Throughout a write-transaction, dbFileSize contains the size of
54445 ** write-transaction is first opened, and updated when VFS calls are made
54465 ** write-transaction is opened (at the same time as dbFileSize and
54468 ** size-hint passed to the method call. See pager_write_pagelist() for
54476 ** sub-codes.
54502 u8 readOnly; /* True for a read-only database */
54504 u8 memVfs; /* VFS-implemented memory database */
54517 u8 changeCountDone; /* Set after incrementing the change-counter */
54518 u8 setSuper; /* Super-jrnl name is written into jrnl */
54519 u8 doNotSpill; /* Do not spill the cache when non-zero */
54520 u8 subjInMemory; /* True to use in-memory sub-journals */
54528 int nRec; /* Pages journalled since last j-header written */
54529 u32 cksumInit; /* Quasi-random value added to every checksum */
54530 u32 nSubRec; /* Number of records written to sub-journal */
54534 sqlite3_file *sjfd; /* File descriptor for sub-journal */
54547 ** End of the routinely-changing class members
54550 u16 nExtra; /* Add this many bytes to each in-memory page */
54571 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
54572 char *zWal; /* File name for write-ahead log */
54589 ** a non-testing build. These variables are not thread-safe.
54608 ** written, semi-random garbage data might appear in the journal
54615 ** of a 32-bit checksum on each page of data. The checksum covers both
54616 ** the page number and the pPager->pageSize bytes of data for the page.
54617 ** This cksum is initialized to a 32-bit random value that appears in the
54633 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
54639 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
54642 ** The macro MEMDB is true if we are dealing with an in-memory database.
54650 # define MEMDB pPager->memDb
54655 ** interfaces to access the database using memory-mapped I/O.
54658 # define USEFETCH(x) ((x)->bUseFetch)
54665 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
54669 ** if( isOpen(pPager->jfd) ){ ...
54673 ** if( pPager->jfd->pMethods ){ ...
54675 #define isOpen(pFd) ((pFd)->pMethods!=0)
54680 ** by the b-tree layer. This is the case if:
54687 if( pPager->fd->pMethods==0 ) return 0;
54688 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
54690 if( pPager->pWal ){
54693 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
54702 # define pagerUseWal(x) ((x)->pWal!=0)
54724 assert( p->eState==PAGER_OPEN
54725 || p->eState==PAGER_READER
54726 || p->eState==PAGER_WRITER_LOCKED
54727 || p->eState==PAGER_WRITER_CACHEMOD
54728 || p->eState==PAGER_WRITER_DBMOD
54729 || p->eState==PAGER_WRITER_FINISHED
54730 || p->eState==PAGER_ERROR
54733 /* Regardless of the current state, a temp-file connection always behaves
54735 ** the change-counter field, so the changeCountDone flag is always set.
54737 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
54738 assert( p->tempFile==0 || pPager->changeCountDone );
54740 /* If the useJournal flag is clear, the journal-mode must be "OFF".
54741 ** And if the journal-mode is "OFF", the journal file must not be open.
54743 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
54744 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
54746 /* Check that MEMDB implies noSync. And an in-memory journal. Since
54747 ** this means an in-memory pager performs no IO at all, it cannot encounter
54749 ** a journal file. (although the in-memory journal implementation may
54751 ** is therefore not possible for an in-memory pager to enter the ERROR
54755 assert( !isOpen(p->fd) );
54756 assert( p->noSync );
54757 assert( p->journalMode==PAGER_JOURNALMODE_OFF
54758 || p->journalMode==PAGER_JOURNALMODE_MEMORY
54760 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
54767 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
54768 assert( p->eLock!=PENDING_LOCK );
54770 switch( p->eState ){
54773 assert( pPager->errCode==SQLITE_OK );
54774 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
54778 assert( pPager->errCode==SQLITE_OK );
54779 assert( p->eLock!=UNKNOWN_LOCK );
54780 assert( p->eLock>=SHARED_LOCK );
54784 assert( p->eLock!=UNKNOWN_LOCK );
54785 assert( pPager->errCode==SQLITE_OK );
54787 assert( p->eLock>=RESERVED_LOCK );
54789 assert( pPager->dbSize==pPager->dbOrigSize );
54790 assert( pPager->dbOrigSize==pPager->dbFileSize );
54791 assert( pPager->dbOrigSize==pPager->dbHintSize );
54792 assert( pPager->setSuper==0 );
54796 assert( p->eLock!=UNKNOWN_LOCK );
54797 assert( pPager->errCode==SQLITE_OK );
54804 assert( p->eLock>=RESERVED_LOCK );
54805 assert( isOpen(p->jfd)
54806 || p->journalMode==PAGER_JOURNALMODE_OFF
54807 || p->journalMode==PAGER_JOURNALMODE_WAL
54810 assert( pPager->dbOrigSize==pPager->dbFileSize );
54811 assert( pPager->dbOrigSize==pPager->dbHintSize );
54815 assert( p->eLock==EXCLUSIVE_LOCK );
54816 assert( pPager->errCode==SQLITE_OK );
54818 assert( p->eLock>=EXCLUSIVE_LOCK );
54819 assert( isOpen(p->jfd)
54820 || p->journalMode==PAGER_JOURNALMODE_OFF
54821 || p->journalMode==PAGER_JOURNALMODE_WAL
54822 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54824 assert( pPager->dbOrigSize<=pPager->dbHintSize );
54828 assert( p->eLock==EXCLUSIVE_LOCK );
54829 assert( pPager->errCode==SQLITE_OK );
54831 assert( isOpen(p->jfd)
54832 || p->journalMode==PAGER_JOURNALMODE_OFF
54833 || p->journalMode==PAGER_JOURNALMODE_WAL
54834 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54843 assert( pPager->errCode!=SQLITE_OK );
54844 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
54877 , p->zFilename
54878 , p->eState==PAGER_OPEN ? "OPEN" :
54879 p->eState==PAGER_READER ? "READER" :
54880 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
54881 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
54882 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
54883 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
54884 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
54885 , (int)p->errCode
54886 , p->eLock==NO_LOCK ? "NO_LOCK" :
54887 p->eLock==RESERVED_LOCK ? "RESERVED" :
54888 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
54889 p->eLock==SHARED_LOCK ? "SHARED" :
54890 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
54891 , p->exclusiveMode ? "exclusive" : "normal"
54892 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
54893 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
54894 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
54895 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
54896 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
54897 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
54898 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
54899 , p->journalOff, p->journalHdr
54900 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
54919 if( pPager->errCode ){
54920 pPager->xGet = getPageError;
54923 pPager->xGet = getPageMMap;
54926 pPager->xGet = getPageNormal;
54931 ** Return true if it is necessary to write page *pPg into the sub-journal.
54932 ** A page needs to be written into the sub-journal if there exists one
54935 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
54936 ** * The bit corresponding to the page-number is not set in
54940 Pager *pPager = pPg->pPager;
54942 Pgno pgno = pPg->pgno;
54944 for(i=0; i<pPager->nSavepoint; i++){
54945 p = &pPager->aSavepoint[i];
54946 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
54947 for(i=i+1; i<pPager->nSavepoint; i++){
54948 pPager->aSavepoint[i].bTruncateOnRelease = 0;
54961 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
54966 ** Read a 32-bit integer from the given file descriptor. Store the integer
54970 ** All values are stored on disk as big-endian.
54982 ** Write a 32-bit integer into a string buffer in big-endian byte order.
54988 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
55009 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
55012 if( isOpen(pPager->fd) ){
55013 assert( pPager->eLock>=eLock );
55014 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
55015 if( pPager->eLock!=UNKNOWN_LOCK ){
55016 pPager->eLock = (u8)eLock;
55020 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
55038 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
55039 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
55040 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
55041 pPager->eLock = (u8)eLock;
55049 ** This function determines whether or not the atomic-write or
55050 ** atomic-batch-write optimizations can be used with this pager. The
55051 ** atomic-write optimization can be used if:
55061 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
55062 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
55074 assert( isOpen(pPager->fd) );
55075 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
55081 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
55082 return -1;
55088 int nSector = pPager->sectorSize;
55089 int szPage = pPager->pageSize;
55111 ** Return a 32-bit hash of the page data for pPage.
55122 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
55125 pPage->pageHash = pager_pagehash(pPage);
55131 ** that the page is either dirty or still matches the calculated page-hash.
55135 Pager *pPager = pPg->pPager;
55136 assert( pPager->eState!=PAGER_ERROR );
55137 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
55149 ** This function attempts to read a super-journal file name from the
55152 ** used to store a super-journal file name at the end of a journal file.
55156 ** enough space to write the super-journal name). If the super-journal
55158 ** nul-terminator), then this is handled as if no super-journal name
55161 ** If a super-journal file name is present at the end of the journal
55163 ** nul-terminator byte is appended to the buffer following the
55164 ** super-journal file name.
55166 ** If it is determined that no super-journal file name is present
55174 u32 len; /* Length in bytes of super-journal name */
55183 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
55185 || len>szJ-16
55187 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
55188 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
55190 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
55195 /* See if the checksum matches the super-journal name */
55197 cksum -= zSuper[u];
55201 ** containing the super-journal filename is corrupted. This means
55203 ** super-journal filename.
55215 ** following the value in pPager->journalOff, assuming a sector
55216 ** size of pPager->sectorSize bytes.
55221 ** ---------------------------------------
55230 i64 c = pPager->journalOff;
55232 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
55236 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
55243 ** This function is a no-op if the journal file has not been written to
55246 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
55248 ** zero the 28-byte header at the start of the journal file. In either case,
55249 ** if the pager is not in no-sync mode, sync the journal file immediately
55252 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
55263 assert( isOpen(pPager->jfd) );
55264 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
55265 if( pPager->journalOff ){
55266 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
55270 rc = sqlite3OsTruncate(pPager->jfd, 0);
55273 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
55275 if( rc==SQLITE_OK && !pPager->noSync ){
55276 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
55287 rc = sqlite3OsFileSize(pPager->jfd, &sz);
55289 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
55302 ** - 8 bytes: Magic identifying journal format.
55303 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
55304 ** - 4 bytes: Random number used for page hash.
55305 ** - 4 bytes: Initial database page count.
55306 ** - 4 bytes: Sector size used by the process that wrote this journal.
55307 ** - 4 bytes: Database page size.
55309 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
55313 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
55314 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
55318 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
55328 for(ii=0; ii<pPager->nSavepoint; ii++){
55329 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
55330 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
55334 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
55337 ** Write the nRec Field - the number of page records that follow this
55340 ** if in full-sync mode), the zero is overwritten with the true number
55350 ** * When the pager is in no-sync mode. Corruption can follow a
55356 assert( isOpen(pPager->fd) || pPager->noSync );
55357 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
55358 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
55366 /* The random check-hash initializer */
55367 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
55368 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
55370 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
55372 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
55375 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
55383 nHeader-(sizeof(aJournalMagic)+20));
55394 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
55397 ** The loop is required here in case the sector-size is larger than the
55403 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
55404 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
55405 assert( pPager->journalHdr <= pPager->journalOff );
55406 pPager->journalOff += nHeader;
55416 ** pPager->journalOff. See comments above function writeJournalHdr() for
55421 ** database before the transaction began, in pages. Also, pPager->cksumInit
55440 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
55446 pPager->journalOff = journalHdrOffset(pPager);
55447 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
55450 iHdrOff = pPager->journalOff;
55457 if( isHot || iHdrOff!=pPager->journalHdr ){
55458 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
55467 /* Read the first three 32-bit fields of the journal header: The nRec
55468 ** field, the checksum-initializer and the database size at the start
55471 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
55472 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
55473 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
55478 if( pPager->journalOff==0 ){
55479 u32 iPageSize; /* Page-size field of journal header */
55480 u32 iSectorSize; /* Sector-size field of journal header */
55482 /* Read the page-size and sector-size journal header fields. */
55483 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
55484 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
55489 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
55494 iPageSize = pPager->pageSize;
55497 /* Check that the values read from the page-size and sector-size fields
55504 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
55506 /* If the either the page-size or sector-size in the journal-header is
55507 ** invalid, then the process that wrote the journal-header must have
55514 /* Update the page-size to match the value read from the journal.
55518 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
55521 /* Update the assumed sector-size to match the value used by
55527 pPager->sectorSize = iSectorSize;
55530 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
55536 ** Write the supplied super-journal name into the journal file for pager
55537 ** pPager at the current location. The super-journal name must be the last
55538 ** thing written to a journal file. If the pager is in full-sync mode, the
55543 ** + N bytes: super-journal filename in utf-8.
55544 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
55545 ** + 4 bytes: super-journal name checksum.
55548 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
55549 ** name, where each byte is interpreted as a signed 8-bit integer.
55552 ** this call is a no-op.
55561 assert( pPager->setSuper==0 );
55565 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
55566 || !isOpen(pPager->jfd)
55570 pPager->setSuper = 1;
55571 assert( pPager->journalHdr <= pPager->journalOff );
55578 /* If in full-sync mode, advance to the next disk sector before writing
55579 ** the super-journal name. This is in case the previous page written to
55582 if( pPager->fullSync ){
55583 pPager->journalOff = journalHdrOffset(pPager);
55585 iHdrOff = pPager->journalOff;
55587 /* Write the super-journal data to the end of the journal file. If
55590 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
55591 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
55592 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
55593 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
55594 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
55599 pPager->journalOff += (nSuper+20);
55601 /* If the pager is in peristent-journal mode, then the physical
55602 ** journal-file may extend past the end of the super-journal name
55604 ** dangerous because the code to rollback a hot-journal file
55605 ** will not be able to find the super-journal name to determine
55611 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
55612 && jrnlSize>pPager->journalOff
55614 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
55620 ** Discard the entire contents of the in-memory page-cache.
55623 pPager->iDataVersion++;
55624 sqlite3BackupRestart(pPager->pBackup);
55625 sqlite3PcacheClear(pPager->pPCache);
55629 ** Return the pPager->iDataVersion value
55632 return pPager->iDataVersion;
55637 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
55642 for(ii=0; ii<pPager->nSavepoint; ii++){
55643 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
55645 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
55646 sqlite3OsClose(pPager->sjfd);
55648 sqlite3_free(pPager->aSavepoint);
55649 pPager->aSavepoint = 0;
55650 pPager->nSavepoint = 0;
55651 pPager->nSubRec = 0;
55663 for(ii=0; ii<pPager->nSavepoint; ii++){
55664 PagerSavepoint *p = &pPager->aSavepoint[ii];
55665 if( pgno<=p->nOrig ){
55666 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
55675 ** This function is a no-op if the pager is in exclusive mode and not
55679 ** If the pager is not in exclusive-access mode, the database file is
55680 ** completely unlocked. If the file is unlocked and the file-system does
55686 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
55687 ** or not, any journal file left in the file-system will be treated
55688 ** as a hot-journal and rolled back the next time a read-transaction
55693 assert( pPager->eState==PAGER_READER
55694 || pPager->eState==PAGER_OPEN
55695 || pPager->eState==PAGER_ERROR
55698 sqlite3BitvecDestroy(pPager->pInJournal);
55699 pPager->pInJournal = 0;
55703 assert( !isOpen(pPager->jfd) );
55704 sqlite3WalEndReadTransaction(pPager->pWal);
55705 pPager->eState = PAGER_OPEN;
55706 }else if( !pPager->exclusiveMode ){
55708 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
55722 || 1!=(pPager->journalMode & 5)
55724 sqlite3OsClose(pPager->jfd);
55733 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
55734 pPager->eLock = UNKNOWN_LOCK;
55738 ** without clearing the error code. This is intentional - the error
55741 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
55742 pPager->eState = PAGER_OPEN;
55748 ** normal and exclusive-locking mode.
55750 assert( pPager->errCode==SQLITE_OK || !MEMDB );
55751 if( pPager->errCode ){
55752 if( pPager->tempFile==0 ){
55754 pPager->changeCountDone = 0;
55755 pPager->eState = PAGER_OPEN;
55757 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
55759 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
55760 pPager->errCode = SQLITE_OK;
55764 pPager->journalOff = 0;
55765 pPager->journalHdr = 0;
55766 pPager->setSuper = 0;
55773 ** the error-code about to be returned by a pager API function. The
55777 ** IOERR sub-codes, the pager enters the ERROR state and the error code
55781 ** The ERROR state indicates that the contents of the pager-cache
55783 ** the contents of the pager-cache. If a transaction was active when
55786 ** it were a hot-journal).
55792 pPager->errCode==SQLITE_FULL ||
55793 pPager->errCode==SQLITE_OK ||
55794 (pPager->errCode & 0xff)==SQLITE_IOERR
55797 pPager->errCode = rc;
55798 pPager->eState = PAGER_ERROR;
55814 ** * For non-TEMP databases, always sync to disk. This is necessary
55823 if( pPager->tempFile==0 ) return 1;
55825 if( !isOpen(pPager->fd) ) return 0;
55826 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
55832 ** after rollback of a hot-journal, or if an error occurs while opening
55833 ** the journal file or writing the very first journal-header of a
55838 ** exclusive than a RESERVED lock, it is a no-op.
55844 ** transaction. Nor will it be considered to be a hot-journal by this
55847 ** the current journal-mode (Pager.journalMode value), as follows:
55851 ** in-memory journal.
55870 ** If running in non-exclusive rollback mode, the lock on the file is
55888 ** is no write-transaction active but a RESERVED or greater lock is
55891 ** 1. After a successful hot-journal rollback, it is called with
55896 ** read-transaction, this function is called with eState==PAGER_READER
55897 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
55900 assert( pPager->eState!=PAGER_ERROR );
55901 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
55906 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
55907 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
55909 if( isOpen(pPager->jfd) ){
55913 if( sqlite3JournalIsInMemory(pPager->jfd) ){
55914 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
55915 sqlite3OsClose(pPager->jfd);
55916 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
55917 if( pPager->journalOff==0 ){
55920 rc = sqlite3OsTruncate(pPager->jfd, 0);
55921 if( rc==SQLITE_OK && pPager->fullSync ){
55927 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
55930 pPager->journalOff = 0;
55931 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
55932 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
55934 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
55935 pPager->journalOff = 0;
55938 ** a hot-journal was just rolled back. In this case the journal
55940 ** the database file, it will do so using an in-memory journal.
55942 int bDelete = !pPager->tempFile;
55943 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
55944 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
55945 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
55946 || pPager->journalMode==PAGER_JOURNALMODE_WAL
55948 sqlite3OsClose(pPager->jfd);
55950 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
55956 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
55957 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
55960 p->pageHash = 0;
55966 sqlite3BitvecDestroy(pPager->pInJournal);
55967 pPager->pInJournal = 0;
55968 pPager->nRec = 0;
55971 sqlite3PcacheCleanAll(pPager->pPCache);
55973 sqlite3PcacheClearWritable(pPager->pPCache);
55975 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
55979 /* Drop the WAL write-lock, if any. Also, if the connection was in
55983 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
55985 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
55986 /* This branch is taken when committing a transaction in rollback-journal
55992 assert( pPager->eLock==EXCLUSIVE_LOCK );
55993 rc = pager_truncate(pPager, pPager->dbSize);
55997 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
56001 if( !pPager->exclusiveMode
56002 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
56006 pPager->eState = PAGER_READER;
56007 pPager->setSuper = 0;
56018 ** call to pager_unlock() will discard all in-memory pages, unlock
56020 ** means that there is a hot-journal left in the file-system, the next
56030 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
56032 if( pPager->eState>=PAGER_WRITER_LOCKED ){
56036 }else if( !pPager->exclusiveMode ){
56037 assert( pPager->eState==PAGER_READER );
56045 ** Parameter aData must point to a buffer of pPager->pageSize bytes
56047 ** page of data and the current value of pPager->cksumInit.
56050 ** random initial value (pPager->cksumInit) and every 200th byte
56051 ** of the page data, starting with byte offset (pPager->pageSize%200).
56052 ** Each byte is interpreted as an 8-bit unsigned integer.
56064 u32 cksum = pPager->cksumInit; /* Checksum value to return */
56065 int i = pPager->pageSize-200; /* Loop counter */
56068 i -= 200;
56075 ** from the sub-journal (if isMainJrnl==0) and playback that page.
56079 ** The main rollback journal uses checksums - the statement journal does
56082 ** If the page number of the page record read from the (sub-)journal file
56092 ** If the page record is successfully read from the (sub-)journal file
56094 ** while reading the record from the (sub-)journal file or while writing
56096 ** is successfully read from the (sub-)journal file but appears to be
56100 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
56114 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
56127 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
56128 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
56130 aData = pPager->pTmpSpace;
56136 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
56137 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
56138 ** only reads from the main journal, not the sub-journal.
56140 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
56141 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
56143 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
56145 /* Read the page number and page data from the journal or sub-journal
56148 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
56151 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
56153 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
56164 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
56168 rc = read32bits(jfd, (*pOffset)-4, &cksum);
56184 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
56185 pPager->nReserve = ((u8*)aData)[20];
56192 ** An exception to the above rule: If the database is in no-sync mode
56202 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
56219 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
56229 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
56231 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
56232 (isMainJrnl?"main-journal":"sub-journal")
56235 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
56237 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
56239 if( isOpen(pPager->fd)
56240 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56243 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
56244 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
56248 ** This is usually safe even for an encrypted database - as the data
56250 ** is if the data was just read from an in-memory sub-journal. In that
56253 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
56255 if( pgno>pPager->dbFileSize ){
56256 pPager->dbFileSize = pgno;
56258 if( pPager->pBackup ){
56259 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
56263 ** the database and the page is not in-memory, there is a potential
56264 ** problem. When the page is next fetched by the b-tree layer, it
56270 ** if the page is on the free-list at the start of the transaction, then
56273 ** The solution is to add an in-memory page to the cache containing
56274 ** the data just read from the sub-journal. Mark the page as dirty
56275 ** and if the pager requires a journal-sync, then mark the page as
56276 ** requiring a journal-sync before it is written.
56279 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
56280 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
56282 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
56283 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
56295 pData = pPg->pData;
56296 memcpy(pData, (u8*)aData, pPager->pageSize);
56297 pPager->xReiniter(pPg);
56307 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
56315 ** Parameter zSuper is the name of a super-journal file. A single journal
56316 ** file that referred to the super-journal file has just been rolled back.
56317 ** This routine checks if it is possible to delete the super-journal file,
56323 ** When a super-journal file is created, it is populated with the names
56324 ** of all of its child journals, one after another, formatted as utf-8
56326 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
56329 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
56331 ** A super-journal file may only be deleted once all of its child
56334 ** This function reads the contents of the super-journal file into
56339 ** * if the child journal contains a reference to super-journal
56345 ** the file-system using sqlite3OsDelete().
56353 ** the entire contents of the super-journal file. This could be
56354 ** a couple of kilobytes or so - potentially larger than the page
56358 sqlite3_vfs *pVfs = pPager->pVfs;
56360 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
56361 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
56362 char *zSuperJournal = 0; /* Contents of super-journal file */
56363 i64 nSuperJournal; /* Size of super-journal file */
56365 char *zSuperPtr; /* Space to hold super-journal filename */
56370 ** If successful, open the super-journal file for reading.
56372 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
56379 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
56383 /* Load the entire super-journal file into space obtained from
56385 ** sufficient space (in zSuperPtr) to hold the names of super-journal
56386 ** files extracted from regular rollback-journals.
56390 nSuperPtr = pVfs->mxPathname+1;
56405 while( (zJournal-zSuperJournal)<nSuperJournal ){
56412 /* One of the journals pointed to by the super-journal exists.
56413 ** Open it and check if it points at the super-journal. If
56414 ** so, return without deleting the super-journal file.
56434 /* We have a match. Do not delete the super-journal file. */
56457 ** file in the file-system. This only happens when committing a transaction,
56458 ** or rolling back a transaction (including rolling back a hot-journal).
56461 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
56462 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
56477 assert( pPager->eState!=PAGER_ERROR );
56478 assert( pPager->eState!=PAGER_READER );
56480 if( isOpen(pPager->fd)
56481 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56484 int szPage = pPager->pageSize;
56485 assert( pPager->eLock==EXCLUSIVE_LOCK );
56487 rc = sqlite3OsFileSize(pPager->fd, ¤tSize);
56491 rc = sqlite3OsTruncate(pPager->fd, newSize);
56493 char *pTmp = pPager->pTmpSpace;
56495 testcase( (newSize-szPage) == currentSize );
56496 testcase( (newSize-szPage) > currentSize );
56497 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
56498 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
56501 pPager->dbFileSize = nPage;
56509 ** Return a sanitized version of the sector-size of OS file pFile. The
56528 ** super-journal pointers within created journal files.
56532 ** Otherwise, for non-temporary files, the effective sector size is
56539 ** pPager->sectorSize is to define the "blast radius" of bytes that
56547 assert( isOpen(pPager->fd) || pPager->tempFile );
56549 if( pPager->tempFile
56550 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
56556 pPager->sectorSize = 512;
56558 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
56569 ** (2) 4 byte big-endian integer which is the number of valid page records
56572 ** (3) 4 byte big-endian integer which is the initial value for the
56576 ** (5) 4 byte big-endian integer which is the sector size. The header
56578 ** (6) 4 byte big-endian integer which is the page size.
56582 ** + pPager->pageSize bytes of data.
56599 ** no-sync option for the journal. A power failure could lead to corruption
56603 ** If the file opened as the journal file is not a well-formed
56609 ** If an I/O or malloc() error occurs, the journal-file is not deleted
56620 sqlite3_vfs *pVfs = pPager->pVfs;
56627 char *zSuper = 0; /* Name of super-journal file if any */
56630 u32 savedPageSize = pPager->pageSize;
56635 assert( isOpen(pPager->jfd) );
56636 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
56641 /* Read the super-journal name from the journal, if it is present.
56642 ** If a super-journal file name is specified, but the file is not
56648 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
56652 zSuper = pPager->pTmpSpace;
56653 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56661 pPager->journalOff = 0;
56683 ** working in no-sync mode. This means that the rest of the journal
56688 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
56689 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
56707 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
56708 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
56714 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
56719 pPager->dbSize = mxPg;
56720 if( pPager->mxPgno<mxPg ){
56721 pPager->mxPgno = mxPg;
56733 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
56738 pPager->journalOff = szJ;
56764 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
56768 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
56772 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
56776 ** malloc error that occurred after the change-counter was updated but
56777 ** before the transaction was committed, then the change-counter
56780 ** update the change-counter at all. This may lead to cache inconsistency
56784 pPager->changeCountDone = pPager->tempFile;
56787 /* Leave 4 bytes of space before the super-journal filename in memory.
56791 zSuper = &pPager->pTmpSpace[4];
56792 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
56796 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
56805 /* If there was a super-journal and this routine will return success,
56806 ** see if it is possible to delete the super-journal.
56808 assert( zSuper==&pPager->pTmpSpace[4] );
56809 memset(&zSuper[-4], 0, 4);
56815 nPlayback, pPager->zJournal);
56830 ** pPg->pData. A shared lock or greater must be held on the database
56840 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
56846 assert( pPager->eState>=PAGER_READER && !MEMDB );
56847 assert( isOpen(pPager->fd) );
56850 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
56854 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
56858 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
56859 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
56865 if( pPg->pgno==1 ){
56872 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
56879 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
56881 u8 *dbFileVers = &((u8*)pPg->pData)[24];
56882 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
56886 PAGER_INCR(pPager->nRead);
56887 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
56889 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
56895 ** Update the value of the change-counter at offsets 24 and 92 in
56899 ** routine which only updates the change-counter if the update is actually
56900 ** needed, as determined by the pPager->changeCountDone state variable.
56907 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
56908 put32bits(((char*)pPg->pData)+24, change_counter);
56913 put32bits(((char*)pPg->pData)+92, change_counter);
56914 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
56943 pPager->xReiniter(pPg);
56957 sqlite3BackupRestart(pPager->pBackup);
56976 pPager->dbSize = pPager->dbOrigSize;
56977 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
56978 pList = sqlite3PcacheDirtyList(pPager->pPCache);
56980 PgHdr *pNext = pList->pDirty;
56981 rc = pagerUndoCallback((void *)pPager, pList->pgno);
57007 assert( pPager->pWal );
57011 for(p=pList; p && p->pDirty; p=p->pDirty){
57012 assert( p->pgno < p->pDirty->pgno );
57016 assert( pList->pDirty==0 || isCommit );
57024 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
57025 if( p->pgno<=nTruncate ){
57026 ppNext = &p->pDirty;
57034 pPager->aStat[PAGER_STAT_WRITE] += nList;
57036 if( pList->pgno==1 ) pager_write_changecounter(pList);
57037 rc = sqlite3WalFrames(pPager->pWal,
57038 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
57040 if( rc==SQLITE_OK && pPager->pBackup ){
57041 for(p=pList; p; p=p->pDirty){
57042 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
57047 pList = sqlite3PcacheDirtyList(pPager->pPCache);
57048 for(p=pList; p; p=p->pDirty){
57069 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
57076 sqlite3WalEndReadTransaction(pPager->pWal);
57078 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
57081 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
57100 /* Query the WAL sub-system for the database size. The WalDbsize()
57103 ** available from the WAL sub-system if the log file is empty or
57106 assert( pPager->eState==PAGER_OPEN );
57107 assert( pPager->eLock>=SHARED_LOCK );
57108 assert( isOpen(pPager->fd) );
57109 assert( pPager->tempFile==0 );
57110 nPage = sqlite3WalDbsize(pPager->pWal);
57113 ** WAL sub-system, determine the page count based on the size of
57115 ** integer multiple of the page-size, round up the result.
57117 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
57119 int rc = sqlite3OsFileSize(pPager->fd, &n);
57123 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
57130 if( nPage>pPager->mxPgno ){
57131 pPager->mxPgno = (Pgno)nPage;
57140 ** Check if the *-wal file that corresponds to the database opened by pPager
57141 ** exists if the database is not empy, or verify that the *-wal file does
57144 ** If the database is not empty and the *-wal file exists, open the pager
57145 ** in WAL mode. If the database is empty or if no *-wal file exists and
57153 ** a WAL on a none-empty database, this ensures there is no race condition
57159 assert( pPager->eState==PAGER_OPEN );
57160 assert( pPager->eLock>=SHARED_LOCK );
57162 if( !pPager->tempFile ){
57165 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
57174 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
57176 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
57179 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
57180 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
57190 ** the entire super-journal file. The case pSavepoint==NULL occurs when
57194 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
57207 ** * Pages are then played back from the sub-journal file, starting
57221 ** (or transaction). No page with a page-number greater than this value
57226 i64 iHdrOff; /* End of first segment of main-journal records */
57230 assert( pPager->eState!=PAGER_ERROR );
57231 assert( pPager->eState>=PAGER_WRITER_LOCKED );
57235 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
57244 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
57245 pPager->changeCountDone = pPager->tempFile;
57251 /* Use pPager->journalOff as the effective size of the main rollback
57254 ** past pPager->journalOff is off-limits to us.
57256 szJ = pPager->journalOff;
57262 ** greater than the current database size (pPager->dbSize) but those
57267 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
57268 pPager->journalOff = pSavepoint->iOffset;
57269 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
57270 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57274 pPager->journalOff = 0;
57279 ** of the main journal file. Continue to skip out-of-range pages and
57282 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
57290 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
57295 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
57297 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
57299 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
57300 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
57304 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
57306 /* Finally, rollback pages from the sub-journal. Page that were
57308 ** will be skipped. Out-of-range pages are also skipped.
57312 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
57315 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
57317 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
57318 assert( offset==(i64)ii*(4+pPager->pageSize) );
57326 pPager->journalOff = szJ;
57333 ** Change the maximum number of in-memory pages that are allowed
57337 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
57341 ** Change the maximum number of in-memory pages that are allowed
57345 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
57353 sqlite3_file *fd = pPager->fd;
57354 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
57356 sz = pPager->szMmap;
57357 pPager->bUseFetch = (sz>0);
57359 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
57368 pPager->szMmap = szMmap;
57376 sqlite3PcacheShrink(pPager->pPCache);
57398 ** database (with some additional information - the nRec field
57399 ** of the journal header - being written in between the two
57409 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
57420 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
57436 if( pPager->tempFile ){
57437 pPager->noSync = 1;
57438 pPager->fullSync = 0;
57439 pPager->extraSync = 0;
57441 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
57442 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
57443 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
57445 if( pPager->noSync ){
57446 pPager->syncFlags = 0;
57448 pPager->syncFlags = SQLITE_SYNC_FULL;
57450 pPager->syncFlags = SQLITE_SYNC_NORMAL;
57452 pPager->walSyncFlags = (pPager->syncFlags<<2);
57453 if( pPager->fullSync ){
57454 pPager->walSyncFlags |= pPager->syncFlags;
57456 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
57457 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
57460 pPager->doNotSpill &= ~SPILLFLAG_OFF;
57462 pPager->doNotSpill |= SPILLFLAG_OFF;
57504 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
57512 ** The pager invokes the busy-handler if sqlite3OsLock() returns
57513 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
57517 ** (which occurs during hot-journal rollback). Summary:
57520 ** --------------------------------------------------------
57521 ** NO_LOCK -> SHARED_LOCK | Yes
57522 ** SHARED_LOCK -> RESERVED_LOCK | No
57523 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
57524 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
57526 ** If the busy-handler callback returns non-zero, the lock is
57532 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
57536 pPager->xBusyHandler = xBusyHandler;
57537 pPager->pBusyHandlerArg = pBusyHandlerArg;
57538 ap = (void **)&pPager->xBusyHandler;
57541 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
57549 ** is a no-op. The value returned is the error state error code (i.e.
57550 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
57559 ** * the database is either not an in-memory database or it is
57560 ** an in-memory database that currently consists of zero pages.
57584 ** is a no-op for that case anyhow.
57589 if( (pPager->memDb==0 || pPager->dbSize==0)
57590 && sqlite3PcacheRefCount(pPager->pPCache)==0
57591 && pageSize && pageSize!=(u32)pPager->pageSize
57596 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
57597 rc = sqlite3OsFileSize(pPager->fd, &nByte);
57600 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
57612 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
57615 sqlite3PageFree(pPager->pTmpSpace);
57616 pPager->pTmpSpace = pNew;
57617 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
57618 pPager->pageSize = pageSize;
57619 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
57625 *pPageSize = pPager->pageSize;
57627 if( nReserve<0 ) nReserve = pPager->nReserve;
57629 pPager->nReserve = (i16)nReserve;
57644 return pPager->pTmpSpace;
57656 pPager->mxPgno = mxPage;
57658 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
57659 /* assert( pPager->mxPgno>=pPager->dbSize ); */
57663 return pPager->mxPgno;
57671 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
57680 sqlite3_io_error_pending = -1;
57707 assert( isOpen(pPager->fd) || pPager->tempFile );
57715 if( isOpen(pPager->fd) ){
57717 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
57726 ** This function may only be called when a read-transaction is open on
57729 ** However, if the file is between 1 and <page-size> bytes in size, then
57733 assert( pPager->eState>=PAGER_READER );
57734 assert( pPager->eState!=PAGER_WRITER_FINISHED );
57735 *pnPage = (int)pPager->dbSize;
57741 ** a similar or greater lock is already held, this function is a no-op
57756 /* Check that this is either a no-op (because the requested lock is
57757 ** already held), or one of the transitions that the busy-handler
57761 assert( (pPager->eLock>=locktype)
57762 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
57763 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
57768 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
57774 ** following is true for all dirty pages currently in the page-cache:
57780 ** be necessary to write the current content out to the sub-journal.
57789 ** sub-journal rolled back the content could not be restored and the
57795 Pager *pPager = pPg->pPager;
57796 assert( pPg->flags&PGHDR_DIRTY );
57797 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
57798 Pgno pgno = pPg->pgno;
57800 for(i=0; i<pPg->pPager->nSavepoint; i++){
57801 PagerSavepoint *p = &pPager->aSavepoint[i];
57802 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
57807 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
57814 ** Truncate the in-memory database file image to nPage pages. This
57825 assert( pPager->dbSize>=nPage || CORRUPT_DB );
57826 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
57827 pPager->dbSize = nPage;
57842 ** This function is called before attempting a hot-journal rollback. It
57843 ** syncs the journal file to disk, then sets pPager->journalHdr to the
57847 ** Syncing a hot-journal to disk before attempting to roll it back ensures
57848 ** that if a power-failure occurs during the rollback, the process that
57857 if( !pPager->noSync ){
57858 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
57861 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
57885 if( pPager->pMmapFreelist ){
57886 *ppPage = p = pPager->pMmapFreelist;
57887 pPager->pMmapFreelist = p->pDirty;
57888 p->pDirty = 0;
57889 assert( pPager->nExtra>=8 );
57890 memset(p->pExtra, 0, 8);
57892 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
57894 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
57897 p->pExtra = (void *)&p[1];
57898 p->flags = PGHDR_MMAP;
57899 p->nRef = 1;
57900 p->pPager = pPager;
57903 assert( p->pExtra==(void *)&p[1] );
57904 assert( p->pPage==0 );
57905 assert( p->flags==PGHDR_MMAP );
57906 assert( p->pPager==pPager );
57907 assert( p->nRef==1 );
57909 p->pgno = pgno;
57910 p->pData = pData;
57911 pPager->nMmapOut++;
57922 Pager *pPager = pPg->pPager;
57923 pPager->nMmapOut--;
57924 pPg->pDirty = pPager->pMmapFreelist;
57925 pPager->pMmapFreelist = pPg;
57927 assert( pPager->fd->pMethods->iVersion>=3 );
57928 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
57937 for(p=pPager->pMmapFreelist; p; p=pNext){
57938 pNext = p->pDirty;
57945 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
57952 if( pPager->tempFile ) return SQLITE_OK;
57953 if( pPager->dbSize==0 ) return SQLITE_OK;
57954 assert( pPager->zFilename && pPager->zFilename[0] );
57955 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
57957 /* If the HAS_MOVED file-control is unimplemented, assume that the file
57983 u8 *pTmp = (u8*)pPager->pTmpSpace;
57989 /* pPager->errCode = 0; */
57990 pPager->exclusiveMode = 0;
57994 assert( db || pPager->pWal==0 );
57995 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
58000 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
58001 pPager->pWal = 0;
58016 ** back or finalize it. The next database user will have to do hot-journal
58019 if( isOpen(pPager->jfd) ){
58028 sqlite3OsClose(pPager->jfd);
58029 sqlite3OsClose(pPager->fd);
58031 sqlite3PcacheClose(pPager->pPCache);
58032 assert( !pPager->aSavepoint && !pPager->pInJournal );
58033 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
58044 return pPg->pgno;
58058 ** disk and can be restored in the event of a hot-journal rollback.
58060 ** If the Pager.noSync flag is set, then this function is a no-op.
58061 ** Otherwise, the actions required depend on the journal-mode and the
58062 ** device characteristics of the file-system, as follows:
58064 ** * If the journal file is an in-memory journal file, no action need
58070 ** been written following it. If the pager is operating in full-sync
58076 ** Or, in pseudo-code:
58078 ** if( NOT <in-memory journal> ){
58080 ** if( <full-sync mode> ) xSync(<journal file>);
58093 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58094 || pPager->eState==PAGER_WRITER_DBMOD
58102 if( !pPager->noSync ){
58103 assert( !pPager->tempFile );
58104 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58105 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58106 assert( isOpen(pPager->jfd) );
58110 ** that wrote to this database was operating in persistent-journal
58113 ** file happens to be a journal-header (written as part of the
58114 ** previous connection's transaction), and a crash or power-failure
58118 ** hot-journal rollback following recovery. It may roll back all
58120 ** out-of-date data that follows it. Database corruption.
58136 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
58139 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
58142 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
58149 ** full-synchronous mode, sync the journal first. This ensures that
58159 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
58162 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
58165 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
58167 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
58174 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
58175 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
58180 pPager->journalHdr = pPager->journalOff;
58182 pPager->nRec = 0;
58187 pPager->journalHdr = pPager->journalOff;
58195 sqlite3PcacheClearSyncFlags(pPager->pPCache);
58196 pPager->eState = PAGER_WRITER_DBMOD;
58204 ** in-memory pages in the list to the database file. The argument may
58206 ** a no-op.
58213 ** If the pager is a temp-file pager and the actual file-system file
58238 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
58239 assert( pPager->eLock==EXCLUSIVE_LOCK );
58240 assert( isOpen(pPager->fd) || pList->pDirty==0 );
58242 /* If the file is a temp-file has not yet been opened, open it now. It
58244 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
58246 if( !isOpen(pPager->fd) ){
58247 assert( pPager->tempFile && rc==SQLITE_OK );
58248 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
58254 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
58256 && pPager->dbHintSize<pPager->dbSize
58257 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
58259 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
58260 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
58261 pPager->dbHintSize = pPager->dbSize;
58265 Pgno pgno = pList->pgno;
58269 ** make the file smaller (presumably by auto-vacuum code). Do not write
58275 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
58276 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
58279 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
58280 if( pList->pgno==1 ) pager_write_changecounter(pList);
58282 pData = pList->pData;
58285 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
58292 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
58294 if( pgno>pPager->dbFileSize ){
58295 pPager->dbFileSize = pgno;
58297 pPager->aStat[PAGER_STAT_WRITE]++;
58300 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
58310 pList = pList->pDirty;
58317 ** Ensure that the sub-journal file is open. If it is already open, this
58318 ** function is a no-op.
58326 if( !isOpen(pPager->sjfd) ){
58331 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
58332 nStmtSpill = -1;
58334 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
58340 ** Append a record of the current state of page pPg to the sub-journal.
58342 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
58346 ** error code if the attempt to write to the sub-journal fails, or
58352 Pager *pPager = pPg->pPager;
58353 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
58355 /* Open the sub-journal, if it has not already been opened */
58356 assert( pPager->useJournal );
58357 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
58358 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
58361 || pPg->pgno>pPager->dbOrigSize
58365 /* If the sub-journal was opened successfully (or was already open),
58368 void *pData = pPg->pData;
58369 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
58372 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
58373 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
58375 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
58380 pPager->nSubRec++;
58381 assert( pPager->nSavepoint>0 );
58382 rc = addToSavepointBitvecs(pPager, pPg->pgno);
58397 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
58417 assert( pPg->pPager==pPager );
58418 assert( pPg->flags&PGHDR_DIRTY );
58436 if( NEVER(pPager->errCode) ) return SQLITE_OK;
58437 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
58438 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
58439 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
58440 if( pPager->doNotSpill
58441 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
58442 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
58447 pPager->aStat[PAGER_STAT_SPILL]++;
58448 pPg->pDirty = 0;
58458 if( pPager->tempFile==0 ){
58459 rc = sqlite3JournalCreate(pPager->jfd);
58465 if( pPg->flags&PGHDR_NEED_SYNC
58466 || pPager->eState==PAGER_WRITER_CACHEMOD
58473 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
58480 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
58491 int rc = pPager->errCode;
58493 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
58496 PgHdr *pNext = pList->pDirty;
58497 if( pList->nRef==0 ){
58513 ** If zFilename is NULL then a randomly-named temporary file is created
58517 ** This can be used to implement an in-memory database.
58543 int nExtra, /* Extra bytes append to each in-memory page */
58551 int tempFile = 0; /* True for temp files (incl. in-memory files) */
58552 int memDb = 0; /* True if this is an in-memory file */
58558 int readOnly = 0; /* True if this is a read-only file */
58569 /* Figure out how much space is required for each journal file-handle
58570 ** (there are two of them, the main journal and the sub-journal). */
58594 nPathname = pVfs->mxPathname+1;
58617 nUriByte = (int)(&z[1] - zUri);
58619 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
58621 ** the database being opened will be more than pVfs->mxPathname
58624 ** check for a hot-journal before reading.
58640 ** Database file handle (pVfs->szOsFile bytes)
58641 ** Sub-journal file handle (journalFileSize bytes)
58651 ** Some 3rd-party software, over which we have no control, depends on
58655 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
58658 ** filename format expected by 3rd-party software should be as follows:
58660 ** - Main Database Path
58661 ** - \0
58662 ** - Multiple URI components consisting of:
58663 ** - Key
58664 ** - \0
58665 ** - Value
58666 ** - \0
58667 ** - \0
58668 ** - Journal Path
58669 ** - \0
58670 ** - WAL Path (zWALName)
58671 ** - \0
58681 ROUND8(pVfs->szOsFile) + /* The main db file */
58699 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
58700 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
58701 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58702 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
58703 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
58708 pPager->zFilename = (char*)pPtr;
58721 pPager->zJournal = (char*)pPtr;
58723 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
58725 sqlite3FileSuffix3(zFilename,pPager->zJournal);
58726 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
58729 pPager->zJournal = 0;
58735 pPager->zWal = (char*)pPtr;
58737 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
58739 sqlite3FileSuffix3(zFilename, pPager->zWal);
58740 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
58743 pPager->zWal = 0;
58749 pPager->pVfs = pVfs;
58750 pPager->vfsFlags = vfsFlags;
58756 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
58759 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
58772 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
58776 if( szPageDflt<pPager->sectorSize ){
58777 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
58780 szPageDflt = (u32)pPager->sectorSize;
58797 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
58799 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
58809 ** This branch is also run for an in-memory database. An in-memory
58810 ** database is the same as a temp-file that is never written out to
58811 ** disk and uses an in-memory rollback journal.
58817 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
58818 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
58819 pPager->noLock = 1; /* Do no locking */
58827 assert( pPager->memDb==0 );
58828 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
58837 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
58843 sqlite3OsClose(pPager->fd);
58844 sqlite3PageFree(pPager->pTmpSpace);
58849 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
58850 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
58852 pPager->useJournal = (u8)useJournal;
58853 /* pPager->stmtOpen = 0; */
58854 /* pPager->stmtInUse = 0; */
58855 /* pPager->nRef = 0; */
58856 /* pPager->stmtSize = 0; */
58857 /* pPager->stmtJSize = 0; */
58858 /* pPager->nPage = 0; */
58859 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
58860 /* pPager->state = PAGER_UNLOCK; */
58861 /* pPager->errMask = 0; */
58862 pPager->tempFile = (u8)tempFile;
58866 pPager->exclusiveMode = (u8)tempFile;
58867 pPager->changeCountDone = pPager->tempFile;
58868 pPager->memDb = (u8)memDb;
58869 pPager->readOnly = (u8)readOnly;
58870 assert( useJournal || pPager->tempFile );
58871 pPager->noSync = pPager->tempFile;
58872 if( pPager->noSync ){
58873 assert( pPager->fullSync==0 );
58874 assert( pPager->extraSync==0 );
58875 assert( pPager->syncFlags==0 );
58876 assert( pPager->walSyncFlags==0 );
58878 pPager->fullSync = 1;
58879 pPager->extraSync = 0;
58880 pPager->syncFlags = SQLITE_SYNC_NORMAL;
58881 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
58883 /* pPager->pFirst = 0; */
58884 /* pPager->pFirstSynced = 0; */
58885 /* pPager->pLast = 0; */
58886 pPager->nExtra = (u16)nExtra;
58887 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
58888 assert( isOpen(pPager->fd) || tempFile );
58891 pPager->journalMode = PAGER_JOURNALMODE_OFF;
58893 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
58895 /* pPager->xBusyHandler = 0; */
58896 /* pPager->pBusyHandlerArg = 0; */
58897 pPager->xReiniter = xReinit;
58899 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
58900 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
58913 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
58914 zName--;
58916 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
58917 return pPager->fd;
58924 ** the file-system for the given pager. A hot journal is one that
58925 ** needs to be played back. According to this function, a hot-journal
58939 ** This routine does not check if there is a super-journal filename
58940 ** at the end of the file. If there is, and that super-journal file
58942 ** case this routine will return a false-positive. The pager_playback()
58946 ** If a hot-journal file is found to exist, *pExists is set to 1 and
58947 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
58949 ** to determine whether or not a hot-journal file exists, the IO error
58953 sqlite3_vfs * const pVfs = pPager->pVfs;
58956 int jrnlOpen = !!isOpen(pPager->jfd);
58958 assert( pPager->useJournal );
58959 assert( isOpen(pPager->fd) );
58960 assert( pPager->eState==PAGER_OPEN );
58962 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
58968 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
58978 ** in fact there is none. This results in a false-positive which will
58981 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
58985 assert( pPager->tempFile==0 );
58999 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
59000 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
59006 ** at least one non-zero bytes at the start of the journal file.
59012 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
59016 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
59021 sqlite3OsClose(pPager->jfd);
59048 ** has been successfully called. If a shared-lock is already held when
59049 ** this function is called, it is a no-op.
59056 ** the SHARED lock, the file-system is checked for a hot-journal,
59057 ** which is played back if present. Following any hot-journal
59059 ** the 'change-counter' field of the database file header and
59062 ** 2) If the pager is running in exclusive-mode, and there are currently
59069 ** occurs while locking the database, checking for a hot-journal file or
59075 /* This routine is only called from b-tree and only when there are no
59079 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
59081 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
59082 assert( pPager->errCode==SQLITE_OK );
59084 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
59085 int bHotJournal = 1; /* True if there exists a hot journal-file */
59088 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
59092 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
59099 if( pPager->eLock<=SHARED_LOCK ){
59106 if( pPager->readOnly ){
59116 ** hot-journal back.
59133 ** in exclusive-access mode the file descriptor will be kept open
59134 ** and possibly used for a transaction later on. Also, write-access
59141 ** may mean that the pager was in the error-state when this
59144 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
59145 sqlite3_vfs * const pVfs = pPager->pVfs;
59148 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
59152 assert( !pPager->tempFile );
59153 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
59154 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
59157 sqlite3OsClose(pPager->jfd);
59164 ** playing back the hot-journal so that we don't end up with
59170 if( isOpen(pPager->jfd) ){
59174 rc = pager_playback(pPager, !pPager->tempFile);
59175 pPager->eState = PAGER_OPEN;
59177 }else if( !pPager->exclusiveMode ){
59183 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
59202 assert( pPager->eState==PAGER_OPEN );
59203 assert( (pPager->eLock==SHARED_LOCK)
59204 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
59208 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
59209 /* The shared-lock has just been acquired then check to
59213 ** single unnecessary sqlite3OsRead() call at the start-up.
59217 ** a 32-bit counter that is incremented with each change. The
59225 char dbFileVers[sizeof(pPager->dbFileVers)];
59228 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
59236 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
59246 sqlite3OsUnfetch(pPager->fd, 0, 0);
59251 /* If there is a WAL file in the file-system, open this database in WAL
59252 ** mode. Otherwise, the following function call is a no-op.
59256 assert( pPager->pWal==0 || rc==SQLITE_OK );
59265 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
59266 rc = pagerPagecount(pPager, &pPager->dbSize);
59273 assert( pPager->eState==PAGER_OPEN );
59275 pPager->eState = PAGER_READER;
59276 pPager->hasHeldSharedLock = 1;
59287 ** nothing to rollback, so this routine is a no-op.
59290 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
59291 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
59304 ** getPageNormal() -- The normal getter
59305 ** getPageError() -- Used if the pager is in an error state
59306 ** getPageMmap() -- Used if memory-mapped I/O is enabled
59328 ** a) When reading a free-list leaf page from the database, and
59346 ** to find a page in the in-memory cache first. If the page is not already
59348 ** just returns 0. This routine acquires a read-lock the first time it
59364 assert( pPager->errCode==SQLITE_OK );
59365 assert( pPager->eState>=PAGER_READER );
59367 assert( pPager->hasHeldSharedLock==1 );
59370 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
59373 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
59380 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
59382 assert( pPg->pgno==pgno );
59383 assert( pPg->pPager==pPager || pPg->pPager==0 );
59386 if( pPg->pPager && !noContent ){
59390 pPager->aStat[PAGER_STAT_HIT]++;
59405 pPg->pPager = pPager;
59407 assert( !isOpen(pPager->fd) || !MEMDB );
59408 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
59409 if( pgno>pPager->mxPgno ){
59414 /* Failure to set the bits in the InJournal bit-vectors is benign.
59421 if( pgno<=pPager->dbOrigSize ){
59422 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
59429 memset(pPg->pData, 0, pPager->pageSize);
59432 assert( pPg->pPager==pPager );
59433 pPager->aStat[PAGER_STAT_MISS]++;
59454 /* The page getter for when memory-mapped I/O is enabled */
59465 /* It is acceptable to use a read-only (mmap) page for any page except
59466 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
59468 ** temporary or in-memory database. */
59470 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
59482 assert( pPager->eState>=PAGER_READER );
59484 assert( pPager->hasHeldSharedLock==1 );
59485 assert( pPager->errCode==SQLITE_OK );
59488 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
59496 rc = sqlite3OsFetch(pPager->fd,
59497 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
59500 if( pPager->eState>PAGER_READER || pPager->tempFile ){
59506 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
59532 assert( pPager->errCode!=SQLITE_OK );
59534 return pPager->errCode;
59547 return pPager->xGet(pPager, pgno, ppPage, flags);
59551 ** Acquire a page if it is already in the in-memory cache. Do
59565 assert( pPager->pPCache!=0 );
59566 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
59567 assert( pPage==0 || pPager->hasHeldSharedLock );
59569 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
59585 TESTONLY( Pager *pPager = pPg->pPager; )
59587 if( pPg->flags & PGHDR_MMAP ){
59588 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
59594 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
59602 assert( pPg->pgno==1 );
59603 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
59604 pPager = pPg->pPager;
59615 ** to the start of it. If there are active savepoints, open the sub-journal
59633 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
59635 assert( pPager->eState==PAGER_WRITER_LOCKED );
59637 assert( pPager->pInJournal==0 );
59639 /* If already in the error state, this function is a no-op. But on
59642 if( NEVER(pPager->errCode) ) return pPager->errCode;
59644 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
59645 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
59646 if( pPager->pInJournal==0 ){
59651 if( !isOpen(pPager->jfd) ){
59652 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
59653 sqlite3MemJournalOpen(pPager->jfd);
59658 if( pPager->tempFile ){
59671 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
59675 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
59680 ** the sub-journal if necessary.
59684 pPager->nRec = 0;
59685 pPager->journalOff = 0;
59686 pPager->setSuper = 0;
59687 pPager->journalHdr = 0;
59693 sqlite3BitvecDestroy(pPager->pInJournal);
59694 pPager->pInJournal = 0;
59696 assert( pPager->eState==PAGER_WRITER_LOCKED );
59697 pPager->eState = PAGER_WRITER_CACHEMOD;
59704 ** Begin a write-transaction on the specified pager object. If a
59705 ** write-transaction has already been opened, this function is a no-op.
59712 ** If the subjInMemory argument is non-zero, then any sub-journal opened
59713 ** within this transaction will be opened as an in-memory file. This
59714 ** has no effect if the sub-journal is already opened (as it may be when
59716 ** sub-journal. If the subjInMemory argument is zero, then any required
59717 ** sub-journal is implemented in-memory if pPager is an in-memory database,
59723 if( pPager->errCode ) return pPager->errCode;
59724 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
59725 pPager->subjInMemory = (u8)subjInMemory;
59727 if( pPager->eState==PAGER_READER ){
59728 assert( pPager->pInJournal==0 );
59734 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
59739 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
59744 ** The busy-handler is not invoked if another connection already
59745 ** holds the write-lock. If possible, the upper layer will call it.
59747 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
59751 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
59766 ** transactions may copy data from the sub-journal into the database
59770 pPager->eState = PAGER_WRITER_LOCKED;
59771 pPager->dbHintSize = pPager->dbSize;
59772 pPager->dbFileSize = pPager->dbSize;
59773 pPager->dbOrigSize = pPager->dbSize;
59774 pPager->journalOff = 0;
59777 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
59778 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
59790 Pager *pPager = pPg->pPager;
59794 i64 iOff = pPager->journalOff;
59799 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
59801 assert( pPager->journalHdr<=pPager->journalOff );
59802 pData2 = pPg->pData;
59806 ** page in the block above, set the need-sync flag for the page.
59812 pPg->flags |= PGHDR_NEED_SYNC;
59814 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
59816 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
59818 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
59821 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
59822 pPager->journalOff, pPager->pageSize));
59825 PAGERID(pPager), pPg->pgno,
59826 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
59828 pPager->journalOff += 8 + pPager->pageSize;
59829 pPager->nRec++;
59830 assert( pPager->pInJournal!=0 );
59831 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
59834 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
59841 ** main journal or sub-journal as required. If the page is written into
59847 Pager *pPager = pPg->pPager;
59850 /* This routine is not called unless a write-transaction has already
59854 assert( pPager->eState==PAGER_WRITER_LOCKED
59855 || pPager->eState==PAGER_WRITER_CACHEMOD
59856 || pPager->eState==PAGER_WRITER_DBMOD
59859 assert( pPager->errCode==0 );
59860 assert( pPager->readOnly==0 );
59864 ** obtained the necessary locks to begin the write-transaction, but the
59872 if( pPager->eState==PAGER_WRITER_LOCKED ){
59876 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
59886 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
59887 if( pPager->pInJournal!=0
59888 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
59891 if( pPg->pgno<=pPager->dbOrigSize ){
59897 if( pPager->eState!=PAGER_WRITER_DBMOD ){
59898 pPg->flags |= PGHDR_NEED_SYNC;
59901 PAGERID(pPager), pPg->pgno,
59902 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
59906 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
59911 pPg->flags |= PGHDR_WRITEABLE;
59916 if( pPager->nSavepoint>0 ){
59921 if( pPager->dbSize<pPg->pgno ){
59922 pPager->dbSize = pPg->pgno;
59945 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
59946 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
59953 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
59954 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
59956 /* This trick assumes that both the page-size and sector-size are
59960 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
59962 nPageCount = pPager->dbSize;
59963 if( pPg->pgno>nPageCount ){
59964 nPage = (pPg->pgno - pg1)+1;
59965 }else if( (pg1+nPagePerSector-1)>nPageCount ){
59966 nPage = nPageCount+1-pg1;
59971 assert(pg1<=pPg->pgno);
59972 assert((pg1+nPage)>pPg->pgno);
59977 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
59982 if( pPage->flags&PGHDR_NEED_SYNC ){
59989 if( pPage->flags&PGHDR_NEED_SYNC ){
60007 pPage->flags |= PGHDR_NEED_SYNC;
60013 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
60014 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
60026 ** fit on a single disk sector. In this case all co-resident pages
60033 Pager *pPager = pPg->pPager;
60034 assert( (pPg->flags & PGHDR_MMAP)==0 );
60035 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60037 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
60038 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
60040 }else if( pPager->errCode ){
60041 return pPager->errCode;
60042 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
60043 assert( pPager->tempFile==0 );
60057 return pPg->flags & PGHDR_WRITEABLE;
60075 ** This optimization cannot be used with a temp-file, as the page may
60082 Pager *pPager = pPg->pPager;
60083 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
60084 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
60085 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
60086 pPg->flags |= PGHDR_DONT_WRITE;
60087 pPg->flags &= ~PGHDR_WRITEABLE;
60088 testcase( pPg->flags & PGHDR_NEED_SYNC );
60095 ** change-counter, stored as a 4-byte big-endian integer starting at
60099 ** But this only happens if the pPager->changeCountDone flag is false.
60109 ** The isDirectMode flag may only be non-zero if the library was compiled
60111 ** if isDirect is non-zero, then the database file is updated directly
60118 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60119 || pPager->eState==PAGER_WRITER_DBMOD
60124 ** atomic-write optimization is enabled in this build, then isDirect
60128 ** The idea is that if the atomic-write optimization is not
60141 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
60144 assert( !pPager->tempFile && isOpen(pPager->fd) );
60151 ** operating in direct-mode, make page 1 writable. When not in
60153 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
60166 assert( pPager->dbFileSize>0 );
60167 zBuf = pPgHdr->pData;
60169 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
60170 pPager->aStat[PAGER_STAT_WRITE]++;
60173 /* Update the pager's copy of the change-counter. Otherwise, the
60175 ** flushed (as the change-counter values will not match). */
60177 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
60178 pPager->changeCountDone = 1;
60181 pPager->changeCountDone = 1;
60192 ** Sync the database file to disk. This is a no-op for in-memory databases
60195 ** If successful, or if called on a pager for which it is a no-op, this
60201 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
60203 if( rc==SQLITE_OK && !pPager->noSync ){
60205 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
60211 ** This function may only be called while a write-transaction is active in
60212 ** rollback. If the connection is in WAL mode, this call is a no-op.
60222 int rc = pPager->errCode;
60225 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60226 || pPager->eState==PAGER_WRITER_DBMOD
60227 || pPager->eState==PAGER_WRITER_LOCKED
60239 ** of a super-journal file that should be written into the individual
60241 ** super-journal (a single database transaction).
60245 ** * The database file change-counter is updated,
60246 ** * the journal is synced (unless the atomic-write optimization is used),
60253 ** delete the super-journal file if specified).
60258 ** If the final parameter - noSync - is true, then the database file itself
60265 const char *zSuper, /* If not NULL, the super-journal name */
60270 assert( pPager->eState==PAGER_WRITER_LOCKED
60271 || pPager->eState==PAGER_WRITER_CACHEMOD
60272 || pPager->eState==PAGER_WRITER_DBMOD
60273 || pPager->eState==PAGER_ERROR
60278 if( NEVER(pPager->errCode) ) return pPager->errCode;
60284 pPager->zFilename, zSuper, pPager->dbSize));
60287 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
60289 assert( MEMDB==0 || pPager->tempFile );
60290 assert( isOpen(pPager->fd) || pPager->tempFile );
60292 /* If this is an in-memory db, or no pages have been written to, or this
60293 ** function has already been called, it is mostly a no-op. However, any
60295 sqlite3BackupRestart(pPager->pBackup);
60300 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60303 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
60306 pList->pDirty = 0;
60310 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
60314 sqlite3PcacheCleanAll(pPager->pPCache);
60317 /* The bBatch boolean is true if the batch-atomic-write commit method
60318 ** should be used. No rollback journal is created if batch-atomic-write
60322 sqlite3_file *fd = pPager->fd;
60325 && !pPager->noSync
60326 && sqlite3JournalIsInMemory(pPager->jfd);
60332 /* The following block updates the change-counter. Exactly how it
60333 ** does this depends on whether or not the atomic-update optimization
60337 ** * The file-system supports the atomic-write property for
60338 ** blocks of size page-size, and
60339 ** * This commit is not part of a multi-file transaction, and
60344 ** counter in 'indirect-mode'. If the optimization is compiled in but
60347 ** pager_incr_changecounter() to update the change-counter in indirect
60351 ** then call pager_incr_changecounter() to update the change-counter
60357 assert( isOpen(pPager->jfd)
60358 || pPager->journalMode==PAGER_JOURNALMODE_OFF
60359 || pPager->journalMode==PAGER_JOURNALMODE_WAL
60361 if( !zSuper && isOpen(pPager->jfd)
60362 && pPager->journalOff==jrnlBufferSize(pPager)
60363 && pPager->dbSize>=pPager->dbOrigSize
60364 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
60366 /* Update the db file change counter via the direct-write method. The
60367 ** following call will modify the in-memory representation of page 1
60369 ** directly to the database file. Because of the atomic-write
60370 ** property of the host file-system, this is safe.
60374 rc = sqlite3JournalCreate(pPager->jfd);
60383 rc = sqlite3JournalCreate(pPager->jfd);
60392 /* Write the super-journal name into the journal file. If a
60393 ** super-journal file name has already been written to the journal file,
60394 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
60400 ** If the atomic-update optimization is being used, this sync will not
60403 ** Because the change-counter page was just modified, unless the
60404 ** atomic-update optimization is used it is almost certain that the
60408 ** xSync() call will be changed to a no-op by the OS anyhow.
60413 pList = sqlite3PcacheDirtyList(pPager->pPCache);
60428 rc = sqlite3JournalCreate(pPager->jfd);
60430 sqlite3OsClose(pPager->jfd);
60435 sqlite3OsClose(pPager->jfd);
60447 sqlite3PcacheCleanAll(pPager->pPCache);
60452 ** last page in the db image moved to the free-list. In this case the
60455 if( pPager->dbSize>pPager->dbFileSize ){
60456 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
60457 assert( pPager->eState==PAGER_WRITER_DBMOD );
60472 pPager->eState = PAGER_WRITER_FINISHED;
60481 ** synced to disk. The journal file still exists in the file-system
60483 ** be used as a hot-journal and the current transaction rolled back.
60487 ** for hot-journal rollback. Once this is done the transaction is
60499 if( NEVER(pPager->errCode) ) return pPager->errCode;
60500 pPager->iDataVersion++;
60502 assert( pPager->eState==PAGER_WRITER_LOCKED
60503 || pPager->eState==PAGER_WRITER_FINISHED
60504 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
60509 ** this transaction, the pager is running in exclusive-mode and is
60510 ** using persistent journals, then this function is a no-op.
60514 ** a hot-journal during hot-journal rollback, 0 changes will be made
60519 if( pPager->eState==PAGER_WRITER_LOCKED
60520 && pPager->exclusiveMode
60521 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
60523 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
60524 pPager->eState = PAGER_READER;
60529 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
60535 ** transaction are reverted and the current write-transaction is closed.
60545 ** in-memory cache pages to the state they were in when the transaction
60554 ** In WAL mode, all cache-entries containing data modified within the
60556 ** their pre-transaction state by re-reading data from the database or
60563 /* PagerRollback() is a no-op if called in READER or OPEN state. If
60568 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
60569 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
60573 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
60574 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
60576 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
60577 int eState = pPager->eState;
60584 pPager->errCode = SQLITE_ABORT;
60585 pPager->eState = PAGER_ERROR;
60593 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
60606 ** Return TRUE if the database file is opened read-only. Return FALSE
60610 return pPager->readOnly;
60618 return sqlite3PcacheRefCount(pPager->pPCache);
60627 int perPageSize = pPager->pageSize + pPager->nExtra
60629 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
60631 + pPager->pageSize;
60647 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
60648 a[1] = sqlite3PcachePagecount(pPager->pPCache);
60649 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
60650 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
60651 a[4] = pPager->eState;
60652 a[5] = pPager->errCode;
60653 a[6] = pPager->aStat[PAGER_STAT_HIT];
60654 a[7] = pPager->aStat[PAGER_STAT_MISS];
60655 a[8] = 0; /* Used to be pPager->nOvfl */
60656 a[9] = pPager->nRead;
60657 a[10] = pPager->aStat[PAGER_STAT_WRITE];
60670 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
60686 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
60687 *pnVal += pPager->aStat[eStat];
60689 pPager->aStat[eStat] = 0;
60694 ** Return true if this is an in-memory or temp-file backed pager.
60697 return pPager->tempFile || pPager->memVfs;
60704 ** equal to nSavepoint, then this function is a no-op.
60707 ** occurs while opening the sub-journal file, then an IO error code is
60712 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
60716 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60718 assert( nSavepoint>nCurrent && pPager->useJournal );
60725 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
60730 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
60731 pPager->aSavepoint = aNew;
60735 aNew[ii].nOrig = pPager->dbSize;
60736 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
60737 aNew[ii].iOffset = pPager->journalOff;
60741 aNew[ii].iSubRec = pPager->nSubRec;
60742 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
60748 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
60750 pPager->nSavepoint = ii+1;
60752 assert( pPager->nSavepoint==nSavepoint );
60757 assert( pPager->eState>=PAGER_WRITER_LOCKED );
60760 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
60780 ** (the first created). A value of (Pager.nSavepoint-1) means operate
60782 ** (Pager.nSavepoint-1), then this function is a no-op.
60799 int rc = pPager->errCode;
60808 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
60817 for(ii=nNew; ii<pPager->nSavepoint; ii++){
60818 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
60820 pPager->nSavepoint = nNew;
60822 /* Truncate the sub-journal so that it only includes the parts
60825 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
60826 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
60827 /* Only truncate if it is an in-memory sub-journal. */
60828 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
60829 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
60830 rc = sqlite3OsTruncate(pPager->sjfd, sz);
60833 pPager->nSubRec = pRel->iSubRec;
60837 ** If this is a temp-file, it is possible that the journal file has
60841 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
60842 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
60853 pPager->journalMode==PAGER_JOURNALMODE_OFF
60854 && pPager->eState>=PAGER_WRITER_CACHEMOD
60856 pPager->errCode = SQLITE_ABORT;
60857 pPager->eState = PAGER_ERROR;
60869 ** Except, if the pager is in-memory only, then return an empty string if
60873 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
60874 ** participate in shared-cache.
60881 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
60888 return pPager->pVfs;
60897 return pPager->fd;
60906 return pPager->jfd;
60908 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
60916 return pPager->zJournal;
60929 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
60937 ** If the fourth argument, isCommit, is non-zero, then this page is being
60947 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
60951 assert( pPg->nRef>0 );
60952 assert( pPager->eState==PAGER_WRITER_CACHEMOD
60953 || pPager->eState==PAGER_WRITER_DBMOD
60957 /* In order to be able to rollback, an in-memory database must journal
60960 assert( pPager->tempFile || !MEMDB );
60961 if( pPager->tempFile ){
60968 ** sub-journal now. This is required to handle the following scenario:
60976 ** If page X were not written to the sub-journal here, it would not
60980 ** subjournalPage() may need to allocate space to store pPg->pgno into
60984 if( (pPg->flags & PGHDR_DIRTY)!=0
60991 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
60992 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
60994 /* If the journal needs to be sync()ed before page pPg->pgno can
60995 ** be written to, store pPg->pgno in local variable needSyncPgno.
60998 ** the journal needs to be sync()ed before database page pPg->pgno
61001 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
61002 needSyncPgno = pPg->pgno;
61003 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
61004 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
61005 assert( pPg->flags&PGHDR_DIRTY );
61008 /* If the cache contains a page with page-number pgno, remove it
61013 pPg->flags &= ~PGHDR_NEED_SYNC;
61015 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
61017 if( NEVER(pPgOld->nRef>1) ){
61021 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
61022 if( pPager->tempFile ){
61023 /* Do not discard pages from an in-memory database since we might
61025 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
61031 origPgno = pPg->pgno;
61035 /* For an in-memory database, make sure the original page continues
61039 if( pPager->tempFile && pPgOld ){
61045 /* If needSyncPgno is non-zero, then the journal file needs to be
61047 ** Currently, no such page exists in the page-cache and the
61049 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
61052 ** If the attempt to load the page into the page-cache fails, (due
61062 if( needSyncPgno<=pPager->dbOrigSize ){
61063 assert( pPager->pTmpSpace!=0 );
61064 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
61068 pPgHdr->flags |= PGHDR_NEED_SYNC;
61084 assert( pPg->pgno!=iNew );
61085 pPg->flags = flags;
61093 assert( pPg->nRef>0 || pPg->pPager->memDb );
61094 return pPg->pData;
61102 return pPg->pExtra;
61106 ** Get/set the locking-mode for this pager. Parameter eMode must be one
61109 ** the locking-mode is set to the value specified.
61113 ** locking-mode.
61121 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
61122 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
61123 pPager->exclusiveMode = (u8)eMode;
61125 return (int)pPager->exclusiveMode;
61129 ** Set the journal-mode for this pager. Parameter eMode must be one of:
61141 ** * An in-memory database can only have its journal_mode set to _OFF
61146 ** The returned indicate the current (possibly updated) journal-mode.
61149 u8 eOld = pPager->journalMode; /* Prior journalmode */
61163 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
61165 /* Do allow the journalmode of an in-memory database to be set to
61178 assert( pPager->eState!=PAGER_ERROR );
61179 pPager->journalMode = (u8)eMode;
61192 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
61193 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
61202 sqlite3OsClose(pPager->jfd);
61203 if( pPager->eLock>=RESERVED_LOCK ){
61204 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61207 int state = pPager->eState;
61212 if( pPager->eState==PAGER_READER ){
61217 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
61224 assert( state==pPager->eState );
61227 sqlite3OsClose(pPager->jfd);
61232 return (int)pPager->journalMode;
61239 return (int)pPager->journalMode;
61249 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
61250 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
61255 ** Get/set the size-limit used for persistent journal files.
61257 ** Setting the size limit to -1 means no limit is enforced.
61258 ** An attempt to set a limit smaller than -1 is a no-op.
61261 if( iLimit>=-1 ){
61262 pPager->journalSizeLimit = iLimit;
61263 sqlite3WalLimit(pPager->pWal, iLimit);
61265 return pPager->journalSizeLimit;
61269 ** Return a pointer to the pPager->pBackup variable. The backup module
61275 return &pPager->pBackup;
61280 ** Unless this is an in-memory or temporary database, clear the pager cache.
61283 assert( MEMDB==0 || pPager->tempFile );
61284 if( pPager->tempFile==0 ) pager_reset(pPager);
61305 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
61317 if( pPager->pWal ){
61318 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
61319 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
61320 pPager->pBusyHandlerArg,
61321 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
61329 return sqlite3WalCallback(pPager->pWal);
61334 ** primitives necessary for write-ahead logging.
61337 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
61338 if( pPager->noLock ) return 0;
61339 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
61349 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
61362 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
61363 ** lock on the database file and use heap-memory to store the wal-index
61364 ** in. Otherwise, use the normal shared-memory.
61369 assert( pPager->pWal==0 && pPager->tempFile==0 );
61370 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
61372 /* If the pager is already in exclusive-mode, the WAL module will use
61373 ** heap-memory for the wal-index instead of the VFS shared-memory
61377 if( pPager->exclusiveMode ){
61385 rc = sqlite3WalOpen(pPager->pVfs,
61386 pPager->fd, pPager->zWal, pPager->exclusiveMode,
61387 pPager->journalSizeLimit, &pPager->pWal
61401 ** file (not a temp file or an in-memory database), and the WAL file
61407 ** If the pager is open on a temp-file (or in-memory database), or if
61413 int *pbOpen /* OUT: Set to true if call is a no-op */
61418 assert( pPager->eState==PAGER_OPEN || pbOpen );
61419 assert( pPager->eState==PAGER_READER || !pbOpen );
61421 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
61423 if( !pPager->tempFile && !pPager->pWal ){
61427 sqlite3OsClose(pPager->jfd);
61431 pPager->journalMode = PAGER_JOURNALMODE_WAL;
61432 pPager->eState = PAGER_OPEN;
61453 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
61455 /* If the log file is not already open, but does exist in the file-system,
61459 if( !pPager->pWal ){
61464 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
61473 ** the database file, the log and log-summary files will be deleted.
61475 if( rc==SQLITE_OK && pPager->pWal ){
61478 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
61479 pPager->pageSize, (u8*)pPager->pTmpSpace);
61480 pPager->pWal = 0;
61482 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
61490 ** If pager pPager is a wal-mode database not in exclusive locking mode,
61497 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
61498 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
61509 sqlite3WalDb(pPager->pWal, db);
61521 if( pPager->pWal ){
61522 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
61537 if( pPager->pWal ){
61538 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
61551 if( pPager->pWal ){
61552 rc = sqlite3WalSnapshotRecover(pPager->pWal);
61573 if( pPager->pWal ){
61574 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
61586 assert( pPager->pWal );
61587 sqlite3WalSnapshotUnlock(pPager->pWal);
61595 ** A read-lock must be held on the pager when this function is called. If
61602 assert( pPager->eState>=PAGER_READER );
61603 return sqlite3WalFramesize(pPager->pWal);
61623 ** This file contains the implementation of a write-ahead log (WAL) used in
61626 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
61645 ** big-endian 32-bit unsigned integer values:
61651 ** 16: Salt-1, random integer incremented with each checkpoint
61652 ** 20: Salt-2, a different random integer changing with each ckpt
61653 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
61654 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
61656 ** Immediately following the wal-header are zero or more frames. Each
61657 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
61658 ** of page data. The frame-header is six big-endian 32-bit unsigned
61664 ** 8: Salt-1 (copied from the header)
61665 ** 12: Salt-2 (copied from the header)
61666 ** 16: Checksum-1.
61667 ** 20: Checksum-2.
61672 ** (1) The salt-1 and salt-2 values in the frame-header match
61673 ** salt values in the wal-header
61675 ** (2) The checksum values in the final 8 bytes of the frame-header
61680 ** The checksum is computed using 32-bit big-endian integers if the
61682 ** is computed using little-endian if the magic number is 0x377f0682.
61684 ** big-endian format regardless of which byte order is used to compute
61686 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
61689 ** for i from 0 to n-1 step 2:
61696 ** of the sequence being summed.) The s1 value spans all 32-bit
61699 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
61700 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
61701 ** The VFS.xSync operations serve as write barriers - all writes launched
61705 ** After each checkpoint, the salt-1 value is incremented and the salt-2
61734 ** data structure called the wal-index is maintained to expedite the
61737 ** WAL-INDEX FORMAT
61739 ** Conceptually, the wal-index is shared memory, though VFS implementations
61740 ** might choose to implement the wal-index using a mmapped file. Because
61741 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
61745 ** In the default unix and windows implementation, the wal-index is a mmapped
61746 ** file whose name is the database name with a "-shm" suffix added. For that
61747 ** reason, the wal-index is sometimes called the "shm" file.
61749 ** The wal-index is transient. After a crash, the wal-index can (and should
61751 ** to either truncate or zero the header of the wal-index when the last
61752 ** connection to it closes. Because the wal-index is transient, it can
61753 ** use an architecture-specific format; it does not have to be cross-platform.
61755 ** as big endian, the wal-index can store multi-byte values in the native
61758 ** The purpose of the wal-index is to answer this question quickly: Given
61763 ** The wal-index consists of a header region, followed by an one or
61766 ** The wal-index header contains the total number of frames within the WAL
61772 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
61774 ** wal-index. The values are:
61779 ** Each index block contains two sections, a page-mapping that contains the
61780 ** database page number associated with each wal frame, and a hash-table
61782 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
61783 ** for the first index block) 32-bit page numbers. The first entry in the
61784 ** first index-block contains the database page number corresponding to the
61789 ** The last index block in a wal-index usually contains less than the full
61790 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
61792 ** allocated size of the page-mapping array - the page-mapping array merely
61796 ** can be found by scanning the page-mapping sections of each index block
61802 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
61807 ** 1-based index of an entry in the mapping section of the same
61808 ** index block. Let K be the 1-based index of the largest entry in
61823 ** wrap-around.) Because the hash table is never more than half full,
61828 ** current index block. Otherwise the iMax-th mapping entry of the
61846 ** and to the wal-index) might be using a different value K1, where K1>K0.
61852 ** in the first place - which is what reader one wants. Meanwhile, the
61875 ** The maximum (and only) versions of the wal and wal-index formats
61879 ** values in the wal-header are correct and (b) the version field is not
61882 ** Similarly, if a client successfully reads a wal-index header (i.e. the
61884 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
61899 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
61909 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
61919 ** The following object holds a copy of the wal-index header content.
61921 ** The actual header in the wal-index consists of two copies of this
61928 ** Or it can be 1 to represent a 65536-byte page. The latter case was
61932 u32 iVersion; /* Wal-index version */
61936 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
61946 ** A copy of the following object occurs in the wal-index immediately
61969 ** holds read-lock K, then the value in aReadMark[K] is no greater than
61972 ** a special case; its value is never used and it exists as a place-holder
62001 ** We assume that 32-bit loads are atomic and so no locks are needed in
62014 ** This is a schematic view of the complete 136-byte header of the
62015 ** wal-index file (also known as the -shm file):
62017 ** +-----------------------------+
62019 ** +-----------------------------+ |
62021 ** +-----------------------------+ |
62023 ** +-------+-------+-------------+ |
62025 ** +-------+-------+-------------+ |
62027 ** +-----------------------------+ | WalIndexHdr object
62029 ** +-----------------------------+ |
62032 ** +-----------------------------+ |
62035 ** +-----------------------------+ |
62038 ** +-----------------------------+
62040 ** +-----------------------------+ |
62042 ** +-----------------------------+ |
62044 ** +-------+-------+-------------+ |
62046 ** +-------+-------+-------------+ | Second copy of the
62048 ** +-----------------------------+ |
62050 ** +-----------------------------+ |
62053 ** +-----------------------------+ |
62056 ** +-----------------------------+ |
62059 ** +-----------------------------+
62061 ** +-----------------------------+
62067 ** +-------+-------+------+------+
62069 ** +-------+-------+------+------+ ) 8 lock bytes
62071 ** +-------+-------+------+------+
62073 ** +-----------------------------+
62075 ** +-----------------------------+
62080 ** only support mandatory file-locks, we do not read or write data
62093 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
62094 ** big-endian format in the first 4 bytes of a WAL file.
62097 ** file are calculated by treating all data as an array of 32-bit
62098 ** big-endian words. Otherwise, they are calculated by interpreting
62099 ** all data as 32-bit little-endian words.
62104 ** Return the offset of frame iFrame in the write-ahead log file,
62106 ** is to the start of the write-ahead log frame-header.
62109 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
62113 ** An open write-ahead log file is represented by an instance of the
62124 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
62126 i16 readLock; /* Which read lock is being held. -1 for none */
62128 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
62135 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
62136 WalIndexHdr hdr; /* Wal-index header for current transaction */
62140 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
62167 ** Each page of the wal-index mapping contains a hash-table made up of
62181 ** walIteratorInit() - Create a new iterator,
62182 ** walIteratorNext() - Step an iterator,
62183 ** walIteratorFree() - Free an iterator.
62196 } aSegment[1]; /* One for every 32KB page in the wal-index */
62200 ** Define the parameters of the hash tables in the wal-index file. There
62201 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
62202 ** wal-index.
62204 ** Changing any of these constants will alter the wal-index format and
62212 ** The block of page numbers associated with the first hash-table in a
62213 ** wal-index is smaller than usual. This is so that there is a complete
62214 ** hash-table on each aligned 32KB page of the wal-index.
62216 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
62218 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
62224 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
62225 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
62228 ** If the wal-index is currently smaller the iPage pages then the size
62229 ** of the wal-index might be increased, but only if it is safe to do
62230 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
62231 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
62235 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
62239 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
62248 /* Enlarge the pWal->apWiData[] array if required */
62249 if( pWal->nWiData<=iPage ){
62252 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
62257 memset((void*)&apNew[pWal->nWiData], 0,
62258 sizeof(u32*)*(iPage+1-pWal->nWiData));
62259 pWal->apWiData = apNew;
62260 pWal->nWiData = iPage+1;
62264 assert( pWal->apWiData[iPage]==0 );
62265 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
62266 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
62267 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
62269 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
62270 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
62272 assert( pWal->apWiData[iPage]!=0
62274 || (pWal->writeLock==0 && iPage==0) );
62275 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
62279 pWal->readOnly |= WAL_SHM_RDONLY;
62286 *ppPage = pWal->apWiData[iPage];
62295 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
62302 ** Return a pointer to the WalCkptInfo structure in the wal-index.
62305 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62306 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
62310 ** Return a pointer to the WalIndexHdr structure in the wal-index.
62313 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62314 return (volatile WalIndexHdr*)pWal->apWiData[0];
62318 ** The argument to this macro must be of type u32. On a little-endian
62320 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
62322 ** of the input value as a little-endian integer.
62339 int nativeCksum, /* True for native byte-order, false for non-native */
62382 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
62383 sqlite3OsShmBarrier(pWal->pDbFd);
62388 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
62390 ** might give false-positive TSAN warnings.
62392 ** See tag-20200519-1.
62401 ** Write the header information in pWal->hdr into the wal-index.
62403 ** The checksum on pWal->hdr is updated before it is written.
62409 assert( pWal->writeLock );
62410 pWal->hdr.isInit = 1;
62411 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
62412 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
62413 /* Possible TSAN false-positive. See tag-20200519-1 */
62414 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62416 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
62421 ** supplied by the caller. A frame-header is made up of a series of
62422 ** 4-byte big-endian integers, as follows:
62427 ** 8: Salt-1 (copied from the wal-header)
62428 ** 12: Salt-2 (copied from the wal-header)
62429 ** 16: Checksum-1.
62430 ** 20: Checksum-2.
62433 Wal *pWal, /* The write-ahead log */
62435 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
62439 int nativeCksum; /* True for native byte-order checksums */
62440 u32 *aCksum = pWal->hdr.aFrameCksum;
62444 if( pWal->iReCksum==0 ){
62445 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
62447 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
62449 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62464 Wal *pWal, /* The write-ahead log */
62470 int nativeCksum; /* True for native byte-order checksums */
62471 u32 *aCksum = pWal->hdr.aFrameCksum;
62475 /* A frame is only valid if the salt values in the frame-header
62476 ** match the salt values in the wal-header.
62478 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
62490 ** all prior frams, the first 16 bytes of this frame-header,
62491 ** and the frame-data matches the checksum in the last 8
62492 ** bytes of this frame-header.
62494 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
62496 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
62520 return "WRITE-LOCK";
62522 return "CKPT-LOCK";
62524 return "RECOVER-LOCK";
62527 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
62528 lockIdx-WAL_READ_LOCK(0));
62537 ** A lock cannot be moved directly between shared and exclusive - it must go
62540 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
62544 if( pWal->exclusiveMode ) return SQLITE_OK;
62545 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
62547 WALTRACE(("WAL%p: acquire SHARED-%s %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, 1,
62556 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
62560 if( pWal->exclusiveMode ) return SQLITE_OK;
62561 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62563 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
62565 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
62569 if( pWal->exclusiveMode ) return;
62570 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
62572 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
62578 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
62583 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
62584 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
62587 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
62592 ** of a page hash table in the wal-index. This becomes the return value
62597 volatile ht_slot *aHash; /* Start of the wal-index hash table */
62604 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
62607 ** Set output variable pLoc->aHash to point to the start of the hash table
62608 ** in the wal-index file. Set pLoc->iZero to one less than the frame
62611 ** (pLoc->iZero+N) in the log.
62613 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
62614 ** first frame indexed by the hash table, frame (pLoc->iZero).
62623 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
62626 if( pLoc->aPgno ){
62627 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
62629 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
62630 pLoc->iZero = 0;
62632 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
62641 ** Return the number of the wal-index page that contains the hash-table
62642 ** and page-number array that contain entries corresponding to WAL frame
62643 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
62647 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
62664 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
62666 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
62671 ** than pWal->hdr.mxFrame.
62673 ** This function is called whenever pWal->hdr.mxFrame is decreased due
62676 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
62678 ** pWal->hdr.mxFrame advances to the point where those hash tables are
62687 assert( pWal->writeLock );
62688 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
62689 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
62690 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
62692 if( pWal->hdr.mxFrame==0 ) return;
62694 /* Obtain pointers to the hash-table and page-number array containing
62695 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
62696 ** that the page said hash-table and array reside on is already mapped.(1)
62698 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
62699 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
62700 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
62701 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
62703 /* Zero all hash-table entries that correspond to frame numbers greater
62704 ** than pWal->hdr.mxFrame.
62706 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
62715 ** frame numbers greater than pWal->hdr.mxFrame.
62717 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
62740 ** Set an entry in the wal-index that will map database page number
62745 WalHashLoc sLoc; /* Wal-index hash table location */
62749 /* Assuming the wal-index file was successfully mapped, populate the
62754 int idx; /* Value to write to hash-table slot */
62757 idx = iFrame - sLoc.iZero;
62760 /* If this is the first entry to be added to this hash-table, zero the
62764 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
62773 ** the hash-table before writing any new entries.
62775 if( sLoc.aPgno[idx-1] ){
62777 assert( !sLoc.aPgno[idx-1] );
62780 /* Write the aPgno[] array entry and the hash-table slot. */
62783 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
62785 sLoc.aPgno[idx-1] = iPage;
62801 ** thing to check, so only do this occasionally - not on every
62823 ** Recover the wal-index by reading the write-ahead log file.
62826 ** wal-index to prevent other threads/processes from doing anything
62827 ** with the WAL or wal-index while recovery is running. The
62844 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
62847 assert( pWal->writeLock );
62848 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
62849 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
62856 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
62858 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
62865 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
62873 u32 iPg; /* Current 32KB wal-index page */
62877 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
62890 || szPage&(szPage-1)
62896 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
62897 pWal->szPage = szPage;
62898 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
62899 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
62902 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
62903 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
62905 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
62906 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
62930 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
62935 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
62940 pWal->apWiData[iPg] = aPrivate;
62948 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
62955 /* If nTruncate is non-zero, this is a commit record. */
62957 pWal->hdr.mxFrame = iFrame;
62958 pWal->hdr.nPage = nTruncate;
62959 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
62962 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
62963 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
62966 pWal->apWiData[iPg] = aShare;
62978 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
63007 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
63008 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
63011 /* Reset the checkpoint-header. This is safe because this thread is
63013 ** checkpointers. Then set the values of read-mark slots 1 through N.
63016 pInfo->nBackfill = 0;
63017 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
63018 pInfo->aReadMark[0] = 0;
63022 if( i==1 && pWal->hdr.mxFrame ){
63023 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
63025 pInfo->aReadMark[i] = READMARK_NOT_USED;
63038 if( pWal->hdr.nPage ){
63041 pWal->hdr.mxFrame, pWal->zWalName
63048 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
63053 ** Close an open wal-index.
63056 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
63058 for(i=0; i<pWal->nWiData; i++){
63059 sqlite3_free((void *)pWal->apWiData[i]);
63060 pWal->apWiData[i] = 0;
63063 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
63064 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
63075 ** client from unlinking the WAL or wal-index file. If another process
63084 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
63087 int bNoShm, /* True to run in heap-memory mode */
63099 ** of these constants would result in an incompatible on-disk format
63100 ** for the -shm file. Any change that causes one of these asserts to
63104 ** This table also serves as a helpful cross-reference when trying to
63105 ** interpret hex dumps of the -shm file.
63145 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
63150 pRet->pVfs = pVfs;
63151 pRet->pWalFd = (sqlite3_file *)&pRet[1];
63152 pRet->pDbFd = pDbFd;
63153 pRet->readLock = -1;
63154 pRet->mxWalSize = mxWalSize;
63155 pRet->zWalName = zWalName;
63156 pRet->syncHeader = 1;
63157 pRet->padToSectorBoundary = 1;
63158 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
63160 /* Open file handle on the write-ahead log file. */
63162 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
63164 pRet->readOnly = WAL_RDONLY;
63169 sqlite3OsClose(pRet->pWalFd);
63173 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
63175 pRet->padToSectorBoundary = 0;
63187 if( pWal ) pWal->mxWalSize = iLimit;
63209 iMin = p->iPrior;
63211 for(i=p->nSegment-1; i>=0; i--){
63212 struct WalSegment *pSegment = &p->aSegment[i];
63213 while( pSegment->iNext<pSegment->nEntry ){
63214 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
63218 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
63222 pSegment->iNext++;
63226 *piPage = p->iPrior = iRet;
63254 const u32 *aContent, /* Pages in wal - keys for the sort */
63318 ht_slot *aList; /* Pointer to sub-list content */
63326 struct Sublist aSub[13]; /* Array of sub-lists */
63330 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
63339 assert( p->aList && p->nList<=(1<<iSub) );
63340 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
63341 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63352 assert( p->nList<=(1<<iSub) );
63353 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
63354 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
63364 assert( aContent[aList[i]] > aContent[aList[i-1]] );
63380 ** nBackfill or earlier may be included - excluding them is an optimization
63396 ht_slot *aTmp; /* Temp space used by merge-sort */
63402 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
63403 iLast = pWal->hdr.mxFrame;
63408 + (nSegment-1)*sizeof(struct WalSegment)
63415 p->nSegment = nSegment;
63417 /* Allocate temporary space used by the merge-sort routine. This block
63437 nEntry = (int)(iLast - sLoc.iZero);
63439 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
63441 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
63448 p->aSegment[i].iZero = sLoc.iZero;
63449 p->aSegment[i].nEntry = nEntry;
63450 p->aSegment[i].aIndex = aIndex;
63451 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
63468 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
63473 if( pWal->db ){
63474 int tmout = pWal->db->busyTimeout;
63478 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
63491 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
63505 assert( pWal->readLock<0 || bLock==0 );
63507 assert( pWal->db );
63511 pWal->writeLock = 1;
63515 }else if( pWal->writeLock ){
63517 pWal->writeLock = 0;
63526 pWal->db = db;
63550 ** busy-handler function. Invoke it and retry the lock until either the
63551 ** lock is successfully obtained or the busy-handler returns 0.
63574 ** The cache of the wal-index header must be valid to call this function.
63575 ** Return the page-size in bytes used by the database.
63578 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63587 ** file - there are no readers that may attempt to read a frame from
63590 ** This function updates the shared-memory structures so that the next
63595 ** new wal-index header. It should be passed a pseudo-random value (i.e.
63601 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
63602 pWal->nCkpt++;
63603 pWal->hdr.mxFrame = 0;
63605 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
63607 AtomicStore(&pInfo->nBackfill, 0);
63608 pInfo->nBackfillAttempted = 0;
63609 pInfo->aReadMark[1] = 0;
63610 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
63611 assert( pInfo->aReadMark[0]==0 );
63623 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
63629 ** in the WAL and can be recovered following a power-loss or hard reset.
63636 ** This routine uses and updates the nBackfill field of the wal-index header.
63655 int szPage; /* Database page-size */
63668 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63670 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63679 mxSafeFrame = pWal->hdr.mxFrame;
63680 mxPage = pWal->hdr.nPage;
63682 u32 y = AtomicLoad(pInfo->aReadMark+i);
63684 assert( y<=pWal->hdr.mxFrame );
63688 AtomicStore(pInfo->aReadMark+i, iMark);
63700 if( pInfo->nBackfill<mxSafeFrame ){
63701 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
63708 u32 nBackfill = pInfo->nBackfill;
63710 pInfo->nBackfillAttempted = mxSafeFrame;
63713 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
63721 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
63722 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
63724 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
63727 ** maximum size of the pending-byte page (65536 bytes), then
63731 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
63741 if( AtomicLoad(&db->u1.isInterrupted) ){
63742 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
63750 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
63752 iOffset = (iDbpage-1)*(i64)szPage;
63754 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
63757 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
63761 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
63762 i64 szDb = pWal->hdr.nPage*(i64)szPage;
63764 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
63766 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
63770 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
63791 assert( pWal->writeLock );
63792 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
63797 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
63798 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
63801 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
63807 ** wal-index header in shared memory, as all subsequent reader or
63811 ** the wal-index header do not match the contents of the
63812 ** file-system. To avoid this, update the wal-index header to
63815 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
63817 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63835 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
63837 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
63841 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
63857 int isDelete = 0; /* True to unlink wal and wal-index files */
63860 ** ordinary, rollback-mode locking methods, this guarantees that the
63863 ** the wal and wal-index files.
63868 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
63870 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
63871 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63877 int bPersist = -1;
63879 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
63883 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
63886 }else if( pWal->mxWalSize>=0 ){
63890 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
63899 sqlite3OsClose(pWal->pWalFd);
63902 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
63906 sqlite3_free((void *)pWal->apWiData);
63913 ** Try to read the wal-index header. Return 0 on success and 1 if
63916 ** The wal-index is in shared memory. Another thread or process might
63923 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
63926 ** If the checksum cannot be verified return non-zero. If the header
63934 /* The first page of the wal-index must be mapped at this point. */
63935 assert( pWal->nWiData>0 && pWal->apWiData[0] );
63940 ** from the file. If this happens, return non-zero.
63942 ** tag-20200519-1:
63943 ** There are two copies of the header at the beginning of the wal-index.
63947 ** give false-positive warnings about these accesses because the tools do not
63948 ** account for the double-read and the memory barrier. The use of mutexes
63954 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
63962 return 1; /* Malformed header - probably all zeros */
63964 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
63969 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
63971 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
63972 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
63973 testcase( pWal->szPage<=32768 );
63974 testcase( pWal->szPage>=65536 );
63985 #define WAL_RETRY (-1)
63988 ** Read the wal-index header from the wal-index and into pWal->hdr.
63989 ** If the wal-header appears to be corrupt, try to reconstruct the
63990 ** wal-index from the WAL before returning.
63992 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
63993 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
63996 ** If the wal-index header is successfully read, return SQLITE_OK.
64002 volatile u32 *page0; /* Chunk of wal-index containing header */
64004 /* Ensure that page 0 of the wal-index (the page that contains the
64005 ** wal-index header) is mapped. Return early if an error occurs here.
64012 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
64014 ** confirm that another write-capable connection has the shared-memory
64015 ** open, and hence the content of the shared-memory is unreliable,
64016 ** since the shared-memory might be inconsistent with the WAL file
64019 assert( pWal->writeLock==0 );
64020 assert( pWal->readOnly & WAL_SHM_RDONLY );
64021 pWal->bShmUnreliable = 1;
64022 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
64025 return rc; /* Any other non-OK return is just an error */
64028 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
64032 assert( page0!=0 || pWal->writeLock==0 );
64034 /* If the first page of the wal-index has been mapped, try to read the
64035 ** wal-index header immediately, without holding any lock. This usually
64036 ** works, but may fail if the wal-index header is corrupt or currently
64045 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
64051 int bWriteLock = pWal->writeLock;
64053 pWal->writeLock = 1;
64057 /* If the wal-index header is still malformed even while holding
64066 pWal->writeLock = 0;
64074 ** sure the wal-index was not constructed with some future format that
64077 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
64080 if( pWal->bShmUnreliable ){
64083 pWal->bShmUnreliable = 0;
64084 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
64090 pWal->exclusiveMode = WAL_NORMAL_MODE;
64097 ** Open a transaction in a connection where the shared-memory is read-only
64098 ** and where we cannot verify that there is a separate write-capable connection
64099 ** on hand to keep the shared-memory up-to-date with the WAL file.
64101 ** This can happen, for example, when the shared-memory is implemented by
64102 ** memory-mapping a *-shm file, where a prior writer has shut down and
64103 ** left the *-shm file on disk, and now the present connection is trying
64104 ** to use that database but lacks write permission on the *-shm file.
64109 ** The *-wal file has been read and an appropriate wal-index has been
64110 ** constructed in pWal->apWiData[] using heap memory instead of shared
64117 ** WAL_RETRY, then the heap memory wal-index has been discarded and
64119 ** beginning (including attempting to map the *-shm file).
64132 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
64134 assert( pWal->bShmUnreliable );
64135 assert( pWal->readOnly & WAL_SHM_RDONLY );
64136 assert( pWal->nWiData>0 && pWal->apWiData[0] );
64146 pWal->readLock = 0;
64148 /* Check to see if a separate writer has attached to the shared-memory area,
64149 ** thus making the shared-memory "reliable" again. Do this by invoking
64153 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
64154 ** cause the heap-memory WAL-index to be discarded and the actual
64166 ** even if some external agent does a "chmod" to make the shared-memory
64170 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
64171 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
64177 /* We reach this point only if the real shared-memory is still unreliable.
64178 ** Assume the in-memory WAL-index substitute is correct and load it
64179 ** into pWal->hdr.
64181 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
64186 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
64191 /* If the wal file is too small to contain a wal-header and the
64192 ** wal-index header has mxFrame==0, then it must be safe to proceed
64198 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
64203 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
64207 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
64209 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
64216 assert( (pWal->szPage & (pWal->szPage-1))==0 );
64217 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
64218 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
64227 ** wal file since the heap-memory wal-index was created. If so, the
64228 ** heap-memory wal-index is discarded and WAL_RETRY returned to
64230 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
64231 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
64232 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
64240 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
64244 /* If nTruncate is non-zero, then a complete transaction has been
64252 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
64253 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
64259 for(i=0; i<pWal->nWiData; i++){
64260 sqlite3_free((void*)pWal->apWiData[i]);
64261 pWal->apWiData[i] = 0;
64263 pWal->bShmUnreliable = 0;
64282 ** to make a copy of the wal-index header into pWal->hdr. If the
64283 ** wal-index header has changed, *pChanged is set to 1 (as an indication
64285 ** flushed.) When useWal==1, the wal-index header is assumed to already
64296 ** bad luck when there is lots of contention for the wal-index, but that
64300 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
64301 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
64304 ** including frame number aReadMark[pWal->readLock]. The reader will
64305 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
64306 ** Or if pWal->readLock==0, then the reader will ignore the WAL
64309 ** this routine will always set pWal->readLock>0 on success.
64311 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
64321 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
64328 assert( pWal->readLock<0 ); /* Not currently locked */
64331 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
64339 ** paged out or take a page-fault that is time-consuming to resolve,
64353 VVA_ONLY( pWal->lockError = 1; )
64356 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
64357 sqlite3OsSleep(pWal->pVfs, nDelay);
64362 if( pWal->bShmUnreliable==0 ){
64374 if( pWal->apWiData[0]==0 ){
64379 ** code that determines whether or not the shared-memory region
64393 else if( pWal->bShmUnreliable ){
64398 assert( pWal->nWiData>0 );
64399 assert( pWal->apWiData[0]!=0 );
64401 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
64403 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
64412 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
64429 pWal->readLock = 0;
64439 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
64443 mxFrame = pWal->hdr.mxFrame;
64445 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
64446 mxFrame = pWal->pSnapshot->mxFrame;
64450 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
64457 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
64463 AtomicStore(pInfo->aReadMark+i,mxFrame);
64474 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
64482 /* Now that the read-lock has been obtained, check that neither the
64483 ** value in the aReadMark[] array or the contents of the wal-index
64486 ** It is necessary to check that the wal-index header did not change
64487 ** between the time it was read and when the shared-lock was obtained
64490 ** that occur later in the log than pWal->hdr.mxFrame may have been
64493 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
64496 ** Before checking that the live wal-index header has not changed
64499 ** to read any frames earlier than minFrame from the wal file - they
64503 ** nBackfill and checking that the wal-header in shared-memory still
64504 ** matches the one cached in pWal->hdr, it is guaranteed that the
64505 ** checkpointer that set nBackfill was not working with a wal-index
64506 ** header newer than that cached in pWal->hdr. If it were, that could
64508 ** a version of page X that lies before pWal->minFrame (call that version
64511 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
64514 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
64516 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
64518 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
64519 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
64524 assert( mxReadMark<=pWal->hdr.mxFrame );
64525 pWal->readLock = (i16)mxI;
64539 ** This is only really safe if the file-system is such that any page
64553 assert( pWal->readLock>=0 );
64557 int szPage = (int)pWal->szPage;
64560 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
64567 u32 i = pInfo->nBackfillAttempted;
64568 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
64576 assert( i - sLoc.iZero - 1 >=0 );
64577 pgno = sLoc.aPgno[i-sLoc.iZero-1];
64578 iDbOff = (i64)(pgno-1) * szPage;
64582 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
64585 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
64593 pInfo->nBackfillAttempted = i-1;
64611 ** it takes a snapshot of the state of the WAL and wal-index for the current
64613 ** Other threads might append new content to the WAL and wal-index but
64618 ** Pager layer will use this to know that its cache is stale and
64626 WalIndexHdr *pSnapshot = pWal->pSnapshot;
64629 assert( pWal->ckptLock==0 );
64633 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64641 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
64644 ** before checking pInfo->nBackfillAttempted. */
64652 pWal->ckptLock = 1;
64666 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
64668 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
64669 ** is populated with the wal-index header corresponding to the head
64677 ** pSnapshot->mxFrame into the database file. Note that the
64682 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
64683 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
64687 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
64688 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
64691 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
64692 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
64694 assert( pWal->readLock>0 );
64695 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
64701 /* A client using a non-current snapshot may not ignore any frames
64707 pWal->minFrame = 1;
64716 if( pWal->ckptLock ){
64719 pWal->ckptLock = 0;
64727 ** read-lock.
64731 if( pWal->readLock>=0 ){
64732 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
64733 pWal->readLock = -1;
64751 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
64756 assert( pWal->readLock>=0 || pWal->lockError );
64758 /* If the "last page" field of the wal-index header snapshot is 0, then
64760 ** in this case as an optimization. Likewise, if pWal->readLock==0,
64764 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
64774 ** that adds entries to the wal-index (and possibly to this hash
64778 ** read transaction was opened may have been written incorrectly -
64785 ** if we had exclusive access to the hash-table:
64788 ** This condition filters out normal hash-table collisions.
64792 ** table after the current read-transaction had started.
64794 iMinHash = walFramePage(pWal->minFrame);
64795 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
64810 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
64814 if( (nCollide--)==0 ){
64824 ** of the wal-index file content. Make sure the results agree with the
64829 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
64830 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
64857 sz = pWal->hdr.szPage;
64863 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
64870 if( pWal && ALWAYS(pWal->readLock>=0) ){
64871 return pWal->hdr.nPage;
64894 /* If the write-lock is already held, then it was obtained before the
64895 ** read-transaction was even opened, making this call a no-op.
64897 if( pWal->writeLock ){
64898 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
64905 assert( pWal->readLock>=0 );
64906 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
64908 if( pWal->readOnly ){
64919 pWal->writeLock = 1;
64925 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
64927 pWal->writeLock = 0;
64939 if( pWal->writeLock ){
64941 pWal->writeLock = 0;
64942 pWal->iReCksum = 0;
64943 pWal->truncateOnCommit = 0;
64950 ** function moves the write-pointer back to the start of the transaction.
64962 if( ALWAYS(pWal->writeLock) ){
64963 Pgno iMax = pWal->hdr.mxFrame;
64966 /* Restore the clients cache of the wal-index header to the state it
64969 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
64971 for(iFrame=pWal->hdr.mxFrame+1;
64977 ** (b) has an outstanding reference, then xUndo is either a no-op
64989 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
65001 assert( pWal->writeLock );
65002 aWalData[0] = pWal->hdr.mxFrame;
65003 aWalData[1] = pWal->hdr.aFrameCksum[0];
65004 aWalData[2] = pWal->hdr.aFrameCksum[1];
65005 aWalData[3] = pWal->nCkpt;
65017 assert( pWal->writeLock );
65018 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
65020 if( aWalData[3]!=pWal->nCkpt ){
65021 /* This savepoint was opened immediately after the write-transaction
65026 aWalData[3] = pWal->nCkpt;
65029 if( aWalData[0]<pWal->hdr.mxFrame ){
65030 pWal->hdr.mxFrame = aWalData[0];
65031 pWal->hdr.aFrameCksum[0] = aWalData[1];
65032 pWal->hdr.aFrameCksum[1] = aWalData[2];
65044 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
65048 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
65055 if( pWal->readLock==0 ){
65057 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
65058 if( pInfo->nBackfill>0 ){
65061 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65066 ** wal-index header to reflect this.
65069 ** at this point. But updating the actual wal-index header is also
65073 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
65079 pWal->readLock = -1;
65095 ** the next fsync should occur - passed from sqlite3WalFrames() into
65108 ** Do a sync when crossing the p->iSyncPoint boundary.
65121 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
65122 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
65123 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
65126 iAmt -= iFirstAmt;
65128 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
65129 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
65132 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
65147 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
65148 pData = pPage->pData;
65149 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
65153 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
65166 const int szPage = pWal->szPage;/* Database page size */
65169 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
65179 ** wal-file header. Otherwise, read them from the frame header of the
65181 assert( pWal->iReCksum>0 );
65182 if( pWal->iReCksum==1 ){
65185 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
65187 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
65188 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
65189 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
65191 iRead = pWal->iReCksum;
65192 pWal->iReCksum = 0;
65195 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
65202 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
65211 ** Write a set of frames to the log. The caller must hold the write-lock
65216 int szPage, /* Database page-size in bytes */
65234 assert( pWal->writeLock );
65241 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
65243 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
65248 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
65249 iFirst = pLive->mxFrame+1;
65253 ** log file, instead of appending to it at pWal->hdr.mxFrame.
65263 iFrame = pWal->hdr.mxFrame;
65265 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
65266 u32 aCksum[2]; /* Checksum for wal-header */
65271 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
65272 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
65273 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
65274 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
65278 pWal->szPage = szPage;
65279 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
65280 pWal->hdr.aFrameCksum[0] = aCksum[0];
65281 pWal->hdr.aFrameCksum[1] = aCksum[1];
65282 pWal->truncateOnCommit = 1;
65284 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
65285 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
65292 ** an out-of-order write following a WAL restart could result in
65297 if( pWal->syncHeader ){
65298 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
65302 assert( (int)pWal->szPage==szPage );
65306 w.pFd = pWal->pWalFd;
65314 for(p=pList; p; p=p->pDirty){
65319 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
65321 if( iFirst && (p->pDirty || isCommit==0) ){
65323 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
65328 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
65329 pWal->iReCksum = iWrite;
65331 pData = p->pData;
65332 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
65334 p->flags &= ~PGHDR_WAL_APPEND;
65341 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
65346 p->flags |= PGHDR_WAL_APPEND;
65350 if( isCommit && pWal->iReCksum ){
65371 if( pWal->padToSectorBoundary ){
65372 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
65373 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
65394 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
65395 i64 sz = pWal->mxWalSize;
65396 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
65400 pWal->truncateOnCommit = 0;
65403 /* Append data to the wal-index. It is not necessary to lock the
65404 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
65408 iFrame = pWal->hdr.mxFrame;
65409 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
65410 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
65412 rc = walIndexAppend(pWal, iFrame, p->pgno);
65417 nExtra--;
65418 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
65423 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
65426 pWal->hdr.mxFrame = iFrame;
65428 pWal->hdr.iChange++;
65429 pWal->hdr.nPage = nTruncate;
65431 /* If this is a commit, update the wal-index header too. */
65434 pWal->iCallback = iFrame;
65449 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
65465 int isChanged = 0; /* True if a new wal-index header is loaded */
65469 assert( pWal->ckptLock==0 );
65470 assert( pWal->writeLock==0 );
65472 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
65476 if( pWal->readOnly ) return SQLITE_READONLY;
65480 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
65484 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
65486 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
65489 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
65496 pWal->ckptLock = 1;
65498 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
65502 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
65503 ** immediately, and a busy-handler is configured, it is invoked and the
65504 ** writer lock retried until either the busy-handler returns 0 or the
65510 pWal->writeLock = 1;
65520 /* Read the wal-index header. */
65525 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
65526 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
65533 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
65541 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
65542 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
65547 /* If a new wal-index header was loaded before the checkpoint was
65548 ** performed, then the pager-cache associated with pWal is now
65549 ** out of date. So zero the cached wal-index header to ensure that
65553 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
65561 if( pWal->ckptLock ){
65563 pWal->ckptLock = 0;
65580 ret = pWal->iCallback;
65581 pWal->iCallback = 0;
65592 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
65594 ** transition out of exclusive-mode is successful, return 1. This
65599 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
65601 ** WAL is already in exclusive-locking mode - meaning that this
65602 ** routine is a no-op. The pager must already hold the exclusive lock
65605 ** If op is negative, then do a dry-run of the op==1 case but do
65612 assert( pWal->writeLock==0 );
65613 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
65615 /* pWal->readLock is usually set, but might be -1 if there was a
65616 ** prior error while attempting to acquire are read-lock. This cannot
65619 ** upgrade to exclusive-mode following such an error.
65621 assert( pWal->readLock>=0 || pWal->lockError );
65622 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
65625 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
65626 pWal->exclusiveMode = WAL_NORMAL_MODE;
65627 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
65628 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65630 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
65636 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
65637 assert( pWal->readLock>=0 );
65638 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
65639 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
65642 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
65648 ** Return true if the argument is non-NULL and the WAL module is using
65649 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
65650 ** WAL module is using shared-memory, return false.
65653 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
65666 assert( pWal->readLock>=0 && pWal->writeLock==0 );
65668 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
65676 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
65683 /* Try to open on pSnapshot when the next read-transaction starts
65689 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
65693 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
65702 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
65703 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
65704 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
65705 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
65725 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
65726 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
65750 ** read-lock. This function returns the database page-size if it is known,
65754 assert( pWal==0 || pWal->readLock>=0 );
65755 return (pWal ? pWal->szPage : 0);
65762 return pWal->pWalFd;
65799 ** This file implements an external (disk-based) database using BTrees.
65803 ** "Sorting And Searching", pages 473-480. Addison-Wesley
65809 ** ----------------------------------------------------------------
65810 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
65811 ** ----------------------------------------------------------------
65816 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
65838 ** page, or a pointer-map page.
65857 ** 40 60 15 4-byte meta values passed to higher layers
65862 ** 52 4 Largest root-page (auto/incr_vacuum)
65863 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
65866 ** 68 4 Application-ID
65868 ** 92 4 The version-valid-for number
65871 ** All of the integer values are big-endian (most significant byte first).
65879 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
65894 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
65897 ** |----------------|
65899 ** |----------------|
65901 ** |----------------|
65905 ** |----------------|
65908 ** |----------------| ^ Grows upwards
65911 ** |----------------|
65930 ** The cell pointer array contains zero or more 2-byte numbers which are
65961 ** appears first. A variable-length integer may not be more than 9 bytes long.
65963 ** allows a 64-bit integer to be encoded in 9 bytes.
65986 ** filled with data (pagesize - 4 bytes). The last page can have as little
66009 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
66016 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
66027 ** You can change this value at compile-time by specifying a
66028 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
66029 ** header must be exactly 16 bytes including the zero-terminator so
66041 ** first byte of on-disk image of every BTree page.
66051 ** is derived from the raw on-disk page content.
66058 ** stored in MemPage.pBt->mutex.
66062 u8 intKey; /* True if table b-trees. False for index b-trees */
66075 int nFree; /* Number of free bytes on the page. -1 for unknown */
66078 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
66079 ** non-overflow cell */
66083 u8 *aDataEnd; /* One byte past the end of the entire page - not just
66085 ** corruption-induced buffer overflow. */
66141 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
66155 ** If the shared-data extension is enabled, there may be multiple users
66167 # error wrong numeric code for no-transaction
66170 # error wrong numeric code for read-transaction
66173 # error wrong numeric code for write-transaction
66196 ** If a BtShared client fails to obtain a write-lock on a database
66197 ** table (because there exists one or more read-locks on the table),
66198 ** the shared-cache enters 'pending-lock' state and isPending is
66201 ** The shared-cache leaves the 'pending lock' state when either of
66207 ** while in the 'pending-lock' state, no connection may start a new
66210 ** This feature is included to help prevent writer-starvation.
66219 u8 autoVacuum; /* True if auto-vacuum is enabled */
66220 u8 incrVacuum; /* True if incr-vacuum is enabled */
66224 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
66227 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
66228 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
66237 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
66238 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
66242 BtLock *pLock; /* List of locks held on this shared-btree struct */
66258 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
66260 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
66272 u16 nSize; /* Size of the cell content on the main b-tree page */
66276 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
66279 ** root-node and 3 for all other internal nodes.
66288 ** b-tree within a database file.
66298 ** found at self->pBt->mutex.
66306 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
66307 ** sqlite3BtreePrevious() is no-op if skipNext<0.
66332 u8 curIntKey; /* Value of apPage[0]->intKey */
66334 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
66337 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
66363 ** Cursor is valid except that the Cursor.skipNext field is non-zero
66365 ** operation should be a no-op.
66390 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
66393 ** These macros define the location of the pointer-map entry for a
66398 ** PTRMAP_PAGENO returns the database page number of the pointer-map
66402 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
66404 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
66408 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
66425 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
66428 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
66436 ** overflow pages. The page-number identifies the previous
66439 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
66452 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
66453 assert( p->pBt->inTransaction>=p->inTrans );
66458 ** if the database supports auto-vacuum or not. Because it is used
66464 #define ISAUTOVACUUM (pBt->autoVacuum)
66475 ** the database. As the integrity-check proceeds, for each page used in
66476 ** the database the corresponding bit is set. This allows integrity-check to
66483 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
66489 const char *zPfx; /* Error message prefix */
66492 StrAccum errMsg; /* Accumulate the error message text here */
66493 u32 *heap; /* Min-heap used for analyzing cell coverage */
66498 ** Routines to read or write a two- and four-byte big-endian integer values.
66507 ** two-byte aligned address. get2bytea() is only used for accessing the
66526 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
66528 ** p->locked boolean to true.
66531 assert( p->locked==0 );
66532 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
66533 assert( sqlite3_mutex_held(p->db->mutex) );
66535 sqlite3_mutex_enter(p->pBt->mutex);
66536 p->pBt->db = p->db;
66537 p->locked = 1;
66541 ** Release the BtShared mutex associated with B-Tree handle p and
66542 ** clear the p->locked boolean.
66545 BtShared *pBt = p->pBt;
66546 assert( p->locked==1 );
66547 assert( sqlite3_mutex_held(pBt->mutex) );
66548 assert( sqlite3_mutex_held(p->db->mutex) );
66549 assert( p->db==pBt->db );
66551 sqlite3_mutex_leave(pBt->mutex);
66552 p->locked = 0;
66562 ** and this routine is a no-op. The underlying mutex is non-recursive.
66567 ** by all database connections. The p->pNext is a list of other
66570 ** p, then first unlock all of the others on p->pNext, then wait
66579 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
66580 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
66581 assert( p->pNext==0 || p->pNext->db==p->db );
66582 assert( p->pPrev==0 || p->pPrev->db==p->db );
66583 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
66586 assert( !p->locked || p->wantToLock>0 );
66587 assert( p->sharable || p->wantToLock==0 );
66590 assert( sqlite3_mutex_held(p->db->mutex) );
66594 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
66596 if( !p->sharable ) return;
66597 p->wantToLock++;
66598 if( p->locked ) return;
66615 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
66616 p->pBt->db = p->db;
66617 p->locked = 1;
66626 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66627 assert( pLater->sharable );
66628 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
66629 assert( !pLater->locked || pLater->wantToLock>0 );
66630 if( pLater->locked ){
66635 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
66636 if( pLater->wantToLock ){
66647 assert( sqlite3_mutex_held(p->db->mutex) );
66648 if( p->sharable ){
66649 assert( p->wantToLock>0 );
66650 p->wantToLock--;
66651 if( p->wantToLock==0 ){
66660 ** B-Tree is not marked as sharable.
66665 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
66666 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
66667 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
66668 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
66670 return (p->sharable==0 || p->locked);
66682 ** There is a corresponding leave-all procedures.
66693 assert( sqlite3_mutex_held(db->mutex) );
66694 for(i=0; i<db->nDb; i++){
66695 p = db->aDb[i].pBt;
66696 if( p && p->sharable ){
66701 db->noSharedCache = skipOk;
66704 if( db->noSharedCache==0 ) btreeEnterAll(db);
66709 assert( sqlite3_mutex_held(db->mutex) );
66710 for(i=0; i<db->nDb; i++){
66711 p = db->aDb[i].pBt;
66716 if( db->noSharedCache==0 ) btreeLeaveAll(db);
66728 if( !sqlite3_mutex_held(db->mutex) ){
66731 for(i=0; i<db->nDb; i++){
66733 p = db->aDb[i].pBt;
66734 if( p && p->sharable &&
66735 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
66746 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
66750 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
66759 assert( iDb>=0 && iDb<db->nDb );
66760 if( !sqlite3_mutex_held(db->mutex) ) return 0;
66762 p = db->aDb[iDb].pBt;
66764 return p->sharable==0 || p->locked==1;
66772 ** these two routines, all mutex operations are no-ops in that case and
66776 ** the ones below, are no-ops and are null #defines in btree.h.
66780 p->pBt->db = p->db;
66784 for(i=0; i<db->nDb; i++){
66785 Btree *p = db->aDb[i].pBt;
66787 p->pBt->db = p->db;
66802 sqlite3BtreeEnter(pCur->pBtree);
66806 sqlite3BtreeLeave(pCur->pBtree);
66826 ** This file implements an external (disk-based) database using BTrees.
66850 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
66855 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
66858 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
66871 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
66916 ** shared-cache table level locks. If the library is compiled with the
66917 ** shared-cache feature disabled, then there is only ever one user
66919 ** So define the lock related functions as no-ops.
66934 u64 n = pBt->nSeek;
66935 pBt->nSeek = 0;
66945 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
66946 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
66954 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
66965 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
66977 ** For example, when writing to a table with root-page iRoot via
66990 ** hold a write-lock on the schema table (root page 1). This is also
66995 Pgno iRoot, /* Root page of b-tree */
66996 int isIndex, /* True if iRoot is the root of an index b-tree */
66999 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
67004 ** and has the read-uncommitted flag set, then no lock is required.
67007 if( (pBtree->sharable==0)
67008 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
67015 ** the correct locks are held. So do not bother - just return true.
67018 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
67022 /* Figure out the root-page that the lock should be held on. For table
67023 ** b-trees, this is just the root page of the b-tree being read or
67024 ** written. For index b-trees, it is the root page of the associated
67029 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
67031 if( pIdx->tnum==iRoot ){
67038 iTab = pIdx->pTable->tnum;
67046 /* Search for the required lock. Either a write-lock on root-page iTab, a
67047 ** write-lock on the schema table, or (if the client is reading) a
67048 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
67049 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
67050 if( pLock->pBtree==pBtree
67051 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
67052 && pLock->eLock>=eLockType
67074 ** read-uncommitted flag set, then it is OK for the other object to
67084 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67085 if( p->pgnoRoot==iRoot
67086 && p->pBtree!=pBtree
67087 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
67098 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
67103 BtShared *pBt = p->pBt;
67108 assert( p->db!=0 );
67109 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
67111 /* If requesting a write-lock, then the Btree must have an open write
67115 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
67116 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
67118 /* This routine is a no-op if the shared-cache is not enabled */
67119 if( !p->sharable ){
67126 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
67127 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
67131 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67132 /* The condition (pIter->eLock!=eLock) in the following if(...)
67135 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
67141 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
67142 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
67143 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
67144 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
67146 assert( p==pBt->pWriter );
67147 pBt->btsFlags |= BTS_PENDING;
67158 ** Add a lock on the table with root-page iTable to the shared-btree used
67175 BtShared *pBt = p->pBt;
67181 assert( p->db!=0 );
67183 /* A connection with the read-uncommitted flag set will never try to
67184 ** obtain a read-lock using this function. The only read-lock obtained
67185 ** by a connection in read-uncommitted mode is on the sqlite_schema
67187 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
67189 /* This function should only be called on a sharable b-tree after it
67190 ** has been determined that no other b-tree holds a conflicting lock. */
67191 assert( p->sharable );
67195 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
67196 if( pIter->iTable==iTable && pIter->pBtree==p ){
67210 pLock->iTable = iTable;
67211 pLock->pBtree = p;
67212 pLock->pNext = pBt->pLock;
67213 pBt->pLock = pLock;
67217 ** and the requested lock. This means if a write-lock was already held
67218 ** and a read-lock requested, we don't incorrectly downgrade the lock.
67221 if( eLock>pLock->eLock ){
67222 pLock->eLock = eLock;
67239 BtShared *pBt = p->pBt;
67240 BtLock **ppIter = &pBt->pLock;
67243 assert( p->sharable || 0==*ppIter );
67244 assert( p->inTrans>0 );
67248 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
67249 assert( pLock->pBtree->inTrans>=pLock->eLock );
67250 if( pLock->pBtree==p ){
67251 *ppIter = pLock->pNext;
67252 assert( pLock->iTable!=1 || pLock==&p->lock );
67253 if( pLock->iTable!=1 ){
67257 ppIter = &pLock->pNext;
67261 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
67262 if( pBt->pWriter==p ){
67263 pBt->pWriter = 0;
67264 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
67265 }else if( pBt->nTransaction==2 ){
67275 pBt->btsFlags &= ~BTS_PENDING;
67280 ** This function changes all write-locks held by Btree p into read-locks.
67283 BtShared *pBt = p->pBt;
67284 if( pBt->pWriter==p ){
67286 pBt->pWriter = 0;
67287 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
67288 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
67289 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
67290 pLock->eLock = READ_LOCK;
67308 return sqlite3_mutex_held(p->pBt->mutex);
67312 ** database connetion. This is important in shared-cache mode. If the database
67313 ** connection pointers get out-of-sync, it is possible for routines like
67314 ** btreeInitPage() to reference an stale connection pointer that references a
67316 ** statements only and for the purpose of double-checking that the btree code
67317 ** does keep the database connection pointers up-to-date.
67321 return (p->pBtree->db==p->pBt->db);
67329 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
67332 ** Invalidate the overflow page-list cache for all cursors opened
67337 assert( sqlite3_mutex_held(pBt->mutex) );
67338 for(p=pBt->pCursor; p; p=p->pNext){
67351 ** cursors open on any row within the table with root-page pgnoRoot.
67364 assert( pBtree->hasIncrblobCur );
67366 pBtree->hasIncrblobCur = 0;
67367 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
67368 if( (p->curFlags & BTCF_Incrblob)!=0 ){
67369 pBtree->hasIncrblobCur = 1;
67370 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
67371 p->eState = CURSOR_INVALID;
67384 ** when a page that previously contained data becomes a free-list leaf
67389 ** free-list leaf pages:
67392 ** a free-list leaf page, the page is not written to the database
67393 ** (as free-list leaf pages contain no meaningful data). Sometimes
67397 ** 2) When a free-list leaf page is reused, its content is not read
67403 ** a page is moved to the free-list and then reused within the same
67405 ** it is moved to the free-list and it is also not journalled when it
67406 ** is extracted from the free-list and reused, then the original data
67411 ** moved to become a free-list leaf page, the corresponding bit is
67412 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
67419 if( !pBt->pHasContent ){
67420 assert( pgno<=pBt->nPage );
67421 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
67422 if( !pBt->pHasContent ){
67426 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
67427 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
67435 ** This function is called when a free-list leaf page is removed from the
67436 ** free-list for reuse. It returns false if it is safe to retrieve the
67437 ** page from the pager layer with the 'no-content' flag set. True otherwise.
67440 Bitvec *p = pBt->pHasContent;
67446 ** invoked at the conclusion of each write-transaction.
67449 sqlite3BitvecDestroy(pBt->pHasContent);
67450 pBt->pHasContent = 0;
67458 if( pCur->iPage>=0 ){
67459 for(i=0; i<pCur->iPage; i++){
67460 releasePageNotNull(pCur->apPage[i]);
67462 releasePageNotNull(pCur->pPage);
67463 pCur->iPage = -1;
67470 ** function saves the current cursor key in variables pCur->nKey and
67471 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
67475 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
67476 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
67477 ** set to point to a malloced buffer pCur->nKey bytes in size containing
67482 assert( CURSOR_VALID==pCur->eState );
67483 assert( 0==pCur->pKey );
67486 if( pCur->curIntKey ){
67488 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
67493 ** up to the size of 1 varint plus 1 8-byte value when the cursor
67497 pCur->nKey = sqlite3BtreePayloadSize(pCur);
67498 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
67500 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
67502 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
67503 pCur->pKey = pKey;
67511 assert( !pCur->curIntKey || !pCur->pKey );
67525 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
67526 assert( 0==pCur->pKey );
67529 if( pCur->curFlags & BTCF_Pinned ){
67532 if( pCur->eState==CURSOR_SKIPNEXT ){
67533 pCur->eState = CURSOR_VALID;
67535 pCur->skipNext = 0;
67541 pCur->eState = CURSOR_REQUIRESEEK;
67544 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
67553 ** the table with root-page iRoot. "Saving the cursor position" means that
67564 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
67574 assert( sqlite3_mutex_held(pBt->mutex) );
67575 assert( pExcept==0 || pExcept->pBt==pBt );
67576 for(p=pBt->pCursor; p; p=p->pNext){
67577 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
67580 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
67595 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
67596 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
67602 testcase( p->iPage>=0 );
67606 p = p->pNext;
67616 sqlite3_free(pCur->pKey);
67617 pCur->pKey = 0;
67618 pCur->eState = CURSOR_INVALID;
67637 KeyInfo *pKeyInfo = pCur->pKeyInfo;
67642 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
67647 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
67666 assert( pCur->eState>=CURSOR_REQUIRESEEK );
67667 if( pCur->eState==CURSOR_FAULT ){
67668 return pCur->skipNext;
67670 pCur->eState = CURSOR_INVALID;
67674 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
67677 sqlite3_free(pCur->pKey);
67678 pCur->pKey = 0;
67679 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
67680 if( skipNext ) pCur->skipNext = skipNext;
67681 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
67682 pCur->eState = CURSOR_SKIPNEXT;
67689 (p->eState>=CURSOR_REQUIRESEEK ? \
67709 assert( sizeof(pCur->eState)==1 );
67741 assert( pCur->eState!=CURSOR_VALID );
67747 if( pCur->eState!=CURSOR_VALID ){
67771 pCur->hints = x;
67778 ** number for the pointer-map page that contains the entry for the
67788 assert( sqlite3_mutex_held(pBt->mutex) );
67790 nPagesPerMapPage = (pBt->usableSize/5)+1;
67791 iPtrMap = (pgno-2)/nPagesPerMapPage;
67805 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
67806 ** a no-op. If an error occurs, the appropriate error code is written
67818 assert( sqlite3_mutex_held(pBt->mutex) );
67819 /* The super-journal page number must never be used as a pointer map page */
67822 assert( pBt->autoVacuum );
67828 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67845 assert( offset <= (int)pBt->usableSize-5 );
67849 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
67875 assert( sqlite3_mutex_held(pBt->mutex) );
67878 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
67889 assert( offset <= (int)pBt->usableSize-5 );
67911 ** 4-byte child pointer found on interior pages, if there is one.
67916 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
67918 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
67924 ** on a single B-tree page. Make necessary adjustments to the CellInfo
67945 minLocal = pPage->minLocal;
67946 maxLocal = pPage->maxLocal;
67947 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
67951 pInfo->nLocal = (u16)surplus;
67953 pInfo->nLocal = (u16)minLocal;
67955 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
67964 maxLocal = pPage->maxLocal;
67970 minLocal = pPage->minLocal;
67971 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
67995 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67996 assert( pPage->leaf==0 );
67997 assert( pPage->childPtrSize==4 );
68001 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
68002 pInfo->nPayload = 0;
68003 pInfo->nLocal = 0;
68004 pInfo->pPayload = 0;
68016 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68017 assert( pPage->leaf==0 || pPage->leaf==1 );
68018 assert( pPage->intKeyLeaf );
68019 assert( pPage->childPtrSize==0 );
68040 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
68043 ** This routine is a high-runner.
68073 pInfo->nKey = *(i64*)&iKey;
68074 pInfo->nPayload = nPayload;
68075 pInfo->pPayload = pIter;
68076 testcase( nPayload==pPage->maxLocal );
68077 testcase( nPayload==(u32)pPage->maxLocal+1 );
68078 if( nPayload<=pPage->maxLocal ){
68082 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68083 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68084 pInfo->nLocal = (u16)nPayload;
68097 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68098 assert( pPage->leaf==0 || pPage->leaf==1 );
68099 assert( pPage->intKeyLeaf==0 );
68100 pIter = pCell + pPage->childPtrSize;
68110 pInfo->nKey = nPayload;
68111 pInfo->nPayload = nPayload;
68112 pInfo->pPayload = pIter;
68113 testcase( nPayload==pPage->maxLocal );
68114 testcase( nPayload==(u32)pPage->maxLocal+1 );
68115 if( nPayload<=pPage->maxLocal ){
68119 pInfo->nSize = nPayload + (u16)(pIter - pCell);
68120 if( pInfo->nSize<4 ) pInfo->nSize = 4;
68121 pInfo->nLocal = (u16)nPayload;
68131 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
68139 ** data area of the btree-page. The return number includes the cell
68148 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
68158 pPage->xParseCell(pPage, pCell, &debuginfo);
68170 testcase( nSize==pPage->maxLocal );
68171 testcase( nSize==(u32)pPage->maxLocal+1 );
68172 if( nSize<=pPage->maxLocal ){
68173 nSize += (u32)(pIter - pCell);
68176 int minLocal = pPage->minLocal;
68177 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68178 testcase( nSize==pPage->maxLocal );
68179 testcase( nSize==(u32)pPage->maxLocal+1 );
68180 if( nSize>pPage->maxLocal ){
68183 nSize += 4 + (u16)(pIter - pCell);
68198 pPage->xParseCell(pPage, pCell, &debuginfo);
68203 assert( pPage->childPtrSize==4 );
68206 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
68207 return (u16)(pIter - pCell);
68220 pPage->xParseCell(pPage, pCell, &debuginfo);
68232 /* pIter now points at the 64-bit integer key value, a variable length
68243 testcase( nSize==pPage->maxLocal );
68244 testcase( nSize==(u32)pPage->maxLocal+1 );
68245 if( nSize<=pPage->maxLocal ){
68246 nSize += (u32)(pIter - pCell);
68249 int minLocal = pPage->minLocal;
68250 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
68251 testcase( nSize==pPage->maxLocal );
68252 testcase( nSize==(u32)pPage->maxLocal+1 );
68253 if( nSize>pPage->maxLocal ){
68256 nSize += 4 + (u16)(pIter - pCell);
68267 return pPage->xCellSize(pPage, findCell(pPage, iCell));
68275 ** pointer to an overflow page, insert an entry into the pointer-map for
68282 pPage->xParseCell(pPage, pCell, &info);
68285 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
68290 ovfl = get4byte(&pCell[info.nSize-4]);
68291 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
68299 ** page so that there are no free-blocks on the free-block list.
68304 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
68305 ** b-tree page so that there are no freeblocks or fragment bytes, all
68311 int pc; /* Address of the i-th cell */
68325 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68326 assert( pPage->pBt!=0 );
68327 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
68328 assert( pPage->nOverflow==0 );
68329 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68331 src = data = pPage->aData;
68332 hdr = pPage->hdrOffset;
68333 cellOffset = pPage->cellOffset;
68334 nCell = pPage->nCell;
68337 usableSize = pPage->pBt->usableSize;
68342 ** offsets to each pointer in the cell-pointer array than it is to
68346 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
68349 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
68363 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
68370 assert( cbrk+(iFree-top) <= usableSize );
68371 memmove(&data[cbrk], &data[top], iFree-top);
68383 iCellLast = usableSize - 4;
68386 u8 *pAddr; /* The i-th cell pointer */
68398 size = pPage->xCellSize(pPage, &src[pc]);
68399 cbrk -= size;
68409 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
68410 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart);
68418 assert( pPage->nFree>=0 );
68419 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
68426 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
68427 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68432 ** Search the free-list on page pPg for space to store a cell nByte bytes in
68434 ** from the free-list.
68436 ** If no suitable space can be found on the free-list, return NULL.
68446 const int hdr = pPg->hdrOffset; /* Offset to page header */
68447 u8 * const aData = pPg->aData; /* Page data */
68452 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
68457 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
68458 ** freeblock form a big-endian integer which is the size of the freeblock
68459 ** in bytes, including the 4-byte header. */
68462 if( (x = size - nByte)>=0 ){
68466 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
68470 /* Remove the slot from the free-list. Update the number of
68481 /* The slot remains on the free-list. Reduce its size to account
68498 if( pc>maxPC+nByte-4 ){
68506 ** Allocate nByte bytes of space from within the B-Tree page passed
68507 ** as the first argument. Write into *pIdx the index into pPage->aData[]
68519 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
68520 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
68526 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68527 assert( pPage->pBt );
68528 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68530 assert( pPage->nFree>=nByte );
68531 assert( pPage->nOverflow==0 );
68532 assert( nByte < (int)(pPage->pBt->usableSize-8) );
68534 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
68535 gap = pPage->cellOffset + 2*pPage->nCell;
68537 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
68540 ** However, that integer is too large to be stored in a 2-byte unsigned
68544 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
68546 if( top==0 && pPage->pBt->usableSize==65536 ){
68564 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
68565 *pIdx = g2 = (int)(pSpace-data);
68581 assert( pPage->nCell>0 || CORRUPT_DB );
68582 assert( pPage->nFree>=0 );
68583 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
68596 top -= nByte;
68598 assert( top+nByte <= (int)pPage->pBt->usableSize );
68604 ** Return a section of the pPage->aData to the freelist.
68605 ** The first byte of the new free block is pPage->aData[iStart]
68624 unsigned char *data = pPage->aData; /* Page content */
68627 assert( pPage->pBt!=0 );
68628 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68629 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
68630 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
68631 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68633 assert( iStart<=pPage->pBt->usableSize-4 );
68638 hdr = pPage->hdrOffset;
68650 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
68662 nFrag = iFreeBlk - iEnd;
68665 if( iEnd > pPage->pBt->usableSize ){
68668 iSize = iEnd - iStart;
68680 nFrag += iStart - iPtrEnd;
68681 iSize = iEnd - iPtr;
68686 data[hdr+7] -= nFrag;
68702 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
68709 pPage->nFree += iOrigSize;
68726 BtShared *pBt; /* A copy of pPage->pBt */
68728 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
68729 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68730 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
68732 pPage->childPtrSize = 4-4*pPage->leaf;
68733 pBt = pPage->pBt;
68735 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
68736 ** interior table b-tree page. */
68738 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
68739 ** leaf table b-tree page. */
68741 pPage->intKey = 1;
68742 if( pPage->leaf ){
68743 pPage->intKeyLeaf = 1;
68744 pPage->xCellSize = cellSizePtrTableLeaf;
68745 pPage->xParseCell = btreeParseCellPtr;
68747 pPage->intKeyLeaf = 0;
68748 pPage->xCellSize = cellSizePtrNoPayload;
68749 pPage->xParseCell = btreeParseCellPtrNoPayload;
68751 pPage->maxLocal = pBt->maxLeaf;
68752 pPage->minLocal = pBt->minLeaf;
68754 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
68755 ** interior index b-tree page. */
68757 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
68758 ** leaf index b-tree page. */
68760 pPage->intKey = 0;
68761 pPage->intKeyLeaf = 0;
68762 pPage->xCellSize = cellSizePtr;
68763 pPage->xParseCell = btreeParseCellPtrIndex;
68764 pPage->maxLocal = pBt->maxLocal;
68765 pPage->minLocal = pBt->minLocal;
68767 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
68769 pPage->intKey = 0;
68770 pPage->intKeyLeaf = 0;
68771 pPage->xCellSize = cellSizePtr;
68772 pPage->xParseCell = btreeParseCellPtrIndex;
68775 pPage->max1bytePayload = pBt->max1bytePayload;
68781 ** in the pPage->nFree field.
68784 int pc; /* Address of a freeblock within pPage->aData[] */
68786 u8 *data; /* Equal to pPage->aData */
68793 assert( pPage->pBt!=0 );
68794 assert( pPage->pBt->db!=0 );
68795 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68796 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
68797 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
68798 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
68799 assert( pPage->isInit==1 );
68800 assert( pPage->nFree<0 );
68802 usableSize = pPage->pBt->usableSize;
68803 hdr = pPage->hdrOffset;
68804 data = pPage->aData;
68805 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
68809 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
68810 iCellLast = usableSize - 4;
68813 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
68817 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
68821 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
68848 ** of the cell-content area plus the number of free bytes within
68849 ** the cell-content area. If this is greater than the usable-size
68851 ** serves to verify that the offset to the start of the cell-content
68857 pPage->nFree = (u16)(nFree - iCellFirst);
68870 int pc; /* Address of a freeblock within pPage->aData[] */
68871 u8 *data; /* Equal to pPage->aData */
68875 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
68876 usableSize = pPage->pBt->usableSize;
68877 iCellLast = usableSize - 4;
68878 data = pPage->aData;
68879 cellOffset = pPage->cellOffset;
68880 if( !pPage->leaf ) iCellLast--;
68881 for(i=0; i<pPage->nCell; i++){
68888 sz = pPage->xCellSize(pPage, &data[pc]);
68901 ** not contain a well-formed database page, then return
68903 ** guarantee that the page is well-formed. It only shows that
68907 u8 *data; /* Equal to pPage->aData */
68910 assert( pPage->pBt!=0 );
68911 assert( pPage->pBt->db!=0 );
68912 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68913 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
68914 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
68915 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
68916 assert( pPage->isInit==0 );
68918 pBt = pPage->pBt;
68919 data = pPage->aData + pPage->hdrOffset;
68920 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
68921 ** the b-tree page type. */
68925 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
68926 pPage->maskPage = (u16)(pBt->pageSize - 1);
68927 pPage->nOverflow = 0;
68928 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
68929 pPage->aCellIdx = data + pPage->childPtrSize + 8;
68930 pPage->aDataEnd = pPage->aData + pBt->pageSize;
68931 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
68932 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
68934 pPage->nCell = get2byte(&data[3]);
68935 if( pPage->nCell>MX_CELL(pBt) ){
68939 testcase( pPage->nCell==MX_CELL(pBt) );
68940 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
68944 assert( pPage->nCell>0
68945 || get2byteNotZero(&data[5])==(int)pBt->usableSize
68947 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
68948 pPage->isInit = 1;
68949 if( pBt->db->flags & SQLITE_CellSizeCk ){
68960 unsigned char *data = pPage->aData;
68961 BtShared *pBt = pPage->pBt;
68962 u8 hdr = pPage->hdrOffset;
68965 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
68966 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
68967 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
68968 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68969 assert( sqlite3_mutex_held(pBt->mutex) );
68970 if( pBt->btsFlags & BTS_FAST_SECURE ){
68971 memset(&data[hdr], 0, pBt->usableSize - hdr);
68977 put2byte(&data[hdr+5], pBt->usableSize);
68978 pPage->nFree = (u16)(pBt->usableSize - first);
68980 pPage->cellOffset = first;
68981 pPage->aDataEnd = &data[pBt->pageSize];
68982 pPage->aCellIdx = &data[first];
68983 pPage->aDataOfst = &data[pPage->childPtrSize];
68984 pPage->nOverflow = 0;
68985 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
68986 pPage->maskPage = (u16)(pBt->pageSize - 1);
68987 pPage->nCell = 0;
68988 pPage->isInit = 1;
68998 if( pgno!=pPage->pgno ){
68999 pPage->aData = sqlite3PagerGetData(pDbPage);
69000 pPage->pDbPage = pDbPage;
69001 pPage->pBt = pBt;
69002 pPage->pgno = pgno;
69003 pPage->hdrOffset = pgno==1 ? 100 : 0;
69005 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
69030 assert( sqlite3_mutex_held(pBt->mutex) );
69031 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
69044 assert( sqlite3_mutex_held(pBt->mutex) );
69045 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
69054 ** error, return ((unsigned int)-1).
69057 return pBt->nPage;
69061 return btreePagecount(p->pBt);
69069 ** And if the fetch fails, this routine must decrement pCur->iPage.
69071 ** The page is fetched as read-write unless pCur is not NULL and is
69072 ** a read-only cursor.
69082 int bReadOnly /* True for a read-only page */
69086 assert( sqlite3_mutex_held(pBt->mutex) );
69087 assert( pCur==0 || ppPage==&pCur->pPage );
69088 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
69089 assert( pCur==0 || pCur->iPage>0 );
69095 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
69100 if( (*ppPage)->isInit==0 ){
69107 assert( (*ppPage)->pgno==pgno || CORRUPT_DB );
69108 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
69112 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
69122 pCur->iPage--;
69123 pCur->pPage = pCur->apPage[pCur->iPage];
69139 assert( pPage->aData );
69140 assert( pPage->pBt );
69141 assert( pPage->pDbPage!=0 );
69142 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
69143 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
69144 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69145 sqlite3PagerUnrefNotNull(pPage->pDbPage);
69152 assert( pPage->aData );
69153 assert( pPage->pBt );
69154 assert( pPage->pDbPage!=0 );
69155 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
69156 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
69157 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69158 sqlite3PagerUnrefPageOne(pPage->pDbPage);
69178 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
69183 (*ppPage)->isInit = 0;
69203 if( pPage->isInit ){
69204 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
69205 pPage->isInit = 0;
69212 ** the call for every page that comes in for re-initing. */
69223 assert( pBt->db );
69224 assert( sqlite3_mutex_held(pBt->db->mutex) );
69225 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
69233 ** be exclusively in memory, or it might use a disk-based memory cache.
69237 ** If zFilename is ":memory:" then an in-memory database is created
69250 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
69267 /* Set the variable isMemdb to true for an in-memory database, or
69268 ** false for a file-based database.
69280 assert( sqlite3_mutex_held(db->mutex) );
69299 p->inTrans = TRANS_NONE;
69300 p->db = db;
69302 p->lock.pBtree = p;
69303 p->lock.iTable = 1;
69314 int nFullPathname = pVfs->mxPathname+1;
69318 p->sharable = 1;
69344 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
69345 assert( pBt->nRef>0 );
69346 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
69347 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
69349 for(iDb=db->nDb-1; iDb>=0; iDb--){
69350 Btree *pExisting = db->aDb[iDb].pBt;
69351 if( pExisting && pExisting->pBt==pBt ){
69359 p->pBt = pBt;
69360 pBt->nRef++;
69374 p->sharable = 1;
69396 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
69399 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
69400 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
69405 pBt->openFlags = (u8)flags;
69406 pBt->db = db;
69407 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
69408 p->pBt = pBt;
69410 pBt->pCursor = 0;
69411 pBt->pPage1 = 0;
69412 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
69414 pBt->btsFlags |= BTS_SECURE_DELETE;
69416 pBt->btsFlags |= BTS_OVERWRITE;
69418 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
69419 ** determined by the 2-byte integer located at an offset of 16 bytes from
69421 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
69422 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
69423 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
69424 pBt->pageSize = 0;
69426 /* If the magic name ":memory:" will create an in-memory database, then
69427 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
69430 ** regular file-name. In this case the auto-vacuum applies as per normal.
69433 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
69434 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
69439 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
69440 ** determined by the one-byte unsigned integer found at an offset of 20
69443 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
69445 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
69446 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
69449 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69451 pBt->usableSize = pBt->pageSize - nReserve;
69452 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
69457 pBt->nRef = 1;
69458 if( p->sharable ){
69462 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
69463 if( pBt->mutex==0 ){
69469 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
69481 if( p->sharable ){
69484 for(i=0; i<db->nDb; i++){
69485 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
69486 while( pSib->pPrev ){ pSib = pSib->pPrev; }
69487 if( (uptr)p->pBt<(uptr)pSib->pBt ){
69488 p->pNext = pSib;
69489 p->pPrev = 0;
69490 pSib->pPrev = p;
69492 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
69493 pSib = pSib->pNext;
69495 p->pNext = pSib->pNext;
69496 p->pPrev = pSib;
69497 if( p->pNext ){
69498 p->pNext->pPrev = p;
69500 pSib->pNext = p;
69511 if( pBt && pBt->pPager ){
69512 sqlite3PagerClose(pBt->pPager, 0);
69520 /* If the B-Tree was successfully opened, set the pager-cache size to the
69521 ** default value. Except, when opening on an existing shared pager-cache,
69522 ** do not change the pager-cache size.
69528 pFile = sqlite3PagerFile(pBt->pPager);
69529 if( pFile->pMethods ){
69530 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
69553 assert( sqlite3_mutex_notheld(pBt->mutex) );
69556 pBt->nRef--;
69557 if( pBt->nRef<=0 ){
69559 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
69562 while( ALWAYS(pList) && pList->pNext!=pBt ){
69563 pList=pList->pNext;
69566 pList->pNext = pBt->pNext;
69570 sqlite3_mutex_free(pBt->mutex);
69582 ** Make sure pBt->pTmpSpace points to an allocation of
69583 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
69588 assert( pBt->pTmpSpace==0 );
69591 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
69592 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
69593 if( pBt->pTmpSpace==0 ){
69594 BtCursor *pCur = pBt->pCursor;
69595 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
69600 /* One of the uses of pBt->pTmpSpace is to format cells before
69613 ** left-child pointer to the beginning of a cell.
69615 memset(pBt->pTmpSpace, 0, 8);
69616 pBt->pTmpSpace += 4;
69621 ** Free the pBt->pTmpSpace allocation
69624 if( pBt->pTmpSpace ){
69625 pBt->pTmpSpace -= 4;
69626 sqlite3PageFree(pBt->pTmpSpace);
69627 pBt->pTmpSpace = 0;
69635 BtShared *pBt = p->pBt;
69638 assert( sqlite3_mutex_held(p->db->mutex) );
69644 BtCursor *pCur = pBt->pCursor;
69647 pCur = pCur->pNext;
69648 assert( pTmp->pBtree!=p );
69655 ** The call to sqlite3BtreeRollback() drops any table-locks held by
69661 /* If there are still other outstanding references to the shared-btree
69663 ** up the shared-btree.
69665 assert( p->wantToLock==0 && p->locked==0 );
69666 if( !p->sharable || removeFromSharingList(pBt) ){
69672 assert( !pBt->pCursor );
69673 sqlite3PagerClose(pBt->pPager, p->db);
69674 if( pBt->xFreeSchema && pBt->pSchema ){
69675 pBt->xFreeSchema(pBt->pSchema);
69677 sqlite3DbFree(0, pBt->pSchema);
69683 assert( p->wantToLock==0 );
69684 assert( p->locked==0 );
69685 if( p->pPrev ) p->pPrev->pNext = p->pNext;
69686 if( p->pNext ) p->pNext->pPrev = p->pPrev;
69701 BtShared *pBt = p->pBt;
69702 assert( sqlite3_mutex_held(p->db->mutex) );
69704 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
69720 BtShared *pBt = p->pBt;
69722 assert( sqlite3_mutex_held(p->db->mutex) );
69724 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
69735 BtShared *pBt = p->pBt;
69736 assert( sqlite3_mutex_held(p->db->mutex) );
69738 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
69749 ** is a very low but non-zero probability of damage. Level 3 reduces the
69757 BtShared *pBt = p->pBt;
69758 assert( sqlite3_mutex_held(p->db->mutex) );
69760 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
69789 BtShared *pBt = p->pBt;
69792 pBt->nReserveWanted = nReserve;
69793 x = pBt->pageSize - pBt->usableSize;
69795 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
69801 ((pageSize-1)&pageSize)==0 ){
69803 assert( !pBt->pCursor );
69805 pBt->pageSize = (u32)pageSize;
69808 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
69809 pBt->usableSize = pBt->pageSize - (u16)nReserve;
69810 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
69819 return p->pBt->pageSize;
69824 ** may only be called if it is guaranteed that the b-tree mutex is already
69828 ** known that the shared b-tree mutex is held, but the mutex on the
69835 assert( sqlite3_mutex_held(p->pBt->mutex) );
69836 n = p->pBt->pageSize - p->pBt->usableSize;
69847 ** The amount of reserve can only grow - never shrink.
69852 n1 = (int)p->pBt->nReserveWanted;
69867 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
69878 ** newFlag==(-1) No changes
69883 ** freelist leaf pages are not written back to the database. Thus in-page
69897 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
69898 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
69900 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
69906 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
69907 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
69908 ** is disabled. The default value for the auto-vacuum property is
69915 BtShared *pBt = p->pBt;
69920 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
69923 pBt->autoVacuum = av ?1:0;
69924 pBt->incrVacuum = av==2 ?1:0;
69932 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
69942 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
69943 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
69952 ** If the user has not set the safety-level for this database connection
69953 ** using "PRAGMA synchronous", and if the safety-level is not already
69962 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
69963 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
69964 if( pDb->bSyncSet==0
69965 && pDb->safety_level!=safety_level
69966 && pDb!=&db->aDb[1]
69968 pDb->safety_level = safety_level;
69969 sqlite3PagerSetFlags(pBt->pPager,
69970 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
69987 ** well-formed database file, then SQLITE_CORRUPT is returned.
69997 assert( sqlite3_mutex_held(pBt->mutex) );
69998 assert( pBt->pPage1==0 );
69999 rc = sqlite3PagerSharedLock(pBt->pPager);
70007 nPage = get4byte(28+(u8*)pPage1->aData);
70008 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
70009 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
70012 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
70018 u8 *page1 = pPage1->aData;
70020 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
70029 pBt->btsFlags |= BTS_READ_ONLY;
70036 pBt->btsFlags |= BTS_READ_ONLY;
70047 ** may not be the latest version - there may be a newer one in the log
70050 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
70052 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
70068 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
70077 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
70078 ** determined by the 2-byte integer located at an offset of 16 bytes from
70081 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
70083 if( ((pageSize-1)&pageSize)!=0
70089 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
70091 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
70095 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
70096 ** determined by the one-byte unsigned integer found at an offset of 20
70098 usableSize = pageSize - page1[20];
70099 if( (u32)pageSize!=pBt->pageSize ){
70101 ** of BtShared.pageSize, we have discovered that the page-size is
70102 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
70104 ** again with the correct page-size.
70107 pBt->usableSize = usableSize;
70108 pBt->pageSize = pageSize;
70110 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
70111 pageSize-usableSize);
70115 if( sqlite3WritableSchema(pBt->db)==0 ){
70122 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
70128 pBt->pageSize = pageSize;
70129 pBt->usableSize = usableSize;
70131 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
70132 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
70138 ** cells can will fit on one page. We assume a 10-byte page header.
70140 ** 2-byte pointer to the cell
70141 ** 4-byte child pointer
70142 ** 9-byte nKey value
70143 ** 4-byte nData value
70144 ** 4-byte overflow page pointer
70145 ** So a cell consists of a 2-byte pointer, a header which is as much as
70149 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
70150 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
70151 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
70152 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
70153 if( pBt->maxLocal>127 ){
70154 pBt->max1bytePayload = 127;
70156 pBt->max1bytePayload = (u8)pBt->maxLocal;
70158 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
70159 pBt->pPage1 = pPage1;
70160 pBt->nPage = nPage;
70165 pBt->pPage1 = 0;
70185 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
70186 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
70187 && pCur->eState!=CURSOR_FAULT ) r++;
70199 ** If there is a transaction in progress, this routine is a no-op.
70202 assert( sqlite3_mutex_held(pBt->mutex) );
70203 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
70204 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
70205 MemPage *pPage1 = pBt->pPage1;
70206 assert( pPage1->aData );
70207 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
70208 pBt->pPage1 = 0;
70223 assert( sqlite3_mutex_held(pBt->mutex) );
70224 if( pBt->nPage>0 ){
70227 pP1 = pBt->pPage1;
70229 data = pP1->aData;
70230 rc = sqlite3PagerWrite(pP1->pDbPage);
70234 data[16] = (u8)((pBt->pageSize>>8)&0xff);
70235 data[17] = (u8)((pBt->pageSize>>16)&0xff);
70238 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
70239 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
70243 memset(&data[24], 0, 100-24);
70245 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
70247 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
70248 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
70249 put4byte(&data[36 + 4*4], pBt->autoVacuum);
70250 put4byte(&data[36 + 7*4], pBt->incrVacuum);
70252 pBt->nPage = 1;
70265 p->pBt->nPage = 0;
70266 rc = newDatabase(p->pBt);
70272 ** Attempt to start a new transaction. A write-transaction
70273 ** is started if the second argument is nonzero, otherwise a read-
70277 ** upgraded to exclusive by calling this routine a second time - the
70280 ** A write-transaction must be started before attempting any
70294 ** if there is one. But if there was previously a read-lock, do not
70295 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
70296 ** returned when there is already a read-lock in order to avoid a deadlock.
70307 BtShared *pBt = p->pBt;
70308 Pager *pPager = pBt->pPager;
70314 /* If the btree is already in a write-transaction, or it
70315 ** is already in a read-transaction and a read-transaction
70316 ** is requested, this is a no-op.
70318 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
70321 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
70323 if( (p->db->flags & SQLITE_ResetDatabase)
70326 pBt->btsFlags &= ~BTS_READ_ONLY;
70329 /* Write transactions are not possible on a read-only database */
70330 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
70339 ** on this shared-btree structure and a second write transaction is
70342 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
70343 || (pBt->btsFlags & BTS_PENDING)!=0
70345 pBlock = pBt->pWriter->db;
70348 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
70349 if( pIter->pBtree!=p ){
70350 pBlock = pIter->pBtree->db;
70356 sqlite3ConnectionBlocked(p->db, pBlock);
70363 /* Any read-only or read-write transaction implies a read-lock on
70364 ** page 1. So if some other shared-cache client already has a write-lock
70369 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
70370 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
70372 sqlite3PagerWalDb(pPager, p->db);
70377 if( pBt->pPage1==0 && wrflag ){
70378 assert( pBt->inTransaction==TRANS_NONE );
70384 /* Call lockBtree() until either pBt->pPage1 is populated or
70386 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
70387 ** reading page 1 it discovers that the page-size of the database
70388 ** file is not pBt->pageSize. In this case lockBtree() will update
70389 ** pBt->pageSize to the page-size of the file on disk.
70391 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
70394 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
70397 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
70400 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
70413 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
70421 if( p->inTrans==TRANS_NONE ){
70422 pBt->nTransaction++;
70424 if( p->sharable ){
70425 assert( p->lock.pBtree==p && p->lock.iTable==1 );
70426 p->lock.eLock = READ_LOCK;
70427 p->lock.pNext = pBt->pLock;
70428 pBt->pLock = &p->lock;
70432 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
70433 if( p->inTrans>pBt->inTransaction ){
70434 pBt->inTransaction = p->inTrans;
70437 MemPage *pPage1 = pBt->pPage1;
70439 assert( !pBt->pWriter );
70440 pBt->pWriter = p;
70441 pBt->btsFlags &= ~BTS_EXCLUSIVE;
70442 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
70445 /* If the db-size header field is incorrect (as it may be if an old
70448 ** re-read the database size from page 1 if a savepoint or transaction
70451 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
70452 rc = sqlite3PagerWrite(pPage1->pDbPage);
70454 put4byte(&pPage1->aData[28], pBt->nPage);
70463 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
70468 ** the sub-journal is not already open, then it will be opened here.
70470 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
70482 ** Set the pointer-map entries for all children of page pPage. Also, if
70490 BtShared *pBt = pPage->pBt;
70491 Pgno pgno = pPage->pgno;
70493 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70494 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
70496 nCell = pPage->nCell;
70503 if( !pPage->leaf ){
70509 if( !pPage->leaf ){
70510 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70522 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
70525 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
70528 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
70532 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
70533 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
70536 if( get4byte(pPage->aData)!=iFrom ){
70539 put4byte(pPage->aData, iTo);
70545 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
70547 nCell = pPage->nCell;
70553 pPage->xParseCell(pPage, pCell, &info);
70555 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
70558 if( iFrom==get4byte(pCell+info.nSize-4) ){
70559 put4byte(pCell+info.nSize-4, iTo);
70573 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
70576 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
70588 ** the journal needs to be sync()ed before database page pDbPage->pgno
70596 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
70601 Pgno iDbPage = pDbPage->pgno;
70602 Pager *pPager = pBt->pPager;
70607 assert( sqlite3_mutex_held(pBt->mutex) );
70608 assert( pDbPage->pBt==pBt );
70614 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
70618 pDbPage->pgno = iFreePage;
70620 /* If pDbPage was a btree-page, then it may have child pages and/or cells
70634 Pgno nextOvfl = get4byte(pDbPage->aData);
70652 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
70670 ** Perform a single step of an incremental-vacuum. If successful, return
70675 ** More specifically, this function attempts to re-organize the database so
70681 ** If the bCommit parameter is non-zero, this function assumes that the
70683 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
70687 Pgno nFreeList; /* Number of pages still on the free-list */
70690 assert( sqlite3_mutex_held(pBt->mutex) );
70697 nFreeList = get4byte(&pBt->pPage1->aData[36]);
70712 /* Remove the page from the files free-list. This is not required
70713 ** if bCommit is non-zero. In that case, the free-list will be
70741 ** looping until a free-page located within the first nFin pages
70774 iLastPg--;
70776 pBt->bDoTruncate = 1;
70777 pBt->nPage = iLastPg;
70783 ** The database opened by the first argument is an auto-vacuum database
70785 ** size of the database in pages following an auto-vacuum operation.
70792 nEntry = pBt->usableSize/5;
70793 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
70794 nFin = nOrig - nFree - nPtrmap;
70796 nFin--;
70799 nFin--;
70806 ** A write-transaction must be opened before calling this function.
70815 BtShared *pBt = p->pBt;
70818 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
70819 if( !pBt->autoVacuum ){
70823 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
70835 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70836 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
70848 ** is committed for an auto-vacuum database.
70858 pBt = p->pBt;
70859 pPager = pBt->pPager;
70862 assert( sqlite3_mutex_held(pBt->mutex) );
70864 assert(pBt->autoVacuum);
70865 if( !pBt->incrVacuum ){
70875 ** is either a pointer-map page or the pending-byte page. If one
70881 nFree = get4byte(&pBt->pPage1->aData[36]);
70882 db = p->db;
70883 if( db->xAutovacPages ){
70885 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
70886 if( db->aDb[iDb].pBt==p ) break;
70888 nVac = db->xAutovacPages(
70889 db->pAutovacPagesArg,
70890 db->aDb[iDb].zDbSName,
70893 pBt->pageSize
70909 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
70913 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
70915 put4byte(&pBt->pPage1->aData[32], 0);
70916 put4byte(&pBt->pPage1->aData[36], 0);
70918 put4byte(&pBt->pPage1->aData[28], nFin);
70919 pBt->bDoTruncate = 1;
70920 pBt->nPage = nFin;
70936 ** This routine does the first phase of a two-phase commit. This routine
70948 ** This call is a no-op if no write-transaction is currently active on pBt.
70951 ** the name of a super-journal file that should be written into the
70952 ** individual journal file, or is NULL, indicating no super-journal file
70955 ** When this is called, the super-journal should already have been
70959 ** the write-transaction for this database file is to delete the journal.
70963 if( p->inTrans==TRANS_WRITE ){
70964 BtShared *pBt = p->pBt;
70967 if( pBt->autoVacuum ){
70974 if( pBt->bDoTruncate ){
70975 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
70978 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
70989 BtShared *pBt = p->pBt;
70990 sqlite3 *db = p->db;
70994 pBt->bDoTruncate = 0;
70996 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
70998 ** handle, downgrade to a read-only transaction. The other statements
71001 p->inTrans = TRANS_READ;
71007 if( p->inTrans!=TRANS_NONE ){
71009 pBt->nTransaction--;
71010 if( 0==pBt->nTransaction ){
71011 pBt->inTransaction = TRANS_NONE;
71017 p->inTrans = TRANS_NONE;
71027 ** This routine implements the second phase of a 2-phase commit. The
71039 ** is non-zero then this b-tree transaction is part of a multi-file
71041 ** (by deleting a super-journal file) and the caller will ignore this
71043 ** reset the b-tree objects internal state to indicate that the write
71052 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
71056 /* If the handle has a write-transaction open, commit the shared-btrees
71059 if( p->inTrans==TRANS_WRITE ){
71061 BtShared *pBt = p->pBt;
71062 assert( pBt->inTransaction==TRANS_WRITE );
71063 assert( pBt->nTransaction>0 );
71064 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
71069 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
71070 pBt->inTransaction = TRANS_READ;
71104 ** flag is true, then only write-cursors need be tripped - read-only
71108 ** rolled back modified the database schema. In this case b-tree root
71113 ** saving the current position of a read-only cursor, all cursors,
71114 ** including all read-cursors are tripped.
71126 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71127 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
71128 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
71137 p->eState = CURSOR_FAULT;
71138 p->skipNext = errCode;
71148 ** Set the pBt->nPage field correctly, according to the current
71149 ** state of the database. Assume pBt->pPage1 is valid.
71152 int nPage = get4byte(&pPage1->aData[28]);
71154 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
71155 testcase( pBt->nPage!=(u32)nPage );
71156 pBt->nPage = nPage;
71172 BtShared *pBt = p->pBt;
71191 if( p->inTrans==TRANS_WRITE ){
71194 assert( TRANS_WRITE==pBt->inTransaction );
71195 rc2 = sqlite3PagerRollback(pBt->pPager);
71200 /* The rollback may have destroyed the pPage1->aData value. So
71202 ** sure pPage1->aData is set correctly. */
71208 pBt->inTransaction = TRANS_READ;
71228 ** A statement sub-transaction is implemented as an anonymous savepoint. The
71230 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
71231 ** are no active savepoints and no other statement-transactions open,
71237 BtShared *pBt = p->pBt;
71239 assert( p->inTrans==TRANS_WRITE );
71240 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
71242 assert( iStatement>p->db->nSavepoint );
71243 assert( pBt->inTransaction==TRANS_WRITE );
71249 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
71261 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
71268 if( p && p->inTrans==TRANS_WRITE ){
71269 BtShared *pBt = p->pBt;
71271 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
71277 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
71280 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
71281 pBt->nPage = 0;
71284 btreeSetNPage(pBt, pBt->pPage1);
71286 /* pBt->nPage might be zero if the database was corrupt when
71288 assert( CORRUPT_DB || pBt->nPage>0 );
71297 ** iTable. If a read-only cursor is requested, it is assumed that
71298 ** the caller already has at least a read-only transaction open
71299 ** on the database already. If a write-cursor is requested, then
71316 ** 3: The database must be writable (not on read-only media)
71327 ** operations on this cursor can be no-ops and all READ operations can
71328 ** return a null row (2-bytes: 0x01 0x00).
71331 ** root page of a b-tree. If it is not, then the cursor acquired
71340 int wrFlag, /* 1 to write. 0 read-only */
71344 BtShared *pBt = p->pBt; /* Shared b-tree handle */
71354 ** b-tree database, the connection is holding the required table locks,
71362 assert( p->inTrans>TRANS_NONE );
71363 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
71364 assert( pBt->pPage1 && pBt->pPage1->aData );
71365 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
71378 pCur->pgnoRoot = iTable;
71379 pCur->iPage = -1;
71380 pCur->pKeyInfo = pKeyInfo;
71381 pCur->pBtree = p;
71382 pCur->pBt = pBt;
71383 pCur->curFlags = 0;
71386 for(pX=pBt->pCursor; pX; pX=pX->pNext){
71387 if( pX->pgnoRoot==iTable ){
71388 pX->curFlags |= BTCF_Multiple;
71389 pCur->curFlags = BTCF_Multiple;
71392 pCur->eState = CURSOR_INVALID;
71393 pCur->pNext = pBt->pCursor;
71394 pBt->pCursor = pCur;
71396 pCur->curFlags |= BTCF_WriteFlag;
71397 pCur->curPagerFlags = 0;
71398 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
71400 pCur->curPagerFlags = PAGER_GET_READONLY;
71407 int wrFlag, /* 1 to write. 0 read-only */
71420 int wrFlag, /* 1 to write. 0 read-only */
71424 if( p->sharable ){
71436 ** to users so they cannot do the sizeof() themselves - they must call
71449 ** of run-time by skipping the initialization of those elements.
71460 Btree *pBtree = pCur->pBtree;
71462 BtShared *pBt = pCur->pBt;
71464 assert( pBt->pCursor!=0 );
71465 if( pBt->pCursor==pCur ){
71466 pBt->pCursor = pCur->pNext;
71468 BtCursor *pPrev = pBt->pCursor;
71470 if( pPrev->pNext==pCur ){
71471 pPrev->pNext = pCur->pNext;
71474 pPrev = pPrev->pNext;
71479 sqlite3_free(pCur->aOverflow);
71480 sqlite3_free(pCur->pKey);
71481 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
71484 assert( pBtree->sharable==0 );
71489 pCur->pBtree = 0;
71504 if( a->nKey!=b->nKey ) return 0;
71505 if( a->pPayload!=b->pPayload ) return 0;
71506 if( a->nPayload!=b->nPayload ) return 0;
71507 if( a->nLocal!=b->nLocal ) return 0;
71508 if( a->nSize!=b->nSize ) return 0;
71514 btreeParseCell(pCur->pPage, pCur->ix, &info);
71515 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
71521 if( pCur->info.nSize==0 ){
71522 pCur->curFlags |= BTCF_ValidNKey;
71523 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
71532 ** that is currently pointing to a row in a (non-empty) table.
71536 return pCur && pCur->eState==CURSOR_VALID;
71541 return pCur->eState==CURSOR_VALID;
71552 assert( pCur->eState==CURSOR_VALID );
71553 assert( pCur->curIntKey );
71555 return pCur->info.nKey;
71562 assert( (pCur->curFlags & BTCF_Pinned)==0 );
71563 pCur->curFlags |= BTCF_Pinned;
71566 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
71567 pCur->curFlags &= ~BTCF_Pinned;
71577 assert( pCur->eState==CURSOR_VALID );
71579 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
71580 (i64)(pCur->info.pPayload - pCur->pPage->aData);
71589 ** The caller must guarantee that the cursor is pointing to a non-NULL
71595 assert( pCur->eState==CURSOR_VALID );
71597 return pCur->info.nPayload;
71615 assert( pCur->eState==CURSOR_VALID );
71616 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
71622 ** linked list of overflow pages. If possible, it uses the auto-vacuum
71623 ** pointer-map data instead of reading the content of page ovfl to do so.
71635 ** the pointer-map was used to obtain the value for *pPgnoNext), then
71648 assert( sqlite3_mutex_held(pBt->mutex) );
71653 ** autovacuum pointer-map pages. Guess that the next page in
71658 if( pBt->autoVacuum ){
71682 next = get4byte(pPage->aData);
71710 int eOp, /* 0 -> copy from page, 1 -> copy to page */
71743 ** the overflow page-list cache array (BtCursor.aOverflow).
71747 ** Once an overflow page-list cache has been allocated, it must be
71749 ** the cursor is moved to a different row. Additionally, in auto-vacuum
71750 ** mode, the following events may invalidate an overflow page-list cache.
71761 int eOp /* zero to read. non-zero to write. */
71766 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
71767 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
71774 assert( pCur->eState==CURSOR_VALID );
71775 if( pCur->ix>=pPage->nCell ){
71781 aPayload = pCur->info.pPayload;
71782 assert( offset+amt <= pCur->info.nPayload );
71784 assert( aPayload > pPage->aData );
71785 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
71788 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
71795 if( offset<pCur->info.nLocal ){
71797 if( a+offset>pCur->info.nLocal ){
71798 a = pCur->info.nLocal - offset;
71800 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
71803 amt -= a;
71805 offset -= pCur->info.nLocal;
71810 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
71813 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
71822 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
71823 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
71824 if( pCur->aOverflow==0
71825 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
71828 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
71833 pCur->aOverflow = aNew;
71836 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
71837 pCur->curFlags |= BTCF_ValidOvfl;
71839 /* If the overflow page-list cache has been allocated and the
71843 if( pCur->aOverflow[offset/ovflSize] ){
71845 nextPage = pCur->aOverflow[iIdx];
71852 /* If required, populate the overflow page-list cache. */
71853 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
71854 assert( pCur->aOverflow[iIdx]==0
71855 || pCur->aOverflow[iIdx]==nextPage
71857 pCur->aOverflow[iIdx] = nextPage;
71863 ** page-list cache, if any, then fall back to the getOverflowPage()
71866 assert( pCur->curFlags & BTCF_ValidOvfl );
71867 assert( pCur->pBtree->db==pBt->db );
71868 if( pCur->aOverflow[iIdx+1] ){
71869 nextPage = pCur->aOverflow[iIdx+1];
71873 offset -= ovflSize;
71880 a = ovflSize - offset;
71888 ** 3) there are no dirty pages in the page-cache
71889 ** 4) the database is file-backed, and
71894 ** output buffer, bypassing the page-cache altogether. This speeds
71899 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
71900 && &pBuf[-4]>=pBufStart /* (6) */
71902 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
71904 u8 *aWrite = &pBuf[-4];
71907 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
71908 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
71916 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
71927 amt -= a;
71948 ** pCur can be pointing to either a table or an index b-tree.
71950 ** pCur is pointing to an index b-tree then the key section is read.
71962 assert( pCur->eState==CURSOR_VALID );
71963 assert( pCur->iPage>=0 && pCur->pPage );
71980 if ( pCur->eState==CURSOR_INVALID ){
71988 if( pCur->eState==CURSOR_VALID ){
72000 ** the key if index btrees (pPage->intKey==0) and is the data for
72001 ** table btrees (pPage->intKey==1). The number of bytes of available
72021 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
72022 assert( pCur->eState==CURSOR_VALID );
72023 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72025 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
72026 assert( pCur->info.nSize>0 );
72027 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
72028 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
72029 amt = pCur->info.nLocal;
72030 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
72034 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
72037 return (void*)pCur->info.pPayload;
72044 ** b-tree page. Write the number of available bytes into *pAmt.
72064 ** This function returns SQLITE_CORRUPT if the page-header flags field of
72066 ** if an intkey page appears to be the parent of a non-intkey page, or
72067 ** vice-versa).
72070 BtShared *pBt = pCur->pBt;
72073 assert( pCur->eState==CURSOR_VALID );
72074 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
72075 assert( pCur->iPage>=0 );
72076 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
72079 pCur->info.nSize = 0;
72080 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72081 pCur->aiIdx[pCur->iPage] = pCur->ix;
72082 pCur->apPage[pCur->iPage] = pCur->pPage;
72083 pCur->ix = 0;
72084 pCur->iPage++;
72085 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
72090 ** Page pParent is an internal (non-leaf) tree page. This function
72091 ** asserts that page number iChild is the left-child if the iIdx'th
72093 ** cells in pParent, that page number iChild is the right-child of
72099 assert( iIdx<=pParent->nCell );
72100 if( iIdx==pParent->nCell ){
72101 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
72113 ** pCur->idx is set to the cell index that contains the pointer
72115 ** right-most child page then pCur->idx is set to one more than
72121 assert( pCur->eState==CURSOR_VALID );
72122 assert( pCur->iPage>0 );
72123 assert( pCur->pPage );
72125 pCur->apPage[pCur->iPage-1],
72126 pCur->aiIdx[pCur->iPage-1],
72127 pCur->pPage->pgno
72129 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
72130 pCur->info.nSize = 0;
72131 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72132 pCur->ix = pCur->aiIdx[pCur->iPage-1];
72133 pLeaf = pCur->pPage;
72134 pCur->pPage = pCur->apPage[--pCur->iPage];
72139 ** Move the cursor to point to the root page of its b-tree structure.
72146 ** If the b-tree structure is empty, the cursor state is set to
72152 ** page-header flags indicate that the [virtual] root-page is the expected
72153 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
72155 ** indicating a table b-tree, or if the caller did specify a KeyInfo
72157 ** b-tree).
72167 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
72168 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
72170 if( pCur->iPage>=0 ){
72171 if( pCur->iPage ){
72172 releasePageNotNull(pCur->pPage);
72173 while( --pCur->iPage ){
72174 releasePageNotNull(pCur->apPage[pCur->iPage]);
72176 pRoot = pCur->pPage = pCur->apPage[0];
72179 }else if( pCur->pgnoRoot==0 ){
72180 pCur->eState = CURSOR_INVALID;
72183 assert( pCur->iPage==(-1) );
72184 if( pCur->eState>=CURSOR_REQUIRESEEK ){
72185 if( pCur->eState==CURSOR_FAULT ){
72186 assert( pCur->skipNext!=SQLITE_OK );
72187 return pCur->skipNext;
72191 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
72192 0, pCur->curPagerFlags);
72194 pCur->eState = CURSOR_INVALID;
72197 pCur->iPage = 0;
72198 pCur->curIntKey = pCur->pPage->intKey;
72200 pRoot = pCur->pPage;
72201 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
72203 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
72204 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
72205 ** NULL, the caller expects a table b-tree. If this is not the case,
72210 ** if pCur->iPage>=0). But this is not so if the database is corrupted
72211 ** in such a way that page pRoot is linked into a second b-tree table
72213 assert( pRoot->intKey==1 || pRoot->intKey==0 );
72214 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
72215 return SQLITE_CORRUPT_PAGE(pCur->pPage);
72219 pCur->ix = 0;
72220 pCur->info.nSize = 0;
72221 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
72223 if( pRoot->nCell>0 ){
72224 pCur->eState = CURSOR_VALID;
72225 }else if( !pRoot->leaf ){
72227 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
72228 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
72229 pCur->eState = CURSOR_VALID;
72232 pCur->eState = CURSOR_INVALID;
72239 ** Move the cursor down to the left-most leaf entry beneath the
72242 ** The left-most leaf is the one with the smallest key - the first
72251 assert( pCur->eState==CURSOR_VALID );
72252 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
72253 assert( pCur->ix<pPage->nCell );
72254 pgno = get4byte(findCell(pPage, pCur->ix));
72261 ** Move the cursor down to the right-most leaf entry beneath the
72264 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
72265 ** finds the right-most entry beneath the *page*.
72267 ** The right-most entry is the one with the largest key - the last
72276 assert( pCur->eState==CURSOR_VALID );
72277 while( !(pPage = pCur->pPage)->leaf ){
72278 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72279 pCur->ix = pPage->nCell;
72283 pCur->ix = pPage->nCell-1;
72284 assert( pCur->info.nSize==0 );
72285 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
72297 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72300 assert( pCur->pPage->nCell>0 );
72304 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72319 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72321 /* If the cursor already points to the last entry, this is a no-op. */
72322 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
72325 ** to the last entry in the b-tree. */
72327 for(ii=0; ii<pCur->iPage; ii++){
72328 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
72330 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
72331 testcase( pCur->ix!=pCur->pPage->nCell-1 );
72332 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
72333 assert( pCur->pPage->leaf );
72341 assert( pCur->eState==CURSOR_VALID );
72345 pCur->curFlags |= BTCF_AtLast;
72347 pCur->curFlags &= ~BTCF_AtLast;
72350 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72389 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72391 assert( pCur->pKeyInfo==0 );
72392 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
72396 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
72397 if( pCur->info.nKey==intKey ){
72401 if( pCur->info.nKey<intKey ){
72402 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
72403 *pRes = -1;
72410 if( pCur->info.nKey+1==intKey ){
72415 if( pCur->info.nKey==intKey ){
72426 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72432 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72433 *pRes = -1;
72438 assert( pCur->pPage );
72439 assert( pCur->pPage->isInit );
72440 assert( pCur->eState==CURSOR_VALID );
72441 assert( pCur->pPage->nCell > 0 );
72442 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
72443 assert( pCur->curIntKey );
72448 MemPage *pPage = pCur->pPage;
72451 /* pPage->nCell must be greater than zero. If this is the root-page
72453 ** not run. If this is not the root-page, then the moveToChild() routine
72455 ** be the right kind (index or table) of b-tree page. Otherwise
72457 assert( pPage->nCell>0 );
72458 assert( pPage->intKey );
72460 upr = pPage->nCell-1;
72462 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
72466 if( pPage->intKeyLeaf ){
72468 if( pCell>=pPage->aDataEnd ){
72476 if( lwr>upr ){ c = -1; break; }
72478 upr = idx-1;
72482 pCur->ix = (u16)idx;
72483 if( !pPage->leaf ){
72487 pCur->curFlags |= BTCF_ValidNKey;
72488 pCur->info.nKey = nCellKey;
72489 pCur->info.nSize = 0;
72497 assert( lwr==upr+1 || !pPage->leaf );
72498 assert( pPage->isInit );
72499 if( pPage->leaf ){
72500 assert( pCur->ix<pCur->pPage->nCell );
72501 pCur->ix = (u16)idx;
72507 if( lwr>=pPage->nCell ){
72508 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72512 pCur->ix = (u16)lwr;
72517 pCur->info.nSize = 0;
72518 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72523 ** Compare the "idx"-th cell on the page the cursor pCur is currently
72544 MemPage *pPage = pCur->pPage;
72550 if( nCell<=pPage->max1bytePayload ){
72551 /* This branch runs if the record-size field of the cell is a
72553 ** b-tree page. */
72554 testcase( pCell+nCell+1==pPage->aDataEnd );
72557 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72559 /* The record-size field is a 2 byte varint and the record
72560 ** fits entirely on the main b-tree page. */
72561 testcase( pCell+nCell+2==pPage->aDataEnd );
72572 ** Return true (non-zero) if pCur is current pointing to the last
72577 assert( pCur->eState==CURSOR_VALID );
72578 for(i=0; i<pCur->iPage; i++){
72579 MemPage *pPage = pCur->apPage[i];
72580 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
72608 ** The pIdxKey->eqSeen field is set to 1 if there
72620 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72622 assert( pCur->pKeyInfo!=0 );
72625 pCur->pBtree->nSeek++; /* Performance measurement during testing */
72629 pIdxKey->errCode = 0;
72630 assert( pIdxKey->default_rc==1
72631 || pIdxKey->default_rc==0
72632 || pIdxKey->default_rc==-1
72647 if( pCur->eState==CURSOR_VALID
72648 && pCur->pPage->leaf
72652 if( pCur->ix==pCur->pPage->nCell-1
72653 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
72654 && pIdxKey->errCode==SQLITE_OK
72659 if( pCur->iPage>0
72661 && pIdxKey->errCode==SQLITE_OK
72663 pCur->curFlags &= ~BTCF_ValidOvfl;
72664 if( !pCur->pPage->isInit ){
72669 pIdxKey->errCode = SQLITE_OK;
72675 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
72676 *pRes = -1;
72683 assert( pCur->pPage );
72684 assert( pCur->pPage->isInit );
72685 assert( pCur->eState==CURSOR_VALID );
72686 assert( pCur->pPage->nCell > 0 );
72687 assert( pCur->curIntKey==0 );
72692 MemPage *pPage = pCur->pPage;
72695 /* pPage->nCell must be greater than zero. If this is the root-page
72697 ** not run. If this is not the root-page, then the moveToChild() routine
72699 ** be the right kind (index or table) of b-tree page. Otherwise
72701 assert( pPage->nCell>0 );
72702 assert( pPage->intKey==0 );
72704 upr = pPage->nCell-1;
72710 /* The maximum supported page-size is 65536 bytes. This means that
72711 ** the maximum number of record bytes stored on an index B-Tree
72712 ** page is less than 16384 bytes and may be stored as a 2-byte
72715 ** stored entirely within the b-tree page by inspecting the first
72719 if( nCell<=pPage->max1bytePayload ){
72720 /* This branch runs if the record-size field of the cell is a
72722 ** b-tree page. */
72723 testcase( pCell+nCell+1==pPage->aDataEnd );
72726 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
72728 /* The record-size field is a 2 byte varint and the record
72729 ** fits entirely on the main b-tree page. */
72730 testcase( pCell+nCell+2==pPage->aDataEnd );
72743 u8 * const pCellBody = pCell - pPage->childPtrSize;
72745 pPage->xParseCell(pPage, pCellBody, &pCur->info);
72746 nCell = (int)pCur->info.nKey;
72751 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
72760 pCur->ix = (u16)idx;
72763 pCur->curFlags &= ~BTCF_ValidOvfl;
72772 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
72773 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
72778 upr = idx-1;
72783 pCur->ix = (u16)idx;
72784 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
72791 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
72792 assert( pPage->isInit );
72793 if( pPage->leaf ){
72794 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
72795 pCur->ix = (u16)idx;
72800 if( lwr>=pPage->nCell ){
72801 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72805 pCur->ix = (u16)lwr;
72810 pCur->info.nSize = 0;
72811 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72828 return (CURSOR_VALID!=pCur->eState);
72841 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
72846 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
72847 if( NEVER(pCur->pPage->leaf==0) ) return -1;
72849 n = pCur->pPage->nCell;
72850 for(i=0; i<pCur->iPage; i++){
72851 n *= pCur->apPage[i]->nCell;
72882 if( pCur->eState!=CURSOR_VALID ){
72883 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
72888 if( CURSOR_INVALID==pCur->eState ){
72891 if( pCur->eState==CURSOR_SKIPNEXT ){
72892 pCur->eState = CURSOR_VALID;
72893 if( pCur->skipNext>0 ) return SQLITE_OK;
72897 pPage = pCur->pPage;
72898 idx = ++pCur->ix;
72899 if( !pPage->isInit || sqlite3FaultSim(412) ){
72903 ** in a corrupt database file where the table being DELETE-ed from
72905 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
72910 if( idx>=pPage->nCell ){
72911 if( !pPage->leaf ){
72912 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
72917 if( pCur->iPage==0 ){
72918 pCur->eState = CURSOR_INVALID;
72922 pPage = pCur->pPage;
72923 }while( pCur->ix>=pPage->nCell );
72924 if( pPage->intKey ){
72930 if( pPage->leaf ){
72941 pCur->info.nSize = 0;
72942 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
72943 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
72944 pPage = pCur->pPage;
72945 if( (++pCur->ix)>=pPage->nCell ){
72946 pCur->ix--;
72949 if( pPage->leaf ){
72981 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
72982 assert( pCur->info.nSize==0 );
72983 if( pCur->eState!=CURSOR_VALID ){
72988 if( CURSOR_INVALID==pCur->eState ){
72991 if( CURSOR_SKIPNEXT==pCur->eState ){
72992 pCur->eState = CURSOR_VALID;
72993 if( pCur->skipNext<0 ) return SQLITE_OK;
72997 pPage = pCur->pPage;
72998 assert( pPage->isInit );
72999 if( !pPage->leaf ){
73000 int idx = pCur->ix;
73005 while( pCur->ix==0 ){
73006 if( pCur->iPage==0 ){
73007 pCur->eState = CURSOR_INVALID;
73012 assert( pCur->info.nSize==0 );
73013 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
73015 pCur->ix--;
73016 pPage = pCur->pPage;
73017 if( pPage->intKey && !pPage->leaf ){
73029 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
73030 pCur->info.nSize = 0;
73031 if( pCur->eState!=CURSOR_VALID
73032 || pCur->ix==0
73033 || pCur->pPage->leaf==0
73037 pCur->ix--;
73058 ** anywhere on the free-list, then it is guaranteed to be returned. If
73078 assert( sqlite3_mutex_held(pBt->mutex) );
73079 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
73080 pPage1 = pBt->pPage1;
73082 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
73084 n = get4byte(&pPage1->aData[36]);
73085 testcase( n==mxPage-1 );
73092 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
73095 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
73096 ** shows that the page 'nearby' is somewhere on the free-list, then
73097 ** the entire-list will be searched for that page.
73104 assert( pBt->autoVacuum );
73116 /* Decrement the free-list count by 1. Set iTrunk to the index of the
73117 ** first free-list trunk page. iPrevTrunk is initially 1.
73119 rc = sqlite3PagerWrite(pPage1->pDbPage);
73121 put4byte(&pPage1->aData[36], n-1);
73124 ** is not true. Otherwise, it runs once for each trunk-page on the
73125 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
73131 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
73134 iTrunk = get4byte(&pPrevTrunk->aData[0]);
73136 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
73139 iTrunk = get4byte(&pPage1->aData[32]);
73143 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
73152 assert( pTrunk->aData!=0 );
73153 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
73155 k = get4byte(&pTrunk->aData[4]);
73161 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73166 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73169 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73170 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
73184 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73190 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
73192 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73196 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
73200 ** pointers to free-list leaves. The first leaf becomes a trunk
73204 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
73214 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
73219 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
73220 put4byte(&pNewTrunk->aData[4], k-1);
73221 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
73224 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
73225 put4byte(&pPage1->aData[32], iNewTrunk);
73227 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
73231 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
73235 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
73241 unsigned char *aData = pTrunk->aData;
73255 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
73257 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
73282 *pPgno, closest+1, k, pTrunk->pgno, n-1));
73283 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73285 if( closest<k-1 ){
73288 put4byte(&aData[4], k-1);
73292 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73309 ** pager layer with the 'no-content' flag set. This prevents the pager
73311 ** current transaction has already run one or more incremental-vacuum
73314 ** not set the no-content flag. This causes the pager to load and journal
73319 ** file on disk. So the effects of disabling the no-content optimization
73323 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
73325 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
73327 pBt->nPage++;
73328 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
73331 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
73332 /* If *pPgno refers to a pointer-map page, allocate two new pages
73334 ** becomes a new pointer-map page, the second is used by the caller.
73337 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
73338 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
73339 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
73341 rc = sqlite3PagerWrite(pPg->pDbPage);
73345 pBt->nPage++;
73346 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
73349 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
73350 *pPgno = pBt->nPage;
73355 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
73368 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
73369 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
73374 ** This function is used to add page iPage to the database file free-list.
73375 ** It is assumed that the page is not already a part of the free-list.
73386 MemPage *pTrunk = 0; /* Free-list trunk page */
73387 Pgno iTrunk = 0; /* Page number of free-list trunk page */
73388 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
73391 u32 nFree; /* Initial number of pages on free-list */
73393 assert( sqlite3_mutex_held(pBt->mutex) );
73395 assert( !pMemPage || pMemPage->pgno==iPage );
73397 if( iPage<2 || iPage>pBt->nPage ){
73402 sqlite3PagerRef(pPage->pDbPage);
73408 rc = sqlite3PagerWrite(pPage1->pDbPage);
73410 nFree = get4byte(&pPage1->aData[36]);
73411 put4byte(&pPage1->aData[36], nFree+1);
73413 if( pBt->btsFlags & BTS_SECURE_DELETE ){
73418 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
73422 memset(pPage->aData, 0, pPage->pBt->pageSize);
73425 /* If the database supports auto-vacuum, write an entry in the pointer-map
73433 /* Now manipulate the actual database free-list structure. There are two
73434 ** possibilities. If the free-list is currently empty, or if the first
73435 ** trunk page in the free-list is full, then this page will become a
73436 ** new free-list trunk page. Otherwise, it will become a leaf of the
73437 ** first trunk page in the current free-list. This block tests if it
73438 ** is possible to add the page as a new free-list leaf.
73443 iTrunk = get4byte(&pPage1->aData[32]);
73453 nLeaf = get4byte(&pTrunk->aData[4]);
73454 assert( pBt->usableSize>32 );
73455 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
73459 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
73464 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
73467 ** usableSize/4 - 8 entries will be reported as corrupt. In order
73469 ** we will continue to restrict the number of entries to usableSize/4 - 8
73472 ** to read "usableSize/4-2" instead of "usableSize/4-8".
73474 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
73479 rc = sqlite3PagerWrite(pTrunk->pDbPage);
73481 put4byte(&pTrunk->aData[4], nLeaf+1);
73482 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
73483 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
73484 sqlite3PagerDontWrite(pPage->pDbPage);
73488 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
73494 ** the page being freed as a leaf page of the first trunk in the free-list.
73495 ** Possibly because the free-list is empty, or possibly because the
73496 ** first trunk in the free-list is full. Either way, the page being freed
73497 ** will become the new first trunk page in the free-list.
73502 rc = sqlite3PagerWrite(pPage->pDbPage);
73506 put4byte(pPage->aData, iTrunk);
73507 put4byte(&pPage->aData[4], 0);
73508 put4byte(&pPage1->aData[32], iPage);
73509 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
73513 pPage->isInit = 0;
73521 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
73539 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73540 assert( pInfo->nLocal!=pInfo->nPayload );
73541 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
73542 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
73543 if( pCell + pInfo->nSize > pPage->aDataEnd ){
73547 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
73548 pBt = pPage->pBt;
73549 assert( pBt->usableSize > 4 );
73550 ovflPageSize = pBt->usableSize - 4;
73551 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
73553 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
73555 while( nOvfl-- ){
73570 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
73577 ** freePage2() may zero the page contents if secure-delete mode is
73588 sqlite3PagerUnref(pOvfl->pDbPage);
73603 pPage->xParseCell(pPage, pCell, &sInfo); \
73618 ** Note that pCell does not necessary need to point to the pPage->aData
73620 ** be constructed in this temporary area then copied into pPage->aData
73640 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73644 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
73645 || sqlite3PagerIswriteable(pPage->pDbPage) );
73648 nHeader = pPage->childPtrSize;
73649 if( pPage->intKey ){
73650 nPayload = pX->nData + pX->nZero;
73651 pSrc = pX->pData;
73652 nSrc = pX->nData;
73653 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
73655 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
73657 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
73658 nSrc = nPayload = (int)pX->nKey;
73659 pSrc = pX->pKey;
73665 if( nPayload<=pPage->maxLocal ){
73676 memset(pPayload+nSrc, 0, nPayload-nSrc);
73683 mn = pPage->minLocal;
73684 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
73685 testcase( n==pPage->maxLocal );
73686 testcase( n==pPage->maxLocal+1 );
73687 if( n > pPage->maxLocal ) n = mn;
73693 pBt = pPage->pBt;
73710 pPage->xParseCell(pPage, pCell, &info);
73711 assert( nHeader==(int)(info.pPayload - pCell) );
73712 assert( info.nKey==pX->nKey );
73725 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
73729 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
73730 || sqlite3PagerIswriteable(pPage->pDbPage) );
73740 nPayload -= n;
73744 nSrc -= n;
73745 spaceLeft -= n;
73749 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
73750 if( pBt->autoVacuum ){
73760 /* If the database supports auto-vacuum, and the second or subsequent
73761 ** overflow page is being allocated, add an entry to the pointer-map
73765 ** to the pointer-map. If we write nothing to this pointer-map slot,
73770 if( pBt->autoVacuum && rc==SQLITE_OK ){
73785 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
73789 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
73790 || sqlite3PagerIswriteable(pPage->pDbPage) );
73795 pPrior = pOvfl->aData;
73797 pPayload = &pOvfl->aData[4];
73798 spaceLeft = pBt->usableSize - 4;
73806 ** Remove the i-th cell from pPage. This routine effects pPage only.
73815 u8 *data; /* pPage->aData */
73822 assert( idx<pPage->nCell );
73824 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73825 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73826 assert( pPage->nFree>=0 );
73827 data = pPage->aData;
73828 ptr = &pPage->aCellIdx[2*idx];
73829 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
73831 hdr = pPage->hdrOffset;
73833 if( pc<hdr+pPage->nCell*2 ){
73839 testcase( pc+sz==pPage->pBt->usableSize );
73840 if( pc+sz > pPage->pBt->usableSize ){
73849 pPage->nCell--;
73850 if( pPage->nCell==0 ){
73853 put2byte(&data[hdr+5], pPage->pBt->usableSize);
73854 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
73855 - pPage->childPtrSize - 8;
73857 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
73858 put2byte(&data[hdr+3], pPage->nCell);
73859 pPage->nFree += 2;
73870 ** in pPage->apOvfl[] and make it point to the cell content (either
73872 ** Allocating a new entry in pPage->aCell[] implies that
73873 ** pPage->nOverflow is incremented.
73879 int i, /* New cell becomes the i-th cell of the page */
73883 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
73889 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
73892 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
73893 assert( MX_CELL(pPage->pBt)<=10921 );
73894 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
73895 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
73896 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
73897 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73898 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
73899 assert( pPage->nFree>=0 );
73900 if( pPage->nOverflow || sz+2>pPage->nFree ){
73908 j = pPage->nOverflow++;
73909 /* Comparison against ArraySize-1 since we hold back one extra slot
73912 assert( j < ArraySize(pPage->apOvfl)-1 );
73913 pPage->apOvfl[j] = pCell;
73914 pPage->aiOvfl[j] = (u16)i;
73921 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
73922 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
73924 int rc = sqlite3PagerWrite(pPage->pDbPage);
73929 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73930 data = pPage->aData;
73931 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
73937 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
73938 assert( idx+sz <= (int)pPage->pBt->usableSize );
73939 pPage->nFree -= (u16)(2 + sz);
73946 memcpy(&data[idx+4], pCell+4, sz-4);
73951 pIns = pPage->aCellIdx + i*2;
73952 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
73954 pPage->nCell++;
73956 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
73957 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
73959 if( pPage->pBt->autoVacuum ){
73982 ** tunable - as if you could change them and recompile and it would all work.
73997 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
74009 ** -----------
74011 ** -----------
74014 ** --------- --------- ---------
74015 ** |Child-1| |Child-2| |Child-3|
74016 ** --------- --------- ---------
74020 ** 1. All cells from Child-1 in order
74022 ** 3. All cells from Child-2 in order
74024 ** 5. All cells from Child-3 in order
74026 ** For a table-btree (with rowids) the items 2 and 4 are empty because
74030 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
74034 ** ixNx[0] = Number of cells in Child-1.
74035 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
74036 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
74037 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
74040 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
74043 ** ixNx[0] = Number of cells in Child-1.
74044 ** ixNx[1] = Number of cells in Child-1 and Child-2.
74063 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
74067 assert( idx>=0 && idx+N<=p->nCell );
74069 assert( p->apCell[idx]!=0 );
74070 if( p->szCell[idx]==0 ){
74071 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
74074 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
74077 N--;
74085 assert( N>=0 && N<p->nCell );
74086 assert( p->szCell[N]==0 );
74087 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
74088 return p->szCell[N];
74091 assert( N>=0 && N<p->nCell );
74092 if( p->szCell[N] ) return p->szCell[N];
74097 ** Array apCell[] contains pointers to nCell b-tree page cells. The
74115 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
74116 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
74117 const int usableSize = pPg->pBt->usableSize;
74122 u8 *pCellptr = pPg->aCellIdx;
74123 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74125 int k; /* Current slot in pCArray->apEnd[] */
74126 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
74131 memcpy(&pTmp[j], &aData[j], usableSize - j);
74133 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
74134 pSrcEnd = pCArray->apEnd[k];
74138 u8 *pCell = pCArray->apCell[i];
74139 u16 sz = pCArray->szCell[i];
74143 pCell = &pTmp[pCell - aData];
74150 pData -= sz;
74151 put2byte(pCellptr, (pData - aData));
74155 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
74158 if( pCArray->ixNx[k]<=i ){
74160 pSrcEnd = pCArray->apEnd[k];
74164 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
74165 pPg->nCell = nCell;
74166 pPg->nOverflow = 0;
74169 put2byte(&aData[hdr+3], pPg->nCell);
74170 put2byte(&aData[hdr+5], pData - aData);
74176 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
74179 ** will fit), non-zero is returned. Otherwise, if the cells are added
74182 ** Argument pCellptr points to the first entry in the cell-pointer array
74184 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
74186 ** that it is safe to overwrite this part of the cell-pointer array.
74194 ** end of the space required by this page for the cell-pointer area (for
74195 ** all cells - not just those inserted by the current call). If the content
74197 ** cells in apCell[], then the cells do not fit and non-zero is returned.
74201 u8 *pBegin, /* End of cell-pointer array */
74202 u8 **ppData, /* IN/OUT: Page content-area pointer */
74203 u8 *pCellptr, /* Pointer to cell-pointer area */
74208 int i = iFirst; /* Loop counter - cell index to insert */
74209 u8 *aData = pPg->aData; /* Complete page */
74212 int k; /* Current slot in pCArray->apEnd[] */
74214 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
74216 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
74217 pEnd = pCArray->apEnd[k];
74221 assert( pCArray->szCell[i]!=0 );
74222 sz = pCArray->szCell[i];
74224 if( (pData - pBegin)<sz ) return 1;
74225 pData -= sz;
74228 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
74231 assert( (pSlot+sz)<=pCArray->apCell[i]
74232 || pSlot>=(pCArray->apCell[i]+sz)
74234 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
74235 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
74241 memmove(pSlot, pCArray->apCell[i], sz);
74242 put2byte(pCellptr, (pSlot - aData));
74246 if( pCArray->ixNx[k]<=i ){
74248 pEnd = pCArray->apEnd[k];
74256 ** The pCArray object contains pointers to b-tree cells and their sizes.
74259 ** that is currently stored within the body of pPg to the pPg free-list.
74260 ** The cell-pointers and other fields of the page are not updated.
74262 ** This function returns the total number of cells added to the free-list.
74270 u8 * const aData = pPg->aData;
74271 u8 * const pEnd = &aData[pPg->pBt->usableSize];
74272 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
74280 u8 *pCell = pCArray->apCell[i];
74286 sz = pCArray->szCell[i]; assert( sz>0 );
74289 assert( pFree>aData && (pFree - aData)<65536 );
74290 freeSpace(pPg, (u16)(pFree - aData), szFree);
74305 assert( pFree>aData && (pFree - aData)<65536 );
74306 freeSpace(pPg, (u16)(pFree - aData), szFree);
74313 ** balanced. The current page, pPg, has pPg->nCell cells starting with
74314 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
74320 ** The pPg->nFree field is invalid when this function returns. It is the
74330 u8 * const aData = pPg->aData;
74331 const int hdr = pPg->hdrOffset;
74332 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
74333 int nCell = pPg->nCell; /* Cells stored on pPg */
74337 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
74341 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
74342 memcpy(pTmp, aData, pPg->pBt->usableSize);
74348 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
74350 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
74351 nCell -= nShift;
74354 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
74356 nCell -= nTail;
74361 if( pData>pPg->aDataEnd ) goto editpage_fail;
74365 int nAdd = MIN(nNew,iOld-iNew);
74366 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
74368 pCellptr = pPg->aCellIdx;
74378 for(i=0; i<pPg->nOverflow; i++){
74379 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
74381 pCellptr = &pPg->aCellIdx[iCell * 2];
74383 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
74396 pCellptr = &pPg->aCellIdx[nCell*2];
74399 iNew+nCell, nNew-nCell, pCArray
74402 pPg->nCell = nNew;
74403 pPg->nOverflow = 0;
74405 put2byte(&aData[hdr+3], pPg->nCell);
74406 put2byte(&aData[hdr+5], pData - aData);
74410 u8 *pCell = pCArray->apCell[i+iNew];
74411 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
74412 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
74413 pCell = &pTmp[pCell - aData];
74416 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
74431 ** a new entry is being inserted on the extreme right-end of the
74435 ** Instead of trying to balance the 3 right-most leaf pages, just add
74436 ** a new page to the right-hand side and put the one new entry in
74442 ** pPage is the leaf page which is the right-most page in the tree.
74444 ** which is also the right-most entry on the page.
74453 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
74458 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74459 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
74460 assert( pPage->nOverflow==1 );
74462 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
74463 assert( pPage->nFree>=0 );
74464 assert( pParent->nFree>=0 );
74466 /* Allocate a new page. This page will become the right-sibling of
74475 u8 *pCell = pPage->apOvfl[0];
74476 u16 szCell = pPage->xCellSize(pPage, pCell);
74480 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
74481 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
74487 b.apEnd[0] = pPage->aDataEnd;
74494 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
74496 /* If this is an auto-vacuum database, update the pointer map
74506 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
74507 if( szCell>pNew->minLocal ){
74513 ** consists of a 4-byte page number (the page number of pPage) and
74517 ** To find the largest key value on pPage, first find the right-most
74519 ** record-length (a variable length integer at most 32-bits in size)
74521 ** The first of the while(...) loops below skips over the record-length
74525 pCell = findCell(pPage, pPage->nCell-1);
74533 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
74534 0, pPage->pgno, &rc);
74537 /* Set the right-child pointer of pParent to point to the new page. */
74538 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
74552 ** for setting pointer-map entries.
74560 BtShared *pBt = pPage->pBt;
74561 assert( pPage->isInit );
74563 for(j=0; j<pPage->nCell; j++){
74568 pPage->xParseCell(pPage, z, &info);
74570 Pgno ovfl = get4byte(&z[info.nSize-4]);
74572 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
74574 if( !pPage->leaf ){
74577 assert( n==pPage->pgno && e==PTRMAP_BTREE );
74580 if( !pPage->leaf ){
74581 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
74583 assert( n==pPage->pgno && e==PTRMAP_BTREE );
74591 ** This function is used to copy the contents of the b-tree node stored
74593 ** the pointer-map entries for each child page are updated so that the
74609 BtShared * const pBt = pFrom->pBt;
74610 u8 * const aFrom = pFrom->aData;
74611 u8 * const aTo = pTo->aData;
74612 int const iFromHdr = pFrom->hdrOffset;
74613 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
74618 assert( pFrom->isInit );
74619 assert( pFrom->nFree>=iToHdr );
74620 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
74622 /* Copy the b-tree node content from page pFrom to page pTo. */
74624 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
74625 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
74632 pTo->isInit = 0;
74640 /* If this is an auto-vacuum database, update the pointer-map entries
74641 ** for any b-tree or overflow pages that pTo now contains the pointers to.
74683 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
74692 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
74693 int isRoot, /* True if pParent is a root-page */
74701 int nxDiv; /* Next divider slot in pParent->aCell[] */
74706 int pageFlags; /* Value of pPage->aData[0] */
74712 u8 *pRight; /* Location in parent of right-sibling pointer */
74713 u8 *apDiv[NB-1]; /* Divider cells in pParent */
74714 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
74716 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
74727 pBt = pParent->pBt;
74728 assert( sqlite3_mutex_held(pBt->mutex) );
74729 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
74736 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
74737 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
74742 assert( pParent->nFree>=0 );
74755 i = pParent->nOverflow + pParent->nCell;
74763 nxDiv = i-2+bBulk;
74765 nxDiv = iParentIdx-1;
74767 i = 2-bBulk;
74770 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
74771 pRight = &pParent->aData[pParent->hdrOffset+8];
74773 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
74784 if( apOld[i]->nFree<0 ){
74791 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
74792 if( (i--)==0 ) break;
74794 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
74795 apDiv[i] = pParent->apOvfl[0];
74797 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74798 pParent->nOverflow = 0;
74800 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
74802 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
74811 ** But not if we are in secure-delete mode. In secure-delete mode,
74816 if( pBt->btsFlags & BTS_FAST_SECURE ){
74821 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
74822 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
74824 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
74827 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
74831 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
74841 + pBt->pageSize; /* aSpace1 */
74843 assert( szScratch<=7*(int)pBt->pageSize );
74870 leafCorrection = b.pRef->leaf*4;
74871 leafData = b.pRef->intKeyLeaf;
74874 int limit = pOld->nCell;
74875 u8 *aData = pOld->aData;
74876 u16 maskPage = pOld->maskPage;
74877 u8 *piCell = aData + pOld->cellOffset;
74881 /* Verify that all sibling pages are of the same "type" (table-leaf,
74882 ** table-interior, index-leaf, or index-interior).
74884 if( pOld->aData[0]!=apOld[0]->aData[0] ){
74906 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
74907 if( pOld->nOverflow>0 ){
74908 if( NEVER(limit<pOld->aiOvfl[0]) ){
74912 limit = pOld->aiOvfl[0];
74918 for(k=0; k<pOld->nOverflow; k++){
74919 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
74920 b.apCell[b.nCell] = pOld->apOvfl[k];
74924 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
74931 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
74934 if( i<nOld-1 && !leafData){
74941 assert( sz<=pBt->maxLocal+23 );
74942 assert( iSpace1 <= (int)pBt->pageSize );
74946 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
74947 if( !pOld->leaf ){
74949 assert( pOld->hdrOffset==0 || CORRUPT_DB );
74952 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
74959 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
74971 ** size of all cells on the i-th page and cntNew[] which is the index
74978 ** szNew[i]: Spaced used on the i-th sibling page.
74980 ** the right of the i-th sibling page.
74984 usableSpace = pBt->usableSize - 12 + leafCorrection;
74987 b.apEnd[k] = p->aDataEnd;
74989 if( k && b.ixNx[k]==b.ixNx[k-1] ){
74990 k--; /* Omit b.ixNx[] entry for child pages with no cells */
74994 b.apEnd[k] = pParent->aDataEnd;
74997 assert( p->nFree>=0 );
74998 szNew[i] = usableSpace - p->nFree;
74999 for(j=0; j<p->nOverflow; j++){
75000 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
75011 szNew[k-1] = 0;
75012 cntNew[k-1] = b.nCell;
75014 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
75015 szNew[i] -= sz;
75024 cntNew[i]--;
75038 szNew[i+1] -= sz;
75042 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
75051 ** always nearly full, while the right-most sibling might be nearly empty.
75056 ** be so out of balance as to be illegal. For example, the right-most
75059 for(i=k-1; i>0; i--){
75061 int szLeft = szNew[i-1]; /* Size of sibling on the left */
75062 int r; /* Index of right-most cell in left sibling */
75065 r = cntNew[i-1] - 1;
75066 d = r + 1 - leafData;
75073 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
75077 szLeft -= b.szCell[r] + 2;
75078 cntNew[i-1] = r;
75079 r--;
75080 d--;
75083 szNew[i-1] = szLeft;
75084 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
75090 /* Sanity check: For a non-corrupt database file one of the follwing
75097 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
75099 apOld[0]->pgno, apOld[0]->nCell,
75100 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
75101 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
75107 pageFlags = apOld[0]->aData[0];
75113 rc = sqlite3PagerWrite(pNew->pDbPage);
75115 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
75130 /* Set the pointer-map entry for the new sibling page. */
75132 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
75153 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
75154 aPgFlags[i] = apNew[i]->pDbPage->flags;
75178 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
75180 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
75181 apNew[i]->pgno = pgno;
75187 apNew[0]->pgno, szNew[0], cntNew[0],
75188 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
75189 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
75190 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
75191 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
75192 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
75193 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
75194 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
75195 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
75198 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
75200 assert( apNew[nNew-1]!=0 );
75201 put4byte(pRight, apNew[nNew-1]->pgno);
75203 /* If the sibling pages are not leaves, ensure that the right-child pointer
75204 ** of the right-most new sibling page is set to the value that was
75205 ** originally in the same field of the right-most old sibling page. */
75207 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
75208 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
75223 ** associated with the right-child of each sibling may also need to be
75230 int cntOldNext = pNew->nCell + pNew->nOverflow;
75241 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
75255 || pNew->pgno!=aPgno[iOld]
75256 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
75259 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
75261 if( cachedCellSize(&b,i)>pNew->minLocal ){
75270 for(i=0; i<nNew-1; i++){
75283 if( !pNew->leaf ){
75284 memcpy(&pNew->aData[8], pCell, 4);
75286 /* If the tree is a leaf-data tree, and the siblings are leaves,
75288 ** cell consists of the integer key for the right-most cell of
75289 ** the sibling-page assembled above only.
75292 j--;
75293 pNew->xParseCell(pNew, b.apCell[j], &info);
75298 pCell -= 4;
75299 /* Obscure case for non-leaf-data trees: If the cell at pCell was
75306 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
75312 sz = pParent->xCellSize(pParent, pCell);
75316 assert( sz<=pBt->maxLocal+23 );
75317 assert( iOvflSpace <= (int)pBt->pageSize );
75324 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
75326 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
75333 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
75335 ** the left-hand sibling apNew[iPg-1] has been updated.
75339 ** the right-hand sibling apNew[iPg+1] has been updated.
75343 ** The iPg value in the following loop starts at nNew-1 goes down
75344 ** to 0, then back up to nNew-1 again, thus making two passes over
75351 for(i=1-nNew; i<nNew; i++){
75352 int iPg = i<0 ? -i : i;
75356 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
75363 ** only after iPg-1 has already been updated. */
75364 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
75374 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
75375 iNew = cntNew[iPg-1] + !leafData;
75376 nNewCell = cntNew[iPg] - iNew;
75382 apNew[iPg]->nFree = usableSpace-szNew[iPg];
75383 assert( apNew[iPg]->nOverflow==0 );
75384 assert( apNew[iPg]->nCell==nNewCell );
75394 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
75395 /* The root page of the b-tree now contains no cells. The only sibling
75396 ** page is the right-child of the parent. Copy the contents of the
75398 ** b-tree structure by one. This is described as the "balance-shallower"
75399 ** sub-algorithm in some documentation.
75401 ** If this is an auto-vacuum database, the call to copyNodeContent()
75402 ** sets all pointer-map entries corresponding to database image pages
75411 rc = defragmentPage(apNew[0], -1);
75413 assert( apNew[0]->nFree ==
75414 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
75415 - apNew[0]->nCell*2)
75421 /* Fix the pointer map entries associated with the right-child of each
75425 u32 key = get4byte(&apNew[i]->aData[8]);
75426 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
75430 assert( pParent->isInit );
75441 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
75468 ** This function is called when the root page of a b-tree structure is
75473 ** page is then overwritten to make it an empty page with the right-child
75476 ** Before returning, all pointer-map entries corresponding to pages
75477 ** that the new child-page now contains pointers to are updated. The
75478 ** entry corresponding to the new right-child pointer of the root
75490 BtShared *pBt = pRoot->pBt; /* The BTree */
75492 assert( pRoot->nOverflow>0 );
75493 assert( sqlite3_mutex_held(pBt->mutex) );
75495 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
75496 ** page that will become the new right-child of pPage. Copy the contents
75499 rc = sqlite3PagerWrite(pRoot->pDbPage);
75501 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
75504 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
75512 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
75513 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
75514 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
75516 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
75519 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
75520 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
75521 memcpy(pChild->apOvfl, pRoot->apOvfl,
75522 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
75523 pChild->nOverflow = pRoot->nOverflow;
75525 /* Zero the contents of pRoot. Then install pChild as the right-child. */
75526 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
75527 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
75535 ** on the same B-tree as pCur.
75538 ** pointing to the same b-tree. If an insert occurs on one SQL table
75540 ** table linked to the same b-tree. If the secondary insert causes a
75546 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
75548 && pOther->eState==CURSOR_VALID
75549 && pOther->pPage==pCur->pPage
75577 MemPage *pPage = pCur->pPage;
75579 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
75580 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
75586 }else if( (iPage = pCur->iPage)==0 ){
75587 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
75588 /* The root page of the b-tree is overfull. In this case call the
75589 ** balance_deeper() function to create a new child for the root-page
75590 ** and copy the current contents of the root-page to it. The
75591 ** next iteration of the do-loop will balance the child page.
75595 rc = balance_deeper(pPage, &pCur->apPage[1]);
75597 pCur->iPage = 1;
75598 pCur->ix = 0;
75599 pCur->aiIdx[0] = 0;
75600 pCur->apPage[0] = pPage;
75601 pCur->pPage = pCur->apPage[1];
75602 assert( pCur->pPage->nOverflow );
75608 MemPage * const pParent = pCur->apPage[iPage-1];
75609 int const iIdx = pCur->aiIdx[iPage-1];
75611 rc = sqlite3PagerWrite(pParent->pDbPage);
75612 if( rc==SQLITE_OK && pParent->nFree<0 ){
75617 if( pPage->intKeyLeaf
75618 && pPage->nOverflow==1
75619 && pPage->aiOvfl[0]==pPage->nCell
75620 && pParent->pgno!=1
75621 && pParent->nCell==iIdx
75626 ** happens, the next iteration of the do-loop will balance pParent
75645 ** become overfull or underfull. The next iteration of the do-loop
75650 ** A subsequent iteration of the do-loop will deal with this by
75652 ** but it doesn't deal with overflow cells - just moves them to a
75659 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
75661 pCur->hints&BTREE_BULKLOAD);
75677 pPage->nOverflow = 0;
75679 /* The next iteration of the do-loop balances the parent page. */
75681 pCur->iPage--;
75682 assert( pCur->iPage>=0 );
75683 pCur->pPage = pCur->apPage[pCur->iPage];
75703 int nData = pX->nData - iOffset;
75709 int rc = sqlite3PagerWrite(pPage->pDbPage);
75711 memset(pDest + i, 0, iAmt - i);
75718 iAmt-nData);
75722 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
75723 int rc = sqlite3PagerWrite(pPage->pDbPage);
75729 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
75740 int iOffset; /* Next byte of pX->pData to write */
75741 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
75743 MemPage *pPage = pCur->pPage; /* Page being written */
75748 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
75749 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
75754 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
75755 0, pCur->info.nLocal);
75757 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
75760 iOffset = pCur->info.nLocal;
75763 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
75764 pBt = pPage->pBt;
75765 ovflPageSize = pBt->usableSize - 4;
75769 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
75773 ovflPgno = get4byte(pPage->aData);
75775 ovflPageSize = nTotal - iOffset;
75777 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
75780 sqlite3PagerUnref(pPage->pDbPage);
75803 ** If the seekResult parameter is non-zero, then a successful call to
75814 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
75815 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
75825 int loc = seekResult; /* -1: before desired location +1: after */
75829 Btree *p = pCur->pBtree;
75830 BtShared *pBt = p->pBt;
75835 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
75839 ** In some cases, the call to btreeMoveto() below is a no-op. For
75840 ** example, when inserting data into a table with auto-generated integer
75843 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
75848 if( pCur->curFlags & BTCF_Multiple ){
75849 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
75851 if( loc && pCur->iPage<0 ){
75864 if( pCur->eState>=CURSOR_REQUIRESEEK ){
75865 testcase( pCur->eState==CURSOR_REQUIRESEEK );
75866 testcase( pCur->eState==CURSOR_FAULT );
75872 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
75873 && pBt->inTransaction==TRANS_WRITE
75874 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
75875 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
75878 ** expecting an index b-tree, then the caller should be inserting blob
75882 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
75884 if( pCur->pKeyInfo==0 ){
75885 assert( pX->pKey==0 );
75886 /* If this is an insert into a table b-tree, invalidate any incrblob
75888 if( p->hasIncrblobCur ){
75889 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
75897 assert( pCur->curFlags & BTCF_ValidNKey );
75898 assert( pX->nKey==pCur->info.nKey );
75907 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
75910 assert( pX->nData>=0 && pX->nZero>=0 );
75911 if( pCur->info.nSize!=0
75912 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
75923 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
75941 if( pX->nMem ){
75943 r.pKeyInfo = pCur->pKeyInfo;
75944 r.aMem = pX->aMem;
75945 r.nField = pX->nMem;
75950 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
75962 if( pCur->info.nKey==pX->nKey ){
75964 x2.pData = pX->pKey;
75965 x2.nData = pX->nKey;
75971 assert( pCur->eState==CURSOR_VALID
75972 || (pCur->eState==CURSOR_INVALID && loc) );
75974 pPage = pCur->pPage;
75975 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
75976 assert( pPage->leaf || !pPage->intKey );
75977 if( pPage->nFree<0 ){
75978 if( NEVER(pCur->eState>CURSOR_INVALID) ){
75979 /* ^^^^^--- due to the moveToRoot() call above */
75988 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
75990 assert( pPage->isInit || CORRUPT_DB );
75991 newCell = pBt->pTmpSpace;
75995 szNew = pBt->nPreformatSize;
75997 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
75999 pPage->xParseCell(pPage, newCell, &info);
76001 Pgno ovfl = get4byte(&newCell[szNew-4]);
76002 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
76009 assert( szNew==pPage->xCellSize(pPage, newCell) );
76011 idx = pCur->ix;
76015 if( idx>=pPage->nCell ){
76018 rc = sqlite3PagerWrite(pPage->pDbPage);
76023 if( !pPage->leaf ){
76027 testcase( pCur->curFlags & BTCF_ValidOvfl );
76030 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
76040 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
76042 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
76045 if( oldCell+szNew > pPage->aDataEnd ){
76053 }else if( loc<0 && pPage->nCell>0 ){
76054 assert( pPage->leaf );
76055 idx = ++pCur->ix;
76056 pCur->curFlags &= ~BTCF_ValidNKey;
76058 assert( pPage->leaf );
76061 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
76062 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
76076 ** multiple records into an intkey b-tree using a single cursor (as can
76079 ** the b-tree if possible. If the cursor is left pointing to the last
76084 pCur->info.nSize = 0;
76085 if( pPage->nOverflow ){
76087 pCur->curFlags &= ~(BTCF_ValidNKey);
76094 pCur->pPage->nOverflow = 0;
76095 pCur->eState = CURSOR_INVALID;
76098 if( pCur->pKeyInfo ){
76099 assert( pCur->pKey==0 );
76100 pCur->pKey = sqlite3Malloc( pX->nKey );
76101 if( pCur->pKey==0 ){
76104 memcpy(pCur->pKey, pX->pKey, pX->nKey);
76107 pCur->eState = CURSOR_REQUIRESEEK;
76108 pCur->nKey = pX->nKey;
76111 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
76134 BtShared *pBt = pDest->pBt;
76135 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
76141 if( pSrc->info.nPayload<0x80 ){
76142 *(aOut++) = pSrc->info.nPayload;
76144 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
76146 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
76147 nIn = pSrc->info.nLocal;
76148 aIn = pSrc->info.pPayload;
76149 if( aIn+nIn>pSrc->pPage->aDataEnd ){
76152 nRem = pSrc->info.nPayload;
76153 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
76155 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
76157 Pager *pSrcPager = pSrc->pBt->pPager;
76164 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
76165 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
76166 if( nOut<pSrc->info.nPayload ){
76168 pBt->nPreformatSize += 4;
76172 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
76175 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
76179 nRem -= nOut;
76185 nOut -= nCopy;
76186 nIn -= nCopy;
76198 nIn = pSrc->pBt->usableSize - 4;
76209 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
76214 pPgnoOut = pPageOut->aData;
76217 nOut = MIN(pBt->usableSize - 4, nRem);
76242 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
76247 Btree *p = pCur->pBtree;
76248 BtShared *pBt = p->pBt;
76258 assert( pBt->inTransaction==TRANS_WRITE );
76259 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
76260 assert( pCur->curFlags & BTCF_WriteFlag );
76261 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
76262 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
76264 if( pCur->eState!=CURSOR_VALID ){
76265 if( pCur->eState>=CURSOR_REQUIRESEEK ){
76267 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
76268 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
76273 assert( pCur->eState==CURSOR_VALID );
76275 iCellDepth = pCur->iPage;
76276 iCellIdx = pCur->ix;
76277 pPage = pCur->pPage;
76278 if( pPage->nCell<=iCellIdx ){
76282 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
76288 ** will cause a b-tree rebalance, then this is done by saving the cursor
76304 if( !pPage->leaf
76305 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
76306 (int)(pBt->usableSize*2/3)
76307 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
76309 /* A b-tree rebalance will be required after deleting this entry.
76323 ** sub-tree headed by the child page of the cell being deleted. This makes
76325 if( !pPage->leaf ){
76333 if( pCur->curFlags & BTCF_Multiple ){
76334 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
76338 /* If this is a delete operation to remove a row from a table b-tree,
76340 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
76341 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
76347 rc = sqlite3PagerWrite(pPage->pDbPage);
76354 ** is currently pointing to the largest entry in the sub-tree headed
76355 ** by the child-page of the cell that was just deleted from an internal
76358 if( !pPage->leaf ){
76359 MemPage *pLeaf = pCur->pPage;
76364 if( pLeaf->nFree<0 ){
76368 if( iCellDepth<pCur->iPage-1 ){
76369 n = pCur->apPage[iCellDepth+1]->pgno;
76371 n = pCur->pPage->pgno;
76373 pCell = findCell(pLeaf, pLeaf->nCell-1);
76374 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
76375 nCell = pLeaf->xCellSize(pLeaf, pCell);
76377 pTmp = pBt->pTmpSpace;
76379 rc = sqlite3PagerWrite(pLeaf->pDbPage);
76381 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
76383 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
76402 assert( pCur->pPage->nOverflow==0 );
76403 assert( pCur->pPage->nFree>=0 );
76404 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
76406 ** then balance() will always be a no-op. No need to invoke it. */
76411 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
76412 releasePageNotNull(pCur->pPage);
76413 pCur->iPage--;
76414 while( pCur->iPage>iCellDepth ){
76415 releasePage(pCur->apPage[pCur->iPage--]);
76417 pCur->pPage = pCur->apPage[pCur->iPage];
76423 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
76424 assert( pPage==pCur->pPage || CORRUPT_DB );
76425 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
76426 pCur->eState = CURSOR_SKIPNEXT;
76427 if( iCellIdx>=pPage->nCell ){
76428 pCur->skipNext = -1;
76429 pCur->ix = pPage->nCell-1;
76431 pCur->skipNext = 1;
76437 pCur->eState = CURSOR_REQUIRESEEK;
76457 BtShared *pBt = p->pBt;
76461 int ptfFlags; /* Page-type flage for the root page of new table */
76464 assert( pBt->inTransaction==TRANS_WRITE );
76465 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
76473 if( pBt->autoVacuum ){
76474 Pgno pgnoMove; /* Move a page here to make room for the root-page */
76479 ** out to be an overflow page, delete all overflow page-map caches
76485 ** root page of the new table should go. meta[3] is the largest root-page
76486 ** created so far, so the new root-page is (meta[3]+1).
76494 /* The new root-page may not be allocated on a pointer-map page, or the
76513 /* pgnoRoot is the page that will be used for the root-page of
76557 rc = sqlite3PagerWrite(pRoot->pDbPage);
76566 /* Update the pointer-map and meta-data with the new root-page number. */
76577 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
76589 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
76596 sqlite3PagerUnref(pRoot->pDbPage);
76597 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
76626 assert( sqlite3_mutex_held(pBt->mutex) );
76632 if( (pBt->openFlags & BTREE_SINGLE)==0
76633 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
76638 hdr = pPage->hdrOffset;
76639 for(i=0; i<pPage->nCell; i++){
76641 if( !pPage->leaf ){
76648 if( !pPage->leaf ){
76649 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
76651 if( pPage->intKey ) pnChange = 0;
76654 testcase( !pPage->intKey );
76655 *pnChange += pPage->nCell;
76659 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
76660 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
76682 BtShared *pBt = p->pBt;
76684 assert( p->inTrans==TRANS_WRITE );
76690 ** is the root of a table b-tree - if it is not, the following call is
76691 ** a no-op). */
76692 if( p->hasIncrblobCur ){
76707 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
76733 BtShared *pBt = p->pBt;
76736 assert( p->inTrans==TRANS_WRITE );
76756 if( pBt->autoVacuum ){
76761 /* If the table being dropped is the table with the largest root-page
76770 /* The table being dropped does not have the largest root-page
76772 ** gap left by the deleted root-page.
76795 /* Set the new 'max-root-page' value in the database header. This
76797 ** be a root-page number, less one again if that is the
76800 maxRootPgno--;
76803 maxRootPgno--;
76825 ** This function may only be called if the b-tree connection already
76828 ** Read the meta-information out of a database file. Meta[0]
76831 ** is read-only, the others are read/write.
76845 BtShared *pBt = p->pBt;
76848 assert( p->inTrans>TRANS_NONE );
76850 assert( pBt->pPage1 );
76854 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
76856 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
76859 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
76860 ** database, mark the database as read-only. */
76863 pBt->btsFlags |= BTS_READ_ONLY;
76871 ** Write meta-information back into the database. Meta[0] is
76872 ** read-only and may not be written.
76875 BtShared *pBt = p->pBt;
76880 assert( p->inTrans==TRANS_WRITE );
76881 assert( pBt->pPage1!=0 );
76882 pP1 = pBt->pPage1->aData;
76883 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
76888 assert( pBt->autoVacuum || iMeta==0 );
76890 pBt->incrVacuum = (u8)iMeta;
76899 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
76900 ** number of entries in the b-tree and write the result to *pnEntry.
76917 ** page in the B-Tree structure (not including overflow pages).
76919 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
76921 MemPage *pPage; /* Current page of the b-tree */
76923 /* If this is a leaf page or the tree is not an int-key tree, then
76927 pPage = pCur->pPage;
76928 if( pPage->leaf || !pPage->intKey ){
76929 nEntry += pPage->nCell;
76935 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
76937 ** to visit is the right-child of its parent.
76942 if( pPage->leaf ){
76944 if( pCur->iPage==0 ){
76945 /* All pages of the b-tree have been visited. Return successfully. */
76950 }while ( pCur->ix>=pCur->pPage->nCell );
76952 pCur->ix++;
76953 pPage = pCur->pPage;
76957 ** points at. This is the right-child if (iIdx==pPage->nCell).
76959 iIdx = pCur->ix;
76960 if( iIdx==pPage->nCell ){
76961 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
76976 return p->pBt->pPager;
76981 ** Append a message to the error message string.
76989 if( !pCheck->mxErr ) return;
76990 pCheck->mxErr--;
76991 pCheck->nErr++;
76993 if( pCheck->errMsg.nChar ){
76994 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
76996 if( pCheck->zPfx ){
76997 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
76999 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
77001 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
77002 pCheck->bOomFault = 1;
77010 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
77014 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
77015 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
77022 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
77023 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
77029 ** reference to the page, add an error message to pCheck->zErrMsg.
77036 if( iPage>pCheck->nPage || iPage==0 ){
77044 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
77051 ** Check that the entry in the pointer-map for page iChild maps to
77052 ** page iParent, pointer type ptrType. If not, append an error message
77065 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
77067 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
77092 int nErrAtStart = pCheck->nErr;
77093 while( iPage!=0 && pCheck->mxErr ){
77097 N--;
77098 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
77106 if( pCheck->pBt->autoVacuum ){
77110 if( n>pCheck->pBt->usableSize/4-2 ){
77113 N--;
77118 if( pCheck->pBt->autoVacuum ){
77124 N -= n;
77129 /* If this database supports auto-vacuum and iPage is not the last
77130 ** page in this overflow list, check that the pointer-map entry for
77133 if( pCheck->pBt->autoVacuum && N>0 ){
77142 if( N && nErrAtStart==pCheck->nErr ){
77146 expected-N, expected);
77152 ** An implementation of a min-heap.
77162 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
77189 aHeap[0]--;
77226 int depth = -1, d2; /* Depth of a subtree */
77242 u32 *heap = 0; /* Min-heap used for checking cell coverage */
77243 u32 x, prev = 0; /* Next and previous entry on the min-heap */
77244 const char *saved_zPfx = pCheck->zPfx;
77245 int saved_v1 = pCheck->v1;
77246 int saved_v2 = pCheck->v2;
77251 pBt = pCheck->pBt;
77252 usableSize = pBt->usableSize;
77255 pCheck->zPfx = "Page %u: ";
77256 pCheck->v1 = iPage;
77265 savedIsInit = pPage->isInit;
77266 pPage->isInit = 0;
77278 data = pPage->aData;
77279 hdr = pPage->hdrOffset;
77282 pCheck->zPfx = "On tree page %u cell %d: ";
77286 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
77289 assert( pPage->nCell==nCell );
77291 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
77292 ** immediately follows the b-tree page header. */
77293 cellStart = hdr + 12 - 4*pPage->leaf;
77294 assert( pPage->aCellIdx==&data[cellStart] );
77295 pCellIdx = &data[cellStart + 2*(nCell-1)];
77297 if( !pPage->leaf ){
77298 /* Analyze the right-child page of internal pages */
77301 if( pBt->autoVacuum ){
77302 pCheck->zPfx = "On page %u at right child: ";
77311 heap = pCheck->heap;
77315 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
77317 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
77321 pCheck->v2 = i;
77324 pCellIdx -= 2;
77325 if( pc<contentOffset || pc>usableSize-4 ){
77327 pc, contentOffset, usableSize-4);
77332 pPage->xParseCell(pPage, pCell, &info);
77340 if( pPage->intKey ){
77352 assert( pc + info.nSize - 4 <= usableSize );
77353 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
77354 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
77356 if( pBt->autoVacuum ){
77363 if( !pPage->leaf ){
77367 if( pBt->autoVacuum ){
77378 /* Populate the coverage-checking heap for leaf pages */
77379 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
77386 pCheck->zPfx = 0;
77387 if( doCoverageCheck && pCheck->mxErr>0 ){
77388 /* For leaf pages, the min-heap has already been initialized and the
77391 if( !pPage->leaf ){
77392 heap = pCheck->heap;
77394 for(i=nCell-1; i>=0; i--){
77397 size = pPage->xCellSize(pPage, &data[pc]);
77398 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
77401 /* Add the freeblocks to the min-heap
77403 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
77410 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
77413 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
77414 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
77415 ** big-endian integer which is the offset in the b-tree page of the next
77419 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
77422 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
77425 /* Analyze the min-heap looking for overlap between cells and/or
77428 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
77433 ** The loop below pulls entries from the min-heap in order and compares
77439 prev = contentOffset - 1; /* Implied first min-heap entry */
77446 nFrag += (x>>16) - (prev&0xffff) - 1;
77450 nFrag += usableSize - (prev&0xffff) - 1;
77451 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
77452 ** is stored in the fifth field of the b-tree page header.
77453 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
77464 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
77466 pCheck->zPfx = saved_zPfx;
77467 pCheck->v1 = saved_v1;
77468 pCheck->v2 = saved_v2;
77479 ** A read-only or read-write transaction must be opened before calling
77483 ** allocation errors, an error message held in memory obtained from
77484 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
77488 ** root pages is incomplete. This is a "partial integrity-check". This
77506 BtShared *pBt = p->pBt;
77507 u64 savedDbFlags = pBt->db->flags;
77522 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
77523 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
77527 sCheck.pPager = pBt->pPager;
77548 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
77561 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
77562 get4byte(&pBt->pPage1->aData[36]));
77570 if( pBt->autoVacuum ){
77574 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
77581 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
77588 testcase( pBt->db->flags & SQLITE_CellSizeCk );
77589 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
77594 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
77600 pBt->db->flags = savedDbFlags;
77611 /* If the database supports auto-vacuum, make sure no tables contain
77612 ** references to pointer-map pages.
77615 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
77619 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
77638 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
77646 ** an empty string if the database is in-memory or a TEMP database.
77652 assert( p->pBt->pPager!=0 );
77653 return sqlite3PagerFilename(p->pBt->pPager, 1);
77665 assert( p->pBt->pPager!=0 );
77666 return sqlite3PagerJournalname(p->pBt->pPager);
77674 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
77675 return p ? p->inTrans : 0;
77683 ** transaction on the shared-cache the argument Btree is connected to.
77690 BtShared *pBt = p->pBt;
77692 if( pBt->inTransaction!=TRANS_NONE ){
77695 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
77708 assert( sqlite3_mutex_held(p->db->mutex) );
77709 return p->nBackup!=0;
77714 ** a single shared-btree. The memory is used by client code for its own
77715 ** purposes (for example, to store a high-level schema associated with
77716 ** the shared-btree). The btree layer manages reference counting issues.
77718 ** The first time this is called on a shared-btree, nBytes bytes of memory
77727 ** Just before the shared-btree is closed, the function passed as the
77733 BtShared *pBt = p->pBt;
77735 if( !pBt->pSchema && nBytes ){
77736 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
77737 pBt->xFreeSchema = xFree;
77740 return pBt->pSchema;
77750 assert( sqlite3_mutex_held(p->db->mutex) );
77767 assert( p->inTrans!=TRANS_NONE );
77768 if( p->sharable ){
77798 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
77799 assert( pCsr->curFlags & BTCF_Incrblob );
77805 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
77806 if( pCsr->eState!=CURSOR_VALID ){
77812 ** version of the b-tree page modified by the accessPayload call below.
77818 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
77824 ** (c) the connection holds a write-lock on the table (if required),
77825 ** (d) there are no conflicting read-locks, and
77828 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
77831 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
77832 && pCsr->pBt->inTransaction==TRANS_WRITE );
77833 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
77834 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
77835 assert( pCsr->pPage->intKey );
77844 pCur->curFlags |= BTCF_Incrblob;
77845 pCur->pBtree->hasIncrblobCur = 1;
77855 BtShared *pBt = pBtree->pBt;
77863 pBt->btsFlags &= ~BTS_NO_WAL;
77864 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
77868 u8 *aData = pBt->pPage1->aData;
77872 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
77881 pBt->btsFlags &= ~BTS_NO_WAL;
77890 return (pCsr->hints & mask)!=0;
77894 ** Return true if the given Btree is read-only.
77897 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
77910 return p->sharable;
77919 testcase( p->sharable );
77920 return p->pBt->nRef;
77948 Btree *pDest; /* Destination b-tree file */
77950 int bDestLocked; /* True once a write-transaction is open on pDest */
77954 Btree *pSrc; /* Source b-tree file */
77972 ** structure may be accessed via two groups of thread-safe entry points:
77986 ** backup_pagecount() are not thread-safe functions. If they are called
77994 ** Non-sharable Btrees (in-memory databases for example), do not have
78001 ** a NULL pointer and write an error message to pErrorDb.
78005 ** error message to pErrorDb.
78030 return pDb->aDb[i].pBt;
78039 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
78044 ** Check that there is no open read-transaction on the b-tree passed as the
78046 ** is an open read-transaction, return SQLITE_ERROR and leave an error
78047 ** message in database handle db.
78062 ** If an error occurs, NULL is returned and an error code and error message
78088 sqlite3_mutex_enter(pSrcDb->mutex);
78089 sqlite3_mutex_enter(pDestDb->mutex);
78098 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78109 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
78110 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
78111 p->pDestDb = pDestDb;
78112 p->pSrcDb = pSrcDb;
78113 p->iNext = 1;
78114 p->isAttached = 0;
78116 if( 0==p->pSrc || 0==p->pDest
78117 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
78129 p->pSrc->nBackup++;
78132 sqlite3_mutex_leave(pDestDb->mutex);
78133 sqlite3_mutex_leave(pSrcDb->mutex);
78157 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
78158 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
78159 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
78165 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
78166 assert( p->bDestLocked );
78167 assert( !isFatalError(p->rc) );
78168 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
78171 /* Catch the case where the destination is an in-memory database and the
78182 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
78185 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
78203 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
78215 ** this function is a no-op.
78235 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
78236 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78237 p->pNext = *pp;
78239 p->isAttached = 1;
78243 ** Copy nPage pages from the source b-tree to the destination.
78254 sqlite3_mutex_enter(p->pSrcDb->mutex);
78255 sqlite3BtreeEnter(p->pSrc);
78256 if( p->pDestDb ){
78257 sqlite3_mutex_enter(p->pDestDb->mutex);
78260 rc = p->rc;
78262 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
78263 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
78265 int nSrcPage = -1; /* Size of source db in pages */
78268 /* If the source pager is currently in a write-transaction, return
78271 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
78277 /* If there is no open read-transaction on the source database, open
78281 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
78282 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
78292 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
78297 if( SQLITE_OK==rc && p->bDestLocked==0
78298 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
78299 (int*)&p->iDestSchema))
78301 p->bDestLocked = 1;
78306 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
78307 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
78308 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
78313 /* Now that there is a read-lock on the source database, query the
78316 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
78318 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
78319 const Pgno iSrcPg = p->iNext; /* Source page number */
78320 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
78328 p->iNext++;
78331 p->nPagecount = nSrcPage;
78332 p->nRemaining = nSrcPage+1-p->iNext;
78333 if( p->iNext>(Pgno)nSrcPage ){
78335 }else if( !p->isAttached ){
78341 ** is to make sure that the schema-version really does change in
78347 rc = sqlite3BtreeNewDb(p->pDest);
78351 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
78354 if( p->pDestDb ){
78355 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
78358 rc = sqlite3BtreeSetVersion(p->pDest, 2);
78375 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
78376 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
78379 nDestTruncate = (nSrcPage+ratio-1)/ratio;
78380 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
78381 nDestTruncate--;
78389 /* If the source page-size is smaller than the destination page-size,
78395 ** pending-byte page in the source database may need to be
78408 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
78420 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
78464 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
78474 ** "committing" a read-only transaction cannot fail.
78478 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
78479 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
78486 p->rc = rc;
78488 if( p->pDestDb ){
78489 sqlite3_mutex_leave(p->pDestDb->mutex);
78491 sqlite3BtreeLeave(p->pSrc);
78492 sqlite3_mutex_leave(p->pSrcDb->mutex);
78506 pSrcDb = p->pSrcDb;
78507 sqlite3_mutex_enter(pSrcDb->mutex);
78508 sqlite3BtreeEnter(p->pSrc);
78509 if( p->pDestDb ){
78510 sqlite3_mutex_enter(p->pDestDb->mutex);
78514 if( p->pDestDb ){
78515 p->pSrc->nBackup--;
78517 if( p->isAttached ){
78518 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
78521 pp = &(*pp)->pNext;
78524 *pp = p->pNext;
78528 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
78531 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
78532 if( p->pDestDb ){
78533 sqlite3Error(p->pDestDb, rc);
78536 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
78538 sqlite3BtreeLeave(p->pSrc);
78539 if( p->pDestDb ){
78540 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
78560 return p->nRemaining;
78574 return p->nPagecount;
78596 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
78597 if( !isFatalError(p->rc) && iPage<p->iNext ){
78603 assert( p->pDestDb );
78604 sqlite3_mutex_enter(p->pDestDb->mutex);
78606 sqlite3_mutex_leave(p->pDestDb->mutex);
78609 p->rc = rc;
78612 }while( (p = p->pNext)!=0 );
78631 for(p=pBackup; p; p=p->pNext){
78632 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
78633 p->iNext = 1;
78655 if( pFd->pMethods ){
78668 b.pSrcDb = pFrom->db;
78677 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
78684 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
78722 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
78735 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
78741 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
78744 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
78746 if( p->flags & MEM_Null ){
78748 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
78755 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
78758 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
78759 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
78760 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
78763 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
78771 assert( (p->flags & MEM_Cleared)==0 );
78775 assert( p->szMalloc==0
78776 || (p->flags==MEM_Undefined
78777 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
78778 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
78788 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
78790 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
78791 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
78792 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
78793 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
78806 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
78808 if( p->flags & MEM_Int ){
78810 /* Work-around for GCC bug
78813 assert( (p->flags&MEM_Int)*2==sizeof(x) );
78814 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
78817 sqlite3Int64ToText(p->u.i, zBuf);
78822 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
78841 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
78855 if( (p->flags & MEM_Str)==0 ) return 1;
78856 if( p->flags & MEM_Term ){
78857 /* Insure that the string is properly zero-terminated. Pay particular
78858 ** attention to the case where p->n is odd */
78859 if( p->szMalloc>0 && p->z==p->zMalloc ){
78860 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
78861 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
78863 assert( p->z[p->n]==0 );
78864 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
78865 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
78867 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
78869 z = p->z;
78872 if( p->enc!=SQLITE_UTF8 ){
78874 if( p->enc==SQLITE_UTF16BE ) z++;
78891 ** routine is a no-op.
78905 if( !(pMem->flags&MEM_Str) ){
78906 pMem->enc = desiredEnc;
78909 if( pMem->enc==desiredEnc ){
78912 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
78922 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
78923 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
78929 ** Make sure pMem->z points to a writable allocation of at least n bytes.
78932 ** pMem->z into the new allocation. pMem must be either a string or
78934 ** in pMem->z is discarded.
78939 testcase( pMem->db==0 );
78943 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
78944 testcase( bPreserve && pMem->z==0 );
78946 assert( pMem->szMalloc==0
78947 || (pMem->flags==MEM_Undefined
78948 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
78949 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
78950 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
78951 if( pMem->db ){
78952 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
78954 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
78955 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
78956 pMem->z = pMem->zMalloc;
78960 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
78961 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
78963 if( pMem->zMalloc==0 ){
78965 pMem->z = 0;
78966 pMem->szMalloc = 0;
78969 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
78972 if( bPreserve && pMem->z ){
78973 assert( pMem->z!=pMem->zMalloc );
78974 memcpy(pMem->zMalloc, pMem->z, pMem->n);
78976 if( (pMem->flags&MEM_Dyn)!=0 ){
78977 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
78978 pMem->xDel((void *)(pMem->z));
78981 pMem->z = pMem->zMalloc;
78982 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
78987 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
78988 ** If pMem->zMalloc already meets or exceeds the requested size, this
78989 ** routine is a no-op.
78992 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
79001 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
79002 if( pMem->szMalloc<szNew ){
79005 assert( (pMem->flags & MEM_Dyn)==0 );
79006 pMem->z = pMem->zMalloc;
79007 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
79016 ** to be a double-zero byte at an even byte boundary in order to
79021 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
79024 pMem->z[pMem->n] = 0;
79025 pMem->z[pMem->n+1] = 0;
79026 pMem->z[pMem->n+2] = 0;
79027 pMem->flags |= MEM_Term;
79039 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79041 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
79043 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
79048 pMem->flags &= ~MEM_Ephem;
79050 pMem->pScopyFrom = 0;
79057 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
79064 assert( pMem->flags & MEM_Zero );
79065 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
79068 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79071 nByte = pMem->n + pMem->u.nZero;
79073 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
79079 assert( pMem->z!=0 );
79080 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
79082 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
79083 pMem->n += pMem->u.nZero;
79084 pMem->flags &= ~(MEM_Zero|MEM_Term);
79094 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79095 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
79096 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
79097 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
79122 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79123 assert( !(pMem->flags&MEM_Zero) );
79124 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
79125 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
79131 pMem->enc = 0;
79135 vdbeMemRenderNum(nByte, pMem->z, pMem);
79136 assert( pMem->z!=0 );
79137 pMem->n = sqlite3Strlen30NN(pMem->z);
79138 pMem->enc = SQLITE_UTF8;
79139 pMem->flags |= MEM_Str|MEM_Term;
79140 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
79158 assert( pMem->db!=0 );
79159 assert( pFunc->xFinalize!=0 );
79160 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
79161 assert( sqlite3_mutex_held(pMem->db->mutex) );
79165 t.db = pMem->db;
79170 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
79171 assert( (pMem->flags & MEM_Dyn)==0 );
79172 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
79189 assert( pFunc->xValue!=0 );
79190 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
79191 assert( pAccum->db!=0 );
79192 assert( sqlite3_mutex_held(pAccum->db->mutex) );
79198 ctx.enc = ENC(pAccum->db);
79199 pFunc->xValue(&ctx);
79214 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
79216 if( p->flags&MEM_Agg ){
79217 sqlite3VdbeMemFinalize(p, p->u.pDef);
79218 assert( (p->flags & MEM_Agg)==0 );
79219 testcase( p->flags & MEM_Dyn );
79221 if( p->flags&MEM_Dyn ){
79222 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
79223 p->xDel((void *)p->z);
79225 p->flags = MEM_Null;
79230 ** by p->xDel and memory in p->zMalloc.
79240 if( p->szMalloc ){
79241 sqlite3DbFreeNN(p->db, p->zMalloc);
79242 p->szMalloc = 0;
79244 p->z = 0;
79259 if( VdbeMemDynamic(p) || p->szMalloc ){
79269 if( p->szMalloc ) vdbeMemClear(p);
79273 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
79274 ** If the double is out of range of a 64-bit signed integer then
79275 ** return the closest available 64-bit signed integer.
79279 /* When floating-point is omitted, double and int64 are the same thing */
79284 ** minimum and maximum 64-bit integers, or they define them
79287 ** larger than a 32-bit integer constant.
79306 ** a floating-point then the value returned is the integer part.
79309 ** an SQL-NULL value, return 0.
79315 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
79321 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79323 flags = pMem->flags;
79326 return pMem->u.i;
79328 return doubleToInt64(pMem->u.r);
79329 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
79345 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
79350 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79352 if( pMem->flags & MEM_Real ){
79353 return pMem->u.r;
79354 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
79355 testcase( pMem->flags & MEM_IntReal );
79356 return (double)pMem->u.i;
79357 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
79370 testcase( pMem->flags & MEM_IntReal );
79371 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
79372 if( pMem->flags & MEM_Null ) return ifNull;
79383 assert( pMem->flags & MEM_Real );
79385 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79388 ix = doubleToInt64(pMem->u.r);
79392 ** (1) the round-trip conversion real->int->real is a no-op, and
79400 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
79401 pMem->u.i = ix;
79411 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79415 pMem->u.i = sqlite3VdbeIntValue(pMem);
79426 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79429 pMem->u.r = sqlite3VdbeRealValue(pMem);
79439 ** For some versions of GCC on 32-bit machines, if you do the more obvious
79441 ** though the r1 and (double)i values are bit-for-bit the same.
79447 && i >= -2251799813685248LL && i < 2251799813685248LL);
79460 testcase( pMem->flags & MEM_Int );
79461 testcase( pMem->flags & MEM_Real );
79462 testcase( pMem->flags & MEM_IntReal );
79463 testcase( pMem->flags & MEM_Null );
79464 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
79467 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
79468 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79469 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
79470 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
79471 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
79473 pMem->u.i = ix;
79479 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
79480 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
79492 if( pMem->flags & MEM_Null ) return SQLITE_OK;
79495 if( (pMem->flags & MEM_Blob)==0 ){
79497 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
79498 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
79500 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
79519 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
79521 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
79522 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
79536 pMem->flags = flags;
79537 pMem->db = db;
79538 pMem->szMalloc = 0;
79558 pMem->flags = MEM_Null;
79572 pMem->flags = MEM_Blob|MEM_Zero;
79573 pMem->n = 0;
79575 pMem->u.nZero = n;
79576 pMem->enc = SQLITE_UTF8;
79577 pMem->z = 0;
79585 assert( pMem->z!=0 );
79586 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
79587 memset(pMem->z, 0, nByte);
79588 pMem->n = n>0?n:0;
79589 pMem->flags = MEM_Blob;
79590 pMem->enc = SQLITE_UTF8;
79598 ** a 64-bit integer.
79602 pMem->u.i = val;
79603 pMem->flags = MEM_Int;
79614 pMem->u.i = val;
79615 pMem->flags = MEM_Int;
79619 /* A no-op destructor */
79632 assert( pMem->flags==MEM_Null );
79634 pMem->u.zPType = zPType ? zPType : "";
79635 pMem->z = pPtr;
79636 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
79637 pMem->eSubtype = 'p';
79638 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
79649 pMem->u.r = val;
79650 pMem->flags = MEM_Real;
79661 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
79662 && pMem->xDel==sqlite3RowSetDelete;
79674 sqlite3 *db = pMem->db;
79681 pMem->z = (char*)p;
79682 pMem->flags = MEM_Blob|MEM_Dyn;
79683 pMem->xDel = sqlite3RowSetDelete;
79689 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
79692 assert( p->db!=0 );
79693 if( p->flags & (MEM_Str|MEM_Blob) ){
79694 int n = p->n;
79695 if( p->flags & MEM_Zero ){
79696 n += p->u.nZero;
79698 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
79709 ** This is used for testing and debugging only - to help ensure that shallow
79715 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
79716 if( pX->pScopyFrom==pMem ){
79718 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
79720 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
79728 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
79729 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
79732 ** undefined so that we can quickly detect the shallow-copy error */
79733 pX->flags = MEM_Undefined;
79734 pX->pScopyFrom = 0;
79737 pMem->pScopyFrom = 0;
79743 ** pTo are freed. The pFrom->z field is not duplicated. If
79744 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
79754 assert( pTo->db==pFrom->db );
79757 if( (pFrom->flags&MEM_Static)==0 ){
79758 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
79760 pTo->flags |= srcType;
79774 pTo->flags &= ~MEM_Dyn;
79775 if( pTo->flags&(MEM_Str|MEM_Blob) ){
79776 if( 0==(pFrom->flags&MEM_Static) ){
79777 pTo->flags |= MEM_Ephem;
79792 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
79793 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
79794 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
79798 pFrom->flags = MEM_Null;
79799 pFrom->szMalloc = 0;
79822 ** non-negative for blobs.
79831 i64 nByte = n; /* New value for pMem->n */
79833 u16 flags; /* New value for pMem->flags */
79836 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
79846 if( pMem->db ){
79847 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
79868 sqlite3DbFree(pMem->db, (void*)z);
79874 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
79892 memcpy(pMem->z, z, nAlloc);
79895 pMem->z = (char *)z;
79897 pMem->zMalloc = pMem->z;
79898 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
79900 pMem->xDel = xDel;
79905 pMem->n = (int)(nByte & 0x7fffffff);
79906 pMem->flags = flags;
79907 pMem->enc = enc;
79926 ** pMem->zMalloc to hold the content from the btree, if possible. New
79927 ** pMem->zMalloc space will be allocated if necessary. The calling routine
79941 pMem->flags = MEM_Null;
79946 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
79948 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
79949 pMem->flags = MEM_Blob;
79950 pMem->n = (int)amt;
79971 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
79972 assert( pMem->z!=0 );
79975 pMem->flags = MEM_Blob|MEM_Ephem;
79976 pMem->n = (int)amt;
79987 ** to a zero-terminated version of that string.
79991 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
79994 assert( (pVal->flags & (MEM_Null))==0 );
79995 if( pVal->flags & (MEM_Blob|MEM_Str) ){
79997 pVal->flags |= MEM_Str;
79998 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
80001 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
80002 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
80007 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
80010 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
80012 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
80013 || pVal->db->mallocFailed );
80014 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
80016 return pVal->z;
80028 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
80034 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
80037 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
80039 return pVal->z;
80041 if( pVal->flags&MEM_Null ){
80053 p->flags = MEM_Null;
80054 p->db = db;
80075 ** Otherwise, if the second argument is non-zero, then this function is
80084 UnpackedRecord *pRec = p->ppRec[0];
80087 Index *pIdx = p->pIdx; /* Index being probed */
80090 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
80095 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
80096 if( pRec->pKeyInfo ){
80097 assert( pRec->pKeyInfo->nAllField==nCol );
80098 assert( pRec->pKeyInfo->enc==ENC(db) );
80099 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
80101 pRec->aMem[i].flags = MEM_Null;
80102 pRec->aMem[i].db = db;
80110 p->ppRec[0] = pRec;
80113 pRec->nField = p->iVal+1;
80114 return &pRec->aMem[p->iVal];
80161 assert( (p->flags & EP_TokenOnly)==0 );
80163 pList = p->x.pList;
80164 if( pList ) nVal = pList->nExpr;
80166 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
80168 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
80169 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
80181 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
80192 testcase( pCtx->pParse->rc==SQLITE_ERROR );
80193 testcase( pCtx->pParse->rc==SQLITE_OK );
80198 pFunc->xSFunc(&ctx, nVal, apVal);
80201 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
80208 pCtx->pParse->nErr++;
80211 pCtx->pParse->rc = rc;
80257 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
80258 if( op==TK_REGISTER ) op = pExpr->op2;
80264 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
80269 aff = sqlite3AffinityType(pExpr->u.zToken,0);
80270 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
80280 ** case when the value is -9223372036854775808.
80283 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
80284 pExpr = pExpr->pLeft;
80285 op = pExpr->op;
80286 negInt = -1;
80287 zNeg = "-";
80294 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
80296 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
80298 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
80305 assert( (pVal->flags & MEM_IntReal)==0 );
80306 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
80307 testcase( pVal->flags & MEM_Int );
80308 testcase( pVal->flags & MEM_Real );
80309 pVal->flags &= ~MEM_Str;
80315 /* This branch happens for multiple negative signs. Ex: -(-5) */
80316 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
80320 if( pVal->flags & MEM_Real ){
80321 pVal->u.r = -pVal->u.r;
80322 }else if( pVal->u.i==SMALLEST_INT64 ){
80324 pVal->u.r = -(double)SMALLEST_INT64;
80326 pVal->u.r = LARGEST_INT64;
80330 pVal->u.i = -pVal->u.i;
80343 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
80344 assert( pExpr->u.zToken[1]=='\'' );
80347 zVal = &pExpr->u.zToken[2];
80348 nVal = sqlite3Strlen30(zVal)-1;
80363 pVal->flags = MEM_Int;
80364 pVal->u.i = pExpr->u.zToken[4]==0;
80373 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
80434 sqlite3 *db = pParse->db;
80439 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
80445 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
80447 int iBindVar = pExpr->iColumn;
80448 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
80449 if( (v = pParse->pReprepare)!=0 ){
80452 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
80454 pVal->db = pParse->db;
80461 assert( pVal==0 || pVal->db==db );
80512 if( pExpr==0 || pExpr->op!=TK_SELECT ){
80523 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
80555 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
80597 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
80598 pMem->enc = ENC(db);
80610 int nCol = pRec->pKeyInfo->nAllField;
80611 Mem *aMem = pRec->aMem;
80616 sqlite3KeyInfoUnref(pRec->pKeyInfo);
80641 sqlite3DbFreeNN(((Mem*)v)->db, v);
80650 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
80654 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
80655 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
80656 return p->n;
80658 if( (p->flags & MEM_Blob)!=0 ){
80659 if( p->flags & MEM_Zero ){
80660 return p->n + p->u.nZero;
80662 return p->n;
80665 if( p->flags & MEM_Null ) return 0;
80696 sqlite3 *db = pParse->db;
80700 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
80701 p->db = db;
80702 if( db->pVdbe ){
80703 db->pVdbe->pPrev = p;
80705 p->pNext = db->pVdbe;
80706 p->pPrev = 0;
80707 db->pVdbe = p;
80708 assert( p->eVdbeState==VDBE_INIT_STATE );
80709 p->pParse = pParse;
80710 pParse->pVdbe = p;
80711 assert( pParse->aLabel==0 );
80712 assert( pParse->nLabel==0 );
80713 assert( p->nOpAlloc==0 );
80714 assert( pParse->szOpAlloc==0 );
80723 return p->pParse;
80731 sqlite3DbFree(p->db, p->zErrMsg);
80733 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
80742 p->prepFlags = prepFlags;
80744 p->expmask = 0;
80746 assert( p->zSql==0 );
80747 p->zSql = sqlite3DbStrNDup(p->db, z, n);
80752 ** Add a new element to the Vdbe->pDblStr list.
80758 sizeof(*pStr)+n+1-sizeof(pStr->z));
80760 pStr->pNextStr = p->pDblStr;
80761 p->pDblStr = pStr;
80762 memcpy(pStr->z, z, n+1);
80770 ** zId of length nId is a double-quoted identifier. Check to see if
80775 const char *zId /* The double-quoted identifier, already dequoted */
80779 if( pVdbe->pDblStr==0 ) return 0;
80780 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
80781 if( strcmp(zId, pStr->z)==0 ) return 1;
80793 assert( pA->db==pB->db );
80797 pTmp = pA->pNext;
80798 pA->pNext = pB->pNext;
80799 pB->pNext = pTmp;
80800 pTmp = pA->pPrev;
80801 pA->pPrev = pB->pPrev;
80802 pB->pPrev = pTmp;
80803 zTmp = pA->zSql;
80804 pA->zSql = pB->zSql;
80805 pB->zSql = zTmp;
80807 zTmp = pA->zNormSql;
80808 pA->zNormSql = pB->zNormSql;
80809 pB->zNormSql = zTmp;
80811 pB->expmask = pA->expmask;
80812 pB->prepFlags = pA->prepFlags;
80813 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
80814 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
80822 ** If an out-of-memory error occurs while resizing the array, return
80829 Parse *p = v->pParse;
80831 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
80839 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
80840 : (sqlite3_int64)v->nOpAlloc+nOp);
80842 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
80848 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
80849 sqlite3OomFault(p->db);
80854 assert( nNew>=(v->nOpAlloc+nOp) );
80855 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
80857 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
80858 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
80859 v->aOp = pNew;
80899 assert( p->nOpAlloc<=p->nOp );
80901 assert( p->nOpAlloc>p->nOp );
80908 i = p->nOp;
80909 assert( p->eVdbeState==VDBE_INIT_STATE );
80911 if( p->nOpAlloc<=i ){
80914 assert( p->aOp!=0 );
80915 p->nOp++;
80916 pOp = &p->aOp[i];
80918 pOp->opcode = (u8)op;
80919 pOp->p5 = 0;
80920 pOp->p1 = p1;
80921 pOp->p2 = p2;
80922 pOp->p3 = p3;
80923 pOp->p4.p = 0;
80924 pOp->p4type = P4_NOTUSED;
80926 pOp->zComment = 0;
80929 if( p->db->flags & SQLITE_VdbeAddopTrace ){
80930 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
80931 test_addop_breakpoint(i, &p->aOp[i]);
80935 pOp->cycles = 0;
80936 pOp->cnt = 0;
80939 pOp->iSrcLine = 0;
81033 Vdbe *v = pParse->pVdbe;
81038 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
81039 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
81041 assert( pParse->db->mallocFailed );
81042 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
81045 pCtx->pOut = 0;
81046 pCtx->pFunc = (FuncDef*)pFunc;
81047 pCtx->pVdbe = 0;
81048 pCtx->isError = 0;
81049 pCtx->argc = nArg;
81050 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
81082 if( pParse->addrExplain==0 ) return 0;
81083 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
81084 return pOp->p2;
81108 if( pParse->explain==2 )
81116 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
81118 v = pParse->pVdbe;
81119 iThis = v->nOp;
81120 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
81122 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
81124 pParse->addrExplain = iThis;
81134 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
81150 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
81151 sqlite3MayAbort(p->pParse);
81166 if( p->db->mallocFailed==0 ){
81167 VdbeOp *pOp = &p->aOp[addr];
81168 pOp->p4type = P4_INT32;
81169 pOp->p4.i = p4;
81174 /* Insert the end of a co-routine
81180 ** co-routine has its own independent set of registers, because co-routines
81182 ** that could cause problems if two or more co-routines are using the same
81185 v->pParse->nTempReg = 0;
81186 v->pParse->nRangeReg = 0;
81198 ** always negative and P2 values are suppose to be non-negative.
81205 ** Parse.aLabel[x] Stores the address that the x-th label resolves
81207 ** labels stores -1, but that is not required.
81215 return --pParse->nLabel;
81224 int nNewSize = 10 - p->nLabel;
81225 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
81226 nNewSize*sizeof(p->aLabel[0]));
81227 if( p->aLabel==0 ){
81228 p->nLabelAlloc = 0;
81232 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
81234 p->nLabelAlloc = nNewSize;
81235 p->aLabel[j] = v->nOp;
81239 Parse *p = v->pParse;
81241 assert( v->eVdbeState==VDBE_INIT_STATE );
81242 assert( j<-p->nLabel );
81245 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81246 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
81249 if( p->nLabelAlloc + p->nLabel < 0 ){
81252 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
81253 p->aLabel[j] = v->nOp;
81269 for(i=1; ALWAYS(i<p->nOp); i++){
81270 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
81271 p->aOp[1].opcode = OP_Noop;
81281 ** in a Vdbe main program and each of the sub-programs (triggers) it may
81292 ** sqlite3DbFree(v->db, sIter.apSub);
81301 int iSub; /* 0 = main program, 1 = first sub-program etc. */
81304 Vdbe *v = p->v;
81309 if( p->iSub<=p->nSub ){
81311 if( p->iSub==0 ){
81312 aOp = v->aOp;
81313 nOp = v->nOp;
81315 aOp = p->apSub[p->iSub-1]->aOp;
81316 nOp = p->apSub[p->iSub-1]->nOp;
81318 assert( p->iAddr<nOp );
81320 pRet = &aOp[p->iAddr];
81321 p->iAddr++;
81322 if( p->iAddr==nOp ){
81323 p->iSub++;
81324 p->iAddr = 0;
81327 if( pRet->p4type==P4_SUBPROGRAM ){
81328 int nByte = (p->nSub+1)*sizeof(SubProgram*);
81330 for(j=0; j<p->nSub; j++){
81331 if( p->apSub[j]==pRet->p4.pProgram ) break;
81333 if( j==p->nSub ){
81334 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
81335 if( !p->apSub ){
81338 p->apSub[p->nSub++] = pRet->p4.pProgram;
81351 ** sub-programs contains any of the following:
81368 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
81384 int opcode = pOp->opcode;
81390 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
81395 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
81399 ** where a "DELETE FROM tbl" has a statement-journal but does not
81400 ** require one. This is not so bad - it is an inefficiency, not a bug. */
81401 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
81406 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
81411 sqlite3DbFree(v->db, sIter.apSub);
81418 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
81422 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
81431 || (pC->eCurType!=CURTYPE_SORTER
81432 && pC->eCurType!=CURTYPE_PSEUDO
81433 && !pC->isEphemeral)
81435 p->nWrite++;
81446 assert( p->nWrite==0 || p->usesStmtJournal );
81474 Parse *pParse = p->pParse;
81475 int *aLabel = pParse->aLabel;
81476 p->readOnly = 1;
81477 p->bIsReader = 0;
81478 pOp = &p->aOp[p->nOp-1];
81487 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
81490 switch( pOp->opcode ){
81492 if( pOp->p2!=0 ) p->readOnly = 0;
81497 p->bIsReader = 1;
81505 p->readOnly = 0;
81506 p->bIsReader = 1;
81511 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
81516 assert( (pOp - p->aOp) >= 3 );
81517 assert( pOp[-1].opcode==OP_Integer );
81518 n = pOp[-1].p1;
81525 if( pOp->p2<0 ){
81527 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81528 ** have non-negative values for P2. */
81529 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
81530 assert( ADDR(pOp->p2)<-pParse->nLabel );
81531 pOp->p2 = aLabel[ADDR(pOp->p2)];
81537 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
81538 ** have non-negative values for P2. */
81539 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
81541 if( pOp==p->aOp ) break;
81542 pOp--;
81545 sqlite3DbFreeNN(p->db, pParse->aLabel);
81546 pParse->aLabel = 0;
81548 pParse->nLabel = 0;
81550 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
81557 ** that will cause the program to halt with an error message.
81575 Vdbe *v, /* The byte-code program under construction */
81585 pParse = v->pParse;
81587 if( pParse->nErr ) return;
81589 assert( iLast<v->nOp );
81590 pOp = &v->aOp[iFirst];
81592 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
81593 int iDest = pOp->p2; /* Jump destination */
81595 if( pOp->opcode==OP_Gosub ) continue;
81599 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
81602 iDest = pParse->aLabel[j];
81606 for(; j<v->nOp; j++){
81607 VdbeOp *pX = &v->aOp[j];
81608 if( pX->opcode==OP_Return ){
81609 if( pX->p1==iRetReg ) break;
81612 if( pX->opcode==OP_Noop ) continue;
81613 if( pX->opcode==OP_Explain ) continue;
81641 assert( p->eVdbeState==VDBE_INIT_STATE );
81642 return p->nOp;
81651 ** sqlite3VdbeAddOpList() will always be non-NULL.
81655 assert( p->nOp + N <= p->nOpAlloc );
81669 for(i=0; i<p->nOp; i++){
81670 assert( p->aOp[i].opcode!=OP_ResultRow );
81698 VdbeOp *aOp = p->aOp;
81699 assert( aOp && !p->db->mallocFailed );
81702 assert( DbMaskAllZero(p->btreeMask) );
81705 *pnOp = p->nOp;
81706 p->aOp = 0;
81714 ** Non-zero P2 arguments to jump instructions are automatically adjusted
81721 int iLineno /* Source-file line number of first opcode */
81726 assert( p->eVdbeState==VDBE_INIT_STATE );
81727 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
81730 pFirst = pOut = &p->aOp[p->nOp];
81732 pOut->opcode = aOp->opcode;
81733 pOut->p1 = aOp->p1;
81734 pOut->p2 = aOp->p2;
81735 assert( aOp->p2>=0 );
81736 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
81737 pOut->p2 += p->nOp;
81739 pOut->p3 = aOp->p3;
81740 pOut->p4type = P4_NOTUSED;
81741 pOut->p4.p = 0;
81742 pOut->p5 = 0;
81744 pOut->zComment = 0;
81747 pOut->iSrcLine = iLineno+i;
81752 if( p->db->flags & SQLITE_VdbeAddopTrace ){
81753 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
81757 p->nOp += nOp;
81773 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
81775 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
81777 ScanStatus *pNew = &aNew[p->nScan++];
81778 pNew->addrExplain = addrExplain;
81779 pNew->addrLoop = addrLoop;
81780 pNew->addrVisit = addrVisit;
81781 pNew->nEst = nEst;
81782 pNew->zName = sqlite3DbStrDup(p->db, zName);
81783 p->aScan = aNew;
81794 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
81797 sqlite3VdbeGetOp(p,addr)->p1 = val;
81800 sqlite3VdbeGetOp(p,addr)->p2 = val;
81803 sqlite3VdbeGetOp(p,addr)->p3 = val;
81806 assert( p->nOp>0 || p->db->mallocFailed );
81807 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
81815 sqlite3VdbeChangeP2(p, addr, p->nOp);
81821 ** the previous opcode (and is thus a no-op) then simply back up
81826 ** strives to omit useless byte-code like this:
81832 if( addr==p->nOp-1 ){
81833 assert( p->aOp[addr].opcode==OP_Once
81834 || p->aOp[addr].opcode==OP_If
81835 || p->aOp[addr].opcode==OP_FkIfZero );
81836 assert( p->aOp[addr].p4type==0 );
81838 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
81840 p->nOp--;
81842 sqlite3VdbeChangeP2(p, addr, p->nOp);
81852 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
81861 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
81865 freeEphemeralFunction(db, p->pFunc);
81883 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
81897 if( db->pnBytesFreed==0 ){
81905 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
81919 Op *pOp = &aOp[nOp-1];
81921 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
81923 sqlite3DbFree(db, pOp->zComment);
81926 pOp--;
81934 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
81938 p->pNext = pVdbe->pProgram;
81939 pVdbe->pProgram = p;
81946 return pVdbe->pProgram!=0;
81954 if( p->db->mallocFailed ) return 0;
81955 assert( addr>=0 && addr<p->nOp );
81956 pOp = &p->aOp[addr];
81957 freeP4(p->db, pOp->p4type, pOp->p4.p);
81958 pOp->p4type = P4_NOTUSED;
81959 pOp->p4.z = 0;
81960 pOp->opcode = OP_Noop;
81969 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
81970 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
81988 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
81989 assert( pParse->pVdbe );
81991 assert( iFirst+N-1<=pParse->nMem );
81996 N--;
81998 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
81999 mask &= ~MASKBIT32(N-1);
82000 N--;
82004 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
82005 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
82034 if( pOp->p4type ){
82035 freeP4(p->db, pOp->p4type, pOp->p4.p);
82036 pOp->p4type = 0;
82037 pOp->p4.p = 0;
82040 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
82043 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
82044 pOp->p4type = P4_DYNAMIC;
82051 db = p->db;
82052 assert( p->eVdbeState==VDBE_INIT_STATE );
82053 assert( p->aOp!=0 || db->mallocFailed );
82054 if( db->mallocFailed ){
82058 assert( p->nOp>0 );
82059 assert( addr<p->nOp );
82061 addr = p->nOp - 1;
82063 pOp = &p->aOp[addr];
82064 if( n>=0 || pOp->p4type ){
82071 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
82072 pOp->p4type = P4_INT32;
82075 pOp->p4.p = (void*)zP4;
82076 pOp->p4type = (signed char)n;
82083 ** to the value defined by the arguments. This is a high-speed
82094 if( p->db->mallocFailed ){
82095 freeP4(p->db, n, pP4);
82098 assert( p->nOp>0 );
82099 pOp = &p->aOp[p->nOp-1];
82100 assert( pOp->p4type==P4_NOTUSED );
82101 pOp->p4type = n;
82102 pOp->p4.p = pP4;
82111 Vdbe *v = pParse->pVdbe;
82122 ** insert a No-op and add the comment to that new instruction. This
82127 assert( p->nOp>0 || p->aOp==0 );
82128 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
82129 if( p->nOp ){
82130 assert( p->aOp );
82131 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
82132 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
82159 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
82164 ** Return the opcode for a given address. If the address is -1, then
82180 assert( p->eVdbeState==VDBE_INIT_STATE );
82182 addr = p->nOp - 1;
82184 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
82185 if( p->db->mallocFailed ){
82188 return &p->aOp[addr];
82198 if( c=='1' ) return pOp->p1;
82199 if( c=='2' ) return pOp->p2;
82200 if( c=='3' ) return pOp->p3;
82201 if( c=='4' ) return pOp->p4.i;
82202 return pOp->p5;
82213 ** "PX" -> "r[X]"
82214 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
82215 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
82216 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
82219 sqlite3 *db, /* Optional - Oom error reporting only */
82231 zOpName = sqlite3OpcodeName(pOp->opcode);
82247 if( pOp->zComment && pOp->zComment[0] ){
82248 sqlite3_str_appendall(&x, pOp->zComment);
82265 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
82268 sqlite3_context *pCtx = pOp->p4.pCtx;
82269 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
82271 }else if( pCtx->argc>1 ){
82272 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
82275 x.nChar -= 2;
82281 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
82290 if( !seenCom && pOp->zComment ){
82291 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
82293 }else if( pOp->zComment ){
82294 sqlite3_str_appendall(&x, pOp->zComment);
82310 switch( pExpr->op ){
82313 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
82316 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
82322 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
82326 if( pExpr->iColumn<0 ){
82329 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
82367 displayP4Expr(p, pExpr->pLeft);
82368 if( pExpr->pRight ){
82370 displayP4Expr(p, pExpr->pRight);
82388 switch( pOp->p4type ){
82391 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
82392 assert( pKeyInfo->aSortFlags!=0 );
82393 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
82394 for(j=0; j<pKeyInfo->nKeyField; j++){
82395 CollSeq *pColl = pKeyInfo->aColl[j];
82396 const char *zColl = pColl ? pColl->zName : "";
82399 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
82400 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
82408 displayP4Expr(&x, pOp->p4.pExpr);
82414 CollSeq *pColl = pOp->p4.pColl;
82415 assert( pColl->enc<4 );
82416 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
82417 encnames[pColl->enc]);
82421 FuncDef *pDef = pOp->p4.pFunc;
82422 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82426 FuncDef *pDef = pOp->p4.pCtx->pFunc;
82427 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
82431 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
82435 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
82439 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
82443 Mem *pMem = pOp->p4.pMem;
82444 if( pMem->flags & MEM_Str ){
82445 zP4 = pMem->z;
82446 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
82447 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
82448 }else if( pMem->flags & MEM_Real ){
82449 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
82450 }else if( pMem->flags & MEM_Null ){
82453 assert( pMem->flags & MEM_Blob );
82460 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
82467 u32 *ai = pOp->p4.ai;
82481 zP4 = pOp->p4.pTab->zName;
82485 zP4 = pOp->p4.z;
82497 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
82501 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
82502 ** p->btreeMask of databases that will require a lock.
82505 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
82506 assert( i<(int)sizeof(p->btreeMask)*8 );
82507 DbMaskSet(p->btreeMask, i);
82508 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
82509 DbMaskSet(p->lockMask, i);
82515 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
82519 ** that the correct busy-handler callback is invoked if required.
82521 ** If SQLite is not threadsafe but does support shared-cache mode, then
82526 ** If SQLite is not threadsafe and does not support shared-cache mode, this
82527 ** function is a no-op.
82529 ** The p->btreeMask field is a bitmask of all btrees that the prepared
82530 ** statement p will ever use. Let N be the number of bits in p->btreeMask
82540 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
82541 db = p->db;
82542 aDb = db->aDb;
82543 nDb = db->nDb;
82545 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
82561 db = p->db;
82562 aDb = db->aDb;
82563 nDb = db->nDb;
82565 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
82571 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
82584 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
82598 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
82599 zP4 ? zP4 : "", pOp->p5,
82612 ** This is a high-runner, so only those fields that really do need to
82627 p->flags = flags;
82628 p->db = db;
82629 p->szMalloc = 0;
82631 p->pScopyFrom = 0;
82634 }while( (--N)>0 );
82649 sqlite3 *db = p->db;
82650 if( db->pnBytesFreed ){
82652 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
82666 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
82667 ** sqlite3MemRelease() were called from here. With -O2, this jumps
82672 testcase( p->flags & MEM_Agg );
82673 testcase( p->flags & MEM_Dyn );
82674 if( p->flags&(MEM_Agg|MEM_Dyn) ){
82675 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
82677 p->flags = MEM_Undefined;
82678 }else if( p->szMalloc ){
82679 sqlite3DbFreeNN(db, p->zMalloc);
82680 p->szMalloc = 0;
82681 p->flags = MEM_Undefined;
82685 p->flags = MEM_Undefined;
82700 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
82716 pFrame->pParent = pFrame->v->pDelFrame;
82717 pFrame->v->pDelFrame = pFrame;
82737 int nSub = 0; /* Number of sub-vdbes seen so far */
82738 SubProgram **apSub = 0; /* Array of sub-vdbes */
82749 ** encountered, but p->pc will eventually catch up to nRow.
82751 nRow = p->nOp;
82753 if( pSub->flags&MEM_Blob ){
82756 nSub = pSub->n/sizeof(Vdbe*);
82757 apSub = (SubProgram **)pSub->z;
82760 nRow += apSub[i]->nOp;
82767 p->rc = SQLITE_OK;
82771 if( i<p->nOp ){
82774 aOp = p->aOp;
82779 i -= p->nOp;
82782 for(j=0; i>=apSub[j]->nOp; j++){
82783 i -= apSub[j]->nOp;
82784 assert( i<apSub[j]->nOp || j+1<nSub );
82786 aOp = apSub[j]->aOp;
82791 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
82801 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
82802 if( p->rc!=SQLITE_OK ){
82806 apSub = (SubProgram **)pSub->z;
82809 pSub->n = nSub*sizeof(SubProgram*);
82810 nRow += aOp[i].p4.pProgram->nOp;
82817 if( pOp->opcode==OP_OpenRead ) break;
82818 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
82819 if( pOp->opcode==OP_ReopenIdx ) break;
82843 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
82845 for(i=0; i<p->nChildCsr; i++){
82846 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
82848 releaseMemArray(aMem, p->nChildMem);
82849 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
82850 sqlite3DbFree(p->v->db, p);
82861 ** When p->explain==1, each instruction is listed. When
82862 ** p->explain==2, only OP_Explain instructions are listed and these
82863 ** are shown in a different format. p->explain==2 is used to implement
82865 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
82869 ** When p->explain==1, first the main program is listed, then each of
82876 sqlite3 *db = p->db; /* The database connection */
82879 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
82880 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
82884 assert( p->explain );
82885 assert( p->eVdbeState==VDBE_RUN_STATE );
82886 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
82890 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
82893 p->pResultSet = 0;
82895 if( p->rc==SQLITE_NOMEM ){
82907 assert( p->nMem>9 );
82908 pSub = &p->aMem[9];
82914 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
82918 if( AtomicLoad(&db->u1.isInterrupted) ){
82919 p->rc = SQLITE_INTERRUPT;
82921 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
82924 if( p->explain==2 ){
82925 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
82926 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
82927 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
82928 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
82929 p->nResColumn = 4;
82932 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
82933 -1, SQLITE_UTF8, SQLITE_STATIC);
82934 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
82935 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
82936 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
82938 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
82942 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
82947 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
82948 p->nResColumn = 8;
82950 p->pResultSet = pMem;
82951 if( db->mallocFailed ){
82952 p->rc = SQLITE_NOMEM;
82955 p->rc = SQLITE_OK;
82970 if( p->zSql ){
82971 z = p->zSql;
82972 }else if( p->nOp>=1 ){
82973 const VdbeOp *pOp = &p->aOp[0];
82974 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
82975 z = pOp->p4.z;
82985 ** Print an IOTRACE message showing SQL content.
82988 int nOp = p->nOp;
82992 pOp = &p->aOp[0];
82993 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
82996 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
83000 if( z[i-1]!=' ' ){
83023 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
83042 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
83045 if( nByte <= p->nFree ){
83046 p->nFree -= nByte;
83047 pBuf = &p->pSpace[p->nFree];
83049 p->nNeeded += nByte;
83065 assert( p->eVdbeState==VDBE_INIT_STATE
83066 || p->eVdbeState==VDBE_READY_STATE
83067 || p->eVdbeState==VDBE_HALT_STATE );
83071 assert( p->nOp>0 );
83073 p->eVdbeState = VDBE_READY_STATE;
83076 for(i=0; i<p->nMem; i++){
83077 assert( p->aMem[i].db==p->db );
83080 p->pc = -1;
83081 p->rc = SQLITE_OK;
83082 p->errorAction = OE_Abort;
83083 p->nChange = 0;
83084 p->cacheCtr = 1;
83085 p->minWriteFileFormat = 255;
83086 p->iStatement = 0;
83087 p->nFkConstraint = 0;
83089 for(i=0; i<p->nOp; i++){
83090 p->aOp[i].cnt = 0;
83091 p->aOp[i].cycles = 0;
83127 assert( p->nOp>0 );
83129 assert( p->eVdbeState==VDBE_INIT_STATE );
83130 assert( pParse==p->pParse );
83131 p->pVList = pParse->pVList;
83132 pParse->pVList = 0;
83133 db = p->db;
83134 assert( db->mallocFailed==0 );
83135 nVar = pParse->nVar;
83136 nMem = pParse->nMem;
83137 nCursor = pParse->nTab;
83138 nArg = pParse->nMaxArg;
83152 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
83153 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
83155 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
83160 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
83161 if( pParse->explain ){
83168 p->explain = pParse->explain;
83169 if( pParse->explain==2 ){
83179 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
83183 p->expired = 0;
83191 ** This two-pass approach that reuses as much memory as possible from
83196 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
83197 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
83198 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
83199 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
83201 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
83204 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
83206 if( !db->mallocFailed ){
83207 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
83208 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
83209 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
83210 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
83212 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
83217 if( db->mallocFailed ){
83218 p->nVar = 0;
83219 p->nCursor = 0;
83220 p->nMem = 0;
83222 p->nCursor = nCursor;
83223 p->nVar = (ynVar)nVar;
83224 initMemArray(p->aVar, nVar, db, MEM_Null);
83225 p->nMem = nMem;
83226 initMemArray(p->aMem, nMem, db, MEM_Undefined);
83227 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
83229 memset(p->anExec, 0, p->nOp*sizeof(i64));
83243 switch( pCx->eCurType ){
83245 sqlite3VdbeSorterClose(p->db, pCx);
83249 assert( pCx->uc.pCursor!=0 );
83250 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
83255 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
83256 const sqlite3_module *pModule = pVCur->pVtab->pModule;
83257 assert( pVCur->pVtab->nRef>0 );
83258 pVCur->pVtab->nRef--;
83259 pModule->xClose(pVCur);
83271 for(i=0; i<p->nCursor; i++){
83272 VdbeCursor *pC = p->apCsr[i];
83275 p->apCsr[i] = 0;
83282 ** is used, for example, when a trigger sub-program is halted to restore
83286 Vdbe *v = pFrame->v;
83289 v->anExec = pFrame->anExec;
83291 v->aOp = pFrame->aOp;
83292 v->nOp = pFrame->nOp;
83293 v->aMem = pFrame->aMem;
83294 v->nMem = pFrame->nMem;
83295 v->apCsr = pFrame->apCsr;
83296 v->nCursor = pFrame->nCursor;
83297 v->db->lastRowid = pFrame->lastRowid;
83298 v->nChange = pFrame->nChange;
83299 v->db->nChange = pFrame->nDbChange;
83300 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
83301 v->pAuxData = pFrame->pAuxData;
83302 pFrame->pAuxData = 0;
83303 return pFrame->pc;
83315 if( p->pFrame ){
83317 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
83319 p->pFrame = 0;
83320 p->nFrame = 0;
83322 assert( p->nFrame==0 );
83324 releaseMemArray(p->aMem, p->nMem);
83325 while( p->pDelFrame ){
83326 VdbeFrame *pDel = p->pDelFrame;
83327 p->pDelFrame = pDel->pParent;
83332 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
83333 assert( p->pAuxData==0 );
83344 sqlite3 *db = p->db;
83346 if( p->nResColumn ){
83347 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
83348 sqlite3DbFree(db, p->aColName);
83351 p->nResColumn = (u16)nResColumn;
83352 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
83353 if( p->aColName==0 ) return;
83354 initMemArray(p->aColName, n, db, MEM_Null);
83376 assert( idx<p->nResColumn );
83378 if( p->db->mallocFailed ){
83382 assert( p->aColName!=0 );
83383 pColName = &(p->aColName[idx+var*p->nResColumn]);
83384 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
83385 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
83392 ** write-transaction spanning more than one database file, this routine
83393 ** takes care of the super-journal trickery.
83397 int nTrans = 0; /* Number of databases with an active write-transaction
83398 ** that are candidates for a two-phase commit using a
83399 ** super-journal */
83412 ** be done before determining whether a super-journal file is
83421 ** one database file has an open write transaction, a super-journal
83424 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83425 Btree *pBt = db->aDb[i].pBt;
83427 /* Whether or not a database might need a super-journal depends upon
83429 ** journal modes use a super-journal and which do not */
83442 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
83457 /* If there are any write-transactions at all, invoke the commit hook */
83458 if( needXcommit && db->xCommitCallback ){
83459 rc = db->xCommitCallback(db->pCommitArg);
83465 /* The simple case - no more than one database file (not counting the
83467 ** super-journal.
83471 ** that case we do not support atomic multi-file commits, so use the
83474 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
83477 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83478 Btree *pBt = db->aDb[i].pBt;
83489 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83490 Btree *pBt = db->aDb[i].pBt;
83500 /* The complex case - There is a multi-file write-transaction active.
83501 ** This requires a super-journal file to ensure the transaction is
83506 sqlite3_vfs *pVfs = db->pVfs;
83507 char *zSuper = 0; /* File-name for the super-journal */
83508 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
83515 /* Select a super-journal file name */
83533 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
83535 /* The antipenultimate character of the super-journal name must
83537 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
83542 /* Open the super-journal. */
83549 sqlite3DbFree(db, zSuper-4);
83554 ** super-journal file. If an error occurs at this point close
83555 ** and delete the super-journal file. All the individual journal files
83556 ** still have 'null' as the super-journal pointer, so they will roll
83559 for(i=0; i<db->nDb; i++){
83560 Btree *pBt = db->aDb[i].pBt;
83572 sqlite3DbFree(db, zSuper-4);
83578 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
83586 sqlite3DbFree(db, zSuper-4);
83591 ** sets the super-journal pointer in each individual journal. If
83592 ** an error occurs here, do not delete the super-journal file.
83596 ** super-journal file will be orphaned. But we cannot delete it,
83597 ** in case the super-journal file name was written into the journal
83600 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
83601 Btree *pBt = db->aDb[i].pBt;
83609 sqlite3DbFree(db, zSuper-4);
83613 /* Delete the super-journal file. This commits the transaction. After
83618 sqlite3DbFree(db, zSuper-4);
83633 for(i=0; i<db->nDb; i++){
83634 Btree *pBt = db->aDb[i].pBt;
83653 ** This is an internal self-check only - it is not an essential processing
83656 ** This is a no-op if NDEBUG is defined.
83664 p = db->pVdbe;
83668 if( p->readOnly==0 ) nWrite++;
83669 if( p->bIsReader ) nRead++;
83671 p = p->pNext;
83673 assert( cnt==db->nVdbeActive );
83674 assert( nWrite==db->nVdbeWrite );
83675 assert( nRead==db->nVdbeRead );
83682 ** If the Vdbe passed as the first argument opened a statement-transaction,
83692 sqlite3 *const db = p->db;
83695 const int iSavepoint = p->iStatement-1;
83698 assert( db->nStatement>0 );
83699 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
83701 for(i=0; i<db->nDb; i++){
83703 Btree *pBt = db->aDb[i].pBt;
83716 db->nStatement--;
83717 p->iStatement = 0;
83732 db->nDeferredCons = p->nStmtDefCons;
83733 db->nDeferredImmCons = p->nStmtDefImmCons;
83738 if( p->db->nStatement && p->iStatement ){
83753 ** and write an error message to it. Then return SQLITE_ERROR.
83757 sqlite3 *db = p->db;
83758 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
83759 || (!deferred && p->nFkConstraint>0)
83761 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
83762 p->errorAction = OE_Abort;
83764 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
83786 sqlite3 *db = p->db;
83804 assert( p->eVdbeState==VDBE_RUN_STATE );
83805 if( db->mallocFailed ){
83806 p->rc = SQLITE_NOMEM_BKPT;
83813 if( p->bIsReader ){
83814 int mrc; /* Primary error code from p->rc */
83822 if( p->rc ){
83823 mrc = p->rc & 0xff;
83832 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
83837 ** Even if the statement is read-only, it is important to perform
83839 ** occurred while writing to the journal, sub-journal or database
83844 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
83845 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
83853 db->autoCommit = 1;
83854 p->nChange = 0;
83860 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
83864 /* If the auto-commit flag is set and this is the only active writer
83871 && db->autoCommit
83872 && db->nVdbeWrite==(p->readOnly==0)
83874 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
83877 if( NEVER(p->readOnly) ){
83882 }else if( db->flags & SQLITE_CorruptRdOnly ){
83884 db->flags &= ~SQLITE_CorruptRdOnly;
83886 /* The auto-commit flag is true, the vdbe program was successful
83892 if( rc==SQLITE_BUSY && p->readOnly ){
83896 p->rc = rc;
83898 p->nChange = 0;
83900 db->nDeferredCons = 0;
83901 db->nDeferredImmCons = 0;
83902 db->flags &= ~(u64)SQLITE_DeferFKs;
83907 p->nChange = 0;
83909 db->nStatement = 0;
83911 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
83913 }else if( p->errorAction==OE_Abort ){
83918 db->autoCommit = 1;
83919 p->nChange = 0;
83923 /* If eStatementOp is non-zero, then a statement transaction needs to
83932 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
83933 p->rc = rc;
83934 sqlite3DbFree(db, p->zErrMsg);
83935 p->zErrMsg = 0;
83939 db->autoCommit = 1;
83940 p->nChange = 0;
83945 ** has been rolled back, update the database connection change-counter.
83947 if( p->changeCntOn ){
83949 sqlite3VdbeSetChanges(db, p->nChange);
83953 p->nChange = 0;
83961 db->nVdbeActive--;
83962 if( !p->readOnly ) db->nVdbeWrite--;
83963 if( p->bIsReader ) db->nVdbeRead--;
83964 assert( db->nVdbeActive>=db->nVdbeRead );
83965 assert( db->nVdbeRead>=db->nVdbeWrite );
83966 assert( db->nVdbeWrite>=0 );
83967 p->eVdbeState = VDBE_HALT_STATE;
83969 if( db->mallocFailed ){
83970 p->rc = SQLITE_NOMEM_BKPT;
83973 /* If the auto-commit flag is set to true, then any locks that were held
83975 ** to invoke any required unlock-notify callbacks.
83977 if( db->autoCommit ){
83981 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
83982 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
83988 ** in p->rc. This routine sets that result back to SQLITE_OK.
83991 p->rc = SQLITE_OK;
83995 ** Copy the error code and error message belonging to the VDBE passed
83999 ** This function does not clear the VDBE error code or message, just
84003 sqlite3 *db = p->db;
84004 int rc = p->rc;
84005 if( p->zErrMsg ){
84006 db->bBenignMalloc++;
84008 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
84009 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
84011 db->bBenignMalloc--;
84012 }else if( db->pErr ){
84013 sqlite3ValueSetNull(db->pErr);
84015 db->errCode = rc;
84016 db->errByteOffset = -1;
84026 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
84027 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
84028 assert( v->db->init.busy==0 );
84031 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
84033 sqlite3DbFree(v->db, zExpanded);
84058 db = p->db;
84064 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
84067 ** and error message from the VDBE into the main database structure. But
84071 if( p->pc>=0 ){
84073 if( db->pErr || p->zErrMsg ){
84076 db->errCode = p->rc;
84080 /* Reset register contents and reclaim error message memory.
84085 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
84086 if( p->aMem ){
84087 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
84090 if( p->zErrMsg ){
84091 sqlite3DbFree(db, p->zErrMsg);
84092 p->zErrMsg = 0;
84094 p->pResultSet = 0;
84096 p->nWrite = 0;
84105 fprintf(out, "---- ");
84106 for(i=0; i<p->nOp; i++){
84107 fprintf(out, "%02x", p->aOp[i].opcode);
84110 if( p->zSql ){
84112 fprintf(out, "-- ");
84113 for(i=0; (c = p->zSql[i])!=0; i++){
84114 if( pc=='\n' ) fprintf(out, "-- ");
84120 for(i=0; i<p->nOp; i++){
84123 p->aOp[i].cnt,
84124 p->aOp[i].cycles,
84125 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
84128 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
84134 return p->rc & db->errMask;
84139 ** the result code. Write any error message text into *pzErrMsg.
84146 if( p->eVdbeState>=VDBE_READY_STATE ){
84148 assert( (rc & p->db->errMask)==rc );
84174 || (pAux->iAuxOp==iOp
84175 && pAux->iAuxArg>=0
84176 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
84178 testcase( pAux->iAuxArg==31 );
84179 if( pAux->xDeleteAux ){
84180 pAux->xDeleteAux(pAux->pAux);
84182 *pp = pAux->pNextAux;
84185 pp= &pAux->pNextAux;
84200 assert( p->db==0 || p->db==db );
84201 if( p->aColName ){
84202 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
84203 sqlite3DbFreeNN(db, p->aColName);
84205 for(pSub=p->pProgram; pSub; pSub=pNext){
84206 pNext = pSub->pNext;
84207 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
84210 if( p->eVdbeState!=VDBE_INIT_STATE ){
84211 releaseMemArray(p->aVar, p->nVar);
84212 if( p->pVList ) sqlite3DbFreeNN(db, p->pVList);
84213 if( p->pFree ) sqlite3DbFreeNN(db, p->pFree);
84215 vdbeFreeOpArray(db, p->aOp, p->nOp);
84216 sqlite3DbFree(db, p->zSql);
84218 sqlite3DbFree(db, p->zNormSql);
84221 for(pThis=p->pDblStr; pThis; pThis=pNext){
84222 pNext = pThis->pNextStr;
84230 for(i=0; i<p->nScan; i++){
84231 sqlite3DbFree(db, p->aScan[i].zName);
84233 sqlite3DbFree(db, p->aScan);
84245 db = p->db;
84246 assert( sqlite3_mutex_held(db->mutex) );
84248 if( db->pnBytesFreed==0 ){
84249 if( p->pPrev ){
84250 p->pPrev->pNext = p->pNext;
84252 assert( db->pVdbe==p );
84253 db->pVdbe = p->pNext;
84255 if( p->pNext ){
84256 p->pNext->pPrev = p->pPrev;
84272 assert( p->deferredMoveto );
84273 assert( p->isTable );
84274 assert( p->eCurType==CURTYPE_BTREE );
84275 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
84281 p->deferredMoveto = 0;
84282 p->cacheStatus = CACHE_STALE;
84295 assert( p->eCurType==CURTYPE_BTREE );
84296 assert( p->uc.pCursor!=0 );
84297 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
84298 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
84299 p->cacheStatus = CACHE_STALE;
84300 if( isDifferentRow ) p->nullRow = 1;
84309 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
84310 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
84322 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
84327 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
84334 ** serial-type and data blob separately.
84339 ** -------------- --------------- ---------------
84351 ** N>=12 and even (N-12)/2 BLOB
84352 ** N>=13 and odd (N-13)/2 text
84360 ** Return the serial-type for the value stored in pMem.
84364 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
84365 ** opcode in the byte-code engine. But by moving this routine in-line, we
84371 int flags = pMem->flags;
84381 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
84382 i64 i = pMem->u.i;
84407 ** as an integer, then we might as well make it an 8-byte floating
84409 pMem->u.r = (double)pMem->u.i;
84410 pMem->flags &= ~MEM_IntReal;
84411 pMem->flags |= MEM_Real;
84420 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
84421 assert( pMem->n>=0 );
84422 n = (u32)pMem->n;
84424 n += pMem->u.nZero;
84452 ** Return the length of the data corresponding to the supplied serial-type.
84456 return (serial_type-12)/2;
84459 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
84469 ** If we are on an architecture with mixed-endian floating
84473 ** For most architectures, this is a no-op.
84475 ** (later): It is reported to me that the mixed-endian problem
84477 ** that early versions of GCC stored the two words of a 64-bit
84485 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
84489 ** (2007-08-30) Frank van Vugt has studied this problem closely
84492 ** emulation that uses only 32-bit mantissas instead of a full
84493 ** 48-bits as required by the IEEE standard. (This is the
84496 ** the necessary byte swapping is carried out using a 64-bit integer
84497 ** rather than a 64-bit float. Frank assures us that the code here
84520 ** big-endian integer. Return the equivalent native integer
84546 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
84547 ** twos-complement integer. */
84548 pMem->u.i = *(i64*)&x;
84549 pMem->flags = MEM_Int;
84550 testcase( pMem->u.i<0 );
84552 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
84557 ** defined that 64-bit floating point values really are mixed
84566 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
84568 memcpy(&pMem->u.r, &x, sizeof(x));
84569 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
84579 ** UPDATE no-change flag set */
84580 pMem->flags = MEM_Null|MEM_Zero;
84581 pMem->n = 0;
84582 pMem->u.nZero = 0;
84587 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
84588 pMem->flags = MEM_Null;
84592 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
84594 pMem->u.i = ONE_BYTE_INT(buf);
84595 pMem->flags = MEM_Int;
84596 testcase( pMem->u.i<0 );
84599 case 2: { /* 2-byte signed integer */
84600 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
84601 ** twos-complement integer. */
84602 pMem->u.i = TWO_BYTE_INT(buf);
84603 pMem->flags = MEM_Int;
84604 testcase( pMem->u.i<0 );
84607 case 3: { /* 3-byte signed integer */
84608 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
84609 ** twos-complement integer. */
84610 pMem->u.i = THREE_BYTE_INT(buf);
84611 pMem->flags = MEM_Int;
84612 testcase( pMem->u.i<0 );
84615 case 4: { /* 4-byte signed integer */
84616 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
84617 ** twos-complement integer. */
84618 pMem->u.i = FOUR_BYTE_INT(buf);
84620 /* Work around a sign-extension bug in the HP compiler for HP/UX */
84621 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
84623 pMem->flags = MEM_Int;
84624 testcase( pMem->u.i<0 );
84627 case 5: { /* 6-byte signed integer */
84628 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
84629 ** twos-complement integer. */
84630 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
84631 pMem->flags = MEM_Int;
84632 testcase( pMem->u.i<0 );
84635 case 6: /* 8-byte signed integer */
84644 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
84645 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
84646 pMem->u.i = serial_type-8;
84647 pMem->flags = MEM_Int;
84651 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
84653 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
84654 ** (N-13)/2 bytes in length. */
84656 pMem->z = (char *)buf;
84657 pMem->n = (serial_type-12)/2;
84658 pMem->flags = aFlag[serial_type&1];
84683 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
84684 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
84686 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
84687 assert( pKeyInfo->aSortFlags!=0 );
84688 p->pKeyInfo = pKeyInfo;
84689 p->nField = pKeyInfo->nKeyField + 1;
84694 ** Given the nKey-byte encoding of a record in pKey[], populate the
84709 Mem *pMem = p->aMem;
84711 p->default_rc = 0;
84720 pMem->enc = pKeyInfo->enc;
84721 pMem->db = pKeyInfo->db;
84722 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
84723 pMem->szMalloc = 0;
84724 pMem->z = 0;
84728 if( (++u)>=p->nField ) break;
84735 sqlite3VdbeMemSetNull(pMem-1);
84737 assert( u<=pKeyInfo->nKeyField + 1 );
84738 p->nField = u;
84767 pKeyInfo = pPKey2->pKeyInfo;
84768 if( pKeyInfo->db==0 ) return 1;
84769 mem1.enc = pKeyInfo->enc;
84770 mem1.db = pKeyInfo->db;
84786 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
84787 assert( pKeyInfo->aSortFlags!=0 );
84788 assert( pKeyInfo->nKeyField>0 );
84815 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
84816 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
84819 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
84820 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
84822 rc = -rc;
84824 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
84825 rc = -rc; /* Invert the result for DESC sort order. */
84830 }while( idx1<szHdr1 && i<pPKey2->nField );
84841 rc = pPKey2->default_rc;
84848 if( pKeyInfo->db->mallocFailed ) return 1;
84857 ** limit given by pKeyInfo->nAllField.
84859 ** If this constraint is not satisfied, it means that the high-speed
84883 assert( nField <= pKeyInfo->nAllField );
84893 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
84901 if( pMem1->enc==pColl->enc ){
84904 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
84910 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
84911 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
84914 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
84915 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
84920 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
84930 ** with MEM_Zero. Return true if it could be a zero-blob.
84947 int n1 = pB1->n;
84948 int n2 = pB2->n;
84950 /* It is possible to have a Blob value that has some non-zero content
84954 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
84955 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
84957 if( (pB1->flags|pB2->flags) & MEM_Zero ){
84958 if( pB1->flags & pB2->flags & MEM_Zero ){
84959 return pB1->u.nZero - pB2->u.nZero;
84960 }else if( pB1->flags & MEM_Zero ){
84961 if( !isAllZero(pB2->z, pB2->n) ) return -1;
84962 return pB1->u.nZero - n2;
84964 if( !isAllZero(pB1->z, pB1->n) ) return +1;
84965 return n1 - pB2->u.nZero;
84968 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
84970 return n1 - n2;
84974 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
84984 if( x<r ) return -1;
84990 if( r<-9223372036854775808.0 ) return +1;
84991 if( r>=9223372036854775808.0 ) return -1;
84993 if( i<y ) return -1;
84996 if( s<r ) return -1;
85015 f1 = pMem1->flags;
85016 f2 = pMem2->flags;
85024 return (f2&MEM_Null) - (f1&MEM_Null);
85036 if( pMem1->u.i < pMem2->u.i ) return -1;
85037 if( pMem1->u.i > pMem2->u.i ) return +1;
85041 if( pMem1->u.r < pMem2->u.r ) return -1;
85042 if( pMem1->u.r > pMem2->u.r ) return +1;
85049 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
85051 if( pMem1->u.i < pMem2->u.i ) return -1;
85052 if( pMem1->u.i > pMem2->u.i ) return +1;
85055 return -1;
85062 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
85064 return -1;
85078 return -1;
85081 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
85082 assert( pMem1->enc==SQLITE_UTF8 ||
85083 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
85089 assert( !pColl || pColl->xCmp );
85104 ** The first argument passed to this function is a serial-type that
85105 ** corresponds to an integer - all values between 1 and 9 inclusive
85141 return (serial_type - 8);
85153 ** If argument bSkip is non-zero, it is assumed that the caller has already
85157 ** fields that appear in both keys are equal, then pPKey2->default_rc is
85160 ** If database corruption is discovered, set pPKey2->errCode to
85162 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
85163 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
85175 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
85204 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85209 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
85211 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
85212 assert( pPKey2->pKeyInfo->nKeyField>0 );
85218 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
85219 testcase( pRhs->flags & MEM_Int );
85220 testcase( pRhs->flags & MEM_IntReal );
85226 rc = -1;
85229 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
85232 i64 rhs = pRhs->u.i;
85234 rc = -1;
85242 else if( pRhs->flags & MEM_Real ){
85251 rc = -1;
85255 if( mem1.u.r<pRhs->u.r ){
85256 rc = -1;
85257 }else if( mem1.u.r>pRhs->u.r ){
85261 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
85267 else if( pRhs->flags & MEM_Str ){
85271 rc = -1;
85275 mem1.n = (serial_type - 12) / 2;
85279 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
85281 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85283 }else if( pKeyInfo->aColl[i] ){
85284 mem1.enc = pKeyInfo->enc;
85285 mem1.db = pKeyInfo->db;
85289 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
85292 int nCmp = MIN(mem1.n, pRhs->n);
85293 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85294 if( rc==0 ) rc = mem1.n - pRhs->n;
85300 else if( pRhs->flags & MEM_Blob ){
85301 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
85305 rc = -1;
85307 int nStr = (serial_type - 12) / 2;
85311 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85313 }else if( pRhs->flags & MEM_Zero ){
85317 rc = nStr - pRhs->u.nZero;
85320 int nCmp = MIN(nStr, pRhs->n);
85321 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
85322 if( rc==0 ) rc = nStr - pRhs->n;
85334 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
85338 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
85340 rc = -rc;
85349 if( i==pPKey2->nField ) break;
85364 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
85365 || pPKey2->pKeyInfo->db->mallocFailed
85367 pPKey2->eqSeen = 1;
85368 return pPKey2->default_rc;
85381 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
85399 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85402 case 1: { /* 1-byte signed integer */
85407 case 2: { /* 2-byte signed integer */
85412 case 3: { /* 3-byte signed integer */
85417 case 4: { /* 4-byte signed integer */
85423 case 5: { /* 6-byte signed integer */
85428 case 6: { /* 8-byte signed integer */
85455 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
85456 v = pPKey2->u.i;
85458 res = pPKey2->r1;
85460 res = pPKey2->r2;
85461 }else if( pPKey2->nField>1 ){
85467 ** fields. Return pPKey2->default_rc in this case. */
85468 res = pPKey2->default_rc;
85469 pPKey2->eqSeen = 1;
85479 ** uses the collation sequence BINARY and (c) that the size-of-header varint
85490 assert( pPKey2->aMem[0].flags & MEM_Str );
85491 assert( pPKey2->aMem[0].n == pPKey2->n );
85492 assert( pPKey2->aMem[0].z == pPKey2->u.z );
85493 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
85503 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
85505 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
85511 nStr = (serial_type-12) / 2;
85513 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
85516 nCmp = MIN( pPKey2->n, nStr );
85517 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
85520 res = pPKey2->r2;
85522 res = pPKey2->r1;
85524 res = nStr - pPKey2->n;
85526 if( pPKey2->nField>1 ){
85529 res = pPKey2->default_rc;
85530 pPKey2->eqSeen = 1;
85533 res = pPKey2->r2;
85535 res = pPKey2->r1;
85542 || pPKey2->pKeyInfo->db->mallocFailed
85554 ** that the size-of-header varint that occurs at the start of each record
85566 if( p->pKeyInfo->nAllField<=13 ){
85567 int flags = p->aMem[0].flags;
85568 if( p->pKeyInfo->aSortFlags[0] ){
85569 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
85572 p->r1 = 1;
85573 p->r2 = -1;
85575 p->r1 = -1;
85576 p->r2 = 1;
85579 p->u.i = p->aMem[0].u.i;
85586 && p->pKeyInfo->aColl[0]==0
85589 p->u.z = p->aMem[0].z;
85590 p->n = p->aMem[0].n;
85615 ** than 2GiB are support - anything large must be database corruption.
85617 ** this code can safely assume that nCellKey is 32-bits
85640 /* The last field of the index should be an integer - the ROWID.
85642 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
85661 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
85696 assert( pC->eCurType==CURTYPE_BTREE );
85697 pCur = pC->uc.pCursor;
85721 assert( sqlite3_mutex_held(db->mutex) );
85722 db->nChange = nChange;
85723 db->nTotalChange += nChange;
85731 v->changeCntOn = 1;
85740 ** programs obsolete. Removing user-defined functions or collating
85754 for(p = db->pVdbe; p; p=p->pNext){
85755 p->expired = iCode+1;
85763 return v->db;
85770 return v->prepFlags;
85784 Mem *pMem = &v->aVar[iVar-1];
85785 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
85786 if( 0==(pMem->flags & MEM_Null) ){
85787 sqlite3_value *pRet = sqlite3ValueNew(v->db);
85800 ** to sqlite3_reoptimize() that re-preparing the statement may result
85805 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
85807 v->expmask |= 0x80000000;
85809 v->expmask |= ((u32)1 << (iVar-1));
85818 ** throw an error if it is given inputs that would make it non-deterministic.
85819 ** This routine is invoked by date/time functions that use non-deterministic
85825 if( pCtx->pVdbe==0 ) return 1;
85827 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
85828 if( pOp->opcode==OP_PureFunc ){
85831 if( pOp->p5 & NC_IsCheck ){
85833 }else if( pOp->p5 & NC_GenCol ){
85838 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
85839 pCtx->pFunc->zName, zContext);
85840 sqlite3_result_error(pCtx, zMsg, -1);
85849 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
85854 if( pVtab->zErrMsg ){
85855 sqlite3 *db = p->db;
85856 sqlite3DbFree(db, p->zErrMsg);
85857 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
85858 sqlite3_free(pVtab->zErrMsg);
85859 pVtab->zErrMsg = 0;
85868 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
85878 Mem *pMem = &p->aMem[i];
85879 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
85888 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
85894 Vdbe *v, /* Vdbe pre-update hook is invoked by */
85903 sqlite3 *db = v->db;
85906 const char *zTbl = pTab->zName;
85909 assert( db->pPreUpdate==0 );
85916 iKey2 = v->aMem[iReg].u.i;
85923 assert( pCsr->eCurType==CURTYPE_BTREE );
85924 assert( pCsr->nField==pTab->nCol
85925 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
85934 preupdate.keyinfo.nKeyField = pTab->nCol;
85941 db->pPreUpdate = &preupdate;
85942 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
85943 db->pPreUpdate = 0;
85949 for(i=0; i<pCsr->nField; i++){
85979 ** Return TRUE (non-zero) of the statement supplied as an argument needs
85988 return p==0 || p->expired;
85998 if( p->db==0 ){
86022 assert( p->startTime>0 );
86023 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
86024 assert( db->init.busy==0 );
86025 assert( p->zSql!=0 );
86026 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
86027 iElapse = (iNow - p->startTime)*1000000;
86029 if( db->xProfile ){
86030 db->xProfile(db->pProfileArg, p->zSql, iElapse);
86033 if( db->mTrace & SQLITE_TRACE_PROFILE ){
86034 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
86036 p->startTime = 0;
86043 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
86045 # define checkProfileCallback(DB,P) /*no-op*/
86060 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
86061 ** pointer is a harmless no-op. */
86065 sqlite3 *db = v->db;
86067 sqlite3_mutex_enter(db->mutex);
86090 sqlite3 *db = v->db;
86091 sqlite3_mutex_enter(db->mutex);
86095 assert( (rc & (db->errMask))==rc );
86097 sqlite3_mutex_leave(db->mutex);
86110 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
86113 for(i=0; i<p->nVar; i++){
86114 sqlite3VdbeMemRelease(&p->aVar[i]);
86115 p->aVar[i].flags = MEM_Null;
86117 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
86118 if( p->expmask ){
86119 p->expired = 1;
86132 if( p->flags & (MEM_Blob|MEM_Str) ){
86134 assert( p->flags==MEM_Null && p->z==0 );
86137 p->flags |= MEM_Blob;
86138 return p->n ? p->z : 0;
86160 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
86164 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
86167 && p->eSubtype=='p'
86168 && strcmp(p->u.zPType, zPType)==0
86170 return (void*)p->z;
86189 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
86190 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
86263 if( pVal->flags & MEM_Null ){
86265 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
86267 }else if( pVal->flags & MEM_Int ){
86269 }else if( pVal->flags & MEM_Str ){
86272 assert( eType == aType[pVal->flags&MEM_AffMask] );
86275 return aType[pVal->flags&MEM_AffMask];
86280 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
86285 return (pVal->flags&MEM_FromBind)!=0;
86297 pNew->flags &= ~MEM_Dyn;
86298 pNew->db = 0;
86299 if( pNew->flags&(MEM_Str|MEM_Blob) ){
86300 pNew->flags &= ~(MEM_Static|MEM_Dyn);
86301 pNew->flags |= MEM_Ephem;
86306 }else if( pNew->flags & MEM_Null ){
86308 pNew->flags &= ~(MEM_Term|MEM_Subtype);
86322 ** The following routines are used by user-defined functions to specify
86339 Mem *pOut = pCtx->pOut;
86352 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86380 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86389 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86398 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86399 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
86402 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86403 pCtx->isError = SQLITE_ERROR;
86404 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
86408 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86409 pCtx->isError = SQLITE_ERROR;
86410 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
86414 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86415 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
86418 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86419 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
86422 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86423 sqlite3VdbeMemSetNull(pCtx->pOut);
86431 Mem *pOut = pCtx->pOut;
86432 assert( sqlite3_mutex_held(pOut->db->mutex) );
86434 pOut->flags = MEM_Null;
86438 Mem *pOut = pCtx->pOut;
86439 assert( sqlite3_mutex_held(pOut->db->mutex) );
86440 pOut->eSubtype = eSubtype & 0xff;
86441 pOut->flags |= MEM_Subtype;
86449 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86459 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86475 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86484 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86493 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86498 Mem *pOut = pCtx->pOut;
86499 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86501 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
86510 Mem *pOut = pCtx->pOut;
86511 assert( sqlite3_mutex_held(pOut->db->mutex) );
86512 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
86517 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86520 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
86524 pCtx->isError = errCode ? errCode : -1;
86526 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
86528 if( pCtx->pOut->flags & MEM_Null ){
86529 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
86536 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86537 pCtx->isError = SQLITE_TOOBIG;
86538 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
86544 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86545 sqlite3VdbeMemSetNull(pCtx->pOut);
86546 pCtx->isError = SQLITE_NOMEM_BKPT;
86547 sqlite3OomFault(pCtx->pOut->db);
86553 ** test-control.
86556 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86557 if( pCtx->pOut->flags & MEM_Int ){
86558 pCtx->pOut->flags &= ~MEM_Int;
86559 pCtx->pOut->flags |= MEM_IntReal;
86573 for(i=0; i<db->nDb; i++){
86574 Btree *pBt = db->aDb[i].pBt;
86580 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
86581 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
86604 db = p->db;
86605 if( p->eVdbeState!=VDBE_RUN_STATE ){
86607 if( p->eVdbeState==VDBE_READY_STATE ){
86608 if( p->expired ){
86609 p->rc = SQLITE_SCHEMA;
86611 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
86613 ** error has occurred, then return the error code in p->rc to the
86626 if( db->nVdbeActive==0 ){
86627 AtomicStore(&db->u1.isInterrupted, 0);
86630 assert( db->nVdbeWrite>0 || db->autoCommit==0
86631 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
86635 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
86636 && !db->init.busy && p->zSql ){
86637 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
86639 assert( p->startTime==0 );
86643 db->nVdbeActive++;
86644 if( p->readOnly==0 ) db->nVdbeWrite++;
86645 if( p->bIsReader ) db->nVdbeRead++;
86646 p->pc = 0;
86647 p->eVdbeState = VDBE_RUN_STATE;
86650 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
86655 ** This "automatic-reset" change is not technically an incompatibility,
86661 ** returns, and those were broken by the automatic-reset change. As a
86662 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
86668 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
86676 assert( p->eVdbeState==VDBE_READY_STATE );
86682 p->rcApp = SQLITE_OK;
86685 if( p->explain ){
86690 db->nVdbeExec++;
86692 db->nVdbeExec--;
86696 assert( p->rc==SQLITE_OK );
86697 assert( db->mallocFailed==0 );
86698 db->errCode = SQLITE_ROW;
86706 if( rc==SQLITE_DONE && db->autoCommit ){
86707 assert( p->rc==SQLITE_OK );
86708 p->rc = doWalCallbacks(db);
86709 if( p->rc!=SQLITE_OK ){
86712 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
86714 ** error has occurred, then return the error code in p->rc to the
86721 db->errCode = rc;
86722 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
86723 p->rc = SQLITE_NOMEM_BKPT;
86724 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
86729 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
86733 return (rc&db->errMask);
86737 ** This is the top-level implementation of sqlite3_step(). Call
86750 db = v->db;
86751 sqlite3_mutex_enter(db->mutex);
86754 int savedPc = v->pc;
86758 ** The error message from the SQL compiler has already been loaded
86759 ** into the database handle. This block copies the error message
86762 ** finalized or reset the parser error message is available via
86765 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
86766 sqlite3DbFree(db, v->zErrMsg);
86767 if( !db->mallocFailed ){
86768 v->zErrMsg = sqlite3DbStrDup(db, zErr);
86769 v->rc = rc = sqlite3ApiExit(db, rc);
86771 v->zErrMsg = 0;
86772 v->rc = rc = SQLITE_NOMEM_BKPT;
86781 ** SQLITE_SCHEMA. tag-20220401a */
86782 v->minWriteFileFormat = 254;
86784 assert( v->expired==0 );
86786 sqlite3_mutex_leave(db->mutex);
86796 assert( p && p->pFunc );
86797 return p->pFunc->pUserData;
86804 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
86811 assert( p && p->pOut );
86812 return p->pOut->db;
86826 ** performance by substituting a NULL result, or some other light-weight
86831 return sqlite3_value_nochange(p->pOut);
86851 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
86854 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
86855 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
86856 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
86862 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
86863 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
86867 sqlite3_value *pOut = pRhs->pOut;
86870 pOut->enc = ENC(pOut->db);
86871 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
86908 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
86909 assert( p->pVdbe!=0 );
86912 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
86915 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
86923 ** its pMem->z element.
86926 Mem *pMem = p->pMem;
86927 assert( (pMem->flags & MEM_Agg)==0 );
86930 pMem->z = 0;
86933 pMem->flags = MEM_Agg;
86934 pMem->u.pDef = p->pFunc;
86935 if( pMem->z ){
86936 memset(pMem->z, 0, nByte);
86939 return (void*)pMem->z;
86948 assert( p && p->pFunc && p->pFunc->xFinalize );
86949 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
86951 if( (p->pMem->flags & MEM_Agg)==0 ){
86954 return (void*)p->pMem->z;
86960 ** the user-function defined by pCtx.
86962 ** The left-most argument is 0.
86971 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
86973 if( pCtx->pVdbe==0 ) return 0;
86975 assert( pCtx->pVdbe!=0 );
86977 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
86978 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
86979 return pAuxData->pAux;
86987 ** argument to the user-function defined by pCtx. Any previous value is
86990 ** The left-most argument is 0.
87003 Vdbe *pVdbe = pCtx->pVdbe;
87005 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
87012 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
87013 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
87018 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
87020 pAuxData->iAuxOp = pCtx->iOp;
87021 pAuxData->iAuxArg = iArg;
87022 pAuxData->pNextAux = pVdbe->pAuxData;
87023 pVdbe->pAuxData = pAuxData;
87024 if( pCtx->isError==0 ) pCtx->isError = -1;
87025 }else if( pAuxData->xDeleteAux ){
87026 pAuxData->xDeleteAux(pAuxData->pAux);
87029 pAuxData->pAux = pAux;
87030 pAuxData->xDeleteAux = xDelete;
87050 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
87051 return p->pMem->n;
87060 return pVm ? pVm->nResColumn : 0;
87069 if( pVm==0 || pVm->pResultSet==0 ) return 0;
87070 return pVm->nResColumn;
87079 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
87080 ** instead of an 8-byte one. This all works fine, except that when
87082 ** that a Mem structure is located on an 8-byte boundary. To prevent
87084 ** using gcc, we force nullMem to be 8-byte aligned using the magical
87122 assert( pVm->db );
87123 sqlite3_mutex_enter(pVm->db->mutex);
87124 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
87125 pOut = &pVm->pResultSet[i];
87127 sqlite3Error(pVm->db, SQLITE_RANGE);
87160 assert( p->db!=0 );
87161 assert( sqlite3_mutex_held(p->db->mutex) );
87162 p->rc = sqlite3ApiExit(p->db, p->rc);
87163 sqlite3_mutex_leave(p->db->mutex);
87213 if( pOut->flags&MEM_Static ){
87214 pOut->flags &= ~MEM_Static;
87215 pOut->flags |= MEM_Ephem;
87234 ** Convert the N-th element of pStmt->pColName[] into a string using
87267 db = p->db;
87272 sqlite3_mutex_enter(db->mutex);
87273 assert( db->mallocFailed==0 );
87276 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
87280 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
87285 if( db->mallocFailed ){
87289 sqlite3_mutex_leave(db->mutex);
87388 ** The error code stored in database p->db is overwritten with the return
87396 sqlite3_mutex_enter(p->db->mutex);
87397 if( p->eVdbeState!=VDBE_READY_STATE ){
87398 sqlite3Error(p->db, SQLITE_MISUSE);
87399 sqlite3_mutex_leave(p->db->mutex);
87401 "bind on a busy prepared statement: [%s]", p->zSql);
87404 if( i<1 || i>p->nVar ){
87405 sqlite3Error(p->db, SQLITE_RANGE);
87406 sqlite3_mutex_leave(p->db->mutex);
87409 i--;
87410 pVar = &p->aVar[i];
87412 pVar->flags = MEM_Null;
87413 p->db->errCode = SQLITE_OK;
87418 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
87424 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
87425 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
87426 p->expired = 1;
87449 pVar = &p->aVar[i-1];
87452 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
87455 sqlite3Error(p->db, rc);
87456 rc = sqlite3ApiExit(p->db, rc);
87459 sqlite3_mutex_leave(p->db->mutex);
87497 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
87498 sqlite3_mutex_leave(p->db->mutex);
87510 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
87511 sqlite3_mutex_leave(p->db->mutex);
87520 sqlite3_mutex_leave(p->db->mutex);
87535 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
87536 sqlite3_mutex_leave(p->db->mutex);
87578 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
87582 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
87584 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
87589 if( pValue->flags & MEM_Zero ){
87590 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
87592 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
87597 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
87598 pValue->enc);
87614 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87616 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
87618 sqlite3_mutex_leave(p->db->mutex);
87625 sqlite3_mutex_enter(p->db->mutex);
87626 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
87632 rc = sqlite3ApiExit(p->db, rc);
87633 sqlite3_mutex_leave(p->db->mutex);
87643 return p ? p->nVar : 0;
87650 ** The result is always UTF-8.
87655 return sqlite3VListNumToName(p->pVList, i);
87665 return sqlite3VListNameToNum(p->pVList, zName, nName);
87678 assert( pTo->db==pFrom->db );
87679 assert( pTo->nVar==pFrom->nVar );
87680 sqlite3_mutex_enter(pTo->db->mutex);
87681 for(i=0; i<pFrom->nVar; i++){
87682 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
87684 sqlite3_mutex_leave(pTo->db->mutex);
87704 if( pFrom->nVar!=pTo->nVar ){
87707 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
87708 if( pTo->expmask ){
87709 pTo->expired = 1;
87711 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
87712 if( pFrom->expmask ){
87713 pFrom->expired = 1;
87726 return pStmt ? ((Vdbe*)pStmt)->db : 0;
87734 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
87742 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
87750 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
87767 sqlite3_mutex_enter(pDb->mutex);
87769 pNext = (sqlite3_stmt*)pDb->pVdbe;
87771 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
87773 sqlite3_mutex_leave(pDb->mutex);
87785 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
87792 sqlite3 *db = pVdbe->db;
87793 sqlite3_mutex_enter(db->mutex);
87795 db->pnBytesFreed = (int*)&v;
87797 db->pnBytesFreed = 0;
87798 sqlite3_mutex_leave(db->mutex);
87800 v = pVdbe->aCounter[op];
87801 if( resetFlag ) pVdbe->aCounter[op] = 0;
87811 return p ? p->zSql : 0;
87831 sqlite3_mutex_enter(p->db->mutex);
87833 sqlite3_mutex_leave(p->db->mutex);
87846 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
87847 sqlite3_mutex_enter(p->db->mutex);
87848 p->zNormSql = sqlite3Normalize(p, p->zSql);
87849 sqlite3_mutex_leave(p->db->mutex);
87851 return p->zNormSql;
87870 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
87877 ** This function is called from within a pre-update callback to retrieve
87881 PreUpdate *p = db->pPreUpdate;
87886 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
87887 if( !p || p->op==SQLITE_INSERT ){
87891 if( p->pPk ){
87892 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87894 if( iIdx>=p->pCsr->nField || iIdx<0 ){
87900 if( p->pUnpacked==0 ){
87904 assert( p->pCsr->eCurType==CURTYPE_BTREE );
87905 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
87908 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
87910 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
87911 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
87917 p->aRecord = aRec;
87920 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
87921 if( iIdx==p->pTab->iPKey ){
87922 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
87923 }else if( iIdx>=p->pUnpacked->nField ){
87925 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
87926 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
87927 testcase( pMem->flags & MEM_Int );
87928 testcase( pMem->flags & MEM_IntReal );
87941 ** This function is called from within a pre-update callback to retrieve
87945 PreUpdate *p = db->pPreUpdate;
87946 return (p ? p->keyinfo.nKeyField : 0);
87952 ** This function is designed to be called from within a pre-update callback
87956 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
87957 ** top-level trigger etc.).
87963 PreUpdate *p = db->pPreUpdate;
87964 return (p ? p->v->nFrame : 0);
87970 ** This function is designed to be called from within a pre-update callback
87974 PreUpdate *p = db->pPreUpdate;
87975 return (p ? p->iBlobWrite : -1);
87981 ** This function is called from within a pre-update callback to retrieve
87985 PreUpdate *p = db->pPreUpdate;
87989 if( !p || p->op==SQLITE_DELETE ){
87993 if( p->pPk && p->op!=SQLITE_UPDATE ){
87994 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
87996 if( iIdx>=p->pCsr->nField || iIdx<0 ){
88001 if( p->op==SQLITE_INSERT ){
88002 /* For an INSERT, memory cell p->iNewReg contains the serialized record
88004 UnpackedRecord *pUnpack = p->pNewUnpacked;
88006 Mem *pData = &p->v->aMem[p->iNewReg];
88009 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
88014 p->pNewUnpacked = pUnpack;
88016 pMem = &pUnpack->aMem[iIdx];
88017 if( iIdx==p->pTab->iPKey ){
88018 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88019 }else if( iIdx>=pUnpack->nField ){
88023 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
88028 assert( p->op==SQLITE_UPDATE );
88029 if( !p->aNew ){
88030 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
88031 if( !p->aNew ){
88036 assert( iIdx>=0 && iIdx<p->pCsr->nField );
88037 pMem = &p->aNew[iIdx];
88038 if( pMem->flags==0 ){
88039 if( iIdx==p->pTab->iPKey ){
88040 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
88042 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
88067 if( idx<0 || idx>=p->nScan ) return 1;
88068 pScan = &p->aScan[idx];
88071 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
88075 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
88080 LogEst x = pScan->nEst;
88089 *(const char**)pOut = pScan->zName;
88093 if( pScan->addrExplain ){
88094 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
88101 if( pScan->addrExplain ){
88102 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
88104 *(int*)pOut = -1;
88120 memset(p->anExec, 0, p->nOp * sizeof(i64));
88141 ** The Vdbe parse-tree explainer is also found here.
88149 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
88174 ** This function returns a pointer to a nul-terminated string in memory
88178 ** then the returned string holds a copy of zRawSql with "-- " prepended
88184 ** with large (multi-megabyte) strings and blobs.
88194 ** parameter index is known, locate the value in p->aVar[]. Then render
88213 db = p->db;
88214 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
88215 if( db->nVdbeExec>1 ){
88219 sqlite3_str_append(&out, "-- ", 3);
88220 assert( (zRawSql - zStart) > 0 );
88221 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
88223 }else if( p->nVar==0 ){
88252 assert( idx>0 && idx<=p->nVar );
88253 pVar = &p->aVar[idx-1];
88254 if( pVar->flags & MEM_Null ){
88256 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
88257 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
88258 }else if( pVar->flags & MEM_Real ){
88259 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
88260 }else if( pVar->flags & MEM_Str ){
88267 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
88275 nOut = pVar->n;
88279 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
88282 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
88284 if( nOut<pVar->n ){
88285 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88291 }else if( pVar->flags & MEM_Zero ){
88292 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
88295 assert( pVar->flags & MEM_Blob );
88297 nOut = pVar->n;
88302 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
88306 if( nOut<pVar->n ){
88307 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
88396 ** use this information to make sure that the zero-blob functionality
88403 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
88404 sqlite3_max_blobsize = p->n;
88414 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
88416 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
88468 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
88469 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
88470 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
88472 ** In other words, if M is 2, then I is either 0 (for fall-through) or
88474 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
88485 ** always taken, the flags should be 0x05 since the fall-through and
88487 ** flags should be 0x06 since only the fall-through approach is allowed.
88490 ** interested in equal or not-equal. In other words, I==0 and I==2
88495 ** should be no-ops except for special builds used to measure test coverage.
88500 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
88504 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
88516 ** iSrcLine - the line number of the VdbeCoverage() macro, with
88518 ** I - Mask of bits 0x07 indicating which cases are are
88520 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
88525 ** M - Type of jump. Same as M argument above
88550 if( ((P)->flags&MEM_Ephem)!=0 \
88554 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
88581 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
88582 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
88584 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
88592 assert( iCur>=0 && iCur<p->nCursor );
88593 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
88594 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
88595 p->apCsr[iCur] = 0;
88600 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
88601 ** to hold cursors, it is faster to in-line the logic. */
88602 assert( pMem->flags==MEM_Undefined );
88603 assert( (pMem->flags & MEM_Dyn)==0 );
88604 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
88605 if( pMem->szMalloc<nByte ){
88606 if( pMem->szMalloc>0 ){
88607 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
88609 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
88610 if( pMem->zMalloc==0 ){
88611 pMem->szMalloc = 0;
88614 pMem->szMalloc = nByte;
88617 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
88619 pCx->eCurType = eCurType;
88620 pCx->nField = nField;
88621 pCx->aOffset = &pCx->aType[nField];
88623 pCx->uc.pCursor = (BtCursor*)
88624 &pMem->z[ROUND8P(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
88625 sqlite3BtreeCursorZero(pCx->uc.pCursor);
88642 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
88662 u8 enc = pRec->enc;
88664 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
88665 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
88667 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
88668 pRec->flags |= MEM_Int;
88670 pRec->u.r = rValue;
88671 pRec->flags |= MEM_Real;
88674 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
88677 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
88678 pRec->flags &= ~MEM_Str;
88688 ** floating-point representation if an integer representation
88698 ** No-op. pRec is unchanged.
88708 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
88709 if( (pRec->flags & MEM_Real)==0 ){
88710 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
88721 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
88722 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
88723 testcase( pRec->flags & MEM_Int );
88724 testcase( pRec->flags & MEM_Real );
88725 testcase( pRec->flags & MEM_IntReal );
88729 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
88764 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
88770 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
88771 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
88773 pMem->u.i = 0;
88776 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
88778 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
88779 pMem->u.i = ix;
88784 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
88785 pMem->u.i = ix;
88795 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
88796 ** But it does set pMem->u.r and pMem->u.i appropriately.
88799 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
88800 testcase( pMem->flags & MEM_Int );
88801 testcase( pMem->flags & MEM_Real );
88802 testcase( pMem->flags & MEM_IntReal );
88803 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
88805 if( pMem->flags & (MEM_Str|MEM_Blob) ){
88806 testcase( pMem->flags & MEM_Str );
88807 testcase( pMem->flags & MEM_Blob );
88819 int f = pMem->flags;
88837 for(i=0; i<25 && i<pMem->n; i++){
88838 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
88841 for(i=0; i<25 && i<pMem->n; i++){
88842 char z = pMem->z[i];
88847 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
88864 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
88865 for(j=0; j<25 && j<pMem->n; j++){
88866 c = pMem->z[j];
88869 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
88879 if( p->flags & MEM_Undefined ){
88881 }else if( p->flags & MEM_Null ){
88882 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
88883 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
88884 printf(" si:%lld", p->u.i);
88885 }else if( (p->flags & (MEM_IntReal))!=0 ){
88886 printf(" ir:%lld", p->u.i);
88887 }else if( p->flags & MEM_Int ){
88888 printf(" i:%lld", p->u.i);
88890 }else if( p->flags & MEM_Real ){
88891 printf(" r:%.17g", p->u.r);
88902 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
88907 if( p->pScopyFrom ){
88908 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
88927 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
88933 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
88943 ** high-performance timing routines.
88953 ** the number of non-transaction savepoints currently in the
88963 for(p=db->pSavepoint; p; p=p->pNext) n++;
88964 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
88970 ** Return the register of pOp->p2 after first preparing it to be
88975 pOut->flags = MEM_Int;
88980 assert( pOp->p2>0 );
88981 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
88982 pOut = &p->aMem[pOp->p2];
88984 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
88987 pOut->flags = MEM_Int;
88993 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
88994 ** with pOp->p3. Return the hash.
89000 assert( pOp->p4type==P4_INT32 );
89001 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
89003 if( p->flags & (MEM_Int|MEM_IntReal) ){
89004 h += p->u.i;
89005 }else if( p->flags & MEM_Real ){
89007 }else if( p->flags & (MEM_Str|MEM_Blob) ){
89008 h += p->n;
89009 if( p->flags & MEM_Zero ) h += p->u.nZero;
89026 return azTypes[sqlite3_value_type(pMem)-1];
89036 Op *aOp = p->aOp; /* Copy of p->aOp */
89045 sqlite3 *db = p->db; /* The database */
89053 Mem *aMem = p->aMem; /* Copy of p->aMem */
89063 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
89066 if( db->xProgress ){
89067 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
89068 assert( 0 < db->nProgressOps );
89069 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
89074 if( p->rc==SQLITE_NOMEM ){
89079 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
89080 testcase( p->rc!=SQLITE_OK );
89081 p->rc = SQLITE_OK;
89082 assert( p->bIsReader || p->readOnly!=0 );
89083 p->iCurrentTime = 0;
89084 assert( p->explain==0 );
89085 p->pResultSet = 0;
89086 db->busyHandler.nBusy = 0;
89087 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
89091 if( p->pc==0
89092 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
89097 if( p->db->flags & SQLITE_VdbeListing ){
89099 for(i=0; i<p->nOp; i++){
89103 if( p->db->flags & SQLITE_VdbeEQP ){
89104 for(i=0; i<p->nOp; i++){
89112 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
89116 for(pOp=&aOp[p->pc]; 1; pOp++){
89121 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
89127 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
89133 if( db->flags & SQLITE_VdbeTrace ){
89134 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
89135 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
89145 sqlite3_interrupt_count--;
89155 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
89157 assert( pOp->p1>0 );
89158 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
89159 assert( memIsValid(&aMem[pOp->p1]) );
89160 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
89161 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
89164 assert( pOp->p2>0 );
89165 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
89166 assert( memIsValid(&aMem[pOp->p2]) );
89167 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
89168 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
89171 assert( pOp->p3>0 );
89172 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89173 assert( memIsValid(&aMem[pOp->p3]) );
89174 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
89175 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
89178 assert( pOp->p2>0 );
89179 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
89180 memAboutToChange(p, &aMem[pOp->p2]);
89183 assert( pOp->p3>0 );
89184 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89185 memAboutToChange(p, &aMem[pOp->p3]);
89193 switch( pOp->opcode ){
89200 ** the switch statement will break with convention and be flush-left. Another
89238 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
89248 if( pOp->p5 ){
89249 assert( pOp->p2 < (int)(pOp - aOp) );
89250 assert( pOp->p2 > 1 );
89251 pOp = &aOp[pOp->p2 - 2];
89258 pOp = &aOp[pOp->p2 - 1];
89269 ** faster according to "valgrind --tool=cachegrind" */
89271 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
89276 ** If the progress callback returns non-zero, exit the virtual machine with
89279 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
89280 assert( db->nProgressOps!=0 );
89281 nProgressLimit += db->nProgressOps;
89282 if( db->xProgress(db->pProgressArg) ){
89299 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89300 pIn1 = &aMem[pOp->p1];
89303 pIn1->flags = MEM_Int;
89304 pIn1->u.i = (int)(pOp-aOp);
89305 REGISTER_TRACE(pOp->p1, pIn1);
89316 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
89323 ** P2 is not used by the byte-code engine. However, if P2 is positive
89328 ** value is a byte-code indentation hint. See tag-20220407a in
89332 pIn1 = &aMem[pOp->p1];
89333 if( pIn1->flags & MEM_Int ){
89334 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
89335 pOp = &aOp[pIn1->u.i];
89336 }else if( ALWAYS(pOp->p3) ){
89354 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89355 assert( pOp->p2>=0 && pOp->p2<p->nOp );
89356 assert( pOp->p3>=0 && pOp->p3<p->nOp );
89357 pOut = &aMem[pOp->p1];
89359 pOut->u.i = pOp->p3 - 1;
89360 pOut->flags = MEM_Int;
89361 if( pOp->p2==0 ) break;
89366 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
89367 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
89368 pOp = &aOp[pOp->p2 - 1];
89382 pIn1 = &aMem[pOp->p1];
89383 assert( pIn1->flags==MEM_Int );
89384 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
89385 pCaller = &aOp[pIn1->u.i];
89386 assert( pCaller->opcode==OP_Yield );
89387 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
89388 pOp = &aOp[pCaller->p2 - 1];
89389 pIn1->flags = MEM_Undefined;
89408 pIn1 = &aMem[pOp->p1];
89410 pIn1->flags = MEM_Int;
89411 pcDest = (int)pIn1->u.i;
89412 pIn1->u.i = (int)(pOp - aOp);
89413 REGISTER_TRACE(pOp->p1, pIn1);
89423 ** value in register P3 is not NULL, then this routine is a no-op.
89427 pIn3 = &aMem[pOp->p3];
89429 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
89431 if( (pIn3->flags & MEM_Null)==0 ) break;
89449 ** If P4 is not null then it is an error message string.
89471 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
89473 if( p->pFrame && pOp->p1==SQLITE_OK ){
89474 /* Halt the sub-program. Return control to the parent frame. */
89475 pFrame = p->pFrame;
89476 p->pFrame = pFrame->pParent;
89477 p->nFrame--;
89478 sqlite3VdbeSetChanges(db, p->nChange);
89480 if( pOp->p2==OE_Ignore ){
89481 /* Instruction pcx is the OP_Program that invoked the sub-program
89483 ** instruction is set to OE_Ignore, then the sub-program is throwing
89486 pcx = p->aOp[pcx].p2-1;
89488 aOp = p->aOp;
89489 aMem = p->aMem;
89493 p->rc = pOp->p1;
89494 p->errorAction = (u8)pOp->p2;
89495 assert( pOp->p5<=4 );
89496 if( p->rc ){
89497 if( pOp->p5 ){
89500 testcase( pOp->p5==1 );
89501 testcase( pOp->p5==2 );
89502 testcase( pOp->p5==3 );
89503 testcase( pOp->p5==4 );
89504 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
89505 if( pOp->p4.z ){
89506 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
89509 sqlite3VdbeError(p, "%s", pOp->p4.z);
89511 pcx = (int)(pOp - aOp);
89512 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
89517 p->rc = SQLITE_BUSY;
89519 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
89520 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
89521 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
89529 ** The 32-bit integer value P1 is written into register P2.
89533 pOut->u.i = pOp->p1;
89540 ** P4 is a pointer to a 64-bit integer value.
89545 assert( pOp->p4.pI64!=0 );
89546 pOut->u.i = *pOp->p4.pI64;
89554 ** P4 is a pointer to a 64-bit floating point value.
89559 pOut->flags = MEM_Real;
89560 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
89561 pOut->u.r = *pOp->p4.pReal;
89569 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
89575 assert( pOp->p4.z!=0 );
89577 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
89581 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
89585 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
89587 pOut->szMalloc = 0;
89588 pOut->flags |= MEM_Static;
89589 if( pOp->p4type==P4_DYNAMIC ){
89590 sqlite3DbFree(db, pOp->p4.z);
89592 pOp->p4type = P4_DYNAMIC;
89593 pOp->p4.z = pOut->z;
89594 pOp->p1 = pOut->n;
89597 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
89600 pOp->opcode = OP_String;
89619 assert( pOp->p4.z!=0 );
89621 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
89622 pOut->z = pOp->p4.z;
89623 pOut->n = pOp->p1;
89624 pOut->enc = encoding;
89627 if( pOp->p3>0 ){
89628 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89629 pIn3 = &aMem[pOp->p3];
89630 assert( pIn3->flags & MEM_Int );
89631 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
89640 ** Mark the beginning of a subroutine that can be entered in-line
89644 ** If the subroutine is entered in-line, then the OP_Return will simply
89650 ** a no-op that simply falls through to the next instruction (assuming that
89652 ** entered in-line, then the OP_Return will cause in-line execution to
89667 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
89676 cnt = pOp->p3-pOp->p2;
89677 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
89678 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
89679 pOut->n = 0;
89681 pOut->uTemp = 0;
89687 pOut->flags = nullFlag;
89688 pOut->n = 0;
89689 cnt--;
89703 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
89704 pOut = &aMem[pOp->p1];
89705 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
89714 ** a zero-filled blob that is P1 bytes long in P2.
89717 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
89719 if( pOp->p4.z==0 ){
89720 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
89723 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
89725 pOut->enc = encoding;
89741 assert( pOp->p1>0 && pOp->p1<=p->nVar );
89742 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
89743 pVar = &p->aVar[pOp->p1 - 1];
89747 pOut = &aMem[pOp->p2];
89750 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
89751 pOut->flags |= MEM_Static|MEM_FromBind;
89759 ** Move the P3 values in register P1..P1+P3-1 over into
89760 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
89762 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
89770 n = pOp->p3;
89771 p1 = pOp->p1;
89772 p2 = pOp->p2;
89779 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
89780 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
89785 pIn1->pScopyFrom = 0;
89787 for(i=1; i<p->nMem; i++){
89798 }while( --n );
89818 n = pOp->p3;
89819 pIn1 = &aMem[pOp->p1];
89820 pOut = &aMem[pOp->p2];
89826 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
89827 pOut->flags &= ~MEM_Subtype;
89830 pOut->pScopyFrom = 0;
89832 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
89833 if( (n--)==0 ) break;
89854 pIn1 = &aMem[pOp->p1];
89855 pOut = &aMem[pOp->p2];
89859 pOut->pScopyFrom = pIn1;
89860 pOut->mScopyFlags = pIn1->flags;
89874 pIn1 = &aMem[pOp->p1];
89875 assert( (pIn1->flags & MEM_Int)!=0 );
89876 pOut = &aMem[pOp->p2];
89877 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
89885 ** constraint violations, this is a no-op.
89902 ** The registers P1 through P1+P2-1 contain a single row of
89905 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
89909 assert( p->nResColumn==pOp->p2 );
89910 assert( pOp->p1>0 || CORRUPT_DB );
89911 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
89913 p->cacheCtr = (p->cacheCtr + 2)|1;
89914 p->pResultSet = &aMem[pOp->p1];
89917 Mem *pMem = p->pResultSet;
89919 for(i=0; i<pOp->p2; i++){
89921 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
89931 if( db->mallocFailed ) goto no_mem;
89932 if( db->mTrace & SQLITE_TRACE_ROW ){
89933 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
89935 p->pc = (int)(pOp - aOp) + 1;
89958 pIn1 = &aMem[pOp->p1];
89959 pIn2 = &aMem[pOp->p2];
89960 pOut = &aMem[pOp->p3];
89963 flags1 = pIn1->flags;
89965 testcase( pIn2->flags & MEM_Null );
89966 if( (flags1 | pIn2->flags) & MEM_Null ){
89972 flags1 = pIn1->flags & ~MEM_Str;
89975 flags1 = pIn1->flags & ~MEM_Str;
89977 flags2 = pIn2->flags;
89980 flags2 = pIn2->flags & ~MEM_Str;
89983 flags2 = pIn2->flags & ~MEM_Str;
89985 nByte = pIn1->n + pIn2->n;
89986 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
89994 memcpy(pOut->z, pIn2->z, pIn2->n);
89995 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
89996 pIn2->flags = flags2;
89998 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
89999 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
90000 pIn1->flags = flags1;
90002 pOut->z[nByte]=0;
90003 pOut->z[nByte+1] = 0;
90004 pOut->flags |= MEM_Term;
90005 pOut->n = (int)nByte;
90006 pOut->enc = encoding;
90027 ** Synopsis: r[P3]=r[P2]-r[P1]
90062 pIn1 = &aMem[pOp->p1];
90064 pIn2 = &aMem[pOp->p2];
90066 pOut = &aMem[pOp->p3];
90067 flags = pIn1->flags | pIn2->flags;
90069 iA = pIn1->u.i;
90070 iB = pIn2->u.i;
90071 switch( pOp->opcode ){
90077 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
90083 if( iA==-1 ) iA = 1;
90088 pOut->u.i = iB;
90096 switch( pOp->opcode ){
90098 case OP_Subtract: rB -= rA; break;
90110 if( iA==-1 ) iA = 1;
90116 pOut->u.i = rB;
90122 pOut->u.r = rB;
90137 ** be returned. This is used by the built-in min(), max() and nullif()
90146 ** publicly. Only built-in functions have access to this feature.
90149 assert( pOp->p4type==P4_COLLSEQ );
90150 if( pOp->p1 ){
90151 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
90159 ** Take the bit-wise AND of the values in register P1 and P2 and
90166 ** Take the bit-wise OR of the values in register P1 and P2 and
90195 pIn1 = &aMem[pOp->p1];
90196 pIn2 = &aMem[pOp->p2];
90197 pOut = &aMem[pOp->p3];
90198 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
90204 op = pOp->opcode;
90215 op = 2*OP_ShiftLeft + 1 - op;
90216 iB = iB>(-64) ? -iB : 64;
90220 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
90227 /* Sign-extend on a right shift of a negative number */
90228 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
90233 pOut->u.i = iA;
90247 pIn1 = &aMem[pOp->p1];
90250 pIn1->u.i += pOp->p2;
90262 pIn1 = &aMem[pOp->p1];
90263 if( (pIn1->flags & MEM_Int)==0 ){
90265 if( (pIn1->flags & MEM_Int)==0 ){
90267 if( pOp->p2==0 ){
90291 pIn1 = &aMem[pOp->p1];
90292 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
90293 testcase( pIn1->flags & MEM_Int );
90294 testcase( pIn1->flags & MEM_IntReal );
90296 REGISTER_TRACE(pOp->p1, pIn1);
90319 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
90320 testcase( pOp->p2==SQLITE_AFF_TEXT );
90321 testcase( pOp->p2==SQLITE_AFF_BLOB );
90322 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
90323 testcase( pOp->p2==SQLITE_AFF_INTEGER );
90324 testcase( pOp->p2==SQLITE_AFF_REAL );
90325 pIn1 = &aMem[pOp->p1];
90329 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
90332 REGISTER_TRACE(pOp->p1, pIn1);
90343 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90387 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
90437 u16 flags1; /* Copy of initial value of pIn1->flags */
90438 u16 flags3; /* Copy of initial value of pIn3->flags */
90440 pIn1 = &aMem[pOp->p1];
90441 pIn3 = &aMem[pOp->p3];
90442 flags1 = pIn1->flags;
90443 flags3 = pIn3->flags;
90445 assert( (pOp->p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_TEXT || CORRUPT_DB );
90447 if( pIn3->u.i > pIn1->u.i ){
90448 if( sqlite3aGTb[pOp->opcode] ){
90449 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90453 }else if( pIn3->u.i < pIn1->u.i ){
90454 if( sqlite3aLTb[pOp->opcode] ){
90455 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90458 iCompare = -1;
90460 if( sqlite3aEQb[pOp->opcode] ){
90461 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90466 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90471 if( pOp->p5 & SQLITE_NULLEQ ){
90477 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
90478 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
90484 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
90492 if( pOp->p5 & SQLITE_JUMPIFNULL ){
90499 /* Neither operand is NULL and we couldn't do the special high-speed
90500 ** integer comparison case. So do a general-case comparison. */
90501 affinity = pOp->p5 & SQLITE_AFF_MASK;
90506 testcase( flags3==pIn3->flags );
90507 flags3 = pIn3->flags;
90515 testcase( pIn1->flags & MEM_Int );
90516 testcase( pIn1->flags & MEM_Real );
90517 testcase( pIn1->flags & MEM_IntReal );
90519 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
90520 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
90524 testcase( pIn3->flags & MEM_Int );
90525 testcase( pIn3->flags & MEM_Real );
90526 testcase( pIn3->flags & MEM_IntReal );
90528 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
90529 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
90532 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
90533 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
90545 res2 = sqlite3aLTb[pOp->opcode];
90547 res2 = sqlite3aEQb[pOp->opcode];
90549 res2 = sqlite3aGTb[pOp->opcode];
90554 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
90555 pIn3->flags = flags3;
90556 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
90557 pIn1->flags = flags1;
90559 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
90581 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
90584 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
90608 assert( pOp->p4type==P4_INTARRAY );
90609 assert( pOp->p4.ai );
90616 ** Synopsis: r[P1@P3] <-> r[P2@P3]
90618 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
90619 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
90648 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
90652 assert( pOp[-1].opcode==OP_Permutation );
90653 assert( pOp[-1].p4type==P4_INTARRAY );
90654 aPermute = pOp[-1].p4.ai + 1;
90657 n = pOp->p3;
90658 pKeyInfo = pOp->p4.pKeyInfo;
90661 p1 = pOp->p1;
90662 p2 = pOp->p2;
90667 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
90668 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
90670 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
90671 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
90680 assert( i<pKeyInfo->nKeyField );
90681 pColl = pKeyInfo->aColl[i];
90682 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
90685 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
90688 iCompare = -iCompare;
90690 if( bRev ) iCompare = -iCompare;
90707 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
90709 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
90711 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
90713 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
90743 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
90744 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
90745 if( pOp->opcode==OP_And ){
90752 pOut = &aMem[pOp->p3];
90756 pOut->u.i = v1;
90783 assert( pOp->p4type==P4_INT32 );
90784 assert( pOp->p4.i==0 || pOp->p4.i==1 );
90785 assert( pOp->p3==0 || pOp->p3==1 );
90786 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
90787 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
90799 pIn1 = &aMem[pOp->p1];
90800 pOut = &aMem[pOp->p2];
90801 if( (pIn1->flags & MEM_Null)==0 ){
90813 ** ones-complement of the P1 value into register P2. If P1 holds
90817 pIn1 = &aMem[pOp->p1];
90818 pOut = &aMem[pOp->p2];
90820 if( (pIn1->flags & MEM_Null)==0 ){
90821 pOut->flags = MEM_Int;
90822 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
90830 ** encountered on each invocation of the byte-code program. Jump to P2
90833 ** Top-level programs determine first invocation by comparing the P1
90841 ** because the self-altering code trick does not work for recursive
90846 assert( p->aOp[0].opcode==OP_Init );
90847 if( p->pFrame ){
90848 iAddr = (int)(pOp - p->aOp);
90849 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
90853 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
90855 if( p->aOp[0].p1==pOp->p1 ){
90861 pOp->p1 = p->aOp[0].p1;
90868 ** is considered true if it is numeric and non-zero. If the value
90869 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
90873 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
90883 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
90887 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
90899 pIn1 = &aMem[pOp->p1];
90900 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
90901 if( (pIn1->flags & MEM_Null)!=0 ){
90916 pIn1 = &aMem[pOp->p1];
90917 doTheJump = (pIn1->flags & MEM_Null)!=0 || sqlite3_value_type(pIn1)==pOp->p3;
90931 if( (aMem[pOp->p1].flags & MEM_Null)!=0
90932 || (aMem[pOp->p3].flags & MEM_Null)!=0
90934 sqlite3VdbeMemSetNull(aMem + pOp->p2);
90936 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
90947 pIn1 = &aMem[pOp->p1];
90948 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
90949 if( (pIn1->flags & MEM_Null)==0 ){
90964 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90965 assert( p->apCsr[pOp->p1]!=0 );
90966 if( p->apCsr[pOp->p1]->nullRow ){
90967 sqlite3VdbeMemSetNull(aMem + pOp->p3);
90987 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
90991 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90992 pC = p->apCsr[pOp->p1];
90993 pOut = &p->aMem[pOp->p3];
90994 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
90997 if( pC->deferredMoveto ){
91001 if( sqlite3BtreeEof(pC->uc.pCursor) ){
91004 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
91016 ** information about the format of the data.) Extract the P2-th column
91034 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
91035 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
91043 u64 offset64; /* 64-bit offset */
91047 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91048 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91049 pC = p->apCsr[pOp->p1];
91050 p2 = (u32)pOp->p2;
91054 assert( p2<(u32)pC->nField
91055 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
91056 aOffset = pC->aOffset;
91057 assert( aOffset==pC->aType+pC->nField );
91058 assert( pC->eCurType!=CURTYPE_VTAB );
91059 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
91060 assert( pC->eCurType!=CURTYPE_SORTER );
91062 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
91063 if( pC->nullRow ){
91064 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
91065 /* For the special case of as pseudo-cursor, the seekResult field
91067 pReg = &aMem[pC->seekResult];
91068 assert( pReg->flags & MEM_Blob );
91070 pC->payloadSize = pC->szRow = pReg->n;
91071 pC->aRow = (u8*)pReg->z;
91073 pDest = &aMem[pOp->p3];
91079 pCrsr = pC->uc.pCursor;
91080 if( pC->deferredMoveto ){
91082 assert( !pC->isEphemeral );
91083 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
91084 pC = pC->pAltCursor;
91085 p2 = iMap - 1;
91095 assert( pC->eCurType==CURTYPE_BTREE );
91098 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
91099 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
91100 assert( pC->szRow<=pC->payloadSize );
91101 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
91103 pC->cacheStatus = p->cacheCtr;
91104 if( (aOffset[0] = pC->aRow[0])<0x80 ){
91105 pC->iHdrOffset = 1;
91107 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
91109 pC->nHdrParsed = 0;
91111 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
91112 /* pC->aRow does not have to hold the entire row, but it does at least
91113 ** need to cover the header of the record. If pC->aRow does not contain
91116 pC->aRow = 0;
91117 pC->szRow = 0;
91125 ** 3-byte type for each of the maximum of 32768 columns plus three
91128 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
91133 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
91145 zData = pC->aRow;
91146 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
91150 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
91157 ** parsed and valid information is in aOffset[] and pC->aType[].
91159 if( pC->nHdrParsed<=p2 ){
91161 ** to extract additional fields up through the p2+1-th field
91163 if( pC->iHdrOffset<aOffset[0] ){
91165 if( pC->aRow==0 ){
91167 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
91171 zData = pC->aRow;
91174 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
91176 i = pC->nHdrParsed;
91178 zHdr = zData + pC->iHdrOffset;
91182 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
91187 pC->aType[i] = t;
91198 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
91199 || (offset64 > pC->payloadSize)
91205 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91210 pC->nHdrParsed = i;
91211 pC->iHdrOffset = (u32)(zHdr - zData);
91212 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
91221 if( pC->nHdrParsed<=p2 ){
91222 pDest = &aMem[pOp->p3];
91224 if( pOp->p4type==P4_MEM ){
91225 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
91232 t = pC->aType[p2];
91235 /* Extract the content for the p2+1-th column. Control can only
91236 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
91239 assert( p2<pC->nHdrParsed );
91241 pDest = &aMem[pOp->p3];
91247 assert( t==pC->aType[p2] );
91248 if( pC->szRow>=aOffset[p2+1] ){
91250 ** page - where the content is not on an overflow page */
91251 zData = pC->aRow + aOffset[p2];
91256 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
91260 pDest->n = len = (t-12)/2;
91261 pDest->enc = encoding;
91262 if( pDest->szMalloc < len+2 ){
91263 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
91264 pDest->flags = MEM_Null;
91267 pDest->z = pDest->zMalloc;
91269 memcpy(pDest->z, zData, len);
91270 pDest->z[len] = 0;
91271 pDest->z[len+1] = 0;
91272 pDest->flags = aFlag[t&1];
91275 pDest->enc = encoding;
91277 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
91278 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
91296 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
91297 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
91299 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
91300 pDest->flags &= ~MEM_Ephem;
91306 REGISTER_TRACE(pOp->p3, pDest);
91311 pOp = &aOp[aOp[0].p3-1];
91331 ** is zero. When P3 is non-zero, no type checking occurs for
91338 ** <li> P2 should be the number of non-virtual columns in the
91350 assert( pOp->p4type==P4_TABLE );
91351 pTab = pOp->p4.pTab;
91352 assert( pTab->tabFlags & TF_Strict );
91353 assert( pTab->nNVCol==pOp->p2 );
91354 aCol = pTab->aCol;
91355 pIn1 = &aMem[pOp->p1];
91356 for(i=0; i<pTab->nCol; i++){
91359 if( pOp->p3 ){ pIn1++; continue; }
91361 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
91363 if( (pIn1->flags & MEM_Null)==0 ){
91366 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
91371 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
91375 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
91379 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
91380 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_IntReal );
91381 if( pIn1->flags & MEM_Int ){
91384 ** so that we keep the high-resolution integer value but know that
91386 testcase( pIn1->u.i==140737488355328LL );
91387 testcase( pIn1->u.i==140737488355327LL );
91388 testcase( pIn1->u.i==-140737488355328LL );
91389 testcase( pIn1->u.i==-140737488355329LL );
91390 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
91391 pIn1->flags |= MEM_IntReal;
91392 pIn1->flags &= ~MEM_Int;
91394 pIn1->u.r = (double)pIn1->u.i;
91395 pIn1->flags |= MEM_Real;
91396 pIn1->flags &= ~MEM_Int;
91398 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
91409 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91412 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
91417 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
91418 pTab->zName, aCol[i].zCnName);
91428 ** P4 is a string that is P2 characters long. The N-th character of the
91429 ** string indicates the column affinity that should be used for the N-th
91435 zAffinity = pOp->p4.z;
91437 assert( pOp->p2>0 );
91438 assert( zAffinity[pOp->p2]==0 );
91439 pIn1 = &aMem[pOp->p1];
91440 while( 1 /*exit-by-break*/ ){
91441 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
91444 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
91447 ** so that we keep the high-resolution integer value but know that
91449 testcase( pIn1->u.i==140737488355328LL );
91450 testcase( pIn1->u.i==140737488355327LL );
91451 testcase( pIn1->u.i==-140737488355328LL );
91452 testcase( pIn1->u.i==-140737488355329LL );
91453 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
91454 pIn1->flags |= MEM_IntReal;
91455 pIn1->flags &= ~MEM_Int;
91457 pIn1->u.r = (double)pIn1->u.i;
91458 pIn1->flags |= MEM_Real;
91459 pIn1->flags &= ~MEM_Int;
91462 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
91477 ** P4 may be a string that is P2 characters long. The N-th character of the
91478 ** string indicates the column affinity that should be used for the N-th
91487 ** compile-time option is enabled:
91490 ** of the right-most table that can be null-trimmed.
91494 ** accept no-change records with serial_type 10. This value is
91516 ** ------------------------------------------------------------------------
91517 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
91518 ** ------------------------------------------------------------------------
91525 ** hdr-size field is also a varint which is the offset from the beginning
91531 nField = pOp->p1;
91532 zAffinity = pOp->p4.z;
91533 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
91535 nField = pOp->p2;
91536 pLast = &pData0[nField-1];
91539 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
91540 pOut = &aMem[pOp->p3];
91550 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
91551 pRec->flags |= MEM_IntReal;
91552 pRec->flags &= ~(MEM_Int);
91554 REGISTER_TRACE((int)(pRec-aMem), pRec);
91564 ** have a non-NULL default value. Also, the record must be left with
91566 ** index of the right-most column with a non-NULL default value */
91567 if( pOp->p5 ){
91568 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
91569 pLast--;
91570 nField--;
91577 ** the Mem.uTemp field of each term should hold the serial-type that will
91581 ** --------------- ---------------
91583 ** 1 1-byte signed integer
91584 ** 2 2-byte signed integer
91585 ** 3 3-byte signed integer
91586 ** 4 4-byte signed integer
91587 ** 5 6-byte signed integer
91588 ** 6 8-byte signed integer
91604 if( pRec->flags & MEM_Null ){
91605 if( pRec->flags & MEM_Zero ){
91609 ** Give such values a special internal-use-only serial-type of 10
91613 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
91615 pRec->uTemp = 10;
91617 pRec->uTemp = 0;
91620 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
91622 i64 i = pRec->u.i;
91624 testcase( pRec->flags & MEM_Int );
91625 testcase( pRec->flags & MEM_IntReal );
91638 if( (i&1)==i && p->minWriteFileFormat>=4 ){
91639 pRec->uTemp = 8+(u32)uu;
91642 pRec->uTemp = 1;
91646 pRec->uTemp = 2;
91649 pRec->uTemp = 3;
91652 pRec->uTemp = 4;
91655 pRec->uTemp = 5;
91658 if( pRec->flags & MEM_IntReal ){
91660 ** as an integer, then we might as well make it an 8-byte floating
91662 pRec->u.r = (double)pRec->u.i;
91663 pRec->flags &= ~MEM_IntReal;
91664 pRec->flags |= MEM_Real;
91665 pRec->uTemp = 7;
91667 pRec->uTemp = 6;
91670 }else if( pRec->flags & MEM_Real ){
91673 pRec->uTemp = 7;
91675 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
91676 assert( pRec->n>=0 );
91677 len = (u32)pRec->n;
91678 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
91679 if( pRec->flags & MEM_Zero ){
91680 serial_type += pRec->u.nZero*2;
91683 len += pRec->u.nZero;
91685 nZero += pRec->u.nZero;
91690 pRec->uTemp = serial_type;
91693 pRec--;
91696 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
91714 ** the new record. The output register (pOp->p3) is not allowed to
91718 if( nByte+nZero<=pOut->szMalloc ){
91721 pOut->z = pOut->zMalloc;
91725 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
91732 pOut->n = (int)nByte;
91733 pOut->flags = MEM_Blob;
91735 pOut->u.nZero = nZero;
91736 pOut->flags |= MEM_Zero;
91739 zHdr = (u8 *)pOut->z;
91750 while( 1 /*exit-by-break*/ ){
91751 serial_type = pRec->uTemp;
91752 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
91754 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
91764 assert( sizeof(v)==sizeof(pRec->u.r) );
91765 memcpy(&v, &pRec->u.r, sizeof(v));
91768 v = pRec->u.i;
91772 while( 1 /*exit-by-break*/ ){
91773 zPayload[--i] = (u8)(v&0xFF);
91781 if( serial_type>=14 && pRec->n>0 ){
91782 assert( pRec->z!=0 );
91783 memcpy(zPayload, pRec->z, pRec->n);
91784 zPayload += pRec->n;
91788 if( pRec->n ){
91789 assert( pRec->z!=0 );
91790 memcpy(zPayload, pRec->z, pRec->n);
91791 zPayload += pRec->n;
91797 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
91798 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
91800 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
91801 REGISTER_TRACE(pOp->p3, pOut);
91812 ** every btree page of the table. But if P3 is non-zero, an estimate
91819 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
91820 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
91822 if( pOp->p3 ){
91830 pOut->u.i = nEntry;
91851 p1 = pOp->p1;
91852 zName = pOp->p4.z;
91857 assert( db->pSavepoint==0 || db->autoCommit==0 );
91859 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
91861 assert( p->bIsReader );
91864 if( db->nVdbeWrite>0 ){
91868 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
91877 ** that the db->aVTrans[] array is empty. */
91878 assert( db->autoCommit==0 || db->nVTrans==0 );
91880 db->nStatement+db->nSavepoint);
91887 pNew->zName = (char *)&pNew[1];
91888 memcpy(pNew->zName, zName, nName+1);
91892 if( db->autoCommit ){
91893 db->autoCommit = 0;
91894 db->isTransactionSavepoint = 1;
91896 db->nSavepoint++;
91900 pNew->pNext = db->pSavepoint;
91901 db->pSavepoint = pNew;
91902 pNew->nDeferredCons = db->nDeferredCons;
91903 pNew->nDeferredImmCons = db->nDeferredImmCons;
91913 pSavepoint = db->pSavepoint;
91914 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
91915 pSavepoint = pSavepoint->pNext
91922 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
91926 sqlite3VdbeError(p, "cannot release savepoint - "
91935 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
91940 db->autoCommit = 1;
91942 p->pc = (int)(pOp - aOp);
91943 db->autoCommit = 0;
91944 p->rc = rc = SQLITE_BUSY;
91947 rc = p->rc;
91949 db->autoCommit = 0;
91951 db->isTransactionSavepoint = 0;
91955 iSavepoint = db->nSavepoint - iSavepoint - 1;
91957 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
91958 for(ii=0; ii<db->nDb; ii++){
91959 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
91968 for(ii=0; ii<db->nDb; ii++){
91969 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
91977 db->mDbFlags |= DBFLAG_SchemaChange;
91984 while( db->pSavepoint!=pSavepoint ){
91985 pTmp = db->pSavepoint;
91986 db->pSavepoint = pTmp->pNext;
91988 db->nSavepoint--;
91996 assert( pSavepoint==db->pSavepoint );
91997 db->pSavepoint = pSavepoint->pNext;
92000 db->nSavepoint--;
92004 db->nDeferredCons = pSavepoint->nDeferredCons;
92005 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
92015 if( p->eVdbeState==VDBE_HALT_STATE ){
92024 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
92035 desiredAutoCommit = pOp->p1;
92036 iRollback = pOp->p2;
92039 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
92040 assert( p->bIsReader );
92042 if( desiredAutoCommit!=db->autoCommit ){
92046 db->autoCommit = 1;
92047 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
92051 sqlite3VdbeError(p, "cannot commit transaction - "
92058 db->autoCommit = (u8)desiredAutoCommit;
92061 p->pc = (int)(pOp - aOp);
92062 db->autoCommit = (u8)(1-desiredAutoCommit);
92063 p->rc = rc = SQLITE_BUSY;
92067 if( p->rc==SQLITE_OK ){
92076 (iRollback)?"cannot rollback - no transaction is active":
92077 "cannot commit - no transaction is active"));
92089 ** If P2 is non-zero, then a write-transaction is started, or if a
92090 ** read-transaction is already active, it is upgraded to a write-transaction.
92091 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
92099 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
92125 assert( p->bIsReader );
92126 assert( p->readOnly==0 || pOp->p2==0 );
92127 assert( pOp->p2>=0 && pOp->p2<=2 );
92128 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92129 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92131 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
92132 if( db->flags & SQLITE_QueryOnly ){
92142 pDb = &db->aDb[pOp->p1];
92143 pBt = pDb->pBt;
92146 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
92151 p->pc = (int)(pOp - aOp);
92152 p->rc = rc;
92158 if( p->usesStmtJournal
92159 && pOp->p2
92160 && (db->autoCommit==0 || db->nVdbeRead>1)
92163 if( p->iStatement==0 ){
92164 assert( db->nStatement>=0 && db->nSavepoint>=0 );
92165 db->nStatement++;
92166 p->iStatement = db->nSavepoint + db->nStatement;
92169 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
92171 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
92177 p->nStmtDefCons = db->nDeferredCons;
92178 p->nStmtDefImmCons = db->nDeferredImmCons;
92181 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
92183 && pOp->p5
92184 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
92187 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
92191 sqlite3DbFree(db, p->zErrMsg);
92192 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
92193 /* If the schema-cookie from the database file matches the cookie
92194 ** stored with the in-memory representation of the schema, do
92197 ** If virtual-tables are in use, this is not just an optimization.
92198 ** Often, v-tables store their data in other SQLite tables, which
92199 ** are queried from within xNext() and other v-table methods using
92200 ** prepared queries. If such a query is out-of-date, we do not want to
92202 ** v-table would have to be ready for the sqlite3_vtab structure itself
92204 ** a v-table method.
92206 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
92207 sqlite3ResetOneSchema(db, pOp->p1);
92209 p->expired = 1;
92215 p->changeCntOn = 0;
92229 ** There must be a read-lock on the database (either a transaction
92238 assert( p->bIsReader );
92239 iDb = pOp->p1;
92240 iCookie = pOp->p3;
92241 assert( pOp->p3<SQLITE_N_BTREE_META );
92242 assert( iDb>=0 && iDb<db->nDb );
92243 assert( db->aDb[iDb].pBt!=0 );
92244 assert( DbMaskTest(p->btreeMask, iDb) );
92246 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
92248 pOut->u.i = iMeta;
92263 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
92271 assert( pOp->p2<SQLITE_N_BTREE_META );
92272 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92273 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92274 assert( p->readOnly==0 );
92275 pDb = &db->aDb[pOp->p1];
92276 assert( pDb->pBt!=0 );
92277 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
92279 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
92280 if( pOp->p2==BTREE_SCHEMA_VERSION ){
92282 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
92283 db->mDbFlags |= DBFLAG_SchemaChange;
92284 sqlite3FkClearTriggerCache(db, pOp->p1);
92285 }else if( pOp->p2==BTREE_FILE_FORMAT ){
92287 pDb->pSchema->file_format = pOp->p3;
92289 if( pOp->p1==1 ){
92293 p->expired = 0;
92302 ** Open a read-only cursor for the database table whose root page is
92319 ** object, then table being opened must be an [index b-tree] where the
92321 ** sequence of that index b-tree. Otherwise, if P4 is an integer
92322 ** value, then the table being opened must be a [table b-tree] with a
92332 ** b-tree and if it is this opcode becomes a no-op. In other words,
92354 ** OPFLAG_P2ISREG bit is set in P5 - see below).
92358 ** object, then table being opened must be an [index b-tree] where the
92360 ** sequence of that index b-tree. Otherwise, if P4 is an integer
92361 ** value, then the table being opened must be a [table b-tree] with a
92393 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
92394 assert( pOp->p4type==P4_KEYINFO );
92395 pCur = p->apCsr[pOp->p1];
92396 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
92397 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
92398 assert( pCur->eCurType==CURTYPE_BTREE );
92399 sqlite3BtreeClearCursor(pCur->uc.pCursor);
92407 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
92408 assert( p->bIsReader );
92409 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
92410 || p->readOnly==0 );
92412 if( p->expired==1 ){
92419 p2 = (u32)pOp->p2;
92420 iDb = pOp->p3;
92421 assert( iDb>=0 && iDb<db->nDb );
92422 assert( DbMaskTest(p->btreeMask, iDb) );
92423 pDb = &db->aDb[iDb];
92424 pX = pDb->pBt;
92426 if( pOp->opcode==OP_OpenWrite ){
92428 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
92430 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
92431 p->minWriteFileFormat = pDb->pSchema->file_format;
92436 if( pOp->p5 & OPFLAG_P2ISREG ){
92438 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
92439 assert( pOp->opcode==OP_OpenWrite );
92442 assert( (pIn2->flags & MEM_Int)!=0 );
92444 p2 = (int)pIn2->u.i;
92451 if( pOp->p4type==P4_KEYINFO ){
92452 pKeyInfo = pOp->p4.pKeyInfo;
92453 assert( pKeyInfo->enc==ENC(db) );
92454 assert( pKeyInfo->db==db );
92455 nField = pKeyInfo->nAllField;
92456 }else if( pOp->p4type==P4_INT32 ){
92457 nField = pOp->p4.i;
92459 assert( pOp->p1>=0 );
92462 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
92464 pCur->iDb = iDb;
92465 pCur->nullRow = 1;
92466 pCur->isOrdered = 1;
92467 pCur->pgnoRoot = p2;
92469 pCur->wrFlag = wrFlag;
92471 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
92472 pCur->pKeyInfo = pKeyInfo;
92474 ** SQLite used to check if the root-page flags were sane at this point
92477 pCur->isTable = pOp->p4type!=P4_KEYINFO;
92482 testcase( pOp->p5 & OPFLAG_BULKCSR );
92483 testcase( pOp->p2 & OPFLAG_SEEKEQ );
92484 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
92485 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
92496 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
92502 pOrig = p->apCsr[pOp->p2];
92504 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
92506 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
92508 pCx->nullRow = 1;
92509 pCx->isEphemeral = 1;
92510 pCx->pKeyInfo = pOrig->pKeyInfo;
92511 pCx->isTable = pOrig->isTable;
92512 pCx->pgnoRoot = pOrig->pgnoRoot;
92513 pCx->isOrdered = pOrig->isOrdered;
92514 pCx->ub.pBtx = pOrig->ub.pBtx;
92515 pCx->noReuse = 1;
92516 pOrig->noReuse = 1;
92517 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
92518 pCx->pKeyInfo, pCx->uc.pCursor);
92532 ** the main database is read-only. The ephemeral
92549 ** can be used as zero-length data for OP_Insert. This is an optimization
92571 assert( pOp->p1>=0 );
92572 assert( pOp->p2>=0 );
92573 if( pOp->p3>0 ){
92576 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
92577 assert( pOp->opcode==OP_OpenEphemeral );
92578 assert( aMem[pOp->p3].flags & MEM_Null );
92579 aMem[pOp->p3].n = 0;
92580 aMem[pOp->p3].z = "";
92582 pCx = p->apCsr[pOp->p1];
92583 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
92587 assert( pCx->isEphemeral );
92588 pCx->seqCount = 0;
92589 pCx->cacheStatus = CACHE_STALE;
92590 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
92592 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
92594 pCx->isEphemeral = 1;
92595 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
92596 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
92599 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
92604 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
92606 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
92607 assert( pOp->p4type==P4_KEYINFO );
92608 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
92609 BTREE_BLOBKEY | pOp->p5);
92611 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
92612 assert( pKeyInfo->db==db );
92613 assert( pKeyInfo->enc==ENC(db) );
92614 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
92615 pKeyInfo, pCx->uc.pCursor);
92617 pCx->isTable = 0;
92619 pCx->pgnoRoot = SCHEMA_ROOT;
92620 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
92621 0, pCx->uc.pCursor);
92622 pCx->isTable = 1;
92625 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
92627 sqlite3BtreeClose(pCx->ub.pBtx);
92632 pCx->nullRow = 1;
92640 ** tables using an external merge-sort algorithm.
92642 ** If argument P3 is non-zero, then it indicates that the sorter may
92649 assert( pOp->p1>=0 );
92650 assert( pOp->p2>=0 );
92651 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
92653 pCx->pKeyInfo = pOp->p4.pKeyInfo;
92654 assert( pCx->pKeyInfo->db==db );
92655 assert( pCx->pKeyInfo->enc==ENC(db) );
92656 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
92670 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92671 pC = p->apCsr[pOp->p1];
92673 if( (pC->seqCount++)==0 ){
92687 ** A pseudo-table created by this opcode is used to hold a single
92690 ** is the only cursor opcode that works with a pseudo-table.
92693 ** the pseudo-table.
92698 assert( pOp->p1>=0 );
92699 assert( pOp->p3>=0 );
92700 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
92702 pCx->nullRow = 1;
92703 pCx->seekResult = pOp->p2;
92704 pCx->isTable = 1;
92705 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
92707 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
92709 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
92710 assert( pOp->p5==0 );
92717 ** currently open, this instruction is a no-op.
92720 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92721 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
92722 p->apCsr[pOp->p1] = 0;
92731 ** table or index for cursor P1 are used. P4 is a 64-bit integer
92734 ** by the cursor. The high-order bit is set if any column after
92739 pC = p->apCsr[pOp->p1];
92740 assert( pC->eCurType==CURTYPE_BTREE );
92741 pC->maskUsed = *(u64*)pOp->p4.pI64;
92749 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92776 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92794 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92812 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
92848 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92849 assert( pOp->p2!=0 );
92850 pC = p->apCsr[pOp->p1];
92852 assert( pC->eCurType==CURTYPE_BTREE );
92856 assert( pC->isOrdered );
92857 assert( pC->uc.pCursor!=0 );
92858 oc = pOp->opcode;
92860 pC->nullRow = 0;
92862 pC->seekOp = pOp->opcode;
92865 pC->deferredMoveto = 0;
92866 pC->cacheStatus = CACHE_STALE;
92867 if( pC->isTable ){
92870 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
92876 pIn3 = &aMem[pOp->p3];
92877 flags3 = pIn3->flags;
92882 newType = pIn3->flags; /* Record the type after applying numeric affinity */
92883 pIn3->flags = flags3; /* But convert the type back to its original */
92894 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
92899 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
92905 ** (x > 4.9) -> (x >= 5)
92906 ** (x <= 4.9) -> (x < 5)
92909 assert( OP_SeekGE==(OP_SeekGT-1) );
92910 assert( OP_SeekLT==(OP_SeekLE-1) );
92912 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
92924 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
92925 pC->movetoTarget = iKey; /* Used by OP_Delete */
92935 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
92937 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
92939 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
92940 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
92947 nField = pOp->p4.i;
92948 assert( pOp->p4type==P4_INT32 );
92950 r.pKeyInfo = pC->pKeyInfo;
92955 ** r.default_rc = -1;
92960 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
92961 assert( oc!=OP_SeekGT || r.default_rc==-1 );
92962 assert( oc!=OP_SeekLE || r.default_rc==-1 );
92966 r.aMem = &aMem[pOp->p3];
92971 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
92986 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
93002 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
93015 res = sqlite3BtreeEof(pC->uc.pCursor);
93019 assert( pOp->p2>0 );
93032 ** Synopsis: Scan-ahead up to P1 rows
93044 ** This opcode helps to optimize IN operators on a multi-column index
93047 ** of the b-tree instead. A correct answer is obtained if this opcode
93048 ** is omitted or is a no-op.
93055 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
93085 /* pOp->p2 points to the first instruction past the OP_IdxGT that
93087 assert( pOp->p2>=(int)(pOp-aOp)+2 );
93088 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
93089 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
93090 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
93091 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
93092 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
93094 assert( pOp->p1>0 );
93095 pC = p->apCsr[pOp[1].p1];
93097 assert( pC->eCurType==CURTYPE_BTREE );
93098 assert( !pC->isTable );
93099 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
93101 if( db->flags&SQLITE_VdbeTrace ){
93102 printf("... cursor not valid - fall through\n");
93107 nStep = pOp->p1;
93109 r.pKeyInfo = pC->pKeyInfo;
93129 if( db->flags&SQLITE_VdbeTrace ){
93130 printf("... %d steps and then skip\n", pOp->p1 - nStep);
93139 if( db->flags&SQLITE_VdbeTrace ){
93140 printf("... %d steps and then success\n", pOp->p1 - nStep);
93149 if( db->flags&SQLITE_VdbeTrace ){
93150 printf("... fall through after %d steps\n", pOp->p1);
93156 nStep--;
93157 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
93182 ** early, thus saving work. This is part of the IN-early-out optimization.
93184 ** P1 must be a valid b-tree cursor.
93188 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93189 pC = p->apCsr[pOp->p1];
93191 assert( pOp->p3>=pOp->p2 );
93192 if( pC->seekHit<pOp->p2 ){
93194 if( db->flags&SQLITE_VdbeTrace ){
93195 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
93198 pC->seekHit = pOp->p2;
93199 }else if( pC->seekHit>pOp->p3 ){
93201 if( db->flags&SQLITE_VdbeTrace ){
93202 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
93205 pC->seekHit = pOp->p3;
93216 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93217 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
93218 if( !p->apCsr[pOp->p1] ){
93279 ** This opcode is used in IN clause processing for a multi-column key.
93281 ** left-most element, and if there are no matches on the most recent
93300 ** record are not-NULL then a check is done to determine if any row in the
93316 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93317 pC = p->apCsr[pOp->p1];
93320 if( db->flags&SQLITE_VdbeTrace ){
93321 printf("seekHit is %d\n", pC->seekHit);
93324 if( pC->seekHit>=pOp->p4.i ) break;
93338 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
93341 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93342 assert( pOp->p4type==P4_INT32 );
93343 pC = p->apCsr[pOp->p1];
93346 pC->seekOp = pOp->opcode;
93348 r.aMem = &aMem[pOp->p3];
93349 assert( pC->eCurType==CURTYPE_BTREE );
93350 assert( pC->uc.pCursor!=0 );
93351 assert( pC->isTable==0 );
93352 r.nField = (u16)pOp->p4.i;
93355 r.pKeyInfo = pC->pKeyInfo;
93361 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
93364 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
93367 assert( r.aMem->flags & MEM_Blob );
93368 assert( pOp->opcode!=OP_NoConflict );
93372 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
93374 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
93375 pIdxKey->default_rc = 0;
93376 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
93382 alreadyExists = (pC->seekResult==0);
93383 pC->nullRow = 1-alreadyExists;
93384 pC->deferredMoveto = 0;
93385 pC->cacheStatus = CACHE_STALE;
93386 if( pOp->opcode==OP_Found ){
93394 if( pOp->opcode==OP_NoConflict ){
93406 if( pOp->opcode==OP_IfNoHope ){
93407 pC->seekHit = pOp->p4.i;
93429 ** (with arbitrary multi-value keys).
93448 ** P3 register to contain a non-integer value, in which case the jump is
93452 ** (with arbitrary multi-value keys).
93466 pIn3 = &aMem[pOp->p3];
93467 testcase( pIn3->flags & MEM_Int );
93468 testcase( pIn3->flags & MEM_IntReal );
93469 testcase( pIn3->flags & MEM_Real );
93470 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
93471 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
93472 /* If pIn3->u.i does not contain an integer, compute iKey as the
93486 pIn3 = &aMem[pOp->p3];
93487 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
93488 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93489 iKey = pIn3->u.i;
93491 pC = p->apCsr[pOp->p1];
93494 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
93496 assert( pC->isTable );
93497 assert( pC->eCurType==CURTYPE_BTREE );
93498 pCrsr = pC->uc.pCursor;
93503 pC->movetoTarget = iKey; /* Used by OP_Delete */
93504 pC->nullRow = 0;
93505 pC->cacheStatus = CACHE_STALE;
93506 pC->deferredMoveto = 0;
93508 pC->seekResult = res;
93511 if( pOp->p2==0 ){
93530 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93531 assert( p->apCsr[pOp->p1]!=0 );
93532 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
93534 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
93567 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93568 pC = p->apCsr[pOp->p1];
93570 assert( pC->isTable );
93571 assert( pC->eCurType==CURTYPE_BTREE );
93572 assert( pC->uc.pCursor!=0 );
93575 ** thing) is obtained in a two-step algorithm.
93587 assert( pC->isTable );
93599 if( !pC->useRandomRowid ){
93600 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
93605 v = 1; /* IMP: R-61914-48074 */
93607 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
93608 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93610 pC->useRandomRowid = 1;
93612 v++; /* IMP: R-29538-34987 */
93618 if( pOp->p3 ){
93620 assert( pOp->p3>0 );
93621 if( p->pFrame ){
93622 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93624 assert( pOp->p3<=pFrame->nMem );
93625 pMem = &pFrame->aMem[pOp->p3];
93628 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
93629 pMem = &aMem[pOp->p3];
93634 REGISTER_TRACE(pOp->p3, pMem);
93636 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
93637 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
93638 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
93641 if( v<pMem->u.i+1 ){
93642 v = pMem->u.i + 1;
93644 pMem->u.i = v;
93647 if( pC->useRandomRowid ){
93648 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
93652 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
93658 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
93664 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
93667 assert( v>0 ); /* EV: R-40812-03570 */
93669 pC->deferredMoveto = 0;
93670 pC->cacheStatus = CACHE_STALE;
93672 pOut->u.i = v;
93701 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
93704 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
93705 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
93718 const char *zDb; /* database name - used by the update hook */
93719 Table *pTab; /* Table structure - used by update and pre-update hooks */
93722 pData = &aMem[pOp->p2];
93723 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93725 pC = p->apCsr[pOp->p1];
93727 assert( pC->eCurType==CURTYPE_BTREE );
93728 assert( pC->deferredMoveto==0 );
93729 assert( pC->uc.pCursor!=0 );
93730 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
93731 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
93732 REGISTER_TRACE(pOp->p2, pData);
93735 pKey = &aMem[pOp->p3];
93736 assert( pKey->flags & MEM_Int );
93738 REGISTER_TRACE(pOp->p3, pKey);
93739 x.nKey = pKey->u.i;
93741 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
93742 assert( pC->iDb>=0 );
93743 zDb = db->aDb[pC->iDb].zDbSName;
93744 pTab = pOp->p4.pTab;
93745 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
93752 /* Invoke the pre-update hook, if any */
93754 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
93755 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
93757 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
93758 /* Prevent post-update hook from running in cases when it should not */
93762 if( pOp->p5 & OPFLAG_ISNOOP ) break;
93765 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
93766 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
93767 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
93768 x.pData = pData->z;
93769 x.nData = pData->n;
93770 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
93771 if( pData->flags & MEM_Zero ){
93772 x.nZero = pData->u.nZero;
93777 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
93778 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
93781 pC->deferredMoveto = 0;
93782 pC->cacheStatus = CACHE_STALE;
93784 /* Invoke the update-hook if required. */
93787 assert( db->xUpdateCallback!=0 );
93788 assert( pTab->aCol!=0 );
93789 db->xUpdateCallback(db->pUpdateArg,
93790 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
93791 zDb, pTab->zName, x.nKey);
93799 ** of table - intkey or index. This opcode is used as part of copying
93812 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
93813 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
93815 pDest = p->apCsr[pOp->p1];
93816 pSrc = p->apCsr[pOp->p2];
93817 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
93818 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
93830 ** the next Next instruction will be a no-op. As a result, in this case
93844 ** P1 must not be pseudo-table. It has to be a real table with
93848 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
93850 ** this case. Specifically, if one is configured, the pre-update hook is
93851 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
93864 opflags = pOp->p2;
93865 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93866 pC = p->apCsr[pOp->p1];
93868 assert( pC->eCurType==CURTYPE_BTREE );
93869 assert( pC->uc.pCursor!=0 );
93870 assert( pC->deferredMoveto==0 );
93874 if( pOp->p4type==P4_TABLE
93875 && HasRowid(pOp->p4.pTab)
93876 && pOp->p5==0
93877 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
93880 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
93882 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93883 assert( CORRUPT_DB || pC->movetoTarget==iKey );
93887 /* If the update-hook or pre-update-hook will be invoked, set zDb to
93892 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
93893 assert( pC->iDb>=0 );
93894 assert( pOp->p4.pTab!=0 );
93895 zDb = db->aDb[pC->iDb].zDbSName;
93896 pTab = pOp->p4.pTab;
93897 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
93898 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
93906 /* Invoke the pre-update-hook if required. */
93907 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
93908 if( db->xPreUpdateCallback && pTab ){
93911 || (aMem[pOp->p3].flags & MEM_Int)
93915 zDb, pTab, pC->movetoTarget,
93916 pOp->p3, -1
93923 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
93928 if( p->pFrame==0 ){
93929 if( pC->isEphemeral==0
93930 && (pOp->p5 & OPFLAG_AUXDELETE)==0
93931 && (pC->wrFlag & OPFLAG_FORDELETE)==0
93935 if( pOp->p2 & OPFLAG_NCHANGE ){
93936 nExtraDelete--;
93941 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
93942 pC->cacheStatus = CACHE_STALE;
93943 pC->seekResult = 0;
93946 /* Invoke the update-hook if required. */
93948 p->nChange++;
93949 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
93950 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
93951 pC->movetoTarget);
93952 assert( pC->iDb>=0 );
93966 sqlite3VdbeSetChanges(db, p->nChange);
93967 p->nChange = 0;
93991 pC = p->apCsr[pOp->p1];
93993 assert( pOp->p4type==P4_INT32 );
93994 pIn3 = &aMem[pOp->p3];
93995 nKeyCol = pOp->p4.i;
94011 ** a register that is the source for a pseudo-table cursor created using
94012 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
94019 pOut = &aMem[pOp->p2];
94020 pC = p->apCsr[pOp->p1];
94023 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
94024 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94026 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
94043 ** of a real table, not a pseudo-table.
94047 ** register will be invalidated as soon as the cursor moves - including
94065 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94066 pC = p->apCsr[pOp->p1];
94068 assert( pC->eCurType==CURTYPE_BTREE );
94070 assert( pC->nullRow==0 );
94071 assert( pC->uc.pCursor!=0 );
94072 pCrsr = pC->uc.pCursor;
94082 assert( pC->deferredMoveto==0 );
94086 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
94092 if( !pOp->p3 ) Deephemeralize(pOut);
94094 REGISTER_TRACE(pOp->p2, pOut);
94115 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94116 pC = p->apCsr[pOp->p1];
94118 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
94119 if( pC->nullRow ){
94120 pOut->flags = MEM_Null;
94122 }else if( pC->deferredMoveto ){
94123 v = pC->movetoTarget;
94125 }else if( pC->eCurType==CURTYPE_VTAB ){
94126 assert( pC->uc.pVCur!=0 );
94127 pVtab = pC->uc.pVCur->pVtab;
94128 pModule = pVtab->pModule;
94129 assert( pModule->xRowid );
94130 rc = pModule->xRowid(pC->uc.pVCur, &v);
94135 assert( pC->eCurType==CURTYPE_BTREE );
94136 assert( pC->uc.pCursor!=0 );
94139 if( pC->nullRow ){
94140 pOut->flags = MEM_Null;
94143 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
94145 pOut->u.i = v;
94156 ** pseudo-cursor that always returns NULL for every column.
94161 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94162 pC = p->apCsr[pOp->p1];
94165 ** pseudo-cursor that always gives null rows. */
94166 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
94168 pC->seekResult = 0;
94169 pC->isTable = 1;
94170 pC->noReuse = 1;
94171 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
94173 pC->nullRow = 1;
94174 pC->cacheStatus = CACHE_STALE;
94175 if( pC->eCurType==CURTYPE_BTREE ){
94176 assert( pC->uc.pCursor!=0 );
94177 sqlite3BtreeClearCursor(pC->uc.pCursor);
94180 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
94213 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94214 pC = p->apCsr[pOp->p1];
94216 assert( pC->eCurType==CURTYPE_BTREE );
94217 pCrsr = pC->uc.pCursor;
94221 pC->seekOp = pOp->opcode;
94223 if( pOp->opcode==OP_SeekEnd ){
94224 assert( pOp->p2==0 );
94225 pC->seekResult = -1;
94231 pC->nullRow = (u8)res;
94232 pC->deferredMoveto = 0;
94233 pC->cacheStatus = CACHE_STALE;
94235 if( pOp->p2>0 ){
94253 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94254 pC = p->apCsr[pOp->p1];
94256 pCrsr = pC->uc.pCursor;
94262 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
94295 sqlite3_search_count--;
94297 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
94318 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94319 assert( pOp->p5==0 );
94320 pC = p->apCsr[pOp->p1];
94322 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
94325 pC->seekOp = OP_Rewind;
94330 assert( pC->eCurType==CURTYPE_BTREE );
94331 pCrsr = pC->uc.pCursor;
94334 pC->deferredMoveto = 0;
94335 pC->cacheStatus = CACHE_STALE;
94338 pC->nullRow = (u8)res;
94339 assert( pOp->p2>0 && pOp->p2<p->nOp );
94356 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
94365 ** number P5-1 in the prepared statement is incremented.
94381 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
94390 ** number P5-1 in the prepared statement is incremented.
94402 pC = p->apCsr[pOp->p1];
94408 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94409 assert( pOp->p5<ArraySize(p->aCounter) );
94410 pC = p->apCsr[pOp->p1];
94412 assert( pC->deferredMoveto==0 );
94413 assert( pC->eCurType==CURTYPE_BTREE );
94414 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
94415 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
94416 || pC->seekOp==OP_NullRow);
94417 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
94421 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94422 assert( pOp->p5<ArraySize(p->aCounter) );
94423 pC = p->apCsr[pOp->p1];
94425 assert( pC->deferredMoveto==0 );
94426 assert( pC->eCurType==CURTYPE_BTREE );
94427 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
94428 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
94429 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
94430 || pC->seekOp==OP_IfNoHope);
94431 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
94434 pC->cacheStatus = CACHE_STALE;
94437 pC->nullRow = 0;
94438 p->aCounter[pOp->p5]++;
94446 pC->nullRow = 1;
94462 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
94482 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94483 pC = p->apCsr[pOp->p1];
94487 pIn2 = &aMem[pOp->p2];
94488 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
94489 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
94490 assert( pC->eCurType==CURTYPE_BTREE );
94491 assert( pC->isTable==0 );
94494 x.nKey = pIn2->n;
94495 x.pKey = pIn2->z;
94496 x.aMem = aMem + pOp->p3;
94497 x.nMem = (u16)pOp->p4.i;
94498 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
94499 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
94500 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
94502 assert( pC->deferredMoveto==0 );
94503 pC->cacheStatus = CACHE_STALE;
94518 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94519 pC = p->apCsr[pOp->p1];
94523 pIn2 = &aMem[pOp->p2];
94524 assert( pIn2->flags & MEM_Blob );
94525 assert( pC->isTable==0 );
94546 ** this (self-correcting and non-critical) error if in writable_schema mode.
94554 assert( pOp->p3>0 );
94555 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
94556 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94557 pC = p->apCsr[pOp->p1];
94559 assert( pC->eCurType==CURTYPE_BTREE );
94561 pCrsr = pC->uc.pCursor;
94563 r.pKeyInfo = pC->pKeyInfo;
94564 r.nField = (u16)pOp->p3;
94566 r.aMem = &aMem[pOp->p2];
94572 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
94576 assert( pC->deferredMoveto==0 );
94577 pC->cacheStatus = CACHE_STALE;
94578 pC->seekResult = 0;
94595 ** is non-zero, then reading column a(i)-1 from cursor P3 is
94616 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94617 pC = p->apCsr[pOp->p1];
94619 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
94620 assert( pC->uc.pCursor!=0 );
94621 assert( pC->isTable==0 || IsNullCursor(pC) );
94622 assert( pC->deferredMoveto==0 );
94623 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
94634 if( !pC->nullRow ){
94636 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
94640 if( pOp->opcode==OP_DeferredSeek ){
94641 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
94642 pTabCur = p->apCsr[pOp->p3];
94644 assert( pTabCur->eCurType==CURTYPE_BTREE );
94645 assert( pTabCur->uc.pCursor!=0 );
94646 assert( pTabCur->isTable );
94647 pTabCur->nullRow = 0;
94648 pTabCur->movetoTarget = rowid;
94649 pTabCur->deferredMoveto = 1;
94650 pTabCur->cacheStatus = CACHE_STALE;
94651 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
94652 assert( !pTabCur->isEphemeral );
94653 pTabCur->ub.aAltMap = pOp->p4.ai;
94654 assert( !pC->isEphemeral );
94655 pTabCur->pAltCursor = pC;
94658 pOut->u.i = rowid;
94661 assert( pOp->opcode==OP_IdxRowid );
94662 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
94671 ** already occurred, this instruction is a no-op.
94676 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94677 pC = p->apCsr[pOp->p1];
94678 if( pC->deferredMoveto ){
94737 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94738 pC = p->apCsr[pOp->p1];
94740 assert( pC->isOrdered );
94741 assert( pC->eCurType==CURTYPE_BTREE );
94742 assert( pC->uc.pCursor!=0);
94743 assert( pC->deferredMoveto==0 );
94744 assert( pOp->p4type==P4_INT32 );
94745 r.pKeyInfo = pC->pKeyInfo;
94746 r.nField = (u16)pOp->p4.i;
94747 if( pOp->opcode<OP_IdxLT ){
94748 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
94749 r.default_rc = -1;
94751 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
94754 r.aMem = &aMem[pOp->p3];
94760 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
94771 assert( pC->eCurType==CURTYPE_BTREE );
94772 pCur = pC->uc.pCursor;
94790 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
94791 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
94792 res = -res;
94794 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
94815 ** value of the root page that moved - its value before the move occurred -
94826 ** and non-autovacuum modes.
94835 assert( p->readOnly==0 );
94836 assert( pOp->p1>1 );
94838 pOut->flags = MEM_Null;
94839 if( db->nVdbeRead > db->nVDestroy+1 ){
94841 p->errorAction = OE_Abort;
94844 iDb = pOp->p3;
94845 assert( DbMaskTest(p->btreeMask, iDb) );
94847 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
94848 pOut->flags = MEM_Int;
94849 pOut->u.i = iMoved;
94853 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
94873 ** If the P3 value is non-zero, then the row change count is incremented
94885 assert( p->readOnly==0 );
94886 assert( DbMaskTest(p->btreeMask, pOp->p2) );
94887 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
94888 if( pOp->p3 ){
94889 p->nChange += nChange;
94890 if( pOp->p3>0 ){
94891 assert( memIsValid(&aMem[pOp->p3]) );
94892 memAboutToChange(p, &aMem[pOp->p3]);
94893 aMem[pOp->p3].u.i += nChange;
94911 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94912 pC = p->apCsr[pOp->p1];
94915 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
94917 assert( pC->eCurType==CURTYPE_BTREE );
94918 assert( pC->isEphemeral );
94919 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
94928 ** Allocate a new b-tree in the main database file if P1==0 or in the
94932 ** The root page number of the new b-tree is stored in register P2.
94941 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
94942 assert( pOp->p1>=0 && pOp->p1<db->nDb );
94943 assert( DbMaskTest(p->btreeMask, pOp->p1) );
94944 assert( p->readOnly==0 );
94945 pDb = &db->aDb[pOp->p1];
94946 assert( pDb->pBt!=0 );
94947 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
94949 pOut->u.i = pgno;
94959 db->nSqlExec++;
94960 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
94961 db->nSqlExec--;
94973 ** then runs the new virtual machine. It is thus a re-entrant opcode.
94986 for(iDb=0; iDb<db->nDb; iDb++){
94987 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
94991 iDb = pOp->p1;
94992 assert( iDb>=0 && iDb<db->nDb );
94994 || db->mallocFailed
94995 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
94998 if( pOp->p4.z==0 ){
94999 sqlite3SchemaClear(db->aDb[iDb].pSchema);
95000 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
95001 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
95002 db->mDbFlags |= DBFLAG_SchemaChange;
95003 p->expired = 0;
95010 initData.pzErrMsg = &p->zErrMsg;
95012 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
95015 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
95019 assert( db->init.busy==0 );
95020 db->init.busy = 1;
95023 assert( !db->mallocFailed );
95027 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
95033 db->init.busy = 0;
95054 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95055 rc = sqlite3AnalysisLoad(db, pOp->p1);
95063 ** Remove the internal (in-memory) data structures that describe
95071 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
95077 ** Remove the internal (in-memory) data structures that describe
95085 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
95091 ** Remove the internal (in-memory) data structures that describe
95099 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
95108 ** register P1 the text of an error message describing any problems.
95131 assert( p->bIsReader );
95132 nRoot = pOp->p2;
95133 aRoot = pOp->p4.ai;
95136 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95137 pnErr = &aMem[pOp->p3];
95138 assert( (pnErr->flags & MEM_Int)!=0 );
95139 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
95140 pIn1 = &aMem[pOp->p1];
95141 assert( pOp->p5<db->nDb );
95142 assert( DbMaskTest(p->btreeMask, pOp->p5) );
95143 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
95144 (int)pnErr->u.i+1, &nErr);
95151 pnErr->u.i -= nErr-1;
95152 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
95169 pIn1 = &aMem[pOp->p1];
95170 pIn2 = &aMem[pOp->p2];
95171 assert( (pIn2->flags & MEM_Int)!=0 );
95172 if( (pIn1->flags & MEM_Blob)==0 ){
95176 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
95191 pIn1 = &aMem[pOp->p1];
95192 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
95193 if( (pIn1->flags & MEM_Blob)==0
95194 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
95203 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
95211 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
95220 ** must have P4==0, the final set must have P4==-1, and for all other sets
95225 ** (b) when P4==-1 there is no need to insert the value, as it will
95235 pIn1 = &aMem[pOp->p1];
95236 pIn3 = &aMem[pOp->p3];
95237 iSet = pOp->p4.i;
95238 assert( pIn3->flags&MEM_Int );
95243 if( (pIn1->flags & MEM_Blob)==0 ){
95247 assert( pOp->p4type==P4_INT32 );
95248 assert( iSet==-1 || iSet>=0 );
95250 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
95255 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
95268 ** cell in an array of values used as arguments to the sub-program. P2
95269 ** contains the address to jump to if the sub-program throws an IGNORE
95272 ** memory required by the sub-vdbe at runtime.
95276 ** If P5 is non-zero, then recursive program invocation is enabled.
95279 int nMem; /* Number of memory registers for sub-program */
95280 int nByte; /* Bytes of runtime space required for sub-program */
95285 SubProgram *pProgram; /* Sub-program to execute */
95288 pProgram = pOp->p4.pProgram;
95289 pRt = &aMem[pOp->p3];
95290 assert( pProgram->nOp>0 );
95293 ** disabled for backwards compatibility (p5 is set if this sub-program
95303 if( pOp->p5 ){
95304 t = pProgram->token;
95305 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
95309 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
95319 if( (pRt->flags&MEM_Blob)==0 ){
95325 nMem = pProgram->nMem + pProgram->nCsr;
95327 if( pProgram->nCsr==0 ) nMem++;
95330 + pProgram->nCsr * sizeof(VdbeCursor*)
95331 + (pProgram->nOp + 7)/8;
95337 pRt->flags = MEM_Blob|MEM_Dyn;
95338 pRt->z = (char*)pFrame;
95339 pRt->n = nByte;
95340 pRt->xDel = sqlite3VdbeFrameMemDel;
95342 pFrame->v = p;
95343 pFrame->nChildMem = nMem;
95344 pFrame->nChildCsr = pProgram->nCsr;
95345 pFrame->pc = (int)(pOp - aOp);
95346 pFrame->aMem = p->aMem;
95347 pFrame->nMem = p->nMem;
95348 pFrame->apCsr = p->apCsr;
95349 pFrame->nCursor = p->nCursor;
95350 pFrame->aOp = p->aOp;
95351 pFrame->nOp = p->nOp;
95352 pFrame->token = pProgram->token;
95354 pFrame->anExec = p->anExec;
95357 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
95360 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
95362 pMem->flags = MEM_Undefined;
95363 pMem->db = db;
95366 pFrame = (VdbeFrame*)pRt->z;
95367 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
95368 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
95369 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
95370 assert( pProgram->nCsr==pFrame->nChildCsr );
95371 assert( (int)(pOp - aOp)==pFrame->pc );
95374 p->nFrame++;
95375 pFrame->pParent = p->pFrame;
95376 pFrame->lastRowid = db->lastRowid;
95377 pFrame->nChange = p->nChange;
95378 pFrame->nDbChange = p->db->nChange;
95379 assert( pFrame->pAuxData==0 );
95380 pFrame->pAuxData = p->pAuxData;
95381 p->pAuxData = 0;
95382 p->nChange = 0;
95383 p->pFrame = pFrame;
95384 p->aMem = aMem = VdbeFrameMem(pFrame);
95385 p->nMem = pFrame->nChildMem;
95386 p->nCursor = (u16)pFrame->nChildCsr;
95387 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
95388 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
95389 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
95390 p->aOp = aOp = pProgram->aOp;
95391 p->nOp = pProgram->nOp;
95393 p->anExec = 0;
95400 for(i=0; i<p->nMem; i++){
95401 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
95406 pOp = &aOp[-1];
95412 ** This opcode is only ever present in sub-programs called via the
95426 pFrame = p->pFrame;
95427 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
95439 ** If P1 is non-zero, the database constraint counter is incremented
95444 if( db->flags & SQLITE_DeferFKs ){
95445 db->nDeferredImmCons += pOp->p2;
95446 }else if( pOp->p1 ){
95447 db->nDeferredCons += pOp->p2;
95449 p->nFkConstraint += pOp->p2;
95457 ** This opcode tests if a foreign key constraint-counter is currently zero.
95461 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
95463 ** zero, the jump is taken if the statement constraint-counter is zero
95467 if( pOp->p1 ){
95468 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
95469 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95471 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
95472 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
95484 ** within a sub-program). Set the value of register P1 to the maximum of
95492 if( p->pFrame ){
95493 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
95494 pIn1 = &pFrame->aMem[pOp->p1];
95496 pIn1 = &aMem[pOp->p1];
95500 pIn2 = &aMem[pOp->p2];
95502 if( pIn1->u.i<pIn2->u.i){
95503 pIn1->u.i = pIn2->u.i;
95510 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
95520 pIn1 = &aMem[pOp->p1];
95521 assert( pIn1->flags&MEM_Int );
95522 VdbeBranchTaken( pIn1->u.i>0, 2);
95523 if( pIn1->u.i>0 ){
95524 pIn1->u.i -= pOp->p3;
95531 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
95544 ** and r[P2] is set to -1.
95550 pIn1 = &aMem[pOp->p1];
95551 pIn3 = &aMem[pOp->p3];
95553 assert( pIn1->flags & MEM_Int );
95554 assert( pIn3->flags & MEM_Int );
95555 x = pIn1->u.i;
95556 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
95564 pOut->u.i = -1;
95566 pOut->u.i = x;
95572 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
95576 ** If it is non-zero (negative or positive) and then also jump to P2.
95580 pIn1 = &aMem[pOp->p1];
95581 assert( pIn1->flags&MEM_Int );
95582 VdbeBranchTaken(pIn1->u.i<0, 2);
95583 if( pIn1->u.i ){
95584 if( pIn1->u.i>0 ) pIn1->u.i--;
95591 ** Synopsis: if (--r[P1])==0 goto P2
95597 pIn1 = &aMem[pOp->p1];
95598 assert( pIn1->flags&MEM_Int );
95599 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
95600 VdbeBranchTaken(pIn1->u.i==0, 2);
95601 if( pIn1->u.i==0 ) goto jump_to_p2;
95650 assert( pOp->p4type==P4_FUNCDEF );
95651 n = pOp->p5;
95652 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
95653 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
95654 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
95656 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
95658 pCtx->pMem = 0;
95659 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
95660 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
95661 pCtx->pFunc = pOp->p4.pFunc;
95662 pCtx->iOp = (int)(pOp - aOp);
95663 pCtx->pVdbe = p;
95664 pCtx->skipFlag = 0;
95665 pCtx->isError = 0;
95666 pCtx->enc = encoding;
95667 pCtx->argc = n;
95668 pOp->p4type = P4_FUNCCTX;
95669 pOp->p4.pCtx = pCtx;
95672 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
95674 pOp->opcode = OP_AggStep1;
95683 assert( pOp->p4type==P4_FUNCCTX );
95684 pCtx = pOp->p4.pCtx;
95685 pMem = &aMem[pOp->p3];
95688 if( pOp->p1 ){
95691 assert( pMem->uTemp==0x1122e0e3 );
95694 pMem->uTemp = 0x1122e0e3;
95702 if( pCtx->pMem != pMem ){
95703 pCtx->pMem = pMem;
95704 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
95708 for(i=0; i<pCtx->argc; i++){
95709 assert( memIsValid(pCtx->argv[i]) );
95710 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
95714 pMem->n++;
95715 assert( pCtx->pOut->flags==MEM_Null );
95716 assert( pCtx->isError==0 );
95717 assert( pCtx->skipFlag==0 );
95719 if( pOp->p1 ){
95720 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
95723 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
95725 if( pCtx->isError ){
95726 if( pCtx->isError>0 ){
95727 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
95728 rc = pCtx->isError;
95730 if( pCtx->skipFlag ){
95731 assert( pOp[-1].opcode==OP_CollSeq );
95732 i = pOp[-1].p1;
95734 pCtx->skipFlag = 0;
95736 sqlite3VdbeMemRelease(pCtx->pOut);
95737 pCtx->pOut->flags = MEM_Null;
95738 pCtx->isError = 0;
95741 assert( pCtx->pOut->flags==MEM_Null );
95742 assert( pCtx->skipFlag==0 );
95775 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
95776 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
95777 pMem = &aMem[pOp->p1];
95778 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
95780 if( pOp->p3 ){
95781 memAboutToChange(p, &aMem[pOp->p3]);
95782 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
95783 pMem = &aMem[pOp->p3];
95787 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
95802 ** Checkpoint database P1. This is a no-op if P1 is not currently in
95809 ** mem[P3+2] are initialized to -1.
95816 assert( p->readOnly==0 );
95818 aRes[1] = aRes[2] = -1;
95819 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
95820 || pOp->p2==SQLITE_CHECKPOINT_FULL
95821 || pOp->p2==SQLITE_CHECKPOINT_RESTART
95822 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
95824 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
95830 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
95847 ** Write a string containing the final journal-mode to register P2.
95859 eNew = pOp->p3;
95868 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95869 assert( p->readOnly==0 );
95871 pBt = db->aDb[pOp->p1].pBt;
95886 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
95894 if( !db->autoCommit || db->nVdbeRead>1 ){
95905 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
95933 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
95934 pOut->z = (char *)sqlite3JournalModename(eNew);
95935 pOut->n = sqlite3Strlen30(pOut->z);
95936 pOut->enc = SQLITE_UTF8;
95954 assert( p->readOnly==0 );
95955 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
95956 pOp->p2 ? &aMem[pOp->p2] : 0);
95972 assert( pOp->p1>=0 && pOp->p1<db->nDb );
95973 assert( DbMaskTest(p->btreeMask, pOp->p1) );
95974 assert( p->readOnly==0 );
95975 pBt = db->aDb[pOp->p1].pBt;
95994 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
96004 assert( pOp->p2==0 || pOp->p2==1 );
96005 if( !pOp->p1 ){
96006 sqlite3ExpirePreparedStatements(db, pOp->p2);
96008 p->expired = pOp->p2+1;
96020 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96021 pC = p->apCsr[pOp->p1];
96023 assert( pC->eCurType==CURTYPE_BTREE );
96024 sqlite3BtreeCursorPin(pC->uc.pCursor);
96035 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96036 pC = p->apCsr[pOp->p1];
96038 assert( pC->eCurType==CURTYPE_BTREE );
96039 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
96048 ** the shared-cache feature is enabled.
96054 ** P2 contains the root-page of the table to lock.
96057 ** used to generate an error message if the lock cannot be obtained.
96060 u8 isWriteLock = (u8)pOp->p3;
96061 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
96062 int p1 = pOp->p1;
96063 assert( p1>=0 && p1<db->nDb );
96064 assert( DbMaskTest(p->btreeMask, p1) );
96066 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
96069 const char *z = pOp->p4.z;
96091 pVTab = pOp->p4.pVtab;
96093 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
96113 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
96114 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
96115 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
96118 assert( zTab || db->mallocFailed );
96120 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
96135 db->nVDestroy++;
96136 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
96137 db->nVDestroy--;
96138 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
96157 assert( p->bIsReader );
96160 pVtab = pOp->p4.pVtab->pVtab;
96161 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
96165 pModule = pVtab->pModule;
96166 rc = pModule->xOpen(pVtab, &pVCur);
96171 pVCur->pVtab = pVtab;
96174 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
96176 pCur->uc.pVCur = pVCur;
96177 pVtab->nRef++;
96179 assert( db->mallocFailed );
96180 pModule->xClose(pVCur);
96202 pC = p->apCsr[pOp->p1];
96205 pRhs->pCsr = pC->uc.pCursor;
96206 pRhs->pOut = &aMem[pOp->p3];
96208 pOut->flags = MEM_Null;
96248 pQuery = &aMem[pOp->p3];
96250 pCur = p->apCsr[pOp->p1];
96252 REGISTER_TRACE(pOp->p3, pQuery);
96254 assert( pCur->eCurType==CURTYPE_VTAB );
96255 pVCur = pCur->uc.pVCur;
96256 pVtab = pVCur->pVtab;
96257 pModule = pVtab->pModule;
96260 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
96261 nArg = (int)pArgc->u.i;
96262 iQuery = (int)pQuery->u.i;
96265 apArg = p->apArg;
96269 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
96272 res = pModule->xEof(pVCur);
96273 pCur->nullRow = 0;
96284 ** Store in register P3 the value of the P2-th column of
96285 ** the current row of the virtual-table of cursor P1.
96301 VdbeCursor *pCur = p->apCsr[pOp->p1];
96303 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
96304 pDest = &aMem[pOp->p3];
96306 if( pCur->nullRow ){
96310 assert( pCur->eCurType==CURTYPE_VTAB );
96311 pVtab = pCur->uc.pVCur->pVtab;
96312 pModule = pVtab->pModule;
96313 assert( pModule->xColumn );
96317 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
96318 if( pOp->p5 & OPFLAG_NOCHNG ){
96320 pDest->flags = MEM_Null|MEM_Zero;
96321 pDest->u.nZero = 0;
96325 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
96332 REGISTER_TRACE(pOp->p3, pDest);
96353 pCur = p->apCsr[pOp->p1];
96355 assert( pCur->eCurType==CURTYPE_VTAB );
96356 if( pCur->nullRow ){
96359 pVtab = pCur->uc.pVCur->pVtab;
96360 pModule = pVtab->pModule;
96361 assert( pModule->xNext );
96369 rc = pModule->xNext(pCur->uc.pVCur);
96372 res = pModule->xEof(pCur->uc.pVCur);
96394 isLegacy = (db->flags & SQLITE_LegacyAlter);
96395 db->flags |= SQLITE_LegacyAlter;
96396 pVtab = pOp->p4.pVtab->pVtab;
96397 pName = &aMem[pOp->p1];
96398 assert( pVtab->pModule->xRename );
96400 assert( p->readOnly==0 );
96401 REGISTER_TRACE(pOp->p1, pName);
96402 assert( pName->flags & MEM_Str );
96403 testcase( pName->enc==SQLITE_UTF8 );
96404 testcase( pName->enc==SQLITE_UTF16BE );
96405 testcase( pName->enc==SQLITE_UTF16LE );
96408 rc = pVtab->pModule->xRename(pVtab, pName->z);
96409 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
96411 p->expired = 0;
96424 ** invocation. The value in register (P3+P2-1) corresponds to the
96454 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
96455 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
96457 assert( p->readOnly==0 );
96458 if( db->mallocFailed ) goto no_mem;
96460 pVtab = pOp->p4.pVtab->pVtab;
96461 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
96465 pModule = pVtab->pModule;
96466 nArg = pOp->p2;
96467 assert( pOp->p4type==P4_VTAB );
96468 if( ALWAYS(pModule->xUpdate) ){
96469 u8 vtabOnConflict = db->vtabOnConflict;
96470 apArg = p->apArg;
96471 pX = &aMem[pOp->p3];
96478 db->vtabOnConflict = pOp->p5;
96479 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
96480 db->vtabOnConflict = vtabOnConflict;
96482 if( rc==SQLITE_OK && pOp->p1 ){
96483 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
96484 db->lastRowid = rowid;
96486 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
96487 if( pOp->p5==OE_Ignore ){
96490 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
96493 p->nChange++;
96508 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
96528 pBt = db->aDb[pOp->p1].pBt;
96530 if( pOp->p3 ){
96532 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
96534 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
96549 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
96568 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
96577 ** purely non-deterministic. Some built-in date/time functions can be
96578 ** either determinitic of non-deterministic, depending on their arguments.
96579 ** When those function are used in a non-deterministic way, they will check
96590 assert( pOp->p4type==P4_FUNCCTX );
96591 pCtx = pOp->p4.pCtx;
96597 pOut = &aMem[pOp->p3];
96598 if( pCtx->pOut != pOut ){
96599 pCtx->pVdbe = p;
96600 pCtx->pOut = pOut;
96601 pCtx->enc = encoding;
96602 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
96604 assert( pCtx->pVdbe==p );
96608 for(i=0; i<pCtx->argc; i++){
96609 assert( memIsValid(pCtx->argv[i]) );
96610 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
96614 assert( pCtx->isError==0 );
96615 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
96618 if( pCtx->isError ){
96619 if( pCtx->isError>0 ){
96621 rc = pCtx->isError;
96623 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
96624 pCtx->isError = 0;
96628 assert( (pOut->flags&MEM_Str)==0
96629 || pOut->enc==encoding
96630 || db->mallocFailed );
96633 REGISTER_TRACE(pOp->p3, pOut);
96644 pIn1 = &aMem[pOp->p1];
96645 pIn1->flags &= ~MEM_Subtype;
96658 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
96659 pIn1 = &aMem[pOp->p1];
96660 assert( pIn1->flags & MEM_Blob );
96661 assert( pIn1->n>0 );
96664 if( db->flags&SQLITE_VdbeTrace ){
96666 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96669 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96672 h %= pIn1->n;
96673 pIn1->z[h/8] |= 1<<(h&7);
96689 ** false positive - if the jump is taken when it should fall through.
96694 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
96695 pIn1 = &aMem[pOp->p1];
96696 assert( (pIn1->flags & MEM_Blob)!=0 );
96697 assert( pIn1->n >= 1 );
96700 if( db->flags&SQLITE_VdbeTrace ){
96702 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
96705 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
96708 h %= pIn1->n;
96709 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
96711 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
96714 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
96734 ** the UTF-8 string contained in P4 is emitted on the trace callback.
96753 ** The "--" string is broken up to prevent false-positives with srcck1.c.
96756 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
96758 ** using the X argument when X begins with "--" and invoking
96761 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
96764 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
96767 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
96768 && p->minWriteFileFormat!=254 /* tag-20220401a */
96769 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96772 if( db->mTrace & SQLITE_TRACE_LEGACY ){
96774 db->trace.xLegacy(db->pTraceArg, z);
96778 if( db->nVdbeExec>1 ){
96779 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
96780 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
96783 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
96787 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
96790 for(j=0; j<db->nDb; j++){
96791 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
96792 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
96797 if( (db->flags & SQLITE_SqlTrace)!=0
96798 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
96800 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
96804 assert( pOp->p2>0 );
96805 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
96806 if( pOp->opcode==OP_Trace ) break;
96807 for(i=1; i<p->nOp; i++){
96808 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
96810 pOp->p1 = 0;
96812 pOp->p1++;
96813 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
96823 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
96828 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
96829 assert( pOp->p4type==P4_EXPR );
96830 pC = p->apCsr[pOp->p1];
96832 assert( pC->eCurType==CURTYPE_BTREE );
96833 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
96834 pOp->p4.pExpr, aMem);
96891 assert( pOp->p1>0 );
96892 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
96893 pMem = &aMem[pOp->p1];
96894 constMask = pOp->p3;
96895 for(i=0; i<pOp->p2; i++, pMem++){
96897 pMem->pScopyFrom = 0;
96898 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
96914 ** the same as a no-op. This opcodesnever appears in a real VM program.
96917 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
96924 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
96933 if( endTime>start ) pOrigOp->cycles += endTime - start;
96934 pOrigOp->cnt++;
96944 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
96947 if( db->flags & SQLITE_VdbeTrace ){
96948 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
96951 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
96954 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
96971 if( db->mallocFailed ){
96978 if( db->flags & SQLITE_VdbeTrace ){
96979 const char *zTrace = p->zSql;
96986 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
96989 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
96992 p->rc = rc;
96996 (int)(pOp - aOp), p->zSql, p->zErrMsg);
96997 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
96999 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
97000 db->flags |= SQLITE_CorruptRdOnly;
97004 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
97012 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
97013 nProgressLimit += db->nProgressOps;
97014 if( db->xProgress(db->pProgressArg) ){
97021 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
97024 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
97048 assert( AtomicLoad(&db->u1.isInterrupted) );
97094 ** the b-tree cursor associated with blob handle p to point to row iRow.
97101 ** be set to point to a buffer containing an error message. It is the
97102 ** responsibility of the caller to free the error message buffer using
97105 ** If an error does occur, then the b-tree cursor is closed. All subsequent
97111 char *zErr = 0; /* Error message */
97112 Vdbe *v = (Vdbe *)p->pStmt;
97117 v->aMem[1].flags = MEM_Int;
97118 v->aMem[1].u.i = iRow;
97124 if( v->pc>4 ){
97125 v->pc = 4;
97126 assert( v->aOp[v->pc].opcode==OP_NotExists );
97129 rc = sqlite3_step(p->pStmt);
97132 VdbeCursor *pC = v->apCsr[0];
97135 assert( pC->eCurType==CURTYPE_BTREE );
97136 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
97137 testcase( pC->nHdrParsed==p->iCol );
97138 testcase( pC->nHdrParsed==p->iCol+1 );
97140 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
97144 sqlite3_finalize(p->pStmt);
97145 p->pStmt = 0;
97147 p->iOffset = pC->aType[p->iCol + pC->nField];
97148 p->nByte = sqlite3VdbeSerialTypeLen(type);
97149 p->pCsr = pC->uc.pCursor;
97150 sqlite3BtreeIncrblobCursor(p->pCsr);
97156 }else if( p->pStmt ){
97157 rc = sqlite3_finalize(p->pStmt);
97158 p->pStmt = 0;
97160 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
97163 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
97183 int wrFlag, /* True -> read/write access, false -> read-only */
97187 int iCol; /* Index of zColumn in row-record */
97207 sqlite3_mutex_enter(db->mutex);
97242 pBlob->pTab = pTab;
97243 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
97246 for(iCol=0; iCol<pTab->nCol; iCol++) {
97247 if( sqlite3StrICmp(pTab->aCol[iCol].zCnName, zColumn)==0 ){
97251 if( iCol==pTab->nCol ){
97266 if( db->flags&SQLITE_ForeignKeys ){
97273 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
97275 for(j=0; j<pFKey->nCol; j++){
97276 if( pFKey->aCol[j].iFrom==iCol ){
97283 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
97285 for(j=0; j<pIdx->nKeyCol; j++){
97287 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
97301 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
97302 assert( pBlob->pStmt || db->mallocFailed );
97303 if( pBlob->pStmt ){
97307 ** of writing code to use the b-tree layer directly is that the
97312 ** Code external to the Vdbe then "borrows" the b-tree cursor and
97317 ** which closes the b-tree cursor and (possibly) commits the
97330 Vdbe *v = (Vdbe *)pBlob->pStmt;
97331 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
97335 pTab->pSchema->schema_cookie,
97336 pTab->pSchema->iGeneration);
97338 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
97344 if( db->mallocFailed==0 ){
97351 aOp[0].p2 = pTab->tnum;
97353 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
97355 if( db->mallocFailed==0 ){
97359 ** parameter of the other to pTab->tnum. */
97361 aOp[1].p2 = pTab->tnum;
97372 aOp[1].p4.i = pTab->nCol+1;
97373 aOp[3].p2 = pTab->nCol;
97382 pBlob->iCol = iCol;
97383 pBlob->db = db;
97385 if( db->mallocFailed ){
97394 if( rc==SQLITE_OK && db->mallocFailed==0 ){
97397 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
97404 sqlite3_mutex_leave(db->mutex);
97418 sqlite3_stmt *pStmt = p->pStmt;
97419 db = p->db;
97420 sqlite3_mutex_enter(db->mutex);
97422 sqlite3_mutex_leave(db->mutex);
97446 db = p->db;
97447 sqlite3_mutex_enter(db->mutex);
97448 v = (Vdbe*)p->pStmt;
97450 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
97454 /* If there is no statement handle, then the blob-handle has
97460 ** returned, clean-up the statement handle.
97462 assert( db == v->db );
97463 sqlite3BtreeEnterCursor(p->pCsr);
97466 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
97467 /* If a pre-update hook is registered and this is a write cursor,
97470 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
97477 ** using the incremental-blob API, this works. For the sessions module
97481 iKey = sqlite3BtreeIntegerKey(p->pCsr);
97482 assert( v->apCsr[0]!=0 );
97483 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
97485 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
97490 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
97491 sqlite3BtreeLeaveCursor(p->pCsr);
97494 p->pStmt = 0;
97496 v->rc = rc;
97501 sqlite3_mutex_leave(db->mutex);
97527 return (p && p->pStmt) ? p->nByte : 0;
97536 ** database handle error code and message set. If this happens, then all
97546 db = p->db;
97547 sqlite3_mutex_enter(db->mutex);
97549 if( p->pStmt==0 ){
97550 /* If there is no statement handle, then the blob-handle has
97556 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
97566 assert( rc==SQLITE_OK || p->pStmt==0 );
97567 sqlite3_mutex_leave(db->mutex);
97576 ** 2011-07-09
97591 ** The VdbeSorter object implements a multi-threaded external merge sort
97595 ** Here is the (internal, non-API) interface between this module and the
97648 ** an in-memory merge sort. In this case, no temporary files are required
97655 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
97656 ** of PMAs may be created by merging existing PMAs together - for example
97657 ** merging two or more level-0 PMAs together creates a level-1 PMA.
97661 ** page-cache of the main database. Specifically, the threshold is set to
97665 ** If the sorter is running in single-threaded mode, then all PMAs generated
97667 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
97674 ** The sorter is running in multi-threaded mode if (a) the library was built
97675 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
97684 ** sorter is running in single-threaded mode, then these PMAs are merged
97689 ** Or, if running in multi-threaded mode, then a background thread is
97699 ** Rewind() is called, then a hierarchy of incremental-merges is used.
97705 ** If running in multi-threaded mode and there are more than
97718 ** characteristics of the sorter in multi-threaded mode.
97725 ** Hard-coded maximum amount of data to accumulate in memory before flushing
97738 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
97740 typedef struct SorterList SorterList; /* In-memory list of records */
97753 ** An in-memory list of objects to be sorted.
97762 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
97785 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
97799 ** aReadr[0] -> Banana
97800 ** aReadr[1] -> Feijoa
97801 ** aReadr[2] -> Elderberry
97802 ** aReadr[3] -> Currant
97803 ** aReadr[4] -> Grapefruit
97804 ** aReadr[5] -> Apple
97805 ** aReadr[6] -> Durian
97806 ** aReadr[7] -> EOF
97815 ** aReadr[5] -> Eggplant
97820 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
97843 ** single-threaded operation, there is exactly one instance of this object
97844 ** and for multi-threaded operation there are two or more instances.
97855 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
97872 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
97877 SorterFile file; /* Temp file for level-0 PMAs */
97901 SorterList list; /* List of in-memory records */
97944 ** There are two types of IncrMerger object - single (bUseThread==0) and
97945 ** multi-threaded (bUseThread==1).
97947 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
97962 ** A single-threaded IncrMerger does not open any temporary files of its
97964 ** at offset iStartOff of file pTask->file2. And instead of using a
97966 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
97984 ** size. But I/O is more efficient if it occurs in page-sized blocks where
97986 ** the PMA so that aligned, page-size blocks are written.
97989 int eFWErr; /* Non-zero if in an error state */
98003 ** by this module. If using a separate allocation for each in-memory record
98011 ** has finished passing records to the sorter, or when the in-memory buffer
98044 sqlite3_free(pReadr->aAlloc);
98045 sqlite3_free(pReadr->aBuffer);
98046 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98047 vdbeIncrFree(pReadr->pIncr);
98068 if( p->aMap ){
98069 *ppOut = &p->aMap[p->iReadOff];
98070 p->iReadOff += nByte;
98074 assert( p->aBuffer );
98077 ** p->nBuffer bytes of data from the file into it. Or, if there are less
98078 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
98079 iBuf = p->iReadOff % p->nBuffer;
98085 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
98086 nRead = p->nBuffer;
98088 nRead = (int)(p->iEof - p->iReadOff);
98093 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
98097 nAvail = p->nBuffer - iBuf;
98100 /* The requested data is available in the in-memory buffer. In this
98103 *ppOut = &p->aBuffer[iBuf];
98104 p->iReadOff += nByte;
98106 /* The requested data is not all available in the in-memory buffer.
98107 ** In this case, allocate space at p->aAlloc[] to copy the requested
98108 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
98111 /* Extend the p->aAlloc[] allocation if required. */
98112 if( p->nAlloc<nByte ){
98114 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
98116 aNew = sqlite3Realloc(p->aAlloc, nNew);
98118 p->nAlloc = nNew;
98119 p->aAlloc = aNew;
98123 ** p->aAlloc[]. */
98124 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
98125 p->iReadOff += nAvail;
98126 nRem = nByte - nAvail;
98128 /* The following loop copies up to p->nBuffer bytes per iteration into
98129 ** the p->aAlloc[] buffer. */
98136 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
98139 assert( aNext!=p->aAlloc );
98140 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
98141 nRem -= nCopy;
98144 *ppOut = p->aAlloc;
98157 if( p->aMap ){
98158 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
98160 iBuf = p->iReadOff % p->nBuffer;
98161 if( iBuf && (p->nBuffer-iBuf)>=9 ){
98162 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
98189 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
98190 sqlite3_file *pFd = pFile->pFd;
98191 if( pFd->pMethods->iVersion>=3 ){
98192 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
98212 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
98215 if( pReadr->aMap ){
98216 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
98217 pReadr->aMap = 0;
98219 pReadr->iReadOff = iOff;
98220 pReadr->iEof = pFile->iEof;
98221 pReadr->pFd = pFile->pFd;
98223 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
98224 if( rc==SQLITE_OK && pReadr->aMap==0 ){
98225 int pgsz = pTask->pSorter->pgsz;
98226 int iBuf = pReadr->iReadOff % pgsz;
98227 if( pReadr->aBuffer==0 ){
98228 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
98229 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
98230 pReadr->nBuffer = pgsz;
98233 int nRead = pgsz - iBuf;
98234 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
98235 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
98238 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
98256 if( pReadr->iReadOff>=pReadr->iEof ){
98257 IncrMerger *pIncr = pReadr->pIncr;
98261 if( rc==SQLITE_OK && pIncr->bEof==0 ){
98263 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
98281 pReadr->nKey = (int)nRec;
98282 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
98291 ** starting at offset iStart and ending at offset iEof-1. This function
98307 assert( pFile->iEof>iStart );
98308 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
98309 assert( pReadr->aBuffer==0 );
98310 assert( pReadr->aMap==0 );
98316 pReadr->iEof = pReadr->iReadOff + nByte;
98333 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98337 UnpackedRecord *r2 = pTask->pUnpacked;
98339 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98347 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
98351 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
98352 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
98355 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
98360 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98364 UnpackedRecord *r2 = pTask->pUnpacked;
98366 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
98379 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98394 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
98396 res = n1 - n2;
98400 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98406 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
98407 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98408 res = res * -1;
98421 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
98443 if( (res = v1[i] - v2[i])!=0 ){
98445 res = v1[0] & 0x80 ? -1 : +1;
98451 res = s1 - s2;
98456 res = -1;
98458 res = s1 - s2;
98463 if( *v1 & 0x80 ) res = -1;
98470 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
98475 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
98476 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
98477 res = res * -1;
98486 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
98489 ** is non-zero and the sorter is able to guarantee a stable sort, nField
98497 ** The sorter can guarantee a stable sort when running in single-threaded
98498 ** mode, but not in multi-threaded mode.
98510 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
98511 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
98525 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
98533 nWorker = SORTER_MAX_MERGE_COUNT-1;
98537 assert( pCsr->pKeyInfo );
98538 assert( !pCsr->isEphemeral );
98539 assert( pCsr->eCurType==CURTYPE_SORTER );
98540 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
98544 pCsr->uc.pSorter = pSorter;
98548 Btree *pBt = db->aDb[0].pBt;
98549 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
98550 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
98551 pKeyInfo->db = 0;
98553 pKeyInfo->nKeyField = nField;
98556 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
98558 pSorter->nTask = nWorker + 1;
98559 pSorter->iPrev = (u8)(nWorker - 1);
98560 pSorter->bUseThreads = (pSorter->nTask>1);
98561 pSorter->db = db;
98562 for(i=0; i<pSorter->nTask; i++){
98563 SortSubtask *pTask = &pSorter->aTask[i];
98564 pTask->pSorter = pSorter;
98570 pSorter->mnPmaSize = szPma * pgsz;
98572 mxCache = db->aDb[0].pSchema->cache_size;
98574 /* A negative cache-size value C indicates that the cache is abs(C)
98576 mxCache = mxCache * -1024;
98581 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
98586 assert( pSorter->iMemory==0 );
98587 pSorter->nMemory = pgsz;
98588 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
98589 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
98593 if( pKeyInfo->nAllField<13
98594 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
98595 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
98597 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
98612 pNext = p->u.pNext;
98622 sqlite3DbFree(db, pTask->pUnpacked);
98624 /* pTask->list.aMemory can only be non-zero if it was handed memory
98626 if( pTask->list.aMemory ){
98627 sqlite3_free(pTask->list.aMemory);
98631 assert( pTask->list.aMemory==0 );
98632 vdbeSorterRecordFree(0, pTask->list.pList);
98634 if( pTask->file.pFd ){
98635 sqlite3OsCloseFree(pTask->file.pFd);
98637 if( pTask->file2.pFd ){
98638 sqlite3OsCloseFree(pTask->file2.pFd);
98646 int iTask = (pTask - pTask->pSorter->aTask);
98647 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98661 int iTask = (pTask - pTask->pSorter->aTask);
98662 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98672 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
98685 ** Join thread pTask->thread.
98689 if( pTask->pThread ){
98691 int bDone = pTask->bDone;
98695 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
98698 assert( pTask->bDone==1 );
98699 pTask->bDone = 0;
98700 pTask->pThread = 0;
98713 assert( pTask->pThread==0 && pTask->bDone==0 );
98714 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
98719 ** level-0 PMAs.
98728 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
98731 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
98732 for(i=pSorter->nTask-1; i>=0; i--){
98733 SortSubtask *pTask = &pSorter->aTask[i];
98763 pNew->nTree = N;
98764 pNew->pTask = 0;
98765 pNew->aReadr = (PmaReader*)&pNew[1];
98766 pNew->aTree = (int*)&pNew->aReadr[N];
98777 for(i=0; i<pMerger->nTree; i++){
98778 vdbePmaReaderClear(&pMerger->aReadr[i]);
98791 if( pIncr->bUseThread ){
98792 vdbeSorterJoinThread(pIncr->pTask);
98793 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
98794 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
98797 vdbeMergeEngineFree(pIncr->pMerger);
98808 assert( pSorter->bUseThreads || pSorter->pReader==0 );
98810 if( pSorter->pReader ){
98811 vdbePmaReaderClear(pSorter->pReader);
98812 sqlite3DbFree(db, pSorter->pReader);
98813 pSorter->pReader = 0;
98816 vdbeMergeEngineFree(pSorter->pMerger);
98817 pSorter->pMerger = 0;
98818 for(i=0; i<pSorter->nTask; i++){
98819 SortSubtask *pTask = &pSorter->aTask[i];
98821 pTask->pSorter = pSorter;
98823 if( pSorter->list.aMemory==0 ){
98824 vdbeSorterRecordFree(0, pSorter->list.pList);
98826 pSorter->list.pList = 0;
98827 pSorter->list.szPMA = 0;
98828 pSorter->bUsePMA = 0;
98829 pSorter->iMemory = 0;
98830 pSorter->mxKeysize = 0;
98831 sqlite3DbFree(db, pSorter->pUnpacked);
98832 pSorter->pUnpacked = 0;
98840 assert( pCsr->eCurType==CURTYPE_SORTER );
98841 pSorter = pCsr->uc.pSorter;
98844 sqlite3_free(pSorter->list.aMemory);
98846 pCsr->uc.pSorter = 0;
98852 ** The first argument is a file-handle open on a temporary file. The file
98861 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
98875 ** Allocate space for a file-handle and open a temporary file. If successful,
98876 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
98886 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
98903 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
98907 if( pTask->pUnpacked==0 ){
98908 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
98909 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
98910 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
98911 pTask->pUnpacked->errCode = 0;
98932 res = pTask->xCompare(
98933 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
98938 pp = &p1->u.pNext;
98939 p1 = p1->u.pNext;
98946 pp = &p2->u.pNext;
98947 p2 = p2->u.pNext;
98963 if( p->typeMask==SORTER_TYPE_INTEGER ){
98965 }else if( p->typeMask==SORTER_TYPE_TEXT ){
98972 ** Sort the linked list of records headed at pTask->pList. Return
98985 p = pList->pList;
98986 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
98991 if( pList->aMemory ){
98992 if( (u8*)p==pList->aMemory ){
98995 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
98996 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
98999 pNext = p->u.pNext;
99002 p->u.pNext = 0;
99016 pList->pList = p;
99018 assert( pTask->pUnpacked->errCode==SQLITE_OK
99019 || pTask->pUnpacked->errCode==SQLITE_NOMEM
99021 return pTask->pUnpacked->errCode;
99025 ** Initialize a PMA-writer object.
99034 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
99035 if( !p->aBuffer ){
99036 p->eFWErr = SQLITE_NOMEM_BKPT;
99038 p->iBufEnd = p->iBufStart = (iStart % nBuf);
99039 p->iWriteOff = iStart - p->iBufStart;
99040 p->nBuffer = nBuf;
99041 p->pFd = pFd;
99051 while( nRem>0 && p->eFWErr==0 ){
99053 if( nCopy>(p->nBuffer - p->iBufEnd) ){
99054 nCopy = p->nBuffer - p->iBufEnd;
99057 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
99058 p->iBufEnd += nCopy;
99059 if( p->iBufEnd==p->nBuffer ){
99060 p->eFWErr = sqlite3OsWrite(p->pFd,
99061 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99062 p->iWriteOff + p->iBufStart
99064 p->iBufStart = p->iBufEnd = 0;
99065 p->iWriteOff += p->nBuffer;
99067 assert( p->iBufEnd<p->nBuffer );
99069 nRem -= nCopy;
99074 ** Flush any buffered data to disk and clean up the PMA-writer object.
99075 ** The results of using the PMA-writer after this call are undefined.
99084 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
99085 p->eFWErr = sqlite3OsWrite(p->pFd,
99086 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
99087 p->iWriteOff + p->iBufStart
99090 *piEof = (p->iWriteOff + p->iBufEnd);
99091 sqlite3_free(p->aBuffer);
99092 rc = p->eFWErr;
99109 ** Write the current contents of in-memory linked-list pList to a level-0
99110 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
99118 ** * One or more records packed end-to-end in order of ascending keys.
99123 sqlite3 *db = pTask->pSorter->db;
99128 /* Set iSz to the expected size of file pTask->file after writing the PMA.
99130 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
99135 assert( pList->szPMA>0 );
99138 if( pTask->file.pFd==0 ){
99139 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
99140 assert( rc!=SQLITE_OK || pTask->file.pFd );
99141 assert( pTask->file.iEof==0 );
99142 assert( pTask->nPMA==0 );
99147 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
99159 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
99160 pTask->file.iEof);
99161 pTask->nPMA++;
99162 vdbePmaWriteVarint(&writer, pList->szPMA);
99163 for(p=pList->pList; p; p=pNext){
99164 pNext = p->u.pNext;
99165 vdbePmaWriteVarint(&writer, p->nVal);
99166 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
99167 if( pList->aMemory==0 ) sqlite3_free(p);
99169 pList->pList = p;
99170 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
99174 assert( rc!=SQLITE_OK || pList->pList==0 );
99175 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
99191 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
99192 SortSubtask *pTask = pMerger->pTask;
99195 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
99206 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
99207 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
99209 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
99212 if( pReadr1->pFd==0 ){
99214 }else if( pReadr2->pFd==0 ){
99215 iRes = -1;
99217 iRes = pTask->xCompare(pTask, &bCached,
99218 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
99224 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
99229 ** was actually called above, then pTask->pUnpacked now contains
99238 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
99239 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99242 if( pReadr1->pFd ) bCached = 0;
99243 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
99244 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
99247 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
99250 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
99255 ** The main routine for background threads that write level-0 PMAs.
99260 assert( pTask->bDone==0 );
99261 rc = vdbeSorterListToPMA(pTask, &pTask->list);
99262 pTask->bDone = 1;
99273 pSorter->bUsePMA = 1;
99274 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
99279 int nWorker = (pSorter->nTask-1);
99283 pSorter->bUsePMA = 1;
99285 /* Select a sub-task to sort and flush the current list of in-memory
99286 ** records to disk. If the sorter is running in multi-threaded mode,
99287 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
99288 ** the background thread from a sub-tasks previous turn is still running,
99289 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
99290 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
99291 ** sub-tasks are prefered as they use background threads - the final
99292 ** sub-task uses the main thread. */
99294 int iTest = (pSorter->iPrev + i + 1) % nWorker;
99295 pTask = &pSorter->aTask[iTest];
99296 if( pTask->bDone ){
99299 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
99305 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
99312 assert( pTask->pThread==0 && pTask->bDone==0 );
99313 assert( pTask->list.pList==0 );
99314 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
99316 aMem = pTask->list.aMemory;
99318 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
99319 pTask->list = pSorter->list;
99320 pSorter->list.pList = 0;
99321 pSorter->list.szPMA = 0;
99323 pSorter->list.aMemory = aMem;
99324 pSorter->nMemory = sqlite3MallocSize(aMem);
99325 }else if( pSorter->list.aMemory ){
99326 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
99327 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
99353 assert( pCsr->eCurType==CURTYPE_SORTER );
99354 pSorter = pCsr->uc.pSorter;
99355 getVarint32NR((const u8*)&pVal->z[1], t);
99357 pSorter->typeMask &= SORTER_TYPE_INTEGER;
99359 pSorter->typeMask &= SORTER_TYPE_TEXT;
99361 pSorter->typeMask = 0;
99369 ** If using the single large allocation mode (pSorter->aMemory!=0), then
99376 ** * The total memory allocated for the in-memory list is greater
99377 ** than (page-size * cache-size), or
99379 ** * The total memory allocated for the in-memory list is greater
99380 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
99382 nReq = pVal->n + sizeof(SorterRecord);
99383 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
99384 if( pSorter->mxPmaSize ){
99385 if( pSorter->list.aMemory ){
99386 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
99389 (pSorter->list.szPMA > pSorter->mxPmaSize)
99390 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
99395 pSorter->list.szPMA = 0;
99396 pSorter->iMemory = 0;
99397 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
99401 pSorter->list.szPMA += nPMA;
99402 if( nPMA>pSorter->mxKeysize ){
99403 pSorter->mxKeysize = nPMA;
99406 if( pSorter->list.aMemory ){
99407 int nMin = pSorter->iMemory + nReq;
99409 if( nMin>pSorter->nMemory ){
99411 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
99412 int iListOff = -1;
99413 if( pSorter->list.pList ){
99414 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
99417 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
99419 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
99422 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
99424 pSorter->list.aMemory = aNew;
99425 pSorter->nMemory = nNew;
99428 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
99429 pSorter->iMemory += ROUND8(nReq);
99430 if( pSorter->list.pList ){
99431 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
99438 pNew->u.pNext = pSorter->list.pList;
99441 memcpy(SRVAL(pNew), pVal->z, pVal->n);
99442 pNew->nVal = pVal->n;
99443 pSorter->list.pList = pNew;
99449 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
99451 ** except that the number-of-bytes varint is omitted from the start.
99456 i64 iStart = pIncr->iStartOff;
99457 SorterFile *pOut = &pIncr->aFile[1];
99458 SortSubtask *pTask = pIncr->pTask;
99459 MergeEngine *pMerger = pIncr->pMerger;
99461 assert( pIncr->bEof==0 );
99465 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
99468 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
99469 int nKey = pReader->nKey;
99474 if( pReader->pFd==0 ) break;
99475 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
99479 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
99480 assert( pIncr->pMerger->pTask==pTask );
99481 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
99484 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
99493 ** multi-threaded IncrMerger objects.
99498 pIncr->pTask->bDone = 1;
99507 assert( pIncr->bUseThread );
99508 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
99518 ** For single-threaded objects, this is accomplished by literally reading
99519 ** keys from pIncr->pMerger and repopulating aFile[0].
99521 ** For multi-threaded objects, all that is required is to wait until the
99533 if( pIncr->bUseThread ){
99534 rc = vdbeSorterJoinThread(pIncr->pTask);
99537 SorterFile f0 = pIncr->aFile[0];
99538 pIncr->aFile[0] = pIncr->aFile[1];
99539 pIncr->aFile[1] = f0;
99543 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99544 pIncr->bEof = 1;
99553 pIncr->aFile[0] = pIncr->aFile[1];
99554 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
99555 pIncr->bEof = 1;
99577 pIncr->pMerger = pMerger;
99578 pIncr->pTask = pTask;
99579 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
99580 pTask->file2.iEof += pIncr->mxSz;
99591 ** Set the "use-threads" flag on object pIncr.
99594 pIncr->bUseThread = 1;
99595 pIncr->pTask->file2.iEof -= pIncr->mxSz;
99602 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
99608 int iOut /* Store the result in pMerger->aTree[iOut] */
99616 assert( iOut<pMerger->nTree && iOut>0 );
99618 if( iOut>=(pMerger->nTree/2) ){
99619 i1 = (iOut - pMerger->nTree/2) * 2;
99622 i1 = pMerger->aTree[iOut*2];
99623 i2 = pMerger->aTree[iOut*2+1];
99626 p1 = &pMerger->aReadr[i1];
99627 p2 = &pMerger->aReadr[i2];
99629 if( p1->pFd==0 ){
99631 }else if( p2->pFd==0 ){
99634 SortSubtask *pTask = pMerger->pTask;
99637 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
99638 res = pTask->xCompare(
99639 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
99648 pMerger->aTree[iOut] = iRes;
99655 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
99701 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99705 assert( pMerger->pTask==0 );
99706 pMerger->pTask = pTask;
99708 nTree = pMerger->nTree;
99713 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
99716 ** on this PmaReader before any of the multi-threaded PmaReaders takes
99717 ** better advantage of multi-processor hardware. */
99718 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
99720 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
99725 for(i=pMerger->nTree-1; i>0; i--){
99728 return pTask->pUnpacked->errCode;
99733 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
99735 ** object at (pReadr->pIncr).
99738 ** in the sub-tree headed by pReadr are also initialized. Data is then
99743 ** to be a multi-threaded PmaReader and this function is being called in a
99744 ** background thread. In this case all PmaReaders in the sub-tree are
99752 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
99753 ** this entire function is being run by thread (pTask->thread), that will
99757 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
99758 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
99766 IncrMerger *pIncr = pReadr->pIncr;
99767 SortSubtask *pTask = pIncr->pTask;
99768 sqlite3 *db = pTask->pSorter->db;
99770 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
99773 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
99775 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
99776 ** requires two temp files to itself, whereas a single-threaded object
99777 ** only requires a region of pTask->file2. */
99779 int mxSz = pIncr->mxSz;
99781 if( pIncr->bUseThread ){
99782 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
99784 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
99788 /*if( !pIncr->bUseThread )*/{
99789 if( pTask->file2.pFd==0 ){
99790 assert( pTask->file2.iEof>0 );
99791 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
99792 pTask->file2.iEof = 0;
99795 pIncr->aFile[1].pFd = pTask->file2.pFd;
99796 pIncr->iStartOff = pTask->file2.iEof;
99797 pTask->file2.iEof += mxSz;
99803 if( rc==SQLITE_OK && pIncr->bUseThread ){
99805 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
99807 ** pIncr->pTask->thread.
99836 pReader->pIncr->pTask->bDone = 1;
99842 ** If the PmaReader passed as the first argument is not an incremental-reader
99843 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
99847 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
99853 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
99857 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
99858 if( pIncr->bUseThread ){
99860 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
99871 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
99872 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
99877 ** first PMA to read from pTask->file. Assuming no error occurs, it is
99885 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
99886 MergeEngine **ppOut /* OUT: New merge-engine */
99898 PmaReader *pReadr = &pNew->aReadr[i];
99899 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
99900 iOff = pReadr->iEof;
99917 ** nPMA<=16 -> TreeDepth() == 0
99918 ** nPMA<=256 -> TreeDepth() == 1
99919 ** nPMA<=65536 -> TreeDepth() == 2
99932 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
99960 PmaReader *pReadr = &p->aReadr[iIter];
99962 if( pReadr->pIncr==0 ){
99967 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
99971 p = pReadr->pIncr->pMerger;
99977 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
99986 ** that has already written two or more level-0 PMAs to one or more temp
100004 /* If the sorter uses more than one task, then create the top-level
100006 ** one PmaReader per sub-task. */
100007 assert( pSorter->bUseThreads || pSorter->nTask==1 );
100008 if( pSorter->nTask>1 ){
100009 pMain = vdbeMergeEngineNew(pSorter->nTask);
100014 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
100015 SortSubtask *pTask = &pSorter->aTask[iTask];
100016 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
100017 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
100019 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
100022 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
100023 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
100029 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
100030 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
100031 int nReader; /* Number of level-0 PMAs to merge */
100033 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
100044 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
100069 ** (for multi-threaded sorters) so that it can be used to iterate through
100076 SortSubtask *pTask0 = &pSorter->aTask[0];
100079 sqlite3 *db = pTask0->pSorter->db;
100082 for(i=0; i<pSorter->nTask; i++){
100083 pSorter->aTask[i].xCompare = xCompare;
100090 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
100091 if( pSorter->bUseThreads ){
100094 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
100098 pSorter->pReader = pReadr;
100102 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
100104 vdbeIncrMergerSetThreads(pReadr->pIncr);
100105 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
100107 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
100109 assert( pIncr->pTask!=pLast );
100112 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
100117 ** b) If it is using task (nTask-1), it is configured to run
100118 ** in single-threaded mode. This is important, as the
100122 PmaReader *p = &pMain->aReadr[iTask];
100123 assert( p->pIncr==0 || (
100124 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
100125 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
100139 pSorter->pMerger = pMain;
100160 assert( pCsr->eCurType==CURTYPE_SORTER );
100161 pSorter = pCsr->uc.pSorter;
100166 ** from the in-memory list. */
100167 if( pSorter->bUsePMA==0 ){
100168 if( pSorter->list.pList ){
100170 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
100177 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
100181 assert( pSorter->list.pList );
100191 assert( pSorter->pReader==0 );
100212 assert( pCsr->eCurType==CURTYPE_SORTER );
100213 pSorter = pCsr->uc.pSorter;
100214 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
100215 if( pSorter->bUsePMA ){
100216 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
100217 assert( pSorter->bUseThreads==0 || pSorter->pReader );
100218 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
100220 if( pSorter->bUseThreads ){
100221 rc = vdbePmaReaderNext(pSorter->pReader);
100222 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
100225 /*if( !pSorter->bUseThreads )*/ {
100227 assert( pSorter->pMerger!=0 );
100228 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
100229 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
100233 SorterRecord *pFree = pSorter->list.pList;
100234 pSorter->list.pList = pFree->u.pNext;
100235 pFree->u.pNext = 0;
100236 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
100237 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
100251 if( pSorter->bUsePMA ){
100254 if( pSorter->bUseThreads ){
100255 pReader = pSorter->pReader;
100258 /*if( !pSorter->bUseThreads )*/{
100259 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
100261 *pnKey = pReader->nKey;
100262 pKey = pReader->aKey;
100264 *pnKey = pSorter->list.pList->nVal;
100265 pKey = SRVAL(pSorter->list.pList);
100277 assert( pCsr->eCurType==CURTYPE_SORTER );
100278 pSorter = pCsr->uc.pSorter;
100283 pOut->n = nKey;
100285 memcpy(pOut->z, pKey, nKey);
100318 assert( pCsr->eCurType==CURTYPE_SORTER );
100319 pSorter = pCsr->uc.pSorter;
100320 r2 = pSorter->pUnpacked;
100321 pKeyInfo = pCsr->pKeyInfo;
100323 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
100325 r2->nField = nKeyCol;
100327 assert( r2->nField==nKeyCol );
100332 if( r2->aMem[i].flags & MEM_Null ){
100333 *pRes = -1;
100338 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
100345 ** 2020-03-23
100356 ** This file implements virtual-tables for examining the bytecode content
100363 /* An instance of the bytecode() table-valued function.
100367 sqlite3_vtab base; /* Base class - must be first */
100376 sqlite3_vtab_cursor base; /* Base class - must be first */
100391 ** Create a new bytecode() table-valued function.
100435 pNew->db = db;
100436 pNew->bTablesUsed = isTabUsed*2;
100459 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
100460 *ppCursor = &pCur->base;
100468 sqlite3_free(pCur->zP4);
100469 pCur->zP4 = 0;
100470 sqlite3VdbeMemRelease(&pCur->sub);
100471 sqlite3VdbeMemSetNull(&pCur->sub);
100472 if( pCur->needFinalize ){
100473 sqlite3_finalize(pCur->pStmt);
100475 pCur->pStmt = 0;
100476 pCur->needFinalize = 0;
100477 pCur->zType = 0;
100478 pCur->zSchema = 0;
100479 pCur->zName = 0;
100498 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
100500 if( pCur->zP4 ){
100501 sqlite3_free(pCur->zP4);
100502 pCur->zP4 = 0;
100504 if( pCur->zName ){
100505 pCur->zName = 0;
100506 pCur->zType = 0;
100507 pCur->zSchema = 0;
100510 (Vdbe*)pCur->pStmt,
100511 pCur->showSubprograms ? &pCur->sub : 0,
100512 pTab->bTablesUsed,
100513 &pCur->iRowid,
100514 &pCur->iAddr,
100515 &pCur->aOp);
100517 sqlite3VdbeMemSetNull(&pCur->sub);
100518 pCur->aOp = 0;
100529 return pCur->aOp==0;
100542 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
100543 Op *pOp = pCur->aOp + pCur->iAddr;
100544 if( pVTab->bTablesUsed ){
100548 if( i<=2 && pCur->zType==0 ){
100551 int iDb = pOp->p3;
100552 Pgno iRoot = (Pgno)pOp->p2;
100553 sqlite3 *db = pVTab->db;
100554 pSchema = db->aDb[iDb].pSchema;
100555 pCur->zSchema = db->aDb[iDb].zDbSName;
100556 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
100558 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
100559 pCur->zName = pTab->zName;
100560 pCur->zType = "table";
100564 if( pCur->zName==0 ){
100565 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
100567 if( pIdx->tnum==iRoot ){
100568 pCur->zName = pIdx->zName;
100569 pCur->zType = "index";
100579 sqlite3_result_int(ctx, pCur->iAddr);
100582 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
100583 -1, SQLITE_STATIC);
100586 sqlite3_result_int(ctx, pOp->p1);
100589 sqlite3_result_int(ctx, pOp->p2);
100592 sqlite3_result_int(ctx, pOp->p3);
100596 if( pCur->zP4==0 ){
100597 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
100600 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
100603 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
100604 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
100609 sqlite3_result_int(ctx, pOp->p5);
100612 Op *aOp = pCur->aOp;
100614 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
100615 if( pCur->iRowid==pCur->iAddr+1 ){
100618 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
100625 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
100628 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
100631 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
100634 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
100646 *pRowid = pCur->iRowid;
100662 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
100666 pCur->iRowid = 0;
100667 pCur->iAddr = 0;
100668 pCur->showSubprograms = idxNum==0;
100675 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
100676 pCur->needFinalize = 1;
100679 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
100681 if( pCur->pStmt==0 ){
100682 pVTab->base.zErrMsg = sqlite3_mprintf(
100684 pVTab->bTablesUsed ? "tables_used" : "bytecode"
100706 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
100707 pIdxInfo->estimatedCost = (double)100;
100708 pIdxInfo->estimatedRows = 100;
100709 pIdxInfo->idxNum = 0;
100710 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
100711 if( p->usable==0 ) continue;
100712 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
100714 pIdxInfo->aConstraintUsage[i].omit = 1;
100715 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
100717 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
100718 pIdxInfo->aConstraintUsage[i].omit = 1;
100719 pIdxInfo->idxNum = 1;
100783 ** This file contains code use to implement an in-memory rollback journal.
100784 ** The in-memory rollback journal is used to journal transactions for
100787 ** Update: The in-memory journal is also used to temporarily cache
100788 ** smaller journals that are not critical for power-loss recovery.
100806 ** The zChunk array is always at least 8 bytes in size - usually much more.
100823 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
100835 ** This structure is a subclass of sqlite3_file. Each open memory-journal
100840 int nChunkSize; /* In-memory chunk-size */
100843 FileChunk *pFirst; /* Head of in-memory chunk-list */
100853 ** Read data from the in-memory journal file. This is the implementation
100868 if( (iAmt+iOfst)>p->endpoint.iOffset ){
100871 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
100872 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
100874 for(pChunk=p->pFirst;
100875 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
100876 pChunk=pChunk->pNext
100878 iOff += p->nChunkSize;
100881 pChunk = p->readpoint.pChunk;
100885 iChunkOffset = (int)(iOfst%p->nChunkSize);
100887 int iSpace = p->nChunkSize - iChunkOffset;
100888 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
100889 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
100891 nRead -= iSpace;
100893 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
100894 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
100895 p->readpoint.pChunk = pChunk;
100907 pNext = pIter->pNext;
100926 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
100928 nChunk = copy.endpoint.iOffset - iOff;
100930 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
100935 /* No error has occurred. Free the in-memory buffers. */
100941 ** the original before returning. This way, SQLite uses the in-memory
100942 ** journal data to roll back changes made to the internal page-cache
100969 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
100979 /* An in-memory journal file should only ever be appended to. Random
100981 ** the in-memory journal is being used by a connection using the
100982 ** atomic-write optimization. In this case the first 28 bytes of the
100984 assert( iOfst<=p->endpoint.iOffset );
100985 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
100988 if( iOfst==0 && p->pFirst ){
100989 assert( p->nChunkSize>iAmt );
100990 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
100993 FileChunk *pChunk = p->endpoint.pChunk;
100994 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
100995 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
101000 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
101004 pNew->pNext = 0;
101006 assert( p->pFirst );
101007 pChunk->pNext = pNew;
101009 assert( !p->pFirst );
101010 p->pFirst = pNew;
101012 pChunk = p->endpoint.pChunk = pNew;
101016 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
101018 nWrite -= iSpace;
101019 p->endpoint.iOffset += iSpace;
101028 ** Truncate the in-memory file.
101032 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
101033 if( size<p->endpoint.iOffset ){
101036 memjrnlFreeChunks(p->pFirst);
101037 p->pFirst = 0;
101039 i64 iOff = p->nChunkSize;
101040 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
101041 iOff += p->nChunkSize;
101044 memjrnlFreeChunks(pIter->pNext);
101045 pIter->pNext = 0;
101049 p->endpoint.pChunk = pIter;
101050 p->endpoint.iOffset = size;
101051 p->readpoint.pChunk = 0;
101052 p->readpoint.iOffset = 0;
101062 memjrnlFreeChunks(p->pFirst);
101070 ** syncing an in-memory journal is a no-op.
101082 *pSize = (sqlite_int64) p->endpoint.iOffset;
101117 ** all content is always stored in main-memory. Finally, if nSpill is a
101118 ** positive value, then the journal file is initially created in-memory
101132 /* Zero the file-handle object. If nSpill was passed zero, initialize
101135 ** made on the journal file-handle. */
101142 p->nChunkSize = nSpill;
101144 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
101145 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
101148 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
101149 p->nSpill = nSpill;
101150 p->flags = flags;
101151 p->zJournal = zName;
101152 p->pVfs = pVfs;
101157 ** Open an in-memory journal file.
101160 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
101167 ** in-memory-only journal file (i.e. is one that was opened with a +ve
101174 if( pJfd->pMethods==&MemJournalMethods && (
101176 p->nSpill>0
101181 NEVER(p->nSpill>0)
101184 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
101194 ** The file-handle passed as the only argument is open on a journal file.
101199 return p->pMethods==&MemJournalMethods;
101204 ** pVfs to create the underlying on-disk files.
101207 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
101238 for(pWin=pList; pWin; pWin=pWin->pNextWin){
101240 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
101242 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
101244 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
101246 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
101248 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
101270 ** return from the top-level walk call.
101280 rc = pWalker->xExprCallback(pWalker, pExpr);
101283 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
101284 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
101285 if( pExpr->pRight ){
101287 pExpr = pExpr->pRight;
101291 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
101293 if( pExpr->x.pList ){
101294 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
101298 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
101319 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
101320 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
101327 ** This is a no-op callback for Walker->xSelectCallback2. If this
101328 ** callback is set, then the Select->pWinDefn list is traversed.
101333 /* No-op */
101343 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
101344 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
101345 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
101346 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
101347 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
101348 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
101350 if( p->pWinDefn ){
101352 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
101353 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
101355 || pWalker->xSelectCallback2==sqlite3SelectPopWith
101360 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
101380 pSrc = p->pSrc;
101382 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
101383 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
101386 if( pItem->fg.isTabFunc
101387 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
101399 ** on the compound select chain, p->pPrior.
101404 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
101411 ** is a no-op returning WRC_Continue.
101416 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
101418 rc = pWalker->xSelectCallback(pWalker, p);
101425 if( pWalker->xSelectCallback2 ){
101426 pWalker->xSelectCallback2(pWalker, p);
101428 p = p->pPrior;
101438 pWalker->walkerDepth++;
101443 pWalker->walkerDepth--;
101448 ** No-op routine for the parse-tree walker.
101462 ** No-op routine for the parse-tree walker for SELECT statements.
101502 ** is a helper function - a callback for the tree walker.
101507 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
101521 ** Turn the pExpr expression into an alias for the iCol-th column of the
101542 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
101546 Expr *pOrig; /* The iCol-th column of the result set */
101550 assert( iCol>=0 && iCol<pEList->nExpr );
101551 pOrig = pEList->a[iCol].pExpr;
101553 db = pParse->db;
101555 if( db->mallocFailed ){
101561 if( pExpr->op==TK_COLLATE ){
101563 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
101569 if( ALWAYS(pExpr->y.pWin!=0) ){
101570 pExpr->y.pWin->pOwner = pExpr;
101594 if( pItem->fg.eEName!=ENAME_TAB ) return 0;
101595 zSpan = pItem->zEName;
101613 ** Return TRUE if the double-quoted string mis-feature should be supported.
101616 if( db->init.busy ) return 1; /* Always support for legacy schemas */
101617 if( pTopNC->ncFlags & NC_IsDDL ){
101619 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
101622 return (db->flags & SQLITE_DqsDDL)!=0;
101625 return (db->flags & SQLITE_DqsDML)!=0;
101630 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
101637 n = pExpr->iColumn;
101639 pExTab = pExpr->y.pTab;
101641 if( (pExTab->tabFlags & TF_HasGenerated)!=0
101642 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
101644 testcase( pExTab->nCol==BMS-1 );
101645 testcase( pExTab->nCol==BMS );
101646 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
101648 testcase( n==BMS-1 );
101650 if( n>=BMS ) n = BMS-1;
101667 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
101669 pNew->iTable = pMatch->iCursor;
101670 pNew->iColumn = iColumn;
101671 pNew->y.pTab = pMatch->pTab;
101672 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
101684 ** pExpr->iDb Set the index in db->aDb[] of the database X
101686 ** pExpr->iTable Set to the cursor number for the table obtained
101688 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
101690 ** pExpr->iColumn Set to the column number within the table.
101691 ** pExpr->op Set to TK_COLUMN.
101692 ** pExpr->pLeft Any expression this points to is deleted
101693 ** pExpr->pRight Any expression this points to is deleted.
101702 ** If the name cannot be resolved unambiguously, leave an error message
101717 sqlite3 *db = pParse->db; /* The database connection */
101722 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
101732 /* Initialize the node to no-match */
101733 pExpr->iTable = -1;
101738 ** resulting in an appropriate error message toward the end of this routine
101741 testcase( pNC->ncFlags & NC_PartIdx );
101742 testcase( pNC->ncFlags & NC_IsCheck );
101743 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
101750 for(i=0; i<db->nDb; i++){
101751 assert( db->aDb[i].zDbSName );
101752 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
101753 pSchema = db->aDb[i].pSchema;
101757 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
101760 pSchema = db->aDb[0].pSchema;
101761 zDb = db->aDb[0].zDbSName;
101766 /* Start at the inner-most context and move outward until a match is found */
101770 SrcList *pSrcList = pNC->pSrcList;
101773 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
101775 pTab = pItem->pTab;
101776 assert( pTab!=0 && pTab->zName!=0 );
101777 assert( pTab->nCol>0 || pParse->nErr );
101778 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
101779 if( pItem->fg.isNestedFrom ){
101784 ** This pItem -------------^
101787 assert( pItem->pSelect!=0 );
101788 pEList = pItem->pSelect->pEList;
101790 assert( pEList->nExpr==pTab->nCol );
101791 for(j=0; j<pEList->nExpr; j++){
101792 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
101796 if( pItem->fg.isUsing==0
101797 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101805 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
101806 /* An INNER or LEFT JOIN. Use the left-most table */
101809 if( (pItem->fg.jointype & JT_LEFT)==0 ){
101810 /* A RIGHT JOIN. Use the right-most table */
101816 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101822 pExpr->iColumn = j;
101823 pEList->a[j].fg.bUsed = 1;
101825 if( pEList->a[j].fg.bUsingTerm ) break;
101833 if( pTab->pSchema!=pSchema ) continue;
101836 zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
101842 if( IN_RENAME_OBJECT && pItem->zAlias ){
101843 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
101847 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
101848 if( pCol->hName==hCol
101849 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101852 if( pItem->fg.isUsing==0
101853 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
101861 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
101862 /* An INNER or LEFT JOIN. Use the left-most table */
101865 if( (pItem->fg.jointype & JT_LEFT)==0 ){
101866 /* A RIGHT JOIN. Use the right-most table */
101872 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
101877 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
101878 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
101879 if( pItem->fg.isNestedFrom ){
101891 pExpr->iTable = pMatch->iCursor;
101893 pExpr->y.pTab = pMatch->pTab;
101894 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
101897 pSchema = pExpr->y.pTab->pSchema;
101910 if( pParse->pTriggerTab!=0 ){
101911 int op = pParse->eTriggerOp;
101913 if( pParse->bReturning ){
101914 if( (pNC->ncFlags & NC_UBaseReg)!=0
101915 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
101917 pExpr->iTable = op!=TK_DELETE;
101918 pTab = pParse->pTriggerTab;
101921 pExpr->iTable = 1;
101922 pTab = pParse->pTriggerTab;
101924 pExpr->iTable = 0;
101925 pTab = pParse->pTriggerTab;
101930 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
101931 Upsert *pUpsert = pNC->uNC.pUpsert;
101933 pTab = pUpsert->pUpsertSrc->a[0].pTab;
101934 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
101942 pSchema = pTab->pSchema;
101944 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
101945 if( pCol->hName==hCol
101946 && sqlite3StrICmp(pCol->zCnName, zCol)==0
101948 if( iCol==pTab->iPKey ){
101949 iCol = -1;
101954 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
101955 /* IMP: R-51414-32910 */
101956 iCol = -1;
101958 if( iCol<pTab->nCol ){
101962 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
101963 testcase( iCol==(-1) );
101966 pExpr->iColumn = iCol;
101967 pExpr->y.pTab = pTab;
101970 pExpr->iTable = pNC->uNC.pUpsert->regData +
101978 pExpr->y.pTab = pTab;
101979 if( pParse->bReturning ){
101981 pExpr->op2 = TK_COLUMN;
101982 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
101985 pExpr->iColumn = (i16)iCol;
101989 pExpr->affExpr = SQLITE_AFF_INTEGER;
101990 }else if( pExpr->iTable==0 ){
101993 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
101997 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
102013 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
102015 && ALWAYS(VisibleRowid(pMatch->pTab))
102018 pExpr->iColumn = -1;
102019 pExpr->affExpr = SQLITE_AFF_INTEGER;
102024 ** might refer to an result-set alias. This happens, for example, when
102034 ** The ability to use an output result-set column in the WHERE, GROUP BY,
102041 && (pNC->ncFlags & NC_UEList)!=0
102044 pEList = pNC->uNC.pEList;
102046 for(j=0; j<pEList->nExpr; j++){
102047 char *zAs = pEList->a[j].zEName;
102048 if( pEList->a[j].fg.eEName==ENAME_NAME
102052 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
102053 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
102054 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
102055 pOrig = pEList->a[j].pExpr;
102056 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
102061 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
102086 pNC = pNC->pNext;
102093 ** supplied) and the value of Z is enclosed in double-quotes, then
102098 ** Because no reference was made to outer contexts, the pNC->nRef
102102 assert( pExpr->op==TK_ID );
102106 /* If a double-quoted identifier does not match any known column name,
102110 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
102121 "double-quoted string literal: \"%w\"", zCol);
102123 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
102125 pExpr->op = TK_STRING;
102126 memset(&pExpr->y, 0, sizeof(pExpr->y));
102146 if( pFJMatch->nExpr==cnt-1 ){
102150 sqlite3ExprDelete(db, pExpr->pLeft);
102151 pExpr->pLeft = 0;
102152 sqlite3ExprDelete(db, pExpr->pRight);
102153 pExpr->pRight = 0;
102155 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
102156 pExpr->op = TK_FUNCTION;
102157 pExpr->u.zToken = "coalesce";
102158 pExpr->x.pList = pFJMatch;
102174 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102175 pParse->checkSchema = 1;
102176 pTopNC->nNcErr++;
102182 sqlite3ExprDelete(db, pExpr->pLeft);
102183 pExpr->pLeft = 0;
102184 sqlite3ExprDelete(db, pExpr->pRight);
102185 pExpr->pRight = 0;
102203 if( pExpr->iColumn>=0 && pMatch!=0 ){
102204 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
102207 pExpr->op = eNewExprOp;
102212 if( pParse->db->xAuth
102213 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
102215 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
102222 pTopNC->nRef++;
102224 pTopNC = pTopNC->pNext;
102239 SrcItem *pItem = &pSrc->a[iSrc];
102242 pTab = p->y.pTab = pItem->pTab;
102243 p->iTable = pItem->iCursor;
102244 if( p->y.pTab->iPKey==iCol ){
102245 p->iColumn = -1;
102247 p->iColumn = (ynVar)iCol;
102248 if( (pTab->tabFlags & TF_HasGenerated)!=0
102249 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
102251 testcase( pTab->nCol==63 );
102252 testcase( pTab->nCol==64 );
102253 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
102256 testcase( iCol==BMS-1 );
102257 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
102266 ** pNC->ncFlags values determined by validMask.
102269 ** Parse *pParse, // Leave error message here
102281 Parse *pParse, /* Leave error message here */
102288 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
102290 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
102293 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
102296 if( pExpr ) pExpr->op = TK_NULL;
102297 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102301 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
102305 ** Return 1024 times this value. Or return -1 if p is not a floating point
102309 double r = -1.0;
102310 if( p->op!=TK_FLOAT ) return -1;
102312 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
102314 if( r>1.0 ) return -1;
102333 pNC = pWalker->u.pNC;
102335 pParse = pNC->pParse;
102336 assert( pParse==pWalker->pParse );
102339 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
102340 SrcList *pSrcList = pNC->pSrcList;
102342 for(i=0; i<pNC->pSrcList->nSrc; i++){
102343 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
102347 switch( pExpr->op ){
102355 SrcList *pSrcList = pNC->pSrcList;
102357 assert( pSrcList && pSrcList->nSrc>=1 );
102358 pItem = pSrcList->a;
102359 pExpr->op = TK_COLUMN;
102361 pExpr->y.pTab = pItem->pTab;
102362 pExpr->iTable = pItem->iCursor;
102363 pExpr->iColumn--;
102364 pExpr->affExpr = SQLITE_AFF_INTEGER;
102370 ** "expr IS NOT NULL" --> "TRUE"
102371 ** "expr IS NULL" --> "FALSE"
102376 ** If this optimization occurs, also restore the NameContext ref-counts
102387 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
102388 anRef[i] = p->nRef;
102390 sqlite3WalkExpr(pWalker, pExpr->pLeft);
102391 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
102394 if( pExpr->op==TK_NOTNULL ){
102395 pExpr->u.zToken = "true";
102398 pExpr->u.zToken = "false";
102401 pExpr->op = TK_TRUEFALSE;
102402 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
102403 p->nRef = anRef[i];
102405 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
102406 pExpr->pLeft = 0;
102416 ** be one call to lookupName(). Then the compiler will in-line
102426 if( pExpr->op==TK_ID ){
102430 zColumn = pExpr->u.zToken;
102432 Expr *pLeft = pExpr->pLeft;
102433 testcase( pNC->ncFlags & NC_IdxExpr );
102434 testcase( pNC->ncFlags & NC_GenCol );
102437 pRight = pExpr->pRight;
102438 if( pRight->op==TK_ID ){
102441 assert( pRight->op==TK_DOT );
102443 zDb = pLeft->u.zToken;
102444 pLeft = pRight->pLeft;
102445 pRight = pRight->pRight;
102448 zTable = pLeft->u.zToken;
102449 zColumn = pRight->u.zToken;
102453 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
102462 ExprList *pList = pExpr->x.pList; /* The argument list */
102463 int n = pList ? pList->nExpr : 0; /* Number of arguments */
102469 u8 enc = ENC(pParse->db); /* The database encoding */
102470 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
102472 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
102475 zId = pExpr->u.zToken;
102476 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
102478 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
102485 is_agg = pDef->xFinalize!=0;
102486 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
102489 pExpr->iTable = exprProbability(pList->a[1].pExpr);
102490 if( pExpr->iTable<0 ){
102494 pNC->nNcErr++;
102497 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
102499 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
102500 ** short-hand for likelihood(X,0.0625).
102501 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
102503 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
102506 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
102511 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
102516 pNC->nNcErr++;
102518 pExpr->op = TK_NULL;
102523 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
102530 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
102531 /* Clearly non-deterministic functions like random(), but also
102537 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
102541 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
102542 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
102544 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
102545 && pParse->nested==0
102546 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
102548 /* Internal-use-only functions are disallowed unless the
102550 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
102555 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
102564 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
102565 || (pDef->xValue==0 && pDef->xInverse==0)
102566 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
102568 if( pDef && pDef->xValue==0 && pWin ){
102572 pNC->nNcErr++;
102574 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
102575 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
102576 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
102579 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
102585 pNC->nNcErr++;
102589 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
102591 pNC->nNcErr++;
102595 else if( no_such_func && pParse->db->init.busy==0
102597 && pParse->explain==0
102601 pNC->nNcErr++;
102605 pNC->nNcErr++;
102610 "FILTER may not be used with non-aggregate %#T()",
102613 pNC->nNcErr++;
102621 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
102623 pNC->ncFlags &= ~NC_AllowAgg;
102636 Select *pSel = pNC->pWinSelect;
102637 assert( pWin==0 || (ExprUseYWin(pExpr) && pWin==pExpr->y.pWin) );
102639 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
102640 if( pParse->db->mallocFailed ) break;
102642 sqlite3WalkExprList(pWalker, pWin->pPartition);
102643 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
102644 sqlite3WalkExpr(pWalker, pWin->pFilter);
102646 pNC->ncFlags |= NC_HasWin;
102651 pExpr->op = TK_AGG_FUNCTION;
102652 pExpr->op2 = 0;
102655 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
102660 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
102662 pExpr->op2++;
102663 pNC2 = pNC2->pNext;
102669 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
102670 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
102671 pNC2->ncFlags |= NC_HasAgg
102672 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
102676 pNC->ncFlags |= savedAllowFlags;
102678 /* FIX ME: Compute pExpr->affinity based on the expected return
102685 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
102688 testcase( pExpr->op==TK_IN );
102690 int nRef = pNC->nRef;
102691 testcase( pNC->ncFlags & NC_IsCheck );
102692 testcase( pNC->ncFlags & NC_PartIdx );
102693 testcase( pNC->ncFlags & NC_IdxExpr );
102694 testcase( pNC->ncFlags & NC_GenCol );
102695 if( pNC->ncFlags & NC_SelfRef ){
102698 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
102700 assert( pNC->nRef>=nRef );
102701 if( nRef!=pNC->nRef ){
102703 pNC->ncFlags |= NC_VarSelect;
102709 testcase( pNC->ncFlags & NC_IsCheck );
102710 testcase( pNC->ncFlags & NC_PartIdx );
102711 testcase( pNC->ncFlags & NC_IdxExpr );
102712 testcase( pNC->ncFlags & NC_GenCol );
102719 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
102723 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
102726 if( pRight->op==TK_TRUEFALSE ){
102727 pExpr->op2 = pExpr->op;
102728 pExpr->op = TK_TRUTH;
102742 if( pParse->db->mallocFailed ) break;
102743 assert( pExpr->pLeft!=0 );
102744 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
102745 if( pExpr->op==TK_BETWEEN ){
102747 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
102749 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
102752 assert( pExpr->pRight!=0 );
102753 nRight = sqlite3ExprVectorSize(pExpr->pRight);
102756 testcase( pExpr->op==TK_EQ );
102757 testcase( pExpr->op==TK_NE );
102758 testcase( pExpr->op==TK_LT );
102759 testcase( pExpr->op==TK_LE );
102760 testcase( pExpr->op==TK_GT );
102761 testcase( pExpr->op==TK_GE );
102762 testcase( pExpr->op==TK_IS );
102763 testcase( pExpr->op==TK_ISNOT );
102764 testcase( pExpr->op==TK_BETWEEN );
102766 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
102771 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
102772 return pParse->nErr ? WRC_Abort : WRC_Continue;
102779 ** to the AS-name of one of the terms of the expression list. If it is,
102796 if( pE->op==TK_ID ){
102799 zCol = pE->u.zToken;
102800 for(i=0; i<pEList->nExpr; i++){
102801 if( pEList->a[i].fg.eEName==ENAME_NAME
102802 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
102820 ** Attempt to match pE against result set columns in the left-most
102822 ** as an indication to the caller that it should sort by the i-th column.
102823 ** The left-most column is 1. In other words, the value returned is the
102827 ** If there is no match, return 0. Return -1 if an error occurs.
102839 u8 savedSuppErr; /* Saved value of db->suppressErr */
102842 pEList = pSelect->pEList;
102848 nc.pSrcList = pSelect->pSrc;
102852 db = pParse->db;
102853 savedSuppErr = db->suppressErr;
102854 db->suppressErr = 1;
102856 db->suppressErr = savedSuppErr;
102860 ** in the result set. Return an 1-based index of the matching
102861 ** result-set entry.
102863 for(i=0; i<pEList->nExpr; i++){
102864 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
102874 ** Generate an ORDER BY or GROUP BY term out-of-range error.
102879 int i, /* The index (1-based) of the term out of range */
102884 "%r %s BY term out of range - should be "
102886 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
102898 ** beginning with the left-most SELECT and working toward the right.
102914 pOrderBy = pSelect->pOrderBy;
102916 db = pParse->db;
102917 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
102921 for(i=0; i<pOrderBy->nExpr; i++){
102922 pOrderBy->a[i].fg.done = 0;
102924 pSelect->pNext = 0;
102925 while( pSelect->pPrior ){
102926 pSelect->pPrior->pNext = pSelect;
102927 pSelect = pSelect->pPrior;
102932 pEList = pSelect->pEList;
102934 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
102935 int iCol = -1;
102937 if( pItem->fg.done ) continue;
102938 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
102941 if( iCol<=0 || iCol>pEList->nExpr ){
102942 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
102960 if( !db->mallocFailed ){
102976 pNew->flags |= EP_IntValue;
102977 pNew->u.iValue = iCol;
102978 if( pItem->pExpr==pE ){
102979 pItem->pExpr = pNew;
102981 Expr *pParent = pItem->pExpr;
102982 assert( pParent->op==TK_COLLATE );
102983 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
102984 assert( pParent->pLeft==pE );
102985 pParent->pLeft = pNew;
102988 pItem->u.x.iOrderByCol = (u16)iCol;
102990 pItem->fg.done = 1;
102995 pSelect = pSelect->pNext;
102997 for(i=0; i<pOrderBy->nExpr; i++){
102998 if( pOrderBy->a[i].fg.done==0 ){
103014 ** If any errors are detected, add an error message to pParse and
103015 ** return non-zero. Return zero if no errors are seen.
103024 sqlite3 *db = pParse->db;
103028 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
103029 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
103033 pEList = pSelect->pEList;
103035 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103036 if( pItem->u.x.iOrderByCol ){
103037 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
103038 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
103041 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
103054 Window *pWin = pExpr->y.pWin;
103065 if( pSelect->pWin ){
103083 ** If the order-by term is an integer I between 1 and N (where N is the
103085 ** in the resolution is a copy of the I-th result-set expression. If
103086 ** the order-by term is an identifier that corresponds to the AS-name of
103087 ** a result-set expression, then the term resolves to a copy of the
103088 ** result-set expression. Otherwise, the expression is resolved in
103089 ** the usual way - using sqlite3ResolveExprNames().
103092 ** an appropriate error message might be left in pParse. (OOM errors
103108 nResult = pSelect->pEList->nExpr;
103109 pParse = pNC->pParse;
103110 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
103111 Expr *pE = pItem->pExpr;
103115 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
103117 /* If an AS-name match is found, mark this ORDER BY column as being
103118 ** a copy of the iCol-th result-set column. The subsequent call to
103120 ** copy of the iCol-th result-set expression. */
103121 pItem->u.x.iOrderByCol = (u16)iCol;
103128 ** order-by term to a copy of the result-set expression */
103133 pItem->u.x.iOrderByCol = (u16)iCol;
103138 pItem->u.x.iOrderByCol = 0;
103142 for(j=0; j<pSelect->pEList->nExpr; j++){
103143 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
103148 pItem->u.x.iOrderByCol = j+1;
103166 Select *pLeftmost; /* Left-most of SELECT of a compound */
103171 if( p->selFlags & SF_Resolved ){
103174 pOuterNC = pWalker->u.pNC;
103175 pParse = pWalker->pParse;
103176 db = pParse->db;
103186 if( (p->selFlags & SF_Expanded)==0 ){
103188 return pParse->nErr ? WRC_Abort : WRC_Prune;
103191 isCompound = p->pPrior!=0;
103195 assert( (p->selFlags & SF_Expanded)!=0 );
103196 assert( (p->selFlags & SF_Resolved)==0 );
103197 assert( db->suppressErr==0 ); /* SF_Resolved not set if errors suppressed */
103198 p->selFlags |= SF_Resolved;
103207 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
103213 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
103214 ** as if it were part of the sub-query, not the parent. This block
103215 ** moves the pOrderBy down to the sub-query. It will be moved back
103217 if( p->selFlags & SF_Converted ){
103218 Select *pSub = p->pSrc->a[0].pSelect;
103219 assert( p->pSrc->nSrc==1 && p->pOrderBy );
103220 assert( pSub->pPrior && pSub->pOrderBy==0 );
103221 pSub->pOrderBy = p->pOrderBy;
103222 p->pOrderBy = 0;
103227 for(i=0; i<p->pSrc->nSrc; i++){
103228 SrcItem *pItem = &p->pSrc->a[i];
103229 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
103230 int nRef = pOuterNC ? pOuterNC->nRef : 0;
103231 const char *zSavedContext = pParse->zAuthContext;
103233 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
103234 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
103235 pParse->zAuthContext = zSavedContext;
103236 if( pParse->nErr ) return WRC_Abort;
103237 assert( db->mallocFailed==0 );
103240 ** expressions in the sub-select were resolved, the sub-select
103246 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
103247 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
103252 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
103253 ** resolve the result-set expression list.
103256 sNC.pSrcList = p->pSrc;
103260 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
103263 /* If there are no aggregate functions in the result-set, and no GROUP BY
103266 assert( (p->selFlags & SF_Aggregate)==0 );
103267 pGroupBy = p->pGroupBy;
103271 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
103276 /* Add the output column list to the name-context before parsing the
103282 ** re-evaluated for each reference to it.
103285 sNC.uNC.pEList = p->pEList;
103287 if( p->pHaving ){
103288 if( (p->selFlags & SF_Aggregate)==0 ){
103289 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
103292 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
103294 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
103296 /* Resolve names in table-valued-function arguments */
103297 for(i=0; i<p->pSrc->nSrc; i++){
103298 SrcItem *pItem = &p->pSrc->a[i];
103299 if( pItem->fg.isTabFunc
103300 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
103309 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
103310 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
103311 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
103326 ** the sub-query back to the parent query. At this point each term
103330 if( p->selFlags & SF_Converted ){
103331 Select *pSub = p->pSrc->a[0].pSelect;
103332 p->pOrderBy = pSub->pOrderBy;
103333 pSub->pOrderBy = 0;
103338 ** below, after all of the result-sets for all of the elements of
103341 ** If there is an ORDER BY clause on a term of a compound-select other
103342 ** than the right-most term, then that is a syntax error. But the error
103346 if( p->pOrderBy!=0
103347 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
103348 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
103352 if( db->mallocFailed ){
103363 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
103366 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
103367 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
103377 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
103378 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
103384 p = p->pPrior;
103400 ** table columns and result-set columns. At the same time, do error
103408 ** the symbolic name assigned to an ATTACH-ed database.
103419 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
103423 ** To resolve result-set references, look for expression nodes of the
103424 ** form Z (with no X and Y prefix) where the Z matches the right-hand
103425 ** size of an AS clause in the result-set of a SELECT. The Z expression
103426 ** is replaced by a copy of the left-hand side of the result-set expression.
103427 ** Table-name and function resolution occurs on the substituted expression
103443 ** An error message is left in pParse if anything is amiss. The number
103454 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103455 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103456 w.pParse = pNC->pParse;
103458 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
103462 w.pParse->nHeight += pExpr->nHeight;
103463 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103469 w.pParse->nHeight -= pExpr->nHeight;
103473 testcase( pNC->ncFlags & NC_HasAgg );
103474 testcase( pNC->ncFlags & NC_HasWin );
103475 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
103476 pNC->ncFlags |= savedHasAgg;
103477 return pNC->nNcErr>0 || w.pParse->nErr>0;
103493 w.pParse = pNC->pParse;
103498 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103499 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103500 for(i=0; i<pList->nExpr; i++){
103501 Expr *pExpr = pList->a[i].pExpr;
103504 w.pParse->nHeight += pExpr->nHeight;
103505 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
103511 w.pParse->nHeight -= pExpr->nHeight;
103515 testcase( pNC->ncFlags & NC_HasAgg );
103516 testcase( pNC->ncFlags & NC_HasWin );
103517 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
103518 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
103519 savedHasAgg |= pNC->ncFlags &
103521 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
103523 if( w.pParse->nErr>0 ) return WRC_Abort;
103525 pNC->ncFlags |= savedHasAgg;
103531 ** decendents of the SELECT, including compounds off of p->pPrior,
103562 ** ------------
103570 ** nodes of the expression is set to -1 and the Expr.iColumn value is
103573 ** Any errors cause an error message to be set in pParse.
103582 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
103583 NameContext sNC; /* Name context for pParse->pNewTable */
103593 sSrc.a[0].zName = pTab->zName;
103595 sSrc.a[0].iCursor = -1;
103596 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
103597 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
103636 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
103637 return pTab->aCol[iCol].affinity;
103644 ** or a sub-select with a column as the return value, then the
103659 assert( pExpr->op==TK_COLLATE
103660 || pExpr->op==TK_IF_NULL_ROW
103661 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
103662 pExpr = pExpr->pLeft;
103665 op = pExpr->op;
103666 if( op==TK_REGISTER ) op = pExpr->op2;
103669 if( pExpr->y.pTab ){
103670 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
103675 assert( pExpr->x.pSelect!=0 );
103676 assert( pExpr->x.pSelect->pEList!=0 );
103677 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
103678 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
103683 return sqlite3AffinityType(pExpr->u.zToken, 0);
103687 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
103688 assert( pExpr->iColumn < pExpr->iTable );
103689 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
103691 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
103696 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
103698 return pExpr->affExpr;
103706 ** If a memory allocation error occurs, that fact is recorded in pParse->db
103715 if( pCollName->n>0 ){
103716 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
103718 pNew->pLeft = pExpr;
103719 pNew->flags |= EP_Collate|EP_Skip;
103741 assert( pExpr->op==TK_COLLATE );
103742 pExpr = pExpr->pLeft;
103756 assert( pExpr->x.pList->nExpr>0 );
103757 assert( pExpr->op==TK_FUNCTION );
103758 pExpr = pExpr->x.pList->a[0].pExpr;
103760 assert( pExpr->op==TK_COLLATE );
103761 pExpr = pExpr->pLeft;
103782 sqlite3 *db = pParse->db;
103786 int op = p->op;
103787 if( op==TK_REGISTER ) op = p->op2;
103790 if( p->y.pTab!=0 ){
103791 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
103793 int j = p->iColumn;
103795 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
103802 p = p->pLeft;
103807 p = p->x.pList->a[0].pExpr;
103812 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
103815 if( p->flags & EP_Collate ){
103816 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
103817 p = p->pLeft;
103819 Expr *pNext = p->pRight;
103822 assert( p->x.pList==0 || p->pRight==0 );
103823 if( p->x.pList!=0 && !db->mallocFailed ){
103825 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
103826 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
103827 pNext = p->x.pList->a[i].pExpr;
103856 if( p==0 ) p = pParse->db->pDfltColl;
103867 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
103899 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
103900 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
103901 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
103902 assert( pExpr->pLeft );
103903 aff = sqlite3ExprAffinity(pExpr->pLeft);
103904 if( pExpr->pRight ){
103905 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
103907 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
103964 if( pLeft->flags & EP_Collate ){
103966 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
103987 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
103989 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
104010 if( pParse->nErr ) return 0;
104017 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
104019 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
104039 ** is a sub-select, return the number of columns in the sub-select. For
104043 u8 op = pExpr->op;
104044 if( op==TK_REGISTER ) op = pExpr->op2;
104047 return pExpr->x.pList->nExpr;
104050 return pExpr->x.pSelect->pEList->nExpr;
104057 ** Return a pointer to a subexpression of pVector that is the i-th
104067 ** just the expression for the i-th term of the result set, and may
104072 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
104074 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
104075 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
104077 return pVector->x.pSelect->pEList->a[i].pExpr;
104080 return pVector->x.pList->a[i].pExpr;
104089 ** the iField-th column of the vector expression pVector.
104109 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
104114 if( pVector->op==TK_SELECT ){
104122 ** pLeft->iTable: First in an array of register holding result, or 0
104134 pRet->iTable = nField;
104135 pRet->iColumn = iField;
104136 pRet->pLeft = pVector;
104139 if( pVector->op==TK_VECTOR ){
104142 ppVector = &pVector->x.pList->a[iField].pExpr;
104150 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
104158 ** sub-select returns more than one column, the first in an array
104166 if( pExpr->op==TK_SELECT ){
104174 ** Argument pVector points to a vector expression - either a TK_VECTOR
104182 ** containing the results of the sub-select.
104199 u8 op = pVector->op;
104203 return pVector->iTable+iField;
104207 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
104212 *ppExpr = pVector->x.pList->a[iField].pExpr;
104225 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
104226 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
104227 ** otherwise: op==pExpr->op and p5==0
104236 Vdbe *v = pParse->pVdbe;
104237 Expr *pLeft = pExpr->pLeft;
104238 Expr *pRight = pExpr->pRight;
104249 if( pParse->nErr ) return;
104254 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
104255 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
104256 || pExpr->op==TK_LT || pExpr->op==TK_GT
104257 || pExpr->op==TK_LE || pExpr->op==TK_GE
104259 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
104260 || (pExpr->op==TK_ISNOT && op==TK_NE) );
104261 assert( p5==0 || pExpr->op!=op );
104262 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
104274 Expr *pL = 0, *pR = 0;
104279 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2);
104281 codeCompare(pParse, pL, pR, opx, r1, r2, addrDone, p5, isCommuted);
104290 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
104300 if( i==nLeft-1 ){
104308 if( i==nLeft-2 ) opx = op;
104321 ** expression depth allowed. If it is not, leave an error message in
104326 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
104347 if( p->nHeight>*pnHeight ){
104348 *pnHeight = p->nHeight;
104355 for(i=0; i<p->nExpr; i++){
104356 heightOfExpr(p->a[i].pExpr, pnHeight);
104362 for(p=pSelect; p; p=p->pPrior){
104363 heightOfExpr(p->pWhere, pnHeight);
104364 heightOfExpr(p->pHaving, pnHeight);
104365 heightOfExpr(p->pLimit, pnHeight);
104366 heightOfExprList(p->pEList, pnHeight);
104367 heightOfExprList(p->pGroupBy, pnHeight);
104368 heightOfExprList(p->pOrderBy, pnHeight);
104383 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
104384 if( p->pRight && p->pRight->nHeight>nHeight ) nHeight = p->pRight->nHeight;
104386 heightOfSelect(p->x.pSelect, &nHeight);
104387 }else if( p->x.pList ){
104388 heightOfExprList(p->x.pList, &nHeight);
104389 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
104391 p->nHeight = nHeight + 1;
104403 if( pParse->nErr ) return;
104405 sqlite3ExprCheckHeight(pParse, p->nHeight);
104423 if( pParse->nErr ) return;
104424 if( p && ExprUseXList(p) && p->x.pList ){
104425 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
104442 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
104446 ** can be translated into a 32-bit integer, then the token is not
104463 if( op!=TK_INTEGER || pToken->z==0
104464 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
104465 nExtra = pToken->n+1;
104472 pNew->op = (u8)op;
104473 pNew->iAgg = -1;
104476 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
104477 pNew->u.iValue = iValue;
104479 pNew->u.zToken = (char*)&pNew[1];
104480 assert( pToken->z!=0 || pToken->n==0 );
104481 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
104482 pNew->u.zToken[pToken->n] = 0;
104483 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
104489 pNew->nHeight = 1;
104496 ** Allocate a new expression node from a zero-terminated token that has
104523 assert( db->mallocFailed );
104528 pRoot->pRight = pRight;
104529 pRoot->flags |= EP_Propagate & pRight->flags;
104532 pRoot->pLeft = pLeft;
104533 pRoot->flags |= EP_Propagate & pLeft->flags;
104543 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
104553 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
104556 p->op = op & 0xff;
104557 p->iAgg = -1;
104558 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
104559 sqlite3ExprCheckHeight(pParse, p->nHeight);
104561 sqlite3ExprDelete(pParse->db, pLeft);
104562 sqlite3ExprDelete(pParse->db, pRight);
104573 pExpr->x.pSelect = pSelect;
104577 assert( pParse->db->mallocFailed );
104578 sqlite3SelectDelete(pParse->db, pSelect);
104596 ** an error message is left in pParse.
104605 for(ii=0; ii<pEList->nExpr; ii++){
104607 Expr *pExpr = pEList->a[ii].pExpr;
104609 if( pExpr->op==TK_VECTOR ){
104611 nExprElem = pExpr->x.pList->nExpr;
104616 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
104622 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
104623 pExpr->x.pList = 0;
104626 pSel->op = TK_ALL;
104627 pSel->pPrior = pRet;
104633 if( pRet && pRet->pPrior ){
104634 pRet->selFlags |= SF_MultiValue;
104636 sqlite3ExprListDelete(pParse->db, pEList);
104649 sqlite3 *db = pParse->db;
104676 sqlite3 *db = pParse->db;
104684 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
104686 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
104687 && !pParse->nested
104691 pNew->x.pList = pList;
104703 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
104705 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
104706 ** top-level SQL
104716 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
104718 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
104719 || (pParse->db->flags & SQLITE_TrustedSchema)==0
104750 sqlite3 *db = pParse->db;
104756 z = pExpr->u.zToken;
104763 x = (ynVar)(++pParse->nVar);
104771 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
104772 i = z[1]-'0'; /* The common case of ?N for a single digit N */
104775 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
104779 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
104780 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
104781 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
104783 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
104784 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104788 if( x>pParse->nVar ){
104789 pParse->nVar = (int)x;
104791 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
104799 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
104801 x = (ynVar)(++pParse->nVar);
104806 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
104809 pExpr->iColumn = x;
104810 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
104812 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
104821 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
104823 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
104824 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
104827 assert( p->pLeft==0 );
104828 assert( p->pRight==0 );
104829 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
104830 assert( !ExprUseXList(p) || p->x.pList==0 );
104835 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
104836 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
104837 if( p->pRight ){
104839 sqlite3ExprDeleteNN(db, p->pRight);
104842 sqlite3SelectDelete(db, p->x.pSelect);
104844 sqlite3ExprListDelete(db, p->x.pList);
104847 sqlite3WindowDelete(db, p->y.pWin);
104854 sqlite3DbFree(db, p->u.zToken);
104870 }else if( p->pOn ){
104871 sqlite3ExprDeleteNN(db, p->pOn);
104872 }else if( p->pUsing ){
104873 sqlite3IdListDelete(db, p->pUsing);
104885 ** pExpr to the pParse->pConstExpr list with a register number of 0.
104888 pParse->pConstExpr =
104889 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
104900 sqlite3ExprDeleteNN(pParse->db, p);
104926 ** The dupedExprStructSize() function returns two values OR-ed together:
104939 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
104954 if( 0==flags || p->op==TK_SELECT_COLUMN
104965 if( p->pLeft || p->x.pList ){
104968 assert( p->pRight==0 );
104982 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
104983 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
105006 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
105015 ** to store the copy of expression p, the copies of p->u.zToken
105016 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
105045 ** by the copy of the p->u.zToken string (if any).
105050 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
105051 nToken = sqlite3Strlen30(p->u.zToken) + 1;
105062 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
105067 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
105068 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
105069 pNew->flags |= staticFlag;
105075 /* Copy the p->u.zToken string, if any. */
105077 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
105078 memcpy(zToken, p->u.zToken, nToken);
105081 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
105082 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
105084 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
105086 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
105090 /* Fill in pNew->pLeft and pNew->pRight. */
105094 pNew->pLeft = p->pLeft ?
105095 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
105096 pNew->pRight = p->pRight ?
105097 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
105101 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
105110 if( pNew->op==TK_SELECT_COLUMN ){
105111 pNew->pLeft = p->pLeft;
105112 assert( p->pRight==0 || p->pRight==p->pLeft
105113 || ExprHasProperty(p->pLeft, EP_Subquery) );
105115 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
105117 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
105127 ** and the db->mallocFailed flag set.
105133 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
105137 pRet->nCte = p->nCte;
105138 for(i=0; i<p->nCte; i++){
105139 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
105140 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
105141 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
105142 pRet->a[i].eM10d = p->a[i].eM10d;
105157 ** objects found there, assembling them onto the linked list at Select->pWin.
105160 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
105161 Select *pSelect = pWalker->u.pSelect;
105162 Window *pWin = pExpr->y.pWin;
105165 assert( pWin->ppThis==0 );
105171 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
105200 ** part of the in-memory representation of the database schema.
105217 pNew->nExpr = p->nExpr;
105218 pNew->nAlloc = p->nAlloc;
105219 pItem = pNew->a;
105220 pOldItem = p->a;
105221 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
105222 Expr *pOldExpr = pOldItem->pExpr;
105224 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
105226 && pOldExpr->op==TK_SELECT_COLUMN
105227 && (pNewExpr = pItem->pExpr)!=0
105229 if( pNewExpr->pRight ){
105230 pPriorSelectColOld = pOldExpr->pRight;
105231 pPriorSelectColNew = pNewExpr->pRight;
105232 pNewExpr->pLeft = pNewExpr->pRight;
105234 if( pOldExpr->pLeft!=pPriorSelectColOld ){
105235 pPriorSelectColOld = pOldExpr->pLeft;
105237 pNewExpr->pRight = pPriorSelectColNew;
105239 pNewExpr->pLeft = pPriorSelectColNew;
105242 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
105243 pItem->fg = pOldItem->fg;
105244 pItem->fg.done = 0;
105245 pItem->u = pOldItem->u;
105264 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
105267 pNew->nSrc = pNew->nAlloc = p->nSrc;
105268 for(i=0; i<p->nSrc; i++){
105269 SrcItem *pNewItem = &pNew->a[i];
105270 const SrcItem *pOldItem = &p->a[i];
105272 pNewItem->pSchema = pOldItem->pSchema;
105273 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
105274 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105275 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
105276 pNewItem->fg = pOldItem->fg;
105277 pNewItem->iCursor = pOldItem->iCursor;
105278 pNewItem->addrFillSub = pOldItem->addrFillSub;
105279 pNewItem->regReturn = pOldItem->regReturn;
105280 if( pNewItem->fg.isIndexedBy ){
105281 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
105283 pNewItem->u2 = pOldItem->u2;
105284 if( pNewItem->fg.isCte ){
105285 pNewItem->u2.pCteUse->nUse++;
105287 if( pNewItem->fg.isTabFunc ){
105288 pNewItem->u1.pFuncArg =
105289 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
105291 pTab = pNewItem->pTab = pOldItem->pTab;
105293 pTab->nTabRef++;
105295 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
105296 if( pOldItem->fg.isUsing ){
105297 assert( pNewItem->fg.isUsing );
105298 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
105300 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
105302 pNewItem->colUsed = pOldItem->colUsed;
105311 assert( p->eU4!=EU4_EXPR );
105312 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew)+(p->nId-1)*sizeof(p->a[0]) );
105314 pNew->nId = p->nId;
105315 pNew->eU4 = p->eU4;
105316 for(i=0; i<p->nId; i++){
105317 struct IdList_item *pNewItem = &pNew->a[i];
105318 const struct IdList_item *pOldItem = &p->a[i];
105319 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
105320 pNewItem->u4 = pOldItem->u4;
105331 for(p=pDup; p; p=p->pPrior){
105334 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
105335 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
105336 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
105337 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
105338 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
105339 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
105340 pNew->op = p->op;
105341 pNew->pNext = pNext;
105342 pNew->pPrior = 0;
105343 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
105344 pNew->iLimit = 0;
105345 pNew->iOffset = 0;
105346 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
105347 pNew->addrOpenEphm[0] = -1;
105348 pNew->addrOpenEphm[1] = -1;
105349 pNew->nSelectRow = p->nSelectRow;
105350 pNew->pWith = sqlite3WithDup(db, p->pWith);
105352 pNew->pWin = 0;
105353 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
105354 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
105356 pNew->selId = p->selId;
105357 if( db->mallocFailed ){
105361 pNew->pNext = 0;
105366 pp = &pNew->pPrior;
105387 ** Reason: This routine assumes that the number of slots in pList->a[]
105392 ** NULL is returned. If non-NULL is returned, then it is guaranteed
105403 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList)+sizeof(pList->a[0])*4 );
105408 pList->nAlloc = 4;
105409 pList->nExpr = 1;
105410 pItem = &pList->a[0];
105412 pItem->pExpr = pExpr;
105422 pList->nAlloc *= 2;
105424 sizeof(*pList)+(pList->nAlloc-1)*sizeof(pList->a[0]));
105432 pItem = &pList->a[pList->nExpr++];
105434 pItem->pExpr = pExpr;
105444 return sqlite3ExprListAppendNew(pParse->db,pExpr);
105446 if( pList->nAlloc<pList->nExpr+1 ){
105447 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
105449 pItem = &pList->a[pList->nExpr++];
105451 pItem->pExpr = pExpr;
105472 sqlite3 *db = pParse->db;
105475 int iFirst = pList ? pList->nExpr : 0;
105486 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
105488 pColumns->nId, n);
105492 for(i=0; i<pColumns->nId; i++){
105493 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
105494 assert( pSubExpr!=0 || db->mallocFailed );
105498 assert( pList->nExpr==iFirst+i+1 );
105499 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
105500 pColumns->a[i].zName = 0;
105504 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
105505 Expr *pFirst = pList->a[iFirst].pExpr;
105507 assert( pFirst->op==TK_SELECT_COLUMN );
105511 pFirst->pRight = pExpr;
105516 pFirst->iTable = pColumns->nId;
105531 assert( p->nExpr>0 );
105543 pItem = &p->a[p->nExpr-1];
105544 assert( pItem->fg.bNulls==0 );
105548 pItem->fg.sortFlags = (u8)iSortOrder;
105551 pItem->fg.bNulls = 1;
105553 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
105563 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105572 assert( pList!=0 || pParse->db->mallocFailed!=0 );
105573 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
105576 assert( pList->nExpr>0 );
105577 pItem = &pList->a[pList->nExpr-1];
105578 assert( pItem->zEName==0 );
105579 assert( pItem->fg.eEName==ENAME_NAME );
105580 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
105582 /* If dequote==0, then pName->z does not point to part of a DDL
105584 ** to the token-map. */
105585 sqlite3Dequote(pItem->zEName);
105587 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
105598 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
105607 sqlite3 *db = pParse->db;
105608 assert( pList!=0 || db->mallocFailed!=0 );
105610 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
105611 assert( pList->nExpr>0 );
105612 if( pItem->zEName==0 ){
105613 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
105614 pItem->fg.eEName = ENAME_SPAN;
105621 ** leave an error message in pParse.
105628 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
105629 testcase( pEList && pEList->nExpr==mx );
105630 testcase( pEList && pEList->nExpr==mx+1 );
105631 if( pEList && pEList->nExpr>mx ){
105640 int i = pList->nExpr;
105641 struct ExprList_item *pItem = pList->a;
105642 assert( pList->nExpr>0 );
105644 sqlite3ExprDelete(db, pItem->pExpr);
105645 sqlite3DbFree(db, pItem->zEName);
105647 }while( --i>0 );
105655 ** Return the bitwise-OR of all Expr.flags fields in the given
105662 for(i=0; i<pList->nExpr; i++){
105663 Expr *pExpr = pList->a[i].pExpr;
105665 m |= pExpr->flags;
105671 ** This is a SELECT-node callback for the expression walker that
105673 ** pWalker->eCode to zero and abort.
105679 pWalker->eCode = 0;
105700 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
105705 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
105707 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
105709 pExpr->op = TK_TRUEFALSE;
105722 assert( pExpr->op==TK_TRUEFALSE );
105724 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
105725 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
105726 return pExpr->u.zToken[4]==0;
105744 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
105745 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
105746 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
105748 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
105750 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
105765 ** sqlite3ExprIsConstant() pWalker->eCode==1
105766 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
105767 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
105768 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
105785 /* If pWalker->eCode is 2 then any term of the expression that comes from
105788 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
105789 pWalker->eCode = 0;
105793 switch( pExpr->op ){
105795 ** and either pWalker->eCode==4 or 5 or the function has the
105798 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
105801 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
105804 pWalker->eCode = 0;
105817 testcase( pExpr->op==TK_ID );
105818 testcase( pExpr->op==TK_COLUMN );
105819 testcase( pExpr->op==TK_AGG_FUNCTION );
105820 testcase( pExpr->op==TK_AGG_COLUMN );
105821 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
105824 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
105831 testcase( pExpr->op==TK_REGISTER );
105832 testcase( pExpr->op==TK_IF_NULL_ROW );
105833 testcase( pExpr->op==TK_DOT );
105834 pWalker->eCode = 0;
105837 if( pWalker->eCode==5 ){
105841 pExpr->op = TK_NULL;
105842 }else if( pWalker->eCode==4 ){
105845 pWalker->eCode = 0;
105850 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
105851 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
105869 ** Walk an expression tree. Return non-zero if the expression is constant
105872 ** For the purposes of this function, a double-quoted string (ex: "abc")
105873 ** is considered a variable but a single-quoted string (ex: 'abc') is
105881 ** Walk an expression tree. Return non-zero if
105890 ** can be added to the pParse->pConstExpr list and evaluated once when
105898 ** Walk an expression tree. Return non-zero if the expression is constant
105900 ** expression must not refer to any non-deterministic function nor any
105915 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
105917 ** (2) pExpr cannot use subqueries or non-deterministic functions.
105931 if( pSrc->fg.jointype & JT_LTORJ ){
105934 if( pSrc->fg.jointype & JT_LEFT ){
105936 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
105940 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor); /* rules (1), (2) */
105948 ExprList *pGroupBy = pWalker->u.pGroupBy;
105953 for(i=0; i<pGroupBy->nExpr; i++){
105954 Expr *p = pGroupBy->a[i].pExpr;
105955 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
105956 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
105963 /* Check if pExpr is a sub-select. If so, consider it variable. */
105965 pWalker->eCode = 0;
105973 ** Walk the expression tree passed as the first argument. Return non-zero
105981 ** assumes that no other collating sequence will have a finer-grained
106004 ** in a CREATE TABLE statement. Return non-zero if the expression is
106005 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
106019 ** For the purposes of this function, a double-quoted string (ex: "abc")
106020 ** is considered a variable but a single-quoted string (ex: 'abc') is
106048 ** to fit in a 32-bit integer, return 1 and put the value of the integer
106050 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
106056 /* If an expression is an integer literal that fits in a signed 32-bit
106058 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
106059 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
106061 if( p->flags & EP_IntValue ){
106062 *pValue = p->u.iValue;
106065 switch( p->op ){
106067 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
106072 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
106074 *pValue = -v;
106101 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
106102 p = p->pLeft;
106105 op = p->op;
106106 if( op==TK_REGISTER ) op = p->op2;
106116 p->y.pTab==0 || /* Reference to column of index on expression */
106117 (p->iColumn>=0
106118 && p->y.pTab->aCol!=0 /* Possible due to prior error */
106119 && p->y.pTab->aCol[p->iColumn].notNull==0);
106139 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
106140 if( p->op==TK_UMINUS ) unaryMinus = 1;
106141 p = p->pLeft;
106143 op = p->op;
106144 if( op==TK_REGISTER ) op = p->op2;
106159 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
106160 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
106169 ** Return TRUE if the given string is a row-id column name.
106194 p = pX->x.pSelect;
106195 if( p->pPrior ) return 0; /* Not a compound SELECT */
106196 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
106197 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
106198 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
106201 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
106202 if( p->pLimit ) return 0; /* Has no LIMIT clause */
106203 if( p->pWhere ) return 0; /* Has no WHERE clause */
106204 pSrc = p->pSrc;
106206 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
106207 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
106208 pTab = pSrc->a[0].pTab;
106212 pEList = p->pEList;
106215 for(i=0; i<pEList->nExpr; i++){
106216 Expr *pRes = pEList->a[i].pExpr;
106217 if( pRes->op!=TK_COLUMN ) return 0;
106218 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
106226 ** Generate code that checks the left-most column of index table iCur to see if
106228 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
106246 ** right-hand side. Return TRUE if that list is constant.
106252 pLHS = pIn->pLeft;
106253 pIn->pLeft = 0;
106255 pIn->pLeft = pLHS;
106265 ** The job of this routine is to find or create a b-tree object that can
106269 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
106272 ** The returned value of this function indicates the b-tree type, as follows:
106274 ** IN_INDEX_ROWID - The cursor was opened on a database table.
106275 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
106276 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
106277 ** IN_INDEX_EPH - The cursor was opened on a specially created and
106279 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
106282 ** An existing b-tree might be used if the RHS expression pX is a simple
106289 ** pX->iTable made to point to the ephemeral table instead of an
106292 ** will be set on pX and the pX->y.sub fields will be set to show where
106301 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
106302 ** through the set members) then the b-tree must not contain duplicates.
106304 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
106307 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
106310 ** index can be found with the specified <columns> as its left-most.
106314 ** routine might decide that creating an ephemeral b-tree for membership
106319 ** When the b-tree is being used for membership tests, the calling function
106328 ** the value in that register will be NULL if the b-tree contains one or more
106329 ** NULL values, and it will be some non-NULL value if the b-tree contains no
106358 assert( pX->op==TK_IN );
106360 iTab = pParse->nTab++;
106369 ExprList *pEList = pX->x.pSelect->pEList;
106370 for(i=0; i<pEList->nExpr; i++){
106371 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
106373 if( i==pEList->nExpr ){
106381 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
106382 sqlite3 *db = pParse->db; /* Database connection */
106385 ExprList *pEList = p->pEList;
106386 int nExpr = pEList->nExpr;
106388 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
106389 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
106390 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
106391 pTab = p->pSrc->a[0].pTab;
106394 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
106397 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
106400 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
106408 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
106420 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106421 int iCol = pEList->a[i].pExpr->iColumn;
106443 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
106446 if( pIdx->nColumn<nExpr ) continue;
106447 if( pIdx->pPartIdxWhere!=0 ) continue;
106448 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
106450 testcase( pIdx->nColumn==BMS-2 );
106451 testcase( pIdx->nColumn==BMS-1 );
106452 if( pIdx->nColumn>=BMS-1 ) continue;
106454 if( pIdx->nKeyCol>nExpr
106455 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
106463 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
106464 Expr *pRhs = pEList->a[i].pExpr;
106468 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
106470 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
106471 assert( pIdx->azColl[j] );
106472 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
106484 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
106485 if( colUsed==(MASKBIT(nExpr)-1) ){
106489 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
106490 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
106492 VdbeComment((v, "%s", pIdx->zName));
106494 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
106498 i64 mask = (1<<nExpr)-1;
106502 *prRhsHasNull = ++pParse->nMem;
106524 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
106526 pParse->nTab--; /* Back out the allocation of the unused cursor */
106527 iTab = -1; /* Cursor is not allocated */
106532 /* Could not find an existing table or index to use as the RHS b-tree.
106535 u32 savedNQueryLoop = pParse->nQueryLoop;
106539 pParse->nQueryLoop = 0;
106541 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
106543 assert( pX->op==TK_IN );
106548 pParse->nQueryLoop = savedNQueryLoop;
106553 n = sqlite3ExprVectorSize(pX->pLeft);
106564 ** function allocates and returns a nul-terminated string containing
106571 Expr *pLeft = pExpr->pLeft;
106573 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
106576 assert( pExpr->op==TK_IN );
106577 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
106584 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
106598 ** message of the form:
106600 ** "sub-select returns N columns - expected M"
106603 if( pParse->nErr==0 ){
106604 const char *zFmt = "sub-select returns %d columns - expected %d";
106612 ** it is not permitted. If pExpr is a sub-select vector, this routine
106613 ** loads the Parse object with a message of the form:
106615 ** "sub-select returns N columns - expected 1"
106624 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
106638 ** x IN (4,5,11) -- IN operator with list on right-hand side
106639 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
106643 ** table is computed, the cursor number is also stored in pExpr->iTable,
106667 v = pParse->pVdbe;
106673 ** * The right-hand side is a correlated subquery
106674 ** * The right-hand side is an expression list containing variables
106680 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
106689 pExpr->x.pSelect->selId));
106692 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
106693 pExpr->y.sub.iAddr);
106694 assert( iTab!=pExpr->iTable );
106695 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
106704 pExpr->y.sub.regReturn = ++pParse->nMem;
106705 pExpr->y.sub.iAddr =
106706 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
106712 pLeft = pExpr->pLeft;
106718 pExpr->iTable = iTab;
106719 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
106722 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
106727 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
106735 Select *pSelect = pExpr->x.pSelect;
106736 ExprList *pEList = pSelect->pEList;
106739 addrOnce?"":"CORRELATED ", pSelect->selId
106743 if( ALWAYS(pEList->nExpr==nVal) ){
106750 pSelect->iLimit = 0;
106751 testcase( pSelect->selFlags & SF_Distinct );
106753 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
106754 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
106755 sqlite3SelectDelete(pParse->db, pCopy);
106756 sqlite3DbFree(pParse->db, dest.zAffSdst);
106763 assert( pEList->nExpr>0 );
106767 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
106768 pParse, p, pEList->a[i].pExpr
106772 }else if( ALWAYS(pExpr->x.pList!=0) ){
106782 ExprList *pList = pExpr->x.pList;
106793 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
106799 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
106800 Expr *pE2 = pItem->pExpr;
106804 ** this code only executes once. Because for a non-constant
106808 sqlite3VdbeChangeToNoop(v, addrOnce-1);
106829 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
106830 || pParse->nErr );
106831 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
106832 pExpr->y.sub.iAddr, 1);
106843 ** (SELECT a FROM b) -- subquery
106844 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
106848 ** Return the register that holds the result. For a multi-column SELECT,
106850 ** return value is the register of the left-most result column.
106862 Vdbe *v = pParse->pVdbe;
106864 if( pParse->nErr ) return 0;
106865 testcase( pExpr->op==TK_EXISTS );
106866 testcase( pExpr->op==TK_SELECT );
106867 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
106869 pSel = pExpr->x.pSelect;
106874 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
106876 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
106877 pExpr->y.sub.iAddr);
106878 return pExpr->iTable;
106885 pExpr->y.sub.regReturn = ++pParse->nMem;
106886 pExpr->y.sub.iAddr =
106887 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
106892 ** * The right-hand side is a correlated subquery
106893 ** * The right-hand side is an expression list containing variables
106914 addrOnce?"":"CORRELATED ", pSel->selId));
106915 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
106916 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
106917 pParse->nMem += nReg;
106918 if( pExpr->op==TK_SELECT ){
106922 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
106929 if( pSel->pLimit ){
106930 /* The subquery already has a limit. If the pre-existing limit is X
106932 sqlite3 *db = pParse->db;
106935 pLimit->affExpr = SQLITE_AFF_NUMERIC;
106937 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
106939 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
106940 pSel->pLimit->pLeft = pLimit;
106942 /* If there is no pre-existing limit add a limit of 1 */
106943 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
106944 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
106946 pSel->iLimit = 0;
106948 pExpr->op2 = pExpr->op;
106949 pExpr->op = TK_ERROR;
106952 pExpr->iTable = rReg = dest.iSDParm;
106960 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
106961 || pParse->nErr );
106962 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
106963 pExpr->y.sub.iAddr, 1);
106973 ** sub-select on the RHS of the IN() operator has the same number of
106975 ** a sub-query, that the LHS is a vector of size 1.
106978 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
106979 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
106980 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
106981 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
106985 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
106999 ** The left-hand side (LHS) is a scalar or vector expression. The
107000 ** right-hand side (RHS) is an array of zero or more scalar values, or a
107015 ** See the separate in-operator.md documentation file in the canonical
107039 int addrTop; /* Top of the step-6 loop */
107041 u8 okConstFactor = pParse->okConstFactor;
107044 pLeft = pExpr->pLeft;
107047 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
107049 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
107051 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
107057 v = pParse->pVdbe;
107065 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
107070 ** nVector-1. */
107090 assert( pParse->okConstFactor==okConstFactor );
107091 pParse->okConstFactor = 0;
107093 pParse->okConstFactor = okConstFactor;
107110 ** This is step (1) in the in-operator.md optimized algorithm.
107120 pList = pExpr->x.pList;
107121 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
107126 for(ii=0; ii<pList->nExpr; ii++){
107127 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree);
107128 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
107132 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
107136 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
107137 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
107138 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
107139 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
107170 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
107171 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
107178 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
107183 /* In this case, the RHS is the ROWID of table b-tree and so we also
107184 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
107202 /* Step 4. If the RHS is known to be non-NULL and we did not find
107262 sqlite3DbFree(pParse->db, aiMap);
107263 sqlite3DbFree(pParse->db, zAff);
107270 ** value described by z[0..n-1] into register iMem.
107272 ** The z[] string will probably not be zero-terminated. But the
107281 if( negateFlag ) value = -value;
107290 ** text z[0..n-1] into register iMem.
107292 ** Expr.u.zToken is always UTF8 and zero-terminated.
107295 Vdbe *v = pParse->pVdbe;
107296 if( pExpr->flags & EP_IntValue ){
107297 int i = pExpr->u.iValue;
107299 if( negFlag ) i = -i;
107304 const char *z = pExpr->u.zToken;
107309 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
107314 negFlag?"-":"",pExpr);
107322 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
107330 ** appropriate for the iIdxCol-th column of index pIdx.
107339 i16 iTabCol = pIdx->aiColumn[iIdxCol];
107341 assert( pIdx->aColExpr );
107342 assert( pIdx->aColExpr->nExpr>iIdxCol );
107343 pParse->iSelfTab = iTabCur + 1;
107344 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
107345 pParse->iSelfTab = 0;
107347 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
107364 Vdbe *v = pParse->pVdbe;
107366 assert( pParse->iSelfTab!=0 );
107367 if( pParse->iSelfTab>0 ){
107368 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
107373 if( pCol->affinity>=SQLITE_AFF_TEXT ){
107374 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
107381 ** Generate code to extract the value of the iCol-th column of a table.
107396 if( iCol<0 || iCol==pTab->iPKey ){
107398 VdbeComment((v, "%s.rowid", pTab->zName));
107406 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
107408 if( pCol->colFlags & COLFLAG_BUSY ){
107410 pCol->zCnName);
107412 int savedSelfTab = pParse->iSelfTab;
107413 pCol->colFlags |= COLFLAG_BUSY;
107414 pParse->iSelfTab = iTabCur+1;
107416 pParse->iSelfTab = savedSelfTab;
107417 pCol->colFlags &= ~COLFLAG_BUSY;
107436 ** Generate code that will extract the iColumn-th column from
107450 assert( pParse->pVdbe!=0 );
107451 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
107453 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
107454 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
107460 ** Generate code to move content from registers iFrom...iFrom+nReg-1
107461 ** over to iTo..iTo+nReg-1.
107464 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
107475 p->op2 = p->op;
107476 p->op = TK_REGISTER;
107477 p->iTable = iReg;
107498 if( p->op==TK_SELECT ){
107506 iResult = pParse->nMem+1;
107507 pParse->nMem += nResult;
107510 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
107518 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
107522 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
107529 ** in-line rather than by using the usual callbacks.
107538 Vdbe *v = pParse->pVdbe;
107541 nFarg = pFarg->nExpr;
107542 assert( nFarg>0 ); /* All in-line functions have at least one argument */
107545 /* Attempt a direct implementation of the built-in COALESCE() and
107547 ** arguments past the first non-NULL argument.
107552 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
107556 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
107571 Expr *pArg = pFarg->a[0].pExpr;
107572 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
107573 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
107581 /* The UNLIKELY() function is a no-op. The result is the value
107585 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
107590 ** Test-only SQL functions that are only usable if enabled
107598 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107607 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
107616 pA1 = pFarg->a[1].pExpr;
107617 if( pA1->op==TK_COLUMN ){
107619 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
107635 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
107637 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
107658 Vdbe *v = pParse->pVdbe; /* The VM under construction */
107661 int regFree1 = 0; /* If non-zero free this temporary register */
107662 int regFree2 = 0; /* If non-zero free this temporary register */
107667 assert( target>0 && target<=pParse->nMem );
107675 op = pExpr->op;
107679 AggInfo *pAggInfo = pExpr->pAggInfo;
107682 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
107683 pCol = &pAggInfo->aCol[pExpr->iAgg];
107684 if( !pAggInfo->directMode ){
107685 assert( pCol->iMem>0 );
107686 return pCol->iMem;
107687 }else if( pAggInfo->useSortingIdx ){
107688 Table *pTab = pCol->pTab;
107689 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
107690 pCol->iSorterColumn, target);
107691 if( pCol->iColumn<0 ){
107692 VdbeComment((v,"%s.rowid",pTab->zName));
107695 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
107696 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
107706 int iTab = pExpr->iTable;
107710 ** constraints, and that constant is coded by the pExpr->pLeft
107716 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
107718 if( pExpr->y.pTab ){
107719 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
107721 aff = pExpr->affExpr;
107728 &zAff[(aff-'B')*2], P4_STATIC);
107733 if( pParse->iSelfTab<0 ){
107737 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
107743 int iCol = pExpr->iColumn;
107745 pTab = pExpr->y.pTab;
107748 assert( iCol<pTab->nCol );
107750 return -1-pParse->iSelfTab;
107752 pCol = pTab->aCol + iCol;
107754 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
107756 if( pCol->colFlags & COLFLAG_GENERATED ){
107757 if( pCol->colFlags & COLFLAG_BUSY ){
107759 pCol->zCnName);
107762 pCol->colFlags |= COLFLAG_BUSY;
107763 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
107766 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
107770 if( pCol->affinity==SQLITE_AFF_REAL ){
107780 iTab = pParse->iSelfTab - 1;
107784 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
107785 pExpr->iColumn, iTab, target,
107786 pExpr->op2);
107787 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
107803 codeReal(v, pExpr->u.zToken, 0, target);
107809 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
107817 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
107827 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
107828 assert( pExpr->u.zToken[1]=='\'' );
107829 z = &pExpr->u.zToken[2];
107830 n = sqlite3Strlen30(z) - 1;
107839 assert( pExpr->u.zToken!=0 );
107840 assert( pExpr->u.zToken[0]!=0 );
107841 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
107842 if( pExpr->u.zToken[1]!=0 ){
107843 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
107844 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
107845 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
107851 return pExpr->iTable;
107856 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
107863 sqlite3AffinityType(pExpr->u.zToken, 0));
107871 /* fall-through */
107878 Expr *pLeft = pExpr->pLeft;
107883 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
107885 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
107927 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107928 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
107935 Expr *pLeft = pExpr->pLeft;
107937 if( pLeft->op==TK_INTEGER ){
107941 }else if( pLeft->op==TK_FLOAT ){
107943 codeReal(v, pLeft->u.zToken, 1, target);
107952 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2);
107962 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107970 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107972 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
107973 bNormal = pExpr->op2==TK_IS;
107985 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
107995 AggInfo *pInfo = pExpr->pAggInfo;
107997 || NEVER(pExpr->iAgg<0)
107998 || NEVER(pExpr->iAgg>=pInfo->nFunc)
108003 return pInfo->aFunc[pExpr->iAgg].iMem;
108014 sqlite3 *db = pParse->db; /* The database connection */
108020 return pExpr->y.pWin->regResult;
108027 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108031 pFarg = pExpr->x.pList;
108032 nFarg = pFarg ? pFarg->nExpr : 0;
108034 zId = pExpr->u.zToken;
108037 if( pDef==0 && pParse->explain ){
108041 if( pDef==0 || pDef->xFinalize!=0 ){
108045 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
108046 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
108047 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
108049 SQLITE_PTR_TO_INT(pDef->pUserData), target);
108050 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
108055 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
108059 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
108060 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
108065 r1 = pParse->nMem+1;
108066 pParse->nMem += nFarg;
108076 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
108079 assert( pFarg->a[0].pExpr!=0 );
108080 exprOp = pFarg->a[0].pExpr->op;
108084 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
108085 pFarg->a[0].pExpr->op2 =
108086 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
108109 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
108111 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
108114 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
108115 if( !pColl ) pColl = db->pDfltColl;
108119 pDef, pExpr->op2);
108135 if( pParse->db->mallocFailed ){
108139 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
108149 Expr *pLeft = pExpr->pLeft;
108150 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
108151 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
108152 pLeft->op2 = pParse->withinRJSubrtn;
108154 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
108156 if( pExpr->iTable!=n ){
108158 pExpr->iTable, n);
108160 return pLeft->iTable + pExpr->iColumn;
108183 ** X is stored in pExpr->pLeft.
108184 ** Y is stored in pExpr->pList->a[0].pExpr.
108185 ** Z is stored in pExpr->pList->a[1].pExpr.
108193 && ALWAYS(pExpr->pLeft)
108194 && pExpr->pLeft->op==TK_FUNCTION
108196 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108204 pExpr = pExpr->pLeft;
108205 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
108210 pExpr = pExpr->pLeft;
108211 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
108216 ** to a column in the new.* or old.* pseudo-tables available to
108218 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
108219 ** is set to the column of the pseudo-table to read, or to -1 to
108224 ** to reference another column of the old.* pseudo-table, where
108226 ** set to (n+1), where n is the number of columns in each pseudo-table.
108227 ** For a reference to any other column in the new.* pseudo-table, p1
108236 ** p1==0 -> old.rowid p1==3 -> new.rowid
108237 ** p1==1 -> old.a p1==4 -> new.a
108238 ** p1==2 -> old.b p1==5 -> new.b
108245 pTab = pExpr->y.pTab;
108246 iCol = pExpr->iColumn;
108247 p1 = pExpr->iTable * (pTab->nCol+1) + 1
108250 assert( pExpr->iTable==0 || pExpr->iTable==1 );
108251 assert( iCol>=-1 && iCol<pTab->nCol );
108252 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
108253 assert( p1>=0 && p1<(pTab->nCol*2+2) );
108257 (pExpr->iTable ? "new" : "old"),
108258 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
108265 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
108267 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
108280 ** that derive from the right-hand table of a LEFT JOIN. The
108281 ** Expr.iTable value is the table number for the right-hand table.
108287 u8 okConstFactor = pParse->okConstFactor;
108288 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
108291 ** really constant because they originate from the right-hand side
108293 pParse->okConstFactor = 0;
108294 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
108295 pParse->okConstFactor = okConstFactor;
108312 ** X (if it exists) is in pExpr->pLeft.
108313 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
108316 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
108333 sqlite3 *db = pParse->db;
108335 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
108336 assert(pExpr->x.pList->nExpr > 0);
108337 pEList = pExpr->x.pList;
108338 aListelem = pEList->a;
108339 nExpr = pEList->nExpr;
108341 if( (pX = pExpr->pLeft)!=0 ){
108343 if( db->mallocFailed ){
108347 testcase( pX->op==TK_COLUMN );
108355 ** The value in regFree1 might get SCopy-ed into the file result.
108360 for(i=0; i<nExpr-1; i=i+2){
108368 testcase( pTest->op==TK_COLUMN );
108370 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
108376 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
108387 assert( pExpr->affExpr==OE_Rollback
108388 || pExpr->affExpr==OE_Abort
108389 || pExpr->affExpr==OE_Fail
108390 || pExpr->affExpr==OE_Ignore
108392 if( !pParse->pTriggerTab && !pParse->nested ){
108394 "RAISE() may only be used within a trigger-program");
108397 if( pExpr->affExpr==OE_Abort ){
108401 if( pExpr->affExpr==OE_Ignore ){
108403 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
108407 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
108408 pExpr->affExpr, pExpr->u.zToken, 0, 0);
108444 p = pParse->pConstExpr;
108448 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
108449 if( pItem->fg.reusable
108450 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
108452 return pItem->u.iConstExprReg;
108456 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
108458 Vdbe *v = pParse->pVdbe;
108462 pParse->okConstFactor = 0;
108463 if( !pParse->db->mallocFailed ){
108464 if( regDest<0 ) regDest = ++pParse->nMem;
108467 pParse->okConstFactor = 1;
108468 sqlite3ExprDelete(pParse->db, pExpr);
108473 struct ExprList_item *pItem = &p->a[p->nExpr-1];
108474 pItem->fg.reusable = regDest<0;
108475 if( regDest<0 ) regDest = ++pParse->nMem;
108476 pItem->u.iConstExprReg = regDest;
108478 pParse->pConstExpr = p;
108501 && pExpr->op!=TK_REGISTER
108505 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
108528 assert( target>0 && target<=pParse->nMem );
108529 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
108530 if( pParse->pVdbe==0 ) return;
108539 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
108549 sqlite3 *db = pParse->db;
108551 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
108562 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
108574 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
108599 Vdbe *v = pParse->pVdbe;
108602 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
108603 n = pList->nExpr;
108605 for(pItem=pList->a, i=0; i<n; i++, pItem++){
108606 Expr *pExpr = pItem->pExpr;
108608 if( pItem->fg.bSorterRef ){
108609 i--;
108610 n--;
108613 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
108615 i--;
108616 n--;
108618 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
108629 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
108630 && pOp->p1+pOp->p3+1==inReg
108631 && pOp->p2+pOp->p3+1==target+i
108632 && pOp->p5==0 /* The do-not-merge flag must be clear */
108634 pOp->p3++;
108676 sqlite3 *db = pParse->db;
108683 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
108684 if( db->mallocFailed==0 ){
108690 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
108693 compRight.pRight = pExpr->x.pList->a[1].pExpr;
108703 pDel->flags |= EP_OuterON;
108737 Vdbe *v = pParse->pVdbe;
108747 op = pExpr->op;
108757 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
108759 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108763 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108764 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
108770 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108777 isNot = pExpr->op2==TK_ISNOT;
108778 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108782 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108785 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108803 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108805 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108806 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
108807 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
108827 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108854 /* No-op */
108879 Vdbe *v = pParse->pVdbe;
108890 /* The value of pExpr->op and op are related as follows:
108892 ** pExpr->op op
108893 ** --------- ----------
108903 ** For other values of pExpr->op, op is undefined and unused.
108908 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
108912 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
108913 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
108914 assert( pExpr->op!=TK_NE || op==OP_Eq );
108915 assert( pExpr->op!=TK_EQ || op==OP_Ne );
108916 assert( pExpr->op!=TK_LT || op==OP_Ge );
108917 assert( pExpr->op!=TK_LE || op==OP_Gt );
108918 assert( pExpr->op!=TK_GT || op==OP_Le );
108919 assert( pExpr->op!=TK_GE || op==OP_Lt );
108921 switch( pExpr->op ){
108927 }else if( pExpr->op==TK_AND ){
108929 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
108930 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108934 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
108936 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
108943 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
108950 isNot = pExpr->op2==TK_ISNOT;
108951 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
108956 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
108961 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
108968 testcase( pExpr->op==TK_IS );
108969 testcase( pExpr->op==TK_ISNOT );
108970 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
108979 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
108981 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
108982 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2);
108983 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
109001 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1);
109030 /* no-op */
109051 sqlite3 *db = pParse->db;
109053 if( db->mallocFailed==0 ){
109063 ** If pExpr is a simple SQL value - an integer, real, string, blob
109064 ** or NULL value - then the VDBE currently being prepared is configured
109065 ** to re-prepare each time a new value is bound to variable pVar.
109068 ** same as that currently bound to variable pVar, non-zero is returned.
109079 sqlite3_value *pL, *pR = 0;
109081 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
109082 if( pR ){
109083 iVar = pVar->iColumn;
109084 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
109085 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
109088 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
109090 res = 0==sqlite3MemCompare(pL, pR, 0);
109092 sqlite3ValueFree(pR);
109103 ** other than the top-level COLLATE operator.
109105 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109117 ** this routine is used, it does not hurt to get an extra 2 - that
109122 ** pParse->pReprepare can be matched against literals in pB. The
109123 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
109138 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
109141 combinedFlags = pA->flags | pB->flags;
109143 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
109148 if( pA->op!=pB->op || pA->op==TK_RAISE ){
109149 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
109152 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
109159 if( pA->u.zToken ){
109160 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
109161 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109163 assert( pA->op==pB->op );
109168 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
109173 }else if( pA->op==TK_NULL ){
109175 }else if( pA->op==TK_COLLATE ){
109176 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
109178 if( pB->u.zToken!=0
109179 && pA->op!=TK_COLUMN
109180 && pA->op!=TK_AGG_COLUMN
109181 && strcmp(pA->u.zToken,pB->u.zToken)!=0
109186 if( (pA->flags & (EP_Distinct|EP_Commuted))
109187 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
109191 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
109192 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
109193 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
109194 if( pA->op!=TK_STRING
109195 && pA->op!=TK_TRUEFALSE
109198 if( pA->iColumn!=pB->iColumn ) return 2;
109199 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
109200 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
109213 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
109216 ** This routine might return non-zero for equivalent ExprLists. The
109222 ** always differs from a non-NULL pointer.
109228 if( pA->nExpr!=pB->nExpr ) return 1;
109229 for(i=0; i<pA->nExpr; i++){
109231 Expr *pExprA = pA->a[i].pExpr;
109232 Expr *pExprB = pB->a[i].pExpr;
109233 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
109240 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
109251 ** Return non-zero if Expr p can only be true if pNN is not NULL.
109253 ** Or if seenNot is true, return non-zero if Expr p can only be
109254 ** non-NULL if pNN is not NULL
109261 int seenNot /* Return true only if p can be any non-NULL value */
109266 return pNN->op!=TK_NULL;
109268 switch( p->op ){
109271 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
109272 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109277 pList = p->x.pList;
109279 assert( pList->nExpr==2 );
109281 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
109282 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
109286 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109306 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
109313 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
109317 if( p->op2!=TK_IS ) return 0;
109318 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109322 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
109345 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
109362 if( pE2->op==TK_OR
109363 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
109364 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
109368 if( pE2->op==TK_NOTNULL
109369 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
109378 ** If the expression node requires that the table at pWalker->iCur
109379 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
109382 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
109383 ** (never setting pWalker->eCode) is a harmless missed optimization.
109386 testcase( pExpr->op==TK_AGG_COLUMN );
109387 testcase( pExpr->op==TK_AGG_FUNCTION );
109389 switch( pExpr->op ){
109400 testcase( pExpr->op==TK_ISNOT );
109401 testcase( pExpr->op==TK_ISNULL );
109402 testcase( pExpr->op==TK_NOTNULL );
109403 testcase( pExpr->op==TK_IS );
109404 testcase( pExpr->op==TK_OR );
109405 testcase( pExpr->op==TK_VECTOR );
109406 testcase( pExpr->op==TK_CASE );
109407 testcase( pExpr->op==TK_IN );
109408 testcase( pExpr->op==TK_FUNCTION );
109409 testcase( pExpr->op==TK_TRUTH );
109412 if( pWalker->u.iCur==pExpr->iTable ){
109413 pWalker->eCode = 1;
109419 if( pWalker->eCode==0 ){
109420 sqlite3WalkExpr(pWalker, pExpr->pLeft);
109421 if( pWalker->eCode ){
109422 pWalker->eCode = 0;
109423 sqlite3WalkExpr(pWalker, pExpr->pRight);
109429 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
109430 assert( pWalker->eCode );
109444 Expr *pLeft = pExpr->pLeft;
109445 Expr *pRight = pExpr->pRight;
109446 testcase( pExpr->op==TK_EQ );
109447 testcase( pExpr->op==TK_NE );
109448 testcase( pExpr->op==TK_LT );
109449 testcase( pExpr->op==TK_LE );
109450 testcase( pExpr->op==TK_GT );
109451 testcase( pExpr->op==TK_GE );
109454 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
109455 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
109456 if( (pLeft->op==TK_COLUMN
109457 && pLeft->y.pTab!=0
109458 && IsVirtual(pLeft->y.pTab))
109459 || (pRight->op==TK_COLUMN
109460 && pRight->y.pTab!=0
109461 && IsVirtual(pRight->y.pTab))
109473 ** Return true (non-zero) if expression p can only be true if at least
109474 ** one column of table iTab is non-null. In other words, return true
109491 ** be non-NULL, then the LEFT JOIN can be safely converted into an
109498 if( p->op==TK_NOTNULL ){
109499 p = p->pLeft;
109501 while( p->op==TK_AND ){
109502 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
109503 p = p->pRight;
109529 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
109530 ** pWalker->u.pIdxCover->pIdx.
109533 if( pExpr->op==TK_COLUMN
109534 && pExpr->iTable==pWalker->u.pIdxCover->iCur
109535 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
109537 pWalker->eCode = 1;
109589 struct RefSrcList *p = pWalker->u.pRefSrcList;
109590 SrcList *pSrc = pSelect->pSrc;
109593 if( pSrc->nSrc==0 ) return WRC_Continue;
109594 j = p->nExclude;
109595 p->nExclude += pSrc->nSrc;
109596 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
109598 p->nExclude = 0;
109601 p->aiExclude = piNew;
109603 for(i=0; i<pSrc->nSrc; i++, j++){
109604 p->aiExclude[j] = pSrc->a[i].iCursor;
109609 struct RefSrcList *p = pWalker->u.pRefSrcList;
109610 SrcList *pSrc = pSelect->pSrc;
109611 if( p->nExclude ){
109612 assert( p->nExclude>=pSrc->nSrc );
109613 p->nExclude -= pSrc->nSrc;
109619 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
109622 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
109626 if( pExpr->op==TK_COLUMN
109627 || pExpr->op==TK_AGG_COLUMN
109630 struct RefSrcList *p = pWalker->u.pRefSrcList;
109631 SrcList *pSrc = p->pRef;
109632 int nSrc = pSrc ? pSrc->nSrc : 0;
109634 if( pExpr->iTable==pSrc->a[i].iCursor ){
109635 pWalker->eCode |= 1;
109639 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
109640 if( i>=p->nExclude ){
109641 pWalker->eCode |= 2;
109656 ** -1 pExpr only references no tables at all, or it only
109671 x.db = pParse->db;
109673 assert( pExpr->op==TK_AGG_FUNCTION );
109675 sqlite3WalkExprList(&w, pExpr->x.pList);
109678 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
109681 sqlite3DbFree(pParse->db, x.aiExclude);
109687 return -1;
109699 ** The copy is stored on pParse->pConstExpr with a register number of 0.
109706 && pExpr->pAggInfo!=0
109708 AggInfo *pAggInfo = pExpr->pAggInfo;
109709 int iAgg = pExpr->iAgg;
109710 Parse *pParse = pWalker->pParse;
109711 sqlite3 *db = pParse->db;
109712 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
109713 if( pExpr->op==TK_AGG_COLUMN ){
109714 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
109715 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
109718 pAggInfo->aCol[iAgg].pCExpr = pExpr;
109723 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
109724 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
109727 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
109742 pWalker->pParse = pParse;
109743 pWalker->xExprCallback = agginfoPersistExprCb;
109744 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
109748 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
109753 pInfo->aCol = sqlite3ArrayAllocate(
109755 pInfo->aCol,
109756 sizeof(pInfo->aCol[0]),
109757 &pInfo->nColumn,
109764 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
109769 pInfo->aFunc = sqlite3ArrayAllocate(
109771 pInfo->aFunc,
109772 sizeof(pInfo->aFunc[0]),
109773 &pInfo->nFunc,
109786 NameContext *pNC = pWalker->u.pNC;
109787 Parse *pParse = pNC->pParse;
109788 SrcList *pSrcList = pNC->pSrcList;
109789 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
109791 assert( pNC->ncFlags & NC_UAggInfo );
109792 switch( pExpr->op ){
109795 testcase( pExpr->op==TK_AGG_COLUMN );
109796 testcase( pExpr->op==TK_COLUMN );
109800 SrcItem *pItem = pSrcList->a;
109801 for(i=0; i<pSrcList->nSrc; i++, pItem++){
109804 if( pExpr->iTable==pItem->iCursor ){
109808 ** Make an entry for the column in pAggInfo->aCol[] if there
109812 pCol = pAggInfo->aCol;
109813 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
109814 if( pCol->iTable==pExpr->iTable &&
109815 pCol->iColumn==pExpr->iColumn ){
109819 if( (k>=pAggInfo->nColumn)
109820 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
109822 pCol = &pAggInfo->aCol[k];
109824 pCol->pTab = pExpr->y.pTab;
109825 pCol->iTable = pExpr->iTable;
109826 pCol->iColumn = pExpr->iColumn;
109827 pCol->iMem = ++pParse->nMem;
109828 pCol->iSorterColumn = -1;
109829 pCol->pCExpr = pExpr;
109830 if( pAggInfo->pGroupBy ){
109832 ExprList *pGB = pAggInfo->pGroupBy;
109833 struct ExprList_item *pTerm = pGB->a;
109834 n = pGB->nExpr;
109836 Expr *pE = pTerm->pExpr;
109837 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
109838 pE->iColumn==pExpr->iColumn ){
109839 pCol->iSorterColumn = j;
109844 if( pCol->iSorterColumn<0 ){
109845 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
109848 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
109851 ** pAggInfo->aCol[] entry.
109854 pExpr->pAggInfo = pAggInfo;
109855 pExpr->op = TK_AGG_COLUMN;
109856 pExpr->iAgg = (i16)k;
109858 } /* endif pExpr->iTable==pItem->iCursor */
109864 if( (pNC->ncFlags & NC_InAggFunc)==0
109865 && pWalker->walkerDepth==pExpr->op2
109870 struct AggInfo_func *pItem = pAggInfo->aFunc;
109871 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
109872 if( pItem->pFExpr==pExpr ) break;
109873 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
109877 if( i>=pAggInfo->nFunc ){
109878 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
109880 u8 enc = ENC(pParse->db);
109881 i = addAggInfoFunc(pParse->db, pAggInfo);
109884 pItem = &pAggInfo->aFunc[i];
109885 pItem->pFExpr = pExpr;
109886 pItem->iMem = ++pParse->nMem;
109888 pItem->pFunc = sqlite3FindFunction(pParse->db,
109889 pExpr->u.zToken,
109890 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
109891 if( pExpr->flags & EP_Distinct ){
109892 pItem->iDistinct = pParse->nTab++;
109894 pItem->iDistinct = -1;
109898 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
109902 pExpr->iAgg = (i16)i;
109903 pExpr->pAggInfo = pAggInfo;
109915 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
109930 assert( pNC->pSrcList!=0 );
109944 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
109945 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
109954 if( pParse->nTempReg==0 ){
109955 return ++pParse->nMem;
109957 return pParse->aTempReg[--pParse->nTempReg];
109967 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
109968 pParse->aTempReg[pParse->nTempReg++] = iReg;
109979 i = pParse->iRangeReg;
109980 n = pParse->nRangeReg;
109982 pParse->iRangeReg += nReg;
109983 pParse->nRangeReg -= nReg;
109985 i = pParse->nMem+1;
109986 pParse->nMem += nReg;
109996 if( nReg>pParse->nRangeReg ){
109997 pParse->nRangeReg = nReg;
109998 pParse->iRangeReg = iReg;
110005 ** Always invoke this procedure after coding a subroutine or co-routine
110007 ** the sub/co-routine does not use registers in common with the code that
110008 ** invokes the sub/co-routine.
110011 pParse->nTempReg = 0;
110012 pParse->nRangeReg = 0;
110023 if( pParse->nRangeReg>0
110024 && pParse->iRangeReg+pParse->nRangeReg > iFirst
110025 && pParse->iRangeReg <= iLast
110029 for(i=0; i<pParse->nTempReg; i++){
110030 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
110065 ** If the table is a system table, this function leaves an error message
110066 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
110071 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
110073 || (pTab->tabFlags & TF_Eponymous)!=0
110074 || ( (pTab->tabFlags & TF_Shadow)!=0
110075 && sqlite3ReadOnlyShadowTables(pParse->db)
110079 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
110096 const char *zWhen, /* "when" part of error message */
110099 pParse->colNamesSet = 1;
110123 ** Generate VM code to replace any double-quoted strings (but not double-quoted
110125 ** database zDb with their single-quoted equivalents. If argument bTemp is
110151 Vdbe *v = pParse->pVdbe;
110154 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
110155 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
110171 char *zName = 0; /* NULL-terminated version of pName */
110172 sqlite3 *db = pParse->db; /* Database connection */
110173 int nTabName; /* Number of UTF-8 characters in zTabName */
110176 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
110178 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
110179 assert( pSrc->nSrc==1 );
110180 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
110182 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110184 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
110185 zDb = db->aDb[iDb].zDbSName;
110215 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
110222 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110233 if( pVTab->pVtab->pModule->xRename==0 ){
110249 /* figure out how many UTF-8 characters are in zName */
110250 zTabName = pTab->zName;
110251 nTabName = sqlite3Utf8CharLen(zTabName, -1);
110288 zDb, zName, pTab->zName);
110309 ** of any resources used by the v-table implementation (including other
110314 int i = ++pParse->nMem;
110336 const char *zErr /* Error message text */
110349 ** The Table structure pParse->pNewTable was extended to include
110353 Table *pNew; /* Copy of pParse->pNewTable */
110358 char *zCol; /* Null-terminated column definition */
110365 db = pParse->db;
110366 assert( db->pParse==pParse );
110367 if( pParse->nErr ) return;
110368 assert( db->mallocFailed==0 );
110369 pNew = pParse->pNewTable;
110373 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
110374 zDb = db->aDb[iDb].zDbSName;
110375 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
110376 pCol = &pNew->aCol[pNew->nCol-1];
110383 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110393 if( pCol->colFlags & COLFLAG_PRIMKEY ){
110397 if( pNew->pIndex ){
110402 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
110407 assert( pDflt==0 || pDflt->op==TK_SPAN );
110408 if( pDflt && pDflt->pLeft->op==TK_NULL ){
110412 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
110414 "Cannot add a REFERENCES column with non-NULL default value");
110416 if( pCol->notNull && !pDflt ){
110431 assert( db->mallocFailed == 1 );
110436 "Cannot add a column with non-constant default");
110440 }else if( pCol->colFlags & COLFLAG_STORED ){
110446 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
110448 char *zEnd = &zCol[pColDef->n-1];
110450 *zEnd-- = '\0';
110461 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
110476 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
110486 if( pNew->pCheck!=0
110487 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
110503 ** This function is called by the parser after the table-name in
110504 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
110505 ** pSrc is the full-name of the table being altered.
110523 sqlite3 *db = pParse->db;
110526 assert( pParse->pNewTable==0 );
110528 if( db->mallocFailed ) goto exit_begin_add_column;
110529 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110550 assert( pTab->u.tab.addColOffset>0 );
110551 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
110562 pParse->pNewTable = pNew;
110563 pNew->nTabRef = 1;
110564 pNew->nCol = pTab->nCol;
110565 assert( pNew->nCol>0 );
110566 nAlloc = (((pNew->nCol-1)/8)*8)+8;
110567 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
110568 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
110569 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
110570 if( !pNew->aCol || !pNew->zName ){
110571 assert( db->mallocFailed );
110574 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
110575 for(i=0; i<pNew->nCol; i++){
110576 Column *pCol = &pNew->aCol[i];
110577 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
110578 pCol->hName = sqlite3StrIHash(pCol->zCnName);
110581 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
110582 pNew->pSchema = db->aDb[iDb].pSchema;
110583 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
110584 pNew->nTabRef = 1;
110595 ** it loads an error message into pParse and returns non-zero.
110615 zType, pTab->zName
110632 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
110636 sqlite3 *db = pParse->db; /* Database connection */
110646 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
110654 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
110656 zDb = db->aDb[iSchema].zDbSName;
110660 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
110669 for(iCol=0; iCol<pTab->nCol; iCol++){
110670 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zCnName, zOld) ) break;
110672 if( iCol==pTab->nCol ){
110677 /* Ensure the schema contains no double-quoted strings */
110688 assert( pNew->n>0 );
110689 bQuote = sqlite3Isquote(pNew->z[0]);
110696 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
110697 pTab->zName
110704 zDb, pTab->zName, iCol, zNew, bQuote
110759 ** rename-token list.
110761 ** 2. Dereferences each pointer in the rename-token list.
110764 ** address-sanitizer or similar. If any of these pointers no longer
110765 ** point to valid objects, an exception is raised by the memory-checking
110779 assert( pParse==pParse->db->pParse );
110780 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
110781 if( pParse->nErr==0 ){
110784 for(p=pParse->pRename; p; p=p->pNext){
110785 if( p->p ){
110786 assert( p->p!=pPtr );
110787 i += *(u8*)(p->p);
110802 ** in pParse->pRename.
110814 assert( pPtr || pParse->db->mallocFailed );
110816 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
110817 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
110819 pNew->p = pPtr;
110820 pNew->t = *pToken;
110821 pNew->pNext = pParse->pRename;
110822 pParse->pRename = pNew;
110837 for(p=pParse->pRename; p; p=p->pNext){
110838 if( p->p==pFrom ){
110839 p->p = pTo;
110849 Parse *pParse = pWalker->pParse;
110852 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
110862 With *pWith = pSelect->pWith;
110864 Parse *pParse = pWalker->pParse;
110867 assert( pWith->nCte>0 );
110868 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
110869 /* Push a copy of the With object onto the with-stack. We use a copy
110871 ** and SF_Resolved) below. And the parser code that uses the with-stack
110874 pCopy = sqlite3WithDup(pParse->db, pWith);
110877 for(i=0; i<pWith->nCte; i++){
110878 Select *p = pWith->a[i].pSelect;
110883 if( sNC.pParse->db->mallocFailed ) return;
110885 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
110887 if( pCopy && pParse->pWith==pCopy ){
110888 pParse->pWith = pCopy->pOuter;
110902 for(ii=0; ii<pIdList->nId; ii++){
110903 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
110911 Parse *pParse = pWalker->pParse;
110913 if( pParse->nErr ) return WRC_Abort;
110914 testcase( p->selFlags & SF_View );
110915 testcase( p->selFlags & SF_CopyCte );
110916 if( p->selFlags & (SF_View|SF_CopyCte) ){
110919 if( ALWAYS(p->pEList) ){
110920 ExprList *pList = p->pEList;
110921 for(i=0; i<pList->nExpr; i++){
110922 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
110923 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
110927 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
110928 SrcList *pSrc = p->pSrc;
110929 for(i=0; i<pSrc->nSrc; i++){
110930 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
110931 if( pSrc->a[i].fg.isUsing==0 ){
110932 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
110934 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
110947 u8 eMode = pParse->eParseMode;
110953 pParse->eParseMode = PARSE_MODE_UNMAP;
110955 pParse->eParseMode = eMode;
110959 ** Remove all nodes that are part of expression-list pEList from the
110970 for(i=0; i<pEList->nExpr; i++){
110971 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
110972 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
110985 pNext = p->pNext;
111008 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
111009 if( (*pp)->p==pPtr ){
111012 *pp = pToken->pNext;
111013 pToken->pNext = pCtx->pList;
111014 pCtx->pList = pToken;
111015 pCtx->nList++;
111026 ** descend into sub-select statements.
111029 if( p->selFlags & (SF_View|SF_CopyCte) ){
111030 testcase( p->selFlags & SF_View );
111031 testcase( p->selFlags & SF_CopyCte );
111045 ** constructed in RenameCtx object at pWalker->u.pRename.
111048 RenameCtx *p = pWalker->u.pRename;
111049 if( pExpr->op==TK_TRIGGER
111050 && pExpr->iColumn==p->iCol
111051 && pWalker->pParse->pTriggerTab==p->pTab
111053 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111054 }else if( pExpr->op==TK_COLUMN
111055 && pExpr->iColumn==p->iCol
111057 && p->pTab==pExpr->y.pTab
111059 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
111074 RenameToken *pBest = pCtx->pList;
111078 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
111079 if( pToken->t.z>pBest->t.z ) pBest = pToken;
111081 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
111082 *pp = pBest->pNext;
111089 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
111090 ** ALTER TABLE RENAME COLUMN program. The error message emitted by the
111091 ** sub-routine is currently stored in pParse->zErrMsg. This function
111092 ** adds context to the error message and then stores it in pCtx.
111105 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
111107 pParse->zErrMsg
111109 sqlite3_result_error(pCtx, zErr, -1);
111110 sqlite3DbFree(pParse->db, zErr);
111114 ** For each name in the the expression-list pEList (i.e. each
111115 ** pEList->a[i].zName) that matches the string in zOld, extract the
111116 ** corresponding rename-token from Parse object pParse and add it
111127 for(i=0; i<pEList->nExpr; i++){
111128 const char *zName = pEList->a[i].zEName;
111129 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
111140 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
111141 ** that matches the string in zOld, extract the corresponding rename-token
111152 for(i=0; i<pIdList->nId; i++){
111153 const char *zName = pIdList->a[i].zName;
111182 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
111183 p->eParseMode = PARSE_MODE_RENAME;
111184 p->db = db;
111185 p->nQueryLoop = 1;
111187 if( db->mallocFailed ) rc = SQLITE_NOMEM;
111189 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
111200 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
111201 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
111206 db->init.iDb = 0;
111246 nQuot = sqlite3Strlen30(zQuot)-1;
111250 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
111259 /* At this point pRename->pList contains a list of RenameToken objects
111261 ** with the new column name, or with single-quoted versions of themselves.
111266 while( pRename->pList ){
111273 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
111279 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
111282 /* Dequote the double-quoted token. Then requote it again, this time
111285 ** add another space after the new, single-quoted version of the
111288 memcpy(zBuf1, pBest->t.z, pBest->t.n);
111289 zBuf1[pBest->t.n] = 0;
111292 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
111298 iOff = pBest->t.z - zSql;
111299 if( pBest->t.n!=nReplace ){
111300 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
111301 nOut - (iOff + pBest->t.n)
111303 nOut += nReplace - pBest->t.n;
111310 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
111321 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
111324 ** message in the Parse object.
111327 sqlite3 *db = pParse->db;
111328 Trigger *pNew = pParse->pNewTrigger;
111335 assert( pNew->pTabSchema );
111336 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
111337 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
111339 pParse->eTriggerOp = pNew->op;
111342 if( ALWAYS(pParse->pTriggerTab) ){
111343 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
111347 if( rc==SQLITE_OK && pNew->pWhen ){
111348 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
111351 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
111352 if( pStep->pSelect ){
111353 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
111354 if( pParse->nErr ) rc = pParse->rc;
111356 if( rc==SQLITE_OK && pStep->zTarget ){
111360 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
111363 pStep->pExprList = 0;
111368 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
111369 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
111370 assert( pSrc==pSel->pSrc );
111371 if( pStep->pExprList ) pSel->pEList = 0;
111372 pSel->pSrc = 0;
111375 if( pStep->pFrom ){
111377 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
111378 SrcItem *p = &pStep->pFrom->a[i];
111379 if( p->pSelect ){
111380 sqlite3SelectPrep(pParse, p->pSelect, 0);
111385 if( db->mallocFailed ){
111389 if( rc==SQLITE_OK && pStep->pWhere ){
111390 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
111393 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
111395 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
111396 if( pStep->pUpsert && rc==SQLITE_OK ){
111397 Upsert *pUpsert = pStep->pUpsert;
111398 pUpsert->pUpsertSrc = pSrc;
111401 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
111403 ExprList *pUpsertSet = pUpsert->pUpsertSet;
111407 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
111410 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
111432 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
111435 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111436 sqlite3WalkSelect(pWalker, pStep->pSelect);
111437 sqlite3WalkExpr(pWalker, pStep->pWhere);
111438 sqlite3WalkExprList(pWalker, pStep->pExprList);
111439 if( pStep->pUpsert ){
111440 Upsert *pUpsert = pStep->pUpsert;
111441 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
111442 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
111443 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
111444 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
111446 if( pStep->pFrom ){
111448 for(i=0; i<pStep->pFrom->nSrc; i++){
111449 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
111460 sqlite3 *db = pParse->db;
111462 if( pParse->pVdbe ){
111463 sqlite3VdbeFinalize(pParse->pVdbe);
111465 sqlite3DeleteTable(db, pParse->pNewTable);
111466 while( (pIdx = pParse->pNewIndex)!=0 ){
111467 pParse->pNewIndex = pIdx->pNext;
111470 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
111471 sqlite3DbFree(db, pParse->zErrMsg);
111472 renameTokenFree(db, pParse->pRename);
111488 ** 7. bQuote: Non-zero if the new column name should be quoted.
111492 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
111522 sqlite3_xauth xAuth = db->xAuth;
111532 if( pTab==0 || iCol>=pTab->nCol ){
111536 zOld = pTab->aCol[iCol].zCnName;
111538 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
111541 db->xAuth = 0;
111556 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111557 pSelect->selFlags &= ~SF_View;
111560 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
111567 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
111571 if( iCol<sParse.pNewTable->nCol ){
111573 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
111577 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
111579 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111580 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
111581 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111583 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
111584 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
111587 for(i=0; i<sParse.pNewTable->nCol; i++){
111589 &sParse.pNewTable->aCol[i]);
111596 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111597 for(i=0; i<pFKey->nCol; i++){
111598 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
111599 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
111601 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
111602 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
111604 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
111610 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111611 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111618 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
111619 if( pStep->zTarget ){
111620 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
111622 if( pStep->pUpsert ){
111623 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
111626 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
111627 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
111635 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
111659 db->xAuth = xAuth;
111668 RenameCtx *p = pWalker->u.pRename;
111669 if( pExpr->op==TK_COLUMN
111671 && p->pTab==pExpr->y.pTab
111673 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
111683 RenameCtx *p = pWalker->u.pRename;
111684 SrcList *pSrc = pSelect->pSrc;
111685 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
111686 testcase( pSelect->selFlags & SF_View );
111687 testcase( pSelect->selFlags & SF_CopyCte );
111691 assert( pWalker->pParse->db->mallocFailed );
111694 for(i=0; i<pSrc->nSrc; i++){
111695 SrcItem *pItem = &pSrc->a[i];
111696 if( pItem->pTab==p->pTab ){
111697 renameTokenFind(pWalker->pParse, p, pItem->zName);
111723 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
111746 sqlite3_xauth xAuth = db->xAuth;
111747 db->xAuth = 0;
111763 int isLegacy = (db->flags & SQLITE_LegacyAlter);
111769 Select *pSelect = pTab->u.view.pSelect;
111774 assert( pSelect->selFlags & SF_View );
111775 pSelect->selFlags &= ~SF_View;
111776 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
111780 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
111786 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
111791 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
111792 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
111793 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
111802 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
111805 sqlite3WalkExprList(&sWalker, pTab->pCheck);
111807 renameTokenFind(&sParse, &sCtx, pTab->zName);
111813 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
111815 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111823 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
111824 && sCtx.pTab->pSchema==pTrigger->pTabSchema
111826 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
111833 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
111834 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
111835 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
111837 if( pStep->pFrom ){
111839 for(i=0; i<pStep->pFrom->nSrc; i++){
111840 SrcItem *pItem = &pStep->pFrom->a[i];
111841 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
111842 renameTokenFind(&sParse, &sCtx, pItem->zName);
111870 db->xAuth = xAuth;
111878 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
111879 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
111887 ** double-quotes use single quotes instead.
111921 sqlite3_xauth xAuth = db->xAuth;
111922 db->xAuth = 0;
111947 Select *pSelect = sParse.pNewTable->u.view.pSelect;
111948 pSelect->selFlags &= ~SF_View;
111951 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
111957 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
111959 for(i=0; i<sParse.pNewTable->nCol; i++){
111962 &sParse.pNewTable->aCol[i]));
111967 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
111968 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
111994 db->xAuth = xAuth;
112013 ** 5: "when" part of error message.
112033 int isLegacy = (db->flags & SQLITE_LegacyAlter);
112038 sqlite3_xauth xAuth = db->xAuth;
112039 db->xAuth = 0;
112047 int flags = db->flags;
112048 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
112050 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
112056 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
112065 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
112083 db->xAuth = xAuth;
112092 ** argv[0]: An integer - the index of the schema containing the table
112094 ** argv[2]: An integer - the index of the column to remove.
112108 const char *zDb = db->aDb[iSchema].zDbSName;
112117 sqlite3_xauth xAuth = db->xAuth;
112118 db->xAuth = 0;
112125 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
112131 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
112132 if( iCol<pTab->nCol-1 ){
112134 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
112135 zEnd = (const char*)pEnd->t.z;
112138 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
112139 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
112142 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
112143 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
112149 db->xAuth = xAuth;
112165 sqlite3 *db = pParse->db; /* Database handle */
112170 int iCol; /* Index of column zCol in pTab->aCol[] */
112173 assert( pParse->pNewTable==0 );
112175 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
112176 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
112187 assert( db->mallocFailed );
112198 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
112200 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
112207 if( pTab->nCol<=1 ){
112213 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
112215 zDb = db->aDb[iDb].zDbSName;
112218 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
112228 , zDb, iDb, iCol, pTab->zName
112236 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
112242 int nField = 0; /* Number of non-virtual columns after drop */
112245 iCur = pParse->nTab++;
112248 reg = ++pParse->nMem;
112251 pParse->nMem += pTab->nCol;
112254 pParse->nMem += pPk->nColumn;
112255 for(i=0; i<pPk->nKeyCol; i++){
112258 nField = pPk->nKeyCol;
112260 regRec = ++pParse->nMem;
112261 for(i=0; i<pTab->nCol; i++){
112262 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
112267 if( iPos<pPk->nKeyCol ) continue;
112268 regOut = reg+1+iPos-(iPos>iColPos);
112272 if( i==pTab->iPKey ){
112282 pParse->nMem++;
112288 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
112304 ** Register built-in functions used to help implement ALTER TABLE
112321 ** 2005-07-08
112371 ** columns. The N-th integer (for N>1) is the average number of rows in
112372 ** the index which have the same value for the first N-1 columns. For
112373 ** a K-column index, there will be K+1 integers in the stat column. If
112397 ** inclusive are samples of the left-most key value in the index taken at
112404 ** For i between 0 and S-1. Conceptually, the index space is divided into
112431 ** of entries in the index whose left-most column exactly matches
112432 ** the left-most column of the sample. The second integer in nEq
112438 ** left-most column is less than the left-most column of the sample.
112439 ** The K-th integer in the nLt entry is the number of index entries
112454 ** looks at the left-most column of the index. The sqlite_stat3.sample
112455 ** column contains the actual value of the left-most column instead
112476 ** appropriate compile-time options are provided.
112505 sqlite3 *db = pParse->db;
112519 pDb = &db->aDb[iDb];
112528 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
112531 ** side-effect of the CREATE TABLE statement is to leave the rootpage
112532 ** of the new table in register pParse->regRoot. This is important
112535 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
112537 aRoot[i] = (u32)pParse->regRoot;
112544 aRoot[i] = pStat->tnum;
112549 pDb->zDbSName, zTab, zWhereType, zWhere
112552 }else if( db->xPreUpdateCallback ){
112553 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
112579 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
112604 int nLimit; /* Analysis row-scan limit */
112607 u8 nSkipAhead; /* Number of times of skip-ahead */
112612 u32 iPrn; /* Pseudo-random number used for sampling */
112627 if( p->nRowid ){
112628 sqlite3DbFree(db, p->u.aRowid);
112629 p->nRowid = 0;
112639 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112640 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
112641 if( p->u.aRowid ){
112642 p->nRowid = n;
112643 memcpy(p->u.aRowid, pData, n);
112645 p->nRowid = 0;
112655 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
112656 p->nRowid = 0;
112657 p->u.iRowid = iRowid;
112667 pTo->isPSample = pFrom->isPSample;
112668 pTo->iCol = pFrom->iCol;
112669 pTo->iHash = pFrom->iHash;
112670 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
112671 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
112672 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
112673 if( pFrom->nRowid ){
112674 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
112676 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
112687 if( p->mxSample ){
112689 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
112690 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
112691 sampleClear(p->db, &p->current);
112694 sqlite3DbFree(p->db, p);
112703 ** L: A limit on the number of rows to scan, or 0 for no-limit
112761 p->db = db;
112762 p->nEst = sqlite3_value_int64(argv[2]);
112763 p->nRow = 0;
112764 p->nLimit = sqlite3_value_int64(argv[3]);
112765 p->nCol = nCol;
112766 p->nKeyCol = nKeyCol;
112767 p->nSkipAhead = 0;
112768 p->current.anDLt = (tRowcnt*)&p[1];
112769 p->current.anEq = &p->current.anDLt[nColUp];
112772 p->mxSample = p->nLimit==0 ? mxSample : 0;
112775 int i; /* Used to iterate through p->aSample[] */
112777 p->iGet = -1;
112778 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
112779 p->current.anLt = &p->current.anEq[nColUp];
112780 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
112783 p->a = (struct StatSample*)&p->current.anLt[nColUp];
112784 p->aBest = &p->a[mxSample];
112785 pSpace = (u8*)(&p->a[mxSample+nCol]);
112787 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112788 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112789 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
112791 assert( (pSpace - (u8*)p)==n );
112794 p->aBest[i].iCol = i;
112819 ** pNew and pOld are both candidate non-periodic samples selected for
112820 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
112827 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
112834 int nCol = pAccum->nCol;
112836 assert( pNew->iCol==pOld->iCol );
112837 for(i=pNew->iCol+1; i<nCol; i++){
112838 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
112839 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
112841 if( pNew->iHash>pOld->iHash ) return 1;
112851 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
112858 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
112859 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
112861 assert( pOld->isPSample==0 && pNew->isPSample==0 );
112862 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
112866 if( pNew->iCol<pOld->iCol ) return 1;
112867 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
112873 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
112874 ** remove the least desirable sample from p->a[] to make room.
112886 if( nEqZero>p->nMaxEqZero ){
112887 p->nMaxEqZero = nEqZero;
112889 if( pNew->isPSample==0 ){
112891 assert( pNew->anEq[pNew->iCol]>0 );
112898 for(i=p->nSample-1; i>=0; i--){
112899 StatSample *pOld = &p->a[i];
112900 if( pOld->anEq[pNew->iCol]==0 ){
112901 if( pOld->isPSample ) return;
112902 assert( pOld->iCol>pNew->iCol );
112910 pUpgrade->iCol = pNew->iCol;
112911 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
112917 if( p->nSample>=p->mxSample ){
112918 StatSample *pMin = &p->a[p->iMin];
112919 tRowcnt *anEq = pMin->anEq;
112920 tRowcnt *anLt = pMin->anLt;
112921 tRowcnt *anDLt = pMin->anDLt;
112922 sampleClear(p->db, pMin);
112923 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
112924 pSample = &p->a[p->nSample-1];
112925 pSample->nRowid = 0;
112926 pSample->anEq = anEq;
112927 pSample->anDLt = anDLt;
112928 pSample->anLt = anLt;
112929 p->nSample = p->mxSample-1;
112932 /* The "rows less-than" for the rowid column must be greater than that
112933 ** for the last sample in the p->a[] array. Otherwise, the samples would
112935 assert( p->nSample==0
112936 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
112939 pSample = &p->a[p->nSample];
112941 p->nSample++;
112944 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
112947 if( p->nSample>=p->mxSample ){
112948 int iMin = -1;
112949 for(i=0; i<p->mxSample; i++){
112950 if( p->a[i].isPSample ) continue;
112951 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
112956 p->iMin = iMin;
112964 ** p->current contains a sample that reflects the previous row of the
112973 for(i=(p->nCol-2); i>=iChng; i--){
112974 StatSample *pBest = &p->aBest[i];
112975 pBest->anEq[i] = p->current.anEq[i];
112976 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
112982 ** p->nMaxEqZero or greater set to zero. */
112983 for(i=p->nSample-1; i>=0; i--){
112985 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
112989 if( iChng<p->nMaxEqZero ){
112990 for(i=p->nSample-1; i>=0; i--){
112992 for(j=iChng; j<p->nCol; j++){
112993 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
112996 p->nMaxEqZero = iChng;
113006 ** C Index of left-most column to differ from previous row
113016 ** if it wants the byte-code to do special processing.
113033 assert( p->nCol>0 );
113034 assert( iChng<p->nCol );
113036 if( p->nRow==0 ){
113038 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
113042 if( p->mxSample ) samplePushPrevious(p, iChng);
113048 p->current.anEq[i]++;
113050 for(i=iChng; i<p->nCol; i++){
113051 p->current.anDLt[i]++;
113053 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
113055 p->current.anEq[i] = 1;
113059 p->nRow++;
113061 if( p->mxSample ){
113064 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
113066 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
113069 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
113071 nLt = p->current.anLt[p->nCol-1];
113073 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
113074 p->current.isPSample = 1;
113075 p->current.iCol = 0;
113076 sampleInsert(p, &p->current, p->nCol-1);
113077 p->current.isPSample = 0;
113081 for(i=0; i<(p->nCol-1); i++){
113082 p->current.iCol = i;
113083 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
113084 sampleCopy(p, &p->aBest[i], &p->current);
113089 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
113090 p->nSkipAhead++;
113091 sqlite3_result_int(context, p->current.anDLt[0]>0);
113129 ** a one-parameter function, stat_get(P), that always returns the
113146 assert( eCall==STAT_GET_STAT1 || p->mxSample );
113171 ** I = (K+D-1)/D
113181 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
113183 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
113184 for(i=0; i<p->nKeyCol; i++){
113185 u64 nDistinct = p->current.anDLt[i] + 1;
113186 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
113187 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
113189 assert( p->current.anEq[i] );
113195 if( p->iGet<0 ){
113197 p->iGet = 0;
113199 if( p->iGet<p->nSample ){
113200 StatSample *pS = p->a + p->iGet;
113201 if( pS->nRowid==0 ){
113202 sqlite3_result_int64(context, pS->u.iRowid);
113204 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
113213 assert( p->iGet<p->nSample );
113215 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
113216 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
113218 aCnt = p->a[p->iGet].anDLt;
113219 p->iGet++;
113223 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
113224 for(i=0; i<p->nCol; i++){
113227 if( sStat.nChar ) sStat.nChar--;
113249 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
113262 ** of the k-th column of the pIdx index.
113270 assert( k>=0 && k<pIdx->nColumn );
113271 i = pIdx->aiColumn[k];
113273 VdbeComment((v,"%s.rowid",pIdx->zName));
113275 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
113277 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
113296 sqlite3 *db = pParse->db; /* Database handle */
113302 int jZeroRows = -1; /* Jump from here if number of rows is zero */
113319 pParse->nMem = MAX(pParse->nMem, iMem);
113328 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
113333 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113337 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
113338 db->aDb[iDb].zDbSName ) ){
113344 if( db->xPreUpdateCallback ){
113347 pStat1->zName = (char*)&pStat1[1];
113348 memcpy(pStat1->zName, "sqlite_stat1", 13);
113349 pStat1->nCol = 3;
113350 pStat1->iPKey = -1;
113351 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
113355 /* Establish a read-lock on the table at the shared-cache level.
113356 ** Open a read-only cursor on the table. Also allocate a cursor number
113359 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113362 pParse->nTab = MAX(pParse->nTab, iTab);
113364 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
113366 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113374 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
113376 nCol = pIdx->nKeyCol;
113377 zIdxName = pTab->zName;
113378 nColTest = nCol - 1;
113380 nCol = pIdx->nColumn;
113381 zIdxName = pIdx->zName;
113382 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
113387 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
113390 ** Pseudo-code for loop that calls stat_push():
113425 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
113427 /* Open a read-only cursor on the index being analyzed. */
113428 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
113429 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
113431 VdbeComment((v, "%s", pIdx->zName));
113442 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
113456 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
113489 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
113490 /* For a single-column UNIQUE index, once we have found a non-NULL
113497 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
113517 sqlite3VdbeJumpHere(v, addrNextRow-1);
113540 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
113542 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
113543 for(j=0; j<pPk->nKeyCol; j++){
113544 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
113545 assert( k>=0 && k<pIdx->nColumn );
113549 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
113550 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
113558 if( db->nAnalysisLimit ){
113580 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
113586 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
113597 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
113615 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
113629 VdbeComment((v, "%s", pTab->zName));
113639 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
113661 sqlite3 *db = pParse->db;
113662 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
113669 iStatCur = pParse->nTab;
113670 pParse->nTab += 3;
113672 iMem = pParse->nMem+1;
113673 iTab = pParse->nTab;
113675 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
113692 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113693 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
113695 iStatCur = pParse->nTab;
113696 pParse->nTab += 3;
113698 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
113700 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
113702 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
113710 ** ANALYZE -- 1
113711 ** ANALYZE <database> -- 2
113712 ** ANALYZE ?<database>.?<tablename> -- 3
113719 sqlite3 *db = pParse->db;
113728 /* Read the database schema. If an error occurs, leave an error message
113730 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
113738 for(i=0; i<db->nDb; i++){
113742 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
113749 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
113753 analyzeTable(pParse, pIdx->pTable, pIdx);
113761 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
113777 ** The first argument points to a nul-terminated string containing a
113801 v = v*10 + c - '0';
113820 pIndex->bUnordered = 0;
113821 pIndex->noSkipScan = 0;
113824 pIndex->bUnordered = 1;
113825 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
113828 pIndex->szIdxRow = sqlite3LogEst(sz);
113830 pIndex->noSkipScan = 1;
113833 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
113834 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
113849 ** argv[2] = results of analysis - on integer for each column
113866 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
113875 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
113881 int nCol = pIndex->nKeyCol+1;
113886 if( pIndex->aiRowEst==0 ){
113887 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
113888 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
113890 aiRowEst = pIndex->aiRowEst;
113892 pIndex->bUnordered = 0;
113893 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
113894 pIndex->hasStat1 = 1;
113895 if( pIndex->pPartIdxWhere==0 ){
113896 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
113897 pTable->tabFlags |= TF_HasStat1;
113901 fakeIdx.szIdxRow = pTable->szTabRow;
113905 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
113906 pTable->szTabRow = fakeIdx.szIdxRow;
113907 pTable->tabFlags |= TF_HasStat1;
113919 if( pIdx->aSample ){
113921 for(j=0; j<pIdx->nSample; j++){
113922 IndexSample *p = &pIdx->aSample[j];
113923 sqlite3DbFree(db, p->p);
113925 sqlite3DbFree(db, pIdx->aSample);
113927 if( db && db->pnBytesFreed==0 ){
113928 pIdx->nSample = 0;
113929 pIdx->aSample = 0;
113939 ** Populate the pIdx->aAvgEq[] array based on the samples currently
113940 ** stored in pIdx->aSample[].
113944 IndexSample *aSample = pIdx->aSample;
113945 IndexSample *pFinal = &aSample[pIdx->nSample-1];
113948 if( pIdx->nSampleCol>1 ){
113953 nCol = pIdx->nSampleCol-1;
113954 pIdx->aAvgEq[nCol] = 1;
113957 int nSample = pIdx->nSample;
113965 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
113966 nRow = pFinal->anLt[iCol];
113967 nDist100 = (i64)100 * pFinal->anDLt[iCol];
113968 nSample--;
113970 nRow = pIdx->aiRowEst[0];
113971 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
113973 pIdx->nRowEst0 = nRow;
113980 if( i==(pIdx->nSample-1)
113989 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
113992 pIdx->aAvgEq[iCol] = avgEq;
114036 IndexSample *pSample; /* A slot in pIdx->aSample[] */
114038 assert( db->lookaside.bDisable );
114043 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114061 assert( pIdx==0 || pIdx->nSample==0 );
114063 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
114064 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
114065 nIdxCol = pIdx->nKeyCol;
114067 nIdxCol = pIdx->nColumn;
114069 pIdx->nSampleCol = nIdxCol;
114074 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
114075 if( pIdx->aSample==0 ){
114079 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
114080 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
114081 pIdx->pTable->tabFlags |= TF_HasStat4;
114083 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
114084 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
114085 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
114087 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
114096 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
114111 nCol = pIdx->nSampleCol;
114116 pSample = &pIdx->aSample[pIdx->nSample];
114117 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
114118 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
114119 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
114127 pSample->n = sqlite3_column_bytes(pStmt, 4);
114128 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
114129 if( pSample->p==0 ){
114133 if( pSample->n ){
114134 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
114136 pIdx->nSample++;
114151 assert( db->lookaside.bDisable );
114181 ** If an OOM error occurs, this function always sets db->mallocFailed.
114190 Schema *pSchema = db->aDb[iDb].pSchema;
114193 assert( iDb>=0 && iDb<db->nDb );
114194 assert( db->aDb[iDb].pBt!=0 );
114198 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
114200 pTab->tabFlags &= ~TF_HasStat1;
114202 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114204 pIdx->hasStat1 = 0;
114207 pIdx->aSample = 0;
114213 sInfo.zDatabase = db->aDb[iDb].zDbSName;
114229 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114231 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
114241 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
114243 sqlite3_free(pIdx->aiRowEst);
114244 pIdx->aiRowEst = 0;
114297 if( pExpr->op!=TK_ID ){
114300 pExpr->op = TK_STRING;
114312 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
114318 ** An SQL user-function registered to do the work of an ATTACH statement. The
114328 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
114329 ** new database, close the database on db->init.iDb and reopen it as an
114357 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
114364 ** from sqlite3_deserialize() to close database db->init.iDb and
114368 pNew = &db->aDb[db->init.iDb];
114369 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
114370 pNew->pBt = 0;
114371 pNew->pSchema = 0;
114372 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
114382 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
114383 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
114384 db->aLimit[SQLITE_LIMIT_ATTACHED]
114388 for(i=0; i<db->nDb; i++){
114396 /* Allocate the new entry in the db->aDb[] array and initialize the schema
114399 if( db->aDb==db->aDbStatic ){
114400 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
114402 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
114404 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
114407 db->aDb = aNew;
114408 pNew = &db->aDb[db->nDb];
114415 flags = db->openFlags;
114416 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
114419 sqlite3_result_error(context, zErr, -1);
114425 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
114426 db->nDb++;
114427 pNew->zDbSName = sqlite3DbStrDup(db, zName);
114429 db->noSharedCache = 0;
114435 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
114436 if( !pNew->pSchema ){
114438 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
114443 sqlite3BtreeEnter(pNew->pBt);
114444 pPager = sqlite3BtreePager(pNew->pBt);
114445 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
114446 sqlite3BtreeSecureDelete(pNew->pBt,
114447 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
114449 sqlite3BtreeSetPagerFlags(pNew->pBt,
114450 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
114452 sqlite3BtreeLeave(pNew->pBt);
114454 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
114455 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
114462 ** remove the entry from the db->aDb[] array. i.e. put everything back the
114467 db->init.iDb = 0;
114468 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
114479 if( newAuth<db->auth.authLevel ){
114486 int iDb = db->nDb - 1;
114488 if( db->aDb[iDb].pBt ){
114489 sqlite3BtreeClose(db->aDb[iDb].pBt);
114490 db->aDb[iDb].pBt = 0;
114491 db->aDb[iDb].pSchema = 0;
114494 db->nDb = iDb;
114511 sqlite3_result_error(context, zErrDyn, -1);
114518 ** An SQL user-function registered to do the work of an DETACH statement. The
114540 for(i=0; i<db->nDb; i++){
114541 pDb = &db->aDb[i];
114542 if( pDb->pBt==0 ) continue;
114546 if( i>=db->nDb ){
114554 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
114555 || sqlite3BtreeIsInBackup(pDb->pBt)
114563 assert( db->aDb[1].pSchema );
114564 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
114567 if( pTrig->pTabSchema==pDb->pSchema ){
114568 pTrig->pTabSchema = pTrig->pSchema;
114573 sqlite3BtreeClose(pDb->pBt);
114574 pDb->pBt = 0;
114575 pDb->pSchema = 0;
114580 sqlite3_result_error(context, zErr, -1);
114599 sqlite3* db = pParse->db;
114602 if( pParse->nErr ) goto attach_end;
114617 if( pAuthArg->op==TK_STRING ){
114619 zAuthArg = pAuthArg->u.zToken;
114637 assert( v || db->mallocFailed );
114639 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
114640 pFunc->nArg, pFunc, 0);
114699 DbFixer *pFix = p->u.pFix;
114700 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
114701 if( pExpr->op==TK_VARIABLE ){
114702 if( pFix->pParse->db->init.busy ){
114703 pExpr->op = TK_NULL;
114705 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
114716 DbFixer *pFix = p->u.pFix;
114719 sqlite3 *db = pFix->pParse->db;
114720 int iDb = sqlite3FindDbName(db, pFix->zDb);
114721 SrcList *pList = pSelect->pSrc;
114724 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
114725 if( pFix->bTemp==0 ){
114726 if( pItem->zDatabase ){
114727 if( iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
114728 sqlite3ErrorMsg(pFix->pParse,
114730 pFix->zType, pFix->pName, pItem->zDatabase);
114733 sqlite3DbFree(db, pItem->zDatabase);
114734 pItem->zDatabase = 0;
114735 pItem->fg.notCte = 1;
114737 pItem->pSchema = pFix->pSchema;
114738 pItem->fg.fromDDL = 1;
114741 if( pList->a[i].fg.isUsing==0
114742 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
114748 if( pSelect->pWith ){
114749 for(i=0; i<pSelect->pWith->nCte; i++){
114750 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
114769 sqlite3 *db = pParse->db;
114770 assert( db->nDb>iDb );
114771 pFix->pParse = pParse;
114772 pFix->zDb = db->aDb[iDb].zDbSName;
114773 pFix->pSchema = db->aDb[iDb].pSchema;
114774 pFix->zType = zType;
114775 pFix->pName = pName;
114776 pFix->bTemp = (iDb==1);
114777 pFix->w.pParse = pParse;
114778 pFix->w.xExprCallback = fixExprCb;
114779 pFix->w.xSelectCallback = fixSelectCb;
114780 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
114781 pFix->w.walkerDepth = 0;
114782 pFix->w.eCode = 0;
114783 pFix->w.u.pFix = pFix;
114796 ** to an object in a different database, an error message is added to
114797 ** pParse->zErrMsg and these routines return non-zero. If everything
114809 res = sqlite3WalkSelect(&pFix->w, &s);
114818 return sqlite3WalkSelect(&pFix->w, pSelect);
114824 return sqlite3WalkExpr(&pFix->w, pExpr);
114834 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
114835 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
114836 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
114837 || sqlite3FixSrcList(pFix, pStep->pFrom)
114844 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
114845 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
114846 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
114847 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
114848 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
114855 pStep = pStep->pNext;
114878 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
114925 ** means that the SQL statement will never-run - the sqlite3_exec() call
114941 sqlite3_mutex_enter(db->mutex);
114942 db->xAuth = (sqlite3_xauth)xAuth;
114943 db->pAuthArg = pArg;
114944 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
114945 sqlite3_mutex_leave(db->mutex);
114950 ** Write an error message into pParse->zErrMsg that explains that the
114951 ** user-supplied authorization function returned an illegal value.
114955 pParse->rc = SQLITE_ERROR;
114973 sqlite3 *db = pParse->db; /* Database handle */
114974 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
114977 if( db->init.busy ) return SQLITE_OK;
114978 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
114980 ,db->auth.zAuthUser
114985 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
114987 pParse->rc = SQLITE_AUTH;
115011 int iSrc; /* Index in pTabList->a[] of table being read */
115015 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
115017 assert( pParse->db->xAuth!=0 );
115018 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
115025 if( pExpr->op==TK_TRIGGER ){
115026 pTab = pParse->pTriggerTab;
115029 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
115030 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
115031 pTab = pTabList->a[iSrc].pTab;
115036 iCol = pExpr->iColumn;
115040 assert( iCol<pTab->nCol );
115041 zCol = pTab->aCol[iCol].zCnName;
115042 }else if( pTab->iPKey>=0 ){
115043 assert( pTab->iPKey<pTab->nCol );
115044 zCol = pTab->aCol[pTab->iPKey].zCnName;
115048 assert( iDb>=0 && iDb<pParse->db->nDb );
115049 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
115050 pExpr->op = TK_NULL;
115057 ** is returned, then the error count and error message in pParse are
115067 sqlite3 *db = pParse->db;
115073 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
115074 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
115078 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
115079 ** callback are either NULL pointers or zero-terminated strings that
115087 testcase( pParse->zAuthContext==0 );
115089 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
115091 ,db->auth.zAuthUser
115096 pParse->rc = SQLITE_AUTH;
115107 ** popped. Or if pParse==0, this routine is a no-op.
115115 pContext->pParse = pParse;
115116 pContext->zAuthContext = pParse->zAuthContext;
115117 pParse->zAuthContext = zContext;
115125 if( pContext->pParse ){
115126 pContext->pParse->zAuthContext = pContext->zAuthContext;
115127 pContext->pParse = 0;
115174 ** Record the fact that we want to lock a table at run-time.
115197 for(i=0; i<pToplevel->nTableLock; i++){
115198 p = &pToplevel->aTableLock[i];
115199 if( p->iDb==iDb && p->iTab==iTab ){
115200 p->isWriteLock = (p->isWriteLock || isWriteLock);
115205 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
115206 pToplevel->aTableLock =
115207 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
115208 if( pToplevel->aTableLock ){
115209 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
115210 p->iDb = iDb;
115211 p->iTab = iTab;
115212 p->isWriteLock = isWriteLock;
115213 p->zLockName = zName;
115215 pToplevel->nTableLock = 0;
115216 sqlite3OomFault(pToplevel->db);
115227 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
115237 Vdbe *pVdbe = pParse->pVdbe;
115240 for(i=0; i<pParse->nTableLock; i++){
115241 TableLock *p = &pParse->aTableLock[i];
115242 int p1 = p->iDb;
115243 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
115244 p->zLockName, P4_STATIC);
115252 ** Return TRUE if the given yDbMask object is empty - if it contains no
115278 assert( pParse->pToplevel==0 );
115279 db = pParse->db;
115280 assert( db->pParse==pParse );
115281 if( pParse->nested ) return;
115282 if( pParse->nErr ){
115283 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
115286 assert( db->mallocFailed==0 );
115291 v = pParse->pVdbe;
115293 if( db->init.busy ){
115294 pParse->rc = SQLITE_DONE;
115298 if( v==0 ) pParse->rc = SQLITE_ERROR;
115300 assert( !pParse->isMultiWrite
115301 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
115303 if( pParse->bReturning ){
115304 Returning *pReturning = pParse->u1.pReturning;
115309 if( pReturning->nRetCol ){
115312 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
115314 reg = pReturning->iRetReg;
115315 for(i=0; i<pReturning->nRetCol; i++){
115316 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
115319 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
115327 if( pParse->nTableLock>0 && db->init.busy==0 ){
115329 if( db->auth.authLevel<UAUTH_User ){
115331 pParse->rc = SQLITE_AUTH_USER;
115343 if( db->mallocFailed==0
115344 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
115347 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
115349 assert( db->nDb>0 );
115353 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
115355 pSchema = db->aDb[iDb].pSchema;
115359 DbMaskTest(pParse->writeMask,iDb), /* P2 */
115360 pSchema->schema_cookie, /* P3 */
115361 pSchema->iGeneration /* P4 */
115363 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
115365 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
115366 }while( ++iDb<db->nDb );
115368 for(i=0; i<pParse->nVtabLock; i++){
115369 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
115372 pParse->nVtabLock = 0;
115376 ** obtain the required table-locks. This is a no-op unless the
115377 ** shared-cache feature is enabled.
115392 if( pParse->pConstExpr ){
115393 ExprList *pEL = pParse->pConstExpr;
115394 pParse->okConstFactor = 0;
115395 for(i=0; i<pEL->nExpr; i++){
115396 int iReg = pEL->a[i].u.iConstExprReg;
115398 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
115403 if( pParse->bReturning ){
115404 Returning *pRet = pParse->u1.pReturning;
115405 if( pRet->nRetCol ){
115406 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
115417 assert( v!=0 || pParse->nErr );
115418 assert( db->mallocFailed==0 || pParse->nErr );
115419 if( pParse->nErr==0 ){
115422 assert( pParse->pAinc==0 || pParse->nTab>0 );
115424 pParse->rc = SQLITE_DONE;
115426 pParse->rc = SQLITE_ERROR;
115439 ** * Built-in SQL functions always take precedence over application-defined
115441 ** built-in function.
115446 sqlite3 *db = pParse->db;
115447 u32 savedDbFlags = db->mDbFlags;
115450 if( pParse->nErr ) return;
115451 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
115459 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
115460 pParse->nErr++;
115463 pParse->nested++;
115466 db->mDbFlags |= DBFLAG_PreferBuiltin;
115468 db->mDbFlags = savedDbFlags;
115471 pParse->nested--;
115485 ** Locate the in-memory structure that describes a particular database
115505 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
115510 for(i=0; i<db->nDb; i++){
115511 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
115513 if( i>=db->nDb ){
115522 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115529 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115534 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
115541 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
115544 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
115547 for(i=2; i<db->nDb; i++){
115549 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
115554 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
115556 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
115565 ** Locate the in-memory structure that describes a particular database
115568 ** error message in pParse->zErrMsg.
115571 ** routine leaves an error message in pParse->zErrMsg where
115581 sqlite3 *db = pParse->db;
115583 /* Read the database schema. If an error occurs, leave an error message
115585 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
115597 if( pParse->disableVtab==0 && db->init.busy==0 ){
115598 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
115603 testcase( pMod->pEpoTab==0 );
115604 return pMod->pEpoTab;
115609 pParse->checkSchema = 1;
115610 }else if( IsVirtual(p) && pParse->disableVtab ){
115622 assert( HasRowid(p) || p->iPKey<0 );
115633 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
115634 ** non-NULL if it is part of a view or trigger program definition. See
115643 assert( p->pSchema==0 || p->zDatabase==0 );
115644 if( p->pSchema ){
115645 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
115646 zDb = pParse->db->aDb[iDb].zDbSName;
115648 zDb = p->zDatabase;
115650 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
115670 ** Locate the in-memory structure that describes
115686 for(i=OMIT_TEMPDB; i<db->nDb; i++){
115688 Schema *pSchema = db->aDb[j].pSchema;
115692 p = sqlite3HashFind(&pSchema->idxHash, zName);
115705 sqlite3ExprDelete(db, p->pPartIdxWhere);
115706 sqlite3ExprListDelete(db, p->aColExpr);
115707 sqlite3DbFree(db, p->zColAff);
115708 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
115710 sqlite3_free(p->aiRowEst);
115726 pHash = &db->aDb[iDb].pSchema->idxHash;
115729 if( pIndex->pTable->pIndex==pIndex ){
115730 pIndex->pTable->pIndex = pIndex->pNext;
115735 p = pIndex->pTable->pIndex;
115736 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
115737 if( ALWAYS(p && p->pNext==pIndex) ){
115738 p->pNext = pIndex->pNext;
115743 db->mDbFlags |= DBFLAG_SchemaChange;
115747 ** Look through the list of open database files in db->aDb[] and if
115749 ** db->aDb[] structure to a smaller size, if possible.
115756 for(i=j=2; i<db->nDb; i++){
115757 struct Db *pDb = &db->aDb[i];
115758 if( pDb->pBt==0 ){
115759 sqlite3DbFree(db, pDb->zDbSName);
115760 pDb->zDbSName = 0;
115764 db->aDb[j] = db->aDb[i];
115768 db->nDb = j;
115769 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
115770 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
115771 sqlite3DbFree(db, db->aDb);
115772 db->aDb = db->aDbStatic;
115778 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
115783 assert( iDb<db->nDb );
115789 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
115792 if( db->nSchemaLock==0 ){
115793 for(i=0; i<db->nDb; i++){
115795 sqlite3SchemaClear(db->aDb[i].pSchema);
115808 for(i=0; i<db->nDb; i++){
115809 Db *pDb = &db->aDb[i];
115810 if( pDb->pSchema ){
115811 if( db->nSchemaLock==0 ){
115812 sqlite3SchemaClear(pDb->pSchema);
115818 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
115821 if( db->nSchemaLock==0 ){
115830 db->mDbFlags &= ~DBFLAG_SchemaChange;
115846 pList = pTab->u.tab.pDfltList;
115847 if( pCol->iDflt==0
115849 || NEVER(pList->nExpr<pCol->iDflt)
115851 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
115852 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
115854 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
115855 pList->a[pCol->iDflt-1].pExpr = pExpr;
115865 if( pCol->iDflt==0 ) return 0;
115867 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
115868 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
115869 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
115884 n = sqlite3Strlen30(pCol->zCnName) + 1;
115885 if( pCol->colFlags & COLFLAG_HASTYPE ){
115886 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
115889 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
115891 pCol->zCnName = zNew;
115892 memcpy(pCol->zCnName + n, zColl, nColl);
115893 pCol->colFlags |= COLFLAG_HASCOLL;
115902 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
115903 z = pCol->zCnName;
115905 if( pCol->colFlags & COLFLAG_HASTYPE ){
115919 if( (pCol = pTable->aCol)!=0 ){
115920 for(i=0; i<pTable->nCol; i++, pCol++){
115921 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
115922 sqlite3DbFree(db, pCol->zCnName);
115924 sqlite3DbFree(db, pTable->aCol);
115926 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
115928 if( db==0 || db->pnBytesFreed==0 ){
115929 pTable->aCol = 0;
115930 pTable->nCol = 0;
115932 pTable->u.tab.pDfltList = 0;
115950 ** db parameter can be used with db->pnBytesFreed to measure the memory
115965 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
115971 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
115972 pNext = pIndex->pNext;
115973 assert( pIndex->pSchema==pTable->pSchema
115974 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
115975 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
115976 char *zName = pIndex->zName;
115978 &pIndex->pSchema->idxHash, zName, 0
115980 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
115996 sqlite3SelectDelete(db, pTable->u.view.pSelect);
116002 sqlite3DbFree(db, pTable->zName);
116003 sqlite3DbFree(db, pTable->zColAff);
116004 sqlite3ExprListDelete(db, pTable->pCheck);
116013 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
116027 assert( iDb>=0 && iDb<db->nDb );
116030 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
116031 pDb = &db->aDb[iDb];
116032 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
116034 db->mDbFlags |= DBFLAG_SchemaChange;
116053 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
116069 if( p->nTab==0 ){
116070 p->nTab = 1;
116075 ** Parameter zName points to a nul-terminated buffer containing the name
116077 ** function returns the index of the named database in db->aDb[], or
116078 ** -1 if the named db cannot be found.
116081 int i = -1; /* Database number */
116084 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
116085 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
116097 ** index of the named database in db->aDb[], or -1 if the named db
116132 sqlite3 *db = pParse->db;
116135 if( pName2->n>0 ){
116136 if( db->init.busy ) {
116138 return -1;
116144 return -1;
116147 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
116148 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
116149 iDb = db->init.iDb;
116159 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
116160 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116162 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116164 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
116166 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
116170 ** This routine is used to check if the UTF-8 string zName is a legal
116186 sqlite3 *db = pParse->db;
116188 || db->init.imposterTable
116194 if( db->init.busy ){
116195 if( sqlite3_stricmp(zType, db->init.azInit[0])
116196 || sqlite3_stricmp(zName, db->init.azInit[1])
116197 || sqlite3_stricmp(zTblName, db->init.azInit[2])
116203 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
116220 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
116227 ** find the (first) offset of that column in index pIdx. Or return -1
116232 for(i=0; i<pIdx->nColumn; i++){
116233 if( iCol==pIdx->aiColumn[i] ) return i;
116235 return -1;
116248 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
116251 if( pTab->tabFlags & TF_HasVirtual ){
116254 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
116266 ** the N-th virtual column (zero-based) then the storage number is
116267 ** the number of non-virtual columns in the table plus N.
116283 ** -- 0 1 2 3 4 5 6 7 8
116294 ** this routine is a no-op macro. If the pTab does not have any virtual
116295 ** columns, then this routine is no-op that always return iCol. If iCol
116301 assert( iCol<pTab->nCol );
116302 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
116304 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
116306 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
116308 return pTab->nNVCol + i - n;
116322 ** is a read-only no-op.
116325 int iReg = ++pParse->nMem;
116343 ** The new table record is initialized and put in pParse->pNewTable.
116360 sqlite3 *db = pParse->db;
116365 if( db->init.busy && db->init.newTnum==1 ){
116367 iDb = db->init.iDb;
116374 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
116386 pParse->sNameToken = *pName;
116391 if( db->init.iDb==1 ) isTemp = 1;
116402 char *zDb = db->aDb[iDb].zDbSName;
116414 ** index or table name in the same database. Issue an error message if
116421 char *zDb = db->aDb[iDb].zDbSName;
116431 assert( !db->init.busy || CORRUPT_DB );
116445 assert( db->mallocFailed );
116446 pParse->rc = SQLITE_NOMEM_BKPT;
116447 pParse->nErr++;
116450 pTable->zName = zName;
116451 pTable->iPKey = -1;
116452 pTable->pSchema = db->aDb[iDb].pSchema;
116453 pTable->nTabRef = 1;
116455 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
116457 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
116459 assert( pParse->pNewTable==0 );
116460 pParse->pNewTable = pTable;
116470 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
116487 reg1 = pParse->regRowid = ++pParse->nMem;
116488 reg2 = pParse->regRoot = ++pParse->nMem;
116489 reg3 = ++pParse->nMem;
116493 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
116499 /* This just creates a place-holder record in the sqlite_schema table.
116503 ** The rowid for the new entry is left in register pParse->regRowid.
116504 ** The root page number of the new table is left in reg pParse->regRoot.
116514 assert( !pParse->bReturning );
116515 pParse->u1.addrCrTab =
116526 /* Normal (non-error) return. */
116531 pParse->checkSchema = 1;
116541 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
116542 pCol->colFlags |= COLFLAG_HIDDEN;
116543 if( pTab ) pTab->tabFlags |= TF_HasHidden;
116544 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
116545 pTab->tabFlags |= TF_OOOHidden;
116553 ** with any application-generated triggers.
116562 pHash = &(db->aDb[1].pSchema->trigHash);
116564 sqlite3ExprListDelete(db, pRet->pReturnEL);
116587 sqlite3 *db = pParse->db;
116588 if( pParse->pNewTrigger ){
116591 assert( pParse->bReturning==0 );
116593 pParse->bReturning = 1;
116599 pParse->u1.pReturning = pRet;
116600 pRet->pParse = pParse;
116601 pRet->pReturnEL = pList;
116604 testcase( pParse->earlyCleanup );
116605 if( db->mallocFailed ) return;
116606 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
116607 pRet->retTrig.op = TK_RETURNING;
116608 pRet->retTrig.tr_tm = TRIGGER_AFTER;
116609 pRet->retTrig.bReturning = 1;
116610 pRet->retTrig.pSchema = db->aDb[1].pSchema;
116611 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
116612 pRet->retTrig.step_list = &pRet->retTStep;
116613 pRet->retTStep.op = TK_RETURNING;
116614 pRet->retTStep.pTrig = &pRet->retTrig;
116615 pRet->retTStep.pExprList = pList;
116616 pHash = &(db->aDb[1].pSchema->trigHash);
116617 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
116618 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
116619 ==&pRet->retTrig ){
116638 sqlite3 *db = pParse->db;
116645 if( (p = pParse->pNewTable)==0 ) return;
116646 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
116647 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
116657 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
116659 sType.n -= 6;
116660 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
116662 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
116664 sType.n -= 9;
116665 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
116694 for(i=0; i<p->nCol; i++){
116695 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zCnName)==0 ){
116701 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
116706 p->aCol = aNew;
116707 pCol = &p->aCol[p->nCol];
116708 memset(pCol, 0, sizeof(p->aCol[0]));
116709 pCol->zCnName = z;
116710 pCol->hName = hName;
116716 pCol->affinity = affinity;
116717 pCol->eCType = eType;
116718 pCol->szEst = szEst;
116722 pCol->colFlags |= COLFLAG_SORTERREF;
116731 pCol->affinity = sqlite3AffinityType(zType, pCol);
116732 pCol->colFlags |= COLFLAG_HASTYPE;
116734 p->nCol++;
116735 p->nNVCol++;
116736 pParse->constraintName.n = 0;
116748 p = pParse->pNewTable;
116749 if( p==0 || NEVER(p->nCol<1) ) return;
116750 pCol = &p->aCol[p->nCol-1];
116751 pCol->notNull = (u8)onError;
116752 p->tabFlags |= TF_HasNotNull;
116756 if( pCol->colFlags & COLFLAG_UNIQUE ){
116758 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
116759 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
116760 if( pIdx->aiColumn[0]==p->nCol-1 ){
116761 pIdx->uniqNotNull = 1;
116771 ** This routine does a case-independent search of zType for the
116779 ** --------------------------------
116837 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
116844 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
116849 pCol->colFlags |= COLFLAG_SORTERREF;
116854 pCol->szEst = v;
116877 sqlite3 *db = pParse->db;
116878 p = pParse->pNewTable;
116880 int isInit = db->init.busy && db->init.iDb!=1;
116881 pCol = &(p->aCol[p->nCol-1]);
116884 pCol->zCnName);
116886 }else if( pCol->colFlags & COLFLAG_GENERATED ){
116887 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116888 testcase( pCol->colFlags & COLFLAG_STORED );
116929 if( p->op==TK_STRING ){
116930 p->op = TK_ID;
116931 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
116932 p->pLeft->op = TK_ID;
116940 pCol->colFlags |= COLFLAG_PRIMKEY;
116942 if( pCol->colFlags & COLFLAG_GENERATED ){
116943 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
116944 testcase( pCol->colFlags & COLFLAG_STORED );
116963 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
116976 Table *pTab = pParse->pNewTable;
116978 int iCol = -1, i;
116981 if( pTab->tabFlags & TF_HasPrimaryKey ){
116983 "table \"%s\" has more than one primary key", pTab->zName);
116986 pTab->tabFlags |= TF_HasPrimaryKey;
116988 iCol = pTab->nCol - 1;
116989 pCol = &pTab->aCol[iCol];
116993 nTerm = pList->nExpr;
116995 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
116998 if( pCExpr->op==TK_ID ){
117001 zCName = pCExpr->u.zToken;
117002 for(iCol=0; iCol<pTab->nCol; iCol++){
117003 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zCnName)==0 ){
117004 pCol = &pTab->aCol[iCol];
117014 && pCol->eCType==COLTYPE_INTEGER
117018 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
117019 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
117021 pTab->iPKey = iCol;
117022 pTab->keyConf = (u8)onError;
117024 pTab->tabFlags |= autoInc*TF_Autoincrement;
117025 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
117039 sqlite3ExprListDelete(pParse->db, pList);
117053 Table *pTab = pParse->pNewTable;
117054 sqlite3 *db = pParse->db;
117056 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
117058 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
117059 if( pParse->constraintName.n ){
117060 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
117064 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
117066 t.n = (int)(zEnd - t.z);
117067 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
117072 sqlite3ExprDelete(pParse->db, pCheckExpr);
117086 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
117087 i = p->nCol-1;
117088 db = pParse->db;
117094 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
117100 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117101 assert( pIdx->nKeyCol==1 );
117102 if( pIdx->aiColumn[0]==i ){
117103 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
117116 Table *pTab = pParse->pNewTable;
117122 pCol = &(pTab->aCol[pTab->nCol-1]);
117127 if( pCol->iDflt>0 ) goto generated_error;
117129 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
117130 /* no-op */
117131 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
117137 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
117138 pCol->colFlags |= eType;
117141 pTab->tabFlags |= eType;
117142 if( pCol->colFlags & COLFLAG_PRIMKEY ){
117143 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
117151 pCol->zCnName);
117153 sqlite3ExprDelete(pParse->db, pExpr);
117156 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
117158 sqlite3ExprDelete(pParse->db, pExpr);
117172 ** This plan is not completely bullet-proof. It is possible for
117178 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
117179 ** the schema-version whenever the schema changes.
117182 sqlite3 *db = pParse->db;
117183 Vdbe *v = pParse->pVdbe;
117186 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
117209 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
117213 ** If the string zSignedIdent consists entirely of alpha-numeric
117216 ** it is quoted using double-quotes.
117252 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
117253 n += identLength(pCol->zCnName) + 5;
117255 n += identLength(p->zName);
117265 n += 35 + 6*p->nCol;
117273 identPut(zStmt, &k, p->zName);
117275 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
117286 sqlite3_snprintf(n-k, &zStmt[k], zSep);
117289 identPut(zStmt, &k, pCol->zCnName);
117290 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
117291 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
117292 testcase( pCol->affinity==SQLITE_AFF_BLOB );
117293 testcase( pCol->affinity==SQLITE_AFF_TEXT );
117294 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
117295 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
117296 testcase( pCol->affinity==SQLITE_AFF_REAL );
117298 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
117300 assert( pCol->affinity==SQLITE_AFF_BLOB
117301 || pCol->affinity==sqlite3AffinityType(zType, 0) );
117306 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
117317 if( pIdx->nColumn>=N ) return SQLITE_OK;
117318 assert( pIdx->isResized==0 );
117322 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
117323 pIdx->azColl = (const char**)zExtra;
117325 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
117326 pIdx->aiRowLogEst = (LogEst*)zExtra;
117328 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
117329 pIdx->aiColumn = (i16*)zExtra;
117331 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
117332 pIdx->aSortOrder = (u8*)zExtra;
117333 pIdx->nColumn = N;
117334 pIdx->isResized = 1;
117345 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
117346 wTable += pTabCol->szEst;
117348 if( pTab->iPKey<0 ) wTable++;
117349 pTab->szTabRow = sqlite3LogEst(wTable*4);
117358 const Column *aCol = pIdx->pTable->aCol;
117359 for(i=0; i<pIdx->nColumn; i++){
117360 i16 x = pIdx->aiColumn[i];
117361 assert( x<pIdx->pTable->nCol );
117362 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
117364 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
117372 while( nCol-- > 0 ){
117382 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
117395 assert( nKey<=pIdx->nColumn );
117396 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
117397 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
117398 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
117399 assert( pPk->pTable==pIdx->pTable );
117401 j = pPk->aiColumn[iCol];
117404 assert( pIdx->aiColumn[i]>=0 || j>=0 );
117405 if( pIdx->aiColumn[i]==j
117406 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
117418 ** high-order bit of colNotIdxed is always 1. All unindexed columns
117421 ** 2019-10-24: For the purpose of this computation, virtual columns are
117429 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
117435 Table *pTab = pIdx->pTable;
117436 for(j=pIdx->nColumn-1; j>=0; j--){
117437 int x = pIdx->aiColumn[j];
117438 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
117439 testcase( x==BMS-1 );
117440 testcase( x==BMS-2 );
117441 if( x<BMS-1 ) m |= MASKBIT(x);
117444 pIdx->colNotIdxed = ~m;
117445 assert( (pIdx->colNotIdxed>>63)==1 );
117478 sqlite3 *db = pParse->db;
117479 Vdbe *v = pParse->pVdbe;
117483 if( !db->init.imposterTable ){
117484 for(i=0; i<pTab->nCol; i++){
117485 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
117486 && (pTab->aCol[i].notNull==OE_None)
117488 pTab->aCol[i].notNull = OE_Abort;
117491 pTab->tabFlags |= TF_HasNotNull;
117497 assert( !pParse->bReturning );
117498 if( pParse->u1.addrCrTab ){
117500 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
117506 if( pTab->iPKey>=0 ){
117509 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
117513 pTab->tabFlags &= ~TF_WithoutRowid;
117517 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
117519 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
117520 assert( pParse->pNewTable==pTab );
117521 pTab->iPKey = -1;
117522 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
117524 if( pParse->nErr ){
117525 pTab->tabFlags &= ~TF_WithoutRowid;
117528 assert( db->mallocFailed==0 );
117530 assert( pPk->nKeyCol==1 );
117540 for(i=j=1; i<pPk->nKeyCol; i++){
117542 pPk->nColumn--;
117544 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
117545 pPk->azColl[j] = pPk->azColl[i];
117546 pPk->aSortOrder[j] = pPk->aSortOrder[i];
117547 pPk->aiColumn[j++] = pPk->aiColumn[i];
117550 pPk->nKeyCol = j;
117553 pPk->isCovering = 1;
117554 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
117555 nPk = pPk->nColumn = pPk->nKeyCol;
117561 if( v && pPk->tnum>0 ){
117562 assert( db->init.busy==0 );
117563 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
117567 pPk->tnum = pTab->tnum;
117569 /* Update the in-memory representation of all UNIQUE indices by converting
117572 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117576 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117577 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117583 pIdx->nColumn = pIdx->nKeyCol;
117586 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
117587 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
117588 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
117589 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
117590 pIdx->aiColumn[j] = pPk->aiColumn[i];
117591 pIdx->azColl[j] = pPk->azColl[i];
117592 if( pPk->aSortOrder[i] ){
117594 pIdx->bAscKeyBug = 1;
117599 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
117600 assert( pIdx->nColumn>=j );
117606 for(i=0; i<pTab->nCol; i++){
117607 if( !hasColumn(pPk->aiColumn, nPk, i)
117608 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
117611 for(i=0, j=nPk; i<pTab->nCol; i++){
117612 if( !hasColumn(pPk->aiColumn, j, i)
117613 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
117615 assert( j<pPk->nColumn );
117616 pPk->aiColumn[j] = i;
117617 pPk->azColl[j] = sqlite3StrBINARY;
117621 assert( pPk->nColumn==j );
117622 assert( pTab->nNVCol<=j );
117637 nName = sqlite3Strlen30(pTab->zName);
117638 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
117640 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117642 if( pMod->pModule->iVersion<3 ) return 0;
117643 if( pMod->pModule->xShadowName==0 ) return 0;
117644 return pMod->pModule->xShadowName(zName+nName+1);
117656 int nName; /* Length of pTab->zName */
117661 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
117663 if( NEVER(pMod->pModule==0) ) return;
117664 if( pMod->pModule->iVersion<3 ) return;
117665 if( pMod->pModule->xShadowName==0 ) return;
117666 assert( pTab->zName!=0 );
117667 nName = sqlite3Strlen30(pTab->zName);
117668 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
117670 assert( pOther->zName!=0 );
117672 if( pOther->tabFlags & TF_Shadow ) continue;
117673 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
117674 && pOther->zName[nName]=='_'
117675 && pMod->pModule->xShadowName(pOther->zName+nName+1)
117677 pOther->tabFlags |= TF_Shadow;
117728 #define markExprListImmutable(X) /* no-op */
117741 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
117760 sqlite3 *db = pParse->db; /* The database connection */
117767 p = pParse->pNewTable;
117770 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
117771 p->tabFlags |= TF_Shadow;
117774 /* If the db->init.busy is 1 it means we are reading the SQL off the
117777 ** for the table from the db->init.newTnum field. (The page number
117781 ** table itself. So mark it read-only.
117783 if( db->init.busy ){
117784 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
117788 p->tnum = db->init.newTnum;
117789 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
117803 p->tabFlags |= TF_Strict;
117804 for(ii=0; ii<p->nCol; ii++){
117805 Column *pCol = &p->aCol[ii];
117806 if( pCol->eCType==COLTYPE_CUSTOM ){
117807 if( pCol->colFlags & COLFLAG_HASTYPE ){
117810 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
117814 p->zName, pCol->zCnName);
117817 }else if( pCol->eCType==COLTYPE_ANY ){
117818 pCol->affinity = SQLITE_AFF_BLOB;
117820 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
117821 && p->iPKey!=ii
117822 && pCol->notNull == OE_None
117824 pCol->notNull = OE_Abort;
117825 p->tabFlags |= TF_HasNotNull;
117830 assert( (p->tabFlags & TF_HasPrimaryKey)==0
117831 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
117832 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
117833 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
117837 if( (p->tabFlags & TF_Autoincrement) ){
117842 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
117843 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
117846 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
117849 iDb = sqlite3SchemaToIndex(db, p->pSchema);
117854 if( p->pCheck ){
117855 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
117856 if( pParse->nErr ){
117859 sqlite3ExprListDelete(db, p->pCheck);
117860 p->pCheck = 0;
117862 markExprListImmutable(p->pCheck);
117867 if( p->tabFlags & TF_HasGenerated ){
117869 testcase( p->tabFlags & TF_HasVirtual );
117870 testcase( p->tabFlags & TF_HasStored );
117871 for(ii=0; ii<p->nCol; ii++){
117872 u32 colFlags = p->aCol[ii].colFlags;
117874 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
117884 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
117892 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
117900 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
117910 if( !db->init.busy ){
117938 ** statement to populate the new table. The root-page number for the
117939 ** new table is in register pParse->regRoot.
117945 ** A shared-cache write-lock is not required to write to the new table,
117946 ** as a schema-lock must have already been obtained to create it. Since
117947 ** a schema-lock excludes all other database users, the write-lock would
117952 int regYield; /* Register holding co-routine entry-point */
117953 int addrTop; /* Top of the co-routine */
117960 pParse->rc = SQLITE_ERROR;
117961 pParse->nErr++;
117964 regYield = ++pParse->nMem;
117965 regRec = ++pParse->nMem;
117966 regRowid = ++pParse->nMem;
117967 assert(pParse->nTab==1);
117969 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
117971 pParse->nTab = 2;
117974 if( pParse->nErr ) return;
117977 assert( p->aCol==0 );
117978 p->nCol = p->nNVCol = pSelTab->nCol;
117979 p->aCol = pSelTab->aCol;
117980 pSelTab->nCol = 0;
117981 pSelTab->aCol = 0;
117985 if( pParse->nErr ) return;
117987 sqlite3VdbeJumpHere(v, addrTop - 1);
118003 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
118004 n = (int)(pEnd2->z - pParse->sNameToken.z);
118005 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
118007 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
118019 db->aDb[iDb].zDbSName,
118021 p->zName,
118022 p->zName,
118023 pParse->regRoot,
118025 pParse->regRowid
118034 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
118035 Db *pDb = &db->aDb[iDb];
118037 if( pDb->pSchema->pSeqTab==0 ){
118040 pDb->zDbSName
118048 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
118051 /* Add the table to the in-memory representation of the database.
118053 if( db->init.busy ){
118055 Schema *pSchema = p->pSchema;
118057 assert( HasRowid(p) || p->iPKey<0 );
118058 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
118064 pParse->pNewTable = 0;
118065 db->mDbFlags |= DBFLAG_SchemaChange;
118070 assert( !pParse->nested );
118072 if( strcmp(p->zName, "sqlite_sequence")==0 ){
118074 p->pSchema->pSeqTab = p;
118082 if( pCons->z==0 ){
118085 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
118111 sqlite3 *db = pParse->db;
118113 if( pParse->nVar>0 ){
118118 p = pParse->pNewTable;
118119 if( p==0 || pParse->nErr ) goto create_view_fail;
118124 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
118127 p->tabFlags |= TF_NoVisibleRowid;
118131 iDb = sqlite3SchemaToIndex(db, p->pSchema);
118137 ** allocated rather than point to the input string - which means that
118140 pSelect->selFlags |= SF_View;
118142 p->u.view.pSelect = pSelect;
118145 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
118147 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
118148 p->eTabType = TABTYP_VIEW;
118149 if( db->mallocFailed ) goto create_view_fail;
118154 sEnd = pParse->sLastToken;
118160 n = (int)(sEnd.z - pBegin->z);
118162 z = pBegin->z;
118163 while( sqlite3Isspace(z[n-1]) ){ n--; }
118164 sEnd.z = &z[n-1];
118184 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
118190 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
118202 db->nSchemaLock++;
118204 db->nSchemaLock--;
118213 if( pTable->nCol>0 ) return 0;
118230 if( pTable->nCol<0 ){
118231 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
118234 assert( pTable->nCol>=0 );
118244 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
118246 u8 eParseMode = pParse->eParseMode;
118247 int nTab = pParse->nTab;
118248 int nSelect = pParse->nSelect;
118249 pParse->eParseMode = PARSE_MODE_NORMAL;
118250 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
118251 pTable->nCol = -1;
118254 xAuth = db->xAuth;
118255 db->xAuth = 0;
118257 db->xAuth = xAuth;
118261 pParse->nTab = nTab;
118262 pParse->nSelect = nSelect;
118264 pTable->nCol = 0;
118266 }else if( pTable->pCheck ){
118269 ** arglist which is stored in pTable->pCheck. The pCheck field
118273 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
118274 &pTable->nCol, &pTable->aCol);
118275 if( pParse->nErr==0
118276 && pTable->nCol==pSel->pEList->nExpr
118278 assert( db->mallocFailed==0 );
118286 assert( pTable->aCol==0 );
118287 pTable->nCol = pSelTab->nCol;
118288 pTable->aCol = pSelTab->aCol;
118289 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
118290 pSelTab->nCol = 0;
118291 pSelTab->aCol = 0;
118292 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
118294 pTable->nNVCol = pTable->nCol;
118298 pParse->eParseMode = eParseMode;
118302 pTable->pSchema->schemaFlags |= DB_UnresetViews;
118303 if( db->mallocFailed ){
118319 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
118334 ** root-page of a table or index in database iDb has changed from iFrom
118355 pDb = &db->aDb[iDb];
118356 pHash = &pDb->pSchema->tblHash;
118359 if( pTab->tnum==iFrom ){
118360 pTab->tnum = iTo;
118363 pHash = &pDb->pSchema->idxHash;
118366 if( pIdx->tnum==iFrom ){
118367 pIdx->tnum = iTo;
118374 ** Write code to erase the table with root-page iTable from database iDb.
118376 ** if a root-page of another table is moved by the btree-layer whilst
118377 ** erasing iTable (this can happen with an auto-vacuum database).
118387 ** is non-zero, then it is the root page number of a table moved to
118398 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
118406 ** in case a root-page belonging to another table is moved by the btree layer
118407 ** is also added (this can happen with an auto-vacuum database).
118410 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
118412 ** table and index root-pages in order, starting with the numerically
118413 ** largest root-page number. This guarantees that none of the root-pages
118421 ** and root page 5 happened to be the largest root-page number in the
118424 ** a free-list page.
118426 Pgno iTab = pTab->tnum;
118436 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
118437 Pgno iIdx = pIdx->tnum;
118438 assert( pIdx->pSchema==pTab->pSchema );
118446 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
118447 assert( iDb>=0 && iDb<pParse->db->nDb );
118465 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
118469 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
118483 sqlite3 *db = pParse->db;
118485 Db *pDb = &db->aDb[iDb];
118503 assert( pTrigger->pSchema==pTab->pSchema ||
118504 pTrigger->pSchema==db->aDb[1].pSchema );
118506 pTrigger = pTrigger->pNext;
118513 ** move as a result of the drop (can happen in auto-vacuum mode).
118515 if( pTab->tabFlags & TF_Autoincrement ){
118518 pDb->zDbSName, pTab->zName
118533 pDb->zDbSName, pTab->zName);
118542 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
118545 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
118551 ** Return TRUE if shadow tables should be read-only in the current
118556 if( (db->flags & SQLITE_Defensive)!=0
118557 && db->pVtabCtx==0
118558 && db->nVdbeExec==0
118571 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
118572 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
118573 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
118576 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
118579 if( pTab->tabFlags & TF_Eponymous ){
118592 sqlite3 *db = pParse->db;
118595 if( db->mallocFailed ){
118598 assert( pParse->nErr==0 );
118599 assert( pName->nSrc==1 );
118601 if( noErr ) db->suppressErr++;
118603 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
118604 if( noErr ) db->suppressErr--;
118608 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
118613 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118614 assert( iDb>=0 && iDb<db->nDb );
118626 const char *zDb = db->aDb[iDb].zDbSName;
118640 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
118649 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
118652 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
118658 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
118667 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
118671 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
118683 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
118704 ** under construction in the pParse->pNewTable field.
118716 sqlite3 *db = pParse->db;
118720 Table *p = pParse->pNewTable;
118729 int iCol = p->nCol-1;
118731 if( pToCol && pToCol->nExpr!=1 ){
118734 p->aCol[iCol].zCnName, pTo);
118738 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
118744 nCol = pFromCol->nExpr;
118746 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
118748 for(i=0; i<pToCol->nExpr; i++){
118749 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
118756 pFKey->pFrom = p;
118758 pFKey->pNextFrom = p->u.tab.pFKey;
118759 z = (char*)&pFKey->aCol[nCol];
118760 pFKey->zTo = z;
118764 memcpy(z, pTo->z, pTo->n);
118765 z[pTo->n] = 0;
118767 z += pTo->n+1;
118768 pFKey->nCol = nCol;
118770 pFKey->aCol[0].iFrom = p->nCol-1;
118774 for(j=0; j<p->nCol; j++){
118775 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
118776 pFKey->aCol[i].iFrom = j;
118780 if( j>=p->nCol ){
118783 pFromCol->a[i].zEName);
118787 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
118793 int n = sqlite3Strlen30(pToCol->a[i].zEName);
118794 pFKey->aCol[i].zCol = z;
118796 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
118798 memcpy(z, pToCol->a[i].zEName, n);
118803 pFKey->isDeferred = 0;
118804 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
118805 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
118807 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
118808 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
118809 pFKey->zTo, (void *)pFKey
118816 assert( pNextTo->pPrevTo==0 );
118817 pFKey->pNextTo = pNextTo;
118818 pNextTo->pPrevTo = pFKey;
118824 p->u.tab.pFKey = pFKey;
118845 if( (pTab = pParse->pNewTable)==0 ) return;
118847 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
118848 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
118849 pFKey->isDeferred = (u8)isDeferred;
118862 ** the root page number of the index is taken from pIndex->tnum.
118865 Table *pTab = pIndex->pTable; /* The table that is indexed */
118866 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
118867 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
118876 sqlite3 *db = pParse->db; /* The database connection */
118877 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
118880 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
118881 db->aDb[iDb].zDbSName ) ){
118886 /* Require a write-lock on the table to perform this operation */
118887 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
118894 tnum = pIndex->tnum;
118897 assert( pKey!=0 || pParse->nErr );
118900 iSorter = pParse->nTab++;
118901 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
118927 pIndex->nKeyCol); VdbeCoverage(v);
118942 if( !pIndex->bAscKeyBug ){
118967 ** of 8-byte aligned space after the Index object and return a
118987 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
118988 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
118989 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
118990 p->aSortOrder = (u8*)pExtra;
118991 p->nColumn = nCol;
118992 p->nKeyCol = nCol - 1;
119001 ** pParse and return non-zero. Otherwise, return zero.
119006 for(i=0; i<pList->nExpr; i++){
119007 if( pList->a[i].fg.bNulls ){
119008 u8 sf = pList->a[i].fg.sortFlags;
119023 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
119024 ** as the table to be indexed. pParse->pNewTable is a table that is
119028 ** is a primary key or unique-constraint on the most recent column added
119035 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
119051 sqlite3 *db = pParse->db;
119061 assert( db->pParse==pParse );
119062 if( pParse->nErr ){
119065 assert( db->mallocFailed==0 );
119081 /* Use the two-part index name to determine the database
119088 assert( pName && pName->z );
119095 if( !db->init.busy ){
119097 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
119109 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
119110 assert( db->mallocFailed==0 || pTab==0 );
119112 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
119114 "cannot create a TEMP index on non-TEMP table \"%s\"",
119115 pTab->zName);
119122 pTab = pParse->pNewTable;
119124 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119126 pDb = &db->aDb[iDb];
119129 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
119130 && db->init.busy==0
119133 && sqlite3UserAuthTable(pTab->zName)==0
119136 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
119168 assert( pName->z!=0 );
119169 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
119173 if( !db->init.busy ){
119179 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
119183 assert( !db->init.busy );
119193 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
119194 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
119211 const char *zDb = pDb->zDbSName;
119217 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
119229 Column *pCol = &pTab->aCol[pTab->nCol-1];
119230 pCol->colFlags |= COLFLAG_UNIQUE;
119231 sqlite3TokenInit(&prevCol, pCol->zCnName);
119235 assert( pList->nExpr==1 );
119239 if( pParse->nErr ) goto exit_create_index;
119245 for(i=0; i<pList->nExpr; i++){
119246 Expr *pExpr = pList->a[i].pExpr;
119248 if( pExpr->op==TK_COLLATE ){
119250 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
119258 nExtraCol = pPk ? pPk->nKeyCol : 1;
119259 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
119260 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
119262 if( db->mallocFailed ){
119265 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
119266 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
119267 pIndex->zName = zExtra;
119269 memcpy(pIndex->zName, zName, nName+1);
119270 pIndex->pTable = pTab;
119271 pIndex->onError = (u8)onError;
119272 pIndex->uniqNotNull = onError!=OE_None;
119273 pIndex->idxType = idxType;
119274 pIndex->pSchema = db->aDb[iDb].pSchema;
119275 pIndex->nKeyCol = pList->nExpr;
119278 pIndex->pPartIdxWhere = pPIWhere;
119285 if( pDb->pSchema->file_format>=4 ){
119286 sortOrderMask = -1; /* Honor DESC */
119294 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
119300 pListItem = pList->a;
119302 pIndex->aColExpr = pList;
119305 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
119306 Expr *pCExpr; /* The i-th index expression */
119307 int requestedSortOrder; /* ASC or DESC on the i-th expression */
119310 sqlite3StringToId(pListItem->pExpr);
119311 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
119312 if( pParse->nErr ) goto exit_create_index;
119313 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
119314 if( pCExpr->op!=TK_COLUMN ){
119315 if( pTab==pParse->pNewTable ){
119320 if( pIndex->aColExpr==0 ){
119321 pIndex->aColExpr = pList;
119325 pIndex->aiColumn[i] = XN_EXPR;
119326 pIndex->uniqNotNull = 0;
119328 j = pCExpr->iColumn;
119331 j = pTab->iPKey;
119333 if( pTab->aCol[j].notNull==0 ){
119334 pIndex->uniqNotNull = 0;
119336 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
119337 pIndex->bHasVCol = 1;
119340 pIndex->aiColumn[i] = (i16)j;
119343 if( pListItem->pExpr->op==TK_COLLATE ){
119345 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
119346 zColl = pListItem->pExpr->u.zToken;
119352 nExtra -= nColl;
119354 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
119357 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
119360 pIndex->azColl[i] = zColl;
119361 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
119362 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
119370 for(j=0; j<pPk->nKeyCol; j++){
119371 int x = pPk->aiColumn[j];
119373 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
119374 pIndex->nColumn--;
119376 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
119377 pIndex->aiColumn[i] = x;
119378 pIndex->azColl[i] = pPk->azColl[j];
119379 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
119383 assert( i==pIndex->nColumn );
119385 pIndex->aiColumn[i] = XN_ROWID;
119386 pIndex->azColl[i] = sqlite3StrBINARY;
119389 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
119394 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
119396 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
119397 pIndex->isCovering = 1;
119398 for(j=0; j<pTab->nCol; j++){
119399 if( j==pTab->iPKey ) continue;
119401 pIndex->isCovering = 0;
119406 if( pTab==pParse->pNewTable ){
119429 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
119432 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
119435 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
119436 for(k=0; k<pIdx->nKeyCol; k++){
119439 assert( pIdx->aiColumn[k]>=0 );
119440 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
119441 z1 = pIdx->azColl[k];
119442 z2 = pIndex->azColl[k];
119445 if( k==pIdx->nKeyCol ){
119446 if( pIdx->onError!=pIndex->onError ){
119454 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
119458 if( pIdx->onError==OE_Default ){
119459 pIdx->onError = pIndex->onError;
119462 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
119464 pIndex->pNext = pParse->pNewIndex;
119465 pParse->pNewIndex = pIndex;
119476 ** in-memory database structures.
119478 assert( pParse->nErr==0 );
119479 if( db->init.busy ){
119482 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
119484 pIndex->tnum = db->init.newTnum;
119487 pParse->rc = SQLITE_CORRUPT_BKPT;
119491 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
119492 pIndex->zName, pIndex);
119498 db->mDbFlags |= DBFLAG_SchemaChange;
119517 int iMem = ++pParse->nMem;
119530 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
119538 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
119539 if( pName->z[n-1]==';' ) n--;
119542 onError==OE_None ? "" : " UNIQUE", n, pName->z);
119553 db->aDb[iDb].zDbSName,
119554 pIndex->zName,
119555 pTab->zName,
119562 ** to invalidate all pre-compiled statements.
119568 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
119572 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
119575 if( db->init.busy || pTblName==0 ){
119576 pIndex->pNext = pTab->pIndex;
119577 pTab->pIndex = pIndex;
119581 assert( pParse->pNewIndex==0 );
119582 pParse->pNewIndex = pIndex;
119596 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
119598 if( pThis->onError!=OE_Replace ) continue;
119599 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
119601 pThis->pNext = pNext->pNext;
119602 pNext->pNext = pThis;
119603 ppFrom = &pNext->pNext;
119611 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
119612 assert( pThis->onError!=OE_Replace
119613 || pThis->pNext==0
119614 || pThis->pNext->onError==OE_Replace );
119625 ** Fill the Index.aiRowEst[] array with default information - information
119635 ** aiRowEst[N]<=aiRowEst[N-1]
119645 LogEst *a = pIdx->aiRowLogEst;
119647 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
119651 assert( !pIdx->hasStat1 );
119657 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
119663 x = pIdx->pTable->nRowLogEst;
119666 pIdx->pTable->nRowLogEst = x = 99;
119668 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
119674 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
119679 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
119689 sqlite3 *db = pParse->db;
119692 if( db->mallocFailed ){
119695 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
119696 assert( pName->nSrc==1 );
119700 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
119703 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
119705 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
119708 pParse->checkSchema = 1;
119711 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
119716 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
119720 Table *pTab = pIndex->pTable;
119721 const char *zDb = db->aDb[iDb].zDbSName;
119727 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
119739 db->aDb[iDb].zDbSName, pIndex->zName
119741 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
119743 destroyRootPage(pParse, pIndex->tnum, iDb);
119744 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
119757 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
119765 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
119777 if( (n & (n-1))==0 ){
119781 *pIdx = -1;
119799 sqlite3 *db = pParse->db;
119807 sizeof(IdList) + pList->nId*sizeof(pList->a));
119814 i = pList->nId++;
119815 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
119816 if( IN_RENAME_OBJECT && pList->a[i].zName ){
119817 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
119828 assert( pList->eU4!=EU4_EXPR ); /* EU4_EXPR mode is not currently used */
119829 for(i=0; i<pList->nId; i++){
119830 sqlite3DbFree(db, pList->a[i].zName);
119836 ** Return the index in pList of the identifier named zId. Return -1
119842 for(i=0; i<pList->nId; i++){
119843 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
119845 return -1;
119854 ** appropriate for small and memory-limited applications.
119877 ** the original SrcList unchanged, return NULL, and leave an error message
119883 int nExtra, /* Number of new slots to add to pSrc->a[] */
119884 int iStart /* Index in pSrc->a[] of first new slot */
119892 assert( iStart<=pSrc->nSrc );
119895 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
119897 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
119898 sqlite3 *db = pParse->db;
119900 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
119907 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
119909 assert( db->mallocFailed );
119913 pSrc->nAlloc = nAlloc;
119918 for(i=pSrc->nSrc-1; i>=iStart; i--){
119919 pSrc->a[i+nExtra] = pSrc->a[i];
119921 pSrc->nSrc += nExtra;
119924 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
119926 pSrc->a[i].iCursor = -1;
119979 assert( pParse->db!=0 );
119980 db = pParse->db;
119982 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
119984 pList->nAlloc = 1;
119985 pList->nSrc = 1;
119986 memset(&pList->a[0], 0, sizeof(pList->a[0]));
119987 pList->a[0].iCursor = -1;
119989 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
119997 pItem = &pList->a[pList->nSrc-1];
119998 if( pDatabase && pDatabase->z==0 ){
120002 pItem->zName = sqlite3NameFromToken(db, pDatabase);
120003 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
120005 pItem->zName = sqlite3NameFromToken(db, pTable);
120006 pItem->zDatabase = 0;
120017 assert( pList || pParse->db->mallocFailed );
120019 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
120020 if( pItem->iCursor>=0 ) continue;
120021 pItem->iCursor = pParse->nTab++;
120022 if( pItem->pSelect ){
120023 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
120036 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
120037 if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
120038 sqlite3DbFree(db, pItem->zName);
120039 if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
120040 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
120041 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
120042 sqlite3DeleteTable(db, pItem->pTab);
120043 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
120044 if( pItem->fg.isUsing ){
120045 sqlite3IdListDelete(db, pItem->u3.pUsing);
120046 }else if( pItem->u3.pOn ){
120047 sqlite3ExprDelete(db, pItem->u3.pOn);
120059 ** pDatabase is NULL if the database name qualifier is missing - the
120074 Token *pAlias, /* The right-hand side of the AS subexpression */
120079 sqlite3 *db = pParse->db;
120080 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
120082 (pOnUsing->pOn ? "ON" : "USING")
120090 assert( p->nSrc>0 );
120091 pItem = &p->a[p->nSrc-1];
120093 assert( pItem->zName==0 || pDatabase!=0 );
120094 if( IN_RENAME_OBJECT && pItem->zName ){
120095 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
120096 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
120099 if( pAlias->n ){
120100 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
120103 pItem->pSelect = pSubquery;
120104 if( pSubquery->selFlags & SF_NestedFrom ){
120105 pItem->fg.isNestedFrom = 1;
120108 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
120109 assert( pItem->fg.isUsing==0 );
120111 pItem->u3.pOn = 0;
120112 }else if( pOnUsing->pUsing ){
120113 pItem->fg.isUsing = 1;
120114 pItem->u3.pUsing = pOnUsing->pUsing;
120116 pItem->u3.pOn = pOnUsing->pOn;
120129 ** element of the source-list passed as the second argument.
120133 if( p && pIndexedBy->n>0 ){
120135 assert( p->nSrc>0 );
120136 pItem = &p->a[p->nSrc-1];
120137 assert( pItem->fg.notIndexed==0 );
120138 assert( pItem->fg.isIndexedBy==0 );
120139 assert( pItem->fg.isTabFunc==0 );
120140 if( pIndexedBy->n==1 && !pIndexedBy->z ){
120143 pItem->fg.notIndexed = 1;
120145 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
120146 pItem->fg.isIndexedBy = 1;
120147 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
120158 assert( p1 && p1->nSrc==1 );
120160 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
120162 sqlite3SrcListDelete(pParse->db, p2);
120165 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
120166 sqlite3DbFree(pParse->db, p2);
120167 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
120175 ** table-valued-function.
120179 SrcItem *pItem = &p->a[p->nSrc-1];
120180 assert( pItem->fg.notIndexed==0 );
120181 assert( pItem->fg.isIndexedBy==0 );
120182 assert( pItem->fg.isTabFunc==0 );
120183 pItem->u1.pFuncArg = pList;
120184 pItem->fg.isTabFunc = 1;
120186 sqlite3ExprListDelete(pParse->db, pList);
120202 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
120207 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
120214 if( p && p->nSrc>1 ){
120215 int i = p->nSrc-1;
120218 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
120219 }while( (--i)>0 );
120220 p->a[0].fg.jointype = 0;
120225 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
120226 i--;
120229 p->a[i].fg.jointype |= JT_LTORJ;
120230 }while( (--i)>=0 );
120244 db = pParse->db;
120252 for(i=0; i<db->nDb; i++){
120254 Btree *pBt = db->aDb[i].pBt;
120279 assert( pParse->db!=0 );
120297 char *zName = sqlite3NameFromToken(pParse->db, pName);
120305 sqlite3DbFree(pParse->db, zName);
120317 sqlite3 *db = pParse->db;
120318 if( db->aDb[1].pBt==0 && !pParse->explain ){
120328 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
120332 pParse->rc = rc;
120335 db->aDb[1].pBt = pBt;
120336 assert( db->aDb[1].pSchema );
120337 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
120348 ** will occur at the end of the top-level VDBE and will be generated
120352 assert( iDb>=0 && iDb<pToplevel->db->nDb );
120353 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
120355 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
120356 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
120357 DbMaskSet(pToplevel->cookieMask, iDb);
120373 sqlite3 *db = pParse->db;
120375 for(i=0; i<db->nDb; i++){
120376 Db *pDb = &db->aDb[i];
120377 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
120399 DbMaskSet(pToplevel->writeMask, iDb);
120400 pToplevel->isMultiWrite |= setStatement;
120412 pToplevel->isMultiWrite = 1;
120433 pToplevel->mayAbort = 1;
120445 char *p4, /* Error message */
120450 assert( pParse->pVdbe!=0 );
120452 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
120471 Table *pTab = pIdx->pTable;
120473 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
120474 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
120475 if( pIdx->aColExpr ){
120476 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
120478 for(j=0; j<pIdx->nKeyCol; j++){
120480 assert( pIdx->aiColumn[j]>=0 );
120481 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
120483 sqlite3_str_appendall(&errMsg, pTab->zName);
120497 ** Code an OP_Halt due to non-unique rowid.
120502 Table *pTab /* The table with the non-unique rowid */
120506 if( pTab->iPKey>=0 ){
120507 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
120508 pTab->aCol[pTab->iPKey].zCnName);
120511 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
120526 for(i=0; i<pIndex->nColumn; i++){
120527 const char *z = pIndex->azColl[i];
120528 assert( z!=0 || pIndex->aiColumn[i]<0 );
120529 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
120546 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
120548 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
120550 sqlite3RefillIndex(pParse, pIndex, -1);
120566 sqlite3 *db = pParse->db; /* The database connection */
120571 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
120573 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
120584 ** REINDEX -- 1
120585 ** REINDEX <collation> -- 2
120586 ** REINDEX ?<database>.?<tablename> -- 3
120587 ** REINDEX ?<database>.?<indexname> -- 4
120602 sqlite3 *db = pParse->db; /* The database connection */
120605 /* Read the database schema. If an error occurs, leave an error message
120614 }else if( NEVER(pName2==0) || pName2->z==0 ){
120616 assert( pName1->z );
120617 zColl = sqlite3NameFromToken(pParse->db, pName1);
120631 zDb = db->aDb[iDb].zDbSName;
120642 sqlite3RefillIndex(pParse, pIndex, -1);
120657 int nCol = pIdx->nColumn;
120658 int nKey = pIdx->nKeyCol;
120660 if( pParse->nErr ) return 0;
120661 if( pIdx->uniqNotNull ){
120662 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
120664 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
120669 const char *zColl = pIdx->azColl[i];
120670 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
120672 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
120673 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
120675 if( pParse->nErr ){
120676 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
120677 if( pIdx->bNoQuery==0 ){
120682 ** the missing index using the collation-needed callback. For
120685 pIdx->bNoQuery = 1;
120686 pParse->rc = SQLITE_ERROR_RETRY;
120701 Token *pName, /* Name of the common-table */
120707 sqlite3 *db = pParse->db;
120710 assert( pNew!=0 || db->mallocFailed );
120712 if( db->mallocFailed ){
120716 pNew->pSelect = pQuery;
120717 pNew->pCols = pArglist;
120718 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
120719 pNew->eM10d = eM10d;
120730 sqlite3ExprListDelete(db, pCte->pCols);
120731 sqlite3SelectDelete(db, pCte->pSelect);
120732 sqlite3DbFree(db, pCte->zName);
120755 sqlite3 *db = pParse->db;
120765 zName = pCte->zName;
120768 for(i=0; i<pWith->nCte; i++){
120769 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
120776 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
120781 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
120783 if( db->mallocFailed ){
120787 pNew->a[pNew->nCte++] = *pCte;
120800 for(i=0; i<pWith->nCte; i++){
120801 cteClear(db, &pWith->a[i]);
120833 assert( !db->xCollNeeded || !db->xCollNeeded16 );
120834 if( db->xCollNeeded ){
120837 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
120841 if( db->xCollNeeded16 ){
120844 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
120847 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
120858 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
120863 char *z = pColl->zName;
120868 if( pColl2->xCmp!=0 ){
120870 pColl->xDel = 0; /* Do not copy the destructor */
120889 if( pColl && pColl->xCmp==0 ){
120890 const char *zName = pColl->zName;
120891 sqlite3 *db = pParse->db;
120910 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
120922 pColl = sqlite3HashFind(&db->aCollSeq, zName);
120936 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
120954 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
120963 ** if necessary and generates an error message if the collating sequence
120979 if( pColl ) pColl += enc-1;
120981 pColl = db->pDfltColl;
120992 db->enc = enc;
120993 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
120996 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
121009 ** sequence can be found. If no collation is found, leave an error message.
121020 sqlite3 *db = pParse->db;
121026 if( !p || !p->xCmp ){
121033 if( p && !p->xCmp && synthCollSeq(db, p) ){
121036 assert( !p || p->xCmp );
121039 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
121055 ** another error occurs, NULL is returned and an error message written into
121060 ** and generates an error message.
121065 sqlite3 *db = pParse->db;
121067 u8 initbusy = db->init.busy;
121071 if( !initbusy && (!pColl || !pColl->xCmp) ){
121084 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
121085 ** is also -1. In other words, we are searching for a function that
121088 ** If nArg is -2 that means that we are searching for any function
121098 ** 4: UTF8/16 conversion required - argument count matches exactly
121099 ** 5: UTF16 byte order conversion required - argument count matches exactly
121102 ** If nArg==(-2) then any function with a non-null xSFunc is
121104 ** a non-match.
121109 int nArg, /* Desired number of arguments. (-1)==any */
121113 assert( p->nArg>=-1 );
121116 if( p->nArg!=nArg ){
121117 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
121118 if( p->nArg>=0 ) return 0;
121123 if( p->nArg==nArg ){
121130 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
121132 }else if( (enc & p->funcFlags & 2)!=0 ){
121148 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
121149 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
121150 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
121173 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
121174 aDef[i].pNext = pOther->pNext;
121175 pOther->pNext = &aDef[i];
121188 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
121196 ** If nArg is -2, then the first valid function found is returned. A
121197 ** function is valid if xSFunc is non-zero. The nArg==(-2)
121199 ** of arguments. If nArg is -2, then createFlag must be 0.
121207 const char *zName, /* Name of the function. zero-terminated */
121208 int nArg, /* Number of arguments. -1 means any number */
121218 assert( nArg>=(-2) );
121219 assert( nArg>=(-1) || createFlag==0 );
121222 /* First search for a match amongst the application-defined functions.
121224 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
121231 p = p->pNext;
121234 /* If no match is found, search the built-in functions.
121236 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
121237 ** functions even if a prior app-defined function was found. And give
121238 ** priority to built-in functions.
121243 ** new function. But the FuncDefs for built-in functions are read-only.
121244 ** So we must not search for built-ins when creating a new function.
121246 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
121256 p = p->pNext;
121268 pBest->zName = (const char*)&pBest[1];
121269 pBest->nArg = (u16)nArg;
121270 pBest->funcFlags = enc;
121272 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
121273 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
121279 pBest->pNext = pOther;
121283 if( pBest && (pBest->xSFunc || createFlag) ){
121303 temp1 = pSchema->tblHash;
121304 temp2 = pSchema->trigHash;
121305 sqlite3HashInit(&pSchema->trigHash);
121306 sqlite3HashClear(&pSchema->idxHash);
121311 sqlite3HashInit(&pSchema->tblHash);
121317 sqlite3HashClear(&pSchema->fkeyHash);
121318 pSchema->pSeqTab = 0;
121319 if( pSchema->schemaFlags & DB_SchemaLoaded ){
121320 pSchema->iGeneration++;
121322 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
121338 }else if ( 0==p->file_format ){
121339 sqlite3HashInit(&p->tblHash);
121340 sqlite3HashInit(&p->idxHash);
121341 sqlite3HashInit(&p->trigHash);
121342 sqlite3HashInit(&p->fkeyHash);
121343 p->enc = SQLITE_UTF8;
121371 ** return a pointer. Set an error message and return NULL if the table
121376 ** pSrc->a[0].pTab Pointer to the Table object
121377 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
121381 SrcItem *pItem = pSrc->a;
121383 assert( pItem && pSrc->nSrc>=1 );
121385 sqlite3DeleteTable(pParse->db, pItem->pTab);
121386 pItem->pTab = pTab;
121388 pTab->nTabRef++;
121389 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
121396 /* Generate byte-code that will report the number of rows modified
121406 /* Return true if table pTab is read-only.
121408 ** A table is read-only if any of the following are true:
121419 ** is for a top-level SQL statement.
121424 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
121426 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
121427 db = pParse->db;
121428 if( (pTab->tabFlags & TF_Readonly)!=0 ){
121429 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
121431 assert( pTab->tabFlags & TF_Shadow );
121437 ** writable, generate an error message and return 1. If it is
121442 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
121447 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
121472 sqlite3 *db = pParse->db;
121473 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
121477 assert( pFrom->nSrc==1 );
121478 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
121479 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
121480 assert( pFrom->a[0].fg.isUsing==0 );
121481 assert( pFrom->a[0].u3.pOn==0 );
121502 SrcList *pSrc, /* the FROM clause -- which tables to scan */
121508 sqlite3 *db = pParse->db;
121520 sqlite3ExprDelete(pParse->db, pWhere);
121521 sqlite3ExprListDelete(pParse->db, pOrderBy);
121541 pTab = pSrc->a[0].pTab;
121549 if( pPk->nKeyCol==1 ){
121550 const char *zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
121555 for(i=0; i<pPk->nKeyCol; i++){
121556 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
121561 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
121568 pSrc->a[0].pTab = 0;
121570 pSrc->a[0].pTab = pTab;
121571 if( pSrc->a[0].fg.isIndexedBy ){
121572 assert( pSrc->a[0].fg.isCte==0 );
121573 pSrc->a[0].u2.pIBIndex = 0;
121574 pSrc->a[0].fg.isIndexedBy = 0;
121575 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
121576 }else if( pSrc->a[0].fg.isCte ){
121577 pSrc->a[0].u2.pCteUse->nUse++;
121644 db = pParse->db;
121645 assert( db->pParse==pParse );
121646 if( pParse->nErr ){
121649 assert( db->mallocFailed==0 );
121650 assert( pTabList->nSrc==1 );
121679 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
121703 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121704 assert( iDb<db->nDb );
121705 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
121706 db->aDb[iDb].zDbSName);
121715 assert( pTabList->nSrc==1 );
121716 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
121717 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
121718 pParse->nTab++;
121724 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
121733 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
121762 if( (db->flags & SQLITE_CountRows)!=0
121763 && !pParse->nested
121764 && !pParse->pTriggerTab
121765 && !pParse->bReturning
121767 memCnt = ++pParse->nMem;
121778 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
121788 && db->xPreUpdateCallback==0
121792 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
121794 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
121795 pTab->zName, P4_STATIC);
121797 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121798 assert( pIdx->pSchema==pTab->pSchema );
121799 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
121801 sqlite3VdbeChangeP3(v, -1, memCnt ? memCnt : -1);
121814 iRowSet = ++pParse->nMem;
121821 nPk = pPk->nKeyCol;
121822 iPk = pParse->nMem+1;
121823 pParse->nMem += nPk;
121824 iEphCur = pParse->nTab++;
121833 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
121834 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
121835 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
121855 assert( pPk->aiColumn[i]>=0 );
121857 pPk->aiColumn[i], iPk+i);
121861 iKey = ++pParse->nMem;
121862 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
121866 /* For ONEPASS, no need to store the rowid/primary-key. There is only
121877 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
121878 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
121884 iKey = ++pParse->nMem;
121887 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
121917 /* Set up a loop over the rowids/primary-keys that were found in the
121918 ** where-clause loop above.
121922 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
121951 pParse->isMultiWrite = 0;
121959 int count = (pParse->nested==0); /* True to count changes */
121964 /* End of the loop over all rowids/primary-keys. */
121975 } /* End non-truncate path */
121981 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
122027 ** cursor number iIdxCur+i for the i-th index.
122063 u8 count, /* If non-zero, increment the row change counter */
122068 Vdbe *v = pParse->pVdbe; /* Vdbe */
122102 iOld = pParse->nMem+1;
122103 pParse->nMem += (1 + pTab->nCol);
122105 /* Populate the OLD.* pseudo-table register array. These values will be
122108 for(iCol=0; iCol<pTab->nCol; iCol++){
122136 iIdxNoSeek = -1;
122149 ** If variable 'count' is non-zero, then this OP_Delete instruction should
122150 ** invoke the update-hook. The pre-update-hook, on the other hand should
122152 ** the update-hook is not invoked for rows removed by REPLACE, but the
122153 ** pre-update-hook is.
122159 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
122201 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
122202 ** index is the 0-th index.)
122216 int r1 = -1; /* Register holding an index key */
122223 v = pParse->pVdbe;
122225 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
122230 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
122234 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
122282 Vdbe *v = pParse->pVdbe;
122288 if( pIdx->pPartIdxWhere ){
122290 pParse->iSelfTab = iDataCur + 1;
122291 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
122293 pParse->iSelfTab = 0;
122294 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
122300 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
122302 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
122305 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
122306 && pPrior->aiColumn[j]!=XN_EXPR
122312 if( pIdx->aiColumn[j]>=0 ){
122330 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
122336 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
122353 ** This file contains the C-language implementations for many of the SQL
122370 assert( context->pVdbe!=0 );
122371 pOp = &context->pVdbe->aOp[context->iOp-1];
122372 assert( pOp->opcode==OP_CollSeq );
122373 assert( pOp->p4type==P4_COLLSEQ );
122374 return pOp->p4.pColl;
122382 assert( context->isError<=0 );
122383 context->isError = -1;
122384 context->skipFlag = 1;
122388 ** Implementation of the non-aggregate min() and max() functions
122401 mask = sqlite3_user_data(context)==0 ? 0 : -1;
122404 assert( mask==-1 || mask==0 );
122426 int i = sqlite3_value_type(argv[0]) - 1;
122434 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
122438 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
122483 sqlite3_result_int(context, (int)(z-z0));
122496 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
122507 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
122509 ** equivalent positive 64-bit two complement value. */
122510 sqlite3_result_error(context, "integer overflow", -1);
122513 iVal = -iVal;
122519 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
122526 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
122530 if( rVal<0 ) rVal = -rVal;
122597 nHaystack--;
122628 x.nArg = argc-1;
122631 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
122644 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
122645 ** of x. If x is text, then we actually count UTF-8 characters.
122689 ** as substr(X,1,N) - it returns the first N characters of X. This
122690 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
122691 ** from 2009-02-02 for compatibility of applications that exploited the
122698 p2 = -p2;
122702 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
122712 p1--;
122714 p2--;
122717 p1 -= p2;
122727 p1--;
122729 for(z2=z; *z2 && p2; p2--){
122732 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
122736 p2 = len-p1;
122760 /* If Y==0 and X will fit in a 64-bit int,
122764 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
122767 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
122792 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
122793 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
122794 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
122856 #define noopFunc versionFunc /* Substitute function - never called */
122871 ** (or -9223372036854775808) since when you do abs() of that
122876 ** therefore be no less than -9223372036854775807.
122878 r = -(r & LARGEST_INT64);
122918 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
122927 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
122952 /* IMP: R-11217-42568 This function is a wrapper around the
122958 ** A structure defining how to do GLOB-style comparisons.
122981 /* The correct SQL-92 behavior is for the LIKE operator to ignore
122996 ** Compare two UTF-8 strings for equality where the first string is
123016 ** range of characters can be specified using '-'. Example:
123017 ** "[a-z]" matches any single lower-case letter. To match a '-', make
123040 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
123041 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
123042 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
123059 if( pInfo->matchSet==0 ){
123065 assert( matchOther<0x80 ); /* '[' is a single-byte character */
123067 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
123080 ** For a case-insensitive search, set variable cx to be the same as
123113 if( pInfo->matchSet==0 ){
123133 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
123164 ** non-zero if there is no match.
123171 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
123172 ** a miss - like strcmp().
123190 ** the build-in LIKE operator. The first argument to the function is the
123228 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
123229 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
123230 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
123231 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
123235 /* The escape character string must consist of a single UTF-8 character.
123240 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
123242 "ESCAPE expression must be a single character", -1);
123246 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
123249 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
123250 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
123253 escape = pInfo->matchSet;
123293 /* IMP: R-48699-48617 This function is an SQL wrapper around the
123294 ** sqlite3_libversion() C-interface. */
123295 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
123309 /* IMP: R-24470-31136 This function is an SQL wrapper around the
123311 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
123317 ** its side-effects.
123343 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
123367 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
123371 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
123375 /* Array for converting from half-bytes (nybbles) into ASCII hex
123390 assert( pStr!=0 && pStr->nChar==0 );
123400 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
123417 if( pStr->accError==0 ){
123418 char *zText = pStr->zText;
123428 pStr->nChar = nBlob*2 + 3;
123450 ** Strings are surrounded by single-quotes with escapes on interior quotes
123460 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
123471 ** The unicode() function. Return the integer unicode code-point value
123523 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
123556 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
123569 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
123608 || sqlite3_context_db_handle(context)->mallocFailed );
123628 loopLimit = nStr - nPattern;
123635 nOut += nRep - nPattern;
123636 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
123637 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
123638 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
123644 if( (cntExpand&(cntExpand-1))==0 ){
123649 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
123659 i += nPattern-1;
123662 assert( j+nStr-i+1<=nOut );
123663 memcpy(&zOut[j], &zStr[i], nStr-i);
123664 j += nStr - i;
123719 aLen[nChar] = (unsigned)(z - azChar[nChar]);
123734 nIn -= len;
123742 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
123745 nIn -= len;
123760 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
123761 ** When the "sqlite3" command-line shell is built using this functionality,
123763 ** involving application-defined functions to be examined in a generic
123771 /* no-op */
123776 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
123777 ** is only available if the SQLITE_SOUNDEX compile-time option is used
123784 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
123829 /* IMP: R-64894-50321 The string "?000" is returned if the argument
123838 ** A function that loads a shared-library extension then returns NULL.
123849 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
123850 sqlite3_result_error(context, "not authorized", -1);
123860 sqlite3_result_error(context, zErrMsg, -1);
123877 u8 approx; /* True if non-integer value was input to the sum */
123898 p->cnt++;
123901 p->rSum += v;
123902 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
123903 p->approx = p->overflow = 1;
123906 p->rSum += sqlite3_value_double(argv[0]);
123907 p->approx = 1;
123919 /* p is always non-NULL because sumStep() will have been called first
123922 assert( p->cnt>0 );
123923 p->cnt--;
123924 assert( type==SQLITE_INTEGER || p->approx );
123925 if( type==SQLITE_INTEGER && p->approx==0 ){
123927 p->rSum -= v;
123928 p->iSum -= v;
123930 p->rSum -= sqlite3_value_double(argv[0]);
123940 if( p && p->cnt>0 ){
123941 if( p->overflow ){
123942 sqlite3_result_error(context,"integer overflow",-1);
123943 }else if( p->approx ){
123944 sqlite3_result_double(context, p->rSum);
123946 sqlite3_result_int64(context, p->iSum);
123953 if( p && p->cnt>0 ){
123954 sqlite3_result_double(context, p->rSum/(double)p->cnt);
123961 sqlite3_result_double(context, p ? p->rSum : (double)0);
123983 p->n++;
123990 ** expressed as a 32-bit integer. */
123991 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
123992 || p->n==sqlite3_aggregate_count(context) );
123998 sqlite3_result_int64(context, p ? p->n : 0);
124004 /* p is always non-NULL since countStep() will have been called first */
124006 p->n--;
124008 p->bInverse = 1;
124032 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
124033 }else if( pBest->flags ){
124040 ** sqlite3_user_data() function returns (void *)-1. For min() it
124053 pBest->db = sqlite3_context_db_handle(context);
124061 if( pRes->flags ){
124092 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
124094 ** (Hence, its slots in use number nAccum-1 between method calls.)
124115 int firstTerm = pGCC->str.mxAlloc==0;
124116 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
124119 sqlite3_str_appendchar(&pGCC->str, 1, ',');
124123 pGCC->nFirstSepLength = 1;
124130 sqlite3_str_append(&pGCC->str, zSep, nSep);
124136 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
124137 int *pnsl = pGCC->pnSepLengths;
124140 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
124142 int i = 0, nA = pGCC->nAccum-1;
124143 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
124146 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
124149 if( ALWAYS(pGCC->nAccum>0) ){
124150 pnsl[pGCC->nAccum-1] = nSep;
124152 pGCC->pnSepLengths = pnsl;
124154 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
124161 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
124163 pGCC->nAccum += 1;
124167 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
124182 /* pGCC is always non-NULL since groupConcatStep() will have always
124190 pGCC->nAccum -= 1;
124191 if( pGCC->pnSepLengths!=0 ){
124192 assert(pGCC->nAccum >= 0);
124193 if( pGCC->nAccum>0 ){
124194 nVS += *pGCC->pnSepLengths;
124195 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
124196 (pGCC->nAccum-1)*sizeof(int));
124199 /* If removing single accumulated string, harmlessly over-do. */
124200 nVS += pGCC->nFirstSepLength;
124202 if( nVS>=(int)pGCC->str.nChar ){
124203 pGCC->str.nChar = 0;
124205 pGCC->str.nChar -= nVS;
124206 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
124208 if( pGCC->str.nChar==0 ){
124209 pGCC->str.mxAlloc = 0;
124210 sqlite3_free(pGCC->pnSepLengths);
124211 pGCC->pnSepLengths = 0;
124222 sqlite3ResultStrAccum(context, &pGCC->str);
124224 sqlite3_free(pGCC->pnSepLengths);
124233 StrAccum *pAccum = &pGCC->str;
124234 if( pAccum->accError==SQLITE_TOOBIG ){
124236 }else if( pAccum->accError==SQLITE_NOMEM ){
124240 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
124249 ** This routine does per-connection function registration. Most
124250 ** of the built-in functions above are part of the global function set.
124262 ** Re-register the built-in LIKE functions. The caseSensitive
124278 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
124279 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
124287 ** LIKE-style function then return FALSE.
124303 assert( pExpr->op==TK_FUNCTION );
124305 if( !pExpr->x.pList ){
124308 nExpr = pExpr->x.pList->nExpr;
124310 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
124314 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
124322 memcpy(aWc, pDef->pUserData, 3);
124330 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
124332 if( pEscape->op!=TK_STRING ) return 0;
124334 zEscape = pEscape->u.zToken;
124341 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
124357 /* Extra math functions that require linking with -lm
124403 ** ln(X) - natural logarithm
124404 ** log(X) - log X base 10
124405 ** log10(X) - log X base 10
124406 ** log(B,X) - log X base B
124462 ** Implementation of 1-argument SQL math functions:
124464 ** exp(X) - Compute e to the X-th power
124484 ** Implementation of 2-argument SQL math functions:
124486 ** power(X,Y) - Compute X to the Y-th power
124509 ** Implementation of 0-argument pi() function.
124537 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
124542 ** to the global function hash table. This occurs at start-time (as
124553 ** FuncDef.pHash elements at start-time. The elements of this array
124554 ** are read-only after initialization is complete.
124593 FUNCTION(min, -1, 0, 1, minmaxFunc ),
124597 FUNCTION(max, -1, 1, 1, minmaxFunc ),
124605 FUNCTION(printf, -1, 0, 0, printfFunc ),
124606 FUNCTION(format, -1, 0, 0, printfFunc ),
124608 FUNCTION(char, -1, 0, 0, charFunc ),
124656 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
124697 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
124708 #if 0 /* Enable to print out how the built-in functions are hashed */
124713 printf("FUNC-HASH %02d:", i);
124714 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
124715 int n = sqlite3Strlen30(p->zName);
124716 int h = p->zName[0] + n;
124717 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
124718 printf(" %s(%d)", p->zName, h);
124748 ** --------------------------
124830 ** ---------------
124832 ** Before coding an UPDATE or DELETE row operation, the code-generator
124837 ** accessed). No information is required by the code-generator before
124841 ** sqlite3FkRequired() - Test to see if FK processing is required.
124842 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
124846 ** --------------------------------------
124848 ** sqlite3FkCheck() - Check for foreign key violations.
124849 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
124850 ** sqlite3FkDelete() - Delete an FKey structure.
124855 ** -----------------------
124885 ** constraint to the parent table column stored in the left-most column
124887 ** child table column that corresponds to the second left-most column of
124906 ** then non-zero is returned, and a "foreign key mismatch" error loaded
124907 ** into pParse. If an OOM error occurs, non-zero is returned and the
124908 ** pParse->db->mallocFailed flag is set.
124919 int nCol = pFKey->nCol; /* Number of columns in parent key */
124920 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
124927 /* If this is a non-composite (single column) foreign key, check if it
124933 ** Non-composite foreign keys do not require the aiCol array.
124943 if( pParent->iPKey>=0 ){
124945 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
124951 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
124956 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
124957 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
124969 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
124974 /* If zKey is non-NULL, then this foreign key was declared to
124980 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
124989 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
124991 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
124993 zIdxCol = pParent->aCol[iCol].zCnName;
124995 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
124996 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
125008 if( !pParse->disableTriggers ){
125010 "foreign key mismatch - \"%w\" referencing \"%w\"",
125011 pFKey->pFrom->zName, pFKey->zTo);
125013 sqlite3DbFree(pParse->db, aiCol);
125025 ** affected - once to "delete" the old row, and then again to "insert" the
125035 ** --------------------------------------------------------------------------
125060 int iCur = pParse->nTab - 1; /* Cursor number to use */
125064 (!pFKey->isDeferred
125065 && !(pParse->db->flags & SQLITE_DeferFKs)
125066 && !pParse->pToplevel
125067 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
125077 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
125080 for(i=0; i<pFKey->nCol; i++){
125081 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
125098 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
125103 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125105 ** increment the constraint-counter. */
125106 if( pTab==pFKey->pFrom && nIncr==1 ){
125114 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
125118 int nCol = pFKey->nCol;
125121 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
125125 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
125130 ** to increment the constraint-counter (i.e. this is an INSERT operation),
125132 ** increment the constraint-counter.
125134 ** If any of the parent-key values are NULL, then the row cannot match
125136 ** of the parent-key values are NULL (at this point it is known that
125139 if( pTab==pFKey->pFrom && nIncr==1 ){
125142 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
125145 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
125146 pIdx->aiColumn[i]);
125147 assert( pIdx->aiColumn[i]>=0 );
125148 assert( aiCol[i]!=pTab->iPKey );
125149 if( pIdx->aiColumn[i]==pTab->iPKey ){
125160 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
125167 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
125168 && !pParse->pToplevel
125169 && !pParse->isMultiWrite
125179 if( nIncr>0 && pFKey->isDeferred==0 ){
125182 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
125207 sqlite3 *db = pParse->db;
125211 if( iCol>=0 && iCol!=pTab->iPKey ){
125212 pCol = &pTab->aCol[iCol];
125213 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
125214 pExpr->affExpr = pCol->affinity;
125216 if( zColl==0 ) zColl = db->pDfltColl->zName;
125219 pExpr->iTable = regBase;
125220 pExpr->affExpr = SQLITE_AFF_INTEGER;
125239 pExpr->y.pTab = pTab;
125240 pExpr->iTable = iCursor;
125241 pExpr->iColumn = iCol;
125250 ** code for an SQL UPDATE operation, this function may be called twice -
125253 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
125262 ** --------------------------------------------------------------------------
125284 sqlite3 *db = pParse->db; /* Database handle */
125292 assert( pIdx==0 || pIdx->pTable==pTab );
125293 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
125294 assert( pIdx!=0 || pFKey->nCol==1 );
125298 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
125304 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
125310 for(i=0; i<pFKey->nCol; i++){
125317 iCol = pIdx ? pIdx->aiColumn[i] : -1;
125319 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125321 zCol = pFKey->pFrom->aCol[iCol].zCnName;
125341 if( pTab==pFKey->pFrom && nIncr>0 ){
125346 pLeft = exprTableRegister(pParse, pTab, regData, -1);
125347 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
125352 for(i=0; i<pIdx->nKeyCol; i++){
125353 i16 iCol = pIdx->aiColumn[i];
125356 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
125374 if( pParse->nErr==0 ){
125376 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
125404 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
125410 ** and all of its sub-components.
125412 ** The Trigger structure or any of its sub-components may be allocated from
125417 TriggerStep *pStep = p->step_list;
125418 sqlite3ExprDelete(dbMem, pStep->pWhere);
125419 sqlite3ExprListDelete(dbMem, pStep->pExprList);
125420 sqlite3SelectDelete(dbMem, pStep->pSelect);
125421 sqlite3ExprDelete(dbMem, p->pWhen);
125433 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
125438 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125439 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
125440 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
125463 sqlite3 *db = pParse->db;
125464 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
125477 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125478 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
125485 pParse->disableTriggers = 1;
125487 pParse->disableTriggers = 0;
125498 if( (db->flags & SQLITE_DeferFKs)==0 ){
125519 ** is set to -1). If the rowid column is modified by the UPDATE statement
125520 ** the bChngRowid argument is non-zero.
125532 for(i=0; i<p->nCol; i++){
125533 int iChildKey = p->aCol[i].iFrom;
125535 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
125546 ** is set to -1). If the rowid column is modified by the UPDATE statement
125547 ** the bChngRowid argument is non-zero.
125559 for(i=0; i<p->nCol; i++){
125560 char *zKey = p->aCol[i].zCol;
125562 for(iKey=0; iKey<pTab->nCol; iKey++){
125563 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
125564 Column *pCol = &pTab->aCol[iKey];
125566 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
125567 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
125583 if( pTop->pTriggerPrg ){
125584 Trigger *p = pTop->pTriggerPrg->pTrigger;
125585 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
125586 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
125600 ** first register in an array of (pTab->nCol+1) registers containing the
125606 ** first register of an array of (pTab->nCol+1) registers containing the new
125622 sqlite3 *db = pParse->db; /* Database handle */
125626 int isIgnoreErrors = pParse->disableTriggers;
125628 /* Exactly one of regOld and regNew should be non-zero. */
125631 /* If foreign-keys are disabled, this function is a no-op. */
125632 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
125635 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
125636 zDb = db->aDb[iDb].zDbSName;
125640 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
125650 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
125660 if( pParse->disableTriggers ){
125661 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
125663 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
125667 if( !isIgnoreErrors || db->mallocFailed ) return;
125674 ** FK counter for each row of the current table with non-NULL keys.
125677 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
125678 for(i=0; i<pFKey->nCol; i++){
125680 iFromCol = pFKey->aCol[i].iFrom;
125681 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
125684 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
125688 assert( pFKey->nCol==1 || (aiFree && pIdx) );
125693 iCol = pFKey->aCol[0].iFrom;
125696 for(i=0; i<pFKey->nCol; i++){
125697 if( aiCol[i]==pTab->iPKey ){
125698 aiCol[i] = -1;
125700 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
125705 if( db->xAuth ){
125707 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
125708 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
125714 /* Take a shared-cache advisory read-lock on the parent table. Allocate
125717 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
125718 pParse->nTab++;
125724 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
125743 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
125752 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
125753 && !pParse->pToplevel && !pParse->isMultiWrite
125762 if( !isIgnoreErrors || db->mallocFailed ) return;
125765 assert( aiCol || pFKey->nCol==1 );
125771 SrcItem *pItem = pSrc->a;
125772 pItem->pTab = pFKey->pFrom;
125773 pItem->zName = pFKey->pFrom->zName;
125774 pItem->pTab->nTabRef++;
125775 pItem->iCursor = pParse->nTab++;
125778 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
125781 int eAction = pFKey->aAction[aChange!=0];
125786 ** So do not set the "may-abort" flag in this case.
125789 ** may-abort flag will eventually be set on this statement anyway
125800 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
125804 pItem->zName = 0;
125822 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
125825 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125826 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
125828 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125832 for(i=0; i<pIdx->nKeyCol; i++){
125833 assert( pIdx->aiColumn[i]>=0 );
125834 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
125849 ** entry in the aChange[] array is set to -1. If the column is modified,
125854 ** non-zero. If there is no foreign key related processing, this function
125870 int *aChange, /* Non-NULL for UPDATE operations */
125875 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
125880 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
125887 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
125889 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
125895 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
125897 if( p->aAction[1]!=OE_None ) return 2;
125908 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
125916 ** require no special handling by the triggers sub-system, code for them is
125939 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
125941 sqlite3 *db = pParse->db; /* Database handle */
125946 action = pFKey->aAction[iAction];
125947 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
125950 pTrigger = pFKey->apTrigger[iAction];
125956 int *aiCol = 0; /* child table cols -> parent key cols */
125965 assert( aiCol || pFKey->nCol==1 );
125967 for(i=0; i<pFKey->nCol; i++){
125975 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
125977 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
125978 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
125980 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
125981 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
126019 Column *pCol = pFKey->pFrom->aCol + iFromCol;
126021 if( pCol->colFlags & COLFLAG_GENERATED ){
126022 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
126023 testcase( pCol->colFlags & COLFLAG_STORED );
126026 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
126042 zFrom = pFKey->pFrom->zName;
126046 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126053 tDb.z = db->aDb[iDb].zDbSName;
126058 pRaise->affExpr = OE_Abort;
126075 nFrom + 1 /* Space for pStep->zTarget */
126078 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
126079 pStep->zTarget = (char *)&pStep[1];
126080 memcpy((char *)pStep->zTarget, zFrom, nFrom);
126082 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
126083 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
126084 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
126087 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
126091 /* Re-enable the lookaside buffer, if it was disabled earlier. */
126098 if( db->mallocFailed==1 ){
126107 pStep->op = TK_SELECT;
126111 pStep->op = TK_DELETE;
126116 pStep->op = TK_UPDATE;
126118 pStep->pTrig = pTrigger;
126119 pTrigger->pSchema = pTab->pSchema;
126120 pTrigger->pTabSchema = pTab->pSchema;
126121 pFKey->apTrigger[iAction] = pTrigger;
126122 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
126135 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
126140 /* If foreign-key support is enabled, iterate through all FKs that
126143 ** trigger sub-program. */
126144 if( pParse->db->flags&SQLITE_ForeignKeys ){
126146 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
126166 FKey *pNext; /* Copy of pFKey->pNextFrom */
126169 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
126170 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
126173 if( !db || db->pnBytesFreed==0 ){
126174 if( pFKey->pPrevTo ){
126175 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
126177 void *p = (void *)pFKey->pNextTo;
126178 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
126179 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
126181 if( pFKey->pNextTo ){
126182 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
126186 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
126189 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
126193 fkTriggerDelete(db, pFKey->apTrigger[0]);
126194 fkTriggerDelete(db, pFKey->apTrigger[1]);
126197 pNext = pFKey->pNextFrom;
126239 assert( pParse->pVdbe!=0 );
126240 v = pParse->pVdbe;
126242 sqlite3TableLock(pParse, iDb, pTab->tnum,
126243 (opcode==OP_OpenWrite)?1:0, pTab->zName);
126245 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
126246 VdbeComment((v, "%s", pTab->zName));
126250 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
126251 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
126253 VdbeComment((v, "%s", pTab->zName));
126263 ** ------------------------------
126278 if( !pIdx->zColAff ){
126288 Table *pTab = pIdx->pTable;
126289 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
126290 if( !pIdx->zColAff ){
126294 for(n=0; n<pIdx->nColumn; n++){
126295 i16 x = pIdx->aiColumn[n];
126298 aff = pTab->aCol[x].affinity;
126303 assert( pIdx->aColExpr!=0 );
126304 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
126308 pIdx->zColAff[n] = aff;
126310 pIdx->zColAff[n] = 0;
126313 return pIdx->zColAff;
126320 ** For ordinary (legacy, non-strict) tables:
126321 ** -----------------------------------------
126327 ** which were then optimized out) then this routine becomes a no-op.
126337 ** --------- ---------------
126345 ** ------------------
126359 if( pTab->tabFlags & TF_Strict ){
126366 pPrev = sqlite3VdbeGetOp(v, -1);
126368 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
126369 pPrev->opcode = OP_TypeCheck;
126370 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
126373 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
126378 zColAff = pTab->zColAff;
126381 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
126387 for(i=j=0; i<pTab->nCol; i++){
126388 assert( pTab->aCol[i].affinity!=0 || sqlite3VdbeParser(v)->nErr>0 );
126389 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
126390 zColAff[j++] = pTab->aCol[i].affinity;
126394 zColAff[j--] = 0;
126396 pTab->zColAff = zColAff;
126404 assert( sqlite3VdbeGetOp(v, -1)->opcode==OP_MakeRecord
126405 || sqlite3VdbeDb(v)->mallocFailed );
126406 sqlite3VdbeChangeP4(v, -1, zColAff, i);
126412 ** Return non-zero if the table pTab in database iDb or any of its indices
126422 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
126428 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
126430 Pgno tnum = pOp->p2;
126431 if( tnum==pTab->tnum ){
126434 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
126435 if( tnum==pIndex->tnum ){
126441 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
126442 assert( pOp->p4.pVtab!=0 );
126443 assert( pOp->p4type==P4_VTAB );
126455 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
126456 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
126457 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
126481 assert( pTab->tabFlags & TF_HasGenerated );
126482 testcase( pTab->tabFlags & TF_HasVirtual );
126483 testcase( pTab->tabFlags & TF_HasStored );
126488 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
126489 if( (pTab->tabFlags & TF_HasStored)!=0 ){
126490 pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
126491 if( pOp->opcode==OP_Affinity ){
126493 ** columns. '@' is the no-op affinity and those columns have not
126496 char *zP4 = pOp->p4.z;
126498 assert( pOp->p4type==P4_DYNAMIC );
126500 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
126503 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
126508 }else if( pOp->opcode==OP_TypeCheck ){
126512 pOp->p3 = 1;
126517 ** this is a two-pass algorithm. On the first pass, mark all generated
126520 for(i=0; i<pTab->nCol; i++){
126521 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
126522 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
126523 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
126524 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
126533 /* On the second pass, compute the value of each NOT-AVAILABLE column.
126538 pParse->iSelfTab = -iRegStore;
126542 for(i=0; i<pTab->nCol; i++){
126543 Column *pCol = pTab->aCol + i;
126544 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
126546 pCol->colFlags |= COLFLAG_BUSY;
126549 pCol->colFlags &= ~COLFLAG_BUSY;
126555 assert( pCol->colFlags & COLFLAG_GENERATED );
126558 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
126563 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
126565 pParse->iSelfTab = 0;
126600 assert( pParse->db->aDb[iDb].pSchema!=0 );
126601 if( (pTab->tabFlags & TF_Autoincrement)!=0
126602 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
126606 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
126610 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
126614 || pSeqTab->nCol!=2
126616 pParse->nErr++;
126617 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
126621 pInfo = pToplevel->pAinc;
126622 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
126624 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
126626 testcase( pParse->earlyCleanup );
126627 if( pParse->db->mallocFailed ) return 0;
126628 pInfo->pNext = pToplevel->pAinc;
126629 pToplevel->pAinc = pInfo;
126630 pInfo->pTab = pTab;
126631 pInfo->iDb = iDb;
126632 pToplevel->nMem++; /* Register to hold name of table */
126633 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
126634 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
126636 memId = pInfo->regCtr;
126647 sqlite3 *db = pParse->db; /* The database connection */
126650 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
126652 /* This routine is never called during trigger-generation. It is
126653 ** only called from the top-level */
126654 assert( pParse->pTriggerTab==0 );
126658 for(p = pParse->pAinc; p; p = p->pNext){
126675 pDb = &db->aDb[p->iDb];
126676 memId = p->regCtr;
126677 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
126678 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
126679 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
126685 aOp[3].p1 = memId-1;
126694 if( pParse->nTab==0 ) pParse->nTab = 1;
126708 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
126721 Vdbe *v = pParse->pVdbe;
126722 sqlite3 *db = pParse->db;
126725 for(p = pParse->pAinc; p; p = p->pNext){
126735 Db *pDb = &db->aDb[p->iDb];
126737 int memId = p->regCtr;
126740 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
126743 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
126748 aOp[2].p1 = memId-1;
126757 if( pParse->pAinc ) autoIncrementEnd(pParse);
126762 ** above are all no-ops
126786 ** then a list of all (non-hidden) columns for the table is substituted.
126791 ** first two forms shown above. A VALUES clause is really just short-hand
126797 ** insert with data coming from a single-row VALUES clause, the code executes
126798 ** once straight down through. Pseudo-code follows (we call this
126810 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
126834 ** X <- A
126842 ** end-coroutine X
126855 ** X <- A
126863 ** end co-routine R
126892 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
126921 db = pParse->db;
126922 assert( db->pParse==pParse );
126923 if( pParse->nErr ){
126926 assert( db->mallocFailed==0 );
126933 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
126934 pList = pSelect->pEList;
126935 pSelect->pEList = 0;
126942 assert( pTabList->nSrc==1 );
126947 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126948 assert( iDb<db->nDb );
126949 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
126950 db->aDb[iDb].zDbSName) ){
126975 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
126981 ** ViewGetColumnNames() is a no-op if pTab is not a view.
126987 /* Cannot insert into a read-only table.
126997 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
127029 regRowid = regIns = pParse->nMem+1;
127030 pParse->nMem += pTab->nCol + 1;
127033 pParse->nMem++;
127046 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
127047 ** loop, if ipkColumn==(-1), that means that integer primary key
127056 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
127058 assert( pColumn->eU4!=EU4_EXPR );
127059 pColumn->eU4 = EU4_IDX;
127060 for(i=0; i<pColumn->nId; i++){
127061 pColumn->a[i].u4.idx = -1;
127063 for(i=0; i<pColumn->nId; i++){
127064 for(j=0; j<pTab->nCol; j++){
127065 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zCnName)==0 ){
127066 pColumn->a[i].u4.idx = j;
127068 if( j==pTab->iPKey ){
127072 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
127075 pTab->aCol[j].zCnName);
127082 if( j>=pTab->nCol ){
127083 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
127088 pTabList->a, pColumn->a[i].zName);
127089 pParse->checkSchema = 1;
127097 ** is coming from a SELECT statement, then generate a co-routine that
127099 ** co-routine is the common header to the 3rd and 4th templates.
127102 /* Data is coming from a SELECT or from a multi-row VALUES clause.
127103 ** Generate a co-routine to run the SELECT. */
127104 int regYield; /* Register holding co-routine entry-point */
127105 int addrTop; /* Top of the co-routine */
127108 regYield = ++pParse->nMem;
127113 dest.nSdst = pTab->nCol;
127116 assert( db->pParse==pParse );
127117 if( rc || pParse->nErr ) goto insert_cleanup;
127118 assert( db->mallocFailed==0 );
127120 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
127121 assert( pSelect->pEList );
127122 nColumn = pSelect->pEList->nExpr;
127152 srcTab = pParse->nTab++;
127167 ** single-row VALUES clause
127172 srcTab = -1;
127175 nColumn = pList->nExpr;
127189 ipkColumn = pTab->iPKey;
127191 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
127192 testcase( pTab->tabFlags & TF_HasVirtual );
127193 testcase( pTab->tabFlags & TF_HasStored );
127194 for(i=ipkColumn-1; i>=0; i--){
127195 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
127196 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
127197 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
127198 ipkColumn--;
127210 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
127211 for(i=0; i<pTab->nCol; i++){
127212 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
127215 if( nColumn!=(pTab->nCol-nHidden) ){
127218 pTabList->a, pTab->nCol-nHidden, nColumn);
127222 if( pColumn!=0 && nColumn!=pColumn->nId ){
127223 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
127229 if( (db->flags & SQLITE_CountRows)!=0
127230 && !pParse->nested
127231 && !pParse->pTriggerTab
127232 && !pParse->bReturning
127234 regRowCount = ++pParse->nMem;
127241 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
127247 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
127249 aRegIdx[i] = ++pParse->nMem;
127250 pParse->nMem += pIdx->nColumn;
127252 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
127259 pTab->zName);
127266 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
127269 pTabList->a[0].iCursor = iDataCur;
127272 pNx->pUpsertSrc = pTabList;
127273 pNx->regData = regData;
127274 pNx->iDataCur = iDataCur;
127275 pNx->iIdxCur = iIdxCur;
127276 if( pNx->pUpsertTarget ){
127281 pNx = pNx->pNextUpsert;
127309 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
127313 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
127315 ** the value does not get overwritten by a NULL at tag-20191021-002. */
127329 for(i=0; i<pTab->nCol; i++, iRegStore++){
127333 if( i==pTab->iPKey ){
127334 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
127337 ** NULL - we cannot optimize further by skipping the column completely */
127341 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
127347 iRegStore--;
127351 ** triggers, the slots used for stored columns will be OP_Copy-ed
127362 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127368 assert( pColumn->eU4==EU4_IDX );
127369 for(j=0; j<pColumn->nId && pColumn->a[j].u4.idx!=i; j++){}
127370 if( j>=pColumn->nId ){
127374 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127382 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
127386 k = i - nHidden;
127396 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
127405 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
127411 ** not happened yet) so we substitute a rowid of -1
127414 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
127422 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
127425 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
127431 assert( pTab->nNVCol>0 );
127432 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
127439 if( pTab->tabFlags & TF_HasGenerated ){
127440 testcase( pTab->tabFlags & TF_HasVirtual );
127441 testcase( pTab->tabFlags & TF_HasStored );
127457 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
127459 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
127472 /* Rowid already initialized at tag-20191021-001 */
127474 Expr *pIpk = pList->a[ipkColumn].pExpr;
127475 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
127479 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
127510 if( pTab->tabFlags & TF_HasGenerated ){
127522 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
127549 }else if( pParse->bReturning ){
127551 ** constant value -1, in case one or more of the returned expressions
127553 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
127566 pTab, regData-2-pTab->nCol, onError, endOfLoop);
127583 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
127584 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
127598 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
127634 ** Meanings of bits in of pWalker->eCode for
127641 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
127646 if( pExpr->op==TK_COLUMN ){
127647 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
127648 if( pExpr->iColumn>=0 ){
127649 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
127650 pWalker->eCode |= CKCNSTRNT_COLUMN;
127653 pWalker->eCode |= CKCNSTRNT_ROWID;
127660 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
127669 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
127670 ** The operation of this routine is the same - return true if an only if
127698 ** the indexes of a table in the order provided in the Table->pIndex list.
127699 ** However, sometimes (rarely - when there is an upsert) it wants to visit
127733 assert( pIter->i==0 );
127734 if( pIter->eType ){
127735 *pIx = pIter->u.ax.aIdx[0].ix;
127736 return pIter->u.ax.aIdx[0].p;
127739 return pIter->u.lx.pIdx;
127745 if( pIter->eType ){
127746 int i = ++pIter->i;
127747 if( i>=pIter->u.ax.nIdx ){
127751 *pIx = pIter->u.ax.aIdx[i].ix;
127752 return pIter->u.ax.aIdx[i].p;
127755 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
127756 return pIter->u.lx.pIdx;
127766 ** pTab->nCol+1 registers in this range. The first register (the one
127793 ** at pTab->pIndex.
127795 ** (2019-05-07) The generated code also creates a new record for the
127797 ** register identified by aRegIdx[nIdx] - in other words in the first
127808 ** for the first index in the pTab->pIndex list. Cursors for other indices
127809 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
127817 ** --------------- ---------- ----------------------------------------
127847 ** Or if overrideError==OE_Default, then the pParse->onError parameter
127848 ** is used. Or if pParse->onError==OE_Default then the onError value
127859 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
127893 db = pParse->db;
127894 v = pParse->pVdbe;
127897 nCol = pTab->nCol;
127908 nPkField = pPk->nKeyCol;
127917 if( pTab->tabFlags & TF_HasNotNull ){
127924 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
127925 int isGenerated; /* non-zero if column is generated */
127926 onError = pCol->notNull;
127928 if( i==pTab->iPKey ){
127931 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
127947 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
127949 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
127950 testcase( pCol->colFlags & COLFLAG_STORED );
127951 testcase( pCol->colFlags & COLFLAG_GENERATED );
127967 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
127979 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
127980 pCol->zCnName);
128005 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
128013 } /* end of 2-pass loop */
128014 } /* end if( has-not-null-constraints ) */
128019 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
128020 ExprList *pCheck = pTab->pCheck;
128021 pParse->iSelfTab = -(regNewData+1);
128023 for(i=0; i<pCheck->nExpr; i++){
128026 Expr *pExpr = pCheck->a[i].pExpr;
128041 if( !db->mallocFailed ){
128048 char *zName = pCheck->a[i].zEName;
128049 assert( zName!=0 || pParse->db->mallocFailed );
128050 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
128057 pParse->iSelfTab = 0;
128074 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
128092 sIdxIter.u.lx.pIdx = pTab->pIndex;
128094 if( pUpsert->pUpsertTarget==0 ){
128095 /* There is just on ON CONFLICT clause and it has no constraint-target */
128096 assert( pUpsert->pNextUpsert==0 );
128097 if( pUpsert->isDoUpdate==0 ){
128098 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
128106 }else if( pTab->pIndex!=0 ){
128114 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
128123 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
128124 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
128125 if( pTerm->pUpsertTarget==0 ) break;
128126 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
128128 pIdx = pTab->pIndex;
128129 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
128130 pIdx = pIdx->pNext;
128139 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
128169 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
128175 if( db->flags&SQLITE_RecTriggers ){
128185 regTrigCnt = ++pParse->nMem;
128200 onError = pTab->keyConf;
128211 if( pUpsertClause->isDoUpdate==0 ){
128232 && pTab->pIndex /* There exist other constraints */
128271 ** recursive-triggers flag is set, call GenerateRowDelete() to
128273 ** the triggers and remove both the table and index b-tree entries.
128275 ** Otherwise, if there are no triggers or the recursive-triggers
128277 ** GenerateRowIndexDelete(). This removes the index b-tree entries
128278 ** only. The table b-tree entry will be replaced by the new entry
128295 regNewData, 1, 0, OE_Replace, 1, -1);
128301 /* This OP_Delete opcode fires the pre-update-hook only. It does
128302 ** not modify the b-tree. It is more efficient to let the coming
128308 if( pTab->pIndex ){
128310 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
128333 sqlite3VdbeJumpHere(v, ipkTop-1);
128366 VdbeNoopComment((v, "prep index %s", pIdx->zName));
128371 if( pIdx->pPartIdxWhere ){
128373 pParse->iSelfTab = -(regNewData+1);
128374 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
128376 pParse->iSelfTab = 0;
128383 for(i=0; i<pIdx->nColumn; i++){
128384 int iField = pIdx->aiColumn[i];
128387 pParse->iSelfTab = -(regNewData+1);
128388 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
128389 pParse->iSelfTab = 0;
128390 VdbeComment((v, "%s column %d", pIdx->zName, i));
128391 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
128399 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
128402 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
128403 VdbeComment((v, "for %s", pIdx->zName));
128405 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
128406 sqlite3SetMakeRecordP5(v, pIdx->pTable);
128409 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
128421 onError = pIdx->onError;
128434 if( pUpsertClause->isDoUpdate==0 ){
128449 ** must be explicitly deleted in order to ensure any pre-update hook
128453 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
128456 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
128458 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
128459 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
128470 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
128478 ** is different from old-rowid */
128487 ** store it in registers regR..regR+nPk-1 */
128489 for(i=0; i<pPk->nKeyCol; i++){
128490 assert( pPk->aiColumn[i]>=0 );
128491 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
128493 VdbeComment((v, "%s.%s", pTab->zName,
128494 pTab->aCol[pPk->aiColumn[i]].zCnName));
128505 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
128509 for(i=0; i<pPk->nKeyCol; i++){
128510 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
128511 x = pPk->aiColumn[i];
128513 if( i==(pPk->nKeyCol-1) ){
128557 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
128558 assert( nConflictCk>0 || db->mallocFailed );
128586 if( pIdx->pPartIdxWhere ){
128589 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
128593 ** the constraint-ok jump destination to be the address of
128614 nConflictCk--;
128669 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
128683 ** to be the number of columns in table pTab that must not be NULL-trimmed.
128685 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
128691 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
128692 if( pTab->pSchema->file_format<2 ) return;
128694 for(i=pTab->nCol-1; i>0; i--){
128695 if( pTab->aCol[i].iDflt!=0 ) break;
128696 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
128705 ** PK index. This function adds code to invoke the pre-update hook,
128715 Vdbe *v = pParse->pVdbe;
128718 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
128758 v = pParse->pVdbe;
128761 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128763 assert( pIdx->onError!=OE_Replace
128764 || pIdx->pNext==0
128765 || pIdx->pNext->onError==OE_Replace );
128767 if( pIdx->pPartIdxWhere ){
128781 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
128785 if( pParse->nested ){
128798 if( !pParse->nested ){
128814 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
128820 ** pTab->pIndex list.
128822 ** If pTab is a virtual table, then this routine is a no-op and the
128844 /* This routine is a no-op for virtual tables. Leave the output
128847 *piDataCur = *piIdxCur = -999;
128850 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128851 v = pParse->pVdbe;
128853 if( iBase<0 ) iBase = pParse->nTab;
128859 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
128862 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
128864 assert( pIdx->pSchema==pTab->pSchema );
128870 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
128873 VdbeComment((v, "%s", pIdx->zName));
128876 if( iBase>pParse->nTab ) pParse->nTab = iBase;
128885 ** purposes only - to make sure the transfer optimization really
128907 assert( pDest->pTable!=pSrc->pTable );
128908 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
128911 if( pDest->onError!=pSrc->onError ){
128914 for(i=0; i<pSrc->nKeyCol; i++){
128915 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
128918 if( pSrc->aiColumn[i]==XN_EXPR ){
128919 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
128920 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
128921 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
128925 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
128928 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
128932 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
128950 ** There are lots of rules for determining compatibility - see comments
128955 ** is empty - a factor that can only be determined at run-time. In that
128972 sqlite3 *db = pParse->db;
128976 SrcItem *pItem; /* An element of pSelect->pSrc */
128989 if( pParse->pWith || pSelect->pWith ){
129001 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
129004 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
129005 if( pSelect->pSrc->nSrc!=1 ){
129008 if( pSelect->pSrc->a[0].pSelect ){
129011 if( pSelect->pWhere ){
129014 if( pSelect->pOrderBy ){
129019 if( pSelect->pGroupBy ){
129022 if( pSelect->pLimit ){
129025 if( pSelect->pPrior ){
129028 if( pSelect->selFlags & SF_Distinct ){
129031 pEList = pSelect->pEList;
129033 if( pEList->nExpr!=1 ){
129036 assert( pEList->a[0].pExpr );
129037 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
129045 pItem = pSelect->pSrc->a;
129050 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
129060 if( pDest->nCol!=pSrc->nCol ){
129063 if( pDest->iPKey!=pSrc->iPKey ){
129066 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
129067 return 0; /* Cannot feed from a non-strict into a strict table */
129069 for(i=0; i<pDest->nCol; i++){
129070 Column *pDestCol = &pDest->aCol[i];
129071 Column *pSrcCol = &pSrc->aCol[i];
129073 if( (db->mDbFlags & DBFLAG_Vacuum)==0
129074 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
129096 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
129097 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
129098 return 0; /* Both columns have the same generated-column type */
129104 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
129107 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
129108 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
129109 testcase( pDestCol->colFlags & COLFLAG_STORED );
129114 if( pDestCol->affinity!=pSrcCol->affinity ){
129121 if( pDestCol->notNull && !pSrcCol->notNull ){
129125 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
129128 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
129130 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
129133 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
129134 pSrcExpr->u.zToken)!=0)
129140 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129144 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
129150 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
129155 return 0; /* Corrupt schema - two indexes on the same btree */
129159 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
129172 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
129176 if( (db->flags & SQLITE_CountRows)!=0 ){
129187 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
129190 iSrc = pParse->nTab++;
129191 iDest = pParse->nTab++;
129198 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
129199 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
129228 if( pDest->iPKey>=0 ){
129230 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129238 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
129242 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
129245 if( db->mDbFlags & DBFLAG_Vacuum ){
129252 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129261 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
129262 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
129270 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
129271 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
129273 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
129275 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
129279 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
129281 VdbeComment((v, "%s", pSrcIdx->zName));
129282 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
129285 VdbeComment((v, "%s", pDestIdx->zName));
129287 if( db->mDbFlags & DBFLAG_Vacuum ){
129292 ** order. In this case, instead of seeking within the b-tree as part
129294 ** OP_IdxInsert to seek to the point within the b-tree where each key
129302 for(i=0; i<pSrcIdx->nColumn; i++){
129303 const char *zColl = pSrcIdx->azColl[i];
129306 if( i==pSrcIdx->nColumn ){
129311 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
129316 if( (db->mDbFlags & DBFLAG_Vacuum)==0
129368 ** codes. Also write an error message into memory obtained from
129369 ** malloc() and make *pzErrMsg point to that message.
129392 sqlite3_mutex_enter(db->mutex);
129399 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
129405 /* this happens for a comment or white-space */
129418 && db->flags&SQLITE_NullCallback)) ){
129445 /* EVIDENCE-OF: R-38229-40159 If the callback function to
129446 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
129484 assert( (rc&db->errMask)==rc );
129485 sqlite3_mutex_leave(db->mutex);
129887 ** (part of the main SQLite library - not an extension) so that
129894 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
129896 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
129898 #define sqlite3_bind_blob sqlite3_api->bind_blob
129899 #define sqlite3_bind_double sqlite3_api->bind_double
129900 #define sqlite3_bind_int sqlite3_api->bind_int
129901 #define sqlite3_bind_int64 sqlite3_api->bind_int64
129902 #define sqlite3_bind_null sqlite3_api->bind_null
129903 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
129904 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
129905 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
129906 #define sqlite3_bind_text sqlite3_api->bind_text
129907 #define sqlite3_bind_text16 sqlite3_api->bind_text16
129908 #define sqlite3_bind_value sqlite3_api->bind_value
129909 #define sqlite3_busy_handler sqlite3_api->busy_handler
129910 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
129911 #define sqlite3_changes sqlite3_api->changes
129912 #define sqlite3_close sqlite3_api->close
129913 #define sqlite3_collation_needed sqlite3_api->collation_needed
129914 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
129915 #define sqlite3_column_blob sqlite3_api->column_blob
129916 #define sqlite3_column_bytes sqlite3_api->column_bytes
129917 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
129918 #define sqlite3_column_count sqlite3_api->column_count
129919 #define sqlite3_column_database_name sqlite3_api->column_database_name
129920 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
129921 #define sqlite3_column_decltype sqlite3_api->column_decltype
129922 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
129923 #define sqlite3_column_double sqlite3_api->column_double
129924 #define sqlite3_column_int sqlite3_api->column_int
129925 #define sqlite3_column_int64 sqlite3_api->column_int64
129926 #define sqlite3_column_name sqlite3_api->column_name
129927 #define sqlite3_column_name16 sqlite3_api->column_name16
129928 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
129929 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
129930 #define sqlite3_column_table_name sqlite3_api->column_table_name
129931 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
129932 #define sqlite3_column_text sqlite3_api->column_text
129933 #define sqlite3_column_text16 sqlite3_api->column_text16
129934 #define sqlite3_column_type sqlite3_api->column_type
129935 #define sqlite3_column_value sqlite3_api->column_value
129936 #define sqlite3_commit_hook sqlite3_api->commit_hook
129937 #define sqlite3_complete sqlite3_api->complete
129938 #define sqlite3_complete16 sqlite3_api->complete16
129939 #define sqlite3_create_collation sqlite3_api->create_collation
129940 #define sqlite3_create_collation16 sqlite3_api->create_collation16
129941 #define sqlite3_create_function sqlite3_api->create_function
129942 #define sqlite3_create_function16 sqlite3_api->create_function16
129943 #define sqlite3_create_module sqlite3_api->create_module
129944 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
129945 #define sqlite3_data_count sqlite3_api->data_count
129946 #define sqlite3_db_handle sqlite3_api->db_handle
129947 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
129948 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
129949 #define sqlite3_errcode sqlite3_api->errcode
129950 #define sqlite3_errmsg sqlite3_api->errmsg
129951 #define sqlite3_errmsg16 sqlite3_api->errmsg16
129952 #define sqlite3_exec sqlite3_api->exec
129954 #define sqlite3_expired sqlite3_api->expired
129956 #define sqlite3_finalize sqlite3_api->finalize
129957 #define sqlite3_free sqlite3_api->free
129958 #define sqlite3_free_table sqlite3_api->free_table
129959 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
129960 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
129961 #define sqlite3_get_table sqlite3_api->get_table
129963 #define sqlite3_global_recover sqlite3_api->global_recover
129965 #define sqlite3_interrupt sqlite3_api->interruptx
129966 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
129967 #define sqlite3_libversion sqlite3_api->libversion
129968 #define sqlite3_libversion_number sqlite3_api->libversion_number
129969 #define sqlite3_malloc sqlite3_api->malloc
129970 #define sqlite3_mprintf sqlite3_api->mprintf
129971 #define sqlite3_open sqlite3_api->open
129972 #define sqlite3_open16 sqlite3_api->open16
129973 #define sqlite3_prepare sqlite3_api->prepare
129974 #define sqlite3_prepare16 sqlite3_api->prepare16
129975 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
129976 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
129977 #define sqlite3_profile sqlite3_api->profile
129978 #define sqlite3_progress_handler sqlite3_api->progress_handler
129979 #define sqlite3_realloc sqlite3_api->realloc
129980 #define sqlite3_reset sqlite3_api->reset
129981 #define sqlite3_result_blob sqlite3_api->result_blob
129982 #define sqlite3_result_double sqlite3_api->result_double
129983 #define sqlite3_result_error sqlite3_api->result_error
129984 #define sqlite3_result_error16 sqlite3_api->result_error16
129985 #define sqlite3_result_int sqlite3_api->result_int
129986 #define sqlite3_result_int64 sqlite3_api->result_int64
129987 #define sqlite3_result_null sqlite3_api->result_null
129988 #define sqlite3_result_text sqlite3_api->result_text
129989 #define sqlite3_result_text16 sqlite3_api->result_text16
129990 #define sqlite3_result_text16be sqlite3_api->result_text16be
129991 #define sqlite3_result_text16le sqlite3_api->result_text16le
129992 #define sqlite3_result_value sqlite3_api->result_value
129993 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
129994 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
129995 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
129996 #define sqlite3_snprintf sqlite3_api->xsnprintf
129997 #define sqlite3_step sqlite3_api->step
129998 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
129999 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
130000 #define sqlite3_total_changes sqlite3_api->total_changes
130001 #define sqlite3_trace sqlite3_api->trace
130003 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
130005 #define sqlite3_update_hook sqlite3_api->update_hook
130006 #define sqlite3_user_data sqlite3_api->user_data
130007 #define sqlite3_value_blob sqlite3_api->value_blob
130008 #define sqlite3_value_bytes sqlite3_api->value_bytes
130009 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
130010 #define sqlite3_value_double sqlite3_api->value_double
130011 #define sqlite3_value_int sqlite3_api->value_int
130012 #define sqlite3_value_int64 sqlite3_api->value_int64
130013 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
130014 #define sqlite3_value_text sqlite3_api->value_text
130015 #define sqlite3_value_text16 sqlite3_api->value_text16
130016 #define sqlite3_value_text16be sqlite3_api->value_text16be
130017 #define sqlite3_value_text16le sqlite3_api->value_text16le
130018 #define sqlite3_value_type sqlite3_api->value_type
130019 #define sqlite3_vmprintf sqlite3_api->vmprintf
130020 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
130021 #define sqlite3_overload_function sqlite3_api->overload_function
130022 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
130023 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
130024 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
130025 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
130026 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
130027 #define sqlite3_blob_close sqlite3_api->blob_close
130028 #define sqlite3_blob_open sqlite3_api->blob_open
130029 #define sqlite3_blob_read sqlite3_api->blob_read
130030 #define sqlite3_blob_write sqlite3_api->blob_write
130031 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
130032 #define sqlite3_file_control sqlite3_api->file_control
130033 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
130034 #define sqlite3_memory_used sqlite3_api->memory_used
130035 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
130036 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
130037 #define sqlite3_mutex_free sqlite3_api->mutex_free
130038 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
130039 #define sqlite3_mutex_try sqlite3_api->mutex_try
130040 #define sqlite3_open_v2 sqlite3_api->open_v2
130041 #define sqlite3_release_memory sqlite3_api->release_memory
130042 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
130043 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
130044 #define sqlite3_sleep sqlite3_api->sleep
130045 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
130046 #define sqlite3_vfs_find sqlite3_api->vfs_find
130047 #define sqlite3_vfs_register sqlite3_api->vfs_register
130048 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
130049 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
130050 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
130051 #define sqlite3_result_error_code sqlite3_api->result_error_code
130052 #define sqlite3_test_control sqlite3_api->test_control
130053 #define sqlite3_randomness sqlite3_api->randomness
130054 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
130055 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
130056 #define sqlite3_limit sqlite3_api->limit
130057 #define sqlite3_next_stmt sqlite3_api->next_stmt
130058 #define sqlite3_sql sqlite3_api->sql
130059 #define sqlite3_status sqlite3_api->status
130060 #define sqlite3_backup_finish sqlite3_api->backup_finish
130061 #define sqlite3_backup_init sqlite3_api->backup_init
130062 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
130063 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
130064 #define sqlite3_backup_step sqlite3_api->backup_step
130065 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
130066 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
130067 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
130068 #define sqlite3_db_config sqlite3_api->db_config
130069 #define sqlite3_db_mutex sqlite3_api->db_mutex
130070 #define sqlite3_db_status sqlite3_api->db_status
130071 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
130072 #define sqlite3_log sqlite3_api->log
130073 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
130074 #define sqlite3_sourceid sqlite3_api->sourceid
130075 #define sqlite3_stmt_status sqlite3_api->stmt_status
130076 #define sqlite3_strnicmp sqlite3_api->strnicmp
130077 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
130078 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
130079 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
130080 #define sqlite3_wal_hook sqlite3_api->wal_hook
130081 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
130082 #define sqlite3_vtab_config sqlite3_api->vtab_config
130083 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
130085 #define sqlite3_close_v2 sqlite3_api->close_v2
130086 #define sqlite3_db_filename sqlite3_api->db_filename
130087 #define sqlite3_db_readonly sqlite3_api->db_readonly
130088 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
130089 #define sqlite3_errstr sqlite3_api->errstr
130090 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
130091 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
130092 #define sqlite3_stricmp sqlite3_api->stricmp
130093 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
130094 #define sqlite3_uri_int64 sqlite3_api->uri_int64
130095 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
130096 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
130097 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
130099 #define sqlite3_auto_extension sqlite3_api->auto_extension
130100 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
130101 #define sqlite3_bind_text64 sqlite3_api->bind_text64
130102 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
130103 #define sqlite3_load_extension sqlite3_api->load_extension
130104 #define sqlite3_malloc64 sqlite3_api->malloc64
130105 #define sqlite3_msize sqlite3_api->msize
130106 #define sqlite3_realloc64 sqlite3_api->realloc64
130107 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
130108 #define sqlite3_result_blob64 sqlite3_api->result_blob64
130109 #define sqlite3_result_text64 sqlite3_api->result_text64
130110 #define sqlite3_strglob sqlite3_api->strglob
130112 #define sqlite3_value_dup sqlite3_api->value_dup
130113 #define sqlite3_value_free sqlite3_api->value_free
130114 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
130115 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
130117 #define sqlite3_value_subtype sqlite3_api->value_subtype
130118 #define sqlite3_result_subtype sqlite3_api->result_subtype
130120 #define sqlite3_status64 sqlite3_api->status64
130121 #define sqlite3_strlike sqlite3_api->strlike
130122 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
130124 #define sqlite3_system_errno sqlite3_api->system_errno
130126 #define sqlite3_trace_v2 sqlite3_api->trace_v2
130127 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
130129 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
130131 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
130132 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
130133 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
130134 #define sqlite3_result_pointer sqlite3_api->result_pointer
130135 #define sqlite3_value_pointer sqlite3_api->value_pointer
130137 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
130138 #define sqlite3_value_nochange sqlite3_api->value_nochange
130139 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
130141 #define sqlite3_keyword_count sqlite3_api->keyword_count
130142 #define sqlite3_keyword_name sqlite3_api->keyword_name
130143 #define sqlite3_keyword_check sqlite3_api->keyword_check
130144 #define sqlite3_str_new sqlite3_api->str_new
130145 #define sqlite3_str_finish sqlite3_api->str_finish
130146 #define sqlite3_str_appendf sqlite3_api->str_appendf
130147 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
130148 #define sqlite3_str_append sqlite3_api->str_append
130149 #define sqlite3_str_appendall sqlite3_api->str_appendall
130150 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
130151 #define sqlite3_str_reset sqlite3_api->str_reset
130152 #define sqlite3_str_errcode sqlite3_api->str_errcode
130153 #define sqlite3_str_length sqlite3_api->str_length
130154 #define sqlite3_str_value sqlite3_api->str_value
130156 #define sqlite3_create_window_function sqlite3_api->create_window_function
130158 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
130160 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
130161 #define sqlite3_value_frombind sqlite3_api->value_frombind
130163 #define sqlite3_drop_modules sqlite3_api->drop_modules
130165 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
130166 #define sqlite3_uri_key sqlite3_api->uri_key
130167 #define sqlite3_filename_database sqlite3_api->filename_database
130168 #define sqlite3_filename_journal sqlite3_api->filename_journal
130169 #define sqlite3_filename_wal sqlite3_api->filename_wal
130171 #define sqlite3_create_filename sqlite3_api->create_filename
130172 #define sqlite3_free_filename sqlite3_api->free_filename
130173 #define sqlite3_database_file_object sqlite3_api->database_file_object
130175 #define sqlite3_txn_state sqlite3_api->txn_state
130177 #define sqlite3_changes64 sqlite3_api->changes64
130178 #define sqlite3_total_changes64 sqlite3_api->total_changes64
130180 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
130182 #define sqlite3_error_offset sqlite3_api->error_offset
130183 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130184 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130185 #define sqlite3_vtab_in sqlite3_api->vtab_in
130186 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130187 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
130190 #define sqlite3_deserialize sqlite3_api->deserialize
130191 #define sqlite3_serialize sqlite3_api->serialize
130193 #define sqlite3_db_name sqlite3_api->db_name
130206 # define SQLITE_EXTENSION_INIT1 /*no-op*/
130208 # define SQLITE_EXTENSION_INIT3 /*no-op*/
130726 ** error message text. The calling function should free this memory
130733 char **pzErrMsg /* Put error message here if not 0 */
130735 sqlite3_vfs *pVfs = db->pVfs;
130767 if( (db->flags & SQLITE_LoadExtension)==0 ){
130776 /* tag-20210611-1. Some dlopen() implementations will segfault if given
130814 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
130834 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130853 /* Append the new shared library handle to the db->aExtension array. */
130854 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
130858 if( db->nExtension>0 ){
130859 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
130861 sqlite3DbFree(db, db->aExtension);
130862 db->aExtension = aHandle;
130864 db->aExtension[db->nExtension++] = handle;
130875 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
130884 char **pzErrMsg /* Put error message here if not 0 */
130887 sqlite3_mutex_enter(db->mutex);
130890 sqlite3_mutex_leave(db->mutex);
130900 assert( sqlite3_mutex_held(db->mutex) );
130901 for(i=0; i<db->nExtension; i++){
130902 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
130904 sqlite3DbFree(db, db->aExtension);
130912 sqlite3_mutex_enter(db->mutex);
130914 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
130916 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
130918 sqlite3_mutex_leave(db->mutex);
130939 ** we have to locate the state vector at run-time. In the more common
130999 ** routine is a no-op.
131014 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
131016 wsdAutoext.nExt--;
131182 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
131186 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
131188 /* Names of columns for pragmas that return multi-column result
131189 ** or that return single-column results where the name of the
131256 /* Definitions of all built-in pragmas */
131847 ** Interpret the given string as an auto-vacuum mode value.
131865 ** backed temporary databases, 2 for the Red-Black tree in memory database
131866 ** and 0 to use the compile-time default.
131870 return z[0] - '0';
131887 sqlite3 *db = pParse->db;
131888 if( db->aDb[1].pBt!=0 ){
131889 if( !db->autoCommit
131890 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
131896 sqlite3BtreeClose(db->aDb[1].pBt);
131897 db->aDb[1].pBt = 0;
131912 sqlite3 *db = pParse->db;
131913 if( db->temp_store==ts ) return SQLITE_OK;
131917 db->temp_store = (u8)ts;
131929 u8 n = pPragma->nPragCName;
131932 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
131935 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
131969 if( db->autoCommit ){
131970 Db *pDb = db->aDb;
131971 int n = db->nDb;
131977 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
131978 while( (n--) > 0 ){
131979 if( pDb->pBt ){
131980 sqlite3BtreeSetPagerFlags(pDb->pBt,
131981 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
131988 # define setAllPagerFlags(X) /* no-op */
131993 ** Return a human-readable name for a constraint resolution action.
132014 ** journal-mode name.
132041 upr = ArraySize(aPragmaName)-1;
132047 upr = mid - 1;
132062 int isBuiltin, /* True if this is a built-in function */
132073 for(; p; p=p->pNext){
132082 if( p->xSFunc==0 ) continue;
132083 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
132088 if( p->xValue!=0 ){
132090 }else if( p->xFinalize!=0 ){
132096 p->zName, isBuiltin,
132097 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
132098 p->nArg,
132099 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
132108 ** Generate code to output a single-column result row with a value of the
132141 int minusFlag /* True if a '-' sign preceded <value> */
132143 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
132144 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
132150 sqlite3 *db = pParse->db; /* The database connection */
132157 pParse->nMem = 2;
132163 pDb = &db->aDb[iDb];
132175 zRight = sqlite3MPrintf(db, "-%T", pValue);
132181 zDb = pId2->n>0 ? pDb->zDbSName : 0;
132186 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
132188 ** handled the pragma and generate a no-op prepared statement.
132190 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
132195 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
132205 db->busyHandler.nBusy = 0;
132219 pParse->nErr++;
132220 pParse->rc = rc;
132227 /* IMP: R-43042-22504 No error messages are generated if an
132233 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
132238 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
132239 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
132245 switch( pPragma->ePragTyp ){
132280 pParse->nMem += 2;
132292 pDb->pSchema->cache_size = size;
132293 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132310 Btree *pBt = pDb->pBt;
132316 /* Malloc may fail when setting the page-size, as there is an internal
132319 db->nextPagesize = sqlite3Atoi(zRight);
132320 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
132336 Btree *pBt = pDb->pBt;
132337 int b = -1;
132346 if( pId2->n==0 && b>=0 ){
132348 for(ii=0; ii<db->nDb; ii++){
132349 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
132378 iReg = ++pParse->nMem;
132402 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
132405 ** the locking-mode of the main database).
132407 eMode = db->dfltLockMode;
132410 if( pId2->n==0 ){
132412 ** of the PRAGMA command. In this case the locking-mode must be
132420 assert(pDb==&db->aDb[0]);
132421 for(ii=2; ii<db->nDb; ii++){
132422 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
132425 db->dfltLockMode = (u8)eMode;
132427 pPager = sqlite3BtreePager(pDb->pBt);
132464 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
132465 /* Do not allow journal-mode "OFF" in defensive since the database
132470 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
132473 pId2->n = 1;
132475 for(ii=db->nDb-1; ii>=0; ii--){
132476 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132492 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132493 i64 iLimit = -2;
132496 if( iLimit<-1 ) iLimit = -1;
132509 ** Get or set the value of the database 'auto-vacuum' parameter.
132514 Btree *pBt = pDb->pBt;
132521 db->nextAutovac = (u8)eAuto;
132523 ** incr-vacuum flags. This is required in case this connection
132525 ** as an auto-vacuum capable db.
132532 ** that this really is an auto-vacuum capable database.
132551 aOp[4].p3 = eAuto - 1;
132574 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
132590 ** number of pages is adjusted so that the cache uses -N kibibytes
132596 returnSingleInt(v, pDb->pSchema->cache_size);
132599 pDb->pSchema->cache_size = size;
132600 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
132617 ** number of pages is adjusted so that the cache uses -N kibibytes
132631 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
132632 sqlite3BtreeSetSpillSize(pDb->pBt,0));
132636 sqlite3BtreeSetSpillSize(pDb->pBt, size);
132639 db->flags |= SQLITE_CacheSpill;
132641 db->flags &= ~(u64)SQLITE_CacheSpill;
132670 if( pId2->n==0 ) db->szMmap = sz;
132671 for(ii=db->nDb-1; ii>=0; ii--){
132672 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
132673 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
132677 sz = -1;
132686 pParse->nErr++;
132687 pParse->rc = rc;
132700 ** Note that it is possible for the library compile-time options to
132705 returnSingleInt(v, db->temp_store);
132729 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132736 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
132737 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
132773 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
132802 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132809 Pager *pPager = sqlite3BtreePager(pDb->pBt);
132839 returnSingleInt(v, pDb->safety_level-1);
132841 if( !db->autoCommit ){
132847 pDb->safety_level = iLevel;
132848 pDb->bSyncSet = 1;
132860 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
132862 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
132863 if( db->autoCommit==0 ){
132865 ** in auto-commit mode. */
132869 if( db->auth.authLevel==UAUTH_User ){
132870 /* Do not allow non-admin users to modify the schema arbitrarily */
132876 db->flags |= mask;
132878 db->flags &= ~mask;
132879 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
132883 /* IMP: R-60817-01178 If the argument is "RESET" then schema
132890 /* Many of the flag-pragmas modify the code generated by the SQL
132913 ** pk: Non-zero for PK fields.
132924 pParse->nMem = 7;
132926 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
132929 if( pCol->colFlags & COLFLAG_NOINSERT ){
132930 if( pPragma->iArg==0 ){
132934 if( pCol->colFlags & COLFLAG_VIRTUAL ){
132936 }else if( pCol->colFlags & COLFLAG_STORED ){
132938 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
132942 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
132947 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
132950 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
132953 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
132954 i-nHidden,
132955 pCol->zCnName,
132957 pCol->notNull ? 1 : 0,
132958 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
132981 pParse->nMem = 6;
132983 for(ii=0; ii<db->nDb; ii++){
132987 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
132994 pHash = &db->aDb[ii].pSchema->tblHash;
132996 while( initNCol-- ){
133001 if( pTab->nCol==0 ){
133002 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
133005 (void)sqlite3_prepare(db, zSql, -1, &pDummy, 0);
133009 if( db->mallocFailed ){
133010 sqlite3ErrorMsg(db->pParse, "out of memory");
133011 db->pParse->rc = SQLITE_NOMEM_BKPT;
133013 pHash = &db->aDb[ii].pSchema->tblHash;
133022 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
133027 }else if( pTab->tabFlags & TF_Shadow ){
133033 db->aDb[ii].zDbSName,
133034 sqlite3PreferredTableName(pTab->zName),
133036 pTab->nCol,
133037 (pTab->tabFlags & TF_WithoutRowid)!=0,
133038 (pTab->tabFlags & TF_Strict)!=0
133049 pParse->nMem = 5;
133051 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
133054 sqlite3PreferredTableName(pTab->zName),
133056 pTab->szTabRow,
133057 pTab->nRowLogEst,
133058 pTab->tabFlags);
133059 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133061 pIdx->zName,
133062 pIdx->szIdxRow,
133063 pIdx->aiRowLogEst[0],
133064 pIdx->hasStat1);
133086 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
133089 if( pPragma->iArg ){
133091 mx = pIdx->nColumn;
133092 pParse->nMem = 6;
133095 mx = pIdx->nKeyCol;
133096 pParse->nMem = 3;
133098 pTab = pIdx->pTable;
133100 assert( pParse->nMem<=pPragma->nPragCName );
133102 i16 cnum = pIdx->aiColumn[i];
133104 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
133105 if( pPragma->iArg ){
133107 pIdx->aSortOrder[i],
133108 pIdx->azColl[i],
133109 i<pIdx->nKeyCol);
133111 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
133123 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133124 pParse->nMem = 5;
133126 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
133130 pIdx->zName,
133132 azOrigin[pIdx->idxType],
133133 pIdx->pPartIdxWhere!=0);
133141 pParse->nMem = 3;
133142 for(i=0; i<db->nDb; i++){
133143 if( db->aDb[i].pBt==0 ) continue;
133144 assert( db->aDb[i].zDbSName!=0 );
133147 db->aDb[i].zDbSName,
133148 sqlite3BtreeGetFilename(db->aDb[i].pBt));
133156 pParse->nMem = 2;
133157 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
133159 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
133169 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
133170 pParse->nMem = 6;
133172 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
133173 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
133177 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
133179 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
133188 pParse->nMem = 1;
133189 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
133191 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
133214 pFK = pTab->u.tab.pFKey;
133216 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133218 pParse->nMem = 8;
133222 for(j=0; j<pFK->nCol; j++){
133226 pFK->zTo,
133227 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
133228 pFK->aCol[j].zCol,
133229 actionName(pFK->aAction[1]), /* ON UPDATE */
133230 actionName(pFK->aAction[0]), /* ON DELETE */
133234 pFK = pFK->pNextFrom;
133259 regResult = pParse->nMem+1;
133260 pParse->nMem += 4;
133261 regRow = ++pParse->nMem;
133262 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
133271 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
133272 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
133273 zDb = db->aDb[iDb].zDbSName;
133275 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
133276 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
133278 sqlite3VdbeLoadString(v, regResult, pTab->zName);
133280 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133281 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133284 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
133290 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
133298 assert( pParse->nErr>0 || pFK==0 );
133300 if( pParse->nTab<i ) pParse->nTab = i;
133303 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
133304 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
133309 assert( x==0 || db->mallocFailed );
133317 if( regRow+pFK->nCol>pParse->nMem ) pParse->nMem = regRow+pFK->nCol;
133318 for(j=0; j<pFK->nCol; j++){
133319 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
133327 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
133328 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
133329 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
133335 assert( pFK->nCol==1 || db->mallocFailed );
133344 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
133383 ** without the overhead of cross-checking indexes. Quick_check
133411 ** to -1 here, to indicate that the VDBE should verify the integrity
133414 assert( iDb==0 || pId2->z );
133415 if( pId2->z==0 ) iDb = -1;
133418 pParse->nMem = 6;
133429 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
133432 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
133435 for(i=0; i<db->nDb; i++){
133447 /* Do an integrity check of the B-Tree
133453 pTbls = &db->aDb[i].pSchema->tblHash;
133460 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
133473 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
133474 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133475 aRoot[++cnt] = pIdx->tnum;
133481 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
133484 /* Do the b-tree integrity checks */
133489 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
133503 int r1 = -1;
133512 ** reg[8+i] counts the number of entries in the i-th index
133515 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133518 assert( pParse->nMem>=8+j );
133524 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
133526 VdbeComment((v, "(right-most column)"));
133530 bStrict = (pTab->tabFlags & TF_Strict)!=0;
133531 for(j=0; j<pTab->nCol; j++){
133533 Column *pCol = pTab->aCol + j;
133535 if( j==pTab->iPKey ) continue;
133536 if( pCol->notNull==0 && !bStrict ) continue;
133539 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
133542 if( pCol->notNull ){
133544 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
133545 pCol->zCnName);
133547 if( bStrict && pCol->eCType!=COLTYPE_ANY ){
133554 if( (pTab->tabFlags & TF_Strict)!=0
133555 && pCol->eCType!=COLTYPE_ANY
133558 sqlite3StdTypeMap[pCol->eCType-1]);
133560 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
133561 sqlite3StdType[pCol->eCType-1],
133562 pTab->zName, pTab->aCol[j].zCnName);
133570 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
133571 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
133572 if( db->mallocFailed==0 ){
133577 pParse->iSelfTab = iDataCur + 1;
133578 for(k=pCheck->nExpr-1; k>0; k--){
133579 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
133581 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
133584 pParse->iSelfTab = 0;
133586 pTab->zName);
133595 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133605 pIdx->nColumn); VdbeCoverage(v);
133610 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
133621 for(kk=0; kk<pIdx->nKeyCol; kk++){
133622 int iCol = pIdx->aiColumn[kk];
133623 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
133624 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
133632 pIdx->nKeyCol); VdbeCoverage(v);
133633 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
133642 sqlite3VdbeJumpHere(v, loopTop-1);
133645 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
133650 sqlite3VdbeLoadString(v, 4, pIdx->zName);
133673 aOp[0].p2 = 1-mxErr;
133679 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
133688 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
133693 ** The second form of this pragma is a no-op if the main database file
133714 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
133715 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
133716 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
133719 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
133729 returnSingleText(v, encnames[ENC(pParse->db)].zName);
133736 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
133737 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
133738 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
133739 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
133745 if( !pEnc->zName ){
133770 ** the value of the schema-version and user-version, respectively. Both
133771 ** the schema-version and the user-version are 32-bit signed integers
133774 ** The schema-cookie is usually only manipulated internally by SQLite. It
133781 ** the schema-version is potentially dangerous and may lead to program
133784 ** The user-version is not used internally by SQLite. It may be used by
133788 int iCookie = pPragma->iArg; /* Which cookie to read or write */
133790 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
133829 ** Return the names of all compile-time options used in this build,
133835 pParse->nMem = 1;
133852 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
133863 pParse->nMem = 3;
133882 db->xWalCallback==sqlite3WalDefaultHook ?
133883 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
133891 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
133933 ** ever added that should be off by default, those off-by-default
133943 ** (2) The query planner used sqlite_stat1-style statistics for one or
133971 iTabCur = pParse->nTab++;
133972 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
133975 pSchema = db->aDb[iDb].pSchema;
133976 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
133982 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
133985 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
133986 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
133987 if( !pIdx->hasStat1 ){
133999 db->aDb[iDb].zDbSName, pTab->zName);
134023 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
134027 returnSingleInt(v, db->busyTimeout);
134035 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
134037 ** specified and is a non-negative integer.
134038 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
134040 ** sqlite3_soft_heap_limit64(-1) C-language function.
134047 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
134058 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
134065 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
134068 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
134087 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
134101 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
134104 db->nAnalysisLimit = (int)(N&0x7fffffff);
134106 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
134119 pParse->nMem = 2;
134120 for(i=0; i<db->nDb; i++){
134124 if( db->aDb[i].zDbSName==0 ) continue;
134125 pBt = db->aDb[i].pBt;
134128 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
134132 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
134140 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
134149 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
134154 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
134205 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
134210 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
134214 if( pPragma->mPragFlg & PragFlg_Result1 ){
134218 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
134224 assert( strlen(zBuf) < sizeof(zBuf)-1 );
134232 pTab->pName = pPragma;
134233 pTab->db = db;
134234 pTab->iHidden = i;
134235 pTab->nHidden = j;
134267 pIdxInfo->estimatedCost = (double)1;
134268 if( pTab->nHidden==0 ){ return SQLITE_OK; }
134269 pConstraint = pIdxInfo->aConstraint;
134272 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
134273 if( pConstraint->usable==0 ) continue;
134274 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
134275 if( pConstraint->iColumn < pTab->iHidden ) continue;
134276 j = pConstraint->iColumn - pTab->iHidden;
134281 pIdxInfo->estimatedCost = (double)2147483647;
134282 pIdxInfo->estimatedRows = 2147483647;
134285 j = seen[0]-1;
134286 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
134287 pIdxInfo->aConstraintUsage[j].omit = 1;
134289 pIdxInfo->estimatedCost = (double)20;
134290 pIdxInfo->estimatedRows = 20;
134291 j = seen[1]-1;
134292 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
134293 pIdxInfo->aConstraintUsage[j].omit = 1;
134303 pCsr->base.pVtab = pVtab;
134304 *ppCursor = &pCsr->base;
134311 sqlite3_finalize(pCsr->pPragma);
134312 pCsr->pPragma = 0;
134313 for(i=0; i<ArraySize(pCsr->azArg); i++){
134314 sqlite3_free(pCsr->azArg[i]);
134315 pCsr->azArg[i] = 0;
134333 pCsr->iRowid++;
134334 assert( pCsr->pPragma );
134335 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
134336 rc = sqlite3_finalize(pCsr->pPragma);
134337 pCsr->pPragma = 0;
134352 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134361 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
134364 assert( j<ArraySize(pCsr->azArg) );
134365 assert( pCsr->azArg[j]==0 );
134367 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
134368 if( pCsr->azArg[j]==0 ){
134373 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
134375 if( pCsr->azArg[1] ){
134376 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
134378 sqlite3_str_appendall(&acc, pTab->pName->zName);
134379 if( pCsr->azArg[0] ){
134380 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
134384 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
134387 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
134398 return (pCsr->pPragma==0);
134410 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
134411 if( i<pTab->iHidden ){
134412 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
134414 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
134424 *p = pCsr->iRowid;
134431 0, /* xCreate - create a table */
134432 pragmaVtabConnect, /* xConnect - connect to an existing table */
134433 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
134434 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
134435 0, /* xDestroy - Drop a table */
134436 pragmaVtabOpen, /* xOpen - open a cursor */
134437 pragmaVtabClose, /* xClose - close a cursor */
134438 pragmaVtabFilter, /* xFilter - configure scan constraints */
134439 pragmaVtabNext, /* xNext - advance a cursor */
134441 pragmaVtabColumn, /* xColumn - read data */
134442 pragmaVtabRowid, /* xRowid - read data */
134443 0, /* xUpdate - write data */
134444 0, /* xBegin - begin transaction */
134445 0, /* xSync - sync transaction */
134446 0, /* xCommit - commit transaction */
134447 0, /* xRollback - rollback transaction */
134448 0, /* xFindFunction - function overloading */
134449 0, /* xRename - rename the table */
134466 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
134467 assert( sqlite3HashFind(&db->aModule, zName)==0 );
134495 ** Fill the InitData structure with an error message that indicates
134503 sqlite3 *db = pData->db;
134504 if( db->mallocFailed ){
134505 pData->rc = SQLITE_NOMEM_BKPT;
134506 }else if( pData->pzErrMsg[0]!=0 ){
134507 /* A error message has already been generated. Do not overwrite it */
134508 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
134514 *pData->pzErrMsg = sqlite3MPrintf(db,
134516 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
134519 pData->rc = SQLITE_ERROR;
134520 }else if( db->flags & SQLITE_WriteSchema ){
134521 pData->rc = SQLITE_CORRUPT_BKPT;
134526 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
134527 *pData->pzErrMsg = z;
134528 pData->rc = SQLITE_CORRUPT_BKPT;
134539 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
134540 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
134548 const char *zSql, /* UTF-8 encoded SQL statement. */
134573 sqlite3 *db = pData->db;
134574 int iDb = pData->iDb;
134578 assert( sqlite3_mutex_held(db->mutex) );
134579 db->mDbFlags |= DBFLAG_EncodingFixed;
134581 pData->nInitRow++;
134582 if( db->mallocFailed ){
134587 assert( iDb>=0 && iDb<db->nDb );
134594 ** But because db->init.busy is set to 1, no VDBE code is generated
134604 u8 saved_iDb = db->init.iDb;
134608 assert( db->init.busy );
134609 db->init.iDb = iDb;
134610 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
134611 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
134617 db->init.orphanTrigger = 0;
134618 db->init.azInit = (const char**)argv;
134620 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
134621 rc = db->errCode;
134623 db->init.iDb = saved_iDb;
134624 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
134626 if( db->init.orphanTrigger ){
134629 if( rc > pData->rc ) pData->rc = rc;
134637 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
134649 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
134653 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
134654 || pIndex->tnum<2
134655 || pIndex->tnum>pData->mxPage
134686 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
134688 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
134689 assert( iDb>=0 && iDb<db->nDb );
134690 assert( db->aDb[iDb].pSchema );
134691 assert( sqlite3_mutex_held(db->mutex) );
134692 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
134694 db->init.busy = 1;
134696 /* Construct the in-memory representation schema tables (sqlite_schema or
134700 ** the schema table as read-only. */
134716 db->mDbFlags &= mask;
134724 pDb = &db->aDb[iDb];
134725 if( pDb->pBt==0 ){
134732 /* If there is not already a read-only (or read-write) transaction opened
134733 ** on the b-tree database, open one now. If a transaction is opened, it
134735 sqlite3BtreeEnter(pDb->pBt);
134736 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
134737 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
134752 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
134763 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
134765 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
134768 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
134770 /* If opening a non-empty database, check the text encoding. For the
134775 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
134776 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
134780 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
134788 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
134796 pDb->pSchema->enc = ENC(db);
134798 if( pDb->pSchema->cache_size==0 ){
134800 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
134802 pDb->pSchema->cache_size = size;
134804 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
134806 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
134812 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
134815 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
134816 if( pDb->pSchema->file_format==0 ){
134817 pDb->pSchema->file_format = 1;
134819 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
134830 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
134831 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
134836 assert( db->init.busy );
134837 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
134842 db->aDb[iDb].zDbSName, zSchemaTabName);
134846 xAuth = db->xAuth;
134847 db->xAuth = 0;
134851 db->xAuth = xAuth;
134862 assert( pDb == &(db->aDb[iDb]) );
134863 if( db->mallocFailed ){
134866 pDb = &db->aDb[iDb];
134868 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
134889 sqlite3BtreeCommit(pDb->pBt);
134891 sqlite3BtreeLeave(pDb->pBt);
134900 db->init.busy = 0;
134905 ** Initialize all database files - the main database file, the file
134908 ** error occurs, write an error message into *pzErrMsg.
134915 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
134917 assert( sqlite3_mutex_held(db->mutex) );
134918 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
134919 assert( db->init.busy==0 );
134921 assert( db->nDb>0 );
134928 for(i=db->nDb-1; i>0; i--){
134929 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
134942 ** This routine is a no-op if the database schema is already initialized.
134947 sqlite3 *db = pParse->db;
134948 assert( sqlite3_mutex_held(db->mutex) );
134949 if( !db->init.busy ){
134950 rc = sqlite3Init(db, &pParse->zErrMsg);
134952 pParse->rc = rc;
134953 pParse->nErr++;
134954 }else if( db->noSharedCache ){
134955 db->mDbFlags |= DBFLAG_SchemaKnownOk;
134964 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
134965 ** make no changes to pParse->rc.
134968 sqlite3 *db = pParse->db;
134973 assert( pParse->checkSchema );
134974 assert( sqlite3_mutex_held(db->mutex) );
134975 for(iDb=0; iDb<db->nDb; iDb++){
134977 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
134980 /* If there is not already a read-only (or read-write) transaction opened
134981 ** on the b-tree database, open one now. If a transaction is opened, it
134982 ** will be closed immediately after reading the meta-value. */
134987 pParse->rc = SQLITE_NOMEM;
134994 ** value stored as part of the in-memory schema representation,
134998 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
135000 pParse->rc = SQLITE_SCHEMA;
135012 ** which database file in db->aDb[] the schema refers to.
135018 int i = -32768;
135020 /* If pSchema is NULL, then return -32768. This happens when code in
135022 ** created by a sub-select). In this case the return value of this
135025 ** We return -32768 instead of the more usual -1 simply because using
135026 ** -32768 as the incorrect index into db->aDb[] is much
135027 ** more likely to cause a segfault than -1 (of course there are assert()
135029 ** -32768 will still fit into a 16-bit signed integer.
135031 assert( sqlite3_mutex_held(db->mutex) );
135034 assert( i<db->nDb );
135035 if( db->aDb[i].pSchema==pSchema ){
135039 assert( i>=0 && i<db->nDb );
135048 sqlite3 *db = pParse->db;
135050 assert( db->pParse==pParse );
135051 assert( pParse->nested==0 );
135053 sqlite3DbFree(db, pParse->aTableLock);
135055 while( pParse->pCleanup ){
135056 ParseCleanup *pCleanup = pParse->pCleanup;
135057 pParse->pCleanup = pCleanup->pNext;
135058 pCleanup->xCleanup(db, pCleanup->pPtr);
135061 sqlite3DbFree(db, pParse->aLabel);
135062 if( pParse->pConstExpr ){
135063 sqlite3ExprListDelete(db, pParse->pConstExpr);
135065 assert( db->lookaside.bDisable >= pParse->disableLookaside );
135066 db->lookaside.bDisable -= pParse->disableLookaside;
135067 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
135068 assert( pParse->db->pParse==pParse );
135069 db->pParse = pParse->pOuterParse;
135070 pParse->db = 0;
135071 pParse->disableLookaside = 0;
135082 ** common cleanups, we save a single NULL-pointer comparison in
135087 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
135089 ** use-after-free errors following an OOM. The preferred way to do this is
135092 ** testcase( pParse->earlyCleanup );
135107 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
135109 pCleanup->pNext = pParse->pCleanup;
135110 pParse->pCleanup = pCleanup;
135111 pCleanup->pPtr = pPtr;
135112 pCleanup->xCleanup = xCleanup;
135114 xCleanup(pParse->db, pPtr);
135117 pParse->earlyCleanup = 1;
135135 assert( db->pParse!=pParse );
135136 pParse->pOuterParse = db->pParse;
135137 db->pParse = pParse;
135138 pParse->db = db;
135139 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
135151 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
135155 const char *zSql, /* UTF-8 encoded SQL statement. */
135169 sParse.pOuterParse = db->pParse;
135170 db->pParse = &sParse;
135174 if( db->mallocFailed ) sqlite3ErrorMsg(&sParse, "out of memory");
135175 assert( sqlite3_mutex_held(db->mutex) );
135177 /* For a long-term use prepared statement avoid the use of
135188 ** some other database connection is holding a write-lock, which in
135209 if( !db->noSharedCache ){
135210 for(i=0; i<db->nDb; i++) {
135211 Btree *pBt = db->aDb[i].pBt;
135216 const char *zDb = db->aDb[i].zDbSName;
135218 testcase( db->flags & SQLITE_ReadUncommit );
135227 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
135229 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
135240 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
135254 if( db->init.busy==0 ){
135255 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
135257 if( db->mallocFailed ){
135262 if( sParse.checkSchema && db->init.busy==0 ){
135287 sParse.pTriggerPrg = pT->pNext;
135298 const char *zSql, /* UTF-8 encoded SQL statement. */
135315 sqlite3_mutex_enter(db->mutex);
135323 if( rc==SQLITE_OK || db->mallocFailed ) break;
135325 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
135328 assert( (rc&db->errMask)==rc );
135329 db->busyHandler.nBusy = 0;
135330 sqlite3_mutex_leave(db->mutex);
135350 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
135354 assert( sqlite3_mutex_held(db->mutex) );
135356 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
135384 const char *zSql, /* UTF-8 encoded SQL statement. */
135396 const char *zSql, /* UTF-8 encoded SQL statement. */
135402 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
135414 const char *zSql, /* UTF-8 encoded SQL statement. */
135421 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
135438 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
135442 const void *zSql, /* UTF-16 encoded SQL statement. */
135448 /* This function currently works by first transforming the UTF-16
135449 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
135469 sqlite3_mutex_enter(db->mutex);
135472 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
135477 ** equivalent pointer into the UTF-16 string by counting the unicode
135479 ** the same number of characters into the UTF-16 string.
135481 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
135486 sqlite3_mutex_leave(db->mutex);
135500 const void *zSql, /* UTF-16 encoded SQL statement. */
135512 const void *zSql, /* UTF-16 encoded SQL statement. */
135524 const void *zSql, /* UTF-16 encoded SQL statement. */
135575 ** The aDefer[] array is used by the sorter-references optimization. For
135585 ** When the sorter-reference optimization is used, there is one entry in the
135594 int regReturn; /* Register holding block-output return address */
135595 int labelBkOut; /* Start label for the block-output subroutine */
135621 Select *pPrior = p->pPrior;
135622 sqlite3ExprListDelete(db, p->pEList);
135623 sqlite3SrcListDelete(db, p->pSrc);
135624 sqlite3ExprDelete(db, p->pWhere);
135625 sqlite3ExprListDelete(db, p->pGroupBy);
135626 sqlite3ExprDelete(db, p->pHaving);
135627 sqlite3ExprListDelete(db, p->pOrderBy);
135628 sqlite3ExprDelete(db, p->pLimit);
135629 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
135631 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
135632 sqlite3WindowListDelete(db, p->pWinDefn);
135634 while( p->pWin ){
135635 assert( p->pWin->ppThis==&p->pWin );
135636 sqlite3WindowUnlinkFromSelect(p->pWin);
135649 pDest->eDest = (u8)eDest;
135650 pDest->iSDParm = iParm;
135651 pDest->iSDParm2 = 0;
135652 pDest->zAffSdst = 0;
135653 pDest->iSdst = 0;
135654 pDest->nSdst = 0;
135665 SrcList *pSrc, /* the FROM clause -- which tables to scan */
135675 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
135677 assert( pParse->db->mallocFailed );
135682 sqlite3Expr(pParse->db,TK_ASTERISK,0));
135684 pNew->pEList = pEList;
135685 pNew->op = TK_SELECT;
135686 pNew->selFlags = selFlags;
135687 pNew->iLimit = 0;
135688 pNew->iOffset = 0;
135689 pNew->selId = ++pParse->nSelect;
135690 pNew->addrOpenEphm[0] = -1;
135691 pNew->addrOpenEphm[1] = -1;
135692 pNew->nSelectRow = 0;
135693 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
135694 pNew->pSrc = pSrc;
135695 pNew->pWhere = pWhere;
135696 pNew->pGroupBy = pGroupBy;
135697 pNew->pHaving = pHaving;
135698 pNew->pOrderBy = pOrderBy;
135699 pNew->pPrior = 0;
135700 pNew->pNext = 0;
135701 pNew->pLimit = pLimit;
135702 pNew->pWith = 0;
135704 pNew->pWin = 0;
135705 pNew->pWinDefn = 0;
135707 if( pParse->db->mallocFailed ) {
135708 clearSelect(pParse->db, pNew, pNew!=&standin);
135711 assert( pNew->pSrc!=0 || pParse->nErr>0 );
135725 ** Return a pointer to the right-most SELECT statement in a compound.
135728 while( p->pNext ) p = p->pNext;
135753 ** ------- ----- ----- ------------
135754 ** CROSS - - JT_CROSS
135755 ** INNER - - JT_INNER
135756 ** LEFT - - JT_LEFT|JT_OUTER
135757 ** LEFT OUTER - JT_LEFT|JT_OUTER
135758 ** RIGHT - - JT_RIGHT|JT_OUTER
135759 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
135760 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
135761 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
135762 ** NATURAL INNER - JT_NATURAL|JT_INNER
135763 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
135765 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
135767 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
135771 ** of other non-standard and in many cases non-sensical join types.
135776 ** INNER CROSS JOIN -> same as JOIN
135777 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
135778 ** OUTER LEFT JOIN -> same as LEFT JOIN
135779 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
135780 ** LEFT RIGHT JOIN -> same as FULL JOIN
135781 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
135782 ** CROSS CROSS CROSS JOIN -> same as JOIN
135821 if( p->n==aKeyword[j].nChar
135822 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
135850 ** Return the index of a column in a table. Return -1 if the column
135857 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
135858 if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i;
135860 return -1;
135868 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) );
135869 if( pItem->fg.isNestedFrom ){
135871 assert( pItem->pSelect!=0 );
135872 pResults = pItem->pSelect->pEList;
135874 assert( iCol>=0 && iCol<pResults->nExpr );
135875 pResults->a[iCol].fg.bUsed = 1;
135881 ** table that has a column named zCol. The search is left-to-right.
135891 int iStart, /* First member of pSrc->a[] to check */
135892 int iEnd, /* Last member of pSrc->a[] to check */
135894 int *piTab, /* Write index of pSrc->a[] here */
135895 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
135901 assert( iEnd<pSrc->nSrc );
135906 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
135908 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
135911 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
135953 p->w.iJoin = iTable;
135954 if( p->op==TK_FUNCTION ){
135956 if( p->x.pList ){
135958 for(i=0; i<p->x.pList->nExpr; i++){
135959 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
135963 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
135964 p = p->pRight;
135984 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
135988 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
135991 if( p->op==TK_FUNCTION ){
135993 if( p->x.pList ){
135995 for(i=0; i<p->x.pList->nExpr; i++){
135996 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
136000 unsetJoinExpr(p->pLeft, iTable, nullable);
136001 p = p->pRight;
136018 ** The left most table is the first entry in Select.pSrc. The right-most
136032 pSrc = p->pSrc;
136033 pLeft = &pSrc->a[0];
136035 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
136036 Table *pRightTab = pRight->pTab;
136039 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
136040 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
136045 if( pRight->fg.jointype & JT_NATURAL ){
136047 if( pRight->fg.isUsing || pRight->u3.pOn ){
136052 for(j=0; j<pRightTab->nCol; j++){
136055 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
136056 zName = pRightTab->aCol[j].zCnName;
136060 assert( pUsing->nId>0 );
136061 assert( pUsing->a[pUsing->nId-1].zName==0 );
136062 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
136067 pRight->fg.isUsing = 1;
136068 pRight->fg.isSynthUsing = 1;
136069 pRight->u3.pUsing = pUsing;
136071 if( pParse->nErr ) return 1;
136081 if( pRight->fg.isUsing ){
136082 IdList *pList = pRight->u3.pUsing;
136083 sqlite3 *db = pParse->db;
136085 for(j=0; j<pList->nId; j++){
136094 zName = pList->a[j].zName;
136098 pRight->fg.isSynthUsing)==0
136100 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
136105 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136106 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
136109 ** contains the zName column, then this branch is a no-op.
136116 ** non-USING references to zName on the left of an INNER or LEFT
136123 pRight->fg.isSynthUsing)!=0 ){
136124 if( pSrc->a[iLeft].fg.isUsing==0
136125 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
136133 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
136148 pEq->w.iJoin = pE2->iTable;
136150 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
136157 else if( pRight->u3.pOn ){
136158 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
136159 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
136160 pRight->u3.pOn = 0;
136161 pRight->fg.isOn = 1;
136190 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
136191 0, pInfo->ecelFlags);
136193 if( pInfo->pExtra ){
136194 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
136195 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
136213 int nOBSat = pSort->nOBSat;
136214 Vdbe *v = pParse->pVdbe;
136215 int regOut = ++pParse->nMem;
136216 if( pSort->pDeferredRowLoad ){
136217 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
136219 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
136225 ** through regData+nData-1 onto the sorter.
136236 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
136237 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
136238 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
136242 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
136266 regBase = regData - nPrefixReg;
136268 regBase = pParse->nMem + 1;
136269 pParse->nMem += nBase;
136271 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
136272 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
136273 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
136274 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
136277 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
136291 regPrevKey = pParse->nMem+1;
136292 pParse->nMem += pSort->nOBSat;
136293 nKey = nExpr - pSort->nOBSat + bSeq;
136297 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
136300 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
136301 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136302 if( pParse->db->mallocFailed ) return;
136303 pOp->p2 = nKey + nData;
136304 pKI = pOp->p4.pKeyInfo;
136305 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
136306 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
136307 testcase( pKI->nAllField > pKI->nKeyField+2 );
136308 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
136309 pKI->nAllField-pKI->nKeyField-1);
136313 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
136314 pSort->regReturn = ++pParse->nMem;
136315 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
136316 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
136318 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
136322 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
136336 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
136341 int iCsr = pSort->iECursor;
136346 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
136353 if( pSort->sortFlags & SORTFLAG_UseSorter ){
136358 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
136359 regBase+nOBSat, nBase-nOBSat);
136362 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
136432 int nResultCol = pEList->nExpr;
136433 Vdbe *v = pParse->pVdbe;
136442 iRet = regPrev = pParse->nMem+1;
136443 pParse->nMem += nResultCol;
136447 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
136448 if( i<nResultCol-1 ){
136455 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
136458 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
136459 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
136494 ** No adjustments necessary. This function is a no-op.
136513 if( pParse->nErr==0
136516 Vdbe *v = pParse->pVdbe;
136518 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
136527 pOp->opcode = OP_Null;
136528 pOp->p1 = 1;
136529 pOp->p2 = iVal;
136536 ** This function is called as part of inner-loop generation for a SELECT
136538 ** determines the expressions, if any, that the sorter-reference
136539 ** optimization should be used for. The sorter-reference optimization
136552 ** for which the sorter-reference optimization should be enabled.
136553 ** Additionally, the pSort->aDefer[] array is populated with entries
136568 for(i=0; i<pEList->nExpr; i++){
136569 struct ExprList_item *pItem = &pEList->a[i];
136570 if( pItem->u.x.iOrderByCol==0 ){
136571 Expr *pExpr = pItem->pExpr;
136573 if( pExpr->op==TK_COLUMN
136574 && pExpr->iColumn>=0
136576 && (pTab = pExpr->y.pTab)!=0
136578 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
136582 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
136585 if( nDefer==ArraySize(pSort->aDefer) ){
136593 nKey = pPk->nKeyCol;
136598 pNew->iTable = pExpr->iTable;
136600 pNew->y.pTab = pExpr->y.pTab;
136601 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
136605 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
136606 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
136607 pSort->aDefer[nDefer].nKey = nKey;
136611 pItem->fg.bSorterRef = 1;
136615 pSort->nDefer = (u8)nDefer;
136624 ** If srcTab is negative, then the p->pEList expressions
136626 ** zero or more, then data is pulled from srcTab and p->pEList is used only
136632 int srcTab, /* Pull data from this table if non-negative */
136639 Vdbe *v = pParse->pVdbe;
136642 int eDest = pDest->eDest; /* How to dispose of results */
136643 int iParm = pDest->iSDParm; /* First argument to disposal method */
136651 ** values for any expressions that are also part of the sort-key are omitted
136657 assert( p->pEList!=0 );
136658 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
136659 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
136662 codeOffset(v, p->iOffset, iContinue);
136667 nResultCol = p->pEList->nExpr;
136669 if( pDest->iSdst==0 ){
136671 nPrefixReg = pSort->pOrderBy->nExpr;
136672 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
136673 pParse->nMem += nPrefixReg;
136675 pDest->iSdst = pParse->nMem+1;
136676 pParse->nMem += nResultCol;
136677 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
136679 ** on the right-hand side of an INSERT contains more result columns than
136683 pParse->nMem += nResultCol;
136685 pDest->nSdst = nResultCol;
136686 regOrig = regResult = pDest->iSdst;
136690 VdbeComment((v, "%s", p->pEList->a[i].zEName));
136707 /* For each expression in p->pEList that is a copy of an expression in
136708 ** the ORDER BY clause (pSort->pOrderBy), set the associated
136710 ** expression within the sort-key that pushOntoSorter() will generate.
136711 ** This allows the p->pEList field to be omitted from the sorted record,
136715 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
136717 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
136718 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
136722 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
136723 if( pExtra && pParse->db->mallocFailed==0 ){
136729 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
136730 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
136731 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
136732 pParse->nMem += pExtra->nExpr;
136738 pEList = p->pEList;
136739 for(i=0; i<pEList->nExpr; i++){
136740 if( pEList->a[i].u.x.iOrderByCol>0
136742 || pEList->a[i].fg.bSorterRef
136745 nResultCol--;
136764 if( pExtra ) nResultCol += pExtra->nExpr;
136766 if( p->iLimit
136772 pSort->pDeferredRowLoad = &sRowLoadInfo;
136784 int eType = pDistinct->eTnctType;
136785 int iTab = pDistinct->tabTnct;
136786 assert( nResultCol==p->pEList->nExpr );
136787 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
136788 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
136790 codeOffset(v, p->iOffset, iContinue);
136863 int i2 = pDest->iSDParm2;
136872 regResult+(i2<0), nResultCol-(i2<0), r1);
136897 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
136899 r1, pDest->zAffSdst, nResultCol);
136921 assert( nResultCol<=pDest->nSdst );
136925 assert( nResultCol==pDest->nSdst );
136933 case SRT_Coroutine: /* Send data to a co-routine */
136941 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
136950 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
136951 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
136952 ** pSO->nExpr columns, then make sure all keys are unique by adding a
136961 pSO = pDest->pOrderBy;
136963 nKey = pSO->nExpr;
136982 regResult + pSO->a[i].u.x.iOrderByCol - 1,
137000 ** user-defined functions that have side effects. We do not care
137014 if( pSort==0 && p->iLimit ){
137015 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
137024 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
137027 p->aSortFlags = (u8*)&p->aColl[N+X];
137028 p->nKeyField = (u16)N;
137029 p->nAllField = (u16)(N+X);
137030 p->enc = ENC(db);
137031 p->db = db;
137032 p->nRef = 1;
137045 assert( p->nRef>0 );
137046 p->nRef--;
137047 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
137056 assert( p->nRef>0 );
137057 p->nRef++;
137069 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
137095 sqlite3 *db = pParse->db;
137098 nExpr = pList->nExpr;
137099 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
137102 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
137103 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
137104 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
137127 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
137130 ** "USE TEMP B-TREE FOR xxx"
137136 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
137140 ** Assign expression b to lvalue a. A second, no-op, version of this macro
137149 /* No-op versions of the explainXXX() functions and macros. */
137156 ** If the inner loop was generated using a non-null pOrderBy argument,
137168 Vdbe *v = pParse->pVdbe; /* The prepared statement */
137169 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
137174 ExprList *pOrderBy = pSort->pOrderBy;
137175 int eDest = pDest->eDest;
137176 int iParm = pDest->iSDParm;
137185 struct ExprList_item *aOutEx = p->pEList->a;
137188 if( pSort->labelBkOut ){
137189 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
137191 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
137195 /* Open any cursors needed for sorter-reference expressions */
137196 for(i=0; i<pSort->nDefer; i++){
137197 Table *pTab = pSort->aDefer[i].pTab;
137198 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137199 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
137200 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
137204 iTab = pSort->iECursor;
137206 if( eDest==SRT_Mem && p->iOffset ){
137207 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
137210 regRow = pDest->iSdst;
137220 nKey = pOrderBy->nExpr - pSort->nOBSat;
137221 if( pSort->sortFlags & SORTFLAG_UseSorter ){
137222 int regSortOut = ++pParse->nMem;
137223 iSortTab = pParse->nTab++;
137224 if( pSort->labelBkOut ){
137232 codeOffset(v, p->iOffset, addrContinue);
137237 codeOffset(v, p->iOffset, addrContinue);
137241 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
137248 if( pSort->nDefer ){
137252 for(i=0; i<pSort->nDefer; i++){
137253 int iCsr = pSort->aDefer[i].iCsr;
137254 Table *pTab = pSort->aDefer[i].pTab;
137255 int nKey = pSort->aDefer[i].nKey;
137265 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
137278 for(i=nColumn-1; i>=0; i--){
137287 iRead = aOutEx[i].u.x.iOrderByCol-1;
137289 iRead = iCol--;
137306 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
137308 pDest->zAffSdst, nColumn);
137318 int i2 = pDest->iSDParm2;
137320 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
137333 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
137335 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
137351 if( pSort->sortFlags & SORTFLAG_UseSorter ){
137356 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
137371 ** result-set expression in all of the following SELECT statements is
137382 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
137409 assert( pNC->pSrcList!=0 );
137410 switch( pExpr->op ){
137418 int iCol = pExpr->iColumn; /* Index of column in pTab */
137420 SrcList *pTabList = pNC->pSrcList;
137421 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
137422 if( j<pTabList->nSrc ){
137423 pTab = pTabList->a[j].pTab;
137424 pS = pTabList->a[j].pSelect;
137426 pNC = pNC->pNext;
137441 ** sub-select. In this case, set the column type to NULL, even
137451 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
137453 /* The "table" is actually a sub-select or a view in the FROM clause
137455 ** data for the result-set column of the sub-select.
137457 if( iCol<pS->pEList->nExpr
137465 ** rowid of the sub-select or view. This expression is legal (see
137466 ** test case misc2.2.2) - it always evaluates to NULL.
137469 Expr *p = pS->pEList->a[iCol].pExpr;
137470 sNC.pSrcList = pS->pSrc;
137472 sNC.pParse = pNC->pParse;
137479 if( iCol<0 ) iCol = pTab->iPKey;
137480 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
137485 zOrigCol = pTab->aCol[iCol].zCnName;
137486 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137488 zOrigTab = pTab->zName;
137489 if( pNC->pParse && pTab->pSchema ){
137490 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
137491 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
137494 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
137498 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
137506 /* The expression is a sub-select. Return the declaration type and
137514 pS = pExpr->x.pSelect;
137515 p = pS->pEList->a[0].pExpr;
137516 sNC.pSrcList = pS->pSrc;
137518 sNC.pParse = pNC->pParse;
137546 Vdbe *v = pParse->pVdbe;
137552 for(i=0; i<pEList->nExpr; i++){
137553 Expr *p = pEList->a[i].pExpr;
137561 /* The vdbe must make its own copy of the column-type and other
137611 Vdbe *v = pParse->pVdbe;
137616 sqlite3 *db = pParse->db;
137622 if( pParse->explain ){
137627 if( pParse->colNamesSet ) return;
137628 /* Column names are determined by the left-most term of a compound select */
137629 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137631 pTabList = pSelect->pSrc;
137632 pEList = pSelect->pEList;
137635 pParse->colNamesSet = 1;
137636 fullName = (db->flags & SQLITE_FullColNames)!=0;
137637 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
137638 sqlite3VdbeSetNumCols(v, pEList->nExpr);
137639 for(i=0; i<pEList->nExpr; i++){
137640 Expr *p = pEList->a[i].pExpr;
137643 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
137644 assert( p->op!=TK_COLUMN
137645 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
137646 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
137648 char *zName = pEList->a[i].zEName;
137650 }else if( srcName && p->op==TK_COLUMN ){
137652 int iCol = p->iColumn;
137653 pTab = p->y.pTab;
137655 if( iCol<0 ) iCol = pTab->iPKey;
137656 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
137660 zCol = pTab->aCol[iCol].zCnName;
137664 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
137670 const char *z = pEList->a[i].zEName;
137706 sqlite3 *db = pParse->db; /* Database connection */
137718 nCol = pEList->nExpr;
137730 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
137731 struct ExprList_item *pX = &pEList->a[i];
137735 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
137738 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
137739 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
137740 pColExpr = pColExpr->pRight;
137743 if( pColExpr->op==TK_COLUMN
137745 && ALWAYS( pColExpr->y.pTab!=0 )
137748 int iCol = pColExpr->iColumn;
137749 pTab = pColExpr->y.pTab;
137750 if( iCol<0 ) iCol = pTab->iPKey;
137751 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
137752 }else if( pColExpr->op==TK_ID ){
137754 zName = pColExpr->u.zToken;
137757 assert( zName==pX->zEName ); /* pointer comparison intended */
137771 if( pCollide->fg.bUsingTerm ){
137772 pCol->colFlags |= COLFLAG_NOEXPAND;
137776 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
137782 pCol->zCnName = zName;
137783 pCol->hName = sqlite3StrIHash(zName);
137784 if( pX->fg.bNoExpand ){
137785 pCol->colFlags |= COLFLAG_NOEXPAND;
137793 if( db->mallocFailed ){
137822 sqlite3 *db = pParse->db;
137831 assert( (pSelect->selFlags & SF_Resolved)!=0 );
137832 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
137833 if( db->mallocFailed ) return;
137835 sNC.pSrcList = pSelect->pSrc;
137836 a = pSelect->pEList->a;
137837 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
137840 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
137843 /* pCol->szEst = ... // Column size est for SELECT tables never used */
137844 pCol->affinity = sqlite3ExprAffinity(p);
137847 n = sqlite3Strlen30(pCol->zCnName);
137848 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2);
137849 if( pCol->zCnName ){
137850 memcpy(&pCol->zCnName[n+1], zType, m+1);
137851 pCol->colFlags |= COLFLAG_HASTYPE;
137853 testcase( pCol->colFlags & COLFLAG_HASTYPE );
137854 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
137857 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
137860 assert( pTab->pIndex==0 );
137861 sqlite3ColumnSetColl(db, pCol, pColl->zName);
137864 pTab->szTabRow = 1; /* Any non-zero value works */
137873 sqlite3 *db = pParse->db;
137876 savedFlags = db->flags;
137877 db->flags &= ~(u64)SQLITE_FullColNames;
137878 db->flags |= SQLITE_ShortColNames;
137880 db->flags = savedFlags;
137881 if( pParse->nErr ) return 0;
137882 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
137887 pTab->nTabRef = 1;
137888 pTab->zName = 0;
137889 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
137890 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
137892 pTab->iPKey = -1;
137893 if( db->mallocFailed ){
137902 ** If an error occurs, return NULL and leave a message in pParse.
137905 if( pParse->pVdbe ){
137906 return pParse->pVdbe;
137908 if( pParse->pToplevel==0
137909 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
137911 pParse->okConstFactor = 1;
137919 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
137927 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
137935 ** Only if pLimit->pLeft!=0 do the limit registers get
137945 Expr *pLimit = p->pLimit;
137947 if( p->iLimit ) return;
137950 ** "LIMIT -1" always shows all rows. There is some
137956 assert( pLimit->op==TK_LIMIT );
137957 assert( pLimit->pLeft!=0 );
137958 p->iLimit = iLimit = ++pParse->nMem;
137961 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
137966 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
137967 p->nSelectRow = sqlite3LogEst((u64)n);
137968 p->selFlags |= SF_FixedLimit;
137971 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
137976 if( pLimit->pRight ){
137977 p->iOffset = iOffset = ++pParse->nMem;
137978 pParse->nMem++; /* Allocate an extra register for limit+offset */
137979 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
137990 ** Return the appropriate collating sequence for the iCol-th column of
137991 ** the result set for the compound-select statement "p". Return NULL if
137995 ** left-most term of the select that has a collating sequence.
137999 if( p->pPrior ){
138000 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
138005 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
138008 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
138009 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
138024 ExprList *pOrderBy = p->pOrderBy;
138025 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
138026 sqlite3 *db = pParse->db;
138031 struct ExprList_item *pItem = &pOrderBy->a[i];
138032 Expr *pTerm = pItem->pExpr;
138035 if( pTerm->flags & EP_Collate ){
138038 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
138039 if( pColl==0 ) pColl = db->pDfltColl;
138040 pOrderBy->a[i].pExpr =
138041 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
138044 pRet->aColl[i] = pColl;
138045 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
138057 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
138059 ** p->pPrior p
138062 ** There is exactly one reference to the recursive-table in the FROM clause
138063 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
138065 ** The setup-query runs once to generate an initial set of rows that go
138069 ** recursive-table for a recursive-query run. The output of the recursive-query
138094 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
138095 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
138096 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
138098 Select *pFirstRec; /* Left-most recursive term */
138114 if( p->pWin ){
138125 p->nSelectRow = 320; /* 4 billion rows */
138127 pLimit = p->pLimit;
138128 regLimit = p->iLimit;
138129 regOffset = p->iOffset;
138130 p->pLimit = 0;
138131 p->iLimit = p->iOffset = 0;
138132 pOrderBy = p->pOrderBy;
138135 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
138136 if( pSrc->a[i].fg.isRecursive ){
138137 iCurrent = pSrc->a[i].iCursor;
138145 iQueue = pParse->nTab++;
138146 if( p->op==TK_UNION ){
138148 iDistinct = pParse->nTab++;
138155 regCurrent = ++pParse->nMem;
138159 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
138167 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
138168 p->selFlags |= SF_UsesEphemeral;
138172 p->pOrderBy = 0;
138178 ** iDistinct table. pFirstRec is left pointing to the left-most
138181 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
138182 if( pFirstRec->selFlags & SF_Aggregate ){
138186 pFirstRec->op = TK_ALL;
138187 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
138190 /* Store the results of the setup-query in Queue. */
138191 pSetup = pFirstRec->pPrior;
138192 pSetup->pNext = 0;
138195 pSetup->pNext = p;
138204 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
138222 ** the value for the recursive-table. Store the results in the Queue.
138224 pFirstRec->pPrior = 0;
138227 assert( pFirstRec->pPrior==0 );
138228 pFirstRec->pPrior = pSetup;
138235 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
138236 p->pOrderBy = pOrderBy;
138237 p->pLimit = pLimit;
138245 Select *p, /* The right-most of SELECTs to be coded */
138250 ** Handle the special case of a compound-select that originates from a
138263 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
138267 Select *p, /* The right-most of SELECTs to be coded */
138272 int bShowAll = p->pLimit==0;
138273 assert( p->selFlags & SF_MultiValue );
138275 assert( p->selFlags & SF_Values );
138276 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
138277 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
138279 if( p->pWin ) return -1;
138281 if( p->pPrior==0 ) break;
138282 assert( p->pPrior->pNext==p );
138283 p = p->pPrior;
138289 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
138291 p->nSelectRow = nRow;
138292 p = p->pNext;
138303 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
138312 ** "p" points to the right-most of the two queries. the query on the
138313 ** left is p->pPrior. The left query could also be a compound query
138319 ** Example 1: Consider a three-way compound SQL statement.
138327 ** `-----> SELECT b FROM t2
138329 ** `------> SELECT a FROM t1
138333 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
138340 Select *p, /* The right-most of SELECTs to be coded */
138351 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
138353 assert( p && p->pPrior ); /* Calling function guarantees this much */
138354 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
138355 assert( p->selFlags & SF_Compound );
138356 db = pParse->db;
138357 pPrior = p->pPrior;
138359 assert( pPrior->pOrderBy==0 );
138360 assert( pPrior->pLimit==0 );
138368 assert( p->pEList );
138369 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
138373 /* Special handling for a compound-select that originates as a VALUES clause.
138375 if( p->selFlags & SF_MultiValue ){
138384 assert( p->pEList && pPrior->pEList );
138385 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
138388 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
138395 if( p->pOrderBy ){
138400 if( pPrior->pPrior==0 ){
138402 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
138408 switch( p->op ){
138412 assert( !pPrior->pLimit );
138413 pPrior->iLimit = p->iLimit;
138414 pPrior->iOffset = p->iOffset;
138415 pPrior->pLimit = p->pLimit;
138418 pPrior->pLimit = 0;
138422 p->pPrior = 0;
138423 p->iLimit = pPrior->iLimit;
138424 p->iOffset = pPrior->iOffset;
138425 if( p->iLimit ){
138426 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
138428 if( p->iOffset ){
138430 p->iLimit, p->iOffset+1, p->iOffset);
138437 pDelete = p->pPrior;
138438 p->pPrior = pPrior;
138439 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138440 if( p->pLimit
138441 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
138442 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
138444 p->nSelectRow = sqlite3LogEst((u64)nLimit);
138456 Expr *pLimit; /* Saved values of p->nLimit */
138460 testcase( p->op==TK_EXCEPT );
138461 testcase( p->op==TK_UNION );
138467 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
138473 unionTab = pParse->nTab++;
138474 assert( p->pOrderBy==0 );
138476 assert( p->addrOpenEphm[0] == -1 );
138477 p->addrOpenEphm[0] = addr;
138478 findRightmost(p)->selFlags |= SF_UsesEphemeral;
138479 assert( p->pEList );
138485 assert( !pPrior->pOrderBy );
138495 if( p->op==TK_EXCEPT ){
138498 assert( p->op==TK_UNION );
138501 p->pPrior = 0;
138502 pLimit = p->pLimit;
138503 p->pLimit = 0;
138505 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
138506 sqlite3SelectOpName(p->op)));
138510 assert( p->pOrderBy==0 );
138511 pDelete = p->pPrior;
138512 p->pPrior = pPrior;
138513 p->pOrderBy = 0;
138514 if( p->op==TK_UNION ){
138515 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
138517 sqlite3ExprDelete(db, p->pLimit);
138518 p->pLimit = pLimit;
138519 p->iLimit = 0;
138520 p->iOffset = 0;
138526 assert( p->pEList || db->mallocFailed );
138527 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
138543 default: assert( p->op==TK_INTERSECT ); {
138555 tab1 = pParse->nTab++;
138556 tab2 = pParse->nTab++;
138557 assert( p->pOrderBy==0 );
138560 assert( p->addrOpenEphm[0] == -1 );
138561 p->addrOpenEphm[0] = addr;
138562 findRightmost(p)->selFlags |= SF_UsesEphemeral;
138563 assert( p->pEList );
138577 assert( p->addrOpenEphm[1] == -1 );
138578 p->addrOpenEphm[1] = addr;
138579 p->pPrior = 0;
138580 pLimit = p->pLimit;
138581 p->pLimit = 0;
138583 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
138584 sqlite3SelectOpName(p->op)));
138588 pDelete = p->pPrior;
138589 p->pPrior = pPrior;
138590 if( p->nSelectRow>pPrior->nSelectRow ){
138591 p->nSelectRow = pPrior->nSelectRow;
138593 sqlite3ExprDelete(db, p->pLimit);
138594 p->pLimit = pLimit;
138600 assert( p->pEList );
138622 if( p->pNext==0 ){
138627 if( pParse->nErr ) goto multi_select_end;
138633 ** This section is run by the right-most SELECT statement only.
138634 ** SELECT statements to the left always skip this part. The right-most
138638 if( p->selFlags & SF_UsesEphemeral ){
138642 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
138645 assert( p->pNext==0 );
138646 assert( p->pEList!=0 );
138647 nCol = p->pEList->nExpr;
138653 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
138656 *apColl = db->pDfltColl;
138660 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
138662 int addr = pLoop->addrOpenEphm[i];
138666 assert( pLoop->addrOpenEphm[1]<0 );
138672 pLoop->addrOpenEphm[i] = -1;
138679 pDest->iSdst = dest.iSdst;
138680 pDest->nSdst = dest.nSdst;
138691 ** Error message for when two or more terms of a compound select have different
138695 if( p->selFlags & SF_Values ){
138700 sqlite3SelectOpName(p->op));
138708 ** The data to be output is contained in pIn->iSdst. There are
138709 ** pIn->nSdst columns to be output. pDest is where the output should
138721 ** If the LIMIT found in p->iLimit is reached, jump immediately to
138734 Vdbe *v = pParse->pVdbe;
138746 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
138750 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
138753 if( pParse->db->mallocFailed ) return 0;
138757 codeOffset(v, p->iOffset, iContinue);
138759 assert( pDest->eDest!=SRT_Exists );
138760 assert( pDest->eDest!=SRT_Table );
138761 switch( pDest->eDest ){
138767 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
138768 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
138769 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
138781 testcase( pIn->nSdst>1 );
138783 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
138784 r1, pDest->zAffSdst, pIn->nSdst);
138785 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
138786 pIn->iSdst, pIn->nSdst);
138794 ** if it is the RHS of a row-value IN operator.
138797 testcase( pIn->nSdst>1 );
138798 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
138805 ** starting at pDest->iSdst. Then the co-routine yields.
138808 if( pDest->iSdst==0 ){
138809 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
138810 pDest->nSdst = pIn->nSdst;
138812 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
138813 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
138826 assert( pDest->eDest==SRT_Output );
138827 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
138834 if( p->iLimit ){
138835 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
138856 ** co-routines. Then run the co-routines in parallel and merge the results
138883 ** ------------- ----------------- -------------- -----------------
138934 Select *p, /* The right-most of SELECTs to be coded */
138939 Select *pSplit; /* Left-most SELECT in the right-hand group */
138944 int regAddrA; /* Address register for select-A coroutine */
138945 int regAddrB; /* Address register for select-B coroutine */
138946 int addrSelectA; /* Address of the select-A coroutine */
138947 int addrSelectB; /* Address of the select-B coroutine */
138948 int regOutA; /* Address register for the output-A subroutine */
138949 int regOutB; /* Address register for the output-B subroutine */
138950 int addrOutA; /* Address of the output-A subroutine */
138951 int addrOutB = 0; /* Address of the output-B subroutine */
138952 int addrEofA; /* Address of the select-A-exhausted subroutine */
138954 int addrEofB; /* Address of the select-B-exhausted subroutine */
138958 int regLimitA; /* Limit register for select-A */
138959 int regLimitB; /* Limit register for select-A */
138961 int savedLimit; /* Saved value of p->iLimit */
138962 int savedOffset; /* Saved value of p->iOffset */
138974 assert( p->pOrderBy!=0 );
138976 db = pParse->db;
138977 v = pParse->pVdbe;
138985 op = p->op;
138986 assert( p->pPrior->pOrderBy==0 );
138987 pOrderBy = p->pOrderBy;
138989 nOrderBy = pOrderBy->nExpr;
138996 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
138998 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
139000 assert( pItem->u.x.iOrderByCol>0 );
139001 if( pItem->u.x.iOrderByCol==i ) break;
139006 pNew->flags |= EP_IntValue;
139007 pNew->u.iValue = i;
139008 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
139009 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
139025 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
139027 assert( pItem->u.x.iOrderByCol>0 );
139028 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
139029 aPermute[i] = pItem->u.x.iOrderByCol - 1;
139043 int nExpr = p->pEList->nExpr;
139044 assert( nOrderBy>=nExpr || db->mallocFailed );
139045 regPrev = pParse->nMem+1;
139046 pParse->nMem += nExpr+1;
139052 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
139053 pKeyDup->aSortFlags[i] = 0;
139064 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
139066 assert( pSplit->pPrior->pNext==pSplit );
139073 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
139075 pPrior = pSplit->pPrior;
139077 pSplit->pPrior = 0;
139078 pPrior->pNext = 0;
139079 assert( p->pOrderBy == pOrderBy );
139080 assert( pOrderBy!=0 || db->mallocFailed );
139081 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
139082 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
139083 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
139087 if( p->iLimit && op==TK_ALL ){
139088 regLimitA = ++pParse->nMem;
139089 regLimitB = ++pParse->nMem;
139090 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
139096 sqlite3ExprDelete(db, p->pLimit);
139097 p->pLimit = 0;
139099 regAddrA = ++pParse->nMem;
139100 regAddrB = ++pParse->nMem;
139101 regOutA = ++pParse->nMem;
139102 regOutB = ++pParse->nMem;
139106 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
139109 ** left of the compound operator - the "A" select.
139114 pPrior->iLimit = regLimitA;
139121 ** the right - the "B" select
139126 savedLimit = p->iLimit;
139127 savedOffset = p->iOffset;
139128 p->iLimit = regLimitB;
139129 p->iOffset = 0;
139132 p->iLimit = savedLimit;
139133 p->iOffset = savedOffset;
139161 VdbeNoopComment((v, "eof-A subroutine"));
139166 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
139174 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
139176 VdbeNoopComment((v, "eof-B subroutine"));
139184 VdbeNoopComment((v, "A-lt-B subroutine"));
139197 VdbeNoopComment((v, "A-eq-B subroutine"));
139205 VdbeNoopComment((v, "A-gt-B subroutine"));
139234 if( pSplit->pPrior ){
139235 sqlite3SelectDelete(db, pSplit->pPrior);
139237 pSplit->pPrior = pPrior;
139238 pPrior->pNext = pSplit;
139239 sqlite3ExprListDelete(db, pPrior->pOrderBy);
139240 pPrior->pOrderBy = 0;
139245 return pParse->nErr!=0;
139260 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
139283 ** when processing a non-matched row of the left.
139299 ** a column in table number iTable with a copy of the iColumn-th
139316 && pExpr->w.iJoin==pSubst->iTable
139319 pExpr->w.iJoin = pSubst->iNewTable;
139321 if( pExpr->op==TK_COLUMN
139322 && pExpr->iTable==pSubst->iTable
139326 if( pExpr->iColumn<0 ){
139327 pExpr->op = TK_NULL;
139332 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
139334 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
139335 assert( pExpr->pRight==0 );
139337 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
139339 sqlite3 *db = pSubst->pParse->db;
139340 if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){
139344 ifNullRow.iTable = pSubst->iNewTable;
139350 if( db->mallocFailed ){
139354 if( pSubst->isOuterJoin ){
139358 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
139359 pExpr->flags & (EP_OuterON|EP_InnerON));
139363 if( pExpr->op==TK_TRUEFALSE ){
139364 pExpr->u.iValue = sqlite3ExprTruthValue(pExpr);
139365 pExpr->op = TK_INTEGER;
139370 ** just as it did when it was a column of a view or sub-query. */
139371 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
139372 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
139373 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
139374 (pColl ? pColl->zName : "BINARY")
139381 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
139382 pExpr->iTable = pSubst->iNewTable;
139384 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
139385 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
139387 substSelect(pSubst, pExpr->x.pSelect, 1);
139389 substExprList(pSubst, pExpr->x.pList);
139393 Window *pWin = pExpr->y.pWin;
139394 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
139395 substExprList(pSubst, pWin->pPartition);
139396 substExprList(pSubst, pWin->pOrderBy);
139408 for(i=0; i<pList->nExpr; i++){
139409 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
139415 int doPrior /* Do substitutes on p->pPrior too */
139422 substExprList(pSubst, p->pEList);
139423 substExprList(pSubst, p->pGroupBy);
139424 substExprList(pSubst, p->pOrderBy);
139425 p->pHaving = substExpr(pSubst, p->pHaving);
139426 p->pWhere = substExpr(pSubst, p->pWhere);
139427 pSrc = p->pSrc;
139429 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
139430 substSelect(pSubst, pItem->pSelect, 1);
139431 if( pItem->fg.isTabFunc ){
139432 substExprList(pSubst, pItem->u1.pFuncArg);
139435 }while( doPrior && (p = p->pPrior)!=0 );
139445 ** pSrcItem->colUsed mask.
139449 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
139450 pItem = pWalker->u.pSrcItem;
139451 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
139452 if( pExpr->iColumn<0 ) return WRC_Continue;
139453 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
139461 if( NEVER(pSrcItem->pTab==0) ) return;
139466 pSrcItem->colUsed = 0;
139482 ** If pSrc contains any sub-selects, call this routine recursively
139483 ** on the FROM clause of each such sub-select, with iExcept set to -1.
139493 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
139496 assert( pItem->iCursor < aCsrMap[0] );
139497 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
139498 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
139500 pItem->iCursor = aCsrMap[pItem->iCursor+1];
139501 for(p=pItem->pSelect; p; p=p->pPrior){
139502 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
139512 int *aCsrMap = pWalker->u.aiCol;
139524 int op = pExpr->op;
139526 renumberCursorDoMapping(pWalker, &pExpr->iTable);
139529 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
139537 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
139559 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
139604 ** other than the one FROM-clause subquery that is a candidate
139606 ** from 2015-02-09.)
139619 ** sub-queries that were excluded from this optimization. Restriction
139634 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
139635 ** accidently carried the comment forward until 2014-09-15. Original
139670 ** The parent and sub-query may contain WHERE clauses. Subject to
139677 ** Also, each component of the sub-query must return the same number
139680 ** such (illegal) sub-query is flattened. The caller will detect the
139681 ** syntax error and return a detailed message.
139683 ** (18) If the sub-query is a compound select, then all terms of the
139690 ** (20) If the sub-query is a compound select, then it must not use
139701 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
139707 ** The subquery may not be an aggregate that uses the built-in min() or
139728 ** (29) Either the subquery is not the right-hand operand of a join with an
139729 ** ON or USING clause nor the right-hand operand of a NATURAL JOIN, or
139730 ** the right-most table within the FROM clause of the subquery
139735 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
139738 ** If flattening is not attempted, this routine is a no-op and returns 0.
139747 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
139750 const char *zSavedAuthContext = pParse->zAuthContext;
139753 Select *pSub1; /* Pointer to the rightmost select in sub-query */
139757 int iNewParent = -1;/* Replacement table for iParent */
139762 sqlite3 *db = pParse->db;
139769 assert( p->pPrior==0 );
139771 pSrc = p->pSrc;
139772 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
139773 pSubitem = &pSrc->a[iFrom];
139774 iParent = pSubitem->iCursor;
139775 pSub = pSubitem->pSelect;
139779 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
139782 pSubSrc = pSub->pSrc;
139786 ** because they could be computed at compile-time. But when LIMIT and OFFSET
139789 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
139790 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
139791 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
139794 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
139795 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
139796 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
139799 if( p->pOrderBy && pSub->pOrderBy ){
139802 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
139803 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
139804 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
139807 if( pSub->selFlags & (SF_Recursive) ){
139826 ** aggregates are processed - there is no mechanism to determine if
139827 ** the LEFT JOIN table should be all-NULL.
139831 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
139832 if( pSubSrc->nSrc>1 /* (3a) */
139834 || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */
139835 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
139836 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
139844 /* Setting isOuterJoin to -1 causes OP_IfNullRow opcodes to be generated for
139846 ** though they are not necessary. This will stress-test the OP_IfNullRow
139848 isOuterJoin = -1;
139852 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
139853 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139856 if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){
139867 ** (29a) The right-most entry in the FROM clause of the subquery
139877 if( pSubSrc->nSrc>=2
139878 && (pSubSrc->a[pSubSrc->nSrc-1].fg.jointype & JT_OUTER)!=0
139880 if( (pSubitem->fg.jointype & JT_NATURAL)!=0
139881 || pSubitem->fg.isUsing
139882 || NEVER(pSubitem->u3.pOn!=0) /* ON clause already shifted into WHERE */
139883 || pSubitem->fg.isOn
139889 /* Restriction (17): If the sub-query is a compound SELECT, then it must
139894 if( pSub->pPrior ){
139895 if( pSub->pOrderBy ){
139898 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
139901 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
139902 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
139903 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
139904 assert( pSub->pSrc!=0 );
139905 assert( (pSub->selFlags & SF_Recursive)==0 );
139906 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
139907 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
139908 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
139909 || pSub1->pSrc->nSrc<1 /* (17c) */
139911 || pSub1->pWin /* (17e) */
139916 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
139918 ** omitted on left-hand tables of the right join that is being
139922 testcase( pSub1->pSrc->nSrc>1 );
139926 if( p->pOrderBy ){
139928 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
139929 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
139934 if( (p->selFlags & SF_Recursive) ) return 0;
139936 if( pSrc->nSrc>1 ){
139937 if( pParse->nSelect>500 ) return 0;
139939 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
139940 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
139946 pSub->selId, pSub, iFrom));
139949 pParse->zAuthContext = pSubitem->zName;
139952 pParse->zAuthContext = zSavedAuthContext;
139955 pSub1 = pSubitem->pSelect;
139956 sqlite3DbFree(db, pSubitem->zDatabase);
139957 sqlite3DbFree(db, pSubitem->zName);
139958 sqlite3DbFree(db, pSubitem->zAlias);
139959 pSubitem->zDatabase = 0;
139960 pSubitem->zName = 0;
139961 pSubitem->zAlias = 0;
139962 pSubitem->pSelect = 0;
139963 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
139965 /* If the sub-query is a compound SELECT statement, then (by restrictions
139969 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
139972 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
139973 ** OFFSET clauses and joins them to the left-hand-side of the original
139975 ** select statements in the compound sub-query.
139996 ** We call this the "compound-subquery flattening".
139998 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
140000 ExprList *pOrderBy = p->pOrderBy;
140001 Expr *pLimit = p->pLimit;
140002 Select *pPrior = p->pPrior;
140003 Table *pItemTab = pSubitem->pTab;
140004 pSubitem->pTab = 0;
140005 p->pOrderBy = 0;
140006 p->pPrior = 0;
140007 p->pLimit = 0;
140009 p->pLimit = pLimit;
140010 p->pOrderBy = pOrderBy;
140011 p->op = TK_ALL;
140012 pSubitem->pTab = pItemTab;
140014 p->pPrior = pPrior;
140016 pNew->selId = ++pParse->nSelect;
140017 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
140020 pNew->pPrior = pPrior;
140021 if( pPrior ) pPrior->pNext = pNew;
140022 pNew->pNext = p;
140023 p->pPrior = pNew;
140024 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
140025 " creates %u as peer\n",pNew->selId));
140027 assert( pSubitem->pSelect==0 );
140030 if( db->mallocFailed ){
140031 pSubitem->pSelect = pSub1;
140040 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
140042 if( ALWAYS(pSubitem->pTab!=0) ){
140043 Table *pTabToDel = pSubitem->pTab;
140044 if( pTabToDel->nTabRef==1 ){
140049 testcase( pToplevel->earlyCleanup );
140051 pTabToDel->nTabRef--;
140053 pSubitem->pTab = 0;
140056 /* The following loop runs once for each term in a compound-subquery
140058 ** of flattening - a flattening other than a compound-subquery flattening -
140070 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
140073 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
140075 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
140076 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
140077 pSrc = pParent->pSrc; /* FROM clause of the outer query */
140080 jointype = pSubitem->fg.jointype; /* First time through the loop */
140099 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
140101 pParent->pSrc = pSrc;
140108 SrcItem *pItem = &pSrc->a[i+iFrom];
140109 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
140110 assert( pItem->fg.isTabFunc==0 );
140111 *pItem = pSubSrc->a[i];
140112 pItem->fg.jointype |= ltorj;
140113 iNewParent = pSubSrc->a[i].iCursor;
140114 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
140116 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
140117 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
140131 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
140132 /* At this point, any non-zero iOrderByCol values indicate that the
140139 ** function attempts to flatten a compound sub-query into pParent
140140 ** (the only way this can happen is if the compound sub-query is
140141 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
140142 ExprList *pOrderBy = pSub->pOrderBy;
140143 for(i=0; i<pOrderBy->nExpr; i++){
140144 pOrderBy->a[i].u.x.iOrderByCol = 0;
140146 assert( pParent->pOrderBy==0 );
140147 pParent->pOrderBy = pOrderBy;
140148 pSub->pOrderBy = 0;
140150 pWhere = pSub->pWhere;
140151 pSub->pWhere = 0;
140156 if( pParent->pWhere ){
140157 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
140159 pParent->pWhere = pWhere;
140162 if( db->mallocFailed==0 ){
140168 x.pEList = pSub->pEList;
140174 pParent->selFlags |= pSub->selFlags & SF_Compound;
140175 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
140183 if( pSub->pLimit ){
140184 pParent->pLimit = pSub->pLimit;
140185 pSub->pLimit = 0;
140191 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
140220 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
140244 assert( pColumn->op==TK_COLUMN );
140249 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
140253 /* 2018-10-25 ticket [cf5ed20f]
140255 for(i=0; i<pConst->nConst; i++){
140256 const Expr *pE2 = pConst->apExpr[i*2];
140257 assert( pE2->op==TK_COLUMN );
140258 if( pE2->iTable==pColumn->iTable
140259 && pE2->iColumn==pColumn->iColumn
140265 pConst->bHasAffBlob = 1;
140268 pConst->nConst++;
140269 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
140270 pConst->nConst*2*sizeof(Expr*));
140271 if( pConst->apExpr==0 ){
140272 pConst->nConst = 0;
140274 pConst->apExpr[pConst->nConst*2-2] = pColumn;
140275 pConst->apExpr[pConst->nConst*2-1] = pValue;
140282 ** is part of the AND-connected terms of the expression. For each term
140288 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
140293 if( pExpr->op==TK_AND ){
140294 findConstInWhere(pConst, pExpr->pRight);
140295 findConstInWhere(pConst, pExpr->pLeft);
140298 if( pExpr->op!=TK_EQ ) return;
140299 pRight = pExpr->pRight;
140300 pLeft = pExpr->pLeft;
140303 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
140306 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
140315 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
140317 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
140326 if( pConst->pOomFault[0] ) return WRC_Prune;
140327 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
140328 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
140334 for(i=0; i<pConst->nConst; i++){
140335 Expr *pColumn = pConst->apExpr[i*2];
140337 if( pColumn->iTable!=pExpr->iTable ) continue;
140338 if( pColumn->iColumn!=pExpr->iColumn ) continue;
140343 pConst->nChng++;
140346 assert( pExpr->pLeft==0 );
140347 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
140348 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
140357 ** any substitutions based on the contents of pWalker->u.pConst should
140363 ** one of the columns in pWalker->u.pConst, or
140368 ** pWalker->u.pConst.
140371 WhereConst *pConst = pWalker->u.pConst;
140376 if( pConst->bHasAffBlob ){
140377 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
140378 || pExpr->op==TK_IS
140380 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
140381 if( pConst->pOomFault[0] ) return WRC_Prune;
140382 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
140383 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
140387 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
140391 ** The WHERE-clause constant propagation optimization.
140394 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
140426 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
140449 x.pOomFault = &pParse->db->mallocFailed;
140455 if( ALWAYS(p->pSrc!=0)
140456 && p->pSrc->nSrc>0
140457 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
140466 findConstInWhere(&x, p->pWhere);
140475 sqlite3WalkExpr(&w, p->pWhere);
140476 sqlite3DbFree(x.pParse->db, x.apExpr);
140487 ** push WHERE clause expression pExpr down to FROM clause sub-query
140494 ** BY clause of all window function used by the sub-query. It is safe
140500 ** * the sub-query uses only one distinct window frame, and
140504 assert( pSubq->pWin->pPartition );
140505 assert( (pSubq->selFlags & SF_MultiPart)==0 );
140506 assert( pSubq->pPrior==0 );
140507 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
140517 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
140521 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
140529 ** (1) (** This restriction was removed on 2017-09-29. We used to
140547 ** of a LEFT JOIN where iCursor is not the right-hand table of that
140566 ** (6b) The inner query is a compound and uses window-functions.
140570 ** all window-functions used by the sub-query. It is safe to
140572 ** window over which any window-function is calculated.
140578 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
140590 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
140591 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0;
140594 if( pSubq->pPrior ){
140596 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
140597 if( pSel->pWin ) return 0; /* restriction (6b) */
140600 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
140611 for(pX=pSubq; pX; pX=pX->pPrior){
140612 assert( (pX->selFlags & (SF_Recursive))==0 );
140617 if( pSubq->pLimit!=0 ){
140620 while( pWhere->op==TK_AND ){
140621 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc);
140622 pWhere = pWhere->pLeft;
140628 || pWhere->w.iJoin!=iCursor)
140633 && pWhere->w.iJoin!=iCursor
140641 pSubq->selFlags |= SF_PushDown;
140644 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
140645 unsetJoinExpr(pNew, -1, 1);
140647 x.iTable = pSrc->iCursor;
140648 x.iNewTable = pSrc->iCursor;
140650 x.pEList = pSubq->pEList;
140653 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
140654 /* Restriction 6c has prevented push-down in this case */
140655 sqlite3ExprDelete(pParse->db, pNew);
140656 nChng--;
140660 if( pSubq->selFlags & SF_Aggregate ){
140661 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
140663 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
140665 pSubq = pSubq->pPrior;
140696 assert( pFunc->op==TK_AGG_FUNCTION );
140699 pEList = pFunc->x.pList;
140701 || pEList->nExpr!=1
140708 zFunc = pFunc->u.zToken;
140711 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
140721 assert( pOrderBy!=0 || db->mallocFailed );
140722 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
140728 ** The second argument is the associated aggregate-info object. This
140733 ** where table is a database table, not a sub-select or view. If the query
140747 assert( !p->pGroupBy );
140749 if( p->pWhere
140750 || p->pEList->nExpr!=1
140751 || p->pSrc->nSrc!=1
140752 || p->pSrc->a[0].pSelect
140753 || pAggInfo->nFunc!=1
140757 pTab = p->pSrc->a[0].pTab;
140761 pExpr = p->pEList->a[0].pExpr;
140763 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
140764 if( pExpr->pAggInfo!=pAggInfo ) return 0;
140765 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
140766 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
140775 ** If the source-list item passed as an argument was augmented with an
140779 ** pFrom->pIndex and return SQLITE_OK.
140782 Table *pTab = pFrom->pTab;
140783 char *zIndexedBy = pFrom->u1.zIndexedBy;
140786 assert( pFrom->fg.isIndexedBy!=0 );
140788 for(pIdx=pTab->pIndex;
140789 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
140790 pIdx=pIdx->pNext
140794 pParse->checkSchema = 1;
140797 assert( pFrom->fg.isCte==0 );
140798 pFrom->u2.pIBIndex = pIdx;
140833 if( p->pPrior==0 ) return WRC_Continue;
140834 if( p->pOrderBy==0 ) return WRC_Continue;
140835 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
140837 a = p->pOrderBy->a;
140839 /* If iOrderByCol is already non-zero, then it has already been matched
140841 ** SELECT is rewritten for window-functions processing and then passed
140846 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
140847 if( a[i].pExpr->flags & EP_Collate ) break;
140853 pParse = pWalker->pParse;
140854 db = pParse->db;
140861 p->pSrc = pNewSrc;
140862 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
140863 p->op = TK_SELECT;
140864 p->pWhere = 0;
140865 pNew->pGroupBy = 0;
140866 pNew->pHaving = 0;
140867 pNew->pOrderBy = 0;
140868 p->pPrior = 0;
140869 p->pNext = 0;
140870 p->pWith = 0;
140872 p->pWinDefn = 0;
140874 p->selFlags &= ~SF_Compound;
140875 assert( (p->selFlags & SF_Converted)==0 );
140876 p->selFlags |= SF_Converted;
140877 assert( pNew->pPrior!=0 );
140878 pNew->pPrior->pNext = pNew;
140879 pNew->pLimit = 0;
140884 ** Check to see if the FROM clause term pFrom has table-valued function
140885 ** arguments. If it does, leave an error message in pParse and return
140886 ** non-zero, since pFrom is not allowed to be a table-valued function.
140889 if( pFrom->fg.isTabFunc ){
140890 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
140900 ** FROM clause element pItem is really a common-table-expression (CTE)
140904 ** If a non-NULL value is returned, set *ppContext to point to the With
140912 const char *zName = pItem->zName;
140914 assert( pItem->zDatabase==0 );
140916 for(p=pWith; p; p=p->pOuter){
140918 for(i=0; i<p->nCte; i++){
140919 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
140921 return &p->a[i];
140924 if( p->bView ) break;
140930 ** with the inner-most WITH clause being at the top of the stack.
140954 if( pParse->nErr==0 ){
140955 assert( pParse->pWith!=pWith );
140956 pWith->pOuter = pParse->pWith;
140957 pParse->pWith = pWith;
140966 ** pParse->pWith linked list). And if currently processing a CTE
140970 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
140985 assert( pFrom->pTab==0 );
140986 if( pParse->pWith==0 ){
140990 if( pParse->nErr ){
140991 /* Prior errors might have left pParse->pWith in a goofy state, so
140995 if( pFrom->zDatabase!=0 ){
141000 if( pFrom->fg.notCte ){
141008 pCte = searchWith(pParse->pWith, pFrom, &pWith);
141010 sqlite3 *db = pParse->db;
141014 Select *pLeft; /* Left-most SELECT statement */
141015 Select *pRecTerm; /* Left-most recursive term */
141017 With *pSavedWith; /* Initial value of pParse->pWith */
141018 int iRecTab = -1; /* Cursor for recursive table */
141021 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
141023 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
141025 if( pCte->zCteErr ){
141026 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
141031 assert( pFrom->pTab==0 );
141034 pCteUse = pCte->pUse;
141036 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
141043 pCteUse->eM10d = pCte->eM10d;
141045 pFrom->pTab = pTab;
141046 pTab->nTabRef = 1;
141047 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
141048 pTab->iPKey = -1;
141049 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141050 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
141051 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
141052 if( db->mallocFailed ) return 2;
141053 pFrom->pSelect->selFlags |= SF_CopyCte;
141054 assert( pFrom->pSelect );
141055 if( pFrom->fg.isIndexedBy ){
141056 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
141059 pFrom->fg.isCte = 1;
141060 pFrom->u2.pCteUse = pCteUse;
141061 pCteUse->nUse++;
141062 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
141063 pCteUse->eM10d = M10d_Yes;
141067 pRecTerm = pSel = pFrom->pSelect;
141068 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
141069 while( bMayRecursive && pRecTerm->op==pSel->op ){
141071 SrcList *pSrc = pRecTerm->pSrc;
141072 assert( pRecTerm->pPrior!=0 );
141073 for(i=0; i<pSrc->nSrc; i++){
141074 SrcItem *pItem = &pSrc->a[i];
141075 if( pItem->zDatabase==0
141076 && pItem->zName!=0
141077 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
141079 pItem->pTab = pTab;
141080 pTab->nTabRef++;
141081 pItem->fg.isRecursive = 1;
141082 if( pRecTerm->selFlags & SF_Recursive ){
141084 "multiple references to recursive table: %s", pCte->zName
141088 pRecTerm->selFlags |= SF_Recursive;
141089 if( iRecTab<0 ) iRecTab = pParse->nTab++;
141090 pItem->iCursor = iRecTab;
141093 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
141094 pRecTerm = pRecTerm->pPrior;
141097 pCte->zCteErr = "circular reference: %s";
141098 pSavedWith = pParse->pWith;
141099 pParse->pWith = pWith;
141100 if( pSel->selFlags & SF_Recursive ){
141103 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
141104 assert( pRecTerm->pNext!=0 );
141105 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
141106 assert( pRecTerm->pWith==0 );
141107 pRecTerm->pWith = pSel->pWith;
141109 pRecTerm->pWith = 0;
141111 pParse->pWith = pSavedWith;
141116 pParse->pWith = pSavedWith;
141120 pParse->pWith = pWith;
141122 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
141123 pEList = pLeft->pEList;
141124 if( pCte->pCols ){
141125 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
141127 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
141129 pParse->pWith = pSavedWith;
141132 pEList = pCte->pCols;
141135 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
141137 if( pSel->selFlags & SF_Recursive ){
141138 pCte->zCteErr = "multiple recursive references: %s";
141140 pCte->zCteErr = "recursive reference in a subquery: %s";
141144 pCte->zCteErr = 0;
141145 pParse->pWith = pSavedWith;
141162 Parse *pParse = pWalker->pParse;
141163 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
141164 With *pWith = findRightmost(p)->pWith;
141166 assert( pParse->pWith==pWith || pParse->nErr );
141167 pParse->pWith = pWith->pOuter;
141175 ** sub-query in the FROM clause of a SELECT statement. This function
141181 Select *pSel = pFrom->pSelect;
141185 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
141187 pTab->nTabRef = 1;
141188 if( pFrom->zAlias ){
141189 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
141191 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
141193 while( pSel->pPrior ){ pSel = pSel->pPrior; }
141194 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
141195 pTab->iPKey = -1;
141196 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
141198 /* The usual case - do not allow ROWID on a subquery */
141199 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
141201 pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */
141203 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
141222 N--;
141224 if( pBase->fg.isUsing==0 ) continue;
141225 if( NEVER(pBase->u3.pUsing==0) ) continue;
141226 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
141239 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
141241 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
141257 Parse *pParse = pWalker->pParse;
141262 sqlite3 *db = pParse->db;
141264 u16 selFlags = p->selFlags;
141267 p->selFlags |= SF_Expanded;
141268 if( db->mallocFailed ){
141271 assert( p->pSrc!=0 );
141275 if( pWalker->eCode ){
141277 p->selId = ++pParse->nSelect;
141279 pTabList = p->pSrc;
141280 pEList = p->pEList;
141281 if( pParse->pWith && (p->selFlags & SF_View) ){
141282 if( p->pWith==0 ){
141283 p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With));
141284 if( p->pWith==0 ){
141288 p->pWith->bView = 1;
141290 sqlite3WithPush(pParse, p->pWith, 0);
141301 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141303 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
141304 if( pFrom->pTab ) continue;
141305 assert( pFrom->fg.isRecursive==0 );
141306 if( pFrom->zName==0 ){
141308 Select *pSel = pFrom->pSelect;
141309 /* A sub-query in the FROM clause of a SELECT */
141311 assert( pFrom->pTab==0 );
141318 pTab = pFrom->pTab;
141323 assert( pFrom->pTab==0 );
141324 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
141326 if( pTab->nTabRef>=0xffff ){
141328 pTab->zName);
141329 pFrom->pTab = 0;
141332 pTab->nTabRef++;
141339 u8 eCodeOrig = pWalker->eCode;
141341 assert( pFrom->pSelect==0 );
141343 if( (db->flags & SQLITE_EnableView)==0
141344 && pTab->pSchema!=db->aDb[1].pSchema
141347 pTab->zName);
141349 pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0);
141353 && pFrom->fg.fromDDL
141354 && ALWAYS(pTab->u.vtab.p!=0)
141355 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
141358 pTab->zName);
141362 nCol = pTab->nCol;
141363 pTab->nCol = -1;
141364 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
141365 sqlite3WalkSelect(pWalker, pFrom->pSelect);
141366 pWalker->eCode = eCodeOrig;
141367 pTab->nCol = nCol;
141373 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
141380 assert( db->mallocFailed==0 || pParse->nErr!=0 );
141381 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
141396 for(k=0; k<pEList->nExpr; k++){
141397 pE = pEList->a[k].pExpr;
141398 if( pE->op==TK_ASTERISK ) break;
141399 assert( pE->op!=TK_DOT || pE->pRight!=0 );
141400 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
141401 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
141402 elistFlags |= pE->flags;
141404 if( k<pEList->nExpr ){
141410 struct ExprList_item *a = pEList->a;
141412 int flags = pParse->db->flags;
141416 for(k=0; k<pEList->nExpr; k++){
141418 elistFlags |= pE->flags;
141419 pRight = pE->pRight;
141420 assert( pE->op!=TK_DOT || pRight!=0 );
141421 if( pE->op!=TK_ASTERISK
141422 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
141428 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
141429 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
141438 if( pE->op==TK_DOT ){
141439 assert( pE->pLeft!=0 );
141440 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
141441 zTName = pE->pLeft->u.zToken;
141443 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141444 Table *pTab = pFrom->pTab; /* Table for this data source */
141445 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
141451 if( (zTabName = pFrom->zAlias)==0 ){
141452 zTabName = pTab->zName;
141454 if( db->mallocFailed ) break;
141455 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) );
141456 if( pFrom->fg.isNestedFrom ){
141457 assert( pFrom->pSelect!=0 );
141458 pNestedFrom = pFrom->pSelect->pEList;
141460 assert( pNestedFrom->nExpr==pTab->nCol );
141466 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
141467 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
141469 if( i+1<pTabList->nSrc
141475 for(ii=0; ii<pUsing->nId; ii++){
141476 const char *zUName = pUsing->a[ii].zName;
141480 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
141481 assert( pX->zEName==0 );
141482 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
141483 pX->fg.eEName = ENAME_TAB;
141484 pX->fg.bUsingTerm = 1;
141490 for(j=0; j<pTab->nCol; j++){
141491 char *zName = pTab->aCol[j].zCnName;
141497 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0
141503 ** result-set list unless the SELECT has the SF_IncludeHidden
141506 if( (p->selFlags & SF_IncludeHidden)==0
141507 && IsHiddenColumn(&pTab->aCol[j])
141511 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
141520 if( pFrom->fg.isUsing
141521 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
141529 if( (pTabList->nSrc>1
141530 && ( (pFrom->fg.jointype & JT_LTORJ)==0
141532 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
141540 if( IN_RENAME_OBJECT && pE->pLeft ){
141541 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
141554 pX = &pNew->a[pNew->nExpr-1];
141555 assert( pX->zEName==0 );
141558 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
141559 testcase( pX->zEName==0 );
141561 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
141563 testcase( pX->zEName==0 );
141565 pX->fg.eEName = ENAME_TAB;
141566 if( (pFrom->fg.isUsing
141567 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
141569 || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
141571 pX->fg.bNoExpand = 1;
141574 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
141575 pX->fg.eEName = ENAME_NAME;
141577 pX->zEName = sqlite3DbStrDup(db, zName);
141578 pX->fg.eEName = ENAME_NAME;
141592 p->pEList = pNew;
141594 if( p->pEList ){
141595 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
141600 p->selFlags |= SF_ComplexResult;
141605 SELECTTRACE(0x100,pParse,p,("After result-set wildcard expansion:\n"));
141631 ** If anything goes wrong, an error message is written into pParse.
141632 ** The calling function can detect the problem by looking at pParse->nErr
141633 ** and/or pParse->db->mallocFailed.
141639 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
141656 ** For each FROM-clause subquery, add Column.zType and Column.zColl
141671 assert( p->selFlags & SF_Resolved );
141672 if( p->selFlags & SF_HasTypeInfo ) return;
141673 p->selFlags |= SF_HasTypeInfo;
141674 pParse = pWalker->pParse;
141675 pTabList = p->pSrc;
141676 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
141677 Table *pTab = pFrom->pTab;
141679 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
141680 /* A sub-query in the FROM clause of a SELECT */
141681 Select *pSel = pFrom->pSelect;
141683 while( pSel->pPrior ) pSel = pSel->pPrior;
141695 ** the Table structures of all FROM-clause subqueries in a
141716 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
141717 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
141729 assert( p!=0 || pParse->db->mallocFailed );
141730 assert( pParse->db->pParse==pParse );
141731 if( pParse->db->mallocFailed ) return;
141732 if( p->selFlags & SF_HasTypeInfo ) return;
141734 if( pParse->nErr ) return;
141736 if( pParse->nErr ) return;
141749 Vdbe *v = pParse->pVdbe;
141752 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
141753 assert( pParse->db->pParse==pParse );
141754 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
141756 if( pParse->nErr ) return;
141760 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
141761 for(i=0; i<pAggInfo->nColumn; i++){
141762 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
141763 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
141765 for(i=0; i<pAggInfo->nFunc; i++){
141766 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
141767 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
141770 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
141771 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
141772 if( pFunc->iDistinct>=0 ){
141773 Expr *pE = pFunc->pFExpr;
141775 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
141778 pFunc->iDistinct = -1;
141780 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
141781 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
141782 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
141783 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
141784 pFunc->pFunc->zName));
141795 Vdbe *v = pParse->pVdbe;
141798 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141800 assert( ExprUseXList(pF->pFExpr) );
141801 pList = pF->pFExpr->x.pList;
141802 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
141803 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
141812 ** If regAcc is non-zero and there are no min() or max() aggregates
141813 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
141823 Vdbe *v = pParse->pVdbe;
141830 pAggInfo->directMode = 1;
141831 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
141836 assert( ExprUseXList(pF->pFExpr) );
141837 assert( !IsWindowFunc(pF->pFExpr) );
141838 pList = pF->pFExpr->x.pList;
141839 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
141840 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
141841 if( pAggInfo->nAccumulator
141842 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
141848 if( regHit==0 ) regHit = ++pParse->nMem;
141862 nArg = pList->nExpr;
141869 if( pF->iDistinct>=0 && pList ){
141873 pF->iDistinct = codeDistinct(pParse, eDistinctType,
141874 pF->iDistinct, addrNext, pList, regAgg);
141876 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
141880 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
141881 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
141882 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
141885 pColl = pParse->db->pDfltColl;
141887 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
141890 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
141891 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
141898 if( regHit==0 && pAggInfo->nAccumulator ){
141904 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
141905 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
141908 pAggInfo->directMode = 0;
141924 if( pParse->explain==2 ){
141927 pTab->zName,
141929 bCover ? pIdx->zName : ""
141944 ** sub-expression matches the criteria for being moved to the WHERE
141945 ** clause. If so, add it to the WHERE clause and replace the sub-expression
141949 if( pExpr->op!=TK_AND ){
141950 Select *pS = pWalker->u.pSelect;
141952 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
141958 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
141960 && pExpr->pAggInfo==0
141962 sqlite3 *db = pWalker->pParse->db;
141965 Expr *pWhere = pS->pWhere;
141967 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
141968 pS->pWhere = pNew;
141969 pWalker->eCode = 1;
141998 sqlite3WalkExpr(&sWalker, p->pHaving);
142008 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
142013 SrcList *pTabList, /* Search for self-joins in this FROM clause */
142017 assert( pThis->pSelect!=0 );
142018 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
142019 for(pItem = pTabList->a; pItem<pThis; pItem++){
142021 if( pItem->pSelect==0 ) continue;
142022 if( pItem->fg.viaCoroutine ) continue;
142023 if( pItem->zName==0 ) continue;
142024 assert( pItem->pTab!=0 );
142025 assert( pThis->pTab!=0 );
142026 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
142027 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
142028 pS1 = pItem->pSelect;
142029 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
142034 if( pItem->pSelect->selFlags & SF_PushDown ){
142048 sqlite3DbFree(db, p->aCol);
142049 sqlite3DbFree(db, p->aFunc);
142078 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
142079 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
142080 if( p->pWhere ) return 0;
142081 if( p->pGroupBy ) return 0;
142082 pExpr = p->pEList->a[0].pExpr;
142083 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
142085 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
142087 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
142088 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
142089 pSub = p->pSrc->a[0].pSelect;
142091 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
142093 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
142094 if( pSub->pWhere ) return 0; /* No WHERE clause */
142095 if( pSub->pLimit ) return 0; /* No LIMIT clause */
142096 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
142097 pSub = pSub->pPrior; /* Repeat over compound */
142102 db = pParse->db;
142105 pSub = p->pSrc->a[0].pSelect;
142106 p->pSrc->a[0].pSelect = 0;
142107 sqlite3SrcListDelete(db, p->pSrc);
142108 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
142111 pPrior = pSub->pPrior;
142112 pSub->pPrior = 0;
142113 pSub->pNext = 0;
142114 pSub->selFlags |= SF_Aggregate;
142115 pSub->selFlags &= ~SF_Compound;
142116 pSub->nSelectRow = 0;
142117 sqlite3ExprListDelete(db, pSub->pEList);
142119 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
142129 p->pEList->a[0].pExpr = pExpr;
142130 p->selFlags &= ~SF_Aggregate;
142134 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
142143 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
142149 for(i=0; i<pSrc->nSrc; i++){
142150 SrcItem *p1 = &pSrc->a[i];
142152 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
142155 if( p1->pSelect
142156 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
142157 && sameSrcAlias(p0, p1->pSelect->pSrc)
142172 ** encountered, then an appropriate error message is left in
142173 ** pParse->zErrMsg.
142201 db = pParse->db;
142202 assert( pParse==db->pParse );
142204 if( p==0 || pParse->nErr ){
142207 assert( db->mallocFailed==0 );
142210 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
142220 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
142221 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
142222 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
142223 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
142225 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
142226 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
142227 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
142229 if( p->pOrderBy ){
142233 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
142238 p->pOrderBy);
142239 testcase( pParse->earlyCleanup );
142240 p->pOrderBy = 0;
142242 p->selFlags &= ~SF_Distinct;
142243 p->selFlags |= SF_NoopOrderBy;
142246 if( pParse->nErr ){
142249 assert( db->mallocFailed==0 );
142250 assert( p->pEList!=0 );
142260 ** In this case, it is an error if the target object (pSrc->a[0]) name
142261 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
142267 if( p->selFlags & SF_UFSrcCheck ){
142268 SrcItem *p0 = &p->pSrc->a[0];
142269 if( sameSrcAlias(p0, p->pSrc) ){
142272 p0->zAlias ? p0->zAlias : p0->pTab->zName
142278 ** and leaving this flag set can cause errors if a compound sub-query
142279 ** in p->pSrc is flattened into this query and this function called
142281 p->selFlags &= ~SF_UFSrcCheck;
142284 if( pDest->eDest==SRT_Output ){
142290 assert( pParse->nErr );
142294 if( p->pWin && (sqlite3TreeTrace & 0x108)!=0 ){
142300 pTabList = p->pSrc;
142301 isAgg = (p->selFlags & SF_Aggregate)!=0;
142303 sSort.pOrderBy = p->pOrderBy;
142309 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
142310 SrcItem *pItem = &pTabList->a[i];
142311 Select *pSub = pItem->pSelect;
142312 Table *pTab = pItem->pTab;
142322 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT
142323 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
142327 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
142328 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
142329 assert( pItem->iCursor>=0 );
142330 unsetJoinExpr(p->pWhere, pItem->iCursor,
142331 pTabList->a[0].fg.jointype & JT_LTORJ);
142339 if( pTab->nCol!=pSub->pEList->nExpr ){
142341 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
142349 ** will be implemented as a co-routine and there is no advantage to
142352 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
142353 assert( pSub->pGroupBy==0 );
142355 /* If a FROM-clause subquery has an ORDER BY clause that is not
142364 ** (2) The subquery was added to help with window-function
142368 ** the built-in count(), min(), or max().
142375 if( pSub->pOrderBy!=0
142376 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
142377 && pSub->pLimit==0 /* Condition (1) */
142378 && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
142379 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
142383 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
142386 pSub->pOrderBy);
142387 pSub->pOrderBy = 0;
142393 ** it will be implemented as a co-routine, then do not flatten. This
142408 if( pSub->pOrderBy!=0
142410 && (p->selFlags & SF_ComplexResult)!=0
142411 && (pTabList->nSrc==1
142412 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
142418 if( pParse->nErr ) goto select_end;
142420 i = -1;
142422 pTabList = p->pSrc;
142423 if( db->mallocFailed ) goto select_end;
142425 sSort.pOrderBy = p->pOrderBy;
142434 if( p->pPrior ){
142437 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
142442 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
142447 /* Do the WHERE-clause constant propagation optimization if this is
142448 ** a join. No need to speed time on this operation for non-join queries
142452 if( p->pWhere!=0
142453 && p->pWhere->op==TK_AND
142471 if( db->mallocFailed ) goto select_end;
142472 pEList = p->pEList;
142473 pTabList = p->pSrc;
142479 ** (2) Generate code for all sub-queries
142481 for(i=0; i<pTabList->nSrc; i++){
142482 SrcItem *pItem = &pTabList->a[i];
142495 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
142496 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
142503 ** assume the column name is non-NULL and segfault. The use of an empty
142506 if( pItem->colUsed==0 && pItem->zName!=0 ){
142507 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
142511 /* Generate code for all sub-queries in the FROM clause
142513 pSub = pItem->pSelect;
142517 assert( pItem->addrFillSub==0 );
142522 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
142526 pParse->nHeight += sqlite3SelectExprHeight(p);
142528 /* Make copies of constant WHERE-clause terms in the outer query down
142532 && (pItem->fg.isCte==0
142533 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
142534 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem)
142539 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
142543 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
142545 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
142548 zSavedAuthContext = pParse->zAuthContext;
142549 pParse->zAuthContext = pItem->zName;
142553 ** The subquery is implemented as a co-routine if all of the following are
142562 && (pTabList->nSrc==1
142563 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) /* (1) */
142564 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
142565 && (pTabList->a[0].fg.jointype & JT_LTORJ)==0 /* (3) */
142567 /* Implement a co-routine that will return a single row of the result
142572 pItem->regReturn = ++pParse->nMem;
142573 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
142575 pItem->addrFillSub = addrTop;
142576 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
142577 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
142579 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142580 pItem->fg.viaCoroutine = 1;
142581 pItem->regResult = dest.iSdst;
142582 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
142583 sqlite3VdbeJumpHere(v, addrTop-1);
142585 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
142588 ** the make the pItem->iCursor be a copy of the ephemerial table that
142590 CteUse *pCteUse = pItem->u2.pCteUse;
142591 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
142592 if( pItem->iCursor!=pCteUse->iCur ){
142593 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
142596 pSub->nSelectRow = pCteUse->nRowEst;
142600 if( pPrior->addrFillSub ){
142601 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
142603 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
142604 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
142612 pItem->regReturn = ++pParse->nMem;
142614 pItem->addrFillSub = topAddr+1;
142615 pItem->fg.isMaterialized = 1;
142616 if( pItem->fg.isCorrelated==0 ){
142625 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
142628 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142630 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
142634 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142635 CteUse *pCteUse = pItem->u2.pCteUse;
142636 pCteUse->addrM9e = pItem->addrFillSub;
142637 pCteUse->regRtn = pItem->regReturn;
142638 pCteUse->iCur = pItem->iCursor;
142639 pCteUse->nRowEst = pSub->nSelectRow;
142642 if( db->mallocFailed ) goto select_end;
142643 pParse->nHeight -= sqlite3SelectExprHeight(p);
142644 pParse->zAuthContext = zSavedAuthContext;
142650 pEList = p->pEList;
142651 pWhere = p->pWhere;
142652 pGroupBy = p->pGroupBy;
142653 pHaving = p->pHaving;
142654 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
142658 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
142664 ** if the select-list is the same as the ORDER BY list, then this query
142673 ** The second form is preferred as a single index (or temp-table) may be
142675 ** written the query must use a temp-table for at least one of the ORDER
142676 ** BY and DISTINCT, and an index or separate temp-table for the other.
142678 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
142679 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
142681 && p->pWin==0
142684 p->selFlags &= ~SF_Distinct;
142685 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
142686 p->selFlags |= SF_Aggregate;
142687 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
142703 ** being unused if the data can be extracted in pre-sorted order.
142712 pParse, sSort.pOrderBy, 0, pEList->nExpr);
142713 sSort.iECursor = pParse->nTab++;
142716 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
142720 sSort.addrSortIndex = -1;
142725 if( pDest->eDest==SRT_EphemTab ){
142726 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
142727 if( p->selFlags & SF_NestedFrom ){
142728 /* Delete or NULL-out result columns that will never be used */
142730 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
142731 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
142732 sqlite3DbFree(db, pEList->a[ii].zEName);
142733 pEList->nExpr--;
142735 for(ii=0; ii<pEList->nExpr; ii++){
142736 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
142744 if( (p->selFlags & SF_FixedLimit)==0 ){
142745 p->nSelectRow = 320; /* 4 billion rows */
142748 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
142755 if( p->selFlags & SF_Distinct ){
142756 sDistinct.tabTnct = pParse->nTab++;
142759 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
142770 | (p->selFlags & SF_FixedLimit);
142772 Window *pWin = p->pWin; /* Main window object (or NULL) */
142783 p->pEList, p, wctrlFlags, p->nSelectRow);
142785 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
142786 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
142794 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
142808 assert( p->pEList==pEList );
142814 int regGosub = ++pParse->nMem;
142820 VdbeNoopComment((v, "inner-loop subroutine"));
142822 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
142825 VdbeComment((v, "end inner-loop subroutine"));
142831 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
142863 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
142864 pItem->u.x.iAlias = 0;
142866 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
142867 pItem->u.x.iAlias = 0;
142870 if( p->nSelectRow>66 ) p->nSelectRow = 66;
142875 ** in the correct order. It also may not - the GROUP BY might use a
142880 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
142883 ** ASC or DESC order - only that each group is returned contiguously.
142887 for(ii=0; ii<pGroupBy->nExpr; ii++){
142889 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
142890 pGroupBy->a[ii].fg.sortFlags = sortFlags;
142892 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
142898 p->nSelectRow = 0;
142912 testcase( pParse->earlyCleanup );
142914 if( db->mallocFailed ){
142917 pAggInfo->selId = p->selId;
142923 pAggInfo->mnReg = pParse->nMem+1;
142924 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
142925 pAggInfo->pGroupBy = pGroupBy;
142930 assert( pWhere==p->pWhere );
142931 assert( pHaving==p->pHaving );
142932 assert( pGroupBy==p->pGroupBy );
142934 pWhere = p->pWhere;
142938 pAggInfo->nAccumulator = pAggInfo->nColumn;
142939 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
142940 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
142944 for(i=0; i<pAggInfo->nFunc; i++){
142945 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
142948 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
142952 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
142957 pAggInfo->mxReg = pParse->nMem;
142958 if( db->mallocFailed ) goto select_end;
142968 for(ii=0; ii<pAggInfo->nColumn; ii++){
142969 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
142970 ii, pAggInfo->aCol[ii].iMem);
142971 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
142973 for(ii=0; ii<pAggInfo->nFunc; ii++){
142974 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
142975 ii, pAggInfo->aFunc[ii].iMem);
142976 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
142987 int addr1; /* A-vs-B comparision jump */
142999 if( pAggInfo->nFunc==1
143000 && pAggInfo->aFunc[0].iDistinct>=0
143001 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
143002 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
143003 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
143005 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
143017 pAggInfo->sortingIdx = pParse->nTab++;
143019 0, pAggInfo->nColumn);
143021 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
143026 iUseFlag = ++pParse->nMem;
143027 iAbortFlag = ++pParse->nMem;
143028 regOutputRow = ++pParse->nMem;
143030 regReset = ++pParse->nMem;
143032 iAMem = pParse->nMem + 1;
143033 pParse->nMem += pGroupBy->nExpr;
143034 iBMem = pParse->nMem + 1;
143035 pParse->nMem += pGroupBy->nExpr;
143038 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
143057 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
143075 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
143079 nGroupBy = pGroupBy->nExpr;
143082 for(i=0; i<pAggInfo->nColumn; i++){
143083 if( pAggInfo->aCol[i].iSorterColumn>=j ){
143091 for(i=0; i<pAggInfo->nColumn; i++){
143092 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
143093 if( pCol->iSorterColumn>=j ){
143096 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
143102 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
143107 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
143110 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
143112 pAggInfo->useSortingIdx = 1;
143119 ** This is an optimization - the correct answer should result regardless.
143136 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
143139 for(j=0; j<pGroupBy->nExpr; j++){
143143 pAggInfo->directMode = 1;
143144 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
143147 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
143161 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
143180 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
143200 ** is less than or equal to zero, the subroutine is a no-op. If
143217 selectInnerLoop(pParse, p, -1, &sSort,
143223 /* Generate a subroutine that will reset the group-by accumulator
143232 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
143233 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143252 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
143253 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
143257 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
143260 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
143264 ** (2011-04-15) Do not do a full scan of an unordered index.
143266 ** (2013-10-03) Do not count the entries in a partial index.
143272 if( !p->pSrc->a[0].fg.notIndexed ){
143273 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143274 if( pIdx->bUnordered==0
143275 && pIdx->szIdxRow<pTab->szTabRow
143276 && pIdx->pPartIdxWhere==0
143277 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
143284 iRoot = pBest->tnum;
143288 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
143291 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
143293 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
143311 if( pAggInfo->nAccumulator ){
143312 for(i=0; i<pAggInfo->nFunc; i++){
143313 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
143316 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
143320 if( i==pAggInfo->nFunc ){
143321 regAcc = ++pParse->nMem;
143324 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
143325 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
143326 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
143334 assert( p->pGroupBy==0 );
143343 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
143355 struct AggInfo_func *pF = pAggInfo->aFunc;
143357 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
143372 selectInnerLoop(pParse, p, -1, 0, 0,
143389 assert( p->pEList==pEList );
143390 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
143399 rc = (pParse->nErr>0);
143405 assert( db->mallocFailed==0 || db->mallocFailed==1 );
143406 assert( db->mallocFailed==0 || pParse->nErr!=0 );
143409 if( pAggInfo && !db->mallocFailed ){
143410 for(i=0; i<pAggInfo->nColumn; i++){
143411 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
143413 assert( pExpr->pAggInfo==pAggInfo );
143414 assert( pExpr->iAgg==i );
143416 for(i=0; i<pAggInfo->nFunc; i++){
143417 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
143419 assert( pExpr->pAggInfo==pAggInfo );
143420 assert( pExpr->iAgg==i );
143465 char *zErrMsg; /* Error message text, if an error occurs */
143480 int need; /* Slots needed in p->azResult[] */
143484 /* Make sure there is enough space in p->azResult to hold everything
143487 if( p->nRow==0 && argv!=0 ){
143492 if( p->nData + need > p->nAlloc ){
143494 p->nAlloc = p->nAlloc*2 + need;
143495 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
143497 p->azResult = azNew;
143503 if( p->nRow==0 ){
143504 p->nColumn = nCol;
143508 p->azResult[p->nData++] = z;
143510 }else if( (int)p->nColumn!=nCol ){
143511 sqlite3_free(p->zErrMsg);
143512 p->zErrMsg = sqlite3_mprintf(
143515 p->rc = SQLITE_ERROR;
143531 p->azResult[p->nData++] = z;
143533 p->nRow++;
143538 p->rc = SQLITE_NOMEM_BKPT;
143578 db->errCode = SQLITE_NOMEM;
143594 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
143607 db->errCode = SQLITE_NOMEM;
143626 azResult--;
143659 pTriggerStep = pTriggerStep->pNext;
143661 sqlite3ExprDelete(db, pTmp->pWhere);
143662 sqlite3ExprListDelete(db, pTmp->pExprList);
143663 sqlite3SelectDelete(db, pTmp->pSelect);
143664 sqlite3IdListDelete(db, pTmp->pIdList);
143665 sqlite3UpsertDelete(db, pTmp->pUpsert);
143666 sqlite3SrcListDelete(db, pTmp->pFrom);
143667 sqlite3DbFree(db, pTmp->zSpan);
143678 ** are already attached to pTab->pTrigger. But there might be additional
143680 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
143685 ** pTab as well as the triggers lised in pTab->pTrigger.
143692 assert( pParse->disableTriggers==0 );
143693 pTmpSchema = pParse->db->aDb[1].pSchema;
143694 p = sqliteHashFirst(&pTmpSchema->trigHash);
143695 pList = pTab->pTrigger;
143698 if( pTrig->pTabSchema==pTab->pSchema
143699 && pTrig->table
143700 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
143701 && pTrig->pTabSchema!=pTmpSchema
143703 pTrig->pNext = pList;
143705 }else if( pTrig->op==TK_RETURNING ){
143707 assert( pParse->db->pVtabCtx==0 );
143709 assert( pParse->bReturning );
143710 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
143711 pTrig->table = pTab->zName;
143712 pTrig->pTabSchema = pTab->pSchema;
143713 pTrig->pNext = pList;
143721 printf("Triggers for %s:", pTab->zName);
143722 for(pX=pList; pX; pX=pX->pNext){
143723 printf(" %s", pX->zName);
143736 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
143755 sqlite3 *db = pParse->db; /* The database connection */
143760 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
143766 if( pName2->n>0 ){
143779 if( !pTableName || db->mallocFailed ){
143783 /* A long-standing parser bug is that this syntax was allowed:
143791 if( db->init.busy && iDb!=1 ){
143792 sqlite3DbFree(db, pTableName->a[0].zDatabase);
143793 pTableName->a[0].zDatabase = 0;
143802 if( db->init.busy==0 && pName2->n==0 && pTab
143803 && pTab->pSchema==db->aDb[1].pSchema ){
143808 if( db->mallocFailed ) goto trigger_cleanup;
143809 assert( pTableName->nSrc==1 );
143828 assert( db->mallocFailed );
143831 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
143836 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
143840 assert( !db->init.busy );
143848 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
143858 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
143863 " trigger on table: %S", pTableName->a);
143869 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
143871 const char *zDb = db->aDb[iTabDb].zDbSName;
143872 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
143874 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
143895 pTrigger->zName = zName;
143897 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
143898 pTrigger->pSchema = db->aDb[iDb].pSchema;
143899 pTrigger->pTabSchema = pTab->pSchema;
143900 pTrigger->op = (u8)op;
143901 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
143903 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
143904 pTrigger->pWhen = pWhen;
143907 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
143909 pTrigger->pColumns = pColumns;
143911 assert( pParse->pNewTrigger==0 );
143912 pParse->pNewTrigger = pTrigger;
143919 if( !pParse->pNewTrigger ){
143922 assert( pParse->pNewTrigger==pTrigger );
143927 if( db->init.iDb==1 ){
143930 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
143934 ** "orphaned trigger" - a trigger whose associated table is missing.
143936 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
143938 db->init.orphanTrigger = 1;
143952 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
143954 sqlite3 *db = pParse->db; /* The database */
143959 pParse->pNewTrigger = 0;
143960 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
143961 zName = pTrig->zName;
143962 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
143963 pTrig->step_list = pStepList;
143965 pStepList->pTrig = pTrig;
143966 pStepList = pStepList->pNext;
143968 sqlite3TokenInit(&nameToken, pTrig->zName);
143970 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
143971 || sqlite3FixExpr(&sFix, pTrig->pWhen)
143978 assert( !db->init.busy );
143979 pParse->pNewTrigger = pTrig;
143987 if( !db->init.busy ){
143995 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
144000 db->aDb[iDb].zDbSName, zName,
144001 pTrig->table, z);
144008 if( db->init.busy ){
144010 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
144016 }else if( pLink->pSchema==pLink->pTabSchema ){
144018 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
144020 pLink->pNext = pTab->pTrigger;
144021 pTab->pTrigger = pLink;
144027 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
144060 pTriggerStep->op = TK_SELECT;
144061 pTriggerStep->pSelect = pSelect;
144062 pTriggerStep->orconf = OE_Default;
144063 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144071 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
144080 sqlite3 *db = pParse->db;
144083 if( pParse->nErr ) return 0;
144084 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
144087 memcpy(z, pName->z, pName->n);
144089 pTriggerStep->zTarget = z;
144090 pTriggerStep->op = op;
144091 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
144093 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
144116 sqlite3 *db = pParse->db;
144119 assert(pSelect != 0 || db->mallocFailed);
144124 pTriggerStep->pSelect = pSelect;
144127 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
144129 pTriggerStep->pIdList = pColumn;
144130 pTriggerStep->pUpsert = pUpsert;
144131 pTriggerStep->orconf = orconf;
144133 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
144154 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
144161 sqlite3 *db = pParse->db;
144167 pTriggerStep->pExprList = pEList;
144168 pTriggerStep->pWhere = pWhere;
144169 pTriggerStep->pFrom = pFrom;
144174 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
144175 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
144176 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
144178 pTriggerStep->orconf = orconf;
144198 sqlite3 *db = pParse->db;
144204 pTriggerStep->pWhere = pWhere;
144207 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
144209 pTriggerStep->orconf = OE_Default;
144219 if( pTrigger==0 || pTrigger->bReturning ) return;
144220 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
144221 sqlite3DbFree(db, pTrigger->zName);
144222 sqlite3DbFree(db, pTrigger->table);
144223 sqlite3ExprDelete(db, pTrigger->pWhen);
144224 sqlite3IdListDelete(db, pTrigger->pColumns);
144241 sqlite3 *db = pParse->db;
144243 if( db->mallocFailed ) goto drop_trigger_cleanup;
144248 assert( pName->nSrc==1 );
144249 zDb = pName->a[0].zDatabase;
144250 zName = pName->a[0].zName;
144252 for(i=OMIT_TEMPDB; i<db->nDb; i++){
144256 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
144261 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
144265 pParse->checkSchema = 1;
144279 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
144289 sqlite3 *db = pParse->db;
144292 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
144293 assert( iDb>=0 && iDb<db->nDb );
144295 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
144299 const char *zDb = db->aDb[iDb].zDbSName;
144302 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
144314 db->aDb[iDb].zDbSName, pTrigger->zName
144317 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
144329 pHash = &(db->aDb[iDb].pSchema->trigHash);
144332 if( pTrigger->pSchema==pTrigger->pTabSchema ){
144336 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
144338 *pp = (*pp)->pNext;
144345 db->mDbFlags |= DBFLAG_SchemaChange;
144361 for(e=0; e<pEList->nExpr; e++){
144362 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
144371 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
144372 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
144395 || (pList->bReturning && pList->pNext==0) );
144398 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
144399 && pTab->pTrigger!=0
144404 if( pList==pTab->pTrigger ){
144408 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
144409 p->pNext = 0;
144413 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
144414 mask |= p->tr_tm;
144415 }else if( p->op==TK_RETURNING ){
144419 p->op = op;
144426 p->tr_tm = TRIGGER_BEFORE;
144428 p->tr_tm = TRIGGER_AFTER;
144430 mask |= p->tr_tm;
144431 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
144434 mask |= p->tr_tm;
144436 p = p->pNext;
144453 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
144454 || pParse->disableTriggers
144463 ** Convert the pStep->zTarget string into a SrcList and return a pointer
144476 sqlite3 *db = pParse->db;
144478 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
144480 assert( pSrc==0 || pSrc->nSrc==1 );
144483 Schema *pSchema = pStep->pTrig->pSchema;
144484 pSrc->a[0].zName = zName;
144485 if( pSchema!=db->aDb[1].pSchema ){
144486 pSrc->a[0].pSchema = pSchema;
144488 if( pStep->pFrom ){
144489 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
144490 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
144516 if( pTerm->op==TK_ASTERISK ) return 1;
144517 if( pTerm->op!=TK_DOT ) return 0;
144518 assert( pTerm->pRight!=0 );
144519 assert( pTerm->pLeft!=0 );
144520 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
144537 sqlite3 *db = pParse->db;
144540 for(i=0; i<pList->nExpr; i++){
144541 Expr *pOldExpr = pList->a[i].pExpr;
144545 for(jj=0; jj<pTab->nCol; jj++){
144547 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
144548 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
144550 if( !db->mallocFailed ){
144551 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144552 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
144553 pItem->fg.eEName = ENAME_NAME;
144559 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
144560 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
144561 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
144562 pItem->fg.eEName = pList->a[i].fg.eEName;
144572 ** is generated in-line.
144580 Vdbe *v = pParse->pVdbe;
144581 sqlite3 *db = pParse->db;
144588 assert( pParse->bReturning );
144589 assert( db->pParse==pParse );
144590 pReturning = pParse->u1.pReturning;
144591 assert( pTrigger == &(pReturning->retTrig) );
144594 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
144598 sFrom.a[0].iCursor = -1;
144600 if( pParse->nErr==0 ){
144601 assert( db->mallocFailed==0 );
144605 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
144606 if( !db->mallocFailed ){
144609 if( pReturning->nRetCol==0 ){
144610 pReturning->nRetCol = pNew->nExpr;
144611 pReturning->iRetCur = pParse->nTab++;
144616 pParse->eTriggerOp = pTrigger->op;
144617 pParse->pTriggerTab = pTab;
144619 && ALWAYS(!db->mallocFailed)
144622 int nCol = pNew->nExpr;
144623 int reg = pParse->nMem+1;
144624 pParse->nMem += nCol+2;
144625 pReturning->iRetReg = reg;
144627 Expr *pCol = pNew->a[i].pExpr;
144628 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
144635 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
144636 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
144640 pParse->eTriggerOp = 0;
144641 pParse->pTriggerTab = 0;
144656 Vdbe *v = pParse->pVdbe;
144657 sqlite3 *db = pParse->db;
144659 assert( pParse->pTriggerTab && pParse->pToplevel );
144662 for(pStep=pStepList; pStep; pStep=pStep->pNext){
144673 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
144674 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
144676 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
144677 assert( pParse->okConstFactor==0 );
144680 if( pStep->zSpan ){
144682 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
144687 switch( pStep->op ){
144691 sqlite3ExprListDup(db, pStep->pExprList, 0),
144692 sqlite3ExprDup(db, pStep->pWhere, 0),
144693 pParse->eOrconf, 0, 0, 0
144701 sqlite3SelectDup(db, pStep->pSelect, 0),
144702 sqlite3IdListDup(db, pStep->pIdList),
144703 pParse->eOrconf,
144704 sqlite3UpsertDup(db, pStep->pUpsert)
144712 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
144717 default: assert( pStep->op==TK_SELECT ); {
144719 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
144750 ** Parse context structure pFrom has just been used to create a sub-vdbe
144755 assert( pFrom->zErrMsg==0 || pFrom->nErr );
144756 assert( pTo->zErrMsg==0 || pTo->nErr );
144757 if( pTo->nErr==0 ){
144758 pTo->zErrMsg = pFrom->zErrMsg;
144759 pTo->nErr = pFrom->nErr;
144760 pTo->rc = pFrom->rc;
144762 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
144767 ** Create and populate a new TriggerPrg object with a sub-program
144777 sqlite3 *db = pParse->db; /* Database handle */
144781 NameContext sNC; /* Name context for sub-vdbe */
144782 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
144784 Parse sSubParse; /* Parse context for sub-vdbe */
144786 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
144787 assert( pTop->pVdbe );
144791 ** list of the top-level Parse object sooner rather than later. */
144794 pPrg->pNext = pTop->pTriggerPrg;
144795 pTop->pTriggerPrg = pPrg;
144796 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
144798 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
144799 pPrg->pTrigger = pTrigger;
144800 pPrg->orconf = orconf;
144801 pPrg->aColmask[0] = 0xffffffff;
144802 pPrg->aColmask[1] = 0xffffffff;
144805 ** trigger sub-program. */
144811 sSubParse.zAuthContext = pTrigger->zName;
144812 sSubParse.eTriggerOp = pTrigger->op;
144813 sSubParse.nQueryLoop = pParse->nQueryLoop;
144814 sSubParse.disableVtab = pParse->disableVtab;
144819 pTrigger->zName, onErrorText(orconf),
144820 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
144821 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
144822 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
144823 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
144824 pTab->zName
144827 if( pTrigger->zName ){
144828 sqlite3VdbeChangeP4(v, -1,
144829 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
144835 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
144837 if( pTrigger->pWhen ){
144838 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
144839 if( db->mallocFailed==0
144848 /* Code the trigger program into the sub-vdbe. */
144849 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
144851 /* Insert an OP_Halt at the end of the sub-program. */
144856 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
144859 if( pParse->nErr==0 ){
144860 assert( db->mallocFailed==0 );
144861 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
144863 pProgram->nMem = sSubParse.nMem;
144864 pProgram->nCsr = sSubParse.nTab;
144865 pProgram->token = (void *)pTrigger;
144866 pPrg->aColmask[0] = sSubParse.oldmask;
144867 pPrg->aColmask[1] = sSubParse.newmask;
144879 ** Return a pointer to a TriggerPrg object containing the sub-program for
144893 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
144899 for(pPrg=pRoot->pTriggerPrg;
144900 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
144901 pPrg=pPrg->pNext
144907 pParse->db->errByteOffset = -1;
144930 assert( pPrg || pParse->nErr );
144933 ** is a pointer to the sub-vdbe containing the trigger program. */
144935 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
144937 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
144938 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
144940 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
144942 /* Set the P5 operand of the OP_Program instruction to non-zero if
144944 ** invocation is disallowed if (a) the sub-program is really a trigger,
144959 ** operation on pTab, this function is a no-op.
144964 ** (a copy of pTab->nCol), then registers are populated as follows:
144967 ** ------------------------------------------------------
144969 ** reg+1 OLD.* value of left-most column of pTab
144971 ** reg+N OLD.* value of right-most column of pTab
144973 ** reg+N+2 NEW.* value of left-most column of pTab
144975 ** reg+N+N+1 NEW.* value of right-most column of pTab
145008 for(p=pTrigger; p; p=p->pNext){
145013 assert( p->pSchema!=0 );
145014 assert( p->pTabSchema!=0 );
145015 assert( p->pSchema==p->pTabSchema
145016 || p->pSchema==pParse->db->aDb[1].pSchema );
145023 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
145024 && p->tr_tm==tr_tm
145025 && checkColumnOverlap(p->pColumns, pChanges)
145027 if( !p->bReturning ){
145037 ** Triggers may access values stored in the old.* or new.* pseudo-table.
145038 ** This function returns a 32-bit bitmask indicating which columns of the
145043 ** Bit 0 of the returned mask is set if the left-most column of the
145075 for(p=pTrigger; p; p=p->pNext){
145076 if( p->op==op
145077 && (tr_tm&p->tr_tm)
145078 && checkColumnOverlap(p->pColumns,pChanges)
145080 if( p->bReturning ){
145086 mask |= pPrg->aColmask[isNew];
145131 ** i-th column of table pTab. This routine sets the P4 parameter of the
145137 ** command. If the latter, then the row-records in the table btree on disk
145140 ** If the former, then all row-records are guaranteed to include a value
145154 ** If column as REAL affinity and the table is an ordinary b-tree table
145164 Column *pCol = &pTab->aCol[i];
145165 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
145166 assert( i<pTab->nCol );
145169 pCol->affinity, &pValue);
145175 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
145184 ** and false if not. This is an optimization. False-positives are a
145185 ** performance degradation, but false-negatives can result in a corrupt
145188 ** aXRef[j] will be non-negative if column j of the original table is
145198 i16 iIdxCol = pIdx->aiColumn[iCol];
145204 assert( pIdx->aColExpr!=0 );
145205 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
145206 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
145214 ** to be unchanged. This is an optimization. False-positives are a
145215 ** performance degradation, but false-negatives can result in a corrupt
145218 ** aXRef[j] will be non-negative if column j of the original table is
145227 if( pIdx->pPartIdxWhere==0 ) return 0;
145228 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
145236 ** table in the source-list (pSrc->a[0]).
145240 if( pRet ) pRet->iColumn = iCol+1;
145248 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
145256 ** SELECT <other-columns>, pChanges FROM pTabList
145258 ** GROUP BY <other-columns>
145264 ** the <other-columns> in the query above are is determined by the type
145265 ** of table pTabList->a[0].pTab.
145268 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
145272 ** If the table is actually a view, then <other-columns> are all columns of
145276 ** If the table is a virtual or ordinary intkey table, then <other-columns>
145279 ** rowid value in <other-columns> is used as the integer key, and the
145299 sqlite3 *db = pParse->db;
145300 Table *pTab = pTabList->a[0].pTab;
145320 assert( pTabList->nSrc>1 );
145322 pSrc->a[0].fg.notCte = 1;
145323 pSrc->a[0].iCursor = -1;
145324 pSrc->a[0].pTab->nTabRef--;
145325 pSrc->a[0].pTab = 0;
145328 for(i=0; i<pPk->nKeyCol; i++){
145329 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
145339 for(i=0; i<pTab->nCol; i++){
145352 assert( pChanges!=0 || pParse->db->mallocFailed );
145354 for(i=0; i<pChanges->nExpr; i++){
145356 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
145363 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
145365 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
145403 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
145404 ** an expression for the i-th column of the table.
145405 ** aXRef[i]==-1 if the i-th column is not changed. */
145411 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
145413 NameContext sNC; /* The name-context to resolve expressions in */
145435 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
145447 db = pParse->db;
145448 assert( db->pParse==pParse );
145449 if( pParse->nErr ){
145452 assert( db->mallocFailed==0 );
145458 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
145480 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
145486 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
145489 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
145514 iBaseCur = iDataCur = pParse->nTab++;
145517 testcase( pPk!=0 && pPk!=pTab->pIndex );
145518 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
145520 iDataCur = pParse->nTab;
145522 pParse->nTab++;
145526 iDataCur = pUpsert->iDataCur;
145527 iIdxCur = pUpsert->iIdxCur;
145528 pParse->nTab = iBaseCur;
145530 pTabList->a[0].iCursor = iDataCur;
145535 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
145537 aRegIdx = aXRef+pTab->nCol;
145541 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
145543 /* Initialize the name-context */
145561 for(i=0; i<pChanges->nExpr; i++){
145562 u8 hCol = sqlite3StrIHash(pChanges->a[i].zEName);
145565 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
145568 for(j=0; j<pTab->nCol; j++){
145569 if( pTab->aCol[j].hName==hCol
145570 && sqlite3StrICmp(pTab->aCol[j].zCnName, pChanges->a[i].zEName)==0
145572 if( j==pTab->iPKey ){
145574 pRowidExpr = pChanges->a[i].pExpr;
145576 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
145580 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
145581 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
145582 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
145585 pTab->aCol[j].zCnName);
145593 if( j>=pTab->nCol ){
145594 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
145595 j = -1;
145597 pRowidExpr = pChanges->a[i].pExpr;
145600 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
145601 pParse->checkSchema = 1;
145608 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
145609 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
145610 db->aDb[iDb].zDbSName);
145614 aXRef[j] = -1;
145628 ** is non-negative, so the value of aXRef[] for generated columns can be
145629 ** set to any non-negative number. We use 99999 so that the value is
145632 if( pTab->tabFlags & TF_HasGenerated ){
145634 testcase( pTab->tabFlags & TF_HasVirtual );
145635 testcase( pTab->tabFlags & TF_HasStored );
145638 for(i=0; i<pTab->nCol; i++){
145640 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
145642 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
145658 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
145667 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
145672 reg = ++pParse->nMem;
145673 pParse->nMem += pIdx->nColumn;
145676 for(i=0; i<pIdx->nKeyCol; i++){
145678 reg = ++pParse->nMem;
145679 pParse->nMem += pIdx->nColumn;
145680 if( onError==OE_Default && pIdx->onError==OE_Replace ){
145690 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
145697 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
145706 ** two-pass update algorithm. */
145707 assert( aRegIdx[nAllIdx]==pParse->nMem );
145709 regOldRowid = regNewRowid = ++pParse->nMem;
145711 regOld = pParse->nMem + 1;
145712 pParse->nMem += pTab->nCol;
145715 regNewRowid = ++pParse->nMem;
145717 regNew = pParse->nMem + 1;
145718 pParse->nMem += pTab->nCol;
145723 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
145760 if( (db->flags&SQLITE_CountRows)!=0
145761 && !pParse->pTriggerTab
145762 && !pParse->nested
145763 && !pParse->bReturning
145766 regRowCount = ++pParse->nMem;
145772 iEph = pParse->nTab++;
145776 nPk = pPk ? pPk->nKeyCol : 0;
145777 iPk = pParse->nMem+1;
145778 pParse->nMem += nPk;
145779 pParse->nMem += nChangeFrom;
145780 regKey = ++pParse->nMem;
145782 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
145783 iEph = pParse->nTab++;
145784 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
145789 pKeyInfo->nAllField = nEphCol;
145823 ** Do not consider a single-pass strategy for a multi-row update if
145827 ** or index, causing a single-pass approach to malfunction. */
145829 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
145835 /* A one-pass strategy that might update more than one row may not
145851 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
145861 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
145865 aRegIdx[nAllIdx] = ++pParse->nMem;
145877 assert( pPk->aiColumn[i]>=0 );
145879 pPk->aiColumn[i], iPk+i);
145903 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
145904 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
145907 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
145985 /* Compute the old pre-UPDATE content of the row being changed, if that
145992 for(i=0; i<pTab->nCol; i++){
145993 u32 colFlags = pTab->aCol[i].colFlags;
146026 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146027 if( i==pTab->iPKey ){
146029 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
146030 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
146035 int nOff = (isView ? pTab->nCol : nPk);
146039 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
146057 if( pTab->tabFlags & TF_HasGenerated ){
146058 testcase( pTab->tabFlags & TF_HasVirtual );
146059 testcase( pTab->tabFlags & TF_HasStored );
146073 /* The row-trigger may have deleted the row being updated. In this
146075 ** required. This behavior - what happens when the row being updated
146076 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
146087 /* After-BEFORE-trigger-reload-loop:
146093 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
146096 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
146097 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
146098 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
146099 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
146104 if( pTab->tabFlags & TF_HasGenerated ){
146105 testcase( pTab->tabFlags & TF_HasVirtual );
146106 testcase( pTab->tabFlags & TF_HasStored );
146138 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
146151 ** to invoke the pre-update hook.
146154 ** pre-update hook. If the caller invokes preupdate_new(), the returned
146168 if( !pParse->nested ){
146209 /* Nothing to do at end-of-loop for a single-pass */
146223 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
146261 ** There are two possible strategies - the default and the special
146289 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
146292 sqlite3 *db = pParse->db; /* Database connection */
146295 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
146299 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
146308 ephemTab = pParse->nTab++;
146310 regArg = pParse->nMem + 1;
146311 pParse->nMem += nArg;
146312 if( pSrc->nSrc>1 ){
146326 assert( pPk->nKeyCol==1 );
146327 iPk = pPk->aiColumn[0];
146329 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
146336 for(i=0; i<pTab->nCol; i++){
146339 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
146350 regRec = ++pParse->nMem;
146351 regRowid = ++pParse->nMem;
146360 for(i=0; i<pTab->nCol; i++){
146361 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
146363 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
146381 assert( pPk->nKeyCol==1 );
146382 iPk = pPk->aiColumn[0];
146393 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
146404 ** accept no-change records with serial_type 10 */
146415 if( pSrc->nSrc==1 ){
146448 ** 2018-04-12
146469 Upsert *pNext = p->pNextUpsert;
146470 sqlite3ExprListDelete(db, p->pUpsertTarget);
146471 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
146472 sqlite3ExprListDelete(db, p->pUpsertSet);
146473 sqlite3ExprDelete(db, p->pUpsertWhere);
146474 sqlite3DbFree(db, p->pToFree);
146490 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
146491 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
146492 sqlite3ExprListDup(db, p->pUpsertSet, 0),
146493 sqlite3ExprDup(db, p->pUpsertWhere, 0),
146494 sqlite3UpsertDup(db, p->pNextUpsert)
146519 pNew->pUpsertTarget = pTarget;
146520 pNew->pUpsertTargetWhere = pTargetWhere;
146521 pNew->pUpsertSet = pSet;
146522 pNew->pUpsertWhere = pWhere;
146523 pNew->isDoUpdate = pSet!=0;
146524 pNew->pNextUpsert = pNext;
146531 ** symbols in the conflict-target.
146545 ExprList *pTarget; /* The conflict-target clause */
146546 Expr *pTerm; /* One term of the conflict-target clause */
146551 assert( pTabList->nSrc==1 );
146552 assert( pTabList->a[0].pTab!=0 );
146554 assert( pUpsert->pUpsertTarget!=0 );
146556 /* Resolve all symbolic names in the conflict-target clause, which
146557 ** includes both the list of columns and the optional partial-index
146563 for(; pUpsert && pUpsert->pUpsertTarget;
146564 pUpsert=pUpsert->pNextUpsert, nClause++){
146565 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
146567 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
146571 pTab = pTabList->a[0].pTab;
146572 pTarget = pUpsert->pUpsertTarget;
146573 iCursor = pTabList->a[0].iCursor;
146575 && pTarget->nExpr==1
146576 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
146577 && pTerm->iColumn==XN_ROWID
146579 /* The conflict-target is the rowid of the primary table */
146580 assert( pUpsert->pUpsertIdx==0 );
146588 ** prior to comparing against the conflict-target expression.
146594 sCol[1].iTable = pTabList->a[0].iCursor;
146597 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
146600 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
146601 if( pIdx->pPartIdxWhere ){
146602 if( pUpsert->pUpsertTargetWhere==0 ) continue;
146603 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
146604 pIdx->pPartIdxWhere, iCursor)!=0 ){
146608 nn = pIdx->nKeyCol;
146611 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
146612 if( pIdx->aiColumn[ii]==XN_EXPR ){
146613 assert( pIdx->aColExpr!=0 );
146614 assert( pIdx->aColExpr->nExpr>ii );
146615 pExpr = pIdx->aColExpr->a[ii].pExpr;
146616 if( pExpr->op!=TK_COLLATE ){
146622 sCol[1].iColumn = pIdx->aiColumn[ii];
146626 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
146640 pUpsert->pUpsertIdx = pIdx;
146643 if( pUpsert->pUpsertIdx==0 ){
146645 if( nClause==0 && pUpsert->pNextUpsert==0 ){
146666 pNext = pUpsert->pNextUpsert;
146668 if( pNext->pUpsertTarget==0 ) return 1;
146669 if( pNext->pUpsertIdx==0 ) return 1;
146682 && pUpsert->pUpsertTarget!=0
146683 && pUpsert->pUpsertIdx!=pIdx
146685 pUpsert = pUpsert->pNextUpsert;
146694 ** In this case parameter iCur is a cursor open on the table b-tree that
146700 Parse *pParse, /* The parsing and code-generating context */
146706 Vdbe *v = pParse->pVdbe;
146707 sqlite3 *db = pParse->db;
146715 iDataCur = pUpsert->iDataCur;
146727 int nPk = pPk->nKeyCol;
146728 int iPk = pParse->nMem+1;
146729 pParse->nMem += nPk;
146732 assert( pPk->aiColumn[i]>=0 );
146733 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
146735 VdbeComment((v, "%s.%s", pIdx->zName,
146736 pTab->aCol[pPk->aiColumn[i]].zCnName));
146747 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
146749 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
146751 for(i=0; i<pTab->nCol; i++){
146752 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
146753 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
146756 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
146757 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
146801 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
146873 if( pParse->nErr ) goto build_vacuum_end;
146882 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
146883 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
146886 iDb = sqlite3FindDb(pParse->db, pNm);
146893 iIntoReg = ++pParse->nMem;
146900 sqlite3ExprDelete(pParse->db, pInto);
146908 char **pzErrMsg, /* Write error message here */
146916 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
146917 u64 saved_flags; /* Saved value of db->flags */
146918 i64 saved_nChange; /* Saved value of db->nChange */
146919 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
146920 u32 saved_openFlags; /* Saved value of db->openFlags */
146929 if( !db->autoCommit ){
146931 return SQLITE_ERROR; /* IMP: R-12218-18073 */
146933 if( db->nVdbeActive>1 ){
146934 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
146935 return SQLITE_ERROR; /* IMP: R-15610-35227 */
146937 saved_openFlags = db->openFlags;
146940 sqlite3SetString(pzErrMsg, db, "non-text filename");
146944 db->openFlags &= ~SQLITE_OPEN_READONLY;
146945 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
146951 ** restored before returning. Then set the writable-schema flag, and
146953 saved_flags = db->flags;
146954 saved_mDbFlags = db->mDbFlags;
146955 saved_nChange = db->nChange;
146956 saved_nTotalChange = db->nTotalChange;
146957 saved_mTrace = db->mTrace;
146958 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
146959 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
146960 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
146962 db->mTrace = 0;
146964 zDbMain = db->aDb[iDb].zDbSName;
146965 pMain = db->aDb[iDb].pBt;
146974 ** An optimisation would be to use a non-journaled pager.
146982 nDb = db->nDb;
146984 db->openFlags = saved_openFlags;
146986 assert( (db->nDb-1)==nDb );
146987 pDb = &db->aDb[nDb];
146988 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
146989 pTemp = pDb->pBt;
146993 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
146998 db->mDbFlags |= DBFLAG_VacuumInto;
147002 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
147008 ** to ensure that we do not try to change the page-size on a WAL database.
147020 db->nextPagesize = 0;
147024 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
147025 || NEVER(db->mallocFailed)
147032 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
147039 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
147053 db->init.iDb = 0;
147066 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
147067 db->mDbFlags &= ~DBFLAG_Vacuum;
147086 ** both transactions are closed by this block - the main database
147140 /* Restore the original value of db->flags */
147141 db->init.iDb = 0;
147142 db->mDbFlags = saved_mDbFlags;
147143 db->flags = saved_flags;
147144 db->nChange = saved_nChange;
147145 db->nTotalChange = saved_nTotalChange;
147146 db->mTrace = saved_mTrace;
147147 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
147156 db->autoCommit = 1;
147159 sqlite3BtreeClose(pDb->pBt);
147160 pDb->pBt = 0;
147161 pDb->pSchema = 0;
147164 /* This both clears the schemas and reduces the size of the db->aDb[]
147235 pMod->zName = zCopy;
147236 pMod->pModule = pModule;
147237 pMod->pAux = pAux;
147238 pMod->xDestroy = xDestroy;
147239 pMod->pEpoTab = 0;
147240 pMod->nRefModule = 1;
147242 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
147270 sqlite3_mutex_enter(db->mutex);
147274 sqlite3_mutex_leave(db->mutex);
147280 ** External API function used to create a new virtual-table module.
147295 ** External API function used to create a new virtual-table module.
147311 ** External API to drop all virtual-table modules, except those named
147319 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
147324 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
147327 createModule(db, pMod->zName, 0, 0, 0);
147337 assert( pMod->nRefModule>0 );
147338 pMod->nRefModule--;
147339 if( pMod->nRefModule==0 ){
147340 if( pMod->xDestroy ){
147341 pMod->xDestroy(pMod->pAux);
147343 assert( pMod->pEpoTab==0 );
147357 pVTab->nRef++;
147362 ** pTab is a pointer to a Table structure representing a virtual-table.
147364 ** this virtual-table, if one has been created, or NULL otherwise.
147369 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
147374 ** Decrement the ref-count on a virtual table object. When the ref-count
147378 sqlite3 *db = pVTab->db;
147381 assert( pVTab->nRef>0 );
147382 assert( db->eOpenState==SQLITE_STATE_OPEN
147383 || db->eOpenState==SQLITE_STATE_ZOMBIE );
147385 pVTab->nRef--;
147386 if( pVTab->nRef==0 ){
147387 sqlite3_vtab *p = pVTab->pVtab;
147388 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
147390 p->pModule->xDisconnect(p);
147398 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
147401 ** connection db is left in the p->u.vtab.p list.
147408 pVTable = p->u.vtab.p;
147409 p->u.vtab.p = 0;
147415 ** database connection that may have an entry in the p->u.vtab.p list.
147417 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
147420 sqlite3 *db2 = pVTable->db;
147421 VTable *pNext = pVTable->pNext;
147425 p->u.vtab.p = pRet;
147426 pRet->pNext = 0;
147428 pVTable->pNext = db2->pDisconnect;
147429 db2->pDisconnect = pVTable;
147441 ** list in p->pVTab. It also decrements the VTable ref count. This is
147444 ** be being used by other shared-cache connections).
147451 assert( sqlite3_mutex_held(db->mutex) );
147453 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
147454 if( (*ppVTab)->db==db ){
147456 *ppVTab = pVTab->pNext;
147468 ** shared b-tree databases opened using connection db are held by the
147478 ** or, if the virtual table is stored in a non-sharable database, then
147482 ** by multiple threads. It is thread-safe.
147485 VTable *p = db->pDisconnect;
147488 assert( sqlite3_mutex_held(db->mutex) );
147491 db->pDisconnect = 0;
147494 VTable *pNext = p->pNext;
147502 ** Clear any and all virtual-table information from the Table record.
147506 ** Since it is a virtual-table, the Table structure contains a pointer
147517 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
147518 if( p->u.vtab.azArg ){
147520 for(i=0; i<p->u.vtab.nArg; i++){
147521 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
147523 sqlite3DbFree(db, p->u.vtab.azArg);
147528 ** Add a new module argument to pTable->u.vtab.azArg[].
147529 ** The string is not copied - the pointer is stored. The
147536 sqlite3 *db = pParse->db;
147539 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
147540 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
147541 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
147543 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
147547 int i = pTable->u.vtab.nArg++;
147550 pTable->u.vtab.azArg = azModuleArg;
147570 pTable = pParse->pNewTable;
147572 assert( 0==pTable->pIndex );
147573 pTable->eTabType = TABTYP_VTAB;
147575 db = pParse->db;
147577 assert( pTable->u.vtab.nArg==0 );
147580 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
147581 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
147582 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
147584 pParse->sNameToken.n = (int)(
147585 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
147594 if( pTable->u.vtab.azArg ){
147595 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
147597 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
147598 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
147605 ** in pParse->zArg[] and appends it to the list of arguments on the
147606 ** virtual table currently under construction in pParse->pTable.
147609 if( pParse->sArg.z && pParse->pNewTable ){
147610 const char *z = (const char*)pParse->sArg.z;
147611 int n = pParse->sArg.n;
147612 sqlite3 *db = pParse->db;
147613 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
147622 Table *pTab = pParse->pNewTable; /* The table being constructed */
147623 sqlite3 *db = pParse->db; /* The database connection */
147628 pParse->sArg.z = 0;
147629 if( pTab->u.vtab.nArg<1 ) return;
147637 if( !db->init.busy ){
147648 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
147650 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
147656 ** The VM register number pParse->regRowid holds the rowid of an
147660 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147665 db->aDb[iDb].zDbSName,
147666 pTab->zName,
147667 pTab->zName,
147669 pParse->regRowid
147675 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
147679 iReg = ++pParse->nMem;
147680 sqlite3VdbeLoadString(v, iReg, pTab->zName);
147683 /* If we are rereading the sqlite_schema table create the in-memory
147686 Schema *pSchema = pTab->pSchema;
147687 const char *zName = pTab->zName;
147690 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
147696 pParse->pNewTable = 0;
147706 pParse->sArg.z = 0;
147707 pParse->sArg.n = 0;
147715 Token *pArg = &pParse->sArg;
147716 if( pArg->z==0 ){
147717 pArg->z = p->z;
147718 pArg->n = p->n;
147720 assert(pArg->z <= p->z);
147721 pArg->n = (int)(&p->z[p->n] - pArg->z);
147741 int nArg = pTab->u.vtab.nArg;
147748 azArg = (const char *const*)pTab->u.vtab.azArg;
147750 /* Check that the virtual-table is not already being initialized */
147751 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
147752 if( pCtx->pTab==pTab ){
147754 "vtable constructor called recursively: %s", pTab->zName
147760 zModuleName = sqlite3DbStrDup(db, pTab->zName);
147771 pVTable->db = db;
147772 pVTable->pMod = pMod;
147773 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
147775 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
147776 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
147779 assert( &db->pVtabCtx );
147783 sCtx.pPrior = db->pVtabCtx;
147785 db->pVtabCtx = &sCtx;
147786 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
147787 db->pVtabCtx = sCtx.pPrior;
147799 }else if( ALWAYS(pVTable->pVtab) ){
147802 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
147803 pVTable->pVtab->pModule = pMod->pModule;
147804 pMod->nRefModule++;
147805 pVTable->nRef = 1;
147808 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
147815 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
147819 pVTable->pNext = pTab->u.vtab.p;
147820 pTab->u.vtab.p = pVTable;
147822 for(iCol=0; iCol<pTab->nCol; iCol++){
147823 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
147829 && (i==0 || zType[i-1]==' ')
147842 assert(zType[i-1]==' ');
147843 zType[i-1] = '\0';
147845 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
147846 pTab->tabFlags |= TF_HasHidden;
147849 pTab->tabFlags |= oooHidden;
147864 ** This call is a no-op if table pTab is not a virtual table.
147867 sqlite3 *db = pParse->db;
147879 zMod = pTab->u.vtab.azArg[0];
147880 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147883 const char *zModule = pTab->u.vtab.azArg[0];
147888 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
147891 pParse->rc = rc;
147899 ** Grow the db->aVTrans[] array so that there is room for at least one
147900 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
147906 if( (db->nVTrans%ARRAY_INCR)==0 ){
147909 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
147910 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
147914 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
147915 db->aVTrans = aVTrans;
147927 db->aVTrans[db->nVTrans++] = pVTab;
147945 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
147946 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
147949 zMod = pTab->u.vtab.azArg[0];
147950 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
147956 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
147960 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
147992 sqlite3_mutex_enter(db->mutex);
147993 pCtx = db->pVtabCtx;
147994 if( !pCtx || pCtx->bDeclared ){
147996 sqlite3_mutex_leave(db->mutex);
147999 pTab = pCtx->pTab;
148006 ** schema. Nevertheless, defend against that (turn off db->init.busy)
148008 assert( db->init.busy==0 );
148009 initBusy = db->init.busy;
148010 db->init.busy = 0;
148014 && ALWAYS(!db->mallocFailed)
148018 if( !pTab->aCol ){
148021 pTab->aCol = pNew->aCol;
148022 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
148023 pTab->nNVCol = pTab->nCol = pNew->nCol;
148024 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
148025 pNew->nCol = 0;
148026 pNew->aCol = 0;
148027 assert( pTab->pIndex==0 );
148030 && pCtx->pVTable->pMod->pModule->xUpdate!=0
148031 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
148033 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
148034 ** or else must have a single-column PRIMARY KEY */
148037 pIdx = pNew->pIndex;
148039 assert( pIdx->pNext==0 );
148040 pTab->pIndex = pIdx;
148041 pNew->pIndex = 0;
148042 pIdx->pTable = pTab;
148045 pCtx->bDeclared = 1;
148059 db->init.busy = initBusy;
148063 sqlite3_mutex_leave(db->mutex);
148072 ** This call is a no-op if zTab is not a virtual table.
148078 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
148081 && ALWAYS(pTab->u.vtab.p!=0)
148085 for(p=pTab->u.vtab.p; p; p=p->pNext){
148086 assert( p->pVtab );
148087 if( p->pVtab->nRef>0 ){
148092 xDestroy = p->pMod->pModule->xDestroy;
148093 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
148095 pTab->nTabRef++;
148096 rc = xDestroy(p->pVtab);
148099 assert( pTab->u.vtab.p==p && p->pNext==0 );
148100 p->pVtab = 0;
148101 pTab->u.vtab.p = 0;
148120 if( db->aVTrans ){
148121 VTable **aVTrans = db->aVTrans;
148122 db->aVTrans = 0;
148123 for(i=0; i<db->nVTrans; i++){
148125 sqlite3_vtab *p = pVTab->pVtab;
148128 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
148131 pVTab->iSavepoint = 0;
148135 db->nVTrans = 0;
148144 ** If an error message is available, leave it in p->zErrMsg.
148149 VTable **aVTrans = db->aVTrans;
148151 db->aVTrans = 0;
148152 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
148154 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
148155 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
148160 db->aVTrans = aVTrans;
148194 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
148205 pModule = pVTab->pVtab->pModule;
148207 if( pModule->xBegin ){
148211 for(i=0; i<db->nVTrans; i++){
148212 if( db->aVTrans[i]==pVTab ){
148221 rc = pModule->xBegin(pVTab->pVtab);
148223 int iSvpt = db->nStatement + db->nSavepoint;
148225 if( iSvpt && pModule->xSavepoint ){
148226 pVTab->iSavepoint = iSvpt;
148227 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
148254 assert( iSavepoint>=-1 );
148255 if( db->aVTrans ){
148257 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
148258 VTable *pVTab = db->aVTrans[i];
148259 const sqlite3_module *pMod = pVTab->pMod->pModule;
148260 if( pVTab->pVtab && pMod->iVersion>=2 ){
148265 xMethod = pMod->xSavepoint;
148266 pVTab->iSavepoint = iSavepoint+1;
148269 xMethod = pMod->xRollbackTo;
148272 xMethod = pMod->xRelease;
148275 if( xMethod && pVTab->iSavepoint>iSavepoint ){
148276 rc = xMethod(pVTab->pVtab, iSavepoint);
148314 if( pExpr->op!=TK_COLUMN ) return pDef;
148316 pTab = pExpr->y.pTab;
148319 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
148321 assert( pVtab->pModule!=0 );
148322 pMod = (sqlite3_module *)pVtab->pModule;
148323 if( pMod->xFindFunction==0 ) return pDef;
148329 ** with an all lower-case function name. Continue in this tradition to
148335 for(i=0; pDef->zName[i]; i++){
148336 unsigned char x = (unsigned char)pDef->zName[i];
148341 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
148349 + sqlite3Strlen30(pDef->zName) + 1);
148354 pNew->zName = (const char*)&pNew[1];
148355 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
148356 pNew->xSFunc = xSFunc;
148357 pNew->pUserData = pArg;
148358 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
148363 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
148366 ** is a no-op.
148374 for(i=0; i<pToplevel->nVtabLock; i++){
148375 if( pTab==pToplevel->apVtabLock[i] ) return;
148377 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
148378 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
148380 pToplevel->apVtabLock = apVtabLock;
148381 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
148383 sqlite3OomFault(pToplevel->db);
148390 ** exist. Return non-zero if either the eponymous virtual table instance
148392 ** and an error message was left in pParse.
148397 ** instances always exist. They cannot be DROP-ed.
148403 const sqlite3_module *pModule = pMod->pModule;
148407 sqlite3 *db = pParse->db;
148408 if( pMod->pEpoTab ) return 1;
148409 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
148412 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
148413 if( pTab->zName==0 ){
148417 pMod->pEpoTab = pTab;
148418 pTab->nTabRef = 1;
148419 pTab->eTabType = TABTYP_VTAB;
148420 pTab->pSchema = db->aDb[0].pSchema;
148421 assert( pTab->u.vtab.nArg==0 );
148422 pTab->iPKey = -1;
148423 pTab->tabFlags |= TF_Eponymous;
148424 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148426 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
148427 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
148441 Table *pTab = pMod->pEpoTab;
148446 pTab->tabFlags |= TF_Ephemeral;
148448 pMod->pEpoTab = 0;
148468 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
148469 return (int)aMap[db->vtabOnConflict-1];
148485 sqlite3_mutex_enter(db->mutex);
148486 p = db->pVtabCtx;
148490 assert( p->pTab==0 || IsVirtual(p->pTab) );
148494 p->pVTable->bConstraint = (u8)va_arg(ap, int);
148498 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
148502 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
148514 sqlite3_mutex_leave(db->mutex);
148523 ** 2015-06-06
148536 ** This file was split off from where.c on 2015-06-06 in order to reduce the
148545 ** 2013-11-12
148614 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
148622 int addrSkip; /* Jump here for next iteration of skip-scan */
148626 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
148627 int addrBignull; /* Jump here for next part of big-null scan */
148637 union { /* Information that depends on pWLoop->wsFlags */
148643 int iBase; /* Base register of multi-key index record */
148647 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
148661 ** prevent a query solution - which is an error) and many terms of the
148663 ** potential way of implementing that FROM-clause term, together with
148679 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
148722 ** correspond to the subquery(s) of OR-clause processing. Only the
148738 ** vector, not a scalar, and because dependencies are many-to-one, not
148739 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
148755 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
148768 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
148799 ** spread out over the non-negative integers. For example, the cursor
148818 int iParent; /* Disable pWC->a[iParent] when this term disabled */
148828 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
148841 #define TERM_OK 0x0040 /* Used during OR-clause processing */
148847 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
148854 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
148865 int k; /* Resume scanning at this->pWC->a[this->k] */
148871 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
148893 int nBase; /* Number of terms through the last non-Virtual */
148931 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
148932 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
148939 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
148940 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
148979 ** routine. The limit is high enough that is should not impact real-world
149031 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
149040 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
149055 ** Private interfaces - callable only by other where.c routines.
149107 int iLevel, /* Which level of pWInfo->a[] should be coded */
149135 ** OR-ed combination of these values can be used when searching for
149147 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
149148 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
149149 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
149150 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
149154 #define WO_OR 0x0200 /* Two or more OR-connected terms */
149155 #define WO_AND 0x0400 /* Two or more AND-connected terms */
149158 #define WO_ROWVAL 0x2000 /* A row-value term */
149161 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
149176 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
149184 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
149189 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
149191 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
149203 ** Return the name of the i-th column of the pIdx index.
149206 i = pIdx->aiColumn[i];
149209 return pIdx->pTable->aCol[i].zCnName;
149224 int iTerm, /* Zero-based index of first term. */
149225 int bAnd, /* Non-zero to append " AND " */
149265 Index *pIndex = pLoop->u.btree.pIndex;
149266 u16 nEq = pLoop->u.btree.nEq;
149267 u16 nSkip = pLoop->nSkip;
149270 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
149279 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
149280 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
149283 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
149284 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
149290 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
149292 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
149306 if( sqlite3ParseToplevel(pParse)->explain==2 )
149309 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
149310 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149311 sqlite3 *db = pParse->db; /* Database handle */
149319 pLoop = pLevel->pWLoop;
149320 flags = pLoop->wsFlags;
149324 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
149334 assert( pLoop->u.btree.pIndex!=0 );
149335 pIdx = pLoop->u.btree.pIndex;
149337 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
149352 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
149358 const Table *pTab = pItem->pTab;
149359 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
149381 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
149384 if( pItem->fg.jointype & JT_LEFT ){
149385 sqlite3_str_appendf(&str, " LEFT-JOIN");
149388 if( pLoop->nOut>=10 ){
149390 sqlite3LogEstToInt(pLoop->nOut));
149398 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
149408 ** required and this routine is a no-op.
149419 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
149420 Vdbe *v = pParse->pVdbe; /* VM being constructed */
149421 sqlite3 *db = pParse->db; /* Database handle */
149431 pLoop = pLevel->pWLoop;
149432 if( pLoop->wsFlags & WHERE_IPK ){
149433 const Table *pTab = pItem->pTab;
149434 if( pTab->iPKey>=0 ){
149435 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
149440 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
149441 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
149442 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
149449 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
149471 WhereLoop *pLoop = pLvl->pWLoop;
149472 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
149473 zObj = pLoop->u.btree.pIndex->zName;
149475 zObj = pSrclist->a[pLvl->iFrom].zName;
149478 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
149524 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
149529 while( (pTerm->wtFlags & TERM_CODED)==0
149530 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
149531 && (pLevel->notReady & pTerm->prereqAll)==0
149533 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
149534 pTerm->wtFlags |= TERM_LIKECOND;
149536 pTerm->wtFlags |= TERM_CODED;
149540 sqlite3DebugPrintf("DISABLE-");
149541 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
149544 if( pTerm->iParent<0 ) break;
149545 pTerm = &pTerm->pWC->a[pTerm->iParent];
149547 pTerm->nChild--;
149548 if( pTerm->nChild!=0 ) break;
149558 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
149565 Vdbe *v = pParse->pVdbe;
149567 assert( pParse->db->mallocFailed );
149577 n--;
149581 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
149582 n--;
149653 sqlite3 *db = pParse->db;
149656 if( db->mallocFailed==0 ){
149665 pOrigRhs = pNew->x.pSelect->pEList;
149666 assert( pNew->pLeft!=0 );
149667 assert( ExprUseXList(pNew->pLeft) );
149668 pOrigLhs = pNew->pLeft->x.pList;
149669 for(i=iEq; i<pLoop->nLTerm; i++){
149670 if( pLoop->aLTerm[i]->pExpr==pX ){
149672 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
149673 iField = pLoop->aLTerm[i]->u.x.iField - 1;
149674 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
149675 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
149676 pOrigRhs->a[iField].pExpr = 0;
149677 assert( pOrigLhs->a[iField].pExpr!=0 );
149678 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
149679 pOrigLhs->a[iField].pExpr = 0;
149684 pNew->pLeft->x.pList = pLhs;
149685 pNew->x.pSelect->pEList = pRhs;
149686 if( pLhs && pLhs->nExpr==1 ){
149690 Expr *p = pLhs->a[0].pExpr;
149691 pLhs->a[0].pExpr = 0;
149692 sqlite3ExprDelete(db, pNew->pLeft);
149693 pNew->pLeft = p;
149695 pSelect = pNew->x.pSelect;
149696 if( pSelect->pOrderBy ){
149698 ** iOrderByCol variables. These are set to non-zero when an
149700 ** result-set. Since the result-set of the SELECT statement may
149704 ExprList *pOrderBy = pSelect->pOrderBy;
149705 for(i=0; i<pOrderBy->nExpr; i++){
149706 pOrderBy->a[i].u.x.iOrderByCol = 0;
149733 ** straight-line code. For constraints of the form X IN (...)
149741 int bRev, /* True for reverse-order IN operations */
149744 Expr *pX = pTerm->pExpr;
149745 Vdbe *v = pParse->pVdbe;
149748 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
149750 if( pX->op==TK_EQ || pX->op==TK_IS ){
149751 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
149752 }else if( pX->op==TK_ISNULL ){
149760 WhereLoop *pLoop = pLevel->pWLoop;
149765 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
149766 && pLoop->u.btree.pIndex!=0
149767 && pLoop->u.btree.pIndex->aSortOrder[iEq]
149773 assert( pX->op==TK_IN );
149777 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
149782 for(i=iEq;i<pLoop->nLTerm; i++){
149783 assert( pLoop->aLTerm[i]!=0 );
149784 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
149788 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
149791 Expr *pExpr = pTerm->pExpr;
149792 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
149793 sqlite3 *db = pParse->db;
149795 if( !db->mallocFailed ){
149796 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149798 pExpr->iTable = iTab;
149802 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
149816 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
149817 pLoop->wsFlags |= WHERE_IN_ABLE;
149818 if( pLevel->u.in.nIn==0 ){
149819 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
149821 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
149822 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
149825 i = pLevel->u.in.nIn;
149826 pLevel->u.in.nIn += nEq;
149827 pLevel->u.in.aInLoop =
149828 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
149829 pLevel->u.in.aInLoop,
149830 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
149831 pIn = pLevel->u.in.aInLoop;
149835 for(i=iEq;i<pLoop->nLTerm; i++){
149836 if( pLoop->aLTerm[i]->pExpr==pX ){
149837 int iOut = iReg + i - iEq;
149839 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
149842 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
149846 pIn->iCur = iTab;
149847 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
149849 pIn->iBase = iReg - i;
149850 pIn->nPrefix = i;
149852 pIn->nPrefix = 0;
149855 pIn->eEndLoopOp = OP_Noop;
149861 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
149862 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
149864 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
149866 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
149869 pLevel->u.in.nIn = 0;
149871 sqlite3DbFree(pParse->db, aiMap);
149882 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
149884 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
149885 || (pTerm->eOperator & WO_EQUIV)==0
149906 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
149907 ** The only thing it does is allocate the pLevel->iMem memory cell and
149946 u16 nSkip; /* Number of left-most columns to skip */
149947 Vdbe *v = pParse->pVdbe; /* The vm under construction */
149957 pLoop = pLevel->pWLoop;
149958 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
149959 nEq = pLoop->u.btree.nEq;
149960 nSkip = pLoop->nSkip;
149961 pIdx = pLoop->u.btree.pIndex;
149966 regBase = pParse->nMem + 1;
149967 nReg = pLoop->u.btree.nEq + nExtraReg;
149968 pParse->nMem += nReg;
149970 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
149971 assert( zAff!=0 || pParse->db->mallocFailed );
149974 int iIdxCur = pLevel->iIdxCur;
149975 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
149979 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
149981 assert( pLevel->addrSkip==0 );
149982 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
149989 testcase( pIdx->aiColumn[j]==XN_EXPR );
149999 pTerm = pLoop->aLTerm[j];
150003 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
150004 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150016 pTerm = pLoop->aLTerm[j];
150017 if( pTerm->eOperator & WO_IN ){
150018 if( pTerm->pExpr->flags & EP_xIsSelect ){
150025 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
150026 Expr *pRight = pTerm->pExpr->pRight;
150027 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
150028 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
150031 if( pParse->nErr==0 ){
150032 assert( pParse->db->mallocFailed==0 );
150062 ** becomes a no-op.
150069 if( pTerm->wtFlags & TERM_LIKEOPT ){
150071 assert( pLevel->iLikeRepCntr>0 );
150072 pOp = sqlite3VdbeGetOp(v, -1);
150074 assert( pOp->opcode==OP_String8
150075 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
150076 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
150077 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
150100 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
150103 struct CCurHint *pHint = pWalker->u.pCCurHint;
150104 assert( pHint->pIdx!=0 );
150105 if( pExpr->op==TK_COLUMN
150106 && pExpr->iTable==pHint->iTabCur
150107 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
150109 pWalker->eCode = 1;
150116 ** should be included in the cursor-hint for a table that is on the rhs
150117 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
150130 if( pExpr->op==TK_IS
150131 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
150132 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
150134 pWalker->eCode = 1;
150135 }else if( pExpr->op==TK_FUNCTION ){
150138 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
150139 pWalker->eCode = 1;
150166 struct CCurHint *pHint = pWalker->u.pCCurHint;
150167 if( pExpr->op==TK_COLUMN ){
150168 if( pExpr->iTable!=pHint->iTabCur ){
150169 int reg = ++pWalker->pParse->nMem; /* Register for column value */
150170 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
150171 pExpr->op = TK_REGISTER;
150172 pExpr->iTable = reg;
150173 }else if( pHint->pIdx!=0 ){
150174 pExpr->iTable = pHint->iIdxCur;
150175 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
150176 assert( pExpr->iColumn>=0 );
150178 }else if( pExpr->op==TK_AGG_FUNCTION ){
150180 ** be a correlated sub-query, and expression pExpr is an aggregate from
150198 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
150200 Parse *pParse = pWInfo->pParse;
150201 sqlite3 *db = pParse->db;
150202 Vdbe *v = pParse->pVdbe;
150204 WhereLoop *pLoop = pLevel->pWLoop;
150213 iCur = pLevel->iTabCur;
150214 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
150216 sHint.iIdxCur = pLevel->iIdxCur;
150217 sHint.pIdx = pLoop->u.btree.pIndex;
150221 pWC = &pWInfo->sWC;
150222 for(i=0; i<pWC->nBase; i++){
150223 pTerm = &pWC->a[i];
150224 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
150225 if( pTerm->prereqAll & pLevel->notReady ) continue;
150229 ** from the cursor-hint.
150249 if( pTabItem->fg.jointype & JT_LEFT ){
150250 Expr *pExpr = pTerm->pExpr;
150252 || pExpr->w.iJoin!=pTabItem->iCursor
150256 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150260 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
150263 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
150266 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
150267 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
150268 if( j<pLoop->nLTerm ) continue;
150271 /* No subqueries or non-deterministic functions allowed */
150272 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
150279 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
150284 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
150295 # define codeCursorHint(A,B,C,D) /* No-op */
150299 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
150310 ** However, if the scan currently being coded is a branch of an OR-loop and
150327 int iCur, /* Cursor for IPK b-tree */
150330 Parse *pParse = pWInfo->pParse; /* Parse context */
150331 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
150334 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
150336 pWInfo->bDeferredSeek = 1;
150338 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
150339 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
150342 Table *pTab = pIdx->pTable;
150343 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
150345 ai[0] = pTab->nCol;
150346 for(i=0; i<pIdx->nColumn-1; i++){
150348 assert( pIdx->aiColumn[i]<pTab->nCol );
150349 x1 = pIdx->aiColumn[i];
150354 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
150373 Vdbe *v = pParse->pVdbe;
150375 assert( p->op==TK_SELECT );
150377 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
150384 pList = p->x.pList;
150385 assert( nReg<=pList->nExpr );
150387 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
150391 assert( nReg==1 || pParse->nErr );
150415 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
150417 pNew->pNext = pTrans->pWInfo->pExprMods;
150418 pTrans->pWInfo->pExprMods = pNew;
150419 pNew->pExpr = pExpr;
150420 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
150430 IdxExprTrans *pX = p->u.pIdxTrans;
150431 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
150434 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
150435 pExpr->op = TK_COLUMN;
150436 pExpr->iTable = pX->iIdxCur;
150437 pExpr->iColumn = pX->iIdxCol;
150440 pExpr->y.pTab = 0;
150452 if( pExpr->op==TK_COLUMN ){
150453 IdxExprTrans *pX = p->u.pIdxTrans;
150454 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
150455 assert( ExprUseYTab(pExpr) && pExpr->y.pTab!=0 );
150457 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
150458 pExpr->iTable = pX->iIdxCur;
150459 pExpr->iColumn = pX->iIdxCol;
150460 pExpr->y.pTab = 0;
150472 ** 2019-10-24: Updated to also translate references to a VIRTUAL column in
150487 aColExpr = pIdx->aColExpr;
150488 if( aColExpr==0 && !pIdx->bHasVCol ){
150493 pTab = pIdx->pTable;
150499 x.db = pWInfo->pParse->db;
150500 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
150501 i16 iRef = pIdx->aiColumn[iIdxCol];
150503 assert( aColExpr!=0 && aColExpr->a[iIdxCol].pExpr!=0 );
150504 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
150509 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
150510 && ((pTab->aCol[iRef].colFlags & COLFLAG_HASCOLL)==0
150511 || sqlite3StrICmp(sqlite3ColumnColl(&pTab->aCol[iRef]),
150516 ** out of the index is an optimization only - the main table is always
150519 ** the column is non-standard */
150527 sqlite3WalkExpr(&w, pWInfo->pWhere);
150528 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
150529 sqlite3WalkExprList(&w, pWInfo->pResultSet);
150547 while( pTruth->op==TK_AND ){
150548 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
150549 pTruth = pTruth->pRight;
150551 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
150553 if( pTerm->wtFlags & TERM_CODED ) continue;
150554 pExpr = pTerm->pExpr;
150556 pTerm->wtFlags |= TERM_CODED;
150566 ** inner-loop Bloom filters, then evaluate those filters now, before the
150572 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
150579 int iLevel, /* Which level of pWInfo->a[] should be coded */
150583 while( ++iLevel < pWInfo->nLevel ){
150584 WhereLevel *pLevel = &pWInfo->a[iLevel];
150585 WhereLoop *pLoop = pLevel->pWLoop;
150586 if( pLevel->regFilter==0 ) continue;
150587 if( pLevel->pWLoop->nSkip ) continue;
150588 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
150589 ** vvvvv--' pLevel->regFilter if this were true. */
150590 if( NEVER(pLoop->prereq & notReady) ) continue;
150591 assert( pLevel->addrBrk==0 );
150592 pLevel->addrBrk = addrNxt;
150593 if( pLoop->wsFlags & WHERE_IPK ){
150594 WhereTerm *pTerm = pLoop->aLTerm[0];
150597 assert( pTerm->pExpr!=0 );
150598 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150601 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150603 VdbeCoverage(pParse->pVdbe);
150605 u16 nEq = pLoop->u.btree.nEq;
150609 assert( pLoop->wsFlags & WHERE_INDEXED );
150610 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
150613 sqlite3DbFree(pParse->db, zStartAff);
150614 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
150616 VdbeCoverage(pParse->pVdbe);
150618 pLevel->regFilter = 0;
150619 pLevel->addrBrk = 0;
150624 ** Generate code for the start of the iLevel-th loop in the WHERE clause
150631 int iLevel, /* Which level of pWInfo->a[] should be coded */
150652 pWC = &pWInfo->sWC;
150653 db = pParse->db;
150654 pLoop = pLevel->pWLoop;
150655 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
150656 iCur = pTabItem->iCursor;
150657 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
150658 bRev = (pWInfo->revMask>>iLevel)&1;
150659 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
150663 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
150669 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
150671 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
150686 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
150687 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
150693 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
150694 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
150696 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
150697 pLevel->iLeftJoin = ++pParse->nMem;
150698 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
150699 VdbeComment((v, "init LEFT JOIN no-match flag"));
150704 for(j=iLevel; j>0; j--){
150705 if( pWInfo->a[j].iLeftJoin ) break;
150706 if( pWInfo->a[j].pRJ ) break;
150708 addrHalt = pWInfo->a[j].addrBrk;
150710 /* Special case of a FROM clause subquery implemented as a co-routine */
150711 if( pTabItem->fg.viaCoroutine ){
150712 int regYield = pTabItem->regReturn;
150713 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
150714 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
150716 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
150717 pLevel->op = OP_Goto;
150721 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
150722 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
150727 int nConstraint = pLoop->nLTerm;
150730 addrNotFound = pLevel->addrBrk;
150733 pTerm = pLoop->aLTerm[j];
150735 if( pTerm->eOperator & WO_IN ){
150736 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
150737 int iTab = pParse->nTab++;
150738 int iCache = ++pParse->nMem;
150739 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
150743 addrNotFound = pLevel->addrNxt;
150746 Expr *pRight = pTerm->pExpr->pRight;
150748 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
150749 && pLoop->u.vtab.bOmitOffset
150751 assert( pTerm->eOperator==WO_AUX );
150752 assert( pWInfo->pLimit!=0 );
150753 assert( pWInfo->pLimit->iOffset>0 );
150754 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pLimit->iOffset);
150759 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
150762 pLoop->u.vtab.idxStr,
150763 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
150765 pLoop->u.vtab.needFree = 0;
150767 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
150768 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
150769 pLevel->p1 = iCur;
150770 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
150771 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
150772 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
150775 pTerm = pLoop->aLTerm[j];
150776 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
150780 if( (pTerm->eOperator & WO_IN)!=0
150781 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
150782 && !db->mallocFailed
150787 int iIn; /* IN loop corresponding to the j-th constraint */
150794 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
150795 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
150796 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
150797 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
150799 testcase( pOp->opcode==OP_Rowid );
150800 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
150809 if( !db->mallocFailed ){
150810 int iFld = pTerm->u.x.iField;
150811 Expr *pLeft = pTerm->pExpr->pLeft;
150814 assert( pLeft->op==TK_VECTOR );
150816 assert( iFld<=pLeft->x.pList->nExpr );
150817 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
150819 pCompare->pLeft = pLeft;
150821 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
150823 pRight->iTable = iReg+j+2;
150825 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
150828 pCompare->pLeft = 0;
150836 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
150844 if( (pLoop->wsFlags & WHERE_IPK)!=0
150845 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
150852 assert( pLoop->u.btree.nEq==1 );
150853 pTerm = pLoop->aLTerm[0];
150855 assert( pTerm->pExpr!=0 );
150856 testcase( pTerm->wtFlags & TERM_VIRTUAL );
150857 iReleaseReg = ++pParse->nMem;
150860 addrNxt = pLevel->addrNxt;
150861 if( pLevel->regFilter ){
150862 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
150869 pLevel->op = OP_Noop;
150870 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
150871 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
150882 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
150883 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
150909 assert( (pStart->wtFlags & TERM_VNULL)==0 );
150910 testcase( pStart->wtFlags & TERM_VIRTUAL );
150911 pX = pStart->pExpr;
150913 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
150914 if( sqlite3ExprIsVector(pX->pRight) ){
150916 codeExprOrVector(pParse, pX->pRight, r1, 1);
150917 testcase( pX->op==TK_GT );
150918 testcase( pX->op==TK_GE );
150919 testcase( pX->op==TK_LT );
150920 testcase( pX->op==TK_LE );
150921 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
150922 assert( pX->op!=TK_GT || op==OP_SeekGE );
150923 assert( pX->op!=TK_GE || op==OP_SeekGE );
150924 assert( pX->op!=TK_LT || op==OP_SeekLE );
150925 assert( pX->op!=TK_LE || op==OP_SeekLE );
150927 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
150929 op = aMoveOp[(pX->op - TK_GT)];
150933 VdbeCoverageIf(v, pX->op==TK_GT);
150934 VdbeCoverageIf(v, pX->op==TK_LE);
150935 VdbeCoverageIf(v, pX->op==TK_LT);
150936 VdbeCoverageIf(v, pX->op==TK_GE);
150945 pX = pEnd->pExpr;
150947 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
150948 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
150949 testcase( pEnd->wtFlags & TERM_VIRTUAL );
150950 memEndValue = ++pParse->nMem;
150951 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
150952 if( 0==sqlite3ExprIsVector(pX->pRight)
150953 && (pX->op==TK_LT || pX->op==TK_GT)
150959 if( 0==sqlite3ExprIsVector(pX->pRight) ){
150964 pLevel->op = bRev ? OP_Prev : OP_Next;
150965 pLevel->p1 = iCur;
150966 pLevel->p2 = start;
150967 assert( pLevel->p5==0 );
150969 iRowidReg = ++pParse->nMem;
150978 }else if( pLoop->wsFlags & WHERE_INDEXED ){
150983 ** left-most columns of the index. It may also contain
150986 ** the right-most column can be an inequality - the rest must
151026 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
151027 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
151028 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
151044 int regBignull = 0; /* big-null flag register */
151047 pIdx = pLoop->u.btree.pIndex;
151048 iIdxCur = pLevel->iIdxCur;
151049 assert( nEq>=pLoop->nSkip );
151055 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
151056 pRangeStart = pLoop->aLTerm[j++];
151057 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
151059 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
151060 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
151062 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
151063 pRangeEnd = pLoop->aLTerm[j++];
151064 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
151066 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
151068 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
151069 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
151070 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
151072 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
151076 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
151078 pLevel->iLikeRepCntr <<=1;
151079 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
151083 j = pIdx->aiColumn[nEq];
151084 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
151089 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
151092 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
151095 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
151098 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
151099 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
151103 testcase( pLoop->nSkip>0 );
151106 pLevel->regBignull = regBignull = ++pParse->nMem;
151107 if( pLevel->iLeftJoin ){
151110 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
151117 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
151123 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
151139 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
151141 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
151142 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
151143 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
151144 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
151145 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
151146 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
151152 Expr *pRight = pRangeStart->pExpr->pRight;
151155 if( (pRangeStart->wtFlags & TERM_VNULL)==0
151165 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
151182 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
151183 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
151184 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
151190 VdbeComment((v, "NULL-scan pass ctr"));
151192 if( pLevel->regFilter ){
151193 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
151201 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
151212 (pIdx->aiRowLogEst[0]+9)/10);
151232 nConstraint-startEq);
151246 assert( pLevel->p2==0 );
151248 Expr *pRight = pRangeEnd->pExpr->pRight;
151250 /* For a seek-scan that has a range on the lowest term of the index,
151253 ** over that initialization, leaving the range-end value set to the
151254 ** range-start value, resulting in a wrong answer.
151255 ** See ticket 5981a8c041a3c2f3 (2021-11-02).
151257 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151261 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
151271 assert( pParse->db->mallocFailed );
151274 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
151292 if( pLevel->p2==0 ) pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151297 /* Except, skip the end-of-range check while doing the NULL-scan */
151299 VdbeComment((v, "If NULL-scan 2nd pass"));
151311 /* During a NULL-scan, check to see if we have reached the end of
151317 VdbeComment((v, "If NULL-scan 1st pass"));
151328 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
151333 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
151334 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
151337 }else if( HasRowid(pIdx->pTable) ){
151340 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
151341 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
151342 for(j=0; j<pPk->nKeyCol; j++){
151343 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
151347 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
151350 if( pLevel->iLeftJoin==0 ){
151363 ** Also, do not do this when processing one index an a multi-index
151368 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ){
151376 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
151379 if( pIdx->pPartIdxWhere ){
151380 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
151383 testcase( pIdx->pPartIdxWhere );
151385 ** The OR-optimization doesn't work for the right hand table of
151387 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
151391 if( pLoop->wsFlags & WHERE_ONEROW ){
151392 pLevel->op = OP_Noop;
151394 pLevel->op = OP_Prev;
151396 pLevel->op = OP_Next;
151398 pLevel->p1 = iIdxCur;
151399 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
151400 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
151401 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
151403 assert( pLevel->p5==0 );
151409 if( pLoop->wsFlags & WHERE_MULTI_OR ){
151448 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
151453 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
151454 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
151456 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
151458 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
151466 Table *pTab = pTabItem->pTab;
151468 pTerm = pLoop->aLTerm[0];
151470 assert( pTerm->eOperator & WO_OR );
151471 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
151472 pOrWc = &pTerm->u.pOrInfo->wc;
151473 pLevel->op = OP_Return;
151474 pLevel->p1 = regReturn;
151480 if( pWInfo->nLevel>1 ){
151483 nNotReady = pWInfo->nLevel - iLevel - 1;
151485 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
151487 pOrTab->nAlloc = (u8)(nNotReady + 1);
151488 pOrTab->nSrc = pOrTab->nAlloc;
151489 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
151490 origSrc = pWInfo->pTabList->a;
151492 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
151495 pOrTab = pWInfo->pTabList;
151506 ** correct response for the end-of-loop code (the OP_Return) is to
151510 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
151512 regRowset = ++pParse->nMem;
151516 regRowset = pParse->nTab++;
151517 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
151520 regRowid = ++pParse->nMem;
151530 ** the "interesting" terms of z - terms that did not originate in the
151538 ** 2022-02-04: Do not push down slices of a row-value comparison.
151540 ** the initialization of the right-hand operand of the vector comparison
151544 ** 2022-03-03: Do not push down expressions that involve subqueries.
151545 ** The subquery might get coded as a subroutine. Any table-references
151546 ** in the subquery might be resolved to index-references for the index on
151549 ** index-references will not work. tag-20220303a
151552 if( pWC->nTerm>1 ){
151554 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
151555 Expr *pExpr = pWC->a[iTerm].pExpr;
151556 if( &pWC->a[iTerm] == pTerm ) continue;
151557 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
151558 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
151559 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
151560 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
151563 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
151564 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
151572 ** prevents sqlite3PExpr() from applying the AND short-circuit
151580 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
151582 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
151583 for(ii=0; ii<pOrWc->nTerm; ii++){
151584 WhereTerm *pOrTerm = &pOrWc->a[ii];
151585 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
151586 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
151587 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
151594 if( db->mallocFailed ){
151599 pAndExpr->pLeft = pOrExpr;
151604 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
151607 assert( pSubWInfo || pParse->nErr );
151611 pParse, pOrTab, &pSubWInfo->a[0], 0
151613 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
151615 /* This is the sub-WHERE clause body. First skip over
151616 ** duplicate rows from prior sub-WHERE clauses, and record the
151618 ** row will be skipped in subsequent sub-WHERE clauses.
151620 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
151621 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
151623 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
151629 int nPk = pPk->nKeyCol;
151636 int iCol = pPk->aiColumn[iPk];
151648 ** the temp table. And if iSet is -1, assume that there is no
151671 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
151674 /* The pSubWInfo->untestedTerms flag means that this OR term
151679 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
151681 /* If all of the OR-connected terms are optimized using the same
151687 ** uses an index, and this is either the first OR-connected term
151693 pSubLoop = pSubWInfo->a[0].pWLoop;
151694 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
151695 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
151696 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
151697 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
151699 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
151700 pCov = pSubLoop->u.btree.pIndex;
151705 pWInfo->bDeferredSeek = 1;
151716 assert( pLevel->pWLoop==pLoop );
151717 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
151718 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
151719 pLevel->u.pCoveringIdx = pCov;
151720 if( pCov ) pLevel->iIdxCur = iCovCur;
151722 pAndExpr->pLeft = 0;
151726 sqlite3VdbeGoto(v, pLevel->addrBrk);
151731 ** loop. The byte-code formatter will use that P2 value as a hint to
151733 ** See tag-20220407a in vdbe.c and shell.c */
151734 assert( pLevel->op==OP_Return );
151735 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
151737 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
151749 if( pTabItem->fg.isRecursive ){
151751 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
151752 pLevel->op = OP_Noop;
151755 pLevel->op = aStep[bRev];
151756 pLevel->p1 = iCur;
151757 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
151760 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
151765 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
151777 ** sub-queries.
151785 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
151788 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151789 testcase( pTerm->wtFlags & TERM_CODED );
151790 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151791 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151792 testcase( pWInfo->untestedTerms==0
151793 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
151794 pWInfo->untestedTerms = 1;
151797 pE = pTerm->pExpr;
151799 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
151802 ** join processing. tag-20220513a */
151804 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
151808 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
151809 if( m & pLevel->notReady ){
151815 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
151819 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
151824 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
151833 u32 x = pLevel->iLikeRepCntr;
151844 pWC->nTerm-j, pTerm, iLoop));
151848 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151853 pTerm->wtFlags |= TERM_CODED;
151866 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
151869 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151870 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
151871 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
151872 if( pTerm->leftCursor!=iCur ) continue;
151873 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
151874 pE = pTerm->pExpr;
151878 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
151882 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
151883 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
151884 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
151887 if( pAlt->wtFlags & (TERM_CODED) ) continue;
151888 if( (pAlt->eOperator & WO_IN)
151889 && ExprUseXSelect(pAlt->pExpr)
151890 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
151894 testcase( pAlt->eOperator & WO_EQ );
151895 testcase( pAlt->eOperator & WO_IS );
151896 testcase( pAlt->eOperator & WO_IN );
151898 sEAlt = *pAlt->pExpr;
151899 sEAlt.pLeft = pE->pLeft;
151901 pAlt->wtFlags |= TERM_CODED;
151907 if( pLevel->pRJ ){
151912 WhereRightJoin *pRJ = pLevel->pRJ;
151914 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
151919 pTab = pWInfo->pTabList->a[pLevel->iFrom].pTab;
151922 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
151927 nPk = pPk->nKeyCol;
151930 int iCol = pPk->aiColumn[iPk];
151934 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
151936 VdbeComment((v, "match against %s", pTab->zName));
151938 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
151939 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
151948 if( pLevel->iLeftJoin ){
151949 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
151950 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
151952 if( pLevel->pRJ==0 ){
151957 if( pLevel->pRJ ){
151960 ** be in-line with the rest of the code. But at the end, a separate
151964 WhereRightJoin *pRJ = pLevel->pRJ;
151965 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
151966 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
151967 assert( pParse->withinRJSubrtn < 255 );
151968 pParse->withinRJSubrtn++;
151973 ** appropriate WHERE clause constraint checks. tag-20220513a.
151976 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
151977 testcase( pTerm->wtFlags & TERM_VIRTUAL );
151978 testcase( pTerm->wtFlags & TERM_CODED );
151979 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
151980 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
151981 assert( pWInfo->untestedTerms );
151984 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
151985 assert( pTerm->pExpr );
151986 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
151987 pTerm->wtFlags |= TERM_CODED;
151993 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
151999 iLevel, (u64)pLevel->notReady);
152002 return pLevel->notReady;
152006 ** Generate the code for the loop that finds all non-matched terms
152014 Parse *pParse = pWInfo->pParse;
152015 Vdbe *v = pParse->pVdbe;
152016 WhereRightJoin *pRJ = pLevel->pRJ;
152018 WhereClause *pWC = &pWInfo->sWC;
152020 WhereLoop *pLoop = pLevel->pWLoop;
152021 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
152026 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pTab->zName));
152027 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
152028 pRJ->regReturn);
152031 mAll |= pWInfo->a[k].pWLoop->maskSelf;
152032 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
152033 iIdxCur = pWInfo->a[k].iIdxCur;
152038 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
152039 mAll |= pLoop->maskSelf;
152040 for(k=0; k<pWC->nTerm; k++){
152041 WhereTerm *pTerm = &pWC->a[k];
152042 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
152043 && pTerm->eOperator!=WO_ROWVAL
152047 if( pTerm->prereqAll & ~mAll ) continue;
152048 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
152050 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
152057 assert( pParse->withinRJSubrtn < 100 );
152058 pParse->withinRJSubrtn++;
152062 int iCur = pLevel->iTabCur;
152063 int r = ++pParse->nMem;
152067 Table *pTab = pTabItem->pTab;
152069 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
152074 nPk = pPk->nKeyCol;
152075 pParse->nMem += nPk - 1;
152077 int iCol = pPk->aiColumn[iPk];
152081 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
152083 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
152086 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
152089 sqlite3ExprDelete(pParse->db, pSubWhere);
152091 assert( pParse->withinRJSubrtn>0 );
152092 pParse->withinRJSubrtn--;
152098 ** 2015-06-08
152125 sqlite3WhereClauseClear(&p->wc);
152133 sqlite3WhereClauseClear(&p->wc);
152140 ** The index in pWC->a[] of the new WhereTerm is returned on success.
152143 ** the db->mallocFailed flag so that higher-level functions can detect it.
152145 ** This routine will increase the size of the pWC->a[] array as necessary.
152154 ** the pWC->a[] array.
152160 if( pWC->nTerm>=pWC->nSlot ){
152161 WhereTerm *pOld = pWC->a;
152162 sqlite3 *db = pWC->pWInfo->pParse->db;
152163 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
152164 if( pWC->a==0 ){
152168 pWC->a = pOld;
152171 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
152172 pWC->nSlot = pWC->nSlot*2;
152174 pTerm = &pWC->a[idx = pWC->nTerm++];
152175 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
152177 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
152179 pTerm->truthProb = 1;
152181 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
152182 pTerm->wtFlags = wtFlags;
152183 pTerm->pWC = pWC;
152184 pTerm->iParent = -1;
152185 memset(&pTerm->eOperator, 0,
152186 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
152208 if( pExpr->pLeft->op==TK_VECTOR
152209 || pExpr->pRight->op==TK_VECTOR
152210 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
152211 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
152213 pExpr->flags ^= EP_Commuted;
152215 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
152216 if( pExpr->op>=TK_GT ){
152221 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
152222 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
152240 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
152241 c = (u16)(WO_EQ<<(op-TK_EQ));
152279 int cnt; /* Number of non-wildcard prefix characters */
152281 sqlite3 *db = pParse->db; /* Database connection */
152293 pList = pExpr->x.pList;
152294 pLeft = pList->a[1].pExpr;
152296 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
152297 op = pRight->op;
152298 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
152299 Vdbe *pReprepare = pParse->pReprepare;
152300 int iCol = pRight->iColumn;
152305 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
152306 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
152309 z = (u8*)pRight->u.zToken;
152321 ** with a wildcard and if (2) the non-wildcard prefix does not end with
152328 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
152340 zNew = pPrefix->u.zToken;
152356 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
152357 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
152358 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
152359 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
152360 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
152362 if( pLeft->op!=TK_COLUMN
152365 && pLeft->y.pTab
152366 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
152372 if( iTo==1 && zNew[0]=='-' ){
152375 zNew[iTo-1]++;
152377 zNew[iTo-1]--;
152392 Vdbe *v = pParse->pVdbe;
152393 sqlite3VdbeSetVarmask(v, pRight->iColumn);
152395 if( *pisComplete && pRight->u.zToken[1] ){
152404 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
152426 ** ----------------------- ---------------------------------
152454 if( pExpr->op==TK_FUNCTION ){
152469 pList = pExpr->x.pList;
152470 if( pList==0 || pList->nExpr!=2 ){
152474 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
152476 ** the left-hand side operand in their in-fix form.
152481 pCol = pList->a[1].pExpr;
152482 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152483 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152487 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
152489 *ppRight = pList->a[0].pExpr;
152502 ** Historically, xFindFunction expected to see lower-case function
152506 pCol = pList->a[0].pExpr;
152507 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
152508 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
152514 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
152516 assert( pVtab->pModule!=0 );
152518 pMod = (sqlite3_module *)pVtab->pModule;
152519 if( pMod->xFindFunction!=0 ){
152520 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
152523 *ppRight = pList->a[1].pExpr;
152529 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
152531 Expr *pLeft = pExpr->pLeft;
152532 Expr *pRight = pExpr->pRight;
152533 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
152534 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
152538 assert( pRight==0 || pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
152539 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
152546 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
152547 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
152548 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
152561 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
152562 pDerived->w.iJoin = pBase->w.iJoin;
152570 pWC->a[iChild].iParent = iParent;
152571 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
152572 pWC->a[iParent].nChild++;
152576 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
152581 if( pTerm->eOperator!=WO_AND ){
152584 if( N<pTerm->u.pAndInfo->wc.nTerm ){
152585 return &pTerm->u.pAndInfo->wc.a[N];
152592 ** two subterms are in disjunction - they are OR-ed together.
152602 ** x<y OR x=y --> x<=y
152603 ** x=y OR x=y --> x=y
152604 ** x<=y OR x<y --> x<=y
152608 ** x<y OR x>y --> x!=y
152616 u16 eOp = pOne->eOperator | pTwo->eOperator;
152622 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
152623 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
152624 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
152627 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
152628 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
152629 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
152630 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
152632 if( (eOp & (eOp-1))!=0 ){
152640 db = pWC->pWInfo->pParse->db;
152641 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
152643 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
152644 pNew->op = op;
152651 ** Analyze a term that consists of two or more OR-connected
152664 ** The term being analyzed must have two or more of OR-connected subterms.
152665 ** A single subterm might be a set of AND-connected sub-subterms.
152703 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
152740 int idxTerm /* Index of the OR-term to be analyzed */
152742 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
152743 Parse *pParse = pWInfo->pParse; /* Parser context */
152744 sqlite3 *db = pParse->db; /* Database connection */
152745 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
152746 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
152749 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
152759 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
152760 assert( pExpr->op==TK_OR );
152761 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
152763 pTerm->wtFlags |= TERM_ORINFO;
152764 pOrWc = &pOrInfo->wc;
152765 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
152769 if( db->mallocFailed ) return;
152770 assert( pOrWc->nTerm>=2 );
152777 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
152778 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
152780 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
152788 pOrTerm->u.pAndInfo = pAndInfo;
152789 pOrTerm->wtFlags |= TERM_ANDINFO;
152790 pOrTerm->eOperator = WO_AND;
152791 pOrTerm->leftCursor = -1;
152792 pAndWC = &pAndInfo->wc;
152793 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
152794 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
152795 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
152797 pAndWC->pOuter = pWC;
152798 if( !db->mallocFailed ){
152799 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
152800 assert( pAndTerm->pExpr );
152801 if( allowedOp(pAndTerm->pExpr->op)
152802 || pAndTerm->eOperator==WO_AUX
152804 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
152810 }else if( pOrTerm->wtFlags & TERM_COPIED ){
152815 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
152816 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
152817 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
152818 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
152821 if( (pOrTerm->eOperator & WO_EQ)==0 ){
152833 pOrInfo->indexable = indexable;
152834 pTerm->eOperator = WO_OR;
152835 pTerm->leftCursor = -1;
152837 pWC->hasOr = 1;
152840 /* For a two-way OR, attempt to implementation case 2.
152842 if( indexable && pOrWc->nTerm==2 ){
152845 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
152848 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
152859 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
152862 ** something other than == on a column in the single table. The 1-bit
152866 ** sure the same column is used on all terms. The 2-bit case is when
152877 int iColumn = -1; /* Column index on lhs of IN operator */
152878 int iCursor = -1; /* Table cursor common to all terms */
152889 pOrTerm = pOrWc->a;
152890 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
152891 assert( pOrTerm->eOperator & WO_EQ );
152892 pOrTerm->wtFlags &= ~TERM_OK;
152893 if( pOrTerm->leftCursor==iCursor ){
152894 /* This is the 2-bit case and we are on the second iteration and
152899 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
152900 pOrTerm->leftCursor))==0 ){
152905 testcase( pOrTerm->wtFlags & TERM_COPIED );
152906 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
152907 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
152910 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152911 iColumn = pOrTerm->u.x.leftColumn;
152912 iCursor = pOrTerm->leftCursor;
152913 pLeft = pOrTerm->pExpr->pLeft;
152921 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
152929 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
152930 assert( pOrTerm->eOperator & WO_EQ );
152931 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152932 if( pOrTerm->leftCursor!=iCursor ){
152933 pOrTerm->wtFlags &= ~TERM_OK;
152934 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
152935 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
152940 /* If the right-hand side is also a column, then the affinities
152944 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
152945 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
152949 pOrTerm->wtFlags |= TERM_OK;
152965 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
152966 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
152967 assert( pOrTerm->eOperator & WO_EQ );
152968 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
152969 assert( pOrTerm->leftCursor==iCursor );
152970 assert( pOrTerm->u.x.leftColumn==iColumn );
152971 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
152972 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
152973 pLeft = pOrTerm->pExpr->pLeft;
152982 pNew->x.pList = pList;
152986 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
153014 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
153015 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
153017 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
153018 aff2 = sqlite3ExprAffinity(pExpr->pRight);
153026 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
153037 SrcList *pSrc = pS->pSrc;
153038 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
153039 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
153040 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
153041 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
153042 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
153045 for(i=0; i<pSrc->nSrc; i++){
153046 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
153047 if( pSrc->a[i].fg.isUsing==0 ){
153048 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
153050 if( pSrc->a[i].fg.isTabFunc ){
153051 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
153055 pS = pS->pPrior;
153066 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
153083 iCur = pFrom->a[i].iCursor;
153084 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153085 if( pIdx->aColExpr==0 ) continue;
153086 for(i=0; i<pIdx->nKeyCol; i++){
153087 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
153088 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
153110 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
153112 pExpr = pExpr->x.pList->a[0].pExpr;
153116 if( pExpr->op==TK_COLUMN ){
153117 aiCurCol[0] = pExpr->iTable;
153118 aiCurCol[1] = pExpr->iColumn;
153122 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
153150 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
153154 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
153160 int op; /* Top-level operator. pExpr->op */
153161 Parse *pParse = pWInfo->pParse; /* Parsing context */
153162 sqlite3 *db = pParse->db; /* Database connection */
153166 if( db->mallocFailed ){
153169 assert( pWC->nTerm > idxTerm );
153170 pTerm = &pWC->a[idxTerm];
153171 pMaskSet = &pWInfo->sMaskSet;
153172 pExpr = pTerm->pExpr;
153174 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
153175 pMaskSet->bVarSelect = 0;
153176 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
153177 op = pExpr->op;
153179 assert( pExpr->pRight==0 );
153182 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
153184 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
153186 prereqAll = prereqLeft | pTerm->prereqRight;
153188 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
153189 if( pExpr->pLeft==0
153191 || pExpr->x.pList!=0
153195 prereqAll = prereqLeft | pTerm->prereqRight;
153198 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
153209 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
153212 extraRight = x-1; /* ON clause terms may not be used with an index
153225 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
153232 pTerm->prereqAll = prereqAll;
153233 pTerm->leftCursor = -1;
153234 pTerm->iParent = -1;
153235 pTerm->eOperator = 0;
153238 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
153239 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
153240 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
153242 if( pTerm->u.x.iField>0 ){
153244 assert( pLeft->op==TK_VECTOR );
153246 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
153250 pTerm->leftCursor = aiCurCol[0];
153251 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
153252 pTerm->u.x.leftColumn = aiCurCol[1];
153253 pTerm->eOperator = operatorMask(op) & opMask;
153255 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
153257 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
153262 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
153263 assert( pTerm->u.x.iField==0 );
153264 if( pTerm->leftCursor>=0 ){
153267 if( db->mallocFailed ){
153273 pNew = &pWC->a[idxNew];
153275 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
153276 pTerm = &pWC->a[idxTerm];
153277 pTerm->wtFlags |= TERM_COPIED;
153280 pTerm->eOperator |= WO_EQUIV;
153287 pNew->wtFlags |= exprCommute(pParse, pDup);
153288 pNew->leftCursor = aiCurCol[0];
153289 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
153290 pNew->u.x.leftColumn = aiCurCol[1];
153292 pNew->prereqRight = prereqLeft | extraRight;
153293 pNew->prereqAll = prereqAll;
153294 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
153301 pExpr->op = TK_TRUEFALSE;
153302 pExpr->u.zToken = "false";
153304 pTerm->prereqAll = 0;
153305 pTerm->eOperator = 0;
153325 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
153330 pList = pExpr->x.pList;
153332 assert( pList->nExpr==2 );
153337 sqlite3ExprDup(db, pExpr->pLeft, 0),
153338 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
153343 pTerm = &pWC->a[idxTerm];
153353 else if( pExpr->op==TK_OR ){
153354 assert( pWC->op==TK_AND );
153356 pTerm = &pWC->a[idxTerm];
153365 else if( pExpr->op==TK_NOTNULL ){
153366 if( pExpr->pLeft->op==TK_COLUMN
153367 && pExpr->pLeft->iColumn>=0
153371 Expr *pLeft = pExpr->pLeft;
153382 pNewTerm = &pWC->a[idxNew];
153383 pNewTerm->prereqRight = 0;
153384 pNewTerm->leftCursor = pLeft->iTable;
153385 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153386 pNewTerm->eOperator = WO_GT;
153388 pTerm = &pWC->a[idxTerm];
153389 pTerm->wtFlags |= TERM_COPIED;
153390 pNewTerm->prereqAll = pTerm->prereqAll;
153406 ** for LIKE) then the lower-bound is made all uppercase and the upper-
153410 else if( pExpr->op==TK_FUNCTION
153411 && pWC->op==TK_AND
153415 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
153424 pLeft = pExpr->x.pList->a[1].pExpr;
153430 /* Convert the lower bound to upper-case and the upper bound to
153431 ** lower-case (upper-case is less than lower-case in ASCII) so that
153434 if( noCase && !pParse->db->mallocFailed ){
153437 pTerm->wtFlags |= TERM_LIKE;
153438 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
153439 pStr1->u.zToken[i] = sqlite3Toupper(c);
153440 pStr2->u.zToken[i] = sqlite3Tolower(c);
153444 if( !db->mallocFailed ){
153446 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
153455 if( c=='A'-1 ) isComplete = 0;
153477 pTerm = &pWC->a[idxTerm];
153485 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
153486 ** new terms for each component comparison - "a = ?" and "b = ?". The
153491 ** is not a sub-select.
153493 ** tag-20220128a
153495 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
153496 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
153497 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
153498 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
153499 || (pExpr->pRight->flags & EP_xIsSelect)==0)
153500 && pWC->op==TK_AND
153506 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
153507 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
153509 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
153514 pTerm = &pWC->a[idxTerm];
153515 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
153516 pTerm->eOperator = WO_ROWVAL;
153519 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
153528 else if( pExpr->op==TK_IN
153529 && pTerm->u.x.iField==0
153530 && pExpr->pLeft->op==TK_VECTOR
153532 && pExpr->x.pSelect->pPrior==0
153534 && pExpr->x.pSelect->pWin==0
153536 && pWC->op==TK_AND
153539 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
153542 pWC->a[idxNew].u.x.iField = i+1;
153558 else if( pWC->op==TK_AND ){
153561 while( res-- > 0 ){
153574 pNewExpr->w.iJoin = pExpr->w.iJoin;
153578 pNewTerm = &pWC->a[idxNew];
153579 pNewTerm->prereqRight = prereqExpr;
153580 pNewTerm->leftCursor = pLeft->iTable;
153581 pNewTerm->u.x.leftColumn = pLeft->iColumn;
153582 pNewTerm->eOperator = WO_AUX;
153583 pNewTerm->eMatchOp = eOp2;
153585 pTerm = &pWC->a[idxTerm];
153586 pTerm->wtFlags |= TERM_COPIED;
153587 pNewTerm->prereqAll = pTerm->prereqAll;
153597 testcase( pTerm!=&pWC->a[idxTerm] );
153598 pTerm = &pWC->a[idxTerm];
153599 pTerm->prereqRight |= extraRight;
153626 pWC->op = op;
153629 if( pE2->op!=op ){
153632 sqlite3WhereSplit(pWC, pE2->pLeft, op);
153633 sqlite3WhereSplit(pWC, pE2->pRight, op);
153640 ** where-clause passed as the first argument. The value for the term
153655 Parse *pParse = pWC->pWInfo->pParse;
153656 sqlite3 *db = pParse->db;
153664 pVal->u.iValue = iVal;
153669 pVal->iTable = iReg;
153676 pTerm = &pWC->a[idx];
153677 pTerm->leftCursor = iCsr;
153678 pTerm->eOperator = WO_AUX;
153679 pTerm->eMatchOp = eMatchOp;
153702 assert( p==0 || (p->pGroupBy==0 && (p->selFlags & SF_Aggregate)==0) );
153703 if( (p && p->pLimit) /* 1 */
153704 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
153705 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pTab)) /* 3 */
153707 ExprList *pOrderBy = p->pOrderBy;
153708 int iCsr = p->pSrc->a[0].iCursor;
153712 for(ii=0; ii<pWC->nTerm; ii++){
153713 if( pWC->a[ii].wtFlags & TERM_CODED ){
153715 ** other, subsequent terms. It can be ignored. See tag-20220128a */
153716 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
153717 assert( pWC->a[ii].eOperator==WO_ROWVAL );
153720 if( pWC->a[ii].leftCursor!=iCsr ) return;
153725 for(ii=0; ii<pOrderBy->nExpr; ii++){
153726 Expr *pExpr = pOrderBy->a[ii].pExpr;
153727 if( pExpr->op!=TK_COLUMN ) return;
153728 if( pExpr->iTable!=iCsr ) return;
153729 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
153733 /* All conditions are met. Add the terms to the where-clause object. */
153734 assert( p->pLimit->op==TK_LIMIT );
153735 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
153737 if( p->iOffset>0 ){
153738 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
153751 pWC->pWInfo = pWInfo;
153752 pWC->hasOr = 0;
153753 pWC->pOuter = 0;
153754 pWC->nTerm = 0;
153755 pWC->nBase = 0;
153756 pWC->nSlot = ArraySize(pWC->aStatic);
153757 pWC->a = pWC->aStatic;
153766 sqlite3 *db = pWC->pWInfo->pParse->db;
153767 assert( pWC->nTerm>=pWC->nBase );
153768 if( pWC->nTerm>0 ){
153769 WhereTerm *a = pWC->a;
153770 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
153773 /* Verify that every term past pWC->nBase is virtual */
153774 for(i=pWC->nBase; i<pWC->nTerm; i++){
153775 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
153779 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
153780 if( a->wtFlags & TERM_DYNAMIC ){
153781 sqlite3ExprDelete(db, a->pExpr);
153783 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
153784 if( a->wtFlags & TERM_ORINFO ){
153785 assert( (a->wtFlags & TERM_ANDINFO)==0 );
153786 whereOrInfoDelete(db, a->u.pOrInfo);
153788 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
153789 whereAndInfoDelete(db, a->u.pAndInfo);
153804 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
153809 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
153814 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
153820 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
153827 ** "no-inline" function to avoid the stack push overhead in the
153835 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
153836 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
153837 if( p->pRight ){
153838 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
153839 assert( p->x.pList==0 );
153841 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
153842 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
153843 }else if( p->x.pList ){
153844 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
153847 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
153848 assert( p->y.pWin!=0 );
153849 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
153850 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
153851 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
153857 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
153858 return sqlite3WhereGetMask(pMaskSet, p->iTable);
153860 assert( p->op!=TK_IF_NULL_ROW );
153872 for(i=0; i<pList->nExpr; i++){
153873 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
153893 for(i=pWC->nTerm-1; i>=0; i--){
153899 ** For table-valued-functions, transform the function arguments into
153915 if( pItem->fg.isTabFunc==0 ) return;
153916 pTab = pItem->pTab;
153918 pArgs = pItem->u1.pFuncArg;
153920 for(j=k=0; j<pArgs->nExpr; j++){
153923 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
153924 if( k>=pTab->nCol ){
153925 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
153926 pTab->zName, j);
153929 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
153931 pColRef->iTable = pItem->iCursor;
153932 pColRef->iColumn = k++;
153934 pColRef->y.pTab = pTab;
153935 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
153937 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
153939 if( pItem->fg.jointype & (JT_LEFT|JT_LTORJ) ){
153944 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
154000 return pWInfo->nRowOut;
154008 return pWInfo->eDistinct;
154020 return pWInfo->nOBSat;
154024 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
154026 ** inner-most loop did not fit within the sorter, then we can skip all
154029 ** loop - the loop immediately outside the inner-most.
154036 ** be the continuation for the second-inner-most loop. If the ORDER BY
154038 ** be the continuation for the inner-most loop.
154041 ** inner-most loop, in the sense that a correct answer will result.
154048 if( !pWInfo->bOrderedInnerLoop ){
154050 ** continuation of the inner-most loop. */
154051 return pWInfo->iContinue;
154053 pInner = &pWInfo->a[pWInfo->nLevel-1];
154054 assert( pInner->addrNxt!=0 );
154055 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
154060 ** the aggregate-step call to min() or max(), check to see if any
154072 if( !pWInfo->bOrderedInnerLoop ) return;
154073 if( pWInfo->nOBSat==0 ) return;
154074 for(i=pWInfo->nLevel-1; i>=0; i--){
154075 pInner = &pWInfo->a[i];
154076 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
154077 sqlite3VdbeGoto(v, pInner->addrNxt);
154081 sqlite3VdbeGoto(v, pWInfo->iBreak);
154089 assert( pWInfo->iContinue!=0 );
154090 return pWInfo->iContinue;
154098 return pWInfo->iBreak;
154105 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
154112 ** Either value may be -1, indicating that cursor is not used.
154115 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
154119 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
154121 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
154123 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
154127 return pWInfo->eOnePass;
154135 return pWInfo->bDeferredSeek;
154142 pDest->n = pSrc->n;
154143 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
154156 LogEst rRun, /* Run-cost of the new entry */
154161 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
154162 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
154165 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
154169 if( pSet->n<N_OR_COST ){
154170 p = &pSet->a[pSet->n++];
154171 p->nOut = nOut;
154173 p = pSet->a;
154174 for(i=1; i<pSet->n; i++){
154175 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
154177 if( p->rRun<=rRun ) return 0;
154180 p->prereq = prereq;
154181 p->rRun = rRun;
154182 if( p->nOut>nOut ) p->nOut = nOut;
154192 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
154193 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
154194 assert( iCursor>=-1 );
154195 if( pMaskSet->ix[0]==iCursor ){
154198 for(i=1; i<pMaskSet->n; i++){
154199 if( pMaskSet->ix[i]==iCursor ){
154210 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
154212 pBlock->pNext = pWInfo->pMemToFree;
154213 pBlock->sz = nByte;
154214 pWInfo->pMemToFree = pBlock;
154223 pOldBlk--;
154224 assert( pOldBlk->sz<nByte );
154225 memcpy(pNew, pOld, pOldBlk->sz);
154235 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
154239 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
154240 pMaskSet->ix[pMaskSet->n++] = iCursor;
154244 ** If the right-hand branch of the expression is a TK_COLUMN, then return
154245 ** a pointer to the right-hand branch. Otherwise, return NULL.
154248 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
154249 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
154262 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
154264 WhereClause *pWC; /* Shorthand for pScan->pWC */
154266 int k = pScan->k; /* Where to start scanning */
154268 assert( pScan->iEquiv<=pScan->nEquiv );
154269 pWC = pScan->pWC;
154271 iColumn = pScan->aiColumn[pScan->iEquiv-1];
154272 iCur = pScan->aiCur[pScan->iEquiv-1];
154276 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
154277 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
154278 if( pTerm->leftCursor==iCur
154279 && pTerm->u.x.leftColumn==iColumn
154281 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
154282 pScan->pIdxExpr,iCur)==0)
154283 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
154285 if( (pTerm->eOperator & WO_EQUIV)!=0
154286 && pScan->nEquiv<ArraySize(pScan->aiCur)
154287 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
154290 for(j=0; j<pScan->nEquiv; j++){
154291 if( pScan->aiCur[j]==pX->iTable
154292 && pScan->aiColumn[j]==pX->iColumn ){
154296 if( j==pScan->nEquiv ){
154297 pScan->aiCur[j] = pX->iTable;
154298 pScan->aiColumn[j] = pX->iColumn;
154299 pScan->nEquiv++;
154302 if( (pTerm->eOperator & pScan->opMask)!=0 ){
154304 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
154306 Parse *pParse = pWC->pWInfo->pParse;
154307 pX = pTerm->pExpr;
154308 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
154311 assert(pX->pLeft);
154313 if( pColl==0 ) pColl = pParse->db->pDfltColl;
154314 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
154318 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
154319 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
154320 && pX->op==TK_COLUMN
154321 && pX->iTable==pScan->aiCur[0]
154322 && pX->iColumn==pScan->aiColumn[0]
154324 testcase( pTerm->eOperator & WO_IS );
154327 pScan->pWC = pWC;
154328 pScan->k = k+1;
154332 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
154333 pTerm, pScan->nEquiv);
154334 for(ii=0; ii<pScan->nEquiv; ii++){
154336 pScan->aiCur[ii], pScan->aiColumn[ii]);
154345 pWC = pWC->pOuter;
154348 if( pScan->iEquiv>=pScan->nEquiv ) break;
154349 pWC = pScan->pOrigWC;
154351 pScan->iEquiv++;
154358 ** It is factored out into a separate tail-recursion subroutine so that
154359 ** the normal whereScanInit() routine, which is a high-runner, does not
154363 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
154394 pScan->pOrigWC = pWC;
154395 pScan->pWC = pWC;
154396 pScan->pIdxExpr = 0;
154397 pScan->idxaff = 0;
154398 pScan->zCollName = 0;
154399 pScan->opMask = opMask;
154400 pScan->k = 0;
154401 pScan->aiCur[0] = iCur;
154402 pScan->nEquiv = 1;
154403 pScan->iEquiv = 1;
154406 iColumn = pIdx->aiColumn[j];
154407 if( iColumn==pIdx->pTable->iPKey ){
154410 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
154411 pScan->zCollName = pIdx->azColl[j];
154413 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
154414 pScan->zCollName = pIdx->azColl[j];
154415 pScan->aiColumn[0] = XN_EXPR;
154421 pScan->aiColumn[0] = iColumn;
154432 ** Search for terms matching the iColumn-th column of pIdx
154433 ** rather than the iColumn-th column of table iCur.
154437 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
154444 ** then try for the one with no dependencies on <expr> - in other words where
154447 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
154465 if( (p->prereqRight & notReady)==0 ){
154466 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
154467 testcase( p->eOperator & WO_IS );
154478 ** This function searches pList for an entry that matches the iCol-th column
154481 ** If such an expression is found, its index in pList->a[] is returned. If
154482 ** no expression is found, -1 is returned.
154492 const char *zColl = pIdx->azColl[iCol];
154494 for(i=0; i<pList->nExpr; i++){
154495 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
154497 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
154498 && p->iColumn==pIdx->aiColumn[iCol]
154499 && p->iTable==iBase
154501 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
154502 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
154508 return -1;
154512 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
154517 assert( iCol>=0 && iCol<pIdx->nColumn );
154518 j = pIdx->aiColumn[iCol];
154520 return pIdx->pTable->aCol[j].notNull;
154521 }else if( j==(-1) ){
154524 assert( j==(-2) );
154531 ** Return true if the DISTINCT expression-list passed as the third argument
154535 ** DISTINCT list are collectively unique and individually non-null.
154548 /* If there is more than one table or sub-select in the FROM clause of
154551 if( pTabList->nSrc!=1 ) return 0;
154552 iBase = pTabList->a[0].iCursor;
154553 pTab = pTabList->a[0].pTab;
154556 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
154557 ** current SELECT is a correlated sub-query.
154559 for(i=0; i<pDistinct->nExpr; i++){
154560 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
154562 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
154563 if( p->iTable==iBase && p->iColumn<0 ) return 1;
154574 ** comparison and select-list expressions must match those of the index.
154579 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
154581 if( pIdx->pPartIdxWhere ) continue;
154582 for(i=0; i<pIdx->nKeyCol; i++){
154588 if( i==pIdx->nKeyCol ){
154602 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
154609 ** opcodes into OP_Copy when the table is being accessed via co-routine
154622 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
154624 Vdbe *v = pParse->pVdbe;
154627 if( pParse->db->mallocFailed ) return;
154629 if( pOp->p1!=iTabCur ) continue;
154630 if( pOp->opcode==OP_Column ){
154631 pOp->opcode = OP_Copy;
154632 pOp->p1 = pOp->p2 + iRegister;
154633 pOp->p2 = pOp->p3;
154634 pOp->p3 = 0;
154635 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
154636 }else if( pOp->opcode==OP_Rowid ){
154637 pOp->opcode = OP_Sequence;
154638 pOp->p1 = iAutoidxCur;
154641 pOp->opcode = OP_Null;
154642 pOp->p3 = 0;
154653 ** are no-ops.
154659 for(i=0; i<p->nConstraint; i++){
154663 p->aConstraint[i].iColumn,
154664 p->aConstraint[i].iTermOffset,
154665 p->aConstraint[i].op,
154666 p->aConstraint[i].usable,
154669 for(i=0; i<p->nOrderBy; i++){
154672 p->aOrderBy[i].iColumn,
154673 p->aOrderBy[i].desc);
154679 for(i=0; i<p->nConstraint; i++){
154682 p->aConstraintUsage[i].argvIndex,
154683 p->aConstraintUsage[i].omit);
154685 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
154686 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
154687 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
154688 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
154689 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
154708 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
154709 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
154710 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
154711 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
154712 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
154713 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
154714 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
154715 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
154716 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
154717 || pTerm->pExpr->w.iJoin != pSrc->iCursor
154719 return 0; /* See tag-20191211-001 */
154722 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
154723 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154724 if( pTerm->u.x.leftColumn<0 ) return 0;
154725 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
154726 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
154727 testcase( pTerm->pExpr->op==TK_IS );
154748 WhereTerm *pWCEnd; /* End of pWC->a[] */
154757 int mxBitCol; /* Maximum column in pSrc->colUsed */
154772 v = pParse->pVdbe;
154779 pTable = pSrc->pTab;
154780 pWCEnd = &pWC->a[pWC->nTerm];
154781 pLoop = pLevel->pWLoop;
154783 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154784 Expr *pExpr = pTerm->pExpr;
154788 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
154792 sqlite3ExprDup(pParse->db, pExpr, 0));
154797 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154798 iCol = pTerm->u.x.leftColumn;
154799 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
154801 testcase( iCol==BMS-1 );
154804 "automatic index on %s(%s)", pTable->zName,
154805 pTable->aCol[iCol].zCnName);
154809 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
154812 pLoop->aLTerm[nKeyCol++] = pTerm;
154817 assert( nKeyCol>0 || pParse->db->mallocFailed );
154818 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
154819 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
154830 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
154831 mxBitCol = MIN(BMS-1,pTable->nCol);
154832 testcase( pTable->nCol==BMS-1 );
154833 testcase( pTable->nCol==BMS-2 );
154837 if( pSrc->colUsed & MASKBIT(BMS-1) ){
154838 nKeyCol += pTable->nCol - BMS + 1;
154842 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
154844 pLoop->u.btree.pIndex = pIdx;
154845 pIdx->zName = "auto-index";
154846 pIdx->pTable = pTable;
154849 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
154853 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
154854 iCol = pTerm->u.x.leftColumn;
154855 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
154856 testcase( iCol==BMS-1 );
154859 Expr *pX = pTerm->pExpr;
154861 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
154863 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
154864 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
154869 assert( (u32)n==pLoop->u.btree.nEq );
154875 pIdx->aiColumn[n] = i;
154876 pIdx->azColl[n] = sqlite3StrBINARY;
154880 if( pSrc->colUsed & MASKBIT(BMS-1) ){
154881 for(i=BMS-1; i<pTable->nCol; i++){
154882 pIdx->aiColumn[n] = i;
154883 pIdx->azColl[n] = sqlite3StrBINARY;
154888 pIdx->aiColumn[n] = XN_ROWID;
154889 pIdx->azColl[n] = sqlite3StrBINARY;
154892 assert( pLevel->iIdxCur>=0 );
154893 pLevel->iIdxCur = pParse->nTab++;
154894 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
154896 VdbeComment((v, "for %s", pTable->zName));
154897 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){
154898 pLevel->regFilter = ++pParse->nMem;
154899 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
154903 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
154904 if( pTabItem->fg.viaCoroutine ){
154905 int regYield = pTabItem->regReturn;
154907 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
154910 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
154912 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
154917 pLoop->wsFlags |= WHERE_PARTIALIDX;
154921 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
154923 if( pLevel->regFilter ){
154924 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
154925 regBase, pLoop->u.btree.nEq);
154927 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
154930 if( pTabItem->fg.viaCoroutine ){
154932 testcase( pParse->db->mallocFailed );
154933 assert( pLevel->iIdxCur>0 );
154934 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
154935 pTabItem->regResult, pLevel->iIdxCur);
154937 pTabItem->fg.viaCoroutine = 0;
154939 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
154949 sqlite3ExprDelete(pParse->db, pPartial);
154966 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
154975 int iLevel, /* Index in pWInfo->a[] that is pLevel */
154984 Parse *pParse = pWInfo->pParse; /* Parsing context */
154985 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
154986 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
154991 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
155000 iCur = pLevel->iTabCur;
155001 pLevel->regFilter = ++pParse->nMem;
155004 ** to zero-filled blob of at least 80K bits, but maybe more if the
155006 ** measure the size of the table at run-time using OP_Count with
155011 pItem = &pWInfo->pTabList->a[pLevel->iFrom];
155013 pTab = pItem->pTab;
155015 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
155021 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
155024 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
155025 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
155026 Expr *pExpr = pTerm->pExpr;
155027 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
155030 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
155033 if( pLoop->wsFlags & WHERE_IPK ){
155036 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
155039 Index *pIdx = pLoop->u.btree.pIndex;
155040 int n = pLoop->u.btree.nEq;
155044 int iCol = pIdx->aiColumn[jj];
155045 assert( pIdx->pTable==pItem->pTab );
155046 sqlite3ExprCodeGetColumnOfTable(v, pIdx->pTable, iCur, iCol,r1+jj);
155048 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
155052 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
155055 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
155056 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
155057 while( ++iLevel < pWInfo->nLevel ){
155059 pLevel = &pWInfo->a[iLevel];
155060 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
155061 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
155062 pLoop = pLevel->pWLoop;
155064 if( pLoop->prereq & notReady ) continue;
155065 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
155068 /* This is a candidate for bloom-filter pull-down (early evaluation).
155075 }while( iLevel < pWInfo->nLevel );
155095 Parse *pParse = pWInfo->pParse;
155106 ExprList *pOrderBy = pWInfo->pOrderBy;
155109 pTab = pSrc->pTab;
155117 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155118 pTerm->wtFlags &= ~TERM_OK;
155119 if( pTerm->leftCursor != pSrc->iCursor ) continue;
155120 if( pTerm->prereqRight & mUnusable ) continue;
155121 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
155122 testcase( pTerm->eOperator & WO_IN );
155123 testcase( pTerm->eOperator & WO_ISNULL );
155124 testcase( pTerm->eOperator & WO_IS );
155125 testcase( pTerm->eOperator & WO_ALL );
155126 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
155127 if( pTerm->wtFlags & TERM_VNULL ) continue;
155129 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
155130 assert( pTerm->u.x.leftColumn>=XN_ROWID );
155131 assert( pTerm->u.x.leftColumn<pTab->nCol );
155133 /* tag-20191211-002: WHERE-clause constraints are not useful to the
155134 ** right-hand table of a LEFT JOIN nor to the either table of a
155135 ** RIGHT JOIN. See tag-20191211-001 for the
155137 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
155138 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
155139 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
155140 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
155141 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) );
155142 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
155143 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
155144 || pTerm->pExpr->w.iJoin != pSrc->iCursor
155150 pTerm->wtFlags |= TERM_OK;
155159 int n = pOrderBy->nExpr;
155161 Expr *pExpr = pOrderBy->a[i].pExpr;
155170 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
155172 /* First case - a direct column references without a COLLATE operator */
155173 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
155174 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
155178 /* 2nd case - a column reference with a COLLATE operator. Only match
155180 if( pExpr->op==TK_COLLATE
155181 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
155182 && pE2->iTable==pSrc->iCursor
155186 assert( pExpr->u.zToken!=0 );
155187 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
155188 pExpr->iColumn = pE2->iColumn;
155189 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
155190 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
155192 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
155200 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) ){
155201 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
155202 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
155210 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
155219 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
155222 pIdxInfo->aConstraint = pIdxCons;
155223 pIdxInfo->aOrderBy = pIdxOrderBy;
155224 pIdxInfo->aConstraintUsage = pUsage;
155225 pHidden->pWC = pWC;
155226 pHidden->pParse = pParse;
155227 pHidden->eDistinct = eDistinct;
155228 pHidden->mIn = 0;
155229 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
155231 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
155232 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
155234 op = pTerm->eOperator & WO_ALL;
155236 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
155237 pHidden->mIn |= SMASKBIT32(j);
155242 pIdxCons[j].op = pTerm->eMatchOp;
155259 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
155262 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
155274 pIdxInfo->nConstraint = j;
155276 Expr *pExpr = pOrderBy->a[i].pExpr;
155278 assert( pExpr->op==TK_COLUMN
155279 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
155280 && pExpr->iColumn==pExpr->pLeft->iColumn) );
155281 pIdxOrderBy[j].iColumn = pExpr->iColumn;
155282 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
155285 pIdxInfo->nOrderBy = j;
155300 assert( pHidden->pParse!=0 );
155301 assert( pHidden->pParse->db==db );
155302 for(i=0; i<pIdxInfo->nConstraint; i++){
155303 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
155304 pHidden->aRhs[i] = 0;
155315 ** If an error occurs, pParse is populated with an error message and an
155322 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
155326 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
155330 pParse->db->nSchemaLock++;
155331 rc = pVtab->pModule->xBestIndex(pVtab, p);
155332 pParse->db->nSchemaLock--;
155337 sqlite3OomFault(pParse->db);
155338 }else if( !pVtab->zErrMsg ){
155341 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
155344 sqlite3_free(pVtab->zErrMsg);
155345 pVtab->zErrMsg = 0;
155360 ** into the aSample[] array - it is an index into a virtual set of samples
155371 IndexSample *aSample = pIdx->aSample;
155385 assert( pIdx->nSample>0 );
155386 assert( pRec->nField>0 );
155432 nField = MIN(pRec->nField, pIdx->nSample);
155434 iSample = pIdx->nSample * nField;
155446 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
155452 pRec->nField = n;
155455 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
155458 iLower = aSample[iSamp].anLt[n-1];
155460 res = -1;
155463 iCol = n-1;
155472 if( pParse->db->mallocFailed==0 ){
155475 assert( i<pIdx->nSample );
155476 assert( iCol==nField-1 );
155477 pRec->nField = nField;
155479 || pParse->db->mallocFailed
155482 /* Unless i==pIdx->nSample, indicating that pRec is larger than
155485 assert( i<=pIdx->nSample && i>=0 );
155486 pRec->nField = iCol+1;
155487 assert( i==pIdx->nSample
155489 || pParse->db->mallocFailed );
155494 ** If (i>0), then pRec must also be greater than sample (i-1). */
155496 pRec->nField = iCol;
155498 || pParse->db->mallocFailed );
155501 pRec->nField = nField;
155502 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
155503 || pParse->db->mallocFailed );
155511 assert( iCol==nField-1 );
155516 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
155519 if( i>=pIdx->nSample ){
155520 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
155528 iGap = iUpper - iLower;
155536 aStat[1] = pIdx->aAvgEq[nField-1];
155539 /* Restore the pRec->nField value before returning. */
155540 pRec->nField = nField;
155559 if( pTerm->truthProb<=0 ){
155560 nRet += pTerm->truthProb;
155561 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
155562 nRet -= 20; assert( 20==sqlite3LogEst(4) );
155574 assert( iCol>=0 && iCol<pIdx->nColumn );
155575 if( !pIdx->zColAff ){
155578 assert( pIdx->zColAff[iCol]!=0 );
155579 return pIdx->zColAff[iCol];
155587 ** range-scan on a skip-scan index. For example:
155592 ** Value pLoop->nOut is currently set to the estimated number of rows
155603 ** N is the total number of samples, the pLoop->nOut value is adjusted
155606 ** nOut = nOut * ( min(U - L, 1) / N )
155627 Index *p = pLoop->u.btree.pIndex;
155628 int nEq = pLoop->u.btree.nEq;
155629 sqlite3 *db = pParse->db;
155630 int nLower = -1;
155631 int nUpper = p->nSample+1;
155640 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
155642 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
155646 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
155647 nUpper = p2 ? 0 : p->nSample;
155653 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
155654 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
155664 nDiff = (nUpper - nLower);
155673 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
155674 pLoop->nOut -= nAdjust;
155676 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
155677 nLower, nUpper, nAdjust*-1, pLoop->nOut));
155707 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
155715 ** left-most column of the index). Or, if the query is:
155740 int nOut = pLoop->nOut;
155744 Index *p = pLoop->u.btree.pIndex;
155745 int nEq = pLoop->u.btree.nEq;
155747 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
155748 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
155750 if( nEq==pBuilder->nRecValid ){
155751 UnpackedRecord *pRec = pBuilder->pRec;
155753 int nBtm = pLoop->u.btree.nBtm;
155754 int nTop = pLoop->u.btree.nTop;
155759 ** key-prefix formed by the nEq values matched against the nEq left-most
155773 ** The number of rows between the two bounds is then just iUpper-iLower.
155777 int iLwrIdx = -2; /* aSample[] for the lower bound */
155778 int iUprIdx = -1; /* aSample[] for the upper bound */
155781 testcase( pRec->nField!=pBuilder->nRecValid );
155782 pRec->nField = pBuilder->nRecValid;
155787 iUpper = p->nRowEst0;
155789 /* Note: this call could be optimized away - since the same values must
155796 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
155797 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
155798 assert( p->aSortOrder!=0 );
155799 if( p->aSortOrder[nEq] ){
155808 Expr *pExpr = pLower->pExpr->pRight;
155815 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
155817 nOut--;
155825 Expr *pExpr = pUpper->pExpr->pRight;
155832 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
155834 nOut--;
155839 pBuilder->pRec = pRec;
155842 nNew = sqlite3LogEst(iUpper - iLower);
155847 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
155868 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
155873 ** has an application-defined likelihood(), assume the range is
155874 ** reduced by an additional 75%. This means that, by default, an open-ended
155878 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
155879 nNew -= 20;
155882 nOut -= (pLower!=0) + (pUpper!=0);
155886 if( pLoop->nOut>nOut ){
155888 pLoop->nOut, nOut));
155891 pLoop->nOut = (LogEst)nOut;
155899 ** the histogram data. This only works when x is the left-most
155906 ** non-zero.
155919 Index *p = pBuilder->pNew->u.btree.pIndex;
155920 int nEq = pBuilder->pNew->u.btree.nEq;
155921 UnpackedRecord *pRec = pBuilder->pRec;
155927 assert( nEq<=p->nColumn );
155928 assert( p->aSample!=0 );
155929 assert( p->nSample>0 );
155930 assert( pBuilder->nRecValid<nEq );
155934 if( pBuilder->nRecValid<(nEq-1) ){
155940 if( nEq>=p->nColumn ){
155945 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
155946 pBuilder->pRec = pRec;
155949 pBuilder->nRecValid = nEq;
155953 p->zName, nEq-1, (int)a[1]));
155963 ** an IN constraint where the right-hand side of the IN operator
155970 ** non-zero.
155983 Index *p = pBuilder->pNew->u.btree.pIndex;
155984 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
155985 int nRecValid = pBuilder->nRecValid;
155991 assert( p->aSample!=0 );
155992 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
155994 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
155996 pBuilder->nRecValid = nRecValid;
156004 assert( pBuilder->nRecValid==nRecValid );
156016 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
156021 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
156022 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
156023 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
156024 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
156025 if( pTerm->eOperator & WO_SINGLE ){
156026 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
156028 pTerm->leftCursor, pTerm->u.x.leftColumn);
156029 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
156031 pTerm->u.pOrInfo->indexable);
156033 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
156036 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
156037 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
156041 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
156042 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
156044 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
156045 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
156047 if( pTerm->iParent>=0 ){
156048 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
156051 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
156062 for(i=0; i<pWC->nTerm; i++){
156063 sqlite3WhereTermPrint(&pWC->a[i], i);
156073 WhereInfo *pWInfo = pWC->pWInfo;
156074 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
156075 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
156076 Table *pTab = pItem->pTab;
156077 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
156078 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
156079 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
156081 pItem->zAlias ? pItem->zAlias : pTab->zName);
156082 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
156084 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
156086 int i = sqlite3Strlen30(zName) - 1;
156087 while( zName[i]!='_' ) i--;
156090 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
156096 if( p->u.vtab.idxStr ){
156098 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
156100 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
156102 sqlite3DebugPrintf(" %-19s", z);
156105 if( p->wsFlags & WHERE_SKIPSCAN ){
156106 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
156108 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
156110 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
156111 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
156113 for(i=0; i<p->nLTerm; i++){
156114 sqlite3WhereTermPrint(p->aLTerm[i], i);
156125 p->aLTerm = p->aLTermSpace;
156126 p->nLTerm = 0;
156127 p->nLSlot = ArraySize(p->aLTermSpace);
156128 p->wsFlags = 0;
156135 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
156136 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
156137 sqlite3_free(p->u.vtab.idxStr);
156138 p->u.vtab.needFree = 0;
156139 p->u.vtab.idxStr = 0;
156140 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
156141 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
156142 sqlite3DbFreeNN(db, p->u.btree.pIndex);
156143 p->u.btree.pIndex = 0;
156152 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156158 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
156162 if( p->nLSlot>=n ) return SQLITE_OK;
156164 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
156166 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
156167 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
156168 p->aLTerm = paNew;
156169 p->nLSlot = n;
156178 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
156183 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
156184 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
156185 pFrom->u.vtab.needFree = 0;
156186 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
156187 pFrom->u.btree.pIndex = 0;
156205 sqlite3WhereClauseClear(&pWInfo->sWC);
156206 while( pWInfo->pLoops ){
156207 WhereLoop *p = pWInfo->pLoops;
156208 pWInfo->pLoops = p->pNextLoop;
156211 assert( pWInfo->pExprMods==0 );
156212 while( pWInfo->pMemToFree ){
156213 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
156214 sqlite3DbFreeNN(db, pWInfo->pMemToFree);
156215 pWInfo->pMemToFree = pNext;
156223 while( pWInfo->pExprMods ){
156224 WhereExprMod *p = pWInfo->pExprMods;
156225 pWInfo->pExprMods = p->pNext;
156226 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
156227 sqlite3DbFree(pWInfo->pParse->db, p);
156245 ** was added because if X uses skip-scan less than Y it still might
156248 ** than a non-covering index even if it is a proper subset.
156255 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
156258 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0;
156259 if( pY->nSkip > pX->nSkip ) return 0;
156260 for(i=pX->nLTerm-1; i>=0; i--){
156261 if( pX->aLTerm[i]==0 ) continue;
156262 for(j=pY->nLTerm-1; j>=0; j--){
156263 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
156267 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
156268 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
156289 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
156290 for(; p; p=p->pNextLoop){
156291 if( p->iTab!=pTemplate->iTab ) continue;
156292 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
156297 pTemplate->rRun, pTemplate->nOut,
156298 MIN(p->rRun, pTemplate->rRun),
156299 MIN(p->nOut - 1, pTemplate->nOut)));
156300 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
156301 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
156306 pTemplate->rRun, pTemplate->nOut,
156307 MAX(p->rRun, pTemplate->rRun),
156308 MAX(p->nOut + 1, pTemplate->nOut)));
156309 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
156310 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
156334 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
156335 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
156344 assert( p->rSetup==0 || pTemplate->rSetup==0
156345 || p->rSetup==pTemplate->rSetup );
156349 ** rSetup. Call this SETUP-INVARIANT */
156350 assert( p->rSetup>=pTemplate->rSetup );
156352 /* Any loop using an appliation-defined index (or PRIMARY KEY or
156354 ** than an automatic index. Unless it is a skip-scan. */
156355 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
156356 && (pTemplate->nSkip)==0
156357 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
156358 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
156359 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
156369 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
156370 && p->rSetup<=pTemplate->rSetup /* (2a) */
156371 && p->rRun<=pTemplate->rRun /* (2b) */
156372 && p->nOut<=pTemplate->nOut /* (2c) */
156382 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
156383 && p->rRun>=pTemplate->rRun /* (2a) */
156384 && p->nOut>=pTemplate->nOut /* (2b) */
156386 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
156402 ** If pBuilder->pOrSet is not NULL then we care about only the
156404 ** information is gathered in the pBuilder->pOrSet object. This special
156407 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
156419 WhereInfo *pWInfo = pBuilder->pWInfo;
156420 sqlite3 *db = pWInfo->pParse->db;
156424 if( pBuilder->iPlanLimit==0 ){
156426 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
156429 pBuilder->iPlanLimit--;
156431 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
156433 /* If pBuilder->pOrSet is defined, then only keep track of the costs
156436 if( pBuilder->pOrSet!=0 ){
156437 if( pTemplate->nLTerm ){
156439 u16 n = pBuilder->pOrSet->n;
156442 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
156443 pTemplate->nOut);
156446 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
156447 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156456 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
156464 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156480 sqlite3WhereLoopPrint(p, pBuilder->pWC);
156485 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
156493 p->pNextLoop = 0;
156498 WhereLoop **ppTail = &p->pNextLoop;
156505 *ppTail = pToDel->pNextLoop;
156509 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
156516 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
156517 Index *pIndex = p->u.btree.pIndex;
156518 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
156519 p->u.btree.pIndex = 0;
156538 ** TODO --> Perhaps this is something that could be improved by better
156542 ** value corresponds to -1 in LogEst notation, so this means decrement
156549 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
156550 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
156560 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
156562 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
156564 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
156565 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
156567 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
156568 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
156569 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
156570 for(j=pLoop->nLTerm-1; j>=0; j--){
156571 pX = pLoop->aLTerm[j];
156574 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
156577 if( pLoop->maskSelf==pTerm->prereqAll ){
156581 ** "self-culling".
156583 ** 2022-03-24: Self-culling only applies if either the extra terms
156584 ** are straight comparison operators that are non-true with NULL
156587 if( (pTerm->eOperator & 0x3f)!=0
156588 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
156591 pLoop->wsFlags |= WHERE_SELFCULL;
156594 if( pTerm->truthProb<=0 ){
156597 pLoop->nOut += pTerm->truthProb;
156601 pLoop->nOut--;
156602 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
156603 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
156605 Expr *pRight = pTerm->pExpr->pRight;
156607 testcase( pTerm->pExpr->op==TK_IS );
156608 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
156614 pTerm->wtFlags |= TERM_HEURTRUTH;
156621 if( pLoop->nOut > nRow-iReduce ){
156622 pLoop->nOut = nRow - iReduce;
156650 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
156653 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
156662 assert( ExprUseXList(pTerm->pExpr->pLeft) );
156663 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
156664 pRhs = pTerm->pExpr->pRight;
156666 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
156668 pRhs = pRhs->x.pList->a[i].pExpr;
156675 if( pLhs->op!=TK_COLUMN
156676 || pLhs->iTable!=iCur
156677 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
156678 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
156683 testcase( pLhs->iColumn==XN_ROWID );
156685 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
156690 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
156697 ** compiled with -DSQLITE_ENABLE_COSTMULT
156706 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
156709 ** When this function is called, pBuilder->pNew->nOut contains the
156714 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
156723 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
156724 Parse *pParse = pWInfo->pParse; /* Parsing context */
156725 sqlite3 *db = pParse->db; /* Database connection malloc context */
156730 Bitmask saved_prereq; /* Original value of pNew->prereq */
156731 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
156732 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
156733 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
156734 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
156735 u16 saved_nSkip; /* Original value of pNew->nSkip */
156736 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
156737 LogEst saved_nOut; /* Original value of pNew->nOut */
156743 pNew = pBuilder->pNew;
156744 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
156746 pProbe->pTable->zName,pProbe->zName,
156747 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
156749 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
156750 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
156751 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
156754 assert( pNew->u.btree.nBtm==0 );
156757 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
156759 assert( pNew->u.btree.nEq<pProbe->nColumn );
156760 assert( pNew->u.btree.nEq<pProbe->nKeyCol
156761 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
156763 saved_nEq = pNew->u.btree.nEq;
156764 saved_nBtm = pNew->u.btree.nBtm;
156765 saved_nTop = pNew->u.btree.nTop;
156766 saved_nSkip = pNew->nSkip;
156767 saved_nLTerm = pNew->nLTerm;
156768 saved_wsFlags = pNew->wsFlags;
156769 saved_prereq = pNew->prereq;
156770 saved_nOut = pNew->nOut;
156771 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
156773 pNew->rSetup = 0;
156774 rSize = pProbe->aiRowLogEst[0];
156777 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
156782 int nRecValid = pBuilder->nRecValid;
156784 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
156789 if( pTerm->prereqRight & pNew->maskSelf ) continue;
156793 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
156795 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
156798 ** See tag-20191211-002 for the vtab equivalent.
156800 ** 2022-06-06: See https://sqlite.org/forum/forumpost/206d99a16dd9212f
156803 ** not-NULL condition on the left table of the RIGHT JOIN.
156805 ** 2022-06-10: The same condition applies to termCanDriveIndex() above.
156808 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ){
156809 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
156810 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_RIGHT );
156811 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
156812 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
156813 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
156814 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
156815 || pTerm->pExpr->w.iJoin != pSrc->iCursor
156821 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
156822 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
156824 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
156826 pNew->wsFlags = saved_wsFlags;
156827 pNew->u.btree.nEq = saved_nEq;
156828 pNew->u.btree.nBtm = saved_nBtm;
156829 pNew->u.btree.nTop = saved_nTop;
156830 pNew->nLTerm = saved_nLTerm;
156831 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156832 pNew->aLTerm[pNew->nLTerm++] = pTerm;
156833 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
156836 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
156837 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
156838 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
156842 Expr *pExpr = pTerm->pExpr;
156853 for(i=0; i<pNew->nLTerm-1; i++){
156854 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
156856 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
156858 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
156860 if( pProbe->hasStat1 && rLogSize>=10 ){
156867 ** the left-most index column, M==N.
156877 ** with the index, as using an index has better worst-case behavior.
156882 M = pProbe->aiRowLogEst[saved_nEq];
156884 /* TUNING v----- 10 to bias toward indexed IN */
156885 x = M + logK + 10 - (nIn + rLogSize);
156894 " nInMul=%d) prefers skip-scan\n",
156896 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
156905 pNew->wsFlags |= WHERE_COLUMN_IN;
156907 int iCol = pProbe->aiColumn[saved_nEq];
156908 pNew->wsFlags |= WHERE_COLUMN_EQ;
156909 assert( saved_nEq==pNew->u.btree.nEq );
156911 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
156913 if( iCol==XN_ROWID || pProbe->uniqNotNull
156914 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
156916 pNew->wsFlags |= WHERE_ONEROW;
156918 pNew->wsFlags |= WHERE_UNQ_WANTED;
156921 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
156923 pNew->wsFlags |= WHERE_COLUMN_NULL;
156927 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
156928 pNew->u.btree.nBtm = whereRangeVectorLen(
156929 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156933 if( pTerm->wtFlags & TERM_LIKEOPT ){
156937 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
156938 assert( pTop->wtFlags & TERM_LIKEOPT );
156939 assert( pTop->eOperator==WO_LT );
156940 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
156941 pNew->aLTerm[pNew->nLTerm++] = pTop;
156942 pNew->wsFlags |= WHERE_TOP_LIMIT;
156943 pNew->u.btree.nTop = 1;
156949 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
156950 pNew->u.btree.nTop = whereRangeVectorLen(
156951 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
156954 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
156955 pNew->aLTerm[pNew->nLTerm-2] : 0;
156958 /* At this point pNew->nOut is set to the number of rows expected to
156962 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
156963 assert( pNew->nOut==saved_nOut );
156964 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
156969 int nEq = ++pNew->u.btree.nEq;
156972 assert( pNew->nOut==saved_nOut );
156973 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
156976 pNew->nOut += pTerm->truthProb;
156977 pNew->nOut -= nIn;
156982 && pProbe->nSample
156983 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
156984 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
156987 Expr *pExpr = pTerm->pExpr;
156992 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
156994 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
156999 pNew->nOut = sqlite3LogEst(nOut);
157003 ** See tag-202002240-1 */
157004 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
157013 pTerm->wtFlags |= TERM_HIGHTRUTH;
157014 if( pTerm->wtFlags & TERM_HEURTRUTH ){
157018 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
157021 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
157022 pNew->nOut -= nIn;
157028 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
157033 pNew->nOut += 10;
157040 ** it to pNew->rRun, which is currently set to the cost of the index
157041 ** seek only. Then, if this is a non-covering index, add the cost of
157043 assert( pSrc->pTab->szTabRow>0 );
157044 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
157045 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
157046 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
157047 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
157049 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
157051 nOutUnadjusted = pNew->nOut;
157052 pNew->rRun += nInMul + nIn;
157053 pNew->nOut += nInMul + nIn;
157054 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
157057 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
157058 pNew->nOut = saved_nOut;
157060 pNew->nOut = nOutUnadjusted;
157063 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
157064 && pNew->u.btree.nEq<pProbe->nColumn
157065 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
157066 pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY)
157070 pNew->nOut = saved_nOut;
157072 pBuilder->nRecValid = nRecValid;
157075 pNew->prereq = saved_prereq;
157076 pNew->u.btree.nEq = saved_nEq;
157077 pNew->u.btree.nBtm = saved_nBtm;
157078 pNew->u.btree.nTop = saved_nTop;
157079 pNew->nSkip = saved_nSkip;
157080 pNew->wsFlags = saved_wsFlags;
157081 pNew->nOut = saved_nOut;
157082 pNew->nLTerm = saved_nLTerm;
157084 /* Consider using a skip-scan if there are no WHERE clause constraints
157085 ** available for the left-most terms of the index, and if the average
157086 ** number of repeats in the left-most terms is at least 18.
157096 && saved_nEq+1<pProbe->nKeyCol
157097 && saved_nEq==pNew->nLTerm
157098 && pProbe->noSkipScan==0
157099 && pProbe->hasStat1!=0
157101 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
157102 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
157105 pNew->u.btree.nEq++;
157106 pNew->nSkip++;
157107 pNew->aLTerm[pNew->nLTerm++] = 0;
157108 pNew->wsFlags |= WHERE_SKIPSCAN;
157109 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
157110 pNew->nOut -= nIter;
157111 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
157112 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
157115 pNew->nOut = saved_nOut;
157116 pNew->u.btree.nEq = saved_nEq;
157117 pNew->nSkip = saved_nSkip;
157118 pNew->wsFlags = saved_wsFlags;
157122 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
157143 if( pIndex->bUnordered ) return 0;
157144 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
157145 for(ii=0; ii<pOB->nExpr; ii++){
157146 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
157148 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
157149 if( pExpr->iColumn<0 ) return 1;
157150 for(jj=0; jj<pIndex->nKeyCol; jj++){
157151 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
157153 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
157154 for(jj=0; jj<pIndex->nKeyCol; jj++){
157155 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
157156 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
157179 pParse = pWC->pWInfo->pParse;
157180 while( pWhere->op==TK_AND ){
157181 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
157182 pWhere = pWhere->pRight;
157184 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
157185 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
157187 pExpr = pTerm->pExpr;
157188 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
157191 && (pTerm->wtFlags & TERM_VNULL)==0
157201 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
157202 ** a b-tree table, not a virtual table.
157204 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
157209 ** cost = nRow * 3.0 // full-table scan
157211 ** cost = nRow * (K+3.0) // scan of non-covering index
157218 ** the index b-tree:
157221 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
157225 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
157230 ** log(nRow) factor is omitted from a non-covering index scan in order to
157231 ** bias the scoring in favor of using an index, since the worst-case
157232 ** performance of using an index is far better than the worst-case performance
157243 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
157254 pNew = pBuilder->pNew;
157255 pWInfo = pBuilder->pWInfo;
157256 pTabList = pWInfo->pTabList;
157257 pSrc = pTabList->a + pNew->iTab;
157258 pTab = pSrc->pTab;
157259 pWC = pBuilder->pWC;
157260 assert( !IsVirtual(pSrc->pTab) );
157262 if( pSrc->fg.isIndexedBy ){
157263 assert( pSrc->fg.isCte==0 );
157265 pProbe = pSrc->u2.pIBIndex;
157267 pProbe = pTab->pIndex;
157281 sPk.szIdxRow = pTab->szTabRow;
157283 aiRowEstPk[0] = pTab->nRowLogEst;
157285 pFirst = pSrc->pTab->pIndex;
157286 if( pSrc->fg.notIndexed==0 ){
157293 rSize = pTab->nRowLogEst;
157297 if( !pBuilder->pOrSet /* Not part of an OR optimization */
157298 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
157299 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
157300 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
157301 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
157303 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
157304 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
157305 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
157307 /* Generate auto-index WhereLoops */
157310 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
157312 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
157313 if( pTerm->prereqRight & pNew->maskSelf ) continue;
157315 pNew->u.btree.nEq = 1;
157316 pNew->nSkip = 0;
157317 pNew->u.btree.pIndex = 0;
157318 pNew->nLTerm = 1;
157319 pNew->aLTerm[0] = pTerm;
157320 /* TUNING: One-time cost for computing the automatic index is
157323 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
157328 pNew->rSetup = rLogSize + rSize;
157329 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
157330 pNew->rSetup += 28;
157332 pNew->rSetup -= 10;
157334 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
157335 if( pNew->rSetup<0 ) pNew->rSetup = 0;
157340 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
157341 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
157342 pNew->wsFlags = WHERE_AUTO_INDEX;
157343 pNew->prereq = mPrereq | pTerm->prereqRight;
157353 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
157355 if( pProbe->pPartIdxWhere!=0
157356 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
157357 pProbe->pPartIdxWhere)
157359 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
157362 if( pProbe->bNoQuery ) continue;
157363 rSize = pProbe->aiRowLogEst[0];
157364 pNew->u.btree.nEq = 0;
157365 pNew->u.btree.nBtm = 0;
157366 pNew->u.btree.nTop = 0;
157367 pNew->nSkip = 0;
157368 pNew->nLTerm = 0;
157369 pNew->iSortIdx = 0;
157370 pNew->rSetup = 0;
157371 pNew->prereq = mPrereq;
157372 pNew->nOut = rSize;
157373 pNew->u.btree.pIndex = pProbe;
157374 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
157377 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
157378 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
157380 pNew->wsFlags = WHERE_IPK;
157383 pNew->iSortIdx = b ? iSortIdx : 0;
157386 ** since index lookups have better worst-case performance if our
157397 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
157399 pNew->rRun = rSize + 16;
157401 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157404 pNew->nOut = rSize;
157408 if( pProbe->isCovering ){
157409 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
157412 m = pSrc->colUsed & pProbe->colNotIdxed;
157413 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
157419 || pProbe->pPartIdxWhere!=0
157420 || pSrc->fg.isIndexedBy
157422 && pProbe->bUnordered==0
157423 && (pProbe->szIdxRow<pTab->szTabRow)
157424 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
157426 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
157429 pNew->iSortIdx = b ? iSortIdx : 0;
157434 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
157436 /* If this is a non-covering index scan, add in the cost of
157443 int iCur = pSrc->iCursor;
157444 WhereClause *pWC2 = &pWInfo->sWC;
157445 for(ii=0; ii<pWC2->nTerm; ii++){
157446 WhereTerm *pTerm = &pWC2->a[ii];
157447 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
157452 if( pTerm->truthProb<=0 ){
157453 nLookup += pTerm->truthProb;
157455 nLookup--;
157456 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
157460 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
157462 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
157464 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
157465 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
157467 ** positioned to the correct row during the right-join no-match
157472 pNew->nOut = rSize;
157477 pBuilder->bldFlags1 = 0;
157479 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
157480 /* If a non-unique index is used, or if a prefix of the key for
157481 ** unique index is used (making the index functionally non-unique)
157484 pTab->tabFlags |= TF_StatsUsed;
157487 sqlite3Stat4ProbeFree(pBuilder->pRec);
157488 pBuilder->nRecValid = 0;
157489 pBuilder->pRec = 0;
157501 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
157502 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
157503 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
157508 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
157536 WhereClause *pWC = pBuilder->pWC;
157539 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
157543 WhereLoop *pNew = pBuilder->pNew;
157544 Parse *pParse = pBuilder->pWInfo->pParse;
157545 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
157546 int nConstraint = pIdxInfo->nConstraint;
157550 pNew->prereq = mPrereq;
157554 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157556 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
157557 pIdxCons->usable = 0;
157558 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
157559 && (pTerm->eOperator & mExclude)==0
157562 pIdxCons->usable = 1;
157568 assert( pIdxInfo->needToFreeIdxStr==0 );
157569 pIdxInfo->idxStr = 0;
157570 pIdxInfo->idxNum = 0;
157571 pIdxInfo->orderByConsumed = 0;
157572 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
157573 pIdxInfo->estimatedRows = 25;
157574 pIdxInfo->idxFlags = 0;
157575 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
157576 pHidden->mHandleIn = 0;
157579 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
157586 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
157592 mxTerm = -1;
157593 assert( pNew->nLSlot>=nConstraint );
157594 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
157595 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
157596 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
157599 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
157601 int j = pIdxCons->iTermOffset;
157604 || j>=pWC->nTerm
157605 || pNew->aLTerm[iTerm]!=0
157606 || pIdxCons->usable==0
157608 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157609 testcase( pIdxInfo->needToFreeIdxStr );
157612 testcase( iTerm==nConstraint-1 );
157614 testcase( j==pWC->nTerm-1 );
157615 pTerm = &pWC->a[j];
157616 pNew->prereq |= pTerm->prereqRight;
157617 assert( iTerm<pNew->nLSlot );
157618 pNew->aLTerm[iTerm] = pTerm;
157625 pNew->u.vtab.omitMask |= 1<<iTerm;
157629 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
157630 pNew->u.vtab.bOmitOffset = 1;
157633 if( SMASKBIT32(i) & pHidden->mHandleIn ){
157634 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
157635 }else if( (pTerm->eOperator & WO_IN)!=0 ){
157641 pIdxInfo->orderByConsumed = 0;
157642 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
157653 if( pIdxInfo->needToFreeIdxStr ){
157654 sqlite3_free(pIdxInfo->idxStr);
157655 pIdxInfo->idxStr = 0;
157656 pIdxInfo->needToFreeIdxStr = 0;
157664 pNew->nLTerm = mxTerm+1;
157666 if( pNew->aLTerm[i]==0 ){
157667 /* The non-zero argvIdx values must be contiguous. Raise an
157669 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
157670 testcase( pIdxInfo->needToFreeIdxStr );
157674 assert( pNew->nLTerm<=pNew->nLSlot );
157675 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
157676 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
157677 pIdxInfo->needToFreeIdxStr = 0;
157678 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
157679 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
157680 pIdxInfo->nOrderBy : 0);
157681 pNew->rSetup = 0;
157682 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
157683 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
157687 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
157688 pNew->wsFlags |= WHERE_ONEROW;
157690 pNew->wsFlags &= ~WHERE_ONEROW;
157693 if( pNew->u.vtab.needFree ){
157694 sqlite3_free(pNew->u.vtab.idxStr);
157695 pNew->u.vtab.needFree = 0;
157699 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
157722 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
157724 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
157725 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
157726 if( pX->pLeft ){
157727 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
157729 zRet = (pC ? pC->zName : sqlite3StrBINARY);
157742 if( m & pHidden->mIn ){
157744 pHidden->mHandleIn &= ~m;
157746 pHidden->mHandleIn |= m;
157757 ** on the right-hand-side of constraint iCons.
157767 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
157768 rc = SQLITE_MISUSE; /* EV: R-30545-25046 */
157770 if( pH->aRhs[iCons]==0 ){
157771 WhereTerm *pTerm = &pH->pWC->a[pIdxInfo->aConstraint[iCons].iTermOffset];
157773 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
157774 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
157778 pVal = pH->aRhs[iCons];
157782 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
157783 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
157794 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
157795 return pHidden->eDistinct;
157803 ** prepared is read-only, then just start read transactions on all
157807 ** This is used by the (built-in) sqlite_dbpage virtual table.
157811 Parse *pParse = pHidden->pParse;
157812 int nDb = pParse->db->nDb;
157817 if( pParse->writeMask ){
157827 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
157848 ** mUnusable should always be configured as "not-usable" for xBestIndex.
157869 pWInfo = pBuilder->pWInfo;
157870 pParse = pWInfo->pParse;
157871 pWC = pBuilder->pWC;
157872 pNew = pBuilder->pNew;
157873 pSrc = &pWInfo->pTabList->a[pNew->iTab];
157874 assert( IsVirtual(pSrc->pTab) );
157877 pNew->rSetup = 0;
157878 pNew->wsFlags = WHERE_VIRTUALTABLE;
157879 pNew->nLTerm = 0;
157880 pNew->u.vtab.needFree = 0;
157881 nConstraint = p->nConstraint;
157882 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
157883 freeIndexInfo(pParse->db, p);
157888 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
157905 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
157918 mBestNoIn = pNew->prereq & ~mPrereq;
157933 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
157944 if( pNew->prereq==mPrereq ){
157970 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
157971 freeIndexInfo(pParse->db, p);
157972 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
157986 WhereInfo *pWInfo = pBuilder->pWInfo;
157997 pWC = pBuilder->pWC;
157998 pWCEnd = pWC->a + pWC->nTerm;
157999 pNew = pBuilder->pNew;
158001 pItem = pWInfo->pTabList->a + pNew->iTab;
158002 iCur = pItem->iCursor;
158004 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
158005 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
158007 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
158008 if( (pTerm->eOperator & WO_OR)!=0
158009 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
158011 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
158012 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
158020 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
158021 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
158022 if( (pOrTerm->eOperator & WO_AND)!=0 ){
158023 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
158024 }else if( pOrTerm->leftCursor==iCur ){
158025 tempWC.pWInfo = pWC->pWInfo;
158037 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
158038 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
158044 if( IsVirtual(pItem->pTab) ){
158077 pNew->nLTerm = 1;
158078 pNew->aLTerm[0] = pTerm;
158079 pNew->wsFlags = WHERE_MULTI_OR;
158080 pNew->rSetup = 0;
158081 pNew->iSortIdx = 0;
158082 memset(&pNew->u, 0, sizeof(pNew->u));
158085 ** of all sub-scans required by the OR-scan. However, due to rounding
158086 ** errors, it may be that the cost of the OR-scan is equal to its
158087 ** most expensive sub-scan. Add the smallest possible penalty
158094 ** the planner may elect to "OR" together a full-table scan and an
158096 pNew->rRun = sSum.a[i].rRun + 1;
158097 pNew->nOut = sSum.a[i].nOut;
158098 pNew->prereq = sSum.a[i].prereq;
158101 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
158111 WhereInfo *pWInfo = pBuilder->pWInfo;
158115 SrcList *pTabList = pWInfo->pTabList;
158117 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
158118 sqlite3 *db = pWInfo->pParse->db;
158126 pNew = pBuilder->pNew;
158128 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
158129 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
158131 pNew->iTab = iTab;
158132 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
158133 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
158135 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
158143 ** prevent FROM-clause terms from moving from the right side of
158147 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
158149 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
158154 if( IsVirtual(pItem->pTab) ){
158157 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
158158 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
158167 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
158170 mPrior |= pNew->maskSelf;
158171 if( rc || db->mallocFailed ){
158201 ** pOrderBy terms must be matched in strict left-to-right order.
158208 u16 nLoop, /* Number of entries in pPath->aLoop[] */
158209 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
158215 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
158231 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
158234 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
158238 ** We say the WhereLoop is "one-row" if it generates no more than one
158239 ** row of output. A WhereLoop is one-row if all of the following are true:
158242 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
158243 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
158245 ** We say the WhereLoop is "order-distinct" if the set of columns from
158247 ** row of the WhereLoop. Every one-row WhereLoop is automatically
158248 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
158249 ** is not order-distinct. To be order-distinct is not quite the same as being
158251 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
158252 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
158256 ** automatically order-distinct.
158262 nOrderBy = pOrderBy->nExpr;
158263 testcase( nOrderBy==BMS-1 );
158264 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
158266 obDone = MASKBIT(nOrderBy)-1;
158274 if( iLoop>0 ) ready |= pLoop->maskSelf;
158276 pLoop = pPath->aLoop[iLoop];
158281 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
158282 if( pLoop->u.vtab.isOrdered
158289 pLoop->u.btree.nDistinctCol = 0;
158291 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
158300 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158302 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
158303 if( pOBExpr->iTable!=iCur ) continue;
158304 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
158307 if( pTerm->eOperator==WO_IN ){
158313 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
158314 if( j>=pLoop->nLTerm ) continue;
158316 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
158317 Parse *pParse = pWInfo->pParse;
158318 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
158319 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
158321 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
158324 testcase( pTerm->pExpr->op==TK_IS );
158329 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
158330 if( pLoop->wsFlags & WHERE_IPK ){
158334 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
158337 nKeyCol = pIndex->nKeyCol;
158338 nColumn = pIndex->nColumn;
158339 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
158340 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
158341 || !HasRowid(pIndex->pTable));
158342 /* All relevant terms of the index must also be non-NULL in order
158345 ** made at tag-20210426-1 below */
158347 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
158358 assert( j>=pLoop->u.btree.nEq
158359 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
158361 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
158362 u16 eOp = pLoop->aLTerm[j]->eOperator;
158367 ** the loop need to be marked as not order-distinct because it can
158386 ** j<pLoop->u.btree.nEq constraint above. Any equality other
158389 Expr *pX = pLoop->aLTerm[j]->pExpr;
158390 for(i=j+1; i<pLoop->u.btree.nEq; i++){
158391 if( pLoop->aLTerm[i]->pExpr==pX ){
158392 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
158401 ** (revIdx) for the j-th column of the index.
158404 iColumn = pIndex->aiColumn[j];
158405 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
158406 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
158413 ** WhereLoop is not well-ordered. tag-20210426-1
158417 && j>=pLoop->u.btree.nEq
158418 && pIndex->pTable->aCol[iColumn].notNull==0
158427 /* Find the ORDER BY term that corresponds to the j-th column
158433 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
158439 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
158440 if( pOBExpr->iTable!=iCur ) continue;
158441 if( pOBExpr->iColumn!=iColumn ) continue;
158443 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
158449 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
158450 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
158453 pLoop->u.btree.nDistinctCol = j+1;
158463 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
158468 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
158473 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
158474 if( j==pLoop->u.btree.nEq ){
158475 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
158499 } /* end-if not one-row */
158503 orderDistinctMask |= pLoop->maskSelf;
158508 p = pOrderBy->a[i].pExpr;
158509 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
158516 } /* End the loop over all WhereLoops from outer-most down to inner-most */
158519 for(i=nOrderBy-1; i>0; i--){
158520 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
158525 return -1;
158532 ** BY clause - and so any order that groups rows as required satisfies the
158549 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
158550 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
158553 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
158554 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
158555 return pWInfo->sorted;
158563 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
158564 if( pLast ) zName[i++] = pLast->cId;
158586 ** Or, if the order-by clause has X terms but only the last Y
158587 ** terms are out of order, then block-sorting will reduce the
158597 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
158605 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
158606 nRow = pWInfo->iLimit;
158607 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
158610 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
158617 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
158619 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
158650 pParse = pWInfo->pParse;
158651 db = pParse->db;
158652 nLoop = pWInfo->nLevel;
158654 ** For 2-way joins, the 5 best paths are followed.
158657 assert( nLoop<=pWInfo->pTabList->nSrc );
158658 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
158665 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
158668 nOrderBy = pWInfo->pOrderBy->nExpr;
158680 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
158681 pFrom->aLoop = pX;
158686 ** is either zero - meaning it has not yet been initialized - or the
158701 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
158709 ** -1, indicating that the result set may or may not be ordered,
158711 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
158720 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
158724 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
158726 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
158728 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
158729 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
158730 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
158741 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
158742 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
158743 nOut = pFrom->nRow + pWLoop->nOut;
158744 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
158747 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
158750 revMask = pFrom->revLoop;
158765 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
158766 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
158770 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
158774 ** mxChoice best-so-far paths.
158776 ** First look for an existing path among best-so-far paths
158780 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
158781 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
158782 ** of legal values for isOrdered, -1..64.
158785 if( pTo->maskLoop==maskNew
158786 && ((pTo->isOrdered^isOrdered)&0x80)==0
158788 testcase( jj==nTo-1 );
158793 /* None of the existing best-so-far paths match the candidate. */
158798 ** paths currently in the best-so-far buffer. So discard
158802 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
158810 ** needs to be added to the set of best-so-far paths. */
158821 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
158827 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
158833 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
158835 if( pTo->rCost<rCost
158836 || (pTo->rCost==rCost
158837 && (pTo->nRow<nOut
158838 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
158845 "Skip %s cost=%-3d,%3d,%3d order=%c",
158848 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
158849 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158850 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158854 testcase( pTo->rCost==rCost );
158857 testcase( pTo->rCost==rCost+1 );
158863 "Update %s cost=%-3d,%3d,%3d order=%c",
158866 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
158867 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158868 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
158873 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
158874 pTo->revLoop = revMask;
158875 pTo->nRow = nOut;
158876 pTo->rCost = rCost;
158877 pTo->rUnsorted = rUnsorted;
158878 pTo->isOrdered = isOrdered;
158879 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
158880 pTo->aLoop[iLoop] = pWLoop;
158886 if( pTo->rCost>mxCost
158887 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
158889 mxCost = pTo->rCost;
158890 mxUnsorted = pTo->rUnsorted;
158900 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
158902 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
158903 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
158904 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
158905 if( pTo->isOrdered>0 ){
158906 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
158930 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
158932 assert( pWInfo->nLevel==nLoop );
158935 WhereLevel *pLevel = pWInfo->a + iLoop;
158936 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
158937 pLevel->iFrom = pWLoop->iTab;
158938 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
158940 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
158941 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
158942 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
158946 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
158947 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used);
158948 if( rc==pWInfo->pResultSet->nExpr ){
158949 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
158952 pWInfo->bOrderedInnerLoop = 0;
158953 if( pWInfo->pOrderBy ){
158954 pWInfo->nOBSat = pFrom->isOrdered;
158955 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
158956 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
158957 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
158960 pWInfo->revMask = pFrom->revLoop;
158961 if( pWInfo->nOBSat<=0 ){
158962 pWInfo->nOBSat = 0;
158964 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
158969 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
158970 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
158973 if( rc==pWInfo->pOrderBy->nExpr ){
158974 pWInfo->bOrderedInnerLoop = 1;
158975 pWInfo->revMask = m;
158980 && pWInfo->nOBSat==1
158981 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
158983 pWInfo->bOrderedInnerLoop = 1;
158986 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
158987 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
158990 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
158991 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
158993 assert( pWInfo->sorted==0 );
158994 if( nOrder==pWInfo->pOrderBy->nExpr ){
158995 pWInfo->sorted = 1;
158996 pWInfo->revMask = revMask;
159002 pWInfo->nRowOut = pFrom->nRow;
159013 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
159016 ** Return non-zero on success, if this query can be handled by this
159017 ** no-frills query planner. Return zero if this query needs the
159018 ** general-purpose query planner.
159032 pWInfo = pBuilder->pWInfo;
159033 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
159034 assert( pWInfo->pTabList->nSrc>=1 );
159035 pItem = pWInfo->pTabList->a;
159036 pTab = pItem->pTab;
159038 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
159039 testcase( pItem->fg.isIndexedBy );
159040 testcase( pItem->fg.notIndexed );
159043 iCur = pItem->iCursor;
159044 pWC = &pWInfo->sWC;
159045 pLoop = pBuilder->pNew;
159046 pLoop->wsFlags = 0;
159047 pLoop->nSkip = 0;
159048 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
159049 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159051 testcase( pTerm->eOperator & WO_IS );
159052 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
159053 pLoop->aLTerm[0] = pTerm;
159054 pLoop->nLTerm = 1;
159055 pLoop->u.btree.nEq = 1;
159057 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
159059 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
159061 assert( pLoop->aLTermSpace==pLoop->aLTerm );
159063 || pIdx->pPartIdxWhere!=0
159064 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
159066 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
159067 for(j=0; j<pIdx->nKeyCol; j++){
159069 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
159071 testcase( pTerm->eOperator & WO_IS );
159072 pLoop->aLTerm[j] = pTerm;
159074 if( j!=pIdx->nKeyCol ) continue;
159075 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
159076 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
159077 pLoop->wsFlags |= WHERE_IDX_ONLY;
159079 pLoop->nLTerm = j;
159080 pLoop->u.btree.nEq = j;
159081 pLoop->u.btree.pIndex = pIdx;
159083 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
159087 if( pLoop->wsFlags ){
159088 pLoop->nOut = (LogEst)1;
159089 pWInfo->a[0].pWLoop = pLoop;
159090 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
159091 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
159092 pWInfo->a[0].iTabCur = iCur;
159093 pWInfo->nRowOut = 1;
159094 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
159095 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
159096 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159098 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
159100 pLoop->cId = '0';
159116 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
159117 pWalker->eCode = 0;
159124 ** Return true if the expression contains no non-deterministic SQL
159125 ** functions. Do not consider non-deterministic SQL functions that are
159126 ** part of sub-select statements.
159149 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
159150 p->cId = zLabel[i%(sizeof(zLabel)-1)];
159197 assert( pWInfo->nLevel>=2 );
159198 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
159202 assert( pWInfo->pResultSet!=0 );
159203 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
159205 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
159206 if( pWInfo->pOrderBy ){
159207 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
159209 for(i=pWInfo->nLevel-1; i>=1; i--){
159213 pLoop = pWInfo->a[i].pWLoop;
159214 pItem = &pWInfo->pTabList->a[pLoop->iTab];
159215 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
159216 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
159217 && (pLoop->wsFlags & WHERE_ONEROW)==0
159221 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
159222 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
159223 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159224 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159225 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
159226 || pTerm->pExpr->w.iJoin!=pItem->iCursor
159233 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
159234 notReady &= ~pLoop->maskSelf;
159235 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
159236 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
159237 pTerm->wtFlags |= TERM_CODED;
159240 if( i!=pWInfo->nLevel-1 ){
159241 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
159242 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
159244 pWInfo->nLevel--;
159245 assert( pWInfo->nLevel>0 );
159259 ** (3) Bloom-filter processing is not disabled. (Checked by the
159274 assert( pWInfo->nLevel>=2 );
159275 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
159276 nSearch = pWInfo->a[0].pWLoop->nOut;
159277 for(i=1; i<pWInfo->nLevel; i++){
159278 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
159280 if( (pLoop->wsFlags & reqFlags)==reqFlags
159281 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
159282 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
159284 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
159285 Table *pTab = pItem->pTab;
159286 pTab->tabFlags |= TF_StatsUsed;
159287 if( nSearch > pTab->nRowLogEst
159288 && (pTab->tabFlags & TF_HasStat1)!=0
159290 testcase( pItem->fg.jointype & JT_LEFT );
159291 pLoop->wsFlags |= WHERE_BLOOMFILTER;
159292 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
159294 "-> use Bloom-filter on loop %c because there are ~%.1e "
159296 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
159297 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
159300 nSearch += pLoop->nOut;
159323 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
159327 ** end |-- by sqlite3WhereEnd()
159334 ** scanning through all values on the right-hand side of the IN.
159337 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
159347 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
159357 ** inner loops (or around the "..." if the test occurs within the inner-
159406 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
159410 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
159427 db = pParse->db;
159431 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
159432 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
159437 testcase( pTabList->nSrc==BMS );
159438 if( pTabList->nSrc>BMS ){
159448 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
159453 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
159454 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
159457 nByteWInfo = ROUND8P(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
159459 if( db->mallocFailed ){
159464 pWInfo->pParse = pParse;
159465 pWInfo->pTabList = pTabList;
159466 pWInfo->pOrderBy = pOrderBy;
159467 pWInfo->pWhere = pWhere;
159468 pWInfo->pResultSet = pResultSet;
159469 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
159470 pWInfo->nLevel = nTabList;
159471 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
159472 pWInfo->wctrlFlags = wctrlFlags;
159473 pWInfo->iLimit = iAuxArg;
159474 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
159476 pWInfo->pLimit = pLimit;
159478 memset(&pWInfo->nOBSat, 0,
159479 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
159480 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
159481 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
159482 pMaskSet = &pWInfo->sMaskSet;
159483 pMaskSet->n = 0;
159484 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
159486 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
159488 sWLB.pWC = &pWInfo->sWC;
159493 sWLB.pNew->cId = '*';
159499 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
159500 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
159505 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
159509 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159515 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
159518 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
159522 ** Note that bitmasks are created for all pTabList->nSrc tables in
159524 ** equal to pTabList->nSrc but might be shortened to 1 if the
159529 createMask(pMaskSet, pTabList->a[ii].iCursor);
159530 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
159531 }while( (++ii)<pTabList->nSrc );
159535 for(ii=0; ii<pTabList->nSrc; ii++){
159536 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
159545 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
159546 sqlite3WhereAddLimit(&pWInfo->sWC, pLimit);
159547 if( pParse->nErr ) goto whereBeginError;
159553 ** Do not do this if the expression contains non-deterministic functions
159554 ** that are not within a sub-select. This is not strictly required, but
159557 ** FROM ... WHERE random()>0; -- eval random() once per row
159558 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
159560 for(ii=0; ii<sWLB.pWC->nBase; ii++){
159561 WhereTerm *pT = &sWLB.pWC->a[ii];
159562 if( pT->wtFlags & TERM_VIRTUAL ) continue;
159563 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
159564 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
159565 pT->wtFlags |= TERM_CODED;
159574 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
159575 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
159577 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
159580 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
159581 pWInfo->pOrderBy = pResultSet;
159605 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
159625 while( pWInfo->pLoops ){
159626 WhereLoop *p = pWInfo->pLoops;
159627 pWInfo->pLoops = p->pNextLoop;
159637 if( db->mallocFailed ) goto whereBeginError;
159638 if( pWInfo->pOrderBy ){
159639 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
159640 if( db->mallocFailed ) goto whereBeginError;
159643 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
159644 pWInfo->revMask = ALLBITS;
159646 if( pParse->nErr ){
159649 assert( db->mallocFailed==0 );
159652 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
159653 if( pWInfo->nOBSat>0 ){
159654 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
159656 switch( pWInfo->eDistinct ){
159671 for(ii=0; ii<pWInfo->nLevel; ii++){
159672 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
159680 ** This query optimization is factored out into a separate "no-inline"
159683 ** some C-compiler optimizers from in-lining the
159685 ** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
159688 if( pWInfo->nLevel>=2
159694 nTabList = pWInfo->nLevel;
159701 if( pWInfo->nLevel>=2
159709 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
159714 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
159717 ** to use a one-pass algorithm, determine if this is appropriate.
159719 ** A one-pass approach can be used if the caller has requested one
159723 ** * the caller has indicated that a one-pass approach can be used
159732 ** use a one-pass approach, and this is not set accurately for scans
159735 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
159737 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
159739 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
159742 && !IsVirtual(pTabList->a[0].pTab)
159745 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
159746 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
159750 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
159758 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
159763 pTabItem = &pTabList->a[pLevel->iFrom];
159764 pTab = pTabItem->pTab;
159765 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
159766 pLoop = pLevel->pWLoop;
159767 if( (pTab->tabFlags & TF_Ephemeral)!=0 || IsView(pTab) ){
159771 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
159773 int iCur = pTabItem->iCursor;
159779 if( ((pLoop->wsFlags & WHERE_IDX_ONLY)==0
159781 || (pTabItem->fg.jointype & (JT_LTORJ|JT_RIGHT))!=0
159784 if( pWInfo->eOnePass!=ONEPASS_OFF ){
159786 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
159788 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
159789 assert( pTabItem->iCursor==pLevel->iTabCur );
159790 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
159791 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
159792 if( pWInfo->eOnePass==ONEPASS_OFF
159793 && pTab->nCol<BMS
159794 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
159795 && (pLoop->wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))==0
159800 Bitmask b = pTabItem->colUsed;
159803 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
159804 assert( n<=pTab->nCol );
159807 if( pLoop->u.btree.pIndex!=0 ){
159815 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
159816 (const u8*)&pTabItem->colUsed, P4_INT64);
159819 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
159821 if( pLoop->wsFlags & WHERE_INDEXED ){
159822 Index *pIx = pLoop->u.btree.pIndex;
159826 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
159830 /* This is one term of an OR-optimization using the PRIMARY KEY of a
159832 iIndexCur = pLevel->iTabCur;
159834 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
159835 Index *pJ = pTabItem->pTab->pIndex;
159840 pJ = pJ->pNext;
159843 pWInfo->aiCurOnePass[1] = iIndexCur;
159848 iIndexCur = pParse->nTab++;
159850 pLevel->iIdxCur = iIndexCur;
159852 assert( pIx->pSchema==pTab->pSchema );
159855 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159857 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
159858 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
159859 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
159860 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
159861 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
159862 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
159866 VdbeComment((v, "%s", pIx->zName));
159871 for(ii=0; ii<pIx->nColumn; ii++){
159872 jj = pIx->aiColumn[ii];
159875 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
159885 if( (pTabItem->fg.jointype & JT_RIGHT)!=0
159886 && (pLevel->pRJ = sqlite3WhereMalloc(pWInfo, sizeof(WhereRightJoin)))!=0
159888 WhereRightJoin *pRJ = pLevel->pRJ;
159889 pRJ->iMatch = pParse->nTab++;
159890 pRJ->regBloom = ++pParse->nMem;
159891 sqlite3VdbeAddOp2(v, OP_Blob, 65536, pRJ->regBloom);
159892 pRJ->regReturn = ++pParse->nMem;
159893 sqlite3VdbeAddOp2(v, OP_Null, 0, pRJ->regReturn);
159894 assert( pTab==pTabItem->pTab );
159897 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, 1);
159898 pInfo = sqlite3KeyInfoAlloc(pParse->db, 1, 0);
159900 pInfo->aColl[0] = 0;
159901 pInfo->aSortFlags[0] = 0;
159906 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRJ->iMatch, pPk->nKeyCol);
159909 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
159913 pWInfo->nOBSat = 0;
159914 pWInfo->eDistinct = WHERE_DISTINCT_UNORDERED;
159917 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
159918 if( db->mallocFailed ) goto whereBeginError;
159928 if( pParse->nErr ) goto whereBeginError;
159929 pLevel = &pWInfo->a[ii];
159930 wsFlags = pLevel->pWLoop->wsFlags;
159931 pSrc = &pTabList->a[pLevel->iFrom];
159932 if( pSrc->fg.isMaterialized ){
159933 if( pSrc->fg.isCorrelated ){
159934 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159937 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159944 constructAutomaticIndex(pParse, &pWInfo->sWC,
159945 &pTabList->a[pLevel->iFrom], notReady, pLevel);
159950 if( db->mallocFailed ) goto whereBeginError;
159955 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
159957 pWInfo->iContinue = pLevel->addrCont;
159964 VdbeModuleComment((v, "Begin WHERE-core"));
159965 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
159971 testcase( pWInfo->pExprMods!=0 );
159973 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
159986 # define OpcodeRewriteTrace(D,K,P) /* no-op */
159994 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
160006 VdbeOp *pOp = sqlite3VdbeGetOp(v,k--);
160007 if( pOp->p1!=iCur ) continue;
160008 if( pOp->opcode==OP_Close ) return 0;
160009 if( pOp->opcode==OP_OpenRead ) return 1;
160010 if( pOp->opcode==OP_OpenWrite ) return 1;
160011 if( pOp->opcode==OP_OpenDup ) return 1;
160012 if( pOp->opcode==OP_OpenAutoindex ) return 1;
160013 if( pOp->opcode==OP_OpenEphemeral ) return 1;
160024 Parse *pParse = pWInfo->pParse;
160025 Vdbe *v = pParse->pVdbe;
160029 SrcList *pTabList = pWInfo->pTabList;
160030 sqlite3 *db = pParse->db;
160036 VdbeModuleComment((v, "End WHERE-core"));
160037 for(i=pWInfo->nLevel-1; i>=0; i--){
160039 pLevel = &pWInfo->a[i];
160040 if( pLevel->pRJ ){
160043 WhereRightJoin *pRJ = pLevel->pRJ;
160044 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160045 pLevel->addrCont = 0;
160046 pRJ->endSubrtn = sqlite3VdbeCurrentAddr(v);
160047 sqlite3VdbeAddOp3(v, OP_Return, pRJ->regReturn, pRJ->addrSubrtn, 1);
160051 pLoop = pLevel->pWLoop;
160052 if( pLevel->op!=OP_Noop ){
160057 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
160058 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
160059 && (pLoop->wsFlags & WHERE_INDEXED)!=0
160060 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
160061 && (n = pLoop->u.btree.nDistinctCol)>0
160062 && pIdx->aiRowLogEst[n]>=36
160064 int r1 = pParse->nMem+1;
160067 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
160069 pParse->nMem += n+1;
160070 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
160071 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
160074 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
160078 if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160079 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
160080 sqlite3VdbeChangeP5(v, pLevel->p5);
160082 VdbeCoverageIf(v, pLevel->op==OP_Next);
160083 VdbeCoverageIf(v, pLevel->op==OP_Prev);
160084 VdbeCoverageIf(v, pLevel->op==OP_VNext);
160085 if( pLevel->regBignull ){
160086 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
160087 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
160093 }else if( pLevel->addrCont ){
160094 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
160096 if( (pLoop->wsFlags & WHERE_IN_ABLE)!=0 && pLevel->u.in.nIn>0 ){
160099 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
160100 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
160101 assert( sqlite3VdbeGetOp(v, pIn->addrInTop+1)->opcode==OP_IsNull
160102 || pParse->db->mallocFailed );
160103 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160104 if( pIn->eEndLoopOp!=OP_Noop ){
160105 if( pIn->nPrefix ){
160107 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
160108 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
160109 if( pLevel->iLeftJoin ){
160110 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
160115 ** return the null-row. So, if the cursor is not open yet,
160118 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
160123 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
160125 pIn->iBase, pIn->nPrefix);
160131 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
160134 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
160136 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
160137 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
160139 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
160142 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
160143 if( pLevel->pRJ ){
160144 sqlite3VdbeAddOp3(v, OP_Return, pLevel->pRJ->regReturn, 0, 1);
160147 if( pLevel->addrSkip ){
160148 sqlite3VdbeGoto(v, pLevel->addrSkip);
160149 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
160150 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
160151 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
160154 if( pLevel->addrLikeRep ){
160155 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
160156 pLevel->addrLikeRep);
160160 if( pLevel->iLeftJoin ){
160161 int ws = pLoop->wsFlags;
160162 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
160165 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
160166 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
160169 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCoveringIdx)
160172 Index *pIx = pLevel->u.pCoveringIdx;
160173 int iDb = sqlite3SchemaToIndex(db, pIx->pSchema);
160174 sqlite3VdbeAddOp3(v, OP_ReopenIdx, pLevel->iIdxCur, pIx->tnum, iDb);
160177 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
160179 if( pLevel->op==OP_Return ){
160180 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
160182 sqlite3VdbeGoto(v, pLevel->addrFirst);
160186 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
160187 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
160190 assert( pWInfo->nLevel<=pTabList->nSrc );
160191 if( pWInfo->pExprMods ) whereUndoExprMods(pWInfo);
160192 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
160196 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
160197 Table *pTab = pTabItem->pTab;
160199 pLoop = pLevel->pWLoop;
160205 if( pLevel->pRJ ){
160210 /* For a co-routine, change all OP_Column references to the table of
160211 ** the co-routine into OP_Copy of result contained in a register.
160214 if( pTabItem->fg.viaCoroutine ){
160215 testcase( pParse->db->mallocFailed );
160216 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
160217 pTabItem->regResult, 0);
160232 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
160233 pIdx = pLoop->u.btree.pIndex;
160234 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
160235 pIdx = pLevel->u.pCoveringIdx;
160238 && !db->mallocFailed
160240 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
160243 last = pWInfo->iEndWhere;
160245 k = pLevel->addrBody + 1;
160247 if( db->flags & SQLITE_VdbeAddopTrace ){
160248 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
160251 pOp = sqlite3VdbeGetOp(v, k - 1);
160252 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
160253 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
160254 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
160257 pLastOp = pOp + (last - k);
160260 if( pOp->p1!=pLevel->iTabCur ){
160261 /* no-op */
160262 }else if( pOp->opcode==OP_Column
160264 || pOp->opcode==OP_Offset
160267 int x = pOp->p2;
160268 assert( pIdx->pTable==pTab );
160270 if( pOp->opcode==OP_Offset ){
160276 x = pPk->aiColumn[x];
160284 pOp->p2 = x;
160285 pOp->p1 = pLevel->iIdxCur;
160289 ** reference. Verify that this is harmless - that the
160293 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
160294 || cursorIsOpen(v,pOp->p1,k)
160295 || pOp->opcode==OP_Offset
160298 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
160299 || cursorIsOpen(v,pOp->p1,k)
160303 }else if( pOp->opcode==OP_Rowid ){
160304 pOp->p1 = pLevel->iIdxCur;
160305 pOp->opcode = OP_IdxRowid;
160307 }else if( pOp->opcode==OP_IfNullRow ){
160308 pOp->p1 = pLevel->iIdxCur;
160316 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
160324 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
160328 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
160330 pParse->withinRJSubrtn -= nRJ;
160379 ** the sub-query.
160384 ** select-list and ORDER BY expressions in the parent query are
160385 ** selected by the sub-query. For the purposes of the transformation,
160417 ** sub-query, which is always implemented as a co-routine. It then calls
160422 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
160423 ** When the sub-routine is invoked:
160425 ** * The results of all window-functions for the row are stored
160437 ** BUILT-IN WINDOW FUNCTIONS
160439 ** This implementation features the following built-in window functions:
160453 ** These are the same built-in window functions supported by Postgres.
160456 ** be implemented using an API, built-in window functions are much more
160459 ** As such, some built-in window functions use the same API as aggregate
160470 ** As well as some of the built-in window functions, aggregate window
160477 ** Implementation of built-in window function row_number(). Assumes that the
160508 ** Implementation of built-in window function dense_rank(). Assumes that
160520 if( p ) p->nStep = 1;
160528 if( p->nStep ){
160529 p->nValue++;
160530 p->nStep = 0;
160532 sqlite3_result_int64(pCtx, p->nValue);
160537 ** Implementation of built-in window function nth_value(). This
160538 ** implementation is used in "slow mode" only - when the EXCLUDE clause
160569 p->nStep++;
160570 if( iVal==p->nStep ){
160571 p->pValue = sqlite3_value_dup(apArg[0]);
160572 if( !p->pValue ){
160583 pCtx, "second argument to nth_value must be a positive integer", -1
160589 if( p && p->pValue ){
160590 sqlite3_result_value(pCtx, p->pValue);
160591 sqlite3_value_free(p->pValue);
160592 p->pValue = 0;
160605 if( p && p->pValue==0 ){
160606 p->pValue = sqlite3_value_dup(apArg[0]);
160607 if( !p->pValue ){
160617 if( p && p->pValue ){
160618 sqlite3_result_value(pCtx, p->pValue);
160619 sqlite3_value_free(p->pValue);
160620 p->pValue = 0;
160627 ** Implementation of built-in window function rank(). Assumes that
160640 p->nStep++;
160641 if( p->nValue==0 ){
160642 p->nValue = p->nStep;
160652 sqlite3_result_int64(pCtx, p->nValue);
160653 p->nValue = 0;
160658 ** Implementation of built-in window function percent_rank(). Assumes that
160673 p->nTotal++;
160685 p->nStep++;
160691 p->nValue = p->nStep;
160692 if( p->nTotal>1 ){
160693 double r = (double)p->nValue / (double)(p->nTotal-1);
160703 ** Implementation of built-in window function cume_dist(). Assumes that
160718 p->nTotal++;
160730 p->nStep++;
160736 double r = (double)(p->nStep) / (double)(p->nTotal);
160766 if( p->nTotal==0 ){
160767 p->nParam = sqlite3_value_int64(apArg[0]);
160768 if( p->nParam<=0 ){
160770 pCtx, "argument of ntile must be a positive integer", -1
160774 p->nTotal++;
160786 p->iRow++;
160791 if( p && p->nParam>0 ){
160792 int nSize = (p->nTotal / p->nParam);
160794 sqlite3_result_int64(pCtx, p->iRow+1);
160796 i64 nLarge = p->nTotal - p->nParam*nSize;
160798 i64 iRow = p->iRow;
160800 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
160805 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
160832 sqlite3_value_free(p->pVal);
160833 p->pVal = sqlite3_value_dup(apArg[0]);
160834 if( p->pVal==0 ){
160837 p->nVal++;
160851 p->nVal--;
160852 if( p->nVal==0 ){
160853 sqlite3_value_free(p->pVal);
160854 p->pVal = 0;
160861 if( p && p->pVal ){
160862 sqlite3_result_value(pCtx, p->pVal);
160868 if( p && p->pVal ){
160869 sqlite3_result_value(pCtx, p->pVal);
160870 sqlite3_value_free(p->pVal);
160871 p->pVal = 0;
160876 ** Static names for the built-in window function names. These static
160881 ** if( pFuncDef->zName==row_valueName ){ ... }
160896 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
160897 ** for built-in window functions that never call those interfaces.
160914 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
160925 ** no-op routines for their methods */
160943 ** Register those built-in window functions that are not also aggregates.
160968 for(p=pList; p; p=p->pNextWin){
160969 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
160990 ** * If the function is a built-in window function that requires the
160991 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
161000 if( pWin->zName && pWin->eFrmType==0 ){
161001 Window *p = windowFind(pParse, pList, pWin->zName);
161003 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
161004 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
161005 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
161006 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
161007 pWin->eStart = p->eStart;
161008 pWin->eEnd = p->eEnd;
161009 pWin->eFrmType = p->eFrmType;
161010 pWin->eExclude = p->eExclude;
161014 if( (pWin->eFrmType==TK_RANGE)
161015 && (pWin->pStart || pWin->pEnd)
161016 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
161022 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
161023 sqlite3 *db = pParse->db;
161024 if( pWin->pFilter ){
161046 if( pFunc->zName==aUp[i].zFunc ){
161047 sqlite3ExprDelete(db, pWin->pStart);
161048 sqlite3ExprDelete(db, pWin->pEnd);
161049 pWin->pEnd = pWin->pStart = 0;
161050 pWin->eFrmType = aUp[i].eFrmType;
161051 pWin->eStart = aUp[i].eStart;
161052 pWin->eEnd = aUp[i].eEnd;
161053 pWin->eExclude = 0;
161054 if( pWin->eStart==TK_FOLLOWING ){
161055 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
161062 pWin->pWFunc = pFunc;
161075 Select *pSubSelect; /* Current sub-select, if any */
161080 ** this function appends to the output expression-list and updates
161084 struct WindowRewrite *p = pWalker->u.pRewrite;
161085 Parse *pParse = pWalker->pParse;
161087 assert( p->pWin!=0 );
161089 /* If this function is being called from within a scalar sub-select
161093 ** to the scalar sub-select. */
161094 if( p->pSubSelect ){
161095 if( pExpr->op!=TK_COLUMN ){
161098 int nSrc = p->pSrc->nSrc;
161101 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
161107 switch( pExpr->op ){
161114 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
161115 if( pExpr->y.pWin==pWin ){
161116 assert( pWin->pOwner==pExpr );
161125 int iCol = -1;
161126 if( pParse->db->mallocFailed ) return WRC_Abort;
161127 if( p->pSub ){
161129 for(i=0; i<p->pSub->nExpr; i++){
161130 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
161137 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
161138 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
161139 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
161141 if( p->pSub ){
161142 int f = pExpr->flags & EP_Collate;
161145 sqlite3ExprDelete(pParse->db, pExpr);
161149 pExpr->op = TK_COLUMN;
161150 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
161151 pExpr->iTable = p->pWin->iEphCsr;
161152 pExpr->y.pTab = p->pTab;
161153 pExpr->flags = f;
161155 if( pParse->db->mallocFailed ) return WRC_Abort;
161159 default: /* no-op */
161166 struct WindowRewrite *p = pWalker->u.pRewrite;
161167 Select *pSave = p->pSubSelect;
161171 p->pSubSelect = pSelect;
161173 p->pSubSelect = pSave;
161180 ** Iterate through each expression in expression-list pEList. For each:
161187 ** Append the node to output expression-list (*ppSub). And replace it
161188 ** with a TK_COLUMN that reads the (N-1)th element of table
161189 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
161198 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
161223 ** Append a copy of each expression in expression-list pAppend to
161234 int nInit = pList ? pList->nExpr : 0;
161235 for(i=0; i<pAppend->nExpr; i++){
161236 sqlite3 *db = pParse->db;
161237 Expr *pDup = sqlite3ExprDup(db, pAppend->a[i].pExpr, 0);
161239 if( db->mallocFailed ){
161248 pSub->op = TK_NULL;
161249 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
161250 pSub->u.zToken = 0;
161254 if( pList ) pList->a[nInit+i].fg.sortFlags = pAppend->a[i].fg.sortFlags;
161263 ** then we have to increase the Expr->op2 values of those nodes
161269 if( pExpr->op==TK_AGG_FUNCTION
161270 && pExpr->op2>=pWalker->walkerDepth
161272 pExpr->op2++;
161278 if( pExpr->op==TK_AGG_FUNCTION && pExpr->pAggInfo==0 ){
161280 sqlite3ErrorMsg(pWalker->pParse,
161281 "misuse of aggregate: %s()", pExpr->u.zToken);
161288 ** any SQL window functions, this function is a no-op. Otherwise, it
161295 if( p->pWin
161296 && p->pPrior==0
161297 && ALWAYS((p->selFlags & SF_WinRewrite)==0)
161301 sqlite3 *db = pParse->db;
161303 SrcList *pSrc = p->pSrc;
161304 Expr *pWhere = p->pWhere;
161305 ExprList *pGroupBy = p->pGroupBy;
161306 Expr *pHaving = p->pHaving;
161309 ExprList *pSublist = 0; /* Expression list for sub-query */
161310 Window *pMWin = p->pWin; /* Main window object */
161315 u32 selFlags = p->selFlags;
161323 if( (p->selFlags & SF_Aggregate)==0 ){
161326 sqlite3WalkExprList(&w, p->pOrderBy);
161329 p->pSrc = 0;
161330 p->pWhere = 0;
161331 p->pGroupBy = 0;
161332 p->pHaving = 0;
161333 p->selFlags &= ~SF_Aggregate;
161334 p->selFlags |= SF_WinRewrite;
161336 /* Create the ORDER BY clause for the sub-select. This is the concatenation
161339 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
161340 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
161341 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
161342 int nSave = pSort->nExpr;
161343 pSort->nExpr = p->pOrderBy->nExpr;
161344 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
161345 sqlite3ExprListDelete(db, p->pOrderBy);
161346 p->pOrderBy = 0;
161348 pSort->nExpr = nSave;
161354 pMWin->iEphCsr = pParse->nTab++;
161355 pParse->nTab += 3;
161357 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
161358 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
161359 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
161362 ** sub-select expression list. They are required to figure out where
161364 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
161365 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
161368 ** sub-select expression list. Also allocate two registers for each
161369 ** window function - one for the accumulator, another for interim
161371 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161373 assert( ExprUseXList(pWin->pOwner) );
161374 assert( pWin->pWFunc!=0 );
161375 pArgs = pWin->pOwner->x.pList;
161376 if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
161378 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161379 pWin->bExprArgs = 1;
161381 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
161384 if( pWin->pFilter ){
161385 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
161388 pWin->regAccum = ++pParse->nMem;
161389 pWin->regResult = ++pParse->nMem;
161390 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
161409 ("New window-function subquery in FROM clause of (%u/%p)\n",
161410 p->selId, p));
161411 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
161412 assert( pSub!=0 || p->pSrc==0 ); /* Due to db->mallocFailed test inside
161415 if( p->pSrc ){
161417 p->pSrc->a[0].pSelect = pSub;
161418 sqlite3SrcListAssignCursors(pParse, p->pSrc);
161419 pSub->selFlags |= SF_Expanded|SF_OrderByReqd;
161421 pSub->selFlags |= (selFlags & SF_Aggregate);
161424 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
161425 ** the correct error message regardless. */
161429 pTab->tabFlags |= TF_Ephemeral;
161430 p->pSrc->a[0].pTab = pTab;
161441 if( db->mallocFailed ) rc = SQLITE_NOMEM;
161445 ** result-set or ORDER BY clause of the SELECT statement p. */
161449 assert( rc==SQLITE_OK || pParse->nErr!=0 );
161458 if( p->ppThis ){
161459 *p->ppThis = p->pNextWin;
161460 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
161461 p->ppThis = 0;
161471 sqlite3ExprDelete(db, p->pFilter);
161472 sqlite3ExprListDelete(db, p->pPartition);
161473 sqlite3ExprListDelete(db, p->pOrderBy);
161474 sqlite3ExprDelete(db, p->pEnd);
161475 sqlite3ExprDelete(db, p->pStart);
161476 sqlite3DbFree(db, p->zName);
161477 sqlite3DbFree(db, p->zBase);
161487 Window *pNext = p->pNextWin;
161495 ** value should be a non-negative integer. If the value is not a
161496 ** constant, change it to NULL. The fact that it is then a non-negative
161503 sqlite3ExprDelete(pParse->db, pExpr);
161504 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
161559 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
161561 pWin->eFrmType = eType;
161562 pWin->eStart = eStart;
161563 pWin->eEnd = eEnd;
161564 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
161567 pWin->eExclude = eExclude;
161568 pWin->bImplicitFrame = bImplicitFrame;
161569 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
161570 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
161574 sqlite3ExprDelete(pParse->db, pEnd);
161575 sqlite3ExprDelete(pParse->db, pStart);
161581 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
161582 ** equivalent nul-terminated string.
161592 pWin->pPartition = pPartition;
161593 pWin->pOrderBy = pOrderBy;
161595 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
161598 sqlite3ExprListDelete(pParse->db, pPartition);
161599 sqlite3ExprListDelete(pParse->db, pOrderBy);
161607 ** stored in the linked list starting at pWin->pNextWin. This function
161612 if( pWin->zBase ){
161613 sqlite3 *db = pParse->db;
161614 Window *pExist = windowFind(pParse, pList, pWin->zBase);
161618 if( pWin->pPartition ){
161620 }else if( pExist->pOrderBy && pWin->pOrderBy ){
161622 }else if( pExist->bImplicitFrame==0 ){
161627 "cannot override %s of window: %s", zErr, pWin->zBase
161630 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
161631 if( pExist->pOrderBy ){
161632 assert( pWin->pOrderBy==0 );
161633 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
161635 sqlite3DbFree(db, pWin->zBase);
161636 pWin->zBase = 0;
161647 assert( p->op==TK_FUNCTION );
161649 p->y.pWin = pWin;
161651 pWin->pOwner = p;
161652 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
161658 sqlite3WindowDelete(pParse->db, pWin);
161663 ** Possibly link window pWin into the list at pSel->pWin (window functions
161670 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
161671 pWin->pNextWin = pSel->pWin;
161672 if( pSel->pWin ){
161673 pSel->pWin->ppThis = &pWin->pNextWin;
161675 pSel->pWin = pWin;
161676 pWin->ppThis = &pSel->pWin;
161678 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
161679 pSel->selFlags |= SF_MultiPart;
161698 if( p1->eFrmType!=p2->eFrmType ) return 1;
161699 if( p1->eStart!=p2->eStart ) return 1;
161700 if( p1->eEnd!=p2->eEnd ) return 1;
161701 if( p1->eExclude!=p2->eExclude ) return 1;
161702 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
161703 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
161704 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
161707 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
161711 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
161721 ** to begin iterating through the sub-query results. It is used to allocate
161725 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
161726 Window *pMWin = pSelect->pWin;
161730 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
161731 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
161732 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
161733 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
161737 if( pMWin->pPartition ){
161738 int nExpr = pMWin->pPartition->nExpr;
161739 pMWin->regPart = pParse->nMem+1;
161740 pParse->nMem += nExpr;
161741 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
161744 pMWin->regOne = ++pParse->nMem;
161745 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
161747 if( pMWin->eExclude ){
161748 pMWin->regStartRowid = ++pParse->nMem;
161749 pMWin->regEndRowid = ++pParse->nMem;
161750 pMWin->csrApp = pParse->nTab++;
161751 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
161752 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
161753 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
161757 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161758 FuncDef *p = pWin->pWFunc;
161759 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
161769 assert( ExprUseXList(pWin->pOwner) );
161770 pList = pWin->pOwner->x.pList;
161772 pWin->csrApp = pParse->nTab++;
161773 pWin->regApp = pParse->nMem+1;
161774 pParse->nMem += 3;
161775 if( pKeyInfo && pWin->pWFunc->zName[1]=='i' ){
161776 assert( pKeyInfo->aSortFlags[0]==0 );
161777 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
161779 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
161781 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
161783 else if( p->zName==nth_valueName || p->zName==first_valueName ){
161784 /* Allocate two registers at pWin->regApp. These will be used to
161786 pWin->regApp = pParse->nMem+1;
161787 pWin->csrApp = pParse->nTab++;
161788 pParse->nMem += 2;
161789 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
161791 else if( p->zName==leadName || p->zName==lagName ){
161792 pWin->csrApp = pParse->nTab++;
161793 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
161808 ** code to check that the value is a non-negative integer and throws an
161813 "frame starting offset must be a non-negative integer",
161814 "frame ending offset must be a non-negative integer",
161816 "frame starting offset must be a non-negative number",
161817 "frame ending offset must be a non-negative number",
161855 ** Return the number of arguments passed to the window-function associated
161860 assert( ExprUseXList(pWin->pOwner) );
161861 pList = pWin->pOwner->x.pList;
161862 return (pList ? pList->nExpr : 0);
161885 ** in an array of accumulator registers - one for each window function
161892 ** order to reduce memory requirements - it would always be safe just
161907 ** Consider a window-frame similar to the following:
161927 ** Depending on the window-frame in question, all three cursors may not
161955 Window *pMWin = p->pMWin;
161956 ExprList *pOrderBy = pMWin->pOrderBy;
161958 Vdbe *v = sqlite3GetVdbe(p->pParse);
161959 ExprList *pPart = pMWin->pPartition;
161960 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
161962 for(i=0; i<pOrderBy->nExpr; i++){
161970 ** xInverse (if bInverse is non-zero) for each window function in the
161971 ** linked list starting at pMWin. Or, for built-in window functions
161982 ** already populated with all columns from the current row of the sub-query.
161984 ** If argument regPartSize is non-zero, then it is a register containing the
161994 Parse *pParse = p->pParse;
161997 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
161998 FuncDef *pFunc = pWin->pWFunc;
162000 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
162003 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
162010 if( i!=1 || pFunc->zName!=nth_valueName ){
162011 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
162013 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
162018 if( pMWin->regStartRowid==0
162019 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
162020 && (pWin->eStart!=TK_UNBOUNDED)
162025 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
162026 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
162027 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
162028 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
162030 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
162032 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
162033 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162036 }else if( pWin->regApp ){
162037 assert( pFunc->zName==nth_valueName
162038 || pFunc->zName==first_valueName
162041 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
162042 }else if( pFunc->xSFunc!=noopStepFunc ){
162044 if( pWin->pFilter ){
162046 assert( ExprUseXList(pWin->pOwner) );
162047 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
162048 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
162050 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
162056 if( pWin->bExprArgs ){
162060 assert( ExprUseXList(pWin->pOwner) );
162061 nArg = pWin->pOwner->x.pList->nExpr;
162063 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
162067 if( pOp->opcode==OP_Column && pOp->p1==pMWin->iEphCsr ){
162068 pOp->p1 = csr;
162072 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
162075 assert( ExprUseXList(pWin->pOwner) );
162076 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
162080 bInverse, regArg, pWin->regAccum);
162083 if( pWin->bExprArgs ){
162101 ** pMWin. Or, for built-in window-functions that do not use the standard
162105 Parse *pParse = p->pParse;
162106 Window *pMWin = p->pMWin;
162110 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162111 if( pMWin->regStartRowid==0
162112 && (pWin->pWFunc->funcFlags & SQLITE_FUNC_MINMAX)
162113 && (pWin->eStart!=TK_UNBOUNDED)
162115 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162116 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
162118 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
162119 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
162120 }else if( pWin->regApp ){
162121 assert( pMWin->regStartRowid==0 );
162125 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
162126 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
162127 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
162128 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162130 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
162131 sqlite3VdbeAppendP4(v, pWin->pWFunc, P4_FUNCDEF);
162139 ** p->pMWin list by doing a full scan of the current window frame. Store the
162145 Parse *pParse = p->pParse;
162146 Window *pMWin = p->pMWin;
162147 Vdbe *v = p->pVdbe;
162163 csr = pMWin->csrApp;
162164 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162176 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
162177 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
162179 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162180 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162183 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
162187 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
162190 if( pMWin->eExclude==TK_CURRENT ){
162193 }else if( pMWin->eExclude!=TK_NO ){
162198 if( pMWin->pOrderBy ){
162199 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
162201 if( pMWin->eExclude==TK_TIES ){
162218 windowAggStep(p, pMWin, csr, 0, p->regArg);
162223 sqlite3VdbeJumpHere(v, addrNext-1);
162237 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
162241 ** for per-row processing is only generated for the following built-in window
162250 Window *pMWin = p->pMWin;
162251 Vdbe *v = p->pVdbe;
162253 if( pMWin->regStartRowid ){
162256 Parse *pParse = p->pParse;
162259 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162260 FuncDef *pFunc = pWin->pWFunc;
162261 assert( ExprUseXList(pWin->pOwner) );
162262 if( pFunc->zName==nth_valueName
162263 || pFunc->zName==first_valueName
162265 int csr = pWin->csrApp;
162268 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162270 if( pFunc->zName==nth_valueName ){
162271 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
162276 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
162277 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
162281 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
162285 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
162286 int nArg = pWin->pOwner->x.pList->nExpr;
162287 int csr = pWin->csrApp;
162290 int iEph = pMWin->iEphCsr;
162293 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
162295 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
162299 int val = (pFunc->zName==leadName ? 1 : -1);
162302 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
162304 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
162311 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
162317 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
162323 ** any equivalent initialization required by any built-in window functions
162331 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162332 FuncDef *pFunc = pWin->pWFunc;
162333 assert( pWin->regAccum );
162334 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
162336 if( pMWin->regStartRowid==0 ){
162337 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
162338 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
162339 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
162342 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
162343 assert( pWin->eStart!=TK_UNBOUNDED );
162344 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
162345 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
162349 regArg = pParse->nMem+1;
162350 pParse->nMem += nArg;
162360 if( pMWin->regStartRowid ) return 1;
162361 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
162362 FuncDef *pFunc = pWin->pWFunc;
162363 if( (pFunc->zName==nth_valueName)
162364 || (pFunc->zName==first_valueName)
162365 || (pFunc->zName==leadName)
162366 || (pFunc->zName==lagName)
162376 ** pOrderBy->nExpr. This function generates code to compare the two
162393 int nVal = pOrderBy->nExpr;
162401 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
162416 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
162418 ** If the sort-order for the ORDER BY term in the window is DESC, then the
162420 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
162424 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
162434 int regVal, /* Register containing non-negative number */
162438 Parse *pParse = p->pParse;
162440 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
162443 int regString = ++pParse->nMem; /* Reg. for constant value '' */
162449 /* Read the peer-value from each cursor into a register */
162454 assert( pOrderBy && pOrderBy->nExpr==1 );
162455 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_DESC ){
162465 reg1, (arith==OP_Add ? "+" : "-"), regVal,
162488 if( pOrderBy->a[0].fg.sortFlags & KEYINFO_ORDER_BIGNULL ){
162503 default: assert( op==OP_Lt ); /* no-op */ break;
162511 sqlite3VdbeChangeP2(v, -1, addrDone);
162515 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
162518 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
162521 ** reg1 = reg1 +/- regVal
162524 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
162541 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
162570 Parse *pParse = p->pParse;
162571 Window *pMWin = p->pMWin;
162573 Vdbe *v = p->pVdbe;
162575 int bPeer = (pMWin->eFrmType!=TK_ROWS);
162580 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
162582 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
162588 if( pMWin->eFrmType==TK_RANGE ){
162592 if( pMWin->eStart==TK_FOLLOWING ){
162594 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
162598 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
162603 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
162612 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
162623 if( pMWin->eStart==pMWin->eEnd && regCountdown
162624 && pMWin->eFrmType==TK_RANGE
162629 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
162630 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
162633 }else if( p->regRowid ){
162634 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid1);
162635 sqlite3VdbeAddOp3(v, OP_Ge, p->regRowid, lblDone, regRowid1);
162640 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
162645 csr = p->current.csr;
162646 reg = p->current.reg;
162651 csr = p->start.csr;
162652 reg = p->start.reg;
162653 if( pMWin->regStartRowid ){
162654 assert( pMWin->regEndRowid );
162655 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
162657 windowAggStep(p, pMWin, csr, 1, p->regArg);
162663 csr = p->end.csr;
162664 reg = p->end.reg;
162665 if( pMWin->regStartRowid ){
162666 assert( pMWin->regEndRowid );
162667 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
162669 windowAggStep(p, pMWin, csr, 0, p->regArg);
162674 if( op==p->eDelete ){
162692 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
162695 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
162717 pNew->zName = sqlite3DbStrDup(db, p->zName);
162718 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
162719 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
162720 pNew->pWFunc = p->pWFunc;
162721 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
162722 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
162723 pNew->eFrmType = p->eFrmType;
162724 pNew->eEnd = p->eEnd;
162725 pNew->eStart = p->eStart;
162726 pNew->eExclude = p->eExclude;
162727 pNew->regResult = p->regResult;
162728 pNew->regAccum = p->regAccum;
162729 pNew->iArgCol = p->iArgCol;
162730 pNew->iEphCsr = p->iEphCsr;
162731 pNew->bExprArgs = p->bExprArgs;
162732 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
162733 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
162734 pNew->pOwner = pOwner;
162735 pNew->bImplicitFrame = p->bImplicitFrame;
162750 for(pWin=p; pWin; pWin=pWin->pNextWin){
162753 pp = &((*pp)->pNextWin);
162769 sqlite3 *db = pParse->db;
162771 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
162783 ** and invoke the sub-routine at instruction addrGosub once for each row.
162813 ** if( (regEnd--)<=0 ){
162815 ** if( (regStart--)<=0 ){
162826 ** if( (regStart--)<=0 ){
162832 ** The pseudo-code above uses the following shorthand:
162847 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
162866 ** if( (regEnd--)<=0 ){
162870 ** if( (regStart--)<=0 ){
162876 ** if( (regEnd--)<=0 ){
162892 ** regStart = regEnd - <expr1>
162895 ** if( (regEnd--)<=0 ){
162898 ** if( (regStart--)<=0 ){
162906 ** if( (regEnd--)<=0 ){
162910 ** if( (regStart--)<=0 ){
162922 ** This is optimized of course - branches that will never be taken and
162943 ** if( (regStart--)<=0 ){
162959 ** To handle this case, the pseudo-code programs depicted above are modified
162979 ** of the outer loop - the one started by sqlite3WhereBegin().
163121 Window *pMWin = p->pWin;
163122 ExprList *pOrderBy = pMWin->pOrderBy;
163125 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
163126 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
163137 WindowCodeArg s; /* Context object for sub-routines */
163142 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
163143 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
163145 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
163146 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
163148 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
163149 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
163150 || pMWin->eExclude==TK_NO
163162 s.current.csr = pMWin->iEphCsr;
163168 ** are four options - they may never be deleted (eDelete==0), they may
163173 switch( pMWin->eStart ){
163175 if( pMWin->eFrmType!=TK_RANGE
163176 && windowExprGtZero(pParse, pMWin->pStart)
163183 if( pMWin->eEnd==TK_PRECEDING ){
163184 if( pMWin->eFrmType!=TK_RANGE
163185 && windowExprGtZero(pParse, pMWin->pEnd)
163199 /* Allocate registers for the array of values from the sub-query, the
163202 regNew = pParse->nMem+1;
163203 pParse->nMem += nInput;
163204 regRecord = ++pParse->nMem;
163205 s.regRowid = ++pParse->nMem;
163210 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
163211 regStart = ++pParse->nMem;
163213 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
163214 regEnd = ++pParse->nMem;
163220 if( pMWin->eFrmType!=TK_ROWS ){
163221 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
163222 regNewPeer = regNew + pMWin->nBufferCol;
163223 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
163224 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
163225 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
163226 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
163227 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
163230 /* Load the column values for the row returned by the sub-select
163243 if( pMWin->pPartition ){
163245 ExprList *pPart = pMWin->pPartition;
163246 int nPart = pPart->nExpr;
163247 int regNewPart = regNew + pMWin->nBufferCol;
163250 regFlushPart = ++pParse->nMem;
163251 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
163257 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
163263 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, s.regRowid);
163270 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
163271 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
163274 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
163275 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
163278 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
163279 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
163291 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
163292 assert( pMWin->eEnd==TK_FOLLOWING );
163296 if( pMWin->eStart!=TK_UNBOUNDED ){
163305 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
163306 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
163307 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
163308 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
163319 if( pMWin->eStart==TK_FOLLOWING ){
163321 if( pMWin->eEnd!=TK_UNBOUNDED ){
163322 if( pMWin->eFrmType==TK_RANGE ){
163336 if( pMWin->eEnd==TK_PRECEDING ){
163337 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
163345 if( pMWin->eEnd!=TK_UNBOUNDED ){
163346 if( pMWin->eFrmType==TK_RANGE ){
163376 if( pMWin->pPartition ){
163384 if( pMWin->eEnd==TK_PRECEDING ){
163385 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
163389 }else if( pMWin->eStart==TK_FOLLOWING ){
163395 if( pMWin->eFrmType==TK_RANGE ){
163400 if( pMWin->eEnd==TK_UNBOUNDED ){
163405 assert( pMWin->eEnd==TK_FOLLOWING );
163430 if( pMWin->pPartition ){
163431 if( pMWin->regStartRowid ){
163432 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
163433 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
163447 ** 2001-09-15
163461 ** That input file is processed by Lemon to generate a C-language
163463 ** this comment as part of the translated C-code. Edits should be made
163470 ** Disable all error recovery processing in the parser push-down
163523 sqlite3 *db = pParse->db;
163524 pParse->disableLookaside++;
163531 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
163544 sqlite3ExprListDelete(pParse->db, pOrderBy);
163545 sqlite3ExprDelete(pParse->db, pLimit);
163551 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
163557 if( p->pPrior ){
163561 pLoop->pNext = pNext;
163562 pLoop->selFlags |= SF_Compound;
163564 pLoop = pLoop->pPrior;
163567 if( pLoop->pOrderBy || pLoop->pLimit ){
163569 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
163570 sqlite3SelectOpName(pNext->op));
163574 if( (p->selFlags & SF_MultiValue)==0 &&
163575 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
163588 pSelect->pWith = pWith;
163591 sqlite3WithDelete(pParse->db, pWith);
163599 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
163602 p->op = (u8)op;
163603 p->affExpr = 0;
163604 p->flags = EP_Leaf;
163606 /* p->iAgg = -1; // Not required */
163607 p->pLeft = p->pRight = 0;
163608 p->pAggInfo = 0;
163609 memset(&p->x, 0, sizeof(p->x));
163610 memset(&p->y, 0, sizeof(p->y));
163611 p->op2 = 0;
163612 p->iTable = 0;
163613 p->iColumn = 0;
163614 p->u.zToken = (char*)&p[1];
163615 memcpy(p->u.zToken, t.z, t.n);
163616 p->u.zToken[t.n] = 0;
163617 p->w.iOfst = (int)(t.z - pParse->zTail);
163618 if( sqlite3Isquote(p->u.zToken[0]) ){
163622 p->nHeight = 1;
163635 sqlite3 *db = pParse->db;
163636 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
163637 pA->op = (u8)op;
163638 sqlite3ExprDelete(db, pA->pRight);
163639 pA->pRight = 0;
163657 && pParse->db->init.busy==0
163660 pIdToken->n, pIdToken->z);
163863 ** that represent terminal and non-terminal symbols.
163869 ** (also known as: "terminal symbols") have fall-back
163873 ** YYACTIONTYPE is the data type used for "action codes" - numbers
163878 ** value associated with a terminal or non-terminal
163881 ** Each non-terminal can have a different minor type.
163903 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
163904 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
163907 ** YY_NO_ACTION The yy_action[] code for no-op
163953 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
163954 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
163971 /* Define the yytestcase() macro to be a no-op if is not already defined
163996 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
164005 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
164018 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
164030 ** shifting non-terminals after a reduce.
164542 #define YY_REDUCE_MIN (-271)
164545 /* 0 */ -125, 733, 789, 241, 293, -123, -193, -191, -183, -187,
164546 /* 10 */ 166, 238, 133, -207, -199, -267, -176, -6, 204, 489,
164547 /* 20 */ 576, -175, 598, 686, 615, 725, 860, 778, 781, 857,
164548 /* 30 */ 616, 887, 87, 240, -192, 408, 626, 796, 843, 854,
164549 /* 40 */ 1003, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164550 /* 50 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164551 /* 60 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164552 /* 70 */ -271, -271, -271, -271, -271, -271, -271, -271, 80, 83,
164558 /* 130 */ -271, -271, -271, -271, -271, -271, -271, -271, -271, -271,
164559 /* 140 */ -271, 138, 459, 396, -158, 470, 302, -212, 521, 201,
164560 /* 150 */ -195, -92, 559, 630, 632, 630, -271, 632, 901, 63,
164561 /* 160 */ 407, -271, -271, -271, -271, 161, 161, 161, 251, 335,
164562 /* 170 */ 847, 960, 980, 537, 588, 618, 628, 688, 688, -166,
164563 /* 180 */ -161, 674, 790, 794, 799, 851, 852, -122, 680, -120,
164566 /* 210 */ 362, 994, 1139, 1005, 1037, 1202, 1205, 1195, 1210, -194,
164567 /* 220 */ 56, 185, -135, 232, 522, 560, 601, 617, 669, 683,
164647 /********** End of lemon-generated parsing tables *****************************/
164870 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
164873 YYMINORTYPE minor; /* The user-supplied minor token value. This
164883 int yyhwm; /* High-water mark of the stack */
164911 ** and a prompt to preface each trace message. Tracing is turned off
165683 newSize = p->yystksz*2 + 100;
165684 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
165685 if( p->yystack==&p->yystk0 ){
165687 if( pNew ) pNew[0] = p->yystk0;
165689 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
165692 p->yystack = pNew;
165693 p->yytos = &p->yystack[idx];
165697 yyTracePrompt, p->yystksz, newSize);
165700 p->yystksz = newSize;
165721 yypParser->yyhwm = 0;
165724 yypParser->yytos = NULL;
165725 yypParser->yystack = NULL;
165726 yypParser->yystksz = 0;
165728 yypParser->yystack = &yypParser->yystk0;
165729 yypParser->yystksz = 1;
165733 yypParser->yyerrcnt = -1;
165735 yypParser->yytos = yypParser->yystack;
165736 yypParser->yystack[0].stateno = 0;
165737 yypParser->yystack[0].major = 0;
165739 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
165784 ** terminal or non-terminal is destroyed. This can happen
165799 sqlite3SelectDelete(pParse->db, (yypminor->yy47));
165814 sqlite3ExprDelete(pParse->db, (yypminor->yy528));
165831 sqlite3ExprListDelete(pParse->db, (yypminor->yy322));
165840 sqlite3SrcListDelete(pParse->db, (yypminor->yy131));
165845 sqlite3WithDelete(pParse->db, (yypminor->yy521));
165851 sqlite3WindowListDelete(pParse->db, (yypminor->yy41));
165857 sqlite3IdListDelete(pParse->db, (yypminor->yy254));
165866 sqlite3WindowDelete(pParse->db, (yypminor->yy41));
165872 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy33));
165877 sqlite3IdListDelete(pParse->db, (yypminor->yy180).b);
165884 sqlite3ExprDelete(pParse->db, (yypminor->yy595).pExpr);
165900 assert( pParser->yytos!=0 );
165901 assert( pParser->yytos > pParser->yystack );
165902 yytos = pParser->yytos--;
165907 yyTokenName[yytos->major]);
165910 yy_destructor(pParser, yytos->major, &yytos->minor);
165918 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
165920 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
165951 return pParser->yyhwm;
165957 ** is in state X and has a lookahead token Y. In a well-tested
165994 ** look-ahead token iLookAhead.
165997 YYCODETYPE iLookAhead, /* The look-ahead token */
166035 int j = i - iLookAhead + YYWILDCARD;
166058 ** Find the appropriate action for a parser given the non-terminal
166059 ** look-ahead token iLookAhead.
166063 YYCODETYPE iLookAhead /* The look-ahead token */
166098 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
166117 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166121 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
166122 yyNewState - YY_MIN_REDUCE);
166140 yypParser->yytos++;
166142 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
166143 yypParser->yyhwm++;
166144 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
166148 if( yypParser->yytos>yypParser->yystackEnd ){
166149 yypParser->yytos--;
166154 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
166156 yypParser->yytos--;
166163 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
166165 yytos = yypParser->yytos;
166166 yytos->stateno = yyNewState;
166167 yytos->major = yyMajor;
166168 yytos->minor.yy0 = yyMinor;
166172 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
166583 ** of symbols on the right-hand side of that rule. */
166585 -1, /* (0) explain ::= EXPLAIN */
166586 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
166587 -1, /* (2) cmdx ::= cmd */
166588 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
166590 -1, /* (5) transtype ::= DEFERRED */
166591 -1, /* (6) transtype ::= IMMEDIATE */
166592 -1, /* (7) transtype ::= EXCLUSIVE */
166593 -2, /* (8) cmd ::= COMMIT|END trans_opt */
166594 -2, /* (9) cmd ::= ROLLBACK trans_opt */
166595 -2, /* (10) cmd ::= SAVEPOINT nm */
166596 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
166597 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
166598 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
166599 -1, /* (14) createkw ::= CREATE */
166601 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
166602 -1, /* (17) temp ::= TEMP */
166604 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_option_set */
166605 -2, /* (20) create_table_args ::= AS select */
166607 -3, /* (22) table_option_set ::= table_option_set COMMA table_option */
166608 -2, /* (23) table_option ::= WITHOUT nm */
166609 -1, /* (24) table_option ::= nm */
166610 -2, /* (25) columnname ::= nm typetoken */
166612 -4, /* (27) typetoken ::= typename LP signed RP */
166613 -6, /* (28) typetoken ::= typename LP signed COMMA signed RP */
166614 -2, /* (29) typename ::= typename ID|STRING */
166617 -2, /* (32) ccons ::= CONSTRAINT nm */
166618 -3, /* (33) ccons ::= DEFAULT scantok term */
166619 -4, /* (34) ccons ::= DEFAULT LP expr RP */
166620 -4, /* (35) ccons ::= DEFAULT PLUS scantok term */
166621 -4, /* (36) ccons ::= DEFAULT MINUS scantok term */
166622 -3, /* (37) ccons ::= DEFAULT scantok ID|INDEXED */
166623 -3, /* (38) ccons ::= NOT NULL onconf */
166624 -5, /* (39) ccons ::= PRIMARY KEY sortorder onconf autoinc */
166625 -2, /* (40) ccons ::= UNIQUE onconf */
166626 -4, /* (41) ccons ::= CHECK LP expr RP */
166627 -4, /* (42) ccons ::= REFERENCES nm eidlist_opt refargs */
166628 -1, /* (43) ccons ::= defer_subclause */
166629 -2, /* (44) ccons ::= COLLATE ID|STRING */
166630 -3, /* (45) generated ::= LP expr RP */
166631 -4, /* (46) generated ::= LP expr RP ID */
166633 -1, /* (48) autoinc ::= AUTOINCR */
166635 -2, /* (50) refargs ::= refargs refarg */
166636 -2, /* (51) refarg ::= MATCH nm */
166637 -3, /* (52) refarg ::= ON INSERT refact */
166638 -3, /* (53) refarg ::= ON DELETE refact */
166639 -3, /* (54) refarg ::= ON UPDATE refact */
166640 -2, /* (55) refact ::= SET NULL */
166641 -2, /* (56) refact ::= SET DEFAULT */
166642 -1, /* (57) refact ::= CASCADE */
166643 -1, /* (58) refact ::= RESTRICT */
166644 -2, /* (59) refact ::= NO ACTION */
166645 -3, /* (60) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
166646 -2, /* (61) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
166648 -2, /* (63) init_deferred_pred_opt ::= INITIALLY DEFERRED */
166649 -2, /* (64) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
166651 -1, /* (66) tconscomma ::= COMMA */
166652 -2, /* (67) tcons ::= CONSTRAINT nm */
166653 -7, /* (68) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
166654 -5, /* (69) tcons ::= UNIQUE LP sortlist RP onconf */
166655 -5, /* (70) tcons ::= CHECK LP expr RP onconf */
166656 …-10, /* (71) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
166659 -3, /* (74) onconf ::= ON CONFLICT resolvetype */
166661 -2, /* (76) orconf ::= OR resolvetype */
166662 -1, /* (77) resolvetype ::= IGNORE */
166663 -1, /* (78) resolvetype ::= REPLACE */
166664 -4, /* (79) cmd ::= DROP TABLE ifexists fullname */
166665 -2, /* (80) ifexists ::= IF EXISTS */
166667 -9, /* (82) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
166668 -4, /* (83) cmd ::= DROP VIEW ifexists fullname */
166669 -1, /* (84) cmd ::= select */
166670 -3, /* (85) select ::= WITH wqlist selectnowith */
166671 -4, /* (86) select ::= WITH RECURSIVE wqlist selectnowith */
166672 -1, /* (87) select ::= selectnowith */
166673 -3, /* (88) selectnowith ::= selectnowith multiselect_op oneselect */
166674 -1, /* (89) multiselect_op ::= UNION */
166675 -2, /* (90) multiselect_op ::= UNION ALL */
166676 -1, /* (91) multiselect_op ::= EXCEPT|INTERSECT */
166677 …-9, /* (92) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
166678 …-10, /* (93) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
166679 -4, /* (94) values ::= VALUES LP nexprlist RP */
166680 -5, /* (95) values ::= values COMMA LP nexprlist RP */
166681 -1, /* (96) distinct ::= DISTINCT */
166682 -1, /* (97) distinct ::= ALL */
166685 -5, /* (100) selcollist ::= sclp scanpt expr scanpt as */
166686 -3, /* (101) selcollist ::= sclp scanpt STAR */
166687 -5, /* (102) selcollist ::= sclp scanpt nm DOT STAR */
166688 -2, /* (103) as ::= AS nm */
166691 -2, /* (106) from ::= FROM seltablist */
166692 -2, /* (107) stl_prefix ::= seltablist joinop */
166694 -5, /* (109) seltablist ::= stl_prefix nm dbnm as on_using */
166695 -6, /* (110) seltablist ::= stl_prefix nm dbnm as indexed_by on_using */
166696 -8, /* (111) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_using */
166697 -6, /* (112) seltablist ::= stl_prefix LP select RP as on_using */
166698 -6, /* (113) seltablist ::= stl_prefix LP seltablist RP as on_using */
166700 -2, /* (115) dbnm ::= DOT nm */
166701 -1, /* (116) fullname ::= nm */
166702 -3, /* (117) fullname ::= nm DOT nm */
166703 -1, /* (118) xfullname ::= nm */
166704 -3, /* (119) xfullname ::= nm DOT nm */
166705 -5, /* (120) xfullname ::= nm DOT nm AS nm */
166706 -3, /* (121) xfullname ::= nm AS nm */
166707 -1, /* (122) joinop ::= COMMA|JOIN */
166708 -2, /* (123) joinop ::= JOIN_KW JOIN */
166709 -3, /* (124) joinop ::= JOIN_KW nm JOIN */
166710 -4, /* (125) joinop ::= JOIN_KW nm nm JOIN */
166711 -2, /* (126) on_using ::= ON expr */
166712 -4, /* (127) on_using ::= USING LP idlist RP */
166715 -3, /* (130) indexed_by ::= INDEXED BY nm */
166716 -2, /* (131) indexed_by ::= NOT INDEXED */
166718 -3, /* (133) orderby_opt ::= ORDER BY sortlist */
166719 -5, /* (134) sortlist ::= sortlist COMMA expr sortorder nulls */
166720 -3, /* (135) sortlist ::= expr sortorder nulls */
166721 -1, /* (136) sortorder ::= ASC */
166722 -1, /* (137) sortorder ::= DESC */
166724 -2, /* (139) nulls ::= NULLS FIRST */
166725 -2, /* (140) nulls ::= NULLS LAST */
166728 -3, /* (143) groupby_opt ::= GROUP BY nexprlist */
166730 -2, /* (145) having_opt ::= HAVING expr */
166732 -2, /* (147) limit_opt ::= LIMIT expr */
166733 -4, /* (148) limit_opt ::= LIMIT expr OFFSET expr */
166734 -4, /* (149) limit_opt ::= LIMIT expr COMMA expr */
166735 -6, /* (150) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
166737 -2, /* (152) where_opt ::= WHERE expr */
166739 -2, /* (154) where_opt_ret ::= WHERE expr */
166740 -2, /* (155) where_opt_ret ::= RETURNING selcollist */
166741 -4, /* (156) where_opt_ret ::= WHERE expr RETURNING selcollist */
166742 -9, /* (157) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
166743 -5, /* (158) setlist ::= setlist COMMA nm EQ expr */
166744 -7, /* (159) setlist ::= setlist COMMA LP idlist RP EQ expr */
166745 -3, /* (160) setlist ::= nm EQ expr */
166746 -5, /* (161) setlist ::= LP idlist RP EQ expr */
166747 -7, /* (162) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
166748 -8, /* (163) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
166750 -2, /* (165) upsert ::= RETURNING selcollist */
166751 …-12, /* (166) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
166752 -9, /* (167) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
166753 -5, /* (168) upsert ::= ON CONFLICT DO NOTHING returning */
166754 -8, /* (169) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
166755 -2, /* (170) returning ::= RETURNING selcollist */
166756 -2, /* (171) insert_cmd ::= INSERT orconf */
166757 -1, /* (172) insert_cmd ::= REPLACE */
166759 -3, /* (174) idlist_opt ::= LP idlist RP */
166760 -3, /* (175) idlist ::= idlist COMMA nm */
166761 -1, /* (176) idlist ::= nm */
166762 -3, /* (177) expr ::= LP expr RP */
166763 -1, /* (178) expr ::= ID|INDEXED */
166764 -1, /* (179) expr ::= JOIN_KW */
166765 -3, /* (180) expr ::= nm DOT nm */
166766 -5, /* (181) expr ::= nm DOT nm DOT nm */
166767 -1, /* (182) term ::= NULL|FLOAT|BLOB */
166768 -1, /* (183) term ::= STRING */
166769 -1, /* (184) term ::= INTEGER */
166770 -1, /* (185) expr ::= VARIABLE */
166771 -3, /* (186) expr ::= expr COLLATE ID|STRING */
166772 -6, /* (187) expr ::= CAST LP expr AS typetoken RP */
166773 -5, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP */
166774 -4, /* (189) expr ::= ID|INDEXED LP STAR RP */
166775 -6, /* (190) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
166776 -5, /* (191) expr ::= ID|INDEXED LP STAR RP filter_over */
166777 -1, /* (192) term ::= CTIME_KW */
166778 -5, /* (193) expr ::= LP nexprlist COMMA expr RP */
166779 -3, /* (194) expr ::= expr AND expr */
166780 -3, /* (195) expr ::= expr OR expr */
166781 -3, /* (196) expr ::= expr LT|GT|GE|LE expr */
166782 -3, /* (197) expr ::= expr EQ|NE expr */
166783 -3, /* (198) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
166784 -3, /* (199) expr ::= expr PLUS|MINUS expr */
166785 -3, /* (200) expr ::= expr STAR|SLASH|REM expr */
166786 -3, /* (201) expr ::= expr CONCAT expr */
166787 -2, /* (202) likeop ::= NOT LIKE_KW|MATCH */
166788 -3, /* (203) expr ::= expr likeop expr */
166789 -5, /* (204) expr ::= expr likeop expr ESCAPE expr */
166790 -2, /* (205) expr ::= expr ISNULL|NOTNULL */
166791 -3, /* (206) expr ::= expr NOT NULL */
166792 -3, /* (207) expr ::= expr IS expr */
166793 -4, /* (208) expr ::= expr IS NOT expr */
166794 -6, /* (209) expr ::= expr IS NOT DISTINCT FROM expr */
166795 -5, /* (210) expr ::= expr IS DISTINCT FROM expr */
166796 -2, /* (211) expr ::= NOT expr */
166797 -2, /* (212) expr ::= BITNOT expr */
166798 -2, /* (213) expr ::= PLUS|MINUS expr */
166799 -3, /* (214) expr ::= expr PTR expr */
166800 -1, /* (215) between_op ::= BETWEEN */
166801 -2, /* (216) between_op ::= NOT BETWEEN */
166802 -5, /* (217) expr ::= expr between_op expr AND expr */
166803 -1, /* (218) in_op ::= IN */
166804 -2, /* (219) in_op ::= NOT IN */
166805 -5, /* (220) expr ::= expr in_op LP exprlist RP */
166806 -3, /* (221) expr ::= LP select RP */
166807 -5, /* (222) expr ::= expr in_op LP select RP */
166808 -5, /* (223) expr ::= expr in_op nm dbnm paren_exprlist */
166809 -4, /* (224) expr ::= EXISTS LP select RP */
166810 -5, /* (225) expr ::= CASE case_operand case_exprlist case_else END */
166811 -5, /* (226) case_exprlist ::= case_exprlist WHEN expr THEN expr */
166812 -4, /* (227) case_exprlist ::= WHEN expr THEN expr */
166813 -2, /* (228) case_else ::= ELSE expr */
166815 -1, /* (230) case_operand ::= expr */
166818 -3, /* (233) nexprlist ::= nexprlist COMMA expr */
166819 -1, /* (234) nexprlist ::= expr */
166821 -3, /* (236) paren_exprlist ::= LP exprlist RP */
166822 …-12, /* (237) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
166823 -1, /* (238) uniqueflag ::= UNIQUE */
166826 -3, /* (241) eidlist_opt ::= LP eidlist RP */
166827 -5, /* (242) eidlist ::= eidlist COMMA nm collate sortorder */
166828 -3, /* (243) eidlist ::= nm collate sortorder */
166830 -2, /* (245) collate ::= COLLATE ID|STRING */
166831 -4, /* (246) cmd ::= DROP INDEX ifexists fullname */
166832 -2, /* (247) cmd ::= VACUUM vinto */
166833 -3, /* (248) cmd ::= VACUUM nm vinto */
166834 -2, /* (249) vinto ::= INTO expr */
166836 -3, /* (251) cmd ::= PRAGMA nm dbnm */
166837 -5, /* (252) cmd ::= PRAGMA nm dbnm EQ nmnum */
166838 -6, /* (253) cmd ::= PRAGMA nm dbnm LP nmnum RP */
166839 -5, /* (254) cmd ::= PRAGMA nm dbnm EQ minus_num */
166840 -6, /* (255) cmd ::= PRAGMA nm dbnm LP minus_num RP */
166841 -2, /* (256) plus_num ::= PLUS INTEGER|FLOAT */
166842 -2, /* (257) minus_num ::= MINUS INTEGER|FLOAT */
166843 -5, /* (258) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
166844 …-11, /* (259) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
166845 -1, /* (260) trigger_time ::= BEFORE|AFTER */
166846 -2, /* (261) trigger_time ::= INSTEAD OF */
166848 -1, /* (263) trigger_event ::= DELETE|INSERT */
166849 -1, /* (264) trigger_event ::= UPDATE */
166850 -3, /* (265) trigger_event ::= UPDATE OF idlist */
166852 -2, /* (267) when_clause ::= WHEN expr */
166853 -3, /* (268) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
166854 -2, /* (269) trigger_cmd_list ::= trigger_cmd SEMI */
166855 -3, /* (270) trnm ::= nm DOT nm */
166856 -3, /* (271) tridxby ::= INDEXED BY nm */
166857 -2, /* (272) tridxby ::= NOT INDEXED */
166858 -9, /* (273) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
166859 -8, /* (274) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
166860 -6, /* (275) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
166861 -3, /* (276) trigger_cmd ::= scanpt select scanpt */
166862 -4, /* (277) expr ::= RAISE LP IGNORE RP */
166863 -6, /* (278) expr ::= RAISE LP raisetype COMMA nm RP */
166864 -1, /* (279) raisetype ::= ROLLBACK */
166865 -1, /* (280) raisetype ::= ABORT */
166866 -1, /* (281) raisetype ::= FAIL */
166867 -4, /* (282) cmd ::= DROP TRIGGER ifexists fullname */
166868 -6, /* (283) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
166869 -3, /* (284) cmd ::= DETACH database_kw_opt expr */
166871 -2, /* (286) key_opt ::= KEY expr */
166872 -1, /* (287) cmd ::= REINDEX */
166873 -3, /* (288) cmd ::= REINDEX nm dbnm */
166874 -1, /* (289) cmd ::= ANALYZE */
166875 -3, /* (290) cmd ::= ANALYZE nm dbnm */
166876 -6, /* (291) cmd ::= ALTER TABLE fullname RENAME TO nm */
166877 -7, /* (292) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
166878 -6, /* (293) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
166879 -1, /* (294) add_column_fullname ::= fullname */
166880 -8, /* (295) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
166881 -1, /* (296) cmd ::= create_vtab */
166882 -4, /* (297) cmd ::= create_vtab LP vtabarglist RP */
166883 -8, /* (298) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
166885 -1, /* (300) vtabargtoken ::= ANY */
166886 -3, /* (301) vtabargtoken ::= lp anylist RP */
166887 -1, /* (302) lp ::= LP */
166888 -2, /* (303) with ::= WITH wqlist */
166889 -3, /* (304) with ::= WITH RECURSIVE wqlist */
166890 -1, /* (305) wqas ::= AS */
166891 -2, /* (306) wqas ::= AS MATERIALIZED */
166892 -3, /* (307) wqas ::= AS NOT MATERIALIZED */
166893 -6, /* (308) wqitem ::= nm eidlist_opt wqas LP select RP */
166894 -1, /* (309) wqlist ::= wqitem */
166895 -3, /* (310) wqlist ::= wqlist COMMA wqitem */
166896 -1, /* (311) windowdefn_list ::= windowdefn */
166897 -3, /* (312) windowdefn_list ::= windowdefn_list COMMA windowdefn */
166898 -5, /* (313) windowdefn ::= nm AS LP window RP */
166899 -5, /* (314) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
166900 -6, /* (315) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
166901 -4, /* (316) window ::= ORDER BY sortlist frame_opt */
166902 -5, /* (317) window ::= nm ORDER BY sortlist frame_opt */
166903 -1, /* (318) window ::= frame_opt */
166904 -2, /* (319) window ::= nm frame_opt */
166906 -3, /* (321) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
166907 …-6, /* (322) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
166908 -1, /* (323) range_or_rows ::= RANGE|ROWS|GROUPS */
166909 -1, /* (324) frame_bound_s ::= frame_bound */
166910 -2, /* (325) frame_bound_s ::= UNBOUNDED PRECEDING */
166911 -1, /* (326) frame_bound_e ::= frame_bound */
166912 -2, /* (327) frame_bound_e ::= UNBOUNDED FOLLOWING */
166913 -2, /* (328) frame_bound ::= expr PRECEDING|FOLLOWING */
166914 -2, /* (329) frame_bound ::= CURRENT ROW */
166916 -2, /* (331) frame_exclude_opt ::= EXCLUDE frame_exclude */
166917 -2, /* (332) frame_exclude ::= NO OTHERS */
166918 -2, /* (333) frame_exclude ::= CURRENT ROW */
166919 -1, /* (334) frame_exclude ::= GROUP|TIES */
166920 -2, /* (335) window_clause ::= WINDOW windowdefn_list */
166921 -2, /* (336) filter_over ::= filter_clause over_clause */
166922 -1, /* (337) filter_over ::= over_clause */
166923 -1, /* (338) filter_over ::= filter_clause */
166924 -4, /* (339) over_clause ::= OVER LP window RP */
166925 -2, /* (340) over_clause ::= OVER nm */
166926 -5, /* (341) filter_clause ::= FILTER LP WHERE expr RP */
166927 -1, /* (342) input ::= cmdlist */
166928 -2, /* (343) cmdlist ::= cmdlist ecmd */
166929 -1, /* (344) cmdlist ::= ecmd */
166930 -1, /* (345) ecmd ::= SEMI */
166931 -2, /* (346) ecmd ::= cmdx SEMI */
166932 -3, /* (347) ecmd ::= explain cmdx SEMI */
166934 -1, /* (349) trans_opt ::= TRANSACTION */
166935 -2, /* (350) trans_opt ::= TRANSACTION nm */
166936 -1, /* (351) savepoint_opt ::= SAVEPOINT */
166938 -2, /* (353) cmd ::= create_table create_table_args */
166939 -1, /* (354) table_option_set ::= table_option */
166940 -4, /* (355) columnlist ::= columnlist COMMA columnname carglist */
166941 -2, /* (356) columnlist ::= columnname carglist */
166942 -1, /* (357) nm ::= ID|INDEXED */
166943 -1, /* (358) nm ::= STRING */
166944 -1, /* (359) nm ::= JOIN_KW */
166945 -1, /* (360) typetoken ::= typename */
166946 -1, /* (361) typename ::= ID|STRING */
166947 -1, /* (362) signed ::= plus_num */
166948 -1, /* (363) signed ::= minus_num */
166949 -2, /* (364) carglist ::= carglist ccons */
166951 -2, /* (366) ccons ::= NULL onconf */
166952 -4, /* (367) ccons ::= GENERATED ALWAYS AS generated */
166953 -2, /* (368) ccons ::= AS generated */
166954 -2, /* (369) conslist_opt ::= COMMA conslist */
166955 -3, /* (370) conslist ::= conslist tconscomma tcons */
166956 -1, /* (371) conslist ::= tcons */
166958 -1, /* (373) defer_subclause_opt ::= defer_subclause */
166959 -1, /* (374) resolvetype ::= raisetype */
166960 -1, /* (375) selectnowith ::= oneselect */
166961 -1, /* (376) oneselect ::= values */
166962 -2, /* (377) sclp ::= selcollist COMMA */
166963 -1, /* (378) as ::= ID|STRING */
166964 -1, /* (379) indexed_opt ::= indexed_by */
166966 -1, /* (381) expr ::= term */
166967 -1, /* (382) likeop ::= LIKE_KW|MATCH */
166968 -1, /* (383) exprlist ::= nexprlist */
166969 -1, /* (384) nmnum ::= plus_num */
166970 -1, /* (385) nmnum ::= nm */
166971 -1, /* (386) nmnum ::= ON */
166972 -1, /* (387) nmnum ::= DELETE */
166973 -1, /* (388) nmnum ::= DEFAULT */
166974 -1, /* (389) plus_num ::= INTEGER|FLOAT */
166976 -3, /* (391) foreach_clause ::= FOR EACH ROW */
166977 -1, /* (392) trnm ::= nm */
166979 -1, /* (394) database_kw_opt ::= DATABASE */
166982 -1, /* (397) kwcolumn_opt ::= COLUMNKW */
166983 -1, /* (398) vtabarglist ::= vtabarg */
166984 -3, /* (399) vtabarglist ::= vtabarglist COMMA vtabarg */
166985 -2, /* (400) vtabarg ::= vtabarg vtabargtoken */
166987 -4, /* (402) anylist ::= anylist LP anylist RP */
166988 -2, /* (403) anylist ::= anylist ANY */
167001 ** only called from one place, optimizing compilers will in-line it, which
167018 yymsp = yypParser->yytos;
167032 { pParse->explain = 1; }
167035 { pParse->explain = 2; }
167041 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy394);}
167050 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/}
167054 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
167073 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy394,0,0,yymsp[…
167090 {yymsp[-2].minor.yy394 = 1;}
167093 {yymsp[0].minor.yy394 = pParse->db->init.busy==0;}
167097 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy285,0);
167103 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167110 {yylhsminor.yy285 = yymsp[-2].minor.yy285|yymsp[0].minor.yy285;}
167111 yymsp[-2].minor.yy285 = yylhsminor.yy285;
167116 yymsp[-1].minor.yy285 = TF_WithoutRowid | TF_NoVisibleRowid;
167118 yymsp[-1].minor.yy285 = 0;
167135 {sqlite3AddColumn(pParse,yymsp[-1].minor.yy0,yymsp[0].minor.yy0);}
167144 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
167149 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
167153 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
167169 {pParse->constraintName = yymsp[0].minor.yy0;}
167172 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
167175 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
167178 …te3AddDefaultValue(pParse,yymsp[0].minor.yy528,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[…
167183 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
167191 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
167200 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy394,yymsp[0].minor.yy394,yymsp[-2].minor.yy394);}
167207 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy528,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
167210 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy394);}
167219 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy528,0);}
167222 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy528,&yymsp[0].minor.yy0);}
167228 { yymsp[1].minor.yy394 = OE_None*0x0101; /* EV: R-19803-45884 */}
167231 { yymsp[-1].minor.yy394 = (yymsp[-1].minor.yy394 & ~yymsp[0].minor.yy231.mask) | yymsp[0].minor.yy2…
167234 { yymsp[-1].minor.yy231.value = 0; yymsp[-1].minor.yy231.mask = 0x000000; }
167237 { yymsp[-2].minor.yy231.value = 0; yymsp[-2].minor.yy231.mask = 0x000000; }
167240 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394; yymsp[-2].minor.yy231.mask = 0x0000ff; }
167243 { yymsp[-2].minor.yy231.value = yymsp[0].minor.yy394<<8; yymsp[-2].minor.yy231.mask = 0x00ff00; }
167246 { yymsp[-1].minor.yy394 = OE_SetNull; /* EV: R-33326-45252 */}
167249 { yymsp[-1].minor.yy394 = OE_SetDflt; /* EV: R-33326-45252 */}
167252 { yymsp[0].minor.yy394 = OE_Cascade; /* EV: R-33326-45252 */}
167255 { yymsp[0].minor.yy394 = OE_Restrict; /* EV: R-33326-45252 */}
167258 { yymsp[-1].minor.yy394 = OE_None; /* EV: R-33326-45252 */}
167261 {yymsp[-2].minor.yy394 = 0;}
167266 {yymsp[-1].minor.yy394 = yymsp[0].minor.yy394;}
167273 {yymsp[-1].minor.yy394 = 1;}
167276 {yymsp[-1].minor.yy394 = 0;}
167279 {pParse->constraintName.n = 0;}
167282 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy394,yymsp[-2].minor.yy394,0);}
167285 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy394,0,0,0,0,
167289 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy528,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
167293 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322…
167302 {yymsp[-2].minor.yy394 = yymsp[0].minor.yy394;}
167313 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 0, yymsp[-1].minor.yy394);
167318 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[…
167323 sqlite3DropTable(pParse, yymsp[0].minor.yy131, 1, yymsp[-1].minor.yy394);
167330 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy47);
167334 {yymsp[-2].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167337 {yymsp[-3].minor.yy47 = attachWithToSelect(pParse,yymsp[0].minor.yy47,yymsp[-1].minor.yy521);}
167345 yymsp[0].minor.yy47 = p; /*A-overwrites-X*/
167351 Select *pLhs = yymsp[-2].minor.yy47;
167352 if( pRhs && pRhs->pPrior ){
167361 pRhs->op = (u8)yymsp[-1].minor.yy394;
167362 pRhs->pPrior = pLhs;
167363 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
167364 pRhs->selFlags &= ~SF_MultiValue;
167365 if( yymsp[-1].minor.yy394!=TK_ALL ) pParse->hasCompound = 1;
167367 sqlite3SelectDelete(pParse->db, pLhs);
167369 yymsp[-2].minor.yy47 = pRhs;
167374 {yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-OP*/}
167377 {yymsp[-1].minor.yy394 = TK_ALL;}
167381 …-8].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy131,yymsp[-4].min…
167386 …-9].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy322,yymsp[-6].minor.yy131,yymsp[-5].min…
167387 if( yymsp[-9].minor.yy47 ){
167388 yymsp[-9].minor.yy47->pWinDefn = yymsp[-2].minor.yy41;
167390 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy41);
167396 yymsp[-3].minor.yy47 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values,0);
167401 Select *pRight, *pLeft = yymsp[-4].minor.yy47;
167402 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy322,0,0,0,0,0,SF_Values|SF_MultiValue,0);
167403 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
167405 pRight->op = TK_ALL;
167406 pRight->pPrior = pLeft;
167407 yymsp[-4].minor.yy47 = pRight;
167409 yymsp[-4].minor.yy47 = pLeft;
167429 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[-2].minor.yy528…
167430 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[0].minor…
167431 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy322,yymsp[-3].minor.yy522,yymsp[-1].minor.yy522);
167436 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
167437 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, p);
167443 Expr *pLeft = tokenExpr(pParse, TK_ID, yymsp[-2].minor.yy0);
167445 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, pDot);
167452 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
167460 yymsp[-1].minor.yy131 = yymsp[0].minor.yy131;
167461 sqlite3SrcListShiftJoinType(pParse,yymsp[-1].minor.yy131);
167466 …if( ALWAYS(yymsp[-1].minor.yy131 && yymsp[-1].minor.yy131->nSrc>0) ) yymsp[-1].minor.yy131->a[yyms…
167471 …yymsp[-4].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-4].minor.yy131,&yymsp[-3].minor…
167476 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,&yymsp[-4].minor…
167477 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-1].minor.yy0);
167482 …yymsp[-7].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-7].minor.yy131,&yymsp[-6].minor…
167483 sqlite3SrcListFuncArgs(pParse, yymsp[-7].minor.yy131, yymsp[-3].minor.yy322);
167488 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167493 …if( yymsp[-5].minor.yy131==0 && yymsp[-1].minor.yy0.n==0 && yymsp[0].minor.yy561.pOn==0 && yymsp[0…
167494 yymsp[-5].minor.yy131 = yymsp[-3].minor.yy131;
167495 }else if( yymsp[-3].minor.yy131->nSrc==1 ){
167496 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167497 if( yymsp[-5].minor.yy131 ){
167498 SrcItem *pNew = &yymsp[-5].minor.yy131->a[yymsp[-5].minor.yy131->nSrc-1];
167499 SrcItem *pOld = yymsp[-3].minor.yy131->a;
167500 pNew->zName = pOld->zName;
167501 pNew->zDatabase = pOld->zDatabase;
167502 pNew->pSelect = pOld->pSelect;
167503 if( pNew->pSelect && (pNew->pSelect->selFlags & SF_NestedFrom)!=0 ){
167504 pNew->fg.isNestedFrom = 1;
167506 if( pOld->fg.isTabFunc ){
167507 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
167508 pOld->u1.pFuncArg = 0;
167509 pOld->fg.isTabFunc = 0;
167510 pNew->fg.isTabFunc = 1;
167512 pOld->zName = pOld->zDatabase = 0;
167513 pOld->pSelect = 0;
167515 sqlite3SrcListDelete(pParse->db, yymsp[-3].minor.yy131);
167518 sqlite3SrcListShiftJoinType(pParse,yymsp[-3].minor.yy131);
167519 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-3].minor.yy131,0,0,0,0,SF_NestedFrom,0);
167520 …yymsp[-5].minor.yy131 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-5].minor.yy131,0,0,&yymsp[-1].m…
167531 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
167537 yylhsminor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
167538 …if( IN_RENAME_OBJECT && yylhsminor.yy131 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy131->a[0].zN…
167540 yymsp[-2].minor.yy131 = yylhsminor.yy131;
167543 {yymsp[0].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
167546 {yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
167550 …yymsp[-4].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
167551 …if( yymsp[-4].minor.yy131 ) yymsp[-4].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
167556 yymsp[-2].minor.yy131 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
167557 …if( yymsp[-2].minor.yy131 ) yymsp[-2].minor.yy131->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
167564 {yymsp[-1].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
167567 {yymsp[-2].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-o…
167570 {yymsp[-3].minor.yy394 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1…
167573 {yymsp[-1].minor.yy561.pOn = yymsp[0].minor.yy528; yymsp[-1].minor.yy561.pUsing = 0;}
167576 {yymsp[-3].minor.yy561.pOn = 0; yymsp[-3].minor.yy561.pUsing = yymsp[-1].minor.yy254;}
167582 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
167585 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
167589 {yymsp[-2].minor.yy322 = yymsp[0].minor.yy322;}
167593 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322,yymsp[-2].minor.yy528);
167594 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167599 yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy528); /*A-overwrites-Y*/
167600 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy322,yymsp[-1].minor.yy394,yymsp[0].minor.yy394);
167614 {yymsp[-1].minor.yy394 = SQLITE_SO_ASC;}
167617 {yymsp[-1].minor.yy394 = SQLITE_SO_DESC;}
167633 {yymsp[-1].minor.yy528 = yymsp[0].minor.yy528;}
167636 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,0);}
167639 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167642 {yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy528,yymsp[-2].minor.yy528);}
167646 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy131, &yymsp[-1].minor.yy0);
167647 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy131,yymsp[0].minor.yy528,0,0);
167651 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-1].minor.yy528 = 0;}
167654 {sqlite3AddReturning(pParse,yymsp[0].minor.yy322); yymsp[-3].minor.yy528 = yymsp[-2].minor.yy528;}
167658 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167659 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167660 if( yymsp[-1].minor.yy131 ){
167661 SrcList *pFromClause = yymsp[-1].minor.yy131;
167662 if( pFromClause->nSrc>1 ){
167670 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
167672 …sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].mi…
167677 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
167678 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy0, 1);
167683 …yymsp[-6].minor.yy322 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy322, yymsp[-3].minor…
167689 sqlite3ExprListSetName(pParse, yylhsminor.yy322, &yymsp[-2].minor.yy0, 1);
167691 yymsp[-2].minor.yy322 = yylhsminor.yy322;
167695 …yymsp[-4].minor.yy322 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy254, yymsp[0].min…
167700 …sqlite3Insert(pParse, yymsp[-3].minor.yy131, yymsp[-1].minor.yy47, yymsp[-2].minor.yy254, yymsp[-5…
167705 sqlite3Insert(pParse, yymsp[-4].minor.yy131, 0, yymsp[-3].minor.yy254, yymsp[-6].minor.yy394, 0);
167712 { yymsp[-1].minor.yy444 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy322); }
167715 { yymsp[-11].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy322,yymsp[-6].minor.yy528,…
167718 { yymsp[-8].minor.yy444 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy322,yymsp[-3].minor.yy528,0…
167721 { yymsp[-4].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
167724 { yymsp[-7].minor.yy444 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy322,yymsp[-1].minor.yy5…
167733 {yymsp[-2].minor.yy254 = yymsp[-1].minor.yy254;}
167736 {yymsp[-2].minor.yy254 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy254,&yymsp[0].minor.yy0);}
167739 {yymsp[0].minor.yy254 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
167742 {yymsp[-2].minor.yy528 = yymsp[-1].minor.yy528;}
167746 {yymsp[0].minor.yy528=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167750 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
167754 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167758 Expr *temp1 = tokenExpr(pParse,TK_ID,yymsp[-4].minor.yy0);
167759 Expr *temp2 = tokenExpr(pParse,TK_ID,yymsp[-2].minor.yy0);
167767 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167771 {yymsp[0].minor.yy528=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
167775 yylhsminor.yy528 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
167776 if( yylhsminor.yy528 ) yylhsminor.yy528->w.iOfst = (int)(yymsp[0].minor.yy0.z - pParse->zTail);
167789 ** in the virtual machine. #N is the N-th register. */
167790 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
167792 if( pParse->nested==0 ){
167797 if( yymsp[0].minor.yy528 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy528->iTable);
167804 …yymsp[-2].minor.yy528 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy528, &yymsp[0].minor.…
167809 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
167810 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy528, yymsp[-3].minor.yy528, 0);
167815 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0, yymsp[…
167817 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167821 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
167823 yymsp[-3].minor.yy528 = yylhsminor.yy528;
167827 …yylhsminor.yy528 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy322, &yymsp[-5].minor.yy0, yymsp[…
167830 yymsp[-5].minor.yy528 = yylhsminor.yy528;
167834 yylhsminor.yy528 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
167837 yymsp[-4].minor.yy528 = yylhsminor.yy528;
167847 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy322, yymsp[-1].minor.yy528);
167848 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
167849 if( yymsp[-4].minor.yy528 ){
167850 yymsp[-4].minor.yy528->x.pList = pList;
167851 if( ALWAYS(pList->nExpr) ){
167852 yymsp[-4].minor.yy528->flags |= pList->a[0].pExpr->flags & EP_Propagate;
167855 sqlite3ExprListDelete(pParse->db, pList);
167860 {yymsp[-2].minor.yy528=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);}
167869 {yymsp[-2].minor.yy528=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy528,yymsp[0].minor.yy5…
167872 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
167877 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
167878 yymsp[-1].minor.yy0.n &= 0x7fffffff;
167880 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy528);
167881 yymsp[-2].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167882 if( bNot ) yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy528, 0);
167883 if( yymsp[-2].minor.yy528 ) yymsp[-2].minor.yy528->flags |= EP_InfixFunc;
167889 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
167890 yymsp[-3].minor.yy0.n &= 0x7fffffff;
167891 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167892 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy528);
167894 yymsp[-4].minor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
167895 if( bNot ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy528, 0);
167896 if( yymsp[-4].minor.yy528 ) yymsp[-4].minor.yy528->flags |= EP_InfixFunc;
167900 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy528,0);}
167903 {yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy528,0);}
167907 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy528,yymsp[0].minor.yy528);
167908 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-2].minor.yy528, TK_ISNULL);
167913 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy528,yymsp[0].minor.yy528);
167914 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-3].minor.yy528, TK_NOTNULL);
167919 yymsp[-5].minor.yy528 = sqlite3PExpr(pParse,TK_IS,yymsp[-5].minor.yy528,yymsp[0].minor.yy528);
167920 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-5].minor.yy528, TK_ISNULL);
167925 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-4].minor.yy528,yymsp[0].minor.yy528);
167926 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy528, yymsp[-4].minor.yy528, TK_NOTNULL);
167931 {yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy528, 0);/*A-overwri…
167935 …yymsp[-1].minor.yy528 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
167936 /*A-overwrites-B*/
167941 ExprList *pList = sqlite3ExprListAppend(pParse, 0, yymsp[-2].minor.yy528);
167943 yylhsminor.yy528 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
167945 yymsp[-2].minor.yy528 = yylhsminor.yy528;
167953 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
167955 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy528, 0);
167956 if( yymsp[-4].minor.yy528 ){
167957 yymsp[-4].minor.yy528->x.pList = pList;
167959 sqlite3ExprListDelete(pParse->db, pList);
167961 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
167966 if( yymsp[-1].minor.yy322==0 ){
167975 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167976 …yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false…
167977 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
167979 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167980 …if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=T…
167981 yymsp[-1].minor.yy322->a[0].pExpr = 0;
167982 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167984 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy528, pRHS);
167986 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
167987 if( yymsp[-4].minor.yy528==0 ){
167988 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
167989 }else if( yymsp[-4].minor.yy528->pLeft->op==TK_VECTOR ){
167990 int nExpr = yymsp[-4].minor.yy528->pLeft->x.pList->nExpr;
167991 Select *pSelectRHS = sqlite3ExprListToValues(pParse, nExpr, yymsp[-1].minor.yy322);
167994 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelectRHS);
167997 yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy322;
167998 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
168001 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
168007 yymsp[-2].minor.yy528 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
168008 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy528, yymsp[-1].minor.yy47);
168013 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
168014 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, yymsp[-1].minor.yy47);
168015 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
168020 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
168023 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy528, 0);
168024 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy528, pSelect);
168025 …if( yymsp[-3].minor.yy394 ) yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.y…
168031 p = yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
168032 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy47);
168037 yymsp[-4].minor.yy528 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy528, 0);
168038 if( yymsp[-4].minor.yy528 ){
168039 …yymsp[-4].minor.yy528->x.pList = yymsp[-1].minor.yy528 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
168040 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy528);
168042 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322);
168043 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168049 …yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy528);
168050 yymsp[-4].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[0].minor.yy528);
168055 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy528);
168056 yymsp[-3].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, yymsp[0].minor.yy528);
168060 {yymsp[0].minor.yy528 = yymsp[0].minor.yy528; /*A-overwrites-X*/}
168063 {yymsp[-2].minor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy528);}
168066 {yymsp[0].minor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy528); /*A-overwrites-Y*/}
168070 {yymsp[-2].minor.yy322 = yymsp[-1].minor.yy322;}
168074 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
168075 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy322, yymsp[-10].minor.yy3…
168076 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy528, SQLITE_SO_ASC, yymsp[-8].minor.yy394, SQLITE_IDXTYPE_…
168077 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
168078 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
168091 …yymsp[-4].minor.yy322 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy322, &yymsp[-2].minor.yy…
168096 …yymsp[-2].minor.yy322 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
168100 {sqlite3DropIndex(pParse, yymsp[0].minor.yy131, yymsp[-1].minor.yy394);}
168106 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy528);}
168109 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
168112 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
168115 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
168118 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
168121 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
168126 all.z = yymsp[-3].minor.yy0.z;
168127 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
168128 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy33, &all);
168133 …-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy394, yymsp[-4].minor.yy180.a, yymsp[-4].min…
168134 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
168138 { yymsp[0].minor.yy394 = yymsp[0].major; /*A-overwrites-X*/ }
168141 { yymsp[-1].minor.yy394 = TK_INSTEAD;}
168148 {yymsp[0].minor.yy180.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy180.b = 0;}
168151 {yymsp[-2].minor.yy180.a = TK_UPDATE; yymsp[-2].minor.yy180.b = yymsp[0].minor.yy254;}
168159 { yymsp[-1].minor.yy528 = yymsp[0].minor.yy528; }
168163 assert( yymsp[-2].minor.yy33!=0 );
168164 yymsp[-2].minor.yy33->pLast->pNext = yymsp[-1].minor.yy33;
168165 yymsp[-2].minor.yy33->pLast = yymsp[-1].minor.yy33;
168170 assert( yymsp[-1].minor.yy33!=0 );
168171 yymsp[-1].minor.yy33->pLast = yymsp[-1].minor.yy33;
168176 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
168197 …tep(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy131, yymsp[-3].minor.yy322, yymsp[-1].minor.yy…
168198 yymsp[-8].minor.yy33 = yylhsminor.yy33;
168202 …-4].minor.yy0,yymsp[-3].minor.yy254,yymsp[-2].minor.yy47,yymsp[-6].minor.yy394,yymsp[-1].minor.yy4…
168204 yymsp[-7].minor.yy33 = yylhsminor.yy33;
168207 …sminor.yy33 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy528, yymsp[…
168208 yymsp[-5].minor.yy33 = yylhsminor.yy33;
168211 …ggerSelectStep(pParse->db, yymsp[-1].minor.yy47, yymsp[-2].minor.yy522, yymsp[0].minor.yy522); /*y…
168212 yymsp[-2].minor.yy33 = yylhsminor.yy33;
168216 yymsp[-3].minor.yy528 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
168217 if( yymsp[-3].minor.yy528 ){
168218 yymsp[-3].minor.yy528->affExpr = OE_Ignore;
168224 yymsp[-5].minor.yy528 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
168225 if( yymsp[-5].minor.yy528 ) {
168226 yymsp[-5].minor.yy528->affExpr = (char)yymsp[-3].minor.yy394;
168238 sqlite3DropTrigger(pParse,yymsp[0].minor.yy131,yymsp[-1].minor.yy394);
168243 sqlite3Attach(pParse, yymsp[-3].minor.yy528, yymsp[-1].minor.yy528, yymsp[0].minor.yy528);
168255 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168261 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
168265 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy131,&yymsp[0].minor.yy0);
168270 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
168271 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
168276 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy131, &yymsp[0].minor.yy0);
168287 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy131, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
168298 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
168317 {yymsp[-1].minor.yy516 = M10d_Yes;}
168320 {yymsp[-2].minor.yy516 = M10d_No;}
168324 …sp[-5].minor.yy385 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy322, yymsp[-1].…
168329 yymsp[0].minor.yy521 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy385); /*A-overwrites-X*/
168334 yymsp[-2].minor.yy521 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy521, yymsp[0].minor.yy385);
168344 sqlite3WindowChain(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy41);
168345 yymsp[0].minor.yy41->pNextWin = yymsp[-2].minor.yy41;
168348 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168352 if( ALWAYS(yymsp[-1].minor.yy41) ){
168353 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
168355 yylhsminor.yy41 = yymsp[-1].minor.yy41;
168357 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168361 …yymsp[-4].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, y…
168366 …e3WindowAssemble(pParse, yymsp[0].minor.yy41, yymsp[-2].minor.yy322, yymsp[-1].minor.yy322, &yymsp…
168368 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168372 …yymsp[-3].minor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322…
168377 …1 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, yymsp[-1].minor.yy322, &yymsp[-4].minor.…
168379 yymsp[-4].minor.yy41 = yylhsminor.yy41;
168390 yylhsminor.yy41 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy41, 0, 0, &yymsp[-1].minor.yy0);
168392 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168401 …ylhsminor.yy41 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy394, yymsp[-1].minor.yy595.eType, yy…
168403 yymsp[-2].minor.yy41 = yylhsminor.yy41;
168407 …wAlloc(pParse, yymsp[-5].minor.yy394, yymsp[-3].minor.yy595.eType, yymsp[-3].minor.yy595.pExpr, yy…
168409 yymsp[-5].minor.yy41 = yylhsminor.yy41;
168419 {yylhsminor.yy595.eType = yymsp[-1].major; yylhsminor.yy595.pExpr = 0;}
168420 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168423 {yylhsminor.yy595.eType = yymsp[0].major; yylhsminor.yy595.pExpr = yymsp[-1].minor.yy528;}
168424 yymsp[-1].minor.yy595 = yylhsminor.yy595;
168430 {yymsp[-1].minor.yy516 = yymsp[0].minor.yy516;}
168434 {yymsp[-1].minor.yy516 = yymsp[-1].major; /*A-overwrites-X*/}
168437 {yymsp[0].minor.yy516 = yymsp[0].major; /*A-overwrites-X*/}
168440 { yymsp[-1].minor.yy41 = yymsp[0].minor.yy41; }
168445 yymsp[0].minor.yy41->pFilter = yymsp[-1].minor.yy528;
168447 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy528);
168451 yymsp[-1].minor.yy41 = yylhsminor.yy41;
168455 yylhsminor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168457 yylhsminor.yy41->eFrmType = TK_FILTER;
168458 yylhsminor.yy41->pFilter = yymsp[0].minor.yy528;
168460 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy528);
168467 yymsp[-3].minor.yy41 = yymsp[-1].minor.yy41;
168468 assert( yymsp[-3].minor.yy41!=0 );
168473 yymsp[-1].minor.yy41 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
168474 if( yymsp[-1].minor.yy41 ){
168475 …yymsp[-1].minor.yy41->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
168480 { yymsp[-4].minor.yy528 = yymsp[-1].minor.yy528; }
168562 yypParser->yytos = yymsp;
168563 yymsp->stateno = (YYACTIONTYPE)yyact;
168564 yymsp->major = (YYCODETYPE)yygoto;
168583 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
168631 yypParser->yyerrcnt = -1;
168633 assert( yypParser->yytos==yypParser->yystack );
168655 ** <li> An option argument of a grammar-specified type.
168679 assert( yypParser->yytos!=0 );
168684 yyact = yypParser->yytos->stateno;
168692 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
168698 assert( yypParser->yytos>=yypParser->yystack );
168699 assert( yyact==yypParser->yytos->stateno );
168702 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
168712 yypParser->yytos[yysize].stateno);
168726 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
168727 yypParser->yyhwm++;
168728 assert( yypParser->yyhwm ==
168729 (int)(yypParser->yytos - yypParser->yystack));
168733 if( yypParser->yytos>=yypParser->yystackEnd ){
168738 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
168750 yypParser->yyerrcnt--;
168754 yypParser->yytos--;
168788 if( yypParser->yyerrcnt<0 ){
168791 yymx = yypParser->yytos->major;
168802 while( yypParser->yytos > yypParser->yystack ){
168803 yyact = yy_find_reduce_action(yypParser->yytos->stateno,
168808 if( yypParser->yytos <= yypParser->yystack || yymajor==0 ){
168812 yypParser->yyerrcnt = -1;
168819 yypParser->yyerrcnt = 3;
168822 yyact = yypParser->yytos->stateno;
168837 ** * Report an error message, and throw away the input token.
168844 if( yypParser->yyerrcnt<=0 ){
168847 yypParser->yyerrcnt = 3;
168852 yypParser->yyerrcnt = -1;
168864 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
168865 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
168904 ** individual tokens and sends those tokens one-by-one over to the
168929 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
168934 #define CC_SLASH 16 /* '/'. / or c-style comment */
168993 ** lower-case ASCII equivalent. On ASCII machines, this is just
168994 ** an upper-to-lower case map. On EBCDIC machines we also need
169047 ** might be implemented more directly using a hand-written hash table.
169105 /* aKWHash[i] is the hash value for the i-th keyword */
169119 ** then the i-th keyword has no more hash collisions. Otherwise,
169120 ** the next keyword with the same hash is aKWHash[i]-1. */
169135 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
169151 ** the text for the i-th keyword. */
169166 /* aKWCode[i] is the parser symbol code for the i-th keyword */
169328 /* Check to see if z[0..n-1] is a keyword. If it is, write the
169335 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
169336 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
169530 ** For ASCII, any character with the high-order bit set is
169531 ** allowed in an identifier. For 7-bit characters,
169561 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
169603 ** impossible to call a window-function without a FILTER clause.
169650 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
169664 if( z[1]=='-' ){
169666 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
169702 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
169828 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
169944 void *pEngine; /* The LEMON-generated LALR(1) parser */
169947 int lastTokenParsed = -1; /* type of the previous token */
169948 sqlite3 *db = pParse->db; /* The database connection */
169952 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
169954 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
169957 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
169958 if( db->nVdbeActive==0 ){
169959 AtomicStore(&db->u1.isInterrupted, 0);
169961 pParse->rc = SQLITE_OK;
169962 pParse->zTail = zSql;
169964 if( db->flags & SQLITE_ParserTrace ){
169981 assert( pParse->pNewTable==0 );
169982 assert( pParse->pNewTrigger==0 );
169983 assert( pParse->nVar==0 );
169984 assert( pParse->pVList==0 );
169985 pParentParse = db->pParse;
169986 db->pParse = pParse;
169989 mxSqlLen -= n;
169991 pParse->rc = SQLITE_TOOBIG;
169992 pParse->nErr++;
170004 if( AtomicLoad(&db->u1.isInterrupted) ){
170005 pParse->rc = SQLITE_INTERRUPT;
170006 pParse->nErr++;
170043 pParse->sLastToken.z = zSql;
170044 pParse->sLastToken.n = n;
170045 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
170048 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
170049 if( pParse->rc!=SQLITE_OK ) break;
170064 if( db->mallocFailed ){
170065 pParse->rc = SQLITE_NOMEM_BKPT;
170067 if( pParse->zErrMsg || (pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE) ){
170068 if( pParse->zErrMsg==0 ){
170069 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
170071 sqlite3_log(pParse->rc, "%s in \"%s\"", pParse->zErrMsg, pParse->zTail);
170074 pParse->zTail = zSql;
170076 sqlite3_free(pParse->apVtabLock);
170079 if( pParse->pNewTable && !IN_SPECIAL_PARSE ){
170080 /* If the pParse->declareVtab flag is set, do not delete any table
170081 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
170084 sqlite3DeleteTable(db, pParse->pNewTable);
170086 if( pParse->pNewTrigger && !IN_RENAME_OBJECT ){
170087 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
170089 if( pParse->pVList ) sqlite3DbFreeNN(db, pParse->pVList);
170090 db->pParse = pParentParse;
170091 assert( nErr==0 || pParse->rc!=SQLITE_OK );
170102 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
170108 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
170120 int prevType = 0; /* Previous non-whitespace token */
170128 tokenType = -1;
170132 for(i=0; zSql[i] && pStr->accError==0; i+=n){
170160 iStartIN = pStr->nChar;
170168 assert( pStr->nChar>=(u32)iStartIN );
170169 pStr->nChar = iStartIN+1;
170173 nParen--;
170179 j = pStr->nChar;
170203 while( j<pStr->nChar ){
170204 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
170215 j = pStr->nChar;
170217 while( j<pStr->nChar ){
170218 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
170262 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
170291 ** (0) INVALID We have not yet seen a non-whitespace character.
170389 case '/': { /* C-style comments */
170401 case '-': { /* SQL-style comments from "--" to end of line */
170402 if( zSql[1]!='-' ){
170411 case '[': { /* Microsoft-style identifiers in [...] */
170418 case '`': /* Grave-accent quoted symbols used by MySQL */
170419 case '"': /* single- and double-quoted strings */
170480 zSql += nId-1;
170497 ** above, except that the parameter is required to be UTF-16 encoded, not
170498 ** UTF-8.
170510 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
170648 ** This is an extension initializer that is a no-op and always
170649 ** succeeds, except that it fails if the fault-simulation is set
170677 ** built-in extensions.
170717 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
170723 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
170728 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
170736 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
170741 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
170743 ** the SQLITE_THREADSAFE compile-time option being set to 0.
170749 ** this variable being set to non-zero will cause OSTRACE macros to emit
170796 ** This routine is a no-op except on its very first call for the process,
170812 ** * Calls to this routine from Y must block until the outer-most
170833 ** combination, the work-around is to set the correct pointer
170834 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
170838 ** to sqlite3_initialize() should be a no-op. But the initialization
170861 ** malloc subsystem - this implies that the allocation of a static
170898 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
170945 sqlite3GlobalConfig.nRefInitMutex--;
170962 u64 x = (((u64)1)<<63)-1;
170973 ** compile-time option.
170991 ** when this routine is invoked, then this routine is a harmless no-op.
171040 ** the SQLite library at run-time.
171061 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
171063 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
171064 ** Single-thread. */
171070 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
171072 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
171073 ** Multi-thread. */
171079 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
171081 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
171088 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
171095 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
171104 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
171107 ** low-level memory allocation routines to be used in place of the memory
171113 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
171122 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
171133 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
171134 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
171143 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
171155 /* no-op */
171165 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
171173 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
171184 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
171189 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
171190 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
171205 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
171215 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
171250 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
171251 ** can be changed at start-time using the
171256 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
171257 ** argument of type int. If non-zero, then URI handling is globally
171265 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
171283 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
171289 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
171290 ** negative, then that argument is changed to its compile-time default.
171292 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
171294 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
171295 ** compile-time option.
171307 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
171309 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
171369 int nBig; /* Number of full-size slots */
171370 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
171379 if( db->lookaside.bMalloced ){
171380 sqlite3_free(db->lookaside.pStart);
171385 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
171393 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
171402 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
171405 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
171414 db->lookaside.pStart = pStart;
171415 db->lookaside.pInit = 0;
171416 db->lookaside.pFree = 0;
171417 db->lookaside.sz = (u16)sz;
171418 db->lookaside.szTrue = (u16)sz;
171425 p->pNext = db->lookaside.pInit;
171426 db->lookaside.pInit = p;
171430 db->lookaside.pSmallInit = 0;
171431 db->lookaside.pSmallFree = 0;
171432 db->lookaside.pMiddle = p;
171434 p->pNext = db->lookaside.pSmallInit;
171435 db->lookaside.pSmallInit = p;
171440 db->lookaside.pEnd = p;
171441 db->lookaside.bDisable = 0;
171442 db->lookaside.bMalloced = pBuf==0 ?1:0;
171443 db->lookaside.nSlot = nBig+nSm;
171445 db->lookaside.pStart = db;
171447 db->lookaside.pSmallInit = 0;
171448 db->lookaside.pSmallFree = 0;
171449 db->lookaside.pMiddle = db;
171451 db->lookaside.pEnd = db;
171452 db->lookaside.bDisable = 1;
171453 db->lookaside.sz = 0;
171454 db->lookaside.bMalloced = 0;
171455 db->lookaside.nSlot = 0;
171472 return db->mutex;
171485 sqlite3_mutex_enter(db->mutex);
171487 for(i=0; i<db->nDb; i++){
171488 Btree *pBt = db->aDb[i].pBt;
171495 sqlite3_mutex_leave(db->mutex);
171500 ** Flush any dirty pages in the pager-cache for any attached database
171511 sqlite3_mutex_enter(db->mutex);
171513 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
171514 Btree *pBt = db->aDb[i].pBt;
171525 sqlite3_mutex_leave(db->mutex);
171538 /* IMP: R-06824-28531 */
171539 /* IMP: R-36257-52125 */
171540 db->aDb[0].zDbSName = va_arg(ap,char*);
171545 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
171546 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
171547 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
171575 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
171580 u64 oldFlags = db->flags;
171582 db->flags |= aFlagOp[i].mask;
171584 db->flags &= ~(u64)aFlagOp[i].mask;
171586 if( oldFlags!=db->flags ){
171590 *pRes = (db->flags & aFlagOp[i].mask)!=0;
171615 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
171621 rc = nKey1 - nKey2;
171637 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
171638 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
171643 ** Return true if CollSeq is the default built-in BINARY.
171646 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
171647 return p==0 || p->xCmp==binCollFunc;
171651 ** Another built-in collating sequence: NOCASE.
171657 ** At the moment there is only a UTF-8 implementation.
171668 r = nKey1-nKey2;
171683 return db->lastRowid;
171696 sqlite3_mutex_enter(db->mutex);
171697 db->lastRowid = iRowid;
171698 sqlite3_mutex_leave(db->mutex);
171711 return db->nChange;
171727 return db->nTotalChange;
171736 ** at the b-tree/pager level.
171739 while( db->pSavepoint ){
171740 Savepoint *pTmp = db->pSavepoint;
171741 db->pSavepoint = pTmp->pNext;
171744 db->nSavepoint = 0;
171745 db->nStatement = 0;
171746 db->isTransactionSavepoint = 0;
171757 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
171758 pDestructor = p->u.pDestructor;
171760 pDestructor->nRef--;
171761 if( pDestructor->nRef==0 ){
171762 pDestructor->xDestroy(pDestructor->pUserData);
171777 for(i=0; i<db->nDb; i++){
171778 Schema *pSchema = db->aDb[i].pSchema;
171780 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
171786 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
171788 if( pMod->pEpoTab ){
171789 sqlite3VtabDisconnect(db, pMod->pEpoTab);
171805 assert( sqlite3_mutex_held(db->mutex) );
171806 if( db->pVdbe ) return 1;
171807 for(j=0; j<db->nDb; j++){
171808 Btree *pBt = db->aDb[j].pBt;
171819 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
171820 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
171826 sqlite3_mutex_enter(db->mutex);
171827 if( db->mTrace & SQLITE_TRACE_CLOSE ){
171828 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
171836 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
171838 ** SQL statements below, as the v-table implementation may be storing
171849 sqlite3_mutex_leave(db->mutex);
171862 db->eOpenState = SQLITE_STATE_ZOMBIE;
171873 int iTxn = -1;
171877 return -1;
171880 sqlite3_mutex_enter(db->mutex);
171883 if( iDb<0 ) nDb--;
171886 nDb = db->nDb-1;
171889 Btree *pBt = db->aDb[iDb].pBt;
171893 sqlite3_mutex_leave(db->mutex);
171926 if( db->eOpenState!=SQLITE_STATE_ZOMBIE || connectionIsBusy(db) ){
171927 sqlite3_mutex_leave(db->mutex);
171939 ** they are reset. And that the required b-tree mutex is held to make
171947 for(j=0; j<db->nDb; j++){
171948 struct Db *pDb = &db->aDb[j];
171949 if( pDb->pBt ){
171950 sqlite3BtreeClose(pDb->pBt);
171951 pDb->pBt = 0;
171953 pDb->pSchema = 0;
171958 if( db->aDb[1].pSchema ){
171959 sqlite3SchemaClear(db->aDb[1].pSchema);
171965 assert( db->nDb<=2 );
171966 assert( db->aDb==db->aDbStatic );
171969 ** locks and does not require any further unlock-notify callbacks.
171973 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
171978 pNext = p->pNext;
171983 sqlite3HashClear(&db->aFunc);
171984 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
171994 sqlite3HashClear(&db->aCollSeq);
171996 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
172001 sqlite3HashClear(&db->aModule);
172005 sqlite3ValueFree(db->pErr);
172008 sqlite3_free(db->auth.zAuthUser);
172009 sqlite3_free(db->auth.zAuthPW);
172012 db->eOpenState = SQLITE_STATE_ERROR;
172014 /* The temp-database schema is allocated differently from the other schema
172020 sqlite3DbFree(db, db->aDb[1].pSchema);
172021 if( db->xAutovacDestr ){
172022 db->xAutovacDestr(db->pAutovacPagesArg);
172024 sqlite3_mutex_leave(db->mutex);
172025 db->eOpenState = SQLITE_STATE_CLOSED;
172026 sqlite3_mutex_free(db->mutex);
172028 if( db->lookaside.bMalloced ){
172029 sqlite3_free(db->lookaside.pStart);
172036 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
172045 assert( sqlite3_mutex_held(db->mutex) );
172048 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
172050 ** modified the database schema. If the b-tree mutexes are not taken
172051 ** here, then another shared-cache connection might sneak in between
172055 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
172057 for(i=0; i<db->nDb; i++){
172058 Btree *p = db->aDb[i].pBt;
172076 db->nDeferredCons = 0;
172077 db->nDeferredImmCons = 0;
172078 db->flags &= ~(u64)(SQLITE_DeferFKs|SQLITE_CorruptRdOnly);
172080 /* If one has been configured, invoke the rollback-hook callback */
172081 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
172082 db->xRollbackCallback(db->pRollbackArg);
172235 /* SQLITE_NOTICE */ "notification message",
172236 /* SQLITE_WARNING */ "warning message",
172269 ** Return non-zero to retry the lock. Return zero to stop trying
172285 int tmout = db->busyTimeout;
172293 delay = delays[NDELAY-1];
172294 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
172297 delay = tmout - prior;
172300 sqlite3OsSleep(db->pVfs, delay*1000);
172306 int tmout = ((sqlite3 *)ptr)->busyTimeout;
172310 sqlite3OsSleep(db->pVfs, 1000000);
172321 ** If this routine returns non-zero, the lock is retried. If it
172326 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
172327 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
172329 p->nBusy = -1;
172331 p->nBusy++;
172348 sqlite3_mutex_enter(db->mutex);
172349 db->busyHandler.xBusyHandler = xBusy;
172350 db->busyHandler.pBusyArg = pArg;
172351 db->busyHandler.nBusy = 0;
172352 db->busyTimeout = 0;
172353 sqlite3_mutex_leave(db->mutex);
172375 sqlite3_mutex_enter(db->mutex);
172377 db->xProgress = xProgress;
172378 db->nProgressOps = (unsigned)nOps;
172379 db->pProgressArg = pArg;
172381 db->xProgress = 0;
172382 db->nProgressOps = 0;
172383 db->pProgressArg = 0;
172385 sqlite3_mutex_leave(db->mutex);
172401 db->busyTimeout = ms;
172413 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->eOpenState!=SQLITE_STATE_ZOMBIE) ){
172418 AtomicStore(&db->u1.isInterrupted, 1);
172444 assert( sqlite3_mutex_held(db->mutex) );
172450 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
172514 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
172515 if( db->nVdbeActive ){
172517 "unable to delete/modify user-function due to active statements");
172518 assert( !db->mallocFailed );
172524 /* Trying to delete a function that does not exist. This is a no-op.
172530 assert(p || db->mallocFailed);
172540 pDestructor->nRef++;
172542 p->u.pDestructor = pDestructor;
172543 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
172544 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
172545 testcase( p->funcFlags & SQLITE_DIRECTONLY );
172546 p->xSFunc = xSFunc ? xSFunc : xStep;
172547 p->xFinalize = xFinal;
172548 p->xValue = xValue;
172549 p->xInverse = xInverse;
172550 p->pUserData = pUserData;
172551 p->nArg = (u16)nArg;
172556 ** Worker function used by utf-8 APIs that create new functions:
172583 sqlite3_mutex_enter(db->mutex);
172591 pArg->nRef = 0;
172592 pArg->xDestroy = xDestroy;
172593 pArg->pUserData = p;
172598 if( pArg && pArg->nRef==0 ){
172606 sqlite3_mutex_leave(db->mutex);
172673 sqlite3_mutex_enter(db->mutex);
172674 assert( !db->mallocFailed );
172675 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
172679 sqlite3_mutex_leave(db->mutex);
172687 ** fails with an error message stating that the function is used in the
172703 sqlite3_result_error(context, zErr, -1);
172711 ** this routine is a no-op. If the function does not exist, then create
172712 ** a new one that always throws a run-time error.
172728 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
172732 sqlite3_mutex_enter(db->mutex);
172734 sqlite3_mutex_leave(db->mutex);
172747 ** A NULL trace function means that no tracing is executes. A non-NULL
172761 sqlite3_mutex_enter(db->mutex);
172762 pOld = db->pTraceArg;
172763 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
172764 db->trace.xLegacy = xTrace;
172765 db->pTraceArg = pArg;
172766 sqlite3_mutex_leave(db->mutex);
172771 /* Register a trace callback using the version-2 interface.
172784 sqlite3_mutex_enter(db->mutex);
172787 db->mTrace = mTrace;
172788 db->trace.xV2 = xTrace;
172789 db->pTraceArg = pArg;
172790 sqlite3_mutex_leave(db->mutex);
172799 ** A NULL profile function means that no profiling is executes. A non-NULL
172816 sqlite3_mutex_enter(db->mutex);
172817 pOld = db->pProfileArg;
172818 db->xProfile = xProfile;
172819 db->pProfileArg = pArg;
172820 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
172821 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
172822 sqlite3_mutex_leave(db->mutex);
172830 ** If the invoked function returns non-zero, then the commit becomes a
172846 sqlite3_mutex_enter(db->mutex);
172847 pOld = db->pCommitArg;
172848 db->xCommitCallback = xCallback;
172849 db->pCommitArg = pArg;
172850 sqlite3_mutex_leave(db->mutex);
172871 sqlite3_mutex_enter(db->mutex);
172872 pRet = db->pUpdateArg;
172873 db->xUpdateCallback = xCallback;
172874 db->pUpdateArg = pArg;
172875 sqlite3_mutex_leave(db->mutex);
172896 sqlite3_mutex_enter(db->mutex);
172897 pRet = db->pRollbackArg;
172898 db->xRollbackCallback = xCallback;
172899 db->pRollbackArg = pArg;
172900 sqlite3_mutex_leave(db->mutex);
172916 sqlite3_mutex_enter(db->mutex);
172917 pRet = db->pPreUpdateArg;
172918 db->xPreUpdateCallback = xCallback;
172919 db->pPreUpdateArg = pArg;
172920 sqlite3_mutex_leave(db->mutex);
172941 sqlite3_mutex_enter(db->mutex);
172942 if( db->xAutovacDestr ){
172943 db->xAutovacDestr(db->pAutovacPagesArg);
172945 db->xAutovacPages = xCallback;
172946 db->pAutovacPagesArg = pArg;
172947 db->xAutovacDestr = xDestructor;
172948 sqlite3_mutex_leave(db->mutex);
173005 ** into the write-ahead-log by this database connection.
173020 sqlite3_mutex_enter(db->mutex);
173021 pRet = db->pWalArg;
173022 db->xWalCallback = xCallback;
173023 db->pWalArg = pArg;
173024 sqlite3_mutex_leave(db->mutex);
173051 /* Initialize the output variables to -1 in case an error occurs. */
173052 if( pnLog ) *pnLog = -1;
173053 if( pnCkpt ) *pnCkpt = -1;
173060 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
173065 sqlite3_mutex_enter(db->mutex);
173075 db->busyHandler.nBusy = 0;
173083 if( db->nVdbeActive==0 ){
173084 AtomicStore(&db->u1.isInterrupted, 0);
173087 sqlite3_mutex_leave(db->mutex);
173095 ** to contains a zero-length string, all attached databases are
173099 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
173106 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
173115 ** associated with the specific b-tree being checkpointed is taken by
173119 ** checkpointed. If an error is encountered it is returned immediately -
173130 assert( sqlite3_mutex_held(db->mutex) );
173131 assert( !pnLog || *pnLog==-1 );
173132 assert( !pnCkpt || *pnCkpt==-1 );
173136 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
173138 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
173153 ** This function returns true if main-memory should be used instead of
173155 ** The value returned depends on the value of db->temp_store (runtime
173160 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
173161 ** ----------------- -------------- ------------------------------
173173 return ( db->temp_store==2 );
173176 return ( db->temp_store!=1 );
173189 ** Return UTF-8 encoded English language explanation of the most recent
173200 sqlite3_mutex_enter(db->mutex);
173201 if( db->mallocFailed ){
173204 testcase( db->pErr==0 );
173205 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
173206 assert( !db->mallocFailed );
173208 z = sqlite3ErrStr(db->errCode);
173211 sqlite3_mutex_leave(db->mutex);
173219 int iOffset = -1;
173220 if( db && sqlite3SafetyCheckSickOrOk(db) && db->errCode ){
173221 sqlite3_mutex_enter(db->mutex);
173222 iOffset = db->errByteOffset;
173223 sqlite3_mutex_leave(db->mutex);
173230 ** Return UTF-16 encoded English language explanation of the most recent
173250 sqlite3_mutex_enter(db->mutex);
173251 if( db->mallocFailed ){
173254 z = sqlite3_value_text16(db->pErr);
173256 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
173257 z = sqlite3_value_text16(db->pErr);
173260 ** above. If this is the case, then the db->mallocFailed flag needs to
173262 ** sqlite3ApiExit(), to avoid setting the database handle error message.
173266 sqlite3_mutex_leave(db->mutex);
173279 if( !db || db->mallocFailed ){
173282 return db->errCode & db->errMask;
173288 if( !db || db->mallocFailed ){
173291 return db->errCode;
173294 return db ? db->iSysErrno : 0;
173321 assert( sqlite3_mutex_held(db->mutex) );
173339 ** are no active VMs, invalidate any pre-compiled statements.
173342 if( pColl && pColl->xCmp ){
173343 if( db->nVdbeActive ){
173353 ** Also, collation destructor - CollSeq.xDel() - function may need
173356 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
173357 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
173361 if( p->enc==pColl->enc ){
173362 if( p->xDel ){
173363 p->xDel(p->pUser);
173365 p->xCmp = 0;
173373 pColl->xCmp = xCompare;
173374 pColl->pUser = pCtx;
173375 pColl->xDel = xDel;
173376 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
173397 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
173442 ** If an invalid limit index is supplied, report -1.
173456 return -1;
173460 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
173461 ** there is a hard upper bound set at compile-time by a C preprocessor
173478 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
173482 return -1;
173484 oldLimit = db->aLimit[limitId];
173485 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
173487 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
173491 db->aLimit[limitId] = newLimit;
173493 return oldLimit; /* IMP: R-53341-35419 */
173497 ** This function is used to parse both URIs and non-URI filenames passed by the
173503 ** query parameter. The second argument contains the URI (or non-URI filename)
173519 ** message. It is the responsibility of the caller to eventually release
173524 const char *zUri, /* Nul-terminated URI to parse */
173528 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
173539 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
173540 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
173541 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
173550 ** method that there may be extra parameters following the file-name. */
173557 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
173580 iIn-7, &zUri[7]);
173593 ** 0: Parsing file-name.
173631 if( zFile[iOut-1]==0 ){
173633 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
173649 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
173775 ** is UTF-8 encoded.
173778 const char *zFilename, /* Database filename UTF-8 encoded */
173787 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
173846 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
173847 if( db->mutex==0 ){
173853 sqlite3MutexWarnOnContention(db->mutex);
173856 sqlite3_mutex_enter(db->mutex);
173857 db->errMask = (flags & SQLITE_OPEN_EXRESCODE)!=0 ? 0xffffffff : 0xff;
173858 db->nDb = 2;
173859 db->eOpenState = SQLITE_STATE_BUSY;
173860 db->aDb = db->aDbStatic;
173861 db->lookaside.bDisable = 1;
173862 db->lookaside.sz = 0;
173864 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
173865 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
173866 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
173867 db->autoCommit = 1;
173868 db->nextAutovac = -1;
173869 db->szMmap = sqlite3GlobalConfig.szMmap;
173870 db->nextPagesize = 0;
173871 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
173873 /* Beginning with version 3.37.0, using the VFS xFetch() API to memory-map
173877 ** SQLITE_TESTCTRL_SORTER_MMAP test-control at runtime. */
173878 db->nMaxSorterMmap = 0x7FFFFFFF;
173880 db->flags |= SQLITE_ShortColNames
173887 /* The SQLITE_DQS compile-time option determines the default settings
173891 ** ---------- ----------------------- -----------------------
173898 ** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
173900 ** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
173949 sqlite3HashInit(&db->aCollSeq);
173951 sqlite3HashInit(&db->aModule);
173954 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
173955 ** and UTF-16, so add a version for each to avoid any unnecessary
173958 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
173966 if( db->mallocFailed ){
173973 ** Throw an error if any non-sense combination is used. If we
173982 db->openFlags = flags;
173990 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
173992 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
174002 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
174011 sqlite3BtreeEnter(db->aDb[0].pBt);
174012 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
174013 if( !db->mallocFailed ){
174016 sqlite3BtreeLeave(db->aDb[0].pBt);
174017 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
174022 db->aDb[0].zDbSName = "main";
174023 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
174024 db->aDb[1].zDbSName = "temp";
174025 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
174027 db->eOpenState = SQLITE_STATE_OPEN;
174028 if( db->mallocFailed ){
174032 /* Register all built-in functions, but do not attempt to read the
174041 /* Load compiled-in extensions */
174046 /* Load automatic extensions - extensions that have been registered
174058 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
174061 db->mDbFlags |= DBFLAG_InternalFunc;
174064 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
174065 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
174069 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
174070 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
174076 /* Enable the lookaside-malloc subsystem */
174084 assert( db->mutex!=0 || isThreadsafe==0
174086 sqlite3_mutex_leave(db->mutex);
174094 db->eOpenState = SQLITE_STATE_SICK;
174120 const char *filename, /* Database filename (UTF-8) */
174136 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
174150 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
174197 sqlite3_mutex_enter(db->mutex);
174198 assert( !db->mallocFailed );
174201 sqlite3_mutex_leave(db->mutex);
174222 sqlite3_mutex_enter(db->mutex);
174223 assert( !db->mallocFailed );
174224 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
174230 sqlite3_mutex_leave(db->mutex);
174247 sqlite3_mutex_enter(db->mutex);
174248 db->xCollNeeded = xCollNeeded;
174249 db->xCollNeeded16 = 0;
174250 db->pCollNeededArg = pCollNeededArg;
174251 sqlite3_mutex_leave(db->mutex);
174268 sqlite3_mutex_enter(db->mutex);
174269 db->xCollNeeded = 0;
174270 db->xCollNeeded16 = xCollNeeded16;
174271 db->pCollNeededArg = pCollNeededArg;
174272 sqlite3_mutex_leave(db->mutex);
174300 return db->autoCommit;
174312 ** a low-level error is first detected.
174352 ** This is a convenience routine that makes sure that all thread-specific
174355 ** SQLite no longer uses thread-specific data so this routine is now a
174356 ** no-op. It is retained for historical compatibility.
174375 int *pAutoinc /* OUTPUT: True if column is auto-increment */
174396 sqlite3_mutex_enter(db->mutex);
174414 for(iCol=0; iCol<pTab->nCol; iCol++){
174415 pCol = &pTab->aCol[iCol];
174416 if( 0==sqlite3StrICmp(pCol->zCnName, zColumnName) ){
174420 if( iCol==pTab->nCol ){
174422 iCol = pTab->iPKey;
174423 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
174444 notnull = pCol->notNull!=0;
174445 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
174446 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
174477 sqlite3_mutex_leave(db->mutex);
174504 sqlite3_mutex_enter(db->mutex);
174505 db->errMask = onoff ? 0xffffffff : 0xff;
174506 sqlite3_mutex_leave(db->mutex);
174520 sqlite3_mutex_enter(db->mutex);
174550 int nSave = db->busyHandler.nBusy;
174552 db->busyHandler.nBusy = nSave;
174556 sqlite3_mutex_leave(db->mutex);
174592 ** Control the seed for the pseudo-random number generator (PRNG) that
174607 ** This test-control also resets the PRNG so that the new seed will
174615 assert( db==0 || db->aDb[0].pSchema!=0 );
174616 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
174627 ** is an array of integers that defines the test. Return -1 on a
174628 ** memory allocation error, 0 on success, or non-zero for an error.
174653 ** http://support.microsoft.com/kb/47961 <-- dead hyperlink
174654 ** Search at http://web.archive.org/ to find the 2015-03-16 archive
174706 ** This action provides a run-time test to see whether or not
174707 ** assert() was enabled at compile-time. If X is true and assert()
174716 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
174747 ** This action provides a run-time test to see how the ALWAYS and
174748 ** NEVER macros were defined at compile-time.
174753 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
174755 ** hard-coded to true or else it asserts if its argument is false.
174756 ** The first behavior (hard-coded to true) is the case if
174761 ** The run-time test procedure might look something like this:
174764 ** // ALWAYS() and NEVER() are no-op pass-through macros
174780 ** The integer returned reveals the byte-order of the computer on which
174783 ** 1 big-endian, determined at run-time
174784 ** 10 little-endian, determined at run-time
174785 ** 432101 big-endian, determined at compile-time
174786 ** 123410 little-endian, determined at compile-time
174804 db->dbOptFlags = va_arg(ap, u32);
174819 ** and return zero on success, or return non-zero on failure.
174839 db->mDbFlags ^= DBFLAG_InternalFunc;
174845 ** Set or clear a flag that indicates that the database file is always well-
174849 ** that demonstrat invariants on well-formed database files.
174862 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
174901 db->nMaxSorterMmap = va_arg(ap, int);
174920 ** or off. "tnum" is the root page of the b-tree to which the imposter
174934 sqlite3_mutex_enter(db->mutex);
174937 db->init.iDb = iDb;
174938 db->init.busy = db->init.imposterTable = va_arg(ap,int);
174939 db->init.newTnum = va_arg(ap,int);
174940 if( db->init.busy==0 && db->init.newTnum>0 ){
174944 sqlite3_mutex_leave(db->mutex);
174952 ** -DYYCOVERAGE) writes a report onto "out" that shows all
174966 ** This test-control causes the most recent sqlite3_result_int64() value
174970 ** test-control enables us to write an intreal() SQL function that can
174985 ** This test-control queries the seek-counter on the "main" database
174986 ** file. The seek-counter is written into *pnSeek and is then reset.
174987 ** The seek-count is only available if compiled with SQLITE_DEBUG.
174992 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
175044 ** of the id-th tuning parameter to *piValue. If "id" is between -1
175045 ** and -SQLITE_NTUNE, then write the current value of the (-id)-th
175054 ** checked-in code.
175061 }else if( id<0 && id>=-SQLITE_NTUNE ){
175062 *piValue = Tuning(-id);
175084 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
175085 zName--;
175136 assert( (sqlite3_int64)(p - pResult)==nByte );
175148 sqlite3_free(p - 4);
175176 while( ALWAYS(zFilename) && zFilename[0] && (N--)>0 ){
175193 ** Return a 64-bit integer value for a query parameter.
175214 ** passing free() a pointer that was not obtained from malloc() - it is
175247 return iDb<0 ? 0 : db->aDb[iDb].pBt;
175251 ** Return the name of the N-th database schema. Return NULL if N is out
175261 if( N<0 || N>=db->nDb ){
175264 return db->aDb[N].zDbSName;
175285 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
175293 return -1;
175297 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
175318 sqlite3_mutex_enter(db->mutex);
175320 if( db->autoCommit==0 ){
175323 Btree *pBt = db->aDb[iDb].pBt;
175333 sqlite3_mutex_leave(db->mutex);
175339 ** Open a read-transaction on the snapshot idendified by pSnapshot.
175354 sqlite3_mutex_enter(db->mutex);
175355 if( db->autoCommit==0 ){
175359 Btree *pBt = db->aDb[iDb].pBt;
175364 if( db->nVdbeActive==0 ){
175388 sqlite3_mutex_leave(db->mutex);
175408 sqlite3_mutex_enter(db->mutex);
175411 Btree *pBt = db->aDb[iDb].pBt;
175420 sqlite3_mutex_leave(db->mutex);
175435 ** Given the name of a compile-time option, return true if that option
175471 ** Return the N-th compile-time option string. If N is out of range,
175533 ** 1) Each entry in the list has a non-NULL value for either
175545 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
175550 assert( p->pUnlockConnection || p->pBlockingConnection );
175553 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
175554 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
175555 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
175556 assert( db==0 || p->pUnlockConnection!=db );
175557 assert( db==0 || p->pBlockingConnection!=db );
175567 ** db is not currently a part of the list, this function is a no-op.
175572 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
175574 *pp = (*pp)->pNextBlocked;
175589 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
175590 pp=&(*pp)->pNextBlocked
175592 db->pNextBlocked = *pp;
175614 ** Register an unlock-notify callback.
175619 ** cache. pOther is found by looking at db->pBlockingConnection.
175641 sqlite3_mutex_enter(db->mutex);
175646 db->pBlockingConnection = 0;
175647 db->pUnlockConnection = 0;
175648 db->xUnlockNotify = 0;
175649 db->pUnlockArg = 0;
175650 }else if( 0==db->pBlockingConnection ){
175659 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
175663 db->pUnlockConnection = db->pBlockingConnection;
175664 db->xUnlockNotify = xNotify;
175665 db->pUnlockArg = pArg;
175672 assert( !db->mallocFailed );
175674 sqlite3_mutex_leave(db->mutex);
175686 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
175689 db->pBlockingConnection = pBlocker;
175705 ** set to db, then invoke the configured unlock-notify callback and
175713 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
175723 /* This loop runs once for each entry in the blocked-connections list. */
175724 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
175728 if( p->pBlockingConnection==db ){
175729 p->pBlockingConnection = 0;
175733 if( p->pUnlockConnection==db ){
175734 assert( p->xUnlockNotify );
175735 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
175754 ** be passed to the unlock-notify callback is larger than the
175761 ** closed and the unlock-notify callbacks on blocked connections
175763 ** indefinitely for an unlock-notify callback that will never
175766 ** Instead, invoke the unlock-notify callback with the context
175769 ** requiring any dynamic allocation. This is sub-optimal because
175783 aArg[nArg++] = p->pUnlockArg;
175784 xUnlockNotify = p->xUnlockNotify;
175785 p->pUnlockConnection = 0;
175786 p->xUnlockNotify = 0;
175787 p->pUnlockArg = 0;
175791 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
175793 *pp = p->pNextBlocked;
175794 p->pNextBlocked = 0;
175796 pp = &p->pNextBlocked;
175834 ** This is an SQLite module implementing full-text search.
175847 /* The full-text index is stored in a series of b+tree (-like)
175856 ** The basic unit of encoding is a variable-length integer called a
175857 ** varint. We encode variable-length integers in little-endian order
175864 ** 7 bits - A
175865 ** 14 bits - BA
175866 ** 21 bits - BBA
175870 ** the encoding is not the same. SQLite varints are big-endian
175872 ** little-endian and can be up to 10 bytes in length (in theory).
175882 ** A doclist (document list) holds a docid-sorted list of hits for a
175888 ** FTS3 used to optionally store character offsets using a compile-time
175921 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
175924 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
175926 ** (72-2) and then terminates with the 0 at K.
175928 ** A "position-list" is the list of positions for multiple columns for
175929 ** a single docid. A "column-list" is the set of positions for a single
175930 ** column. Hence, a position-list consists of one or more column-lists,
175931 ** a document record consists of a docid followed by a position-list and
175935 ** array of varint-encoded docids.
175950 ** (further terms are delta-encoded)
175996 ** (further terms are delta-encoded)
176026 ** The segment directory in table %_segdir stores meta-information for
176038 ** The meta-information in the segment directory is:
176039 ** level - segment level (see below)
176040 ** idx - index within level
176041 ** - (level,idx uniquely identify a segment)
176042 ** start_block - first leaf node
176043 ** leaves_end_block - last leaf node
176044 ** end_block - last block (including interior nodes)
176045 ** root - contents of root node
176074 ** a tiny bit slower (perhaps due to more overhead in merge-time
176090 ** costs), and infrequent and non-existent terms still seem to be fast
176093 ** TODO(shess) That said, it would be nice to have a better query-side
176101 ** Since we're using a segmented structure, with no docid-oriented
176166 ** Defines the interface to tokenizers used by fulltext-search. There
176224 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
176271 ** normalized version of the token (i.e. after any case-folding and/or
176285 ** nul-terminated. This should either be fixed, or pInput/nBytes
176337 ** This is the header file for the generic hash-table implementation
176339 ** hash table implementation for the full-text indexing module.
176350 ** The internals of this structure are intended to be opaque -- client
176370 ** structure. All elements are stored on a single doubly-linked list.
176385 ** (including the null-terminator, if any). Case
176426 #define fts3HashFirst(H) ((H)->first)
176427 #define fts3HashNext(E) ((E)->next)
176428 #define fts3HashData(E) ((E)->data)
176429 #define fts3HashKey(E) ((E)->pKey)
176430 #define fts3HashKeysize(E) ((E)->nKey)
176435 #define fts3HashCount(H) ((H)->count)
176474 ** a collision when building an amalgamation with built-in FTS3.
176495 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
176497 ** as one or more b+-trees in the %_segments and %_segdir tables.
176499 ** It is possible to determine which index a b+-tree belongs to based on the
176501 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
176507 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
176514 ** make it a no-op.
176521 ** Terminator values for position-lists and column-lists.
176523 #define POS_COLUMN (1) /* Column-list terminator */
176524 #define POS_END (0) /* Position-list terminator */
176565 typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
176566 typedef short int i16; /* 2-byte (or larger) signed integer */
176567 typedef unsigned int u32; /* 4-byte unsigned integer */
176568 typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
176569 typedef sqlite3_int64 i64; /* 8-byte signed integer */
176595 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
176666 ** time must share a common language-id (see the FTS4 langid= feature).
176669 ** A single FTS4 table may have multiple full-text indexes. For each index
176703 ** by special insert command 'test-no-incr-doclist'. */
176713 # define MergeCount(P) ((P)->nMergeCount)
176736 char *aDoclist; /* List of docids for full-text queries */
176755 ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
176769 #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
176772 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
176774 ** above. The upper 16-bits contain a combination of the following
176775 ** bits, used to describe extra constraints on full-text searches.
176795 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
176808 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
176854 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
176922 #define FTS3_SEGCURSOR_PENDING -1
176923 #define FTS3_SEGCURSOR_ALL -2
176952 int nAdvance; /* How many seg-readers to advance */
177085 ** assert() conditions in the fts3 code are activated - conditions that are
177093 ** Write a 64-bit variable-length integer to memory starting at p[0].
177104 q[-1] &= 0x7f; /* turn off high bit in final byte */
177105 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
177106 return (int) (q - (unsigned char *)p);
177135 return (int)(p - pStart);
177139 ** Read a 64-bit variable-length integer from memory starting at p[0].
177148 ** Read a 64-bit variable-length integer from memory starting at p[0] and
177149 ** not extending past pEnd[-1].
177170 return (int)(p - pStart);
177175 ** a non-negative 32-bit integer before it is returned.
177211 ** Convert an SQL-style quoted string into a normal string by removing
177212 ** the quote characters. The conversion is done in-place. If the
177214 ** is a no-op.
177232 /* If the first byte was a '[', then the close-quote character is a ']' */
177261 ** varint that is part of a doclist (or position-list, or any other list
177278 ** clear on character p[-1]. */
177279 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
177294 assert( p->nPendingData==0 );
177295 assert( p->pSegments==0 );
177298 sqlite3_finalize(p->pSeekStmt);
177299 for(i=0; i<SizeofArray(p->aStmt); i++){
177300 sqlite3_finalize(p->aStmt[i]);
177302 sqlite3_free(p->zSegmentsTbl);
177303 sqlite3_free(p->zReadExprlist);
177304 sqlite3_free(p->zWriteExprlist);
177305 sqlite3_free(p->zContentTbl);
177306 sqlite3_free(p->zLanguageid);
177309 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
177316 ** Write an error message into *pzErr
177331 ** If *pRc is initially non-zero then this routine is a no-op.
177359 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
177360 sqlite3 *db = p->db; /* Database handle */
177369 zDb, p->zName,
177370 zDb, p->zName,
177371 zDb, p->zName,
177372 zDb, p->zName,
177373 (p->zContentTbl ? "--" : ""), zDb,p->zName
177389 ** If *pRc is non-zero when this function is called, it is a no-op.
177401 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
177402 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
177405 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
177406 for(i=1; zCols && i<p->nColumn; i++){
177407 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
177413 zCols, p->zName, zLanguageid
177418 rc = sqlite3_declare_vtab(p->db, zSql);
177431 fts3DbExec(pRc, p->db,
177434 p->zDb, p->zName
177436 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
177444 ** If the p->bHasDocsize boolean is true (indicating that this is an
177451 sqlite3 *db = p->db; /* The database connection */
177453 if( p->zContentTbl==0 ){
177454 const char *zLanguageid = p->zLanguageid;
177459 for(i=0; zContentCols && i<p->nColumn; i++){
177460 char *z = p->azColumn[i];
177471 p->zDb, p->zName, zContentCols
177479 p->zDb, p->zName
177491 p->zDb, p->zName
177493 if( p->bHasDocsize ){
177496 p->zDb, p->zName
177499 assert( p->bHasStat==p->bFts4 );
177500 if( p->bHasStat ){
177507 ** Store the current database page-size in bytes in p->nPgsz.
177509 ** If *pRc is non-zero when this function is called, it is a no-op.
177519 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
177523 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
177526 p->nPgsz = sqlite3_column_int(pStmt, 0);
177529 p->nPgsz = 1024;
177533 assert( p->nPgsz>0 || rc!=SQLITE_OK );
177560 *pnKey = (int)(zCsr-z);
177596 ** Return a copy of input string zInput enclosed in double-quotes (") and
177599 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
177634 ** table has the three user-defined columns "a", "b", and "c", the following
177642 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177653 if( p->zContentTbl==0 ){
177660 for(i=0; i<p->nColumn; i++){
177661 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
177663 if( p->zLanguageid ){
177669 for(i=0; i<p->nColumn; i++){
177670 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
177672 if( p->zLanguageid ){
177673 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
177677 p->zDb,
177678 (p->zContentTbl ? p->zContentTbl : p->zName),
177679 (p->zContentTbl ? "" : "_content")
177687 ** user-defined text column).
177692 ** user-defined text columns, the following string is returned:
177699 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
177716 for(i=0; i<p->nColumn; i++){
177719 if( p->zLanguageid ){
177727 ** Buffer z contains a positive integer value encoded as utf-8 text.
177735 iVal = iVal*10 + (z[i] - '0');
177736 if( iVal>0x7FFFFFFF ) return -1;
177743 ** This function interprets the string at (*pp) as a non-negative integer
177819 nIndex--;
177820 i--;