Lines Matching +full:quirk +full:- +full:frame +full:- +full:length +full:- +full:adjustment

17 ** language. The code for the "sqlite3" command-line shell is also in a
20 ** The content in this amalgamation comes from Fossil check-in
54 ** NO_TEST - The branches on this line are not
59 ** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
63 ** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
67 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
72 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
147 ** 2015-03-02
185 ** large file support, or if the OS is windows, these should be no-ops.
191 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
200 ** on 2008-11-28.) These days, all Linux kernels support large files, so
217 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
224 ** so the GCC_VERSION macro will be set to a correct non-zero value even
280 ** some MinGW-specific macros). When compiling for MinGW, either the
303 /* Optionally #include a user-defined header, whereby compilation options
321 ** 2001-09-15
332 ** presents to client programs. If a C-function, structure, datatype,
343 ** The official C-language API documentation for SQLite is derived
388 ** that require non-default calling conventions.
413 ** These no-op macros are used in front of interfaces to mark those
415 ** should not use deprecated interfaces - they are supported for backwards
439 ** CAPI3REF: Compile-Time Library Version Numbers
455 ** <a href="http://fossil-scm.org/">Fossil configuration management
457 ** a string which identifies a particular check-in of SQLite
459 ** string contains the date and time of the check-in (UTC) and a SHA1
460 ** or SHA3-256 hash of the entire source tree. If the source code has
470 #define SQLITE_SOURCE_ID "2025-06-28 14:00:48 2af157d77fb1304a74176eaee7fbc7c7e932d946bf25325e…
473 ** CAPI3REF: Run-Time Library Version Numbers
510 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
519 ** returning the N-th compile time option string. ^If N is out of range,
544 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
562 ** This interface only reports on the compile-time mutex setting
568 ** sqlite3_threadsafe() function shows only the compile-time setting of
569 ** thread safety, not any run-time changes to that setting made by
594 ** CAPI3REF: 64-Bit Integer Types
597 ** Because there is no cross-platform way to specify 64-bit integer types
598 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
605 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
628 ** substitute integer for floating-point.
670 ** argument is a harmless no-op.
683 ** CAPI3REF: One-Step Query Execution Interface
691 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
692 ** semicolon-separate SQL statements passed into its 2nd argument,
714 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
766 /* beginning-of-error-codes */
797 /* end-of-error-codes */
805 ** these result codes are too coarse-grained. They do not provide as
911 ** [sqlite3_open_v2()] has historically be a no-op and might become an
967 ** read-only media and cannot be changed even by processes with
978 ** property, SQLite is careful to only do full-page reads and write
979 ** on aligned pages, with the one exception that it will do a sub-page
1037 ** (Third-party VFS implementations might also make the distinction
1072 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1096 ** requested lock, then the call to xLock() is a no-op.
1099 ** to xUnlock() is a no-op.
1112 ** locking strategy (for example to use dot-file locks), to inquire
1160 ** fails to zero-fill short reads might seem to work. However,
1161 ** failure to zero-fill short reads will eventually lead to
1217 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1219 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1229 ** point to an integer (type int) containing the new chunk-size to use
1231 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1242 ** the [rollback journal] or the [write-ahead log]) for a particular database
1255 ** this file-control is NULL. However, if the database file is being synced
1256 ** as part of a multi-database commit, the argument points to a nul-terminated
1257 ** string containing the transactions super-journal file name. VFSes that
1274 ** anti-virus programs. By default, the windows VFS will retry file read,
1300 ** WAL mode. If the integer is -1, then it is overwritten with the current
1305 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1309 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1310 ** mode. If the integer is -1, then it is overwritten with the current
1311 ** zero-damage mode setting.
1322 ** final bottom-level VFS are written into memory obtained from
1326 ** all file-control actions, there is no guarantee that this will actually
1328 ** pointer in case this file-control is not implemented. This file-control
1332 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
1336 ** to a pointer to the top-level VFS.)^
1338 ** upper-most shim only.
1355 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1357 ** of the result string if the string is non-NULL.
1363 ** it is able to override built-in [PRAGMA] statements.
1367 ** file-control may be invoked by SQLite on the database file handle
1369 ** to the connection's busy-handler callback. The argument is of type (void**)
1370 ** - an array of two (void *) values. The first (void *) actually points
1372 ** busy-handler, this function should be invoked with the second (void *) in
1373 ** the array as the only argument. If it returns non-zero, then the operation
1378 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1388 ** maximum number of bytes that will be used for memory-mapped I/O.
1394 ** file-control is used internally to implement [PRAGMA mmap_size].
1400 ** The argument is a zero-terminated string. Higher layers in the
1402 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1423 ** The [SQLITE_FCNTL_NULL_IO] opcode sets the low-level file descriptor
1432 ** Applications should <em>not</em> use this file-control.
1481 ** The parameter is a pointer to a 32-bit signed integer that contains
1482 ** the value that M is to be set to. Before returning, the 32-bit signed
1493 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1519 ** file to the database file, but before the *-shm file is updated to
1524 ** whether or not there is a database client in another process with a wal-mode
1526 ** (void*) argument passed with this file-control should be a pointer to a
1530 ** the database is not a wal-mode db, or if there is no such connection in any
1540 ** database is not a temp db, then the [SQLITE_FCNTL_RESET_CACHE] file-control
1541 ** purges the contents of the in-memory page cache. If there is an open
1542 ** transaction, or if the db is a temp-db, this opcode is a no-op, not an error.
1622 ** as a normal, nul-terminated, UTF-8 buffer containing the filename, but
1659 ** structure used by this VFS. mxPathname is the maximum length of
1665 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
1683 ** consist of a single "-" character followed by no more than
1684 ** 11 alphanumeric and/or "-" characters.
1699 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1719 ** the open of a journal file a no-op. Writes to this journal would
1720 ** also be no-ops, and any attempt to read the journal would return
1722 ** file will be doing page-aligned sector reads and writes in a random
1761 ** flag is never actually used and is not implemented in the built-in
1764 ** non-zero error code if there is an I/O error or if the name of
1766 ** is returned, then non-zero or zero is written into *pResOut to indicate
1780 ** of good-quality randomness into zOut. The return value is
1788 ** a 24-hour day).
1811 int mxPathname; /* Maximum file pathname length */
1814 void *pAppData; /* Pointer to application-specific data */
1924 ** are harmless no-ops.)^
1929 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1952 ** compile-time option, then the automatic calls to sqlite3_initialize()
1961 ** The sqlite3_os_init() routine does operating-system specific
1977 ** (using the [SQLITE_OS_OTHER=1] compile-time
1979 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2017 ** implementation of an application-defined [sqlite3_os_init()].
2021 ** then this routine returns a non-zero [error code].
2035 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2048 ** and low-level memory allocation routines.
2060 ** Note that SQLite comes with several [built-in memory allocators]
2065 ** memory allocator that simulates memory out-of-memory conditions in
2132 ** [sqlite3_shutdown()] is a no-op that returns SQLITE_MISUSE.
2147 ** non-zero [error code] if a discontinued or unsupported configuration option
2153 ** [threading mode] to Single-thread. In other words, it disables
2156 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2158 ** value of Single-thread and so [sqlite3_config()] will return
2164 ** [threading mode] to Multi-thread. In other words, it disables
2168 ** are enabled so that SQLite will be safe to use in a multi-threaded
2171 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2172 ** it is not possible to set the Multi-thread [threading mode] and
2187 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2196 ** alternative low-level memory allocation routines to be used in place of
2224 ** disabled, the following SQLite interfaces become non-operational:
2245 ** This configuration option is a no-op if an application-defined page
2248 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2256 ** argument must be either a NULL pointer or a pointer to an 8-byte
2263 ** ^If pMem is NULL and N is non-zero, then each database connection
2266 ** of -1024*N bytes if N is negative, . ^If additional
2279 ** An 8-byte aligned pointer to the memory,
2286 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2294 ** The argument specifies alternative low-level mutex routines to be used
2298 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2311 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2325 ** The [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to change the
2326 ** default lookaside configuration at compile-time.
2347 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2349 ** passed through as the first parameter to the application-defined logger
2357 ** In a multi-threaded application, the application-defined logger
2362 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2379 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2380 ** if that compile-time option is omitted.
2390 ** They are retained for backwards compatibility but are now no-ops.
2396 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2412 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2419 ** compile-time maximum mmap size set by the
2420 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
2422 ** changed to its compile-time default.
2427 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
2428 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
2444 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
2453 ** becomes the [statement journal] spill-to-disk threshold.
2456 ** Or if the threshold is -1, statement journals are always held
2462 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
2467 ** of type (int) - the new value of the sorter-reference size threshold.
2471 ** of a table column that its values are likely to be very large - larger
2472 ** than the configured sorter-reference size threshold - then a reference
2478 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2483 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2486 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
2488 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2489 ** compile-time option is not set, then the default maximum is 1073741824.
2495 ** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2503 ** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2521 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2522 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2545 ** The [sqlite3_db_config()] interface is a var-args functions. It takes a
2550 ** page says "the N-th argument" it means "the N-th parameter past the
2551 ** configuration option" or "the (N+2)-th parameter to sqlite3_db_config()".
2557 ** non-zero [error code] if a discontinued or unsupported configuration option
2590 ** The buffer must be aligned to an 8-byte boundary.
2604 ** [-DSQLITE_DEFAULT_LOOKASIDE] option can be used to set the default lookaside
2605 ** configuration at compile-time. Typical values for lookaside are 1200 for
2635 ** triggers in the main database schema or in the schemas of [ATTACH]-ed
2652 ** views in the main database schema or in the schemas of ATTACH-ed
2659 ** [FTS3] full-text search engine extension.
2674 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2676 ** When the first argument to this interface is 1, then only the C-API is
2678 ** this interface is 0, then both the C-API and the SQL function are disabled.
2679 ** If the first argument is -1, then no changes are made to state of either the
2680 ** C-API or the SQL function.
2710 ** which is positive to disable checkpoints-on-close, or zero (the default)
2713 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
2714 ** have been disabled - 0 if they are not disabled, 1 if they are.
2737 ** behavior. The first parameter passed to this operation is an integer -
2741 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2799 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
2808 ** the legacy [double-quoted string literal] misfeature for DML statements
2810 ** default value of this setting is determined by the [-DSQLITE_DQS]
2811 ** compile-time option.
2817 ** the legacy [double-quoted string literal] misfeature for DDL statements,
2819 ** default value of this setting is determined by the [-DSQLITE_DQS]
2820 ** compile-time option.
2847 ** created database file to have a schema format version number (the 4-byte
2874 ** an integer.. The first argument is 1, 0, or -1 to enable, disable, or
2889 ** argument is 1, 0, or -1 to enable, disable, or leave unchanged the
2905 ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2906 ** leave unchanged the attach-create flag, respectively. If the second
2908 ** second argument points to depending on if the attach-create flag is set
2919 ** but the database will be opened read-only. If this option is disabled,
2924 ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2939 ** to an integer. The first argument is 1, 0, or -1 to enable, disable, or
2955 ** option is disabled. If the first argument is -1, then the option setting
3007 ** has a unique 64-bit signed
3092 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3114 ** any changes performed by sub-triggers, as the sqlite3_changes()
3115 ** value will be saved and restored after each sub-trigger has run.)^
3183 ** CAPI3REF: Interrupt A Long-Running Query
3189 ** or Ctrl-C where the user wants a long query operation to halt
3214 ** SQL statements is a no-op and has no effect on SQL statements
3227 ** These routines are useful during command-line input to determine if the
3233 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3248 ** then the return value from sqlite3_complete16() will be non-zero
3251 ** The input to [sqlite3_complete()] must be a zero-terminated
3252 ** UTF-8 string.
3254 ** The input to [sqlite3_complete16()] must be a zero-terminated
3255 ** UTF-16 string in native byte order.
3262 ** KEYWORDS: {busy-handler callback} {busy handler}
3284 ** ^If the callback returns non-zero, then another attempt
3351 ** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3352 ** not support blocking locks, this function is a no-op.
3355 ** -1 to this function requests that the VFS blocks for a long time -
3359 ** Internally, each SQLite database handle store two timeout values - the
3360 ** busy-timeout (used for rollback mode databases, or if the VFS does not
3361 ** support blocking locks) and the setlk-timeout (used for blocking locks
3362 ** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3363 ** values, this function sets only the setlk-timeout value. Therefore,
3364 ** to configure separate busy-timeout and setlk-timeout values for a single
3398 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3400 ** to zero-terminated strings that contain the names of the columns.
3402 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3414 ** -----------------------
3436 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3468 ** These routines are work-alikes of the "printf()" family of functions
3472 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3473 ** See the [built-in printf()] documentation for details.
3496 ** guarantees that the buffer is always zero-terminated. ^The first
3499 ** written will be n-1 characters.
3503 ** See also: [built-in printf()], [printf() SQL function]
3515 ** does not include operating-system specific [VFS] implementation. The
3519 ** of memory at least N bytes in length, where N is the parameter.
3526 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3527 ** of a signed 32-bit integer.
3532 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3537 ** might result if sqlite3_free() is called with a non-NULL pointer that
3557 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3558 ** of a 32-bit signed integer.
3573 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3597 ** routines, which form the built-in memory allocation subsystem.
3602 ** value of [sqlite3_memory_used()] since the high-water mark
3609 ** ^The memory high-water mark is reset to the current value of
3612 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3619 ** CAPI3REF: Pseudo-Random Number Generator
3621 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
3624 ** the built-in random() and randomblob() SQL functions. This interface allows
3635 ** non-NULL P then the pseudo-randomness is generated
3642 ** CAPI3REF: Compile-Time Authorization Callbacks
3673 ** to the callback are either NULL pointers or zero-terminated strings
3701 ** user-entered SQL is being [sqlite3_prepare | prepared] that
3720 ** statement might be re-prepared during [sqlite3_step()] due to a
3766 ** is the name of the inner-most trigger or view that is responsible for
3768 ** top-level SQL code.
3818 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3822 ** contain a UTF-8 SQL comment that identifies the trigger.)^
3824 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3825 ** the length of [bound parameter] expansion in the output of sqlite3_trace().
3829 ** the original statement text and an estimate of wall-clock time
3849 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3871 ** interface by using the X argument when X begins with "--" and invoking
3878 ** X argument points to a 64-bit integer which is approximately
3908 ** M argument should be the bitwise OR-ed combination of
3960 ** ^If the progress callback returns non-zero, the operation is
3984 ** filename argument. ^The filename argument is interpreted as UTF-8 for
3985 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3996 ** ^The default encoding will be UTF-8 for databases created using
3998 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
4012 ** <dd>The database is opened in read-only mode. If the database does
4020 ** read-write mode fails due to OS-level permissions, an attempt is
4021 ** made to open it in read-only mode. [sqlite3_db_readonly()] can be
4023 ** read-write.</dd>)^
4039 ** <dd>The database will be opened as an in-memory database. The database
4040 ** is named by the "filename" argument for the purposes of cache-sharing,
4045 ** <dd>The new database connection will use the "multi-thread"
4063 ** this option is a no-op.
4089 ** upon it. Note in particular that the SQLITE_OPEN_EXCLUSIVE flag is a no-op
4100 ** ^If the filename is ":memory:", then a private, temporary in-memory database
4101 ** is created for the connection. ^This in-memory database will vanish when
4109 ** on-disk database will be created. ^This private database will be
4119 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4142 ** SQLite and its built-in [VFSes] interpret the
4157 ** ^If "ro" is specified, then the database is opened for read-only
4160 ** "rw", then the database is opened for read-write (but not create)
4164 ** set to "memory" then a pure [in-memory database] that never reads
4191 ** read-only media. ^When immutable is set, SQLite assumes that the
4193 ** privilege, and so the database is opened read-only and all locking
4218 ** <tr><td style="white-space:nowrap">
4222 ** necessary - space characters can be used literally
4225 ** Open file "data.db" in the current directory for read-only access.
4226 ** Regardless of whether or not shared-cache mode is enabled by
4228 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4229 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4230 ** that uses dot-files in place of posix advisory locking.
4238 ** percent sign - "%" - followed by exactly two hexadecimal digits
4240 ** URI filename are interpreted, they are encoded using UTF-8 and all
4242 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4246 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4248 ** characters must be converted to UTF-8 prior to passing them into
4258 const char *filename, /* Database filename (UTF-8) */
4262 const void *filename, /* Database filename (UTF-16) */
4266 const char *filename, /* Database filename (UTF-8) */
4303 ** case or if the value begins with a non-zero number. The
4311 ** 64-bit signed integer and returns that integer, or D if P does not
4316 ** the value) of the N-th query parameter for filename F, or a NULL
4318 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4377 ** ^If X is the name of a rollback or WAL-mode journal file that is
4383 ** only. It is not a general-purpose interface.
4427 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4463 ** change the value of the error code. The error-code preserving
4474 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4475 ** text that describes the error, as either UTF-8 or UTF-16 respectively,
4483 ** ^The sqlite3_errstr(E) interface returns the English-language text
4484 ** that describes the [result code] E, as UTF-8, or NULL if E is not an
4494 ** SQL, then the sqlite3_error_offset() function returns -1.
4529 ** The life-cycle of a prepared statement object usually goes like this:
4544 ** CAPI3REF: Run-time Limits
4557 ** set at compile-time by a C preprocessor macro called
4566 ** simply invoke this interface with the third parameter set to -1.
4568 ** Run-time limits are intended for use in applications that manage
4581 ** New run-time limit categories may be added in future releases.
4586 ** CAPI3REF: Run-Time Limit Categories
4590 ** that can be lowered at run-time using [sqlite3_limit()].
4599 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
4626 ** <dd>The maximum length of the pattern argument to the [LIKE] or
4676 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
4692 ** compiles to see if some SQL syntax is well-formed, without generating
4710 ** To execute an SQL statement, it must first be compiled into a byte-code
4719 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
4720 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
4721 ** as a convenience. The UTF-16 interfaces work by converting the
4722 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
4729 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
4731 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
4732 ** and sqlite3_prepare16_v3() use UTF-16.
4740 ** If the caller knows that the supplied string is nul-terminated, then
4743 ** the nul-terminator.
4744 ** Note that nByte measure the length of the input in bytes, not
4745 ** characters, even for the UTF-16 interfaces.
4795 ** ^The specific value of a WHERE-clause [parameter] might influence the
4796 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
4798 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4810 const char *zSql, /* SQL statement, UTF-8 encoded */
4811 int nByte, /* Maximum length of zSql in bytes. */
4817 const char *zSql, /* SQL statement, UTF-8 encoded */
4818 int nByte, /* Maximum length of zSql in bytes. */
4824 const char *zSql, /* SQL statement, UTF-8 encoded */
4825 int nByte, /* Maximum length of zSql in bytes. */
4832 const void *zSql, /* SQL statement, UTF-16 encoded */
4833 int nByte, /* Maximum length of zSql in bytes. */
4839 const void *zSql, /* SQL statement, UTF-16 encoded */
4840 int nByte, /* Maximum length of zSql in bytes. */
4846 const void *zSql, /* SQL statement, UTF-16 encoded */
4847 int nByte, /* Maximum length of zSql in bytes. */
4857 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4861 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4864 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4878 ** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4880 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4881 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
4892 ** the [SQLITE_ENABLE_NORMALIZE] compile-time option is defined.
4904 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4908 ** Note that [application-defined SQL functions] or
4912 ** change the database file through side-effects:
4938 ** makes it a no-op, but the sqlite3_stmt_readonly() result would still
4940 ** read-only no-op if the table already exists, but
5000 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5037 ** sqlite3_value object. If SQLite is compiled to be single-threaded
5048 ** implementation of [application-defined SQL functions] are protected.
5066 ** is always first parameter to [application-defined SQL functions].
5067 ** The application-defined SQL function implementation will pass this
5117 ** it should be a pointer to well-formed UTF8 text.
5119 ** it should be a pointer to well-formed UTF16 text.
5121 ** it should be a pointer to a well-formed unicode string that is
5125 ** [[byte-order determination rules]] ^The byte-order of
5126 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5139 ** is negative, then the length of the string is
5143 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5177 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
5183 ** ^A negative value for the zeroblob results in a zero-length BLOB.
5185 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5257 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5268 ** ^If the value N is out of range or if the N-th parameter is
5270 ** always in UTF-8 encoding even if the named parameter was
5271 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5288 ** name must be given in UTF-8 even if the original statement
5289 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5330 ** interface returns a pointer to a zero-terminated UTF-8 string
5331 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5332 ** UTF-16 string. ^The first parameter is the [prepared statement]
5343 ** (for example during a conversion from UTF-8 to UTF-16) then a
5362 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5371 ** ^The names returned are the original un-aliased names of the
5377 ** ^The left-most column is column 0 for these routines.
5386 ** UTF-16 encoded strings and the other functions return UTF-8.
5389 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5413 ** ^The returned string is always UTF-8 encoded.
5426 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5476 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5502 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5532 ** will return non-zero if previous call to [sqlite3_step](P) returned
5534 ** where it always returns zero since each step of that multi-step
5548 ** <li> 64-bit signed integer
5549 ** <li> 64-bit IEEE floating point number
5582 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5583 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5584 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5585 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5590 ** or a UTF-8 TEXT result in bytes
5592 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5644 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5646 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5647 ** the string to UTF-8 and then returns the number of bytes.
5649 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5653 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5655 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5656 ** the string to UTF-16 and then returns the number of bytes.
5658 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5669 ** even empty strings, are always zero-terminated. ^The return
5670 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5686 ** [application-defined SQL functions] or [virtual tables], not within
5687 ** top-level application code.
5705 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5726 ** sqlite3_column_text16() is called. A zero-terminator might
5728 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5730 ** to UTF-16.</li>
5731 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5733 ** to UTF-8.</li>
5736 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
5767 ** fail if an out-of-memory error occurs during a format conversion.
5768 ** Only the following subset of interfaces are subject to out-of-memory
5779 ** If an out-of-memory error occurs, then the return value from these
5781 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
5815 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5830 ** object back to its initial state, ready to be re-executed.
5882 ** connection then application-defined SQL functions must be added
5886 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
5887 ** representation, exclusive of the zero-terminator. ^Note that the name
5888 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5894 ** aggregate takes. ^If this parameter is -1, then the SQL function or
5897 ** parameter is less than -1 or greater than 127 then the behavior is
5916 ** deterministic. The built-in [random()] SQL function is an example of a
5927 ** all application-defined SQL functions that do not need to be
5941 ** pointers to C-language functions that implement the SQL function or
5951 ** C-language callbacks that implement the new function. xStep and xFinal
5952 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
5954 ** non-NULL, in which case the new function may be used as either an aggregate
5957 ** [user-defined window functions|available here].
5972 ** SQL function is used. ^A function implementation with a non-negative
5981 ** ^Built-in functions may be overloaded by new application-defined functions.
5983 ** ^An application-defined function is permitted to call other
6038 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
6039 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
6040 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
6067 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
6072 ** [application-defined SQL function]
6073 ** that has side-effects or that could potentially leak sensitive information.
6076 ** modified to invoke the application-defined function in ways that are
6080 ** [application-defined SQL functions], regardless of whether or not they
6084 ** that do not have access to the application-defined functions.
6103 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
6105 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
6107 ** security-adverse side-effects and information-leaks.
6112 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6113 ** This flag instructs SQLite to omit some corner-case optimizations that
6119 ** a non-zero subtype was specified by the function argument expression.
6123 ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
6127 ** might become a no-op if the function is used as term in an
6136 ** ordered-set aggregate SQL notation with a single ORDER BY term can be
6137 ** used to invoke this function. If the ordered-set aggregate notation is
6139 ** that the ordered-set aggregate syntax is only available if SQLite is
6140 ** built using the -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES compile-time option.
6179 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6180 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6182 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6183 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6185 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6186 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6189 ** or a UTF-8 TEXT in bytes
6191 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6209 ** implement [application-defined SQL functions] and [virtual tables].
6219 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6220 ** in the native byte-order of the host machine. ^The
6222 ** extract UTF-16 strings as big-endian and little-endian respectively.
6263 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6278 ** fail if an out-of-memory error occurs during a format conversion.
6279 ** Only the following subset of interfaces are subject to out-of-memory
6292 ** If an out-of-memory error occurs, then the return value from these
6294 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6342 ** an [application-defined SQL function] argument V. The subtype
6347 ** Every [application-defined SQL function] that invokes this interface
6368 ** then sqlite3_value_free(V) is a harmless no-op.
6429 ** the application-defined function is running.
6449 ** These functions may be used by (non-aggregate) SQL functions to
6454 ** regular-expression matching function. The compiled version of the regular
6462 ** value to the application-defined function. ^N is zero for the left-most
6468 ** N-th argument of the application-defined function. ^Subsequent
6494 ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6500 ** function parameters that are compile-time constants, including literal
6503 ** The value of the N parameter to these interfaces should be non-negative.
6528 ** If P and X are both non-NULL, then the destructor X is invoked with
6531 ** <li> An out-of-memory error occurs during the call to
6587 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6603 ** an application-defined function to be the BLOB whose content is pointed
6608 ** interfaces set the result of the application-defined function to be
6612 ** an application-defined function to be a floating point value specified
6620 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6621 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6622 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6627 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6645 ** of the application-defined function to be the 32-bit signed integer
6648 ** of the application-defined function to be the 64-bit signed integer
6652 ** of the application-defined function to be NULL.
6656 ** set the return value of the application-defined function to be
6657 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6658 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6660 ** application-defined function to be a text string in an encoding
6667 ** the string length itself by searching the 2nd parameter for the first
6670 ** is non-negative, then as many bytes (not characters) of the text
6671 ** pointed to by the 2nd parameter are taken as the application-defined
6672 ** function result. If the 3rd parameter is non-negative, then it
6679 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6695 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6697 ** byte-order specified by the BOM. ^The byte-order specified by
6698 ** the BOM at the beginning of the text overrides the byte-order
6701 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6712 ** the application-defined function to be a copy of the
6723 ** also associates the host-language pointer P or type T with that
6725 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6733 ** than the one containing the application-defined function that received
6765 ** the result from the [application-defined SQL function] with
6772 ** Every [application-defined SQL function] that invokes this interface
6780 ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6783 ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6795 ** ^The name of the collation is a UTF-8 string
6797 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6830 ** function callback are the length of the two strings, in bytes. The collating
6860 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
6903 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
6904 ** the names are passed as UTF-16 in machine native byte order.
6905 ** ^A call to either function replaces the existing collation-needed callback.
6971 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
7013 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
7014 ** &nbsp; TemporaryFolder->Path->Data();
7017 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
7030 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
7069 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
7074 ** the current directory on the sub-platforms of Win32 where that concept is
7078 ** UTF-8 or UTF-16, respectively.
7097 ** CAPI3REF: Test For Auto-Commit Mode
7101 ** ^The sqlite3_get_autocommit() interface returns non-zero or
7105 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
7107 ** If certain kinds of errors occur on a statement within a multi-statement
7138 ** for the N-th database on database connection D, or a NULL pointer if N is
7140 ** the "temp" schema. Larger values of N correspond to various ATTACH-ed
7148 ** remember the string long-term should make their own copy. Applications that
7150 ** threads should mutex-protect calls to this API and should make their own
7162 ** connection D, or if database N is a temporary or in-memory database, then
7167 ** is [DETACH]-ed or until the database connection closes.
7188 ** CAPI3REF: Determine if a database is read-only
7192 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7211 ** a valid schema, then -1 is returned.
7277 ** ^If the callback on a commit hook function returns non-zero,
7299 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7301 ** hook returning non-zero, just as it would be with any other rollback.
7321 ** the schema-name of the attached database that is being autovacuumed,
7329 ** <p>^If there are multiple ATTACH-ed database files that are being
7452 ** [-DSQLITE_OMIT_SHARED_CACHE]. The [-DSQLITE_OMIT_SHARED_CACHE]
7453 ** compile-time option is recommended because the
7479 ** shared cache mode should be enabled per-database connection via
7483 ** 32-bit integer is atomic.
7485 ** See Also: [SQLite Shared-Cache Mode]
7493 ** of heap memory by deallocating non-essential memory allocations
7495 ** pages to improve performance is an example of non-essential memory.
7498 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7512 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7547 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7571 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7572 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7605 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7609 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7676 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7688 ** with various operating-system specific extensions added.
7697 ** X consists of the lower-case equivalent of all ASCII alphabetic
7736 ** [extension loading] while evaluating user-entered SQL, the following API
7744 ** ^This interface enables or disables both the C-API
7747 ** to enable or disable only the C-API.)^
7787 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7889 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7901 ** expr on the right-hand side can be evaluated (and thus the constraint
7916 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7922 ** non-zero.
7926 ** the right-hand side of the corresponding aConstraint[] is evaluated
7927 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7989 int iColumn; /* Column constrained. -1 for ROWID */
7992 int iTermOffset; /* Used internally - xBestIndex should ignore */
8036 ** ^The left-hand operand of the operator is given by the corresponding
8037 ** aConstraint[].iColumn field. ^An iColumn of -1 indicates the left-hand
8040 ** operators have no left-hand operand, and so for those operators the
8049 ** The right-hand operands for each constraint might be accessible using
8050 ** the [sqlite3_vtab_rhs_value()] interface. Usually the right-hand
8052 ** in the input SQL. If the right-hand operand is another column or an
8056 ** SQLITE_INDEX_CONSTRAINT_ISNOTNULL operators have no right-hand operand
8061 ** the [sqlite3_vtab_collation()] interface. For most real-world virtual
8256 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
8258 ** read-only access.
8294 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8310 ** and the built-in [zeroblob] SQL function may be used to create a
8311 ** zero-filled blob to read or write using the incremental-blob interface.
8341 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8363 ** ^If the blob handle being closed was opened for read-write access, and if
8364 ** the database is in auto-commit mode and there are no other open read-write
8372 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8399 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8428 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8477 ** ^Names are zero-terminated UTF-8 strings.
8508 ** is selected automatically at compile-time. The following
8519 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8524 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8561 ** cases where it really needs one. If a faster non-recursive mutex
8610 ** then any of the four routines behaves as a no-op.
8623 ** An instance of this structure defines the low-level routines
8673 ** xMutexInit() must be no-ops.
8719 ** the routine should return 1. This seems counter-intuitive since
8723 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8739 ** next. Applications that override the built-in mutex logic must be
8755 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8770 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8776 ** CAPI3REF: Low-Level Control Of Database Files
8892 ** by enclosing in double-quotes) so as not to confuse the parser.
8897 ** The sqlite3_keyword_name(N,Z,L) interface finds the 0-based N-th keyword and
8900 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8906 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8918 ** <li> Put all identifier names inside double-quotes. This is the official
8929 ** compile-time options. For example, "VACUUM" is not a keyword if
8930 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8941 ** An instance of the sqlite3_str object contains a dynamically-sized
8965 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8976 ** length of the string contained in the [sqlite3_str] object will be
9005 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
9010 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
9011 ** S must contain at least N non-zero bytes of content. To append a
9012 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
9016 ** zero-terminated string S onto the end of [sqlite3_str] object X.
9019 ** single-byte character C onto the end of [sqlite3_str] object X.
9023 ** inside [sqlite3_str] object X back to zero bytes in length.
9045 ** [SQLITE_NOMEM] following any out-of-memory error, or
9049 ** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
9051 ** ^The length returned by [sqlite3_str_length(X)] does not include the
9052 ** zero-termination byte.
9087 ** SQLITE_OK on success and a non-zero [error code] on failure.
9090 ** be represented by a 32-bit integer, then the values returned by
9108 ** These integer constants designate various run-time status parameters
9116 ** and internal memory usage by the SQLite library. Auxiliary page-cache
9201 ** non-zero [error code] on failure.
9217 ** The [sqlite3_db_status()] interface will return a non-zero error code
9227 ** satisfied using lookaside memory. Only the high-water value is meaningful;
9235 ** Only the high-water value is meaningful;
9243 ** Only the high-water value is meaningful;
9267 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9308 ** to disk all at once. When pages spill mid-transaction, that introduces
9379 ** A non-zero value in this counter may indicate an opportunity to
9385 ** A non-zero value in this counter may indicate an opportunity to
9414 ** step was bypassed because a Bloom filter returned not-found. The
9484 ** The built-in page cache is recommended for most uses.
9499 ** built-in default page cache is used instead of the application defined
9531 ** false if it is used for an in-memory database. The cache implementation
9542 ** suggested maximum cache-size (number of pages stored by) the cache
9588 ** as its second argument. If the third parameter, discard, is non-zero,
9622 ** is not obligated to free any memory, but well-behaved implementations should
9681 ** for copying in-memory databases to or from persistent files.
9687 ** ^The source database is read-locked only while it is being read;
9721 ** there is already a read or read-write transaction open on the
9753 ** <li> the destination database was opened read-only, or
9754 ** <li> the destination database is using write-ahead-log journaling
9756 ** <li> the destination database is an in-memory database and the
9760 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9761 ** the [sqlite3_busy_handler | busy-handler function]
9763 ** busy-handler returns non-zero before the lock is available, then
9784 ** sqlite3_backup_step(), the source database may be modified mid-way
9801 ** active write-transaction on the destination database is rolled back.
9808 ** ^If an out-of-memory condition or IO error occurred during any prior
9887 ** ^When running in shared-cache mode, a database operation may fail with
9888 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9889 ** individual tables within the shared-cache cannot be obtained. See
9890 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9894 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9898 ** ^Shared-cache locks are released when a database connection concludes
9902 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9912 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9918 ** ^If the blocked connection is attempting to obtain a write-lock on a
9919 ** shared-cache table, and more than one other connection currently holds
9920 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9923 ** ^(There may be at most one unlock-notify callback registered by a
9925 ** blocked connection already has a registered unlock-notify callback,
9928 ** unlock-notify callback is canceled. ^The blocked connections
9929 ** unlock-notify callback may also be canceled by closing the blocked
9932 ** The unlock-notify callback is not reentrant. If an application invokes
9933 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9941 ** When an unlock-notify callback is registered, the application provides a
9945 ** an unlock-notify callback is a pointer to an array of void* pointers,
9949 ** more than one blocked connection that has registered for an unlock-notify
9959 ** Assuming that after registering for an unlock-notify callback a
9970 ** unlock-notify callback is registered. The system is said to be in
9971 ** a deadlocked state if connection A has registered for an unlock-notify
9973 ** B has itself registered for an unlock-notify callback when connection
9976 ** registered for an unlock-notify callback on the conclusion of connection
9988 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9989 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
10009 ** and extensions to compare the contents of two buffers containing UTF-8
10010 ** strings in a case-independent fashion, using the same definition of "case
10026 ** Note that this routine returns zero on a match and non-zero if the strings
10043 ** insensitive - equivalent upper and lower case ASCII characters match
10049 ** Note that this routine returns zero on a match and non-zero if the strings
10073 ** a fixed-length buffer on the stack. If the log message is longer than
10074 ** a few hundred characters, it will be truncated to the length of the
10080 ** CAPI3REF: Write-Ahead Log Commit Hook
10087 ** the associated write-lock on the database released)^, so the implementation
10093 ** ^The third parameter is the name of the database that was written to -
10094 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
10095 ** is the number of pages currently in the write-ahead log file,
10106 ** A single database handle may have at most a single write-ahead log callback
10108 ** previously registered write-ahead log callback. ^The return value is
10121 ** CAPI3REF: Configure an auto-checkpoint
10128 ** more frames in the [write-ahead log] file. ^Passing zero or
10143 ** ^Every new [database connection] defaults to having the auto-checkpoint
10159 ** [write-ahead log] for database X on [database connection] D to be
10160 ** transferred into the database file and for the write-ahead log to
10186 ** in the log were checkpointed. ^The [busy-handler callback]
10193 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
10202 ** [busy-handler callback])
10215 ** the log file or to -1 if the checkpoint could not run because
10219 ** was called) or to -1 if the checkpoint could not run due to an error or
10227 ** busy-handler configured, it will not be invoked in this case.
10231 ** obtained immediately, and a busy-handler is configured, it is invoked and
10232 ** the writer lock retried until either the busy-handler returns 0 or the lock
10233 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
10234 ** database readers as described above. ^If the busy-handler returns 0 before
10237 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10240 ** ^If parameter zDb is NULL or points to a zero length string, then the
10253 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10254 ** zDb is not NULL (or a zero length string) and is not the name of any
10329 ** If X is non-zero, then the virtual table implementation guarantees
10373 ** all schemas ("main", "temp", and any ATTACH-ed databases) whenever the
10404 ** [xUpdate] method understands as a "no-change" value.
10566 ** on the right-hand side of the IN operator.)^ Thus the virtual table
10567 ** only sees a single value from the right-hand side of the IN operator
10571 ** table to see all values on the right-hand of the IN operator all at
10576 ** ^A call to sqlite3_vtab_in(P,N,-1) will return true (non-zero)
10577 ** if and only if the [sqlite3_index_info|P->aConstraint][N] constraint
10579 ** sqlite3_vtab_in() with -1 in the third argument is a mechanism
10580 ** by which the virtual table can ask SQLite if all-at-once processing
10586 ** the IN operator all-at-once, respectively. ^Thus when the third
10587 ** parameter (F) is non-negative, this interface is the mechanism by
10596 ** (non-zero), that means that the constraint is an IN operator
10597 ** that can be processed all-at-once. ^If the constraint is not an IN
10598 ** operator or cannot be processed all-at-once, then the interface returns
10601 ** ^(All-at-once processing of the IN operator is selected if both of the
10605 ** <li><p> The P->aConstraintUsage[N].argvIndex value is set to a positive
10607 ** use the N-th constraint.
10610 ** non-negative had F>=1.
10614 ** the traditional one-at-a-time processing strategy for the IN constraint.
10615 ** ^If both conditions are true, then the argvIndex-th parameter to the
10618 ** [sqlite3_vtab_in_next()] to find all values on the right-hand side
10624 ** CAPI3REF: Find all elements on the right-hand side of an IN constraint.
10634 ** a parameter that was previously selected for all-at-once IN constraint
10637 ** an xFilter argument that was selected for all-at-once IN constraint
10640 ** ^(Use these routines to access all values on the right-hand side
10682 ** J being a 0-based index into P->aConstraint[], then this routine
10683 ** attempts to set *V to the value of the right-hand operand of
10684 ** that constraint if the right-hand operand is known. ^If the
10685 ** right-hand operand is not known, then *V is set to a NULL pointer.
10688 ** inteface returns SQLITE_NOTFOUND if the right-hand side of the J-th
10694 ** the right-hand operand of a constraint is a literal value in the original
10695 ** SQL statement. If the right-hand operand is an expression or a reference
10700 ** [SQLITE_INDEX_CONSTRAINT_ISNOTNULL], have no right-hand operand. For such
10709 ** "Right-Hand Side".
10744 ** not available, the output variable is set to -1 if the value is numeric,
10750 ** set to the total number of times that the X-th loop has run.</dd>
10754 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10759 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10766 ** to a zero-terminated UTF-8 string containing the name of the index or table
10767 ** used for the X-th loop.
10771 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10772 ** description for the X-th loop.
10776 ** id for the X-th query plan element. The id value is unique within the
10777 ** statement. The select-id is the same value as is output in the first
10788 ** according to the processor time-stamp counter, that elapsed while the
10790 ** all query elements - if it is unavailable the output variable is
10791 ** set to -1.
10814 ** compile-time option.
10822 ** one flag is defined - SQLITE_SCANSTAT_COMPLEX. If SQLITE_SCANSTAT_COMPLEX
10832 ** for. Query elements are numbered starting from zero. A value of -1 may be
10834 ** - less than -1 or greater than or equal to the total number of query
10835 ** elements used to implement the statement - a non-zero value is returned and
10861 ** CAPI3REF: Zero Scan-Status Counters
10866 ** This API is only available if the library is built with pre-processor
10872 ** CAPI3REF: Flush caches to disk mid-transaction
10875 ** ^If a write-transaction is open on [database connection] D when the
10877 ** pages in the pager-cache that are not currently in use are written out
10881 ** interface flushes caches for all schemas - "main", "temp", and
10886 ** immediately and there is a busy-handler callback configured, it is invoked
10894 ** example an IO error or out-of-memory condition), then processing is
10905 ** CAPI3REF: The pre-update hook.
10909 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10985 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10986 ** triggers; or 2 for changes resulting from triggers called by top-level
10990 ** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10995 ** pre-update hook is being invoked for some other reason, including a
10996 ** regular DELETE, sqlite3_preupdate_blobwrite() returns -1.
11022 ** CAPI3REF: Low-level system error code
11027 ** The return value is OS-dependent. For example, on unix systems, after
11067 ** If there is not already a read-transaction open on schema S when
11070 ** If a read-transaction is opened by this function, then it is guaranteed
11072 ** writer or checkpointer until after the read-transaction is closed. This
11073 ** is not guaranteed if a read-transaction is already open when this
11076 ** even while the read-transaction remains open.
11107 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11141 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
11142 ** SQLITE_IOERR error code - is returned, then the final state of the
11156 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11173 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
11241 ** For an ordinary on-disk database file, the serialization is just a
11242 ** copy of the disk file. For an in-memory database or a "TEMP" database,
11286 ** Zero or more of the following constants can be OR-ed together for
11290 ** a pointer to contiguous in-memory database that it is currently using,
11292 ** a contiguous in-memory database, then this option causes
11294 ** using a contiguous in-memory database if it has been initialized by a
11304 ** reopen S as an in-memory database based on the serialization contained
11308 ** permitted to add content to the in-memory database as long as the total
11370 ** should be treated as read-only.
11374 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
11425 /* The double-precision datatype used by RTree depends on the
11426 ** SQLITE_RTREE_INT_ONLY compile-time option.
11436 ** R-Tree geometry query as follows:
11461 ** Register a 2nd-generation geometry callback named zScore that can be
11462 ** used as part of an R-Tree geometry query as follows:
11568 ** is not possible for an application to register a pre-update hook on a
11571 ** which a pre-update hook is already defined. The results of attempting
11652 ** disabled - it does not. A newly created session object is enabled.
11659 ** no-op, and may be used to query the current state of the session.
11736 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11742 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11746 ** conflict-handler callback) then the X'' value is returned. The application
11794 ** database row along with the updated values for each updated non-primary-key
11803 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11805 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11818 ** are sorted in the same order in which they were attached (or auto-attached)
11832 ** recorded once - the first time a row with said primary key is inserted,
11839 ** The session object therefore accumulates two types of records - those
11857 ** found and one or more of the non-primary key fields have been
11874 ** This may appear to have some counter-intuitive effects if a single row
11891 ** CAPI3REF: Return An Upper-limit For The Size Of The Changeset
11913 ** does not have a primary key, this function is a no-op (but does not return
11933 ** used to update the table in database zFrom (call this the "from-table")
11935 ** object (call this the "to-table"). Specifically:
11938 ** <li> For each row (primary key) that exists in the to-table but not in
11939 ** the from-table, an INSERT record is added to the session object.
11941 ** <li> For each row (primary key) that exists in the to-table but not in
11942 ** the from-table, a DELETE record is added to the session object.
11945 ** different non-PK values in each, an UPDATE record is added to the
11954 ** Unless the call to this function is a no-op as described above, it is an
11991 ** Because the non-primary key "old.*" fields are omitted, no
12010 ** Return non-zero if no changes to attached tables have been recorded by
12018 ** are restored. However, if this function returns non-zero, then it is
12105 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
12129 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12142 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
12145 ** or until the conflict-handler function returns.
12192 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
12201 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12232 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
12266 ** conflict-handler callback by [sqlite3changeset_apply()] with either
12277 ** "conflicting row" associated with the current conflict-handler callback
12315 ** function with an iterator passed to a conflict-handler by
12323 ** returned. This is to allow the following pattern (pseudo-code):
12507 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12508 ** <tr><th style="white-space:pre">Existing Change </th>
12509 ** <th style="white-space:pre">New Change </th>
12559 ** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12641 ** Otherwise, if the return value is non-zero or the xFilter argument to
12677 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12681 ** returned by each invocation of the conflict-handler function. Refer to
12690 ** stored in all non-primary key columns also match the values stored in
12694 ** the non-primary key fields contains a value different from the original
12695 ** row value stored in the changeset, the conflict-handler function is
12698 ** only the values of those non-primary key fields are compared against
12699 ** the current database contents - any trailing database table columns
12703 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12708 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12727 ** This includes the case where the INSERT operation is re-attempted because
12735 ** stored in all modified non-primary key columns also match the values
12739 ** the modified non-primary key fields contains a value different from an
12740 ** original row value stored in the changeset, the conflict-handler function
12742 ** UPDATE changes only contain values for non-primary key fields that are
12744 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12747 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12751 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12769 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12867 ** Values that may be passed as the second argument to a conflict-handler.
12874 ** (non primary-key) fields modified by the update do not contain the
13035 ** combined on a per-field basis, not per-row. This means that in the
13119 ** corresponding non-streaming API functions:
13121 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
13122 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
13132 ** Non-streaming functions that accept changesets (or patchsets) as input
13137 ** low-memory environment is required to handle very large changesets, the
13401 ** non-negative but less than the number of columns in the table, return
13416 ** non-negative but less than the number of columns in the table, set
13433 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13538 ** If the bClear argument is non-zero, then the auxiliary data is cleared
13560 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
13562 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
13568 ** modify this structure directly - it should only be used as shown above
13576 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
13594 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
13596 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13605 ** xPhraseFirstColumn() set iCol to -1).
13647 ** to scan the portion of the full-text index that matches the prefix
13654 ** of the initial querying of the full-text index, avoiding the second scan
13657 ** either on a per-table basis using the [FTS5 insttoken | 'insttoken']
13658 ** option, or on a per-query basis using the
13673 ** into the fts5 table, then (*pzLocale) is set to point to a nul-terminated
13674 ** buffer containing the name of the locale in utf-8 encoding. (*pnLocale)
13676 ** nul-terminator.
13761 ** The second and third arguments are an array of nul-terminated strings
13779 ** by argument pText. pText may or may not be nul-terminated. The first
13787 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13792 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13796 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13801 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13807 ** The sixth and seventh arguments passed to xTokenize() - pLocale and
13808 ** nLocale - are a pointer to a buffer containing the locale to use for
13810 ** pLocale buffer is not nul-terminated. pLocale may be passed NULL (in
13838 ** then the xTokenize() method has two additional arguments - pLocale
13842 ** an nLocale byte buffer containing the name of the locale to use as utf-8
13843 ** text. pLocale is not nul-terminated.
13865 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13899 ** still appears to contain just two phrases - "(first OR 1st)"
13910 ** when tokenizing query text (it should not - to do so would be
13957 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
14106 ** autoconf-based build
14131 ** The maximum length of a TEXT or BLOB in bytes. This also
14134 ** The hard limit is the ability of a 32-bit signed integer
14135 ** to count the size: 2^31-1 or 2147483647.
14140 #define SQLITE_MIN_LENGTH 30 /* Minimum value for the length limit */
14154 ** tell you that in a well-normalized database, you usually should
14160 ** point of view, but the underlying b-tree that implements the index
14172 ** The maximum length of a single SQL statement in bytes.
14224 ** The suggested maximum number of in-memory pages to use for
14227 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
14229 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
14230 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
14233 # define SQLITE_DEFAULT_CACHE_SIZE -2000
14247 ** counted using a signed 8-bit integer which has a maximum value of 127
14259 ** as a signed 32-bit integer can hold.
14266 ** imposed by the use of 16-bit offsets within each page.
14272 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
14273 ** compiled with the default page-size limit will not be able to rollback
14296 ** device characteristics (sector-size and atomic write() support),
14313 ** This value can be lowered (or raised) at run-time using that the
14321 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
14344 #pragma warn -rch /* unreachable code */
14345 #pragma warn -ccc /* Condition is always true or false */
14346 #pragma warn -aus /* Assigned value is never used */
14347 #pragma warn -csu /* Comparing signed and unsigned */
14348 #pragma warn -spa /* Suspicious pointer arithmetic */
14356 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
14389 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
14402 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
14403 #else /* Generates a warning - but it always works */
14448 ** SEH support if the -DSQLITE_OMIT_SEH option is given.
14458 ** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14461 /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14473 ** level of threadsafety. 2 means the library is multithreaded - multiple
14481 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
14489 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
14495 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
14502 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
14531 # error "Two or more of the following compile-time configuration options\
14569 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
14634 ** Disable ALWAYS() and NEVER() (make them pass-throughs) for coverage
14646 ** of SQLite to unexpected behavior - to make the code "self-healing"
14652 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
14740 ** Return true (non-zero) if the input is an integer that is too large
14741 ** to fit in 32-bits. This macro is used inside of various testcase()
14742 ** macros to verify that we have tested SQLite for large-file support.
14769 ** This is the header file for the generic hash-table implementation
14780 ** The internals of this structure are intended to be opaque -- client
14787 ** All elements of the hash table are on a single doubly-linked list.
14791 ** the global doubly-linked list. The contents of the bucket are the
14792 ** element pointed to plus the next _ht.count-1 elements in the list.
14811 ** structure. All elements are stored on a single doubly-linked list.
14843 #define sqliteHashFirst(H) ((H)->first)
14844 #define sqliteHashNext(E) ((E)->next)
14845 #define sqliteHashData(E) ((E)->data)
14846 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14847 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14852 #define sqliteHashCount(H) ((H)->count)
15065 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
15070 ** substitute integer for floating-point
15075 # define fabs(X) ((X)<0?-(X):(X))
15102 ** the VDBE-level file format changes. The following macros define the
15113 ** changed at run-time using a pragma.
15121 ** on the command-line
15151 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
15153 ** The default value of "20" was chosen to minimize the run-time of the
15154 ** speedtest1 test program with options: --shrink-memory --reprepare
15168 ** The compile-time options SQLITE_MMAP_READWRITE and
15181 #define offsetof(STRUCTURE,FIELD) ((size_t)((char*)&((STRUCTURE*)0)->FIELD))
15185 ** Work around C99 "flex-array" syntax for pre-C99 compilers, so as
15186 ** to avoid complaints from -fsanitize=strict-bounds.
15222 ** types can be conveniently redefined at compile-type. Like this:
15224 ** cc '-DUINTPTR_TYPE=long long int' ...
15261 typedef sqlite_int64 i64; /* 8-byte signed integer */
15262 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
15263 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
15264 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
15265 typedef INT16_TYPE i16; /* 2-byte signed integer */
15266 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
15267 typedef INT8_TYPE i8; /* 1-byte signed integer */
15280 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
15289 ** Estimated quantities used for query planning are stored as 16-bit
15291 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
15300 ** 1 -> 0 20 -> 43 10000 -> 132
15301 ** 2 -> 10 25 -> 46 25000 -> 146
15302 ** 3 -> 16 100 -> 66 1000000 -> 199
15303 ** 4 -> 20 1000 -> 99 1048576 -> 200
15304 ** 10 -> 33 1024 -> 100 4294967296 -> 320
15309 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
15312 #define LOGEST_MIN (-32768)
15354 ** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
15359 ** |-----------------| FALSE
15360 ** |-------|
15364 ** |-----------------|
15365 ** |-------| TRUE
15369 ** |-----------------|
15370 ** |-------| FALSE
15377 ** and whether or not that determination is run-time or compile-time.
15379 ** For best performance, an attempt is made to guess at the byte-order
15380 ** using C-preprocessor macros. If that is unsuccessful, or if
15381 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
15382 ** at run-time.
15387 ** -DSQLITE_BYTEORDER=1234
15391 ** -DSQLITE_BYTEORDER=4321
15393 ** to cause the build to work for little-endian or big-endian processors,
15396 #ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
15434 ** Constants for the largest and smallest possible 64-bit signed integers.
15435 ** These macros are designed to work correctly on both 32-bit and 64-bit
15440 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
15447 #define SMXV(n) ((((i64)1)<<(sizeof(n)*8-1))-1)
15448 #define UMXV(n) ((((i64)1)<<(sizeof(n)*8))-1)
15452 ** to force 8-byte alignment on 64-bit architectures.
15457 ** pointers in size, and so it is a no-op on systems where the pointer
15473 ** Assert that the pointer X is aligned to an 8-byte boundary. This
15478 ** underlying malloc() implementation might return us 4-byte aligned
15479 ** pointers. In that case, only verify 4-byte alignment.
15482 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&3)==0)
15484 # define EIGHT_BYTE_ALIGNMENT(X) ((((uptr)(X) - (uptr)0)&7)==0)
15496 ** Default maximum size of memory used by memory-mapped I/O in the VFS
15516 ** default MMAP_SIZE is specified at compile-time, make sure that it does
15540 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
15552 ** 0x00000008 Result-set wildcard expansion
15558 ** 0x00000200 Count-of-view optimization
15563 ** 0x00004000 Push-down optimization
15564 ** 0x00008000 After all FROM-clause analysis
15586 ** (---any--) Top-level block structure
15587 ** 0x-------F High-level debug messages
15588 ** 0x----FFF- More detail
15589 ** 0xFFFF---- Low-level debug messages
15612 ** 0x00080000 Star-query heuristic
15617 ** An instance of the following structure is used to store the busy-handler
15622 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
15656 ** The root-page of the schema table.
15675 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
15694 ** directly, we use its constant as a key to lookup the run-time allocated
15696 ** for the run-time allocated buffer.
15699 ** macros become no-ops and have zero performance impact.
15801 ** Changing this from a 64-bit to a 32-bit type limits the number of
15823 #define ALLBITS ((Bitmask)-1)
15824 #define TOPBIT (((Bitmask)1)<<(BMS-1))
15853 ** This header file (together with is companion C source-code file
15857 ** This header file is #include-ed by sqliteInt.h and thus ends up
15865 ** necessary pre-processor macros for it.
15881 ** This file contains pre-processor directives related to operating system
15902 ** If SQLITE_OS_OTHER=1 is specified at compile-time, then the application
15965 ** a no-op
15971 /* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
15998 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16000 ** 2006-10-31: The default prefix used to be "sqlite_". But then
16001 ** Mcafee started using SQLite in their anti-virus product and it
16047 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
16155 ** sqlite3_malloc() to obtain space for the file-handle structure.
16191 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
16214 ** is devoted to storing a super-journal name - there are no more pages to
16218 #define PAGER_SJ_PGNO_COMPUTED(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
16219 #define PAGER_SJ_PGNO(x) ((x)->lckPgno)
16227 #define PAGER_MEMORY 0x0002 /* In-memory database */
16232 #define PAGER_LOCKINGMODE_QUERY -1
16243 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
16248 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
16249 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
16255 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
16259 ** if( isOpen(pPager->jfd) ){ ...
16263 ** if( pPager->jfd->pMethods ){ ...
16265 #define isOpen(pFd) ((pFd)->pMethods!=0)
16271 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
16293 ** that make up the Pager sub-system API. See source code comments for
16445 ** This header file defines the interface that the sqlite B-Tree file
16458 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
16465 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
16466 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
16479 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
16494 #define BTREE_MEMORY 2 /* This is an in-memory DB */
16495 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
16549 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
16552 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
16555 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
16556 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
16576 ** For example, the free-page-count field is located at byte offset 36 of
16577 ** the database file header. The incr-vacuum-flag field is located at
16581 ** It is a read-only number computed by the pager. But we merge it with
16594 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
16608 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
16609 ** column of the b-tree of the cursor. The Expr tree will not contain
16610 ** any function calls nor subqueries nor references to b-trees other than
16613 ** The design of the _RANGE hint is aid b-tree implementations that try
16614 ** to prefetch content from remote machines - to provide those
16639 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
16644 ** For read-only cursors the wrFlag argument is always zero. For read-write
16655 ** by this, the native b-tree engine of SQLite, but it is available to
16657 ** b-tree system. For alternative storage engines in which a delete of
16661 ** and DELETE operations as no-ops, and any READ operation against a
16664 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
16670 int wrFlag, /* 1 for writing. 0 for read-only */
16727 ** nKey the ROWID length of pKey
16731 ** nData length of pData not used
16819 ** Enter and Leave procedures no-ops.
16904 int selId; /* SELECT-id for the SELECT statement on the RHS */
16920 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
16948 u32 iSrcLine; /* Source-code line that generated this opcode
16960 ** A sub-routine used to implement a trigger program.
16963 VdbeOp *aOp; /* Array of opcodes for sub-program */
16969 SubProgram *pNext; /* Next sub-program already visited */
16989 #define P4_STATIC (-1) /* Pointer to a static string */
16990 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
16991 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
16992 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
16993 #define P4_TABLE (-5) /* P4 is a pointer to a Table structure */
16995 #define P4_FREE_IF_LE (-6)
16996 #define P4_DYNAMIC (-6) /* Pointer to memory from sqliteMalloc() */
16997 #define P4_FUNCDEF (-7) /* P4 is a pointer to a FuncDef structure */
16998 #define P4_KEYINFO (-8) /* P4 is a pointer to a KeyInfo structure */
16999 #define P4_EXPR (-9) /* P4 is a pointer to an Expr tree */
17000 #define P4_MEM (-10) /* P4 is a pointer to a Mem* structure */
17001 #define P4_VTAB (-11) /* P4 is a pointer to an sqlite3_vtab structure */
17002 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
17003 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
17004 #define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
17005 #define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
17006 #define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
17007 #define P4_SUBRTNSIG (-17) /* P4 is a SubrtnSig pointer */
17099 #define OP_IfPos 50 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
17109 #define OP_IfNotZero 60 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
17110 #define OP_DecrJumpZero 61 /* jump, synopsis: if (--r[P1])==0 goto P2 */
17139 #define OP_Compare 90 /* synopsis: r[P1@P3] <-> r[P2@P3] */
17157 #define OP_Subtract 108 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
17173 #define OP_SeekScan 124 /* synopsis: Scan-ahead up to P1 rows */
17209 #define OP_OffsetLimit 160 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
17290 ** Additional non-public SQLITE_PREPARE_* flags
17345 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17350 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
17429 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
17466 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
17471 ** // in distinguishing equal and not-equal.
17474 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
17475 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
17483 ** is responsible for keeping track of this and reporting byte-code branches
17621 /* Modify the page-size after the cache has been created. */
17643 /* Change a page number. Used by incr-vacuum. */
17685 /* Set and get the suggested cache-size for the specified pager-cache.
17688 ** the total number of pages cached by purgeable pager-caches to the sum
17689 ** of the suggested cache-sizes.
17696 /* Set or get the suggested spill-size for the specified pager-cache.
17698 ** The spill-size is the minimum number of pages in cache before the cache
17762 ** at start-time.
17764 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
17767 ** start-time.
17769 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
17771 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
17788 ** If this is a no-op implementation, implement everything as macros.
17809 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
17829 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
17830 ** In other words, the zero-based numbers are used for all external interfaces
17831 ** and the one-based values are used internally.
17859 ** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17888 ** Db.pSchema->flags field.
17890 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
17891 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
17892 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
17893 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
17896 ** Allowed values for the DB.pSchema->flags field.
17916 ** Lookaside malloc is a set of fixed-size buffers that can be used
17938 ** in a performance-critical path. sz should be set by to szTrue whenever
17943 ** come off of pFree first, then pInit as a fallback. This dual-list
17944 ** allows use to compute a high-water mark - the maximum number of allocations
17945 ** outstanding at any point in the past - by subtracting the number of
17948 ** Enhancement on 2019-12-12: Two-size-lookaside
17952 ** The two-size-lookaside enhancement breaks up the lookaside allocation
17953 ** into two pools: One of 128-byte slots and the other of the default size
17954 ** (1200-byte) slots. Allocations are filled from the small-pool first,
17955 ** failing over to the full-size pool if that does not work. Thus more
17972 void *pMiddle; /* First byte past end of full-size buffers and
17977 void *pTrueEnd; /* True value of pEnd, when db->pnBytesFreed!=0 */
17983 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
17984 #define EnableLookaside db->lookaside.bDisable--;\
17985 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17987 /* Size of the smaller allocations in two-size lookaside */
17995 ** A hash table for built-in function definitions. (Application-defined
18046 u32 nSchemaLock; /* Do not reset the schema when non-zero */
18054 u8 autoCommit; /* The auto-commit flag. */
18058 u8 dfltLockMode; /* Default locking-mode for attached dbs */
18064 u8 noSharedCache; /* True if no shared-cache backends */
18112 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
18151 int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
18154 int nSavepoint; /* Number of non-transaction savepoints */
18155 int nStatement; /* Number of nested statement-transactions */
18182 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
18183 #define ENC(db) ((db)->enc)
18188 ** C-compilers still do not accept LL integer literals.
18231 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
18232 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
18238 #define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
18258 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
18279 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
18283 #define SQLITE_PushDown 0x00001000 /* WHERE-clause push-down opt */
18285 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
18293 #define SQLITE_BalancedMerge 0x00200000 /* Balance multi-way merges */
18298 #define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
18300 #define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
18308 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
18309 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
18315 #define ConstFactorOk(P) ((P)->okConstFactor)
18330 ** structure. For global built-in functions (ex: substr(), max(), count())
18332 ** For per-connection application-defined functions, a pointer to this
18333 ** structure is held in the db->aHash hash table.
18335 ** The u.pHash field is used by the global built-ins. The u.pDestructor
18336 ** field is used by per-connection app-def functions.
18339 i16 nArg; /* Number of arguments. -1 means unlimited */
18343 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
18346 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
18355 ** This structure encapsulates a user-function destructor callback (as
18388 ** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
18395 ** See multiple instances of tag-20230109-1.
18399 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
18402 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
18403 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
18404 #define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
18405 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
18406 /* 0x0200 -- available for reuse */
18407 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
18411 ** single query - might change over time */
18412 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
18414 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
18419 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
18420 #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
18424 /* Identifier numbers for each in-line function */
18442 ** as the user-data (sqlite3_user_data()) for the function. If
18453 ** zName is the name of a function that is implemented by in-line
18459 ** zName is the name of a test-only function implemented by in-line
18468 ** a single query. The iArg is ignored. The user-data is always set
18472 ** For math-library functions. xPtr is an arbitrary pointer.
18477 ** ignored and the user-data for these functions is set to an
18478 ** arbitrary non-NULL pointer. The bNC parameter is not used.
18496 ** available as the function user-data (sqlite3_user_data()). The
18561 char *zName; /* Savepoint name (nul-terminated) */
18622 u16 iDflt; /* 1-based index of DEFAULT. 0 means "none" */
18653 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
18673 char *zName; /* Name of the collating sequence, UTF-8 encoded */
18685 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
18708 #define SQLITE_AFF_DEFER 0x58 /* 'X' - defer computation until later */
18740 ** database connections, even when the rest of the in-memory database
18750 ** database schema are initially stored in a linked-list pointed to by
18757 ** When an in-memory Table object is deleted (for example when the
18808 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
18840 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
18841 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
18851 #define TF_Readonly 0x00000001 /* Read-only system table */
18861 #define TF_NoVisibleRowid 0x00000200 /* No user-visible "rowid" column */
18862 #define TF_OOOHidden 0x00000400 /* Out-of-Order hidden columns */
18877 #define IsView(X) ((X)->eTabType==TABTYP_VIEW)
18878 #define IsOrdinaryTable(X) ((X)->eTabType==TABTYP_NORM)
18886 # define IsVirtual(X) ((X)->eTabType==TABTYP_VTAB)
18888 ((X)->op==TK_COLUMN && (X)->y.pTab->eTabType==TABTYP_VTAB)
18896 ** only works for non-virtual tables (ordinary tables and views) and is
18901 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18902 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18904 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
18913 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18914 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18916 /* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18938 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
18941 ** from-table == child-table
18942 ** to-table == parent-table
18945 ** which is attached to the from-table. The to-table need not exist when
18946 ** the from-table is created. The existence of the to-table is not checked.
18960 /* EV: R-30323-21917 */
19030 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
19061 ** pKeyInfo->nField.
19065 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
19069 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
19070 ** multiple entries in the b-tree with the same key (when only looking
19071 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
19076 ** get and equal results when comparing this structure to a b-tree record.
19080 ** b-tree.
19083 KeyInfo *pKeyInfo; /* Collation and sort-order information */
19122 ** algorithm to employ when an attempt is made to insert a non-unique
19129 ** "colUsed & colNotIdxed" will be non-zero if the index is not a
19131 ** be true (note-20221022-a). If a column beyond the 63rd column of the
19132 ** table is used, the "colUsed & colNotIdxed" test will always be non-zero
19142 ** number (it cannot - the database page is not allocated until the VDBE
19167 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
19180 IndexSample *aSample; /* Samples of the left-most key */
19181 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
19182 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
19196 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
19199 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
19204 #define XN_ROWID (-1) /* Indexed column is the rowid */
19205 #define XN_EXPR (-2) /* Indexed column is an expression */
19237 const char *z; /* Text of the token. Not NULL-terminated! */
19261 int sortingIdxPTab; /* Cursor number of pseudo-table */
19296 ** assignAggregateRegisters() that computes the value of pAggInfo->iFirstReg.
19300 #define AggInfoColumnReg(A,I) (assert((A)->iFirstReg),(A)->iFirstReg+(I))
19302 (assert((A)->iFirstReg),(A)->iFirstReg+(A)->nColumn+(I))
19304 #define AggInfoColumnReg(A,I) ((A)->iFirstReg+(I))
19306 ((A)->iFirstReg+(A)->nColumn+(I))
19310 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
19311 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
19312 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
19317 ** the option is available (at compile-time).
19332 ** to represent the greater-than-or-equal-to operator in the expression
19346 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
19365 ** subquery gives a constant result, then iTable is -1. If the subquery
19370 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
19401 int iValue; /* Non-negative integer value if EP_IntValue */
19426 ** TK_TRIGGER: 1 -> new, 0 -> old
19431 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
19434 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
19498 #define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0)
19499 #define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P))
19500 #define ExprSetProperty(E,P) (E)->flags|=(u32)(P)
19501 #define ExprClearProperty(E,P) (E)->flags&=~(u32)(P)
19502 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19503 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19504 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19509 #define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
19510 #define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
19511 #define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
19512 #define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
19513 #define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
19514 #define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
19515 #define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
19516 #define ExprUseYWin(E) (((E)->flags&EP_WinFunc)!=0)
19517 #define ExprUseYSub(E) (((E)->flags&EP_Subrtn)!=0)
19526 ** processes but is a no-op for delivery.
19529 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
19530 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
19531 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
19551 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
19561 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
19577 ** ---------- -------------------------
19642 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
19660 #define EU4_EXPR 2 /* Uses IdList.a.u4.pExpr -- NOT CURRENTLY USED */
19669 int regResult; /* Registers holding results of a co-routine */
19681 ** In the colUsed field, the high-order bit (bit 63) is set if the table
19682 ** contains more than 63 columns and the 64-th or later column is used.
19687 ** always be checked, either by an if-statement or by an assert().
19690 ** --------------- -----------------------------------
19712 Table *pSTab; /* Table object for zName. Mnemonic: Srcitem-TABle */
19718 unsigned isTabFunc :1; /* True if table-valued-function syntax */
19719 unsigned isCorrelated :1; /* True if sub-query is correlated */
19721 unsigned viaCoroutine :1; /* Implemented as a co-routine */
19727 unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
19738 ExprList *pFuncArg; /* Arguments to table-valued-function */
19806 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
19809 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
19812 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
19821 #define WHERE_KEEP_ALL_JOINS 0x2000 /* Do not do the omit-noop-join opt */
19841 ** NameContexts can be nested. When resolving names, the inner-most
19857 ExprList *pEList; /* Optional list of result-set columns */
19899 #define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19908 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
19909 ** conflict-target clause.) The pUpsertTargetWhere is the optional
20014 #define SF_PushDown 0x1000000 /* Modified by WHERE-clause push-down opt */
20023 ((X)->fg.isSubquery && \
20024 ((X)->u4.pSubq->pSelect->selFlags&SF_NestedFrom)!=0)
20032 ** identified by pDest->iSDParm.
20034 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
20036 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
20041 ** the side-effects of functions.
20048 ** in register pDest->iSDParm then abandon the rest
20052 ** row of result as the key in table pDest->iSDParm.
20053 ** Apply the affinity pDest->affSdst before storing
20054 ** results. if pDest->iSDParm2 is positive, then it is
20057 ** pDest->iSDParm table. This SRT is used to
20060 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
20066 ** SRT_Coroutine Generate a co-routine that returns a new row of
20068 ** of the co-routine is stored in register pDest->iSDParm
20069 ** and the result row is stored in pDest->nDest registers
20070 ** starting with pDest->iSdst.
20072 ** SRT_Table Store results in temporary table pDest->iSDParm.
20078 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
20079 ** But also use temporary table pDest->iSDParm+1 as
20083 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
20087 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
20089 ** index at pDest->iSDParm+1 hold all prior stores.
20092 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
20093 ** table is an intkey table - in this case the first
20095 ** key. If (pDest->iSDParm>0), then the table is an index
20096 ** table. (pDest->iSDParm) is the number of key columns in
20108 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
20114 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
20142 ** the code generator needs. We have to keep per-table autoincrement
20161 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
20243 ** The nTableLock and aTableLock variables are only used if the shared-cache
20244 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
20245 ** used to store the set of table-locks required by the statement being
20287 ** of the base register during check-constraint eval */
20303 TableLock *aTableLock; /* Required table locks for shared-cache mode */
20350 int nHeight; /* Expression tree height of current sub-select */
20360 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
20382 #define PARSE_HDR_SZ (offsetof(Parse,aTempReg)-offsetof(Parse,zErrMsg)) /* Recursive part w/o aColC…
20384 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
20393 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
20399 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
20405 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
20429 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
20433 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
20437 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
20438 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
20473 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
20474 the <column-list> is stored here */
20493 ** that is a part of a trigger-program.
20498 ** the first step of the trigger-program.
20505 ** orconf -> stores the ON CONFLICT algorithm
20506 ** pSelect -> The content to be inserted - either a SELECT statement or
20508 ** zTarget -> Dequoted name of the table to insert into.
20509 ** pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
20510 ** statement, then this stores the column-names to be
20512 ** pUpsert -> The ON CONFLICT clauses for an Upsert
20515 ** zTarget -> Dequoted name of the table to delete from.
20516 ** pWhere -> The WHERE clause of the DELETE statement if one is specified.
20520 ** zTarget -> Dequoted name of the table to update.
20521 ** pWhere -> The WHERE clause of the UPDATE statement if one is specified.
20523 ** pExprList -> A list of the columns to update and the expressions to update
20528 ** pSelect -> The SELECT statement
20531 ** pExprList -> The list of expressions that follow the RETURNING keyword.
20547 TriggerStep *pNext; /* Next in the link-list */
20548 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
20574 u32 nChar; /* Length of the string so far */
20578 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
20582 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
20585 ** The following object is the header for an "RCStr" or "reference-counted
20596 ** 3. Make a (read-only) copy of a read-only RCStr string using
20630 ** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20633 ** be used on trunk check-ins. They are a temporary mechanism available
20638 #define SQLITE_NTUNE 6 /* Should be zero for all trunk check-ins */
20640 # define Tuning(X) (sqlite3Config.aTune[(X)-1])
20655 u8 bUseCis; /* Use covering indices for full-scans */
20659 u8 bJsonSelfcheck; /* Double-check JSON parsing */
20661 int mxStrlen; /* Maximum string length */
20662 int neverCorrupt; /* Database is always well-formed */
20665 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
20666 sqlite3_mem_methods m; /* Low-level memory allocation interface */
20667 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
20668 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
20678 int sharedCacheEnabled; /* true if shared-cache mode enabled */
20680 /* The above might be initialized to non-zero. The following need to always
20716 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
20718 /* vvvv--- must be last ---vvv */
20726 ** the assert is only valid on a well-formed database. Instead of:
20738 ** things that are always true for well-formed databases.
20743 ** Context pointer passed down through the tree-walk.
20752 u16 mWFlags; /* Use-dependent flags */
20786 const char *zType; /* Type of the container - used for error messages */
20787 const Token *pName; /* Name of the container - used for error messages */
20815 ** Return code from the parse-tree walking primitives and their
20888 ** that is N bytes long, including the zero-terminator. */
20894 ** data structures on sqlite3DebugPrintf() using a tree-like view.
20911 ** (2) All window functions in a single SELECT form a linked-list
20933 u8 bImplicitFrame; /* True if frame was implicitly specified */
20984 ** Assuming zIn points to the first byte of a UTF-8 character,
20985 ** advance zIn to point to the first byte of the next UTF-8 character.
20996 ** routines that report the line-number on which the error originated
21168 # define MAN754 ((((u64)1)<<52)-1)
21194 char sign; /* '+' or '-' */
21335 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21336 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
21345 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
21611 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
21612 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
21676 ** Routines to read and write variable-length integers. These used to
21904 ** The interface to the LEMON-generated parser
21965 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
22042 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
22044 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
22046 ** provided (enforcement of FK constraints requires the triggers sub-system).
22097 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
22151 ** sqlite3IoTrace is a pointer to a printf-like routine used to
22184 ** passed back to non-lookaside free() routines. Asserts such as the
22185 ** example above are placed on the non-lookaside free() routines to verify
22188 ** All of this is no-op for a production build. It only comes into
22189 ** play when the SQLITE_MEMDEBUG compile-time option is used.
22196 # define sqlite3MemdebugSetType(X,Y) /* no-op */
22240 # define IS_STMT_SCANSTATUS(db) (db->flags & SQLITE_StmtScanStatus)
22262 ** all of the platform-specific files (os_*.c) and is #included into those
22274 ** switch. The following code should catch this problem at compile-time.
22289 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
22313 || sqlite3_io_error_pending-- == 1 ) \
22328 sqlite3_diskfull_pending--; \
22371 ** This file implements routines used to report what compile-time options
22374 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
22378 ** autoconf-based build
22392 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
22398 ** An array of names of all compile-time options. This array should
22399 ** be sorted A-Z.
22402 ** only a handful of compile-time options, so most times this array is usually
22443 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
22447 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
22449 "COMPILER=gcc-" __VERSION__,
23175 /* An array to map all upper-case characters into their corresponding
23176 ** lower-case character.
23178 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
23218 /* All of the upper-to-lower conversion data is above. The following
23235 ** index (here shown as [256-OP_Ne]) would be out-of-bounds and thus
23236 ** be undefined behavior. That's goofy, but the C-standards people thought
23244 SQLITE_PRIVATE const unsigned char *sqlite3aLTb = &sqlite3UpperToLower[256-OP_Ne];
23245 SQLITE_PRIVATE const unsigned char *sqlite3aEQb = &sqlite3UpperToLower[256+6-OP_Ne];
23246 SQLITE_PRIVATE const unsigned char *sqlite3aGTb = &sqlite3UpperToLower[256+12-OP_Ne];
23249 ** The following 256 byte lookup table is used to support SQLites built-in
23258 ** SQLite identifier character 0x40 $, _, or non-ascii
23262 ** case. i.e. if the character is a lower-case ASCII character.
23263 ** If x is a lower-case ASCII character, then its upper-case equivalent
23264 ** is (x - 0x20). Therefore toupper() can be implemented as:
23271 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
23273 ** non-ASCII UTF character. Hence the test for whether or not a character is
23282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
23314 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
23318 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
23319 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
23321 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
23329 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
23330 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
23331 ** that compile-time option is omitted.
23337 # error "Compile-time disabling of covering index scan using the\
23338 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
23354 ** before 3.12.0). -1 means always keep the entire statement journal in
23364 ** The default lookaside-configuration, the format "SZ,N". SZ is the
23366 ** and N is the number of slots. The lookaside-configuration can be
23367 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
23368 ** or at run-time for an individual database connection using
23371 ** With the two-size-lookaside enhancement, less lookaside is required.
23372 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
23373 ** and 93 128-byte slots, which is more lookaside than is available
23374 ** using the older 1200,100 configuration without two-size-lookaside.
23385 /* The default maximum size of an in-memory database created using
23450 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
23463 ** Hash table for global functions - functions common to all
23465 ** read-only.
23484 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
23491 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
23537 ** sqlite3StdTypeLen[] The length (in bytes) of each entry
23651 ** * A b-tree cursor
23652 ** - In the main database or in an ephemeral database
23653 ** - On either an index or a table
23656 ** * A one-row "pseudotable" stored in a single register
23661 i8 iDb; /* Index of cursor database in db->aDb[] */
23670 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23673 Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23729 /* Return true if P is a null-only cursor
23732 ((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
23754 ** When a sub-program is executed (OP_Program), a structure of this type
23756 ** well as the current memory cell array and various other frame specific
23757 ** values stored in the Vdbe struct. When the sub-program is finished,
23759 ** restoring the state of the VM to as it was before the sub-program
23763 ** cell in the parent (calling) frame. When the memory cell is deleted or
23769 ** child frame are released.
23771 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
23772 ** set to NULL if the currently executing frame is the main program.
23776 Vdbe *v; /* VM this frame belongs to */
23777 VdbeFrame *pParent; /* Parent of this frame, or NULL if parent is main */
23778 Op *aOp; /* Program instructions for parent frame */
23779 Mem *aMem; /* Array of memory cells for parent frame */
23780 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
23789 int pc; /* Program Counter in parent (calling) frame */
23792 int nChildMem; /* Number of memory cells for child frame */
23793 int nChildCsr; /* Number of cursors for child frame */
23795 i64 nDbChange; /* Value of db->nChange */
23830 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
23850 ** UPDATE no-change flag set
23856 ** * MEM_Null|MEM_Cleared Special SQL NULL that compares non-equal
23860 ** length Mem.n. Zero-terminated if
23866 ** * MEM_Blob A blob, stored in Mem.z length Mem.n.
23870 ** * MEM_Blob|MEM_Zero A blob in Mem.z of length Mem.n plus
23916 /* Return TRUE if Mem X contains dynamically allocated content - anything
23920 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
23926 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
23929 ** True if Mem X is a NULL-nochng type.
23932 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
23933 && (X)->n==0 && (X)->u.nZero==0)
23944 #define memIsValid(M) ((M)->flags & MEM_AffMask)!=0
24013 int iSelectID; /* The "Select-ID" for this loop */
24018 /* The DblquoteStr object holds the text of a double-quoted
24022 ** list is consulted for each double-quoted identifier to see if the
24070 i64 startTime; /* Time when query started - used for profiling */
24085 bft changeCntOn:1; /* True to update the change-counter */
24090 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
24096 DblquoteStr *pDblStr; /* List of double-quoted string literals */
24099 VdbeFrame *pFrame; /* Parent frame */
24100 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
24103 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
24146 ** set of values on the right-hand side of an IN constraint.
24162 ** single-byte varint.
24259 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*); /* Actually deletes the Frame */
24320 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
24365 ** we have to locate the state vector at run-time. In the more common
24395 ** The value of N is added to the current status value and the high-water
24399 ** mark is unchanged. N must be non-negative for StatusDown().
24419 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
24491 p = p->pNext; in countLookasideSlots()
24501 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
24502 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
24504 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
24505 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
24507 assert( db->lookaside.nSlot >= nInit+nFree ); in sqlite3LookasideUsed()
24508 if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit); in sqlite3LookasideUsed()
24509 return (int)(db->lookaside.nSlot - (nInit+nFree)); in sqlite3LookasideUsed()
24519 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
24520 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
24528 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
24533 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
24535 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24536 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
24537 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
24538 db->lookaside.pFree = 0; in sqlite3_db_status()
24541 p = db->lookaside.pSmallFree; in sqlite3_db_status()
24543 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
24544 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
24545 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
24546 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
24559 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
24560 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
24562 *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
24564 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
24579 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24580 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
24606 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24607 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24608 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24609 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24610 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
24615 pSchema->tblHash.count in sqlite3_db_status()
24616 + pSchema->trigHash.count in sqlite3_db_status()
24617 + pSchema->idxHash.count in sqlite3_db_status()
24618 + pSchema->fkeyHash.count in sqlite3_db_status()
24620 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
24621 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
24622 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
24623 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
24625 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24628 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
24633 db->pnBytesFreed = 0; in sqlite3_db_status()
24634 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24651 db->pnBytesFreed = &nByte; in sqlite3_db_status()
24652 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); in sqlite3_db_status()
24653 db->lookaside.pEnd = db->lookaside.pStart; in sqlite3_db_status()
24654 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ in sqlite3_db_status()
24657 db->lookaside.pEnd = db->lookaside.pTrueEnd; in sqlite3_db_status()
24658 db->pnBytesFreed = 0; in sqlite3_db_status()
24660 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
24682 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
24683 if( db->aDb[i].pBt ){ in sqlite3_db_status()
24684 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
24688 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
24689 /* IMP: R-54100-20147 */ in sqlite3_db_status()
24690 /* IMP: R-29431-39229 */ in sqlite3_db_status()
24695 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
24700 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
24701 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
24709 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
24729 ** There is only one exported symbol in this file - the function
24738 ** 1970-01-01 00:00:00 is JD 2440587.5
24739 ** 2000-01-01 00:00:00 is JD 2451544.5
24741 ** This implementation requires years to be expressed as a 4-digit number
24742 ** which means that only dates between 0000-01-01 and 9999-12-31 can
24748 ** use the julian calendar for dates prior to 1582-10-15 and for some
24756 ** ISBN 0-943396-61-1
24757 ** Willmann-Bell, Inc
24805 ** of a four-character format specifiers ABCD is:
24819 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
24820 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
24821 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
24822 ** the 2-digit day which is the last integer in the set.
24835 char N = zFormat[0] - '0'; in getDigits()
24836 char min = zFormat[1] - '0'; in getDigits()
24841 max = aMx[zFormat[2] - 'a']; in getDigits()
24844 while( N-- ){ in getDigits()
24848 val = val*10 + *zDate - '0'; in getDigits()
24865 ** Parse a timezone extension on the end of a date-time.
24868 ** (+/-)HH:MM
24875 ** of change in p->tz and return 0. If a parser error occurs,
24876 ** return non-zero.
24885 p->tz = 0; in parseTimezone()
24887 if( c=='-' ){ in parseTimezone()
24888 sgn = -1; in parseTimezone()
24893 p->isLocal = 0; in parseTimezone()
24894 p->isUtc = 1; in parseTimezone()
24904 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
24934 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
24939 /* Truncate to avoid problems with sub-milliseconds in parseHhMmSs()
24946 p->validJD = 0; in parseHhMmSs()
24947 p->rawS = 0; in parseHhMmSs()
24948 p->validHMS = 1; in parseHhMmSs()
24949 p->h = h; in parseHhMmSs()
24950 p->m = m; in parseHhMmSs()
24951 p->s = s + ms; in parseHhMmSs()
24961 p->isError = 1; in datetimeError()
24965 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
24966 ** that the YYYY-MM-DD is according to the Gregorian calendar.
24973 if( p->validJD ) return; in computeJD()
24974 if( p->validYMD ){ in computeJD()
24975 Y = p->Y; in computeJD()
24976 M = p->M; in computeJD()
24977 D = p->D; in computeJD()
24979 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
24983 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
24988 Y--; in computeJD()
24992 B = 38 - A + (A/4); in computeJD()
24995 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
24996 p->validJD = 1; in computeJD()
24997 if( p->validHMS ){ in computeJD()
24998 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000 + 0.5); in computeJD()
24999 if( p->tz ){ in computeJD()
25000 p->iJD -= p->tz*60000; in computeJD()
25001 p->validYMD = 0; in computeJD()
25002 p->validHMS = 0; in computeJD()
25003 p->tz = 0; in computeJD()
25004 p->isUtc = 1; in computeJD()
25005 p->isLocal = 0; in computeJD()
25011 ** Given the YYYY-MM-DD information current in p, determine if there
25012 ** is day-of-month overflow and set nFloor to the number of days that
25017 assert( p->validYMD || p->isError ); in computeFloor()
25018 assert( p->D>=0 && p->D<=31 ); in computeFloor()
25019 assert( p->M>=0 && p->M<=12 ); in computeFloor()
25020 if( p->D<=28 ){ in computeFloor()
25021 p->nFloor = 0; in computeFloor()
25022 }else if( (1<<p->M) & 0x15aa ){ in computeFloor()
25023 p->nFloor = 0; in computeFloor()
25024 }else if( p->M!=2 ){ in computeFloor()
25025 p->nFloor = (p->D==31); in computeFloor()
25026 }else if( p->Y%4!=0 || (p->Y%100==0 && p->Y%400!=0) ){ in computeFloor()
25027 p->nFloor = p->D - 28; in computeFloor()
25029 p->nFloor = p->D - 29; in computeFloor()
25036 ** YYYY-MM-DD HH:MM:SS.FFF
25037 ** YYYY-MM-DD HH:MM:SS
25038 ** YYYY-MM-DD HH:MM
25039 ** YYYY-MM-DD
25042 ** on success and 1 if the input string is not a well-formed
25048 if( zDate[0]=='-' ){ in parseYyyyMmDd()
25054 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
25062 p->validHMS = 0; in parseYyyyMmDd()
25066 p->validJD = 0; in parseYyyyMmDd()
25067 p->validYMD = 1; in parseYyyyMmDd()
25068 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
25069 p->M = M; in parseYyyyMmDd()
25070 p->D = D; in parseYyyyMmDd()
25072 if( p->tz ){ in parseYyyyMmDd()
25087 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
25088 if( p->iJD>0 ){ in setDateTimeToCurrent()
25089 p->validJD = 1; in setDateTimeToCurrent()
25090 p->isUtc = 1; in setDateTimeToCurrent()
25091 p->isLocal = 0; in setDateTimeToCurrent()
25103 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
25106 p->s = r; in setRawDateNumber()
25107 p->rawS = 1; in setRawDateNumber()
25109 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
25110 p->validJD = 1; in setRawDateNumber()
25120 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
25124 ** In the first form, the +/-HH:MM is always optional. The fractional
25148 p->useSubsec = 1; in parseDateOrTime()
25154 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
25177 if( p->validYMD ) return; in computeYMD()
25178 if( !p->validJD ){ in computeYMD()
25179 p->Y = 2000; in computeYMD()
25180 p->M = 1; in computeYMD()
25181 p->D = 1; in computeYMD()
25182 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
25186 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
25187 alpha = (int)((Z + 32044.75)/36524.25) - 52; in computeYMD()
25188 A = Z + 1 + alpha - ((alpha+100)/4) + 25; in computeYMD()
25190 C = (int)((B - 122.1)/365.25); in computeYMD()
25192 E = (int)((B-D)/30.6001); in computeYMD()
25194 p->D = B - D - X1; in computeYMD()
25195 p->M = E<14 ? E-1 : E-13; in computeYMD()
25196 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
25198 p->validYMD = 1; in computeYMD()
25206 if( p->validHMS ) return; in computeHMS()
25208 day_ms = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
25209 p->s = (day_ms % 60000)/1000.0; in computeHMS()
25211 p->m = day_min % 60; in computeHMS()
25212 p->h = day_min / 60; in computeHMS()
25213 p->rawS = 0; in computeHMS()
25214 p->validHMS = 1; in computeHMS()
25229 p->validYMD = 0; in clearYMD_HMS_TZ()
25230 p->validHMS = 0; in clearYMD_HMS_TZ()
25231 p->tz = 0; in clearYMD_HMS_TZ()
25241 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
25255 ** using whatever operating-system specific localtime facility that
25257 ** non-zero on any kind of error.
25259 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is non-zero then this
25262 ** invoked in place of the OS-defined localtime() function.
25264 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
25330 if( p->iJD<2108667600*(i64)100000 /* 1970-01-01 */ in toLocaltime()
25331 || p->iJD>2130141456*(i64)100000 /* 2038-01-18 */ in toLocaltime()
25333 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in toLocaltime()
25340 iYearDiff = (2000 + x.Y%4) - x.Y; in toLocaltime()
25344 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in toLocaltime()
25347 t = (time_t)(p->iJD/1000 - 21086676*(i64)10000); in toLocaltime()
25350 sqlite3_result_error(pCtx, "local time unavailable", -1); in toLocaltime()
25353 p->Y = sLocal.tm_year + 1900 - iYearDiff; in toLocaltime()
25354 p->M = sLocal.tm_mon + 1; in toLocaltime()
25355 p->D = sLocal.tm_mday; in toLocaltime()
25356 p->h = sLocal.tm_hour; in toLocaltime()
25357 p->m = sLocal.tm_min; in toLocaltime()
25358 p->s = sLocal.tm_sec + (p->iJD%1000)*0.001; in toLocaltime()
25359 p->validYMD = 1; in toLocaltime()
25360 p->validHMS = 1; in toLocaltime()
25361 p->validJD = 0; in toLocaltime()
25362 p->rawS = 0; in toLocaltime()
25363 p->tz = 0; in toLocaltime()
25364 p->isError = 0; in toLocaltime()
25374 ** Where NNN is an arbitrary floating-point number and "days" can be one
25378 u8 nName; /* Length of the name */
25397 if( !p->rawS || p->validJD ){ in autoAdjustDate()
25398 p->rawS = 0; in autoAdjustDate()
25399 }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */ in autoAdjustDate()
25400 && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */ in autoAdjustDate()
25402 double r = p->s*1000.0 + 210866760000000.0; in autoAdjustDate()
25404 p->iJD = (sqlite3_int64)(r + 0.5); in autoAdjustDate()
25405 p->validJD = 1; in autoAdjustDate()
25406 p->rawS = 0; in autoAdjustDate()
25411 ** Process a modifier to a date-time stamp. The modifiers are
25420 ** +/-YYYY-MM-DD HH:MM:SS.SSS
25443 int n, /* Length of zMod in bytes */ in parseModifier()
25458 if( idx>1 ) return 1; /* IMP: R-33611-57934 */ in parseModifier()
25468 ** Resolve day-of-month overflow by rolling forward into the next in parseModifier()
25470 ** a no-op that is only included for symmetry. See "floor". in parseModifier()
25476 p->nFloor = 0; in parseModifier()
25484 ** Resolve day-of-month overflow by rolling back to the end of the in parseModifier()
25489 p->iJD -= p->nFloor*86400000; in parseModifier()
25499 ** Always interpret the prior number as a julian-day value. If this in parseModifier()
25505 if( idx>1 ) return 1; /* IMP: R-31176-64601 */ in parseModifier()
25506 if( p->validJD && p->rawS ){ in parseModifier()
25508 p->rawS = 0; in parseModifier()
25521 rc = p->isLocal ? SQLITE_OK : toLocaltime(p, pCtx); in parseModifier()
25522 p->isUtc = 0; in parseModifier()
25523 p->isLocal = 1; in parseModifier()
25532 ** Treat the current value of p->s as the number of in parseModifier()
25535 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
25536 if( idx>1 ) return 1; /* IMP: R-49255-55373 */ in parseModifier()
25537 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
25540 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
25541 p->validJD = 1; in parseModifier()
25542 p->rawS = 0; in parseModifier()
25548 if( p->isUtc==0 ){ in parseModifier()
25555 iGuess = iOrigJD = p->iJD; in parseModifier()
25560 iGuess -= iErr; in parseModifier()
25566 iErr = new.iJD - iOrigJD; in parseModifier()
25569 p->iJD = iGuess; in parseModifier()
25570 p->validJD = 1; in parseModifier()
25571 p->isUtc = 1; in parseModifier()
25572 p->isLocal = 0; in parseModifier()
25585 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
25592 p->tz = 0; in parseModifier()
25593 p->validJD = 0; in parseModifier()
25595 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
25596 if( Z>n ) Z -= 7; in parseModifier()
25597 p->iJD += (n - Z)*86400000; in parseModifier()
25620 p->useSubsec = 1; in parseModifier()
25625 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
25628 p->validHMS = 1; in parseModifier()
25629 p->h = p->m = 0; in parseModifier()
25630 p->s = 0.0; in parseModifier()
25631 p->rawS = 0; in parseModifier()
25632 p->tz = 0; in parseModifier()
25633 p->validJD = 0; in parseModifier()
25635 p->D = 1; in parseModifier()
25638 p->M = 1; in parseModifier()
25639 p->D = 1; in parseModifier()
25647 case '-': in parseModifier()
25666 if( z[n]=='-' ){ in parseModifier()
25675 if( z[n]=='-' ){ in parseModifier()
25676 /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the in parseModifier()
25678 ** the range 0-11 and DD is limited to 0-30. in parseModifier()
25680 if( z0!='+' && z0!='-' ) break; /* Must start with +/- */ in parseModifier()
25682 if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25685 if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break; in parseModifier()
25691 p->validJD = 0; in parseModifier()
25692 if( z0=='-' ){ in parseModifier()
25693 p->Y -= Y; in parseModifier()
25694 p->M -= M; in parseModifier()
25695 D = -D; in parseModifier()
25697 p->Y += Y; in parseModifier()
25698 p->M += M; in parseModifier()
25700 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25701 p->Y += x; in parseModifier()
25702 p->M -= x*12; in parseModifier()
25705 p->validHMS = 0; in parseModifier()
25706 p->validYMD = 0; in parseModifier()
25707 p->iJD += (i64)D*86400000; in parseModifier()
25722 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
25734 tx.iJD -= 43200000; in parseModifier()
25736 tx.iJD -= day*86400000; in parseModifier()
25737 if( z0=='-' ) tx.iJD = -tx.iJD; in parseModifier()
25740 p->iJD += tx.iJD; in parseModifier()
25751 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
25754 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
25755 p->nFloor = 0; in parseModifier()
25759 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
25765 p->M += (int)r; in parseModifier()
25766 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
25767 p->Y += x; in parseModifier()
25768 p->M -= x*12; in parseModifier()
25770 p->validJD = 0; in parseModifier()
25771 r -= (int)r; in parseModifier()
25778 assert( p->M>=0 && p->M<=12 ); in parseModifier()
25779 p->Y += y; in parseModifier()
25781 p->validJD = 0; in parseModifier()
25782 r -= (int)r; in parseModifier()
25787 p->iJD += (sqlite3_int64)(r*1000.0*aXformType[i].rXform + rRounder); in parseModifier()
25803 ** Process time function arguments. argv[0] is a date-time stamp.
25840 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
25841 if( argc==1 && p->validYMD && p->D>28 ){ in isDate()
25842 /* Make sure a YYYY-MM-DD is normalized. in isDate()
25843 ** Example: 2023-02-31 -> 2023-03-03 */ in isDate()
25844 assert( p->validJD ); in isDate()
25845 p->validYMD = 0; in isDate()
25877 ** the unix epoch of 1970-01-01 00:00:00 GMT.
25888 sqlite3_result_double(context, (x.iJD - 21086676*(i64)10000000)/1000.0); in unixepochFunc()
25890 sqlite3_result_int64(context, x.iJD/1000 - 21086676*(i64)10000); in unixepochFunc()
25898 ** Return YYYY-MM-DD HH:MM:SS
25911 if( Y<0 ) Y = -Y; in datetimeFunc()
25916 zBuf[5] = '-'; in datetimeFunc()
25919 zBuf[8] = '-'; in datetimeFunc()
25947 zBuf[0] = '-'; in datetimeFunc()
25950 sqlite3_result_text(context, &zBuf[1], n-1, SQLITE_TRANSIENT); in datetimeFunc()
26000 ** Return YYYY-MM-DD
26013 if( Y<0 ) Y = -Y; in dateFunc()
26018 zBuf[5] = '-'; in dateFunc()
26021 zBuf[8] = '-'; in dateFunc()
26026 zBuf[0] = '-'; in dateFunc()
26037 ** In other words, compute the zero-based day number for the
26047 assert( pDate->validJD ); in daysAfterJan01()
26052 return (int)((pDate->iJD-jan01.iJD+43200000)/86400000); in daysAfterJan01()
26064 assert( pDate->validJD ); in daysAfterMonday()
26065 return (int)((pDate->iJD+43200000)/86400000) % 7; in daysAfterMonday()
26077 assert( pDate->validJD ); in daysAfterSunday()
26078 return (int)((pDate->iJD+129600000)/86400000) % 7; in daysAfterSunday()
26086 ** %d day of month 01-31
26087 ** %e day of month 1-31
26089 ** %F ISO date. YYYY-MM-DD
26090 ** %G ISO year corresponding to %V 0000-9999.
26091 ** %g 2-digit ISO year corresponding to %V 00-99
26092 ** %H hour 00-24
26093 ** %k hour 0-24 (leading zero converted to space)
26094 ** %I hour 01-12
26095 ** %j day of year 001-366
26097 ** %l hour 1-12 (leading zero converted to space)
26098 ** %m month 01-12
26099 ** %M minute 00-59
26103 ** %s seconds since 1970-01-01
26104 ** %S seconds 00-59
26106 ** %u day of week 1-7 Monday==1, Sunday==7
26107 ** %w day of week 0-6 Sunday==0, Monday==1
26108 ** %U week of year 00-53 (First Sunday is start of week 01)
26109 ** %V week of year 01-53 (First week containing Thursday is week 01)
26110 ** %W week of year 00-53 (First Monday is start of week 01)
26111 ** %Y year 0000-9999
26128 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
26130 sqlite3StrAccumInit(&sRes, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); in strftimeFunc()
26137 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
26147 case 'f': { /* Fractional seconds. (Non-standard) */ in strftimeFunc()
26154 sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D); in strftimeFunc()
26162 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
26180 if( h>12 ) h -= 12; in strftimeFunc()
26189 case 'J': { /* Julian day number. (Non-standard) */ in strftimeFunc()
26217 (x.iJD - 21086676*(i64)10000000)/1000.0); in strftimeFunc()
26219 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
26239 case 'U': { /* Week num. 00-53. First Sun of the year is week 01 */ in strftimeFunc()
26241 (daysAfterJan01(&x)-daysAfterSunday(&x)+7)/7); in strftimeFunc()
26244 case 'V': { /* Week num. 01-53. First week with a Thur is week 01 */ in strftimeFunc()
26248 y.iJD += (3 - daysAfterMonday(&x))*86400000; in strftimeFunc()
26254 case 'W': { /* Week num. 00-53. First Mon of the year is week 01 */ in strftimeFunc()
26256 (daysAfterJan01(&x)-daysAfterMonday(&x)+7)/7); in strftimeFunc()
26273 if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j)); in strftimeFunc()
26311 ** +YYYY-MM-DD HH:MM:SS.SSS
26313 ** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
26319 ** ISO-8601 string. The unix timestamps are not supported by this
26338 Y = d1.Y - d2.Y; in timediffFunc()
26344 M = d1.M - d2.M; in timediffFunc()
26346 Y--; in timediffFunc()
26355 M--; in timediffFunc()
26358 Y--; in timediffFunc()
26360 d2.M--; in timediffFunc()
26363 d2.Y--; in timediffFunc()
26368 d1.iJD -= d2.iJD; in timediffFunc()
26371 sign = '-'; in timediffFunc()
26372 Y = d2.Y - d1.Y; in timediffFunc()
26378 M = d2.M - d1.M; in timediffFunc()
26380 Y--; in timediffFunc()
26389 M--; in timediffFunc()
26392 Y--; in timediffFunc()
26402 d1.iJD = d2.iJD - d1.iJD; in timediffFunc()
26408 sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f", in timediffFunc()
26409 sign, Y, M, d1.D-1, d1.h, d1.m, d1.s); in timediffFunc()
26431 ** If the library is compiled to omit the full-scale date and time
26437 ** This function uses the C-library functions time(), gmtime()
26439 ** as the user-data for the function.
26458 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
26469 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
26498 sqlite3_result_text(context, zJson, -1, sqlite3_free); in datedebugFunc()
26512 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
26513 PURE_DATE(unixepoch, -1, 0, 0, unixepochFunc ), in sqlite3RegisterDateTimeFunctions()
26514 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
26515 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
26516 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
26517 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
26520 PURE_DATE(datedebug, -1, 0, 0, datedebugFunc ), in sqlite3RegisterDateTimeFunctions()
26527 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26528 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
26560 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
26578 ** from within OsOpen()), but some third-party implementations may.
26618 if( pId->pMethods ){ in sqlite3OsClose()
26619 pId->pMethods->xClose(pId); in sqlite3OsClose()
26620 pId->pMethods = 0; in sqlite3OsClose()
26625 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
26629 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
26632 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
26636 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
26640 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
26645 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
26649 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
26653 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
26665 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
26675 ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM in sqlite3OsFileControl()
26683 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
26690 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
26693 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
26697 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
26701 if( NEVER(id->pMethods==0) ) return 0; in sqlite3OsDeviceCharacteristics()
26702 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
26706 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
26709 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
26712 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
26722 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
26730 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
26733 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
26736 /* No-op stubs to use when memory-mapped I/O is disabled */
26764 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
26765 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
26771 return pVfs->xDelete!=0 ? pVfs->xDelete(pVfs, zPath, dirSync) : SQLITE_OK; in sqlite3OsDelete()
26780 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
26790 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
26795 assert( strlen(zPath)<=SQLITE_MAX_PATHLEN ); /* tag-20210611-1 */ in sqlite3OsDlOpen()
26796 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
26799 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
26802 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
26805 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
26815 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
26820 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
26823 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
26827 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
26833 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
26834 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
26837 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
26852 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
26910 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
26912 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
26924 /* No-op */ in vfsUnlink()
26926 vfsList = pVfs->pNext; in vfsUnlink()
26929 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
26930 p = p->pNext; in vfsUnlink()
26932 if( p->pNext==pVfs ){ in vfsUnlink()
26933 p->pNext = pVfs->pNext; in vfsUnlink()
26957 pVfs->pNext = vfsList; in sqlite3_vfs_register()
26960 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
26961 vfsList->pNext = pVfs; in sqlite3_vfs_register()
27003 ** Most malloc failures are non-benign. After they occur, SQLite
27027 ** we have to locate the state vector at run-time. In the more common
27057 ** indicates that subsequent malloc failures are non-benign.
27088 ** This file contains a no-op memory allocation drivers for use when
27098 ** used when no other memory allocator is specified using compile-time
27104 ** No-op versions of all memory allocation routines
27117 ** Populate the low-level memory allocation function pointers in
27150 ** This file contains low-level memory allocation drivers for when
27151 ** SQLite will use the standard C-library malloc/realloc/free interface
27154 ** This file contains implementations of the low-level memory allocation
27162 ** C-preprocessor macro summary:
27169 ** a different name, using a separate -D
27185 ** used when no other memory allocator is specified using compile-time
27205 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
27210 ** Use standard C library malloc and free on non-Apple systems.
27227 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
27261 ** For this low-level routine, we are guaranteed that nByte>0 because
27295 ** For this low-level routine, we already know that pPrior!=0 since
27297 ** by higher-level routines.
27305 p--; in sqlite3MemFree()
27322 p--; in sqlite3MemSize()
27331 ** For this low-level interface, we know that pPrior!=0. Cases where
27332 ** pPrior==0 while have been intercepted by higher-level routine and
27334 ** cases where nByte<=0 will have been intercepted by higher-level
27350 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
27351 p--; in sqlite3MemRealloc()
27411 ** Populate the low-level memory allocation function pointers in
27444 ** This file contains low-level memory allocation drivers for when
27445 ** SQLite will use the standard C-library malloc/realloc/free interface
27450 ** This file contains implementations of the low-level memory allocation
27476 ** ------------------------------------------------------------------------
27478 ** ------------------------------------------------------------------------
27562 if( i>NCSIZE-1 ){ in adjustStats()
27563 i = NCSIZE - 1; in adjustStats()
27572 mem.nCurrent[i]--; in adjustStats()
27590 p--; in sqlite3MemsysGetHeader()
27591 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
27592 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
27600 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
27613 return (int)pHdr->iSize; in sqlite3MemSize()
27646 ** Fill a buffer with pseudo-random bytes. This is used to preset
27655 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27660 nByte -= 4; in randomFill()
27662 while( nByte-- > 0 ){ in randomFill()
27663 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
27691 pHdr->pNext = 0; in sqlite3MemMalloc()
27692 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
27694 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
27699 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
27700 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
27701 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
27702 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
27705 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
27706 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
27709 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
27712 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
27717 pHdr->iSize = nByte; in sqlite3MemMalloc()
27722 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
27740 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
27742 if( pHdr->pPrev ){ in sqlite3MemFree()
27743 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
27744 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
27747 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
27749 if( pHdr->pNext ){ in sqlite3MemFree()
27750 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
27751 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
27754 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
27757 z -= pHdr->nTitle; in sqlite3MemFree()
27758 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
27759 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
27760 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
27778 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
27782 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
27783 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
27784 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
27792 ** Populate the low-level memory allocation function pointers in
27816 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
27817 pHdr->eType = eType; in sqlite3MemdebugSetType()
27835 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
27836 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
27857 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
27858 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
27887 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
27896 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
27898 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
27899 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
27918 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
27920 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
27922 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
27923 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
27926 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
27927 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
27932 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
27938 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
27940 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
27941 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
27993 ** mean that the library will use a memory-pool by default, just that
28034 ** two fields form a double-linked list of chunks of related sizes.
28071 ** True if we are evaluating an out-of-memory callback.
28099 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
28132 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
28134 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
28135 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
28138 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
28167 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
28168 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
28169 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
28172 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
28211 ** size parameters for check-out and return a pointer to the
28218 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
28219 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
28220 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
28221 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
28222 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
28223 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
28235 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
28245 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
28247 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
28248 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
28249 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
28250 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
28251 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
28252 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
28253 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
28283 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
28287 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
28288 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
28293 size = i + size/4 - prev; in memsys3Merge()
28294 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
28295 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
28296 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
28336 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
28338 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
28344 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
28377 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
28404 i = p - mem3.aPool; in memsys3FreeUnsafe()
28405 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
28406 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
28408 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
28409 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
28410 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
28415 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
28416 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
28417 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
28420 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
28421 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
28422 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
28424 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
28425 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
28427 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
28428 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
28429 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
28436 ** size returned omits the 8-byte header overhead. This only
28443 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
28444 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
28454 return ((n+11)&~7) - 4; in memsys3Roundup()
28494 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
28523 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
28569 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
28575 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
28580 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
28586 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
28588 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
28592 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
28597 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28606 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
28611 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
28612 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
28628 ** Populate the low-level memory allocation function pointers in
28689 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
28698 ** N >= M*(1 + log2(n)/2) - n + 1
28727 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
28762 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
28793 ** structures, return a pointer to the idx-th such link.
28807 next = MEM5LINK(i)->next; in memsys5Unlink()
28808 prev = MEM5LINK(i)->prev; in memsys5Unlink()
28812 MEM5LINK(prev)->next = next; in memsys5Unlink()
28815 MEM5LINK(next)->prev = prev; in memsys5Unlink()
28830 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
28831 MEM5LINK(i)->prev = -1; in memsys5Link()
28834 MEM5LINK(x)->prev = i; in memsys5Link()
28856 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
28911 iBin--; in memsys5MallocUnsafe()
28922 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
28949 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
28951 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
28953 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
28958 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
28961 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
28966 mem5.currentCount--; in memsys5FreeUnsafe()
28967 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
28976 iBuddy = iBlock - size; in memsys5FreeUnsafe()
29038 ** memsys5Round(). Hence nBytes is always a non-negative power
29047 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
29070 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
29092 ** Examples: memsys5Log(1) -> 0
29093 ** memsys5Log(2) -> 1
29094 ** memsys5Log(4) -> 2
29095 ** memsys5Log(5) -> 3
29096 ** memsys5Log(8) -> 3
29097 ** memsys5Log(9) -> 4
29101 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
29126 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
29144 mem5.aiFreelist[ii] = -1; in memsys5Init()
29148 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
29198 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
29289 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
29300 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
29310 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
29313 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
29344 p->iType = iType; in checkMutexAlloc()
29347 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
29352 p = &staticMutexes[iType-2]; in checkMutexAlloc()
29355 if( p->mutex==0 ){ in checkMutexAlloc()
29356 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
29357 if( p->mutex==0 ){ in checkMutexAlloc()
29377 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
29381 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
29396 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
29397 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
29401 "illegal multi-threaded access to database connection" in checkMutexEnter()
29404 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
29412 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
29420 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
29450 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
29451 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
29479 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
29480 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
29481 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
29482 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
29483 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
29484 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
29485 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
29486 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
29488 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
29577 ** this function is a no-op.
29591 ** Because these routines raise false-positive alerts in TSAN, disable
29634 ** here are place-holders. Applications can substitute working
29635 ** mutex routines at start-time using the
29708 return p==0 || p->cnt>0; in debugMutexHeld()
29712 return p==0 || p->cnt==0; in debugMutexNotheld()
29727 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
29734 pNew->id = id; in debugMutexAlloc()
29735 pNew->cnt = 0; in debugMutexAlloc()
29741 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
29746 pNew = &aStatic[id-2]; in debugMutexAlloc()
29747 pNew->id = id; in debugMutexAlloc()
29759 assert( p->cnt==0 ); in debugMutexFree()
29760 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
29782 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
29783 p->cnt++; in debugMutexEnter()
29787 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
29788 p->cnt++; in debugMutexTry()
29801 p->cnt--; in debugMutexLeave()
29802 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
29824 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
29825 ** is used regardless of the run-time threadsafety setting.
29865 ** home-grown mutexes. Encapsulate these conditions into a single #define.
29908 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
29914 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
29917 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
29970 ** cases where it really needs one. If a faster non-recursive mutex
30011 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
30017 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
30021 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
30029 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
30031 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
30038 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
30043 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
30048 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
30060 assert( p->nRef==0 ); in pthreadMutexFree()
30062 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
30065 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
30087 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
30092 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
30093 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
30095 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
30102 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
30103 p->nRef++; in pthreadMutexEnter()
30105 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
30106 assert( p->nRef==0 ); in pthreadMutexEnter()
30107 p->owner = self; in pthreadMutexEnter()
30108 p->nRef = 1; in pthreadMutexEnter()
30112 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
30114 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
30116 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
30117 p->owner = pthread_self(); in pthreadMutexEnter()
30118 p->nRef++; in pthreadMutexEnter()
30123 if( p->trace ){ in pthreadMutexEnter()
30124 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
30130 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
30135 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
30136 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
30138 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
30145 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
30146 p->nRef++; in pthreadMutexTry()
30148 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
30149 assert( p->nRef==0 ); in pthreadMutexTry()
30150 p->owner = self; in pthreadMutexTry()
30151 p->nRef = 1; in pthreadMutexTry()
30158 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
30160 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
30162 p->owner = pthread_self(); in pthreadMutexTry()
30163 p->nRef++; in pthreadMutexTry()
30172 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
30173 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
30188 p->nRef--; in pthreadMutexLeave()
30189 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
30191 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
30194 if( p->nRef==0 ){ in pthreadMutexLeave()
30195 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
30198 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
30202 if( p->trace ){ in pthreadMutexLeave()
30203 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
30310 ** Determine if we are dealing with Windows CE - which has a much reduced
30338 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
30393 return p->nRef!=0 && p->owner==GetCurrentThreadId(); in winMutexHeld()
30397 return p->nRef==0 || p->owner!=tid; in winMutexNotheld2()
30442 static int winMutex_isNt = -1; /* <0 means "need to query" */
30520 ** cases where it really needs one. If a faster non-recursive mutex
30546 p->id = iType; in winMutexAlloc()
30549 p->trace = 1; in winMutexAlloc()
30553 InitializeCriticalSectionEx(&p->mutex, 0, 0); in winMutexAlloc()
30555 InitializeCriticalSection(&p->mutex); in winMutexAlloc()
30562 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ in winMutexAlloc()
30567 p = &winMutex_staticMutexes[iType-2]; in winMutexAlloc()
30570 InterlockedCompareExchange(&p->trace, 1, 0); in winMutexAlloc()
30576 assert( p==0 || p->id==iType ); in winMutexAlloc()
30588 assert( p->nRef==0 && p->owner==0 ); in winMutexFree()
30589 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ in winMutexFree()
30590 DeleteCriticalSection(&p->mutex); in winMutexFree()
30616 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexEnter()
30621 EnterCriticalSection(&p->mutex); in winMutexEnter()
30623 assert( p->nRef>0 || p->owner==0 ); in winMutexEnter()
30624 p->owner = tid; in winMutexEnter()
30625 p->nRef++; in winMutexEnter()
30626 if( p->trace ){ in winMutexEnter()
30627 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexEnter()
30628 tid, p->id, p, p->trace, p->nRef)); in winMutexEnter()
30639 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); in winMutexTry()
30653 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); in winMutexTry()
30658 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ in winMutexTry()
30660 p->owner = tid; in winMutexTry()
30661 p->nRef++; in winMutexTry()
30669 if( p->trace ){ in winMutexTry()
30670 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n", in winMutexTry()
30671 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); in winMutexTry()
30689 assert( p->nRef>0 ); in winMutexLeave()
30690 assert( p->owner==tid ); in winMutexLeave()
30691 p->nRef--; in winMutexLeave()
30692 if( p->nRef==0 ) p->owner = 0; in winMutexLeave()
30693 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in winMutexLeave()
30696 LeaveCriticalSection(&p->mutex); in winMutexLeave()
30698 if( p->trace ){ in winMutexLeave()
30699 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n", in winMutexLeave()
30700 tid, p->id, p, p->trace, p->nRef)); in winMutexLeave()
30747 ** Attempt to release up to n bytes of non-essential memory currently
30748 ** held by SQLite. An example of non-essential memory is memory used to
30755 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine in sqlite3_release_memory()
30756 ** is a no-op returning zero if SQLite is not compiled with in sqlite3_release_memory()
30798 ** no-op.
30813 ** Set the soft heap-size limit for the library. An argument of
30814 ** zero disables the limit. A negative argument is a no-op used to
30829 if( rc ) return -1; in sqlite3_soft_heap_limit64()
30844 excess = sqlite3_memory_used() - n; in sqlite3_soft_heap_limit64()
30854 ** Set the hard heap-size limit for the library. An argument of zero
30855 ** disables the hard heap limit. A negative argument is a no-op used
30869 if( rc ) return -1; in sqlite3_hard_heap_limit64()
30904 ** Return true if the heap is currently under memory pressure - in other
30954 ** This routine is called whenever an out-of-memory condition is seen,
30956 ** code debuggers when working on out-of-memory conditions, for example
30967 # define test_oom_breakpoint(X) /* No-op for production builds */
30980 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal in mallocWithAlarm()
30990 if( nUsed >= mem0.alarmThreshold - nFull ){ in mallocWithAlarm()
30995 if( nUsed >= mem0.hardLimit - nFull ){ in mallocWithAlarm()
31027 ** This provides a 256-byte safety margin for defense against 32-bit
31056 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ in sqlite3Malloc()
31083 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pTrueEnd); in isLookaside()
31099 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL; in lookasideMallocSize()
31101 return db->lookaside.szTrue; in lookasideMallocSize()
31116 if( ((uptr)p)<(uptr)(db->lookaside.pTrueEnd) ){ in sqlite3DbMallocSize()
31118 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbMallocSize()
31119 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
31123 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbMallocSize()
31124 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocSize()
31125 return db->lookaside.szTrue; in sqlite3DbMallocSize()
31141 if( p==0 ) return; /* IMP: R-49053-54554 */ in sqlite3_free()
31157 ** *db->pnBytesFreed.
31160 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); in measureAllocationSize()
31165 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
31166 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
31169 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFreeNN()
31172 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbFreeNN()
31174 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbFreeNN()
31176 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
31180 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbFreeNN()
31181 db->lookaside.pSmallFree = pBuf; in sqlite3DbFreeNN()
31185 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbFreeNN()
31187 assert( db->pnBytesFreed==0 ); in sqlite3DbFreeNN()
31189 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbFreeNN()
31191 pBuf->pNext = db->lookaside.pFree; in sqlite3DbFreeNN()
31192 db->lookaside.pFree = pBuf; in sqlite3DbFreeNN()
31196 if( db->pnBytesFreed ){ in sqlite3DbFreeNN()
31209 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbNNFreeNN()
31211 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){ in sqlite3DbNNFreeNN()
31213 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){ in sqlite3DbNNFreeNN()
31215 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
31219 pBuf->pNext = db->lookaside.pSmallFree; in sqlite3DbNNFreeNN()
31220 db->lookaside.pSmallFree = pBuf; in sqlite3DbNNFreeNN()
31224 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){ in sqlite3DbNNFreeNN()
31226 assert( db->pnBytesFreed==0 ); in sqlite3DbNNFreeNN()
31228 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */ in sqlite3DbNNFreeNN()
31230 pBuf->pNext = db->lookaside.pFree; in sqlite3DbNNFreeNN()
31231 db->lookaside.pFree = pBuf; in sqlite3DbNNFreeNN()
31235 if( db->pnBytesFreed ){ in sqlite3DbNNFreeNN()
31245 assert( db==0 || sqlite3_mutex_held(db->mutex) ); in sqlite3DbFree()
31258 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ in sqlite3Realloc()
31261 sqlite3_free(pOld); /* IMP: R-26507-47431 */ in sqlite3Realloc()
31269 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second in sqlite3Realloc()
31279 nDiff = nNew - nOld; in sqlite3Realloc()
31281 mem0.alarmThreshold-nDiff ){ in sqlite3Realloc()
31283 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){ in sqlite3Realloc()
31298 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); in sqlite3Realloc()
31304 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ in sqlite3Realloc()
31316 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ in sqlite3_realloc()
31360 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); in dbMallocRawFinish()
31369 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
31397 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
31398 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
31399 if( n>db->lookaside.sz ){ in sqlite3DbMallocRawNN()
31400 if( !db->lookaside.bDisable ){ in sqlite3DbMallocRawNN()
31401 db->lookaside.anStat[1]++; in sqlite3DbMallocRawNN()
31402 }else if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
31409 if( (pBuf = db->lookaside.pSmallFree)!=0 ){ in sqlite3DbMallocRawNN()
31410 db->lookaside.pSmallFree = pBuf->pNext; in sqlite3DbMallocRawNN()
31411 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
31413 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){ in sqlite3DbMallocRawNN()
31414 db->lookaside.pSmallInit = pBuf->pNext; in sqlite3DbMallocRawNN()
31415 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
31420 if( (pBuf = db->lookaside.pFree)!=0 ){ in sqlite3DbMallocRawNN()
31421 db->lookaside.pFree = pBuf->pNext; in sqlite3DbMallocRawNN()
31422 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
31424 }else if( (pBuf = db->lookaside.pInit)!=0 ){ in sqlite3DbMallocRawNN()
31425 db->lookaside.pInit = pBuf->pNext; in sqlite3DbMallocRawNN()
31426 db->lookaside.anStat[0]++; in sqlite3DbMallocRawNN()
31429 db->lookaside.anStat[2]++; in sqlite3DbMallocRawNN()
31433 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbMallocRawNN()
31434 assert( db->pnBytesFreed==0 ); in sqlite3DbMallocRawNN()
31435 if( db->mallocFailed ){ in sqlite3DbMallocRawNN()
31452 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3DbRealloc()
31453 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){ in sqlite3DbRealloc()
31455 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){ in sqlite3DbRealloc()
31459 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){ in sqlite3DbRealloc()
31460 if( n<=db->lookaside.szTrue ) return p; in sqlite3DbRealloc()
31469 if( db->mallocFailed==0 ){ in dbReallocFinish()
31485 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); in dbReallocFinish()
31546 ** at least one non-space character */ in sqlite3DbSpanDup()
31550 n = (int)(zEnd - zStart); in sqlite3DbSpanDup()
31551 while( sqlite3Isspace(zStart[n-1]) ) n--; in sqlite3DbSpanDup()
31565 ** Call this routine to record the fact that an OOM (out-of-memory) error
31566 ** has happened. This routine will set db->mallocFailed, and also
31574 ** and thereby avoid unnecessary stack frame allocations for the overwhelmingly
31578 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){ in sqlite3OomFault()
31579 db->mallocFailed = 1; in sqlite3OomFault()
31580 if( db->nVdbeExec>0 ){ in sqlite3OomFault()
31581 AtomicStore(&db->u1.isInterrupted, 1); in sqlite3OomFault()
31584 if( db->pParse ){ in sqlite3OomFault()
31586 sqlite3ErrorMsg(db->pParse, "out of memory"); in sqlite3OomFault()
31587 db->pParse->rc = SQLITE_NOMEM_BKPT; in sqlite3OomFault()
31588 for(pParse=db->pParse->pOuterParse; pParse; pParse = pParse->pOuterParse){ in sqlite3OomFault()
31589 pParse->nErr++; in sqlite3OomFault()
31590 pParse->rc = SQLITE_NOMEM; in sqlite3OomFault()
31599 ** db->mallocFailed flag as necessary.
31605 if( db->mallocFailed && db->nVdbeExec==0 ){ in sqlite3OomClear()
31606 db->mallocFailed = 0; in sqlite3OomClear()
31607 AtomicStore(&db->u1.isInterrupted, 0); in sqlite3OomClear()
31608 assert( db->lookaside.bDisable>0 ); in sqlite3OomClear()
31617 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ in apiHandleError()
31622 return rc & db->errMask; in apiHandleError()
31634 ** If an OOM as occurred, then the connection error-code (the value
31639 ** Otherwise the read (and possible write) of db->mallocFailed in sqlite3ApiExit()
31643 assert( sqlite3_mutex_held(db->mutex) ); in sqlite3ApiExit()
31644 if( db->mallocFailed || rc ){ in sqlite3ApiExit()
31658 ** This file contains code for a set of "printf"-like routines. These
31669 #define etRADIX 0 /* non-decimal integer types. %x %o */
31685 #define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */
31686 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31693 ** An "etByte" is an 8-bit unsigned value.
31722 static const char aPrefix[] = "-x0\000X0";
31764 p->accError = eError; in sqlite3StrAccumSetError()
31765 if( p->mxAlloc ) sqlite3_str_reset(p); in sqlite3StrAccumSetError()
31766 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError); in sqlite3StrAccumSetError()
31773 if( p->nArg<=p->nUsed ) return 0; in getIntArg()
31774 return sqlite3_value_int64(p->apArg[p->nUsed++]); in getIntArg()
31777 if( p->nArg<=p->nUsed ) return 0.0; in getDoubleArg()
31778 return sqlite3_value_double(p->apArg[p->nUsed++]); in getDoubleArg()
31781 if( p->nArg<=p->nUsed ) return 0; in getTextArg()
31782 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); in getTextArg()
31796 if( pAccum->accError ) return 0; in printfTempBuf()
31797 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){ in printfTempBuf()
31801 z = sqlite3DbMallocRaw(pAccum->db, n); in printfTempBuf()
31818 ** Hard limit on the precision of floating-point conversions.
31835 int length; /* Length of the field */ in sqlite3_str_vappendf() local
31838 etByte flag_leftjustify; /* True if "-" flag is present */ in sqlite3_str_vappendf()
31848 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ in sqlite3_str_vappendf()
31865 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
31868 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ in sqlite3_str_vappendf()
31882 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt)); in sqlite3_str_vappendf()
31895 precision = -1; in sqlite3_str_vappendf()
31898 case '-': flag_leftjustify = 1; break; in sqlite3_str_vappendf()
31918 unsigned wx = c - '0'; in sqlite3_str_vappendf()
31920 wx = wx*10 + c - '0'; in sqlite3_str_vappendf()
31932 fmt--; in sqlite3_str_vappendf()
31944 width = width >= -2147483647 ? -width : 0; in sqlite3_str_vappendf()
31966 precision = precision >= -2147483647 ? -precision : -1; in sqlite3_str_vappendf()
31972 px = px*10 + c - '0'; in sqlite3_str_vappendf()
31984 --fmt; in sqlite3_str_vappendf()
31999 xtype = infop->type; in sqlite3_str_vappendf()
32010 ** flag_leftjustify TRUE if a '-' is present or if the in sqlite3_str_vappendf()
32015 ** always non-negative. Zero is the default. in sqlite3_str_vappendf()
32017 ** is -1. in sqlite3_str_vappendf()
32022 assert( precision>=(-1) ); in sqlite3_str_vappendf()
32033 if( infop->flags & FLAG_SIGNED ){ in sqlite3_str_vappendf()
32048 testcase( v==(-1) ); in sqlite3_str_vappendf()
32051 prefix = '-'; in sqlite3_str_vappendf()
32071 if( flag_zeropad && precision<width-(prefix!=0) ){ in sqlite3_str_vappendf()
32072 precision = width-(prefix!=0); in sqlite3_str_vappendf()
32074 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){ in sqlite3_str_vappendf()
32085 bufpt = &zOut[nOut-1]; in sqlite3_str_vappendf()
32092 *(--bufpt) = zOrd[x*2+1]; in sqlite3_str_vappendf()
32093 *(--bufpt) = zOrd[x*2]; in sqlite3_str_vappendf()
32096 const char *cset = &aDigits[infop->charset]; in sqlite3_str_vappendf()
32097 u8 base = infop->base; in sqlite3_str_vappendf()
32099 *(--bufpt) = cset[longvalue%base]; in sqlite3_str_vappendf()
32103 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
32104 while( precision>length ){ in sqlite3_str_vappendf()
32105 *(--bufpt) = '0'; /* Zero pad */ in sqlite3_str_vappendf()
32106 length++; in sqlite3_str_vappendf()
32109 int nn = (length - 1)/3; /* Number of "," to insert */ in sqlite3_str_vappendf()
32110 int ix = (length - 1)%3 + 1; in sqlite3_str_vappendf()
32111 bufpt -= nn; in sqlite3_str_vappendf()
32114 ix--; in sqlite3_str_vappendf()
32117 nn--; in sqlite3_str_vappendf()
32122 if( prefix ) *(--bufpt) = prefix; /* Add sign */ in sqlite3_str_vappendf()
32123 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ in sqlite3_str_vappendf()
32126 pre = &aPrefix[infop->prefix]; in sqlite3_str_vappendf()
32127 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; in sqlite3_str_vappendf()
32129 length = (int)(&zOut[nOut-1]-bufpt); in sqlite3_str_vappendf()
32150 iRound = -precision; in sqlite3_str_vappendf()
32161 length = sqlite3Strlen30(bufpt); in sqlite3_str_vappendf()
32168 memcpy(buf, "-Inf", 5); in sqlite3_str_vappendf()
32170 if( s.sign=='-' ){ in sqlite3_str_vappendf()
32171 /* no-op */ in sqlite3_str_vappendf()
32177 length = sqlite3Strlen30(bufpt); in sqlite3_str_vappendf()
32181 if( s.sign=='-' ){ in sqlite3_str_vappendf()
32182 prefix = '-'; in sqlite3_str_vappendf()
32187 exp = s.iDP-1; in sqlite3_str_vappendf()
32195 precision--; in sqlite3_str_vappendf()
32197 if( exp<-4 || exp>precision ){ in sqlite3_str_vappendf()
32200 precision = precision - exp; in sqlite3_str_vappendf()
32209 e2 = s.iDP - 1; in sqlite3_str_vappendf()
32232 for(; e2>=0; e2--){ in sqlite3_str_vappendf()
32243 for(e2++; e2<0 && precision>0; precision--, e2++){ in sqlite3_str_vappendf()
32247 while( (precision--)>0 ){ in sqlite3_str_vappendf()
32252 while( bufpt[-1]=='0' ) *(--bufpt) = 0; in sqlite3_str_vappendf()
32254 if( bufpt[-1]=='.' ){ in sqlite3_str_vappendf()
32258 *(--bufpt) = 0; in sqlite3_str_vappendf()
32264 exp = s.iDP - 1; in sqlite3_str_vappendf()
32265 *(bufpt++) = aDigits[infop->charset]; in sqlite3_str_vappendf()
32267 *(bufpt++) = '-'; exp = -exp; in sqlite3_str_vappendf()
32283 length = (int)(bufpt-zOut); in sqlite3_str_vappendf()
32288 if( flag_zeropad && !flag_leftjustify && length < width){ in sqlite3_str_vappendf()
32290 int nPad = width - length; in sqlite3_str_vappendf()
32291 for(i=width; i>=nPad; i--){ in sqlite3_str_vappendf()
32292 bufpt[i] = bufpt[i-nPad]; in sqlite3_str_vappendf()
32295 while( nPad-- ) bufpt[i++] = '0'; in sqlite3_str_vappendf()
32296 length = width; in sqlite3_str_vappendf()
32302 *(va_arg(ap,int*)) = pAccum->nChar; in sqlite3_str_vappendf()
32304 length = width = 0; in sqlite3_str_vappendf()
32309 length = 1; in sqlite3_str_vappendf()
32314 length = 1; in sqlite3_str_vappendf()
32318 while( length<4 && (bufpt[0]&0xc0)==0x80 ){ in sqlite3_str_vappendf()
32319 buf[length++] = *(bufpt++); in sqlite3_str_vappendf()
32327 length = sqlite3AppendOneUtf8Character(buf, ch); in sqlite3_str_vappendf()
32331 width -= precision-1; in sqlite3_str_vappendf()
32333 sqlite3_str_appendchar(pAccum, width-1, ' '); in sqlite3_str_vappendf()
32336 sqlite3_str_append(pAccum, buf, length); in sqlite3_str_vappendf()
32337 precision--; in sqlite3_str_vappendf()
32340 if( nPrior > precision-1 ) nPrior = precision - 1; in sqlite3_str_vappendf()
32341 nCopyBytes = length*nPrior; in sqlite3_str_vappendf()
32342 if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ in sqlite3_str_vappendf()
32345 if( pAccum->accError ) break; in sqlite3_str_vappendf()
32347 &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); in sqlite3_str_vappendf()
32348 precision -= nPrior; in sqlite3_str_vappendf()
32366 if( pAccum->nChar==0 in sqlite3_str_vappendf()
32367 && pAccum->mxAlloc in sqlite3_str_vappendf()
32370 && pAccum->accError==0 in sqlite3_str_vappendf()
32375 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); in sqlite3_str_vappendf()
32376 pAccum->zText = bufpt; in sqlite3_str_vappendf()
32377 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); in sqlite3_str_vappendf()
32378 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
32379 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3_str_vappendf()
32380 length = 0; in sqlite3_str_vappendf()
32387 /* Set length to the number of bytes needed in order to display in sqlite3_str_vappendf()
32390 while( precision-- > 0 && z[0] ){ in sqlite3_str_vappendf()
32393 length = (int)(z - (unsigned char*)bufpt); in sqlite3_str_vappendf()
32395 for(length=0; length<precision && bufpt[length]; length++){} in sqlite3_str_vappendf()
32398 length = 0x7fffffff & (int)strlen(bufpt); in sqlite3_str_vappendf()
32402 /* Adjust width to account for extra bytes in UTF-8 characters */ in sqlite3_str_vappendf()
32403 int ii = length - 1; in sqlite3_str_vappendf()
32404 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++; in sqlite3_str_vappendf()
32438 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ in sqlite3_str_vappendf()
32445 /* For %#q, do unistr()-style backslash escapes for in sqlite3_str_vappendf()
32493 bufpt[j-1] = '\\'; in sqlite3_str_vappendf()
32512 length = j; in sqlite3_str_vappendf()
32516 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
32521 sqlite3_str_appendall(pAccum, (const char*)pExpr->u.zToken); in sqlite3_str_vappendf()
32522 sqlite3RecordErrorOffsetOfExpr(pAccum->db, pExpr); in sqlite3_str_vappendf()
32528 if( pToken && pToken->n ){ in sqlite3_str_vappendf()
32529 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n); in sqlite3_str_vappendf()
32530 sqlite3RecordErrorByteOffset(pAccum->db, pToken->z); in sqlite3_str_vappendf()
32533 length = width = 0; in sqlite3_str_vappendf()
32538 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return; in sqlite3_str_vappendf()
32541 if( pItem->zAlias && !flag_altform2 ){ in sqlite3_str_vappendf()
32542 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
32543 }else if( pItem->zName ){ in sqlite3_str_vappendf()
32544 if( pItem->fg.fixedSchema==0 in sqlite3_str_vappendf()
32545 && pItem->fg.isSubquery==0 in sqlite3_str_vappendf()
32546 && pItem->u4.zDatabase!=0 in sqlite3_str_vappendf()
32548 sqlite3_str_appendall(pAccum, pItem->u4.zDatabase); in sqlite3_str_vappendf()
32551 sqlite3_str_appendall(pAccum, pItem->zName); in sqlite3_str_vappendf()
32552 }else if( pItem->zAlias ){ in sqlite3_str_vappendf()
32553 sqlite3_str_appendall(pAccum, pItem->zAlias); in sqlite3_str_vappendf()
32554 }else if( ALWAYS(pItem->fg.isSubquery) ){/* Because of tag-20240424-1 */ in sqlite3_str_vappendf()
32555 Select *pSel = pItem->u4.pSubq->pSelect; in sqlite3_str_vappendf()
32557 if( pSel->selFlags & SF_NestedFrom ){ in sqlite3_str_vappendf()
32558 sqlite3_str_appendf(pAccum, "(join-%u)", pSel->selId); in sqlite3_str_vappendf()
32559 }else if( pSel->selFlags & SF_MultiValue ){ in sqlite3_str_vappendf()
32560 assert( !pItem->fg.isTabFunc && !pItem->fg.isIndexedBy ); in sqlite3_str_vappendf()
32561 sqlite3_str_appendf(pAccum, "%u-ROW VALUES CLAUSE", in sqlite3_str_vappendf()
32562 pItem->u1.nRow); in sqlite3_str_vappendf()
32564 sqlite3_str_appendf(pAccum, "(subquery-%u)", pSel->selId); in sqlite3_str_vappendf()
32567 length = width = 0; in sqlite3_str_vappendf()
32577 ** "length" characters long. The field width is "width". Do in sqlite3_str_vappendf()
32578 ** the output. Both length and width are in bytes, not characters, in sqlite3_str_vappendf()
32583 width -= length; in sqlite3_str_vappendf()
32586 sqlite3_str_append(pAccum, bufpt, length); in sqlite3_str_vappendf()
32589 sqlite3_str_append(pAccum, bufpt, length); in sqlite3_str_vappendf()
32593 sqlite3DbFree(pAccum->db, zExtra); in sqlite3_str_vappendf()
32612 if( db->errByteOffset!=(-2) ) return; in sqlite3RecordErrorByteOffset()
32613 pParse = db->pParse; in sqlite3RecordErrorByteOffset()
32615 zText =pParse->zTail; in sqlite3RecordErrorByteOffset()
32619 db->errByteOffset = (int)(z-zText); in sqlite3RecordErrorByteOffset()
32629 && (ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) || pExpr->w.iOfst<=0) in sqlite3RecordErrorOffsetOfExpr()
32631 pExpr = pExpr->pLeft; in sqlite3RecordErrorOffsetOfExpr()
32635 db->errByteOffset = pExpr->w.iOfst; in sqlite3RecordErrorOffsetOfExpr()
32647 assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ in sqlite3StrAccumEnlarge()
32648 if( p->accError ){ in sqlite3StrAccumEnlarge()
32649 testcase(p->accError==SQLITE_TOOBIG); in sqlite3StrAccumEnlarge()
32650 testcase(p->accError==SQLITE_NOMEM); in sqlite3StrAccumEnlarge()
32653 if( p->mxAlloc==0 ){ in sqlite3StrAccumEnlarge()
32655 return p->nAlloc - p->nChar - 1; in sqlite3StrAccumEnlarge()
32657 char *zOld = isMalloced(p) ? p->zText : 0; in sqlite3StrAccumEnlarge()
32658 i64 szNew = p->nChar + N + 1; in sqlite3StrAccumEnlarge()
32659 if( szNew+p->nChar<=p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32662 szNew += p->nChar; in sqlite3StrAccumEnlarge()
32664 if( szNew > p->mxAlloc ){ in sqlite3StrAccumEnlarge()
32669 p->nAlloc = (int)szNew; in sqlite3StrAccumEnlarge()
32671 if( p->db ){ in sqlite3StrAccumEnlarge()
32672 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32674 zNew = sqlite3Realloc(zOld, p->nAlloc); in sqlite3StrAccumEnlarge()
32677 assert( p->zText!=0 || p->nChar==0 ); in sqlite3StrAccumEnlarge()
32678 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); in sqlite3StrAccumEnlarge()
32679 p->zText = zNew; in sqlite3StrAccumEnlarge()
32680 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); in sqlite3StrAccumEnlarge()
32681 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in sqlite3StrAccumEnlarge()
32696 testcase( p->nChar + (i64)N > 0x7fffffff ); in sqlite3_str_appendchar()
32697 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ in sqlite3_str_appendchar()
32700 while( (N--)>0 ) p->zText[p->nChar++] = c; in sqlite3_str_appendchar()
32707 ** This is a helper routine to sqlite3_str_append() that does special-case
32714 memcpy(&p->zText[p->nChar], z, N); in enlargeAndAppend()
32715 p->nChar += N; in enlargeAndAppend()
32725 assert( p->zText!=0 || p->nChar==0 || p->accError ); in sqlite3_str_append()
32727 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 ); in sqlite3_str_append()
32728 if( p->nChar+N >= p->nAlloc ){ in sqlite3_str_append()
32731 assert( p->zText ); in sqlite3_str_append()
32732 p->nChar += N; in sqlite3_str_append()
32733 memcpy(&p->zText[p->nChar-N], z, N); in sqlite3_str_append()
32738 ** Append the complete text of zero-terminated string z[] to the p string.
32746 ** Finish off a string by making sure it is zero-terminated.
32752 assert( p->mxAlloc>0 && !isMalloced(p) ); in strAccumFinishRealloc()
32753 zText = sqlite3DbMallocRaw(p->db, 1+(u64)p->nChar ); in strAccumFinishRealloc()
32755 memcpy(zText, p->zText, p->nChar+1); in strAccumFinishRealloc()
32756 p->printfFlags |= SQLITE_PRINTF_MALLOCED; in strAccumFinishRealloc()
32760 p->zText = zText; in strAccumFinishRealloc()
32764 if( p->zText ){ in sqlite3StrAccumFinish()
32765 p->zText[p->nChar] = 0; in sqlite3StrAccumFinish()
32766 if( p->mxAlloc>0 && !isMalloced(p) ){ in sqlite3StrAccumFinish()
32770 return p->zText; in sqlite3StrAccumFinish()
32778 if( p->accError ){ in sqlite3ResultStrAccum()
32779 sqlite3_result_error_code(pCtx, p->accError); in sqlite3ResultStrAccum()
32782 sqlite3_result_text(pCtx, p->zText, p->nChar, SQLITE_DYNAMIC); in sqlite3ResultStrAccum()
32814 return p ? p->accError : SQLITE_NOMEM; in sqlite3_str_errcode()
32817 /* Return the current length of p in bytes */
32819 return p ? p->nChar : 0; in sqlite3_str_length()
32824 if( p==0 || p->nChar==0 ) return 0; in sqlite3_str_value()
32825 p->zText[p->nChar] = 0; in sqlite3_str_value()
32826 return p->zText; in sqlite3_str_value()
32834 sqlite3DbFree(p->db, p->zText); in sqlite3_str_reset()
32835 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; in sqlite3_str_reset()
32837 p->nAlloc = 0; in sqlite3_str_reset()
32838 p->nChar = 0; in sqlite3_str_reset()
32839 p->zText = 0; in sqlite3_str_reset()
32847 ** memory is used if not NULL. db->mallocFailed is set appropriately
32857 p->zText = zBase; in sqlite3StrAccumInit()
32858 p->db = db; in sqlite3StrAccumInit()
32859 p->nAlloc = n; in sqlite3StrAccumInit()
32860 p->mxAlloc = mx; in sqlite3StrAccumInit()
32861 p->nChar = 0; in sqlite3StrAccumInit()
32862 p->accError = 0; in sqlite3StrAccumInit()
32863 p->printfFlags = 0; in sqlite3StrAccumInit()
32871 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); in sqlite3_str_new()
32880 ** %-conversion extensions.
32888 db->aLimit[SQLITE_LIMIT_LENGTH]); in sqlite3VMPrintf()
32900 ** %-conversion extensions.
32913 ** %-conversion extensions.
32937 ** %-conversion extensions.
33010 ** stack space on small-stack systems when logging is disabled.
33072 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
33095 p--; in sqlite3RCStrRef()
33096 p->nRCRef++; in sqlite3RCStrRef()
33107 p--; in sqlite3RCStrUnref()
33108 assert( p->nRCRef>0 ); in sqlite3RCStrUnref()
33109 if( p->nRCRef>=2 ){ in sqlite3RCStrUnref()
33110 p->nRCRef--; in sqlite3RCStrUnref()
33128 p->nRCRef = 1; in sqlite3RCStrNew()
33140 p--; in sqlite3RCStrResize()
33141 assert( p->nRCRef==1 ); in sqlite3RCStrResize()
33154 ** 2015-06-08
33186 p->iLevel++; in sqlite3TreeViewPush()
33189 if( p->iLevel<(int)sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; in sqlite3TreeViewPush()
33198 p->iLevel--; in sqlite3TreeViewPop()
33199 if( p->iLevel<0 ){ in sqlite3TreeViewPop()
33216 for(i=0; i<p->iLevel && i<(int)sizeof(p->bLine)-1; i++){ in sqlite3TreeViewLine()
33217 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4); in sqlite3TreeViewLine()
33219 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); in sqlite3TreeViewLine()
33255 int colMoreToFollow = i<(nCol - 1); in sqlite3TreeViewColumnList()
33270 printf(" X-%s", z); in sqlite3TreeViewColumnList()
33278 if( flg & COLFLAG_NOEXPAND ) printf(" NO-EXPAND"); in sqlite3TreeViewColumnList()
33289 ** Generate a human-readable description of a WITH clause.
33294 if( pWith->nCte==0 ) return; in sqlite3TreeViewWith()
33295 if( pWith->pOuter ){ in sqlite3TreeViewWith()
33296 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); in sqlite3TreeViewWith()
33300 if( pWith->nCte>0 ){ in sqlite3TreeViewWith()
33302 for(i=0; i<pWith->nCte; i++){ in sqlite3TreeViewWith()
33305 const struct Cte *pCte = &pWith->a[i]; in sqlite3TreeViewWith()
33307 sqlite3_str_appendf(&x, "%s", pCte->zName); in sqlite3TreeViewWith()
33308 if( pCte->pCols && pCte->pCols->nExpr>0 ){ in sqlite3TreeViewWith()
33311 for(j=0; j<pCte->pCols->nExpr; j++){ in sqlite3TreeViewWith()
33312 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName); in sqlite3TreeViewWith()
33317 if( pCte->eM10d!=M10d_Any ){ in sqlite3TreeViewWith()
33319 pCte->eM10d==M10d_No ? "NOT " : ""); in sqlite3TreeViewWith()
33321 if( pCte->pUse ){ in sqlite3TreeViewWith()
33322 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse, in sqlite3TreeViewWith()
33323 pCte->pUse->nUse); in sqlite3TreeViewWith()
33326 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); in sqlite3TreeViewWith()
33327 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); in sqlite3TreeViewWith()
33335 ** Generate a human-readable description of a SrcList object.
33340 for(i=0; i<pSrc->nSrc; i++){ in sqlite3TreeViewSrcList()
33341 const SrcItem *pItem = &pSrc->a[i]; in sqlite3TreeViewSrcList()
33347 sqlite3_str_appendf(&x, "{%d:*} %!S", pItem->iCursor, pItem); in sqlite3TreeViewSrcList()
33348 if( pItem->pSTab ){ in sqlite3TreeViewSrcList()
33350 pItem->pSTab->zName, pItem->pSTab->nCol, pItem->pSTab, in sqlite3TreeViewSrcList()
33351 pItem->colUsed, in sqlite3TreeViewSrcList()
33352 pItem->fg.rowidUsed ? "+rowid" : ""); in sqlite3TreeViewSrcList()
33354 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==(JT_LEFT|JT_RIGHT) ){ in sqlite3TreeViewSrcList()
33355 sqlite3_str_appendf(&x, " FULL-OUTER-JOIN"); in sqlite3TreeViewSrcList()
33356 }else if( pItem->fg.jointype & JT_LEFT ){ in sqlite3TreeViewSrcList()
33357 sqlite3_str_appendf(&x, " LEFT-JOIN"); in sqlite3TreeViewSrcList()
33358 }else if( pItem->fg.jointype & JT_RIGHT ){ in sqlite3TreeViewSrcList()
33359 sqlite3_str_appendf(&x, " RIGHT-JOIN"); in sqlite3TreeViewSrcList()
33360 }else if( pItem->fg.jointype & JT_CROSS ){ in sqlite3TreeViewSrcList()
33361 sqlite3_str_appendf(&x, " CROSS-JOIN"); in sqlite3TreeViewSrcList()
33363 if( pItem->fg.jointype & JT_LTORJ ){ in sqlite3TreeViewSrcList()
33366 if( pItem->fg.fromDDL ){ in sqlite3TreeViewSrcList()
33369 if( pItem->fg.isCte ){ in sqlite3TreeViewSrcList()
33370 static const char *aMat[] = {",MAT", "", ",NO-MAT"}; in sqlite3TreeViewSrcList()
33372 pItem->u2.pCteUse->nUse, in sqlite3TreeViewSrcList()
33373 aMat[pItem->u2.pCteUse->eM10d]); in sqlite3TreeViewSrcList()
33375 if( pItem->fg.isOn || (pItem->fg.isUsing==0 && pItem->u3.pOn!=0) ){ in sqlite3TreeViewSrcList()
33378 if( pItem->fg.isTabFunc ) sqlite3_str_appendf(&x, " isTabFunc"); in sqlite3TreeViewSrcList()
33379 if( pItem->fg.isCorrelated ) sqlite3_str_appendf(&x, " isCorrelated"); in sqlite3TreeViewSrcList()
33380 if( pItem->fg.isMaterialized ) sqlite3_str_appendf(&x, " isMaterialized"); in sqlite3TreeViewSrcList()
33381 if( pItem->fg.viaCoroutine ) sqlite3_str_appendf(&x, " viaCoroutine"); in sqlite3TreeViewSrcList()
33382 if( pItem->fg.notCte ) sqlite3_str_appendf(&x, " notCte"); in sqlite3TreeViewSrcList()
33383 if( pItem->fg.isNestedFrom ) sqlite3_str_appendf(&x, " isNestedFrom"); in sqlite3TreeViewSrcList()
33384 if( pItem->fg.fixedSchema ) sqlite3_str_appendf(&x, " fixedSchema"); in sqlite3TreeViewSrcList()
33385 if( pItem->fg.hadSchema ) sqlite3_str_appendf(&x, " hadSchema"); in sqlite3TreeViewSrcList()
33386 if( pItem->fg.isSubquery ) sqlite3_str_appendf(&x, " isSubquery"); in sqlite3TreeViewSrcList()
33389 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1); in sqlite3TreeViewSrcList()
33391 if( pItem->fg.isSubquery ) n++; in sqlite3TreeViewSrcList()
33392 if( pItem->fg.isTabFunc ) n++; in sqlite3TreeViewSrcList()
33393 if( pItem->fg.isUsing ) n++; in sqlite3TreeViewSrcList()
33394 if( pItem->fg.isUsing ){ in sqlite3TreeViewSrcList()
33395 sqlite3TreeViewIdList(pView, pItem->u3.pUsing, (--n)>0, "USING"); in sqlite3TreeViewSrcList()
33397 if( pItem->fg.isSubquery ){ in sqlite3TreeViewSrcList()
33399 if( pItem->pSTab ){ in sqlite3TreeViewSrcList()
33400 Table *pTab = pItem->pSTab; in sqlite3TreeViewSrcList()
33401 sqlite3TreeViewColumnList(pView, pTab->aCol, pTab->nCol, 1); in sqlite3TreeViewSrcList()
33403 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) ); in sqlite3TreeViewSrcList()
33404 sqlite3TreeViewSelect(pView, pItem->u4.pSubq->pSelect, 0); in sqlite3TreeViewSrcList()
33406 if( pItem->fg.isTabFunc ){ in sqlite3TreeViewSrcList()
33407 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); in sqlite3TreeViewSrcList()
33414 ** Generate a human-readable description of a Select object.
33420 sqlite3TreeViewLine(pView, "nil-SELECT"); in sqlite3TreeViewSelect()
33424 if( p->pWith ){ in sqlite3TreeViewSelect()
33425 sqlite3TreeViewWith(pView, p->pWith, 1); in sqlite3TreeViewSelect()
33430 if( p->selFlags & SF_WhereBegin ){ in sqlite3TreeViewSelect()
33435 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), in sqlite3TreeViewSelect()
33436 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), in sqlite3TreeViewSelect()
33437 p->selId, p, p->selFlags, in sqlite3TreeViewSelect()
33438 (int)p->nSelectRow in sqlite3TreeViewSelect()
33442 if( p->pPrior ){ in sqlite3TreeViewSelect()
33446 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ) n++; in sqlite3TreeViewSelect()
33447 if( p->pWhere ) n++; in sqlite3TreeViewSelect()
33448 if( p->pGroupBy ) n++; in sqlite3TreeViewSelect()
33449 if( p->pHaving ) n++; in sqlite3TreeViewSelect()
33450 if( p->pOrderBy ) n++; in sqlite3TreeViewSelect()
33451 if( p->pLimit ) n++; in sqlite3TreeViewSelect()
33453 if( p->pWin ) n++; in sqlite3TreeViewSelect()
33454 if( p->pWinDefn ) n++; in sqlite3TreeViewSelect()
33457 if( p->pEList ){ in sqlite3TreeViewSelect()
33458 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set"); in sqlite3TreeViewSelect()
33460 n--; in sqlite3TreeViewSelect()
33462 if( p->pWin ){ in sqlite3TreeViewSelect()
33464 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
33465 sqlite3TreeViewLine(pView, "window-functions"); in sqlite3TreeViewSelect()
33466 for(pX=p->pWin; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
33467 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
33472 if( p->pSrc && p->pSrc->nSrc && p->pSrc->nAlloc ){ in sqlite3TreeViewSelect()
33473 sqlite3TreeViewPush(&pView, (n--)>0); in sqlite3TreeViewSelect()
33475 sqlite3TreeViewSrcList(pView, p->pSrc); in sqlite3TreeViewSelect()
33478 if( p->pWhere ){ in sqlite3TreeViewSelect()
33479 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewSelect()
33480 sqlite3TreeViewExpr(pView, p->pWhere, 0); in sqlite3TreeViewSelect()
33483 if( p->pGroupBy ){ in sqlite3TreeViewSelect()
33484 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); in sqlite3TreeViewSelect()
33486 if( p->pHaving ){ in sqlite3TreeViewSelect()
33487 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); in sqlite3TreeViewSelect()
33488 sqlite3TreeViewExpr(pView, p->pHaving, 0); in sqlite3TreeViewSelect()
33492 if( p->pWinDefn ){ in sqlite3TreeViewSelect()
33494 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0); in sqlite3TreeViewSelect()
33495 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){ in sqlite3TreeViewSelect()
33496 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0); in sqlite3TreeViewSelect()
33501 if( p->pOrderBy ){ in sqlite3TreeViewSelect()
33502 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); in sqlite3TreeViewSelect()
33504 if( p->pLimit ){ in sqlite3TreeViewSelect()
33505 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); in sqlite3TreeViewSelect()
33506 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0); in sqlite3TreeViewSelect()
33507 if( p->pLimit->pRight ){ in sqlite3TreeViewSelect()
33509 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0); in sqlite3TreeViewSelect()
33514 if( p->pPrior ){ in sqlite3TreeViewSelect()
33516 switch( p->op ){ in sqlite3TreeViewSelect()
33523 p = p->pPrior; in sqlite3TreeViewSelect()
33567 ** Generate a human-readable explanation for a Window object
33572 if( pWin->pFilter ){ in sqlite3TreeViewWindow()
33574 sqlite3TreeViewExpr(pView, pWin->pFilter, 0); in sqlite3TreeViewWindow()
33576 if( pWin->eFrmType==TK_FILTER ) return; in sqlite3TreeViewWindow()
33579 if( pWin->zName ){ in sqlite3TreeViewWindow()
33580 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin); in sqlite3TreeViewWindow()
33584 if( pWin->zBase ) nElement++; in sqlite3TreeViewWindow()
33585 if( pWin->pOrderBy ) nElement++; in sqlite3TreeViewWindow()
33586 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++; in sqlite3TreeViewWindow()
33587 if( pWin->eExclude ) nElement++; in sqlite3TreeViewWindow()
33588 if( pWin->zBase ){ in sqlite3TreeViewWindow()
33589 sqlite3TreeViewPush(&pView, (--nElement)>0); in sqlite3TreeViewWindow()
33590 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase); in sqlite3TreeViewWindow()
33593 if( pWin->pPartition ){ in sqlite3TreeViewWindow()
33594 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY"); in sqlite3TreeViewWindow()
33596 if( pWin->pOrderBy ){ in sqlite3TreeViewWindow()
33597 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY"); in sqlite3TreeViewWindow()
33599 if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){ in sqlite3TreeViewWindow()
33602 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE"; in sqlite3TreeViewWindow()
33603 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS"; in sqlite3TreeViewWindow()
33605 pWin->bImplicitFrame ? " (implied)" : ""); in sqlite3TreeViewWindow()
33606 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0); in sqlite3TreeViewWindow()
33607 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1); in sqlite3TreeViewWindow()
33608 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0); in sqlite3TreeViewWindow()
33611 if( pWin->eExclude ){ in sqlite3TreeViewWindow()
33614 switch( pWin->eExclude ){ in sqlite3TreeViewWindow()
33620 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude); in sqlite3TreeViewWindow()
33634 ** Generate a human-readable explanation for a Window Function object
33640 pWin->pWFunc->zName, pWin->pWFunc->nArg); in sqlite3TreeViewWinFunc()
33647 ** Generate a human-readable explanation of an expression tree.
33659 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags || pExpr->pAggInfo ){ in sqlite3TreeViewExpr()
33663 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n'); in sqlite3TreeViewExpr()
33665 sqlite3_str_appendf(&x, " outer.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33668 sqlite3_str_appendf(&x, " inner.iJoin=%d", pExpr->w.iJoin); in sqlite3TreeViewExpr()
33676 if( pExpr->pAggInfo!=0 ){ in sqlite3TreeViewExpr()
33677 sqlite3_str_appendf(&x, " agg-column[%d]", pExpr->iAgg); in sqlite3TreeViewExpr()
33683 switch( pExpr->op ){ in sqlite3TreeViewExpr()
33686 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33690 if( pExpr->iTable<0 ){ in sqlite3TreeViewExpr()
33693 if( pExpr->op2 ){ in sqlite3TreeViewExpr()
33694 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33699 pExpr->iColumn, zFlgs, zOp2); in sqlite3TreeViewExpr()
33703 pExpr->iTable, pExpr->iColumn, in sqlite3TreeViewExpr()
33704 pExpr->y.pTab, zFlgs); in sqlite3TreeViewExpr()
33707 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33712 if( pExpr->flags & EP_IntValue ){ in sqlite3TreeViewExpr()
33713 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); in sqlite3TreeViewExpr()
33715 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33722 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33728 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33743 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); in sqlite3TreeViewExpr()
33750 pExpr->u.zToken, pExpr->iColumn); in sqlite3TreeViewExpr()
33754 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); in sqlite3TreeViewExpr()
33759 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); in sqlite3TreeViewExpr()
33766 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33767 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33804 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE" in sqlite3TreeViewExpr()
33806 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT ); in sqlite3TreeViewExpr()
33807 assert( pExpr->pRight ); in sqlite3TreeViewExpr()
33808 assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op in sqlite3TreeViewExpr()
33810 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight); in sqlite3TreeViewExpr()
33817 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken); in sqlite3TreeViewExpr()
33818 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33825 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE in sqlite3TreeViewExpr()
33830 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "", in sqlite3TreeViewExpr()
33831 pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33832 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33845 pFarg = pExpr->x.pList; in sqlite3TreeViewExpr()
33847 pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0; in sqlite3TreeViewExpr()
33853 if( pExpr->op==TK_AGG_FUNCTION ){ in sqlite3TreeViewExpr()
33855 pExpr->op2, pExpr->u.zToken, zFlgs, in sqlite3TreeViewExpr()
33856 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0, in sqlite3TreeViewExpr()
33857 pExpr->iAgg, pExpr->pAggInfo); in sqlite3TreeViewExpr()
33858 }else if( pExpr->op2!=0 ){ in sqlite3TreeViewExpr()
33861 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2); in sqlite3TreeViewExpr()
33863 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck"; in sqlite3TreeViewExpr()
33864 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr"; in sqlite3TreeViewExpr()
33865 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx"; in sqlite3TreeViewExpr()
33866 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol"; in sqlite3TreeViewExpr()
33868 pExpr->u.zToken, zFlgs, zOp2); in sqlite3TreeViewExpr()
33870 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs); in sqlite3TreeViewExpr()
33873 sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33874 if( pExpr->pLeft ){ in sqlite3TreeViewExpr()
33875 Expr *pOB = pExpr->pLeft; in sqlite3TreeViewExpr()
33876 assert( pOB->op==TK_ORDER ); in sqlite3TreeViewExpr()
33878 sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY"); in sqlite3TreeViewExpr()
33889 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY"); in sqlite3TreeViewExpr()
33895 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33896 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33901 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33902 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33908 sqlite3_str_appendf(pStr, "IN flags=0x%x", pExpr->flags); in sqlite3TreeViewExpr()
33909 if( pExpr->iTable ) sqlite3_str_appendf(pStr, " iTable=%d",pExpr->iTable); in sqlite3TreeViewExpr()
33912 pExpr->y.sub.regReturn, pExpr->y.sub.iAddr); in sqlite3TreeViewExpr()
33917 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33919 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); in sqlite3TreeViewExpr()
33921 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33934 ** X is stored in pExpr->pLeft. in sqlite3TreeViewExpr()
33935 ** Y is stored in pExpr->pList->a[0].pExpr. in sqlite3TreeViewExpr()
33936 ** Z is stored in pExpr->pList->a[1].pExpr. in sqlite3TreeViewExpr()
33940 pX = pExpr->pLeft; in sqlite3TreeViewExpr()
33942 assert( pExpr->x.pList->nExpr==2 ); in sqlite3TreeViewExpr()
33943 pY = pExpr->x.pList->a[0].pExpr; in sqlite3TreeViewExpr()
33944 pZ = pExpr->x.pList->a[1].pExpr; in sqlite3TreeViewExpr()
33953 ** to a column in the new.* or old.* pseudo-tables available to in sqlite3TreeViewExpr()
33955 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn in sqlite3TreeViewExpr()
33956 ** is set to the column of the pseudo-table to read, or to -1 to in sqlite3TreeViewExpr()
33960 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); in sqlite3TreeViewExpr()
33965 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
33967 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); in sqlite3TreeViewExpr()
33973 switch( pExpr->affExpr ){ in sqlite3TreeViewExpr()
33981 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
33987 pExpr->iTable, pExpr->iColumn, zFlgs); in sqlite3TreeViewExpr()
33988 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
33994 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z); in sqlite3TreeViewExpr()
33999 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d of [0..%d]%s", in sqlite3TreeViewExpr()
34000 pExpr->iColumn, pExpr->iTable-1, in sqlite3TreeViewExpr()
34001 pExpr->pRight==pExpr->pLeft ? " (SELECT-owner)" : ""); in sqlite3TreeViewExpr()
34002 assert( ExprUseXSelect(pExpr->pLeft) ); in sqlite3TreeViewExpr()
34003 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0); in sqlite3TreeViewExpr()
34007 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable); in sqlite3TreeViewExpr()
34008 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
34015 tmp.op = pExpr->op2; in sqlite3TreeViewExpr()
34020 if( pExpr->iColumn<=0 ){ in sqlite3TreeViewExpr()
34024 pExpr->iColumn-1); in sqlite3TreeViewExpr()
34029 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); in sqlite3TreeViewExpr()
34035 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); in sqlite3TreeViewExpr()
34036 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); in sqlite3TreeViewExpr()
34039 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); in sqlite3TreeViewExpr()
34046 ** Generate a human-readable explanation of an expression list.
34059 for(i=0; i<pList->nExpr; i++){ in sqlite3TreeViewBareExprList()
34060 int j = pList->a[i].u.x.iOrderByCol; in sqlite3TreeViewBareExprList()
34061 u8 sortFlags = pList->a[i].fg.sortFlags; in sqlite3TreeViewBareExprList()
34062 char *zName = pList->a[i].zEName; in sqlite3TreeViewBareExprList()
34063 int moreToFollow = i<pList->nExpr - 1; in sqlite3TreeViewBareExprList()
34069 switch( pList->a[i].fg.eEName ){ in sqlite3TreeViewBareExprList()
34074 fprintf(stdout, "TABLE-ALIAS-NAME(\"%s\") ", zName); in sqlite3TreeViewBareExprList()
34075 if( pList->a[i].fg.bUsed ) fprintf(stdout, "(used) "); in sqlite3TreeViewBareExprList()
34076 if( pList->a[i].fg.bUsingTerm ) fprintf(stdout, "(USING-term) "); in sqlite3TreeViewBareExprList()
34077 if( pList->a[i].fg.bNoExpand ) fprintf(stdout, "(NoExpand) "); in sqlite3TreeViewBareExprList()
34090 fprintf(stdout, "NULLS-LAST"); in sqlite3TreeViewBareExprList()
34095 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow); in sqlite3TreeViewBareExprList()
34114 ** Generate a human-readable explanation of an id-list.
34127 for(i=0; i<pList->nId; i++){ in sqlite3TreeViewBareIdList()
34128 char *zName = pList->a[i].zName; in sqlite3TreeViewBareIdList()
34129 int moreToFollow = i<pList->nId - 1; in sqlite3TreeViewBareIdList()
34150 ** Generate a human-readable explanation of a list of Upsert objects
34161 sqlite3TreeViewPush(&pView, pUpsert->pNextUpsert!=0 || moreToFollow); in sqlite3TreeViewUpsert()
34163 pUpsert->isDoUpdate ? "UPDATE" : "NOTHING"); in sqlite3TreeViewUpsert()
34164 n = (pUpsert->pUpsertSet!=0) + (pUpsert->pUpsertWhere!=0); in sqlite3TreeViewUpsert()
34165 sqlite3TreeViewExprList(pView, pUpsert->pUpsertTarget, (n--)>0, "TARGET"); in sqlite3TreeViewUpsert()
34166 sqlite3TreeViewExprList(pView, pUpsert->pUpsertSet, (n--)>0, "SET"); in sqlite3TreeViewUpsert()
34167 if( pUpsert->pUpsertWhere ){ in sqlite3TreeViewUpsert()
34168 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); in sqlite3TreeViewUpsert()
34169 sqlite3TreeViewExpr(pView, pUpsert->pUpsertWhere, 0); in sqlite3TreeViewUpsert()
34173 pUpsert = pUpsert->pNextUpsert; in sqlite3TreeViewUpsert()
34180 ** Generate a human-readable diagram of the data structure that go
34202 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
34207 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
34213 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
34219 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewDelete()
34222 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewDelete()
34228 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewDelete()
34236 ** Generate a human-readable diagram of the data structure that go
34269 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
34274 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
34280 sqlite3TreeViewIdList(pView, pColumnList, (--n)>0, "COLUMNS"); in sqlite3TreeViewInsert()
34283 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
34284 sqlite3TreeViewLine(pView, "DATA-SOURCE"); in sqlite3TreeViewInsert()
34289 sqlite3TreeViewExprList(pView, pExprList, (--n)>0, "VALUES"); in sqlite3TreeViewInsert()
34292 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewInsert()
34298 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewInsert()
34306 ** Generate a human-readable diagram of the data structure that go
34341 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
34346 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
34352 sqlite3TreeViewExprList(pView, pChanges, (--n)>0, "SET"); in sqlite3TreeViewUpdate()
34355 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
34361 sqlite3TreeViewExprList(pView, pOrderBy, (--n)>0, "ORDER-BY"); in sqlite3TreeViewUpdate()
34364 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
34370 sqlite3TreeViewPush(&pView, (--n)>0); in sqlite3TreeViewUpdate()
34376 sqlite3TreeViewTrigger(pView, pTrigger, (--n)>0, 1); in sqlite3TreeViewUpdate()
34384 ** Show a human-readable graph of a TriggerStep
34395 moreToFollow || (showFullList && pStep->pNext!=0)); in sqlite3TreeViewTriggerStep()
34397 if( cnt++ && pStep->pNext==0 ){ in sqlite3TreeViewTriggerStep()
34401 sqlite3TreeViewLine(pView, "%s", pStep->zSpan ? pStep->zSpan : "RETURNING"); in sqlite3TreeViewTriggerStep()
34402 }while( showFullList && (pStep = pStep->pNext)!=0 ); in sqlite3TreeViewTriggerStep()
34407 ** Show a human-readable graph of a Trigger
34418 moreToFollow || (showFullList && pTrigger->pNext!=0)); in sqlite3TreeViewTrigger()
34420 if( cnt++ && pTrigger->pNext==0 ){ in sqlite3TreeViewTrigger()
34424 sqlite3TreeViewLine(pView, "TRIGGER %s", pTrigger->zName); in sqlite3TreeViewTrigger()
34426 sqlite3TreeViewTriggerStep(pView, pTrigger->step_list, 0, 1); in sqlite3TreeViewTrigger()
34428 }while( showFullList && (pTrigger = pTrigger->pNext)!=0 ); in sqlite3TreeViewTrigger()
34435 ** These simplified versions of the tree-view routines omit unnecessary
34485 ** This file contains code to implement a pseudo-random number
34504 /* The RFC-7539 ChaCha20 block function
34506 #define ROTL(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
34535 /* The "wsdPrng" macro will resolve to the pseudo-random number generator in sqlite3_randomness()
34537 ** we have to locate the state vector at run-time. In the more common in sqlite3_randomness()
34589 memcpy(zBuf, &wsdPrng.out[wsdPrng.n-N], N); in sqlite3_randomness()
34590 wsdPrng.n -= N; in sqlite3_randomness()
34595 N -= wsdPrng.n; in sqlite3_randomness()
34646 ** This file presents a simple cross-platform threading interface for
34659 ** single-threaded if desired.
34671 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34694 /* This routine is never used in single-threaded mode */ in sqlite3ThreadCreate()
34701 p->xTask = xTask; in sqlite3ThreadCreate()
34702 p->pIn = pIn; in sqlite3ThreadCreate()
34710 rc = pthread_create(&p->tid, 0, xTask, pIn); in sqlite3ThreadCreate()
34713 p->done = 1; in sqlite3ThreadCreate()
34714 p->pOut = xTask(pIn); in sqlite3ThreadCreate()
34726 if( p->done ){ in sqlite3ThreadJoin()
34727 *ppOut = p->pOut; in sqlite3ThreadJoin()
34730 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; in sqlite3ThreadJoin()
34743 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
34769 assert( p->id==GetCurrentThreadId() ); in sqlite3ThreadProc()
34771 assert( p->xTask!=0 ); in sqlite3ThreadProc()
34772 p->pResult = p->xTask(p->pIn); in sqlite3ThreadProc()
34799 p->xTask = xTask; in sqlite3ThreadCreate()
34800 p->pIn = pIn; in sqlite3ThreadCreate()
34801 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); in sqlite3ThreadCreate()
34802 if( p->tid==0 ){ in sqlite3ThreadCreate()
34806 if( p->xTask==0 ){ in sqlite3ThreadCreate()
34807 p->id = GetCurrentThreadId(); in sqlite3ThreadCreate()
34808 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34823 if( p->xTask==0 ){ in sqlite3ThreadJoin()
34824 /* assert( p->id==GetCurrentThreadId() ); */ in sqlite3ThreadJoin()
34826 assert( p->tid==0 ); in sqlite3ThreadJoin()
34828 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); in sqlite3ThreadJoin()
34829 rc = sqlite3Win32Wait((HANDLE)p->tid); in sqlite3ThreadJoin()
34831 bRc = CloseHandle((HANDLE)p->tid); in sqlite3ThreadJoin()
34834 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; in sqlite3ThreadJoin()
34843 /********************************* Single-Threaded **************************/
34872 p->xTask = xTask; in sqlite3ThreadCreate()
34873 p->pIn = pIn; in sqlite3ThreadCreate()
34875 p->xTask = 0; in sqlite3ThreadCreate()
34876 p->pResult = xTask(pIn); in sqlite3ThreadCreate()
34887 if( p->xTask ){ in sqlite3ThreadJoin()
34888 *ppOut = p->xTask(p->pIn); in sqlite3ThreadJoin()
34890 *ppOut = p->pResult; in sqlite3ThreadJoin()
34906 /****************************** End Single-Threaded *************************/
34922 ** This file contains routines used to translate between UTF-8,
34923 ** UTF-16, UTF-16BE, and UTF-16LE.
34925 ** Notes on UTF-8:
34927 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
34934 ** Notes on UTF-16: (with wwww+1==uuuuu)
34936 ** Word-0 Word-1 Value
34942 ** 0xff 0xfe little-endian utf-16 follows
34943 ** 0xfe 0xff big-endian utf-16 follows
34960 ** a multi-byte UTF8 character.
34999 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
35000 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
35011 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
35012 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
35048 ** Translate a single UTF-8 character. Return the unicode value.
35055 ** Notes On Invalid UTF-8:
35057 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
35058 ** be encoded as a multi-byte character. Any multi-byte character that
35062 ** If a multi-byte character attempts to encode a value between
35066 ** byte of a character are interpreted as single-byte characters
35070 ** * This routine accepts over-length UTF8 encodings
35071 ** for unicode values 0x80 and greater. It does not change over-length
35077 c = sqlite3Utf8Trans1[c-0xc0]; \
35091 ** For this routine, we assume the UTF8 string is always zero-terminated. in sqlite3Utf8Read()
35095 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8Read()
35108 ** more than n characters from the buffer. z[] is not zero-terminated.
35128 c = sqlite3Utf8Trans1[c-0xc0]; in sqlite3Utf8ReadLimited()
35153 sqlite3_int64 len; /* Maximum length of output string in bytes */ in sqlite3VdbeMemTranslate()
35160 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); in sqlite3VdbeMemTranslate()
35161 assert( pMem->flags&MEM_Str ); in sqlite3VdbeMemTranslate()
35162 assert( pMem->enc!=desiredEnc ); in sqlite3VdbeMemTranslate()
35163 assert( pMem->enc!=0 ); in sqlite3VdbeMemTranslate()
35164 assert( pMem->n>=0 ); in sqlite3VdbeMemTranslate()
35176 /* If the translation is between UTF-16 little and big endian, then in sqlite3VdbeMemTranslate()
35180 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
35188 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
35189 zTerm = &zIn[pMem->n&~1]; in sqlite3VdbeMemTranslate()
35196 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
35202 /* When converting from UTF-16, the maximum growth results from in sqlite3VdbeMemTranslate()
35203 ** translating a 2-byte character to a 4-byte UTF-8 character. in sqlite3VdbeMemTranslate()
35205 ** nul-terminator. in sqlite3VdbeMemTranslate()
35207 pMem->n &= ~1; in sqlite3VdbeMemTranslate()
35208 len = 2 * (sqlite3_int64)pMem->n + 1; in sqlite3VdbeMemTranslate()
35210 /* When converting from UTF-8 to UTF-16 the maximum growth is caused in sqlite3VdbeMemTranslate()
35211 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 in sqlite3VdbeMemTranslate()
35213 ** nul-terminator. in sqlite3VdbeMemTranslate()
35215 len = 2 * (sqlite3_int64)pMem->n + 2; in sqlite3VdbeMemTranslate()
35224 zIn = (u8*)pMem->z; in sqlite3VdbeMemTranslate()
35225 zTerm = &zIn[pMem->n]; in sqlite3VdbeMemTranslate()
35226 zOut = sqlite3DbMallocRaw(pMem->db, len); in sqlite3VdbeMemTranslate()
35232 if( pMem->enc==SQLITE_UTF8 ){ in sqlite3VdbeMemTranslate()
35234 /* UTF-8 -> UTF-16 Little-endian */ in sqlite3VdbeMemTranslate()
35241 /* UTF-8 -> UTF-16 Big-endian */ in sqlite3VdbeMemTranslate()
35247 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
35251 if( pMem->enc==SQLITE_UTF16LE ){ in sqlite3VdbeMemTranslate()
35252 /* UTF-16 Little-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
35264 zIn -= 2; in sqlite3VdbeMemTranslate()
35281 /* UTF-16 Big-endian -> UTF-8 */ in sqlite3VdbeMemTranslate()
35293 zIn -= 2; in sqlite3VdbeMemTranslate()
35310 pMem->n = (int)(z - zOut); in sqlite3VdbeMemTranslate()
35313 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); in sqlite3VdbeMemTranslate()
35315 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype)); in sqlite3VdbeMemTranslate()
35317 pMem->flags = c; in sqlite3VdbeMemTranslate()
35318 pMem->enc = desiredEnc; in sqlite3VdbeMemTranslate()
35319 pMem->z = (char*)zOut; in sqlite3VdbeMemTranslate()
35320 pMem->zMalloc = pMem->z; in sqlite3VdbeMemTranslate()
35321 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); in sqlite3VdbeMemTranslate()
35339 ** This routine checks for a byte-order mark at the beginning of the
35340 ** UTF-16 string stored in *pMem. If one is present, it is removed and
35342 ** byte-swapping, it just sets Mem.enc appropriately.
35351 assert( pMem->n>=0 ); in sqlite3VdbeMemHandleBom()
35352 if( pMem->n>1 ){ in sqlite3VdbeMemHandleBom()
35353 u8 b1 = *(u8 *)pMem->z; in sqlite3VdbeMemHandleBom()
35354 u8 b2 = *(((u8 *)pMem->z) + 1); in sqlite3VdbeMemHandleBom()
35366 pMem->n -= 2; in sqlite3VdbeMemHandleBom()
35367 memmove(pMem->z, &pMem->z[2], pMem->n); in sqlite3VdbeMemHandleBom()
35368 pMem->z[pMem->n] = '\0'; in sqlite3VdbeMemHandleBom()
35369 pMem->z[pMem->n+1] = '\0'; in sqlite3VdbeMemHandleBom()
35370 pMem->flags |= MEM_Term; in sqlite3VdbeMemHandleBom()
35371 pMem->enc = bom; in sqlite3VdbeMemHandleBom()
35379 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
35392 zTerm = (const u8*)(-1); in sqlite3Utf8CharLen()
35402 /* This test function is not currently used by the automated test-suite.
35407 ** Translate UTF-8 to UTF-8.
35409 ** This has the effect of making sure that the string is well-formed
35410 ** UTF-8. Miscoded characters are removed.
35412 ** The translation is done in-place and aborted if the output
35427 return (int)(zOut - zStart); in sqlite3Utf8To8()
35433 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
35434 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
35445 if( db->mallocFailed ){ in sqlite3Utf16to8()
35449 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
35450 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); in sqlite3Utf16to8()
35451 assert( m.z || db->mallocFailed ); in sqlite3Utf16to8()
35456 ** zIn is a UTF-16 encoded unicode string at least nByte bytes long.
35458 ** in pZ. nChar must be non-negative. Surrogate pairs count as a single
35464 unsigned char const *zEnd = &z[nByte-1]; in sqlite3Utf16ByteLen()
35474 return (int)(z-(unsigned char const *)zIn) in sqlite3Utf16ByteLen()
35475 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE); in sqlite3Utf16ByteLen()
35494 n = (int)(z-zBuf); in sqlite3UtfSelfTest()
35503 assert( (z-zBuf)==n ); in sqlite3UtfSelfTest()
35540 ** sqlite3FaultSim() function only returns non-zero during testing.
35542 ** During testing, if the test harness has set a fault-sim callback using
35544 ** each call to sqlite3FaultSim() is relayed to that application-supplied
35545 ** callback and the integer return value form the application-supplied
35583 ** Return true if the floating point value is NaN or +Inf or -Inf.
35595 ** Compute a string length that is limited to what can be stored in
35596 ** lower 30 bits of a 32-bit signed integer.
35599 ** than the actual length of the string. For very long strings (greater
35600 ** than 1GiB) the value returned might be less than the true string length.
35611 ** The column type is an extra string stored after the zero-terminator on
35615 if( pCol->colFlags & COLFLAG_HASTYPE ){ in sqlite3ColumnType()
35616 return pCol->zCnName + strlen(pCol->zCnName) + 1; in sqlite3ColumnType()
35617 }else if( pCol->eCType ){ in sqlite3ColumnType()
35618 assert( pCol->eCType<=SQLITE_N_STDTYPE ); in sqlite3ColumnType()
35619 return (char*)sqlite3StdType[pCol->eCType-1]; in sqlite3ColumnType()
35626 ** Helper function for sqlite3Error() - called rarely. Broken out into
35631 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorFinish()
35642 db->errCode = err_code; in sqlite3Error()
35643 if( err_code || db->pErr ){ in sqlite3Error()
35646 db->errByteOffset = -1; in sqlite3Error()
35656 db->errCode = SQLITE_OK; in sqlite3ErrorClear()
35657 db->errByteOffset = -1; in sqlite3ErrorClear()
35658 if( db->pErr ) sqlite3ValueSetNull(db->pErr); in sqlite3ErrorClear()
35672 for(ii=0; ii<db->nDb; ii++){ in sqlite3SystemError()
35673 if( db->aDb[ii].pBt ){ in sqlite3SystemError()
35674 iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt)); in sqlite3SystemError()
35676 db->iSysErrno = iErr; in sqlite3SystemError()
35686 db->iSysErrno = sqlite3OsGetLastError(db->pVfs); in sqlite3SystemError()
35696 ** assumed to be encoded in UTF-8.
35704 db->errCode = err_code; in sqlite3ErrorWithMsg()
35708 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ in sqlite3ErrorWithMsg()
35714 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); in sqlite3ErrorWithMsg()
35722 sqlite3 *db = p->db; in sqlite3ProgressCheck()
35723 if( AtomicLoad(&db->u1.isInterrupted) ){ in sqlite3ProgressCheck()
35724 p->nErr++; in sqlite3ProgressCheck()
35725 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35728 if( db->xProgress ){ in sqlite3ProgressCheck()
35729 if( p->rc==SQLITE_INTERRUPT ){ in sqlite3ProgressCheck()
35730 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35731 }else if( (++p->nProgressSteps)>=db->nProgressOps ){ in sqlite3ProgressCheck()
35732 if( db->xProgress(db->pProgressArg) ){ in sqlite3ProgressCheck()
35733 p->nErr++; in sqlite3ProgressCheck()
35734 p->rc = SQLITE_INTERRUPT; in sqlite3ProgressCheck()
35736 p->nProgressSteps = 0; in sqlite3ProgressCheck()
35743 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
35755 sqlite3 *db = pParse->db; in sqlite3ErrorMsg()
35757 assert( db->pParse==pParse || db->pParse->pToplevel==pParse ); in sqlite3ErrorMsg()
35758 db->errByteOffset = -2; in sqlite3ErrorMsg()
35762 if( db->errByteOffset<-1 ) db->errByteOffset = -1; in sqlite3ErrorMsg()
35763 if( db->suppressErr ){ in sqlite3ErrorMsg()
35765 if( db->mallocFailed ){ in sqlite3ErrorMsg()
35766 pParse->nErr++; in sqlite3ErrorMsg()
35767 pParse->rc = SQLITE_NOMEM; in sqlite3ErrorMsg()
35770 pParse->nErr++; in sqlite3ErrorMsg()
35771 sqlite3DbFree(db, pParse->zErrMsg); in sqlite3ErrorMsg()
35772 pParse->zErrMsg = zMsg; in sqlite3ErrorMsg()
35773 pParse->rc = SQLITE_ERROR; in sqlite3ErrorMsg()
35774 pParse->pWith = 0; in sqlite3ErrorMsg()
35785 if( db==0 || (pParse = db->pParse)==0 ) return errCode; in sqlite3ErrorToParser()
35786 pParse->rc = errCode; in sqlite3ErrorToParser()
35787 pParse->nErr++; in sqlite3ErrorToParser()
35792 ** Convert an SQL-style quoted string into a normal string by removing
35793 ** the quote characters. The conversion is done in-place. If the
35795 ** is a no-op.
35797 ** The input string must be zero-terminated. A new zero-terminator
35800 ** The return value is -1 if no dequoting occurs or the length of the
35804 ** 2002-02-14: This routine is extended to remove MS-Access style
35805 ** brackets from around identifiers. For example: "[a-b-c]" becomes
35806 ** "a-b-c".
35832 assert( sqlite3Isquote(p->u.zToken[0]) ); in sqlite3DequoteExpr()
35833 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted; in sqlite3DequoteExpr()
35834 sqlite3Dequote(p->u.zToken); in sqlite3DequoteExpr()
35838 ** Expression p is a QNUMBER (quoted number). Dequote the value in p->u.zToken
35843 assert( p!=0 || pParse->db->mallocFailed ); in sqlite3DequoteNumber()
35845 const char *pIn = p->u.zToken; in sqlite3DequoteNumber()
35846 char *pOut = p->u.zToken; in sqlite3DequoteNumber()
35849 assert( p->op==TK_QNUMBER ); in sqlite3DequoteNumber()
35850 p->op = TK_INTEGER; in sqlite3DequoteNumber()
35854 if( *pIn=='e' || *pIn=='E' || *pIn=='.' ) p->op = TK_FLOAT; in sqlite3DequoteNumber()
35856 if( (bHex==0 && (!sqlite3Isdigit(pIn[-1]) || !sqlite3Isdigit(pIn[1]))) in sqlite3DequoteNumber()
35857 || (bHex==1 && (!sqlite3Isxdigit(pIn[-1]) || !sqlite3Isxdigit(pIn[1]))) in sqlite3DequoteNumber()
35859 sqlite3ErrorMsg(pParse, "unrecognized token: \"%s\"", p->u.zToken); in sqlite3DequoteNumber()
35863 if( bHex ) p->op = TK_INTEGER; in sqlite3DequoteNumber()
35865 /* tag-20240227-a: If after dequoting, the number is an integer that in sqlite3DequoteNumber()
35867 ** parts of the code expect this. See also tag-20240227-b. */ in sqlite3DequoteNumber()
35868 if( p->op==TK_INTEGER && sqlite3GetInt32(p->u.zToken, &iValue) ){ in sqlite3DequoteNumber()
35869 p->u.iValue = iValue; in sqlite3DequoteNumber()
35870 p->flags |= EP_IntValue; in sqlite3DequoteNumber()
35879 ** "abc" -> abc
35880 ** "ab""cd" -> (not possible because of the interior "")
35884 ** is always a no-op.
35888 if( p->n<2 ) return; in sqlite3DequoteToken()
35889 if( !sqlite3Isquote(p->z[0]) ) return; in sqlite3DequoteToken()
35890 for(i=1; i<p->n-1; i++){ in sqlite3DequoteToken()
35891 if( sqlite3Isquote(p->z[i]) ) return; in sqlite3DequoteToken()
35893 p->n -= 2; in sqlite3DequoteToken()
35894 p->z++; in sqlite3DequoteToken()
35901 p->z = z; in sqlite3TokenInit()
35902 p->n = sqlite3Strlen30(z); in sqlite3TokenInit()
35905 /* Convenient short-hand */
35912 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
35914 ** the contents of two buffers containing UTF-8 strings in a
35915 ** case-independent fashion, using the same definition of "case
35920 return zRight ? -1 : 0; in sqlite3_stricmp()
35937 c = (int)UpperToLower[c] - (int)UpperToLower[x]; in sqlite3StrICmp()
35948 return zRight ? -1 : 0; in sqlite3_strnicmp()
35954 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } in sqlite3_strnicmp()
35955 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; in sqlite3_strnicmp()
35959 ** Compute an 8-bit hash on a string that is insensitive to case differences
35971 /* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35974 ** T. J. Dekker, "A Floating-Point Technique for Extending the
35975 ** Available Precision". 1971-07-26.
35981 ** binary64 rather than be carried around in an extended-precision in dekkerMul2()
35992 tx = x[0] - hx; in dekkerMul2()
35996 ty = y - hy; in dekkerMul2()
36000 cc = p - c + q + tx*ty; in dekkerMul2()
36003 x[1] = c - x[0]; in dekkerMul2()
36011 ** The string z[] is length bytes in length (bytes, not characters) and
36012 ** uses the encoding enc. The string is not necessarily zero-terminated.
36020 ** -1 => Not a valid number, but has a valid prefix which
36025 ** [+-]digits[E[+-]digits]
36026 ** [+-]digits.[digits][E[+-]digits]
36027 ** [+-].digits[E[+-]digits]
36039 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){ in sqlite3AtoF() argument
36049 int eValid = 1; /* True exponent is either not used or is well-formed */ in sqlite3AtoF()
36051 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */ in sqlite3AtoF()
36052 u64 s2; /* round-tripped significand */ in sqlite3AtoF()
36057 if( length==0 ) return 0; in sqlite3AtoF()
36061 zEnd = z + length; in sqlite3AtoF()
36065 length &= ~1; in sqlite3AtoF()
36069 for(i=3-enc; i<length && z[i]==0; i+=2){} in sqlite3AtoF()
36070 if( i<length ) eType = -100; in sqlite3AtoF()
36080 if( *z=='-' ){ in sqlite3AtoF()
36081 sign = -1; in sqlite3AtoF()
36089 s = s*10 + (*z - '0'); in sqlite3AtoF()
36091 if( s>=((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
36092 /* skip non-significant significand digits in sqlite3AtoF()
36106 if( s<((LARGEST_UINT64-9)/10) ){ in sqlite3AtoF()
36107 s = s*10 + (*z - '0'); in sqlite3AtoF()
36108 d--; in sqlite3AtoF()
36125 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/ in sqlite3AtoF()
36128 if( *z=='-' ){ in sqlite3AtoF()
36129 esign = -1; in sqlite3AtoF()
36136 e = e<10000 ? (e*10 + (*z - '0')) : 10000; in sqlite3AtoF()
36148 *pResult = sign<0 ? -0.0 : +0.0; in sqlite3AtoF()
36156 while( e>0 && s<((LARGEST_UINT64-0x7ff)/10) ){ in sqlite3AtoF()
36158 e--; in sqlite3AtoF()
36176 rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s); in sqlite3AtoF()
36180 assert( rr[1]<=1.0e-10*rr[0] ); /* Equal only when rr[0]==0.0 */ in sqlite3AtoF()
36184 e -= 100; in sqlite3AtoF()
36185 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3AtoF()
36188 e -= 10; in sqlite3AtoF()
36192 e -= 1; in sqlite3AtoF()
36196 while( e<=-100 ){ in sqlite3AtoF()
36198 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3AtoF()
36200 while( e<=-10 ){ in sqlite3AtoF()
36202 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3AtoF()
36204 while( e<=-1 ){ in sqlite3AtoF()
36206 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3AtoF()
36211 if( sign<0 ) *pResult = -*pResult; in sqlite3AtoF()
36215 /* return true if number and no extra non-whitespace characters after */ in sqlite3AtoF()
36219 return -1; in sqlite3AtoF()
36224 return !sqlite3Atoi64(z, pResult, length, enc); in sqlite3AtoF()
36232 ** Render an signed 64-bit integer as text. Store the result in zOut[] and
36233 ** return the length of the string that was stored, in bytes. The value
36244 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v; in sqlite3Int64ToText()
36248 i = sizeof(zTemp)-2; in sqlite3Int64ToText()
36249 zTemp[sizeof(zTemp)-1] = 0; in sqlite3Int64ToText()
36250 while( 1 /*exit-by-break*/ ){ in sqlite3Int64ToText()
36254 i--; in sqlite3Int64ToText()
36256 if( v<0 ) zTemp[--i] = '-'; in sqlite3Int64ToText()
36257 memcpy(zOut, &zTemp[i], sizeof(zTemp)-i); in sqlite3Int64ToText()
36258 return sizeof(zTemp)-1-i; in sqlite3Int64ToText()
36262 ** Compare the 19-character string zNum against the text representation
36273 ** will return -8.
36281 c = (zNum[i*incr]-pow63[i])*10; in compare2pow63()
36284 c = zNum[18*incr] - '8'; in compare2pow63()
36285 testcase( c==(-1) ); in compare2pow63()
36293 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
36298 ** -1 Not even a prefix of the input text looks like an integer
36299 ** 0 Successful transformation. Fits in a 64-bit signed integer.
36300 ** 1 Excess non-space text after the integer value
36301 ** 2 Integer too large for a 64-bit signed integer or is malformed
36304 ** length is the number of bytes in the string (bytes, not characters).
36305 ** The string is not necessarily zero-terminated. The encoding is
36308 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){ in sqlite3Atoi64() argument
36314 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */ in sqlite3Atoi64()
36317 const char *zEnd = zNum + length; in sqlite3Atoi64()
36323 length &= ~1; in sqlite3Atoi64()
36325 for(i=3-enc; i<length && zNum[i]==0; i+=2){} in sqlite3Atoi64()
36326 nonNum = i<length; in sqlite3Atoi64()
36332 if( *zNum=='-' ){ in sqlite3Atoi64()
36342 u = u*10 + c - '0'; in sqlite3Atoi64()
36349 ** from clang and -fsanitize=undefined. This test and assignment make in sqlite3Atoi64()
36351 ** them, but we must appease the undefined-behavior pharisees. */ in sqlite3Atoi64()
36354 *pNum = -(i64)u; in sqlite3Atoi64()
36360 rc = -1; in sqlite3Atoi64()
36361 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */ in sqlite3Atoi64()
36367 rc = 1; /* Extra non-space text after the integer */ in sqlite3Atoi64()
36378 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ in sqlite3Atoi64()
36392 assert( u-1==LARGEST_INT64 ); in sqlite3Atoi64()
36400 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
36401 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
36406 ** 0 Successful transformation. Fits in a 64-bit signed integer.
36408 ** 2 Integer too large for a 64-bit signed integer or is malformed
36423 if( k-i>16 ) return 2; in sqlite3DecOrHexToI64()
36429 int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789")); in sqlite3DecOrHexToI64()
36436 ** If zNum represents an integer that will fit in 32-bits, then set
36441 ** Any non-numeric characters that following zNum are ignored.
36443 ** input number to be zero-terminated.
36449 if( zNum[0]=='-' ){ in sqlite3GetInt32()
36476 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ in sqlite3GetInt32()
36483 ** 2^31 -> 2147483648 in sqlite3GetInt32()
36489 testcase( v-neg==2147483647 ); in sqlite3GetInt32()
36490 if( v-neg>2147483647 ){ in sqlite3GetInt32()
36494 v = -v; in sqlite3GetInt32()
36501 ** Return a 32-bit integer value extracted from a string. If the
36511 ** Decode a floating-point value into an approximate decimal
36514 ** If iRound<=0 then round to -iRound significant digits to the
36523 ** stored in p->z[] which is a often (but not always) a pointer
36524 ** into the middle of p->zBuf[]. There are p->n significant digits.
36525 ** The p->z[] array is *not* zero-terminated.
36533 p->isSpecial = 0; in sqlite3FpDecode()
36534 p->z = p->zBuf; in sqlite3FpDecode()
36540 p->sign = '-'; in sqlite3FpDecode()
36541 r = -r; in sqlite3FpDecode()
36543 p->sign = '+'; in sqlite3FpDecode()
36544 p->n = 1; in sqlite3FpDecode()
36545 p->iDP = 1; in sqlite3FpDecode()
36546 p->z = "0"; in sqlite3FpDecode()
36549 p->sign = '+'; in sqlite3FpDecode()
36554 p->isSpecial = 1 + (v!=0x7ff0000000000000LL); in sqlite3FpDecode()
36555 p->n = 0; in sqlite3FpDecode()
36556 p->iDP = 0; in sqlite3FpDecode()
36563 ** Use Dekker-style double-double computation to increase the in sqlite3FpDecode()
36576 dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117); in sqlite3FpDecode()
36580 dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27); in sqlite3FpDecode()
36584 dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18); in sqlite3FpDecode()
36587 while( rr[0]<9.223372036854774784e-83 ){ in sqlite3FpDecode()
36588 exp -= 100; in sqlite3FpDecode()
36589 dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83); in sqlite3FpDecode()
36592 exp -= 10; in sqlite3FpDecode()
36596 exp -= 1; in sqlite3FpDecode()
36600 v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1]; in sqlite3FpDecode()
36603 i = sizeof(p->zBuf)-1; in sqlite3FpDecode()
36605 while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; } in sqlite3FpDecode()
36606 assert( i>=0 && i<sizeof(p->zBuf)-1 ); in sqlite3FpDecode()
36607 p->n = sizeof(p->zBuf) - 1 - i; in sqlite3FpDecode()
36608 assert( p->n>0 ); in sqlite3FpDecode()
36609 assert( p->n<sizeof(p->zBuf) ); in sqlite3FpDecode()
36610 p->iDP = p->n + exp; in sqlite3FpDecode()
36612 iRound = p->iDP - iRound; in sqlite3FpDecode()
36613 if( iRound==0 && p->zBuf[i+1]>='5' ){ in sqlite3FpDecode()
36615 p->zBuf[i--] = '0'; in sqlite3FpDecode()
36616 p->n++; in sqlite3FpDecode()
36617 p->iDP++; in sqlite3FpDecode()
36620 if( iRound>0 && (iRound<p->n || p->n>mxRound) ){ in sqlite3FpDecode()
36621 char *z = &p->zBuf[i+1]; in sqlite3FpDecode()
36623 p->n = iRound; in sqlite3FpDecode()
36625 int j = iRound-1; in sqlite3FpDecode()
36626 while( 1 /*exit-by-break*/ ){ in sqlite3FpDecode()
36631 p->z[i--] = '1'; in sqlite3FpDecode()
36632 p->n++; in sqlite3FpDecode()
36633 p->iDP++; in sqlite3FpDecode()
36636 j--; in sqlite3FpDecode()
36641 p->z = &p->zBuf[i+1]; in sqlite3FpDecode()
36642 assert( i+p->n < sizeof(p->zBuf) ); in sqlite3FpDecode()
36643 assert( p->n>0 ); in sqlite3FpDecode()
36644 while( p->z[p->n-1]=='0' ){ in sqlite3FpDecode()
36645 p->n--; in sqlite3FpDecode()
36646 assert( p->n>0 ); in sqlite3FpDecode()
36651 ** Try to convert z into an unsigned 32-bit integer. Return true on
36660 v = v*10 + z[i] - '0'; in sqlite3GetUInt32()
36669 ** The variable-length integer encoding is as follows:
36676 ** 7 bits - A
36677 ** 14 bits - BA
36678 ** 21 bits - BBA
36679 ** 28 bits - BBBA
36680 ** 35 bits - BBBBA
36681 ** 42 bits - BBBBBA
36682 ** 49 bits - BBBBBBA
36683 ** 56 bits - BBBBBBBA
36684 ** 64 bits - BBBBBBBBC
36688 ** Write a 64-bit variable-length integer to memory starting at p[0].
36689 ** The length of data write will be between 1 and 9 bytes. The number
36692 ** A variable-length integer consists of the lower 7 bits of each byte
36703 for(i=7; i>=0; i--){ in putVarint64()
36716 for(i=0, j=n-1; j>=0; j--, i++){ in putVarint64()
36748 ** Read a 64-bit variable-length integer from memory starting at p[0].
36891 b = p[-4]; in sqlite3GetVarint()
36902 ** Read a 32-bit variable-length integer from memory starting at p[0].
36905 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
36909 ** single-byte case. All code should use the MACRO version as
36910 ** this function assumes the single-byte case has already been handled.
36916 /* Assume that the single-byte case has already been handled by in sqlite3GetVarint32()
36921 /* This is the two-byte case */ in sqlite3GetVarint32()
36926 /* This is the three-byte case */ in sqlite3GetVarint32()
36943 ** 64-bit integer.
36953 ** Read or write a four-byte big-endian integer value.
37020 n--; in sqlite3HexToBlob()
37063 eOpenState = db->eOpenState; in sqlite3SafetyCheckOk()
37076 eOpenState = db->eOpenState; in sqlite3SafetyCheckSickOrOk()
37089 ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
37090 ** the other 64-bit signed integer at *pA and store the result in *pA.
37100 testcase( iB==-1 ); testcase( iB==0 ); in sqlite3AddInt64()
37102 testcase( iA>0 && LARGEST_INT64 - iA == iB ); in sqlite3AddInt64()
37103 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); in sqlite3AddInt64()
37104 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; in sqlite3AddInt64()
37106 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); in sqlite3AddInt64()
37107 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); in sqlite3AddInt64()
37108 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; in sqlite3AddInt64()
37120 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); in sqlite3SubInt64()
37122 *pA -= iB; in sqlite3SubInt64()
37125 return sqlite3AddInt64(pA, -iB); in sqlite3SubInt64()
37143 if( -iA>LARGEST_INT64/-iB ) return 1; in sqlite3MulInt64()
37152 ** Compute the absolute value of a 32-bit signed integer, if possible. Or
37153 ** if the integer has a value of -2147483648, return +2147483647
37158 return -x; in sqlite3AbsInt32()
37163 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
37169 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
37174 ** test.db-journal => test.nal
37175 ** test.db-wal => test.wal
37176 ** test.db-shm => test.shm
37177 ** test.db-mj7f3319fa => test.9fa
37186 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} in sqlite3FileSuffix3()
37187 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); in sqlite3FileSuffix3()
37205 5, 5, 5, /* 12-14 */ in sqlite3LogEstAdd()
37206 4, 4, 4, 4, /* 15-18 */ in sqlite3LogEstAdd()
37207 3, 3, 3, 3, 3, 3, /* 19-24 */ in sqlite3LogEstAdd()
37208 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ in sqlite3LogEstAdd()
37213 return a+x[a-b]; in sqlite3LogEstAdd()
37217 return b+x[b-a]; in sqlite3LogEstAdd()
37230 while( x<8 ){ y -= 10; x <<= 1; } in sqlite3LogEst()
37233 int i = 60 - __builtin_clzll(x); in sqlite3LogEst()
37237 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/ in sqlite3LogEst()
37241 return a[x&7] + y - 10; in sqlite3LogEst()
37255 e = (a>>52) - 1022; in sqlite3LogEstFromDouble()
37266 if( n>=5 ) n -= 2; in sqlite3LogEstToInt()
37267 else if( n>=1 ) n -= 1; in sqlite3LogEstToInt()
37269 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x); in sqlite3LogEstToInt()
37276 ** db->mallocFailed flag is set.
37289 ** is always zero-terminated.
37388 ** This is the implementation of generic hash-tables
37401 pNew->first = 0; in sqlite3HashInit()
37402 pNew->count = 0; in sqlite3HashInit()
37403 pNew->htsize = 0; in sqlite3HashInit()
37404 pNew->ht = 0; in sqlite3HashInit()
37415 elem = pH->first; in sqlite3HashClear()
37416 pH->first = 0; in sqlite3HashClear()
37417 sqlite3_free(pH->ht); in sqlite3HashClear()
37418 pH->ht = 0; in sqlite3HashClear()
37419 pH->htsize = 0; in sqlite3HashClear()
37421 HashElem *next_elem = elem->next; in sqlite3HashClear()
37425 pH->count = 0; in sqlite3HashClear()
37433 while( z[0] ){ /*OPTIMIZATION-IF-TRUE*/ in strHash()
37436 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. in strHash()
37462 pHead = pEntry->count ? pEntry->chain : 0; in insertElement()
37463 pEntry->count++; in insertElement()
37464 pEntry->chain = pNew; in insertElement()
37469 pNew->next = pHead; in insertElement()
37470 pNew->prev = pHead->prev; in insertElement()
37471 if( pHead->prev ){ pHead->prev->next = pNew; } in insertElement()
37472 else { pH->first = pNew; } in insertElement()
37473 pHead->prev = pNew; in insertElement()
37475 pNew->next = pH->first; in insertElement()
37476 if( pH->first ){ pH->first->prev = pNew; } in insertElement()
37477 pNew->prev = 0; in insertElement()
37478 pH->first = pNew; in insertElement()
37497 if( new_size==pH->htsize ) return 0; in rehash()
37513 sqlite3_free(pH->ht); in rehash()
37514 pH->ht = new_ht; in rehash()
37515 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); in rehash()
37517 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ in rehash()
37518 next_elem = elem->next; in rehash()
37519 insertElement(pH, &new_ht[elem->h % new_size], elem); in rehash()
37540 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/ in findElementWithHash()
37542 pEntry = &pH->ht[h % pH->htsize]; in findElementWithHash()
37543 elem = pEntry->chain; in findElementWithHash()
37544 count = pEntry->count; in findElementWithHash()
37546 elem = pH->first; in findElementWithHash()
37547 count = pH->count; in findElementWithHash()
37552 if( h==elem->h && sqlite3StrICmp(elem->pKey,pKey)==0 ){ in findElementWithHash()
37555 elem = elem->next; in findElementWithHash()
37556 count--; in findElementWithHash()
37569 if( elem->prev ){ in removeElement()
37570 elem->prev->next = elem->next; in removeElement()
37572 pH->first = elem->next; in removeElement()
37574 if( elem->next ){ in removeElement()
37575 elem->next->prev = elem->prev; in removeElement()
37577 if( pH->ht ){ in removeElement()
37578 pEntry = &pH->ht[elem->h % pH->htsize]; in removeElement()
37579 if( pEntry->chain==elem ){ in removeElement()
37580 pEntry->chain = elem->next; in removeElement()
37582 assert( pEntry->count>0 ); in removeElement()
37583 pEntry->count--; in removeElement()
37586 pH->count--; in removeElement()
37587 if( pH->count==0 ){ in removeElement()
37588 assert( pH->first==0 ); in removeElement()
37589 assert( pH->count==0 ); in removeElement()
37601 return findElementWithHash(pH, pKey, 0)->data; in sqlite3HashFind()
37626 if( elem->data ){ in sqlite3HashInsert()
37627 void *old_data = elem->data; in sqlite3HashInsert()
37631 elem->data = data; in sqlite3HashInsert()
37632 elem->pKey = pKey; in sqlite3HashInsert()
37639 new_elem->pKey = pKey; in sqlite3HashInsert()
37640 new_elem->h = h; in sqlite3HashInsert()
37641 new_elem->data = data; in sqlite3HashInsert()
37642 pH->count++; in sqlite3HashInsert()
37643 if( pH->count>=5 && pH->count > 2*pH->htsize ){ in sqlite3HashInsert()
37644 rehash(pH, pH->count*3); in sqlite3HashInsert()
37646 insertElement(pH, pH->ht ? &pH->ht[new_elem->h % pH->htsize] : 0, new_elem); in sqlite3HashInsert()
37714 /* 50 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), in sqlite3OpcodeName()
37724 /* 60 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"), in sqlite3OpcodeName()
37725 /* 61 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), in sqlite3OpcodeName()
37754 /* 90 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), in sqlite3OpcodeName()
37772 /* 108 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), in sqlite3OpcodeName()
37788 /* 124 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"), in sqlite3OpcodeName()
37824 /* 160 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"), in sqlite3OpcodeName()
37862 ** 2022-09-06
37905 ** VFS - the database and the rollback journal.
37914 sqlite3_int64 szDb; /* Database file size. -1 means unknown */
38029 /* Forward declarations for the low-level storage engine
38045 sqlite3_snprintf(KVSTORAGE_KEY_SZ, zKeyOut, "kvvfs-%s-%s", zClass, zKeyIn); in kvstorageMakeKey()
38049 ** underlying key/value store to use - either "local" or "session".
38051 ** Both zKey and zData are zero-terminated pure text strings.
38065 SQLITE_KV_TRACE(("KVVFS-WRITE %-15s (%d) %.50s%s\n", zXKey, in kvstorageWrite()
38078 ** this routine is a no-op.
38084 SQLITE_KV_TRACE(("KVVFS-DELETE %-15s\n", zXKey)); in kvstorageDelete()
38095 ** not counting the final zero terminator. Return -1 if the key does
38115 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); in kvstorageRead()
38116 return -1; in kvstorageRead()
38122 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%d)\n", zXKey, in kvstorageRead()
38131 SQLITE_KV_TRACE(("KVVFS-READ %-15s (-1)\n", zXKey)); in kvstorageRead()
38132 return -1; in kvstorageRead()
38134 sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd); in kvstorageRead()
38137 SQLITE_KV_TRACE(("KVVFS-READ %-15s (%lld) %.50s%s\n", zXKey, in kvstorageRead()
38164 ** for JavaScript-side implementations in WASM builds. In such builds
38187 ** nData+1 bytes in length.
38189 ** Return the actual length of the encoded text, not counting the
38193 ** ---------------
38195 ** * Non-zero bytes are encoded as upper-case hexadecimal
38197 ** * A sequence of one or more zero-bytes that are not at the
38198 ** beginning of the buffer are encoded as a little-endian
38199 ** base-26 number using a..z. "a" means 0. "b" means 1,
38203 ** of hexadecimal and base-26 numbers, it is always clear where
38215 /* A sequence of 1 or more zeros is stored as a little-endian in kvvfsEncode()
38216 ** base-26 number using a..z as the digits. So one zero is "b". in kvvfsEncode()
38222 i += k-1; in kvvfsEncode()
38234 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38235 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38236 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38237 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
38238 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38239 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38240 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38241 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38243 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38244 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38245 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38246 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38247 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38248 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38249 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38250 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
38255 ** written into pOut, which must be at least nOut bytes in length.
38273 n += (c - 'a')*mult; in kvvfsDecode()
38277 if( j+n>nOut ) return -1; in kvvfsDecode()
38293 ** Decode a complete journal file. Allocate space in pFile->aJrnl
38294 ** and store the decoding there. Or leave pFile->aJrnl set to NULL
38297 ** The first few characters of the text encoding will be a little-endian
38298 ** base-26 number (digits a..z) that is the total number of bytes
38299 ** in the decoded journal file image. This base-26 number is followed
38301 ** separator is required to act as a terminator for the base-26 number.
38304 KVVfsFile *pFile, /* Store decoding in pFile->aJrnl */ in kvvfsDecodeJournal()
38305 const char *zTxt, /* Text encoding. Zero-terminated */ in kvvfsDecodeJournal()
38313 n += (zTxt[i] - 'a')*mult; in kvvfsDecodeJournal()
38316 sqlite3_free(pFile->aJrnl); in kvvfsDecodeJournal()
38317 pFile->aJrnl = sqlite3_malloc64( n ); in kvvfsDecodeJournal()
38318 if( pFile->aJrnl==0 ){ in kvvfsDecodeJournal()
38319 pFile->nJrnl = 0; in kvvfsDecodeJournal()
38322 pFile->nJrnl = n; in kvvfsDecodeJournal()
38323 n = kvvfsDecode(zTxt+i, pFile->aJrnl, pFile->nJrnl); in kvvfsDecodeJournal()
38324 if( n<pFile->nJrnl ){ in kvvfsDecodeJournal()
38325 sqlite3_free(pFile->aJrnl); in kvvfsDecodeJournal()
38326 pFile->aJrnl = 0; in kvvfsDecodeJournal()
38327 pFile->nJrnl = 0; in kvvfsDecodeJournal()
38337 sqlite3KvvfsMethods.xRead(pFile->zClass, "sz", zData, sizeof(zData)-1); in kvvfsReadFileSize()
38343 return sqlite3KvvfsMethods.xWrite(pFile->zClass, "sz", zData); in kvvfsWriteFileSize()
38349 ** Close an kvvfs-file.
38354 SQLITE_KV_LOG(("xClose %s %s\n", pFile->zClass, in kvvfsClose()
38355 pFile->isJournal ? "journal" : "db")); in kvvfsClose()
38356 sqlite3_free(pFile->aJrnl); in kvvfsClose()
38357 sqlite3_free(pFile->aData); in kvvfsClose()
38362 ** Read from the -journal file.
38371 assert( pFile->isJournal ); in kvvfsReadJrnl()
38372 SQLITE_KV_LOG(("xRead('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); in kvvfsReadJrnl()
38373 if( pFile->aJrnl==0 ){ in kvvfsReadJrnl()
38374 int szTxt = kvstorageRead(pFile->zClass, "jrnl", 0, 0); in kvvfsReadJrnl()
38381 kvstorageRead(pFile->zClass, "jrnl", aTxt, szTxt+1); in kvvfsReadJrnl()
38384 if( pFile->aJrnl==0 ) return SQLITE_IOERR; in kvvfsReadJrnl()
38386 if( iOfst+iAmt>pFile->nJrnl ){ in kvvfsReadJrnl()
38389 memcpy(zBuf, pFile->aJrnl+iOfst, iAmt); in kvvfsReadJrnl()
38406 char *aData = pFile->aData; in kvvfsReadDb()
38409 SQLITE_KV_LOG(("xRead('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); in kvvfsReadDb()
38414 if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){ in kvvfsReadDb()
38417 pFile->szPage = iAmt; in kvvfsReadDb()
38423 got = sqlite3KvvfsMethods.xRead(pFile->zClass, zKey, in kvvfsReadDb()
38424 aData, SQLITE_KVOS_SZ-1); in kvvfsReadDb()
38432 n = kvvfsDecode(aData, &aData[2000], SQLITE_KVOS_SZ-2000); in kvvfsReadDb()
38444 memset(zBuf+n, 0, iAmt-n); in kvvfsReadDb()
38452 ** Write into the -journal file.
38462 SQLITE_KV_LOG(("xWrite('%s-journal',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); in kvvfsWriteJrnl()
38464 if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){ in kvvfsWriteJrnl()
38465 char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd); in kvvfsWriteJrnl()
38469 pFile->aJrnl = aNew; in kvvfsWriteJrnl()
38470 if( pFile->nJrnl<iOfst ){ in kvvfsWriteJrnl()
38471 memset(pFile->aJrnl+pFile->nJrnl, 0, iOfst-pFile->nJrnl); in kvvfsWriteJrnl()
38473 pFile->nJrnl = iEnd; in kvvfsWriteJrnl()
38475 memcpy(pFile->aJrnl+iOfst, zBuf, iAmt); in kvvfsWriteJrnl()
38491 char *aData = pFile->aData; in kvvfsWriteDb()
38492 SQLITE_KV_LOG(("xWrite('%s-db',%d,%lld)\n", pFile->zClass, iAmt, iOfst)); in kvvfsWriteDb()
38494 assert( (iAmt & (iAmt-1))==0 ); in kvvfsWriteDb()
38495 assert( pFile->szPage<0 || pFile->szPage==iAmt ); in kvvfsWriteDb()
38496 pFile->szPage = iAmt; in kvvfsWriteDb()
38500 if( sqlite3KvvfsMethods.xWrite(pFile->zClass, zKey, aData) ){ in kvvfsWriteDb()
38503 if( iOfst+iAmt > pFile->szDb ){ in kvvfsWriteDb()
38504 pFile->szDb = iOfst + iAmt; in kvvfsWriteDb()
38510 ** Truncate an kvvfs-file.
38514 SQLITE_KV_LOG(("xTruncate('%s-journal',%lld)\n", pFile->zClass, size)); in kvvfsTruncateJrnl()
38516 sqlite3KvvfsMethods.xDelete(pFile->zClass, "jrnl"); in kvvfsTruncateJrnl()
38517 sqlite3_free(pFile->aJrnl); in kvvfsTruncateJrnl()
38518 pFile->aJrnl = 0; in kvvfsTruncateJrnl()
38519 pFile->nJrnl = 0; in kvvfsTruncateJrnl()
38524 if( pFile->szDb>size in kvvfsTruncateDb()
38525 && pFile->szPage>0 in kvvfsTruncateDb()
38526 && (size % pFile->szPage)==0 in kvvfsTruncateDb()
38530 SQLITE_KV_LOG(("xTruncate('%s-db',%lld)\n", pFile->zClass, size)); in kvvfsTruncateDb()
38531 pgno = 1 + size/pFile->szPage; in kvvfsTruncateDb()
38532 pgnoMax = 2 + pFile->szDb/pFile->szPage; in kvvfsTruncateDb()
38535 sqlite3KvvfsMethods.xDelete(pFile->zClass, zKey); in kvvfsTruncateDb()
38538 pFile->szDb = size; in kvvfsTruncateDb()
38545 ** Sync an kvvfs-file.
38551 SQLITE_KV_LOG(("xSync('%s-journal')\n", pFile->zClass)); in kvvfsSyncJrnl()
38552 if( pFile->nJrnl<=0 ){ in kvvfsSyncJrnl()
38555 zOut = sqlite3_malloc64( pFile->nJrnl*2 + 50 ); in kvvfsSyncJrnl()
38559 n = pFile->nJrnl; in kvvfsSyncJrnl()
38566 kvvfsEncode(pFile->aJrnl, pFile->nJrnl, &zOut[i]); in kvvfsSyncJrnl()
38567 i = sqlite3KvvfsMethods.xWrite(pFile->zClass, "jrnl", zOut); in kvvfsSyncJrnl()
38576 ** Return the current file-size of an kvvfs-file.
38580 SQLITE_KV_LOG(("xFileSize('%s-journal')\n", pFile->zClass)); in kvvfsFileSizeJrnl()
38581 *pSize = pFile->nJrnl; in kvvfsFileSizeJrnl()
38586 SQLITE_KV_LOG(("xFileSize('%s-db')\n", pFile->zClass)); in kvvfsFileSizeDb()
38587 if( pFile->szDb>=0 ){ in kvvfsFileSizeDb()
38588 *pSize = pFile->szDb; in kvvfsFileSizeDb()
38596 ** Lock an kvvfs-file.
38600 assert( !pFile->isJournal ); in kvvfsLock()
38601 SQLITE_KV_LOG(("xLock(%s,%d)\n", pFile->zClass, eLock)); in kvvfsLock()
38604 pFile->szDb = kvvfsReadFileSize(pFile); in kvvfsLock()
38610 ** Unlock an kvvfs-file.
38614 assert( !pFile->isJournal ); in kvvfsUnlock()
38615 SQLITE_KV_LOG(("xUnlock(%s,%d)\n", pFile->zClass, eLock)); in kvvfsUnlock()
38617 pFile->szDb = -1; in kvvfsUnlock()
38623 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
38632 ** File control method. For custom operations on an kvvfs-file.
38643 SQLITE_KV_LOG(("xSync('%s-db')\n", pFile->zClass)); in kvvfsFileControlDb()
38644 if( pFile->szDb>0 && 0!=kvvfsWriteFileSize(pFile, pFile->szDb) ){ in kvvfsFileControlDb()
38653 ** Return the sector-size in bytes for an kvvfs-file.
38660 ** Return the device characteristic flags supported by an kvvfs-file.
38684 pFile->isJournal = 0; in kvvfsOpen()
38685 pFile->base.pMethods = &kvvfs_db_io_methods; in kvvfsOpen()
38687 if( strcmp(zName, "local-journal")==0 in kvvfsOpen()
38688 || strcmp(zName, "session-journal")==0 in kvvfsOpen()
38690 pFile->isJournal = 1; in kvvfsOpen()
38691 pFile->base.pMethods = &kvvfs_jrnl_io_methods; in kvvfsOpen()
38696 pFile->zClass = "session"; in kvvfsOpen()
38698 pFile->zClass = "local"; in kvvfsOpen()
38700 pFile->aData = sqlite3_malloc64(SQLITE_KVOS_SZ); in kvvfsOpen()
38701 if( pFile->aData==0 ){ in kvvfsOpen()
38704 pFile->aJrnl = 0; in kvvfsOpen()
38705 pFile->nJrnl = 0; in kvvfsOpen()
38706 pFile->szPage = -1; in kvvfsOpen()
38707 pFile->szDb = -1; in kvvfsOpen()
38713 ** ensure the file-system modifications are synced to disk before
38717 if( strcmp(zPath, "local-journal")==0 ){ in kvvfsDelete()
38720 if( strcmp(zPath, "session-journal")==0 ){ in kvvfsDelete()
38737 if( strcmp(zPath, "local-journal")==0 ){ in kvvfsAccess()
38740 if( strcmp(zPath, "session-journal")==0 ){ in kvvfsAccess()
38773 if( nOut<nPath+1 ) nPath = nOut - 1; in kvvfsFullPathname()
38825 ** This routine is called initialize the KV-vfs as the default VFS.
38855 ** This file contains the VFS implementation for unix-like operating systems
38861 ** use flock(), dot-files, various proprietary locking schemas, or simply
38871 ** * General-purpose declarations and utility functions.
38875 ** + for no-op locks
38876 ** + for dot-file locks
38884 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
38897 ** 3. Dot-file locking,
38957 ** -DHAVE_GETHOSTUUID=0
38958 ** -DHAVE_GETHOSTUUID=1
38961 ** -DSQLITE_ENABLE_LOCKING_STYLE.
38999 ** If we are to be thread-safe, include the pthreads header.
39020 ** Maximum supported path-length.
39039 # define SQLITE_DEFAULT_UNIX_VFS "unix-dotfile"
39040 /* ^^^ should SQLITE_DEFAULT_UNIX_VFS be "unix-none"? */
39079 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
39108 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
39136 ** transaction counter in bytes 24-27 of database files are updated
39201 ** The threadid macro resolves to the thread-id or to 0. Used for
39222 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
39223 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
39231 ** Linux-specific IOCTL magic numbers used for controlling F2FS
39249 ** which always has the same well-defined interface.
39260 ** Many system calls are accessed through pointer-to-functions so that
39289 ** DJGPP. But it is DOS - what did you expect?
39442 ** log if they come from non-root processes. So avoid calling fchown() if
39522 int i = -1; in unixNextSystemCall()
39526 for(i=0; i<ArraySize(aSyscall)-1; i++){ in unixNextSystemCall()
39554 ** The m parameter will be non-zero only when creating -wal, -journal,
39555 ** and -shm files. We want those files to have *exactly* the same
39557 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
39582 fd = -1; in robust_open()
39647 ** integer lock-type.
39666 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
39667 ** command-line option on the compiler. This code is normally
39683 if( p->l_type==F_RDLCK ){ in lockTrace()
39685 }else if( p->l_type==F_WRLCK ){ in lockTrace()
39687 }else if( p->l_type==F_UNLCK ){ in lockTrace()
39692 assert( p->l_whence==SEEK_SET ); in lockTrace()
39696 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, in lockTrace()
39697 (int)p->l_pid, s); in lockTrace()
39698 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ in lockTrace()
39711 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", in lockTrace()
39731 /* On Android, ftruncate() always uses 32-bit offsets, even if in robust_ftruncate()
39792 ** a performance-critical path, so it is sufficient to put these
39798 int nName; /* Length of the zCanonicalName[] string */
39817 ** Changes are made in-place. Return the new name length.
39819 ** The original filename is in z[0..n-1]. Return the number of
39824 while( n>1 && z[n-1]=='/' ){ n--; } in vxworksSimplifyName()
39833 while( j>0 && z[j-1]!='/' ){ j--; } in vxworksSimplifyName()
39834 if( j>0 ){ j--; } in vxworksSimplifyName()
39859 int n; /* Length of zAbsoluteName string */ in vxworksFindFileId()
39865 pNew->zCanonicalName = (char*)&pNew[1]; in vxworksFindFileId()
39866 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); in vxworksFindFileId()
39867 n = vxworksSimplifyName(pNew->zCanonicalName, n); in vxworksFindFileId()
39874 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ in vxworksFindFileId()
39875 if( pCandidate->nName==n in vxworksFindFileId()
39876 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 in vxworksFindFileId()
39879 pCandidate->nRef++; in vxworksFindFileId()
39886 pNew->nRef = 1; in vxworksFindFileId()
39887 pNew->nName = n; in vxworksFindFileId()
39888 pNew->pNext = vxworksFileList; in vxworksFindFileId()
39900 assert( pId->nRef>0 ); in vxworksReleaseFileId()
39901 pId->nRef--; in vxworksReleaseFileId()
39902 if( pId->nRef==0 ){ in vxworksReleaseFileId()
39904 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} in vxworksReleaseFileId()
39906 *pp = pId->pNext; in vxworksReleaseFileId()
39964 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
39992 ** on linux - with NPTL a lock created by thread A can override locks
39993 ** in thread B. But there is no way to know at compile-time which
39995 ** compile-time whether or not thread A can override locks on thread B.
39996 ** One has to do a run-time check to discover the behavior of the
40016 ** sizes ino_t at only 32-bits instead of 64-bits. (See
40017 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
40018 ** To work around this, always allocate 64-bits for the inode number.
40019 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
40086 assert( pFile->pInode ); in unixFileMutexHeld()
40087 return sqlite3_mutex_held(pFile->pInode->pLockMutex); in unixFileMutexHeld()
40090 assert( pFile->pInode ); in unixFileMutexNotheld()
40091 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); in unixFileMutexNotheld()
40097 ** This function - unixLogErrorAtLine(), is only ever called via the macro
40102 ** errno and, if possible, the human-readable equivalent from strerror() or
40108 ** failed (e.g. "unlink", "open") and the associated file-system path,
40122 ** the strerror() function to obtain the human-readable error message in unixLogErrorAtLine()
40143 ** int-type return, depending on its version. in unixLogErrorAtLine()
40149 strerror_r(iErrno, aErr, sizeof(aErr)-1); in unixLogErrorAtLine()
40155 /* Non-threadsafe build, use strerror(). */ in unixLogErrorAtLine()
40161 "os_unix.c:%d: (%d) %s(%s) - %s", in unixLogErrorAtLine()
40184 pFile ? pFile->zPath : 0, lineno); in robust_close()
40189 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
40193 pFile->lastErrno = error; in storeLastErrno()
40197 ** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
40200 unixInodeInfo *pInode = pFile->pInode; in closePendingFds()
40204 for(p=pInode->pUnused; p; p=pNext){ in closePendingFds()
40205 pNext = p->pNext; in closePendingFds()
40206 robust_close(pFile, p->fd, __LINE__); in closePendingFds()
40209 pInode->pUnused = 0; in closePendingFds()
40219 unixInodeInfo *pInode = pFile->pInode; in releaseInodeInfo()
40223 pInode->nRef--; in releaseInodeInfo()
40224 if( pInode->nRef==0 ){ in releaseInodeInfo()
40225 assert( pInode->pShmNode==0 ); in releaseInodeInfo()
40226 sqlite3_mutex_enter(pInode->pLockMutex); in releaseInodeInfo()
40228 sqlite3_mutex_leave(pInode->pLockMutex); in releaseInodeInfo()
40229 if( pInode->pPrev ){ in releaseInodeInfo()
40230 assert( pInode->pPrev->pNext==pInode ); in releaseInodeInfo()
40231 pInode->pPrev->pNext = pInode->pNext; in releaseInodeInfo()
40234 inodeList = pInode->pNext; in releaseInodeInfo()
40236 if( pInode->pNext ){ in releaseInodeInfo()
40237 assert( pInode->pNext->pPrev==pInode ); in releaseInodeInfo()
40238 pInode->pNext->pPrev = pInode->pPrev; in releaseInodeInfo()
40240 sqlite3_mutex_free(pInode->pLockMutex); in releaseInodeInfo()
40262 struct stat statbuf; /* Low-level file information */ in findInodeInfo()
40267 /* Get low-level information about the file that we can used to in findInodeInfo()
40270 fd = pFile->h; in findInodeInfo()
40275 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; in findInodeInfo()
40282 ** incorrectly for zero-size files. See ticket #3260. To work in findInodeInfo()
40291 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ in findInodeInfo()
40308 fileId.pId = pFile->pId; in findInodeInfo()
40314 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ in findInodeInfo()
40315 pInode = pInode->pNext; in findInodeInfo()
40323 memcpy(&pInode->fileId, &fileId, sizeof(fileId)); in findInodeInfo()
40325 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); in findInodeInfo()
40326 if( pInode->pLockMutex==0 ){ in findInodeInfo()
40331 pInode->nRef = 1; in findInodeInfo()
40333 pInode->pNext = inodeList; in findInodeInfo()
40334 pInode->pPrev = 0; in findInodeInfo()
40335 if( inodeList ) inodeList->pPrev = pInode; in findInodeInfo()
40338 pInode->nRef++; in findInodeInfo()
40349 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; in fileHasMoved()
40352 return pFile->pInode!=0 && in fileHasMoved()
40353 (osStat(pFile->zPath, &buf)!=0 in fileHasMoved()
40354 || (u64)buf.st_ino!=pFile->pInode->fileId.ino); in fileHasMoved()
40373 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; in verifyDbFile()
40375 rc = osFstat(pFile->h, &buf); in verifyDbFile()
40377 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); in verifyDbFile()
40381 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); in verifyDbFile()
40385 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); in verifyDbFile()
40389 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); in verifyDbFile()
40398 ** to a non-zero value otherwise *pResOut is set to zero. The return value
40409 assert( pFile->eFileLock<=SHARED_LOCK ); in unixCheckReservedLock()
40410 sqlite3_mutex_enter(pFile->pInode->pLockMutex); in unixCheckReservedLock()
40413 if( pFile->pInode->eFileLock>SHARED_LOCK ){ in unixCheckReservedLock()
40420 if( !reserved && !pFile->pInode->bProcessLock ){ in unixCheckReservedLock()
40426 if( osFcntl(pFile->h, F_GETLK, &lock) ){ in unixCheckReservedLock()
40435 sqlite3_mutex_leave(pFile->pInode->pLockMutex); in unixCheckReservedLock()
40436 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); in unixCheckReservedLock()
40446 ** Set a posix-advisory-lock.
40450 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
40455 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
40466 int tm = pFile->iBusyTimeout; in osSetPosixAdvisoryLock()
40476 tm--; in osSetPosixAdvisoryLock()
40484 ** Attempt to set a system-lock on the file pFile. The lock is
40487 ** If the pFile was opened read/write from unix-excl, then the only lock
40490 ** operations become no-ops. Locking operations still happen internally,
40495 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
40496 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
40497 ** and is read-only.
40499 ** Zero is returned if the call completes successfully, or -1 if a call
40504 unixInodeInfo *pInode = pFile->pInode; in unixFileLock()
40506 assert( sqlite3_mutex_held(pInode->pLockMutex) ); in unixFileLock()
40507 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ in unixFileLock()
40508 if( pInode->bProcessLock==0 ){ in unixFileLock()
40510 /* assert( pInode->nLock==0 ); <-- Not true if unix-excl READONLY used */ in unixFileLock()
40515 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); in unixFileLock()
40517 pInode->bProcessLock = 1; in unixFileLock()
40518 pInode->nLock++; in unixFileLock()
40524 if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK in unixFileLock()
40525 && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE in unixFileLock()
40527 rc = osFcntl(pFile->h, F_SETLKW, pLock); in unixFileLock()
40530 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); in unixFileLock()
40536 ** Lock the file with the lock specified by parameter eFileLock - one
40550 ** UNLOCKED -> SHARED
40551 ** SHARED -> RESERVED
40552 ** SHARED -> EXCLUSIVE
40553 ** RESERVED -> (PENDING) -> EXCLUSIVE
40554 ** PENDING -> EXCLUSIVE
40562 ** lock primitives (called read-locks and write-locks below, to avoid in unixLock()
40571 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending in unixLock()
40572 ** byte'. If this is successful, 'shared byte range' is read-locked in unixLock()
40575 ** and Windows95 lacks a shared-lock capability. So on Windows95, a in unixLock()
40577 ** Windows95 is now pretty much extinct, but this work-around for the in unixLock()
40578 ** lack of shared-locks on Windows95 lives on, for backwards in unixLock()
40582 ** A RESERVED lock is implemented by grabbing a write-lock on the in unixLock()
40587 ** a write-lock on the entire 'shared byte range'. Since all other locks in unixLock()
40588 ** require a read-lock on one of the bytes within this range, this ensures in unixLock()
40593 ** obtaining a write-lock on the 'pending byte'. This ensures that no new in unixLock()
40597 ** then re-attempt the EXCLUSIVE lock later on, after existing SHARED in unixLock()
40607 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, in unixLock()
40608 azFileLock(eFileLock), azFileLock(pFile->eFileLock), in unixLock()
40609 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, in unixLock()
40616 if( pFile->eFileLock>=eFileLock ){ in unixLock()
40617 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, in unixLock()
40627 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); in unixLock()
40629 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); in unixLock()
40631 /* This mutex is needed because pFile->pInode is shared across threads in unixLock()
40633 pInode = pFile->pInode; in unixLock()
40634 sqlite3_mutex_enter(pInode->pLockMutex); in unixLock()
40639 if( (pFile->eFileLock!=pInode->eFileLock && in unixLock()
40640 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) in unixLock()
40651 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ in unixLock()
40653 assert( pFile->eFileLock==0 ); in unixLock()
40654 assert( pInode->nShared>0 ); in unixLock()
40655 pFile->eFileLock = SHARED_LOCK; in unixLock()
40656 pInode->nShared++; in unixLock()
40657 pInode->nLock++; in unixLock()
40669 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock==RESERVED_LOCK) in unixLock()
40681 pFile->eFileLock = PENDING_LOCK; in unixLock()
40682 pInode->eFileLock = PENDING_LOCK; in unixLock()
40691 assert( pInode->nShared==0 ); in unixLock()
40692 assert( pInode->eFileLock==0 ); in unixLock()
40695 /* Now get the read-lock */ in unixLock()
40719 pFile->eFileLock = SHARED_LOCK; in unixLock()
40720 pInode->nLock++; in unixLock()
40721 pInode->nShared = 1; in unixLock()
40723 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ in unixLock()
40732 assert( 0!=pFile->eFileLock ); in unixLock()
40755 /* Set up the transaction-counter change checking flags when in unixLock()
40761 && pFile->eFileLock<=SHARED_LOCK in unixLock()
40764 pFile->transCntrChng = 0; in unixLock()
40765 pFile->dbUpdate = 0; in unixLock()
40766 pFile->inNormalWrite = 1; in unixLock()
40771 pFile->eFileLock = eFileLock; in unixLock()
40772 pInode->eFileLock = eFileLock; in unixLock()
40776 sqlite3_mutex_leave(pInode->pLockMutex); in unixLock()
40777 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), in unixLock()
40787 unixInodeInfo *pInode = pFile->pInode; in setPendingFd()
40788 UnixUnusedFd *p = pFile->pPreallocatedUnused; in setPendingFd()
40790 p->pNext = pInode->pUnused; in setPendingFd()
40791 pInode->pUnused = p; in setPendingFd()
40792 pFile->h = -1; in setPendingFd()
40793 pFile->pPreallocatedUnused = 0; in setPendingFd()
40801 ** the requested locking level, this routine is a no-op.
40816 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, in posixUnlock()
40817 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, in posixUnlock()
40821 if( pFile->eFileLock<=eFileLock ){ in posixUnlock()
40824 pInode = pFile->pInode; in posixUnlock()
40825 sqlite3_mutex_enter(pInode->pLockMutex); in posixUnlock()
40826 assert( pInode->nShared!=0 ); in posixUnlock()
40827 if( pFile->eFileLock>SHARED_LOCK ){ in posixUnlock()
40828 assert( pInode->eFileLock==pFile->eFileLock ); in posixUnlock()
40839 pFile->inNormalWrite = 0; in posixUnlock()
40843 ** before establishing the readlock - to avoid a race condition we downgrade in posixUnlock()
40859 off_t divSize = SHARED_SIZE - 1; in posixUnlock()
40865 if( unixFileLock(pFile, &lock)==(-1) ){ in posixUnlock()
40875 if( unixFileLock(pFile, &lock)==(-1) ){ in posixUnlock()
40886 lock.l_len = SHARED_SIZE-divSize; in posixUnlock()
40887 if( unixFileLock(pFile, &lock)==(-1) ){ in posixUnlock()
40918 pInode->eFileLock = SHARED_LOCK; in posixUnlock()
40930 pInode->nShared--; in posixUnlock()
40931 if( pInode->nShared==0 ){ in posixUnlock()
40936 pInode->eFileLock = NO_LOCK; in posixUnlock()
40940 pInode->eFileLock = NO_LOCK; in posixUnlock()
40941 pFile->eFileLock = NO_LOCK; in posixUnlock()
40949 pInode->nLock--; in posixUnlock()
40950 assert( pInode->nLock>=0 ); in posixUnlock()
40951 if( pInode->nLock==0 ) closePendingFds(pFile); in posixUnlock()
40955 sqlite3_mutex_leave(pInode->pLockMutex); in posixUnlock()
40957 pFile->eFileLock = eFileLock; in posixUnlock()
40967 ** the requested locking level, this routine is a no-op.
40971 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); in unixUnlock()
40996 if( pFile->h>=0 ){ in closeUnixFile()
40997 robust_close(pFile, pFile->h, __LINE__); in closeUnixFile()
40998 pFile->h = -1; in closeUnixFile()
41001 if( pFile->pId ){ in closeUnixFile()
41002 if( pFile->ctrlFlags & UNIXFILE_DELETE ){ in closeUnixFile()
41003 osUnlink(pFile->pId->zCanonicalName); in closeUnixFile()
41005 vxworksReleaseFileId(pFile->pId); in closeUnixFile()
41006 pFile->pId = 0; in closeUnixFile()
41010 if( pFile->ctrlFlags & UNIXFILE_DELETE ){ in closeUnixFile()
41011 osUnlink(pFile->zPath); in closeUnixFile()
41012 sqlite3_free(*(char**)&pFile->zPath); in closeUnixFile()
41013 pFile->zPath = 0; in closeUnixFile()
41016 OSTRACE(("CLOSE %-3d\n", pFile->h)); in closeUnixFile()
41017 OpenCounter(-1); in closeUnixFile()
41018 sqlite3_free(pFile->pPreallocatedUnused); in closeUnixFile()
41029 unixInodeInfo *pInode = pFile->pInode; in unixClose()
41040 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); in unixClose()
41041 sqlite3_mutex_enter(pInode->pLockMutex); in unixClose()
41042 if( pInode->nLock ){ in unixClose()
41045 ** descriptor to pInode->pUnused list. It will be automatically closed in unixClose()
41050 sqlite3_mutex_leave(pInode->pLockMutex); in unixClose()
41052 assert( pFile->pShm==0 ); in unixClose()
41062 ****************************** No-op Locking **********************************
41068 ** This locking mode is appropriate for use on read-only databases
41069 ** (ex: databases that are burned into CD-ROM, for example.) It can
41099 /******************* End of the no-op lock implementation *********************
41103 ************************* Begin dot-file Locking ******************************
41135 ** is assumed another client holds RESERVED if the lock-file exists.
41141 if( pFile->eFileLock>=SHARED_LOCK ){ in dotlockCheckReservedLock()
41144 *pResOut = osAccess((const char*)pFile->lockingContext, 0)==0; in dotlockCheckReservedLock()
41146 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, 0, *pResOut)); in dotlockCheckReservedLock()
41151 ** Lock the file with the lock specified by parameter eFileLock - one
41165 ** UNLOCKED -> SHARED
41166 ** SHARED -> RESERVED
41167 ** SHARED -> (PENDING) -> EXCLUSIVE
41168 ** RESERVED -> (PENDING) -> EXCLUSIVE
41169 ** PENDING -> EXCLUSIVE
41179 char *zLockFile = (char *)pFile->lockingContext; in dotlockLock()
41186 if( pFile->eFileLock > NO_LOCK ){ in dotlockLock()
41187 pFile->eFileLock = eFileLock; in dotlockLock()
41214 pFile->eFileLock = eFileLock; in dotlockLock()
41223 ** the requested locking level, this routine is a no-op.
41229 char *zLockFile = (char *)pFile->lockingContext; in dotlockUnlock()
41233 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, in dotlockUnlock()
41234 pFile->eFileLock, osGetpid(0))); in dotlockUnlock()
41237 /* no-op if possible */ in dotlockUnlock()
41238 if( pFile->eFileLock==eFileLock ){ in dotlockUnlock()
41246 pFile->eFileLock = SHARED_LOCK; in dotlockUnlock()
41263 pFile->eFileLock = NO_LOCK; in dotlockUnlock()
41274 sqlite3_free(pFile->lockingContext); in dotlockClose()
41277 /****************** End of the dot-file lock implementation *******************
41285 ** flock() locking is like dot-file locking in that the various
41286 ** fine-grain locking levels supported by SQLite are collapsed into
41313 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41326 assert( pFile->eFileLock<=SHARED_LOCK ); in flockCheckReservedLock()
41339 ** work with the flock VFS - as it always takes EXCLUSIVE locks - so it is in flockCheckReservedLock()
41347 ** Lock the file with the lock specified by parameter eFileLock - one
41361 ** UNLOCKED -> SHARED
41362 ** SHARED -> RESERVED
41363 ** SHARED -> (PENDING) -> EXCLUSIVE
41364 ** RESERVED -> (PENDING) -> EXCLUSIVE
41365 ** PENDING -> EXCLUSIVE
41383 if (pFile->eFileLock > NO_LOCK) { in flockLock()
41384 pFile->eFileLock = eFileLock; in flockLock()
41390 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { in flockLock()
41399 pFile->eFileLock = eFileLock; in flockLock()
41401 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), in flockLock()
41417 ** the requested locking level, this routine is a no-op.
41423 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, in flockUnlock()
41424 pFile->eFileLock, osGetpid(0))); in flockUnlock()
41427 /* no-op if possible */ in flockUnlock()
41428 if( pFile->eFileLock==eFileLock ){ in flockUnlock()
41434 pFile->eFileLock = eFileLock; in flockUnlock()
41439 if( robust_flock(pFile->h, LOCK_UN) ){ in flockUnlock()
41445 pFile->eFileLock = NO_LOCK; in flockUnlock()
41469 ** Semaphore locking is like dot-lock and flock in that it really only
41479 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41492 if( pFile->eFileLock>SHARED_LOCK ){ in semXCheckReservedLock()
41498 sem_t *pSem = pFile->pInode->pSem; in semXCheckReservedLock()
41500 if( sem_trywait(pSem)==-1 ){ in semXCheckReservedLock()
41507 reserved = (pFile->eFileLock < SHARED_LOCK); in semXCheckReservedLock()
41514 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); in semXCheckReservedLock()
41521 ** Lock the file with the lock specified by parameter eFileLock - one
41535 ** UNLOCKED -> SHARED
41536 ** SHARED -> RESERVED
41537 ** SHARED -> (PENDING) -> EXCLUSIVE
41538 ** RESERVED -> (PENDING) -> EXCLUSIVE
41539 ** PENDING -> EXCLUSIVE
41551 sem_t *pSem = pFile->pInode->pSem; in semXLock()
41556 if (pFile->eFileLock > NO_LOCK) { in semXLock()
41557 pFile->eFileLock = eFileLock; in semXLock()
41563 if( sem_trywait(pSem)==-1 ){ in semXLock()
41569 pFile->eFileLock = eFileLock; in semXLock()
41580 ** the requested locking level, this routine is a no-op.
41584 sem_t *pSem = pFile->pInode->pSem; in semXUnlock()
41588 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, in semXUnlock()
41589 pFile->eFileLock, osGetpid(0))); in semXUnlock()
41592 /* no-op if possible */ in semXUnlock()
41593 if( pFile->eFileLock==eFileLock ){ in semXUnlock()
41599 pFile->eFileLock = eFileLock; in semXUnlock()
41604 if ( sem_post(pSem)==-1 ) { in semXUnlock()
41612 pFile->eFileLock = NO_LOCK; in semXUnlock()
41645 ** on Apple Macintosh computers - both OS9 and OSX.
41647 ** Third-party implementations of AFP are available. But this code here
41664 unsigned long long length; /* nbr of bytes to lock */ member
41674 ** This is a utility for setting or clearing a bit-range lock on an
41683 unsigned long long length, /* Number of bytes to lock */ in afpSetLock() argument
41692 pb.length = length; in afpSetLock()
41693 pb.fd = pFile->h; in afpSetLock()
41696 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), in afpSetLock()
41697 offset, length)); in afpSetLock()
41699 if ( err==-1 ) { in afpSetLock()
41722 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41734 context = (afpLockingContext *) pFile->lockingContext; in afpCheckReservedLock()
41735 if( context->reserved ){ in afpCheckReservedLock()
41739 sqlite3_mutex_enter(pFile->pInode->pLockMutex); in afpCheckReservedLock()
41741 if( pFile->pInode->eFileLock>SHARED_LOCK ){ in afpCheckReservedLock()
41749 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); in afpCheckReservedLock()
41753 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); in afpCheckReservedLock()
41763 sqlite3_mutex_leave(pFile->pInode->pLockMutex); in afpCheckReservedLock()
41764 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); in afpCheckReservedLock()
41771 ** Lock the file with the lock specified by parameter eFileLock - one
41785 ** UNLOCKED -> SHARED
41786 ** SHARED -> RESERVED
41787 ** SHARED -> (PENDING) -> EXCLUSIVE
41788 ** RESERVED -> (PENDING) -> EXCLUSIVE
41789 ** PENDING -> EXCLUSIVE
41797 unixInodeInfo *pInode = pFile->pInode; in afpLock()
41798 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; in afpLock()
41801 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, in afpLock()
41802 azFileLock(eFileLock), azFileLock(pFile->eFileLock), in afpLock()
41803 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); in afpLock()
41809 if( pFile->eFileLock>=eFileLock ){ in afpLock()
41810 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, in afpLock()
41820 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); in afpLock()
41822 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); in afpLock()
41824 /* This mutex is needed because pFile->pInode is shared across threads in afpLock()
41826 pInode = pFile->pInode; in afpLock()
41827 sqlite3_mutex_enter(pInode->pLockMutex); in afpLock()
41832 if( (pFile->eFileLock!=pInode->eFileLock && in afpLock()
41833 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) in afpLock()
41844 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ in afpLock()
41846 assert( pFile->eFileLock==0 ); in afpLock()
41847 assert( pInode->nShared>0 ); in afpLock()
41848 pFile->eFileLock = SHARED_LOCK; in afpLock()
41849 pInode->nShared++; in afpLock()
41850 pInode->nLock++; in afpLock()
41859 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) in afpLock()
41862 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); in afpLock()
41876 assert( pInode->nShared==0 ); in afpLock()
41877 assert( pInode->eFileLock==0 ); in afpLock()
41880 /* Now get the read-lock SHARED_LOCK */ in afpLock()
41883 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); in afpLock()
41884 lrc1 = afpSetLock(context->dbPath, pFile, in afpLock()
41885 SHARED_FIRST+pInode->sharedByte, 1, 1); in afpLock()
41887 lrc1Errno = pFile->lastErrno; in afpLock()
41890 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); in afpLock()
41902 pFile->eFileLock = SHARED_LOCK; in afpLock()
41903 pInode->nLock++; in afpLock()
41904 pInode->nShared = 1; in afpLock()
41906 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ in afpLock()
41916 assert( 0!=pFile->eFileLock ); in afpLock()
41917 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { in afpLock()
41919 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); in afpLock()
41921 context->reserved = 1; in afpLock()
41930 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + in afpLock()
41931 pInode->sharedByte, 1, 0)) ){ in afpLock()
41934 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, in afpLock()
41936 if( failed && (failed2 = afpSetLock(context->dbPath, pFile, in afpLock()
41937 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ in afpLock()
41955 pFile->eFileLock = eFileLock; in afpLock()
41956 pInode->eFileLock = eFileLock; in afpLock()
41958 pFile->eFileLock = PENDING_LOCK; in afpLock()
41959 pInode->eFileLock = PENDING_LOCK; in afpLock()
41963 sqlite3_mutex_leave(pInode->pLockMutex); in afpLock()
41964 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), in afpLock()
41974 ** the requested locking level, this routine is a no-op.
41980 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; in afpUnlock()
41984 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, in afpUnlock()
41985 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, in afpUnlock()
41989 if( pFile->eFileLock<=eFileLock ){ in afpUnlock()
41992 pInode = pFile->pInode; in afpUnlock()
41993 sqlite3_mutex_enter(pInode->pLockMutex); in afpUnlock()
41994 assert( pInode->nShared!=0 ); in afpUnlock()
41995 if( pFile->eFileLock>SHARED_LOCK ){ in afpUnlock()
41996 assert( pInode->eFileLock==pFile->eFileLock ); in afpUnlock()
42007 assert( pFile->inNormalWrite==0 in afpUnlock()
42008 || pFile->dbUpdate==0 in afpUnlock()
42009 || pFile->transCntrChng==1 ); in afpUnlock()
42010 pFile->inNormalWrite = 0; in afpUnlock()
42013 if( pFile->eFileLock==EXCLUSIVE_LOCK ){ in afpUnlock()
42014 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); in afpUnlock()
42015 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ in afpUnlock()
42016 /* only re-establish the shared lock if necessary */ in afpUnlock()
42017 int sharedLockByte = SHARED_FIRST+pInode->sharedByte; in afpUnlock()
42018 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); in afpUnlock()
42023 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ in afpUnlock()
42024 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); in afpUnlock()
42026 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ in afpUnlock()
42027 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); in afpUnlock()
42029 context->reserved = 0; in afpUnlock()
42032 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ in afpUnlock()
42033 pInode->eFileLock = SHARED_LOCK; in afpUnlock()
42042 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; in afpUnlock()
42043 pInode->nShared--; in afpUnlock()
42044 if( pInode->nShared==0 ){ in afpUnlock()
42046 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); in afpUnlock()
42049 pInode->eFileLock = NO_LOCK; in afpUnlock()
42050 pFile->eFileLock = NO_LOCK; in afpUnlock()
42054 pInode->nLock--; in afpUnlock()
42055 assert( pInode->nLock>=0 ); in afpUnlock()
42056 if( pInode->nLock==0 ) closePendingFds(pFile); in afpUnlock()
42060 sqlite3_mutex_leave(pInode->pLockMutex); in afpUnlock()
42062 pFile->eFileLock = eFileLock; in afpUnlock()
42077 if( pFile->pInode ){ in afpClose()
42078 unixInodeInfo *pInode = pFile->pInode; in afpClose()
42079 sqlite3_mutex_enter(pInode->pLockMutex); in afpClose()
42080 if( pInode->nLock ){ in afpClose()
42083 ** descriptor to pInode->aPending. It will be automatically closed when in afpClose()
42088 sqlite3_mutex_leave(pInode->pLockMutex); in afpClose()
42091 sqlite3_free(pFile->lockingContext); in afpClose()
42101 ** is available. If you don't compile for a mac, then the "unix-afp"
42116 ** the requested locking level, this routine is a no-op.
42132 **************** Non-locking sqlite3_file methods *****************************
42156 assert( id->h>2 ); in seekAndRead()
42159 got = osPread(id->h, pBuf, cnt, offset); in seekAndRead()
42160 SimulateIOError( got = -1 ); in seekAndRead()
42162 got = osPread64(id->h, pBuf, cnt, offset); in seekAndRead()
42163 SimulateIOError( got = -1 ); in seekAndRead()
42165 newOffset = lseek(id->h, offset, SEEK_SET); in seekAndRead()
42166 SimulateIOError( newOffset = -1 ); in seekAndRead()
42169 return -1; in seekAndRead()
42171 got = osRead(id->h, pBuf, cnt); in seekAndRead()
42180 cnt -= got; in seekAndRead()
42187 OSTRACE(("READ %-3d %5d %7lld %llu\n", in seekAndRead()
42188 id->h, got+prior, offset-prior, TIMER_ELAPSED)); in seekAndRead()
42209 /* If this is a database file (not a journal, super-journal or temp in unixRead()
42212 assert( pFile->pPreallocatedUnused==0 in unixRead()
42221 if( offset<pFile->mmapSize ){ in unixRead()
42222 if( offset+amt <= pFile->mmapSize ){ in unixRead()
42223 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); in unixRead()
42226 int nCopy = pFile->mmapSize - offset; in unixRead()
42227 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); in unixRead()
42229 amt -= nCopy; in unixRead()
42239 /* pFile->lastErrno has been set by seekAndRead(). in unixRead()
42246 switch( pFile->lastErrno ){ in unixRead()
42260 /* Unread parts of the buffer must be zero-filled */ in unixRead()
42261 memset(&((char*)pBuf)[got], 0, amt-got); in unixRead()
42267 ** Attempt to seek the file-descriptor passed as the first argument to
42269 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
42295 SimulateIOError( iSeek = -1 ); in seekAndWriteFd()
42297 rc = -1; in seekAndWriteFd()
42305 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); in seekAndWriteFd()
42313 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
42320 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); in seekAndWrite()
42339 /* If this is a database file (not a journal, super-journal or temp in unixWrite()
42342 assert( pFile->pPreallocatedUnused==0 in unixWrite()
42350 ** doing a hot-journal rollback or a write to some file other than a in unixWrite()
42355 if( pFile->inNormalWrite ){ in unixWrite()
42356 pFile->dbUpdate = 1; /* The database has been modified */ in unixWrite()
42363 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ in unixWrite()
42364 pFile->transCntrChng = 1; /* The transaction counter has changed */ in unixWrite()
42373 if( offset<pFile->mmapSize ){ in unixWrite()
42374 if( offset+amt <= pFile->mmapSize ){ in unixWrite()
42375 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); in unixWrite()
42378 int nCopy = pFile->mmapSize - offset; in unixWrite()
42379 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); in unixWrite()
42381 amt -= nCopy; in unixWrite()
42388 amt -= wrote; in unixWrite()
42392 SimulateIOError(( wrote=(-1), amt=1 )); in unixWrite()
42396 if( wrote<0 && pFile->lastErrno!=ENOSPC ){ in unixWrite()
42421 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
42491 ** no-op. But go ahead and call fstat() to validate the file in full_fsync()
42524 if( rc==-1 && errno==ENOTSUP ){ in full_fsync()
42530 if( OS_VXWORKS && rc!= -1 ){ in full_fsync()
42543 ** The directory file descriptor is used for only one thing - to
42552 ** replace this routine with a harmless no-op. To make this routine
42553 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
42561 int fd = -1; in openDirectory()
42565 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); in openDirectory()
42574 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); in openDirectory()
42589 ** has been created by fsync-ing the directory that contains the file.
42594 ** will not roll back - possibly leading to database corruption.
42614 OSTRACE(("SYNC %-3d\n", pFile->h)); in unixSync()
42615 rc = full_fsync(pFile->h, isFullsync, isDataOnly); in unixSync()
42619 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); in unixSync()
42623 ** is set. This is a one-time occurrence. Many systems (examples: AIX) in unixSync()
42626 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ in unixSync()
42628 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, in unixSync()
42630 rc = osOpenDirectory(pFile->zPath, &dirfd); in unixSync()
42638 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; in unixSync()
42652 /* If the user has configured a chunk-size for this file, truncate the in unixTruncate()
42657 if( pFile->szChunk>0 ){ in unixTruncate()
42658 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; in unixTruncate()
42661 rc = robust_ftruncate(pFile->h, nByte); in unixTruncate()
42664 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); in unixTruncate()
42668 ** doing a hot-journal rollback or a write to some file other than a in unixTruncate()
42669 ** normal database file) and we truncate the file to zero length, in unixTruncate()
42671 ** when restoring a database using the backup API from a zero-length in unixTruncate()
42674 if( pFile->inNormalWrite && nByte==0 ){ in unixTruncate()
42675 pFile->transCntrChng = 1; in unixTruncate()
42684 if( nByte<pFile->mmapSize ){ in unixTruncate()
42685 pFile->mmapSize = nByte; in unixTruncate()
42700 rc = osFstat(((unixFile*)id)->h, &buf); in unixFileSize()
42708 /* When opening a zero-size database, the findInodeInfo() procedure in unixFileSize()
42710 ** in the OS-X msdos filesystem. In order to avoid problems with upper in unixFileSize()
42722 ** Handler for proxy-locking file-control verbs. Defined below in the
42730 ** file-control operation. Enlarge the database to nBytes in size
42731 ** (rounded up to the next chunk-size). If the database is already
42732 ** nBytes or larger, this routine is a no-op.
42735 if( pFile->szChunk>0 ){ in fcntlSizeHint()
42739 if( osFstat(pFile->h, &buf) ){ in fcntlSizeHint()
42743 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; in fcntlSizeHint()
42752 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); in fcntlSizeHint()
42759 ** at offset (nSize-1), to set the size of the file correctly. in fcntlSizeHint()
42763 int nBlk = buf.st_blksize; /* File-system block size */ in fcntlSizeHint()
42767 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; in fcntlSizeHint()
42770 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ in fcntlSizeHint()
42771 if( iWrite>=nSize ) iWrite = nSize - 1; in fcntlSizeHint()
42780 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ in fcntlSizeHint()
42782 if( pFile->szChunk<=0 ){ in fcntlSizeHint()
42783 if( robust_ftruncate(pFile->h, nByte) ){ in fcntlSizeHint()
42785 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); in fcntlSizeHint()
42799 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
42801 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
42805 *pArg = (pFile->ctrlFlags & mask)!=0; in unixModeBit()
42807 pFile->ctrlFlags &= ~mask; in unixModeBit()
42809 pFile->ctrlFlags |= mask; in unixModeBit()
42827 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); in unixFileControl()
42831 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); in unixFileControl()
42835 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); in unixFileControl()
42841 osClose(pFile->h); in unixFileControl()
42842 pFile->h = -1; in unixFileControl()
42846 *(int*)pArg = pFile->eFileLock; in unixFileControl()
42850 *(int*)pArg = pFile->lastErrno; in unixFileControl()
42854 pFile->szChunk = *(int *)pArg; in unixFileControl()
42873 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); in unixFileControl()
42877 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); in unixFileControl()
42879 unixGetTempname(pFile->pVfs->mxPathname, zTFile); in unixFileControl()
42890 int iOld = pFile->iBusyTimeout; in unixFileControl()
42893 pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew; in unixFileControl()
42895 pFile->iBusyTimeout = !!(*(int*)pArg); in unixFileControl()
42904 pFile->bBlockOnConnect = iNew; in unixFileControl()
42918 ** 64-bit type. */ in unixFileControl()
42923 *(i64*)pArg = pFile->mmapSizeMax; in unixFileControl()
42924 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ in unixFileControl()
42925 pFile->mmapSizeMax = newLimit; in unixFileControl()
42926 if( pFile->mmapSize>0 ){ in unixFileControl()
42928 rc = unixMapfile(pFile, -1); in unixFileControl()
42941 ((unixFile*)id)->dbUpdate = 0; in unixFileControl()
42965 ** If pFd->sectorSize is non-zero when this function is called, it is a
42966 ** no-op. Otherwise, the values of pFd->sectorSize and
42967 ** pFd->deviceCharacteristics are set according to the file-system
42975 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); in setDeviceCharacteristics()
42976 if( pFd->sectorSize==0 ){ in setDeviceCharacteristics()
42982 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); in setDeviceCharacteristics()
42984 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; in setDeviceCharacteristics()
42989 if( pFd->ctrlFlags & UNIXFILE_PSOW ){ in setDeviceCharacteristics()
42990 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; in setDeviceCharacteristics()
42992 pFd->deviceCharacteristics |= SQLITE_IOCAP_SUBPAGE_READ; in setDeviceCharacteristics()
42994 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; in setDeviceCharacteristics()
43001 if( pFile->sectorSize == 0 ){ in setDeviceCharacteristics()
43004 /* Set defaults for non-supported filesystems */ in setDeviceCharacteristics()
43005 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; in setDeviceCharacteristics()
43006 pFile->deviceCharacteristics = 0; in setDeviceCharacteristics()
43007 if( fstatvfs(pFile->h, &fsInfo) == -1 ) { in setDeviceCharacteristics()
43012 pFile->sectorSize = fsInfo.f_bsize; in setDeviceCharacteristics()
43013 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43021 pFile->sectorSize = fsInfo.f_bsize; in setDeviceCharacteristics()
43022 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43024 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | in setDeviceCharacteristics()
43031 pFile->sectorSize = fsInfo.f_bsize; in setDeviceCharacteristics()
43032 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43040 pFile->sectorSize = fsInfo.f_bsize; in setDeviceCharacteristics()
43041 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43043 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) | in setDeviceCharacteristics()
43048 pFile->sectorSize = fsInfo.f_bsize; in setDeviceCharacteristics()
43049 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43051 (((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2) | in setDeviceCharacteristics()
43056 pFile->deviceCharacteristics = in setDeviceCharacteristics()
43065 if( pFile->sectorSize % 512 != 0 ){ in setDeviceCharacteristics()
43066 pFile->deviceCharacteristics = 0; in setDeviceCharacteristics()
43067 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; in setDeviceCharacteristics()
43078 ** if two files are created in the same file-system directory (i.e.
43085 return pFd->sectorSize; in unixSectorSize()
43094 ** words, after a power-loss event, parts of the file that were never
43095 ** written might end up being altered.) However, non-PSOW behavior is very,
43098 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
43104 return pFd->deviceCharacteristics; in unixDeviceCharacteristics()
43132 ** When multiple threads all reference the same wal-index, each thread
43134 ** of this unixShmNode object. In other words, each wal-index is opened
43149 ** The following fields are read-only after the object is created:
43161 ** locks are held by the process on this slot. If it is set to -1, then
43172 ** of mutexes - one for each locking slot. To read or write locking
43182 int szRegion; /* Size of shared-memory regions */
43184 u8 isReadonly; /* True if read-only */
43186 char **apRegion; /* Array of mapped shared-memory regions */
43192 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
43203 ** are read-only thereafter:
43208 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
43214 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
43228 ** wal-mode transactions in other processes on database file pFile. If
43237 if( pFile->pShm){ in unixFcntlExternalReader()
43238 unixShmNode *pShmNode = pFile->pShm->pShmNode; in unixFcntlExternalReader()
43245 f.l_len = SQLITE_SHM_NLOCK - 3; in unixFcntlExternalReader()
43247 sqlite3_mutex_enter(pShmNode->pShmMutex); in unixFcntlExternalReader()
43248 if( osFcntl(pShmNode->hShm, F_GETLK, &f)<0 ){ in unixFcntlExternalReader()
43253 sqlite3_mutex_leave(pShmNode->pShmMutex); in unixFcntlExternalReader()
43261 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
43263 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
43272 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ in unixShmSystemLock()
43276 pShmNode = pFile->pInode->pShmNode; in unixShmSystemLock()
43280 assert( pShmNode->nRef>=0 ); in unixShmSystemLock()
43285 assert( pShmNode->nRef>0 || unixMutexHeld() ); in unixShmSystemLock()
43286 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); in unixShmSystemLock()
43290 for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){ in unixShmSystemLock()
43291 assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) ); in unixShmSystemLock()
43294 assert( sqlite3_mutex_held(pShmNode->pShmMutex) ); in unixShmSystemLock()
43295 assert( pShmNode->nRef>0 ); in unixShmSystemLock()
43306 if( pShmNode->hShm>=0 ){ in unixShmSystemLock()
43313 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); in unixShmSystemLock()
43314 if( res==-1 ){ in unixShmSystemLock()
43316 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); in unixShmSystemLock()
43325 OSTRACE(("SHM-LOCK ")); in unixShmSystemLock()
43328 OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1)); in unixShmSystemLock()
43330 OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1)); in unixShmSystemLock()
43333 OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1)); in unixShmSystemLock()
43337 OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1)); in unixShmSystemLock()
43339 OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1)); in unixShmSystemLock()
43342 OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1)); in unixShmSystemLock()
43353 ** current system page-size.
43356 ** to use 64KB pages - in this case each mapping must cover at least two
43362 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ in unixShmRegionPerMap()
43370 ** This is not a VFS shared-memory method; it is a utility function called
43371 ** by VFS shared-memory methods.
43374 unixShmNode *p = pFd->pInode->pShmNode; in unixShmPurge()
43376 if( p && ALWAYS(p->nRef==0) ){ in unixShmPurge()
43379 assert( p->pInode==pFd->pInode ); in unixShmPurge()
43380 sqlite3_mutex_free(p->pShmMutex); in unixShmPurge()
43383 sqlite3_mutex_free(p->aMutex[i]); in unixShmPurge()
43386 for(i=0; i<p->nRegion; i+=nShmPerMap){ in unixShmPurge()
43387 if( p->hShm>=0 ){ in unixShmPurge()
43388 osMunmap(p->apRegion[i], p->szRegion); in unixShmPurge()
43390 sqlite3_free(p->apRegion[i]); in unixShmPurge()
43393 sqlite3_free(p->apRegion); in unixShmPurge()
43394 if( p->hShm>=0 ){ in unixShmPurge()
43395 robust_close(pFd, p->hShm, __LINE__); in unixShmPurge()
43396 p->hShm = -1; in unixShmPurge()
43398 p->pInode->pShmNode = 0; in unixShmPurge()
43410 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
43419 ** and proceed with opening the *-shm file. in unixLockSharedMemory()
43423 ** DMS byte and truncate the *-shm file to zero bytes in size. Then in unixLockSharedMemory()
43430 ** EXCLUSIVE failed just before truncating the *-shm file, then this in unixLockSharedMemory()
43431 ** process might open and use the *-shm file without truncating it. in unixLockSharedMemory()
43432 ** And if the *-shm file has been corrupted by a power failure or in unixLockSharedMemory()
43438 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { in unixLockSharedMemory()
43441 if( pShmNode->isReadonly ){ in unixLockSharedMemory()
43442 pShmNode->isUnlocked = 1; in unixLockSharedMemory()
43448 ** *-shm file. And after it has done so, it will not release its in unixLockSharedMemory()
43452 int iSaveTimeout = pDbFd->iBusyTimeout; in unixLockSharedMemory()
43453 pDbFd->iBusyTimeout = 0; in unixLockSharedMemory()
43457 pDbFd->iBusyTimeout = iSaveTimeout; in unixLockSharedMemory()
43459 /* The first connection to attach must truncate the -shm file. We in unixLockSharedMemory()
43461 ** -shm header size) rather than 0 as a system debugging aid, to in unixLockSharedMemory()
43462 ** help detect if a -shm file truncation is legitimate or is the work in unixLockSharedMemory()
43464 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ in unixLockSharedMemory()
43465 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); in unixLockSharedMemory()
43480 ** Open a shared-memory area associated with open database file pDbFd.
43483 ** The file used to implement shared-memory is in the same directory
43485 ** file with the "-shm" suffix added. For example, if the database file
43487 ** for shared memory will be called "/home/user1/config.db-shm".
43493 ** database to end up using different files for shared memory -
43494 ** meaning that their memory would not really be shared - resulting
43496 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
43497 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
43501 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
43504 ** When opening a new shared-memory file, if no other instances of that
43506 ** the file must be truncated to zero length or have its header cleared.
43508 ** If the original database file (pDbFd) is using the "unix-excl" VFS
43526 assert( pDbFd->pShm==0 ); in unixOpenSharedMemory()
43533 pInode = pDbFd->pInode; in unixOpenSharedMemory()
43534 pShmNode = pInode->pShmNode; in unixOpenSharedMemory()
43538 const char *zBasePath = pDbFd->zPath; in unixOpenSharedMemory()
43542 ** a new *-shm file is created, an attempt will be made to create it in unixOpenSharedMemory()
43545 if( osFstat(pDbFd->h, &sStat) ){ in unixOpenSharedMemory()
43561 zShm = pShmNode->zFilename = (char*)&pShmNode[1]; in unixOpenSharedMemory()
43564 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", in unixOpenSharedMemory()
43567 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); in unixOpenSharedMemory()
43568 sqlite3FileSuffix3(pDbFd->zPath, zShm); in unixOpenSharedMemory()
43570 pShmNode->hShm = -1; in unixOpenSharedMemory()
43571 pDbFd->pInode->pShmNode = pShmNode; in unixOpenSharedMemory()
43572 pShmNode->pInode = pDbFd->pInode; in unixOpenSharedMemory()
43574 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); in unixOpenSharedMemory()
43575 if( pShmNode->pShmMutex==0 ){ in unixOpenSharedMemory()
43583 pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); in unixOpenSharedMemory()
43584 if( pShmNode->aMutex[ii]==0 ){ in unixOpenSharedMemory()
43593 if( pInode->bProcessLock==0 ){ in unixOpenSharedMemory()
43594 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ in unixOpenSharedMemory()
43595 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW, in unixOpenSharedMemory()
43598 if( pShmNode->hShm<0 ){ in unixOpenSharedMemory()
43599 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW, in unixOpenSharedMemory()
43601 if( pShmNode->hShm<0 ){ in unixOpenSharedMemory()
43605 pShmNode->isReadonly = 1; in unixOpenSharedMemory()
43612 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); in unixOpenSharedMemory()
43620 p->pShmNode = pShmNode; in unixOpenSharedMemory()
43622 p->id = pShmNode->nextShmId++; in unixOpenSharedMemory()
43624 pShmNode->nRef++; in unixOpenSharedMemory()
43625 pDbFd->pShm = p; in unixOpenSharedMemory()
43632 ** at pShmNode->pFirst. This must be done while holding the in unixOpenSharedMemory()
43633 ** pShmNode->pShmMutex. in unixOpenSharedMemory()
43635 sqlite3_mutex_enter(pShmNode->pShmMutex); in unixOpenSharedMemory()
43636 p->pNext = pShmNode->pFirst; in unixOpenSharedMemory()
43637 pShmNode->pFirst = p; in unixOpenSharedMemory()
43638 sqlite3_mutex_leave(pShmNode->pShmMutex); in unixOpenSharedMemory()
43651 ** shared-memory associated with the database file fd. Shared-memory regions
43652 ** are numbered starting from zero. Each shared-memory region is szRegion
43657 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
43660 ** bExtend is non-zero and the requested shared-memory region has not yet
43663 ** If the shared-memory region has already been allocated or is allocated by
43682 /* If the shared-memory file has not yet been opened, open it now. */ in unixShmMap()
43683 if( pDbFd->pShm==0 ){ in unixShmMap()
43688 p = pDbFd->pShm; in unixShmMap()
43689 pShmNode = p->pShmNode; in unixShmMap()
43690 sqlite3_mutex_enter(pShmNode->pShmMutex); in unixShmMap()
43691 if( pShmNode->isUnlocked ){ in unixShmMap()
43694 pShmNode->isUnlocked = 0; in unixShmMap()
43696 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); in unixShmMap()
43697 assert( pShmNode->pInode==pDbFd->pInode ); in unixShmMap()
43698 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); in unixShmMap()
43699 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); in unixShmMap()
43704 if( pShmNode->nRegion<nReqRegion ){ in unixShmMap()
43709 pShmNode->szRegion = szRegion; in unixShmMap()
43711 if( pShmNode->hShm>=0 ){ in unixShmMap()
43713 ** Check to see if it has been allocated (i.e. if the wal-index file is in unixShmMap()
43716 if( osFstat(pShmNode->hShm, &sStat) ){ in unixShmMap()
43744 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ in unixShmMap()
43745 const char *zFile = pShmNode->zFilename; in unixShmMap()
43756 pShmNode->apRegion, nReqRegion*sizeof(char *) in unixShmMap()
43762 pShmNode->apRegion = apNew; in unixShmMap()
43763 while( pShmNode->nRegion<nReqRegion ){ in unixShmMap()
43767 if( pShmNode->hShm>=0 ){ in unixShmMap()
43769 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, in unixShmMap()
43770 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion in unixShmMap()
43773 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); in unixShmMap()
43786 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; in unixShmMap()
43788 pShmNode->nRegion += nShmPerMap; in unixShmMap()
43793 if( pShmNode->nRegion>iRegion ){ in unixShmMap()
43794 *pp = pShmNode->apRegion[iRegion]; in unixShmMap()
43798 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; in unixShmMap()
43799 sqlite3_mutex_leave(pShmNode->pShmMutex); in unixShmMap()
43804 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
43819 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ in assertLockingArrayOk()
43822 if( pX->exclMask & (1<<i) ){ in assertLockingArrayOk()
43824 aLock[i] = -1; in assertLockingArrayOk()
43825 }else if( pX->sharedMask & (1<<i) ){ in assertLockingArrayOk()
43832 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) ); in assertLockingArrayOk()
43833 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0); in assertLockingArrayOk()
43839 ** Change the lock state for a shared-memory segment.
43856 u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */ in unixShmLock()
43859 p = pDbFd->pShm; in unixShmLock()
43861 pShmNode = p->pShmNode; in unixShmLock()
43863 aLock = pShmNode->aLock; in unixShmLock()
43865 assert( pShmNode==pDbFd->pInode->pShmNode ); in unixShmLock()
43866 assert( pShmNode->pInode==pDbFd->pInode ); in unixShmLock()
43874 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); in unixShmLock()
43875 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); in unixShmLock()
43891 u16 lockMask = (p->exclMask|p->sharedMask); in unixShmLock()
43892 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( in unixShmLock()
43911 || 0==(p->exclMask & mask) in unixShmLock()
43913 if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask)) in unixShmLock()
43914 || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask)) in unixShmLock()
43927 ** case thread 2 does a non-blocking request for the WRITER lock. But - in unixShmLock()
43931 ** a lock on a read-locking slot at this point, this breaks the in unixShmLock()
43932 ** anti-deadlock rules (see above). */ in unixShmLock()
43937 rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]); in unixShmLock()
43940 sqlite3_mutex_enter(pShmNode->aMutex[iMutex]); in unixShmLock()
43944 sqlite3_mutex_enter(pShmNode->pShmMutex); in unixShmLock()
43949 /* Case (a) - unlock. */ in unixShmLock()
43951 assert( (p->exclMask & p->sharedMask)==0 ); in unixShmLock()
43952 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask ); in unixShmLock()
43953 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask ); in unixShmLock()
43958 ** from the file-descriptor below. */ in unixShmLock()
43964 aLock[ofst]--; in unixShmLock()
43965 p->sharedMask &= ~mask; in unixShmLock()
43973 p->sharedMask &= ~mask; in unixShmLock()
43974 p->exclMask &= ~mask; in unixShmLock()
43978 /* Case (b) - a shared lock. */ in unixShmLock()
43989 p->sharedMask |= mask; in unixShmLock()
43993 /* Case (c) - an exclusive lock. */ in unixShmLock()
43997 assert( (p->sharedMask & mask)==0 ); in unixShmLock()
43998 assert( (p->exclMask & mask)==0 ); in unixShmLock()
44010 ** also update the in-memory values. */ in unixShmLock()
44014 p->exclMask |= mask; in unixShmLock()
44016 aLock[ii] = -1; in unixShmLock()
44027 for(iMutex--; iMutex>=ofst; iMutex--){ in unixShmLock()
44028 sqlite3_mutex_leave(pShmNode->aMutex[iMutex]); in unixShmLock()
44031 sqlite3_mutex_leave(pShmNode->pShmMutex); in unixShmLock()
44035 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", in unixShmLock()
44036 p->id, osGetpid(0), p->sharedMask, p->exclMask)); in unixShmLock()
44050 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ in unixShmBarrier()
44051 assert( fd->pMethods->xLock==nolockLock in unixShmBarrier()
44059 ** Close a connection to shared-memory. Delete the underlying
44063 ** routine is a harmless no-op.
44067 int deleteFlag /* Delete shared-memory if true */ in unixShmUnmap()
44070 unixShmNode *pShmNode; /* The underlying shared-memory file */ in unixShmUnmap()
44075 p = pDbFd->pShm; in unixShmUnmap()
44077 pShmNode = p->pShmNode; in unixShmUnmap()
44079 assert( pShmNode==pDbFd->pInode->pShmNode ); in unixShmUnmap()
44080 assert( pShmNode->pInode==pDbFd->pInode ); in unixShmUnmap()
44084 sqlite3_mutex_enter(pShmNode->pShmMutex); in unixShmUnmap()
44085 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} in unixShmUnmap()
44086 *pp = p->pNext; in unixShmUnmap()
44090 pDbFd->pShm = 0; in unixShmUnmap()
44091 sqlite3_mutex_leave(pShmNode->pShmMutex); in unixShmUnmap()
44093 /* If pShmNode->nRef has reached 0, then close the underlying in unixShmUnmap()
44094 ** shared-memory file, too */ in unixShmUnmap()
44097 assert( pShmNode->nRef>0 ); in unixShmUnmap()
44098 pShmNode->nRef--; in unixShmUnmap()
44099 if( pShmNode->nRef==0 ){ in unixShmUnmap()
44100 if( deleteFlag && pShmNode->hShm>=0 ){ in unixShmUnmap()
44101 osUnlink(pShmNode->zFilename); in unixShmUnmap()
44123 assert( pFd->nFetchOut==0 ); in unixUnmapfile()
44124 if( pFd->pMapRegion ){ in unixUnmapfile()
44125 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); in unixUnmapfile()
44126 pFd->pMapRegion = 0; in unixUnmapfile()
44127 pFd->mmapSize = 0; in unixUnmapfile()
44128 pFd->mmapSizeActual = 0; in unixUnmapfile()
44152 int h = pFd->h; /* File descriptor open on db file */ in unixRemapfile()
44153 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ in unixRemapfile()
44154 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ in unixRemapfile()
44158 assert( pFd->nFetchOut==0 ); in unixRemapfile()
44159 assert( nNew>pFd->mmapSize ); in unixRemapfile()
44160 assert( nNew<=pFd->mmapSizeMax ); in unixRemapfile()
44162 assert( pFd->mmapSizeActual>=pFd->mmapSize ); in unixRemapfile()
44166 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; in unixRemapfile()
44171 i64 nReuse = pFd->mmapSize; in unixRemapfile()
44174 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); in unixRemapfile()
44180 osMunmap(pReq, nOrig-nReuse); in unixRemapfile()
44187 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); in unixRemapfile()
44190 osMunmap(pNew, nNew - nReuse); in unixRemapfile()
44212 unixLogError(SQLITE_OK, zErr, pFd->zPath); in unixRemapfile()
44217 pFd->mmapSizeMax = 0; in unixRemapfile()
44219 pFd->pMapRegion = (void *)pNew; in unixRemapfile()
44220 pFd->mmapSize = pFd->mmapSizeActual = nNew; in unixRemapfile()
44224 ** Memory map or remap the file opened by file-descriptor pFd (if the file
44227 ** outstanding xFetch() references to it, this function is a no-op.
44229 ** If parameter nByte is non-negative, then it is the requested size of
44240 assert( nMap>=0 || pFd->nFetchOut==0 ); in unixMapfile()
44241 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); in unixMapfile()
44242 if( pFd->nFetchOut>0 ) return SQLITE_OK; in unixMapfile()
44245 struct stat statbuf; /* Low-level file information */ in unixMapfile()
44246 if( osFstat(pFd->h, &statbuf) ){ in unixMapfile()
44251 if( nMap>pFd->mmapSizeMax ){ in unixMapfile()
44252 nMap = pFd->mmapSizeMax; in unixMapfile()
44255 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); in unixMapfile()
44256 if( nMap!=pFd->mmapSize ){ in unixMapfile()
44283 if( pFd->mmapSizeMax>0 ){ in unixFetch()
44289 if( pFd->pMapRegion==0 ){ in unixFetch()
44290 int rc = unixMapfile(pFd, -1); in unixFetch()
44293 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){ in unixFetch()
44294 *pp = &((u8 *)pFd->pMapRegion)[iOff]; in unixFetch()
44295 pFd->nFetchOut++; in unixFetch()
44303 ** If the third argument is non-NULL, then this function releases a
44320 assert( (p==0)==(pFd->nFetchOut==0) ); in unixUnfetch()
44323 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); in unixUnfetch()
44326 pFd->nFetchOut--; in unixUnfetch()
44331 assert( pFd->nFetchOut>=0 ); in unixUnfetch()
44349 ** of "finder" functions. A finder-function is used to locate the appropriate
44352 ** the correct finder-function for that VFS.
44355 ** object. The only interesting finder-function is autolockIoFinder, which
44359 ** For finder-function F, two objects are created:
44361 ** (1) The real finder-function named "FImpt()".
44368 ** directly at the finder-function since C90 rules prevent a void*
44485 ** The proxy locking method is a "super-method" in the sense that it
44487 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
44556 if( statfs(filePath, &fsInfo) != -1 ){ in autolockIoFinderImpl()
44568 ** Test byte-range lock using fcntl(). If the call succeeds, in autolockIoFinderImpl()
44569 ** assume that the file-system supports POSIX style locks. in autolockIoFinderImpl()
44575 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { in autolockIoFinderImpl()
44615 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { in vxworksIoFinderImpl()
44653 assert( pNew->pInode==NULL ); in fillInUnixFile()
44658 OSTRACE(("OPEN %-3d %s\n", h, zFilename)); in fillInUnixFile()
44659 pNew->h = h; in fillInUnixFile()
44660 pNew->pVfs = pVfs; in fillInUnixFile()
44661 pNew->zPath = zFilename; in fillInUnixFile()
44662 pNew->ctrlFlags = (u8)ctrlFlags; in fillInUnixFile()
44664 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; in fillInUnixFile()
44668 pNew->ctrlFlags |= UNIXFILE_PSOW; in fillInUnixFile()
44670 if( strcmp(pVfs->zName,"unix-excl")==0 ){ in fillInUnixFile()
44671 pNew->ctrlFlags |= UNIXFILE_EXCL; in fillInUnixFile()
44675 pNew->pId = vxworksFindFileId(zFilename); in fillInUnixFile()
44676 if( pNew->pId==0 ){ in fillInUnixFile()
44685 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); in fillInUnixFile()
44690 pNew->lockingContext = (void*)zFilename; in fillInUnixFile()
44700 rc = findInodeInfo(pNew, &pNew->pInode); in fillInUnixFile()
44713 ** handle h - as it is guaranteed that no posix locks will be released in fillInUnixFile()
44721 h = -1; in fillInUnixFile()
44732 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); in fillInUnixFile()
44739 pCtx->dbPath = zFilename; in fillInUnixFile()
44740 pCtx->reserved = 0; in fillInUnixFile()
44743 rc = findInodeInfo(pNew, &pNew->pInode); in fillInUnixFile()
44745 sqlite3_free(pNew->lockingContext); in fillInUnixFile()
44747 h = -1; in fillInUnixFile()
44768 pNew->lockingContext = zLockFile; in fillInUnixFile()
44777 rc = findInodeInfo(pNew, &pNew->pInode); in fillInUnixFile()
44778 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ in fillInUnixFile()
44779 char *zSemName = pNew->pInode->aSemName; in fillInUnixFile()
44782 pNew->pId->zCanonicalName); in fillInUnixFile()
44785 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); in fillInUnixFile()
44786 if( pNew->pInode->pSem == SEM_FAILED ){ in fillInUnixFile()
44788 pNew->pInode->aSemName[0] = '\0'; in fillInUnixFile()
44799 h = -1; in fillInUnixFile()
44801 pNew->ctrlFlags |= UNIXFILE_DELETE; in fillInUnixFile()
44807 pId->pMethods = pLockingStyle; in fillInUnixFile()
44860 ** pVfs->mxPathname bytes.
44867 /* It's odd to simulate an io-error here, but really this is just in unixGetTempname()
44868 ** using the io-error infrastructure to test that SQLite handles this in unixGetTempname()
44883 zBuf[nBuf-2] = 0; in unixGetTempname()
44886 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ){ in unixGetTempname()
44898 ** Routine to transform a unixFile into a proxy-locking unixFile.
44899 ** Implementation in the proxy-lock division, but used by unixOpen()
44907 ** file (not a journal or super-journal file) identified by pathname
44913 ** other file descriptor open on the same file is holding a file-lock.
44919 ** such file descriptor is located, -1 is returned.
44937 ** ignored and -1 is returned. The caller will try to open a new file in findReusableFd()
44946 while( pInode && (pInode->fileId.dev!=sStat.st_dev in findReusableFd()
44947 || pInode->fileId.ino!=(u64)sStat.st_ino) ){ in findReusableFd()
44948 pInode = pInode->pNext; in findReusableFd()
44952 assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); in findReusableFd()
44953 sqlite3_mutex_enter(pInode->pLockMutex); in findReusableFd()
44955 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); in findReusableFd()
44958 *pp = pUnused->pNext; in findReusableFd()
44960 sqlite3_mutex_leave(pInode->pLockMutex); in findReusableFd()
45000 ** this function queries the file-system for the permissions on the
45029 ** "<path to db>-journal" in findCreateFileMode()
45030 ** "<path to db>-wal" in findCreateFileMode()
45031 ** "<path to db>-journalNN" in findCreateFileMode()
45032 ** "<path to db>-walNN" in findCreateFileMode()
45038 ** a '-' character. However in 8+3 filename mode, or if a corrupt in findCreateFileMode()
45039 ** rollback journal specifies a super-journal with a goofy name, then in findCreateFileMode()
45040 ** the '-' might be missing or the '-' might be the first character in in findCreateFileMode()
45043 nDb = sqlite3Strlen30(zPath) - 1; in findCreateFileMode()
45045 if( zPath[nDb]=='-' ){ in findCreateFileMode()
45051 nDb--; in findCreateFileMode()
45080 ** ReadWrite() -> (READWRITE | CREATE)
45081 ** ReadOnly() -> (READONLY)
45082 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
45084 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
45098 int fd = -1; /* File descriptor returned by open() */ in unixOpen()
45117 /* If creating a super- or main-file journal, this function will open in unixOpen()
45118 ** a file-descriptor on the directory too. The first time unixSync() in unixOpen()
45145 /* The main DB, main journal, WAL file and super-journal are never in unixOpen()
45152 /* Assert that the upper layer has set one of the "file-type" flags. */ in unixOpen()
45179 fd = pUnused->fd; in unixOpen()
45186 p->pPreallocatedUnused = pUnused; in unixOpen()
45188 /* Database filenames are double-zero terminated if they are not in unixOpen()
45196 rc = unixGetTempname(pVfs->mxPathname, zTmpname); in unixOpen()
45202 /* Generated temporary filenames are always double-zero terminated in unixOpen()
45223 assert( !p->pPreallocatedUnused ); in unixOpen()
45228 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); in unixOpen()
45236 /* Failed to open the file for read/write access. Try read-only. */ in unixOpen()
45245 fd = pReadonly->fd; in unixOpen()
45260 ** the case. The chown() system call will be a no-op if the current in unixOpen()
45279 if( p->pPreallocatedUnused ){ in unixOpen()
45280 p->pPreallocatedUnused->fd = fd; in unixOpen()
45281 p->pPreallocatedUnused->flags = in unixOpen()
45300 p->openFlags = openFlags; in unixOpen()
45305 if( fstatfs(fd, &fsInfo) == -1 ){ in unixOpen()
45311 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; in unixOpen()
45314 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; in unixOpen()
45330 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ in unixOpen()
45335 ** never use proxy, NULL means use proxy for non-local files only. */ in unixOpen()
45348 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op in unixOpen()
45366 sqlite3_free(p->pPreallocatedUnused); in unixOpen()
45384 if( osUnlink(zPath)==(-1) ){ in unixDelete()
45453 int rc; /* Non-zero following any error */
45468 const char *zName, /* Name to append to pPath. Not zero-terminated */ in appendOnePathElement()
45476 if( pPath->nUsed>1 ){ in appendOnePathElement()
45477 assert( pPath->zOut[0]=='/' ); in appendOnePathElement()
45478 while( pPath->zOut[--pPath->nUsed]!='/' ){} in appendOnePathElement()
45483 if( pPath->nUsed + nName + 2 >= pPath->nOut ){ in appendOnePathElement()
45484 pPath->rc = SQLITE_ERROR; in appendOnePathElement()
45487 pPath->zOut[pPath->nUsed++] = '/'; in appendOnePathElement()
45488 memcpy(&pPath->zOut[pPath->nUsed], zName, nName); in appendOnePathElement()
45489 pPath->nUsed += nName; in appendOnePathElement()
45491 if( pPath->rc==SQLITE_OK ){ in appendOnePathElement()
45494 pPath->zOut[pPath->nUsed] = 0; in appendOnePathElement()
45495 zIn = pPath->zOut; in appendOnePathElement()
45498 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); in appendOnePathElement()
45503 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){ in appendOnePathElement()
45504 pPath->rc = SQLITE_CANTOPEN_BKPT; in appendOnePathElement()
45507 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2); in appendOnePathElement()
45508 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){ in appendOnePathElement()
45509 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); in appendOnePathElement()
45514 pPath->nUsed = 0; in appendOnePathElement()
45516 pPath->nUsed -= nName + 1; in appendOnePathElement()
45529 const char *zPath /* Path to append to pPath. Is zero-terminated */ in appendAllPathElements()
45536 appendOnePathElement(pPath, &zPath[j], i-j); in appendAllPathElements()
45544 ** is stored as a nul-terminated string in the buffer pointed to by
45548 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
45566 if( osGetcwd(zPwd, sizeof(zPwd)-2)==0 ){ in unixFullPathname()
45608 ** GCC with -pedantic-errors says that C90 does not allow a void* to be in unixDlSym()
45611 ** use dlsym() with -pedantic-errors? in unixDlSym()
45619 ** This work-around is unlikely to work correctly on any system where in unixDlSym()
45648 ** errors. The reports issued by valgrind are incorrect - we would in unixRandomness()
45650 ** uninitialized space in zBuf - but valgrind errors tend to worry in unixRandomness()
45698 ** -DHAVE_NANOSLEEP=0 (perhaps in conjunction with -DHAVE_USLEEP if in unixSleep()
45718 ** The following variable, if set to a non-zero value, is interpreted as
45782 ** low-level error message when operating-system problems come up
45801 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
45803 ** meta-layer over top of the primitive locking implemented above. For
45806 ** been defined - so that the primitive locking methods are available
45811 ** The default locking schemes in SQLite use byte-range locks on the
45816 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
45824 ** SHARED_RANGE 0x40000002 -> 0x40000200
45828 ** the read cache when byte-range locks are present. Enabling the read
45831 ** close-to-open semantics for ensuring cache coherency
45835 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
45844 ** -----------------
45876 ** -----------------------
45886 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
45887 ** by taking an sqlite-style shared lock on the conch file, reading the
45891 ** is patterned after the database file name as ".<databasename>-conch".
45899 ** The proxy file - a single-byte file used for all advisory file locks
45915 ** ---------------------
45919 ** Database files accessed on non-local file systems are
45964 int conchHeld; /* 1 if the conch is held, -1 if lockless */
45972 ** which must point to valid, writable memory large enough for a maxLen length
45997 if( lPath[len-1]!='/' ){ in proxyGetLockPath()
46026 if( lockPath[i] == '/' && (i - start > 0) ){ in proxyCreateLockPath()
46028 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') in proxyCreateLockPath()
46029 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ in proxyCreateLockPath()
46061 int fd = -1; in proxyCreateUnixFile()
46070 ** 2. if that fails, and this is a lock file (not-conch), try creating in proxyCreateUnixFile()
46072 ** 3. if that fails, try to open the file read-only in proxyCreateUnixFile()
46077 fd = pUnused->fd; in proxyCreateUnixFile()
46118 pNew->openFlags = openFlags; in proxyCreateUnixFile()
46122 pUnused->fd = fd; in proxyCreateUnixFile()
46123 pUnused->flags = openFlags; in proxyCreateUnixFile()
46124 pNew->pPreallocatedUnused = pUnused; in proxyCreateUnixFile()
46143 #define PROXY_HOSTIDLEN 16 /* conch file host id length */
46182 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
46183 #define PROXY_HEADERLEN 1 /* conch file header length */
46194 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyBreakConchLock()
46195 unixFile *conchFile = pCtx->conchFile; in proxyBreakConchLock()
46198 char *cPath = pCtx->conchFilePath; in proxyBreakConchLock()
46202 int fd = -1; in proxyBreakConchLock()
46203 int rc = -1; in proxyBreakConchLock()
46206 /* create a new path by replace the trailing '-conch' with '-break' */ in proxyBreakConchLock()
46209 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ in proxyBreakConchLock()
46214 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); in proxyBreakConchLock()
46235 robust_close(pFile, conchFile->h, __LINE__); in proxyBreakConchLock()
46236 conchFile->h = fd; in proxyBreakConchLock()
46237 conchFile->openFlags = O_RDWR | O_CREAT; in proxyBreakConchLock()
46254 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyConchLock()
46255 unixFile *conchFile = pCtx->conchFile; in proxyConchLock()
46262 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); in proxyConchLock()
46272 if( osFstat(conchFile->h, &buf) ){ in proxyConchLock()
46291 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); in proxyConchLock()
46313 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); in proxyConchLock()
46316 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); in proxyConchLock()
46326 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
46332 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyTakeConch()
46334 if( pCtx->conchHeld!=0 ){ in proxyTakeConch()
46337 unixFile *conchFile = pCtx->conchFile; in proxyTakeConch()
46350 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, in proxyTakeConch()
46351 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), in proxyTakeConch()
46367 storeLastErrno(pFile, conchFile->lastErrno); in proxyTakeConch()
46379 ** retry with a new auto-generated path in proxyTakeConch()
46387 if( !pCtx->lockProxyPath ){ in proxyTakeConch()
46388 /* for auto-named local lock file, just check the host ID and we'll in proxyTakeConch()
46392 size_t pathLen = (readLen - PROXY_PATHINDEX); in proxyTakeConch()
46395 pathLen=MAXPATHLEN-1; in proxyTakeConch()
46405 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], in proxyTakeConch()
46406 readLen-PROXY_PATHINDEX) in proxyTakeConch()
46414 if( (conchFile->openFlags&O_RDWR) == 0 ){ in proxyTakeConch()
46420 if( !pCtx->lockProxyPath ){ in proxyTakeConch()
46421 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); in proxyTakeConch()
46430 futimes(conchFile->h, NULL); in proxyTakeConch()
46432 if( conchFile->pInode && conchFile->pInode->nShared>1 ){ in proxyTakeConch()
46448 if( pCtx->lockProxyPath!=NULL ){ in proxyTakeConch()
46449 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, in proxyTakeConch()
46455 robust_ftruncate(conchFile->h, writeSize); in proxyTakeConch()
46457 full_fsync(conchFile->h,0,0); in proxyTakeConch()
46463 int err = osFstat(pFile->h, &buf); in proxyTakeConch()
46469 osFchmod(conchFile->h, cmode); in proxyTakeConch()
46472 rc = osFchmod(conchFile->h, cmode); in proxyTakeConch()
46473 }while( rc==(-1) && errno==EINTR ); in proxyTakeConch()
46489 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); in proxyTakeConch()
46492 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); in proxyTakeConch()
46493 if( rc==SQLITE_OK && pFile->openFlags ){ in proxyTakeConch()
46495 if( pFile->h>=0 ){ in proxyTakeConch()
46496 robust_close(pFile, pFile->h, __LINE__); in proxyTakeConch()
46498 pFile->h = -1; in proxyTakeConch()
46499 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); in proxyTakeConch()
46502 pFile->h = fd; in proxyTakeConch()
46508 if( rc==SQLITE_OK && !pCtx->lockProxy ){ in proxyTakeConch()
46509 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; in proxyTakeConch()
46510 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); in proxyTakeConch()
46513 ** so try again via auto-naming in proxyTakeConch()
46525 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); in proxyTakeConch()
46526 if( !pCtx->lockProxyPath ){ in proxyTakeConch()
46532 pCtx->conchHeld = 1; in proxyTakeConch()
46534 if( pCtx->lockProxy->pMethod == &afpIoMethods ){ in proxyTakeConch()
46536 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; in proxyTakeConch()
46537 afpCtx->dbPath = pCtx->lockProxyPath; in proxyTakeConch()
46540 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); in proxyTakeConch()
46542 OSTRACE(("TAKECONCH %d %s\n", conchFile->h, in proxyTakeConch()
46545 } while (1); /* in case we need to retry the :auto: lock file - in proxyTakeConch()
46558 pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyReleaseConch()
46559 conchFile = pCtx->conchFile; in proxyReleaseConch()
46560 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, in proxyReleaseConch()
46561 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), in proxyReleaseConch()
46563 if( pCtx->conchHeld>0 ){ in proxyReleaseConch()
46564 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); in proxyReleaseConch()
46566 pCtx->conchHeld = 0; in proxyReleaseConch()
46567 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, in proxyReleaseConch()
46585 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ in proxyCreateConchPathname()
46597 for( i=(len-1); i>=0; i-- ){ in proxyCreateConchPathname()
46609 /* append the "-conch" suffix to the file */ in proxyCreateConchPathname()
46610 memcpy(&conchPath[i+1], "-conch", 7); in proxyCreateConchPathname()
46617 /* Takes a fully configured proxy locking-style unix file and switches
46621 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; in switchLockProxyPath()
46622 char *oldPath = pCtx->lockProxyPath; in switchLockProxyPath()
46625 if( pFile->eFileLock!=NO_LOCK ){ in switchLockProxyPath()
46634 unixFile *lockProxy = pCtx->lockProxy; in switchLockProxyPath()
46635 pCtx->lockProxy=NULL; in switchLockProxyPath()
46636 pCtx->conchHeld = 0; in switchLockProxyPath()
46638 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); in switchLockProxyPath()
46643 pCtx->lockProxyPath = sqlite3DbStrDup(0, path); in switchLockProxyPath()
46658 if( pFile->pMethod == &afpIoMethods ){ in proxyGetDbPathForUnixFile()
46661 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); in proxyGetDbPathForUnixFile()
46662 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, in proxyGetDbPathForUnixFile()
46666 if( pFile->pMethod == &dotlockIoMethods ){ in proxyGetDbPathForUnixFile()
46669 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); in proxyGetDbPathForUnixFile()
46670 memcpy(dbPath, (char *)pFile->lockingContext, len + 1); in proxyGetDbPathForUnixFile()
46673 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); in proxyGetDbPathForUnixFile()
46674 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); in proxyGetDbPathForUnixFile()
46684 ** ->lockingContext
46685 ** ->pMethod
46693 if( pFile->eFileLock!=NO_LOCK ){ in proxyTransformUnixFile()
46703 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, in proxyTransformUnixFile()
46712 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); in proxyTransformUnixFile()
46714 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); in proxyTransformUnixFile()
46715 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ in proxyTransformUnixFile()
46717 ** (c) the file system is read-only, then enable no-locking access. in proxyTransformUnixFile()
46725 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { in proxyTransformUnixFile()
46727 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ in proxyTransformUnixFile()
46732 pCtx->conchHeld = -1; /* read only FS/ lockless */ in proxyTransformUnixFile()
46738 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); in proxyTransformUnixFile()
46742 pCtx->dbPath = sqlite3DbStrDup(0, dbPath); in proxyTransformUnixFile()
46743 if( pCtx->dbPath==NULL ){ in proxyTransformUnixFile()
46751 pCtx->oldLockingContext = pFile->lockingContext; in proxyTransformUnixFile()
46752 pFile->lockingContext = pCtx; in proxyTransformUnixFile()
46753 pCtx->pOldMethod = pFile->pMethod; in proxyTransformUnixFile()
46754 pFile->pMethod = &proxyIoMethods; in proxyTransformUnixFile()
46756 if( pCtx->conchFile ){ in proxyTransformUnixFile()
46757 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); in proxyTransformUnixFile()
46758 sqlite3_free(pCtx->conchFile); in proxyTransformUnixFile()
46760 sqlite3DbFree(0, pCtx->lockProxyPath); in proxyTransformUnixFile()
46761 sqlite3_free(pCtx->conchFilePath); in proxyTransformUnixFile()
46764 OSTRACE(("TRANSPROXY %d %s\n", pFile->h, in proxyTransformUnixFile()
46778 if( pFile->pMethod == &proxyIoMethods ){ in proxyFileControl()
46779 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; in proxyFileControl()
46781 if( pCtx->lockProxyPath ){ in proxyFileControl()
46782 *(const char **)pArg = pCtx->lockProxyPath; in proxyFileControl()
46794 int isProxyStyle = (pFile->pMethod == &proxyIoMethods); in proxyFileControl()
46797 /* turn off proxy locking - not supported. If support is added for in proxyFileControl()
46803 /* turn off proxy locking - already off - NOOP */ in proxyFileControl()
46810 (proxyLockingContext*)pFile->lockingContext; in proxyFileControl()
46812 || (pCtx->lockProxyPath && in proxyFileControl()
46813 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) in proxyFileControl()
46836 ** above this point are all utilities. The lock-related methods of the
46837 ** proxy-locking sqlite3_io_method object follow.
46844 ** to a non-zero value otherwise *pResOut is set to zero. The return value
46851 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyCheckReservedLock()
46852 if( pCtx->conchHeld>0 ){ in proxyCheckReservedLock()
46853 unixFile *proxy = pCtx->lockProxy; in proxyCheckReservedLock()
46854 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); in proxyCheckReservedLock()
46863 ** Lock the file with the lock specified by parameter eFileLock - one
46877 ** UNLOCKED -> SHARED
46878 ** SHARED -> RESERVED
46879 ** SHARED -> (PENDING) -> EXCLUSIVE
46880 ** RESERVED -> (PENDING) -> EXCLUSIVE
46881 ** PENDING -> EXCLUSIVE
46890 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyLock()
46891 if( pCtx->conchHeld>0 ){ in proxyLock()
46892 unixFile *proxy = pCtx->lockProxy; in proxyLock()
46893 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); in proxyLock()
46894 pFile->eFileLock = proxy->eFileLock; in proxyLock()
46908 ** the requested locking level, this routine is a no-op.
46914 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyUnlock()
46915 if( pCtx->conchHeld>0 ){ in proxyUnlock()
46916 unixFile *proxy = pCtx->lockProxy; in proxyUnlock()
46917 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); in proxyUnlock()
46918 pFile->eFileLock = proxy->eFileLock; in proxyUnlock()
46932 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; in proxyClose()
46933 unixFile *lockProxy = pCtx->lockProxy; in proxyClose()
46934 unixFile *conchFile = pCtx->conchFile; in proxyClose()
46938 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); in proxyClose()
46940 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); in proxyClose()
46943 pCtx->lockProxy = 0; in proxyClose()
46946 if( pCtx->conchHeld ){ in proxyClose()
46950 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); in proxyClose()
46954 sqlite3DbFree(0, pCtx->lockProxyPath); in proxyClose()
46955 sqlite3_free(pCtx->conchFilePath); in proxyClose()
46956 sqlite3DbFree(0, pCtx->dbPath); in proxyClose()
46958 pFile->lockingContext = pCtx->oldLockingContext; in proxyClose()
46959 pFile->pMethod = pCtx->pOldMethod; in proxyClose()
46961 return pFile->pMethod->xClose(id); in proxyClose()
46981 ** This routine registers all VFS implementations for unix-like operating
46998 ** when compiling with -pedantic-errors on GCC.) in sqlite3_os_init()
47001 ** finder-function. The finder-function returns a pointer to the in sqlite3_os_init()
47004 ** macro for addition information on finder-functions. in sqlite3_os_init()
47052 UNIXVFS("unix-none", nolockIoFinder ), in sqlite3_os_init()
47053 UNIXVFS("unix-dotfile", dotlockIoFinder ), in sqlite3_os_init()
47054 UNIXVFS("unix-excl", posixIoFinder ), in sqlite3_os_init()
47056 UNIXVFS("unix-namedsem", semIoFinder ), in sqlite3_os_init()
47059 UNIXVFS("unix-posix", posixIoFinder ), in sqlite3_os_init()
47062 UNIXVFS("unix-flock", flockIoFinder ), in sqlite3_os_init()
47065 UNIXVFS("unix-afp", afpIoFinder ), in sqlite3_os_init()
47066 UNIXVFS("unix-nfs", nfsIoFinder ), in sqlite3_os_init()
47067 UNIXVFS("unix-proxy", proxyIoFinder ), in sqlite3_os_init()
47072 /* Double-check that the aSyscall[] array has been constructed in sqlite3_os_init()
47098 ** READ-0 UNIX_SHM_BASE+3 123 in sqlite3_os_init()
47099 ** READ-1 UNIX_SHM_BASE+4 124 in sqlite3_os_init()
47100 ** READ-2 UNIX_SHM_BASE+5 125 in sqlite3_os_init()
47101 ** READ-3 UNIX_SHM_BASE+6 126 in sqlite3_os_init()
47102 ** READ-4 UNIX_SHM_BASE+7 127 in sqlite3_os_init()
47105 assert( UNIX_SHM_DMS==128 ); /* Byte offset of the deadman-switch */ in sqlite3_os_init()
47119 ** This routine is a no-op for unix.
47173 ** based on the sub-platform)?
47181 ** based on the sub-platform)?
47245 ** Maximum pathname length (in chars) for Win32. This should normally be
47260 ** Maximum pathname length (in chars) for WinNT. This should normally be
47268 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
47269 ** characters, so we allocate 4 bytes per character assuming worst-case of
47270 ** 4-bytes-per-character for UTF8.
47277 ** Maximum pathname length (in bytes) for WinNT. This should normally be
47286 ** Maximum error message length (in chars) for WinRT.
47293 ** Returns non-zero if the character should be treated as a directory
47357 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
47370 typedef struct winShm winShm; /* A connection to shared-memory */
47371 typedef struct winShmNode winShmNode; /* A region of shared-memory */
47426 # define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout
47439 BOOL bNoLock; /* Non-zero if locking is disabled. */
47453 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
47463 * If this is non-zero, an isolated heap will be created by the native Win32
47469 * WARNING: It is important to note that when this setting is non-zero and the
47481 * This is the maximum possible initial size of the Win32-specific heap, in
47489 * This is the extra space for the initial size of the Win32-specific heap,
47502 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
47509 * Win32-specific heap. It cannot be negative.
47515 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
47521 * initial size of the Win32-specific heap to exceed the maximum amount
47530 * The initial size of the Win32-specific heap. This value may be zero.
47539 * The maximum size of the Win32-specific heap. This value may be zero.
47555 ** The winMemData structure stores information required by the Win32-specific
47639 ** Many system calls are accessed through pointer-to-functions so that
48264 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
48467 int i = -1; in winNextSystemCall()
48471 for(i=0; i<ArraySize(aSyscall)-1; i++){ in winNextSystemCall()
48486 ** "pnLargest" argument, if non-zero, will be used to return the size of the
48577 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ in sqlite3_win32_write_debug()
48578 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ in sqlite3_win32_write_debug()
48579 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE ); in sqlite3_win32_write_debug()
48646 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
48674 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT in sqlite3_win32_is_nt()
48701 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are in sqlite3_win32_is_nt()
48796 if( n==(SIZE_T)-1 ){ in winMemSize()
48818 assert( pWinMemData->magic1==WINMEM_MAGIC1 ); in winMemInit()
48819 assert( pWinMemData->magic2==WINMEM_MAGIC2 ); in winMemInit()
48822 if( !pWinMemData->hHeap ){ in winMemInit()
48830 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS, in winMemInit()
48832 if( !pWinMemData->hHeap ){ in winMemInit()
48839 pWinMemData->bOwned = TRUE; in winMemInit()
48840 assert( pWinMemData->bOwned ); in winMemInit()
48843 pWinMemData->hHeap = osGetProcessHeap(); in winMemInit()
48844 if( !pWinMemData->hHeap ){ in winMemInit()
48849 pWinMemData->bOwned = FALSE; in winMemInit()
48850 assert( !pWinMemData->bOwned ); in winMemInit()
48852 assert( pWinMemData->hHeap!=0 ); in winMemInit()
48853 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); in winMemInit()
48855 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); in winMemInit()
48867 assert( pWinMemData->magic1==WINMEM_MAGIC1 ); in winMemShutdown()
48868 assert( pWinMemData->magic2==WINMEM_MAGIC2 ); in winMemShutdown()
48870 if( pWinMemData->hHeap ){ in winMemShutdown()
48871 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); in winMemShutdown()
48873 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); in winMemShutdown()
48875 if( pWinMemData->bOwned ){ in winMemShutdown()
48876 if( !osHeapDestroy(pWinMemData->hHeap) ){ in winMemShutdown()
48878 osGetLastError(), (void*)pWinMemData->hHeap); in winMemShutdown()
48880 pWinMemData->bOwned = FALSE; in winMemShutdown()
48882 pWinMemData->hHeap = NULL; in winMemShutdown()
48887 ** Populate the low-level memory allocation function pointers in
48915 ** Convert a UTF-8 string to Microsoft Unicode.
48923 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0); in winUtf8ToUnicode()
48931 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText, in winUtf8ToUnicode()
48942 ** Convert a Microsoft Unicode string to UTF-8.
48950 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0); in winUnicodeToUtf8()
48958 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte, in winUnicodeToUtf8()
48978 nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, in winMbcsToUnicode()
48987 nWideChar = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText, in winMbcsToUnicode()
48998 ** Convert a Microsoft Unicode string to a multi-byte character string,
49008 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0); in winUnicodeToMbcs()
49016 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText, in winUnicodeToMbcs()
49027 ** Convert a multi-byte character string to UTF-8.
49046 ** Convert a UTF-8 string to a multi-byte character string.
49163 ** it accepts a UTF-8 string.
49207 ** it accepts a UTF-16 string.
49241 ** is zero if the error message fits in the buffer, or non-zero
49321 ** This function - winLogErrorAtLine() - is only ever called via the macro
49326 ** error code and, if possible, the human-readable equivalent from
49332 ** failed and the associated file-system path, if any.
49352 "os_win.c:%d: (%lu) %s(%s) - %s", in winLogErrorAtLine()
49361 ** will be retried following a locking error - probably caused by
49378 ** non-zero if the error code is transient in nature and the operation
49466 y.tm_year = pTm.wYear - 1900; in localtime()
49467 y.tm_mon = pTm.wMonth - 1; in localtime()
49481 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
49515 memset(&pFile->local, 0, sizeof(pFile->local)); in winceCreateLock()
49525 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName); in winceCreateLock()
49526 if (!pFile->hMutex){ in winceCreateLock()
49527 pFile->lastErrno = osGetLastError(); in winceCreateLock()
49529 return winLogError(SQLITE_IOERR, pFile->lastErrno, in winceCreateLock()
49534 winceMutexAcquire(pFile->hMutex); in winceCreateLock()
49537 ** case-sensitive, take advantage of that by uppercasing the mutex name in winceCreateLock()
49541 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL, in winceCreateLock()
49546 ** must be zero-initialized */ in winceCreateLock()
49555 if( pFile->hShared ){ in winceCreateLock()
49556 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, in winceCreateLock()
49559 if( !pFile->shared ){ in winceCreateLock()
49560 pFile->lastErrno = osGetLastError(); in winceCreateLock()
49561 winLogError(SQLITE_IOERR, pFile->lastErrno, in winceCreateLock()
49564 osCloseHandle(pFile->hShared); in winceCreateLock()
49565 pFile->hShared = NULL; in winceCreateLock()
49570 if( pFile->hShared==NULL ){ in winceCreateLock()
49572 pFile->lastErrno = lastErrno; in winceCreateLock()
49573 winLogError(SQLITE_IOERR, pFile->lastErrno, in winceCreateLock()
49577 winceMutexRelease(pFile->hMutex); in winceCreateLock()
49578 osCloseHandle(pFile->hMutex); in winceCreateLock()
49579 pFile->hMutex = NULL; in winceCreateLock()
49585 memset(pFile->shared, 0, sizeof(winceLock)); in winceCreateLock()
49588 winceMutexRelease(pFile->hMutex); in winceCreateLock()
49596 if (pFile->hMutex){ in winceDestroyLock()
49598 winceMutexAcquire(pFile->hMutex); in winceDestroyLock()
49602 if (pFile->local.nReaders){ in winceDestroyLock()
49603 pFile->shared->nReaders --; in winceDestroyLock()
49605 if (pFile->local.bReserved){ in winceDestroyLock()
49606 pFile->shared->bReserved = FALSE; in winceDestroyLock()
49608 if (pFile->local.bPending){ in winceDestroyLock()
49609 pFile->shared->bPending = FALSE; in winceDestroyLock()
49611 if (pFile->local.bExclusive){ in winceDestroyLock()
49612 pFile->shared->bExclusive = FALSE; in winceDestroyLock()
49615 /* De-reference and close our copy of the shared memory handle */ in winceDestroyLock()
49616 osUnmapViewOfFile(pFile->shared); in winceDestroyLock()
49617 osCloseHandle(pFile->hShared); in winceDestroyLock()
49620 winceMutexRelease(pFile->hMutex); in winceDestroyLock()
49621 osCloseHandle(pFile->hMutex); in winceDestroyLock()
49622 pFile->hMutex = NULL; in winceDestroyLock()
49642 if (!pFile->hMutex) return TRUE; in winceLockFile()
49643 winceMutexAcquire(pFile->hMutex); in winceLockFile()
49648 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ in winceLockFile()
49649 pFile->shared->bExclusive = TRUE; in winceLockFile()
49650 pFile->local.bExclusive = TRUE; in winceLockFile()
49655 /* Want a read-only lock? */ in winceLockFile()
49658 if (pFile->shared->bExclusive == 0){ in winceLockFile()
49659 pFile->local.nReaders ++; in winceLockFile()
49660 if (pFile->local.nReaders == 1){ in winceLockFile()
49661 pFile->shared->nReaders ++; in winceLockFile()
49671 if (pFile->shared->bPending == 0) { in winceLockFile()
49672 pFile->shared->bPending = TRUE; in winceLockFile()
49673 pFile->local.bPending = TRUE; in winceLockFile()
49681 if (pFile->shared->bReserved == 0) { in winceLockFile()
49682 pFile->shared->bReserved = TRUE; in winceLockFile()
49683 pFile->local.bReserved = TRUE; in winceLockFile()
49688 winceMutexRelease(pFile->hMutex); in winceLockFile()
49708 if (!pFile->hMutex) return TRUE; in winceUnlockFile()
49709 winceMutexAcquire(pFile->hMutex); in winceUnlockFile()
49714 if (pFile->local.bExclusive){ in winceUnlockFile()
49716 pFile->local.bExclusive = FALSE; in winceUnlockFile()
49717 pFile->shared->bExclusive = FALSE; in winceUnlockFile()
49722 else if (pFile->local.nReaders){ in winceUnlockFile()
49725 pFile->local.nReaders --; in winceUnlockFile()
49726 if (pFile->local.nReaders == 0) in winceUnlockFile()
49728 pFile->shared->nReaders --; in winceUnlockFile()
49737 if (pFile->local.bPending){ in winceUnlockFile()
49738 pFile->local.bPending = FALSE; in winceUnlockFile()
49739 pFile->shared->bPending = FALSE; in winceUnlockFile()
49746 if (pFile->local.bReserved) { in winceUnlockFile()
49747 pFile->local.bReserved = FALSE; in winceUnlockFile()
49748 pFile->shared->bReserved = FALSE; in winceUnlockFile()
49753 winceMutexRelease(pFile->hMutex); in winceUnlockFile()
49836 /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was in winHandleLockTimeout()
49928 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
49951 ** containing the lower 32-bits of the new file-offset. Or, if it fails, in winHandleSeek()
49982 ** Otherwise, set pFile->lastErrno and return non-zero.
49987 rc = winHandleSeek(pFile->h, iOffset); in winSeekFile()
49989 pFile->lastErrno = osGetLastError(); in winSeekFile()
49990 winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath); in winSeekFile()
50019 assert( pFile->pShm==0 ); in winClose()
50021 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE ); in winClose()
50023 osGetCurrentProcessId(), pFile, pFile->h)); in winClose()
50030 rc = osCloseHandle(pFile->h); in winClose()
50036 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData; in winClose()
50037 if( pAppData==NULL || !pAppData->bNoLock ){ in winClose()
50041 if( pFile->zDeleteOnClose ){ in winClose()
50044 osDeleteFileW(pFile->zDeleteOnClose)==0 in winClose()
50045 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff in winClose()
50050 sqlite3_free(pFile->zDeleteOnClose); in winClose()
50054 pFile->h = NULL; in winClose()
50056 OpenCounter(-1); in winClose()
50058 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed")); in winClose()
50061 "winClose", pFile->zPath); in winClose()
50088 pFile->h, pBuf, amt, offset, pFile->locktype));
50093 if( offset<pFile->mmapSize ){
50094 if( offset+amt <= pFile->mmapSize ){
50095 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
50096 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50097 osGetCurrentProcessId(), pFile, pFile->h));
50100 int nCopy = (int)(pFile->mmapSize - offset);
50101 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
50103 amt -= nCopy;
50112 osGetCurrentProcessId(), pFile, pFile->h));
50115 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
50120 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
50125 pFile->lastErrno = lastErrno;
50127 osGetCurrentProcessId(), pFile, pFile->h));
50128 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
50129 "winRead", pFile->zPath);
50133 /* Unread parts of the buffer must be zero-filled */
50134 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
50136 osGetCurrentProcessId(), pFile, pFile->h));
50141 osGetCurrentProcessId(), pFile, pFile->h));
50166 pFile->h, pBuf, amt, offset, pFile->locktype));
50171 if( offset<pFile->mmapSize ){
50172 if( offset+amt <= pFile->mmapSize ){
50173 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
50174 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50175 osGetCurrentProcessId(), pFile, pFile->h));
50178 int nCopy = (int)(pFile->mmapSize - offset);
50179 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
50181 amt -= nCopy;
50209 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
50211 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
50227 nRem -= nWrite;
50230 pFile->lastErrno = lastErrno;
50236 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
50237 || ( pFile->lastErrno==ERROR_DISK_FULL )){
50239 osGetCurrentProcessId(), pFile, pFile->h));
50240 return winLogError(SQLITE_FULL, pFile->lastErrno,
50241 "winWrite1", pFile->zPath);
50244 osGetCurrentProcessId(), pFile, pFile->h));
50245 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
50246 "winWrite2", pFile->zPath);
50251 osGetCurrentProcessId(), pFile, pFile->h));
50318 if( pFile->nFetchOut>0 ){
50319 /* File truncation is a no-op if there are outstanding memory mapped
50325 ** No real harm comes of this - the database file is not corrupted,
50329 ** The only feasible work-around is to defer the truncation until after
50330 ** all references to memory-mapped content are closed. That is doable,
50333 ** now to simply make transactions a no-op if there are pending reads. We
50343 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
50345 /* If the user has configured a chunk-size for this file, truncate the
50350 if( pFile->szChunk>0 ){
50351 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
50355 if( pFile->pMapRegion ){
50356 oldMmapSize = pFile->mmapSize;
50363 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
50365 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
50366 "winTruncate1", pFile->zPath);
50367 }else if( 0==osSetEndOfFile(pFile->h) &&
50369 pFile->lastErrno = lastErrno;
50370 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
50371 "winTruncate2", pFile->zPath);
50377 winMapfile(pFile, -1);
50385 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
50431 osGetCurrentProcessId(), pFile, pFile->h, flags,
50432 pFile->locktype));
50444 ** no-op
50447 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50448 osGetCurrentProcessId(), pFile, pFile->h));
50452 if( pFile->pMapRegion ){
50453 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
50454 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
50456 pFile, pFile->pMapRegion));
50458 pFile->lastErrno = osGetLastError();
50459 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
50461 pFile, pFile->pMapRegion));
50462 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
50463 "winSync1", pFile->zPath);
50467 rc = osFlushFileBuffers(pFile->h);
50471 osGetCurrentProcessId(), pFile, pFile->h));
50474 pFile->lastErrno = osGetLastError();
50476 osGetCurrentProcessId(), pFile, pFile->h));
50477 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
50478 "winSync2", pFile->zPath);
50493 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
50498 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
50502 pFile->lastErrno = osGetLastError();
50503 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
50504 "winFileSize", pFile->zPath);
50513 lowerBits = osGetFileSize(pFile->h, &upperBits);
50517 pFile->lastErrno = lastErrno;
50518 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
50519 "winFileSize", pFile->zPath);
50524 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
50567 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50574 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
50576 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0,
50584 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
50585 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask,
50586 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
50590 pFile->lastErrno = osGetLastError();
50593 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
50603 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50605 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50609 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
50613 pFile->lastErrno = lastErrno;
50614 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
50615 "winUnlockReadLock", pFile->zPath);
50617 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
50622 ** Lock the file with the lock specified by parameter locktype - one
50636 ** UNLOCKED -> SHARED
50637 ** SHARED -> RESERVED
50638 ** SHARED -> (PENDING) -> EXCLUSIVE
50639 ** RESERVED -> (PENDING) -> EXCLUSIVE
50640 ** PENDING -> EXCLUSIVE
50650 int newLocktype; /* Set pFile->locktype to this value before exiting */
50657 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50663 if( pFile->locktype>=locktype ){
50664 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
50668 /* Do not allow any kind of write-lock on a read-only database
50670 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
50676 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
50678 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
50684 newLocktype = pFile->locktype;
50686 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
50699 ** around problems caused by indexing and/or anti-virus software on
50704 res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0);
50708 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50709 pFile->h, cnt, res
50713 pFile->lastErrno = lastErrno;
50715 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50716 pFile->h, cnt, sqlite3ErrName(rc)
50721 cnt--;
50730 assert( pFile->locktype==NO_LOCK );
50732 res = winGetReadLock(pFile, pFile->bBlockOnConnect);
50746 assert( pFile->locktype==SHARED_LOCK );
50747 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
50765 assert( pFile->locktype>=SHARED_LOCK );
50767 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
50781 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50790 pFile->lastErrno = lastErrno;
50792 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
50793 pFile->h, locktype, newLocktype));
50795 pFile->locktype = (u8)newLocktype;
50797 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50804 ** non-zero, otherwise zero.
50811 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
50814 if( pFile->locktype>=RESERVED_LOCK ){
50816 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
50818 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
50820 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50823 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
50826 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
50827 pFile->h, pResOut, *pResOut));
50836 ** the requested locking level, this routine is a no-op.
50849 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50850 type = pFile->locktype;
50852 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50857 "winUnlock", pFile->zPath);
50861 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
50867 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
50869 pFile->locktype = (u8)locktype;
50871 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
50876 ****************************** No-op Locking **********************************
50882 ** This locking mode is appropriate for use on read-only databases
50883 ** (ex: databases that are burned into CD-ROM, for example.) It can
50910 /******************* End of the no-op lock implementation *********************
50915 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
50917 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
50921 *pArg = (pFile->ctrlFlags & mask)!=0;
50923 pFile->ctrlFlags &= ~mask;
50925 pFile->ctrlFlags |= mask;
50940 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
50943 *(int*)pArg = pFile->locktype;
50944 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50948 *(int*)pArg = (int)pFile->lastErrno;
50949 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50953 pFile->szChunk = *(int *)pArg;
50954 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50958 if( pFile->szChunk>0 ){
50969 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50972 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50977 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50982 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
50986 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
50987 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
51002 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
51007 *phFile = pFile->h;
51008 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
51014 HANDLE hOldFile = pFile->h;
51015 pFile->h = *phFile;
51018 hOldFile, pFile->h));
51023 (void)osCloseHandle(pFile->h);
51024 pFile->h = NULL;
51029 int rc = winGetTempname(pFile->pVfs, &zTFile);
51033 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
51046 ** least a 64-bit type. */
51051 *(i64*)pArg = pFile->mmapSizeMax;
51052 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
51053 pFile->mmapSizeMax = newLimit;
51054 if( pFile->mmapSize>0 ){
51056 rc = winMapfile(pFile, -1);
51059 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
51066 int iOld = pFile->iBusyTimeout;
51069 pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew;
51071 pFile->iBusyTimeout = (DWORD)(!!iNew);
51080 pFile->bBlockOnConnect = iNew;
51086 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
51096 ** if two files are created in the same file-system directory (i.e.
51111 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
51153 ** log-summary, each thread has its own winFile object, but they all
51155 ** log-summary is opened only once per process.
51163 ** The following fields are read-only after the object is created:
51171 ** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51172 ** the *-shm file if the DMS-locking protocol demands it, and (c) map
51173 ** regions of the *-shm file into memory using MapViewOfFile() or
51183 int isReadonly; /* True if read-only */
51184 int szRegion; /* Size of shared-memory regions */
51216 HANDLE hShm; /* File-handle on *-shm file. For locking. */
51217 int bReadonly; /* True if hShm is opened read-only */
51236 ** This is not a VFS shared-memory method; it is a utility function called
51237 ** by VFS shared-memory methods.
51243 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
51247 if( p->nRef==0 ){
51249 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
51250 for(i=0; i<p->nRegion; i++){
51251 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
51252 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
51255 bRc = osCloseHandle(p->aRegion[i].hMap);
51256 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
51260 winHandleClose(p->hSharedShm);
51264 winDelete(pVfs, p->zFilename, 0);
51268 *pp = p->pNext;
51269 sqlite3_free(p->aRegion);
51272 pp = &p->pNext;
51279 ** pShmNode. Take the lock. Truncate the *-shm file if required.
51283 HANDLE h = pShmNode->hSharedShm;
51286 assert( sqlite3_mutex_held(pShmNode->mutex) );
51292 if( pShmNode->isReadonly ){
51308 pShmNode->isUnlocked = 0;
51317 ** Convert a UTF-8 filename into whatever form the underlying
51323 ** - If the filename starts with "<drive>:/" or "<drive>:\",
51324 ** it is converted to UTF-16 as-is.
51325 ** - If the filename contains '/', it is assumed to be a
51327 ** absolute path in UTF-16.
51328 ** - Otherwise it must be a filename only, the win32 filename
51329 ** is returned in UTF-16.
51331 ** UTF-8 -> UTF-16 conversion will be done. This can only
51374 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
51382 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1,
51414 ** This function is used to open a handle on a *-shm file.
51451 ** TODO: retry-on-ioerr.
51478 /* Due to pre-processor directives earlier in this file,
51510 ** Open the shared-memory area associated with database file pDbFd.
51519 assert( pDbFd->pShm==0 ); /* Not previously opened */
51525 nName = sqlite3Strlen30(pDbFd->zPath);
51531 pNew->zFilename = (char*)&pNew[1];
51532 pNew->hSharedShm = INVALID_HANDLE_VALUE;
51533 pNew->isUnlocked = 1;
51534 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
51535 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
51537 /* Open a file-handle on the *-shm file for this connection. This file-handle
51538 ** is only used for locking. The mapping of the *-shm file is created using
51540 p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0);
51541 rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm);
51546 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
51549 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
51556 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
51557 if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT;
51560 /* Open a file-handle to use for mappings, and for the DMS lock. */
51563 pShmNode->isReadonly = p->bReadonly;
51564 rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h);
51565 pShmNode->hSharedShm = h;
51571 pShmNode->pNext = winShmNodeList;
51575 sqlite3_mutex_free(pShmNode->mutex);
51576 if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){
51577 osCloseHandle(pShmNode->hSharedShm);
51585 p->pShmNode = pShmNode;
51586 pShmNode->nRef++;
51588 p->id = pShmNode->nextShmId++;
51590 pDbFd->pShm = p;
51592 winHandleClose(p->hShm);
51596 assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 );
51603 ** Close a connection to shared-memory. Delete the underlying
51610 winFile *pDbFd; /* Database holding shared-memory */
51612 winShmNode *pShmNode; /* The underlying shared-memory file */
51615 p = pDbFd->pShm;
51617 if( p->hShm!=INVALID_HANDLE_VALUE ){
51618 osCloseHandle(p->hShm);
51621 pShmNode = p->pShmNode;
51624 /* If pShmNode->nRef has reached 0, then close the underlying
51625 ** shared-memory file, too. */
51626 assert( pShmNode->nRef>0 );
51627 pShmNode->nRef--;
51628 if( pShmNode->nRef==0 ){
51629 winShmPurge(pDbFd->pVfs, deleteFlag);
51635 pDbFd->pShm = 0;
51640 ** Change the lock state for a shared-memory segment.
51649 winShm *p = pDbFd->pShm; /* The shared memory being locked */
51652 u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */
51655 pShmNode = p->pShmNode;
51680 u16 lockMask = (p->exclMask|p->sharedMask);
51681 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
51699 || 0==(p->exclMask & mask)
51701 if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
51702 || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
51707 /* Case (a) - unlock. */
51709 assert( (p->exclMask & p->sharedMask)==0 );
51710 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
51711 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
51713 rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n);
51717 p->exclMask = (p->exclMask & ~mask);
51718 p->sharedMask = (p->sharedMask & ~mask);
51723 rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs);
51726 p->exclMask = (p->exclMask | mask);
51728 p->sharedMask = (p->sharedMask | mask);
51735 "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x,"
51738 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51754 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
51761 ** shared-memory associated with the database file fd. Shared-memory regions
51762 ** are numbered starting from zero. Each shared-memory region is szRegion
51767 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
51770 ** isWrite is non-zero and the requested shared-memory region has not yet
51773 ** If the shared-memory region has already been allocated or is allocated by
51786 winShm *pShm = pDbFd->pShm;
51795 pShm = pDbFd->pShm;
51798 pShmNode = pShm->pShmNode;
51800 sqlite3_mutex_enter(pShmNode->mutex);
51801 if( pShmNode->isUnlocked ){
51803 assert( pShmNode->nRegion==0 );
51808 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
51809 if( pShmNode->nRegion<=iRegion ){
51810 HANDLE hShared = pShmNode->hSharedShm;
51813 sqlite3_int64 sz; /* Current size of wal-index file */
51815 pShmNode->szRegion = szRegion;
51818 ** Check to see if it has been allocated (i.e. if the wal-index file is
51823 rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath);
51831 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51836 rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath);
51843 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51849 pShmNode->aRegion = apNew;
51851 if( pShmNode->isReadonly ){
51856 while( pShmNode->nRegion<=iRegion ){
51857 HANDLE hMap = NULL; /* file-mapping handle */
51868 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51869 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51872 int iOffset = pShmNode->nRegion*szRegion;
51876 iOffset - iOffsetShift, szRegion + iOffsetShift
51880 0, iOffset - iOffsetShift, szRegion + iOffsetShift
51883 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
51884 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
51888 pShmNode->lastErrno = osGetLastError();
51889 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
51890 "winShmMap3", pDbFd->zPath);
51895 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
51896 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
51897 pShmNode->nRegion++;
51902 if( pShmNode->nRegion>iRegion ){
51905 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51910 if( pShmNode->isReadonly && rc==SQLITE_OK ){
51913 sqlite3_mutex_leave(pShmNode->mutex);
51930 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
51932 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
51933 pFile->mmapSize, pFile->mmapSizeMax));
51934 if( pFile->pMapRegion ){
51935 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
51936 pFile->lastErrno = osGetLastError();
51937 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
51939 pFile->pMapRegion));
51940 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51941 "winUnmapfile1", pFile->zPath);
51943 pFile->pMapRegion = 0;
51944 pFile->mmapSize = 0;
51946 if( pFile->hMap!=NULL ){
51947 if( !osCloseHandle(pFile->hMap) ){
51948 pFile->lastErrno = osGetLastError();
51949 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
51950 osGetCurrentProcessId(), pFile, pFile->hMap));
51951 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
51952 "winUnmapfile2", pFile->zPath);
51954 pFile->hMap = NULL;
51956 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
51962 ** Memory map or remap the file opened by file-descriptor pFd (if the file
51965 ** outstanding xFetch() references to it, this function is a no-op.
51967 ** If parameter nByte is non-negative, then it is the requested size of
51981 assert( nMap>=0 || pFd->nFetchOut==0 );
51982 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
51985 if( pFd->nFetchOut>0 ) return SQLITE_OK;
51990 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
51995 if( nMap>pFd->mmapSizeMax ){
51996 nMap = pFd->mmapSizeMax;
51998 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
52000 if( nMap==0 && pFd->mmapSize>0 ){
52003 if( nMap!=pFd->mmapSize ){
52010 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
52016 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
52018 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
52022 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
52026 if( pFd->hMap==NULL ){
52027 pFd->lastErrno = osGetLastError();
52028 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
52029 "winMapfile1", pFd->zPath);
52031 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
52038 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
52040 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
52043 osCloseHandle(pFd->hMap);
52044 pFd->hMap = NULL;
52045 pFd->lastErrno = osGetLastError();
52046 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
52047 "winMapfile2", pFd->zPath);
52049 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
52053 pFd->pMapRegion = pNew;
52054 pFd->mmapSize = nMap;
52057 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
52085 if( pFd->mmapSizeMax>0 ){
52091 if( pFd->pMapRegion==0 ){
52092 int rc = winMapfile(pFd, -1);
52099 if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
52100 assert( pFd->pMapRegion!=0 );
52101 *pp = &((u8 *)pFd->pMapRegion)[iOff];
52102 pFd->nFetchOut++;
52113 ** If the third argument is non-NULL, then this function releases a
52129 assert( (p==0)==(pFd->nFetchOut==0) );
52132 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
52138 pFd->nFetchOut--;
52142 ** is unnecessary can be omitted - potentially improving
52147 assert( pFd->nFetchOut>=0 );
52233 ** This function returns non-zero if the specified UTF-8 string buffer
52241 if( winIsDirSep(zBuf[nLen-1]) ){
52288 /* It's odd to simulate an io-error here, but really this is just
52289 ** using the io-error infrastructure to test that SQLite handles this
52297 nMax = pVfs->mxPathname;
52301 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52309 nDir = nMax - (nPre + 15);
52314 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
52320 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52362 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52382 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52388 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
52400 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52410 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52415 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
52425 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
52439 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52457 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
52461 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
52469 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
52475 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
52518 ** parameter that prevents an anti-virus retry loop.
52526 sqlite3_vfs *pVfs, /* Used to get maximum path length and AppData */
52527 const char *zName, /* Name of the file (UTF-8) */
52544 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
52587 /* The main DB, main journal, WAL file and super-journal are never
52594 /* Assert that the upper layer has set one of the "file-type" flags. */
52603 pFile->h = INVALID_HANDLE_VALUE;
52625 /* Database filenames are double-zero terminated if they are not
52771 pFile->lastErrno = lastErrno;
52772 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
52789 pAppData = (winVfsAppData*)pVfs->pAppData;
52794 && ((pAppData==NULL) || !pAppData->bNoLock)
52800 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
52805 pFile->zDeleteOnClose = zConverted;
52813 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
52814 pFile->pVfs = pVfs;
52815 pFile->h = h;
52817 pFile->ctrlFlags |= WINFILE_RDONLY;
52822 pFile->ctrlFlags |= WINFILE_PSOW;
52824 pFile->lastErrno = NO_ERROR;
52825 pFile->zPath = zName;
52827 pFile->hMap = NULL;
52828 pFile->pMapRegion = 0;
52829 pFile->mmapSize = 0;
52830 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
53002 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
53048 ** Returns non-zero if the specified path name starts with the "long path"
53059 ** Returns non-zero if the specified path name starts with a drive letter
53070 ** Returns non-zero if the specified path name should be used verbatim. If
53071 ** non-zero is returned from this function, the calling function must simply
53072 ** use the provided path name verbatim -OR- resolve it into a full path name
53116 ** Changes are made in-place. Return the new name length.
53119 ** no-longer used bytes will be written by '\0'.
53126 /* Some test-cases assume that "./foo" and "foo" are different */
53153 if( osGetcwd(zOut, nOut-2)==0 ){
53160 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
53165 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
53172 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
53199 zFull[nFull-1] = '\0';
53232 nByte = osReadlink(zIn, zDel, nFull-1);
53233 if( nByte ==(DWORD)-1 ){
53238 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
53281 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
53284 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
53291 /* It's odd to simulate an io-error here, but really this is just
53292 ** using the io-error infrastructure to test that SQLite handles this
53304 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
53367 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
53369 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+4);
53380 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut+6);
53383 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
53473 for(j=0, k=p->i; j<sz; j++){
53474 p->a[k++] ^= x[j];
53475 if( k>=p->na ) k = 0;
53477 p->i = k;
53478 p->nXor += sz;
53549 ** The following variable, if set to a non-zero value, is interpreted as
53568 /* FILETIME structure is a 64-bit value representing the number of
53569 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
53576 /* 2^32 - to avoid use of LL and warnings in gcc */
53626 ** buffer with a nul-terminated UTF-8 encoded error message
53632 ** is zero if the error message fits in the buffer, or non-zero
53633 ** otherwise (if the message was truncated). If non-zero is returned,
53634 ** then it is not necessary to include the nul-terminator character
53691 "win32-longpath", /* zName */
53716 "win32-none", /* zName */
53741 "win32-longpath-none", /* zName */
53762 /* Double-check that the aSyscall[] array has been constructed
53815 ** 2016-09-07
53826 ** This file implements an in-memory VFS. A database is held as a contiguous
53842 /* Access to a lower-level VFS that (might) implement dynamic loading,
53845 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
53905 ** File-scope variables for holding the memdb files that are accessible
54009 sqlite3_mutex_enter(p->pMutex);
54012 sqlite3_mutex_leave(p->pMutex);
54019 ** Close an memdb-file.
54024 MemStore *p = ((MemFile*)pFile)->pStore;
54025 if( p->zFName ){
54034 if( p->nRef==1 ){
54035 memdb_g.apMemStore[i] = memdb_g.apMemStore[--memdb_g.nMemStore];
54048 p->nRef--;
54049 if( p->nRef<=0 ){
54050 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
54051 sqlite3_free(p->aData);
54054 sqlite3_mutex_free(p->pMutex);
54063 ** Read data from an memdb-file.
54071 MemStore *p = ((MemFile*)pFile)->pStore;
54073 if( iOfst+iAmt>p->sz ){
54075 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
54079 memcpy(zBuf, p->aData+iOfst, iAmt);
54089 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || NEVER(p->nMmap>0) ){
54092 if( newSz>p->szMax ){
54096 if( newSz>p->szMax ) newSz = p->szMax;
54097 pNew = sqlite3Realloc(p->aData, newSz);
54099 p->aData = pNew;
54100 p->szAlloc = newSz;
54105 ** Write data to an memdb-file.
54113 MemStore *p = ((MemFile*)pFile)->pStore;
54115 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
54121 if( iOfst+iAmt>p->sz ){
54123 if( iOfst+iAmt>p->szAlloc
54129 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
54130 p->sz = iOfst+iAmt;
54132 memcpy(p->aData+iOfst, z, iAmt);
54138 ** Truncate an memdb-file.
54145 MemStore *p = ((MemFile*)pFile)->pStore;
54148 if( size>p->sz ){
54152 p->sz = size;
54159 ** Sync an memdb-file.
54168 ** Return the current file-size of an memdb-file.
54171 MemStore *p = ((MemFile*)pFile)->pStore;
54173 *pSize = p->sz;
54179 ** Lock an memdb-file.
54183 MemStore *p = pThis->pStore;
54185 if( eLock<=pThis->eLock ) return SQLITE_OK;
54188 assert( p->nWrLock==0 || p->nWrLock==1 );
54189 assert( pThis->eLock<=SQLITE_LOCK_SHARED || p->nWrLock==1 );
54190 assert( pThis->eLock==SQLITE_LOCK_NONE || p->nRdLock>=1 );
54192 if( eLock>SQLITE_LOCK_SHARED && (p->mFlags & SQLITE_DESERIALIZE_READONLY) ){
54197 assert( pThis->eLock==SQLITE_LOCK_NONE );
54198 if( p->nWrLock>0 ){
54201 p->nRdLock++;
54208 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
54209 if( ALWAYS(pThis->eLock==SQLITE_LOCK_SHARED) ){
54210 if( p->nWrLock>0 ){
54213 p->nWrLock = 1;
54221 assert( pThis->eLock>=SQLITE_LOCK_SHARED );
54222 if( p->nRdLock>1 ){
54224 }else if( pThis->eLock==SQLITE_LOCK_SHARED ){
54225 p->nWrLock = 1;
54231 if( rc==SQLITE_OK ) pThis->eLock = eLock;
54237 ** Unlock an memdb-file.
54241 MemStore *p = pThis->pStore;
54242 if( eLock>=pThis->eLock ) return SQLITE_OK;
54247 if( ALWAYS(pThis->eLock>SQLITE_LOCK_SHARED) ){
54248 p->nWrLock--;
54251 if( pThis->eLock>SQLITE_LOCK_SHARED ){
54252 p->nWrLock--;
54254 p->nRdLock--;
54257 pThis->eLock = eLock;
54265 ** occur on an in-memory database.
54275 ** File control method. For custom operations on an memdb-file.
54278 MemStore *p = ((MemFile*)pFile)->pStore;
54282 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
54287 if( iLimit<p->sz ){
54289 iLimit = p->szMax;
54291 iLimit = p->sz;
54294 p->szMax = iLimit;
54304 ** Return the sector-size in bytes for an memdb-file.
54312 ** Return the device characteristic flags supported by an memdb-file.
54322 /* Fetch a page of a memory-mapped file */
54329 MemStore *p = ((MemFile*)pFile)->pStore;
54331 if( iOfst+iAmt>p->sz || (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)!=0 ){
54334 p->nMmap++;
54335 *pp = (void*)(p->aData + iOfst);
54341 /* Release a memory-mapped page */
54343 MemStore *p = ((MemFile*)pFile)->pStore;
54347 p->nMmap--;
54376 if( strcmp(memdb_g.apMemStore[i]->zFName,zName)==0 ){
54398 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE|SQLITE_DESERIALIZE_FREEONCLOSE;
54399 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
54400 p->zFName = (char*)&p[1];
54401 memcpy(p->zFName, zName, szName+1);
54402 p->pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
54403 if( p->pMutex==0 ){
54404 memdb_g.nMemStore--;
54409 p->nRef = 1;
54413 p->nRef++;
54422 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
54423 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
54425 pFile->pStore = p;
54429 pFd->pMethods = &memdb_io_methods;
54434 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
54438 ** ensure the file-system modifications are synced to disk before
54485 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
54490 ** utf-8 string describing the most recent error encountered associated
54494 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
54501 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
54508 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
54516 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
54524 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
54532 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
54537 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
54540 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
54552 if( p->base.pMethods!=&memdb_io_methods ) return 0;
54553 pStore = p->pStore;
54555 if( pStore->zFName!=0 ) p = 0;
54586 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
54589 if( piSize ) *piSize = -1;
54592 MemStore *pStore = p->pStore;
54593 assert( pStore->pMutex==0 );
54594 if( piSize ) *piSize = pStore->sz;
54596 pOut = pStore->aData;
54598 pOut = sqlite3_malloc64( pStore->sz );
54599 if( pOut ) memcpy(pOut, pStore->aData, pStore->sz);
54603 pBt = db->aDb[iDb].pBt;
54607 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
54634 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
54674 sqlite3_mutex_enter(db->mutex);
54675 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
54686 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
54690 db->init.iDb = (u8)iDb;
54691 db->init.reopenMemdb = 1;
54693 db->init.reopenMemdb = 0;
54702 MemStore *pStore = p->pStore;
54703 pStore->aData = pData;
54705 pStore->sz = szDb;
54706 pStore->szAlloc = szBuf;
54707 pStore->szMax = szBuf;
54708 if( pStore->szMax<sqlite3GlobalConfig.mxMemdbSize ){
54709 pStore->szMax = sqlite3GlobalConfig.mxMemdbSize;
54711 pStore->mFlags = mFlags;
54720 sqlite3_mutex_leave(db->mutex);
54739 sz = pLower->szOsFile;
54764 ** This file implements an object that represents a fixed-length
54768 ** journalled during a transaction, or which pages have the "dont-write"
54797 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
54814 ** sub-dividing and re-hashing. */
54819 ** no fewer collisions than the no-op *1. */
54839 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
54848 u32 nSet; /* Number of bits that are set - only valid for aHash
54872 p->iSize = iSize;
54878 ** Check to see if the i-th bit is set. Return true or false.
54884 i--;
54885 if( i>=p->iSize ) return 0;
54886 while( p->iDivisor ){
54887 u32 bin = i/p->iDivisor;
54888 i = i%p->iDivisor;
54889 p = p->u.apSub[bin];
54894 if( p->iSize<=BITVEC_NBIT ){
54895 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
54898 while( p->u.aHash[h] ){
54899 if( p->u.aHash[h]==i ) return 1;
54910 ** Set the i-th bit. Return 0 on success and an error code if
54913 ** This routine might cause sub-bitmaps to be allocated. Failing
54914 ** to get the memory needed to hold the sub-bitmap is the only
54925 assert( i<=p->iSize );
54926 i--;
54927 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
54928 u32 bin = i/p->iDivisor;
54929 i = i%p->iDivisor;
54930 if( p->u.apSub[bin]==0 ){
54931 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
54932 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
54934 p = p->u.apSub[bin];
54936 if( p->iSize<=BITVEC_NBIT ){
54937 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
54943 /* worrying about sub-dividing and re-hashing. */
54944 if( !p->u.aHash[h] ){
54945 if (p->nSet<(BITVEC_NINT-1)) {
54954 if( p->u.aHash[h]==i ) return SQLITE_OK;
54957 } while( p->u.aHash[h] );
54962 if( p->nSet>=BITVEC_MXHASH ){
54965 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
54969 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54970 memset(p->u.apSub, 0, sizeof(p->u.apSub));
54971 p->iDivisor = p->iSize/BITVEC_NPTR;
54972 if( (p->iSize%BITVEC_NPTR)!=0 ) p->iDivisor++;
54973 if( p->iDivisor<BITVEC_NBIT ) p->iDivisor = BITVEC_NBIT;
54983 p->nSet++;
54984 p->u.aHash[h] = i;
54989 ** Clear the i-th bit.
54997 i--;
54998 while( p->iDivisor ){
54999 u32 bin = i/p->iDivisor;
55000 i = i%p->iDivisor;
55001 p = p->u.apSub[bin];
55006 if( p->iSize<=BITVEC_NBIT ){
55007 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(BITVEC_TELEM)(1<<(i&(BITVEC_SZELEM-1)));
55011 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
55012 memset(p->u.aHash, 0, sizeof(p->u.aHash));
55013 p->nSet = 0;
55016 u32 h = BITVEC_HASH(aiValues[j]-1);
55017 p->nSet++;
55018 while( p->u.aHash[h] ){
55022 p->u.aHash[h] = aiValues[j];
55033 if( p->iDivisor ){
55036 sqlite3BitvecDestroy(p->u.apSub[i]);
55047 return p->iSize;
55089 ** If a memory allocation error occurs, return -1.
55094 int rc = -1;
55117 i = aOp[pc+2] - 1;
55129 if( (--aOp[pc+1]) > 0 ) nx = 0;
55145 ** match (rc==0). Change rc to non-zero if a discrepancy
55150 + (sqlite3BitvecSize(pBitvec) - sz);
55196 ** such that p was added to the list more recently than p->pDirtyNext.
55201 ** page to eject from the cache mid-transaction. It is better to eject
55244 pPg = (PgHdr*)pLower->pExtra;
55245 printf("%3d: nRef %2lld flgs %02x data ", i, pPg->nRef, pPg->flags);
55246 a = (unsigned char *)pLower->pBuf;
55257 if( pCache->pCache==0 ) return;
55261 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
55263 if( pLower && ((PgHdr*)pLower)->pPage==0 ){
55264 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
55281 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55288 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55311 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
55312 pCache = pPg->pCache;
55314 if( pPg->flags & PGHDR_CLEAN ){
55315 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
55318 assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
55319 assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
55320 assert( pPg->pDirtyPrev==0 || pPg->pDirtyPrev->pDirtyNext==pPg );
55321 assert( pPg->pDirtyPrev!=0 || pCache->pDirty==pPg );
55325 if( pPg->flags & PGHDR_WRITEABLE ){
55326 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
55364 PCache *p = pPage->pCache;
55368 pPage->pgno));
55370 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
55371 assert( pPage->pDirtyPrev || pPage==p->pDirty );
55374 if( p->pSynced==pPage ){
55375 p->pSynced = pPage->pDirtyPrev;
55378 if( pPage->pDirtyNext ){
55379 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
55381 assert( pPage==p->pDirtyTail );
55382 p->pDirtyTail = pPage->pDirtyPrev;
55384 if( pPage->pDirtyPrev ){
55385 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
55391 assert( pPage==p->pDirty );
55392 p->pDirty = pPage->pDirtyNext;
55393 assert( p->bPurgeable || p->eCreate==2 );
55394 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
55395 assert( p->bPurgeable==0 || p->eCreate==1 );
55396 p->eCreate = 2;
55401 pPage->pDirtyPrev = 0;
55402 pPage->pDirtyNext = p->pDirty;
55403 if( pPage->pDirtyNext ){
55404 assert( pPage->pDirtyNext->pDirtyPrev==0 );
55405 pPage->pDirtyNext->pDirtyPrev = pPage;
55407 p->pDirtyTail = pPage;
55408 if( p->bPurgeable ){
55409 assert( p->eCreate==2 );
55410 p->eCreate = 1;
55413 p->pDirty = pPage;
55419 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
55420 if( !p->pSynced
55421 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
55423 p->pSynced = pPage;
55431 ** being used for an in-memory database, this function is a no-op.
55434 if( p->pCache->bPurgeable ){
55435 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
55436 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
55437 pcacheDump(p->pCache);
55442 ** Compute the number of pages of cache requested. p->szCache is the
55446 if( p->szCache>=0 ){
55447 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
55449 return p->szCache;
55452 /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
55456 n = ((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
55469 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
55470 ** built-in default page cache is used instead of the application defined
55479 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
55510 p->szPage = 1;
55511 p->szExtra = szExtra;
55513 p->bPurgeable = bPurgeable;
55514 p->eCreate = 2;
55515 p->xStress = xStress;
55516 p->pStress = pStress;
55517 p->szCache = 100;
55518 p->szSpill = 1;
55528 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
55529 if( pCache->szPage ){
55532 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
55533 pCache->bPurgeable
55537 if( pCache->pCache ){
55538 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
55540 pCache->pCache = pNew;
55541 pCache->szPage = szPage;
55580 assert( pCache->pCache!=0 );
55582 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
55591 eCreate = createFlag & pCache->eCreate;
55593 assert( createFlag==0 || pCache->eCreate==eCreate );
55594 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
55595 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
55619 if( pCache->eCreate==2 ) return 0;
55621 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
55622 /* Find a dirty page to write-out and recycle. First try to find a
55623 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
55631 for(pPg=pCache->pSynced;
55632 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
55633 pPg=pPg->pDirtyPrev
55635 pCache->pSynced = pPg;
55637 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
55643 "spill page %d making room for %d - cache used: %d/%d",
55644 pPg->pgno, pgno,
55645 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
55648 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
55649 rc = pCache->xStress(pCache->pStress, pPg);
55656 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
55676 pPgHdr = (PgHdr*)pPage->pExtra;
55677 assert( pPgHdr->pPage==0 );
55678 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
55679 pPgHdr->pPage = pPage;
55680 pPgHdr->pData = pPage->pBuf;
55681 pPgHdr->pExtra = (void *)&pPgHdr[1];
55682 memset(pPgHdr->pExtra, 0, 8);
55683 assert( EIGHT_BYTE_ALIGNMENT( pPgHdr->pExtra ) );
55684 pPgHdr->pCache = pCache;
55685 pPgHdr->pgno = pgno;
55686 pPgHdr->flags = PGHDR_CLEAN;
55704 pPgHdr = (PgHdr *)pPage->pExtra;
55706 if( !pPgHdr->pPage ){
55709 pCache->nRefSum++;
55710 pPgHdr->nRef++;
55720 assert( p->nRef>0 );
55721 p->pCache->nRefSum--;
55722 if( (--p->nRef)==0 ){
55723 if( p->flags&PGHDR_CLEAN ){
55736 assert(p->nRef>0);
55738 p->nRef++;
55739 p->pCache->nRefSum++;
55748 assert( p->nRef==1 );
55750 if( p->flags&PGHDR_DIRTY ){
55753 p->pCache->nRefSum--;
55754 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
55762 assert( p->nRef>0 );
55764 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
55765 p->flags &= ~PGHDR_DONT_WRITE;
55766 if( p->flags & PGHDR_CLEAN ){
55767 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
55768 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
55769 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
55783 assert( (p->flags & PGHDR_DIRTY)!=0 );
55784 assert( (p->flags & PGHDR_CLEAN)==0 );
55786 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
55787 p->flags |= PGHDR_CLEAN;
55788 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
55790 if( p->nRef==0 ){
55800 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
55801 while( (p = pCache->pDirty)!=0 ){
55811 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
55812 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55813 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
55815 pCache->pSynced = pCache->pDirtyTail;
55823 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55824 p->flags &= ~PGHDR_NEED_SYNC;
55826 pCache->pSynced = pCache->pDirtyTail;
55833 PCache *pCache = p->pCache;
55835 assert( p->nRef>0 );
55838 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
55839 pOther = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, newPgno, 0);
55841 PgHdr *pXPage = (PgHdr*)pOther->pExtra;
55842 assert( pXPage->nRef==0 );
55843 pXPage->nRef++;
55844 pCache->nRefSum++;
55847 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
55848 p->pgno = newPgno;
55849 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
55865 if( pCache->pCache ){
55869 for(p=pCache->pDirty; p; p=pNext){
55870 pNext = p->pDirtyNext;
55875 assert( p->pgno>0 );
55876 if( p->pgno>pgno ){
55877 assert( p->flags&PGHDR_DIRTY );
55881 if( pgno==0 && pCache->nRefSum ){
55883 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
55885 ** pCache->nRefSum>0 */
55886 memset(pPage1->pBuf, 0, pCache->szPage);
55890 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
55898 assert( pCache->pCache!=0 );
55900 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
55919 if( pA->pgno<pB->pgno ){
55920 pTail->pDirty = pA;
55922 pA = pA->pDirty;
55924 pTail->pDirty = pB;
55928 pTail->pDirty = pB;
55930 pB = pB->pDirty;
55932 pTail->pDirty = pA;
55957 pIn = p->pDirty;
55958 p->pDirty = 0;
55959 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
55968 if( NEVER(i==N_SORT_BUCKET-1) ){
55988 for(p=pCache->pDirty; p; p=p->pDirtyNext){
55989 p->pDirty = p->pDirtyNext;
55991 return pcacheSortDirtyList(pCache->pDirty);
56001 return pCache->nRefSum;
56008 return p->nRef;
56015 assert( pCache->pCache!=0 );
56016 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
56021 ** Get the suggested cache-size value.
56029 ** Set the suggested cache-size value.
56032 assert( pCache->pCache!=0 );
56033 pCache->szCache = mxPage;
56034 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
56039 ** Set the suggested cache-spill value. Make no changes if if the
56040 ** argument is zero. Return the effective cache-spill size, which will
56045 assert( p->pCache!=0 );
56048 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
56050 p->szSpill = mxPage;
56053 if( res<p->szSpill ) res = p->szSpill;
56061 assert( pCache->pCache!=0 );
56062 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
56067 ** in the page-cache hierarchy.
56079 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
56088 return (pCache->pDirty!=0);
56100 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
56128 ** -------------------------------------------------------------
56130 ** -------------------------------------------------------------
56146 ** size can vary according to architecture, compile-time options, and
56152 ** allocations. However, the btree layer needs a small (16-byte) overrun
56167 ** (1) The general-purpose memory allocator - sqlite3Malloc()
56168 ** (2) Global page-cache memory provided using sqlite3_config() with
56170 ** (3) PCache-local bulk allocation.
56180 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
56184 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
56200 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
56210 ** but causes a 2-byte gap in the structure for most architectures (since
56211 ** pointers must be either 4 or 8-byte aligned). As this structure is located
56213 ** corrupt, code at the b-tree layer may overread the page buffer and
56219 ** The pLruNext and pLruPrev pointers form a double-linked circular list
56240 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
56241 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
56269 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
56275 ** open database file (including each in-memory database and each
56290 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
56308 PgHdr1 *pFree; /* List of unused pcache-local pages */
56309 void *pBulk; /* Bulk memory used by pcache-local */
56356 # define pcache1EnterMutex(X) assert((X)->mutex==0)
56357 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
56360 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
56361 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
56371 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
56390 while( n-- ){
56392 p->pNext = pcache1.pFree;
56401 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
56402 ** true if pCache->pFree ends up containing one or more free pages.
56409 if( pCache->nMax<3 ) return 0;
56412 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
56414 szBulk = -1024 * (i64)pcache1.nInitPage;
56416 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
56417 szBulk = pCache->szAlloc*(i64)pCache->nMax;
56419 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
56422 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
56424 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
56425 pX->page.pBuf = zBulk;
56426 pX->page.pExtra = (u8*)pX + ROUND8(sizeof(*pX));
56427 assert( EIGHT_BYTE_ALIGNMENT( pX->page.pExtra ) );
56428 pX->isBulkLocal = 1;
56429 pX->isAnchor = 0;
56430 pX->pNext = pCache->pFree;
56431 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
56432 pCache->pFree = pX;
56433 zBulk += pCache->szAlloc;
56434 }while( --nBulk );
56436 return pCache->pFree!=0;
56455 pcache1.pFree = pcache1.pFree->pNext;
56456 pcache1.nFreeSlot--;
56493 pSlot->pNext = pcache1.pFree;
56540 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
56541 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
56542 assert( pCache->pFree!=0 );
56543 p = pCache->pFree;
56544 pCache->pFree = p->pNext;
56545 p->pNext = 0;
56552 assert( pCache->pGroup==&pcache1.grp );
56553 pcache1LeaveMutex(pCache->pGroup);
56556 pPg = pcache1Alloc(pCache->szAlloc);
56559 pcache1EnterMutex(pCache->pGroup);
56562 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
56563 p->page.pBuf = pPg;
56564 p->page.pExtra = (u8*)p + ROUND8(sizeof(*p));
56565 assert( EIGHT_BYTE_ALIGNMENT( p->page.pExtra ) );
56566 p->isBulkLocal = 0;
56567 p->isAnchor = 0;
56568 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
56570 (*pCache->pnPurgeable)++;
56580 pCache = p->pCache;
56581 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
56582 if( p->isBulkLocal ){
56583 p->pNext = pCache->pFree;
56584 pCache->pFree = p;
56586 pcache1Free(p->page.pBuf);
56588 (*pCache->pnPurgeable)--;
56626 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
56647 assert( sqlite3_mutex_held(p->pGroup->mutex) );
56649 nNew = 2*(u64)p->nHash;
56654 pcache1LeaveMutex(p->pGroup);
56655 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
56657 if( p->nHash ){ sqlite3EndBenignMalloc(); }
56658 pcache1EnterMutex(p->pGroup);
56660 for(i=0; i<p->nHash; i++){
56662 PgHdr1 *pNext = p->apHash[i];
56664 unsigned int h = pPage->iKey % nNew;
56665 pNext = pPage->pNext;
56666 pPage->pNext = apNew[h];
56670 sqlite3_free(p->apHash);
56671 p->apHash = apNew;
56672 p->nHash = nNew;
56679 ** LRU list, then this function is a no-op.
56686 assert( pPage->pLruNext );
56687 assert( pPage->pLruPrev );
56688 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
56689 pPage->pLruPrev->pLruNext = pPage->pLruNext;
56690 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
56691 pPage->pLruNext = 0;
56692 /* pPage->pLruPrev = 0;
56694 assert( pPage->isAnchor==0 );
56695 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
56696 pPage->pCache->nRecyclable--;
56710 PCache1 *pCache = pPage->pCache;
56713 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
56714 h = pPage->iKey % pCache->nHash;
56715 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
56716 *pp = (*pp)->pNext;
56718 pCache->nPage--;
56727 PGroup *pGroup = pCache->pGroup;
56729 assert( sqlite3_mutex_held(pGroup->mutex) );
56730 while( pGroup->nPurgeable>pGroup->nMaxPage
56731 && (p=pGroup->lru.pLruPrev)->isAnchor==0
56733 assert( p->pCache->pGroup==pGroup );
56738 if( pCache->nPage==0 && pCache->pBulk ){
56739 sqlite3_free(pCache->pBulk);
56740 pCache->pBulk = pCache->pFree = 0;
56755 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
56757 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
56758 assert( pCache->iMaxKey >= iLimit );
56759 assert( pCache->nHash > 0 );
56760 if( pCache->iMaxKey - iLimit < pCache->nHash ){
56765 h = iLimit % pCache->nHash;
56766 iStop = pCache->iMaxKey % pCache->nHash;
56767 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
56771 h = pCache->nHash/2;
56772 iStop = h - 1;
56777 assert( h<pCache->nHash );
56778 pp = &pCache->apHash[h];
56780 if( pPage->iKey>=iLimit ){
56781 pCache->nPage--;
56782 *pp = pPage->pNext;
56786 pp = &pPage->pNext;
56791 h = (h+1) % pCache->nHash;
56793 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
56810 ** private PGroup (mode-1). pcache1.separateCache is false if the single
56811 ** PGroup in pcache1.grp is used for all page caches (mode-2).
56813 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
56815 ** * Use a unified cache in single-threaded applications that have
56816 ** configured a start-time buffer for use as page-cache memory using
56817 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
56820 ** * Otherwise use separate caches (mode-1)
56874 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
56882 pGroup->mxPinned = 10;
56887 if( pGroup->lru.isAnchor==0 ){
56888 pGroup->lru.isAnchor = 1;
56889 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
56891 pCache->pGroup = pGroup;
56892 pCache->szPage = szPage;
56893 pCache->szExtra = szExtra;
56894 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
56895 pCache->bPurgeable = (bPurgeable ? 1 : 0);
56898 pCache->nMin = 10;
56899 pGroup->nMinPage += pCache->nMin;
56900 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56901 pCache->pnPurgeable = &pGroup->nPurgeable;
56903 pCache->pnPurgeable = &pCache->nPurgeableDummy;
56906 if( pCache->nHash==0 ){
56923 if( pCache->bPurgeable ){
56924 PGroup *pGroup = pCache->pGroup;
56927 if( n > 0x7fff0000 - pGroup->nMaxPage + pCache->nMax ){
56928 n = 0x7fff0000 - pGroup->nMaxPage + pCache->nMax;
56930 pGroup->nMaxPage += (n - pCache->nMax);
56931 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
56932 pCache->nMax = n;
56933 pCache->n90pct = pCache->nMax*9/10;
56946 if( pCache->bPurgeable ){
56947 PGroup *pGroup = pCache->pGroup;
56950 savedMaxPage = pGroup->nMaxPage;
56951 pGroup->nMaxPage = 0;
56953 pGroup->nMaxPage = savedMaxPage;
56964 pcache1EnterMutex(pCache->pGroup);
56965 n = pCache->nPage;
56966 pcache1LeaveMutex(pCache->pGroup);
56985 PGroup *pGroup = pCache->pGroup;
56989 assert( pCache->nPage >= pCache->nRecyclable );
56990 nPinned = pCache->nPage - pCache->nRecyclable;
56991 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
56992 assert( pCache->n90pct == pCache->nMax*9/10 );
56994 nPinned>=pGroup->mxPinned
56995 || nPinned>=pCache->n90pct
56996 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
57001 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
57002 assert( pCache->nHash>0 && pCache->apHash );
57005 if( pCache->bPurgeable
57006 && !pGroup->lru.pLruPrev->isAnchor
57007 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
57010 pPage = pGroup->lru.pLruPrev;
57014 pOther = pPage->pCache;
57015 if( pOther->szAlloc != pCache->szAlloc ){
57019 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
57031 unsigned int h = iKey % pCache->nHash;
57032 pCache->nPage++;
57033 pPage->iKey = iKey;
57034 pPage->pNext = pCache->apHash[h];
57035 pPage->pCache = pCache;
57036 pPage->pLruNext = 0;
57037 /* pPage->pLruPrev = 0;
57039 *(void **)pPage->page.pExtra = 0;
57040 pCache->apHash[h] = pPage;
57041 if( iKey>pCache->iMaxKey ){
57042 pCache->iMaxKey = iKey;
57058 ** For a non-purgeable cache (a cache used as the storage for an in-memory
57061 ** a non-purgeable cache.
57104 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
57116 pPage = pCache->apHash[iKey % pCache->nHash];
57117 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
57145 pcache1EnterMutex(pCache->pGroup);
57147 assert( pPage==0 || pCache->iMaxKey>=iKey );
57148 pcache1LeaveMutex(pCache->pGroup);
57162 assert( pCache->bPurgeable || createFlag!=1 );
57163 assert( pCache->bPurgeable || pCache->nMin==0 );
57164 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
57165 assert( pCache->nMin==0 || pCache->bPurgeable );
57166 assert( pCache->nHash>0 );
57168 if( pCache->pGroup->mutex ){
57190 PGroup *pGroup = pCache->pGroup;
57192 assert( pPage->pCache==pCache );
57198 assert( pPage->pLruNext==0 );
57201 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
57205 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
57206 pPage->pLruPrev = &pGroup->lru;
57207 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
57209 pCache->nRecyclable++;
57212 pcache1LeaveMutex(pCache->pGroup);
57228 assert( pPage->iKey==iOld );
57229 assert( pPage->pCache==pCache );
57232 pcache1EnterMutex(pCache->pGroup);
57235 hOld = iOld%pCache->nHash;
57236 pp = &pCache->apHash[hOld];
57238 pp = &(*pp)->pNext;
57240 *pp = pPage->pNext;
57243 hNew = iNew%pCache->nHash;
57244 pPage->iKey = iNew;
57245 pPage->pNext = pCache->apHash[hNew];
57246 pCache->apHash[hNew] = pPage;
57247 if( iNew>pCache->iMaxKey ){
57248 pCache->iMaxKey = iNew;
57251 pcache1LeaveMutex(pCache->pGroup);
57263 pcache1EnterMutex(pCache->pGroup);
57264 if( iLimit<=pCache->iMaxKey ){
57266 pCache->iMaxKey = iLimit-1;
57268 pcache1LeaveMutex(pCache->pGroup);
57278 PGroup *pGroup = pCache->pGroup;
57279 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
57281 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
57282 assert( pGroup->nMaxPage >= pCache->nMax );
57283 pGroup->nMaxPage -= pCache->nMax;
57284 assert( pGroup->nMinPage >= pCache->nMin );
57285 pGroup->nMinPage -= pCache->nMin;
57286 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
57289 sqlite3_free(pCache->pBulk);
57290 sqlite3_free(pCache->apHash);
57350 && p->isAnchor==0
57352 nFree += pcache1MemSize(p->page.pBuf);
57376 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
57436 ** a non-zero batch number, it will see all prior INSERTs.
57464 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
57522 p->pChunk = 0;
57523 p->db = db;
57524 p->pEntry = 0;
57525 p->pLast = 0;
57526 p->pForest = 0;
57527 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
57528 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
57529 p->rsFlags = ROWSET_SORTED;
57530 p->iBatch = 0;
57543 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
57544 pNextChunk = pChunk->pNextChunk;
57545 sqlite3DbFree(p->db, pChunk);
57547 p->pChunk = 0;
57548 p->nFresh = 0;
57549 p->pEntry = 0;
57550 p->pLast = 0;
57551 p->pForest = 0;
57552 p->rsFlags = ROWSET_SORTED;
57562 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
57570 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
57575 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
57579 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
57583 pNew->pNextChunk = p->pChunk;
57584 p->pChunk = pNew;
57585 p->pFresh = pNew->aEntry;
57586 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
57588 p->nFresh--;
57589 return p->pFresh++;
57603 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
57607 pEntry->v = rowid;
57608 pEntry->pRight = 0;
57609 pLast = p->pLast;
57611 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
57614 p->rsFlags &= ~ROWSET_SORTED;
57616 pLast->pRight = pEntry;
57618 p->pEntry = pEntry;
57620 p->pLast = pEntry;
57639 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
57640 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
57641 if( pA->v<=pB->v ){
57642 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
57643 pA = pA->pRight;
57645 pTail->pRight = pB;
57649 pTail = pTail->pRight = pB;
57650 pB = pB->pRight;
57652 pTail->pRight = pA;
57670 pNext = pIn->pRight;
57671 pIn->pRight = 0;
57699 if( pIn->pLeft ){
57701 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
57702 p->pRight = pIn;
57706 if( pIn->pRight ){
57707 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
57711 assert( (*ppLast)->pRight==0 );
57734 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
57738 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
57742 pLeft = rowSetNDeepTree(ppList, iDepth-1);
57744 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
57749 p->pLeft = pLeft;
57750 *ppList = p->pRight;
57751 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
57754 *ppList = p->pRight;
57755 p->pLeft = p->pRight = 0;
57771 pList = p->pRight;
57772 p->pLeft = p->pRight = 0;
57776 pList = p->pRight;
57777 p->pLeft = pLeft;
57778 p->pRight = rowSetNDeepTree(&pList, iDepth);
57798 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
57801 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57802 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57803 p->pEntry = rowSetEntrySort(p->pEntry);
57805 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
57809 if( p->pEntry ){
57810 *pRowid = p->pEntry->v;
57811 p->pEntry = p->pEntry->pRight;
57812 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
57827 ** on pRowSet->pEntry, then sort those entries into the forest at
57828 ** pRowSet->pForest so that they can be tested.
57834 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
57839 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
57840 p = pRowSet->pEntry;
57842 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
57843 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
57847 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
57848 ppPrevTree = &pTree->pRight;
57849 if( pTree->pLeft==0 ){
57850 pTree->pLeft = rowSetListToTree(p);
57854 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
57855 pTree->pLeft = 0;
57862 pTree->v = 0;
57863 pTree->pRight = 0;
57864 pTree->pLeft = rowSetListToTree(p);
57867 pRowSet->pEntry = 0;
57868 pRowSet->pLast = 0;
57869 pRowSet->rsFlags |= ROWSET_SORTED;
57871 pRowSet->iBatch = iBatch;
57877 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
57878 p = pTree->pLeft;
57880 if( p->v<iRowid ){
57881 p = p->pRight;
57882 }else if( p->v>iRowid ){
57883 p = p->pLeft;
57929 ** This header file defines the interface to the write-ahead logging
57970 /* Connection to a write-ahead log (WAL) file.
57975 /* Open and close a connection to a write-ahead log. */
57983 ** snapshot is like a read-transaction. It is the state of the database
57992 /* Read a page from the write-ahead log, if it is present. */
58014 /* Write a frame or frames to the log. */
58019 Wal *pWal, /* Write-ahead log connection */
58043 /* Return true if the argument is non-NULL and the WAL module is using
58044 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
58045 ** WAL module is using shared-memory, return false.
58059 ** stored in each frame (i.e. the db page-size when the WAL was created).
58121 ** in length and are aligned on a page boundary.
58124 ** an integer multiple of the page size in length or are taken from the
58130 ** (6) If a super-journal file is used, then all writes to the database file
58131 ** are synced prior to the super-journal being deleted.
58147 ** method is a no-op, but that does not change the fact the SQLite will
58158 ** (11) A database file is well-formed at the beginning and at the conclusion
58182 ** to print out file-descriptors.
58185 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
58188 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
58196 ** OPEN <------+------+
58199 ** +---------> READER-------+ |
58202 ** |<-------WRITER_LOCKED------> ERROR
58205 ** |<------WRITER_CACHEMOD-------->|
58208 ** |<-------WRITER_DBMOD---------->|
58211 ** +<------WRITER_FINISHED-------->+
58216 ** OPEN -> READER [sqlite3PagerSharedLock]
58217 ** READER -> OPEN [pager_unlock]
58219 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
58220 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
58221 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
58222 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
58223 ** WRITER_*** -> READER [pager_end_transaction]
58225 ** WRITER_*** -> ERROR [pager_error]
58226 ** ERROR -> OPEN [pager_unlock]
58232 ** state - the file may or may not be locked and the database size is
58242 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
58243 ** was) in exclusive-locking mode, a user-level read transaction is
58247 ** it opens a read-transaction on the database and returns to state
58248 ** OPEN after the read-transaction is completed. However a connection
58250 ** this state even after the read-transaction is closed. The only way
58254 ** * A read transaction may be active (but a write-transaction cannot).
58256 ** * The dbSize variable may be trusted (even if a user-level read
58260 ** * Even if a read-transaction is not open, it is guaranteed that
58261 ** there is no hot-journal in the file-system.
58265 ** The pager moves to this state from READER when a write-transaction
58267 ** required to start a write-transaction are held, but no actual
58282 ** * If the connection is open in rollback-mode, a RESERVED or greater
58284 ** * If the connection is open in WAL-mode, a WAL write transaction
58323 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
58339 ** The ERROR state is entered when an IO or disk-full error (including
58341 ** difficult to be sure that the in-memory pager state (cache contents,
58342 ** db size etc.) are consistent with the contents of the file-system.
58344 ** Temporary pager files may enter the ERROR state, but in-memory pagers
58348 ** the contents of the page-cache may be left in an inconsistent state.
58360 ** page-cache and any other in-memory state at the same time. Everything
58361 ** is reloaded from disk (and, if necessary, hot-journal rollback performed)
58362 ** when a read-transaction is next opened on the pager (transitioning
58378 ** In other cases, the error is returned to the b-tree layer. The b-tree
58382 ** Condition (3) is necessary because it can be triggered by a read-only
58384 ** code were simply returned to the user, the b-tree layer would not
58386 ** read-only statement cannot leave the pager in an internally inconsistent
58392 ** * The pager is not an in-memory pager.
58402 ** state. There are two exceptions: immediately after exclusive-mode has
58418 ** following locking-states, according to the lock currently held on
58426 ** pagerUnlockDb() take a conservative approach - eLock is always updated
58437 ** from ERROR to OPEN state. At this point there may be a hot-journal file
58438 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
58442 ** of hot-journal detection.
58448 ** a hot-journal may be mistaken for a journal being created by an active
58455 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
58456 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
58457 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
58482 ** set to 0. If a journal-header is written into the main journal while
58485 ** journal before the journal-header. This is required during savepoint
58494 Pgno iSubRec; /* Index of first record in sub-journal */
58519 ** For a real on-disk database, the current lock held on the database file -
58522 ** For a temporary or in-memory database (neither of which require any
58534 ** This boolean variable is used to make sure that the change-counter
58535 ** (the 4-byte header field at byte offset 24 of the database file) is
58538 ** It is set to true when the change-counter field is updated, which
58543 ** updating the change-counter is omitted for the current transaction.
58546 ** need only update the change-counter once, for the first transaction
58552 ** (or may not) specify a super-journal name to be written into the
58555 ** Whether or not a journal file contains a super-journal pointer affects
58558 ** If a journal file does not contain a super-journal pointer, it is
58560 ** it does contain a super-journal pointer the journal file is finalized
58564 ** Journal files that contain super-journal pointers cannot be finalized
58565 ** simply by overwriting the first journal-header with zeroes, as the
58566 ** super-journal pointer could interfere with hot-journal rollback of any
58576 ** This variables control the behavior of cache-spills (calls made by
58578 ** to the file-system in order to free up memory).
58590 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
58591 ** is larger than the database page-size in order to prevent a journal sync
58596 ** This is a boolean variable. If true, then any required sub-journal
58597 ** is opened as an in-memory journal file. If false, then in-memory
58598 ** sub-journals are only used for in-memory pager files.
58601 ** write-transaction is opened.
58612 ** is not an integer multiple of the page-size, the value stored in
58613 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
58615 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
58618 ** During a write-transaction, if pages with page-numbers greater than
58629 ** Throughout a write-transaction, dbFileSize contains the size of
58631 ** write-transaction is first opened, and updated when VFS calls are made
58651 ** write-transaction is opened (at the same time as dbFileSize and
58654 ** size-hint passed to the method call. See pager_write_pagelist() for
58662 ** sub-codes.
58688 u8 readOnly; /* True for a read-only database */
58690 u8 memVfs; /* VFS-implemented memory database */
58703 u8 changeCountDone; /* Set after incrementing the change-counter */
58704 u8 setSuper; /* Super-jrnl name is written into jrnl */
58705 u8 doNotSpill; /* Do not spill the cache when non-zero */
58706 u8 subjInMemory; /* True to use in-memory sub-journals */
58714 int nRec; /* Pages journalled since last j-header written */
58715 u32 cksumInit; /* Quasi-random value added to every checksum */
58716 u32 nSubRec; /* Number of records written to sub-journal */
58720 sqlite3_file *sjfd; /* File descriptor for sub-journal */
58733 ** End of the routinely-changing class members
58736 u16 nExtra; /* Add this many bytes to each in-memory page */
58757 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
58758 char *zWal; /* File name for write-ahead log */
58778 ** a non-testing build. These variables are not thread-safe.
58797 ** written, semi-random garbage data might appear in the journal
58804 ** of a 32-bit checksum on each page of data. The checksum covers both
58805 ** the page number and the pPager->pageSize bytes of data for the page.
58806 ** This cksum is initialized to a 32-bit random value that appears in the
58822 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
58828 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
58831 ** The macro MEMDB is true if we are dealing with an in-memory database.
58839 # define MEMDB pPager->memDb
58844 ** interfaces to access the database using memory-mapped I/O.
58847 # define USEFETCH(x) ((x)->bUseFetch)
58855 ** by the b-tree layer. This is the case if:
58858 ** (2) the VFS for the database is able to do unaligned sub-page reads
58864 assert( pPager->fd!=0 );
58865 if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */
58866 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */
58868 if( pPager->pWal ){
58870 (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
58874 assert( pPager->fd->pMethods->xDeviceCharacteristics!=0 );
58875 if( (pPager->fd->pMethods->xDeviceCharacteristics(pPager->fd)
58884 # define pagerUseWal(x) ((x)->pWal!=0)
58906 assert( p->eState==PAGER_OPEN
58907 || p->eState==PAGER_READER
58908 || p->eState==PAGER_WRITER_LOCKED
58909 || p->eState==PAGER_WRITER_CACHEMOD
58910 || p->eState==PAGER_WRITER_DBMOD
58911 || p->eState==PAGER_WRITER_FINISHED
58912 || p->eState==PAGER_ERROR
58915 /* Regardless of the current state, a temp-file connection always behaves
58917 ** the change-counter field, so the changeCountDone flag is always set.
58919 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
58920 assert( p->tempFile==0 || pPager->changeCountDone );
58922 /* If the useJournal flag is clear, the journal-mode must be "OFF".
58923 ** And if the journal-mode is "OFF", the journal file must not be open.
58925 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
58926 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
58928 /* Check that MEMDB implies noSync. And an in-memory journal. Since
58929 ** this means an in-memory pager performs no IO at all, it cannot encounter
58931 ** a journal file. (although the in-memory journal implementation may
58933 ** is therefore not possible for an in-memory pager to enter the ERROR
58937 assert( !isOpen(p->fd) );
58938 assert( p->noSync );
58939 assert( p->journalMode==PAGER_JOURNALMODE_OFF
58940 || p->journalMode==PAGER_JOURNALMODE_MEMORY
58942 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
58949 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
58950 assert( p->eLock!=PENDING_LOCK );
58952 switch( p->eState ){
58955 assert( pPager->errCode==SQLITE_OK );
58956 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
58960 assert( pPager->errCode==SQLITE_OK );
58961 assert( p->eLock!=UNKNOWN_LOCK );
58962 assert( p->eLock>=SHARED_LOCK );
58966 assert( p->eLock!=UNKNOWN_LOCK );
58967 assert( pPager->errCode==SQLITE_OK );
58969 assert( p->eLock>=RESERVED_LOCK );
58971 assert( pPager->dbSize==pPager->dbOrigSize );
58972 assert( pPager->dbOrigSize==pPager->dbFileSize );
58973 assert( pPager->dbOrigSize==pPager->dbHintSize );
58974 assert( pPager->setSuper==0 );
58978 assert( p->eLock!=UNKNOWN_LOCK );
58979 assert( pPager->errCode==SQLITE_OK );
58986 assert( p->eLock>=RESERVED_LOCK );
58987 assert( isOpen(p->jfd)
58988 || p->journalMode==PAGER_JOURNALMODE_OFF
58989 || p->journalMode==PAGER_JOURNALMODE_WAL
58992 assert( pPager->dbOrigSize==pPager->dbFileSize );
58993 assert( pPager->dbOrigSize==pPager->dbHintSize );
58997 assert( p->eLock==EXCLUSIVE_LOCK );
58998 assert( pPager->errCode==SQLITE_OK );
59000 assert( p->eLock>=EXCLUSIVE_LOCK );
59001 assert( isOpen(p->jfd)
59002 || p->journalMode==PAGER_JOURNALMODE_OFF
59003 || p->journalMode==PAGER_JOURNALMODE_WAL
59004 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
59006 assert( pPager->dbOrigSize<=pPager->dbHintSize );
59010 assert( p->eLock==EXCLUSIVE_LOCK );
59011 assert( pPager->errCode==SQLITE_OK );
59013 assert( isOpen(p->jfd)
59014 || p->journalMode==PAGER_JOURNALMODE_OFF
59015 || p->journalMode==PAGER_JOURNALMODE_WAL
59016 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
59025 assert( pPager->errCode!=SQLITE_OK );
59026 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
59059 , p->zFilename
59060 , p->eState==PAGER_OPEN ? "OPEN" :
59061 p->eState==PAGER_READER ? "READER" :
59062 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
59063 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
59064 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
59065 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
59066 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
59067 , (int)p->errCode
59068 , p->eLock==NO_LOCK ? "NO_LOCK" :
59069 p->eLock==RESERVED_LOCK ? "RESERVED" :
59070 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
59071 p->eLock==SHARED_LOCK ? "SHARED" :
59072 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
59073 , p->exclusiveMode ? "exclusive" : "normal"
59074 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
59075 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
59076 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
59077 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
59078 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
59079 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
59080 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
59081 , p->journalOff, p->journalHdr
59082 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
59101 if( pPager->errCode ){
59102 pPager->xGet = getPageError;
59105 pPager->xGet = getPageMMap;
59108 pPager->xGet = getPageNormal;
59113 ** Return true if it is necessary to write page *pPg into the sub-journal.
59114 ** A page needs to be written into the sub-journal if there exists one
59117 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
59118 ** * The bit corresponding to the page-number is not set in
59122 Pager *pPager = pPg->pPager;
59124 Pgno pgno = pPg->pgno;
59126 for(i=0; i<pPager->nSavepoint; i++){
59127 p = &pPager->aSavepoint[i];
59128 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
59129 for(i=i+1; i<pPager->nSavepoint; i++){
59130 pPager->aSavepoint[i].bTruncateOnRelease = 0;
59143 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
59148 ** Read a 32-bit integer from the given file descriptor. Store the integer
59152 ** All values are stored on disk as big-endian.
59164 ** Write a 32-bit integer into a string buffer in big-endian byte order.
59170 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
59191 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
59194 if( isOpen(pPager->fd) ){
59195 assert( pPager->eLock>=eLock );
59196 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
59197 if( pPager->eLock!=UNKNOWN_LOCK ){
59198 pPager->eLock = (u8)eLock;
59202 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
59220 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
59221 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
59222 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
59223 pPager->eLock = (u8)eLock;
59231 ** This function determines whether or not the atomic-write or
59232 ** atomic-batch-write optimizations can be used with this pager. The
59233 ** atomic-write optimization can be used if:
59243 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
59244 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
59256 assert( isOpen(pPager->fd) );
59257 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
59263 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
59264 return -1;
59270 int nSector = pPager->sectorSize;
59271 int szPage = pPager->pageSize;
59293 ** Return a 32-bit hash of the page data for pPage.
59304 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
59307 pPage->pageHash = pager_pagehash(pPage);
59313 ** that the page is either dirty or still matches the calculated page-hash.
59317 Pager *pPager = pPg->pPager;
59318 assert( pPager->eState!=PAGER_ERROR );
59319 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
59331 ** This function attempts to read a super-journal file name from the
59334 ** used to store a super-journal file name at the end of a journal file.
59338 ** enough space to write the super-journal name). If the super-journal
59340 ** nul-terminator), then this is handled as if no super-journal name
59343 ** If a super-journal file name is present at the end of the journal
59345 ** nul-terminator byte is appended to the buffer following the
59346 ** super-journal file name.
59348 ** If it is determined that no super-journal file name is present
59356 u32 len; /* Length in bytes of super-journal name */
59365 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
59367 || len>szJ-16
59369 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
59370 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
59372 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
59377 /* See if the checksum matches the super-journal name */
59379 cksum -= zSuper[u];
59383 ** containing the super-journal filename is corrupted. This means
59385 ** super-journal filename.
59397 ** following the value in pPager->journalOff, assuming a sector
59398 ** size of pPager->sectorSize bytes.
59403 ** ---------------------------------------
59412 i64 c = pPager->journalOff;
59414 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
59418 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
59425 ** This function is a no-op if the journal file has not been written to
59428 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
59430 ** zero the 28-byte header at the start of the journal file. In either case,
59431 ** if the pager is not in no-sync mode, sync the journal file immediately
59434 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
59445 assert( isOpen(pPager->jfd) );
59446 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
59447 if( pPager->journalOff ){
59448 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
59452 rc = sqlite3OsTruncate(pPager->jfd, 0);
59455 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
59457 if( rc==SQLITE_OK && !pPager->noSync ){
59458 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
59469 rc = sqlite3OsFileSize(pPager->jfd, &sz);
59471 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
59484 ** - 8 bytes: Magic identifying journal format.
59485 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
59486 ** - 4 bytes: Random number used for page hash.
59487 ** - 4 bytes: Initial database page count.
59488 ** - 4 bytes: Sector size used by the process that wrote this journal.
59489 ** - 4 bytes: Database page size.
59491 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
59495 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
59496 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
59500 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
59510 for(ii=0; ii<pPager->nSavepoint; ii++){
59511 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
59512 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
59516 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
59519 ** Write the nRec Field - the number of page records that follow this
59522 ** if in full-sync mode), the zero is overwritten with the true number
59532 ** * When the pager is in no-sync mode. Corruption can follow a
59538 assert( isOpen(pPager->fd) || pPager->noSync );
59539 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
59540 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
59550 /* The random check-hash initializer */
59551 if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
59552 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
59566 sqlite3OsFileSize(pPager->jfd, &sz);
59568 assert( pPager->journalOff==journalHdrOffset(pPager) );
59569 assert( sqlite3JournalIsInMemory(pPager->jfd) );
59572 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
59575 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
59577 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
59580 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
59588 nHeader-(sizeof(aJournalMagic)+20));
59599 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
59602 ** The loop is required here in case the sector-size is larger than the
59608 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
59609 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
59610 assert( pPager->journalHdr <= pPager->journalOff );
59611 pPager->journalOff += nHeader;
59621 ** pPager->journalOff. See comments above function writeJournalHdr() for
59626 ** database before the transaction began, in pages. Also, pPager->cksumInit
59645 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
59651 pPager->journalOff = journalHdrOffset(pPager);
59652 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
59655 iHdrOff = pPager->journalOff;
59662 if( isHot || iHdrOff!=pPager->journalHdr ){
59663 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
59672 /* Read the first three 32-bit fields of the journal header: The nRec
59673 ** field, the checksum-initializer and the database size at the start
59676 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
59677 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
59678 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
59683 if( pPager->journalOff==0 ){
59684 u32 iPageSize; /* Page-size field of journal header */
59685 u32 iSectorSize; /* Sector-size field of journal header */
59687 /* Read the page-size and sector-size journal header fields. */
59688 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
59689 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
59694 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
59699 iPageSize = pPager->pageSize;
59702 /* Check that the values read from the page-size and sector-size fields
59709 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
59711 /* If the either the page-size or sector-size in the journal-header is
59712 ** invalid, then the process that wrote the journal-header must have
59719 /* Update the page-size to match the value read from the journal.
59723 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
59726 /* Update the assumed sector-size to match the value used by
59732 pPager->sectorSize = iSectorSize;
59735 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
59741 ** Write the supplied super-journal name into the journal file for pager
59742 ** pPager at the current location. The super-journal name must be the last
59743 ** thing written to a journal file. If the pager is in full-sync mode, the
59748 ** + N bytes: super-journal filename in utf-8.
59749 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
59750 ** + 4 bytes: super-journal name checksum.
59753 ** The super-journal page checksum is the sum of the bytes in the super-journal
59754 ** name, where each byte is interpreted as a signed 8-bit integer.
59757 ** this call is a no-op.
59761 int nSuper; /* Length of string zSuper */
59766 assert( pPager->setSuper==0 );
59770 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
59771 || !isOpen(pPager->jfd)
59775 pPager->setSuper = 1;
59776 assert( pPager->journalHdr <= pPager->journalOff );
59778 /* Calculate the length in bytes and the checksum of zSuper */
59783 /* If in full-sync mode, advance to the next disk sector before writing
59784 ** the super-journal name. This is in case the previous page written to
59787 if( pPager->fullSync ){
59788 pPager->journalOff = journalHdrOffset(pPager);
59790 iHdrOff = pPager->journalOff;
59792 /* Write the super-journal data to the end of the journal file. If
59795 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_SJ_PGNO(pPager))))
59796 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
59797 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
59798 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
59799 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
59804 pPager->journalOff += (nSuper+20);
59806 /* If the pager is in persistent-journal mode, then the physical
59807 ** journal-file may extend past the end of the super-journal name
59809 ** dangerous because the code to rollback a hot-journal file
59810 ** will not be able to find the super-journal name to determine
59816 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
59817 && jrnlSize>pPager->journalOff
59819 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
59825 ** Discard the entire contents of the in-memory page-cache.
59828 pPager->iDataVersion++;
59829 sqlite3BackupRestart(pPager->pBackup);
59830 sqlite3PcacheClear(pPager->pPCache);
59834 ** Return the pPager->iDataVersion value
59837 return pPager->iDataVersion;
59842 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
59847 for(ii=0; ii<pPager->nSavepoint; ii++){
59848 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
59850 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
59851 sqlite3OsClose(pPager->sjfd);
59853 sqlite3_free(pPager->aSavepoint);
59854 pPager->aSavepoint = 0;
59855 pPager->nSavepoint = 0;
59856 pPager->nSubRec = 0;
59868 for(ii=0; ii<pPager->nSavepoint; ii++){
59869 PagerSavepoint *p = &pPager->aSavepoint[ii];
59870 if( pgno<=p->nOrig ){
59871 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
59880 ** This function is a no-op if the pager is in exclusive mode and not
59884 ** If the pager is not in exclusive-access mode, the database file is
59885 ** completely unlocked. If the file is unlocked and the file-system does
59891 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
59892 ** or not, any journal file left in the file-system will be treated
59893 ** as a hot-journal and rolled back the next time a read-transaction
59898 assert( pPager->eState==PAGER_READER
59899 || pPager->eState==PAGER_OPEN
59900 || pPager->eState==PAGER_ERROR
59903 sqlite3BitvecDestroy(pPager->pInJournal);
59904 pPager->pInJournal = 0;
59908 assert( !isOpen(pPager->jfd) );
59909 if( pPager->eState==PAGER_ERROR ){
59911 ** then the Wal object may be holding a write-lock but no read-lock.
59912 ** This call ensures that the write-lock is dropped as well. We cannot
59913 ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once
59916 sqlite3WalEndWriteTransaction(pPager->pWal);
59918 sqlite3WalEndReadTransaction(pPager->pWal);
59919 pPager->eState = PAGER_OPEN;
59920 }else if( !pPager->exclusiveMode ){
59922 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
59936 || 1!=(pPager->journalMode & 5)
59938 sqlite3OsClose(pPager->jfd);
59947 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
59948 pPager->eLock = UNKNOWN_LOCK;
59952 ** without clearing the error code. This is intentional - the error
59955 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
59956 pPager->eState = PAGER_OPEN;
59962 ** normal and exclusive-locking mode.
59964 assert( pPager->errCode==SQLITE_OK || !MEMDB );
59965 if( pPager->errCode ){
59966 if( pPager->tempFile==0 ){
59968 pPager->changeCountDone = 0;
59969 pPager->eState = PAGER_OPEN;
59971 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
59973 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
59974 pPager->errCode = SQLITE_OK;
59978 pPager->journalOff = 0;
59979 pPager->journalHdr = 0;
59980 pPager->setSuper = 0;
59987 ** the error-code about to be returned by a pager API function. The
59991 ** IOERR sub-codes, the pager enters the ERROR state and the error code
59995 ** The ERROR state indicates that the contents of the pager-cache
59997 ** the contents of the pager-cache. If a transaction was active when
60000 ** it were a hot-journal).
60006 pPager->errCode==SQLITE_FULL ||
60007 pPager->errCode==SQLITE_OK ||
60008 (pPager->errCode & 0xff)==SQLITE_IOERR
60011 pPager->errCode = rc;
60012 pPager->eState = PAGER_ERROR;
60028 ** * For non-TEMP databases, always sync to disk. This is necessary
60037 if( pPager->tempFile==0 ) return 1;
60039 if( !isOpen(pPager->fd) ) return 0;
60040 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
60046 ** after rollback of a hot-journal, or if an error occurs while opening
60047 ** the journal file or writing the very first journal-header of a
60052 ** exclusive than a RESERVED lock, it is a no-op.
60058 ** transaction. Nor will it be considered to be a hot-journal by this
60061 ** the current journal-mode (Pager.journalMode value), as follows:
60065 ** in-memory journal.
60084 ** If running in non-exclusive rollback mode, the lock on the file is
60102 ** is no write-transaction active but a RESERVED or greater lock is
60105 ** 1. After a successful hot-journal rollback, it is called with
60110 ** read-transaction, this function is called with eState==PAGER_READER
60111 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
60114 assert( pPager->eState!=PAGER_ERROR );
60115 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
60120 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
60121 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
60123 if( isOpen(pPager->jfd) ){
60127 if( sqlite3JournalIsInMemory(pPager->jfd) ){
60128 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
60129 sqlite3OsClose(pPager->jfd);
60130 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
60131 if( pPager->journalOff==0 ){
60134 rc = sqlite3OsTruncate(pPager->jfd, 0);
60135 if( rc==SQLITE_OK && pPager->fullSync ){
60141 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
60144 pPager->journalOff = 0;
60145 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
60146 || (pPager->exclusiveMode && pPager->journalMode<PAGER_JOURNALMODE_WAL)
60148 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
60149 pPager->journalOff = 0;
60152 ** a hot-journal was just rolled back. In this case the journal
60154 ** the database file, it will do so using an in-memory journal.
60156 int bDelete = !pPager->tempFile;
60157 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
60158 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
60159 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
60160 || pPager->journalMode==PAGER_JOURNALMODE_WAL
60162 sqlite3OsClose(pPager->jfd);
60164 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
60170 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
60171 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
60174 p->pageHash = 0;
60180 sqlite3BitvecDestroy(pPager->pInJournal);
60181 pPager->pInJournal = 0;
60182 pPager->nRec = 0;
60185 sqlite3PcacheCleanAll(pPager->pPCache);
60187 sqlite3PcacheClearWritable(pPager->pPCache);
60189 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
60193 /* Drop the WAL write-lock, if any. Also, if the connection was in
60197 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
60199 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
60200 /* This branch is taken when committing a transaction in rollback-journal
60206 assert( pPager->eLock==EXCLUSIVE_LOCK );
60207 rc = pager_truncate(pPager, pPager->dbSize);
60211 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
60215 if( !pPager->exclusiveMode
60216 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
60220 pPager->eState = PAGER_READER;
60221 pPager->setSuper = 0;
60235 ** call to pager_unlock() will discard all in-memory pages, unlock
60237 ** means that there is a hot-journal left in the file-system, the next
60247 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
60249 if( pPager->eState>=PAGER_WRITER_LOCKED ){
60253 }else if( !pPager->exclusiveMode ){
60254 assert( pPager->eState==PAGER_READER );
60257 }else if( pPager->eState==PAGER_ERROR
60258 && pPager->journalMode==PAGER_JOURNALMODE_MEMORY
60259 && isOpen(pPager->jfd)
60261 /* Special case for a ROLLBACK due to I/O error with an in-memory
60264 int errCode = pPager->errCode;
60265 u8 eLock = pPager->eLock;
60266 pPager->eState = PAGER_OPEN;
60267 pPager->errCode = SQLITE_OK;
60268 pPager->eLock = EXCLUSIVE_LOCK;
60270 pPager->errCode = errCode;
60271 pPager->eLock = eLock;
60277 ** Parameter aData must point to a buffer of pPager->pageSize bytes
60279 ** page of data and the current value of pPager->cksumInit.
60282 ** random initial value (pPager->cksumInit) and every 200th byte
60283 ** of the page data, starting with byte offset (pPager->pageSize%200).
60284 ** Each byte is interpreted as an 8-bit unsigned integer.
60296 u32 cksum = pPager->cksumInit; /* Checksum value to return */
60297 int i = pPager->pageSize-200; /* Loop counter */
60300 i -= 200;
60307 ** from the sub-journal (if isMainJrnl==0) and playback that page.
60311 ** The main rollback journal uses checksums - the statement journal does
60314 ** If the page number of the page record read from the (sub-)journal file
60324 ** If the page record is successfully read from the (sub-)journal file
60326 ** while reading the record from the (sub-)journal file or while writing
60328 ** is successfully read from the (sub-)journal file but appears to be
60332 ** * If the record page-number is illegal (0 or PAGER_SJ_PGNO), or
60346 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
60359 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
60360 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
60362 aData = pPager->pTmpSpace;
60368 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
60369 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
60370 ** only reads from the main journal, not the sub-journal.
60372 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
60373 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
60375 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
60377 /* Read the page number and page data from the journal or sub-journal
60380 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
60383 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
60385 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
60396 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
60400 rc = read32bits(jfd, (*pOffset)-4, &cksum);
60416 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
60417 pPager->nReserve = ((u8*)aData)[20];
60424 ** An exception to the above rule: If the database is in no-sync mode
60434 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
60451 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
60461 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
60463 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
60464 (isMainJrnl?"main-journal":"sub-journal")
60467 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
60469 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
60471 if( isOpen(pPager->fd)
60472 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
60475 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
60476 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
60480 ** This is usually safe even for an encrypted database - as the data
60482 ** is if the data was just read from an in-memory sub-journal. In that
60485 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
60487 if( pgno>pPager->dbFileSize ){
60488 pPager->dbFileSize = pgno;
60490 if( pPager->pBackup ){
60491 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
60495 ** the database and the page is not in-memory, there is a potential
60496 ** problem. When the page is next fetched by the b-tree layer, it
60502 ** if the page is on the free-list at the start of the transaction, then
60505 ** The solution is to add an in-memory page to the cache containing
60506 ** the data just read from the sub-journal. Mark the page as dirty
60507 ** and if the pager requires a journal-sync, then mark the page as
60508 ** requiring a journal-sync before it is written.
60511 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
60512 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
60514 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
60515 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
60527 pData = pPg->pData;
60528 memcpy(pData, (u8*)aData, pPager->pageSize);
60529 pPager->xReiniter(pPg);
60539 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
60547 ** Parameter zSuper is the name of a super-journal file. A single journal
60548 ** file that referred to the super-journal file has just been rolled back.
60549 ** This routine checks if it is possible to delete the super-journal file,
60555 ** When a super-journal file is created, it is populated with the names
60556 ** of all of its child journals, one after another, formatted as utf-8
60558 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
60561 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
60563 ** A super-journal file may only be deleted once all of its child
60566 ** This function reads the contents of the super-journal file into
60571 ** * if the child journal contains a reference to super-journal
60577 ** the file-system using sqlite3OsDelete().
60585 ** the entire contents of the super-journal file. This could be
60586 ** a couple of kilobytes or so - potentially larger than the page
60590 sqlite3_vfs *pVfs = pPager->pVfs;
60592 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
60593 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
60594 char *zSuperJournal = 0; /* Contents of super-journal file */
60595 i64 nSuperJournal; /* Size of super-journal file */
60597 char *zSuperPtr; /* Space to hold super-journal filename */
60602 ** If successful, open the super-journal file for reading.
60604 pSuper = (sqlite3_file *)sqlite3MallocZero(2 * (i64)pVfs->szOsFile);
60611 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
60615 /* Load the entire super-journal file into space obtained from
60617 ** sufficient space (in zSuperPtr) to hold the names of super-journal
60618 ** files extracted from regular rollback-journals.
60622 nSuperPtr = 1 + (i64)pVfs->mxPathname;
60640 while( (zJournal-zSuperJournal)<nSuperJournal ){
60647 /* One of the journals pointed to by the super-journal exists.
60648 ** Open it and check if it points at the super-journal. If
60649 ** so, return without deleting the super-journal file.
60669 /* We have a match. Do not delete the super-journal file. */
60692 ** file in the file-system. This only happens when committing a transaction,
60693 ** or rolling back a transaction (including rolling back a hot-journal).
60696 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
60697 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
60712 assert( pPager->eState!=PAGER_ERROR );
60713 assert( pPager->eState!=PAGER_READER );
60717 if( isOpen(pPager->fd)
60718 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
60721 int szPage = pPager->pageSize;
60722 assert( pPager->eLock==EXCLUSIVE_LOCK );
60724 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
60728 rc = sqlite3OsTruncate(pPager->fd, newSize);
60730 char *pTmp = pPager->pTmpSpace;
60732 testcase( (newSize-szPage) == currentSize );
60733 testcase( (newSize-szPage) > currentSize );
60734 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &newSize);
60735 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
60738 pPager->dbFileSize = nPage;
60746 ** Return a sanitized version of the sector-size of OS file pFile. The
60765 ** super-journal pointers within created journal files.
60769 ** Otherwise, for non-temporary files, the effective sector size is
60776 ** pPager->sectorSize is to define the "blast radius" of bytes that
60784 assert( isOpen(pPager->fd) || pPager->tempFile );
60786 if( pPager->tempFile
60787 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
60793 pPager->sectorSize = 512;
60795 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
60806 ** (2) 4 byte big-endian integer which is the number of valid page records
60809 ** (3) 4 byte big-endian integer which is the initial value for the
60813 ** (5) 4 byte big-endian integer which is the sector size. The header
60815 ** (6) 4 byte big-endian integer which is the page size.
60819 ** + pPager->pageSize bytes of data.
60836 ** no-sync option for the journal. A power failure could lead to corruption
60840 ** If the file opened as the journal file is not a well-formed
60846 ** If an I/O or malloc() error occurs, the journal-file is not deleted
60857 sqlite3_vfs *pVfs = pPager->pVfs;
60864 char *zSuper = 0; /* Name of super-journal file if any */
60867 u32 savedPageSize = pPager->pageSize;
60872 assert( isOpen(pPager->jfd) );
60873 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
60878 /* Read the super-journal name from the journal, if it is present.
60879 ** If a super-journal file name is specified, but the file is not
60885 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
60889 zSuper = pPager->pTmpSpace;
60890 rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
60898 pPager->journalOff = 0;
60920 ** working in no-sync mode. This means that the rest of the journal
60925 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
60926 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
60944 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
60945 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
60951 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
60956 pPager->dbSize = mxPg;
60957 if( pPager->mxPgno<mxPg ){
60958 pPager->mxPgno = mxPg;
60970 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
60975 pPager->journalOff = szJ;
61001 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
61005 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
61009 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
61013 ** malloc error that occurred after the change-counter was updated but
61014 ** before the transaction was committed, then the change-counter
61017 ** update the change-counter at all. This may lead to cache inconsistency
61021 pPager->changeCountDone = pPager->tempFile;
61024 /* Leave 4 bytes of space before the super-journal filename in memory.
61028 zSuper = &pPager->pTmpSpace[4];
61029 rc = readSuperJournal(pPager->jfd, zSuper, 1+(i64)pPager->pVfs->mxPathname);
61033 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
61042 /* If there was a super-journal and this routine will return success,
61043 ** see if it is possible to delete the super-journal.
61045 assert( zSuper==&pPager->pTmpSpace[4] );
61046 memset(pPager->pTmpSpace, 0, 4);
61052 nPlayback, pPager->zJournal);
61067 ** pPg->pData. A shared lock or greater must be held on the database
61077 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
61081 u32 iFrame = 0; /* Frame of WAL containing pgno */
61083 assert( pPager->eState>=PAGER_READER && !MEMDB );
61084 assert( isOpen(pPager->fd) );
61087 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
61091 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
61095 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
61096 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
61102 if( pPg->pgno==1 ){
61109 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
61116 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
61118 u8 *dbFileVers = &((u8*)pPg->pData)[24];
61119 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
61123 PAGER_INCR(pPager->nRead);
61124 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
61126 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
61132 ** Update the value of the change-counter at offsets 24 and 92 in
61136 ** routine which only updates the change-counter if the update is actually
61137 ** needed, as determined by the pPager->changeCountDone state variable.
61144 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
61145 put32bits(((char*)pPg->pData)+24, change_counter);
61150 put32bits(((char*)pPg->pData)+92, change_counter);
61151 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
61180 pPager->xReiniter(pPg);
61194 sqlite3BackupRestart(pPager->pBackup);
61213 pPager->dbSize = pPager->dbOrigSize;
61214 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
61215 pList = sqlite3PcacheDirtyList(pPager->pPCache);
61217 PgHdr *pNext = pList->pDirty;
61218 rc = pagerUndoCallback((void *)pPager, pList->pgno);
61244 assert( pPager->pWal );
61248 for(p=pList; p && p->pDirty; p=p->pDirty){
61249 assert( p->pgno < p->pDirty->pgno );
61253 assert( pList->pDirty==0 || isCommit );
61261 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
61262 if( p->pgno<=nTruncate ){
61263 ppNext = &p->pDirty;
61271 pPager->aStat[PAGER_STAT_WRITE] += nList;
61273 if( pList->pgno==1 ) pager_write_changecounter(pList);
61274 rc = sqlite3WalFrames(pPager->pWal,
61275 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
61277 if( rc==SQLITE_OK && pPager->pBackup ){
61278 for(p=pList; p; p=p->pDirty){
61279 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
61284 pList = sqlite3PcacheDirtyList(pPager->pPCache);
61285 for(p=pList; p; p=p->pDirty){
61306 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
61313 sqlite3WalEndReadTransaction(pPager->pWal);
61315 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
61318 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
61337 /* Query the WAL sub-system for the database size. The WalDbsize()
61340 ** available from the WAL sub-system if the log file is empty or
61343 assert( pPager->eState==PAGER_OPEN );
61344 assert( pPager->eLock>=SHARED_LOCK );
61345 assert( isOpen(pPager->fd) );
61346 assert( pPager->tempFile==0 );
61347 nPage = sqlite3WalDbsize(pPager->pWal);
61350 ** WAL sub-system, determine the page count based on the size of
61352 ** integer multiple of the page-size, round up the result.
61354 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
61356 int rc = sqlite3OsFileSize(pPager->fd, &n);
61360 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
61367 if( nPage>pPager->mxPgno ){
61368 pPager->mxPgno = (Pgno)nPage;
61377 ** Check if the *-wal file that corresponds to the database opened by pPager
61378 ** exists if the database is not empty, or verify that the *-wal file does
61381 ** If the database is not empty and the *-wal file exists, open the pager
61382 ** in WAL mode. If the database is empty or if no *-wal file exists and
61390 ** a WAL on a none-empty database, this ensures there is no race condition
61396 assert( pPager->eState==PAGER_OPEN );
61397 assert( pPager->eLock>=SHARED_LOCK );
61399 if( !pPager->tempFile ){
61402 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
61411 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
61413 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
61416 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
61417 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
61427 ** the entire super-journal file. The case pSavepoint==NULL occurs when
61431 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
61444 ** * Pages are then played back from the sub-journal file, starting
61458 ** (or transaction). No page with a page-number greater than this value
61463 i64 iHdrOff; /* End of first segment of main-journal records */
61467 assert( pPager->eState!=PAGER_ERROR );
61468 assert( pPager->eState>=PAGER_WRITER_LOCKED );
61472 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
61481 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
61482 pPager->changeCountDone = pPager->tempFile;
61488 /* Use pPager->journalOff as the effective size of the main rollback
61491 ** past pPager->journalOff is off-limits to us.
61493 szJ = pPager->journalOff;
61499 ** greater than the current database size (pPager->dbSize) but those
61504 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
61505 pPager->journalOff = pSavepoint->iOffset;
61506 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
61507 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
61511 pPager->journalOff = 0;
61516 ** of the main journal file. Continue to skip out-of-range pages and
61519 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
61527 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
61532 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
61534 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
61536 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
61537 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
61541 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
61543 /* Finally, rollback pages from the sub-journal. Page that were
61545 ** will be skipped. Out-of-range pages are also skipped.
61549 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
61552 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
61554 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
61555 assert( offset==(i64)ii*(4+pPager->pageSize) );
61563 pPager->journalOff = szJ;
61570 ** Change the maximum number of in-memory pages that are allowed
61574 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
61578 ** Change the maximum number of in-memory pages that are allowed
61582 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
61590 sqlite3_file *fd = pPager->fd;
61591 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
61593 sz = pPager->szMmap;
61594 pPager->bUseFetch = (sz>0);
61596 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
61605 pPager->szMmap = szMmap;
61613 sqlite3PcacheShrink(pPager->pPCache);
61635 ** database (with some additional information - the nRec field
61636 ** of the journal header - being written in between the two
61646 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
61657 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
61672 if( pPager->tempFile ){
61673 pPager->noSync = 1;
61674 pPager->fullSync = 0;
61675 pPager->extraSync = 0;
61677 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
61678 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
61679 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
61681 if( pPager->noSync ){
61682 pPager->syncFlags = 0;
61684 pPager->syncFlags = SQLITE_SYNC_FULL;
61686 pPager->syncFlags = SQLITE_SYNC_NORMAL;
61688 pPager->walSyncFlags = (pPager->syncFlags<<2);
61689 if( pPager->fullSync ){
61690 pPager->walSyncFlags |= pPager->syncFlags;
61692 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
61693 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
61696 pPager->doNotSpill &= ~SPILLFLAG_OFF;
61698 pPager->doNotSpill |= SPILLFLAG_OFF;
61739 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
61747 ** The pager invokes the busy-handler if sqlite3OsLock() returns
61748 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
61752 ** (which occurs during hot-journal rollback). Summary:
61755 ** --------------------------------------------------------
61756 ** NO_LOCK -> SHARED_LOCK | Yes
61757 ** SHARED_LOCK -> RESERVED_LOCK | No
61758 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
61759 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
61761 ** If the busy-handler callback returns non-zero, the lock is
61767 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
61771 pPager->xBusyHandler = xBusyHandler;
61772 pPager->pBusyHandlerArg = pBusyHandlerArg;
61773 ap = (void **)&pPager->xBusyHandler;
61776 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
61784 ** is a no-op. The value returned is the error state error code (i.e.
61785 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
61794 ** * the database is either not an in-memory database or it is
61795 ** an in-memory database that currently consists of zero pages.
61819 ** is a no-op for that case anyhow.
61824 if( (pPager->memDb==0 || pPager->dbSize==0)
61825 && sqlite3PcacheRefCount(pPager->pPCache)==0
61826 && pageSize && pageSize!=(u32)pPager->pageSize
61831 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
61832 rc = sqlite3OsFileSize(pPager->fd, &nByte);
61835 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
61847 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
61850 sqlite3PageFree(pPager->pTmpSpace);
61851 pPager->pTmpSpace = pNew;
61852 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
61853 pPager->pageSize = pageSize;
61854 pPager->lckPgno = (Pgno)(PENDING_BYTE/pageSize) + 1;
61860 *pPageSize = pPager->pageSize;
61862 if( nReserve<0 ) nReserve = pPager->nReserve;
61864 pPager->nReserve = (i16)nReserve;
61879 return pPager->pTmpSpace;
61891 pPager->mxPgno = mxPage;
61893 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
61894 /* assert( pPager->mxPgno>=pPager->dbSize ); */
61898 return pPager->mxPgno;
61906 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
61915 sqlite3_io_error_pending = -1;
61942 assert( isOpen(pPager->fd) || pPager->tempFile );
61950 if( isOpen(pPager->fd) ){
61952 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
61961 ** This function may only be called when a read-transaction is open on
61964 ** However, if the file is between 1 and <page-size> bytes in size, then
61968 assert( pPager->eState>=PAGER_READER );
61969 assert( pPager->eState!=PAGER_WRITER_FINISHED );
61970 *pnPage = (int)pPager->dbSize;
61976 ** a similar or greater lock is already held, this function is a no-op
61991 /* Check that this is either a no-op (because the requested lock is
61992 ** already held), or one of the transitions that the busy-handler
61996 assert( (pPager->eLock>=locktype)
61997 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
61998 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
62003 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
62009 ** following is true for all dirty pages currently in the page-cache:
62015 ** be necessary to write the current content out to the sub-journal.
62024 ** sub-journal rolled back the content could not be restored and the
62030 Pager *pPager = pPg->pPager;
62031 assert( pPg->flags&PGHDR_DIRTY );
62032 if( pPg->pgno>pPager->dbSize ){ /* if (a) is false */
62033 Pgno pgno = pPg->pgno;
62035 for(i=0; i<pPg->pPager->nSavepoint; i++){
62036 PagerSavepoint *p = &pPager->aSavepoint[i];
62037 assert( p->nOrig<pgno || sqlite3BitvecTestNotNull(p->pInSavepoint,pgno) );
62042 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
62049 ** Truncate the in-memory database file image to nPage pages. This
62060 assert( pPager->dbSize>=nPage || CORRUPT_DB );
62061 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
62062 pPager->dbSize = nPage;
62077 ** This function is called before attempting a hot-journal rollback. It
62078 ** syncs the journal file to disk, then sets pPager->journalHdr to the
62082 ** Syncing a hot-journal to disk before attempting to roll it back ensures
62083 ** that if a power-failure occurs during the rollback, the process that
62092 if( !pPager->noSync ){
62093 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
62096 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
62120 if( pPager->pMmapFreelist ){
62121 *ppPage = p = pPager->pMmapFreelist;
62122 pPager->pMmapFreelist = p->pDirty;
62123 p->pDirty = 0;
62124 assert( pPager->nExtra>=8 );
62125 memset(p->pExtra, 0, 8);
62127 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
62129 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
62132 p->pExtra = (void *)&p[1];
62133 assert( EIGHT_BYTE_ALIGNMENT( p->pExtra ) );
62134 p->flags = PGHDR_MMAP;
62135 p->nRef = 1;
62136 p->pPager = pPager;
62139 assert( p->pExtra==(void *)&p[1] );
62140 assert( p->pPage==0 );
62141 assert( p->flags==PGHDR_MMAP );
62142 assert( p->pPager==pPager );
62143 assert( p->nRef==1 );
62145 p->pgno = pgno;
62146 p->pData = pData;
62147 pPager->nMmapOut++;
62158 Pager *pPager = pPg->pPager;
62159 pPager->nMmapOut--;
62160 pPg->pDirty = pPager->pMmapFreelist;
62161 pPager->pMmapFreelist = pPg;
62163 assert( pPager->fd->pMethods->iVersion>=3 );
62164 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
62173 for(p=pPager->pMmapFreelist; p; p=pNext){
62174 pNext = p->pDirty;
62181 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
62188 if( pPager->tempFile ) return SQLITE_OK;
62189 if( pPager->dbSize==0 ) return SQLITE_OK;
62190 assert( pPager->zFilename && pPager->zFilename[0] );
62191 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
62193 /* If the HAS_MOVED file-control is unimplemented, assume that the file
62219 u8 *pTmp = (u8*)pPager->pTmpSpace;
62225 /* pPager->errCode = 0; */
62226 pPager->exclusiveMode = 0;
62230 assert( db || pPager->pWal==0 );
62231 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
62236 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
62237 pPager->pWal = 0;
62252 ** back or finalize it. The next database user will have to do hot-journal
62255 if( isOpen(pPager->jfd) ){
62264 sqlite3OsClose(pPager->jfd);
62265 sqlite3OsClose(pPager->fd);
62267 sqlite3PcacheClose(pPager->pPCache);
62268 assert( !pPager->aSavepoint && !pPager->pInJournal );
62269 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
62280 return pPg->pgno;
62294 ** disk and can be restored in the event of a hot-journal rollback.
62296 ** If the Pager.noSync flag is set, then this function is a no-op.
62297 ** Otherwise, the actions required depend on the journal-mode and the
62298 ** device characteristics of the file-system, as follows:
62300 ** * If the journal file is an in-memory journal file, no action need
62306 ** been written following it. If the pager is operating in full-sync
62312 ** Or, in pseudo-code:
62314 ** if( NOT <in-memory journal> ){
62316 ** if( <full-sync mode> ) xSync(<journal file>);
62329 assert( pPager->eState==PAGER_WRITER_CACHEMOD
62330 || pPager->eState==PAGER_WRITER_DBMOD
62338 if( !pPager->noSync ){
62339 assert( !pPager->tempFile );
62340 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
62341 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
62342 assert( isOpen(pPager->jfd) );
62346 ** that wrote to this database was operating in persistent-journal
62349 ** file happens to be a journal-header (written as part of the
62350 ** previous connection's transaction), and a crash or power-failure
62354 ** hot-journal rollback following recovery. It may roll back all
62356 ** out-of-date data that follows it. Database corruption.
62372 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
62375 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
62378 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
62385 ** full-synchronous mode, sync the journal first. This ensures that
62395 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
62398 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
62401 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
62403 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
62410 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
62411 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
62416 pPager->journalHdr = pPager->journalOff;
62418 pPager->nRec = 0;
62423 pPager->journalHdr = pPager->journalOff;
62431 sqlite3PcacheClearSyncFlags(pPager->pPCache);
62432 pPager->eState = PAGER_WRITER_DBMOD;
62440 ** in-memory pages in the list to the database file. The argument may
62442 ** a no-op.
62449 ** If the pager is a temp-file pager and the actual file-system file
62474 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
62475 assert( pPager->eLock==EXCLUSIVE_LOCK );
62476 assert( isOpen(pPager->fd) || pList->pDirty==0 );
62478 /* If the file is a temp-file has not yet been opened, open it now. It
62480 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
62482 if( !isOpen(pPager->fd) ){
62483 assert( pPager->tempFile && rc==SQLITE_OK );
62484 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
62490 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
62492 && pPager->dbHintSize<pPager->dbSize
62493 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
62495 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
62496 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
62497 pPager->dbHintSize = pPager->dbSize;
62501 Pgno pgno = pList->pgno;
62505 ** make the file smaller (presumably by auto-vacuum code). Do not write
62511 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
62512 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
62515 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
62516 if( pList->pgno==1 ) pager_write_changecounter(pList);
62518 pData = pList->pData;
62521 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
62528 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
62530 if( pgno>pPager->dbFileSize ){
62531 pPager->dbFileSize = pgno;
62533 pPager->aStat[PAGER_STAT_WRITE]++;
62536 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
62546 pList = pList->pDirty;
62553 ** Ensure that the sub-journal file is open. If it is already open, this
62554 ** function is a no-op.
62562 if( !isOpen(pPager->sjfd) ){
62567 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
62568 nStmtSpill = -1;
62570 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
62576 ** Append a record of the current state of page pPg to the sub-journal.
62578 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
62582 ** error code if the attempt to write to the sub-journal fails, or
62588 Pager *pPager = pPg->pPager;
62589 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
62591 /* Open the sub-journal, if it has not already been opened */
62592 assert( pPager->useJournal );
62593 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
62594 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
62597 || pPg->pgno>pPager->dbOrigSize
62601 /* If the sub-journal was opened successfully (or was already open),
62604 void *pData = pPg->pData;
62605 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
62608 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
62609 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
62611 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
62616 pPager->nSubRec++;
62617 assert( pPager->nSavepoint>0 );
62618 rc = addToSavepointBitvecs(pPager, pPg->pgno);
62633 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
62653 assert( pPg->pPager==pPager );
62654 assert( pPg->flags&PGHDR_DIRTY );
62672 if( NEVER(pPager->errCode) ) return SQLITE_OK;
62673 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
62674 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
62675 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
62676 if( pPager->doNotSpill
62677 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
62678 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
62683 pPager->aStat[PAGER_STAT_SPILL]++;
62684 pPg->pDirty = 0;
62686 /* Write a single frame for this page to the log. */
62694 if( pPager->tempFile==0 ){
62695 rc = sqlite3JournalCreate(pPager->jfd);
62701 if( pPg->flags&PGHDR_NEED_SYNC
62702 || pPager->eState==PAGER_WRITER_CACHEMOD
62709 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
62716 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
62727 int rc = pPager->errCode;
62729 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
62732 PgHdr *pNext = pList->pDirty;
62733 if( pList->nRef==0 ){
62749 ** If zFilename is NULL then a randomly-named temporary file is created
62753 ** This can be used to implement an in-memory database.
62779 int nExtra, /* Extra bytes append to each in-memory page */
62787 int tempFile = 0; /* True for temp files (incl. in-memory files) */
62788 int memDb = 0; /* True if this is an in-memory file */
62790 int readOnly = 0; /* True if this is a read-only file */
62801 /* Figure out how much space is required for each journal file-handle
62802 ** (there are two of them, the main journal and the sub-journal). */
62821 ** to by zPathname, length nPathname. Or, if this is a temporary file,
62826 nPathname = pVfs->mxPathname + 1;
62848 nUriByte = (int)(&z[1] - zUri);
62850 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
62852 ** the database being opened will be more than pVfs->mxPathname
62853 ** bytes in length. This means the database cannot be opened,
62855 ** check for a hot-journal before reading.
62871 ** Database file handle (pVfs->szOsFile bytes)
62872 ** Sub-journal file handle (journalFileSize bytes)
62882 ** Some 3rd-party software, over which we have no control, depends on
62886 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
62889 ** filename format expected by 3rd-party software should be as follows:
62891 ** - Main Database Path
62892 ** - \0
62893 ** - Multiple URI components consisting of:
62894 ** - Key
62895 ** - \0
62896 ** - Value
62897 ** - \0
62898 ** - \0
62899 ** - Journal Path
62900 ** - \0
62901 ** - WAL Path (zWALName)
62902 ** - \0
62913 ROUND8(pVfs->szOsFile) + /* The main db file */
62931 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
62932 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
62933 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62934 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
62935 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
62940 pPager->zFilename = (char*)pPtr;
62953 pPager->zJournal = (char*)pPtr;
62955 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
62957 sqlite3FileSuffix3(zFilename,pPager->zJournal);
62958 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
62961 pPager->zJournal = 0;
62967 pPager->zWal = (char*)pPtr;
62969 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
62971 sqlite3FileSuffix3(zFilename, pPager->zWal);
62972 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
62975 pPager->zWal = 0;
62981 pPager->pVfs = pVfs;
62982 pPager->vfsFlags = vfsFlags;
62988 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
62990 pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
63002 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
63006 if( szPageDflt<pPager->sectorSize ){
63007 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
63010 szPageDflt = (u32)pPager->sectorSize;
63027 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
63029 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
63039 ** This branch is also run for an in-memory database. An in-memory
63040 ** database is the same as a temp-file that is never written out to
63041 ** disk and uses an in-memory rollback journal.
63047 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
63048 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
63049 pPager->noLock = 1; /* Do no locking */
63057 assert( pPager->memDb==0 );
63058 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
63067 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
63073 sqlite3OsClose(pPager->fd);
63074 sqlite3PageFree(pPager->pTmpSpace);
63079 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
63080 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
63082 pPager->useJournal = (u8)useJournal;
63083 /* pPager->stmtOpen = 0; */
63084 /* pPager->stmtInUse = 0; */
63085 /* pPager->nRef = 0; */
63086 /* pPager->stmtSize = 0; */
63087 /* pPager->stmtJSize = 0; */
63088 /* pPager->nPage = 0; */
63089 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
63090 /* pPager->state = PAGER_UNLOCK; */
63091 /* pPager->errMask = 0; */
63092 pPager->tempFile = (u8)tempFile;
63096 pPager->exclusiveMode = (u8)tempFile;
63097 pPager->changeCountDone = pPager->tempFile;
63098 pPager->memDb = (u8)memDb;
63099 pPager->readOnly = (u8)readOnly;
63100 assert( useJournal || pPager->tempFile );
63102 /* pPager->pFirst = 0; */
63103 /* pPager->pFirstSynced = 0; */
63104 /* pPager->pLast = 0; */
63105 pPager->nExtra = (u16)nExtra;
63106 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
63107 assert( isOpen(pPager->fd) || tempFile );
63110 pPager->journalMode = PAGER_JOURNALMODE_OFF;
63112 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
63114 /* pPager->xBusyHandler = 0; */
63115 /* pPager->pBusyHandlerArg = 0; */
63116 pPager->xReiniter = xReinit;
63118 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
63119 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
63133 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
63134 zName--;
63136 p = zName - 4 - sizeof(Pager*);
63139 return pPager->fd;
63146 ** the file-system for the given pager. A hot journal is one that
63147 ** needs to be played back. According to this function, a hot-journal
63161 ** This routine does not check if there is a super-journal filename
63162 ** at the end of the file. If there is, and that super-journal file
63164 ** case this routine will return a false-positive. The pager_playback()
63168 ** If a hot-journal file is found to exist, *pExists is set to 1 and
63169 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
63171 ** to determine whether or not a hot-journal file exists, the IO error
63175 sqlite3_vfs * const pVfs = pPager->pVfs;
63178 int jrnlOpen = !!isOpen(pPager->jfd);
63180 assert( pPager->useJournal );
63181 assert( isOpen(pPager->fd) );
63182 assert( pPager->eState==PAGER_OPEN );
63184 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
63190 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
63200 ** in fact there is none. This results in a false-positive which will
63203 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
63207 assert( pPager->tempFile==0 );
63221 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
63222 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
63228 ** at least one non-zero bytes at the start of the journal file.
63234 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
63238 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
63243 sqlite3OsClose(pPager->jfd);
63270 ** has been successfully called. If a shared-lock is already held when
63271 ** this function is called, it is a no-op.
63278 ** the SHARED lock, the file-system is checked for a hot-journal,
63279 ** which is played back if present. Following any hot-journal
63281 ** the 'change-counter' field of the database file header and
63284 ** 2) If the pager is running in exclusive-mode, and there are currently
63291 ** occurs while locking the database, checking for a hot-journal file or
63297 /* This routine is only called from b-tree and only when there are no
63301 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
63303 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
63304 assert( pPager->errCode==SQLITE_OK );
63306 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
63307 int bHotJournal = 1; /* True if there exists a hot journal-file */
63310 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
63314 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
63321 if( pPager->eLock<=SHARED_LOCK ){
63328 if( pPager->readOnly ){
63338 ** hot-journal back.
63355 ** in exclusive-access mode the file descriptor will be kept open
63356 ** and possibly used for a transaction later on. Also, write-access
63363 ** may mean that the pager was in the error-state when this
63366 if( !isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
63367 sqlite3_vfs * const pVfs = pPager->pVfs;
63370 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
63374 assert( !pPager->tempFile );
63375 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
63376 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
63379 sqlite3OsClose(pPager->jfd);
63386 ** playing back the hot-journal so that we don't end up with
63392 if( isOpen(pPager->jfd) ){
63396 rc = pager_playback(pPager, !pPager->tempFile);
63397 pPager->eState = PAGER_OPEN;
63399 }else if( !pPager->exclusiveMode ){
63405 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
63424 assert( pPager->eState==PAGER_OPEN );
63425 assert( (pPager->eLock==SHARED_LOCK)
63426 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
63430 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
63431 /* The shared-lock has just been acquired then check to
63435 ** single unnecessary sqlite3OsRead() call at the start-up.
63439 ** a 32-bit counter that is incremented with each change. The
63447 char dbFileVers[sizeof(pPager->dbFileVers)];
63450 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
63458 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
63468 sqlite3OsUnfetch(pPager->fd, 0, 0);
63473 /* If there is a WAL file in the file-system, open this database in WAL
63474 ** mode. Otherwise, the following function call is a no-op.
63478 assert( pPager->pWal==0 || rc==SQLITE_OK );
63487 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
63488 rc = pagerPagecount(pPager, &pPager->dbSize);
63495 assert( pPager->eState==PAGER_OPEN );
63497 pPager->eState = PAGER_READER;
63498 pPager->hasHeldSharedLock = 1;
63509 ** nothing to rollback, so this routine is a no-op.
63512 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
63513 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
63526 ** getPageNormal() -- The normal getter
63527 ** getPageError() -- Used if the pager is in an error state
63528 ** getPageMmap() -- Used if memory-mapped I/O is enabled
63550 ** a) When reading a free-list leaf page from the database, and
63568 ** to find a page in the in-memory cache first. If the page is not already
63570 ** just returns 0. This routine acquires a read-lock the first time it
63586 assert( pPager->errCode==SQLITE_OK );
63587 assert( pPager->eState>=PAGER_READER );
63589 assert( pPager->hasHeldSharedLock==1 );
63592 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
63595 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
63602 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
63604 assert( pPg->pgno==pgno );
63605 assert( pPg->pPager==pPager || pPg->pPager==0 );
63608 if( pPg->pPager && !noContent ){
63612 pPager->aStat[PAGER_STAT_HIT]++;
63627 pPg->pPager = pPager;
63629 assert( !isOpen(pPager->fd) || !MEMDB );
63630 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
63631 if( pgno>pPager->mxPgno ){
63633 if( pgno<=pPager->dbSize ){
63640 /* Failure to set the bits in the InJournal bit-vectors is benign.
63647 if( pgno<=pPager->dbOrigSize ){
63648 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
63655 memset(pPg->pData, 0, pPager->pageSize);
63658 assert( pPg->pPager==pPager );
63659 pPager->aStat[PAGER_STAT_MISS]++;
63680 /* The page getter for when memory-mapped I/O is enabled */
63689 u32 iFrame = 0; /* Frame to read from WAL file */
63691 /* It is acceptable to use a read-only (mmap) page for any page except
63692 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
63694 ** temporary or in-memory database. */
63696 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
63708 assert( pPager->eState>=PAGER_READER );
63710 assert( pPager->hasHeldSharedLock==1 );
63711 assert( pPager->errCode==SQLITE_OK );
63714 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
63722 rc = sqlite3OsFetch(pPager->fd,
63723 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
63726 if( pPager->eState>PAGER_READER || pPager->tempFile ){
63732 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
63758 assert( pPager->errCode!=SQLITE_OK );
63760 return pPager->errCode;
63776 rc = pPager->xGet(pPager, pgno, ppPage, flags);
63783 /* Normal, high-speed version of sqlite3PagerGet() */
63784 return pPager->xGet(pPager, pgno, ppPage, flags);
63789 ** Acquire a page if it is already in the in-memory cache. Do
63803 assert( pPager->pPCache!=0 );
63804 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
63805 assert( pPage==0 || pPager->hasHeldSharedLock );
63807 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
63817 ** The assert() at tag-20230419-2 proves that this constraint is always
63825 TESTONLY( Pager *pPager = pPg->pPager; )
63827 if( pPg->flags & PGHDR_MMAP ){
63828 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
63834 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 ); /* tag-20230419-2 */
63842 assert( pPg->pgno==1 );
63843 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
63844 pPager = pPg->pPager;
63855 ** to the start of it. If there are active savepoints, open the sub-journal
63873 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
63875 assert( pPager->eState==PAGER_WRITER_LOCKED );
63877 assert( pPager->pInJournal==0 );
63879 /* If already in the error state, this function is a no-op. But on
63882 if( NEVER(pPager->errCode) ) return pPager->errCode;
63884 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
63885 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
63886 if( pPager->pInJournal==0 ){
63891 if( !isOpen(pPager->jfd) ){
63892 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
63893 sqlite3MemJournalOpen(pPager->jfd);
63898 if( pPager->tempFile ){
63912 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
63916 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
63921 ** the sub-journal if necessary.
63925 pPager->nRec = 0;
63926 pPager->journalOff = 0;
63927 pPager->setSuper = 0;
63928 pPager->journalHdr = 0;
63934 sqlite3BitvecDestroy(pPager->pInJournal);
63935 pPager->pInJournal = 0;
63936 pPager->journalOff = 0;
63938 assert( pPager->eState==PAGER_WRITER_LOCKED );
63939 pPager->eState = PAGER_WRITER_CACHEMOD;
63946 ** Begin a write-transaction on the specified pager object. If a
63947 ** write-transaction has already been opened, this function is a no-op.
63954 ** If the subjInMemory argument is non-zero, then any sub-journal opened
63955 ** within this transaction will be opened as an in-memory file. This
63956 ** has no effect if the sub-journal is already opened (as it may be when
63958 ** sub-journal. If the subjInMemory argument is zero, then any required
63959 ** sub-journal is implemented in-memory if pPager is an in-memory database,
63965 if( pPager->errCode ) return pPager->errCode;
63966 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
63967 pPager->subjInMemory = (u8)subjInMemory;
63969 if( pPager->eState==PAGER_READER ){
63970 assert( pPager->pInJournal==0 );
63976 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
63981 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
63986 ** The busy-handler is not invoked if another connection already
63987 ** holds the write-lock. If possible, the upper layer will call it.
63989 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
63993 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
64008 ** transactions may copy data from the sub-journal into the database
64012 pPager->eState = PAGER_WRITER_LOCKED;
64013 pPager->dbHintSize = pPager->dbSize;
64014 pPager->dbFileSize = pPager->dbSize;
64015 pPager->dbOrigSize = pPager->dbSize;
64016 pPager->journalOff = 0;
64019 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
64020 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
64032 Pager *pPager = pPg->pPager;
64036 i64 iOff = pPager->journalOff;
64041 assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) );
64043 assert( pPager->journalHdr<=pPager->journalOff );
64044 pData2 = pPg->pData;
64048 ** page in the block above, set the need-sync flag for the page.
64054 pPg->flags |= PGHDR_NEED_SYNC;
64056 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
64058 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
64060 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
64063 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
64064 pPager->journalOff, pPager->pageSize));
64067 PAGERID(pPager), pPg->pgno,
64068 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
64070 pPager->journalOff += 8 + pPager->pageSize;
64071 pPager->nRec++;
64072 assert( pPager->pInJournal!=0 );
64073 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
64076 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
64083 ** main journal or sub-journal as required. If the page is written into
64089 Pager *pPager = pPg->pPager;
64092 /* This routine is not called unless a write-transaction has already
64096 assert( pPager->eState==PAGER_WRITER_LOCKED
64097 || pPager->eState==PAGER_WRITER_CACHEMOD
64098 || pPager->eState==PAGER_WRITER_DBMOD
64101 assert( pPager->errCode==0 );
64102 assert( pPager->readOnly==0 );
64106 ** obtained the necessary locks to begin the write-transaction, but the
64114 if( pPager->eState==PAGER_WRITER_LOCKED ){
64118 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
64128 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
64129 if( pPager->pInJournal!=0
64130 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
64133 if( pPg->pgno<=pPager->dbOrigSize ){
64139 if( pPager->eState!=PAGER_WRITER_DBMOD ){
64140 pPg->flags |= PGHDR_NEED_SYNC;
64143 PAGERID(pPager), pPg->pgno,
64144 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
64148 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
64153 pPg->flags |= PGHDR_WRITEABLE;
64158 if( pPager->nSavepoint>0 ){
64163 if( pPager->dbSize<pPg->pgno ){
64164 pPager->dbSize = pPg->pgno;
64187 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
64188 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
64195 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
64196 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
64198 /* This trick assumes that both the page-size and sector-size are
64202 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
64204 nPageCount = pPager->dbSize;
64205 if( pPg->pgno>nPageCount ){
64206 nPage = (pPg->pgno - pg1)+1;
64207 }else if( (pg1+nPagePerSector-1)>nPageCount ){
64208 nPage = nPageCount+1-pg1;
64213 assert(pg1<=pPg->pgno);
64214 assert((pg1+nPage)>pPg->pgno);
64219 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
64224 if( pPage->flags&PGHDR_NEED_SYNC ){
64231 if( pPage->flags&PGHDR_NEED_SYNC ){
64249 pPage->flags |= PGHDR_NEED_SYNC;
64255 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
64256 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
64268 ** fit on a single disk sector. In this case all co-resident pages
64275 Pager *pPager = pPg->pPager;
64276 assert( (pPg->flags & PGHDR_MMAP)==0 );
64277 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64279 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
64280 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
64282 }else if( pPager->errCode ){
64283 return pPager->errCode;
64284 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
64285 assert( pPager->tempFile==0 );
64299 return pPg->flags & PGHDR_WRITEABLE;
64317 ** This optimization cannot be used with a temp-file, as the page may
64324 Pager *pPager = pPg->pPager;
64325 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
64326 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
64327 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
64328 pPg->flags |= PGHDR_DONT_WRITE;
64329 pPg->flags &= ~PGHDR_WRITEABLE;
64330 testcase( pPg->flags & PGHDR_NEED_SYNC );
64337 ** change-counter, stored as a 4-byte big-endian integer starting at
64341 ** But this only happens if the pPager->changeCountDone flag is false.
64351 ** The isDirectMode flag may only be non-zero if the library was compiled
64353 ** if isDirect is non-zero, then the database file is updated directly
64360 assert( pPager->eState==PAGER_WRITER_CACHEMOD
64361 || pPager->eState==PAGER_WRITER_DBMOD
64366 ** atomic-write optimization is enabled in this build, then isDirect
64370 ** The idea is that if the atomic-write optimization is not
64383 if( !pPager->changeCountDone && pPager->dbSize>0 ){
64386 assert( !pPager->tempFile && isOpen(pPager->fd) );
64393 ** operating in direct-mode, make page 1 writable. When not in
64395 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
64408 assert( pPager->dbFileSize>0 );
64409 zBuf = pPgHdr->pData;
64411 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
64412 pPager->aStat[PAGER_STAT_WRITE]++;
64415 /* Update the pager's copy of the change-counter. Otherwise, the
64417 ** flushed (as the change-counter values will not match). */
64419 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
64420 pPager->changeCountDone = 1;
64423 pPager->changeCountDone = 1;
64434 ** Sync the database file to disk. This is a no-op for in-memory databases
64437 ** If successful, or if called on a pager for which it is a no-op, this
64443 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
64445 if( rc==SQLITE_OK && !pPager->noSync ){
64447 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
64453 ** This function may only be called while a write-transaction is active in
64454 ** rollback. If the connection is in WAL mode, this call is a no-op.
64464 int rc = pPager->errCode;
64467 assert( pPager->eState==PAGER_WRITER_CACHEMOD
64468 || pPager->eState==PAGER_WRITER_DBMOD
64469 || pPager->eState==PAGER_WRITER_LOCKED
64481 ** of a super-journal file that should be written into the individual
64483 ** super-journal (a single database transaction).
64487 ** * The database file change-counter is updated,
64488 ** * the journal is synced (unless the atomic-write optimization is used),
64495 ** delete the super-journal file if specified).
64500 ** If the final parameter - noSync - is true, then the database file itself
64507 const char *zSuper, /* If not NULL, the super-journal name */
64512 assert( pPager->eState==PAGER_WRITER_LOCKED
64513 || pPager->eState==PAGER_WRITER_CACHEMOD
64514 || pPager->eState==PAGER_WRITER_DBMOD
64515 || pPager->eState==PAGER_ERROR
64520 if( NEVER(pPager->errCode) ) return pPager->errCode;
64526 pPager->zFilename, zSuper, pPager->dbSize));
64529 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
64531 assert( MEMDB==0 || pPager->tempFile );
64532 assert( isOpen(pPager->fd) || pPager->tempFile );
64534 /* If this is an in-memory db, or no pages have been written to, or this
64535 ** function has already been called, it is mostly a no-op. However, any
64537 sqlite3BackupRestart(pPager->pBackup);
64542 pList = sqlite3PcacheDirtyList(pPager->pPCache);
64545 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
64548 pList->pDirty = 0;
64552 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
64556 sqlite3PcacheCleanAll(pPager->pPCache);
64559 /* The bBatch boolean is true if the batch-atomic-write commit method
64560 ** should be used. No rollback journal is created if batch-atomic-write
64564 sqlite3_file *fd = pPager->fd;
64567 && !pPager->noSync
64568 && sqlite3JournalIsInMemory(pPager->jfd);
64574 /* The following block updates the change-counter. Exactly how it
64575 ** does this depends on whether or not the atomic-update optimization
64579 ** * The file-system supports the atomic-write property for
64580 ** blocks of size page-size, and
64581 ** * This commit is not part of a multi-file transaction, and
64586 ** counter in 'indirect-mode'. If the optimization is compiled in but
64589 ** pager_incr_changecounter() to update the change-counter in indirect
64593 ** then call pager_incr_changecounter() to update the change-counter
64599 assert( isOpen(pPager->jfd)
64600 || pPager->journalMode==PAGER_JOURNALMODE_OFF
64601 || pPager->journalMode==PAGER_JOURNALMODE_WAL
64603 if( !zSuper && isOpen(pPager->jfd)
64604 && pPager->journalOff==jrnlBufferSize(pPager)
64605 && pPager->dbSize>=pPager->dbOrigSize
64606 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
64608 /* Update the db file change counter via the direct-write method. The
64609 ** following call will modify the in-memory representation of page 1
64611 ** directly to the database file. Because of the atomic-write
64612 ** property of the host file-system, this is safe.
64616 rc = sqlite3JournalCreate(pPager->jfd);
64625 rc = sqlite3JournalCreate(pPager->jfd);
64634 /* Write the super-journal name into the journal file. If a
64635 ** super-journal file name has already been written to the journal file,
64636 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
64642 ** If the atomic-update optimization is being used, this sync will not
64645 ** Because the change-counter page was just modified, unless the
64646 ** atomic-update optimization is used it is almost certain that the
64650 ** xSync() call will be changed to a no-op by the OS anyhow.
64655 pList = sqlite3PcacheDirtyList(pPager->pPCache);
64661 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
64662 char *pTmp = pPager->pTmpSpace;
64663 int szPage = (int)pPager->pageSize;
64665 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
64666 ((i64)pPager->dbSize*pPager->pageSize)-szPage);
64677 rc = sqlite3JournalCreate(pPager->jfd);
64679 sqlite3OsClose(pPager->jfd);
64684 sqlite3OsClose(pPager->jfd);
64696 sqlite3PcacheCleanAll(pPager->pPCache);
64701 ** last page in the db image moved to the free-list. In this case the
64704 if( pPager->dbSize>pPager->dbFileSize ){
64705 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_SJ_PGNO(pPager));
64706 assert( pPager->eState==PAGER_WRITER_DBMOD );
64721 pPager->eState = PAGER_WRITER_FINISHED;
64730 ** synced to disk. The journal file still exists in the file-system
64732 ** be used as a hot-journal and the current transaction rolled back.
64736 ** for hot-journal rollback. Once this is done the transaction is
64748 if( NEVER(pPager->errCode) ) return pPager->errCode;
64749 pPager->iDataVersion++;
64751 assert( pPager->eState==PAGER_WRITER_LOCKED
64752 || pPager->eState==PAGER_WRITER_FINISHED
64753 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
64758 ** this transaction, the pager is running in exclusive-mode and is
64759 ** using persistent journals, then this function is a no-op.
64763 ** a hot-journal during hot-journal rollback, 0 changes will be made
64768 if( pPager->eState==PAGER_WRITER_LOCKED
64769 && pPager->exclusiveMode
64770 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
64772 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
64773 pPager->eState = PAGER_READER;
64778 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
64784 ** transaction are reverted and the current write-transaction is closed.
64794 ** in-memory cache pages to the state they were in when the transaction
64803 ** In WAL mode, all cache-entries containing data modified within the
64805 ** their pre-transaction state by re-reading data from the database or
64812 /* PagerRollback() is a no-op if called in READER or OPEN state. If
64817 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
64818 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
64822 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
64823 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
64825 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
64826 int eState = pPager->eState;
64833 pPager->errCode = SQLITE_ABORT;
64834 pPager->eState = PAGER_ERROR;
64842 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
64855 ** Return TRUE if the database file is opened read-only. Return FALSE
64859 return pPager->readOnly;
64867 return sqlite3PcacheRefCount(pPager->pPCache);
64876 int perPageSize = pPager->pageSize + pPager->nExtra
64878 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
64880 + pPager->pageSize;
64896 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
64897 a[1] = sqlite3PcachePagecount(pPager->pPCache);
64898 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
64899 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
64900 a[4] = pPager->eState;
64901 a[5] = pPager->errCode;
64902 a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
64903 a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
64904 a[8] = 0; /* Used to be pPager->nOvfl */
64905 a[9] = pPager->nRead;
64906 a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
64919 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
64935 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
64936 *pnVal += pPager->aStat[eStat];
64938 pPager->aStat[eStat] = 0;
64943 ** Return true if this is an in-memory or temp-file backed pager.
64946 return pPager->tempFile || pPager->memVfs;
64953 ** equal to nSavepoint, then this function is a no-op.
64956 ** occurs while opening the sub-journal file, then an IO error code is
64961 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
64965 assert( pPager->eState>=PAGER_WRITER_LOCKED );
64967 assert( nSavepoint>nCurrent && pPager->useJournal );
64974 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
64979 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
64980 pPager->aSavepoint = aNew;
64984 aNew[ii].nOrig = pPager->dbSize;
64985 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
64986 aNew[ii].iOffset = pPager->journalOff;
64990 aNew[ii].iSubRec = pPager->nSubRec;
64991 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
64997 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
64999 pPager->nSavepoint = ii+1;
65001 assert( pPager->nSavepoint==nSavepoint );
65006 assert( pPager->eState>=PAGER_WRITER_LOCKED );
65009 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
65029 ** (the first created). A value of (Pager.nSavepoint-1) means operate
65031 ** (Pager.nSavepoint-1), then this function is a no-op.
65048 int rc = pPager->errCode;
65057 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
65066 for(ii=nNew; ii<pPager->nSavepoint; ii++){
65067 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
65069 pPager->nSavepoint = nNew;
65071 /* Truncate the sub-journal so that it only includes the parts
65074 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
65075 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
65076 /* Only truncate if it is an in-memory sub-journal. */
65077 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
65078 i64 sz = (pPager->pageSize+4)*(i64)pRel->iSubRec;
65079 rc = sqlite3OsTruncate(pPager->sjfd, sz);
65082 pPager->nSubRec = pRel->iSubRec;
65086 ** If this is a temp-file, it is possible that the journal file has
65090 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
65091 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
65102 pPager->journalMode==PAGER_JOURNALMODE_OFF
65103 && pPager->eState>=PAGER_WRITER_CACHEMOD
65105 pPager->errCode = SQLITE_ABORT;
65106 pPager->eState = PAGER_ERROR;
65118 ** Except, if the pager is in-memory only, then return an empty string if
65122 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
65123 ** participate in shared-cache.
65130 if( nullIfMemDb && (pPager->memDb || sqlite3IsMemdb(pPager->pVfs)) ){
65133 return pPager->zFilename;
65141 return pPager->pVfs;
65150 return pPager->fd;
65159 return pPager->jfd;
65161 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
65169 return pPager->zJournal;
65182 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
65190 ** If the fourth argument, isCommit, is non-zero, then this page is being
65200 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
65204 assert( pPg->nRef>0 );
65205 assert( pPager->eState==PAGER_WRITER_CACHEMOD
65206 || pPager->eState==PAGER_WRITER_DBMOD
65210 /* In order to be able to rollback, an in-memory database must journal
65213 assert( pPager->tempFile || !MEMDB );
65214 if( pPager->tempFile ){
65221 ** sub-journal now. This is required to handle the following scenario:
65229 ** If page X were not written to the sub-journal here, it would not
65233 ** subjournalPage() may need to allocate space to store pPg->pgno into
65237 if( (pPg->flags & PGHDR_DIRTY)!=0
65244 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
65245 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
65247 /* If the journal needs to be sync()ed before page pPg->pgno can
65248 ** be written to, store pPg->pgno in local variable needSyncPgno.
65251 ** the journal needs to be sync()ed before database page pPg->pgno
65254 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
65255 needSyncPgno = pPg->pgno;
65256 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
65257 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
65258 assert( pPg->flags&PGHDR_DIRTY );
65261 /* If the cache contains a page with page-number pgno, remove it
65266 pPg->flags &= ~PGHDR_NEED_SYNC;
65268 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
65270 if( NEVER(pPgOld->nRef>1) ){
65274 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
65275 if( pPager->tempFile ){
65276 /* Do not discard pages from an in-memory database since we might
65278 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
65284 origPgno = pPg->pgno;
65288 /* For an in-memory database, make sure the original page continues
65292 if( pPager->tempFile && pPgOld ){
65298 /* If needSyncPgno is non-zero, then the journal file needs to be
65300 ** Currently, no such page exists in the page-cache and the
65302 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
65305 ** If the attempt to load the page into the page-cache fails, (due
65315 if( needSyncPgno<=pPager->dbOrigSize ){
65316 assert( pPager->pTmpSpace!=0 );
65317 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
65321 pPgHdr->flags |= PGHDR_NEED_SYNC;
65337 assert( pPg->pgno!=iNew );
65338 pPg->flags = flags;
65346 assert( pPg->nRef>0 || pPg->pPager->memDb );
65347 return pPg->pData;
65355 return pPg->pExtra;
65359 ** Get/set the locking-mode for this pager. Parameter eMode must be one
65362 ** the locking-mode is set to the value specified.
65366 ** locking-mode.
65374 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
65375 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
65376 pPager->exclusiveMode = (u8)eMode;
65378 return (int)pPager->exclusiveMode;
65382 ** Set the journal-mode for this pager. Parameter eMode must be one of:
65394 ** * An in-memory database can only have its journal_mode set to _OFF
65399 ** The returned indicate the current (possibly updated) journal-mode.
65402 u8 eOld = pPager->journalMode; /* Prior journalmode */
65416 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
65418 /* Do allow the journalmode of an in-memory database to be set to
65431 assert( pPager->eState!=PAGER_ERROR );
65432 pPager->journalMode = (u8)eMode;
65445 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
65446 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
65455 sqlite3OsClose(pPager->jfd);
65456 if( pPager->eLock>=RESERVED_LOCK ){
65457 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
65460 int state = pPager->eState;
65465 if( pPager->eState==PAGER_READER ){
65470 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
65477 assert( state==pPager->eState );
65480 sqlite3OsClose(pPager->jfd);
65485 return (int)pPager->journalMode;
65492 return (int)pPager->journalMode;
65502 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
65503 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
65508 ** Get/set the size-limit used for persistent journal files.
65510 ** Setting the size limit to -1 means no limit is enforced.
65511 ** An attempt to set a limit smaller than -1 is a no-op.
65514 if( iLimit>=-1 ){
65515 pPager->journalSizeLimit = iLimit;
65516 sqlite3WalLimit(pPager->pWal, iLimit);
65518 return pPager->journalSizeLimit;
65522 ** Return a pointer to the pPager->pBackup variable. The backup module
65528 return &pPager->pBackup;
65533 ** Unless this is an in-memory or temporary database, clear the pager cache.
65536 assert( MEMDB==0 || pPager->tempFile );
65537 if( pPager->tempFile==0 ) pager_reset(pPager);
65558 if( pPager->pWal==0 && pPager->journalMode==PAGER_JOURNALMODE_WAL ){
65570 if( pPager->pWal ){
65571 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
65572 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
65573 pPager->pBusyHandlerArg,
65574 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
65582 return sqlite3WalCallback(pPager->pWal);
65587 ** primitives necessary for write-ahead logging.
65590 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
65591 if( pPager->noLock ) return 0;
65592 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
65603 assert( pPager->eLock>=SHARED_LOCK );
65604 eOrigLock = pPager->eLock;
65617 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
65618 ** lock on the database file and use heap-memory to store the wal-index
65619 ** in. Otherwise, use the normal shared-memory.
65624 assert( pPager->pWal==0 && pPager->tempFile==0 );
65625 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
65627 /* If the pager is already in exclusive-mode, the WAL module will use
65628 ** heap-memory for the wal-index instead of the VFS shared-memory
65632 if( pPager->exclusiveMode ){
65640 rc = sqlite3WalOpen(pPager->pVfs,
65641 pPager->fd, pPager->zWal, pPager->exclusiveMode,
65642 pPager->journalSizeLimit, &pPager->pWal
65646 sqlite3WalDb(pPager->pWal, pPager->dbWal);
65661 ** file (not a temp file or an in-memory database), and the WAL file
65667 ** If the pager is open on a temp-file (or in-memory database), or if
65673 int *pbOpen /* OUT: Set to true if call is a no-op */
65678 assert( pPager->eState==PAGER_OPEN || pbOpen );
65679 assert( pPager->eState==PAGER_READER || !pbOpen );
65681 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
65683 if( !pPager->tempFile && !pPager->pWal ){
65687 sqlite3OsClose(pPager->jfd);
65691 pPager->journalMode = PAGER_JOURNALMODE_WAL;
65692 pPager->eState = PAGER_OPEN;
65713 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
65715 /* If the log file is not already open, but does exist in the file-system,
65719 if( !pPager->pWal ){
65724 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
65733 ** the database file, the log and log-summary files will be deleted.
65735 if( rc==SQLITE_OK && pPager->pWal ){
65738 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
65739 pPager->pageSize, (u8*)pPager->pTmpSpace);
65740 pPager->pWal = 0;
65742 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
65750 ** If pager pPager is a wal-mode database not in exclusive locking mode,
65757 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
65758 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
65768 pPager->dbWal = db;
65770 sqlite3WalDb(pPager->pWal, db);
65782 if( pPager->pWal ){
65783 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
65798 if( pPager->pWal ){
65799 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
65812 if( pPager->pWal ){
65813 rc = sqlite3WalSnapshotRecover(pPager->pWal);
65834 if( pPager->pWal ){
65835 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
65847 assert( pPager->pWal );
65848 sqlite3WalSnapshotUnlock(pPager->pWal);
65856 ** A read-lock must be held on the pager when this function is called. If
65863 assert( pPager->eState>=PAGER_READER );
65864 return sqlite3WalFramesize(pPager->pWal);
65870 return sqlite3WalSystemErrno(pPager->pWal);
65890 ** This file contains the implementation of a write-ahead log (WAL) used in
65893 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
65896 ** Each frame records the revised content of a single page from the
65898 ** frames into the WAL. Transactions commit when a frame is written that
65907 ** toward the end. Checksums and counters attached to each frame are
65912 ** big-endian 32-bit unsigned integer values:
65918 ** 16: Salt-1, random integer incremented with each checkpoint
65919 ** 20: Salt-2, a different random integer changing with each ckpt
65920 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
65921 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
65923 ** Immediately following the wal-header are zero or more frames. Each
65924 ** frame consists of a 24-byte frame-header followed by <page-size> bytes
65925 ** of page data. The frame-header is six big-endian 32-bit unsigned
65931 ** 8: Salt-1 (copied from the header)
65932 ** 12: Salt-2 (copied from the header)
65933 ** 16: Checksum-1.
65934 ** 20: Checksum-2.
65936 ** A frame is considered valid if and only if the following conditions are
65939 ** (1) The salt-1 and salt-2 values in the frame-header match
65940 ** salt values in the wal-header
65942 ** (2) The checksum values in the final 8 bytes of the frame-header
65945 ** up to and including the current frame.
65947 ** The checksum is computed using 32-bit big-endian integers if the
65949 ** is computed using little-endian if the magic number is 0x377f0682.
65950 ** The checksum values are always stored in the frame header in a
65951 ** big-endian format regardless of which byte order is used to compute
65953 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
65956 ** for i from 0 to n-1 step 2:
65963 ** of the sequence being summed.) The s1 value spans all 32-bit
65966 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
65967 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
65968 ** The VFS.xSync operations serve as write barriers - all writes launched
65972 ** After each checkpoint, the salt-1 value is incremented and the salt-2
65981 ** last valid instance of page P that is a followed by a commit frame
65982 ** or is a commit frame itself becomes the value read. If the WAL
65984 ** frame or are followed by a commit frame, then page P is read from
65988 ** valid frame in the WAL. The reader uses this recorded "mxFrame" value
66001 ** data structure called the wal-index is maintained to expedite the
66004 ** WAL-INDEX FORMAT
66006 ** Conceptually, the wal-index is shared memory, though VFS implementations
66007 ** might choose to implement the wal-index using a mmapped file. Because
66008 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
66012 ** In the default unix and windows implementation, the wal-index is a mmapped
66013 ** file whose name is the database name with a "-shm" suffix added. For that
66014 ** reason, the wal-index is sometimes called the "shm" file.
66016 ** The wal-index is transient. After a crash, the wal-index can (and should
66018 ** to either truncate or zero the header of the wal-index when the last
66019 ** connection to it closes. Because the wal-index is transient, it can
66020 ** use an architecture-specific format; it does not have to be cross-platform.
66022 ** as big endian, the wal-index can store multi-byte values in the native
66025 ** The purpose of the wal-index is to answer this question quickly: Given
66026 ** a page number P and a maximum frame index M, return the index of the
66027 ** last frame in the wal before frame M for page P in the WAL, or return
66030 ** The wal-index consists of a header region, followed by an one or
66033 ** The wal-index header contains the total number of frames within the WAL
66039 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
66041 ** wal-index. The values are:
66046 ** Each index block contains two sections, a page-mapping that contains the
66047 ** database page number associated with each wal frame, and a hash-table
66049 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
66050 ** for the first index block) 32-bit page numbers. The first entry in the
66051 ** first index-block contains the database page number corresponding to the
66052 ** first frame in the WAL file. The first entry in the second index block
66053 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
66056 ** The last index block in a wal-index usually contains less than the full
66057 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
66059 ** allocated size of the page-mapping array - the page-mapping array merely
66062 ** Even without using the hash table, the last frame for page P
66063 ** can be found by scanning the page-mapping sections of each index block
66066 ** beginning. The first entry that equals P corresponds to the frame
66069 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
66074 ** 1-based index of an entry in the mapping section of the same
66075 ** index block. Let K be the 1-based index of the largest entry in
66090 ** wrap-around.) Because the hash table is never more than half full,
66095 ** current index block. Otherwise the iMax-th mapping entry of the
66106 ** comparisons (on average) suffice to either locate a frame in the
66107 ** WAL or to establish that the frame does not exist in the WAL. This
66113 ** and to the wal-index) might be using a different value K1, where K1>K0.
66119 ** in the first place - which is what reader one wants. Meanwhile, the
66142 ** The maximum (and only) versions of the wal and wal-index formats
66146 ** values in the wal-header are correct and (b) the version field is not
66149 ** Similarly, if a client successfully reads a wal-index header (i.e. the
66151 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
66166 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
66176 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
66186 ** The following object holds a copy of the wal-index header content.
66188 ** The actual header in the wal-index consists of two copies of this
66195 ** Or it can be 1 to represent a 65536-byte page. The latter case was
66199 u32 iVersion; /* Wal-index version */
66203 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
66205 u32 mxFrame; /* Index of last valid frame in the WAL */
66207 u32 aFrameCksum[2]; /* Checksum of last frame in log */
66213 ** A copy of the following object occurs in the wal-index immediately
66236 ** holds read-lock K, then the value in aReadMark[K] is no greater than
66239 ** a special case; its value is never used and it exists as a place-holder
66250 ** the frame numbers are less than or equal to every aReadMark[] that is
66266 ** writing new content beginning at frame 1.
66268 ** We assume that 32-bit loads are atomic and so no locks are needed in
66281 ** This is a schematic view of the complete 136-byte header of the
66282 ** wal-index file (also known as the -shm file):
66284 ** +-----------------------------+
66286 ** +-----------------------------+ |
66288 ** +-----------------------------+ |
66290 ** +-------+-------+-------------+ |
66292 ** +-------+-------+-------------+ |
66294 ** +-----------------------------+ | WalIndexHdr object
66296 ** +-----------------------------+ |
66299 ** +-----------------------------+ |
66302 ** +-----------------------------+ |
66305 ** +-----------------------------+
66307 ** +-----------------------------+ |
66309 ** +-----------------------------+ |
66311 ** +-------+-------+-------------+ |
66313 ** +-------+-------+-------------+ | Second copy of the
66315 ** +-----------------------------+ |
66317 ** +-----------------------------+ |
66320 ** +-----------------------------+ |
66323 ** +-----------------------------+ |
66326 ** +-----------------------------+
66328 ** +-----------------------------+
66334 ** +-------+-------+------+------+
66336 ** +-------+-------+------+------+ ) 8 lock bytes
66338 ** +-------+-------+------+------+
66340 ** +-----------------------------+
66342 ** +-----------------------------+
66347 ** only support mandatory file-locks, we do not read or write data
66353 /* Size of header before each frame in wal */
66360 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
66361 ** big-endian format in the first 4 bytes of a WAL file.
66363 ** If the LSB is set, then the checksums for each frame within the WAL
66364 ** file are calculated by treating all data as an array of 32-bit
66365 ** big-endian words. Otherwise, they are calculated by interpreting
66366 ** all data as 32-bit little-endian words.
66371 ** Return the offset of frame iFrame in the write-ahead log file,
66373 ** is to the start of the write-ahead log frame-header.
66376 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
66380 ** An open write-ahead log file is represented by an instance of the
66396 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
66398 i16 readLock; /* Which read lock is being held. -1 for none */
66400 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
66407 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
66408 WalIndexHdr hdr; /* Wal-index header for current transaction */
66412 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
66448 ** Each page of the wal-index mapping contains a hash-table made up of
66457 ** frame most recently written to the WAL (in other words, the frame with
66462 ** walIteratorInit() - Create a new iterator,
66463 ** walIteratorNext() - Step an iterator,
66464 ** walIteratorFree() - Free an iterator.
66476 int iZero; /* Frame number associated with aPgno[0] */
66477 } aSegment[FLEXARRAY]; /* One for every 32KB page in the wal-index */
66485 ** Define the parameters of the hash tables in the wal-index file. There
66486 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
66487 ** wal-index.
66489 ** Changing any of these constants will alter the wal-index format and
66497 ** The block of page numbers associated with the first hash-table in a
66498 ** wal-index is smaller than usual. This is so that there is a complete
66499 ** hash-table on each aligned 32KB page of the wal-index.
66501 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
66503 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
66509 ** Structured Exception Handling (SEH) is a Windows-specific technique
66510 ** for catching exceptions raised while accessing memory-mapped files.
66512 ** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
66513 ** deal with system-level errors that arise during WAL -shm file processing.
66514 ** Without this compile-time option, any system-level faults that appear
66515 ** while accessing the memory-mapped -shm file will cause a process-wide
66524 assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
66525 VVA_ONLY(pWal->nSehTry++);
66529 VVA_ONLY(pWal->nSehTry--); \
66530 assert( pWal->nSehTry==0 ); \
66533 /* Simulate a memory-mapping fault in the -shm file for testing purposes */
66539 ** indicates that the exception may have been caused by accessing the *-shm
66543 VVA_ONLY(pWal->nSehTry--);
66545 if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
66547 ** ExceptionInformation[] array is a read-write flag - 0 if the exception
66551 pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
66561 ** thrown by the system if the *-shm file mapping is accessed after it
66566 assert( pWal->nSehTry>0 );
66592 assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
66595 ** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
66604 #define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
66607 # define SEH_TRY VVA_ONLY(pWal->nSehTry++);
66608 # define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
66609 # define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
66616 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
66617 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
66620 ** If the wal-index is currently smaller the iPage pages then the size
66621 ** of the wal-index might be increased, but only if it is safe to do
66622 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
66623 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
66627 ** (1) rc==SQLITE_OK and *ppPage==Requested-Wal-Index-Page
66631 ** Scenario (3) can only occur when pWal->writeLock is false and iPage==0
66640 /* Enlarge the pWal->apWiData[] array if required */
66641 if( pWal->nWiData<=iPage ){
66644 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
66649 memset((void*)&apNew[pWal->nWiData], 0,
66650 sizeof(u32*)*(iPage+1-pWal->nWiData));
66651 pWal->apWiData = apNew;
66652 pWal->nWiData = iPage+1;
66656 assert( pWal->apWiData[iPage]==0 );
66657 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
66658 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
66659 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
66661 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
66662 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
66664 assert( pWal->apWiData[iPage]!=0
66666 || (pWal->writeLock==0 && iPage==0) );
66667 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
66671 pWal->readOnly |= WAL_SHM_RDONLY;
66678 *ppPage = pWal->apWiData[iPage];
66688 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
66695 ** Return a pointer to the WalCkptInfo structure in the wal-index.
66698 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66700 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
66704 ** Return a pointer to the WalIndexHdr structure in the wal-index.
66707 assert( pWal->nWiData>0 && pWal->apWiData[0] );
66709 return (volatile WalIndexHdr*)pWal->apWiData[0];
66713 ** The argument to this macro must be of type u32. On a little-endian
66715 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
66717 ** of the input value as a little-endian integer.
66734 int nativeCksum, /* True for native byte-order, false for non-native */
66796 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
66797 sqlite3OsShmBarrier(pWal->pDbFd);
66802 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
66804 ** might give false-positive TSAN warnings.
66806 ** See tag-20200519-1.
66815 ** Write the header information in pWal->hdr into the wal-index.
66817 ** The checksum on pWal->hdr is updated before it is written.
66823 assert( pWal->writeLock );
66824 pWal->hdr.isInit = 1;
66825 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
66826 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
66827 /* Possible TSAN false-positive. See tag-20200519-1 */
66828 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
66830 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
66834 ** This function encodes a single frame header and writes it to a buffer
66835 ** supplied by the caller. A frame-header is made up of a series of
66836 ** 4-byte big-endian integers, as follows:
66841 ** 8: Salt-1 (copied from the wal-header)
66842 ** 12: Salt-2 (copied from the wal-header)
66843 ** 16: Checksum-1.
66844 ** 20: Checksum-2.
66847 Wal *pWal, /* The write-ahead log */
66848 u32 iPage, /* Database page number for frame */
66849 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
66851 u8 *aFrame /* OUT: Write encoded frame here */
66853 int nativeCksum; /* True for native byte-order checksums */
66854 u32 *aCksum = pWal->hdr.aFrameCksum;
66858 if( pWal->iReCksum==0 ){
66859 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
66861 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
66863 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
66873 ** Check to see if the frame with header in aFrame[] and content
66874 ** in aData[] is valid. If it is a valid frame, fill *piPage and
66875 ** *pnTruncate and return true. Return if the frame is not valid.
66878 Wal *pWal, /* The write-ahead log */
66879 u32 *piPage, /* OUT: Database page number for frame */
66882 u8 *aFrame /* Frame data */
66884 int nativeCksum; /* True for native byte-order checksums */
66885 u32 *aCksum = pWal->hdr.aFrameCksum;
66886 u32 pgno; /* Page number of the frame */
66889 /* A frame is only valid if the salt values in the frame-header
66890 ** match the salt values in the wal-header.
66892 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
66896 /* A frame is only valid if the page number is greater than zero.
66903 /* A frame is only valid if a checksum of the WAL header,
66904 ** all prior frames, the first 16 bytes of this frame-header,
66905 ** and the frame-data matches the checksum in the last 8
66906 ** bytes of this frame-header.
66908 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
66910 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
66918 /* If we reach this point, the frame is valid. Return the page number
66934 return "WRITE-LOCK";
66936 return "CKPT-LOCK";
66938 return "RECOVER-LOCK";
66941 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
66942 lockIdx-WAL_READ_LOCK(0));
66951 ** A lock cannot be moved directly between shared and exclusive - it must go
66954 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
66958 if( pWal->exclusiveMode ) return SQLITE_OK;
66959 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66961 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
66963 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66965 if( rc==SQLITE_OK ) pWal->lockMask |= (1 << lockIdx);
66970 if( pWal->exclusiveMode ) return;
66971 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
66974 pWal->lockMask &= ~(1 << lockIdx);
66976 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
66980 if( pWal->exclusiveMode ) return SQLITE_OK;
66981 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66983 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
66985 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
66988 pWal->lockMask |= (((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
66994 if( pWal->exclusiveMode ) return;
66995 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
66998 pWal->lockMask &= ~(((1<<n)-1) << (SQLITE_SHM_NLOCK+lockIdx));
67000 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
67006 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
67011 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
67012 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
67015 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
67020 ** of a page hash table in the wal-index. This becomes the return value
67025 volatile ht_slot *aHash; /* Start of the wal-index hash table */
67026 volatile u32 *aPgno; /* aPgno[1] is the page of first frame indexed */
67027 u32 iZero; /* One less than the frame number of first indexed*/
67032 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
67035 ** Set output variable pLoc->aHash to point to the start of the hash table
67036 ** in the wal-index file. Set pLoc->iZero to one less than the frame
67037 ** number of the first frame indexed by this hash table. If a
67038 ** slot in the hash table is set to N, it refers to frame number
67039 ** (pLoc->iZero+N) in the log.
67041 ** Finally, set pLoc->aPgno so that pLoc->aPgno[0] is the page number of the
67042 ** first frame indexed by the hash table, frame (pLoc->iZero).
67051 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
67054 if( pLoc->aPgno ){
67055 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
67057 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
67058 pLoc->iZero = 0;
67060 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
67069 ** Return the number of the wal-index page that contains the hash-table
67070 ** and page-number array that contain entries corresponding to WAL frame
67071 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
67075 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
67087 ** Return the page number associated with frame iFrame in this WAL.
67093 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
67095 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
67100 ** than pWal->hdr.mxFrame.
67102 ** This function is called whenever pWal->hdr.mxFrame is decreased due
67105 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
67107 ** pWal->hdr.mxFrame advances to the point where those hash tables are
67116 assert( pWal->writeLock );
67117 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
67118 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
67119 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
67121 if( pWal->hdr.mxFrame==0 ) return;
67123 /* Obtain pointers to the hash-table and page-number array containing
67124 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
67125 ** that the page said hash-table and array reside on is already mapped.(1)
67127 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
67128 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
67129 i = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
67130 if( NEVER(i) ) return; /* Defense-in-depth, in case (1) above is wrong */
67132 /* Zero all hash-table entries that correspond to frame numbers greater
67133 ** than pWal->hdr.mxFrame.
67135 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
67144 ** frame numbers greater than pWal->hdr.mxFrame.
67146 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit]);
67169 ** Set an entry in the wal-index that will map database page number
67170 ** pPage into WAL frame iFrame.
67174 WalHashLoc sLoc; /* Wal-index hash table location */
67178 /* Assuming the wal-index file was successfully mapped, populate the
67183 int idx; /* Value to write to hash-table slot */
67186 idx = iFrame - sLoc.iZero;
67189 /* If this is the first entry to be added to this hash-table, zero the
67193 int nByte = (int)((u8*)&sLoc.aHash[HASHTABLE_NSLOT] - (u8*)sLoc.aPgno);
67202 ** the hash-table before writing any new entries.
67204 if( sLoc.aPgno[idx-1] ){
67206 assert( !sLoc.aPgno[idx-1] );
67209 /* Write the aPgno[] array entry and the hash-table slot. */
67212 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
67214 sLoc.aPgno[idx-1] = iPage;
67230 ** thing to check, so only do this occasionally - not on every
67252 ** Recover the wal-index by reading the write-ahead log file.
67255 ** wal-index to prevent other threads/processes from doing anything
67256 ** with the WAL or wal-index while recovery is running. The
67273 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
67276 assert( pWal->writeLock );
67277 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
67278 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
67285 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
67287 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
67294 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
67295 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
67301 int isValid; /* True if this frame is valid */
67302 u32 iPg; /* Current 32KB wal-index page */
67303 u32 iLastFrame; /* Last frame in wal, based on nSize alone */
67306 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
67319 || szPage&(szPage-1)
67325 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
67326 pWal->szPage = szPage;
67327 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
67328 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
67331 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
67332 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
67334 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
67335 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
67360 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
67363 u32 iFrame; /* Index of last frame read */
67365 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
67371 pWal->apWiData[iPg] = aPrivate;
67375 u32 pgno; /* Database page number for frame */
67376 u32 nTruncate; /* dbsize field from frame header */
67378 /* Read and decode the next log frame. */
67379 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
67386 /* If nTruncate is non-zero, this is a commit record. */
67388 pWal->hdr.mxFrame = iFrame;
67389 pWal->hdr.nPage = nTruncate;
67390 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
67393 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
67394 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
67397 pWal->apWiData[iPg] = aShare;
67410 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
67441 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
67442 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
67445 /* Reset the checkpoint-header. This is safe because this thread is
67447 ** checkpointers. Then set the values of read-mark slots 1 through N.
67450 pInfo->nBackfill = 0;
67451 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
67452 pInfo->aReadMark[0] = 0;
67456 if( i==1 && pWal->hdr.mxFrame ){
67457 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
67459 pInfo->aReadMark[i] = READMARK_NOT_USED;
67468 /* If more than one frame was recovered from the log file, report an
67473 if( pWal->hdr.nPage ){
67476 pWal->hdr.mxFrame, pWal->zWalName
67483 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
67488 ** Close an open wal-index.
67491 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
67493 for(i=0; i<pWal->nWiData; i++){
67494 sqlite3_free((void *)pWal->apWiData[i]);
67495 pWal->apWiData[i] = 0;
67498 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
67499 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
67510 ** client from unlinking the WAL or wal-index file. If another process
67519 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
67522 int bNoShm, /* True to run in heap-memory mode */
67534 ** of these constants would result in an incompatible on-disk format
67535 ** for the -shm file. Any change that causes one of these asserts to
67539 ** This table also serves as a helpful cross-reference when trying to
67540 ** interpret hex dumps of the -shm file.
67580 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
67585 pRet->pVfs = pVfs;
67586 pRet->pWalFd = (sqlite3_file *)&pRet[1];
67587 pRet->pDbFd = pDbFd;
67588 pRet->readLock = -1;
67589 pRet->mxWalSize = mxWalSize;
67590 pRet->zWalName = zWalName;
67591 pRet->syncHeader = 1;
67592 pRet->padToSectorBoundary = 1;
67593 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
67595 /* Open file handle on the write-ahead log file. */
67597 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
67599 pRet->readOnly = WAL_RDONLY;
67604 sqlite3OsClose(pRet->pWalFd);
67608 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
67610 pRet->padToSectorBoundary = 0;
67622 if( pWal ) pWal->mxWalSize = iLimit;
67628 ** same WalIterator object. Write into *piFrame the frame index where
67638 u32 *piFrame /* OUT: Wal frame index of next page */
67644 iMin = p->iPrior;
67646 for(i=p->nSegment-1; i>=0; i--){
67647 struct WalSegment *pSegment = &p->aSegment[i];
67648 while( pSegment->iNext<pSegment->nEntry ){
67649 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
67653 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
67657 pSegment->iNext++;
67661 *piPage = p->iPrior = iRet;
67689 const u32 *aContent, /* Pages in wal - keys for the sort */
67753 ht_slot *aList; /* Pointer to sub-list content */
67761 struct Sublist aSub[13]; /* Array of sub-lists */
67765 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
67774 assert( p->aList && p->nList<=(1<<iSub) );
67775 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
67776 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
67787 assert( p->nList<=(1<<iSub) );
67788 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
67789 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
67799 assert( aContent[aList[i]] > aContent[aList[i-1]] );
67814 ** pages in the WAL following frame nBackfill in ascending order. Frames
67815 ** nBackfill or earlier may be included - excluding them is an optimization
67828 u32 iLast; /* Last frame in log */
67831 ht_slot *aTmp; /* Temp space used by merge-sort */
67837 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
67838 iLast = pWal->hdr.mxFrame;
67851 p->nSegment = nSegment;
67864 nEntry = (int)(iLast - sLoc.iZero);
67866 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
67868 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
67875 p->aSegment[i].iZero = sLoc.iZero;
67876 p->aSegment[i].nEntry = nEntry;
67877 p->aSegment[i].aIndex = aIndex;
67878 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
67899 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&nMs
67907 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
67912 if( pWal->db ){
67913 int tmout = pWal->db->setlkTimeout;
67926 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
67940 assert( pWal->readLock<0 || bLock==0 );
67942 assert( pWal->db );
67946 pWal->writeLock = 1;
67950 }else if( pWal->writeLock ){
67952 pWal->writeLock = 0;
67961 pWal->db = db;
67975 ** busy-handler function. Invoke it and retry the lock until either the
67976 ** lock is successfully obtained or the busy-handler returns 0.
67999 ** The cache of the wal-index header must be valid to call this function.
68000 ** Return the page-size in bytes used by the database.
68003 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
68012 ** file - there are no readers that may attempt to read a frame from
68015 ** This function updates the shared-memory structures so that the next
68020 ** new wal-index header. It should be passed a pseudo-random value (i.e.
68026 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
68027 pWal->nCkpt++;
68028 pWal->hdr.mxFrame = 0;
68030 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
68032 AtomicStore(&pInfo->nBackfill, 0);
68033 pInfo->nBackfillAttempted = 0;
68034 pInfo->aReadMark[1] = 0;
68035 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
68036 assert( pInfo->aReadMark[0]==0 );
68048 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
68054 ** in the WAL and can be recovered following a power-loss or hard reset.
68061 ** This routine uses and updates the nBackfill field of the wal-index header.
68080 int szPage; /* Database page-size */
68083 u32 iFrame = 0; /* Wal frame containing data for iDbpage */
68084 u32 mxSafeFrame; /* Max frame that can be backfilled */
68093 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
68095 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
68099 /* Compute in mxSafeFrame the index of the last frame of the WAL that is
68104 mxSafeFrame = pWal->hdr.mxFrame;
68105 mxPage = pWal->hdr.nPage;
68107 u32 y = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
68109 assert( y<=pWal->hdr.mxFrame );
68113 AtomicStore(pInfo->aReadMark+i, iMark); SEH_INJECT_FAULT;
68125 if( pInfo->nBackfill<mxSafeFrame ){
68126 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
68133 u32 nBackfill = pInfo->nBackfill;
68134 pInfo->nBackfillAttempted = mxSafeFrame; SEH_INJECT_FAULT;
68137 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
68145 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
68146 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
68148 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
68151 ** maximum size of the pending-byte page (65536 bytes), then
68155 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
68166 if( AtomicLoad(&db->u1.isInterrupted) ){
68167 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
68175 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
68177 iOffset = (iDbpage-1)*(i64)szPage;
68179 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
68182 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
68186 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
68187 i64 szDb = pWal->hdr.nPage*(i64)szPage;
68189 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
68191 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
68195 AtomicStore(&pInfo->nBackfill, mxSafeFrame); SEH_INJECT_FAULT;
68216 assert( pWal->writeLock );
68218 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
68223 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
68224 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
68227 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
68233 ** wal-index header in shared memory, as all subsequent reader or
68237 ** the wal-index header do not match the contents of the
68238 ** file-system. To avoid this, update the wal-index header to
68241 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
68243 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
68262 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
68264 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
68268 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
68275 ** an exception thrown by reading from the *-shm mapping after it has become
68290 ** 3) Set pWal->apWiData[pWal->iWiPg] to pWal->pWiValue if not NULL
68295 if( pWal->exclusiveMode==0 ){
68300 if( pWal->writeLock==2 ) pWal->writeLock = 0;
68301 mUnlock = pWal->lockMask & ~(
68302 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
68303 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
68304 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
68311 sqlite3_free(pWal->pFree);
68312 pWal->pFree = 0;
68313 if( pWal->pWiValue ){
68314 pWal->apWiData[pWal->iWiPg] = pWal->pWiValue;
68315 pWal->pWiValue = 0;
68328 if( pWal->exclusiveMode==0 ){
68332 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
68333 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
68334 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
68336 | (pWal->pSnapshot ? (pWal->lockMask & (1 << WAL_CKPT_LOCK)) : 0)
68339 assert( mExpect==pWal->lockMask );
68351 iRet = pWal->iSysErrno;
68352 pWal->iSysErrno = 0;
68373 int isDelete = 0; /* True to unlink wal and wal-index files */
68378 ** ordinary, rollback-mode locking methods, this guarantees that the
68381 ** the wal and wal-index files.
68386 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
68388 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
68389 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
68395 int bPersist = -1;
68397 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
68401 ** fsynced (rc==SQLITE_OK) and if we are not in persistent-wal
68404 }else if( pWal->mxWalSize>=0 ){
68408 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
68417 sqlite3OsClose(pWal->pWalFd);
68420 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
68424 sqlite3_free((void *)pWal->apWiData);
68431 ** Try to read the wal-index header. Return 0 on success and 1 if
68434 ** The wal-index is in shared memory. Another thread or process might
68441 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
68444 ** If the checksum cannot be verified return non-zero. If the header
68452 /* The first page of the wal-index must be mapped at this point. */
68453 assert( pWal->nWiData>0 && pWal->apWiData[0] );
68458 ** from the file. If this happens, return non-zero.
68460 ** tag-20200519-1:
68461 ** There are two copies of the header at the beginning of the wal-index.
68465 ** give false-positive warnings about these accesses because the tools do not
68466 ** account for the double-read and the memory barrier. The use of mutexes
68472 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
68480 return 1; /* Malformed header - probably all zeros */
68482 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
68487 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
68489 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
68490 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
68491 testcase( pWal->szPage<=32768 );
68492 testcase( pWal->szPage>=65536 );
68503 #define WAL_RETRY (-1)
68506 ** Read the wal-index header from the wal-index and into pWal->hdr.
68507 ** If the wal-header appears to be corrupt, try to reconstruct the
68508 ** wal-index from the WAL before returning.
68510 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
68511 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
68514 ** If the wal-index header is successfully read, return SQLITE_OK.
68520 volatile u32 *page0; /* Chunk of wal-index containing header */
68522 /* Ensure that page 0 of the wal-index (the page that contains the
68523 ** wal-index header) is mapped. Return early if an error occurs here.
68530 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
68532 ** confirm that another write-capable connection has the shared-memory
68533 ** open, and hence the content of the shared-memory is unreliable,
68534 ** since the shared-memory might be inconsistent with the WAL file
68537 assert( pWal->writeLock==0 );
68538 assert( pWal->readOnly & WAL_SHM_RDONLY );
68539 pWal->bShmUnreliable = 1;
68540 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
68543 return rc; /* Any other non-OK return is just an error */
68546 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
68550 assert( page0!=0 || pWal->writeLock==0 );
68552 /* If the first page of the wal-index has been mapped, try to read the
68553 ** wal-index header immediately, without holding any lock. This usually
68554 ** works, but may fail if the wal-index header is corrupt or currently
68563 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
68569 int bWriteLock = pWal->writeLock;
68573 /* If the write-lock was just obtained, set writeLock to 2 instead of
68575 ** write-lock were held (so that it allocates new pages as required),
68576 ** and walHandleException() to unlock the write-lock if a SEH exception
68578 if( !bWriteLock ) pWal->writeLock = 2;
68582 /* If the wal-index header is still malformed even while holding
68592 pWal->writeLock = 0;
68600 ** sure the wal-index was not constructed with some future format that
68603 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
68606 if( pWal->bShmUnreliable ){
68609 pWal->bShmUnreliable = 0;
68610 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
68616 pWal->exclusiveMode = WAL_NORMAL_MODE;
68623 ** Open a transaction in a connection where the shared-memory is read-only
68624 ** and where we cannot verify that there is a separate write-capable connection
68625 ** on hand to keep the shared-memory up-to-date with the WAL file.
68627 ** This can happen, for example, when the shared-memory is implemented by
68628 ** memory-mapping a *-shm file, where a prior writer has shut down and
68629 ** left the *-shm file on disk, and now the present connection is trying
68630 ** to use that database but lacks write permission on the *-shm file.
68635 ** The *-wal file has been read and an appropriate wal-index has been
68636 ** constructed in pWal->apWiData[] using heap memory instead of shared
68643 ** WAL_RETRY, then the heap memory wal-index has been discarded and
68645 ** beginning (including attempting to map the *-shm file).
68653 u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */
68658 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
68660 assert( pWal->bShmUnreliable );
68661 assert( pWal->readOnly & WAL_SHM_RDONLY );
68662 assert( pWal->nWiData>0 && pWal->apWiData[0] );
68672 pWal->readLock = 0;
68674 /* Check to see if a separate writer has attached to the shared-memory area,
68675 ** thus making the shared-memory "reliable" again. Do this by invoking
68679 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
68680 ** cause the heap-memory WAL-index to be discarded and the actual
68692 ** even if some external agent does a "chmod" to make the shared-memory
68696 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
68697 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
68703 /* We reach this point only if the real shared-memory is still unreliable.
68704 ** Assume the in-memory WAL-index substitute is correct and load it
68705 ** into pWal->hdr.
68707 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
68712 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
68717 /* If the wal file is too small to contain a wal-header and the
68718 ** wal-index header has mxFrame==0, then it must be safe to proceed
68724 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
68729 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
68733 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
68735 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
68742 assert( (pWal->szPage & (pWal->szPage-1))==0 );
68743 assert( pWal->szPage>=512 && pWal->szPage<=65536 );
68744 szFrame = pWal->szPage + WAL_FRAME_HDRSIZE;
68753 ** wal file since the heap-memory wal-index was created. If so, the
68754 ** heap-memory wal-index is discarded and WAL_RETRY returned to
68756 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
68757 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
68758 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->szPage);
68762 u32 pgno; /* Database page number for frame */
68763 u32 nTruncate; /* dbsize field from frame header */
68765 /* Read and decode the next log frame. */
68766 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
68770 /* If nTruncate is non-zero, then a complete transaction has been
68778 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
68779 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
68785 for(i=0; i<pWal->nWiData; i++){
68786 sqlite3_free((void*)pWal->apWiData[i]);
68787 pWal->apWiData[i] = 0;
68789 pWal->bShmUnreliable = 0;
68809 ** it reaches WAL_RETRY_PROTOCOL_LIMIT - indicating that walTryBeginRead()
68810 ** has many times been invoked and failed with WAL_RETRY - walTryBeginRead()
68839 ** to make a copy of the wal-index header into pWal->hdr. If the
68840 ** wal-index header has changed, *pChanged is set to 1 (as an indication
68842 ** flushed.) When useWal==1, the wal-index header is assumed to already
68853 ** bad luck when there is lots of contention for the wal-index, but that
68857 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
68858 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
68860 ** access any database page that is modified by a WAL frame up to and
68861 ** including frame number aReadMark[pWal->readLock]. The reader will
68862 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
68863 ** Or if pWal->readLock==0, then the reader will ignore the WAL
68866 ** this routine will always set pWal->readLock>0 on success.
68868 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
68878 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
68884 assert( pWal->readLock<0 ); /* Not currently locked */
68887 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
68895 ** paged out or take a page-fault that is time-consuming to resolve,
68911 VVA_ONLY( pWal->lockError = 1; )
68914 if( *pCnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
68916 /* In SQLITE_ENABLE_SETLK_TIMEOUT builds, configure the file-descriptor
68921 ** read-mark.
68932 sqlite3OsSleep(pWal->pVfs, nDelay);
68938 if( pWal->bShmUnreliable==0 ){
68957 if( pWal->apWiData[0]==0 ){
68962 ** code that determines whether or not the shared-memory region
68977 else if( pWal->bShmUnreliable ){
68982 assert( pWal->nWiData>0 );
68983 assert( pWal->apWiData[0]!=0 );
68990 u32 mxFrame; /* Wal frame to lock to */
68991 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
68993 && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0)
69002 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr,sizeof(WalIndexHdr)) ){
69019 pWal->readLock = 0;
69029 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
69033 mxFrame = pWal->hdr.mxFrame;
69035 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
69036 mxFrame = pWal->pSnapshot->mxFrame;
69040 u32 thisMark = AtomicLoad(pInfo->aReadMark+i); SEH_INJECT_FAULT;
69047 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
69053 AtomicStore(pInfo->aReadMark+i,mxFrame);
69064 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
69082 /* Now that the read-lock has been obtained, check that neither the
69083 ** value in the aReadMark[] array or the contents of the wal-index
69086 ** It is necessary to check that the wal-index header did not change
69087 ** between the time it was read and when the shared-lock was obtained
69090 ** that occur later in the log than pWal->hdr.mxFrame may have been
69093 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
69096 ** Before checking that the live wal-index header has not changed
69097 ** since it was read, set Wal.minFrame to the first frame in the wal
69099 ** to read any frames earlier than minFrame from the wal file - they
69103 ** nBackfill and checking that the wal-header in shared-memory still
69104 ** matches the one cached in pWal->hdr, it is guaranteed that the
69105 ** checkpointer that set nBackfill was not working with a wal-index
69106 ** header newer than that cached in pWal->hdr. If it were, that could
69108 ** a version of page X that lies before pWal->minFrame (call that version
69111 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
69114 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
69116 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1; SEH_INJECT_FAULT;
69118 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
69119 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
69124 assert( mxReadMark<=pWal->hdr.mxFrame );
69125 pWal->readLock = (i16)mxI;
69137 void *pBuf1, /* Temp buffer pWal->szPage bytes in size */
69138 void *pBuf2 /* Temp buffer pWal->szPage bytes in size */
69140 int szPage = (int)pWal->szPage;
69144 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
69147 u32 i = pInfo->nBackfillAttempted;
69148 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
69156 assert( i - sLoc.iZero - 1 >=0 );
69157 pgno = sLoc.aPgno[i-sLoc.iZero-1];
69158 iDbOff = (i64)(pgno-1) * szPage;
69162 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
69165 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
69173 pInfo->nBackfillAttempted = i-1;
69185 ** file, if any. Set nBackfillAttempted to the frame number of the
69186 ** first frame for which the wal file content matches the db file.
69188 ** This is only really safe if the file-system is such that any page
69191 ** may be left set to a value larger than expected, if a wal frame
69202 assert( pWal->readLock>=0 );
69205 void *pBuf1 = sqlite3_malloc(pWal->szPage);
69206 void *pBuf2 = sqlite3_malloc(pWal->szPage);
69210 pWal->ckptLock = 1;
69215 pWal->ckptLock = 0;
69237 WalIndexHdr *pSnapshot = pWal->pSnapshot;
69240 assert( pWal->ckptLock==0 );
69241 assert( pWal->nSehTry>0 );
69245 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
69253 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
69256 ** before checking pInfo->nBackfillAttempted. */
69278 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
69280 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
69281 ** is populated with the wal-index header corresponding to the head
69289 ** pSnapshot->mxFrame into the database file. Note that the
69294 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
69295 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
69299 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
69300 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
69303 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
69304 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
69306 assert( pWal->readLock>0 );
69307 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
69313 /* A client using a non-current snapshot may not ignore any frames
69316 ** have omitted to checkpoint a frame earlier than minFrame in
69317 ** the file because there exists a frame after iSnapshot that
69319 pWal->minFrame = 1;
69340 ** it takes a snapshot of the state of the WAL and wal-index for the current
69342 ** Other threads might append new content to the WAL and wal-index but
69361 ** read-lock.
69365 assert( pWal->writeLock==0 || pWal->readLock<0 );
69367 if( pWal->readLock>=0 ){
69369 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
69370 pWal->readLock = -1;
69375 ** Search the wal file for page pgno. If found, set *piRead to the frame that
69385 u32 *piRead /* OUT: Frame number (or zero) */
69387 u32 iRead = 0; /* If !=0, WAL frame to return data from */
69388 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
69393 assert( pWal->readLock>=0 || pWal->lockError );
69395 /* If the "last page" field of the wal-index header snapshot is 0, then
69397 ** in this case as an optimization. Likewise, if pWal->readLock==0,
69401 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
69411 ** that adds entries to the wal-index (and possibly to this hash
69415 ** read transaction was opened may have been written incorrectly -
69422 ** if we had exclusive access to the hash-table:
69425 ** This condition filters out normal hash-table collisions.
69429 ** table after the current read-transaction had started.
69431 iMinHash = walFramePage(pWal->minFrame);
69432 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
69448 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH-1]==pgno ){
69452 if( (nCollide--)==0 ){
69463 ** of the wal-index file content. Make sure the results agree with the
69468 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
69469 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
69484 ** Search the wal file for page pgno. If found, set *piRead to the frame that
69497 u32 *piRead /* OUT: Frame number (or zero) */
69508 ** Read the contents of frame iRead from the wal file into buffer pOut
69514 u32 iRead, /* Frame to read */
69520 sz = pWal->hdr.szPage;
69526 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
69533 if( pWal && ALWAYS(pWal->readLock>=0) ){
69534 return pWal->hdr.nPage;
69557 /* If the write-lock is already held, then it was obtained before the
69558 ** read-transaction was even opened, making this call a no-op.
69560 if( pWal->writeLock ){
69561 assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) );
69568 assert( pWal->readLock>=0 );
69569 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
69571 if( pWal->readOnly ){
69582 pWal->writeLock = 1;
69589 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
69597 pWal->writeLock = 0;
69607 if( pWal->writeLock ){
69609 pWal->writeLock = 0;
69610 pWal->iReCksum = 0;
69611 pWal->truncateOnCommit = 0;
69618 ** function moves the write-pointer back to the start of the transaction.
69620 ** Additionally, the callback function is invoked for each frame written
69630 if( ALWAYS(pWal->writeLock) ){
69631 Pgno iMax = pWal->hdr.mxFrame;
69635 /* Restore the clients cache of the wal-index header to the state it
69638 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
69640 for(iFrame=pWal->hdr.mxFrame+1;
69646 ** (b) has an outstanding reference, then xUndo is either a no-op
69658 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
69661 pWal->iReCksum = 0;
69673 assert( pWal->writeLock );
69674 aWalData[0] = pWal->hdr.mxFrame;
69675 aWalData[1] = pWal->hdr.aFrameCksum[0];
69676 aWalData[2] = pWal->hdr.aFrameCksum[1];
69677 aWalData[3] = pWal->nCkpt;
69689 assert( pWal->writeLock );
69690 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
69692 if( aWalData[3]!=pWal->nCkpt ){
69693 /* This savepoint was opened immediately after the write-transaction
69698 aWalData[3] = pWal->nCkpt;
69701 if( aWalData[0]<pWal->hdr.mxFrame ){
69702 pWal->hdr.mxFrame = aWalData[0];
69703 pWal->hdr.aFrameCksum[0] = aWalData[1];
69704 pWal->hdr.aFrameCksum[1] = aWalData[2];
69709 if( pWal->iReCksum>pWal->hdr.mxFrame ){
69710 pWal->iReCksum = 0;
69722 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
69726 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
69733 if( pWal->readLock==0 ){
69735 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
69736 if( pInfo->nBackfill>0 ){
69739 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
69744 ** wal-index header to reflect this.
69747 ** at this point. But updating the actual wal-index header is also
69751 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
69757 pWal->readLock = -1;
69773 ** the next fsync should occur - passed from sqlite3WalFrames() into
69786 ** Do a sync when crossing the p->iSyncPoint boundary.
69799 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
69800 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
69801 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
69804 iAmt -= iFirstAmt;
69806 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
69807 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
69810 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
69815 ** Write out a single frame of the WAL
69818 WalWriter *p, /* Where to write the frame */
69819 PgHdr *pPage, /* The page of the frame to be written */
69825 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
69826 pData = pPage->pData;
69827 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
69831 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
69844 const int szPage = pWal->szPage;/* Database page size */
69847 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
69848 u32 iRead; /* Next frame to read from wal file */
69855 ** first checksum. If the first frame is frame 1 (implying that the current
69857 ** wal-file header. Otherwise, read them from the frame header of the
69858 ** previous frame. */
69859 assert( pWal->iReCksum>0 );
69860 if( pWal->iReCksum==1 ){
69863 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
69865 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
69866 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
69867 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
69869 iRead = pWal->iReCksum;
69870 pWal->iReCksum = 0;
69873 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
69880 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
69889 ** Write a set of frames to the log. The caller must hold the write-lock
69894 int szPage, /* Database page-size in bytes */
69901 u32 iFrame; /* Next frame address */
69903 PgHdr *pLast = 0; /* Last frame in list */
69905 int szFrame; /* The size of a single frame */
69908 u32 iFirst = 0; /* First frame that may be overwritten */
69912 assert( pWal->writeLock );
69914 /* If this frame set completes a transaction, then nTruncate>0. If
69915 ** nTruncate==0 then this frame set does not complete the transaction. */
69919 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
69920 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
69921 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
69926 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
69927 iFirst = pLive->mxFrame+1;
69931 ** log file, instead of appending to it at pWal->hdr.mxFrame.
69937 /* If this is the first frame written into the log, write the WAL
69941 iFrame = pWal->hdr.mxFrame;
69943 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
69944 u32 aCksum[2]; /* Checksum for wal-header */
69949 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
69950 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
69951 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
69952 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
69956 pWal->szPage = szPage;
69957 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
69958 pWal->hdr.aFrameCksum[0] = aCksum[0];
69959 pWal->hdr.aFrameCksum[1] = aCksum[1];
69960 pWal->truncateOnCommit = 1;
69962 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
69963 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
69970 ** an out-of-order write following a WAL restart could result in
69975 if( pWal->syncHeader ){
69976 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
69980 if( (int)pWal->szPage!=szPage ){
69986 w.pFd = pWal->pWalFd;
69994 for(p=pList; p; p=p->pDirty){
69998 ** the current transaction. If so, overwrite the existing frame and
69999 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
70001 if( iFirst && (p->pDirty || isCommit==0) ){
70003 VVA_ONLY(rc =) walFindFrame(pWal, p->pgno, &iWrite);
70008 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
70009 pWal->iReCksum = iWrite;
70011 pData = p->pData;
70012 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
70014 p->flags &= ~PGHDR_WAL_APPEND;
70021 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
70026 p->flags |= PGHDR_WAL_APPEND;
70030 if( isCommit && pWal->iReCksum ){
70044 ** final frame is repeated (with its commit mark) until the next sector
70046 ** sector boundary is synced; the part of the last frame that extends
70051 if( pWal->padToSectorBoundary ){
70052 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
70053 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
70070 /* If this frame set completes the first transaction in the WAL and
70074 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
70075 i64 sz = pWal->mxWalSize;
70076 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
70080 pWal->truncateOnCommit = 0;
70083 /* Append data to the wal-index. It is not necessary to lock the
70084 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
70088 iFrame = pWal->hdr.mxFrame;
70089 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
70090 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
70092 rc = walIndexAppend(pWal, iFrame, p->pgno);
70097 nExtra--;
70098 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
70103 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
70106 pWal->hdr.mxFrame = iFrame;
70108 pWal->hdr.iChange++;
70109 pWal->hdr.nPage = nTruncate;
70111 /* If this is a commit, update the wal-index header too. */
70114 pWal->iCallback = iFrame;
70118 WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
70123 ** Write a set of frames to the log. The caller must hold the write-lock
70131 int szPage, /* Database page-size in bytes */
70152 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
70168 int isChanged = 0; /* True if a new wal-index header is loaded */
70172 assert( pWal->ckptLock==0 );
70173 assert( pWal->writeLock==0 );
70175 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
70179 if( pWal->readOnly ) return SQLITE_READONLY;
70186 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
70188 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
70191 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
70198 pWal->ckptLock = 1;
70200 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
70204 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
70205 ** immediately, and a busy-handler is configured, it is invoked and the
70206 ** writer lock retried until either the busy-handler returns 0 or the
70212 pWal->writeLock = 1;
70222 /* Read the wal-index header. */
70225 /* For a passive checkpoint, do not re-enable blocking locks after
70226 ** reading the wal-index header. A passive checkpoint should not block
70228 ** attempt to obtain is a lock on a read-slot, and it should give up
70233 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
70234 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
70240 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
70248 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
70250 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
70257 /* If a new wal-index header was loaded before the checkpoint was
70258 ** performed, then the pager-cache associated with pWal is now
70259 ** out of date. So zero the cached wal-index header to ensure that
70263 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
70271 if( pWal->ckptLock ){
70273 pWal->ckptLock = 0;
70290 ret = pWal->iCallback;
70291 pWal->iCallback = 0;
70302 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
70304 ** transition out of exclusive-mode is successful, return 1. This
70309 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
70311 ** WAL is already in exclusive-locking mode - meaning that this
70312 ** routine is a no-op. The pager must already hold the exclusive lock
70315 ** If op is negative, then do a dry-run of the op==1 case but do
70322 assert( pWal->writeLock==0 );
70323 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
70325 /* pWal->readLock is usually set, but might be -1 if there was a
70326 ** prior error while attempting to acquire are read-lock. This cannot
70329 ** upgrade to exclusive-mode following such an error.
70332 assert( pWal->readLock>=0 || pWal->lockError );
70334 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
70337 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
70338 pWal->exclusiveMode = WAL_NORMAL_MODE;
70339 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
70340 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
70342 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
70348 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
70349 assert( pWal->readLock>=0 );
70350 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
70351 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
70354 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
70360 ** Return true if the argument is non-NULL and the WAL module is using
70361 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
70362 ** WAL module is using shared-memory, return false.
70365 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
70378 assert( pWal->readLock>=0 && pWal->writeLock==0 );
70380 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
70388 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
70395 /* Try to open on pSnapshot when the next read-transaction starts
70401 if( pSnapshot && ((WalIndexHdr*)pSnapshot)->iVersion==0 ){
70405 ** file, it does not take read-lock 0 if the wal file has been completely
70406 ** checkpointed. Taking read-lock 0 would work, but then it would be
70408 ** this connection is holding its read-transaction open. This is contrary
70409 ** to user expectations, so we avoid it by not taking read-lock 0. */
70410 pWal->bGetSnapshot = 1;
70412 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
70413 pWal->bGetSnapshot = 0;
70418 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
70427 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
70428 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
70429 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
70430 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
70451 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
70452 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
70478 ** read-lock. This function returns the database page-size if it is known,
70482 assert( pWal==0 || pWal->readLock>=0 );
70483 return (pWal ? pWal->szPage : 0);
70490 return pWal->pWalFd;
70527 ** This file implements an external (disk-based) database using BTrees.
70531 ** "Sorting And Searching", pages 473-480. Addison-Wesley
70537 ** ----------------------------------------------------------------
70538 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
70539 ** ----------------------------------------------------------------
70544 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
70566 ** page, or a pointer-map page.
70585 ** 40 60 15 4-byte meta values passed to higher layers
70590 ** 52 4 Largest root-page (auto/incr_vacuum)
70591 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
70594 ** 68 4 Application-ID
70596 ** 92 4 The version-valid-for number
70599 ** All of the integer values are big-endian (most significant byte first).
70607 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
70622 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
70625 ** |----------------|
70627 ** |----------------|
70629 ** |----------------|
70633 ** |----------------|
70636 ** |----------------| ^ Grows upwards
70639 ** |----------------|
70658 ** The cell pointer array contains zero or more 2-byte numbers which are
70680 ** Cells are of variable length. Cells are stored in the cell content area at
70685 ** Cell content makes use of variable length integers. A variable
70686 ** length integer is 1 to 9 bytes where the lower 7 bits of each
70689 ** appears first. A variable-length integer may not be more than 9 bytes long.
70691 ** allows a 64-bit integer to be encoded in 9 bytes.
70701 ** Variable length integers are used for rowids and to hold the number of
70714 ** filled with data (pagesize - 4 bytes). The last page can have as little
70737 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
70744 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
70755 ** You can change this value at compile-time by specifying a
70756 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
70757 ** header must be exactly 16 bytes including the zero-terminator so
70769 ** first byte of on-disk image of every BTree page.
70779 ** is derived from the raw on-disk page content.
70786 ** stored in MemPage.pBt->mutex.
70790 u8 intKey; /* True if table b-trees. False for index b-trees */
70803 int nFree; /* Number of free bytes on the page. -1 for unknown */
70806 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
70807 ** non-overflow cell */
70811 u8 *aDataEnd; /* One byte past the end of the entire page - not just
70813 ** corruption-induced buffer overflow. */
70869 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
70883 ** If the shared-data extension is enabled, there may be multiple users
70895 # error wrong numeric code for no-transaction
70898 # error wrong numeric code for read-transaction
70901 # error wrong numeric code for write-transaction
70924 ** If a BtShared client fails to obtain a write-lock on a database
70925 ** table (because there exists one or more read-locks on the table),
70926 ** the shared-cache enters 'pending-lock' state and isPending is
70929 ** The shared-cache leaves the 'pending lock' state when either of
70935 ** while in the 'pending-lock' state, no connection may start a new
70938 ** This feature is included to help prevent writer-starvation.
70947 u8 autoVacuum; /* True if auto-vacuum is enabled */
70948 u8 incrVacuum; /* True if incr-vacuum is enabled */
70952 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
70955 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
70956 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
70965 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
70966 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
70970 BtLock *pLock; /* List of locks held on this shared-btree struct */
70986 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
70988 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
71000 u16 nSize; /* Size of the cell content on the main b-tree page */
71004 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
71007 ** root-node and 3 for all other internal nodes.
71018 #define BT_MAX_LOCAL 65501 /* 65536 - 35 */
71022 ** b-tree within a database file.
71032 ** found at self->pBt->mutex.
71040 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
71041 ** sqlite3BtreePrevious() is no-op if skipNext<0.
71066 u8 curIntKey; /* Value of apPage[0]->intKey */
71068 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
71071 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
71097 ** Cursor is valid except that the Cursor.skipNext field is non-zero
71099 ** operation should be a no-op.
71124 #define PENDING_BYTE_PAGE(pBt) ((Pgno)((PENDING_BYTE/((pBt)->pageSize))+1))
71127 ** These macros define the location of the pointer-map entry for a
71132 ** PTRMAP_PAGENO returns the database page number of the pointer-map
71136 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
71138 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
71142 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
71159 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
71162 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
71170 ** overflow pages. The page-number identifies the previous
71173 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
71186 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
71187 assert( p->pBt->inTransaction>=p->inTrans );
71192 ** if the database supports auto-vacuum or not. Because it is used
71198 #define ISAUTOVACUUM(pBt) (pBt->autoVacuum)
71209 ** the database. As the integrity-check proceeds, for each page used in
71210 ** the database the corresponding bit is set. This allows integrity-check to
71217 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
71229 u32 *heap; /* Min-heap used for analyzing cell coverage */
71235 ** Routines to read or write a two- and four-byte big-endian integer values.
71244 ** two-byte aligned address. get2byteAligned() is only used for accessing the
71263 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
71265 ** p->locked boolean to true.
71268 assert( p->locked==0 );
71269 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
71270 assert( sqlite3_mutex_held(p->db->mutex) );
71272 sqlite3_mutex_enter(p->pBt->mutex);
71273 p->pBt->db = p->db;
71274 p->locked = 1;
71278 ** Release the BtShared mutex associated with B-Tree handle p and
71279 ** clear the p->locked boolean.
71282 BtShared *pBt = p->pBt;
71283 assert( p->locked==1 );
71284 assert( sqlite3_mutex_held(pBt->mutex) );
71285 assert( sqlite3_mutex_held(p->db->mutex) );
71286 assert( p->db==pBt->db );
71288 sqlite3_mutex_leave(pBt->mutex);
71289 p->locked = 0;
71299 ** and this routine is a no-op. The underlying mutex is non-recursive.
71304 ** by all database connections. The p->pNext is a list of other
71307 ** p, then first unlock all of the others on p->pNext, then wait
71316 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
71317 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
71318 assert( p->pNext==0 || p->pNext->db==p->db );
71319 assert( p->pPrev==0 || p->pPrev->db==p->db );
71320 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
71323 assert( !p->locked || p->wantToLock>0 );
71324 assert( p->sharable || p->wantToLock==0 );
71327 assert( sqlite3_mutex_held(p->db->mutex) );
71331 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
71333 if( !p->sharable ) return;
71334 p->wantToLock++;
71335 if( p->locked ) return;
71352 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
71353 p->pBt->db = p->db;
71354 p->locked = 1;
71363 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
71364 assert( pLater->sharable );
71365 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
71366 assert( !pLater->locked || pLater->wantToLock>0 );
71367 if( pLater->locked ){
71372 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
71373 if( pLater->wantToLock ){
71384 assert( sqlite3_mutex_held(p->db->mutex) );
71385 if( p->sharable ){
71386 assert( p->wantToLock>0 );
71387 p->wantToLock--;
71388 if( p->wantToLock==0 ){
71397 ** B-Tree is not marked as sharable.
71402 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
71403 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
71404 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
71405 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
71407 return (p->sharable==0 || p->locked);
71419 ** There is a corresponding leave-all procedures.
71430 assert( sqlite3_mutex_held(db->mutex) );
71431 for(i=0; i<db->nDb; i++){
71432 p = db->aDb[i].pBt;
71433 if( p && p->sharable ){
71438 db->noSharedCache = skipOk;
71441 if( db->noSharedCache==0 ) btreeEnterAll(db);
71446 assert( sqlite3_mutex_held(db->mutex) );
71447 for(i=0; i<db->nDb; i++){
71448 p = db->aDb[i].pBt;
71453 if( db->noSharedCache==0 ) btreeLeaveAll(db);
71465 if( !sqlite3_mutex_held(db->mutex) ){
71468 for(i=0; i<db->nDb; i++){
71470 p = db->aDb[i].pBt;
71471 if( p && p->sharable &&
71472 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
71483 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
71487 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
71495 if( db->pVfs==0 && db->nDb==0 ) return 1;
71497 assert( iDb>=0 && iDb<db->nDb );
71498 if( !sqlite3_mutex_held(db->mutex) ) return 0;
71500 p = db->aDb[iDb].pBt;
71502 return p->sharable==0 || p->locked==1;
71510 ** these two routines, all mutex operations are no-ops in that case and
71514 ** the ones below, are no-ops and are null #defines in btree.h.
71518 p->pBt->db = p->db;
71522 for(i=0; i<db->nDb; i++){
71523 Btree *p = db->aDb[i].pBt;
71525 p->pBt->db = p->db;
71540 sqlite3BtreeEnter(pCur->pBtree);
71544 sqlite3BtreeLeave(pCur->pBtree);
71564 ** This file implements an external (disk-based) database using BTrees.
71588 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
71593 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
71594 ** This routine makes the necessary adjustment to 65536.
71596 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
71609 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
71654 ** shared-cache table level locks. If the library is compiled with the
71655 ** shared-cache feature disabled, then there is only ever one user
71657 ** So define the lock related functions as no-ops.
71672 u64 n = pBt->nSeek;
71673 pBt->nSeek = 0;
71684 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
71692 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
71703 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
71706 /* Default value for SHARED_LOCK_TRACE macro if shared-cache is disabled
71710 #define SHARED_LOCK_TRACE(X,MSG,TAB,TYPE) /*no-op*/
71715 /* ^---- Change to 1 and recompile to enable shared-lock tracing
71718 ** Print all shared-cache locks on a BtShared. Debugging use only.
71728 printf("%s-%p %u%s:", zMsg, pBt, iRoot, eLockType==READ_LOCK?"R":"W");
71730 printf("%s-%p:", zMsg, pBt);
71732 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
71733 printf(" %p/%u%s", pLock->pBtree, pLock->iTable,
71734 pLock->eLock==READ_LOCK ? "R" : "W");
71735 while( pLock->pNext && pLock->pBtree==pLock->pNext->pBtree ){
71736 pLock = pLock->pNext;
71737 printf(",%u%s", pLock->iTable, pLock->eLock==READ_LOCK ? "R" : "W");
71745 #endif /* Shared-lock tracing */
71754 ** For example, when writing to a table with root-page iRoot via
71767 ** hold a write-lock on the schema table (root page 1). This is also
71772 Pgno iRoot, /* Root page of b-tree */
71773 int isIndex, /* True if iRoot is the root of an index b-tree */
71776 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
71781 ** and has the read-uncommitted flag set, then no lock is required.
71784 if( (pBtree->sharable==0)
71785 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
71792 ** the correct locks are held. So do not bother - just return true.
71795 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
71799 /* Figure out the root-page that the lock should be held on. For table
71800 ** b-trees, this is just the root page of the b-tree being read or
71801 ** written. For index b-trees, it is the root page of the associated
71806 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
71808 if( pIdx->tnum==iRoot ){
71815 iTab = pIdx->pTable->tnum;
71823 SHARED_LOCK_TRACE(pBtree->pBt,"hasLock",iRoot,eLockType);
71825 /* Search for the required lock. Either a write-lock on root-page iTab, a
71826 ** write-lock on the schema table, or (if the client is reading) a
71827 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
71828 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
71829 if( pLock->pBtree==pBtree
71830 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
71831 && pLock->eLock>=eLockType
71853 ** read-uncommitted flag set, then it is OK for the other object to
71863 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
71864 if( p->pgnoRoot==iRoot
71865 && p->pBtree!=pBtree
71866 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
71877 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
71882 BtShared *pBt = p->pBt;
71887 assert( p->db!=0 );
71888 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
71890 /* If requesting a write-lock, then the Btree must have an open write
71894 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
71895 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
71897 /* This routine is a no-op if the shared-cache is not enabled */
71898 if( !p->sharable ){
71905 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
71906 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
71910 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71911 /* The condition (pIter->eLock!=eLock) in the following if(...)
71914 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
71920 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
71921 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
71922 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
71923 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
71925 assert( p==pBt->pWriter );
71926 pBt->btsFlags |= BTS_PENDING;
71937 ** Add a lock on the table with root-page iTable to the shared-btree used
71954 BtShared *pBt = p->pBt;
71962 assert( p->db!=0 );
71964 /* A connection with the read-uncommitted flag set will never try to
71965 ** obtain a read-lock using this function. The only read-lock obtained
71966 ** by a connection in read-uncommitted mode is on the sqlite_schema
71968 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
71970 /* This function should only be called on a sharable b-tree after it
71971 ** has been determined that no other b-tree holds a conflicting lock. */
71972 assert( p->sharable );
71976 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
71977 if( pIter->iTable==iTable && pIter->pBtree==p ){
71991 pLock->iTable = iTable;
71992 pLock->pBtree = p;
71993 pLock->pNext = pBt->pLock;
71994 pBt->pLock = pLock;
71998 ** and the requested lock. This means if a write-lock was already held
71999 ** and a read-lock requested, we don't incorrectly downgrade the lock.
72002 if( eLock>pLock->eLock ){
72003 pLock->eLock = eLock;
72020 BtShared *pBt = p->pBt;
72021 BtLock **ppIter = &pBt->pLock;
72024 assert( p->sharable || 0==*ppIter );
72025 assert( p->inTrans>0 );
72031 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
72032 assert( pLock->pBtree->inTrans>=pLock->eLock );
72033 if( pLock->pBtree==p ){
72034 *ppIter = pLock->pNext;
72035 assert( pLock->iTable!=1 || pLock==&p->lock );
72036 if( pLock->iTable!=1 ){
72040 ppIter = &pLock->pNext;
72044 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
72045 if( pBt->pWriter==p ){
72046 pBt->pWriter = 0;
72047 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
72048 }else if( pBt->nTransaction==2 ){
72058 pBt->btsFlags &= ~BTS_PENDING;
72063 ** This function changes all write-locks held by Btree p into read-locks.
72066 BtShared *pBt = p->pBt;
72070 if( pBt->pWriter==p ){
72072 pBt->pWriter = 0;
72073 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
72074 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
72075 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
72076 pLock->eLock = READ_LOCK;
72094 return sqlite3_mutex_held(p->pBt->mutex);
72098 ** database connetion. This is important in shared-cache mode. If the database
72099 ** connection pointers get out-of-sync, it is possible for routines like
72102 ** statements only and for the purpose of double-checking that the btree code
72103 ** does keep the database connection pointers up-to-date.
72107 return (p->pBtree->db==p->pBt->db);
72115 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
72118 ** Invalidate the overflow page-list cache for all cursors opened
72123 assert( sqlite3_mutex_held(pBt->mutex) );
72124 for(p=pBt->pCursor; p; p=p->pNext){
72137 ** cursors open on any row within the table with root-page pgnoRoot.
72150 assert( pBtree->hasIncrblobCur );
72152 pBtree->hasIncrblobCur = 0;
72153 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
72154 if( (p->curFlags & BTCF_Incrblob)!=0 ){
72155 pBtree->hasIncrblobCur = 1;
72156 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
72157 p->eState = CURSOR_INVALID;
72170 ** when a page that previously contained data becomes a free-list leaf
72175 ** free-list leaf pages:
72178 ** a free-list leaf page, the page is not written to the database
72179 ** (as free-list leaf pages contain no meaningful data). Sometimes
72183 ** 2) When a free-list leaf page is reused, its content is not read
72189 ** a page is moved to the free-list and then reused within the same
72191 ** it is moved to the free-list and it is also not journalled when it
72192 ** is extracted from the free-list and reused, then the original data
72197 ** moved to become a free-list leaf page, the corresponding bit is
72198 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
72205 if( !pBt->pHasContent ){
72206 assert( pgno<=pBt->nPage );
72207 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
72208 if( !pBt->pHasContent ){
72212 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
72213 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
72221 ** This function is called when a free-list leaf page is removed from the
72222 ** free-list for reuse. It returns false if it is safe to retrieve the
72223 ** page from the pager layer with the 'no-content' flag set. True otherwise.
72226 Bitvec *p = pBt->pHasContent;
72232 ** invoked at the conclusion of each write-transaction.
72235 sqlite3BitvecDestroy(pBt->pHasContent);
72236 pBt->pHasContent = 0;
72244 if( pCur->iPage>=0 ){
72245 for(i=0; i<pCur->iPage; i++){
72246 releasePageNotNull(pCur->apPage[i]);
72248 releasePageNotNull(pCur->pPage);
72249 pCur->iPage = -1;
72256 ** function saves the current cursor key in variables pCur->nKey and
72257 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
72261 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
72262 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
72263 ** set to point to a malloced buffer pCur->nKey bytes in size containing
72268 assert( CURSOR_VALID==pCur->eState );
72269 assert( 0==pCur->pKey );
72272 if( pCur->curIntKey ){
72274 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
72279 ** up to the size of 1 varint plus 1 8-byte value when the cursor
72283 pCur->nKey = sqlite3BtreePayloadSize(pCur);
72284 pKey = sqlite3Malloc( ((i64)pCur->nKey) + 9 + 8 );
72286 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
72288 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
72289 pCur->pKey = pKey;
72297 assert( !pCur->curIntKey || !pCur->pKey );
72311 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
72312 assert( 0==pCur->pKey );
72315 if( pCur->curFlags & BTCF_Pinned ){
72318 if( pCur->eState==CURSOR_SKIPNEXT ){
72319 pCur->eState = CURSOR_VALID;
72321 pCur->skipNext = 0;
72327 pCur->eState = CURSOR_REQUIRESEEK;
72330 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
72339 ** the table with root-page iRoot. "Saving the cursor position" means that
72350 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
72360 assert( sqlite3_mutex_held(pBt->mutex) );
72361 assert( pExcept==0 || pExcept->pBt==pBt );
72362 for(p=pBt->pCursor; p; p=p->pNext){
72363 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
72366 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
72381 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
72382 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
72388 testcase( p->iPage>=0 );
72392 p = p->pNext;
72402 sqlite3_free(pCur->pKey);
72403 pCur->pKey = 0;
72404 pCur->eState = CURSOR_INVALID;
72423 KeyInfo *pKeyInfo = pCur->pKeyInfo;
72428 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
72433 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
72452 assert( pCur->eState>=CURSOR_REQUIRESEEK );
72453 if( pCur->eState==CURSOR_FAULT ){
72454 return pCur->skipNext;
72456 pCur->eState = CURSOR_INVALID;
72460 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
72463 sqlite3_free(pCur->pKey);
72464 pCur->pKey = 0;
72465 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
72466 if( skipNext ) pCur->skipNext = skipNext;
72467 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
72468 pCur->eState = CURSOR_SKIPNEXT;
72475 (p->eState>=CURSOR_REQUIRESEEK ? \
72495 assert( sizeof(pCur->eState)==1 );
72527 assert( pCur->eState!=CURSOR_VALID );
72533 if( pCur->eState!=CURSOR_VALID ){
72574 pCur->hints = (u8)x;
72581 ** number for the pointer-map page that contains the entry for the
72591 assert( sqlite3_mutex_held(pBt->mutex) );
72593 nPagesPerMapPage = (pBt->usableSize/5)+1;
72594 iPtrMap = (pgno-2)/nPagesPerMapPage;
72608 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
72609 ** a no-op. If an error occurs, the appropriate error code is written
72621 assert( sqlite3_mutex_held(pBt->mutex) );
72622 /* The super-journal page number must never be used as a pointer map page */
72625 assert( pBt->autoVacuum );
72631 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
72648 assert( offset <= (int)pBt->usableSize-5 );
72652 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
72678 assert( sqlite3_mutex_held(pBt->mutex) );
72681 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
72692 assert( offset <= (int)pBt->usableSize-5 );
72714 ** 4-byte child pointer found on interior pages, if there is one.
72719 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
72721 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
72727 ** on a single B-tree page. Make necessary adjustments to the CellInfo
72748 minLocal = pPage->minLocal;
72749 maxLocal = pPage->maxLocal;
72750 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
72754 pInfo->nLocal = (u16)surplus;
72756 pInfo->nLocal = (u16)minLocal;
72758 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
72767 maxLocal = pPage->maxLocal;
72774 minLocal = pPage->minLocal;
72775 surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
72799 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72800 assert( pPage->leaf==0 );
72801 assert( pPage->childPtrSize==4 );
72805 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
72806 pInfo->nPayload = 0;
72807 pInfo->nLocal = 0;
72808 pInfo->pPayload = 0;
72820 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72821 assert( pPage->leaf==0 || pPage->leaf==1 );
72822 assert( pPage->intKeyLeaf );
72823 assert( pPage->childPtrSize==0 );
72844 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
72847 ** This routine is a high-runner.
72881 pInfo->nKey = *(i64*)&iKey;
72882 pInfo->nPayload = nPayload;
72883 pInfo->pPayload = pIter;
72884 testcase( nPayload==pPage->maxLocal );
72885 testcase( nPayload==(u32)pPage->maxLocal+1 );
72887 assert( pPage->maxLocal <= BT_MAX_LOCAL );
72888 if( nPayload<=pPage->maxLocal ){
72892 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72893 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72894 pInfo->nLocal = (u16)nPayload;
72907 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72908 assert( pPage->leaf==0 || pPage->leaf==1 );
72909 assert( pPage->intKeyLeaf==0 );
72910 pIter = pCell + pPage->childPtrSize;
72920 pInfo->nKey = nPayload;
72921 pInfo->nPayload = nPayload;
72922 pInfo->pPayload = pIter;
72923 testcase( nPayload==pPage->maxLocal );
72924 testcase( nPayload==(u32)pPage->maxLocal+1 );
72926 assert( pPage->maxLocal <= BT_MAX_LOCAL );
72927 if( nPayload<=pPage->maxLocal ){
72931 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72932 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72933 pInfo->nLocal = (u16)nPayload;
72943 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
72951 ** data area of the btree-page. The return number includes the cell
72971 pPage->xParseCell(pPage, pCell, &debuginfo);
72974 assert( pPage->childPtrSize==4 );
72984 testcase( nSize==pPage->maxLocal );
72985 testcase( nSize==(u32)pPage->maxLocal+1 );
72986 if( nSize<=pPage->maxLocal ){
72987 nSize += (u32)(pIter - pCell);
72990 int minLocal = pPage->minLocal;
72991 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
72992 testcase( nSize==pPage->maxLocal );
72993 testcase( nSize==(u32)pPage->maxLocal+1 );
72994 if( nSize>pPage->maxLocal ){
72997 nSize += 4 + (u16)(pIter - pCell);
73013 pPage->xParseCell(pPage, pCell, &debuginfo);
73016 assert( pPage->childPtrSize==0 );
73026 testcase( nSize==pPage->maxLocal );
73027 testcase( nSize==(u32)pPage->maxLocal+1 );
73028 if( nSize<=pPage->maxLocal ){
73029 nSize += (u32)(pIter - pCell);
73032 int minLocal = pPage->minLocal;
73033 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
73034 testcase( nSize==pPage->maxLocal );
73035 testcase( nSize==(u32)pPage->maxLocal+1 );
73036 if( nSize>pPage->maxLocal ){
73039 nSize += 4 + (u16)(pIter - pCell);
73054 pPage->xParseCell(pPage, pCell, &debuginfo);
73059 assert( pPage->childPtrSize==4 );
73062 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
73063 return (u16)(pIter - pCell);
73076 pPage->xParseCell(pPage, pCell, &debuginfo);
73088 /* pIter now points at the 64-bit integer key value, a variable length
73099 testcase( nSize==pPage->maxLocal );
73100 testcase( nSize==(u32)pPage->maxLocal+1 );
73101 if( nSize<=pPage->maxLocal ){
73102 nSize += (u32)(pIter - pCell);
73105 int minLocal = pPage->minLocal;
73106 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
73107 testcase( nSize==pPage->maxLocal );
73108 testcase( nSize==(u32)pPage->maxLocal+1 );
73109 if( nSize>pPage->maxLocal ){
73112 nSize += 4 + (u16)(pIter - pCell);
73123 return pPage->xCellSize(pPage, findCell(pPage, iCell));
73131 ** pointer to an overflow page, insert an entry into the pointer-map for
73138 pPage->xParseCell(pPage, pCell, &info);
73141 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
73146 ovfl = get4byte(&pCell[info.nSize-4]);
73147 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
73155 ** page so that there are no free-blocks on the free-block list.
73160 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
73161 ** b-tree page so that there are no freeblocks or fragment bytes, all
73167 int pc; /* Address of the i-th cell */
73181 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73182 assert( pPage->pBt!=0 );
73183 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
73184 assert( pPage->nOverflow==0 );
73185 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73186 data = pPage->aData;
73187 hdr = pPage->hdrOffset;
73188 cellOffset = pPage->cellOffset;
73189 nCell = pPage->nCell;
73192 usableSize = pPage->pBt->usableSize;
73197 ** offsets to each pointer in the cell-pointer array than it is to
73201 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
73204 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
73218 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
73225 assert( cbrk+(iFree-top) <= usableSize );
73226 memmove(&data[cbrk], &data[top], iFree-top);
73238 iCellLast = usableSize - 4;
73241 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
73245 u8 *pAddr; /* The i-th cell pointer */
73257 size = pPage->xCellSize(pPage, &src[pc]);
73258 cbrk -= size;
73272 assert( pPage->nFree>=0 );
73273 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
73280 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
73281 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73286 ** Search the free-list on page pPg for space to store a cell nByte bytes in
73288 ** from the free-list.
73290 ** If no suitable space can be found on the free-list, return NULL.
73300 const int hdr = pPg->hdrOffset; /* Offset to page header */
73301 u8 * const aData = pPg->aData; /* Page data */
73306 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
73311 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
73312 ** freeblock form a big-endian integer which is the size of the freeblock
73313 ** in bytes, including the 4-byte header. */
73316 if( (x = size - nByte)>=0 ){
73320 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
73324 /* Remove the slot from the free-list. Update the number of
73334 /* The slot remains on the free-list. Reduce its size to account
73351 if( pc>maxPC+nByte-4 ){
73359 ** Allocate nByte bytes of space from within the B-Tree page passed
73360 ** as the first argument. Write into *pIdx the index into pPage->aData[]
73372 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
73373 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
73379 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73380 assert( pPage->pBt );
73381 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73383 assert( pPage->nFree>=nByte );
73384 assert( pPage->nOverflow==0 );
73385 assert( nByte < (int)(pPage->pBt->usableSize-8) );
73387 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
73388 gap = pPage->cellOffset + 2*pPage->nCell;
73390 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
73393 ** However, that integer is too large to be stored in a 2-byte unsigned
73398 if( top==0 && pPage->pBt->usableSize==65536 ){
73403 }else if( top>(int)pPage->pBt->usableSize ){
73418 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
73419 *pIdx = g2 = (int)(pSpace-data);
73435 assert( pPage->nCell>0 || CORRUPT_DB );
73436 assert( pPage->nFree>=0 );
73437 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
73450 top -= nByte;
73452 assert( top+nByte <= (int)pPage->pBt->usableSize );
73458 ** Return a section of the pPage->aData to the freelist.
73459 ** The first byte of the new free block is pPage->aData[iStart]
73478 unsigned char *data = pPage->aData; /* Page content */
73481 assert( pPage->pBt!=0 );
73482 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73483 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
73484 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
73485 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73487 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
73492 hdr = pPage->hdrOffset;
73504 if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73516 nFrag = iFreeBlk - iEnd;
73519 if( iEnd > (int)pPage->pBt->usableSize ){
73522 iSize = iEnd - iStart;
73534 nFrag += iStart - iPtrEnd;
73535 iSize = iEnd - iPtr;
73540 data[hdr+7] -= (u8)nFrag;
73544 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
73564 pPage->nFree += iOrigSize;
73581 BtShared *pBt; /* A copy of pPage->pBt */
73583 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
73584 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73585 pBt = pPage->pBt;
73586 pPage->max1bytePayload = pBt->max1bytePayload;
73588 pPage->childPtrSize = 0;
73589 pPage->leaf = 1;
73591 pPage->intKeyLeaf = 1;
73592 pPage->xCellSize = cellSizePtrTableLeaf;
73593 pPage->xParseCell = btreeParseCellPtr;
73594 pPage->intKey = 1;
73595 pPage->maxLocal = pBt->maxLeaf;
73596 pPage->minLocal = pBt->minLeaf;
73598 pPage->intKey = 0;
73599 pPage->intKeyLeaf = 0;
73600 pPage->xCellSize = cellSizePtrIdxLeaf;
73601 pPage->xParseCell = btreeParseCellPtrIndex;
73602 pPage->maxLocal = pBt->maxLocal;
73603 pPage->minLocal = pBt->minLocal;
73605 pPage->intKey = 0;
73606 pPage->intKeyLeaf = 0;
73607 pPage->xCellSize = cellSizePtrIdxLeaf;
73608 pPage->xParseCell = btreeParseCellPtrIndex;
73612 pPage->childPtrSize = 4;
73613 pPage->leaf = 0;
73615 pPage->intKey = 0;
73616 pPage->intKeyLeaf = 0;
73617 pPage->xCellSize = cellSizePtr;
73618 pPage->xParseCell = btreeParseCellPtrIndex;
73619 pPage->maxLocal = pBt->maxLocal;
73620 pPage->minLocal = pBt->minLocal;
73622 pPage->intKeyLeaf = 0;
73623 pPage->xCellSize = cellSizePtrNoPayload;
73624 pPage->xParseCell = btreeParseCellPtrNoPayload;
73625 pPage->intKey = 1;
73626 pPage->maxLocal = pBt->maxLeaf;
73627 pPage->minLocal = pBt->minLeaf;
73629 pPage->intKey = 0;
73630 pPage->intKeyLeaf = 0;
73631 pPage->xCellSize = cellSizePtr;
73632 pPage->xParseCell = btreeParseCellPtrIndex;
73641 ** in the pPage->nFree field.
73644 int pc; /* Address of a freeblock within pPage->aData[] */
73646 u8 *data; /* Equal to pPage->aData */
73653 assert( pPage->pBt!=0 );
73654 assert( pPage->pBt->db!=0 );
73655 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73656 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
73657 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
73658 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
73659 assert( pPage->isInit==1 );
73660 assert( pPage->nFree<0 );
73662 usableSize = pPage->pBt->usableSize;
73663 hdr = pPage->hdrOffset;
73664 data = pPage->aData;
73665 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
73669 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
73670 iCellLast = usableSize - 4;
73673 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
73677 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
73681 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
73708 ** of the cell-content area plus the number of free bytes within
73709 ** the cell-content area. If this is greater than the usable-size
73711 ** serves to verify that the offset to the start of the cell-content
73717 pPage->nFree = (u16)(nFree - iCellFirst);
73730 int pc; /* Address of a freeblock within pPage->aData[] */
73731 u8 *data; /* Equal to pPage->aData */
73735 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
73736 usableSize = pPage->pBt->usableSize;
73737 iCellLast = usableSize - 4;
73738 data = pPage->aData;
73739 cellOffset = pPage->cellOffset;
73740 if( !pPage->leaf ) iCellLast--;
73741 for(i=0; i<pPage->nCell; i++){
73748 sz = pPage->xCellSize(pPage, &data[pc]);
73761 ** not contain a well-formed database page, then return
73763 ** guarantee that the page is well-formed. It only shows that
73767 u8 *data; /* Equal to pPage->aData */
73770 assert( pPage->pBt!=0 );
73771 assert( pPage->pBt->db!=0 );
73772 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73773 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
73774 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
73775 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
73776 assert( pPage->isInit==0 );
73778 pBt = pPage->pBt;
73779 data = pPage->aData + pPage->hdrOffset;
73780 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
73781 ** the b-tree page type. */
73785 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
73786 pPage->maskPage = (u16)(pBt->pageSize - 1);
73787 pPage->nOverflow = 0;
73788 pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
73789 pPage->aCellIdx = data + pPage->childPtrSize + 8;
73790 pPage->aDataEnd = pPage->aData + pBt->pageSize;
73791 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
73792 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
73794 pPage->nCell = get2byte(&data[3]);
73795 if( pPage->nCell>MX_CELL(pBt) ){
73799 testcase( pPage->nCell==MX_CELL(pBt) );
73800 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
73804 assert( pPage->nCell>0
73805 || get2byteNotZero(&data[5])==(int)pBt->usableSize
73807 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
73808 pPage->isInit = 1;
73809 if( pBt->db->flags & SQLITE_CellSizeCk ){
73820 unsigned char *data = pPage->aData;
73821 BtShared *pBt = pPage->pBt;
73822 int hdr = pPage->hdrOffset;
73825 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
73826 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
73827 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
73828 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
73829 assert( sqlite3_mutex_held(pBt->mutex) );
73830 if( pBt->btsFlags & BTS_FAST_SECURE ){
73831 memset(&data[hdr], 0, pBt->usableSize - hdr);
73837 put2byte(&data[hdr+5], pBt->usableSize);
73838 pPage->nFree = (u16)(pBt->usableSize - first);
73840 pPage->cellOffset = (u16)first;
73841 pPage->aDataEnd = &data[pBt->pageSize];
73842 pPage->aCellIdx = &data[first];
73843 pPage->aDataOfst = &data[pPage->childPtrSize];
73844 pPage->nOverflow = 0;
73845 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
73846 pPage->maskPage = (u16)(pBt->pageSize - 1);
73847 pPage->nCell = 0;
73848 pPage->isInit = 1;
73858 if( pgno!=pPage->pgno ){
73859 pPage->aData = sqlite3PagerGetData(pDbPage);
73860 pPage->pDbPage = pDbPage;
73861 pPage->pBt = pBt;
73862 pPage->pgno = pgno;
73863 pPage->hdrOffset = pgno==1 ? 100 : 0;
73865 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
73890 assert( sqlite3_mutex_held(pBt->mutex) );
73891 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
73904 assert( sqlite3_mutex_held(pBt->mutex) );
73905 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
73914 ** error, return ((unsigned int)-1).
73917 return pBt->nPage;
73921 return btreePagecount(p->pBt);
73931 int bReadOnly /* True for a read-only page */
73936 assert( sqlite3_mutex_held(pBt->mutex) );
73942 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
73948 if( pPage->isInit==0 ){
73957 assert( pPage->pgno==pgno || CORRUPT_DB );
73958 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
73970 assert( pPage->aData );
73971 assert( pPage->pBt );
73972 assert( pPage->pDbPage!=0 );
73973 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
73974 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
73975 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73976 sqlite3PagerUnrefNotNull(pPage->pDbPage);
73983 assert( pPage->aData );
73984 assert( pPage->pBt );
73985 assert( pPage->pDbPage!=0 );
73986 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
73987 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
73988 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
73989 sqlite3PagerUnrefPageOne(pPage->pDbPage);
74009 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
74014 (*ppPage)->isInit = 0;
74034 if( pPage->isInit ){
74035 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
74036 pPage->isInit = 0;
74043 ** the call for every page that comes in for re-initializing. */
74054 assert( pBt->db );
74055 assert( sqlite3_mutex_held(pBt->db->mutex) );
74056 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
74064 ** be exclusively in memory, or it might use a disk-based memory cache.
74068 ** If zFilename is ":memory:" then an in-memory database is created
74081 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
74098 /* Set the variable isMemdb to true for an in-memory database, or
74099 ** false for a file-based database.
74111 assert( sqlite3_mutex_held(db->mutex) );
74130 p->inTrans = TRANS_NONE;
74131 p->db = db;
74133 p->lock.pBtree = p;
74134 p->lock.iTable = 1;
74145 int nFullPathname = pVfs->mxPathname+1;
74149 p->sharable = 1;
74175 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
74176 assert( pBt->nRef>0 );
74177 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
74178 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
74180 for(iDb=db->nDb-1; iDb>=0; iDb--){
74181 Btree *pExisting = db->aDb[iDb].pBt;
74182 if( pExisting && pExisting->pBt==pBt ){
74190 p->pBt = pBt;
74191 pBt->nRef++;
74205 p->sharable = 1;
74222 /* Suppress false-positive compiler warning from PVS-Studio */
74230 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
74233 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
74234 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
74239 pBt->openFlags = (u8)flags;
74240 pBt->db = db;
74241 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
74242 p->pBt = pBt;
74244 pBt->pCursor = 0;
74245 pBt->pPage1 = 0;
74246 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
74248 pBt->btsFlags |= BTS_SECURE_DELETE;
74250 pBt->btsFlags |= BTS_OVERWRITE;
74252 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
74253 ** determined by the 2-byte integer located at an offset of 16 bytes from
74255 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
74256 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
74257 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
74258 pBt->pageSize = 0;
74260 /* If the magic name ":memory:" will create an in-memory database, then
74261 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
74264 ** regular file-name. In this case the auto-vacuum applies as per normal.
74267 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
74268 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
74273 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
74274 ** determined by the one-byte unsigned integer found at an offset of 20
74277 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74279 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
74280 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
74283 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
74285 pBt->usableSize = pBt->pageSize - nReserve;
74286 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
74291 pBt->nRef = 1;
74292 if( p->sharable ){
74296 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
74297 if( pBt->mutex==0 ){
74303 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
74315 if( p->sharable ){
74318 for(i=0; i<db->nDb; i++){
74319 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
74320 while( pSib->pPrev ){ pSib = pSib->pPrev; }
74321 if( (uptr)p->pBt<(uptr)pSib->pBt ){
74322 p->pNext = pSib;
74323 p->pPrev = 0;
74324 pSib->pPrev = p;
74326 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
74327 pSib = pSib->pNext;
74329 p->pNext = pSib->pNext;
74330 p->pPrev = pSib;
74331 if( p->pNext ){
74332 p->pNext->pPrev = p;
74334 pSib->pNext = p;
74345 if( pBt && pBt->pPager ){
74346 sqlite3PagerClose(pBt->pPager, 0);
74354 /* If the B-Tree was successfully opened, set the pager-cache size to the
74355 ** default value. Except, when opening on an existing shared pager-cache,
74356 ** do not change the pager-cache size.
74362 pFile = sqlite3PagerFile(pBt->pPager);
74363 if( pFile->pMethods ){
74364 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
74387 assert( sqlite3_mutex_notheld(pBt->mutex) );
74390 pBt->nRef--;
74391 if( pBt->nRef<=0 ){
74393 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
74396 while( ALWAYS(pList) && pList->pNext!=pBt ){
74397 pList=pList->pNext;
74400 pList->pNext = pBt->pNext;
74404 sqlite3_mutex_free(pBt->mutex);
74416 ** Make sure pBt->pTmpSpace points to an allocation of
74417 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
74422 assert( pBt->pTmpSpace==0 );
74425 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
74426 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
74427 if( pBt->pTmpSpace==0 ){
74428 BtCursor *pCur = pBt->pCursor;
74429 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
74434 /* One of the uses of pBt->pTmpSpace is to format cells before
74447 ** left-child pointer to the beginning of a cell.
74449 memset(pBt->pTmpSpace, 0, 8);
74450 pBt->pTmpSpace += 4;
74455 ** Free the pBt->pTmpSpace allocation
74458 if( pBt->pTmpSpace ){
74459 pBt->pTmpSpace -= 4;
74460 sqlite3PageFree(pBt->pTmpSpace);
74461 pBt->pTmpSpace = 0;
74469 BtShared *pBt = p->pBt;
74472 assert( sqlite3_mutex_held(p->db->mutex) );
74478 BtCursor *pCur = pBt->pCursor;
74481 pCur = pCur->pNext;
74482 assert( pTmp->pBtree!=p );
74489 ** The call to sqlite3BtreeRollback() drops any table-locks held by
74495 /* If there are still other outstanding references to the shared-btree
74497 ** up the shared-btree.
74499 assert( p->wantToLock==0 && p->locked==0 );
74500 if( !p->sharable || removeFromSharingList(pBt) ){
74506 assert( !pBt->pCursor );
74507 sqlite3PagerClose(pBt->pPager, p->db);
74508 if( pBt->xFreeSchema && pBt->pSchema ){
74509 pBt->xFreeSchema(pBt->pSchema);
74511 sqlite3DbFree(0, pBt->pSchema);
74517 assert( p->wantToLock==0 );
74518 assert( p->locked==0 );
74519 if( p->pPrev ) p->pPrev->pNext = p->pNext;
74520 if( p->pNext ) p->pNext->pPrev = p->pPrev;
74535 BtShared *pBt = p->pBt;
74536 assert( sqlite3_mutex_held(p->db->mutex) );
74538 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
74554 BtShared *pBt = p->pBt;
74556 assert( sqlite3_mutex_held(p->db->mutex) );
74558 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
74569 BtShared *pBt = p->pBt;
74570 assert( sqlite3_mutex_held(p->db->mutex) );
74572 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
74583 ** is a very low but non-zero probability of damage. Level 3 reduces the
74591 BtShared *pBt = p->pBt;
74592 assert( sqlite3_mutex_held(p->db->mutex) );
74594 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
74623 BtShared *pBt = p->pBt;
74626 pBt->nReserveWanted = (u8)nReserve;
74627 x = pBt->pageSize - pBt->usableSize;
74629 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
74635 ((pageSize-1)&pageSize)==0 ){
74637 assert( !pBt->pCursor );
74639 pBt->pageSize = (u32)pageSize;
74642 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
74643 pBt->usableSize = pBt->pageSize - (u16)nReserve;
74644 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74653 return p->pBt->pageSize;
74658 ** may only be called if it is guaranteed that the b-tree mutex is already
74662 ** known that the shared b-tree mutex is held, but the mutex on the
74669 assert( sqlite3_mutex_held(p->pBt->mutex) );
74670 n = p->pBt->pageSize - p->pBt->usableSize;
74681 ** The amount of reserve can only grow - never shrink.
74686 n1 = (int)p->pBt->nReserveWanted;
74701 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
74712 ** newFlag==(-1) No changes
74717 ** freelist leaf pages are not written back to the database. Thus in-page
74731 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
74732 p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag);
74734 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
74740 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
74741 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
74742 ** is disabled. The default value for the auto-vacuum property is
74749 BtShared *pBt = p->pBt;
74754 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
74757 pBt->autoVacuum = av ?1:0;
74758 pBt->incrVacuum = av==2 ?1:0;
74766 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
74776 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
74777 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
74786 ** If the user has not set the safety-level for this database connection
74787 ** using "PRAGMA synchronous", and if the safety-level is not already
74796 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
74797 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
74798 if( pDb->bSyncSet==0
74799 && pDb->safety_level!=safety_level
74800 && pDb!=&db->aDb[1]
74802 pDb->safety_level = safety_level;
74803 sqlite3PagerSetFlags(pBt->pPager,
74804 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
74821 ** well-formed database file, then SQLITE_CORRUPT is returned.
74831 assert( sqlite3_mutex_held(pBt->mutex) );
74832 assert( pBt->pPage1==0 );
74833 rc = sqlite3PagerSharedLock(pBt->pPager);
74841 nPage = get4byte(28+(u8*)pPage1->aData);
74842 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
74843 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
74846 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
74852 u8 *page1 = pPage1->aData;
74854 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
74863 pBt->btsFlags |= BTS_READ_ONLY;
74870 pBt->btsFlags |= BTS_READ_ONLY;
74881 ** may not be the latest version - there may be a newer one in the log
74884 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
74886 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
74902 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
74911 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
74912 ** determined by the 2-byte integer located at an offset of 16 bytes from
74915 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
74917 if( ((pageSize-1)&pageSize)!=0
74924 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
74928 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
74929 ** determined by the one-byte unsigned integer found at an offset of 20
74931 usableSize = pageSize - page1[20];
74932 if( (u32)pageSize!=pBt->pageSize ){
74934 ** of BtShared.pageSize, we have discovered that the page-size is
74935 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
74937 ** again with the correct page-size.
74940 pBt->usableSize = usableSize;
74941 pBt->pageSize = pageSize;
74942 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74944 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
74945 pageSize-usableSize);
74949 if( sqlite3WritableSchema(pBt->db)==0 ){
74956 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
74962 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
74963 pBt->pageSize = pageSize;
74964 pBt->usableSize = usableSize;
74966 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
74967 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
74973 ** cells can will fit on one page. We assume a 10-byte page header.
74975 ** 2-byte pointer to the cell
74976 ** 4-byte child pointer
74977 ** 9-byte nKey value
74978 ** 4-byte nData value
74979 ** 4-byte overflow page pointer
74980 ** So a cell consists of a 2-byte pointer, a header which is as much as
74984 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
74985 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
74986 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
74987 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
74988 if( pBt->maxLocal>127 ){
74989 pBt->max1bytePayload = 127;
74991 pBt->max1bytePayload = (u8)pBt->maxLocal;
74993 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
74994 pBt->pPage1 = pPage1;
74995 pBt->nPage = nPage;
75000 pBt->pPage1 = 0;
75020 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
75021 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
75022 && pCur->eState!=CURSOR_FAULT ) r++;
75034 ** If there is a transaction in progress, this routine is a no-op.
75037 assert( sqlite3_mutex_held(pBt->mutex) );
75038 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
75039 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
75040 MemPage *pPage1 = pBt->pPage1;
75041 assert( pPage1->aData );
75042 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
75043 pBt->pPage1 = 0;
75058 assert( sqlite3_mutex_held(pBt->mutex) );
75059 if( pBt->nPage>0 ){
75062 pP1 = pBt->pPage1;
75064 data = pP1->aData;
75065 rc = sqlite3PagerWrite(pP1->pDbPage);
75069 data[16] = (u8)((pBt->pageSize>>8)&0xff);
75070 data[17] = (u8)((pBt->pageSize>>16)&0xff);
75073 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
75074 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
75078 memset(&data[24], 0, 100-24);
75080 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
75082 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
75083 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
75084 put4byte(&data[36 + 4*4], pBt->autoVacuum);
75085 put4byte(&data[36 + 7*4], pBt->incrVacuum);
75087 pBt->nPage = 1;
75100 p->pBt->nPage = 0;
75101 rc = newDatabase(p->pBt);
75107 ** Attempt to start a new transaction. A write-transaction
75108 ** is started if the second argument is nonzero, otherwise a read-
75112 ** upgraded to exclusive by calling this routine a second time - the
75115 ** A write-transaction must be started before attempting any
75129 ** if there is one. But if there was previously a read-lock, do not
75130 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
75131 ** returned when there is already a read-lock in order to avoid a deadlock.
75146 BtShared *pBt = p->pBt;
75147 Pager *pPager = pBt->pPager;
75153 /* If the btree is already in a write-transaction, or it
75154 ** is already in a read-transaction and a read-transaction
75155 ** is requested, this is a no-op.
75157 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
75160 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
75162 if( (p->db->flags & SQLITE_ResetDatabase)
75165 pBt->btsFlags &= ~BTS_READ_ONLY;
75168 /* Write transactions are not possible on a read-only database */
75169 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
75178 ** on this shared-btree structure and a second write transaction is
75181 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
75182 || (pBt->btsFlags & BTS_PENDING)!=0
75184 pBlock = pBt->pWriter->db;
75187 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
75188 if( pIter->pBtree!=p ){
75189 pBlock = pIter->pBtree->db;
75195 sqlite3ConnectionBlocked(p->db, pBlock);
75202 /* Any read-only or read-write transaction implies a read-lock on
75203 ** page 1. So if some other shared-cache client already has a write-lock
75208 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
75209 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
75211 sqlite3PagerWalDb(pPager, p->db);
75216 if( pBt->pPage1==0 && wrflag ){
75217 assert( pBt->inTransaction==TRANS_NONE );
75223 /* Call lockBtree() until either pBt->pPage1 is populated or
75225 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
75226 ** reading page 1 it discovers that the page-size of the database
75227 ** file is not pBt->pageSize. In this case lockBtree() will update
75228 ** pBt->pageSize to the page-size of the file on disk.
75230 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
75233 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
75236 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
75239 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
75255 ** the busy-handler is not invoked. */
75259 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
75267 if( p->inTrans==TRANS_NONE ){
75268 pBt->nTransaction++;
75270 if( p->sharable ){
75271 assert( p->lock.pBtree==p && p->lock.iTable==1 );
75272 p->lock.eLock = READ_LOCK;
75273 p->lock.pNext = pBt->pLock;
75274 pBt->pLock = &p->lock;
75278 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
75279 if( p->inTrans>pBt->inTransaction ){
75280 pBt->inTransaction = p->inTrans;
75283 MemPage *pPage1 = pBt->pPage1;
75285 assert( !pBt->pWriter );
75286 pBt->pWriter = p;
75287 pBt->btsFlags &= ~BTS_EXCLUSIVE;
75288 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
75291 /* If the db-size header field is incorrect (as it may be if an old
75294 ** re-read the database size from page 1 if a savepoint or transaction
75297 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
75298 rc = sqlite3PagerWrite(pPage1->pDbPage);
75300 put4byte(&pPage1->aData[28], pBt->nPage);
75309 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
75314 ** the sub-journal is not already open, then it will be opened here.
75316 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
75326 if( p->sharable
75327 || p->inTrans==TRANS_NONE
75328 || (p->inTrans==TRANS_READ && wrflag!=0)
75332 pBt = p->pBt;
75334 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
75339 ** the sub-journal is not already open, then it will be opened here.
75341 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
75350 ** Set the pointer-map entries for all children of page pPage. Also, if
75358 BtShared *pBt = pPage->pBt;
75359 Pgno pgno = pPage->pgno;
75361 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75362 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
75364 nCell = pPage->nCell;
75371 if( !pPage->leaf ){
75377 if( !pPage->leaf ){
75378 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
75390 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
75393 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
75396 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
75400 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
75401 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
75404 if( get4byte(pPage->aData)!=iFrom ){
75407 put4byte(pPage->aData, iTo);
75413 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
75415 nCell = pPage->nCell;
75421 pPage->xParseCell(pPage, pCell, &info);
75423 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
75426 if( iFrom==get4byte(pCell+info.nSize-4) ){
75427 put4byte(pCell+info.nSize-4, iTo);
75432 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
75444 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
75447 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
75459 ** the journal needs to be sync()ed before database page pDbPage->pgno
75467 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
75472 Pgno iDbPage = pDbPage->pgno;
75473 Pager *pPager = pBt->pPager;
75478 assert( sqlite3_mutex_held(pBt->mutex) );
75479 assert( pDbPage->pBt==pBt );
75485 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
75489 pDbPage->pgno = iFreePage;
75491 /* If pDbPage was a btree-page, then it may have child pages and/or cells
75505 Pgno nextOvfl = get4byte(pDbPage->aData);
75523 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
75541 ** Perform a single step of an incremental-vacuum. If successful, return
75546 ** More specifically, this function attempts to re-organize the database so
75552 ** If the bCommit parameter is non-zero, this function assumes that the
75554 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
75558 Pgno nFreeList; /* Number of pages still on the free-list */
75561 assert( sqlite3_mutex_held(pBt->mutex) );
75568 nFreeList = get4byte(&pBt->pPage1->aData[36]);
75583 /* Remove the page from the files free-list. This is not required
75584 ** if bCommit is non-zero. In that case, the free-list will be
75612 ** looping until a free-page located within the first nFin pages
75645 iLastPg--;
75647 pBt->bDoTruncate = 1;
75648 pBt->nPage = iLastPg;
75654 ** The database opened by the first argument is an auto-vacuum database
75656 ** size of the database in pages following an auto-vacuum operation.
75663 nEntry = pBt->usableSize/5;
75664 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
75665 nFin = nOrig - nFree - nPtrmap;
75667 nFin--;
75670 nFin--;
75677 ** A write-transaction must be opened before calling this function.
75686 BtShared *pBt = p->pBt;
75689 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
75690 if( !pBt->autoVacuum ){
75694 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
75706 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75707 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
75719 ** is committed for an auto-vacuum database.
75729 pBt = p->pBt;
75730 pPager = pBt->pPager;
75733 assert( sqlite3_mutex_held(pBt->mutex) );
75735 assert(pBt->autoVacuum);
75736 if( !pBt->incrVacuum ){
75746 ** is either a pointer-map page or the pending-byte page. If one
75752 nFree = get4byte(&pBt->pPage1->aData[36]);
75753 db = p->db;
75754 if( db->xAutovacPages ){
75756 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
75757 if( db->aDb[iDb].pBt==p ) break;
75759 nVac = db->xAutovacPages(
75760 db->pAutovacPagesArg,
75761 db->aDb[iDb].zDbSName,
75764 pBt->pageSize
75780 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
75784 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75786 put4byte(&pBt->pPage1->aData[32], 0);
75787 put4byte(&pBt->pPage1->aData[36], 0);
75789 put4byte(&pBt->pPage1->aData[28], nFin);
75790 pBt->bDoTruncate = 1;
75791 pBt->nPage = nFin;
75807 ** This routine does the first phase of a two-phase commit. This routine
75819 ** This call is a no-op if no write-transaction is currently active on pBt.
75822 ** the name of a super-journal file that should be written into the
75823 ** individual journal file, or is NULL, indicating no super-journal file
75826 ** When this is called, the super-journal should already have been
75830 ** the write-transaction for this database file is to delete the journal.
75834 if( p->inTrans==TRANS_WRITE ){
75835 BtShared *pBt = p->pBt;
75838 if( pBt->autoVacuum ){
75845 if( pBt->bDoTruncate ){
75846 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
75849 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
75860 BtShared *pBt = p->pBt;
75861 sqlite3 *db = p->db;
75865 pBt->bDoTruncate = 0;
75867 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
75869 ** handle, downgrade to a read-only transaction. The other statements
75872 p->inTrans = TRANS_READ;
75878 if( p->inTrans!=TRANS_NONE ){
75880 pBt->nTransaction--;
75881 if( 0==pBt->nTransaction ){
75882 pBt->inTransaction = TRANS_NONE;
75888 p->inTrans = TRANS_NONE;
75898 ** This routine implements the second phase of a 2-phase commit. The
75910 ** is non-zero then this b-tree transaction is part of a multi-file
75912 ** (by deleting a super-journal file) and the caller will ignore this
75914 ** reset the b-tree objects internal state to indicate that the write
75923 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
75927 /* If the handle has a write-transaction open, commit the shared-btrees
75930 if( p->inTrans==TRANS_WRITE ){
75932 BtShared *pBt = p->pBt;
75933 assert( pBt->inTransaction==TRANS_WRITE );
75934 assert( pBt->nTransaction>0 );
75935 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
75940 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
75941 pBt->inTransaction = TRANS_READ;
75975 ** flag is true, then only write-cursors need be tripped - read-only
75979 ** rolled back modified the database schema. In this case b-tree root
75984 ** saving the current position of a read-only cursor, all cursors,
75985 ** including all read-cursors are tripped.
75997 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
75998 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
75999 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
76008 p->eState = CURSOR_FAULT;
76009 p->skipNext = errCode;
76019 ** Set the pBt->nPage field correctly, according to the current
76020 ** state of the database. Assume pBt->pPage1 is valid.
76023 int nPage = get4byte(&pPage1->aData[28]);
76025 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
76026 testcase( pBt->nPage!=(u32)nPage );
76027 pBt->nPage = nPage;
76043 BtShared *pBt = p->pBt;
76062 if( p->inTrans==TRANS_WRITE ){
76065 assert( TRANS_WRITE==pBt->inTransaction );
76066 rc2 = sqlite3PagerRollback(pBt->pPager);
76071 /* The rollback may have destroyed the pPage1->aData value. So
76073 ** sure pPage1->aData is set correctly. */
76079 pBt->inTransaction = TRANS_READ;
76099 ** A statement sub-transaction is implemented as an anonymous savepoint. The
76101 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
76102 ** are no active savepoints and no other statement-transactions open,
76108 BtShared *pBt = p->pBt;
76110 assert( p->inTrans==TRANS_WRITE );
76111 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
76113 assert( iStatement>p->db->nSavepoint );
76114 assert( pBt->inTransaction==TRANS_WRITE );
76120 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
76132 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
76139 if( p && p->inTrans==TRANS_WRITE ){
76140 BtShared *pBt = p->pBt;
76142 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
76148 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
76151 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
76152 pBt->nPage = 0;
76155 btreeSetNPage(pBt, pBt->pPage1);
76157 /* pBt->nPage might be zero if the database was corrupt when
76159 assert( CORRUPT_DB || pBt->nPage>0 );
76168 ** iTable. If a read-only cursor is requested, it is assumed that
76169 ** the caller already has at least a read-only transaction open
76170 ** on the database already. If a write-cursor is requested, then
76187 ** 3: The database must be writable (not on read-only media)
76198 ** operations on this cursor can be no-ops and all READ operations can
76199 ** return a null row (2-bytes: 0x01 0x00).
76202 ** root page of a b-tree. If it is not, then the cursor acquired
76211 int wrFlag, /* 1 to write. 0 read-only */
76215 BtShared *pBt = p->pBt; /* Shared b-tree handle */
76225 ** b-tree database, the connection is holding the required table locks,
76233 assert( p->inTrans>TRANS_NONE );
76234 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
76235 assert( pBt->pPage1 && pBt->pPage1->aData );
76236 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
76249 pCur->pgnoRoot = iTable;
76250 pCur->iPage = -1;
76251 pCur->pKeyInfo = pKeyInfo;
76252 pCur->pBtree = p;
76253 pCur->pBt = pBt;
76254 pCur->curFlags = 0;
76257 for(pX=pBt->pCursor; pX; pX=pX->pNext){
76258 if( pX->pgnoRoot==iTable ){
76259 pX->curFlags |= BTCF_Multiple;
76260 pCur->curFlags = BTCF_Multiple;
76263 pCur->eState = CURSOR_INVALID;
76264 pCur->pNext = pBt->pCursor;
76265 pBt->pCursor = pCur;
76267 pCur->curFlags |= BTCF_WriteFlag;
76268 pCur->curPagerFlags = 0;
76269 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
76271 pCur->curPagerFlags = PAGER_GET_READONLY;
76278 int wrFlag, /* 1 to write. 0 read-only */
76291 int wrFlag, /* 1 to write. 0 read-only */
76295 if( p->sharable ){
76307 ** to users so they cannot do the sizeof() themselves - they must call
76324 BtShared *pBt = pBtree->pBt;
76325 if( (pBt->openFlags & BTREE_SINGLE)==0 ) return 0;
76326 if( pBt->pCursor!=pCur ) return 0;
76327 if( pCur->pNext!=0 ) return 0;
76328 if( pCur->pBtree!=pBtree ) return 0;
76339 ** of run-time by skipping the initialization of those elements.
76350 Btree *pBtree = pCur->pBtree;
76352 BtShared *pBt = pCur->pBt;
76354 assert( pBt->pCursor!=0 );
76355 if( pBt->pCursor==pCur ){
76356 pBt->pCursor = pCur->pNext;
76358 BtCursor *pPrev = pBt->pCursor;
76360 if( pPrev->pNext==pCur ){
76361 pPrev->pNext = pCur->pNext;
76364 pPrev = pPrev->pNext;
76369 sqlite3_free(pCur->aOverflow);
76370 sqlite3_free(pCur->pKey);
76371 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
76374 assert( pBtree->sharable==0 );
76379 pCur->pBtree = 0;
76394 if( a->nKey!=b->nKey ) return 0;
76395 if( a->pPayload!=b->pPayload ) return 0;
76396 if( a->nPayload!=b->nPayload ) return 0;
76397 if( a->nLocal!=b->nLocal ) return 0;
76398 if( a->nSize!=b->nSize ) return 0;
76404 btreeParseCell(pCur->pPage, pCur->ix, &info);
76405 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
76411 if( pCur->info.nSize==0 ){
76412 pCur->curFlags |= BTCF_ValidNKey;
76413 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
76422 ** that is currently pointing to a row in a (non-empty) table.
76426 return pCur && pCur->eState==CURSOR_VALID;
76431 return pCur->eState==CURSOR_VALID;
76442 assert( pCur->eState==CURSOR_VALID );
76443 assert( pCur->curIntKey );
76445 return pCur->info.nKey;
76452 assert( (pCur->curFlags & BTCF_Pinned)==0 );
76453 pCur->curFlags |= BTCF_Pinned;
76456 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
76457 pCur->curFlags &= ~BTCF_Pinned;
76466 assert( pCur->eState==CURSOR_VALID );
76468 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
76469 (i64)(pCur->info.pPayload - pCur->pPage->aData);
76477 ** The caller must guarantee that the cursor is pointing to a non-NULL
76483 assert( pCur->eState==CURSOR_VALID );
76485 return pCur->info.nPayload;
76503 assert( pCur->eState==CURSOR_VALID );
76504 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
76510 ** linked list of overflow pages. If possible, it uses the auto-vacuum
76511 ** pointer-map data instead of reading the content of page ovfl to do so.
76523 ** the pointer-map was used to obtain the value for *pPgnoNext), then
76536 assert( sqlite3_mutex_held(pBt->mutex) );
76541 ** autovacuum pointer-map pages. Guess that the next page in
76546 if( pBt->autoVacuum ){
76570 next = get4byte(pPage->aData);
76598 int eOp, /* 0 -> copy from page, 1 -> copy to page */
76631 ** the overflow page-list cache array (BtCursor.aOverflow).
76635 ** Once an overflow page-list cache has been allocated, it must be
76637 ** the cursor is moved to a different row. Additionally, in auto-vacuum
76638 ** mode, the following events may invalidate an overflow page-list cache.
76649 int eOp /* zero to read. non-zero to write. */
76654 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
76655 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
76662 assert( pCur->eState==CURSOR_VALID );
76663 if( pCur->ix>=pPage->nCell ){
76669 aPayload = pCur->info.pPayload;
76670 assert( offset+amt <= pCur->info.nPayload );
76672 assert( aPayload > pPage->aData );
76673 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
76676 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
76683 if( offset<pCur->info.nLocal ){
76685 if( a+offset>pCur->info.nLocal ){
76686 a = pCur->info.nLocal - offset;
76688 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
76691 amt -= a;
76693 offset -= pCur->info.nLocal;
76698 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
76701 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
76710 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
76711 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
76712 if( pCur->aOverflow==0
76713 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
76719 aNew = (Pgno*)sqlite3Realloc(pCur->aOverflow, nOvfl*2*sizeof(Pgno));
76724 pCur->aOverflow = aNew;
76727 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
76728 pCur->curFlags |= BTCF_ValidOvfl;
76731 assert( pCur->aOverflow[0]==nextPage
76732 || pCur->aOverflow[0]==0
76734 assert( pCur->aOverflow[0]!=0 || pCur->aOverflow[offset/ovflSize]==0 );
76736 /* If the overflow page-list cache has been allocated and the
76740 if( pCur->aOverflow[offset/ovflSize] ){
76742 nextPage = pCur->aOverflow[iIdx];
76749 /* If required, populate the overflow page-list cache. */
76750 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
76751 assert( pCur->aOverflow[iIdx]==0
76752 || pCur->aOverflow[iIdx]==nextPage
76754 pCur->aOverflow[iIdx] = nextPage;
76760 ** page-list cache, if any, then fall back to the getOverflowPage()
76763 assert( pCur->curFlags & BTCF_ValidOvfl );
76764 assert( pCur->pBtree->db==pBt->db );
76765 if( pCur->aOverflow[iIdx+1] ){
76766 nextPage = pCur->aOverflow[iIdx+1];
76770 offset -= ovflSize;
76777 a = ovflSize - offset;
76785 ** 3) there are no dirty pages in the page-cache
76786 ** 4) the database is file-backed, and
76791 ** output buffer, bypassing the page-cache altogether. This speeds
76796 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
76797 && &pBuf[-4]>=pBufStart /* (6) */
76799 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
76801 u8 *aWrite = &pBuf[-4];
76804 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
76812 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
76823 amt -= a;
76844 ** pCur can be pointing to either a table or an index b-tree.
76846 ** pCur is pointing to an index b-tree then the key section is read.
76858 assert( pCur->eState==CURSOR_VALID );
76859 assert( pCur->iPage>=0 && pCur->pPage );
76876 if ( pCur->eState==CURSOR_INVALID ){
76884 if( pCur->eState==CURSOR_VALID ){
76896 ** the key if index btrees (pPage->intKey==0) and is the data for
76897 ** table btrees (pPage->intKey==1). The number of bytes of available
76917 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
76918 assert( pCur->eState==CURSOR_VALID );
76919 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
76921 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
76922 assert( pCur->info.nSize>0 );
76923 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
76924 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
76925 amt = pCur->info.nLocal;
76926 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
76930 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
76933 return (void*)pCur->info.pPayload;
76940 ** b-tree page. Write the number of available bytes into *pAmt.
76960 ** This function returns SQLITE_CORRUPT if the page-header flags field of
76962 ** if an intkey page appears to be the parent of a non-intkey page, or
76963 ** vice-versa).
76968 assert( pCur->eState==CURSOR_VALID );
76969 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
76970 assert( pCur->iPage>=0 );
76971 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
76974 pCur->info.nSize = 0;
76975 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
76976 pCur->aiIdx[pCur->iPage] = pCur->ix;
76977 pCur->apPage[pCur->iPage] = pCur->pPage;
76978 pCur->ix = 0;
76979 pCur->iPage++;
76980 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
76981 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
76983 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
76985 releasePage(pCur->pPage);
76989 pCur->pPage = pCur->apPage[--pCur->iPage];
76996 ** Page pParent is an internal (non-leaf) tree page. This function
76997 ** asserts that page number iChild is the left-child if the iIdx'th
76999 ** cells in pParent, that page number iChild is the right-child of
77005 assert( iIdx<=pParent->nCell );
77006 if( iIdx==pParent->nCell ){
77007 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
77019 ** pCur->idx is set to the cell index that contains the pointer
77021 ** right-most child page then pCur->idx is set to one more than
77027 assert( pCur->eState==CURSOR_VALID );
77028 assert( pCur->iPage>0 );
77029 assert( pCur->pPage );
77031 pCur->apPage[pCur->iPage-1],
77032 pCur->aiIdx[pCur->iPage-1],
77033 pCur->pPage->pgno
77035 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
77036 pCur->info.nSize = 0;
77037 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
77038 pCur->ix = pCur->aiIdx[pCur->iPage-1];
77039 pLeaf = pCur->pPage;
77040 pCur->pPage = pCur->apPage[--pCur->iPage];
77045 ** Move the cursor to point to the root page of its b-tree structure.
77052 ** If the b-tree structure is empty, the cursor state is set to
77058 ** page-header flags indicate that the [virtual] root-page is the expected
77059 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
77061 ** indicating a table b-tree, or if the caller did specify a KeyInfo
77063 ** b-tree).
77073 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
77074 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
77076 if( pCur->iPage>=0 ){
77077 if( pCur->iPage ){
77078 releasePageNotNull(pCur->pPage);
77079 while( --pCur->iPage ){
77080 releasePageNotNull(pCur->apPage[pCur->iPage]);
77082 pRoot = pCur->pPage = pCur->apPage[0];
77085 }else if( pCur->pgnoRoot==0 ){
77086 pCur->eState = CURSOR_INVALID;
77089 assert( pCur->iPage==(-1) );
77090 if( pCur->eState>=CURSOR_REQUIRESEEK ){
77091 if( pCur->eState==CURSOR_FAULT ){
77092 assert( pCur->skipNext!=SQLITE_OK );
77093 return pCur->skipNext;
77097 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
77098 pCur->curPagerFlags);
77100 pCur->eState = CURSOR_INVALID;
77103 pCur->iPage = 0;
77104 pCur->curIntKey = pCur->pPage->intKey;
77106 pRoot = pCur->pPage;
77107 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
77109 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
77110 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
77111 ** NULL, the caller expects a table b-tree. If this is not the case,
77116 ** if pCur->iPage>=0). But this is not so if the database is corrupted
77117 ** in such a way that page pRoot is linked into a second b-tree table
77119 assert( pRoot->intKey==1 || pRoot->intKey==0 );
77120 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
77121 return SQLITE_CORRUPT_PAGE(pCur->pPage);
77125 pCur->ix = 0;
77126 pCur->info.nSize = 0;
77127 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
77129 if( pRoot->nCell>0 ){
77130 pCur->eState = CURSOR_VALID;
77131 }else if( !pRoot->leaf ){
77133 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
77134 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
77135 pCur->eState = CURSOR_VALID;
77138 pCur->eState = CURSOR_INVALID;
77145 ** Move the cursor down to the left-most leaf entry beneath the
77148 ** The left-most leaf is the one with the smallest key - the first
77157 assert( pCur->eState==CURSOR_VALID );
77158 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
77159 assert( pCur->ix<pPage->nCell );
77160 pgno = get4byte(findCell(pPage, pCur->ix));
77167 ** Move the cursor down to the right-most leaf entry beneath the
77170 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
77171 ** finds the right-most entry beneath the *page*.
77173 ** The right-most entry is the one with the largest key - the last
77182 assert( pCur->eState==CURSOR_VALID );
77183 while( !(pPage = pCur->pPage)->leaf ){
77184 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
77185 pCur->ix = pPage->nCell;
77189 pCur->ix = pPage->nCell-1;
77190 assert( pCur->info.nSize==0 );
77191 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
77203 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77206 assert( pCur->pPage->nCell>0 );
77210 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
77227 for(ii=0; ii<pCur->iPage; ii++){
77228 if( pCur->aiIdx[ii]!=pCur->apPage[ii]->nCell ) return 0;
77230 return pCur->ix==pCur->pPage->nCell-1 && pCur->pPage->leaf!=0;
77241 assert( pCur->eState==CURSOR_VALID );
77245 pCur->curFlags |= BTCF_AtLast;
77247 pCur->curFlags &= ~BTCF_AtLast;
77250 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
77258 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77260 /* If the cursor already points to the last entry, this is a no-op. */
77261 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
77301 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77303 assert( pCur->pKeyInfo==0 );
77304 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
77308 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
77309 if( pCur->info.nKey==intKey ){
77313 if( pCur->info.nKey<intKey ){
77314 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
77316 *pRes = -1;
77323 if( pCur->info.nKey+1==intKey ){
77328 if( pCur->info.nKey==intKey ){
77339 pCur->pBtree->nSeek++; /* Performance measurement during testing */
77345 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
77346 *pRes = -1;
77351 assert( pCur->pPage );
77352 assert( pCur->pPage->isInit );
77353 assert( pCur->eState==CURSOR_VALID );
77354 assert( pCur->pPage->nCell > 0 );
77355 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
77356 assert( pCur->curIntKey );
77361 MemPage *pPage = pCur->pPage;
77364 /* pPage->nCell must be greater than zero. If this is the root-page
77366 ** not run. If this is not the root-page, then the moveToChild() routine
77368 ** be the right kind (index or table) of b-tree page. Otherwise
77370 assert( pPage->nCell>0 );
77371 assert( pPage->intKey );
77373 upr = pPage->nCell-1;
77375 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
77379 if( pPage->intKeyLeaf ){
77381 if( pCell>=pPage->aDataEnd ){
77389 if( lwr>upr ){ c = -1; break; }
77391 upr = idx-1;
77395 pCur->ix = (u16)idx;
77396 if( !pPage->leaf ){
77400 pCur->curFlags |= BTCF_ValidNKey;
77401 pCur->info.nKey = nCellKey;
77402 pCur->info.nSize = 0;
77410 assert( lwr==upr+1 || !pPage->leaf );
77411 assert( pPage->isInit );
77412 if( pPage->leaf ){
77413 assert( pCur->ix<pCur->pPage->nCell );
77414 pCur->ix = (u16)idx;
77420 if( lwr>=pPage->nCell ){
77421 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
77425 pCur->ix = (u16)lwr;
77430 pCur->info.nSize = 0;
77431 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
77436 ** Compare the "idx"-th cell on the page the cursor pCur is currently
77457 MemPage *pPage = pCur->pPage;
77463 if( nCell<=pPage->max1bytePayload ){
77464 /* This branch runs if the record-size field of the cell is a
77466 ** b-tree page. */
77467 testcase( pCell+nCell+1==pPage->aDataEnd );
77470 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
77472 /* The record-size field is a 2 byte varint and the record
77473 ** fits entirely on the main b-tree page. */
77474 testcase( pCell+nCell+2==pPage->aDataEnd );
77485 ** Return true (non-zero) if pCur is current pointing to the last
77490 assert( pCur->eState==CURSOR_VALID );
77491 for(i=0; i<pCur->iPage; i++){
77492 MemPage *pPage = pCur->apPage[i];
77493 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
77521 ** The pIdxKey->eqSeen field is set to 1 if there
77533 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77535 assert( pCur->pKeyInfo!=0 );
77538 pCur->pBtree->nSeek++; /* Performance measurement during testing */
77542 pIdxKey->errCode = 0;
77543 assert( pIdxKey->default_rc==1
77544 || pIdxKey->default_rc==0
77545 || pIdxKey->default_rc==-1
77560 if( pCur->eState==CURSOR_VALID
77561 && pCur->pPage->leaf
77565 if( pCur->ix==pCur->pPage->nCell-1
77566 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
77567 && pIdxKey->errCode==SQLITE_OK
77572 if( pCur->iPage>0
77574 && pIdxKey->errCode==SQLITE_OK
77576 pCur->curFlags &= ~(BTCF_ValidOvfl|BTCF_AtLast);
77577 if( !pCur->pPage->isInit ){
77582 pIdxKey->errCode = SQLITE_OK;
77588 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
77589 *pRes = -1;
77596 assert( pCur->pPage );
77597 assert( pCur->pPage->isInit );
77598 assert( pCur->eState==CURSOR_VALID );
77599 assert( pCur->pPage->nCell > 0 );
77600 assert( pCur->curIntKey==0 );
77605 MemPage *pPage = pCur->pPage;
77608 /* pPage->nCell must be greater than zero. If this is the root-page
77610 ** not run. If this is not the root-page, then the moveToChild() routine
77612 ** be the right kind (index or table) of b-tree page. Otherwise
77614 assert( pPage->nCell>0 );
77615 assert( pPage->intKey==0 );
77617 upr = pPage->nCell-1;
77623 /* The maximum supported page-size is 65536 bytes. This means that
77624 ** the maximum number of record bytes stored on an index B-Tree
77625 ** page is less than 16384 bytes and may be stored as a 2-byte
77628 ** stored entirely within the b-tree page by inspecting the first
77632 if( nCell<=pPage->max1bytePayload ){
77633 /* This branch runs if the record-size field of the cell is a
77635 ** b-tree page. */
77636 testcase( pCell+nCell+1==pPage->aDataEnd );
77639 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
77641 /* The record-size field is a 2 byte varint and the record
77642 ** fits entirely on the main b-tree page. */
77643 testcase( pCell+nCell+2==pPage->aDataEnd );
77656 u8 * const pCellBody = pCell - pPage->childPtrSize;
77658 pPage->xParseCell(pPage, pCellBody, &pCur->info);
77659 nCell = (int)pCur->info.nKey;
77664 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
77673 pCur->ix = (u16)idx;
77676 pCur->curFlags &= ~BTCF_ValidOvfl;
77685 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
77686 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
77691 upr = idx-1;
77696 pCur->ix = (u16)idx;
77697 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
77704 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
77705 assert( pPage->isInit );
77706 if( pPage->leaf ){
77707 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
77708 pCur->ix = (u16)idx;
77713 if( lwr>=pPage->nCell ){
77714 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
77719 /* This block is similar to an in-lined version of:
77721 ** pCur->ix = (u16)lwr;
77725 pCur->info.nSize = 0;
77726 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
77727 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
77730 pCur->aiIdx[pCur->iPage] = (u16)lwr;
77731 pCur->apPage[pCur->iPage] = pCur->pPage;
77732 pCur->ix = 0;
77733 pCur->iPage++;
77734 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
77736 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
77738 releasePage(pCur->pPage);
77742 pCur->pPage = pCur->apPage[--pCur->iPage];
77746 ***** End of in-lined moveToChild() call */
77749 pCur->info.nSize = 0;
77750 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
77767 return (CURSOR_VALID!=pCur->eState);
77780 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
77785 if( pCur->eState!=CURSOR_VALID ) return 0;
77786 if( NEVER(pCur->pPage->leaf==0) ) return -1;
77788 n = pCur->pPage->nCell;
77789 for(i=0; i<pCur->iPage; i++){
77790 n *= pCur->apPage[i]->nCell;
77821 if( pCur->eState!=CURSOR_VALID ){
77822 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
77827 if( CURSOR_INVALID==pCur->eState ){
77830 if( pCur->eState==CURSOR_SKIPNEXT ){
77831 pCur->eState = CURSOR_VALID;
77832 if( pCur->skipNext>0 ) return SQLITE_OK;
77836 pPage = pCur->pPage;
77837 idx = ++pCur->ix;
77838 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
77839 if( !pPage->isInit ){
77843 if( idx>=pPage->nCell ){
77844 if( !pPage->leaf ){
77845 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
77850 if( pCur->iPage==0 ){
77851 pCur->eState = CURSOR_INVALID;
77855 pPage = pCur->pPage;
77856 }while( pCur->ix>=pPage->nCell );
77857 if( pPage->intKey ){
77863 if( pPage->leaf ){
77874 pCur->info.nSize = 0;
77875 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
77876 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
77877 pPage = pCur->pPage;
77878 if( (++pCur->ix)>=pPage->nCell ){
77879 pCur->ix--;
77882 if( pPage->leaf ){
77914 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
77915 assert( pCur->info.nSize==0 );
77916 if( pCur->eState!=CURSOR_VALID ){
77921 if( CURSOR_INVALID==pCur->eState ){
77924 if( CURSOR_SKIPNEXT==pCur->eState ){
77925 pCur->eState = CURSOR_VALID;
77926 if( pCur->skipNext<0 ) return SQLITE_OK;
77930 pPage = pCur->pPage;
77931 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
77932 if( !pPage->isInit ){
77935 if( !pPage->leaf ){
77936 int idx = pCur->ix;
77941 while( pCur->ix==0 ){
77942 if( pCur->iPage==0 ){
77943 pCur->eState = CURSOR_INVALID;
77948 assert( pCur->info.nSize==0 );
77949 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
77951 pCur->ix--;
77952 pPage = pCur->pPage;
77953 if( pPage->intKey && !pPage->leaf ){
77965 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
77966 pCur->info.nSize = 0;
77967 if( pCur->eState!=CURSOR_VALID
77968 || pCur->ix==0
77969 || pCur->pPage->leaf==0
77973 pCur->ix--;
77994 ** anywhere on the free-list, then it is guaranteed to be returned. If
78014 assert( sqlite3_mutex_held(pBt->mutex) );
78015 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
78016 pPage1 = pBt->pPage1;
78018 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
78020 n = get4byte(&pPage1->aData[36]);
78021 testcase( n==mxPage-1 );
78028 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
78031 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
78032 ** shows that the page 'nearby' is somewhere on the free-list, then
78033 ** the entire-list will be searched for that page.
78040 assert( pBt->autoVacuum );
78052 /* Decrement the free-list count by 1. Set iTrunk to the index of the
78053 ** first free-list trunk page. iPrevTrunk is initially 1.
78055 rc = sqlite3PagerWrite(pPage1->pDbPage);
78057 put4byte(&pPage1->aData[36], n-1);
78060 ** is not true. Otherwise, it runs once for each trunk-page on the
78061 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
78067 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
78070 iTrunk = get4byte(&pPrevTrunk->aData[0]);
78072 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
78075 iTrunk = get4byte(&pPage1->aData[32]);
78079 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
78088 assert( pTrunk->aData!=0 );
78089 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
78091 k = get4byte(&pTrunk->aData[4]);
78097 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78102 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
78105 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
78106 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
78120 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78126 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
78128 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
78132 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
78136 ** pointers to free-list leaves. The first leaf becomes a trunk
78140 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
78150 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
78155 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
78156 put4byte(&pNewTrunk->aData[4], k-1);
78157 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
78160 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
78161 put4byte(&pPage1->aData[32], iNewTrunk);
78163 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
78167 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
78171 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
78177 unsigned char *aData = pTrunk->aData;
78191 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
78193 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
78218 *pPgno, closest+1, k, pTrunk->pgno, n-1));
78219 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78221 if( closest<k-1 ){
78224 put4byte(&aData[4], k-1);
78228 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
78245 ** pager layer with the 'no-content' flag set. This prevents the pager
78247 ** current transaction has already run one or more incremental-vacuum
78250 ** not set the no-content flag. This causes the pager to load and journal
78255 ** file on disk. So the effects of disabling the no-content optimization
78259 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
78261 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
78263 pBt->nPage++;
78264 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
78267 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
78268 /* If *pPgno refers to a pointer-map page, allocate two new pages
78270 ** becomes a new pointer-map page, the second is used by the caller.
78273 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
78274 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
78275 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
78277 rc = sqlite3PagerWrite(pPg->pDbPage);
78281 pBt->nPage++;
78282 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
78285 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
78286 *pPgno = pBt->nPage;
78291 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
78304 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
78305 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
78310 ** This function is used to add page iPage to the database file free-list.
78311 ** It is assumed that the page is not already a part of the free-list.
78322 MemPage *pTrunk = 0; /* Free-list trunk page */
78323 Pgno iTrunk = 0; /* Page number of free-list trunk page */
78324 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
78327 u32 nFree; /* Initial number of pages on free-list */
78329 assert( sqlite3_mutex_held(pBt->mutex) );
78331 assert( !pMemPage || pMemPage->pgno==iPage );
78333 if( iPage<2 || iPage>pBt->nPage ){
78338 sqlite3PagerRef(pPage->pDbPage);
78344 rc = sqlite3PagerWrite(pPage1->pDbPage);
78346 nFree = get4byte(&pPage1->aData[36]);
78347 put4byte(&pPage1->aData[36], nFree+1);
78349 if( pBt->btsFlags & BTS_SECURE_DELETE ){
78354 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
78358 memset(pPage->aData, 0, pPage->pBt->pageSize);
78361 /* If the database supports auto-vacuum, write an entry in the pointer-map
78369 /* Now manipulate the actual database free-list structure. There are two
78370 ** possibilities. If the free-list is currently empty, or if the first
78371 ** trunk page in the free-list is full, then this page will become a
78372 ** new free-list trunk page. Otherwise, it will become a leaf of the
78373 ** first trunk page in the current free-list. This block tests if it
78374 ** is possible to add the page as a new free-list leaf.
78379 iTrunk = get4byte(&pPage1->aData[32]);
78389 nLeaf = get4byte(&pTrunk->aData[4]);
78390 assert( pBt->usableSize>32 );
78391 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
78395 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
78400 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
78403 ** usableSize/4 - 8 entries will be reported as corrupt. In order
78405 ** we will continue to restrict the number of entries to usableSize/4 - 8
78408 ** to read "usableSize/4-2" instead of "usableSize/4-8".
78410 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
78415 rc = sqlite3PagerWrite(pTrunk->pDbPage);
78417 put4byte(&pTrunk->aData[4], nLeaf+1);
78418 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
78419 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
78420 sqlite3PagerDontWrite(pPage->pDbPage);
78424 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
78430 ** the page being freed as a leaf page of the first trunk in the free-list.
78431 ** Possibly because the free-list is empty, or possibly because the
78432 ** first trunk in the free-list is full. Either way, the page being freed
78433 ** will become the new first trunk page in the free-list.
78438 rc = sqlite3PagerWrite(pPage->pDbPage);
78442 put4byte(pPage->aData, iTrunk);
78443 put4byte(&pPage->aData[4], 0);
78444 put4byte(&pPage1->aData[32], iPage);
78445 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
78449 pPage->isInit = 0;
78457 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
78475 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78476 assert( pInfo->nLocal!=pInfo->nPayload );
78477 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
78478 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
78479 if( pCell + pInfo->nSize > pPage->aDataEnd ){
78483 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
78484 pBt = pPage->pBt;
78485 assert( pBt->usableSize > 4 );
78486 ovflPageSize = pBt->usableSize - 4;
78487 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
78489 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
78491 while( nOvfl-- ){
78506 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
78513 ** freePage2() may zero the page contents if secure-delete mode is
78524 sqlite3PagerUnref(pOvfl->pDbPage);
78539 pPage->xParseCell(pPage, pCell, &sInfo); \
78554 ** Note that pCell does not necessary need to point to the pPage->aData
78556 ** be constructed in this temporary area then copied into pPage->aData
78576 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78580 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
78581 || sqlite3PagerIswriteable(pPage->pDbPage) );
78584 nHeader = pPage->childPtrSize;
78585 if( pPage->intKey ){
78586 nPayload = pX->nData + pX->nZero;
78587 pSrc = pX->pData;
78588 nSrc = pX->nData;
78589 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
78591 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
78593 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
78594 nSrc = nPayload = (int)pX->nKey;
78595 pSrc = pX->pKey;
78601 if( nPayload<=pPage->maxLocal ){
78615 memset(pPayload+nSrc, 0, nPayload-nSrc);
78622 mn = pPage->minLocal;
78623 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
78624 testcase( n==pPage->maxLocal );
78625 testcase( n==pPage->maxLocal+1 );
78626 if( n > pPage->maxLocal ) n = mn;
78632 pBt = pPage->pBt;
78649 pPage->xParseCell(pPage, pCell, &info);
78650 assert( nHeader==(int)(info.pPayload - pCell) );
78651 assert( info.nKey==pX->nKey );
78664 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
78668 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
78669 || sqlite3PagerIswriteable(pPage->pDbPage) );
78679 nPayload -= n;
78683 nSrc -= n;
78684 spaceLeft -= n;
78688 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
78689 if( pBt->autoVacuum ){
78699 /* If the database supports auto-vacuum, and the second or subsequent
78700 ** overflow page is being allocated, add an entry to the pointer-map
78704 ** to the pointer-map. If we write nothing to this pointer-map slot,
78709 if( pBt->autoVacuum && rc==SQLITE_OK ){
78724 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
78728 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
78729 || sqlite3PagerIswriteable(pPage->pDbPage) );
78734 pPrior = pOvfl->aData;
78736 pPayload = &pOvfl->aData[4];
78737 spaceLeft = pBt->usableSize - 4;
78745 ** Remove the i-th cell from pPage. This routine effects pPage only.
78754 u8 *data; /* pPage->aData */
78761 assert( idx<pPage->nCell );
78763 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
78764 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78765 assert( pPage->nFree>=0 );
78766 data = pPage->aData;
78767 ptr = &pPage->aCellIdx[2*idx];
78768 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
78770 hdr = pPage->hdrOffset;
78772 testcase( pc+sz==pPage->pBt->usableSize );
78773 if( pc+sz > pPage->pBt->usableSize ){
78782 pPage->nCell--;
78783 if( pPage->nCell==0 ){
78786 put2byte(&data[hdr+5], pPage->pBt->usableSize);
78787 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
78788 - pPage->childPtrSize - 8;
78790 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
78791 put2byte(&data[hdr+3], pPage->nCell);
78792 pPage->nFree += 2;
78803 ** in pPage->apOvfl[] and make it point to the cell content (either
78805 ** Allocating a new entry in pPage->aCell[] implies that
78806 ** pPage->nOverflow is incremented.
78818 int i, /* New cell becomes the i-th cell of the page */
78822 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
78827 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
78829 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
78830 assert( MX_CELL(pPage->pBt)<=10921 );
78831 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
78832 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
78833 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
78834 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78835 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
78836 assert( pPage->nFree>=0 );
78838 if( pPage->nOverflow || sz+2>pPage->nFree ){
78844 j = pPage->nOverflow++;
78845 /* Comparison against ArraySize-1 since we hold back one extra slot
78848 assert( j < ArraySize(pPage->apOvfl)-1 );
78849 pPage->apOvfl[j] = pCell;
78850 pPage->aiOvfl[j] = (u16)i;
78857 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
78858 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
78860 int rc = sqlite3PagerWrite(pPage->pDbPage);
78864 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
78865 data = pPage->aData;
78866 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
78872 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
78873 assert( idx+sz <= (int)pPage->pBt->usableSize );
78874 pPage->nFree -= (u16)(2 + sz);
78880 memcpy(&data[idx+4], pCell+4, sz-4);
78882 pIns = pPage->aCellIdx + i*2;
78883 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
78885 pPage->nCell++;
78887 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
78888 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
78890 if( pPage->pBt->autoVacuum ){
78915 int i, /* New cell becomes the i-th cell of the page */
78922 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
78924 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
78925 assert( MX_CELL(pPage->pBt)<=10921 );
78926 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
78927 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
78928 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
78929 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
78930 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
78931 assert( pPage->nFree>=0 );
78932 assert( pPage->nOverflow==0 );
78933 if( sz+2>pPage->nFree ){
78934 j = pPage->nOverflow++;
78935 /* Comparison against ArraySize-1 since we hold back one extra slot
78938 assert( j < ArraySize(pPage->apOvfl)-1 );
78939 pPage->apOvfl[j] = pCell;
78940 pPage->aiOvfl[j] = (u16)i;
78947 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
78948 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
78950 int rc = sqlite3PagerWrite(pPage->pDbPage);
78954 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
78955 data = pPage->aData;
78956 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
78962 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
78963 assert( idx+sz <= (int)pPage->pBt->usableSize );
78964 pPage->nFree -= (u16)(2 + sz);
78966 pIns = pPage->aCellIdx + i*2;
78967 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
78969 pPage->nCell++;
78971 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
78972 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
78974 if( pPage->pBt->autoVacuum ){
79000 ** tunable - as if you could change them and recompile and it would all work.
79015 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
79027 ** -----------
79029 ** -----------
79032 ** --------- --------- ---------
79033 ** |Child-1| |Child-2| |Child-3|
79034 ** --------- --------- ---------
79038 ** 1. All cells from Child-1 in order
79040 ** 3. All cells from Child-2 in order
79042 ** 5. All cells from Child-3 in order
79044 ** For a table-btree (with rowids) the items 2 and 4 are empty because
79048 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
79052 ** ixNx[0] = Number of cells in Child-1.
79053 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
79054 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
79055 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
79058 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
79061 ** ixNx[0] = Number of cells in Child-1.
79062 ** ixNx[1] = Number of cells in Child-1 and Child-2.
79081 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
79085 MemPage *pRef = p->pRef;
79086 u16 *szCell = p->szCell;
79087 assert( idx>=0 && idx+N<=p->nCell );
79089 assert( p->apCell[idx]!=0 );
79091 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
79094 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
79097 N--;
79105 assert( N>=0 && N<p->nCell );
79106 assert( p->szCell[N]==0 );
79107 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
79108 return p->szCell[N];
79111 assert( N>=0 && N<p->nCell );
79112 if( p->szCell[N] ) return p->szCell[N];
79117 ** Array apCell[] contains pointers to nCell b-tree page cells. The
79135 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
79136 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
79137 const int usableSize = pPg->pBt->usableSize;
79142 u8 *pCellptr = pPg->aCellIdx;
79143 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
79145 int k; /* Current slot in pCArray->apEnd[] */
79146 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
79152 memcpy(&pTmp[j], &aData[j], usableSize - j);
79154 assert( pCArray->ixNx[NB*2-1]>i );
79155 for(k=0; pCArray->ixNx[k]<=i; k++){}
79156 pSrcEnd = pCArray->apEnd[k];
79160 u8 *pCell = pCArray->apCell[i];
79161 u16 sz = pCArray->szCell[i];
79165 pCell = &pTmp[pCell - aData];
79172 pData -= sz;
79173 put2byte(pCellptr, (pData - aData));
79177 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
79180 if( pCArray->ixNx[k]<=i ){
79182 pSrcEnd = pCArray->apEnd[k];
79186 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
79188 pPg->nCell = (u16)nCell;
79189 pPg->nOverflow = 0;
79192 put2byte(&aData[hdr+3], pPg->nCell);
79193 put2byte(&aData[hdr+5], pData - aData);
79199 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
79202 ** will fit), non-zero is returned. Otherwise, if the cells are added
79205 ** Argument pCellptr points to the first entry in the cell-pointer array
79207 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
79209 ** that it is safe to overwrite this part of the cell-pointer array.
79217 ** end of the space required by this page for the cell-pointer area (for
79218 ** all cells - not just those inserted by the current call). If the content
79220 ** cells in apCell[], then the cells do not fit and non-zero is returned.
79224 u8 *pBegin, /* End of cell-pointer array */
79225 u8 **ppData, /* IN/OUT: Page content-area pointer */
79226 u8 *pCellptr, /* Pointer to cell-pointer area */
79231 int i = iFirst; /* Loop counter - cell index to insert */
79232 u8 *aData = pPg->aData; /* Complete page */
79235 int k; /* Current slot in pCArray->apEnd[] */
79237 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
79239 assert( pCArray->ixNx[NB*2-1]>i );
79240 for(k=0; pCArray->ixNx[k]<=i ; k++){}
79241 pEnd = pCArray->apEnd[k];
79245 assert( pCArray->szCell[i]!=0 );
79246 sz = pCArray->szCell[i];
79248 if( (pData - pBegin)<sz ) return 1;
79249 pData -= sz;
79252 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
79255 assert( (pSlot+sz)<=pCArray->apCell[i]
79256 || pSlot>=(pCArray->apCell[i]+sz)
79258 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
79259 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
79265 memmove(pSlot, pCArray->apCell[i], sz);
79266 put2byte(pCellptr, (pSlot - aData));
79270 if( pCArray->ixNx[k]<=i ){
79272 pEnd = pCArray->apEnd[k];
79280 ** The pCArray object contains pointers to b-tree cells and their sizes.
79283 ** that is currently stored within the body of pPg to the pPg free-list.
79284 ** The cell-pointers and other fields of the page are not updated.
79286 ** This function returns the total number of cells added to the free-list.
79294 u8 * const aData = pPg->aData;
79295 u8 * const pEnd = &aData[pPg->pBt->usableSize];
79296 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
79305 u8 *pCell = pCArray->apCell[i];
79313 sz = pCArray->szCell[i]; assert( sz>0 );
79314 iOfst = (u16)(pCell - aData);
79328 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
79341 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
79348 ** balanced. The current page, pPg, has pPg->nCell cells starting with
79349 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
79355 ** The pPg->nFree field is invalid when this function returns. It is the
79365 u8 * const aData = pPg->aData;
79366 const int hdr = pPg->hdrOffset;
79367 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
79368 int nCell = pPg->nCell; /* Cells stored on pPg */
79372 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
79376 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
79377 memcpy(pTmp, aData, pPg->pBt->usableSize);
79383 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
79385 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
79386 nCell -= nShift;
79389 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
79391 nCell -= nTail;
79396 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
79400 int nAdd = MIN(nNew,iOld-iNew);
79401 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
79403 pCellptr = pPg->aCellIdx;
79413 for(i=0; i<pPg->nOverflow; i++){
79414 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
79416 pCellptr = &pPg->aCellIdx[iCell * 2];
79418 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
79431 pCellptr = &pPg->aCellIdx[nCell*2];
79434 iNew+nCell, nNew-nCell, pCArray
79441 pPg->nCell = (u16)nNew;
79442 pPg->nOverflow = 0;
79444 put2byte(&aData[hdr+3], pPg->nCell);
79445 put2byte(&aData[hdr+5], pData - aData);
79449 u8 *pCell = pCArray->apCell[i+iNew];
79450 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
79451 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
79452 pCell = &pTmp[pCell - aData];
79455 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
79471 ** a new entry is being inserted on the extreme right-end of the
79475 ** Instead of trying to balance the 3 right-most leaf pages, just add
79476 ** a new page to the right-hand side and put the one new entry in
79482 ** pPage is the leaf page which is the right-most page in the tree.
79484 ** which is also the right-most entry on the page.
79488 ** byte page number followed by a variable length integer. In other
79493 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
79498 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
79499 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79500 assert( pPage->nOverflow==1 );
79502 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
79503 assert( pPage->nFree>=0 );
79504 assert( pParent->nFree>=0 );
79506 /* Allocate a new page. This page will become the right-sibling of
79515 u8 *pCell = pPage->apOvfl[0];
79516 u16 szCell = pPage->xCellSize(pPage, pCell);
79520 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
79521 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
79527 b.apEnd[0] = pPage->aDataEnd;
79529 b.ixNx[NB*2-1] = 0x7fffffff;
79535 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
79537 /* If this is an auto-vacuum database, update the pointer map
79547 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
79548 if( szCell>pNew->minLocal ){
79554 ** consists of a 4-byte page number (the page number of pPage) and
79555 ** a variable length key value (which must be the same value as the
79558 ** To find the largest key value on pPage, first find the right-most
79560 ** record-length (a variable length integer at most 32-bits in size)
79561 ** and the key value (a variable length integer, may have any value).
79562 ** The first of the while(...) loops below skips over the record-length
79566 pCell = findCell(pPage, pPage->nCell-1);
79574 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
79575 0, pPage->pgno);
79578 /* Set the right-child pointer of pParent to point to the new page. */
79579 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
79593 ** for setting pointer-map entries.
79601 BtShared *pBt = pPage->pBt;
79602 assert( pPage->isInit );
79604 for(j=0; j<pPage->nCell; j++){
79609 pPage->xParseCell(pPage, z, &info);
79611 Pgno ovfl = get4byte(&z[info.nSize-4]);
79613 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
79615 if( !pPage->leaf ){
79618 assert( n==pPage->pgno && e==PTRMAP_BTREE );
79621 if( !pPage->leaf ){
79622 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
79624 assert( n==pPage->pgno && e==PTRMAP_BTREE );
79632 ** This function is used to copy the contents of the b-tree node stored
79634 ** the pointer-map entries for each child page are updated so that the
79650 BtShared * const pBt = pFrom->pBt;
79651 u8 * const aFrom = pFrom->aData;
79652 u8 * const aTo = pTo->aData;
79653 int const iFromHdr = pFrom->hdrOffset;
79654 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
79659 assert( pFrom->isInit );
79660 assert( pFrom->nFree>=iToHdr );
79661 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
79663 /* Copy the b-tree node content from page pFrom to page pTo. */
79665 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
79666 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
79673 pTo->isInit = 0;
79681 /* If this is an auto-vacuum database, update the pointer-map entries
79682 ** for any b-tree or overflow pages that pTo now contains the pointers to.
79724 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
79733 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
79734 int isRoot, /* True if pParent is a root-page */
79742 int nxDiv; /* Next divider slot in pParent->aCell[] */
79747 int pageFlags; /* Value of pPage->aData[0] */
79753 u8 *pRight; /* Location in parent of right-sibling pointer */
79754 u8 *apDiv[NB-1]; /* Divider cells in pParent */
79755 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
79757 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
79765 assert( sizeof(b) - sizeof(b.ixNx) == offsetof(CellArray,ixNx) );
79766 memset(&b, 0, sizeof(b)-sizeof(b.ixNx[0]));
79767 b.ixNx[NB*2-1] = 0x7fffffff;
79768 pBt = pParent->pBt;
79769 assert( sqlite3_mutex_held(pBt->mutex) );
79770 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
79777 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
79778 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
79783 assert( pParent->nFree>=0 );
79796 i = pParent->nOverflow + pParent->nCell;
79804 nxDiv = i-2+bBulk;
79806 nxDiv = iParentIdx-1;
79808 i = 2-bBulk;
79811 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
79812 pRight = &pParent->aData[pParent->hdrOffset+8];
79814 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
79825 if( apOld[i]->nFree<0 ){
79832 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
79833 if( (i--)==0 ) break;
79835 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
79836 apDiv[i] = pParent->apOvfl[0];
79838 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
79839 pParent->nOverflow = 0;
79841 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
79843 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
79852 ** But not if we are in secure-delete mode. In secure-delete mode,
79857 if( pBt->btsFlags & BTS_FAST_SECURE ){
79862 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
79863 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
79865 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
79868 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
79872 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
79882 + pBt->pageSize; /* aSpace1 */
79884 assert( szScratch<=7*(int)pBt->pageSize );
79911 leafCorrection = b.pRef->leaf*4;
79912 leafData = b.pRef->intKeyLeaf;
79915 int limit = pOld->nCell;
79916 u8 *aData = pOld->aData;
79917 u16 maskPage = pOld->maskPage;
79918 u8 *piCell = aData + pOld->cellOffset;
79922 /* Verify that all sibling pages are of the same "type" (table-leaf,
79923 ** table-interior, index-leaf, or index-interior).
79925 if( pOld->aData[0]!=apOld[0]->aData[0] ){
79947 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
79948 if( pOld->nOverflow>0 ){
79949 if( NEVER(limit<pOld->aiOvfl[0]) ){
79953 limit = pOld->aiOvfl[0];
79959 for(k=0; k<pOld->nOverflow; k++){
79960 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
79961 b.apCell[b.nCell] = pOld->apOvfl[k];
79965 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
79972 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
79975 if( i<nOld-1 && !leafData){
79982 assert( sz<=pBt->maxLocal+23 );
79983 assert( iSpace1 <= (int)pBt->pageSize );
79987 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
79988 if( !pOld->leaf ){
79990 assert( pOld->hdrOffset==0 || CORRUPT_DB );
79993 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
80000 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
80012 ** size of all cells on the i-th page and cntNew[] which is the index
80019 ** szNew[i]: Spaced used on the i-th sibling page.
80021 ** the right of the i-th sibling page.
80025 usableSpace = pBt->usableSize - 12 + leafCorrection;
80028 b.apEnd[k] = p->aDataEnd;
80030 if( k && b.ixNx[k]==b.ixNx[k-1] ){
80031 k--; /* Omit b.ixNx[] entry for child pages with no cells */
80035 b.apEnd[k] = pParent->aDataEnd;
80038 assert( p->nFree>=0 );
80039 szNew[i] = usableSpace - p->nFree;
80040 for(j=0; j<p->nOverflow; j++){
80041 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
80052 szNew[k-1] = 0;
80053 cntNew[k-1] = b.nCell;
80055 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
80056 szNew[i] -= sz;
80065 cntNew[i]--;
80079 szNew[i+1] -= sz;
80083 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
80092 ** always nearly full, while the right-most sibling might be nearly empty.
80096 ** This adjustment is more than an optimization. The packing above might
80097 ** be so out of balance as to be illegal. For example, the right-most
80098 ** sibling might be completely empty. This adjustment is not optional.
80100 for(i=k-1; i>0; i--){
80102 int szLeft = szNew[i-1]; /* Size of sibling on the left */
80103 int r; /* Index of right-most cell in left sibling */
80106 r = cntNew[i-1] - 1;
80107 d = r + 1 - leafData;
80116 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
80120 szLeft -= szR + 2;
80121 cntNew[i-1] = r;
80122 r--;
80123 d--;
80126 szNew[i-1] = szLeft;
80127 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
80133 /* Sanity check: For a non-corrupt database file one of the following
80140 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
80142 apOld[0]->pgno, apOld[0]->nCell,
80143 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
80144 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
80150 pageFlags = apOld[0]->aData[0];
80156 rc = sqlite3PagerWrite(pNew->pDbPage);
80158 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
80173 /* Set the pointer-map entry for the new sibling page. */
80175 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
80196 aPgno[i] = apNew[i]->pgno;
80197 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
80198 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
80200 for(i=0; i<nNew-1; i++){
80203 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
80212 Pgno pgnoA = apNew[i]->pgno;
80213 Pgno pgnoB = apNew[iB]->pgno;
80214 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
80215 u16 fgA = apNew[i]->pDbPage->flags;
80216 u16 fgB = apNew[iB]->pDbPage->flags;
80217 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
80218 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
80219 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
80220 apNew[i]->pgno = pgnoB;
80221 apNew[iB]->pgno = pgnoA;
80227 apNew[0]->pgno, szNew[0], cntNew[0],
80228 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
80229 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
80230 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
80231 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
80232 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
80233 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
80234 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
80235 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
80238 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
80240 assert( apNew[nNew-1]!=0 );
80241 put4byte(pRight, apNew[nNew-1]->pgno);
80243 /* If the sibling pages are not leaves, ensure that the right-child pointer
80244 ** of the right-most new sibling page is set to the value that was
80245 ** originally in the same field of the right-most old sibling page. */
80247 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
80248 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
80263 ** associated with the right-child of each sibling may also need to be
80270 int cntOldNext = pNew->nCell + pNew->nOverflow;
80281 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
80295 || pNew->pgno!=aPgno[iOld]
80296 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
80299 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
80301 if( cachedCellSize(&b,i)>pNew->minLocal ){
80310 for(i=0; i<nNew-1; i++){
80323 if( !pNew->leaf ){
80324 memcpy(&pNew->aData[8], pCell, 4);
80326 /* If the tree is a leaf-data tree, and the siblings are leaves,
80328 ** cell consists of the integer key for the right-most cell of
80329 ** the sibling-page assembled above only.
80332 j--;
80333 pNew->xParseCell(pNew, b.apCell[j], &info);
80338 pCell -= 4;
80339 /* Obscure case for non-leaf-data trees: If the cell at pCell was
80346 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
80352 sz = pParent->xCellSize(pParent, pCell);
80356 assert( sz<=pBt->maxLocal+23 );
80357 assert( iOvflSpace <= (int)pBt->pageSize );
80358 assert( b.ixNx[NB*2-1]>j );
80365 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
80367 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
80374 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
80376 ** the left-hand sibling apNew[iPg-1] has been updated.
80380 ** the right-hand sibling apNew[iPg+1] has been updated.
80384 ** The iPg value in the following loop starts at nNew-1 goes down
80385 ** to 0, then back up to nNew-1 again, thus making two passes over
80392 for(i=1-nNew; i<nNew; i++){
80393 int iPg = i<0 ? -i : i;
80399 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
80406 ** only after iPg-1 has already been updated. */
80407 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
80417 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
80418 iNew = cntNew[iPg-1] + !leafData;
80419 nNewCell = cntNew[iPg] - iNew;
80425 apNew[iPg]->nFree = usableSpace-szNew[iPg];
80426 assert( apNew[iPg]->nOverflow==0 );
80427 assert( apNew[iPg]->nCell==nNewCell );
80437 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
80438 /* The root page of the b-tree now contains no cells. The only sibling
80439 ** page is the right-child of the parent. Copy the contents of the
80441 ** b-tree structure by one. This is described as the "balance-shallower"
80442 ** sub-algorithm in some documentation.
80444 ** If this is an auto-vacuum database, the call to copyNodeContent()
80445 ** sets all pointer-map entries corresponding to database image pages
80454 rc = defragmentPage(apNew[0], -1);
80456 assert( apNew[0]->nFree ==
80457 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
80458 - apNew[0]->nCell*2)
80464 /* Fix the pointer map entries associated with the right-child of each
80468 u32 key = get4byte(&apNew[i]->aData[8]);
80469 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
80473 assert( pParent->isInit );
80484 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
80511 ** This function is called when the root page of a b-tree structure is
80516 ** page is then overwritten to make it an empty page with the right-child
80519 ** Before returning, all pointer-map entries corresponding to pages
80520 ** that the new child-page now contains pointers to are updated. The
80521 ** entry corresponding to the new right-child pointer of the root
80533 BtShared *pBt = pRoot->pBt; /* The BTree */
80535 assert( pRoot->nOverflow>0 );
80536 assert( sqlite3_mutex_held(pBt->mutex) );
80538 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
80539 ** page that will become the new right-child of pPage. Copy the contents
80542 rc = sqlite3PagerWrite(pRoot->pDbPage);
80544 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
80547 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
80555 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
80556 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
80557 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
80559 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
80562 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
80563 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
80564 memcpy(pChild->apOvfl, pRoot->apOvfl,
80565 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
80566 pChild->nOverflow = pRoot->nOverflow;
80568 /* Zero the contents of pRoot. Then install pChild as the right-child. */
80569 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
80570 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
80578 ** on the same B-tree as pCur.
80581 ** pointing to the same b-tree. If an insert occurs on one SQL table
80583 ** table linked to the same b-tree. If the secondary insert causes a
80589 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
80591 && pOther->eState==CURSOR_VALID
80592 && pOther->pPage==pCur->pPage
80594 return SQLITE_CORRUPT_PAGE(pCur->pPage);
80620 MemPage *pPage = pCur->pPage;
80622 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
80623 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
80629 }else if( (iPage = pCur->iPage)==0 ){
80630 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
80631 /* The root page of the b-tree is overfull. In this case call the
80632 ** balance_deeper() function to create a new child for the root-page
80633 ** and copy the current contents of the root-page to it. The
80634 ** next iteration of the do-loop will balance the child page.
80638 rc = balance_deeper(pPage, &pCur->apPage[1]);
80640 pCur->iPage = 1;
80641 pCur->ix = 0;
80642 pCur->aiIdx[0] = 0;
80643 pCur->apPage[0] = pPage;
80644 pCur->pPage = pCur->apPage[1];
80645 assert( pCur->pPage->nOverflow );
80650 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
80656 MemPage * const pParent = pCur->apPage[iPage-1];
80657 int const iIdx = pCur->aiIdx[iPage-1];
80659 rc = sqlite3PagerWrite(pParent->pDbPage);
80660 if( rc==SQLITE_OK && pParent->nFree<0 ){
80665 if( pPage->intKeyLeaf
80666 && pPage->nOverflow==1
80667 && pPage->aiOvfl[0]==pPage->nCell
80668 && pParent->pgno!=1
80669 && pParent->nCell==iIdx
80674 ** happens, the next iteration of the do-loop will balance pParent
80693 ** become overfull or underfull. The next iteration of the do-loop
80698 ** A subsequent iteration of the do-loop will deal with this by
80700 ** but it doesn't deal with overflow cells - just moves them to a
80707 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
80709 pCur->hints&BTREE_BULKLOAD);
80725 pPage->nOverflow = 0;
80727 /* The next iteration of the do-loop balances the parent page. */
80729 pCur->iPage--;
80730 assert( pCur->iPage>=0 );
80731 pCur->pPage = pCur->apPage[pCur->iPage];
80751 int nData = pX->nData - iOffset;
80757 int rc = sqlite3PagerWrite(pPage->pDbPage);
80759 memset(pDest + i, 0, iAmt - i);
80766 iAmt-nData);
80770 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
80771 int rc = sqlite3PagerWrite(pPage->pDbPage);
80777 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
80792 int iOffset; /* Next byte of pX->pData to write */
80793 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
80795 MemPage *pPage = pCur->pPage; /* Page being written */
80800 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
80803 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
80804 0, pCur->info.nLocal);
80808 iOffset = pCur->info.nLocal;
80811 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
80812 pBt = pPage->pBt;
80813 ovflPageSize = pBt->usableSize - 4;
80817 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
80821 ovflPgno = get4byte(pPage->aData);
80823 ovflPageSize = nTotal - iOffset;
80825 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
80828 sqlite3PagerUnref(pPage->pDbPage);
80840 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
80841 MemPage *pPage = pCur->pPage; /* Page being written */
80843 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
80844 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
80848 if( pCur->info.nLocal==nTotal ){
80850 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
80851 0, pCur->info.nLocal);
80874 ** If the seekResult parameter is non-zero, then a successful call to
80885 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
80886 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
80896 int loc = seekResult; /* -1: before desired location +1: after */
80900 Btree *p = pCur->pBtree;
80905 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
80909 ** In some cases, the call to btreeMoveto() below is a no-op. For
80910 ** example, when inserting data into a table with auto-generated integer
80913 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
80918 if( pCur->curFlags & BTCF_Multiple ){
80919 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
80921 if( loc && pCur->iPage<0 ){
80927 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
80934 if( pCur->eState>=CURSOR_REQUIRESEEK ){
80935 testcase( pCur->eState==CURSOR_REQUIRESEEK );
80936 testcase( pCur->eState==CURSOR_FAULT );
80942 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
80943 && p->pBt->inTransaction==TRANS_WRITE
80944 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
80945 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
80948 ** expecting an index b-tree, then the caller should be inserting blob
80952 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
80954 if( pCur->pKeyInfo==0 ){
80955 assert( pX->pKey==0 );
80956 /* If this is an insert into a table b-tree, invalidate any incrblob
80958 if( p->hasIncrblobCur ){
80959 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
80967 assert( pCur->curFlags & BTCF_ValidNKey );
80968 assert( pX->nKey==pCur->info.nKey );
80977 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
80980 assert( pX->nData>=0 && pX->nZero>=0 );
80981 if( pCur->info.nSize!=0
80982 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
80993 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
81011 if( pX->nMem ){
81013 r.pKeyInfo = pCur->pKeyInfo;
81014 r.aMem = pX->aMem;
81015 r.nField = pX->nMem;
81020 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
81032 if( pCur->info.nKey==pX->nKey ){
81034 x2.pData = pX->pKey;
81035 x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff );
81041 assert( pCur->eState==CURSOR_VALID
81042 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
81044 pPage = pCur->pPage;
81045 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
81046 assert( pPage->leaf || !pPage->intKey );
81047 if( pPage->nFree<0 ){
81048 if( NEVER(pCur->eState>CURSOR_INVALID) ){
81049 /* ^^^^^--- due to the moveToRoot() call above */
81058 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
81060 assert( pPage->isInit || CORRUPT_DB );
81061 newCell = p->pBt->pTmpSpace;
81066 szNew = p->pBt->nPreformatSize;
81071 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
81073 pPage->xParseCell(pPage, newCell, &info);
81075 Pgno ovfl = get4byte(&newCell[szNew-4]);
81076 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
81084 assert( szNew==pPage->xCellSize(pPage, newCell) );
81085 assert( szNew <= MX_CELL_SIZE(p->pBt) );
81086 idx = pCur->ix;
81087 pCur->info.nSize = 0;
81091 if( idx>=pPage->nCell ){
81094 rc = sqlite3PagerWrite(pPage->pDbPage);
81099 if( !pPage->leaf ){
81103 testcase( pCur->curFlags & BTCF_ValidOvfl );
81106 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
81116 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
81118 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
81121 if( oldCell+szNew > pPage->aDataEnd ){
81129 }else if( loc<0 && pPage->nCell>0 ){
81130 assert( pPage->leaf );
81131 idx = ++pCur->ix;
81132 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
81134 assert( pPage->leaf );
81137 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
81138 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
81152 ** multiple records into an intkey b-tree using a single cursor (as can
81155 ** the b-tree if possible. If the cursor is left pointing to the last
81160 if( pPage->nOverflow ){
81162 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
81169 pCur->pPage->nOverflow = 0;
81170 pCur->eState = CURSOR_INVALID;
81173 if( pCur->pKeyInfo ){
81174 assert( pCur->pKey==0 );
81175 pCur->pKey = sqlite3Malloc( pX->nKey );
81176 if( pCur->pKey==0 ){
81179 memcpy(pCur->pKey, pX->pKey, pX->nKey);
81182 pCur->eState = CURSOR_REQUIRESEEK;
81183 pCur->nKey = pX->nKey;
81186 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
81208 BtShared *pBt = pDest->pBt;
81209 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
81215 if( pSrc->info.nPayload<0x80 ){
81216 *(aOut++) = (u8)pSrc->info.nPayload;
81218 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
81220 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
81221 nIn = pSrc->info.nLocal;
81222 aIn = pSrc->info.pPayload;
81223 if( aIn+nIn>pSrc->pPage->aDataEnd ){
81224 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
81226 nRem = pSrc->info.nPayload;
81227 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
81229 pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
81233 Pager *pSrcPager = pSrc->pBt->pPager;
81240 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
81241 pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
81242 if( nOut<pSrc->info.nPayload ){
81244 pBt->nPreformatSize += 4;
81248 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
81249 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
81251 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
81255 nRem -= nOut;
81261 nOut -= nCopy;
81262 nIn -= nCopy;
81274 nIn = pSrc->pBt->usableSize - 4;
81285 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
81290 pPgnoOut = pPageOut->aData;
81293 nOut = MIN(pBt->usableSize - 4, nRem);
81317 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
81322 Btree *p = pCur->pBtree;
81323 BtShared *pBt = p->pBt;
81333 assert( pBt->inTransaction==TRANS_WRITE );
81334 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
81335 assert( pCur->curFlags & BTCF_WriteFlag );
81336 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
81337 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
81339 if( pCur->eState!=CURSOR_VALID ){
81340 if( pCur->eState>=CURSOR_REQUIRESEEK ){
81342 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
81343 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
81345 return SQLITE_CORRUPT_PGNO(pCur->pgnoRoot);
81348 assert( pCur->eState==CURSOR_VALID );
81350 iCellDepth = pCur->iPage;
81351 iCellIdx = pCur->ix;
81352 pPage = pCur->pPage;
81353 if( pPage->nCell<=iCellIdx ){
81357 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
81360 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
81366 ** will cause a b-tree rebalance, then this is done by saving the cursor
81382 if( !pPage->leaf
81383 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
81384 (int)(pBt->usableSize*2/3)
81385 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
81387 /* A b-tree rebalance will be required after deleting this entry.
81401 ** sub-tree headed by the child page of the cell being deleted. This makes
81403 if( !pPage->leaf ){
81411 if( pCur->curFlags & BTCF_Multiple ){
81412 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
81416 /* If this is a delete operation to remove a row from a table b-tree,
81418 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
81419 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
81425 rc = sqlite3PagerWrite(pPage->pDbPage);
81432 ** is currently pointing to the largest entry in the sub-tree headed
81433 ** by the child-page of the cell that was just deleted from an internal
81436 if( !pPage->leaf ){
81437 MemPage *pLeaf = pCur->pPage;
81442 if( pLeaf->nFree<0 ){
81446 if( iCellDepth<pCur->iPage-1 ){
81447 n = pCur->apPage[iCellDepth+1]->pgno;
81449 n = pCur->pPage->pgno;
81451 pCell = findCell(pLeaf, pLeaf->nCell-1);
81452 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_PAGE(pLeaf);
81453 nCell = pLeaf->xCellSize(pLeaf, pCell);
81455 pTmp = pBt->pTmpSpace;
81457 rc = sqlite3PagerWrite(pLeaf->pDbPage);
81459 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
81461 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
81480 assert( pCur->pPage->nOverflow==0 );
81481 assert( pCur->pPage->nFree>=0 );
81482 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
81484 ** then balance() will always be a no-op. No need to invoke it. */
81489 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
81490 releasePageNotNull(pCur->pPage);
81491 pCur->iPage--;
81492 while( pCur->iPage>iCellDepth ){
81493 releasePage(pCur->apPage[pCur->iPage--]);
81495 pCur->pPage = pCur->apPage[pCur->iPage];
81501 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
81502 assert( pPage==pCur->pPage || CORRUPT_DB );
81503 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
81504 pCur->eState = CURSOR_SKIPNEXT;
81505 if( iCellIdx>=pPage->nCell ){
81506 pCur->skipNext = -1;
81507 pCur->ix = pPage->nCell-1;
81509 pCur->skipNext = 1;
81515 pCur->eState = CURSOR_REQUIRESEEK;
81535 BtShared *pBt = p->pBt;
81539 int ptfFlags; /* Page-type flags for the root page of new table */
81542 assert( pBt->inTransaction==TRANS_WRITE );
81543 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
81551 if( pBt->autoVacuum ){
81552 Pgno pgnoMove; /* Move a page here to make room for the root-page */
81557 ** out to be an overflow page, delete all overflow page-map caches
81563 ** root page of the new table should go. meta[3] is the largest root-page
81564 ** created so far, so the new root-page is (meta[3]+1).
81572 /* The new root-page may not be allocated on a pointer-map page, or the
81591 /* pgnoRoot is the page that will be used for the root-page of
81635 rc = sqlite3PagerWrite(pRoot->pDbPage);
81644 /* Update the pointer-map and meta-data with the new root-page number. */
81655 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
81667 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
81674 sqlite3PagerUnref(pRoot->pDbPage);
81675 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
81704 assert( sqlite3_mutex_held(pBt->mutex) );
81710 if( (pBt->openFlags & BTREE_SINGLE)==0
81711 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
81716 hdr = pPage->hdrOffset;
81717 for(i=0; i<pPage->nCell; i++){
81719 if( !pPage->leaf ){
81726 if( !pPage->leaf ){
81727 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
81729 if( pPage->intKey ) pnChange = 0;
81732 testcase( !pPage->intKey );
81733 *pnChange += pPage->nCell;
81737 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
81738 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
81760 BtShared *pBt = p->pBt;
81762 assert( p->inTrans==TRANS_WRITE );
81768 ** is the root of a table b-tree - if it is not, the following call is
81769 ** a no-op). */
81770 if( p->hasIncrblobCur ){
81785 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
81811 BtShared *pBt = p->pBt;
81814 assert( p->inTrans==TRANS_WRITE );
81834 if( pBt->autoVacuum ){
81839 /* If the table being dropped is the table with the largest root-page
81848 /* The table being dropped does not have the largest root-page
81850 ** gap left by the deleted root-page.
81873 /* Set the new 'max-root-page' value in the database header. This
81875 ** be a root-page number, less one again if that is the
81878 maxRootPgno--;
81881 maxRootPgno--;
81903 ** This function may only be called if the b-tree connection already
81906 ** Read the meta-information out of a database file. Meta[0]
81909 ** is read-only, the others are read/write.
81923 BtShared *pBt = p->pBt;
81926 assert( p->inTrans>TRANS_NONE );
81928 assert( pBt->pPage1 );
81932 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
81934 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
81937 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
81938 ** database, mark the database as read-only. */
81941 pBt->btsFlags |= BTS_READ_ONLY;
81949 ** Write meta-information back into the database. Meta[0] is
81950 ** read-only and may not be written.
81953 BtShared *pBt = p->pBt;
81958 assert( p->inTrans==TRANS_WRITE );
81959 assert( pBt->pPage1!=0 );
81960 pP1 = pBt->pPage1->aData;
81961 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
81966 assert( pBt->autoVacuum || iMeta==0 );
81968 pBt->incrVacuum = (u8)iMeta;
81977 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
81978 ** number of entries in the b-tree and write the result to *pnEntry.
81995 ** page in the B-Tree structure (not including overflow pages).
81997 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
81999 MemPage *pPage; /* Current page of the b-tree */
82001 /* If this is a leaf page or the tree is not an int-key tree, then
82005 pPage = pCur->pPage;
82006 if( pPage->leaf || !pPage->intKey ){
82007 nEntry += pPage->nCell;
82013 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
82015 ** to visit is the right-child of its parent.
82020 if( pPage->leaf ){
82022 if( pCur->iPage==0 ){
82023 /* All pages of the b-tree have been visited. Return successfully. */
82028 }while ( pCur->ix>=pCur->pPage->nCell );
82030 pCur->ix++;
82031 pPage = pCur->pPage;
82035 ** points at. This is the right-child if (iIdx==pPage->nCell).
82037 iIdx = pCur->ix;
82038 if( iIdx==pPage->nCell ){
82039 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
82054 return p->pBt->pPager;
82062 pCheck->rc = SQLITE_NOMEM;
82063 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
82064 if( pCheck->nErr==0 ) pCheck->nErr++;
82072 sqlite3 *db = pCheck->db;
82073 if( AtomicLoad(&db->u1.isInterrupted) ){
82074 pCheck->rc = SQLITE_INTERRUPT;
82075 pCheck->nErr++;
82076 pCheck->mxErr = 0;
82079 if( db->xProgress ){
82080 assert( db->nProgressOps>0 );
82081 pCheck->nStep++;
82082 if( (pCheck->nStep % db->nProgressOps)==0
82083 && db->xProgress(db->pProgressArg)
82085 pCheck->rc = SQLITE_INTERRUPT;
82086 pCheck->nErr++;
82087 pCheck->mxErr = 0;
82103 if( !pCheck->mxErr ) return;
82104 pCheck->mxErr--;
82105 pCheck->nErr++;
82107 if( pCheck->errMsg.nChar ){
82108 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
82110 if( pCheck->zPfx ){
82111 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
82112 pCheck->v0, pCheck->v1, pCheck->v2);
82114 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
82116 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
82125 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
82129 assert( pCheck->aPgRef!=0 );
82130 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
82131 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
82138 assert( pCheck->aPgRef!=0 );
82139 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
82140 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
82146 ** reference to the page, add an error message to pCheck->zErrMsg.
82153 if( iPage>pCheck->nCkPage || iPage==0 ){
82167 ** Check that the entry in the pointer-map for page iChild maps to
82181 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
82208 int nErrAtStart = pCheck->nErr;
82209 while( iPage!=0 && pCheck->mxErr ){
82213 N--;
82214 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
82222 if( pCheck->pBt->autoVacuum ){
82226 if( n>pCheck->pBt->usableSize/4-2 ){
82229 N--;
82234 if( pCheck->pBt->autoVacuum ){
82240 N -= n;
82245 /* If this database supports auto-vacuum and iPage is not the last
82246 ** page in this overflow list, check that the pointer-map entry for
82249 if( pCheck->pBt->autoVacuum && N>0 ){
82258 if( N && nErrAtStart==pCheck->nErr ){
82261 isFreeList ? "size" : "overflow list length",
82262 expected-N, expected);
82268 ** An implementation of a min-heap.
82278 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
82307 aHeap[0]--;
82344 int depth = -1, d2; /* Depth of a subtree */
82360 u32 *heap = 0; /* Min-heap used for checking cell coverage */
82361 u32 x, prev = 0; /* Next and previous entry on the min-heap */
82362 const char *saved_zPfx = pCheck->zPfx;
82363 int saved_v1 = pCheck->v1;
82364 int saved_v2 = pCheck->v2;
82370 if( pCheck->mxErr==0 ) goto end_of_check;
82371 pBt = pCheck->pBt;
82372 usableSize = pBt->usableSize;
82375 pCheck->zPfx = "Tree %u page %u: ";
82376 pCheck->v1 = iPage;
82380 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
82386 savedIsInit = pPage->isInit;
82387 pPage->isInit = 0;
82399 data = pPage->aData;
82400 hdr = pPage->hdrOffset;
82403 pCheck->zPfx = "Tree %u page %u cell %u: ";
82407 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
82410 assert( pPage->nCell==nCell );
82411 if( pPage->leaf || pPage->intKey==0 ){
82412 pCheck->nRow += nCell;
82415 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
82416 ** immediately follows the b-tree page header. */
82417 cellStart = hdr + 12 - 4*pPage->leaf;
82418 assert( pPage->aCellIdx==&data[cellStart] );
82419 pCellIdx = &data[cellStart + 2*(nCell-1)];
82421 if( !pPage->leaf ){
82422 /* Analyze the right-child page of internal pages */
82425 if( pBt->autoVacuum ){
82426 pCheck->zPfx = "Tree %u page %u right child: ";
82435 heap = pCheck->heap;
82439 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
82441 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
82445 pCheck->v2 = i;
82448 pCellIdx -= 2;
82449 if( pc<contentOffset || pc>usableSize-4 ){
82451 pc, contentOffset, usableSize-4);
82456 pPage->xParseCell(pPage, pCell, &info);
82464 if( pPage->intKey ){
82476 assert( pc + info.nSize - 4 <= usableSize );
82477 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
82478 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
82480 if( pBt->autoVacuum ){
82487 if( !pPage->leaf ){
82491 if( pBt->autoVacuum ){
82502 /* Populate the coverage-checking heap for leaf pages */
82503 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
82510 pCheck->zPfx = 0;
82511 if( doCoverageCheck && pCheck->mxErr>0 ){
82512 /* For leaf pages, the min-heap has already been initialized and the
82515 if( !pPage->leaf ){
82516 heap = pCheck->heap;
82518 for(i=nCell-1; i>=0; i--){
82521 size = pPage->xCellSize(pPage, &data[pc]);
82522 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
82526 /* Add the freeblocks to the min-heap
82528 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
82535 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
82538 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
82539 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
82540 ** big-endian integer which is the offset in the b-tree page of the next
82544 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
82547 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
82550 /* Analyze the min-heap looking for overlap between cells and/or
82553 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
82558 ** The loop below pulls entries from the min-heap in order and compares
82564 prev = contentOffset - 1; /* Implied first min-heap entry */
82571 nFrag += (x>>16) - (prev&0xffff) - 1;
82575 nFrag += usableSize - (prev&0xffff) - 1;
82576 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
82577 ** is stored in the fifth field of the b-tree page header.
82578 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
82589 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
82591 pCheck->zPfx = saved_zPfx;
82592 pCheck->v1 = saved_v1;
82593 pCheck->v2 = saved_v2;
82604 ** A read-only or read-write transaction must be opened before calling
82609 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
82613 ** root pages is incomplete. This is a "partial integrity-check". This
82633 BtShared *pBt = p->pBt;
82634 u64 savedDbFlags = pBt->db->flags;
82651 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
82652 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
82657 sCheck.pPager = pBt->pPager;
82671 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
82684 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
82685 get4byte(&pBt->pPage1->aData[36]));
82693 if( pBt->autoVacuum ){
82697 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
82704 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
82711 testcase( pBt->db->flags & SQLITE_CellSizeCk );
82712 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
82718 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
82727 pBt->db->flags = savedDbFlags;
82738 /* If the database supports auto-vacuum, make sure no tables contain
82739 ** references to pointer-map pages.
82742 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
82746 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
82766 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
82774 ** an empty string if the database is in-memory or a TEMP database.
82780 assert( p->pBt->pPager!=0 );
82781 return sqlite3PagerFilename(p->pBt->pPager, 1);
82793 assert( p->pBt->pPager!=0 );
82794 return sqlite3PagerJournalname(p->pBt->pPager);
82802 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
82803 return p ? p->inTrans : 0;
82811 ** transaction on the shared-cache the argument Btree is connected to.
82818 BtShared *pBt = p->pBt;
82820 if( pBt->inTransaction!=TRANS_NONE ){
82823 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
82836 assert( sqlite3_mutex_held(p->db->mutex) );
82837 return p->nBackup!=0;
82842 ** a single shared-btree. The memory is used by client code for its own
82843 ** purposes (for example, to store a high-level schema associated with
82844 ** the shared-btree). The btree layer manages reference counting issues.
82846 ** The first time this is called on a shared-btree, nBytes bytes of memory
82855 ** Just before the shared-btree is closed, the function passed as the
82861 BtShared *pBt = p->pBt;
82864 if( !pBt->pSchema && nBytes ){
82865 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
82866 pBt->xFreeSchema = xFree;
82869 return pBt->pSchema;
82879 assert( sqlite3_mutex_held(p->db->mutex) );
82896 assert( p->inTrans!=TRANS_NONE );
82897 if( p->sharable ){
82920 ** change the length of the data stored. If this function is called with
82927 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
82928 assert( pCsr->curFlags & BTCF_Incrblob );
82934 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
82935 if( pCsr->eState!=CURSOR_VALID ){
82941 ** version of the b-tree page modified by the accessPayload call below.
82947 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
82953 ** (c) the connection holds a write-lock on the table (if required),
82954 ** (d) there are no conflicting read-locks, and
82957 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
82960 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
82961 && pCsr->pBt->inTransaction==TRANS_WRITE );
82962 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
82963 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
82964 assert( pCsr->pPage->intKey );
82973 pCur->curFlags |= BTCF_Incrblob;
82974 pCur->pBtree->hasIncrblobCur = 1;
82984 BtShared *pBt = pBtree->pBt;
82992 pBt->btsFlags &= ~BTS_NO_WAL;
82993 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
82997 u8 *aData = pBt->pPage1->aData;
83001 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
83010 pBt->btsFlags &= ~BTS_NO_WAL;
83019 return (pCsr->hints & mask)!=0;
83023 ** Return true if the given Btree is read-only.
83026 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
83035 ** If no transaction is active and the database is not a temp-db, clear
83036 ** the in-memory pager cache.
83039 BtShared *pBt = p->pBt;
83040 if( pBt->inTransaction==TRANS_NONE ){
83041 sqlite3PagerClearCache(pBt->pPager);
83050 return p->sharable;
83059 testcase( p->sharable );
83060 return p->pBt->nRef;
83088 Btree *pDest; /* Destination b-tree file */
83090 int bDestLocked; /* True once a write-transaction is open on pDest */
83094 Btree *pSrc; /* Source b-tree file */
83112 ** structure may be accessed via two groups of thread-safe entry points:
83126 ** backup_pagecount() are not thread-safe functions. If they are called
83134 ** Non-sharable Btrees (in-memory databases for example), do not have
83170 return pDb->aDb[i].pBt;
83179 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
83184 ** Check that there is no open read-transaction on the b-tree passed as the
83186 ** is an open read-transaction, return SQLITE_ERROR and leave an error
83228 sqlite3_mutex_enter(pSrcDb->mutex);
83229 sqlite3_mutex_enter(pDestDb->mutex);
83238 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
83249 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
83250 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
83251 p->pDestDb = pDestDb;
83252 p->pSrcDb = pSrcDb;
83253 p->iNext = 1;
83254 p->isAttached = 0;
83256 if( 0==p->pSrc || 0==p->pDest
83257 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
83269 p->pSrc->nBackup++;
83272 sqlite3_mutex_leave(pDestDb->mutex);
83273 sqlite3_mutex_leave(pSrcDb->mutex);
83297 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
83298 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
83299 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
83305 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
83306 assert( p->bDestLocked );
83307 assert( !isFatalError(p->rc) );
83308 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
83316 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
83319 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
83337 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
83349 ** this function is a no-op.
83369 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
83370 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
83371 p->pNext = *pp;
83373 p->isAttached = 1;
83377 ** Copy nPage pages from the source b-tree to the destination.
83388 sqlite3_mutex_enter(p->pSrcDb->mutex);
83389 sqlite3BtreeEnter(p->pSrc);
83390 if( p->pDestDb ){
83391 sqlite3_mutex_enter(p->pDestDb->mutex);
83394 rc = p->rc;
83396 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
83397 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
83399 int nSrcPage = -1; /* Size of source db in pages */
83402 /* If the source pager is currently in a write-transaction, return
83405 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
83411 /* If there is no open read-transaction on the source database, open
83415 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
83416 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
83426 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
83431 if( SQLITE_OK==rc && p->bDestLocked==0
83432 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
83433 (int*)&p->iDestSchema))
83435 p->bDestLocked = 1;
83440 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
83441 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
83442 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
83450 /* Now that there is a read-lock on the source database, query the
83453 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
83455 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
83456 const Pgno iSrcPg = p->iNext; /* Source page number */
83457 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
83465 p->iNext++;
83468 p->nPagecount = nSrcPage;
83469 p->nRemaining = nSrcPage+1-p->iNext;
83470 if( p->iNext>(Pgno)nSrcPage ){
83472 }else if( !p->isAttached ){
83478 ** is to make sure that the schema-version really does change in
83484 rc = sqlite3BtreeNewDb(p->pDest);
83488 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
83491 if( p->pDestDb ){
83492 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
83495 rc = sqlite3BtreeSetVersion(p->pDest, 2);
83512 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
83513 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
83516 nDestTruncate = (nSrcPage+ratio-1)/ratio;
83517 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
83518 nDestTruncate--;
83526 /* If the source page-size is smaller than the destination page-size,
83532 ** pending-byte page in the source database may need to be
83545 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
83557 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
83601 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
83611 ** "committing" a read-only transaction cannot fail.
83615 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
83616 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
83623 p->rc = rc;
83625 if( p->pDestDb ){
83626 sqlite3_mutex_leave(p->pDestDb->mutex);
83628 sqlite3BtreeLeave(p->pSrc);
83629 sqlite3_mutex_leave(p->pSrcDb->mutex);
83643 pSrcDb = p->pSrcDb;
83644 sqlite3_mutex_enter(pSrcDb->mutex);
83645 sqlite3BtreeEnter(p->pSrc);
83646 if( p->pDestDb ){
83647 sqlite3_mutex_enter(p->pDestDb->mutex);
83651 if( p->pDestDb ){
83652 p->pSrc->nBackup--;
83654 if( p->isAttached ){
83655 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
83658 pp = &(*pp)->pNext;
83661 *pp = p->pNext;
83665 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
83668 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
83669 if( p->pDestDb ){
83670 sqlite3Error(p->pDestDb, rc);
83673 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
83675 sqlite3BtreeLeave(p->pSrc);
83676 if( p->pDestDb ){
83677 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
83697 return p->nRemaining;
83711 return p->nPagecount;
83733 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
83734 if( !isFatalError(p->rc) && iPage<p->iNext ){
83740 assert( p->pDestDb );
83741 sqlite3_mutex_enter(p->pDestDb->mutex);
83743 sqlite3_mutex_leave(p->pDestDb->mutex);
83746 p->rc = rc;
83749 }while( (p = p->pNext)!=0 );
83768 for(p=pBackup; p; p=p->pNext){
83769 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
83770 p->iNext = 1;
83792 if( pFd->pMethods ){
83805 b.pSrcDb = pFrom->db;
83814 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
83821 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
83859 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
83872 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
83878 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
83881 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
83883 if( p->flags & MEM_Null ){
83885 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
83892 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
83895 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
83896 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
83897 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
83900 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
83908 assert( (p->flags & MEM_Cleared)==0 );
83912 assert( p->szMalloc==0
83913 || (p->flags==MEM_Undefined
83914 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))
83915 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));
83925 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
83927 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
83928 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
83929 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
83930 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
83943 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
83945 if( p->flags & MEM_Int ){
83947 /* Work-around for GCC bug
83950 assert( (p->flags&MEM_Int)*2==sizeof(x) );
83951 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
83952 p->n = sqlite3Int64ToText(x, zBuf);
83954 p->n = sqlite3Int64ToText(p->u.i, zBuf);
83959 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
83962 p->n = acc.nChar;
83979 ** https://sqlite.org/src/info/343634942dd54ab (2018-01-31) for
83994 if( (p->flags & MEM_Str)==0 ) return 1;
83995 if( p->db && p->db->mallocFailed ) return 1;
83996 if( p->flags & MEM_Term ){
83997 /* Insure that the string is properly zero-terminated. Pay particular
83998 ** attention to the case where p->n is odd */
83999 if( p->szMalloc>0 && p->z==p->zMalloc ){
84000 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
84001 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
84003 assert( p->z[p->n]==0 );
84004 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
84005 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
84007 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
84010 z = p->z;
84013 if( p->enc!=SQLITE_UTF8 ){
84015 if( p->enc==SQLITE_UTF16BE ) z++;
84032 ** routine is a no-op.
84046 if( !(pMem->flags&MEM_Str) ){
84047 pMem->enc = desiredEnc;
84050 if( pMem->enc==desiredEnc ){
84053 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84063 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
84064 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
84070 ** Make sure pMem->z points to a writable allocation of at least n bytes.
84073 ** pMem->z into the new allocation. pMem must be either a string or
84075 ** in pMem->z is discarded.
84080 testcase( pMem->db==0 );
84084 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
84085 testcase( bPreserve && pMem->z==0 );
84087 assert( pMem->szMalloc==0
84088 || (pMem->flags==MEM_Undefined
84089 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))
84090 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));
84091 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
84092 if( pMem->db ){
84093 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
84095 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
84096 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
84097 pMem->z = pMem->zMalloc;
84101 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
84102 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
84104 if( pMem->zMalloc==0 ){
84106 pMem->z = 0;
84107 pMem->szMalloc = 0;
84110 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
84113 if( bPreserve && pMem->z ){
84114 assert( pMem->z!=pMem->zMalloc );
84115 memcpy(pMem->zMalloc, pMem->z, pMem->n);
84117 if( (pMem->flags&MEM_Dyn)!=0 ){
84118 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
84119 pMem->xDel((void *)(pMem->z));
84122 pMem->z = pMem->zMalloc;
84123 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
84128 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
84129 ** If pMem->zMalloc already meets or exceeds the requested size, this
84130 ** routine is a no-op.
84133 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
84142 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
84143 if( pMem->szMalloc<szNew ){
84146 assert( (pMem->flags & MEM_Dyn)==0 );
84147 pMem->z = pMem->zMalloc;
84148 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
84153 ** If pMem is already a string, detect if it is a zero-terminated
84157 ** this routine is a no-op.
84160 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){
84164 if( pMem->enc!=SQLITE_UTF8 ) return;
84165 assert( pMem->z!=0 );
84166 if( pMem->flags & MEM_Dyn ){
84167 if( pMem->xDel==sqlite3_free
84168 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)
84170 pMem->z[pMem->n] = 0;
84171 pMem->flags |= MEM_Term;
84174 if( pMem->xDel==sqlite3RCStrUnref ){
84175 /* Blindly assume that all RCStr objects are zero-terminated */
84176 pMem->flags |= MEM_Term;
84179 }else if( pMem->szMalloc >= pMem->n+1 ){
84180 pMem->z[pMem->n] = 0;
84181 pMem->flags |= MEM_Term;
84191 ** to be a double-zero byte at an even byte boundary in order to
84196 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
84199 pMem->z[pMem->n] = 0;
84200 pMem->z[pMem->n+1] = 0;
84201 pMem->z[pMem->n+2] = 0;
84202 pMem->flags |= MEM_Term;
84214 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84216 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
84218 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
84223 pMem->flags &= ~MEM_Ephem;
84225 pMem->pScopyFrom = 0;
84232 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
84239 assert( pMem->flags & MEM_Zero );
84240 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
84243 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84246 nByte = pMem->n + pMem->u.nZero;
84248 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
84254 assert( pMem->z!=0 );
84255 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );
84257 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
84258 pMem->n += pMem->u.nZero;
84259 pMem->flags &= ~(MEM_Zero|MEM_Term);
84269 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84270 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
84271 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
84272 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
84297 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84298 assert( !(pMem->flags&MEM_Zero) );
84299 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
84300 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
84306 pMem->enc = 0;
84310 vdbeMemRenderNum(nByte, pMem->z, pMem);
84311 assert( pMem->z!=0 );
84312 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );
84313 pMem->enc = SQLITE_UTF8;
84314 pMem->flags |= MEM_Str|MEM_Term;
84315 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
84333 assert( pMem->db!=0 );
84334 assert( pFunc->xFinalize!=0 );
84335 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
84336 assert( sqlite3_mutex_held(pMem->db->mutex) );
84340 t.db = pMem->db;
84345 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
84346 assert( (pMem->flags & MEM_Dyn)==0 );
84347 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
84364 assert( pFunc->xValue!=0 );
84365 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
84366 assert( pAccum->db!=0 );
84367 assert( sqlite3_mutex_held(pAccum->db->mutex) );
84373 ctx.enc = ENC(pAccum->db);
84374 pFunc->xValue(&ctx);
84389 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
84391 if( p->flags&MEM_Agg ){
84392 sqlite3VdbeMemFinalize(p, p->u.pDef);
84393 assert( (p->flags & MEM_Agg)==0 );
84394 testcase( p->flags & MEM_Dyn );
84396 if( p->flags&MEM_Dyn ){
84397 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
84398 p->xDel((void *)p->z);
84400 p->flags = MEM_Null;
84405 ** by p->xDel and memory in p->zMalloc.
84415 if( p->szMalloc ){
84416 sqlite3DbFreeNN(p->db, p->zMalloc);
84417 p->szMalloc = 0;
84419 p->z = 0;
84434 if( VdbeMemDynamic(p) || p->szMalloc ){
84444 if( p->szMalloc ) vdbeMemClear(p);
84451 ** a floating-point then the value returned is the integer part.
84454 ** an SQL-NULL value, return 0.
84460 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
84466 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84468 flags = pMem->flags;
84471 return pMem->u.i;
84473 return sqlite3RealToI64(pMem->u.r);
84474 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
84490 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
84495 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84497 if( pMem->flags & MEM_Real ){
84498 return pMem->u.r;
84499 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
84500 testcase( pMem->flags & MEM_IntReal );
84501 return (double)pMem->u.i;
84502 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
84515 testcase( pMem->flags & MEM_IntReal );
84516 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
84517 if( pMem->flags & MEM_Null ) return ifNull;
84527 assert( pMem->flags & (MEM_Real|MEM_IntReal) );
84529 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84532 if( pMem->flags & MEM_IntReal ){
84535 i64 ix = sqlite3RealToI64(pMem->u.r);
84539 ** (1) the round-trip conversion real->int->real is a no-op, and
84547 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
84548 pMem->u.i = ix;
84559 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84563 pMem->u.i = sqlite3VdbeIntValue(pMem);
84574 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84577 pMem->u.r = sqlite3VdbeRealValue(pMem);
84587 ** For some versions of GCC on 32-bit machines, if you do the more obvious
84589 ** though the r1 and (double)i values are bit-for-bit the same.
84595 && i >= -2251799813685248LL && i < 2251799813685248LL);
84603 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;
84618 testcase( pMem->flags & MEM_Int );
84619 testcase( pMem->flags & MEM_Real );
84620 testcase( pMem->flags & MEM_IntReal );
84621 testcase( pMem->flags & MEM_Null );
84622 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
84625 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
84626 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
84627 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
84628 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
84629 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))
84631 pMem->u.i = ix;
84637 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
84638 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
84650 if( pMem->flags & MEM_Null ) return SQLITE_OK;
84653 if( (pMem->flags & MEM_Blob)==0 ){
84655 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
84656 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
84658 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
84678 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
84680 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
84681 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
84682 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;
84698 pMem->flags = flags;
84699 pMem->db = db;
84700 pMem->szMalloc = 0;
84720 pMem->flags = MEM_Null;
84728 ** Delete any previous value and set the value to be a BLOB of length
84734 pMem->flags = MEM_Blob|MEM_Zero;
84735 pMem->n = 0;
84737 pMem->u.nZero = n;
84738 pMem->enc = SQLITE_UTF8;
84739 pMem->z = 0;
84747 assert( pMem->z!=0 );
84748 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );
84749 memset(pMem->z, 0, nByte);
84750 pMem->n = n>0?n:0;
84751 pMem->flags = MEM_Blob;
84752 pMem->enc = SQLITE_UTF8;
84760 ** a 64-bit integer.
84764 pMem->u.i = val;
84765 pMem->flags = MEM_Int;
84776 pMem->u.i = val;
84777 pMem->flags = MEM_Int;
84788 /* A no-op destructor */
84801 assert( pMem->flags==MEM_Null );
84803 pMem->u.zPType = zPType ? zPType : "";
84804 pMem->z = pPtr;
84805 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
84806 pMem->eSubtype = 'p';
84807 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
84818 pMem->u.r = val;
84819 pMem->flags = MEM_Real;
84830 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
84831 && pMem->xDel==sqlite3RowSetDelete;
84843 sqlite3 *db = pMem->db;
84850 pMem->z = (char*)p;
84851 pMem->flags = MEM_Blob|MEM_Dyn;
84852 pMem->xDel = sqlite3RowSetDelete;
84858 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
84861 assert( p->db!=0 );
84862 if( p->flags & (MEM_Str|MEM_Blob) ){
84863 int n = p->n;
84864 if( p->flags & MEM_Zero ){
84865 n += p->u.nZero;
84867 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
84878 ** This is used for testing and debugging only - to help ensure that shallow
84884 if( pMem->bScopy ){
84885 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
84886 if( pX->pScopyFrom==pMem ){
84888 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
84890 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
84898 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
84899 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
84902 ** undefined so that we can quickly detect the shallow-copy error */
84903 pX->flags = MEM_Undefined;
84904 pX->pScopyFrom = 0;
84907 pMem->bScopy = 0;
84909 pMem->pScopyFrom = 0;
84915 ** pTo are freed. The pFrom->z field is not duplicated. If
84916 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
84926 assert( pTo->db==pFrom->db );
84929 if( (pFrom->flags&MEM_Static)==0 ){
84930 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
84932 pTo->flags |= srcType;
84946 pTo->flags &= ~MEM_Dyn;
84947 if( pTo->flags&(MEM_Str|MEM_Blob) ){
84948 if( 0==(pFrom->flags&MEM_Static) ){
84949 pTo->flags |= MEM_Ephem;
84964 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
84965 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
84966 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
84970 pFrom->flags = MEM_Null;
84971 pFrom->szMalloc = 0;
84994 ** non-negative for blobs.
85003 i64 nByte = n; /* New value for pMem->n */
85005 u16 flags; /* New value for pMem->flags */
85008 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
85018 if( pMem->db ){
85019 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
85040 sqlite3DbFree(pMem->db, (void*)z);
85046 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
85064 memcpy(pMem->z, z, nAlloc);
85067 pMem->z = (char *)z;
85069 pMem->zMalloc = pMem->z;
85070 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
85072 pMem->xDel = xDel;
85077 pMem->n = (int)(nByte & 0x7fffffff);
85078 pMem->flags = flags;
85079 pMem->enc = enc;
85098 ** pMem->zMalloc to hold the content from the btree, if possible. New
85099 ** pMem->zMalloc space will be allocated if necessary. The calling routine
85113 pMem->flags = MEM_Null;
85118 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
85120 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
85121 pMem->flags = MEM_Blob;
85122 pMem->n = (int)amt;
85143 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
85144 assert( pMem->z!=0 );
85147 pMem->flags = MEM_Blob|MEM_Ephem;
85148 pMem->n = (int)amt;
85159 ** to a zero-terminated version of that string.
85163 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
85166 assert( (pVal->flags & (MEM_Null))==0 );
85167 if( pVal->flags & (MEM_Blob|MEM_Str) ){
85169 pVal->flags |= MEM_Str;
85170 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
85173 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
85174 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
85179 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
85182 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
85184 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
85185 || pVal->db->mallocFailed );
85186 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
85188 return pVal->z;
85200 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
85206 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
85209 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
85211 return pVal->z;
85213 if( pVal->flags&MEM_Null ){
85223 ** that third-party extensions can get access to it?
85227 && ALWAYS((pVal->flags & (MEM_Str|MEM_Blob))!=0)
85228 && (pVal->flags & MEM_Dyn)!=0
85229 && pVal->xDel==xFree
85243 p->flags = MEM_Null;
85244 p->db = db;
85265 ** Otherwise, if the second argument is non-zero, then this function is
85274 UnpackedRecord *pRec = p->ppRec[0];
85277 Index *pIdx = p->pIdx; /* Index being probed */
85280 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
85285 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
85286 if( pRec->pKeyInfo ){
85287 assert( pRec->pKeyInfo->nAllField==nCol );
85288 assert( pRec->pKeyInfo->enc==ENC(db) );
85289 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
85291 pRec->aMem[i].flags = MEM_Null;
85292 pRec->aMem[i].db = db;
85300 p->ppRec[0] = pRec;
85303 pRec->nField = p->iVal+1;
85304 sqlite3VdbeMemSetNull(&pRec->aMem[p->iVal]);
85305 return &pRec->aMem[p->iVal];
85352 assert( (p->flags & EP_TokenOnly)==0 );
85354 pList = p->x.pList;
85355 if( pList ) nVal = pList->nExpr;
85357 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
85362 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
85363 || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
85375 rc = sqlite3Stat4ValueFromExpr(pCtx->pParse, pList->a[i].pExpr, aff,
85391 pFunc->xSFunc(&ctx, nVal, apVal);
85394 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
85401 pCtx->pParse->nErr++;
85408 pCtx->pParse->rc = rc;
85450 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
85451 if( op==TK_REGISTER ) op = pExpr->op2;
85457 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
85462 aff = sqlite3AffinityType(pExpr->u.zToken,0);
85463 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
85469 /* zero-blobs only come from functions, not literal values. And
85480 ** case when the value is -9223372036854775808. Except - do not do this
85483 Expr *pLeft = pExpr->pLeft;
85484 if( (pLeft->op==TK_INTEGER || pLeft->op==TK_FLOAT) ){
85486 || pLeft->u.zToken[0]!='0' || (pLeft->u.zToken[1] & ~0x20)!='X'
85489 op = pExpr->op;
85490 negInt = -1;
85491 zNeg = "-";
85500 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
85503 if( op==TK_INTEGER && 0==sqlite3DecOrHexToI64(pExpr->u.zToken, &iVal) ){
85506 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
85508 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
85513 assert( pVal && pVal->z && pVal->flags==(MEM_Str|MEM_Term) );
85514 sqlite3AtoF(pVal->z, &pVal->u.r, pVal->n, SQLITE_UTF8);
85515 pVal->flags = MEM_Real;
85517 /* This case is required by -9223372036854775808 and other strings
85525 assert( (pVal->flags & MEM_IntReal)==0 );
85526 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
85527 testcase( pVal->flags & MEM_Int );
85528 testcase( pVal->flags & MEM_Real );
85529 pVal->flags &= ~MEM_Str;
85535 /* This branch happens for multiple negative signs. Ex: -(-5) */
85536 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
85540 if( pVal->flags & MEM_Real ){
85541 pVal->u.r = -pVal->u.r;
85542 }else if( pVal->u.i==SMALLEST_INT64 ){
85544 pVal->u.r = -(double)SMALLEST_INT64;
85546 pVal->u.r = LARGEST_INT64;
85550 pVal->u.i = -pVal->u.i;
85563 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
85564 assert( pExpr->u.zToken[1]=='\'' );
85567 zVal = &pExpr->u.zToken[2];
85568 nVal = sqlite3Strlen30(zVal)-1;
85583 pVal->flags = MEM_Int;
85584 pVal->u.i = pExpr->u.zToken[4]==0;
85594 if( pCtx==0 || NEVER(pCtx->pParse->nErr==0) )
85655 sqlite3 *db = pParse->db;
85660 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
85666 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
85668 int iBindVar = pExpr->iColumn;
85669 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
85670 if( (v = pParse->pReprepare)!=0 ){
85673 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
85675 pVal->db = pParse->db;
85682 assert( pVal==0 || pVal->db==db );
85733 if( pExpr==0 || pExpr->op!=TK_SELECT ){
85744 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
85776 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
85818 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
85819 pMem->enc = ENC(db);
85831 int nCol = pRec->pKeyInfo->nAllField;
85832 Mem *aMem = pRec->aMem;
85837 sqlite3KeyInfoUnref(pRec->pKeyInfo);
85848 int n, /* Length of string z */
85862 sqlite3DbFreeNN(((Mem*)v)->db, v);
85871 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
85875 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
85876 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
85877 return p->n;
85879 if( (p->flags & MEM_Str)!=0 && enc!=SQLITE_UTF8 && pVal->enc!=SQLITE_UTF8 ){
85880 return p->n;
85882 if( (p->flags & MEM_Blob)!=0 ){
85883 if( p->flags & MEM_Zero ){
85884 return p->n + p->u.nZero;
85886 return p->n;
85889 if( p->flags & MEM_Null ) return 0;
85920 sqlite3 *db = pParse->db;
85924 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
85925 p->db = db;
85926 if( db->pVdbe ){
85927 db->pVdbe->ppVPrev = &p->pVNext;
85929 p->pVNext = db->pVdbe;
85930 p->ppVPrev = &db->pVdbe;
85931 db->pVdbe = p;
85932 assert( p->eVdbeState==VDBE_INIT_STATE );
85933 p->pParse = pParse;
85934 pParse->pVdbe = p;
85935 assert( pParse->aLabel==0 );
85936 assert( pParse->nLabel==0 );
85937 assert( p->nOpAlloc==0 );
85938 assert( pParse->szOpAlloc==0 );
85947 return p->pParse;
85955 sqlite3DbFree(p->db, p->zErrMsg);
85957 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
85966 p->prepFlags = prepFlags;
85968 p->expmask = 0;
85970 assert( p->zSql==0 );
85971 p->zSql = sqlite3DbStrNDup(p->db, z, n);
85976 ** Add a new element to the Vdbe->pDblStr list.
85982 sizeof(*pStr)+n+1-sizeof(pStr->z));
85984 pStr->pNextStr = p->pDblStr;
85985 p->pDblStr = pStr;
85986 memcpy(pStr->z, z, n+1);
85994 ** zId of length nId is a double-quoted identifier. Check to see if
85999 const char *zId /* The double-quoted identifier, already dequoted */
86003 if( pVdbe->pDblStr==0 ) return 0;
86004 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
86005 if( strcmp(zId, pStr->z)==0 ) return 1;
86012 ** Swap byte-code between two VDBE structures.
86024 assert( pA->db==pB->db );
86028 pTmp = pA->pVNext;
86029 pA->pVNext = pB->pVNext;
86030 pB->pVNext = pTmp;
86031 ppTmp = pA->ppVPrev;
86032 pA->ppVPrev = pB->ppVPrev;
86033 pB->ppVPrev = ppTmp;
86034 zTmp = pA->zSql;
86035 pA->zSql = pB->zSql;
86036 pB->zSql = zTmp;
86038 zTmp = pA->zNormSql;
86039 pA->zNormSql = pB->zNormSql;
86040 pB->zNormSql = zTmp;
86042 pB->expmask = pA->expmask;
86043 pB->prepFlags = pA->prepFlags;
86044 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
86045 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
86053 ** If an out-of-memory error occurs while resizing the array, return
86060 Parse *p = v->pParse;
86062 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
86070 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
86071 : (sqlite3_int64)v->nOpAlloc+nOp);
86073 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
86079 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
86080 sqlite3OomFault(p->db);
86085 assert( nNew>=(v->nOpAlloc+nOp) );
86086 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
86088 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
86089 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
86090 v->aOp = pNew;
86122 assert( p->nOpAlloc<=p->nOp );
86124 assert( p->nOpAlloc>p->nOp );
86136 if( p->db->mallocFailed==0 ){
86137 VdbeOp *pOp = &p->aOp[addr];
86138 pOp->p4type = P4_INT32;
86139 pOp->p4.i = p4;
86170 i = p->nOp;
86171 assert( p->eVdbeState==VDBE_INIT_STATE );
86173 if( p->nOpAlloc<=i ){
86176 assert( p->aOp!=0 );
86177 p->nOp++;
86178 pOp = &p->aOp[i];
86180 pOp->opcode = (u8)op;
86181 pOp->p5 = 0;
86182 pOp->p1 = p1;
86183 pOp->p2 = p2;
86184 pOp->p3 = p3;
86185 pOp->p4.p = 0;
86186 pOp->p4type = P4_NOTUSED;
86191 pOp->zComment = 0;
86194 pOp->nExec = 0;
86195 pOp->nCycle = 0;
86198 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86199 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
86200 test_addop_breakpoint(i, &p->aOp[i]);
86204 pOp->iSrcLine = 0;
86222 i = p->nOp;
86223 if( p->nOpAlloc<=i ){
86226 p->nOp++;
86227 pOp = &p->aOp[i];
86229 pOp->opcode = (u8)op;
86230 pOp->p5 = 0;
86231 pOp->p1 = p1;
86232 pOp->p2 = p2;
86233 pOp->p3 = p3;
86234 pOp->p4.i = p4;
86235 pOp->p4type = P4_INT32;
86240 pOp->zComment = 0;
86243 pOp->nExec = 0;
86244 pOp->nCycle = 0;
86247 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86248 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
86249 test_addop_breakpoint(i, &p->aOp[i]);
86253 pOp->iSrcLine = 0;
86341 Vdbe *v = pParse->pVdbe;
86345 pCtx = sqlite3DbMallocRawNN(pParse->db, SZ_CONTEXT(nArg));
86347 assert( pParse->db->mallocFailed );
86348 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
86351 pCtx->pOut = 0;
86352 pCtx->pFunc = (FuncDef*)pFunc;
86353 pCtx->pVdbe = 0;
86354 pCtx->isError = 0;
86355 pCtx->argc = nArg;
86356 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
86389 if( pParse->addrExplain==0 ) return 0;
86390 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
86391 return pOp->p2;
86416 if( pParse->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
86424 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
86426 v = pParse->pVdbe;
86427 iThis = v->nOp;
86428 addr = sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
86430 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetLastOp(v)->p4.z);
86432 pParse->addrExplain = iThis;
86434 sqlite3VdbeScanStatus(v, iThis, -1, -1, 0, 0);
86444 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
86460 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
86461 sqlite3MayAbort(p->pParse);
86464 /* Insert the end of a co-routine
86470 ** co-routine has its own independent set of registers, because co-routines
86472 ** that could cause problems if two or more co-routines are using the same
86475 v->pParse->nTempReg = 0;
86476 v->pParse->nRangeReg = 0;
86488 ** always negative and P2 values are suppose to be non-negative.
86495 ** Parse.aLabel[x] Stores the address that the x-th label resolves
86497 ** labels stores -1, but that is not required.
86505 return --pParse->nLabel;
86514 int nNewSize = 10 - p->nLabel;
86515 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
86516 nNewSize*sizeof(p->aLabel[0]));
86517 if( p->aLabel==0 ){
86518 p->nLabelAlloc = 0;
86522 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
86524 if( nNewSize>=100 && (nNewSize/100)>(p->nLabelAlloc/100) ){
86527 p->nLabelAlloc = nNewSize;
86528 p->aLabel[j] = v->nOp;
86532 Parse *p = v->pParse;
86534 assert( v->eVdbeState==VDBE_INIT_STATE );
86535 assert( j<-p->nLabel );
86538 if( p->db->flags & SQLITE_VdbeAddopTrace ){
86539 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
86542 if( p->nLabelAlloc + p->nLabel < 0 ){
86545 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
86546 p->aLabel[j] = v->nOp;
86562 for(i=1; ALWAYS(i<p->nOp); i++){
86563 if( ALWAYS(p->aOp[i].opcode==OP_Expire) ){
86564 p->aOp[1].opcode = OP_Noop;
86574 ** in a Vdbe main program and each of the sub-programs (triggers) it may
86585 ** sqlite3DbFree(v->db, sIter.apSub);
86594 int iSub; /* 0 = main program, 1 = first sub-program etc. */
86597 Vdbe *v = p->v;
86602 if( p->iSub<=p->nSub ){
86604 if( p->iSub==0 ){
86605 aOp = v->aOp;
86606 nOp = v->nOp;
86608 aOp = p->apSub[p->iSub-1]->aOp;
86609 nOp = p->apSub[p->iSub-1]->nOp;
86611 assert( p->iAddr<nOp );
86613 pRet = &aOp[p->iAddr];
86614 p->iAddr++;
86615 if( p->iAddr==nOp ){
86616 p->iSub++;
86617 p->iAddr = 0;
86620 if( pRet->p4type==P4_SUBPROGRAM ){
86621 i64 nByte = (1+(u64)p->nSub)*sizeof(SubProgram*);
86623 for(j=0; j<p->nSub; j++){
86624 if( p->apSub[j]==pRet->p4.pProgram ) break;
86626 if( j==p->nSub ){
86627 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
86628 if( !p->apSub ){
86631 p->apSub[p->nSub++] = pRet->p4.pProgram;
86644 ** sub-programs contains any of the following:
86661 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
86677 int opcode = pOp->opcode;
86684 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
86689 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
86693 ** where a "DELETE FROM tbl" has a statement-journal but does not
86694 ** require one. This is not so bad - it is an inefficiency, not a bug. */
86695 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
86700 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
86705 sqlite3DbFree(v->db, sIter.apSub);
86710 ** true for this case to prevent the assert() in the callers frame
86712 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
86716 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
86725 || (pC->eCurType!=CURTYPE_SORTER
86726 && pC->eCurType!=CURTYPE_PSEUDO
86727 && !pC->isEphemeral)
86729 p->nWrite++;
86740 assert( p->nWrite==0 || p->usesStmtJournal );
86768 Parse *pParse = p->pParse;
86769 int *aLabel = pParse->aLabel;
86771 assert( pParse->db->mallocFailed==0 ); /* tag-20230419-1 */
86772 p->readOnly = 1;
86773 p->bIsReader = 0;
86774 pOp = &p->aOp[p->nOp-1];
86775 assert( p->aOp[0].opcode==OP_Init );
86783 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
86786 switch( pOp->opcode ){
86788 if( pOp->p2!=0 ) p->readOnly = 0;
86793 p->bIsReader = 1;
86801 p->readOnly = 0;
86802 p->bIsReader = 1;
86806 assert( pOp->p2>=0 );
86811 if( pOp->p2>nMaxVtabArgs ) nMaxVtabArgs = pOp->p2;
86818 ** the code where OP_VFilter is generated at tag-20250207a. */
86819 assert( (pOp - p->aOp) >= 3 );
86820 assert( pOp[-1].opcode==OP_Integer );
86821 assert( pOp[-1].p2==pOp->p3+1 );
86822 n = pOp[-1].p1;
86829 if( pOp->p2<0 ){
86831 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
86832 ** have non-negative values for P2. */
86833 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
86834 assert( ADDR(pOp->p2)<-pParse->nLabel );
86835 assert( aLabel!=0 ); /* True because of tag-20230419-1 */
86836 pOp->p2 = aLabel[ADDR(pOp->p2)];
86841 assert( pOp->p2>0
86842 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP0)!=0 );
86845 assert( pOp->p2<p->nOp
86846 || (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)==0 );
86851 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
86852 ** have non-negative values for P2. */
86853 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
86855 assert( pOp>p->aOp );
86856 pOp--;
86860 sqlite3DbNNFreeNN(p->db, pParse->aLabel);
86861 pParse->aLabel = 0;
86863 pParse->nLabel = 0;
86865 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
86890 Vdbe *v, /* The byte-code program under construction */
86900 pParse = v->pParse;
86902 if( pParse->nErr ) return;
86904 assert( iLast<v->nOp );
86905 pOp = &v->aOp[iFirst];
86907 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ){
86908 int iDest = pOp->p2; /* Jump destination */
86910 if( pOp->opcode==OP_Gosub ) continue;
86911 if( pOp->p3==20230325 && pOp->opcode==OP_NotNull ){
86912 /* This is a deliberately taken illegal branch. tag-20230325-2 */
86918 if( j>=-pParse->nLabel || pParse->aLabel[j]<0 ){
86921 iDest = pParse->aLabel[j];
86925 for(; j<v->nOp; j++){
86926 VdbeOp *pX = &v->aOp[j];
86927 if( pX->opcode==OP_Return ){
86928 if( pX->p1==iRetReg ) break;
86931 if( pX->opcode==OP_Noop ) continue;
86932 if( pX->opcode==OP_Explain ) continue;
86960 assert( p->eVdbeState==VDBE_INIT_STATE );
86961 return p->nOp;
86970 ** sqlite3VdbeAddOpList() will always be non-NULL.
86974 assert( p->nOp + N <= p->nOpAlloc );
86988 for(i=0; i<p->nOp; i++){
86989 assert( p->aOp[i].opcode!=OP_ResultRow );
87017 VdbeOp *aOp = p->aOp;
87018 assert( aOp && !p->db->mallocFailed );
87021 assert( DbMaskAllZero(p->btreeMask) );
87024 *pnOp = p->nOp;
87025 p->aOp = 0;
87033 ** Non-zero P2 arguments to jump instructions are automatically adjusted
87040 int iLineno /* Source-file line number of first opcode */
87045 assert( p->eVdbeState==VDBE_INIT_STATE );
87046 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
87049 pFirst = pOut = &p->aOp[p->nOp];
87051 pOut->opcode = aOp->opcode;
87052 pOut->p1 = aOp->p1;
87053 pOut->p2 = aOp->p2;
87054 assert( aOp->p2>=0 );
87055 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
87056 pOut->p2 += p->nOp;
87058 pOut->p3 = aOp->p3;
87059 pOut->p4type = P4_NOTUSED;
87060 pOut->p4.p = 0;
87061 pOut->p5 = 0;
87063 pOut->zComment = 0;
87066 pOut->iSrcLine = iLineno+i;
87071 if( p->db->flags & SQLITE_VdbeAddopTrace ){
87072 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
87076 p->nOp += nOp;
87092 if( IS_STMT_SCANSTATUS(p->db) ){
87093 i64 nByte = (1+(i64)p->nScan) * sizeof(ScanStatus);
87095 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
87097 ScanStatus *pNew = &aNew[p->nScan++];
87099 pNew->addrExplain = addrExplain;
87100 pNew->addrLoop = addrLoop;
87101 pNew->addrVisit = addrVisit;
87102 pNew->nEst = nEst;
87103 pNew->zName = sqlite3DbStrDup(p->db, zName);
87104 p->aScan = aNew;
87122 if( IS_STMT_SCANSTATUS(p->db) ){
87125 for(ii=p->nScan-1; ii>=0; ii--){
87126 pScan = &p->aScan[ii];
87127 if( pScan->addrExplain==addrExplain ) break;
87131 if( addrEnd<0 ) addrEnd = sqlite3VdbeCurrentAddr(p)-1;
87132 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
87133 if( pScan->aAddrRange[ii]==0 ){
87134 pScan->aAddrRange[ii] = addrStart;
87135 pScan->aAddrRange[ii+1] = addrEnd;
87154 if( IS_STMT_SCANSTATUS(p->db) ){
87157 for(ii=p->nScan-1; ii>=0; ii--){
87158 pScan = &p->aScan[ii];
87159 if( pScan->addrExplain==addrExplain ) break;
87163 if( addrLoop>0 ) pScan->addrLoop = addrLoop;
87164 if( addrVisit>0 ) pScan->addrVisit = addrVisit;
87177 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
87181 sqlite3VdbeGetOp(p,addr)->p1 = val;
87184 assert( addr>=0 || p->db->mallocFailed );
87185 sqlite3VdbeGetOp(p,addr)->p2 = val;
87189 sqlite3VdbeGetOp(p,addr)->p3 = val;
87192 assert( p->nOp>0 || p->db->mallocFailed );
87193 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
87204 while( pOp->opcode==OP_ReleaseReg ) pOp--;
87206 if( pOp->p3==iDest && pOp->opcode==OP_Column ){
87207 pOp->p5 |= OPFLAG_TYPEOFARG;
87216 sqlite3VdbeChangeP2(p, addr, p->nOp);
87222 ** the previous opcode (and is thus a no-op) then simply back up
87227 ** strives to omit useless byte-code like this:
87233 if( addr==p->nOp-1 ){
87234 assert( p->aOp[addr].opcode==OP_Once
87235 || p->aOp[addr].opcode==OP_If
87236 || p->aOp[addr].opcode==OP_FkIfZero );
87237 assert( p->aOp[addr].p4type==0 );
87239 sqlite3VdbeGetLastOp(p)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
87241 p->nOp--;
87243 sqlite3VdbeChangeP2(p, addr, p->nOp);
87254 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
87263 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
87268 freeEphemeralFunction(db, p->pFunc);
87286 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
87300 if( db->pnBytesFreed==0 ){
87308 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
87312 if( db->pnBytesFreed==0 ) sqlite3DeleteTable(db, (Table*)p4);
87317 sqlite3DbFree(db, pSig->zAff);
87333 Op *pOp = &aOp[nOp-1];
87335 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
87337 sqlite3DbFree(db, pOp->zComment);
87340 pOp--;
87348 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
87352 p->pNext = pVdbe->pProgram;
87353 pVdbe->pProgram = p;
87360 return pVdbe->pProgram!=0;
87368 if( p->db->mallocFailed ) return 0;
87369 assert( addr>=0 && addr<p->nOp );
87370 pOp = &p->aOp[addr];
87371 freeP4(p->db, pOp->p4type, pOp->p4.p);
87372 pOp->p4type = P4_NOTUSED;
87373 pOp->p4.z = 0;
87374 pOp->opcode = OP_Noop;
87383 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
87384 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
87402 if( N==0 || OptimizationDisabled(pParse->db, SQLITE_ReleaseReg) ) return;
87403 assert( pParse->pVdbe );
87405 assert( iFirst+N-1<=pParse->nMem );
87410 N--;
87412 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
87413 mask &= ~MASKBIT32(N-1);
87414 N--;
87418 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
87419 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
87447 if( pOp->p4type ){
87448 assert( pOp->p4type > P4_FREE_IF_LE );
87449 pOp->p4type = 0;
87450 pOp->p4.p = 0;
87453 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
87456 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
87457 pOp->p4type = P4_DYNAMIC;
87464 db = p->db;
87465 assert( p->eVdbeState==VDBE_INIT_STATE );
87466 assert( p->aOp!=0 || db->mallocFailed );
87467 if( db->mallocFailed ){
87471 assert( p->nOp>0 );
87472 assert( addr<p->nOp );
87474 addr = p->nOp - 1;
87476 pOp = &p->aOp[addr];
87477 if( n>=0 || pOp->p4type ){
87484 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
87485 pOp->p4type = P4_INT32;
87488 pOp->p4.p = (void*)zP4;
87489 pOp->p4type = (signed char)n;
87496 ** to the value defined by the arguments. This is a high-speed
87507 if( p->db->mallocFailed ){
87508 freeP4(p->db, n, pP4);
87511 assert( p->nOp>0 );
87512 pOp = &p->aOp[p->nOp-1];
87513 assert( pOp->p4type==P4_NOTUSED );
87514 pOp->p4type = n;
87515 pOp->p4.p = pP4;
87524 Vdbe *v = pParse->pVdbe;
87535 ** insert a No-op and add the comment to that new instruction. This
87540 assert( p->nOp>0 || p->aOp==0 );
87541 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->pParse->nErr>0 );
87542 if( p->nOp ){
87543 assert( p->aOp );
87544 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
87545 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
87572 sqlite3VdbeGetLastOp(v)->iSrcLine = iLine;
87577 ** Return the opcode for a given address. The address must be non-negative.
87593 assert( p->eVdbeState==VDBE_INIT_STATE );
87594 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
87595 if( p->db->mallocFailed ){
87598 return &p->aOp[addr];
87605 return sqlite3VdbeGetOp(p, p->nOp - 1);
87614 if( c=='1' ) return pOp->p1;
87615 if( c=='2' ) return pOp->p2;
87616 if( c=='3' ) return pOp->p3;
87617 if( c=='4' ) return pOp->p4.i;
87618 return pOp->p5;
87629 ** "PX" -> "r[X]"
87630 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
87631 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
87632 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
87635 sqlite3 *db, /* Optional - Oom error reporting only */
87647 zOpName = sqlite3OpcodeName(pOp->opcode);
87663 if( pOp->zComment && pOp->zComment[0] ){
87664 sqlite3_str_appendall(&x, pOp->zComment);
87681 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
87684 sqlite3_context *pCtx = pOp->p4.pCtx;
87685 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
87687 }else if( pCtx->argc>1 ){
87688 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
87691 x.nChar -= 2;
87697 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
87706 if( !seenCom && pOp->zComment ){
87707 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
87709 }else if( pOp->zComment ){
87710 sqlite3_str_appendall(&x, pOp->zComment);
87726 switch( pExpr->op ){
87729 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
87732 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
87738 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
87742 if( pExpr->iColumn<0 ){
87745 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
87783 displayP4Expr(p, pExpr->pLeft);
87784 if( pExpr->pRight ){
87786 displayP4Expr(p, pExpr->pRight);
87804 switch( pOp->p4type ){
87807 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
87808 assert( pKeyInfo->aSortFlags!=0 );
87809 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
87810 for(j=0; j<pKeyInfo->nKeyField; j++){
87811 CollSeq *pColl = pKeyInfo->aColl[j];
87812 const char *zColl = pColl ? pColl->zName : "";
87815 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
87816 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
87824 displayP4Expr(&x, pOp->p4.pExpr);
87830 CollSeq *pColl = pOp->p4.pColl;
87831 assert( pColl->enc<4 );
87832 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
87833 encnames[pColl->enc]);
87837 FuncDef *pDef = pOp->p4.pFunc;
87838 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
87842 FuncDef *pDef = pOp->p4.pCtx->pFunc;
87843 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
87847 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
87851 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
87855 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
87859 Mem *pMem = pOp->p4.pMem;
87860 if( pMem->flags & MEM_Str ){
87861 zP4 = pMem->z;
87862 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
87863 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
87864 }else if( pMem->flags & MEM_Real ){
87865 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
87866 }else if( pMem->flags & MEM_Null ){
87869 assert( pMem->flags & MEM_Blob );
87876 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
87883 u32 *ai = pOp->p4.ai;
87897 zP4 = pOp->p4.pTab->zName;
87901 SubrtnSig *pSig = pOp->p4.pSubrtnSig;
87902 sqlite3_str_appendf(&x, "subrtnsig:%d,%s", pSig->selId, pSig->zAff);
87906 zP4 = pOp->p4.z;
87918 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
87922 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
87923 ** p->btreeMask of databases that will require a lock.
87926 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
87927 assert( i<(int)sizeof(p->btreeMask)*8 );
87928 DbMaskSet(p->btreeMask, i);
87929 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
87930 DbMaskSet(p->lockMask, i);
87936 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
87940 ** that the correct busy-handler callback is invoked if required.
87942 ** If SQLite is not threadsafe but does support shared-cache mode, then
87947 ** If SQLite is not threadsafe and does not support shared-cache mode, this
87948 ** function is a no-op.
87950 ** The p->btreeMask field is a bitmask of all btrees that the prepared
87951 ** statement p will ever use. Let N be the number of bits in p->btreeMask
87961 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
87962 db = p->db;
87963 aDb = db->aDb;
87964 nDb = db->nDb;
87966 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
87982 db = p->db;
87983 aDb = db->aDb;
87984 nDb = db->nDb;
87986 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
87992 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
88005 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
88019 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
88020 zP4 ? zP4 : "", pOp->p5,
88033 ** This is a high-runner, so only those fields that really do need to
88049 p->flags = flags;
88050 p->db = db;
88051 p->szMalloc = 0;
88053 p->pScopyFrom = 0;
88054 p->bScopy = 0;
88057 }while( (--N)>0 );
88072 sqlite3 *db = p->db;
88074 if( db->pnBytesFreed ){
88076 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
88090 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
88091 ** sqlite3MemRelease() were called from here. With -O2, this jumps
88096 testcase( p->flags & MEM_Agg );
88097 testcase( p->flags & MEM_Dyn );
88098 if( p->flags&(MEM_Agg|MEM_Dyn) ){
88099 testcase( (p->flags & MEM_Dyn)!=0 && p->xDel==sqlite3VdbeFrameMemDel );
88101 p->flags = MEM_Undefined;
88102 }else if( p->szMalloc ){
88103 sqlite3DbNNFreeNN(db, p->zMalloc);
88104 p->szMalloc = 0;
88105 p->flags = MEM_Undefined;
88109 p->flags = MEM_Undefined;
88124 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
88132 ** that deletes the Frame object that is attached to it as a blob.
88134 ** This routine does not delete the Frame right away. It merely adds the
88135 ** frame to a list of frames to be deleted when the Vdbe halts.
88140 pFrame->pParent = pFrame->v->pDelFrame;
88141 pFrame->v->pDelFrame = pFrame;
88161 int nSub = 0; /* Number of sub-vdbes seen so far */
88162 SubProgram **apSub = 0; /* Array of sub-vdbes */
88173 ** encountered, but p->pc will eventually catch up to nRow.
88175 nRow = p->nOp;
88177 if( pSub->flags&MEM_Blob ){
88180 nSub = pSub->n/sizeof(Vdbe*);
88181 apSub = (SubProgram **)pSub->z;
88184 nRow += apSub[i]->nOp;
88191 p->rc = SQLITE_OK;
88195 if( i<p->nOp ){
88198 aOp = p->aOp;
88203 i -= p->nOp;
88206 for(j=0; i>=apSub[j]->nOp; j++){
88207 i -= apSub[j]->nOp;
88208 assert( i<apSub[j]->nOp || j+1<nSub );
88210 aOp = apSub[j]->aOp;
88215 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
88225 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
88226 if( p->rc!=SQLITE_OK ){
88230 apSub = (SubProgram **)pSub->z;
88233 pSub->n = nSub*sizeof(SubProgram*);
88234 nRow += aOp[i].p4.pProgram->nOp;
88241 if( pOp->opcode==OP_OpenRead ) break;
88242 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
88243 if( pOp->opcode==OP_ReopenIdx ) break;
88267 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
88269 for(i=0; i<p->nChildCsr; i++){
88270 if( apCsr[i] ) sqlite3VdbeFreeCursorNN(p->v, apCsr[i]);
88272 releaseMemArray(aMem, p->nChildMem);
88273 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
88274 sqlite3DbFree(p->v->db, p);
88285 ** When p->explain==1, each instruction is listed. When
88286 ** p->explain==2, only OP_Explain instructions are listed and these
88287 ** are shown in a different format. p->explain==2 is used to implement
88289 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
88293 ** When p->explain==1, first the main program is listed, then each of
88300 sqlite3 *db = p->db; /* The database connection */
88303 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
88304 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
88308 assert( p->explain );
88309 assert( p->eVdbeState==VDBE_RUN_STATE );
88310 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
88314 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
88318 if( p->rc==SQLITE_NOMEM ){
88330 assert( p->nMem>9 );
88331 pSub = &p->aMem[9];
88337 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
88341 if( AtomicLoad(&db->u1.isInterrupted) ){
88342 p->rc = SQLITE_INTERRUPT;
88344 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
88347 if( p->explain==2 ){
88348 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
88349 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
88350 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
88351 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
88352 assert( p->nResColumn==4 );
88355 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
88356 -1, SQLITE_UTF8, SQLITE_STATIC);
88357 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
88358 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
88359 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
88361 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
88365 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
88370 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
88371 assert( p->nResColumn==8 );
88373 p->pResultRow = pMem;
88374 if( db->mallocFailed ){
88375 p->rc = SQLITE_NOMEM;
88378 p->rc = SQLITE_OK;
88393 if( p->zSql ){
88394 z = p->zSql;
88395 }else if( p->nOp>=1 ){
88396 const VdbeOp *pOp = &p->aOp[0];
88397 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
88398 z = pOp->p4.z;
88411 int nOp = p->nOp;
88415 pOp = &p->aOp[0];
88416 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
88419 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
88423 if( z[i-1]!=' ' ){
88446 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
88465 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
88468 if( nByte <= p->nFree ){
88469 p->nFree -= nByte;
88470 pBuf = &p->pSpace[p->nFree];
88472 p->nNeeded += nByte;
88488 assert( p->eVdbeState==VDBE_INIT_STATE
88489 || p->eVdbeState==VDBE_READY_STATE
88490 || p->eVdbeState==VDBE_HALT_STATE );
88494 assert( p->nOp>0 );
88496 p->eVdbeState = VDBE_READY_STATE;
88499 for(i=0; i<p->nMem; i++){
88500 assert( p->aMem[i].db==p->db );
88503 p->pc = -1;
88504 p->rc = SQLITE_OK;
88505 p->errorAction = OE_Abort;
88506 p->nChange = 0;
88507 p->cacheCtr = 1;
88508 p->minWriteFileFormat = 255;
88509 p->iStatement = 0;
88510 p->nFkConstraint = 0;
88512 for(i=0; i<p->nOp; i++){
88513 p->aOp[i].nExec = 0;
88514 p->aOp[i].nCycle = 0;
88550 assert( p->nOp>0 );
88552 assert( p->eVdbeState==VDBE_INIT_STATE );
88553 assert( pParse==p->pParse );
88554 assert( pParse->db==p->db );
88555 p->pVList = pParse->pVList;
88556 pParse->pVList = 0;
88557 db = p->db;
88558 assert( db->mallocFailed==0 );
88559 nVar = pParse->nVar;
88560 nMem = pParse->nMem;
88561 nCursor = pParse->nTab;
88562 nArg = pParse->nMaxArg;
88576 n = ROUND8P(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
88577 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
88579 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
88584 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
88585 if( pParse->explain ){
88587 p->explain = pParse->explain;
88588 p->nResColumn = 12 - 4*p->explain;
88590 p->expired = 0;
88598 ** This two-pass approach that reuses as much memory as possible from
88603 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
88604 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
88605 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
88606 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
88608 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
88610 if( !db->mallocFailed ){
88611 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
88612 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
88613 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
88614 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
88618 p->napArg = nArg;
88621 if( db->mallocFailed ){
88622 p->nVar = 0;
88623 p->nCursor = 0;
88624 p->nMem = 0;
88626 p->nCursor = nCursor;
88627 p->nVar = (ynVar)nVar;
88628 initMemArray(p->aVar, nVar, db, MEM_Null);
88629 p->nMem = nMem;
88630 initMemArray(p->aMem, nMem, db, MEM_Undefined);
88631 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
88644 VdbeTxtBlbCache *pCache = pCx->pCache;
88645 assert( pCx->colCache );
88646 pCx->colCache = 0;
88647 pCx->pCache = 0;
88648 if( pCache->pCValue ){
88649 sqlite3RCStrUnref(pCache->pCValue);
88650 pCache->pCValue = 0;
88652 sqlite3DbFree(p->db, pCache);
88656 if( pCx->colCache ){
88660 switch( pCx->eCurType ){
88662 sqlite3VdbeSorterClose(p->db, pCx);
88666 assert( pCx->uc.pCursor!=0 );
88667 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
88672 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
88673 const sqlite3_module *pModule = pVCur->pVtab->pModule;
88674 assert( pVCur->pVtab->nRef>0 );
88675 pVCur->pVtab->nRef--;
88676 pModule->xClose(pVCur);
88684 ** Close all cursors in the current frame.
88688 for(i=0; i<p->nCursor; i++){
88689 VdbeCursor *pC = p->apCsr[i];
88692 p->apCsr[i] = 0;
88699 ** is used, for example, when a trigger sub-program is halted to restore
88703 Vdbe *v = pFrame->v;
88705 v->aOp = pFrame->aOp;
88706 v->nOp = pFrame->nOp;
88707 v->aMem = pFrame->aMem;
88708 v->nMem = pFrame->nMem;
88709 v->apCsr = pFrame->apCsr;
88710 v->nCursor = pFrame->nCursor;
88711 v->db->lastRowid = pFrame->lastRowid;
88712 v->nChange = pFrame->nChange;
88713 v->db->nChange = pFrame->nDbChange;
88714 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
88715 v->pAuxData = pFrame->pAuxData;
88716 pFrame->pAuxData = 0;
88717 return pFrame->pc;
88729 if( p->pFrame ){
88731 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
88733 p->pFrame = 0;
88734 p->nFrame = 0;
88736 assert( p->nFrame==0 );
88738 releaseMemArray(p->aMem, p->nMem);
88739 while( p->pDelFrame ){
88740 VdbeFrame *pDel = p->pDelFrame;
88741 p->pDelFrame = pDel->pParent;
88746 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
88747 assert( p->pAuxData==0 );
88758 sqlite3 *db = p->db;
88760 if( p->nResAlloc ){
88761 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
88762 sqlite3DbFree(db, p->aColName);
88765 p->nResColumn = p->nResAlloc = (u16)nResColumn;
88766 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
88767 if( p->aColName==0 ) return;
88768 initMemArray(p->aColName, n, db, MEM_Null);
88790 assert( idx<p->nResAlloc );
88792 if( p->db->mallocFailed ){
88796 assert( p->aColName!=0 );
88797 pColName = &(p->aColName[idx+var*p->nResAlloc]);
88798 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
88799 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
88806 ** write-transaction spanning more than one database file, this routine
88807 ** takes care of the super-journal trickery.
88811 int nTrans = 0; /* Number of databases with an active write-transaction
88812 ** that are candidates for a two-phase commit using a
88813 ** super-journal */
88826 ** be done before determining whether a super-journal file is
88835 ** one database file has an open write transaction, a super-journal
88838 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
88839 Btree *pBt = db->aDb[i].pBt;
88841 /* Whether or not a database might need a super-journal depends upon
88843 ** journal modes use a super-journal and which do not */
88856 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
88871 /* If there are any write-transactions at all, invoke the commit hook */
88872 if( needXcommit && db->xCommitCallback ){
88873 rc = db->xCommitCallback(db->pCommitArg);
88879 /* The simple case - no more than one database file (not counting the
88881 ** super-journal.
88883 ** If the return value of sqlite3BtreeGetFilename() is a zero length
88885 ** that case we do not support atomic multi-file commits, so use the
88888 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
88891 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
88892 Btree *pBt = db->aDb[i].pBt;
88903 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
88904 Btree *pBt = db->aDb[i].pBt;
88914 /* The complex case - There is a multi-file write-transaction active.
88915 ** This requires a super-journal file to ensure the transaction is
88920 sqlite3_vfs *pVfs = db->pVfs;
88921 char *zSuper = 0; /* File-name for the super-journal */
88922 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
88929 /* Select a super-journal file name */
88947 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
88949 /* The antipenultimate character of the super-journal name must
88951 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
88956 /* Open the super-journal. */
88963 sqlite3DbFree(db, zSuper-4);
88968 ** super-journal file. If an error occurs at this point close
88969 ** and delete the super-journal file. All the individual journal files
88970 ** still have 'null' as the super-journal pointer, so they will roll
88973 for(i=0; i<db->nDb; i++){
88974 Btree *pBt = db->aDb[i].pBt;
88986 sqlite3DbFree(db, zSuper-4);
88992 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
89000 sqlite3DbFree(db, zSuper-4);
89005 ** sets the super-journal pointer in each individual journal. If
89006 ** an error occurs here, do not delete the super-journal file.
89010 ** super-journal file will be orphaned. But we cannot delete it,
89011 ** in case the super-journal file name was written into the journal
89014 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
89015 Btree *pBt = db->aDb[i].pBt;
89023 sqlite3DbFree(db, zSuper-4);
89027 /* Delete the super-journal file. This commits the transaction. After
89032 sqlite3DbFree(db, zSuper-4);
89047 for(i=0; i<db->nDb; i++){
89048 Btree *pBt = db->aDb[i].pBt;
89067 ** This is an internal self-check only - it is not an essential processing
89070 ** This is a no-op if NDEBUG is defined.
89078 p = db->pVdbe;
89082 if( p->readOnly==0 ) nWrite++;
89083 if( p->bIsReader ) nRead++;
89085 p = p->pVNext;
89087 assert( cnt==db->nVdbeActive );
89088 assert( nWrite==db->nVdbeWrite );
89089 assert( nRead==db->nVdbeRead );
89096 ** If the Vdbe passed as the first argument opened a statement-transaction,
89106 sqlite3 *const db = p->db;
89109 const int iSavepoint = p->iStatement-1;
89112 assert( db->nStatement>0 );
89113 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
89115 for(i=0; i<db->nDb; i++){
89117 Btree *pBt = db->aDb[i].pBt;
89130 db->nStatement--;
89131 p->iStatement = 0;
89146 db->nDeferredCons = p->nStmtDefCons;
89147 db->nDeferredImmCons = p->nStmtDefImmCons;
89152 if( p->db->nStatement && p->iStatement ){
89171 sqlite3 *db = p->db;
89172 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
89173 || (!deferred && p->nFkConstraint>0)
89175 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
89176 p->errorAction = OE_Abort;
89178 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ) return SQLITE_ERROR;
89200 sqlite3 *db = p->db;
89218 assert( p->eVdbeState==VDBE_RUN_STATE );
89219 if( db->mallocFailed ){
89220 p->rc = SQLITE_NOMEM_BKPT;
89227 if( p->bIsReader ){
89228 int mrc; /* Primary error code from p->rc */
89236 if( p->rc ){
89237 mrc = p->rc & 0xff;
89246 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
89251 ** Even if the statement is read-only, it is important to perform
89253 ** occurred while writing to the journal, sub-journal or database
89258 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
89259 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
89267 db->autoCommit = 1;
89268 p->nChange = 0;
89274 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
89278 /* If the auto-commit flag is set and this is the only active writer
89285 && db->autoCommit
89286 && db->nVdbeWrite==(p->readOnly==0)
89288 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
89291 if( NEVER(p->readOnly) ){
89296 }else if( db->flags & SQLITE_CorruptRdOnly ){
89298 db->flags &= ~SQLITE_CorruptRdOnly;
89300 /* The auto-commit flag is true, the vdbe program was successful
89306 if( rc==SQLITE_BUSY && p->readOnly ){
89311 p->rc = rc;
89313 p->nChange = 0;
89315 db->nDeferredCons = 0;
89316 db->nDeferredImmCons = 0;
89317 db->flags &= ~(u64)SQLITE_DeferFKs;
89320 }else if( p->rc==SQLITE_SCHEMA && db->nVdbeActive>1 ){
89321 p->nChange = 0;
89324 p->nChange = 0;
89326 db->nStatement = 0;
89328 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
89330 }else if( p->errorAction==OE_Abort ){
89335 db->autoCommit = 1;
89336 p->nChange = 0;
89340 /* If eStatementOp is non-zero, then a statement transaction needs to
89349 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
89350 p->rc = rc;
89351 sqlite3DbFree(db, p->zErrMsg);
89352 p->zErrMsg = 0;
89356 db->autoCommit = 1;
89357 p->nChange = 0;
89362 ** has been rolled back, update the database connection change-counter.
89364 if( p->changeCntOn ){
89366 sqlite3VdbeSetChanges(db, p->nChange);
89370 p->nChange = 0;
89378 db->nVdbeActive--;
89379 if( !p->readOnly ) db->nVdbeWrite--;
89380 if( p->bIsReader ) db->nVdbeRead--;
89381 assert( db->nVdbeActive>=db->nVdbeRead );
89382 assert( db->nVdbeRead>=db->nVdbeWrite );
89383 assert( db->nVdbeWrite>=0 );
89384 p->eVdbeState = VDBE_HALT_STATE;
89386 if( db->mallocFailed ){
89387 p->rc = SQLITE_NOMEM_BKPT;
89390 /* If the auto-commit flag is set to true, then any locks that were held
89392 ** to invoke any required unlock-notify callbacks.
89394 if( db->autoCommit ){
89398 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
89399 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
89405 ** in p->rc. This routine sets that result back to SQLITE_OK.
89408 p->rc = SQLITE_OK;
89420 sqlite3 *db = p->db;
89421 int rc = p->rc;
89422 if( p->zErrMsg ){
89423 db->bBenignMalloc++;
89425 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
89426 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
89428 db->bBenignMalloc--;
89429 }else if( db->pErr ){
89430 sqlite3ValueSetNull(db->pErr);
89432 db->errCode = rc;
89433 db->errByteOffset = -1;
89443 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
89444 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
89445 assert( v->db->init.busy==0 );
89448 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
89450 sqlite3DbFree(v->db, zExpanded);
89475 db = p->db;
89481 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
89488 if( p->pc>=0 ){
89490 if( db->pErr || p->zErrMsg ){
89493 db->errCode = p->rc;
89502 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
89503 if( p->aMem ){
89504 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
89507 if( p->zErrMsg ){
89508 sqlite3DbFree(db, p->zErrMsg);
89509 p->zErrMsg = 0;
89511 p->pResultRow = 0;
89513 p->nWrite = 0;
89522 fprintf(out, "---- ");
89523 for(i=0; i<p->nOp; i++){
89524 fprintf(out, "%02x", p->aOp[i].opcode);
89527 if( p->zSql ){
89529 fprintf(out, "-- ");
89530 for(i=0; (c = p->zSql[i])!=0; i++){
89531 if( pc=='\n' ) fprintf(out, "-- ");
89537 for(i=0; i<p->nOp; i++){
89539 i64 cnt = p->aOp[i].nExec;
89540 i64 cycles = p->aOp[i].nCycle;
89547 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
89553 return p->rc & db->errMask;
89565 if( p->eVdbeState>=VDBE_READY_STATE ){
89567 assert( (rc & p->db->errMask)==rc );
89593 || (pAux->iAuxOp==iOp
89594 && pAux->iAuxArg>=0
89595 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
89597 testcase( pAux->iAuxArg==31 );
89598 if( pAux->xDeleteAux ){
89599 pAux->xDeleteAux(pAux->pAux);
89601 *pp = pAux->pNextAux;
89604 pp= &pAux->pNextAux;
89620 assert( p->db==0 || p->db==db );
89621 if( p->aColName ){
89622 releaseMemArray(p->aColName, p->nResAlloc*COLNAME_N);
89623 sqlite3DbNNFreeNN(db, p->aColName);
89625 for(pSub=p->pProgram; pSub; pSub=pNext){
89626 pNext = pSub->pNext;
89627 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
89630 if( p->eVdbeState!=VDBE_INIT_STATE ){
89631 releaseMemArray(p->aVar, p->nVar);
89632 if( p->pVList ) sqlite3DbNNFreeNN(db, p->pVList);
89633 if( p->pFree ) sqlite3DbNNFreeNN(db, p->pFree);
89635 vdbeFreeOpArray(db, p->aOp, p->nOp);
89636 if( p->zSql ) sqlite3DbNNFreeNN(db, p->zSql);
89638 sqlite3DbFree(db, p->zNormSql);
89641 for(pThis=p->pDblStr; pThis; pThis=pNxt){
89642 pNxt = pThis->pNextStr;
89650 for(i=0; i<p->nScan; i++){
89651 sqlite3DbFree(db, p->aScan[i].zName);
89653 sqlite3DbFree(db, p->aScan);
89665 db = p->db;
89667 assert( sqlite3_mutex_held(db->mutex) );
89669 if( db->pnBytesFreed==0 ){
89670 assert( p->ppVPrev!=0 );
89671 *p->ppVPrev = p->pVNext;
89672 if( p->pVNext ){
89673 p->pVNext->ppVPrev = p->ppVPrev;
89689 assert( p->deferredMoveto );
89690 assert( p->isTable );
89691 assert( p->eCurType==CURTYPE_BTREE );
89692 rc = sqlite3BtreeTableMoveto(p->uc.pCursor, p->movetoTarget, 0, &res);
89698 p->deferredMoveto = 0;
89699 p->cacheStatus = CACHE_STALE;
89712 assert( p->eCurType==CURTYPE_BTREE );
89713 assert( p->uc.pCursor!=0 );
89714 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
89715 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
89716 p->cacheStatus = CACHE_STALE;
89717 if( isDifferentRow ) p->nullRow = 1;
89726 assert( p->eCurType==CURTYPE_BTREE || IsNullCursor(p) );
89727 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
89739 ** sqlite3VdbeSerialPut() <--- in-lined into OP_MakeRecord as of 2022-04-02
89744 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
89751 ** serial-type and data blob separately.
89756 ** -------------- --------------- ---------------
89768 ** N>=12 and even (N-12)/2 BLOB
89769 ** N>=13 and odd (N-13)/2 text
89777 ** Return the serial-type for the value stored in pMem.
89781 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
89782 ** opcode in the byte-code engine. But by moving this routine in-line, we
89788 int flags = pMem->flags;
89798 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
89799 i64 i = pMem->u.i;
89824 ** as an integer, then we might as well make it an 8-byte floating
89826 pMem->u.r = (double)pMem->u.i;
89827 pMem->flags &= ~MEM_IntReal;
89828 pMem->flags |= MEM_Real;
89837 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
89838 assert( pMem->n>=0 );
89839 n = (u32)pMem->n;
89841 n += pMem->u.nZero;
89869 ** Return the length of the data corresponding to the supplied serial-type.
89873 return (serial_type-12)/2;
89876 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
89886 ** If we are on an architecture with mixed-endian floating
89890 ** For most architectures, this is a no-op.
89892 ** (later): It is reported to me that the mixed-endian problem
89894 ** that early versions of GCC stored the two words of a 64-bit
89902 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
89906 ** (2007-08-30) Frank van Vugt has studied this problem closely
89909 ** emulation that uses only 32-bit mantissas instead of a full
89910 ** 48-bits as required by the IEEE standard. (This is the
89913 ** the necessary byte swapping is carried out using a 64-bit integer
89914 ** rather than a 64-bit float. Frank assures us that the code here
89937 ** big-endian integer. Return the equivalent native integer
89963 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
89964 ** twos-complement integer. */
89965 pMem->u.i = *(i64*)&x;
89966 pMem->flags = MEM_Int;
89967 testcase( pMem->u.i<0 );
89969 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
89974 ** defined that 64-bit floating point values really are mixed
89983 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
89985 memcpy(&pMem->u.r, &x, sizeof(x));
89986 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
89996 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
89998 memcpy(&pMem->u.r, &x, sizeof(x));
90000 pMem->flags = MEM_Null;
90003 pMem->flags = MEM_Real;
90013 ** UPDATE no-change flag set */
90014 pMem->flags = MEM_Null|MEM_Zero;
90015 pMem->n = 0;
90016 pMem->u.nZero = 0;
90021 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
90022 pMem->flags = MEM_Null;
90026 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
90028 pMem->u.i = ONE_BYTE_INT(buf);
90029 pMem->flags = MEM_Int;
90030 testcase( pMem->u.i<0 );
90033 case 2: { /* 2-byte signed integer */
90034 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
90035 ** twos-complement integer. */
90036 pMem->u.i = TWO_BYTE_INT(buf);
90037 pMem->flags = MEM_Int;
90038 testcase( pMem->u.i<0 );
90041 case 3: { /* 3-byte signed integer */
90042 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
90043 ** twos-complement integer. */
90044 pMem->u.i = THREE_BYTE_INT(buf);
90045 pMem->flags = MEM_Int;
90046 testcase( pMem->u.i<0 );
90049 case 4: { /* 4-byte signed integer */
90050 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
90051 ** twos-complement integer. */
90052 pMem->u.i = FOUR_BYTE_INT(buf);
90054 /* Work around a sign-extension bug in the HP compiler for HP/UX */
90055 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
90057 pMem->flags = MEM_Int;
90058 testcase( pMem->u.i<0 );
90061 case 5: { /* 6-byte signed integer */
90062 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
90063 ** twos-complement integer. */
90064 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
90065 pMem->flags = MEM_Int;
90066 testcase( pMem->u.i<0 );
90069 case 6: /* 8-byte signed integer */
90072 ** to avoid having to move the frame pointer in the common case */
90078 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
90079 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
90080 pMem->u.i = serial_type-8;
90081 pMem->flags = MEM_Int;
90085 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
90086 ** length.
90087 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
90088 ** (N-13)/2 bytes in length. */
90090 pMem->z = (char *)buf;
90091 pMem->n = (serial_type-12)/2;
90092 pMem->flags = aFlag[serial_type&1];
90118 nByte = ROUND8P(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
90119 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
90121 p->aMem = (Mem*)&((char*)p)[ROUND8P(sizeof(UnpackedRecord))];
90122 assert( pKeyInfo->aSortFlags!=0 );
90123 p->pKeyInfo = pKeyInfo;
90124 p->nField = pKeyInfo->nKeyField + 1;
90129 ** Given the nKey-byte encoding of a record in pKey[], populate the
90144 Mem *pMem = p->aMem;
90146 p->default_rc = 0;
90155 pMem->enc = pKeyInfo->enc;
90156 pMem->db = pKeyInfo->db;
90157 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
90158 pMem->szMalloc = 0;
90159 pMem->z = 0;
90163 if( (++u)>=p->nField ) break;
90170 sqlite3VdbeMemSetNull(pMem-1);
90172 assert( u<=pKeyInfo->nKeyField + 1 );
90173 p->nField = u;
90202 pKeyInfo = pPKey2->pKeyInfo;
90203 if( pKeyInfo->db==0 ) return 1;
90204 mem1.enc = pKeyInfo->enc;
90205 mem1.db = pKeyInfo->db;
90221 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
90222 assert( pKeyInfo->aSortFlags!=0 );
90223 assert( pKeyInfo->nKeyField>0 );
90259 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
90260 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
90263 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
90264 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
90266 rc = -rc;
90268 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
90269 rc = -rc; /* Invert the result for DESC sort order. */
90274 }while( idx1<szHdr1 && i<pPKey2->nField );
90285 rc = pPKey2->default_rc;
90292 if( pKeyInfo->db->mallocFailed ) return 1;
90301 ** limit given by pKeyInfo->nAllField.
90303 ** If this constraint is not satisfied, it means that the high-speed
90327 assert( nField <= pKeyInfo->nAllField );
90337 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
90345 if( pMem1->enc==pColl->enc ){
90348 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
90354 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
90355 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
90358 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
90359 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
90364 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
90374 ** with MEM_Zero. Return true if it could be a zero-blob.
90391 int n1 = pB1->n;
90392 int n2 = pB2->n;
90394 /* It is possible to have a Blob value that has some non-zero content
90398 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
90399 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
90401 if( (pB1->flags|pB2->flags) & MEM_Zero ){
90402 if( pB1->flags & pB2->flags & MEM_Zero ){
90403 return pB1->u.nZero - pB2->u.nZero;
90404 }else if( pB1->flags & MEM_Zero ){
90405 if( !isAllZero(pB2->z, pB2->n) ) return -1;
90406 return pB1->u.nZero - n2;
90408 if( !isAllZero(pB1->z, pB1->n) ) return +1;
90409 return n1 - pB2->u.nZero;
90412 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
90414 return n1 - n2;
90428 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
90439 if( r<-9223372036854775808.0 ) return +1;
90440 if( r>=9223372036854775808.0 ) return -1;
90442 if( i<y ) return -1;
90447 return (((double)i)<r) ? -1 : (((double)i)>r);
90464 f1 = pMem1->flags;
90465 f2 = pMem2->flags;
90473 return (f2&MEM_Null) - (f1&MEM_Null);
90485 if( pMem1->u.i < pMem2->u.i ) return -1;
90486 if( pMem1->u.i > pMem2->u.i ) return +1;
90490 if( pMem1->u.r < pMem2->u.r ) return -1;
90491 if( pMem1->u.r > pMem2->u.r ) return +1;
90498 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
90500 if( pMem1->u.i < pMem2->u.i ) return -1;
90501 if( pMem1->u.i > pMem2->u.i ) return +1;
90504 return -1;
90511 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
90513 return -1;
90527 return -1;
90530 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
90531 assert( pMem1->enc==SQLITE_UTF8 ||
90532 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
90538 assert( !pColl || pColl->xCmp );
90553 ** The first argument passed to this function is a serial-type that
90554 ** corresponds to an integer - all values between 1 and 9 inclusive
90590 return (serial_type - 8);
90602 ** If argument bSkip is non-zero, it is assumed that the caller has already
90606 ** fields that appear in both keys are equal, then pPKey2->default_rc is
90609 ** If database corruption is discovered, set pPKey2->errCode to
90611 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
90612 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
90624 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
90653 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
90658 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
90660 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
90661 assert( pPKey2->pKeyInfo->nKeyField>0 );
90663 while( 1 /*exit-by-break*/ ){
90667 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
90668 testcase( pRhs->flags & MEM_Int );
90669 testcase( pRhs->flags & MEM_IntReal );
90673 rc = serial_type==10 ? -1 : +1;
90675 rc = -1;
90678 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
90681 i64 rhs = pRhs->u.i;
90683 rc = -1;
90691 else if( pRhs->flags & MEM_Real ){
90698 rc = serial_type==10 ? -1 : +1;
90700 rc = -1;
90704 rc = -1; /* mem1 is a NaN */
90705 }else if( mem1.u.r<pRhs->u.r ){
90706 rc = -1;
90707 }else if( mem1.u.r>pRhs->u.r ){
90714 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
90720 else if( pRhs->flags & MEM_Str ){
90724 rc = -1;
90728 mem1.n = (serial_type - 12) / 2;
90732 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
90734 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
90736 }else if( pKeyInfo->aColl[i] ){
90737 mem1.enc = pKeyInfo->enc;
90738 mem1.db = pKeyInfo->db;
90742 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
90745 int nCmp = MIN(mem1.n, pRhs->n);
90746 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
90747 if( rc==0 ) rc = mem1.n - pRhs->n;
90753 else if( pRhs->flags & MEM_Blob ){
90754 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
90758 rc = -1;
90760 int nStr = (serial_type - 12) / 2;
90764 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
90766 }else if( pRhs->flags & MEM_Zero ){
90770 rc = nStr - pRhs->u.nZero;
90773 int nCmp = MIN(nStr, pRhs->n);
90774 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
90775 if( rc==0 ) rc = nStr - pRhs->n;
90794 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
90798 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
90800 rc = -rc;
90809 if( i==pPKey2->nField ) break;
90815 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
90829 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
90830 || pPKey2->pKeyInfo->db->mallocFailed
90832 pPKey2->eqSeen = 1;
90833 return pPKey2->default_rc;
90846 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
90864 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
90867 case 1: { /* 1-byte signed integer */
90872 case 2: { /* 2-byte signed integer */
90877 case 3: { /* 3-byte signed integer */
90882 case 4: { /* 4-byte signed integer */
90888 case 5: { /* 6-byte signed integer */
90893 case 6: { /* 8-byte signed integer */
90920 assert( pPKey2->u.i == pPKey2->aMem[0].u.i );
90921 v = pPKey2->u.i;
90923 res = pPKey2->r1;
90925 res = pPKey2->r2;
90926 }else if( pPKey2->nField>1 ){
90932 ** fields. Return pPKey2->default_rc in this case. */
90933 res = pPKey2->default_rc;
90934 pPKey2->eqSeen = 1;
90944 ** uses the collation sequence BINARY and (c) that the size-of-header varint
90955 assert( pPKey2->aMem[0].flags & MEM_Str );
90956 assert( pPKey2->aMem[0].n == pPKey2->n );
90957 assert( pPKey2->aMem[0].z == pPKey2->u.z );
90958 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
90968 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
90970 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
90976 nStr = (serial_type-12) / 2;
90978 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
90981 nCmp = MIN( pPKey2->n, nStr );
90982 res = memcmp(&aKey1[szHdr], pPKey2->u.z, nCmp);
90985 res = pPKey2->r2;
90987 res = pPKey2->r1;
90989 res = nStr - pPKey2->n;
90991 if( pPKey2->nField>1 ){
90994 res = pPKey2->default_rc;
90995 pPKey2->eqSeen = 1;
90998 res = pPKey2->r2;
91000 res = pPKey2->r1;
91007 || pPKey2->pKeyInfo->db->mallocFailed
91019 ** that the size-of-header varint that occurs at the start of each record
91031 if( p->pKeyInfo->nAllField<=13 ){
91032 int flags = p->aMem[0].flags;
91033 if( p->pKeyInfo->aSortFlags[0] ){
91034 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
91037 p->r1 = 1;
91038 p->r2 = -1;
91040 p->r1 = -1;
91041 p->r2 = 1;
91044 p->u.i = p->aMem[0].u.i;
91051 && p->pKeyInfo->aColl[0]==0
91054 p->u.z = p->aMem[0].z;
91055 p->n = p->aMem[0].n;
91080 ** than 2GiB are support - anything large must be database corruption.
91082 ** this code can safely assume that nCellKey is 32-bits
91105 /* The last field of the index should be an integer - the ROWID.
91107 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
91126 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
91161 assert( pC->eCurType==CURTYPE_BTREE );
91162 pCur = pC->uc.pCursor;
91186 assert( sqlite3_mutex_held(db->mutex) );
91187 db->nChange = nChange;
91188 db->nTotalChange += nChange;
91196 v->changeCntOn = 1;
91205 ** programs obsolete. Removing user-defined functions or collating
91219 for(p = db->pVdbe; p; p=p->pVNext){
91220 p->expired = iCode+1;
91228 return v->db;
91235 return v->prepFlags;
91249 Mem *pMem = &v->aVar[iVar-1];
91250 assert( (v->db->flags & SQLITE_EnableQPSG)==0
91251 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
91252 if( 0==(pMem->flags & MEM_Null) ){
91253 sqlite3_value *pRet = sqlite3ValueNew(v->db);
91266 ** to sqlite3_reoptimize() that re-preparing the statement may result
91271 assert( (v->db->flags & SQLITE_EnableQPSG)==0
91272 || (v->db->mDbFlags & DBFLAG_InternalFunc)!=0 );
91274 v->expmask |= 0x80000000;
91276 v->expmask |= ((u32)1 << (iVar-1));
91285 ** throw an error if it is given inputs that would make it non-deterministic.
91286 ** This routine is invoked by date/time functions that use non-deterministic
91292 if( pCtx->pVdbe==0 ) return 1;
91294 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
91295 if( pOp->opcode==OP_PureFunc ){
91298 if( pOp->p5 & NC_IsCheck ){
91300 }else if( pOp->p5 & NC_GenCol ){
91305 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
91306 pCtx->pFunc->zName, zContext);
91307 sqlite3_result_error(pCtx, zMsg, -1);
91318 ** byte-code register values correctly initialized.
91321 if( pExpr->op==TK_REGISTER ){
91322 assert( (pWalker->u.aMem[pExpr->iTable].flags & MEM_Undefined)==0 );
91335 if( pVtab->zErrMsg ){
91336 sqlite3 *db = p->db;
91337 sqlite3DbFree(db, p->zErrMsg);
91338 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
91339 sqlite3_free(pVtab->zErrMsg);
91340 pVtab->zErrMsg = 0;
91349 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
91360 Mem *pMem = &p->aMem[i];
91361 if( pMem->zMalloc ) sqlite3VdbeMemReleaseMalloc(pMem);
91370 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
91376 Vdbe *v, /* Vdbe pre-update hook is invoked by */
91385 sqlite3 *db = v->db;
91388 const char *zTbl = pTab->zName;
91392 if( pTab->tabFlags & TF_WithoutRowid ){
91393 nRealCol = sqlite3PrimaryKeyIndex(pTab)->nColumn;
91394 }else if( pTab->tabFlags & TF_HasVirtual ){
91395 nRealCol = pTab->nNVCol;
91397 nRealCol = pTab->nCol;
91401 assert( db->pPreUpdate==0 );
91408 iKey2 = v->aMem[iReg].u.i;
91415 assert( pCsr->eCurType==CURTYPE_BTREE );
91416 assert( pCsr->nField==nRealCol
91417 || (pCsr->nField==nRealCol+1 && op==SQLITE_DELETE && iReg==-1)
91425 preupdate.pKeyinfo->db = db;
91426 preupdate.pKeyinfo->enc = ENC(db);
91427 preupdate.pKeyinfo->nKeyField = pTab->nCol;
91428 preupdate.pKeyinfo->aSortFlags = (u8*)&fakeSortOrder;
91434 db->pPreUpdate = &preupdate;
91435 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
91436 db->pPreUpdate = 0;
91438 vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pUnpacked);
91439 vdbeFreeUnpacked(db, preupdate.pKeyinfo->nKeyField+1,preupdate.pNewUnpacked);
91443 for(i=0; i<pCsr->nField; i++){
91450 for(i=0; i<pTab->nCol; i++){
91481 ** Return TRUE (non-zero) of the statement supplied as an argument needs
91490 return p==0 || p->expired;
91500 if( p->db==0 ){
91524 assert( p->startTime>0 );
91525 assert( db->init.busy==0 );
91526 assert( p->zSql!=0 );
91527 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
91528 iElapse = (iNow - p->startTime)*1000000;
91530 if( db->xProfile ){
91531 db->xProfile(db->pProfileArg, p->zSql, iElapse);
91534 if( db->mTrace & SQLITE_TRACE_PROFILE ){
91535 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
91537 p->startTime = 0;
91544 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
91546 # define checkProfileCallback(DB,P) /*no-op*/
91561 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
91562 ** pointer is a harmless no-op. */
91566 sqlite3 *db = v->db;
91568 sqlite3_mutex_enter(db->mutex);
91570 assert( v->eVdbeState>=VDBE_READY_STATE );
91593 sqlite3 *db = v->db;
91594 sqlite3_mutex_enter(db->mutex);
91598 assert( (rc & (db->errMask))==rc );
91600 sqlite3_mutex_leave(db->mutex);
91621 mutex = p->db->mutex;
91624 for(i=0; i<p->nVar; i++){
91625 sqlite3VdbeMemRelease(&p->aVar[i]);
91626 p->aVar[i].flags = MEM_Null;
91628 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
91629 if( p->expmask ){
91630 p->expired = 1;
91643 if( p->flags & (MEM_Blob|MEM_Str) ){
91645 assert( p->flags==MEM_Null && p->z==0 );
91648 p->flags |= MEM_Blob;
91649 return p->n ? p->z : 0;
91671 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
91675 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
91678 && p->eSubtype=='p'
91679 && strcmp(p->u.zPType, zPType)==0
91681 return (void*)p->z;
91700 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
91701 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
91774 if( pVal->flags & MEM_Null ){
91776 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
91778 }else if( pVal->flags & MEM_Int ){
91780 }else if( pVal->flags & MEM_Str ){
91783 assert( eType == aType[pVal->flags&MEM_AffMask] );
91786 return aType[pVal->flags&MEM_AffMask];
91789 return pVal->enc;
91794 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
91799 return (pVal->flags&MEM_FromBind)!=0;
91811 pNew->flags &= ~MEM_Dyn;
91812 pNew->db = 0;
91813 if( pNew->flags&(MEM_Str|MEM_Blob) ){
91814 pNew->flags &= ~(MEM_Static|MEM_Dyn);
91815 pNew->flags |= MEM_Ephem;
91820 }else if( pNew->flags & MEM_Null ){
91822 pNew->flags &= ~(MEM_Term|MEM_Subtype);
91836 ** The following routines are used by user-defined functions to specify
91853 Mem *pOut = pCtx->pOut;
91866 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
91907 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91923 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91934 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91935 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
91941 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91942 pCtx->isError = SQLITE_ERROR;
91943 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
91950 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91951 pCtx->isError = SQLITE_ERROR;
91952 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
91959 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91960 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
91966 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91967 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
91973 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
91974 sqlite3VdbeMemSetNull(pCtx->pOut);
91989 pOut = pCtx->pOut;
91990 assert( sqlite3_mutex_held(pOut->db->mutex) );
91992 pOut->flags = MEM_Null;
92001 if( pCtx->pFunc!=0
92002 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
92007 pCtx->pFunc->zName);
92008 sqlite3_result_error(pCtx, zErr, -1);
92012 pOut = pCtx->pOut;
92013 assert( sqlite3_mutex_held(pOut->db->mutex) );
92014 pOut->eSubtype = eSubtype & 0xff;
92015 pOut->flags |= MEM_Subtype;
92029 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92045 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92055 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut);
92065 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92074 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92083 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92097 pOut = pCtx->pOut;
92098 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92100 sqlite3VdbeChangeEncoding(pOut, pCtx->enc);
92114 pOut = pCtx->pOut;
92115 assert( sqlite3_mutex_held(pOut->db->mutex) );
92116 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
92121 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
92124 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
92131 pCtx->isError = errCode ? errCode : -1;
92133 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
92135 if( pCtx->pOut->flags & MEM_Null ){
92136 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8,
92146 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92147 pCtx->isError = SQLITE_TOOBIG;
92148 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
92157 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92158 sqlite3VdbeMemSetNull(pCtx->pOut);
92159 pCtx->isError = SQLITE_NOMEM_BKPT;
92160 sqlite3OomFault(pCtx->pOut->db);
92166 ** test-control.
92169 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92170 if( pCtx->pOut->flags & MEM_Int ){
92171 pCtx->pOut->flags &= ~MEM_Int;
92172 pCtx->pOut->flags |= MEM_IntReal;
92186 for(i=0; i<db->nDb; i++){
92187 Btree *pBt = db->aDb[i].pBt;
92193 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
92194 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
92217 db = p->db;
92218 if( p->eVdbeState!=VDBE_RUN_STATE ){
92220 if( p->eVdbeState==VDBE_READY_STATE ){
92221 if( p->expired ){
92222 p->rc = SQLITE_SCHEMA;
92224 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
92226 ** error has occurred, then return the error code in p->rc to the
92239 if( db->nVdbeActive==0 ){
92240 AtomicStore(&db->u1.isInterrupted, 0);
92243 assert( db->nVdbeWrite>0 || db->autoCommit==0
92244 || ((db->nDeferredCons + db->nDeferredImmCons)==0)
92248 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
92249 && !db->init.busy && p->zSql ){
92250 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
92252 assert( p->startTime==0 );
92256 db->nVdbeActive++;
92257 if( p->readOnly==0 ) db->nVdbeWrite++;
92258 if( p->bIsReader ) db->nVdbeRead++;
92259 p->pc = 0;
92260 p->eVdbeState = VDBE_RUN_STATE;
92263 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){
92268 ** This "automatic-reset" change is not technically an incompatibility,
92274 ** returns, and those were broken by the automatic-reset change. As a
92275 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
92281 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
92289 assert( p->eVdbeState==VDBE_READY_STATE );
92295 p->rcApp = SQLITE_OK;
92298 if( p->explain ){
92303 db->nVdbeExec++;
92305 db->nVdbeExec--;
92309 assert( p->rc==SQLITE_OK );
92310 assert( db->mallocFailed==0 );
92311 db->errCode = SQLITE_ROW;
92318 p->pResultRow = 0;
92319 if( rc==SQLITE_DONE && db->autoCommit ){
92320 assert( p->rc==SQLITE_OK );
92321 p->rc = doWalCallbacks(db);
92322 if( p->rc!=SQLITE_OK ){
92325 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
92327 ** error has occurred, then return the error code in p->rc to the
92334 db->errCode = rc;
92335 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
92336 p->rc = SQLITE_NOMEM_BKPT;
92337 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
92342 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
92346 return (rc&db->errMask);
92350 ** This is the top-level implementation of sqlite3_step(). Call
92363 db = v->db;
92364 sqlite3_mutex_enter(db->mutex);
92367 int savedPc = v->pc;
92378 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
92379 sqlite3DbFree(db, v->zErrMsg);
92380 if( !db->mallocFailed ){
92381 v->zErrMsg = sqlite3DbStrDup(db, zErr);
92382 v->rc = rc = sqlite3ApiExit(db, rc);
92384 v->zErrMsg = 0;
92385 v->rc = rc = SQLITE_NOMEM_BKPT;
92394 ** SQLITE_SCHEMA. tag-20220401a */
92395 v->minWriteFileFormat = 254;
92397 assert( v->expired==0 );
92399 sqlite3_mutex_leave(db->mutex);
92412 assert( p && p->pFunc );
92413 return p->pFunc->pUserData;
92420 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
92430 assert( p && p->pOut );
92432 return p->pOut->db;
92446 ** performance by substituting a NULL result, or some other light-weight
92455 return sqlite3_value_nochange(p->pOut);
92483 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){
92486 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
92488 assert( pVal->eSubtype=='p' );
92489 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 );
92490 pRhs = (ValueList*)pVal->z;
92493 rc = sqlite3BtreeNext(pRhs->pCsr, 0);
92496 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy);
92497 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) );
92498 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE;
92504 sz = sqlite3BtreePayloadSize(pRhs->pCsr);
92505 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem);
92509 sqlite3_value *pOut = pRhs->pOut;
92512 pOut->enc = ENC(pOut->db);
92513 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){
92550 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
92551 assert( p->pVdbe!=0 );
92554 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
92557 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
92565 ** its pMem->z element.
92568 Mem *pMem = p->pMem;
92569 assert( (pMem->flags & MEM_Agg)==0 );
92572 pMem->z = 0;
92575 pMem->flags = MEM_Agg;
92576 pMem->u.pDef = p->pFunc;
92577 if( pMem->z ){
92578 memset(pMem->z, 0, nByte);
92581 return (void*)pMem->z;
92590 assert( p && p->pFunc && p->pFunc->xFinalize );
92591 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
92593 if( (p->pMem->flags & MEM_Agg)==0 ){
92596 return (void*)p->pMem->z;
92602 ** the user-function defined by pCtx.
92604 ** The left-most argument is 0.
92616 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92618 if( pCtx->pVdbe==0 ) return 0;
92620 assert( pCtx->pVdbe!=0 );
92622 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
92623 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
92624 return pAuxData->pAux;
92632 ** argument to the user-function defined by pCtx. Any previous value is
92635 ** The left-most argument is 0.
92653 pVdbe= pCtx->pVdbe;
92654 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
92661 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
92662 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
92667 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
92669 pAuxData->iAuxOp = pCtx->iOp;
92670 pAuxData->iAuxArg = iArg;
92671 pAuxData->pNextAux = pVdbe->pAuxData;
92672 pVdbe->pAuxData = pAuxData;
92673 if( pCtx->isError==0 ) pCtx->isError = -1;
92674 }else if( pAuxData->xDeleteAux ){
92675 pAuxData->xDeleteAux(pAuxData->pAux);
92678 pAuxData->pAux = pAux;
92679 pAuxData->xDeleteAux = xDelete;
92699 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
92700 return p->pMem->n;
92710 return pVm->nResColumn;
92719 if( pVm==0 || pVm->pResultRow==0 ) return 0;
92720 return pVm->nResColumn;
92729 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
92730 ** instead of an 8-byte one. This all works fine, except that when
92732 ** that a Mem structure is located on an 8-byte boundary. To prevent
92734 ** using gcc, we force nullMem to be 8-byte aligned using the magical
92773 assert( pVm->db );
92774 sqlite3_mutex_enter(pVm->db->mutex);
92775 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){
92776 pOut = &pVm->pResultRow[i];
92778 sqlite3Error(pVm->db, SQLITE_RANGE);
92811 assert( p->db!=0 );
92812 assert( sqlite3_mutex_held(p->db->mutex) );
92813 p->rc = sqlite3ApiExit(p->db, p->rc);
92814 sqlite3_mutex_leave(p->db->mutex);
92864 if( pOut->flags&MEM_Static ){
92865 pOut->flags &= ~MEM_Static;
92866 pOut->flags |= MEM_Ephem;
92911 ** Convert the N-th element of pStmt->pColName[] into a string using
92945 db = p->db;
92947 sqlite3_mutex_enter(db->mutex);
92949 if( p->explain ){
92951 n = p->explain==1 ? 8 : 4;
92954 int i = iExplainColNames16[N + 8*p->explain - 8];
92957 ret = (void*)azExplainColNames8[N + 8*p->explain - 8];
92961 n = p->nResColumn;
92963 u8 prior_mallocFailed = db->mallocFailed;
92967 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
92971 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
92976 assert( db->mallocFailed==0 || db->mallocFailed==1 );
92977 if( db->mallocFailed > prior_mallocFailed ){
92983 sqlite3_mutex_leave(db->mutex);
93081 ** The error code stored in database p->db is overwritten with the return
93084 ** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK,
93088 ** p->pVar!=0
93090 ** i<=p->nVar
93100 sqlite3_mutex_enter(p->db->mutex);
93101 if( p->eVdbeState!=VDBE_READY_STATE ){
93102 sqlite3Error(p->db, SQLITE_MISUSE_BKPT);
93103 sqlite3_mutex_leave(p->db->mutex);
93105 "bind on a busy prepared statement: [%s]", p->zSql);
93108 if( i>=(unsigned int)p->nVar ){
93109 sqlite3Error(p->db, SQLITE_RANGE);
93110 sqlite3_mutex_leave(p->db->mutex);
93113 pVar = &p->aVar[i];
93115 pVar->flags = MEM_Null;
93116 p->db->errCode = SQLITE_OK;
93121 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
93127 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
93128 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
93129 p->expired = 1;
93149 rc = vdbeUnbind(p, (u32)(i-1));
93151 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93153 pVar = &p->aVar[i-1];
93156 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
93159 sqlite3Error(p->db, rc);
93160 rc = sqlite3ApiExit(p->db, rc);
93163 sqlite3_mutex_leave(p->db->mutex);
93199 rc = vdbeUnbind(p, (u32)(i-1));
93201 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93202 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
93203 sqlite3_mutex_leave(p->db->mutex);
93213 rc = vdbeUnbind(p, (u32)(i-1));
93215 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93216 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
93217 sqlite3_mutex_leave(p->db->mutex);
93224 rc = vdbeUnbind(p, (u32)(i-1));
93226 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93227 sqlite3_mutex_leave(p->db->mutex);
93240 rc = vdbeUnbind(p, (u32)(i-1));
93242 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93243 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
93244 sqlite3_mutex_leave(p->db->mutex);
93289 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
93293 assert( pValue->flags & (MEM_Real|MEM_IntReal) );
93295 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i
93300 if( pValue->flags & MEM_Zero ){
93301 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
93303 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
93308 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
93309 pValue->enc);
93322 rc = vdbeUnbind(p, (u32)(i-1));
93324 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */
93326 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
93328 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
93330 sqlite3_mutex_leave(p->db->mutex);
93340 sqlite3_mutex_enter(p->db->mutex);
93341 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
93347 rc = sqlite3ApiExit(p->db, rc);
93348 sqlite3_mutex_leave(p->db->mutex);
93358 return p ? p->nVar : 0;
93365 ** The result is always UTF-8.
93370 return sqlite3VListNumToName(p->pVList, i);
93380 return sqlite3VListNameToNum(p->pVList, zName, nName);
93393 assert( pTo->db==pFrom->db );
93394 assert( pTo->nVar==pFrom->nVar );
93395 sqlite3_mutex_enter(pTo->db->mutex);
93396 for(i=0; i<pFrom->nVar; i++){
93397 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
93399 sqlite3_mutex_leave(pTo->db->mutex);
93419 if( pFrom->nVar!=pTo->nVar ){
93422 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
93423 if( pTo->expmask ){
93424 pTo->expired = 1;
93426 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
93427 if( pFrom->expmask ){
93428 pFrom->expired = 1;
93441 return pStmt ? ((Vdbe*)pStmt)->db : 0;
93449 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
93457 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
93469 sqlite3_mutex_enter(v->db->mutex);
93470 if( ((int)v->explain)==eMode ){
93474 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){
93476 }else if( v->eVdbeState!=VDBE_READY_STATE ){
93478 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){
93480 v->explain = eMode;
93483 v->explain = eMode;
93485 v->haveEqpOps = eMode==2;
93487 if( v->explain ){
93488 v->nResColumn = 12 - 4*v->explain;
93490 v->nResColumn = v->nResAlloc;
93492 sqlite3_mutex_leave(v->db->mutex);
93501 return v!=0 && v->eVdbeState==VDBE_RUN_STATE;
93518 sqlite3_mutex_enter(pDb->mutex);
93520 pNext = (sqlite3_stmt*)pDb->pVdbe;
93522 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext;
93524 sqlite3_mutex_leave(pDb->mutex);
93536 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
93543 sqlite3 *db = pVdbe->db;
93544 sqlite3_mutex_enter(db->mutex);
93546 db->pnBytesFreed = (int*)&v;
93547 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
93548 db->lookaside.pEnd = db->lookaside.pStart;
93550 db->pnBytesFreed = 0;
93551 db->lookaside.pEnd = db->lookaside.pTrueEnd;
93552 sqlite3_mutex_leave(db->mutex);
93554 v = pVdbe->aCounter[op];
93555 if( resetFlag ) pVdbe->aCounter[op] = 0;
93565 return p ? p->zSql : 0;
93585 sqlite3_mutex_enter(p->db->mutex);
93587 sqlite3_mutex_leave(p->db->mutex);
93600 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
93601 sqlite3_mutex_enter(p->db->mutex);
93602 p->zNormSql = sqlite3Normalize(p, p->zSql);
93603 sqlite3_mutex_leave(p->db->mutex);
93605 return p->zNormSql;
93624 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
93631 ** This function is called from within a pre-update callback to retrieve
93645 p = db->pPreUpdate;
93647 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
93648 if( !p || p->op==SQLITE_INSERT ){
93652 if( p->pPk ){
93653 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
93655 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
93657 if( iStore>=p->pCsr->nField || iStore<0 ){
93662 if( iIdx==p->pTab->iPKey ){
93663 *ppValue = pMem = &p->oldipk;
93664 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
93668 if( p->pUnpacked==0 ){
93672 assert( p->pCsr->eCurType==CURTYPE_BTREE );
93673 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
93676 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
93678 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec);
93679 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
93685 p->aRecord = aRec;
93688 pMem = *ppValue = &p->pUnpacked->aMem[iStore];
93689 if( iStore>=p->pUnpacked->nField ){
93692 Column *pCol = &p->pTab->aCol[iIdx];
93693 if( pCol->iDflt>0 ){
93694 if( p->apDflt==0 ){
93696 assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff );
93697 nByte = sizeof(sqlite3_value*)*p->pTab->nCol;
93698 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte);
93699 if( p->apDflt==0 ) goto preupdate_old_out;
93701 if( p->apDflt[iIdx]==0 ){
93704 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) );
93705 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
93706 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal);
93710 p->apDflt[iIdx] = pVal;
93712 *ppValue = p->apDflt[iIdx];
93716 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
93717 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
93718 testcase( pMem->flags & MEM_Int );
93719 testcase( pMem->flags & MEM_IntReal );
93733 ** This function is called from within a pre-update callback to retrieve
93739 p = db!=0 ? db->pPreUpdate : 0;
93741 p = db->pPreUpdate;
93743 return (p ? p->pKeyinfo->nKeyField : 0);
93749 ** This function is designed to be called from within a pre-update callback
93753 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
93754 ** top-level trigger etc.).
93762 p = db!=0 ? db->pPreUpdate : 0;
93764 p = db->pPreUpdate;
93766 return (p ? p->v->nFrame : 0);
93772 ** This function is designed to be called from within a pre-update callback
93778 p = db!=0 ? db->pPreUpdate : 0;
93780 p = db->pPreUpdate;
93782 return (p ? p->iBlobWrite : -1);
93788 ** This function is called from within a pre-update callback to retrieve
93802 p = db->pPreUpdate;
93803 if( !p || p->op==SQLITE_DELETE ){
93807 if( p->pPk && p->op!=SQLITE_UPDATE ){
93808 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx);
93810 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx);
93813 if( iStore>=p->pCsr->nField || iStore<0 ){
93818 if( p->op==SQLITE_INSERT ){
93819 /* For an INSERT, memory cell p->iNewReg contains the serialized record
93821 UnpackedRecord *pUnpack = p->pNewUnpacked;
93823 Mem *pData = &p->v->aMem[p->iNewReg];
93826 pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z);
93831 p->pNewUnpacked = pUnpack;
93833 pMem = &pUnpack->aMem[iStore];
93834 if( iIdx==p->pTab->iPKey ){
93835 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
93836 }else if( iStore>=pUnpack->nField ){
93840 /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required
93845 assert( p->op==SQLITE_UPDATE );
93846 if( !p->aNew ){
93847 assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff );
93848 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField);
93849 if( !p->aNew ){
93854 assert( iStore>=0 && iStore<p->pCsr->nField );
93855 pMem = &p->aNew[iStore];
93856 if( pMem->flags==0 ){
93857 if( iIdx==p->pTab->iPKey ){
93858 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
93860 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]);
93897 aOp = p->aOp;
93898 nOp = p->nOp;
93899 if( p->pFrame ){
93901 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
93902 aOp = pFrame->aOp;
93903 nOp = pFrame->nOp;
93923 for(idx=0; idx<p->nScan; idx++){
93924 pScan = &p->aScan[idx];
93925 if( pScan->zName ){
93926 iScan--;
93931 if( idx>=p->nScan ) return 1;
93932 assert( pScan==0 || pScan==&p->aScan[idx] );
93933 pScan = &p->aScan[idx];
93937 if( pScan->addrLoop>0 ){
93938 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec;
93940 *(sqlite3_int64*)pOut = -1;
93945 if( pScan->addrVisit>0 ){
93946 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec;
93948 *(sqlite3_int64*)pOut = -1;
93954 LogEst x = pScan->nEst;
93963 *(const char**)pOut = pScan->zName;
93967 if( pScan->addrExplain ){
93968 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z;
93975 if( pScan->addrExplain ){
93976 *(int*)pOut = aOp[ pScan->addrExplain ].p1;
93978 *(int*)pOut = -1;
93983 if( pScan->addrExplain ){
93984 *(int*)pOut = aOp[ pScan->addrExplain ].p2;
93986 *(int*)pOut = -1;
93992 if( pScan->aAddrRange[0]==0 ){
93993 res = -1;
93996 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){
93997 int iIns = pScan->aAddrRange[ii];
93998 int iEnd = pScan->aAddrRange[ii+1];
94009 if( pOp->p1!=iEnd ) continue;
94010 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){
94046 for(ii=0; p!=0 && ii<p->nOp; ii++){
94047 Op *pOp = &p->aOp[ii];
94048 pOp->nExec = 0;
94049 pOp->nCycle = 0;
94071 ** The Vdbe parse-tree explainer is also found here.
94079 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
94104 ** This function returns a pointer to a nul-terminated string in memory
94108 ** then the returned string holds a copy of zRawSql with "-- " prepended
94114 ** with large (multi-megabyte) strings and blobs.
94124 ** parameter index is known, locate the value in p->aVar[]. Then render
94134 int n; /* Length of a token prefix */
94135 int nToken; /* Length of the parameter token */
94143 db = p->db;
94144 sqlite3StrAccumInit(&out, 0, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
94145 if( db->nVdbeExec>1 ){
94149 sqlite3_str_append(&out, "-- ", 3);
94150 assert( (zRawSql - zStart) > 0 );
94151 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
94153 }else if( p->nVar==0 ){
94182 assert( idx>0 && idx<=p->nVar );
94183 pVar = &p->aVar[idx-1];
94184 if( pVar->flags & MEM_Null ){
94186 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
94187 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
94188 }else if( pVar->flags & MEM_Real ){
94189 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
94190 }else if( pVar->flags & MEM_Str ){
94197 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
94205 nOut = pVar->n;
94209 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
94212 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
94214 if( nOut<pVar->n ){
94215 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
94221 }else if( pVar->flags & MEM_Zero ){
94222 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
94225 assert( pVar->flags & MEM_Blob );
94227 nOut = pVar->n;
94232 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
94236 if( nOut<pVar->n ){
94237 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
94275 ** High-resolution hardware timer used for debugging and testing only.
94294 ** This file contains inline asm code for retrieving "high-performance"
94301 ** The following routine only works on Pentium-class (or newer) processors.
94303 ** processor and returns that value. This can be used for high-res
94424 ** use this information to make sure that the zero-blob functionality
94431 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
94432 sqlite3_max_blobsize = p->n;
94442 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
94444 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
94500 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
94501 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
94502 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
94504 ** In other words, if M is 2, then I is either 0 (for fall-through) or
94506 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
94517 ** always taken, the flags should be 0x05 since the fall-through and
94519 ** flags should be 0x06 since only the fall-through approach is allowed.
94522 ** interested in equal or not-equal. In other words, I==0 and I==2
94527 ** should be no-ops except for special builds used to measure test coverage.
94532 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
94536 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
94548 ** iSrcLine - the line number of the VdbeCoverage() macro, with
94550 ** I - Mask of bits 0x07 indicating which cases are are
94552 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
94557 ** M - Type of jump. Same as M argument above
94582 if( ((P)->flags&MEM_Ephem)!=0 \
94586 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
94613 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
94614 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
94616 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
94624 assert( iCur>=0 && iCur<p->nCursor );
94625 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
94626 sqlite3VdbeFreeCursorNN(p, p->apCsr[iCur]);
94627 p->apCsr[iCur] = 0;
94632 ** in pMem->zMalloc. But for the special case of the aMem[] entries used
94633 ** to hold cursors, it is faster to in-line the logic. */
94634 assert( pMem->flags==MEM_Undefined );
94635 assert( (pMem->flags & MEM_Dyn)==0 );
94636 assert( pMem->szMalloc==0 || pMem->z==pMem->zMalloc );
94637 if( pMem->szMalloc<nByte ){
94638 if( pMem->szMalloc>0 ){
94639 sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
94641 pMem->z = pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, nByte);
94642 if( pMem->zMalloc==0 ){
94643 pMem->szMalloc = 0;
94646 pMem->szMalloc = (int)nByte;
94649 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->zMalloc;
94651 pCx->eCurType = eCurType;
94652 pCx->nField = nField;
94653 pCx->aOffset = &pCx->aType[nField];
94656 pCx->uc.pCursor = (BtCursor*)&pMem->z[SZ_VDBECURSOR(nField)];
94657 sqlite3BtreeCursorZero(pCx->uc.pCursor);
94675 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
94695 u8 enc = pRec->enc;
94697 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
94698 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
94700 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
94701 pRec->flags |= MEM_Int;
94703 pRec->u.r = rValue;
94704 pRec->flags |= MEM_Real;
94707 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
94710 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
94711 pRec->flags &= ~MEM_Str;
94721 ** floating-point representation if an integer representation
94735 ** No-op. pRec is unchanged.
94745 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
94746 if( (pRec->flags & (MEM_Real|MEM_IntReal))==0 ){
94747 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
94758 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
94759 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
94760 testcase( pRec->flags & MEM_Int );
94761 testcase( pRec->flags & MEM_Real );
94762 testcase( pRec->flags & MEM_IntReal );
94766 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
94801 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
94807 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
94808 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
94810 pMem->u.i = 0;
94813 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
94815 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
94816 pMem->u.i = ix;
94821 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
94822 pMem->u.i = ix;
94832 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
94833 ** But it does set pMem->u.r and pMem->u.i appropriately.
94836 assert( (pMem->flags & MEM_Null)==0
94837 || pMem->db==0 || pMem->db->mallocFailed );
94838 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null) ){
94839 testcase( pMem->flags & MEM_Int );
94840 testcase( pMem->flags & MEM_Real );
94841 testcase( pMem->flags & MEM_IntReal );
94842 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null);
94844 assert( pMem->flags & (MEM_Str|MEM_Blob) );
94845 testcase( pMem->flags & MEM_Str );
94846 testcase( pMem->flags & MEM_Blob );
94854 ** into buffer zBuf, length nBuf.
94857 int f = pMem->flags;
94875 for(i=0; i<25 && i<pMem->n; i++){
94876 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
94879 for(i=0; i<25 && i<pMem->n; i++){
94880 char z = pMem->z[i];
94885 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
94902 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
94903 for(j=0; j<25 && j<pMem->n; j++){
94904 c = pMem->z[j];
94907 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
94909 sqlite3_str_appendf(pStr, "(0-term)");
94920 if( p->flags & MEM_Undefined ){
94922 }else if( p->flags & MEM_Null ){
94923 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
94924 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
94925 printf(" si:%lld", p->u.i);
94926 }else if( (p->flags & (MEM_IntReal))!=0 ){
94927 printf(" ir:%lld", p->u.i);
94928 }else if( p->flags & MEM_Int ){
94929 printf(" i:%lld", p->u.i);
94931 }else if( p->flags & MEM_Real ){
94932 printf(" r:%.17g", p->u.r);
94943 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
94948 if( p->pScopyFrom ){
94949 assert( p->pScopyFrom->bScopy );
94950 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
94969 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
94975 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
94984 ** the number of non-transaction savepoints currently in the
94994 for(p=db->pSavepoint; p; p=p->pNext) n++;
94995 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
95001 ** Return the register of pOp->p2 after first preparing it to be
95006 pOut->flags = MEM_Int;
95011 assert( pOp->p2>0 );
95012 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
95013 pOut = &p->aMem[pOp->p2];
95015 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
95018 pOut->flags = MEM_Int;
95024 ** Compute a bloom filter hash using pOp->p4.i registers from aMem[] beginning
95025 ** with pOp->p3. Return the hash.
95031 assert( pOp->p4type==P4_INT32 );
95032 for(i=pOp->p3, mx=i+pOp->p4.i; i<mx; i++){
95034 if( p->flags & (MEM_Int|MEM_IntReal) ){
95035 h += p->u.i;
95036 }else if( p->flags & MEM_Real ){
95038 }else if( p->flags & (MEM_Str|MEM_Blob) ){
95042 h += 4093 + (p->flags & (MEM_Str|MEM_Blob));
95059 int t, /* The serial-type code for the column value */
95066 sqlite3 *db = pDest->db;
95067 int encoding = pDest->enc;
95069 assert( pC->eCurType==CURTYPE_BTREE );
95070 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) return SQLITE_TOOBIG;
95071 if( len > 4000 && pC->pKeyInfo==0 ){
95078 ** Only do this on table-btrees so that writes to index-btrees do not
95084 if( pC->colCache==0 ){
95085 pC->pCache = sqlite3DbMallocZero(db, sizeof(VdbeTxtBlbCache) );
95086 if( pC->pCache==0 ) return SQLITE_NOMEM;
95087 pC->colCache = 1;
95089 pCache = pC->pCache;
95090 if( pCache->pCValue==0
95091 || pCache->iCol!=iCol
95092 || pCache->cacheStatus!=cacheStatus
95093 || pCache->colCacheCtr!=colCacheCtr
95094 || pCache->iOffset!=sqlite3BtreeOffset(pC->uc.pCursor)
95096 if( pCache->pCValue ) sqlite3RCStrUnref(pCache->pCValue);
95097 pBuf = pCache->pCValue = sqlite3RCStrNew( len+3 );
95099 rc = sqlite3BtreePayload(pC->uc.pCursor, iOffset, len, pBuf);
95104 pCache->iCol = iCol;
95105 pCache->cacheStatus = cacheStatus;
95106 pCache->colCacheCtr = colCacheCtr;
95107 pCache->iOffset = sqlite3BtreeOffset(pC->uc.pCursor);
95109 pBuf = pCache->pCValue;
95116 pDest->flags |= MEM_Term;
95122 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, iOffset, len, pDest);
95124 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
95126 pDest->z[len] = 0;
95127 pDest->flags |= MEM_Term;
95130 pDest->flags &= ~MEM_Ephem;
95146 return azTypes[sqlite3_value_type(pMem)-1];
95156 Op *aOp = p->aOp; /* Copy of p->aOp */
95164 sqlite3 *db = p->db; /* The database */
95172 Mem *aMem = p->aMem; /* Copy of p->aMem */
95184 assert( p->eVdbeState==VDBE_RUN_STATE ); /* sqlite3_step() verifies this */
95185 if( DbMaskNonZero(p->lockMask) ){
95189 if( db->xProgress ){
95190 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
95191 assert( 0 < db->nProgressOps );
95192 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
95197 if( p->rc==SQLITE_NOMEM ){
95202 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
95203 testcase( p->rc!=SQLITE_OK );
95204 p->rc = SQLITE_OK;
95205 assert( p->bIsReader || p->readOnly!=0 );
95206 p->iCurrentTime = 0;
95207 assert( p->explain==0 );
95208 db->busyHandler.nBusy = 0;
95209 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
95213 if( p->pc==0
95214 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
95219 if( p->db->flags & SQLITE_VdbeListing ){
95221 for(i=0; i<p->nOp; i++){
95225 if( p->db->flags & SQLITE_VdbeEQP ){
95226 for(i=0; i<p->nOp; i++){
95234 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
95238 for(pOp=&aOp[p->pc]; 1; pOp++){
95243 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
95247 pOp->nExec++;
95248 pnCycle = &pOp->nCycle;
95249 if( sqlite3NProfileCnt==0 ) *pnCycle -= sqlite3Hwtime();
95252 pOp->nExec++;
95253 pnCycle = &pOp->nCycle;
95254 *pnCycle -= sqlite3Hwtime();
95261 if( db->flags & SQLITE_VdbeTrace ){
95262 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
95263 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
95273 sqlite3_interrupt_count--;
95283 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
95285 assert( pOp->p1>0 );
95286 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
95287 assert( memIsValid(&aMem[pOp->p1]) );
95288 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
95289 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
95292 assert( pOp->p2>0 );
95293 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
95294 assert( memIsValid(&aMem[pOp->p2]) );
95295 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
95296 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
95299 assert( pOp->p3>0 );
95300 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
95301 assert( memIsValid(&aMem[pOp->p3]) );
95302 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
95303 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
95306 assert( pOp->p2>0 );
95307 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
95308 memAboutToChange(p, &aMem[pOp->p2]);
95311 assert( pOp->p3>0 );
95312 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
95313 memAboutToChange(p, &aMem[pOp->p3]);
95321 switch( pOp->opcode ){
95328 ** the switch statement will break with convention and be flush-left. Another
95366 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
95376 if( pOp->p5 ){
95377 assert( pOp->p2 < (int)(pOp - aOp) );
95378 assert( pOp->p2 > 1 );
95379 pOp = &aOp[pOp->p2 - 2];
95386 pOp = &aOp[pOp->p2 - 1];
95397 ** faster according to "valgrind --tool=cachegrind" */
95399 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
95404 ** If the progress callback returns non-zero, exit the virtual machine with
95407 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
95408 assert( db->nProgressOps!=0 );
95409 nProgressLimit += db->nProgressOps;
95410 if( db->xProgress(db->pProgressArg) ){
95427 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
95428 pIn1 = &aMem[pOp->p1];
95431 pIn1->flags = MEM_Int;
95432 pIn1->u.i = (int)(pOp-aOp);
95433 REGISTER_TRACE(pOp->p1, pIn1);
95444 ** OP_Return becomes a no-op. If P3 is 0, then register P1 must hold an
95451 ** P2 is not used by the byte-code engine. However, if P2 is positive
95456 ** value is a byte-code indentation hint. See tag-20220407a in
95460 pIn1 = &aMem[pOp->p1];
95461 if( pIn1->flags & MEM_Int ){
95462 if( pOp->p3 ){ VdbeBranchTaken(1, 2); }
95463 pOp = &aOp[pIn1->u.i];
95464 }else if( ALWAYS(pOp->p3) ){
95482 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
95483 assert( pOp->p2>=0 && pOp->p2<p->nOp );
95484 assert( pOp->p3>=0 && pOp->p3<p->nOp );
95485 pOut = &aMem[pOp->p1];
95487 pOut->u.i = pOp->p3 - 1;
95488 pOut->flags = MEM_Int;
95489 if( pOp->p2==0 ) break;
95494 assert( pOp->p2>0 ); /* There are never any jumps to instruction 0 */
95495 assert( pOp->p2<p->nOp ); /* Jumps must be in range */
95496 pOp = &aOp[pOp->p2 - 1];
95512 pIn1 = &aMem[pOp->p1];
95513 assert( pIn1->flags==MEM_Int );
95514 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
95515 pCaller = &aOp[pIn1->u.i];
95516 assert( pCaller->opcode==OP_Yield );
95517 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
95518 pIn1->u.i = (int)(pOp - p->aOp) - 1;
95519 pOp = &aOp[pCaller->p2 - 1];
95538 pIn1 = &aMem[pOp->p1];
95540 pIn1->flags = MEM_Int;
95541 pcDest = (int)pIn1->u.i;
95542 pIn1->u.i = (int)(pOp - aOp);
95543 REGISTER_TRACE(pOp->p1, pIn1);
95553 ** value in register P3 is not NULL, then this routine is a no-op.
95557 pIn3 = &aMem[pOp->p3];
95559 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
95561 if( (pIn3->flags & MEM_Null)==0 ) break;
95605 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
95607 assert( pOp->p4type==P4_NOTUSED
95608 || pOp->p4type==P4_STATIC
95609 || pOp->p4type==P4_DYNAMIC );
95614 assert( pOp->p1!=SQLITE_INTERNAL );
95616 if( p->pFrame && pOp->p1==SQLITE_OK ){
95617 /* Halt the sub-program. Return control to the parent frame. */
95618 pFrame = p->pFrame;
95619 p->pFrame = pFrame->pParent;
95620 p->nFrame--;
95621 sqlite3VdbeSetChanges(db, p->nChange);
95623 if( pOp->p2==OE_Ignore ){
95624 /* Instruction pcx is the OP_Program that invoked the sub-program
95626 ** instruction is set to OE_Ignore, then the sub-program is throwing
95629 pcx = p->aOp[pcx].p2-1;
95631 aOp = p->aOp;
95632 aMem = p->aMem;
95636 p->rc = pOp->p1;
95637 p->errorAction = (u8)pOp->p2;
95638 assert( pOp->p5<=4 );
95639 if( p->rc ){
95640 if( pOp->p3>0 && pOp->p4type==P4_NOTUSED ){
95642 assert( pOp->p3<=(p->nMem + 1 - p->nCursor) );
95643 zErr = sqlite3ValueText(&aMem[pOp->p3], SQLITE_UTF8);
95645 }else if( pOp->p5 ){
95648 testcase( pOp->p5==1 );
95649 testcase( pOp->p5==2 );
95650 testcase( pOp->p5==3 );
95651 testcase( pOp->p5==4 );
95652 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
95653 if( pOp->p4.z ){
95654 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
95657 sqlite3VdbeError(p, "%s", pOp->p4.z);
95659 pcx = (int)(pOp - aOp);
95660 sqlite3_log(pOp->p1, "abort at %d: %s; [%s]", pcx, p->zErrMsg, p->zSql);
95665 p->rc = SQLITE_BUSY;
95667 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
95668 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
95669 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
95677 ** The 32-bit integer value P1 is written into register P2.
95681 pOut->u.i = pOp->p1;
95688 ** P4 is a pointer to a 64-bit integer value.
95693 assert( pOp->p4.pI64!=0 );
95694 pOut->u.i = *pOp->p4.pI64;
95702 ** P4 is a pointer to a 64-bit floating point value.
95707 pOut->flags = MEM_Real;
95708 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
95709 pOut->u.r = *pOp->p4.pReal;
95717 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
95719 ** this transformation, the length of string P4 is computed and stored
95723 assert( pOp->p4.z!=0 );
95725 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
95729 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
95733 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
95735 pOut->szMalloc = 0;
95736 pOut->flags |= MEM_Static;
95737 if( pOp->p4type==P4_DYNAMIC ){
95738 sqlite3DbFree(db, pOp->p4.z);
95740 pOp->p4type = P4_DYNAMIC;
95741 pOp->p4.z = pOut->z;
95742 pOp->p1 = pOut->n;
95745 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
95748 pOp->opcode = OP_String;
95757 ** The string value P4 of length P1 (bytes) is stored in register P2.
95767 assert( pOp->p4.z!=0 );
95769 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
95770 pOut->z = pOp->p4.z;
95771 pOut->n = pOp->p1;
95772 pOut->enc = encoding;
95775 if( pOp->p3>0 ){
95776 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
95777 pIn3 = &aMem[pOp->p3];
95778 assert( pIn3->flags & MEM_Int );
95779 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
95788 ** Mark the beginning of a subroutine that can be entered in-line
95792 ** If the subroutine is entered in-line, then the OP_Return will simply
95798 ** a no-op that simply falls through to the next instruction (assuming that
95800 ** entered in-line, then the OP_Return will cause in-line execution to
95815 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
95824 cnt = pOp->p3-pOp->p2;
95825 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
95826 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
95827 pOut->n = 0;
95829 pOut->uTemp = 0;
95835 pOut->flags = nullFlag;
95836 pOut->n = 0;
95837 cnt--;
95851 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
95852 pOut = &aMem[pOp->p1];
95853 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
95862 ** a zero-filled blob that is P1 bytes long in P2.
95865 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
95867 if( pOp->p4.z==0 ){
95868 sqlite3VdbeMemSetZeroBlob(pOut, pOp->p1);
95871 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
95873 pOut->enc = encoding;
95886 assert( pOp->p1>0 && pOp->p1<=p->nVar );
95887 pVar = &p->aVar[pOp->p1 - 1];
95891 pOut = &aMem[pOp->p2];
95894 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
95895 pOut->flags |= MEM_Static|MEM_FromBind;
95903 ** Move the P3 values in register P1..P1+P3-1 over into
95904 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
95906 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
95914 n = pOp->p3;
95915 p1 = pOp->p1;
95916 p2 = pOp->p2;
95923 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
95924 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
95929 pIn1->pScopyFrom = 0;
95931 for(i=1; i<p->nMem; i++){
95943 }while( --n );
95963 n = pOp->p3;
95964 pIn1 = &aMem[pOp->p1];
95965 pOut = &aMem[pOp->p2];
95971 if( (pOut->flags & MEM_Subtype)!=0 && (pOp->p5 & 0x0002)!=0 ){
95972 pOut->flags &= ~MEM_Subtype;
95975 pOut->pScopyFrom = 0;
95977 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
95978 if( (n--)==0 ) break;
95999 pIn1 = &aMem[pOp->p1];
96000 pOut = &aMem[pOp->p2];
96004 pOut->pScopyFrom = pIn1;
96005 pOut->mScopyFlags = pIn1->flags;
96006 pIn1->bScopy = 1;
96020 pIn1 = &aMem[pOp->p1];
96021 assert( (pIn1->flags & MEM_Int)!=0 );
96022 pOut = &aMem[pOp->p2];
96023 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
96031 ** constraint violations, this is a no-op.
96048 ** The registers P1 through P1+P2-1 contain a single row of
96051 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
96055 assert( p->nResColumn==pOp->p2 );
96056 assert( pOp->p1>0 || CORRUPT_DB );
96057 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
96059 p->cacheCtr = (p->cacheCtr + 2)|1;
96060 p->pResultRow = &aMem[pOp->p1];
96063 Mem *pMem = p->pResultRow;
96065 for(i=0; i<pOp->p2; i++){
96067 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
96077 if( db->mallocFailed ) goto no_mem;
96078 if( db->mTrace & SQLITE_TRACE_ROW ){
96079 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
96081 p->pc = (int)(pOp - aOp) + 1;
96104 pIn1 = &aMem[pOp->p1];
96105 pIn2 = &aMem[pOp->p2];
96106 pOut = &aMem[pOp->p3];
96109 flags1 = pIn1->flags;
96111 testcase( pIn2->flags & MEM_Null );
96112 if( (flags1 | pIn2->flags) & MEM_Null ){
96118 flags1 = pIn1->flags & ~MEM_Str;
96121 flags1 = pIn1->flags & ~MEM_Str;
96123 flags2 = pIn2->flags;
96126 flags2 = pIn2->flags & ~MEM_Str;
96129 flags2 = pIn2->flags & ~MEM_Str;
96131 nByte = pIn1->n + pIn2->n;
96132 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
96140 memcpy(pOut->z, pIn2->z, pIn2->n);
96141 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
96142 pIn2->flags = flags2;
96144 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
96145 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
96146 pIn1->flags = flags1;
96148 pOut->z[nByte]=0;
96149 pOut->z[nByte+1] = 0;
96150 pOut->flags |= MEM_Term;
96151 pOut->n = (int)nByte;
96152 pOut->enc = encoding;
96173 ** Synopsis: r[P3]=r[P2]-r[P1]
96207 pIn1 = &aMem[pOp->p1];
96208 type1 = pIn1->flags;
96209 pIn2 = &aMem[pOp->p2];
96210 type2 = pIn2->flags;
96211 pOut = &aMem[pOp->p3];
96214 iA = pIn1->u.i;
96215 iB = pIn2->u.i;
96216 switch( pOp->opcode ){
96222 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
96228 if( iA==-1 ) iA = 1;
96233 pOut->u.i = iB;
96244 switch( pOp->opcode ){
96246 case OP_Subtract: rB -= rA; break;
96258 if( iA==-1 ) iA = 1;
96264 pOut->u.i = rB;
96270 pOut->u.r = rB;
96285 ** be returned. This is used by the built-in min(), max() and nullif()
96294 ** publicly. Only built-in functions have access to this feature.
96297 assert( pOp->p4type==P4_COLLSEQ );
96298 if( pOp->p1 ){
96299 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
96307 ** Take the bit-wise AND of the values in register P1 and P2 and
96314 ** Take the bit-wise OR of the values in register P1 and P2 and
96343 pIn1 = &aMem[pOp->p1];
96344 pIn2 = &aMem[pOp->p2];
96345 pOut = &aMem[pOp->p3];
96346 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
96352 op = pOp->opcode;
96363 op = 2*OP_ShiftLeft + 1 - op;
96364 iB = iB>(-64) ? -iB : 64;
96368 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
96375 /* Sign-extend on a right shift of a negative number */
96376 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
96381 pOut->u.i = iA;
96395 pIn1 = &aMem[pOp->p1];
96398 *(u64*)&pIn1->u.i += (u64)pOp->p2;
96410 pIn1 = &aMem[pOp->p1];
96411 if( (pIn1->flags & MEM_Int)==0 ){
96413 if( (pIn1->flags & MEM_Int)==0 ){
96415 if( pOp->p2==0 ){
96439 pIn1 = &aMem[pOp->p1];
96440 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
96441 testcase( pIn1->flags & MEM_Int );
96442 testcase( pIn1->flags & MEM_IntReal );
96444 REGISTER_TRACE(pOp->p1, pIn1);
96467 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
96468 testcase( pOp->p2==SQLITE_AFF_TEXT );
96469 testcase( pOp->p2==SQLITE_AFF_BLOB );
96470 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
96471 testcase( pOp->p2==SQLITE_AFF_INTEGER );
96472 testcase( pOp->p2==SQLITE_AFF_REAL );
96473 pIn1 = &aMem[pOp->p1];
96477 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
96480 REGISTER_TRACE(pOp->p1, pIn1);
96491 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
96535 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
96585 u16 flags1; /* Copy of initial value of pIn1->flags */
96586 u16 flags3; /* Copy of initial value of pIn3->flags */
96588 pIn1 = &aMem[pOp->p1];
96589 pIn3 = &aMem[pOp->p3];
96590 flags1 = pIn1->flags;
96591 flags3 = pIn3->flags;
96594 if( pIn3->u.i > pIn1->u.i ){
96595 if( sqlite3aGTb[pOp->opcode] ){
96596 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96601 }else if( pIn3->u.i < pIn1->u.i ){
96602 if( sqlite3aLTb[pOp->opcode] ){
96603 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96606 iCompare = -1;
96609 if( sqlite3aEQb[pOp->opcode] ){
96610 VdbeBranchTaken(1, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96616 VdbeBranchTaken(0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96621 if( pOp->p5 & SQLITE_NULLEQ ){
96627 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
96628 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
96634 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
96642 if( pOp->p5 & SQLITE_JUMPIFNULL ){
96650 /* Neither operand is NULL and we couldn't do the special high-speed
96651 ** integer comparison case. So do a general-case comparison. */
96652 affinity = pOp->p5 & SQLITE_AFF_MASK;
96657 assert( flags3==pIn3->flags || CORRUPT_DB );
96658 flags3 = pIn3->flags;
96666 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
96668 testcase( pIn1->flags & MEM_Int );
96669 testcase( pIn1->flags & MEM_Real );
96670 testcase( pIn1->flags & MEM_IntReal );
96672 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
96673 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
96677 pIn3->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
96679 testcase( pIn3->flags & MEM_Int );
96680 testcase( pIn3->flags & MEM_Real );
96681 testcase( pIn3->flags & MEM_IntReal );
96683 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
96684 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
96687 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
96688 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
96700 res2 = sqlite3aLTb[pOp->opcode];
96702 res2 = sqlite3aEQb[pOp->opcode];
96704 res2 = sqlite3aGTb[pOp->opcode];
96710 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
96711 pIn3->flags = flags3;
96712 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
96713 pIn1->flags = flags1;
96715 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
96737 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
96740 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
96761 ** The first integer in the P4 integer array is the length of the array
96765 assert( pOp->p4type==P4_INTARRAY );
96766 assert( pOp->p4.ai );
96773 ** Synopsis: r[P1@P3] <-> r[P2@P3]
96775 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
96776 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
96805 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
96809 assert( pOp[-1].opcode==OP_Permutation );
96810 assert( pOp[-1].p4type==P4_INTARRAY );
96811 aPermute = pOp[-1].p4.ai + 1;
96814 n = pOp->p3;
96815 pKeyInfo = pOp->p4.pKeyInfo;
96818 p1 = pOp->p1;
96819 p2 = pOp->p2;
96824 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
96825 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
96827 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
96828 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
96837 assert( i<pKeyInfo->nKeyField );
96838 pColl = pKeyInfo->aColl[i];
96839 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
96843 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
96846 iCompare = -iCompare;
96848 if( bRev ) iCompare = -iCompare;
96865 assert( pOp>aOp && pOp[-1].opcode==OP_Compare );
96868 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
96870 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
96872 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
96902 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
96903 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
96904 if( pOp->opcode==OP_And ){
96911 pOut = &aMem[pOp->p3];
96915 pOut->u.i = v1;
96942 assert( pOp->p4type==P4_INT32 );
96943 assert( pOp->p4.i==0 || pOp->p4.i==1 );
96944 assert( pOp->p3==0 || pOp->p3==1 );
96945 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
96946 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
96958 pIn1 = &aMem[pOp->p1];
96959 pOut = &aMem[pOp->p2];
96960 if( (pIn1->flags & MEM_Null)==0 ){
96972 ** ones-complement of the P1 value into register P2. If P1 holds
96976 pIn1 = &aMem[pOp->p1];
96977 pOut = &aMem[pOp->p2];
96979 if( (pIn1->flags & MEM_Null)==0 ){
96980 pOut->flags = MEM_Int;
96981 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
96989 ** encountered on each invocation of the byte-code program. Jump to P2
96992 ** Top-level programs determine first invocation by comparing the P1
97000 ** because the self-altering code trick does not work for recursive
97006 ** be the register that holds that Bloom filter. See tag-202407032019
97011 assert( p->aOp[0].opcode==OP_Init );
97012 if( p->pFrame ){
97013 iAddr = (int)(pOp - p->aOp);
97014 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
97018 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
97020 if( p->aOp[0].p1==pOp->p1 ){
97026 pOp->p1 = p->aOp[0].p1;
97033 ** is considered true if it is numeric and non-zero. If the value
97034 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
97038 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
97048 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
97052 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
97064 pIn1 = &aMem[pOp->p1];
97065 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
97066 if( (pIn1->flags & MEM_Null)!=0 ){
97085 ** If P1 is -1, then P3 is a register number and the datatype is taken
97108 assert( pOp->p1>=(-1) && pOp->p1<p->nCursor );
97109 assert( pOp->p1>=0 || (pOp->p3>=0 && pOp->p3<=(p->nMem+1 - p->nCursor)) );
97110 if( pOp->p1>=0 ){
97111 pC = p->apCsr[pOp->p1];
97113 assert( pOp->p3>=0 );
97114 if( pOp->p3<pC->nHdrParsed ){
97115 serialType = pC->aType[pOp->p3];
97142 typeMask = 1 << (pOp->p4.i - 1);
97150 assert( memIsValid(&aMem[pOp->p3]) );
97151 typeMask = 1 << (sqlite3_value_type((sqlite3_value*)&aMem[pOp->p3])-1);
97158 VdbeBranchTaken( (typeMask & pOp->p5)!=0, 2);
97159 if( typeMask & pOp->p5 ){
97173 if( (aMem[pOp->p1].flags & MEM_Null)!=0
97174 || (aMem[pOp->p3].flags & MEM_Null)!=0
97176 sqlite3VdbeMemSetNull(aMem + pOp->p2);
97178 sqlite3VdbeMemSetInt64(aMem + pOp->p2, 0);
97189 pIn1 = &aMem[pOp->p1];
97190 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
97191 if( (pIn1->flags & MEM_Null)==0 ){
97205 ** If P1 is not an open cursor, then this opcode is a no-op.
97209 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97210 pC = p->apCsr[pOp->p1];
97211 if( pC && pC->nullRow ){
97212 sqlite3VdbeMemSetNull(aMem + pOp->p3);
97232 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
97236 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97237 pC = p->apCsr[pOp->p1];
97238 pOut = &p->aMem[pOp->p3];
97239 if( pC==0 || pC->eCurType!=CURTYPE_BTREE ){
97242 if( pC->deferredMoveto ){
97246 if( sqlite3BtreeEof(pC->uc.pCursor) ){
97249 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
97261 ** information about the format of the data.) Extract the P2-th column
97272 ** to only be used by the length() function or the equivalent. The content
97281 BtCursor *pCrsr; /* The B-Tree cursor corresponding to pC */
97282 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
97283 int len; /* The length of the serialized data for the column */
97290 u64 offset64; /* 64-bit offset */
97294 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
97295 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
97296 pC = p->apCsr[pOp->p1];
97297 p2 = (u32)pOp->p2;
97301 assert( p2<(u32)pC->nField
97302 || (pC->eCurType==CURTYPE_PSEUDO && pC->seekResult==0) );
97303 aOffset = pC->aOffset;
97304 assert( aOffset==pC->aType+pC->nField );
97305 assert( pC->eCurType!=CURTYPE_VTAB );
97306 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
97307 assert( pC->eCurType!=CURTYPE_SORTER );
97309 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
97310 if( pC->nullRow ){
97311 if( pC->eCurType==CURTYPE_PSEUDO && pC->seekResult>0 ){
97312 /* For the special case of as pseudo-cursor, the seekResult field
97314 pReg = &aMem[pC->seekResult];
97315 assert( pReg->flags & MEM_Blob );
97317 pC->payloadSize = pC->szRow = pReg->n;
97318 pC->aRow = (u8*)pReg->z;
97320 pDest = &aMem[pOp->p3];
97326 pCrsr = pC->uc.pCursor;
97327 if( pC->deferredMoveto ){
97329 assert( !pC->isEphemeral );
97330 if( pC->ub.aAltMap && (iMap = pC->ub.aAltMap[1+p2])>0 ){
97331 pC = pC->pAltCursor;
97332 p2 = iMap - 1;
97342 assert( pC->eCurType==CURTYPE_BTREE );
97345 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
97346 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
97347 assert( pC->szRow<=pC->payloadSize );
97348 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
97350 pC->cacheStatus = p->cacheCtr;
97351 if( (aOffset[0] = pC->aRow[0])<0x80 ){
97352 pC->iHdrOffset = 1;
97354 pC->iHdrOffset = sqlite3GetVarint32(pC->aRow, aOffset);
97356 pC->nHdrParsed = 0;
97358 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
97359 /* pC->aRow does not have to hold the entire row, but it does at least
97360 ** need to cover the header of the record. If pC->aRow does not contain
97363 pC->aRow = 0;
97364 pC->szRow = 0;
97371 ** them, respectively. So the maximum header length results from a
97372 ** 3-byte type for each of the maximum of 32768 columns plus three
97373 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
97375 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
97380 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
97392 zData = pC->aRow;
97393 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
97397 }else if( sqlite3BtreeCursorHasMoved(pC->uc.pCursor) ){
97404 ** parsed and valid information is in aOffset[] and pC->aType[].
97406 if( pC->nHdrParsed<=p2 ){
97408 ** to extract additional fields up through the p2+1-th field
97410 if( pC->iHdrOffset<aOffset[0] ){
97412 if( pC->aRow==0 ){
97414 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
97418 zData = pC->aRow;
97421 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
97423 i = pC->nHdrParsed;
97425 zHdr = zData + pC->iHdrOffset;
97429 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
97434 pC->aType[i] = t;
97445 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
97446 || (offset64 > pC->payloadSize)
97452 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
97457 pC->nHdrParsed = i;
97458 pC->iHdrOffset = (u32)(zHdr - zData);
97459 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
97468 if( pC->nHdrParsed<=p2 ){
97469 pDest = &aMem[pOp->p3];
97471 if( pOp->p4type==P4_MEM ){
97472 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
97479 t = pC->aType[p2];
97482 /* Extract the content for the p2+1-th column. Control can only
97483 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
97486 assert( p2<pC->nHdrParsed );
97488 pDest = &aMem[pOp->p3];
97494 assert( t==pC->aType[p2] );
97495 if( pC->szRow>=aOffset[p2+1] ){
97497 ** page - where the content is not on an overflow page */
97498 zData = pC->aRow + aOffset[p2];
97503 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
97507 pDest->n = len = (t-12)/2;
97508 pDest->enc = encoding;
97509 if( pDest->szMalloc < len+2 ){
97510 if( len>db->aLimit[SQLITE_LIMIT_LENGTH] ) goto too_big;
97511 pDest->flags = MEM_Null;
97514 pDest->z = pDest->zMalloc;
97516 memcpy(pDest->z, zData, len);
97517 pDest->z[len] = 0;
97518 pDest->z[len+1] = 0;
97519 pDest->flags = aFlag[t&1];
97523 pDest->enc = encoding;
97524 assert( pDest->db==db );
97526 if( ((p5 = (pOp->p5 & OPFLAG_BYTELENARG))!=0
97535 ** 2. the length(X) function if X is a blob, and
97536 ** 3. if the content length is zero.
97549 p->cacheCtr, colCacheCtr, pDest);
97560 REGISTER_TRACE(pOp->p3, pDest);
97565 pOp = &aOp[aOp[0].p3-1];
97585 ** is zero. When P3 is non-zero, no type checking occurs for
97592 ** <li> P2 should be the number of non-virtual columns in the
97604 assert( pOp->p4type==P4_TABLE );
97605 pTab = pOp->p4.pTab;
97606 assert( pTab->tabFlags & TF_Strict );
97607 assert( pTab->nNVCol==pOp->p2 );
97608 aCol = pTab->aCol;
97609 pIn1 = &aMem[pOp->p1];
97610 for(i=0; i<pTab->nCol; i++){
97613 if( pOp->p3 ){ pIn1++; continue; }
97615 assert( pIn1 < &aMem[pOp->p1+pOp->p2] );
97617 if( (pIn1->flags & MEM_Null)==0 ){
97620 if( (pIn1->flags & MEM_Blob)==0 ) goto vdbe_type_error;
97625 if( (pIn1->flags & MEM_Int)==0 ) goto vdbe_type_error;
97629 if( (pIn1->flags & MEM_Str)==0 ) goto vdbe_type_error;
97633 testcase( (pIn1->flags & (MEM_Real|MEM_IntReal))==MEM_Real );
97634 assert( (pIn1->flags & MEM_IntReal)==0 );
97635 if( pIn1->flags & MEM_Int ){
97638 ** so that we keep the high-resolution integer value but know that
97640 testcase( pIn1->u.i==140737488355328LL );
97641 testcase( pIn1->u.i==140737488355327LL );
97642 testcase( pIn1->u.i==-140737488355328LL );
97643 testcase( pIn1->u.i==-140737488355329LL );
97644 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL){
97645 pIn1->flags |= MEM_IntReal;
97646 pIn1->flags &= ~MEM_Int;
97648 pIn1->u.r = (double)pIn1->u.i;
97649 pIn1->flags |= MEM_Real;
97650 pIn1->flags &= ~MEM_Int;
97652 }else if( (pIn1->flags & (MEM_Real|MEM_IntReal))==0 ){
97663 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
97666 assert( pIn1 == &aMem[pOp->p1+pOp->p2] );
97671 vdbeMemTypeName(pIn1), sqlite3StdType[aCol[i].eCType-1],
97672 pTab->zName, aCol[i].zCnName);
97682 ** P4 is a string that is P2 characters long. The N-th character of the
97683 ** string indicates the column affinity that should be used for the N-th
97689 zAffinity = pOp->p4.z;
97691 assert( pOp->p2>0 );
97692 assert( zAffinity[pOp->p2]==0 );
97693 pIn1 = &aMem[pOp->p1];
97694 while( 1 /*exit-by-break*/ ){
97695 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
97698 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
97701 ** so that we keep the high-resolution integer value but know that
97703 testcase( pIn1->u.i==140737488355328LL );
97704 testcase( pIn1->u.i==140737488355327LL );
97705 testcase( pIn1->u.i==-140737488355328LL );
97706 testcase( pIn1->u.i==-140737488355329LL );
97707 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
97708 pIn1->flags |= MEM_IntReal;
97709 pIn1->flags &= ~MEM_Int;
97711 pIn1->u.r = (double)pIn1->u.i;
97712 pIn1->flags |= MEM_Real;
97713 pIn1->flags &= ~(MEM_Int|MEM_Str);
97716 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
97731 ** P4 may be a string that is P2 characters long. The N-th character of the
97732 ** string indicates the column affinity that should be used for the N-th
97741 ** compile-time option is enabled:
97744 ** of the right-most table that can be null-trimmed.
97748 ** accept no-change records with serial_type 10. This value is
97763 u32 len; /* Length of a field */
97770 ** ------------------------------------------------------------------------
97771 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
97772 ** ------------------------------------------------------------------------
97779 ** hdr-size field is also a varint which is the offset from the beginning
97785 nField = pOp->p1;
97786 zAffinity = pOp->p4.z;
97787 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
97789 nField = pOp->p2;
97790 pLast = &pData0[nField-1];
97793 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
97794 pOut = &aMem[pOp->p3];
97804 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
97805 pRec->flags |= MEM_IntReal;
97806 pRec->flags &= ~(MEM_Int);
97808 REGISTER_TRACE((int)(pRec-aMem), pRec);
97818 ** have a non-NULL default value. Also, the record must be left with
97820 ** index of the right-most column with a non-NULL default value */
97821 if( pOp->p5 ){
97822 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
97823 pLast--;
97824 nField--;
97831 ** the Mem.uTemp field of each term should hold the serial-type that will
97835 ** --------------- ---------------
97837 ** 1 1-byte signed integer
97838 ** 2 2-byte signed integer
97839 ** 3 3-byte signed integer
97840 ** 4 4-byte signed integer
97841 ** 5 6-byte signed integer
97842 ** 6 8-byte signed integer
97858 if( pRec->flags & MEM_Null ){
97859 if( pRec->flags & MEM_Zero ){
97863 ** Give such values a special internal-use-only serial-type of 10
97867 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
97869 pRec->uTemp = 10;
97871 pRec->uTemp = 0;
97874 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
97876 i64 i = pRec->u.i;
97878 testcase( pRec->flags & MEM_Int );
97879 testcase( pRec->flags & MEM_IntReal );
97892 if( (i&1)==i && p->minWriteFileFormat>=4 ){
97893 pRec->uTemp = 8+(u32)uu;
97896 pRec->uTemp = 1;
97900 pRec->uTemp = 2;
97903 pRec->uTemp = 3;
97906 pRec->uTemp = 4;
97909 pRec->uTemp = 5;
97912 if( pRec->flags & MEM_IntReal ){
97914 ** as an integer, then we might as well make it an 8-byte floating
97916 pRec->u.r = (double)pRec->u.i;
97917 pRec->flags &= ~MEM_IntReal;
97918 pRec->flags |= MEM_Real;
97919 pRec->uTemp = 7;
97921 pRec->uTemp = 6;
97924 }else if( pRec->flags & MEM_Real ){
97927 pRec->uTemp = 7;
97929 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
97930 assert( pRec->n>=0 );
97931 len = (u32)pRec->n;
97932 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
97933 if( pRec->flags & MEM_Zero ){
97934 serial_type += pRec->u.nZero*2;
97937 len += pRec->u.nZero;
97939 nZero += pRec->u.nZero;
97944 pRec->uTemp = serial_type;
97947 pRec--;
97950 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
97968 ** the new record. The output register (pOp->p3) is not allowed to
97972 if( nByte+nZero<=pOut->szMalloc ){
97975 pOut->z = pOut->zMalloc;
97979 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
97986 pOut->n = (int)nByte;
97987 pOut->flags = MEM_Blob;
97989 pOut->u.nZero = nZero;
97990 pOut->flags |= MEM_Zero;
97993 zHdr = (u8 *)pOut->z;
98004 while( 1 /*exit-by-break*/ ){
98005 serial_type = pRec->uTemp;
98006 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
98008 ** EVIDENCE-OF: R-64536-51728 The values for each column in the record
98017 assert( sizeof(v)==sizeof(pRec->u.r) );
98018 memcpy(&v, &pRec->u.r, sizeof(v));
98021 v = pRec->u.i;
98044 if( serial_type>=14 && pRec->n>0 ){
98045 assert( pRec->z!=0 );
98046 memcpy(zPayload, pRec->z, pRec->n);
98047 zPayload += pRec->n;
98051 if( pRec->n ){
98052 assert( pRec->z!=0 );
98053 assert( pRec->z!=(const char*)sqlite3CtypeMap );
98054 memcpy(zPayload, pRec->z, pRec->n);
98055 zPayload += pRec->n;
98061 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
98062 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
98064 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
98065 REGISTER_TRACE(pOp->p3, pOut);
98076 ** every btree page of the table. But if P3 is non-zero, an estimate
98083 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
98084 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
98086 if( pOp->p3 ){
98094 pOut->u.i = nEntry;
98115 p1 = pOp->p1;
98116 zName = pOp->p4.z;
98121 assert( db->pSavepoint==0 || db->autoCommit==0 );
98123 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
98125 assert( p->bIsReader );
98128 if( db->nVdbeWrite>0 ){
98132 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
98141 ** that the db->aVTrans[] array is empty. */
98142 assert( db->autoCommit==0 || db->nVTrans==0 );
98144 db->nStatement+db->nSavepoint);
98151 pNew->zName = (char *)&pNew[1];
98152 memcpy(pNew->zName, zName, nName+1);
98156 if( db->autoCommit ){
98157 db->autoCommit = 0;
98158 db->isTransactionSavepoint = 1;
98160 db->nSavepoint++;
98164 pNew->pNext = db->pSavepoint;
98165 db->pSavepoint = pNew;
98166 pNew->nDeferredCons = db->nDeferredCons;
98167 pNew->nDeferredImmCons = db->nDeferredImmCons;
98177 pSavepoint = db->pSavepoint;
98178 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
98179 pSavepoint = pSavepoint->pNext
98186 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
98190 sqlite3VdbeError(p, "cannot release savepoint - "
98199 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
98204 db->autoCommit = 1;
98206 p->pc = (int)(pOp - aOp);
98207 db->autoCommit = 0;
98208 p->rc = rc = SQLITE_BUSY;
98211 rc = p->rc;
98213 db->autoCommit = 0;
98215 db->isTransactionSavepoint = 0;
98219 iSavepoint = db->nSavepoint - iSavepoint - 1;
98221 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
98222 for(ii=0; ii<db->nDb; ii++){
98223 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
98232 for(ii=0; ii<db->nDb; ii++){
98233 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
98241 db->mDbFlags |= DBFLAG_SchemaChange;
98248 while( db->pSavepoint!=pSavepoint ){
98249 pTmp = db->pSavepoint;
98250 db->pSavepoint = pTmp->pNext;
98252 db->nSavepoint--;
98260 assert( pSavepoint==db->pSavepoint );
98261 db->pSavepoint = pSavepoint->pNext;
98264 db->nSavepoint--;
98268 db->nDeferredCons = pSavepoint->nDeferredCons;
98269 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
98279 if( p->eVdbeState==VDBE_HALT_STATE ){
98288 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
98299 desiredAutoCommit = pOp->p1;
98300 iRollback = pOp->p2;
98303 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
98304 assert( p->bIsReader );
98306 if( desiredAutoCommit!=db->autoCommit ){
98310 db->autoCommit = 1;
98311 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
98315 sqlite3VdbeError(p, "cannot commit transaction - "
98322 db->autoCommit = (u8)desiredAutoCommit;
98325 p->pc = (int)(pOp - aOp);
98326 db->autoCommit = (u8)(1-desiredAutoCommit);
98327 p->rc = rc = SQLITE_BUSY;
98331 if( p->rc==SQLITE_OK ){
98340 (iRollback)?"cannot rollback - no transaction is active":
98341 "cannot commit - no transaction is active"));
98353 ** If P2 is non-zero, then a write-transaction is started, or if a
98354 ** read-transaction is already active, it is upgraded to a write-transaction.
98355 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
98363 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
98389 assert( p->bIsReader );
98390 assert( p->readOnly==0 || pOp->p2==0 );
98391 assert( pOp->p2>=0 && pOp->p2<=2 );
98392 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98393 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98395 if( pOp->p2 && (db->flags & (SQLITE_QueryOnly|SQLITE_CorruptRdOnly))!=0 ){
98396 if( db->flags & SQLITE_QueryOnly ){
98406 pDb = &db->aDb[pOp->p1];
98407 pBt = pDb->pBt;
98410 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
98415 p->pc = (int)(pOp - aOp);
98416 p->rc = rc;
98422 if( p->usesStmtJournal
98423 && pOp->p2
98424 && (db->autoCommit==0 || db->nVdbeRead>1)
98427 if( p->iStatement==0 ){
98428 assert( db->nStatement>=0 && db->nSavepoint>=0 );
98429 db->nStatement++;
98430 p->iStatement = db->nSavepoint + db->nStatement;
98433 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
98435 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
98441 p->nStmtDefCons = db->nDeferredCons;
98442 p->nStmtDefImmCons = db->nDeferredImmCons;
98445 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
98447 && pOp->p5
98448 && (iMeta!=pOp->p3 || pDb->pSchema->iGeneration!=pOp->p4.i)
98451 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
98455 sqlite3DbFree(db, p->zErrMsg);
98456 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
98457 /* If the schema-cookie from the database file matches the cookie
98458 ** stored with the in-memory representation of the schema, do
98461 ** If virtual-tables are in use, this is not just an optimization.
98462 ** Often, v-tables store their data in other SQLite tables, which
98463 ** are queried from within xNext() and other v-table methods using
98464 ** prepared queries. If such a query is out-of-date, we do not want to
98466 ** v-table would have to be ready for the sqlite3_vtab structure itself
98468 ** a v-table method.
98470 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
98471 sqlite3ResetOneSchema(db, pOp->p1);
98473 p->expired = 1;
98479 p->changeCntOn = 0;
98493 ** There must be a read-lock on the database (either a transaction
98502 assert( p->bIsReader );
98503 iDb = pOp->p1;
98504 iCookie = pOp->p3;
98505 assert( pOp->p3<SQLITE_N_BTREE_META );
98506 assert( iDb>=0 && iDb<db->nDb );
98507 assert( db->aDb[iDb].pBt!=0 );
98508 assert( DbMaskTest(p->btreeMask, iDb) );
98510 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
98512 pOut->u.i = iMeta;
98527 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
98535 assert( pOp->p2<SQLITE_N_BTREE_META );
98536 assert( pOp->p1>=0 && pOp->p1<db->nDb );
98537 assert( DbMaskTest(p->btreeMask, pOp->p1) );
98538 assert( p->readOnly==0 );
98539 pDb = &db->aDb[pOp->p1];
98540 assert( pDb->pBt!=0 );
98541 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
98543 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
98544 if( pOp->p2==BTREE_SCHEMA_VERSION ){
98546 *(u32*)&pDb->pSchema->schema_cookie = *(u32*)&pOp->p3 - pOp->p5;
98547 db->mDbFlags |= DBFLAG_SchemaChange;
98548 sqlite3FkClearTriggerCache(db, pOp->p1);
98549 }else if( pOp->p2==BTREE_FILE_FORMAT ){
98551 pDb->pSchema->file_format = pOp->p3;
98553 if( pOp->p1==1 ){
98557 p->expired = 0;
98566 ** Open a read-only cursor for the database table whose root page is
98583 ** object, then table being opened must be an [index b-tree] where the
98585 ** sequence of that index b-tree. Otherwise, if P4 is an integer
98586 ** value, then the table being opened must be a [table b-tree] with a
98596 ** b-tree and if it is this opcode becomes a no-op. In other words,
98618 ** OPFLAG_P2ISREG bit is set in P5 - see below).
98622 ** object, then table being opened must be an [index b-tree] where the
98624 ** sequence of that index b-tree. Otherwise, if P4 is an integer
98625 ** value, then the table being opened must be a [table b-tree] with a
98657 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
98658 assert( pOp->p4type==P4_KEYINFO );
98659 pCur = p->apCsr[pOp->p1];
98660 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
98661 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
98662 assert( pCur->eCurType==CURTYPE_BTREE );
98663 sqlite3BtreeClearCursor(pCur->uc.pCursor);
98671 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
98672 assert( p->bIsReader );
98673 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
98674 || p->readOnly==0 );
98676 if( p->expired==1 ){
98683 p2 = (u32)pOp->p2;
98684 iDb = pOp->p3;
98685 assert( iDb>=0 && iDb<db->nDb );
98686 assert( DbMaskTest(p->btreeMask, iDb) );
98687 pDb = &db->aDb[iDb];
98688 pX = pDb->pBt;
98690 if( pOp->opcode==OP_OpenWrite ){
98692 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
98694 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
98695 p->minWriteFileFormat = pDb->pSchema->file_format;
98697 if( pOp->p5 & OPFLAG_P2ISREG ){
98699 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
98702 assert( (pIn2->flags & MEM_Int)!=0 );
98704 p2 = (int)pIn2->u.i;
98713 assert( (pOp->p5 & OPFLAG_P2ISREG)==0 );
98715 if( pOp->p4type==P4_KEYINFO ){
98716 pKeyInfo = pOp->p4.pKeyInfo;
98717 assert( pKeyInfo->enc==ENC(db) );
98718 assert( pKeyInfo->db==db );
98719 nField = pKeyInfo->nAllField;
98720 }else if( pOp->p4type==P4_INT32 ){
98721 nField = pOp->p4.i;
98723 assert( pOp->p1>=0 );
98726 pCur = allocateCursor(p, pOp->p1, nField, CURTYPE_BTREE);
98728 pCur->iDb = iDb;
98729 pCur->nullRow = 1;
98730 pCur->isOrdered = 1;
98731 pCur->pgnoRoot = p2;
98733 pCur->wrFlag = wrFlag;
98735 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
98736 pCur->pKeyInfo = pKeyInfo;
98738 ** SQLite used to check if the root-page flags were sane at this point
98741 pCur->isTable = pOp->p4type!=P4_KEYINFO;
98746 testcase( pOp->p5 & OPFLAG_BULKCSR );
98747 testcase( pOp->p2 & OPFLAG_SEEKEQ );
98748 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
98749 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
98760 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
98766 pOrig = p->apCsr[pOp->p2];
98768 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
98770 pCx = allocateCursor(p, pOp->p1, pOrig->nField, CURTYPE_BTREE);
98772 pCx->nullRow = 1;
98773 pCx->isEphemeral = 1;
98774 pCx->pKeyInfo = pOrig->pKeyInfo;
98775 pCx->isTable = pOrig->isTable;
98776 pCx->pgnoRoot = pOrig->pgnoRoot;
98777 pCx->isOrdered = pOrig->isOrdered;
98778 pCx->ub.pBtx = pOrig->ub.pBtx;
98779 pCx->noReuse = 1;
98780 pOrig->noReuse = 1;
98781 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
98782 pCx->pKeyInfo, pCx->uc.pCursor);
98796 ** the main database is read-only. The ephemeral
98813 ** can be used as zero-length data for OP_Insert. This is an optimization
98835 assert( pOp->p1>=0 );
98836 assert( pOp->p2>=0 );
98837 if( pOp->p3>0 ){
98839 ** form sqlite3BtreeInsert() where the length of the data is zero. */
98840 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
98841 assert( pOp->opcode==OP_OpenEphemeral );
98842 assert( aMem[pOp->p3].flags & MEM_Null );
98843 aMem[pOp->p3].n = 0;
98844 aMem[pOp->p3].z = "";
98846 pCx = p->apCsr[pOp->p1];
98847 if( pCx && !pCx->noReuse && ALWAYS(pOp->p2<=pCx->nField) ){
98851 assert( pCx->isEphemeral );
98852 pCx->seqCount = 0;
98853 pCx->cacheStatus = CACHE_STALE;
98854 rc = sqlite3BtreeClearTable(pCx->ub.pBtx, pCx->pgnoRoot, 0);
98856 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_BTREE);
98858 pCx->isEphemeral = 1;
98859 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->ub.pBtx,
98860 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
98863 rc = sqlite3BtreeBeginTrans(pCx->ub.pBtx, 1, 0);
98868 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
98870 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
98871 assert( pOp->p4type==P4_KEYINFO );
98872 rc = sqlite3BtreeCreateTable(pCx->ub.pBtx, &pCx->pgnoRoot,
98873 BTREE_BLOBKEY | pOp->p5);
98875 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
98876 assert( pKeyInfo->db==db );
98877 assert( pKeyInfo->enc==ENC(db) );
98878 rc = sqlite3BtreeCursor(pCx->ub.pBtx, pCx->pgnoRoot, BTREE_WRCSR,
98879 pKeyInfo, pCx->uc.pCursor);
98881 pCx->isTable = 0;
98883 pCx->pgnoRoot = SCHEMA_ROOT;
98884 rc = sqlite3BtreeCursor(pCx->ub.pBtx, SCHEMA_ROOT, BTREE_WRCSR,
98885 0, pCx->uc.pCursor);
98886 pCx->isTable = 1;
98889 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
98890 assert( p->apCsr[pOp->p1]==pCx );
98892 assert( !sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
98893 sqlite3BtreeClose(pCx->ub.pBtx);
98894 p->apCsr[pOp->p1] = 0; /* Not required; helps with static analysis */
98896 assert( sqlite3BtreeClosesWithCursor(pCx->ub.pBtx, pCx->uc.pCursor) );
98901 pCx->nullRow = 1;
98909 ** tables using an external merge-sort algorithm.
98911 ** If argument P3 is non-zero, then it indicates that the sorter may
98918 assert( pOp->p1>=0 );
98919 assert( pOp->p2>=0 );
98920 pCx = allocateCursor(p, pOp->p1, pOp->p2, CURTYPE_SORTER);
98922 pCx->pKeyInfo = pOp->p4.pKeyInfo;
98923 assert( pCx->pKeyInfo->db==db );
98924 assert( pCx->pKeyInfo->enc==ENC(db) );
98925 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
98939 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98940 pC = p->apCsr[pOp->p1];
98942 if( (pC->seqCount++)==0 ){
98956 ** A pseudo-table created by this opcode is used to hold a single
98959 ** is the only cursor opcode that works with a pseudo-table.
98962 ** the pseudo-table. If P2 is 0 or negative then the pseudo-cursor
98968 assert( pOp->p1>=0 );
98969 assert( pOp->p3>=0 );
98970 pCx = allocateCursor(p, pOp->p1, pOp->p3, CURTYPE_PSEUDO);
98972 pCx->nullRow = 1;
98973 pCx->seekResult = pOp->p2;
98974 pCx->isTable = 1;
98975 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
98977 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
98979 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
98980 assert( pOp->p5==0 );
98987 ** currently open, this instruction is a no-op.
98990 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
98991 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
98992 p->apCsr[pOp->p1] = 0;
99001 ** table or index for cursor P1 are used. P4 is a 64-bit integer
99004 ** by the cursor. The high-order bit is set if any column after
99009 pC = p->apCsr[pOp->p1];
99010 assert( pC->eCurType==CURTYPE_BTREE );
99011 pC->maskUsed = *(u64*)pOp->p4.pI64;
99019 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99046 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99064 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99082 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
99118 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99119 assert( pOp->p2!=0 );
99120 pC = p->apCsr[pOp->p1];
99122 assert( pC->eCurType==CURTYPE_BTREE );
99126 assert( pC->isOrdered );
99127 assert( pC->uc.pCursor!=0 );
99128 oc = pOp->opcode;
99130 pC->nullRow = 0;
99132 pC->seekOp = pOp->opcode;
99135 pC->deferredMoveto = 0;
99136 pC->cacheStatus = CACHE_STALE;
99137 if( pC->isTable ){
99140 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
99146 pIn3 = &aMem[pOp->p3];
99147 flags3 = pIn3->flags;
99152 newType = pIn3->flags; /* Record the type after applying numeric affinity */
99153 pIn3->flags = flags3; /* But convert the type back to its original */
99164 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
99169 c = sqlite3IntFloatCompare(iKey, pIn3->u.r);
99175 ** (x > 4.9) -> (x >= 5)
99176 ** (x <= 4.9) -> (x < 5)
99179 assert( OP_SeekGE==(OP_SeekGT-1) );
99180 assert( OP_SeekLT==(OP_SeekLE-1) );
99182 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
99194 rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)iKey, 0, &res);
99195 pC->movetoTarget = iKey; /* Used by OP_Delete */
99205 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
99207 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
99209 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
99210 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
99217 nField = pOp->p4.i;
99218 assert( pOp->p4type==P4_INT32 );
99220 r.pKeyInfo = pC->pKeyInfo;
99225 ** r.default_rc = -1;
99230 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
99231 assert( oc!=OP_SeekGT || r.default_rc==-1 );
99232 assert( oc!=OP_SeekLE || r.default_rc==-1 );
99236 r.aMem = &aMem[pOp->p3];
99242 if( i>0 ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
99247 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &res);
99262 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
99278 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
99291 res = sqlite3BtreeEof(pC->uc.pCursor);
99295 assert( pOp->p2>0 );
99308 ** Synopsis: Scan-ahead up to P1 rows
99320 ** This opcode helps to optimize IN operators on a multi-column index
99323 ** of the b-tree instead. A correct answer is obtained if this opcode
99324 ** is omitted or is a no-op.
99331 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
99343 ** This.P5 is true (non-zero) then a jump is made to This.P2. The P5==0
99383 /* If pOp->p5 is clear, then pOp->p2 points to the first instruction past the
99386 assert( pOp->p2>=(int)(pOp-aOp)+2 );
99388 if( pOp->p5==0 ){
99390 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
99391 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
99392 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
99393 assert( aOp[pOp->p2-1].opcode==OP_IdxGT
99394 || aOp[pOp->p2-1].opcode==OP_IdxGE );
99395 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
99398 assert( pOp->p2==(int)(pOp-aOp)+2 );
99399 assert( aOp[pOp->p2-1].opcode==OP_SeekGE );
99403 assert( pOp->p1>0 );
99404 pC = p->apCsr[pOp[1].p1];
99406 assert( pC->eCurType==CURTYPE_BTREE );
99407 assert( !pC->isTable );
99408 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
99410 if( db->flags&SQLITE_VdbeTrace ){
99411 printf("... cursor not valid - fall through\n");
99416 nStep = pOp->p1;
99418 r.pKeyInfo = pC->pKeyInfo;
99435 if( res>0 && pOp->p5==0 ){
99439 if( db->flags&SQLITE_VdbeTrace ){
99440 printf("... %d steps and then skip\n", pOp->p1 - nStep);
99450 if( db->flags&SQLITE_VdbeTrace ){
99451 printf("... %d steps and then success\n", pOp->p1 - nStep);
99460 if( db->flags&SQLITE_VdbeTrace ){
99461 printf("... fall through after %d steps\n", pOp->p1);
99467 nStep--;
99468 pC->cacheStatus = CACHE_STALE;
99469 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
99494 ** early, thus saving work. This is part of the IN-early-out optimization.
99496 ** P1 must be a valid b-tree cursor.
99500 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99501 pC = p->apCsr[pOp->p1];
99503 assert( pOp->p3>=pOp->p2 );
99504 if( pC->seekHit<pOp->p2 ){
99506 if( db->flags&SQLITE_VdbeTrace ){
99507 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p2);
99510 pC->seekHit = pOp->p2;
99511 }else if( pC->seekHit>pOp->p3 ){
99513 if( db->flags&SQLITE_VdbeTrace ){
99514 printf("seekHit changes from %d to %d\n", pC->seekHit, pOp->p3);
99517 pC->seekHit = pOp->p3;
99531 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99532 pCur = p->apCsr[pOp->p1];
99533 VdbeBranchTaken(pCur==0 || pCur->nullRow, 2);
99534 if( pCur==0 || pCur->nullRow ){
99595 ** This opcode is used in IN clause processing for a multi-column key.
99597 ** left-most element, and if there are no matches on the most recent
99616 ** record are not-NULL then a check is done to determine if any row in the
99632 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99633 pC = p->apCsr[pOp->p1];
99636 if( db->flags&SQLITE_VdbeTrace ){
99637 printf("seekHit is %d\n", pC->seekHit);
99640 if( pC->seekHit>=pOp->p4.i ) break;
99654 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
99657 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99658 assert( pOp->p4type==P4_INT32 );
99659 pC = p->apCsr[pOp->p1];
99662 pC->seekOp = pOp->opcode;
99664 r.aMem = &aMem[pOp->p3];
99665 assert( pC->eCurType==CURTYPE_BTREE );
99666 assert( pC->uc.pCursor!=0 );
99667 assert( pC->isTable==0 );
99668 r.nField = (u16)pOp->p4.i;
99671 r.pKeyInfo = pC->pKeyInfo;
99674 (void)sqlite3FaultSim(50); /* For use by --counter in TH3 */
99678 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
99681 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, &r, &pC->seekResult);
99684 assert( r.aMem->flags & MEM_Blob );
99685 assert( pOp->opcode!=OP_NoConflict );
99689 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
99691 sqlite3VdbeRecordUnpack(pC->pKeyInfo, r.aMem->n, r.aMem->z, pIdxKey);
99692 pIdxKey->default_rc = 0;
99693 rc = sqlite3BtreeIndexMoveto(pC->uc.pCursor, pIdxKey, &pC->seekResult);
99699 alreadyExists = (pC->seekResult==0);
99700 pC->nullRow = 1-alreadyExists;
99701 pC->deferredMoveto = 0;
99702 pC->cacheStatus = CACHE_STALE;
99703 if( pOp->opcode==OP_Found ){
99711 if( pOp->opcode==OP_NoConflict ){
99723 if( pOp->opcode==OP_IfNoHope ){
99724 pC->seekHit = pOp->p4.i;
99746 ** (with arbitrary multi-value keys).
99765 ** P3 register to contain a non-integer value, in which case the jump is
99769 ** (with arbitrary multi-value keys).
99783 pIn3 = &aMem[pOp->p3];
99784 testcase( pIn3->flags & MEM_Int );
99785 testcase( pIn3->flags & MEM_IntReal );
99786 testcase( pIn3->flags & MEM_Real );
99787 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
99788 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
99789 /* If pIn3->u.i does not contain an integer, compute iKey as the
99803 pIn3 = &aMem[pOp->p3];
99804 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
99805 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99806 iKey = pIn3->u.i;
99808 pC = p->apCsr[pOp->p1];
99811 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
99813 assert( pC->isTable );
99814 assert( pC->eCurType==CURTYPE_BTREE );
99815 pCrsr = pC->uc.pCursor;
99820 pC->movetoTarget = iKey; /* Used by OP_Delete */
99821 pC->nullRow = 0;
99822 pC->cacheStatus = CACHE_STALE;
99823 pC->deferredMoveto = 0;
99825 pC->seekResult = res;
99828 if( pOp->p2==0 ){
99847 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99848 assert( p->apCsr[pOp->p1]!=0 );
99849 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
99851 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
99864 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
99878 VdbeFrame *pFrame; /* Root frame of VDBE */
99884 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
99885 pC = p->apCsr[pOp->p1];
99887 assert( pC->isTable );
99888 assert( pC->eCurType==CURTYPE_BTREE );
99889 assert( pC->uc.pCursor!=0 );
99892 ** thing) is obtained in a two-step algorithm.
99904 assert( pC->isTable );
99916 if( !pC->useRandomRowid ){
99917 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
99922 v = 1; /* IMP: R-61914-48074 */
99924 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
99925 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
99927 pC->useRandomRowid = 1;
99929 v++; /* IMP: R-29538-34987 */
99935 if( pOp->p3 ){
99937 assert( pOp->p3>0 );
99938 if( p->pFrame ){
99939 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
99941 assert( pOp->p3<=pFrame->nMem );
99942 pMem = &pFrame->aMem[pOp->p3];
99945 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
99946 pMem = &aMem[pOp->p3];
99951 REGISTER_TRACE(pOp->p3, pMem);
99953 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
99954 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
99955 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
99958 if( v<pMem->u.i+1 ){
99959 v = pMem->u.i + 1;
99961 pMem->u.i = v;
99964 if( pC->useRandomRowid ){
99965 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
99969 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
99975 }while( ((rc = sqlite3BtreeTableMoveto(pC->uc.pCursor, (u64)v,
99981 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
99984 assert( v>0 ); /* EV: R-40812-03570 */
99986 pC->deferredMoveto = 0;
99987 pC->cacheStatus = CACHE_STALE;
99989 pOut->u.i = v;
100018 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
100021 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
100022 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
100035 const char *zDb; /* database name - used by the update hook */
100036 Table *pTab; /* Table structure - used by update and pre-update hooks */
100039 pData = &aMem[pOp->p2];
100040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100042 pC = p->apCsr[pOp->p1];
100044 assert( pC->eCurType==CURTYPE_BTREE );
100045 assert( pC->deferredMoveto==0 );
100046 assert( pC->uc.pCursor!=0 );
100047 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
100048 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
100049 REGISTER_TRACE(pOp->p2, pData);
100052 pKey = &aMem[pOp->p3];
100053 assert( pKey->flags & MEM_Int );
100055 REGISTER_TRACE(pOp->p3, pKey);
100056 x.nKey = pKey->u.i;
100058 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
100059 assert( pC->iDb>=0 );
100060 zDb = db->aDb[pC->iDb].zDbSName;
100061 pTab = pOp->p4.pTab;
100062 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
100069 /* Invoke the pre-update hook, if any */
100071 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
100072 sqlite3VdbePreUpdateHook(p,pC,SQLITE_INSERT,zDb,pTab,x.nKey,pOp->p2,-1);
100074 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
100075 /* Prevent post-update hook from running in cases when it should not */
100079 if( pOp->p5 & OPFLAG_ISNOOP ) break;
100082 assert( (pOp->p5 & OPFLAG_LASTROWID)==0 || (pOp->p5 & OPFLAG_NCHANGE)!=0 );
100083 if( pOp->p5 & OPFLAG_NCHANGE ){
100084 p->nChange++;
100085 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
100087 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
100088 x.pData = pData->z;
100089 x.nData = pData->n;
100090 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
100091 if( pData->flags & MEM_Zero ){
100092 x.nZero = pData->u.nZero;
100098 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
100099 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
100102 pC->deferredMoveto = 0;
100103 pC->cacheStatus = CACHE_STALE;
100106 /* Invoke the update-hook if required. */
100109 assert( db->xUpdateCallback!=0 );
100110 assert( pTab->aCol!=0 );
100111 db->xUpdateCallback(db->pUpdateArg,
100112 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
100113 zDb, pTab->zName, x.nKey);
100121 ** of table - intkey or index. This opcode is used as part of copying
100134 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
100135 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
100137 pDest = p->apCsr[pOp->p1];
100138 pSrc = p->apCsr[pOp->p2];
100139 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
100140 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
100152 ** the next Next instruction will be a no-op. As a result, in this case
100167 ** pre-update-hook for deletes is run, but the btree is otherwise unchanged.
100171 ** P1 must not be pseudo-table. It has to be a real table with
100175 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
100177 ** this case. Specifically, if one is configured, the pre-update hook is
100178 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
100191 opflags = pOp->p2;
100192 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100193 pC = p->apCsr[pOp->p1];
100195 assert( pC->eCurType==CURTYPE_BTREE );
100196 assert( pC->uc.pCursor!=0 );
100197 assert( pC->deferredMoveto==0 );
100201 if( pOp->p4type==P4_TABLE
100202 && HasRowid(pOp->p4.pTab)
100203 && pOp->p5==0
100204 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
100207 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
100209 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100210 assert( CORRUPT_DB || pC->movetoTarget==iKey );
100214 /* If the update-hook or pre-update-hook will be invoked, set zDb to
100219 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
100220 assert( pC->iDb>=0 );
100221 assert( pOp->p4.pTab!=0 );
100222 zDb = db->aDb[pC->iDb].zDbSName;
100223 pTab = pOp->p4.pTab;
100224 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
100225 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100233 /* Invoke the pre-update-hook if required. */
100234 assert( db->xPreUpdateCallback==0 || pTab==pOp->p4.pTab );
100235 if( db->xPreUpdateCallback && pTab ){
100238 || (aMem[pOp->p3].flags & MEM_Int)
100242 zDb, pTab, pC->movetoTarget,
100243 pOp->p3, -1
100250 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
100255 if( p->pFrame==0 ){
100256 if( pC->isEphemeral==0
100257 && (pOp->p5 & OPFLAG_AUXDELETE)==0
100258 && (pC->wrFlag & OPFLAG_FORDELETE)==0
100262 if( pOp->p2 & OPFLAG_NCHANGE ){
100263 nExtraDelete--;
100268 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
100269 pC->cacheStatus = CACHE_STALE;
100271 pC->seekResult = 0;
100274 /* Invoke the update-hook if required. */
100276 p->nChange++;
100277 if( db->xUpdateCallback && ALWAYS(pTab!=0) && HasRowid(pTab) ){
100278 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
100279 pC->movetoTarget);
100280 assert( pC->iDb>=0 );
100294 sqlite3VdbeSetChanges(db, p->nChange);
100295 p->nChange = 0;
100319 pC = p->apCsr[pOp->p1];
100321 assert( pOp->p4type==P4_INT32 );
100322 pIn3 = &aMem[pOp->p3];
100323 nKeyCol = pOp->p4.i;
100339 ** a register that is the source for a pseudo-table cursor created using
100340 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
100347 pOut = &aMem[pOp->p2];
100348 pC = p->apCsr[pOp->p1];
100351 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
100352 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100354 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
100371 ** of a real table, not a pseudo-table.
100375 ** register will be invalidated as soon as the cursor moves - including
100393 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100394 pC = p->apCsr[pOp->p1];
100396 assert( pC->eCurType==CURTYPE_BTREE );
100398 assert( pC->nullRow==0 );
100399 assert( pC->uc.pCursor!=0 );
100400 pCrsr = pC->uc.pCursor;
100410 assert( pC->deferredMoveto==0 );
100414 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
100420 if( !pOp->p3 ) Deephemeralize(pOut);
100422 REGISTER_TRACE(pOp->p2, pOut);
100443 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100444 pC = p->apCsr[pOp->p1];
100446 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
100447 if( pC->nullRow ){
100448 pOut->flags = MEM_Null;
100450 }else if( pC->deferredMoveto ){
100451 v = pC->movetoTarget;
100453 }else if( pC->eCurType==CURTYPE_VTAB ){
100454 assert( pC->uc.pVCur!=0 );
100455 pVtab = pC->uc.pVCur->pVtab;
100456 pModule = pVtab->pModule;
100457 assert( pModule->xRowid );
100458 rc = pModule->xRowid(pC->uc.pVCur, &v);
100463 assert( pC->eCurType==CURTYPE_BTREE );
100464 assert( pC->uc.pCursor!=0 );
100467 if( pC->nullRow ){
100468 pOut->flags = MEM_Null;
100471 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
100473 pOut->u.i = v;
100484 ** pseudo-cursor that always returns NULL for every column.
100489 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100490 pC = p->apCsr[pOp->p1];
100493 ** pseudo-cursor that always gives null rows. */
100494 pC = allocateCursor(p, pOp->p1, 1, CURTYPE_PSEUDO);
100496 pC->seekResult = 0;
100497 pC->isTable = 1;
100498 pC->noReuse = 1;
100499 pC->uc.pCursor = sqlite3BtreeFakeValidCursor();
100501 pC->nullRow = 1;
100502 pC->cacheStatus = CACHE_STALE;
100503 if( pC->eCurType==CURTYPE_BTREE ){
100504 assert( pC->uc.pCursor!=0 );
100505 sqlite3BtreeClearCursor(pC->uc.pCursor);
100508 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
100541 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100542 pC = p->apCsr[pOp->p1];
100544 assert( pC->eCurType==CURTYPE_BTREE );
100545 pCrsr = pC->uc.pCursor;
100549 pC->seekOp = pOp->opcode;
100551 if( pOp->opcode==OP_SeekEnd ){
100552 assert( pOp->p2==0 );
100553 pC->seekResult = -1;
100559 pC->nullRow = (u8)res;
100560 pC->deferredMoveto = 0;
100561 pC->cacheStatus = CACHE_STALE;
100563 if( pOp->p2>0 ){
100573 ** with cursor P1 and let X be 10*log2(N) if N is positive or -1
100584 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100585 assert( pOp->p4type==P4_INT32 );
100586 assert( pOp->p3>=-1 && pOp->p3<=640*2 );
100587 assert( pOp->p4.i>=-1 && pOp->p4.i<=640*2 );
100588 pC = p->apCsr[pOp->p1];
100590 pCrsr = pC->uc.pCursor;
100595 sz = -1; /* -Infinity encoding */
100601 res = sz>=pOp->p3 && sz<=pOp->p4.i;
100633 sqlite3_search_count--;
100635 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
100659 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100660 assert( pOp->p5==0 );
100661 assert( pOp->p2>=0 && pOp->p2<p->nOp );
100663 pC = p->apCsr[pOp->p1];
100665 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
100668 pC->seekOp = OP_Rewind;
100673 assert( pC->eCurType==CURTYPE_BTREE );
100674 pCrsr = pC->uc.pCursor;
100677 pC->deferredMoveto = 0;
100678 pC->cacheStatus = CACHE_STALE;
100681 pC->nullRow = (u8)res;
100682 if( pOp->p2>0 ){
100700 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
100709 ** number P5-1 in the prepared statement is incremented.
100725 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
100734 ** number P5-1 in the prepared statement is incremented.
100746 pC = p->apCsr[pOp->p1];
100752 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100753 assert( pOp->p5==0
100754 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
100755 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
100756 pC = p->apCsr[pOp->p1];
100758 assert( pC->deferredMoveto==0 );
100759 assert( pC->eCurType==CURTYPE_BTREE );
100760 assert( pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
100761 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
100762 || pC->seekOp==OP_NullRow);
100763 rc = sqlite3BtreePrevious(pC->uc.pCursor, pOp->p3);
100767 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100768 assert( pOp->p5==0
100769 || pOp->p5==SQLITE_STMTSTATUS_FULLSCAN_STEP
100770 || pOp->p5==SQLITE_STMTSTATUS_AUTOINDEX);
100771 pC = p->apCsr[pOp->p1];
100773 assert( pC->deferredMoveto==0 );
100774 assert( pC->eCurType==CURTYPE_BTREE );
100775 assert( pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
100776 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
100777 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
100778 || pC->seekOp==OP_IfNoHope);
100779 rc = sqlite3BtreeNext(pC->uc.pCursor, pOp->p3);
100782 pC->cacheStatus = CACHE_STALE;
100785 pC->nullRow = 0;
100786 p->aCounter[pOp->p5]++;
100794 pC->nullRow = 1;
100810 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
100830 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100831 pC = p->apCsr[pOp->p1];
100835 pIn2 = &aMem[pOp->p2];
100836 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
100837 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
100838 assert( pC->eCurType==CURTYPE_BTREE );
100839 assert( pC->isTable==0 );
100842 x.nKey = pIn2->n;
100843 x.pKey = pIn2->z;
100844 x.aMem = aMem + pOp->p3;
100845 x.nMem = (u16)pOp->p4.i;
100846 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
100847 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
100848 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
100850 assert( pC->deferredMoveto==0 );
100851 pC->cacheStatus = CACHE_STALE;
100866 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100867 pC = p->apCsr[pOp->p1];
100871 pIn2 = &aMem[pOp->p2];
100872 assert( pIn2->flags & MEM_Blob );
100873 assert( pC->isTable==0 );
100894 ** this (self-correcting and non-critical) error if in writable_schema mode.
100902 assert( pOp->p3>0 );
100903 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
100904 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100905 pC = p->apCsr[pOp->p1];
100907 assert( pC->eCurType==CURTYPE_BTREE );
100909 pCrsr = pC->uc.pCursor;
100911 r.pKeyInfo = pC->pKeyInfo;
100912 r.nField = (u16)pOp->p3;
100914 r.aMem = &aMem[pOp->p2];
100920 }else if( pOp->p5 && !sqlite3WritableSchema(db) ){
100924 assert( pC->deferredMoveto==0 );
100925 pC->cacheStatus = CACHE_STALE;
100926 pC->seekResult = 0;
100943 ** is non-zero, then reading column a(i)-1 from cursor P3 is
100964 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
100965 pC = p->apCsr[pOp->p1];
100967 assert( pC->eCurType==CURTYPE_BTREE || IsNullCursor(pC) );
100968 assert( pC->uc.pCursor!=0 );
100969 assert( pC->isTable==0 || IsNullCursor(pC) );
100970 assert( pC->deferredMoveto==0 );
100971 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
100982 if( !pC->nullRow ){
100984 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
100988 if( pOp->opcode==OP_DeferredSeek ){
100989 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
100990 pTabCur = p->apCsr[pOp->p3];
100992 assert( pTabCur->eCurType==CURTYPE_BTREE );
100993 assert( pTabCur->uc.pCursor!=0 );
100994 assert( pTabCur->isTable );
100995 pTabCur->nullRow = 0;
100996 pTabCur->movetoTarget = rowid;
100997 pTabCur->deferredMoveto = 1;
100998 pTabCur->cacheStatus = CACHE_STALE;
100999 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
101000 assert( !pTabCur->isEphemeral );
101001 pTabCur->ub.aAltMap = pOp->p4.ai;
101002 assert( !pC->isEphemeral );
101003 pTabCur->pAltCursor = pC;
101006 pOut->u.i = rowid;
101009 assert( pOp->opcode==OP_IdxRowid );
101010 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
101019 ** already occurred, this instruction is a no-op.
101024 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101025 pC = p->apCsr[pOp->p1];
101026 if( pC->deferredMoveto ){
101085 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101086 pC = p->apCsr[pOp->p1];
101088 assert( pC->isOrdered );
101089 assert( pC->eCurType==CURTYPE_BTREE );
101090 assert( pC->uc.pCursor!=0);
101091 assert( pC->deferredMoveto==0 );
101092 assert( pOp->p4type==P4_INT32 );
101093 r.pKeyInfo = pC->pKeyInfo;
101094 r.nField = (u16)pOp->p4.i;
101095 if( pOp->opcode<OP_IdxLT ){
101096 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
101097 r.default_rc = -1;
101099 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
101102 r.aMem = &aMem[pOp->p3];
101108 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
101119 assert( pC->eCurType==CURTYPE_BTREE );
101120 pCur = pC->uc.pCursor;
101138 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
101139 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
101140 res = -res;
101142 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
101163 ** value of the root page that moved - its value before the move occurred -
101174 ** and non-autovacuum modes.
101183 assert( p->readOnly==0 );
101184 assert( pOp->p1>1 );
101186 pOut->flags = MEM_Null;
101187 if( db->nVdbeRead > db->nVDestroy+1 ){
101189 p->errorAction = OE_Abort;
101192 iDb = pOp->p3;
101193 assert( DbMaskTest(p->btreeMask, iDb) );
101195 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
101196 pOut->flags = MEM_Int;
101197 pOut->u.i = iMoved;
101201 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
101221 ** If the P3 value is non-zero, then the row change count is incremented
101233 assert( p->readOnly==0 );
101234 assert( DbMaskTest(p->btreeMask, pOp->p2) );
101235 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, (u32)pOp->p1, &nChange);
101236 if( pOp->p3 ){
101237 p->nChange += nChange;
101238 if( pOp->p3>0 ){
101239 assert( memIsValid(&aMem[pOp->p3]) );
101240 memAboutToChange(p, &aMem[pOp->p3]);
101241 aMem[pOp->p3].u.i += nChange;
101259 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
101260 pC = p->apCsr[pOp->p1];
101263 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
101265 assert( pC->eCurType==CURTYPE_BTREE );
101266 assert( pC->isEphemeral );
101267 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
101276 ** Allocate a new b-tree in the main database file if P1==0 or in the
101280 ** The root page number of the new b-tree is stored in register P2.
101289 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
101290 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101291 assert( DbMaskTest(p->btreeMask, pOp->p1) );
101292 assert( p->readOnly==0 );
101293 pDb = &db->aDb[pOp->p1];
101294 assert( pDb->pBt!=0 );
101295 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
101297 pOut->u.i = pgno;
101310 ** 0x0002 Set db->nAnalysisLimit to P2 while the statements in
101323 db->nSqlExec++;
101326 xAuth = db->xAuth;
101328 mTrace = db->mTrace;
101329 savedAnalysisLimit = db->nAnalysisLimit;
101330 if( pOp->p1 & 0x0001 ){
101332 db->xAuth = 0;
101334 db->mTrace = 0;
101336 if( pOp->p1 & 0x0002 ){
101337 db->nAnalysisLimit = pOp->p2;
101339 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, &zErr);
101340 db->nSqlExec--;
101342 db->xAuth = xAuth;
101344 db->mTrace = mTrace;
101345 db->nAnalysisLimit = savedAnalysisLimit;
101362 ** then runs the new virtual machine. It is thus a re-entrant opcode.
101375 for(iDb=0; iDb<db->nDb; iDb++){
101376 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
101380 iDb = pOp->p1;
101381 assert( iDb>=0 && iDb<db->nDb );
101383 || db->mallocFailed
101384 || (CORRUPT_DB && (db->flags & SQLITE_NoSchemaError)!=0) );
101387 if( pOp->p4.z==0 ){
101388 sqlite3SchemaClear(db->aDb[iDb].pSchema);
101389 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
101390 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
101391 db->mDbFlags |= DBFLAG_SchemaChange;
101392 p->expired = 0;
101399 initData.pzErrMsg = &p->zErrMsg;
101401 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
101404 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
101408 assert( db->init.busy==0 );
101409 db->init.busy = 1;
101412 assert( !db->mallocFailed );
101416 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
101422 db->init.busy = 0;
101443 assert( pOp->p1>=0 && pOp->p1<db->nDb );
101444 rc = sqlite3AnalysisLoad(db, pOp->p1);
101452 ** Remove the internal (in-memory) data structures that describe
101460 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
101466 ** Remove the internal (in-memory) data structures that describe
101474 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
101480 ** Remove the internal (in-memory) data structures that describe
101488 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
101520 assert( p->bIsReader );
101521 assert( pOp->p4type==P4_INTARRAY );
101522 nRoot = pOp->p2;
101523 aRoot = pOp->p4.ai;
101527 assert( pOp->p1>0 && (pOp->p1+1)<=(p->nMem+1 - p->nCursor) );
101528 pnErr = &aMem[pOp->p1];
101529 assert( (pnErr->flags & MEM_Int)!=0 );
101530 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
101531 pIn1 = &aMem[pOp->p1+1];
101532 assert( pOp->p5<db->nDb );
101533 assert( DbMaskTest(p->btreeMask, pOp->p5) );
101534 rc = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1],
101535 &aMem[pOp->p3], nRoot, (int)pnErr->u.i+1, &nErr, &z);
101543 pnErr->u.i -= nErr-1;
101544 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
101561 pIn1 = &aMem[pOp->p1];
101562 pIn2 = &aMem[pOp->p2];
101563 assert( (pIn2->flags & MEM_Int)!=0 );
101564 if( (pIn1->flags & MEM_Blob)==0 ){
101568 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
101583 pIn1 = &aMem[pOp->p1];
101584 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
101585 if( (pIn1->flags & MEM_Blob)==0
101586 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
101595 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
101603 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
101612 ** must have P4==0, the final set must have P4==-1, and for all other sets
101617 ** (b) when P4==-1 there is no need to insert the value, as it will
101627 pIn1 = &aMem[pOp->p1];
101628 pIn3 = &aMem[pOp->p3];
101629 iSet = pOp->p4.i;
101630 assert( pIn3->flags&MEM_Int );
101635 if( (pIn1->flags & MEM_Blob)==0 ){
101639 assert( pOp->p4type==P4_INT32 );
101640 assert( iSet==-1 || iSet>=0 );
101642 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
101647 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
101660 ** cell in an array of values used as arguments to the sub-program. P2
101661 ** contains the address to jump to if the sub-program throws an IGNORE
101666 ** memory required by the sub-vdbe at runtime.
101670 ** If P5 is non-zero, then recursive program invocation is enabled.
101673 int nMem; /* Number of memory registers for sub-program */
101674 i64 nByte; /* Bytes of runtime space required for sub-program */
101678 VdbeFrame *pFrame; /* New vdbe frame to execute in */
101679 SubProgram *pProgram; /* Sub-program to execute */
101682 pProgram = pOp->p4.pProgram;
101683 pRt = &aMem[pOp->p3];
101684 assert( pProgram->nOp>0 );
101687 ** disabled for backwards compatibility (p5 is set if this sub-program
101697 if( pOp->p5 ){
101698 t = pProgram->token;
101699 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
101703 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
101713 if( (pRt->flags&MEM_Blob)==0 ){
101719 nMem = pProgram->nMem + pProgram->nCsr;
101721 if( pProgram->nCsr==0 ) nMem++;
101724 + pProgram->nCsr * sizeof(VdbeCursor*)
101725 + (7 + (i64)pProgram->nOp)/8;
101731 pRt->flags = MEM_Blob|MEM_Dyn;
101732 pRt->z = (char*)pFrame;
101733 pRt->n = (int)nByte;
101734 pRt->xDel = sqlite3VdbeFrameMemDel;
101736 pFrame->v = p;
101737 pFrame->nChildMem = nMem;
101738 pFrame->nChildCsr = pProgram->nCsr;
101739 pFrame->pc = (int)(pOp - aOp);
101740 pFrame->aMem = p->aMem;
101741 pFrame->nMem = p->nMem;
101742 pFrame->apCsr = p->apCsr;
101743 pFrame->nCursor = p->nCursor;
101744 pFrame->aOp = p->aOp;
101745 pFrame->nOp = p->nOp;
101746 pFrame->token = pProgram->token;
101748 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
101751 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
101753 pMem->flags = MEM_Undefined;
101754 pMem->db = db;
101757 pFrame = (VdbeFrame*)pRt->z;
101758 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
101759 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
101760 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
101761 assert( pProgram->nCsr==pFrame->nChildCsr );
101762 assert( (int)(pOp - aOp)==pFrame->pc );
101765 p->nFrame++;
101766 pFrame->pParent = p->pFrame;
101767 pFrame->lastRowid = db->lastRowid;
101768 pFrame->nChange = p->nChange;
101769 pFrame->nDbChange = p->db->nChange;
101770 assert( pFrame->pAuxData==0 );
101771 pFrame->pAuxData = p->pAuxData;
101772 p->pAuxData = 0;
101773 p->nChange = 0;
101774 p->pFrame = pFrame;
101775 p->aMem = aMem = VdbeFrameMem(pFrame);
101776 p->nMem = pFrame->nChildMem;
101777 p->nCursor = (u16)pFrame->nChildCsr;
101778 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
101779 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
101780 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
101781 p->aOp = aOp = pProgram->aOp;
101782 p->nOp = pProgram->nOp;
101788 for(i=0; i<p->nMem; i++){
101789 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
101794 pOp = &aOp[-1];
101800 ** This opcode is only ever present in sub-programs called via the
101802 ** cell of the calling (parent) frame to cell P2 in the current frames
101806 ** The address of the cell in the parent frame is determined by adding
101814 pFrame = p->pFrame;
101815 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
101827 ** If P1 is non-zero, the database constraint counter is incremented
101832 if( pOp->p1 ){
101833 db->nDeferredCons += pOp->p2;
101835 if( db->flags & SQLITE_DeferFKs ){
101836 db->nDeferredImmCons += pOp->p2;
101838 p->nFkConstraint += pOp->p2;
101847 ** This opcode tests if a foreign key constraint-counter is currently zero.
101851 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
101853 ** zero, the jump is taken if the statement constraint-counter is zero
101857 if( pOp->p1 ){
101858 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
101859 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
101861 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
101862 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
101872 ** P1 is a register in the root frame of this VM (the root frame is
101873 ** different from the current frame if this instruction is being executed
101874 ** within a sub-program). Set the value of register P1 to the maximum of
101882 if( p->pFrame ){
101883 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
101884 pIn1 = &pFrame->aMem[pOp->p1];
101886 pIn1 = &aMem[pOp->p1];
101890 pIn2 = &aMem[pOp->p2];
101892 if( pIn1->u.i<pIn2->u.i){
101893 pIn1->u.i = pIn2->u.i;
101900 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
101910 pIn1 = &aMem[pOp->p1];
101911 assert( pIn1->flags&MEM_Int );
101912 VdbeBranchTaken( pIn1->u.i>0, 2);
101913 if( pIn1->u.i>0 ){
101914 pIn1->u.i -= pOp->p3;
101921 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
101934 ** and r[P2] is set to -1.
101940 pIn1 = &aMem[pOp->p1];
101941 pIn3 = &aMem[pOp->p3];
101943 assert( pIn1->flags & MEM_Int );
101944 assert( pIn3->flags & MEM_Int );
101945 x = pIn1->u.i;
101946 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
101954 pOut->u.i = -1;
101956 pOut->u.i = x;
101962 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
101966 ** If it is non-zero (negative or positive) and then also jump to P2.
101970 pIn1 = &aMem[pOp->p1];
101971 assert( pIn1->flags&MEM_Int );
101972 VdbeBranchTaken(pIn1->u.i<0, 2);
101973 if( pIn1->u.i ){
101974 if( pIn1->u.i>0 ) pIn1->u.i--;
101981 ** Synopsis: if (--r[P1])==0 goto P2
101987 pIn1 = &aMem[pOp->p1];
101988 assert( pIn1->flags&MEM_Int );
101989 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
101990 VdbeBranchTaken(pIn1->u.i==0, 2);
101991 if( pIn1->u.i==0 ) goto jump_to_p2;
102041 assert( pOp->p4type==P4_FUNCDEF );
102042 n = pOp->p5;
102043 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
102044 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
102045 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
102047 /* Allocate space for (a) the context object and (n-1) extra pointers
102050 ** cell is 8-byte aligned, even on platforms where a pointer is 32-bits.
102057 pCtx->pOut = (Mem*)((u8*)pCtx + nAlloc);
102058 assert( EIGHT_BYTE_ALIGNMENT(pCtx->pOut) );
102060 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
102061 pCtx->pMem = 0;
102062 pCtx->pFunc = pOp->p4.pFunc;
102063 pCtx->iOp = (int)(pOp - aOp);
102064 pCtx->pVdbe = p;
102065 pCtx->skipFlag = 0;
102066 pCtx->isError = 0;
102067 pCtx->enc = encoding;
102068 pCtx->argc = n;
102069 pOp->p4type = P4_FUNCCTX;
102070 pOp->p4.pCtx = pCtx;
102073 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
102075 pOp->opcode = OP_AggStep1;
102084 assert( pOp->p4type==P4_FUNCCTX );
102085 pCtx = pOp->p4.pCtx;
102086 pMem = &aMem[pOp->p3];
102089 if( pOp->p1 ){
102092 assert( pMem->uTemp==0x1122e0e3 );
102095 pMem->uTemp = 0x1122e0e3;
102103 if( pCtx->pMem != pMem ){
102104 pCtx->pMem = pMem;
102105 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
102109 for(i=0; i<pCtx->argc; i++){
102110 assert( memIsValid(pCtx->argv[i]) );
102111 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
102115 pMem->n++;
102116 assert( pCtx->pOut->flags==MEM_Null );
102117 assert( pCtx->isError==0 );
102118 assert( pCtx->skipFlag==0 );
102120 if( pOp->p1 ){
102121 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
102124 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
102126 if( pCtx->isError ){
102127 if( pCtx->isError>0 ){
102128 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
102129 rc = pCtx->isError;
102131 if( pCtx->skipFlag ){
102132 assert( pOp[-1].opcode==OP_CollSeq );
102133 i = pOp[-1].p1;
102135 pCtx->skipFlag = 0;
102137 sqlite3VdbeMemRelease(pCtx->pOut);
102138 pCtx->pOut->flags = MEM_Null;
102139 pCtx->isError = 0;
102142 assert( pCtx->pOut->flags==MEM_Null );
102143 assert( pCtx->skipFlag==0 );
102176 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
102177 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
102178 pMem = &aMem[pOp->p1];
102179 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
102181 if( pOp->p3 ){
102182 memAboutToChange(p, &aMem[pOp->p3]);
102183 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
102184 pMem = &aMem[pOp->p3];
102188 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
102197 REGISTER_TRACE((int)(pMem-aMem), pMem);
102204 ** Checkpoint database P1. This is a no-op if P1 is not currently in
102211 ** mem[P3+2] are initialized to -1.
102218 assert( p->readOnly==0 );
102220 aRes[1] = aRes[2] = -1;
102221 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
102222 || pOp->p2==SQLITE_CHECKPOINT_FULL
102223 || pOp->p2==SQLITE_CHECKPOINT_RESTART
102224 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
102226 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
102232 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
102249 ** Write a string containing the final journal-mode to register P2.
102261 eNew = pOp->p3;
102270 assert( pOp->p1>=0 && pOp->p1<db->nDb );
102271 assert( p->readOnly==0 );
102273 pBt = db->aDb[pOp->p1].pBt;
102288 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
102296 if( !db->autoCommit || db->nVdbeRead>1 ){
102307 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
102335 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
102336 pOut->z = (char *)sqlite3JournalModename(eNew);
102337 pOut->n = sqlite3Strlen30(pOut->z);
102338 pOut->enc = SQLITE_UTF8;
102356 assert( p->readOnly==0 );
102357 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
102358 pOp->p2 ? &aMem[pOp->p2] : 0);
102374 assert( pOp->p1>=0 && pOp->p1<db->nDb );
102375 assert( DbMaskTest(p->btreeMask, pOp->p1) );
102376 assert( p->readOnly==0 );
102377 pBt = db->aDb[pOp->p1].pBt;
102396 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
102406 assert( pOp->p2==0 || pOp->p2==1 );
102407 if( !pOp->p1 ){
102408 sqlite3ExpirePreparedStatements(db, pOp->p2);
102410 p->expired = pOp->p2+1;
102422 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
102423 pC = p->apCsr[pOp->p1];
102425 assert( pC->eCurType==CURTYPE_BTREE );
102426 sqlite3BtreeCursorPin(pC->uc.pCursor);
102437 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
102438 pC = p->apCsr[pOp->p1];
102440 assert( pC->eCurType==CURTYPE_BTREE );
102441 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
102450 ** the shared-cache feature is enabled.
102456 ** P2 contains the root-page of the table to lock.
102462 u8 isWriteLock = (u8)pOp->p3;
102463 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
102464 int p1 = pOp->p1;
102465 assert( p1>=0 && p1<db->nDb );
102466 assert( DbMaskTest(p->btreeMask, p1) );
102468 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
102471 const char *z = pOp->p4.z;
102493 pVTab = pOp->p4.pVtab;
102495 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
102515 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
102516 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
102517 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
102520 assert( zTab || db->mallocFailed );
102522 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
102537 db->nVDestroy++;
102538 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
102539 db->nVDestroy--;
102540 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
102559 assert( p->bIsReader );
102562 pVtab = pOp->p4.pVtab->pVtab;
102563 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
102567 pModule = pVtab->pModule;
102568 rc = pModule->xOpen(pVtab, &pVCur);
102573 pVCur->pVtab = pVtab;
102576 pCur = allocateCursor(p, pOp->p1, 0, CURTYPE_VTAB);
102578 pCur->uc.pVCur = pVCur;
102579 pVtab->nRef++;
102581 assert( db->mallocFailed );
102582 pModule->xClose(pVCur);
102605 pOut = &aMem[pOp->p2];
102607 assert( pOp->p4type==P4_TABLEREF );
102608 pTab = pOp->p4.pTab;
102610 assert( pTab->nTabRef>0 );
102612 if( pTab->u.vtab.p==0 ) break;
102613 pVtab = pTab->u.vtab.p->pVtab;
102615 pModule = pVtab->pModule;
102617 assert( pModule->iVersion>=4 );
102618 assert( pModule->xIntegrity!=0 );
102619 sqlite3VtabLock(pTab->u.vtab.p);
102620 assert( pOp->p1>=0 && pOp->p1<db->nDb );
102621 rc = pModule->xIntegrity(pVtab, db->aDb[pOp->p1].zDbSName, pTab->zName,
102622 pOp->p3, &zErr);
102623 sqlite3VtabUnlock(pTab->u.vtab.p);
102629 sqlite3VdbeMemSetStr(pOut, zErr, -1, SQLITE_UTF8, sqlite3_free);
102650 pC = p->apCsr[pOp->p1];
102653 pRhs->pCsr = pC->uc.pCursor;
102654 pRhs->pOut = &aMem[pOp->p3];
102656 pOut->flags = MEM_Null;
102696 pQuery = &aMem[pOp->p3];
102698 pCur = p->apCsr[pOp->p1];
102700 REGISTER_TRACE(pOp->p3, pQuery);
102702 assert( pCur->eCurType==CURTYPE_VTAB );
102703 pVCur = pCur->uc.pVCur;
102704 pVtab = pVCur->pVtab;
102705 pModule = pVtab->pModule;
102708 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
102709 nArg = (int)pArgc->u.i;
102710 iQuery = (int)pQuery->u.i;
102713 apArg = p->apArg;
102714 assert( nArg<=p->napArg );
102718 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
102721 res = pModule->xEof(pVCur);
102722 pCur->nullRow = 0;
102733 ** Store in register P3 the value of the P2-th column of
102734 ** the current row of the virtual-table of cursor P1.
102751 VdbeCursor *pCur = p->apCsr[pOp->p1];
102753 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
102754 pDest = &aMem[pOp->p3];
102756 if( pCur->nullRow ){
102760 assert( pCur->eCurType==CURTYPE_VTAB );
102761 pVtab = pCur->uc.pVCur->pVtab;
102762 pModule = pVtab->pModule;
102763 assert( pModule->xColumn );
102770 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
102771 if( pOp->p5 & OPFLAG_NOCHNG ){
102773 pDest->flags = MEM_Null|MEM_Zero;
102774 pDest->u.nZero = 0;
102778 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
102785 REGISTER_TRACE(pOp->p3, pDest);
102806 pCur = p->apCsr[pOp->p1];
102808 assert( pCur->eCurType==CURTYPE_VTAB );
102809 if( pCur->nullRow ){
102812 pVtab = pCur->uc.pVCur->pVtab;
102813 pModule = pVtab->pModule;
102814 assert( pModule->xNext );
102822 rc = pModule->xNext(pCur->uc.pVCur);
102825 res = pModule->xEof(pCur->uc.pVCur);
102847 isLegacy = (db->flags & SQLITE_LegacyAlter);
102848 db->flags |= SQLITE_LegacyAlter;
102849 pVtab = pOp->p4.pVtab->pVtab;
102850 pName = &aMem[pOp->p1];
102851 assert( pVtab->pModule->xRename );
102853 assert( p->readOnly==0 );
102854 REGISTER_TRACE(pOp->p1, pName);
102855 assert( pName->flags & MEM_Str );
102856 testcase( pName->enc==SQLITE_UTF8 );
102857 testcase( pName->enc==SQLITE_UTF16BE );
102858 testcase( pName->enc==SQLITE_UTF16LE );
102861 rc = pVtab->pModule->xRename(pVtab, pName->z);
102862 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
102864 p->expired = 0;
102877 ** invocation. The value in register (P3+P2-1) corresponds to the
102907 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
102908 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
102910 assert( p->readOnly==0 );
102911 if( db->mallocFailed ) goto no_mem;
102913 pVtab = pOp->p4.pVtab->pVtab;
102914 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
102918 pModule = pVtab->pModule;
102919 nArg = pOp->p2;
102920 assert( pOp->p4type==P4_VTAB );
102921 if( ALWAYS(pModule->xUpdate) ){
102922 u8 vtabOnConflict = db->vtabOnConflict;
102923 apArg = p->apArg;
102924 pX = &aMem[pOp->p3];
102925 assert( nArg<=p->napArg );
102932 db->vtabOnConflict = pOp->p5;
102933 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
102934 db->vtabOnConflict = vtabOnConflict;
102936 if( rc==SQLITE_OK && pOp->p1 ){
102937 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
102938 db->lastRowid = rowid;
102940 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
102941 if( pOp->p5==OE_Ignore ){
102944 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
102947 p->nChange++;
102962 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
102982 pBt = db->aDb[pOp->p1].pBt;
102984 if( pOp->p3 ){
102986 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
102988 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
103003 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
103022 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
103031 ** purely non-deterministic. Some built-in date/time functions can be
103032 ** either deterministic of non-deterministic, depending on their arguments.
103033 ** When those function are used in a non-deterministic way, they will check
103044 assert( pOp->p4type==P4_FUNCCTX );
103045 pCtx = pOp->p4.pCtx;
103051 pOut = &aMem[pOp->p3];
103052 if( pCtx->pOut != pOut ){
103053 pCtx->pVdbe = p;
103054 pCtx->pOut = pOut;
103055 pCtx->enc = encoding;
103056 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
103058 assert( pCtx->pVdbe==p );
103062 for(i=0; i<pCtx->argc; i++){
103063 assert( memIsValid(pCtx->argv[i]) );
103064 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
103068 assert( pCtx->isError==0 );
103069 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
103072 if( pCtx->isError ){
103073 if( pCtx->isError>0 ){
103075 rc = pCtx->isError;
103077 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
103078 pCtx->isError = 0;
103082 assert( (pOut->flags&MEM_Str)==0
103083 || pOut->enc==encoding
103084 || db->mallocFailed );
103087 REGISTER_TRACE(pOp->p3, pOut);
103098 pIn1 = &aMem[pOp->p1];
103099 pIn1->flags &= ~MEM_Subtype;
103110 pIn1 = &aMem[pOp->p1];
103111 pOut = &aMem[pOp->p2];
103112 if( pIn1->flags & MEM_Subtype ){
103113 sqlite3VdbeMemSetInt64(pOut, pIn1->eSubtype);
103127 pIn1 = &aMem[pOp->p1];
103128 pOut = &aMem[pOp->p2];
103129 if( pIn1->flags & MEM_Null ){
103130 pOut->flags &= ~MEM_Subtype;
103132 assert( pIn1->flags & MEM_Int );
103133 pOut->flags |= MEM_Subtype;
103134 pOut->eSubtype = (u8)(pIn1->u.i & 0xff);
103148 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
103149 pIn1 = &aMem[pOp->p1];
103150 assert( pIn1->flags & MEM_Blob );
103151 assert( pIn1->n>0 );
103154 if( db->flags&SQLITE_VdbeTrace ){
103156 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
103159 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
103162 h %= (pIn1->n*8);
103163 pIn1->z[h/8] |= 1<<(h&7);
103179 ** false positive - if the jump is taken when it should fall through.
103184 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
103185 pIn1 = &aMem[pOp->p1];
103186 assert( (pIn1->flags & MEM_Blob)!=0 );
103187 assert( pIn1->n >= 1 );
103190 if( db->flags&SQLITE_VdbeTrace ){
103192 for(ii=pOp->p3; ii<pOp->p3+pOp->p4.i; ii++){
103195 printf("hash: %llu modulo %d -> %u\n", h, pIn1->n, (int)(h%pIn1->n));
103198 h %= (pIn1->n*8);
103199 if( (pIn1->z[h/8] & (1<<(h&7)))==0 ){
103201 p->aCounter[SQLITE_STMTSTATUS_FILTER_HIT]++;
103204 p->aCounter[SQLITE_STMTSTATUS_FILTER_MISS]++;
103224 ** the UTF-8 string contained in P4 is emitted on the trace callback.
103243 ** The "--" string is broken up to prevent false-positives with srcck1.c.
103246 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
103248 ** using the X argument when X begins with "--" and invoking
103251 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
103254 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
103257 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
103258 && p->minWriteFileFormat!=254 /* tag-20220401a */
103259 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
103262 if( db->mTrace & SQLITE_TRACE_LEGACY ){
103264 db->trace.xLegacy(db->pTraceArg, z);
103268 if( db->nVdbeExec>1 ){
103269 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
103270 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
103273 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
103277 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
103280 for(j=0; j<db->nDb; j++){
103281 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
103282 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
103287 if( (db->flags & SQLITE_SqlTrace)!=0
103288 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
103290 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
103294 assert( pOp->p2>0 );
103295 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
103296 if( pOp->opcode==OP_Trace ) break;
103297 for(i=1; i<p->nOp; i++){
103298 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
103300 pOp->p1 = 0;
103302 pOp->p1++;
103303 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
103313 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
103318 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
103319 assert( pOp->p4type==P4_EXPR );
103320 pC = p->apCsr[pOp->p1];
103322 assert( pC->eCurType==CURTYPE_BTREE );
103323 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
103324 pOp->p4.pExpr, aMem);
103381 assert( pOp->p1>0 );
103382 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
103383 pMem = &aMem[pOp->p1];
103384 constMask = pOp->p3;
103385 for(i=0; i<pOp->p2; i++, pMem++){
103387 pMem->pScopyFrom = 0;
103388 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
103405 ** The P4 value is human-readable text that describes the query plan
103410 ** then this element is a top-level element.
103422 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
103429 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
103451 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
103454 if( db->flags & SQLITE_VdbeTrace ){
103455 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
103458 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
103461 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
103478 if( db->mallocFailed ){
103485 if( db->flags & SQLITE_VdbeTrace ){
103486 const char *zTrace = p->zSql;
103493 printf("ABORT-due-to-error (rc=%d): %s\n", rc, zTrace);
103496 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
103499 p->rc = rc;
103503 (int)(pOp - aOp), p->zErrMsg, p->zSql);
103504 if( p->eVdbeState==VDBE_RUN_STATE ) sqlite3VdbeHalt(p);
103506 if( rc==SQLITE_CORRUPT && db->autoCommit==0 ){
103507 db->flags |= SQLITE_CorruptRdOnly;
103511 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
103531 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
103532 nProgressLimit += db->nProgressOps;
103533 if( db->xProgress(db->pProgressArg) ){
103540 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
103541 if( DbMaskNonZero(p->lockMask) ){
103545 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
103569 assert( AtomicLoad(&db->u1.isInterrupted) );
103615 ** the b-tree cursor associated with blob handle p to point to row iRow.
103626 ** If an error does occur, then the b-tree cursor is closed. All subsequent
103633 Vdbe *v = (Vdbe *)p->pStmt;
103638 sqlite3VdbeMemSetInt64(&v->aMem[1], iRow);
103644 if( v->pc>4 ){
103645 v->pc = 4;
103646 assert( v->aOp[v->pc].opcode==OP_NotExists );
103649 rc = sqlite3_step(p->pStmt);
103652 VdbeCursor *pC = v->apCsr[0];
103655 assert( pC->eCurType==CURTYPE_BTREE );
103656 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
103657 testcase( pC->nHdrParsed==p->iCol );
103658 testcase( pC->nHdrParsed==p->iCol+1 );
103660 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
103664 sqlite3_finalize(p->pStmt);
103665 p->pStmt = 0;
103667 p->iOffset = pC->aType[p->iCol + pC->nField];
103668 p->nByte = sqlite3VdbeSerialTypeLen(type);
103669 p->pCsr = pC->uc.pCursor;
103670 sqlite3BtreeIncrblobCursor(p->pCsr);
103676 }else if( p->pStmt ){
103677 rc = sqlite3_finalize(p->pStmt);
103678 p->pStmt = 0;
103680 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
103683 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
103703 int wrFlag, /* True -> read/write access, false -> read-only */
103707 int iCol; /* Index of zColumn in row-record */
103728 sqlite3_mutex_enter(db->mutex);
103747 if( pTab && (pTab->tabFlags&TF_HasGenerated)!=0 ){
103759 || ((iDb = sqlite3SchemaToIndex(db, pTab->pSchema))==1 &&
103771 pBlob->pTab = pTab;
103772 pBlob->zDb = db->aDb[iDb].zDbSName;
103791 if( db->flags&SQLITE_ForeignKeys ){
103798 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
103800 for(j=0; j<pFKey->nCol; j++){
103801 if( pFKey->aCol[j].iFrom==iCol ){
103808 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
103810 for(j=0; j<pIdx->nKeyCol; j++){
103812 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
103826 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
103827 assert( pBlob->pStmt || db->mallocFailed );
103828 if( pBlob->pStmt ){
103832 ** of writing code to use the b-tree layer directly is that the
103837 ** Code external to the Vdbe then "borrows" the b-tree cursor and
103842 ** which closes the b-tree cursor and (possibly) commits the
103855 Vdbe *v = (Vdbe *)pBlob->pStmt;
103859 pTab->pSchema->schema_cookie,
103860 pTab->pSchema->iGeneration);
103862 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
103868 if( db->mallocFailed==0 ){
103875 aOp[0].p2 = pTab->tnum;
103877 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
103879 if( db->mallocFailed==0 ){
103883 ** parameter of the other to pTab->tnum. */
103885 aOp[1].p2 = pTab->tnum;
103896 aOp[1].p4.i = pTab->nCol+1;
103897 aOp[3].p2 = pTab->nCol;
103906 pBlob->iCol = iCol;
103907 pBlob->db = db;
103909 if( db->mallocFailed ){
103918 if( rc==SQLITE_OK && db->mallocFailed==0 ){
103921 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
103928 sqlite3_mutex_leave(db->mutex);
103942 sqlite3_stmt *pStmt = p->pStmt;
103943 db = p->db;
103944 sqlite3_mutex_enter(db->mutex);
103946 sqlite3_mutex_leave(db->mutex);
103970 db = p->db;
103971 sqlite3_mutex_enter(db->mutex);
103972 v = (Vdbe*)p->pStmt;
103974 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
103978 /* If there is no statement handle, then the blob-handle has
103984 ** returned, clean-up the statement handle.
103986 assert( db == v->db );
103987 sqlite3BtreeEnterCursor(p->pCsr);
103990 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
103991 /* If a pre-update hook is registered and this is a write cursor,
103994 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
104001 ** using the incremental-blob API, this works. For the sessions module
104005 iKey = sqlite3BtreeIntegerKey(p->pCsr);
104006 assert( v->apCsr[0]!=0 );
104007 assert( v->apCsr[0]->eCurType==CURTYPE_BTREE );
104009 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1, p->iCol
104014 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
104015 sqlite3BtreeLeaveCursor(p->pCsr);
104018 p->pStmt = 0;
104020 v->rc = rc;
104025 sqlite3_mutex_leave(db->mutex);
104051 return (p && p->pStmt) ? p->nByte : 0;
104070 db = p->db;
104071 sqlite3_mutex_enter(db->mutex);
104073 if( p->pStmt==0 ){
104074 /* If there is no statement handle, then the blob-handle has
104080 ((Vdbe*)p->pStmt)->rc = SQLITE_OK;
104090 assert( rc==SQLITE_OK || p->pStmt==0 );
104091 sqlite3_mutex_leave(db->mutex);
104100 ** 2011-07-09
104115 ** The VdbeSorter object implements a multi-threaded external merge sort
104119 ** Here is the (internal, non-API) interface between this module and the
104172 ** an in-memory merge sort. In this case, no temporary files are required
104179 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
104180 ** of PMAs may be created by merging existing PMAs together - for example
104181 ** merging two or more level-0 PMAs together creates a level-1 PMA.
104185 ** page-cache of the main database. Specifically, the threshold is set to
104189 ** If the sorter is running in single-threaded mode, then all PMAs generated
104191 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
104198 ** The sorter is running in multi-threaded mode if (a) the library was built
104199 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
104208 ** sorter is running in single-threaded mode, then these PMAs are merged
104213 ** Or, if running in multi-threaded mode, then a background thread is
104223 ** Rewind() is called, then a hierarchy of incremental-merges is used.
104229 ** If running in multi-threaded mode and there are more than
104242 ** characteristics of the sorter in multi-threaded mode.
104249 ** Hard-coded maximum amount of data to accumulate in memory before flushing
104262 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
104264 typedef struct SorterList SorterList; /* In-memory list of records */
104277 ** An in-memory list of objects to be sorted.
104286 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
104309 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
104323 ** aReadr[0] -> Banana
104324 ** aReadr[1] -> Feijoa
104325 ** aReadr[2] -> Elderberry
104326 ** aReadr[3] -> Currant
104327 ** aReadr[4] -> Grapefruit
104328 ** aReadr[5] -> Apple
104329 ** aReadr[6] -> Durian
104330 ** aReadr[7] -> EOF
104339 ** aReadr[5] -> Eggplant
104344 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
104367 ** single-threaded operation, there is exactly one instance of this object
104368 ** and for multi-threaded operation there are two or more instances.
104379 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
104397 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
104401 SorterFile file; /* Temp file for level-0 PMAs */
104425 SorterList list; /* List of in-memory records */
104471 ** There are two types of IncrMerger object - single (bUseThread==0) and
104472 ** multi-threaded (bUseThread==1).
104474 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
104489 ** A single-threaded IncrMerger does not open any temporary files of its
104491 ** at offset iStartOff of file pTask->file2. And instead of using a
104493 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
104511 ** size. But I/O is more efficient if it occurs in page-sized blocks where
104513 ** the PMA so that aligned, page-size blocks are written.
104516 int eFWErr; /* Non-zero if in an error state */
104530 ** by this module. If using a separate allocation for each in-memory record
104538 ** has finished passing records to the sorter, or when the in-memory buffer
104571 sqlite3_free(pReadr->aAlloc);
104572 sqlite3_free(pReadr->aBuffer);
104573 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
104574 vdbeIncrFree(pReadr->pIncr);
104595 if( p->aMap ){
104596 *ppOut = &p->aMap[p->iReadOff];
104597 p->iReadOff += nByte;
104601 assert( p->aBuffer );
104604 ** p->nBuffer bytes of data from the file into it. Or, if there are less
104605 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
104606 iBuf = p->iReadOff % p->nBuffer;
104612 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
104613 nRead = p->nBuffer;
104615 nRead = (int)(p->iEof - p->iReadOff);
104620 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
104624 nAvail = p->nBuffer - iBuf;
104627 /* The requested data is available in the in-memory buffer. In this
104630 *ppOut = &p->aBuffer[iBuf];
104631 p->iReadOff += nByte;
104633 /* The requested data is not all available in the in-memory buffer.
104634 ** In this case, allocate space at p->aAlloc[] to copy the requested
104635 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
104638 /* Extend the p->aAlloc[] allocation if required. */
104639 if( p->nAlloc<nByte ){
104641 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
104643 aNew = sqlite3Realloc(p->aAlloc, nNew);
104645 p->nAlloc = nNew;
104646 p->aAlloc = aNew;
104650 ** p->aAlloc[]. */
104651 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
104652 p->iReadOff += nAvail;
104653 nRem = nByte - nAvail;
104655 /* The following loop copies up to p->nBuffer bytes per iteration into
104656 ** the p->aAlloc[] buffer. */
104663 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
104666 assert( aNext!=p->aAlloc );
104668 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
104669 nRem -= nCopy;
104672 *ppOut = p->aAlloc;
104685 if( p->aMap ){
104686 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
104688 iBuf = p->iReadOff % p->nBuffer;
104689 if( iBuf && (p->nBuffer-iBuf)>=9 ){
104690 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
104717 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
104718 sqlite3_file *pFd = pFile->pFd;
104719 if( pFd->pMethods->iVersion>=3 ){
104720 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
104740 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
104743 if( pReadr->aMap ){
104744 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
104745 pReadr->aMap = 0;
104747 pReadr->iReadOff = iOff;
104748 pReadr->iEof = pFile->iEof;
104749 pReadr->pFd = pFile->pFd;
104751 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
104752 if( rc==SQLITE_OK && pReadr->aMap==0 ){
104753 int pgsz = pTask->pSorter->pgsz;
104754 int iBuf = pReadr->iReadOff % pgsz;
104755 if( pReadr->aBuffer==0 ){
104756 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
104757 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
104758 pReadr->nBuffer = pgsz;
104761 int nRead = pgsz - iBuf;
104762 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
104763 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
104766 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
104784 if( pReadr->iReadOff>=pReadr->iEof ){
104785 IncrMerger *pIncr = pReadr->pIncr;
104789 if( rc==SQLITE_OK && pIncr->bEof==0 ){
104791 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
104809 pReadr->nKey = (int)nRec;
104810 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
104819 ** starting at offset iStart and ending at offset iEof-1. This function
104824 ** contains a single PMA, and that that PMA omits the initial length varint.
104835 assert( pFile->iEof>iStart );
104836 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
104837 assert( pReadr->aBuffer==0 );
104838 assert( pReadr->aMap==0 );
104844 pReadr->iEof = pReadr->iReadOff + nByte;
104861 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104865 UnpackedRecord *r2 = pTask->pUnpacked;
104867 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
104875 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
104879 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
104880 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
104883 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
104888 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104892 UnpackedRecord *r2 = pTask->pUnpacked;
104894 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
104907 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104922 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
104924 res = n1 - n2;
104928 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
104934 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
104935 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
104936 res = res * -1;
104949 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
104971 if( (res = v1[i] - v2[i])!=0 ){
104973 res = v1[0] & 0x80 ? -1 : +1;
104979 res = s1 - s2;
104984 res = -1;
104986 res = s1 - s2;
104991 if( *v1 & 0x80 ) res = -1;
104998 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
105003 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
105004 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
105005 res = res * -1;
105014 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
105017 ** is non-zero and the sorter is able to guarantee a stable sort, nField
105025 ** The sorter can guarantee a stable sort when running in single-threaded
105026 ** mode, but not in multi-threaded mode.
105038 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
105039 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
105053 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
105061 nWorker = SORTER_MAX_MERGE_COUNT-1;
105065 assert( pCsr->pKeyInfo );
105066 assert( !pCsr->isEphemeral );
105067 assert( pCsr->eCurType==CURTYPE_SORTER );
105068 assert( sizeof(KeyInfo) + UMXV(pCsr->pKeyInfo->nKeyField)*sizeof(CollSeq*)
105070 szKeyInfo = SZ_KEYINFO(pCsr->pKeyInfo->nKeyField);
105074 pCsr->uc.pSorter = pSorter;
105078 Btree *pBt = db->aDb[0].pBt;
105079 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
105080 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
105081 pKeyInfo->db = 0;
105083 pKeyInfo->nKeyField = nField;
105086 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
105088 pSorter->nTask = nWorker + 1;
105089 pSorter->iPrev = (u8)(nWorker - 1);
105090 pSorter->bUseThreads = (pSorter->nTask>1);
105091 pSorter->db = db;
105092 for(i=0; i<pSorter->nTask; i++){
105093 SortSubtask *pTask = &pSorter->aTask[i];
105094 pTask->pSorter = pSorter;
105100 pSorter->mnPmaSize = szPma * pgsz;
105102 mxCache = db->aDb[0].pSchema->cache_size;
105104 /* A negative cache-size value C indicates that the cache is abs(C)
105106 mxCache = mxCache * -1024;
105111 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
105116 assert( pSorter->iMemory==0 );
105117 pSorter->nMemory = pgsz;
105118 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
105119 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
105123 if( pKeyInfo->nAllField<13
105124 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
105125 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
105127 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
105142 pNext = p->u.pNext;
105152 sqlite3DbFree(db, pTask->pUnpacked);
105154 /* pTask->list.aMemory can only be non-zero if it was handed memory
105156 if( pTask->list.aMemory ){
105157 sqlite3_free(pTask->list.aMemory);
105161 assert( pTask->list.aMemory==0 );
105162 vdbeSorterRecordFree(0, pTask->list.pList);
105164 if( pTask->file.pFd ){
105165 sqlite3OsCloseFree(pTask->file.pFd);
105167 if( pTask->file2.pFd ){
105168 sqlite3OsCloseFree(pTask->file2.pFd);
105176 int iTask = (pTask - pTask->pSorter->aTask);
105177 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105191 int iTask = (pTask - pTask->pSorter->aTask);
105192 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105202 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
105215 ** Join thread pTask->thread.
105219 if( pTask->pThread ){
105221 int bDone = pTask->bDone;
105225 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
105228 assert( pTask->bDone==1 );
105229 pTask->bDone = 0;
105230 pTask->pThread = 0;
105243 assert( pTask->pThread==0 && pTask->bDone==0 );
105244 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
105249 ** level-0 PMAs.
105258 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
105261 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
105262 for(i=pSorter->nTask-1; i>=0; i--){
105263 SortSubtask *pTask = &pSorter->aTask[i];
105293 pNew->nTree = N;
105294 pNew->pTask = 0;
105295 pNew->aReadr = (PmaReader*)&pNew[1];
105296 pNew->aTree = (int*)&pNew->aReadr[N];
105307 for(i=0; i<pMerger->nTree; i++){
105308 vdbePmaReaderClear(&pMerger->aReadr[i]);
105321 if( pIncr->bUseThread ){
105322 vdbeSorterJoinThread(pIncr->pTask);
105323 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
105324 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
105327 vdbeMergeEngineFree(pIncr->pMerger);
105338 assert( pSorter->bUseThreads || pSorter->pReader==0 );
105340 if( pSorter->pReader ){
105341 vdbePmaReaderClear(pSorter->pReader);
105342 sqlite3DbFree(db, pSorter->pReader);
105343 pSorter->pReader = 0;
105346 vdbeMergeEngineFree(pSorter->pMerger);
105347 pSorter->pMerger = 0;
105348 for(i=0; i<pSorter->nTask; i++){
105349 SortSubtask *pTask = &pSorter->aTask[i];
105351 pTask->pSorter = pSorter;
105353 if( pSorter->list.aMemory==0 ){
105354 vdbeSorterRecordFree(0, pSorter->list.pList);
105356 pSorter->list.pList = 0;
105357 pSorter->list.szPMA = 0;
105358 pSorter->bUsePMA = 0;
105359 pSorter->iMemory = 0;
105360 pSorter->mxKeysize = 0;
105361 sqlite3DbFree(db, pSorter->pUnpacked);
105362 pSorter->pUnpacked = 0;
105370 assert( pCsr->eCurType==CURTYPE_SORTER );
105371 pSorter = pCsr->uc.pSorter;
105374 sqlite3_free(pSorter->list.aMemory);
105376 pCsr->uc.pSorter = 0;
105382 ** The first argument is a file-handle open on a temporary file. The file
105391 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
105405 ** Allocate space for a file-handle and open a temporary file. If successful,
105406 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
105416 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
105433 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
105437 if( pTask->pUnpacked==0 ){
105438 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
105439 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
105440 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
105441 pTask->pUnpacked->errCode = 0;
105462 res = pTask->xCompare(
105463 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
105468 pp = &p1->u.pNext;
105469 p1 = p1->u.pNext;
105476 pp = &p2->u.pNext;
105477 p2 = p2->u.pNext;
105493 if( p->typeMask==SORTER_TYPE_INTEGER ){
105495 }else if( p->typeMask==SORTER_TYPE_TEXT ){
105502 ** Sort the linked list of records headed at pTask->pList. Return
105515 p = pList->pList;
105516 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
105521 if( pList->aMemory ){
105522 if( (u8*)p==pList->aMemory ){
105525 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
105526 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
105529 pNext = p->u.pNext;
105532 p->u.pNext = 0;
105535 /* ,--Each aSlot[] holds twice as much as the previous. So we cannot use
105536 ** | up all 64 aSlots[] with only a 64-bit address space.
105550 pList->pList = p;
105552 assert( pTask->pUnpacked->errCode==SQLITE_OK
105553 || pTask->pUnpacked->errCode==SQLITE_NOMEM
105555 return pTask->pUnpacked->errCode;
105559 ** Initialize a PMA-writer object.
105568 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
105569 if( !p->aBuffer ){
105570 p->eFWErr = SQLITE_NOMEM_BKPT;
105572 p->iBufEnd = p->iBufStart = (iStart % nBuf);
105573 p->iWriteOff = iStart - p->iBufStart;
105574 p->nBuffer = nBuf;
105575 p->pFd = pFd;
105585 while( nRem>0 && p->eFWErr==0 ){
105587 if( nCopy>(p->nBuffer - p->iBufEnd) ){
105588 nCopy = p->nBuffer - p->iBufEnd;
105591 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
105592 p->iBufEnd += nCopy;
105593 if( p->iBufEnd==p->nBuffer ){
105594 p->eFWErr = sqlite3OsWrite(p->pFd,
105595 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
105596 p->iWriteOff + p->iBufStart
105598 p->iBufStart = p->iBufEnd = 0;
105599 p->iWriteOff += p->nBuffer;
105601 assert( p->iBufEnd<p->nBuffer );
105603 nRem -= nCopy;
105608 ** Flush any buffered data to disk and clean up the PMA-writer object.
105609 ** The results of using the PMA-writer after this call are undefined.
105618 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
105619 p->eFWErr = sqlite3OsWrite(p->pFd,
105620 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
105621 p->iWriteOff + p->iBufStart
105624 *piEof = (p->iWriteOff + p->iBufEnd);
105625 sqlite3_free(p->aBuffer);
105626 rc = p->eFWErr;
105643 ** Write the current contents of in-memory linked-list pList to a level-0
105644 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
105652 ** * One or more records packed end-to-end in order of ascending keys.
105657 sqlite3 *db = pTask->pSorter->db;
105662 /* Set iSz to the expected size of file pTask->file after writing the PMA.
105664 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
105669 assert( pList->szPMA>0 );
105672 if( pTask->file.pFd==0 ){
105673 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
105674 assert( rc!=SQLITE_OK || pTask->file.pFd );
105675 assert( pTask->file.iEof==0 );
105676 assert( pTask->nPMA==0 );
105681 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
105693 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
105694 pTask->file.iEof);
105695 pTask->nPMA++;
105696 vdbePmaWriteVarint(&writer, pList->szPMA);
105697 for(p=pList->pList; p; p=pNext){
105698 pNext = p->u.pNext;
105699 vdbePmaWriteVarint(&writer, p->nVal);
105700 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
105701 if( pList->aMemory==0 ) sqlite3_free(p);
105703 pList->pList = p;
105704 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
105708 assert( rc!=SQLITE_OK || pList->pList==0 );
105709 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
105725 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
105726 SortSubtask *pTask = pMerger->pTask;
105729 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
105740 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
105741 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
105743 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
105746 if( pReadr1->pFd==0 ){
105748 }else if( pReadr2->pFd==0 ){
105749 iRes = -1;
105751 iRes = pTask->xCompare(pTask, &bCached,
105752 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
105758 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
105763 ** was actually called above, then pTask->pUnpacked now contains
105772 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
105773 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
105776 if( pReadr1->pFd ) bCached = 0;
105777 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
105778 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
105781 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
105784 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
105789 ** The main routine for background threads that write level-0 PMAs.
105794 assert( pTask->bDone==0 );
105795 rc = vdbeSorterListToPMA(pTask, &pTask->list);
105796 pTask->bDone = 1;
105807 pSorter->bUsePMA = 1;
105808 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
105813 int nWorker = (pSorter->nTask-1);
105817 pSorter->bUsePMA = 1;
105819 /* Select a sub-task to sort and flush the current list of in-memory
105820 ** records to disk. If the sorter is running in multi-threaded mode,
105821 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
105822 ** the background thread from a sub-tasks previous turn is still running,
105823 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
105824 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
105825 ** sub-tasks are preferred as they use background threads - the final
105826 ** sub-task uses the main thread. */
105828 int iTest = (pSorter->iPrev + i + 1) % nWorker;
105829 pTask = &pSorter->aTask[iTest];
105830 if( pTask->bDone ){
105833 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
105839 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
105846 assert( pTask->pThread==0 && pTask->bDone==0 );
105847 assert( pTask->list.pList==0 );
105848 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
105850 aMem = pTask->list.aMemory;
105852 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
105853 pTask->list = pSorter->list;
105854 pSorter->list.pList = 0;
105855 pSorter->list.szPMA = 0;
105857 pSorter->list.aMemory = aMem;
105858 pSorter->nMemory = sqlite3MallocSize(aMem);
105859 }else if( pSorter->list.aMemory ){
105860 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
105861 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
105887 assert( pCsr->eCurType==CURTYPE_SORTER );
105888 pSorter = pCsr->uc.pSorter;
105889 getVarint32NR((const u8*)&pVal->z[1], t);
105891 pSorter->typeMask &= SORTER_TYPE_INTEGER;
105893 pSorter->typeMask &= SORTER_TYPE_TEXT;
105895 pSorter->typeMask = 0;
105903 ** If using the single large allocation mode (pSorter->aMemory!=0), then
105910 ** * The total memory allocated for the in-memory list is greater
105911 ** than (page-size * cache-size), or
105913 ** * The total memory allocated for the in-memory list is greater
105914 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
105916 nReq = pVal->n + sizeof(SorterRecord);
105917 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
105918 if( pSorter->mxPmaSize ){
105919 if( pSorter->list.aMemory ){
105920 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
105923 (pSorter->list.szPMA > pSorter->mxPmaSize)
105924 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
105929 pSorter->list.szPMA = 0;
105930 pSorter->iMemory = 0;
105931 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
105935 pSorter->list.szPMA += nPMA;
105936 if( nPMA>pSorter->mxKeysize ){
105937 pSorter->mxKeysize = nPMA;
105940 if( pSorter->list.aMemory ){
105941 int nMin = pSorter->iMemory + nReq;
105943 if( nMin>pSorter->nMemory ){
105945 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
105946 int iListOff = -1;
105947 if( pSorter->list.pList ){
105948 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
105951 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
105953 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
105956 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
105958 pSorter->list.aMemory = aNew;
105959 pSorter->nMemory = nNew;
105962 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
105963 pSorter->iMemory += ROUND8(nReq);
105964 if( pSorter->list.pList ){
105965 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
105972 pNew->u.pNext = pSorter->list.pList;
105975 memcpy(SRVAL(pNew), pVal->z, pVal->n);
105976 pNew->nVal = pVal->n;
105977 pSorter->list.pList = pNew;
105983 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
105985 ** except that the number-of-bytes varint is omitted from the start.
105990 i64 iStart = pIncr->iStartOff;
105991 SorterFile *pOut = &pIncr->aFile[1];
105992 SortSubtask *pTask = pIncr->pTask;
105993 MergeEngine *pMerger = pIncr->pMerger;
105995 assert( pIncr->bEof==0 );
105999 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
106002 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
106003 int nKey = pReader->nKey;
106008 if( pReader->pFd==0 ) break;
106009 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
106013 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
106014 assert( pIncr->pMerger->pTask==pTask );
106015 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
106018 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
106027 ** multi-threaded IncrMerger objects.
106032 pIncr->pTask->bDone = 1;
106041 assert( pIncr->bUseThread );
106042 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
106052 ** For single-threaded objects, this is accomplished by literally reading
106053 ** keys from pIncr->pMerger and repopulating aFile[0].
106055 ** For multi-threaded objects, all that is required is to wait until the
106067 if( pIncr->bUseThread ){
106068 rc = vdbeSorterJoinThread(pIncr->pTask);
106071 SorterFile f0 = pIncr->aFile[0];
106072 pIncr->aFile[0] = pIncr->aFile[1];
106073 pIncr->aFile[1] = f0;
106077 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
106078 pIncr->bEof = 1;
106087 pIncr->aFile[0] = pIncr->aFile[1];
106088 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
106089 pIncr->bEof = 1;
106111 pIncr->pMerger = pMerger;
106112 pIncr->pTask = pTask;
106113 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
106114 pTask->file2.iEof += pIncr->mxSz;
106125 ** Set the "use-threads" flag on object pIncr.
106128 pIncr->bUseThread = 1;
106129 pIncr->pTask->file2.iEof -= pIncr->mxSz;
106136 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
106142 int iOut /* Store the result in pMerger->aTree[iOut] */
106150 assert( iOut<pMerger->nTree && iOut>0 );
106152 if( iOut>=(pMerger->nTree/2) ){
106153 i1 = (iOut - pMerger->nTree/2) * 2;
106156 i1 = pMerger->aTree[iOut*2];
106157 i2 = pMerger->aTree[iOut*2+1];
106160 p1 = &pMerger->aReadr[i1];
106161 p2 = &pMerger->aReadr[i2];
106163 if( p1->pFd==0 ){
106165 }else if( p2->pFd==0 ){
106168 SortSubtask *pTask = pMerger->pTask;
106171 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
106172 res = pTask->xCompare(
106173 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
106182 pMerger->aTree[iOut] = iRes;
106189 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
106235 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
106239 assert( pMerger->pTask==0 );
106240 pMerger->pTask = pTask;
106242 nTree = pMerger->nTree;
106247 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
106250 ** on this PmaReader before any of the multi-threaded PmaReaders takes
106251 ** better advantage of multi-processor hardware. */
106252 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
106254 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
106259 for(i=pMerger->nTree-1; i>0; i--){
106262 return pTask->pUnpacked->errCode;
106267 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
106269 ** object at (pReadr->pIncr).
106272 ** in the sub-tree headed by pReadr are also initialized. Data is then
106277 ** to be a multi-threaded PmaReader and this function is being called in a
106278 ** background thread. In this case all PmaReaders in the sub-tree are
106286 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
106287 ** this entire function is being run by thread (pTask->thread), that will
106291 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
106292 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
106300 IncrMerger *pIncr = pReadr->pIncr;
106301 SortSubtask *pTask = pIncr->pTask;
106302 sqlite3 *db = pTask->pSorter->db;
106304 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
106307 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
106309 /* Set up the required files for pIncr. A multi-threaded IncrMerge object
106310 ** requires two temp files to itself, whereas a single-threaded object
106311 ** only requires a region of pTask->file2. */
106313 int mxSz = pIncr->mxSz;
106315 if( pIncr->bUseThread ){
106316 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
106318 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
106322 /*if( !pIncr->bUseThread )*/{
106323 if( pTask->file2.pFd==0 ){
106324 assert( pTask->file2.iEof>0 );
106325 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
106326 pTask->file2.iEof = 0;
106329 pIncr->aFile[1].pFd = pTask->file2.pFd;
106330 pIncr->iStartOff = pTask->file2.iEof;
106331 pTask->file2.iEof += mxSz;
106337 if( rc==SQLITE_OK && pIncr->bUseThread ){
106339 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
106341 ** pIncr->pTask->thread.
106370 pReader->pIncr->pTask->bDone = 1;
106376 ** If the PmaReader passed as the first argument is not an incremental-reader
106377 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
106381 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
106387 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
106391 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
106392 if( pIncr->bUseThread ){
106394 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
106405 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
106406 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
106411 ** first PMA to read from pTask->file. Assuming no error occurs, it is
106419 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
106420 MergeEngine **ppOut /* OUT: New merge-engine */
106432 PmaReader *pReadr = &pNew->aReadr[i];
106433 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
106434 iOff = pReadr->iEof;
106451 ** nPMA<=16 -> TreeDepth() == 0
106452 ** nPMA<=256 -> TreeDepth() == 1
106453 ** nPMA<=65536 -> TreeDepth() == 2
106466 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
106494 PmaReader *pReadr = &p->aReadr[iIter];
106496 if( pReadr->pIncr==0 ){
106501 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
106505 p = pReadr->pIncr->pMerger;
106511 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
106520 ** that has already written two or more level-0 PMAs to one or more temp
106538 /* If the sorter uses more than one task, then create the top-level
106540 ** one PmaReader per sub-task. */
106541 assert( pSorter->bUseThreads || pSorter->nTask==1 );
106542 if( pSorter->nTask>1 ){
106543 pMain = vdbeMergeEngineNew(pSorter->nTask);
106548 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
106549 SortSubtask *pTask = &pSorter->aTask[iTask];
106550 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
106551 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
106553 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
106556 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
106557 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
106563 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
106564 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
106565 int nReader; /* Number of level-0 PMAs to merge */
106567 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
106578 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
106603 ** (for multi-threaded sorters) so that it can be used to iterate through
106610 SortSubtask *pTask0 = &pSorter->aTask[0];
106613 sqlite3 *db = pTask0->pSorter->db;
106616 for(i=0; i<pSorter->nTask; i++){
106617 pSorter->aTask[i].xCompare = xCompare;
106624 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
106625 if( pSorter->bUseThreads ){
106628 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
106632 pSorter->pReader = pReadr;
106636 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
106638 vdbeIncrMergerSetThreads(pReadr->pIncr);
106639 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
106641 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
106643 assert( pIncr->pTask!=pLast );
106646 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
106651 ** b) If it is using task (nTask-1), it is configured to run
106652 ** in single-threaded mode. This is important, as the
106656 PmaReader *p = &pMain->aReadr[iTask];
106657 assert( p->pIncr==0 || (
106658 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
106659 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
106673 pSorter->pMerger = pMain;
106694 assert( pCsr->eCurType==CURTYPE_SORTER );
106695 pSorter = pCsr->uc.pSorter;
106700 ** from the in-memory list. */
106701 if( pSorter->bUsePMA==0 ){
106702 if( pSorter->list.pList ){
106704 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
106711 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
106715 assert( pSorter->list.pList );
106725 assert( pSorter->pReader==0 );
106746 assert( pCsr->eCurType==CURTYPE_SORTER );
106747 pSorter = pCsr->uc.pSorter;
106748 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
106749 if( pSorter->bUsePMA ){
106750 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
106751 assert( pSorter->bUseThreads==0 || pSorter->pReader );
106752 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
106754 if( pSorter->bUseThreads ){
106755 rc = vdbePmaReaderNext(pSorter->pReader);
106756 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
106759 /*if( !pSorter->bUseThreads )*/ {
106761 assert( pSorter->pMerger!=0 );
106762 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
106763 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
106767 SorterRecord *pFree = pSorter->list.pList;
106768 pSorter->list.pList = pFree->u.pNext;
106769 pFree->u.pNext = 0;
106770 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
106771 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
106785 if( pSorter->bUsePMA ){
106788 if( pSorter->bUseThreads ){
106789 pReader = pSorter->pReader;
106792 /*if( !pSorter->bUseThreads )*/{
106793 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
106795 *pnKey = pReader->nKey;
106796 pKey = pReader->aKey;
106798 *pnKey = pSorter->list.pList->nVal;
106799 pKey = SRVAL(pSorter->list.pList);
106811 assert( pCsr->eCurType==CURTYPE_SORTER );
106812 pSorter = pCsr->uc.pSorter;
106817 pOut->n = nKey;
106819 memcpy(pOut->z, pKey, nKey);
106852 assert( pCsr->eCurType==CURTYPE_SORTER );
106853 pSorter = pCsr->uc.pSorter;
106854 r2 = pSorter->pUnpacked;
106855 pKeyInfo = pCsr->pKeyInfo;
106857 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
106859 r2->nField = nKeyCol;
106861 assert( r2->nField==nKeyCol );
106866 if( r2->aMem[i].flags & MEM_Null ){
106867 *pRes = -1;
106872 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
106879 ** 2020-03-23
106890 ** This file implements virtual-tables for examining the bytecode content
106897 /* An instance of the bytecode() table-valued function.
106901 sqlite3_vtab base; /* Base class - must be first */
106910 sqlite3_vtab_cursor base; /* Base class - must be first */
106925 ** Create a new bytecode() table-valued function.
106974 pNew->db = db;
106975 pNew->bTablesUsed = isTabUsed*2;
106998 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
106999 *ppCursor = &pCur->base;
107007 sqlite3_free(pCur->zP4);
107008 pCur->zP4 = 0;
107009 sqlite3VdbeMemRelease(&pCur->sub);
107010 sqlite3VdbeMemSetNull(&pCur->sub);
107011 if( pCur->needFinalize ){
107012 sqlite3_finalize(pCur->pStmt);
107014 pCur->pStmt = 0;
107015 pCur->needFinalize = 0;
107016 pCur->zType = 0;
107017 pCur->zSchema = 0;
107018 pCur->zName = 0;
107037 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
107039 if( pCur->zP4 ){
107040 sqlite3_free(pCur->zP4);
107041 pCur->zP4 = 0;
107043 if( pCur->zName ){
107044 pCur->zName = 0;
107045 pCur->zType = 0;
107046 pCur->zSchema = 0;
107049 (Vdbe*)pCur->pStmt,
107050 pCur->showSubprograms ? &pCur->sub : 0,
107051 pTab->bTablesUsed,
107052 &pCur->iRowid,
107053 &pCur->iAddr,
107054 &pCur->aOp);
107056 sqlite3VdbeMemSetNull(&pCur->sub);
107057 pCur->aOp = 0;
107068 return pCur->aOp==0;
107081 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
107082 Op *pOp = pCur->aOp + pCur->iAddr;
107083 if( pVTab->bTablesUsed ){
107087 if( i<=2 && pCur->zType==0 ){
107090 int iDb = pOp->p3;
107091 Pgno iRoot = (Pgno)pOp->p2;
107092 sqlite3 *db = pVTab->db;
107093 pSchema = db->aDb[iDb].pSchema;
107094 pCur->zSchema = db->aDb[iDb].zDbSName;
107095 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
107097 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
107098 pCur->zName = pTab->zName;
107099 pCur->zType = "table";
107103 if( pCur->zName==0 ){
107104 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
107106 if( pIdx->tnum==iRoot ){
107107 pCur->zName = pIdx->zName;
107108 pCur->zType = "index";
107118 sqlite3_result_int(ctx, pCur->iAddr);
107121 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
107122 -1, SQLITE_STATIC);
107125 sqlite3_result_int(ctx, pOp->p1);
107128 sqlite3_result_int(ctx, pOp->p2);
107131 sqlite3_result_int(ctx, pOp->p3);
107135 if( pCur->zP4==0 ){
107136 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
107139 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
107142 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
107143 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
107148 sqlite3_result_int(ctx, pOp->p5);
107151 Op *aOp = pCur->aOp;
107153 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
107154 if( pCur->iRowid==pCur->iAddr+1 ){
107157 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
107166 sqlite3_result_int64(ctx, pOp->nExec);
107169 sqlite3_result_int64(ctx, pOp->nCycle);
107179 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
107182 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
107185 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
107188 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
107200 *pRowid = pCur->iRowid;
107216 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
107221 pCur->iRowid = 0;
107222 pCur->iAddr = 0;
107223 pCur->showSubprograms = idxNum==0;
107230 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
107231 pCur->needFinalize = 1;
107234 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
107236 if( pCur->pStmt==0 ){
107237 pVTab->base.zErrMsg = sqlite3_mprintf(
107239 pVTab->bTablesUsed ? "tables_used" : "bytecode"
107261 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
107262 pIdxInfo->estimatedCost = (double)100;
107263 pIdxInfo->estimatedRows = 100;
107264 pIdxInfo->idxNum = 0;
107265 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
107266 if( p->usable==0 ) continue;
107267 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
107269 pIdxInfo->aConstraintUsage[i].omit = 1;
107270 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
107272 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
107273 pIdxInfo->aConstraintUsage[i].omit = 1;
107274 pIdxInfo->idxNum = 1;
107339 ** This file contains code use to implement an in-memory rollback journal.
107340 ** The in-memory rollback journal is used to journal transactions for
107343 ** Update: The in-memory journal is also used to temporarily cache
107344 ** smaller journals that are not critical for power-loss recovery.
107362 ** The zChunk array is always at least 8 bytes in size - usually much more.
107379 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
107391 ** This structure is a subclass of sqlite3_file. Each open memory-journal
107396 int nChunkSize; /* In-memory chunk-size */
107399 FileChunk *pFirst; /* Head of in-memory chunk-list */
107409 ** Read data from the in-memory journal file. This is the implementation
107424 if( (iAmt+iOfst)>p->endpoint.iOffset ){
107427 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
107428 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
107430 for(pChunk=p->pFirst;
107431 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
107432 pChunk=pChunk->pNext
107434 iOff += p->nChunkSize;
107437 pChunk = p->readpoint.pChunk;
107441 iChunkOffset = (int)(iOfst%p->nChunkSize);
107443 int iSpace = p->nChunkSize - iChunkOffset;
107444 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
107445 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
107447 nRead -= iSpace;
107449 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
107450 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
107451 p->readpoint.pChunk = pChunk;
107463 pNext = pIter->pNext;
107482 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
107484 nChunk = copy.endpoint.iOffset - iOff;
107486 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
107491 /* No error has occurred. Free the in-memory buffers. */
107497 ** the original before returning. This way, SQLite uses the in-memory
107498 ** journal data to roll back changes made to the internal page-cache
107525 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
107535 /* An in-memory journal file should only ever be appended to. Random
107537 ** the in-memory journal is being used by a connection using the
107538 ** atomic-write optimization. In this case the first 28 bytes of the
107540 assert( iOfst<=p->endpoint.iOffset );
107541 if( iOfst>0 && iOfst!=p->endpoint.iOffset ){
107544 if( iOfst==0 && p->pFirst ){
107545 assert( p->nChunkSize>iAmt );
107546 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
107549 FileChunk *pChunk = p->endpoint.pChunk;
107550 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
107551 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
107556 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
107560 pNew->pNext = 0;
107562 assert( p->pFirst );
107563 pChunk->pNext = pNew;
107565 assert( !p->pFirst );
107566 p->pFirst = pNew;
107568 pChunk = p->endpoint.pChunk = pNew;
107572 memcpy((u8*)pChunk->zChunk + iChunkOffset, zWrite, iSpace);
107574 nWrite -= iSpace;
107575 p->endpoint.iOffset += iSpace;
107584 ** Truncate the in-memory file.
107588 assert( p->endpoint.pChunk==0 || p->endpoint.pChunk->pNext==0 );
107589 if( size<p->endpoint.iOffset ){
107592 memjrnlFreeChunks(p->pFirst);
107593 p->pFirst = 0;
107595 i64 iOff = p->nChunkSize;
107596 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<size; pIter=pIter->pNext){
107597 iOff += p->nChunkSize;
107600 memjrnlFreeChunks(pIter->pNext);
107601 pIter->pNext = 0;
107605 p->endpoint.pChunk = pIter;
107606 p->endpoint.iOffset = size;
107607 p->readpoint.pChunk = 0;
107608 p->readpoint.iOffset = 0;
107618 memjrnlFreeChunks(p->pFirst);
107626 ** syncing an in-memory journal is a no-op.
107638 *pSize = (sqlite_int64) p->endpoint.iOffset;
107673 ** all content is always stored in main-memory. Finally, if nSpill is a
107674 ** positive value, then the journal file is initially created in-memory
107690 /* Zero the file-handle object. If nSpill was passed zero, initialize
107693 ** made on the journal file-handle. */
107700 p->nChunkSize = nSpill;
107702 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
107703 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
107706 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
107707 p->nSpill = nSpill;
107708 p->flags = flags;
107709 p->zJournal = zName;
107710 p->pVfs = pVfs;
107715 ** Open an in-memory journal file.
107718 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
107725 ** in-memory-only journal file (i.e. is one that was opened with a +ve
107732 if( pJfd->pMethods==&MemJournalMethods && (
107734 p->nSpill>0
107739 NEVER(p->nSpill>0)
107742 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
107752 ** The file-handle passed as the only argument is open on a journal file.
107757 return p->pMethods==&MemJournalMethods;
107762 ** pVfs to create the underlying on-disk files.
107765 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
107796 for(pWin=pList; pWin; pWin=pWin->pNextWin){
107798 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
107800 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
107802 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
107804 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
107806 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
107828 ** return from the top-level walk call.
107838 rc = pWalker->xExprCallback(pWalker, pExpr);
107841 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
107842 if( pExpr->pLeft && sqlite3WalkExprNN(pWalker, pExpr->pLeft) ){
107845 if( pExpr->pRight ){
107847 pExpr = pExpr->pRight;
107851 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
107853 if( pExpr->x.pList ){
107854 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
107858 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
107879 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
107880 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
107887 ** This is a no-op callback for Walker->xSelectCallback2. If this
107888 ** callback is set, then the Select->pWinDefn list is traversed.
107893 /* No-op */
107903 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
107904 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
107905 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
107906 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
107907 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
107908 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
107910 if( p->pWinDefn ){
107912 if( pWalker->xSelectCallback2==sqlite3WalkWinDefnDummyCallback
107913 || ((pParse = pWalker->pParse)!=0 && IN_RENAME_OBJECT)
107915 || pWalker->xSelectCallback2==sqlite3SelectPopWith
107920 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
107940 pSrc = p->pSrc;
107942 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
107943 if( pItem->fg.isSubquery
107944 && sqlite3WalkSelect(pWalker, pItem->u4.pSubq->pSelect)
107948 if( pItem->fg.isTabFunc
107949 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
107961 ** on the compound select chain, p->pPrior.
107966 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
107973 ** is a no-op returning WRC_Continue.
107978 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
107980 rc = pWalker->xSelectCallback(pWalker, p);
107987 if( pWalker->xSelectCallback2 ){
107988 pWalker->xSelectCallback2(pWalker, p);
107990 p = p->pPrior;
108000 pWalker->walkerDepth++;
108005 pWalker->walkerDepth--;
108010 ** No-op routine for the parse-tree walker.
108024 ** No-op routine for the parse-tree walker for SELECT statements.
108064 ** is a helper function - a callback for the tree walker.
108069 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
108083 ** Turn the pExpr expression into an alias for the iCol-th column of the
108104 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
108108 Expr *pOrig; /* The iCol-th column of the result set */
108112 assert( iCol>=0 && iCol<pEList->nExpr );
108113 pOrig = pEList->a[iCol].pExpr;
108116 if( pExpr->pAggInfo ) return;
108117 db = pParse->db;
108119 if( db->mallocFailed ){
108125 if( pExpr->op==TK_COLLATE ){
108127 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
108133 if( ALWAYS(pExpr->y.pWin!=0) ){
108134 pExpr->y.pWin->pOwner = pExpr;
108144 ** and mark the expression-list item by setting ExprList.a[].fg.eEName
108147 ** Check to see if the zSpan/eEName of the expression-list item passed to this
108154 ** and setting zSpan to "DATABASE.TABLE.<rowid-alias>". This type of pItem
108167 int eEName = pItem->fg.eEName;
108172 zSpan = pItem->zEName;
108192 ** Return TRUE if the double-quoted string mis-feature should be supported.
108195 if( db->init.busy ) return 1; /* Always support for legacy schemas */
108196 if( pTopNC->ncFlags & NC_IsDDL ){
108198 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
108201 return (db->flags & SQLITE_DqsDDL)!=0;
108204 return (db->flags & SQLITE_DqsDML)!=0;
108209 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
108216 n = pExpr->iColumn;
108218 pExTab = pExpr->y.pTab;
108220 assert( n < pExTab->nCol );
108221 if( (pExTab->tabFlags & TF_HasGenerated)!=0
108222 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
108224 testcase( pExTab->nCol==BMS-1 );
108225 testcase( pExTab->nCol==BMS );
108226 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
108228 testcase( n==BMS-1 );
108230 if( n>=BMS ) n = BMS-1;
108247 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
108249 pNew->iTable = pMatch->iCursor;
108250 pNew->iColumn = iColumn;
108251 pNew->y.pTab = pMatch->pSTab;
108252 assert( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 );
108259 ** Return TRUE (non-zero) if zTab is a valid name for the schema table pTab.
108264 const char *zDb /* non-NULL if a database qualifier is present */
108268 assert( pTab->tnum==1 );
108270 zLegacy = pTab->zName;
108290 ** pExpr->iDb Set the index in db->aDb[] of the database X
108292 ** pExpr->iTable Set to the cursor number for the table obtained
108294 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
108296 ** pExpr->iColumn Set to the column number within the table.
108297 ** pExpr->op Set to TK_COLUMN.
108298 ** pExpr->pLeft Any expression this points to is deleted
108299 ** pExpr->pRight Any expression this points to is deleted.
108323 sqlite3 *db = pParse->db; /* The database connection */
108328 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
108331 const char *zCol = pRight->u.zToken;
108338 /* Initialize the node to no-match */
108339 pExpr->iTable = -1;
108347 testcase( pNC->ncFlags & NC_PartIdx );
108348 testcase( pNC->ncFlags & NC_IsCheck );
108349 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
108356 for(i=0; i<db->nDb; i++){
108357 assert( db->aDb[i].zDbSName );
108358 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
108359 pSchema = db->aDb[i].pSchema;
108363 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
108366 pSchema = db->aDb[0].pSchema;
108367 zDb = db->aDb[0].zDbSName;
108372 /* Start at the inner-most context and move outward until a match is found */
108376 SrcList *pSrcList = pNC->pSrcList;
108379 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
108380 pTab = pItem->pSTab;
108381 assert( pTab!=0 && pTab->zName!=0 );
108382 assert( pTab->nCol>0 || pParse->nErr );
108383 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem));
108384 if( pItem->fg.isNestedFrom ){
108389 ** This pItem -------------^
108393 assert( pItem->fg.isSubquery );
108394 assert( pItem->u4.pSubq!=0 );
108395 pSel = pItem->u4.pSubq->pSelect;
108397 pEList = pSel->pEList;
108399 assert( pEList->nExpr==pTab->nCol );
108400 for(j=0; j<pEList->nExpr; j++){
108402 if( !sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb, &bRowid) ){
108407 if( pItem->fg.isUsing==0
108408 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
108416 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
108417 /* An INNER or LEFT JOIN. Use the left-most table */
108420 if( (pItem->fg.jointype & JT_LEFT)==0 ){
108421 /* A RIGHT JOIN. Use the right-most table */
108427 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108439 pExpr->iColumn = j;
108440 pEList->a[j].fg.bUsed = 1;
108442 /* rowid cannot be part of a USING clause - assert() this. */
108443 assert( bRowid==0 || pEList->a[j].fg.bUsingTerm==0 );
108444 if( pEList->a[j].fg.bUsingTerm ) break;
108451 if( pTab->pSchema!=pSchema ) continue;
108454 if( pItem->zAlias!=0 ){
108455 if( sqlite3StrICmp(zTab, pItem->zAlias)!=0 ){
108458 }else if( sqlite3StrICmp(zTab, pTab->zName)!=0 ){
108459 if( pTab->tnum!=1 ) continue;
108463 if( IN_RENAME_OBJECT && pItem->zAlias ){
108464 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
108470 if( pItem->fg.isUsing==0
108471 || sqlite3IdListIndex(pItem->u3.pUsing, zCol)<0
108479 if( (pItem->fg.jointype & JT_RIGHT)==0 ){
108480 /* An INNER or LEFT JOIN. Use the left-most table */
108483 if( (pItem->fg.jointype & JT_LEFT)==0 ){
108484 /* A RIGHT JOIN. Use the right-most table */
108490 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108495 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
108496 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
108497 if( pItem->fg.isNestedFrom ){
108510 ** non-VIEW candidate plus multiple VIEW candidates. In other
108511 ** words non-VIEW candidate terms take precedence over VIEWs.
108516 && ALWAYS(pMatch->pSTab!=0)
108517 && (pMatch->pSTab->tabFlags & TF_Ephemeral)!=0
108518 && (pTab->tabFlags & TF_Ephemeral)==0)
108526 /* The (much more common) non-SQLITE_ALLOW_ROWID_IN_VIEW case is
108528 ** always be a non-VIEW
108536 pExpr->iTable = pMatch->iCursor;
108538 pExpr->y.pTab = pMatch->pSTab;
108539 if( (pMatch->fg.jointype & (JT_LEFT|JT_LTORJ))!=0 ){
108542 pSchema = pExpr->y.pTab->pSchema;
108555 if( pParse->pTriggerTab!=0 ){
108556 int op = pParse->eTriggerOp;
108558 if( pParse->bReturning ){
108559 if( (pNC->ncFlags & NC_UBaseReg)!=0
108561 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0
108562 || isValidSchemaTableName(zTab, pParse->pTriggerTab, 0))
108564 pExpr->iTable = op!=TK_DELETE;
108565 pTab = pParse->pTriggerTab;
108568 pExpr->iTable = 1;
108569 pTab = pParse->pTriggerTab;
108571 pExpr->iTable = 0;
108572 pTab = pParse->pTriggerTab;
108577 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
108578 Upsert *pUpsert = pNC->uNC.pUpsert;
108580 pTab = pUpsert->pUpsertSrc->a[0].pSTab;
108581 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
108588 pSchema = pTab->pSchema;
108592 if( pTab->iPKey==iCol ) iCol = -1;
108595 iCol = -1;
108597 iCol = pTab->nCol;
108600 if( iCol<pTab->nCol ){
108604 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
108605 testcase( iCol==(-1) );
108608 pExpr->iColumn = iCol;
108609 pExpr->y.pTab = pTab;
108612 pExpr->iTable = pNC->uNC.pUpsert->regData +
108620 pExpr->y.pTab = pTab;
108621 if( pParse->bReturning ){
108623 pExpr->op2 = TK_COLUMN;
108624 pExpr->iColumn = iCol;
108625 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
108628 pExpr->iColumn = (i16)iCol;
108632 pExpr->affExpr = SQLITE_AFF_INTEGER;
108633 }else if( pExpr->iTable==0 ){
108636 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108640 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
108656 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
108658 && ALWAYS(VisibleRowid(pMatch->pSTab) || pMatch->fg.isNestedFrom)
108662 if( pMatch->pSTab!=0 && IsView(pMatch->pSTab) ){
108666 if( pMatch->fg.isNestedFrom==0 ) pExpr->iColumn = -1;
108667 pExpr->affExpr = SQLITE_AFF_INTEGER;
108672 ** might refer to an result-set alias. This happens, for example, when
108682 ** The ability to use an output result-set column in the WHERE, GROUP BY,
108689 && (pNC->ncFlags & NC_UEList)!=0
108692 pEList = pNC->uNC.pEList;
108694 for(j=0; j<pEList->nExpr; j++){
108695 char *zAs = pEList->a[j].zEName;
108696 if( pEList->a[j].fg.eEName==ENAME_NAME
108700 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
108701 assert( ExprUseXList(pExpr)==0 || pExpr->x.pList==0 );
108702 assert( ExprUseXSelect(pExpr)==0 || pExpr->x.pSelect==0 );
108703 pOrig = pEList->a[j].pExpr;
108704 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
108709 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
108734 pNC = pNC->pNext;
108741 ** supplied) and the value of Z is enclosed in double-quotes, then
108746 ** Because no reference was made to outer contexts, the pNC->nRef
108750 assert( pExpr->op==TK_ID );
108754 /* If a double-quoted identifier does not match any known column name,
108758 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
108769 "double-quoted string literal: \"%w\"", zCol);
108771 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
108773 pExpr->op = TK_STRING;
108774 memset(&pExpr->y, 0, sizeof(pExpr->y));
108794 if( pFJMatch->nExpr==cnt-1 ){
108798 sqlite3ExprDelete(db, pExpr->pLeft);
108799 pExpr->pLeft = 0;
108800 sqlite3ExprDelete(db, pExpr->pRight);
108801 pExpr->pRight = 0;
108803 extendFJMatch(pParse, &pFJMatch, pMatch, pExpr->iColumn);
108804 pExpr->op = TK_FUNCTION;
108805 pExpr->u.zToken = "coalesce";
108806 pExpr->x.pList = pFJMatch;
108820 sqlite3ErrorMsg(pParse, "%s: \"%s\" - should this be a"
108821 " string literal in single-quotes?",
108826 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
108827 pParse->checkSchema = 1;
108828 pTopNC->nNcErr++;
108835 sqlite3ExprDelete(db, pExpr->pLeft);
108836 pExpr->pLeft = 0;
108837 sqlite3ExprDelete(db, pExpr->pRight);
108838 pExpr->pRight = 0;
108857 if( pExpr->iColumn>=0 ){
108858 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
108860 pMatch->fg.rowidUsed = 1;
108864 pExpr->op = eNewExprOp;
108869 if( pParse->db->xAuth
108870 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
108872 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
108879 pTopNC->nRef++;
108881 pTopNC = pTopNC->pNext;
108896 SrcItem *pItem = &pSrc->a[iSrc];
108899 pTab = p->y.pTab = pItem->pSTab;
108900 p->iTable = pItem->iCursor;
108901 if( p->y.pTab->iPKey==iCol ){
108902 p->iColumn = -1;
108904 p->iColumn = (ynVar)iCol;
108905 if( (pTab->tabFlags & TF_HasGenerated)!=0
108906 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
108908 testcase( pTab->nCol==63 );
108909 testcase( pTab->nCol==64 );
108910 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
108913 testcase( iCol==BMS-1 );
108914 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
108923 ** pNC->ncFlags values determined by validMask.
108945 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
108947 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
108950 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
108953 if( pExpr ) pExpr->op = TK_NULL;
108954 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
108958 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E,R);
108962 ** Return 1024 times this value. Or return -1 if p is not a floating point
108966 double r = -1.0;
108967 if( p->op!=TK_FLOAT ) return -1;
108969 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
108971 if( r>1.0 ) return -1;
108990 pNC = pWalker->u.pNC;
108992 pParse = pNC->pParse;
108993 assert( pParse==pWalker->pParse );
108996 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
108997 SrcList *pSrcList = pNC->pSrcList;
108999 for(i=0; i<pNC->pSrcList->nSrc; i++){
109000 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
109004 switch( pExpr->op ){
109012 SrcList *pSrcList = pNC->pSrcList;
109014 assert( pSrcList && pSrcList->nSrc>=1 );
109015 pItem = pSrcList->a;
109016 pExpr->op = TK_COLUMN;
109018 pExpr->y.pTab = pItem->pSTab;
109019 pExpr->iTable = pItem->iCursor;
109020 pExpr->iColumn--;
109021 pExpr->affExpr = SQLITE_AFF_INTEGER;
109027 ** "expr IS NOT NULL" --> "TRUE"
109028 ** "expr IS NULL" --> "FALSE"
109033 ** If this optimization occurs, also restore the NameContext ref-counts
109039 ** 2024-03-28: Beware of aggregates. A bare column of aggregated table
109057 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
109058 anRef[i] = p->nRef;
109060 sqlite3WalkExpr(pWalker, pExpr->pLeft);
109062 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
109067 for(i=0, p=pNC; p; p=p->pNext, i++){
109068 if( (p->ncFlags & NC_Where)==0 ){
109078 pExpr->op==TK_NOTNULL
109083 pExpr->u.iValue = (pExpr->op==TK_NOTNULL);
109084 pExpr->flags |= EP_IntValue;
109085 pExpr->op = TK_INTEGER;
109086 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
109087 p->nRef = anRef[i];
109089 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
109090 pExpr->pLeft = 0;
109099 ** be one call to lookupName(). Then the compiler will in-line
109108 if( pExpr->op==TK_ID ){
109114 Expr *pLeft = pExpr->pLeft;
109115 testcase( pNC->ncFlags & NC_IdxExpr );
109116 testcase( pNC->ncFlags & NC_GenCol );
109119 pRight = pExpr->pRight;
109120 if( pRight->op==TK_ID ){
109123 assert( pRight->op==TK_DOT );
109125 zDb = pLeft->u.zToken;
109126 pLeft = pRight->pLeft;
109127 pRight = pRight->pRight;
109130 zTable = pLeft->u.zToken;
109134 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
109150 u8 enc = ENC(pParse->db); /* The database encoding */
109151 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
109153 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
109156 assert( pExpr->pLeft==0 || pExpr->pLeft->op==TK_ORDER );
109157 pList = pExpr->x.pList;
109158 n = pList ? pList->nExpr : 0;
109159 zId = pExpr->u.zToken;
109160 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
109162 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
109169 is_agg = pDef->xFinalize!=0;
109170 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
109173 pExpr->iTable = exprProbability(pList->a[1].pExpr);
109174 if( pExpr->iTable<0 ){
109178 pNC->nNcErr++;
109181 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
109183 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
109184 ** short-hand for likelihood(X,0.0625).
109185 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
109187 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
109190 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
109195 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
109200 pNC->nNcErr++;
109202 pExpr->op = TK_NULL;
109216 if( (pDef->funcFlags & SQLITE_SUBTYPE)
109221 ExprSetProperty(pList->a[ii].pExpr, EP_SubtArg);
109225 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
109232 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
109233 /* Clearly non-deterministic functions like random(), but also
109239 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
109243 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
109245 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
109246 && pParse->nested==0
109247 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
109249 /* Internal-use-only functions are disallowed unless the
109251 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
109256 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
109259 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
109266 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
109267 || (pDef->xValue==0 && pDef->xInverse==0)
109268 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
109270 if( pDef && pDef->xValue==0 && pWin ){
109274 pNC->nNcErr++;
109276 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
109277 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
109278 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
109281 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
109287 pNC->nNcErr++;
109291 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
109293 pNC->nNcErr++;
109297 else if( no_such_func && pParse->db->init.busy==0
109299 && pParse->explain==0
109303 pNC->nNcErr++;
109307 pNC->nNcErr++;
109312 "FILTER may not be used with non-aggregate %#T()",
109315 pNC->nNcErr++;
109318 else if( is_agg==0 && pExpr->pLeft ){
109320 pNC->nNcErr++;
109327 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
109329 pNC->ncFlags &= ~NC_AllowAgg;
109333 else if( ExprHasProperty(pExpr, EP_WinFunc) || pExpr->pLeft ){
109338 if( pExpr->pLeft ){
109339 assert( pExpr->pLeft->op==TK_ORDER );
109340 assert( ExprUseXList(pExpr->pLeft) );
109341 sqlite3WalkExprList(pWalker, pExpr->pLeft->x.pList);
109344 if( pWin && pParse->nErr==0 ){
109345 Select *pSel = pNC->pWinSelect;
109346 assert( ExprUseYWin(pExpr) && pWin==pExpr->y.pWin );
109348 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
109349 if( pParse->db->mallocFailed ) break;
109351 sqlite3WalkExprList(pWalker, pWin->pPartition);
109352 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
109353 sqlite3WalkExpr(pWalker, pWin->pFilter);
109355 pNC->ncFlags |= NC_HasWin;
109360 pExpr->op = TK_AGG_FUNCTION;
109361 pExpr->op2 = 0;
109364 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
109369 && sqlite3ReferencesSrcList(pParse, pExpr, pNC2->pSrcList)==0
109371 pExpr->op2 += (1 + pNC2->nNestedSelect);
109372 pNC2 = pNC2->pNext;
109376 pExpr->op2 += pNC2->nNestedSelect;
109379 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
109380 testcase( (pDef->funcFlags & SQLITE_FUNC_ANYORDER)!=0 );
109381 pNC2->ncFlags |= NC_HasAgg
109382 | ((pDef->funcFlags^SQLITE_FUNC_ANYORDER)
109386 pNC->ncFlags |= savedAllowFlags;
109388 /* FIX ME: Compute pExpr->affinity based on the expected return
109395 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
109398 testcase( pExpr->op==TK_IN );
109400 int nRef = pNC->nRef;
109401 testcase( pNC->ncFlags & NC_IsCheck );
109402 testcase( pNC->ncFlags & NC_PartIdx );
109403 testcase( pNC->ncFlags & NC_IdxExpr );
109404 testcase( pNC->ncFlags & NC_GenCol );
109405 assert( pExpr->x.pSelect );
109406 if( pNC->ncFlags & NC_SelfRef ){
109409 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
109411 assert( pNC->nRef>=nRef );
109412 if( nRef!=pNC->nRef ){
109414 pExpr->x.pSelect->selFlags |= SF_Correlated;
109416 pNC->ncFlags |= NC_Subquery;
109421 testcase( pNC->ncFlags & NC_IsCheck );
109422 testcase( pNC->ncFlags & NC_PartIdx );
109423 testcase( pNC->ncFlags & NC_IdxExpr );
109424 testcase( pNC->ncFlags & NC_GenCol );
109431 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
109435 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
109438 if( pRight->op==TK_TRUEFALSE ){
109439 pExpr->op2 = pExpr->op;
109440 pExpr->op = TK_TRUTH;
109454 if( pParse->db->mallocFailed ) break;
109455 assert( pExpr->pLeft!=0 );
109456 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
109457 if( pExpr->op==TK_BETWEEN ){
109459 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
109461 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
109464 assert( pExpr->pRight!=0 );
109465 nRight = sqlite3ExprVectorSize(pExpr->pRight);
109468 testcase( pExpr->op==TK_EQ );
109469 testcase( pExpr->op==TK_NE );
109470 testcase( pExpr->op==TK_LT );
109471 testcase( pExpr->op==TK_LE );
109472 testcase( pExpr->op==TK_GT );
109473 testcase( pExpr->op==TK_GE );
109474 testcase( pExpr->op==TK_IS );
109475 testcase( pExpr->op==TK_ISNOT );
109476 testcase( pExpr->op==TK_BETWEEN );
109478 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
109483 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
109484 return pParse->nErr ? WRC_Abort : WRC_Continue;
109491 ** to the AS-name of one of the terms of the expression list. If it is,
109508 if( pE->op==TK_ID ){
109511 zCol = pE->u.zToken;
109512 for(i=0; i<pEList->nExpr; i++){
109513 if( pEList->a[i].fg.eEName==ENAME_NAME
109514 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
109532 ** Attempt to match pE against result set columns in the left-most
109534 ** as an indication to the caller that it should sort by the i-th column.
109535 ** The left-most column is 1. In other words, the value returned is the
109539 ** If there is no match, return 0. Return -1 if an error occurs.
109551 u8 savedSuppErr; /* Saved value of db->suppressErr */
109554 pEList = pSelect->pEList;
109560 nc.pSrcList = pSelect->pSrc;
109564 db = pParse->db;
109565 savedSuppErr = db->suppressErr;
109566 db->suppressErr = 1;
109568 db->suppressErr = savedSuppErr;
109572 ** in the result set. Return an 1-based index of the matching
109573 ** result-set entry.
109575 for(i=0; i<pEList->nExpr; i++){
109576 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
109586 ** Generate an ORDER BY or GROUP BY term out-of-range error.
109591 int i, /* The index (1-based) of the term out of range */
109596 "%r %s BY term out of range - should be "
109598 sqlite3RecordErrorOffsetOfExpr(pParse->db, pError);
109610 ** beginning with the left-most SELECT and working toward the right.
109626 pOrderBy = pSelect->pOrderBy;
109628 db = pParse->db;
109629 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
109633 for(i=0; i<pOrderBy->nExpr; i++){
109634 pOrderBy->a[i].fg.done = 0;
109636 pSelect->pNext = 0;
109637 while( pSelect->pPrior ){
109638 pSelect->pPrior->pNext = pSelect;
109639 pSelect = pSelect->pPrior;
109644 pEList = pSelect->pEList;
109646 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109647 int iCol = -1;
109649 if( pItem->fg.done ) continue;
109650 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
109653 if( iCol<=0 || iCol>pEList->nExpr ){
109654 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr, pE);
109672 if( !db->mallocFailed ){
109688 pNew->flags |= EP_IntValue;
109689 pNew->u.iValue = iCol;
109690 if( pItem->pExpr==pE ){
109691 pItem->pExpr = pNew;
109693 Expr *pParent = pItem->pExpr;
109694 assert( pParent->op==TK_COLLATE );
109695 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
109696 assert( pParent->pLeft==pE );
109697 pParent->pLeft = pNew;
109700 pItem->u.x.iOrderByCol = (u16)iCol;
109702 pItem->fg.done = 1;
109707 pSelect = pSelect->pNext;
109709 for(i=0; i<pOrderBy->nExpr; i++){
109710 if( pOrderBy->a[i].fg.done==0 ){
109727 ** return non-zero. Return zero if no errors are seen.
109736 sqlite3 *db = pParse->db;
109740 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
109741 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
109745 pEList = pSelect->pEList;
109747 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109748 if( pItem->u.x.iOrderByCol ){
109749 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
109750 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr, 0);
109753 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
109766 Window *pWin = pExpr->y.pWin;
109777 if( pSelect->pWin ){
109795 ** If the order-by term is an integer I between 1 and N (where N is the
109797 ** in the resolution is a copy of the I-th result-set expression. If
109798 ** the order-by term is an identifier that corresponds to the AS-name of
109799 ** a result-set expression, then the term resolves to a copy of the
109800 ** result-set expression. Otherwise, the expression is resolved in
109801 ** the usual way - using sqlite3ResolveExprNames().
109820 nResult = pSelect->pEList->nExpr;
109821 pParse = pNC->pParse;
109822 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
109823 Expr *pE = pItem->pExpr;
109827 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
109829 /* If an AS-name match is found, mark this ORDER BY column as being
109830 ** a copy of the iCol-th result-set column. The subsequent call to
109832 ** copy of the iCol-th result-set expression. */
109833 pItem->u.x.iOrderByCol = (u16)iCol;
109840 ** order-by term to a copy of the result-set expression */
109845 pItem->u.x.iOrderByCol = (u16)iCol;
109850 pItem->u.x.iOrderByCol = 0;
109854 for(j=0; j<pSelect->pEList->nExpr; j++){
109855 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
109860 pItem->u.x.iOrderByCol = j+1;
109878 Select *pLeftmost; /* Left-most of SELECT of a compound */
109883 if( p->selFlags & SF_Resolved ){
109886 pOuterNC = pWalker->u.pNC;
109887 pParse = pWalker->pParse;
109888 db = pParse->db;
109898 if( (p->selFlags & SF_Expanded)==0 ){
109900 return pParse->nErr ? WRC_Abort : WRC_Prune;
109903 isCompound = p->pPrior!=0;
109907 assert( (p->selFlags & SF_Expanded)!=0 );
109908 assert( (p->selFlags & SF_Resolved)==0 );
109909 p->selFlags |= SF_Resolved;
109917 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
109923 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
109924 ** as if it were part of the sub-query, not the parent. This block
109925 ** moves the pOrderBy down to the sub-query. It will be moved back
109927 if( p->selFlags & SF_Converted ){
109929 assert( p->pSrc->a[0].fg.isSubquery );
109930 assert( p->pSrc->a[0].u4.pSubq!=0 );
109931 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
109933 assert( p->pSrc->nSrc==1 && p->pOrderBy );
109934 assert( pSub->pPrior && pSub->pOrderBy==0 );
109935 pSub->pOrderBy = p->pOrderBy;
109936 p->pOrderBy = 0;
109941 if( pOuterNC ) pOuterNC->nNestedSelect++;
109942 for(i=0; i<p->pSrc->nSrc; i++){
109943 SrcItem *pItem = &p->pSrc->a[i];
109944 assert( pItem->zName!=0
109945 || pItem->fg.isSubquery ); /* Test of tag-20240424-1*/
109946 if( pItem->fg.isSubquery
109947 && (pItem->u4.pSubq->pSelect->selFlags & SF_Resolved)==0
109949 int nRef = pOuterNC ? pOuterNC->nRef : 0;
109950 const char *zSavedContext = pParse->zAuthContext;
109952 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
109953 sqlite3ResolveSelectNames(pParse, pItem->u4.pSubq->pSelect, pOuterNC);
109954 pParse->zAuthContext = zSavedContext;
109955 if( pParse->nErr ) return WRC_Abort;
109956 assert( db->mallocFailed==0 );
109959 ** expressions in the sub-select were resolved, the sub-select
109965 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
109966 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
109970 if( pOuterNC && ALWAYS(pOuterNC->nNestedSelect>0) ){
109971 pOuterNC->nNestedSelect--;
109974 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
109975 ** resolve the result-set expression list.
109978 sNC.pSrcList = p->pSrc;
109982 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
109985 /* If there are no aggregate functions in the result-set, and no GROUP BY
109988 assert( (p->selFlags & SF_Aggregate)==0 );
109989 pGroupBy = p->pGroupBy;
109993 p->selFlags |= SF_Aggregate | (sNC.ncFlags&(NC_MinMaxAgg|NC_OrderAgg));
109998 /* Add the output column list to the name-context before parsing the
110004 ** re-evaluated for each reference to it.
110007 sNC.uNC.pEList = p->pEList;
110009 if( p->pHaving ){
110010 if( (p->selFlags & SF_Aggregate)==0 ){
110011 sqlite3ErrorMsg(pParse, "HAVING clause on a non-aggregate query");
110014 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
110017 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
110020 /* Resolve names in table-valued-function arguments */
110021 for(i=0; i<p->pSrc->nSrc; i++){
110022 SrcItem *pItem = &p->pSrc->a[i];
110023 if( pItem->fg.isTabFunc
110024 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
110033 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
110034 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
110035 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
110050 ** the sub-query back to the parent query. At this point each term
110054 if( p->selFlags & SF_Converted ){
110056 assert( p->pSrc->a[0].fg.isSubquery );
110057 pSub = p->pSrc->a[0].u4.pSubq->pSelect;
110059 p->pOrderBy = pSub->pOrderBy;
110060 pSub->pOrderBy = 0;
110065 ** below, after all of the result-sets for all of the elements of
110068 ** If there is an ORDER BY clause on a term of a compound-select other
110069 ** than the right-most term, then that is a syntax error. But the error
110073 if( p->pOrderBy!=0
110074 && isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
110075 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
110079 if( db->mallocFailed ){
110090 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
110093 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
110094 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
110104 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
110105 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
110111 p = p->pPrior;
110127 ** table columns and result-set columns. At the same time, do error
110135 ** the symbolic name assigned to an ATTACH-ed database.
110146 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
110150 ** To resolve result-set references, look for expression nodes of the
110151 ** form Z (with no X and Y prefix) where the Z matches the right-hand
110152 ** size of an AS clause in the result-set of a SELECT. The Z expression
110153 ** is replaced by a copy of the left-hand side of the result-set expression.
110154 ** Table-name and function resolution occurs on the substituted expression
110181 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
110182 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
110183 w.pParse = pNC->pParse;
110185 w.xSelectCallback = (pNC->ncFlags & NC_NoSelect) ? 0 : resolveSelectStep;
110189 w.pParse->nHeight += pExpr->nHeight;
110190 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
110197 w.pParse->nHeight -= pExpr->nHeight;
110201 testcase( pNC->ncFlags & NC_HasAgg );
110202 testcase( pNC->ncFlags & NC_HasWin );
110203 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
110204 pNC->ncFlags |= savedHasAgg;
110205 return pNC->nNcErr>0 || w.pParse->nErr>0;
110224 w.pParse = pNC->pParse;
110229 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
110230 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
110231 for(i=0; i<pList->nExpr; i++){
110232 Expr *pExpr = pList->a[i].pExpr;
110235 w.pParse->nHeight += pExpr->nHeight;
110236 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
110242 w.pParse->nHeight -= pExpr->nHeight;
110246 testcase( pNC->ncFlags & NC_HasAgg );
110247 testcase( pNC->ncFlags & NC_HasWin );
110248 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg) ){
110249 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
110250 savedHasAgg |= pNC->ncFlags &
110252 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin|NC_OrderAgg);
110254 if( w.pParse->nErr>0 ) return SQLITE_ERROR;
110256 pNC->ncFlags |= savedHasAgg;
110262 ** descendants of the SELECT, including compounds off of p->pPrior,
110293 ** ------------
110301 ** nodes of the expression is set to -1 and the Expr.iColumn value is
110313 SrcList *pSrc; /* Fake SrcList for pParse->pNewTable */
110314 NameContext sNC; /* Name context for pParse->pNewTable */
110325 pSrc->nSrc = 1;
110326 pSrc->a[0].zName = pTab->zName;
110327 pSrc->a[0].pSTab = pTab;
110328 pSrc->a[0].iCursor = -1;
110329 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
110330 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
110369 if( iCol<0 || NEVER(iCol>=pTab->nCol) ) return SQLITE_AFF_INTEGER;
110370 return pTab->aCol[iCol].affinity;
110377 ** or a sub-select with a column as the return value, then the
110391 op = pExpr->op;
110392 while( 1 /* exit-by-break */ ){
110393 if( op==TK_COLUMN || (op==TK_AGG_COLUMN && pExpr->y.pTab!=0) ){
110395 assert( pExpr->y.pTab!=0 );
110396 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
110400 assert( pExpr->x.pSelect!=0 );
110401 assert( pExpr->x.pSelect->pEList!=0 );
110402 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
110403 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
110408 return sqlite3AffinityType(pExpr->u.zToken, 0);
110412 assert( pExpr->pLeft!=0 && ExprUseXSelect(pExpr->pLeft) );
110413 assert( pExpr->iColumn < pExpr->iTable );
110414 assert( pExpr->iColumn >= 0 );
110415 assert( pExpr->iTable==pExpr->pLeft->x.pSelect->pEList->nExpr );
110417 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
110421 || (op==TK_FUNCTION && pExpr->affExpr==SQLITE_AFF_DEFER)
110424 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
110427 assert( pExpr->op==TK_COLLATE
110428 || pExpr->op==TK_IF_NULL_ROW
110429 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
110430 pExpr = pExpr->pLeft;
110431 op = pExpr->op;
110435 op = pExpr->op2;
110438 return pExpr->affExpr;
110453 switch( pExpr->op ){
110457 pExpr = pExpr->pLeft;
110492 ExprList *pList = pExpr->x.pList;
110494 assert( pList->nExpr > 0);
110495 for(ii=1; ii<pList->nExpr; ii+=2){
110496 res |= sqlite3ExprDataType(pList->a[ii].pExpr);
110498 if( pList->nExpr % 2 ){
110499 res |= sqlite3ExprDataType(pList->a[pList->nExpr-1].pExpr);
110516 ** If a memory allocation error occurs, that fact is recorded in pParse->db
110525 if( pCollName->n>0 ){
110526 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
110528 pNew->pLeft = pExpr;
110529 pNew->flags |= EP_Collate|EP_Skip;
110551 assert( pExpr->op==TK_COLLATE );
110552 pExpr = pExpr->pLeft;
110566 assert( pExpr->x.pList->nExpr>0 );
110567 assert( pExpr->op==TK_FUNCTION );
110568 pExpr = pExpr->x.pList->a[0].pExpr;
110569 }else if( pExpr->op==TK_COLLATE ){
110570 pExpr = pExpr->pLeft;
110593 sqlite3 *db = pParse->db;
110597 int op = p->op;
110598 if( op==TK_REGISTER ) op = p->op2;
110599 if( (op==TK_AGG_COLUMN && p->y.pTab!=0)
110604 assert( p->y.pTab!=0 );
110605 if( (j = p->iColumn)>=0 ){
110606 const char *zColl = sqlite3ColumnColl(&p->y.pTab->aCol[j]);
110612 p = p->pLeft;
110616 || (op==TK_FUNCTION && p->affExpr==SQLITE_AFF_DEFER)
110619 p = p->x.pList->a[0].pExpr;
110624 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
110627 if( p->flags & EP_Collate ){
110628 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
110629 p = p->pLeft;
110631 Expr *pNext = p->pRight;
110633 assert( !ExprUseXList(p) || p->x.pList==0 || p->pRight==0 );
110634 if( ExprUseXList(p) && p->x.pList!=0 && !db->mallocFailed ){
110636 for(i=0; i<p->x.pList->nExpr; i++){
110637 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
110638 pNext = p->x.pList->a[i].pExpr;
110667 if( p==0 ) p = pParse->db->pDfltColl;
110678 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
110710 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
110711 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
110712 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
110713 assert( pExpr->pLeft );
110714 aff = sqlite3ExprAffinity(pExpr->pLeft);
110715 if( pExpr->pRight ){
110716 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
110718 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
110775 if( pLeft->flags & EP_Collate ){
110777 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
110798 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
110800 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
110821 if( pParse->nErr ) return 0;
110828 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
110830 sqlite3VdbeChangeP5(pParse->pVdbe, (u16)p5);
110850 ** is a sub-select, return the number of columns in the sub-select. For
110854 u8 op = pExpr->op;
110855 if( op==TK_REGISTER ) op = pExpr->op2;
110858 return pExpr->x.pList->nExpr;
110861 return pExpr->x.pSelect->pEList->nExpr;
110868 ** Return a pointer to a subexpression of pVector that is the i-th
110878 ** just the expression for the i-th term of the result set, and may
110883 assert( i<sqlite3ExprVectorSize(pVector) || pVector->op==TK_ERROR );
110885 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
110886 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
110888 return pVector->x.pSelect->pEList->a[i].pExpr;
110891 return pVector->x.pList->a[i].pExpr;
110900 ** the iField-th column of the vector expression pVector.
110920 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
110925 if( pVector->op==TK_SELECT ){
110933 ** pLeft->iTable: First in an array of register holding result, or 0
110946 pRet->iTable = nField;
110947 pRet->iColumn = iField;
110948 pRet->pLeft = pVector;
110951 if( pVector->op==TK_VECTOR ){
110954 ppVector = &pVector->x.pList->a[iField].pExpr;
110962 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
110970 ** sub-select returns more than one column, the first in an array
110978 if( pExpr->op==TK_SELECT ){
110986 ** Argument pVector points to a vector expression - either a TK_VECTOR
110994 ** containing the results of the sub-select.
111011 u8 op = pVector->op;
111015 return pVector->iTable+iField;
111019 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
111024 *ppExpr = pVector->x.pList->a[iField].pExpr;
111037 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
111038 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
111039 ** otherwise: op==pExpr->op and p5==0
111048 Vdbe *v = pParse->pVdbe;
111049 Expr *pLeft = pExpr->pLeft;
111050 Expr *pRight = pExpr->pRight;
111061 if( pParse->nErr ) return;
111066 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
111067 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
111068 || pExpr->op==TK_LT || pExpr->op==TK_GT
111069 || pExpr->op==TK_LE || pExpr->op==TK_GE
111071 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
111072 || (pExpr->op==TK_ISNOT && op==TK_NE) );
111073 assert( p5==0 || pExpr->op!=op );
111074 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
111102 if( (opx==TK_LT || opx==TK_GT) && i<nLeft-1 ){
111112 if( i==nLeft-1 ){
111120 if( i==nLeft-2 ) opx = op;
111138 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
111159 if( p->nHeight>*pnHeight ){
111160 *pnHeight = p->nHeight;
111167 for(i=0; i<p->nExpr; i++){
111168 heightOfExpr(p->a[i].pExpr, pnHeight);
111174 for(p=pSelect; p; p=p->pPrior){
111175 heightOfExpr(p->pWhere, pnHeight);
111176 heightOfExpr(p->pHaving, pnHeight);
111177 heightOfExpr(p->pLimit, pnHeight);
111178 heightOfExprList(p->pEList, pnHeight);
111179 heightOfExprList(p->pGroupBy, pnHeight);
111180 heightOfExprList(p->pOrderBy, pnHeight);
111195 int nHeight = p->pLeft ? p->pLeft->nHeight : 0;
111196 if( NEVER(p->pRight) && p->pRight->nHeight>nHeight ){
111197 nHeight = p->pRight->nHeight;
111200 heightOfSelect(p->x.pSelect, &nHeight);
111201 }else if( p->x.pList ){
111202 heightOfExprList(p->x.pList, &nHeight);
111203 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
111205 p->nHeight = nHeight + 1;
111217 if( pParse->nErr ) return;
111219 sqlite3ExprCheckHeight(pParse, p->nHeight);
111237 if( pParse->nErr ) return;
111238 if( p && ExprUseXList(p) && p->x.pList ){
111239 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
111251 pExpr->w.iOfst = iOfst;
111265 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
111268 ** Special case (tag-20240227-a): If op==TK_INTEGER and pToken points to
111269 ** a string that can be translated into a 32-bit integer, then the token is
111273 ** See also tag-20240227-b.
111287 if( op!=TK_INTEGER || pToken->z==0
111288 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
111289 nExtra = pToken->n+1; /* tag-20240227-a */
111296 pNew->op = (u8)op;
111297 pNew->iAgg = -1;
111300 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
111301 pNew->u.iValue = iValue;
111303 pNew->u.zToken = (char*)&pNew[1];
111304 assert( pToken->z!=0 || pToken->n==0 );
111305 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
111306 pNew->u.zToken[pToken->n] = 0;
111307 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
111313 pNew->nHeight = 1;
111320 ** Allocate a new expression node from a zero-terminated token that has
111347 assert( db->mallocFailed );
111352 assert( pRoot->x.pSelect==0 );
111354 pRoot->pRight = pRight;
111355 pRoot->flags |= EP_Propagate & pRight->flags;
111357 pRoot->nHeight = pRight->nHeight+1;
111359 pRoot->nHeight = 1;
111363 pRoot->pLeft = pLeft;
111364 pRoot->flags |= EP_Propagate & pLeft->flags;
111366 if( pLeft->nHeight>=pRoot->nHeight ){
111367 pRoot->nHeight = pLeft->nHeight+1;
111378 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
111388 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
111391 p->op = op & 0xff;
111392 p->iAgg = -1;
111393 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
111394 sqlite3ExprCheckHeight(pParse, p->nHeight);
111396 sqlite3ExprDelete(pParse->db, pLeft);
111397 sqlite3ExprDelete(pParse->db, pRight);
111408 pExpr->x.pSelect = pSelect;
111412 assert( pParse->db->mallocFailed );
111413 sqlite3SelectDelete(pParse->db, pSelect);
111440 for(ii=0; ii<pEList->nExpr; ii++){
111442 Expr *pExpr = pEList->a[ii].pExpr;
111444 if( pExpr->op==TK_VECTOR ){
111446 nExprElem = pExpr->x.pList->nExpr;
111451 sqlite3ErrorMsg(pParse, "IN(...) element has %d term%s - expected %d",
111457 pSel = sqlite3SelectNew(pParse, pExpr->x.pList, 0, 0, 0, 0, 0, SF_Values,0);
111458 pExpr->x.pList = 0;
111461 pSel->op = TK_ALL;
111462 pSel->pPrior = pRet;
111468 if( pRet && pRet->pPrior ){
111469 pRet->selFlags |= SF_MultiValue;
111471 sqlite3ExprListDelete(pParse->db, pEList);
111484 sqlite3 *db = pParse->db;
111490 u32 f = pLeft->flags | pRight->flags;
111514 sqlite3 *db = pParse->db;
111522 pNew->w.iOfst = (int)(pToken->z - pParse->zTail);
111524 && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG]
111525 && !pParse->nested
111529 pNew->x.pList = pList;
111539 ** the arguments of a non-aggregate function.
111543 "ORDER BY may not be used with non-aggregate %#T()", p
111563 sqlite3 *db = pParse->db;
111565 assert( db->mallocFailed );
111569 assert( db->mallocFailed );
111573 assert( pExpr->op==TK_FUNCTION );
111574 assert( pExpr->pLeft==0 );
111576 if( pExpr->x.pList==0 || NEVER(pExpr->x.pList->nExpr==0) ){
111577 /* Ignore ORDER BY on zero-argument aggregates */
111592 pOB->x.pList = pOrderBy;
111594 pExpr->pLeft = pOB;
111602 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
111604 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
111605 ** top-level SQL
111615 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
111617 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
111618 || (pParse->db->flags & SQLITE_TrustedSchema)==0
111649 sqlite3 *db = pParse->db;
111655 z = pExpr->u.zToken;
111662 x = (ynVar)(++pParse->nVar);
111670 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
111671 i = z[1]-'0'; /* The common case of ?N for a single digit N */
111674 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
111678 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
111679 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
111680 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
111682 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
111683 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
111687 if( x>pParse->nVar ){
111688 pParse->nVar = (int)x;
111690 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
111698 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
111700 x = (ynVar)(++pParse->nVar);
111705 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
111708 pExpr->iColumn = x;
111709 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
111711 sqlite3RecordErrorOffsetOfExpr(pParse->db, pExpr);
111722 assert( !ExprUseUValue(p) || p->u.iValue>=0 );
111724 assert( !ExprUseYWin(p) || p->y.pWin!=0 || db->mallocFailed );
111725 assert( p->op!=TK_FUNCTION || !ExprUseYSub(p) );
111728 assert( p->pLeft==0 );
111729 assert( p->pRight==0 );
111730 assert( !ExprUseXSelect(p) || p->x.pSelect==0 );
111731 assert( !ExprUseXList(p) || p->x.pList==0 );
111736 assert( (ExprUseXList(p) && p->x.pList==0) || p->pRight==0 );
111737 if( p->pRight ){
111739 sqlite3ExprDeleteNN(db, p->pRight);
111742 sqlite3SelectDelete(db, p->x.pSelect);
111744 sqlite3ExprListDelete(db, p->x.pList);
111747 sqlite3WindowDelete(db, p->y.pWin);
111751 if( p->pLeft && p->op!=TK_SELECT_COLUMN ){
111752 Expr *pLeft = p->pLeft;
111782 }else if( p->pOn ){
111783 sqlite3ExprDeleteNN(db, p->pOn);
111784 }else if( p->pUsing ){
111785 sqlite3IdListDelete(db, p->pUsing);
111796 ** Return 0 if the delete was successfully deferred. Return non-zero
111811 sqlite3ExprDeleteNN(pParse->db, p);
111837 ** The dupedExprStructSize() function returns two values OR-ed together:
111850 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
111871 if( p->pLeft || p->x.pList ){
111874 assert( p->pRight==0 );
111888 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
111889 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
111909 if( p->pLeft ) nByte += dupedExprSize(p->pLeft);
111910 if( p->pRight ) nByte += dupedExprSize(p->pRight);
111918 ** compression while doing sqlite3ExprDup(). The top-level Expr does the
111934 ** of the input Expr p together with its p->u.zToken (if any). pEdupBuf
111946 int nToken = -1; /* Space needed for p->u.zToken. -1 means unknown */
111955 sEdupBuf.zAlloc = pEdupBuf->zAlloc;
111957 sEdupBuf.zEnd = pEdupBuf->zEnd;
111966 }else if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
111967 nToken = sqlite3Strlen30NN(p->u.zToken)+1;
111988 ** by the copy of the p->u.zToken string (if any).
111993 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
111994 nToken = sqlite3Strlen30(p->u.zToken) + 1;
112000 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >= nNewSize+nToken );
112005 assert( (int)(sEdupBuf.zEnd - sEdupBuf.zAlloc) >=
112009 memset(&sEdupBuf.zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
112015 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static);
112016 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
112017 pNew->flags |= staticFlag;
112023 /* Copy the p->u.zToken string, if any. */
112026 char *zToken = pNew->u.zToken = (char*)&sEdupBuf.zAlloc[nNewSize];
112027 memcpy(zToken, p->u.zToken, nToken);
112032 if( ((p->flags|pNew->flags)&(EP_TokenOnly|EP_Leaf))==0 ){
112034 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
112036 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
112038 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList,
112039 p->op!=TK_ORDER ? dupFlags : 0);
112044 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
112049 /* Fill in pNew->pLeft and pNew->pRight. */
112051 if( p->op==TK_SELECT_COLUMN ){
112052 pNew->pLeft = p->pLeft;
112053 assert( p->pRight==0
112054 || p->pRight==p->pLeft
112055 || ExprHasProperty(p->pLeft, EP_Subquery) );
112057 pNew->pLeft = p->pLeft ?
112058 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &sEdupBuf) : 0;
112060 pNew->pRight = p->pRight ?
112061 exprDup(db, p->pRight, EXPRDUP_REDUCE, &sEdupBuf) : 0;
112063 if( p->op==TK_SELECT_COLUMN ){
112064 pNew->pLeft = p->pLeft;
112065 assert( p->pRight==0
112066 || p->pRight==p->pLeft
112067 || ExprHasProperty(p->pLeft, EP_Subquery) );
112069 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
112071 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
112083 ** and the db->mallocFailed flag set.
112089 sqlite3_int64 nByte = SZ_WITH(p->nCte);
112093 pRet->nCte = p->nCte;
112094 for(i=0; i<p->nCte; i++){
112095 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
112096 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
112097 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
112098 pRet->a[i].eM10d = p->a[i].eM10d;
112113 ** objects found there, assembling them onto the linked list at Select->pWin.
112116 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
112117 Select *pSelect = pWalker->u.pSelect;
112118 Window *pWin = pExpr->y.pWin;
112121 assert( pWin->ppThis==0 );
112127 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
112156 ** part of the in-memory representation of the database schema.
112173 pNew->nExpr = p->nExpr;
112174 pNew->nAlloc = p->nAlloc;
112175 pItem = pNew->a;
112176 pOldItem = p->a;
112177 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
112178 Expr *pOldExpr = pOldItem->pExpr;
112180 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
112182 && pOldExpr->op==TK_SELECT_COLUMN
112183 && (pNewExpr = pItem->pExpr)!=0
112185 if( pNewExpr->pRight ){
112186 pPriorSelectColOld = pOldExpr->pRight;
112187 pPriorSelectColNew = pNewExpr->pRight;
112188 pNewExpr->pLeft = pNewExpr->pRight;
112190 if( pOldExpr->pLeft!=pPriorSelectColOld ){
112191 pPriorSelectColOld = pOldExpr->pLeft;
112193 pNewExpr->pRight = pPriorSelectColNew;
112195 pNewExpr->pLeft = pPriorSelectColNew;
112198 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
112199 pItem->fg = pOldItem->fg;
112200 pItem->u = pOldItem->u;
112218 pNew = sqlite3DbMallocRawNN(db, SZ_SRCLIST(p->nSrc) );
112220 pNew->nSrc = pNew->nAlloc = p->nSrc;
112221 for(i=0; i<p->nSrc; i++){
112222 SrcItem *pNewItem = &pNew->a[i];
112223 const SrcItem *pOldItem = &p->a[i];
112225 pNewItem->fg = pOldItem->fg;
112226 if( pOldItem->fg.isSubquery ){
112229 assert( db->mallocFailed );
112230 pNewItem->fg.isSubquery = 0;
112232 memcpy(pNewSubq, pOldItem->u4.pSubq, sizeof(*pNewSubq));
112233 pNewSubq->pSelect = sqlite3SelectDup(db, pNewSubq->pSelect, flags);
112234 if( pNewSubq->pSelect==0 ){
112237 pNewItem->fg.isSubquery = 0;
112240 pNewItem->u4.pSubq = pNewSubq;
112241 }else if( pOldItem->fg.fixedSchema ){
112242 pNewItem->u4.pSchema = pOldItem->u4.pSchema;
112244 pNewItem->u4.zDatabase = sqlite3DbStrDup(db, pOldItem->u4.zDatabase);
112246 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
112247 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
112248 pNewItem->iCursor = pOldItem->iCursor;
112249 if( pNewItem->fg.isIndexedBy ){
112250 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
112251 }else if( pNewItem->fg.isTabFunc ){
112252 pNewItem->u1.pFuncArg =
112253 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
112255 pNewItem->u1.nRow = pOldItem->u1.nRow;
112257 pNewItem->u2 = pOldItem->u2;
112258 if( pNewItem->fg.isCte ){
112259 pNewItem->u2.pCteUse->nUse++;
112261 pTab = pNewItem->pSTab = pOldItem->pSTab;
112263 pTab->nTabRef++;
112265 if( pOldItem->fg.isUsing ){
112266 assert( pNewItem->fg.isUsing );
112267 pNewItem->u3.pUsing = sqlite3IdListDup(db, pOldItem->u3.pUsing);
112269 pNewItem->u3.pOn = sqlite3ExprDup(db, pOldItem->u3.pOn, flags);
112271 pNewItem->colUsed = pOldItem->colUsed;
112280 pNew = sqlite3DbMallocRawNN(db, SZ_IDLIST(p->nId));
112282 pNew->nId = p->nId;
112283 for(i=0; i<p->nId; i++){
112284 struct IdList_item *pNewItem = &pNew->a[i];
112285 const struct IdList_item *pOldItem = &p->a[i];
112286 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
112297 for(p=pDup; p; p=p->pPrior){
112300 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
112301 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
112302 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
112303 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
112304 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
112305 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
112306 pNew->op = p->op;
112307 pNew->pNext = pNext;
112308 pNew->pPrior = 0;
112309 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
112310 pNew->iLimit = 0;
112311 pNew->iOffset = 0;
112312 pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
112313 pNew->addrOpenEphm[0] = -1;
112314 pNew->addrOpenEphm[1] = -1;
112315 pNew->nSelectRow = p->nSelectRow;
112316 pNew->pWith = sqlite3WithDup(db, p->pWith);
112318 pNew->pWin = 0;
112319 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
112320 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
112322 pNew->selId = p->selId;
112323 if( db->mallocFailed ){
112327 pNew->pNext = 0;
112332 pp = &pNew->pPrior;
112353 ** NULL is returned. If non-NULL is returned, then it is guaranteed
112369 pList->nAlloc = 4;
112370 pList->nExpr = 1;
112371 pItem = &pList->a[0];
112373 pItem->pExpr = pExpr;
112383 pList->nAlloc *= 2;
112384 pNew = sqlite3DbRealloc(db, pList, SZ_EXPRLIST(pList->nAlloc));
112392 pItem = &pList->a[pList->nExpr++];
112394 pItem->pExpr = pExpr;
112404 return sqlite3ExprListAppendNew(pParse->db,pExpr);
112406 if( pList->nAlloc<pList->nExpr+1 ){
112407 return sqlite3ExprListAppendGrow(pParse->db,pList,pExpr);
112409 pItem = &pList->a[pList->nExpr++];
112411 pItem->pExpr = pExpr;
112432 sqlite3 *db = pParse->db;
112435 int iFirst = pList ? pList->nExpr : 0;
112446 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
112448 pColumns->nId, n);
112452 for(i=0; i<pColumns->nId; i++){
112453 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i, pColumns->nId);
112454 assert( pSubExpr!=0 || db->mallocFailed );
112458 assert( pList->nExpr==iFirst+i+1 );
112459 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
112460 pColumns->a[i].zName = 0;
112464 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
112465 Expr *pFirst = pList->a[iFirst].pExpr;
112467 assert( pFirst->op==TK_SELECT_COLUMN );
112471 pFirst->pRight = pExpr;
112476 pFirst->iTable = pColumns->nId;
112491 assert( p->nExpr>0 );
112503 pItem = &p->a[p->nExpr-1];
112504 assert( pItem->fg.bNulls==0 );
112508 pItem->fg.sortFlags = (u8)iSortOrder;
112511 pItem->fg.bNulls = 1;
112513 pItem->fg.sortFlags |= KEYINFO_ORDER_BIGNULL;
112523 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
112532 assert( pList!=0 || pParse->db->mallocFailed!=0 );
112533 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
112536 assert( pList->nExpr>0 );
112537 pItem = &pList->a[pList->nExpr-1];
112538 assert( pItem->zEName==0 );
112539 assert( pItem->fg.eEName==ENAME_NAME );
112540 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
112542 /* If dequote==0, then pName->z does not point to part of a DDL
112544 ** to the token-map. */
112545 sqlite3Dequote(pItem->zEName);
112547 sqlite3RenameTokenMap(pParse, (const void*)pItem->zEName, pName);
112558 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
112567 sqlite3 *db = pParse->db;
112568 assert( pList!=0 || db->mallocFailed!=0 );
112570 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
112571 assert( pList->nExpr>0 );
112572 if( pItem->zEName==0 ){
112573 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
112574 pItem->fg.eEName = ENAME_SPAN;
112588 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
112589 testcase( pEList && pEList->nExpr==mx );
112590 testcase( pEList && pEList->nExpr==mx+1 );
112591 if( pEList && pEList->nExpr>mx ){
112600 int i = pList->nExpr;
112601 struct ExprList_item *pItem = pList->a;
112602 assert( pList->nExpr>0 );
112605 sqlite3ExprDelete(db, pItem->pExpr);
112606 if( pItem->zEName ) sqlite3DbNNFreeNN(db, pItem->zEName);
112608 }while( --i>0 );
112619 ** Return the bitwise-OR of all Expr.flags fields in the given
112626 for(i=0; i<pList->nExpr; i++){
112627 Expr *pExpr = pList->a[i].pExpr;
112629 m |= pExpr->flags;
112635 ** This is a SELECT-node callback for the expression walker that
112637 ** pWalker->eCode to zero and abort.
112643 pWalker->eCode = 0;
112664 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
112669 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
112671 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
112673 pExpr->op = TK_TRUEFALSE;
112686 assert( pExpr->op==TK_TRUEFALSE );
112688 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
112689 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
112690 return pExpr->u.zToken[4]==0;
112708 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
112709 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
112710 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
112712 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
112714 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
112730 ** This routine sets pWalker->eCode to 0 if pExpr is not a constant.
112731 ** It makes no changes to pWalker->eCode if pExpr is constant. In
112745 assert( pExpr->op==TK_FUNCTION );
112747 || (pList = pExpr->x.pList)==0
112751 n = pList->nExpr;
112753 if( pWalker->eCode==0 ) return WRC_Abort;
112755 db = pWalker->pParse->db;
112756 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
112758 || pDef->xFinalize!=0
112759 || (pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
112762 pWalker->eCode = 0;
112777 ** sqlite3ExprIsConstant() pWalker->eCode==1
112778 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
112779 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
112780 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
112796 assert( pWalker->eCode>0 );
112798 /* If pWalker->eCode is 2 then any term of the expression that comes from
112801 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_OuterON) ){
112802 pWalker->eCode = 0;
112806 switch( pExpr->op ){
112808 ** and either pWalker->eCode==4 or 5 or the function has the
112811 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
112814 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
112816 }else if( pWalker->pParse ){
112819 pWalker->eCode = 0;
112832 testcase( pExpr->op==TK_ID );
112833 testcase( pExpr->op==TK_COLUMN );
112834 testcase( pExpr->op==TK_AGG_FUNCTION );
112835 testcase( pExpr->op==TK_AGG_COLUMN );
112836 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
112839 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
112847 testcase( pExpr->op==TK_REGISTER );
112848 testcase( pExpr->op==TK_IF_NULL_ROW );
112849 testcase( pExpr->op==TK_DOT );
112850 testcase( pExpr->op==TK_RAISE );
112851 pWalker->eCode = 0;
112854 if( pWalker->eCode==5 ){
112858 pExpr->op = TK_NULL;
112859 }else if( pWalker->eCode==4 ){
112862 pWalker->eCode = 0;
112867 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
112868 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
112886 ** Walk an expression tree. Return non-zero if the expression is constant
112889 ** For the purposes of this function, a double-quoted string (ex: "abc")
112890 ** is considered a variable but a single-quoted string (ex: 'abc') is
112895 ** function calls will be considered to be non-constant. If pParse is
112904 ** Walk an expression tree. Return non-zero if
112913 ** can be added to the pParse->pConstExpr list and evaluated once when
112921 ** This routine examines sub-SELECT statements as an expression is being
112922 ** walked as part of sqlite3ExprIsTableConstant(). Sub-SELECTs are considered
112923 ** constant as long as they are uncorrelated - meaning that they do not
112928 assert( pWalker->eCode==3 || pWalker->eCode==0 );
112929 if( (pSelect->selFlags & SF_Correlated)!=0 ){
112930 pWalker->eCode = 0;
112937 ** Walk an expression tree. Return non-zero if the expression is constant
112939 ** expression must not refer to any non-deterministic function nor any
112965 ** pSrc = &pSrcList->a[iSrc]. In other words, check to see if pExpr
112967 ** sources anywhere else in the query. Return true (non-zero) if pExpr
112974 ** To be an single-source constraint, the following must be true:
112976 ** (1) pExpr cannot refer to any table other than pSrc->iCursor.
112979 ** true and the subquery is non-correlated
112981 ** (2b) pExpr cannot use non-deterministic functions.
113001 ** Without this restriction, accepting pExpr as a single-table
113005 ** on push-down.
113011 int bAllowSubq /* Allow non-correlated subqueries */
113013 const SrcItem *pSrc = &pSrcList->a[iSrc];
113014 if( pSrc->fg.jointype & JT_LTORJ ){
113017 if( pSrc->fg.jointype & JT_LEFT ){
113019 if( pExpr->w.iJoin!=pSrc->iCursor ) return 0; /* rule (4b) */
113024 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (6b) */
113028 if( pExpr->w.iJoin==pSrcList->a[jj].iCursor ){
113029 if( (pSrcList->a[jj].fg.jointype & JT_LTORJ)!=0 ){
113037 return sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor, bAllowSubq);
113045 ExprList *pGroupBy = pWalker->u.pGroupBy;
113050 for(i=0; i<pGroupBy->nExpr; i++){
113051 Expr *p = pGroupBy->a[i].pExpr;
113052 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
113053 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
113060 /* Check if pExpr is a sub-select. If so, consider it variable. */
113062 pWalker->eCode = 0;
113070 ** Walk the expression tree passed as the first argument. Return non-zero
113078 ** assumes that no other collating sequence will have a finer-grained
113101 ** in a CREATE TABLE statement. Return non-zero if the expression is
113102 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
113116 ** For the purposes of this function, a double-quoted string (ex: "abc")
113117 ** is considered a variable but a single-quoted string (ex: 'abc') is
113145 ** to fit in a 32-bit integer, return 1 and put the value of the integer
113147 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
113157 /* If an expression is an integer literal that fits in a signed 32-bit
113159 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
113160 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
113162 if( p->flags & EP_IntValue ){
113163 *pValue = p->u.iValue;
113166 switch( p->op ){
113168 rc = sqlite3ExprIsInteger(p->pLeft, pValue, 0);
113173 if( sqlite3ExprIsInteger(p->pLeft, &v, 0) ){
113175 *pValue = -v;
113183 if( NEVER(pParse->pVdbe==0) ) break;
113184 if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) break;
113185 sqlite3VdbeSetVarmask(pParse->pVdbe, p->iColumn);
113186 pVal = sqlite3VdbeGetBoundValue(pParse->pReprepare, p->iColumn,
113191 if( vv == (vv & 0x7fffffff) ){ /* non-negative numbers only */
113222 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
113223 p = p->pLeft;
113226 op = p->op;
113227 if( op==TK_REGISTER ) op = p->op2;
113237 || NEVER(p->y.pTab==0) /* Reference to column of index on expr */
113239 || (p->iColumn==XN_ROWID && IsView(p->y.pTab))
113241 || (p->iColumn>=0
113242 && p->y.pTab->aCol!=0 /* Possible due to prior error */
113243 && ALWAYS(p->iColumn<p->y.pTab->nCol)
113244 && p->y.pTab->aCol[p->iColumn].notNull==0);
113264 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
113265 if( p->op==TK_UMINUS ) unaryMinus = 1;
113266 p = p->pLeft;
113268 op = p->op;
113269 if( op==TK_REGISTER ) op = p->op2;
113284 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
113285 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
113294 ** Return TRUE if the given string is a row-id column name.
113305 ** pTab. An alias is usable if there is not an explicit user-defined column
113334 p = pX->x.pSelect;
113335 if( p->pPrior ) return 0; /* Not a compound SELECT */
113336 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
113337 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
113338 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
113341 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
113342 if( p->pLimit ) return 0; /* Has no LIMIT clause */
113343 if( p->pWhere ) return 0; /* Has no WHERE clause */
113344 pSrc = p->pSrc;
113346 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
113347 if( pSrc->a[0].fg.isSubquery) return 0;/* FROM is not a subquery or view */
113348 pTab = pSrc->a[0].pSTab;
113352 pEList = p->pEList;
113355 for(i=0; i<pEList->nExpr; i++){
113356 Expr *pRes = pEList->a[i].pExpr;
113357 if( pRes->op!=TK_COLUMN ) return 0;
113358 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
113366 ** Generate code that checks the left-most column of index table iCur to see if
113368 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
113386 ** right-hand side. Return TRUE if that list is constant.
113392 pLHS = pIn->pLeft;
113393 pIn->pLeft = 0;
113395 pIn->pLeft = pLHS;
113405 ** The job of this routine is to find or create a b-tree object that can
113409 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
113412 ** The returned value of this function indicates the b-tree type, as follows:
113414 ** IN_INDEX_ROWID - The cursor was opened on a database table.
113415 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
113416 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
113417 ** IN_INDEX_EPH - The cursor was opened on a specially created and
113419 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
113422 ** An existing b-tree might be used if the RHS expression pX is a simple
113429 ** pX->iTable made to point to the ephemeral table instead of an
113432 ** will be set on pX and the pX->y.sub fields will be set to show where
113441 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
113442 ** through the set members) then the b-tree must not contain duplicates.
113444 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
113447 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
113450 ** index can be found with the specified <columns> as its left-most.
113454 ** routine might decide that creating an ephemeral b-tree for membership
113459 ** When the b-tree is being used for membership tests, the calling function
113468 ** the value in that register will be NULL if the b-tree contains one or more
113469 ** NULL values, and it will be some non-NULL value if the b-tree contains no
113498 assert( pX->op==TK_IN );
113500 iTab = pParse->nTab++;
113509 ExprList *pEList = pX->x.pSelect->pEList;
113510 for(i=0; i<pEList->nExpr; i++){
113511 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
113513 if( i==pEList->nExpr ){
113521 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
113522 sqlite3 *db = pParse->db; /* Database connection */
113525 ExprList *pEList = p->pEList;
113526 int nExpr = pEList->nExpr;
113528 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
113529 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
113530 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
113531 pTab = p->pSrc->a[0].pSTab;
113534 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
113537 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
113540 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
113548 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
113560 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
113561 int iCol = pEList->a[i].pExpr->iColumn;
113583 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
113586 if( pIdx->nColumn<nExpr ) continue;
113587 if( pIdx->pPartIdxWhere!=0 ) continue;
113588 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
113590 testcase( pIdx->nColumn==BMS-2 );
113591 testcase( pIdx->nColumn==BMS-1 );
113592 if( pIdx->nColumn>=BMS-1 ) continue;
113594 if( pIdx->nKeyCol>nExpr
113595 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
113603 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
113604 Expr *pRhs = pEList->a[i].pExpr;
113609 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
113610 assert( pIdx->azColl[j] );
113611 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
113624 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
113625 if( colUsed==(MASKBIT(nExpr)-1) ){
113629 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
113630 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
113632 VdbeComment((v, "%s", pIdx->zName));
113634 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
113638 i64 mask = (1<<nExpr)-1;
113642 *prRhsHasNull = ++pParse->nMem;
113664 && (!sqlite3InRhsIsConstant(pParse,pX) || pX->x.pList->nExpr<=2)
113666 pParse->nTab--; /* Back out the allocation of the unused cursor */
113667 iTab = -1; /* Cursor is not allocated */
113672 /* Could not find an existing table or index to use as the RHS b-tree.
113675 u32 savedNQueryLoop = pParse->nQueryLoop;
113679 pParse->nQueryLoop = 0;
113681 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
113683 assert( pX->op==TK_IN );
113688 pParse->nQueryLoop = savedNQueryLoop;
113693 n = sqlite3ExprVectorSize(pX->pLeft);
113704 ** function allocates and returns a nul-terminated string containing
113711 Expr *pLeft = pExpr->pLeft;
113713 Select *pSelect = ExprUseXSelect(pExpr) ? pExpr->x.pSelect : 0;
113716 assert( pExpr->op==TK_IN );
113717 zRet = sqlite3DbMallocRaw(pParse->db, 1+(i64)nVal);
113724 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
113740 ** "sub-select returns N columns - expected M"
113743 if( pParse->nErr==0 ){
113744 const char *zFmt = "sub-select returns %d columns - expected %d";
113752 ** it is not permitted. If pExpr is a sub-select vector, this routine
113755 ** "sub-select returns N columns - expected 1"
113764 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
113788 if( (pParse->mSubrtnSig & (1<<(pNewSig->selId&7)))==0 ) return 0;
113789 assert( pExpr->op==TK_IN );
113792 assert( pExpr->x.pSelect!=0 );
113793 assert( (pExpr->x.pSelect->selFlags & SF_All)==0 );
113794 v = pParse->pVdbe;
113799 if( pOp->p4type!=P4_SUBRTNSIG ) continue;
113800 assert( pOp->opcode==OP_BeginSubrtn );
113801 pSig = pOp->p4.pSubrtnSig;
113803 if( !pSig->bComplete ) continue;
113804 if( pNewSig->selId!=pSig->selId ) continue;
113805 if( strcmp(pNewSig->zAff,pSig->zAff)!=0 ) continue;
113806 pExpr->y.sub.iAddr = pSig->iAddr;
113807 pExpr->y.sub.regReturn = pSig->regReturn;
113808 pExpr->iTable = pSig->iTable;
113822 ** x IN (4,5,11) -- IN operator with list on right-hand side
113823 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
113827 ** table is computed, the cursor number is also stored in pExpr->iTable,
113852 v = pParse->pVdbe;
113858 ** * The right-hand side is a correlated subquery
113859 ** * The right-hand side is an expression list containing variables
113865 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
113871 assert( !ExprUseXSelect(pExpr) || pExpr->x.pSelect!=0 );
113872 if( ExprUseXSelect(pExpr) && (pExpr->x.pSelect->selFlags & SF_All)==0 ){
113873 pSig = sqlite3DbMallocRawNN(pParse->db, sizeof(pSig[0]));
113875 pSig->selId = pExpr->x.pSelect->selId;
113876 pSig->zAff = exprINAffinity(pParse, pExpr);
113890 pExpr->x.pSelect->selId));
113893 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
113894 pExpr->y.sub.iAddr);
113895 assert( iTab!=pExpr->iTable );
113896 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
113899 sqlite3DbFree(pParse->db, pSig->zAff);
113900 sqlite3DbFree(pParse->db, pSig);
113909 pExpr->y.sub.regReturn = ++pParse->nMem;
113910 pExpr->y.sub.iAddr =
113911 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
113913 pSig->bComplete = 0;
113914 pSig->iAddr = pExpr->y.sub.iAddr;
113915 pSig->regReturn = pExpr->y.sub.regReturn;
113916 pSig->iTable = iTab;
113917 pParse->mSubrtnSig = 1 << (pSig->selId&7);
113918 sqlite3VdbeChangeP4(v, -1, (const char*)pSig, P4_SUBRTNSIG);
113924 pLeft = pExpr->pLeft;
113930 pExpr->iTable = iTab;
113931 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
113934 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
113939 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
113947 Select *pSelect = pExpr->x.pSelect;
113948 ExprList *pEList = pSelect->pEList;
113951 addrOnce?"":"CORRELATED ", pSelect->selId
113955 if( ALWAYS(pEList->nExpr==nVal) ){
113963 pSelect->iLimit = 0;
113964 if( addrOnce && OptimizationEnabled(pParse->db, SQLITE_BloomFilter) ){
113965 int regBloom = ++pParse->nMem;
113970 testcase( pSelect->selFlags & SF_Distinct );
113972 pCopy = sqlite3SelectDup(pParse->db, pSelect, 0);
113973 rc = pParse->db->mallocFailed ? 1 :sqlite3Select(pParse, pCopy, &dest);
113974 sqlite3SelectDelete(pParse->db, pCopy);
113975 sqlite3DbFree(pParse->db, dest.zAffSdst);
113978 ** of the OP_Once that began this subroutine. tag-202407032019 */
113979 sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
113982 sqlite3VdbeGetOp(v, addrBloom)->p1 = 10;
113991 assert( pEList->nExpr>0 );
113995 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
113996 pParse, p, pEList->a[i].pExpr
114000 }else if( ALWAYS(pExpr->x.pList!=0) ){
114010 ExprList *pList = pExpr->x.pList;
114021 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
114027 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
114028 Expr *pE2 = pItem->pExpr;
114032 ** this code only executes once. Because for a non-constant
114036 sqlite3VdbeChangeToNoop(v, addrOnce-1);
114050 if( pSig ) pSig->bComplete = 1;
114059 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
114060 || pParse->nErr );
114061 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
114062 pExpr->y.sub.iAddr, 1);
114073 ** (SELECT a FROM b) -- subquery
114074 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
114078 ** Return the register that holds the result. For a multi-column SELECT,
114080 ** return value is the register of the left-most result column.
114095 Vdbe *v = pParse->pVdbe;
114097 if( pParse->nErr ) return 0;
114098 testcase( pExpr->op==TK_EXISTS );
114099 testcase( pExpr->op==TK_SELECT );
114100 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
114102 pSel = pExpr->x.pSelect;
114107 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
114109 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
114110 pExpr->y.sub.iAddr);
114111 return pExpr->iTable;
114118 pExpr->y.sub.regReturn = ++pParse->nMem;
114119 pExpr->y.sub.iAddr =
114120 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pExpr->y.sub.regReturn) + 1;
114125 ** * The right-hand side is a correlated subquery
114126 ** * The right-hand side is an expression list containing variables
114147 addrOnce?"":"CORRELATED ", pSel->selId));
114148 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, -1);
114149 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
114150 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
114151 pParse->nMem += nReg;
114152 if( pExpr->op==TK_SELECT ){
114156 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
114163 if( pSel->pLimit ){
114164 /* The subquery already has a limit. If the pre-existing limit is X
114166 sqlite3 *db = pParse->db;
114169 pLimit->affExpr = SQLITE_AFF_NUMERIC;
114171 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
114173 sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
114174 pSel->pLimit->pLeft = pLimit;
114176 /* If there is no pre-existing limit add a limit of 1 */
114177 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
114178 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
114180 pSel->iLimit = 0;
114182 pExpr->op2 = pExpr->op;
114183 pExpr->op = TK_ERROR;
114186 pExpr->iTable = rReg = dest.iSDParm;
114191 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
114195 assert( sqlite3VdbeGetOp(v,pExpr->y.sub.iAddr-1)->opcode==OP_BeginSubrtn
114196 || pParse->nErr );
114197 sqlite3VdbeAddOp3(v, OP_Return, pExpr->y.sub.regReturn,
114198 pExpr->y.sub.iAddr, 1);
114208 ** sub-select on the RHS of the IN() operator has the same number of
114210 ** a sub-query, that the LHS is a vector of size 1.
114213 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
114214 if( ExprUseXSelect(pIn) && !pParse->db->mallocFailed ){
114215 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
114216 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
114220 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
114234 ** The left-hand side (LHS) is a scalar or vector expression. The
114235 ** right-hand side (RHS) is an array of zero or more scalar values, or a
114250 ** See the separate in-operator.md documentation file in the canonical
114274 int addrTop; /* Top of the step-6 loop */
114276 u8 okConstFactor = pParse->okConstFactor;
114279 pLeft = pExpr->pLeft;
114282 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
114283 aiMap = (int*)sqlite3DbMallocZero(pParse->db, nVector*sizeof(int));
114284 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
114290 v = pParse->pVdbe;
114298 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
114303 ** nVector-1. */
114323 assert( pParse->okConstFactor==okConstFactor );
114324 pParse->okConstFactor = 0;
114326 pParse->okConstFactor = okConstFactor;
114343 ** This is step (1) in the in-operator.md optimized algorithm.
114353 pList = pExpr->x.pList;
114354 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
114359 for(ii=0; ii<pList->nExpr; ii++){
114360 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
114361 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
114365 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
114369 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
114370 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
114371 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
114372 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
114403 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
114404 if( pParse->nErr ) goto sqlite3ExprCodeIN_oom_error;
114411 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
114416 /* In this case, the RHS is the ROWID of table b-tree and so we also
114417 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
114427 const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
114428 assert( pOp->opcode==OP_Once || pParse->nErr );
114429 if( pOp->opcode==OP_Once && pOp->p3>0 ){ /* tag-202407032019 */
114430 assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) );
114431 sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse,
114444 /* Step 4. If the RHS is known to be non-NULL and we did not find
114504 sqlite3DbFree(pParse->db, aiMap);
114505 sqlite3DbFree(pParse->db, zAff);
114512 ** value described by z[0..n-1] into register iMem.
114514 ** The z[] string will probably not be zero-terminated. But the
114523 if( negateFlag ) value = -value;
114532 ** text z[0..n-1] into register iMem.
114534 ** Expr.u.zToken is always UTF8 and zero-terminated.
114537 Vdbe *v = pParse->pVdbe;
114538 if( pExpr->flags & EP_IntValue ){
114539 int i = pExpr->u.iValue;
114541 if( negFlag ) i = -i;
114546 const char *z = pExpr->u.zToken;
114551 sqlite3ErrorMsg(pParse, "oversized integer: %s%#T", negFlag?"-":"",pExpr);
114556 negFlag?"-":"",pExpr);
114564 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
114572 ** appropriate for the iIdxCol-th column of index pIdx.
114581 i16 iTabCol = pIdx->aiColumn[iIdxCol];
114583 assert( pIdx->aColExpr );
114584 assert( pIdx->aColExpr->nExpr>iIdxCol );
114585 pParse->iSelfTab = iTabCur + 1;
114586 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
114587 pParse->iSelfTab = 0;
114589 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
114606 Vdbe *v = pParse->pVdbe;
114607 int nErr = pParse->nErr;
114609 assert( pParse->iSelfTab!=0 );
114610 if( pParse->iSelfTab>0 ){
114611 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
114616 if( pCol->affinity>=SQLITE_AFF_TEXT ){
114617 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
114620 if( pParse->nErr>nErr ) pParse->db->errByteOffset = -1;
114625 ** Generate code to extract the value of the iCol-th column of a table.
114638 if( iCol<0 || iCol==pTab->iPKey ){
114640 VdbeComment((v, "%s.rowid", pTab->zName));
114648 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
114650 if( pCol->colFlags & COLFLAG_BUSY ){
114652 pCol->zCnName);
114654 int savedSelfTab = pParse->iSelfTab;
114655 pCol->colFlags |= COLFLAG_BUSY;
114656 pParse->iSelfTab = iTabCur+1;
114658 pParse->iSelfTab = savedSelfTab;
114659 pCol->colFlags &= ~COLFLAG_BUSY;
114678 ** Generate code that will extract the iColumn-th column from
114692 assert( pParse->pVdbe!=0 );
114695 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
114697 VdbeOp *pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
114698 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
114699 if( pOp->opcode==OP_VColumn ) pOp->p5 = (p5 & OPFLAG_NOCHNG);
114705 ** Generate code to move content from registers iFrom...iFrom+nReg-1
114706 ** over to iTo..iTo+nReg-1.
114709 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
114720 if( p->op==TK_REGISTER ){
114721 assert( p->iTable==iReg );
114723 p->op2 = p->op;
114724 p->op = TK_REGISTER;
114725 p->iTable = iReg;
114747 if( p->op==TK_SELECT ){
114755 iResult = pParse->nMem+1;
114756 pParse->nMem += nResult;
114759 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
114767 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
114771 if( sqlite3VdbeGetLastOp(v)->opcode==OP_Copy ){
114778 ** in-line rather than by using the usual callbacks.
114787 Vdbe *v = pParse->pVdbe;
114790 nFarg = pFarg->nExpr;
114791 assert( nFarg>0 ); /* All in-line functions have at least one argument */
114794 /* Attempt a direct implementation of the built-in COALESCE() and
114796 ** arguments past the first non-NULL argument.
114801 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
114805 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
114820 Expr *pArg = pFarg->a[0].pExpr;
114821 if( pArg->op==TK_COLUMN && pArg->iTable>=0 ){
114822 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
114830 /* The UNLIKELY() function is a no-op. The result is the value
114834 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
114839 ** Test-only SQL functions that are only usable if enabled
114847 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
114856 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
114865 pA1 = pFarg->a[1].pExpr;
114866 if( pA1->op==TK_COLUMN ){
114868 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable,1),
114885 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
114889 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
114906 ** Assume that every function is able to pass-through a subtype from
114918 if( pExpr->op!=TK_FUNCTION ){
114922 db = pWalker->pParse->db;
114923 n = ALWAYS(pExpr->x.pList) ? pExpr->x.pList->nExpr : 0;
114924 pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
114925 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
114926 pWalker->eCode = 1;
114951 ** Check to see if pExpr is one of the indexed expressions on pParse->pIdxEpr.
114963 for(p=pParse->pIdxEpr; p; p=p->pIENext){
114965 int iDataCur = p->iDataCur;
114967 if( pParse->iSelfTab ){
114968 if( p->iDataCur!=pParse->iSelfTab-1 ) continue;
114969 iDataCur = -1;
114971 if( sqlite3ExprCompare(0, pExpr, p->pExpr, iDataCur)!=0 ) continue;
114972 assert( p->aff>=SQLITE_AFF_BLOB && p->aff<=SQLITE_AFF_NUMERIC );
114974 if( (exprAff<=SQLITE_AFF_BLOB && p->aff!=SQLITE_AFF_BLOB)
114975 || (exprAff==SQLITE_AFF_TEXT && p->aff!=SQLITE_AFF_TEXT)
114976 || (exprAff>=SQLITE_AFF_NUMERIC && p->aff!=SQLITE_AFF_NUMERIC)
114993 v = pParse->pVdbe;
114995 if( p->bMaybeNullRow ){
115000 sqlite3VdbeAddOp3(v, OP_IfNullRow, p->iIdxCur, addr+3, target);
115002 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
115003 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
115005 p = pParse->pIdxEpr;
115006 pParse->pIdxEpr = 0;
115008 pParse->pIdxEpr = p;
115011 sqlite3VdbeAddOp3(v, OP_Column, p->iIdxCur, p->iIdxCol, target);
115012 VdbeComment((v, "%s expr-column %d", p->zIdxName, p->iIdxCol));
115016 return -1; /* Not found */
115032 for(p=pParse->pIdxPartExpr; p; p=p->pIENext){
115033 if( pExpr->iColumn==p->iIdxCol && pExpr->iTable==p->iDataCur ){
115034 Vdbe *v = pParse->pVdbe;
115038 if( p->bMaybeNullRow ){
115039 addr = sqlite3VdbeAddOp1(v, OP_IfNullRow, p->iIdxCur);
115041 ret = sqlite3ExprCodeTarget(pParse, p->pExpr, iTarget);
115042 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Affinity, ret, 1, 0,
115043 (const char*)&p->aff, 1);
115067 Vdbe *v = pParse->pVdbe; /* The VM under construction */
115070 int regFree1 = 0; /* If non-zero free this temporary register */
115071 int regFree2 = 0; /* If non-zero free this temporary register */
115076 assert( target>0 && target<=pParse->nMem );
115082 }else if( pParse->pIdxEpr!=0
115089 op = pExpr->op;
115094 AggInfo *pAggInfo = pExpr->pAggInfo;
115097 assert( pExpr->iAgg>=0 );
115098 if( pExpr->iAgg>=pAggInfo->nColumn ){
115104 ** tag-20230325-2 */
115110 pCol = &pAggInfo->aCol[pExpr->iAgg];
115111 if( !pAggInfo->directMode ){
115112 return AggInfoColumnReg(pAggInfo, pExpr->iAgg);
115113 }else if( pAggInfo->useSortingIdx ){
115114 Table *pTab = pCol->pTab;
115115 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
115116 pCol->iSorterColumn, target);
115119 }else if( pCol->iColumn<0 ){
115120 VdbeComment((v,"%s.rowid",pTab->zName));
115123 pTab->zName, pTab->aCol[pCol->iColumn].zCnName));
115124 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
115129 }else if( pExpr->y.pTab==0 ){
115132 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, pExpr->iColumn, target);
115139 int iTab = pExpr->iTable;
115143 ** constraints, and that constant is coded by the pExpr->pLeft
115149 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
115151 assert( pExpr->y.pTab!=0 );
115152 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
115158 &zAff[(aff-'B')*2], P4_STATIC);
115163 if( pParse->iSelfTab<0 ){
115167 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
115173 int iCol = pExpr->iColumn;
115175 pTab = pExpr->y.pTab;
115178 assert( iCol<pTab->nCol );
115180 return -1-pParse->iSelfTab;
115182 pCol = pTab->aCol + iCol;
115184 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
115186 if( pCol->colFlags & COLFLAG_GENERATED ){
115187 if( pCol->colFlags & COLFLAG_BUSY ){
115189 pCol->zCnName);
115192 pCol->colFlags |= COLFLAG_BUSY;
115193 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
115196 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
115200 if( pCol->affinity==SQLITE_AFF_REAL ){
115210 iTab = pParse->iSelfTab - 1;
115213 else if( pParse->pIdxPartExpr
115219 assert( pExpr->y.pTab!=0 );
115220 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
115221 pExpr->iColumn, iTab, target,
115222 pExpr->op2);
115236 codeReal(v, pExpr->u.zToken, 0, target);
115242 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
115250 assert( op==TK_NULL || op==TK_ERROR || pParse->db->mallocFailed );
115260 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
115261 assert( pExpr->u.zToken[1]=='\'' );
115262 z = &pExpr->u.zToken[2];
115263 n = sqlite3Strlen30(z) - 1;
115272 assert( pExpr->u.zToken!=0 );
115273 assert( pExpr->u.zToken[0]!=0 );
115274 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
115278 return pExpr->iTable;
115283 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115287 sqlite3AffinityType(pExpr->u.zToken, 0));
115295 /* fall-through */
115302 Expr *pLeft = pExpr->pLeft;
115307 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115309 codeCompare(pParse, pLeft, pExpr->pRight, op, r1, r2,
115351 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115352 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
115359 Expr *pLeft = pExpr->pLeft;
115361 if( pLeft->op==TK_INTEGER ){
115365 }else if( pLeft->op==TK_FLOAT ){
115367 codeReal(v, pLeft->u.zToken, 1, target);
115376 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
115386 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115394 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115396 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
115397 bNormal = pExpr->op2==TK_IS;
115409 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115419 AggInfo *pInfo = pExpr->pAggInfo;
115421 || NEVER(pExpr->iAgg<0)
115422 || NEVER(pExpr->iAgg>=pInfo->nFunc)
115427 return AggInfoFuncReg(pInfo, pExpr->iAgg);
115438 sqlite3 *db = pParse->db; /* The database connection */
115444 return pExpr->y.pWin->regResult;
115453 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
115457 pFarg = pExpr->x.pList;
115458 nFarg = pFarg ? pFarg->nExpr : 0;
115460 zId = pExpr->u.zToken;
115463 if( pDef==0 && pParse->explain ){
115467 if( pDef==0 || pDef->xFinalize!=0 ){
115471 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)!=0 && ALWAYS(pFarg!=0) ){
115472 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
115473 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
115475 SQLITE_PTR_TO_INT(pDef->pUserData), target);
115476 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
115481 if( i<32 && sqlite3ExprIsConstant(pParse, pFarg->a[i].pExpr) ){
115485 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
115486 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
115491 r1 = pParse->nMem+1;
115492 pParse->nMem += nFarg;
115497 /* For length() and typeof() and octet_length() functions,
115502 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
115505 assert( pFarg->a[0].pExpr!=0 );
115506 exprOp = pFarg->a[0].pExpr->op;
115512 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_LENGTHARG );
115513 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_TYPEOFARG );
115514 testcase( (pDef->funcFlags & OPFLAG_BYTELENARG)==OPFLAG_BYTELENARG);
115515 pFarg->a[0].pExpr->op2 = pDef->funcFlags & OPFLAG_BYTELENARG;
115537 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
115539 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
115542 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
115543 if( !pColl ) pColl = db->pDfltColl;
115547 pDef, pExpr->op2);
115563 if( pParse->db->mallocFailed ){
115567 && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1
115577 Expr *pLeft = pExpr->pLeft;
115578 if( pLeft->iTable==0 || pParse->withinRJSubrtn > pLeft->op2 ){
115579 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft);
115580 pLeft->op2 = pParse->withinRJSubrtn;
115582 assert( pLeft->op==TK_SELECT || pLeft->op==TK_ERROR );
115584 if( pExpr->iTable!=n ){
115586 pExpr->iTable, n);
115588 return pLeft->iTable + pExpr->iColumn;
115611 ** X is stored in pExpr->pLeft.
115612 ** Y is stored in pExpr->pList->a[0].pExpr.
115613 ** Z is stored in pExpr->pList->a[1].pExpr.
115621 /* A TK_COLLATE Expr node without the EP_Collate tag is a so-called
115622 ** "SOFT-COLLATE" that is added to constraints that are pushed down
115623 ** from outer queries into sub-queries by the WHERE-clause push-down
115627 assert( pExpr->pLeft );
115628 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115632 pExpr = pExpr->pLeft;
115633 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. */
115638 pExpr = pExpr->pLeft;
115639 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
115644 ** to a column in the new.* or old.* pseudo-tables available to
115646 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
115647 ** is set to the column of the pseudo-table to read, or to -1 to
115652 ** to reference another column of the old.* pseudo-table, where
115654 ** set to (n+1), where n is the number of columns in each pseudo-table.
115655 ** For a reference to any other column in the new.* pseudo-table, p1
115664 ** p1==0 -> old.rowid p1==3 -> new.rowid
115665 ** p1==1 -> old.a p1==4 -> new.a
115666 ** p1==2 -> old.b p1==5 -> new.b
115673 pTab = pExpr->y.pTab;
115674 iCol = pExpr->iColumn;
115675 p1 = pExpr->iTable * (pTab->nCol+1) + 1
115678 assert( pExpr->iTable==0 || pExpr->iTable==1 );
115679 assert( iCol>=-1 && iCol<pTab->nCol );
115680 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
115681 assert( p1>=0 && p1<(pTab->nCol*2+2) );
115685 (pExpr->iTable ? "new" : "old"),
115686 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zCnName)
115693 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
115695 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
115708 ** that derive from the right-hand table of a LEFT JOIN. The
115709 ** Expr.iTable value is the table number for the right-hand table.
115715 u8 okConstFactor = pParse->okConstFactor;
115716 AggInfo *pAggInfo = pExpr->pAggInfo;
115718 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
115719 if( !pAggInfo->directMode ){
115720 inReg = AggInfoColumnReg(pAggInfo, pExpr->iAgg);
115723 if( pExpr->pAggInfo->useSortingIdx ){
115724 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
115725 pAggInfo->aCol[pExpr->iAgg].iSorterColumn,
115731 addrINR = sqlite3VdbeAddOp3(v, OP_IfNullRow, pExpr->iTable, 0, target);
115739 pParse->okConstFactor = 0; /* note (1) above */
115740 sqlite3ExprCode(pParse, pExpr->pLeft, target);
115742 pParse->okConstFactor = okConstFactor;
115758 ** X (if it exists) is in pExpr->pLeft.
115759 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
115762 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
115779 sqlite3 *db = pParse->db;
115781 assert( ExprUseXList(pExpr) && pExpr->x.pList!=0 );
115782 assert(pExpr->x.pList->nExpr > 0);
115783 pEList = pExpr->x.pList;
115784 aListelem = pEList->a;
115785 nExpr = pEList->nExpr;
115787 if( (pX = pExpr->pLeft)!=0 ){
115789 if( db->mallocFailed ){
115793 testcase( pX->op==TK_COLUMN );
115801 ** The value in regFree1 might get SCopy-ed into the file result.
115806 for(i=0; i<nExpr-1; i=i+2){
115814 testcase( pTest->op==TK_COLUMN );
115816 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
115822 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
115833 assert( pExpr->affExpr==OE_Rollback
115834 || pExpr->affExpr==OE_Abort
115835 || pExpr->affExpr==OE_Fail
115836 || pExpr->affExpr==OE_Ignore
115838 if( !pParse->pTriggerTab && !pParse->nested ){
115840 "RAISE() may only be used within a trigger-program");
115843 if( pExpr->affExpr==OE_Abort ){
115847 if( pExpr->affExpr==OE_Ignore ){
115851 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
115853 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
115854 pExpr->affExpr, r1);
115890 p = pParse->pConstExpr;
115894 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
115895 if( pItem->fg.reusable
115896 && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0
115898 return pItem->u.iConstExprReg;
115902 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
115904 Vdbe *v = pParse->pVdbe;
115908 pParse->okConstFactor = 0;
115909 if( !pParse->db->mallocFailed ){
115910 if( regDest<0 ) regDest = ++pParse->nMem;
115913 pParse->okConstFactor = 1;
115914 sqlite3ExprDelete(pParse->db, pExpr);
115919 struct ExprList_item *pItem = &p->a[p->nExpr-1];
115920 pItem->fg.reusable = regDest<0;
115921 if( regDest<0 ) regDest = ++pParse->nMem;
115922 pItem->u.iConstExprReg = regDest;
115924 pParse->pConstExpr = p;
115947 && pExpr->op!=TK_REGISTER
115951 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
115974 assert( target>0 && target<=pParse->nMem );
115975 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
115976 if( pParse->pVdbe==0 ) return;
115983 && (ExprHasProperty(pX,EP_Subquery) || pX->op==TK_REGISTER)
115989 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
115999 sqlite3 *db = pParse->db;
116001 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
116012 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pParse,pExpr) ){
116024 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
116049 Vdbe *v = pParse->pVdbe;
116052 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
116053 n = pList->nExpr;
116055 for(pItem=pList->a, i=0; i<n; i++, pItem++){
116056 Expr *pExpr = pItem->pExpr;
116058 if( pItem->fg.bSorterRef ){
116059 i--;
116060 n--;
116063 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
116065 i--;
116066 n--;
116068 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
116079 && (pOp=sqlite3VdbeGetLastOp(v))->opcode==OP_Copy
116080 && pOp->p1+pOp->p3+1==inReg
116081 && pOp->p2+pOp->p3+1==target+i
116082 && pOp->p5==0 /* The do-not-merge flag must be clear */
116084 pOp->p3++;
116126 sqlite3 *db = pParse->db;
116133 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
116134 if( db->mallocFailed==0 ){
116140 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
116143 compRight.pRight = pExpr->x.pList->a[1].pExpr;
116153 pDel->flags |= EP_OuterON;
116187 Vdbe *v = pParse->pVdbe;
116197 op = pExpr->op;
116207 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
116209 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
116213 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
116214 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
116220 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
116227 isNot = pExpr->op2==TK_ISNOT;
116228 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
116232 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
116235 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
116253 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
116255 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116256 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
116257 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
116277 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116305 /* No-op */
116330 Vdbe *v = pParse->pVdbe;
116341 /* The value of pExpr->op and op are related as follows:
116343 ** pExpr->op op
116344 ** --------- ----------
116354 ** For other values of pExpr->op, op is undefined and unused.
116359 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
116363 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
116364 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
116365 assert( pExpr->op!=TK_NE || op==OP_Eq );
116366 assert( pExpr->op!=TK_EQ || op==OP_Ne );
116367 assert( pExpr->op!=TK_LT || op==OP_Ge );
116368 assert( pExpr->op!=TK_LE || op==OP_Gt );
116369 assert( pExpr->op!=TK_GT || op==OP_Le );
116370 assert( pExpr->op!=TK_GE || op==OP_Lt );
116372 switch( pExpr->op ){
116378 }else if( pExpr->op==TK_AND ){
116380 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
116381 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
116385 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
116387 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
116394 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
116401 isNot = pExpr->op2==TK_ISNOT;
116402 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
116407 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
116412 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
116419 testcase( pExpr->op==TK_IS );
116420 testcase( pExpr->op==TK_ISNOT );
116421 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
116430 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
116432 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116433 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
116434 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
116452 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
116482 /* no-op */
116503 sqlite3 *db = pParse->db;
116505 if( db->mallocFailed==0 ){
116515 ** If pExpr is a simple SQL value - an integer, real, string, blob
116516 ** or NULL value - then the VDBE currently being prepared is configured
116517 ** to re-prepare each time a new value is bound to variable pVar.
116520 ** same as that currently bound to variable pVar, non-zero is returned.
116536 if( pExpr->op==TK_VARIABLE && pVar->iColumn==pExpr->iColumn ){
116539 if( (pParse->db->flags & SQLITE_EnableQPSG)!=0 ) return 2;
116540 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
116542 iVar = pVar->iColumn;
116543 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
116544 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
116547 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
116561 ** other than the top-level COLLATE operator.
116563 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
116575 ** this routine is used, it does not hurt to get an extra 2 - that
116580 ** terms in pA with bindings in pParse->pReprepare can be matched against
116581 ** literals in pB. The pParse->pVdbe->expmask bitmask is updated for
116594 if( pParse && pA->op==TK_VARIABLE ){
116597 combinedFlags = pA->flags | pB->flags;
116599 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
116604 if( pA->op!=pB->op || pA->op==TK_RAISE ){
116605 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
116608 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
116611 if( pA->op==TK_AGG_COLUMN && pB->op==TK_COLUMN
116612 && pB->iTable<0 && pA->iTable==iTab
116621 if( pA->u.zToken ){
116622 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
116623 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
116625 assert( pA->op==pB->op );
116630 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
116635 }else if( pA->op==TK_NULL ){
116637 }else if( pA->op==TK_COLLATE ){
116638 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
116640 if( pB->u.zToken!=0
116641 && pA->op!=TK_COLUMN
116642 && pA->op!=TK_AGG_COLUMN
116643 && strcmp(pA->u.zToken,pB->u.zToken)!=0
116648 if( (pA->flags & (EP_Distinct|EP_Commuted))
116649 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
116653 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
116654 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
116655 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
116656 if( pA->op!=TK_STRING
116657 && pA->op!=TK_TRUEFALSE
116660 if( pA->iColumn!=pB->iColumn ) return 2;
116661 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
116662 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
116675 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
116678 ** This routine might return non-zero for equivalent ExprLists. The
116684 ** always differs from a non-NULL pointer.
116690 if( pA->nExpr!=pB->nExpr ) return 1;
116691 for(i=0; i<pA->nExpr; i++){
116693 Expr *pExprA = pA->a[i].pExpr;
116694 Expr *pExprB = pB->a[i].pExpr;
116695 if( pA->a[i].fg.sortFlags!=pB->a[i].fg.sortFlags ) return 1;
116702 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
116713 ** Return non-zero if Expr p can only be true if pNN is not NULL.
116715 ** Or if seenNot is true, return non-zero if Expr p can only be
116716 ** non-NULL if pNN is not NULL
116723 int seenNot /* Return true only if p can be any non-NULL value */
116728 return pNN->op!=TK_NULL;
116730 switch( p->op ){
116733 assert( ExprUseXSelect(p) || (p->x.pList!=0 && p->x.pList->nExpr>0) );
116734 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116739 pList = p->x.pList;
116741 assert( pList->nExpr==2 );
116743 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
116744 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
116748 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116768 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
116775 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
116779 if( p->op2!=TK_IS ) return 0;
116780 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116784 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
116796 if( pExpr->op==TK_NULL ) return 1;
116797 if( pExpr->op==TK_TRUEFALSE && sqlite3ExprTruthValue(pExpr)==0 ) return 1;
116815 if( pExpr->op==TK_FUNCTION ){
116816 const char *z = pExpr->u.zToken;
116819 if( pExpr->x.pList==0 ) return 0;
116820 pDef = sqlite3FindFunction(db, z, pExpr->x.pList->nExpr, ENC(db), 0);
116826 if( (pDef->funcFlags & SQLITE_FUNC_INLINE)==0 ) return 0;
116827 if( SQLITE_PTR_TO_INT(pDef->pUserData)!=INLINEFUNC_iif ) return 0;
116828 }else if( pExpr->op==TK_CASE ){
116829 if( pExpr->pLeft!=0 ) return 0;
116833 pList = pExpr->x.pList;
116835 if( pList->nExpr==2 ) return 1;
116836 if( pList->nExpr==3 && sqlite3ExprIsNotTrue(pList->a[2].pExpr) ) return 1;
116859 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
116876 if( pE2->op==TK_OR
116877 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
116878 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
116882 if( pE2->op==TK_NOTNULL
116883 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
116887 if( sqlite3ExprIsIIF(pParse->db, pE1) ){
116888 return sqlite3ExprImpliesExpr(pParse,pE1->x.pList->a[0].pExpr,pE2,iTab);
116894 ** set pWalker->eCode to one only if *both* of the input expressions
116895 ** separately have the implies-not-null-row property.
116898 if( pWalker->eCode==0 ){
116900 if( pWalker->eCode ){
116901 pWalker->eCode = 0;
116909 ** If the expression node requires that the table at pWalker->iCur
116910 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
116912 ** pWalker->mWFlags is non-zero if this inquiry is being undertaking on
116917 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
116918 ** (never setting pWalker->eCode) is a harmless missed optimization.
116921 testcase( pExpr->op==TK_AGG_COLUMN );
116922 testcase( pExpr->op==TK_AGG_FUNCTION );
116924 if( ExprHasProperty(pExpr, EP_InnerON) && pWalker->mWFlags ){
116925 /* If iCur is used in an inner-join ON clause to the left of a
116926 ** RIGHT JOIN, that does *not* mean that the table must be non-null.
116928 ** To keep things simple, any use of iCur from any inner-join is
116932 switch( pExpr->op ){
116941 testcase( pExpr->op==TK_ISNOT );
116942 testcase( pExpr->op==TK_ISNULL );
116943 testcase( pExpr->op==TK_NOTNULL );
116944 testcase( pExpr->op==TK_IS );
116945 testcase( pExpr->op==TK_VECTOR );
116946 testcase( pExpr->op==TK_FUNCTION );
116947 testcase( pExpr->op==TK_TRUTH );
116948 testcase( pExpr->op==TK_CASE );
116952 if( pWalker->u.iCur==pExpr->iTable ){
116953 pWalker->eCode = 1;
116960 /* Both sides of an AND or OR must separately imply non-null-row.
116964 ** If only one of x or y is non-null-row, then the overall expression
116967 testcase( pExpr->op==TK_OR );
116968 testcase( pExpr->op==TK_AND );
116969 bothImplyNotNullRow(pWalker, pExpr->pLeft, pExpr->pRight);
116975 ** the left-hand side of the IN is NULL then the IN itself will be
116977 if( ExprUseXList(pExpr) && ALWAYS(pExpr->x.pList->nExpr>0) ){
116978 sqlite3WalkExpr(pWalker, pExpr->pLeft);
116983 /* In "x NOT BETWEEN y AND z" either x must be non-null-row or else
116984 ** both y and z must be non-null row */
116986 assert( pExpr->x.pList->nExpr==2 );
116987 sqlite3WalkExpr(pWalker, pExpr->pLeft);
116988 bothImplyNotNullRow(pWalker, pExpr->x.pList->a[0].pExpr,
116989 pExpr->x.pList->a[1].pExpr);
117001 Expr *pLeft = pExpr->pLeft;
117002 Expr *pRight = pExpr->pRight;
117003 testcase( pExpr->op==TK_EQ );
117004 testcase( pExpr->op==TK_NE );
117005 testcase( pExpr->op==TK_LT );
117006 testcase( pExpr->op==TK_LE );
117007 testcase( pExpr->op==TK_GT );
117008 testcase( pExpr->op==TK_GE );
117011 assert( pLeft->op!=TK_COLUMN || ExprUseYTab(pLeft) );
117012 assert( pRight->op!=TK_COLUMN || ExprUseYTab(pRight) );
117013 if( (pLeft->op==TK_COLUMN
117014 && ALWAYS(pLeft->y.pTab!=0)
117015 && IsVirtual(pLeft->y.pTab))
117016 || (pRight->op==TK_COLUMN
117017 && ALWAYS(pRight->y.pTab!=0)
117018 && IsVirtual(pRight->y.pTab))
117030 ** Return true (non-zero) if expression p can only be true if at least
117031 ** one column of table iTab is non-null. In other words, return true
117048 ** be non-NULL, then the LEFT JOIN can be safely converted into an
117055 if( p->op==TK_NOTNULL ){
117056 p = p->pLeft;
117058 while( p->op==TK_AND ){
117059 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab, isRJ) ) return 1;
117060 p = p->pRight;
117087 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
117088 ** pWalker->u.pIdxCover->pIdx.
117091 if( pExpr->op==TK_COLUMN
117092 && pExpr->iTable==pWalker->u.pIdxCover->iCur
117093 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
117095 pWalker->eCode = 1;
117147 struct RefSrcList *p = pWalker->u.pRefSrcList;
117148 SrcList *pSrc = pSelect->pSrc;
117151 if( pSrc->nSrc==0 ) return WRC_Continue;
117152 j = p->nExclude;
117153 p->nExclude += pSrc->nSrc;
117154 piNew = sqlite3DbRealloc(p->db, p->aiExclude, p->nExclude*sizeof(int));
117156 p->nExclude = 0;
117159 p->aiExclude = piNew;
117161 for(i=0; i<pSrc->nSrc; i++, j++){
117162 p->aiExclude[j] = pSrc->a[i].iCursor;
117167 struct RefSrcList *p = pWalker->u.pRefSrcList;
117168 SrcList *pSrc = pSelect->pSrc;
117169 if( p->nExclude ){
117170 assert( p->nExclude>=pSrc->nSrc );
117171 p->nExclude -= pSrc->nSrc;
117177 ** Set the 0x01 bit of pWalker->eCode if there is a reference to any
117180 ** Set the 0x02 bit of pWalker->eCode if there is a reference to a
117184 if( pExpr->op==TK_COLUMN
117185 || pExpr->op==TK_AGG_COLUMN
117188 struct RefSrcList *p = pWalker->u.pRefSrcList;
117189 SrcList *pSrc = p->pRef;
117190 int nSrc = pSrc ? pSrc->nSrc : 0;
117192 if( pExpr->iTable==pSrc->a[i].iCursor ){
117193 pWalker->eCode |= 1;
117197 for(i=0; i<p->nExclude && p->aiExclude[i]!=pExpr->iTable; i++){}
117198 if( i>=p->nExclude ){
117199 pWalker->eCode |= 2;
117214 ** -1 pExpr only references no tables at all, or it only
117223 assert( pParse->db!=0 );
117230 x.db = pParse->db;
117232 assert( pExpr->op==TK_AGG_FUNCTION );
117234 sqlite3WalkExprList(&w, pExpr->x.pList);
117235 if( pExpr->pLeft ){
117236 assert( pExpr->pLeft->op==TK_ORDER );
117237 assert( ExprUseXList(pExpr->pLeft) );
117238 assert( pExpr->pLeft->x.pList!=0 );
117239 sqlite3WalkExprList(&w, pExpr->pLeft->x.pList);
117243 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
117246 if( x.aiExclude ) sqlite3DbNNFreeNN(pParse->db, x.aiExclude);
117252 return -1;
117269 && pExpr->pAggInfo!=0
117271 AggInfo *pAggInfo = pExpr->pAggInfo;
117272 int iAgg = pExpr->iAgg;
117273 Parse *pParse = pWalker->pParse;
117274 sqlite3 *db = pParse->db;
117276 if( pExpr->op!=TK_AGG_FUNCTION ){
117277 if( iAgg<pAggInfo->nColumn
117278 && pAggInfo->aCol[iAgg].pCExpr==pExpr
117282 pAggInfo->aCol[iAgg].pCExpr = pExpr;
117286 assert( pExpr->op==TK_AGG_FUNCTION );
117287 if( ALWAYS(iAgg<pAggInfo->nFunc)
117288 && pAggInfo->aFunc[iAgg].pFExpr==pExpr
117292 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
117306 pWalker->pParse = pParse;
117307 pWalker->xExprCallback = agginfoPersistExprCb;
117308 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
117312 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
117317 pInfo->aCol = sqlite3ArrayAllocate(
117319 pInfo->aCol,
117320 sizeof(pInfo->aCol[0]),
117321 &pInfo->nColumn,
117328 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
117333 pInfo->aFunc = sqlite3ArrayAllocate(
117335 pInfo->aFunc,
117336 sizeof(pInfo->aFunc[0]),
117337 &pInfo->nFunc,
117347 ** If no prior entry is found, create a new one and return -1. The
117348 ** new column will have an index of pAggInfo->nColumn-1.
117357 int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
117360 assert( pAggInfo->iFirstReg==0 );
117361 pCol = pAggInfo->aCol;
117362 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
117363 if( pCol->pCExpr==pExpr ) return;
117364 if( pCol->iTable==pExpr->iTable
117365 && pCol->iColumn==pExpr->iColumn
117366 && pExpr->op!=TK_IF_NULL_ROW
117371 k = addAggInfoColumn(pParse->db, pAggInfo);
117374 assert( pParse->db->mallocFailed );
117381 pCol = &pAggInfo->aCol[k];
117383 pCol->pTab = pExpr->y.pTab;
117384 pCol->iTable = pExpr->iTable;
117385 pCol->iColumn = pExpr->iColumn;
117386 pCol->iSorterColumn = -1;
117387 pCol->pCExpr = pExpr;
117388 if( pAggInfo->pGroupBy && pExpr->op!=TK_IF_NULL_ROW ){
117390 ExprList *pGB = pAggInfo->pGroupBy;
117391 struct ExprList_item *pTerm = pGB->a;
117392 n = pGB->nExpr;
117394 Expr *pE = pTerm->pExpr;
117395 if( pE->op==TK_COLUMN
117396 && pE->iTable==pExpr->iTable
117397 && pE->iColumn==pExpr->iColumn
117399 pCol->iSorterColumn = j;
117404 if( pCol->iSorterColumn<0 ){
117405 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
117409 assert( pExpr->pAggInfo==0 || pExpr->pAggInfo==pAggInfo );
117410 pExpr->pAggInfo = pAggInfo;
117411 if( pExpr->op==TK_COLUMN ){
117412 pExpr->op = TK_AGG_COLUMN;
117414 assert( k <= SMXV(pExpr->iAgg) );
117415 pExpr->iAgg = (i16)k;
117425 NameContext *pNC = pWalker->u.pNC;
117426 Parse *pParse = pNC->pParse;
117427 SrcList *pSrcList = pNC->pSrcList;
117428 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
117430 assert( pNC->ncFlags & NC_UAggInfo );
117431 assert( pAggInfo->iFirstReg==0 );
117432 switch( pExpr->op ){
117436 assert( pParse->iSelfTab==0 );
117437 if( (pNC->ncFlags & NC_InAggFunc)==0 ) break;
117438 if( pParse->pIdxEpr==0 ) break;
117439 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
117440 int iDataCur = pIEpr->iDataCur;
117442 if( sqlite3ExprCompare(0, pExpr, pIEpr->pExpr, iDataCur)==0 ) break;
117446 for(i=0; i<pSrcList->nSrc; i++){
117447 if( pSrcList->a[0].iCursor==pIEpr->iDataCur ) break;
117449 if( i>=pSrcList->nSrc ) break;
117450 if( NEVER(pExpr->pAggInfo!=0) ) break; /* Resolved by outer context */
117451 if( pParse->nErr ){ return WRC_Abort; }
117459 tmp.iTable = pIEpr->iIdxCur;
117460 tmp.iColumn = pIEpr->iIdxCol;
117462 if( pParse->nErr ){ return WRC_Abort; }
117463 assert( pAggInfo->aCol!=0 );
117464 assert( tmp.iAgg<pAggInfo->nColumn );
117465 pAggInfo->aCol[tmp.iAgg].pCExpr = pExpr;
117466 pExpr->pAggInfo = pAggInfo;
117467 pExpr->iAgg = tmp.iAgg;
117473 testcase( pExpr->op==TK_AGG_COLUMN );
117474 testcase( pExpr->op==TK_COLUMN );
117475 testcase( pExpr->op==TK_IF_NULL_ROW );
117479 SrcItem *pItem = pSrcList->a;
117480 for(i=0; i<pSrcList->nSrc; i++, pItem++){
117482 if( pExpr->iTable==pItem->iCursor ){
117485 } /* endif pExpr->iTable==pItem->iCursor */
117491 if( (pNC->ncFlags & NC_InAggFunc)==0
117492 && pWalker->walkerDepth==pExpr->op2
117493 && pExpr->pAggInfo==0
117498 struct AggInfo_func *pItem = pAggInfo->aFunc;
117499 int mxTerm = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
117501 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
117502 if( NEVER(pItem->pFExpr==pExpr) ) break;
117503 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
117510 assert( i<pAggInfo->nFunc );
117511 }else if( i>=pAggInfo->nFunc ){
117512 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
117514 u8 enc = ENC(pParse->db);
117515 i = addAggInfoFunc(pParse->db, pAggInfo);
117519 pItem = &pAggInfo->aFunc[i];
117520 pItem->pFExpr = pExpr;
117522 nArg = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
117523 pItem->pFunc = sqlite3FindFunction(pParse->db,
117524 pExpr->u.zToken, nArg, enc, 0);
117525 assert( pItem->bOBUnique==0 );
117526 if( pExpr->pLeft
117527 && (pItem->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)==0
117533 assert( pExpr->pLeft->op==TK_ORDER );
117534 assert( ExprUseXList(pExpr->pLeft) );
117535 pItem->iOBTab = pParse->nTab++;
117536 pOBList = pExpr->pLeft->x.pList;
117537 assert( pOBList->nExpr>0 );
117538 assert( pItem->bOBUnique==0 );
117539 if( pOBList->nExpr==1
117541 && sqlite3ExprCompare(0,pOBList->a[0].pExpr,
117542 pExpr->x.pList->a[0].pExpr,0)==0
117544 pItem->bOBPayload = 0;
117545 pItem->bOBUnique = ExprHasProperty(pExpr, EP_Distinct);
117547 pItem->bOBPayload = 1;
117549 pItem->bUseSubtype =
117550 (pItem->pFunc->funcFlags & SQLITE_SUBTYPE)!=0;
117552 pItem->iOBTab = -1;
117554 if( ExprHasProperty(pExpr, EP_Distinct) && !pItem->bOBUnique ){
117555 pItem->iDistinct = pParse->nTab++;
117557 pItem->iDistinct = -1;
117561 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
117565 assert( i <= SMXV(pExpr->iAgg) );
117566 pExpr->iAgg = (i16)i;
117567 pExpr->pAggInfo = pAggInfo;
117579 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
117594 assert( pNC->pSrcList!=0 );
117608 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
117609 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
117618 if( pParse->nTempReg==0 ){
117619 return ++pParse->nMem;
117621 return pParse->aTempReg[--pParse->nTempReg];
117631 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
117632 pParse->aTempReg[pParse->nTempReg++] = iReg;
117643 i = pParse->iRangeReg;
117644 n = pParse->nRangeReg;
117646 pParse->iRangeReg += nReg;
117647 pParse->nRangeReg -= nReg;
117649 i = pParse->nMem+1;
117650 pParse->nMem += nReg;
117660 if( nReg>pParse->nRangeReg ){
117661 pParse->nRangeReg = nReg;
117662 pParse->iRangeReg = iReg;
117669 ** Always invoke this procedure after coding a subroutine or co-routine
117671 ** the sub/co-routine does not use registers in common with the code that
117672 ** invokes the sub/co-routine.
117675 pParse->nTempReg = 0;
117676 pParse->nRangeReg = 0;
117684 if( pParse->nMem<iReg ) pParse->nMem = iReg;
117695 const ExprList *pList = pParse->pConstExpr;
117698 for(i=0; i<pList->nExpr; i++){
117699 if( pList->a[i].u.iConstExprReg>=iMin ){
117700 iMin = pList->a[i].u.iConstExprReg + 1;
117704 pParse->nTempReg = 0;
117705 pParse->nRangeReg = 0;
117718 if( pParse->nRangeReg>0
117719 && pParse->iRangeReg+pParse->nRangeReg > iFirst
117720 && pParse->iRangeReg <= iLast
117724 for(i=0; i<pParse->nTempReg; i++){
117725 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
117729 if( pParse->pConstExpr ){
117730 ExprList *pList = pParse->pConstExpr;
117731 for(i=0; i<pList->nExpr; i++){
117732 int iReg = pList->a[i].u.iConstExprReg;
117769 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
117774 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
117776 || (pTab->tabFlags & TF_Eponymous)!=0
117777 || ( (pTab->tabFlags & TF_Shadow)!=0
117778 && sqlite3ReadOnlyShadowTables(pParse->db)
117782 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
117802 pParse->colNamesSet = 1;
117826 ** Generate VM code to replace any double-quoted strings (but not double-quoted
117828 ** database zDb with their single-quoted equivalents. If argument bTemp is
117854 Vdbe *v = pParse->pVdbe;
117857 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
117858 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
117874 char *zName = 0; /* NULL-terminated version of pName */
117875 sqlite3 *db = pParse->db; /* Database connection */
117876 int nTabName; /* Number of UTF-8 characters in zTabName */
117879 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
117881 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
117882 assert( pSrc->nSrc==1 );
117883 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
117885 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
117887 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
117888 zDb = db->aDb[iDb].zDbSName;
117918 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
117925 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
117936 if( pVTab->pVtab->pModule->xRename==0 ){
117952 /* figure out how many UTF-8 characters are in zName */
117953 zTabName = pTab->zName;
117954 nTabName = sqlite3Utf8CharLen(zTabName, -1);
117991 zDb, zName, pTab->zName);
118012 ** of any resources used by the v-table implementation (including other
118017 int i = ++pParse->nMem;
118052 ** The Table structure pParse->pNewTable was extended to include
118056 Table *pNew; /* Copy of pParse->pNewTable */
118061 char *zCol; /* Null-terminated column definition */
118068 db = pParse->db;
118069 assert( db->pParse==pParse );
118070 if( pParse->nErr ) return;
118071 assert( db->mallocFailed==0 );
118072 pNew = pParse->pNewTable;
118076 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
118077 zDb = db->aDb[iDb].zDbSName;
118078 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
118079 pCol = &pNew->aCol[pNew->nCol-1];
118086 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
118096 if( pCol->colFlags & COLFLAG_PRIMKEY ){
118100 if( pNew->pIndex ){
118105 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
118110 assert( pDflt==0 || pDflt->op==TK_SPAN );
118111 if( pDflt && pDflt->pLeft->op==TK_NULL ){
118115 if( (db->flags&SQLITE_ForeignKeys) && pNew->u.tab.pFKey && pDflt ){
118117 "Cannot add a REFERENCES column with non-NULL default value");
118119 if( pCol->notNull && !pDflt ){
118134 assert( db->mallocFailed == 1 );
118139 "Cannot add a column with non-constant default");
118143 }else if( pCol->colFlags & COLFLAG_STORED ){
118149 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
118151 char *zEnd = &zCol[pColDef->n-1];
118153 *zEnd-- = '\0';
118162 " || substr(sql,1+length(printf('%%.%ds',sql))) "
118164 zDb, pNew->u.tab.addColOffset, zCol, pNew->u.tab.addColOffset,
118179 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
118189 if( pNew->pCheck!=0
118190 || (pCol->notNull && (pCol->colFlags & COLFLAG_GENERATED)!=0)
118191 || (pTab->tabFlags & TF_Strict)!=0
118196 " WHEN quick_check GLOB 'non-* value in*'"
118203 " OR quick_check GLOB 'non-* value in*'",
118211 ** This function is called by the parser after the table-name in
118212 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
118213 ** pSrc is the full-name of the table being altered.
118231 sqlite3 *db = pParse->db;
118234 assert( pParse->pNewTable==0 );
118236 if( db->mallocFailed ) goto exit_begin_add_column;
118237 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118258 assert( pTab->u.tab.addColOffset>0 );
118259 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
118270 pParse->pNewTable = pNew;
118271 pNew->nTabRef = 1;
118272 pNew->nCol = pTab->nCol;
118273 assert( pNew->nCol>0 );
118274 nAlloc = (((pNew->nCol-1)/8)*8)+8;
118275 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
118276 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*(u32)nAlloc);
118277 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
118278 if( !pNew->aCol || !pNew->zName ){
118279 assert( db->mallocFailed );
118282 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*(size_t)pNew->nCol);
118283 for(i=0; i<pNew->nCol; i++){
118284 Column *pCol = &pNew->aCol[i];
118285 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
118286 pCol->hName = sqlite3StrIHash(pCol->zCnName);
118289 pNew->u.tab.pDfltList = sqlite3ExprListDup(db, pTab->u.tab.pDfltList, 0);
118290 pNew->pSchema = db->aDb[iDb].pSchema;
118291 pNew->u.tab.addColOffset = pTab->u.tab.addColOffset;
118292 assert( pNew->nTabRef==1 );
118303 ** it loads an error message into pParse and returns non-zero.
118323 zType, pTab->zName
118340 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
118344 sqlite3 *db = pParse->db; /* Database connection */
118354 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
118362 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
118364 zDb = db->aDb[iSchema].zDbSName;
118368 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
118383 /* Ensure the schema contains no double-quoted strings */
118394 assert( pNew->n>0 );
118395 bQuote = sqlite3Isquote(pNew->z[0]);
118402 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
118403 pTab->zName
118410 zDb, pTab->zName, iCol, zNew, bQuote
118465 ** rename-token list.
118467 ** 2. Dereferences each pointer in the rename-token list.
118470 ** address-sanitizer or similar. If any of these pointers no longer
118471 ** point to valid objects, an exception is raised by the memory-checking
118485 assert( pParse==pParse->db->pParse );
118486 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
118487 if( pParse->nErr==0 ){
118490 for(p=pParse->pRename; p; p=p->pNext){
118491 if( p->p ){
118492 assert( p->p!=pPtr );
118493 i += *(u8*)(p->p) | 1;
118509 ** in pParse->pRename.
118521 assert( pPtr || pParse->db->mallocFailed );
118523 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
118524 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
118526 pNew->p = pPtr;
118527 pNew->t = *pToken;
118528 pNew->pNext = pParse->pRename;
118529 pParse->pRename = pNew;
118544 for(p=pParse->pRename; p; p=p->pNext){
118545 if( p->p==pFrom ){
118546 p->p = pTo;
118556 Parse *pParse = pWalker->pParse;
118559 sqlite3RenameTokenRemap(pParse, 0, (const void*)&pExpr->y.pTab);
118569 With *pWith = pSelect->pWith;
118571 Parse *pParse = pWalker->pParse;
118574 assert( pWith->nCte>0 );
118575 if( (pWith->a[0].pSelect->selFlags & SF_Expanded)==0 ){
118576 /* Push a copy of the With object onto the with-stack. We use a copy
118578 ** and SF_Resolved) below. And the parser code that uses the with-stack
118581 pCopy = sqlite3WithDup(pParse->db, pWith);
118584 for(i=0; i<pWith->nCte; i++){
118585 Select *p = pWith->a[i].pSelect;
118590 if( sNC.pParse->db->mallocFailed ) return;
118592 sqlite3RenameExprlistUnmap(pParse, pWith->a[i].pCols);
118594 if( pCopy && pParse->pWith==pCopy ){
118595 pParse->pWith = pCopy->pOuter;
118609 for(ii=0; ii<pIdList->nId; ii++){
118610 sqlite3RenameTokenRemap(pParse, 0, (const void*)pIdList->a[ii].zName);
118618 Parse *pParse = pWalker->pParse;
118620 if( pParse->nErr ) return WRC_Abort;
118621 testcase( p->selFlags & SF_View );
118622 testcase( p->selFlags & SF_CopyCte );
118623 if( p->selFlags & (SF_View|SF_CopyCte) ){
118626 if( ALWAYS(p->pEList) ){
118627 ExprList *pList = p->pEList;
118628 for(i=0; i<pList->nExpr; i++){
118629 if( pList->a[i].zEName && pList->a[i].fg.eEName==ENAME_NAME ){
118630 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
118634 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
118635 SrcList *pSrc = p->pSrc;
118636 for(i=0; i<pSrc->nSrc; i++){
118637 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
118638 if( pSrc->a[i].fg.isUsing==0 ){
118639 sqlite3WalkExpr(pWalker, pSrc->a[i].u3.pOn);
118641 unmapColumnIdlistNames(pParse, pSrc->a[i].u3.pUsing);
118654 u8 eMode = pParse->eParseMode;
118660 pParse->eParseMode = PARSE_MODE_UNMAP;
118662 pParse->eParseMode = eMode;
118666 ** Remove all nodes that are part of expression-list pEList from the
118677 for(i=0; i<pEList->nExpr; i++){
118678 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME) ){
118679 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
118692 pNext = p->pNext;
118715 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
118716 if( (*pp)->p==pPtr ){
118719 *pp = pToken->pNext;
118720 pToken->pNext = pCtx->pList;
118721 pCtx->pList = pToken;
118722 pCtx->nList++;
118733 ** descend into sub-select statements.
118736 if( p->selFlags & (SF_View|SF_CopyCte) ){
118737 testcase( p->selFlags & SF_View );
118738 testcase( p->selFlags & SF_CopyCte );
118752 ** constructed in RenameCtx object at pWalker->u.pRename.
118755 RenameCtx *p = pWalker->u.pRename;
118756 if( pExpr->op==TK_TRIGGER
118757 && pExpr->iColumn==p->iCol
118758 && pWalker->pParse->pTriggerTab==p->pTab
118760 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
118761 }else if( pExpr->op==TK_COLUMN
118762 && pExpr->iColumn==p->iCol
118764 && p->pTab==pExpr->y.pTab
118766 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
118781 RenameToken *pBest = pCtx->pList;
118785 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
118786 if( pToken->t.z>pBest->t.z ) pBest = pToken;
118788 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
118789 *pp = pBest->pNext;
118796 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
118798 ** sub-routine is currently stored in pParse->zErrMsg. This function
118812 zErr = sqlite3MPrintf(pParse->db, "error in %s %s%s%s: %s",
118814 pParse->zErrMsg
118816 sqlite3_result_error(pCtx, zErr, -1);
118817 sqlite3DbFree(pParse->db, zErr);
118821 ** For each name in the the expression-list pEList (i.e. each
118822 ** pEList->a[i].zName) that matches the string in zOld, extract the
118823 ** corresponding rename-token from Parse object pParse and add it
118834 for(i=0; i<pEList->nExpr; i++){
118835 const char *zName = pEList->a[i].zEName;
118836 if( ALWAYS(pEList->a[i].fg.eEName==ENAME_NAME)
118847 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
118848 ** that matches the string in zOld, extract the corresponding rename-token
118859 for(i=0; i<pIdList->nId; i++){
118860 const char *zName = pIdList->a[i].zName;
118891 db->init.iDb = 1;
118895 db->init.iDb = (u8)iDb;
118897 p->eParseMode = PARSE_MODE_RENAME;
118898 p->db = db;
118899 p->nQueryLoop = 1;
118900 flags = db->flags;
118901 testcase( (db->flags & SQLITE_Comments)==0 && strstr(zSql," /* ")!=0 );
118902 db->flags |= SQLITE_Comments;
118904 db->flags = flags;
118905 if( db->mallocFailed ) rc = SQLITE_NOMEM;
118907 && NEVER(p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0)
118918 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
118919 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
118924 db->init.iDb = 0;
118964 nQuot = sqlite3Strlen30(zQuot)-1;
118968 zOut = sqlite3DbMallocZero(db, (u64)nSql + pRename->nList*(u64)nQuot + 1);
118978 /* At this point pRename->pList contains a list of RenameToken objects
118980 ** with the new column name, or with single-quoted versions of themselves.
118986 while( pRename->pList ){
118993 if( bQuote==0 && sqlite3IsIdChar(*(u8*)pBest->t.z) ){
118999 if( pBest->t.z[pBest->t.n]=='"' ) nReplace++;
119002 /* Dequote the double-quoted token. Then requote it again, this time
119005 ** add another space after the new, single-quoted version of the
119008 memcpy(zBuf1, pBest->t.z, pBest->t.n);
119009 zBuf1[pBest->t.n] = 0;
119013 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
119019 iOff = (int)(pBest->t.z - zSql);
119020 if( pBest->t.n!=nReplace ){
119021 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
119022 nOut - (iOff + pBest->t.n)
119024 nOut += nReplace - pBest->t.n;
119031 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
119042 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
119048 for(i=0; i<pEList->nExpr; i++){
119049 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
119050 pEList->a[i].fg.eEName = val&0x3;
119056 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
119062 sqlite3 *db = pParse->db;
119063 Trigger *pNew = pParse->pNewTrigger;
119070 assert( pNew->pTabSchema );
119071 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
119072 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
119074 pParse->eTriggerOp = pNew->op;
119077 if( ALWAYS(pParse->pTriggerTab) ){
119078 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab)!=0;
119082 if( rc==SQLITE_OK && pNew->pWhen ){
119083 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
119086 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
119087 if( pStep->pSelect ){
119088 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
119089 if( pParse->nErr ) rc = pParse->rc;
119091 if( rc==SQLITE_OK && pStep->zTarget ){
119095 pParse, pStep->pExprList, pSrc, 0, 0, 0, 0, 0, 0
119098 pStep->pExprList = 0;
119102 /* pStep->pExprList contains an expression-list used for an UPDATE
119106 ** pStep->pExprList to ENAME_SPAN (from their current value of
119110 renameSetENames(pStep->pExprList, ENAME_SPAN);
119112 renameSetENames(pStep->pExprList, ENAME_NAME);
119113 rc = pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
119114 assert( pStep->pExprList==0 || pStep->pExprList==pSel->pEList );
119115 assert( pSrc==pSel->pSrc );
119116 if( pStep->pExprList ) pSel->pEList = 0;
119117 pSel->pSrc = 0;
119120 if( pStep->pFrom ){
119122 for(i=0; i<pStep->pFrom->nSrc && rc==SQLITE_OK; i++){
119123 SrcItem *p = &pStep->pFrom->a[i];
119124 if( p->fg.isSubquery ){
119125 assert( p->u4.pSubq!=0 );
119126 sqlite3SelectPrep(pParse, p->u4.pSubq->pSelect, 0);
119131 if( db->mallocFailed ){
119135 if( rc==SQLITE_OK && pStep->pWhere ){
119136 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
119139 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
119141 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
119142 if( pStep->pUpsert && rc==SQLITE_OK ){
119143 Upsert *pUpsert = pStep->pUpsert;
119144 pUpsert->pUpsertSrc = pSrc;
119147 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
119149 ExprList *pUpsertSet = pUpsert->pUpsertSet;
119153 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
119156 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
119178 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
119181 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
119182 sqlite3WalkSelect(pWalker, pStep->pSelect);
119183 sqlite3WalkExpr(pWalker, pStep->pWhere);
119184 sqlite3WalkExprList(pWalker, pStep->pExprList);
119185 if( pStep->pUpsert ){
119186 Upsert *pUpsert = pStep->pUpsert;
119187 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
119188 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
119189 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
119190 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
119192 if( pStep->pFrom ){
119194 SrcList *pFrom = pStep->pFrom;
119195 for(i=0; i<pFrom->nSrc; i++){
119196 if( pFrom->a[i].fg.isSubquery ){
119197 assert( pFrom->a[i].u4.pSubq!=0 );
119198 sqlite3WalkSelect(pWalker, pFrom->a[i].u4.pSubq->pSelect);
119210 sqlite3 *db = pParse->db;
119212 if( pParse->pVdbe ){
119213 sqlite3VdbeFinalize(pParse->pVdbe);
119215 sqlite3DeleteTable(db, pParse->pNewTable);
119216 while( (pIdx = pParse->pNewIndex)!=0 ){
119217 pParse->pNewIndex = pIdx->pNext;
119220 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
119221 sqlite3DbFree(db, pParse->zErrMsg);
119222 renameTokenFree(db, pParse->pRename);
119238 ** 7. bQuote: Non-zero if the new column name should be quoted.
119242 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
119272 sqlite3_xauth xAuth = db->xAuth;
119282 if( pTab==0 || iCol>=pTab->nCol ){
119286 zOld = pTab->aCol[iCol].zCnName;
119288 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
119291 db->xAuth = 0;
119306 Select *pSelect = sParse.pNewTable->u.view.pSelect;
119307 pSelect->selFlags &= ~(u32)SF_View;
119310 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
119317 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
119321 if( iCol<sParse.pNewTable->nCol ){
119323 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zCnName
119327 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
119329 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
119330 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
119331 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
119333 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
119334 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
119337 for(i=0; i<sParse.pNewTable->nCol; i++){
119339 &sParse.pNewTable->aCol[i]);
119346 for(pFKey=sParse.pNewTable->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
119347 for(i=0; i<pFKey->nCol; i++){
119348 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
119349 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
119351 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
119352 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
119354 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
119360 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
119361 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119368 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
119369 if( pStep->zTarget ){
119370 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
119372 if( pStep->pUpsert ){
119373 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
119376 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
119377 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
119385 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
119409 db->xAuth = xAuth;
119418 RenameCtx *p = pWalker->u.pRename;
119419 if( pExpr->op==TK_COLUMN
119421 && p->pTab==pExpr->y.pTab
119423 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
119433 RenameCtx *p = pWalker->u.pRename;
119434 SrcList *pSrc = pSelect->pSrc;
119435 if( pSelect->selFlags & (SF_View|SF_CopyCte) ){
119436 testcase( pSelect->selFlags & SF_View );
119437 testcase( pSelect->selFlags & SF_CopyCte );
119441 assert( pWalker->pParse->db->mallocFailed );
119444 for(i=0; i<pSrc->nSrc; i++){
119445 SrcItem *pItem = &pSrc->a[i];
119446 if( pItem->pSTab==p->pTab ){
119447 renameTokenFind(pWalker->pParse, p, pItem->zName);
119473 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
119496 sqlite3_xauth xAuth = db->xAuth;
119497 db->xAuth = 0;
119513 int isLegacy = (db->flags & SQLITE_LegacyAlter);
119519 Select *pSelect = pTab->u.view.pSelect;
119524 assert( pSelect->selFlags & SF_View );
119525 pSelect->selFlags &= ~(u32)SF_View;
119526 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
119530 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
119536 if( (isLegacy==0 || (db->flags & SQLITE_ForeignKeys))
119541 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
119542 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
119543 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
119552 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
119555 sqlite3WalkExprList(&sWalker, pTab->pCheck);
119557 renameTokenFind(&sParse, &sCtx, pTab->zName);
119563 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
119565 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119573 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
119574 && sCtx.pTab->pSchema==pTrigger->pTabSchema
119576 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
119583 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
119584 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
119585 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
119587 if( pStep->pFrom ){
119589 for(i=0; i<pStep->pFrom->nSrc; i++){
119590 SrcItem *pItem = &pStep->pFrom->a[i];
119591 if( 0==sqlite3_stricmp(pItem->zName, zOld) ){
119592 renameTokenFind(&sParse, &sCtx, pItem->zName);
119620 db->xAuth = xAuth;
119628 if( pExpr->op==TK_STRING && (pExpr->flags & EP_DblQuoted) ){
119629 renameTokenFind(pWalker->pParse, pWalker->u.pRename, (const void*)pExpr);
119637 ** double-quotes use single quotes instead.
119671 sqlite3_xauth xAuth = db->xAuth;
119672 db->xAuth = 0;
119697 Select *pSelect = sParse.pNewTable->u.view.pSelect;
119698 pSelect->selFlags &= ~(u32)SF_View;
119701 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
119707 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
119709 for(i=0; i<sParse.pNewTable->nCol; i++){
119712 &sParse.pNewTable->aCol[i]));
119717 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
119718 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
119744 db->xAuth = xAuth;
119764 ** 6: True to disable the DQS quirk when parsing SQL.
119783 int isLegacy = (db->flags & SQLITE_LegacyAlter);
119788 sqlite3_xauth xAuth = db->xAuth;
119789 db->xAuth = 0;
119797 u64 flags = db->flags;
119798 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
119800 db->flags = flags;
119806 sqlite3SelectPrep(&sParse, sParse.pNewTable->u.view.pSelect, &sNC);
119815 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
119833 db->xAuth = xAuth;
119842 ** argv[0]: An integer - the index of the schema containing the table
119844 ** argv[2]: An integer - the index of the column to remove.
119858 const char *zDb = db->aDb[iSchema].zDbSName;
119867 sqlite3_xauth xAuth = db->xAuth;
119868 db->xAuth = 0;
119875 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
119881 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zCnName);
119882 if( iCol<pTab->nCol-1 ){
119884 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zCnName);
119885 zEnd = (const char*)pEnd->t.z;
119888 zEnd = (const char*)&zSql[pTab->u.tab.addColOffset];
119889 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
119892 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
119893 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
119899 db->xAuth = xAuth;
119915 sqlite3 *db = pParse->db; /* Database handle */
119920 int iCol; /* Index of column zCol in pTab->aCol[] */
119923 assert( pParse->pNewTable==0 );
119925 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
119926 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
119937 assert( db->mallocFailed );
119948 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
119950 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
119957 if( pTab->nCol<=1 ){
119963 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
119965 zDb = db->aDb[iDb].zDbSName;
119968 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, zCol) ){
119978 , zDb, iDb, iCol, pTab->zName
119986 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
119992 int nField = 0; /* Number of non-virtual columns after drop */
119995 iCur = pParse->nTab++;
119998 reg = ++pParse->nMem;
120001 pParse->nMem += pTab->nCol;
120004 pParse->nMem += pPk->nColumn;
120005 for(i=0; i<pPk->nKeyCol; i++){
120008 nField = pPk->nKeyCol;
120010 regRec = ++pParse->nMem;
120011 for(i=0; i<pTab->nCol; i++){
120012 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
120017 if( iPos<pPk->nKeyCol ) continue;
120018 regOut = reg+1+iPos-(iPos>iColPos);
120022 if( i==pTab->iPKey ){
120025 char aff = pTab->aCol[i].affinity;
120027 pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC;
120030 pTab->aCol[i].affinity = aff;
120037 pParse->nMem++;
120043 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
120059 ** Register built-in functions used to help implement ALTER TABLE
120076 ** 2005-07-08
120126 ** columns. The N-th integer (for N>1) is the average number of rows in
120127 ** the index which have the same value for the first N-1 columns. For
120128 ** a K-column index, there will be K+1 integers in the stat column. If
120152 ** inclusive are samples of the left-most key value in the index taken at
120159 ** For i between 0 and S-1. Conceptually, the index space is divided into
120186 ** of entries in the index whose left-most column exactly matches
120187 ** the left-most column of the sample. The second integer in nEq
120193 ** left-most column is less than the left-most column of the sample.
120194 ** The K-th integer in the nLt entry is the number of index entries
120209 ** looks at the left-most column of the index. The sqlite_stat3.sample
120210 ** column contains the actual value of the left-most column instead
120231 ** appropriate compile-time options are provided.
120260 sqlite3 *db = pParse->db;
120274 pDb = &db->aDb[iDb];
120283 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
120286 ** side-effect of the CREATE TABLE statement is to leave the rootpage
120287 ** of the new table in register pParse->regRoot. This is important
120290 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
120292 assert( pParse->isCreate || pParse->nErr );
120293 aRoot[i] = (u32)pParse->u1.cr.regRoot;
120300 aRoot[i] = pStat->tnum;
120305 pDb->zDbSName, zTab, zWhereType, zWhere
120308 }else if( db->xPreUpdateCallback ){
120309 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
120335 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
120360 int nLimit; /* Analysis row-scan limit */
120363 u8 nSkipAhead; /* Number of times of skip-ahead */
120368 u32 iPrn; /* Pseudo-random number used for sampling */
120383 if( p->nRowid ){
120384 sqlite3DbFree(db, p->u.aRowid);
120385 p->nRowid = 0;
120395 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
120396 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
120397 if( p->u.aRowid ){
120398 p->nRowid = n;
120399 memcpy(p->u.aRowid, pData, n);
120401 p->nRowid = 0;
120411 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
120412 p->nRowid = 0;
120413 p->u.iRowid = iRowid;
120423 pTo->isPSample = pFrom->isPSample;
120424 pTo->iCol = pFrom->iCol;
120425 pTo->iHash = pFrom->iHash;
120426 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
120427 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
120428 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
120429 if( pFrom->nRowid ){
120430 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
120432 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
120443 if( p->mxSample ){
120445 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
120446 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
120447 sampleClear(p->db, &p->current);
120450 sqlite3DbFree(p->db, p);
120459 ** L: A limit on the number of rows to scan, or 0 for no-limit
120517 p->db = db;
120518 p->nEst = sqlite3_value_int64(argv[2]);
120519 p->nRow = 0;
120520 p->nLimit = sqlite3_value_int(argv[3]);
120521 p->nCol = nCol;
120522 p->nKeyCol = nKeyCol;
120523 p->nSkipAhead = 0;
120524 p->current.anDLt = (tRowcnt*)&p[1];
120527 p->current.anEq = &p->current.anDLt[nColUp];
120528 p->mxSample = p->nLimit==0 ? mxSample : 0;
120531 int i; /* Used to iterate through p->aSample[] */
120533 p->iGet = -1;
120534 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
120535 p->current.anLt = &p->current.anEq[nColUp];
120536 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
120539 p->a = (struct StatSample*)&p->current.anLt[nColUp];
120540 p->aBest = &p->a[mxSample];
120541 pSpace = (u8*)(&p->a[mxSample+nCol]);
120543 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120544 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120545 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
120547 assert( (pSpace - (u8*)p)==n );
120550 p->aBest[i].iCol = i;
120575 ** pNew and pOld are both candidate non-periodic samples selected for
120576 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
120583 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
120590 int nCol = pAccum->nCol;
120592 assert( pNew->iCol==pOld->iCol );
120593 for(i=pNew->iCol+1; i<nCol; i++){
120594 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
120595 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
120597 if( pNew->iHash>pOld->iHash ) return 1;
120607 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
120614 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
120615 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
120617 assert( pOld->isPSample==0 && pNew->isPSample==0 );
120618 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
120622 if( pNew->iCol<pOld->iCol ) return 1;
120623 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
120629 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
120630 ** remove the least desirable sample from p->a[] to make room.
120642 if( nEqZero>p->nMaxEqZero ){
120643 p->nMaxEqZero = nEqZero;
120645 if( pNew->isPSample==0 ){
120647 assert( pNew->anEq[pNew->iCol]>0 );
120654 for(i=p->nSample-1; i>=0; i--){
120655 StatSample *pOld = &p->a[i];
120656 if( pOld->anEq[pNew->iCol]==0 ){
120657 if( pOld->isPSample ) return;
120658 assert( pOld->iCol>pNew->iCol );
120666 pUpgrade->iCol = pNew->iCol;
120667 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
120673 if( p->nSample>=p->mxSample ){
120674 StatSample *pMin = &p->a[p->iMin];
120675 tRowcnt *anEq = pMin->anEq;
120676 tRowcnt *anLt = pMin->anLt;
120677 tRowcnt *anDLt = pMin->anDLt;
120678 sampleClear(p->db, pMin);
120679 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
120680 pSample = &p->a[p->nSample-1];
120681 pSample->nRowid = 0;
120682 pSample->anEq = anEq;
120683 pSample->anDLt = anDLt;
120684 pSample->anLt = anLt;
120685 p->nSample = p->mxSample-1;
120688 /* The "rows less-than" for the rowid column must be greater than that
120689 ** for the last sample in the p->a[] array. Otherwise, the samples would
120691 assert( p->nSample==0
120692 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
120695 pSample = &p->a[p->nSample];
120697 p->nSample++;
120700 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
120703 if( p->nSample>=p->mxSample ){
120704 int iMin = -1;
120705 for(i=0; i<p->mxSample; i++){
120706 if( p->a[i].isPSample ) continue;
120707 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
120712 p->iMin = iMin;
120720 ** p->current contains a sample that reflects the previous row of the
120729 for(i=(p->nCol-2); i>=iChng; i--){
120730 StatSample *pBest = &p->aBest[i];
120731 pBest->anEq[i] = p->current.anEq[i];
120732 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
120738 ** p->nMaxEqZero or greater set to zero. */
120739 for(i=p->nSample-1; i>=0; i--){
120741 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
120745 if( iChng<p->nMaxEqZero ){
120746 for(i=p->nSample-1; i>=0; i--){
120748 for(j=iChng; j<p->nCol; j++){
120749 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
120752 p->nMaxEqZero = iChng;
120762 ** C Index of left-most column to differ from previous row
120772 ** if it wants the byte-code to do special processing.
120789 assert( p->nCol>0 );
120790 assert( iChng<p->nCol );
120792 if( p->nRow==0 ){
120795 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
120800 if( p->mxSample ) samplePushPrevious(p, iChng);
120807 p->current.anEq[i]++;
120810 for(i=iChng; i<p->nCol; i++){
120811 p->current.anDLt[i]++;
120813 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
120814 p->current.anEq[i] = 1;
120819 p->nRow++;
120821 if( p->mxSample ){
120824 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
120826 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
120829 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
120831 nLt = p->current.anLt[p->nCol-1];
120833 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
120834 p->current.isPSample = 1;
120835 p->current.iCol = 0;
120836 sampleInsert(p, &p->current, p->nCol-1);
120837 p->current.isPSample = 0;
120841 for(i=0; i<(p->nCol-1); i++){
120842 p->current.iCol = i;
120843 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
120844 sampleCopy(p, &p->aBest[i], &p->current);
120849 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
120850 p->nSkipAhead++;
120851 sqlite3_result_int(context, p->current.anDLt[0]>0);
120889 ** a one-parameter function, stat_get(P), that always returns the
120906 assert( eCall==STAT_GET_STAT1 || p->mxSample );
120931 ** I = (K+D-1)/D
120941 sqlite3StrAccumInit(&sStat, 0, 0, 0, (p->nKeyCol+1)*100);
120943 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
120944 for(i=0; i<p->nKeyCol; i++){
120945 u64 nDistinct = p->current.anDLt[i] + 1;
120946 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
120947 if( iVal==2 && p->nRow*10 <= nDistinct*11 ) iVal = 1;
120950 assert( p->current.anEq[i] || p->nRow==0 );
120957 if( p->iGet<0 ){
120959 p->iGet = 0;
120961 if( p->iGet<p->nSample ){
120962 StatSample *pS = p->a + p->iGet;
120963 if( pS->nRowid==0 ){
120964 sqlite3_result_int64(context, pS->u.iRowid);
120966 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
120975 assert( p->iGet<p->nSample );
120977 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
120978 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
120980 aCnt = p->a[p->iGet].anDLt;
120981 p->iGet++;
120985 sqlite3StrAccumInit(&sStat, 0, 0, 0, p->nCol*100);
120986 for(i=0; i<p->nCol; i++){
120989 if( sStat.nChar ) sStat.nChar--;
121011 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
121024 ** of the k-th column of the pIdx index.
121032 assert( k>=0 && k<pIdx->nColumn );
121033 i = pIdx->aiColumn[k];
121035 VdbeComment((v,"%s.rowid",pIdx->zName));
121037 assert( pIdx->bHasExpr );
121038 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
121040 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zCnName));
121059 sqlite3 *db = pParse->db; /* Database handle */
121065 int jZeroRows = -1; /* Jump from here if number of rows is zero */
121079 int doOnce = 1; /* Flag for a one-time computation */
121095 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
121100 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121104 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
121105 db->aDb[iDb].zDbSName ) ){
121111 if( db->xPreUpdateCallback ){
121114 pStat1->zName = (char*)&pStat1[1];
121115 memcpy(pStat1->zName, "sqlite_stat1", 13);
121116 pStat1->nCol = 3;
121117 pStat1->iPKey = -1;
121118 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNAMIC);
121122 /* Establish a read-lock on the table at the shared-cache level.
121123 ** Open a read-only cursor on the table. Also allocate a cursor number
121126 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
121129 pParse->nTab = MAX(pParse->nTab, iTab);
121131 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
121133 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121141 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
121143 nCol = pIdx->nKeyCol;
121144 zIdxName = pTab->zName;
121145 nColTest = nCol - 1;
121147 nCol = pIdx->nColumn;
121148 zIdxName = pIdx->zName;
121149 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
121154 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
121157 ** Pseudo-code for loop that calls stat_push():
121199 /* Open a read-only cursor on the index being analyzed. */
121200 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
121201 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
121203 VdbeComment((v, "%s", pIdx->zName));
121218 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
121227 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
121256 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
121257 /* For a single-column UNIQUE index, once we have found a non-NULL
121264 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
121284 sqlite3VdbeJumpHere(v, addrNextRow-1);
121307 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
121309 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
121310 for(j=0; j<pPk->nKeyCol; j++){
121311 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
121312 assert( k>=0 && k<pIdx->nColumn );
121316 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
121317 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
121325 if( db->nAnalysisLimit ){
121341 if( pIdx->pPartIdxWhere ){
121342 /* Partial indexes might get a zero-entry in sqlite_stat1. But
121353 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
121359 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
121382 for(pX=pTab->pIndex; pX; pX=pX->pNext){
121385 nColX = pX->nKeyCol;
121387 nColX = pX->nColumn;
121398 ** https://sqlite.org/forum/forumpost/83cb4a95a0 (2023-03-25)
121402 sqlite3ClearTempRegCache(pParse); /* tag-20230325-1 */
121423 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
121437 VdbeComment((v, "%s", pTab->zName));
121447 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
121469 sqlite3 *db = pParse->db;
121470 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
121477 iStatCur = pParse->nTab;
121478 pParse->nTab += 3;
121480 iMem = pParse->nMem+1;
121481 iTab = pParse->nTab;
121483 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
121505 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
121506 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
121508 iStatCur = pParse->nTab;
121509 pParse->nTab += 3;
121511 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
121513 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
121515 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
121523 ** ANALYZE -- 1
121524 ** ANALYZE <database> -- 2
121525 ** ANALYZE ?<database>.?<tablename> -- 3
121532 sqlite3 *db = pParse->db;
121543 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
121551 for(i=0; i<db->nDb; i++){
121555 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
121562 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
121566 analyzeTable(pParse, pIdx->pTable, pIdx);
121574 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
121590 ** The first argument points to a nul-terminated string containing a
121614 v = v*10 + c - '0';
121633 pIndex->bUnordered = 0;
121634 pIndex->noSkipScan = 0;
121637 pIndex->bUnordered = 1;
121638 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
121641 pIndex->szIdxRow = sqlite3LogEst(sz);
121643 pIndex->noSkipScan = 1;
121646 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
121647 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
121662 ** argv[2] = results of analysis - on integer for each column
121679 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
121688 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
121694 int nCol = pIndex->nKeyCol+1;
121699 if( pIndex->aiRowEst==0 ){
121700 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
121701 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
121703 aiRowEst = pIndex->aiRowEst;
121705 pIndex->bUnordered = 0;
121706 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
121707 pIndex->hasStat1 = 1;
121708 if( pIndex->pPartIdxWhere==0 ){
121709 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
121710 pTable->tabFlags |= TF_HasStat1;
121714 fakeIdx.szIdxRow = pTable->szTabRow;
121718 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
121719 pTable->szTabRow = fakeIdx.szIdxRow;
121720 pTable->tabFlags |= TF_HasStat1;
121734 if( pIdx->aSample ){
121736 for(j=0; j<pIdx->nSample; j++){
121737 IndexSample *p = &pIdx->aSample[j];
121738 sqlite3DbFree(db, p->p);
121740 sqlite3DbFree(db, pIdx->aSample);
121742 if( db->pnBytesFreed==0 ){
121743 pIdx->nSample = 0;
121744 pIdx->aSample = 0;
121754 ** Populate the pIdx->aAvgEq[] array based on the samples currently
121755 ** stored in pIdx->aSample[].
121759 IndexSample *aSample = pIdx->aSample;
121760 IndexSample *pFinal = &aSample[pIdx->nSample-1];
121763 if( pIdx->nSampleCol>1 ){
121768 nCol = pIdx->nSampleCol-1;
121769 pIdx->aAvgEq[nCol] = 1;
121772 int nSample = pIdx->nSample;
121780 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
121781 nRow = pFinal->anLt[iCol];
121782 nDist100 = (i64)100 * pFinal->anDLt[iCol];
121783 nSample--;
121785 nRow = pIdx->aiRowEst[0];
121786 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
121788 pIdx->nRowEst0 = nRow;
121795 if( i==(pIdx->nSample-1)
121804 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
121807 pIdx->aAvgEq[iCol] = avgEq;
121851 IndexSample *pSample; /* A slot in pIdx->aSample[] */
121853 assert( db->lookaside.bDisable );
121858 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
121877 assert( pIdx==0 || pIdx->nSample==0 );
121879 if( pIdx->aSample!=0 ){
121883 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
121884 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
121885 nIdxCol = pIdx->nKeyCol;
121887 nIdxCol = pIdx->nColumn;
121889 pIdx->nSampleCol = nIdxCol;
121890 pIdx->mxSample = nSample;
121895 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
121896 if( pIdx->aSample==0 ){
121900 pPtr = (u8*)pIdx->aSample;
121901 pPtr += ROUND8(nSample*sizeof(pIdx->aSample[0]));
121904 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
121905 pIdx->pTable->tabFlags |= TF_HasStat4;
121907 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
121908 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
121909 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
121911 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
121920 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
121933 if( pIdx->nSample>=pIdx->mxSample ){
121940 nCol = pIdx->nSampleCol;
121945 pSample = &pIdx->aSample[pIdx->nSample];
121946 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
121947 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
121948 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
121957 pSample->n = sqlite3_column_bytes(pStmt, 4);
121958 pSample->p = sqlite3DbMallocZero(db, pSample->n + 8);
121959 if( pSample->p==0 ){
121963 if( pSample->n ){
121964 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
121966 pIdx->nSample++;
121981 assert( db->lookaside.bDisable );
122012 ** If an OOM error occurs, this function always sets db->mallocFailed.
122021 Schema *pSchema = db->aDb[iDb].pSchema;
122024 assert( iDb>=0 && iDb<db->nDb );
122025 assert( db->aDb[iDb].pBt!=0 );
122029 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
122031 pTab->tabFlags &= ~TF_HasStat1;
122033 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
122035 pIdx->hasStat1 = 0;
122038 pIdx->aSample = 0;
122044 sInfo.zDatabase = db->aDb[iDb].zDbSName;
122060 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
122062 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
122072 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
122074 sqlite3_free(pIdx->aiRowEst);
122075 pIdx->aiRowEst = 0;
122128 if( pExpr->op!=TK_ID ){
122131 pExpr->op = TK_STRING;
122143 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
122149 ** An SQL user-function registered to do the work of an ATTACH statement. The
122159 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
122160 ** new database, close the database on db->init.iDb and reopen it as an
122188 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
122195 ** from sqlite3_deserialize() to close database db->init.iDb and
122207 pNew = &db->aDb[db->init.iDb];
122208 if( ALWAYS(pNew->pBt) ) sqlite3BtreeClose(pNew->pBt);
122209 pNew->pBt = pNewBt;
122210 pNew->pSchema = pNewSchema;
122226 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
122227 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
122228 db->aLimit[SQLITE_LIMIT_ATTACHED]
122232 for(i=0; i<db->nDb; i++){
122240 /* Allocate the new entry in the db->aDb[] array and initialize the schema
122243 if( db->aDb==db->aDbStatic ){
122244 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
122246 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
122248 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(1+(i64)db->nDb));
122251 db->aDb = aNew;
122252 pNew = &db->aDb[db->nDb];
122259 flags = db->openFlags;
122260 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
122263 sqlite3_result_error(context, zErr, -1);
122267 if( (db->flags & SQLITE_AttachWrite)==0 ){
122270 }else if( (db->flags & SQLITE_AttachCreate)==0 ){
122275 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
122276 db->nDb++;
122277 pNew->zDbSName = sqlite3DbStrDup(db, zName);
122279 db->noSharedCache = 0;
122285 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
122286 if( !pNew->pSchema ){
122288 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
122293 sqlite3BtreeEnter(pNew->pBt);
122294 pPager = sqlite3BtreePager(pNew->pBt);
122295 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
122296 sqlite3BtreeSecureDelete(pNew->pBt,
122297 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
122299 sqlite3BtreeSetPagerFlags(pNew->pBt,
122300 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
122302 sqlite3BtreeLeave(pNew->pBt);
122304 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
122305 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
122312 ** remove the entry from the db->aDb[] array. i.e. put everything back the
122317 db->init.iDb = 0;
122318 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
122320 if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT ){
122322 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt));
122334 int iDb = db->nDb - 1;
122336 if( db->aDb[iDb].pBt ){
122337 sqlite3BtreeClose(db->aDb[iDb].pBt);
122338 db->aDb[iDb].pBt = 0;
122339 db->aDb[iDb].pSchema = 0;
122342 db->nDb = iDb;
122359 sqlite3_result_error(context, zErrDyn, -1);
122366 ** An SQL user-function registered to do the work of an DETACH statement. The
122388 for(i=0; i<db->nDb; i++){
122389 pDb = &db->aDb[i];
122390 if( pDb->pBt==0 ) continue;
122394 if( i>=db->nDb ){
122402 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
122403 || sqlite3BtreeIsInBackup(pDb->pBt)
122411 assert( db->aDb[1].pSchema );
122412 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
122415 if( pTrig->pTabSchema==pDb->pSchema ){
122416 pTrig->pTabSchema = pTrig->pSchema;
122421 sqlite3BtreeClose(pDb->pBt);
122422 pDb->pBt = 0;
122423 pDb->pSchema = 0;
122428 sqlite3_result_error(context, zErr, -1);
122447 sqlite3* db = pParse->db;
122452 if( pParse->nErr ) goto attach_end;
122467 if( pAuthArg->op==TK_STRING ){
122469 zAuthArg = pAuthArg->u.zToken;
122487 assert( v || db->mallocFailed );
122489 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
122490 pFunc->nArg, pFunc, 0);
122549 DbFixer *pFix = p->u.pFix;
122550 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
122551 if( pExpr->op==TK_VARIABLE ){
122552 if( pFix->pParse->db->init.busy ){
122553 pExpr->op = TK_NULL;
122555 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
122566 DbFixer *pFix = p->u.pFix;
122569 sqlite3 *db = pFix->pParse->db;
122570 int iDb = sqlite3FindDbName(db, pFix->zDb);
122571 SrcList *pList = pSelect->pSrc;
122574 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
122575 if( pFix->bTemp==0 && pItem->fg.isSubquery==0 ){
122576 if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
122577 if( iDb!=sqlite3FindDbName(db, pItem->u4.zDatabase) ){
122578 sqlite3ErrorMsg(pFix->pParse,
122580 pFix->zType, pFix->pName, pItem->u4.zDatabase);
122583 sqlite3DbFree(db, pItem->u4.zDatabase);
122584 pItem->fg.notCte = 1;
122585 pItem->fg.hadSchema = 1;
122587 pItem->u4.pSchema = pFix->pSchema;
122588 pItem->fg.fromDDL = 1;
122589 pItem->fg.fixedSchema = 1;
122592 if( pList->a[i].fg.isUsing==0
122593 && sqlite3WalkExpr(&pFix->w, pList->a[i].u3.pOn)
122599 if( pSelect->pWith ){
122600 for(i=0; i<pSelect->pWith->nCte; i++){
122601 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
122620 sqlite3 *db = pParse->db;
122621 assert( db->nDb>iDb );
122622 pFix->pParse = pParse;
122623 pFix->zDb = db->aDb[iDb].zDbSName;
122624 pFix->pSchema = db->aDb[iDb].pSchema;
122625 pFix->zType = zType;
122626 pFix->pName = pName;
122627 pFix->bTemp = (iDb==1);
122628 pFix->w.pParse = pParse;
122629 pFix->w.xExprCallback = fixExprCb;
122630 pFix->w.xSelectCallback = fixSelectCb;
122631 pFix->w.xSelectCallback2 = sqlite3WalkWinDefnDummyCallback;
122632 pFix->w.walkerDepth = 0;
122633 pFix->w.eCode = 0;
122634 pFix->w.u.pFix = pFix;
122648 ** pParse->zErrMsg and these routines return non-zero. If everything
122660 res = sqlite3WalkSelect(&pFix->w, &s);
122669 return sqlite3WalkSelect(&pFix->w, pSelect);
122675 return sqlite3WalkExpr(&pFix->w, pExpr);
122685 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
122686 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
122687 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
122688 || sqlite3FixSrcList(pFix, pStep->pFrom)
122695 for(pUp=pStep->pUpsert; pUp; pUp=pUp->pNextUpsert){
122696 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
122697 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
122698 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
122699 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
122706 pStep = pStep->pNext;
122729 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
122776 ** means that the SQL statement will never-run - the sqlite3_exec() call
122792 sqlite3_mutex_enter(db->mutex);
122793 db->xAuth = (sqlite3_xauth)xAuth;
122794 db->pAuthArg = pArg;
122795 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
122796 sqlite3_mutex_leave(db->mutex);
122801 ** Write an error message into pParse->zErrMsg that explains that the
122802 ** user-supplied authorization function returned an illegal value.
122806 pParse->rc = SQLITE_ERROR;
122824 sqlite3 *db = pParse->db; /* Database handle */
122825 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
122828 if( db->init.busy ) return SQLITE_OK;
122829 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
122832 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
122834 pParse->rc = SQLITE_AUTH;
122858 int iSrc; /* Index in pTabList->a[] of table being read */
122862 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
122864 assert( pParse->db->xAuth!=0 );
122865 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
122872 if( pExpr->op==TK_TRIGGER ){
122873 pTab = pParse->pTriggerTab;
122876 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
122877 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
122878 pTab = pTabList->a[iSrc].pSTab;
122883 iCol = pExpr->iColumn;
122887 assert( iCol<pTab->nCol );
122888 zCol = pTab->aCol[iCol].zCnName;
122889 }else if( pTab->iPKey>=0 ){
122890 assert( pTab->iPKey<pTab->nCol );
122891 zCol = pTab->aCol[pTab->iPKey].zCnName;
122895 assert( iDb>=0 && iDb<pParse->db->nDb );
122896 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
122897 pExpr->op = TK_NULL;
122914 sqlite3 *db = pParse->db;
122920 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
122921 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
122925 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
122926 ** callback are either NULL pointers or zero-terminated strings that
122934 testcase( pParse->zAuthContext==0 );
122936 rc = db->xAuth(db->pAuthArg,code,zArg1,zArg2,zArg3,pParse->zAuthContext);
122939 pParse->rc = SQLITE_AUTH;
122950 ** popped. Or if pParse==0, this routine is a no-op.
122958 pContext->pParse = pParse;
122959 pContext->zAuthContext = pParse->zAuthContext;
122960 pParse->zAuthContext = zContext;
122968 if( pContext->pParse ){
122969 pContext->pParse->zAuthContext = pContext->zAuthContext;
122970 pContext->pParse = 0;
123017 ** Record the fact that we want to lock a table at run-time.
123040 for(i=0; i<pToplevel->nTableLock; i++){
123041 p = &pToplevel->aTableLock[i];
123042 if( p->iDb==iDb && p->iTab==iTab ){
123043 p->isWriteLock = (p->isWriteLock || isWriteLock);
123048 assert( pToplevel->nTableLock < 0x7fff0000 );
123049 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
123050 pToplevel->aTableLock =
123051 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
123052 if( pToplevel->aTableLock ){
123053 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
123054 p->iDb = iDb;
123055 p->iTab = iTab;
123056 p->isWriteLock = isWriteLock;
123057 p->zLockName = zName;
123059 pToplevel->nTableLock = 0;
123060 sqlite3OomFault(pToplevel->db);
123071 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
123081 Vdbe *pVdbe = pParse->pVdbe;
123084 for(i=0; i<pParse->nTableLock; i++){
123085 TableLock *p = &pParse->aTableLock[i];
123086 int p1 = p->iDb;
123087 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
123088 p->zLockName, P4_STATIC);
123096 ** Return TRUE if the given yDbMask object is empty - if it contains no
123123 assert( pParse->pToplevel==0 );
123124 db = pParse->db;
123125 assert( db->pParse==pParse );
123126 if( pParse->nested ) return;
123127 if( pParse->nErr ){
123128 if( db->mallocFailed ) pParse->rc = SQLITE_NOMEM;
123131 assert( db->mallocFailed==0 );
123136 v = pParse->pVdbe;
123138 if( db->init.busy ){
123139 pParse->rc = SQLITE_DONE;
123143 if( v==0 ) pParse->rc = SQLITE_ERROR;
123145 assert( !pParse->isMultiWrite
123146 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
123148 if( pParse->bReturning ){
123153 assert( !pParse->isCreate );
123154 pReturning = pParse->u1.d.pReturning;
123155 if( pReturning->nRetCol ){
123158 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
123160 reg = pReturning->iRetReg;
123161 for(i=0; i<pReturning->nRetCol; i++){
123162 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
123165 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
123178 assert( pParse->nErr>0 || sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
123180 assert( db->nDb>0 );
123184 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
123186 pSchema = db->aDb[iDb].pSchema;
123190 DbMaskTest(pParse->writeMask,iDb), /* P2 */
123191 pSchema->schema_cookie, /* P3 */
123192 pSchema->iGeneration /* P4 */
123194 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
123196 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
123197 }while( ++iDb<db->nDb );
123199 for(i=0; i<pParse->nVtabLock; i++){
123200 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
123203 pParse->nVtabLock = 0;
123208 ** obtain the required table-locks. This is a no-op unless the
123209 ** shared-cache feature is enabled.
123211 if( pParse->nTableLock ) codeTableLocks(pParse);
123216 if( pParse->pAinc ) sqlite3AutoincrementBegin(pParse);
123220 if( pParse->pConstExpr ){
123221 ExprList *pEL = pParse->pConstExpr;
123222 pParse->okConstFactor = 0;
123223 for(i=0; i<pEL->nExpr; i++){
123224 assert( pEL->a[i].u.iConstExprReg>0 );
123225 sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
123229 if( pParse->bReturning ){
123231 assert( !pParse->isCreate );
123232 pRet = pParse->u1.d.pReturning;
123233 if( pRet->nRetCol ){
123234 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
123244 assert( v!=0 || pParse->nErr );
123245 assert( db->mallocFailed==0 || pParse->nErr );
123246 if( pParse->nErr==0 ){
123249 assert( pParse->pAinc==0 || pParse->nTab>0 );
123251 pParse->rc = SQLITE_DONE;
123253 pParse->rc = SQLITE_ERROR;
123266 ** * Built-in SQL functions always take precedence over application-defined
123268 ** built-in function.
123273 sqlite3 *db = pParse->db;
123274 u32 savedDbFlags = db->mDbFlags;
123277 if( pParse->nErr ) return;
123278 if( pParse->eParseMode ) return;
123279 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
123287 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
123288 pParse->nErr++;
123291 pParse->nested++;
123294 db->mDbFlags |= DBFLAG_PreferBuiltin;
123296 db->mDbFlags = savedDbFlags;
123299 pParse->nested--;
123303 ** Locate the in-memory structure that describes a particular database
123321 for(i=0; i<db->nDb; i++){
123322 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
123324 if( i>=db->nDb ){
123333 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
123340 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
123345 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
123352 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
123355 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
123358 for(i=2; i<db->nDb; i++){
123360 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
123365 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, LEGACY_SCHEMA_TABLE);
123367 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
123376 ** Locate the in-memory structure that describes a particular database
123379 ** error message in pParse->zErrMsg.
123382 ** routine leaves an error message in pParse->zErrMsg where
123392 sqlite3 *db = pParse->db;
123396 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
123408 if( (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)==0 && db->init.busy==0 ){
123409 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
123414 testcase( pMod->pEpoTab==0 );
123415 return pMod->pEpoTab;
123420 pParse->checkSchema = 1;
123421 }else if( IsVirtual(p) && (pParse->prepFlags & SQLITE_PREPARE_NO_VTAB)!=0 ){
123433 assert( HasRowid(p) || p->iPKey<0 );
123444 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
123445 ** non-NULL if it is part of a view or trigger program definition. See
123454 if( p->fg.fixedSchema ){
123455 int iDb = sqlite3SchemaToIndex(pParse->db, p->u4.pSchema);
123456 zDb = pParse->db->aDb[iDb].zDbSName;
123458 assert( !p->fg.isSubquery );
123459 zDb = p->u4.zDatabase;
123461 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
123481 ** Locate the in-memory structure that describes
123497 for(i=OMIT_TEMPDB; i<db->nDb; i++){
123499 Schema *pSchema = db->aDb[j].pSchema;
123503 p = sqlite3HashFind(&pSchema->idxHash, zName);
123516 sqlite3ExprDelete(db, p->pPartIdxWhere);
123517 sqlite3ExprListDelete(db, p->aColExpr);
123518 sqlite3DbFree(db, p->zColAff);
123519 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
123521 sqlite3_free(p->aiRowEst);
123537 pHash = &db->aDb[iDb].pSchema->idxHash;
123540 if( pIndex->pTable->pIndex==pIndex ){
123541 pIndex->pTable->pIndex = pIndex->pNext;
123546 p = pIndex->pTable->pIndex;
123547 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
123548 if( ALWAYS(p && p->pNext==pIndex) ){
123549 p->pNext = pIndex->pNext;
123554 db->mDbFlags |= DBFLAG_SchemaChange;
123558 ** Look through the list of open database files in db->aDb[] and if
123560 ** db->aDb[] structure to a smaller size, if possible.
123567 for(i=j=2; i<db->nDb; i++){
123568 struct Db *pDb = &db->aDb[i];
123569 if( pDb->pBt==0 ){
123570 sqlite3DbFree(db, pDb->zDbSName);
123571 pDb->zDbSName = 0;
123575 db->aDb[j] = db->aDb[i];
123579 db->nDb = j;
123580 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
123581 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
123582 sqlite3DbFree(db, db->aDb);
123583 db->aDb = db->aDbStatic;
123589 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
123594 assert( iDb<db->nDb );
123600 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
123603 if( db->nSchemaLock==0 ){
123604 for(i=0; i<db->nDb; i++){
123606 sqlite3SchemaClear(db->aDb[i].pSchema);
123619 for(i=0; i<db->nDb; i++){
123620 Db *pDb = &db->aDb[i];
123621 if( pDb->pSchema ){
123622 if( db->nSchemaLock==0 ){
123623 sqlite3SchemaClear(pDb->pSchema);
123629 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
123632 if( db->nSchemaLock==0 ){
123641 db->mDbFlags &= ~DBFLAG_SchemaChange;
123657 pList = pTab->u.tab.pDfltList;
123658 if( pCol->iDflt==0
123660 || NEVER(pList->nExpr<pCol->iDflt)
123662 pCol->iDflt = pList==0 ? 1 : pList->nExpr+1;
123663 pTab->u.tab.pDfltList = sqlite3ExprListAppend(pParse, pList, pExpr);
123665 sqlite3ExprDelete(pParse->db, pList->a[pCol->iDflt-1].pExpr);
123666 pList->a[pCol->iDflt-1].pExpr = pExpr;
123676 if( pCol->iDflt==0 ) return 0;
123678 if( NEVER(pTab->u.tab.pDfltList==0) ) return 0;
123679 if( NEVER(pTab->u.tab.pDfltList->nExpr<pCol->iDflt) ) return 0;
123680 return pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr;
123695 n = sqlite3Strlen30(pCol->zCnName) + 1;
123696 if( pCol->colFlags & COLFLAG_HASTYPE ){
123697 n += sqlite3Strlen30(pCol->zCnName+n) + 1;
123700 zNew = sqlite3DbRealloc(db, pCol->zCnName, nColl+n);
123702 pCol->zCnName = zNew;
123703 memcpy(pCol->zCnName + n, zColl, nColl);
123704 pCol->colFlags |= COLFLAG_HASCOLL;
123713 if( (pCol->colFlags & COLFLAG_HASCOLL)==0 ) return 0;
123714 z = pCol->zCnName;
123716 if( pCol->colFlags & COLFLAG_HASTYPE ){
123731 if( (pCol = pTable->aCol)!=0 ){
123732 for(i=0; i<pTable->nCol; i++, pCol++){
123733 assert( pCol->zCnName==0 || pCol->hName==sqlite3StrIHash(pCol->zCnName) );
123734 sqlite3DbFree(db, pCol->zCnName);
123736 sqlite3DbNNFreeNN(db, pTable->aCol);
123738 sqlite3ExprListDelete(db, pTable->u.tab.pDfltList);
123740 if( db->pnBytesFreed==0 ){
123741 pTable->aCol = 0;
123742 pTable->nCol = 0;
123744 pTable->u.tab.pDfltList = 0;
123762 ** db parameter can be used with db->pnBytesFreed to measure the memory
123778 if( !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
123784 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
123785 pNext = pIndex->pNext;
123786 assert( pIndex->pSchema==pTable->pSchema
123787 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
123788 if( db->pnBytesFreed==0 && !IsVirtual(pTable) ){
123789 char *zName = pIndex->zName;
123791 &pIndex->pSchema->idxHash, zName, 0
123793 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
123809 sqlite3SelectDelete(db, pTable->u.view.pSelect);
123815 sqlite3DbFree(db, pTable->zName);
123816 sqlite3DbFree(db, pTable->zColAff);
123817 sqlite3ExprListDelete(db, pTable->pCheck);
123827 if( db->pnBytesFreed==0 && (--pTable->nTabRef)>0 ) return;
123844 assert( iDb>=0 && iDb<db->nDb );
123847 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
123848 pDb = &db->aDb[iDb];
123849 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
123851 db->mDbFlags |= DBFLAG_SchemaChange;
123870 zName = sqlite3DbStrNDup(db, (const char*)pName->z, pName->n);
123886 if( p->nTab==0 ){
123887 p->nTab = 1;
123892 ** Parameter zName points to a nul-terminated buffer containing the name
123894 ** function returns the index of the named database in db->aDb[], or
123895 ** -1 if the named db cannot be found.
123898 int i = -1; /* Database number */
123901 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
123902 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
123914 ** index of the named database in db->aDb[], or -1 if the named db
123949 sqlite3 *db = pParse->db;
123952 if( pName2->n>0 ){
123953 if( db->init.busy ) {
123955 return -1;
123961 return -1;
123964 assert( db->init.iDb==0 || db->init.busy || IN_SPECIAL_PARSE
123965 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
123966 iDb = db->init.iDb;
123976 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
123977 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123979 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123981 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
123983 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
123987 ** This routine is used to check if the UTF-8 string zName is a legal
124003 sqlite3 *db = pParse->db;
124005 || db->init.imposterTable
124011 if( db->init.busy ){
124012 if( sqlite3_stricmp(zType, db->init.azInit[0])
124013 || sqlite3_stricmp(zName, db->init.azInit[1])
124014 || sqlite3_stricmp(zTblName, db->init.azInit[2])
124020 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
124037 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
124044 ** find the (first) offset of that column in index pIdx. Or return -1
124050 assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN );
124051 assert( pIdx->nColumn<=SQLITE_MAX_COLUMN+1 );
124053 for(i=0; i<pIdx->nColumn; i++){
124054 if( iCol16==pIdx->aiColumn[i] ){
124058 return -1;
124071 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
124074 if( pTab->tabFlags & TF_HasVirtual ){
124077 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
124089 ** the N-th virtual column (zero-based) then the storage number is
124090 ** the number of non-virtual columns in the table plus N.
124106 ** -- 0 1 2 3 4 5 6 7 8
124117 ** this routine is a no-op macro. If the pTab does not have any virtual
124118 ** columns, then this routine is no-op that always return iCol. If iCol
124124 assert( iCol<pTab->nCol );
124125 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
124127 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
124129 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
124131 return pTab->nNVCol + i - n;
124145 ** is a read-only no-op.
124148 int iReg = ++pParse->nMem;
124166 ** The new table record is initialized and put in pParse->pNewTable.
124183 sqlite3 *db = pParse->db;
124188 if( db->init.busy && db->init.newTnum==1 ){
124190 iDb = db->init.iDb;
124197 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
124209 pParse->sNameToken = *pName;
124214 if( db->init.iDb==1 ) isTemp = 1;
124225 char *zDb = db->aDb[iDb].zDbSName;
124244 char *zDb = db->aDb[iDb].zDbSName;
124254 assert( !db->init.busy || CORRUPT_DB );
124268 assert( db->mallocFailed );
124269 pParse->rc = SQLITE_NOMEM_BKPT;
124270 pParse->nErr++;
124273 pTable->zName = zName;
124274 pTable->iPKey = -1;
124275 pTable->pSchema = db->aDb[iDb].pSchema;
124276 pTable->nTabRef = 1;
124278 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
124280 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
124282 assert( pParse->pNewTable==0 );
124283 pParse->pNewTable = pTable;
124293 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
124310 assert( pParse->isCreate );
124311 reg1 = pParse->u1.cr.regRowid = ++pParse->nMem;
124312 reg2 = pParse->u1.cr.regRoot = ++pParse->nMem;
124313 reg3 = ++pParse->nMem;
124317 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
124323 /* This just creates a place-holder record in the sqlite_schema table.
124327 ** The rowid for the new entry is left in register pParse->u1.cr.regRowid.
124328 ** The root page of the new table is left in reg pParse->u1.cr.regRoot.
124338 assert( !pParse->bReturning );
124339 pParse->u1.cr.addrCrTab =
124350 /* Normal (non-error) return. */
124355 pParse->checkSchema = 1;
124365 if( sqlite3_strnicmp(pCol->zCnName, "__hidden__", 10)==0 ){
124366 pCol->colFlags |= COLFLAG_HIDDEN;
124367 if( pTab ) pTab->tabFlags |= TF_HasHidden;
124368 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
124369 pTab->tabFlags |= TF_OOOHidden;
124380 pHash = &(db->aDb[1].pSchema->trigHash);
124381 sqlite3HashInsert(pHash, pRet->zName, 0);
124382 sqlite3ExprListDelete(db, pRet->pReturnEL);
124405 sqlite3 *db = pParse->db;
124406 if( pParse->pNewTrigger ){
124409 assert( pParse->bReturning==0 || pParse->ifNotExists );
124411 pParse->bReturning = 1;
124417 assert( !pParse->isCreate );
124418 pParse->u1.d.pReturning = pRet;
124419 pRet->pParse = pParse;
124420 pRet->pReturnEL = pList;
124422 testcase( pParse->earlyCleanup );
124423 if( db->mallocFailed ) return;
124424 sqlite3_snprintf(sizeof(pRet->zName), pRet->zName,
124426 pRet->retTrig.zName = pRet->zName;
124427 pRet->retTrig.op = TK_RETURNING;
124428 pRet->retTrig.tr_tm = TRIGGER_AFTER;
124429 pRet->retTrig.bReturning = 1;
124430 pRet->retTrig.pSchema = db->aDb[1].pSchema;
124431 pRet->retTrig.pTabSchema = db->aDb[1].pSchema;
124432 pRet->retTrig.step_list = &pRet->retTStep;
124433 pRet->retTStep.op = TK_RETURNING;
124434 pRet->retTStep.pTrig = &pRet->retTrig;
124435 pRet->retTStep.pExprList = pList;
124436 pHash = &(db->aDb[1].pSchema->trigHash);
124437 assert( sqlite3HashFind(pHash, pRet->zName)==0
124438 || pParse->nErr || pParse->ifNotExists );
124439 if( sqlite3HashInsert(pHash, pRet->zName, &pRet->retTrig)
124440 ==&pRet->retTrig ){
124459 sqlite3 *db = pParse->db;
124465 if( (p = pParse->pNewTable)==0 ) return;
124466 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
124467 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
124477 && sqlite3_strnicmp(sType.z+(sType.n-6),"always",6)==0
124479 sType.n -= 6;
124480 while( ALWAYS(sType.n>0) && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
124482 && sqlite3_strnicmp(sType.z+(sType.n-9),"generated",9)==0
124484 sType.n -= 9;
124485 while( sType.n>0 && sqlite3Isspace(sType.z[sType.n-1]) ) sType.n--;
124513 if( p->nCol && sqlite3ColumnIndex(p, z)>=0 ){
124518 aNew = sqlite3DbRealloc(db,p->aCol,((i64)p->nCol+1)*sizeof(p->aCol[0]));
124523 p->aCol = aNew;
124524 pCol = &p->aCol[p->nCol];
124525 memset(pCol, 0, sizeof(p->aCol[0]));
124526 pCol->zCnName = z;
124527 pCol->hName = sqlite3StrIHash(z);
124533 pCol->affinity = affinity;
124534 pCol->eCType = eType;
124535 pCol->szEst = szEst;
124539 pCol->colFlags |= COLFLAG_SORTERREF;
124548 pCol->affinity = sqlite3AffinityType(zType, pCol);
124549 pCol->colFlags |= COLFLAG_HASTYPE;
124551 if( p->nCol<=0xff ){
124552 u8 h = pCol->hName % sizeof(p->aHx);
124553 p->aHx[h] = p->nCol;
124555 p->nCol++;
124556 p->nNVCol++;
124557 assert( pParse->isCreate );
124558 pParse->u1.cr.constraintName.n = 0;
124570 p = pParse->pNewTable;
124571 if( p==0 || NEVER(p->nCol<1) ) return;
124572 pCol = &p->aCol[p->nCol-1];
124573 pCol->notNull = (u8)onError;
124574 p->tabFlags |= TF_HasNotNull;
124578 if( pCol->colFlags & COLFLAG_UNIQUE ){
124580 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124581 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
124582 if( pIdx->aiColumn[0]==p->nCol-1 ){
124583 pIdx->uniqNotNull = 1;
124590 ** Scan the column type name zType (length nType) and return the
124593 ** This routine does a case-independent search of zType for the
124601 ** --------------------------------
124660 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
124667 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
124672 pCol->colFlags |= COLFLAG_SORTERREF;
124677 pCol->szEst = v;
124700 sqlite3 *db = pParse->db;
124701 p = pParse->pNewTable;
124703 int isInit = db->init.busy && db->init.iDb!=1;
124704 pCol = &(p->aCol[p->nCol-1]);
124707 pCol->zCnName);
124709 }else if( pCol->colFlags & COLFLAG_GENERATED ){
124710 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
124711 testcase( pCol->colFlags & COLFLAG_STORED );
124752 if( p->op==TK_STRING ){
124753 p->op = TK_ID;
124754 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
124755 p->pLeft->op = TK_ID;
124763 pCol->colFlags |= COLFLAG_PRIMKEY;
124765 if( pCol->colFlags & COLFLAG_GENERATED ){
124766 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
124767 testcase( pCol->colFlags & COLFLAG_STORED );
124786 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
124799 Table *pTab = pParse->pNewTable;
124801 int iCol = -1, i;
124804 if( pTab->tabFlags & TF_HasPrimaryKey ){
124806 "table \"%s\" has more than one primary key", pTab->zName);
124809 pTab->tabFlags |= TF_HasPrimaryKey;
124811 iCol = pTab->nCol - 1;
124812 pCol = &pTab->aCol[iCol];
124816 nTerm = pList->nExpr;
124818 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
124821 if( pCExpr->op==TK_ID ){
124823 iCol = sqlite3ColumnIndex(pTab, pCExpr->u.zToken);
124825 pCol = &pTab->aCol[iCol];
124833 && pCol->eCType==COLTYPE_INTEGER
124837 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
124838 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
124840 pTab->iPKey = iCol;
124841 pTab->keyConf = (u8)onError;
124843 pTab->tabFlags |= autoInc*TF_Autoincrement;
124844 if( pList ) pParse->iPkSortOrder = pList->a[0].fg.sortFlags;
124858 sqlite3ExprListDelete(pParse->db, pList);
124872 Table *pTab = pParse->pNewTable;
124873 sqlite3 *db = pParse->db;
124875 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
124877 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
124878 assert( pParse->isCreate );
124879 if( pParse->u1.cr.constraintName.n ){
124880 sqlite3ExprListSetName(pParse, pTab->pCheck,
124881 &pParse->u1.cr.constraintName, 1);
124885 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
124887 t.n = (int)(zEnd - t.z);
124888 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
124893 sqlite3ExprDelete(pParse->db, pCheckExpr);
124907 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
124908 i = p->nCol-1;
124909 db = pParse->db;
124915 sqlite3ColumnSetColl(db, &p->aCol[i], zColl);
124921 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
124922 assert( pIdx->nKeyCol==1 );
124923 if( pIdx->aiColumn[0]==i ){
124924 pIdx->azColl[0] = sqlite3ColumnColl(&p->aCol[i]);
124937 Table *pTab = pParse->pNewTable;
124943 pCol = &(pTab->aCol[pTab->nCol-1]);
124948 if( pCol->iDflt>0 ) goto generated_error;
124950 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
124951 /* no-op */
124952 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
124958 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
124959 pCol->colFlags |= eType;
124962 pTab->tabFlags |= eType;
124963 if( pCol->colFlags & COLFLAG_PRIMKEY ){
124966 if( ALWAYS(pExpr) && pExpr->op==TK_ID ){
124973 if( pExpr && pExpr->op!=TK_RAISE ) pExpr->affExpr = pCol->affinity;
124980 pCol->zCnName);
124982 sqlite3ExprDelete(pParse->db, pExpr);
124985 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
124987 sqlite3ExprDelete(pParse->db, pExpr);
125001 ** This plan is not completely bullet-proof. It is possible for
125007 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
125008 ** the schema-version whenever the schema changes.
125011 sqlite3 *db = pParse->db;
125012 Vdbe *v = pParse->pVdbe;
125015 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
125038 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
125045 ** it is quoted using double-quotes.
125082 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
125083 n += identLength(pCol->zCnName) + 5;
125085 n += identLength(p->zName);
125095 n += 35 + 6*p->nCol;
125104 identPut(zStmt, &k, p->zName);
125106 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
125122 identPut(zStmt, &k, pCol->zCnName);
125124 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
125125 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
125126 testcase( pCol->affinity==SQLITE_AFF_BLOB );
125127 testcase( pCol->affinity==SQLITE_AFF_TEXT );
125128 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
125129 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
125130 testcase( pCol->affinity==SQLITE_AFF_REAL );
125131 testcase( pCol->affinity==SQLITE_AFF_FLEXNUM );
125133 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
125135 assert( pCol->affinity==SQLITE_AFF_BLOB
125136 || pCol->affinity==SQLITE_AFF_FLEXNUM
125137 || pCol->affinity==sqlite3AffinityType(zType, 0) );
125157 if( pIdx->nColumn>=N ) return SQLITE_OK;
125158 db = pParse->db;
125160 assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ );
125161 testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] );
125162 assert( pIdx->isResized==0 );
125166 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
125167 pIdx->azColl = (const char**)zExtra;
125169 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
125170 pIdx->aiRowLogEst = (LogEst*)zExtra;
125172 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
125173 pIdx->aiColumn = (i16*)zExtra;
125175 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
125176 pIdx->aSortOrder = (u8*)zExtra;
125177 pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */
125178 pIdx->isResized = 1;
125189 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
125190 wTable += pTabCol->szEst;
125192 if( pTab->iPKey<0 ) wTable++;
125193 pTab->szTabRow = sqlite3LogEst(wTable*4);
125202 const Column *aCol = pIdx->pTable->aCol;
125203 for(i=0; i<pIdx->nColumn; i++){
125204 i16 x = pIdx->aiColumn[i];
125205 assert( x<pIdx->pTable->nCol );
125208 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
125216 while( nCol-- > 0 ){
125226 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
125239 assert( nKey<=pIdx->nColumn );
125240 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
125241 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
125242 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
125243 assert( pPk->pTable==pIdx->pTable );
125245 j = pPk->aiColumn[iCol];
125248 assert( pIdx->aiColumn[i]>=0 || j>=0 );
125249 if( pIdx->aiColumn[i]==j
125250 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
125263 ** high-order bit of colNotIdxed is always 1. All unindexed columns
125266 ** 2019-10-24: For the purpose of this computation, virtual columns are
125274 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
125280 Table *pTab = pIdx->pTable;
125281 for(j=pIdx->nColumn-1; j>=0; j--){
125282 int x = pIdx->aiColumn[j];
125283 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
125284 testcase( x==BMS-1 );
125285 testcase( x==BMS-2 );
125286 if( x<BMS-1 ) m |= MASKBIT(x);
125289 pIdx->colNotIdxed = ~m;
125290 assert( (pIdx->colNotIdxed>>63)==1 ); /* See note-20221022-a */
125323 sqlite3 *db = pParse->db;
125324 Vdbe *v = pParse->pVdbe;
125328 if( !db->init.imposterTable ){
125329 for(i=0; i<pTab->nCol; i++){
125330 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
125331 && (pTab->aCol[i].notNull==OE_None)
125333 pTab->aCol[i].notNull = OE_Abort;
125336 pTab->tabFlags |= TF_HasNotNull;
125342 assert( !pParse->bReturning );
125343 if( pParse->u1.cr.addrCrTab ){
125345 sqlite3VdbeChangeP3(v, pParse->u1.cr.addrCrTab, BTREE_BLOBKEY);
125351 if( pTab->iPKey>=0 ){
125354 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zCnName);
125358 pTab->tabFlags &= ~TF_WithoutRowid;
125362 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
125364 pList->a[0].fg.sortFlags = pParse->iPkSortOrder;
125365 assert( pParse->pNewTable==pTab );
125366 pTab->iPKey = -1;
125367 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
125369 if( pParse->nErr ){
125370 pTab->tabFlags &= ~TF_WithoutRowid;
125373 assert( db->mallocFailed==0 );
125375 assert( pPk->nKeyCol==1 );
125385 for(i=j=1; i<pPk->nKeyCol; i++){
125387 pPk->nColumn--;
125389 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
125390 pPk->azColl[j] = pPk->azColl[i];
125391 pPk->aSortOrder[j] = pPk->aSortOrder[i];
125392 pPk->aiColumn[j++] = pPk->aiColumn[i];
125395 pPk->nKeyCol = j;
125398 pPk->isCovering = 1;
125399 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
125400 nPk = pPk->nColumn = pPk->nKeyCol;
125406 if( v && pPk->tnum>0 ){
125407 assert( db->init.busy==0 );
125408 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
125412 pPk->tnum = pTab->tnum;
125414 /* Update the in-memory representation of all UNIQUE indices by converting
125417 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
125421 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
125422 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
125428 pIdx->nColumn = pIdx->nKeyCol;
125431 if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return;
125432 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
125433 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
125434 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
125435 pIdx->aiColumn[j] = pPk->aiColumn[i];
125436 pIdx->azColl[j] = pPk->azColl[i];
125437 if( pPk->aSortOrder[i] ){
125439 pIdx->bAscKeyBug = 1;
125444 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
125445 assert( pIdx->nColumn>=j );
125451 for(i=0; i<pTab->nCol; i++){
125452 if( !hasColumn(pPk->aiColumn, nPk, i)
125453 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
125456 for(i=0, j=nPk; i<pTab->nCol; i++){
125457 if( !hasColumn(pPk->aiColumn, j, i)
125458 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
125460 assert( j<pPk->nColumn );
125461 pPk->aiColumn[j] = i;
125462 pPk->azColl[j] = sqlite3StrBINARY;
125466 assert( pPk->nColumn==j );
125467 assert( pTab->nNVCol<=j );
125478 int nName; /* Length of zName */
125482 nName = sqlite3Strlen30(pTab->zName);
125483 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
125485 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
125487 if( pMod->pModule->iVersion<3 ) return 0;
125488 if( pMod->pModule->xShadowName==0 ) return 0;
125489 return pMod->pModule->xShadowName(zName+nName+1);
125501 int nName; /* Length of pTab->zName */
125506 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->u.vtab.azArg[0]);
125508 if( NEVER(pMod->pModule==0) ) return;
125509 if( pMod->pModule->iVersion<3 ) return;
125510 if( pMod->pModule->xShadowName==0 ) return;
125511 assert( pTab->zName!=0 );
125512 nName = sqlite3Strlen30(pTab->zName);
125513 for(k=sqliteHashFirst(&pTab->pSchema->tblHash); k; k=sqliteHashNext(k)){
125515 assert( pOther->zName!=0 );
125517 if( pOther->tabFlags & TF_Shadow ) continue;
125518 if( sqlite3StrNICmp(pOther->zName, pTab->zName, nName)==0
125519 && pOther->zName[nName]=='_'
125520 && pMod->pModule->xShadowName(pOther->zName+nName+1)
125522 pOther->tabFlags |= TF_Shadow;
125574 #define markExprListImmutable(X) /* no-op */
125587 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
125606 sqlite3 *db = pParse->db; /* The database connection */
125613 p = pParse->pNewTable;
125616 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
125617 p->tabFlags |= TF_Shadow;
125620 /* If the db->init.busy is 1 it means we are reading the SQL off the
125623 ** for the table from the db->init.newTnum field. (The page number
125627 ** table itself. So mark it read-only.
125629 if( db->init.busy ){
125630 if( pSelect || (!IsOrdinaryTable(p) && db->init.newTnum) ){
125634 p->tnum = db->init.newTnum;
125635 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
125649 p->tabFlags |= TF_Strict;
125650 for(ii=0; ii<p->nCol; ii++){
125651 Column *pCol = &p->aCol[ii];
125652 if( pCol->eCType==COLTYPE_CUSTOM ){
125653 if( pCol->colFlags & COLFLAG_HASTYPE ){
125656 p->zName, pCol->zCnName, sqlite3ColumnType(pCol, "")
125660 p->zName, pCol->zCnName);
125663 }else if( pCol->eCType==COLTYPE_ANY ){
125664 pCol->affinity = SQLITE_AFF_BLOB;
125666 if( (pCol->colFlags & COLFLAG_PRIMKEY)!=0
125667 && p->iPKey!=ii
125668 && pCol->notNull == OE_None
125670 pCol->notNull = OE_Abort;
125671 p->tabFlags |= TF_HasNotNull;
125676 assert( (p->tabFlags & TF_HasPrimaryKey)==0
125677 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
125678 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
125679 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
125683 if( (p->tabFlags & TF_Autoincrement) ){
125688 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
125689 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
125692 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
125695 iDb = sqlite3SchemaToIndex(db, p->pSchema);
125700 if( p->pCheck ){
125701 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
125702 if( pParse->nErr ){
125705 sqlite3ExprListDelete(db, p->pCheck);
125706 p->pCheck = 0;
125708 markExprListImmutable(p->pCheck);
125713 if( p->tabFlags & TF_HasGenerated ){
125715 testcase( p->tabFlags & TF_HasVirtual );
125716 testcase( p->tabFlags & TF_HasStored );
125717 for(ii=0; ii<p->nCol; ii++){
125718 u32 colFlags = p->aCol[ii].colFlags;
125720 Expr *pX = sqlite3ColumnExpr(p, &p->aCol[ii]);
125730 sqlite3ColumnSetExpr(pParse, p, &p->aCol[ii],
125738 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
125746 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
125756 if( !db->init.busy ){
125784 ** statement to populate the new table. The root-page number for the
125785 ** new table is in register pParse->u1.cr.regRoot.
125791 ** A shared-cache write-lock is not required to write to the new table,
125792 ** as a schema-lock must have already been obtained to create it. Since
125793 ** a schema-lock excludes all other database users, the write-lock would
125798 int regYield; /* Register holding co-routine entry-point */
125799 int addrTop; /* Top of the co-routine */
125807 pParse->rc = SQLITE_ERROR;
125808 pParse->nErr++;
125811 iCsr = pParse->nTab++;
125812 regYield = ++pParse->nMem;
125813 regRec = ++pParse->nMem;
125814 regRowid = ++pParse->nMem;
125816 assert( pParse->isCreate );
125817 sqlite3VdbeAddOp3(v, OP_OpenWrite, iCsr, pParse->u1.cr.regRoot, iDb);
125821 if( pParse->nErr ) return;
125824 assert( p->aCol==0 );
125825 p->nCol = p->nNVCol = pSelTab->nCol;
125826 p->aCol = pSelTab->aCol;
125827 pSelTab->nCol = 0;
125828 pSelTab->aCol = 0;
125832 if( pParse->nErr ) return;
125834 sqlite3VdbeJumpHere(v, addrTop - 1);
125850 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
125851 n = (int)(pEnd2->z - pParse->sNameToken.z);
125852 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
125854 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
125862 assert( pParse->isCreate );
125867 db->aDb[iDb].zDbSName,
125869 p->zName,
125870 p->zName,
125871 pParse->u1.cr.regRoot,
125873 pParse->u1.cr.regRowid
125882 if( (p->tabFlags & TF_Autoincrement)!=0 && !IN_SPECIAL_PARSE ){
125883 Db *pDb = &db->aDb[iDb];
125885 if( pDb->pSchema->pSeqTab==0 ){
125888 pDb->zDbSName
125896 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
125900 if( p->tabFlags & TF_HasGenerated ){
125903 db->aDb[iDb].zDbSName, p->zName), P4_DYNAMIC);
125907 /* Add the table to the in-memory representation of the database.
125909 if( db->init.busy ){
125911 Schema *pSchema = p->pSchema;
125913 assert( HasRowid(p) || p->iPKey<0 );
125914 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
125920 pParse->pNewTable = 0;
125921 db->mDbFlags |= DBFLAG_SchemaChange;
125926 assert( !pParse->nested );
125928 if( strcmp(p->zName, "sqlite_sequence")==0 ){
125930 p->pSchema->pSeqTab = p;
125938 if( pCons->z==0 ){
125941 p->u.tab.addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
125967 sqlite3 *db = pParse->db;
125969 if( pParse->nVar>0 ){
125974 p = pParse->pNewTable;
125975 if( p==0 || pParse->nErr ) goto create_view_fail;
125980 ** with -DSQLITE_ALLOW_ROWID_IN_VIEW in case there are legacy apps that
125984 p->tabFlags |= sqlite3Config.mNoVisibleRowid; /* Optional. Allow by default */
125986 p->tabFlags |= TF_NoVisibleRowid; /* Never allow rowid in view */
125990 iDb = sqlite3SchemaToIndex(db, p->pSchema);
125996 ** allocated rather than point to the input string - which means that
125999 pSelect->selFlags |= SF_View;
126001 p->u.view.pSelect = pSelect;
126004 p->u.view.pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
126006 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
126007 p->eTabType = TABTYP_VIEW;
126008 if( db->mallocFailed ) goto create_view_fail;
126013 sEnd = pParse->sLastToken;
126019 n = (int)(sEnd.z - pBegin->z);
126021 z = pBegin->z;
126022 while( sqlite3Isspace(z[n-1]) ){ n--; }
126023 sEnd.z = &z[n-1];
126042 ** the columns of the view in the pTable structure. Return non-zero if
126044 ** in pParse->zErrMsg.
126050 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
126062 db->nSchemaLock++;
126064 db->nSchemaLock--;
126074 assert( pTable->nCol<=0 );
126091 if( pTable->nCol<0 ){
126092 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
126095 assert( pTable->nCol>=0 );
126105 pSel = sqlite3SelectDup(db, pTable->u.view.pSelect, 0);
126107 u8 eParseMode = pParse->eParseMode;
126108 int nTab = pParse->nTab;
126109 int nSelect = pParse->nSelect;
126110 pParse->eParseMode = PARSE_MODE_NORMAL;
126111 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
126112 pTable->nCol = -1;
126115 xAuth = db->xAuth;
126116 db->xAuth = 0;
126118 db->xAuth = xAuth;
126122 pParse->nTab = nTab;
126123 pParse->nSelect = nSelect;
126125 pTable->nCol = 0;
126127 }else if( pTable->pCheck ){
126130 ** arglist which is stored in pTable->pCheck. The pCheck field
126134 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
126135 &pTable->nCol, &pTable->aCol);
126136 if( pParse->nErr==0
126137 && pTable->nCol==pSel->pEList->nExpr
126139 assert( db->mallocFailed==0 );
126146 assert( pTable->aCol==0 );
126147 pTable->nCol = pSelTab->nCol;
126148 pTable->aCol = pSelTab->aCol;
126149 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
126150 pSelTab->nCol = 0;
126151 pSelTab->aCol = 0;
126152 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
126154 pTable->nNVCol = pTable->nCol;
126158 pParse->eParseMode = eParseMode;
126162 pTable->pSchema->schemaFlags |= DB_UnresetViews;
126163 if( db->mallocFailed ){
126167 return nErr + pParse->nErr;
126171 if( !IsVirtual(pTable) && pTable->nCol>0 ) return 0;
126184 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
126199 ** root-page of a table or index in database iDb has changed from iFrom
126220 pDb = &db->aDb[iDb];
126221 pHash = &pDb->pSchema->tblHash;
126224 if( pTab->tnum==iFrom ){
126225 pTab->tnum = iTo;
126228 pHash = &pDb->pSchema->idxHash;
126231 if( pIdx->tnum==iFrom ){
126232 pIdx->tnum = iTo;
126239 ** Write code to erase the table with root-page iTable from database iDb.
126241 ** if a root-page of another table is moved by the btree-layer whilst
126242 ** erasing iTable (this can happen with an auto-vacuum database).
126252 ** is non-zero, then it is the root page number of a table moved to
126263 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
126271 ** in case a root-page belonging to another table is moved by the btree layer
126272 ** is also added (this can happen with an auto-vacuum database).
126275 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
126277 ** table and index root-pages in order, starting with the numerically
126278 ** largest root-page number. This guarantees that none of the root-pages
126286 ** and root page 5 happened to be the largest root-page number in the
126289 ** a free-list page.
126291 Pgno iTab = pTab->tnum;
126301 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
126302 Pgno iIdx = pIdx->tnum;
126303 assert( pIdx->pSchema==pTab->pSchema );
126311 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
126312 assert( iDb>=0 && iDb<pParse->db->nDb );
126330 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
126334 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
126348 sqlite3 *db = pParse->db;
126350 Db *pDb = &db->aDb[iDb];
126368 assert( pTrigger->pSchema==pTab->pSchema ||
126369 pTrigger->pSchema==db->aDb[1].pSchema );
126371 pTrigger = pTrigger->pNext;
126378 ** move as a result of the drop (can happen in auto-vacuum mode).
126380 if( pTab->tabFlags & TF_Autoincrement ){
126383 pDb->zDbSName, pTab->zName
126398 pDb->zDbSName, pTab->zName);
126407 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
126410 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
126416 ** Return TRUE if shadow tables should be read-only in the current
126421 if( (db->flags & SQLITE_Defensive)!=0
126422 && db->pVtabCtx==0
126423 && db->nVdbeExec==0
126436 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
126437 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
126438 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
126441 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
126444 if( pTab->tabFlags & TF_Eponymous ){
126457 sqlite3 *db = pParse->db;
126460 if( db->mallocFailed ){
126463 assert( pParse->nErr==0 );
126464 assert( pName->nSrc==1 );
126465 assert( pName->a[0].fg.fixedSchema==0 );
126466 assert( pName->a[0].fg.isSubquery==0 );
126468 if( noErr ) db->suppressErr++;
126470 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
126471 if( noErr ) db->suppressErr--;
126475 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
126480 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126481 assert( iDb>=0 && iDb<db->nDb );
126493 const char *zDb = db->aDb[iDb].zDbSName;
126507 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
126516 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
126519 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
126525 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
126534 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
126538 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
126550 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
126571 ** under construction in the pParse->pNewTable field.
126583 sqlite3 *db = pParse->db;
126587 Table *p = pParse->pNewTable;
126596 int iCol = p->nCol-1;
126598 if( pToCol && pToCol->nExpr!=1 ){
126601 p->aCol[iCol].zCnName, pTo);
126605 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
126611 nCol = pFromCol->nExpr;
126613 nByte = SZ_FKEY(nCol) + pTo->n + 1;
126615 for(i=0; i<pToCol->nExpr; i++){
126616 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
126623 pFKey->pFrom = p;
126625 pFKey->pNextFrom = p->u.tab.pFKey;
126626 z = (char*)&pFKey->aCol[nCol];
126627 pFKey->zTo = z;
126631 memcpy(z, pTo->z, pTo->n);
126632 z[pTo->n] = 0;
126634 z += pTo->n+1;
126635 pFKey->nCol = nCol;
126637 pFKey->aCol[0].iFrom = p->nCol-1;
126641 for(j=0; j<p->nCol; j++){
126642 if( sqlite3StrICmp(p->aCol[j].zCnName, pFromCol->a[i].zEName)==0 ){
126643 pFKey->aCol[i].iFrom = j;
126647 if( j>=p->nCol ){
126650 pFromCol->a[i].zEName);
126654 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
126660 int n = sqlite3Strlen30(pToCol->a[i].zEName);
126661 pFKey->aCol[i].zCol = z;
126663 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
126665 memcpy(z, pToCol->a[i].zEName, n);
126670 pFKey->isDeferred = 0;
126671 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
126672 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
126674 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
126675 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
126676 pFKey->zTo, (void *)pFKey
126683 assert( pNextTo->pPrevTo==0 );
126684 pFKey->pNextTo = pNextTo;
126685 pNextTo->pPrevTo = pFKey;
126691 p->u.tab.pFKey = pFKey;
126712 if( (pTab = pParse->pNewTable)==0 ) return;
126714 if( (pFKey = pTab->u.tab.pFKey)==0 ) return;
126715 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
126716 pFKey->isDeferred = (u8)isDeferred;
126729 ** the root page number of the index is taken from pIndex->tnum.
126732 Table *pTab = pIndex->pTable; /* The table that is indexed */
126733 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
126734 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
126743 sqlite3 *db = pParse->db; /* The database connection */
126744 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
126747 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
126748 db->aDb[iDb].zDbSName ) ){
126753 /* Require a write-lock on the table to perform this operation */
126754 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
126761 tnum = pIndex->tnum;
126764 assert( pKey!=0 || pParse->nErr );
126767 iSorter = pParse->nTab++;
126768 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
126794 pIndex->nKeyCol); VdbeCoverage(v);
126809 if( !pIndex->bAscKeyBug ){
126834 ** of 8-byte aligned space after the Index object and return a
126846 assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] );
126855 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
126856 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
126857 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
126858 p->aSortOrder = (u8*)pExtra;
126860 p->nColumn = (u16)nCol;
126861 p->nKeyCol = (u16)(nCol - 1);
126870 ** pParse and return non-zero. Otherwise, return zero.
126875 for(i=0; i<pList->nExpr; i++){
126876 if( pList->a[i].fg.bNulls ){
126877 u8 sf = pList->a[i].fg.sortFlags;
126892 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
126893 ** as the table to be indexed. pParse->pNewTable is a table that is
126897 ** is a primary key or unique-constraint on the most recent column added
126904 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
126920 sqlite3 *db = pParse->db;
126930 assert( db->pParse==pParse );
126931 if( pParse->nErr ){
126934 assert( db->mallocFailed==0 );
126950 /* Use the two-part index name to determine the database
126957 assert( pName && pName->z );
126964 if( !db->init.busy ){
126966 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
126978 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
126979 assert( db->mallocFailed==0 || pTab==0 );
126981 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
126983 "cannot create a TEMP index on non-TEMP table \"%s\"",
126984 pTab->zName);
126991 pTab = pParse->pNewTable;
126993 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
126995 pDb = &db->aDb[iDb];
126998 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
126999 && db->init.busy==0
127002 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
127034 assert( pName->z!=0 );
127035 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
127039 if( !db->init.busy ){
127040 if( sqlite3FindTable(db, zName, pDb->zDbSName)!=0 ){
127045 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
127049 assert( !db->init.busy );
127059 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
127060 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
127077 const char *zDb = pDb->zDbSName;
127083 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
127095 Column *pCol = &pTab->aCol[pTab->nCol-1];
127096 pCol->colFlags |= COLFLAG_UNIQUE;
127097 sqlite3TokenInit(&prevCol, pCol->zCnName);
127101 assert( pList->nExpr==1 );
127105 if( pParse->nErr ) goto exit_create_index;
127111 for(i=0; i<pList->nExpr; i++){
127112 Expr *pExpr = pList->a[i].pExpr;
127114 if( pExpr->op==TK_COLLATE ){
127116 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
127124 nExtraCol = pPk ? pPk->nKeyCol : 1;
127125 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
127126 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
127128 if( db->mallocFailed ){
127131 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
127132 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
127133 pIndex->zName = zExtra;
127135 memcpy(pIndex->zName, zName, nName+1);
127136 pIndex->pTable = pTab;
127137 pIndex->onError = (u8)onError;
127138 pIndex->uniqNotNull = onError!=OE_None;
127139 pIndex->idxType = idxType;
127140 pIndex->pSchema = db->aDb[iDb].pSchema;
127141 pIndex->nKeyCol = pList->nExpr;
127144 pIndex->pPartIdxWhere = pPIWhere;
127151 if( pDb->pSchema->file_format>=4 ){
127152 sortOrderMask = -1; /* Honor DESC */
127160 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
127166 pListItem = pList->a;
127168 pIndex->aColExpr = pList;
127171 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
127172 Expr *pCExpr; /* The i-th index expression */
127173 int requestedSortOrder; /* ASC or DESC on the i-th expression */
127176 sqlite3StringToId(pListItem->pExpr);
127177 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
127178 if( pParse->nErr ) goto exit_create_index;
127179 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
127180 if( pCExpr->op!=TK_COLUMN ){
127181 if( pTab==pParse->pNewTable ){
127186 if( pIndex->aColExpr==0 ){
127187 pIndex->aColExpr = pList;
127191 pIndex->aiColumn[i] = XN_EXPR;
127192 pIndex->uniqNotNull = 0;
127193 pIndex->bHasExpr = 1;
127195 j = pCExpr->iColumn;
127198 j = pTab->iPKey;
127199 pIndex->bIdxRowid = 1;
127201 if( pTab->aCol[j].notNull==0 ){
127202 pIndex->uniqNotNull = 0;
127204 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
127205 pIndex->bHasVCol = 1;
127206 pIndex->bHasExpr = 1;
127209 pIndex->aiColumn[i] = (i16)j;
127212 if( pListItem->pExpr->op==TK_COLLATE ){
127214 assert( !ExprHasProperty(pListItem->pExpr, EP_IntValue) );
127215 zColl = pListItem->pExpr->u.zToken;
127221 nExtra -= nColl;
127223 zColl = sqlite3ColumnColl(&pTab->aCol[j]);
127226 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
127229 pIndex->azColl[i] = zColl;
127230 requestedSortOrder = pListItem->fg.sortFlags & sortOrderMask;
127231 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
127239 for(j=0; j<pPk->nKeyCol; j++){
127240 int x = pPk->aiColumn[j];
127242 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
127243 pIndex->nColumn--;
127245 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
127246 pIndex->aiColumn[i] = x;
127247 pIndex->azColl[i] = pPk->azColl[j];
127248 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
127252 assert( i==pIndex->nColumn );
127254 pIndex->aiColumn[i] = XN_ROWID;
127255 pIndex->azColl[i] = sqlite3StrBINARY;
127258 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
127263 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
127265 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
127266 pIndex->isCovering = 1;
127267 for(j=0; j<pTab->nCol; j++){
127268 if( j==pTab->iPKey ) continue;
127270 pIndex->isCovering = 0;
127275 if( pTab==pParse->pNewTable ){
127298 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
127301 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
127304 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
127305 for(k=0; k<pIdx->nKeyCol; k++){
127308 assert( pIdx->aiColumn[k]>=0 );
127309 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
127310 z1 = pIdx->azColl[k];
127311 z2 = pIndex->azColl[k];
127314 if( k==pIdx->nKeyCol ){
127315 if( pIdx->onError!=pIndex->onError ){
127323 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
127327 if( pIdx->onError==OE_Default ){
127328 pIdx->onError = pIndex->onError;
127331 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
127333 pIndex->pNext = pParse->pNewIndex;
127334 pParse->pNewIndex = pIndex;
127345 ** in-memory database structures.
127347 assert( pParse->nErr==0 );
127348 if( db->init.busy ){
127351 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
127353 pIndex->tnum = db->init.newTnum;
127356 pParse->rc = SQLITE_CORRUPT_BKPT;
127360 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
127361 pIndex->zName, pIndex);
127367 db->mDbFlags |= DBFLAG_SchemaChange;
127386 int iMem = ++pParse->nMem;
127399 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
127407 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
127408 if( pName->z[n-1]==';' ) n--;
127411 onError==OE_None ? "" : " UNIQUE", n, pName->z);
127422 db->aDb[iDb].zDbSName,
127423 pIndex->zName,
127424 pTab->zName,
127431 ** to invalidate all pre-compiled statements.
127437 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
127441 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
127444 if( db->init.busy || pTblName==0 ){
127445 pIndex->pNext = pTab->pIndex;
127446 pTab->pIndex = pIndex;
127450 assert( pParse->pNewIndex==0 );
127451 pParse->pNewIndex = pIndex;
127465 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
127467 if( pThis->onError!=OE_Replace ) continue;
127468 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
127470 pThis->pNext = pNext->pNext;
127471 pNext->pNext = pThis;
127472 ppFrom = &pNext->pNext;
127480 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
127481 assert( pThis->onError!=OE_Replace
127482 || pThis->pNext==0
127483 || pThis->pNext->onError==OE_Replace );
127494 ** Fill the Index.aiRowEst[] array with default information - information
127504 ** aiRowEst[N]<=aiRowEst[N-1]
127514 LogEst *a = pIdx->aiRowLogEst;
127516 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
127520 assert( !pIdx->hasStat1 );
127526 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
127532 x = pIdx->pTable->nRowLogEst;
127535 pIdx->pTable->nRowLogEst = x = 99;
127537 if( pIdx->pPartIdxWhere!=0 ){ x -= 10; assert( 10==sqlite3LogEst(2) ); }
127543 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
127548 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
127558 sqlite3 *db = pParse->db;
127561 if( db->mallocFailed ){
127564 assert( pParse->nErr==0 ); /* Never called with prior non-OOM errors */
127565 assert( pName->nSrc==1 );
127566 assert( pName->a[0].fg.fixedSchema==0 );
127567 assert( pName->a[0].fg.isSubquery==0 );
127571 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].u4.zDatabase);
127574 sqlite3ErrorMsg(pParse, "no such index: %S", pName->a);
127576 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].u4.zDatabase);
127579 pParse->checkSchema = 1;
127582 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
127587 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
127591 Table *pTab = pIndex->pTable;
127592 const char *zDb = db->aDb[iDb].zDbSName;
127598 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
127610 db->aDb[iDb].zDbSName, pIndex->zName
127612 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
127614 destroyRootPage(pParse, pIndex->tnum, iDb);
127615 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
127628 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
127636 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
127648 if( (n & (n-1))==0 ){
127652 *pIdx = -1;
127670 sqlite3 *db = pParse->db;
127677 pNew = sqlite3DbRealloc(db, pList, SZ_IDLIST(pList->nId+1));
127684 i = pList->nId++;
127685 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
127686 if( IN_RENAME_OBJECT && pList->a[i].zName ){
127687 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
127699 for(i=0; i<pList->nId; i++){
127700 sqlite3DbFree(db, pList->a[i].zName);
127706 ** Return the index in pList of the identifier named zId. Return -1
127712 for(i=0; i<pList->nId; i++){
127713 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
127715 return -1;
127724 ** appropriate for small and memory-limited applications.
127753 int nExtra, /* Number of new slots to add to pSrc->a[] */
127754 int iStart /* Index in pSrc->a[] of first new slot */
127762 assert( iStart<=pSrc->nSrc );
127765 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
127767 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
127768 sqlite3 *db = pParse->db;
127770 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
127778 assert( db->mallocFailed );
127782 pSrc->nAlloc = nAlloc;
127787 for(i=pSrc->nSrc-1; i>=iStart; i--){
127788 pSrc->a[i+nExtra] = pSrc->a[i];
127790 pSrc->nSrc += nExtra;
127793 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
127795 pSrc->a[i].iCursor = -1;
127848 assert( pParse->db!=0 );
127849 db = pParse->db;
127851 pList = sqlite3DbMallocRawNN(pParse->db, SZ_SRCLIST(1));
127853 pList->nAlloc = 1;
127854 pList->nSrc = 1;
127855 memset(&pList->a[0], 0, sizeof(pList->a[0]));
127856 pList->a[0].iCursor = -1;
127858 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
127866 pItem = &pList->a[pList->nSrc-1];
127867 if( pDatabase && pDatabase->z==0 ){
127870 assert( pItem->fg.fixedSchema==0 );
127871 assert( pItem->fg.isSubquery==0 );
127873 pItem->zName = sqlite3NameFromToken(db, pDatabase);
127874 pItem->u4.zDatabase = sqlite3NameFromToken(db, pTable);
127876 pItem->zName = sqlite3NameFromToken(db, pTable);
127877 pItem->u4.zDatabase = 0;
127888 assert( pList || pParse->db->mallocFailed );
127890 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
127891 if( pItem->iCursor>=0 ) continue;
127892 pItem->iCursor = pParse->nTab++;
127893 if( pItem->fg.isSubquery ){
127894 assert( pItem->u4.pSubq!=0 );
127895 assert( pItem->u4.pSubq->pSelect!=0 );
127896 assert( pItem->u4.pSubq->pSelect->pSrc!=0 );
127897 sqlite3SrcListAssignCursors(pParse, pItem->u4.pSubq->pSelect->pSrc);
127907 assert( pSubq!=0 && pSubq->pSelect!=0 );
127908 sqlite3SelectDelete(db, pSubq->pSelect);
127919 assert( pItem->fg.isSubquery );
127920 pSel = pItem->u4.pSubq->pSelect;
127921 sqlite3DbFree(db, pItem->u4.pSubq);
127922 pItem->u4.pSubq = 0;
127923 pItem->fg.isSubquery = 0;
127935 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
127938 assert( !pItem->fg.isIndexedBy || !pItem->fg.isTabFunc );
127939 assert( !pItem->fg.isCte || !pItem->fg.isIndexedBy );
127940 assert( !pItem->fg.fixedSchema || !pItem->fg.isSubquery );
127941 assert( !pItem->fg.isSubquery || (pItem->u4.pSubq!=0 &&
127942 pItem->u4.pSubq->pSelect!=0) );
127944 if( pItem->zName ) sqlite3DbNNFreeNN(db, pItem->zName);
127945 if( pItem->zAlias ) sqlite3DbNNFreeNN(db, pItem->zAlias);
127946 if( pItem->fg.isSubquery ){
127947 sqlite3SubqueryDelete(db, pItem->u4.pSubq);
127948 }else if( pItem->fg.fixedSchema==0 && pItem->u4.zDatabase!=0 ){
127949 sqlite3DbNNFreeNN(db, pItem->u4.zDatabase);
127951 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
127952 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
127953 sqlite3DeleteTable(db, pItem->pSTab);
127954 if( pItem->fg.isUsing ){
127955 sqlite3IdListDelete(db, pItem->u3.pUsing);
127956 }else if( pItem->u3.pOn ){
127957 sqlite3ExprDelete(db, pItem->u3.pOn);
127964 ** Attach a Subquery object to pItem->uv.pSubq. Set the
127976 ** Return non-zero on success. Return zero on an OOM error.
127981 Select *pSelect, /* The subquery SELECT. Must be non-NULL */
127986 assert( pItem->fg.isSubquery==0 );
127987 if( pItem->fg.fixedSchema ){
127988 pItem->u4.pSchema = 0;
127989 pItem->fg.fixedSchema = 0;
127990 }else if( pItem->u4.zDatabase!=0 ){
127991 sqlite3DbFree(pParse->db, pItem->u4.zDatabase);
127992 pItem->u4.zDatabase = 0;
127995 pSelect = sqlite3SelectDup(pParse->db, pSelect, 0);
127998 p = pItem->u4.pSubq = sqlite3DbMallocRawNN(pParse->db, sizeof(Subquery));
128000 sqlite3SelectDelete(pParse->db, pSelect);
128003 pItem->fg.isSubquery = 1;
128004 p->pSelect = pSelect;
128006 memset(((char*)p)+sizeof(p->pSelect), 0, sizeof(*p)-sizeof(p->pSelect));
128017 ** pDatabase is NULL if the database name qualifier is missing - the
128032 Token *pAlias, /* The right-hand side of the AS subexpression */
128037 sqlite3 *db = pParse->db;
128038 if( !p && pOnUsing!=0 && (pOnUsing->pOn || pOnUsing->pUsing) ){
128040 (pOnUsing->pOn ? "ON" : "USING")
128048 assert( p->nSrc>0 );
128049 pItem = &p->a[p->nSrc-1];
128051 assert( pItem->zName==0 || pDatabase!=0 );
128052 if( IN_RENAME_OBJECT && pItem->zName ){
128053 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
128054 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
128057 if( pAlias->n ){
128058 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
128063 if( pSubquery->selFlags & SF_NestedFrom ){
128064 pItem->fg.isNestedFrom = 1;
128068 assert( pOnUsing==0 || pOnUsing->pOn==0 || pOnUsing->pUsing==0 );
128069 assert( pItem->fg.isUsing==0 );
128071 pItem->u3.pOn = 0;
128072 }else if( pOnUsing->pUsing ){
128073 pItem->fg.isUsing = 1;
128074 pItem->u3.pUsing = pOnUsing->pUsing;
128076 pItem->u3.pOn = pOnUsing->pOn;
128089 ** element of the source-list passed as the second argument.
128093 if( p && pIndexedBy->n>0 ){
128095 assert( p->nSrc>0 );
128096 pItem = &p->a[p->nSrc-1];
128097 assert( pItem->fg.notIndexed==0 );
128098 assert( pItem->fg.isIndexedBy==0 );
128099 assert( pItem->fg.isTabFunc==0 );
128100 if( pIndexedBy->n==1 && !pIndexedBy->z ){
128103 pItem->fg.notIndexed = 1;
128105 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
128106 pItem->fg.isIndexedBy = 1;
128107 assert( pItem->fg.isCte==0 ); /* No collision on union u2 */
128118 assert( p1 && p1->nSrc==1 );
128120 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
128122 sqlite3SrcListDelete(pParse->db, p2);
128125 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
128126 sqlite3DbFree(pParse->db, p2);
128127 p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
128135 ** table-valued-function.
128139 SrcItem *pItem = &p->a[p->nSrc-1];
128140 assert( pItem->fg.notIndexed==0 );
128141 assert( pItem->fg.isIndexedBy==0 );
128142 assert( pItem->fg.isTabFunc==0 );
128143 pItem->u1.pFuncArg = pList;
128144 pItem->fg.isTabFunc = 1;
128146 sqlite3ExprListDelete(pParse->db, pList);
128162 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
128167 ** * All tables to the left of the right-most RIGHT JOIN are tagged with
128174 if( p && p->nSrc>1 ){
128175 int i = p->nSrc-1;
128178 allFlags |= p->a[i].fg.jointype = p->a[i-1].fg.jointype;
128179 }while( (--i)>0 );
128180 p->a[0].fg.jointype = 0;
128185 for(i=p->nSrc-1; ALWAYS(i>0) && (p->a[i].fg.jointype&JT_RIGHT)==0; i--){}
128186 i--;
128189 p->a[i].fg.jointype |= JT_LTORJ;
128190 }while( (--i)>=0 );
128204 db = pParse->db;
128212 for(i=0; i<db->nDb; i++){
128214 Btree *pBt = db->aDb[i].pBt;
128239 assert( pParse->db!=0 );
128257 char *zName = sqlite3NameFromToken(pParse->db, pName);
128265 sqlite3DbFree(pParse->db, zName);
128277 sqlite3 *db = pParse->db;
128278 if( db->aDb[1].pBt==0 && !pParse->explain ){
128288 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
128292 pParse->rc = rc;
128295 db->aDb[1].pBt = pBt;
128296 assert( db->aDb[1].pSchema );
128297 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
128308 ** will occur at the end of the top-level VDBE and will be generated
128312 assert( iDb>=0 && iDb<pToplevel->db->nDb );
128313 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
128315 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
128316 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
128317 DbMaskSet(pToplevel->cookieMask, iDb);
128333 sqlite3 *db = pParse->db;
128335 for(i=0; i<db->nDb; i++){
128336 Db *pDb = &db->aDb[i];
128337 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
128359 DbMaskSet(pToplevel->writeMask, iDb);
128360 pToplevel->isMultiWrite |= setStatement;
128372 pToplevel->isMultiWrite = 1;
128393 pToplevel->mayAbort = 1;
128410 assert( pParse->pVdbe!=0 );
128412 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
128431 Table *pTab = pIdx->pTable;
128433 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
128434 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
128435 if( pIdx->aColExpr ){
128436 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
128438 for(j=0; j<pIdx->nKeyCol; j++){
128440 assert( pIdx->aiColumn[j]>=0 );
128441 zCol = pTab->aCol[pIdx->aiColumn[j]].zCnName;
128443 sqlite3_str_appendall(&errMsg, pTab->zName);
128457 ** Code an OP_Halt due to non-unique rowid.
128462 Table *pTab /* The table with the non-unique rowid */
128466 if( pTab->iPKey>=0 ){
128467 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
128468 pTab->aCol[pTab->iPKey].zCnName);
128471 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
128486 for(i=0; i<pIndex->nColumn; i++){
128487 const char *z = pIndex->azColl[i];
128488 assert( z!=0 || pIndex->aiColumn[i]<0 );
128489 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
128506 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
128508 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
128510 sqlite3RefillIndex(pParse, pIndex, -1);
128526 sqlite3 *db = pParse->db; /* The database connection */
128531 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
128533 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
128544 ** REINDEX -- 1
128545 ** REINDEX <collation> -- 2
128546 ** REINDEX ?<database>.?<tablename> -- 3
128547 ** REINDEX ?<database>.?<indexname> -- 4
128562 sqlite3 *db = pParse->db; /* The database connection */
128574 }else if( NEVER(pName2==0) || pName2->z==0 ){
128576 assert( pName1->z );
128577 zColl = sqlite3NameFromToken(pParse->db, pName1);
128591 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
128601 iDb = sqlite3SchemaToIndex(db, pIndex->pTable->pSchema);
128603 sqlite3RefillIndex(pParse, pIndex, -1);
128618 int nCol = pIdx->nColumn;
128619 int nKey = pIdx->nKeyCol;
128621 if( pParse->nErr ) return 0;
128622 if( pIdx->uniqNotNull ){
128623 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
128625 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
128630 const char *zColl = pIdx->azColl[i];
128631 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
128633 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
128634 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
128636 if( pParse->nErr ){
128637 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
128638 if( pIdx->bNoQuery==0 ){
128643 ** the missing index using the collation-needed callback. For
128646 pIdx->bNoQuery = 1;
128647 pParse->rc = SQLITE_ERROR_RETRY;
128662 Token *pName, /* Name of the common-table */
128668 sqlite3 *db = pParse->db;
128671 assert( pNew!=0 || db->mallocFailed );
128673 if( db->mallocFailed ){
128677 pNew->pSelect = pQuery;
128678 pNew->pCols = pArglist;
128679 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
128680 pNew->eM10d = eM10d;
128691 sqlite3ExprListDelete(db, pCte->pCols);
128692 sqlite3SelectDelete(db, pCte->pSelect);
128693 sqlite3DbFree(db, pCte->zName);
128716 sqlite3 *db = pParse->db;
128726 zName = pCte->zName;
128729 for(i=0; i<pWith->nCte; i++){
128730 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
128737 pNew = sqlite3DbRealloc(db, pWith, SZ_WITH(pWith->nCte+1));
128741 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
128743 if( db->mallocFailed ){
128747 pNew->a[pNew->nCte++] = *pCte;
128760 for(i=0; i<pWith->nCte; i++){
128761 cteClear(db, &pWith->a[i]);
128793 ** in the encoding enc of name zName, length nName.
128796 assert( !db->xCollNeeded || !db->xCollNeeded16 );
128797 if( db->xCollNeeded ){
128800 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
128804 if( db->xCollNeeded16 ){
128807 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
128810 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
128821 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
128826 char *z = pColl->zName;
128831 if( pColl2->xCmp!=0 ){
128833 pColl->xDel = 0; /* Do not copy the destructor */
128852 if( pColl && pColl->xCmp==0 ){
128853 const char *zName = pColl->zName;
128854 sqlite3 *db = pParse->db;
128873 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
128885 pColl = sqlite3HashFind(&db->aCollSeq, zName);
128899 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
128917 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
128942 if( pColl ) pColl += enc-1;
128944 pColl = db->pDfltColl;
128955 db->enc = enc;
128956 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
128959 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
128969 ** collation sequence with name zName, length nName.
128972 ** db for collation type name zName, length nName, or NULL, if no collation
128984 sqlite3 *db = pParse->db;
128990 if( !p || !p->xCmp ){
128997 if( p && !p->xCmp && synthCollSeq(db, p) ){
129000 assert( !p || p->xCmp );
129003 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
129029 sqlite3 *db = pParse->db;
129031 u8 initbusy = db->init.busy;
129035 if( !initbusy && (!pColl || !pColl->xCmp) ){
129048 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
129049 ** is also -1. In other words, we are searching for a function that
129052 ** If nArg is -2 that means that we are searching for any function
129062 ** 4: UTF8/16 conversion required - argument count matches exactly
129063 ** 5: UTF16 byte order conversion required - argument count matches exactly
129066 ** If nArg==(-2) then any function with a non-null xSFunc is
129068 ** a non-match.
129073 int nArg, /* Desired number of arguments. (-1)==any */
129077 assert( p->nArg>=(-4) && p->nArg!=(-2) );
129078 assert( nArg>=(-2) );
129081 if( p->nArg!=nArg ){
129082 if( nArg==(-2) ) return p->xSFunc==0 ? 0 : FUNC_PERFECT_MATCH;
129083 if( p->nArg>=0 ) return 0;
129084 /* Special p->nArg values available to built-in functions only:
129085 ** -3 1 or more arguments required
129086 ** -4 2 or more arguments required
129088 if( p->nArg<(-2) && nArg<(-2-p->nArg) ) return 0;
129093 if( p->nArg==nArg ){
129100 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
129102 }else if( (enc & p->funcFlags & 2)!=0 ){
129118 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
129119 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
129120 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
129132 int nDef /* Length of the apDef[] list */
129143 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
129144 aDef[i].pNext = pOther->pNext;
129145 pOther->pNext = &aDef[i];
129158 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
129166 ** If nArg is -2, then the first valid function found is returned. A
129167 ** function is valid if xSFunc is non-zero. The nArg==(-2)
129169 ** of arguments. If nArg is -2, then createFlag must be 0.
129177 const char *zName, /* Name of the function. zero-terminated */
129178 int nArg, /* Number of arguments. -1 means any number */
129186 int nName; /* Length of the name */
129188 assert( nArg>=(-2) );
129189 assert( nArg>=(-1) || createFlag==0 );
129192 /* First search for a match amongst the application-defined functions.
129194 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
129201 p = p->pNext;
129204 /* If no match is found, search the built-in functions.
129206 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
129207 ** functions even if a prior app-defined function was found. And give
129208 ** priority to built-in functions.
129213 ** new function. But the FuncDefs for built-in functions are read-only.
129214 ** So we must not search for built-ins when creating a new function.
129216 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
129226 p = p->pNext;
129238 pBest->zName = (const char*)&pBest[1];
129239 pBest->nArg = (u16)nArg;
129240 pBest->funcFlags = enc;
129242 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
129243 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
129249 pBest->pNext = pOther;
129253 if( pBest && (pBest->xSFunc || createFlag) ){
129275 temp1 = pSchema->tblHash;
129276 temp2 = pSchema->trigHash;
129277 sqlite3HashInit(&pSchema->trigHash);
129278 sqlite3HashClear(&pSchema->idxHash);
129283 sqlite3HashInit(&pSchema->tblHash);
129289 sqlite3HashClear(&pSchema->fkeyHash);
129290 pSchema->pSeqTab = 0;
129291 if( pSchema->schemaFlags & DB_SchemaLoaded ){
129292 pSchema->iGeneration++;
129294 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
129310 }else if ( 0==p->file_format ){
129311 sqlite3HashInit(&p->tblHash);
129312 sqlite3HashInit(&p->idxHash);
129313 sqlite3HashInit(&p->trigHash);
129314 sqlite3HashInit(&p->fkeyHash);
129315 p->enc = SQLITE_UTF8;
129348 ** pSrc->a[0].spTab Pointer to the Table object
129349 ** pSrc->a[0].u2.pIBIndex Pointer to the INDEXED BY index, if there is one
129353 SrcItem *pItem = pSrc->a;
129355 assert( pItem && pSrc->nSrc>=1 );
129357 if( pItem->pSTab ) sqlite3DeleteTable(pParse->db, pItem->pSTab);
129358 pItem->pSTab = pTab;
129359 pItem->fg.notCte = 1;
129361 pTab->nTabRef++;
129362 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
129369 /* Generate byte-code that will report the number of rows modified
129379 /* Return true if table pTab is read-only.
129381 ** A table is read-only if any of the following are true:
129396 ** is for a top-level SQL statement.
129400 if( sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ){
129407 ** * Only allow DELETE, INSERT, or UPDATE of non-SQLITE_VTAB_INNOCUOUS
129410 if( pParse->pToplevel!=0
129411 && pTab->u.vtab.p->eVtabRisk >
129412 ((pParse->db->flags & SQLITE_TrustedSchema)!=0)
129415 pTab->zName);
129424 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
129425 db = pParse->db;
129426 if( (pTab->tabFlags & TF_Readonly)!=0 ){
129427 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
129429 assert( pTab->tabFlags & TF_Shadow );
129436 ** If pTab is not writable -> generate an error message and return 1.
129437 ** If pTab is writable but other errors have occurred -> return 1.
129438 ** If pTab is writable and no prior errors -> return 0;
129442 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
129447 && (pTrigger==0 || (pTrigger->bReturning && pTrigger->pNext==0))
129449 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
129474 sqlite3 *db = pParse->db;
129475 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
129479 assert( pFrom->nSrc==1 );
129480 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
129481 assert( pFrom->a[0].fg.fixedSchema==0 && pFrom->a[0].fg.isSubquery==0 );
129482 pFrom->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
129483 assert( pFrom->a[0].fg.isUsing==0 );
129484 assert( pFrom->a[0].u3.pOn==0 );
129505 SrcList *pSrc, /* the FROM clause -- which tables to scan */
129511 sqlite3 *db = pParse->db;
129523 sqlite3ExprDelete(pParse->db, pWhere);
129524 sqlite3ExprListDelete(pParse->db, pOrderBy);
129544 pTab = pSrc->a[0].pSTab;
129553 assert( pPk->nKeyCol>=1 );
129554 if( pPk->nKeyCol==1 ){
129556 assert( pPk->aiColumn[0]>=0 && pPk->aiColumn[0]<pTab->nCol );
129557 zName = pTab->aCol[pPk->aiColumn[0]].zCnName;
129562 for(i=0; i<pPk->nKeyCol; i++){
129564 assert( pPk->aiColumn[i]>=0 && pPk->aiColumn[i]<pTab->nCol );
129565 p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zCnName);
129570 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
129577 pSrc->a[0].pSTab = 0;
129579 pSrc->a[0].pSTab = pTab;
129580 if( pSrc->a[0].fg.isIndexedBy ){
129581 assert( pSrc->a[0].fg.isCte==0 );
129582 pSrc->a[0].u2.pIBIndex = 0;
129583 pSrc->a[0].fg.isIndexedBy = 0;
129584 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
129585 }else if( pSrc->a[0].fg.isCte ){
129586 pSrc->a[0].u2.pCteUse->nUse++;
129653 db = pParse->db;
129654 assert( db->pParse==pParse );
129655 if( pParse->nErr ){
129658 assert( db->mallocFailed==0 );
129659 assert( pTabList->nSrc==1 );
129688 sqlite3TreeViewDelete(pParse->pWith, pTabList, pWhere,
129712 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
129713 assert( iDb<db->nDb );
129714 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
129715 db->aDb[iDb].zDbSName);
129724 assert( pTabList->nSrc==1 );
129725 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
129726 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
129727 pParse->nTab++;
129733 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
129742 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
129771 if( (db->flags & SQLITE_CountRows)!=0
129772 && !pParse->nested
129773 && !pParse->pTriggerTab
129774 && !pParse->bReturning
129776 memCnt = ++pParse->nMem;
129787 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
129797 && db->xPreUpdateCallback==0
129801 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
129803 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
129804 pTab->zName, P4_STATIC);
129806 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
129807 assert( pIdx->pSchema==pTab->pSchema );
129809 sqlite3VdbeAddOp3(v, OP_Clear, pIdx->tnum, iDb, memCnt ? memCnt : -1);
129811 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
129824 iRowSet = ++pParse->nMem;
129831 nPk = pPk->nKeyCol;
129832 iPk = pParse->nMem+1;
129833 pParse->nMem += nPk;
129834 iEphCur = pParse->nTab++;
129843 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
129844 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
129845 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
129866 assert( pPk->aiColumn[i]>=0 );
129868 pPk->aiColumn[i], iPk+i);
129872 iKey = ++pParse->nMem;
129873 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
129877 /* For ONEPASS, no need to store the rowid/primary-key. There is only
129888 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
129889 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
129895 iKey = ++pParse->nMem;
129898 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
129928 /* Set up a loop over the rowids/primary-keys that were found in the
129929 ** where-clause loop above.
129933 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
129962 pParse->isMultiWrite = 0;
129970 int count = (pParse->nested==0); /* True to count changes */
129975 /* End of the loop over all rowids/primary-keys. */
129986 } /* End non-truncate path */
129992 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
130038 ** cursor number iIdxCur+i for the i-th index.
130074 u8 count, /* If non-zero, increment the row change counter */
130079 Vdbe *v = pParse->pVdbe; /* Vdbe */
130113 iOld = pParse->nMem+1;
130114 pParse->nMem += (1 + pTab->nCol);
130116 /* Populate the OLD.* pseudo-table register array. These values will be
130119 for(iCol=0; iCol<pTab->nCol; iCol++){
130147 iIdxNoSeek = -1;
130160 ** If variable 'count' is non-zero, then this OP_Delete instruction should
130161 ** invoke the update-hook. The pre-update-hook, on the other hand should
130163 ** the update-hook is not invoked for rows removed by REPLACE, but the
130164 ** pre-update-hook is.
130170 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
130214 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
130215 ** index is the 0-th index.)
130229 int r1 = -1; /* Register holding an index key */
130236 v = pParse->pVdbe;
130238 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
130243 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
130247 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
130295 Vdbe *v = pParse->pVdbe;
130301 if( pIdx->pPartIdxWhere ){
130303 pParse->iSelfTab = iDataCur + 1;
130304 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
130306 pParse->iSelfTab = 0;
130307 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
130313 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
130315 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
130318 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
130319 && pPrior->aiColumn[j]!=XN_EXPR
130325 if( pIdx->aiColumn[j]>=0 ){
130343 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
130349 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
130366 ** This file contains the C-language implementations for many of the SQL
130383 assert( context->pVdbe!=0 );
130384 pOp = &context->pVdbe->aOp[context->iOp-1];
130385 assert( pOp->opcode==OP_CollSeq );
130386 assert( pOp->p4type==P4_COLLSEQ );
130387 return pOp->p4.pColl;
130395 assert( context->isError<=0 );
130396 context->isError = -1;
130397 context->skipFlag = 1;
130401 ** Implementation of the non-aggregate min() and max() functions
130414 mask = sqlite3_user_data(context)==0 ? 0 : -1;
130417 assert( mask==-1 || mask==0 );
130439 int i = sqlite3_value_type(argv[0]) - 1;
130447 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
130451 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
130468 ** Implementation of the length() function
130496 sqlite3_result_int(context, (int)(z-z0));
130523 i64 m = sqlite3_context_db_handle(context)->enc<=SQLITE_UTF8 ? 1 : 2;
130545 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
130556 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
130558 ** equivalent positive 64-bit two complement value. */
130559 sqlite3_result_error(context, "integer overflow", -1);
130562 iVal = -iVal;
130568 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
130575 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
130579 if( rVal<0 ) rVal = -rVal;
130646 nHaystack--;
130677 x.nArg = argc-1;
130680 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
130693 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
130694 ** of x. If x is text, then we actually count UTF-8 characters.
130734 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
130739 ** as substr(X,1,N) - it returns the first N characters of X. This
130740 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
130741 ** from 2009-02-02 for compatibility of applications that exploited the
130758 p1--;
130760 p2--;
130763 if( p2<-p1 ){
130766 p2 = -p2;
130768 p1 -= p2;
130774 p1--;
130776 for(z2=z; *z2 && p2; p2--){
130779 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
130784 }else if( p2>len-p1 ){
130785 p2 = len-p1;
130809 /* If Y==0 and X will fit in a 64-bit int,
130813 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
130816 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
130834 ** If nByte is larger than the maximum string or blob length, then
130841 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
130842 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
130843 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
130905 #define noopFunc versionFunc /* Substitute function - never called */
130920 ** (or -9223372036854775808) since when you do abs() of that
130925 ** therefore be no less than -9223372036854775807.
130927 r = -(r & LARGEST_INT64);
130967 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
130976 ** IMP: R-32760-32347 The changes() SQL function is a wrapper
131001 /* IMP: R-11217-42568 This function is a wrapper around the
131007 ** A structure defining how to do GLOB-style comparisons.
131030 /* The correct SQL-92 behavior is for the LIKE operator to ignore
131045 ** Compare two UTF-8 strings for equality where the first string is
131065 ** range of characters can be specified using '-'. Example:
131066 ** "[a-z]" matches any single lower-case letter. To match a '-', make
131089 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
131090 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
131091 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
131108 if( pInfo->matchSet==0 ){
131114 assert( matchOther<0x80 ); /* '[' is a single-byte character */
131116 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
131129 ** For a case-insensitive search, set variable cx to be the same as
131162 if( pInfo->matchSet==0 ){
131182 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
131213 ** non-zero if there is no match.
131226 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
131227 ** a miss - like strcmp().
131251 ** the built-in LIKE operator. The first argument to the function is the
131285 /* Limit the length of the LIKE or GLOB pattern to avoid problems
131289 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
131290 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
131291 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
131292 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
131296 /* The escape character string must consist of a single UTF-8 character.
131301 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
131303 "ESCAPE expression must be a single character", -1);
131307 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
131310 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
131311 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
131314 escape = pInfo->matchSet;
131354 /* IMP: R-48699-48617 This function is an SQL wrapper around the
131355 ** sqlite3_libversion() C-interface. */
131356 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
131370 /* IMP: R-24470-31136 This function is an SQL wrapper around the
131372 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
131378 ** its side-effects.
131404 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
131428 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
131432 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
131436 /* Array for converting from half-bytes (nybbles) into ASCII hex
131451 assert( pStr!=0 && pStr->nChar==0 );
131461 sqlite3AtoF(zVal, &r2, pStr->nChar, SQLITE_UTF8);
131478 if( pStr->accError==0 ){
131479 char *zText = pStr->zText;
131489 pStr->nChar = nBlob*2 + 3;
131525 ** This is intended to be a work-alike of the UNISTR() function in
131526 ** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 -
131527 ** scraped on 2025-02-22):
131560 n = nIn - i;
131565 n = z - &zIn[i];
131600 sqlite3_result_error(context, "invalid Unicode escape", -1);
131610 ** Strings are surrounded by single-quotes with escapes on interior quotes
131615 ** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
131624 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
131635 ** The unicode() function. Return the integer unicode code-point value
131688 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
131716 sqlite3_result_text64(context, zHex, (u64)(z-zHex),
131722 ** Buffer zStr contains nStr bytes of utf-8 encoded text. Return 1 if zStr
131802 sqlite3_result_blob(pCtx, pBlob, (p - pBlob), sqlite3_free);
131812 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
131825 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
131864 || sqlite3_context_db_handle(context)->mallocFailed );
131884 loopLimit = nStr - nPattern;
131891 nOut += nRep - nPattern;
131892 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
131893 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
131894 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
131900 if( (cntExpand&(cntExpand-1))==0 ){
131905 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
131915 i += nPattern-1;
131918 assert( j+nStr-i+1<=nOut );
131919 memcpy(&zOut[j], &zStr[i], nStr-i);
131920 j += nStr - i;
131940 unsigned int *aLen = 0; /* Length of each character in zCharSet */
131975 aLen[nChar] = (unsigned)(z - azChar[nChar]);
131990 nIn -= len;
131998 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
132001 nIn -= len;
132014 ** Return a string value that is the concatenation of all non-null
132030 n += (argc-1)*(i64)nSep;
132058 ** concatentation of all non-null arguments.
132072 ** argument. Use the first argument (which must be non-NULL) as the
132083 concatFuncCore(context, argc-1, argv+1, nSep, zSep);
132091 ** when the SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION compile-time option is used.
132092 ** When the "sqlite3" command-line shell is built using this functionality,
132094 ** involving application-defined functions to be examined in a generic
132102 /* no-op */
132110 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
132111 ** is only available if the SQLITE_SOUNDEX compile-time option is used
132118 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
132163 /* IMP: R-64894-50321 The string "?000" is returned if the argument
132172 ** A function that loads a shared-library extension then returns NULL.
132183 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
132184 sqlite3_result_error(context, "not authorized", -1);
132194 sqlite3_result_error(context, zErrMsg, -1);
132208 double rErr; /* Error term for Kahan-Babushka-Neumaier summation */
132211 u8 approx; /* True if any non-integer value was input to the sum */
132216 ** Do one step of the Kahan-Babushka-Neumaier summation.
132227 volatile double s = pSum->rSum;
132230 pSum->rErr += (s - t) + r;
132232 pSum->rErr += (r - t) + s;
132234 pSum->rSum = t;
132241 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
132244 iBig = iVal - iSm;
132253 ** Initialize the Kahan-Babaska-Neumaier sum from a 64-bit integer
132259 if( iVal<=-4503599627370496LL || iVal>=+4503599627370496LL ){
132261 p->rSum = (double)(iVal - iSm);
132262 p->rErr = (double)iSm;
132264 p->rSum = (double)iVal;
132265 p->rErr = 0.0;
132287 p->cnt++;
132288 if( p->approx==0 ){
132290 kahanBabuskaNeumaierInit(p, p->iSum);
132291 p->approx = 1;
132294 i64 x = p->iSum;
132296 p->iSum = x;
132298 p->ovrfl = 1;
132299 kahanBabuskaNeumaierInit(p, p->iSum);
132300 p->approx = 1;
132308 p->ovrfl = 0;
132322 /* p is always non-NULL because sumStep() will have been called first
132325 assert( p->cnt>0 );
132326 p->cnt--;
132327 if( !p->approx ){
132328 if( sqlite3SubInt64(&p->iSum, sqlite3_value_int64(argv[0])) ){
132329 p->ovrfl = 1;
132330 p->approx = 1;
132335 kahanBabuskaNeumaierStepInt64(p, -iVal);
132341 kahanBabuskaNeumaierStep(p, -sqlite3_value_double(argv[0]));
132351 if( p && p->cnt>0 ){
132352 if( p->approx ){
132353 if( p->ovrfl ){
132354 sqlite3_result_error(context,"integer overflow",-1);
132355 }else if( !sqlite3IsOverflow(p->rErr) ){
132356 sqlite3_result_double(context, p->rSum+p->rErr);
132358 sqlite3_result_double(context, p->rSum);
132361 sqlite3_result_int64(context, p->iSum);
132368 if( p && p->cnt>0 ){
132370 if( p->approx ){
132371 r = p->rSum;
132372 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
132374 r = (double)(p->iSum);
132376 sqlite3_result_double(context, r/(double)p->cnt);
132384 if( p->approx ){
132385 r = p->rSum;
132386 if( !sqlite3IsOverflow(p->rErr) ) r += p->rErr;
132388 r = (double)(p->iSum);
132413 p->n++;
132420 ** expressed as a 32-bit integer. */
132421 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
132422 || p->n==sqlite3_aggregate_count(context) );
132428 sqlite3_result_int64(context, p ? p->n : 0);
132434 /* p is always non-NULL since countStep() will have been called first */
132436 p->n--;
132438 p->bInverse = 1;
132462 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
132463 }else if( pBest->flags ){
132470 ** sqlite3_user_data() function returns (void *)-1. For min() it
132483 pBest->db = sqlite3_context_db_handle(context);
132491 if( pRes->flags ){
132526 int nFirstSepLength; /* Used to detect separator length change */
132527 /* If pnSepLengths!=0, refs an array of inter-string separator lengths,
132529 ** (Hence, its slots in use number nAccum-1 between method calls.)
132530 ** If pnSepLengths==0, nFirstSepLength is the length used throughout.
132550 int firstTerm = pGCC->str.mxAlloc==0;
132551 pGCC->str.mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
132554 sqlite3_str_appendchar(&pGCC->str, 1, ',');
132558 pGCC->nFirstSepLength = 1;
132565 sqlite3_str_append(&pGCC->str, zSep, nSep);
132571 if( nSep != pGCC->nFirstSepLength || pGCC->pnSepLengths != 0 ){
132572 int *pnsl = pGCC->pnSepLengths;
132574 /* First separator length variation seen, start tracking them. */
132575 pnsl = (int*)sqlite3_malloc64((pGCC->nAccum+1) * sizeof(int));
132577 int i = 0, nA = pGCC->nAccum-1;
132578 while( i<nA ) pnsl[i++] = pGCC->nFirstSepLength;
132581 pnsl = (int*)sqlite3_realloc64(pnsl, pGCC->nAccum * sizeof(int));
132584 if( ALWAYS(pGCC->nAccum>0) ){
132585 pnsl[pGCC->nAccum-1] = nSep;
132587 pGCC->pnSepLengths = pnsl;
132589 sqlite3StrAccumSetError(&pGCC->str, SQLITE_NOMEM);
132596 pGCC->nFirstSepLength = sqlite3_value_bytes(argv[1]);
132598 pGCC->nAccum += 1;
132602 if( zVal ) sqlite3_str_append(&pGCC->str, zVal, nVal);
132617 /* pGCC is always non-NULL since groupConcatStep() will have always
132625 pGCC->nAccum -= 1;
132626 if( pGCC->pnSepLengths!=0 ){
132627 assert(pGCC->nAccum >= 0);
132628 if( pGCC->nAccum>0 ){
132629 nVS += *pGCC->pnSepLengths;
132630 memmove(pGCC->pnSepLengths, pGCC->pnSepLengths+1,
132631 (pGCC->nAccum-1)*sizeof(int));
132634 /* If removing single accumulated string, harmlessly over-do. */
132635 nVS += pGCC->nFirstSepLength;
132637 if( nVS>=(int)pGCC->str.nChar ){
132638 pGCC->str.nChar = 0;
132640 pGCC->str.nChar -= nVS;
132641 memmove(pGCC->str.zText, &pGCC->str.zText[nVS], pGCC->str.nChar);
132643 if( pGCC->str.nChar==0 ){
132644 pGCC->str.mxAlloc = 0;
132645 sqlite3_free(pGCC->pnSepLengths);
132646 pGCC->pnSepLengths = 0;
132657 sqlite3ResultStrAccum(context, &pGCC->str);
132659 sqlite3_free(pGCC->pnSepLengths);
132668 StrAccum *pAccum = &pGCC->str;
132669 if( pAccum->accError==SQLITE_TOOBIG ){
132671 }else if( pAccum->accError==SQLITE_NOMEM ){
132673 }else if( pGCC->nAccum>0 && pAccum->nChar==0 ){
132677 sqlite3_result_text(context, zText, pAccum->nChar, SQLITE_TRANSIENT);
132686 ** This routine does per-connection function registration. Most
132687 ** of the built-in functions above are part of the global function set.
132699 ** Re-register the built-in LIKE functions. The caseSensitive
132719 pDef->funcFlags |= flags;
132720 pDef->funcFlags &= ~SQLITE_FUNC_UNSAFE;
132729 ** LIKE-style function then return FALSE.
132732 ** operator if c is a string literal that is exactly one byte in length.
132745 assert( pExpr->op==TK_FUNCTION );
132747 if( !pExpr->x.pList ){
132750 nExpr = pExpr->x.pList->nExpr;
132752 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
132756 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
132764 memcpy(aWc, pDef->pUserData, 3);
132772 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
132774 if( pEscape->op!=TK_STRING ) return 0;
132776 zEscape = pEscape->u.zToken;
132783 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
132799 /* Extra math functions that require linking with -lm
132857 ** ln(X) - natural logarithm
132858 ** log(X) - log X base 10
132859 ** log10(X) - log X base 10
132860 ** log(B,X) - log X base B
132914 ** Implementation of 1-argument SQL math functions:
132916 ** exp(X) - Compute e to the X-th power
132936 ** Implementation of 2-argument SQL math functions:
132938 ** power(X,Y) - Compute X to the Y-th power
132961 ** Implementation of 0-argument pi() function.
132990 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
133027 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
133067 zVfs = pVfs->zName;
133077 sqlite3_str_appendf(pResult, ", vfs=%Q", pVfs ? pVfs->zName: 0);
133100 sqlite3_result_text(ctx, sqlite3_str_finish(pResult), -1, sqlite3_free);
133109 ** to the global function hash table. This occurs at start-time (as
133120 ** FuncDef.pHash elements at start-time. The elements of this array
133121 ** are read-only after initialization is complete.
133157 FUNCTION(min, -3, 0, 1, minmaxFunc ),
133160 FUNCTION(max, -3, 1, 1, minmaxFunc ),
133166 FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
133169 FUNCTION(printf, -1, 0, 0, printfFunc ),
133170 FUNCTION(format, -1, 0, 0, printfFunc ),
133172 FUNCTION(char, -1, 0, 0, charFunc ),
133176 FUNCTION(parseuri, -1, 0, 0, parseuriFunc ),
133187 FUNCTION(concat, -3, 0, 0, concatFunc ),
133188 FUNCTION(concat_ws, -4, 0, 0, concatwsFunc ),
133232 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
133271 INLINE_FUNC(coalesce, -4, INLINEFUNC_coalesce, 0 ),
133272 INLINE_FUNC(iif, -4, INLINEFUNC_iif, 0 ),
133273 INLINE_FUNC(if, -4, INLINEFUNC_iif, 0 ),
133283 #if 0 /* Enable to print out how the built-in functions are hashed */
133288 printf("FUNC-HASH %02d:", i);
133289 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
133290 int n = sqlite3Strlen30(p->zName);
133291 int h = p->zName[0] + n;
133292 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
133293 printf(" %s(%d)", p->zName, h);
133323 ** --------------------------
133405 ** ---------------
133407 ** Before coding an UPDATE or DELETE row operation, the code-generator
133412 ** accessed). No information is required by the code-generator before
133416 ** sqlite3FkRequired() - Test to see if FK processing is required.
133417 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
133421 ** --------------------------------------
133423 ** sqlite3FkCheck() - Check for foreign key violations.
133424 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
133425 ** sqlite3FkDelete() - Delete an FKey structure.
133430 ** -----------------------
133460 ** constraint to the parent table column stored in the left-most column
133462 ** child table column that corresponds to the second left-most column of
133481 ** then non-zero is returned, and a "foreign key mismatch" error loaded
133482 ** into pParse. If an OOM error occurs, non-zero is returned and the
133483 ** pParse->db->mallocFailed flag is set.
133494 int nCol = pFKey->nCol; /* Number of columns in parent key */
133495 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
133502 /* If this is a non-composite (single column) foreign key, check if it
133508 ** Non-composite foreign keys do not require the aiCol array.
133518 if( pParent->iPKey>=0 ){
133520 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zCnName, zKey) ){
133526 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
133531 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
133532 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
133544 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
133549 /* If zKey is non-NULL, then this foreign key was declared to
133555 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
133564 zDfltColl = sqlite3ColumnColl(&pParent->aCol[iCol]);
133566 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
133568 zIdxCol = pParent->aCol[iCol].zCnName;
133570 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
133571 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
133583 if( !pParse->disableTriggers ){
133585 "foreign key mismatch - \"%w\" referencing \"%w\"",
133586 pFKey->pFrom->zName, pFKey->zTo);
133588 sqlite3DbFree(pParse->db, aiCol);
133600 ** affected - once to "delete" the old row, and then again to "insert" the
133610 ** --------------------------------------------------------------------------
133635 int iCur = pParse->nTab - 1; /* Cursor number to use */
133639 (!pFKey->isDeferred
133640 && !(pParse->db->flags & SQLITE_DeferFKs)
133641 && !pParse->pToplevel
133642 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
133652 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
133655 for(i=0; i<pFKey->nCol; i++){
133656 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
133673 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
133678 ** to increment the constraint-counter (i.e. this is an INSERT operation),
133680 ** increment the constraint-counter. */
133681 if( pTab==pFKey->pFrom && nIncr==1 ){
133689 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
133693 int nCol = pFKey->nCol;
133696 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
133700 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
133705 ** to increment the constraint-counter (i.e. this is an INSERT operation),
133707 ** increment the constraint-counter.
133709 ** If any of the parent-key values are NULL, then the row cannot match
133711 ** of the parent-key values are NULL (at this point it is known that
133714 if( pTab==pFKey->pFrom && nIncr==1 ){
133717 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
133720 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
133721 pIdx->aiColumn[i]);
133722 assert( pIdx->aiColumn[i]>=0 );
133723 assert( aiCol[i]!=pTab->iPKey );
133724 if( pIdx->aiColumn[i]==pTab->iPKey ){
133735 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
133742 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
133743 && !pParse->pToplevel
133744 && !pParse->isMultiWrite
133754 if( nIncr>0 && pFKey->isDeferred==0 ){
133757 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
133782 sqlite3 *db = pParse->db;
133786 if( iCol>=0 && iCol!=pTab->iPKey ){
133787 pCol = &pTab->aCol[iCol];
133788 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
133789 pExpr->affExpr = pCol->affinity;
133791 if( zColl==0 ) zColl = db->pDfltColl->zName;
133794 pExpr->iTable = regBase;
133795 pExpr->affExpr = SQLITE_AFF_INTEGER;
133814 pExpr->y.pTab = pTab;
133815 pExpr->iTable = iCursor;
133816 pExpr->iColumn = iCol;
133825 ** code for an SQL UPDATE operation, this function may be called twice -
133828 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
133837 ** --------------------------------------------------------------------------
133859 sqlite3 *db = pParse->db; /* Database handle */
133867 assert( pIdx==0 || pIdx->pTable==pTab );
133868 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
133869 assert( pIdx!=0 || pFKey->nCol==1 );
133873 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
133879 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
133885 for(i=0; i<pFKey->nCol; i++){
133892 iCol = pIdx ? pIdx->aiColumn[i] : -1;
133894 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
133896 zCol = pFKey->pFrom->aCol[iCol].zCnName;
133916 if( pTab==pFKey->pFrom && nIncr>0 ){
133921 pLeft = exprTableRegister(pParse, pTab, regData, -1);
133922 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
133927 for(i=0; i<pIdx->nKeyCol; i++){
133928 i16 iCol = pIdx->aiColumn[i];
133931 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zCnName);
133949 if( pParse->nErr==0 ){
133951 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
133979 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
133985 ** and all of its sub-components.
133987 ** The Trigger structure or any of its sub-components may be allocated from
133992 TriggerStep *pStep = p->step_list;
133993 sqlite3ExprDelete(dbMem, pStep->pWhere);
133994 sqlite3ExprListDelete(dbMem, pStep->pExprList);
133995 sqlite3SelectDelete(dbMem, pStep->pSelect);
133996 sqlite3ExprDelete(dbMem, p->pWhen);
134008 Hash *pHash = &db->aDb[iDb].pSchema->tblHash;
134013 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
134014 fkTriggerDelete(db, pFKey->apTrigger[0]); pFKey->apTrigger[0] = 0;
134015 fkTriggerDelete(db, pFKey->apTrigger[1]); pFKey->apTrigger[1] = 0;
134038 sqlite3 *db = pParse->db;
134039 if( (db->flags&SQLITE_ForeignKeys) && IsOrdinaryTable(pTab) ){
134052 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134053 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
134060 pParse->disableTriggers = 1;
134062 pParse->disableTriggers = 0;
134073 if( (db->flags & SQLITE_DeferFKs)==0 ){
134094 ** is set to -1). If the rowid column is modified by the UPDATE statement
134095 ** the bChngRowid argument is non-zero.
134107 for(i=0; i<p->nCol; i++){
134108 int iChildKey = p->aCol[i].iFrom;
134110 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
134121 ** is set to -1). If the rowid column is modified by the UPDATE statement
134122 ** the bChngRowid argument is non-zero.
134134 for(i=0; i<p->nCol; i++){
134135 char *zKey = p->aCol[i].zCol;
134137 for(iKey=0; iKey<pTab->nCol; iKey++){
134138 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
134139 Column *pCol = &pTab->aCol[iKey];
134141 if( 0==sqlite3StrICmp(pCol->zCnName, zKey) ) return 1;
134142 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
134158 if( pTop->pTriggerPrg ){
134159 Trigger *p = pTop->pTriggerPrg->pTrigger;
134160 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
134161 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
134163 assert( (pTop->db->flags & SQLITE_FkNoAction)==0 );
134176 ** first register in an array of (pTab->nCol+1) registers containing the
134182 ** first register of an array of (pTab->nCol+1) registers containing the new
134198 sqlite3 *db = pParse->db; /* Database handle */
134202 int isIgnoreErrors = pParse->disableTriggers;
134204 /* Exactly one of regOld and regNew should be non-zero. */
134207 /* If foreign-keys are disabled, this function is a no-op. */
134208 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
134211 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134212 zDb = db->aDb[iDb].zDbSName;
134216 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pFKey->pNextFrom){
134226 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
134236 if( pParse->disableTriggers ){
134237 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
134239 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
134243 if( !isIgnoreErrors || db->mallocFailed ) return;
134250 ** FK counter for each row of the current table with non-NULL keys.
134253 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
134254 for(i=0; i<pFKey->nCol; i++){
134256 iFromCol = pFKey->aCol[i].iFrom;
134257 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
134260 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
134264 assert( pFKey->nCol==1 || (aiFree && pIdx) );
134269 iCol = pFKey->aCol[0].iFrom;
134272 for(i=0; i<pFKey->nCol; i++){
134273 if( aiCol[i]==pTab->iPKey ){
134274 aiCol[i] = -1;
134276 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
134281 if( db->xAuth ){
134283 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zCnName;
134284 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
134290 /* Take a shared-cache advisory read-lock on the parent table. Allocate
134293 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
134294 pParse->nTab++;
134300 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
134319 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
134328 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
134329 && !pParse->pToplevel && !pParse->isMultiWrite
134338 if( !isIgnoreErrors || db->mallocFailed ) return;
134341 assert( aiCol || pFKey->nCol==1 );
134347 SrcItem *pItem = pSrc->a;
134348 pItem->pSTab = pFKey->pFrom;
134349 pItem->zName = pFKey->pFrom->zName;
134350 pItem->pSTab->nTabRef++;
134351 pItem->iCursor = pParse->nTab++;
134354 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
134357 int eAction = pFKey->aAction[aChange!=0];
134358 if( (db->flags & SQLITE_FkNoAction) ) eAction = OE_None;
134364 ** So do not set the "may-abort" flag in this case.
134367 ** may-abort flag will eventually be set on this statement anyway
134378 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
134382 pItem->zName = 0;
134400 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
134403 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134404 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
134406 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
134410 for(i=0; i<pIdx->nKeyCol; i++){
134411 assert( pIdx->aiColumn[i]>=0 );
134412 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
134427 ** entry in the aChange[] array is set to -1. If the column is modified,
134432 ** non-zero. If there is no foreign key related processing, this function
134448 int *aChange, /* Non-NULL for UPDATE operations */
134453 if( pParse->db->flags&SQLITE_ForeignKeys && IsOrdinaryTable(pTab) ){
134458 bHaveFK = (sqlite3FkReferences(pTab) || pTab->u.tab.pFKey);
134465 for(p=pTab->u.tab.pFKey; p; p=p->pNextFrom){
134467 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
134473 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
134475 if( (pParse->db->flags & SQLITE_FkNoAction)==0
134476 && p->aAction[1]!=OE_None
134490 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
134498 ** require no special handling by the triggers sub-system, code for them is
134521 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
134523 sqlite3 *db = pParse->db; /* Database handle */
134528 action = pFKey->aAction[iAction];
134529 if( (db->flags & SQLITE_FkNoAction) ) action = OE_None;
134530 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
134533 pTrigger = pFKey->apTrigger[iAction];
134537 int nFrom; /* Length in bytes of zFrom */
134539 int *aiCol = 0; /* child table cols -> parent key cols */
134548 assert( aiCol || pFKey->nCol==1 );
134550 for(i=0; i<pFKey->nCol; i++){
134558 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
134560 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
134561 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
134563 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zCnName);
134564 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zCnName);
134602 Column *pCol = pFKey->pFrom->aCol + iFromCol;
134604 if( pCol->colFlags & COLFLAG_GENERATED ){
134605 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
134606 testcase( pCol->colFlags & COLFLAG_STORED );
134609 pDflt = sqlite3ColumnExpr(pFKey->pFrom, pCol);
134625 zFrom = pFKey->pFrom->zName;
134629 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
134636 pRaise->affExpr = OE_Abort;
134640 assert( pSrc->nSrc==1 );
134641 pSrc->a[0].zName = sqlite3DbStrDup(db, zFrom);
134642 assert( pSrc->a[0].fg.fixedSchema==0 && pSrc->a[0].fg.isSubquery==0 );
134643 pSrc->a[0].u4.zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
134660 nFrom + 1 /* Space for pStep->zTarget */
134663 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
134664 pStep->zTarget = (char *)&pStep[1];
134665 memcpy((char *)pStep->zTarget, zFrom, nFrom);
134667 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
134668 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
134669 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
134672 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
134676 /* Re-enable the lookaside buffer, if it was disabled earlier. */
134683 if( db->mallocFailed==1 ){
134692 pStep->op = TK_SELECT;
134696 pStep->op = TK_DELETE;
134701 pStep->op = TK_UPDATE;
134703 pStep->pTrig = pTrigger;
134704 pTrigger->pSchema = pTab->pSchema;
134705 pTrigger->pTabSchema = pTab->pSchema;
134706 pFKey->apTrigger[iAction] = pTrigger;
134707 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
134720 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
134725 /* If foreign-key support is enabled, iterate through all FKs that
134728 ** trigger sub-program. */
134729 if( pParse->db->flags&SQLITE_ForeignKeys ){
134731 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
134751 FKey *pNext; /* Copy of pFKey->pNextFrom */
134755 for(pFKey=pTab->u.tab.pFKey; pFKey; pFKey=pNext){
134756 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
134759 if( db->pnBytesFreed==0 ){
134760 if( pFKey->pPrevTo ){
134761 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
134763 const char *z = (pFKey->pNextTo ? pFKey->pNextTo->zTo : pFKey->zTo);
134764 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, pFKey->pNextTo);
134766 if( pFKey->pNextTo ){
134767 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
134771 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
134774 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
134778 fkTriggerDelete(db, pFKey->apTrigger[0]);
134779 fkTriggerDelete(db, pFKey->apTrigger[1]);
134782 pNext = pFKey->pNextFrom;
134824 assert( pParse->pVdbe!=0 );
134825 v = pParse->pVdbe;
134827 if( !pParse->db->noSharedCache ){
134828 sqlite3TableLock(pParse, iDb, pTab->tnum,
134829 (opcode==OP_OpenWrite)?1:0, pTab->zName);
134832 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
134833 VdbeComment((v, "%s", pTab->zName));
134837 assert( pPk->tnum==pTab->tnum || CORRUPT_DB );
134838 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
134840 VdbeComment((v, "%s", pTab->zName));
134850 ** ------------------------------
134874 Table *pTab = pIdx->pTable;
134875 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
134876 if( !pIdx->zColAff ){
134880 for(n=0; n<pIdx->nColumn; n++){
134881 i16 x = pIdx->aiColumn[n];
134884 aff = pTab->aCol[x].affinity;
134889 assert( pIdx->bHasExpr );
134890 assert( pIdx->aColExpr!=0 );
134891 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
134895 pIdx->zColAff[n] = aff;
134897 pIdx->zColAff[n] = 0;
134898 return pIdx->zColAff;
134901 if( !pIdx->zColAff ) return computeIndexAffStr(db, pIdx);
134902 return pIdx->zColAff;
134913 zColAff = (char *)sqlite3DbMallocRaw(db, pTab->nCol+1);
134916 for(i=j=0; i<pTab->nCol; i++){
134917 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
134918 zColAff[j++] = pTab->aCol[i].affinity;
134922 zColAff[j--] = 0;
134932 ** For ordinary (legacy, non-strict) tables:
134933 ** -----------------------------------------
134939 ** which were then optimized out) then this routine becomes a no-op.
134949 ** --------- ---------------
134957 ** ------------------
134971 if( pTab->tabFlags & TF_Strict ){
134980 assert( pPrev->opcode==OP_MakeRecord || sqlite3VdbeDb(v)->mallocFailed );
134981 pPrev->opcode = OP_TypeCheck;
134982 sqlite3VdbeAddOp3(v, OP_MakeRecord, pPrev->p1, pPrev->p2, pPrev->p3);
134985 sqlite3VdbeAddOp2(v, OP_TypeCheck, iReg, pTab->nNVCol);
134990 zColAff = pTab->zColAff;
134997 pTab->zColAff = zColAff;
135005 assert( sqlite3VdbeGetLastOp(v)->opcode==OP_MakeRecord
135006 || sqlite3VdbeDb(v)->mallocFailed );
135007 sqlite3VdbeChangeP4(v, -1, zColAff, i);
135013 ** Return non-zero if the table pTab in database iDb or any of its indices
135023 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
135029 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
135031 Pgno tnum = pOp->p2;
135032 if( tnum==pTab->tnum ){
135035 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
135036 if( tnum==pIndex->tnum ){
135042 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
135043 assert( pOp->p4.pVtab!=0 );
135044 assert( pOp->p4type==P4_VTAB );
135056 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
135057 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
135058 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
135082 assert( pTab->tabFlags & TF_HasGenerated );
135083 testcase( pTab->tabFlags & TF_HasVirtual );
135084 testcase( pTab->tabFlags & TF_HasStored );
135089 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
135090 if( (pTab->tabFlags & TF_HasStored)!=0 ){
135091 pOp = sqlite3VdbeGetLastOp(pParse->pVdbe);
135092 if( pOp->opcode==OP_Affinity ){
135094 ** columns. '@' is the no-op affinity and those columns have not
135097 char *zP4 = pOp->p4.z;
135099 assert( pOp->p4type==P4_DYNAMIC );
135101 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
135104 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
135109 }else if( pOp->opcode==OP_TypeCheck ){
135113 pOp->p3 = 1;
135118 ** this is a two-pass algorithm. On the first pass, mark all generated
135121 for(i=0; i<pTab->nCol; i++){
135122 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
135123 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
135124 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
135125 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
135134 /* On the second pass, compute the value of each NOT-AVAILABLE column.
135139 pParse->iSelfTab = -iRegStore;
135143 for(i=0; i<pTab->nCol; i++){
135144 Column *pCol = pTab->aCol + i;
135145 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
135147 pCol->colFlags |= COLFLAG_BUSY;
135150 pCol->colFlags &= ~COLFLAG_BUSY;
135156 assert( pCol->colFlags & COLFLAG_GENERATED );
135159 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
135164 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zCnName);
135166 pParse->iSelfTab = 0;
135201 assert( pParse->db->aDb[iDb].pSchema!=0 );
135202 if( (pTab->tabFlags & TF_Autoincrement)!=0
135203 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
135207 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
135211 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
135215 || pSeqTab->nCol!=2
135217 pParse->nErr++;
135218 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
135222 pInfo = pToplevel->pAinc;
135223 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
135225 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
135227 testcase( pParse->earlyCleanup );
135228 if( pParse->db->mallocFailed ) return 0;
135229 pInfo->pNext = pToplevel->pAinc;
135230 pToplevel->pAinc = pInfo;
135231 pInfo->pTab = pTab;
135232 pInfo->iDb = iDb;
135233 pToplevel->nMem++; /* Register to hold name of table */
135234 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
135235 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
135237 memId = pInfo->regCtr;
135248 sqlite3 *db = pParse->db; /* The database connection */
135251 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
135253 /* This routine is never called during trigger-generation. It is
135254 ** only called from the top-level */
135255 assert( pParse->pTriggerTab==0 );
135259 for(p = pParse->pAinc; p; p = p->pNext){
135276 pDb = &db->aDb[p->iDb];
135277 memId = p->regCtr;
135278 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
135279 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
135280 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
135286 aOp[3].p1 = memId-1;
135295 if( pParse->nTab==0 ) pParse->nTab = 1;
135309 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
135322 Vdbe *v = pParse->pVdbe;
135323 sqlite3 *db = pParse->db;
135326 for(p = pParse->pAinc; p; p = p->pNext){
135336 Db *pDb = &db->aDb[p->iDb];
135338 int memId = p->regCtr;
135341 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
135344 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
135349 aOp[2].p1 = memId-1;
135358 if( pParse->pAinc ) autoIncrementEnd(pParse);
135363 ** above are all no-ops
135371 ** that was able to use the co-routine optimization, finish coding the
135372 ** co-routine.
135375 if( ALWAYS(pVal) && pVal->pSrc->nSrc>0 ){
135376 SrcItem *pItem = &pVal->pSrc->a[0];
135377 assert( (pItem->fg.isSubquery && pItem->u4.pSubq!=0) || pParse->nErr );
135378 if( pItem->fg.isSubquery ){
135379 sqlite3VdbeEndCoroutine(pParse->pVdbe, pItem->u4.pSubq->regReturn);
135380 sqlite3VdbeJumpHere(pParse->pVdbe, pItem->u4.pSubq->addrFillSub - 1);
135386 ** Return true if all expressions in the expression-list passed as the
135391 for(ii=0; ii<pRow->nExpr; ii++){
135392 if( 0==sqlite3ExprIsConstant(pParse, pRow->a[ii].pExpr) ) return 0;
135398 ** Return true if all expressions in the expression-list passed as the
135404 for(ii=0; ii<pRow->nExpr; ii++){
135405 Expr *pExpr = pRow->a[ii].pExpr;
135406 assert( pExpr->op!=TK_RAISE );
135407 assert( pExpr->affExpr==0 );
135416 ** rows of a multi-row VALUES clause. Argument pLeft is the part of
135421 ** There are two ways in which this may be achieved - by incremental
135422 ** coding of a co-routine (the "co-routine" method) or by returning a
135430 ** When the co-routine method is used, each row that will be returned
135431 ** by the VALUES clause is coded into part of a co-routine as it is
135435 ** Select object to read co-routine
135438 ** The co-routine method is used in most cases. Exceptions are:
135446 ** This will not work, as the co-routine uses a hard-coded register
135453 ** a co-routine is not possible.
135455 ** c) There are non-constant expressions in the VALUES clause (e.g.
135456 ** the VALUES clause is part of a correlated sub-query).
135467 if( pParse->bHasWith /* condition (a) above */
135468 || pParse->db->init.busy /* condition (b) above */
135470 || (pLeft->pSrc->nSrc==0 &&
135471 exprListIsNoAffinity(pParse,pLeft->pEList)==0) /* condition (d) above */
135474 /* The co-routine method cannot be used. Fall back to UNION ALL. */
135477 if( pLeft->pSrc->nSrc ){
135480 }else if( pLeft->pPrior ){
135482 f = (f & pLeft->selFlags);
135485 pLeft->selFlags &= ~(u32)SF_MultiValue;
135487 pSelect->op = TK_ALL;
135488 pSelect->pPrior = pLeft;
135492 SrcItem *p = 0; /* SrcItem that reads from co-routine */
135494 if( pLeft->pSrc->nSrc==0 ){
135495 /* Co-routine has not yet been started and the special Select object
135496 ** that accesses the co-routine has not yet been created. This block
135503 if( (pParse->db->mDbFlags & DBFLAG_SchemaKnownOk)==0 ){
135510 pRet->pSrc->nSrc = 1;
135511 pRet->pPrior = pLeft->pPrior;
135512 pRet->op = pLeft->op;
135513 if( pRet->pPrior ) pRet->selFlags |= SF_Values;
135514 pLeft->pPrior = 0;
135515 pLeft->op = TK_SELECT;
135516 assert( pLeft->pNext==0 );
135517 assert( pRet->pNext==0 );
135518 p = &pRet->pSrc->a[0];
135519 p->fg.viaCoroutine = 1;
135520 p->iCursor = -1;
135521 assert( !p->fg.isIndexedBy && !p->fg.isTabFunc );
135522 p->u1.nRow = 2;
135524 pSubq = p->u4.pSubq;
135525 pSubq->addrFillSub = sqlite3VdbeCurrentAddr(v) + 1;
135526 pSubq->regReturn = ++pParse->nMem;
135528 pSubq->regReturn, 0, pSubq->addrFillSub);
135529 sqlite3SelectDestInit(&dest, SRT_Coroutine, pSubq->regReturn);
135531 /* Allocate registers for the output of the co-routine. Do so so
135533 ** used by the co-routine. This allows the code in sqlite3Insert()
135535 ** of the co-routine to a separate array for processing. */
135536 dest.iSdst = pParse->nMem + 3;
135537 dest.nSdst = pLeft->pEList->nExpr;
135538 pParse->nMem += 2 + dest.nSdst;
135540 pLeft->selFlags |= SF_MultiValue;
135542 pSubq->regResult = dest.iSdst;
135543 assert( pParse->nErr || dest.iSdst>0 );
135548 p = &pLeft->pSrc->a[0];
135549 assert( !p->fg.isTabFunc && !p->fg.isIndexedBy );
135550 p->u1.nRow++;
135553 if( pParse->nErr==0 ){
135556 assert( p->fg.isSubquery );
135557 pSubq = p->u4.pSubq;
135559 assert( pSubq->pSelect!=0 );
135560 assert( pSubq->pSelect->pEList!=0 );
135561 if( pSubq->pSelect->pEList->nExpr!=pRow->nExpr ){
135562 sqlite3SelectWrongNumTermsError(pParse, pSubq->pSelect);
135564 sqlite3ExprCodeExprList(pParse, pRow, pSubq->regResult, 0, 0);
135565 sqlite3VdbeAddOp1(pParse->pVdbe, OP_Yield, pSubq->regReturn);
135568 sqlite3ExprListDelete(pParse->db, pRow);
135591 ** then a list of all (non-hidden) columns for the table is substituted.
135596 ** first two forms shown above. A VALUES clause is really just short-hand
135602 ** insert with data coming from a single-row VALUES clause, the code executes
135603 ** once straight down through. Pseudo-code follows (we call this
135615 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
135639 ** X <- A
135647 ** end-coroutine X
135660 ** X <- A
135668 ** end co-routine R
135697 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
135727 db = pParse->db;
135728 assert( db->pParse==pParse );
135729 if( pParse->nErr ){
135732 assert( db->mallocFailed==0 );
135739 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
135740 pList = pSelect->pEList;
135741 pSelect->pEList = 0;
135748 assert( pTabList->nSrc==1 );
135753 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
135754 assert( iDb<db->nDb );
135755 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
135756 db->aDb[iDb].zDbSName) ){
135781 sqlite3TreeViewInsert(pParse->pWith, pTabList, pColumn, pSelect, pList,
135787 ** ViewGetColumnNames() is a no-op if pTab is not a view.
135793 /* Cannot insert into a read-only table.
135803 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
135835 regRowid = regIns = pParse->nMem+1;
135836 pParse->nMem += pTab->nCol + 1;
135839 pParse->nMem++;
135852 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
135853 ** loop, if ipkColumn==(-1), that means that integer primary key
135862 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
135864 aTabColMap = sqlite3DbMallocZero(db, pTab->nCol*sizeof(int));
135866 for(i=0; i<pColumn->nId; i++){
135867 j = sqlite3ColumnIndex(pTab, pColumn->a[i].zName);
135871 if( j==pTab->iPKey ){
135875 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
135878 pTab->aCol[j].zCnName);
135883 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
135888 pTabList->a, pColumn->a[i].zName);
135889 pParse->checkSchema = 1;
135897 ** is coming from a SELECT statement, then generate a co-routine that
135899 ** co-routine is the common header to the 3rd and 4th templates.
135902 /* Data is coming from a SELECT or from a multi-row VALUES clause.
135903 ** Generate a co-routine to run the SELECT. */
135906 if( pSelect->pSrc->nSrc==1
135907 && pSelect->pSrc->a[0].fg.viaCoroutine
135908 && pSelect->pPrior==0
135910 SrcItem *pItem = &pSelect->pSrc->a[0];
135912 assert( pItem->fg.isSubquery );
135913 pSubq = pItem->u4.pSubq;
135914 dest.iSDParm = pSubq->regReturn;
135915 regFromSelect = pSubq->regResult;
135916 assert( pSubq->pSelect!=0 );
135917 assert( pSubq->pSelect->pEList!=0 );
135918 nColumn = pSubq->pSelect->pEList->nExpr;
135920 if( bIdListInOrder && nColumn==pTab->nCol ){
135922 regRowid = regData - 1;
135923 regIns = regRowid - (IsVirtual(pTab) ? 1 : 0);
135926 int addrTop; /* Top of the co-routine */
135927 int regYield = ++pParse->nMem;
135932 dest.nSdst = pTab->nCol;
135935 assert( db->pParse==pParse );
135936 if( rc || pParse->nErr ) goto insert_cleanup;
135937 assert( db->mallocFailed==0 );
135939 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
135940 assert( pSelect->pEList );
135941 nColumn = pSelect->pEList->nExpr;
135972 srcTab = pParse->nTab++;
135987 ** single-row VALUES clause
135992 srcTab = -1;
135995 nColumn = pList->nExpr;
136009 ipkColumn = pTab->iPKey;
136011 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
136012 testcase( pTab->tabFlags & TF_HasVirtual );
136013 testcase( pTab->tabFlags & TF_HasStored );
136014 for(i=ipkColumn-1; i>=0; i--){
136015 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
136016 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
136017 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
136018 ipkColumn--;
136030 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
136031 for(i=0; i<pTab->nCol; i++){
136032 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
136035 if( nColumn!=(pTab->nCol-nHidden) ){
136038 pTabList->a, pTab->nCol-nHidden, nColumn);
136042 if( pColumn!=0 && nColumn!=pColumn->nId ){
136043 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
136049 if( (db->flags & SQLITE_CountRows)!=0
136050 && !pParse->nested
136051 && !pParse->pTriggerTab
136052 && !pParse->bReturning
136054 regRowCount = ++pParse->nMem;
136061 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
136067 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
136069 aRegIdx[i] = ++pParse->nMem;
136070 pParse->nMem += pIdx->nColumn;
136072 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
136079 pTab->zName);
136086 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
136089 pTabList->a[0].iCursor = iDataCur;
136092 pNx->pUpsertSrc = pTabList;
136093 pNx->regData = regData;
136094 pNx->iDataCur = iDataCur;
136095 pNx->iIdxCur = iIdxCur;
136096 if( pNx->pUpsertTarget ){
136101 pNx = pNx->pNextUpsert;
136129 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
136133 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
136135 ** the value does not get overwritten by a NULL at tag-20191021-002. */
136149 for(i=0; i<pTab->nCol; i++, iRegStore++){
136153 if( i==pTab->iPKey ){
136154 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
136157 ** NULL - we cannot optimize further by skipping the column completely */
136161 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
136167 iRegStore--;
136171 ** triggers, the slots used for stored columns will be OP_Copy-ed
136182 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136189 assert( j>=0 && j<=pColumn->nId );
136194 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136198 k = j - 1;
136202 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
136206 k = i - nHidden;
136216 Expr *pX = pList->a[k].pExpr;
136230 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
136236 ** not happened yet) so we substitute a rowid of -1
136239 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
136247 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
136250 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
136256 assert( pTab->nNVCol>0 || pParse->nErr>0 );
136257 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
136264 if( pTab->tabFlags & TF_HasGenerated ){
136265 testcase( pTab->tabFlags & TF_HasVirtual );
136266 testcase( pTab->tabFlags & TF_HasStored );
136282 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
136284 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
136297 /* Rowid already initialized at tag-20191021-001 */
136299 Expr *pIpk = pList->a[ipkColumn].pExpr;
136300 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
136304 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
136335 if( pTab->tabFlags & TF_HasGenerated ){
136347 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
136358 if( db->flags & SQLITE_ForeignKeys ){
136376 }else if( pParse->bReturning ){
136378 ** constant value -1, in case one or more of the returned expressions
136380 sqlite3VdbeAddOp2(v, OP_Integer, -1, regRowid);
136393 pTab, regData-2-pTab->nCol, onError, endOfLoop);
136410 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
136411 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
136425 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
136464 ** Meanings of bits in of pWalker->eCode for
136471 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
136476 if( pExpr->op==TK_COLUMN ){
136477 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
136478 if( pExpr->iColumn>=0 ){
136479 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
136480 pWalker->eCode |= CKCNSTRNT_COLUMN;
136483 pWalker->eCode |= CKCNSTRNT_ROWID;
136490 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
136499 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
136500 ** The operation of this routine is the same - return true if an only if
136528 ** the indexes of a table in the order provided in the Table->pIndex list.
136529 ** However, sometimes (rarely - when there is an upsert) it wants to visit
136563 assert( pIter->i==0 );
136564 if( pIter->eType ){
136565 *pIx = pIter->u.ax.aIdx[0].ix;
136566 return pIter->u.ax.aIdx[0].p;
136569 return pIter->u.lx.pIdx;
136575 if( pIter->eType ){
136576 int i = ++pIter->i;
136577 if( i>=pIter->u.ax.nIdx ){
136581 *pIx = pIter->u.ax.aIdx[i].ix;
136582 return pIter->u.ax.aIdx[i].p;
136585 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
136586 return pIter->u.lx.pIdx;
136596 ** pTab->nCol+1 registers in this range. The first register (the one
136623 ** at pTab->pIndex.
136625 ** (2019-05-07) The generated code also creates a new record for the
136627 ** register identified by aRegIdx[nIdx] - in other words in the first
136638 ** for the first index in the pTab->pIndex list. Cursors for other indices
136639 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
136647 ** --------------- ---------- ----------------------------------------
136677 ** Or if overrideError==OE_Default, then the pParse->onError parameter
136678 ** is used. Or if pParse->onError==OE_Default then the onError value
136689 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
136723 db = pParse->db;
136724 v = pParse->pVdbe;
136727 nCol = pTab->nCol;
136738 nPkField = pPk->nKeyCol;
136747 if( pTab->tabFlags & TF_HasNotNull ){
136754 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
136755 int isGenerated; /* non-zero if column is generated */
136756 onError = pCol->notNull;
136758 if( i==pTab->iPKey ){
136761 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
136777 || pCol->iDflt==0 /* REPLACE is ABORT if no DEFAULT value */
136779 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
136780 testcase( pCol->colFlags & COLFLAG_STORED );
136781 testcase( pCol->colFlags & COLFLAG_GENERATED );
136797 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
136809 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
136810 pCol->zCnName);
136811 testcase( zMsg==0 && db->mallocFailed==0 );
136836 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
136844 } /* end of 2-pass loop */
136845 } /* end if( has-not-null-constraints ) */
136850 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
136851 ExprList *pCheck = pTab->pCheck;
136852 pParse->iSelfTab = -(regNewData+1);
136854 for(i=0; i<pCheck->nExpr; i++){
136857 Expr *pExpr = pCheck->a[i].pExpr;
136872 if( !db->mallocFailed ){
136879 char *zName = pCheck->a[i].zEName;
136880 assert( zName!=0 || pParse->db->mallocFailed );
136881 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
136888 pParse->iSelfTab = 0;
136905 ** 2018-08-14: Ticket https://sqlite.org/src/info/908f001483982c43
136923 sIdxIter.u.lx.pIdx = pTab->pIndex;
136925 if( pUpsert->pUpsertTarget==0 ){
136926 /* There is just on ON CONFLICT clause and it has no constraint-target */
136927 assert( pUpsert->pNextUpsert==0 );
136928 if( pUpsert->isDoUpdate==0 ){
136929 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
136937 }else if( pTab->pIndex!=0 ){
136945 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
136954 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
136955 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
136956 if( pTerm->pUpsertTarget==0 ) break;
136957 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
136959 pIdx = pTab->pIndex;
136960 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
136961 pIdx = pIdx->pNext;
136970 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
137000 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
137006 if( db->flags&SQLITE_RecTriggers ){
137016 regTrigCnt = ++pParse->nMem;
137031 onError = pTab->keyConf;
137042 if( pUpsertClause->isDoUpdate==0 ){
137063 && pTab->pIndex /* There exist other constraints */
137102 ** recursive-triggers flag is set, call GenerateRowDelete() to
137104 ** the triggers and remove both the table and index b-tree entries.
137106 ** Otherwise, if there are no triggers or the recursive-triggers
137108 ** GenerateRowIndexDelete(). This removes the index b-tree entries
137109 ** only. The table b-tree entry will be replaced by the new entry
137126 regNewData, 1, 0, OE_Replace, 1, -1);
137132 /* This OP_Delete opcode fires the pre-update-hook only. It does
137133 ** not modify the b-tree. It is more efficient to let the coming
137139 if( pTab->pIndex ){
137141 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
137164 sqlite3VdbeJumpHere(v, ipkTop-1);
137197 VdbeNoopComment((v, "prep index %s", pIdx->zName));
137202 if( pIdx->pPartIdxWhere ){
137204 pParse->iSelfTab = -(regNewData+1);
137205 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
137207 pParse->iSelfTab = 0;
137214 for(i=0; i<pIdx->nColumn; i++){
137215 int iField = pIdx->aiColumn[i];
137218 pParse->iSelfTab = -(regNewData+1);
137219 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
137220 pParse->iSelfTab = 0;
137221 VdbeComment((v, "%s column %d", pIdx->zName, i));
137222 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
137230 VdbeComment((v, "%s", pTab->aCol[iField].zCnName));
137233 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
137234 VdbeComment((v, "for %s", pIdx->zName));
137236 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
137237 sqlite3SetMakeRecordP5(v, pIdx->pTable);
137240 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
137252 onError = pIdx->onError;
137265 if( pUpsertClause->isDoUpdate==0 ){
137280 ** must be explicitly deleted in order to ensure any pre-update hook
137284 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
137287 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
137289 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
137290 (0==pTab->u.tab.pFKey && 0==sqlite3FkReferences(pTab)))
137301 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
137309 ** is different from old-rowid */
137318 ** store it in registers regR..regR+nPk-1 */
137320 for(i=0; i<pPk->nKeyCol; i++){
137321 assert( pPk->aiColumn[i]>=0 );
137322 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
137324 VdbeComment((v, "%s.%s", pTab->zName,
137325 pTab->aCol[pPk->aiColumn[i]].zCnName));
137336 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
137340 for(i=0; i<pPk->nKeyCol; i++){
137341 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
137342 x = pPk->aiColumn[i];
137344 if( i==(pPk->nKeyCol-1) ){
137388 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
137389 assert( nConflictCk>0 || db->mallocFailed );
137417 if( pIdx->pPartIdxWhere ){
137420 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
137424 ** the constraint-ok jump destination to be the address of
137445 nConflictCk--;
137500 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
137514 ** to be the number of columns in table pTab that must not be NULL-trimmed.
137516 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
137522 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
137523 if( pTab->pSchema->file_format<2 ) return;
137525 for(i=pTab->nCol-1; i>0; i--){
137526 if( pTab->aCol[i].iDflt!=0 ) break;
137527 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
137536 ** PK index. This function adds code to invoke the pre-update hook,
137546 Vdbe *v = pParse->pVdbe;
137549 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
137589 v = pParse->pVdbe;
137592 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
137594 assert( pIdx->onError!=OE_Replace
137595 || pIdx->pNext==0
137596 || pIdx->pNext->onError==OE_Replace );
137598 if( pIdx->pPartIdxWhere ){
137612 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
137616 if( pParse->nested ){
137629 if( !pParse->nested ){
137645 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
137651 ** pTab->pIndex list.
137653 ** If pTab is a virtual table, then this routine is a no-op and the
137677 /* This routine is a no-op for virtual tables. Leave the output
137680 *piDataCur = *piIdxCur = -999;
137683 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
137684 v = pParse->pVdbe;
137686 if( iBase<0 ) iBase = pParse->nTab;
137691 }else if( pParse->db->noSharedCache==0 ){
137692 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
137695 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
137697 assert( pIdx->pSchema==pTab->pSchema );
137703 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
137706 VdbeComment((v, "%s", pIdx->zName));
137709 if( iBase>pParse->nTab ) pParse->nTab = iBase;
137718 ** purposes only - to make sure the transfer optimization really
137740 assert( pDest->pTable!=pSrc->pTable );
137741 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
137744 if( pDest->onError!=pSrc->onError ){
137747 for(i=0; i<pSrc->nKeyCol; i++){
137748 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
137751 if( pSrc->aiColumn[i]==XN_EXPR ){
137752 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
137753 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
137754 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
137758 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
137761 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
137765 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
137783 ** There are lots of rules for determining compatibility - see comments
137788 ** is empty - a factor that can only be determined at run-time. In that
137805 sqlite3 *db = pParse->db;
137809 SrcItem *pItem; /* An element of pSelect->pSrc */
137822 if( pParse->pWith || pSelect->pWith ){
137834 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
137837 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
137838 if( pSelect->pSrc->nSrc!=1 ){
137841 if( pSelect->pSrc->a[0].fg.isSubquery ){
137844 if( pSelect->pWhere ){
137847 if( pSelect->pOrderBy ){
137852 if( pSelect->pGroupBy ){
137855 if( pSelect->pLimit ){
137858 if( pSelect->pPrior ){
137861 if( pSelect->selFlags & SF_Distinct ){
137864 pEList = pSelect->pEList;
137866 if( pEList->nExpr!=1 ){
137869 assert( pEList->a[0].pExpr );
137870 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
137878 pItem = pSelect->pSrc->a;
137883 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
137893 if( pDest->nCol!=pSrc->nCol ){
137896 if( pDest->iPKey!=pSrc->iPKey ){
137899 if( (pDest->tabFlags & TF_Strict)!=0 && (pSrc->tabFlags & TF_Strict)==0 ){
137900 return 0; /* Cannot feed from a non-strict into a strict table */
137902 for(i=0; i<pDest->nCol; i++){
137903 Column *pDestCol = &pDest->aCol[i];
137904 Column *pSrcCol = &pSrc->aCol[i];
137906 if( (db->mDbFlags & DBFLAG_Vacuum)==0
137907 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
137929 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
137930 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
137931 return 0; /* Both columns have the same generated-column type */
137937 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
137940 sqlite3ColumnExpr(pDest, pDestCol), -1)!=0 ){
137941 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
137942 testcase( pDestCol->colFlags & COLFLAG_STORED );
137947 if( pDestCol->affinity!=pSrcCol->affinity ){
137954 if( pDestCol->notNull && !pSrcCol->notNull ){
137958 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
137961 assert( pDestExpr==0 || pDestExpr->op==TK_SPAN );
137963 assert( pSrcExpr==0 || pSrcExpr->op==TK_SPAN );
137966 || (pDestExpr!=0 && strcmp(pDestExpr->u.zToken,
137967 pSrcExpr->u.zToken)!=0)
137973 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
137977 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
137983 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
137988 return 0; /* Corrupt schema - two indexes on the same btree */
137992 if( pDest->pCheck
137993 && (db->mDbFlags & DBFLAG_Vacuum)==0
137994 && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1)
138008 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->u.tab.pFKey!=0 ){
138012 if( (db->flags & SQLITE_CountRows)!=0 ){
138023 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
138026 iSrc = pParse->nTab++;
138027 iDest = pParse->nTab++;
138034 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
138035 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
138064 if( pDest->iPKey>=0 ){
138066 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
138074 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
138078 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
138081 if( db->mDbFlags & DBFLAG_Vacuum ){
138088 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
138097 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
138098 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
138106 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
138107 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
138109 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
138111 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
138115 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
138117 VdbeComment((v, "%s", pSrcIdx->zName));
138118 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
138121 VdbeComment((v, "%s", pDestIdx->zName));
138123 if( db->mDbFlags & DBFLAG_Vacuum ){
138128 ** order. In this case, instead of seeking within the b-tree as part
138130 ** OP_IdxInsert to seek to the point within the b-tree where each key
138138 for(i=0; i<pSrcIdx->nColumn; i++){
138139 const char *zColl = pSrcIdx->azColl[i];
138142 if( i==pSrcIdx->nColumn ){
138147 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
138152 if( (db->mDbFlags & DBFLAG_Vacuum)==0
138228 sqlite3_mutex_enter(db->mutex);
138235 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
138241 /* this happens for a comment or white-space */
138254 && db->flags&SQLITE_NullCallback)) ){
138281 /* EVIDENCE-OF: R-38229-40159 If the callback function to
138282 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
138320 assert( (rc&db->errMask)==rc );
138321 sqlite3_mutex_leave(db->mutex);
138734 ** (part of the main SQLite library - not an extension) so that
138741 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
138743 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
138745 #define sqlite3_bind_blob sqlite3_api->bind_blob
138746 #define sqlite3_bind_double sqlite3_api->bind_double
138747 #define sqlite3_bind_int sqlite3_api->bind_int
138748 #define sqlite3_bind_int64 sqlite3_api->bind_int64
138749 #define sqlite3_bind_null sqlite3_api->bind_null
138750 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
138751 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
138752 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
138753 #define sqlite3_bind_text sqlite3_api->bind_text
138754 #define sqlite3_bind_text16 sqlite3_api->bind_text16
138755 #define sqlite3_bind_value sqlite3_api->bind_value
138756 #define sqlite3_busy_handler sqlite3_api->busy_handler
138757 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
138758 #define sqlite3_changes sqlite3_api->changes
138759 #define sqlite3_close sqlite3_api->close
138760 #define sqlite3_collation_needed sqlite3_api->collation_needed
138761 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
138762 #define sqlite3_column_blob sqlite3_api->column_blob
138763 #define sqlite3_column_bytes sqlite3_api->column_bytes
138764 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
138765 #define sqlite3_column_count sqlite3_api->column_count
138766 #define sqlite3_column_database_name sqlite3_api->column_database_name
138767 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
138768 #define sqlite3_column_decltype sqlite3_api->column_decltype
138769 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
138770 #define sqlite3_column_double sqlite3_api->column_double
138771 #define sqlite3_column_int sqlite3_api->column_int
138772 #define sqlite3_column_int64 sqlite3_api->column_int64
138773 #define sqlite3_column_name sqlite3_api->column_name
138774 #define sqlite3_column_name16 sqlite3_api->column_name16
138775 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
138776 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
138777 #define sqlite3_column_table_name sqlite3_api->column_table_name
138778 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
138779 #define sqlite3_column_text sqlite3_api->column_text
138780 #define sqlite3_column_text16 sqlite3_api->column_text16
138781 #define sqlite3_column_type sqlite3_api->column_type
138782 #define sqlite3_column_value sqlite3_api->column_value
138783 #define sqlite3_commit_hook sqlite3_api->commit_hook
138784 #define sqlite3_complete sqlite3_api->complete
138785 #define sqlite3_complete16 sqlite3_api->complete16
138786 #define sqlite3_create_collation sqlite3_api->create_collation
138787 #define sqlite3_create_collation16 sqlite3_api->create_collation16
138788 #define sqlite3_create_function sqlite3_api->create_function
138789 #define sqlite3_create_function16 sqlite3_api->create_function16
138790 #define sqlite3_create_module sqlite3_api->create_module
138791 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
138792 #define sqlite3_data_count sqlite3_api->data_count
138793 #define sqlite3_db_handle sqlite3_api->db_handle
138794 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
138795 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
138796 #define sqlite3_errcode sqlite3_api->errcode
138797 #define sqlite3_errmsg sqlite3_api->errmsg
138798 #define sqlite3_errmsg16 sqlite3_api->errmsg16
138799 #define sqlite3_exec sqlite3_api->exec
138801 #define sqlite3_expired sqlite3_api->expired
138803 #define sqlite3_finalize sqlite3_api->finalize
138804 #define sqlite3_free sqlite3_api->free
138805 #define sqlite3_free_table sqlite3_api->free_table
138806 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
138807 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
138808 #define sqlite3_get_table sqlite3_api->get_table
138810 #define sqlite3_global_recover sqlite3_api->global_recover
138812 #define sqlite3_interrupt sqlite3_api->interruptx
138813 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
138814 #define sqlite3_libversion sqlite3_api->libversion
138815 #define sqlite3_libversion_number sqlite3_api->libversion_number
138816 #define sqlite3_malloc sqlite3_api->malloc
138817 #define sqlite3_mprintf sqlite3_api->mprintf
138818 #define sqlite3_open sqlite3_api->open
138819 #define sqlite3_open16 sqlite3_api->open16
138820 #define sqlite3_prepare sqlite3_api->prepare
138821 #define sqlite3_prepare16 sqlite3_api->prepare16
138822 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
138823 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
138824 #define sqlite3_profile sqlite3_api->profile
138825 #define sqlite3_progress_handler sqlite3_api->progress_handler
138826 #define sqlite3_realloc sqlite3_api->realloc
138827 #define sqlite3_reset sqlite3_api->reset
138828 #define sqlite3_result_blob sqlite3_api->result_blob
138829 #define sqlite3_result_double sqlite3_api->result_double
138830 #define sqlite3_result_error sqlite3_api->result_error
138831 #define sqlite3_result_error16 sqlite3_api->result_error16
138832 #define sqlite3_result_int sqlite3_api->result_int
138833 #define sqlite3_result_int64 sqlite3_api->result_int64
138834 #define sqlite3_result_null sqlite3_api->result_null
138835 #define sqlite3_result_text sqlite3_api->result_text
138836 #define sqlite3_result_text16 sqlite3_api->result_text16
138837 #define sqlite3_result_text16be sqlite3_api->result_text16be
138838 #define sqlite3_result_text16le sqlite3_api->result_text16le
138839 #define sqlite3_result_value sqlite3_api->result_value
138840 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
138841 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
138842 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
138843 #define sqlite3_snprintf sqlite3_api->xsnprintf
138844 #define sqlite3_step sqlite3_api->step
138845 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
138846 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
138847 #define sqlite3_total_changes sqlite3_api->total_changes
138848 #define sqlite3_trace sqlite3_api->trace
138850 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
138852 #define sqlite3_update_hook sqlite3_api->update_hook
138853 #define sqlite3_user_data sqlite3_api->user_data
138854 #define sqlite3_value_blob sqlite3_api->value_blob
138855 #define sqlite3_value_bytes sqlite3_api->value_bytes
138856 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
138857 #define sqlite3_value_double sqlite3_api->value_double
138858 #define sqlite3_value_int sqlite3_api->value_int
138859 #define sqlite3_value_int64 sqlite3_api->value_int64
138860 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
138861 #define sqlite3_value_text sqlite3_api->value_text
138862 #define sqlite3_value_text16 sqlite3_api->value_text16
138863 #define sqlite3_value_text16be sqlite3_api->value_text16be
138864 #define sqlite3_value_text16le sqlite3_api->value_text16le
138865 #define sqlite3_value_type sqlite3_api->value_type
138866 #define sqlite3_vmprintf sqlite3_api->vmprintf
138867 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
138868 #define sqlite3_overload_function sqlite3_api->overload_function
138869 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
138870 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
138871 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
138872 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
138873 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
138874 #define sqlite3_blob_close sqlite3_api->blob_close
138875 #define sqlite3_blob_open sqlite3_api->blob_open
138876 #define sqlite3_blob_read sqlite3_api->blob_read
138877 #define sqlite3_blob_write sqlite3_api->blob_write
138878 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
138879 #define sqlite3_file_control sqlite3_api->file_control
138880 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
138881 #define sqlite3_memory_used sqlite3_api->memory_used
138882 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
138883 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
138884 #define sqlite3_mutex_free sqlite3_api->mutex_free
138885 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
138886 #define sqlite3_mutex_try sqlite3_api->mutex_try
138887 #define sqlite3_open_v2 sqlite3_api->open_v2
138888 #define sqlite3_release_memory sqlite3_api->release_memory
138889 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
138890 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
138891 #define sqlite3_sleep sqlite3_api->sleep
138892 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
138893 #define sqlite3_vfs_find sqlite3_api->vfs_find
138894 #define sqlite3_vfs_register sqlite3_api->vfs_register
138895 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
138896 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
138897 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
138898 #define sqlite3_result_error_code sqlite3_api->result_error_code
138899 #define sqlite3_test_control sqlite3_api->test_control
138900 #define sqlite3_randomness sqlite3_api->randomness
138901 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
138902 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
138903 #define sqlite3_limit sqlite3_api->limit
138904 #define sqlite3_next_stmt sqlite3_api->next_stmt
138905 #define sqlite3_sql sqlite3_api->sql
138906 #define sqlite3_status sqlite3_api->status
138907 #define sqlite3_backup_finish sqlite3_api->backup_finish
138908 #define sqlite3_backup_init sqlite3_api->backup_init
138909 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
138910 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
138911 #define sqlite3_backup_step sqlite3_api->backup_step
138912 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
138913 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
138914 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
138915 #define sqlite3_db_config sqlite3_api->db_config
138916 #define sqlite3_db_mutex sqlite3_api->db_mutex
138917 #define sqlite3_db_status sqlite3_api->db_status
138918 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
138919 #define sqlite3_log sqlite3_api->log
138920 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
138921 #define sqlite3_sourceid sqlite3_api->sourceid
138922 #define sqlite3_stmt_status sqlite3_api->stmt_status
138923 #define sqlite3_strnicmp sqlite3_api->strnicmp
138924 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
138925 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
138926 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
138927 #define sqlite3_wal_hook sqlite3_api->wal_hook
138928 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
138929 #define sqlite3_vtab_config sqlite3_api->vtab_config
138930 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
138932 #define sqlite3_close_v2 sqlite3_api->close_v2
138933 #define sqlite3_db_filename sqlite3_api->db_filename
138934 #define sqlite3_db_readonly sqlite3_api->db_readonly
138935 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
138936 #define sqlite3_errstr sqlite3_api->errstr
138937 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
138938 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
138939 #define sqlite3_stricmp sqlite3_api->stricmp
138940 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
138941 #define sqlite3_uri_int64 sqlite3_api->uri_int64
138942 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
138943 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
138944 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
138946 #define sqlite3_auto_extension sqlite3_api->auto_extension
138947 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
138948 #define sqlite3_bind_text64 sqlite3_api->bind_text64
138949 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
138950 #define sqlite3_load_extension sqlite3_api->load_extension
138951 #define sqlite3_malloc64 sqlite3_api->malloc64
138952 #define sqlite3_msize sqlite3_api->msize
138953 #define sqlite3_realloc64 sqlite3_api->realloc64
138954 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
138955 #define sqlite3_result_blob64 sqlite3_api->result_blob64
138956 #define sqlite3_result_text64 sqlite3_api->result_text64
138957 #define sqlite3_strglob sqlite3_api->strglob
138959 #define sqlite3_value_dup sqlite3_api->value_dup
138960 #define sqlite3_value_free sqlite3_api->value_free
138961 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
138962 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
138964 #define sqlite3_value_subtype sqlite3_api->value_subtype
138965 #define sqlite3_result_subtype sqlite3_api->result_subtype
138967 #define sqlite3_status64 sqlite3_api->status64
138968 #define sqlite3_strlike sqlite3_api->strlike
138969 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
138971 #define sqlite3_system_errno sqlite3_api->system_errno
138973 #define sqlite3_trace_v2 sqlite3_api->trace_v2
138974 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
138976 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
138978 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
138979 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
138980 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
138981 #define sqlite3_result_pointer sqlite3_api->result_pointer
138982 #define sqlite3_value_pointer sqlite3_api->value_pointer
138984 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
138985 #define sqlite3_value_nochange sqlite3_api->value_nochange
138986 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
138988 #define sqlite3_keyword_count sqlite3_api->keyword_count
138989 #define sqlite3_keyword_name sqlite3_api->keyword_name
138990 #define sqlite3_keyword_check sqlite3_api->keyword_check
138991 #define sqlite3_str_new sqlite3_api->str_new
138992 #define sqlite3_str_finish sqlite3_api->str_finish
138993 #define sqlite3_str_appendf sqlite3_api->str_appendf
138994 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
138995 #define sqlite3_str_append sqlite3_api->str_append
138996 #define sqlite3_str_appendall sqlite3_api->str_appendall
138997 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
138998 #define sqlite3_str_reset sqlite3_api->str_reset
138999 #define sqlite3_str_errcode sqlite3_api->str_errcode
139000 #define sqlite3_str_length sqlite3_api->str_length
139001 #define sqlite3_str_value sqlite3_api->str_value
139003 #define sqlite3_create_window_function sqlite3_api->create_window_function
139005 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
139007 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
139008 #define sqlite3_value_frombind sqlite3_api->value_frombind
139010 #define sqlite3_drop_modules sqlite3_api->drop_modules
139012 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
139013 #define sqlite3_uri_key sqlite3_api->uri_key
139014 #define sqlite3_filename_database sqlite3_api->filename_database
139015 #define sqlite3_filename_journal sqlite3_api->filename_journal
139016 #define sqlite3_filename_wal sqlite3_api->filename_wal
139018 #define sqlite3_create_filename sqlite3_api->create_filename
139019 #define sqlite3_free_filename sqlite3_api->free_filename
139020 #define sqlite3_database_file_object sqlite3_api->database_file_object
139022 #define sqlite3_txn_state sqlite3_api->txn_state
139024 #define sqlite3_changes64 sqlite3_api->changes64
139025 #define sqlite3_total_changes64 sqlite3_api->total_changes64
139027 #define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
139029 #define sqlite3_error_offset sqlite3_api->error_offset
139030 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
139031 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
139032 #define sqlite3_vtab_in sqlite3_api->vtab_in
139033 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
139034 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
139037 #define sqlite3_deserialize sqlite3_api->deserialize
139038 #define sqlite3_serialize sqlite3_api->serialize
139040 #define sqlite3_db_name sqlite3_api->db_name
139042 #define sqlite3_value_encoding sqlite3_api->value_encoding
139044 #define sqlite3_is_interrupted sqlite3_api->is_interrupted
139046 #define sqlite3_stmt_explain sqlite3_api->stmt_explain
139048 #define sqlite3_get_clientdata sqlite3_api->get_clientdata
139049 #define sqlite3_set_clientdata sqlite3_api->set_clientdata
139051 #define sqlite3_setlk_timeout sqlite3_api->setlk_timeout
139064 # define SQLITE_EXTENSION_INIT1 /*no-op*/
139066 # define SQLITE_EXTENSION_INIT3 /*no-op*/
139604 sqlite3_vfs *pVfs = db->pVfs;
139636 if( (db->flags & SQLITE_LoadExtension)==0 ){
139645 /* tag-20210611-1. Some dlopen() implementations will segfault if given
139647 ** so limit the extension filename length to about twice that.
139650 ** Later (2023-03-25): Save an extra 6 bytes for the filename suffix.
139693 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
139713 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
139732 /* Append the new shared library handle to the db->aExtension array. */
139733 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
139737 if( db->nExtension>0 ){
139738 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
139740 sqlite3DbFree(db, db->aExtension);
139741 db->aExtension = aHandle;
139743 db->aExtension[db->nExtension++] = handle;
139754 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
139766 sqlite3_mutex_enter(db->mutex);
139769 sqlite3_mutex_leave(db->mutex);
139779 assert( sqlite3_mutex_held(db->mutex) );
139780 for(i=0; i<db->nExtension; i++){
139781 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
139783 sqlite3DbFree(db, db->aExtension);
139794 sqlite3_mutex_enter(db->mutex);
139796 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
139798 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
139800 sqlite3_mutex_leave(db->mutex);
139821 ** we have to locate the state vector at run-time. In the more common
139884 ** routine is a no-op.
139902 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
139904 wsdAutoext.nExt--;
140070 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
140074 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
140076 /* Names of columns for pragmas that return multi-column result
140077 ** or that return single-column results where the name of the
140144 /* Definitions of all built-in pragmas */
140679 ** the following macro or to the actual analysis_limit if it is non-zero,
140682 ** The value of 2000 is chosen empirically so that the worst-case run-time
140684 ** of test databases on a RaspberryPI-4 compiled using -Os and without
140685 ** -DSQLITE_DEBUG. Of course, your mileage may vary. For the purpose of
140686 ** this paragraph, "worst-case" means that ANALYZE ends up being
140693 ** optimize on a PI-4 is more like 1 millisecond or less with the 0x10000
140763 ** Interpret the given string as an auto-vacuum mode value.
140781 ** backed temporary databases, 2 for the Red-Black tree in memory database
140782 ** and 0 to use the compile-time default.
140786 return z[0] - '0';
140803 sqlite3 *db = pParse->db;
140804 if( db->aDb[1].pBt!=0 ){
140805 if( !db->autoCommit
140806 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
140812 sqlite3BtreeClose(db->aDb[1].pBt);
140813 db->aDb[1].pBt = 0;
140828 sqlite3 *db = pParse->db;
140829 if( db->temp_store==ts ) return SQLITE_OK;
140833 db->temp_store = (u8)ts;
140845 u8 n = pPragma->nPragCName;
140848 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
140851 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
140885 if( db->autoCommit ){
140886 Db *pDb = db->aDb;
140887 int n = db->nDb;
140893 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
140894 while( (n--) > 0 ){
140895 if( pDb->pBt ){
140896 sqlite3BtreeSetPagerFlags(pDb->pBt,
140897 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
140904 # define setAllPagerFlags(X) /* no-op */
140909 ** Return a human-readable name for a constraint resolution action.
140930 ** journal-mode name.
140957 upr = ArraySize(aPragmaName)-1;
140963 upr = mid - 1;
140978 int isBuiltin, /* True if this is a built-in function */
140989 for(; p; p=p->pNext){
140998 if( p->xSFunc==0 ) continue;
140999 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
141004 if( p->xValue!=0 ){
141006 }else if( p->xFinalize!=0 ){
141012 p->zName, isBuiltin,
141013 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
141014 p->nArg,
141015 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
141024 ** Generate code to output a single-column result row with a value of the
141057 int minusFlag /* True if a '-' sign preceded <value> */
141059 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
141060 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
141066 sqlite3 *db = pParse->db; /* The database connection */
141073 pParse->nMem = 2;
141079 pDb = &db->aDb[iDb];
141091 zRight = sqlite3MPrintf(db, "-%T", pValue);
141097 zDb = pId2->n>0 ? pDb->zDbSName : 0;
141102 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
141104 ** handled the pragma and generate a no-op prepared statement.
141106 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
141111 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
141121 db->busyHandler.nBusy = 0;
141135 pParse->nErr++;
141136 pParse->rc = rc;
141143 /* IMP: R-43042-22504 No error messages are generated if an
141149 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
141154 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
141155 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
141161 switch( pPragma->ePragTyp ){
141196 pParse->nMem += 2;
141208 pDb->pSchema->cache_size = size;
141209 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
141226 Btree *pBt = pDb->pBt;
141232 /* Malloc may fail when setting the page-size, as there is an internal
141235 db->nextPagesize = sqlite3Atoi(zRight);
141236 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
141252 Btree *pBt = pDb->pBt;
141253 int b = -1;
141262 if( pId2->n==0 && b>=0 ){
141264 for(ii=0; ii<db->nDb; ii++){
141265 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
141294 iReg = ++pParse->nMem;
141318 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
141321 ** the locking-mode of the main database).
141323 eMode = db->dfltLockMode;
141326 if( pId2->n==0 ){
141328 ** of the PRAGMA command. In this case the locking-mode must be
141336 assert(pDb==&db->aDb[0]);
141337 for(ii=2; ii<db->nDb; ii++){
141338 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
141341 db->dfltLockMode = (u8)eMode;
141343 pPager = sqlite3BtreePager(pDb->pBt);
141380 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
141381 /* Do not allow journal-mode "OFF" in defensive since the database
141386 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
141389 pId2->n = 1;
141391 for(ii=db->nDb-1; ii>=0; ii--){
141392 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
141408 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141409 i64 iLimit = -2;
141412 if( iLimit<-1 ) iLimit = -1;
141425 ** Get or set the value of the database 'auto-vacuum' parameter.
141430 Btree *pBt = pDb->pBt;
141437 db->nextAutovac = (u8)eAuto;
141439 ** incr-vacuum flags. This is required in case this connection
141441 ** as an auto-vacuum capable db.
141448 ** that this really is an auto-vacuum capable database.
141467 aOp[4].p3 = eAuto - 1;
141490 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
141506 ** number of pages is adjusted so that the cache uses -N kibibytes
141512 returnSingleInt(v, pDb->pSchema->cache_size);
141515 pDb->pSchema->cache_size = size;
141516 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
141533 ** number of pages is adjusted so that the cache uses -N kibibytes
141547 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
141548 sqlite3BtreeSetSpillSize(pDb->pBt,0));
141552 sqlite3BtreeSetSpillSize(pDb->pBt, size);
141555 db->flags |= SQLITE_CacheSpill;
141557 db->flags &= ~(u64)SQLITE_CacheSpill;
141586 if( pId2->n==0 ) db->szMmap = sz;
141587 for(ii=db->nDb-1; ii>=0; ii--){
141588 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
141589 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
141593 sz = -1;
141602 pParse->nErr++;
141603 pParse->rc = rc;
141616 ** Note that it is possible for the library compile-time options to
141621 returnSingleInt(v, db->temp_store);
141646 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
141654 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
141655 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
141693 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
141724 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141731 Pager *pPager = sqlite3BtreePager(pDb->pBt);
141761 returnSingleInt(v, pDb->safety_level-1);
141763 if( !db->autoCommit ){
141769 pDb->safety_level = iLevel;
141770 pDb->bSyncSet = 1;
141782 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
141784 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
141785 if( db->autoCommit==0 ){
141787 ** in auto-commit mode. */
141793 || (db->flags & SQLITE_Defensive)==0
141795 db->flags |= mask;
141798 db->flags &= ~mask;
141800 db->nDeferredImmCons = 0;
141801 db->nDeferredCons = 0;
141806 /* IMP: R-60817-01178 If the argument is "RESET" then schema
141813 /* Many of the flag-pragmas modify the code generated by the SQL
141836 ** pk: Non-zero for PK fields.
141847 pParse->nMem = 7;
141849 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
141852 if( pCol->colFlags & COLFLAG_NOINSERT ){
141853 if( pPragma->iArg==0 ){
141857 if( pCol->colFlags & COLFLAG_VIRTUAL ){
141859 }else if( pCol->colFlags & COLFLAG_STORED ){
141861 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
141865 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
141870 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
141873 assert( pColExpr==0 || pColExpr->op==TK_SPAN || isHidden>=2 );
141876 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
141877 i-nHidden,
141878 pCol->zCnName,
141880 pCol->notNull ? 1 : 0,
141881 (isHidden>=2 || pColExpr==0) ? 0 : pColExpr->u.zToken,
141904 pParse->nMem = 6;
141906 for(ii=0; ii<db->nDb; ii++){
141910 if( zDb && sqlite3_stricmp(zDb, db->aDb[ii].zDbSName)!=0 ) continue;
141917 pHash = &db->aDb[ii].pSchema->tblHash;
141919 while( initNCol-- ){
141924 if( pTab->nCol==0 ){
141925 char *zSql = sqlite3MPrintf(db, "SELECT*FROM\"%w\"", pTab->zName);
141928 (void)sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_DONT_LOG,
141933 if( db->mallocFailed ){
141934 sqlite3ErrorMsg(db->pParse, "out of memory");
141935 db->pParse->rc = SQLITE_NOMEM_BKPT;
141937 pHash = &db->aDb[ii].pSchema->tblHash;
141946 if( zRight && sqlite3_stricmp(zRight, pTab->zName)!=0 ) continue;
141951 }else if( pTab->tabFlags & TF_Shadow ){
141957 db->aDb[ii].zDbSName,
141958 sqlite3PreferredTableName(pTab->zName),
141960 pTab->nCol,
141961 (pTab->tabFlags & TF_WithoutRowid)!=0,
141962 (pTab->tabFlags & TF_Strict)!=0
141973 pParse->nMem = 5;
141975 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
141978 sqlite3PreferredTableName(pTab->zName),
141980 pTab->szTabRow,
141981 pTab->nRowLogEst,
141982 pTab->tabFlags);
141983 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141985 pIdx->zName,
141986 pIdx->szIdxRow,
141987 pIdx->aiRowLogEst[0],
141988 pIdx->hasStat1);
142010 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
142013 if( pPragma->iArg ){
142015 mx = pIdx->nColumn;
142016 pParse->nMem = 6;
142019 mx = pIdx->nKeyCol;
142020 pParse->nMem = 3;
142022 pTab = pIdx->pTable;
142024 assert( pParse->nMem<=pPragma->nPragCName );
142026 i16 cnum = pIdx->aiColumn[i];
142028 cnum<0 ? 0 : pTab->aCol[cnum].zCnName);
142029 if( pPragma->iArg ){
142031 pIdx->aSortOrder[i],
142032 pIdx->azColl[i],
142033 i<pIdx->nKeyCol);
142035 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
142047 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142048 pParse->nMem = 5;
142050 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
142054 pIdx->zName,
142056 azOrigin[pIdx->idxType],
142057 pIdx->pPartIdxWhere!=0);
142065 pParse->nMem = 3;
142066 for(i=0; i<db->nDb; i++){
142067 if( db->aDb[i].pBt==0 ) continue;
142068 assert( db->aDb[i].zDbSName!=0 );
142071 db->aDb[i].zDbSName,
142072 sqlite3BtreeGetFilename(db->aDb[i].pBt));
142080 pParse->nMem = 2;
142081 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
142083 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
142093 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
142094 pParse->nMem = 6;
142096 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
142097 assert( p->funcFlags & SQLITE_FUNC_BUILTIN );
142101 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
142103 assert( (p->funcFlags & SQLITE_FUNC_BUILTIN)==0 );
142112 pParse->nMem = 1;
142113 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
142115 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
142138 pFK = pTab->u.tab.pFKey;
142140 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142142 pParse->nMem = 8;
142146 for(j=0; j<pFK->nCol; j++){
142150 pFK->zTo,
142151 pTab->aCol[pFK->aCol[j].iFrom].zCnName,
142152 pFK->aCol[j].zCol,
142153 actionName(pFK->aAction[1]), /* ON UPDATE */
142154 actionName(pFK->aAction[0]), /* ON DELETE */
142158 pFK = pFK->pNextFrom;
142183 regResult = pParse->nMem+1;
142184 pParse->nMem += 4;
142185 regRow = ++pParse->nMem;
142186 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
142195 if( pTab==0 || !IsOrdinaryTable(pTab) || pTab->u.tab.pFKey==0 ) continue;
142196 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142197 zDb = db->aDb[iDb].zDbSName;
142199 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
142200 sqlite3TouchRegister(pParse, pTab->nCol+regRow);
142202 sqlite3VdbeLoadString(v, regResult, pTab->zName);
142204 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
142205 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
142208 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
142214 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
142222 assert( pParse->nErr>0 || pFK==0 );
142224 if( pParse->nTab<i ) pParse->nTab = i;
142227 for(i=1, pFK=pTab->u.tab.pFKey; pFK; i++, pFK=pFK->pNextFrom){
142228 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
142233 assert( x==0 || db->mallocFailed );
142241 sqlite3TouchRegister(pParse, regRow + pFK->nCol);
142242 for(j=0; j<pFK->nCol; j++){
142243 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
142251 sqlite3VdbeAddOp4(v, OP_Affinity, regRow, pFK->nCol, 0,
142252 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
142253 sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regRow, pFK->nCol);
142259 assert( pFK->nCol==1 || db->mallocFailed );
142268 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
142307 ** without the overhead of cross-checking indexes. Quick_check
142335 ** to -1 here, to indicate that the VDBE should verify the integrity
142338 assert( iDb==0 || pId2->z );
142339 if( pId2->z==0 ) iDb = -1;
142342 pParse->nMem = 6;
142347 if( sqlite3GetInt32(pValue->z, &mxErr) ){
142353 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
142356 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
142359 for(i=0; i<db->nDb; i++){
142369 pParse->okConstFactor = 0; /* tag-20230327-1 */
142371 /* Do an integrity check of the B-Tree
142377 pTbls = &db->aDb[i].pSchema->tblHash;
142384 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
142396 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
142397 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142398 aRoot[++cnt] = pIdx->tnum;
142408 /* Do the b-tree integrity checks */
142413 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
142431 for(pIdx=pTab->pIndex; ALWAYS(pIdx); pIdx=pIdx->pNext){
142436 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
142437 if( pIdx->pPartIdxWhere==0 ){
142440 sqlite3VdbeLoadString(v, 4, pIdx->zName);
142457 int r1 = -1;
142460 int mxCol; /* Maximum non-virtual column number */
142469 r2 = sqlite3GetTempRange(pParse, pPk->nKeyCol);
142470 sqlite3VdbeAddOp3(v, OP_Null, 1, r2, r2+pPk->nKeyCol-1);
142475 ** reg[8+i] counts the number of entries in the i-th index
142478 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
142481 assert( pParse->nMem>=8+j );
142486 /* Fetch the right-most column from the table. This will cause
142493 mxCol = -1;
142494 for(j=0; j<pTab->nCol; j++){
142495 if( (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)==0 ) mxCol++;
142497 if( mxCol==pTab->iPKey ) mxCol--;
142500 ** PK index column-count, so there is no need to account for them
142502 mxCol = sqlite3PrimaryKeyIndex(pTab)->nColumn-1;
142514 a1 = sqlite3VdbeAddOp4Int(v, OP_IdxGT, iDataCur, 0,r2,pPk->nKeyCol);
142519 pTab->zName);
142524 for(j=0; j<pPk->nKeyCol; j++){
142532 ** (2) Datatype must be exact for non-ANY columns in STRICT tables
142533 ** (3) Datatype for TEXT columns in non-STRICT tables must be
142535 ** (4) Datatype for numeric columns in non-STRICT tables must not
142538 bStrict = (pTab->tabFlags & TF_Strict)!=0;
142539 for(j=0; j<pTab->nCol; j++){
142541 Column *pCol = pTab->aCol + j; /* The column to be checked */
142547 if( j==pTab->iPKey ) continue;
142549 doTypeCheck = pCol->eCType>COLTYPE_ANY;
142551 doTypeCheck = pCol->affinity>SQLITE_AFF_BLOB;
142553 if( pCol->notNull==0 && !doTypeCheck ) continue;
142557 if( pCol->colFlags & COLFLAG_VIRTUAL ){
142559 p1 = -1;
142562 if( pCol->iDflt ){
142565 pCol->affinity, &pDfltValue);
142583 if( pCol->notNull ){
142602 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
142603 pCol->zCnName);
142614 /* (2) Datatype must be exact for non-ANY columns in STRICT tables*/
142624 assert( pCol->eCType>=1 && pCol->eCType<=sizeof(aStdTypeMask) );
142625 sqlite3VdbeChangeP5(v, aStdTypeMask[pCol->eCType-1]);
142627 zErr = sqlite3MPrintf(db, "non-%s value in %s.%s",
142628 sqlite3StdType[pCol->eCType-1],
142629 pTab->zName, pTab->aCol[j].zCnName);
142631 }else if( !bStrict && pCol->affinity==SQLITE_AFF_TEXT ){
142632 /* (3) Datatype for TEXT columns in non-STRICT tables must be
142638 pTab->zName, pTab->aCol[j].zCnName);
142640 }else if( !bStrict && pCol->affinity>=SQLITE_AFF_NUMERIC ){
142641 /* (4) Datatype for numeric columns in non-STRICT tables must not
142650 sqlite3VdbeAddOp4Int(v, OP_IsType, -1, labelOk, 3, p4);
142654 pTab->zName, pTab->aCol[j].zCnName);
142662 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
142663 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
142664 if( db->mallocFailed==0 ){
142669 pParse->iSelfTab = iDataCur + 1;
142670 for(k=pCheck->nExpr-1; k>0; k--){
142671 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
142673 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
142676 pParse->iSelfTab = 0;
142678 pTab->zName);
142687 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
142698 pIdx->nColumn); VdbeCoverage(v);
142703 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
142715 jmp7 = sqlite3VdbeAddOp3(v, OP_Eq, 3, 0, r1+pIdx->nColumn-1);
142718 "rowid not at end-of-record for row ");
142721 sqlite3VdbeGoto(v, jmp5-1);
142725 /* Any indexed columns with non-BINARY collations must still hold
142728 for(kk=0; kk<pIdx->nKeyCol; kk++){
142729 if( pIdx->azColl[kk]==sqlite3StrBINARY ) continue;
142740 sqlite3VdbeGoto(v, jmp5-1);
142750 for(kk=0; kk<pIdx->nKeyCol; kk++){
142751 int iCol = pIdx->aiColumn[kk];
142752 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
142753 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
142761 pIdx->nKeyCol); VdbeCoverage(v);
142762 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
142771 sqlite3VdbeJumpHere(v, loopTop-1);
142774 sqlite3ReleaseTempRange(pParse, r2, pPk->nKeyCol);
142789 if( pTab->nCol<=0 ){
142790 const char *zMod = pTab->u.vtab.azArg[0];
142791 if( sqlite3HashFind(&db->aModule, zMod)==0 ) continue;
142794 if( pTab->u.vtab.p==0 ) continue;
142795 pVTab = pTab->u.vtab.p->pVtab;
142797 if( NEVER(pVTab->pModule==0) ) continue;
142798 if( pVTab->pModule->iVersion<4 ) continue;
142799 if( pVTab->pModule->xIntegrity==0 ) continue;
142801 pTab->nTabRef++;
142825 aOp[0].p2 = 1-mxErr;
142831 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
142840 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
142845 ** The second form of this pragma is a no-op if the main database file
142866 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
142867 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
142868 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
142871 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
142881 returnSingleText(v, encnames[ENC(pParse->db)].zName);
142888 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
142889 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
142890 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
142891 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
142897 if( !pEnc->zName ){
142922 ** the value of the schema-version and user-version, respectively. Both
142923 ** the schema-version and the user-version are 32-bit signed integers
142926 ** The schema-cookie is usually only manipulated internally by SQLite. It
142933 ** the schema-version is potentially dangerous and may lead to program
142936 ** The user-version is not used internally by SQLite. It may be used by
142940 int iCookie = pPragma->iArg; /* Which cookie to read or write */
142942 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
142957 if( iCookie==BTREE_SCHEMA_VERSION && (db->flags & SQLITE_Defensive)!=0 ){
142959 ** mode. Change the OP_SetCookie opcode into a no-op. */
142986 ** Return the names of all compile-time options used in this build,
142992 pParse->nMem = 1;
143009 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
143020 pParse->nMem = 3;
143039 db->xWalCallback==sqlite3WalDefaultHook ?
143040 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
143048 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
143081 ** SQLITE_DEFAULT_OPTIMIZE_LIMIT compile-time option.
143083 ** currently (2024-02-19) set to 2000, which is such that
143084 ** the worst case run-time for PRAGMA optimize on a 100MB
143086 ** a RaspberryPI-4 class machine. On by default.
143113 ** (4c) The query planner used sqlite_stat1-style statistics for one
143121 ** 10-fold. In other words, the current size of the table is
143153 }else if( db->nAnalysisLimit>0
143154 && db->nAnalysisLimit<SQLITE_DEFAULT_OPTIMIZE_LIMIT ){
143159 iTabCur = pParse->nTab++;
143160 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
143163 pSchema = db->aDb[iDb].pSchema;
143164 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
143171 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ) continue;
143174 ** If any index is unanalyzed, then the threshold is -1 to
143177 szThreshold = pTab->nRowLogEst;
143179 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
143181 if( !pIdx->hasStat1 ){
143182 szThreshold = -1; /* Always analyze if any index lacks statistics */
143189 if( (pTab->tabFlags & TF_MaybeReanalyze)!=0 ){
143193 }else if( pTab->pIndex!=0 && szThreshold<0 ){
143216 szThreshold>=iRange ? szThreshold-iRange : -1,
143225 db->aDb[iDb].zDbSName, pTab->zName);
143239 ** the analysis_limit to avoid excess run-time in the worst case.
143241 if( !db->mallocFailed && nLimit>0 && nBtree>100 ){
143265 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
143269 returnSingleInt(v, db->busyTimeout);
143277 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
143279 ** specified and is a non-negative integer.
143280 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
143282 ** sqlite3_soft_heap_limit64(-1) C-language function.
143289 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
143300 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
143307 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
143310 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
143329 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
143343 && sqlite3DecOrHexToI64(zRight, &N)==SQLITE_OK /* IMP: R-40975-20399 */
143346 db->nAnalysisLimit = (int)(N&0x7fffffff);
143348 returnSingleInt(v, db->nAnalysisLimit); /* IMP: R-57594-65522 */
143361 pParse->nMem = 2;
143362 for(i=0; i<db->nDb; i++){
143366 if( db->aDb[i].zDbSName==0 ) continue;
143367 pBt = db->aDb[i].pBt;
143370 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
143374 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
143382 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
143391 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
143396 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
143447 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
143452 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
143456 if( pPragma->mPragFlg & PragFlg_Result1 ){
143460 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
143466 assert( strlen(zBuf) < sizeof(zBuf)-1 );
143474 pTab->pName = pPragma;
143475 pTab->db = db;
143476 pTab->iHidden = i;
143477 pTab->nHidden = j;
143509 pIdxInfo->estimatedCost = (double)1;
143510 if( pTab->nHidden==0 ){ return SQLITE_OK; }
143511 pConstraint = pIdxInfo->aConstraint;
143514 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
143515 if( pConstraint->iColumn < pTab->iHidden ) continue;
143516 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
143517 if( pConstraint->usable==0 ) return SQLITE_CONSTRAINT;
143518 j = pConstraint->iColumn - pTab->iHidden;
143523 pIdxInfo->estimatedCost = (double)2147483647;
143524 pIdxInfo->estimatedRows = 2147483647;
143527 j = seen[0]-1;
143528 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
143529 pIdxInfo->aConstraintUsage[j].omit = 1;
143530 pIdxInfo->estimatedCost = (double)20;
143531 pIdxInfo->estimatedRows = 20;
143533 j = seen[1]-1;
143534 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
143535 pIdxInfo->aConstraintUsage[j].omit = 1;
143546 pCsr->base.pVtab = pVtab;
143547 *ppCursor = &pCsr->base;
143554 sqlite3_finalize(pCsr->pPragma);
143555 pCsr->pPragma = 0;
143556 pCsr->iRowid = 0;
143557 for(i=0; i<ArraySize(pCsr->azArg); i++){
143558 sqlite3_free(pCsr->azArg[i]);
143559 pCsr->azArg[i] = 0;
143577 pCsr->iRowid++;
143578 assert( pCsr->pPragma );
143579 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
143580 rc = sqlite3_finalize(pCsr->pPragma);
143581 pCsr->pPragma = 0;
143596 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
143605 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
143608 assert( j<ArraySize(pCsr->azArg) );
143609 assert( pCsr->azArg[j]==0 );
143611 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
143612 if( pCsr->azArg[j]==0 ){
143617 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
143619 if( pCsr->azArg[1] ){
143620 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
143622 sqlite3_str_appendall(&acc, pTab->pName->zName);
143623 if( pCsr->azArg[0] ){
143624 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
143628 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
143631 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
143642 return (pCsr->pPragma==0);
143654 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
143655 if( i<pTab->iHidden ){
143656 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
143658 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
143668 *p = pCsr->iRowid;
143675 0, /* xCreate - create a table */
143676 pragmaVtabConnect, /* xConnect - connect to an existing table */
143677 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
143678 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
143679 0, /* xDestroy - Drop a table */
143680 pragmaVtabOpen, /* xOpen - open a cursor */
143681 pragmaVtabClose, /* xClose - close a cursor */
143682 pragmaVtabFilter, /* xFilter - configure scan constraints */
143683 pragmaVtabNext, /* xNext - advance a cursor */
143685 pragmaVtabColumn, /* xColumn - read data */
143686 pragmaVtabRowid, /* xRowid - read data */
143687 0, /* xUpdate - write data */
143688 0, /* xBegin - begin transaction */
143689 0, /* xSync - sync transaction */
143690 0, /* xCommit - commit transaction */
143691 0, /* xRollback - rollback transaction */
143692 0, /* xFindFunction - function overloading */
143693 0, /* xRename - rename the table */
143711 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
143712 assert( sqlite3HashFind(&db->aModule, zName)==0 );
143748 sqlite3 *db = pData->db;
143749 if( db->mallocFailed ){
143750 pData->rc = SQLITE_NOMEM_BKPT;
143751 }else if( pData->pzErrMsg[0]!=0 ){
143753 }else if( pData->mInitFlags & (INITFLAG_AlterMask) ){
143759 *pData->pzErrMsg = sqlite3MPrintf(db,
143761 azAlterType[(pData->mInitFlags&INITFLAG_AlterMask)-1],
143764 pData->rc = SQLITE_ERROR;
143765 }else if( db->flags & SQLITE_WriteSchema ){
143766 pData->rc = SQLITE_CORRUPT_BKPT;
143771 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
143772 *pData->pzErrMsg = z;
143773 pData->rc = SQLITE_CORRUPT_BKPT;
143784 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
143785 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
143793 const char *zSql, /* UTF-8 encoded SQL statement. */
143794 int nBytes, /* Length of zSql in bytes. */
143818 sqlite3 *db = pData->db;
143819 int iDb = pData->iDb;
143823 assert( sqlite3_mutex_held(db->mutex) );
143824 db->mDbFlags |= DBFLAG_EncodingFixed;
143826 pData->nInitRow++;
143827 if( db->mallocFailed ){
143832 assert( iDb>=0 && iDb<db->nDb );
143839 ** But because db->init.busy is set to 1, no VDBE code is generated
143849 u8 saved_iDb = db->init.iDb;
143853 assert( db->init.busy );
143854 db->init.iDb = iDb;
143855 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
143856 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
143862 db->init.orphanTrigger = 0;
143863 db->init.azInit = (const char**)argv;
143865 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
143866 rc = db->errCode;
143868 db->init.iDb = saved_iDb;
143869 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
143871 if( db->init.orphanTrigger ){
143874 if( rc > pData->rc ) pData->rc = rc;
143882 db->init.azInit = sqlite3StdType; /* Any array of string ptrs will do */
143894 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
143898 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
143899 || pIndex->tnum<2
143900 || pIndex->tnum>pData->mxPage
143931 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
143933 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
143934 assert( iDb>=0 && iDb<db->nDb );
143935 assert( db->aDb[iDb].pSchema );
143936 assert( sqlite3_mutex_held(db->mutex) );
143937 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
143939 db->init.busy = 1;
143941 /* Construct the in-memory representation schema tables (sqlite_schema or
143945 ** the schema table as read-only. */
143961 db->mDbFlags &= mask;
143969 pDb = &db->aDb[iDb];
143970 if( pDb->pBt==0 ){
143977 /* If there is not already a read-only (or read-write) transaction opened
143978 ** on the b-tree database, open one now. If a transaction is opened, it
143980 sqlite3BtreeEnter(pDb->pBt);
143981 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
143982 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
143997 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
144008 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
144010 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
144013 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
144015 /* If opening a non-empty database, check the text encoding. For the
144020 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
144021 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
144025 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
144033 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
144041 pDb->pSchema->enc = ENC(db);
144043 if( pDb->pSchema->cache_size==0 ){
144045 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
144047 pDb->pSchema->cache_size = size;
144049 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
144051 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
144057 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
144060 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
144061 if( pDb->pSchema->file_format==0 ){
144062 pDb->pSchema->file_format = 1;
144064 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
144075 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
144076 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
144081 assert( db->init.busy );
144082 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
144087 db->aDb[iDb].zDbSName, zSchemaTabName);
144091 xAuth = db->xAuth;
144092 db->xAuth = 0;
144096 db->xAuth = xAuth;
144107 assert( pDb == &(db->aDb[iDb]) );
144108 if( db->mallocFailed ){
144111 pDb = &db->aDb[iDb];
144113 if( rc==SQLITE_OK || ((db->flags&SQLITE_NoSchemaError) && rc!=SQLITE_NOMEM)){
144134 sqlite3BtreeCommit(pDb->pBt);
144136 sqlite3BtreeLeave(pDb->pBt);
144145 db->init.busy = 0;
144150 ** Initialize all database files - the main database file, the file
144160 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
144162 assert( sqlite3_mutex_held(db->mutex) );
144163 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
144164 assert( db->init.busy==0 );
144166 assert( db->nDb>0 );
144173 for(i=db->nDb-1; i>0; i--){
144174 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
144187 ** This routine is a no-op if the database schema is already initialized.
144192 sqlite3 *db = pParse->db;
144193 assert( sqlite3_mutex_held(db->mutex) );
144194 if( !db->init.busy ){
144195 rc = sqlite3Init(db, &pParse->zErrMsg);
144197 pParse->rc = rc;
144198 pParse->nErr++;
144199 }else if( db->noSharedCache ){
144200 db->mDbFlags |= DBFLAG_SchemaKnownOk;
144209 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
144210 ** make no changes to pParse->rc.
144213 sqlite3 *db = pParse->db;
144218 assert( pParse->checkSchema );
144219 assert( sqlite3_mutex_held(db->mutex) );
144220 for(iDb=0; iDb<db->nDb; iDb++){
144222 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
144225 /* If there is not already a read-only (or read-write) transaction opened
144226 ** on the b-tree database, open one now. If a transaction is opened, it
144227 ** will be closed immediately after reading the meta-value. */
144232 pParse->rc = SQLITE_NOMEM;
144239 ** value stored as part of the in-memory schema representation,
144243 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
144244 if( DbHasProperty(db, iDb, DB_SchemaLoaded) ) pParse->rc = SQLITE_SCHEMA;
144257 ** which database file in db->aDb[] the schema refers to.
144263 int i = -32768;
144265 /* If pSchema is NULL, then return -32768. This happens when code in
144267 ** created by a sub-select). In this case the return value of this
144270 ** We return -32768 instead of the more usual -1 simply because using
144271 ** -32768 as the incorrect index into db->aDb[] is much
144272 ** more likely to cause a segfault than -1 (of course there are assert()
144274 ** -32768 will still fit into a 16-bit signed integer.
144276 assert( sqlite3_mutex_held(db->mutex) );
144279 assert( i<db->nDb );
144280 if( db->aDb[i].pSchema==pSchema ){
144284 assert( i>=0 && i<db->nDb );
144293 sqlite3 *db = pParse->db;
144295 assert( db->pParse==pParse );
144296 assert( pParse->nested==0 );
144298 if( pParse->aTableLock ) sqlite3DbNNFreeNN(db, pParse->aTableLock);
144300 while( pParse->pCleanup ){
144301 ParseCleanup *pCleanup = pParse->pCleanup;
144302 pParse->pCleanup = pCleanup->pNext;
144303 pCleanup->xCleanup(db, pCleanup->pPtr);
144306 if( pParse->aLabel ) sqlite3DbNNFreeNN(db, pParse->aLabel);
144307 if( pParse->pConstExpr ){
144308 sqlite3ExprListDelete(db, pParse->pConstExpr);
144310 assert( db->lookaside.bDisable >= pParse->disableLookaside );
144311 db->lookaside.bDisable -= pParse->disableLookaside;
144312 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
144313 assert( pParse->db->pParse==pParse );
144314 db->pParse = pParse->pOuterParse;
144325 ** common cleanups, we save a single NULL-pointer comparison in
144330 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
144332 ** use-after-free errors following an OOM. The preferred way to do this is
144335 ** testcase( pParse->earlyCleanup );
144353 sqlite3OomFault(pParse->db);
144355 pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
144358 pCleanup->pNext = pParse->pCleanup;
144359 pParse->pCleanup = pCleanup;
144360 pCleanup->pPtr = pPtr;
144361 pCleanup->xCleanup = xCleanup;
144363 xCleanup(pParse->db, pPtr);
144366 pParse->earlyCleanup = 1;
144384 assert( db->pParse!=pParse );
144385 pParse->pOuterParse = db->pParse;
144386 db->pParse = pParse;
144387 pParse->db = db;
144388 if( db->mallocFailed ) sqlite3ErrorMsg(pParse, "out of memory");
144400 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
144404 const char *zSql, /* UTF-8 encoded SQL statement. */
144405 int nBytes, /* Length of zSql in bytes. */
144418 sParse.pOuterParse = db->pParse;
144419 db->pParse = &sParse;
144428 if( db->mallocFailed ){
144430 db->errCode = rc = SQLITE_NOMEM;
144433 assert( sqlite3_mutex_held(db->mutex) );
144435 /* For a long-term use prepared statement avoid the use of
144446 ** some other database connection is holding a write-lock, which in
144467 if( !db->noSharedCache ){
144468 for(i=0; i<db->nDb; i++) {
144469 Btree *pBt = db->aDb[i].pBt;
144474 const char *zDb = db->aDb[i].zDbSName;
144476 testcase( db->flags & SQLITE_ReadUncommit );
144484 if( db->pDisconnect ) sqlite3VtabUnlockList(db);
144487 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
144489 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
144500 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
144514 if( db->init.busy==0 ){
144515 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
144517 if( db->mallocFailed ){
144522 if( sParse.checkSchema && db->init.busy==0 ){
144547 sParse.pTriggerPrg = pT->pNext;
144558 const char *zSql, /* UTF-8 encoded SQL statement. */
144559 int nBytes, /* Length of zSql in bytes. */
144575 sqlite3_mutex_enter(db->mutex);
144583 if( rc==SQLITE_OK || db->mallocFailed ) break;
144585 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
144588 assert( (rc&db->errMask)==rc );
144589 db->busyHandler.nBusy = 0;
144590 sqlite3_mutex_leave(db->mutex);
144611 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
144615 assert( sqlite3_mutex_held(db->mutex) );
144617 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
144645 const char *zSql, /* UTF-8 encoded SQL statement. */
144646 int nBytes, /* Length of zSql in bytes. */
144657 const char *zSql, /* UTF-8 encoded SQL statement. */
144658 int nBytes, /* Length of zSql in bytes. */
144663 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
144675 const char *zSql, /* UTF-8 encoded SQL statement. */
144676 int nBytes, /* Length of zSql in bytes. */
144682 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
144699 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
144703 const void *zSql, /* UTF-16 encoded SQL statement. */
144704 int nBytes, /* Length of zSql in bytes. */
144709 /* This function currently works by first transforming the UTF-16
144710 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
144725 /* Make sure nBytes is non-negative and correct. It should be the
144742 sqlite3_mutex_enter(db->mutex);
144745 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
144750 ** equivalent pointer into the UTF-16 string by counting the unicode
144752 ** the same number of characters into the UTF-16 string.
144754 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
144759 sqlite3_mutex_leave(db->mutex);
144773 const void *zSql, /* UTF-16 encoded SQL statement. */
144774 int nBytes, /* Length of zSql in bytes. */
144785 const void *zSql, /* UTF-16 encoded SQL statement. */
144786 int nBytes, /* Length of zSql in bytes. */
144797 const void *zSql, /* UTF-16 encoded SQL statement. */
144798 int nBytes, /* Length of zSql in bytes. */
144848 ** The aDefer[] array is used by the sorter-references optimization. For
144858 ** When the sorter-reference optimization is used, there is one entry in the
144867 int regReturn; /* Register holding block-output return address */
144868 int labelBkOut; /* Start label for the block-output subroutine */
144899 Select *pPrior = p->pPrior;
144900 sqlite3ExprListDelete(db, p->pEList);
144901 sqlite3SrcListDelete(db, p->pSrc);
144902 sqlite3ExprDelete(db, p->pWhere);
144903 sqlite3ExprListDelete(db, p->pGroupBy);
144904 sqlite3ExprDelete(db, p->pHaving);
144905 sqlite3ExprListDelete(db, p->pOrderBy);
144906 sqlite3ExprDelete(db, p->pLimit);
144907 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
144909 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
144910 sqlite3WindowListDelete(db, p->pWinDefn);
144912 while( p->pWin ){
144913 assert( p->pWin->ppThis==&p->pWin );
144914 sqlite3WindowUnlinkFromSelect(p->pWin);
144927 pDest->eDest = (u8)eDest;
144928 pDest->iSDParm = iParm;
144929 pDest->iSDParm2 = 0;
144930 pDest->zAffSdst = 0;
144931 pDest->iSdst = 0;
144932 pDest->nSdst = 0;
144943 SrcList *pSrc, /* the FROM clause -- which tables to scan */
144953 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
144955 assert( pParse->db->mallocFailed );
144960 sqlite3Expr(pParse->db,TK_ASTERISK,0));
144962 pNew->pEList = pEList;
144963 pNew->op = TK_SELECT;
144964 pNew->selFlags = selFlags;
144965 pNew->iLimit = 0;
144966 pNew->iOffset = 0;
144967 pNew->selId = ++pParse->nSelect;
144968 pNew->addrOpenEphm[0] = -1;
144969 pNew->addrOpenEphm[1] = -1;
144970 pNew->nSelectRow = 0;
144971 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);
144972 pNew->pSrc = pSrc;
144973 pNew->pWhere = pWhere;
144974 pNew->pGroupBy = pGroupBy;
144975 pNew->pHaving = pHaving;
144976 pNew->pOrderBy = pOrderBy;
144977 pNew->pPrior = 0;
144978 pNew->pNext = 0;
144979 pNew->pLimit = pLimit;
144980 pNew->pWith = 0;
144982 pNew->pWin = 0;
144983 pNew->pWinDefn = 0;
144985 if( pParse->db->mallocFailed ) {
144986 clearSelect(pParse->db, pNew, pNew!=&standin);
144989 assert( pNew->pSrc!=0 || pParse->nErr>0 );
145006 ** Return a pointer to the right-most SELECT statement in a compound.
145009 while( p->pNext ) p = p->pNext;
145034 ** ------- ----- ----- ------------
145035 ** CROSS - - JT_CROSS
145036 ** INNER - - JT_INNER
145037 ** LEFT - - JT_LEFT|JT_OUTER
145038 ** LEFT OUTER - JT_LEFT|JT_OUTER
145039 ** RIGHT - - JT_RIGHT|JT_OUTER
145040 ** RIGHT OUTER - JT_RIGHT|JT_OUTER
145041 ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER
145042 ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER
145043 ** NATURAL INNER - JT_NATURAL|JT_INNER
145044 ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER
145046 ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER
145048 ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT
145052 ** of other non-standard and in many cases nonsensical join types.
145057 ** INNER CROSS JOIN -> same as JOIN
145058 ** NATURAL CROSS JOIN -> same as NATURAL JOIN
145059 ** OUTER LEFT JOIN -> same as LEFT JOIN
145060 ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN
145061 ** LEFT RIGHT JOIN -> same as FULL JOIN
145062 ** RIGHT OUTER FULL JOIN -> same as FULL JOIN
145063 ** CROSS CROSS CROSS JOIN -> same as JOIN
145084 u8 nChar; /* Length of the keyword in characters */
145102 if( p->n==aKeyword[j].nChar
145103 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
145131 ** Return the index of a column in a table. Return -1 if the column
145141 aCol = pTab->aCol;
145142 nCol = pTab->nCol;
145145 i = pTab->aHx[h % sizeof(pTab->aHx)];
145155 while( 1 /*exit-by-break*/ ){
145164 return -1;
145172 assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem) );
145173 if( pItem->fg.isNestedFrom ){
145175 assert( pItem->fg.isSubquery );
145176 assert( pItem->u4.pSubq!=0 );
145177 assert( pItem->u4.pSubq->pSelect!=0 );
145178 pResults = pItem->u4.pSubq->pSelect->pEList;
145180 assert( iCol>=0 && iCol<pResults->nExpr );
145181 pResults->a[iCol].fg.bUsed = 1;
145187 ** table that has a column named zCol. The search is left-to-right.
145197 int iStart, /* First member of pSrc->a[] to check */
145198 int iEnd, /* Last member of pSrc->a[] to check */
145200 int *piTab, /* Write index of pSrc->a[] here */
145201 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
145207 assert( iEnd<pSrc->nSrc );
145212 iCol = sqlite3ColumnIndex(pSrc->a[i].pSTab, zCol);
145214 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pSTab->aCol[iCol])==0)
145217 sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol);
145259 p->w.iJoin = iTable;
145260 if( p->op==TK_FUNCTION ){
145262 if( p->x.pList ){
145264 for(i=0; i<p->x.pList->nExpr; i++){
145265 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag);
145269 sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag);
145270 p = p->pRight;
145290 if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){
145294 if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){
145297 if( p->op==TK_FUNCTION ){
145299 assert( p->pLeft==0 );
145300 if( p->x.pList ){
145302 for(i=0; i<p->x.pList->nExpr; i++){
145303 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable);
145307 unsetJoinExpr(p->pLeft, iTable, nullable);
145308 p = p->pRight;
145325 ** The left most table is the first entry in Select.pSrc. The right-most
145339 pSrc = p->pSrc;
145340 pLeft = &pSrc->a[0];
145342 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
145343 Table *pRightTab = pRight->pSTab;
145346 if( NEVER(pLeft->pSTab==0 || pRightTab==0) ) continue;
145347 joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON;
145352 if( pRight->fg.jointype & JT_NATURAL ){
145354 if( pRight->fg.isUsing || pRight->u3.pOn ){
145359 for(j=0; j<pRightTab->nCol; j++){
145362 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
145363 zName = pRightTab->aCol[j].zCnName;
145367 assert( pUsing->nId>0 );
145368 assert( pUsing->a[pUsing->nId-1].zName==0 );
145369 pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName);
145374 pRight->fg.isUsing = 1;
145375 pRight->fg.isSynthUsing = 1;
145376 pRight->u3.pUsing = pUsing;
145378 if( pParse->nErr ) return 1;
145388 if( pRight->fg.isUsing ){
145389 IdList *pList = pRight->u3.pUsing;
145390 sqlite3 *db = pParse->db;
145392 for(j=0; j<pList->nId; j++){
145401 zName = pList->a[j].zName;
145405 pRight->fg.isSynthUsing)==0
145407 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
145412 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
145413 if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 && pParse->nErr==0 ){
145416 ** contains the zName column, then this branch is a no-op.
145423 ** non-USING references to zName on the left of an INNER or LEFT
145432 pRight->fg.isSynthUsing)!=0 ){
145433 if( pSrc->a[iLeft].fg.isUsing==0
145434 || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0
145442 sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol);
145448 pE1->affExpr = SQLITE_AFF_DEFER;
145451 }else if( (pSrc->a[i+1].fg.jointype & JT_LEFT)!=0 && pParse->nErr==0 ){
145463 pEq->w.iJoin = pE2->iTable;
145465 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq);
145472 else if( pRight->u3.pOn ){
145473 sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType);
145474 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn);
145475 pRight->u3.pOn = 0;
145476 pRight->fg.isOn = 1;
145505 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
145506 0, pInfo->ecelFlags);
145508 if( pInfo->pExtra ){
145509 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
145510 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
145528 int nOBSat = pSort->nOBSat;
145529 Vdbe *v = pParse->pVdbe;
145530 int regOut = ++pParse->nMem;
145531 if( pSort->pDeferredRowLoad ){
145532 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
145534 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
145540 ** through regData+nData-1 onto the sorter.
145551 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
145552 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
145553 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
145557 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
145580 pSort->addrPush = sqlite3VdbeCurrentAddr(v);
145585 regBase = regData - nPrefixReg;
145587 regBase = pParse->nMem + 1;
145588 pParse->nMem += nBase;
145590 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
145591 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
145592 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
145593 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
145596 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
145610 regPrevKey = pParse->nMem+1;
145611 pParse->nMem += pSort->nOBSat;
145612 nKey = nExpr - pSort->nOBSat + bSeq;
145616 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
145619 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
145620 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
145621 if( pParse->db->mallocFailed ) return;
145622 pOp->p2 = nKey + nData;
145623 pKI = pOp->p4.pKeyInfo;
145624 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
145625 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
145626 testcase( pKI->nAllField > pKI->nKeyField+2 );
145627 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
145628 pKI->nAllField-pKI->nKeyField-1);
145632 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
145633 pSort->regReturn = ++pParse->nMem;
145634 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
145635 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
145637 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
145641 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
145655 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
145660 int iCsr = pSort->iECursor;
145665 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
145672 if( pSort->sortFlags & SORTFLAG_UseSorter ){
145677 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
145678 regBase+nOBSat, nBase-nOBSat);
145681 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
145684 pSort->addrPushEnd = sqlite3VdbeCurrentAddr(v)-1;
145754 int nResultCol = pEList->nExpr;
145755 Vdbe *v = pParse->pVdbe;
145764 iRet = regPrev = pParse->nMem+1;
145765 pParse->nMem += nResultCol;
145769 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
145770 if( i<nResultCol-1 ){
145777 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
145780 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
145781 sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1);
145816 ** No adjustments necessary. This function is a no-op.
145835 if( pParse->nErr==0
145838 Vdbe *v = pParse->pVdbe;
145840 if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){
145849 pOp->opcode = OP_Null;
145850 pOp->p1 = 1;
145851 pOp->p2 = iVal;
145858 ** This function is called as part of inner-loop generation for a SELECT
145860 ** determines the expressions, if any, that the sorter-reference
145861 ** optimization should be used for. The sorter-reference optimization
145874 ** for which the sorter-reference optimization should be enabled.
145875 ** Additionally, the pSort->aDefer[] array is populated with entries
145890 for(i=0; i<pEList->nExpr; i++){
145891 struct ExprList_item *pItem = &pEList->a[i];
145892 if( pItem->u.x.iOrderByCol==0 ){
145893 Expr *pExpr = pItem->pExpr;
145895 if( pExpr->op==TK_COLUMN
145896 && pExpr->iColumn>=0
145898 && (pTab = pExpr->y.pTab)!=0
145900 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0
145904 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
145907 if( nDefer==ArraySize(pSort->aDefer) ){
145915 nKey = pPk->nKeyCol;
145920 pNew->iTable = pExpr->iTable;
145922 pNew->y.pTab = pExpr->y.pTab;
145923 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
145927 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
145928 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
145929 pSort->aDefer[nDefer].nKey = nKey;
145933 pItem->fg.bSorterRef = 1;
145937 pSort->nDefer = (u8)nDefer;
145946 ** If srcTab is negative, then the p->pEList expressions
145948 ** zero or more, then data is pulled from srcTab and p->pEList is used only
145954 int srcTab, /* Pull data from this table if non-negative */
145961 Vdbe *v = pParse->pVdbe;
145964 int eDest = pDest->eDest; /* How to dispose of results */
145965 int iParm = pDest->iSDParm; /* First argument to disposal method */
145973 ** values for any expressions that are also part of the sort-key are omitted
145979 assert( p->pEList!=0 );
145980 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
145981 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
145984 codeOffset(v, p->iOffset, iContinue);
145989 nResultCol = p->pEList->nExpr;
145991 if( pDest->iSdst==0 ){
145993 nPrefixReg = pSort->pOrderBy->nExpr;
145994 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
145995 pParse->nMem += nPrefixReg;
145997 pDest->iSdst = pParse->nMem+1;
145998 pParse->nMem += nResultCol;
145999 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
146001 ** on the right-hand side of an INSERT contains more result columns than
146005 pParse->nMem += nResultCol;
146007 pDest->nSdst = nResultCol;
146008 regOrig = regResult = pDest->iSdst;
146012 VdbeComment((v, "%s", p->pEList->a[i].zEName));
146029 /* For each expression in p->pEList that is a copy of an expression in
146030 ** the ORDER BY clause (pSort->pOrderBy), set the associated
146032 ** expression within the sort-key that pushOntoSorter() will generate.
146033 ** This allows the p->pEList field to be omitted from the sorted record,
146037 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
146039 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
146040 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
146044 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
146045 if( pExtra && pParse->db->mallocFailed==0 ){
146051 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
146052 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
146053 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
146054 pParse->nMem += pExtra->nExpr;
146060 pEList = p->pEList;
146061 for(i=0; i<pEList->nExpr; i++){
146062 if( pEList->a[i].u.x.iOrderByCol>0
146064 || pEList->a[i].fg.bSorterRef
146067 nResultCol--;
146086 if( pExtra ) nResultCol += pExtra->nExpr;
146088 if( p->iLimit
146094 pSort->pDeferredRowLoad = &sRowLoadInfo;
146106 int eType = pDistinct->eTnctType;
146107 int iTab = pDistinct->tabTnct;
146108 assert( nResultCol==p->pEList->nExpr );
146109 iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult);
146110 fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct);
146112 codeOffset(v, p->iOffset, iContinue);
146153 /* A destination of SRT_Table and a non-zero iSDParm2 parameter means
146156 ** This does not affect operation in any way - it just allows MakeRecord
146158 if( eDest==SRT_Table && pDest->iSDParm2 ){
146195 int i2 = pDest->iSDParm2;
146204 regResult+(i2<0), nResultCol-(i2<0), r1);
146227 pDest->iSDParm2 = 0; /* Signal that any Bloom filter is unpopulated */
146230 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
146232 r1, pDest->zAffSdst, nResultCol);
146234 if( pDest->iSDParm2 ){
146235 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0,
146259 assert( nResultCol<=pDest->nSdst );
146263 assert( nResultCol==pDest->nSdst );
146271 case SRT_Coroutine: /* Send data to a co-routine */
146279 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
146288 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
146289 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
146290 ** pSO->nExpr columns, then make sure all keys are unique by adding a
146299 pSO = pDest->pOrderBy;
146301 nKey = pSO->nExpr;
146320 regResult + pSO->a[i].u.x.iOrderByCol - 1,
146338 ** user-defined functions that have side effects. We do not care
146352 if( pSort==0 && p->iLimit ){
146353 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
146365 p->aSortFlags = (u8*)&p->aColl[N+X];
146366 p->nKeyField = (u16)N;
146367 p->nAllField = (u16)(N+X);
146368 p->enc = ENC(db);
146369 p->db = db;
146370 p->nRef = 1;
146371 memset(p->aColl, 0, nExtra);
146383 assert( p->db!=0 );
146384 assert( p->nRef>0 );
146385 p->nRef--;
146386 if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p);
146395 assert( p->nRef>0 );
146396 p->nRef++;
146408 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
146434 sqlite3 *db = pParse->db;
146437 nExpr = pList->nExpr;
146438 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
146441 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
146442 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
146443 pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags;
146466 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
146469 ** "USE TEMP B-TREE FOR xxx"
146475 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
146479 ** Assign expression b to lvalue a. A second, no-op, version of this macro
146488 /* No-op versions of the explainXXX() functions and macros. */
146495 ** If the inner loop was generated using a non-null pOrderBy argument,
146507 Vdbe *v = pParse->pVdbe; /* The prepared statement */
146508 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
146513 ExprList *pOrderBy = pSort->pOrderBy;
146514 int eDest = pDest->eDest;
146515 int iParm = pDest->iSDParm;
146524 struct ExprList_item *aOutEx = p->pEList->a;
146529 nKey = pOrderBy->nExpr - pSort->nOBSat;
146530 if( pSort->nOBSat==0 || nKey==1 ){
146532 "USE TEMP B-TREE FOR %sORDER BY", pSort->nOBSat?"LAST TERM OF ":""
146536 "USE TEMP B-TREE FOR LAST %d TERMS OF ORDER BY", nKey
146539 sqlite3VdbeScanStatusRange(v, addrExplain,pSort->addrPush,pSort->addrPushEnd);
146540 sqlite3VdbeScanStatusCounters(v, addrExplain, addrExplain, pSort->addrPush);
146544 if( pSort->labelBkOut ){
146545 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
146547 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
146551 /* Open any cursors needed for sorter-reference expressions */
146552 for(i=0; i<pSort->nDefer; i++){
146553 Table *pTab = pSort->aDefer[i].pTab;
146554 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
146555 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
146556 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
146560 iTab = pSort->iECursor;
146562 if( eDest==SRT_Mem && p->iOffset ){
146563 sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst);
146566 regRow = pDest->iSdst;
146576 if( pSort->sortFlags & SORTFLAG_UseSorter ){
146577 int regSortOut = ++pParse->nMem;
146578 iSortTab = pParse->nTab++;
146579 if( pSort->labelBkOut ){
146587 assert( p->iLimit==0 && p->iOffset==0 );
146592 codeOffset(v, p->iOffset, addrContinue);
146595 if( p->iOffset>0 ){
146596 sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1);
146599 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
146606 if( pSort->nDefer ){
146610 for(i=0; i<pSort->nDefer; i++){
146611 int iCsr = pSort->aDefer[i].iCsr;
146612 Table *pTab = pSort->aDefer[i].pTab;
146613 int nKey = pSort->aDefer[i].nKey;
146623 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
146636 for(i=nColumn-1; i>=0; i--){
146645 iRead = aOutEx[i].u.x.iOrderByCol-1;
146647 iRead = iCol--;
146653 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
146665 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
146667 pDest->zAffSdst, nColumn);
146677 int i2 = pDest->iSDParm2;
146679 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
146692 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
146694 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
146710 if( pSort->sortFlags & SORTFLAG_UseSorter ){
146715 sqlite3VdbeScanStatusRange(v, addrExplain, sqlite3VdbeCurrentAddr(v)-1, -1);
146716 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
146728 ** result-set expression in all of the following SELECT statements is
146739 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
146766 assert( pNC->pSrcList!=0 );
146767 switch( pExpr->op ){
146775 int iCol = pExpr->iColumn; /* Index of column in pTab */
146777 SrcList *pTabList = pNC->pSrcList;
146778 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
146779 if( j<pTabList->nSrc ){
146780 pTab = pTabList->a[j].pSTab;
146781 if( pTabList->a[j].fg.isSubquery ){
146782 pS = pTabList->a[j].u4.pSubq->pSelect;
146787 pNC = pNC->pNext;
146802 ** sub-select. In this case, set the column type to NULL, even
146812 assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab );
146814 /* The "table" is actually a sub-select or a view in the FROM clause
146816 ** data for the result-set column of the sub-select.
146818 if( iCol<pS->pEList->nExpr
146822 ** rowid of the sub-select or view. This expression is legal (see
146823 ** test case misc2.2.2) - it always evaluates to NULL.
146826 Expr *p = pS->pEList->a[iCol].pExpr;
146827 sNC.pSrcList = pS->pSrc;
146829 sNC.pParse = pNC->pParse;
146836 if( iCol<0 ) iCol = pTab->iPKey;
146837 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
146842 zOrigCol = pTab->aCol[iCol].zCnName;
146843 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
146845 zOrigTab = pTab->zName;
146846 if( pNC->pParse && pTab->pSchema ){
146847 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
146848 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
146851 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
146855 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
146863 /* The expression is a sub-select. Return the declaration type and
146871 pS = pExpr->x.pSelect;
146872 p = pS->pEList->a[0].pExpr;
146873 sNC.pSrcList = pS->pSrc;
146875 sNC.pParse = pNC->pParse;
146903 Vdbe *v = pParse->pVdbe;
146909 for(i=0; i<pEList->nExpr; i++){
146910 Expr *p = pEList->a[i].pExpr;
146918 /* The vdbe must make its own copy of the column-type and other
146968 Vdbe *v = pParse->pVdbe;
146973 sqlite3 *db = pParse->db;
146977 if( pParse->colNamesSet ) return;
146978 /* Column names are determined by the left-most term of a compound select */
146979 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
146981 pTabList = pSelect->pSrc;
146982 pEList = pSelect->pEList;
146985 pParse->colNamesSet = 1;
146986 fullName = (db->flags & SQLITE_FullColNames)!=0;
146987 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
146988 sqlite3VdbeSetNumCols(v, pEList->nExpr);
146989 for(i=0; i<pEList->nExpr; i++){
146990 Expr *p = pEList->a[i].pExpr;
146993 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
146994 assert( p->op!=TK_COLUMN
146995 || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */
146996 if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){
146998 char *zName = pEList->a[i].zEName;
147000 }else if( srcName && p->op==TK_COLUMN ){
147002 int iCol = p->iColumn;
147003 pTab = p->y.pTab;
147005 if( iCol<0 ) iCol = pTab->iPKey;
147006 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
147010 zCol = pTab->aCol[iCol].zCnName;
147014 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
147020 const char *z = pEList->a[i].zEName;
147056 sqlite3 *db = pParse->db; /* Database connection */
147068 nCol = pEList->nExpr;
147080 for(i=0, pCol=aCol; i<nCol && !pParse->nErr; i++, pCol++){
147081 struct ExprList_item *pX = &pEList->a[i];
147085 if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){
147088 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr);
147089 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
147090 pColExpr = pColExpr->pRight;
147093 if( pColExpr->op==TK_COLUMN
147095 && ALWAYS( pColExpr->y.pTab!=0 )
147098 int iCol = pColExpr->iColumn;
147099 pTab = pColExpr->y.pTab;
147100 if( iCol<0 ) iCol = pTab->iPKey;
147101 zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid";
147102 }else if( pColExpr->op==TK_ID ){
147104 zName = pColExpr->u.zToken;
147107 assert( zName==pX->zEName ); /* pointer comparison intended */
147121 if( pCollide->fg.bUsingTerm ){
147122 pCol->colFlags |= COLFLAG_NOEXPAND;
147126 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
147135 pCol->zCnName = zName;
147136 pCol->hName = sqlite3StrIHash(zName);
147137 if( pX->fg.bNoExpand ){
147138 pCol->colFlags |= COLFLAG_NOEXPAND;
147146 if( pParse->nErr ){
147153 return pParse->rc;
147175 sqlite3 *db = pParse->db;
147184 assert( (pSelect->selFlags & SF_Resolved)!=0 );
147185 assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
147187 if( db->mallocFailed || IN_RENAME_OBJECT ) return;
147188 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
147189 a = pSelect->pEList->a;
147191 sNC.pSrcList = pSelect->pSrc;
147192 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
147197 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
147199 /* pCol->szEst = ... // Column size est for SELECT tables never used */
147200 pCol->affinity = sqlite3ExprAffinity(p);
147201 while( pCol->affinity<=SQLITE_AFF_NONE && pS2->pNext!=0 ){
147202 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
147203 pS2 = pS2->pNext;
147204 pCol->affinity = sqlite3ExprAffinity(pS2->pEList->a[i].pExpr);
147206 if( pCol->affinity<=SQLITE_AFF_NONE ){
147207 pCol->affinity = aff;
147209 if( pCol->affinity>=SQLITE_AFF_TEXT && (pS2->pNext || pS2!=pSelect) ){
147210 for(pS2=pS2->pNext; pS2; pS2=pS2->pNext){
147211 m |= sqlite3ExprDataType(pS2->pEList->a[i].pExpr);
147213 if( pCol->affinity==SQLITE_AFF_TEXT && (m&0x01)!=0 ){
147214 pCol->affinity = SQLITE_AFF_BLOB;
147216 if( pCol->affinity>=SQLITE_AFF_NUMERIC && (m&0x02)!=0 ){
147217 pCol->affinity = SQLITE_AFF_BLOB;
147219 if( pCol->affinity>=SQLITE_AFF_NUMERIC && p->op==TK_CAST ){
147220 pCol->affinity = SQLITE_AFF_FLEXNUM;
147224 if( zType==0 || pCol->affinity!=sqlite3AffinityType(zType, 0) ){
147225 if( pCol->affinity==SQLITE_AFF_NUMERIC
147226 || pCol->affinity==SQLITE_AFF_FLEXNUM
147232 if( sqlite3StdTypeAffinity[j]==pCol->affinity ){
147241 n = sqlite3Strlen30(pCol->zCnName);
147242 pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+k+2);
147243 pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL);
147244 if( pCol->zCnName ){
147245 memcpy(&pCol->zCnName[n+1], zType, k+1);
147246 pCol->colFlags |= COLFLAG_HASTYPE;
147251 assert( pTab->pIndex==0 );
147252 sqlite3ColumnSetColl(db, pCol, pColl->zName);
147255 pTab->szTabRow = 1; /* Any non-zero value works */
147264 sqlite3 *db = pParse->db;
147267 savedFlags = db->flags;
147268 db->flags &= ~(u64)SQLITE_FullColNames;
147269 db->flags |= SQLITE_ShortColNames;
147271 db->flags = savedFlags;
147272 if( pParse->nErr ) return 0;
147273 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
147278 pTab->nTabRef = 1;
147279 pTab->zName = 0;
147280 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
147281 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
147283 pTab->iPKey = -1;
147284 if( db->mallocFailed ){
147296 if( pParse->pVdbe ){
147297 return pParse->pVdbe;
147299 if( pParse->pToplevel==0
147300 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
147302 pParse->okConstFactor = 1;
147310 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
147318 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
147326 ** Only if pLimit->pLeft!=0 do the limit registers get
147336 Expr *pLimit = p->pLimit;
147338 if( p->iLimit ) return;
147341 ** "LIMIT -1" always shows all rows. There is some
147347 assert( pLimit->op==TK_LIMIT );
147348 assert( pLimit->pLeft!=0 );
147349 p->iLimit = iLimit = ++pParse->nMem;
147352 if( sqlite3ExprIsInteger(pLimit->pLeft, &n, pParse) ){
147357 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
147358 p->nSelectRow = sqlite3LogEst((u64)n);
147359 p->selFlags |= SF_FixedLimit;
147362 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
147367 if( pLimit->pRight ){
147368 p->iOffset = iOffset = ++pParse->nMem;
147369 pParse->nMem++; /* Allocate an extra register for limit+offset */
147370 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
147381 ** Return the appropriate collating sequence for the iCol-th column of
147382 ** the result set for the compound-select statement "p". Return NULL if
147386 ** left-most term of the select that has a collating sequence.
147390 if( p->pPrior ){
147391 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
147396 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
147399 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
147400 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
147415 ExprList *pOrderBy = p->pOrderBy;
147416 int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0;
147417 sqlite3 *db = pParse->db;
147422 struct ExprList_item *pItem = &pOrderBy->a[i];
147423 Expr *pTerm = pItem->pExpr;
147426 if( pTerm->flags & EP_Collate ){
147429 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
147430 if( pColl==0 ) pColl = db->pDfltColl;
147431 pOrderBy->a[i].pExpr =
147432 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
147435 pRet->aColl[i] = pColl;
147436 pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags;
147448 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
147450 ** p->pPrior p
147453 ** There is exactly one reference to the recursive-table in the FROM clause
147454 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
147456 ** The setup-query runs once to generate an initial set of rows that go
147460 ** recursive-table for a recursive-query run. The output of the recursive-query
147485 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
147486 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
147487 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
147489 Select *pFirstRec; /* Left-most recursive term */
147505 if( p->pWin ){
147516 p->nSelectRow = 320; /* 4 billion rows */
147518 pLimit = p->pLimit;
147519 regLimit = p->iLimit;
147520 regOffset = p->iOffset;
147521 p->pLimit = 0;
147522 p->iLimit = p->iOffset = 0;
147523 pOrderBy = p->pOrderBy;
147526 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
147527 if( pSrc->a[i].fg.isRecursive ){
147528 iCurrent = pSrc->a[i].iCursor;
147536 iQueue = pParse->nTab++;
147537 if( p->op==TK_UNION ){
147539 iDistinct = pParse->nTab++;
147546 regCurrent = ++pParse->nMem;
147550 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
147558 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
147559 p->selFlags |= SF_UsesEphemeral;
147563 p->pOrderBy = 0;
147569 ** iDistinct table. pFirstRec is left pointing to the left-most
147572 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
147573 if( pFirstRec->selFlags & SF_Aggregate ){
147577 pFirstRec->op = TK_ALL;
147578 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
147581 /* Store the results of the setup-query in Queue. */
147582 pSetup = pFirstRec->pPrior;
147583 pSetup->pNext = 0;
147586 pSetup->pNext = p;
147595 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
147613 ** the value for the recursive-table. Store the results in the Queue.
147615 pFirstRec->pPrior = 0;
147618 assert( pFirstRec->pPrior==0 );
147619 pFirstRec->pPrior = pSetup;
147626 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
147627 p->pOrderBy = pOrderBy;
147628 p->pLimit = pLimit;
147636 Select *p, /* The right-most of SELECTs to be coded */
147641 ** Handle the special case of a compound-select that originates from a
147654 ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES.
147658 Select *p, /* The right-most of SELECTs to be coded */
147663 int bShowAll = p->pLimit==0;
147664 assert( p->selFlags & SF_MultiValue );
147666 assert( p->selFlags & SF_Values );
147667 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
147668 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
147670 if( p->pWin ) return -1;
147672 if( p->pPrior==0 ) break;
147673 assert( p->pPrior->pNext==p );
147674 p = p->pPrior;
147680 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
147682 p->nSelectRow = nRow;
147683 p = p->pNext;
147694 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
147703 ** "p" points to the right-most of the two queries. the query on the
147704 ** left is p->pPrior. The left query could also be a compound query
147710 ** Example 1: Consider a three-way compound SQL statement.
147718 ** `-----> SELECT b FROM t2
147720 ** `------> SELECT a FROM t1
147724 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
147731 Select *p, /* The right-most of SELECTs to be coded */
147742 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
147744 assert( p && p->pPrior ); /* Calling function guarantees this much */
147745 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
147746 assert( p->selFlags & SF_Compound );
147747 db = pParse->db;
147748 pPrior = p->pPrior;
147750 assert( pPrior->pOrderBy==0 );
147751 assert( pPrior->pLimit==0 );
147759 assert( p->pEList );
147760 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
147764 /* Special handling for a compound-select that originates as a VALUES clause.
147766 if( p->selFlags & SF_MultiValue ){
147775 assert( p->pEList && pPrior->pEList );
147776 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
147779 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
147786 if( p->pOrderBy ){
147791 if( pPrior->pPrior==0 ){
147793 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
147799 switch( p->op ){
147803 assert( !pPrior->pLimit );
147804 pPrior->iLimit = p->iLimit;
147805 pPrior->iOffset = p->iOffset;
147806 pPrior->pLimit = p->pLimit;
147809 pPrior->pLimit = 0;
147813 p->pPrior = 0;
147814 p->iLimit = pPrior->iLimit;
147815 p->iOffset = pPrior->iOffset;
147816 if( p->iLimit ){
147817 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
147819 if( p->iOffset ){
147821 p->iLimit, p->iOffset+1, p->iOffset);
147828 pDelete = p->pPrior;
147829 p->pPrior = pPrior;
147830 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
147831 if( p->pLimit
147832 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit, pParse)
147833 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
147835 p->nSelectRow = sqlite3LogEst((u64)nLimit);
147847 Expr *pLimit; /* Saved values of p->nLimit */
147851 testcase( p->op==TK_EXCEPT );
147852 testcase( p->op==TK_UNION );
147858 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
147864 unionTab = pParse->nTab++;
147865 assert( p->pOrderBy==0 );
147867 assert( p->addrOpenEphm[0] == -1 );
147868 p->addrOpenEphm[0] = addr;
147869 findRightmost(p)->selFlags |= SF_UsesEphemeral;
147870 assert( p->pEList );
147876 assert( !pPrior->pOrderBy );
147886 if( p->op==TK_EXCEPT ){
147889 assert( p->op==TK_UNION );
147892 p->pPrior = 0;
147893 pLimit = p->pLimit;
147894 p->pLimit = 0;
147896 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
147897 sqlite3SelectOpName(p->op)));
147901 assert( p->pOrderBy==0 );
147902 pDelete = p->pPrior;
147903 p->pPrior = pPrior;
147904 p->pOrderBy = 0;
147905 if( p->op==TK_UNION ){
147906 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
147908 sqlite3ExprDelete(db, p->pLimit);
147909 p->pLimit = pLimit;
147910 p->iLimit = 0;
147911 p->iOffset = 0;
147917 assert( p->pEList || db->mallocFailed );
147918 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
147934 default: assert( p->op==TK_INTERSECT ); {
147946 tab1 = pParse->nTab++;
147947 tab2 = pParse->nTab++;
147948 assert( p->pOrderBy==0 );
147951 assert( p->addrOpenEphm[0] == -1 );
147952 p->addrOpenEphm[0] = addr;
147953 findRightmost(p)->selFlags |= SF_UsesEphemeral;
147954 assert( p->pEList );
147968 assert( p->addrOpenEphm[1] == -1 );
147969 p->addrOpenEphm[1] = addr;
147970 p->pPrior = 0;
147971 pLimit = p->pLimit;
147972 p->pLimit = 0;
147974 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
147975 sqlite3SelectOpName(p->op)));
147979 pDelete = p->pPrior;
147980 p->pPrior = pPrior;
147981 if( p->nSelectRow>pPrior->nSelectRow ){
147982 p->nSelectRow = pPrior->nSelectRow;
147984 sqlite3ExprDelete(db, p->pLimit);
147985 p->pLimit = pLimit;
147991 assert( p->pEList );
148013 if( p->pNext==0 ){
148018 if( pParse->nErr ) goto multi_select_end;
148024 ** This section is run by the right-most SELECT statement only.
148025 ** SELECT statements to the left always skip this part. The right-most
148029 if( p->selFlags & SF_UsesEphemeral ){
148033 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
148036 assert( p->pNext==0 );
148037 assert( p->pEList!=0 );
148038 nCol = p->pEList->nExpr;
148044 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
148047 *apColl = db->pDfltColl;
148051 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
148053 int addr = pLoop->addrOpenEphm[i];
148057 assert( pLoop->addrOpenEphm[1]<0 );
148063 pLoop->addrOpenEphm[i] = -1;
148070 pDest->iSdst = dest.iSdst;
148071 pDest->nSdst = dest.nSdst;
148072 pDest->iSDParm2 = dest.iSDParm2;
148085 if( p->selFlags & SF_Values ){
148090 sqlite3SelectOpName(p->op));
148098 ** The data to be output is contained in pIn->iSdst. There are
148099 ** pIn->nSdst columns to be output. pDest is where the output should
148111 ** If the LIMIT found in p->iLimit is reached, jump immediately to
148124 Vdbe *v = pParse->pVdbe;
148136 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
148140 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
148143 if( pParse->db->mallocFailed ) return 0;
148147 codeOffset(v, p->iOffset, iContinue);
148149 assert( pDest->eDest!=SRT_Exists );
148150 assert( pDest->eDest!=SRT_Table );
148151 switch( pDest->eDest ){
148157 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
148158 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
148159 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
148171 testcase( pIn->nSdst>1 );
148173 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
148174 r1, pDest->zAffSdst, pIn->nSdst);
148175 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
148176 pIn->iSdst, pIn->nSdst);
148177 if( pDest->iSDParm2>0 ){
148178 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pDest->iSDParm2, 0,
148179 pIn->iSdst, pIn->nSdst);
148189 ** if it is the RHS of a row-value IN operator.
148192 testcase( pIn->nSdst>1 );
148193 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
148200 ** starting at pDest->iSdst. Then the co-routine yields.
148203 if( pDest->iSdst==0 ){
148204 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
148205 pDest->nSdst = pIn->nSdst;
148207 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
148208 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
148221 assert( pDest->eDest==SRT_Output );
148222 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
148229 if( p->iLimit ){
148230 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
148251 ** co-routines. Then run the co-routines in parallel and merge the results
148278 ** ------------- ----------------- -------------- -----------------
148329 Select *p, /* The right-most of SELECTs to be coded */
148334 Select *pSplit; /* Left-most SELECT in the right-hand group */
148339 int regAddrA; /* Address register for select-A coroutine */
148340 int regAddrB; /* Address register for select-B coroutine */
148341 int addrSelectA; /* Address of the select-A coroutine */
148342 int addrSelectB; /* Address of the select-B coroutine */
148343 int regOutA; /* Address register for the output-A subroutine */
148344 int regOutB; /* Address register for the output-B subroutine */
148345 int addrOutA; /* Address of the output-A subroutine */
148346 int addrOutB = 0; /* Address of the output-B subroutine */
148347 int addrEofA; /* Address of the select-A-exhausted subroutine */
148349 int addrEofB; /* Address of the select-B-exhausted subroutine */
148353 int regLimitA; /* Limit register for select-A */
148354 int regLimitB; /* Limit register for select-A */
148356 int savedLimit; /* Saved value of p->iLimit */
148357 int savedOffset; /* Saved value of p->iOffset */
148369 assert( p->pOrderBy!=0 );
148371 db = pParse->db;
148372 v = pParse->pVdbe;
148380 op = p->op;
148381 assert( p->pPrior->pOrderBy==0 );
148382 pOrderBy = p->pOrderBy;
148384 nOrderBy = pOrderBy->nExpr;
148391 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
148393 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
148395 assert( pItem->u.x.iOrderByCol>0 );
148396 if( pItem->u.x.iOrderByCol==i ) break;
148401 pNew->flags |= EP_IntValue;
148402 pNew->u.iValue = i;
148403 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
148404 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
148420 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
148422 assert( pItem->u.x.iOrderByCol>0 );
148423 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
148424 aPermute[i] = pItem->u.x.iOrderByCol - 1;
148438 int nExpr = p->pEList->nExpr;
148439 assert( nOrderBy>=nExpr || db->mallocFailed );
148440 regPrev = pParse->nMem+1;
148441 pParse->nMem += nExpr+1;
148447 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
148448 pKeyDup->aSortFlags[i] = 0;
148459 for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){
148461 assert( pSplit->pPrior->pNext==pSplit );
148468 for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; }
148470 pPrior = pSplit->pPrior;
148472 pSplit->pPrior = 0;
148473 pPrior->pNext = 0;
148474 assert( p->pOrderBy == pOrderBy );
148475 assert( pOrderBy!=0 || db->mallocFailed );
148476 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
148477 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
148478 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
148482 if( p->iLimit && op==TK_ALL ){
148483 regLimitA = ++pParse->nMem;
148484 regLimitB = ++pParse->nMem;
148485 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
148491 sqlite3ExprDelete(db, p->pLimit);
148492 p->pLimit = 0;
148494 regAddrA = ++pParse->nMem;
148495 regAddrB = ++pParse->nMem;
148496 regOutA = ++pParse->nMem;
148497 regOutB = ++pParse->nMem;
148501 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
148504 ** left of the compound operator - the "A" select.
148509 pPrior->iLimit = regLimitA;
148516 ** the right - the "B" select
148521 savedLimit = p->iLimit;
148522 savedOffset = p->iOffset;
148523 p->iLimit = regLimitB;
148524 p->iOffset = 0;
148527 p->iLimit = savedLimit;
148528 p->iOffset = savedOffset;
148556 VdbeNoopComment((v, "eof-A subroutine"));
148561 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
148569 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
148571 VdbeNoopComment((v, "eof-B subroutine"));
148579 VdbeNoopComment((v, "A-lt-B subroutine"));
148592 VdbeNoopComment((v, "A-eq-B subroutine"));
148600 VdbeNoopComment((v, "A-gt-B subroutine"));
148629 if( pSplit->pPrior ){
148630 sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSplit->pPrior);
148632 pSplit->pPrior = pPrior;
148633 pPrior->pNext = pSplit;
148634 sqlite3ExprListDelete(db, pPrior->pOrderBy);
148635 pPrior->pOrderBy = 0;
148640 return pParse->nErr!=0;
148655 ** position in the parent that NULL-able due to an OUTER JOIN. Either the
148678 ** when processing a non-matched row of the left.
148695 ** a column in table number iTable with a copy of the iColumn-th
148712 && pExpr->w.iJoin==pSubst->iTable
148715 pExpr->w.iJoin = pSubst->iNewTable;
148717 if( pExpr->op==TK_COLUMN
148718 && pExpr->iTable==pSubst->iTable
148722 if( pExpr->iColumn<0 ){
148723 pExpr->op = TK_NULL;
148731 iColumn = pExpr->iColumn;
148733 assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr );
148734 assert( pExpr->pRight==0 );
148735 pCopy = pSubst->pEList->a[iColumn].pExpr;
148737 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
148739 sqlite3 *db = pSubst->pParse->db;
148740 if( pSubst->isOuterJoin
148741 && (pCopy->op!=TK_COLUMN || pCopy->iTable!=pSubst->iNewTable)
148746 ifNullRow.iTable = pSubst->iNewTable;
148747 ifNullRow.iColumn = -99;
148753 if( db->mallocFailed ){
148757 if( pSubst->isOuterJoin ){
148760 if( pNew->op==TK_TRUEFALSE ){
148761 pNew->u.iValue = sqlite3ExprTruthValue(pNew);
148762 pNew->op = TK_INTEGER;
148767 ** just as it did when it was a column of a view or sub-query. */
148769 CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pNew);
148770 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse,
148771 pSubst->pCList->a[iColumn].pExpr
148773 if( pNat!=pColl || (pNew->op!=TK_COLUMN && pNew->op!=TK_COLLATE) ){
148774 pNew = sqlite3ExprAddCollateString(pSubst->pParse, pNew,
148775 (pColl ? pColl->zName : "BINARY")
148781 sqlite3SetJoinExpr(pNew, pExpr->w.iJoin,
148782 pExpr->flags & (EP_OuterON|EP_InnerON));
148789 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
148790 pExpr->iTable = pSubst->iNewTable;
148792 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
148793 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
148795 substSelect(pSubst, pExpr->x.pSelect, 1);
148797 substExprList(pSubst, pExpr->x.pList);
148801 Window *pWin = pExpr->y.pWin;
148802 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
148803 substExprList(pSubst, pWin->pPartition);
148804 substExprList(pSubst, pWin->pOrderBy);
148816 for(i=0; i<pList->nExpr; i++){
148817 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
148823 int doPrior /* Do substitutes on p->pPrior too */
148830 substExprList(pSubst, p->pEList);
148831 substExprList(pSubst, p->pGroupBy);
148832 substExprList(pSubst, p->pOrderBy);
148833 p->pHaving = substExpr(pSubst, p->pHaving);
148834 p->pWhere = substExpr(pSubst, p->pWhere);
148835 pSrc = p->pSrc;
148837 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
148838 if( pItem->fg.isSubquery ){
148839 substSelect(pSubst, pItem->u4.pSubq->pSelect, 1);
148841 if( pItem->fg.isTabFunc ){
148842 substExprList(pSubst, pItem->u1.pFuncArg);
148845 }while( doPrior && (p = p->pPrior)!=0 );
148855 ** pSrcItem->colUsed mask.
148859 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
148860 pItem = pWalker->u.pSrcItem;
148861 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
148862 if( pExpr->iColumn<0 ) return WRC_Continue;
148863 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
148871 if( NEVER(pSrcItem->pSTab==0) ) return;
148876 pSrcItem->colUsed = 0;
148892 ** If pSrc contains any sub-selects, call this routine recursively
148893 ** on the FROM clause of each such sub-select, with iExcept set to -1.
148903 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
148906 assert( pItem->iCursor < aCsrMap[0] );
148907 if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){
148908 aCsrMap[pItem->iCursor+1] = pParse->nTab++;
148910 pItem->iCursor = aCsrMap[pItem->iCursor+1];
148911 if( pItem->fg.isSubquery ){
148912 for(p=pItem->u4.pSubq->pSelect; p; p=p->pPrior){
148913 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
148924 int *aCsrMap = pWalker->u.aiCol;
148936 int op = pExpr->op;
148938 renumberCursorDoMapping(pWalker, &pExpr->iTable);
148941 renumberCursorDoMapping(pWalker, &pExpr->w.iJoin);
148949 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
148971 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
148986 while( pSel->pPrior ){
148987 pSel = pSel->pPrior;
148989 return pSel->pEList;
148993 ** Return true if any of the result-set columns in the compound query
149000 assert( p->pEList!=0 );
149001 assert( p->pPrior!=0 );
149002 pList = p->pEList;
149003 for(ii=0; ii<pList->nExpr; ii++){
149006 assert( pList->a[ii].pExpr!=0 );
149007 aff = sqlite3ExprAffinity(pList->a[ii].pExpr);
149008 for(pSub1=p->pPrior; pSub1; pSub1=pSub1->pPrior){
149009 assert( pSub1->pEList!=0 );
149010 assert( pSub1->pEList->nExpr>ii );
149011 assert( pSub1->pEList->a[ii].pExpr!=0 );
149012 if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){
149056 ** other than the one FROM-clause subquery that is a candidate
149058 ** from 2015-02-09.)
149072 ** sub-queries that were excluded from this optimization. Restriction
149087 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
149088 ** accidentally carried the comment forward until 2014-09-15. Original
149125 ** The parent and sub-query may contain WHERE clauses. Subject to
149132 ** Also, each component of the sub-query must return the same number
149135 ** such (illegal) sub-query is flattened. The caller will detect the
149138 ** (18) If the sub-query is a compound select, then all terms of the
149145 ** (20) If the sub-query is a compound select, then it must not use
149156 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
149162 ** The subquery may not be an aggregate that uses the built-in min() or
149185 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
149188 ** If flattening is not attempted, this routine is a no-op and returns 0.
149197 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
149200 const char *zSavedAuthContext = pParse->zAuthContext;
149203 Select *pSub1; /* Pointer to the rightmost select in sub-query */
149207 int iNewParent = -1;/* Replacement table for iParent */
149212 sqlite3 *db = pParse->db;
149219 assert( p->pPrior==0 );
149221 pSrc = p->pSrc;
149222 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
149223 pSubitem = &pSrc->a[iFrom];
149224 iParent = pSubitem->iCursor;
149225 assert( pSubitem->fg.isSubquery );
149226 pSub = pSubitem->u4.pSubq->pSelect;
149230 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
149233 pSubSrc = pSub->pSrc;
149237 ** because they could be computed at compile-time. But when LIMIT and OFFSET
149240 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
149241 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
149242 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
149245 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
149246 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
149247 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
149250 if( p->pOrderBy && pSub->pOrderBy ){
149253 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
149254 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
149255 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
149258 if( pSub->selFlags & (SF_Recursive) ){
149277 if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){
149278 if( pSubSrc->nSrc>1 /* (3a) */
149279 /**** || IsVirtual(pSubSrc->a[0].pSTab) (3b)-omitted */
149280 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
149281 || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */
149288 assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */
149289 if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
149294 assert( !pSubitem->fg.isCte || pSubitem->u2.pCteUse->eM10d!=M10d_Yes );
149296 /* Restriction (17): If the sub-query is a compound SELECT, then it must
149301 if( pSub->pPrior ){
149303 if( pSub->pOrderBy ){
149306 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){
149309 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
149310 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
149311 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
149312 assert( pSub->pSrc!=0 );
149313 assert( (pSub->selFlags & SF_Recursive)==0 );
149314 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
149315 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
149316 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
149317 || pSub1->pSrc->nSrc<1 /* (17c) */
149319 || pSub1->pWin /* (17e) */
149324 if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
149326 ** omitted on left-hand tables of the right join that is being
149330 testcase( pSub1->pSrc->nSrc>1 );
149334 if( p->pOrderBy ){
149335 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
149336 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
149341 if( (p->selFlags & SF_Recursive) ) return 0;
149346 if( pSrc->nSrc>1 ){
149347 if( pParse->nSelect>500 ) return 0;
149349 aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int));
149350 if( aCsrMap ) aCsrMap[0] = pParse->nTab;
149356 pSub->selId, pSub, iFrom));
149359 pParse->zAuthContext = pSubitem->zName;
149362 pParse->zAuthContext = zSavedAuthContext;
149366 if( ALWAYS(pSubitem->fg.isSubquery) ){
149371 assert( pSubitem->fg.isSubquery==0 );
149372 assert( pSubitem->fg.fixedSchema==0 );
149373 sqlite3DbFree(db, pSubitem->zName);
149374 sqlite3DbFree(db, pSubitem->zAlias);
149375 pSubitem->zName = 0;
149376 pSubitem->zAlias = 0;
149377 assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 );
149379 /* If the sub-query is a compound SELECT statement, then (by restrictions
149383 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
149386 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
149387 ** OFFSET clauses and joins them to the left-hand-side of the original
149389 ** select statements in the compound sub-query.
149410 ** We call this the "compound-subquery flattening".
149412 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
149414 ExprList *pOrderBy = p->pOrderBy;
149415 Expr *pLimit = p->pLimit;
149416 Select *pPrior = p->pPrior;
149417 Table *pItemTab = pSubitem->pSTab;
149418 pSubitem->pSTab = 0;
149419 p->pOrderBy = 0;
149420 p->pPrior = 0;
149421 p->pLimit = 0;
149423 p->pLimit = pLimit;
149424 p->pOrderBy = pOrderBy;
149425 p->op = TK_ALL;
149426 pSubitem->pSTab = pItemTab;
149428 p->pPrior = pPrior;
149430 pNew->selId = ++pParse->nSelect;
149431 if( aCsrMap && ALWAYS(db->mallocFailed==0) ){
149434 pNew->pPrior = pPrior;
149435 if( pPrior ) pPrior->pNext = pNew;
149436 pNew->pNext = p;
149437 p->pPrior = pNew;
149438 TREETRACE(0x4,pParse,p,("compound-subquery flattener"
149439 " creates %u as peer\n",pNew->selId));
149441 assert( pSubitem->fg.isSubquery==0 );
149444 if( db->mallocFailed ){
149445 assert( pSubitem->fg.fixedSchema==0 );
149446 assert( pSubitem->fg.isSubquery==0 );
149447 assert( pSubitem->u4.zDatabase==0 );
149457 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
149459 if( ALWAYS(pSubitem->pSTab!=0) ){
149460 Table *pTabToDel = pSubitem->pSTab;
149461 if( pTabToDel->nTabRef==1 ){
149464 testcase( pToplevel->earlyCleanup );
149466 pTabToDel->nTabRef--;
149468 pSubitem->pSTab = 0;
149471 /* The following loop runs once for each term in a compound-subquery
149473 ** of flattening - a flattening other than a compound-subquery flattening -
149485 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
149488 u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ;
149490 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
149491 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
149492 pSrc = pParent->pSrc; /* FROM clause of the outer query */
149495 jointype = pSubitem->fg.jointype; /* First time through the loop */
149514 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
149516 pParent->pSrc = pSrc;
149522 iNewParent = pSubSrc->a[0].iCursor;
149524 SrcItem *pItem = &pSrc->a[i+iFrom];
149525 assert( pItem->fg.isTabFunc==0 );
149526 assert( pItem->fg.isSubquery
149527 || pItem->fg.fixedSchema
149528 || pItem->u4.zDatabase==0 );
149529 if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing);
149530 *pItem = pSubSrc->a[i];
149531 pItem->fg.jointype |= ltorj;
149532 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
149534 pSrc->a[iFrom].fg.jointype &= JT_LTORJ;
149535 pSrc->a[iFrom].fg.jointype |= jointype | ltorj;
149549 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
149550 /* At this point, any non-zero iOrderByCol values indicate that the
149557 ** function attempts to flatten a compound sub-query into pParent
149558 ** (the only way this can happen is if the compound sub-query is
149559 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
149560 ExprList *pOrderBy = pSub->pOrderBy;
149561 for(i=0; i<pOrderBy->nExpr; i++){
149562 pOrderBy->a[i].u.x.iOrderByCol = 0;
149564 assert( pParent->pOrderBy==0 );
149565 pParent->pOrderBy = pOrderBy;
149566 pSub->pOrderBy = 0;
149568 pWhere = pSub->pWhere;
149569 pSub->pWhere = 0;
149571 assert( pSubSrc->nSrc==1 );
149575 if( pParent->pWhere ){
149576 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
149578 pParent->pWhere = pWhere;
149581 if( db->mallocFailed==0 ){
149587 x.pEList = pSub->pEList;
149594 pParent->selFlags |= pSub->selFlags & SF_Compound;
149595 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
149603 if( pSub->pLimit ){
149604 pParent->pLimit = pSub->pLimit;
149605 pSub->pLimit = 0;
149611 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
149639 u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */
149663 assert( pColumn->op==TK_COLUMN );
149664 assert( sqlite3ExprIsConstant(pConst->pParse, pValue) );
149668 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
149672 /* 2018-10-25 ticket [cf5ed20f]
149674 for(i=0; i<pConst->nConst; i++){
149675 const Expr *pE2 = pConst->apExpr[i*2];
149676 assert( pE2->op==TK_COLUMN );
149677 if( pE2->iTable==pColumn->iTable
149678 && pE2->iColumn==pColumn->iColumn
149685 pConst->bHasAffBlob = 1;
149688 pConst->nConst++;
149689 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
149690 pConst->nConst*2*sizeof(Expr*));
149691 if( pConst->apExpr==0 ){
149692 pConst->nConst = 0;
149694 pConst->apExpr[pConst->nConst*2-2] = pColumn;
149695 pConst->apExpr[pConst->nConst*2-1] = pValue;
149702 ** is part of the AND-connected terms of the expression. For each term
149708 if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){
149713 if( pExpr->op==TK_AND ){
149714 findConstInWhere(pConst, pExpr->pRight);
149715 findConstInWhere(pConst, pExpr->pLeft);
149718 if( pExpr->op!=TK_EQ ) return;
149719 pRight = pExpr->pRight;
149720 pLeft = pExpr->pLeft;
149723 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pLeft) ){
149726 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pConst->pParse, pRight) ){
149735 ** pExpr is equivalent to one of the columns named in pWalker->u.pConst,
149737 ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr
149746 if( pConst->pOomFault[0] ) return WRC_Prune;
149747 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
149748 if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){
149754 for(i=0; i<pConst->nConst; i++){
149755 Expr *pColumn = pConst->apExpr[i*2];
149757 if( pColumn->iTable!=pExpr->iTable ) continue;
149758 if( pColumn->iColumn!=pExpr->iColumn ) continue;
149764 pConst->nChng++;
149767 assert( pExpr->pLeft==0 );
149768 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
149769 if( pConst->pParse->db->mallocFailed ) return WRC_Prune;
149778 ** any substitutions based on the contents of pWalker->u.pConst should
149784 ** one of the columns in pWalker->u.pConst, or
149789 ** pWalker->u.pConst.
149792 WhereConst *pConst = pWalker->u.pConst;
149797 if( pConst->bHasAffBlob ){
149798 if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE)
149799 || pExpr->op==TK_IS
149801 propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0);
149802 if( pConst->pOomFault[0] ) return WRC_Prune;
149803 if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){
149804 propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0);
149808 return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob);
149812 ** The WHERE-clause constant propagation optimization.
149815 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
149847 ** 2021-05-25 forum post 6a06202608: Another troublesome case is...
149870 x.pOomFault = &pParse->db->mallocFailed;
149876 if( ALWAYS(p->pSrc!=0)
149877 && p->pSrc->nSrc>0
149878 && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0
149887 findConstInWhere(&x, p->pWhere);
149896 sqlite3WalkExpr(&w, p->pWhere);
149897 sqlite3DbFree(x.pParse->db, x.apExpr);
149908 ** push WHERE clause expression pExpr down to FROM clause sub-query
149915 ** BY clause of all window function used by the sub-query. It is safe
149921 ** * the sub-query uses only one distinct window frame, and
149922 ** * that the window frame has a PARTITION BY clause.
149925 assert( pSubq->pWin->pPartition );
149926 assert( (pSubq->selFlags & SF_MultiPart)==0 );
149927 assert( pSubq->pPrior==0 );
149928 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
149938 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
149942 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
149950 ** This optimization is called the "WHERE-clause push-down optimization"
149951 ** or sometimes the "predicate push-down optimization".
149954 ** with a similar name: The "MySQL push-down optimization" causes WHERE
149963 ** (1) (** This restriction was removed on 2017-09-29. We used to
149981 ** of a LEFT JOIN where iCursor is not the right-hand table of that
150000 ** (6b) The inner query is a compound and uses window-functions.
150004 ** all window-functions used by the sub-query. It is safe to
150006 ** window over which any window-function is calculated.
150026 ** Without this restriction, the WHERE-clause push-down optimization
150031 ** (10) The inner query is not the right-hand table of a RIGHT JOIN.
150039 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
150052 pSrc = &pSrcList->a[iSrc];
150054 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ){
150057 if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ){
150061 if( pSubq->pPrior ){
150064 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
150065 u8 op = pSel->op;
150072 if( pSel->pWin ) return 0; /* restriction (6b) */
150078 ** non-BINARY collating sequence. */
150079 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
150081 const ExprList *pList = pSel->pEList;
150083 for(ii=0; ii<pList->nExpr; ii++){
150084 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[ii].pExpr);
150093 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
150104 for(pX=pSubq; pX; pX=pX->pPrior){
150105 assert( (pX->selFlags & (SF_Recursive))==0 );
150110 if( pSubq->pLimit!=0 ){
150113 while( pWhere->op==TK_AND ){
150114 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrcList, iSrc);
150115 pWhere = pWhere->pLeft;
150120 && (pSrcList->a[0].fg.jointype & JT_LTORJ)!=0 /* Fast pre-test of (9c) */
150124 if( pWhere->w.iJoin==pSrcList->a[jj].iCursor ){
150129 if( (pSrcList->a[jj].fg.jointype & JT_RIGHT)!=0 ){
150138 || pWhere->w.iJoin!=iCursor)
150143 && pWhere->w.iJoin!=iCursor
150150 if( ViewCanHaveRowid && (pWhere->op==TK_ISNULL || pWhere->op==TK_NOTNULL) ){
150151 Expr *pLeft = pWhere->pLeft;
150153 && pLeft->op==TK_COLUMN
150154 && pLeft->iColumn < 0
150163 pSubq->selFlags |= SF_PushDown;
150166 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
150167 unsetJoinExpr(pNew, -1, 1);
150169 x.iTable = pSrc->iCursor;
150170 x.iNewTable = pSrc->iCursor;
150172 x.pEList = pSubq->pEList;
150176 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
150177 /* Restriction 6c has prevented push-down in this case */
150178 sqlite3ExprDelete(pParse->db, pNew);
150179 nChng--;
150183 if( pSubq->selFlags & SF_Aggregate ){
150184 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
150186 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
150188 pSubq = pSubq->pPrior;
150196 ** Check to see if a subquery contains result-set columns that are
150197 ** never used. If it does, change the value of those result-set columns
150212 if( pItem->fg.isCorrelated || pItem->fg.isCte ){
150215 assert( pItem->pSTab!=0 );
150216 pTab = pItem->pSTab;
150217 assert( pItem->fg.isSubquery );
150218 pSub = pItem->u4.pSubq->pSelect;
150219 assert( pSub->pEList->nExpr==pTab->nCol );
150220 for(pX=pSub; pX; pX=pX->pPrior){
150221 if( (pX->selFlags & (SF_Distinct|SF_Aggregate))!=0 ){
150222 testcase( pX->selFlags & SF_Distinct );
150223 testcase( pX->selFlags & SF_Aggregate );
150226 if( pX->pPrior && pX->op!=TK_ALL ){
150232 if( pX->pWin ){
150239 colUsed = pItem->colUsed;
150240 if( pSub->pOrderBy ){
150241 ExprList *pList = pSub->pOrderBy;
150242 for(j=0; j<pList->nExpr; j++){
150243 u16 iCol = pList->a[j].u.x.iOrderByCol;
150245 iCol--;
150246 colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
150250 nCol = pTab->nCol;
150252 Bitmask m = j<BMS-1 ? MASKBIT(j) : TOPBIT;
150254 for(pX=pSub; pX; pX=pX->pPrior) {
150255 Expr *pY = pX->pEList->a[j].pExpr;
150256 if( pY->op==TK_NULL ) continue;
150257 pY->op = TK_NULL;
150259 pX->selFlags |= SF_PushDown;
150291 assert( pFunc->op==TK_AGG_FUNCTION );
150294 pEList = pFunc->x.pList;
150296 || pEList->nExpr!=1
150303 zFunc = pFunc->u.zToken;
150306 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
150316 assert( pOrderBy!=0 || db->mallocFailed );
150317 if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags;
150323 ** The second argument is the associated aggregate-info object. This
150328 ** where table is a database table, not a sub-select or view. If the query
150342 assert( !p->pGroupBy );
150344 if( p->pWhere
150345 || p->pEList->nExpr!=1
150346 || p->pSrc->nSrc!=1
150347 || p->pSrc->a[0].fg.isSubquery
150348 || pAggInfo->nFunc!=1
150349 || p->pHaving
150353 pTab = p->pSrc->a[0].pSTab;
150357 pExpr = p->pEList->a[0].pExpr;
150359 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
150360 if( pExpr->pAggInfo!=pAggInfo ) return 0;
150361 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
150362 assert( pAggInfo->aFunc[0].pFExpr==pExpr );
150371 ** If the source-list item passed as an argument was augmented with an
150375 ** pFrom->pIndex and return SQLITE_OK.
150378 Table *pTab = pFrom->pSTab;
150379 char *zIndexedBy = pFrom->u1.zIndexedBy;
150382 assert( pFrom->fg.isIndexedBy!=0 );
150384 for(pIdx=pTab->pIndex;
150385 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
150386 pIdx=pIdx->pNext
150390 pParse->checkSchema = 1;
150393 assert( pFrom->fg.isCte==0 );
150394 pFrom->u2.pIBIndex = pIdx;
150429 if( p->pPrior==0 ) return WRC_Continue;
150430 if( p->pOrderBy==0 ) return WRC_Continue;
150431 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
150433 a = p->pOrderBy->a;
150435 /* If iOrderByCol is already non-zero, then it has already been matched
150437 ** SELECT is rewritten for window-functions processing and then passed
150442 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
150443 if( a[i].pExpr->flags & EP_Collate ) break;
150449 pParse = pWalker->pParse;
150450 db = pParse->db;
150455 assert( pNewSrc!=0 || pParse->nErr );
150456 if( pParse->nErr ){
150461 p->pSrc = pNewSrc;
150462 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
150463 p->op = TK_SELECT;
150464 p->pWhere = 0;
150465 pNew->pGroupBy = 0;
150466 pNew->pHaving = 0;
150467 pNew->pOrderBy = 0;
150468 p->pPrior = 0;
150469 p->pNext = 0;
150470 p->pWith = 0;
150472 p->pWinDefn = 0;
150474 p->selFlags &= ~(u32)SF_Compound;
150475 assert( (p->selFlags & SF_Converted)==0 );
150476 p->selFlags |= SF_Converted;
150477 assert( pNew->pPrior!=0 );
150478 pNew->pPrior->pNext = pNew;
150479 pNew->pLimit = 0;
150484 ** Check to see if the FROM clause term pFrom has table-valued function
150486 ** non-zero, since pFrom is not allowed to be a table-valued function.
150489 if( pFrom->fg.isTabFunc ){
150490 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
150500 ** FROM clause element pItem is really a common-table-expression (CTE)
150504 ** If a non-NULL value is returned, set *ppContext to point to the With
150512 const char *zName = pItem->zName;
150514 assert( pItem->fg.fixedSchema || pItem->u4.zDatabase==0 );
150516 for(p=pWith; p; p=p->pOuter){
150518 for(i=0; i<p->nCte; i++){
150519 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
150521 return &p->a[i];
150524 if( p->bView ) break;
150530 ** with the inner-most WITH clause being at the top of the stack.
150553 if( pParse->nErr==0 ){
150554 assert( pParse->pWith!=pWith );
150555 pWith->pOuter = pParse->pWith;
150556 pParse->pWith = pWith;
150565 ** pParse->pWith linked list). And if currently processing a CTE
150569 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
150584 assert( pFrom->pSTab==0 );
150585 if( pParse->pWith==0 ){
150589 if( pParse->nErr ){
150590 /* Prior errors might have left pParse->pWith in a goofy state, so
150594 assert( pFrom->fg.hadSchema==0 || pFrom->fg.notCte!=0 );
150595 if( pFrom->fg.fixedSchema==0 && pFrom->u4.zDatabase!=0 ){
150600 if( pFrom->fg.notCte ){
150608 pCte = searchWith(pParse->pWith, pFrom, &pWith);
150610 sqlite3 *db = pParse->db;
150614 Select *pLeft; /* Left-most SELECT statement */
150615 Select *pRecTerm; /* Left-most recursive term */
150617 With *pSavedWith; /* Initial value of pParse->pWith */
150618 int iRecTab = -1; /* Cursor for recursive table */
150621 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
150623 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
150625 if( pCte->zCteErr ){
150626 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
150631 assert( pFrom->pSTab==0 );
150634 pCteUse = pCte->pUse;
150636 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
150643 pCteUse->eM10d = pCte->eM10d;
150645 pFrom->pSTab = pTab;
150646 pTab->nTabRef = 1;
150647 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
150648 pTab->iPKey = -1;
150649 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
150650 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
150651 sqlite3SrcItemAttachSubquery(pParse, pFrom, pCte->pSelect, 1);
150652 if( db->mallocFailed ) return 2;
150653 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq );
150654 pSel = pFrom->u4.pSubq->pSelect;
150656 pSel->selFlags |= SF_CopyCte;
150657 if( pFrom->fg.isIndexedBy ){
150658 sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy);
150661 assert( !pFrom->fg.isIndexedBy );
150662 pFrom->fg.isCte = 1;
150663 pFrom->u2.pCteUse = pCteUse;
150664 pCteUse->nUse++;
150668 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
150669 while( bMayRecursive && pRecTerm->op==pSel->op ){
150671 SrcList *pSrc = pRecTerm->pSrc;
150672 assert( pRecTerm->pPrior!=0 );
150673 for(i=0; i<pSrc->nSrc; i++){
150674 SrcItem *pItem = &pSrc->a[i];
150675 if( pItem->zName!=0
150676 && !pItem->fg.hadSchema
150677 && ALWAYS( !pItem->fg.isSubquery )
150678 && (pItem->fg.fixedSchema || pItem->u4.zDatabase==0)
150679 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
150681 pItem->pSTab = pTab;
150682 pTab->nTabRef++;
150683 pItem->fg.isRecursive = 1;
150684 if( pRecTerm->selFlags & SF_Recursive ){
150686 "multiple references to recursive table: %s", pCte->zName
150690 pRecTerm->selFlags |= SF_Recursive;
150691 if( iRecTab<0 ) iRecTab = pParse->nTab++;
150692 pItem->iCursor = iRecTab;
150695 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
150696 pRecTerm = pRecTerm->pPrior;
150699 pCte->zCteErr = "circular reference: %s";
150700 pSavedWith = pParse->pWith;
150701 pParse->pWith = pWith;
150702 if( pSel->selFlags & SF_Recursive ){
150705 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
150706 assert( pRecTerm->pNext!=0 );
150707 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
150708 assert( pRecTerm->pWith==0 );
150709 pRecTerm->pWith = pSel->pWith;
150711 pRecTerm->pWith = 0;
150713 pParse->pWith = pSavedWith;
150718 pParse->pWith = pSavedWith;
150722 pParse->pWith = pWith;
150724 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
150725 pEList = pLeft->pEList;
150726 if( pCte->pCols ){
150727 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
150729 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
150731 pParse->pWith = pSavedWith;
150734 pEList = pCte->pCols;
150737 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
150739 if( pSel->selFlags & SF_Recursive ){
150740 pCte->zCteErr = "multiple recursive references: %s";
150742 pCte->zCteErr = "recursive reference in a subquery: %s";
150746 pCte->zCteErr = 0;
150747 pParse->pWith = pSavedWith;
150764 Parse *pParse = pWalker->pParse;
150765 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
150766 With *pWith = findRightmost(p)->pWith;
150768 assert( pParse->pWith==pWith || pParse->nErr );
150769 pParse->pWith = pWith->pOuter;
150777 ** sub-query in the FROM clause of a SELECT statement. This function
150786 assert( pFrom->fg.isSubquery );
150787 assert( pFrom->u4.pSubq!=0 );
150788 pSel = pFrom->u4.pSubq->pSelect;
150790 pFrom->pSTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
150792 pTab->nTabRef = 1;
150793 if( pFrom->zAlias ){
150794 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
150796 pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom);
150798 while( pSel->pPrior ){ pSel = pSel->pPrior; }
150799 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
150800 pTab->iPKey = -1;
150801 pTab->eTabType = TABTYP_VIEW;
150802 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
150804 /* The usual case - do not allow ROWID on a subquery */
150805 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
150808 pTab->tabFlags |= TF_Ephemeral | sqlite3Config.mNoVisibleRowid;
150810 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
150829 N--;
150831 if( pBase->fg.isUsing==0 ) continue;
150832 if( NEVER(pBase->u3.pUsing==0) ) continue;
150833 if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1;
150846 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
150848 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
150864 Parse *pParse = pWalker->pParse;
150869 sqlite3 *db = pParse->db;
150871 u16 selFlags = p->selFlags;
150874 p->selFlags |= SF_Expanded;
150875 if( db->mallocFailed ){
150878 assert( p->pSrc!=0 );
150882 if( pWalker->eCode ){
150884 p->selId = ++pParse->nSelect;
150886 pTabList = p->pSrc;
150887 pEList = p->pEList;
150888 if( pParse->pWith && (p->selFlags & SF_View) ){
150889 if( p->pWith==0 ){
150890 p->pWith = (With*)sqlite3DbMallocZero(db, SZ_WITH(1) );
150891 if( p->pWith==0 ){
150895 p->pWith->bView = 1;
150897 sqlite3WithPush(pParse, p->pWith, 0);
150908 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
150910 assert( pFrom->fg.isRecursive==0 || pFrom->pSTab!=0 );
150911 if( pFrom->pSTab ) continue;
150912 assert( pFrom->fg.isRecursive==0 );
150913 if( pFrom->zName==0 ){
150916 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq!=0 );
150917 pSel = pFrom->u4.pSubq->pSelect;
150918 /* A sub-query in the FROM clause of a SELECT */
150920 assert( pFrom->pSTab==0 );
150927 pTab = pFrom->pSTab;
150932 assert( pFrom->pSTab==0 );
150933 pFrom->pSTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
150935 if( pTab->nTabRef>=0xffff ){
150937 pTab->zName);
150938 pFrom->pSTab = 0;
150941 pTab->nTabRef++;
150948 u8 eCodeOrig = pWalker->eCode;
150950 assert( pFrom->fg.isSubquery==0 );
150952 if( (db->flags & SQLITE_EnableView)==0
150953 && pTab->pSchema!=db->aDb[1].pSchema
150956 pTab->zName);
150958 sqlite3SrcItemAttachSubquery(pParse, pFrom, pTab->u.view.pSelect, 1);
150962 && pFrom->fg.fromDDL
150963 && ALWAYS(pTab->u.vtab.p!=0)
150964 && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
150967 pTab->zName);
150971 nCol = pTab->nCol;
150972 pTab->nCol = -1;
150973 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
150974 if( pFrom->fg.isSubquery ){
150975 sqlite3WalkSelect(pWalker, pFrom->u4.pSubq->pSelect);
150977 pWalker->eCode = eCodeOrig;
150978 pTab->nCol = nCol;
150984 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
150991 assert( db->mallocFailed==0 || pParse->nErr!=0 );
150992 if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){
151007 for(k=0; k<pEList->nExpr; k++){
151008 pE = pEList->a[k].pExpr;
151009 if( pE->op==TK_ASTERISK ) break;
151010 assert( pE->op!=TK_DOT || pE->pRight!=0 );
151011 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
151012 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
151013 elistFlags |= pE->flags;
151015 if( k<pEList->nExpr ){
151021 struct ExprList_item *a = pEList->a;
151023 int flags = pParse->db->flags;
151027 for(k=0; k<pEList->nExpr; k++){
151029 elistFlags |= pE->flags;
151030 pRight = pE->pRight;
151031 assert( pE->op!=TK_DOT || pRight!=0 );
151032 if( pE->op!=TK_ASTERISK
151033 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
151039 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
151040 pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName;
151050 if( pE->op==TK_DOT ){
151052 assert( pE->pLeft!=0 );
151053 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
151054 zTName = pE->pLeft->u.zToken;
151055 assert( ExprUseWOfst(pE->pLeft) );
151056 iErrOfst = pE->pRight->w.iOfst;
151059 iErrOfst = pE->w.iOfst;
151061 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
151063 Table *pTab = pFrom->pSTab; /* Table for this data source */
151064 ExprList *pNestedFrom; /* Result-set of a nested FROM clause */
151070 if( (zTabName = pFrom->zAlias)==0 ){
151071 zTabName = pTab->zName;
151073 if( db->mallocFailed ) break;
151074 assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom) );
151075 if( pFrom->fg.isNestedFrom ){
151076 assert( pFrom->fg.isSubquery && pFrom->u4.pSubq );
151077 assert( pFrom->u4.pSubq->pSelect!=0 );
151078 pNestedFrom = pFrom->u4.pSubq->pSelect->pEList;
151080 assert( pNestedFrom->nExpr==pTab->nCol );
151087 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
151088 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
151090 if( i+1<pTabList->nSrc
151096 for(ii=0; ii<pUsing->nId; ii++){
151097 const char *zUName = pUsing->a[ii].zName;
151102 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
151103 assert( pX->zEName==0 );
151104 pX->zEName = sqlite3MPrintf(db,"..%s", zUName);
151105 pX->fg.eEName = ENAME_TAB;
151106 pX->fg.bUsingTerm = 1;
151113 nAdd = pTab->nCol;
151119 if( j==pTab->nCol ){
151123 zName = pTab->aCol[j].zCnName;
151125 /* If pTab is actually an SF_NestedFrom sub-select, do not
151127 if( pNestedFrom && pNestedFrom->a[j].fg.eEName==ENAME_ROWID ){
151133 && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0, 0)==0
151139 ** result-set list unless the SELECT has the SF_IncludeHidden
151142 if( (p->selFlags & SF_IncludeHidden)==0
151143 && IsHiddenColumn(&pTab->aCol[j])
151147 if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0
151158 if( pFrom->fg.isUsing
151159 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0
151167 if( (pTabList->nSrc>1
151168 && ( (pFrom->fg.jointype & JT_LTORJ)==0
151170 || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1)
151178 if( IN_RENAME_OBJECT && pE->pLeft ){
151179 sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft);
151193 pX = &pNew->a[pNew->nExpr-1];
151194 assert( pX->zEName==0 );
151196 if( pNestedFrom && (!ViewCanHaveRowid || j<pNestedFrom->nExpr) ){
151197 assert( j<pNestedFrom->nExpr );
151198 pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName);
151199 testcase( pX->zEName==0 );
151201 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
151203 testcase( pX->zEName==0 );
151205 pX->fg.eEName = (j==pTab->nCol ? ENAME_ROWID : ENAME_TAB);
151206 if( (pFrom->fg.isUsing
151207 && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0)
151209 || (j<pTab->nCol && (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND))
151211 pX->fg.bNoExpand = 1;
151214 pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
151215 pX->fg.eEName = ENAME_NAME;
151217 pX->zEName = sqlite3DbStrDup(db, zName);
151218 pX->fg.eEName = ENAME_NAME;
151232 p->pEList = pNew;
151234 if( p->pEList ){
151235 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
151240 p->selFlags |= SF_ComplexResult;
151245 TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n"));
151272 ** The calling function can detect the problem by looking at pParse->nErr
151273 ** and/or pParse->db->mallocFailed.
151279 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
151296 ** For each FROM-clause subquery, add Column.zType, Column.zColl, and
151311 if( p->selFlags & SF_HasTypeInfo ) return;
151312 p->selFlags |= SF_HasTypeInfo;
151313 pParse = pWalker->pParse;
151314 assert( (p->selFlags & SF_Resolved) );
151315 pTabList = p->pSrc;
151316 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
151317 Table *pTab = pFrom->pSTab;
151319 if( (pTab->tabFlags & TF_Ephemeral)!=0 && pFrom->fg.isSubquery ){
151320 /* A sub-query in the FROM clause of a SELECT */
151321 Select *pSel = pFrom->u4.pSubq->pSelect;
151331 ** the Table structures of all FROM-clause subqueries in a
151352 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
151353 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
151365 assert( p!=0 || pParse->db->mallocFailed );
151366 assert( pParse->db->pParse==pParse );
151367 if( pParse->db->mallocFailed ) return;
151368 if( p->selFlags & SF_HasTypeInfo ) return;
151370 if( pParse->nErr ) return;
151372 if( pParse->nErr ) return;
151383 pAggInfo->selId, pAggInfo);
151384 for(ii=0; ii<pAggInfo->nColumn; ii++){
151385 struct AggInfo_col *pCol = &pAggInfo->aCol[ii];
151387 "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d"
151389 ii, pCol->pTab ? pCol->pTab->zName : "NULL",
151390 pCol->iTable, pCol->iColumn, pAggInfo->iFirstReg+ii,
151391 pCol->iSorterColumn,
151392 ii>=pAggInfo->nAccumulator ? "" : " Accumulator");
151393 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
151395 for(ii=0; ii<pAggInfo->nFunc; ii++){
151396 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
151397 ii, pAggInfo->iFirstReg+pAggInfo->nColumn+ii);
151398 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
151404 ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[]
151426 assert( pAggInfo->iFirstReg==0 );
151427 pNC->ncFlags |= NC_InAggFunc;
151428 for(i=0; i<pAggInfo->nFunc; i++){
151429 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
151430 assert( pExpr->op==TK_FUNCTION || pExpr->op==TK_AGG_FUNCTION );
151432 sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList);
151433 if( pExpr->pLeft ){
151434 assert( pExpr->pLeft->op==TK_ORDER );
151435 assert( ExprUseXList(pExpr->pLeft) );
151436 sqlite3ExprAnalyzeAggList(pNC, pExpr->pLeft->x.pList);
151441 sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter);
151445 pNC->ncFlags &= ~NC_InAggFunc;
151459 NameContext *pNC /* Name context used to resolve agg-func args */
151461 assert( pAggInfo->iFirstReg==0 );
151463 assert( pSelect->pGroupBy!=0 );
151464 pAggInfo->nColumn = pAggInfo->nAccumulator;
151465 if( ALWAYS(pAggInfo->nSortingColumn>0) ){
151466 int mx = pSelect->pGroupBy->nExpr - 1;
151468 for(j=0; j<pAggInfo->nColumn; j++){
151469 k = pAggInfo->aCol[j].iSorterColumn;
151472 pAggInfo->nSortingColumn = mx+1;
151481 for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){
151482 printf("data-cursor=%d index={%d,%d}\n",
151483 pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol);
151484 sqlite3TreeViewExpr(0, pIEpr->pExpr, 0);
151501 if( pExpr->pAggInfo==0 ) return WRC_Continue;
151502 if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue;
151503 if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue;
151504 if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue;
151505 pAggInfo = pExpr->pAggInfo;
151506 if( NEVER(pExpr->iAgg>=pAggInfo->nColumn) ) return WRC_Continue;
151507 assert( pExpr->iAgg>=0 );
151508 pCol = &pAggInfo->aCol[pExpr->iAgg];
151509 pExpr->op = TK_AGG_COLUMN;
151510 pExpr->iTable = pCol->iTable;
151511 pExpr->iColumn = pCol->iColumn;
151517 ** Convert every pAggInfo->aFunc[].pExpr such that any node within
151526 for(i=0; i<pAggInfo->nFunc; i++){
151527 sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr);
151534 ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first
151535 ** register in this block is stored in pAggInfo->iFirstReg.
151551 assert( pAggInfo->iFirstReg==0 );
151552 pAggInfo->iFirstReg = pParse->nMem + 1;
151553 pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc;
151565 Vdbe *v = pParse->pVdbe;
151568 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
151569 assert( pAggInfo->iFirstReg>0 );
151570 assert( pParse->db->pParse==pParse );
151571 assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 );
151573 if( pParse->nErr ) return;
151574 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg,
151575 pAggInfo->iFirstReg+nReg-1);
151576 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
151577 if( pFunc->iDistinct>=0 ){
151578 Expr *pE = pFunc->pFExpr;
151580 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
151583 pFunc->iDistinct = -1;
151585 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
151586 pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
151587 pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO);
151588 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)",
151589 pFunc->pFunc->zName));
151592 if( pFunc->iOBTab>=0 ){
151596 assert( pFunc->pFExpr->pLeft!=0 );
151597 assert( pFunc->pFExpr->pLeft->op==TK_ORDER );
151598 assert( ExprUseXList(pFunc->pFExpr->pLeft) );
151599 assert( pFunc->pFunc!=0 );
151600 pOBList = pFunc->pFExpr->pLeft->x.pList;
151601 if( !pFunc->bOBUnique ){
151604 if( pFunc->bOBPayload ){
151606 assert( ExprUseXList(pFunc->pFExpr) );
151607 nExtra += pFunc->pFExpr->x.pList->nExpr;
151609 if( pFunc->bUseSubtype ){
151610 nExtra += pFunc->pFExpr->x.pList->nExpr;
151613 if( !pFunc->bOBUnique && pParse->nErr==0 ){
151614 pKeyInfo->nKeyField++;
151617 pFunc->iOBTab, pOBList->nExpr+nExtra, 0,
151619 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(ORDER BY)",
151620 pFunc->pFunc->zName));
151630 Vdbe *v = pParse->pVdbe;
151633 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
151635 assert( ExprUseXList(pF->pFExpr) );
151636 if( pParse->nErr ) return;
151637 pList = pF->pFExpr->x.pList;
151638 if( pF->iOBTab>=0 ){
151640 ** were stored in emphermal table pF->iOBTab. Here, we extract those
151649 assert( pF->pFunc!=0 );
151650 nArg = pList->nExpr;
151653 if( pF->bOBPayload==0 ){
151656 assert( pF->pFExpr->pLeft!=0 );
151657 assert( ExprUseXList(pF->pFExpr->pLeft) );
151658 assert( pF->pFExpr->pLeft->x.pList!=0 );
151659 nKey = pF->pFExpr->pLeft->x.pList->nExpr;
151660 if( ALWAYS(!pF->bOBUnique) ) nKey++;
151662 iTop = sqlite3VdbeAddOp1(v, OP_Rewind, pF->iOBTab); VdbeCoverage(v);
151663 for(j=nArg-1; j>=0; j--){
151664 sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, nKey+j, regAgg+j);
151666 if( pF->bUseSubtype ){
151668 int iBaseCol = nKey + nArg + (pF->bOBPayload==0 && pF->bOBUnique==0);
151669 for(j=nArg-1; j>=0; j--){
151670 sqlite3VdbeAddOp3(v, OP_Column, pF->iOBTab, iBaseCol+j, regSubtype);
151676 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
151678 sqlite3VdbeAddOp2(v, OP_Next, pF->iOBTab, iTop+1); VdbeCoverage(v);
151683 pList ? pList->nExpr : 0);
151684 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
151692 ** If regAcc is non-zero and there are no min() or max() aggregates
151693 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
151710 Vdbe *v = pParse->pVdbe;
151717 assert( pAggInfo->iFirstReg>0 );
151718 if( pParse->nErr ) return;
151719 pAggInfo->directMode = 1;
151720 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
151727 assert( ExprUseXList(pF->pFExpr) );
151728 assert( !IsWindowFunc(pF->pFExpr) );
151729 assert( pF->pFunc!=0 );
151730 pList = pF->pFExpr->x.pList;
151731 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
151732 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
151733 if( pAggInfo->nAccumulator
151734 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
151740 if( regHit==0 ) regHit = ++pParse->nMem;
151753 if( pF->iOBTab>=0 ){
151759 nArg = pList->nExpr;
151761 assert( pF->pFExpr->pLeft!=0 );
151762 assert( pF->pFExpr->pLeft->op==TK_ORDER );
151763 assert( ExprUseXList(pF->pFExpr->pLeft) );
151764 pOBList = pF->pFExpr->pLeft->x.pList;
151766 assert( pOBList->nExpr>0 );
151767 regAggSz = pOBList->nExpr;
151768 if( !pF->bOBUnique ){
151771 if( pF->bOBPayload ){
151774 if( pF->bUseSubtype ){
151781 jj = pOBList->nExpr;
151782 if( !pF->bOBUnique ){
151783 sqlite3VdbeAddOp2(v, OP_Sequence, pF->iOBTab, regAgg+jj);
151786 if( pF->bOBPayload ){
151791 if( pF->bUseSubtype ){
151793 int regBase = pF->bOBPayload ? regDistinct : regAgg;
151799 nArg = pList->nExpr;
151807 if( pF->iDistinct>=0 && pList ){
151811 pF->iDistinct = codeDistinct(pParse, eDistinctType,
151812 pF->iDistinct, addrNext, pList, regDistinct);
151814 if( pF->iOBTab>=0 ){
151816 sqlite3VdbeAddOp3(v, OP_MakeRecord, regAgg, regAggSz-1,
151817 regAgg+regAggSz-1);
151818 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pF->iOBTab, regAgg+regAggSz-1,
151819 regAgg, regAggSz-1);
151823 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
151827 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
151828 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
151829 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
151832 pColl = pParse->db->pDfltColl;
151834 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
151839 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
151846 if( pParse->nErr ) return;
151848 if( regHit==0 && pAggInfo->nAccumulator ){
151854 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
151855 sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i));
151856 if( pParse->nErr ) return;
151859 pAggInfo->directMode = 0;
151875 if( pParse->explain==2 ){
151878 pTab->zName,
151880 bCover ? pIdx->zName : ""
151895 ** sub-expression matches the criteria for being moved to the WHERE
151896 ** clause. If so, add it to the WHERE clause and replace the sub-expression
151900 if( pExpr->op!=TK_AND ){
151901 Select *pS = pWalker->u.pSelect;
151903 ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set
151909 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
151911 && pExpr->pAggInfo==0
151913 sqlite3 *db = pWalker->pParse->db;
151916 Expr *pWhere = pS->pWhere;
151918 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
151919 pS->pWhere = pNew;
151920 pWalker->eCode = 1;
151949 sqlite3WalkExpr(&sWalker, p->pHaving);
151959 ** Check to see if the pThis entry of pTabList is a self-join of another view.
151960 ** Search FROM-clause entries in the range of iFirst..iEnd, including iFirst
151963 ** If pThis is a self-join, then return the SrcItem for the first other
151964 ** instance of that view found. If pThis is not a self-join then return 0.
151967 SrcList *pTabList, /* Search for self-joins in this FROM clause */
151969 int iFirst, int iEnd /* Range of FROM-clause entries to search. */
151973 assert( pThis->fg.isSubquery );
151974 pSel = pThis->u4.pSubq->pSelect;
151976 if( pSel->selFlags & SF_PushDown ) return 0;
151979 pItem = &pTabList->a[iFirst++];
151980 if( !pItem->fg.isSubquery ) continue;
151981 if( pItem->fg.viaCoroutine ) continue;
151982 if( pItem->zName==0 ) continue;
151983 assert( pItem->pSTab!=0 );
151984 assert( pThis->pSTab!=0 );
151985 if( pItem->pSTab->pSchema!=pThis->pSTab->pSchema ) continue;
151986 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
151987 pS1 = pItem->u4.pSubq->pSelect;
151988 if( pItem->pSTab->pSchema==0 && pSel->selId!=pS1->selId ){
151993 if( pS1->selFlags & SF_PushDown ){
152008 sqlite3DbFree(db, p->aCol);
152009 sqlite3DbFree(db, p->aFunc);
152029 ** * None of the subqueries are DISTINCT (forumpost/a860f5fb2e 2025-03-10)
152039 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
152040 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
152041 if( p->pWhere ) return 0;
152042 if( p->pHaving ) return 0;
152043 if( p->pGroupBy ) return 0;
152044 if( p->pOrderBy ) return 0;
152045 pExpr = p->pEList->a[0].pExpr;
152046 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
152048 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
152050 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
152051 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
152053 pFrom = p->pSrc->a;
152054 if( pFrom->fg.isSubquery==0 ) return 0; /* FROM is a subquery */
152055 pSub = pFrom->u4.pSubq->pSelect;
152056 if( pSub->pPrior==0 ) return 0; /* Must be a compound */
152057 if( pSub->selFlags & SF_CopyCte ) return 0; /* Not a CTE */
152059 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
152060 if( pSub->pWhere ) return 0; /* No WHERE clause */
152061 if( pSub->pLimit ) return 0; /* No LIMIT clause */
152062 if( pSub->selFlags & (SF_Aggregate|SF_Distinct) ){
152063 testcase( pSub->selFlags & SF_Aggregate );
152064 testcase( pSub->selFlags & SF_Distinct );
152067 assert( pSub->pHaving==0 ); /* Due to the previous */
152068 pSub = pSub->pPrior; /* Repeat over compound */
152073 db = pParse->db;
152077 sqlite3SrcListDelete(db, p->pSrc);
152078 p->pSrc = sqlite3DbMallocZero(pParse->db, SZ_SRCLIST_1);
152081 pPrior = pSub->pPrior;
152082 pSub->pPrior = 0;
152083 pSub->pNext = 0;
152084 pSub->selFlags |= SF_Aggregate;
152085 pSub->selFlags &= ~(u32)SF_Compound;
152086 pSub->nSelectRow = 0;
152087 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
152089 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
152099 p->pEList->a[0].pExpr = pExpr;
152100 p->selFlags &= ~(u32)SF_Aggregate;
152104 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
152112 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
152118 for(i=0; i<pSrc->nSrc; i++){
152119 SrcItem *p1 = &pSrc->a[i];
152121 if( p0->pSTab==p1->pSTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
152124 if( p1->fg.isSubquery
152125 && (p1->u4.pSubq->pSelect->selFlags & SF_NestedFrom)!=0
152126 && sameSrcAlias(p0, p1->u4.pSubq->pSelect->pSrc)
152135 ** Return TRUE (non-zero) if the i-th entry in the pTabList SrcList can
152136 ** be implemented as a co-routine. The i-th entry is guaranteed to be
152139 ** The subquery is implemented as a co-routine if all of the following are
152146 ** (b) The subquery is the left-most term and a CROSS JOIN or similar
152149 ** (i) The subquery is the left-most subquery in the FROM clause
152160 ** (5) The subquery is not self-joined
152168 SrcItem *pItem = &pTabList->a[i];
152169 if( pItem->fg.isCte ){
152170 const CteUse *pCteUse = pItem->u2.pCteUse;
152171 if( pCteUse->eM10d==M10d_Yes ) return 0; /* (2a) */
152172 if( pCteUse->nUse>=2 && pCteUse->eM10d!=M10d_No ) return 0; /* (2b) */
152174 if( pTabList->a[0].fg.jointype & JT_LTORJ ) return 0; /* (3) */
152175 if( OptimizationDisabled(pParse->db, SQLITE_Coroutines) ) return 0; /* (4) */
152176 if( isSelfJoinView(pTabList, pItem, i+1, pTabList->nSrc)!=0 ){
152180 if( pTabList->nSrc==1 ) return 1; /* (1a) */
152181 if( pTabList->a[1].fg.jointype & JT_CROSS ) return 1; /* (1b) */
152182 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
152185 if( selFlags & SF_UpdateFrom ) return 0; /* (1c-iii) */
152186 while( 1 /*exit-by-break*/ ){
152187 if( pItem->fg.jointype & (JT_OUTER|JT_CROSS) ) return 0; /* (1c-ii) */
152189 i--;
152190 pItem--;
152191 if( pItem->fg.isSubquery ) return 0; /* (1c-i) */
152197 ** Generate byte-code for the SELECT statement given in the p argument.
152204 ** pParse->zErrMsg.
152212 ** * Resolve names and similar preparation tag-select-0100
152213 ** * Scan of the FROM clause tag-select-0200
152214 ** + OUTER JOIN strength reduction tag-select-0220
152215 ** + Sub-query ORDER BY removal tag-select-0230
152216 ** + Query flattening tag-select-0240
152217 ** * Separate subroutine for compound-SELECT tag-select-0300
152218 ** * WHERE-clause constant propagation tag-select-0330
152219 ** * Count()-of-VIEW optimization tag-select-0350
152220 ** * Scan of the FROM clause again tag-select-0400
152221 ** + Authorize unreferenced tables tag-select-0410
152222 ** + Predicate push-down optimization tag-select-0420
152223 ** + Omit unused subquery columns optimization tag-select-0440
152224 ** + Generate code to implement subqueries tag-select-0480
152225 ** - Co-routines tag-select-0482
152226 ** - Reuse previously computed CTE tag-select-0484
152227 ** - REuse previously computed VIEW tag-select-0486
152228 ** - Materialize a VIEW or CTE tag-select-0488
152229 ** * DISTINCT ORDER BY -> GROUP BY optimization tag-select-0500
152230 ** * Set up for ORDER BY tag-select-0600
152231 ** * Create output table tag-select-0630
152232 ** * Prepare registers for LIMIT tag-select-0650
152233 ** * Setup for DISTINCT tag-select-0680
152234 ** * Generate code for non-aggregate and non-GROUP BY tag-select-0700
152235 ** * Generate code for aggregate and/or GROUP BY tag-select-0800
152236 ** + GROUP BY queries tag-select-0810
152237 ** + non-GROUP BY queries tag-select-0820
152238 ** - Special case of count() w/o GROUP BY tag-select-0821
152239 ** - General case of non-GROUP BY aggregates tag-select-0822
152240 ** * Sort results, as needed tag-select-0900
152241 ** * Internal self-checks tag-select-1000
152266 db = pParse->db;
152267 assert( pParse==db->pParse );
152269 if( p==0 || pParse->nErr ){
152272 assert( db->mallocFailed==0 );
152275 TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain));
152285 /* tag-select-0100 */
152286 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
152287 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
152288 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
152289 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
152291 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
152292 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
152293 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
152295 if( p->pOrderBy ){
152299 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
152303 p->pOrderBy);
152304 testcase( pParse->earlyCleanup );
152305 p->pOrderBy = 0;
152307 p->selFlags &= ~(u32)SF_Distinct;
152308 p->selFlags |= SF_NoopOrderBy;
152311 if( pParse->nErr ){
152314 assert( db->mallocFailed==0 );
152315 assert( p->pEList!=0 );
152325 ** In this case, it is an error if the target object (pSrc->a[0]) name
152326 ** or alias is duplicated within FROM clause (pSrc->a[1..n]).
152332 if( p->selFlags & SF_UFSrcCheck ){
152333 SrcItem *p0 = &p->pSrc->a[0];
152334 if( sameSrcAlias(p0, p->pSrc) ){
152337 p0->zAlias ? p0->zAlias : p0->pSTab->zName
152343 ** and leaving this flag set can cause errors if a compound sub-query
152344 ** in p->pSrc is flattened into this query and this function called
152346 p->selFlags &= ~(u32)SF_UFSrcCheck;
152349 if( pDest->eDest==SRT_Output ){
152355 assert( pParse->nErr );
152359 if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){
152365 pTabList = p->pSrc;
152366 isAgg = (p->selFlags & SF_Aggregate)!=0;
152368 sSort.pOrderBy = p->pOrderBy;
152372 ** tag-select-0200
152375 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
152376 SrcItem *pItem = &pTabList->a[i];
152377 Select *pSub = pItem->fg.isSubquery ? pItem->u4.pSubq->pSelect : 0;
152378 Table *pTab = pItem->pSTab;
152387 ** LEFT JOIN -> JOIN
152388 ** RIGHT JOIN -> JOIN
152389 ** FULL JOIN -> RIGHT JOIN
152391 ** If terms of the i-th table are used in the WHERE clause in such a
152392 ** way that the i-th table cannot be the NULL row of a join, then
152395 ** tag-select-0220
152397 if( (pItem->fg.jointype & (JT_LEFT|JT_LTORJ))!=0
152398 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor,
152399 pItem->fg.jointype & JT_LTORJ)
152402 if( pItem->fg.jointype & JT_LEFT ){
152403 if( pItem->fg.jointype & JT_RIGHT ){
152405 ("FULL-JOIN simplifies to RIGHT-JOIN on term %d\n",i));
152406 pItem->fg.jointype &= ~JT_LEFT;
152409 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
152410 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
152411 unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
152414 if( pItem->fg.jointype & JT_LTORJ ){
152415 for(j=i+1; j<pTabList->nSrc; j++){
152416 SrcItem *pI2 = &pTabList->a[j];
152417 if( pI2->fg.jointype & JT_RIGHT ){
152418 if( pI2->fg.jointype & JT_LEFT ){
152420 ("FULL-JOIN simplifies to LEFT-JOIN on term %d\n",j));
152421 pI2->fg.jointype &= ~JT_RIGHT;
152424 ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
152425 pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
152426 unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
152430 for(j=pTabList->nSrc-1; j>=0; j--){
152431 pTabList->a[j].fg.jointype &= ~JT_LTORJ;
152432 if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
152442 if( pTab->nCol!=pSub->pEList->nExpr ){
152444 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
152452 if( pItem->fg.isCte && pItem->u2.pCteUse->eM10d==M10d_Yes ){
152460 ** will be implemented as a co-routine and there is no advantage to
152463 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
152464 assert( pSub->pGroupBy==0 );
152466 /* tag-select-0230:
152467 ** If a FROM-clause subquery has an ORDER BY clause that is not
152476 ** (2) The subquery was added to help with window-function
152480 ** the built-in count(), min(), or max().
152492 if( pSub->pOrderBy!=0
152493 && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
152494 && pSub->pLimit==0 /* Condition (1) */
152495 && (pSub->selFlags & (SF_OrderByReqd|SF_Recursive))==0 /* (2) and (6) */
152496 && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
152500 ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1));
152502 pSub->pOrderBy);
152503 pSub->pOrderBy = 0;
152509 ** it will be implemented as a co-routine, then do not flatten. This
152524 if( pSub->pOrderBy!=0
152526 && (p->selFlags & SF_ComplexResult)!=0
152527 && (pTabList->nSrc==1
152528 || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0)
152533 /* tag-select-0240 */
152535 if( pParse->nErr ) goto select_end;
152537 i = -1;
152539 pTabList = p->pSrc;
152540 if( db->mallocFailed ) goto select_end;
152542 sSort.pOrderBy = p->pOrderBy;
152549 ** procedure. tag-select-0300
152551 if( p->pPrior ){
152554 TREETRACE(0x400,pParse,p,("end compound-select processing\n"));
152559 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
152564 /* Do the WHERE-clause constant propagation optimization if this is
152565 ** a join. No need to spend time on this operation for non-join queries
152567 ** sqlite3WhereBegin(). tag-select-0330
152569 if( p->pWhere!=0
152570 && p->pWhere->op==TK_AND
152584 /* tag-select-0350 */
152588 if( db->mallocFailed ) goto select_end;
152589 pTabList = p->pSrc;
152595 ** (2) Generate code for all sub-queries
152597 ** tag-select-0400
152599 for(i=0; i<pTabList->nSrc; i++){
152600 SrcItem *pItem = &pTabList->a[i];
152609 /* Authorized unreferenced tables. tag-select-0410
152616 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
152617 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
152624 ** assume the column name is non-NULL and segfault. The use of an empty
152627 if( pItem->colUsed==0 && pItem->zName!=0 ){
152629 if( pItem->fg.fixedSchema ){
152630 int iDb = sqlite3SchemaToIndex(pParse->db, pItem->u4.pSchema);
152631 zDb = db->aDb[iDb].zDbSName;
152632 }else if( pItem->fg.isSubquery ){
152635 zDb = pItem->u4.zDatabase;
152637 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", zDb);
152641 /* Generate code for all sub-queries in the FROM clause
152643 if( pItem->fg.isSubquery==0 ) continue;
152644 pSubq = pItem->u4.pSubq;
152646 pSub = pSubq->pSelect;
152649 if( pSubq->addrFillSub!=0 ) continue;
152654 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
152658 pParse->nHeight += sqlite3SelectExprHeight(p);
152660 /* Make copies of constant WHERE-clause terms in the outer query down
152662 ** This is the "predicate push-down optimization". tag-select-0420
152665 && (pItem->fg.isCte==0
152666 || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2))
152667 && pushDownWhereTerms(pParse, pSub, p->pWhere, pTabList, i)
152672 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
152676 assert( pSubq->pSelect && (pSub->selFlags & SF_PushDown)!=0 );
152678 TREETRACE(0x4000,pParse,p,("WHERE-clause push-down not possible\n"));
152683 ** tag-select-0440
152692 pSub->selId));
152698 zSavedAuthContext = pParse->zAuthContext;
152699 pParse->zAuthContext = pItem->zName;
152701 /* Generate byte-code to implement the subquery tag-select-0480
152703 if( fromClauseTermCanBeCoroutine(pParse, pTabList, i, p->selFlags) ){
152704 /* Implement a co-routine that will return a single row of the result
152705 ** set on each invocation. tag-select-0482
152709 pSubq->regReturn = ++pParse->nMem;
152710 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pSubq->regReturn, 0, addrTop);
152712 pSubq->addrFillSub = addrTop;
152713 sqlite3SelectDestInit(&dest, SRT_Coroutine, pSubq->regReturn);
152714 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem));
152716 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
152717 pItem->fg.viaCoroutine = 1;
152718 pSubq->regResult = dest.iSdst;
152719 sqlite3VdbeEndCoroutine(v, pSubq->regReturn);
152721 sqlite3VdbeJumpHere(v, addrTop-1);
152723 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
152726 ** then make the pItem->iCursor be a copy of the ephemeral table that
152727 ** holds the result of the materialization. tag-select-0484 */
152728 CteUse *pCteUse = pItem->u2.pCteUse;
152729 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
152730 if( pItem->iCursor!=pCteUse->iCur ){
152731 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
152734 pSub->nSelectRow = pCteUse->nRowEst;
152737 ** this same FROM clause. Reuse it. tag-select-0486 */
152739 assert( pPrior->fg.isSubquery );
152740 pPriorSubq = pPrior->u4.pSubq;
152742 if( pPriorSubq->addrFillSub ){
152743 sqlite3VdbeAddOp2(v, OP_Gosub, pPriorSubq->regReturn,
152744 pPriorSubq->addrFillSub);
152746 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
152747 pSub->nSelectRow = pPriorSubq->pSelect->nSelectRow;
152751 ** the same view can reuse the materialization. tag-select-0488 */
152758 pSubq->regReturn = ++pParse->nMem;
152760 pSubq->addrFillSub = topAddr+1;
152761 pItem->fg.isMaterialized = 1;
152762 if( pItem->fg.isCorrelated==0 ){
152771 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
152775 pItem->pSTab->nRowLogEst = pSub->nSelectRow;
152777 sqlite3VdbeAddOp2(v, OP_Return, pSubq->regReturn, topAddr+1);
152779 sqlite3VdbeScanStatusRange(v, addrExplain, addrExplain, -1);
152782 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
152783 CteUse *pCteUse = pItem->u2.pCteUse;
152784 pCteUse->addrM9e = pSubq->addrFillSub;
152785 pCteUse->regRtn = pSubq->regReturn;
152786 pCteUse->iCur = pItem->iCursor;
152787 pCteUse->nRowEst = pSub->nSelectRow;
152790 if( db->mallocFailed ) goto select_end;
152791 pParse->nHeight -= sqlite3SelectExprHeight(p);
152792 pParse->zAuthContext = zSavedAuthContext;
152798 pEList = p->pEList;
152799 pWhere = p->pWhere;
152800 pGroupBy = p->pGroupBy;
152801 pHaving = p->pHaving;
152802 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
152806 TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n"));
152811 /* tag-select-0500
152814 ** if the select-list is the same as the ORDER BY list, then this query
152823 ** The second form is preferred as a single index (or temp-table) may be
152825 ** written the query must use a temp-table for at least one of the ORDER
152826 ** BY and DISTINCT, and an index or separate temp-table for the other.
152828 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
152829 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
152832 && p->pWin==0
152835 p->selFlags &= ~(u32)SF_Distinct;
152836 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
152838 for(i=0; i<pGroupBy->nExpr; i++){
152839 pGroupBy->a[i].u.x.iOrderByCol = i+1;
152842 p->selFlags |= SF_Aggregate;
152843 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
152859 ** being unused if the data can be extracted in pre-sorted order.
152863 ** that change. tag-select-0600
152868 pParse, sSort.pOrderBy, 0, pEList->nExpr);
152869 sSort.iECursor = pParse->nTab++;
152872 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
152876 sSort.addrSortIndex = -1;
152880 ** tag-select-0630
152882 if( pDest->eDest==SRT_EphemTab ){
152883 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
152884 if( p->selFlags & SF_NestedFrom ){
152885 /* Delete or NULL-out result columns that will never be used */
152887 for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){
152888 sqlite3ExprDelete(db, pEList->a[ii].pExpr);
152889 sqlite3DbFree(db, pEList->a[ii].zEName);
152890 pEList->nExpr--;
152892 for(ii=0; ii<pEList->nExpr; ii++){
152893 if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL;
152898 /* Set the limiter. tag-select-0650
152901 if( (p->selFlags & SF_FixedLimit)==0 ){
152902 p->nSelectRow = 320; /* 4 billion rows */
152904 if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd);
152905 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
152910 /* Open an ephemeral index to use for the distinct set. tag-select-0680
152912 if( p->selFlags & SF_Distinct ){
152913 sDistinct.tabTnct = pParse->nTab++;
152916 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
152925 /* No aggregate functions and no GROUP BY clause. tag-select-0700 */
152927 | (p->selFlags & SF_FixedLimit);
152929 Window *pWin = p->pWin; /* Main window object (or NULL) */
152940 p->pEList, p, wctrlFlags, p->nSelectRow);
152942 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
152943 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
152944 if( pDest->eDest<=SRT_DistQueue && pDest->eDest>=SRT_DistFifo ){
152947 ** Search for tag-20250414a to see other cases */
152948 p->nSelectRow -= 30;
152957 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
152971 assert( p->pEList==pEList );
152977 int regGosub = ++pParse->nMem;
152983 VdbeNoopComment((v, "inner-loop subroutine"));
152985 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
152988 VdbeComment((v, "end inner-loop subroutine"));
152994 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
153005 ** clause or both. tag-select-0800 */
153026 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
153027 pItem->u.x.iAlias = 0;
153029 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
153030 pItem->u.x.iAlias = 0;
153033 if( p->nSelectRow>66 ) p->nSelectRow = 66;
153038 ** in the correct order. It also may not - the GROUP BY might use a
153043 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
153046 ** ASC or DESC order - only that each group is returned contiguously.
153050 for(ii=0; ii<pGroupBy->nExpr; ii++){
153052 sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC;
153053 pGroupBy->a[ii].fg.sortFlags = sortFlags;
153055 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
153061 p->nSelectRow = 0;
153074 testcase( pParse->earlyCleanup );
153076 if( db->mallocFailed ){
153079 pAggInfo->selId = p->selId;
153081 pAggInfo->pSelect = p;
153088 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
153089 pAggInfo->pGroupBy = pGroupBy;
153094 assert( pWhere==p->pWhere );
153095 assert( pHaving==p->pHaving );
153096 assert( pGroupBy==p->pGroupBy );
153098 pWhere = p->pWhere;
153102 pAggInfo->nAccumulator = pAggInfo->nColumn;
153103 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
153104 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
153109 if( db->mallocFailed ) goto select_end;
153124 ** much more complex than aggregates without a GROUP BY. tag-select-0810
153128 int addr1; /* A-vs-B comparison jump */
153140 if( pAggInfo->nFunc==1
153141 && pAggInfo->aFunc[0].iDistinct>=0
153142 && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0)
153143 && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr))
153144 && pAggInfo->aFunc[0].pFExpr->x.pList!=0
153146 Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr;
153158 pAggInfo->sortingIdx = pParse->nTab++;
153160 0, pAggInfo->nColumn);
153162 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
153167 iUseFlag = ++pParse->nMem;
153168 iAbortFlag = ++pParse->nMem;
153169 regOutputRow = ++pParse->nMem;
153171 regReset = ++pParse->nMem;
153173 iAMem = pParse->nMem + 1;
153174 pParse->nMem += pGroupBy->nExpr;
153175 iBMem = pParse->nMem + 1;
153176 pParse->nMem += pGroupBy->nExpr;
153179 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
153196 if( pParse->pIdxEpr ){
153202 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
153222 ExplainQueryPlan2(addrExp, (pParse, 0, "USE TEMP B-TREE FOR %s",
153223 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
153228 nGroupBy = pGroupBy->nExpr;
153231 for(i=0; i<pAggInfo->nColumn; i++){
153232 if( pAggInfo->aCol[i].iSorterColumn>=j ){
153240 pAggInfo->directMode = 1;
153241 for(i=0; i<pAggInfo->nColumn; i++){
153242 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
153243 if( pCol->iSorterColumn>=j ){
153244 sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase);
153248 pAggInfo->directMode = 0;
153252 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
153253 sqlite3VdbeScanStatusRange(v, addrExp, sqlite3VdbeCurrentAddr(v)-2, -1);
153258 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
153262 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
153264 pAggInfo->useSortingIdx = 1;
153265 sqlite3VdbeScanStatusRange(v, addrExp, -1, sortPTab);
153266 sqlite3VdbeScanStatusRange(v, addrExp, -1, pAggInfo->sortingIdx);
153269 /* If there are entries in pAgggInfo->aFunc[] that contain subexpressions
153274 if( pParse->pIdxEpr ){
153290 ** This is an optimization - the correct answer should result regardless.
153307 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
153310 for(j=0; j<pGroupBy->nExpr; j++){
153311 int iOrderByCol = pGroupBy->a[j].u.x.iOrderByCol;
153316 pAggInfo->directMode = 1;
153317 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
153321 Expr *pX = p->pEList->a[iOrderByCol-1].pExpr;
153323 while( ALWAYS(pBase!=0) && pBase->op==TK_IF_NULL_ROW ){
153324 pX = pBase->pLeft;
153328 && pBase->op!=TK_AGG_COLUMN
153329 && pBase->op!=TK_REGISTER
153335 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
153351 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
153368 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
153388 ** is less than or equal to zero, the subroutine is a no-op. If
153405 selectInnerLoop(pParse, p, -1, &sSort,
153411 /* Generate a subroutine that will reset the group-by accumulator
153420 struct AggInfo_func *pF = &pAggInfo->aFunc[0];
153421 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
153425 /* Aggregate functions without GROUP BY. tag-select-0820 */
153428 /* tag-select-0821
153443 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
153444 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
153448 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
153451 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
153455 ** (2011-04-15) Do not do a full scan of an unordered index.
153457 ** (2013-10-03) Do not count the entries in a partial index.
153463 if( !p->pSrc->a[0].fg.notIndexed ){
153464 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
153465 if( pIdx->bUnordered==0
153466 && pIdx->szIdxRow<pTab->szTabRow
153467 && pIdx->pPartIdxWhere==0
153468 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
153475 iRoot = pBest->tnum;
153479 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
153482 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
153490 ** tag-select-0822 */
153505 if( pAggInfo->nAccumulator ){
153506 for(i=0; i<pAggInfo->nFunc; i++){
153507 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
153510 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
153514 if( i==pAggInfo->nFunc ){
153515 regAcc = ++pParse->nMem;
153518 }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
153519 assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) );
153520 pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
153529 assert( p->pGroupBy==0 );
153538 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
153550 struct AggInfo_func *pF = pAggInfo->aFunc;
153552 fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr);
153567 selectInnerLoop(pParse, p, -1, 0, 0,
153579 ** and send them to the callback one by one. tag-select-0900
153582 assert( p->pEList==pEList );
153583 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
153592 rc = (pParse->nErr>0);
153598 assert( db->mallocFailed==0 || db->mallocFailed==1 );
153599 assert( db->mallocFailed==0 || pParse->nErr!=0 );
153602 /* Internal self-checks. tag-select-1000 */
153603 if( pAggInfo && !db->mallocFailed ){
153610 for(i=0; i<pAggInfo->nColumn; i++){
153611 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
153613 assert( pExpr->pAggInfo==pAggInfo );
153614 assert( pExpr->iAgg==i );
153616 for(i=0; i<pAggInfo->nFunc; i++){
153617 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
153619 assert( pExpr->pAggInfo==pAggInfo );
153620 assert( pExpr->iAgg==i );
153680 int need; /* Slots needed in p->azResult[] */
153684 /* Make sure there is enough space in p->azResult to hold everything
153687 if( p->nRow==0 && argv!=0 ){
153692 if( p->nData + need > p->nAlloc ){
153694 p->nAlloc = p->nAlloc*2 + need;
153695 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
153697 p->azResult = azNew;
153703 if( p->nRow==0 ){
153704 p->nColumn = nCol;
153708 p->azResult[p->nData++] = z;
153710 }else if( (int)p->nColumn!=nCol ){
153711 sqlite3_free(p->zErrMsg);
153712 p->zErrMsg = sqlite3_mprintf(
153715 p->rc = SQLITE_ERROR;
153731 p->azResult[p->nData++] = z;
153733 p->nRow++;
153738 p->rc = SQLITE_NOMEM_BKPT;
153778 db->errCode = SQLITE_NOMEM;
153794 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
153807 db->errCode = SQLITE_NOMEM;
153826 azResult--;
153859 pTriggerStep = pTriggerStep->pNext;
153861 sqlite3ExprDelete(db, pTmp->pWhere);
153862 sqlite3ExprListDelete(db, pTmp->pExprList);
153863 sqlite3SelectDelete(db, pTmp->pSelect);
153864 sqlite3IdListDelete(db, pTmp->pIdList);
153865 sqlite3UpsertDelete(db, pTmp->pUpsert);
153866 sqlite3SrcListDelete(db, pTmp->pFrom);
153867 sqlite3DbFree(db, pTmp->zSpan);
153878 ** are already attached to pTab->pTrigger. But there might be additional
153880 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
153885 ** pTab as well as the triggers lised in pTab->pTrigger.
153892 assert( pParse->disableTriggers==0 );
153893 pTmpSchema = pParse->db->aDb[1].pSchema;
153894 p = sqliteHashFirst(&pTmpSchema->trigHash);
153895 pList = pTab->pTrigger;
153898 if( pTrig->pTabSchema==pTab->pSchema
153899 && pTrig->table
153900 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
153901 && (pTrig->pTabSchema!=pTmpSchema || pTrig->bReturning)
153903 pTrig->pNext = pList;
153905 }else if( pTrig->op==TK_RETURNING ){
153907 assert( pParse->db->pVtabCtx==0 );
153909 assert( pParse->bReturning );
153910 assert( !pParse->isCreate );
153911 assert( &(pParse->u1.d.pReturning->retTrig) == pTrig );
153912 pTrig->table = pTab->zName;
153913 pTrig->pTabSchema = pTab->pSchema;
153914 pTrig->pNext = pList;
153922 printf("Triggers for %s:", pTab->zName);
153923 for(pX=pList; pX; pX=pX->pNext){
153924 printf(" %s", pX->zName);
153937 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
153956 sqlite3 *db = pParse->db; /* The database connection */
153961 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
153967 if( pName2->n>0 ){
153980 if( !pTableName || db->mallocFailed ){
153984 /* A long-standing parser bug is that this syntax was allowed:
153992 if( db->init.busy && iDb!=1 ){
153993 assert( pTableName->a[0].fg.fixedSchema==0 );
153994 assert( pTableName->a[0].fg.isSubquery==0 );
153995 sqlite3DbFree(db, pTableName->a[0].u4.zDatabase);
153996 pTableName->a[0].u4.zDatabase = 0;
154005 if( db->init.busy==0 && pName2->n==0 && pTab
154006 && pTab->pSchema==db->aDb[1].pSchema ){
154011 if( db->mallocFailed ) goto trigger_cleanup;
154012 assert( pTableName->nSrc==1 );
154026 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
154035 assert( db->mallocFailed );
154038 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
154043 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
154047 assert( !db->init.busy );
154049 VVA_ONLY( pParse->ifNotExists = 1; )
154056 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
154066 (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName->a);
154071 " trigger on table: %S", pTableName->a);
154077 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
154079 const char *zDb = db->aDb[iTabDb].zDbSName;
154080 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
154082 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
154103 pTrigger->zName = zName;
154105 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
154106 pTrigger->pSchema = db->aDb[iDb].pSchema;
154107 pTrigger->pTabSchema = pTab->pSchema;
154108 pTrigger->op = (u8)op;
154109 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
154111 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
154112 pTrigger->pWhen = pWhen;
154115 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
154117 pTrigger->pColumns = pColumns;
154119 assert( pParse->pNewTrigger==0 );
154120 pParse->pNewTrigger = pTrigger;
154127 if( !pParse->pNewTrigger ){
154130 assert( pParse->pNewTrigger==pTrigger );
154135 if( db->init.iDb==1 ){
154138 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
154142 ** "orphaned trigger" - a trigger whose associated table is missing.
154144 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
154146 db->init.orphanTrigger = 1;
154160 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
154162 sqlite3 *db = pParse->db; /* The database */
154167 pParse->pNewTrigger = 0;
154168 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
154169 zName = pTrig->zName;
154170 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
154171 pTrig->step_list = pStepList;
154173 pStepList->pTrig = pTrig;
154174 pStepList = pStepList->pNext;
154176 sqlite3TokenInit(&nameToken, pTrig->zName);
154178 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
154179 || sqlite3FixExpr(&sFix, pTrig->pWhen)
154186 assert( !db->init.busy );
154187 pParse->pNewTrigger = pTrig;
154195 if( !db->init.busy ){
154200 ** are read-only, and the trigger makes a change to a shadow table,
154201 ** then raise an error - do not allow the trigger to be created. */
154204 for(pStep=pTrig->step_list; pStep; pStep=pStep->pNext){
154205 if( pStep->zTarget!=0
154206 && sqlite3ShadowTableName(db, pStep->zTarget)
154210 pTrig->zName, pStep->zTarget);
154220 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
154225 db->aDb[iDb].zDbSName, zName,
154226 pTrig->table, z);
154233 if( db->init.busy ){
154235 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
154241 }else if( pLink->pSchema==pLink->pTabSchema ){
154243 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
154245 pLink->pNext = pTab->pTrigger;
154246 pTab->pTrigger = pLink;
154252 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
154285 pTriggerStep->op = TK_SELECT;
154286 pTriggerStep->pSelect = pSelect;
154287 pTriggerStep->orconf = OE_Default;
154288 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
154296 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
154305 sqlite3 *db = pParse->db;
154308 if( pParse->nErr ) return 0;
154309 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
154312 memcpy(z, pName->z, pName->n);
154314 pTriggerStep->zTarget = z;
154315 pTriggerStep->op = op;
154316 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
154318 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
154341 sqlite3 *db = pParse->db;
154344 assert(pSelect != 0 || db->mallocFailed);
154349 pTriggerStep->pSelect = pSelect;
154352 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
154354 pTriggerStep->pIdList = pColumn;
154355 pTriggerStep->pUpsert = pUpsert;
154356 pTriggerStep->orconf = orconf;
154358 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
154379 SrcList *pFrom, /* FROM clause for an UPDATE-FROM, or NULL */
154386 sqlite3 *db = pParse->db;
154392 pTriggerStep->pExprList = pEList;
154393 pTriggerStep->pWhere = pWhere;
154394 pTriggerStep->pFrom = pFrom;
154399 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
154400 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
154401 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
154403 pTriggerStep->orconf = orconf;
154423 sqlite3 *db = pParse->db;
154429 pTriggerStep->pWhere = pWhere;
154432 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
154434 pTriggerStep->orconf = OE_Default;
154444 if( pTrigger==0 || pTrigger->bReturning ) return;
154445 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
154446 sqlite3DbFree(db, pTrigger->zName);
154447 sqlite3DbFree(db, pTrigger->table);
154448 sqlite3ExprDelete(db, pTrigger->pWhen);
154449 sqlite3IdListDelete(db, pTrigger->pColumns);
154466 sqlite3 *db = pParse->db;
154468 if( db->mallocFailed ) goto drop_trigger_cleanup;
154473 assert( pName->nSrc==1 );
154474 assert( pName->a[0].fg.fixedSchema==0 && pName->a[0].fg.isSubquery==0 );
154475 zDb = pName->a[0].u4.zDatabase;
154476 zName = pName->a[0].zName;
154478 for(i=OMIT_TEMPDB; i<db->nDb; i++){
154482 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
154487 sqlite3ErrorMsg(pParse, "no such trigger: %S", pName->a);
154491 pParse->checkSchema = 1;
154505 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
154515 sqlite3 *db = pParse->db;
154518 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
154519 assert( iDb>=0 && iDb<db->nDb );
154521 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
154525 const char *zDb = db->aDb[iDb].zDbSName;
154528 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
154540 db->aDb[iDb].zDbSName, pTrigger->zName
154543 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
154555 pHash = &(db->aDb[iDb].pSchema->trigHash);
154558 if( pTrigger->pSchema==pTrigger->pTabSchema ){
154562 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
154564 *pp = (*pp)->pNext;
154571 db->mDbFlags |= DBFLAG_SchemaChange;
154587 for(e=0; e<pEList->nExpr; e++){
154588 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
154597 if( NEVER(db->aDb[1].pSchema==0) ) return 0;
154598 if( sqliteHashFirst(&db->aDb[1].pSchema->trigHash)==0 ) return 0;
154621 || (pList->bReturning && pList->pNext==0) );
154624 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
154625 && pTab->pTrigger!=0
154630 if( pList==pTab->pTrigger ){
154634 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
154635 p->pNext = 0;
154639 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
154640 mask |= p->tr_tm;
154641 }else if( p->op==TK_RETURNING ){
154645 p->op = op;
154652 p->tr_tm = TRIGGER_BEFORE;
154654 p->tr_tm = TRIGGER_AFTER;
154656 mask |= p->tr_tm;
154657 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
154660 mask |= p->tr_tm;
154662 p = p->pNext;
154679 if( (pTab->pTrigger==0 && !tempTriggersExist(pParse->db))
154680 || pParse->disableTriggers
154689 ** Convert the pStep->zTarget string into a SrcList and return a pointer
154702 sqlite3 *db = pParse->db;
154704 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
154706 assert( pSrc==0 || pSrc->nSrc==1 );
154709 Schema *pSchema = pStep->pTrig->pSchema;
154710 pSrc->a[0].zName = zName;
154711 if( pSchema!=db->aDb[1].pSchema ){
154712 assert( pSrc->a[0].fg.fixedSchema || pSrc->a[0].u4.zDatabase==0 );
154713 pSrc->a[0].u4.pSchema = pSchema;
154714 pSrc->a[0].fg.fixedSchema = 1;
154716 if( pStep->pFrom ){
154717 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
154718 if( pDup && pDup->nSrc>1 && !IN_RENAME_OBJECT ){
154744 if( pTerm->op==TK_ASTERISK ) return 1;
154745 if( pTerm->op!=TK_DOT ) return 0;
154746 assert( pTerm->pRight!=0 );
154747 assert( pTerm->pLeft!=0 );
154748 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
154765 sqlite3 *db = pParse->db;
154768 for(i=0; i<pList->nExpr; i++){
154769 Expr *pOldExpr = pList->a[i].pExpr;
154773 for(jj=0; jj<pTab->nCol; jj++){
154775 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
154776 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zCnName);
154778 if( !db->mallocFailed ){
154779 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
154780 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zCnName);
154781 pItem->fg.eEName = ENAME_NAME;
154787 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
154788 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
154789 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
154790 pItem->fg.eEName = pList->a[i].fg.eEName;
154804 && (pExpr->x.pSelect->selFlags & SF_Correlated)!=0
154814 ** If the SELECT references the table pWalker->u.pTab, then do two things:
154817 ** (2) Set pWalker->eCode to non-zero so that the caller will know
154824 pSrc = pSelect->pSrc;
154826 for(i=0; i<pSrc->nSrc; i++){
154827 if( pSrc->a[i].pSTab==pWalker->u.pTab ){
154828 testcase( pSelect->selFlags & SF_Correlated );
154829 pSelect->selFlags |= SF_Correlated;
154830 pWalker->eCode = 1;
154866 ** is generated in-line.
154874 Vdbe *v = pParse->pVdbe;
154875 sqlite3 *db = pParse->db;
154883 if( !pParse->bReturning ){
154888 assert( db->pParse==pParse );
154889 assert( !pParse->isCreate );
154890 pReturning = pParse->u1.d.pReturning;
154891 if( pTrigger != &(pReturning->retTrig) ){
154898 sSelect.pEList = sqlite3ExprListDup(db, pReturning->pReturnEL, 0);
154900 pFrom->nSrc = 1;
154901 pFrom->a[0].pSTab = pTab;
154902 pFrom->a[0].zName = pTab->zName; /* tag-20240424-1 */
154903 pFrom->a[0].iCursor = -1;
154905 if( pParse->nErr==0 ){
154906 assert( db->mallocFailed==0 );
154910 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
154911 if( pParse->nErr==0 ){
154914 if( pReturning->nRetCol==0 ){
154915 pReturning->nRetCol = pNew->nExpr;
154916 pReturning->iRetCur = pParse->nTab++;
154921 pParse->eTriggerOp = pTrigger->op;
154922 pParse->pTriggerTab = pTab;
154924 && ALWAYS(!db->mallocFailed)
154927 int nCol = pNew->nExpr;
154928 int reg = pParse->nMem+1;
154930 pParse->nMem += nCol+2;
154931 pReturning->iRetReg = reg;
154933 Expr *pCol = pNew->a[i].pExpr;
154934 assert( pCol!=0 ); /* Due to !db->mallocFailed ~9 lines above */
154941 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
154942 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
154946 pParse->eTriggerOp = 0;
154947 pParse->pTriggerTab = 0;
154962 Vdbe *v = pParse->pVdbe;
154963 sqlite3 *db = pParse->db;
154965 assert( pParse->pTriggerTab && pParse->pToplevel );
154968 for(pStep=pStepList; pStep; pStep=pStep->pNext){
154979 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
154980 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
154982 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
154983 assert( pParse->okConstFactor==0 );
154986 if( pStep->zSpan ){
154988 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
154993 switch( pStep->op ){
154997 sqlite3ExprListDup(db, pStep->pExprList, 0),
154998 sqlite3ExprDup(db, pStep->pWhere, 0),
154999 pParse->eOrconf, 0, 0, 0
155007 sqlite3SelectDup(db, pStep->pSelect, 0),
155008 sqlite3IdListDup(db, pStep->pIdList),
155009 pParse->eOrconf,
155010 sqlite3UpsertDup(db, pStep->pUpsert)
155018 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
155023 default: assert( pStep->op==TK_SELECT ); {
155025 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
155056 ** Parse context structure pFrom has just been used to create a sub-vdbe
155061 assert( pFrom->zErrMsg==0 || pFrom->nErr );
155062 assert( pTo->zErrMsg==0 || pTo->nErr );
155063 if( pTo->nErr==0 ){
155064 pTo->zErrMsg = pFrom->zErrMsg;
155065 pTo->nErr = pFrom->nErr;
155066 pTo->rc = pFrom->rc;
155068 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
155073 ** Create and populate a new TriggerPrg object with a sub-program
155083 sqlite3 *db = pParse->db; /* Database handle */
155087 NameContext sNC; /* Name context for sub-vdbe */
155088 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
155090 Parse sSubParse; /* Parse context for sub-vdbe */
155092 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
155093 assert( pTop->pVdbe );
155097 ** list of the top-level Parse object sooner rather than later. */
155100 pPrg->pNext = pTop->pTriggerPrg;
155101 pTop->pTriggerPrg = pPrg;
155102 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
155104 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
155105 pPrg->pTrigger = pTrigger;
155106 pPrg->orconf = orconf;
155107 pPrg->aColmask[0] = 0xffffffff;
155108 pPrg->aColmask[1] = 0xffffffff;
155111 ** trigger sub-program. */
155117 sSubParse.zAuthContext = pTrigger->zName;
155118 sSubParse.eTriggerOp = pTrigger->op;
155119 sSubParse.nQueryLoop = pParse->nQueryLoop;
155120 sSubParse.prepFlags = pParse->prepFlags;
155127 pTrigger->zName, onErrorText(orconf),
155128 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
155129 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
155130 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
155131 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
155132 pTab->zName
155135 if( pTrigger->zName ){
155136 sqlite3VdbeChangeP4(v, -1,
155137 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
155143 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
155145 if( pTrigger->pWhen ){
155146 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
155147 if( db->mallocFailed==0
155156 /* Code the trigger program into the sub-vdbe. */
155157 codeTriggerProgram(&sSubParse, pTrigger->step_list, orconf);
155159 /* Insert an OP_Halt at the end of the sub-program. */
155164 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
155167 if( pParse->nErr==0 ){
155168 assert( db->mallocFailed==0 );
155169 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
155171 pProgram->nMem = sSubParse.nMem;
155172 pProgram->nCsr = sSubParse.nTab;
155173 pProgram->token = (void *)pTrigger;
155174 pPrg->aColmask[0] = sSubParse.oldmask;
155175 pPrg->aColmask[1] = sSubParse.newmask;
155187 ** Return a pointer to a TriggerPrg object containing the sub-program for
155201 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
155207 for(pPrg=pRoot->pTriggerPrg;
155208 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
155209 pPrg=pPrg->pNext
155215 pParse->db->errByteOffset = -1;
155238 assert( pPrg || pParse->nErr );
155241 ** is a pointer to the sub-vdbe containing the trigger program. */
155243 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
155245 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
155246 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
155248 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
155250 /* Set the P5 operand of the OP_Program instruction to non-zero if
155252 ** invocation is disallowed if (a) the sub-program is really a trigger,
155267 ** operation on pTab, this function is a no-op.
155272 ** (a copy of pTab->nCol), then registers are populated as follows:
155275 ** ------------------------------------------------------
155277 ** reg+1 OLD.* value of left-most column of pTab
155279 ** reg+N OLD.* value of right-most column of pTab
155281 ** reg+N+2 NEW.* value of left-most column of pTab
155283 ** reg+N+N+1 NEW.* value of right-most column of pTab
155316 for(p=pTrigger; p; p=p->pNext){
155321 assert( p->pSchema!=0 );
155322 assert( p->pTabSchema!=0 );
155323 assert( p->pSchema==p->pTabSchema
155324 || p->pSchema==pParse->db->aDb[1].pSchema );
155331 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
155332 && p->tr_tm==tr_tm
155333 && checkColumnOverlap(p->pColumns, pChanges)
155335 if( !p->bReturning ){
155345 ** Triggers may access values stored in the old.* or new.* pseudo-table.
155346 ** This function returns a 32-bit bitmask indicating which columns of the
155351 ** Bit 0 of the returned mask is set if the left-most column of the
155386 for(p=pTrigger; p; p=p->pNext){
155387 if( p->op==op
155388 && (tr_tm&p->tr_tm)
155389 && checkColumnOverlap(p->pColumns,pChanges)
155391 if( p->bReturning ){
155397 mask |= pPrg->aColmask[isNew];
155442 ** i-th column of table pTab. This routine sets the P4 parameter of the
155448 ** command. If the latter, then the row-records in the table btree on disk
155451 ** If the former, then all row-records are guaranteed to include a value
155465 ** If column as REAL affinity and the table is an ordinary b-tree table
155473 assert( pTab->nCol>i );
155474 pCol = &pTab->aCol[i];
155475 if( pCol->iDflt ){
155479 VdbeComment((v, "%s.%s", pTab->zName, pCol->zCnName));
155480 assert( i<pTab->nCol );
155483 pCol->affinity, &pValue);
155489 if( pCol->affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
155498 ** and false if not. This is an optimization. False-positives are a
155499 ** performance degradation, but false-negatives can result in a corrupt
155502 ** aXRef[j] will be non-negative if column j of the original table is
155512 i16 iIdxCol = pIdx->aiColumn[iCol];
155518 assert( pIdx->aColExpr!=0 );
155519 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
155520 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
155528 ** to be unchanged. This is an optimization. False-positives are a
155529 ** performance degradation, but false-negatives can result in a corrupt
155532 ** aXRef[j] will be non-negative if column j of the original table is
155541 if( pIdx->pPartIdxWhere==0 ) return 0;
155542 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
155550 ** table in the source-list (pSrc->a[0]).
155554 if( pRet ) pRet->iColumn = iCol+1;
155562 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
155570 ** SELECT <other-columns>, pChanges FROM pTabList
155572 ** GROUP BY <other-columns>
155578 ** the <other-columns> in the query above are is determined by the type
155579 ** of table pTabList->a[0].pTab.
155582 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
155586 ** If the table is actually a view, then <other-columns> are all columns of
155590 ** If the table is a virtual or ordinary intkey table, then <other-columns>
155593 ** rowid value in <other-columns> is used as the integer key, and the
155613 sqlite3 *db = pParse->db;
155614 Table *pTab = pTabList->a[0].pSTab;
155634 assert( pTabList->nSrc>1 );
155636 assert( pSrc->a[0].fg.notCte );
155637 pSrc->a[0].iCursor = -1;
155638 pSrc->a[0].pSTab->nTabRef--;
155639 pSrc->a[0].pSTab = 0;
155642 for(i=0; i<pPk->nKeyCol; i++){
155643 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
155653 for(i=0; i<pTab->nCol; i++){
155666 assert( pChanges!=0 || pParse->db->mallocFailed );
155668 for(i=0; i<pChanges->nExpr; i++){
155670 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
155678 if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
155680 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
155718 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
155719 ** an expression for the i-th column of the table.
155720 ** aXRef[i]==-1 if the i-th column is not changed. */
155726 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
155728 NameContext sNC; /* The name-context to resolve expressions in */
155750 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
155762 db = pParse->db;
155763 assert( db->pParse==pParse );
155764 if( pParse->nErr ){
155767 assert( db->mallocFailed==0 );
155773 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
155795 sqlite3TreeViewUpdate(pParse->pWith, pTabList, pChanges, pWhere,
155801 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
155804 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
155829 iBaseCur = iDataCur = pParse->nTab++;
155832 testcase( pPk!=0 && pPk!=pTab->pIndex );
155833 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
155835 iDataCur = pParse->nTab;
155837 pParse->nTab++;
155841 iDataCur = pUpsert->iDataCur;
155842 iIdxCur = pUpsert->iIdxCur;
155843 pParse->nTab = iBaseCur;
155845 pTabList->a[0].iCursor = iDataCur;
155850 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
155852 aRegIdx = aXRef+pTab->nCol;
155856 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
155858 /* Initialize the name-context */
155876 for(i=0; i<pChanges->nExpr; i++){
155879 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
155882 j = sqlite3ColumnIndex(pTab, pChanges->a[i].zEName);
155884 if( j==pTab->iPKey ){
155886 pRowidExpr = pChanges->a[i].pExpr;
155888 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
155892 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
155893 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
155894 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
155897 pTab->aCol[j].zCnName);
155903 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
155904 j = -1;
155906 pRowidExpr = pChanges->a[i].pExpr;
155909 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
155910 pParse->checkSchema = 1;
155917 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
155918 j<0 ? "ROWID" : pTab->aCol[j].zCnName,
155919 db->aDb[iDb].zDbSName);
155923 aXRef[j] = -1;
155937 ** is non-negative, so the value of aXRef[] for generated columns can be
155938 ** set to any non-negative number. We use 99999 so that the value is
155941 if( pTab->tabFlags & TF_HasGenerated ){
155943 testcase( pTab->tabFlags & TF_HasVirtual );
155944 testcase( pTab->tabFlags & TF_HasStored );
155947 for(i=0; i<pTab->nCol; i++){
155949 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
155951 sqlite3ColumnExpr(pTab, &pTab->aCol[i]),
155967 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
155976 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
155981 reg = ++pParse->nMem;
155982 pParse->nMem += pIdx->nColumn;
155985 for(i=0; i<pIdx->nKeyCol; i++){
155987 reg = ++pParse->nMem;
155988 pParse->nMem += pIdx->nColumn;
155989 if( onError==OE_Default && pIdx->onError==OE_Replace ){
155999 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
156006 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
156015 ** two-pass update algorithm. */
156016 assert( aRegIdx[nAllIdx]==pParse->nMem );
156018 regOldRowid = regNewRowid = ++pParse->nMem;
156020 regOld = pParse->nMem + 1;
156021 pParse->nMem += pTab->nCol;
156024 regNewRowid = ++pParse->nMem;
156026 regNew = pParse->nMem + 1;
156027 pParse->nMem += pTab->nCol;
156032 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
156069 if( (db->flags&SQLITE_CountRows)!=0
156070 && !pParse->pTriggerTab
156071 && !pParse->nested
156072 && !pParse->bReturning
156075 regRowCount = ++pParse->nMem;
156081 iEph = pParse->nTab++;
156085 nPk = pPk ? pPk->nKeyCol : 0;
156086 iPk = pParse->nMem+1;
156087 pParse->nMem += nPk;
156088 pParse->nMem += nChangeFrom;
156089 regKey = ++pParse->nMem;
156091 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
156092 iEph = pParse->nTab++;
156093 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
156098 pKeyInfo->nAllField = nEphCol;
156132 ** Do not consider a single-pass strategy for a multi-row update if
156142 if( !pParse->nested
156154 /* A one-pass strategy that might update more than one row may not
156170 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
156180 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
156184 aRegIdx[nAllIdx] = ++pParse->nMem;
156196 assert( pPk->aiColumn[i]>=0 );
156198 pPk->aiColumn[i], iPk+i);
156224 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
156225 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
156228 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
156306 /* Compute the old pre-UPDATE content of the row being changed, if that
156313 for(i=0; i<pTab->nCol; i++){
156314 u32 colFlags = pTab->aCol[i].colFlags;
156350 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
156351 if( i==pTab->iPKey ){
156353 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
156354 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
156359 int nOff = (isView ? pTab->nCol : nPk);
156363 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
156381 if( pTab->tabFlags & TF_HasGenerated ){
156382 testcase( pTab->tabFlags & TF_HasVirtual );
156383 testcase( pTab->tabFlags & TF_HasStored );
156397 /* The row-trigger may have deleted the row being updated. In this
156399 ** required. This behavior - what happens when the row being updated
156400 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
156411 /* After-BEFORE-trigger-reload-loop:
156417 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
156420 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
156421 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
156422 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
156423 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
156428 if( pTab->tabFlags & TF_HasGenerated ){
156429 testcase( pTab->tabFlags & TF_HasVirtual );
156430 testcase( pTab->tabFlags & TF_HasStored );
156462 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
156475 ** to invoke the pre-update hook.
156478 ** pre-update hook. If the caller invokes preupdate_new(), the returned
156492 if( !pParse->nested ){
156535 /* Nothing to do at end-of-loop for a single-pass */
156549 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
156587 ** There are two possible strategies - the default and the special
156615 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
156618 sqlite3 *db = pParse->db; /* Database connection */
156621 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
156625 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
156634 ephemTab = pParse->nTab++;
156636 regArg = pParse->nMem + 1;
156637 pParse->nMem += nArg;
156638 if( pSrc->nSrc>1 ){
156652 assert( pPk->nKeyCol==1 );
156653 iPk = pPk->aiColumn[0];
156655 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
156662 for(i=0; i<pTab->nCol; i++){
156665 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
156669 if( pRowExpr ) pRowExpr->op2 = OPFLAG_NOCHNG;
156678 regRec = ++pParse->nMem;
156679 regRowid = ++pParse->nMem;
156688 for(i=0; i<pTab->nCol; i++){
156689 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
156691 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
156709 assert( pPk->nKeyCol==1 );
156710 iPk = pPk->aiColumn[0];
156721 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
156732 ** accept no-change records with serial_type 10 */
156743 if( pSrc->nSrc==1 ){
156776 ** 2018-04-12
156797 Upsert *pNext = p->pNextUpsert;
156798 sqlite3ExprListDelete(db, p->pUpsertTarget);
156799 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
156800 sqlite3ExprListDelete(db, p->pUpsertSet);
156801 sqlite3ExprDelete(db, p->pUpsertWhere);
156802 sqlite3DbFree(db, p->pToFree);
156818 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
156819 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
156820 sqlite3ExprListDup(db, p->pUpsertSet, 0),
156821 sqlite3ExprDup(db, p->pUpsertWhere, 0),
156822 sqlite3UpsertDup(db, p->pNextUpsert)
156847 pNew->pUpsertTarget = pTarget;
156848 pNew->pUpsertTargetWhere = pTargetWhere;
156849 pNew->pUpsertSet = pSet;
156850 pNew->pUpsertWhere = pWhere;
156851 pNew->isDoUpdate = pSet!=0;
156852 pNew->pNextUpsert = pNext;
156859 ** symbols in the conflict-target.
156874 ExprList *pTarget; /* The conflict-target clause */
156875 Expr *pTerm; /* One term of the conflict-target clause */
156880 assert( pTabList->nSrc==1 );
156881 assert( pTabList->a[0].pSTab!=0 );
156883 assert( pUpsert->pUpsertTarget!=0 );
156885 /* Resolve all symbolic names in the conflict-target clause, which
156886 ** includes both the list of columns and the optional partial-index
156892 for(; pUpsert && pUpsert->pUpsertTarget;
156893 pUpsert=pUpsert->pNextUpsert, nClause++){
156894 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
156896 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
156900 pTab = pTabList->a[0].pSTab;
156901 pTarget = pUpsert->pUpsertTarget;
156902 iCursor = pTabList->a[0].iCursor;
156904 && pTarget->nExpr==1
156905 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
156906 && pTerm->iColumn==XN_ROWID
156908 /* The conflict-target is the rowid of the primary table */
156909 assert( pUpsert->pUpsertIdx==0 );
156917 ** prior to comparing against the conflict-target expression.
156923 sCol[1].iTable = pTabList->a[0].iCursor;
156926 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
156929 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
156930 if( pIdx->pPartIdxWhere ){
156931 if( pUpsert->pUpsertTargetWhere==0 ) continue;
156932 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
156933 pIdx->pPartIdxWhere, iCursor)!=0 ){
156937 nn = pIdx->nKeyCol;
156940 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
156941 if( pIdx->aiColumn[ii]==XN_EXPR ){
156942 assert( pIdx->aColExpr!=0 );
156943 assert( pIdx->aColExpr->nExpr>ii );
156944 assert( pIdx->bHasExpr );
156945 pExpr = pIdx->aColExpr->a[ii].pExpr;
156946 if( pExpr->op!=TK_COLLATE ){
156952 sCol[1].iColumn = pIdx->aiColumn[ii];
156956 if( sqlite3ExprCompare(0,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
156970 pUpsert->pUpsertIdx = pIdx;
156974 ** after multi-CONFLICT upsert was added, and so we silently ignore
156977 pUpsert->isDup = 1;
156981 if( pUpsert->pUpsertIdx==0 ){
156983 if( nClause==0 && pUpsert->pNextUpsert==0 ){
157004 pNext = pUpsert->pNextUpsert;
157005 while( 1 /*exit-by-return*/ ){
157007 if( pNext->pUpsertTarget==0 ) return 1;
157008 if( pNext->pUpsertIdx==0 ) return 1;
157009 if( !pNext->isDup ) return 0;
157010 pNext = pNext->pNextUpsert;
157024 && pUpsert->pUpsertTarget!=0
157025 && pUpsert->pUpsertIdx!=pIdx
157027 pUpsert = pUpsert->pNextUpsert;
157036 ** In this case parameter iCur is a cursor open on the table b-tree that
157042 Parse *pParse, /* The parsing and code-generating context */
157048 Vdbe *v = pParse->pVdbe;
157049 sqlite3 *db = pParse->db;
157057 iDataCur = pUpsert->iDataCur;
157069 int nPk = pPk->nKeyCol;
157070 int iPk = pParse->nMem+1;
157071 pParse->nMem += nPk;
157074 assert( pPk->aiColumn[i]>=0 );
157075 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
157077 VdbeComment((v, "%s.%s", pIdx->zName,
157078 pTab->aCol[pPk->aiColumn[i]].zCnName));
157089 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
157091 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
157093 for(i=0; i<pTab->nCol; i++){
157094 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
157095 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
157098 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
157099 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
157143 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
157215 if( pParse->nErr ) goto build_vacuum_end;
157224 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
157225 ** occurred on 2016-08-19 (https://sqlite.org/src/info/083f9e6270).
157228 iDb = sqlite3FindDb(pParse->db, pNm);
157235 iIntoReg = ++pParse->nMem;
157242 sqlite3ExprDelete(pParse->db, pInto);
157258 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
157259 u64 saved_flags; /* Saved value of db->flags */
157260 i64 saved_nChange; /* Saved value of db->nChange */
157261 i64 saved_nTotalChange; /* Saved value of db->nTotalChange */
157262 u32 saved_openFlags; /* Saved value of db->openFlags */
157272 char zDbVacuum[42]; /* Name of the ATTACH-ed database used for vacuum */
157275 if( !db->autoCommit ){
157277 return SQLITE_ERROR; /* IMP: R-12218-18073 */
157279 if( db->nVdbeActive>1 ){
157280 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
157281 return SQLITE_ERROR; /* IMP: R-15610-35227 */
157283 saved_openFlags = db->openFlags;
157286 sqlite3SetString(pzErrMsg, db, "non-text filename");
157290 db->openFlags &= ~SQLITE_OPEN_READONLY;
157291 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
157297 ** restored before returning. Then set the writable-schema flag, and
157299 saved_flags = db->flags;
157300 saved_mDbFlags = db->mDbFlags;
157301 saved_nChange = db->nChange;
157302 saved_nTotalChange = db->nTotalChange;
157303 saved_mTrace = db->mTrace;
157304 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_Comments;
157305 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
157306 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
157308 db->mTrace = 0;
157310 zDbMain = db->aDb[iDb].zDbSName;
157311 pMain = db->aDb[iDb].pBt;
157320 ** An optimization would be to use a non-journaled pager.
157330 nDb = db->nDb;
157332 db->openFlags = saved_openFlags;
157334 assert( (db->nDb-1)==nDb );
157335 pDb = &db->aDb[nDb];
157336 assert( strcmp(pDb->zDbSName,zDbVacuum)==0 );
157337 pTemp = pDb->pBt;
157341 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
157346 db->mDbFlags |= DBFLAG_VacuumInto;
157348 /* For a VACUUM INTO, the pager-flags are set to the same values as
157351 pgflags = db->aDb[iDb].safety_level | (db->flags & PAGER_FLAGS_MASK);
157355 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
157361 ** to ensure that we do not try to change the page-size on a WAL database.
157373 db->nextPagesize = 0;
157377 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
157378 || NEVER(db->mallocFailed)
157385 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
157392 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
157406 db->init.iDb = 0;
157419 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
157420 db->mDbFlags &= ~DBFLAG_Vacuum;
157439 ** both transactions are closed by this block - the main database
157493 /* Restore the original value of db->flags */
157494 db->init.iDb = 0;
157495 db->mDbFlags = saved_mDbFlags;
157496 db->flags = saved_flags;
157497 db->nChange = saved_nChange;
157498 db->nTotalChange = saved_nTotalChange;
157499 db->mTrace = saved_mTrace;
157500 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
157509 db->autoCommit = 1;
157512 sqlite3BtreeClose(pDb->pBt);
157513 pDb->pBt = 0;
157514 pDb->pSchema = 0;
157517 /* This both clears the schemas and reduces the size of the db->aDb[]
157588 pMod->zName = zCopy;
157589 pMod->pModule = pModule;
157590 pMod->pAux = pAux;
157591 pMod->xDestroy = xDestroy;
157592 pMod->pEpoTab = 0;
157593 pMod->nRefModule = 1;
157595 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
157623 sqlite3_mutex_enter(db->mutex);
157627 sqlite3_mutex_leave(db->mutex);
157633 ** External API function used to create a new virtual-table module.
157648 ** External API function used to create a new virtual-table module.
157664 ** External API to drop all virtual-table modules, except those named
157672 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
157677 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
157680 createModule(db, pMod->zName, 0, 0, 0);
157690 assert( pMod->nRefModule>0 );
157691 pMod->nRefModule--;
157692 if( pMod->nRefModule==0 ){
157693 if( pMod->xDestroy ){
157694 pMod->xDestroy(pMod->pAux);
157696 assert( pMod->pEpoTab==0 );
157710 pVTab->nRef++;
157715 ** pTab is a pointer to a Table structure representing a virtual-table.
157717 ** this virtual-table, if one has been created, or NULL otherwise.
157722 for(pVtab=pTab->u.vtab.p; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
157727 ** Decrement the ref-count on a virtual table object. When the ref-count
157731 sqlite3 *db = pVTab->db;
157734 assert( pVTab->nRef>0 );
157735 assert( db->eOpenState==SQLITE_STATE_OPEN
157736 || db->eOpenState==SQLITE_STATE_ZOMBIE );
157738 pVTab->nRef--;
157739 if( pVTab->nRef==0 ){
157740 sqlite3_vtab *p = pVTab->pVtab;
157742 p->pModule->xDisconnect(p);
157744 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
157751 ** p->u.vtab.p list to the sqlite3.pDisconnect lists of their associated
157754 ** connection db is left in the p->u.vtab.p list.
157761 pVTable = p->u.vtab.p;
157762 p->u.vtab.p = 0;
157768 ** database connection that may have an entry in the p->u.vtab.p list.
157770 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
157773 sqlite3 *db2 = pVTable->db;
157774 VTable *pNext = pVTable->pNext;
157778 p->u.vtab.p = pRet;
157779 pRet->pNext = 0;
157781 pVTable->pNext = db2->pDisconnect;
157782 db2->pDisconnect = pVTable;
157794 ** list in p->pVTab. It also decrements the VTable ref count. This is
157797 ** be being used by other shared-cache connections).
157804 assert( sqlite3_mutex_held(db->mutex) );
157806 for(ppVTab=&p->u.vtab.p; *ppVTab; ppVTab=&(*ppVTab)->pNext){
157807 if( (*ppVTab)->db==db ){
157809 *ppVTab = pVTab->pNext;
157821 ** shared b-tree databases opened using connection db are held by the
157831 ** or, if the virtual table is stored in a non-sharable database, then
157835 ** by multiple threads. It is thread-safe.
157838 VTable *p = db->pDisconnect;
157841 assert( sqlite3_mutex_held(db->mutex) );
157844 db->pDisconnect = 0;
157846 VTable *pNext = p->pNext;
157854 ** Clear any and all virtual-table information from the Table record.
157858 ** Since it is a virtual-table, the Table structure contains a pointer
157870 if( db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
157871 if( p->u.vtab.azArg ){
157873 for(i=0; i<p->u.vtab.nArg; i++){
157874 if( i!=1 ) sqlite3DbFree(db, p->u.vtab.azArg[i]);
157876 sqlite3DbFree(db, p->u.vtab.azArg);
157881 ** Add a new module argument to pTable->u.vtab.azArg[].
157882 ** The string is not copied - the pointer is stored. The
157889 sqlite3 *db = pParse->db;
157892 nBytes = sizeof(char *)*(2+pTable->u.vtab.nArg);
157893 if( pTable->u.vtab.nArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
157894 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
157896 azModuleArg = sqlite3DbRealloc(db, pTable->u.vtab.azArg, nBytes);
157900 int i = pTable->u.vtab.nArg++;
157903 pTable->u.vtab.azArg = azModuleArg;
157923 pTable = pParse->pNewTable;
157925 assert( 0==pTable->pIndex );
157926 pTable->eTabType = TABTYP_VTAB;
157928 db = pParse->db;
157930 assert( pTable->u.vtab.nArg==0 );
157933 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
157934 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
157935 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
157937 pParse->sNameToken.n = (int)(
157938 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
157947 if( pTable->u.vtab.azArg ){
157948 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
157950 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
157951 pTable->u.vtab.azArg[0], pParse->db->aDb[iDb].zDbSName);
157958 ** in pParse->zArg[] and appends it to the list of arguments on the
157959 ** virtual table currently under construction in pParse->pTable.
157962 if( pParse->sArg.z && pParse->pNewTable ){
157963 const char *z = (const char*)pParse->sArg.z;
157964 int n = pParse->sArg.n;
157965 sqlite3 *db = pParse->db;
157966 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
157975 Table *pTab = pParse->pNewTable; /* The table being constructed */
157976 sqlite3 *db = pParse->db; /* The database connection */
157981 pParse->sArg.z = 0;
157982 if( pTab->u.vtab.nArg<1 ) return;
157990 if( !db->init.busy ){
158001 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
158003 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
158009 ** The VM register number pParse->u1.cr.regRowid holds the rowid of an
158013 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
158014 assert( pParse->isCreate );
158019 db->aDb[iDb].zDbSName,
158020 pTab->zName,
158021 pTab->zName,
158023 pParse->u1.cr.regRowid
158029 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
158033 iReg = ++pParse->nMem;
158034 sqlite3VdbeLoadString(v, iReg, pTab->zName);
158037 /* If we are rereading the sqlite_schema table create the in-memory
158040 Schema *pSchema = pTab->pSchema;
158041 const char *zName = pTab->zName;
158044 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
158050 pParse->pNewTable = 0;
158060 pParse->sArg.z = 0;
158061 pParse->sArg.n = 0;
158069 Token *pArg = &pParse->sArg;
158070 if( pArg->z==0 ){
158071 pArg->z = p->z;
158072 pArg->n = p->n;
158074 assert(pArg->z <= p->z);
158075 pArg->n = (int)(&p->z[p->n] - pArg->z);
158095 int nArg = pTab->u.vtab.nArg;
158102 azArg = (const char *const*)pTab->u.vtab.azArg;
158104 /* Check that the virtual-table is not already being initialized */
158105 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
158106 if( pCtx->pTab==pTab ){
158108 "vtable constructor called recursively: %s", pTab->zName
158114 zModuleName = sqlite3DbStrDup(db, pTab->zName);
158125 pVTable->db = db;
158126 pVTable->pMod = pMod;
158127 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
158129 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
158130 pTab->u.vtab.azArg[1] = db->aDb[iDb].zDbSName;
158133 assert( &db->pVtabCtx );
158137 sCtx.pPrior = db->pVtabCtx;
158139 db->pVtabCtx = &sCtx;
158140 pTab->nTabRef++;
158141 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
158143 assert( pTab->nTabRef>1 || rc!=SQLITE_OK );
158145 db->pVtabCtx = sCtx.pPrior;
158157 }else if( ALWAYS(pVTable->pVtab) ){
158160 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
158161 pVTable->pVtab->pModule = pMod->pModule;
158162 pMod->nRefModule++;
158163 pVTable->nRef = 1;
158173 ** into the linked list headed by pTab->u.vtab.p. Then loop through the
158177 pVTable->pNext = pTab->u.vtab.p;
158178 pTab->u.vtab.p = pVTable;
158180 for(iCol=0; iCol<pTab->nCol; iCol++){
158181 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
158187 && (i==0 || zType[i-1]==' ')
158200 assert(zType[i-1]==' ');
158201 zType[i-1] = '\0';
158203 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
158204 pTab->tabFlags |= TF_HasHidden;
158207 pTab->tabFlags |= oooHidden;
158222 ** This call is a no-op if table pTab is not a virtual table.
158225 sqlite3 *db = pParse->db;
158237 zMod = pTab->u.vtab.azArg[0];
158238 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
158241 const char *zModule = pTab->u.vtab.azArg[0];
158246 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
158249 pParse->rc = rc;
158257 ** Grow the db->aVTrans[] array so that there is room for at least one
158258 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
158264 if( (db->nVTrans%ARRAY_INCR)==0 ){
158267 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
158268 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
158272 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
158273 db->aVTrans = aVTrans;
158285 db->aVTrans[db->nVTrans++] = pVTab;
158303 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
158304 assert( pTab && IsVirtual(pTab) && !pTab->u.vtab.p );
158307 zMod = pTab->u.vtab.azArg[0];
158308 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
158314 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
158318 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
158370 sqlite3_mutex_enter(db->mutex);
158371 pCtx = db->pVtabCtx;
158372 if( !pCtx || pCtx->bDeclared ){
158374 sqlite3_mutex_leave(db->mutex);
158378 pTab = pCtx->pTab;
158385 ** schema. Nevertheless, defend against that (turn off db->init.busy)
158387 assert( db->init.busy==0 );
158388 initBusy = db->init.busy;
158389 db->init.busy = 0;
158393 assert( !db->mallocFailed );
158396 if( !pTab->aCol ){
158399 pTab->aCol = pNew->aCol;
158401 sqlite3ExprListDelete(db, pNew->u.tab.pDfltList);
158402 pTab->nNVCol = pTab->nCol = pNew->nCol;
158403 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
158404 pNew->nCol = 0;
158405 pNew->aCol = 0;
158406 assert( pTab->pIndex==0 );
158409 && pCtx->pVTable->pMod->pModule->xUpdate!=0
158410 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
158412 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
158413 ** or else must have a single-column PRIMARY KEY */
158416 pIdx = pNew->pIndex;
158418 assert( pIdx->pNext==0 );
158419 pTab->pIndex = pIdx;
158420 pNew->pIndex = 0;
158421 pIdx->pTable = pTab;
158424 pCtx->bDeclared = 1;
158438 db->init.busy = initBusy;
158442 sqlite3_mutex_leave(db->mutex);
158451 ** This call is a no-op if zTab is not a virtual table.
158457 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
158460 && ALWAYS(pTab->u.vtab.p!=0)
158464 for(p=pTab->u.vtab.p; p; p=p->pNext){
158465 assert( p->pVtab );
158466 if( p->pVtab->nRef>0 ){
158471 xDestroy = p->pMod->pModule->xDestroy;
158472 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
158474 pTab->nTabRef++;
158475 rc = xDestroy(p->pVtab);
158478 assert( pTab->u.vtab.p==p && p->pNext==0 );
158479 p->pVtab = 0;
158480 pTab->u.vtab.p = 0;
158499 if( db->aVTrans ){
158500 VTable **aVTrans = db->aVTrans;
158501 db->aVTrans = 0;
158502 for(i=0; i<db->nVTrans; i++){
158504 sqlite3_vtab *p = pVTab->pVtab;
158507 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
158510 pVTab->iSavepoint = 0;
158514 db->nVTrans = 0;
158523 ** If an error message is available, leave it in p->zErrMsg.
158528 VTable **aVTrans = db->aVTrans;
158530 db->aVTrans = 0;
158531 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
158533 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
158534 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
158539 db->aVTrans = aVTrans;
158573 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
158584 pModule = pVTab->pVtab->pModule;
158586 if( pModule->xBegin ){
158590 for(i=0; i<db->nVTrans; i++){
158591 if( db->aVTrans[i]==pVTab ){
158600 rc = pModule->xBegin(pVTab->pVtab);
158602 int iSvpt = db->nStatement + db->nSavepoint;
158604 if( iSvpt && pModule->xSavepoint ){
158605 pVTab->iSavepoint = iSvpt;
158606 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
158633 assert( iSavepoint>=-1 );
158634 if( db->aVTrans ){
158636 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
158637 VTable *pVTab = db->aVTrans[i];
158638 const sqlite3_module *pMod = pVTab->pMod->pModule;
158639 if( pVTab->pVtab && pMod->iVersion>=2 ){
158644 xMethod = pMod->xSavepoint;
158645 pVTab->iSavepoint = iSavepoint+1;
158648 xMethod = pMod->xRollbackTo;
158651 xMethod = pMod->xRelease;
158654 if( xMethod && pVTab->iSavepoint>iSavepoint ){
158655 u64 savedFlags = (db->flags & SQLITE_Defensive);
158656 db->flags &= ~(u64)SQLITE_Defensive;
158657 rc = xMethod(pVTab->pVtab, iSavepoint);
158658 db->flags |= savedFlags;
158696 if( pExpr->op!=TK_COLUMN ) return pDef;
158698 pTab = pExpr->y.pTab;
158701 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
158703 assert( pVtab->pModule!=0 );
158704 pMod = (sqlite3_module *)pVtab->pModule;
158705 if( pMod->xFindFunction==0 ) return pDef;
158711 ** with an all lower-case function name. Continue in this tradition to
158717 for(i=0; pDef->zName[i]; i++){
158718 unsigned char x = (unsigned char)pDef->zName[i];
158723 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
158731 + sqlite3Strlen30(pDef->zName) + 1);
158736 pNew->zName = (const char*)&pNew[1];
158737 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
158738 pNew->xSFunc = xSFunc;
158739 pNew->pUserData = pArg;
158740 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
158745 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
158748 ** is a no-op.
158756 for(i=0; i<pToplevel->nVtabLock; i++){
158757 if( pTab==pToplevel->apVtabLock[i] ) return;
158759 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
158760 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
158762 pToplevel->apVtabLock = apVtabLock;
158763 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
158765 sqlite3OomFault(pToplevel->db);
158772 ** exist. Return non-zero if either the eponymous virtual table instance
158779 ** instances always exist. They cannot be DROP-ed.
158785 const sqlite3_module *pModule = pMod->pModule;
158789 sqlite3 *db = pParse->db;
158790 if( pMod->pEpoTab ) return 1;
158791 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
158794 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
158795 if( pTab->zName==0 ){
158799 pMod->pEpoTab = pTab;
158800 pTab->nTabRef = 1;
158801 pTab->eTabType = TABTYP_VTAB;
158802 pTab->pSchema = db->aDb[0].pSchema;
158803 assert( pTab->u.vtab.nArg==0 );
158804 pTab->iPKey = -1;
158805 pTab->tabFlags |= TF_Eponymous;
158806 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
158808 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
158809 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
158823 Table *pTab = pMod->pEpoTab;
158828 pTab->tabFlags |= TF_Ephemeral;
158830 pMod->pEpoTab = 0;
158850 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
158851 return (int)aMap[db->vtabOnConflict-1];
158867 sqlite3_mutex_enter(db->mutex);
158868 p = db->pVtabCtx;
158872 assert( p->pTab==0 || IsVirtual(p->pTab) );
158876 p->pVTable->bConstraint = (u8)va_arg(ap, int);
158880 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
158884 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
158888 p->pVTable->bAllSchemas = 1;
158900 sqlite3_mutex_leave(db->mutex);
158909 ** 2015-06-06
158922 ** This file was split off from where.c on 2015-06-06 in order to reduce the
158931 ** 2013-11-12
159000 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
159008 int addrSkip; /* Jump here for next iteration of skip-scan */
159012 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
159013 int addrBignull; /* Jump here for next part of big-null scan */
159023 union { /* Information that depends on pWLoop->wsFlags */
159029 int iBase; /* Base register of multi-key index record */
159033 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
159047 ** prevent a query solution - which is an error) and many terms of the
159049 ** potential way of implementing that FROM-clause term, together with
159065 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
159095 LogEst rStarDelta; /* Cost delta due to star-schema heuristic. Not
159096 ** initialized unless pWInfo->bStarUsed */
159114 ** correspond to the subquery(s) of OR-clause processing. Only the
159130 ** vector, not a scalar, and because dependencies are many-to-one, not
159131 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
159135 ** The "solver" works by creating the N best WherePath objects of length
159137 ** of length 2. And so forth until the length of WherePaths equals the
159147 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
159160 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
159191 ** spread out over the non-negative integers. For example, the cursor
159210 int iParent; /* Disable pWC->a[iParent] when this term disabled */
159220 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
159233 #define TERM_OK 0x0040 /* Used during OR-clause processing */
159239 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
159246 #define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
159257 int k; /* Resume scanning at this->pWC->a[this->k] */
159263 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
159285 int nBase; /* Number of terms through the last non-Virtual */
159323 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
159324 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
159331 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
159332 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
159371 ** routine. The limit is high enough that is should not impact real-world
159409 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
159418 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
159420 unsigned bStarDone :1; /* True if check for star-query is complete */
159421 unsigned bStarUsed :1; /* True if star-query heuristic is used */
159442 ** Private interfaces - callable only by other where.c routines.
159502 int iLevel, /* Which level of pWInfo->a[] should be coded */
159530 ** OR-ed combination of these values can be used when searching for
159542 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
159543 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
159544 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
159545 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
159549 #define WO_OR 0x0200 /* Two or more OR-connected terms */
159550 #define WO_AND 0x0400 /* Two or more AND-connected terms */
159553 #define WO_ROWVAL 0x2000 /* A row-value term */
159556 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
159571 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
159579 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
159584 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
159586 #define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
159589 #define WHERE_COROUTINE 0x02000000 /* Implemented by co-routine.
159590 ** NB: False-negatives are possible */
159591 #define WHERE_EXPRIDX 0x04000000 /* Uses an index-on-expressions */
159601 ** Return the name of the i-th column of the pIdx index.
159604 i = pIdx->aiColumn[i];
159607 return pIdx->pTable->aCol[i].zCnName;
159622 int iTerm, /* Zero-based index of first term. */
159623 int bAnd, /* Non-zero to append " AND " */
159663 Index *pIndex = pLoop->u.btree.pIndex;
159664 u16 nEq = pLoop->u.btree.nEq;
159665 u16 nSkip = pLoop->nSkip;
159668 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
159677 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
159678 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
159681 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
159682 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
159700 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
159703 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr);
159705 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
159706 sqlite3 *db = pParse->db; /* Database handle */
159716 if( db->mallocFailed ) return;
159718 pLoop = pLevel->pWLoop;
159719 flags = pLoop->wsFlags;
159722 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
159732 assert( pLoop->u.btree.pIndex!=0 );
159733 pIdx = pLoop->u.btree.pIndex;
159735 if( !HasRowid(pItem->pSTab) && IsPrimaryKeyIndex(pIdx) ){
159750 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
159756 const Table *pTab = pItem->pTab;
159757 const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName:
159780 pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s",
159781 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
159784 if( pItem->fg.jointype & JT_LEFT ){
159785 sqlite3_str_appendf(&str, " LEFT-JOIN");
159788 if( pLoop->nOut>=10 ){
159790 sqlite3LogEstToInt(pLoop->nOut));
159800 assert( pOp->opcode==OP_Explain );
159801 assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 );
159802 sqlite3DbFree(db, pOp->p4.z);
159803 pOp->p4type = P4_DYNAMIC;
159804 pOp->p4.z = sqlite3StrAccumFinish(&str);
159810 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
159812 ** was defined at compile-time. If it is not a no-op, a single OP_Explain
159826 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
159829 if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0
159832 Vdbe *v = pParse->pVdbe;
159835 v, OP_Explain, addr, pParse->addrExplain, pLevel->pWLoop->rRun
159848 ** required and this routine is a no-op.
159859 SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom];
159860 Vdbe *v = pParse->pVdbe; /* VM being constructed */
159861 sqlite3 *db = pParse->db; /* Database handle */
159871 pLoop = pLevel->pWLoop;
159872 if( pLoop->wsFlags & WHERE_IPK ){
159873 const Table *pTab = pItem->pSTab;
159874 if( pTab->iPKey>=0 ){
159875 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
159880 for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){
159881 const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i);
159882 if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5);
159889 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
159891 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
159914 WhereLoop *pLoop = pLvl->pWLoop;
159915 int wsFlags = pLoop->wsFlags;
159918 if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
159919 zObj = pLoop->u.btree.pIndex->zName;
159921 zObj = pSrclist->a[pLvl->iFrom].zName;
159922 viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine;
159925 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
159930 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur);
159933 sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur);
159938 assert( pSrclist->a[pLvl->iFrom].fg.isSubquery );
159939 addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
159940 pOp = sqlite3VdbeGetOp(v, addr-1);
159941 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
159942 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
159943 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
159990 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
159995 while( (pTerm->wtFlags & TERM_CODED)==0
159996 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
159997 && (pLevel->notReady & pTerm->prereqAll)==0
159999 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
160000 pTerm->wtFlags |= TERM_LIKECOND;
160002 pTerm->wtFlags |= TERM_CODED;
160006 sqlite3DebugPrintf("DISABLE-");
160007 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
160010 if( pTerm->iParent<0 ) break;
160011 pTerm = &pTerm->pWC->a[pTerm->iParent];
160013 pTerm->nChild--;
160014 if( pTerm->nChild!=0 ) break;
160024 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
160031 Vdbe *v = pParse->pVdbe;
160033 assert( pParse->db->mallocFailed );
160043 n--;
160047 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
160048 n--;
160084 ** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because
160088 ** pEList is the new result set. The pEList->a[].u.x.iOrderByCol values
160094 ** This routine changes pOrderBy->a[].u.x.iOrderByCol values from
160095 ** pEList->a[N].u.x.iOrderByCol into N+1. (The "+1" is because of the 1-based
160101 for(i=0; i<pOrderBy->nExpr; i++){
160102 int t = pOrderBy->a[i].u.x.iOrderByCol;
160104 for(j=0; j<pEList->nExpr; j++){
160105 if( pEList->a[j].u.x.iOrderByCol==t ){
160106 pOrderBy->a[i].u.x.iOrderByCol = j+1;
160110 if( j>=pEList->nExpr ){
160111 pOrderBy->a[i].u.x.iOrderByCol = 0;
160152 sqlite3 *db = pParse->db;
160156 if( db->mallocFailed==0 ){
160157 for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){
160165 pOrigRhs = pSelect->pEList;
160166 assert( pNew->pLeft!=0 );
160167 assert( ExprUseXList(pNew->pLeft) );
160168 if( pSelect==pNew->x.pSelect ){
160169 pOrigLhs = pNew->pLeft->x.pList;
160171 for(i=iEq; i<pLoop->nLTerm; i++){
160172 if( pLoop->aLTerm[i]->pExpr==pX ){
160174 assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 );
160175 iField = pLoop->aLTerm[i]->u.x.iField - 1;
160176 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
160177 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
160178 pOrigRhs->a[iField].pExpr = 0;
160179 if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1;
160181 assert( pOrigLhs->a[iField].pExpr!=0 );
160182 pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr);
160183 pOrigLhs->a[iField].pExpr = 0;
160190 pNew->pLeft->x.pList = pLhs;
160192 pSelect->pEList = pRhs;
160193 pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */
160194 if( pLhs && pLhs->nExpr==1 ){
160198 Expr *p = pLhs->a[0].pExpr;
160199 pLhs->a[0].pExpr = 0;
160200 sqlite3ExprDelete(db, pNew->pLeft);
160201 pNew->pLeft = p;
160205 ** references to result-set columns, those references might now be
160208 assert( pRhs!=0 || db->mallocFailed );
160210 adjustOrderByCol(pSelect->pOrderBy, pRhs);
160211 adjustOrderByCol(pSelect->pGroupBy, pRhs);
160212 for(i=0; i<pRhs->nExpr; i++) pRhs->a[i].u.x.iOrderByCol = 0;
160231 ** This is a special-case of codeEqualityTerm() that works for IN operators
160244 int bRev, /* True for reverse-order IN operations */
160247 Expr *pX = pTerm->pExpr;
160251 WhereLoop *pLoop = pLevel->pWLoop;
160252 Vdbe *v = pParse->pVdbe;
160257 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
160258 && pLoop->u.btree.pIndex!=0
160259 && pLoop->u.btree.pIndex->aSortOrder[iEq]
160265 assert( pX->op==TK_IN );
160268 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
160273 for(i=iEq;i<pLoop->nLTerm; i++){
160274 assert( pLoop->aLTerm[i]!=0 );
160275 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
160279 if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){
160282 Expr *pExpr = pTerm->pExpr;
160283 if( pExpr->iTable==0 || !ExprHasProperty(pExpr, EP_Subrtn) ){
160284 sqlite3 *db = pParse->db;
160286 if( !db->mallocFailed ){
160287 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
160289 pExpr->iTable = iTab;
160293 int n = sqlite3ExprVectorSize(pX->pLeft);
160294 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*MAX(nEq,n));
160308 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
160309 pLoop->wsFlags |= WHERE_IN_ABLE;
160310 if( pLevel->u.in.nIn==0 ){
160311 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
160313 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
160314 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
160317 i = pLevel->u.in.nIn;
160318 pLevel->u.in.nIn += nEq;
160319 pLevel->u.in.aInLoop =
160320 sqlite3WhereRealloc(pTerm->pWC->pWInfo,
160321 pLevel->u.in.aInLoop,
160322 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
160323 pIn = pLevel->u.in.aInLoop;
160327 for(i=iEq;i<pLoop->nLTerm; i++){
160328 if( pLoop->aLTerm[i]->pExpr==pX ){
160329 int iOut = iTarget + i - iEq;
160331 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
160334 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
160338 pIn->iCur = iTab;
160339 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
160341 pIn->iBase = iTarget - i;
160342 pIn->nPrefix = i;
160344 pIn->nPrefix = 0;
160347 pIn->eEndLoopOp = OP_Noop;
160353 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
160354 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
160356 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
160358 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
160361 pLevel->u.in.nIn = 0;
160363 sqlite3DbFree(pParse->db, aiMap);
160380 ** straight-line code. For constraints of the form X IN (...)
160388 int bRev, /* True for reverse-order IN operations */
160391 Expr *pX = pTerm->pExpr;
160394 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
160396 if( pX->op==TK_EQ || pX->op==TK_IS ){
160397 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
160398 }else if( pX->op==TK_ISNULL ){
160400 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null, 0, iReg);
160403 assert( pX->op==TK_IN );
160416 ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04)
160418 if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0
160419 || (pTerm->eOperator & WO_EQUIV)==0
160440 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
160441 ** The only thing it does is allocate the pLevel->iMem memory cell and
160480 u16 nSkip; /* Number of left-most columns to skip */
160481 Vdbe *v = pParse->pVdbe; /* The vm under construction */
160491 pLoop = pLevel->pWLoop;
160492 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
160493 nEq = pLoop->u.btree.nEq;
160494 nSkip = pLoop->nSkip;
160495 pIdx = pLoop->u.btree.pIndex;
160500 regBase = pParse->nMem + 1;
160502 pParse->nMem += nReg;
160504 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
160505 assert( zAff!=0 || pParse->db->mallocFailed );
160508 int iIdxCur = pLevel->iIdxCur;
160509 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
160513 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
160515 assert( pLevel->addrSkip==0 );
160516 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
160523 testcase( pIdx->aiColumn[j]==XN_EXPR );
160533 pTerm = pLoop->aLTerm[j];
160537 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
160538 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160548 if( pTerm->eOperator & WO_IN ){
160549 if( pTerm->pExpr->flags & EP_xIsSelect ){
160556 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
160557 Expr *pRight = pTerm->pExpr->pRight;
160558 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
160559 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
160562 if( pParse->nErr==0 ){
160563 assert( pParse->db->mallocFailed==0 );
160593 ** becomes a no-op.
160600 if( pTerm->wtFlags & TERM_LIKEOPT ){
160602 assert( pLevel->iLikeRepCntr>0 );
160605 assert( pOp->opcode==OP_String8
160606 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
160607 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
160608 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
160631 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
160634 struct CCurHint *pHint = pWalker->u.pCCurHint;
160635 assert( pHint->pIdx!=0 );
160636 if( pExpr->op==TK_COLUMN
160637 && pExpr->iTable==pHint->iTabCur
160638 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
160640 pWalker->eCode = 1;
160647 ** should be included in the cursor-hint for a table that is on the rhs
160648 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
160661 if( pExpr->op==TK_IS
160662 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
160663 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
160665 pWalker->eCode = 1;
160666 }else if( pExpr->op==TK_FUNCTION ){
160669 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
160670 pWalker->eCode = 1;
160698 struct CCurHint *pHint = pWalker->u.pCCurHint;
160699 if( pExpr->op==TK_COLUMN ){
160700 if( pExpr->iTable!=pHint->iTabCur ){
160701 reg = ++pWalker->pParse->nMem; /* Register for column value */
160702 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
160703 pExpr->op = TK_REGISTER;
160704 pExpr->iTable = reg;
160705 }else if( pHint->pIdx!=0 ){
160706 pExpr->iTable = pHint->iIdxCur;
160707 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
160708 assert( pExpr->iColumn>=0 );
160710 }else if( pExpr->pAggInfo ){
160712 reg = ++pWalker->pParse->nMem; /* Register for column value */
160713 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
160714 pExpr->op = TK_REGISTER;
160715 pExpr->iTable = reg;
160716 }else if( pExpr->op==TK_TRUEFALSE ){
160717 /* Do not walk disabled expressions. tag-20230504-1 */
160730 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
160732 Parse *pParse = pWInfo->pParse;
160733 sqlite3 *db = pParse->db;
160734 Vdbe *v = pParse->pVdbe;
160736 WhereLoop *pLoop = pLevel->pWLoop;
160745 iCur = pLevel->iTabCur;
160746 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
160748 sHint.iIdxCur = pLevel->iIdxCur;
160749 sHint.pIdx = pLoop->u.btree.pIndex;
160753 pWC = &pWInfo->sWC;
160754 for(i=0; i<pWC->nBase; i++){
160755 pTerm = &pWC->a[i];
160756 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
160757 if( pTerm->prereqAll & pLevel->notReady ) continue;
160761 ** from the cursor-hint.
160781 if( pTabItem->fg.jointype & JT_LEFT ){
160782 Expr *pExpr = pTerm->pExpr;
160784 || pExpr->w.iJoin!=pTabItem->iCursor
160788 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
160792 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
160795 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
160798 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
160799 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
160800 if( j<pLoop->nLTerm ) continue;
160803 /* No subqueries or non-deterministic functions allowed */
160804 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
160811 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
160816 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
160820 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
160827 # define codeCursorHint(A,B,C,D) /* No-op */
160831 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
160842 ** However, if the scan currently being coded is a branch of an OR-loop and
160859 int iCur, /* Cursor for IPK b-tree */
160862 Parse *pParse = pWInfo->pParse; /* Parse context */
160863 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
160866 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
160868 pWInfo->bDeferredSeek = 1;
160870 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
160871 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
160874 Table *pTab = pIdx->pTable;
160875 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
160877 ai[0] = pTab->nCol;
160878 for(i=0; i<pIdx->nColumn-1; i++){
160880 assert( pIdx->aiColumn[i]<pTab->nCol );
160881 x1 = pIdx->aiColumn[i];
160886 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
160905 Vdbe *v = pParse->pVdbe;
160907 assert( p->op==TK_SELECT );
160909 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
160916 pList = p->x.pList;
160917 assert( nReg<=pList->nExpr );
160919 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
160923 assert( nReg==1 || pParse->nErr );
160942 while( pTruth->op==TK_AND ){
160943 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
160944 pTruth = pTruth->pRight;
160946 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
160948 if( pTerm->wtFlags & TERM_CODED ) continue;
160949 pExpr = pTerm->pExpr;
160951 pTerm->wtFlags |= TERM_CODED;
160961 ** inner-loop Bloom filters, then evaluate those filters now, before the
160967 ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked,
160974 int iLevel, /* Which level of pWInfo->a[] should be coded */
160978 while( ++iLevel < pWInfo->nLevel ){
160979 WhereLevel *pLevel = &pWInfo->a[iLevel];
160980 WhereLoop *pLoop = pLevel->pWLoop;
160981 if( pLevel->regFilter==0 ) continue;
160982 if( pLevel->pWLoop->nSkip ) continue;
160983 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
160984 ** vvvvv--' pLevel->regFilter if this were true. */
160985 if( NEVER(pLoop->prereq & notReady) ) continue;
160986 assert( pLevel->addrBrk==0 );
160987 pLevel->addrBrk = addrNxt;
160988 if( pLoop->wsFlags & WHERE_IPK ){
160989 WhereTerm *pTerm = pLoop->aLTerm[0];
160992 assert( pTerm->pExpr!=0 );
160993 testcase( pTerm->wtFlags & TERM_VIRTUAL );
160996 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
160997 VdbeCoverage(pParse->pVdbe);
160998 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
161000 VdbeCoverage(pParse->pVdbe);
161002 u16 nEq = pLoop->u.btree.nEq;
161006 assert( pLoop->wsFlags & WHERE_INDEXED );
161007 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
161010 sqlite3DbFree(pParse->db, zStartAff);
161011 sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter,
161013 VdbeCoverage(pParse->pVdbe);
161015 pLevel->regFilter = 0;
161016 pLevel->addrBrk = 0;
161026 if( pLoop->u.btree.pIndex->onError
161027 && pLoop->nSkip==0
161028 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
161031 for(ii=0; ii<pLoop->u.btree.nEq; ii++){
161032 if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){
161042 ** Generate code for the start of the iLevel-th loop in the WHERE clause
161049 int iLevel, /* Which level of pWInfo->a[] should be coded */
161070 pWC = &pWInfo->sWC;
161071 db = pParse->db;
161072 pLoop = pLevel->pWLoop;
161073 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
161074 iCur = pTabItem->iCursor;
161075 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
161076 bRev = (pWInfo->revMask>>iLevel)&1;
161077 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",
161078 iLevel, pTabItem->pSTab->zName));
161082 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
161090 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
161092 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
161107 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
161108 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
161114 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
161115 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
161117 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
161118 pLevel->iLeftJoin = ++pParse->nMem;
161119 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
161125 for(j=iLevel; j>0; j--){
161126 if( pWInfo->a[j].iLeftJoin ) break;
161127 if( pWInfo->a[j].pRJ ) break;
161129 addrHalt = pWInfo->a[j].addrBrk;
161131 /* Special case of a FROM clause subquery implemented as a co-routine */
161132 if( pTabItem->fg.viaCoroutine ){
161135 assert( pTabItem->fg.isSubquery && pTabItem->u4.pSubq!=0 );
161136 pSubq = pTabItem->u4.pSubq;
161137 regYield = pSubq->regReturn;
161138 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSubq->addrFillSub);
161139 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
161141 VdbeComment((v, "next row of %s", pTabItem->pSTab->zName));
161142 pLevel->op = OP_Goto;
161146 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
161147 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
161152 int nConstraint = pLoop->nLTerm;
161155 addrNotFound = pLevel->addrBrk;
161158 pTerm = pLoop->aLTerm[j];
161160 if( pTerm->eOperator & WO_IN ){
161161 if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){
161162 int iTab = pParse->nTab++;
161163 int iCache = ++pParse->nMem;
161164 sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab);
161168 addrNotFound = pLevel->addrNxt;
161171 Expr *pRight = pTerm->pExpr->pRight;
161173 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
161174 && pLoop->u.vtab.bOmitOffset
161176 assert( pTerm->eOperator==WO_AUX );
161177 assert( pWInfo->pSelect!=0 );
161178 assert( pWInfo->pSelect->iOffset>0 );
161179 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
161184 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
161188 ** resolveP2() to work correctly. See tag-20250207a. */
161190 pLoop->u.vtab.idxStr,
161191 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
161193 pLoop->u.vtab.needFree = 0;
161195 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
161196 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
161197 pLevel->p1 = iCur;
161198 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
161199 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
161200 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
161203 pTerm = pLoop->aLTerm[j];
161204 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
161208 if( (pTerm->eOperator & WO_IN)!=0
161209 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
161210 && !db->mallocFailed
161215 int iIn; /* IN loop corresponding to the j-th constraint */
161222 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
161223 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
161224 if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2)
161225 || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2)
161227 testcase( pOp->opcode==OP_Rowid );
161228 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
161237 if( !db->mallocFailed ){
161238 int iFld = pTerm->u.x.iField;
161239 Expr *pLeft = pTerm->pExpr->pLeft;
161242 assert( pLeft->op==TK_VECTOR );
161244 assert( iFld<=pLeft->x.pList->nExpr );
161245 pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr;
161247 pCompare->pLeft = pLeft;
161249 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
161251 pRight->iTable = iReg+j+2;
161253 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
161256 pCompare->pLeft = 0;
161264 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
161272 if( (pLoop->wsFlags & WHERE_IPK)!=0
161273 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
161280 assert( pLoop->u.btree.nEq==1 );
161281 pTerm = pLoop->aLTerm[0];
161283 assert( pTerm->pExpr!=0 );
161284 testcase( pTerm->wtFlags & TERM_VIRTUAL );
161285 iReleaseReg = ++pParse->nMem;
161288 addrNxt = pLevel->addrNxt;
161289 if( pLevel->regFilter ){
161292 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
161299 pLevel->op = OP_Noop;
161300 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
161301 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
161312 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
161313 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
161339 assert( (pStart->wtFlags & TERM_VNULL)==0 );
161340 testcase( pStart->wtFlags & TERM_VIRTUAL );
161341 pX = pStart->pExpr;
161343 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
161344 if( sqlite3ExprIsVector(pX->pRight) ){
161346 codeExprOrVector(pParse, pX->pRight, r1, 1);
161347 testcase( pX->op==TK_GT );
161348 testcase( pX->op==TK_GE );
161349 testcase( pX->op==TK_LT );
161350 testcase( pX->op==TK_LE );
161351 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
161352 assert( pX->op!=TK_GT || op==OP_SeekGE );
161353 assert( pX->op!=TK_GE || op==OP_SeekGE );
161354 assert( pX->op!=TK_LT || op==OP_SeekLE );
161355 assert( pX->op!=TK_LE || op==OP_SeekLE );
161357 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
161359 op = aMoveOp[(pX->op - TK_GT)];
161363 VdbeCoverageIf(v, pX->op==TK_GT);
161364 VdbeCoverageIf(v, pX->op==TK_LE);
161365 VdbeCoverageIf(v, pX->op==TK_LT);
161366 VdbeCoverageIf(v, pX->op==TK_GE);
161375 pX = pEnd->pExpr;
161377 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
161378 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
161379 testcase( pEnd->wtFlags & TERM_VIRTUAL );
161380 memEndValue = ++pParse->nMem;
161381 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
161382 if( 0==sqlite3ExprIsVector(pX->pRight)
161383 && (pX->op==TK_LT || pX->op==TK_GT)
161389 if( 0==sqlite3ExprIsVector(pX->pRight) ){
161394 pLevel->op = bRev ? OP_Prev : OP_Next;
161395 pLevel->p1 = iCur;
161396 pLevel->p2 = start;
161397 assert( pLevel->p5==0 );
161399 iRowidReg = ++pParse->nMem;
161408 }else if( pLoop->wsFlags & WHERE_INDEXED ){
161413 ** left-most columns of the index. It may also contain
161416 ** the right-most column can be an inequality - the rest must
161456 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
161457 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
161458 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
161474 int regBignull = 0; /* big-null flag register */
161477 pIdx = pLoop->u.btree.pIndex;
161478 iIdxCur = pLevel->iIdxCur;
161479 assert( nEq>=pLoop->nSkip );
161485 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
161486 pRangeStart = pLoop->aLTerm[j++];
161487 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
161489 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
161490 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
161492 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
161493 pRangeEnd = pLoop->aLTerm[j++];
161494 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
161496 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
161498 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
161499 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
161500 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
161502 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
161506 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
161508 pLevel->iLikeRepCntr <<=1;
161509 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
161513 j = pIdx->aiColumn[nEq];
161514 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
161519 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
161522 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
161525 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
161528 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
161529 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
161533 testcase( pLoop->nSkip>0 );
161536 pLevel->regBignull = regBignull = ++pParse->nMem;
161537 if( pLevel->iLeftJoin ){
161540 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
161547 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
161553 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
161569 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
161571 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
161572 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
161573 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
161574 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
161575 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
161576 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
161582 Expr *pRight = pRangeStart->pExpr->pRight;
161585 if( (pRangeStart->wtFlags & TERM_VNULL)==0
161595 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
161612 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
161613 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
161614 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
161620 VdbeComment((v, "NULL-scan pass ctr"));
161622 if( pLevel->regFilter ){
161623 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
161631 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
161642 (pIdx->aiRowLogEst[0]+9)/10);
161667 nConstraint-startEq);
161681 assert( pLevel->p2==0 );
161683 Expr *pRight = pRangeEnd->pExpr->pRight;
161687 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
161697 assert( pParse->db->mallocFailed );
161700 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
161718 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
161723 /* Except, skip the end-of-range check while doing the NULL-scan */
161725 VdbeComment((v, "If NULL-scan 2nd pass"));
161737 /* During a NULL-scan, check to see if we have reached the end of
161743 VdbeComment((v, "If NULL-scan 1st pass"));
161754 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
161759 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
161760 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
161763 }else if( HasRowid(pIdx->pTable) ){
161766 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
161767 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
161768 for(j=0; j<pPk->nKeyCol; j++){
161769 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
161773 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
161776 if( pLevel->iLeftJoin==0 ){
161782 ** 2019-11-02 ticket 623eff57e76d45f6 (LEFT JOIN)
161783 ** 2025-05-29 forum post 7dee41d32506c4ae (RIGHT JOIN)
161785 if( pIdx->pPartIdxWhere && pLevel->pRJ==0 ){
161786 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
161789 testcase( pIdx->pPartIdxWhere );
161791 ** The OR-optimization doesn't work for the right hand table of
161793 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
161797 if( (pLoop->wsFlags & WHERE_ONEROW)
161798 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
161800 pLevel->op = OP_Noop;
161802 pLevel->op = OP_Prev;
161804 pLevel->op = OP_Next;
161806 pLevel->p1 = iIdxCur;
161807 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
161808 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
161809 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
161811 assert( pLevel->p5==0 );
161817 if( pLoop->wsFlags & WHERE_MULTI_OR ){
161856 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
161861 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
161862 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
161864 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
161866 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
161874 Table *pTab = pTabItem->pSTab;
161876 pTerm = pLoop->aLTerm[0];
161878 assert( pTerm->eOperator & WO_OR );
161879 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
161880 pOrWc = &pTerm->u.pOrInfo->wc;
161881 pLevel->op = OP_Return;
161882 pLevel->p1 = regReturn;
161888 if( pWInfo->nLevel>1 ){
161891 nNotReady = pWInfo->nLevel - iLevel - 1;
161894 pOrTab->nAlloc = (u8)(nNotReady + 1);
161895 pOrTab->nSrc = pOrTab->nAlloc;
161896 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
161897 origSrc = pWInfo->pTabList->a;
161899 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
161902 pOrTab = pWInfo->pTabList;
161913 ** correct response for the end-of-loop code (the OP_Return) is to
161917 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
161919 regRowset = ++pParse->nMem;
161923 regRowset = pParse->nTab++;
161924 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
161927 regRowid = ++pParse->nMem;
161937 ** the "interesting" terms of z - terms that did not originate in the
161945 ** 2022-02-04: Do not push down slices of a row-value comparison.
161947 ** the initialization of the right-hand operand of the vector comparison
161951 ** 2022-03-03: Do not push down expressions that involve subqueries.
161952 ** The subquery might get coded as a subroutine. Any table-references
161953 ** in the subquery might be resolved to index-references for the index on
161956 ** index-references will not work. tag-20220303a
161959 if( pWC->nTerm>1 ){
161961 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
161962 Expr *pExpr = pWC->a[iTerm].pExpr;
161963 if( &pWC->a[iTerm] == pTerm ) continue;
161964 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
161965 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
161966 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
161967 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
161970 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
161971 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
161979 ** prevents sqlite3PExpr() from applying the AND short-circuit
161987 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
161989 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
161990 for(ii=0; ii<pOrWc->nTerm; ii++){
161991 WhereTerm *pOrTerm = &pOrWc->a[ii];
161992 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
161993 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
161994 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
162001 if( db->mallocFailed ){
162006 pAndExpr->pLeft = pOrExpr;
162011 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
162014 assert( pSubWInfo || pParse->nErr );
162018 pParse, pOrTab, &pSubWInfo->a[0], 0
162020 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
162022 /* This is the sub-WHERE clause body. First skip over
162023 ** duplicate rows from prior sub-WHERE clauses, and record the
162025 ** row will be skipped in subsequent sub-WHERE clauses.
162027 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
162028 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
162030 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
162036 int nPk = pPk->nKeyCol;
162043 int iCol = pPk->aiColumn[iPk];
162055 ** the temp table. And if iSet is -1, assume that there is no
162078 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
162081 /* The pSubWInfo->untestedTerms flag means that this OR term
162086 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
162088 /* If all of the OR-connected terms are optimized using the same
162094 ** uses an index, and this is either the first OR-connected term
162100 pSubLoop = pSubWInfo->a[0].pWLoop;
162101 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
162102 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
162103 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
162104 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
162106 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
162107 pCov = pSubLoop->u.btree.pIndex;
162112 pWInfo->bDeferredSeek = 1;
162123 assert( pLevel->pWLoop==pLoop );
162124 assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 );
162125 assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 );
162126 pLevel->u.pCoveringIdx = pCov;
162127 if( pCov ) pLevel->iIdxCur = iCovCur;
162129 pAndExpr->pLeft = 0;
162133 sqlite3VdbeGoto(v, pLevel->addrBrk);
162138 ** loop. The byte-code formatter will use that P2 value as a hint to
162140 ** See tag-20220407a in vdbe.c and shell.c */
162141 assert( pLevel->op==OP_Return );
162142 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
162144 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
162156 if( pTabItem->fg.isRecursive ){
162158 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
162159 pLevel->op = OP_Noop;
162162 pLevel->op = aStep[bRev];
162163 pLevel->p1 = iCur;
162164 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
162167 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
162172 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
162184 ** sub-queries.
162190 ** more complex one is call the "push-down" optimization in MySQL. Here
162191 ** in SQLite, the name is "MySQL push-down", since there is also another
162192 ** totally unrelated optimization called "WHERE-clause push-down".
162198 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
162201 testcase( pTerm->wtFlags & TERM_VIRTUAL );
162202 testcase( pTerm->wtFlags & TERM_CODED );
162203 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
162204 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
162205 testcase( pWInfo->untestedTerms==0
162206 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
162207 pWInfo->untestedTerms = 1;
162210 pE = pTerm->pExpr;
162212 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){
162215 ** join processing. tag-20220513a */
162217 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
162221 Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin);
162222 if( m & pLevel->notReady ){
162228 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
162232 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
162237 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
162246 u32 x = pLevel->iLikeRepCntr;
162257 pWC->nTerm-j, pTerm, iLoop));
162261 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
162266 pTerm->wtFlags |= TERM_CODED;
162279 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
162282 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
162283 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
162284 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
162285 if( pTerm->leftCursor!=iCur ) continue;
162286 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
162287 pE = pTerm->pExpr;
162291 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
162295 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
162296 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
162297 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
162300 if( pAlt->wtFlags & (TERM_CODED) ) continue;
162301 if( (pAlt->eOperator & WO_IN)
162302 && ExprUseXSelect(pAlt->pExpr)
162303 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
162307 testcase( pAlt->eOperator & WO_EQ );
162308 testcase( pAlt->eOperator & WO_IS );
162309 testcase( pAlt->eOperator & WO_IN );
162311 sEAlt = *pAlt->pExpr;
162312 sEAlt.pLeft = pE->pLeft;
162314 pAlt->wtFlags |= TERM_CODED;
162320 if( pLevel->pRJ ){
162325 WhereRightJoin *pRJ = pLevel->pRJ;
162327 /* pTab is the right-hand table of the RIGHT JOIN. Generate code that
162332 pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab;
162335 sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1);
162340 nPk = pPk->nKeyCol;
162343 int iCol = pPk->aiColumn[iPk];
162347 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
162349 VdbeComment((v, "match against %s", pTab->zName));
162351 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
162352 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
162361 if( pLevel->iLeftJoin ){
162362 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
162363 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
162365 if( pLevel->pRJ==0 ){
162370 if( pLevel->pRJ ){
162373 ** be in-line with the rest of the code. But at the end, a separate
162377 WhereRightJoin *pRJ = pLevel->pRJ;
162378 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
162379 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
162380 assert( pParse->withinRJSubrtn < 255 );
162381 pParse->withinRJSubrtn++;
162386 ** appropriate WHERE clause constraint checks. tag-20220513a.
162389 for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){
162390 testcase( pTerm->wtFlags & TERM_VIRTUAL );
162391 testcase( pTerm->wtFlags & TERM_CODED );
162392 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
162393 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
162394 assert( pWInfo->untestedTerms );
162397 if( pTabItem->fg.jointype & JT_LTORJ ) continue;
162398 assert( pTerm->pExpr );
162399 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
162400 pTerm->wtFlags |= TERM_CODED;
162406 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
162412 iLevel, (u64)pLevel->notReady);
162415 return pLevel->notReady;
162419 ** Generate the code for the loop that finds all non-matched terms
162427 Parse *pParse = pWInfo->pParse;
162428 Vdbe *v = pParse->pVdbe;
162429 WhereRightJoin *pRJ = pLevel->pRJ;
162431 WhereClause *pWC = &pWInfo->sWC;
162433 WhereLoop *pLoop = pLevel->pWLoop;
162434 SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
162440 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName));
162441 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
162442 pRJ->regReturn);
162446 assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom );
162447 pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
162448 mAll |= pWInfo->a[k].pWLoop->maskSelf;
162449 if( pRight->fg.viaCoroutine ){
162451 assert( pRight->fg.isSubquery && pRight->u4.pSubq!=0 );
162452 pSubq = pRight->u4.pSubq;
162453 assert( pSubq->pSelect!=0 && pSubq->pSelect->pEList!=0 );
162455 v, OP_Null, 0, pSubq->regResult,
162456 pSubq->regResult + pSubq->pSelect->pEList->nExpr-1
162459 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
162460 iIdxCur = pWInfo->a[k].iIdxCur;
162465 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
162466 mAll |= pLoop->maskSelf;
162467 for(k=0; k<pWC->nTerm; k++){
162468 WhereTerm *pTerm = &pWC->a[k];
162469 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
162470 && pTerm->eOperator!=WO_ROWVAL
162474 if( pTerm->prereqAll & ~mAll ) continue;
162475 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
162477 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
162481 pFrom->nSrc = 1;
162482 pFrom->nAlloc = 1;
162483 memcpy(&pFrom->a[0], pTabItem, sizeof(SrcItem));
162484 pFrom->a[0].fg.jointype = 0;
162485 assert( pParse->withinRJSubrtn < 100 );
162486 pParse->withinRJSubrtn++;
162490 int iCur = pLevel->iTabCur;
162491 int r = ++pParse->nMem;
162495 Table *pTab = pTabItem->pSTab;
162497 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r);
162502 nPk = pPk->nKeyCol;
162503 pParse->nMem += nPk - 1;
162505 int iCol = pPk->aiColumn[iPk];
162509 jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk);
162511 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
162514 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
162517 sqlite3ExprDelete(pParse->db, pSubWhere);
162519 assert( pParse->withinRJSubrtn>0 );
162520 pParse->withinRJSubrtn--;
162526 ** 2015-06-08
162553 sqlite3WhereClauseClear(&p->wc);
162561 sqlite3WhereClauseClear(&p->wc);
162568 ** The index in pWC->a[] of the new WhereTerm is returned on success.
162571 ** the db->mallocFailed flag so that higher-level functions can detect it.
162573 ** This routine will increase the size of the pWC->a[] array as necessary.
162582 ** the pWC->a[] array.
162588 if( pWC->nTerm>=pWC->nSlot ){
162589 WhereTerm *pOld = pWC->a;
162590 sqlite3 *db = pWC->pWInfo->pParse->db;
162591 pWC->a = sqlite3WhereMalloc(pWC->pWInfo, sizeof(pWC->a[0])*pWC->nSlot*2 );
162592 if( pWC->a==0 ){
162596 pWC->a = pOld;
162599 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
162600 pWC->nSlot = pWC->nSlot*2;
162602 pTerm = &pWC->a[idx = pWC->nTerm++];
162603 if( (wtFlags & TERM_VIRTUAL)==0 ) pWC->nBase = pWC->nTerm;
162605 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
162607 pTerm->truthProb = 1;
162609 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
162610 pTerm->wtFlags = wtFlags;
162611 pTerm->pWC = pWC;
162612 pTerm->iParent = -1;
162613 memset(&pTerm->eOperator, 0,
162614 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
162641 if( pExpr->pLeft->op==TK_VECTOR
162642 || pExpr->pRight->op==TK_VECTOR
162643 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
162644 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
162646 pExpr->flags ^= EP_Commuted;
162648 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
162649 if( pExpr->op>=TK_GT ){
162654 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
162655 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
162667 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
162668 c = (u16)(WO_EQ<<(op-TK_EQ));
162713 int cnt; /* Number of non-wildcard prefix characters */
162715 sqlite3 *db = pParse->db; /* Database connection */
162727 pList = pExpr->x.pList;
162728 pLeft = pList->a[1].pExpr;
162730 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
162731 op = pRight->op;
162732 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
162733 Vdbe *pReprepare = pParse->pReprepare;
162734 int iCol = pRight->iColumn;
162739 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
162740 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
162743 z = (u8*)pRight->u.zToken;
162747 ** U+fffd character, or malformed utf-8. If the underlying database
162749 ** the encoding of z[] is UTF8 - we are dealing with only UTF8 here in this
162758 const u8 *z2 = z+cnt-1;
162759 if( c==0xff || sqlite3Utf8Read(&z2)==0xfffd /* bad utf-8 */
162762 cnt--;
162765 cnt = (int)(z2-z);
162771 ** with a wildcard and if (2) the non-wildcard prefix does not end with
162778 if( (cnt>1 || (cnt>0 && z[0]!=wc[3])) && ALWAYS(255!=(u8)z[cnt-1]) ){
162790 zNew = pPrefix->u.zToken;
162806 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
162807 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
162808 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
162809 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
162810 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
162812 if( pLeft->op!=TK_COLUMN
162815 && ALWAYS(pLeft->y.pTab)
162816 && IsVirtual(pLeft->y.pTab)) /* Might be numeric */
162822 if( iTo==1 && zNew[0]=='-' ){
162825 zNew[iTo-1]++;
162827 zNew[iTo-1]--;
162842 Vdbe *v = pParse->pVdbe;
162843 sqlite3VdbeSetVarmask(v, pRight->iColumn);
162845 if( *pisComplete && pRight->u.zToken[1] ){
162854 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
162876 ** ----------------------- ---------------------------------
162904 if( pExpr->op==TK_FUNCTION ){
162919 pList = pExpr->x.pList;
162920 if( pList==0 || pList->nExpr!=2 ){
162924 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
162926 ** the left-hand side operand in their in-fix form.
162931 pCol = pList->a[1].pExpr;
162932 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
162936 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
162938 *ppRight = pList->a[0].pExpr;
162951 ** Historically, xFindFunction expected to see lower-case function
162955 pCol = pList->a[0].pExpr;
162956 assert( pCol->op!=TK_COLUMN || ExprUseYTab(pCol) );
162957 assert( pCol->op!=TK_COLUMN || (ExprUseYTab(pCol) && pCol->y.pTab!=0) );
162963 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
162965 assert( pVtab->pModule!=0 );
162967 pMod = (sqlite3_module *)pVtab->pModule;
162968 if( pMod->xFindFunction!=0 ){
162969 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
162972 *ppRight = pList->a[1].pExpr;
162978 }else if( pExpr->op>=TK_EQ ){
162985 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
162987 Expr *pLeft = pExpr->pLeft;
162988 Expr *pRight = pExpr->pRight;
162989 assert( pLeft->op!=TK_COLUMN || (ExprUseYTab(pLeft) && pLeft->y.pTab!=0) );
162993 assert( pRight==0 || pRight->op!=TK_COLUMN
162994 || (ExprUseYTab(pRight) && pRight->y.pTab!=0) );
163001 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
163002 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
163003 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
163016 pDerived->flags |= pBase->flags & (EP_OuterON|EP_InnerON);
163017 pDerived->w.iJoin = pBase->w.iJoin;
163025 pWC->a[iChild].iParent = iParent;
163026 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
163027 pWC->a[iParent].nChild++;
163031 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
163036 if( pTerm->eOperator!=WO_AND ){
163039 if( N<pTerm->u.pAndInfo->wc.nTerm ){
163040 return &pTerm->u.pAndInfo->wc.a[N];
163047 ** two subterms are in disjunction - they are OR-ed together.
163057 ** x<y OR x=y --> x<=y
163058 ** x=y OR x=y --> x=y
163059 ** x<=y OR x<y --> x<=y
163063 ** x<y OR x>y --> x!=y
163071 u16 eOp = pOne->eOperator | pTwo->eOperator;
163077 if( (pOne->wtFlags | pTwo->wtFlags) & TERM_VNULL ) return;
163078 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
163079 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
163082 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
163083 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
163084 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
163085 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
163087 if( (eOp & (eOp-1))!=0 ){
163095 db = pWC->pWInfo->pParse->db;
163096 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
163098 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
163099 pNew->op = op;
163106 ** Analyze a term that consists of two or more OR-connected
163119 ** The term being analyzed must have two or more of OR-connected subterms.
163120 ** A single subterm might be a set of AND-connected sub-subterms.
163158 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
163195 int idxTerm /* Index of the OR-term to be analyzed */
163197 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
163198 Parse *pParse = pWInfo->pParse; /* Parser context */
163199 sqlite3 *db = pParse->db; /* Database connection */
163200 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
163201 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
163204 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
163214 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
163215 assert( pExpr->op==TK_OR );
163216 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
163218 pTerm->wtFlags |= TERM_ORINFO;
163219 pOrWc = &pOrInfo->wc;
163220 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
163224 if( db->mallocFailed ) return;
163225 assert( pOrWc->nTerm>=2 );
163232 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
163233 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
163235 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
163243 pOrTerm->u.pAndInfo = pAndInfo;
163244 pOrTerm->wtFlags |= TERM_ANDINFO;
163245 pOrTerm->eOperator = WO_AND;
163246 pOrTerm->leftCursor = -1;
163247 pAndWC = &pAndInfo->wc;
163248 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
163249 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
163250 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
163252 pAndWC->pOuter = pWC;
163253 if( !db->mallocFailed ){
163254 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
163255 assert( pAndTerm->pExpr );
163256 if( allowedOp(pAndTerm->pExpr->op)
163257 || pAndTerm->eOperator==WO_AUX
163259 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
163265 }else if( pOrTerm->wtFlags & TERM_COPIED ){
163270 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
163271 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
163272 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
163273 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
163276 if( (pOrTerm->eOperator & WO_EQ)==0 ){
163288 pOrInfo->indexable = indexable;
163289 pTerm->eOperator = WO_OR;
163290 pTerm->leftCursor = -1;
163292 pWC->hasOr = 1;
163295 /* For a two-way OR, attempt to implementation case 2.
163297 if( indexable && pOrWc->nTerm==2 ){
163300 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
163303 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
163314 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
163317 ** something other than == on a column in the single table. The 1-bit
163321 ** sure the same column is used on all terms. The 2-bit case is when
163332 int iColumn = -1; /* Column index on lhs of IN operator */
163333 int iCursor = -1; /* Table cursor common to all terms */
163344 pOrTerm = pOrWc->a;
163345 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
163346 assert( pOrTerm->eOperator & WO_EQ );
163347 pOrTerm->wtFlags &= ~TERM_OK;
163348 if( pOrTerm->leftCursor==iCursor ){
163349 /* This is the 2-bit case and we are on the second iteration and
163354 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
163355 pOrTerm->leftCursor))==0 ){
163360 testcase( pOrTerm->wtFlags & TERM_COPIED );
163361 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
163362 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
163365 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
163366 iColumn = pOrTerm->u.x.leftColumn;
163367 iCursor = pOrTerm->leftCursor;
163368 pLeft = pOrTerm->pExpr->pLeft;
163376 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
163384 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
163385 assert( pOrTerm->eOperator & WO_EQ );
163386 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
163387 if( pOrTerm->leftCursor!=iCursor ){
163388 pOrTerm->wtFlags &= ~TERM_OK;
163389 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
163390 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
163395 /* If the right-hand side is also a column, then the affinities
163399 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
163400 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
163404 pOrTerm->wtFlags |= TERM_OK;
163420 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
163421 if( (pOrTerm->wtFlags & TERM_OK)==0 ) continue;
163422 assert( pOrTerm->eOperator & WO_EQ );
163423 assert( (pOrTerm->eOperator & (WO_OR|WO_AND))==0 );
163424 assert( pOrTerm->leftCursor==iCursor );
163425 assert( pOrTerm->u.x.leftColumn==iColumn );
163426 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
163427 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
163428 pLeft = pOrTerm->pExpr->pLeft;
163437 pNew->x.pList = pList;
163441 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where reused */
163470 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; /* (1) */
163471 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; /* (2) */
163474 if( pExpr->op==TK_IS
163475 && pSrc->nSrc
163476 && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0
163480 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
163481 aff2 = sqlite3ExprAffinity(pExpr->pRight);
163489 && !sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight)
163504 SrcList *pSrc = pS->pSrc;
163505 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
163506 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
163507 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
163508 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
163509 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
163512 for(i=0; i<pSrc->nSrc; i++){
163513 if( pSrc->a[i].fg.isSubquery ){
163514 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].u4.pSubq->pSelect);
163516 if( pSrc->a[i].fg.isUsing==0 ){
163517 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].u3.pOn);
163519 if( pSrc->a[i].fg.isTabFunc ){
163520 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
163524 pS = pS->pPrior;
163535 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
163546 int j /* Start looking with the j-th pFrom entry */
163552 iCur = pFrom->a[j].iCursor;
163553 for(pIdx=pFrom->a[j].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
163554 if( pIdx->aColExpr==0 ) continue;
163555 for(i=0; i<pIdx->nKeyCol; i++){
163556 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
163557 assert( pIdx->bHasExpr );
163558 if( sqlite3ExprCompareSkip(pExpr,pIdx->aColExpr->a[i].pExpr,iCur)==0
163559 && !sqlite3ExprIsConstant(0,pIdx->aColExpr->a[i].pExpr)
163567 }while( ++j < pFrom->nSrc );
163584 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
163586 pExpr = pExpr->x.pList->a[0].pExpr;
163589 if( pExpr->op==TK_COLUMN ){
163590 aiCurCol[0] = pExpr->iTable;
163591 aiCurCol[1] = pExpr->iColumn;
163595 for(i=0; i<pFrom->nSrc; i++){
163597 for(pIdx=pFrom->a[i].pSTab->pIndex; pIdx; pIdx=pIdx->pNext){
163598 if( pIdx->aColExpr ){
163630 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
163634 Bitmask prereqLeft; /* Prerequisites of the pExpr->pLeft */
163640 int op; /* Top-level operator. pExpr->op */
163641 Parse *pParse = pWInfo->pParse; /* Parsing context */
163642 sqlite3 *db = pParse->db; /* Database connection */
163646 if( db->mallocFailed ){
163649 assert( pWC->nTerm > idxTerm );
163650 pTerm = &pWC->a[idxTerm];
163651 pMaskSet = &pWInfo->sMaskSet;
163652 pExpr = pTerm->pExpr;
163654 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
163655 pMaskSet->bVarSelect = 0;
163656 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
163657 op = pExpr->op;
163659 assert( pExpr->pRight==0 );
163662 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
163664 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
163666 prereqAll = prereqLeft | pTerm->prereqRight;
163668 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
163669 if( pExpr->pLeft==0
163671 || pExpr->x.pList!=0
163675 prereqAll = prereqLeft | pTerm->prereqRight;
163678 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
163689 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
163692 extraRight = x-1; /* ON clause terms may not be used with an index
163705 if( ALWAYS(pSrc->nSrc>0) && (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){
163712 pTerm->prereqAll = prereqAll;
163713 pTerm->leftCursor = -1;
163714 pTerm->iParent = -1;
163715 pTerm->eOperator = 0;
163718 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
163719 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
163720 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
163722 if( pTerm->u.x.iField>0 ){
163724 assert( pLeft->op==TK_VECTOR );
163726 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
163730 pTerm->leftCursor = aiCurCol[0];
163731 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
163732 pTerm->u.x.leftColumn = aiCurCol[1];
163733 pTerm->eOperator = operatorMask(op) & opMask;
163735 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
163742 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
163743 assert( pTerm->u.x.iField==0 );
163744 if( pTerm->leftCursor>=0 ){
163747 if( db->mallocFailed ){
163753 pNew = &pWC->a[idxNew];
163755 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
163756 pTerm = &pWC->a[idxTerm];
163757 pTerm->wtFlags |= TERM_COPIED;
163758 assert( pWInfo->pTabList!=0 );
163759 if( termIsEquivalence(pParse, pDup, pWInfo->pTabList) ){
163760 pTerm->eOperator |= WO_EQUIV;
163767 pNew->wtFlags |= exprCommute(pParse, pDup);
163768 pNew->leftCursor = aiCurCol[0];
163769 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
163770 pNew->u.x.leftColumn = aiCurCol[1];
163772 pNew->prereqRight = prereqLeft | extraRight;
163773 pNew->prereqAll = prereqAll;
163774 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
163781 pExpr->op = TK_TRUEFALSE; /* See tag-20230504-1 */
163782 pExpr->u.zToken = "false";
163784 pTerm->prereqAll = 0;
163785 pTerm->eOperator = 0;
163805 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
163810 pList = pExpr->x.pList;
163812 assert( pList->nExpr==2 );
163817 sqlite3ExprDup(db, pExpr->pLeft, 0),
163818 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
163823 pTerm = &pWC->a[idxTerm];
163833 else if( pExpr->op==TK_OR ){
163834 assert( pWC->op==TK_AND );
163836 pTerm = &pWC->a[idxTerm];
163845 else if( pExpr->op==TK_NOTNULL ){
163846 if( pExpr->pLeft->op==TK_COLUMN
163847 && pExpr->pLeft->iColumn>=0
163851 Expr *pLeft = pExpr->pLeft;
163862 pNewTerm = &pWC->a[idxNew];
163863 pNewTerm->prereqRight = 0;
163864 pNewTerm->leftCursor = pLeft->iTable;
163865 pNewTerm->u.x.leftColumn = pLeft->iColumn;
163866 pNewTerm->eOperator = WO_GT;
163868 pTerm = &pWC->a[idxTerm];
163869 pTerm->wtFlags |= TERM_COPIED;
163870 pNewTerm->prereqAll = pTerm->prereqAll;
163886 ** for LIKE) then the lower-bound is made all uppercase and the upper-
163890 else if( pExpr->op==TK_FUNCTION
163891 && pWC->op==TK_AND
163895 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
163904 pLeft = pExpr->x.pList->a[1].pExpr;
163910 /* Convert the lower bound to upper-case and the upper bound to
163911 ** lower-case (upper-case is less than lower-case in ASCII) so that
163914 if( noCase && !pParse->db->mallocFailed ){
163917 pTerm->wtFlags |= TERM_LIKE;
163918 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
163919 pStr1->u.zToken[i] = sqlite3Toupper(c);
163920 pStr2->u.zToken[i] = sqlite3Tolower(c);
163924 if( !db->mallocFailed ){
163926 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
163934 if( *pC=='A'-1 ) isComplete = 0;
163939 while( *pC==0xBF && pC>(u8*)pStr2->u.zToken ){
163941 pC--;
163963 pTerm = &pWC->a[idxTerm];
163971 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
163972 ** new terms for each component comparison - "a = ?" and "b = ?". The
163977 ** is not a sub-select.
163979 ** tag-20220128a
163981 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
163982 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
163983 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
163984 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
163985 || (pExpr->pRight->flags & EP_xIsSelect)==0)
163986 && pWC->op==TK_AND
163992 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i, nLeft);
163993 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i, nLeft);
163995 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
164000 pTerm = &pWC->a[idxTerm];
164001 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
164002 pTerm->eOperator = WO_ROWVAL;
164005 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
164014 else if( pExpr->op==TK_IN
164015 && pTerm->u.x.iField==0
164016 && pExpr->pLeft->op==TK_VECTOR
164018 && (pExpr->x.pSelect->pPrior==0 || (pExpr->x.pSelect->selFlags & SF_Values))
164020 && pExpr->x.pSelect->pWin==0
164022 && pWC->op==TK_AND
164025 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
164028 pWC->a[idxNew].u.x.iField = i+1;
164044 else if( pWC->op==TK_AND ){
164047 while( res-- > 0 ){
164060 pNewExpr->w.iJoin = pExpr->w.iJoin;
164064 pNewTerm = &pWC->a[idxNew];
164065 pNewTerm->prereqRight = prereqExpr;
164066 pNewTerm->leftCursor = pLeft->iTable;
164067 pNewTerm->u.x.leftColumn = pLeft->iColumn;
164068 pNewTerm->eOperator = WO_AUX;
164069 pNewTerm->eMatchOp = eOp2;
164071 pTerm = &pWC->a[idxTerm];
164072 pTerm->wtFlags |= TERM_COPIED;
164073 pNewTerm->prereqAll = pTerm->prereqAll;
164083 testcase( pTerm!=&pWC->a[idxTerm] );
164084 pTerm = &pWC->a[idxTerm];
164085 pTerm->prereqRight |= extraRight;
164112 pWC->op = op;
164115 if( pE2->op!=op ){
164118 sqlite3WhereSplit(pWC, pE2->pLeft, op);
164119 sqlite3WhereSplit(pWC, pE2->pRight, op);
164126 ** where-clause passed as the first argument. The value for the term
164141 Parse *pParse = pWC->pWInfo->pParse;
164142 sqlite3 *db = pParse->db;
164150 pVal->u.iValue = iVal;
164155 pVal->iTable = iReg;
164162 pTerm = &pWC->a[idx];
164163 pTerm->leftCursor = iCsr;
164164 pTerm->eOperator = WO_AUX;
164165 pTerm->eMatchOp = eMatchOp;
164188 assert( p!=0 && p->pLimit!=0 ); /* 1 -- checked by caller */
164189 if( p->pGroupBy==0
164190 && (p->selFlags & (SF_Distinct|SF_Aggregate))==0 /* 2 */
164191 && (p->pSrc->nSrc==1 && IsVirtual(p->pSrc->a[0].pSTab)) /* 3 */
164193 ExprList *pOrderBy = p->pOrderBy;
164194 int iCsr = p->pSrc->a[0].iCursor;
164198 for(ii=0; ii<pWC->nTerm; ii++){
164199 if( pWC->a[ii].wtFlags & TERM_CODED ){
164201 ** other, subsequent terms. It can be ignored. See tag-20220128a */
164202 assert( pWC->a[ii].wtFlags & TERM_VIRTUAL );
164203 assert( pWC->a[ii].eOperator==WO_ROWVAL );
164206 if( pWC->a[ii].nChild ){
164208 ** pWC->a[] array. So this term can be ignored, as a LIMIT clause
164213 if( pWC->a[ii].leftCursor!=iCsr ) return;
164214 if( pWC->a[ii].prereqRight!=0 ) return;
164219 for(ii=0; ii<pOrderBy->nExpr; ii++){
164220 Expr *pExpr = pOrderBy->a[ii].pExpr;
164221 if( pExpr->op!=TK_COLUMN ) return;
164222 if( pExpr->iTable!=iCsr ) return;
164223 if( pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) return;
164227 /* All conditions are met. Add the terms to the where-clause object. */
164228 assert( p->pLimit->op==TK_LIMIT );
164229 if( p->iOffset!=0 && (p->selFlags & SF_Compound)==0 ){
164230 whereAddLimitExpr(pWC, p->iOffset, p->pLimit->pRight,
164233 if( p->iOffset==0 || (p->selFlags & SF_Compound)==0 ){
164234 whereAddLimitExpr(pWC, p->iLimit, p->pLimit->pLeft,
164247 pWC->pWInfo = pWInfo;
164248 pWC->hasOr = 0;
164249 pWC->pOuter = 0;
164250 pWC->nTerm = 0;
164251 pWC->nBase = 0;
164252 pWC->nSlot = ArraySize(pWC->aStatic);
164253 pWC->a = pWC->aStatic;
164262 sqlite3 *db = pWC->pWInfo->pParse->db;
164263 assert( pWC->nTerm>=pWC->nBase );
164264 if( pWC->nTerm>0 ){
164265 WhereTerm *a = pWC->a;
164266 WhereTerm *aLast = &pWC->a[pWC->nTerm-1];
164269 /* Verify that every term past pWC->nBase is virtual */
164270 for(i=pWC->nBase; i<pWC->nTerm; i++){
164271 assert( (pWC->a[i].wtFlags & TERM_VIRTUAL)!=0 );
164275 assert( a->eMatchOp==0 || a->eOperator==WO_AUX );
164276 if( a->wtFlags & TERM_DYNAMIC ){
164277 sqlite3ExprDelete(db, a->pExpr);
164279 if( a->wtFlags & (TERM_ORINFO|TERM_ANDINFO) ){
164280 if( a->wtFlags & TERM_ORINFO ){
164281 assert( (a->wtFlags & TERM_ANDINFO)==0 );
164282 whereOrInfoDelete(db, a->u.pOrInfo);
164284 assert( (a->wtFlags & TERM_ANDINFO)!=0 );
164285 whereAndInfoDelete(db, a->u.pAndInfo);
164300 ** sqlite3WhereExprUsage(MaskSet, Expr) ->
164305 ** sqlite3WhereExprUsageNN(MaskSet, Expr) ->
164310 ** sqlite3WhereExprListUsage(MaskSet, ExprList) ->
164316 ** sqlite3WhereExprUsageFull(MaskSet, ExprList) ->
164323 ** "no-inline" function to avoid the stack push overhead in the
164331 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
164332 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
164333 if( p->pRight ){
164334 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
164335 assert( p->x.pList==0 );
164337 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
164338 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
164339 }else if( p->x.pList ){
164340 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
164343 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && ExprUseYWin(p) ){
164344 assert( p->y.pWin!=0 );
164345 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
164346 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
164347 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
164353 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
164354 return sqlite3WhereGetMask(pMaskSet, p->iTable);
164356 assert( p->op!=TK_IF_NULL_ROW );
164368 for(i=0; i<pList->nExpr; i++){
164369 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
164389 for(i=pWC->nTerm-1; i>=0; i--){
164395 ** For table-valued-functions, transform the function arguments into
164411 if( pItem->fg.isTabFunc==0 ) return;
164412 pTab = pItem->pSTab;
164414 pArgs = pItem->u1.pFuncArg;
164416 for(j=k=0; j<pArgs->nExpr; j++){
164419 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
164420 if( k>=pTab->nCol ){
164421 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
164422 pTab->zName, j);
164425 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
164427 pColRef->iTable = pItem->iCursor;
164428 pColRef->iColumn = k++;
164430 pColRef->y.pTab = pTab;
164431 pItem->colUsed |= sqlite3ExprColUsed(pColRef);
164433 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
164435 if( pItem->fg.jointype & (JT_LEFT|JT_RIGHT) ){
164436 testcase( pItem->fg.jointype & JT_LEFT ); /* testtag-20230227a */
164437 testcase( pItem->fg.jointype & JT_RIGHT ); /* testtag-20230227b */
164440 testcase( pItem->fg.jointype & JT_LTORJ ); /* testtag-20230227c */
164443 sqlite3SetJoinExpr(pTerm, pItem->iCursor, joinType);
164504 return pWInfo->nRowOut;
164512 return pWInfo->eDistinct;
164524 return pWInfo->nOBSat<0 ? 0 : pWInfo->nOBSat;
164528 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
164530 ** inner-most loop did not fit within the sorter, then we can skip all
164533 ** loop - the loop immediately outside the inner-most.
164540 ** be the continuation for the second-inner-most loop. If the ORDER BY
164542 ** be the continuation for the inner-most loop.
164545 ** inner-most loop, in the sense that a correct answer will result.
164552 if( !pWInfo->bOrderedInnerLoop ){
164554 ** continuation of the inner-most loop. */
164555 return pWInfo->iContinue;
164557 pInner = &pWInfo->a[pWInfo->nLevel-1];
164558 assert( pInner->addrNxt!=0 );
164559 return pInner->pRJ ? pWInfo->iContinue : pInner->addrNxt;
164564 ** the aggregate-step call to min() or max(), check to see if any
164576 if( !pWInfo->bOrderedInnerLoop ) return;
164577 if( pWInfo->nOBSat==0 ) return;
164578 for(i=pWInfo->nLevel-1; i>=0; i--){
164579 pInner = &pWInfo->a[i];
164580 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
164581 sqlite3VdbeGoto(v, pInner->addrNxt);
164585 sqlite3VdbeGoto(v, pWInfo->iBreak);
164593 assert( pWInfo->iContinue!=0 );
164594 return pWInfo->iContinue;
164602 return pWInfo->iBreak;
164609 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
164616 ** Either value may be -1, indicating that cursor is not used.
164619 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
164623 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
164625 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
164627 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
164631 return pWInfo->eOnePass;
164639 return pWInfo->bDeferredSeek;
164646 pDest->n = pSrc->n;
164647 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
164660 LogEst rRun, /* Run-cost of the new entry */
164665 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
164666 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
164669 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
164673 if( pSet->n<N_OR_COST ){
164674 p = &pSet->a[pSet->n++];
164675 p->nOut = nOut;
164677 p = pSet->a;
164678 for(i=1; i<pSet->n; i++){
164679 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
164681 if( p->rRun<=rRun ) return 0;
164684 p->prereq = prereq;
164685 p->rRun = rRun;
164686 if( p->nOut>nOut ) p->nOut = nOut;
164696 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
164697 assert( pMaskSet->n>0 || pMaskSet->ix[0]<0 );
164698 assert( iCursor>=-1 );
164699 if( pMaskSet->ix[0]==iCursor ){
164702 for(i=1; i<pMaskSet->n; i++){
164703 if( pMaskSet->ix[i]==iCursor ){
164714 pBlock = sqlite3DbMallocRawNN(pWInfo->pParse->db, nByte+sizeof(*pBlock));
164716 pBlock->pNext = pWInfo->pMemToFree;
164717 pBlock->sz = nByte;
164718 pWInfo->pMemToFree = pBlock;
164727 pOldBlk--;
164728 assert( pOldBlk->sz<nByte );
164729 memcpy(pNew, pOld, pOldBlk->sz);
164739 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
164743 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
164744 pMaskSet->ix[pMaskSet->n++] = iCursor;
164748 ** If the right-hand branch of the expression is a TK_COLUMN, then return
164749 ** a pointer to the right-hand branch. Otherwise, return NULL.
164752 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
164753 if( ALWAYS(p!=0) && p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
164773 Expr *pX = pTerm->pExpr;
164776 assert( pTerm->eOperator & WO_IN );
164778 if( sqlite3ExprIsVector(pX->pLeft) ){
164779 int iField = pTerm->u.x.iField - 1;
164782 inexpr.pLeft = pX->pLeft->x.pList->a[iField].pExpr;
164784 inexpr.pRight = pX->x.pSelect->pEList->a[iField].pExpr;
164790 return pRet ? pRet->zName : sqlite3StrBINARY;
164802 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
164804 WhereClause *pWC; /* Shorthand for pScan->pWC */
164806 int k = pScan->k; /* Where to start scanning */
164808 assert( pScan->iEquiv<=pScan->nEquiv );
164809 pWC = pScan->pWC;
164811 iColumn = pScan->aiColumn[pScan->iEquiv-1];
164812 iCur = pScan->aiCur[pScan->iEquiv-1];
164816 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
164817 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 || pTerm->leftCursor<0 );
164818 if( pTerm->leftCursor==iCur
164819 && pTerm->u.x.leftColumn==iColumn
164821 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
164822 pScan->pIdxExpr,iCur)==0)
164823 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_OuterON))
164825 if( (pTerm->eOperator & WO_EQUIV)!=0
164826 && pScan->nEquiv<ArraySize(pScan->aiCur)
164827 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
164830 for(j=0; j<pScan->nEquiv; j++){
164831 if( pScan->aiCur[j]==pX->iTable
164832 && pScan->aiColumn[j]==pX->iColumn ){
164836 if( j==pScan->nEquiv ){
164837 pScan->aiCur[j] = pX->iTable;
164838 pScan->aiColumn[j] = pX->iColumn;
164839 pScan->nEquiv++;
164842 if( (pTerm->eOperator & pScan->opMask)!=0 ){
164844 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
164846 Parse *pParse = pWC->pWInfo->pParse;
164847 pX = pTerm->pExpr;
164849 if( (pTerm->eOperator & WO_IN) ){
164850 zCollName = indexInAffinityOk(pParse, pTerm, pScan->idxaff);
164854 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
164857 assert(pX->pLeft);
164859 zCollName = pColl ? pColl->zName : sqlite3StrBINARY;
164862 if( sqlite3StrICmp(zCollName, pScan->zCollName) ){
164866 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
164867 && (pX = pTerm->pExpr->pRight, ALWAYS(pX!=0))
164868 && pX->op==TK_COLUMN
164869 && pX->iTable==pScan->aiCur[0]
164870 && pX->iColumn==pScan->aiColumn[0]
164872 testcase( pTerm->eOperator & WO_IS );
164875 pScan->pWC = pWC;
164876 pScan->k = k+1;
164880 sqlite3DebugPrintf("SCAN-TERM %p: nEquiv=%d",
164881 pTerm, pScan->nEquiv);
164882 for(ii=0; ii<pScan->nEquiv; ii++){
164884 pScan->aiCur[ii], pScan->aiColumn[ii]);
164893 pWC = pWC->pOuter;
164896 if( pScan->iEquiv>=pScan->nEquiv ) break;
164897 pWC = pScan->pOrigWC;
164899 pScan->iEquiv++;
164906 ** It is factored out into a separate tail-recursion subroutine so that
164907 ** the normal whereScanInit() routine, which is a high-runner, does not
164911 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
164942 pScan->pOrigWC = pWC;
164943 pScan->pWC = pWC;
164944 pScan->pIdxExpr = 0;
164945 pScan->idxaff = 0;
164946 pScan->zCollName = 0;
164947 pScan->opMask = opMask;
164948 pScan->k = 0;
164949 pScan->aiCur[0] = iCur;
164950 pScan->nEquiv = 1;
164951 pScan->iEquiv = 1;
164954 iColumn = pIdx->aiColumn[j];
164955 if( iColumn==pIdx->pTable->iPKey ){
164958 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
164959 pScan->zCollName = pIdx->azColl[j];
164961 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
164962 pScan->zCollName = pIdx->azColl[j];
164963 pScan->aiColumn[0] = XN_EXPR;
164969 pScan->aiColumn[0] = iColumn;
164980 ** Search for terms matching the iColumn-th column of pIdx
164981 ** rather than the iColumn-th column of table iCur.
164985 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
164992 ** then try for the one with no dependencies on <expr> - in other words where
164995 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
165013 if( (p->prereqRight & notReady)==0 ){
165014 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
165015 testcase( p->eOperator & WO_IS );
165026 ** This function searches pList for an entry that matches the iCol-th column
165029 ** If such an expression is found, its index in pList->a[] is returned. If
165030 ** no expression is found, -1 is returned.
165040 const char *zColl = pIdx->azColl[iCol];
165042 for(i=0; i<pList->nExpr; i++){
165043 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
165045 && (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN)
165046 && p->iColumn==pIdx->aiColumn[iCol]
165047 && p->iTable==iBase
165049 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
165050 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
165056 return -1;
165060 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
165065 assert( iCol>=0 && iCol<pIdx->nColumn );
165066 j = pIdx->aiColumn[iCol];
165068 return pIdx->pTable->aCol[j].notNull;
165069 }else if( j==(-1) ){
165072 assert( j==(-2) );
165079 ** Return true if the DISTINCT expression-list passed as the third argument
165083 ** DISTINCT list are collectively unique and individually non-null.
165096 /* If there is more than one table or sub-select in the FROM clause of
165099 if( pTabList->nSrc!=1 ) return 0;
165100 iBase = pTabList->a[0].iCursor;
165101 pTab = pTabList->a[0].pSTab;
165104 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
165105 ** current SELECT is a correlated sub-query.
165107 for(i=0; i<pDistinct->nExpr; i++){
165108 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
165110 if( p->op!=TK_COLUMN && p->op!=TK_AGG_COLUMN ) continue;
165111 if( p->iTable==iBase && p->iColumn<0 ) return 1;
165122 ** comparison and select-list expressions must match those of the index.
165127 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
165129 if( pIdx->pPartIdxWhere ) continue;
165130 for(i=0; i<pIdx->nKeyCol; i++){
165136 if( i==pIdx->nKeyCol ){
165150 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
165157 ** opcodes into OP_Copy when the table is being accessed via co-routine
165170 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
165172 Vdbe *v = pParse->pVdbe;
165175 if( pParse->db->mallocFailed ) return;
165177 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165178 printf("CHECKING for column-to-copy on cursor %d for %d..%d\n",
165183 if( pOp->p1!=iTabCur ) continue;
165184 if( pOp->opcode==OP_Column ){
165186 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165190 pOp->opcode = OP_Copy;
165191 pOp->p1 = pOp->p2 + iRegister;
165192 pOp->p2 = pOp->p3;
165193 pOp->p3 = 0;
165194 pOp->p5 = 2; /* Cause the MEM_Subtype flag to be cleared */
165195 }else if( pOp->opcode==OP_Rowid ){
165197 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
165201 pOp->opcode = OP_Sequence;
165202 pOp->p1 = iAutoidxCur;
165205 pOp->opcode = OP_Null;
165206 pOp->p3 = 0;
165217 ** are no-ops.
165226 sqlite3DebugPrintf("sqlite3_index_info inputs for %s:\n", pTab->zName);
165227 for(i=0; i<p->nConstraint; i++){
165231 p->aConstraint[i].iColumn,
165232 p->aConstraint[i].iTermOffset,
165233 p->aConstraint[i].op,
165234 p->aConstraint[i].usable,
165237 for(i=0; i<p->nOrderBy; i++){
165240 p->aOrderBy[i].iColumn,
165241 p->aOrderBy[i].desc);
165250 sqlite3DebugPrintf("sqlite3_index_info outputs for %s:\n", pTab->zName);
165251 for(i=0; i<p->nConstraint; i++){
165254 p->aConstraintUsage[i].argvIndex,
165255 p->aConstraintUsage[i].omit);
165257 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
165258 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
165259 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
165260 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
165261 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
165279 ** not-NULL condition on the left table of the RIGHT JOIN.
165285 assert( (pSrc->fg.jointype&(JT_LEFT|JT_LTORJ|JT_RIGHT))!=0 ); /* By caller */
165286 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LEFT );
165287 testcase( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))==JT_LTORJ );
165288 testcase( ExprHasProperty(pTerm->pExpr, EP_OuterON) )
165289 testcase( ExprHasProperty(pTerm->pExpr, EP_InnerON) );
165290 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON)
165291 || pTerm->pExpr->w.iJoin != pSrc->iCursor
165295 if( (pSrc->fg.jointype & (JT_LEFT|JT_RIGHT))!=0
165296 && NEVER(ExprHasProperty(pTerm->pExpr, EP_InnerON))
165306 ** good column to use as part of a query-time index.
165310 ** 1. If iCol is already the left-most column of some other index,
165318 ** 2025-01-03: I experimented with a new rule that returns false if the
165328 for(pIdx = pTab->pIndex; pIdx!=0; pIdx=pIdx->pNext){
165330 for(j=0; j<pIdx->nKeyCol; j++){
165331 if( pIdx->aiColumn[j]==iCol ){
165333 if( pIdx->hasStat1 && pIdx->aiRowLogEst[j+1]>20 ) return 0;
165358 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
165359 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
165360 assert( (pSrc->fg.jointype & JT_RIGHT)==0 );
165361 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
165366 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
165367 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
165368 leftCol = pTerm->u.x.leftColumn;
165370 aff = pSrc->pSTab->aCol[leftCol].affinity;
165371 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
165372 testcase( pTerm->pExpr->op==TK_IS );
165373 return columnIsGoodIndexCandidate(pSrc->pSTab, leftCol);
165398 if( IS_STMT_SCANSTATUS(pParse->db) && pParse->explain!=2 ){
165399 Table *pTab = pIdx->pTable;
165403 sqlite3_str *pStr = sqlite3_str_new(pParse->db);
165404 sqlite3_str_appendf(pStr,"CREATE AUTOMATIC INDEX ON %s(", pTab->zName);
165405 assert( pIdx->nColumn>1 );
165406 assert( pIdx->aiColumn[pIdx->nColumn-1]==XN_ROWID || !HasRowid(pTab) );
165407 for(ii=0; ii<(pIdx->nColumn-1); ii++){
165409 int iCol = pIdx->aiColumn[ii];
165411 zName = pTab->aCol[iCol].zCnName;
165417 sqlite3OomFault(pParse->db);
165443 WhereTerm *pWCEnd; /* End of pWC->a[] */
165452 int mxBitCol; /* Maximum column in pSrc->colUsed */
165472 v = pParse->pVdbe;
165479 pTabList = pWC->pWInfo->pTabList;
165480 pSrc = &pTabList->a[pLevel->iFrom];
165481 pTable = pSrc->pSTab;
165482 pWCEnd = &pWC->a[pWC->nTerm];
165483 pLoop = pLevel->pWLoop;
165485 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
165486 Expr *pExpr = pTerm->pExpr;
165490 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
165491 && sqlite3ExprIsSingleTableConstraint(pExpr, pTabList, pLevel->iFrom, 0)
165494 sqlite3ExprDup(pParse->db, pExpr, 0));
165499 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
165500 iCol = pTerm->u.x.leftColumn;
165501 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
165503 testcase( iCol==BMS-1 );
165506 "automatic index on %s(%s)", pTable->zName,
165507 pTable->aCol[iCol].zCnName);
165511 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
165514 pLoop->aLTerm[nKeyCol++] = pTerm;
165519 assert( nKeyCol>0 || pParse->db->mallocFailed );
165520 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
165521 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
165535 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
165540 for(i=0; i<pTable->nCol; i++){
165541 if( (pTable->aCol[i].colFlags & COLFLAG_PRIMKEY)==0 ) continue;
165542 if( i>=BMS-1 ){
165543 extraCols |= MASKBIT(BMS-1);
165550 mxBitCol = MIN(BMS-1,pTable->nCol);
165551 testcase( pTable->nCol==BMS-1 );
165552 testcase( pTable->nCol==BMS-2 );
165556 if( pSrc->colUsed & MASKBIT(BMS-1) ){
165557 nKeyCol += pTable->nCol - BMS + 1;
165561 assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) );
165562 /* ^-- This guarantees that the number of index columns will fit in the u16 */
165563 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable),
165566 pLoop->u.btree.pIndex = pIdx;
165567 pIdx->zName = "auto-index";
165568 pIdx->pTable = pTable;
165571 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
165575 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
165576 iCol = pTerm->u.x.leftColumn;
165577 cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
165578 testcase( iCol==BMS-1 );
165581 Expr *pX = pTerm->pExpr;
165583 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
165585 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
165586 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
165588 if( ALWAYS(pX->pLeft!=0)
165589 && sqlite3ExprAffinity(pX->pLeft)!=SQLITE_AFF_TEXT
165601 assert( (u32)n==pLoop->u.btree.nEq );
165607 pIdx->aiColumn[n] = i;
165608 pIdx->azColl[n] = sqlite3StrBINARY;
165612 if( pSrc->colUsed & MASKBIT(BMS-1) ){
165613 for(i=BMS-1; i<pTable->nCol; i++){
165614 pIdx->aiColumn[n] = i;
165615 pIdx->azColl[n] = sqlite3StrBINARY;
165621 pIdx->aiColumn[n] = XN_ROWID;
165622 pIdx->azColl[n] = sqlite3StrBINARY;
165627 assert( pLevel->iIdxCur>=0 );
165628 pLevel->iIdxCur = pParse->nTab++;
165629 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
165631 VdbeComment((v, "for %s", pTable->zName));
165632 if( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) && useBloomFilter ){
165633 sqlite3WhereExplainBloomFilter(pParse, pWC->pWInfo, pLevel);
165634 pLevel->regFilter = ++pParse->nMem;
165635 sqlite3VdbeAddOp2(v, OP_Blob, 10000, pLevel->regFilter);
165639 assert( pSrc == &pWC->pWInfo->pTabList->a[pLevel->iFrom] );
165640 if( pSrc->fg.viaCoroutine ){
165643 assert( pSrc->fg.isSubquery );
165644 pSubq = pSrc->u4.pSubq;
165646 regYield = pSubq->regReturn;
165648 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSubq->addrFillSub);
165651 VdbeComment((v, "next row of %s", pSrc->pSTab->zName));
165653 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
165658 pLoop->wsFlags |= WHERE_PARTIALIDX;
165662 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
165664 if( pLevel->regFilter ){
165665 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0,
165666 regBase, pLoop->u.btree.nEq);
165669 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
165672 if( pSrc->fg.viaCoroutine ){
165673 assert( pSrc->fg.isSubquery && pSrc->u4.pSubq!=0 );
165675 testcase( pParse->db->mallocFailed );
165676 assert( pLevel->iIdxCur>0 );
165677 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
165678 pSrc->u4.pSubq->regResult, pLevel->iIdxCur);
165680 pSrc->fg.viaCoroutine = 0;
165682 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
165690 sqlite3VdbeScanStatusRange(v, addrExp, addrExp, -1);
165693 sqlite3ExprDelete(pParse->db, pPartial);
165710 ** and skip the subsequence B-Tree seek if the Bloom filter indicates that
165719 int iLevel, /* Index in pWInfo->a[] that is pLevel */
165728 Parse *pParse = pWInfo->pParse; /* Parsing context */
165729 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
165730 WhereLoop *pLoop = pLevel->pWLoop; /* The loop being coded */
165735 saved_pIdxEpr = pParse->pIdxEpr;
165736 saved_pIdxPartExpr = pParse->pIdxPartExpr;
165737 pParse->pIdxEpr = 0;
165738 pParse->pIdxPartExpr = 0;
165742 assert( pLoop->wsFlags & WHERE_BLOOMFILTER );
165743 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 );
165754 iCur = pLevel->iTabCur;
165755 pLevel->regFilter = ++pParse->nMem;
165758 ** to zero-filled blob of at least 80K bits, but maybe more if the
165760 ** measure the size of the table at run-time using OP_Count with
165765 pTabList = pWInfo->pTabList;
165766 iSrc = pLevel->iFrom;
165767 pItem = &pTabList->a[iSrc];
165769 pTab = pItem->pSTab;
165771 sz = sqlite3LogEstToInt(pTab->nRowLogEst);
165777 sqlite3VdbeAddOp2(v, OP_Blob, (int)sz, pLevel->regFilter);
165780 pWCEnd = &pWInfo->sWC.a[pWInfo->sWC.nTerm];
165781 for(pTerm=pWInfo->sWC.a; pTerm<pWCEnd; pTerm++){
165782 Expr *pExpr = pTerm->pExpr;
165783 if( (pTerm->wtFlags & TERM_VIRTUAL)==0
165786 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
165789 if( pLoop->wsFlags & WHERE_IPK ){
165792 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, 1);
165795 Index *pIdx = pLoop->u.btree.pIndex;
165796 int n = pLoop->u.btree.nEq;
165800 assert( pIdx->pTable==pItem->pSTab );
165803 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pLevel->regFilter, 0, r1, n);
165807 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
165810 pLoop->wsFlags &= ~WHERE_BLOOMFILTER;
165811 if( OptimizationDisabled(pParse->db, SQLITE_BloomPulldown) ) break;
165812 while( ++iLevel < pWInfo->nLevel ){
165814 pLevel = &pWInfo->a[iLevel];
165815 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
165816 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ) ) continue;
165817 pLoop = pLevel->pWLoop;
165819 if( pLoop->prereq & notReady ) continue;
165820 if( (pLoop->wsFlags & (WHERE_BLOOMFILTER|WHERE_COLUMN_IN))
165823 /* This is a candidate for bloom-filter pull-down (early evaluation).
165830 }while( iLevel < pWInfo->nLevel );
165832 pParse->pIdxEpr = saved_pIdxEpr;
165833 pParse->pIdxPartExpr = saved_pIdxPartExpr;
165840 ** are numbered from 0 upwards, starting with the terms in pWC->a[], then
165841 ** those in pWC->pOuter->a[] (if any), and so on.
165845 for(p=pWC; p; p=p->pOuter){
165846 if( iTerm<p->nTerm ) return &p->a[iTerm];
165847 iTerm -= p->nTerm;
165866 Parse *pParse = pWInfo->pParse;
165877 ExprList *pOrderBy = pWInfo->pOrderBy;
165881 pTab = pSrc->pSTab;
165889 for(p=pWC, nTerm=0; p; p=p->pOuter){
165890 for(i=0, pTerm=p->a; i<p->nTerm; i++, pTerm++){
165891 pTerm->wtFlags &= ~TERM_OK;
165892 if( pTerm->leftCursor != pSrc->iCursor ) continue;
165893 if( pTerm->prereqRight & mUnusable ) continue;
165894 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
165895 testcase( pTerm->eOperator & WO_IN );
165896 testcase( pTerm->eOperator & WO_ISNULL );
165897 testcase( pTerm->eOperator & WO_IS );
165898 testcase( pTerm->eOperator & WO_ALL );
165899 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
165900 if( pTerm->wtFlags & TERM_VNULL ) continue;
165902 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
165903 assert( pTerm->u.x.leftColumn>=XN_ROWID );
165904 assert( pTerm->u.x.leftColumn<pTab->nCol );
165905 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
165911 pTerm->wtFlags |= TERM_OK;
165921 int n = pOrderBy->nExpr;
165923 Expr *pExpr = pOrderBy->a[i].pExpr;
165932 if( pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL ) break;
165934 /* First case - a direct column references without a COLLATE operator */
165935 if( pExpr->op==TK_COLUMN && pExpr->iTable==pSrc->iCursor ){
165936 assert( pExpr->iColumn>=XN_ROWID && pExpr->iColumn<pTab->nCol );
165940 /* 2nd case - a column reference with a COLLATE operator. Only match
165942 if( pExpr->op==TK_COLLATE
165943 && (pE2 = pExpr->pLeft)->op==TK_COLUMN
165944 && pE2->iTable==pSrc->iCursor
165948 assert( pExpr->u.zToken!=0 );
165949 assert( pE2->iColumn>=XN_ROWID && pE2->iColumn<pTab->nCol );
165950 pExpr->iColumn = pE2->iColumn;
165951 if( pE2->iColumn<0 ) continue; /* Collseq does not matter for rowid */
165952 zColl = sqlite3ColumnColl(&pTab->aCol[pE2->iColumn]);
165954 if( sqlite3_stricmp(pExpr->u.zToken, zColl)==0 ) continue;
165962 if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY) && !pSrc->fg.rowidUsed ){
165963 eDistinct = 2 + ((pWInfo->wctrlFlags & WHERE_SORTBYGROUP)!=0);
165964 }else if( pWInfo->wctrlFlags & WHERE_GROUPBY ){
165972 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
165981 pIdxCons = (struct sqlite3_index_constraint*)&pHidden->aRhs[nTerm];
165984 pIdxInfo->aConstraint = pIdxCons;
165985 pIdxInfo->aOrderBy = pIdxOrderBy;
165986 pIdxInfo->aConstraintUsage = pUsage;
165987 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
165993 for(i=0; i<pPk->nKeyCol; i++){
165994 int iCol = pPk->aiColumn[i];
165996 if( iCol>=BMS-1 ) iCol = BMS-1;
165997 pIdxInfo->colUsed |= MASKBIT(iCol);
166000 pHidden->pWC = pWC;
166001 pHidden->pParse = pParse;
166002 pHidden->eDistinct = eDistinct;
166003 pHidden->mIn = 0;
166004 for(p=pWC, i=j=0; p; p=p->pOuter){
166005 int nLast = i+p->nTerm;;
166006 for(pTerm=p->a; i<nLast; i++, pTerm++){
166008 if( (pTerm->wtFlags & TERM_OK)==0 ) continue;
166009 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
166011 op = pTerm->eOperator & WO_ALL;
166013 if( (pTerm->wtFlags & TERM_SLICE)==0 ){
166014 pHidden->mIn |= SMASKBIT32(j);
166019 pIdxCons[j].op = pTerm->eMatchOp;
166036 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
166039 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
166052 pIdxInfo->nConstraint = j;
166054 Expr *pExpr = pOrderBy->a[i].pExpr;
166056 assert( pExpr->op==TK_COLUMN
166057 || (pExpr->op==TK_COLLATE && pExpr->pLeft->op==TK_COLUMN
166058 && pExpr->iColumn==pExpr->pLeft->iColumn) );
166059 pIdxOrderBy[j].iColumn = pExpr->iColumn;
166060 pIdxOrderBy[j].desc = pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC;
166063 pIdxInfo->nOrderBy = j;
166073 if( pIdxInfo->needToFreeIdxStr ){
166074 sqlite3_free(pIdxInfo->idxStr);
166075 pIdxInfo->idxStr = 0;
166076 pIdxInfo->needToFreeIdxStr = 0;
166089 assert( pHidden->pParse!=0 );
166090 assert( pHidden->pParse->db==db );
166091 for(i=0; i<pIdxInfo->nConstraint; i++){
166092 sqlite3ValueFree(pHidden->aRhs[i]); /* IMP: R-14553-25174 */
166093 pHidden->aRhs[i] = 0;
166112 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
166120 pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
166122 pParse->db->nSchemaLock++;
166123 rc = pVtab->pModule->xBestIndex(pVtab, p);
166124 pParse->db->nSchemaLock--;
166129 sqlite3OomFault(pParse->db);
166130 }else if( !pVtab->zErrMsg ){
166133 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
166136 if( pTab->u.vtab.p->bAllSchemas ){
166139 sqlite3_free(pVtab->zErrMsg);
166140 pVtab->zErrMsg = 0;
166155 ** into the aSample[] array - it is an index into a virtual set of samples
166166 IndexSample *aSample = pIdx->aSample;
166180 assert( pIdx->nSample>0 );
166181 assert( pRec->nField>0 );
166228 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
166229 nField = pIdx->nKeyCol;
166231 nField = pIdx->nColumn;
166233 nField = MIN(pRec->nField, nField);
166235 iSample = pIdx->nSample * nField;
166247 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
166253 pRec->nField = n;
166256 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
166259 iLower = aSample[iSamp].anLt[n-1];
166261 res = -1;
166264 iCol = n-1;
166273 if( pParse->db->mallocFailed==0 ){
166276 assert( i<pIdx->nSample );
166277 assert( iCol==nField-1 );
166278 pRec->nField = nField;
166280 || pParse->db->mallocFailed
166283 /* Unless i==pIdx->nSample, indicating that pRec is larger than
166286 assert( i<=pIdx->nSample && i>=0 );
166287 pRec->nField = iCol+1;
166288 assert( i==pIdx->nSample
166290 || pParse->db->mallocFailed );
166295 ** If (i>0), then pRec must also be greater than sample (i-1). */
166297 pRec->nField = iCol;
166299 || pParse->db->mallocFailed || CORRUPT_DB );
166302 pRec->nField = nField;
166303 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
166304 || pParse->db->mallocFailed || CORRUPT_DB );
166312 assert( iCol==nField-1 );
166317 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
166320 if( i>=pIdx->nSample ){
166321 iUpper = pIdx->nRowEst0;
166329 iGap = iUpper - iLower;
166337 aStat[1] = pIdx->aAvgEq[nField-1];
166340 /* Restore the pRec->nField value before returning. */
166341 pRec->nField = nField;
166360 if( pTerm->truthProb<=0 ){
166361 nRet += pTerm->truthProb;
166362 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
166363 nRet -= 20; assert( 20==sqlite3LogEst(4) );
166375 assert( iCol>=0 && iCol<pIdx->nColumn );
166376 if( !pIdx->zColAff ){
166379 assert( pIdx->zColAff[iCol]!=0 );
166380 return pIdx->zColAff[iCol];
166388 ** range-scan on a skip-scan index. For example:
166393 ** Value pLoop->nOut is currently set to the estimated number of rows
166404 ** N is the total number of samples, the pLoop->nOut value is adjusted
166407 ** nOut = nOut * ( min(U - L, 1) / N )
166428 Index *p = pLoop->u.btree.pIndex;
166429 int nEq = pLoop->u.btree.nEq;
166430 sqlite3 *db = pParse->db;
166431 int nLower = -1;
166432 int nUpper = p->nSample+1;
166441 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
166443 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
166447 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
166448 nUpper = p2 ? 0 : p->nSample;
166454 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
166455 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
166465 nDiff = (nUpper - nLower);
166474 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
166475 pLoop->nOut -= nAdjust;
166477 WHERETRACE(0x20, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
166478 nLower, nUpper, nAdjust*-1, pLoop->nOut));
166508 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
166516 ** left-most column of the index). Or, if the query is:
166541 int nOut = pLoop->nOut;
166545 Index *p = pLoop->u.btree.pIndex;
166546 int nEq = pLoop->u.btree.nEq;
166548 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
166549 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
166551 if( nEq==pBuilder->nRecValid ){
166552 UnpackedRecord *pRec = pBuilder->pRec;
166554 int nBtm = pLoop->u.btree.nBtm;
166555 int nTop = pLoop->u.btree.nTop;
166560 ** key-prefix formed by the nEq values matched against the nEq left-most
166565 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
166574 ** The number of rows between the two bounds is then just iUpper-iLower.
166578 int iLwrIdx = -2; /* aSample[] for the lower bound */
166579 int iUprIdx = -1; /* aSample[] for the upper bound */
166582 testcase( pRec->nField!=pBuilder->nRecValid );
166583 pRec->nField = pBuilder->nRecValid;
166588 iUpper = p->nRowEst0;
166590 /* Note: this call could be optimized away - since the same values must
166597 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
166598 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
166599 assert( p->aSortOrder!=0 );
166600 if( p->aSortOrder[nEq] ){
166609 Expr *pExpr = pLower->pExpr->pRight;
166616 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
166618 nOut--;
166626 Expr *pExpr = pUpper->pExpr->pRight;
166633 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
166635 nOut--;
166640 pBuilder->pRec = pRec;
166643 nNew = sqlite3LogEst(iUpper - iLower);
166648 if( iLwrIdx==iUprIdx ){ nNew -= 20; }
166670 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 || pParse->nErr>0 );
166675 ** has an application-defined likelihood(), assume the range is
166676 ** reduced by an additional 75%. This means that, by default, an open-ended
166680 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
166681 nNew -= 20;
166684 nOut -= (pLower!=0) + (pUpper!=0);
166688 if( pLoop->nOut>nOut ){
166690 pLoop->nOut, nOut));
166693 pLoop->nOut = (LogEst)nOut;
166701 ** the histogram data. This only works when x is the left-most
166708 ** non-zero.
166721 Index *p = pBuilder->pNew->u.btree.pIndex;
166722 int nEq = pBuilder->pNew->u.btree.nEq;
166723 UnpackedRecord *pRec = pBuilder->pRec;
166729 assert( nEq<=p->nColumn );
166730 assert( p->aSample!=0 );
166731 assert( p->nSample>0 );
166732 assert( pBuilder->nRecValid<nEq );
166736 if( pBuilder->nRecValid<(nEq-1) ){
166742 if( nEq>=p->nColumn ){
166747 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
166748 pBuilder->pRec = pRec;
166751 pBuilder->nRecValid = nEq;
166755 p->zName, nEq-1, (int)a[1]));
166765 ** an IN constraint where the right-hand side of the IN operator
166772 ** non-zero.
166785 Index *p = pBuilder->pNew->u.btree.pIndex;
166786 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
166787 int nRecValid = pBuilder->nRecValid;
166793 assert( p->aSample!=0 );
166794 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
166796 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
166798 pBuilder->nRecValid = nRecValid;
166806 assert( pBuilder->nRecValid==nRecValid );
166818 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
166823 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
166824 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
166825 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) zType[2] = 'L';
166826 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
166827 if( pTerm->eOperator & WO_SINGLE ){
166828 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
166830 pTerm->leftCursor, pTerm->u.x.leftColumn);
166831 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
166833 pTerm->u.pOrInfo->indexable);
166835 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
166838 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
166839 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
166843 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
166844 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
166846 if( (pTerm->eOperator & (WO_OR|WO_AND))==0 && pTerm->u.x.iField ){
166847 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
166849 if( pTerm->iParent>=0 ){
166850 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
166853 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
166867 for(i=0; i<pWC->nTerm; i++){
166868 sqlite3WhereTermPrint(&pWC->a[i], i);
166879 ** .--- Position in WHERE clause rSetup, rRun, nOut ---.
166881 ** | .--- selfMask nTerm ------. |
166883 ** | | .-- prereq Idx wsFlags----. | |
166885 ** | | | __|__ nEq ---. ___|__ | __|__
166892 pWInfo = pWC->pWInfo;
166893 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
166894 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
166895 Table *pTab = pItem->pSTab;
166896 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
166897 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
166898 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
166900 pItem->zAlias ? pItem->zAlias : pTab->zName);
166904 p->cId, p->iTab, p->maskSelf, p->prereq & 0xfff, p->cId, p->iTab);
166906 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
166908 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
166910 int i = sqlite3Strlen30(zName) - 1;
166911 while( zName[i]!='_' ) i--;
166914 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
166920 if( p->u.vtab.idxStr ){
166922 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
166924 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
166926 sqlite3DebugPrintf(" %-19s", z);
166929 if( p->wsFlags & WHERE_SKIPSCAN ){
166930 sqlite3DebugPrintf(" f %06x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
166932 sqlite3DebugPrintf(" f %06x N %d", p->wsFlags, p->nLTerm);
166934 if( pWInfo && pWInfo->bStarUsed && p->rStarDelta!=0 ){
166936 p->rSetup, p->rRun, p->nOut, p->rStarDelta);
166938 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
166940 if( p->nLTerm && (sqlite3WhereTrace & 0x4000)!=0 ){
166942 for(i=0; i<p->nLTerm; i++){
166943 sqlite3WhereTermPrint(p->aLTerm[i], i);
166953 p = p->pNextLoop;
166963 p->aLTerm = p->aLTermSpace;
166964 p->nLTerm = 0;
166965 p->nLSlot = ArraySize(p->aLTermSpace);
166966 p->wsFlags = 0;
166973 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
166974 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
166975 sqlite3_free(p->u.vtab.idxStr);
166976 p->u.vtab.needFree = 0;
166977 p->u.vtab.idxStr = 0;
166978 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
166979 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
166980 sqlite3DbFreeNN(db, p->u.btree.pIndex);
166981 p->u.btree.pIndex = 0;
166991 if( p->aLTerm!=p->aLTermSpace ){
166992 sqlite3DbFreeNN(db, p->aLTerm);
166993 p->aLTerm = p->aLTermSpace;
166994 p->nLSlot = ArraySize(p->aLTermSpace);
166997 p->nLTerm = 0;
166998 p->wsFlags = 0;
167002 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
167006 if( p->nLSlot>=n ) return SQLITE_OK;
167008 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
167010 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
167011 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
167012 p->aLTerm = paNew;
167013 p->nLSlot = n;
167022 if( pFrom->nLTerm > pTo->nLSlot
167023 && whereLoopResize(db, pTo, pFrom->nLTerm)
167029 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
167030 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
167031 pFrom->u.vtab.needFree = 0;
167032 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
167033 pFrom->u.btree.pIndex = 0;
167053 sqlite3WhereClauseClear(&pWInfo->sWC);
167054 while( pWInfo->pLoops ){
167055 WhereLoop *p = pWInfo->pLoops;
167056 pWInfo->pLoops = p->pNextLoop;
167059 while( pWInfo->pMemToFree ){
167060 WhereMemBlock *pNext = pWInfo->pMemToFree->pNext;
167061 sqlite3DbNNFreeNN(db, pWInfo->pMemToFree);
167062 pWInfo->pMemToFree = pNext;
167080 ** (1c) Neither X nor Y use skip-scan
167097 if( pX->rRun>pY->rRun && pX->nOut>pY->nOut ) return 0; /* (1d) and (2a) */
167098 assert( (pX->wsFlags & WHERE_VIRTUALTABLE)==0 );
167099 assert( (pY->wsFlags & WHERE_VIRTUALTABLE)==0 );
167100 if( pX->u.btree.nEq < pY->u.btree.nEq /* (1b) */
167101 && pX->u.btree.pIndex==pY->u.btree.pIndex /* (1a) */
167102 && pX->nSkip==0 && pY->nSkip==0 /* (1c) */
167106 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
167109 if( pY->nSkip > pX->nSkip ) return 0; /* (2d) */
167110 for(i=pX->nLTerm-1; i>=0; i--){
167111 if( pX->aLTerm[i]==0 ) continue;
167112 for(j=pY->nLTerm-1; j>=0; j--){
167113 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
167117 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
167118 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
167139 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
167140 for(; p; p=p->pNextLoop){
167141 if( p->iTab!=pTemplate->iTab ) continue;
167142 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
167146 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
167147 pTemplate->rRun, pTemplate->nOut,
167148 MIN(p->rRun, pTemplate->rRun),
167149 MIN(p->nOut - 1, pTemplate->nOut)));
167150 pTemplate->rRun = MIN(p->rRun, pTemplate->rRun);
167151 pTemplate->nOut = MIN(p->nOut - 1, pTemplate->nOut);
167155 WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n",
167156 pTemplate->rRun, pTemplate->nOut,
167157 MAX(p->rRun, pTemplate->rRun),
167158 MAX(p->nOut + 1, pTemplate->nOut)));
167159 pTemplate->rRun = MAX(p->rRun, pTemplate->rRun);
167160 pTemplate->nOut = MAX(p->nOut + 1, pTemplate->nOut);
167184 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
167185 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
167194 assert( p->rSetup==0 || pTemplate->rSetup==0
167195 || p->rSetup==pTemplate->rSetup );
167199 ** rSetup. Call this SETUP-INVARIANT */
167200 assert( p->rSetup>=pTemplate->rSetup );
167202 /* Any loop using an application-defined index (or PRIMARY KEY or
167204 ** than an automatic index. Unless it is a skip-scan. */
167205 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
167206 && (pTemplate->nSkip)==0
167207 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
167208 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
167209 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
167219 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
167220 && p->rSetup<=pTemplate->rSetup /* (2a) */
167221 && p->rRun<=pTemplate->rRun /* (2b) */
167222 && p->nOut<=pTemplate->nOut /* (2c) */
167232 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
167233 && p->rRun>=pTemplate->rRun /* (2a) */
167234 && p->nOut>=pTemplate->nOut /* (2b) */
167236 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
167252 ** If pBuilder->pOrSet is not NULL then we care about only the
167254 ** information is gathered in the pBuilder->pOrSet object. This special
167257 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
167269 WhereInfo *pWInfo = pBuilder->pWInfo;
167270 sqlite3 *db = pWInfo->pParse->db;
167274 if( pBuilder->iPlanLimit==0 ){
167276 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
167279 pBuilder->iPlanLimit--;
167281 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
167283 /* If pBuilder->pOrSet is defined, then only keep track of the costs
167286 if( pBuilder->pOrSet!=0 ){
167287 if( pTemplate->nLTerm ){
167289 u16 n = pBuilder->pOrSet->n;
167292 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
167293 pTemplate->nOut);
167296 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
167297 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167306 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
167314 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167330 sqlite3WhereLoopPrint(p, pBuilder->pWC);
167335 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
167343 p->pNextLoop = 0;
167348 WhereLoop **ppTail = &p->pNextLoop;
167355 *ppTail = pToDel->pNextLoop;
167359 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
167366 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
167367 Index *pIndex = p->u.btree.pIndex;
167368 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
167369 p->u.btree.pIndex = 0;
167388 ** TODO --> Perhaps this is something that could be improved by better
167392 ** value corresponds to -1 in LogEst notation, so this means decrement
167399 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
167400 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
167410 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
167412 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
167414 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
167415 for(i=pWC->nBase, pTerm=pWC->a; i>0; i--, pTerm++){
167417 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
167418 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
167419 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue;
167420 for(j=pLoop->nLTerm-1; j>=0; j--){
167421 pX = pLoop->aLTerm[j];
167424 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
167427 sqlite3ProgressCheck(pWC->pWInfo->pParse);
167428 if( pLoop->maskSelf==pTerm->prereqAll ){
167432 ** "self-culling".
167434 ** 2022-03-24: Self-culling only applies if either the extra terms
167435 ** are straight comparison operators that are non-true with NULL
167438 if( (pTerm->eOperator & 0x3f)!=0
167439 || (pWC->pWInfo->pTabList->a[pLoop->iTab].fg.jointype
167442 pLoop->wsFlags |= WHERE_SELFCULL;
167445 if( pTerm->truthProb<=0 ){
167448 pLoop->nOut += pTerm->truthProb;
167452 pLoop->nOut--;
167453 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
167454 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
167456 Expr *pRight = pTerm->pExpr->pRight;
167458 testcase( pTerm->pExpr->op==TK_IS );
167459 if( sqlite3ExprIsInteger(pRight, &k, 0) && k>=(-1) && k<=1 ){
167465 pTerm->wtFlags |= TERM_HEURTRUTH;
167472 if( pLoop->nOut > nRow-iReduce ){
167473 pLoop->nOut = nRow - iReduce;
167501 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
167504 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
167513 assert( ExprUseXList(pTerm->pExpr->pLeft) );
167514 pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
167515 pRhs = pTerm->pExpr->pRight;
167517 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
167519 pRhs = pRhs->x.pList->a[i].pExpr;
167526 if( pLhs->op!=TK_COLUMN
167527 || pLhs->iTable!=iCur
167528 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
167529 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
167534 testcase( pLhs->iColumn==XN_ROWID );
167536 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
167541 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
167548 ** compiled with -DSQLITE_ENABLE_COSTMULT
167557 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
167560 ** When this function is called, pBuilder->pNew->nOut contains the
167565 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
167574 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyze context */
167575 Parse *pParse = pWInfo->pParse; /* Parsing context */
167576 sqlite3 *db = pParse->db; /* Database connection malloc context */
167581 Bitmask saved_prereq; /* Original value of pNew->prereq */
167582 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
167583 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
167584 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
167585 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
167586 u16 saved_nSkip; /* Original value of pNew->nSkip */
167587 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
167588 LogEst saved_nOut; /* Original value of pNew->nOut */
167594 pNew = pBuilder->pNew;
167595 assert( db->mallocFailed==0 || pParse->nErr>0 );
167596 if( pParse->nErr ){
167597 return pParse->rc;
167600 pProbe->pTable->zName,pProbe->zName,
167601 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
167603 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
167604 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
167605 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
167608 assert( pNew->u.btree.nBtm==0 );
167611 if( pProbe->bUnordered ){
167615 assert( pNew->u.btree.nEq<pProbe->nColumn );
167616 assert( pNew->u.btree.nEq<pProbe->nKeyCol
167617 || pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY );
167619 saved_nEq = pNew->u.btree.nEq;
167620 saved_nBtm = pNew->u.btree.nBtm;
167621 saved_nTop = pNew->u.btree.nTop;
167622 saved_nSkip = pNew->nSkip;
167623 saved_nLTerm = pNew->nLTerm;
167624 saved_wsFlags = pNew->wsFlags;
167625 saved_prereq = pNew->prereq;
167626 saved_nOut = pNew->nOut;
167627 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
167629 pNew->rSetup = 0;
167630 rSize = pProbe->aiRowLogEst[0];
167633 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
167638 int nRecValid = pBuilder->nRecValid;
167640 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
167645 if( pTerm->prereqRight & pNew->maskSelf ) continue;
167649 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
167651 if( (pSrc->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0
167656 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
167657 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
167659 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
167661 pNew->wsFlags = saved_wsFlags;
167662 pNew->u.btree.nEq = saved_nEq;
167663 pNew->u.btree.nBtm = saved_nBtm;
167664 pNew->u.btree.nTop = saved_nTop;
167665 pNew->nLTerm = saved_nLTerm;
167666 if( pNew->nLTerm>=pNew->nLSlot
167667 && whereLoopResize(db, pNew, pNew->nLTerm+1)
167669 break; /* OOM while trying to enlarge the pNew->aLTerm array */
167671 pNew->aLTerm[pNew->nLTerm++] = pTerm;
167672 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
167675 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
167676 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
167677 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
167681 Expr *pExpr = pTerm->pExpr;
167692 for(i=0; i<pNew->nLTerm-1; i++){
167693 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
167695 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
167697 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
167699 if( pProbe->hasStat1 && rLogSize>=10 ){
167706 ** the left-most index column, M==N.
167716 ** with the index, as using an index has better worst-case behavior.
167721 M = pProbe->aiRowLogEst[saved_nEq];
167723 /* TUNING v----- 10 to bias toward indexed IN */
167724 x = M + logK + 10 - (nIn + rLogSize);
167733 " nInMul=%d) prefers skip-scan\n",
167735 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
167744 pNew->wsFlags |= WHERE_COLUMN_IN;
167746 int iCol = pProbe->aiColumn[saved_nEq];
167747 pNew->wsFlags |= WHERE_COLUMN_EQ;
167748 assert( saved_nEq==pNew->u.btree.nEq );
167750 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
167752 if( iCol==XN_ROWID || pProbe->uniqNotNull
167753 || (pProbe->nKeyCol==1 && pProbe->onError && (eOp & WO_EQ))
167755 pNew->wsFlags |= WHERE_ONEROW;
167757 pNew->wsFlags |= WHERE_UNQ_WANTED;
167760 if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS;
167762 pNew->wsFlags |= WHERE_COLUMN_NULL;
167765 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
167770 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
167771 pNew->u.btree.nBtm = nVecLen;
167774 if( pTerm->wtFlags & TERM_LIKEOPT ){
167778 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
167779 assert( pTop->wtFlags & TERM_LIKEOPT );
167780 assert( pTop->eOperator==WO_LT );
167781 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
167782 pNew->aLTerm[pNew->nLTerm++] = pTop;
167783 pNew->wsFlags |= WHERE_TOP_LIMIT;
167784 pNew->u.btree.nTop = 1;
167790 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
167791 pNew->u.btree.nTop = nVecLen;
167793 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
167794 pNew->aLTerm[pNew->nLTerm-2] : 0;
167798 /* At this point pNew->nOut is set to the number of rows expected to
167802 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
167803 assert( pNew->nOut==saved_nOut );
167804 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
167809 int nEq = ++pNew->u.btree.nEq;
167812 assert( pNew->nOut==saved_nOut );
167813 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
167816 pNew->nOut += pTerm->truthProb;
167817 pNew->nOut -= nIn;
167822 && pProbe->nSample
167823 && ALWAYS(pNew->u.btree.nEq<=pProbe->nSampleCol)
167824 && ((eOp & WO_IN)==0 || ExprUseXList(pTerm->pExpr))
167827 Expr *pExpr = pTerm->pExpr;
167832 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
167834 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
167839 pNew->nOut = sqlite3LogEst(nOut);
167843 ** See tag-202002240-1 */
167844 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
167853 pTerm->wtFlags |= TERM_HIGHTRUTH;
167854 if( pTerm->wtFlags & TERM_HEURTRUTH ){
167858 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
167861 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
167862 pNew->nOut -= nIn;
167868 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
167873 pNew->nOut += 10;
167881 ** 1. The cost of doing one search-by-key to find the first matching
167883 ** 2. Stepping forward in the index pNew->nOut times to find all
167886 assert( pSrc->pSTab->szTabRow>0 );
167887 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
167888 /* The pProbe->szIdxRow is low for an IPK table since the interior
167890 ** But the leaf pages are full-size, so pProbe->szIdxRow would badly
167891 ** under-estimate the scanning cost. */
167892 rCostIdx = pNew->nOut + 16;
167894 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pSTab->szTabRow;
167901 ** we also have to add in the cost of doing pNew->nOut searches to
167904 pNew->rRun = rCostIdx;
167905 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK|WHERE_EXPRIDX))==0 ){
167906 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
167908 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
167910 nOutUnadjusted = pNew->nOut;
167911 pNew->rRun += nInMul + nIn;
167912 pNew->nOut += nInMul + nIn;
167913 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
167916 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
167917 pNew->nOut = saved_nOut;
167919 pNew->nOut = nOutUnadjusted;
167922 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
167923 && pNew->u.btree.nEq<pProbe->nColumn
167924 && (pNew->u.btree.nEq<pProbe->nKeyCol ||
167925 (pProbe->idxType!=SQLITE_IDXTYPE_PRIMARYKEY && !pProbe->bIdxRowid))
167927 if( pNew->u.btree.nEq>3 ){
167932 pNew->nOut = saved_nOut;
167934 pBuilder->nRecValid = nRecValid;
167937 pNew->prereq = saved_prereq;
167938 pNew->u.btree.nEq = saved_nEq;
167939 pNew->u.btree.nBtm = saved_nBtm;
167940 pNew->u.btree.nTop = saved_nTop;
167941 pNew->nSkip = saved_nSkip;
167942 pNew->wsFlags = saved_wsFlags;
167943 pNew->nOut = saved_nOut;
167944 pNew->nLTerm = saved_nLTerm;
167946 /* Consider using a skip-scan if there are no WHERE clause constraints
167947 ** available for the left-most terms of the index, and if the average
167948 ** number of repeats in the left-most terms is at least 18.
167958 && saved_nEq+1<pProbe->nKeyCol
167959 && saved_nEq==pNew->nLTerm
167960 && pProbe->noSkipScan==0
167961 && pProbe->hasStat1!=0
167963 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
167964 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
167967 pNew->u.btree.nEq++;
167968 pNew->nSkip++;
167969 pNew->aLTerm[pNew->nLTerm++] = 0;
167970 pNew->wsFlags |= WHERE_SKIPSCAN;
167971 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
167972 pNew->nOut -= nIter;
167973 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
167974 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
167977 pNew->nOut = saved_nOut;
167978 pNew->u.btree.nEq = saved_nEq;
167979 pNew->nSkip = saved_nSkip;
167980 pNew->wsFlags = saved_wsFlags;
167984 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
168005 if( pIndex->bUnordered ) return 0;
168006 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
168007 for(ii=0; ii<pOB->nExpr; ii++){
168008 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
168010 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN)
168011 && pExpr->iTable==iCursor
168013 if( pExpr->iColumn<0 ) return 1;
168014 for(jj=0; jj<pIndex->nKeyCol; jj++){
168015 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
168017 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
168018 for(jj=0; jj<pIndex->nKeyCol; jj++){
168019 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
168020 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
168043 pParse = pWC->pWInfo->pParse;
168044 while( pWhere->op==TK_AND ){
168045 if( !whereUsablePartialIndex(iTab,jointype,pWC,pWhere->pLeft) ) return 0;
168046 pWhere = pWhere->pRight;
168048 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
168050 pExpr = pTerm->pExpr;
168051 if( (!ExprHasProperty(pExpr, EP_OuterON) || pExpr->w.iJoin==iTab)
168054 && !sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, -1)
168055 && (pTerm->wtFlags & TERM_VNULL)==0
168073 for(i=0; i<pIdx->nColumn; i++){
168074 if( pIdx->aiColumn[i]==XN_EXPR
168075 && sqlite3ExprCompare(0, pExpr, pIdx->aColExpr->a[i].pExpr, iTabCur)==0
168095 ** Information passed in is pWalk->u.pCovIdxCk. Call it pCk.
168097 ** If the Expr node references the table with cursor pCk->iTabCur, then
168098 ** make sure that column is covered by the index pCk->pIdx. We know that
168099 ** all columns less than 63 (really BMS-1) are covered, so we don't need
168102 ** If the index does not cover the column, then set pWalk->eCode to
168103 ** non-zero and return WRC_Abort to stop the search.
168108 ** If pCk->pIdx contains indexed expressions and one of those expressions
168118 pCk = pWalk->u.pCovIdxCk;
168119 pIdx = pCk->pIdx;
168120 if( (pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN) ){
168121 /* if( pExpr->iColumn<(BMS-1) && pIdx->bHasExpr==0 ) return WRC_Continue;*/
168122 if( pExpr->iTable!=pCk->iTabCur ) return WRC_Continue;
168123 pIdx = pWalk->u.pCovIdxCk->pIdx;
168124 aiColumn = pIdx->aiColumn;
168125 nColumn = pIdx->nColumn;
168127 if( aiColumn[i]==pExpr->iColumn ) return WRC_Continue;
168129 pCk->bUnidx = 1;
168131 }else if( pIdx->bHasExpr
168132 && exprIsCoveredByIndex(pExpr, pIdx, pWalk->u.pCovIdxCk->iTabCur) ){
168133 pCk->bExpr = 1;
168141 ** pIdx is an index that covers all of the low-number columns used by
168142 ** pWInfo->pSelect (columns from 0 through 62) or an index that has
168171 if( pWInfo->pSelect==0 ){
168176 if( pIdx->bHasExpr==0 ){
168177 for(i=0; i<pIdx->nColumn; i++){
168178 if( pIdx->aiColumn[i]>=BMS-1 ) break;
168180 if( i>=pIdx->nColumn ){
168195 sqlite3WalkSelect(&w, pWInfo->pSelect);
168208 ** free the Parse->pIdxEpr list when the Parse object is destroyed.
168214 *pp = p->pIENext;
168215 sqlite3ExprDelete(db, p->pExpr);
168221 ** This function is called for a partial index - one with a WHERE clause - in
168230 ** The "a" in the select-list may be replaced by <expr>, iff:
168255 assert( pItem==0 || (pItem->fg.jointype & JT_RIGHT)==0 );
168258 if( pPart->op==TK_AND ){
168259 wherePartIdxExpr(pParse, pIdx, pPart->pRight, pMask, iIdxCur, pItem);
168260 pPart = pPart->pLeft;
168263 if( (pPart->op==TK_EQ || pPart->op==TK_IS) ){
168264 Expr *pLeft = pPart->pLeft;
168265 Expr *pRight = pPart->pRight;
168268 if( pLeft->op!=TK_COLUMN ) return;
168271 if( pLeft->iColumn<0 ) return;
168272 aff = pIdx->pTable->aCol[pLeft->iColumn].affinity;
168275 sqlite3 *db = pParse->db;
168278 int bNullRow = (pItem->fg.jointype&(JT_LEFT|JT_LTORJ))!=0;
168279 p->pExpr = sqlite3ExprDup(db, pRight, 0);
168280 p->iDataCur = pItem->iCursor;
168281 p->iIdxCur = iIdxCur;
168282 p->iIdxCol = pLeft->iColumn;
168283 p->bMaybeNullRow = bNullRow;
168284 p->pIENext = pParse->pIdxPartExpr;
168285 p->aff = aff;
168286 pParse->pIdxPartExpr = p;
168287 if( p->pIENext==0 ){
168288 void *pArg = (void*)&pParse->pIdxPartExpr;
168292 }else if( pLeft->iColumn<(BMS-1) ){
168293 *pMask &= ~((Bitmask)1 << pLeft->iColumn);
168302 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
168303 ** a b-tree table, not a virtual table.
168305 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
168310 ** cost = nRow * 3.0 // full-table scan
168312 ** cost = nRow * (K+3.0) // scan of non-covering index
168319 ** the index b-tree:
168322 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
168326 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
168331 ** log(nRow) factor is omitted from a non-covering index scan in order to
168332 ** bias the scoring in favor of using an index, since the worst-case
168333 ** performance of using an index is far better than the worst-case performance
168344 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
168355 pNew = pBuilder->pNew;
168356 pWInfo = pBuilder->pWInfo;
168357 pTabList = pWInfo->pTabList;
168358 pSrc = pTabList->a + pNew->iTab;
168359 pTab = pSrc->pSTab;
168360 pWC = pBuilder->pWC;
168361 assert( !IsVirtual(pSrc->pSTab) );
168363 if( pSrc->fg.isIndexedBy ){
168364 assert( pSrc->fg.isCte==0 );
168366 pProbe = pSrc->u2.pIBIndex;
168368 pProbe = pTab->pIndex;
168384 aiRowEstPk[0] = pTab->nRowLogEst;
168386 pFirst = pSrc->pSTab->pIndex;
168387 if( pSrc->fg.notIndexed==0 ){
168394 rSize = pTab->nRowLogEst;
168398 if( !pBuilder->pOrSet /* Not part of an OR optimization */
168399 && (pWInfo->wctrlFlags & (WHERE_RIGHT_JOIN|WHERE_OR_SUBCLAUSE))==0
168400 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
168401 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
168402 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
168403 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
168404 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
168405 && (pSrc->fg.jointype & JT_RIGHT)==0 /* Not the right tab of a RIGHT JOIN */
168407 /* Generate auto-index WhereLoops */
168410 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
168412 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
168413 if( pTerm->prereqRight & pNew->maskSelf ) continue;
168415 pNew->u.btree.nEq = 1;
168416 pNew->nSkip = 0;
168417 pNew->u.btree.pIndex = 0;
168418 pNew->nLTerm = 1;
168419 pNew->aLTerm[0] = pTerm;
168420 /* TUNING: One-time cost for computing the automatic index is
168423 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
168428 pNew->rSetup = rLogSize + rSize;
168429 if( !IsView(pTab) && (pTab->tabFlags & TF_Ephemeral)==0 ){
168430 pNew->rSetup += 28;
168432 pNew->rSetup -= 25; /* Greatly reduced setup cost for auto indexes
168435 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
168436 if( pNew->rSetup<0 ) pNew->rSetup = 0;
168441 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
168442 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
168443 pNew->wsFlags = WHERE_AUTO_INDEX;
168444 pNew->prereq = mPrereq | pTerm->prereqRight;
168454 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
168456 if( pProbe->pPartIdxWhere!=0
168457 && !whereUsablePartialIndex(pSrc->iCursor, pSrc->fg.jointype, pWC,
168458 pProbe->pPartIdxWhere)
168460 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
168463 if( pProbe->bNoQuery ) continue;
168464 rSize = pProbe->aiRowLogEst[0];
168465 pNew->u.btree.nEq = 0;
168466 pNew->u.btree.nBtm = 0;
168467 pNew->u.btree.nTop = 0;
168468 pNew->nSkip = 0;
168469 pNew->nLTerm = 0;
168470 pNew->iSortIdx = 0;
168471 pNew->rSetup = 0;
168472 pNew->prereq = mPrereq;
168473 pNew->nOut = rSize;
168474 pNew->u.btree.pIndex = pProbe;
168475 pNew->u.btree.pOrderBy = 0;
168476 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
168479 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
168480 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
168482 pNew->wsFlags = WHERE_IPK;
168485 pNew->iSortIdx = b ? iSortIdx : 0;
168488 ** since index lookups have better worst-case performance if our
168499 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
168501 pNew->rRun = rSize + 16;
168503 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
168505 if( pSrc->fg.isSubquery ){
168506 if( pSrc->fg.viaCoroutine ) pNew->wsFlags |= WHERE_COROUTINE;
168507 pNew->u.btree.pOrderBy = pSrc->u4.pSubq->pSelect->pOrderBy;
168510 pNew->nOut = rSize;
168514 if( pProbe->isCovering ){
168516 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
168518 m = pSrc->colUsed & pProbe->colNotIdxed;
168519 if( pProbe->pPartIdxWhere ){
168521 pWInfo->pParse, pProbe, pProbe->pPartIdxWhere, &m, 0, 0
168524 pNew->wsFlags = WHERE_INDEXED;
168525 if( m==TOPBIT || (pProbe->bHasExpr && !pProbe->bHasVCol && m!=0) ){
168526 u32 isCov = whereIsCoveringIndex(pWInfo, pProbe, pSrc->iCursor);
168529 ("-> %s is not a covering index"
168530 " according to whereIsCoveringIndex()\n", pProbe->zName));
168534 pNew->wsFlags |= isCov;
168537 ("-> %s is a covering expression index"
168538 " according to whereIsCoveringIndex()\n", pProbe->zName));
168542 ("-> %s might be a covering expression index"
168543 " according to whereIsCoveringIndex()\n", pProbe->zName));
168547 && (HasRowid(pTab) || pWInfo->pSelect!=0 || sqlite3FaultSim(700))
168550 ("-> %s is a covering index according to bitmasks\n",
168551 pProbe->zName, m==0 ? "is" : "is not"));
168552 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
168559 || pProbe->pPartIdxWhere!=0
168560 || pSrc->fg.isIndexedBy
168562 && pProbe->bUnordered==0
168563 && (pProbe->szIdxRow<pTab->szTabRow)
168564 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
168566 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
168569 pNew->iSortIdx = b ? iSortIdx : 0;
168574 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
168576 /* If this is a non-covering index scan, add in the cost of
168583 int iCur = pSrc->iCursor;
168584 WhereClause *pWC2 = &pWInfo->sWC;
168585 for(ii=0; ii<pWC2->nTerm; ii++){
168586 WhereTerm *pTerm = &pWC2->a[ii];
168587 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
168592 if( pTerm->truthProb<=0 ){
168593 nLookup += pTerm->truthProb;
168595 nLookup--;
168596 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
168600 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
168602 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
168604 if( (pSrc->fg.jointype & JT_RIGHT)!=0 && pProbe->aColExpr ){
168605 /* Do not do an SCAN of a index-on-expression in a RIGHT JOIN
168607 ** positioned to the correct row during the right-join no-match
168612 pNew->nOut = rSize;
168617 pBuilder->bldFlags1 = 0;
168619 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
168620 /* If a non-unique index is used, or if a prefix of the key for
168621 ** unique index is used (making the index functionally non-unique)
168624 pTab->tabFlags |= TF_MaybeReanalyze;
168627 sqlite3Stat4ProbeFree(pBuilder->pRec);
168628 pBuilder->nRecValid = 0;
168629 pBuilder->pRec = 0;
168641 assert( pTerm->eOperator==WO_AUX || pTerm->eMatchOp==0 );
168642 return pTerm->eMatchOp>=SQLITE_INDEX_CONSTRAINT_LIMIT
168643 && pTerm->eMatchOp<=SQLITE_INDEX_CONSTRAINT_OFFSET;
168648 ** marked as in-use (have argvIndex>0). False otherwise.
168663 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
168691 WhereClause *pWC = pBuilder->pWC;
168694 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
168698 WhereLoop *pNew = pBuilder->pNew;
168699 Parse *pParse = pBuilder->pWInfo->pParse;
168700 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
168701 int nConstraint = pIdxInfo->nConstraint;
168705 pNew->prereq = mPrereq;
168709 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
168711 WhereTerm *pTerm = termFromWhereClause(pWC, pIdxCons->iTermOffset);
168712 pIdxCons->usable = 0;
168713 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
168714 && (pTerm->eOperator & mExclude)==0
168717 pIdxCons->usable = 1;
168723 assert( pIdxInfo->needToFreeIdxStr==0 );
168724 pIdxInfo->idxStr = 0;
168725 pIdxInfo->idxNum = 0;
168726 pIdxInfo->orderByConsumed = 0;
168727 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
168728 pIdxInfo->estimatedRows = 25;
168729 pIdxInfo->idxFlags = 0;
168730 pHidden->mHandleIn = 0;
168733 rc = vtabBestIndex(pParse, pSrc->pSTab, pIdxInfo);
168740 WHERETRACE(0xffffffff, (" ^^^^--- non-viable plan rejected!\n"));
168747 mxTerm = -1;
168748 assert( pNew->nLSlot>=nConstraint );
168749 memset(pNew->aLTerm, 0, sizeof(pNew->aLTerm[0])*nConstraint );
168750 memset(&pNew->u.vtab, 0, sizeof(pNew->u.vtab));
168751 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
168754 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
168756 int j = pIdxCons->iTermOffset;
168760 || pNew->aLTerm[iTerm]!=0
168761 || pIdxCons->usable==0
168763 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
168767 testcase( iTerm==nConstraint-1 );
168769 testcase( j==pWC->nTerm-1 );
168770 pNew->prereq |= pTerm->prereqRight;
168771 assert( iTerm<pNew->nLSlot );
168772 pNew->aLTerm[iTerm] = pTerm;
168779 pNew->u.vtab.omitMask |= 1<<iTerm;
168783 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET ){
168784 pNew->u.vtab.bOmitOffset = 1;
168787 if( SMASKBIT32(i) & pHidden->mHandleIn ){
168788 pNew->u.vtab.mHandleIn |= MASKBIT32(iTerm);
168789 }else if( (pTerm->eOperator & WO_IN)!=0 ){
168795 pIdxInfo->orderByConsumed = 0;
168796 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
168800 /* Unless pbRetryLimit is non-NULL, there should be no LIMIT/OFFSET
168803 assert( !isLimitTerm(pTerm) || i>=nConstraint-2 );
168804 assert( !isLimitTerm(pTerm) || i==nConstraint-1 || isLimitTerm(pTerm+1) );
168821 pNew->nLTerm = mxTerm+1;
168823 if( pNew->aLTerm[i]==0 ){
168824 /* The non-zero argvIdx values must be contiguous. Raise an
168826 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pSTab->zName);
168831 assert( pNew->nLTerm<=pNew->nLSlot );
168832 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
168833 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
168834 pIdxInfo->needToFreeIdxStr = 0;
168835 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
168836 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
168837 pIdxInfo->nOrderBy : 0);
168838 pNew->u.vtab.bIdxNumHex = (pIdxInfo->idxFlags&SQLITE_INDEX_SCAN_HEX)!=0;
168839 pNew->rSetup = 0;
168840 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
168841 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
168845 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
168846 pNew->wsFlags |= WHERE_ONEROW;
168848 pNew->wsFlags &= ~WHERE_ONEROW;
168851 if( pNew->u.vtab.needFree ){
168852 sqlite3_free(pNew->u.vtab.idxStr);
168853 pNew->u.vtab.needFree = 0;
168857 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
168880 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
168882 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
168883 Expr *pX = termFromWhereClause(pHidden->pWC, iTerm)->pExpr;
168884 if( pX->pLeft ){
168885 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
168887 zRet = (pC ? pC->zName : sqlite3StrBINARY);
168900 if( m & pHidden->mIn ){
168902 pHidden->mHandleIn &= ~m;
168904 pHidden->mHandleIn |= m;
168915 ** on the right-hand-side of constraint iCons.
168925 if( iCons<0 || iCons>=pIdxInfo->nConstraint ){
168926 rc = SQLITE_MISUSE_BKPT; /* EV: R-30545-25046 */
168928 if( pH->aRhs[iCons]==0 ){
168930 pH->pWC, pIdxInfo->aConstraint[iCons].iTermOffset
168933 pH->pParse->db, pTerm->pExpr->pRight, ENC(pH->pParse->db),
168934 SQLITE_AFF_BLOB, &pH->aRhs[iCons]
168938 pVal = pH->aRhs[iCons];
168942 if( rc==SQLITE_OK && pVal==0 ){ /* IMP: R-19933-32160 */
168943 rc = SQLITE_NOTFOUND; /* IMP: R-36424-56542 */
168954 assert( pHidden->eDistinct>=0 && pHidden->eDistinct<=3 );
168955 return pHidden->eDistinct;
168961 ** prepared is read-only, then just start read transactions on all
168965 ** This is used by the (built-in) sqlite_dbpage virtual table.
168968 int nDb = pParse->db->nDb;
168973 if( DbMaskNonZero(pParse->writeMask) ){
168982 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
169003 ** mUnusable should always be configured as "not-usable" for xBestIndex.
169024 pWInfo = pBuilder->pWInfo;
169025 pParse = pWInfo->pParse;
169026 pWC = pBuilder->pWC;
169027 pNew = pBuilder->pNew;
169028 pSrc = &pWInfo->pTabList->a[pNew->iTab];
169029 assert( IsVirtual(pSrc->pSTab) );
169032 pNew->rSetup = 0;
169033 pNew->wsFlags = WHERE_VIRTUALTABLE;
169034 pNew->nLTerm = 0;
169035 pNew->u.vtab.needFree = 0;
169036 nConstraint = p->nConstraint;
169037 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
169038 freeIndexInfo(pParse->db, p);
169043 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pSTab->zName));
169060 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
169073 mBestNoIn = pNew->prereq & ~mPrereq;
169087 int iTerm = p->aConstraint[i].iTermOffset;
169088 Bitmask mThis = termFromWhereClause(pWC, iTerm)->prereqRight & ~mPrereq;
169098 if( pNew->prereq==mPrereq ){
169124 freeIndexInfo(pParse->db, p);
169125 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pSTab->zName, rc));
169139 WhereInfo *pWInfo = pBuilder->pWInfo;
169150 pWC = pBuilder->pWC;
169151 pWCEnd = pWC->a + pWC->nTerm;
169152 pNew = pBuilder->pNew;
169154 pItem = pWInfo->pTabList->a + pNew->iTab;
169155 iCur = pItem->iCursor;
169157 /* The multi-index OR optimization does not work for RIGHT and FULL JOIN */
169158 if( pItem->fg.jointype & JT_RIGHT ) return SQLITE_OK;
169160 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
169161 if( (pTerm->eOperator & WO_OR)!=0
169162 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
169164 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
169165 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
169173 WHERETRACE(0x400, ("Begin processing OR-clause %p\n", pTerm));
169174 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
169175 if( (pOrTerm->eOperator & WO_AND)!=0 ){
169176 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
169177 }else if( pOrTerm->leftCursor==iCur ){
169178 tempWC.pWInfo = pWC->pWInfo;
169190 WHERETRACE(0x400, ("OR-term %d of %p has %d subterms:\n",
169191 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
169197 if( IsVirtual(pItem->pSTab) ){
169228 pNew->nLTerm = 1;
169229 pNew->aLTerm[0] = pTerm;
169230 pNew->wsFlags = WHERE_MULTI_OR;
169231 pNew->rSetup = 0;
169232 pNew->iSortIdx = 0;
169233 memset(&pNew->u, 0, sizeof(pNew->u));
169236 ** of all sub-scans required by the OR-scan. However, due to rounding
169237 ** errors, it may be that the cost of the OR-scan is equal to its
169238 ** most expensive sub-scan. Add the smallest possible penalty
169245 ** the planner may elect to "OR" together a full-table scan and an
169247 pNew->rRun = sSum.a[i].rRun + 1;
169248 pNew->nOut = sSum.a[i].nOut;
169249 pNew->prereq = sSum.a[i].prereq;
169252 WHERETRACE(0x400, ("End processing OR-clause %p\n", pTerm));
169262 WhereInfo *pWInfo = pBuilder->pWInfo;
169266 SrcList *pTabList = pWInfo->pTabList;
169268 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
169269 sqlite3 *db = pWInfo->pParse->db;
169277 pNew = pBuilder->pNew;
169280 assert( pNew->nLTerm==0 );
169281 assert( pNew->wsFlags==0 );
169282 assert( pNew->nLSlot>=ArraySize(pNew->aLTermSpace) );
169283 assert( pNew->aLTerm!=0 );
169285 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
169286 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
169288 pNew->iTab = iTab;
169289 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
169290 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
169292 || (pItem->fg.jointype & (JT_OUTER|JT_CROSS|JT_LTORJ))!=0
169300 ** prevent FROM-clause terms from moving from the right side of
169304 if( pItem->fg.jointype & JT_LTORJ ) hasRightJoin = 1;
169306 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
169311 if( IsVirtual(pItem->pSTab) ){
169314 if( mUnusable || (p->fg.jointype & (JT_OUTER|JT_CROSS)) ){
169315 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
169324 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
169327 mPrior |= pNew->maskSelf;
169328 if( rc || db->mallocFailed ){
169343 /* Implementation of the order-by-subquery optimization:
169345 ** WhereLoop pLoop, which the iLoop-th term of the nested loop, is really
169355 ** WITH t3(p,q) AS MATERIALIZED (SELECT x+y, x-y FROM t2 ORDER BY x+y)
169379 ** implemented as a co-routine, the sort orders must be in the same
169380 ** direction because there is no way to run a co-routine backwards.
169391 int iOB; /* Index into pOrderBy->a[] */
169392 int jSub; /* Index into pSubOB->a[] */
169398 pSubOB = pLoop->u.btree.pOrderBy;
169401 for(jSub=0; jSub<pSubOB->nExpr && iOB<pOrderBy->nExpr; jSub++, iOB++){
169402 if( pSubOB->a[jSub].u.x.iOrderByCol==0 ) break;
169403 pOBExpr = pOrderBy->a[iOB].pExpr;
169404 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) break;
169405 if( pOBExpr->iTable!=iCur ) break;
169406 if( pOBExpr->iColumn!=pSubOB->a[jSub].u.x.iOrderByCol-1 ) break;
169407 if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
169408 u8 sfOB = pOrderBy->a[iOB].fg.sortFlags; /* sortFlags for iOB */
169409 u8 sfSub = pSubOB->a[jSub].fg.sortFlags; /* sortFlags for jSub */
169421 if( (pLoop->wsFlags & WHERE_COROUTINE)!=0 ){
169422 /* Cannot run a co-routine in reverse order */
169449 ** pOrderBy terms must be matched in strict left-to-right order.
169456 u16 nLoop, /* Number of entries in pPath->aLoop[] */
169457 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
169463 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
169479 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
169482 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
169486 ** We say the WhereLoop is "one-row" if it generates no more than one
169487 ** row of output. A WhereLoop is one-row if all of the following are true:
169490 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
169491 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
169493 ** We say the WhereLoop is "order-distinct" if the set of columns from
169495 ** row of the WhereLoop. Every one-row WhereLoop is automatically
169496 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
169497 ** is not order-distinct. To be order-distinct is not quite the same as being
169499 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
169500 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
169504 ** automatically order-distinct.
169510 nOrderBy = pOrderBy->nExpr;
169511 testcase( nOrderBy==BMS-1 );
169512 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
169514 obDone = MASKBIT(nOrderBy)-1;
169522 if( iLoop>0 ) ready |= pLoop->maskSelf;
169524 pLoop = pPath->aLoop[iLoop];
169529 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
169530 if( pLoop->u.vtab.isOrdered
169537 pLoop->u.btree.nDistinctCol = 0;
169539 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
169548 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
169550 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
169551 if( pOBExpr->iTable!=iCur ) continue;
169552 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
169555 if( pTerm->eOperator==WO_IN ){
169561 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
169562 if( j>=pLoop->nLTerm ) continue;
169564 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
169565 Parse *pParse = pWInfo->pParse;
169566 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
169567 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
169569 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
169572 testcase( pTerm->pExpr->op==TK_IS );
169577 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
169578 if( pLoop->wsFlags & WHERE_IPK ){
169579 if( pLoop->u.btree.pOrderBy
169591 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
169594 nKeyCol = pIndex->nKeyCol;
169595 nColumn = pIndex->nColumn;
169596 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
169597 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
169598 || !HasRowid(pIndex->pTable));
169599 /* All relevant terms of the index must also be non-NULL in order
169602 ** made at tag-20210426-1 below */
169604 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
169615 assert( j>=pLoop->u.btree.nEq
169616 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
169618 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
169619 u16 eOp = pLoop->aLTerm[j]->eOperator;
169624 ** the loop need to be marked as not order-distinct because it can
169643 ** j<pLoop->u.btree.nEq constraint above. Any equality other
169646 Expr *pX = pLoop->aLTerm[j]->pExpr;
169647 for(i=j+1; i<pLoop->u.btree.nEq; i++){
169648 if( pLoop->aLTerm[i]->pExpr==pX ){
169649 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
169658 ** (revIdx) for the j-th column of the index.
169661 iColumn = pIndex->aiColumn[j];
169662 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
169663 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
169670 ** WhereLoop is not well-ordered. tag-20210426-1
169674 && j>=pLoop->u.btree.nEq
169675 && pIndex->pTable->aCol[iColumn].notNull==0
169684 /* Find the ORDER BY term that corresponds to the j-th column
169690 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
169696 if( pOBExpr->op!=TK_COLUMN && pOBExpr->op!=TK_AGG_COLUMN ) continue;
169697 if( pOBExpr->iTable!=iCur ) continue;
169698 if( pOBExpr->iColumn!=iColumn ) continue;
169700 Expr *pIxExpr = pIndex->aColExpr->a[j].pExpr;
169706 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
169707 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
169710 pLoop->u.btree.nDistinctCol = j+1;
169720 != (pOrderBy->a[i].fg.sortFlags&KEYINFO_ORDER_DESC)
169725 rev = revIdx ^ (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_DESC);
169730 if( isMatch && (pOrderBy->a[i].fg.sortFlags & KEYINFO_ORDER_BIGNULL) ){
169731 if( j==pLoop->u.btree.nEq ){
169732 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
169756 } /* end-if not one-row */
169760 orderDistinctMask |= pLoop->maskSelf;
169765 p = pOrderBy->a[i].pExpr;
169766 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
169773 } /* End the loop over all WhereLoops from outer-most down to inner-most */
169776 for(i=nOrderBy-1; i>0; i--){
169777 Bitmask m = ALWAYS(i<BMS) ? MASKBIT(i) - 1 : 0;
169782 return -1;
169789 ** BY clause - and so any order that groups rows as required satisfies the
169806 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
169807 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
169810 assert( pWInfo->wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY) );
169811 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
169812 return pWInfo->sorted;
169820 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
169821 if( pLast ) zName[i++] = pLast->cId;
169843 ** Or, if the order-by clause has X terms but only the last Y
169844 ** terms are out of order, then block-sorting will reduce the
169860 assert( pWInfo->pSelect!=0 );
169861 assert( pWInfo->pSelect->pEList!=0 );
169863 nCol = sqlite3LogEst((pWInfo->pSelect->pEList->nExpr+59)/30);
169867 rSortCost += sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
169875 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 ){
169880 if( pWInfo->iLimit<nRow ){
169881 nRow = pWInfo->iLimit;
169883 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
169886 if( nRow>10 ){ nRow -= 10; assert( 10==sqlite3LogEst(2) ); }
169897 ** Query planning is NP-hard. We must limit the number of paths at
169905 ** For the purposes of this heuristic, a star-query is defined as a query
169910 ** self-joined cannot be a dimension table; we assume that dimension
169915 ** If pWInfo describes a star-query, then the cost for SCANs of dimension
169925 ** considered a star-query, using SQLITE_TESTCTRL_OPTIMIZATION to
169927 ** to do that is: ".testctrl opt -starquery".
169931 ** This optimization was first added on 2024-05-09 by check-in 38db9b5c83d.
169937 ** forum post b18ef983e68d06d1 (2024-12-21)
169938 ** forum post 0025389d0860af82 (2025-01-14)
169939 ** forum post d87570a145599033 (2025-01-17)
169941 ** To address these, the criteria for a star-query was tightened to exclude
169943 ** the affect of star-schema detection was changed to increase the rRun cost
169948 int nLoop = pWInfo->nLevel; /* Number of terms in the join */
169952 /* The star-query detection code below makes use of the following
169958 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
169959 assert( pWLoop->maskSelf==MASKBIT(pWLoop->iTab) );
169960 assert( pWLoop->pNextLoop==0 || pWLoop->iTab<=pWLoop->pNextLoop->iTab );
169965 && !pWInfo->bStarDone
169966 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_StarQuery)
169969 int iFromIdx; /* Term of FROM clause is the candidate fact-table */
169970 Bitmask m; /* Bitmask for candidate fact-table */
169972 WhereLoop *pStart; /* Where to start searching for dimension-tables */
169974 pWInfo->bStarDone = 1; /* Only do this computation once */
169980 assert( !pWInfo->bStarUsed );
169981 aFromTabs = pWInfo->pTabList->a;
169982 pStart = pWInfo->pLoops;
169990 if( (pFactTab->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
169991 /* If the candidate fact-table is the right table of an outer join
169992 ** restrict the search for dimension-tables to be tables to the right
169993 ** of the fact-table. */
169995 while( pStart && pStart->iTab<=iFromIdx ){
169996 pStart = pStart->pNextLoop;
169999 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170000 if( (aFromTabs[pWLoop->iTab].fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
170001 /* Fact-tables and dimension-tables cannot be separated by an
170002 ** outer join (at least for the definition of fact- and dimension-
170006 if( (pWLoop->prereq & m)!=0 /* pWInfo depends on iFromIdx */
170007 && (pWLoop->maskSelf & mSeen)==0 /* pWInfo not already a dependency */
170008 && (pWLoop->maskSelf & mSelfJoin)==0 /* Not a self-join */
170010 if( aFromTabs[pWLoop->iTab].pSTab==pFactTab->pSTab ){
170014 mSeen |= pWLoop->maskSelf;
170026 if( !pWInfo->bStarUsed ){
170027 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170028 pWLoop->rStarDelta = 0;
170032 pWInfo->bStarUsed = 1;
170037 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170038 if( pWLoop->iTab<iFromIdx ) continue;
170039 if( pWLoop->iTab>iFromIdx ) break;
170040 if( pWLoop->rRun>mxRun ) mxRun = pWLoop->rRun;
170046 for(pWLoop=pStart; pWLoop; pWLoop=pWLoop->pNextLoop){
170047 if( (pWLoop->maskSelf & mSeen)==0 ) continue;
170048 if( pWLoop->nLTerm ) continue;
170049 if( pWLoop->rRun<mxRun ){
170052 SrcItem *pDim = aFromTabs + pWLoop->iTab;
170055 pDim->zAlias ? pDim->zAlias: pDim->pSTab->zName, pWLoop->iTab,
170056 pFactTab->zAlias ? pFactTab->zAlias : pFactTab->pSTab->zName,
170060 pWLoop->rStarDelta = mxRun - pWLoop->rRun;
170062 pWLoop->rRun = mxRun;
170067 if( (sqlite3WhereTrace & 0x80000)!=0 && pWInfo->bStarUsed ){
170068 sqlite3DebugPrintf("WhereLoops changed by star-query heuristic:\n");
170069 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170070 if( pWLoop->rStarDelta ){
170071 sqlite3WhereLoopPrint(pWLoop, &pWInfo->sWC);
170077 return pWInfo->bStarUsed ? 18 : 12;
170087 ** -------- ----------------------------------------------------------
170095 if( (pCandidate->wsFlags & WHERE_INDEXED)==0 ) return 1;
170096 if( (pBaseline->wsFlags & WHERE_INDEXED)==0 ) return 1;
170097 if( pCandidate->u.btree.pIndex->szIdxRow <
170098 pBaseline->u.btree.pIndex->szIdxRow ) return 0;
170103 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
170105 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
170135 pParse = pWInfo->pParse;
170136 nLoop = pWInfo->nLevel;
170137 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d, nQueryLoop=%d)\n",
170138 nRowEst, pParse->nQueryLoop));
170143 ** ----- --------
170152 }else if( pParse->nErr ){
170157 assert( nLoop<=pWInfo->pTabList->nSrc );
170164 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
170167 nOrderBy = pWInfo->pOrderBy->nExpr;
170173 pSpace = sqlite3StackAllocRawNN(pParse->db, nSpace);
170179 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
170180 pFrom->aLoop = pX;
170185 ** is either zero - meaning it has not yet been initialized - or the
170200 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
170208 ** -1, indicating that the result set may or may not be ordered,
170210 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
170219 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
170225 Bitmask revMask; /* Mask of rev-order loops for (..) */
170227 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
170228 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
170229 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
170240 rUnsort = pWLoop->rRun + pFrom->nRow;
170241 if( pWLoop->rSetup ){
170242 rUnsort = sqlite3LogEstAdd(pWLoop->rSetup, rUnsort);
170244 rUnsort = sqlite3LogEstAdd(rUnsort, pFrom->rUnsort);
170245 nOut = pFrom->nRow + pWLoop->nOut;
170246 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
170247 isOrdered = pFrom->isOrdered;
170251 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
170254 revMask = pFrom->revLoop;
170269 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
170270 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
170274 rUnsort -= 2; /* TUNING: Slight bias in favor of no-sort plans */
170278 ** mxChoice best-so-far paths.
170280 ** First look for an existing path among best-so-far paths
170284 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
170285 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
170286 ** of legal values for isOrdered, -1..64.
170290 if( pTo->maskLoop==maskNew
170291 && ((pTo->isOrdered^isOrdered)&0x80)==0
170293 testcase( jj==nTo-1 );
170298 /* None of the existing best-so-far paths match the candidate. */
170303 ** paths currently in the best-so-far buffer. So discard
170307 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
170315 ** needs to be added to the set of best-so-far paths. */
170326 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
170332 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
170338 ** (pTo->rCost,pTo->nRow,pTo->rUnsort) <= (rCost,nOut,rUnsort)
170340 if( (pTo->rCost<rCost)
170341 || (pTo->rCost==rCost && pTo->nRow<nOut)
170342 || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort<rUnsort)
170343 || (pTo->rCost==rCost && pTo->nRow==nOut && pTo->rUnsort==rUnsort
170344 && whereLoopIsNoBetter(pWLoop, pTo->aLoop[iLoop]) )
170349 "Skip %s cost=%-3d,%3d,%3d order=%c",
170352 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
170353 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170354 pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
170358 testcase( pTo->rCost==rCost );
170361 testcase( pTo->rCost==rCost+1 );
170367 "Update %s cost=%-3d,%3d,%3d order=%c",
170370 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
170371 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170372 pTo->rUnsort, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
170377 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
170378 pTo->revLoop = revMask;
170379 pTo->nRow = nOut;
170380 pTo->rCost = rCost;
170381 pTo->rUnsort = rUnsort;
170382 pTo->isOrdered = isOrdered;
170383 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
170384 pTo->aLoop[iLoop] = pWLoop;
170390 if( pTo->rCost>mxCost
170391 || (pTo->rCost==mxCost && pTo->rUnsort>mxUnsort)
170393 mxCost = pTo->rCost;
170394 mxUnsort = pTo->rUnsort;
170407 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
170412 if( pTo->rCost>rFloor && pTo->rCost<rMin ) rMin = pTo->rCost;
170415 if( pTo->rCost==rMin ){
170416 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
170417 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
170418 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
170419 if( pTo->isOrdered>0 ){
170420 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
170442 sqlite3StackFreeNN(pParse->db, pSpace);
170449 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
170451 assert( pWInfo->nLevel==nLoop );
170454 WhereLevel *pLevel = pWInfo->a + iLoop;
170455 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
170456 pLevel->iFrom = pWLoop->iTab;
170457 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
170459 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
170460 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
170461 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
170465 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
170466 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
170467 if( rc==pWInfo->pResultSet->nExpr ){
170468 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
170471 pWInfo->bOrderedInnerLoop = 0;
170472 if( pWInfo->pOrderBy ){
170473 pWInfo->nOBSat = pFrom->isOrdered;
170474 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
170475 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
170476 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
170478 /* vvv--- See check-in [12ad822d9b827777] on 2023-03-16 ---vvv */
170479 assert( pWInfo->pSelect->pOrderBy==0
170480 || pWInfo->nOBSat <= pWInfo->pSelect->pOrderBy->nExpr );
170482 pWInfo->revMask = pFrom->revLoop;
170483 if( pWInfo->nOBSat<=0 ){
170484 pWInfo->nOBSat = 0;
170486 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
170491 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
170492 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
170495 if( rc==pWInfo->pOrderBy->nExpr ){
170496 pWInfo->bOrderedInnerLoop = 1;
170497 pWInfo->revMask = m;
170502 && pWInfo->nOBSat==1
170503 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
170505 pWInfo->bOrderedInnerLoop = 1;
170508 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
170509 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
170512 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
170513 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
170515 assert( pWInfo->sorted==0 );
170516 if( nOrder==pWInfo->pOrderBy->nExpr ){
170517 pWInfo->sorted = 1;
170518 pWInfo->revMask = revMask;
170523 pWInfo->nRowOut = pFrom->nRow;
170525 pWInfo->rTotalCost = pFrom->rCost;
170529 sqlite3StackFreeNN(pParse->db, pSpace);
170546 ** FROM clause term that would try to do a full-table scan. This prevents
170547 ** an index search from being converted into a full-table scan in order to
170549 ** performance using the full-scan without sorting if the output size
170551 ** degradation using the full-scan if the output size estimate is too large.
170554 ** Except, if the first solver() call generated a full-table scan in an outer
170555 ** loop then stop this analysis at the first full-scan, since the second
170556 ** solver() run might try to swap that full-scan for another in order to
170561 ** |-- SCAN t1 |-- SCAN t2
170562 ** |-- SEARCH t2 `-- SEARCH t1
170563 ** `-- SORT USING B-TREE
170568 ** |-- SEARCH t1 |-- SCAN t2 <--- bad!
170569 ** |-- SEARCH t2 `-- SEARCH t1
170570 ** `-- SORT USING B-TREE
170572 ** See test cases in test/whereN.test for the real-world query that
170580 for(i=0; i<pWInfo->nLevel; i++){
170581 WhereLoop *p = pWInfo->a[i].pWLoop;
170583 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 ) continue;
170584 if( (p->wsFlags & (WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0 ){
170585 u8 iTab = p->iTab;
170587 for(pLoop=pWInfo->pLoops; pLoop; pLoop=pLoop->pNextLoop){
170588 if( pLoop->iTab!=iTab ) continue;
170589 if( (pLoop->wsFlags & (WHERE_CONSTRAINT|WHERE_AUTO_INDEX))!=0 ){
170590 /* Auto-index and index-constrained loops allowed to remain */
170599 sqlite3WhereLoopPrint(pLoop, &pWInfo->sWC);
170602 pLoop->prereq = ALLBITS; /* Prevent 2nd solver() from using this one */
170614 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
170617 ** Return non-zero on success, if this query can be handled by this
170618 ** no-frills query planner. Return zero if this query needs the
170619 ** general-purpose query planner.
170633 pWInfo = pBuilder->pWInfo;
170634 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
170635 assert( pWInfo->pTabList->nSrc>=1 );
170636 pItem = pWInfo->pTabList->a;
170637 pTab = pItem->pSTab;
170639 if( pItem->fg.isIndexedBy || pItem->fg.notIndexed ){
170640 testcase( pItem->fg.isIndexedBy );
170641 testcase( pItem->fg.notIndexed );
170644 iCur = pItem->iCursor;
170645 pWC = &pWInfo->sWC;
170646 pLoop = pBuilder->pNew;
170647 pLoop->wsFlags = 0;
170648 pLoop->nSkip = 0;
170649 pTerm = whereScanInit(&scan, pWC, iCur, -1, WO_EQ|WO_IS, 0);
170650 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
170652 testcase( pTerm->eOperator & WO_IS );
170653 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
170654 pLoop->aLTerm[0] = pTerm;
170655 pLoop->nLTerm = 1;
170656 pLoop->u.btree.nEq = 1;
170658 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
170660 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
170662 assert( pLoop->aLTermSpace==pLoop->aLTerm );
170664 || pIdx->pPartIdxWhere!=0
170665 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
170667 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
170668 for(j=0; j<pIdx->nKeyCol; j++){
170670 while( pTerm && pTerm->prereqRight ) pTerm = whereScanNext(&scan);
170672 testcase( pTerm->eOperator & WO_IS );
170673 pLoop->aLTerm[j] = pTerm;
170675 if( j!=pIdx->nKeyCol ) continue;
170676 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
170677 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
170678 pLoop->wsFlags |= WHERE_IDX_ONLY;
170680 pLoop->nLTerm = j;
170681 pLoop->u.btree.nEq = j;
170682 pLoop->u.btree.pIndex = pIdx;
170684 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
170688 if( pLoop->wsFlags ){
170689 pLoop->nOut = (LogEst)1;
170690 pWInfo->a[0].pWLoop = pLoop;
170691 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
170692 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
170693 pWInfo->a[0].iTabCur = iCur;
170694 pWInfo->nRowOut = 1;
170695 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
170696 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
170697 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
170699 if( scan.iEquiv>1 ) pLoop->wsFlags |= WHERE_TRANSCONS;
170701 pLoop->cId = '0';
170717 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
170718 pWalker->eCode = 0;
170725 ** Return true if the expression contains no non-deterministic SQL
170726 ** functions. Do not consider non-deterministic SQL functions that are
170727 ** part of sub-select statements.
170750 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
170751 p->cId = zLabel[i%(sizeof(zLabel)-1)];
170771 ** 5) The table must not have an inner-join ON or USING clause if there is
170775 ** the right-most table of a subquery that was flattened into the
170776 ** main query and that subquery was the right-hand operand of an
170779 ** 7) The omit-noop-join optimization is enabled.
170810 assert( pWInfo->nLevel>=2 );
170811 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
170815 assert( pWInfo->pResultSet!=0 );
170816 assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
170818 tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
170819 if( pWInfo->pOrderBy ){
170820 tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
170822 hasRightJoin = (pWInfo->pTabList->a[0].fg.jointype & JT_LTORJ)!=0;
170823 for(i=pWInfo->nLevel-1; i>=1; i--){
170828 pLoop = pWInfo->a[i].pWLoop;
170829 pItem = &pWInfo->pTabList->a[pLoop->iTab];
170830 if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ) continue;
170831 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
170832 && (pLoop->wsFlags & WHERE_ONEROW)==0
170836 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
170837 pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
170838 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
170839 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
170840 if( !ExprHasProperty(pTerm->pExpr, EP_OuterON)
170841 || pTerm->pExpr->w.iJoin!=pItem->iCursor
170847 && ExprHasProperty(pTerm->pExpr, EP_InnerON)
170848 && NEVER(pTerm->pExpr->w.iJoin==pItem->iCursor)
170854 WHERETRACE(0xffffffff,("-> omit unused FROM-clause term %c\n",pLoop->cId));
170855 m1 = MASKBIT(i)-1;
170856 testcase( ((pWInfo->revMask>>1) & ~m1)!=0 );
170857 pWInfo->revMask = (m1 & pWInfo->revMask) | ((pWInfo->revMask>>1) & ~m1);
170858 notReady &= ~pLoop->maskSelf;
170859 for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
170860 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
170861 pTerm->wtFlags |= TERM_CODED;
170864 if( i!=pWInfo->nLevel-1 ){
170865 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
170866 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
170868 pWInfo->nLevel--;
170869 assert( pWInfo->nLevel>0 );
170883 ** (3) Bloom-filter processing is not disabled. (Checked by the
170898 assert( pWInfo->nLevel>=2 );
170899 assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_BloomFilter) );
170900 for(i=0; i<pWInfo->nLevel; i++){
170901 WhereLoop *pLoop = pWInfo->a[i].pWLoop;
170903 SrcItem *pItem = &pWInfo->pTabList->a[pLoop->iTab];
170904 Table *pTab = pItem->pSTab;
170905 if( (pTab->tabFlags & TF_HasStat1)==0 ) break;
170906 pTab->tabFlags |= TF_MaybeReanalyze;
170908 && (pLoop->wsFlags & reqFlags)==reqFlags
170909 /* vvvvvv--- Always the case if WHERE_COLUMN_EQ is defined */
170910 && ALWAYS((pLoop->wsFlags & (WHERE_IPK|WHERE_INDEXED))!=0)
170912 if( nSearch > pTab->nRowLogEst ){
170913 testcase( pItem->fg.jointype & JT_LEFT );
170914 pLoop->wsFlags |= WHERE_BLOOMFILTER;
170915 pLoop->wsFlags &= ~WHERE_IDX_ONLY;
170917 "-> use Bloom-filter on loop %c because there are ~%.1e "
170919 pLoop->cId, (double)sqlite3LogEstToInt(nSearch), pTab->zName,
170920 (double)sqlite3LogEstToInt(pTab->nRowLogEst)));
170923 nSearch += pLoop->nOut;
170933 ** This routine adds IndexedExpr entries to the Parse->pIdxEpr field for
170939 Parse *pParse, /* Add IndexedExpr entries to pParse->pIdxEpr */
170940 Index *pIdx, /* The index-on-expression that contains the expressions */
170947 assert( pIdx->bHasExpr );
170948 pTab = pIdx->pTable;
170949 for(i=0; i<pIdx->nColumn; i++){
170951 int j = pIdx->aiColumn[i];
170953 pExpr = pIdx->aColExpr->a[i].pExpr;
170954 }else if( j>=0 && (pTab->aCol[j].colFlags & COLFLAG_VIRTUAL)!=0 ){
170955 pExpr = sqlite3ColumnExpr(pTab, &pTab->aCol[j]);
170960 p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
170962 p->pIENext = pParse->pIdxEpr;
170965 sqlite3DebugPrintf("New pParse->pIdxEpr term {%d,%d}\n", iIdxCur, i);
170969 p->pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
170970 p->iDataCur = pTabItem->iCursor;
170971 p->iIdxCur = iIdxCur;
170972 p->iIdxCol = i;
170973 p->bMaybeNullRow = (pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT))!=0;
170974 if( sqlite3IndexAffinityStr(pParse->db, pIdx) ){
170975 p->aff = pIdx->zColAff[i];
170978 p->zIdxName = pIdx->zName;
170980 pParse->pIdxEpr = p;
170981 if( p->pIENext==0 ){
170982 void *pArg = (void*)&pParse->pIdxEpr;
170989 ** Set the reverse-scan order mask to one for all tables in the query
170998 for(ii=0; ii<pWInfo->pTabList->nSrc; ii++){
170999 SrcItem *pItem = &pWInfo->pTabList->a[ii];
171000 if( !pItem->fg.isCte
171001 || pItem->u2.pCteUse->eM10d!=M10d_Yes
171002 || NEVER(pItem->fg.isSubquery==0)
171003 || pItem->u4.pSubq->pSelect->pOrderBy==0
171005 pWInfo->revMask |= MASKBIT(ii);
171029 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
171033 ** end |-- by sqlite3WhereEnd()
171040 ** scanning through all values on the right-hand side of the IN.
171043 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
171053 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
171063 ** inner loops (or around the "..." if the test occurs within the inner-
171112 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
171116 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
171133 db = pParse->db;
171137 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
171138 if( pOrderBy && pOrderBy->nExpr>=BMS ){
171141 wctrlFlags |= WHERE_KEEP_ALL_JOINS; /* Disable omit-noop-join opt */
171147 testcase( pTabList->nSrc==BMS );
171148 if( pTabList->nSrc>BMS ){
171158 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
171163 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
171164 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
171169 if( db->mallocFailed ){
171174 pWInfo->pParse = pParse;
171175 pWInfo->pTabList = pTabList;
171176 pWInfo->pOrderBy = pOrderBy;
171178 pWInfo->pWhere = pWhere;
171180 pWInfo->pResultSet = pResultSet;
171181 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
171182 pWInfo->nLevel = nTabList;
171183 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
171184 pWInfo->wctrlFlags = wctrlFlags;
171185 pWInfo->iLimit = iAuxArg;
171186 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
171187 pWInfo->pSelect = pSelect;
171188 memset(&pWInfo->nOBSat, 0,
171189 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
171190 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
171191 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
171192 pMaskSet = &pWInfo->sMaskSet;
171193 pMaskSet->n = 0;
171194 pMaskSet->ix[0] = -99; /* Initialize ix[0] to a value that can never be
171196 ** test for pMaskSet->n==0 in sqlite3WhereGetMask() */
171198 sWLB.pWC = &pWInfo->sWC;
171203 sWLB.pNew->cId = '*';
171209 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
171210 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
171215 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
171219 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
171221 if( ALWAYS(pWInfo->pSelect)
171222 && (pWInfo->pSelect->selFlags & SF_MultiValue)==0
171229 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
171232 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
171236 ** Note that bitmasks are created for all pTabList->nSrc tables in
171238 ** equal to pTabList->nSrc but might be shortened to 1 if the
171243 createMask(pMaskSet, pTabList->a[ii].iCursor);
171244 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
171245 }while( (++ii)<pTabList->nSrc );
171249 for(ii=0; ii<pTabList->nSrc; ii++){
171250 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
171259 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
171260 if( pSelect && pSelect->pLimit ){
171261 sqlite3WhereAddLimit(&pWInfo->sWC, pSelect);
171263 if( pParse->nErr ) goto whereBeginError;
171265 /* The False-WHERE-Term-Bypass optimization:
171274 ** right-hand side of a LEFT or FULL JOIN.
171277 ** (4) If the expression contains non-deterministic functions
171278 ** that are not within a sub-select. This is not required
171282 ** WHERE random()>0; -- eval random() once per row
171283 ** WHERE (SELECT random())>0; -- eval random() just once overall
171291 for(ii=0; ii<sWLB.pWC->nBase; ii++){
171292 WhereTerm *pT = &sWLB.pWC->a[ii]; /* A term of the WHERE clause */
171294 if( pT->wtFlags & TERM_VIRTUAL ) continue;
171295 pX = pT->pExpr;
171297 assert( pT->prereqAll!=0 || !ExprHasProperty(pX, EP_OuterON) );
171298 if( pT->prereqAll==0 /* Conditions (1) and (2) */
171301 && (pTabList->a[0].fg.jointype & JT_LTORJ)!=0 )
171303 sqlite3ExprIfFalse(pParse, pX, pWInfo->iBreak, SQLITE_JUMPIFNULL);
171304 pT->wtFlags |= TERM_CODED;
171313 pWInfo->wctrlFlags &= ~WHERE_WANT_DISTINCT;
171314 }else if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
171316 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
171319 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
171320 pWInfo->pOrderBy = pResultSet;
171343 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
171364 while( pWInfo->pLoops ){