• Home
  • Raw
  • Download

Lines Matching +full:stale +full:- +full:pr +full:- +full:message

17 ** language. The code for the "sqlite3" command-line shell is also in a
38 ** This file implements routines used to report what compile-time options
42 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS /* IMP: R-16824-07538 */
46 ** autoconf-based build
60 ** comma. e.g. (-DSQLITE_DEFAULT_LOOKASIDE="100,100") */
65 ** An array of names of all compile-time options. This array should
66 ** be sorted A-Z.
69 ** only a handful of compile-time options, so most times this array is usually
105 "COMPILER=clang-" CTIMEOPT_VAL(__clang_major__) "."
109 "COMPILER=msvc-" CTIMEOPT_VAL(_MSC_VER),
111 "COMPILER=gcc-" __VERSION__,
816 ** NO_TEST - The branches on this line are not
821 ** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
825 ** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
829 ** PREVENTS-HARMLESS-OVERREAD - This branch prevents a buffer overread
834 ** slash-asterisk...asterisk-slash comment marks, with no spaces between the
905 ** 2015-03-02
943 ** large file support, or if the OS is windows, these should be no-ops.
949 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
958 ** on 2008-11-28.) These days, all Linux kernels support large files, so
975 ** SQLITE_DISABLE_INTRINSIC macro means do not use any compiler-specific
982 ** so the GCC_VERSION macro will be set to a correct non-zero value even
1035 ** some MinGW-specific macros). When compiling for MinGW, either the
1065 ** 2001-09-15
1076 ** presents to client programs. If a C-function, structure, datatype,
1087 ** The official C-language API documentation for SQLite is derived
1134 ** These no-op macros are used in front of interfaces to mark those
1136 ** should not use deprecated interfaces - they are supported for backwards
1160 ** CAPI3REF: Compile-Time Library Version Numbers
1176 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
1178 ** a string which identifies a particular check-in of SQLite
1180 ** string contains the date and time of the check-in (UTC) and a SHA1
1181 ** or SHA3-256 hash of the entire source tree. If the source code has
1191 #define SQLITE_SOURCE_ID "2021-04-02 15:20:15 5d4c65779dab868b285519b19e4cf9d451d50c6048f06f65…
1194 ** CAPI3REF: Run-Time Library Version Numbers
1231 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
1240 ** returning the N-th compile time option string. ^If N is out of range,
1265 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
1283 ** This interface only reports on the compile-time mutex setting
1289 ** sqlite3_threadsafe() function shows only the compile-time setting of
1290 ** thread safety, not any run-time changes to that setting made by
1315 ** CAPI3REF: 64-Bit Integer Types
1318 ** Because there is no cross-platform way to specify 64-bit integer types
1319 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
1326 ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
1349 ** substitute integer for floating-point.
1391 ** argument is a harmless no-op.
1404 ** CAPI3REF: One-Step Query Execution Interface
1412 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
1413 ** semicolon-separate SQL statements passed into its 2nd argument,
1426 ** is not NULL then any error message is written into memory obtained
1429 ** on error message strings returned through the 5th parameter of
1430 ** sqlite3_exec() after the error message string is no longer needed.
1435 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
1485 /* beginning-of-error-codes */
1516 /* end-of-error-codes */
1524 ** these result codes are too coarse-grained. They do not provide as
1669 ** read-only media and cannot be changed even by processes with
1727 ** (Third-party VFS implementations might also make the distinction
1762 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
1795 ** locking strategy (for example to use dot-file locks), to inquire
1796 ** about the status of a lock, or to break stale locks. The SQLite
1842 ** fails to zero-fill short reads might seem to work. However,
1843 ** failure to zero-fill short reads will eventually lead to
1889 ** compile-time option is used.
1900 ** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
1902 ** of the in-memory database. The argument is a pointer to a [sqlite3_int64].
1912 ** point to an integer (type int) containing the new chunk-size to use
1914 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
1925 ** the [rollback journal] or the [write-ahead log]) for a particular database
1938 ** this file-control is NULL. However, if the database file is being synced
1939 ** as part of a multi-database commit, the argument points to a nul-terminated
1940 ** string containing the transactions super-journal file name. VFSes that
1957 ** anti-virus programs. By default, the windows VFS will retry file read,
1983 ** WAL mode. If the integer is -1, then it is overwritten with the current
1988 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1992 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1993 ** mode. If the integer is -1, then it is overwritten with the current
1994 ** zero-damage mode setting.
2005 ** final bottom-level VFS are written into memory obtained from
2009 ** all file-control actions, there is no guarantee that this will actually
2011 ** pointer in case this file-control is not implemented. This file-control
2015 ** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
2019 ** to a pointer to the top-level VFS.)^
2021 ** upper-most shim only.
2034 ** the error message if the pragma fails. ^If the
2038 ** VFS has handled the PRAGMA itself and the parser generates a no-op
2040 ** of the result string if the string is non-NULL.
2046 ** it is able to override built-in [PRAGMA] statements.
2050 ** file-control may be invoked by SQLite on the database file handle
2052 ** to the connection's busy-handler callback. The argument is of type (void**)
2053 ** - an array of two (void *) values. The first (void *) actually points
2055 ** busy-handler, this function should be invoked with the second (void *) in
2056 ** the array as the only argument. If it returns non-zero, then the operation
2061 ** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
2071 ** maximum number of bytes that will be used for memory-mapped I/O.
2077 ** file-control is used internally to implement [PRAGMA mmap_size].
2083 ** The argument is a zero-terminated string. Higher layers in the
2085 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
2110 ** Applications should <em>not</em> use this file-control.
2159 ** The parameter is a pointer to a 32-bit signed integer that contains
2160 ** the value that M is to be set to. Before returning, the 32-bit signed
2165 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
2191 ** file to the database file, but before the *-shm file is updated to
2290 ** in a thread-safe way. The [sqlite3_vfs_find()] interface
2308 ** consist of a single "-" character followed by no more than
2309 ** 11 alphanumeric and/or "-" characters.
2324 ** If xOpen() opens a file read-only then it sets *pOutFlags to
2344 ** the open of a journal file a no-op. Writes to this journal would
2345 ** also be no-ops, and any attempt to read the journal would return
2347 ** file will be doing page-aligned sector reads and writes in a random
2386 ** flag is never actually used and is not implemented in the built-in
2389 ** non-zero error code if there is an I/O error or if the name of
2391 ** is returned, then non-zero or zero is written into *pResOut to indicate
2405 ** of good-quality randomness into zOut. The return value is
2413 ** a 24-hour day).
2439 void *pAppData; /* Pointer to application-specific data */
2549 ** are harmless no-ops.)^
2554 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
2577 ** compile-time option, then the automatic calls to sqlite3_initialize()
2586 ** The sqlite3_os_init() routine does operating-system specific
2602 ** (using the [SQLITE_OS_OTHER=1] compile-time
2604 ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
2633 ** implementation of an application-defined [sqlite3_os_init()].
2643 ** then this routine returns a non-zero [error code].
2657 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
2670 ** and low-level memory allocation routines.
2682 ** Note that SQLite comes with several [built-in memory allocators]
2687 ** memory allocator that simulates memory out-of-memory conditions in
2752 ** non-zero [error code] if a discontinued or unsupported configuration option
2758 ** [threading mode] to Single-thread. In other words, it disables
2761 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2763 ** value of Single-thread and so [sqlite3_config()] will return
2769 ** [threading mode] to Multi-thread. In other words, it disables
2773 ** are enabled so that SQLite will be safe to use in a multi-threaded
2776 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2777 ** it is not possible to set the Multi-thread [threading mode] and
2792 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2801 ** alternative low-level memory allocation routines to be used in place of
2829 ** disabled, the following SQLite interfaces become non-operational:
2850 ** This configuration option is a no-op if an application-defined page
2853 ** 8-byte aligned memory (pMem), the size of each page cache line (sz),
2861 ** argument must be either a NULL pointer or a pointer to an 8-byte
2868 ** ^If pMem is NULL and N is non-zero, then each database connection
2871 ** of -1024*N bytes if N is negative, . ^If additional
2884 ** An 8-byte aligned pointer to the memory,
2891 ** The first pointer (the memory pointer) must be aligned to an 8-byte
2899 ** The argument specifies alternative low-level mutex routines to be used
2903 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2916 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
2949 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
2951 ** passed through as the first parameter to the application-defined logger
2956 ** log message after formatting via [sqlite3_snprintf()].
2959 ** In a multi-threaded application, the application-defined logger
2964 ** If non-zero, then URI handling is globally enabled. If the parameter is zero,
2981 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
2982 ** if that compile-time option is omitted.
2992 ** They are retained for backwards compatibility but are now no-ops.
2998 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
3014 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
3021 ** compile-time maximum mmap size set by the
3022 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
3024 ** changed to its compile-time default.
3029 ** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
3030 ** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
3046 ** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched
3055 ** becomes the [statement journal] spill-to-disk threshold.
3058 ** Or if the threshold is -1, statement journals are always held
3064 ** [SQLITE_STMTJRNL_SPILL] compile-time option.
3069 ** of type (int) - the new value of the sorter-reference size threshold.
3073 ** of a table column that its values are likely to be very large - larger
3074 ** than the configured sorter-reference size threshold - then a reference
3080 ** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
3085 ** [sqlite3_int64] parameter which is the default maximum size for an in-memory
3088 ** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control]. If this
3090 ** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
3091 ** compile-time option is not set, then the default maximum is 1073741824.
3107 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
3108 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
3134 ** non-zero [error code] if a discontinued or unsupported configuration option
3150 ** must be aligned to an 8-byte boundary. ^If the second argument to
3186 ** triggers in the main database schema or in the schemas of ATTACH-ed
3203 ** views in the main database schema or in the schemas of ATTACH-ed
3210 ** [FTS3] full-text search engine extension.
3225 ** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
3227 ** When the first argument to this interface is 1, then only the C-API is
3229 ** this interface is 0, then both the C-API and the SQL function are disabled.
3230 ** If the first argument is -1, then no changes are made to state of either the
3231 ** C-API or the SQL function.
3254 ** is an integer - positive to disable checkpoints-on-close, or zero (the
3257 ** into which is written 0 or 1 to indicate whether checkpoints-on-close
3258 ** have been disabled - 0 if they are not disabled, 1 if they are.
3281 ** behavior. The first parameter passed to this operation is an integer -
3285 ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
3338 ** behaves as it did prior to [version 3.24.0] (2018-06-04). See the
3347 ** the legacy [double-quoted string literal] misfeature for DML statements
3349 ** default value of this setting is determined by the [-DSQLITE_DQS]
3350 ** compile-time option.
3356 ** the legacy [double-quoted string literal] misfeature for DDL statements,
3358 ** default value of this setting is determined by the [-DSQLITE_DQS]
3359 ** compile-time option.
3386 ** created database file to have a schema format version number (the 4-byte
3440 ** has a unique 64-bit signed
3518 ** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
3540 ** any changes performed by sub-triggers, as the sqlite3_changes()
3541 ** value will be saved and restored after each sub-trigger has run.)^
3603 ** CAPI3REF: Interrupt A Long-Running Query
3609 ** or Ctrl-C where the user wants a long query operation to halt
3634 ** SQL statements is a no-op and has no effect on SQL statements
3642 ** These routines are useful during command-line input to determine if the
3648 ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
3663 ** then the return value from sqlite3_complete16() will be non-zero
3666 ** The input to [sqlite3_complete()] must be a zero-terminated
3667 ** UTF-8 string.
3669 ** The input to [sqlite3_complete16()] must be a zero-terminated
3670 ** UTF-16 string in native byte order.
3677 ** KEYWORDS: {busy-handler callback} {busy handler}
3699 ** ^If the callback returns non-zero, then another attempt
3775 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
3777 ** to zero-terminated strings that contain the names of the columns.
3779 ** in NULL pointers. All other values are in their UTF-8 zero-terminated
3791 ** -----------------------
3813 ** semicolon-separated SQL statements in the zero-terminated UTF-8
3845 ** These routines are work-alikes of the "printf()" family of functions
3849 ** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
3850 ** See the [built-in printf()] documentation for details.
3873 ** guarantees that the buffer is always zero-terminated. ^The first
3876 ** written will be n-1 characters.
3880 ** See also: [built-in printf()], [printf() SQL function]
3892 ** does not include operating-system specific [VFS] implementation. The
3903 ** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
3904 ** of a signed 32-bit integer.
3909 ** a no-op if is called with a NULL pointer. Passing a NULL pointer
3914 ** might result if sqlite3_free() is called with a non-NULL pointer that
3934 ** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
3935 ** of a 32-bit signed integer.
3950 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
3974 ** routines, which form the built-in memory allocation subsystem.
3979 ** value of [sqlite3_memory_used()] since the high-water mark
3986 ** ^The memory high-water mark is reset to the current value of
3989 ** by [sqlite3_memory_highwater(1)] is the high-water mark
3996 ** CAPI3REF: Pseudo-Random Number Generator
3998 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
4001 ** the built-in random() and randomblob() SQL functions. This interface allows
4012 ** non-NULL P then the pseudo-randomness is generated
4019 ** CAPI3REF: Compile-Time Authorization Callbacks
4038 ** the authorizer will fail with an error message.
4043 ** authorizer will fail with an error message explaining that
4050 ** to the callback are either NULL pointers or zero-terminated strings
4078 ** user-entered SQL is being [sqlite3_prepare | prepared] that
4097 ** statement might be re-prepared during [sqlite3_step()] due to a
4143 ** is the name of the inner-most trigger or view that is responsible for
4145 ** top-level SQL code.
4195 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
4199 ** contain a UTF-8 SQL comment that identifies the trigger.)^
4201 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
4206 ** the original statement text and an estimate of wall-clock time
4226 ** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
4248 ** interface by using the X argument when X begins with "--" and invoking
4255 ** X argument points to a 64-bit integer which is the estimated of
4285 ** M argument should be the bitwise OR-ed combination of
4335 ** ^If the progress callback returns non-zero, the operation is
4352 ** filename argument. ^The filename argument is interpreted as UTF-8 for
4353 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
4364 ** ^The default encoding will be UTF-8 for databases created using
4366 ** created using sqlite3_open16() will be UTF-16 in the native byte order.
4380 ** <dd>The database is opened in read-only mode. If the database does not
4402 ** <dd>The database will be opened as an in-memory database. The database
4403 ** is named by the "filename" argument for the purposes of cache-sharing,
4408 ** <dd>The new database connection will use the "multi-thread"
4444 ** ^If the filename is ":memory:", then a private, temporary in-memory database
4445 ** is created for the connection. ^This in-memory database will vanish when
4453 ** on-disk database will be created. ^This private database will be
4463 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
4486 ** SQLite and its built-in [VFSes] interpret the
4501 ** ^If "ro" is specified, then the database is opened for read-only
4504 ** "rw", then the database is opened for read-write (but not create)
4508 ** set to "memory" then a pure [in-memory database] that never reads
4535 ** read-only media. ^When immutable is set, SQLite assumes that the
4537 ** privilege, and so the database is opened read-only and all locking
4562 ** <tr><td style="white-space:nowrap">
4566 ** necessary - space characters can be used literally
4569 ** Open file "data.db" in the current directory for read-only access.
4570 ** Regardless of whether or not shared-cache mode is enabled by
4572 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
4573 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
4574 ** that uses dot-files in place of posix advisory locking.
4582 ** percent sign - "%" - followed by exactly two hexadecimal digits
4584 ** URI filename are interpreted, they are encoded using UTF-8 and all
4586 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
4590 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
4592 ** characters must be converted to UTF-8 prior to passing them into
4602 const char *filename, /* Database filename (UTF-8) */
4606 const void *filename, /* Database filename (UTF-16) */
4610 const char *filename, /* Database filename (UTF-8) */
4647 ** case or if the value begins with a non-zero number. The
4655 ** 64-bit signed integer and returns that integer, or D if P does not
4660 ** the value) of the N-th query parameter for filename F, or a NULL
4662 ** parameters minus 1. The N value is zero-based so N should be 0 to obtain
4721 ** ^If X is the name of a rollback or WAL-mode journal file that is
4727 ** only. It is not a general-purpose interface.
4771 ** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
4807 ** change the value of the error code. The error-code preserving
4817 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4818 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
4819 ** ^(Memory to hold the error message string is managed internally.
4824 ** ^The sqlite3_errstr() interface returns the English-language text
4825 ** that describes the [result code], as UTF-8.
4826 ** ^(Memory to hold the error message string is managed internally
4841 ** error code and message may or may not be set.
4861 ** The life-cycle of a prepared statement object usually goes like this:
4876 ** CAPI3REF: Run-time Limits
4889 ** set at compile-time by a C preprocessor macro called
4898 ** simply invoke this interface with the third parameter set to -1.
4900 ** Run-time limits are intended for use in applications that manage
4913 ** New run-time limit categories may be added in future releases.
4918 ** CAPI3REF: Run-Time Limit Categories
4922 ** that can be lowered at run-time using [sqlite3_limit()].
5008 ** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
5031 ** To execute an SQL statement, it must first be compiled into a byte-code
5040 ** The use of the UTF-8 interfaces is preferred, as SQLite currently
5041 ** does all parsing using UTF-8. The UTF-16 interfaces are provided
5042 ** as a convenience. The UTF-16 interfaces work by converting the
5043 ** input text into UTF-8, then invoking the corresponding UTF-8 interface.
5050 ** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(),
5052 ** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
5053 ** and sqlite3_prepare16_v3() use UTF-16.
5059 ** If the caller knows that the supplied string is nul-terminated, then
5062 ** the nul-terminator.
5112 ** ^The specific value of a WHERE-clause [parameter] might influence the
5113 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
5115 ** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
5127 const char *zSql, /* SQL statement, UTF-8 encoded */
5134 const char *zSql, /* SQL statement, UTF-8 encoded */
5141 const char *zSql, /* SQL statement, UTF-8 encoded */
5149 const void *zSql, /* SQL statement, UTF-16 encoded */
5156 const void *zSql, /* SQL statement, UTF-16 encoded */
5163 const void *zSql, /* SQL statement, UTF-16 encoded */
5174 ** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
5178 ** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
5181 ** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
5197 ** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
5198 ** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time
5216 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
5220 ** Note that [application-defined SQL functions] or
5224 ** change the database file through side-effects:
5264 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
5301 ** sqlite3_value object. If SQLite is compiled to be single-threaded
5312 ** implementation of [application-defined SQL functions] are protected.
5328 ** is always first parameter to [application-defined SQL functions].
5329 ** The application-defined SQL function implementation will pass this
5379 ** it should be a pointer to well-formed UTF8 text.
5381 ** it should be a pointer to well-formed UTF16 text.
5383 ** it should be a pointer to a well-formed unicode string that is
5387 ** [[byte-order determination rules]] ^The byte-order of
5388 ** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
5405 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
5441 ** ^A negative value for the zeroblob results in a zero-length BLOB.
5443 ** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
5515 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
5526 ** ^If the value N is out of range or if the N-th parameter is
5528 ** always in UTF-8 encoding even if the named parameter was
5529 ** originally specified as UTF-16 in [sqlite3_prepare16()],
5546 ** name must be given in UTF-8 even if the original statement
5547 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
5588 ** interface returns a pointer to a zero-terminated UTF-8 string
5589 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
5590 ** UTF-16 string. ^The first parameter is the [prepared statement]
5601 ** (for example during a conversion from UTF-8 to UTF-16) then a
5620 ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
5629 ** ^The names returned are the original un-aliased names of the
5635 ** ^The left-most column is column 0 for these routines.
5644 ** UTF-16 encoded strings and the other functions return UTF-8.
5647 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
5671 ** ^The returned string is always UTF-8 encoded.
5684 ** ^SQLite uses dynamic run-time typing. ^So just because a column
5734 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
5760 ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
5790 ** will return non-zero if previous call to [sqlite3_step](P) returned
5792 ** where it always returns zero since each step of that multi-step
5806 ** <li> 64-bit signed integer
5807 ** <li> 64-bit IEEE floating point number
5840 ** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
5841 ** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
5842 ** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
5843 ** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
5848 ** or a UTF-8 TEXT result in bytes
5850 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5902 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
5904 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
5905 ** the string to UTF-8 and then returns the number of bytes.
5907 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
5911 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
5913 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
5914 ** the string to UTF-16 and then returns the number of bytes.
5916 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
5927 ** even empty strings, are always zero-terminated. ^The return
5928 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
5940 ** [application-defined SQL functions] or [virtual tables], not within
5941 ** top-level application code.
5959 ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
5980 ** sqlite3_column_text16() is called. A zero-terminator might
5982 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
5984 ** to UTF-16.</li>
5985 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
5987 ** to UTF-8.</li>
5990 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
6021 ** fail if an out-of-memory error occurs during a format conversion.
6022 ** Only the following subset of interfaces are subject to out-of-memory
6033 ** If an out-of-memory error occurs, then the return value from these
6035 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6069 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
6084 ** object back to its initial state, ready to be re-executed.
6123 ** connection then application-defined SQL functions must be added
6127 ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
6128 ** representation, exclusive of the zero-terminator. ^Note that the name
6129 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
6135 ** aggregate takes. ^If this parameter is -1, then the SQL function or
6138 ** parameter is less than -1 or greater than 127 then the behavior is
6157 ** deterministic. The built-in [random()] SQL function is an example of a
6167 ** <span style="background-color:#ffff90;">
6169 ** all application-defined SQL functions that do not need to be
6184 ** pointers to C-language functions that implement the SQL function or
6194 ** C-language callbacks that implement the new function. xStep and xFinal
6195 ** must both be non-NULL. xValue and xInverse may either both be NULL, in
6197 ** non-NULL, in which case the new function may be used as either an aggregate
6200 ** [user-defined window functions|available here].
6215 ** SQL function is used. ^A function implementation with a non-negative
6224 ** ^Built-in functions may be overloaded by new application-defined functions.
6226 ** ^An application-defined function is permitted to call other
6281 #define SQLITE_UTF8 1 /* IMP: R-37514-35566 */
6282 #define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */
6283 #define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */
6310 ** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
6314 ** for all [application-defined SQL functions], and especially for functions
6315 ** that have side-effects or that could potentially leak sensitive
6335 ** the function is tagged with SQLITE_INNOCUOUS. Most built-in functions
6337 ** SQLITE_INNOCUOUS flag for application-defined functions unless the
6339 ** security-adverse side-effects and information-leaks.
6344 ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
6346 ** functions. However, if it is not specified for a user-defined window
6347 ** function, then any sub-types belonging to arguments passed to the window
6386 ** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
6387 ** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
6389 ** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
6390 ** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
6392 ** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
6393 ** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
6396 ** or a UTF-8 TEXT in bytes
6398 ** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
6416 ** implement [application-defined SQL functions] and [virtual tables].
6426 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
6427 ** in the native byte-order of the host machine. ^The
6429 ** extract UTF-16 strings as big-endian and little-endian respectively.
6470 ** ^The sqlite3_value_frombind(X) interface returns non-zero if the
6485 ** fail if an out-of-memory error occurs during a format conversion.
6486 ** Only the following subset of interfaces are subject to out-of-memory
6499 ** If an out-of-memory error occurs, then the return value from these
6501 ** Valid SQL NULL returns can be distinguished from out-of-memory errors
6527 ** an [application-defined SQL function] argument V. The subtype
6546 ** then sqlite3_value_free(V) is a harmless no-op.
6607 ** the application-defined function is running.
6627 ** These functions may be used by (non-aggregate) SQL functions to
6631 ** of where this might be useful is in a regular-expression matching
6640 ** value to the application-defined function. ^N is zero for the left-most
6645 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6646 ** argument of the application-defined function. ^Subsequent
6670 ** function parameters that are compile-time constants, including literal
6673 ** The value of the N parameter to these interfaces should be non-negative.
6700 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
6716 ** an application-defined function to be the BLOB whose content is pointed
6721 ** interfaces set the result of the application-defined function to be
6725 ** an application-defined function to be a floating point value specified
6732 ** as the text of an error message. ^SQLite interprets the error
6733 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
6734 ** interprets the string from sqlite3_result_error16() as UTF-16 using
6735 ** the same [byte-order determination rules] as [sqlite3_bind_text16()].
6738 ** message all text up through the first zero character.
6740 ** sqlite3_result_error16() is non-negative then SQLite takes that many
6741 ** bytes (not characters) from the 2nd parameter as the error message.
6743 ** routines make a private copy of the error message text before
6758 ** of the application-defined function to be the 32-bit signed integer
6761 ** of the application-defined function to be the 64-bit signed integer
6765 ** of the application-defined function to be NULL.
6769 ** set the return value of the application-defined function to be
6770 ** a text string which is represented as UTF-8, UTF-16 native byte order,
6771 ** UTF-16 little endian, or UTF-16 big endian, respectively.
6773 ** application-defined function to be a text string in an encoding
6782 ** is non-negative, then as many bytes (not characters) of the text
6783 ** pointed to by the 2nd parameter are taken as the application-defined
6784 ** function result. If the 3rd parameter is non-negative, then it
6791 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
6807 ** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
6809 ** byte-order specified by the BOM. ^The byte-order specified by
6810 ** the BOM at the beginning of the text overrides the byte-order
6813 ** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
6824 ** the application-defined function to be a copy of the
6835 ** also associates the host-language pointer P or type T with that
6837 ** [application-defined SQL function] using [sqlite3_value_pointer()].
6845 ** than the one containing the application-defined function that received
6877 ** the result from the [application-defined SQL function] with
6893 ** ^The name of the collation is a UTF-8 string
6895 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
6958 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
7001 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
7002 ** the names are passed as UTF-16 in machine native byte order.
7003 ** ^A call to either function replaces the existing collation-needed callback.
7062 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
7104 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
7105 ** &nbsp; TemporaryFolder->Path->Data();
7108 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
7121 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
7160 ** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
7165 ** the current directory on the sub-platforms of Win32 where that concept is
7169 ** UTF-8 or UTF-16, respectively.
7188 ** CAPI3REF: Test For Auto-Commit Mode
7192 ** ^The sqlite3_get_autocommit() interface returns non-zero or
7196 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
7198 ** If certain kinds of errors occur on a statement within a multi-statement
7231 ** connection D, or if database N is a temporary or in-memory database, then
7236 ** is [DETACH]-ed or until the database connection closes.
7257 ** CAPI3REF: Determine if a database is read-only
7261 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
7280 ** a valid schema, then -1 is returned.
7346 ** ^If the callback on a commit hook function returns non-zero,
7368 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
7370 ** hook returning non-zero, just as it would be with any other rollback.
7469 ** shared cache mode should be enabled per-database connection via
7473 ** 32-bit integer is atomic.
7475 ** See Also: [SQLite Shared-Cache Mode]
7483 ** of heap memory by deallocating non-essential memory allocations
7485 ** pages to improve performance is an example of non-essential memory.
7488 ** ^The sqlite3_release_memory() routine is a no-op returning zero
7502 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
7537 ** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
7561 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
7562 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
7595 ** interface returns SQLITE_OK and fills in the non-NULL pointers in
7599 ** ^If the column-name parameter to sqlite3_table_column_metadata() is a
7666 int *pAutoinc /* OUTPUT: True if column is auto-increment */
7678 ** with various operating-system specific extensions added.
7687 ** X is consists of the lower-case equivalent of all ASCII alphabetic
7694 ** fill *pzErrMsg with error message text stored in memory
7717 char **pzErrMsg /* Put error message here if not 0 */
7726 ** [extension loading] while evaluating user-entered SQL, the following API
7734 ** ^This interface enables or disables both the C-API
7737 ** to enable or disable only the C-API.)^
7769 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
7777 ** on the list of automatic extensions is a harmless no-op. ^No entry point
7806 ** The interface to the virtual-table mechanism is currently considered
7810 ** When the virtual-table mechanism stabilizes, we will declare the
7884 ** inputs to xBestIndex and are read-only. xBestIndex inserts its
7896 ** expr on the right-hand side can be evaluated (and thus the constraint
7911 ** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
7917 ** non-zero.
7921 ** the right-hand side of the corresponding aConstraint[] is evaluated
7922 ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
7951 ** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
7982 int iColumn; /* Column constrained. -1 for ROWID */
7985 int iTermOffset; /* Used internally - xBestIndex should ignore */
8117 ** ^Virtual tables methods can set an error message by assigning a
8120 ** prior to assigning a new string to zErrMsg. ^After the error message
8127 char *zErrMsg; /* Error message from sqlite3_mprintf() */
8183 ** The interface to the virtual-table mechanism defined above (back up
8188 ** When the virtual-table mechanism stabilizes, we will declare the
8225 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
8227 ** read-only access.
8252 ** [database connection] error code and message accessible via
8263 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
8279 ** and the built-in [zeroblob] SQL function may be used to create a
8280 ** zero-filled blob to read or write using the incremental-blob interface.
8310 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
8320 ** ^This function sets the database handle error code and message.
8332 ** ^If the blob handle being closed was opened for read-write access, and if
8333 ** the database is in auto-commit mode and there are no other open read-write
8341 ** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8368 ** caller-supplied buffer. N bytes of data are copied into buffer Z
8397 ** caller-supplied buffer. N bytes of data are copied from the buffer Z
8403 ** [database connection] error code and message accessible via
8446 ** ^Names are zero-terminated UTF-8 strings.
8477 ** is selected automatically at compile-time. The following
8488 ** a single-threaded application. The SQLITE_MUTEX_PTHREADS and
8493 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
8530 ** cases where it really needs one. If a faster non-recursive mutex
8577 ** behave as no-ops.
8590 ** An instance of this structure defines the low-level routines
8640 ** xMutexInit() must be no-ops.
8686 ** the routine should return 1. This seems counter-intuitive since
8690 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
8706 ** next. Applications that override the built-in mutex logic must be
8722 #define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */
8737 ** ^If the [threading mode] is Single-thread or Multi-thread then this
8743 ** CAPI3REF: Low-Level Control Of Database Files
8853 ** by enclosing in double-quotes) so as not to confuse the parser.
8858 ** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
8861 ** zero-terminated. The sqlite3_keyword_name(N,Z,L) routine returns
8867 ** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
8879 ** <li> Put all identifier names inside double-quotes. This is the official
8890 ** compile-time options. For example, "VACUUM" is not a keyword if
8891 ** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option. Also,
8902 ** An instance of the sqlite3_str object contains a dynamically-sized
8926 ** valid [sqlite3_str] object, though in the event of an out-of-memory
8966 ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
8971 ** onto the end of the [sqlite3_str] object X. N must be non-negative.
8972 ** S must contain at least N non-zero bytes of content. To append a
8973 ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
8977 ** zero-terminated string S onto the end of [sqlite3_str] object X.
8980 ** single-byte character C onto the end of [sqlite3_str] object X.
9006 ** [SQLITE_NOMEM] following any out-of-memory error, or
9013 ** zero-termination byte.
9048 ** SQLITE_OK on success and a non-zero [error code] on failure.
9051 ** be represented by a 32-bit integer, then the values returned by
9069 ** These integer constants designate various run-time status parameters
9077 ** and internal memory usage by the SQLite library. Auxiliary page-cache
9162 ** non-zero [error code] on failure.
9178 ** The [sqlite3_db_status()] interface will return a non-zero error code
9188 ** satisfied using lookaside memory. Only the high-water value is meaningful;
9196 ** Only the high-water value is meaningful;
9204 ** Only the high-water value is meaningful;
9227 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
9267 ** to disk all at once. When pages spill mid-transaction, that introduces
9338 ** A non-zero value in this counter may indicate an opportunity to
9344 ** A non-zero value in this counter may indicate an opportunity to
9431 ** The built-in page cache is recommended for most uses.
9446 ** built-in default page cache is used instead of the application defined
9478 ** false if it is used for an in-memory database. The cache implementation
9489 ** suggested maximum cache-size (number of pages stored by) the cache
9535 ** as its second argument. If the third parameter, discard, is non-zero,
9569 ** is not obligated to free any memory, but well-behaved implementations should
9628 ** for copying in-memory databases to or from persistent files.
9634 ** ^The source database is read-locked only while it is being read;
9668 ** there is already a read or read-write transaction open on the
9672 ** returned and an error code and error message are stored in the
9674 ** ^The error code and message for the failed call to sqlite3_backup_init()
9700 ** <li> the destination database was opened read-only, or
9701 ** <li> the destination database is using write-ahead-log journaling
9703 ** <li> the destination database is an in-memory database and the
9707 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
9708 ** the [sqlite3_busy_handler | busy-handler function]
9710 ** busy-handler returns non-zero before the lock is available, then
9731 ** sqlite3_backup_step(), the source database may be modified mid-way
9748 ** active write-transaction on the destination database is rolled back.
9755 ** ^If an out-of-memory condition or IO error occurred during any prior
9824 ** ^When running in shared-cache mode, a database operation may fail with
9825 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
9826 ** individual tables within the shared-cache cannot be obtained. See
9827 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
9831 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
9835 ** ^Shared-cache locks are released when a database connection concludes
9839 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
9849 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
9855 ** ^If the blocked connection is attempting to obtain a write-lock on a
9856 ** shared-cache table, and more than one other connection currently holds
9857 ** a read-lock on the same table, then SQLite arbitrarily selects one of
9860 ** ^(There may be at most one unlock-notify callback registered by a
9862 ** blocked connection already has a registered unlock-notify callback,
9865 ** unlock-notify callback is canceled. ^The blocked connections
9866 ** unlock-notify callback may also be canceled by closing the blocked
9869 ** The unlock-notify callback is not reentrant. If an application invokes
9870 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
9878 ** When an unlock-notify callback is registered, the application provides a
9882 ** an unlock-notify callback is a pointer to an array of void* pointers,
9886 ** more than one blocked connection that has registered for an unlock-notify
9896 ** Assuming that after registering for an unlock-notify callback a
9907 ** unlock-notify callback is registered. The system is said to be in
9908 ** a deadlocked state if connection A has registered for an unlock-notify
9910 ** B has itself registered for an unlock-notify callback when connection
9913 ** registered for an unlock-notify callback on the conclusion of connection
9925 ** sqlite3_unlock_notify() results in the unlock-notify callback being
9926 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
9946 ** and extensions to compare the contents of two buffers containing UTF-8
9947 ** strings in a case-independent fashion, using the same definition of "case
9963 ** Note that this routine returns zero on a match and non-zero if the strings
9980 ** insensitive - equivalent upper and lower case ASCII characters match
9986 ** Note that this routine returns zero on a match and non-zero if the strings
9996 ** ^The [sqlite3_log()] interface writes a message into the [error log]
10009 ** will not use dynamically allocated memory. The log message is stored in
10010 ** a fixed-length buffer on the stack. If the log message is longer than
10017 ** CAPI3REF: Write-Ahead Log Commit Hook
10024 ** the associated write-lock on the database released)^, so the implementation
10030 ** ^The third parameter is the name of the database that was written to -
10031 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
10032 ** is the number of pages currently in the write-ahead log file,
10043 ** A single database handle may have at most a single write-ahead log callback
10045 ** previously registered write-ahead log callback. ^Note that the
10057 ** CAPI3REF: Configure an auto-checkpoint
10064 ** more frames in the [write-ahead log] file. ^Passing zero or
10079 ** ^Every new [database connection] defaults to having the auto-checkpoint
10095 ** [write-ahead log] for database X on [database connection] D to be
10096 ** transferred into the database file and for the write-ahead log to
10122 ** in the log were checkpointed. ^The [busy-handler callback]
10129 ** [sqlite3_busy_handler|busy-handler callback]) until there is no
10138 ** [busy-handler callback])
10151 ** the log file or to -1 if the checkpoint could not run because
10155 ** was called) or to -1 if the checkpoint could not run due to an error or
10163 ** busy-handler configured, it will not be invoked in this case.
10167 ** obtained immediately, and a busy-handler is configured, it is invoked and
10168 ** the writer lock retried until either the busy-handler returns 0 or the lock
10169 ** is successfully obtained. ^The busy-handler is also invoked while waiting for
10170 ** database readers as described above. ^If the busy-handler returns 0 before
10173 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
10189 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
10265 ** If X is non-zero, then the virtual table implementation guarantees
10330 ** [xUpdate] method understands as a "no-change" value.
10395 ** set to the total number of times that the X-th loop has run.</dd>
10399 ** to the total number of rows examined by all iterations of the X-th loop.</dd>
10404 ** iteration of the X-th loop. If the query planner's estimates was accurate,
10411 ** to a zero-terminated UTF-8 string containing the name of the index or table
10412 ** used for the X-th loop.
10416 ** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
10417 ** description for the X-th loop.
10421 ** "select-id" for the X-th loop. The select-id identifies which query or
10422 ** subquery the loop is part of. The main query has a select-id of zero.
10423 ** The select-id is the same value as is output in the first column
10445 ** compile-time option.
10453 ** Loops are numbered starting from zero. ^If idx is out of range - less than
10455 ** the statement - a non-zero value is returned and the variable that pOut
10460 ** as if the loop did not exist - it returns non-zero and leave the variable
10473 ** CAPI3REF: Zero Scan-Status Counters
10478 ** This API is only available if the library is built with pre-processor
10484 ** CAPI3REF: Flush caches to disk mid-transaction
10487 ** ^If a write-transaction is open on [database connection] D when the
10489 ** pages in the pager-cache that are not currently in use are written out
10493 ** interface flushes caches for all schemas - "main", "temp", and
10498 ** immediately and there is a busy-handler callback configured, it is invoked
10506 ** example an IO error or out-of-memory condition), then processing is
10511 ** ^This function does not set the database handle error code or message
10517 ** CAPI3REF: The pre-update hook.
10521 ** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
10593 ** operation; or 1 for inserts, updates, or deletes invoked by top-level
10594 ** triggers; or 2 for changes resulting from triggers called by top-level
10620 ** CAPI3REF: Low-level system error code
10625 ** The return value is OS-dependent. For example, on unix systems, after
10665 ** If there is not already a read-transaction open on schema S when
10697 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10731 ** is returned. If another error code - for example SQLITE_PROTOCOL or an
10732 ** SQLITE_IOERR error code - is returned, then the final state of the
10746 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10763 ** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
10830 ** For an ordinary on-disk database file, the serialization is just a
10831 ** copy of the disk file. For an in-memory database or a "TEMP" database,
10868 ** Zero or more of the following constants can be OR-ed together for
10872 ** a pointer to contiguous in-memory database that it is currently using,
10874 ** a contiguous in-memory database, then this option causes
10876 ** using a contiguous in-memory database if it has been initialized by a
10886 ** reopen S as an in-memory database based on the serialization contained
10890 ** permitted to add content to the in-memory database as long as the total
10938 ** should be treated as read-only.
10942 #define SQLITE_DESERIALIZE_READONLY 4 /* Database is read-only */
10982 /* The double-precision datatype used by RTree depends on the
10983 ** SQLITE_RTREE_INT_ONLY compile-time option.
10993 ** R-Tree geometry query as follows:
11018 ** Register a 2nd-generation geometry callback named zScore that can be
11019 ** used as part of an R-Tree geometry query as follows:
11125 ** is not possible for an application to register a pre-update hook on a
11128 ** which a pre-update hook is already defined. The results of attempting
11164 ** disabled - it does not. A newly created session object is enabled.
11171 ** no-op, and may be used to query the current state of the session.
11248 ** rows a zero-length blob (SQL value X'') is stored in the changeset or
11254 ** zero-length blob back to a NULL value when updating the sqlite_stat1
11258 ** conflict-handler callback) then the X'' value is returned. The application
11306 ** database row along with the updated values for each updated non-primary-key
11315 ** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
11317 ** with non-NULL PRIMARY KEY values is updated so that one or more of its
11330 ** are sorted in the same order in which they were attached (or auto-attached)
11344 ** recorded once - the first time a row with said primary key is inserted,
11351 ** The session object therefore accumulates two types of records - those
11369 ** found and one or more of the non-primary key fields have been
11386 ** This may appear to have some counter-intuitive effects if a single row
11408 ** does not have a primary key, this function is a no-op (but does not return
11428 ** used to update the table in database zFrom (call this the "from-table")
11430 ** object (call this the "to-table"). Specifically:
11433 ** <li> For each row (primary key) that exists in the to-table but not in
11434 ** the from-table, an INSERT record is added to the session object.
11436 ** <li> For each row (primary key) that exists in the to-table but not in
11437 ** the from-table, a DELETE record is added to the session object.
11440 ** different non-PK values in each, an UPDATE record is added to the
11455 ** message. It is the responsibility of the caller to free this buffer using
11485 ** Because the non-primary key "old.*" fields are omitted, no
11504 ** Return non-zero if no changes to attached tables have been recorded by
11512 ** are restored. However, if this function returns non-zero, then it is
11599 ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
11623 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11636 ** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
11639 ** or until the conflict-handler function returns.
11686 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
11695 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11726 ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
11760 ** conflict-handler callback by [sqlite3changeset_apply()] with either
11771 ** "conflicting row" associated with the current conflict-handler callback
11809 ** function with an iterator passed to a conflict-handler by
11817 ** returned. This is to allow the following pattern (pseudo-code):
11970 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
11971 ** <tr><th style="white-space:pre">Existing Change </th>
11972 ** <th style="white-space:pre">New Change </th>
12017 ** returned. Or, if an out-of-memory condition occurs during processing, this
12075 ** Otherwise, if the return value is non-zero or the xFilter argument to
12092 ** changes associated with the table are applied. A warning message is issued
12111 ** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
12115 ** returned by each invocation of the conflict-handler function. Refer to
12124 ** stored in all non-primary key columns also match the values stored in
12128 ** the non-primary key fields contains a value different from the original
12129 ** row value stored in the changeset, the conflict-handler function is
12132 ** only the values of those non-primary key fields are compared against
12133 ** the current database contents - any trailing database table columns
12137 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12142 ** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
12161 ** This includes the case where the INSERT operation is re-attempted because
12169 ** stored in all modified non-primary key columns also match the values
12173 ** the modified non-primary key fields contains a value different from an
12174 ** original row value stored in the changeset, the conflict-handler function
12176 ** UPDATE changes only contain values for non-primary key fields that are
12178 ** avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
12181 ** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
12185 ** SQLITE_CONSTRAINT, the conflict-handler function is invoked with
12203 ** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
12280 ** Values that may be passed as the second argument to a conflict-handler.
12287 ** (non primary-key) fields modified by the update do not contain the
12448 ** combined on a per-field basis, not per-row. This means that in the
12532 ** corresponding non-streaming API functions:
12534 ** <table border=1 style="margin-left:8ex;margin-right:8ex">
12535 ** <tr><th>Streaming function<th>Non-streaming equivalent</th>
12545 ** Non-streaming functions that accept changesets (or patchsets) as input
12550 ** low-memory environment is required to handle very large changesets, the
12814 ** non-negative but less than the number of columns in the table, return
12829 ** non-negative but less than the number of columns in the table, set
12843 ** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12941 ** If the bClear argument is non-zero, then the auxiliary data is cleared
12963 ** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
12965 ** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
12971 ** modify this structure directly - it should only be used as shown above
12979 ** through an empty set (all calls to xPhraseFirst() set iCol to -1).
12993 ** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
12995 ** pApi->xPhraseNextColumn(pFts, &iter, &iCol)
13004 ** xPhraseFirstColumn() set iCol to -1).
13073 ** The second and third arguments are an array of nul-terminated strings
13091 ** by argument pText. pText may or may not be nul-terminated. The first
13099 ** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
13104 ** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
13108 ** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
13113 ** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
13146 ** built-in tokenizers, the FTS5 query 'first + place' will match instances
13180 ** still appears to contain just two phrases - "(first OR 1st)"
13191 ** when tokenizing query text (it should not - to do so would be
13238 ** provide synonyms for prefixes). However, a non-prefix query like '1st'
13334 ** autoconf-based build
13362 ** The hard limit is the ability of a 32-bit signed integer
13363 ** to count the size: 2^31-1 or 2147483647.
13381 ** tell you that in a well-normalized database, you usually should
13439 ** The suggested maximum number of in-memory pages to use for
13442 ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
13444 ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
13445 ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
13448 # define SQLITE_DEFAULT_CACHE_SIZE -2000
13462 ** counted using a signed 8-bit integer which has a maximum value of 127
13474 ** as a signed 32-bit integer can hold.
13481 ** imposed by the use of 16-bit offsets within each page.
13487 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
13488 ** compiled with the default page-size limit will not be able to rollback
13511 ** device characteristics (sector-size and atomic write() support),
13528 ** This value can be lowered (or raised) at run-time using that the
13559 #pragma warn -rch /* unreachable code */
13560 #pragma warn -ccc /* Condition is always true or false */
13561 #pragma warn -aus /* Assigned value is never used */
13562 #pragma warn -csu /* Comparing signed and unsigned */
13563 #pragma warn -spa /* Suspicious pointer arithmetic */
13567 ** WAL mode depends on atomic aligned 32-bit loads and stores in a few
13571 # define __has_extension(x) 0 /* compatibility with non-clang compilers */
13603 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
13616 # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
13617 #else /* Generates a warning - but it always works */
13657 ** level of threadsafety. 2 means the library is multithreaded - multiple
13665 ** for compile-time options at runtime (e.g. "PRAGMA compile_options"), this
13673 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
13679 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
13686 ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
13715 # error "Two or more of the following compile-time configuration options\
13753 ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
13821 ** of SQLite to unexpected behavior - to make the code "self-healing"
13827 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
13853 ** For normal production builds, harmless(X) is a no-op, since it does
13926 ** Return true (non-zero) if the input is an integer that is too large
13927 ** to fit in 32-bits. This macro is used inside of various testcase()
13928 ** macros to verify that we have tested SQLite for large-file support.
13955 ** This is the header file for the generic hash-table implementation
13966 ** The internals of this structure are intended to be opaque -- client
13973 ** All elements of the hash table are on a single doubly-linked list.
13977 ** the global doubly-linked list. The contents of the bucket are the
13978 ** element pointed to plus the next _ht.count-1 elements in the list.
13997 ** structure. All elements are stored on a single doubly-linked list.
14028 #define sqliteHashFirst(H) ((H)->first)
14029 #define sqliteHashNext(E) ((E)->next)
14030 #define sqliteHashData(E) ((E)->data)
14031 /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
14032 /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
14037 /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
14245 int xxn=(N);while(xxn-->0)*(xxd++)=*(xxs++);}
14250 ** substitute integer for floating-point
14281 ** the VDBE-level file format changes. The following macros define the
14292 ** changed at run-time using a pragma.
14300 ** on the command-line
14330 ** of -1024*N bytes is allocated and used for as many pages as it will hold.
14332 ** The default value of "20" was choosen to minimize the run-time of the
14333 ** speedtest1 test program with options: --shrink-memory --reprepare
14347 ** The compile-time options SQLITE_MMAP_READWRITE and
14360 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
14391 ** types can be conveniently redefined at compile-type. Like this:
14393 ** cc '-DUINTPTR_TYPE=long long int' ...
14433 typedef sqlite_int64 i64; /* 8-byte signed integer */
14434 typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
14435 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
14436 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
14437 typedef INT16_TYPE i16; /* 2-byte signed integer */
14438 typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
14439 typedef INT8_TYPE i8; /* 1-byte signed integer */
14447 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
14452 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
14453 ** can be used at compile-time if desired.
14456 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
14458 typedef u32 tRowcnt; /* 32-bit is the default */
14462 ** Estimated quantities used for query planning are stored as 16-bit
14464 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
14473 ** 1 -> 0 20 -> 43 10000 -> 132
14474 ** 2 -> 10 25 -> 46 25000 -> 146
14475 ** 3 -> 16 100 -> 66 1000000 -> 199
14476 ** 4 -> 20 1000 -> 99 1048576 -> 200
14477 ** 10 -> 33 1024 -> 100 4294967296 -> 320
14482 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
14524 ** and whether or not that determination is run-time or compile-time.
14526 ** For best performance, an attempt is made to guess at the byte-order
14527 ** using C-preprocessor macros. If that is unsuccessful, or if
14528 ** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14529 ** at run-time.
14564 ** Constants for the largest and smallest possible 64-bit signed integers.
14565 ** These macros are designed to work correctly on both 32-bit and 64-bit
14570 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
14574 ** to force 8-byte alignment on 64-bit architectures.
14584 ** Assert that the pointer X is aligned to an 8-byte boundary. This
14589 ** underlying malloc() implementation might return us 4-byte aligned
14590 ** pointers. In that case, only verify 4-byte alignment.
14593 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
14595 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
14607 ** Default maximum size of memory used by memory-mapped I/O in the VFS
14627 ** default MMAP_SIZE is specified at compile-time, make sure that it does
14650 sqlite3DebugPrintf("%u/%d/%p: ",(S)->selId,(P)->addrExplain,(S)),\
14671 ** An instance of the following structure is used to store the busy-handler
14676 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
14696 ** The root-page of the schema table.
14715 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
14734 ** directly, we use its constant as a key to lookup the run-time allocated
14736 ** for the run-time allocated buffer.
14739 ** macros become no-ops and have zero performance impact.
14835 ** Changing this from a 64-bit to a 32-bit type limits the number of
14856 #define ALLBITS ((Bitmask)-1)
14898 #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
14921 ** is devoted to storing a super-journal name - there are no more pages to
14925 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
14933 #define PAGER_MEMORY 0x0002 /* In-memory database */
14938 #define PAGER_LOCKINGMODE_QUERY -1
14949 #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
14954 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
14955 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
14961 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
14983 ** that make up the Pager sub-system API. See source code comments for
15131 ** This header file defines the interface that the sqlite B-Tree file
15144 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
15151 #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
15152 #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
15165 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
15180 #define BTREE_MEMORY 2 /* This is an in-memory DB */
15181 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
15235 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
15238 ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
15241 #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
15242 #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
15262 ** For example, the free-page-count field is located at byte offset 36 of
15263 ** the database file header. The incr-vacuum-flag field is located at
15267 ** It is a read-only number computed by the pager. But we merge it with
15280 #define BTREE_DATA_VERSION 15 /* A virtual meta-value */
15294 ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
15295 ** column of the b-tree of the cursor. The Expr tree will not contain
15296 ** any function calls nor subqueries nor references to b-trees other than
15299 ** The design of the _RANGE hint is aid b-tree implementations that try
15300 ** to prefetch content from remote machines - to provide those
15325 #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
15330 ** For read-only cursors the wrFlag argument is always zero. For read-write
15341 ** by this, the native b-tree engine of SQLite, but it is available to
15343 ** b-tree system. For alternative storage engines in which a delete of
15347 ** and DELETE operations as no-ops, and any READ operation against a
15350 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
15356 int wrFlag, /* 1 for writing. 0 for read-only */
15489 ** Enter and Leave procedures no-ops.
15576 u16 p5; /* Fifth parameter is an unsigned 16-bit integer */
15608 u32 iSrcLine; /* Source-code line that generated this opcode
15616 ** A sub-routine used to implement a trigger program.
15619 VdbeOp *aOp; /* Array of opcodes for sub-program */
15625 SubProgram *pNext; /* Next sub-program already visited */
15645 #define P4_STATIC (-1) /* Pointer to a static string */
15646 #define P4_COLLSEQ (-2) /* P4 is a pointer to a CollSeq structure */
15647 #define P4_INT32 (-3) /* P4 is a 32-bit signed integer */
15648 #define P4_SUBPROGRAM (-4) /* P4 is a pointer to a SubProgram structure */
15649 #define P4_ADVANCE (-5) /* P4 is a pointer to BtreeNext() or BtreePrev() */
15650 #define P4_TABLE (-6) /* P4 is a pointer to a Table structure */
15652 #define P4_FREE_IF_LE (-7)
15653 #define P4_DYNAMIC (-7) /* Pointer to memory from sqliteMalloc() */
15654 #define P4_FUNCDEF (-8) /* P4 is a pointer to a FuncDef structure */
15655 #define P4_KEYINFO (-9) /* P4 is a pointer to a KeyInfo structure */
15656 #define P4_EXPR (-10) /* P4 is a pointer to an Expr tree */
15657 #define P4_MEM (-11) /* P4 is a pointer to a Mem* structure */
15658 #define P4_VTAB (-12) /* P4 is a pointer to an sqlite3_vtab structure */
15659 #define P4_REAL (-13) /* P4 is a 64-bit floating point value */
15660 #define P4_INT64 (-14) /* P4 is a 64-bit signed integer */
15661 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
15662 #define P4_FUNCCTX (-16) /* P4 is a pointer to an sqlite3_context object */
15663 #define P4_DYNBLOB (-17) /* Pointer to memory from sqliteMalloc() */
15665 /* Error message codes for OP_Halt */
15753 #define OP_IfPos 48 /* jump, synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
15754 #define OP_IfNotZero 49 /* jump, synopsis: if r[P1]!=0 then r[P1]--, goto P2 */
15764 #define OP_DecrJumpZero 59 /* jump, synopsis: if (--r[P1])==0 goto P2 */
15792 #define OP_Compare 87 /* synopsis: r[P1@P3] <-> r[P2@P3] */
15812 #define OP_Subtract 107 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
15824 #define OP_SeekScan 119 /* synopsis: Scan-ahead up to P1 rows */
15860 #define OP_OffsetLimit 155 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) …
15932 ** Additional non-public SQLITE_PREPARE_* flags
15979 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
15984 # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
16060 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
16097 ** VdbeCoverageNeverNull(v) // Previous three-way branch is only
16102 ** // in distingishing equal and not-equal.
16105 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
16106 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
16114 ** is responsible for keeping track of this and reporting byte-code branches
16244 /* Modify the page-size after the cache has been created. */
16266 /* Change a page number. Used by incr-vacuum. */
16308 /* Set and get the suggested cache-size for the specified pager-cache.
16311 ** the total number of pages cached by purgeable pager-caches to the sum
16312 ** of the suggested cache-sizes.
16319 /* Set or get the suggested spill-size for the specified pager-cache.
16321 ** The spill-size is the minimum number of pages in cache before the cache
16369 ** This header file (together with is companion C source-code file
16373 ** This header file is #include-ed by sqliteInt.h and thus ends up
16381 ** necessary pre-processor macros for it.
16397 ** This file contains pre-processor directives related to operating system
16447 ** a no-op
16467 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
16469 ** 2006-10-31: The default prefix used to be "sqlite_". But then
16470 ** Mcafee started using SQLite in their anti-virus product and it
16516 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
16624 ** sqlite3_malloc() to obtain space for the file-handle structure.
16663 ** at start-time.
16665 ** SQLITE_MUTEX_NOOP For single-threaded applications. No
16668 ** start-time.
16670 ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
16672 ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
16689 ** If this is a no-op implementation, implement everything as macros.
16710 /* The SQLITE_EXTRA_DURABLE compile-time option used to set the default
16730 ** The "PRAGMA synchronous" statement also uses the zero-based numbers.
16731 ** In other words, the zero-based numbers are used for all external interfaces
16732 ** and the one-based values are used internally.
16760 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
16789 ** Db.pSchema->flags field.
16791 #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
16792 #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
16793 #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags|=(P)
16794 #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->schemaFlags&=~(P)
16797 ** Allowed values for the DB.pSchema->flags field.
16817 ** Lookaside malloc is a set of fixed-size buffers that can be used
16839 ** in a performance-critical path. sz should be set by to szTrue whenever
16844 ** come off of pFree first, then pInit as a fallback. This dual-list
16845 ** allows use to compute a high-water mark - the maximum number of allocations
16846 ** outstanding at any point in the past - by subtracting the number of
16849 ** Enhancement on 2019-12-12: Two-size-lookaside
16853 ** The two-size-lookaside enhancement breaks up the lookaside allocation
16854 ** into two pools: One of 128-byte slots and the other of the default size
16855 ** (1200-byte) slots. Allocations are filled from the small-pool first,
16856 ** failing over to the full-size pool if that does not work. Thus more
16873 void *pMiddle; /* First byte past end of full-size buffers and
16883 #define DisableLookaside db->lookaside.bDisable++;db->lookaside.sz=0
16884 #define EnableLookaside db->lookaside.bDisable--;\
16885 db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
16887 /* Size of the smaller allocations in two-size lookside */
16895 ** A hash table for built-in function definitions. (Application-defined
16978 u32 nSchemaLock; /* Do not reset the schema when non-zero */
16985 u8 autoCommit; /* The auto-commit flag. */
16989 u8 dfltLockMode; /* Default locking-mode for attached dbs */
16995 u8 noSharedCache; /* True if no shared-cache backends */
17043 PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17052 sqlite3_value *pErr; /* Most recent error message */
17081 int nSavepoint; /* Number of non-transaction savepoints */
17082 int nStatement; /* Number of nested statement-transactions */
17111 #define SCHEMA_ENC(db) ((db)->aDb[0].pSchema->enc)
17112 #define ENC(db) ((db)->enc)
17117 ** C-compilers still do not accept LL integer literals.
17141 #define SQLITE_ReadUncommit 0x00000400 /* READ UNCOMMITTED in shared-cache */
17160 #define SQLITE_DqsDDL 0x20000000 /* dbl-quoted strings allowed in DDL*/
17161 #define SQLITE_DqsDML 0x40000000 /* dbl-quoted strings allowed in DML*/
17181 #define DBFLAG_PreferBuiltin 0x0002 /* Preference to built-in funcs */
17202 #define SQLITE_CountOfView 0x00000200 /* The count-of-view optimization */
17206 #define SQLITE_PushDown 0x00001000 /* The push-down optimization */
17208 #define SQLITE_SkipScan 0x00004000 /* Skip-scans */
17211 #define SQLITE_ExistsToIN 0x00020000 /* The EXISTS-to-IN optimization */
17217 #define OptimizationDisabled(db, mask) (((db)->dbOptFlags&(mask))!=0)
17218 #define OptimizationEnabled(db, mask) (((db)->dbOptFlags&(mask))==0)
17224 #define ConstFactorOk(P) ((P)->okConstFactor)
17240 ** structure. For global built-in functions (ex: substr(), max(), count())
17242 ** For per-connection application-defined functions, a pointer to this
17243 ** structure is held in the db->aHash hash table.
17245 ** The u.pHash field is used by the global built-ins. The u.pDestructor
17246 ** field is used by per-connection app-def functions.
17249 i8 nArg; /* Number of arguments. -1 means unlimited */
17253 void (*xSFunc)(sqlite3_context*,int,sqlite3_value**); /* func or agg-step */
17256 void (*xInverse)(sqlite3_context*,int,sqlite3_value**); /* inverse agg-step */
17265 ** This structure encapsulates a user-function destructor callback (as
17301 #define SQLITE_FUNC_CASE 0x0008 /* Case-sensitive LIKE-type function */
17304 #define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17305 #define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17306 #define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17307 /* 0x0200 -- available for reuse */
17308 #define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
17312 ** single query - might change over time */
17313 #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17314 #define SQLITE_FUNC_OFFSET 0x8000 /* Built-in sqlite_offset() function */
17315 #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17318 #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17320 #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17322 /* Identifier numbers for each in-line function */
17339 ** as the user-data (sqlite3_user_data()) for the function. If
17350 ** zName is the name of a function that is implemented by in-line
17356 ** zName is the name of a test-only function implemented by in-line
17365 ** a single query. The iArg is ignored. The user-data is always set
17369 ** For math-library functions. xPtr is an arbitrary pointer.
17374 ** ignored and the user-data for these functions is set to an
17375 ** arbitrary non-NULL pointer. The bNC parameter is not used.
17393 ** available as the function user-data (sqlite3_user_data()). The
17446 char *zName; /* Savepoint name (nul-terminated) */
17514 #define COLFLAG_SORTERREF 0x0010 /* Use sorter-refs with this column */
17532 char *zName; /* Name of the collating sequence, UTF-8 encoded */
17544 #define SQLITE_SO_UNDEFINED -1 /* No sort order specified */
17599 ** database connections, even when the rest of the in-memory database
17609 ** database schema are initially stored in a linked-list pointed to by
17616 ** When an in-memory Table object is deleted (for example when the
17668 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
17690 ** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING
17691 ** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden,
17701 #define TF_Readonly 0x0001 /* Read-only system table */
17712 #define TF_NoVisibleRowid 0x0200 /* No user-visible "rowid" column */
17713 #define TF_OOOHidden 0x0400 /* Out-of-Order hidden columns */
17725 # define IsVirtual(X) ((X)->nModuleArg)
17727 ((X)->op==TK_COLUMN && (X)->y.pTab!=0 && (X)->y.pTab->nModuleArg)
17735 ** only works for non-virtual tables (ordinary tables and views) and is
17740 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17741 # define IsOrdinaryHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17743 # define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
17752 #define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
17753 #define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
17768 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
17771 ** from-table == child-table
17772 ** to-table == parent-table
17775 ** which is attached to the from-table. The to-table need not exist when
17776 ** the from-table is created. The existence of the to-table is not checked.
17790 /* EV: R-30323-21917 */
17856 u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
17884 ** pKeyInfo->nField.
17888 ** -1 and +1 respectively, but might be inverted to +1 and -1 if the b-tree
17892 ** an equals comparison. default_rc can be -1, 0, or +1. If there are
17893 ** multiple entries in the b-tree with the same key (when only looking
17894 ** at the first pKeyInfo->nFields,) then default_rc can be set to -1 to
17899 ** get and equal results when comparing this structure to a b-tree record.
17903 ** b-tree.
17906 KeyInfo *pKeyInfo; /* Collation and sort-order information */
17940 ** algorithm to employ whenever an attempt is made to insert a non-unique
17948 ** number (it cannot - the database page is not allocated until the VDBE
17973 unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
17982 IndexSample *aSample; /* Samples of the left-most key */
17983 tRowcnt *aiRowEst; /* Non-logarithmic stat1 data for this index */
17984 tRowcnt nRowEst0; /* Non-logarithmic number of rows in the index */
17998 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
18001 #define IsUniqueIndex(X) ((X)->onError!=OE_None)
18006 #define XN_ROWID (-1) /* Indexed column is the rowid */
18007 #define XN_EXPR (-2) /* Indexed column is an expression */
18039 const char *z; /* Text of the token. Not NULL-terminated! */
18062 int sortingIdxPTab; /* Cursor number of pseudo-table */
18089 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
18090 ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
18091 ** than 32767 we have to make it 32-bit. 16-bit is preferred because
18096 ** the option is available (at compile-time).
18111 ** to represent the greater-than-or-equal-to operator in the expression
18125 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
18144 ** subquery gives a constant result, then iTable is -1. If the subquery
18149 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
18180 int iValue; /* Non-negative integer value if EP_IntValue */
18205 ** TK_TRIGGER: 1 -> new, 0 -> old
18210 ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
18213 i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
18277 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
18278 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
18279 #define ExprSetProperty(E,P) (E)->flags|=(P)
18280 #define ExprClearProperty(E,P) (E)->flags&=~(P)
18281 #define ExprAlwaysTrue(E) (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
18282 #define ExprAlwaysFalse(E) (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
18292 ** processes but is a no-op for delivery.
18295 # define ExprSetVVAProperty(E,P) (E)->vvaFlags|=(P)
18296 # define ExprHasVVAProperty(E,P) (((E)->vvaFlags&(P))!=0)
18297 # define ExprClearVVAProperties(E) (E)->vvaFlags = 0
18317 #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
18327 ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
18343 ** ---------- -------------------------
18394 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
18417 int regResult; /* Registers holding results of a co-routine */
18422 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18423 unsigned isCorrelated :1; /* True if sub-query is correlated */
18424 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18435 ExprList *pFuncArg; /* Arguments to table-valued-function */
18459 ** In the colUsed field, the high-order bit (bit 63) is set if the table
18460 ** contains more than 63 columns and the 64-th or later column is used.
18487 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
18490 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
18493 #define WHERE_OR_SUBCLAUSE 0x0020 /* Processing a sub-WHERE as part of
18522 ** NameContexts can be nested. When resolving names, the inner-most
18538 ExprList *pEList; /* Optional list of result-set columns */
18584 ** conflict-target clause. (In "ON CONFLICT(a,b)" the "(a,b)" is the
18585 ** conflict-target clause.) The pUpsertTargetWhere is the optional
18688 #define SF_PushDown 0x1000000 /* SELECT has be modified by push-down opt */
18697 ** identified by pDest->iSDParm.
18699 ** SRT_Except Remove results from the temporary index pDest->iSDParm.
18701 ** SRT_Exists Store a 1 in memory cell pDest->iSDParm if the result
18706 ** the side-effects of functions.
18713 ** in register pDest->iSDParm then abandon the rest
18717 ** row of result as the key in table pDest->iSDParm.
18718 ** Apply the affinity pDest->affSdst before storing
18721 ** SRT_EphemTab Create an temporary table pDest->iSDParm and store
18727 ** SRT_Coroutine Generate a co-routine that returns a new row of
18729 ** of the co-routine is stored in register pDest->iSDParm
18730 ** and the result row is stored in pDest->nDest registers
18731 ** starting with pDest->iSdst.
18733 ** SRT_Table Store results in temporary table pDest->iSDParm.
18739 ** SRT_DistFifo Store results in a temporary table pDest->iSDParm.
18740 ** But also use temporary table pDest->iSDParm+1 as
18744 ** SRT_Queue Store results in priority queue pDest->iSDParm (really
18748 ** SRT_DistQueue Store results in priority queue pDest->iSDParm only if
18750 ** index at pDest->iSDParm+1 hold all prior stores.
18753 ** pDest->iSDParm. If (pDest->iSDParm<0), then the temp
18754 ** table is an intkey table - in this case the first
18756 ** key. If (pDest->iSDParm>0), then the table is an index
18757 ** table. (pDest->iSDParm) is the number of key columns in
18769 #define IgnorableDistinct(X) ((X->eDest)<=SRT_DistQueue)
18775 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Fifo)
18803 ** the code generator needs. We have to keep per-table autoincrement
18822 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
18881 ** The nTableLock and aTableLock variables are only used if the shared-cache
18882 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
18883 ** used to store the set of table-locks required by the statement being
18889 char *zErrMsg; /* An error message */
18912 ** of the base register during check-constraint eval */
18922 int nMaxArg; /* Max args passed to user function by sub-program */
18926 TableLock *aTableLock; /* Required table locks for shared-cache mode */
18969 int nHeight; /* Expression tree height of current sub-select */
18981 const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
19004 #define PARSE_TAIL_SZ (sizeof(Parse)-PARSE_RECURSE_SZ) /* Non-recursive part */
19013 #define IN_DECLARE_VTAB (pParse->eParseMode==PARSE_MODE_DECLARE_VTAB)
19019 #define IN_RENAME_OBJECT (pParse->eParseMode>=PARSE_MODE_RENAME)
19025 #define IN_SPECIAL_PARSE (pParse->eParseMode!=PARSE_MODE_NORMAL)
19049 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
19053 #define OPFLAG_LASTROWID 0x20 /* Set to update db->lastRowid */
19057 #define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19092 IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
19093 the <column-list> is stored here */
19112 * that is a part of a trigger-program.
19117 * the first step of the trigger-program.
19124 * orconf -> stores the ON CONFLICT algorithm
19125 * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
19127 * zTarget -> Dequoted name of the table to insert into.
19128 * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
19130 * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
19131 * statement, then this stores the column-names to be
19135 * zTarget -> Dequoted name of the table to delete from.
19136 * pWhere -> The WHERE clause of the DELETE statement if one is specified.
19140 * zTarget -> Dequoted name of the table to update.
19141 * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
19143 * pExprList -> A list of the columns to update and the expressions to update
19161 TriggerStep *pNext; /* Next in the link-list */
19162 TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
19191 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
19195 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
19204 char **pzErrMsg; /* Error message stored here */
19228 u8 bUseCis; /* Use covering indices for full-scans */
19232 int neverCorrupt; /* Database is always well-formed */
19235 int nStmtSpill; /* Stmt-journal spill-to-disk threshold */
19236 sqlite3_mem_methods m; /* Low-level memory allocation interface */
19237 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
19238 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
19248 int sharedCacheEnabled; /* true if shared-cache mode enabled */
19250 /* The above might be initialized to non-zero. The following need to always
19280 u32 szSorterRef; /* Min size in bytes to use sorter-refs */
19286 ** the assert is only valid on a well-formed database. Instead of:
19298 ** things that are always true for well-formed databases.
19303 ** Context pointer passed down through the tree-walk.
19344 const char *zType; /* Type of the container - used for error messages */
19345 const Token *pName; /* Name of the container - used for error messages */
19365 ** Return code from the parse-tree walking primitives and their
19379 const char *zCteErr; /* Error message for circular references */
19425 ** data structures on sqlite3DebugPrintf() using a tree-like view.
19442 ** (2) All window functions in a single SELECT form a linked-list
19512 ** Assuming zIn points to the first byte of a UTF-8 character,
19513 ** advance zIn to point to the first byte of the next UTF-8 character.
19524 ** routines that report the line-number on which the error originated
19569 ** The ctype.h header is needed for non-ASCII systems. It is also
19698 # define MAN754 ((((u64)1)<<52)-1)
19796 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
19797 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
19806 # define sqlite3ColumnPropertiesFromName(T,C) /* no-op */
20055 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
20056 # define sqlite3IsToplevel(p) ((p)->pToplevel==0)
20122 ** Routines to read and write variable-length integers. These used to
20325 ** The interface to the LEMON-generated parser
20386 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
20457 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
20459 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
20461 ** provided (enforcement of FK constraints requires the triggers sub-system).
20510 #define IN_INDEX_EPH 2 /* Search an ephemeral b-tree */
20563 ** sqlite3IoTrace is a pointer to a printf-like routine used to
20596 ** passed back to non-lookaside free() routines. Asserts such as the
20597 ** example above are placed on the non-lookaside free() routines to verify
20600 ** All of this is no-op for a production build. It only comes into
20601 ** play when the SQLITE_MEMDEBUG compile-time option is used.
20608 # define sqlite3MemdebugSetType(X,Y) /* no-op */
20661 /* An array to map all upper-case characters into their corresponding
20662 ** lower-case character.
20664 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
20707 ** The following 256 byte lookup table is used to support SQLites built-in
20720 ** case. i.e. if the character is a lower-case ASCII character.
20721 ** If x is a lower-case ASCII character, then its upper-case equivalent
20722 ** is (x - 0x20). Therefore toupper() can be implemented as:
20729 ** Bit 0x40 is set if the character is non-alphanumeric and can be used in an
20731 ** non-ASCII UTF character. Hence the test for whether or not a character is
20740 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
20772 /* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
20776 ** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
20777 ** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
20779 ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally
20787 /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the
20788 ** SQLITE_ALLOW_COVERING_INDEX_SCAN compile-time option, or is "on" if
20789 ** that compile-time option is omitted.
20795 # error "Compile-time disabling of covering index scan using the\
20796 -DSQLITE_ALLOW_COVERING_INDEX_SCAN=0 option is deprecated.\
20812 ** before 3.12.0). -1 means always keep the entire statement journal in
20822 ** The default lookaside-configuration, the format "SZ,N". SZ is the
20824 ** and N is the number of slots. The lookaside-configuration can be
20825 ** changed as start-time using sqlite3_config(SQLITE_CONFIG_LOOKASIDE)
20826 ** or at run-time for an individual database connection using
20829 ** With the two-size-lookaside enhancement, less lookaside is required.
20830 ** The default configuration of 1200,40 actually provides 30 1200-byte slots
20831 ** and 93 128-byte slots, which is more lookaside than is available
20832 ** using the older 1200,100 configuration without two-size-lookaside.
20843 /* The default maximum size of an in-memory database created using
20911 ** Hash table for global functions - functions common to all
20913 ** read-only.
20920 ** sqlite3Hwtime() for profiling. This is a no-op on standard builds.
20927 ** 1-gibabyte boundary) in a compatible database. SQLite never uses
21054 ** * A b-tree cursor
21055 ** - In the main database or in an ephemeral database
21056 ** - On either an index or a table
21059 ** * A one-row "pseudotable" stored in a single register
21064 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
21073 Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
21128 ** When a sub-program is executed (OP_Program), a structure of this type
21131 ** values stored in the Vdbe struct. When the sub-program is finished,
21133 ** restoring the state of the VM to as it was before the sub-program
21170 int nDbChange; /* Value of db->nChange */
21205 void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
21261 /* Return TRUE if Mem X contains dynamically allocated content - anything
21265 (((X)->flags&(MEM_Agg|MEM_Dyn))!=0)
21271 ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
21274 ** True if Mem X is a NULL-nochng type.
21277 (((X)->flags&MEM_TypeMask)==(MEM_Null|MEM_Zero) \
21278 && (X)->n==0 && (X)->u.nZero==0)
21285 #define memIsValid(M) ((M)->flags & MEM_Undefined)==0
21341 int iSelectID; /* The "Select-ID" for this loop */
21346 /* The DblquoteStr object holds the text of a double-quoted
21350 ** list is consulted for each double-quoted identifier to see if the
21396 char *zErrMsg; /* Error message written here */
21399 i64 startTime; /* Time when query started - used for profiling */
21409 u8 doingRerun; /* True if rerunning after an auto-reprepare */
21412 bft changeCntOn:1; /* True to update the change-counter */
21417 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
21423 DblquoteStr *pDblStr; /* List of double-quoted string literals */
21430 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
21604 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
21649 ** we have to locate the state vector at run-time. In the more common
21679 ** The value of N is added to the current status value and the high-water
21683 ** mark is unchanged. N must be non-negative for StatusDown().
21703 wsdStat.nowValue[op] -= N; in sqlite3StatusDown()
21775 p = p->pNext; in countLookasideSlots()
21785 u32 nInit = countLookasideSlots(db->lookaside.pInit); in sqlite3LookasideUsed()
21786 u32 nFree = countLookasideSlots(db->lookaside.pFree); in sqlite3LookasideUsed()
21788 nInit += countLookasideSlots(db->lookaside.pSmallInit); in sqlite3LookasideUsed()
21789 nFree += countLookasideSlots(db->lookaside.pSmallFree); in sqlite3LookasideUsed()
21791 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; in sqlite3LookasideUsed()
21792 return db->lookaside.nSlot - (nInit+nFree); in sqlite3LookasideUsed()
21802 int *pHighwater, /* Write high-water mark here */ in sqlite3_db_status()
21803 int resetFlag /* Reset high-water mark if true */ in sqlite3_db_status()
21811 sqlite3_mutex_enter(db->mutex); in sqlite3_db_status()
21816 LookasideSlot *p = db->lookaside.pFree; in sqlite3_db_status()
21818 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
21819 p->pNext = db->lookaside.pInit; in sqlite3_db_status()
21820 db->lookaside.pInit = db->lookaside.pFree; in sqlite3_db_status()
21821 db->lookaside.pFree = 0; in sqlite3_db_status()
21824 p = db->lookaside.pSmallFree; in sqlite3_db_status()
21826 while( p->pNext ) p = p->pNext; in sqlite3_db_status()
21827 p->pNext = db->lookaside.pSmallInit; in sqlite3_db_status()
21828 db->lookaside.pSmallInit = db->lookaside.pSmallFree; in sqlite3_db_status()
21829 db->lookaside.pSmallFree = 0; in sqlite3_db_status()
21842 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); in sqlite3_db_status()
21843 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); in sqlite3_db_status()
21845 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; in sqlite3_db_status()
21847 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; in sqlite3_db_status()
21862 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
21863 Btree *pBt = db->aDb[i].pBt; in sqlite3_db_status()
21889 db->pnBytesFreed = &nByte; in sqlite3_db_status()
21890 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
21891 Schema *pSchema = db->aDb[i].pSchema; in sqlite3_db_status()
21896 pSchema->tblHash.count in sqlite3_db_status()
21897 + pSchema->trigHash.count in sqlite3_db_status()
21898 + pSchema->idxHash.count in sqlite3_db_status()
21899 + pSchema->fkeyHash.count in sqlite3_db_status()
21901 nByte += sqlite3_msize(pSchema->tblHash.ht); in sqlite3_db_status()
21902 nByte += sqlite3_msize(pSchema->trigHash.ht); in sqlite3_db_status()
21903 nByte += sqlite3_msize(pSchema->idxHash.ht); in sqlite3_db_status()
21904 nByte += sqlite3_msize(pSchema->fkeyHash.ht); in sqlite3_db_status()
21906 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
21909 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ in sqlite3_db_status()
21914 db->pnBytesFreed = 0; in sqlite3_db_status()
21931 db->pnBytesFreed = &nByte; in sqlite3_db_status()
21932 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ in sqlite3_db_status()
21936 db->pnBytesFreed = 0; in sqlite3_db_status()
21938 *pHighwater = 0; /* IMP: R-64479-57858 */ in sqlite3_db_status()
21960 for(i=0; i<db->nDb; i++){ in sqlite3_db_status()
21961 if( db->aDb[i].pBt ){ in sqlite3_db_status()
21962 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); in sqlite3_db_status()
21966 *pHighwater = 0; /* IMP: R-42420-56072 */ in sqlite3_db_status()
21967 /* IMP: R-54100-20147 */ in sqlite3_db_status()
21968 /* IMP: R-29431-39229 */ in sqlite3_db_status()
21973 /* Set *pCurrent to non-zero if there are unresolved deferred foreign in sqlite3_db_status()
21978 *pHighwater = 0; /* IMP: R-11967-56545 */ in sqlite3_db_status()
21979 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; in sqlite3_db_status()
21987 sqlite3_mutex_leave(db->mutex); in sqlite3_db_status()
22007 ** There is only one exported symbol in this file - the function
22016 ** 1970-01-01 00:00:00 is JD 2440587.5
22017 ** 2000-01-01 00:00:00 is JD 2451544.5
22019 ** This implementation requires years to be expressed as a 4-digit number
22020 ** which means that only dates between 0000-01-01 and 9999-12-31 can
22026 ** use the julian calendar for dates prior to 1582-10-15 and for some
22034 ** ISBN 0-943396-61-1
22035 ** Willmann-Bell, Inc
22081 ** of a four-character format specifiers ABCD is:
22095 ** Example: To translate an ISO-8601 date YYYY-MM-DD, the format would
22096 ** be "40f-21a-20c". The "40f-" indicates the 4-digit year followed by "-".
22097 ** The "21a-" indicates the 2-digit month followed by "-". The "20c" indicates
22098 ** the 2-digit day which is the last integer in the set.
22111 char N = zFormat[0] - '0'; in getDigits()
22112 char min = zFormat[1] - '0'; in getDigits()
22117 max = aMx[zFormat[2] - 'a']; in getDigits()
22120 while( N-- ){ in getDigits()
22124 val = val*10 + *zDate - '0'; in getDigits()
22141 ** Parse a timezone extension on the end of a date-time.
22144 ** (+/-)HH:MM
22151 ** of change in p->tz and return 0. If a parser error occurs,
22152 ** return non-zero.
22161 p->tz = 0; in parseTimezone()
22163 if( c=='-' ){ in parseTimezone()
22164 sgn = -1; in parseTimezone()
22178 p->tz = sgn*(nMn + nHr*60); in parseTimezone()
22181 p->tzSet = 1; in parseTimezone()
22209 ms = ms*10.0 + *zDate - '0'; in parseHhMmSs()
22218 p->validJD = 0; in parseHhMmSs()
22219 p->rawS = 0; in parseHhMmSs()
22220 p->validHMS = 1; in parseHhMmSs()
22221 p->h = h; in parseHhMmSs()
22222 p->m = m; in parseHhMmSs()
22223 p->s = s + ms; in parseHhMmSs()
22225 p->validTZ = (p->tz!=0)?1:0; in parseHhMmSs()
22234 p->isError = 1; in datetimeError()
22238 ** Convert from YYYY-MM-DD HH:MM:SS to julian day. We always assume
22239 ** that the YYYY-MM-DD is according to the Gregorian calendar.
22246 if( p->validJD ) return; in computeJD()
22247 if( p->validYMD ){ in computeJD()
22248 Y = p->Y; in computeJD()
22249 M = p->M; in computeJD()
22250 D = p->D; in computeJD()
22252 Y = 2000; /* If no YMD specified, assume 2000-Jan-01 */ in computeJD()
22256 if( Y<-4713 || Y>9999 || p->rawS ){ in computeJD()
22261 Y--; in computeJD()
22265 B = 2 - A + (A/4); in computeJD()
22268 p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000); in computeJD()
22269 p->validJD = 1; in computeJD()
22270 if( p->validHMS ){ in computeJD()
22271 p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000); in computeJD()
22272 if( p->validTZ ){ in computeJD()
22273 p->iJD -= p->tz*60000; in computeJD()
22274 p->validYMD = 0; in computeJD()
22275 p->validHMS = 0; in computeJD()
22276 p->validTZ = 0; in computeJD()
22284 ** YYYY-MM-DD HH:MM:SS.FFF
22285 ** YYYY-MM-DD HH:MM:SS
22286 ** YYYY-MM-DD HH:MM
22287 ** YYYY-MM-DD
22290 ** on success and 1 if the input string is not a well-formed
22296 if( zDate[0]=='-' ){ in parseYyyyMmDd()
22302 if( getDigits(zDate, "40f-21a-21d", &Y, &M, &D)!=3 ){ in parseYyyyMmDd()
22310 p->validHMS = 0; in parseYyyyMmDd()
22314 p->validJD = 0; in parseYyyyMmDd()
22315 p->validYMD = 1; in parseYyyyMmDd()
22316 p->Y = neg ? -Y : Y; in parseYyyyMmDd()
22317 p->M = M; in parseYyyyMmDd()
22318 p->D = D; in parseYyyyMmDd()
22319 if( p->validTZ ){ in parseYyyyMmDd()
22331 p->iJD = sqlite3StmtCurrentTime(context); in setDateTimeToCurrent()
22332 if( p->iJD>0 ){ in setDateTimeToCurrent()
22333 p->validJD = 1; in setDateTimeToCurrent()
22344 ** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
22347 p->s = r; in setRawDateNumber()
22348 p->rawS = 1; in setRawDateNumber()
22350 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5); in setRawDateNumber()
22351 p->validJD = 1; in setRawDateNumber()
22361 ** YYYY-MM-DD HH:MM:SS.FFF +/-HH:MM
22365 ** In the first form, the +/-HH:MM is always optional. The fractional
22390 /* The julian day number for 9999-12-31 23:59:59.999 is 5373484.4999999.
22413 if( p->validYMD ) return; in computeYMD()
22414 if( !p->validJD ){ in computeYMD()
22415 p->Y = 2000; in computeYMD()
22416 p->M = 1; in computeYMD()
22417 p->D = 1; in computeYMD()
22418 }else if( !validJulianDay(p->iJD) ){ in computeYMD()
22422 Z = (int)((p->iJD + 43200000)/86400000); in computeYMD()
22423 A = (int)((Z - 1867216.25)/36524.25); in computeYMD()
22424 A = Z + 1 + A - (A/4); in computeYMD()
22426 C = (int)((B - 122.1)/365.25); in computeYMD()
22428 E = (int)((B-D)/30.6001); in computeYMD()
22430 p->D = B - D - X1; in computeYMD()
22431 p->M = E<14 ? E-1 : E-13; in computeYMD()
22432 p->Y = p->M>2 ? C - 4716 : C - 4715; in computeYMD()
22434 p->validYMD = 1; in computeYMD()
22442 if( p->validHMS ) return; in computeHMS()
22444 s = (int)((p->iJD + 43200000) % 86400000); in computeHMS()
22445 p->s = s/1000.0; in computeHMS()
22446 s = (int)p->s; in computeHMS()
22447 p->s -= s; in computeHMS()
22448 p->h = s/3600; in computeHMS()
22449 s -= p->h*3600; in computeHMS()
22450 p->m = s/60; in computeHMS()
22451 p->s += s - p->m*60; in computeHMS()
22452 p->rawS = 0; in computeHMS()
22453 p->validHMS = 1; in computeHMS()
22468 p->validYMD = 0; in clearYMD_HMS_TZ()
22469 p->validHMS = 0; in clearYMD_HMS_TZ()
22470 p->validTZ = 0; in clearYMD_HMS_TZ()
22480 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
22494 ** using whatever operating-system specific localtime facility that
22496 ** non-zero on any kind of error.
22501 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
22559 /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only in localtimeOffset()
22577 t = (time_t)(x.iJD/1000 - 21086676*(i64)10000); in localtimeOffset()
22579 sqlite3_result_error(pCtx, "local time unavailable", -1); in localtimeOffset()
22597 return y.iJD - x.iJD; in localtimeOffset()
22606 ** Where NNN is an arbitrary floating-point number and "days" can be one
22625 ** Process a modifier to a date-time stamp. The modifiers are
22644 ** is in a system call (i.e. localtime()), then an error message is written
22666 p->iJD += localtimeOffset(p, pCtx, &rc); in parseModifier()
22676 ** Treat the current value of p->s as the number of in parseModifier()
22679 if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){ in parseModifier()
22680 r = p->s*1000.0 + 210866760000000.0; in parseModifier()
22683 p->iJD = (sqlite3_int64)(r + 0.5); in parseModifier()
22684 p->validJD = 1; in parseModifier()
22685 p->rawS = 0; in parseModifier()
22691 if( p->tzSet==0 ){ in parseModifier()
22696 p->iJD -= c1; in parseModifier()
22698 p->iJD += c1 - localtimeOffset(p, pCtx, &rc); in parseModifier()
22700 p->tzSet = 1; in parseModifier()
22714 ** date is already on the appropriate weekday, this is a no-op. in parseModifier()
22721 p->validTZ = 0; in parseModifier()
22722 p->validJD = 0; in parseModifier()
22724 Z = ((p->iJD + 129600000)/86400000) % 7; in parseModifier()
22725 if( Z>n ) Z -= 7; in parseModifier()
22726 p->iJD += (n - Z)*86400000; in parseModifier()
22740 if( !p->validJD && !p->validYMD && !p->validHMS ) break; in parseModifier()
22743 p->validHMS = 1; in parseModifier()
22744 p->h = p->m = 0; in parseModifier()
22745 p->s = 0.0; in parseModifier()
22746 p->rawS = 0; in parseModifier()
22747 p->validTZ = 0; in parseModifier()
22748 p->validJD = 0; in parseModifier()
22750 p->D = 1; in parseModifier()
22753 p->M = 1; in parseModifier()
22754 p->D = 1; in parseModifier()
22762 case '-': in parseModifier()
22781 /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the in parseModifier()
22793 tx.iJD -= 43200000; in parseModifier()
22795 tx.iJD -= day*86400000; in parseModifier()
22796 if( z[0]=='-' ) tx.iJD = -tx.iJD; in parseModifier()
22799 p->iJD += tx.iJD; in parseModifier()
22810 if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--; in parseModifier()
22813 rRounder = r<0 ? -0.5 : +0.5; in parseModifier()
22817 && r>-aXformType[i].rLimit && r<aXformType[i].rLimit in parseModifier()
22823 p->M += (int)r; in parseModifier()
22824 x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12; in parseModifier()
22825 p->Y += x; in parseModifier()
22826 p->M -= x*12; in parseModifier()
22827 p->validJD = 0; in parseModifier()
22828 r -= (int)r; in parseModifier()
22834 p->Y += y; in parseModifier()
22835 p->validJD = 0; in parseModifier()
22836 r -= (int)r; in parseModifier()
22841 p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder); in parseModifier()
22857 ** Process time function arguments. argv[0] is a date-time stamp.
22894 if( p->isError || !validJulianDay(p->iJD) ) return 1; in isDate()
22924 ** Return YYYY-MM-DD HH:MM:SS
22935 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d", in datetimeFunc()
22937 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in datetimeFunc()
22956 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in timeFunc()
22963 ** Return YYYY-MM-DD
22974 sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D); in dateFunc()
22975 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in dateFunc()
22986 ** %H hour 00-24
22987 ** %j day of year 000-366
22989 ** %m month 01-12
22990 ** %M minute 00-59
22991 ** %s seconds since 1970-01-01
22992 ** %S seconds 00-59
22993 ** %w day of week 0-6 sunday==0
22994 ** %W week of year 00-53
22995 ** %Y year 0000-9999
23012 if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return; in strftimeFunc()
23047 testcase( n==sizeof(zBuf)-1 ); in strftimeFunc()
23049 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); in strftimeFunc()
23050 testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ); in strftimeFunc()
23053 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){ in strftimeFunc()
23088 nDay = (int)((x.iJD-y.iJD+43200000)/86400000); in strftimeFunc()
23092 sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7); in strftimeFunc()
23108 i64 iS = (i64)(x.iJD/1000 - 21086676*(i64)10000); in strftimeFunc()
23127 sqlite3_result_text(context, z, -1, in strftimeFunc()
23176 ** If the library is compiled to omit the full-scale date and time
23182 ** This function uses the C-library functions time(), gmtime()
23184 ** as the user-data for the function.
23203 t = iT/1000 - 10000*(sqlite3_int64)21086676; in currentTimeFunc()
23214 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); in currentTimeFunc()
23227 PURE_DATE(julianday, -1, 0, 0, juliandayFunc ), in sqlite3RegisterDateTimeFunctions()
23228 PURE_DATE(date, -1, 0, 0, dateFunc ), in sqlite3RegisterDateTimeFunctions()
23229 PURE_DATE(time, -1, 0, 0, timeFunc ), in sqlite3RegisterDateTimeFunctions()
23230 PURE_DATE(datetime, -1, 0, 0, datetimeFunc ), in sqlite3RegisterDateTimeFunctions()
23231 PURE_DATE(strftime, -1, 0, 0, strftimeFunc ), in sqlite3RegisterDateTimeFunctions()
23237 STR_FUNCTION(current_date, 0, "%Y-%m-%d", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
23238 STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc), in sqlite3RegisterDateTimeFunctions()
23270 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
23288 ** from within OsOpen()), but some third-party implementations may.
23328 if( pId->pMethods ){ in sqlite3OsClose()
23329 pId->pMethods->xClose(pId); in sqlite3OsClose()
23330 pId->pMethods = 0; in sqlite3OsClose()
23335 return id->pMethods->xRead(id, pBuf, amt, offset); in sqlite3OsRead()
23339 return id->pMethods->xWrite(id, pBuf, amt, offset); in sqlite3OsWrite()
23342 return id->pMethods->xTruncate(id, size); in sqlite3OsTruncate()
23346 return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK; in sqlite3OsSync()
23350 return id->pMethods->xFileSize(id, pSize); in sqlite3OsFileSize()
23354 return id->pMethods->xLock(id, lockType); in sqlite3OsLock()
23357 return id->pMethods->xUnlock(id, lockType); in sqlite3OsUnlock()
23361 return id->pMethods->xCheckReservedLock(id, pResOut); in sqlite3OsCheckReservedLock()
23373 if( id->pMethods==0 ) return SQLITE_NOTFOUND; in sqlite3OsFileControl()
23383 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM in sqlite3OsFileControl()
23391 ** The CKPT_DONE and CKPT_START file-controls are write-only signals in sqlite3OsFileControl()
23398 return id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControl()
23401 if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg); in sqlite3OsFileControlHint()
23405 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; in sqlite3OsSectorSize()
23409 return id->pMethods->xDeviceCharacteristics(id); in sqlite3OsDeviceCharacteristics()
23413 return id->pMethods->xShmLock(id, offset, n, flags); in sqlite3OsShmLock()
23416 id->pMethods->xShmBarrier(id); in sqlite3OsShmBarrier()
23419 return id->pMethods->xShmUnmap(id, deleteFlag); in sqlite3OsShmUnmap()
23429 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); in sqlite3OsShmMap()
23437 return id->pMethods->xFetch(id, iOff, iAmt, pp); in sqlite3OsFetch()
23440 return id->pMethods->xUnfetch(id, iOff, p); in sqlite3OsUnfetch()
23443 /* No-op stubs to use when memory-mapped I/O is disabled */
23470 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x1087f7f, pFlagsOut); in sqlite3OsOpen()
23471 assert( rc==SQLITE_OK || pFile->pMethods==0 ); in sqlite3OsOpen()
23477 return pVfs->xDelete(pVfs, zPath, dirSync); in sqlite3OsDelete()
23486 return pVfs->xAccess(pVfs, zPath, flags, pResOut); in sqlite3OsAccess()
23496 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); in sqlite3OsFullPathname()
23500 return pVfs->xDlOpen(pVfs, zPath); in sqlite3OsDlOpen()
23503 pVfs->xDlError(pVfs, nByte, zBufOut); in sqlite3OsDlError()
23506 return pVfs->xDlSym(pVfs, pHdle, zSym); in sqlite3OsDlSym()
23509 pVfs->xDlClose(pVfs, pHandle); in sqlite3OsDlClose()
23519 return pVfs->xRandomness(pVfs, nByte, zBufOut); in sqlite3OsRandomness()
23524 return pVfs->xSleep(pVfs, nMicro); in sqlite3OsSleep()
23527 return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0; in sqlite3OsGetLastError()
23531 /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64() in sqlite3OsCurrentTimeInt64()
23537 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in sqlite3OsCurrentTimeInt64()
23538 rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut); in sqlite3OsCurrentTimeInt64()
23541 rc = pVfs->xCurrentTime(pVfs, &r); in sqlite3OsCurrentTimeInt64()
23556 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); in sqlite3OsOpenMalloc()
23611 for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){ in sqlite3_vfs_find()
23613 if( strcmp(zVfs, pVfs->zName)==0 ) break; in sqlite3_vfs_find()
23625 /* No-op */ in vfsUnlink()
23627 vfsList = pVfs->pNext; in vfsUnlink()
23630 while( p->pNext && p->pNext!=pVfs ){ in vfsUnlink()
23631 p = p->pNext; in vfsUnlink()
23633 if( p->pNext==pVfs ){ in vfsUnlink()
23634 p->pNext = pVfs->pNext; in vfsUnlink()
23658 pVfs->pNext = vfsList; in sqlite3_vfs_register()
23661 pVfs->pNext = vfsList->pNext; in sqlite3_vfs_register()
23662 vfsList->pNext = pVfs; in sqlite3_vfs_register()
23704 ** Most malloc failures are non-benign. After they occur, SQLite
23728 ** we have to locate the state vector at run-time. In the more common
23758 ** indicates that subsequent malloc failures are non-benign.
23789 ** This file contains a no-op memory allocation drivers for use when
23799 ** used when no other memory allocator is specified using compile-time
23805 ** No-op versions of all memory allocation routines
23818 ** Populate the low-level memory allocation function pointers in
23851 ** This file contains low-level memory allocation drivers for when
23852 ** SQLite will use the standard C-library malloc/realloc/free interface
23855 ** This file contains implementations of the low-level memory allocation
23863 ** C-preprocessor macro summary:
23870 ** a different name, using a separate -D
23886 ** used when no other memory allocator is specified using compile-time
23906 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
23911 ** Use standard C library malloc and free on non-Apple systems.
23928 ** -DSQLITE_WITHOUT_MSIZE. Using the _msize() function also requires
23962 ** For this low-level routine, we are guaranteed that nByte>0 because
23996 ** For this low-level routine, we already know that pPrior!=0 since
23998 ** by higher-level routines.
24006 p--; in sqlite3MemFree()
24023 p--; in sqlite3MemSize()
24032 ** For this low-level interface, we know that pPrior!=0. Cases where
24033 ** pPrior==0 while have been intercepted by higher-level routine and
24035 ** cases where nByte<=0 will have been intercepted by higher-level
24051 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
24052 p--; in sqlite3MemRealloc()
24112 ** Populate the low-level memory allocation function pointers in
24145 ** This file contains low-level memory allocation drivers for when
24146 ** SQLite will use the standard C-library malloc/realloc/free interface
24151 ** This file contains implementations of the low-level memory allocation
24177 ** ------------------------------------------------------------------------
24179 ** ------------------------------------------------------------------------
24263 if( i>NCSIZE-1 ){ in adjustStats()
24264 i = NCSIZE - 1; in adjustStats()
24273 mem.nCurrent[i]--; in adjustStats()
24291 p--; in sqlite3MemsysGetHeader()
24292 assert( p->iForeGuard==(int)FOREGUARD ); in sqlite3MemsysGetHeader()
24293 nReserve = ROUND8(p->iSize); in sqlite3MemsysGetHeader()
24301 while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 ); in sqlite3MemsysGetHeader()
24314 return (int)pHdr->iSize; in sqlite3MemSize()
24347 ** Fill a buffer with pseudo-random bytes. This is used to preset
24356 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
24361 nByte -= 4; in randomFill()
24363 while( nByte-- > 0 ){ in randomFill()
24364 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001); in randomFill()
24392 pHdr->pNext = 0; in sqlite3MemMalloc()
24393 pHdr->pPrev = mem.pLast; in sqlite3MemMalloc()
24395 mem.pLast->pNext = pHdr; in sqlite3MemMalloc()
24400 pHdr->iForeGuard = FOREGUARD; in sqlite3MemMalloc()
24401 pHdr->eType = MEMTYPE_HEAP; in sqlite3MemMalloc()
24402 pHdr->nBacktraceSlots = mem.nBacktrace; in sqlite3MemMalloc()
24403 pHdr->nTitle = mem.nTitle; in sqlite3MemMalloc()
24406 pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1; in sqlite3MemMalloc()
24407 memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*)); in sqlite3MemMalloc()
24410 mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]); in sqlite3MemMalloc()
24413 pHdr->nBacktrace = 0; in sqlite3MemMalloc()
24418 pHdr->iSize = nByte; in sqlite3MemMalloc()
24423 memset(((char*)pInt)+nByte, 0x65, nReserve-nByte); in sqlite3MemMalloc()
24441 pBt -= pHdr->nBacktraceSlots; in sqlite3MemFree()
24443 if( pHdr->pPrev ){ in sqlite3MemFree()
24444 assert( pHdr->pPrev->pNext==pHdr ); in sqlite3MemFree()
24445 pHdr->pPrev->pNext = pHdr->pNext; in sqlite3MemFree()
24448 mem.pFirst = pHdr->pNext; in sqlite3MemFree()
24450 if( pHdr->pNext ){ in sqlite3MemFree()
24451 assert( pHdr->pNext->pPrev==pHdr ); in sqlite3MemFree()
24452 pHdr->pNext->pPrev = pHdr->pPrev; in sqlite3MemFree()
24455 mem.pLast = pHdr->pPrev; in sqlite3MemFree()
24458 z -= pHdr->nTitle; in sqlite3MemFree()
24459 adjustStats((int)pHdr->iSize, -1); in sqlite3MemFree()
24460 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) + in sqlite3MemFree()
24461 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle); in sqlite3MemFree()
24479 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */ in sqlite3MemRealloc()
24483 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize)); in sqlite3MemRealloc()
24484 if( nByte>pOldHdr->iSize ){ in sqlite3MemRealloc()
24485 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize); in sqlite3MemRealloc()
24493 ** Populate the low-level memory allocation function pointers in
24517 assert( pHdr->iForeGuard==FOREGUARD ); in sqlite3MemdebugSetType()
24518 pHdr->eType = eType; in sqlite3MemdebugSetType()
24536 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugHasType()
24537 if( (pHdr->eType&eType)==0 ){ in sqlite3MemdebugHasType()
24558 assert( pHdr->iForeGuard==FOREGUARD ); /* Allocation is valid */ in sqlite3MemdebugNoType()
24559 if( (pHdr->eType&eType)!=0 ){ in sqlite3MemdebugNoType()
24588 if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1; in sqlite3MemdebugSettitle()
24597 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugSync()
24599 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugSync()
24600 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]); in sqlite3MemdebugSync()
24619 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){ in sqlite3MemdebugDump()
24621 z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle; in sqlite3MemdebugDump()
24623 pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???"); in sqlite3MemdebugDump()
24624 if( pHdr->nBacktrace ){ in sqlite3MemdebugDump()
24627 pBt -= pHdr->nBacktraceSlots; in sqlite3MemdebugDump()
24628 backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out)); in sqlite3MemdebugDump()
24633 for(i=0; i<NCSIZE-1; i++){ in sqlite3MemdebugDump()
24639 if( mem.nAlloc[NCSIZE-1] ){ in sqlite3MemdebugDump()
24641 NCSIZE*8-8, mem.nAlloc[NCSIZE-1], in sqlite3MemdebugDump()
24642 mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]); in sqlite3MemdebugDump()
24694 ** mean that the library will use a memory-pool by default, just that
24735 ** two fields form a double-linked list of chunks of related sizes.
24772 ** True if we are evaluating an out-of-memory callback.
24800 u32 aiSmall[MX_SMALL-1]; /* For sizes 2 through MX_SMALL, inclusive */
24833 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Unlink()
24835 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Unlink()
24836 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Unlink()
24839 memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]); in memsys3Unlink()
24868 assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 ); in memsys3Link()
24869 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3Link()
24870 assert( size==mem3.aPool[i+size-1].u.hdr.prevSize ); in memsys3Link()
24873 memsys3LinkIntoList(i, &mem3.aiSmall[size-2]); in memsys3Link()
24912 ** size parameters for check-out and return a pointer to the
24919 assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ); in memsys3Checkout()
24920 assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock ); in memsys3Checkout()
24921 x = mem3.aPool[i-1].u.hdr.size4x; in memsys3Checkout()
24922 mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2); in memsys3Checkout()
24923 mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock; in memsys3Checkout()
24924 mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2; in memsys3Checkout()
24936 if( nBlock>=mem3.szKeyBlk-1 ){ in memsys3FromKeyBlk()
24946 newi = mem3.iKeyBlk + mem3.szKeyBlk - nBlock; in memsys3FromKeyBlk()
24948 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = nBlock; in memsys3FromKeyBlk()
24949 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x |= 2; in memsys3FromKeyBlk()
24950 mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1; in memsys3FromKeyBlk()
24951 mem3.szKeyBlk -= nBlock; in memsys3FromKeyBlk()
24952 mem3.aPool[newi-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FromKeyBlk()
24953 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FromKeyBlk()
24954 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FromKeyBlk()
24984 size = mem3.aPool[i-1].u.hdr.size4x; in memsys3Merge()
24988 assert( i > mem3.aPool[i-1].u.hdr.prevSize ); in memsys3Merge()
24989 prev = i - mem3.aPool[i-1].u.hdr.prevSize; in memsys3Merge()
24994 size = i + size/4 - prev; in memsys3Merge()
24995 x = mem3.aPool[prev-1].u.hdr.size4x & 2; in memsys3Merge()
24996 mem3.aPool[prev-1].u.hdr.size4x = size*4 | x; in memsys3Merge()
24997 mem3.aPool[prev+size-1].u.hdr.prevSize = size; in memsys3Merge()
25037 i = mem3.aiSmall[nBlock-2]; in memsys3MallocUnsafe()
25039 memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]); in memsys3MallocUnsafe()
25045 if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){ in memsys3MallocUnsafe()
25078 for(i=0; i<MX_SMALL-1; i++){ in memsys3MallocUnsafe()
25105 i = p - mem3.aPool; in memsys3FreeUnsafe()
25106 assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 ); in memsys3FreeUnsafe()
25107 size = mem3.aPool[i-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
25109 mem3.aPool[i-1].u.hdr.size4x &= ~1; in memsys3FreeUnsafe()
25110 mem3.aPool[i+size-1].u.hdr.prevSize = size; in memsys3FreeUnsafe()
25111 mem3.aPool[i+size-1].u.hdr.size4x &= ~2; in memsys3FreeUnsafe()
25116 while( (mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x&2)==0 ){ in memsys3FreeUnsafe()
25117 size = mem3.aPool[mem3.iKeyBlk-1].u.hdr.prevSize; in memsys3FreeUnsafe()
25118 mem3.iKeyBlk -= size; in memsys3FreeUnsafe()
25121 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
25122 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
25123 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
25125 x = mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x & 2; in memsys3FreeUnsafe()
25126 while( (mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x&1)==0 ){ in memsys3FreeUnsafe()
25128 mem3.szKeyBlk += mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.size4x/4; in memsys3FreeUnsafe()
25129 mem3.aPool[mem3.iKeyBlk-1].u.hdr.size4x = mem3.szKeyBlk*4 | x; in memsys3FreeUnsafe()
25130 mem3.aPool[mem3.iKeyBlk+mem3.szKeyBlk-1].u.hdr.prevSize = mem3.szKeyBlk; in memsys3FreeUnsafe()
25137 ** size returned omits the 8-byte header overhead. This only
25144 assert( (pBlock[-1].u.hdr.size4x&1)!=0 ); in memsys3Size()
25145 return (pBlock[-1].u.hdr.size4x&~3)*2 - 4; in memsys3Size()
25155 return ((n+11)&~7) - 4; in memsys3Roundup()
25195 if( nBytes<=nOld && nBytes>=nOld-128 ){ in memsys3Realloc()
25224 mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2; in memsys3Init()
25270 size = mem3.aPool[i-1].u.hdr.size4x; in sqlite3Memsys3Dump()
25276 if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){ in sqlite3Memsys3Dump()
25281 if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){ in sqlite3Memsys3Dump()
25287 fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8); in sqlite3Memsys3Dump()
25289 fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8, in sqlite3Memsys3Dump()
25293 for(i=0; i<MX_SMALL-1; i++){ in sqlite3Memsys3Dump()
25298 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
25307 (mem3.aPool[j-1].u.hdr.size4x/4)*8-8); in sqlite3Memsys3Dump()
25312 fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szKeyBlk*8); in sqlite3Memsys3Dump()
25313 fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnKeyBlk*8); in sqlite3Memsys3Dump()
25329 ** Populate the low-level memory allocation function pointers in
25390 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
25399 ** N >= M*(1 + log2(n)/2) - n + 1
25428 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
25463 u64 totalAlloc; /* Total of all malloc calls - includes internal frag */
25494 ** structures, return a pointer to the idx-th such link.
25508 next = MEM5LINK(i)->next; in memsys5Unlink()
25509 prev = MEM5LINK(i)->prev; in memsys5Unlink()
25513 MEM5LINK(prev)->next = next; in memsys5Unlink()
25516 MEM5LINK(next)->prev = prev; in memsys5Unlink()
25531 x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize]; in memsys5Link()
25532 MEM5LINK(i)->prev = -1; in memsys5Link()
25535 MEM5LINK(x)->prev = i; in memsys5Link()
25557 i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom); in memsys5Size()
25612 iBin--; in memsys5MallocUnsafe()
25623 mem5.totalExcess += iFullSz - nByte; in memsys5MallocUnsafe()
25650 iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom); in memsys5FreeUnsafe()
25652 /* Check that the pointer pOld points to a valid, non-free block. */ in memsys5FreeUnsafe()
25654 assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 ); in memsys5FreeUnsafe()
25659 assert( iBlock+size-1<(u32)mem5.nBlock ); in memsys5FreeUnsafe()
25662 mem5.aCtrl[iBlock+size-1] |= CTRL_FREE; in memsys5FreeUnsafe()
25667 mem5.currentCount--; in memsys5FreeUnsafe()
25668 mem5.currentOut -= size*mem5.szAtom; in memsys5FreeUnsafe()
25677 iBuddy = iBlock - size; in memsys5FreeUnsafe()
25739 ** memsys5Round(). Hence nBytes is always a non-negative power
25748 assert( (nBytes&(nBytes-1))==0 ); /* EV: R-46199-30249 */ in memsys5Realloc()
25771 ** 32-bit signed integer. Hence the largest allocation is 0x40000000
25784 ** Examples: memsys5Log(1) -> 0
25785 ** memsys5Log(2) -> 1
25786 ** memsys5Log(4) -> 2
25787 ** memsys5Log(5) -> 3
25788 ** memsys5Log(8) -> 3
25789 ** memsys5Log(9) -> 4
25793 for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++); in memsys5Log()
25818 assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 ); in memsys5Init()
25836 mem5.aiFreelist[ii] = -1; in memsys5Init()
25840 for(ii=LOGMAX; ii>=0; ii--){ in memsys5Init()
25890 for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){} in sqlite3Memsys5Dump()
25981 ** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
25992 #define SQLITE_MUTEX_WARNONCONTENTION (-1)
26002 return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex); in checkMutexHeld()
26005 return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex); in checkMutexNotheld()
26036 p->iType = iType; in checkMutexAlloc()
26039 if( iType-2>=ArraySize(staticMutexes) ){ in checkMutexAlloc()
26044 p = &staticMutexes[iType-2]; in checkMutexAlloc()
26047 if( p->mutex==0 ){ in checkMutexAlloc()
26048 p->mutex = pGlobalMutexMethods->xMutexAlloc(iType); in checkMutexAlloc()
26049 if( p->mutex==0 ){ in checkMutexAlloc()
26069 if( ((CheckMutex*)p)->iType<2 ) in checkMutexFree()
26073 pGlobalMutexMethods->xMutexFree(pCheck->mutex); in checkMutexFree()
26088 if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){ in checkMutexEnter()
26089 if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){ in checkMutexEnter()
26093 "illegal multi-threaded access to database connection" in checkMutexEnter()
26096 pGlobalMutexMethods->xMutexEnter(pCheck->mutex); in checkMutexEnter()
26104 return pGlobalMutexMethods->xMutexTry(pCheck->mutex); in checkMutexTry()
26112 pGlobalMutexMethods->xMutexLeave(pCheck->mutex); in checkMutexLeave()
26142 assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE ); in sqlite3MutexWarnOnContention()
26143 pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION; in sqlite3MutexWarnOnContention()
26171 pTo->xMutexInit = pFrom->xMutexInit; in sqlite3MutexInit()
26172 pTo->xMutexEnd = pFrom->xMutexEnd; in sqlite3MutexInit()
26173 pTo->xMutexFree = pFrom->xMutexFree; in sqlite3MutexInit()
26174 pTo->xMutexEnter = pFrom->xMutexEnter; in sqlite3MutexInit()
26175 pTo->xMutexTry = pFrom->xMutexTry; in sqlite3MutexInit()
26176 pTo->xMutexLeave = pFrom->xMutexLeave; in sqlite3MutexInit()
26177 pTo->xMutexHeld = pFrom->xMutexHeld; in sqlite3MutexInit()
26178 pTo->xMutexNotheld = pFrom->xMutexNotheld; in sqlite3MutexInit()
26180 pTo->xMutexAlloc = pFrom->xMutexAlloc; in sqlite3MutexInit()
26269 ** this function is a no-op.
26313 ** here are place-holders. Applications can substitute working
26314 ** mutex routines at start-time using the
26387 return p==0 || p->cnt>0; in debugMutexHeld()
26391 return p==0 || p->cnt==0; in debugMutexNotheld()
26406 static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_VFS3 - 1]; in debugMutexAlloc()
26413 pNew->id = id; in debugMutexAlloc()
26414 pNew->cnt = 0; in debugMutexAlloc()
26420 if( id-2<0 || id-2>=ArraySize(aStatic) ){ in debugMutexAlloc()
26425 pNew = &aStatic[id-2]; in debugMutexAlloc()
26426 pNew->id = id; in debugMutexAlloc()
26438 assert( p->cnt==0 ); in debugMutexFree()
26439 if( p->id==SQLITE_MUTEX_RECURSIVE || p->id==SQLITE_MUTEX_FAST ){ in debugMutexFree()
26461 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexEnter()
26462 p->cnt++; in debugMutexEnter()
26466 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexTry()
26467 p->cnt++; in debugMutexTry()
26480 p->cnt--; in debugMutexLeave()
26481 assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) ); in debugMutexLeave()
26503 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
26504 ** is used regardless of the run-time threadsafety setting.
26544 ** home-grown mutexes. Encapsulate these conditions into a single #define.
26587 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
26593 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); in pthreadMutexHeld()
26596 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; in pthreadMutexNotheld()
26649 ** cases where it really needs one. If a faster non-recursive mutex
26690 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
26696 pthread_mutex_init(&p->mutex, &recursiveAttr); in pthreadMutexAlloc()
26700 p->id = SQLITE_MUTEX_RECURSIVE; in pthreadMutexAlloc()
26708 pthread_mutex_init(&p->mutex, 0); in pthreadMutexAlloc()
26710 p->id = SQLITE_MUTEX_FAST; in pthreadMutexAlloc()
26717 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ in pthreadMutexAlloc()
26722 p = &staticMutexes[iType-2]; in pthreadMutexAlloc()
26727 assert( p==0 || p->id==iType ); in pthreadMutexAlloc()
26739 assert( p->nRef==0 ); in pthreadMutexFree()
26741 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) in pthreadMutexFree()
26744 pthread_mutex_destroy(&p->mutex); in pthreadMutexFree()
26766 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexEnter()
26771 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexEnter()
26772 ** and p->owner are equal if p->owner changes between two values in pthreadMutexEnter()
26774 ** This implementation also assumes a coherent cache - that in pthreadMutexEnter()
26781 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexEnter()
26782 p->nRef++; in pthreadMutexEnter()
26784 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
26785 assert( p->nRef==0 ); in pthreadMutexEnter()
26786 p->owner = self; in pthreadMutexEnter()
26787 p->nRef = 1; in pthreadMutexEnter()
26791 /* Use the built-in recursive mutexes if they are available. in pthreadMutexEnter()
26793 pthread_mutex_lock(&p->mutex); in pthreadMutexEnter()
26795 assert( p->nRef>0 || p->owner==0 ); in pthreadMutexEnter()
26796 p->owner = pthread_self(); in pthreadMutexEnter()
26797 p->nRef++; in pthreadMutexEnter()
26802 if( p->trace ){ in pthreadMutexEnter()
26803 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexEnter()
26809 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); in pthreadMutexTry()
26814 ** is atomic - that it cannot be deceived into thinking self in pthreadMutexTry()
26815 ** and p->owner are equal if p->owner changes between two values in pthreadMutexTry()
26817 ** This implementation also assumes a coherent cache - that in pthreadMutexTry()
26824 if( p->nRef>0 && pthread_equal(p->owner, self) ){ in pthreadMutexTry()
26825 p->nRef++; in pthreadMutexTry()
26827 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
26828 assert( p->nRef==0 ); in pthreadMutexTry()
26829 p->owner = self; in pthreadMutexTry()
26830 p->nRef = 1; in pthreadMutexTry()
26837 /* Use the built-in recursive mutexes if they are available. in pthreadMutexTry()
26839 if( pthread_mutex_trylock(&p->mutex)==0 ){ in pthreadMutexTry()
26841 p->owner = pthread_self(); in pthreadMutexTry()
26842 p->nRef++; in pthreadMutexTry()
26851 if( rc==SQLITE_OK && p->trace ){ in pthreadMutexTry()
26852 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexTry()
26867 p->nRef--; in pthreadMutexLeave()
26868 if( p->nRef==0 ) p->owner = 0; in pthreadMutexLeave()
26870 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); in pthreadMutexLeave()
26873 if( p->nRef==0 ){ in pthreadMutexLeave()
26874 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
26877 pthread_mutex_unlock(&p->mutex); in pthreadMutexLeave()
26881 if( p->trace ){ in pthreadMutexLeave()
26882 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); in pthreadMutexLeave()
26946 ** all of the platform-specific files (os_*.c) and is #included into those
26958 ** switch. The following code should catch this problem at compile-time.
26972 ** high-performance timing routines.
26988 ** This file contains inline asm code for retrieving "high-performance"
26995 ** The following routine only works on pentium-class (or newer) processors.
26997 ** processor and returns that value. This can be used for high-res
27068 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
27092 || sqlite3_io_error_pending-- == 1 ) \
27107 sqlite3_diskfull_pending--; \
27185 ** Determine if we are dealing with Windows CE - which has a much reduced
27213 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex()
27268 return p->nRef!=0 && p->owner==GetCurrentThreadId();
27272 return p->nRef==0 || p->owner!=tid;
27317 static int winMutex_isNt = -1; /* <0 means "need to query" */
27395 ** cases where it really needs one. If a faster non-recursive mutex
27421 p->id = iType;
27424 p->trace = 1;
27428 InitializeCriticalSectionEx(&p->mutex, 0, 0);
27430 InitializeCriticalSection(&p->mutex);
27437 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
27442 p = &winMutex_staticMutexes[iType-2];
27445 InterlockedCompareExchange(&p->trace, 1, 0);
27451 assert( p==0 || p->id==iType );
27463 assert( p->nRef==0 && p->owner==0 );
27464 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){
27465 DeleteCriticalSection(&p->mutex);
27491 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27496 EnterCriticalSection(&p->mutex);
27498 assert( p->nRef>0 || p->owner==0 );
27499 p->owner = tid;
27500 p->nRef++;
27501 if( p->trace ){
27502 OSTRACE(("ENTER-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27503 tid, p->id, p, p->trace, p->nRef));
27514 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
27528 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
27533 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
27535 p->owner = tid;
27536 p->nRef++;
27544 if( p->trace ){
27545 OSTRACE(("TRY-MUTEX tid=%lu, mutex(%d)=%p (%d), owner=%lu, nRef=%d, rc=%s\n",
27546 tid, p->id, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc)));
27564 assert( p->nRef>0 );
27565 assert( p->owner==tid );
27566 p->nRef--;
27567 if( p->nRef==0 ) p->owner = 0;
27568 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
27571 LeaveCriticalSection(&p->mutex);
27573 if( p->trace ){
27574 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex(%d)=%p (%d), nRef=%d\n",
27575 tid, p->id, p, p->trace, p->nRef));
27622 ** Attempt to release up to n bytes of non-essential memory currently
27623 ** held by SQLite. An example of non-essential memory is memory used to
27630 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
27631 ** is a no-op returning zero if SQLite is not compiled with
27673 ** no-op.
27688 ** Set the soft heap-size limit for the library. An argument of
27689 ** zero disables the limit. A negative argument is a no-op used to
27704 if( rc ) return -1;
27719 excess = sqlite3_memory_used() - n;
27729 ** Set the hard heap-size limit for the library. An argument of zero
27730 ** disables the hard heap limit. A negative argument is a no-op used
27744 if( rc ) return -1;
27780 ** Return true if the heap is currently under memory pressure - in other
27838 /* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
27848 if( nUsed >= mem0.alarmThreshold - nFull ){
27853 if( nUsed >= mem0.hardLimit - nFull ){
27897 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */
27924 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd);
27940 return p<db->lookaside.pMiddle ? db->lookaside.szTrue : LOOKASIDE_SMALL;
27942 return db->lookaside.szTrue;
27959 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
27961 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
27962 assert( sqlite3_mutex_held(db->mutex) );
27966 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
27967 assert( sqlite3_mutex_held(db->mutex) );
27968 return db->lookaside.szTrue;
27984 if( p==0 ) return; /* IMP: R-49053-54554 */
28000 ** *db->pnBytesFreed.
28003 *db->pnBytesFreed += sqlite3DbMallocSize(db,p);
28008 ** connection. Calling sqlite3DbFree(D,X) for X==0 is a harmless no-op.
28009 ** The sqlite3DbFreeNN(D,X) version requires that X be non-NULL.
28012 assert( db==0 || sqlite3_mutex_held(db->mutex) );
28015 if( db->pnBytesFreed ){
28019 if( ((uptr)p)<(uptr)(db->lookaside.pEnd) ){
28021 if( ((uptr)p)>=(uptr)(db->lookaside.pMiddle) ){
28026 pBuf->pNext = db->lookaside.pSmallFree;
28027 db->lookaside.pSmallFree = pBuf;
28031 if( ((uptr)p)>=(uptr)(db->lookaside.pStart) ){
28034 memset(p, 0xaa, db->lookaside.szTrue); /* Trash freed content */
28036 pBuf->pNext = db->lookaside.pFree;
28037 db->lookaside.pFree = pBuf;
28049 assert( db==0 || sqlite3_mutex_held(db->mutex) );
28062 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
28065 sqlite3_free(pOld); /* IMP: R-26507-47431 */
28073 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
28083 nDiff = nNew - nOld;
28085 mem0.alarmThreshold-nDiff ){
28087 if( mem0.hardLimit>0 && nUsed >= mem0.hardLimit - nDiff ){
28101 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
28107 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
28119 if( n<0 ) n = 0; /* IMP: R-26507-47431 */
28163 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
28172 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
28200 assert( sqlite3_mutex_held(db->mutex) );
28201 assert( db->pnBytesFreed==0 );
28202 if( n>db->lookaside.sz ){
28203 if( !db->lookaside.bDisable ){
28204 db->lookaside.anStat[1]++;
28205 }else if( db->mallocFailed ){
28212 if( (pBuf = db->lookaside.pSmallFree)!=0 ){
28213 db->lookaside.pSmallFree = pBuf->pNext;
28214 db->lookaside.anStat[0]++;
28216 }else if( (pBuf = db->lookaside.pSmallInit)!=0 ){
28217 db->lookaside.pSmallInit = pBuf->pNext;
28218 db->lookaside.anStat[0]++;
28223 if( (pBuf = db->lookaside.pFree)!=0 ){
28224 db->lookaside.pFree = pBuf->pNext;
28225 db->lookaside.anStat[0]++;
28227 }else if( (pBuf = db->lookaside.pInit)!=0 ){
28228 db->lookaside.pInit = pBuf->pNext;
28229 db->lookaside.anStat[0]++;
28232 db->lookaside.anStat[2]++;
28236 assert( sqlite3_mutex_held(db->mutex) );
28237 assert( db->pnBytesFreed==0 );
28238 if( db->mallocFailed ){
28255 assert( sqlite3_mutex_held(db->mutex) );
28256 if( ((uptr)p)<(uptr)db->lookaside.pEnd ){
28258 if( ((uptr)p)>=(uptr)db->lookaside.pMiddle ){
28262 if( ((uptr)p)>=(uptr)db->lookaside.pStart ){
28263 if( n<=db->lookaside.szTrue ) return p;
28272 if( db->mallocFailed==0 ){
28288 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
28348 n = (int)(zEnd - zStart);
28349 while( ALWAYS(n>0) && sqlite3Isspace(zStart[n-1]) ) n--;
28362 ** Call this routine to record the fact that an OOM (out-of-memory) error
28363 ** has happened. This routine will set db->mallocFailed, and also
28368 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
28369 db->mallocFailed = 1;
28370 if( db->nVdbeExec>0 ){
28371 AtomicStore(&db->u1.isInterrupted, 1);
28374 if( db->pParse ){
28375 db->pParse->rc = SQLITE_NOMEM_BKPT;
28382 ** db->mallocFailed flag as necessary.
28388 if( db->mallocFailed && db->nVdbeExec==0 ){
28389 db->mallocFailed = 0;
28390 AtomicStore(&db->u1.isInterrupted, 0);
28391 assert( db->lookaside.bDisable>0 );
28400 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){
28405 return rc & db->errMask;
28417 ** If an OOM as occurred, then the connection error-code (the value
28422 ** Otherwise the read (and possible write) of db->mallocFailed
28426 assert( sqlite3_mutex_held(db->mutex) );
28427 if( db->mallocFailed || rc ){
28430 return rc & db->errMask;
28441 ** This file contains code for a set of "printf"-like routines. These
28452 #define etRADIX 0 /* non-decimal integer types. %x %o */
28468 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
28469 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
28476 ** An "etByte" is an 8-bit unsigned value.
28505 static const char aPrefix[] = "-x0\000X0";
28538 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
28539 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
28557 ** 16 (the number of significant digits in a 64-bit float) '0' is
28564 (*cnt)--;
28568 *val = (*val - d)*10.0;
28578 p->accError = eError;
28579 if( p->mxAlloc ) sqlite3_str_reset(p);
28580 if( eError==SQLITE_TOOBIG ) sqlite3ErrorToParser(p->db, eError);
28587 if( p->nArg<=p->nUsed ) return 0;
28588 return sqlite3_value_int64(p->apArg[p->nUsed++]);
28591 if( p->nArg<=p->nUsed ) return 0.0;
28592 return sqlite3_value_double(p->apArg[p->nUsed++]);
28595 if( p->nArg<=p->nUsed ) return 0;
28596 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
28610 if( pAccum->accError ) return 0;
28611 if( n>pAccum->nAlloc && n>pAccum->mxAlloc ){
28615 z = sqlite3DbMallocRaw(pAccum->db, n);
28632 ** Hard limit on the precision of floating-point conversions.
28652 etByte flag_leftjustify; /* True if "-" flag is present */
28662 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
28682 assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
28685 if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
28699 sqlite3_str_append(pAccum, bufpt, (int)(fmt - bufpt));
28712 precision = -1;
28715 case '-': flag_leftjustify = 1; break;
28735 unsigned wx = c - '0';
28737 wx = wx*10 + c - '0';
28749 fmt--;
28761 width = width >= -2147483647 ? -width : 0;
28783 precision = precision >= -2147483647 ? -precision : -1;
28789 px = px*10 + c - '0';
28801 --fmt;
28816 xtype = infop->type;
28827 ** flag_leftjustify TRUE if a '-' is present or if the
28832 ** always non-negative. Zero is the default.
28834 ** is -1.
28839 assert( precision>=(-1) );
28850 if( infop->flags & FLAG_SIGNED ){
28865 testcase( v==(-1) );
28868 prefix = '-';
28888 if( flag_zeropad && precision<width-(prefix!=0) ){
28889 precision = width-(prefix!=0);
28891 if( precision<etBUFSIZE-10-etBUFSIZE/3 ){
28902 bufpt = &zOut[nOut-1];
28909 *(--bufpt) = zOrd[x*2+1];
28910 *(--bufpt) = zOrd[x*2];
28913 const char *cset = &aDigits[infop->charset];
28914 u8 base = infop->base;
28916 *(--bufpt) = cset[longvalue%base];
28920 length = (int)(&zOut[nOut-1]-bufpt);
28922 *(--bufpt) = '0'; /* Zero pad */
28926 int nn = (length - 1)/3; /* Number of "," to insert */
28927 int ix = (length - 1)%3 + 1;
28928 bufpt -= nn;
28931 ix--;
28934 nn--;
28939 if( prefix ) *(--bufpt) = prefix; /* Add sign */
28940 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
28943 pre = &aPrefix[infop->prefix];
28944 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
28946 length = (int)(&zOut[nOut-1]-bufpt);
28966 realvalue = -realvalue;
28967 prefix = '-';
28971 if( xtype==etGENERIC && precision>0 ) precision--;
28975 while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
28981 ex = -1023 + (int)((u>>52)&0x7ff);
28982 if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
28998 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
28999 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
29019 if( exp<-4 || exp>precision ){
29022 precision = precision - exp;
29052 for(; e2>=0; e2--){
29062 for(e2++; e2<0; precision--, e2++){
29067 while( (precision--)>0 ){
29072 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
29074 if( bufpt[-1]=='.' ){
29078 *(--bufpt) = 0;
29084 *(bufpt++) = aDigits[infop->charset];
29086 *(bufpt++) = '-'; exp = -exp;
29102 length = (int)(bufpt-zOut);
29109 int nPad = width - length;
29110 for(i=width; i>=nPad; i--){
29111 bufpt[i] = bufpt[i-nPad];
29114 while( nPad-- ) bufpt[i++] = '0';
29121 *(va_arg(ap,int*)) = pAccum->nChar;
29167 width -= precision-1;
29169 sqlite3_str_appendchar(pAccum, width-1, ' ');
29172 while( precision-- > 1 ){
29190 if( pAccum->nChar==0
29191 && pAccum->mxAlloc
29194 && pAccum->accError==0
29199 assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
29200 pAccum->zText = bufpt;
29201 pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
29202 pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
29203 pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
29214 while( precision-- > 0 && z[0] ){
29217 length = (int)(z - (unsigned char*)bufpt);
29226 /* Adjust width to account for extra bytes in UTF-8 characters */
29227 int ii = length - 1;
29228 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
29253 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
29281 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29284 if( pToken && pToken->n ){
29285 sqlite3_str_append(pAccum, (const char*)pToken->z, pToken->n);
29294 if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
29297 pItem = &pSrc->a[k];
29299 assert( k>=0 && k<pSrc->nSrc );
29300 if( pItem->zDatabase ){
29301 sqlite3_str_appendall(pAccum, pItem->zDatabase);
29304 sqlite3_str_appendall(pAccum, pItem->zName);
29321 width -= length;
29331 sqlite3DbFree(pAccum->db, zExtra);
29346 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
29347 if( p->accError ){
29348 testcase(p->accError==SQLITE_TOOBIG);
29349 testcase(p->accError==SQLITE_NOMEM);
29352 if( p->mxAlloc==0 ){
29354 return p->nAlloc - p->nChar - 1;
29356 char *zOld = isMalloced(p) ? p->zText : 0;
29357 i64 szNew = p->nChar;
29359 if( szNew+p->nChar<=p->mxAlloc ){
29362 szNew += p->nChar;
29364 if( szNew > p->mxAlloc ){
29369 p->nAlloc = (int)szNew;
29371 if( p->db ){
29372 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
29374 zNew = sqlite3Realloc(zOld, p->nAlloc);
29377 assert( p->zText!=0 || p->nChar==0 );
29378 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
29379 p->zText = zNew;
29380 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
29381 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29395 testcase( p->nChar + (i64)N > 0x7fffffff );
29396 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
29399 while( (N--)>0 ) p->zText[p->nChar++] = c;
29406 ** This is a helper routine to sqlite3_str_append() that does special-case
29413 memcpy(&p->zText[p->nChar], z, N);
29414 p->nChar += N;
29424 assert( p->zText!=0 || p->nChar==0 || p->accError );
29426 assert( p->accError==0 || p->nAlloc==0 || p->mxAlloc==0 );
29427 if( p->nChar+N >= p->nAlloc ){
29430 assert( p->zText );
29431 p->nChar += N;
29432 memcpy(&p->zText[p->nChar-N], z, N);
29437 ** Append the complete text of zero-terminated string z[] to the p string.
29445 ** Finish off a string by making sure it is zero-terminated.
29451 assert( p->mxAlloc>0 && !isMalloced(p) );
29452 zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
29454 memcpy(zText, p->zText, p->nChar+1);
29455 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
29459 p->zText = zText;
29463 if( p->zText ){
29464 p->zText[p->nChar] = 0;
29465 if( p->mxAlloc>0 && !isMalloced(p) ){
29469 return p->zText;
29497 return p ? p->accError : SQLITE_NOMEM;
29502 return p ? p->nChar : 0;
29507 if( p==0 || p->nChar==0 ) return 0;
29508 p->zText[p->nChar] = 0;
29509 return p->zText;
29517 sqlite3DbFree(p->db, p->zText);
29518 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
29520 p->nAlloc = 0;
29521 p->nChar = 0;
29522 p->zText = 0;
29530 ** memory is used if not NULL. db->mallocFailed is set appropriately
29540 p->zText = zBase;
29541 p->db = db;
29542 p->nAlloc = n;
29543 p->mxAlloc = mx;
29544 p->nChar = 0;
29545 p->accError = 0;
29546 p->printfFlags = 0;
29554 db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH);
29563 ** %-conversion extensions.
29571 db->aLimit[SQLITE_LIMIT_LENGTH]);
29583 ** %-conversion extensions.
29596 ** %-conversion extensions.
29620 ** %-conversion extensions.
29672 ** This is the routine that actually formats the sqlite3_log() message.
29674 ** stack space on small-stack systems when logging is disabled.
29687 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
29696 ** Format and write a message to the log if logging is enabled.
29736 ** variable-argument wrapper around sqlite3_str_vappendf(). The bFlags argument
29749 ** 2015-06-08
29780 p->iLevel++;
29783 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
29792 p->iLevel--;
29793 if( p->iLevel<0 ) sqlite3_free(p);
29807 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
29808 sqlite3_str_append(&acc, p->bLine[i] ? "| " : " ", 4);
29810 sqlite3_str_append(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
29833 ** Generate a human-readable description of a WITH clause.
29838 if( pWith->nCte==0 ) return;
29839 if( pWith->pOuter ){
29840 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter);
29844 if( pWith->nCte>0 ){
29846 for(i=0; i<pWith->nCte; i++){
29849 const struct Cte *pCte = &pWith->a[i];
29851 sqlite3_str_appendf(&x, "%s", pCte->zName);
29852 if( pCte->pCols && pCte->pCols->nExpr>0 ){
29855 for(j=0; j<pCte->pCols->nExpr; j++){
29856 sqlite3_str_appendf(&x, "%c%s", cSep, pCte->pCols->a[j].zEName);
29861 if( pCte->pUse ){
29862 sqlite3_str_appendf(&x, " (pUse=0x%p, nUse=%d)", pCte->pUse,
29863 pCte->pUse->nUse);
29866 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
29867 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
29875 ** Generate a human-readable description of a SrcList object.
29879 for(i=0; i<pSrc->nSrc; i++){
29880 const SrcItem *pItem = &pSrc->a[i];
29884 sqlite3_str_appendf(&x, "{%d:*}", pItem->iCursor);
29885 if( pItem->zDatabase ){
29886 sqlite3_str_appendf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
29887 }else if( pItem->zName ){
29888 sqlite3_str_appendf(&x, " %s", pItem->zName);
29890 if( pItem->pTab ){
29892 pItem->pTab->zName, pItem->pTab->nCol, pItem->pTab, pItem->colUsed);
29894 if( pItem->zAlias ){
29895 sqlite3_str_appendf(&x, " (AS %s)", pItem->zAlias);
29897 if( pItem->fg.jointype & JT_LEFT ){
29898 sqlite3_str_appendf(&x, " LEFT-JOIN");
29900 if( pItem->fg.fromDDL ){
29903 if( pItem->fg.isCte ){
29904 sqlite3_str_appendf(&x, " CteUse=0x%p", pItem->u2.pCteUse);
29907 sqlite3TreeViewItem(pView, zLine, i<pSrc->nSrc-1);
29908 if( pItem->pSelect ){
29909 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
29911 if( pItem->fg.isTabFunc ){
29912 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:");
29919 ** Generate a human-readable description of a Select object.
29925 sqlite3TreeViewLine(pView, "nil-SELECT");
29929 if( p->pWith ){
29930 sqlite3TreeViewWith(pView, p->pWith, 1);
29935 if( p->selFlags & SF_WhereBegin ){
29940 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
29941 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
29942 p->selId, p, p->selFlags,
29943 (int)p->nSelectRow
29947 if( p->pPrior ){
29951 if( p->pSrc && p->pSrc->nSrc ) n++;
29952 if( p->pWhere ) n++;
29953 if( p->pGroupBy ) n++;
29954 if( p->pHaving ) n++;
29955 if( p->pOrderBy ) n++;
29956 if( p->pLimit ) n++;
29958 if( p->pWin ) n++;
29959 if( p->pWinDefn ) n++;
29962 if( p->pEList ){
29963 sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
29965 n--;
29967 if( p->pWin ){
29969 pView = sqlite3TreeViewPush(pView, (n--)>0);
29970 sqlite3TreeViewLine(pView, "window-functions");
29971 for(pX=p->pWin; pX; pX=pX->pNextWin){
29972 sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
29977 if( p->pSrc && p->pSrc->nSrc ){
29978 pView = sqlite3TreeViewPush(pView, (n--)>0);
29980 sqlite3TreeViewSrcList(pView, p->pSrc);
29983 if( p->pWhere ){
29984 sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
29985 sqlite3TreeViewExpr(pView, p->pWhere, 0);
29988 if( p->pGroupBy ){
29989 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
29991 if( p->pHaving ){
29992 sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
29993 sqlite3TreeViewExpr(pView, p->pHaving, 0);
29997 if( p->pWinDefn ){
29999 sqlite3TreeViewItem(pView, "WINDOW", (n--)>0);
30000 for(pX=p->pWinDefn; pX; pX=pX->pNextWin){
30001 sqlite3TreeViewWindow(pView, pX, pX->pNextWin!=0);
30006 if( p->pOrderBy ){
30007 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
30009 if( p->pLimit ){
30010 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
30011 sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
30012 if( p->pLimit->pRight ){
30013 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
30014 sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
30019 if( p->pPrior ){
30021 switch( p->op ){
30028 p = p->pPrior;
30072 ** Generate a human-readable explanation for a Window object
30076 if( pWin->pFilter ){
30078 sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
30082 if( pWin->zName ){
30083 sqlite3TreeViewLine(pView, "OVER %s (%p)", pWin->zName, pWin);
30087 if( pWin->zBase ) nElement++;
30088 if( pWin->pOrderBy ) nElement++;
30089 if( pWin->eFrmType ) nElement++;
30090 if( pWin->eExclude ) nElement++;
30091 if( pWin->zBase ){
30092 sqlite3TreeViewPush(pView, (--nElement)>0);
30093 sqlite3TreeViewLine(pView, "window: %s", pWin->zBase);
30096 if( pWin->pPartition ){
30097 sqlite3TreeViewExprList(pView, pWin->pPartition, nElement>0,"PARTITION-BY");
30099 if( pWin->pOrderBy ){
30100 sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
30102 if( pWin->eFrmType ){
30105 if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
30106 if( pWin->eFrmType==TK_GROUPS ) zFrmType = "GROUPS";
30108 pWin->bImplicitFrame ? " (implied)" : "");
30109 sqlite3TreeViewItem(pView, zBuf, (--nElement)>0);
30110 sqlite3TreeViewBound(pView, pWin->eStart, pWin->pStart, 1);
30111 sqlite3TreeViewBound(pView, pWin->eEnd, pWin->pEnd, 0);
30114 if( pWin->eExclude ){
30117 switch( pWin->eExclude ){
30123 sqlite3_snprintf(sizeof(zBuf),zBuf,"invalid(%d)", pWin->eExclude);
30137 ** Generate a human-readable explanation for a Window Function object
30142 pWin->pFunc->zName, pWin->pFunc->nArg);
30149 ** Generate a human-readable explanation of an expression tree.
30161 if( pExpr->flags || pExpr->affExpr || pExpr->vvaFlags ){
30165 pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
30167 sqlite3_str_appendf(&x, " iRJT=%d", pExpr->iRightJoinTable);
30179 switch( pExpr->op ){
30182 pExpr->iTable, pExpr->iColumn, zFlgs);
30186 if( pExpr->iTable<0 ){
30189 if( pExpr->op2 ){
30190 sqlite3_snprintf(sizeof(zOp2),zOp2," op2=0x%02x",pExpr->op2);
30195 pExpr->iColumn, zFlgs, zOp2);
30198 pExpr->iTable, pExpr->iColumn,
30199 pExpr->y.pTab, zFlgs);
30202 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30207 if( pExpr->flags & EP_IntValue ){
30208 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
30210 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
30216 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
30221 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
30235 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
30241 pExpr->u.zToken, pExpr->iColumn);
30245 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
30249 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
30255 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
30256 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30293 "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
30295 assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
30296 assert( pExpr->pRight );
30297 assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
30298 x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
30304 sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
30305 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30312 ** up in the treeview output as "SOFT-COLLATE". Explicit COLLATE
30316 !ExprHasProperty(pExpr, EP_Collate) ? "SOFT-" : "",
30317 pExpr->u.zToken, zFlgs);
30318 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30330 pFarg = pExpr->x.pList;
30332 pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
30337 if( pExpr->op==TK_AGG_FUNCTION ){
30339 pExpr->op2, pExpr->u.zToken, zFlgs,
30340 pExpr->pAggInfo ? pExpr->pAggInfo->selId : 0,
30341 pExpr->iAgg, pExpr->pAggInfo);
30342 }else if( pExpr->op2!=0 ){
30345 sqlite3_snprintf(sizeof(zBuf),zBuf,"0x%02x",pExpr->op2);
30347 if( pExpr->op2==NC_IsCheck ) zOp2 = "NC_IsCheck";
30348 if( pExpr->op2==NC_IdxExpr ) zOp2 = "NC_IdxExpr";
30349 if( pExpr->op2==NC_PartIdx ) zOp2 = "NC_PartIdx";
30350 if( pExpr->op2==NC_GenCol ) zOp2 = "NC_GenCol";
30352 pExpr->u.zToken, zFlgs, zOp2);
30354 sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
30368 sqlite3TreeViewLine(pView, "EXISTS-expr flags=0x%x", pExpr->flags);
30369 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30373 sqlite3TreeViewLine(pView, "subquery-expr flags=0x%x", pExpr->flags);
30374 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30378 sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
30379 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30381 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
30383 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30396 ** X is stored in pExpr->pLeft.
30397 ** Y is stored in pExpr->pList->a[0].pExpr.
30398 ** Z is stored in pExpr->pList->a[1].pExpr.
30401 Expr *pX = pExpr->pLeft;
30402 Expr *pY = pExpr->x.pList->a[0].pExpr;
30403 Expr *pZ = pExpr->x.pList->a[1].pExpr;
30412 ** to a column in the new.* or old.* pseudo-tables available to
30414 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
30415 ** is set to the column of the pseudo-table to read, or to -1 to
30419 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
30424 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30425 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
30431 switch( pExpr->affExpr ){
30437 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
30443 pExpr->iTable, pExpr->iColumn, zFlgs);
30444 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30449 sqlite3TreeViewBareExprList(pView, pExpr->x.pList, z);
30454 sqlite3TreeViewLine(pView, "SELECT-COLUMN %d", pExpr->iColumn);
30455 sqlite3TreeViewSelect(pView, pExpr->pLeft->x.pSelect, 0);
30459 sqlite3TreeViewLine(pView, "IF-NULL-ROW %d", pExpr->iTable);
30460 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30464 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
30470 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
30471 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
30474 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
30481 ** Generate a human-readable explanation of an expression list.
30494 for(i=0; i<pList->nExpr; i++){
30495 int j = pList->a[i].u.x.iOrderByCol;
30496 char *zName = pList->a[i].zEName;
30497 int moreToFollow = i<pList->nExpr - 1;
30498 if( pList->a[i].eEName!=ENAME_NAME ) zName = 0;
30512 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, moreToFollow);
30545 ** This file contains code to implement a pseudo-random number
30570 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
30572 ** we have to locate the state vector at run-time. In the more common
30608 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
30638 }while( --N );
30683 ** This file presents a simple cross-platform threading interface for
30696 ** single-threaded if desired.
30708 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
30731 /* This routine is never used in single-threaded mode */
30738 p->xTask = xTask;
30739 p->pIn = pIn;
30747 rc = pthread_create(&p->tid, 0, xTask, pIn);
30750 p->done = 1;
30751 p->pOut = xTask(pIn);
30763 if( p->done ){
30764 *ppOut = p->pOut;
30767 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK;
30780 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */
30806 assert( p->id==GetCurrentThreadId() );
30808 assert( p->xTask!=0 );
30809 p->pResult = p->xTask(p->pIn);
30836 p->xTask = xTask;
30837 p->pIn = pIn;
30838 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id);
30839 if( p->tid==0 ){
30843 if( p->xTask==0 ){
30844 p->id = GetCurrentThreadId();
30845 p->pResult = xTask(pIn);
30860 if( p->xTask==0 ){
30861 /* assert( p->id==GetCurrentThreadId() ); */
30863 assert( p->tid==0 );
30865 assert( p->id!=0 && p->id!=GetCurrentThreadId() );
30866 rc = sqlite3Win32Wait((HANDLE)p->tid);
30868 bRc = CloseHandle((HANDLE)p->tid);
30871 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult;
30880 /********************************* Single-Threaded **************************/
30909 p->xTask = xTask;
30910 p->pIn = pIn;
30912 p->xTask = 0;
30913 p->pResult = xTask(pIn);
30924 if( p->xTask ){
30925 *ppOut = p->xTask(p->pIn);
30927 *ppOut = p->pResult;
30943 /****************************** End Single-Threaded *************************/
30959 ** This file contains routines used to translate between UTF-8,
30960 ** UTF-16, UTF-16BE, and UTF-16LE.
30962 ** Notes on UTF-8:
30964 ** Byte-0 Byte-1 Byte-2 Byte-3 Value
30971 ** Notes on UTF-16: (with wwww+1==uuuuu)
30973 ** Word-0 Word-1 Value
30979 ** 0xff 0xfe little-endian utf-16 follows
30980 ** 0xfe 0xff big-endian utf-16 follows
30997 ** a multi-byte UTF8 character.
31036 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
31037 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
31048 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \
31049 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
31056 ** Translate a single UTF-8 character. Return the unicode value.
31063 ** Notes On Invalid UTF-8:
31065 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to
31066 ** be encoded as a multi-byte character. Any multi-byte character that
31070 ** If a multi-byte character attempts to encode a value between
31074 ** byte of a character are interpreted as single-byte characters
31078 ** * This routine accepts over-length UTF8 encodings
31079 ** for unicode values 0x80 and greater. It does not change over-length
31085 c = sqlite3Utf8Trans1[c-0xc0]; \
31099 ** For this routine, we assume the UTF8 string is always zero-terminated.
31103 c = sqlite3Utf8Trans1[c-0xc0];
31137 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
31138 assert( pMem->flags&MEM_Str );
31139 assert( pMem->enc!=desiredEnc );
31140 assert( pMem->enc!=0 );
31141 assert( pMem->n>=0 );
31153 /* If the translation is between UTF-16 little and big endian, then
31157 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
31165 zIn = (u8*)pMem->z;
31166 zTerm = &zIn[pMem->n&~1];
31173 pMem->enc = desiredEnc;
31179 /* When converting from UTF-16, the maximum growth results from
31180 ** translating a 2-byte character to a 4-byte UTF-8 character.
31182 ** nul-terminator.
31184 pMem->n &= ~1;
31185 len = 2 * (sqlite3_int64)pMem->n + 1;
31187 /* When converting from UTF-8 to UTF-16 the maximum growth is caused
31188 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
31190 ** nul-terminator.
31192 len = 2 * (sqlite3_int64)pMem->n + 2;
31201 zIn = (u8*)pMem->z;
31202 zTerm = &zIn[pMem->n];
31203 zOut = sqlite3DbMallocRaw(pMem->db, len);
31209 if( pMem->enc==SQLITE_UTF8 ){
31211 /* UTF-8 -> UTF-16 Little-endian */
31218 /* UTF-8 -> UTF-16 Big-endian */
31224 pMem->n = (int)(z - zOut);
31228 if( pMem->enc==SQLITE_UTF16LE ){
31229 /* UTF-16 Little-endian -> UTF-8 */
31241 zIn -= 2;
31258 /* UTF-16 Big-endian -> UTF-8 */
31270 zIn -= 2;
31287 pMem->n = (int)(z - zOut);
31290 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
31292 c = MEM_Str|MEM_Term|(pMem->flags&(MEM_AffMask|MEM_Subtype));
31294 pMem->flags = c;
31295 pMem->enc = desiredEnc;
31296 pMem->z = (char*)zOut;
31297 pMem->zMalloc = pMem->z;
31298 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
31316 ** This routine checks for a byte-order mark at the beginning of the
31317 ** UTF-16 string stored in *pMem. If one is present, it is removed and
31319 ** byte-swapping, it just sets Mem.enc appropriately.
31328 assert( pMem->n>=0 );
31329 if( pMem->n>1 ){
31330 u8 b1 = *(u8 *)pMem->z;
31331 u8 b2 = *(((u8 *)pMem->z) + 1);
31343 pMem->n -= 2;
31344 memmove(pMem->z, &pMem->z[2], pMem->n);
31345 pMem->z[pMem->n] = '\0';
31346 pMem->z[pMem->n+1] = '\0';
31347 pMem->flags |= MEM_Term;
31348 pMem->enc = bom;
31356 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
31369 zTerm = (const u8*)(-1);
31379 /* This test function is not currently used by the automated test-suite.
31384 ** Translate UTF-8 to UTF-8.
31386 ** This has the effect of making sure that the string is well-formed
31387 ** UTF-8. Miscoded characters are removed.
31389 ** The translation is done in-place and aborted if the output
31404 return (int)(zOut - zStart);
31410 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
31411 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
31422 if( db->mallocFailed ){
31426 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
31427 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
31428 assert( m.z || db->mallocFailed );
31433 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
31435 ** in pZ. nChar must be non-negative.
31449 return (int)(z-(unsigned char const *)zIn)
31450 - (SQLITE_UTF16NATIVE==SQLITE_UTF16LE);
31469 n = (int)(z-zBuf);
31478 assert( (z-zBuf)==n );
31525 ** sqlite3FaultSim() function only returns non-zero during testing.
31527 ** During testing, if the test harness has set a fault-sim callback using
31529 ** each call to sqlite3FaultSim() is relayed to that application-supplied
31530 ** callback and the integer return value form the application-supplied
31558 ** lower 30 bits of a 32-bit signed integer.
31573 ** The column type is an extra string stored after the zero-terminator on
31577 if( (pCol->colFlags & COLFLAG_HASTYPE)==0 ) return zDflt;
31578 return pCol->zName + strlen(pCol->zName) + 1;
31582 ** Helper function for sqlite3Error() - called rarely. Broken out into
31587 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
31592 ** Set the current error code to err_code and clear any prior error message.
31598 db->errCode = err_code;
31599 if( err_code || db->pErr ) sqlite3ErrorFinish(db, err_code);
31604 ** and error message.
31608 db->errCode = SQLITE_OK;
31609 if( db->pErr ) sqlite3ValueSetNull(db->pErr);
31620 db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
31639 ** encoded in UTF-8.
31647 db->errCode = err_code;
31651 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
31657 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
31662 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
31681 sqlite3 *db = pParse->db;
31685 if( db->suppressErr ){
31688 pParse->nErr++;
31689 sqlite3DbFree(db, pParse->zErrMsg);
31690 pParse->zErrMsg = zMsg;
31691 pParse->rc = SQLITE_ERROR;
31692 pParse->pWith = 0;
31703 if( db==0 || (pParse = db->pParse)==0 ) return errCode;
31704 pParse->rc = errCode;
31705 pParse->nErr++;
31710 ** Convert an SQL-style quoted string into a normal string by removing
31711 ** the quote characters. The conversion is done in-place. If the
31713 ** is a no-op.
31715 ** The input string must be zero-terminated. A new zero-terminator
31718 ** The return value is -1 if no dequoting occurs or the length of the
31722 ** 2002-02-14: This routine is extended to remove MS-Access style
31723 ** brackets from around identifiers. For example: "[a-b-c]" becomes
31724 ** "a-b-c".
31749 assert( sqlite3Isquote(p->u.zToken[0]) );
31750 p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
31751 sqlite3Dequote(p->u.zToken);
31758 p->z = z;
31759 p->n = sqlite3Strlen30(z);
31762 /* Convenient short-hand */
31769 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
31771 ** the contents of two buffers containing UTF-8 strings in a
31772 ** case-independent fashion, using the same definition of "case
31777 return zRight ? -1 : 0;
31794 c = (int)UpperToLower[c] - (int)UpperToLower[x];
31805 return zRight ? -1 : 0;
31811 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
31812 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
31816 ** Compute an 8-bit hash on a string that is insensitive to case differences
31829 ** Compute 10 to the E-th power. Examples: E==1 results in 10.
31872 ** uses the encoding enc. The string is not necessarily zero-terminated.
31880 ** -1 => Not a valid number, but has a valid prefix which
31885 ** [+-]digits[E[+-]digits]
31886 ** [+-]digits.[digits][E[+-]digits]
31887 ** [+-].digits[E[+-]digits]
31909 int eValid = 1; /* True exponent is either not used or is well-formed */
31912 int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
31928 for(i=3-enc; i<length && z[i]==0; i+=2){}
31929 if( i<length ) eType = -100;
31939 if( *z=='-' ){
31940 sign = -1;
31948 s = s*10 + (*z - '0');
31950 if( s>=((LARGEST_INT64-9)/10) ){
31951 /* skip non-significant significand digits
31965 if( s<((LARGEST_INT64-9)/10) ){
31966 s = s*10 + (*z - '0');
31967 d--;
31984 if( z>=zEnd ) goto do_atof_calc; /*PREVENTS-HARMLESS-OVERREAD*/
31987 if( *z=='-' ){
31988 esign = -1;
31995 e = e<10000 ? (e*10 + (*z - '0')) : 10000;
32008 esign = -1;
32009 e *= -1;
32016 result = sign<0 ? -(double)0 : (double)0;
32024 while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
32026 if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
32029 if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
32032 e--;
32036 s = sign<0 ? -s : s;
32038 if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
32042 if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
32043 if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
32044 LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
32077 /* return true if number and no extra non-whitespace chracters after */
32081 return -1;
32094 ** Render an signed 64-bit integer as text. Store the result in zOut[].
32103 x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
32107 i = sizeof(zTemp)-2;
32108 zTemp[sizeof(zTemp)-1] = 0;
32110 zTemp[i--] = (x%10) + '0';
32113 if( v<0 ) zTemp[i--] = '-';
32114 memcpy(zOut, &zTemp[i+1], sizeof(zTemp)-1-i);
32118 ** Compare the 19-character string zNum against the text representation
32129 ** will return -8.
32137 c = (zNum[i*incr]-pow63[i])*10;
32140 c = zNum[18*incr] - '8';
32141 testcase( c==(-1) );
32149 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This
32154 ** -1 Not even a prefix of the input text looks like an integer
32155 ** 0 Successful transformation. Fits in a 64-bit signed integer.
32156 ** 1 Excess non-space text after the integer value
32157 ** 2 Integer too large for a 64-bit signed integer or is malformed
32161 ** The string is not necessarily zero-terminated. The encoding is
32170 int nonNum = 0; /* True if input contains UTF16 with high byte non-zero */
32181 for(i=3-enc; i<length && zNum[i]==0; i+=2){}
32188 if( *zNum=='-' ){
32198 u = u*10 + c - '0';
32205 ** from clang and -fsanitize=undefined. This test and assignment make
32207 ** them, but we must appaise the undefined-behavior pharisees. */
32210 *pNum = -(i64)u;
32216 rc = -1;
32217 }else if( nonNum ){ /* UTF16 with high-order bytes non-zero */
32223 rc = 1; /* Extra non-space text after the integer */
32234 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */
32248 assert( u-1==LARGEST_INT64 );
32256 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
32257 ** into a 64-bit signed integer. This routine accepts hexadecimal literals,
32262 ** 0 Successful transformation. Fits in a 64-bit signed integer.
32264 ** 2 Integer too large for a 64-bit signed integer or is malformed
32279 return (z[k]==0 && k-i<=16) ? 0 : 2;
32288 ** If zNum represents an integer that will fit in 32-bits, then set
32293 ** Any non-numeric characters that following zNum are ignored.
32295 ** input number to be zero-terminated.
32301 if( zNum[0]=='-' ){
32328 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
32335 ** 2^31 -> 2147483648
32341 testcase( v-neg==2147483647 );
32342 if( v-neg>2147483647 ){
32346 v = -v;
32353 ** Return a 32-bit integer value extracted from a string. If the
32363 ** Try to convert z into an unsigned 32-bit integer. Return true on
32372 v = v*10 + z[i] - '0';
32381 ** The variable-length integer encoding is as follows:
32388 ** 7 bits - A
32389 ** 14 bits - BA
32390 ** 21 bits - BBA
32391 ** 28 bits - BBBA
32392 ** 35 bits - BBBBA
32393 ** 42 bits - BBBBBA
32394 ** 49 bits - BBBBBBA
32395 ** 56 bits - BBBBBBBA
32396 ** 64 bits - BBBBBBBBC
32400 ** Write a 64-bit variable-length integer to memory starting at p[0].
32404 ** A variable-length integer consists of the lower 7 bits of each byte
32415 for(i=7; i>=0; i--){
32428 for(i=0, j=n-1; j>=0; j--, i++){
32460 ** Read a 64-bit variable-length integer from memory starting at p[0].
32603 b = p[-4];
32614 ** Read a 32-bit variable-length integer from memory starting at p[0].
32617 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
32621 ** single-byte case. All code should use the MACRO version as
32622 ** this function assumes the single-byte case has already been handled.
32627 /* The 1-byte case. Overwhelmingly the most common. Handled inline
32640 /* The 2-byte case */
32653 /* The 3-byte case */
32668 /* A 32-bit varint is used to store size information in btrees.
32669 ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
32670 ** A 3-byte varint is sufficient, for example, to record the size
32671 ** of a 1048569-byte BLOB or string.
32673 ** We only unroll the first 1-, 2-, and 3- byte cases. The very
32674 ** rare larger cases can be handled by the slower 64-bit varint
32682 n = sqlite3GetVarint(p-2, &v64);
32694 ** unrolling for the 3- and 4-byte varint cases. This code is
32727 ** slow) general-purpose sqlite3GetVarint() routine to extract the
32733 p -= 4;
32744 ** 64-bit integer.
32754 ** Read or write a four-byte big-endian integer value.
32821 n--;
32864 magic = db->magic;
32877 magic = db->magic;
32890 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
32891 ** the other 64-bit signed integer at *pA and store the result in *pA.
32901 testcase( iB==-1 ); testcase( iB==0 );
32903 testcase( iA>0 && LARGEST_INT64 - iA == iB );
32904 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
32905 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
32907 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
32908 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
32909 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
32921 testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
32923 *pA -= iB;
32926 return sqlite3AddInt64(pA, -iB);
32944 if( -iA>LARGEST_INT64/-iB ) return 1;
32953 ** Compute the absolute value of a 32-bit signed integer, of possible. Or
32954 ** if the integer has a value of -2147483648, return +2147483647
32959 return -x;
32964 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
32970 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
32975 ** test.db-journal => test.nal
32976 ** test.db-wal => test.wal
32977 ** test.db-shm => test.shm
32978 ** test.db-mj7f3319fa => test.9fa
32987 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
32988 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
33006 5, 5, 5, /* 12-14 */
33007 4, 4, 4, 4, /* 15-18 */
33008 3, 3, 3, 3, 3, 3, /* 19-24 */
33009 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
33014 return a+x[a-b];
33018 return b+x[b-a];
33031 while( x<8 ){ y -= 10; x <<= 1; }
33034 int i = 60 - __builtin_clzll(x);
33038 while( x>255 ){ y += 40; x >>= 4; } /*OPTIMIZATION-IF-TRUE*/
33042 return a[x&7] + y - 10;
33057 e = (a>>52) - 1022;
33069 ** non-standard compile-time options is enabled.
33075 if( n>=5 ) n -= 2;
33076 else if( n>=1 ) n -= 1;
33085 return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
33093 ** db->mallocFailed flag is set.
33106 ** is always zero-terminated.
33205 ** This is the implementation of generic hash-tables
33218 pNew->first = 0;
33219 pNew->count = 0;
33220 pNew->htsize = 0;
33221 pNew->ht = 0;
33232 elem = pH->first;
33233 pH->first = 0;
33234 sqlite3_free(pH->ht);
33235 pH->ht = 0;
33236 pH->htsize = 0;
33238 HashElem *next_elem = elem->next;
33242 pH->count = 0;
33251 while( (c = (unsigned char)*z++)!=0 ){ /*OPTIMIZATION-IF-TRUE*/
33254 ** (2**32)*golden_ratio, where golden_ratio = (sqrt(5) - 1)/2. */
33272 pHead = pEntry->count ? pEntry->chain : 0;
33273 pEntry->count++;
33274 pEntry->chain = pNew;
33279 pNew->next = pHead;
33280 pNew->prev = pHead->prev;
33281 if( pHead->prev ){ pHead->prev->next = pNew; }
33282 else { pH->first = pNew; }
33283 pHead->prev = pNew;
33285 pNew->next = pH->first;
33286 if( pH->first ){ pH->first->prev = pNew; }
33287 pNew->prev = 0;
33288 pH->first = pNew;
33307 if( new_size==pH->htsize ) return 0;
33323 sqlite3_free(pH->ht);
33324 pH->ht = new_ht;
33325 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
33327 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
33328 unsigned int h = strHash(elem->pKey) % new_size;
33329 next_elem = elem->next;
33350 if( pH->ht ){ /*OPTIMIZATION-IF-TRUE*/
33352 h = strHash(pKey) % pH->htsize;
33353 pEntry = &pH->ht[h];
33354 elem = pEntry->chain;
33355 count = pEntry->count;
33358 elem = pH->first;
33359 count = pH->count;
33362 while( count-- ){
33364 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){
33367 elem = elem->next;
33381 if( elem->prev ){
33382 elem->prev->next = elem->next;
33384 pH->first = elem->next;
33386 if( elem->next ){
33387 elem->next->prev = elem->prev;
33389 if( pH->ht ){
33390 pEntry = &pH->ht[h];
33391 if( pEntry->chain==elem ){
33392 pEntry->chain = elem->next;
33394 assert( pEntry->count>0 );
33395 pEntry->count--;
33398 pH->count--;
33399 if( pH->count==0 ){
33400 assert( pH->first==0 );
33401 assert( pH->count==0 );
33413 return findElementWithHash(pH, pKey, 0)->data;
33438 if( elem->data ){
33439 void *old_data = elem->data;
33443 elem->data = data;
33444 elem->pKey = pKey;
33451 new_elem->pKey = pKey;
33452 new_elem->data = data;
33453 pH->count++;
33454 if( pH->count>=10 && pH->count > 2*pH->htsize ){
33455 if( rehash(pH, pH->count*2) ){
33456 assert( pH->htsize>0 );
33457 h = strHash(pKey) % pH->htsize;
33460 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem);
33526 /* 48 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
33527 /* 49 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]--, goto P2"),
33537 /* 59 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
33565 /* 87 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
33585 /* 107 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
33597 /* 119 */ "SeekScan" OpHelp("Scan-ahead up to P1 rows"),
33633 /* 155 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
33676 ** This file contains the VFS implementation for unix-like operating systems
33682 ** use flock(), dot-files, various proprietary locking schemas, or simply
33692 ** * General-purpose declarations and utility functions.
33696 ** + for no-op locks
33697 ** + for dot-file locks
33705 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
33718 ** 3. Dot-file locking,
33777 ** -DHAVE_GETHOSTUUID=0
33778 ** -DHAVE_GETHOSTUUID=1
33781 ** -DSQLITE_ENABLE_LOCKING_STYLE.
33819 ** If we are to be thread-safe, include the pthreads header.
33840 ** Maximum supported path-length.
33862 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */
33891 UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */
33918 ** transaction counter in bytes 24-27 of database files are updated
33978 ** all of the platform-specific files (os_*.c) and is #included into those
33990 ** switch. The following code should catch this problem at compile-time.
34004 ** high-performance timing routines.
34020 ** This file contains inline asm code for retrieving "high-performance"
34027 ** The following routine only works on pentium-class (or newer) processors.
34029 ** processor and returns that value. This can be used for high-res
34100 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
34124 || sqlite3_io_error_pending-- == 1 ) \
34139 sqlite3_diskfull_pending--; \
34181 ** The threadid macro resolves to the thread-id or to 0. Used for
34202 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek()
34203 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined.
34211 ** Linux-specific IOCTL magic numbers used for controlling F2FS
34229 ** which always has the same well-defined interface.
34240 ** Many system calls are accessed through pointer-to-functions so that
34269 ** DJGPP. But it is DOS - what did you expect?
34415 ** On some systems, calls to fchown() will trigger a message in a security
34416 ** log if they come from non-root processes. So avoid calling fchown() if
34496 int i = -1;
34500 for(i=0; i<ArraySize(aSyscall)-1; i++){
34528 ** The m parameter will be non-zero only when creating -wal, -journal,
34529 ** and -shm files. We want those files to have *exactly* the same
34531 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
34553 fd = -1;
34618 ** integer lock-type.
34637 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE
34638 ** command-line option on the compiler. This code is normally
34654 if( p->l_type==F_RDLCK ){
34656 }else if( p->l_type==F_WRLCK ){
34658 }else if( p->l_type==F_UNLCK ){
34663 assert( p->l_whence==SEEK_SET );
34667 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
34668 (int)p->l_pid, s);
34669 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
34682 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
34702 /* On Android, ftruncate() always uses 32-bit offsets, even if
34763 ** a performance-critical path, so it is sufficient to put these
34788 ** Changes are made in-place. Return the new name length.
34790 ** The original filename is in z[0..n-1]. Return the number of
34795 while( n>1 && z[n-1]=='/' ){ n--; }
34804 while( j>0 && z[j-1]!='/' ){ j--; }
34805 if( j>0 ){ j--; }
34836 pNew->zCanonicalName = (char*)&pNew[1];
34837 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
34838 n = vxworksSimplifyName(pNew->zCanonicalName, n);
34845 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
34846 if( pCandidate->nName==n
34847 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
34850 pCandidate->nRef++;
34857 pNew->nRef = 1;
34858 pNew->nName = n;
34859 pNew->pNext = vxworksFileList;
34871 assert( pId->nRef>0 );
34872 pId->nRef--;
34873 if( pId->nRef==0 ){
34875 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
34877 *pp = pId->pNext;
34935 ** file is unlocked. cnt==-1 means the file has an exclusive lock.
34963 ** on linux - with NPTL a lock created by thread A can override locks
34964 ** in thread B. But there is no way to know at compile-time which
34966 ** compile-time whether or not thread A can override locks on thread B.
34967 ** One has to do a run-time check to discover the behavior of the
34987 ** sizes ino_t at only 32-bits instead of 64-bits. (See
34988 ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
34989 ** To work around this, always allocate 64-bits for the inode number.
34990 ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
35057 assert( pFile->pInode );
35058 return sqlite3_mutex_held(pFile->pInode->pLockMutex);
35061 assert( pFile->pInode );
35062 return sqlite3_mutex_notheld(pFile->pInode->pLockMutex);
35068 ** This function - unixLogErrorAtLine(), is only ever called via the macro
35072 ** set. It logs a message using sqlite3_log() containing the current value of
35073 ** errno and, if possible, the human-readable equivalent from strerror() or
35079 ** failed (e.g. "unlink", "open") and the associated file-system path,
35089 char *zErr; /* Message from strerror() or equivalent */
35093 ** the strerror() function to obtain the human-readable error message
35103 ** returns a pointer to a buffer containing the error message. That pointer
35106 ** strerror_r(), which always writes an error message into aErr[].
35109 ** available, the error message will often be an empty string. Not a
35116 strerror_r(iErrno, aErr, sizeof(aErr)-1);
35122 /* Non-threadsafe build, use strerror(). */
35128 "os_unix.c:%d: (%d) %s(%s) - %s",
35151 pFile ? pFile->zPath : 0, lineno);
35156 ** Set the pFile->lastErrno. Do this in a subroutine as that provides
35160 pFile->lastErrno = error;
35164 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
35167 unixInodeInfo *pInode = pFile->pInode;
35171 for(p=pInode->pUnused; p; p=pNext){
35172 pNext = p->pNext;
35173 robust_close(pFile, p->fd, __LINE__);
35176 pInode->pUnused = 0;
35186 unixInodeInfo *pInode = pFile->pInode;
35190 pInode->nRef--;
35191 if( pInode->nRef==0 ){
35192 assert( pInode->pShmNode==0 );
35193 sqlite3_mutex_enter(pInode->pLockMutex);
35195 sqlite3_mutex_leave(pInode->pLockMutex);
35196 if( pInode->pPrev ){
35197 assert( pInode->pPrev->pNext==pInode );
35198 pInode->pPrev->pNext = pInode->pNext;
35201 inodeList = pInode->pNext;
35203 if( pInode->pNext ){
35204 assert( pInode->pNext->pPrev==pInode );
35205 pInode->pNext->pPrev = pInode->pPrev;
35207 sqlite3_mutex_free(pInode->pLockMutex);
35229 struct stat statbuf; /* Low-level file information */
35234 /* Get low-level information about the file that we can used to
35237 fd = pFile->h;
35242 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
35249 ** incorrectly for zero-size files. See ticket #3260. To work
35258 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
35275 fileId.pId = pFile->pId;
35281 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
35282 pInode = pInode->pNext;
35290 memcpy(&pInode->fileId, &fileId, sizeof(fileId));
35292 pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
35293 if( pInode->pLockMutex==0 ){
35298 pInode->nRef = 1;
35300 pInode->pNext = inodeList;
35301 pInode->pPrev = 0;
35302 if( inodeList ) inodeList->pPrev = pInode;
35305 pInode->nRef++;
35316 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
35319 return pFile->pInode!=0 &&
35320 (osStat(pFile->zPath, &buf)!=0
35321 || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
35340 if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return;
35342 rc = osFstat(pFile->h, &buf);
35344 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
35348 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
35352 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
35356 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
35365 ** to a non-zero value otherwise *pResOut is set to zero. The return value
35376 assert( pFile->eFileLock<=SHARED_LOCK );
35377 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
35380 if( pFile->pInode->eFileLock>SHARED_LOCK ){
35387 if( !reserved && !pFile->pInode->bProcessLock ){
35393 if( osFcntl(pFile->h, F_GETLK, &lock) ){
35402 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
35403 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
35413 ** Set a posix-advisory-lock.
35417 ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout
35422 ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
35433 int tm = pFile->iBusyTimeout;
35443 tm--;
35451 ** Attempt to set a system-lock on the file pFile. The lock is
35454 ** If the pFile was opened read/write from unix-excl, then the only lock
35457 ** operations become no-ops. Locking operations still happen internally,
35462 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
35463 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
35464 ** and is read-only.
35466 ** Zero is returned if the call completes successfully, or -1 if a call
35471 unixInodeInfo *pInode = pFile->pInode;
35473 assert( sqlite3_mutex_held(pInode->pLockMutex) );
35474 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){
35475 if( pInode->bProcessLock==0 ){
35477 assert( pInode->nLock==0 );
35482 rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
35484 pInode->bProcessLock = 1;
35485 pInode->nLock++;
35490 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
35496 ** Lock the file with the lock specified by parameter eFileLock - one
35510 ** UNLOCKED -> SHARED
35511 ** SHARED -> RESERVED
35512 ** SHARED -> (PENDING) -> EXCLUSIVE
35513 ** RESERVED -> (PENDING) -> EXCLUSIVE
35514 ** PENDING -> EXCLUSIVE
35522 ** lock primitives (called read-locks and write-locks below, to avoid
35531 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
35532 ** byte'. If this is successful, 'shared byte range' is read-locked
35535 ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
35537 ** Windows95 is now pretty much extinct, but this work-around for the
35538 ** lack of shared-locks on Windows95 lives on, for backwards
35542 ** A RESERVED lock is implemented by grabbing a write-lock on the
35546 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
35554 ** implemented by obtaining a write-lock on the entire 'shared byte
35555 ** range'. Since all other locks require a read-lock on one of the bytes
35566 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
35567 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
35568 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared,
35575 if( pFile->eFileLock>=eFileLock ){
35576 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h,
35586 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
35588 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
35590 /* This mutex is needed because pFile->pInode is shared across threads
35592 pInode = pFile->pInode;
35593 sqlite3_mutex_enter(pInode->pLockMutex);
35598 if( (pFile->eFileLock!=pInode->eFileLock &&
35599 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
35610 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
35612 assert( pFile->eFileLock==0 );
35613 assert( pInode->nShared>0 );
35614 pFile->eFileLock = SHARED_LOCK;
35615 pInode->nShared++;
35616 pInode->nLock++;
35628 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
35647 assert( pInode->nShared==0 );
35648 assert( pInode->eFileLock==0 );
35651 /* Now get the read-lock */
35675 pFile->eFileLock = SHARED_LOCK;
35676 pInode->nLock++;
35677 pInode->nShared = 1;
35679 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
35688 assert( 0!=pFile->eFileLock );
35711 /* Set up the transaction-counter change checking flags when
35717 && pFile->eFileLock<=SHARED_LOCK
35720 pFile->transCntrChng = 0;
35721 pFile->dbUpdate = 0;
35722 pFile->inNormalWrite = 1;
35728 pFile->eFileLock = eFileLock;
35729 pInode->eFileLock = eFileLock;
35731 pFile->eFileLock = PENDING_LOCK;
35732 pInode->eFileLock = PENDING_LOCK;
35736 sqlite3_mutex_leave(pInode->pLockMutex);
35737 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
35747 unixInodeInfo *pInode = pFile->pInode;
35748 UnixUnusedFd *p = pFile->pPreallocatedUnused;
35750 p->pNext = pInode->pUnused;
35751 pInode->pUnused = p;
35752 pFile->h = -1;
35753 pFile->pPreallocatedUnused = 0;
35761 ** the requested locking level, this routine is a no-op.
35776 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
35777 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
35781 if( pFile->eFileLock<=eFileLock ){
35784 pInode = pFile->pInode;
35785 sqlite3_mutex_enter(pInode->pLockMutex);
35786 assert( pInode->nShared!=0 );
35787 if( pFile->eFileLock>SHARED_LOCK ){
35788 assert( pInode->eFileLock==pFile->eFileLock );
35797 ** cache. The use of a stale cache can lead to database corruption.
35799 pFile->inNormalWrite = 0;
35803 ** before establishing the readlock - to avoid a race condition we downgrade
35819 off_t divSize = SHARED_SIZE - 1;
35825 if( unixFileLock(pFile, &lock)==(-1) ){
35835 if( unixFileLock(pFile, &lock)==(-1) ){
35846 lock.l_len = SHARED_SIZE-divSize;
35847 if( unixFileLock(pFile, &lock)==(-1) ){
35878 pInode->eFileLock = SHARED_LOCK;
35890 pInode->nShared--;
35891 if( pInode->nShared==0 ){
35896 pInode->eFileLock = NO_LOCK;
35900 pInode->eFileLock = NO_LOCK;
35901 pFile->eFileLock = NO_LOCK;
35909 pInode->nLock--;
35910 assert( pInode->nLock>=0 );
35911 if( pInode->nLock==0 ) closePendingFds(pFile);
35915 sqlite3_mutex_leave(pInode->pLockMutex);
35917 pFile->eFileLock = eFileLock;
35927 ** the requested locking level, this routine is a no-op.
35931 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
35956 if( pFile->h>=0 ){
35957 robust_close(pFile, pFile->h, __LINE__);
35958 pFile->h = -1;
35961 if( pFile->pId ){
35962 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35963 osUnlink(pFile->pId->zCanonicalName);
35965 vxworksReleaseFileId(pFile->pId);
35966 pFile->pId = 0;
35970 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
35971 osUnlink(pFile->zPath);
35972 sqlite3_free(*(char**)&pFile->zPath);
35973 pFile->zPath = 0;
35976 OSTRACE(("CLOSE %-3d\n", pFile->h));
35977 OpenCounter(-1);
35978 sqlite3_free(pFile->pPreallocatedUnused);
35989 unixInodeInfo *pInode = pFile->pInode;
36000 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
36001 sqlite3_mutex_enter(pInode->pLockMutex);
36002 if( pInode->nLock ){
36005 ** descriptor to pInode->pUnused list. It will be automatically closed
36010 sqlite3_mutex_leave(pInode->pLockMutex);
36012 assert( pFile->pShm==0 );
36022 ****************************** No-op Locking **********************************
36028 ** This locking mode is appropriate for use on read-only databases
36029 ** (ex: databases that are burned into CD-ROM, for example.) It can
36059 /******************* End of the no-op lock implementation *********************
36063 ************************* Begin dot-file Locking ******************************
36072 ** (2) An application crash or power loss can leave stale lock files
36093 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36108 reserved = osAccess((const char*)pFile->lockingContext, 0)==0;
36109 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
36115 ** Lock the file with the lock specified by parameter eFileLock - one
36129 ** UNLOCKED -> SHARED
36130 ** SHARED -> RESERVED
36131 ** SHARED -> (PENDING) -> EXCLUSIVE
36132 ** RESERVED -> (PENDING) -> EXCLUSIVE
36133 ** PENDING -> EXCLUSIVE
36143 char *zLockFile = (char *)pFile->lockingContext;
36150 if( pFile->eFileLock > NO_LOCK ){
36151 pFile->eFileLock = eFileLock;
36178 pFile->eFileLock = eFileLock;
36187 ** the requested locking level, this routine is a no-op.
36193 char *zLockFile = (char *)pFile->lockingContext;
36197 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
36198 pFile->eFileLock, osGetpid(0)));
36201 /* no-op if possible */
36202 if( pFile->eFileLock==eFileLock ){
36210 pFile->eFileLock = SHARED_LOCK;
36227 pFile->eFileLock = NO_LOCK;
36238 sqlite3_free(pFile->lockingContext);
36241 /****************** End of the dot-file lock implementation *******************
36249 ** flock() locking is like dot-file locking in that the various
36250 ** fine-grain locking levels supported by SQLite are collapsed into
36277 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36290 if( pFile->eFileLock>SHARED_LOCK ){
36297 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
36300 lrc = robust_flock(pFile->h, LOCK_UN);
36319 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
36332 ** Lock the file with the lock specified by parameter eFileLock - one
36346 ** UNLOCKED -> SHARED
36347 ** SHARED -> RESERVED
36348 ** SHARED -> (PENDING) -> EXCLUSIVE
36349 ** RESERVED -> (PENDING) -> EXCLUSIVE
36350 ** PENDING -> EXCLUSIVE
36368 if (pFile->eFileLock > NO_LOCK) {
36369 pFile->eFileLock = eFileLock;
36375 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
36384 pFile->eFileLock = eFileLock;
36386 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
36402 ** the requested locking level, this routine is a no-op.
36408 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
36409 pFile->eFileLock, osGetpid(0)));
36412 /* no-op if possible */
36413 if( pFile->eFileLock==eFileLock ){
36419 pFile->eFileLock = eFileLock;
36424 if( robust_flock(pFile->h, LOCK_UN) ){
36430 pFile->eFileLock = NO_LOCK;
36454 ** Semaphore locking is like dot-lock and flock in that it really only
36464 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36477 if( pFile->eFileLock>SHARED_LOCK ){
36483 sem_t *pSem = pFile->pInode->pSem;
36485 if( sem_trywait(pSem)==-1 ){
36492 reserved = (pFile->eFileLock < SHARED_LOCK);
36499 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
36506 ** Lock the file with the lock specified by parameter eFileLock - one
36520 ** UNLOCKED -> SHARED
36521 ** SHARED -> RESERVED
36522 ** SHARED -> (PENDING) -> EXCLUSIVE
36523 ** RESERVED -> (PENDING) -> EXCLUSIVE
36524 ** PENDING -> EXCLUSIVE
36536 sem_t *pSem = pFile->pInode->pSem;
36541 if (pFile->eFileLock > NO_LOCK) {
36542 pFile->eFileLock = eFileLock;
36548 if( sem_trywait(pSem)==-1 ){
36554 pFile->eFileLock = eFileLock;
36565 ** the requested locking level, this routine is a no-op.
36569 sem_t *pSem = pFile->pInode->pSem;
36573 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
36574 pFile->eFileLock, osGetpid(0)));
36577 /* no-op if possible */
36578 if( pFile->eFileLock==eFileLock ){
36584 pFile->eFileLock = eFileLock;
36589 if ( sem_post(pSem)==-1 ) {
36597 pFile->eFileLock = NO_LOCK;
36630 ** on Apple Macintosh computers - both OS9 and OSX.
36632 ** Third-party implementations of AFP are available. But this code here
36659 ** This is a utility for setting or clearing a bit-range lock on an
36678 pb.fd = pFile->h;
36681 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
36684 if ( err==-1 ) {
36707 ** to a non-zero value otherwise *pResOut is set to zero. The return value
36719 context = (afpLockingContext *) pFile->lockingContext;
36720 if( context->reserved ){
36724 sqlite3_mutex_enter(pFile->pInode->pLockMutex);
36726 if( pFile->pInode->eFileLock>SHARED_LOCK ){
36734 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36738 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
36748 sqlite3_mutex_leave(pFile->pInode->pLockMutex);
36749 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
36756 ** Lock the file with the lock specified by parameter eFileLock - one
36770 ** UNLOCKED -> SHARED
36771 ** SHARED -> RESERVED
36772 ** SHARED -> (PENDING) -> EXCLUSIVE
36773 ** RESERVED -> (PENDING) -> EXCLUSIVE
36774 ** PENDING -> EXCLUSIVE
36782 unixInodeInfo *pInode = pFile->pInode;
36783 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36786 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
36787 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
36788 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0)));
36794 if( pFile->eFileLock>=eFileLock ){
36795 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h,
36805 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
36807 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
36809 /* This mutex is needed because pFile->pInode is shared across threads
36811 pInode = pFile->pInode;
36812 sqlite3_mutex_enter(pInode->pLockMutex);
36817 if( (pFile->eFileLock!=pInode->eFileLock &&
36818 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
36829 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
36831 assert( pFile->eFileLock==0 );
36832 assert( pInode->nShared>0 );
36833 pFile->eFileLock = SHARED_LOCK;
36834 pInode->nShared++;
36835 pInode->nLock++;
36844 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
36847 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
36861 assert( pInode->nShared==0 );
36862 assert( pInode->eFileLock==0 );
36865 /* Now get the read-lock SHARED_LOCK */
36868 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
36869 lrc1 = afpSetLock(context->dbPath, pFile,
36870 SHARED_FIRST+pInode->sharedByte, 1, 1);
36872 lrc1Errno = pFile->lastErrno;
36875 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
36887 pFile->eFileLock = SHARED_LOCK;
36888 pInode->nLock++;
36889 pInode->nShared = 1;
36891 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
36901 assert( 0!=pFile->eFileLock );
36902 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
36904 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
36906 context->reserved = 1;
36915 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
36916 pInode->sharedByte, 1, 0)) ){
36919 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
36921 if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
36922 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
36940 pFile->eFileLock = eFileLock;
36941 pInode->eFileLock = eFileLock;
36943 pFile->eFileLock = PENDING_LOCK;
36944 pInode->eFileLock = PENDING_LOCK;
36948 sqlite3_mutex_leave(pInode->pLockMutex);
36949 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
36959 ** the requested locking level, this routine is a no-op.
36965 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
36968 int h = pFile->h;
36972 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
36973 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
36977 if( pFile->eFileLock<=eFileLock ){
36980 pInode = pFile->pInode;
36981 sqlite3_mutex_enter(pInode->pLockMutex);
36982 assert( pInode->nShared!=0 );
36983 if( pFile->eFileLock>SHARED_LOCK ){
36984 assert( pInode->eFileLock==pFile->eFileLock );
36986 SimulateIOError( h=(-1) )
36996 ** cache. The use of a stale cache can lead to database corruption.
36998 assert( pFile->inNormalWrite==0
36999 || pFile->dbUpdate==0
37000 || pFile->transCntrChng==1 );
37001 pFile->inNormalWrite = 0;
37004 if( pFile->eFileLock==EXCLUSIVE_LOCK ){
37005 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
37006 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
37007 /* only re-establish the shared lock if necessary */
37008 int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
37009 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
37014 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
37015 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
37017 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
37018 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
37020 context->reserved = 0;
37023 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
37024 pInode->eFileLock = SHARED_LOCK;
37033 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
37034 pInode->nShared--;
37035 if( pInode->nShared==0 ){
37037 SimulateIOError( h=(-1) )
37040 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
37043 pInode->eFileLock = NO_LOCK;
37044 pFile->eFileLock = NO_LOCK;
37048 pInode->nLock--;
37049 assert( pInode->nLock>=0 );
37050 if( pInode->nLock==0 ) closePendingFds(pFile);
37054 sqlite3_mutex_leave(pInode->pLockMutex);
37056 pFile->eFileLock = eFileLock;
37071 if( pFile->pInode ){
37072 unixInodeInfo *pInode = pFile->pInode;
37073 sqlite3_mutex_enter(pInode->pLockMutex);
37074 if( pInode->nLock ){
37077 ** descriptor to pInode->aPending. It will be automatically closed when
37082 sqlite3_mutex_leave(pInode->pLockMutex);
37085 sqlite3_free(pFile->lockingContext);
37095 ** is available. If you don't compile for a mac, then the "unix-afp"
37110 ** the requested locking level, this routine is a no-op.
37126 **************** Non-locking sqlite3_file methods *****************************
37156 assert( id->h>2 );
37159 got = osPread(id->h, pBuf, cnt, offset);
37160 SimulateIOError( got = -1 );
37162 got = osPread64(id->h, pBuf, cnt, offset);
37163 SimulateIOError( got = -1 );
37165 newOffset = lseek(id->h, offset, SEEK_SET);
37166 SimulateIOError( newOffset = -1 );
37169 return -1;
37171 got = osRead(id->h, pBuf, cnt);
37180 cnt -= got;
37187 OSTRACE(("READ %-3d %5d %7lld %llu\n",
37188 id->h, got+prior, offset-prior, TIMER_ELAPSED));
37209 /* If this is a database file (not a journal, super-journal or temp
37212 assert( pFile->pPreallocatedUnused==0
37221 if( offset<pFile->mmapSize ){
37222 if( offset+amt <= pFile->mmapSize ){
37223 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
37226 int nCopy = pFile->mmapSize - offset;
37227 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
37229 amt -= nCopy;
37239 /* pFile->lastErrno has been set by seekAndRead().
37246 switch( pFile->lastErrno ){
37260 /* Unread parts of the buffer must be zero-filled */
37261 memset(&((char*)pBuf)[got], 0, amt-got);
37267 ** Attempt to seek the file-descriptor passed as the first argument to
37269 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
37295 SimulateIOError( iSeek = -1 );
37297 rc = -1;
37305 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
37313 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
37320 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
37339 /* If this is a database file (not a journal, super-journal or temp
37342 assert( pFile->pPreallocatedUnused==0
37350 ** doing a hot-journal rollback or a write to some file other than a
37355 if( pFile->inNormalWrite ){
37356 pFile->dbUpdate = 1; /* The database has been modified */
37363 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
37364 pFile->transCntrChng = 1; /* The transaction counter has changed */
37373 if( offset<pFile->mmapSize ){
37374 if( offset+amt <= pFile->mmapSize ){
37375 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
37378 int nCopy = pFile->mmapSize - offset;
37379 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
37381 amt -= nCopy;
37388 amt -= wrote;
37392 SimulateIOError(( wrote=(-1), amt=1 ));
37396 if( wrote<0 && pFile->lastErrno!=ENOSPC ){
37421 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC
37491 ** no-op. But go ahead and call fstat() to validate the file
37524 if( rc==-1 && errno==ENOTSUP ){
37530 if( OS_VXWORKS && rc!= -1 ){
37543 ** The directory file descriptor is used for only one thing - to
37552 ** replace this routine with a harmless no-op. To make this routine
37553 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
37561 int fd = -1;
37565 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--);
37574 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
37589 ** has been created by fsync-ing the directory that contains the file.
37594 ** will not roll back - possibly leading to database corruption.
37614 OSTRACE(("SYNC %-3d\n", pFile->h));
37615 rc = full_fsync(pFile->h, isFullsync, isDataOnly);
37619 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
37623 ** is set. This is a one-time occurrence. Many systems (examples: AIX)
37626 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
37628 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
37630 rc = osOpenDirectory(pFile->zPath, &dirfd);
37638 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
37652 /* If the user has configured a chunk-size for this file, truncate the
37657 if( pFile->szChunk>0 ){
37658 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
37661 rc = robust_ftruncate(pFile->h, nByte);
37664 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37668 ** doing a hot-journal rollback or a write to some file other than a
37671 ** when restoring a database using the backup API from a zero-length
37674 if( pFile->inNormalWrite && nByte==0 ){
37675 pFile->transCntrChng = 1;
37684 if( nByte<pFile->mmapSize ){
37685 pFile->mmapSize = nByte;
37700 rc = osFstat(((unixFile*)id)->h, &buf);
37708 /* When opening a zero-size database, the findInodeInfo() procedure
37710 ** in the OS-X msdos filesystem. In order to avoid problems with upper
37722 ** Handler for proxy-locking file-control verbs. Defined below in the
37730 ** file-control operation. Enlarge the database to nBytes in size
37731 ** (rounded up to the next chunk-size). If the database is already
37732 ** nBytes or larger, this routine is a no-op.
37735 if( pFile->szChunk>0 ){
37739 if( osFstat(pFile->h, &buf) ){
37743 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
37752 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
37759 ** at offset (nSize-1), to set the size of the file correctly.
37763 int nBlk = buf.st_blksize; /* File-system block size */
37767 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1;
37770 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){
37771 if( iWrite>=nSize ) iWrite = nSize - 1;
37780 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
37782 if( pFile->szChunk<=0 ){
37783 if( robust_ftruncate(pFile->h, nByte) ){
37785 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
37799 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
37801 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
37805 *pArg = (pFile->ctrlFlags & mask)!=0;
37807 pFile->ctrlFlags &= ~mask;
37809 pFile->ctrlFlags |= mask;
37824 int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE);
37828 int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE);
37832 int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE);
37838 *(int*)pArg = pFile->eFileLock;
37842 *(int*)pArg = pFile->lastErrno;
37846 pFile->szChunk = *(int *)pArg;
37865 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
37869 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname );
37871 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
37882 int iOld = pFile->iBusyTimeout;
37883 pFile->iBusyTimeout = *(int*)pArg;
37898 ** 64-bit type. */
37903 *(i64*)pArg = pFile->mmapSizeMax;
37904 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
37905 pFile->mmapSizeMax = newLimit;
37906 if( pFile->mmapSize>0 ){
37908 rc = unixMapfile(pFile, -1);
37921 ((unixFile*)id)->dbUpdate = 0;
37936 ** If pFd->sectorSize is non-zero when this function is called, it is a
37937 ** no-op. Otherwise, the values of pFd->sectorSize and
37938 ** pFd->deviceCharacteristics are set according to the file-system
37946 assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 );
37947 if( pFd->sectorSize==0 ){
37953 res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f);
37955 pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC;
37960 if( pFd->ctrlFlags & UNIXFILE_PSOW ){
37961 pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
37964 pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37971 if( pFile->sectorSize == 0 ){
37974 /* Set defaults for non-supported filesystems */
37975 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
37976 pFile->deviceCharacteristics = 0;
37977 if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
37982 pFile->sectorSize = fsInfo.f_bsize;
37983 pFile->deviceCharacteristics =
37991 pFile->sectorSize = fsInfo.f_bsize;
37992 pFile->deviceCharacteristics =
37994 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
38001 pFile->sectorSize = fsInfo.f_bsize;
38002 pFile->deviceCharacteristics =
38010 pFile->sectorSize = fsInfo.f_bsize;
38011 pFile->deviceCharacteristics =
38013 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
38018 pFile->sectorSize = fsInfo.f_bsize;
38019 pFile->deviceCharacteristics =
38021 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
38026 pFile->deviceCharacteristics =
38035 if( pFile->sectorSize % 512 != 0 ){
38036 pFile->deviceCharacteristics = 0;
38037 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
38048 ** if two files are created in the same file-system directory (i.e.
38055 return pFd->sectorSize;
38064 ** words, after a power-loss event, parts of the file that were never
38065 ** written might end up being altered.) However, non-PSOW behavior is very,
38068 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
38074 return pFd->deviceCharacteristics;
38102 ** When multiple threads all reference the same wal-index, each thread
38104 ** of this unixShmNode object. In other words, each wal-index is opened
38119 ** The following fields are read-only after the object is created:
38133 int szRegion; /* Size of shared-memory regions */
38135 u8 isReadonly; /* True if read-only */
38137 char **apRegion; /* Array of mapped shared-memory regions */
38140 int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
38153 ** are read-only thereafter:
38158 ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must
38164 u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */
38177 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
38179 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
38188 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */
38193 pShmNode = pFile->pInode->pShmNode;
38194 assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
38195 assert( pShmNode->nRef>0 || unixMutexHeld() );
38203 if( pShmNode->hShm>=0 ){
38210 res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
38211 if( res==-1 ){
38213 rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
38223 OSTRACE(("SHM-LOCK "));
38224 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
38228 pShmNode->exclMask &= ~mask;
38229 pShmNode->sharedMask &= ~mask;
38231 OSTRACE(("read-lock %d ok", ofst));
38232 pShmNode->exclMask &= ~mask;
38233 pShmNode->sharedMask |= mask;
38236 OSTRACE(("write-lock %d ok", ofst));
38237 pShmNode->exclMask |= mask;
38238 pShmNode->sharedMask &= ~mask;
38244 OSTRACE(("read-lock failed"));
38247 OSTRACE(("write-lock %d failed", ofst));
38250 OSTRACE((" - afterwards %03x,%03x\n",
38251 pShmNode->sharedMask, pShmNode->exclMask));
38261 ** current system page-size.
38264 ** to use 64KB pages - in this case each mapping must cover at least two
38270 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */
38278 ** This is not a VFS shared-memory method; it is a utility function called
38279 ** by VFS shared-memory methods.
38282 unixShmNode *p = pFd->pInode->pShmNode;
38284 if( p && ALWAYS(p->nRef==0) ){
38287 assert( p->pInode==pFd->pInode );
38288 sqlite3_mutex_free(p->pShmMutex);
38289 for(i=0; i<p->nRegion; i+=nShmPerMap){
38290 if( p->hShm>=0 ){
38291 osMunmap(p->apRegion[i], p->szRegion);
38293 sqlite3_free(p->apRegion[i]);
38296 sqlite3_free(p->apRegion);
38297 if( p->hShm>=0 ){
38298 robust_close(pFd, p->hShm, __LINE__);
38299 p->hShm = -1;
38301 p->pInode->pShmNode = 0;
38313 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
38322 ** and proceed with opening the *-shm file.
38326 ** DMS byte and truncate the *-shm file to zero bytes in size. Then
38333 ** EXCLUSIVE failed just before truncating the *-shm file, then this
38334 ** process might open and use the *-shm file without truncating it.
38335 ** And if the *-shm file has been corrupted by a power failure or
38341 if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) {
38344 if( pShmNode->isReadonly ){
38345 pShmNode->isUnlocked = 1;
38349 /* The first connection to attach must truncate the -shm file. We
38351 ** -shm header size) rather than 0 as a system debugging aid, to
38352 ** help detect if a -shm file truncation is legitimate or is the work
38354 if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){
38355 rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename);
38370 ** Open a shared-memory area associated with open database file pDbFd.
38373 ** The file used to implement shared-memory is in the same directory
38375 ** file with the "-shm" suffix added. For example, if the database file
38377 ** for shared memory will be called "/home/user1/config.db-shm".
38383 ** database to end up using different files for shared memory -
38384 ** meaning that their memory would not really be shared - resulting
38386 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
38387 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time
38391 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
38394 ** When opening a new shared-memory file, if no other instances of that
38398 ** If the original database file (pDbFd) is using the "unix-excl" VFS
38416 assert( pDbFd->pShm==0 );
38423 pInode = pDbFd->pInode;
38424 pShmNode = pInode->pShmNode;
38428 const char *zBasePath = pDbFd->zPath;
38432 ** a new *-shm file is created, an attempt will be made to create it
38435 if( osFstat(pDbFd->h, &sStat) ){
38451 zShm = pShmNode->zFilename = (char*)&pShmNode[1];
38454 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
38457 sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath);
38458 sqlite3FileSuffix3(pDbFd->zPath, zShm);
38460 pShmNode->hShm = -1;
38461 pDbFd->pInode->pShmNode = pShmNode;
38462 pShmNode->pInode = pDbFd->pInode;
38464 pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
38465 if( pShmNode->pShmMutex==0 ){
38471 if( pInode->bProcessLock==0 ){
38472 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
38473 pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW,
38476 if( pShmNode->hShm<0 ){
38477 pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW,
38479 if( pShmNode->hShm<0 ){
38483 pShmNode->isReadonly = 1;
38490 robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid);
38498 p->pShmNode = pShmNode;
38500 p->id = pShmNode->nextShmId++;
38502 pShmNode->nRef++;
38503 pDbFd->pShm = p;
38510 ** at pShmNode->pFirst. This must be done while holding the
38511 ** pShmNode->pShmMutex.
38513 sqlite3_mutex_enter(pShmNode->pShmMutex);
38514 p->pNext = pShmNode->pFirst;
38515 pShmNode->pFirst = p;
38516 sqlite3_mutex_leave(pShmNode->pShmMutex);
38529 ** shared-memory associated with the database file fd. Shared-memory regions
38530 ** are numbered starting from zero. Each shared-memory region is szRegion
38535 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
38538 ** bExtend is non-zero and the requested shared-memory region has not yet
38541 ** If the shared-memory region has already been allocated or is allocated by
38560 /* If the shared-memory file has not yet been opened, open it now. */
38561 if( pDbFd->pShm==0 ){
38566 p = pDbFd->pShm;
38567 pShmNode = p->pShmNode;
38568 sqlite3_mutex_enter(pShmNode->pShmMutex);
38569 if( pShmNode->isUnlocked ){
38572 pShmNode->isUnlocked = 0;
38574 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
38575 assert( pShmNode->pInode==pDbFd->pInode );
38576 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38577 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38582 if( pShmNode->nRegion<nReqRegion ){
38587 pShmNode->szRegion = szRegion;
38589 if( pShmNode->hShm>=0 ){
38591 ** Check to see if it has been allocated (i.e. if the wal-index file is
38594 if( osFstat(pShmNode->hShm, &sStat) ){
38622 if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){
38623 const char *zFile = pShmNode->zFilename;
38634 pShmNode->apRegion, nReqRegion*sizeof(char *)
38640 pShmNode->apRegion = apNew;
38641 while( pShmNode->nRegion<nReqRegion ){
38645 if( pShmNode->hShm>=0 ){
38647 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
38648 MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion
38651 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
38664 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i];
38666 pShmNode->nRegion += nShmPerMap;
38671 if( pShmNode->nRegion>iRegion ){
38672 *pp = pShmNode->apRegion[iRegion];
38676 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
38677 sqlite3_mutex_leave(pShmNode->pShmMutex);
38682 ** Check that the pShmNode->aLock[] array comports with the locking bitmasks
38692 assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
38695 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
38698 if( pX->exclMask & (1<<i) ){
38700 aLock[i] = -1;
38701 }else if( pX->sharedMask & (1<<i) ){
38708 assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
38709 return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
38714 ** Change the lock state for a shared-memory segment.
38728 unixShm *p = pDbFd->pShm; /* The shared memory being locked */
38729 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */
38732 int *aLock = pShmNode->aLock;
38734 assert( pShmNode==pDbFd->pInode->pShmNode );
38735 assert( pShmNode->pInode==pDbFd->pInode );
38743 assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 );
38744 assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 );
38760 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
38762 && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
38763 && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
38764 && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
38768 mask = (1<<(ofst+n)) - (1<<ofst);
38770 sqlite3_mutex_enter(pShmNode->pShmMutex);
38773 if( (p->exclMask|p->sharedMask) & mask ){
38778 if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
38788 }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
38790 aLock[ofst]--;
38795 p->exclMask &= ~mask;
38796 p->sharedMask &= ~mask;
38801 assert( (p->exclMask & (1<<ofst))==0 );
38802 if( (p->sharedMask & mask)==0 ){
38811 p->sharedMask |= mask;
38820 assert( (p->sharedMask & mask)==0 );
38821 if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
38828 ** also update the in-memory values. */
38832 assert( (p->sharedMask & mask)==0 );
38833 p->exclMask |= mask;
38835 aLock[ii] = -1;
38841 sqlite3_mutex_leave(pShmNode->pShmMutex);
38842 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
38843 p->id, osGetpid(0), p->sharedMask, p->exclMask));
38857 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
38858 assert( fd->pMethods->xLock==nolockLock
38866 ** Close a connection to shared-memory. Delete the underlying
38870 ** routine is a harmless no-op.
38874 int deleteFlag /* Delete shared-memory if true */
38877 unixShmNode *pShmNode; /* The underlying shared-memory file */
38882 p = pDbFd->pShm;
38884 pShmNode = p->pShmNode;
38886 assert( pShmNode==pDbFd->pInode->pShmNode );
38887 assert( pShmNode->pInode==pDbFd->pInode );
38891 sqlite3_mutex_enter(pShmNode->pShmMutex);
38892 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
38893 *pp = p->pNext;
38897 pDbFd->pShm = 0;
38898 sqlite3_mutex_leave(pShmNode->pShmMutex);
38900 /* If pShmNode->nRef has reached 0, then close the underlying
38901 ** shared-memory file, too */
38904 assert( pShmNode->nRef>0 );
38905 pShmNode->nRef--;
38906 if( pShmNode->nRef==0 ){
38907 if( deleteFlag && pShmNode->hShm>=0 ){
38908 osUnlink(pShmNode->zFilename);
38930 assert( pFd->nFetchOut==0 );
38931 if( pFd->pMapRegion ){
38932 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
38933 pFd->pMapRegion = 0;
38934 pFd->mmapSize = 0;
38935 pFd->mmapSizeActual = 0;
38949 ** If unsuccessful, an error message is logged via sqlite3_log() and
38959 int h = pFd->h; /* File descriptor open on db file */
38960 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
38961 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
38965 assert( pFd->nFetchOut==0 );
38966 assert( nNew>pFd->mmapSize );
38967 assert( nNew<=pFd->mmapSizeMax );
38969 assert( pFd->mmapSizeActual>=pFd->mmapSize );
38973 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
38978 i64 nReuse = pFd->mmapSize;
38981 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
38987 osMunmap(pReq, nOrig-nReuse);
38994 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
38997 osMunmap(pNew, nNew - nReuse);
39019 unixLogError(SQLITE_OK, zErr, pFd->zPath);
39024 pFd->mmapSizeMax = 0;
39026 pFd->pMapRegion = (void *)pNew;
39027 pFd->mmapSize = pFd->mmapSizeActual = nNew;
39031 ** Memory map or remap the file opened by file-descriptor pFd (if the file
39034 ** outstanding xFetch() references to it, this function is a no-op.
39036 ** If parameter nByte is non-negative, then it is the requested size of
39047 assert( nMap>=0 || pFd->nFetchOut==0 );
39048 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
39049 if( pFd->nFetchOut>0 ) return SQLITE_OK;
39052 struct stat statbuf; /* Low-level file information */
39053 if( osFstat(pFd->h, &statbuf) ){
39058 if( nMap>pFd->mmapSizeMax ){
39059 nMap = pFd->mmapSizeMax;
39062 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) );
39063 if( nMap!=pFd->mmapSize ){
39090 if( pFd->mmapSizeMax>0 ){
39091 if( pFd->pMapRegion==0 ){
39092 int rc = unixMapfile(pFd, -1);
39095 if( pFd->mmapSize >= iOff+nAmt ){
39096 *pp = &((u8 *)pFd->pMapRegion)[iOff];
39097 pFd->nFetchOut++;
39105 ** If the third argument is non-NULL, then this function releases a
39122 assert( (p==0)==(pFd->nFetchOut==0) );
39125 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
39128 pFd->nFetchOut--;
39133 assert( pFd->nFetchOut>=0 );
39151 ** of "finder" functions. A finder-function is used to locate the appropriate
39154 ** the correct finder-function for that VFS.
39157 ** object. The only interesting finder-function is autolockIoFinder, which
39161 ** For finder-function F, two objects are created:
39163 ** (1) The real finder-function named "FImpt()".
39170 ** directly at the finder-function since C90 rules prevent a void*
39287 ** The proxy locking method is a "super-method" in the sense that it
39289 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
39358 if( statfs(filePath, &fsInfo) != -1 ){
39370 ** Test byte-range lock using fcntl(). If the call succeeds,
39371 ** assume that the file-system supports POSIX style locks.
39377 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39417 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
39455 assert( pNew->pInode==NULL );
39460 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
39461 pNew->h = h;
39462 pNew->pVfs = pVfs;
39463 pNew->zPath = zFilename;
39464 pNew->ctrlFlags = (u8)ctrlFlags;
39466 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
39470 pNew->ctrlFlags |= UNIXFILE_PSOW;
39472 if( strcmp(pVfs->zName,"unix-excl")==0 ){
39473 pNew->ctrlFlags |= UNIXFILE_EXCL;
39477 pNew->pId = vxworksFindFileId(zFilename);
39478 if( pNew->pId==0 ){
39487 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
39492 pNew->lockingContext = (void*)zFilename;
39502 rc = findInodeInfo(pNew, &pNew->pInode);
39515 ** handle h - as it is guaranteed that no posix locks will be released
39523 h = -1;
39534 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) );
39541 pCtx->dbPath = zFilename;
39542 pCtx->reserved = 0;
39545 rc = findInodeInfo(pNew, &pNew->pInode);
39547 sqlite3_free(pNew->lockingContext);
39549 h = -1;
39570 pNew->lockingContext = zLockFile;
39579 rc = findInodeInfo(pNew, &pNew->pInode);
39580 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
39581 char *zSemName = pNew->pInode->aSemName;
39584 pNew->pId->zCanonicalName);
39587 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
39588 if( pNew->pInode->pSem == SEM_FAILED ){
39590 pNew->pInode->aSemName[0] = '\0';
39601 h = -1;
39603 pNew->ctrlFlags |= UNIXFILE_DELETE;
39609 pId->pMethods = pLockingStyle;
39652 ** pVfs->mxPathname bytes.
39658 /* It's odd to simulate an io-error here, but really this is just
39659 ** using the io-error infrastructure to test that SQLite handles this
39671 zBuf[nBuf-2] = 0;
39674 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR;
39681 ** Routine to transform a unixFile into a proxy-locking unixFile.
39682 ** Implementation in the proxy-lock division, but used by unixOpen()
39690 ** file (not a journal or super-journal file) identified by pathname
39696 ** other file descriptor open on the same file is holding a file-lock.
39702 ** such file descriptor is located, -1 is returned.
39720 ** ignored and -1 is returned. The caller will try to open a new file
39729 while( pInode && (pInode->fileId.dev!=sStat.st_dev
39730 || pInode->fileId.ino!=(u64)sStat.st_ino) ){
39731 pInode = pInode->pNext;
39735 assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
39736 sqlite3_mutex_enter(pInode->pLockMutex);
39738 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
39741 *pp = pUnused->pNext;
39743 sqlite3_mutex_leave(pInode->pLockMutex);
39783 ** this function queries the file-system for the permissions on the
39812 ** "<path to db>-journal"
39813 ** "<path to db>-wal"
39814 ** "<path to db>-journalNN"
39815 ** "<path to db>-walNN"
39820 nDb = sqlite3Strlen30(zPath) - 1;
39821 while( zPath[nDb]!='-' ){
39823 ** a '-' character. However in 8+3 filename mode, or if a corrupt
39824 ** rollback journal specifies a super-journal with a goofy name, then
39825 ** the '-' might be missing. */
39827 nDb--;
39860 ** ReadWrite() -> (READWRITE | CREATE)
39861 ** ReadOnly() -> (READONLY)
39862 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
39864 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
39878 int fd = -1; /* File descriptor returned by open() */
39897 /* If creating a super- or main-file journal, this function will open
39898 ** a file-descriptor on the directory too. The first time unixSync()
39925 /* The main DB, main journal, WAL file and super-journal are never
39932 /* Assert that the upper layer has set one of the "file-type" flags. */
39954 fd = pUnused->fd;
39961 p->pPreallocatedUnused = pUnused;
39963 /* Database filenames are double-zero terminated if they are not
39971 rc = unixGetTempname(pVfs->mxPathname, zTmpname);
39977 /* Generated temporary filenames are always double-zero terminated
39998 assert( !p->pPreallocatedUnused );
40003 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags));
40011 /* Failed to open the file for read/write access. Try read-only. */
40028 ** the case. The chown() system call will be a no-op if the current
40047 if( p->pPreallocatedUnused ){
40048 p->pPreallocatedUnused->fd = fd;
40049 p->pPreallocatedUnused->flags =
40068 p->openFlags = openFlags;
40073 if( fstatfs(fd, &fsInfo) == -1 ){
40079 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
40082 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
40098 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
40103 ** never use proxy, NULL means use proxy for non-local files only. */
40116 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
40134 sqlite3_free(p->pPreallocatedUnused);
40152 if( osUnlink(zPath)==(-1) ){
40217 ** If the last component of the pathname in z[0]..z[j-1] is something
40225 for(i=j-1; i>0 && z[i-1]!='/'; i--){}
40227 if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0;
40228 *pJ = i-1;
40248 if( osGetcwd(zOut, nOut-2)==0 ){
40255 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
40260 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
40301 ** is stored as a nul-terminated string in the buffer pointed to by
40305 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
40323 assert( pVfs->mxPathname==MAX_PATHNAME );
40326 /* It's odd to simulate an io-error here, but really this is just
40327 ** using the io-error infrastructure to test that SQLite handles this
40357 nByte = osReadlink(zIn, zDel, nOut-1);
40363 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
40408 ** message is available, it is written to zBufOut. If no error message
40410 ** error message.
40424 ** GCC with -pedantic-errors says that C90 does not allow a void* to be
40427 ** use dlsym() with -pedantic-errors?
40435 ** This work-around is unlikely to work correctly on any system where
40464 ** errors. The reports issued by valgrind are incorrect - we would
40466 ** uninitialized space in zBuf - but valgrind errors tend to worry
40529 ** The following variable, if set to a non-zero value, is interpreted as
40593 ** low-level error message when operating-system problems come up
40612 ** Proxy locking is a "uber-locking-method" in this sense: It uses the
40614 ** meta-layer over top of the primitive locking implemented above. For
40617 ** been defined - so that the primitive locking methods are available
40622 ** The default locking schemes in SQLite use byte-range locks on the
40627 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
40635 ** SHARED_RANGE 0x40000002 -> 0x40000200
40639 ** the read cache when byte-range locks are present. Enabling the read
40642 ** close-to-open semantics for ensuring cache coherency
40646 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
40655 ** -----------------
40687 ** -----------------------
40697 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
40698 ** by taking an sqlite-style shared lock on the conch file, reading the
40702 ** is patterned after the database file name as ".<databasename>-conch".
40710 ** The proxy file - a single-byte file used for all advisory file locks
40726 ** ---------------------
40730 ** Database files accessed on non-local file systems are
40775 int conchHeld; /* 1 if the conch is held, -1 if lockless */
40808 if( lPath[len-1]!='/' ){
40837 if( lockPath[i] == '/' && (i - start > 0) ){
40839 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
40840 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
40872 int fd = -1;
40881 ** 2. if that fails, and this is a lock file (not-conch), try creating
40883 ** 3. if that fails, try to open the file read-only
40888 fd = pUnused->fd;
40929 pNew->openFlags = openFlags;
40933 pUnused->fd = fd;
40934 pUnused->flags = openFlags;
40935 pNew->pPreallocatedUnused = pUnused;
40993 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */
41005 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41006 unixFile *conchFile = pCtx->conchFile;
41009 char *cPath = pCtx->conchFilePath;
41013 int fd = -1;
41014 int rc = -1;
41017 /* create a new path by replace the trailing '-conch' with '-break' */
41020 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
41025 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
41045 fprintf(stderr, "broke stale lock on %s\n", cPath);
41046 robust_close(pFile, conchFile->h, __LINE__);
41047 conchFile->h = fd;
41048 conchFile->openFlags = O_RDWR | O_CREAT;
41056 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
41061 /* Take the requested lock on the conch file and break a stale lock if the
41065 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41066 unixFile *conchFile = pCtx->conchFile;
41073 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
41083 if( osFstat(conchFile->h, &buf) ){
41102 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
41124 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
41127 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
41137 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL
41143 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41145 if( pCtx->conchHeld!=0 ){
41148 unixFile *conchFile = pCtx->conchFile;
41161 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h,
41162 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
41178 storeLastErrno(pFile, conchFile->lastErrno);
41190 ** retry with a new auto-generated path
41198 if( !pCtx->lockProxyPath ){
41199 /* for auto-named local lock file, just check the host ID and we'll
41203 size_t pathLen = (readLen - PROXY_PATHINDEX);
41206 pathLen=MAXPATHLEN-1;
41216 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
41217 readLen-PROXY_PATHINDEX)
41225 if( (conchFile->openFlags&O_RDWR) == 0 ){
41231 if( !pCtx->lockProxyPath ){
41232 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
41241 futimes(conchFile->h, NULL);
41243 if( conchFile->pInode && conchFile->pInode->nShared>1 ){
41259 if( pCtx->lockProxyPath!=NULL ){
41260 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath,
41266 robust_ftruncate(conchFile->h, writeSize);
41268 full_fsync(conchFile->h,0,0);
41274 int err = osFstat(pFile->h, &buf);
41280 osFchmod(conchFile->h, cmode);
41283 rc = osFchmod(conchFile->h, cmode);
41284 }while( rc==(-1) && errno==EINTR );
41300 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
41303 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
41304 if( rc==SQLITE_OK && pFile->openFlags ){
41306 if( pFile->h>=0 ){
41307 robust_close(pFile, pFile->h, __LINE__);
41309 pFile->h = -1;
41310 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
41313 pFile->h = fd;
41319 if( rc==SQLITE_OK && !pCtx->lockProxy ){
41320 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
41321 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
41324 ** so try again via auto-naming
41336 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
41337 if( !pCtx->lockProxyPath ){
41343 pCtx->conchHeld = 1;
41345 if( pCtx->lockProxy->pMethod == &afpIoMethods ){
41347 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
41348 afpCtx->dbPath = pCtx->lockProxyPath;
41351 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
41353 OSTRACE(("TAKECONCH %d %s\n", conchFile->h,
41356 } while (1); /* in case we need to retry the :auto: lock file -
41369 pCtx = (proxyLockingContext *)pFile->lockingContext;
41370 conchFile = pCtx->conchFile;
41371 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h,
41372 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
41374 if( pCtx->conchHeld>0 ){
41375 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
41377 pCtx->conchHeld = 0;
41378 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h,
41396 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
41408 for( i=(len-1); i>=0; i-- ){
41420 /* append the "-conch" suffix to the file */
41421 memcpy(&conchPath[i+1], "-conch", 7);
41428 /* Takes a fully configured proxy locking-style unix file and switches
41432 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41433 char *oldPath = pCtx->lockProxyPath;
41436 if( pFile->eFileLock!=NO_LOCK ){
41445 unixFile *lockProxy = pCtx->lockProxy;
41446 pCtx->lockProxy=NULL;
41447 pCtx->conchHeld = 0;
41449 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
41454 pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
41469 if( pFile->pMethod == &afpIoMethods ){
41472 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41473 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath,
41477 if( pFile->pMethod == &dotlockIoMethods ){
41480 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
41481 memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
41484 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
41485 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
41495 ** ->lockingContext
41496 ** ->pMethod
41504 if( pFile->eFileLock!=NO_LOCK ){
41514 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h,
41523 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
41525 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
41526 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
41528 ** (c) the file system is read-only, then enable no-locking access.
41536 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
41538 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
41543 pCtx->conchHeld = -1; /* read only FS/ lockless */
41549 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
41553 pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
41554 if( pCtx->dbPath==NULL ){
41562 pCtx->oldLockingContext = pFile->lockingContext;
41563 pFile->lockingContext = pCtx;
41564 pCtx->pOldMethod = pFile->pMethod;
41565 pFile->pMethod = &proxyIoMethods;
41567 if( pCtx->conchFile ){
41568 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
41569 sqlite3_free(pCtx->conchFile);
41571 sqlite3DbFree(0, pCtx->lockProxyPath);
41572 sqlite3_free(pCtx->conchFilePath);
41575 OSTRACE(("TRANSPROXY %d %s\n", pFile->h,
41589 if( pFile->pMethod == &proxyIoMethods ){
41590 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
41592 if( pCtx->lockProxyPath ){
41593 *(const char **)pArg = pCtx->lockProxyPath;
41605 int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
41608 /* turn off proxy locking - not supported. If support is added for
41614 /* turn off proxy locking - already off - NOOP */
41621 (proxyLockingContext*)pFile->lockingContext;
41623 || (pCtx->lockProxyPath &&
41624 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
41647 ** above this point are all utilities. The lock-related methods of the
41648 ** proxy-locking sqlite3_io_method object follow.
41655 ** to a non-zero value otherwise *pResOut is set to zero. The return value
41662 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41663 if( pCtx->conchHeld>0 ){
41664 unixFile *proxy = pCtx->lockProxy;
41665 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
41674 ** Lock the file with the lock specified by parameter eFileLock - one
41688 ** UNLOCKED -> SHARED
41689 ** SHARED -> RESERVED
41690 ** SHARED -> (PENDING) -> EXCLUSIVE
41691 ** RESERVED -> (PENDING) -> EXCLUSIVE
41692 ** PENDING -> EXCLUSIVE
41701 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41702 if( pCtx->conchHeld>0 ){
41703 unixFile *proxy = pCtx->lockProxy;
41704 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
41705 pFile->eFileLock = proxy->eFileLock;
41719 ** the requested locking level, this routine is a no-op.
41725 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41726 if( pCtx->conchHeld>0 ){
41727 unixFile *proxy = pCtx->lockProxy;
41728 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
41729 pFile->eFileLock = proxy->eFileLock;
41743 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
41744 unixFile *lockProxy = pCtx->lockProxy;
41745 unixFile *conchFile = pCtx->conchFile;
41749 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
41751 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
41754 pCtx->lockProxy = 0;
41757 if( pCtx->conchHeld ){
41761 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
41765 sqlite3DbFree(0, pCtx->lockProxyPath);
41766 sqlite3_free(pCtx->conchFilePath);
41767 sqlite3DbFree(0, pCtx->dbPath);
41769 pFile->lockingContext = pCtx->oldLockingContext;
41770 pFile->pMethod = pCtx->pOldMethod;
41772 return pFile->pMethod->xClose(id);
41792 ** This routine registers all VFS implementations for unix-like operating
41809 ** when compiling with -pedantic-errors on GCC.)
41812 ** finder-function. The finder-function returns a pointer to the
41815 ** macro for addition information on finder-functions.
41863 UNIXVFS("unix-none", nolockIoFinder ),
41864 UNIXVFS("unix-dotfile", dotlockIoFinder ),
41865 UNIXVFS("unix-excl", posixIoFinder ),
41867 UNIXVFS("unix-namedsem", semIoFinder ),
41870 UNIXVFS("unix-posix", posixIoFinder ),
41873 UNIXVFS("unix-flock", flockIoFinder ),
41876 UNIXVFS("unix-afp", afpIoFinder ),
41877 UNIXVFS("unix-nfs", nfsIoFinder ),
41878 UNIXVFS("unix-proxy", proxyIoFinder ),
41883 /* Double-check that the aSyscall[] array has been constructed
41900 ** This routine is a no-op for unix.
41946 ** all of the platform-specific files (os_*.c) and is #included into those
41958 ** switch. The following code should catch this problem at compile-time.
41972 ** high-performance timing routines.
41988 ** This file contains inline asm code for retrieving "high-performance"
41995 ** The following routine only works on pentium-class (or newer) processors.
41997 ** processor and returns that value. This can be used for high-res
42068 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start
42092 || sqlite3_io_error_pending-- == 1 ) \
42107 sqlite3_diskfull_pending--; \
42152 ** based on the sub-platform)?
42160 ** based on the sub-platform)?
42248 ** characters, so we allocate 4 bytes per character assuming worst-case of
42249 ** 4-bytes-per-character for UTF8.
42265 ** Maximum error message length (in chars) for WinRT.
42272 ** Returns non-zero if the character should be treated as a directory
42336 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
42349 typedef struct winShm winShm; /* A connection to shared-memory */
42350 typedef struct winShmNode winShmNode; /* A region of shared-memory */
42408 BOOL bNoLock; /* Non-zero if locking is disabled. */
42422 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD)))
42432 * If this is non-zero, an isolated heap will be created by the native Win32
42438 * WARNING: It is important to note that when this setting is non-zero and the
42450 * This is the maximum possible initial size of the Win32-specific heap, in
42458 * This is the extra space for the initial size of the Win32-specific heap,
42471 # define SQLITE_WIN32_MAX_CACHE_SIZE (((SQLITE_WIN32_HEAP_MAX_INIT_SIZE) - \
42478 * Win32-specific heap. It cannot be negative.
42484 # define SQLITE_WIN32_CACHE_SIZE (-(SQLITE_DEFAULT_CACHE_SIZE))
42490 * initial size of the Win32-specific heap to exceed the maximum amount
42499 * The initial size of the Win32-specific heap. This value may be zero.
42508 * The maximum size of the Win32-specific heap. This value may be zero.
42524 ** The winMemData structure stores information required by the Win32-specific
42608 ** Many system calls are accessed through pointer-to-functions so that
43225 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function"
43337 int i = -1;
43341 for(i=0; i<ArraySize(aSyscall)-1; i++){
43356 ** "pnLargest" argument, if non-zero, will be used to return the size of the
43446 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
43447 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
43448 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
43514 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
43542 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
43567 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
43662 if( n==(SIZE_T)-1 ){
43684 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43685 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43688 if( !pWinMemData->hHeap ){
43696 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
43698 if( !pWinMemData->hHeap ){
43705 pWinMemData->bOwned = TRUE;
43706 assert( pWinMemData->bOwned );
43709 pWinMemData->hHeap = osGetProcessHeap();
43710 if( !pWinMemData->hHeap ){
43715 pWinMemData->bOwned = FALSE;
43716 assert( !pWinMemData->bOwned );
43718 assert( pWinMemData->hHeap!=0 );
43719 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43721 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43733 assert( pWinMemData->magic1==WINMEM_MAGIC1 );
43734 assert( pWinMemData->magic2==WINMEM_MAGIC2 );
43736 if( pWinMemData->hHeap ){
43737 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
43739 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
43741 if( pWinMemData->bOwned ){
43742 if( !osHeapDestroy(pWinMemData->hHeap) ){
43744 osGetLastError(), (void*)pWinMemData->hHeap);
43746 pWinMemData->bOwned = FALSE;
43748 pWinMemData->hHeap = NULL;
43753 ** Populate the low-level memory allocation function pointers in
43780 ** Convert a UTF-8 string to Microsoft Unicode.
43788 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
43796 nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
43806 ** Convert a Microsoft Unicode string to UTF-8.
43814 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
43822 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
43842 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
43851 nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
43861 ** Convert a Microsoft Unicode string to a multi-byte character string,
43871 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
43879 nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
43889 ** Convert a multi-byte character string to UTF-8.
43907 ** Convert a UTF-8 string to a multi-byte character string.
44022 ** it accepts a UTF-8 string.
44059 ** it accepts a UTF-16 string.
44092 ** is zero if the error message fits in the buffer, or non-zero
44093 ** otherwise (if the message was truncated).
44172 ** This function - winLogErrorAtLine() - is only ever called via the macro
44176 ** It logs a message using sqlite3_log() containing the current value of
44177 ** error code and, if possible, the human-readable equivalent from
44183 ** failed and the associated file-system path, if any.
44203 "os_win.c:%d: (%lu) %s(%s) - %s",
44212 ** will be retried following a locking error - probably caused by
44229 ** non-zero if the error code is transient in nature and the operation
44317 y.tm_year = pTm.wYear - 1900;
44318 y.tm_mon = pTm.wMonth - 1;
44332 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
44366 memset(&pFile->local, 0, sizeof(pFile->local));
44376 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
44377 if (!pFile->hMutex){
44378 pFile->lastErrno = osGetLastError();
44380 return winLogError(SQLITE_IOERR, pFile->lastErrno,
44385 winceMutexAcquire(pFile->hMutex);
44388 ** case-sensitive, take advantage of that by uppercasing the mutex name
44392 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
44397 ** must be zero-initialized */
44406 if( pFile->hShared ){
44407 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
44410 if( !pFile->shared ){
44411 pFile->lastErrno = osGetLastError();
44412 winLogError(SQLITE_IOERR, pFile->lastErrno,
44415 osCloseHandle(pFile->hShared);
44416 pFile->hShared = NULL;
44421 if( pFile->hShared==NULL ){
44423 pFile->lastErrno = lastErrno;
44424 winLogError(SQLITE_IOERR, pFile->lastErrno,
44428 winceMutexRelease(pFile->hMutex);
44429 osCloseHandle(pFile->hMutex);
44430 pFile->hMutex = NULL;
44436 memset(pFile->shared, 0, sizeof(winceLock));
44439 winceMutexRelease(pFile->hMutex);
44447 if (pFile->hMutex){
44449 winceMutexAcquire(pFile->hMutex);
44453 if (pFile->local.nReaders){
44454 pFile->shared->nReaders --;
44456 if (pFile->local.bReserved){
44457 pFile->shared->bReserved = FALSE;
44459 if (pFile->local.bPending){
44460 pFile->shared->bPending = FALSE;
44462 if (pFile->local.bExclusive){
44463 pFile->shared->bExclusive = FALSE;
44466 /* De-reference and close our copy of the shared memory handle */
44467 osUnmapViewOfFile(pFile->shared);
44468 osCloseHandle(pFile->hShared);
44471 winceMutexRelease(pFile->hMutex);
44472 osCloseHandle(pFile->hMutex);
44473 pFile->hMutex = NULL;
44493 if (!pFile->hMutex) return TRUE;
44494 winceMutexAcquire(pFile->hMutex);
44499 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
44500 pFile->shared->bExclusive = TRUE;
44501 pFile->local.bExclusive = TRUE;
44506 /* Want a read-only lock? */
44509 if (pFile->shared->bExclusive == 0){
44510 pFile->local.nReaders ++;
44511 if (pFile->local.nReaders == 1){
44512 pFile->shared->nReaders ++;
44522 if (pFile->shared->bPending == 0) {
44523 pFile->shared->bPending = TRUE;
44524 pFile->local.bPending = TRUE;
44532 if (pFile->shared->bReserved == 0) {
44533 pFile->shared->bReserved = TRUE;
44534 pFile->local.bReserved = TRUE;
44539 winceMutexRelease(pFile->hMutex);
44559 if (!pFile->hMutex) return TRUE;
44560 winceMutexAcquire(pFile->hMutex);
44565 if (pFile->local.bExclusive){
44567 pFile->local.bExclusive = FALSE;
44568 pFile->shared->bExclusive = FALSE;
44573 else if (pFile->local.nReaders){
44576 pFile->local.nReaders --;
44577 if (pFile->local.nReaders == 0)
44579 pFile->shared->nReaders --;
44588 if (pFile->local.bPending){
44589 pFile->local.bPending = FALSE;
44590 pFile->shared->bPending = FALSE;
44597 if (pFile->local.bReserved) {
44598 pFile->local.bReserved = FALSE;
44599 pFile->shared->bReserved = FALSE;
44604 winceMutexRelease(pFile->hMutex);
44684 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
44690 ** Otherwise, set pFile->lastErrno and return non-zero.
44699 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
44705 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
44711 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
44715 pFile->lastErrno = lastErrno;
44716 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44717 "winSeekFile", pFile->zPath);
44718 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44722 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44733 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
44736 pFile->lastErrno = osGetLastError();
44737 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
44738 "winSeekFile", pFile->zPath);
44739 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
44743 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
44771 assert( pFile->pShm==0 );
44773 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
44775 osGetCurrentProcessId(), pFile, pFile->h));
44782 rc = osCloseHandle(pFile->h);
44788 winVfsAppData *pAppData = (winVfsAppData*)pFile->pVfs->pAppData;
44789 if( pAppData==NULL || !pAppData->bNoLock ){
44793 if( pFile->zDeleteOnClose ){
44796 osDeleteFileW(pFile->zDeleteOnClose)==0
44797 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
44802 sqlite3_free(pFile->zDeleteOnClose);
44806 pFile->h = NULL;
44808 OpenCounter(-1);
44810 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed"));
44813 "winClose", pFile->zPath);
44840 pFile->h, pBuf, amt, offset, pFile->locktype));
44845 if( offset<pFile->mmapSize ){
44846 if( offset+amt <= pFile->mmapSize ){
44847 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
44848 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44849 osGetCurrentProcessId(), pFile, pFile->h));
44852 int nCopy = (int)(pFile->mmapSize - offset);
44853 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
44855 amt -= nCopy;
44864 osGetCurrentProcessId(), pFile, pFile->h));
44867 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
44872 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
44877 pFile->lastErrno = lastErrno;
44879 osGetCurrentProcessId(), pFile, pFile->h));
44880 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
44881 "winRead", pFile->zPath);
44885 /* Unread parts of the buffer must be zero-filled */
44886 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
44888 osGetCurrentProcessId(), pFile, pFile->h));
44893 osGetCurrentProcessId(), pFile, pFile->h));
44918 pFile->h, pBuf, amt, offset, pFile->locktype));
44923 if( offset<pFile->mmapSize ){
44924 if( offset+amt <= pFile->mmapSize ){
44925 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
44926 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
44927 osGetCurrentProcessId(), pFile, pFile->h));
44930 int nCopy = (int)(pFile->mmapSize - offset);
44931 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
44933 amt -= nCopy;
44961 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
44963 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
44979 nRem -= nWrite;
44982 pFile->lastErrno = lastErrno;
44988 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
44989 || ( pFile->lastErrno==ERROR_DISK_FULL )){
44991 osGetCurrentProcessId(), pFile, pFile->h));
44992 return winLogError(SQLITE_FULL, pFile->lastErrno,
44993 "winWrite1", pFile->zPath);
44996 osGetCurrentProcessId(), pFile, pFile->h));
44997 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
44998 "winWrite2", pFile->zPath);
45003 osGetCurrentProcessId(), pFile, pFile->h));
45016 if( pFile->nFetchOut>0 ){
45017 /* File truncation is a no-op if there are outstanding memory mapped
45023 ** No real harm comes of this - the database file is not corrupted,
45027 ** The only feasible work-around is to defer the truncation until after
45028 ** all references to memory-mapped content are closed. That is doable,
45031 ** now to simply make trancations a no-op if there are pending reads. We
45041 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype));
45043 /* If the user has configured a chunk-size for this file, truncate the
45048 if( pFile->szChunk>0 ){
45049 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
45053 if( pFile->pMapRegion ){
45054 oldMmapSize = pFile->mmapSize;
45061 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
45063 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
45064 "winTruncate1", pFile->zPath);
45065 }else if( 0==osSetEndOfFile(pFile->h) &&
45067 pFile->lastErrno = lastErrno;
45068 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
45069 "winTruncate2", pFile->zPath);
45075 winMapfile(pFile, -1);
45083 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc)));
45129 osGetCurrentProcessId(), pFile, pFile->h, flags,
45130 pFile->locktype));
45142 ** no-op
45145 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
45146 osGetCurrentProcessId(), pFile, pFile->h));
45150 if( pFile->pMapRegion ){
45151 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){
45152 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
45154 pFile, pFile->pMapRegion));
45156 pFile->lastErrno = osGetLastError();
45157 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, "
45159 pFile, pFile->pMapRegion));
45160 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
45161 "winSync1", pFile->zPath);
45165 rc = osFlushFileBuffers(pFile->h);
45169 osGetCurrentProcessId(), pFile, pFile->h));
45172 pFile->lastErrno = osGetLastError();
45174 osGetCurrentProcessId(), pFile, pFile->h));
45175 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
45176 "winSync2", pFile->zPath);
45191 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
45196 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
45200 pFile->lastErrno = osGetLastError();
45201 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
45202 "winFileSize", pFile->zPath);
45211 lowerBits = osGetFileSize(pFile->h, &upperBits);
45215 pFile->lastErrno = lastErrno;
45216 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
45217 "winFileSize", pFile->zPath);
45222 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
45264 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
45271 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
45273 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
45281 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
45282 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
45283 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
45287 pFile->lastErrno = osGetLastError();
45290 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
45300 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
45302 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45306 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
45310 pFile->lastErrno = lastErrno;
45311 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
45312 "winUnlockReadLock", pFile->zPath);
45314 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
45319 ** Lock the file with the lock specified by parameter locktype - one
45333 ** UNLOCKED -> SHARED
45334 ** SHARED -> RESERVED
45335 ** SHARED -> (PENDING) -> EXCLUSIVE
45336 ** RESERVED -> (PENDING) -> EXCLUSIVE
45337 ** PENDING -> EXCLUSIVE
45347 int newLocktype; /* Set pFile->locktype to this value before exiting */
45354 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45360 if( pFile->locktype>=locktype ){
45361 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
45365 /* Do not allow any kind of write-lock on a read-only database
45367 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){
45373 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
45375 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
45381 newLocktype = pFile->locktype;
45382 if( pFile->locktype==NO_LOCK
45383 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
45386 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
45389 ** around problems caused by indexing and/or anti-virus software on
45395 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
45396 pFile->h, cnt, res));
45398 pFile->lastErrno = lastErrno;
45400 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
45401 pFile->h, cnt, sqlite3ErrName(rc)));
45415 assert( pFile->locktype==NO_LOCK );
45427 assert( pFile->locktype==SHARED_LOCK );
45428 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
45446 assert( pFile->locktype>=SHARED_LOCK );
45448 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
45462 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45471 pFile->lastErrno = lastErrno;
45473 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
45474 pFile->h, locktype, newLocktype));
45476 pFile->locktype = (u8)newLocktype;
45478 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45485 ** non-zero, otherwise zero.
45492 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
45495 if( pFile->locktype>=RESERVED_LOCK ){
45497 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
45499 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0);
45501 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45504 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
45507 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
45508 pFile->h, pResOut, *pResOut));
45517 ** the requested locking level, this routine is a no-op.
45530 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
45531 type = pFile->locktype;
45533 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
45538 "winUnlock", pFile->zPath);
45542 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
45548 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
45550 pFile->locktype = (u8)locktype;
45552 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
45557 ****************************** No-op Locking **********************************
45563 ** This locking mode is appropriate for use on read-only databases
45564 ** (ex: databases that are burned into CD-ROM, for example.) It can
45591 /******************* End of the no-op lock implementation *********************
45596 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
45598 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
45602 *pArg = (pFile->ctrlFlags & mask)!=0;
45604 pFile->ctrlFlags &= ~mask;
45606 pFile->ctrlFlags |= mask;
45621 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
45624 *(int*)pArg = pFile->locktype;
45625 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45629 *(int*)pArg = (int)pFile->lastErrno;
45630 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45634 pFile->szChunk = *(int *)pArg;
45635 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45639 if( pFile->szChunk>0 ){
45650 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45653 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45658 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45663 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45667 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
45668 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45683 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45688 *phFile = pFile->h;
45689 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
45695 HANDLE hOldFile = pFile->h;
45696 pFile->h = *phFile;
45699 hOldFile, pFile->h));
45705 int rc = winGetTempname(pFile->pVfs, &zTFile);
45709 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45722 ** least a 64-bit type. */
45727 *(i64*)pArg = pFile->mmapSizeMax;
45728 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
45729 pFile->mmapSizeMax = newLimit;
45730 if( pFile->mmapSize>0 ){
45732 rc = winMapfile(pFile, -1);
45735 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
45740 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
45750 ** if two files are created in the same file-system directory (i.e.
45765 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
45807 ** log-summary, each thread has its own winFile object, but they all
45809 ** log-summary is opened only once per process.
45817 ** The following fields are read-only after the object is created:
45832 int szRegion; /* Size of shared-memory regions */
45834 u8 isReadonly; /* True if read-only */
45863 ** are read-only thereafter:
45868 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
45895 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
45903 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
45905 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
45906 pFile->hFile.h, lockType, ofst, nByte));
45908 /* Release/Acquire the system-level lock */
45910 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
45915 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
45921 pFile->lastErrno = osGetLastError();
45925 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
45926 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
45927 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
45939 ** This is not a VFS shared-memory method; it is a utility function called
45940 ** by VFS shared-memory methods.
45946 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
45950 if( p->nRef==0 ){
45952 if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
45953 for(i=0; i<p->nRegion; i++){
45954 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
45955 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
45958 bRc = osCloseHandle(p->aRegion[i].hMap);
45959 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
45963 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
45965 winClose((sqlite3_file *)&p->hFile);
45971 winDelete(pVfs, p->zFilename, 0);
45975 *pp = p->pNext;
45976 sqlite3_free(p->aRegion);
45979 pp = &p->pNext;
45991 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
45997 if( pShmNode->isReadonly ){
45998 pShmNode->isUnlocked = 1;
46001 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
46004 "winLockSharedMemory", pShmNode->zFilename);
46016 ** Open the shared-memory area associated with database file pDbFd.
46018 ** When opening a new shared-memory file, if no other instances of that
46029 assert( pDbFd->pShm==0 ); /* Not previously opened */
46036 nName = sqlite3Strlen30(pDbFd->zPath);
46042 pNew->zFilename = (char*)&pNew[1];
46043 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
46044 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
46050 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
46054 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
46064 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
46065 pShmNode->pNext = winShmNodeList;
46069 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
46070 if( pShmNode->mutex==0 ){
46076 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
46081 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
46082 (sqlite3_file*)&pShmNode->hFile,
46086 pShmNode->zFilename);
46089 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
46096 p->pShmNode = pShmNode;
46098 p->id = pShmNode->nextShmId++;
46100 pShmNode->nRef++;
46101 pDbFd->pShm = p;
46108 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
46111 sqlite3_mutex_enter(pShmNode->mutex);
46112 p->pNext = pShmNode->pFirst;
46113 pShmNode->pFirst = p;
46114 sqlite3_mutex_leave(pShmNode->mutex);
46120 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
46128 ** Close a connection to shared-memory. Delete the underlying
46135 winFile *pDbFd; /* Database holding shared-memory */
46137 winShmNode *pShmNode; /* The underlying shared-memory file */
46141 p = pDbFd->pShm;
46143 pShmNode = p->pShmNode;
46147 sqlite3_mutex_enter(pShmNode->mutex);
46148 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
46149 *pp = p->pNext;
46153 pDbFd->pShm = 0;
46154 sqlite3_mutex_leave(pShmNode->mutex);
46156 /* If pShmNode->nRef has reached 0, then close the underlying
46157 ** shared-memory file, too */
46159 assert( pShmNode->nRef>0 );
46160 pShmNode->nRef--;
46161 if( pShmNode->nRef==0 ){
46162 winShmPurge(pDbFd->pVfs, deleteFlag);
46170 ** Change the lock state for a shared-memory segment.
46179 winShm *p = pDbFd->pShm; /* The shared memory being locked */
46181 winShmNode *pShmNode = p->pShmNode;
46193 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
46195 sqlite3_mutex_enter(pShmNode->mutex);
46200 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46202 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
46203 allMask |= pX->sharedMask;
46206 /* Unlock the system-level locks */
46215 p->exclMask &= ~mask;
46216 p->sharedMask &= ~mask;
46225 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46226 if( (pX->exclMask & mask)!=0 ){
46230 allShared |= pX->sharedMask;
46244 p->sharedMask |= mask;
46250 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
46251 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
46263 assert( (p->sharedMask & mask)==0 );
46264 p->exclMask |= mask;
46268 sqlite3_mutex_leave(pShmNode->mutex);
46269 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
46270 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
46285 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */
46292 ** shared-memory associated with the database file fd. Shared-memory regions
46293 ** are numbered starting from zero. Each shared-memory region is szRegion
46298 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
46301 ** isWrite is non-zero and the requested shared-memory region has not yet
46304 ** If the shared-memory region has already been allocated or is allocated by
46317 winShm *pShm = pDbFd->pShm;
46326 pShm = pDbFd->pShm;
46329 pShmNode = pShm->pShmNode;
46331 sqlite3_mutex_enter(pShmNode->mutex);
46332 if( pShmNode->isUnlocked ){
46335 pShmNode->isUnlocked = 0;
46337 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
46339 if( pShmNode->nRegion<=iRegion ){
46342 sqlite3_int64 sz; /* Current size of wal-index file */
46344 pShmNode->szRegion = szRegion;
46347 ** Check to see if it has been allocated (i.e. if the wal-index file is
46350 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
46353 "winShmMap1", pDbFd->zPath);
46361 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
46365 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
46368 "winShmMap2", pDbFd->zPath);
46375 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
46381 pShmNode->aRegion = apNew;
46383 if( pShmNode->isReadonly ){
46388 while( pShmNode->nRegion<=iRegion ){
46389 HANDLE hMap = NULL; /* file-mapping handle */
46393 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
46397 hMap = osCreateFileMappingW(pShmNode->hFile.h,
46401 hMap = osCreateFileMappingA(pShmNode->hFile.h,
46405 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
46406 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
46409 int iOffset = pShmNode->nRegion*szRegion;
46413 iOffset - iOffsetShift, szRegion + iOffsetShift
46417 0, iOffset - iOffsetShift, szRegion + iOffsetShift
46420 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
46421 osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
46425 pShmNode->lastErrno = osGetLastError();
46426 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
46427 "winShmMap3", pDbFd->zPath);
46432 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
46433 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
46434 pShmNode->nRegion++;
46439 if( pShmNode->nRegion>iRegion ){
46442 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
46447 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
46448 sqlite3_mutex_leave(pShmNode->mutex);
46465 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
46467 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
46468 pFile->mmapSize, pFile->mmapSizeMax));
46469 if( pFile->pMapRegion ){
46470 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
46471 pFile->lastErrno = osGetLastError();
46472 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
46474 pFile->pMapRegion));
46475 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46476 "winUnmapfile1", pFile->zPath);
46478 pFile->pMapRegion = 0;
46479 pFile->mmapSize = 0;
46481 if( pFile->hMap!=NULL ){
46482 if( !osCloseHandle(pFile->hMap) ){
46483 pFile->lastErrno = osGetLastError();
46484 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
46485 osGetCurrentProcessId(), pFile, pFile->hMap));
46486 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
46487 "winUnmapfile2", pFile->zPath);
46489 pFile->hMap = NULL;
46491 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46497 ** Memory map or remap the file opened by file-descriptor pFd (if the file
46500 ** outstanding xFetch() references to it, this function is a no-op.
46502 ** If parameter nByte is non-negative, then it is the requested size of
46516 assert( nMap>=0 || pFd->nFetchOut==0 );
46517 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
46520 if( pFd->nFetchOut>0 ) return SQLITE_OK;
46525 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
46530 if( nMap>pFd->mmapSizeMax ){
46531 nMap = pFd->mmapSizeMax;
46533 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
46535 if( nMap==0 && pFd->mmapSize>0 ){
46538 if( nMap!=pFd->mmapSize ){
46545 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
46551 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
46553 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
46557 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
46561 if( pFd->hMap==NULL ){
46562 pFd->lastErrno = osGetLastError();
46563 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46564 "winMapfile1", pFd->zPath);
46566 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
46573 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
46575 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
46578 osCloseHandle(pFd->hMap);
46579 pFd->hMap = NULL;
46580 pFd->lastErrno = osGetLastError();
46581 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
46582 "winMapfile2", pFd->zPath);
46584 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
46588 pFd->pMapRegion = pNew;
46589 pFd->mmapSize = nMap;
46592 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
46620 if( pFd->mmapSizeMax>0 ){
46621 if( pFd->pMapRegion==0 ){
46622 int rc = winMapfile(pFd, -1);
46629 if( pFd->mmapSize >= iOff+nAmt ){
46630 assert( pFd->pMapRegion!=0 );
46631 *pp = &((u8 *)pFd->pMapRegion)[iOff];
46632 pFd->nFetchOut++;
46643 ** If the third argument is non-NULL, then this function releases a
46659 assert( (p==0)==(pFd->nFetchOut==0) );
46662 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
46668 pFd->nFetchOut--;
46672 ** is unnecessary can be omitted - potentially improving
46677 assert( pFd->nFetchOut>=0 );
46765 ** supports for filenames into UTF-8. Space to hold the result is
46784 ** Convert a UTF-8 filename into whatever form the underlying
46804 ** This function returns non-zero if the specified UTF-8 string buffer
46812 if( winIsDirSep(zBuf[nLen-1]) ){
46838 /* It's odd to simulate an io-error here, but really this is just
46839 ** using the io-error infrastructure to test that SQLite handles this
46847 nMax = pVfs->mxPathname; nBuf = nMax + 2;
46850 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46858 nDir = nMax - (nPre + 15);
46863 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
46868 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
46908 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46921 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46929 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
46936 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
46942 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46960 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46966 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
46978 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46988 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
46993 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
47003 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
47017 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47035 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
47039 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
47044 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
47050 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
47096 const char *zName, /* Name of the file (UTF-8) */
47113 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
47155 /* The main DB, main journal, WAL file and super-journal are never
47162 /* Assert that the upper layer has set one of the "file-type" flags. */
47171 pFile->h = INVALID_HANDLE_VALUE;
47193 /* Database filenames are double-zero terminated if they are not
47339 pFile->lastErrno = lastErrno;
47340 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
47357 pAppData = (winVfsAppData*)pVfs->pAppData;
47362 && ((pAppData==NULL) || !pAppData->bNoLock)
47368 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
47373 pFile->zDeleteOnClose = zConverted;
47381 id->pMethods = pAppData ? pAppData->pMethod : &winIoMethod;
47382 pFile->pVfs = pVfs;
47383 pFile->h = h;
47385 pFile->ctrlFlags |= WINFILE_RDONLY;
47390 pFile->ctrlFlags |= WINFILE_PSOW;
47392 pFile->lastErrno = NO_ERROR;
47393 pFile->zPath = zName;
47395 pFile->hMap = NULL;
47396 pFile->pMapRegion = 0;
47397 pFile->mmapSize = 0;
47398 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
47554 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
47600 ** Returns non-zero if the specified path name starts with the "long path"
47611 ** Returns non-zero if the specified path name starts with a drive letter
47621 ** Returns non-zero if the specified path name should be used verbatim. If
47622 ** non-zero is returned from this function, the calling function must simply
47623 ** use the provided path name verbatim -OR- resolve it into a full path name
47659 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname
47685 assert( nFull>=pVfs->mxPathname );
47693 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47699 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
47709 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47715 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
47721 zRelative, zOut, pVfs->mxPathname+1)<0 ){
47731 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
47750 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47753 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
47759 /* It's odd to simulate an io-error here, but really this is just
47760 ** using the io-error infrastructure to test that SQLite handles this
47772 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
47833 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
47850 int nFull = pVfs->mxPathname+1;
47925 for(j=0, k=p->i; j<sz; j++){
47926 p->a[k++] ^= x[j];
47927 if( k>=p->na ) k = 0;
47929 p->i = k;
47930 p->nXor += sz;
48001 ** The following variable, if set to a non-zero value, is interpreted as
48020 /* FILETIME structure is a 64-bit value representing the number of
48021 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
48028 /* 2^32 - to avoid use of LL and warnings in gcc */
48078 ** buffer with a nul-terminated UTF-8 encoded error message
48082 ** If the error message is too large for the supplied buffer,
48084 ** is zero if the error message fits in the buffer, or non-zero
48085 ** otherwise (if the message was truncated). If non-zero is returned,
48086 ** then it is not necessary to include the nul-terminator character
48089 ** Not supplying an error message will have no adverse effect
48091 ** returns an error message:
48098 ** However if an error message is supplied, it will be incorporated
48099 ** by sqlite into the error message available to the user using
48143 "win32-longpath", /* zName */
48168 "win32-none", /* zName */
48193 "win32-longpath-none", /* zName */
48214 /* Double-check that the aSyscall[] array has been constructed
48267 ** 2016-09-07
48278 ** This file implements an in-memory VFS. A database is held as a contiguous
48293 /* Access to a lower-level VFS that (might) implement dynamic loading,
48296 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
48375 memdbLock, /* xUnlock - same as xLock in this case */
48391 ** Close an memdb-file.
48399 if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ){
48400 sqlite3_free(p->aData);
48406 ** Read data from an memdb-file.
48415 if( iOfst+iAmt>p->sz ){
48417 if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
48420 memcpy(zBuf, p->aData+iOfst, iAmt);
48429 if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
48432 if( newSz>p->szMax ){
48436 if( newSz>p->szMax ) newSz = p->szMax;
48437 pNew = sqlite3Realloc(p->aData, newSz);
48439 p->aData = pNew;
48440 p->szAlloc = newSz;
48445 ** Write data to an memdb-file.
48454 if( NEVER(p->mFlags & SQLITE_DESERIALIZE_READONLY) ) return SQLITE_READONLY;
48455 if( iOfst+iAmt>p->sz ){
48457 if( iOfst+iAmt>p->szAlloc
48462 if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
48463 p->sz = iOfst+iAmt;
48465 memcpy(p->aData+iOfst, z, iAmt);
48470 ** Truncate an memdb-file.
48478 if( NEVER(size>p->sz) ) return SQLITE_FULL;
48479 p->sz = size;
48484 ** Sync an memdb-file.
48491 ** Return the current file-size of an memdb-file.
48495 *pSize = p->sz;
48500 ** Lock an memdb-file.
48505 && (p->mFlags & SQLITE_DESERIALIZE_READONLY)!=0
48509 p->eLock = eLock;
48515 ** Check if another file-handle holds a RESERVED lock on an memdb-file.
48524 ** File control method. For custom operations on an memdb-file.
48530 *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
48535 if( iLimit<p->sz ){
48537 iLimit = p->szMax;
48539 iLimit = p->sz;
48542 p->szMax = iLimit;
48551 ** Return the sector-size in bytes for an memdb-file.
48559 ** Return the device characteristic flags supported by an memdb-file.
48568 /* Fetch a page of a memory-mapped file */
48576 if( iOfst+iAmt>p->sz ){
48579 p->nMmap++;
48580 *pp = (void*)(p->aData + iOfst);
48585 /* Release a memory-mapped page */
48588 p->nMmap--;
48604 return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
48607 p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
48610 pFile->pMethods = &memdb_io_methods;
48611 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48615 #if 0 /* Only used to delete rollback journals, super-journals, and WAL
48619 ** ensure the file-system modifications are synced to disk before
48662 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
48667 ** utf-8 string describing the most recent error encountered associated
48671 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
48678 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
48685 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
48693 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
48701 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
48709 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
48714 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
48717 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
48728 if( p->base.pMethods!=&memdb_io_methods ) return 0;
48758 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48761 if( piSize ) *piSize = -1;
48764 if( piSize ) *piSize = p->sz;
48766 pOut = p->aData;
48768 pOut = sqlite3_malloc64( p->sz );
48769 if( pOut ) memcpy(pOut, p->aData, p->sz);
48773 pBt = db->aDb[iDb].pBt;
48777 rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
48796 unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
48836 sqlite3_mutex_enter(db->mutex);
48837 if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
48847 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
48851 db->init.iDb = (u8)iDb;
48852 db->init.reopenMemdb = 1;
48854 db->init.reopenMemdb = 0;
48863 p->aData = pData;
48865 p->sz = szDb;
48866 p->szAlloc = szBuf;
48867 p->szMax = szBuf;
48868 if( p->szMax<sqlite3GlobalConfig.mxMemdbSize ){
48869 p->szMax = sqlite3GlobalConfig.mxMemdbSize;
48871 p->mFlags = mFlags;
48880 sqlite3_mutex_leave(db->mutex);
48890 int sz = pLower->szOsFile;
48915 ** This file implements an object that represents a fixed-length
48919 ** journalled during a transaction, or which pages have the "dont-write"
48948 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
48965 ** sub-dividing and re-hashing. */
48970 ** no fewer collisions than the no-op *1. */
48990 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap
48999 u32 nSet; /* Number of bits that are set - only valid for aHash
49023 p->iSize = iSize;
49029 ** Check to see if the i-th bit is set. Return true or false.
49035 i--;
49036 if( i>=p->iSize ) return 0;
49037 while( p->iDivisor ){
49038 u32 bin = i/p->iDivisor;
49039 i = i%p->iDivisor;
49040 p = p->u.apSub[bin];
49045 if( p->iSize<=BITVEC_NBIT ){
49046 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
49049 while( p->u.aHash[h] ){
49050 if( p->u.aHash[h]==i ) return 1;
49061 ** Set the i-th bit. Return 0 on success and an error code if
49064 ** This routine might cause sub-bitmaps to be allocated. Failing
49065 ** to get the memory needed to hold the sub-bitmap is the only
49076 assert( i<=p->iSize );
49077 i--;
49078 while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
49079 u32 bin = i/p->iDivisor;
49080 i = i%p->iDivisor;
49081 if( p->u.apSub[bin]==0 ){
49082 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
49083 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM_BKPT;
49085 p = p->u.apSub[bin];
49087 if( p->iSize<=BITVEC_NBIT ){
49088 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
49094 /* worring about sub-dividing and re-hashing. */
49095 if( !p->u.aHash[h] ){
49096 if (p->nSet<(BITVEC_NINT-1)) {
49105 if( p->u.aHash[h]==i ) return SQLITE_OK;
49108 } while( p->u.aHash[h] );
49113 if( p->nSet>=BITVEC_MXHASH ){
49116 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
49120 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
49121 memset(p->u.apSub, 0, sizeof(p->u.apSub));
49122 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
49132 p->nSet++;
49133 p->u.aHash[h] = i;
49138 ** Clear the i-th bit.
49146 i--;
49147 while( p->iDivisor ){
49148 u32 bin = i/p->iDivisor;
49149 i = i%p->iDivisor;
49150 p = p->u.apSub[bin];
49155 if( p->iSize<=BITVEC_NBIT ){
49156 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
49160 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
49161 memset(p->u.aHash, 0, sizeof(p->u.aHash));
49162 p->nSet = 0;
49165 u32 h = BITVEC_HASH(aiValues[j]-1);
49166 p->nSet++;
49167 while( p->u.aHash[h] ){
49171 p->u.aHash[h] = aiValues[j];
49182 if( p->iDivisor ){
49185 sqlite3BitvecDestroy(p->u.apSub[i]);
49196 return p->iSize;
49238 ** If a memory allocation error occurs, return -1.
49243 int rc = -1;
49266 i = aOp[pc+2] - 1;
49278 if( (--aOp[pc+1]) > 0 ) nx = 0;
49294 ** match (rc==0). Change rc to non-zero if a discrepancy
49299 + (sqlite3BitvecSize(pBitvec) - sz);
49345 ** such that p was added to the list more recently than p->pDirtyNext.
49350 ** page to eject from the cache mid-transaction. It is better to eject
49394 if( pCache->pCache==0 ) return;
49398 pLower = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, i, 0);
49400 pPg = (PgHdr*)pLower->pExtra;
49401 printf("%3d: nRef %2d flgs %02x data ", i, pPg->nRef, pPg->flags);
49402 a = (unsigned char *)pLower->pBuf;
49405 if( pPg->pPage==0 ){
49406 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, pLower, 0);
49428 assert( pPg->pgno>0 || pPg->pPager==0 ); /* Page number is 1 or more */
49429 pCache = pPg->pCache;
49431 if( pPg->flags & PGHDR_CLEAN ){
49432 assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
49433 assert( pCache->pDirty!=pPg ); /* CLEAN pages not on dirty list */
49434 assert( pCache->pDirtyTail!=pPg );
49437 if( pPg->flags & PGHDR_WRITEABLE ){
49438 assert( pPg->flags & PGHDR_DIRTY ); /* WRITEABLE implies DIRTY */
49476 PCache *p = pPage->pCache;
49480 pPage->pgno));
49482 assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
49483 assert( pPage->pDirtyPrev || pPage==p->pDirty );
49486 if( p->pSynced==pPage ){
49487 p->pSynced = pPage->pDirtyPrev;
49490 if( pPage->pDirtyNext ){
49491 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
49493 assert( pPage==p->pDirtyTail );
49494 p->pDirtyTail = pPage->pDirtyPrev;
49496 if( pPage->pDirtyPrev ){
49497 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
49503 assert( pPage==p->pDirty );
49504 p->pDirty = pPage->pDirtyNext;
49505 assert( p->bPurgeable || p->eCreate==2 );
49506 if( p->pDirty==0 ){ /*OPTIMIZATION-IF-TRUE*/
49507 assert( p->bPurgeable==0 || p->eCreate==1 );
49508 p->eCreate = 2;
49513 pPage->pDirtyPrev = 0;
49514 pPage->pDirtyNext = p->pDirty;
49515 if( pPage->pDirtyNext ){
49516 assert( pPage->pDirtyNext->pDirtyPrev==0 );
49517 pPage->pDirtyNext->pDirtyPrev = pPage;
49519 p->pDirtyTail = pPage;
49520 if( p->bPurgeable ){
49521 assert( p->eCreate==2 );
49522 p->eCreate = 1;
49525 p->pDirty = pPage;
49531 ** entries of the dirty-list for a page with NEED_SYNC clear anyway. */
49532 if( !p->pSynced
49533 && 0==(pPage->flags&PGHDR_NEED_SYNC) /*OPTIMIZATION-IF-FALSE*/
49535 p->pSynced = pPage;
49543 ** being used for an in-memory database, this function is a no-op.
49546 if( p->pCache->bPurgeable ){
49547 pcacheTrace(("%p.UNPIN %d\n", p->pCache, p->pgno));
49548 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
49549 pcacheDump(p->pCache);
49554 ** Compute the number of pages of cache requested. p->szCache is the
49558 if( p->szCache>=0 ){
49559 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the
49561 return p->szCache;
49563 /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
49567 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
49578 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
49579 ** built-in default page cache is used instead of the application defined
49588 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
49619 p->szPage = 1;
49620 p->szExtra = szExtra;
49622 p->bPurgeable = bPurgeable;
49623 p->eCreate = 2;
49624 p->xStress = xStress;
49625 p->pStress = pStress;
49626 p->szCache = 100;
49627 p->szSpill = 1;
49637 assert( pCache->nRefSum==0 && pCache->pDirty==0 );
49638 if( pCache->szPage ){
49641 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)),
49642 pCache->bPurgeable
49646 if( pCache->pCache ){
49647 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
49649 pCache->pCache = pNew;
49650 pCache->szPage = szPage;
49689 assert( pCache->pCache!=0 );
49691 assert( pCache->eCreate==((pCache->bPurgeable && pCache->pDirty) ? 1 : 2) );
49700 eCreate = createFlag & pCache->eCreate;
49702 assert( createFlag==0 || pCache->eCreate==eCreate );
49703 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
49704 pRes = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
49727 if( pCache->eCreate==2 ) return 0;
49729 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){
49730 /* Find a dirty page to write-out and recycle. First try to find a
49731 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
49739 for(pPg=pCache->pSynced;
49740 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
49741 pPg=pPg->pDirtyPrev
49743 pCache->pSynced = pPg;
49745 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
49751 "spill page %d making room for %d - cache used: %d/%d",
49752 pPg->pgno, pgno,
49753 sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
49756 pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
49757 rc = pCache->xStress(pCache->pStress, pPg);
49764 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
49784 pPgHdr = (PgHdr*)pPage->pExtra;
49785 assert( pPgHdr->pPage==0 );
49786 memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
49787 pPgHdr->pPage = pPage;
49788 pPgHdr->pData = pPage->pBuf;
49789 pPgHdr->pExtra = (void *)&pPgHdr[1];
49790 memset(pPgHdr->pExtra, 0, 8);
49791 pPgHdr->pCache = pCache;
49792 pPgHdr->pgno = pgno;
49793 pPgHdr->flags = PGHDR_CLEAN;
49811 pPgHdr = (PgHdr *)pPage->pExtra;
49813 if( !pPgHdr->pPage ){
49816 pCache->nRefSum++;
49817 pPgHdr->nRef++;
49827 assert( p->nRef>0 );
49828 p->pCache->nRefSum--;
49829 if( (--p->nRef)==0 ){
49830 if( p->flags&PGHDR_CLEAN ){
49842 assert(p->nRef>0);
49844 p->nRef++;
49845 p->pCache->nRefSum++;
49854 assert( p->nRef==1 );
49856 if( p->flags&PGHDR_DIRTY ){
49859 p->pCache->nRefSum--;
49860 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1);
49868 assert( p->nRef>0 );
49870 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ /*OPTIMIZATION-IF-FALSE*/
49871 p->flags &= ~PGHDR_DONT_WRITE;
49872 if( p->flags & PGHDR_CLEAN ){
49873 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN);
49874 pcacheTrace(("%p.DIRTY %d\n",p->pCache,p->pgno));
49875 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY );
49888 assert( (p->flags & PGHDR_DIRTY)!=0 );
49889 assert( (p->flags & PGHDR_CLEAN)==0 );
49891 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49892 p->flags |= PGHDR_CLEAN;
49893 pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
49895 if( p->nRef==0 ){
49905 pcacheTrace(("%p.CLEAN-ALL\n",pCache));
49906 while( (p = pCache->pDirty)!=0 ){
49916 pcacheTrace(("%p.CLEAR-WRITEABLE\n",pCache));
49917 for(p=pCache->pDirty; p; p=p->pDirtyNext){
49918 p->flags &= ~(PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
49920 pCache->pSynced = pCache->pDirtyTail;
49928 for(p=pCache->pDirty; p; p=p->pDirtyNext){
49929 p->flags &= ~PGHDR_NEED_SYNC;
49931 pCache->pSynced = pCache->pDirtyTail;
49938 PCache *pCache = p->pCache;
49939 assert( p->nRef>0 );
49942 pcacheTrace(("%p.MOVE %d -> %d\n",pCache,p->pgno,newPgno));
49943 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
49944 p->pgno = newPgno;
49945 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
49960 if( pCache->pCache ){
49964 for(p=pCache->pDirty; p; p=pNext){
49965 pNext = p->pDirtyNext;
49970 assert( p->pgno>0 );
49971 if( p->pgno>pgno ){
49972 assert( p->flags&PGHDR_DIRTY );
49976 if( pgno==0 && pCache->nRefSum ){
49978 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0);
49980 ** pCache->nRefSum>0 */
49981 memset(pPage1->pBuf, 0, pCache->szPage);
49985 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
49993 assert( pCache->pCache!=0 );
49995 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
50014 if( pA->pgno<pB->pgno ){
50015 pTail->pDirty = pA;
50017 pA = pA->pDirty;
50019 pTail->pDirty = pB;
50023 pTail->pDirty = pB;
50025 pB = pB->pDirty;
50027 pTail->pDirty = pA;
50052 pIn = p->pDirty;
50053 p->pDirty = 0;
50054 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
50063 if( NEVER(i==N_SORT_BUCKET-1) ){
50083 for(p=pCache->pDirty; p; p=p->pDirtyNext){
50084 p->pDirty = p->pDirtyNext;
50086 return pcacheSortDirtyList(pCache->pDirty);
50096 return pCache->nRefSum;
50103 return p->nRef;
50110 assert( pCache->pCache!=0 );
50111 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
50116 ** Get the suggested cache-size value.
50124 ** Set the suggested cache-size value.
50127 assert( pCache->pCache!=0 );
50128 pCache->szCache = mxPage;
50129 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
50134 ** Set the suggested cache-spill value. Make no changes if if the
50135 ** argument is zero. Return the effective cache-spill size, which will
50140 assert( p->pCache!=0 );
50143 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra));
50145 p->szSpill = mxPage;
50148 if( res<p->szSpill ) res = p->szSpill;
50156 assert( pCache->pCache!=0 );
50157 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
50162 ** in the page-cache hierarchy.
50174 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext) nDirty++;
50183 return (pCache->pDirty!=0);
50195 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
50223 ** -------------------------------------------------------------
50225 ** -------------------------------------------------------------
50241 ** size can vary according to architecture, compile-time options, and
50249 ** power of two, and leaving the rounded-up space unused.
50261 ** (1) The general-purpose memory allocator - sqlite3Malloc()
50262 ** (2) Global page-cache memory provided using sqlite3_config() with
50264 ** (3) PCache-local bulk allocation.
50274 ** Or if N is negative, then -1024*N bytes of memory are allocated and used
50278 ** (3) is exhausted, subsequent allocations fail over to the general-purpose
50295 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
50299 ** but causes a 2-byte gap in the structure for most architectures (since
50300 ** pointers must be either 4 or 8-byte aligned). As this structure is located
50302 ** corrupt, code at the b-tree layer may overread the page buffer and
50324 #define PAGE_IS_PINNED(p) ((p)->pLruNext==0)
50325 #define PAGE_IS_UNPINNED(p) ((p)->pLruNext!=0)
50353 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
50359 ** open database file (including each in-memory database and each
50374 unsigned int *pnPurgeable; /* Pointer to pGroup->nPurgeable */
50392 PgHdr1 *pFree; /* List of unused pcache-local pages */
50393 void *pBulk; /* Bulk memory used by pcache-local */
50427 ** reading because (1) most platforms read a 32-bit integer atomically and
50444 # define pcache1EnterMutex(X) assert((X)->mutex==0)
50445 # define pcache1LeaveMutex(X) assert((X)->mutex==0)
50448 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
50449 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
50459 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
50478 while( n-- ){
50480 p->pNext = pcache1.pFree;
50489 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
50490 ** true if pCache->pFree ends up containing one or more free pages.
50497 if( pCache->nMax<3 ) return 0;
50500 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
50502 szBulk = -1024 * (i64)pcache1.nInitPage;
50504 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
50505 szBulk = pCache->szAlloc*(i64)pCache->nMax;
50507 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
50510 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
50512 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
50513 pX->page.pBuf = zBulk;
50514 pX->page.pExtra = &pX[1];
50515 pX->isBulkLocal = 1;
50516 pX->isAnchor = 0;
50517 pX->pNext = pCache->pFree;
50518 pX->pLruPrev = 0; /* Initializing this saves a valgrind error */
50519 pCache->pFree = pX;
50520 zBulk += pCache->szAlloc;
50521 }while( --nBulk );
50523 return pCache->pFree!=0;
50542 pcache1.pFree = pcache1.pFree->pNext;
50543 pcache1.nFreeSlot--;
50580 pSlot->pNext = pcache1.pFree;
50627 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50628 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
50629 assert( pCache->pFree!=0 );
50630 p = pCache->pFree;
50631 pCache->pFree = p->pNext;
50632 p->pNext = 0;
50639 assert( pCache->pGroup==&pcache1.grp );
50640 pcache1LeaveMutex(pCache->pGroup);
50644 pPg = pcache1Alloc(pCache->szPage);
50645 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
50652 pPg = pcache1Alloc(pCache->szAlloc);
50656 pcache1EnterMutex(pCache->pGroup);
50660 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
50662 p->page.pBuf = pPg;
50663 p->page.pExtra = &p[1];
50664 p->isBulkLocal = 0;
50665 p->isAnchor = 0;
50666 p->pLruPrev = 0; /* Initializing this saves a valgrind error */
50668 (*pCache->pnPurgeable)++;
50678 pCache = p->pCache;
50679 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
50680 if( p->isBulkLocal ){
50681 p->pNext = pCache->pFree;
50682 pCache->pFree = p;
50684 pcache1Free(p->page.pBuf);
50689 (*pCache->pnPurgeable)--;
50727 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
50748 assert( sqlite3_mutex_held(p->pGroup->mutex) );
50750 nNew = p->nHash*2;
50755 pcache1LeaveMutex(p->pGroup);
50756 if( p->nHash ){ sqlite3BeginBenignMalloc(); }
50758 if( p->nHash ){ sqlite3EndBenignMalloc(); }
50759 pcache1EnterMutex(p->pGroup);
50761 for(i=0; i<p->nHash; i++){
50763 PgHdr1 *pNext = p->apHash[i];
50765 unsigned int h = pPage->iKey % nNew;
50766 pNext = pPage->pNext;
50767 pPage->pNext = apNew[h];
50771 sqlite3_free(p->apHash);
50772 p->apHash = apNew;
50773 p->nHash = nNew;
50780 ** LRU list, then this function is a no-op.
50787 assert( pPage->pLruNext );
50788 assert( pPage->pLruPrev );
50789 assert( sqlite3_mutex_held(pPage->pCache->pGroup->mutex) );
50790 pPage->pLruPrev->pLruNext = pPage->pLruNext;
50791 pPage->pLruNext->pLruPrev = pPage->pLruPrev;
50792 pPage->pLruNext = 0;
50793 /* pPage->pLruPrev = 0;
50795 assert( pPage->isAnchor==0 );
50796 assert( pPage->pCache->pGroup->lru.isAnchor==1 );
50797 pPage->pCache->nRecyclable--;
50811 PCache1 *pCache = pPage->pCache;
50814 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50815 h = pPage->iKey % pCache->nHash;
50816 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
50817 *pp = (*pp)->pNext;
50819 pCache->nPage--;
50828 PGroup *pGroup = pCache->pGroup;
50830 assert( sqlite3_mutex_held(pGroup->mutex) );
50831 while( pGroup->nPurgeable>pGroup->nMaxPage
50832 && (p=pGroup->lru.pLruPrev)->isAnchor==0
50834 assert( p->pCache->pGroup==pGroup );
50839 if( pCache->nPage==0 && pCache->pBulk ){
50840 sqlite3_free(pCache->pBulk);
50841 pCache->pBulk = pCache->pFree = 0;
50856 TESTONLY( int nPage = 0; ) /* To assert pCache->nPage is correct */
50858 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
50859 assert( pCache->iMaxKey >= iLimit );
50860 assert( pCache->nHash > 0 );
50861 if( pCache->iMaxKey - iLimit < pCache->nHash ){
50866 h = iLimit % pCache->nHash;
50867 iStop = pCache->iMaxKey % pCache->nHash;
50868 TESTONLY( nPage = -10; ) /* Disable the pCache->nPage validity check */
50872 h = pCache->nHash/2;
50873 iStop = h - 1;
50878 assert( h<pCache->nHash );
50879 pp = &pCache->apHash[h];
50881 if( pPage->iKey>=iLimit ){
50882 pCache->nPage--;
50883 *pp = pPage->pNext;
50887 pp = &pPage->pNext;
50892 h = (h+1) % pCache->nHash;
50894 assert( nPage<0 || pCache->nPage==(unsigned)nPage );
50911 ** private PGroup (mode-1). pcache1.separateCache is false if the single
50912 ** PGroup in pcache1.grp is used for all page caches (mode-2).
50914 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
50916 ** * Use a unified cache in single-threaded applications that have
50917 ** configured a start-time buffer for use as page-cache memory using
50918 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL
50921 ** * Otherwise use separate caches (mode-1)
50975 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
50983 pGroup->mxPinned = 10;
50988 if( pGroup->lru.isAnchor==0 ){
50989 pGroup->lru.isAnchor = 1;
50990 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru;
50992 pCache->pGroup = pGroup;
50993 pCache->szPage = szPage;
50994 pCache->szExtra = szExtra;
50995 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1));
50996 pCache->bPurgeable = (bPurgeable ? 1 : 0);
50999 pCache->nMin = 10;
51000 pGroup->nMinPage += pCache->nMin;
51001 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51002 pCache->pnPurgeable = &pGroup->nPurgeable;
51004 pCache->pnPurgeable = &pCache->nPurgeableDummy;
51007 if( pCache->nHash==0 ){
51022 if( pCache->bPurgeable ){
51023 PGroup *pGroup = pCache->pGroup;
51025 pGroup->nMaxPage += (nMax - pCache->nMax);
51026 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51027 pCache->nMax = nMax;
51028 pCache->n90pct = pCache->nMax*9/10;
51041 if( pCache->bPurgeable ){
51042 PGroup *pGroup = pCache->pGroup;
51045 savedMaxPage = pGroup->nMaxPage;
51046 pGroup->nMaxPage = 0;
51048 pGroup->nMaxPage = savedMaxPage;
51059 pcache1EnterMutex(pCache->pGroup);
51060 n = pCache->nPage;
51061 pcache1LeaveMutex(pCache->pGroup);
51080 PGroup *pGroup = pCache->pGroup;
51084 assert( pCache->nPage >= pCache->nRecyclable );
51085 nPinned = pCache->nPage - pCache->nRecyclable;
51086 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
51087 assert( pCache->n90pct == pCache->nMax*9/10 );
51089 nPinned>=pGroup->mxPinned
51090 || nPinned>=pCache->n90pct
51091 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned)
51096 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache);
51097 assert( pCache->nHash>0 && pCache->apHash );
51100 if( pCache->bPurgeable
51101 && !pGroup->lru.pLruPrev->isAnchor
51102 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache))
51105 pPage = pGroup->lru.pLruPrev;
51109 pOther = pPage->pCache;
51110 if( pOther->szAlloc != pCache->szAlloc ){
51114 pGroup->nPurgeable -= (pOther->bPurgeable - pCache->bPurgeable);
51126 unsigned int h = iKey % pCache->nHash;
51127 pCache->nPage++;
51128 pPage->iKey = iKey;
51129 pPage->pNext = pCache->apHash[h];
51130 pPage->pCache = pCache;
51131 pPage->pLruNext = 0;
51132 /* pPage->pLruPrev = 0;
51134 *(void **)pPage->page.pExtra = 0;
51135 pCache->apHash[h] = pPage;
51136 if( iKey>pCache->iMaxKey ){
51137 pCache->iMaxKey = iKey;
51153 ** For a non-purgeable cache (a cache used as the storage for an in-memory
51156 ** a non-purgeable cache.
51199 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper
51211 pPage = pCache->apHash[iKey % pCache->nHash];
51212 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; }
51240 pcache1EnterMutex(pCache->pGroup);
51242 assert( pPage==0 || pCache->iMaxKey>=iKey );
51243 pcache1LeaveMutex(pCache->pGroup);
51257 assert( pCache->bPurgeable || createFlag!=1 );
51258 assert( pCache->bPurgeable || pCache->nMin==0 );
51259 assert( pCache->bPurgeable==0 || pCache->nMin==10 );
51260 assert( pCache->nMin==0 || pCache->bPurgeable );
51261 assert( pCache->nHash>0 );
51263 if( pCache->pGroup->mutex ){
51285 PGroup *pGroup = pCache->pGroup;
51287 assert( pPage->pCache==pCache );
51293 assert( pPage->pLruNext==0 );
51296 if( reuseUnlikely || pGroup->nPurgeable>pGroup->nMaxPage ){
51300 PgHdr1 **ppFirst = &pGroup->lru.pLruNext;
51301 pPage->pLruPrev = &pGroup->lru;
51302 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage;
51304 pCache->nRecyclable++;
51307 pcache1LeaveMutex(pCache->pGroup);
51323 assert( pPage->iKey==iOld );
51324 assert( pPage->pCache==pCache );
51326 pcache1EnterMutex(pCache->pGroup);
51328 h = iOld%pCache->nHash;
51329 pp = &pCache->apHash[h];
51331 pp = &(*pp)->pNext;
51333 *pp = pPage->pNext;
51335 h = iNew%pCache->nHash;
51336 pPage->iKey = iNew;
51337 pPage->pNext = pCache->apHash[h];
51338 pCache->apHash[h] = pPage;
51339 if( iNew>pCache->iMaxKey ){
51340 pCache->iMaxKey = iNew;
51343 pcache1LeaveMutex(pCache->pGroup);
51355 pcache1EnterMutex(pCache->pGroup);
51356 if( iLimit<=pCache->iMaxKey ){
51358 pCache->iMaxKey = iLimit-1;
51360 pcache1LeaveMutex(pCache->pGroup);
51370 PGroup *pGroup = pCache->pGroup;
51371 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
51373 if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
51374 assert( pGroup->nMaxPage >= pCache->nMax );
51375 pGroup->nMaxPage -= pCache->nMax;
51376 assert( pGroup->nMinPage >= pCache->nMin );
51377 pGroup->nMinPage -= pCache->nMin;
51378 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
51381 sqlite3_free(pCache->pBulk);
51382 sqlite3_free(pCache->apHash);
51442 && p->isAnchor==0
51444 nFree += pcache1MemSize(p->page.pBuf);
51471 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){
51531 ** a non-zero batch number, it will see all prior INSERTs.
51559 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
51617 p->pChunk = 0;
51618 p->db = db;
51619 p->pEntry = 0;
51620 p->pLast = 0;
51621 p->pForest = 0;
51622 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
51623 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
51624 p->rsFlags = ROWSET_SORTED;
51625 p->iBatch = 0;
51638 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
51639 pNextChunk = pChunk->pNextChunk;
51640 sqlite3DbFree(p->db, pChunk);
51642 p->pChunk = 0;
51643 p->nFresh = 0;
51644 p->pEntry = 0;
51645 p->pLast = 0;
51646 p->pForest = 0;
51647 p->rsFlags = ROWSET_SORTED;
51657 sqlite3DbFree(((RowSet*)pArg)->db, pArg);
51665 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
51670 if( p->nFresh==0 ){ /*OPTIMIZATION-IF-FALSE*/
51674 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
51678 pNew->pNextChunk = p->pChunk;
51679 p->pChunk = pNew;
51680 p->pFresh = pNew->aEntry;
51681 p->nFresh = ROWSET_ENTRY_PER_CHUNK;
51683 p->nFresh--;
51684 return p->pFresh++;
51698 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
51702 pEntry->v = rowid;
51703 pEntry->pRight = 0;
51704 pLast = p->pLast;
51706 if( rowid<=pLast->v ){ /*OPTIMIZATION-IF-FALSE*/
51709 p->rsFlags &= ~ROWSET_SORTED;
51711 pLast->pRight = pEntry;
51713 p->pEntry = pEntry;
51715 p->pLast = pEntry;
51734 assert( pA->pRight==0 || pA->v<=pA->pRight->v );
51735 assert( pB->pRight==0 || pB->v<=pB->pRight->v );
51736 if( pA->v<=pB->v ){
51737 if( pA->v<pB->v ) pTail = pTail->pRight = pA;
51738 pA = pA->pRight;
51740 pTail->pRight = pB;
51744 pTail = pTail->pRight = pB;
51745 pB = pB->pRight;
51747 pTail->pRight = pA;
51765 pNext = pIn->pRight;
51766 pIn->pRight = 0;
51794 if( pIn->pLeft ){
51796 rowSetTreeToList(pIn->pLeft, ppFirst, &p);
51797 p->pRight = pIn;
51801 if( pIn->pRight ){
51802 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
51806 assert( (*ppLast)->pRight==0 );
51829 if( *ppList==0 ){ /*OPTIMIZATION-IF-TRUE*/
51833 if( iDepth>1 ){ /*OPTIMIZATION-IF-TRUE*/
51837 pLeft = rowSetNDeepTree(ppList, iDepth-1);
51839 if( p==0 ){ /*OPTIMIZATION-IF-FALSE*/
51844 p->pLeft = pLeft;
51845 *ppList = p->pRight;
51846 p->pRight = rowSetNDeepTree(ppList, iDepth-1);
51849 *ppList = p->pRight;
51850 p->pLeft = p->pRight = 0;
51866 pList = p->pRight;
51867 p->pLeft = p->pRight = 0;
51871 pList = p->pRight;
51872 p->pLeft = pLeft;
51873 p->pRight = rowSetNDeepTree(&pList, iDepth);
51893 assert( p->pForest==0 ); /* Cannot be used with sqlite3RowSetText() */
51896 if( (p->rsFlags & ROWSET_NEXT)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51897 if( (p->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51898 p->pEntry = rowSetEntrySort(p->pEntry);
51900 p->rsFlags |= ROWSET_SORTED|ROWSET_NEXT;
51904 if( p->pEntry ){
51905 *pRowid = p->pEntry->v;
51906 p->pEntry = p->pEntry->pRight;
51907 if( p->pEntry==0 ){ /*OPTIMIZATION-IF-TRUE*/
51922 ** on pRowSet->pEntry, then sort those entries into the forest at
51923 ** pRowSet->pForest so that they can be tested.
51929 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
51934 if( iBatch!=pRowSet->iBatch ){ /*OPTIMIZATION-IF-FALSE*/
51935 p = pRowSet->pEntry;
51937 struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
51938 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ /*OPTIMIZATION-IF-FALSE*/
51942 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51943 ppPrevTree = &pTree->pRight;
51944 if( pTree->pLeft==0 ){
51945 pTree->pLeft = rowSetListToTree(p);
51949 rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
51950 pTree->pLeft = 0;
51957 pTree->v = 0;
51958 pTree->pRight = 0;
51959 pTree->pLeft = rowSetListToTree(p);
51962 pRowSet->pEntry = 0;
51963 pRowSet->pLast = 0;
51964 pRowSet->rsFlags |= ROWSET_SORTED;
51966 pRowSet->iBatch = iBatch;
51972 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
51973 p = pTree->pLeft;
51975 if( p->v<iRowid ){
51976 p = p->pRight;
51977 }else if( p->v>iRowid ){
51978 p = p->pLeft;
52024 ** This header file defines the interface to the write-ahead logging
52064 /* Connection to a write-ahead log (WAL) file.
52069 /* Open and close a connection to a write-ahead log. */
52077 ** snapshot is like a read-transaction. It is the state of the database
52086 /* Read a page from the write-ahead log, if it is present. */
52113 Wal *pWal, /* Write-ahead log connection */
52137 /* Return true if the argument is non-NULL and the WAL module is using
52138 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
52139 ** WAL module is using shared-memory, return false.
52153 ** stored in each frame (i.e. the db page-size when the WAL was created).
52220 ** (6) If a super-journal file is used, then all writes to the database file
52221 ** are synced prior to the super-journal being deleted.
52237 ** method is a no-op, but that does not change the fact the SQLite will
52248 ** (11) A database file is well-formed at the beginning and at the conclusion
52272 ** to print out file-descriptors.
52275 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
52278 #define PAGERID(p) (SQLITE_PTR_TO_INT(p->fd))
52286 ** OPEN <------+------+
52289 ** +---------> READER-------+ |
52292 ** |<-------WRITER_LOCKED------> ERROR
52295 ** |<------WRITER_CACHEMOD-------->|
52298 ** |<-------WRITER_DBMOD---------->|
52301 ** +<------WRITER_FINISHED-------->+
52306 ** OPEN -> READER [sqlite3PagerSharedLock]
52307 ** READER -> OPEN [pager_unlock]
52309 ** READER -> WRITER_LOCKED [sqlite3PagerBegin]
52310 ** WRITER_LOCKED -> WRITER_CACHEMOD [pager_open_journal]
52311 ** WRITER_CACHEMOD -> WRITER_DBMOD [syncJournal]
52312 ** WRITER_DBMOD -> WRITER_FINISHED [sqlite3PagerCommitPhaseOne]
52313 ** WRITER_*** -> READER [pager_end_transaction]
52315 ** WRITER_*** -> ERROR [pager_error]
52316 ** ERROR -> OPEN [pager_unlock]
52322 ** state - the file may or may not be locked and the database size is
52332 ** rollback (non-WAL) mode are met. Unless the pager is (or recently
52333 ** was) in exclusive-locking mode, a user-level read transaction is
52337 ** it opens a read-transaction on the database and returns to state
52338 ** OPEN after the read-transaction is completed. However a connection
52340 ** this state even after the read-transaction is closed. The only way
52344 ** * A read transaction may be active (but a write-transaction cannot).
52346 ** * The dbSize variable may be trusted (even if a user-level read
52350 ** * Even if a read-transaction is not open, it is guaranteed that
52351 ** there is no hot-journal in the file-system.
52355 ** The pager moves to this state from READER when a write-transaction
52357 ** required to start a write-transaction are held, but no actual
52372 ** * If the connection is open in rollback-mode, a RESERVED or greater
52374 ** * If the connection is open in WAL-mode, a WAL write transaction
52413 ** A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
52429 ** The ERROR state is entered when an IO or disk-full error (including
52431 ** difficult to be sure that the in-memory pager state (cache contents,
52432 ** db size etc.) are consistent with the contents of the file-system.
52434 ** Temporary pager files may enter the ERROR state, but in-memory pagers
52438 ** the contents of the page-cache may be left in an inconsistent state.
52450 ** page-cache and any other in-memory state at the same time. Everything
52451 ** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
52452 ** when a read-transaction is next opened on the pager (transitioning
52468 ** In other cases, the error is returned to the b-tree layer. The b-tree
52472 ** Condition (3) is necessary because it can be triggered by a read-only
52474 ** code were simply returned to the user, the b-tree layer would not
52476 ** read-only statement cannot leave the pager in an internally inconsistent
52482 ** * The pager is not an in-memory pager.
52492 ** state. There are two exceptions: immediately after exclusive-mode has
52508 ** following locking-states, according to the lock currently held on
52516 ** pagerUnlockDb() take a conservative approach - eLock is always updated
52527 ** from ERROR to OPEN state. At this point there may be a hot-journal file
52528 ** in the file-system that needs to be rolled back (as part of an OPEN->SHARED
52532 ** of hot-journal detection.
52538 ** a hot-journal may be mistaken for a journal being created by an active
52545 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
52546 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
52547 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
52572 ** set to 0. If a journal-header is written into the main journal while
52575 ** journal before the journal-header. This is required during savepoint
52584 Pgno iSubRec; /* Index of first record in sub-journal */
52609 ** For a real on-disk database, the current lock held on the database file -
52612 ** For a temporary or in-memory database (neither of which require any
52624 ** This boolean variable is used to make sure that the change-counter
52625 ** (the 4-byte header field at byte offset 24 of the database file) is
52628 ** It is set to true when the change-counter field is updated, which
52633 ** updating the change-counter is omitted for the current transaction.
52636 ** need only update the change-counter once, for the first transaction
52642 ** (or may not) specify a super-journal name to be written into the
52645 ** Whether or not a journal file contains a super-journal pointer affects
52648 ** If a journal file does not contain a super-journal pointer, it is
52650 ** it does contain a super-journal pointer the journal file is finalized
52654 ** Journal files that contain super-journal pointers cannot be finalized
52655 ** simply by overwriting the first journal-header with zeroes, as the
52656 ** super-journal pointer could interfere with hot-journal rollback of any
52666 ** This variables control the behavior of cache-spills (calls made by
52668 ** to the file-system in order to free up memory).
52680 ** This flag is set by sqlite3PagerWrite() when the file-system sector-size
52681 ** is larger than the database page-size in order to prevent a journal sync
52686 ** This is a boolean variable. If true, then any required sub-journal
52687 ** is opened as an in-memory journal file. If false, then in-memory
52688 ** sub-journals are only used for in-memory pager files.
52691 ** write-transaction is opened.
52702 ** is not an integer multiple of the page-size, the value stored in
52703 ** dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
52705 ** to have at least one page. (i.e. a 1KB file with 2K page-size leads
52708 ** During a write-transaction, if pages with page-numbers greater than
52719 ** Throughout a write-transaction, dbFileSize contains the size of
52721 ** write-transaction is first opened, and updated when VFS calls are made
52741 ** write-transaction is opened (at the same time as dbFileSize and
52744 ** size-hint passed to the method call. See pager_write_pagelist() for
52752 ** sub-codes.
52778 u8 readOnly; /* True for a read-only database */
52792 u8 changeCountDone; /* Set after incrementing the change-counter */
52793 u8 setSuper; /* Super-jrnl name is written into jrnl */
52794 u8 doNotSpill; /* Do not spill the cache when non-zero */
52795 u8 subjInMemory; /* True to use in-memory sub-journals */
52803 int nRec; /* Pages journalled since last j-header written */
52804 u32 cksumInit; /* Quasi-random value added to every checksum */
52805 u32 nSubRec; /* Number of records written to sub-journal */
52809 sqlite3_file *sjfd; /* File descriptor for sub-journal */
52822 ** End of the routinely-changing class members
52825 u16 nExtra; /* Add this many bytes to each in-memory page */
52845 Wal *pWal; /* Write-ahead log used by "journal_mode=wal" */
52846 char *zWal; /* File name for write-ahead log */
52863 ** a non-testing build. These variables are not thread-safe.
52882 ** written, semi-random garbage data might appear in the journal
52889 ** of a 32-bit checksum on each page of data. The checksum covers both
52890 ** the page number and the pPager->pageSize bytes of data for the page.
52891 ** This cksum is initialized to a 32-bit random value that appears in the
52907 #define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
52913 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
52916 ** The macro MEMDB is true if we are dealing with an in-memory database.
52924 # define MEMDB pPager->memDb
52929 ** interfaces to access the database using memory-mapped I/O.
52932 # define USEFETCH(x) ((x)->bUseFetch)
52939 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
52943 ** if( isOpen(pPager->jfd) ){ ...
52947 ** if( pPager->jfd->pMethods ){ ...
52949 #define isOpen(pFd) ((pFd)->pMethods!=0)
52954 ** by the b-tree layer. This is the case if:
52961 if( pPager->fd->pMethods==0 ) return 0;
52962 if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;
52964 if( pPager->pWal ){
52967 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
52976 # define pagerUseWal(x) ((x)->pWal!=0)
52998 assert( p->eState==PAGER_OPEN
52999 || p->eState==PAGER_READER
53000 || p->eState==PAGER_WRITER_LOCKED
53001 || p->eState==PAGER_WRITER_CACHEMOD
53002 || p->eState==PAGER_WRITER_DBMOD
53003 || p->eState==PAGER_WRITER_FINISHED
53004 || p->eState==PAGER_ERROR
53007 /* Regardless of the current state, a temp-file connection always behaves
53009 ** the change-counter field, so the changeCountDone flag is always set.
53011 assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
53012 assert( p->tempFile==0 || pPager->changeCountDone );
53014 /* If the useJournal flag is clear, the journal-mode must be "OFF".
53015 ** And if the journal-mode is "OFF", the journal file must not be open.
53017 assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
53018 assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
53020 /* Check that MEMDB implies noSync. And an in-memory journal. Since
53021 ** this means an in-memory pager performs no IO at all, it cannot encounter
53023 ** a journal file. (although the in-memory journal implementation may
53025 ** is therefore not possible for an in-memory pager to enter the ERROR
53029 assert( !isOpen(p->fd) );
53030 assert( p->noSync );
53031 assert( p->journalMode==PAGER_JOURNALMODE_OFF
53032 || p->journalMode==PAGER_JOURNALMODE_MEMORY
53034 assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
53041 assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
53042 assert( p->eLock!=PENDING_LOCK );
53044 switch( p->eState ){
53047 assert( pPager->errCode==SQLITE_OK );
53048 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
53052 assert( pPager->errCode==SQLITE_OK );
53053 assert( p->eLock!=UNKNOWN_LOCK );
53054 assert( p->eLock>=SHARED_LOCK );
53058 assert( p->eLock!=UNKNOWN_LOCK );
53059 assert( pPager->errCode==SQLITE_OK );
53061 assert( p->eLock>=RESERVED_LOCK );
53063 assert( pPager->dbSize==pPager->dbOrigSize );
53064 assert( pPager->dbOrigSize==pPager->dbFileSize );
53065 assert( pPager->dbOrigSize==pPager->dbHintSize );
53066 assert( pPager->setSuper==0 );
53070 assert( p->eLock!=UNKNOWN_LOCK );
53071 assert( pPager->errCode==SQLITE_OK );
53078 assert( p->eLock>=RESERVED_LOCK );
53079 assert( isOpen(p->jfd)
53080 || p->journalMode==PAGER_JOURNALMODE_OFF
53081 || p->journalMode==PAGER_JOURNALMODE_WAL
53084 assert( pPager->dbOrigSize==pPager->dbFileSize );
53085 assert( pPager->dbOrigSize==pPager->dbHintSize );
53089 assert( p->eLock==EXCLUSIVE_LOCK );
53090 assert( pPager->errCode==SQLITE_OK );
53092 assert( p->eLock>=EXCLUSIVE_LOCK );
53093 assert( isOpen(p->jfd)
53094 || p->journalMode==PAGER_JOURNALMODE_OFF
53095 || p->journalMode==PAGER_JOURNALMODE_WAL
53096 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53098 assert( pPager->dbOrigSize<=pPager->dbHintSize );
53102 assert( p->eLock==EXCLUSIVE_LOCK );
53103 assert( pPager->errCode==SQLITE_OK );
53105 assert( isOpen(p->jfd)
53106 || p->journalMode==PAGER_JOURNALMODE_OFF
53107 || p->journalMode==PAGER_JOURNALMODE_WAL
53108 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
53117 assert( pPager->errCode!=SQLITE_OK );
53118 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 || pPager->tempFile );
53151 , p->zFilename
53152 , p->eState==PAGER_OPEN ? "OPEN" :
53153 p->eState==PAGER_READER ? "READER" :
53154 p->eState==PAGER_WRITER_LOCKED ? "WRITER_LOCKED" :
53155 p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
53156 p->eState==PAGER_WRITER_DBMOD ? "WRITER_DBMOD" :
53157 p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
53158 p->eState==PAGER_ERROR ? "ERROR" : "?error?"
53159 , (int)p->errCode
53160 , p->eLock==NO_LOCK ? "NO_LOCK" :
53161 p->eLock==RESERVED_LOCK ? "RESERVED" :
53162 p->eLock==EXCLUSIVE_LOCK ? "EXCLUSIVE" :
53163 p->eLock==SHARED_LOCK ? "SHARED" :
53164 p->eLock==UNKNOWN_LOCK ? "UNKNOWN" : "?error?"
53165 , p->exclusiveMode ? "exclusive" : "normal"
53166 , p->journalMode==PAGER_JOURNALMODE_MEMORY ? "memory" :
53167 p->journalMode==PAGER_JOURNALMODE_OFF ? "off" :
53168 p->journalMode==PAGER_JOURNALMODE_DELETE ? "delete" :
53169 p->journalMode==PAGER_JOURNALMODE_PERSIST ? "persist" :
53170 p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
53171 p->journalMode==PAGER_JOURNALMODE_WAL ? "wal" : "?error?"
53172 , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
53173 , p->journalOff, p->journalHdr
53174 , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
53193 if( pPager->errCode ){
53194 pPager->xGet = getPageError;
53197 pPager->xGet = getPageMMap;
53200 pPager->xGet = getPageNormal;
53205 ** Return true if it is necessary to write page *pPg into the sub-journal.
53206 ** A page needs to be written into the sub-journal if there exists one
53209 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
53210 ** * The bit corresponding to the page-number is not set in
53214 Pager *pPager = pPg->pPager;
53216 Pgno pgno = pPg->pgno;
53218 for(i=0; i<pPager->nSavepoint; i++){
53219 p = &pPager->aSavepoint[i];
53220 if( p->nOrig>=pgno && 0==sqlite3BitvecTestNotNull(p->pInSavepoint, pgno) ){
53221 for(i=i+1; i<pPager->nSavepoint; i++){
53222 pPager->aSavepoint[i].bTruncateOnRelease = 0;
53235 return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
53240 ** Read a 32-bit integer from the given file descriptor. Store the integer
53244 ** All values are stored on disk as big-endian.
53256 ** Write a 32-bit integer into a string buffer in big-endian byte order.
53262 ** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
53283 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
53286 if( isOpen(pPager->fd) ){
53287 assert( pPager->eLock>=eLock );
53288 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
53289 if( pPager->eLock!=UNKNOWN_LOCK ){
53290 pPager->eLock = (u8)eLock;
53294 pPager->changeCountDone = pPager->tempFile; /* ticket fb3b3024ea238d5c */
53312 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
53313 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
53314 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
53315 pPager->eLock = (u8)eLock;
53323 ** This function determines whether or not the atomic-write or
53324 ** atomic-batch-write optimizations can be used with this pager. The
53325 ** atomic-write optimization can be used if:
53335 ** The atomic-batch-write optimization can be used if OsDeviceCharacteristics()
53336 ** returns a value with the SQLITE_IOCAP_BATCH_ATOMIC bit set. -1 is
53348 assert( isOpen(pPager->fd) );
53349 dc = sqlite3OsDeviceCharacteristics(pPager->fd);
53355 if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
53356 return -1;
53362 int nSector = pPager->sectorSize;
53363 int szPage = pPager->pageSize;
53385 ** Return a 32-bit hash of the page data for pPage.
53396 return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
53399 pPage->pageHash = pager_pagehash(pPage);
53405 ** that the page is either dirty or still matches the calculated page-hash.
53409 Pager *pPager = pPg->pPager;
53410 assert( pPager->eState!=PAGER_ERROR );
53411 assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
53423 ** This function attempts to read a super-journal file name from the
53426 ** used to store a super-journal file name at the end of a journal file.
53430 ** enough space to write the super-journal name). If the super-journal
53432 ** nul-terminator), then this is handled as if no super-journal name
53435 ** If a super-journal file name is present at the end of the journal
53437 ** nul-terminator byte is appended to the buffer following the
53438 ** super-journal file name.
53440 ** If it is determined that no super-journal file name is present
53448 u32 len; /* Length in bytes of super-journal name */
53457 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
53459 || len>szJ-16
53461 || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
53462 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
53464 || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zSuper, len, szJ-16-len))
53469 /* See if the checksum matches the super-journal name */
53471 cksum -= zSuper[u];
53475 ** containing the super-journal filename is corrupted. This means
53477 ** super-journal filename.
53489 ** following the value in pPager->journalOff, assuming a sector
53490 ** size of pPager->sectorSize bytes.
53495 ** ---------------------------------------
53504 i64 c = pPager->journalOff;
53506 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
53510 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
53517 ** This function is a no-op if the journal file has not been written to
53520 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
53522 ** zero the 28-byte header at the start of the journal file. In either case,
53523 ** if the pager is not in no-sync mode, sync the journal file immediately
53526 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
53537 assert( isOpen(pPager->jfd) );
53538 assert( !sqlite3JournalIsInMemory(pPager->jfd) );
53539 if( pPager->journalOff ){
53540 const i64 iLimit = pPager->journalSizeLimit; /* Local cache of jsl */
53544 rc = sqlite3OsTruncate(pPager->jfd, 0);
53547 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
53549 if( rc==SQLITE_OK && !pPager->noSync ){
53550 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
53561 rc = sqlite3OsFileSize(pPager->jfd, &sz);
53563 rc = sqlite3OsTruncate(pPager->jfd, iLimit);
53576 ** - 8 bytes: Magic identifying journal format.
53577 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
53578 ** - 4 bytes: Random number used for page hash.
53579 ** - 4 bytes: Initial database page count.
53580 ** - 4 bytes: Sector size used by the process that wrote this journal.
53581 ** - 4 bytes: Database page size.
53583 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
53587 char *zHeader = pPager->pTmpSpace; /* Temporary space used to build header */
53588 u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
53592 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
53602 for(ii=0; ii<pPager->nSavepoint; ii++){
53603 if( pPager->aSavepoint[ii].iHdrOffset==0 ){
53604 pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
53608 pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
53611 ** Write the nRec Field - the number of page records that follow this
53614 ** if in full-sync mode), the zero is overwritten with the true number
53624 ** * When the pager is in no-sync mode. Corruption can follow a
53630 assert( isOpen(pPager->fd) || pPager->noSync );
53631 if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
53632 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
53640 /* The random check-hash initializer */
53641 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
53642 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
53644 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
53646 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
53649 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
53657 nHeader-(sizeof(aJournalMagic)+20));
53668 ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
53671 ** The loop is required here in case the sector-size is larger than the
53677 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
53678 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
53679 assert( pPager->journalHdr <= pPager->journalOff );
53680 pPager->journalOff += nHeader;
53690 ** pPager->journalOff. See comments above function writeJournalHdr() for
53695 ** database before the transaction began, in pages. Also, pPager->cksumInit
53714 assert( isOpen(pPager->jfd) ); /* Journal file must be open. */
53720 pPager->journalOff = journalHdrOffset(pPager);
53721 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
53724 iHdrOff = pPager->journalOff;
53731 if( isHot || iHdrOff!=pPager->journalHdr ){
53732 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
53741 /* Read the first three 32-bit fields of the journal header: The nRec
53742 ** field, the checksum-initializer and the database size at the start
53745 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
53746 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
53747 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
53752 if( pPager->journalOff==0 ){
53753 u32 iPageSize; /* Page-size field of journal header */
53754 u32 iSectorSize; /* Sector-size field of journal header */
53756 /* Read the page-size and sector-size journal header fields. */
53757 if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
53758 || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
53763 /* Versions of SQLite prior to 3.5.8 set the page-size field of the
53768 iPageSize = pPager->pageSize;
53771 /* Check that the values read from the page-size and sector-size fields
53778 || ((iPageSize-1)&iPageSize)!=0 || ((iSectorSize-1)&iSectorSize)!=0
53780 /* If the either the page-size or sector-size in the journal-header is
53781 ** invalid, then the process that wrote the journal-header must have
53788 /* Update the page-size to match the value read from the journal.
53792 rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
53795 /* Update the assumed sector-size to match the value used by
53801 pPager->sectorSize = iSectorSize;
53804 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
53810 ** Write the supplied super-journal name into the journal file for pager
53811 ** pPager at the current location. The super-journal name must be the last
53812 ** thing written to a journal file. If the pager is in full-sync mode, the
53817 ** + N bytes: super-journal filename in utf-8.
53818 ** + 4 bytes: N (length of super-journal name in bytes, no nul-terminator).
53819 ** + 4 bytes: super-journal name checksum.
53822 ** The super-journal page checksum is the sum of the bytes in thesuper-journal
53823 ** name, where each byte is interpreted as a signed 8-bit integer.
53826 ** this call is a no-op.
53835 assert( pPager->setSuper==0 );
53839 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
53840 || !isOpen(pPager->jfd)
53844 pPager->setSuper = 1;
53845 assert( pPager->journalHdr <= pPager->journalOff );
53852 /* If in full-sync mode, advance to the next disk sector before writing
53853 ** the super-journal name. This is in case the previous page written to
53856 if( pPager->fullSync ){
53857 pPager->journalOff = journalHdrOffset(pPager);
53859 iHdrOff = pPager->journalOff;
53861 /* Write the super-journal data to the end of the journal file. If
53864 if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
53865 || (0 != (rc = sqlite3OsWrite(pPager->jfd, zSuper, nSuper, iHdrOff+4)))
53866 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper, nSuper)))
53867 || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nSuper+4, cksum)))
53868 || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8,
53873 pPager->journalOff += (nSuper+20);
53875 /* If the pager is in peristent-journal mode, then the physical
53876 ** journal-file may extend past the end of the super-journal name
53878 ** dangerous because the code to rollback a hot-journal file
53879 ** will not be able to find the super-journal name to determine
53885 if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
53886 && jrnlSize>pPager->journalOff
53888 rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
53894 ** Discard the entire contents of the in-memory page-cache.
53897 pPager->iDataVersion++;
53898 sqlite3BackupRestart(pPager->pBackup);
53899 sqlite3PcacheClear(pPager->pPCache);
53903 ** Return the pPager->iDataVersion value
53906 return pPager->iDataVersion;
53911 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
53916 for(ii=0; ii<pPager->nSavepoint; ii++){
53917 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
53919 if( !pPager->exclusiveMode || sqlite3JournalIsInMemory(pPager->sjfd) ){
53920 sqlite3OsClose(pPager->sjfd);
53922 sqlite3_free(pPager->aSavepoint);
53923 pPager->aSavepoint = 0;
53924 pPager->nSavepoint = 0;
53925 pPager->nSubRec = 0;
53937 for(ii=0; ii<pPager->nSavepoint; ii++){
53938 PagerSavepoint *p = &pPager->aSavepoint[ii];
53939 if( pgno<=p->nOrig ){
53940 rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
53949 ** This function is a no-op if the pager is in exclusive mode and not
53953 ** If the pager is not in exclusive-access mode, the database file is
53954 ** completely unlocked. If the file is unlocked and the file-system does
53960 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
53961 ** or not, any journal file left in the file-system will be treated
53962 ** as a hot-journal and rolled back the next time a read-transaction
53967 assert( pPager->eState==PAGER_READER
53968 || pPager->eState==PAGER_OPEN
53969 || pPager->eState==PAGER_ERROR
53972 sqlite3BitvecDestroy(pPager->pInJournal);
53973 pPager->pInJournal = 0;
53977 assert( !isOpen(pPager->jfd) );
53978 sqlite3WalEndReadTransaction(pPager->pWal);
53979 pPager->eState = PAGER_OPEN;
53980 }else if( !pPager->exclusiveMode ){
53982 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
53996 || 1!=(pPager->journalMode & 5)
53998 sqlite3OsClose(pPager->jfd);
54007 if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
54008 pPager->eLock = UNKNOWN_LOCK;
54012 ** without clearing the error code. This is intentional - the error
54015 assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
54016 pPager->eState = PAGER_OPEN;
54022 ** normal and exclusive-locking mode.
54024 assert( pPager->errCode==SQLITE_OK || !MEMDB );
54025 if( pPager->errCode ){
54026 if( pPager->tempFile==0 ){
54028 pPager->changeCountDone = 0;
54029 pPager->eState = PAGER_OPEN;
54031 pPager->eState = (isOpen(pPager->jfd) ? PAGER_OPEN : PAGER_READER);
54033 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
54034 pPager->errCode = SQLITE_OK;
54038 pPager->journalOff = 0;
54039 pPager->journalHdr = 0;
54040 pPager->setSuper = 0;
54047 ** the error-code about to be returned by a pager API function. The
54051 ** IOERR sub-codes, the pager enters the ERROR state and the error code
54055 ** The ERROR state indicates that the contents of the pager-cache
54057 ** the contents of the pager-cache. If a transaction was active when
54060 ** it were a hot-journal).
54066 pPager->errCode==SQLITE_FULL ||
54067 pPager->errCode==SQLITE_OK ||
54068 (pPager->errCode & 0xff)==SQLITE_IOERR
54071 pPager->errCode = rc;
54072 pPager->eState = PAGER_ERROR;
54088 ** * For non-TEMP databases, always sync to disk. This is necessary
54097 if( pPager->tempFile==0 ) return 1;
54099 if( !isOpen(pPager->fd) ) return 0;
54100 return (sqlite3PCachePercentDirty(pPager->pPCache)>=25);
54106 ** after rollback of a hot-journal, or if an error occurs while opening
54107 ** the journal file or writing the very first journal-header of a
54112 ** exclusive than a RESERVED lock, it is a no-op.
54118 ** transaction. Nor will it be considered to be a hot-journal by this
54121 ** the current journal-mode (Pager.journalMode value), as follows:
54125 ** in-memory journal.
54144 ** If running in non-exclusive rollback mode, the lock on the file is
54162 ** is no write-transaction active but a RESERVED or greater lock is
54165 ** 1. After a successful hot-journal rollback, it is called with
54170 ** read-transaction, this function is called with eState==PAGER_READER
54171 ** and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
54174 assert( pPager->eState!=PAGER_ERROR );
54175 if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
54180 assert( isOpen(pPager->jfd) || pPager->pInJournal==0
54181 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_BATCH_ATOMIC)
54183 if( isOpen(pPager->jfd) ){
54187 if( sqlite3JournalIsInMemory(pPager->jfd) ){
54188 /* assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ); */
54189 sqlite3OsClose(pPager->jfd);
54190 }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
54191 if( pPager->journalOff==0 ){
54194 rc = sqlite3OsTruncate(pPager->jfd, 0);
54195 if( rc==SQLITE_OK && pPager->fullSync ){
54201 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
54204 pPager->journalOff = 0;
54205 }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
54206 || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
54208 rc = zeroJournalHdr(pPager, hasSuper||pPager->tempFile);
54209 pPager->journalOff = 0;
54212 ** a hot-journal was just rolled back. In this case the journal
54214 ** the database file, it will do so using an in-memory journal.
54216 int bDelete = !pPager->tempFile;
54217 assert( sqlite3JournalIsInMemory(pPager->jfd)==0 );
54218 assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
54219 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
54220 || pPager->journalMode==PAGER_JOURNALMODE_WAL
54222 sqlite3OsClose(pPager->jfd);
54224 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
54230 sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
54231 if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
54234 p->pageHash = 0;
54240 sqlite3BitvecDestroy(pPager->pInJournal);
54241 pPager->pInJournal = 0;
54242 pPager->nRec = 0;
54245 sqlite3PcacheCleanAll(pPager->pPCache);
54247 sqlite3PcacheClearWritable(pPager->pPCache);
54249 sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
54253 /* Drop the WAL write-lock, if any. Also, if the connection was in
54257 rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
54259 }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
54260 /* This branch is taken when committing a transaction in rollback-journal
54266 assert( pPager->eLock==EXCLUSIVE_LOCK );
54267 rc = pager_truncate(pPager, pPager->dbSize);
54271 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
54275 if( !pPager->exclusiveMode
54276 && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
54280 pPager->eState = PAGER_READER;
54281 pPager->setSuper = 0;
54292 ** call to pager_unlock() will discard all in-memory pages, unlock
54294 ** means that there is a hot-journal left in the file-system, the next
54304 if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
54306 if( pPager->eState>=PAGER_WRITER_LOCKED ){
54310 }else if( !pPager->exclusiveMode ){
54311 assert( pPager->eState==PAGER_READER );
54319 ** Parameter aData must point to a buffer of pPager->pageSize bytes
54321 ** page of data and the current value of pPager->cksumInit.
54324 ** random initial value (pPager->cksumInit) and every 200th byte
54325 ** of the page data, starting with byte offset (pPager->pageSize%200).
54326 ** Each byte is interpreted as an 8-bit unsigned integer.
54338 u32 cksum = pPager->cksumInit; /* Checksum value to return */
54339 int i = pPager->pageSize-200; /* Loop counter */
54342 i -= 200;
54349 ** from the sub-journal (if isMainJrnl==0) and playback that page.
54353 ** The main rollback journal uses checksums - the statement journal does
54356 ** If the page number of the page record read from the (sub-)journal file
54366 ** If the page record is successfully read from the (sub-)journal file
54368 ** while reading the record from the (sub-)journal file or while writing
54370 ** is successfully read from the (sub-)journal file but appears to be
54374 ** * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
54388 int isMainJrnl, /* 1 -> main journal. 0 -> sub-journal. */
54401 assert( isMainJrnl || pDone ); /* pDone always used on sub-journals */
54402 assert( isSavepnt || pDone==0 ); /* pDone never used on non-savepoint */
54404 aData = pPager->pTmpSpace;
54410 ** a hot-journal rollback. If it is a hot-journal rollback, the pager
54411 ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
54412 ** only reads from the main journal, not the sub-journal.
54414 assert( pPager->eState>=PAGER_WRITER_CACHEMOD
54415 || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
54417 assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
54419 /* Read the page number and page data from the journal or sub-journal
54422 jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
54425 rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
54427 *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
54438 if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
54442 rc = read32bits(jfd, (*pOffset)-4, &cksum);
54458 if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
54459 pPager->nReserve = ((u8*)aData)[20];
54466 ** An exception to the above rule: If the database is in no-sync mode
54476 ** a hot-journal rollback, it is guaranteed that the page-cache is empty
54493 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
54503 assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
54505 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
54506 (isMainJrnl?"main-journal":"sub-journal")
54509 isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
54511 isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
54513 if( isOpen(pPager->fd)
54514 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54517 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
54518 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
54522 ** This is usually safe even for an encrypted database - as the data
54524 ** is if the data was just read from an in-memory sub-journal. In that
54527 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
54529 if( pgno>pPager->dbFileSize ){
54530 pPager->dbFileSize = pgno;
54532 if( pPager->pBackup ){
54533 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
54537 ** the database and the page is not in-memory, there is a potential
54538 ** problem. When the page is next fetched by the b-tree layer, it
54544 ** if the page is on the free-list at the start of the transaction, then
54547 ** The solution is to add an in-memory page to the cache containing
54548 ** the data just read from the sub-journal. Mark the page as dirty
54549 ** and if the pager requires a journal-sync, then mark the page as
54550 ** requiring a journal-sync before it is written.
54553 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
54554 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
54556 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
54557 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
54569 pData = pPg->pData;
54570 memcpy(pData, (u8*)aData, pPager->pageSize);
54571 pPager->xReiniter(pPg);
54581 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
54589 ** Parameter zSuper is the name of a super-journal file. A single journal
54590 ** file that referred to the super-journal file has just been rolled back.
54591 ** This routine checks if it is possible to delete the super-journal file,
54597 ** When a super-journal file is created, it is populated with the names
54598 ** of all of its child journals, one after another, formatted as utf-8
54600 ** nul-terminator byte (0x00). i.e. the entire contents of a super-journal
54603 ** "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
54605 ** A super-journal file may only be deleted once all of its child
54608 ** This function reads the contents of the super-journal file into
54613 ** * if the child journal contains a reference to super-journal
54619 ** the file-system using sqlite3OsDelete().
54627 ** the entire contents of the super-journal file. This could be
54628 ** a couple of kilobytes or so - potentially larger than the page
54632 sqlite3_vfs *pVfs = pPager->pVfs;
54634 sqlite3_file *pSuper; /* Malloc'd super-journal file descriptor */
54635 sqlite3_file *pJournal; /* Malloc'd child-journal file descriptor */
54636 char *zSuperJournal = 0; /* Contents of super-journal file */
54637 i64 nSuperJournal; /* Size of super-journal file */
54639 char *zSuperPtr; /* Space to hold super-journal filename */
54644 ** If successful, open the super-journal file for reading.
54646 pSuper = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
54653 pJournal = (sqlite3_file *)(((u8 *)pSuper) + pVfs->szOsFile);
54657 /* Load the entire super-journal file into space obtained from
54659 ** sufficient space (in zSuperPtr) to hold the names of super-journal
54660 ** files extracted from regular rollback-journals.
54664 nSuperPtr = pVfs->mxPathname+1;
54679 while( (zJournal-zSuperJournal)<nSuperJournal ){
54686 /* One of the journals pointed to by the super-journal exists.
54687 ** Open it and check if it points at the super-journal. If
54688 ** so, return without deleting the super-journal file.
54708 /* We have a match. Do not delete the super-journal file. */
54731 ** file in the file-system. This only happens when committing a transaction,
54732 ** or rolling back a transaction (including rolling back a hot-journal).
54735 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
54736 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
54751 assert( pPager->eState!=PAGER_ERROR );
54752 assert( pPager->eState!=PAGER_READER );
54754 if( isOpen(pPager->fd)
54755 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
54758 int szPage = pPager->pageSize;
54759 assert( pPager->eLock==EXCLUSIVE_LOCK );
54761 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
54765 rc = sqlite3OsTruncate(pPager->fd, newSize);
54767 char *pTmp = pPager->pTmpSpace;
54769 testcase( (newSize-szPage) == currentSize );
54770 testcase( (newSize-szPage) > currentSize );
54771 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
54774 pPager->dbFileSize = nPage;
54782 ** Return a sanitized version of the sector-size of OS file pFile. The
54801 ** super-journal pointers within created journal files.
54805 ** Otherwise, for non-temporary files, the effective sector size is
54812 ** pPager->sectorSize is to define the "blast radius" of bytes that
54820 assert( isOpen(pPager->fd) || pPager->tempFile );
54822 if( pPager->tempFile
54823 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
54829 pPager->sectorSize = 512;
54831 pPager->sectorSize = sqlite3SectorSize(pPager->fd);
54842 ** (2) 4 byte big-endian integer which is the number of valid page records
54845 ** (3) 4 byte big-endian integer which is the initial value for the
54849 ** (5) 4 byte big-endian integer which is the sector size. The header
54851 ** (6) 4 byte big-endian integer which is the page size.
54855 ** + pPager->pageSize bytes of data.
54872 ** no-sync option for the journal. A power failure could lead to corruption
54876 ** If the file opened as the journal file is not a well-formed
54882 ** If an I/O or malloc() error occurs, the journal-file is not deleted
54893 sqlite3_vfs *pVfs = pPager->pVfs;
54900 char *zSuper = 0; /* Name of super-journal file if any */
54903 u32 savedPageSize = pPager->pageSize;
54908 assert( isOpen(pPager->jfd) );
54909 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
54914 /* Read the super-journal name from the journal, if it is present.
54915 ** If a super-journal file name is specified, but the file is not
54921 ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
54925 zSuper = pPager->pTmpSpace;
54926 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
54934 pPager->journalOff = 0;
54956 ** working in no-sync mode. This means that the rest of the journal
54961 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
54962 nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
54980 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
54981 nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
54987 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
54992 pPager->dbSize = mxPg;
55003 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
55008 pPager->journalOff = szJ;
55034 rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
55038 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
55042 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
55046 ** malloc error that occurred after the change-counter was updated but
55047 ** before the transaction was committed, then the change-counter
55050 ** update the change-counter at all. This may lead to cache inconsistency
55054 pPager->changeCountDone = pPager->tempFile;
55057 /* Leave 4 bytes of space before the super-journal filename in memory.
55061 zSuper = &pPager->pTmpSpace[4];
55062 rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
55066 && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
55075 /* If there was a super-journal and this routine will return success,
55076 ** see if it is possible to delete the super-journal.
55078 assert( zSuper==&pPager->pTmpSpace[4] );
55079 memset(&zSuper[-4], 0, 4);
55085 nPlayback, pPager->zJournal);
55100 ** pPg->pData. A shared lock or greater must be held on the database
55110 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
55116 assert( pPager->eState>=PAGER_READER && !MEMDB );
55117 assert( isOpen(pPager->fd) );
55120 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
55124 rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
55128 i64 iOffset = (pPg->pgno-1)*(i64)pPager->pageSize;
55129 rc = sqlite3OsRead(pPager->fd, pPg->pData, pPager->pageSize, iOffset);
55135 if( pPg->pgno==1 ){
55142 ** pPager->dbFileVers[] with all 0xff bytes should suffice.
55149 memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
55151 u8 *dbFileVers = &((u8*)pPg->pData)[24];
55152 memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
55156 PAGER_INCR(pPager->nRead);
55157 IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno));
55159 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg)));
55165 ** Update the value of the change-counter at offsets 24 and 92 in
55169 ** routine which only updates the change-counter if the update is actually
55170 ** needed, as determined by the pPager->changeCountDone state variable.
55176 change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
55177 put32bits(((char*)pPg->pData)+24, change_counter);
55182 put32bits(((char*)pPg->pData)+92, change_counter);
55183 put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
55212 pPager->xReiniter(pPg);
55226 sqlite3BackupRestart(pPager->pBackup);
55245 pPager->dbSize = pPager->dbOrigSize;
55246 rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
55247 pList = sqlite3PcacheDirtyList(pPager->pPCache);
55249 PgHdr *pNext = pList->pDirty;
55250 rc = pagerUndoCallback((void *)pPager, pList->pgno);
55276 assert( pPager->pWal );
55280 for(p=pList; p && p->pDirty; p=p->pDirty){
55281 assert( p->pgno < p->pDirty->pgno );
55285 assert( pList->pDirty==0 || isCommit );
55293 for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
55294 if( p->pgno<=nTruncate ){
55295 ppNext = &p->pDirty;
55303 pPager->aStat[PAGER_STAT_WRITE] += nList;
55305 if( pList->pgno==1 ) pager_write_changecounter(pList);
55306 rc = sqlite3WalFrames(pPager->pWal,
55307 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
55309 if( rc==SQLITE_OK && pPager->pBackup ){
55310 for(p=pList; p; p=p->pDirty){
55311 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
55316 pList = sqlite3PcacheDirtyList(pPager->pPCache);
55317 for(p=pList; p; p=p->pDirty){
55338 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
55345 sqlite3WalEndReadTransaction(pPager->pWal);
55347 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
55350 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
55369 /* Query the WAL sub-system for the database size. The WalDbsize()
55372 ** available from the WAL sub-system if the log file is empty or
55375 assert( pPager->eState==PAGER_OPEN );
55376 assert( pPager->eLock>=SHARED_LOCK );
55377 assert( isOpen(pPager->fd) );
55378 assert( pPager->tempFile==0 );
55379 nPage = sqlite3WalDbsize(pPager->pWal);
55382 ** WAL sub-system, determine the page count based on the size of
55384 ** integer multiple of the page-size, round up the result.
55386 if( nPage==0 && ALWAYS(isOpen(pPager->fd)) ){
55388 int rc = sqlite3OsFileSize(pPager->fd, &n);
55392 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
55399 if( nPage>pPager->mxPgno ){
55400 pPager->mxPgno = (Pgno)nPage;
55409 ** Check if the *-wal file that corresponds to the database opened by pPager
55410 ** exists if the database is not empy, or verify that the *-wal file does
55413 ** If the database is not empty and the *-wal file exists, open the pager
55414 ** in WAL mode. If the database is empty or if no *-wal file exists and
55422 ** a WAL on a none-empty database, this ensures there is no race condition
55428 assert( pPager->eState==PAGER_OPEN );
55429 assert( pPager->eLock>=SHARED_LOCK );
55431 if( !pPager->tempFile ){
55434 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
55443 rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
55445 testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
55448 }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
55449 pPager->journalMode = PAGER_JOURNALMODE_DELETE;
55459 ** the entire super-journal file. The case pSavepoint==NULL occurs when
55463 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
55476 ** * Pages are then played back from the sub-journal file, starting
55490 ** (or transaction). No page with a page-number greater than this value
55495 i64 iHdrOff; /* End of first segment of main-journal records */
55499 assert( pPager->eState!=PAGER_ERROR );
55500 assert( pPager->eState>=PAGER_WRITER_LOCKED );
55504 pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
55513 pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
55514 pPager->changeCountDone = pPager->tempFile;
55520 /* Use pPager->journalOff as the effective size of the main rollback
55523 ** past pPager->journalOff is off-limits to us.
55525 szJ = pPager->journalOff;
55531 ** greater than the current database size (pPager->dbSize) but those
55536 iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
55537 pPager->journalOff = pSavepoint->iOffset;
55538 while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
55539 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55543 pPager->journalOff = 0;
55548 ** of the main journal file. Continue to skip out-of-range pages and
55551 while( rc==SQLITE_OK && pPager->journalOff<szJ ){
55559 ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
55564 && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
55566 nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
55568 for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
55569 rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
55573 assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
55575 /* Finally, rollback pages from the sub-journal. Page that were
55577 ** will be skipped. Out-of-range pages are also skipped.
55581 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
55584 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
55586 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
55587 assert( offset==(i64)ii*(4+pPager->pageSize) );
55595 pPager->journalOff = szJ;
55602 ** Change the maximum number of in-memory pages that are allowed
55606 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
55610 ** Change the maximum number of in-memory pages that are allowed
55614 return sqlite3PcacheSetSpillsize(pPager->pPCache, mxPage);
55622 sqlite3_file *fd = pPager->fd;
55623 if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
55625 sz = pPager->szMmap;
55626 pPager->bUseFetch = (sz>0);
55628 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
55637 pPager->szMmap = szMmap;
55645 sqlite3PcacheShrink(pPager->pPCache);
55667 ** database (with some additional information - the nRec field
55668 ** of the journal header - being written in between the two
55678 ** The above is for a rollback-journal mode. For WAL mode, OFF continues
55689 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
55705 if( pPager->tempFile ){
55706 pPager->noSync = 1;
55707 pPager->fullSync = 0;
55708 pPager->extraSync = 0;
55710 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
55711 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
55712 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
55714 if( pPager->noSync ){
55715 pPager->syncFlags = 0;
55717 pPager->syncFlags = SQLITE_SYNC_FULL;
55719 pPager->syncFlags = SQLITE_SYNC_NORMAL;
55721 pPager->walSyncFlags = (pPager->syncFlags<<2);
55722 if( pPager->fullSync ){
55723 pPager->walSyncFlags |= pPager->syncFlags;
55725 if( (pgFlags & PAGER_CKPT_FULLFSYNC) && !pPager->noSync ){
55726 pPager->walSyncFlags |= (SQLITE_SYNC_FULL<<2);
55729 pPager->doNotSpill &= ~SPILLFLAG_OFF;
55731 pPager->doNotSpill |= SPILLFLAG_OFF;
55773 rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
55781 ** The pager invokes the busy-handler if sqlite3OsLock() returns
55782 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
55786 ** (which occurs during hot-journal rollback). Summary:
55789 ** --------------------------------------------------------
55790 ** NO_LOCK -> SHARED_LOCK | Yes
55791 ** SHARED_LOCK -> RESERVED_LOCK | No
55792 ** SHARED_LOCK -> EXCLUSIVE_LOCK | No
55793 ** RESERVED_LOCK -> EXCLUSIVE_LOCK | Yes
55795 ** If the busy-handler callback returns non-zero, the lock is
55801 int (*xBusyHandler)(void *), /* Pointer to busy-handler function */
55805 pPager->xBusyHandler = xBusyHandler;
55806 pPager->pBusyHandlerArg = pBusyHandlerArg;
55807 ap = (void **)&pPager->xBusyHandler;
55810 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
55818 ** is a no-op. The value returned is the error state error code (i.e.
55819 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
55828 ** * the database is either not an in-memory database or it is
55829 ** an in-memory database that currently consists of zero pages.
55853 ** is a no-op for that case anyhow.
55858 if( (pPager->memDb==0 || pPager->dbSize==0)
55859 && sqlite3PcacheRefCount(pPager->pPCache)==0
55860 && pageSize && pageSize!=(u32)pPager->pageSize
55865 if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
55866 rc = sqlite3OsFileSize(pPager->fd, &nByte);
55869 /* 8 bytes of zeroed overrun space is sufficient so that the b-tree
55881 rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
55884 sqlite3PageFree(pPager->pTmpSpace);
55885 pPager->pTmpSpace = pNew;
55886 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
55887 pPager->pageSize = pageSize;
55893 *pPageSize = pPager->pageSize;
55895 if( nReserve<0 ) nReserve = pPager->nReserve;
55897 pPager->nReserve = (i16)nReserve;
55912 return pPager->pTmpSpace;
55924 pPager->mxPgno = mxPage;
55926 assert( pPager->eState!=PAGER_OPEN ); /* Called only by OP_MaxPgcnt */
55927 /* assert( pPager->mxPgno>=pPager->dbSize ); */
55931 return pPager->mxPgno;
55939 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
55948 sqlite3_io_error_pending = -1;
55975 assert( isOpen(pPager->fd) || pPager->tempFile );
55983 if( isOpen(pPager->fd) ){
55985 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
55994 ** This function may only be called when a read-transaction is open on
55997 ** However, if the file is between 1 and <page-size> bytes in size, then
56001 assert( pPager->eState>=PAGER_READER );
56002 assert( pPager->eState!=PAGER_WRITER_FINISHED );
56003 *pnPage = (int)pPager->dbSize;
56009 ** a similar or greater lock is already held, this function is a no-op
56024 /* Check that this is either a no-op (because the requested lock is
56025 ** already held), or one of the transitions that the busy-handler
56029 assert( (pPager->eLock>=locktype)
56030 || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
56031 || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
56036 }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
56042 ** following is true for all dirty pages currently in the page-cache:
56048 ** be necessary to write the current content out to the sub-journal
56058 ** sub-journal rolled back the content could not be restored and the
56064 assert( pPg->flags&PGHDR_DIRTY );
56065 assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
56068 sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
56075 ** Truncate the in-memory database file image to nPage pages. This
56086 assert( pPager->dbSize>=nPage );
56087 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
56088 pPager->dbSize = nPage;
56103 ** This function is called before attempting a hot-journal rollback. It
56104 ** syncs the journal file to disk, then sets pPager->journalHdr to the
56108 ** Syncing a hot-journal to disk before attempting to roll it back ensures
56109 ** that if a power-failure occurs during the rollback, the process that
56118 if( !pPager->noSync ){
56119 rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
56122 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
56146 if( pPager->pMmapFreelist ){
56147 *ppPage = p = pPager->pMmapFreelist;
56148 pPager->pMmapFreelist = p->pDirty;
56149 p->pDirty = 0;
56150 assert( pPager->nExtra>=8 );
56151 memset(p->pExtra, 0, 8);
56153 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
56155 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
56158 p->pExtra = (void *)&p[1];
56159 p->flags = PGHDR_MMAP;
56160 p->nRef = 1;
56161 p->pPager = pPager;
56164 assert( p->pExtra==(void *)&p[1] );
56165 assert( p->pPage==0 );
56166 assert( p->flags==PGHDR_MMAP );
56167 assert( p->pPager==pPager );
56168 assert( p->nRef==1 );
56170 p->pgno = pgno;
56171 p->pData = pData;
56172 pPager->nMmapOut++;
56183 Pager *pPager = pPg->pPager;
56184 pPager->nMmapOut--;
56185 pPg->pDirty = pPager->pMmapFreelist;
56186 pPager->pMmapFreelist = pPg;
56188 assert( pPager->fd->pMethods->iVersion>=3 );
56189 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
56198 for(p=pPager->pMmapFreelist; p; p=pNext){
56199 pNext = p->pDirty;
56206 ** to be on disk. Return non-zero (SQLITE_READONLY_DBMOVED or some other error
56213 if( pPager->tempFile ) return SQLITE_OK;
56214 if( pPager->dbSize==0 ) return SQLITE_OK;
56215 assert( pPager->zFilename && pPager->zFilename[0] );
56216 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
56218 /* If the HAS_MOVED file-control is unimplemented, assume that the file
56244 u8 *pTmp = (u8*)pPager->pTmpSpace;
56250 /* pPager->errCode = 0; */
56251 pPager->exclusiveMode = 0;
56255 assert( db || pPager->pWal==0 );
56256 if( db && 0==(db->flags & SQLITE_NoCkptOnClose)
56261 sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
56262 pPager->pWal = 0;
56277 ** back or finalize it. The next database user will have to do hot-journal
56280 if( isOpen(pPager->jfd) ){
56289 sqlite3OsClose(pPager->jfd);
56290 sqlite3OsClose(pPager->fd);
56292 sqlite3PcacheClose(pPager->pPCache);
56293 assert( !pPager->aSavepoint && !pPager->pInJournal );
56294 assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
56305 return pPg->pgno;
56319 ** disk and can be restored in the event of a hot-journal rollback.
56321 ** If the Pager.noSync flag is set, then this function is a no-op.
56322 ** Otherwise, the actions required depend on the journal-mode and the
56323 ** device characteristics of the file-system, as follows:
56325 ** * If the journal file is an in-memory journal file, no action need
56331 ** been written following it. If the pager is operating in full-sync
56337 ** Or, in pseudo-code:
56339 ** if( NOT <in-memory journal> ){
56341 ** if( <full-sync mode> ) xSync(<journal file>);
56354 assert( pPager->eState==PAGER_WRITER_CACHEMOD
56355 || pPager->eState==PAGER_WRITER_DBMOD
56363 if( !pPager->noSync ){
56364 assert( !pPager->tempFile );
56365 if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
56366 const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
56367 assert( isOpen(pPager->jfd) );
56371 ** that wrote to this database was operating in persistent-journal
56374 ** file happens to be a journal-header (written as part of the
56375 ** previous connection's transaction), and a crash or power-failure
56379 ** hot-journal rollback following recovery. It may roll back all
56381 ** out-of-date data that follows it. Database corruption.
56397 put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
56400 rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
56403 rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
56410 ** full-synchronous mode, sync the journal first. This ensures that
56420 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
56423 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
56426 IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
56428 pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
56435 rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
56436 (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
56441 pPager->journalHdr = pPager->journalOff;
56443 pPager->nRec = 0;
56448 pPager->journalHdr = pPager->journalOff;
56456 sqlite3PcacheClearSyncFlags(pPager->pPCache);
56457 pPager->eState = PAGER_WRITER_DBMOD;
56465 ** in-memory pages in the list to the database file. The argument may
56467 ** a no-op.
56474 ** If the pager is a temp-file pager and the actual file-system file
56499 assert( pPager->tempFile || pPager->eState==PAGER_WRITER_DBMOD );
56500 assert( pPager->eLock==EXCLUSIVE_LOCK );
56501 assert( isOpen(pPager->fd) || pList->pDirty==0 );
56503 /* If the file is a temp-file has not yet been opened, open it now. It
56505 ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
56507 if( !isOpen(pPager->fd) ){
56508 assert( pPager->tempFile && rc==SQLITE_OK );
56509 rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
56515 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
56517 && pPager->dbHintSize<pPager->dbSize
56518 && (pList->pDirty || pList->pgno>pPager->dbHintSize)
56520 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
56521 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
56522 pPager->dbHintSize = pPager->dbSize;
56526 Pgno pgno = pList->pgno;
56530 ** make the file smaller (presumably by auto-vacuum code). Do not write
56536 if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
56537 i64 offset = (pgno-1)*(i64)pPager->pageSize; /* Offset to write */
56540 assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
56541 if( pList->pgno==1 ) pager_write_changecounter(pList);
56543 pData = pList->pData;
56546 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
56553 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
56555 if( pgno>pPager->dbFileSize ){
56556 pPager->dbFileSize = pgno;
56558 pPager->aStat[PAGER_STAT_WRITE]++;
56561 sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
56571 pList = pList->pDirty;
56578 ** Ensure that the sub-journal file is open. If it is already open, this
56579 ** function is a no-op.
56587 if( !isOpen(pPager->sjfd) ){
56592 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
56593 nStmtSpill = -1;
56595 rc = sqlite3JournalOpen(pPager->pVfs, 0, pPager->sjfd, flags, nStmtSpill);
56601 ** Append a record of the current state of page pPg to the sub-journal.
56603 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
56607 ** error code if the attempt to write to the sub-journal fails, or
56613 Pager *pPager = pPg->pPager;
56614 if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
56616 /* Open the sub-journal, if it has not already been opened */
56617 assert( pPager->useJournal );
56618 assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
56619 assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
56622 || pPg->pgno>pPager->dbOrigSize
56626 /* If the sub-journal was opened successfully (or was already open),
56629 void *pData = pPg->pData;
56630 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
56633 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
56634 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
56636 rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
56641 pPager->nSubRec++;
56642 assert( pPager->nSavepoint>0 );
56643 rc = addToSavepointBitvecs(pPager, pPg->pgno);
56658 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
56678 assert( pPg->pPager==pPager );
56679 assert( pPg->flags&PGHDR_DIRTY );
56697 if( NEVER(pPager->errCode) ) return SQLITE_OK;
56698 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
56699 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
56700 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
56701 if( pPager->doNotSpill
56702 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
56703 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
56708 pPager->aStat[PAGER_STAT_SPILL]++;
56709 pPg->pDirty = 0;
56719 if( pPager->tempFile==0 ){
56720 rc = sqlite3JournalCreate(pPager->jfd);
56726 if( pPg->flags&PGHDR_NEED_SYNC
56727 || pPager->eState==PAGER_WRITER_CACHEMOD
56734 assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
56741 PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
56752 int rc = pPager->errCode;
56754 PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
56757 PgHdr *pNext = pList->pDirty;
56758 if( pList->nRef==0 ){
56774 ** If zFilename is NULL then a randomly-named temporary file is created
56778 ** This can be used to implement an in-memory database.
56804 int nExtra, /* Extra bytes append to each in-memory page */
56812 int tempFile = 0; /* True for temp files (incl. in-memory files) */
56813 int memDb = 0; /* True if this is an in-memory file */
56819 int readOnly = 0; /* True if this is a read-only file */
56830 /* Figure out how much space is required for each journal file-handle
56831 ** (there are two of them, the main journal and the sub-journal). */
56855 nPathname = pVfs->mxPathname+1;
56878 nUriByte = (int)(&z[1] - zUri);
56880 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
56882 ** the database being opened will be more than pVfs->mxPathname
56885 ** check for a hot-journal before reading.
56901 ** Database file handle (pVfs->szOsFile bytes)
56902 ** Sub-journal file handle (journalFileSize bytes)
56912 ** Some 3rd-party software, over which we have no control, depends on
56916 ** misuse of SQLite and a bug in the 3rd-party software, but the 3rd-party
56919 ** filename format expected by 3rd-party software should be as follows:
56921 ** - Main Database Path
56922 ** - \0
56923 ** - Multiple URI components consisting of:
56924 ** - Key
56925 ** - \0
56926 ** - Value
56927 ** - \0
56928 ** - \0
56929 ** - Journal Path
56930 ** - \0
56931 ** - WAL Path (zWALName)
56932 ** - \0
56942 ROUND8(pVfs->szOsFile) + /* The main db file */
56960 pPager->pPCache = (PCache*)pPtr; pPtr += ROUND8(pcacheSize);
56961 pPager->fd = (sqlite3_file*)pPtr; pPtr += ROUND8(pVfs->szOsFile);
56962 pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
56963 pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
56964 assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
56969 pPager->zFilename = (char*)pPtr;
56982 pPager->zJournal = (char*)pPtr;
56984 memcpy(pPtr, "-journal",8); pPtr += 8 + 1;
56986 sqlite3FileSuffix3(zFilename,pPager->zJournal);
56987 pPtr = (u8*)(pPager->zJournal + sqlite3Strlen30(pPager->zJournal)+1);
56990 pPager->zJournal = 0;
56996 pPager->zWal = (char*)pPtr;
56998 memcpy(pPtr, "-wal", 4); pPtr += 4 + 1;
57000 sqlite3FileSuffix3(zFilename, pPager->zWal);
57001 pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1);
57004 pPager->zWal = 0;
57009 pPager->pVfs = pVfs;
57010 pPager->vfsFlags = vfsFlags;
57016 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
57032 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
57036 if( szPageDflt<pPager->sectorSize ){
57037 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
57040 szPageDflt = (u32)pPager->sectorSize;
57057 pPager->noLock = sqlite3_uri_boolean(pPager->zFilename, "nolock", 0);
57059 || sqlite3_uri_boolean(pPager->zFilename, "immutable", 0) ){
57069 ** This branch is also run for an in-memory database. An in-memory
57070 ** database is the same as a temp-file that is never written out to
57071 ** disk and uses an in-memory rollback journal.
57077 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
57078 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE mode */
57079 pPager->noLock = 1; /* Do no locking */
57087 assert( pPager->memDb==0 );
57088 rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
57097 !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
57103 sqlite3OsClose(pPager->fd);
57104 sqlite3PageFree(pPager->pTmpSpace);
57109 PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
57110 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
57112 pPager->useJournal = (u8)useJournal;
57113 /* pPager->stmtOpen = 0; */
57114 /* pPager->stmtInUse = 0; */
57115 /* pPager->nRef = 0; */
57116 /* pPager->stmtSize = 0; */
57117 /* pPager->stmtJSize = 0; */
57118 /* pPager->nPage = 0; */
57119 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
57120 /* pPager->state = PAGER_UNLOCK; */
57121 /* pPager->errMask = 0; */
57122 pPager->tempFile = (u8)tempFile;
57126 pPager->exclusiveMode = (u8)tempFile;
57127 pPager->changeCountDone = pPager->tempFile;
57128 pPager->memDb = (u8)memDb;
57129 pPager->readOnly = (u8)readOnly;
57130 assert( useJournal || pPager->tempFile );
57131 pPager->noSync = pPager->tempFile;
57132 if( pPager->noSync ){
57133 assert( pPager->fullSync==0 );
57134 assert( pPager->extraSync==0 );
57135 assert( pPager->syncFlags==0 );
57136 assert( pPager->walSyncFlags==0 );
57138 pPager->fullSync = 1;
57139 pPager->extraSync = 0;
57140 pPager->syncFlags = SQLITE_SYNC_NORMAL;
57141 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | (SQLITE_SYNC_NORMAL<<2);
57143 /* pPager->pFirst = 0; */
57144 /* pPager->pFirstSynced = 0; */
57145 /* pPager->pLast = 0; */
57146 pPager->nExtra = (u16)nExtra;
57147 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
57148 assert( isOpen(pPager->fd) || tempFile );
57151 pPager->journalMode = PAGER_JOURNALMODE_OFF;
57153 pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
57155 /* pPager->xBusyHandler = 0; */
57156 /* pPager->pBusyHandlerArg = 0; */
57157 pPager->xReiniter = xReinit;
57159 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
57160 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
57173 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
57174 zName--;
57176 pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
57177 return pPager->fd;
57184 ** the file-system for the given pager. A hot journal is one that
57185 ** needs to be played back. According to this function, a hot-journal
57199 ** This routine does not check if there is a super-journal filename
57200 ** at the end of the file. If there is, and that super-journal file
57202 ** case this routine will return a false-positive. The pager_playback()
57206 ** If a hot-journal file is found to exist, *pExists is set to 1 and
57207 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
57209 ** to determine whether or not a hot-journal file exists, the IO error
57213 sqlite3_vfs * const pVfs = pPager->pVfs;
57216 int jrnlOpen = !!isOpen(pPager->jfd);
57218 assert( pPager->useJournal );
57219 assert( isOpen(pPager->fd) );
57220 assert( pPager->eState==PAGER_OPEN );
57222 assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
57228 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
57238 ** in fact there is none. This results in a false-positive which will
57241 rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
57245 assert( pPager->tempFile==0 );
57259 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
57260 if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
57266 ** at least one non-zero bytes at the start of the journal file.
57272 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
57276 rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
57281 sqlite3OsClose(pPager->jfd);
57308 ** has been successfully called. If a shared-lock is already held when
57309 ** this function is called, it is a no-op.
57316 ** the SHARED lock, the file-system is checked for a hot-journal,
57317 ** which is played back if present. Following any hot-journal
57319 ** the 'change-counter' field of the database file header and
57322 ** 2) If the pager is running in exclusive-mode, and there are currently
57329 ** occurs while locking the database, checking for a hot-journal file or
57335 /* This routine is only called from b-tree and only when there are no
57339 assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
57341 assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
57342 assert( pPager->errCode==SQLITE_OK );
57344 if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
57345 int bHotJournal = 1; /* True if there exists a hot journal-file */
57348 assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );
57352 assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
57359 if( pPager->eLock<=SHARED_LOCK ){
57366 if( pPager->readOnly ){
57376 ** hot-journal back.
57393 ** in exclusive-access mode the file descriptor will be kept open
57394 ** and possibly used for a transaction later on. Also, write-access
57401 ** may mean that the pager was in the error-state when this
57404 if( !isOpen(pPager->jfd) ){
57405 sqlite3_vfs * const pVfs = pPager->pVfs;
57408 pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
57412 assert( !pPager->tempFile );
57413 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
57414 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
57417 sqlite3OsClose(pPager->jfd);
57424 ** playing back the hot-journal so that we don't end up with
57430 if( isOpen(pPager->jfd) ){
57434 rc = pager_playback(pPager, !pPager->tempFile);
57435 pPager->eState = PAGER_OPEN;
57437 }else if( !pPager->exclusiveMode ){
57443 ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
57462 assert( pPager->eState==PAGER_OPEN );
57463 assert( (pPager->eLock==SHARED_LOCK)
57464 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
57468 if( !pPager->tempFile && pPager->hasHeldSharedLock ){
57469 /* The shared-lock has just been acquired then check to
57473 ** single unnecessary sqlite3OsRead() call at the start-up.
57477 ** a 32-bit counter that is incremented with each change. The
57485 char dbFileVers[sizeof(pPager->dbFileVers)];
57488 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
57496 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
57506 sqlite3OsUnfetch(pPager->fd, 0, 0);
57511 /* If there is a WAL file in the file-system, open this database in WAL
57512 ** mode. Otherwise, the following function call is a no-op.
57516 assert( pPager->pWal==0 || rc==SQLITE_OK );
57525 if( pPager->tempFile==0 && pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
57526 rc = pagerPagecount(pPager, &pPager->dbSize);
57533 assert( pPager->eState==PAGER_OPEN );
57535 pPager->eState = PAGER_READER;
57536 pPager->hasHeldSharedLock = 1;
57547 ** nothing to rollback, so this routine is a no-op.
57550 if( sqlite3PcacheRefCount(pPager->pPCache)==0 ){
57551 assert( pPager->nMmapOut==0 ); /* because page1 is never memory mapped */
57564 ** getPageNormal() -- The normal getter
57565 ** getPageError() -- Used if the pager is in an error state
57566 ** getPageMmap() -- Used if memory-mapped I/O is enabled
57588 ** a) When reading a free-list leaf page from the database, and
57606 ** to find a page in the in-memory cache first. If the page is not already
57608 ** just returns 0. This routine acquires a read-lock the first time it
57624 assert( pPager->errCode==SQLITE_OK );
57625 assert( pPager->eState>=PAGER_READER );
57627 assert( pPager->hasHeldSharedLock==1 );
57630 pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
57633 rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
57640 pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
57642 assert( pPg->pgno==pgno );
57643 assert( pPg->pPager==pPager || pPg->pPager==0 );
57646 if( pPg->pPager && !noContent ){
57650 pPager->aStat[PAGER_STAT_HIT]++;
57665 pPg->pPager = pPager;
57667 assert( !isOpen(pPager->fd) || !MEMDB );
57668 if( !isOpen(pPager->fd) || pPager->dbSize<pgno || noContent ){
57669 if( pgno>pPager->mxPgno ){
57674 /* Failure to set the bits in the InJournal bit-vectors is benign.
57681 if( pgno<=pPager->dbOrigSize ){
57682 TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
57689 memset(pPg->pData, 0, pPager->pageSize);
57692 assert( pPg->pPager==pPager );
57693 pPager->aStat[PAGER_STAT_MISS]++;
57714 /* The page getter for when memory-mapped I/O is enabled */
57725 /* It is acceptable to use a read-only (mmap) page for any page except
57726 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
57728 ** temporary or in-memory database. */
57730 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
57742 assert( pPager->eState>=PAGER_READER );
57744 assert( pPager->hasHeldSharedLock==1 );
57745 assert( pPager->errCode==SQLITE_OK );
57748 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
57756 rc = sqlite3OsFetch(pPager->fd,
57757 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
57760 if( pPager->eState>PAGER_READER || pPager->tempFile ){
57766 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
57792 assert( pPager->errCode!=SQLITE_OK );
57794 return pPager->errCode;
57806 return pPager->xGet(pPager, pgno, ppPage, flags);
57810 ** Acquire a page if it is already in the in-memory cache. Do
57824 assert( pPager->pPCache!=0 );
57825 pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
57826 assert( pPage==0 || pPager->hasHeldSharedLock );
57828 return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
57844 TESTONLY( Pager *pPager = pPg->pPager; )
57846 if( pPg->flags & PGHDR_MMAP ){
57847 assert( pPg->pgno!=1 ); /* Page1 is never memory mapped */
57853 assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
57861 assert( pPg->pgno==1 );
57862 assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
57863 pPager = pPg->pPager;
57874 ** to the start of it. If there are active savepoints, open the sub-journal
57892 sqlite3_vfs * const pVfs = pPager->pVfs; /* Local cache of vfs pointer */
57894 assert( pPager->eState==PAGER_WRITER_LOCKED );
57896 assert( pPager->pInJournal==0 );
57898 /* If already in the error state, this function is a no-op. But on
57901 if( NEVER(pPager->errCode) ) return pPager->errCode;
57903 if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
57904 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
57905 if( pPager->pInJournal==0 ){
57910 if( !isOpen(pPager->jfd) ){
57911 if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
57912 sqlite3MemJournalOpen(pPager->jfd);
57917 if( pPager->tempFile ){
57930 pVfs, pPager->zJournal, pPager->jfd, flags, nSpill
57934 assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
57939 ** the sub-journal if necessary.
57943 pPager->nRec = 0;
57944 pPager->journalOff = 0;
57945 pPager->setSuper = 0;
57946 pPager->journalHdr = 0;
57952 sqlite3BitvecDestroy(pPager->pInJournal);
57953 pPager->pInJournal = 0;
57955 assert( pPager->eState==PAGER_WRITER_LOCKED );
57956 pPager->eState = PAGER_WRITER_CACHEMOD;
57963 ** Begin a write-transaction on the specified pager object. If a
57964 ** write-transaction has already been opened, this function is a no-op.
57971 ** If the subjInMemory argument is non-zero, then any sub-journal opened
57972 ** within this transaction will be opened as an in-memory file. This
57973 ** has no effect if the sub-journal is already opened (as it may be when
57975 ** sub-journal. If the subjInMemory argument is zero, then any required
57976 ** sub-journal is implemented in-memory if pPager is an in-memory database,
57982 if( pPager->errCode ) return pPager->errCode;
57983 assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
57984 pPager->subjInMemory = (u8)subjInMemory;
57986 if( ALWAYS(pPager->eState==PAGER_READER) ){
57987 assert( pPager->pInJournal==0 );
57993 if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
57998 (void)sqlite3WalExclusiveMode(pPager->pWal, 1);
58003 ** The busy-handler is not invoked if another connection already
58004 ** holds the write-lock. If possible, the upper layer will call it.
58006 rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
58010 ** busy-handler callback can be used when upgrading to the EXCLUSIVE
58025 ** transactions may copy data from the sub-journal into the database
58029 pPager->eState = PAGER_WRITER_LOCKED;
58030 pPager->dbHintSize = pPager->dbSize;
58031 pPager->dbFileSize = pPager->dbSize;
58032 pPager->dbOrigSize = pPager->dbSize;
58033 pPager->journalOff = 0;
58036 assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
58037 assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
58049 Pager *pPager = pPg->pPager;
58053 i64 iOff = pPager->journalOff;
58058 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
58060 assert( pPager->journalHdr<=pPager->journalOff );
58061 pData2 = pPg->pData;
58065 ** page in the block above, set the need-sync flag for the page.
58071 pPg->flags |= PGHDR_NEED_SYNC;
58073 rc = write32bits(pPager->jfd, iOff, pPg->pgno);
58075 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
58077 rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
58080 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
58081 pPager->journalOff, pPager->pageSize));
58084 PAGERID(pPager), pPg->pgno,
58085 ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
58087 pPager->journalOff += 8 + pPager->pageSize;
58088 pPager->nRec++;
58089 assert( pPager->pInJournal!=0 );
58090 rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
58093 rc |= addToSavepointBitvecs(pPager, pPg->pgno);
58100 ** main journal or sub-journal as required. If the page is written into
58106 Pager *pPager = pPg->pPager;
58109 /* This routine is not called unless a write-transaction has already
58113 assert( pPager->eState==PAGER_WRITER_LOCKED
58114 || pPager->eState==PAGER_WRITER_CACHEMOD
58115 || pPager->eState==PAGER_WRITER_DBMOD
58118 assert( pPager->errCode==0 );
58119 assert( pPager->readOnly==0 );
58123 ** obtained the necessary locks to begin the write-transaction, but the
58131 if( pPager->eState==PAGER_WRITER_LOCKED ){
58135 assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
58145 assert( (pPager->pInJournal!=0) == isOpen(pPager->jfd) );
58146 if( pPager->pInJournal!=0
58147 && sqlite3BitvecTestNotNull(pPager->pInJournal, pPg->pgno)==0
58150 if( pPg->pgno<=pPager->dbOrigSize ){
58156 if( pPager->eState!=PAGER_WRITER_DBMOD ){
58157 pPg->flags |= PGHDR_NEED_SYNC;
58160 PAGERID(pPager), pPg->pgno,
58161 ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
58165 /* The PGHDR_DIRTY bit is set above when the page was added to the dirty-list
58170 pPg->flags |= PGHDR_WRITEABLE;
58175 if( pPager->nSavepoint>0 ){
58180 if( pPager->dbSize<pPg->pgno ){
58181 pPager->dbSize = pPg->pgno;
58204 Pager *pPager = pPg->pPager; /* The pager that owns pPg */
58205 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
58212 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
58213 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
58215 /* This trick assumes that both the page-size and sector-size are
58219 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
58221 nPageCount = pPager->dbSize;
58222 if( pPg->pgno>nPageCount ){
58223 nPage = (pPg->pgno - pg1)+1;
58224 }else if( (pg1+nPagePerSector-1)>nPageCount ){
58225 nPage = nPageCount+1-pg1;
58230 assert(pg1<=pPg->pgno);
58231 assert((pg1+nPage)>pPg->pgno);
58236 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
58241 if( pPage->flags&PGHDR_NEED_SYNC ){
58248 if( pPage->flags&PGHDR_NEED_SYNC ){
58266 pPage->flags |= PGHDR_NEED_SYNC;
58272 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
58273 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
58285 ** fit on a single disk sector. In this case all co-resident pages
58292 Pager *pPager = pPg->pPager;
58293 assert( (pPg->flags & PGHDR_MMAP)==0 );
58294 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58296 if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
58297 if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
58299 }else if( pPager->errCode ){
58300 return pPager->errCode;
58301 }else if( pPager->sectorSize > (u32)pPager->pageSize ){
58302 assert( pPager->tempFile==0 );
58316 return pPg->flags & PGHDR_WRITEABLE;
58334 ** This optimization cannot be used with a temp-file, as the page may
58341 Pager *pPager = pPg->pPager;
58342 if( !pPager->tempFile && (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
58343 PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
58344 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
58345 pPg->flags |= PGHDR_DONT_WRITE;
58346 pPg->flags &= ~PGHDR_WRITEABLE;
58347 testcase( pPg->flags & PGHDR_NEED_SYNC );
58354 ** change-counter, stored as a 4-byte big-endian integer starting at
58358 ** But this only happens if the pPager->changeCountDone flag is false.
58368 ** The isDirectMode flag may only be non-zero if the library was compiled
58370 ** if isDirect is non-zero, then the database file is updated directly
58377 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58378 || pPager->eState==PAGER_WRITER_DBMOD
58383 ** atomic-write optimization is enabled in this build, then isDirect
58387 ** The idea is that if the atomic-write optimization is not
58400 if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
58403 assert( !pPager->tempFile && isOpen(pPager->fd) );
58410 ** operating in direct-mode, make page 1 writable. When not in
58412 ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
58425 assert( pPager->dbFileSize>0 );
58426 zBuf = pPgHdr->pData;
58428 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
58429 pPager->aStat[PAGER_STAT_WRITE]++;
58432 /* Update the pager's copy of the change-counter. Otherwise, the
58434 ** flushed (as the change-counter values will not match). */
58436 memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
58437 pPager->changeCountDone = 1;
58440 pPager->changeCountDone = 1;
58451 ** Sync the database file to disk. This is a no-op for in-memory databases
58454 ** If successful, or if called on a pager for which it is a no-op, this
58460 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
58462 if( rc==SQLITE_OK && !pPager->noSync ){
58464 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
58470 ** This function may only be called while a write-transaction is active in
58471 ** rollback. If the connection is in WAL mode, this call is a no-op.
58481 int rc = pPager->errCode;
58484 assert( pPager->eState==PAGER_WRITER_CACHEMOD
58485 || pPager->eState==PAGER_WRITER_DBMOD
58486 || pPager->eState==PAGER_WRITER_LOCKED
58498 ** of a super-journal file that should be written into the individual
58500 ** super-journal (a single database transaction).
58504 ** * The database file change-counter is updated,
58505 ** * the journal is synced (unless the atomic-write optimization is used),
58512 ** delete the super-journal file if specified).
58517 ** If the final parameter - noSync - is true, then the database file itself
58524 const char *zSuper, /* If not NULL, the super-journal name */
58529 assert( pPager->eState==PAGER_WRITER_LOCKED
58530 || pPager->eState==PAGER_WRITER_CACHEMOD
58531 || pPager->eState==PAGER_WRITER_DBMOD
58532 || pPager->eState==PAGER_ERROR
58537 if( NEVER(pPager->errCode) ) return pPager->errCode;
58543 pPager->zFilename, zSuper, pPager->dbSize));
58546 if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
58548 assert( MEMDB==0 || pPager->tempFile );
58549 assert( isOpen(pPager->fd) || pPager->tempFile );
58551 /* If this is an in-memory db, or no pages have been written to, or this
58552 ** function has already been called, it is mostly a no-op. However, any
58554 sqlite3BackupRestart(pPager->pBackup);
58559 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58562 ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
58565 pList->pDirty = 0;
58569 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
58573 sqlite3PcacheCleanAll(pPager->pPCache);
58576 /* The bBatch boolean is true if the batch-atomic-write commit method
58577 ** should be used. No rollback journal is created if batch-atomic-write
58581 sqlite3_file *fd = pPager->fd;
58584 && !pPager->noSync
58585 && sqlite3JournalIsInMemory(pPager->jfd);
58591 /* The following block updates the change-counter. Exactly how it
58592 ** does this depends on whether or not the atomic-update optimization
58596 ** * The file-system supports the atomic-write property for
58597 ** blocks of size page-size, and
58598 ** * This commit is not part of a multi-file transaction, and
58603 ** counter in 'indirect-mode'. If the optimization is compiled in but
58606 ** pager_incr_changecounter() to update the change-counter in indirect
58610 ** then call pager_incr_changecounter() to update the change-counter
58616 assert( isOpen(pPager->jfd)
58617 || pPager->journalMode==PAGER_JOURNALMODE_OFF
58618 || pPager->journalMode==PAGER_JOURNALMODE_WAL
58620 if( !zSuper && isOpen(pPager->jfd)
58621 && pPager->journalOff==jrnlBufferSize(pPager)
58622 && pPager->dbSize>=pPager->dbOrigSize
58623 && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
58625 /* Update the db file change counter via the direct-write method. The
58626 ** following call will modify the in-memory representation of page 1
58628 ** directly to the database file. Because of the atomic-write
58629 ** property of the host file-system, this is safe.
58633 rc = sqlite3JournalCreate(pPager->jfd);
58642 rc = sqlite3JournalCreate(pPager->jfd);
58651 /* Write the super-journal name into the journal file. If a
58652 ** super-journal file name has already been written to the journal file,
58653 ** or if zSuper is NULL (no super-journal), then this call is a no-op.
58659 ** If the atomic-update optimization is being used, this sync will not
58662 ** Because the change-counter page was just modified, unless the
58663 ** atomic-update optimization is used it is almost certain that the
58667 ** xSync() call will be changed to a no-op by the OS anyhow.
58672 pList = sqlite3PcacheDirtyList(pPager->pPCache);
58687 rc = sqlite3JournalCreate(pPager->jfd);
58689 sqlite3OsClose(pPager->jfd);
58694 sqlite3OsClose(pPager->jfd);
58706 sqlite3PcacheCleanAll(pPager->pPCache);
58711 ** last page in the db image moved to the free-list. In this case the
58714 if( pPager->dbSize>pPager->dbFileSize ){
58715 Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
58716 assert( pPager->eState==PAGER_WRITER_DBMOD );
58731 pPager->eState = PAGER_WRITER_FINISHED;
58740 ** synced to disk. The journal file still exists in the file-system
58742 ** be used as a hot-journal and the current transaction rolled back.
58746 ** for hot-journal rollback. Once this is done the transaction is
58758 if( NEVER(pPager->errCode) ) return pPager->errCode;
58759 pPager->iDataVersion++;
58761 assert( pPager->eState==PAGER_WRITER_LOCKED
58762 || pPager->eState==PAGER_WRITER_FINISHED
58763 || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
58768 ** this transaction, the pager is running in exclusive-mode and is
58769 ** using persistent journals, then this function is a no-op.
58773 ** a hot-journal during hot-journal rollback, 0 changes will be made
58778 if( pPager->eState==PAGER_WRITER_LOCKED
58779 && pPager->exclusiveMode
58780 && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
58782 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
58783 pPager->eState = PAGER_READER;
58788 rc = pager_end_transaction(pPager, pPager->setSuper, 1);
58794 ** transaction are reverted and the current write-transaction is closed.
58804 ** in-memory cache pages to the state they were in when the transaction
58813 ** In WAL mode, all cache-entries containing data modified within the
58815 ** their pre-transaction state by re-reading data from the database or
58822 /* PagerRollback() is a no-op if called in READER or OPEN state. If
58827 if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
58828 if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
58832 rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
58833 rc2 = pager_end_transaction(pPager, pPager->setSuper, 0);
58835 }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
58836 int eState = pPager->eState;
58843 pPager->errCode = SQLITE_ABORT;
58844 pPager->eState = PAGER_ERROR;
58852 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
58865 ** Return TRUE if the database file is opened read-only. Return FALSE
58869 return pPager->readOnly;
58877 return sqlite3PcacheRefCount(pPager->pPCache);
58886 int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
58888 return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
58890 + pPager->pageSize;
58906 a[0] = sqlite3PcacheRefCount(pPager->pPCache);
58907 a[1] = sqlite3PcachePagecount(pPager->pPCache);
58908 a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
58909 a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
58910 a[4] = pPager->eState;
58911 a[5] = pPager->errCode;
58912 a[6] = pPager->aStat[PAGER_STAT_HIT];
58913 a[7] = pPager->aStat[PAGER_STAT_MISS];
58914 a[8] = 0; /* Used to be pPager->nOvfl */
58915 a[9] = pPager->nRead;
58916 a[10] = pPager->aStat[PAGER_STAT_WRITE];
58929 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
58945 eStat -= SQLITE_DBSTATUS_CACHE_HIT;
58946 *pnVal += pPager->aStat[eStat];
58948 pPager->aStat[eStat] = 0;
58953 ** Return true if this is an in-memory or temp-file backed pager.
58956 return pPager->tempFile;
58963 ** equal to nSavepoint, then this function is a no-op.
58966 ** occurs while opening the sub-journal file, then an IO error code is
58971 int nCurrent = pPager->nSavepoint; /* Current number of savepoints */
58975 assert( pPager->eState>=PAGER_WRITER_LOCKED );
58977 assert( nSavepoint>nCurrent && pPager->useJournal );
58984 pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
58989 memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
58990 pPager->aSavepoint = aNew;
58994 aNew[ii].nOrig = pPager->dbSize;
58995 if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
58996 aNew[ii].iOffset = pPager->journalOff;
59000 aNew[ii].iSubRec = pPager->nSubRec;
59001 aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
59007 sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
59009 pPager->nSavepoint = ii+1;
59011 assert( pPager->nSavepoint==nSavepoint );
59016 assert( pPager->eState>=PAGER_WRITER_LOCKED );
59019 if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
59039 ** (the first created). A value of (Pager.nSavepoint-1) means operate
59041 ** (Pager.nSavepoint-1), then this function is a no-op.
59058 int rc = pPager->errCode;
59067 if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
59076 for(ii=nNew; ii<pPager->nSavepoint; ii++){
59077 sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
59079 pPager->nSavepoint = nNew;
59082 ** the sub-journal to zero bytes in size. */
59084 PagerSavepoint *pRel = &pPager->aSavepoint[nNew];
59085 if( pRel->bTruncateOnRelease && isOpen(pPager->sjfd) ){
59086 /* Only truncate if it is an in-memory sub-journal. */
59087 if( sqlite3JournalIsInMemory(pPager->sjfd) ){
59088 i64 sz = (pPager->pageSize+4)*pRel->iSubRec;
59089 rc = sqlite3OsTruncate(pPager->sjfd, sz);
59092 pPager->nSubRec = pRel->iSubRec;
59096 ** If this is a temp-file, it is possible that the journal file has
59100 else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
59101 PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
59112 pPager->journalMode==PAGER_JOURNALMODE_OFF
59113 && pPager->eState>=PAGER_WRITER_CACHEMOD
59115 pPager->errCode = SQLITE_ABORT;
59116 pPager->eState = PAGER_ERROR;
59128 ** Except, if the pager is in-memory only, then return an empty string if
59132 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
59133 ** participate in shared-cache.
59140 return (nullIfMemDb && pPager->memDb) ? &zFake[4] : pPager->zFilename;
59147 return pPager->pVfs;
59156 return pPager->fd;
59165 return pPager->jfd;
59167 return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
59175 return pPager->zJournal;
59188 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
59196 ** If the fourth argument, isCommit, is non-zero, then this page is being
59206 Pgno needSyncPgno = 0; /* Old value of pPg->pgno, if sync is required */
59210 assert( pPg->nRef>0 );
59211 assert( pPager->eState==PAGER_WRITER_CACHEMOD
59212 || pPager->eState==PAGER_WRITER_DBMOD
59216 /* In order to be able to rollback, an in-memory database must journal
59219 assert( pPager->tempFile || !MEMDB );
59220 if( pPager->tempFile ){
59227 ** sub-journal now. This is required to handle the following scenario:
59235 ** If page X were not written to the sub-journal here, it would not
59239 ** subjournalPage() may need to allocate space to store pPg->pgno into
59243 if( (pPg->flags & PGHDR_DIRTY)!=0
59250 PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
59251 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
59253 /* If the journal needs to be sync()ed before page pPg->pgno can
59254 ** be written to, store pPg->pgno in local variable needSyncPgno.
59257 ** the journal needs to be sync()ed before database page pPg->pgno
59260 if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
59261 needSyncPgno = pPg->pgno;
59262 assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
59263 pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
59264 assert( pPg->flags&PGHDR_DIRTY );
59267 /* If the cache contains a page with page-number pgno, remove it
59272 pPg->flags &= ~PGHDR_NEED_SYNC;
59274 assert( !pPgOld || pPgOld->nRef==1 || CORRUPT_DB );
59276 if( pPgOld->nRef>1 ){
59280 pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
59281 if( pPager->tempFile ){
59282 /* Do not discard pages from an in-memory database since we might
59284 sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
59290 origPgno = pPg->pgno;
59294 /* For an in-memory database, make sure the original page continues
59298 if( pPager->tempFile && pPgOld ){
59304 /* If needSyncPgno is non-zero, then the journal file needs to be
59306 ** Currently, no such page exists in the page-cache and the
59308 ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
59311 ** If the attempt to load the page into the page-cache fails, (due
59321 if( needSyncPgno<=pPager->dbOrigSize ){
59322 assert( pPager->pTmpSpace!=0 );
59323 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
59327 pPgHdr->flags |= PGHDR_NEED_SYNC;
59343 assert( pPg->pgno!=iNew );
59344 pPg->flags = flags;
59352 assert( pPg->nRef>0 || pPg->pPager->memDb );
59353 return pPg->pData;
59361 return pPg->pExtra;
59365 ** Get/set the locking-mode for this pager. Parameter eMode must be one
59368 ** the locking-mode is set to the value specified.
59372 ** locking-mode.
59380 assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
59381 if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
59382 pPager->exclusiveMode = (u8)eMode;
59384 return (int)pPager->exclusiveMode;
59388 ** Set the journal-mode for this pager. Parameter eMode must be one of:
59400 ** * An in-memory database can only have its journal_mode set to _OFF
59405 ** The returned indicate the current (possibly updated) journal-mode.
59408 u8 eOld = pPager->journalMode; /* Prior journalmode */
59422 assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
59424 /* Do allow the journalmode of an in-memory database to be set to
59437 assert( pPager->eState!=PAGER_ERROR );
59438 pPager->journalMode = (u8)eMode;
59451 assert( isOpen(pPager->fd) || pPager->exclusiveMode );
59452 if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
59462 sqlite3OsClose(pPager->jfd);
59463 if( pPager->eLock>=RESERVED_LOCK ){
59464 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
59467 int state = pPager->eState;
59472 if( pPager->eState==PAGER_READER ){
59477 sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
59484 assert( state==pPager->eState );
59487 sqlite3OsClose(pPager->jfd);
59492 return (int)pPager->journalMode;
59499 return (int)pPager->journalMode;
59509 if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
59510 if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
59515 ** Get/set the size-limit used for persistent journal files.
59517 ** Setting the size limit to -1 means no limit is enforced.
59518 ** An attempt to set a limit smaller than -1 is a no-op.
59521 if( iLimit>=-1 ){
59522 pPager->journalSizeLimit = iLimit;
59523 sqlite3WalLimit(pPager->pWal, iLimit);
59525 return pPager->journalSizeLimit;
59529 ** Return a pointer to the pPager->pBackup variable. The backup module
59535 return &pPager->pBackup;
59540 ** Unless this is an in-memory or temporary database, clear the pager cache.
59543 assert( MEMDB==0 || pPager->tempFile );
59544 if( pPager->tempFile==0 ) pager_reset(pPager);
59565 if( pPager->pWal ){
59566 rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
59567 (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
59568 pPager->pBusyHandlerArg,
59569 pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
59577 return sqlite3WalCallback(pPager->pWal);
59582 ** primitives necessary for write-ahead logging.
59585 const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
59586 if( pPager->noLock ) return 0;
59587 return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
59597 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59610 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
59611 ** lock on the database file and use heap-memory to store the wal-index
59612 ** in. Otherwise, use the normal shared-memory.
59617 assert( pPager->pWal==0 && pPager->tempFile==0 );
59618 assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
59620 /* If the pager is already in exclusive-mode, the WAL module will use
59621 ** heap-memory for the wal-index instead of the VFS shared-memory
59625 if( pPager->exclusiveMode ){
59633 rc = sqlite3WalOpen(pPager->pVfs,
59634 pPager->fd, pPager->zWal, pPager->exclusiveMode,
59635 pPager->journalSizeLimit, &pPager->pWal
59649 ** file (not a temp file or an in-memory database), and the WAL file
59655 ** If the pager is open on a temp-file (or in-memory database), or if
59661 int *pbOpen /* OUT: Set to true if call is a no-op */
59666 assert( pPager->eState==PAGER_OPEN || pbOpen );
59667 assert( pPager->eState==PAGER_READER || !pbOpen );
59669 assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
59671 if( !pPager->tempFile && !pPager->pWal ){
59675 sqlite3OsClose(pPager->jfd);
59679 pPager->journalMode = PAGER_JOURNALMODE_WAL;
59680 pPager->eState = PAGER_OPEN;
59701 assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
59703 /* If the log file is not already open, but does exist in the file-system,
59707 if( !pPager->pWal ){
59712 pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
59721 ** the database file, the log and log-summary files will be deleted.
59723 if( rc==SQLITE_OK && pPager->pWal ){
59726 rc = sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags,
59727 pPager->pageSize, (u8*)pPager->pTmpSpace);
59728 pPager->pWal = 0;
59730 if( rc && !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
59738 ** If pager pPager is a wal-mode database not in exclusive locking mode,
59745 if( pagerUseWal(pPager) && pPager->exclusiveMode==0 ){
59746 rc = sqlite3WalWriteLock(pPager->pWal, bLock);
59757 sqlite3WalDb(pPager->pWal, db);
59769 if( pPager->pWal ){
59770 rc = sqlite3WalSnapshotGet(pPager->pWal, ppSnapshot);
59785 if( pPager->pWal ){
59786 sqlite3WalSnapshotOpen(pPager->pWal, pSnapshot);
59799 if( pPager->pWal ){
59800 rc = sqlite3WalSnapshotRecover(pPager->pWal);
59821 if( pPager->pWal ){
59822 rc = sqlite3WalSnapshotCheck(pPager->pWal, pSnapshot);
59834 assert( pPager->pWal );
59835 sqlite3WalSnapshotUnlock(pPager->pWal);
59843 ** A read-lock must be held on the pager when this function is called. If
59850 assert( pPager->eState>=PAGER_READER );
59851 return sqlite3WalFramesize(pPager->pWal);
59871 ** This file contains the implementation of a write-ahead log (WAL) used in
59874 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
59893 ** big-endian 32-bit unsigned integer values:
59899 ** 16: Salt-1, random integer incremented with each checkpoint
59900 ** 20: Salt-2, a different random integer changing with each ckpt
59901 ** 24: Checksum-1 (first part of checksum for first 24 bytes of header).
59902 ** 28: Checksum-2 (second part of checksum for first 24 bytes of header).
59904 ** Immediately following the wal-header are zero or more frames. Each
59905 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
59906 ** of page data. The frame-header is six big-endian 32-bit unsigned
59912 ** 8: Salt-1 (copied from the header)
59913 ** 12: Salt-2 (copied from the header)
59914 ** 16: Checksum-1.
59915 ** 20: Checksum-2.
59920 ** (1) The salt-1 and salt-2 values in the frame-header match
59921 ** salt values in the wal-header
59923 ** (2) The checksum values in the final 8 bytes of the frame-header
59928 ** The checksum is computed using 32-bit big-endian integers if the
59930 ** is computed using little-endian if the magic number is 0x377f0682.
59932 ** big-endian format regardless of which byte order is used to compute
59934 ** an even number of unsigned 32-bit integers: x[0] through x[N]. The
59937 ** for i from 0 to n-1 step 2:
59944 ** of the sequence being summed.) The s1 value spans all 32-bit
59947 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
59948 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
59949 ** The VFS.xSync operations serve as write barriers - all writes launched
59953 ** After each checkpoint, the salt-1 value is incremented and the salt-2
59982 ** data structure called the wal-index is maintained to expedite the
59985 ** WAL-INDEX FORMAT
59987 ** Conceptually, the wal-index is shared memory, though VFS implementations
59988 ** might choose to implement the wal-index using a mmapped file. Because
59989 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
59993 ** In the default unix and windows implementation, the wal-index is a mmapped
59994 ** file whose name is the database name with a "-shm" suffix added. For that
59995 ** reason, the wal-index is sometimes called the "shm" file.
59997 ** The wal-index is transient. After a crash, the wal-index can (and should
59999 ** to either truncate or zero the header of the wal-index when the last
60000 ** connection to it closes. Because the wal-index is transient, it can
60001 ** use an architecture-specific format; it does not have to be cross-platform.
60003 ** as big endian, the wal-index can store multi-byte values in the native
60006 ** The purpose of the wal-index is to answer this question quickly: Given
60011 ** The wal-index consists of a header region, followed by an one or
60014 ** The wal-index header contains the total number of frames within the WAL
60020 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
60022 ** wal-index.
60024 ** Each index block contains two sections, a page-mapping that contains the
60025 ** database page number associated with each wal frame, and a hash-table
60027 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
60028 ** for the first index block) 32-bit page numbers. The first entry in the
60029 ** first index-block contains the database page number corresponding to the
60034 ** The last index block in a wal-index usually contains less than the full
60035 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
60037 ** allocated size of the page-mapping array - the page-mapping array merely
60041 ** can be found by scanning the page-mapping sections of each index block
60047 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
60052 ** 1-based index of an entry in the mapping section of the same
60053 ** index block. Let K be the 1-based index of the largest entry in
60068 ** wrap-around.) Because the hash table is never more than half full,
60073 ** current index block. Otherwise the iMax-th mapping entry of the
60091 ** and to the wal-index) might be using a different value K1, where K1>K0.
60097 ** in the first place - which is what reader one wants. Meanwhile, the
60120 ** The maximum (and only) versions of the wal and wal-index formats
60124 ** values in the wal-header are correct and (b) the version field is not
60127 ** Similarly, if a client successfully reads a wal-index header (i.e. the
60129 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
60144 ** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
60154 #define WAL_NREADER (SQLITE_SHM_NLOCK-3)
60164 ** The following object holds a copy of the wal-index header content.
60166 ** The actual header in the wal-index consists of two copies of this
60173 ** Or it can be 1 to represent a 65536-byte page. The latter case was
60177 u32 iVersion; /* Wal-index version */
60181 u8 bigEndCksum; /* True if checksums in WAL are big-endian */
60191 ** A copy of the following object occurs in the wal-index immediately
60214 ** holds read-lock K, then the value in aReadMark[K] is no greater than
60217 ** a special case; its value is never used and it exists as a place-holder
60246 ** We assume that 32-bit loads are atomic and so no locks are needed in
60261 ** only support mandatory file-locks, we do not read or write data
60274 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
60275 ** big-endian format in the first 4 bytes of a WAL file.
60278 ** file are calculated by treating all data as an array of 32-bit
60279 ** big-endian words. Otherwise, they are calculated by interpreting
60280 ** all data as 32-bit little-endian words.
60285 ** Return the offset of frame iFrame in the write-ahead log file,
60287 ** is to the start of the write-ahead log frame-header.
60290 WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE) \
60294 ** An open write-ahead log file is represented by an instance of the
60305 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
60307 i16 readLock; /* Which read lock is being held. -1 for none */
60309 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
60316 u8 bShmUnreliable; /* SHM content is read-only and unreliable */
60317 WalIndexHdr hdr; /* Wal-index header for current transaction */
60321 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
60348 ** Each page of the wal-index mapping contains a hash-table made up of
60362 ** walIteratorInit() - Create a new iterator,
60363 ** walIteratorNext() - Step an iterator,
60364 ** walIteratorFree() - Free an iterator.
60377 } aSegment[1]; /* One for every 32KB page in the wal-index */
60381 ** Define the parameters of the hash tables in the wal-index file. There
60382 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
60383 ** wal-index.
60385 ** Changing any of these constants will alter the wal-index format and
60393 ** The block of page numbers associated with the first hash-table in a
60394 ** wal-index is smaller than usual. This is so that there is a complete
60395 ** hash-table on each aligned 32KB page of the wal-index.
60397 #define HASHTABLE_NPAGE_ONE (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
60399 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
60405 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
60406 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
60409 ** If the wal-index is currently smaller the iPage pages then the size
60410 ** of the wal-index might be increased, but only if it is safe to do
60411 ** so. It is safe to enlarge the wal-index if pWal->writeLock is true
60412 ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
60414 ** If this call is successful, *ppPage is set to point to the wal-index
60425 /* Enlarge the pWal->apWiData[] array if required */
60426 if( pWal->nWiData<=iPage ){
60429 apNew = (volatile u32 **)sqlite3Realloc((void *)pWal->apWiData, nByte);
60434 memset((void*)&apNew[pWal->nWiData], 0,
60435 sizeof(u32*)*(iPage+1-pWal->nWiData));
60436 pWal->apWiData = apNew;
60437 pWal->nWiData = iPage+1;
60441 assert( pWal->apWiData[iPage]==0 );
60442 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
60443 pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
60444 if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
60446 rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
60447 pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
60449 assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
60450 testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
60454 pWal->readOnly |= WAL_SHM_RDONLY;
60461 *ppPage = pWal->apWiData[iPage];
60470 if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
60477 ** Return a pointer to the WalCkptInfo structure in the wal-index.
60480 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60481 return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
60485 ** Return a pointer to the WalIndexHdr structure in the wal-index.
60488 assert( pWal->nWiData>0 && pWal->apWiData[0] );
60489 return (volatile WalIndexHdr*)pWal->apWiData[0];
60493 ** The argument to this macro must be of type u32. On a little-endian
60495 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
60497 ** of the input value as a little-endian integer.
60514 int nativeCksum, /* True for native byte-order, false for non-native */
60557 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
60558 sqlite3OsShmBarrier(pWal->pDbFd);
60563 ** Add the SQLITE_NO_TSAN as part of the return-type of a function
60565 ** might give false-positive TSAN warnings.
60567 ** See tag-20200519-1.
60576 ** Write the header information in pWal->hdr into the wal-index.
60578 ** The checksum on pWal->hdr is updated before it is written.
60584 assert( pWal->writeLock );
60585 pWal->hdr.isInit = 1;
60586 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
60587 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
60588 /* Possible TSAN false-positive. See tag-20200519-1 */
60589 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60591 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
60596 ** supplied by the caller. A frame-header is made up of a series of
60597 ** 4-byte big-endian integers, as follows:
60602 ** 8: Salt-1 (copied from the wal-header)
60603 ** 12: Salt-2 (copied from the wal-header)
60604 ** 16: Checksum-1.
60605 ** 20: Checksum-2.
60608 Wal *pWal, /* The write-ahead log */
60610 u32 nTruncate, /* New db size (or 0 for non-commit frames) */
60614 int nativeCksum; /* True for native byte-order checksums */
60615 u32 *aCksum = pWal->hdr.aFrameCksum;
60619 if( pWal->iReCksum==0 ){
60620 memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
60622 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60624 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60639 Wal *pWal, /* The write-ahead log */
60645 int nativeCksum; /* True for native byte-order checksums */
60646 u32 *aCksum = pWal->hdr.aFrameCksum;
60650 /* A frame is only valid if the salt values in the frame-header
60651 ** match the salt values in the wal-header.
60653 if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
60665 ** all prior frams, the first 16 bytes of this frame-header,
60666 ** and the frame-data matches the checksum in the last 8
60667 ** bytes of this frame-header.
60669 nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
60671 walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
60695 return "WRITE-LOCK";
60697 return "CKPT-LOCK";
60699 return "RECOVER-LOCK";
60702 sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
60703 lockIdx-WAL_READ_LOCK(0));
60712 ** A lock cannot be moved directly between shared and exclusive - it must go
60715 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
60719 if( pWal->exclusiveMode ) return SQLITE_OK;
60720 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60722 WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
60724 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60728 if( pWal->exclusiveMode ) return;
60729 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
60731 WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
60735 if( pWal->exclusiveMode ) return SQLITE_OK;
60736 rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60738 WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
60740 VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && (rc&0xFF)!=SQLITE_BUSY); )
60744 if( pWal->exclusiveMode ) return;
60745 (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
60747 WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
60753 ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances
60758 assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
60759 return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
60762 return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
60767 ** of a page hash table in the wal-index. This becomes the return value
60772 volatile ht_slot *aHash; /* Start of the wal-index hash table */
60779 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
60782 ** Set output variable pLoc->aHash to point to the start of the hash table
60783 ** in the wal-index file. Set pLoc->iZero to one less than the frame
60786 ** (pLoc->iZero+N) in the log.
60788 ** Finally, set pLoc->aPgno so that pLoc->aPgno[1] is the page number of the
60789 ** first frame indexed by the hash table, frame (pLoc->iZero+1).
60798 rc = walIndexPage(pWal, iHash, &pLoc->aPgno);
60802 pLoc->aHash = (volatile ht_slot *)&pLoc->aPgno[HASHTABLE_NPAGE];
60804 pLoc->aPgno = &pLoc->aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
60805 pLoc->iZero = 0;
60807 pLoc->iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
60809 pLoc->aPgno = &pLoc->aPgno[-1];
60815 ** Return the number of the wal-index page that contains the hash-table
60816 ** and page-number array that contain entries corresponding to WAL frame
60817 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
60821 int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
60838 return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
60840 return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
60845 ** than pWal->hdr.mxFrame.
60847 ** This function is called whenever pWal->hdr.mxFrame is decreased due
60850 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
60852 ** pWal->hdr.mxFrame advances to the point where those hash tables are
60862 assert( pWal->writeLock );
60863 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
60864 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
60865 testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
60867 if( pWal->hdr.mxFrame==0 ) return;
60869 /* Obtain pointers to the hash-table and page-number array containing
60870 ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
60871 ** that the page said hash-table and array reside on is already mapped.(1)
60873 assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
60874 assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
60875 rc = walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &sLoc);
60876 if( NEVER(rc) ) return; /* Defense-in-depth, in case (1) above is wrong */
60878 /* Zero all hash-table entries that correspond to frame numbers greater
60879 ** than pWal->hdr.mxFrame.
60881 iLimit = pWal->hdr.mxFrame - sLoc.iZero;
60890 ** frame numbers greater than pWal->hdr.mxFrame.
60892 nByte = (int)((char *)sLoc.aHash - (char *)&sLoc.aPgno[iLimit+1]);
60914 ** Set an entry in the wal-index that will map database page number
60919 WalHashLoc sLoc; /* Wal-index hash table location */
60923 /* Assuming the wal-index file was successfully mapped, populate the
60928 int idx; /* Value to write to hash-table slot */
60931 idx = iFrame - sLoc.iZero;
60934 /* If this is the first entry to be added to this hash-table, zero the
60939 - (u8 *)&sLoc.aPgno[1]);
60947 ** the hash-table before writing any new entries.
60954 /* Write the aPgno[] array entry and the hash-table slot. */
60957 if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
60975 ** thing to check, so only do this occasionally - not on every
60998 ** Recover the wal-index by reading the write-ahead log file.
61001 ** wal-index to prevent other threads/processes from doing anything
61002 ** with the WAL or wal-index while recovery is running. The
61019 assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
61022 assert( pWal->writeLock );
61023 iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
61024 rc = walLockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
61031 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
61033 rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
61040 u32 *aPrivate = 0; /* Heap copy of *-shm hash being populated */
61048 u32 iPg; /* Current 32KB wal-index page */
61052 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
61065 || szPage&(szPage-1)
61071 pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
61072 pWal->szPage = szPage;
61073 pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
61074 memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
61077 walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
61078 aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
61080 if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
61081 || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
61105 iLastFrame = (nSize - WAL_HDRSIZE) / szFrame;
61110 u32 iFirst = 1 + (iPg==0?0:HASHTABLE_NPAGE_ONE+(iPg-1)*HASHTABLE_NPAGE);
61114 pWal->apWiData[iPg] = aPrivate;
61122 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
61129 /* If nTruncate is non-zero, this is a commit record. */
61131 pWal->hdr.mxFrame = iFrame;
61132 pWal->hdr.nPage = nTruncate;
61133 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
61136 aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
61137 aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
61140 pWal->apWiData[iPg] = aShare;
61152 memcpy(&aShare[nHdr32], &aPrivate[nHdr32], WALINDEX_PGSZ-nHdr);
61181 pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
61182 pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
61185 /* Reset the checkpoint-header. This is safe because this thread is
61187 ** checkpointers. Then set the values of read-mark slots 1 through N.
61190 pInfo->nBackfill = 0;
61191 pInfo->nBackfillAttempted = pWal->hdr.mxFrame;
61192 pInfo->aReadMark[0] = 0;
61196 if( i==1 && pWal->hdr.mxFrame ){
61197 pInfo->aReadMark[i] = pWal->hdr.mxFrame;
61199 pInfo->aReadMark[i] = READMARK_NOT_USED;
61212 if( pWal->hdr.nPage ){
61215 pWal->hdr.mxFrame, pWal->zWalName
61222 walUnlockExclusive(pWal, iLock, WAL_READ_LOCK(0)-iLock);
61227 ** Close an open wal-index.
61230 if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE || pWal->bShmUnreliable ){
61232 for(i=0; i<pWal->nWiData; i++){
61233 sqlite3_free((void *)pWal->apWiData[i]);
61234 pWal->apWiData[i] = 0;
61237 if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
61238 sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
61249 ** client from unlinking the WAL or wal-index file. If another process
61258 sqlite3_vfs *pVfs, /* vfs module to open wal and wal-index */
61261 int bNoShm, /* True to run in heap-memory mode */
61290 pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
61295 pRet->pVfs = pVfs;
61296 pRet->pWalFd = (sqlite3_file *)&pRet[1];
61297 pRet->pDbFd = pDbFd;
61298 pRet->readLock = -1;
61299 pRet->mxWalSize = mxWalSize;
61300 pRet->zWalName = zWalName;
61301 pRet->syncHeader = 1;
61302 pRet->padToSectorBoundary = 1;
61303 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
61305 /* Open file handle on the write-ahead log file. */
61307 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
61309 pRet->readOnly = WAL_RDONLY;
61314 sqlite3OsClose(pRet->pWalFd);
61318 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
61320 pRet->padToSectorBoundary = 0;
61332 if( pWal ) pWal->mxWalSize = iLimit;
61354 iMin = p->iPrior;
61356 for(i=p->nSegment-1; i>=0; i--){
61357 struct WalSegment *pSegment = &p->aSegment[i];
61358 while( pSegment->iNext<pSegment->nEntry ){
61359 u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
61363 *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
61367 pSegment->iNext++;
61371 *piPage = p->iPrior = iRet;
61399 const u32 *aContent, /* Pages in wal - keys for the sort */
61463 ht_slot *aList; /* Pointer to sub-list content */
61471 struct Sublist aSub[13]; /* Array of sub-lists */
61475 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
61484 assert( p->aList && p->nList<=(1<<iSub) );
61485 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
61486 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
61497 assert( p->nList<=(1<<iSub) );
61498 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
61499 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
61509 assert( aContent[aList[i]] > aContent[aList[i-1]] );
61525 ** nBackfill or earlier may be included - excluding them is an optimization
61541 ht_slot *aTmp; /* Temp space used by merge-sort */
61547 assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
61548 iLast = pWal->hdr.mxFrame;
61553 + (nSegment-1)*sizeof(struct WalSegment)
61560 p->nSegment = nSegment;
61562 /* Allocate temporary space used by the merge-sort routine. This block
61583 nEntry = (int)(iLast - sLoc.iZero);
61585 nEntry = (int)((u32*)sLoc.aHash - (u32*)sLoc.aPgno);
61587 aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[sLoc.iZero];
61594 p->aSegment[i].iZero = sLoc.iZero;
61595 p->aSegment[i].nEntry = nEntry;
61596 p->aSegment[i].aIndex = aIndex;
61597 p->aSegment[i].aPgno = (u32 *)sLoc.aPgno;
61614 ** with a busy-timeout. Return 1 if blocking locks are successfully enabled,
61619 if( pWal->db ){
61620 int tmout = pWal->db->busyTimeout;
61624 pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout
61637 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_LOCK_TIMEOUT, (void*)&tmout);
61651 assert( pWal->readLock<0 || bLock==0 );
61653 assert( pWal->db );
61657 pWal->writeLock = 1;
61661 }else if( pWal->writeLock ){
61663 pWal->writeLock = 0;
61672 pWal->db = db;
61696 ** busy-handler function. Invoke it and retry the lock until either the
61697 ** lock is successfully obtained or the busy-handler returns 0.
61720 ** The cache of the wal-index header must be valid to call this function.
61721 ** Return the page-size in bytes used by the database.
61724 return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
61733 ** file - there are no readers that may attempt to read a frame from
61736 ** This function updates the shared-memory structures so that the next
61741 ** new wal-index header. It should be passed a pseudo-random value (i.e.
61747 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
61748 pWal->nCkpt++;
61749 pWal->hdr.mxFrame = 0;
61751 memcpy(&pWal->hdr.aSalt[1], &salt1, 4);
61753 AtomicStore(&pInfo->nBackfill, 0);
61754 pInfo->nBackfillAttempted = 0;
61755 pInfo->aReadMark[1] = 0;
61756 for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
61757 assert( pInfo->aReadMark[0]==0 );
61769 ** SQLite is in WAL-mode in synchronous=NORMAL. That means that if
61775 ** in the WAL and can be recovered following a power-loss or hard reset.
61782 ** This routine uses and updates the nBackfill field of the wal-index header.
61801 int szPage; /* Database page-size */
61814 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61816 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
61825 mxSafeFrame = pWal->hdr.mxFrame;
61826 mxPage = pWal->hdr.nPage;
61828 u32 y = AtomicLoad(pInfo->aReadMark+i);
61830 assert( y<=pWal->hdr.mxFrame );
61834 AtomicStore(pInfo->aReadMark+i, iMark);
61846 if( pInfo->nBackfill<mxSafeFrame ){
61847 rc = walIteratorInit(pWal, pInfo->nBackfill, &pIter);
61854 u32 nBackfill = pInfo->nBackfill;
61856 pInfo->nBackfillAttempted = mxSafeFrame;
61859 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
61867 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_START, 0);
61868 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
61870 if( (nSize+65536+(i64)pWal->hdr.mxFrame*szPage)<nReq ){
61873 ** maximum size of the pending-byte page (65536 bytes), then
61877 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT,&nReq);
61887 if( AtomicLoad(&db->u1.isInterrupted) ){
61888 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
61896 rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
61898 iOffset = (iDbpage-1)*(i64)szPage;
61900 rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
61903 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_CKPT_DONE, 0);
61907 if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
61908 i64 szDb = pWal->hdr.nPage*(i64)szPage;
61910 rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
61912 rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags));
61916 AtomicStore(&pInfo->nBackfill, mxSafeFrame);
61937 assert( pWal->writeLock );
61938 if( pInfo->nBackfill<pWal->hdr.mxFrame ){
61943 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
61944 rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
61947 /* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
61953 ** wal-index header in shared memory, as all subsequent reader or
61957 ** the wal-index header do not match the contents of the
61958 ** file-system. To avoid this, update the wal-index header to
61961 rc = sqlite3OsTruncate(pWal->pWalFd, 0);
61963 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
61981 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
61983 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
61987 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
62003 int isDelete = 0; /* True to unlink wal and wal-index files */
62006 ** ordinary, rollback-mode locking methods, this guarantees that the
62009 ** the wal and wal-index files.
62014 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE))
62016 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
62017 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
62023 int bPersist = -1;
62025 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
62029 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
62032 }else if( pWal->mxWalSize>=0 ){
62036 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
62045 sqlite3OsClose(pWal->pWalFd);
62048 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
62052 sqlite3_free((void *)pWal->apWiData);
62059 ** Try to read the wal-index header. Return 0 on success and 1 if
62062 ** The wal-index is in shared memory. Another thread or process might
62069 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
62072 ** If the checksum cannot be verified return non-zero. If the header
62080 /* The first page of the wal-index must be mapped at this point. */
62081 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62086 ** from the file. If this happens, return non-zero.
62088 ** tag-20200519-1:
62089 ** There are two copies of the header at the beginning of the wal-index.
62093 ** give false-positive warnings about these accesses because the tools do not
62094 ** account for the double-read and the memory barrier. The use of mutexes
62100 memcpy(&h1, (void *)&aHdr[0], sizeof(h1)); /* Possible TSAN false-positive */
62108 return 1; /* Malformed header - probably all zeros */
62110 walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
62115 if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
62117 memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
62118 pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
62119 testcase( pWal->szPage<=32768 );
62120 testcase( pWal->szPage>=65536 );
62131 #define WAL_RETRY (-1)
62134 ** Read the wal-index header from the wal-index and into pWal->hdr.
62135 ** If the wal-header appears to be corrupt, try to reconstruct the
62136 ** wal-index from the WAL before returning.
62138 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
62139 ** changed by this operation. If pWal->hdr is unchanged, set *pChanged
62142 ** If the wal-index header is successfully read, return SQLITE_OK.
62148 volatile u32 *page0; /* Chunk of wal-index containing header */
62150 /* Ensure that page 0 of the wal-index (the page that contains the
62151 ** wal-index header) is mapped. Return early if an error occurs here.
62158 /* The SQLITE_READONLY_CANTINIT return means that the shared-memory
62160 ** confirm that another write-capable connection has the shared-memory
62161 ** open, and hence the content of the shared-memory is unreliable,
62162 ** since the shared-memory might be inconsistent with the WAL file
62165 assert( pWal->writeLock==0 );
62166 assert( pWal->readOnly & WAL_SHM_RDONLY );
62167 pWal->bShmUnreliable = 1;
62168 pWal->exclusiveMode = WAL_HEAPMEMORY_MODE;
62171 return rc; /* Any other non-OK return is just an error */
62174 /* page0 can be NULL if the SHM is zero bytes in size and pWal->writeLock
62178 assert( page0!=0 || pWal->writeLock==0 );
62180 /* If the first page of the wal-index has been mapped, try to read the
62181 ** wal-index header immediately, without holding any lock. This usually
62182 ** works, but may fail if the wal-index header is corrupt or currently
62191 if( pWal->bShmUnreliable==0 && (pWal->readOnly & WAL_SHM_RDONLY) ){
62197 int bWriteLock = pWal->writeLock;
62199 pWal->writeLock = 1;
62203 /* If the wal-index header is still malformed even while holding
62212 pWal->writeLock = 0;
62220 ** sure the wal-index was not constructed with some future format that
62223 if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
62226 if( pWal->bShmUnreliable ){
62229 pWal->bShmUnreliable = 0;
62230 assert( pWal->nWiData>0 && pWal->apWiData[0]==0 );
62236 pWal->exclusiveMode = WAL_NORMAL_MODE;
62243 ** Open a transaction in a connection where the shared-memory is read-only
62244 ** and where we cannot verify that there is a separate write-capable connection
62245 ** on hand to keep the shared-memory up-to-date with the WAL file.
62247 ** This can happen, for example, when the shared-memory is implemented by
62248 ** memory-mapping a *-shm file, where a prior writer has shut down and
62249 ** left the *-shm file on disk, and now the present connection is trying
62250 ** to use that database but lacks write permission on the *-shm file.
62255 ** The *-wal file has been read and an appropriate wal-index has been
62256 ** constructed in pWal->apWiData[] using heap memory instead of shared
62263 ** WAL_RETRY, then the heap memory wal-index has been discarded and
62265 ** beginning (including attempting to map the *-shm file).
62278 u32 aSaveCksum[2]; /* Saved copy of pWal->hdr.aFrameCksum */
62280 assert( pWal->bShmUnreliable );
62281 assert( pWal->readOnly & WAL_SHM_RDONLY );
62282 assert( pWal->nWiData>0 && pWal->apWiData[0] );
62292 pWal->readLock = 0;
62294 /* Check to see if a separate writer has attached to the shared-memory area,
62295 ** thus making the shared-memory "reliable" again. Do this by invoking
62299 ** If the shared-memory is now "reliable" return WAL_RETRY, which will
62300 ** cause the heap-memory WAL-index to be discarded and the actual
62312 ** even if some external agent does a "chmod" to make the shared-memory
62316 rc = sqlite3OsShmMap(pWal->pDbFd, 0, WALINDEX_PGSZ, 0, &pDummy);
62317 assert( rc!=SQLITE_OK ); /* SQLITE_OK not possible for read-only connection */
62323 /* We reach this point only if the real shared-memory is still unreliable.
62324 ** Assume the in-memory WAL-index substitute is correct and load it
62325 ** into pWal->hdr.
62327 memcpy(&pWal->hdr, (void*)walIndexHdr(pWal), sizeof(WalIndexHdr));
62332 rc = sqlite3OsFileSize(pWal->pWalFd, &szWal);
62337 /* If the wal file is too small to contain a wal-header and the
62338 ** wal-index header has mxFrame==0, then it must be safe to proceed
62344 rc = (pWal->hdr.mxFrame==0 ? SQLITE_OK : WAL_RETRY);
62349 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
62353 if( memcmp(&pWal->hdr.aSalt, &aBuf[16], 8) ){
62355 ** Return WAL_RETRY which will cause the in-memory WAL-index to be
62362 szFrame = pWal->hdr.szPage + WAL_FRAME_HDRSIZE;
62371 ** wal file since the heap-memory wal-index was created. If so, the
62372 ** heap-memory wal-index is discarded and WAL_RETRY returned to
62374 aSaveCksum[0] = pWal->hdr.aFrameCksum[0];
62375 aSaveCksum[1] = pWal->hdr.aFrameCksum[1];
62376 for(iOffset=walFrameOffset(pWal->hdr.mxFrame+1, pWal->hdr.szPage);
62384 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
62388 /* If nTruncate is non-zero, then a complete transaction has been
62396 pWal->hdr.aFrameCksum[0] = aSaveCksum[0];
62397 pWal->hdr.aFrameCksum[1] = aSaveCksum[1];
62403 for(i=0; i<pWal->nWiData; i++){
62404 sqlite3_free((void*)pWal->apWiData[i]);
62405 pWal->apWiData[i] = 0;
62407 pWal->bShmUnreliable = 0;
62426 ** to make a copy of the wal-index header into pWal->hdr. If the
62427 ** wal-index header has changed, *pChanged is set to 1 (as an indication
62429 ** flushed.) When useWal==1, the wal-index header is assumed to already
62440 ** bad luck when there is lots of contention for the wal-index, but that
62444 ** WAL_READ_LOCK(pWal->readLock). The pWal->readLock integer is
62445 ** in the range 0 <= pWal->readLock < WAL_NREADER. If pWal->readLock==(-1)
62448 ** including frame number aReadMark[pWal->readLock]. The reader will
62449 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
62450 ** Or if pWal->readLock==0, then the reader will ignore the WAL
62453 ** this routine will always set pWal->readLock>0 on success.
62455 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
62465 volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */
62472 assert( pWal->readLock<0 ); /* Not currently locked */
62475 assert( (pWal->readOnly & WAL_SHM_RDONLY)==0 || useWal==0 );
62483 ** paged out or take a page-fault that is time-consuming to resolve,
62497 VVA_ONLY( pWal->lockError = 1; )
62500 if( cnt>=10 ) nDelay = (cnt-9)*(cnt-9)*39;
62501 sqlite3OsSleep(pWal->pVfs, nDelay);
62506 if( pWal->bShmUnreliable==0 ){
62518 if( pWal->apWiData[0]==0 ){
62523 ** code that determines whether or not the shared-memory region
62537 else if( pWal->bShmUnreliable ){
62542 assert( pWal->nWiData>0 );
62543 assert( pWal->apWiData[0]!=0 );
62545 if( !useWal && AtomicLoad(&pInfo->nBackfill)==pWal->hdr.mxFrame
62547 && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
62556 if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
62573 pWal->readLock = 0;
62583 ** but not exceeding pWal->hdr.mxFrame and lock that entry.
62587 mxFrame = pWal->hdr.mxFrame;
62589 if( pWal->pSnapshot && pWal->pSnapshot->mxFrame<mxFrame ){
62590 mxFrame = pWal->pSnapshot->mxFrame;
62594 u32 thisMark = AtomicLoad(pInfo->aReadMark+i);
62601 if( (pWal->readOnly & WAL_SHM_RDONLY)==0
62607 AtomicStore(pInfo->aReadMark+i,mxFrame);
62618 assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
62626 /* Now that the read-lock has been obtained, check that neither the
62627 ** value in the aReadMark[] array or the contents of the wal-index
62630 ** It is necessary to check that the wal-index header did not change
62631 ** between the time it was read and when the shared-lock was obtained
62634 ** that occur later in the log than pWal->hdr.mxFrame may have been
62637 ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
62640 ** Before checking that the live wal-index header has not changed
62643 ** to read any frames earlier than minFrame from the wal file - they
62647 ** nBackfill and checking that the wal-header in shared-memory still
62648 ** matches the one cached in pWal->hdr, it is guaranteed that the
62649 ** checkpointer that set nBackfill was not working with a wal-index
62650 ** header newer than that cached in pWal->hdr. If it were, that could
62652 ** a version of page X that lies before pWal->minFrame (call that version
62655 ** frame pWal->hdr.mxFrame - then the client would incorrectly assume
62658 ** see any pages past pWal->hdr.mxFrame, this problem does not come up.
62660 pWal->minFrame = AtomicLoad(&pInfo->nBackfill)+1;
62662 if( AtomicLoad(pInfo->aReadMark+mxI)!=mxReadMark
62663 || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
62668 assert( mxReadMark<=pWal->hdr.mxFrame );
62669 pWal->readLock = (i16)mxI;
62683 ** This is only really safe if the file-system is such that any page
62697 assert( pWal->readLock>=0 );
62701 int szPage = (int)pWal->szPage;
62704 rc = sqlite3OsFileSize(pWal->pDbFd, &szDb);
62711 u32 i = pInfo->nBackfillAttempted;
62712 for(i=pInfo->nBackfillAttempted; i>AtomicLoad(&pInfo->nBackfill); i--){
62720 pgno = sLoc.aPgno[i-sLoc.iZero];
62721 iDbOff = (i64)(pgno-1) * szPage;
62725 rc = sqlite3OsRead(pWal->pWalFd, pBuf1, szPage, iWalOff);
62728 rc = sqlite3OsRead(pWal->pDbFd, pBuf2, szPage, iDbOff);
62736 pInfo->nBackfillAttempted = i-1;
62754 ** it takes a snapshot of the state of the WAL and wal-index for the current
62756 ** Other threads might append new content to the WAL and wal-index but
62761 ** Pager layer will use this to know that its cache is stale and
62769 WalIndexHdr *pSnapshot = pWal->pSnapshot;
62772 assert( pWal->ckptLock==0 );
62776 if( memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62784 ** has not yet set the pInfo->nBackfillAttempted variable to indicate
62787 ** before checking pInfo->nBackfillAttempted. */
62795 pWal->ckptLock = 1;
62809 if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){
62811 ** a value equal to or smaller than pSnapshot->mxFrame, but pWal->hdr
62812 ** is populated with the wal-index header corresponding to the head
62820 ** pSnapshot->mxFrame into the database file. Note that the
62825 assert( pWal->readLock>0 || pWal->hdr.mxFrame==0 );
62826 assert( pInfo->aReadMark[pWal->readLock]<=pSnapshot->mxFrame );
62830 ** frames beyond pSnapshot->mxFrame. If either of these conditions are
62831 ** true, return SQLITE_ERROR_SNAPSHOT. Otherwise, overwrite pWal->hdr
62834 if( !memcmp(pSnapshot->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
62835 && pSnapshot->mxFrame>=pInfo->nBackfillAttempted
62837 assert( pWal->readLock>0 );
62838 memcpy(&pWal->hdr, pSnapshot, sizeof(WalIndexHdr));
62844 /* A client using a non-current snapshot may not ignore any frames
62850 pWal->minFrame = 1;
62859 if( pWal->ckptLock ){
62862 pWal->ckptLock = 0;
62870 ** read-lock.
62874 if( pWal->readLock>=0 ){
62875 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
62876 pWal->readLock = -1;
62894 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
62899 assert( pWal->readLock>=0 || pWal->lockError );
62901 /* If the "last page" field of the wal-index header snapshot is 0, then
62903 ** in this case as an optimization. Likewise, if pWal->readLock==0,
62907 if( iLast==0 || (pWal->readLock==0 && pWal->bShmUnreliable==0) ){
62917 ** that adds entries to the wal-index (and possibly to this hash
62921 ** read transaction was opened may have been written incorrectly -
62928 ** if we had exclusive access to the hash-table:
62931 ** This condition filters out normal hash-table collisions.
62935 ** table after the current read-transaction had started.
62937 iMinHash = walFramePage(pWal->minFrame);
62938 for(iHash=walFramePage(iLast); iHash>=iMinHash; iHash--){
62953 if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
62957 if( (nCollide--)==0 ){
62967 ** of the wal-index file content. Make sure the results agree with the
62972 assert( pWal->bShmUnreliable || pWal->minFrame>0 );
62973 for(iTest=iLast; iTest>=pWal->minFrame && iTest>0; iTest--){
63000 sz = pWal->hdr.szPage;
63006 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
63013 if( pWal && ALWAYS(pWal->readLock>=0) ){
63014 return pWal->hdr.nPage;
63037 /* If the write-lock is already held, then it was obtained before the
63038 ** read-transaction was even opened, making this call a no-op.
63040 if( pWal->writeLock ){
63041 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
63048 assert( pWal->readLock>=0 );
63049 assert( pWal->writeLock==0 && pWal->iReCksum==0 );
63051 if( pWal->readOnly ){
63062 pWal->writeLock = 1;
63068 if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
63070 pWal->writeLock = 0;
63082 if( pWal->writeLock ){
63084 pWal->writeLock = 0;
63085 pWal->iReCksum = 0;
63086 pWal->truncateOnCommit = 0;
63093 ** function moves the write-pointer back to the start of the transaction.
63105 if( ALWAYS(pWal->writeLock) ){
63106 Pgno iMax = pWal->hdr.mxFrame;
63109 /* Restore the clients cache of the wal-index header to the state it
63112 memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
63114 for(iFrame=pWal->hdr.mxFrame+1;
63120 ** (b) has an outstanding reference, then xUndo is either a no-op
63132 if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
63144 assert( pWal->writeLock );
63145 aWalData[0] = pWal->hdr.mxFrame;
63146 aWalData[1] = pWal->hdr.aFrameCksum[0];
63147 aWalData[2] = pWal->hdr.aFrameCksum[1];
63148 aWalData[3] = pWal->nCkpt;
63160 assert( pWal->writeLock );
63161 assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
63163 if( aWalData[3]!=pWal->nCkpt ){
63164 /* This savepoint was opened immediately after the write-transaction
63169 aWalData[3] = pWal->nCkpt;
63172 if( aWalData[0]<pWal->hdr.mxFrame ){
63173 pWal->hdr.mxFrame = aWalData[0];
63174 pWal->hdr.aFrameCksum[0] = aWalData[1];
63175 pWal->hdr.aFrameCksum[1] = aWalData[2];
63187 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
63191 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
63198 if( pWal->readLock==0 ){
63200 assert( pInfo->nBackfill==pWal->hdr.mxFrame );
63201 if( pInfo->nBackfill>0 ){
63204 rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63209 ** wal-index header to reflect this.
63212 ** at this point. But updating the actual wal-index header is also
63216 walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
63222 pWal->readLock = -1;
63238 ** the next fsync should occur - passed from sqlite3WalFrames() into
63251 ** Do a sync when crossing the p->iSyncPoint boundary.
63264 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
63265 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
63266 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
63269 iAmt -= iFirstAmt;
63271 assert( WAL_SYNC_FLAGS(p->syncFlags)!=0 );
63272 rc = sqlite3OsSync(p->pFd, WAL_SYNC_FLAGS(p->syncFlags));
63275 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
63290 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
63291 pData = pPage->pData;
63292 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
63296 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
63309 const int szPage = pWal->szPage;/* Database page size */
63312 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */
63322 ** wal-file header. Otherwise, read them from the frame header of the
63324 assert( pWal->iReCksum>0 );
63325 if( pWal->iReCksum==1 ){
63328 iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16;
63330 rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff);
63331 pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf);
63332 pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]);
63334 iRead = pWal->iReCksum;
63335 pWal->iReCksum = 0;
63338 rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff);
63345 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff);
63354 ** Write a set of frames to the log. The caller must hold the write-lock
63359 int szPage, /* Database page-size in bytes */
63377 assert( pWal->writeLock );
63384 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
63386 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
63391 if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){
63392 iFirst = pLive->mxFrame+1;
63396 ** log file, instead of appending to it at pWal->hdr.mxFrame.
63406 iFrame = pWal->hdr.mxFrame;
63408 u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */
63409 u32 aCksum[2]; /* Checksum for wal-header */
63414 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
63415 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
63416 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
63417 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
63421 pWal->szPage = szPage;
63422 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
63423 pWal->hdr.aFrameCksum[0] = aCksum[0];
63424 pWal->hdr.aFrameCksum[1] = aCksum[1];
63425 pWal->truncateOnCommit = 1;
63427 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
63428 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
63435 ** an out-of-order write following a WAL restart could result in
63440 if( pWal->syncHeader ){
63441 rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags));
63445 assert( (int)pWal->szPage==szPage );
63449 w.pFd = pWal->pWalFd;
63457 for(p=pList; p; p=p->pDirty){
63462 ** set Wal.writeLock to WAL_WRITELOCK_RECKSUM - indicating that
63464 if( iFirst && (p->pDirty || isCommit==0) ){
63466 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
63471 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
63472 pWal->iReCksum = iWrite;
63474 pData = p->pData;
63475 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
63477 p->flags &= ~PGHDR_WAL_APPEND;
63484 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
63489 p->flags |= PGHDR_WAL_APPEND;
63493 if( isCommit && pWal->iReCksum ){
63514 if( pWal->padToSectorBoundary ){
63515 int sectorSize = sqlite3SectorSize(pWal->pWalFd);
63516 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
63537 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
63538 i64 sz = pWal->mxWalSize;
63539 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
63543 pWal->truncateOnCommit = 0;
63546 /* Append data to the wal-index. It is not necessary to lock the
63547 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
63551 iFrame = pWal->hdr.mxFrame;
63552 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
63553 if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
63555 rc = walIndexAppend(pWal, iFrame, p->pgno);
63560 nExtra--;
63561 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
63566 pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
63569 pWal->hdr.mxFrame = iFrame;
63571 pWal->hdr.iChange++;
63572 pWal->hdr.nPage = nTruncate;
63574 /* If this is a commit, update the wal-index header too. */
63577 pWal->iCallback = iFrame;
63592 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
63608 int isChanged = 0; /* True if a new wal-index header is loaded */
63612 assert( pWal->ckptLock==0 );
63613 assert( pWal->writeLock==0 );
63615 /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
63619 if( pWal->readOnly ) return SQLITE_READONLY;
63623 ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
63627 /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
63629 ** EVIDENCE-OF: R-10421-19736 If any other process is running a
63632 ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
63639 pWal->ckptLock = 1;
63641 /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
63645 ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
63646 ** immediately, and a busy-handler is configured, it is invoked and the
63647 ** writer lock retried until either the busy-handler returns 0 or the
63653 pWal->writeLock = 1;
63663 /* Read the wal-index header. */
63668 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
63669 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
63676 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
63684 if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
63685 if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
63690 /* If a new wal-index header was loaded before the checkpoint was
63691 ** performed, then the pager-cache associated with pWal is now
63692 ** out of date. So zero the cached wal-index header to ensure that
63696 memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
63704 if( pWal->ckptLock ){
63706 pWal->ckptLock = 0;
63723 ret = pWal->iCallback;
63724 pWal->iCallback = 0;
63735 ** on the pWal->readLock byte. If the WAL is already in locking_mode=NORMAL
63737 ** transition out of exclusive-mode is successful, return 1. This
63742 ** locking_mode=EXCLUSIVE. This means that the pWal->readLock must
63744 ** WAL is already in exclusive-locking mode - meaning that this
63745 ** routine is a no-op. The pager must already hold the exclusive lock
63748 ** If op is negative, then do a dry-run of the op==1 case but do
63755 assert( pWal->writeLock==0 );
63756 assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
63758 /* pWal->readLock is usually set, but might be -1 if there was a
63759 ** prior error while attempting to acquire are read-lock. This cannot
63762 ** upgrade to exclusive-mode following such an error.
63764 assert( pWal->readLock>=0 || pWal->lockError );
63765 assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
63768 if( pWal->exclusiveMode!=WAL_NORMAL_MODE ){
63769 pWal->exclusiveMode = WAL_NORMAL_MODE;
63770 if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
63771 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63773 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63779 assert( pWal->exclusiveMode==WAL_NORMAL_MODE );
63780 assert( pWal->readLock>=0 );
63781 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
63782 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
63785 rc = pWal->exclusiveMode==WAL_NORMAL_MODE;
63791 ** Return true if the argument is non-NULL and the WAL module is using
63792 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
63793 ** WAL module is using shared-memory, return false.
63796 return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
63809 assert( pWal->readLock>=0 && pWal->writeLock==0 );
63811 if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){
63819 memcpy(pRet, &pWal->hdr, sizeof(WalIndexHdr));
63826 /* Try to open on pSnapshot when the next read-transaction starts
63832 pWal->pSnapshot = (WalIndexHdr*)pSnapshot;
63836 ** Return a +ve value if snapshot p1 is newer than p2. A -ve value if
63845 if( pHdr1->aSalt[0]<pHdr2->aSalt[0] ) return -1;
63846 if( pHdr1->aSalt[0]>pHdr2->aSalt[0] ) return +1;
63847 if( pHdr1->mxFrame<pHdr2->mxFrame ) return -1;
63848 if( pHdr1->mxFrame>pHdr2->mxFrame ) return +1;
63868 if( memcmp(pNew->aSalt, pWal->hdr.aSalt, sizeof(pWal->hdr.aSalt))
63869 || pNew->mxFrame<walCkptInfo(pWal)->nBackfillAttempted
63893 ** read-lock. This function returns the database page-size if it is known,
63897 assert( pWal==0 || pWal->readLock>=0 );
63898 return (pWal ? pWal->szPage : 0);
63905 return pWal->pWalFd;
63942 ** This file implements an external (disk-based) database using BTrees.
63946 ** "Sorting And Searching", pages 473-480. Addison-Wesley
63952 ** ----------------------------------------------------------------
63953 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
63954 ** ----------------------------------------------------------------
63959 ** on Ptr(N) and its subpages have values greater than Key(N-1). And
63981 ** page, or a pointer-map page.
64000 ** 40 60 15 4-byte meta values passed to higher layers
64005 ** 52 4 Largest root-page (auto/incr_vacuum)
64006 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be
64009 ** 68 4 Application-ID
64011 ** 92 4 The version-valid-for number
64014 ** All of the integer values are big-endian (most significant byte first).
64022 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
64037 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte
64040 ** |----------------|
64042 ** |----------------|
64044 ** |----------------|
64048 ** |----------------|
64051 ** |----------------| ^ Grows upwards
64054 ** |----------------|
64073 ** The cell pointer array contains zero or more 2-byte numbers which are
64104 ** appears first. A variable-length integer may not be more than 9 bytes long.
64106 ** allows a 64-bit integer to be encoded in 9 bytes.
64129 ** filled with data (pagesize - 4 bytes). The last page can have as little
64152 #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8))
64159 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
64170 ** You can change this value at compile-time by specifying a
64171 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
64172 ** header must be exactly 16 bytes including the zero-terminator so
64184 ** first byte of on-disk image of every BTree page.
64194 ** is derived from the raw on-disk page content.
64201 ** stored in MemPage.pBt->mutex.
64206 u8 intKey; /* True if table b-trees. False for index b-trees */
64219 int nFree; /* Number of free bytes on the page. -1 for unknown */
64222 u16 aiOvfl[4]; /* Insert the i-th overflow cell before the aiOvfl-th
64223 ** non-overflow cell */
64283 u32 iBDataVersion; /* Combines with pBt->pPager->iDataVersion */
64297 ** If the shared-data extension is enabled, there may be multiple users
64309 # error wrong numeric code for no-transaction
64312 # error wrong numeric code for read-transaction
64315 # error wrong numeric code for write-transaction
64338 ** If a BtShared client fails to obtain a write-lock on a database
64339 ** table (because there exists one or more read-locks on the table),
64340 ** the shared-cache enters 'pending-lock' state and isPending is
64343 ** The shared-cache leaves the 'pending lock' state when either of
64349 ** while in the 'pending-lock' state, no connection may start a new
64352 ** This feature is included to help prevent writer-starvation.
64361 u8 autoVacuum; /* True if auto-vacuum is enabled */
64362 u8 incrVacuum; /* True if incr-vacuum is enabled */
64366 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
64369 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
64370 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
64379 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
64380 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */
64384 BtLock *pLock; /* List of locks held on this shared-btree struct */
64400 #define BTS_NO_WAL 0x0020 /* Do not open write-ahead-log files */
64402 #define BTS_PENDING 0x0080 /* Waiting for read-locks to clear */
64414 u16 nSize; /* Size of the cell content on the main b-tree page */
64418 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
64421 ** root-node and 3 for all other internal nodes.
64430 ** b-tree within a database file.
64440 ** found at self->pBt->mutex.
64448 ** SKIPNEXT sqlite3BtreeNext() is a no-op if skipNext>0 and
64449 ** sqlite3BtreePrevious() is no-op if skipNext<0.
64474 u8 curIntKey; /* Value of apPage[0]->intKey */
64476 u16 aiIdx[BTCURSOR_MAX_DEPTH-1]; /* Current index in apPage[i] */
64479 MemPage *apPage[BTCURSOR_MAX_DEPTH-1]; /* Stack of parents of current page */
64505 ** Cursor is valid except that the Cursor.skipNext field is non-zero
64507 ** operation should be a no-op.
64535 ** These macros define the location of the pointer-map entry for a
64540 ** PTRMAP_PAGENO returns the database page number of the pointer-map
64544 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
64546 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
64550 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
64567 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
64570 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
64578 ** overflow pages. The page-number identifies the previous
64581 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
64594 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
64595 assert( p->pBt->inTransaction>=p->inTrans );
64600 ** if the database supports auto-vacuum or not. Because it is used
64606 #define ISAUTOVACUUM (pBt->autoVacuum)
64617 ** the database. As the integrity-check proceeds, for each page used in
64618 ** the database the corresponding bit is set. This allows integrity-check to
64625 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
64631 const char *zPfx; /* Error message prefix */
64634 StrAccum errMsg; /* Accumulate the error message text here */
64635 u32 *heap; /* Min-heap used for analyzing cell coverage */
64640 ** Routines to read or write a two- and four-byte big-endian integer values.
64649 ** two-byte aligned address. get2bytea() is only used for accessing the
64668 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
64670 ** p->locked boolean to true.
64673 assert( p->locked==0 );
64674 assert( sqlite3_mutex_notheld(p->pBt->mutex) );
64675 assert( sqlite3_mutex_held(p->db->mutex) );
64677 sqlite3_mutex_enter(p->pBt->mutex);
64678 p->pBt->db = p->db;
64679 p->locked = 1;
64683 ** Release the BtShared mutex associated with B-Tree handle p and
64684 ** clear the p->locked boolean.
64687 BtShared *pBt = p->pBt;
64688 assert( p->locked==1 );
64689 assert( sqlite3_mutex_held(pBt->mutex) );
64690 assert( sqlite3_mutex_held(p->db->mutex) );
64691 assert( p->db==pBt->db );
64693 sqlite3_mutex_leave(pBt->mutex);
64694 p->locked = 0;
64704 ** and this routine is a no-op. The underlying mutex is non-recursive.
64709 ** by all database connections. The p->pNext is a list of other
64712 ** p, then first unlock all of the others on p->pNext, then wait
64721 assert( p->pNext==0 || p->pNext->pBt>p->pBt );
64722 assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
64723 assert( p->pNext==0 || p->pNext->db==p->db );
64724 assert( p->pPrev==0 || p->pPrev->db==p->db );
64725 assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
64728 assert( !p->locked || p->wantToLock>0 );
64729 assert( p->sharable || p->wantToLock==0 );
64732 assert( sqlite3_mutex_held(p->db->mutex) );
64736 assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
64738 if( !p->sharable ) return;
64739 p->wantToLock++;
64740 if( p->locked ) return;
64757 if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
64758 p->pBt->db = p->db;
64759 p->locked = 1;
64768 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64769 assert( pLater->sharable );
64770 assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
64771 assert( !pLater->locked || pLater->wantToLock>0 );
64772 if( pLater->locked ){
64777 for(pLater=p->pNext; pLater; pLater=pLater->pNext){
64778 if( pLater->wantToLock ){
64789 assert( sqlite3_mutex_held(p->db->mutex) );
64790 if( p->sharable ){
64791 assert( p->wantToLock>0 );
64792 p->wantToLock--;
64793 if( p->wantToLock==0 ){
64802 ** B-Tree is not marked as sharable.
64807 assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
64808 assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
64809 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
64810 assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
64812 return (p->sharable==0 || p->locked);
64824 ** There is a corresponding leave-all procedures.
64835 assert( sqlite3_mutex_held(db->mutex) );
64836 for(i=0; i<db->nDb; i++){
64837 p = db->aDb[i].pBt;
64838 if( p && p->sharable ){
64843 db->noSharedCache = skipOk;
64846 if( db->noSharedCache==0 ) btreeEnterAll(db);
64851 assert( sqlite3_mutex_held(db->mutex) );
64852 for(i=0; i<db->nDb; i++){
64853 p = db->aDb[i].pBt;
64858 if( db->noSharedCache==0 ) btreeLeaveAll(db);
64870 if( !sqlite3_mutex_held(db->mutex) ){
64873 for(i=0; i<db->nDb; i++){
64875 p = db->aDb[i].pBt;
64876 if( p && p->sharable &&
64877 (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
64888 ** db->aDb[iDb].pSchema structure. The mutexes required for schema
64892 ** (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
64901 assert( iDb>=0 && iDb<db->nDb );
64902 if( !sqlite3_mutex_held(db->mutex) ) return 0;
64904 p = db->aDb[iDb].pBt;
64906 return p->sharable==0 || p->locked==1;
64914 ** these two routines, all mutex operations are no-ops in that case and
64918 ** the ones below, are no-ops and are null #defines in btree.h.
64922 p->pBt->db = p->db;
64926 for(i=0; i<db->nDb; i++){
64927 Btree *p = db->aDb[i].pBt;
64929 p->pBt->db = p->db;
64944 sqlite3BtreeEnter(pCur->pBtree);
64948 sqlite3BtreeLeave(pCur->pBtree);
64968 ** This file implements an external (disk-based) database using BTrees.
64992 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
64997 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
65000 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
65013 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
65058 ** shared-cache table level locks. If the library is compiled with the
65059 ** shared-cache feature disabled, then there is only ever one user
65061 ** So define the lock related functions as no-ops.
65076 u64 n = pBt->nSeek;
65077 pBt->nSeek = 0;
65087 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
65088 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
65096 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
65107 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
65119 ** For example, when writing to a table with root-page iRoot via
65132 ** hold a write-lock on the schema table (root page 1). This is also
65137 Pgno iRoot, /* Root page of b-tree */
65138 int isIndex, /* True if iRoot is the root of an index b-tree */
65141 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
65146 ** and has the read-uncommitted flag set, then no lock is required.
65149 if( (pBtree->sharable==0)
65150 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
65157 ** the correct locks are held. So do not bother - just return true.
65160 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
65164 /* Figure out the root-page that the lock should be held on. For table
65165 ** b-trees, this is just the root page of the b-tree being read or
65166 ** written. For index b-trees, it is the root page of the associated
65171 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
65173 if( pIdx->tnum==(int)iRoot ){
65180 iTab = pIdx->pTable->tnum;
65188 /* Search for the required lock. Either a write-lock on root-page iTab, a
65189 ** write-lock on the schema table, or (if the client is reading) a
65190 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
65191 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
65192 if( pLock->pBtree==pBtree
65193 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
65194 && pLock->eLock>=eLockType
65216 ** read-uncommitted flag set, then it is OK for the other object to
65226 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
65227 if( p->pgnoRoot==iRoot
65228 && p->pBtree!=pBtree
65229 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
65240 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
65245 BtShared *pBt = p->pBt;
65250 assert( p->db!=0 );
65251 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
65253 /* If requesting a write-lock, then the Btree must have an open write
65257 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
65258 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
65260 /* This routine is a no-op if the shared-cache is not enabled */
65261 if( !p->sharable ){
65268 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
65269 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
65273 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
65274 /* The condition (pIter->eLock!=eLock) in the following if(...)
65277 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
65283 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
65284 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
65285 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
65286 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
65288 assert( p==pBt->pWriter );
65289 pBt->btsFlags |= BTS_PENDING;
65300 ** Add a lock on the table with root-page iTable to the shared-btree used
65317 BtShared *pBt = p->pBt;
65323 assert( p->db!=0 );
65325 /* A connection with the read-uncommitted flag set will never try to
65326 ** obtain a read-lock using this function. The only read-lock obtained
65327 ** by a connection in read-uncommitted mode is on the sqlite_schema
65329 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
65331 /* This function should only be called on a sharable b-tree after it
65332 ** has been determined that no other b-tree holds a conflicting lock. */
65333 assert( p->sharable );
65337 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
65338 if( pIter->iTable==iTable && pIter->pBtree==p ){
65352 pLock->iTable = iTable;
65353 pLock->pBtree = p;
65354 pLock->pNext = pBt->pLock;
65355 pBt->pLock = pLock;
65359 ** and the requested lock. This means if a write-lock was already held
65360 ** and a read-lock requested, we don't incorrectly downgrade the lock.
65363 if( eLock>pLock->eLock ){
65364 pLock->eLock = eLock;
65381 BtShared *pBt = p->pBt;
65382 BtLock **ppIter = &pBt->pLock;
65385 assert( p->sharable || 0==*ppIter );
65386 assert( p->inTrans>0 );
65390 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
65391 assert( pLock->pBtree->inTrans>=pLock->eLock );
65392 if( pLock->pBtree==p ){
65393 *ppIter = pLock->pNext;
65394 assert( pLock->iTable!=1 || pLock==&p->lock );
65395 if( pLock->iTable!=1 ){
65399 ppIter = &pLock->pNext;
65403 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
65404 if( pBt->pWriter==p ){
65405 pBt->pWriter = 0;
65406 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
65407 }else if( pBt->nTransaction==2 ){
65417 pBt->btsFlags &= ~BTS_PENDING;
65422 ** This function changes all write-locks held by Btree p into read-locks.
65425 BtShared *pBt = p->pBt;
65426 if( pBt->pWriter==p ){
65428 pBt->pWriter = 0;
65429 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
65430 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
65431 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
65432 pLock->eLock = READ_LOCK;
65450 return sqlite3_mutex_held(p->pBt->mutex);
65454 ** database connetion. This is important in shared-cache mode. If the database
65455 ** connection pointers get out-of-sync, it is possible for routines like
65456 ** btreeInitPage() to reference an stale connection pointer that references a
65458 ** statements only and for the purpose of double-checking that the btree code
65459 ** does keep the database connection pointers up-to-date.
65463 return (p->pBtree->db==p->pBt->db);
65471 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
65474 ** Invalidate the overflow page-list cache for all cursors opened
65479 assert( sqlite3_mutex_held(pBt->mutex) );
65480 for(p=pBt->pCursor; p; p=p->pNext){
65493 ** cursors open on any row within the table with root-page pgnoRoot.
65506 if( pBtree->hasIncrblobCur==0 ) return;
65508 pBtree->hasIncrblobCur = 0;
65509 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
65510 if( (p->curFlags & BTCF_Incrblob)!=0 ){
65511 pBtree->hasIncrblobCur = 1;
65512 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
65513 p->eState = CURSOR_INVALID;
65526 ** when a page that previously contained data becomes a free-list leaf
65531 ** free-list leaf pages:
65534 ** a free-list leaf page, the page is not written to the database
65535 ** (as free-list leaf pages contain no meaningful data). Sometimes
65539 ** 2) When a free-list leaf page is reused, its content is not read
65545 ** a page is moved to the free-list and then reused within the same
65547 ** it is moved to the free-list and it is also not journalled when it
65548 ** is extracted from the free-list and reused, then the original data
65553 ** moved to become a free-list leaf page, the corresponding bit is
65554 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
65561 if( !pBt->pHasContent ){
65562 assert( pgno<=pBt->nPage );
65563 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
65564 if( !pBt->pHasContent ){
65568 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
65569 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
65577 ** This function is called when a free-list leaf page is removed from the
65578 ** free-list for reuse. It returns false if it is safe to retrieve the
65579 ** page from the pager layer with the 'no-content' flag set. True otherwise.
65582 Bitvec *p = pBt->pHasContent;
65588 ** invoked at the conclusion of each write-transaction.
65591 sqlite3BitvecDestroy(pBt->pHasContent);
65592 pBt->pHasContent = 0;
65600 if( pCur->iPage>=0 ){
65601 for(i=0; i<pCur->iPage; i++){
65602 releasePageNotNull(pCur->apPage[i]);
65604 releasePageNotNull(pCur->pPage);
65605 pCur->iPage = -1;
65612 ** function saves the current cursor key in variables pCur->nKey and
65613 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
65617 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
65618 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
65619 ** set to point to a malloced buffer pCur->nKey bytes in size containing
65624 assert( CURSOR_VALID==pCur->eState );
65625 assert( 0==pCur->pKey );
65628 if( pCur->curIntKey ){
65630 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
65635 ** up to the size of 1 varint plus 1 8-byte value when the cursor
65639 pCur->nKey = sqlite3BtreePayloadSize(pCur);
65640 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
65642 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
65644 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
65645 pCur->pKey = pKey;
65653 assert( !pCur->curIntKey || !pCur->pKey );
65667 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
65668 assert( 0==pCur->pKey );
65671 if( pCur->curFlags & BTCF_Pinned ){
65674 if( pCur->eState==CURSOR_SKIPNEXT ){
65675 pCur->eState = CURSOR_VALID;
65677 pCur->skipNext = 0;
65683 pCur->eState = CURSOR_REQUIRESEEK;
65686 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
65695 ** the table with root-page iRoot. "Saving the cursor position" means that
65706 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
65716 assert( sqlite3_mutex_held(pBt->mutex) );
65717 assert( pExcept==0 || pExcept->pBt==pBt );
65718 for(p=pBt->pCursor; p; p=p->pNext){
65719 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
65722 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
65737 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
65738 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
65744 testcase( p->iPage>=0 );
65748 p = p->pNext;
65758 sqlite3_free(pCur->pKey);
65759 pCur->pKey = 0;
65760 pCur->eState = CURSOR_INVALID;
65779 KeyInfo *pKeyInfo = pCur->pKeyInfo;
65784 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
65794 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
65810 assert( pCur->eState>=CURSOR_REQUIRESEEK );
65811 if( pCur->eState==CURSOR_FAULT ){
65812 return pCur->skipNext;
65814 pCur->eState = CURSOR_INVALID;
65818 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
65821 sqlite3_free(pCur->pKey);
65822 pCur->pKey = 0;
65823 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
65824 if( skipNext ) pCur->skipNext = skipNext;
65825 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
65826 pCur->eState = CURSOR_SKIPNEXT;
65833 (p->eState>=CURSOR_REQUIRESEEK ? \
65853 assert( sizeof(pCur->eState)==1 );
65885 assert( pCur->eState!=CURSOR_VALID );
65891 if( pCur->eState!=CURSOR_VALID ){
65915 pCur->hints = x;
65922 ** number for the pointer-map page that contains the entry for the
65932 assert( sqlite3_mutex_held(pBt->mutex) );
65934 nPagesPerMapPage = (pBt->usableSize/5)+1;
65935 iPtrMap = (pgno-2)/nPagesPerMapPage;
65949 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
65950 ** a no-op. If an error occurs, the appropriate error code is written
65962 assert( sqlite3_mutex_held(pBt->mutex) );
65963 /* The super-journal page number must never be used as a pointer map page */
65966 assert( pBt->autoVacuum );
65972 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
65989 assert( offset <= (int)pBt->usableSize-5 );
65993 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
66019 assert( sqlite3_mutex_held(pBt->mutex) );
66022 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
66033 assert( offset <= (int)pBt->usableSize-5 );
66055 ** 4-byte child pointer found on interior pages, if there is one.
66060 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
66062 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
66068 ** on a single B-tree page. Make necessary adjustments to the CellInfo
66089 minLocal = pPage->minLocal;
66090 maxLocal = pPage->maxLocal;
66091 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
66095 pInfo->nLocal = (u16)surplus;
66097 pInfo->nLocal = (u16)minLocal;
66099 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
66108 maxLocal = pPage->maxLocal;
66114 minLocal = pPage->minLocal;
66115 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
66139 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66140 assert( pPage->leaf==0 );
66141 assert( pPage->childPtrSize==4 );
66145 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
66146 pInfo->nPayload = 0;
66147 pInfo->nLocal = 0;
66148 pInfo->pPayload = 0;
66160 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66161 assert( pPage->leaf==0 || pPage->leaf==1 );
66162 assert( pPage->intKeyLeaf );
66163 assert( pPage->childPtrSize==0 );
66184 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
66203 pInfo->nKey = *(i64*)&iKey;
66204 pInfo->nPayload = nPayload;
66205 pInfo->pPayload = pIter;
66206 testcase( nPayload==pPage->maxLocal );
66207 testcase( nPayload==pPage->maxLocal+1 );
66208 if( nPayload<=pPage->maxLocal ){
66212 pInfo->nSize = nPayload + (u16)(pIter - pCell);
66213 if( pInfo->nSize<4 ) pInfo->nSize = 4;
66214 pInfo->nLocal = (u16)nPayload;
66227 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66228 assert( pPage->leaf==0 || pPage->leaf==1 );
66229 assert( pPage->intKeyLeaf==0 );
66230 pIter = pCell + pPage->childPtrSize;
66240 pInfo->nKey = nPayload;
66241 pInfo->nPayload = nPayload;
66242 pInfo->pPayload = pIter;
66243 testcase( nPayload==pPage->maxLocal );
66244 testcase( nPayload==pPage->maxLocal+1 );
66245 if( nPayload<=pPage->maxLocal ){
66249 pInfo->nSize = nPayload + (u16)(pIter - pCell);
66250 if( pInfo->nSize<4 ) pInfo->nSize = 4;
66251 pInfo->nLocal = (u16)nPayload;
66261 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
66269 ** data area of the btree-page. The return number includes the cell
66277 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
66287 pPage->xParseCell(pPage, pCell, &debuginfo);
66299 if( pPage->intKey ){
66300 /* pIter now points at the 64-bit integer key value, a variable length
66306 testcase( nSize==pPage->maxLocal );
66307 testcase( nSize==pPage->maxLocal+1 );
66308 if( nSize<=pPage->maxLocal ){
66309 nSize += (u32)(pIter - pCell);
66312 int minLocal = pPage->minLocal;
66313 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
66314 testcase( nSize==pPage->maxLocal );
66315 testcase( nSize==pPage->maxLocal+1 );
66316 if( nSize>pPage->maxLocal ){
66319 nSize += 4 + (u16)(pIter - pCell);
66334 pPage->xParseCell(pPage, pCell, &debuginfo);
66339 assert( pPage->childPtrSize==4 );
66342 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
66343 return (u16)(pIter - pCell);
66351 return pPage->xCellSize(pPage, findCell(pPage, iCell));
66359 ** pointer to an overflow page, insert an entry into the pointer-map for
66366 pPage->xParseCell(pPage, pCell, &info);
66369 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
66374 ovfl = get4byte(&pCell[info.nSize-4]);
66375 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
66383 ** page so that there are no free-blocks on the free-block list.
66388 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
66389 ** b-tree page so that there are no freeblocks or fragment bytes, all
66395 int pc; /* Address of the i-th cell */
66408 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66409 assert( pPage->pBt!=0 );
66410 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
66411 assert( pPage->nOverflow==0 );
66412 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66414 src = data = pPage->aData;
66415 hdr = pPage->hdrOffset;
66416 cellOffset = pPage->cellOffset;
66417 nCell = pPage->nCell;
66420 usableSize = pPage->pBt->usableSize;
66425 ** offsets to each pointer in the cell-pointer array than it is to
66429 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
66432 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
66446 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
66453 assert( cbrk+(iFree-top) <= usableSize );
66454 memmove(&data[cbrk], &data[top], iFree-top);
66466 iCellLast = usableSize - 4;
66468 u8 *pAddr; /* The i-th cell pointer */
66480 size = pPage->xCellSize(pPage, &src[pc]);
66481 cbrk -= size;
66492 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
66494 memcpy(&temp[x], &data[x], (cbrk+size) - x);
66502 assert( pPage->nFree>=0 );
66503 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
66510 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
66511 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66516 ** Search the free-list on page pPg for space to store a cell nByte bytes in
66518 ** from the free-list.
66520 ** If no suitable space can be found on the free-list, return NULL.
66530 const int hdr = pPg->hdrOffset; /* Offset to page header */
66531 u8 * const aData = pPg->aData; /* Page data */
66535 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
66540 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
66541 ** freeblock form a big-endian integer which is the size of the freeblock
66542 ** in bytes, including the 4-byte header. */
66544 if( (x = size - nByte)>=0 ){
66548 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
66552 /* Remove the slot from the free-list. Update the number of
66561 /* The slot remains on the free-list. Reduce its size to account
66577 if( pc>maxPC+nByte-4 ){
66585 ** Allocate nByte bytes of space from within the B-Tree page passed
66586 ** as the first argument. Write into *pIdx the index into pPage->aData[]
66598 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
66599 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
66604 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66605 assert( pPage->pBt );
66606 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66608 assert( pPage->nFree>=nByte );
66609 assert( pPage->nOverflow==0 );
66610 assert( nByte < (int)(pPage->pBt->usableSize-8) );
66612 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
66613 gap = pPage->cellOffset + 2*pPage->nCell;
66615 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
66618 ** However, that integer is too large to be stored in a 2-byte unsigned
66621 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
66623 if( top==0 && pPage->pBt->usableSize==65536 ){
66641 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
66642 *pIdx = g2 = (int)(pSpace-data);
66658 assert( pPage->nCell>0 || CORRUPT_DB );
66659 assert( pPage->nFree>=0 );
66660 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
66673 top -= nByte;
66675 assert( top+nByte <= (int)pPage->pBt->usableSize );
66681 ** Return a section of the pPage->aData to the freelist.
66682 ** The first byte of the new free block is pPage->aData[iStart]
66701 unsigned char *data = pPage->aData; /* Page content */
66703 assert( pPage->pBt!=0 );
66704 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
66705 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
66706 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
66707 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66709 assert( iStart<=pPage->pBt->usableSize-4 );
66714 hdr = pPage->hdrOffset;
66726 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
66738 nFrag = iFreeBlk - iEnd;
66741 if( iEnd > pPage->pBt->usableSize ){
66744 iSize = iEnd - iStart;
66756 nFrag += iStart - iPtrEnd;
66757 iSize = iEnd - iPtr;
66762 data[hdr+7] -= nFrag;
66777 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
66784 pPage->nFree += iOrigSize;
66801 BtShared *pBt; /* A copy of pPage->pBt */
66803 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
66804 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66805 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
66807 pPage->childPtrSize = 4-4*pPage->leaf;
66808 pPage->xCellSize = cellSizePtr;
66809 pBt = pPage->pBt;
66811 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
66812 ** interior table b-tree page. */
66814 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
66815 ** leaf table b-tree page. */
66817 pPage->intKey = 1;
66818 if( pPage->leaf ){
66819 pPage->intKeyLeaf = 1;
66820 pPage->xParseCell = btreeParseCellPtr;
66822 pPage->intKeyLeaf = 0;
66823 pPage->xCellSize = cellSizePtrNoPayload;
66824 pPage->xParseCell = btreeParseCellPtrNoPayload;
66826 pPage->maxLocal = pBt->maxLeaf;
66827 pPage->minLocal = pBt->minLeaf;
66829 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
66830 ** interior index b-tree page. */
66832 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
66833 ** leaf index b-tree page. */
66835 pPage->intKey = 0;
66836 pPage->intKeyLeaf = 0;
66837 pPage->xParseCell = btreeParseCellPtrIndex;
66838 pPage->maxLocal = pBt->maxLocal;
66839 pPage->minLocal = pBt->minLocal;
66841 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
66845 pPage->max1bytePayload = pBt->max1bytePayload;
66851 ** in the pPage->nFree field.
66854 int pc; /* Address of a freeblock within pPage->aData[] */
66856 u8 *data; /* Equal to pPage->aData */
66863 assert( pPage->pBt!=0 );
66864 assert( pPage->pBt->db!=0 );
66865 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66866 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66867 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66868 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66869 assert( pPage->isInit==1 );
66870 assert( pPage->nFree<0 );
66872 usableSize = pPage->pBt->usableSize;
66873 hdr = pPage->hdrOffset;
66874 data = pPage->aData;
66875 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
66879 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
66880 iCellLast = usableSize - 4;
66883 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
66887 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
66891 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
66918 ** of the cell-content area plus the number of free bytes within
66919 ** the cell-content area. If this is greater than the usable-size
66921 ** serves to verify that the offset to the start of the cell-content
66927 pPage->nFree = (u16)(nFree - iCellFirst);
66940 int pc; /* Address of a freeblock within pPage->aData[] */
66941 u8 *data; /* Equal to pPage->aData */
66945 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
66946 usableSize = pPage->pBt->usableSize;
66947 iCellLast = usableSize - 4;
66948 data = pPage->aData;
66949 cellOffset = pPage->cellOffset;
66950 if( !pPage->leaf ) iCellLast--;
66951 for(i=0; i<pPage->nCell; i++){
66958 sz = pPage->xCellSize(pPage, &data[pc]);
66971 ** not contain a well-formed database page, then return
66973 ** guarantee that the page is well-formed. It only shows that
66977 u8 *data; /* Equal to pPage->aData */
66980 assert( pPage->pBt!=0 );
66981 assert( pPage->pBt->db!=0 );
66982 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
66983 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
66984 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
66985 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
66986 assert( pPage->isInit==0 );
66988 pBt = pPage->pBt;
66989 data = pPage->aData + pPage->hdrOffset;
66990 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
66991 ** the b-tree page type. */
66995 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
66996 pPage->maskPage = (u16)(pBt->pageSize - 1);
66997 pPage->nOverflow = 0;
66998 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
66999 pPage->aCellIdx = data + pPage->childPtrSize + 8;
67000 pPage->aDataEnd = pPage->aData + pBt->usableSize;
67001 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
67002 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
67004 pPage->nCell = get2byte(&data[3]);
67005 if( pPage->nCell>MX_CELL(pBt) ){
67009 testcase( pPage->nCell==MX_CELL(pBt) );
67010 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
67014 assert( pPage->nCell>0
67015 || get2byteNotZero(&data[5])==(int)pBt->usableSize
67017 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
67018 pPage->isInit = 1;
67019 if( pBt->db->flags & SQLITE_CellSizeCk ){
67030 unsigned char *data = pPage->aData;
67031 BtShared *pBt = pPage->pBt;
67032 u8 hdr = pPage->hdrOffset;
67035 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
67036 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67037 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
67038 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
67039 assert( sqlite3_mutex_held(pBt->mutex) );
67040 if( pBt->btsFlags & BTS_FAST_SECURE ){
67041 memset(&data[hdr], 0, pBt->usableSize - hdr);
67047 put2byte(&data[hdr+5], pBt->usableSize);
67048 pPage->nFree = (u16)(pBt->usableSize - first);
67050 pPage->cellOffset = first;
67051 pPage->aDataEnd = &data[pBt->usableSize];
67052 pPage->aCellIdx = &data[first];
67053 pPage->aDataOfst = &data[pPage->childPtrSize];
67054 pPage->nOverflow = 0;
67055 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
67056 pPage->maskPage = (u16)(pBt->pageSize - 1);
67057 pPage->nCell = 0;
67058 pPage->isInit = 1;
67068 if( pgno!=pPage->pgno ){
67069 pPage->aData = sqlite3PagerGetData(pDbPage);
67070 pPage->pDbPage = pDbPage;
67071 pPage->pBt = pBt;
67072 pPage->pgno = pgno;
67073 pPage->hdrOffset = pgno==1 ? 100 : 0;
67075 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
67100 assert( sqlite3_mutex_held(pBt->mutex) );
67101 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
67114 assert( sqlite3_mutex_held(pBt->mutex) );
67115 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
67124 ** error, return ((unsigned int)-1).
67127 return pBt->nPage;
67131 return btreePagecount(p->pBt);
67139 ** And if the fetch fails, this routine must decrement pCur->iPage.
67141 ** The page is fetched as read-write unless pCur is not NULL and is
67142 ** a read-only cursor.
67152 int bReadOnly /* True for a read-only page */
67156 assert( sqlite3_mutex_held(pBt->mutex) );
67157 assert( pCur==0 || ppPage==&pCur->pPage );
67158 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
67159 assert( pCur==0 || pCur->iPage>0 );
67165 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
67170 if( (*ppPage)->isInit==0 ){
67177 assert( (*ppPage)->pgno==pgno );
67178 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
67182 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
67192 pCur->iPage--;
67193 pCur->pPage = pCur->apPage[pCur->iPage];
67207 assert( pPage->aData );
67208 assert( pPage->pBt );
67209 assert( pPage->pDbPage!=0 );
67210 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67211 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
67212 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67213 sqlite3PagerUnrefNotNull(pPage->pDbPage);
67220 assert( pPage->aData );
67221 assert( pPage->pBt );
67222 assert( pPage->pDbPage!=0 );
67223 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
67224 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
67225 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67226 sqlite3PagerUnrefPageOne(pPage->pDbPage);
67246 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
67251 (*ppPage)->isInit = 0;
67271 if( pPage->isInit ){
67272 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
67273 pPage->isInit = 0;
67280 ** the call for every page that comes in for re-initing. */
67291 assert( pBt->db );
67292 assert( sqlite3_mutex_held(pBt->db->mutex) );
67293 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
67301 ** be exclusively in memory, or it might use a disk-based memory cache.
67305 ** If zFilename is ":memory:" then an in-memory database is created
67318 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
67335 /* Set the variable isMemdb to true for an in-memory database, or
67336 ** false for a file-based database.
67348 assert( sqlite3_mutex_held(db->mutex) );
67367 p->inTrans = TRANS_NONE;
67368 p->db = db;
67370 p->lock.pBtree = p;
67371 p->lock.iTable = 1;
67382 int nFullPathname = pVfs->mxPathname+1;
67386 p->sharable = 1;
67412 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
67413 assert( pBt->nRef>0 );
67414 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
67415 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
67417 for(iDb=db->nDb-1; iDb>=0; iDb--){
67418 Btree *pExisting = db->aDb[iDb].pBt;
67419 if( pExisting && pExisting->pBt==pBt ){
67427 p->pBt = pBt;
67428 pBt->nRef++;
67442 p->sharable = 1;
67464 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
67467 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
67468 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
67473 pBt->openFlags = (u8)flags;
67474 pBt->db = db;
67475 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
67476 p->pBt = pBt;
67478 pBt->pCursor = 0;
67479 pBt->pPage1 = 0;
67480 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
67482 pBt->btsFlags |= BTS_SECURE_DELETE;
67484 pBt->btsFlags |= BTS_OVERWRITE;
67486 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
67487 ** determined by the 2-byte integer located at an offset of 16 bytes from
67489 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
67490 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
67491 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
67492 pBt->pageSize = 0;
67494 /* If the magic name ":memory:" will create an in-memory database, then
67495 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
67498 ** regular file-name. In this case the auto-vacuum applies as per normal.
67501 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
67502 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
67507 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
67508 ** determined by the one-byte unsigned integer found at an offset of 20
67511 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67513 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
67514 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
67517 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
67519 pBt->usableSize = pBt->pageSize - nReserve;
67520 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
67525 pBt->nRef = 1;
67526 if( p->sharable ){
67530 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
67531 if( pBt->mutex==0 ){
67537 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
67549 if( p->sharable ){
67552 for(i=0; i<db->nDb; i++){
67553 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
67554 while( pSib->pPrev ){ pSib = pSib->pPrev; }
67555 if( (uptr)p->pBt<(uptr)pSib->pBt ){
67556 p->pNext = pSib;
67557 p->pPrev = 0;
67558 pSib->pPrev = p;
67560 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
67561 pSib = pSib->pNext;
67563 p->pNext = pSib->pNext;
67564 p->pPrev = pSib;
67565 if( p->pNext ){
67566 p->pNext->pPrev = p;
67568 pSib->pNext = p;
67579 if( pBt && pBt->pPager ){
67580 sqlite3PagerClose(pBt->pPager, 0);
67588 /* If the B-Tree was successfully opened, set the pager-cache size to the
67589 ** default value. Except, when opening on an existing shared pager-cache,
67590 ** do not change the pager-cache size.
67596 pFile = sqlite3PagerFile(pBt->pPager);
67597 if( pFile->pMethods ){
67598 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
67621 assert( sqlite3_mutex_notheld(pBt->mutex) );
67624 pBt->nRef--;
67625 if( pBt->nRef<=0 ){
67627 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
67630 while( ALWAYS(pList) && pList->pNext!=pBt ){
67631 pList=pList->pNext;
67634 pList->pNext = pBt->pNext;
67638 sqlite3_mutex_free(pBt->mutex);
67650 ** Make sure pBt->pTmpSpace points to an allocation of
67651 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
67655 if( !pBt->pTmpSpace ){
67656 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
67658 /* One of the uses of pBt->pTmpSpace is to format cells before
67671 ** left-child pointer to the beginning of a cell.
67673 if( pBt->pTmpSpace ){
67674 memset(pBt->pTmpSpace, 0, 8);
67675 pBt->pTmpSpace += 4;
67681 ** Free the pBt->pTmpSpace allocation
67684 if( pBt->pTmpSpace ){
67685 pBt->pTmpSpace -= 4;
67686 sqlite3PageFree(pBt->pTmpSpace);
67687 pBt->pTmpSpace = 0;
67695 BtShared *pBt = p->pBt;
67698 assert( sqlite3_mutex_held(p->db->mutex) );
67704 BtCursor *pCur = pBt->pCursor;
67707 pCur = pCur->pNext;
67708 assert( pTmp->pBtree!=p );
67715 ** The call to sqlite3BtreeRollback() drops any table-locks held by
67721 /* If there are still other outstanding references to the shared-btree
67723 ** up the shared-btree.
67725 assert( p->wantToLock==0 && p->locked==0 );
67726 if( !p->sharable || removeFromSharingList(pBt) ){
67732 assert( !pBt->pCursor );
67733 sqlite3PagerClose(pBt->pPager, p->db);
67734 if( pBt->xFreeSchema && pBt->pSchema ){
67735 pBt->xFreeSchema(pBt->pSchema);
67737 sqlite3DbFree(0, pBt->pSchema);
67743 assert( p->wantToLock==0 );
67744 assert( p->locked==0 );
67745 if( p->pPrev ) p->pPrev->pNext = p->pNext;
67746 if( p->pNext ) p->pNext->pPrev = p->pPrev;
67761 BtShared *pBt = p->pBt;
67762 assert( sqlite3_mutex_held(p->db->mutex) );
67764 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
67780 BtShared *pBt = p->pBt;
67782 assert( sqlite3_mutex_held(p->db->mutex) );
67784 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
67795 BtShared *pBt = p->pBt;
67796 assert( sqlite3_mutex_held(p->db->mutex) );
67798 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
67809 ** is a very low but non-zero probability of damage. Level 3 reduces the
67817 BtShared *pBt = p->pBt;
67818 assert( sqlite3_mutex_held(p->db->mutex) );
67820 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
67849 BtShared *pBt = p->pBt;
67852 pBt->nReserveWanted = nReserve;
67853 x = pBt->pageSize - pBt->usableSize;
67855 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
67861 ((pageSize-1)&pageSize)==0 ){
67863 assert( !pBt->pCursor );
67865 pBt->pageSize = (u32)pageSize;
67868 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
67869 pBt->usableSize = pBt->pageSize - (u16)nReserve;
67870 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
67879 return p->pBt->pageSize;
67884 ** may only be called if it is guaranteed that the b-tree mutex is already
67888 ** known that the shared b-tree mutex is held, but the mutex on the
67895 assert( sqlite3_mutex_held(p->pBt->mutex) );
67896 n = p->pBt->pageSize - p->pBt->usableSize;
67907 ** The amount of reserve can only grow - never shrink.
67912 n1 = (int)p->pBt->nReserveWanted;
67927 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
67938 ** newFlag==(-1) No changes
67943 ** freelist leaf pages are not written back to the database. Thus in-page
67957 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
67958 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
67960 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
67966 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
67967 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
67968 ** is disabled. The default value for the auto-vacuum property is
67975 BtShared *pBt = p->pBt;
67980 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
67983 pBt->autoVacuum = av ?1:0;
67984 pBt->incrVacuum = av==2 ?1:0;
67992 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
68002 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
68003 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
68012 ** If the user has not set the safety-level for this database connection
68013 ** using "PRAGMA synchronous", and if the safety-level is not already
68022 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
68023 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
68024 if( pDb->bSyncSet==0
68025 && pDb->safety_level!=safety_level
68026 && pDb!=&db->aDb[1]
68028 pDb->safety_level = safety_level;
68029 sqlite3PagerSetFlags(pBt->pPager,
68030 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
68047 ** well-formed database file, then SQLITE_CORRUPT is returned.
68058 assert( sqlite3_mutex_held(pBt->mutex) );
68059 assert( pBt->pPage1==0 );
68060 rc = sqlite3PagerSharedLock(pBt->pPager);
68068 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
68069 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
68070 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
68073 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
68079 u8 *page1 = pPage1->aData;
68081 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
68090 pBt->btsFlags |= BTS_READ_ONLY;
68097 pBt->btsFlags |= BTS_READ_ONLY;
68108 ** may not be the latest version - there may be a newer one in the log
68111 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
68113 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
68129 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
68138 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
68139 ** determined by the 2-byte integer located at an offset of 16 bytes from
68142 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
68144 if( ((pageSize-1)&pageSize)!=0
68150 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68152 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
68156 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
68157 ** determined by the one-byte unsigned integer found at an offset of 20
68159 usableSize = pageSize - page1[20];
68160 if( (u32)pageSize!=pBt->pageSize ){
68162 ** of BtShared.pageSize, we have discovered that the page-size is
68163 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
68165 ** again with the correct page-size.
68168 pBt->usableSize = usableSize;
68169 pBt->pageSize = pageSize;
68171 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
68172 pageSize-usableSize);
68175 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
68179 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
68185 pBt->pageSize = pageSize;
68186 pBt->usableSize = usableSize;
68188 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
68189 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
68195 ** cells can will fit on one page. We assume a 10-byte page header.
68197 ** 2-byte pointer to the cell
68198 ** 4-byte child pointer
68199 ** 9-byte nKey value
68200 ** 4-byte nData value
68201 ** 4-byte overflow page pointer
68202 ** So a cell consists of a 2-byte pointer, a header which is as much as
68206 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
68207 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
68208 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
68209 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
68210 if( pBt->maxLocal>127 ){
68211 pBt->max1bytePayload = 127;
68213 pBt->max1bytePayload = (u8)pBt->maxLocal;
68215 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
68216 pBt->pPage1 = pPage1;
68217 pBt->nPage = nPage;
68222 pBt->pPage1 = 0;
68242 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
68243 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
68244 && pCur->eState!=CURSOR_FAULT ) r++;
68256 ** If there is a transaction in progress, this routine is a no-op.
68259 assert( sqlite3_mutex_held(pBt->mutex) );
68260 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
68261 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
68262 MemPage *pPage1 = pBt->pPage1;
68263 assert( pPage1->aData );
68264 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
68265 pBt->pPage1 = 0;
68280 assert( sqlite3_mutex_held(pBt->mutex) );
68281 if( pBt->nPage>0 ){
68284 pP1 = pBt->pPage1;
68286 data = pP1->aData;
68287 rc = sqlite3PagerWrite(pP1->pDbPage);
68291 data[16] = (u8)((pBt->pageSize>>8)&0xff);
68292 data[17] = (u8)((pBt->pageSize>>16)&0xff);
68295 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
68296 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
68300 memset(&data[24], 0, 100-24);
68302 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
68304 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
68305 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
68306 put4byte(&data[36 + 4*4], pBt->autoVacuum);
68307 put4byte(&data[36 + 7*4], pBt->incrVacuum);
68309 pBt->nPage = 1;
68322 p->pBt->nPage = 0;
68323 rc = newDatabase(p->pBt);
68329 ** Attempt to start a new transaction. A write-transaction
68330 ** is started if the second argument is nonzero, otherwise a read-
68334 ** upgraded to exclusive by calling this routine a second time - the
68337 ** A write-transaction must be started before attempting any
68351 ** if there is one. But if there was previously a read-lock, do not
68352 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
68353 ** returned when there is already a read-lock in order to avoid a deadlock.
68364 BtShared *pBt = p->pBt;
68365 Pager *pPager = pBt->pPager;
68371 /* If the btree is already in a write-transaction, or it
68372 ** is already in a read-transaction and a read-transaction
68373 ** is requested, this is a no-op.
68375 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
68378 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
68380 if( (p->db->flags & SQLITE_ResetDatabase)
68383 pBt->btsFlags &= ~BTS_READ_ONLY;
68386 /* Write transactions are not possible on a read-only database */
68387 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
68396 ** on this shared-btree structure and a second write transaction is
68399 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
68400 || (pBt->btsFlags & BTS_PENDING)!=0
68402 pBlock = pBt->pWriter->db;
68405 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
68406 if( pIter->pBtree!=p ){
68407 pBlock = pIter->pBtree->db;
68413 sqlite3ConnectionBlocked(p->db, pBlock);
68420 /* Any read-only or read-write transaction implies a read-lock on
68421 ** page 1. So if some other shared-cache client already has a write-lock
68426 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
68427 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
68429 sqlite3PagerWalDb(pPager, p->db);
68434 if( pBt->pPage1==0 && wrflag ){
68435 assert( pBt->inTransaction==TRANS_NONE );
68441 /* Call lockBtree() until either pBt->pPage1 is populated or
68443 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
68444 ** reading page 1 it discovers that the page-size of the database
68445 ** file is not pBt->pageSize. In this case lockBtree() will update
68446 ** pBt->pageSize to the page-size of the file on disk.
68448 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
68451 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
68454 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
68457 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
68470 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
68478 if( p->inTrans==TRANS_NONE ){
68479 pBt->nTransaction++;
68481 if( p->sharable ){
68482 assert( p->lock.pBtree==p && p->lock.iTable==1 );
68483 p->lock.eLock = READ_LOCK;
68484 p->lock.pNext = pBt->pLock;
68485 pBt->pLock = &p->lock;
68489 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
68490 if( p->inTrans>pBt->inTransaction ){
68491 pBt->inTransaction = p->inTrans;
68494 MemPage *pPage1 = pBt->pPage1;
68496 assert( !pBt->pWriter );
68497 pBt->pWriter = p;
68498 pBt->btsFlags &= ~BTS_EXCLUSIVE;
68499 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
68502 /* If the db-size header field is incorrect (as it may be if an old
68505 ** re-read the database size from page 1 if a savepoint or transaction
68508 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
68509 rc = sqlite3PagerWrite(pPage1->pDbPage);
68511 put4byte(&pPage1->aData[28], pBt->nPage);
68520 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
68525 ** the sub-journal is not already open, then it will be opened here.
68527 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
68539 ** Set the pointer-map entries for all children of page pPage. Also, if
68547 BtShared *pBt = pPage->pBt;
68548 Pgno pgno = pPage->pgno;
68550 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68551 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
68553 nCell = pPage->nCell;
68560 if( !pPage->leaf ){
68566 if( !pPage->leaf ){
68567 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
68579 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
68582 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
68585 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
68589 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
68590 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
68593 if( get4byte(pPage->aData)!=iFrom ){
68596 put4byte(pPage->aData, iTo);
68602 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
68604 nCell = pPage->nCell;
68610 pPage->xParseCell(pPage, pCell, &info);
68612 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
68615 if( iFrom==get4byte(pCell+info.nSize-4) ){
68616 put4byte(pCell+info.nSize-4, iTo);
68630 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
68633 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
68645 ** the journal needs to be sync()ed before database page pDbPage->pgno
68653 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
68658 Pgno iDbPage = pDbPage->pgno;
68659 Pager *pPager = pBt->pPager;
68664 assert( sqlite3_mutex_held(pBt->mutex) );
68665 assert( pDbPage->pBt==pBt );
68671 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
68675 pDbPage->pgno = iFreePage;
68677 /* If pDbPage was a btree-page, then it may have child pages and/or cells
68691 Pgno nextOvfl = get4byte(pDbPage->aData);
68709 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
68727 ** Perform a single step of an incremental-vacuum. If successful, return
68732 ** More specifically, this function attempts to re-organize the database so
68738 ** If the bCommit parameter is non-zero, this function assumes that the
68740 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
68744 Pgno nFreeList; /* Number of pages still on the free-list */
68747 assert( sqlite3_mutex_held(pBt->mutex) );
68754 nFreeList = get4byte(&pBt->pPage1->aData[36]);
68769 /* Remove the page from the files free-list. This is not required
68770 ** if bCommit is non-zero. In that case, the free-list will be
68798 ** looping until a free-page located within the first nFin pages
68826 iLastPg--;
68828 pBt->bDoTruncate = 1;
68829 pBt->nPage = iLastPg;
68835 ** The database opened by the first argument is an auto-vacuum database
68837 ** size of the database in pages following an auto-vacuum operation.
68844 nEntry = pBt->usableSize/5;
68845 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
68846 nFin = nOrig - nFree - nPtrmap;
68848 nFin--;
68851 nFin--;
68858 ** A write-transaction must be opened before calling this function.
68867 BtShared *pBt = p->pBt;
68870 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
68871 if( !pBt->autoVacuum ){
68875 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
68887 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68888 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
68900 ** is committed for an auto-vacuum database.
68909 Pager *pPager = pBt->pPager;
68912 assert( sqlite3_mutex_held(pBt->mutex) );
68914 assert(pBt->autoVacuum);
68915 if( !pBt->incrVacuum ){
68924 ** is either a pointer-map page or the pending-byte page. If one
68930 nFree = get4byte(&pBt->pPage1->aData[36]);
68936 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
68940 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
68941 put4byte(&pBt->pPage1->aData[32], 0);
68942 put4byte(&pBt->pPage1->aData[36], 0);
68943 put4byte(&pBt->pPage1->aData[28], nFin);
68944 pBt->bDoTruncate = 1;
68945 pBt->nPage = nFin;
68961 ** This routine does the first phase of a two-phase commit. This routine
68973 ** This call is a no-op if no write-transaction is currently active on pBt.
68976 ** the name of a super-journal file that should be written into the
68977 ** individual journal file, or is NULL, indicating no super-journal file
68980 ** When this is called, the super-journal should already have been
68984 ** the write-transaction for this database file is to delete the journal.
68988 if( p->inTrans==TRANS_WRITE ){
68989 BtShared *pBt = p->pBt;
68992 if( pBt->autoVacuum ){
68999 if( pBt->bDoTruncate ){
69000 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
69003 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
69014 BtShared *pBt = p->pBt;
69015 sqlite3 *db = p->db;
69019 pBt->bDoTruncate = 0;
69021 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
69023 ** handle, downgrade to a read-only transaction. The other statements
69026 p->inTrans = TRANS_READ;
69032 if( p->inTrans!=TRANS_NONE ){
69034 pBt->nTransaction--;
69035 if( 0==pBt->nTransaction ){
69036 pBt->inTransaction = TRANS_NONE;
69042 p->inTrans = TRANS_NONE;
69052 ** This routine implements the second phase of a 2-phase commit. The
69064 ** is non-zero then this b-tree transaction is part of a multi-file
69066 ** (by deleting a super-journal file) and the caller will ignore this
69068 ** reset the b-tree objects internal state to indicate that the write
69077 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
69081 /* If the handle has a write-transaction open, commit the shared-btrees
69084 if( p->inTrans==TRANS_WRITE ){
69086 BtShared *pBt = p->pBt;
69087 assert( pBt->inTransaction==TRANS_WRITE );
69088 assert( pBt->nTransaction>0 );
69089 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
69094 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
69095 pBt->inTransaction = TRANS_READ;
69129 ** flag is true, then only write-cursors need be tripped - read-only
69133 ** rolled back modified the database schema. In this case b-tree root
69138 ** saving the current position of a read-only cursor, all cursors,
69139 ** including all read-cursors are tripped.
69151 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
69152 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
69153 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
69162 p->eState = CURSOR_FAULT;
69163 p->skipNext = errCode;
69173 ** Set the pBt->nPage field correctly, according to the current
69174 ** state of the database. Assume pBt->pPage1 is valid.
69177 int nPage = get4byte(&pPage1->aData[28]);
69179 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
69180 testcase( pBt->nPage!=nPage );
69181 pBt->nPage = nPage;
69197 BtShared *pBt = p->pBt;
69216 if( p->inTrans==TRANS_WRITE ){
69219 assert( TRANS_WRITE==pBt->inTransaction );
69220 rc2 = sqlite3PagerRollback(pBt->pPager);
69225 /* The rollback may have destroyed the pPage1->aData value. So
69227 ** sure pPage1->aData is set correctly. */
69233 pBt->inTransaction = TRANS_READ;
69253 ** A statement sub-transaction is implemented as an anonymous savepoint. The
69255 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
69256 ** are no active savepoints and no other statement-transactions open,
69262 BtShared *pBt = p->pBt;
69264 assert( p->inTrans==TRANS_WRITE );
69265 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
69267 assert( iStatement>p->db->nSavepoint );
69268 assert( pBt->inTransaction==TRANS_WRITE );
69274 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
69286 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
69293 if( p && p->inTrans==TRANS_WRITE ){
69294 BtShared *pBt = p->pBt;
69296 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
69302 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
69305 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
69306 pBt->nPage = 0;
69309 btreeSetNPage(pBt, pBt->pPage1);
69311 /* pBt->nPage might be zero if the database was corrupt when
69313 assert( CORRUPT_DB || pBt->nPage>0 );
69322 ** iTable. If a read-only cursor is requested, it is assumed that
69323 ** the caller already has at least a read-only transaction open
69324 ** on the database already. If a write-cursor is requested, then
69341 ** 3: The database must be writable (not on read-only media)
69352 ** operations on this cursor can be no-ops and all READ operations can
69353 ** return a null row (2-bytes: 0x01 0x00).
69356 ** root page of a b-tree. If it is not, then the cursor acquired
69365 int wrFlag, /* 1 to write. 0 read-only */
69369 BtShared *pBt = p->pBt; /* Shared b-tree handle */
69379 ** b-tree database, the connection is holding the required table locks,
69387 assert( p->inTrans>TRANS_NONE );
69388 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
69389 assert( pBt->pPage1 && pBt->pPage1->aData );
69390 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
69394 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
69407 pCur->pgnoRoot = iTable;
69408 pCur->iPage = -1;
69409 pCur->pKeyInfo = pKeyInfo;
69410 pCur->pBtree = p;
69411 pCur->pBt = pBt;
69412 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
69413 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
69416 for(pX=pBt->pCursor; pX; pX=pX->pNext){
69417 if( pX->pgnoRoot==iTable ){
69418 pX->curFlags |= BTCF_Multiple;
69419 pCur->curFlags |= BTCF_Multiple;
69422 pCur->pNext = pBt->pCursor;
69423 pBt->pCursor = pCur;
69424 pCur->eState = CURSOR_INVALID;
69430 int wrFlag, /* 1 to write. 0 read-only */
69443 int wrFlag, /* 1 to write. 0 read-only */
69447 if( p->sharable ){
69459 ** to users so they cannot do the sizeof() themselves - they must call
69472 ** of run-time by skipping the initialization of those elements.
69483 Btree *pBtree = pCur->pBtree;
69485 BtShared *pBt = pCur->pBt;
69487 assert( pBt->pCursor!=0 );
69488 if( pBt->pCursor==pCur ){
69489 pBt->pCursor = pCur->pNext;
69491 BtCursor *pPrev = pBt->pCursor;
69493 if( pPrev->pNext==pCur ){
69494 pPrev->pNext = pCur->pNext;
69497 pPrev = pPrev->pNext;
69502 sqlite3_free(pCur->aOverflow);
69503 sqlite3_free(pCur->pKey);
69504 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
69507 assert( pBtree->sharable==0 );
69512 pCur->pBtree = 0;
69527 if( a->nKey!=b->nKey ) return 0;
69528 if( a->pPayload!=b->pPayload ) return 0;
69529 if( a->nPayload!=b->nPayload ) return 0;
69530 if( a->nLocal!=b->nLocal ) return 0;
69531 if( a->nSize!=b->nSize ) return 0;
69537 btreeParseCell(pCur->pPage, pCur->ix, &info);
69538 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
69544 if( pCur->info.nSize==0 ){
69545 pCur->curFlags |= BTCF_ValidNKey;
69546 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
69555 ** that is currently pointing to a row in a (non-empty) table.
69559 return pCur && pCur->eState==CURSOR_VALID;
69564 return pCur->eState==CURSOR_VALID;
69575 assert( pCur->eState==CURSOR_VALID );
69576 assert( pCur->curIntKey );
69578 return pCur->info.nKey;
69585 assert( (pCur->curFlags & BTCF_Pinned)==0 );
69586 pCur->curFlags |= BTCF_Pinned;
69589 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
69590 pCur->curFlags &= ~BTCF_Pinned;
69600 assert( pCur->eState==CURSOR_VALID );
69602 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
69603 (i64)(pCur->info.pPayload - pCur->pPage->aData);
69612 ** The caller must guarantee that the cursor is pointing to a non-NULL
69618 assert( pCur->eState==CURSOR_VALID );
69620 return pCur->info.nPayload;
69638 assert( pCur->eState==CURSOR_VALID );
69639 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
69645 ** linked list of overflow pages. If possible, it uses the auto-vacuum
69646 ** pointer-map data instead of reading the content of page ovfl to do so.
69658 ** the pointer-map was used to obtain the value for *pPgnoNext), then
69671 assert( sqlite3_mutex_held(pBt->mutex) );
69676 ** autovacuum pointer-map pages. Guess that the next page in
69681 if( pBt->autoVacuum ){
69705 next = get4byte(pPage->aData);
69733 int eOp, /* 0 -> copy from page, 1 -> copy to page */
69766 ** the overflow page-list cache array (BtCursor.aOverflow).
69770 ** Once an overflow page-list cache has been allocated, it must be
69772 ** the cursor is moved to a different row. Additionally, in auto-vacuum
69773 ** mode, the following events may invalidate an overflow page-list cache.
69784 int eOp /* zero to read. non-zero to write. */
69789 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
69790 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
69797 assert( pCur->eState==CURSOR_VALID );
69798 assert( pCur->ix<pPage->nCell );
69802 aPayload = pCur->info.pPayload;
69803 assert( offset+amt <= pCur->info.nPayload );
69805 assert( aPayload > pPage->aData );
69806 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
69809 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
69816 if( offset<pCur->info.nLocal ){
69818 if( a+offset>pCur->info.nLocal ){
69819 a = pCur->info.nLocal - offset;
69821 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
69824 amt -= a;
69826 offset -= pCur->info.nLocal;
69831 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
69834 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
69843 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
69844 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
69845 if( pCur->aOverflow==0
69846 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
69849 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
69854 pCur->aOverflow = aNew;
69857 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
69858 pCur->curFlags |= BTCF_ValidOvfl;
69860 /* If the overflow page-list cache has been allocated and the
69864 if( pCur->aOverflow[offset/ovflSize] ){
69866 nextPage = pCur->aOverflow[iIdx];
69873 /* If required, populate the overflow page-list cache. */
69874 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
69875 assert( pCur->aOverflow[iIdx]==0
69876 || pCur->aOverflow[iIdx]==nextPage
69878 pCur->aOverflow[iIdx] = nextPage;
69884 ** page-list cache, if any, then fall back to the getOverflowPage()
69887 assert( pCur->curFlags & BTCF_ValidOvfl );
69888 assert( pCur->pBtree->db==pBt->db );
69889 if( pCur->aOverflow[iIdx+1] ){
69890 nextPage = pCur->aOverflow[iIdx+1];
69894 offset -= ovflSize;
69901 a = ovflSize - offset;
69909 ** 3) there are no dirty pages in the page-cache
69910 ** 4) the database is file-backed, and
69915 ** output buffer, bypassing the page-cache altogether. This speeds
69920 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
69921 && &pBuf[-4]>=pBufStart /* (6) */
69923 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
69925 u8 *aWrite = &pBuf[-4];
69928 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
69929 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
69937 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
69948 amt -= a;
69969 ** pCur can be pointing to either a table or an index b-tree.
69971 ** pCur is pointing to an index b-tree then the key section is read.
69983 assert( pCur->eState==CURSOR_VALID );
69984 assert( pCur->iPage>=0 && pCur->pPage );
69985 assert( pCur->ix<pCur->pPage->nCell );
70002 if ( pCur->eState==CURSOR_INVALID ){
70010 if( pCur->eState==CURSOR_VALID ){
70022 ** the key if index btrees (pPage->intKey==0) and is the data for
70023 ** table btrees (pPage->intKey==1). The number of bytes of available
70043 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
70044 assert( pCur->eState==CURSOR_VALID );
70045 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70047 assert( pCur->ix<pCur->pPage->nCell );
70048 assert( pCur->info.nSize>0 );
70049 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
70050 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
70051 amt = pCur->info.nLocal;
70052 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
70056 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
70059 return (void*)pCur->info.pPayload;
70066 ** b-tree page. Write the number of available bytes into *pAmt.
70086 ** This function returns SQLITE_CORRUPT if the page-header flags field of
70088 ** if an intkey page appears to be the parent of a non-intkey page, or
70089 ** vice-versa).
70092 BtShared *pBt = pCur->pBt;
70095 assert( pCur->eState==CURSOR_VALID );
70096 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
70097 assert( pCur->iPage>=0 );
70098 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
70101 pCur->info.nSize = 0;
70102 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70103 pCur->aiIdx[pCur->iPage] = pCur->ix;
70104 pCur->apPage[pCur->iPage] = pCur->pPage;
70105 pCur->ix = 0;
70106 pCur->iPage++;
70107 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
70112 ** Page pParent is an internal (non-leaf) tree page. This function
70113 ** asserts that page number iChild is the left-child if the iIdx'th
70115 ** cells in pParent, that page number iChild is the right-child of
70121 assert( iIdx<=pParent->nCell );
70122 if( iIdx==pParent->nCell ){
70123 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
70135 ** pCur->idx is set to the cell index that contains the pointer
70137 ** right-most child page then pCur->idx is set to one more than
70143 assert( pCur->eState==CURSOR_VALID );
70144 assert( pCur->iPage>0 );
70145 assert( pCur->pPage );
70147 pCur->apPage[pCur->iPage-1],
70148 pCur->aiIdx[pCur->iPage-1],
70149 pCur->pPage->pgno
70151 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
70152 pCur->info.nSize = 0;
70153 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70154 pCur->ix = pCur->aiIdx[pCur->iPage-1];
70155 pLeaf = pCur->pPage;
70156 pCur->pPage = pCur->apPage[--pCur->iPage];
70161 ** Move the cursor to point to the root page of its b-tree structure.
70168 ** If the b-tree structure is empty, the cursor state is set to
70174 ** page-header flags indicate that the [virtual] root-page is the expected
70175 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
70177 ** indicating a table b-tree, or if the caller did specify a KeyInfo
70179 ** b-tree).
70189 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
70190 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
70192 if( pCur->iPage>=0 ){
70193 if( pCur->iPage ){
70194 releasePageNotNull(pCur->pPage);
70195 while( --pCur->iPage ){
70196 releasePageNotNull(pCur->apPage[pCur->iPage]);
70198 pCur->pPage = pCur->apPage[0];
70201 }else if( pCur->pgnoRoot==0 ){
70202 pCur->eState = CURSOR_INVALID;
70205 assert( pCur->iPage==(-1) );
70206 if( pCur->eState>=CURSOR_REQUIRESEEK ){
70207 if( pCur->eState==CURSOR_FAULT ){
70208 assert( pCur->skipNext!=SQLITE_OK );
70209 return pCur->skipNext;
70213 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
70214 0, pCur->curPagerFlags);
70216 pCur->eState = CURSOR_INVALID;
70219 pCur->iPage = 0;
70220 pCur->curIntKey = pCur->pPage->intKey;
70222 pRoot = pCur->pPage;
70223 assert( pRoot->pgno==pCur->pgnoRoot );
70225 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
70226 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
70227 ** NULL, the caller expects a table b-tree. If this is not the case,
70232 ** if pCur->iPage>=0). But this is not so if the database is corrupted
70233 ** in such a way that page pRoot is linked into a second b-tree table
70235 assert( pRoot->intKey==1 || pRoot->intKey==0 );
70236 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
70237 return SQLITE_CORRUPT_PAGE(pCur->pPage);
70241 pCur->ix = 0;
70242 pCur->info.nSize = 0;
70243 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
70245 pRoot = pCur->pPage;
70246 if( pRoot->nCell>0 ){
70247 pCur->eState = CURSOR_VALID;
70248 }else if( !pRoot->leaf ){
70250 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
70251 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
70252 pCur->eState = CURSOR_VALID;
70255 pCur->eState = CURSOR_INVALID;
70262 ** Move the cursor down to the left-most leaf entry beneath the
70265 ** The left-most leaf is the one with the smallest key - the first
70274 assert( pCur->eState==CURSOR_VALID );
70275 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
70276 assert( pCur->ix<pPage->nCell );
70277 pgno = get4byte(findCell(pPage, pCur->ix));
70284 ** Move the cursor down to the right-most leaf entry beneath the
70287 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
70288 ** finds the right-most entry beneath the *page*.
70290 ** The right-most entry is the one with the largest key - the last
70299 assert( pCur->eState==CURSOR_VALID );
70300 while( !(pPage = pCur->pPage)->leaf ){
70301 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70302 pCur->ix = pPage->nCell;
70306 pCur->ix = pPage->nCell-1;
70307 assert( pCur->info.nSize==0 );
70308 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
70320 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70323 assert( pCur->pPage->nCell>0 );
70327 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
70342 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70344 /* If the cursor already points to the last entry, this is a no-op. */
70345 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
70348 ** to the last entry in the b-tree. */
70350 for(ii=0; ii<pCur->iPage; ii++){
70351 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
70353 assert( pCur->ix==pCur->pPage->nCell-1 );
70354 assert( pCur->pPage->leaf );
70362 assert( pCur->eState==CURSOR_VALID );
70366 pCur->curFlags |= BTCF_AtLast;
70368 pCur->curFlags &= ~BTCF_AtLast;
70371 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
70405 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
70419 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70421 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
70422 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
70427 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
70429 if( pCur->info.nKey==intKey ){
70433 if( pCur->info.nKey<intKey ){
70434 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
70435 *pRes = -1;
70442 if( pCur->info.nKey+1==intKey ){
70447 if( pCur->info.nKey==intKey ){
70460 pCur->pBtree->nSeek++; /* Performance measurement during testing */
70465 pIdxKey->errCode = 0;
70466 assert( pIdxKey->default_rc==1
70467 || pIdxKey->default_rc==0
70468 || pIdxKey->default_rc==-1
70477 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
70478 *pRes = -1;
70483 assert( pCur->pPage );
70484 assert( pCur->pPage->isInit );
70485 assert( pCur->eState==CURSOR_VALID );
70486 assert( pCur->pPage->nCell > 0 );
70487 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
70488 assert( pCur->curIntKey || pIdxKey );
70492 MemPage *pPage = pCur->pPage;
70495 /* pPage->nCell must be greater than zero. If this is the root-page
70497 ** not run. If this is not the root-page, then the moveToChild() routine
70499 ** be the right kind (index or table) of b-tree page. Otherwise
70501 assert( pPage->nCell>0 );
70502 assert( pPage->intKey==(pIdxKey==0) );
70504 upr = pPage->nCell-1;
70506 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
70507 pCur->ix = (u16)idx;
70512 if( pPage->intKeyLeaf ){
70514 if( pCell>=pPage->aDataEnd ){
70522 if( lwr>upr ){ c = -1; break; }
70524 upr = idx-1;
70528 pCur->ix = (u16)idx;
70529 if( !pPage->leaf ){
70533 pCur->curFlags |= BTCF_ValidNKey;
70534 pCur->info.nKey = nCellKey;
70535 pCur->info.nSize = 0;
70548 /* The maximum supported page-size is 65536 bytes. This means that
70549 ** the maximum number of record bytes stored on an index B-Tree
70550 ** page is less than 16384 bytes and may be stored as a 2-byte
70553 ** stored entirely within the b-tree page by inspecting the first
70557 if( nCell<=pPage->max1bytePayload ){
70558 /* This branch runs if the record-size field of the cell is a
70560 ** b-tree page. */
70561 testcase( pCell+nCell+1==pPage->aDataEnd );
70564 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
70566 /* The record-size field is a 2 byte varint and the record
70567 ** fits entirely on the main b-tree page. */
70568 testcase( pCell+nCell+2==pPage->aDataEnd );
70581 u8 * const pCellBody = pCell - pPage->childPtrSize;
70583 pPage->xParseCell(pPage, pCellBody, &pCur->info);
70584 nCell = (int)pCur->info.nKey;
70589 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
70598 pCur->ix = (u16)idx;
70601 pCur->curFlags &= ~BTCF_ValidOvfl;
70610 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
70611 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
70616 upr = idx-1;
70621 pCur->ix = (u16)idx;
70622 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
70630 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
70631 assert( pPage->isInit );
70632 if( pPage->leaf ){
70633 assert( pCur->ix<pCur->pPage->nCell );
70634 pCur->ix = (u16)idx;
70640 if( lwr>=pPage->nCell ){
70641 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
70645 pCur->ix = (u16)lwr;
70650 pCur->info.nSize = 0;
70651 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
70668 return (CURSOR_VALID!=pCur->eState);
70681 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
70686 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
70687 if( NEVER(pCur->pPage->leaf==0) ) return -1;
70689 n = pCur->pPage->nCell;
70690 for(i=0; i<pCur->iPage; i++){
70691 n *= pCur->apPage[i]->nCell;
70722 if( pCur->eState!=CURSOR_VALID ){
70723 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
70728 if( CURSOR_INVALID==pCur->eState ){
70731 if( pCur->eState==CURSOR_SKIPNEXT ){
70732 pCur->eState = CURSOR_VALID;
70733 if( pCur->skipNext>0 ) return SQLITE_OK;
70737 pPage = pCur->pPage;
70738 idx = ++pCur->ix;
70739 if( !pPage->isInit || sqlite3FaultSim(412) ){
70743 ** in a corrupt database file where the table being DELETE-ed from
70745 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
70754 ** page into more than one b-tree structure.
70756 ** Update 2019-12-23: appears to long longer be possible after the
70758 harmless( idx>pPage->nCell );
70760 if( idx>=pPage->nCell ){
70761 if( !pPage->leaf ){
70762 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
70767 if( pCur->iPage==0 ){
70768 pCur->eState = CURSOR_INVALID;
70772 pPage = pCur->pPage;
70773 }while( pCur->ix>=pPage->nCell );
70774 if( pPage->intKey ){
70780 if( pPage->leaf ){
70791 pCur->info.nSize = 0;
70792 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
70793 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
70794 pPage = pCur->pPage;
70795 if( (++pCur->ix)>=pPage->nCell ){
70796 pCur->ix--;
70799 if( pPage->leaf ){
70831 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
70832 assert( pCur->info.nSize==0 );
70833 if( pCur->eState!=CURSOR_VALID ){
70838 if( CURSOR_INVALID==pCur->eState ){
70841 if( CURSOR_SKIPNEXT==pCur->eState ){
70842 pCur->eState = CURSOR_VALID;
70843 if( pCur->skipNext<0 ) return SQLITE_OK;
70847 pPage = pCur->pPage;
70848 assert( pPage->isInit );
70849 if( !pPage->leaf ){
70850 int idx = pCur->ix;
70855 while( pCur->ix==0 ){
70856 if( pCur->iPage==0 ){
70857 pCur->eState = CURSOR_INVALID;
70862 assert( pCur->info.nSize==0 );
70863 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
70865 pCur->ix--;
70866 pPage = pCur->pPage;
70867 if( pPage->intKey && !pPage->leaf ){
70879 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
70880 pCur->info.nSize = 0;
70881 if( pCur->eState!=CURSOR_VALID
70882 || pCur->ix==0
70883 || pCur->pPage->leaf==0
70887 pCur->ix--;
70908 ** anywhere on the free-list, then it is guaranteed to be returned. If
70928 assert( sqlite3_mutex_held(pBt->mutex) );
70929 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
70930 pPage1 = pBt->pPage1;
70932 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
70934 n = get4byte(&pPage1->aData[36]);
70935 testcase( n==mxPage-1 );
70942 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
70945 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
70946 ** shows that the page 'nearby' is somewhere on the free-list, then
70947 ** the entire-list will be searched for that page.
70954 assert( pBt->autoVacuum );
70966 /* Decrement the free-list count by 1. Set iTrunk to the index of the
70967 ** first free-list trunk page. iPrevTrunk is initially 1.
70969 rc = sqlite3PagerWrite(pPage1->pDbPage);
70971 put4byte(&pPage1->aData[36], n-1);
70974 ** is not true. Otherwise, it runs once for each trunk-page on the
70975 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
70981 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
70984 iTrunk = get4byte(&pPrevTrunk->aData[0]);
70986 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
70989 iTrunk = get4byte(&pPage1->aData[32]);
70993 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
71002 assert( pTrunk->aData!=0 );
71003 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
71005 k = get4byte(&pTrunk->aData[4]);
71011 rc = sqlite3PagerWrite(pTrunk->pDbPage);
71016 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
71019 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
71020 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
71034 rc = sqlite3PagerWrite(pTrunk->pDbPage);
71040 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
71042 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
71046 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
71050 ** pointers to free-list leaves. The first leaf becomes a trunk
71054 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
71064 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
71069 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
71070 put4byte(&pNewTrunk->aData[4], k-1);
71071 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
71074 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
71075 put4byte(&pPage1->aData[32], iNewTrunk);
71077 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
71081 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
71085 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
71091 unsigned char *aData = pTrunk->aData;
71105 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
71107 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
71132 *pPgno, closest+1, k, pTrunk->pgno, n-1));
71133 rc = sqlite3PagerWrite(pTrunk->pDbPage);
71135 if( closest<k-1 ){
71138 put4byte(&aData[4], k-1);
71142 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
71159 ** pager layer with the 'no-content' flag set. This prevents the pager
71161 ** current transaction has already run one or more incremental-vacuum
71164 ** not set the no-content flag. This causes the pager to load and journal
71169 ** file on disk. So the effects of disabling the no-content optimization
71173 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
71175 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
71177 pBt->nPage++;
71178 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
71181 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
71182 /* If *pPgno refers to a pointer-map page, allocate two new pages
71184 ** becomes a new pointer-map page, the second is used by the caller.
71187 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
71188 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
71189 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
71191 rc = sqlite3PagerWrite(pPg->pDbPage);
71195 pBt->nPage++;
71196 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
71199 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
71200 *pPgno = pBt->nPage;
71205 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
71218 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
71219 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
71224 ** This function is used to add page iPage to the database file free-list.
71225 ** It is assumed that the page is not already a part of the free-list.
71236 MemPage *pTrunk = 0; /* Free-list trunk page */
71237 Pgno iTrunk = 0; /* Page number of free-list trunk page */
71238 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
71241 u32 nFree; /* Initial number of pages on free-list */
71243 assert( sqlite3_mutex_held(pBt->mutex) );
71245 assert( !pMemPage || pMemPage->pgno==iPage );
71247 if( iPage<2 || iPage>pBt->nPage ){
71252 sqlite3PagerRef(pPage->pDbPage);
71258 rc = sqlite3PagerWrite(pPage1->pDbPage);
71260 nFree = get4byte(&pPage1->aData[36]);
71261 put4byte(&pPage1->aData[36], nFree+1);
71263 if( pBt->btsFlags & BTS_SECURE_DELETE ){
71268 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
71272 memset(pPage->aData, 0, pPage->pBt->pageSize);
71275 /* If the database supports auto-vacuum, write an entry in the pointer-map
71283 /* Now manipulate the actual database free-list structure. There are two
71284 ** possibilities. If the free-list is currently empty, or if the first
71285 ** trunk page in the free-list is full, then this page will become a
71286 ** new free-list trunk page. Otherwise, it will become a leaf of the
71287 ** first trunk page in the current free-list. This block tests if it
71288 ** is possible to add the page as a new free-list leaf.
71293 iTrunk = get4byte(&pPage1->aData[32]);
71303 nLeaf = get4byte(&pTrunk->aData[4]);
71304 assert( pBt->usableSize>32 );
71305 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
71309 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
71314 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
71317 ** usableSize/4 - 8 entries will be reported as corrupt. In order
71319 ** we will continue to restrict the number of entries to usableSize/4 - 8
71322 ** to read "usableSize/4-2" instead of "usableSize/4-8".
71324 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
71329 rc = sqlite3PagerWrite(pTrunk->pDbPage);
71331 put4byte(&pTrunk->aData[4], nLeaf+1);
71332 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
71333 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
71334 sqlite3PagerDontWrite(pPage->pDbPage);
71338 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
71344 ** the page being freed as a leaf page of the first trunk in the free-list.
71345 ** Possibly because the free-list is empty, or possibly because the
71346 ** first trunk in the free-list is full. Either way, the page being freed
71347 ** will become the new first trunk page in the free-list.
71352 rc = sqlite3PagerWrite(pPage->pDbPage);
71356 put4byte(pPage->aData, iTrunk);
71357 put4byte(&pPage->aData[4], 0);
71358 put4byte(&pPage1->aData[32], iPage);
71359 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
71363 pPage->isInit = 0;
71371 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
71390 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71391 pPage->xParseCell(pPage, pCell, pInfo);
71392 if( pInfo->nLocal==pInfo->nPayload ){
71395 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
71396 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
71397 if( pCell + pInfo->nSize > pPage->aDataEnd ){
71401 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
71402 pBt = pPage->pBt;
71403 assert( pBt->usableSize > 4 );
71404 ovflPageSize = pBt->usableSize - 4;
71405 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
71407 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
71409 while( nOvfl-- ){
71424 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
71431 ** freePage2() may zero the page contents if secure-delete mode is
71442 sqlite3PagerUnref(pOvfl->pDbPage);
71457 ** Note that pCell does not necessary need to point to the pPage->aData
71459 ** be constructed in this temporary area then copied into pPage->aData
71479 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71483 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
71484 || sqlite3PagerIswriteable(pPage->pDbPage) );
71487 nHeader = pPage->childPtrSize;
71488 if( pPage->intKey ){
71489 nPayload = pX->nData + pX->nZero;
71490 pSrc = pX->pData;
71491 nSrc = pX->nData;
71492 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
71494 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
71496 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
71497 nSrc = nPayload = (int)pX->nKey;
71498 pSrc = pX->pKey;
71504 if( nPayload<=pPage->maxLocal ){
71515 memset(pPayload+nSrc, 0, nPayload-nSrc);
71522 mn = pPage->minLocal;
71523 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
71524 testcase( n==pPage->maxLocal );
71525 testcase( n==pPage->maxLocal+1 );
71526 if( n > pPage->maxLocal ) n = mn;
71532 pBt = pPage->pBt;
71549 pPage->xParseCell(pPage, pCell, &info);
71550 assert( nHeader==(int)(info.pPayload - pCell) );
71551 assert( info.nKey==pX->nKey );
71564 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
71568 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
71569 || sqlite3PagerIswriteable(pPage->pDbPage) );
71579 nPayload -= n;
71583 nSrc -= n;
71584 spaceLeft -= n;
71588 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
71589 if( pBt->autoVacuum ){
71599 /* If the database supports auto-vacuum, and the second or subsequent
71600 ** overflow page is being allocated, add an entry to the pointer-map
71604 ** to the pointer-map. If we write nothing to this pointer-map slot,
71609 if( pBt->autoVacuum && rc==SQLITE_OK ){
71624 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
71628 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
71629 || sqlite3PagerIswriteable(pPage->pDbPage) );
71634 pPrior = pOvfl->aData;
71636 pPayload = &pOvfl->aData[4];
71637 spaceLeft = pBt->usableSize - 4;
71645 ** Remove the i-th cell from pPage. This routine effects pPage only.
71654 u8 *data; /* pPage->aData */
71660 assert( idx>=0 && idx<pPage->nCell );
71662 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71663 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71664 assert( pPage->nFree>=0 );
71665 data = pPage->aData;
71666 ptr = &pPage->aCellIdx[2*idx];
71668 hdr = pPage->hdrOffset;
71670 testcase( pc+sz==pPage->pBt->usableSize );
71671 if( pc+sz > pPage->pBt->usableSize ){
71680 pPage->nCell--;
71681 if( pPage->nCell==0 ){
71684 put2byte(&data[hdr+5], pPage->pBt->usableSize);
71685 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
71686 - pPage->childPtrSize - 8;
71688 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
71689 put2byte(&data[hdr+3], pPage->nCell);
71690 pPage->nFree += 2;
71701 ** in pPage->apOvfl[] and make it point to the cell content (either
71703 ** Allocating a new entry in pPage->aCell[] implies that
71704 ** pPage->nOverflow is incremented.
71710 int i, /* New cell becomes the i-th cell of the page */
71714 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
71720 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
71723 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
71724 assert( MX_CELL(pPage->pBt)<=10921 );
71725 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
71726 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
71727 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
71728 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
71729 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
71730 assert( pPage->nFree>=0 );
71731 if( pPage->nOverflow || sz+2>pPage->nFree ){
71739 j = pPage->nOverflow++;
71740 /* Comparison against ArraySize-1 since we hold back one extra slot
71743 assert( j < ArraySize(pPage->apOvfl)-1 );
71744 pPage->apOvfl[j] = pCell;
71745 pPage->aiOvfl[j] = (u16)i;
71752 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
71753 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
71755 int rc = sqlite3PagerWrite(pPage->pDbPage);
71760 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
71761 data = pPage->aData;
71762 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
71768 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
71769 assert( idx+sz <= (int)pPage->pBt->usableSize );
71770 pPage->nFree -= (u16)(2 + sz);
71777 memcpy(&data[idx+4], pCell+4, sz-4);
71782 pIns = pPage->aCellIdx + i*2;
71783 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
71785 pPage->nCell++;
71787 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
71788 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
71790 if( pPage->pBt->autoVacuum ){
71813 ** tunable - as if you could change them and recompile and it would all work.
71828 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
71840 ** -----------
71842 ** -----------
71845 ** --------- --------- ---------
71846 ** |Child-1| |Child-2| |Child-3|
71847 ** --------- --------- ---------
71851 ** 1. All cells from Child-1 in order
71853 ** 3. All cells from Child-2 in order
71855 ** 5. All cells from Child-3 in order
71857 ** For a table-btree (with rowids) the items 2 and 4 are empty because
71861 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
71865 ** ixNx[0] = Number of cells in Child-1.
71866 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
71867 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
71868 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
71871 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
71874 ** ixNx[0] = Number of cells in Child-1.
71875 ** ixNx[1] = Number of cells in Child-1 and Child-2.
71894 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
71898 assert( idx>=0 && idx+N<=p->nCell );
71900 assert( p->apCell[idx]!=0 );
71901 if( p->szCell[idx]==0 ){
71902 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
71905 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
71908 N--;
71916 assert( N>=0 && N<p->nCell );
71917 assert( p->szCell[N]==0 );
71918 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
71919 return p->szCell[N];
71922 assert( N>=0 && N<p->nCell );
71923 if( p->szCell[N] ) return p->szCell[N];
71928 ** Array apCell[] contains pointers to nCell b-tree page cells. The
71946 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
71947 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
71948 const int usableSize = pPg->pBt->usableSize;
71953 u8 *pCellptr = pPg->aCellIdx;
71954 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
71956 int k; /* Current slot in pCArray->apEnd[] */
71957 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
71962 memcpy(&pTmp[j], &aData[j], usableSize - j);
71964 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
71965 pSrcEnd = pCArray->apEnd[k];
71969 u8 *pCell = pCArray->apCell[i];
71970 u16 sz = pCArray->szCell[i];
71974 pCell = &pTmp[pCell - aData];
71981 pData -= sz;
71982 put2byte(pCellptr, (pData - aData));
71986 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
71987 testcase( sz!=pPg->xCellSize(pPg,pCell) )
71990 if( pCArray->ixNx[k]<=i ){
71992 pSrcEnd = pCArray->apEnd[k];
71996 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
71997 pPg->nCell = nCell;
71998 pPg->nOverflow = 0;
72001 put2byte(&aData[hdr+3], pPg->nCell);
72002 put2byte(&aData[hdr+5], pData - aData);
72008 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
72011 ** will fit), non-zero is returned. Otherwise, if the cells are added
72014 ** Argument pCellptr points to the first entry in the cell-pointer array
72016 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
72018 ** that it is safe to overwrite this part of the cell-pointer array.
72026 ** end of the space required by this page for the cell-pointer area (for
72027 ** all cells - not just those inserted by the current call). If the content
72029 ** cells in apCell[], then the cells do not fit and non-zero is returned.
72033 u8 *pBegin, /* End of cell-pointer array */
72034 u8 **ppData, /* IN/OUT: Page content-area pointer */
72035 u8 *pCellptr, /* Pointer to cell-pointer area */
72040 int i = iFirst; /* Loop counter - cell index to insert */
72041 u8 *aData = pPg->aData; /* Complete page */
72044 int k; /* Current slot in pCArray->apEnd[] */
72046 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
72048 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
72049 pEnd = pCArray->apEnd[k];
72053 assert( pCArray->szCell[i]!=0 );
72054 sz = pCArray->szCell[i];
72056 if( (pData - pBegin)<sz ) return 1;
72057 pData -= sz;
72060 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
72063 assert( (pSlot+sz)<=pCArray->apCell[i]
72064 || pSlot>=(pCArray->apCell[i]+sz)
72066 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
72067 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
72073 memmove(pSlot, pCArray->apCell[i], sz);
72074 put2byte(pCellptr, (pSlot - aData));
72078 if( pCArray->ixNx[k]<=i ){
72080 pEnd = pCArray->apEnd[k];
72088 ** The pCArray object contains pointers to b-tree cells and their sizes.
72091 ** that is currently stored within the body of pPg to the pPg free-list.
72092 ** The cell-pointers and other fields of the page are not updated.
72094 ** This function returns the total number of cells added to the free-list.
72102 u8 * const aData = pPg->aData;
72103 u8 * const pEnd = &aData[pPg->pBt->usableSize];
72104 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
72112 u8 *pCell = pCArray->apCell[i];
72118 sz = pCArray->szCell[i]; assert( sz>0 );
72121 assert( pFree>aData && (pFree - aData)<65536 );
72122 freeSpace(pPg, (u16)(pFree - aData), szFree);
72135 assert( pFree>aData && (pFree - aData)<65536 );
72136 freeSpace(pPg, (u16)(pFree - aData), szFree);
72143 ** balanced. The current page, pPg, has pPg->nCell cells starting with
72144 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
72150 ** The pPg->nFree field is invalid when this function returns. It is the
72160 u8 * const aData = pPg->aData;
72161 const int hdr = pPg->hdrOffset;
72162 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
72163 int nCell = pPg->nCell; /* Cells stored on pPg */
72167 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
72171 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
72172 memcpy(pTmp, aData, pPg->pBt->usableSize);
72178 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
72180 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
72181 nCell -= nShift;
72184 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
72186 nCell -= nTail;
72194 int nAdd = MIN(nNew,iOld-iNew);
72195 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
72197 pCellptr = pPg->aCellIdx;
72207 for(i=0; i<pPg->nOverflow; i++){
72208 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
72210 pCellptr = &pPg->aCellIdx[iCell * 2];
72212 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
72225 pCellptr = &pPg->aCellIdx[nCell*2];
72228 iNew+nCell, nNew-nCell, pCArray
72231 pPg->nCell = nNew;
72232 pPg->nOverflow = 0;
72234 put2byte(&aData[hdr+3], pPg->nCell);
72235 put2byte(&aData[hdr+5], pData - aData);
72239 u8 *pCell = pCArray->apCell[i+iNew];
72240 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
72241 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
72242 pCell = &pTmp[pCell - aData];
72245 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
72260 ** a new entry is being inserted on the extreme right-end of the
72264 ** Instead of trying to balance the 3 right-most leaf pages, just add
72265 ** a new page to the right-hand side and put the one new entry in
72271 ** pPage is the leaf page which is the right-most page in the tree.
72273 ** which is also the right-most entry on the page.
72282 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
72287 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
72288 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72289 assert( pPage->nOverflow==1 );
72291 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
72292 assert( pPage->nFree>=0 );
72293 assert( pParent->nFree>=0 );
72295 /* Allocate a new page. This page will become the right-sibling of
72304 u8 *pCell = pPage->apOvfl[0];
72305 u16 szCell = pPage->xCellSize(pPage, pCell);
72309 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
72310 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
72316 b.apEnd[0] = pPage->aDataEnd;
72323 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
72325 /* If this is an auto-vacuum database, update the pointer map
72335 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
72336 if( szCell>pNew->minLocal ){
72342 ** consists of a 4-byte page number (the page number of pPage) and
72346 ** To find the largest key value on pPage, first find the right-most
72348 ** record-length (a variable length integer at most 32-bits in size)
72350 ** The first of the while(...) loops below skips over the record-length
72354 pCell = findCell(pPage, pPage->nCell-1);
72362 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
72363 0, pPage->pgno, &rc);
72366 /* Set the right-child pointer of pParent to point to the new page. */
72367 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
72381 ** for setting pointer-map entries.
72389 BtShared *pBt = pPage->pBt;
72390 assert( pPage->isInit );
72392 for(j=0; j<pPage->nCell; j++){
72397 pPage->xParseCell(pPage, z, &info);
72399 Pgno ovfl = get4byte(&z[info.nSize-4]);
72401 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
72403 if( !pPage->leaf ){
72406 assert( n==pPage->pgno && e==PTRMAP_BTREE );
72409 if( !pPage->leaf ){
72410 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
72412 assert( n==pPage->pgno && e==PTRMAP_BTREE );
72420 ** This function is used to copy the contents of the b-tree node stored
72422 ** the pointer-map entries for each child page are updated so that the
72438 BtShared * const pBt = pFrom->pBt;
72439 u8 * const aFrom = pFrom->aData;
72440 u8 * const aTo = pTo->aData;
72441 int const iFromHdr = pFrom->hdrOffset;
72442 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
72447 assert( pFrom->isInit );
72448 assert( pFrom->nFree>=iToHdr );
72449 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
72451 /* Copy the b-tree node content from page pFrom to page pTo. */
72453 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
72454 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
72461 pTo->isInit = 0;
72469 /* If this is an auto-vacuum database, update the pointer-map entries
72470 ** for any b-tree or overflow pages that pTo now contains the pointers to.
72512 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
72521 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
72522 int isRoot, /* True if pParent is a root-page */
72530 int nxDiv; /* Next divider slot in pParent->aCell[] */
72535 int pageFlags; /* Value of pPage->aData[0] */
72541 u8 *pRight; /* Location in parent of right-sibling pointer */
72542 u8 *apDiv[NB-1]; /* Divider cells in pParent */
72543 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
72545 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
72557 pBt = pParent->pBt;
72558 assert( sqlite3_mutex_held(pBt->mutex) );
72559 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
72566 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
72567 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
72572 assert( pParent->nFree>=0 );
72585 i = pParent->nOverflow + pParent->nCell;
72593 nxDiv = i-2+bBulk;
72595 nxDiv = iParentIdx-1;
72597 i = 2-bBulk;
72600 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
72601 pRight = &pParent->aData[pParent->hdrOffset+8];
72603 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
72614 if( apOld[i]->nFree<0 ){
72621 if( (i--)==0 ) break;
72623 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
72624 apDiv[i] = pParent->apOvfl[0];
72626 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
72627 pParent->nOverflow = 0;
72629 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
72631 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
72640 ** But not if we are in secure-delete mode. In secure-delete mode,
72645 if( pBt->btsFlags & BTS_FAST_SECURE ){
72650 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
72651 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
72653 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
72656 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
72660 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
72662 nMaxCells = nOld*(MX_CELL(pBt) + ArraySize(pParent->apOvfl));
72671 + pBt->pageSize; /* aSpace1 */
72673 assert( szScratch<=7*(int)pBt->pageSize );
72700 leafCorrection = b.pRef->leaf*4;
72701 leafData = b.pRef->intKeyLeaf;
72704 int limit = pOld->nCell;
72705 u8 *aData = pOld->aData;
72706 u16 maskPage = pOld->maskPage;
72707 u8 *piCell = aData + pOld->cellOffset;
72711 /* Verify that all sibling pages are of the same "type" (table-leaf,
72712 ** table-interior, index-leaf, or index-interior).
72714 if( pOld->aData[0]!=apOld[0]->aData[0] ){
72736 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
72737 if( pOld->nOverflow>0 ){
72738 if( NEVER(limit<pOld->aiOvfl[0]) ){
72742 limit = pOld->aiOvfl[0];
72748 for(k=0; k<pOld->nOverflow; k++){
72749 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
72750 b.apCell[b.nCell] = pOld->apOvfl[k];
72754 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
72761 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
72764 if( i<nOld-1 && !leafData){
72771 assert( sz<=pBt->maxLocal+23 );
72772 assert( iSpace1 <= (int)pBt->pageSize );
72776 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
72777 if( !pOld->leaf ){
72779 assert( pOld->hdrOffset==0 );
72782 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
72789 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
72801 ** size of all cells on the i-th page and cntNew[] which is the index
72808 ** szNew[i]: Spaced used on the i-th sibling page.
72810 ** the right of the i-th sibling page.
72814 usableSpace = pBt->usableSize - 12 + leafCorrection;
72817 b.apEnd[k] = p->aDataEnd;
72819 if( k && b.ixNx[k]==b.ixNx[k-1] ){
72820 k--; /* Omit b.ixNx[] entry for child pages with no cells */
72824 b.apEnd[k] = pParent->aDataEnd;
72827 assert( p->nFree>=0 );
72828 szNew[i] = usableSpace - p->nFree;
72829 for(j=0; j<p->nOverflow; j++){
72830 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
72841 szNew[k-1] = 0;
72842 cntNew[k-1] = b.nCell;
72844 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
72845 szNew[i] -= sz;
72854 cntNew[i]--;
72868 szNew[i+1] -= sz;
72872 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
72881 ** always nearly full, while the right-most sibling might be nearly empty.
72886 ** be so out of balance as to be illegal. For example, the right-most
72889 for(i=k-1; i>0; i--){
72891 int szLeft = szNew[i-1]; /* Size of sibling on the left */
72892 int r; /* Index of right-most cell in left sibling */
72895 r = cntNew[i-1] - 1;
72896 d = r + 1 - leafData;
72903 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
72907 szLeft -= b.szCell[r] + 2;
72908 cntNew[i-1] = r;
72909 r--;
72910 d--;
72913 szNew[i-1] = szLeft;
72914 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
72920 /* Sanity check: For a non-corrupt database file one of the follwing
72927 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
72929 apOld[0]->pgno, apOld[0]->nCell,
72930 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
72931 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
72937 pageFlags = apOld[0]->aData[0];
72943 rc = sqlite3PagerWrite(pNew->pDbPage);
72945 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv)) ){
72958 /* Set the pointer-map entry for the new sibling page. */
72960 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
72981 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
72982 aPgFlags[i] = apNew[i]->pDbPage->flags;
73006 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
73008 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
73009 apNew[i]->pgno = pgno;
73015 apNew[0]->pgno, szNew[0], cntNew[0],
73016 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
73017 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
73018 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
73019 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
73020 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
73021 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
73022 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
73023 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
73026 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73028 assert( apNew[nNew-1]!=0 );
73029 put4byte(pRight, apNew[nNew-1]->pgno);
73031 /* If the sibling pages are not leaves, ensure that the right-child pointer
73032 ** of the right-most new sibling page is set to the value that was
73033 ** originally in the same field of the right-most old sibling page. */
73035 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
73036 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
73051 ** associated with the right-child of each sibling may also need to be
73058 int cntOldNext = pNew->nCell + pNew->nOverflow;
73069 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
73083 || pNew->pgno!=aPgno[iOld]
73084 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
73087 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
73089 if( cachedCellSize(&b,i)>pNew->minLocal ){
73098 for(i=0; i<nNew-1; i++){
73110 if( !pNew->leaf ){
73111 memcpy(&pNew->aData[8], pCell, 4);
73113 /* If the tree is a leaf-data tree, and the siblings are leaves,
73115 ** cell consists of the integer key for the right-most cell of
73116 ** the sibling-page assembled above only.
73119 j--;
73120 pNew->xParseCell(pNew, b.apCell[j], &info);
73125 pCell -= 4;
73126 /* Obscure case for non-leaf-data trees: If the cell at pCell was
73133 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
73139 sz = pParent->xCellSize(pParent, pCell);
73143 assert( sz<=pBt->maxLocal+23 );
73144 assert( iOvflSpace <= (int)pBt->pageSize );
73145 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
73147 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
73154 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
73156 ** the left-hand sibling apNew[iPg-1] has been updated.
73160 ** the right-hand sibling apNew[iPg+1] has been updated.
73164 ** The iPg value in the following loop starts at nNew-1 goes down
73165 ** to 0, then back up to nNew-1 again, thus making two passes over
73172 for(i=1-nNew; i<nNew; i++){
73173 int iPg = i<0 ? -i : i;
73177 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
73184 ** only after iPg-1 has already been updated. */
73185 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
73195 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
73196 iNew = cntNew[iPg-1] + !leafData;
73197 nNewCell = cntNew[iPg] - iNew;
73203 apNew[iPg]->nFree = usableSpace-szNew[iPg];
73204 assert( apNew[iPg]->nOverflow==0 );
73205 assert( apNew[iPg]->nCell==nNewCell );
73215 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
73216 /* The root page of the b-tree now contains no cells. The only sibling
73217 ** page is the right-child of the parent. Copy the contents of the
73219 ** b-tree structure by one. This is described as the "balance-shallower"
73220 ** sub-algorithm in some documentation.
73222 ** If this is an auto-vacuum database, the call to copyNodeContent()
73223 ** sets all pointer-map entries corresponding to database image pages
73232 rc = defragmentPage(apNew[0], -1);
73234 assert( apNew[0]->nFree ==
73235 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
73236 - apNew[0]->nCell*2)
73242 /* Fix the pointer map entries associated with the right-child of each
73246 u32 key = get4byte(&apNew[i]->aData[8]);
73247 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
73251 assert( pParent->isInit );
73262 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
73289 ** This function is called when the root page of a b-tree structure is
73294 ** page is then overwritten to make it an empty page with the right-child
73297 ** Before returning, all pointer-map entries corresponding to pages
73298 ** that the new child-page now contains pointers to are updated. The
73299 ** entry corresponding to the new right-child pointer of the root
73311 BtShared *pBt = pRoot->pBt; /* The BTree */
73313 assert( pRoot->nOverflow>0 );
73314 assert( sqlite3_mutex_held(pBt->mutex) );
73316 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
73317 ** page that will become the new right-child of pPage. Copy the contents
73320 rc = sqlite3PagerWrite(pRoot->pDbPage);
73322 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
73325 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
73333 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
73334 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
73335 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
73337 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
73340 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
73341 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
73342 memcpy(pChild->apOvfl, pRoot->apOvfl,
73343 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
73344 pChild->nOverflow = pRoot->nOverflow;
73346 /* Zero the contents of pRoot. Then install pChild as the right-child. */
73347 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
73348 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
73356 ** on the same B-tree as pCur.
73359 ** pointing to the same b-tree. If an insert occurs on one SQL table
73361 ** table linked to the same b-tree. If the secondary insert causes a
73367 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
73369 && pOther->eState==CURSOR_VALID
73370 && pOther->pPage==pCur->pPage
73390 const int nMin = pCur->pBt->usableSize * 2 / 3;
73399 MemPage *pPage = pCur->pPage;
73401 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
73402 if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
73404 }else if( (iPage = pCur->iPage)==0 ){
73405 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
73406 /* The root page of the b-tree is overfull. In this case call the
73407 ** balance_deeper() function to create a new child for the root-page
73408 ** and copy the current contents of the root-page to it. The
73409 ** next iteration of the do-loop will balance the child page.
73413 rc = balance_deeper(pPage, &pCur->apPage[1]);
73415 pCur->iPage = 1;
73416 pCur->ix = 0;
73417 pCur->aiIdx[0] = 0;
73418 pCur->apPage[0] = pPage;
73419 pCur->pPage = pCur->apPage[1];
73420 assert( pCur->pPage->nOverflow );
73426 MemPage * const pParent = pCur->apPage[iPage-1];
73427 int const iIdx = pCur->aiIdx[iPage-1];
73429 rc = sqlite3PagerWrite(pParent->pDbPage);
73430 if( rc==SQLITE_OK && pParent->nFree<0 ){
73435 if( pPage->intKeyLeaf
73436 && pPage->nOverflow==1
73437 && pPage->aiOvfl[0]==pPage->nCell
73438 && pParent->pgno!=1
73439 && pParent->nCell==iIdx
73444 ** happens, the next iteration of the do-loop will balance pParent
73463 ** become overfull or underfull. The next iteration of the do-loop
73468 ** A subsequent iteration of the do-loop will deal with this by
73470 ** but it doesn't deal with overflow cells - just moves them to a
73477 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
73479 pCur->hints&BTREE_BULKLOAD);
73495 pPage->nOverflow = 0;
73497 /* The next iteration of the do-loop balances the parent page. */
73499 pCur->iPage--;
73500 assert( pCur->iPage>=0 );
73501 pCur->pPage = pCur->apPage[pCur->iPage];
73521 int nData = pX->nData - iOffset;
73527 int rc = sqlite3PagerWrite(pPage->pDbPage);
73529 memset(pDest + i, 0, iAmt - i);
73536 iAmt-nData);
73540 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
73541 int rc = sqlite3PagerWrite(pPage->pDbPage);
73547 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
73558 int iOffset; /* Next byte of pX->pData to write */
73559 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
73561 MemPage *pPage = pCur->pPage; /* Page being written */
73566 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
73567 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
73572 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
73573 0, pCur->info.nLocal);
73575 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
73578 iOffset = pCur->info.nLocal;
73581 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
73582 pBt = pPage->pBt;
73583 ovflPageSize = pBt->usableSize - 4;
73587 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
73591 ovflPgno = get4byte(pPage->aData);
73593 ovflPageSize = nTotal - iOffset;
73595 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
73598 sqlite3PagerUnref(pPage->pDbPage);
73621 ** If the seekResult parameter is non-zero, then a successful call to
73632 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
73633 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
73643 int loc = seekResult; /* -1: before desired location +1: after */
73647 Btree *p = pCur->pBtree;
73648 BtShared *pBt = p->pBt;
73653 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
73655 if( pCur->eState==CURSOR_FAULT ){
73656 assert( pCur->skipNext!=SQLITE_OK );
73657 return pCur->skipNext;
73661 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
73662 && pBt->inTransaction==TRANS_WRITE
73663 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
73664 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
73667 ** expecting an index b-tree, then the caller should be inserting blob
73671 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
73675 ** In some cases, the call to btreeMoveto() below is a no-op. For
73676 ** example, when inserting data into a table with auto-generated integer
73679 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
73684 if( pCur->curFlags & BTCF_Multiple ){
73685 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
73689 if( pCur->pKeyInfo==0 ){
73690 assert( pX->pKey==0 );
73691 /* If this is an insert into a table b-tree, invalidate any incrblob
73693 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
73700 assert( pCur->curFlags & BTCF_ValidNKey );
73701 assert( pX->nKey==pCur->info.nKey );
73710 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
73713 assert( pX->nData>=0 && pX->nZero>=0 );
73714 if( pCur->info.nSize!=0
73715 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
73726 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
73743 if( pX->nMem ){
73745 r.pKeyInfo = pCur->pKeyInfo;
73746 r.aMem = pX->aMem;
73747 r.nField = pX->nMem;
73755 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
73766 if( pCur->info.nKey==pX->nKey ){
73768 x2.pData = pX->pKey;
73769 x2.nData = pX->nKey;
73776 assert( pCur->eState==CURSOR_VALID
73777 || (pCur->eState==CURSOR_INVALID && loc)
73780 pPage = pCur->pPage;
73781 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
73782 assert( pPage->leaf || !pPage->intKey );
73783 if( pPage->nFree<0 ){
73784 if( pCur->eState>CURSOR_INVALID ){
73793 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
73795 assert( pPage->isInit );
73796 newCell = pBt->pTmpSpace;
73800 szNew = pBt->nPreformatSize;
73802 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){
73804 pPage->xParseCell(pPage, newCell, &info);
73806 Pgno ovfl = get4byte(&newCell[szNew-4]);
73807 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
73814 assert( szNew==pPage->xCellSize(pPage, newCell) );
73816 idx = pCur->ix;
73819 assert( idx<pPage->nCell );
73820 rc = sqlite3PagerWrite(pPage->pDbPage);
73825 if( !pPage->leaf ){
73829 testcase( pCur->curFlags & BTCF_ValidOvfl );
73832 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
73842 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
73844 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
73847 if( oldCell+szNew > pPage->aDataEnd ){
73855 }else if( loc<0 && pPage->nCell>0 ){
73856 assert( pPage->leaf );
73857 idx = ++pCur->ix;
73858 pCur->curFlags &= ~BTCF_ValidNKey;
73860 assert( pPage->leaf );
73863 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
73864 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
73878 ** multiple records into an intkey b-tree using a single cursor (as can
73881 ** the b-tree if possible. If the cursor is left pointing to the last
73886 pCur->info.nSize = 0;
73887 if( pPage->nOverflow ){
73889 pCur->curFlags &= ~(BTCF_ValidNKey);
73896 pCur->pPage->nOverflow = 0;
73897 pCur->eState = CURSOR_INVALID;
73900 if( pCur->pKeyInfo ){
73901 assert( pCur->pKey==0 );
73902 pCur->pKey = sqlite3Malloc( pX->nKey );
73903 if( pCur->pKey==0 ){
73906 memcpy(pCur->pKey, pX->pKey, pX->nKey);
73909 pCur->eState = CURSOR_REQUIRESEEK;
73910 pCur->nKey = pX->nKey;
73913 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
73936 BtShared *pBt = pDest->pBt;
73937 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
73943 aOut += putVarint32(aOut, pSrc->info.nPayload);
73944 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
73945 nIn = pSrc->info.nLocal;
73946 aIn = pSrc->info.pPayload;
73947 if( aIn+nIn>pSrc->pPage->aDataEnd ){
73950 nRem = pSrc->info.nPayload;
73951 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
73953 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
73955 Pager *pSrcPager = pSrc->pBt->pPager;
73962 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
73963 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
73964 if( nOut<pSrc->info.nPayload ){
73966 pBt->nPreformatSize += 4;
73970 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
73973 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
73977 nRem -= nOut;
73983 nOut -= nCopy;
73984 nIn -= nCopy;
73996 nIn = pSrc->pBt->usableSize - 4;
74007 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
74012 pPgnoOut = pPageOut->aData;
74015 nOut = MIN(pBt->usableSize - 4, nRem);
74040 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
74045 Btree *p = pCur->pBtree;
74046 BtShared *pBt = p->pBt;
74057 assert( pBt->inTransaction==TRANS_WRITE );
74058 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74059 assert( pCur->curFlags & BTCF_WriteFlag );
74060 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
74061 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
74063 if( pCur->eState==CURSOR_REQUIRESEEK ){
74067 assert( pCur->eState==CURSOR_VALID );
74069 iCellDepth = pCur->iPage;
74070 iCellIdx = pCur->ix;
74071 pPage = pCur->pPage;
74073 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT;
74077 ** will cause a b-tree rebalance, then this is done by saving the cursor
74085 if( !pPage->leaf
74086 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
74087 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
74089 /* A b-tree rebalance will be required after deleting this entry.
74103 ** sub-tree headed by the child page of the cell being deleted. This makes
74105 if( !pPage->leaf ){
74113 if( pCur->curFlags & BTCF_Multiple ){
74114 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
74118 /* If this is a delete operation to remove a row from a table b-tree,
74120 if( pCur->pKeyInfo==0 ){
74121 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
74127 rc = sqlite3PagerWrite(pPage->pDbPage);
74134 ** is currently pointing to the largest entry in the sub-tree headed
74135 ** by the child-page of the cell that was just deleted from an internal
74138 if( !pPage->leaf ){
74139 MemPage *pLeaf = pCur->pPage;
74144 if( pLeaf->nFree<0 ){
74148 if( iCellDepth<pCur->iPage-1 ){
74149 n = pCur->apPage[iCellDepth+1]->pgno;
74151 n = pCur->pPage->pgno;
74153 pCell = findCell(pLeaf, pLeaf->nCell-1);
74154 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
74155 nCell = pLeaf->xCellSize(pLeaf, pCell);
74157 pTmp = pBt->pTmpSpace;
74159 rc = sqlite3PagerWrite(pLeaf->pDbPage);
74161 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
74163 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
74183 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
74184 releasePageNotNull(pCur->pPage);
74185 pCur->iPage--;
74186 while( pCur->iPage>iCellDepth ){
74187 releasePage(pCur->apPage[pCur->iPage--]);
74189 pCur->pPage = pCur->apPage[pCur->iPage];
74195 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
74196 assert( pPage==pCur->pPage || CORRUPT_DB );
74197 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
74198 pCur->eState = CURSOR_SKIPNEXT;
74199 if( iCellIdx>=pPage->nCell ){
74200 pCur->skipNext = -1;
74201 pCur->ix = pPage->nCell-1;
74203 pCur->skipNext = 1;
74209 pCur->eState = CURSOR_REQUIRESEEK;
74229 BtShared *pBt = p->pBt;
74233 int ptfFlags; /* Page-type flage for the root page of new table */
74236 assert( pBt->inTransaction==TRANS_WRITE );
74237 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
74245 if( pBt->autoVacuum ){
74246 Pgno pgnoMove; /* Move a page here to make room for the root-page */
74251 ** out to be an overflow page, delete all overflow page-map caches
74257 ** root page of the new table should go. meta[3] is the largest root-page
74258 ** created so far, so the new root-page is (meta[3]+1).
74266 /* The new root-page may not be allocated on a pointer-map page, or the
74285 /* pgnoRoot is the page that will be used for the root-page of
74329 rc = sqlite3PagerWrite(pRoot->pDbPage);
74338 /* Update the pointer-map and meta-data with the new root-page number. */
74349 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
74361 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
74368 sqlite3PagerUnref(pRoot->pDbPage);
74369 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
74398 assert( sqlite3_mutex_held(pBt->mutex) );
74404 if( pPage->bBusy ){
74408 pPage->bBusy = 1;
74409 hdr = pPage->hdrOffset;
74410 for(i=0; i<pPage->nCell; i++){
74412 if( !pPage->leaf ){
74419 if( !pPage->leaf ){
74420 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
74423 assert( pPage->intKey || CORRUPT_DB );
74424 testcase( !pPage->intKey );
74425 *pnChange += pPage->nCell;
74429 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
74430 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
74434 pPage->bBusy = 0;
74454 BtShared *pBt = p->pBt;
74456 assert( p->inTrans==TRANS_WRITE );
74462 ** is the root of a table b-tree - if it is not, the following call is
74463 ** a no-op). */
74477 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
74503 BtShared *pBt = p->pBt;
74506 assert( p->inTrans==TRANS_WRITE );
74526 if( pBt->autoVacuum ){
74531 /* If the table being dropped is the table with the largest root-page
74540 /* The table being dropped does not have the largest root-page
74542 ** gap left by the deleted root-page.
74565 /* Set the new 'max-root-page' value in the database header. This
74567 ** be a root-page number, less one again if that is the
74570 maxRootPgno--;
74573 maxRootPgno--;
74595 ** This function may only be called if the b-tree connection already
74598 ** Read the meta-information out of a database file. Meta[0]
74601 ** is read-only, the others are read/write.
74615 BtShared *pBt = p->pBt;
74618 assert( p->inTrans>TRANS_NONE );
74620 assert( pBt->pPage1 );
74624 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
74626 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
74629 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
74630 ** database, mark the database as read-only. */
74633 pBt->btsFlags |= BTS_READ_ONLY;
74641 ** Write meta-information back into the database. Meta[0] is
74642 ** read-only and may not be written.
74645 BtShared *pBt = p->pBt;
74650 assert( p->inTrans==TRANS_WRITE );
74651 assert( pBt->pPage1!=0 );
74652 pP1 = pBt->pPage1->aData;
74653 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
74658 assert( pBt->autoVacuum || iMeta==0 );
74660 pBt->incrVacuum = (u8)iMeta;
74669 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
74670 ** number of entries in the b-tree and write the result to *pnEntry.
74687 ** page in the B-Tree structure (not including overflow pages).
74689 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
74691 MemPage *pPage; /* Current page of the b-tree */
74693 /* If this is a leaf page or the tree is not an int-key tree, then
74697 pPage = pCur->pPage;
74698 if( pPage->leaf || !pPage->intKey ){
74699 nEntry += pPage->nCell;
74705 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
74707 ** to visit is the right-child of its parent.
74712 if( pPage->leaf ){
74714 if( pCur->iPage==0 ){
74715 /* All pages of the b-tree have been visited. Return successfully. */
74720 }while ( pCur->ix>=pCur->pPage->nCell );
74722 pCur->ix++;
74723 pPage = pCur->pPage;
74727 ** points at. This is the right-child if (iIdx==pPage->nCell).
74729 iIdx = pCur->ix;
74730 if( iIdx==pPage->nCell ){
74731 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
74746 return p->pBt->pPager;
74751 ** Append a message to the error message string.
74759 if( !pCheck->mxErr ) return;
74760 pCheck->mxErr--;
74761 pCheck->nErr++;
74763 if( pCheck->errMsg.nChar ){
74764 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
74766 if( pCheck->zPfx ){
74767 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
74769 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
74771 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
74772 pCheck->bOomFault = 1;
74780 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
74784 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
74785 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
74792 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
74793 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
74799 ** reference to the page, add an error message to pCheck->zErrMsg.
74806 if( iPage>pCheck->nPage || iPage==0 ){
74814 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1;
74821 ** Check that the entry in the pointer-map for page iChild maps to
74822 ** page iParent, pointer type ptrType. If not, append an error message
74835 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
74837 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1;
74862 int nErrAtStart = pCheck->nErr;
74863 while( iPage!=0 && pCheck->mxErr ){
74867 N--;
74868 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
74876 if( pCheck->pBt->autoVacuum ){
74880 if( n>pCheck->pBt->usableSize/4-2 ){
74883 N--;
74888 if( pCheck->pBt->autoVacuum ){
74894 N -= n;
74899 /* If this database supports auto-vacuum and iPage is not the last
74900 ** page in this overflow list, check that the pointer-map entry for
74903 if( pCheck->pBt->autoVacuum && N>0 ){
74912 if( N && nErrAtStart==pCheck->nErr ){
74916 expected-N, expected);
74922 ** An implementation of a min-heap.
74932 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
74959 aHeap[0]--;
74996 int depth = -1, d2; /* Depth of a subtree */
75012 u32 *heap = 0; /* Min-heap used for checking cell coverage */
75013 u32 x, prev = 0; /* Next and previous entry on the min-heap */
75014 const char *saved_zPfx = pCheck->zPfx;
75015 int saved_v1 = pCheck->v1;
75016 int saved_v2 = pCheck->v2;
75021 pBt = pCheck->pBt;
75022 usableSize = pBt->usableSize;
75025 pCheck->zPfx = "Page %u: ";
75026 pCheck->v1 = iPage;
75035 savedIsInit = pPage->isInit;
75036 pPage->isInit = 0;
75048 data = pPage->aData;
75049 hdr = pPage->hdrOffset;
75052 pCheck->zPfx = "On tree page %u cell %d: ";
75056 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
75059 assert( pPage->nCell==nCell );
75061 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
75062 ** immediately follows the b-tree page header. */
75063 cellStart = hdr + 12 - 4*pPage->leaf;
75064 assert( pPage->aCellIdx==&data[cellStart] );
75065 pCellIdx = &data[cellStart + 2*(nCell-1)];
75067 if( !pPage->leaf ){
75068 /* Analyze the right-child page of internal pages */
75071 if( pBt->autoVacuum ){
75072 pCheck->zPfx = "On page %u at right child: ";
75081 heap = pCheck->heap;
75085 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
75087 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
75091 pCheck->v2 = i;
75094 pCellIdx -= 2;
75095 if( pc<contentOffset || pc>usableSize-4 ){
75097 pc, contentOffset, usableSize-4);
75102 pPage->xParseCell(pPage, pCell, &info);
75110 if( pPage->intKey ){
75122 assert( pc + info.nSize - 4 <= usableSize );
75123 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
75124 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
75126 if( pBt->autoVacuum ){
75133 if( !pPage->leaf ){
75137 if( pBt->autoVacuum ){
75148 /* Populate the coverage-checking heap for leaf pages */
75149 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
75156 pCheck->zPfx = 0;
75157 if( doCoverageCheck && pCheck->mxErr>0 ){
75158 /* For leaf pages, the min-heap has already been initialized and the
75161 if( !pPage->leaf ){
75162 heap = pCheck->heap;
75164 for(i=nCell-1; i>=0; i--){
75167 size = pPage->xCellSize(pPage, &data[pc]);
75168 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
75171 /* Add the freeblocks to the min-heap
75173 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
75180 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
75183 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
75184 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
75185 ** big-endian integer which is the offset in the b-tree page of the next
75189 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
75192 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
75195 /* Analyze the min-heap looking for overlap between cells and/or
75198 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
75203 ** The loop below pulls entries from the min-heap in order and compares
75209 prev = contentOffset - 1; /* Implied first min-heap entry */
75216 nFrag += (x>>16) - (prev&0xffff) - 1;
75220 nFrag += usableSize - (prev&0xffff) - 1;
75221 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
75222 ** is stored in the fifth field of the b-tree page header.
75223 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
75234 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
75236 pCheck->zPfx = saved_zPfx;
75237 pCheck->v1 = saved_v1;
75238 pCheck->v2 = saved_v2;
75249 ** A read-only or read-write transaction must be opened before calling
75253 ** allocation errors, an error message held in memory obtained from
75254 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
75258 ** root pages is incomplete. This is a "partial integrity-check". This
75276 BtShared *pBt = p->pBt;
75277 u64 savedDbFlags = pBt->db->flags;
75292 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
75293 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
75297 sCheck.pPager = pBt->pPager;
75318 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
75331 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
75332 get4byte(&pBt->pPage1->aData[36]));
75340 if( pBt->autoVacuum ){
75344 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
75351 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
75358 testcase( pBt->db->flags & SQLITE_CellSizeCk );
75359 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
75364 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
75370 pBt->db->flags = savedDbFlags;
75381 /* If the database supports auto-vacuum, make sure no tables contain
75382 ** references to pointer-map pages.
75385 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
75389 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
75408 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
75416 ** an empty string if the database is in-memory or a TEMP database.
75422 assert( p->pBt->pPager!=0 );
75423 return sqlite3PagerFilename(p->pBt->pPager, 1);
75435 assert( p->pBt->pPager!=0 );
75436 return sqlite3PagerJournalname(p->pBt->pPager);
75444 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
75445 return p ? p->inTrans : 0;
75453 ** transaction on the shared-cache the argument Btree is connected to.
75460 BtShared *pBt = p->pBt;
75462 if( pBt->inTransaction!=TRANS_NONE ){
75465 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
75478 assert( sqlite3_mutex_held(p->db->mutex) );
75479 return p->nBackup!=0;
75484 ** a single shared-btree. The memory is used by client code for its own
75485 ** purposes (for example, to store a high-level schema associated with
75486 ** the shared-btree). The btree layer manages reference counting issues.
75488 ** The first time this is called on a shared-btree, nBytes bytes of memory
75497 ** Just before the shared-btree is closed, the function passed as the
75503 BtShared *pBt = p->pBt;
75505 if( !pBt->pSchema && nBytes ){
75506 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
75507 pBt->xFreeSchema = xFree;
75510 return pBt->pSchema;
75520 assert( sqlite3_mutex_held(p->db->mutex) );
75537 assert( p->inTrans!=TRANS_NONE );
75538 if( p->sharable ){
75568 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
75569 assert( pCsr->curFlags & BTCF_Incrblob );
75575 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
75576 if( pCsr->eState!=CURSOR_VALID ){
75582 ** version of the b-tree page modified by the accessPayload call below.
75588 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
75594 ** (c) the connection holds a write-lock on the table (if required),
75595 ** (d) there are no conflicting read-locks, and
75598 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
75601 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
75602 && pCsr->pBt->inTransaction==TRANS_WRITE );
75603 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
75604 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
75605 assert( pCsr->pPage->intKey );
75614 pCur->curFlags |= BTCF_Incrblob;
75615 pCur->pBtree->hasIncrblobCur = 1;
75625 BtShared *pBt = pBtree->pBt;
75633 pBt->btsFlags &= ~BTS_NO_WAL;
75634 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
75638 u8 *aData = pBt->pPage1->aData;
75642 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
75651 pBt->btsFlags &= ~BTS_NO_WAL;
75660 return (pCsr->hints & mask)!=0;
75664 ** Return true if the given Btree is read-only.
75667 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
75680 return p->sharable;
75689 testcase( p->sharable );
75690 return p->pBt->nRef;
75718 Btree *pDest; /* Destination b-tree file */
75720 int bDestLocked; /* True once a write-transaction is open on pDest */
75724 Btree *pSrc; /* Source b-tree file */
75742 ** structure may be accessed via two groups of thread-safe entry points:
75756 ** backup_pagecount() are not thread-safe functions. If they are called
75764 ** Non-sharable Btrees (in-memory databases for example), do not have
75771 ** a NULL pointer and write an error message to pErrorDb.
75775 ** error message to pErrorDb.
75801 return pDb->aDb[i].pBt;
75810 rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),0,0);
75815 ** Check that there is no open read-transaction on the b-tree passed as the
75817 ** is an open read-transaction, return SQLITE_ERROR and leave an error
75818 ** message in database handle db.
75833 ** If an error occurs, NULL is returned and an error code and error message
75859 sqlite3_mutex_enter(pSrcDb->mutex);
75860 sqlite3_mutex_enter(pDestDb->mutex);
75869 ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
75880 p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
75881 p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
75882 p->pDestDb = pDestDb;
75883 p->pSrcDb = pSrcDb;
75884 p->iNext = 1;
75885 p->isAttached = 0;
75887 if( 0==p->pSrc || 0==p->pDest
75888 || checkReadTransaction(pDestDb, p->pDest)!=SQLITE_OK
75900 p->pSrc->nBackup++;
75903 sqlite3_mutex_leave(pDestDb->mutex);
75904 sqlite3_mutex_leave(pSrcDb->mutex);
75928 Pager * const pDestPager = sqlite3BtreePager(p->pDest);
75929 const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
75930 int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
75936 assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
75937 assert( p->bDestLocked );
75938 assert( !isFatalError(p->rc) );
75939 assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
75942 /* Catch the case where the destination is an in-memory database and the
75953 for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
75956 if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
75974 sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
75986 ** this function is a no-op.
76006 assert( sqlite3BtreeHoldsMutex(p->pSrc) );
76007 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
76008 p->pNext = *pp;
76010 p->isAttached = 1;
76014 ** Copy nPage pages from the source b-tree to the destination.
76025 sqlite3_mutex_enter(p->pSrcDb->mutex);
76026 sqlite3BtreeEnter(p->pSrc);
76027 if( p->pDestDb ){
76028 sqlite3_mutex_enter(p->pDestDb->mutex);
76031 rc = p->rc;
76033 Pager * const pSrcPager = sqlite3BtreePager(p->pSrc); /* Source pager */
76034 Pager * const pDestPager = sqlite3BtreePager(p->pDest); /* Dest pager */
76036 int nSrcPage = -1; /* Size of source db in pages */
76039 /* If the source pager is currently in a write-transaction, return
76042 if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
76048 /* If there is no open read-transaction on the source database, open
76052 if( rc==SQLITE_OK && SQLITE_TXN_NONE==sqlite3BtreeTxnState(p->pSrc) ){
76053 rc = sqlite3BtreeBeginTrans(p->pSrc, 0, 0);
76063 if( p->bDestLocked==0 && rc==SQLITE_OK && setDestPgsz(p)==SQLITE_NOMEM ){
76068 if( SQLITE_OK==rc && p->bDestLocked==0
76069 && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2,
76070 (int*)&p->iDestSchema))
76072 p->bDestLocked = 1;
76077 pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
76078 pgszDest = sqlite3BtreeGetPageSize(p->pDest);
76079 destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
76084 /* Now that there is a read-lock on the source database, query the
76087 nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
76089 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
76090 const Pgno iSrcPg = p->iNext; /* Source page number */
76091 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
76099 p->iNext++;
76102 p->nPagecount = nSrcPage;
76103 p->nRemaining = nSrcPage+1-p->iNext;
76104 if( p->iNext>(Pgno)nSrcPage ){
76106 }else if( !p->isAttached ){
76112 ** is to make sure that the schema-version really does change in
76118 rc = sqlite3BtreeNewDb(p->pDest);
76122 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
76125 if( p->pDestDb ){
76126 sqlite3ResetAllSchemasOfConnection(p->pDestDb);
76129 rc = sqlite3BtreeSetVersion(p->pDest, 2);
76146 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
76147 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
76150 nDestTruncate = (nSrcPage+ratio-1)/ratio;
76151 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
76152 nDestTruncate--;
76160 /* If the source page-size is smaller than the destination page-size,
76166 ** pending-byte page in the source database may need to be
76179 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
76191 if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
76235 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
76245 ** "committing" a read-only transaction cannot fail.
76249 TESTONLY( rc2 = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
76250 TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
76257 p->rc = rc;
76259 if( p->pDestDb ){
76260 sqlite3_mutex_leave(p->pDestDb->mutex);
76262 sqlite3BtreeLeave(p->pSrc);
76263 sqlite3_mutex_leave(p->pSrcDb->mutex);
76277 pSrcDb = p->pSrcDb;
76278 sqlite3_mutex_enter(pSrcDb->mutex);
76279 sqlite3BtreeEnter(p->pSrc);
76280 if( p->pDestDb ){
76281 sqlite3_mutex_enter(p->pDestDb->mutex);
76285 if( p->pDestDb ){
76286 p->pSrc->nBackup--;
76288 if( p->isAttached ){
76289 pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
76292 pp = &(*pp)->pNext;
76295 *pp = p->pNext;
76299 sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
76302 rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
76303 if( p->pDestDb ){
76304 sqlite3Error(p->pDestDb, rc);
76307 sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
76309 sqlite3BtreeLeave(p->pSrc);
76310 if( p->pDestDb ){
76311 /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
76331 return p->nRemaining;
76345 return p->nPagecount;
76367 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
76368 if( !isFatalError(p->rc) && iPage<p->iNext ){
76374 assert( p->pDestDb );
76375 sqlite3_mutex_enter(p->pDestDb->mutex);
76377 sqlite3_mutex_leave(p->pDestDb->mutex);
76380 p->rc = rc;
76383 }while( (p = p->pNext)!=0 );
76402 for(p=pBackup; p; p=p->pNext){
76403 assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
76404 p->iNext = 1;
76426 if( pFd->pMethods ){
76439 b.pSrcDb = pFrom->db;
76448 ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
76455 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
76493 #define ISPOWEROF2(X) (((X)&((X)-1))==0)
76506 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
76512 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
76515 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
76517 if( p->flags & MEM_Null ){
76519 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
76526 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){
76529 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
76530 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
76531 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );
76534 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind
76542 assert( (p->flags & MEM_Cleared)==0 );
76546 assert( p->szMalloc==0
76547 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
76557 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
76559 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
76560 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
76561 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
76562 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
76575 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
76577 if( p->flags & MEM_Int ){
76579 /* Work-around for GCC bug
76582 assert( (p->flags&MEM_Int)*2==sizeof(x) );
76583 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);
76586 sqlite3Int64ToText(p->u.i, zBuf);
76591 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);
76610 ** https://www.sqlite.org/src/info/343634942dd54ab (2018-01-31) for
76624 if( (p->flags & MEM_Str)==0 ) return 1;
76625 if( p->flags & MEM_Term ){
76626 /* Insure that the string is properly zero-terminated. Pay particular
76627 ** attention to the case where p->n is odd */
76628 if( p->szMalloc>0 && p->z==p->zMalloc ){
76629 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );
76630 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );
76632 assert( p->z[p->n]==0 );
76633 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );
76634 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );
76636 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
76638 z = p->z;
76641 if( p->enc!=SQLITE_UTF8 ){
76643 if( p->enc==SQLITE_UTF16BE ) z++;
76660 ** routine is a no-op.
76673 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
76676 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76686 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
76687 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
76693 ** Make sure pMem->z points to a writable allocation of at least n bytes.
76696 ** pMem->z into the new allocation. pMem must be either a string or
76698 ** in pMem->z is discarded.
76703 testcase( pMem->db==0 );
76707 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
76708 testcase( bPreserve && pMem->z==0 );
76710 assert( pMem->szMalloc==0
76711 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
76712 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){
76713 if( pMem->db ){
76714 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
76716 pMem->zMalloc = sqlite3Realloc(pMem->z, n);
76717 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);
76718 pMem->z = pMem->zMalloc;
76722 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
76723 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
76725 if( pMem->zMalloc==0 ){
76727 pMem->z = 0;
76728 pMem->szMalloc = 0;
76731 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
76734 if( bPreserve && pMem->z ){
76735 assert( pMem->z!=pMem->zMalloc );
76736 memcpy(pMem->zMalloc, pMem->z, pMem->n);
76738 if( (pMem->flags&MEM_Dyn)!=0 ){
76739 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
76740 pMem->xDel((void *)(pMem->z));
76743 pMem->z = pMem->zMalloc;
76744 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
76749 ** Change the pMem->zMalloc allocation to be at least szNew bytes.
76750 ** If pMem->zMalloc already meets or exceeds the requested size, this
76751 ** routine is a no-op.
76754 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str
76763 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
76764 if( pMem->szMalloc<szNew ){
76767 assert( (pMem->flags & MEM_Dyn)==0 );
76768 pMem->z = pMem->zMalloc;
76769 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
76778 ** to be a double-zero byte at an even byte boundary in order to
76783 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
76786 pMem->z[pMem->n] = 0;
76787 pMem->z[pMem->n+1] = 0;
76788 pMem->z[pMem->n+2] = 0;
76789 pMem->flags |= MEM_Term;
76800 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76802 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
76804 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
76809 pMem->flags &= ~MEM_Ephem;
76811 pMem->pScopyFrom = 0;
76818 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
76824 assert( pMem->flags & MEM_Zero );
76825 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );
76828 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76831 nByte = pMem->n + pMem->u.nZero;
76833 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;
76840 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
76841 pMem->n += pMem->u.nZero;
76842 pMem->flags &= ~(MEM_Zero|MEM_Term);
76851 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76852 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
76853 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
76854 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
76878 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76879 assert( !(pMem->flags&MEM_Zero) );
76880 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
76881 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
76887 pMem->enc = 0;
76891 vdbeMemRenderNum(nByte, pMem->z, pMem);
76892 assert( pMem->z!=0 );
76893 pMem->n = sqlite3Strlen30NN(pMem->z);
76894 pMem->enc = SQLITE_UTF8;
76895 pMem->flags |= MEM_Str|MEM_Term;
76896 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
76913 assert( pFunc->xFinalize!=0 );
76914 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
76915 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
76919 t.db = pMem->db;
76923 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
76924 assert( (pMem->flags & MEM_Dyn)==0 );
76925 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
76942 assert( pFunc->xValue!=0 );
76943 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );
76944 assert( pAccum->db==0 || sqlite3_mutex_held(pAccum->db->mutex) );
76950 pFunc->xValue(&ctx);
76965 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
76967 if( p->flags&MEM_Agg ){
76968 sqlite3VdbeMemFinalize(p, p->u.pDef);
76969 assert( (p->flags & MEM_Agg)==0 );
76970 testcase( p->flags & MEM_Dyn );
76972 if( p->flags&MEM_Dyn ){
76973 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
76974 p->xDel((void *)p->z);
76976 p->flags = MEM_Null;
76981 ** by p->xDel and memory in p->zMalloc.
76991 if( p->szMalloc ){
76992 sqlite3DbFreeNN(p->db, p->zMalloc);
76993 p->szMalloc = 0;
76995 p->z = 0;
77010 if( VdbeMemDynamic(p) || p->szMalloc ){
77016 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
77017 ** If the double is out of range of a 64-bit signed integer then
77018 ** return the closest available 64-bit signed integer.
77022 /* When floating-point is omitted, double and int64 are the same thing */
77027 ** minimum and maximum 64-bit integers, or they define them
77030 ** larger than a 32-bit integer constant.
77049 ** a floating-point then the value returned is the integer part.
77052 ** an SQL-NULL value, return 0.
77058 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
77063 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77065 flags = pMem->flags;
77068 return pMem->u.i;
77070 return doubleToInt64(pMem->u.r);
77071 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){
77087 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
77091 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77093 if( pMem->flags & MEM_Real ){
77094 return pMem->u.r;
77095 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
77096 testcase( pMem->flags & MEM_IntReal );
77097 return (double)pMem->u.i;
77098 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
77111 testcase( pMem->flags & MEM_IntReal );
77112 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
77113 if( pMem->flags & MEM_Null ) return ifNull;
77123 assert( pMem->flags & MEM_Real );
77125 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77128 ix = doubleToInt64(pMem->u.r);
77132 ** (1) the round-trip conversion real->int->real is a no-op, and
77140 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
77141 pMem->u.i = ix;
77150 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77154 pMem->u.i = sqlite3VdbeIntValue(pMem);
77164 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77167 pMem->u.r = sqlite3VdbeRealValue(pMem);
77177 ** For some versions of GCC on 32-bit machines, if you do the more obvious
77179 ** though the r1 and (double)i values are bit-for-bit the same.
77185 && i >= -2251799813685248LL && i < 2251799813685248LL);
77197 testcase( pMem->flags & MEM_Int );
77198 testcase( pMem->flags & MEM_Real );
77199 testcase( pMem->flags & MEM_IntReal );
77200 testcase( pMem->flags & MEM_Null );
77201 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
77204 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
77205 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77206 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
77207 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)
77208 || sqlite3RealSameAsInt(pMem->u.r, (ix = (i64)pMem->u.r))
77210 pMem->u.i = ix;
77216 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
77217 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
77229 if( pMem->flags & MEM_Null ) return SQLITE_OK;
77232 if( (pMem->flags & MEM_Blob)==0 ){
77234 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
77235 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
77237 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
77256 pMem->flags |= (pMem->flags&MEM_Blob)>>3;
77258 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
77259 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
77273 pMem->flags = flags;
77274 pMem->db = db;
77275 pMem->szMalloc = 0;
77295 pMem->flags = MEM_Null;
77308 pMem->flags = MEM_Blob|MEM_Zero;
77309 pMem->n = 0;
77311 pMem->u.nZero = n;
77312 pMem->enc = SQLITE_UTF8;
77313 pMem->z = 0;
77319 ** a 64-bit integer.
77323 pMem->u.i = val;
77324 pMem->flags = MEM_Int;
77335 pMem->u.i = val;
77336 pMem->flags = MEM_Int;
77340 /* A no-op destructor */
77353 assert( pMem->flags==MEM_Null );
77354 pMem->u.zPType = zPType ? zPType : "";
77355 pMem->z = pPtr;
77356 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;
77357 pMem->eSubtype = 'p';
77358 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;
77369 pMem->u.r = val;
77370 pMem->flags = MEM_Real;
77381 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)
77382 && pMem->xDel==sqlite3RowSetDelete;
77394 sqlite3 *db = pMem->db;
77401 pMem->z = (char*)p;
77402 pMem->flags = MEM_Blob|MEM_Dyn;
77403 pMem->xDel = sqlite3RowSetDelete;
77409 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
77412 assert( p->db!=0 );
77413 if( p->flags & (MEM_Str|MEM_Blob) ){
77414 int n = p->n;
77415 if( p->flags & MEM_Zero ){
77416 n += p->u.nZero;
77418 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
77429 ** This is used for testing and debugging only - to help ensure that shallow
77435 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){
77436 if( pX->pScopyFrom==pMem ){
77438 if( pVdbe->db->flags & SQLITE_VdbeTrace ){
77440 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));
77448 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
77449 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
77452 ** undefined so that we can quickly detect the shallow-copy error */
77453 pX->flags = MEM_Undefined;
77454 pX->pScopyFrom = 0;
77457 pMem->pScopyFrom = 0;
77463 ** pTo are freed. The pFrom->z field is not duplicated. If
77464 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
77474 assert( pTo->db==pFrom->db );
77477 if( (pFrom->flags&MEM_Static)==0 ){
77478 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
77480 pTo->flags |= srcType;
77494 pTo->flags &= ~MEM_Dyn;
77495 if( pTo->flags&(MEM_Str|MEM_Blob) ){
77496 if( 0==(pFrom->flags&MEM_Static) ){
77497 pTo->flags |= MEM_Ephem;
77512 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
77513 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
77514 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
77518 pFrom->flags = MEM_Null;
77519 pFrom->szMalloc = 0;
77544 int nByte = n; /* New value for pMem->n */
77546 u16 flags = 0; /* New value for pMem->flags */
77548 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
77557 if( pMem->db ){
77558 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
77583 return sqlite3ErrorToParser(pMem->db, SQLITE_TOOBIG);
77591 memcpy(pMem->z, z, nAlloc);
77594 pMem->z = (char *)z;
77596 pMem->zMalloc = pMem->z;
77597 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
77599 pMem->xDel = xDel;
77604 pMem->n = nByte;
77605 pMem->flags = flags;
77607 pMem->enc = enc;
77609 }else if( pMem->db==0 ){
77610 pMem->enc = SQLITE_UTF8;
77613 assert( pMem->db!=0 );
77614 pMem->enc = ENC(pMem->db);
77637 ** pMem->zMalloc to hold the content from the btree, if possible. New
77638 ** pMem->zMalloc space will be allocated if necessary. The calling routine
77652 pMem->flags = MEM_Null;
77657 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
77659 pMem->z[amt] = 0; /* Overrun area used when reading malformed records */
77660 pMem->flags = MEM_Blob;
77661 pMem->n = (int)amt;
77682 pMem->z = (char *)sqlite3BtreePayloadFetch(pCur, &available);
77683 assert( pMem->z!=0 );
77686 pMem->flags = MEM_Blob|MEM_Ephem;
77687 pMem->n = (int)amt;
77698 ** to a zero-terminated version of that string.
77702 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
77705 assert( (pVal->flags & (MEM_Null))==0 );
77706 if( pVal->flags & (MEM_Blob|MEM_Str) ){
77708 pVal->flags |= MEM_Str;
77709 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
77712 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
77713 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
77718 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
77721 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
77723 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
77724 || pVal->db->mallocFailed );
77725 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
77727 return pVal->z;
77739 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
77745 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
77748 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
77750 return pVal->z;
77752 if( pVal->flags&MEM_Null ){
77764 p->flags = MEM_Null;
77765 p->db = db;
77786 ** Otherwise, if the second argument is non-zero, then this function is
77795 UnpackedRecord *pRec = p->ppRec[0];
77798 Index *pIdx = p->pIdx; /* Index being probed */
77801 int nCol = pIdx->nColumn; /* Number of index columns including rowid */
77806 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
77807 if( pRec->pKeyInfo ){
77808 assert( pRec->pKeyInfo->nAllField==nCol );
77809 assert( pRec->pKeyInfo->enc==ENC(db) );
77810 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
77812 pRec->aMem[i].flags = MEM_Null;
77813 pRec->aMem[i].db = db;
77821 p->ppRec[0] = pRec;
77824 pRec->nField = p->iVal+1;
77825 return &pRec->aMem[p->iVal];
77872 assert( (p->flags & EP_TokenOnly)==0 );
77873 pList = p->x.pList;
77874 if( pList ) nVal = pList->nExpr;
77875 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
77877 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
77878 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
77890 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
77901 assert( pCtx->pParse->rc==SQLITE_OK );
77905 pFunc->xSFunc(&ctx, nVal, apVal);
77908 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
77915 pCtx->pParse->nErr++;
77918 pCtx->pParse->rc = rc;
77964 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
77966 if( op==TK_REGISTER ) op = pExpr->op2;
77968 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
77975 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
77978 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
77979 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
77989 ** case when the value is -9223372036854775808.
77992 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
77993 pExpr = pExpr->pLeft;
77994 op = pExpr->op;
77995 negInt = -1;
77996 zNeg = "-";
78003 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
78005 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
78007 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
78014 assert( (pVal->flags & MEM_IntReal)==0 );
78015 if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
78016 testcase( pVal->flags & MEM_Int );
78017 testcase( pVal->flags & MEM_Real );
78018 pVal->flags &= ~MEM_Str;
78024 /* This branch happens for multiple negative signs. Ex: -(-5) */
78025 if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx)
78029 if( pVal->flags & MEM_Real ){
78030 pVal->u.r = -pVal->u.r;
78031 }else if( pVal->u.i==SMALLEST_INT64 ){
78033 pVal->u.r = -(double)SMALLEST_INT64;
78035 pVal->u.r = LARGEST_INT64;
78039 pVal->u.i = -pVal->u.i;
78051 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
78052 assert( pExpr->u.zToken[1]=='\'' );
78055 zVal = &pExpr->u.zToken[2];
78056 nVal = sqlite3Strlen30(zVal)-1;
78070 pVal->flags = MEM_Int;
78071 pVal->u.i = pExpr->u.zToken[4]==0;
78080 if( pCtx==0 || pCtx->pParse->nErr==0 )
78141 sqlite3 *db = pParse->db;
78146 assert( pExpr==0 || pExpr->op!=TK_REGISTER || pExpr->op2!=TK_VARIABLE );
78152 }else if( pExpr->op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
78154 int iBindVar = pExpr->iColumn;
78155 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
78156 if( (v = pParse->pReprepare)!=0 ){
78159 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
78161 pVal->db = pParse->db;
78168 assert( pVal==0 || pVal->db==db );
78219 if( pExpr==0 || pExpr->op!=TK_SELECT ){
78230 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
78262 ** Extract the iCol-th column from the nRec-byte record in pRec. Write
78304 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
78305 pMem->enc = ENC(db);
78317 int nCol = pRec->pKeyInfo->nAllField;
78318 Mem *aMem = pRec->aMem;
78323 sqlite3KeyInfoUnref(pRec->pKeyInfo);
78348 sqlite3DbFreeNN(((Mem*)v)->db, v);
78357 return valueToText(pVal, enc)!=0 ? pVal->n : 0;
78361 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
78362 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
78363 return p->n;
78365 if( (p->flags & MEM_Blob)!=0 ){
78366 if( p->flags & MEM_Zero ){
78367 return p->n + p->u.nZero;
78369 return p->n;
78372 if( p->flags & MEM_Null ) return 0;
78403 sqlite3 *db = pParse->db;
78407 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp));
78408 p->db = db;
78409 if( db->pVdbe ){
78410 db->pVdbe->pPrev = p;
78412 p->pNext = db->pVdbe;
78413 p->pPrev = 0;
78414 db->pVdbe = p;
78415 p->iVdbeMagic = VDBE_MAGIC_INIT;
78416 p->pParse = pParse;
78417 pParse->pVdbe = p;
78418 assert( pParse->aLabel==0 );
78419 assert( pParse->nLabel==0 );
78420 assert( p->nOpAlloc==0 );
78421 assert( pParse->szOpAlloc==0 );
78430 return p->pParse;
78438 sqlite3DbFree(p->db, p->zErrMsg);
78440 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap);
78449 p->prepFlags = prepFlags;
78451 p->expmask = 0;
78453 assert( p->zSql==0 );
78454 p->zSql = sqlite3DbStrNDup(p->db, z, n);
78459 ** Add a new element to the Vdbe->pDblStr list.
78465 sizeof(*pStr)+n+1-sizeof(pStr->z));
78467 pStr->pNextStr = p->pDblStr;
78468 p->pDblStr = pStr;
78469 memcpy(pStr->z, z, n+1);
78477 ** zId of length nId is a double-quoted identifier. Check to see if
78482 const char *zId /* The double-quoted identifier, already dequoted */
78486 if( pVdbe->pDblStr==0 ) return 0;
78487 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){
78488 if( strcmp(zId, pStr->z)==0 ) return 1;
78500 assert( pA->db==pB->db );
78504 pTmp = pA->pNext;
78505 pA->pNext = pB->pNext;
78506 pB->pNext = pTmp;
78507 pTmp = pA->pPrev;
78508 pA->pPrev = pB->pPrev;
78509 pB->pPrev = pTmp;
78510 zTmp = pA->zSql;
78511 pA->zSql = pB->zSql;
78512 pB->zSql = zTmp;
78514 zTmp = pA->zNormSql;
78515 pA->zNormSql = pB->zNormSql;
78516 pB->zNormSql = zTmp;
78518 pB->expmask = pA->expmask;
78519 pB->prepFlags = pA->prepFlags;
78520 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter));
78521 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++;
78529 ** If an out-of-memory error occurs while resizing the array, return
78536 Parse *p = v->pParse;
78538 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
78546 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc
78547 : (sqlite3_int64)v->nOpAlloc+nOp);
78549 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc
78555 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){
78556 sqlite3OomFault(p->db);
78561 assert( nNew>=(v->nOpAlloc+nOp) );
78562 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
78564 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew);
78565 v->nOpAlloc = p->szOpAlloc/sizeof(Op);
78566 v->aOp = pNew;
78606 assert( p->nOpAlloc<=p->nOp );
78608 assert( p->nOpAlloc>p->nOp );
78615 i = p->nOp;
78616 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
78618 if( p->nOpAlloc<=i ){
78621 p->nOp++;
78622 pOp = &p->aOp[i];
78623 pOp->opcode = (u8)op;
78624 pOp->p5 = 0;
78625 pOp->p1 = p1;
78626 pOp->p2 = p2;
78627 pOp->p3 = p3;
78628 pOp->p4.p = 0;
78629 pOp->p4type = P4_NOTUSED;
78631 pOp->zComment = 0;
78634 if( p->db->flags & SQLITE_VdbeAddopTrace ){
78635 sqlite3VdbePrintOp(0, i, &p->aOp[i]);
78636 test_addop_breakpoint(i, &p->aOp[i]);
78640 pOp->cycles = 0;
78641 pOp->cnt = 0;
78644 pOp->iSrcLine = 0;
78738 Vdbe *v = pParse->pVdbe;
78743 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*);
78744 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte);
78746 assert( pParse->db->mallocFailed );
78747 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc);
78750 pCtx->pOut = 0;
78751 pCtx->pFunc = (FuncDef*)pFunc;
78752 pCtx->pVdbe = 0;
78753 pCtx->isError = 0;
78754 pCtx->argc = nArg;
78755 pCtx->iOp = sqlite3VdbeCurrentAddr(v);
78787 if( pParse->addrExplain==0 ) return 0;
78788 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain);
78789 return pOp->p2;
78813 if( pParse->explain==2 )
78821 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap);
78823 v = pParse->pVdbe;
78824 iThis = v->nOp;
78825 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0,
78827 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z);
78829 pParse->addrExplain = iThis;
78839 pParse->addrExplain = sqlite3VdbeExplainParent(pParse);
78855 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
78856 sqlite3MayAbort(p->pParse);
78871 if( p->db->mallocFailed==0 ){
78872 VdbeOp *pOp = &p->aOp[addr];
78873 pOp->p4type = P4_INT32;
78874 pOp->p4.i = p4;
78879 /* Insert the end of a co-routine
78885 ** co-routine has its own independent set of registers, because co-routines
78887 ** that could cause problems if two or more co-routines are using the same
78890 v->pParse->nTempReg = 0;
78891 v->pParse->nRangeReg = 0;
78903 ** always negative and P2 values are suppose to be non-negative.
78910 ** Parse.aLabel[x] Stores the address that the x-th label resolves
78912 ** labels stores -1, but that is not required.
78920 return --pParse->nLabel;
78929 int nNewSize = 10 - p->nLabel;
78930 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
78931 nNewSize*sizeof(p->aLabel[0]));
78932 if( p->aLabel==0 ){
78933 p->nLabelAlloc = 0;
78937 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1;
78939 p->nLabelAlloc = nNewSize;
78940 p->aLabel[j] = v->nOp;
78944 Parse *p = v->pParse;
78946 assert( v->iVdbeMagic==VDBE_MAGIC_INIT );
78947 assert( j<-p->nLabel );
78950 if( p->db->flags & SQLITE_VdbeAddopTrace ){
78951 printf("RESOLVE LABEL %d to %d\n", x, v->nOp);
78954 if( p->nLabelAlloc + p->nLabel < 0 ){
78957 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */
78958 p->aLabel[j] = v->nOp;
78966 p->runOnlyOnce = 1;
78973 p->runOnlyOnce = 0;
78980 ** in a Vdbe main program and each of the sub-programs (triggers) it may
78991 ** sqlite3DbFree(v->db, sIter.apSub);
79000 int iSub; /* 0 = main program, 1 = first sub-program etc. */
79003 Vdbe *v = p->v;
79008 if( p->iSub<=p->nSub ){
79010 if( p->iSub==0 ){
79011 aOp = v->aOp;
79012 nOp = v->nOp;
79014 aOp = p->apSub[p->iSub-1]->aOp;
79015 nOp = p->apSub[p->iSub-1]->nOp;
79017 assert( p->iAddr<nOp );
79019 pRet = &aOp[p->iAddr];
79020 p->iAddr++;
79021 if( p->iAddr==nOp ){
79022 p->iSub++;
79023 p->iAddr = 0;
79026 if( pRet->p4type==P4_SUBPROGRAM ){
79027 int nByte = (p->nSub+1)*sizeof(SubProgram*);
79029 for(j=0; j<p->nSub; j++){
79030 if( p->apSub[j]==pRet->p4.pProgram ) break;
79032 if( j==p->nSub ){
79033 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
79034 if( !p->apSub ){
79037 p->apSub[p->nSub++] = pRet->p4.pProgram;
79050 ** sub-programs contains any of the following:
79067 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
79081 int opcode = pOp->opcode;
79087 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort))
79092 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1;
79096 ** where a "DELETE FROM tbl" has a statement-journal but does not
79097 ** require one. This is not so bad - it is an inefficiency, not a bug. */
79098 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1;
79103 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){
79108 sqlite3DbFree(v->db, sIter.apSub);
79115 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
79119 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
79128 || (pC->eCurType!=CURTYPE_SORTER
79129 && pC->eCurType!=CURTYPE_PSEUDO
79130 && !pC->isEphemeral)
79132 p->nWrite++;
79143 assert( p->nWrite==0 || p->usesStmtJournal );
79171 Parse *pParse = p->pParse;
79172 int *aLabel = pParse->aLabel;
79173 p->readOnly = 1;
79174 p->bIsReader = 0;
79175 pOp = &p->aOp[p->nOp-1];
79184 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){
79187 switch( pOp->opcode ){
79189 if( pOp->p2!=0 ) p->readOnly = 0;
79194 p->bIsReader = 1;
79202 p->readOnly = 0;
79203 p->bIsReader = 1;
79208 pOp->p4.xAdvance = sqlite3BtreeNext;
79209 pOp->p4type = P4_ADVANCE;
79213 assert( pOp->p2>=0 );
79217 pOp->p4.xAdvance = sqlite3BtreePrevious;
79218 pOp->p4type = P4_ADVANCE;
79222 assert( pOp->p2>=0 );
79227 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
79232 assert( (pOp - p->aOp) >= 3 );
79233 assert( pOp[-1].opcode==OP_Integer );
79234 n = pOp[-1].p1;
79241 if( pOp->p2<0 ){
79243 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
79244 ** have non-negative values for P2. */
79245 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 );
79246 assert( ADDR(pOp->p2)<-pParse->nLabel );
79247 pOp->p2 = aLabel[ADDR(pOp->p2)];
79253 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to
79254 ** have non-negative values for P2. */
79255 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0);
79257 if( pOp==p->aOp ) break;
79258 pOp--;
79260 sqlite3DbFree(p->db, pParse->aLabel);
79261 pParse->aLabel = 0;
79262 pParse->nLabel = 0;
79264 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) );
79271 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
79272 return p->nOp;
79281 ** sqlite3VdbeAddOpList() will always be non-NULL.
79285 assert( p->nOp + N <= p->nOpAlloc );
79299 for(i=0; i<p->nOp; i++){
79300 assert( p->aOp[i].opcode!=OP_ResultRow );
79328 VdbeOp *aOp = p->aOp;
79329 assert( aOp && !p->db->mallocFailed );
79332 assert( DbMaskAllZero(p->btreeMask) );
79335 *pnOp = p->nOp;
79336 p->aOp = 0;
79344 ** Non-zero P2 arguments to jump instructions are automatically adjusted
79351 int iLineno /* Source-file line number of first opcode */
79356 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
79357 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){
79360 pFirst = pOut = &p->aOp[p->nOp];
79362 pOut->opcode = aOp->opcode;
79363 pOut->p1 = aOp->p1;
79364 pOut->p2 = aOp->p2;
79365 assert( aOp->p2>=0 );
79366 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
79367 pOut->p2 += p->nOp;
79369 pOut->p3 = aOp->p3;
79370 pOut->p4type = P4_NOTUSED;
79371 pOut->p4.p = 0;
79372 pOut->p5 = 0;
79374 pOut->zComment = 0;
79377 pOut->iSrcLine = iLineno+i;
79382 if( p->db->flags & SQLITE_VdbeAddopTrace ){
79383 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]);
79387 p->nOp += nOp;
79403 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus);
79405 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte);
79407 ScanStatus *pNew = &aNew[p->nScan++];
79408 pNew->addrExplain = addrExplain;
79409 pNew->addrLoop = addrLoop;
79410 pNew->addrVisit = addrVisit;
79411 pNew->nEst = nEst;
79412 pNew->zName = sqlite3DbStrDup(p->db, zName);
79413 p->aScan = aNew;
79424 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
79427 sqlite3VdbeGetOp(p,addr)->p1 = val;
79430 sqlite3VdbeGetOp(p,addr)->p2 = val;
79433 sqlite3VdbeGetOp(p,addr)->p3 = val;
79436 assert( p->nOp>0 || p->db->mallocFailed );
79437 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
79445 sqlite3VdbeChangeP2(p, addr, p->nOp);
79451 ** the previous opcode (and is thus a no-op) then simply back up
79456 ** strives to omit useless byte-code like this:
79462 if( addr==p->nOp-1 ){
79463 assert( p->aOp[addr].opcode==OP_Once
79464 || p->aOp[addr].opcode==OP_If
79465 || p->aOp[addr].opcode==OP_FkIfZero );
79466 assert( p->aOp[addr].p4type==0 );
79468 sqlite3VdbeGetOp(p,-1)->iSrcLine = 0; /* Erase VdbeCoverage() macros */
79470 p->nOp--;
79472 sqlite3VdbeChangeP2(p, addr, p->nOp);
79482 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
79491 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
79495 freeEphemeralFunction(db, p->pFunc);
79514 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
79528 if( db->pnBytesFreed==0 ){
79536 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
79550 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){
79551 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p);
79553 sqlite3DbFree(db, pOp->zComment);
79562 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
79566 p->pNext = pVdbe->pProgram;
79567 pVdbe->pProgram = p;
79574 return pVdbe->pProgram!=0;
79582 if( p->db->mallocFailed ) return 0;
79583 assert( addr>=0 && addr<p->nOp );
79584 pOp = &p->aOp[addr];
79585 freeP4(p->db, pOp->p4type, pOp->p4.p);
79586 pOp->p4type = P4_NOTUSED;
79587 pOp->p4.z = 0;
79588 pOp->opcode = OP_Noop;
79597 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){
79598 return sqlite3VdbeChangeToNoop(p, p->nOp-1);
79617 assert( pParse->pVdbe );
79619 assert( iFirst+N-1<=pParse->nMem );
79624 N--;
79626 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){
79627 mask &= ~MASKBIT32(N-1);
79628 N--;
79632 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask);
79633 if( bUndefine ) sqlite3VdbeChangeP5(pParse->pVdbe, 1);
79662 if( pOp->p4type ){
79663 freeP4(p->db, pOp->p4type, pOp->p4.p);
79664 pOp->p4type = 0;
79665 pOp->p4.p = 0;
79668 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n);
79671 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
79672 pOp->p4type = P4_DYNAMIC;
79679 db = p->db;
79680 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
79681 assert( p->aOp!=0 || db->mallocFailed );
79682 if( db->mallocFailed ){
79686 assert( p->nOp>0 );
79687 assert( addr<p->nOp );
79689 addr = p->nOp - 1;
79691 pOp = &p->aOp[addr];
79692 if( n>=0 || pOp->p4type ){
79699 pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
79700 pOp->p4type = P4_INT32;
79703 pOp->p4.p = (void*)zP4;
79704 pOp->p4type = (signed char)n;
79711 ** to the value defined by the arguments. This is a high-speed
79722 if( p->db->mallocFailed ){
79723 freeP4(p->db, n, pP4);
79726 assert( p->nOp>0 );
79727 pOp = &p->aOp[p->nOp-1];
79728 assert( pOp->p4type==P4_NOTUSED );
79729 pOp->p4type = n;
79730 pOp->p4.p = pP4;
79739 Vdbe *v = pParse->pVdbe;
79750 ** insert a No-op and add the comment to that new instruction. This
79755 assert( p->nOp>0 || p->aOp==0 );
79756 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed
79757 || p->pParse->nErr>0 );
79758 if( p->nOp ){
79759 assert( p->aOp );
79760 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
79761 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
79788 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
79793 ** Return the opcode for a given address. If the address is -1, then
79809 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
79811 addr = p->nOp - 1;
79813 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
79814 if( p->db->mallocFailed ){
79817 return &p->aOp[addr];
79827 if( c=='1' ) return pOp->p1;
79828 if( c=='2' ) return pOp->p2;
79829 if( c=='3' ) return pOp->p3;
79830 if( c=='4' ) return pOp->p4.i;
79831 return pOp->p5;
79842 ** "PX" -> "r[X]"
79843 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
79844 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
79845 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
79848 sqlite3 *db, /* Optional - Oom error reporting only */
79860 zOpName = sqlite3OpcodeName(pOp->opcode);
79867 if( pOp->p5 & SQLITE_STOREP2 ){
79880 sqlite3_str_appendall(&x, pOp->zComment);
79895 sqlite3_str_appendf(&x, "%d..%d", v1, v1+v2-1);
79898 sqlite3_context *pCtx = pOp->p4.pCtx;
79899 if( pOp->p4type!=P4_FUNCCTX || pCtx->argc==1 ){
79901 }else if( pCtx->argc>1 ){
79902 sqlite3_str_appendf(&x, "%d..%d", v1, v1+pCtx->argc-1);
79905 x.nChar -= 2;
79911 if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
79920 if( !seenCom && pOp->zComment ){
79921 sqlite3_str_appendf(&x, "; %s", pOp->zComment);
79923 }else if( pOp->zComment ){
79924 sqlite3_str_appendall(&x, pOp->zComment);
79940 switch( pExpr->op ){
79942 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken);
79945 sqlite3_str_appendf(p, "%d", pExpr->u.iValue);
79951 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable);
79955 if( pExpr->iColumn<0 ){
79958 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn);
79996 displayP4Expr(p, pExpr->pLeft);
79997 if( pExpr->pRight ){
79999 displayP4Expr(p, pExpr->pRight);
80017 switch( pOp->p4type ){
80020 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
80021 assert( pKeyInfo->aSortFlags!=0 );
80022 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField);
80023 for(j=0; j<pKeyInfo->nKeyField; j++){
80024 CollSeq *pColl = pKeyInfo->aColl[j];
80025 const char *zColl = pColl ? pColl->zName : "";
80028 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "",
80029 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "",
80037 displayP4Expr(&x, pOp->p4.pExpr);
80043 CollSeq *pColl = pOp->p4.pColl;
80044 assert( pColl->enc>=0 && pColl->enc<4 );
80045 sqlite3_str_appendf(&x, "%.18s-%s", pColl->zName,
80046 encnames[pColl->enc]);
80050 FuncDef *pDef = pOp->p4.pFunc;
80051 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
80055 FuncDef *pDef = pOp->p4.pCtx->pFunc;
80056 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg);
80060 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64);
80064 sqlite3_str_appendf(&x, "%d", pOp->p4.i);
80068 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
80072 Mem *pMem = pOp->p4.pMem;
80073 if( pMem->flags & MEM_Str ){
80074 zP4 = pMem->z;
80075 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
80076 sqlite3_str_appendf(&x, "%lld", pMem->u.i);
80077 }else if( pMem->flags & MEM_Real ){
80078 sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
80079 }else if( pMem->flags & MEM_Null ){
80082 assert( pMem->flags & MEM_Blob );
80089 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
80096 u32 *ai = pOp->p4.ai;
80114 zP4 = pOp->p4.pTab->zName;
80118 zP4 = pOp->p4.z;
80130 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
80134 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
80135 ** p->btreeMask of databases that will require a lock.
80138 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
80139 assert( i<(int)sizeof(p->btreeMask)*8 );
80140 DbMaskSet(p->btreeMask, i);
80141 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
80142 DbMaskSet(p->lockMask, i);
80148 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
80152 ** that the correct busy-handler callback is invoked if required.
80154 ** If SQLite is not threadsafe but does support shared-cache mode, then
80159 ** If SQLite is not threadsafe and does not support shared-cache mode, this
80160 ** function is a no-op.
80162 ** The p->btreeMask field is a bitmask of all btrees that the prepared
80163 ** statement p will ever use. Let N be the number of bits in p->btreeMask
80173 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
80174 db = p->db;
80175 aDb = db->aDb;
80176 nDb = db->nDb;
80178 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
80194 db = p->db;
80195 aDb = db->aDb;
80196 nDb = db->nDb;
80198 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){
80204 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */
80217 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
80231 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3,
80232 zP4 ? zP4 : "", pOp->p5,
80246 while( (N--)>0 ){
80247 p->db = db;
80248 p->flags = flags;
80249 p->szMalloc = 0;
80251 p->pScopyFrom = 0;
80263 sqlite3 *db = p->db;
80264 if( db->pnBytesFreed ){
80266 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
80280 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
80281 ** sqlite3MemRelease() were called from here. With -O2, this jumps
80286 testcase( p->flags & MEM_Agg );
80287 testcase( p->flags & MEM_Dyn );
80288 testcase( p->xDel==sqlite3VdbeFrameMemDel );
80289 if( p->flags&(MEM_Agg|MEM_Dyn) ){
80291 }else if( p->szMalloc ){
80292 sqlite3DbFreeNN(db, p->zMalloc);
80293 p->szMalloc = 0;
80296 p->flags = MEM_Undefined;
80309 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0;
80325 pFrame->pParent = pFrame->v->pDelFrame;
80326 pFrame->v->pDelFrame = pFrame;
80346 int nSub = 0; /* Number of sub-vdbes seen so far */
80347 SubProgram **apSub = 0; /* Array of sub-vdbes */
80358 ** encountered, but p->pc will eventually catch up to nRow.
80360 nRow = p->nOp;
80362 if( pSub->flags&MEM_Blob ){
80365 nSub = pSub->n/sizeof(Vdbe*);
80366 apSub = (SubProgram **)pSub->z;
80369 nRow += apSub[i]->nOp;
80376 p->rc = SQLITE_OK;
80380 if( i<p->nOp ){
80383 aOp = p->aOp;
80388 i -= p->nOp;
80391 for(j=0; i>=apSub[j]->nOp; j++){
80392 i -= apSub[j]->nOp;
80393 assert( i<apSub[j]->nOp || j+1<nSub );
80395 aOp = apSub[j]->aOp;
80400 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
80410 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0);
80411 if( p->rc!=SQLITE_OK ){
80415 apSub = (SubProgram **)pSub->z;
80418 pSub->n = nSub*sizeof(SubProgram*);
80419 nRow += aOp[i].p4.pProgram->nOp;
80426 if( pOp->opcode==OP_OpenRead ) break;
80427 if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
80428 if( pOp->opcode==OP_ReopenIdx ) break;
80452 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
80454 for(i=0; i<p->nChildCsr; i++){
80455 sqlite3VdbeFreeCursor(p->v, apCsr[i]);
80457 releaseMemArray(aMem, p->nChildMem);
80458 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0);
80459 sqlite3DbFree(p->v->db, p);
80470 ** When p->explain==1, each instruction is listed. When
80471 ** p->explain==2, only OP_Explain instructions are listed and these
80472 ** are shown in a different format. p->explain==2 is used to implement
80474 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers
80478 ** When p->explain==1, first the main program is listed, then each of
80485 sqlite3 *db = p->db; /* The database connection */
80488 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
80489 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0);
80493 assert( p->explain );
80494 assert( p->iVdbeMagic==VDBE_MAGIC_RUN );
80495 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
80499 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
80502 p->pResultSet = 0;
80504 if( p->rc==SQLITE_NOMEM ){
80516 assert( p->nMem>9 );
80517 pSub = &p->aMem[9];
80523 rc = sqlite3VdbeNextOpcode(p, pSub, p->explain==2, &p->pc, &i, &aOp);
80527 if( AtomicLoad(&db->u1.isInterrupted) ){
80528 p->rc = SQLITE_INTERRUPT;
80530 sqlite3VdbeError(p, sqlite3ErrStr(p->rc));
80533 if( p->explain==2 ){
80534 sqlite3VdbeMemSetInt64(pMem, pOp->p1);
80535 sqlite3VdbeMemSetInt64(pMem+1, pOp->p2);
80536 sqlite3VdbeMemSetInt64(pMem+2, pOp->p3);
80537 sqlite3VdbeMemSetStr(pMem+3, zP4, -1, SQLITE_UTF8, sqlite3_free);
80538 p->nResColumn = 4;
80541 sqlite3VdbeMemSetStr(pMem+1, (char*)sqlite3OpcodeName(pOp->opcode),
80542 -1, SQLITE_UTF8, SQLITE_STATIC);
80543 sqlite3VdbeMemSetInt64(pMem+2, pOp->p1);
80544 sqlite3VdbeMemSetInt64(pMem+3, pOp->p2);
80545 sqlite3VdbeMemSetInt64(pMem+4, pOp->p3);
80547 sqlite3VdbeMemSetInt64(pMem+6, pOp->p5);
80551 sqlite3VdbeMemSetStr(pMem+7, zCom, -1, SQLITE_UTF8, sqlite3_free);
80556 sqlite3VdbeMemSetStr(pMem+5, zP4, -1, SQLITE_UTF8, sqlite3_free);
80557 p->nResColumn = 8;
80559 p->pResultSet = pMem;
80560 if( db->mallocFailed ){
80561 p->rc = SQLITE_NOMEM;
80564 p->rc = SQLITE_OK;
80579 if( p->zSql ){
80580 z = p->zSql;
80581 }else if( p->nOp>=1 ){
80582 const VdbeOp *pOp = &p->aOp[0];
80583 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
80584 z = pOp->p4.z;
80594 ** Print an IOTRACE message showing SQL content.
80597 int nOp = p->nOp;
80601 pOp = &p->aOp[0];
80602 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
80605 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
80609 if( z[i-1]!=' ' ){
80632 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
80651 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
80654 if( nByte <= p->nFree ){
80655 p->nFree -= nByte;
80656 pBuf = &p->pSpace[p->nFree];
80658 p->nNeeded += nByte;
80674 assert( p->iVdbeMagic==VDBE_MAGIC_INIT || p->iVdbeMagic==VDBE_MAGIC_RESET );
80678 assert( p->nOp>0 );
80681 p->iVdbeMagic = VDBE_MAGIC_RUN;
80684 for(i=0; i<p->nMem; i++){
80685 assert( p->aMem[i].db==p->db );
80688 p->pc = -1;
80689 p->rc = SQLITE_OK;
80690 p->errorAction = OE_Abort;
80691 p->nChange = 0;
80692 p->cacheCtr = 1;
80693 p->minWriteFileFormat = 255;
80694 p->iStatement = 0;
80695 p->nFkConstraint = 0;
80697 for(i=0; i<p->nOp; i++){
80698 p->aOp[i].cnt = 0;
80699 p->aOp[i].cycles = 0;
80735 assert( p->nOp>0 );
80737 assert( p->iVdbeMagic==VDBE_MAGIC_INIT );
80738 assert( pParse==p->pParse );
80739 p->pVList = pParse->pVList;
80740 pParse->pVList = 0;
80741 db = p->db;
80742 assert( db->mallocFailed==0 );
80743 nVar = pParse->nVar;
80744 nMem = pParse->nMem;
80745 nCursor = pParse->nTab;
80746 nArg = pParse->nMaxArg;
80760 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
80761 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
80763 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
80768 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
80769 if( pParse->explain ){
80776 p->explain = pParse->explain;
80777 if( pParse->explain==2 ){
80787 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME,
80791 p->expired = 0;
80799 ** This two-pass approach that reuses as much memory as possible from
80804 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem));
80805 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem));
80806 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*));
80807 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*));
80809 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64));
80812 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded);
80814 if( !db->mallocFailed ){
80815 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
80816 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
80817 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
80818 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
80820 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
80825 if( db->mallocFailed ){
80826 p->nVar = 0;
80827 p->nCursor = 0;
80828 p->nMem = 0;
80830 p->nCursor = nCursor;
80831 p->nVar = (ynVar)nVar;
80832 initMemArray(p->aVar, nVar, db, MEM_Null);
80833 p->nMem = nMem;
80834 initMemArray(p->aMem, nMem, db, MEM_Undefined);
80835 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*));
80837 memset(p->anExec, 0, p->nOp*sizeof(i64));
80851 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
80852 assert( pCx->pBtx==0 || pCx->isEphemeral );
80853 switch( pCx->eCurType ){
80855 sqlite3VdbeSorterClose(p->db, pCx);
80859 assert( pCx->uc.pCursor!=0 );
80860 sqlite3BtreeCloseCursor(pCx->uc.pCursor);
80865 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur;
80866 const sqlite3_module *pModule = pVCur->pVtab->pModule;
80867 assert( pVCur->pVtab->nRef>0 );
80868 pVCur->pVtab->nRef--;
80869 pModule->xClose(pVCur);
80880 if( p->apCsr ){
80882 for(i=0; i<p->nCursor; i++){
80883 VdbeCursor *pC = p->apCsr[i];
80886 p->apCsr[i] = 0;
80894 ** is used, for example, when a trigger sub-program is halted to restore
80898 Vdbe *v = pFrame->v;
80901 v->anExec = pFrame->anExec;
80903 v->aOp = pFrame->aOp;
80904 v->nOp = pFrame->nOp;
80905 v->aMem = pFrame->aMem;
80906 v->nMem = pFrame->nMem;
80907 v->apCsr = pFrame->apCsr;
80908 v->nCursor = pFrame->nCursor;
80909 v->db->lastRowid = pFrame->lastRowid;
80910 v->nChange = pFrame->nChange;
80911 v->db->nChange = pFrame->nDbChange;
80912 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0);
80913 v->pAuxData = pFrame->pAuxData;
80914 pFrame->pAuxData = 0;
80915 return pFrame->pc;
80927 if( p->pFrame ){
80929 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
80931 p->pFrame = 0;
80932 p->nFrame = 0;
80934 assert( p->nFrame==0 );
80936 if( p->aMem ){
80937 releaseMemArray(p->aMem, p->nMem);
80939 while( p->pDelFrame ){
80940 VdbeFrame *pDel = p->pDelFrame;
80941 p->pDelFrame = pDel->pParent;
80946 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0);
80947 assert( p->pAuxData==0 );
80958 sqlite3 *db = p->db;
80960 if( p->nResColumn ){
80961 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
80962 sqlite3DbFree(db, p->aColName);
80965 p->nResColumn = (u16)nResColumn;
80966 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n );
80967 if( p->aColName==0 ) return;
80968 initMemArray(p->aColName, n, db, MEM_Null);
80990 assert( idx<p->nResColumn );
80992 if( p->db->mallocFailed ){
80996 assert( p->aColName!=0 );
80997 pColName = &(p->aColName[idx+var*p->nResColumn]);
80998 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
80999 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
81006 ** write-transaction spanning more than one database file, this routine
81007 ** takes care of the super-journal trickery.
81011 int nTrans = 0; /* Number of databases with an active write-transaction
81012 ** that are candidates for a two-phase commit using a
81013 ** super-journal */
81026 ** be done before determining whether a super-journal file is
81035 ** one database file has an open write transaction, a super-journal
81038 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81039 Btree *pBt = db->aDb[i].pBt;
81041 /* Whether or not a database might need a super-journal depends upon
81043 ** journal modes use a super-journal and which do not */
81056 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
81071 /* If there are any write-transactions at all, invoke the commit hook */
81072 if( needXcommit && db->xCommitCallback ){
81073 rc = db->xCommitCallback(db->pCommitArg);
81079 /* The simple case - no more than one database file (not counting the
81081 ** super-journal.
81085 ** that case we do not support atomic multi-file commits, so use the
81088 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
81091 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81092 Btree *pBt = db->aDb[i].pBt;
81103 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81104 Btree *pBt = db->aDb[i].pBt;
81114 /* The complex case - There is a multi-file write-transaction active.
81115 ** This requires a super-journal file to ensure the transaction is
81120 sqlite3_vfs *pVfs = db->pVfs;
81121 char *zSuper = 0; /* File-name for the super-journal */
81122 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
81129 /* Select a super-journal file name */
81147 sqlite3_snprintf(13, &zSuper[nMainFile], "-mj%06X9%02X",
81149 /* The antipenultimate character of the super-journal name must
81151 assert( zSuper[sqlite3Strlen30(zSuper)-3]=='9' );
81156 /* Open the super-journal. */
81163 sqlite3DbFree(db, zSuper-4);
81168 ** super-journal file. If an error occurs at this point close
81169 ** and delete the super-journal file. All the individual journal files
81170 ** still have 'null' as the super-journal pointer, so they will roll
81173 for(i=0; i<db->nDb; i++){
81174 Btree *pBt = db->aDb[i].pBt;
81186 sqlite3DbFree(db, zSuper-4);
81192 /* Sync the super-journal file. If the IOCAP_SEQUENTIAL device
81200 sqlite3DbFree(db, zSuper-4);
81205 ** sets the super-journal pointer in each individual journal. If
81206 ** an error occurs here, do not delete the super-journal file.
81210 ** super-journal file will be orphaned. But we cannot delete it,
81211 ** in case the super-journal file name was written into the journal
81214 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
81215 Btree *pBt = db->aDb[i].pBt;
81223 sqlite3DbFree(db, zSuper-4);
81227 /* Delete the super-journal file. This commits the transaction. After
81232 sqlite3DbFree(db, zSuper-4);
81247 for(i=0; i<db->nDb; i++){
81248 Btree *pBt = db->aDb[i].pBt;
81267 ** This is an internal self-check only - it is not an essential processing
81270 ** This is a no-op if NDEBUG is defined.
81278 p = db->pVdbe;
81282 if( p->readOnly==0 ) nWrite++;
81283 if( p->bIsReader ) nRead++;
81285 p = p->pNext;
81287 assert( cnt==db->nVdbeActive );
81288 assert( nWrite==db->nVdbeWrite );
81289 assert( nRead==db->nVdbeRead );
81296 ** If the Vdbe passed as the first argument opened a statement-transaction,
81306 sqlite3 *const db = p->db;
81309 const int iSavepoint = p->iStatement-1;
81312 assert( db->nStatement>0 );
81313 assert( p->iStatement==(db->nStatement+db->nSavepoint) );
81315 for(i=0; i<db->nDb; i++){
81317 Btree *pBt = db->aDb[i].pBt;
81330 db->nStatement--;
81331 p->iStatement = 0;
81346 db->nDeferredCons = p->nStmtDefCons;
81347 db->nDeferredImmCons = p->nStmtDefImmCons;
81352 if( p->db->nStatement && p->iStatement ){
81367 ** and write an error message to it. Then return SQLITE_ERROR.
81371 sqlite3 *db = p->db;
81372 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
81373 || (!deferred && p->nFkConstraint>0)
81375 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
81376 p->errorAction = OE_Abort;
81399 sqlite3 *db = p->db;
81417 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){
81420 if( db->mallocFailed ){
81421 p->rc = SQLITE_NOMEM_BKPT;
81428 if( p->pc>=0 && p->bIsReader ){
81429 int mrc; /* Primary error code from p->rc */
81437 mrc = p->rc & 0xff;
81441 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
81446 ** Even if the statement is read-only, it is important to perform
81448 ** occurred while writing to the journal, sub-journal or database
81453 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
81454 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
81462 db->autoCommit = 1;
81463 p->nChange = 0;
81469 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
81473 /* If the auto-commit flag is set and this is the only active writer
81480 && db->autoCommit
81481 && db->nVdbeWrite==(p->readOnly==0)
81483 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
81486 if( NEVER(p->readOnly) ){
81492 /* The auto-commit flag is true, the vdbe program was successful
81498 if( rc==SQLITE_BUSY && p->readOnly ){
81502 p->rc = rc;
81504 p->nChange = 0;
81506 db->nDeferredCons = 0;
81507 db->nDeferredImmCons = 0;
81508 db->flags &= ~(u64)SQLITE_DeferFKs;
81513 p->nChange = 0;
81515 db->nStatement = 0;
81517 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
81519 }else if( p->errorAction==OE_Abort ){
81524 db->autoCommit = 1;
81525 p->nChange = 0;
81529 /* If eStatementOp is non-zero, then a statement transaction needs to
81538 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
81539 p->rc = rc;
81540 sqlite3DbFree(db, p->zErrMsg);
81541 p->zErrMsg = 0;
81545 db->autoCommit = 1;
81546 p->nChange = 0;
81551 ** has been rolled back, update the database connection change-counter.
81553 if( p->changeCntOn ){
81555 sqlite3VdbeSetChanges(db, p->nChange);
81559 p->nChange = 0;
81567 if( p->pc>=0 ){
81568 db->nVdbeActive--;
81569 if( !p->readOnly ) db->nVdbeWrite--;
81570 if( p->bIsReader ) db->nVdbeRead--;
81571 assert( db->nVdbeActive>=db->nVdbeRead );
81572 assert( db->nVdbeRead>=db->nVdbeWrite );
81573 assert( db->nVdbeWrite>=0 );
81575 p->iVdbeMagic = VDBE_MAGIC_HALT;
81577 if( db->mallocFailed ){
81578 p->rc = SQLITE_NOMEM_BKPT;
81581 /* If the auto-commit flag is set to true, then any locks that were held
81583 ** to invoke any required unlock-notify callbacks.
81585 if( db->autoCommit ){
81589 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
81590 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
81596 ** in p->rc. This routine sets that result back to SQLITE_OK.
81599 p->rc = SQLITE_OK;
81603 ** Copy the error code and error message belonging to the VDBE passed
81607 ** This function does not clear the VDBE error code or message, just
81611 sqlite3 *db = p->db;
81612 int rc = p->rc;
81613 if( p->zErrMsg ){
81614 db->bBenignMalloc++;
81616 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
81617 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
81619 db->bBenignMalloc--;
81620 }else if( db->pErr ){
81621 sqlite3ValueSetNull(db->pErr);
81623 db->errCode = rc;
81633 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
81634 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
81635 assert( v->db->init.busy==0 );
81638 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
81640 sqlite3DbFree(v->db, zExpanded);
81665 db = p->db;
81674 ** and error message from the VDBE into the main database structure. But
81678 if( p->pc>=0 ){
81680 if( db->pErr || p->zErrMsg ){
81683 db->errCode = p->rc;
81685 if( p->runOnlyOnce ) p->expired = 1;
81686 }else if( p->rc && p->expired ){
81691 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
81694 /* Reset register contents and reclaim error message memory.
81699 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
81700 if( p->aMem ){
81701 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
81704 if( p->zErrMsg ){
81705 sqlite3DbFree(db, p->zErrMsg);
81706 p->zErrMsg = 0;
81708 p->pResultSet = 0;
81710 p->nWrite = 0;
81719 fprintf(out, "---- ");
81720 for(i=0; i<p->nOp; i++){
81721 fprintf(out, "%02x", p->aOp[i].opcode);
81724 if( p->zSql ){
81726 fprintf(out, "-- ");
81727 for(i=0; (c = p->zSql[i])!=0; i++){
81728 if( pc=='\n' ) fprintf(out, "-- ");
81734 for(i=0; i<p->nOp; i++){
81737 p->aOp[i].cnt,
81738 p->aOp[i].cycles,
81739 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
81742 sqlite3VdbePrintOp(out, i, &p->aOp[i]);
81748 p->iVdbeMagic = VDBE_MAGIC_RESET;
81749 return p->rc & db->errMask;
81754 ** the result code. Write any error message text into *pzErrMsg.
81758 if( p->iVdbeMagic==VDBE_MAGIC_RUN || p->iVdbeMagic==VDBE_MAGIC_HALT ){
81760 assert( (rc & p->db->errMask)==rc );
81786 || (pAux->iAuxOp==iOp
81787 && pAux->iAuxArg>=0
81788 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg))))
81790 testcase( pAux->iAuxArg==31 );
81791 if( pAux->xDeleteAux ){
81792 pAux->xDeleteAux(pAux->pAux);
81794 *pp = pAux->pNextAux;
81797 pp= &pAux->pNextAux;
81812 assert( p->db==0 || p->db==db );
81813 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
81814 for(pSub=p->pProgram; pSub; pSub=pNext){
81815 pNext = pSub->pNext;
81816 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
81819 if( p->iVdbeMagic!=VDBE_MAGIC_INIT ){
81820 releaseMemArray(p->aVar, p->nVar);
81821 sqlite3DbFree(db, p->pVList);
81822 sqlite3DbFree(db, p->pFree);
81824 vdbeFreeOpArray(db, p->aOp, p->nOp);
81825 sqlite3DbFree(db, p->aColName);
81826 sqlite3DbFree(db, p->zSql);
81828 sqlite3DbFree(db, p->zNormSql);
81831 for(pThis=p->pDblStr; pThis; pThis=pNext){
81832 pNext = pThis->pNextStr;
81840 for(i=0; i<p->nScan; i++){
81841 sqlite3DbFree(db, p->aScan[i].zName);
81843 sqlite3DbFree(db, p->aScan);
81855 db = p->db;
81856 assert( sqlite3_mutex_held(db->mutex) );
81858 if( p->pPrev ){
81859 p->pPrev->pNext = p->pNext;
81861 assert( db->pVdbe==p );
81862 db->pVdbe = p->pNext;
81864 if( p->pNext ){
81865 p->pNext->pPrev = p->pPrev;
81867 p->iVdbeMagic = VDBE_MAGIC_DEAD;
81868 p->db = 0;
81882 assert( p->deferredMoveto );
81883 assert( p->isTable );
81884 assert( p->eCurType==CURTYPE_BTREE );
81885 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res);
81891 p->deferredMoveto = 0;
81892 p->cacheStatus = CACHE_STALE;
81905 assert( p->eCurType==CURTYPE_BTREE );
81906 assert( p->uc.pCursor!=0 );
81907 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) );
81908 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow);
81909 p->cacheStatus = CACHE_STALE;
81910 if( isDifferentRow ) p->nullRow = 1;
81919 assert( p->eCurType==CURTYPE_BTREE );
81920 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81937 ** not been deleted out from under the cursor, then this routine is a no-op.
81941 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO );
81942 if( p->deferredMoveto ){
81944 assert( !p->isEphemeral );
81945 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 && !p->nullRow ){
81946 *pp = p->pAltCursor;
81947 *piCol = iMap - 1;
81952 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
81969 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
81976 ** serial-type and data blob separately.
81981 ** -------------- --------------- ---------------
81993 ** N>=12 and even (N-12)/2 BLOB
81994 ** N>=13 and odd (N-13)/2 text
82002 ** Return the serial-type for the value stored in pMem.
82006 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord
82007 ** opcode in the byte-code engine. But by moving this routine in-line, we
82013 int flags = pMem->flags;
82023 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
82024 i64 i = pMem->u.i;
82049 ** as an integer, then we might as well make it an 8-byte floating
82051 pMem->u.r = (double)pMem->u.i;
82052 pMem->flags &= ~MEM_IntReal;
82053 pMem->flags |= MEM_Real;
82062 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
82063 assert( pMem->n>=0 );
82064 n = (u32)pMem->n;
82066 n += pMem->u.nZero;
82094 ** Return the length of the data corresponding to the supplied serial-type.
82098 return (serial_type-12)/2;
82101 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 );
82111 ** If we are on an architecture with mixed-endian floating
82115 ** For most architectures, this is a no-op.
82117 ** (later): It is reported to me that the mixed-endian problem
82119 ** that early versions of GCC stored the two words of a 64-bit
82127 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
82131 ** (2007-08-30) Frank van Vugt has studied this problem closely
82134 ** emulation that uses only 32-bit mantissas instead of a full
82135 ** 48-bits as required by the IEEE standard. (This is the
82138 ** the necessary byte swapping is carried out using a 64-bit integer
82139 ** rather than a 64-bit float. Frank assures us that the code here
82170 ** of the pMem->u.nZero bytes for a MEM_Zero value.
82173 ** of bytes in the zero-filled tail is included in the return value only
82184 assert( sizeof(v)==sizeof(pMem->u.r) );
82185 memcpy(&v, &pMem->u.r, sizeof(v));
82188 v = pMem->u.i;
82193 buf[--i] = (u8)(v&0xFF);
82201 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
82203 len = pMem->n;
82204 if( len>0 ) memcpy(buf, pMem->z, len);
82213 ** big-endian integer. Return the equivalent native integer
82239 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit
82240 ** twos-complement integer. */
82241 pMem->u.i = *(i64*)&x;
82242 pMem->flags = MEM_Int;
82243 testcase( pMem->u.i<0 );
82245 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit
82250 ** defined that 64-bit floating point values really are mixed
82259 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 );
82261 memcpy(&pMem->u.r, &x, sizeof(x));
82262 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real;
82273 ** UPDATE no-change flag set */
82274 pMem->flags = MEM_Null|MEM_Zero;
82275 pMem->n = 0;
82276 pMem->u.nZero = 0;
82281 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */
82282 pMem->flags = MEM_Null;
82286 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement
82288 pMem->u.i = ONE_BYTE_INT(buf);
82289 pMem->flags = MEM_Int;
82290 testcase( pMem->u.i<0 );
82293 case 2: { /* 2-byte signed integer */
82294 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit
82295 ** twos-complement integer. */
82296 pMem->u.i = TWO_BYTE_INT(buf);
82297 pMem->flags = MEM_Int;
82298 testcase( pMem->u.i<0 );
82301 case 3: { /* 3-byte signed integer */
82302 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit
82303 ** twos-complement integer. */
82304 pMem->u.i = THREE_BYTE_INT(buf);
82305 pMem->flags = MEM_Int;
82306 testcase( pMem->u.i<0 );
82309 case 4: { /* 4-byte signed integer */
82310 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit
82311 ** twos-complement integer. */
82312 pMem->u.i = FOUR_BYTE_INT(buf);
82314 /* Work around a sign-extension bug in the HP compiler for HP/UX */
82315 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL;
82317 pMem->flags = MEM_Int;
82318 testcase( pMem->u.i<0 );
82321 case 5: { /* 6-byte signed integer */
82322 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit
82323 ** twos-complement integer. */
82324 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
82325 pMem->flags = MEM_Int;
82326 testcase( pMem->u.i<0 );
82329 case 6: /* 8-byte signed integer */
82337 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */
82338 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */
82339 pMem->u.i = serial_type-8;
82340 pMem->flags = MEM_Int;
82344 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in
82346 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and
82347 ** (N-13)/2 bytes in length. */
82349 pMem->z = (char *)buf;
82350 pMem->n = (serial_type-12)/2;
82351 pMem->flags = aFlag[serial_type&1];
82352 return pMem->n;
82376 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1);
82377 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
82379 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
82380 assert( pKeyInfo->aSortFlags!=0 );
82381 p->pKeyInfo = pKeyInfo;
82382 p->nField = pKeyInfo->nKeyField + 1;
82387 ** Given the nKey-byte encoding of a record in pKey[], populate the
82402 Mem *pMem = p->aMem;
82404 p->default_rc = 0;
82413 pMem->enc = pKeyInfo->enc;
82414 pMem->db = pKeyInfo->db;
82415 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
82416 pMem->szMalloc = 0;
82417 pMem->z = 0;
82420 if( (++u)>=p->nField ) break;
82427 sqlite3VdbeMemSetNull(pMem-1);
82429 assert( u<=pKeyInfo->nKeyField + 1 );
82430 p->nField = u;
82459 pKeyInfo = pPKey2->pKeyInfo;
82460 if( pKeyInfo->db==0 ) return 1;
82461 mem1.enc = pKeyInfo->enc;
82462 mem1.db = pKeyInfo->db;
82478 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB );
82479 assert( pKeyInfo->aSortFlags!=0 );
82480 assert( pKeyInfo->nKeyField>0 );
82506 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
82507 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0);
82510 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
82511 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null))
82513 rc = -rc;
82515 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){
82516 rc = -rc; /* Invert the result for DESC sort order. */
82521 }while( idx1<szHdr1 && i<pPKey2->nField );
82532 rc = pPKey2->default_rc;
82539 if( pKeyInfo->db->mallocFailed ) return 1;
82548 ** limit given by pKeyInfo->nAllField.
82550 ** If this constraint is not satisfied, it means that the high-speed
82574 assert( nField <= pKeyInfo->nAllField );
82584 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
82592 if( pMem1->enc==pColl->enc ){
82595 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
82601 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null);
82602 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null);
82605 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
82606 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
82611 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2);
82621 ** with MEM_Zero. Return true if it could be a zero-blob.
82638 int n1 = pB1->n;
82639 int n2 = pB2->n;
82641 /* It is possible to have a Blob value that has some non-zero content
82645 assert( (pB1->flags & MEM_Zero)==0 || n1==0 );
82646 assert( (pB2->flags & MEM_Zero)==0 || n2==0 );
82648 if( (pB1->flags|pB2->flags) & MEM_Zero ){
82649 if( pB1->flags & pB2->flags & MEM_Zero ){
82650 return pB1->u.nZero - pB2->u.nZero;
82651 }else if( pB1->flags & MEM_Zero ){
82652 if( !isAllZero(pB2->z, pB2->n) ) return -1;
82653 return pB1->u.nZero - n2;
82655 if( !isAllZero(pB1->z, pB1->n) ) return +1;
82656 return n1 - pB2->u.nZero;
82659 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1);
82661 return n1 - n2;
82665 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point
82675 if( x<r ) return -1;
82681 if( r<-9223372036854775808.0 ) return +1;
82682 if( r>=9223372036854775808.0 ) return -1;
82684 if( i<y ) return -1;
82687 if( s<r ) return -1;
82706 f1 = pMem1->flags;
82707 f2 = pMem2->flags;
82715 return (f2&MEM_Null) - (f1&MEM_Null);
82727 if( pMem1->u.i < pMem2->u.i ) return -1;
82728 if( pMem1->u.i > pMem2->u.i ) return +1;
82732 if( pMem1->u.r < pMem2->u.r ) return -1;
82733 if( pMem1->u.r > pMem2->u.r ) return +1;
82740 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
82742 if( pMem1->u.i < pMem2->u.i ) return -1;
82743 if( pMem1->u.i > pMem2->u.i ) return +1;
82746 return -1;
82753 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
82755 return -1;
82769 return -1;
82772 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed );
82773 assert( pMem1->enc==SQLITE_UTF8 ||
82774 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
82780 assert( !pColl || pColl->xCmp );
82795 ** The first argument passed to this function is a serial-type that
82796 ** corresponds to an integer - all values between 1 and 9 inclusive
82832 return (serial_type - 8);
82844 ** If argument bSkip is non-zero, it is assumed that the caller has already
82848 ** fields that appear in both keys are equal, then pPKey2->default_rc is
82851 ** If database corruption is discovered, set pPKey2->errCode to
82853 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
82854 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
82866 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */
82887 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82892 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField
82894 assert( pPKey2->pKeyInfo->aSortFlags!=0 );
82895 assert( pPKey2->pKeyInfo->nKeyField>0 );
82901 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
82902 testcase( pRhs->flags & MEM_Int );
82903 testcase( pRhs->flags & MEM_IntReal );
82909 rc = -1;
82912 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r);
82915 i64 rhs = pRhs->u.i;
82917 rc = -1;
82925 else if( pRhs->flags & MEM_Real ){
82934 rc = -1;
82938 if( mem1.u.r<pRhs->u.r ){
82939 rc = -1;
82940 }else if( mem1.u.r>pRhs->u.r ){
82944 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r);
82950 else if( pRhs->flags & MEM_Str ){
82954 rc = -1;
82958 mem1.n = (serial_type - 12) / 2;
82962 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i
82964 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82966 }else if( pKeyInfo->aColl[i] ){
82967 mem1.enc = pKeyInfo->enc;
82968 mem1.db = pKeyInfo->db;
82972 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode
82975 int nCmp = MIN(mem1.n, pRhs->n);
82976 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
82977 if( rc==0 ) rc = mem1.n - pRhs->n;
82983 else if( pRhs->flags & MEM_Blob ){
82984 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 );
82988 rc = -1;
82990 int nStr = (serial_type - 12) / 2;
82994 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
82996 }else if( pRhs->flags & MEM_Zero ){
83000 rc = nStr - pRhs->u.nZero;
83003 int nCmp = MIN(nStr, pRhs->n);
83004 rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
83005 if( rc==0 ) rc = nStr - pRhs->n;
83017 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i];
83021 !=(serial_type==0 || (pRhs->flags&MEM_Null)))
83023 rc = -rc;
83032 if( i==pPKey2->nField ) break;
83047 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc)
83048 || pPKey2->pKeyInfo->db->mallocFailed
83050 pPKey2->eqSeen = 1;
83051 return pPKey2->default_rc;
83064 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
83082 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
83085 case 1: { /* 1-byte signed integer */
83090 case 2: { /* 2-byte signed integer */
83095 case 3: { /* 3-byte signed integer */
83100 case 4: { /* 4-byte signed integer */
83106 case 5: { /* 6-byte signed integer */
83111 case 6: { /* 8-byte signed integer */
83138 v = pPKey2->aMem[0].u.i;
83140 res = pPKey2->r1;
83142 res = pPKey2->r2;
83143 }else if( pPKey2->nField>1 ){
83149 ** fields. Return pPKey2->default_rc in this case. */
83150 res = pPKey2->default_rc;
83151 pPKey2->eqSeen = 1;
83161 ** uses the collation sequence BINARY and (c) that the size-of-header varint
83172 assert( pPKey2->aMem[0].flags & MEM_Str );
83173 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo);
83179 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */
83181 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */
83187 nStr = (serial_type-12) / 2;
83189 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
83192 nCmp = MIN( pPKey2->aMem[0].n, nStr );
83193 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
83196 res = pPKey2->r2;
83198 res = pPKey2->r1;
83200 res = nStr - pPKey2->aMem[0].n;
83202 if( pPKey2->nField>1 ){
83205 res = pPKey2->default_rc;
83206 pPKey2->eqSeen = 1;
83209 res = pPKey2->r2;
83211 res = pPKey2->r1;
83218 || pPKey2->pKeyInfo->db->mallocFailed
83230 ** that the size-of-header varint that occurs at the start of each record
83242 if( p->pKeyInfo->nAllField<=13 ){
83243 int flags = p->aMem[0].flags;
83244 if( p->pKeyInfo->aSortFlags[0] ){
83245 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){
83248 p->r1 = 1;
83249 p->r2 = -1;
83251 p->r1 = -1;
83252 p->r2 = 1;
83261 && p->pKeyInfo->aColl[0]==0
83288 ** than 2GiB are support - anything large must be database corruption.
83290 ** this code can safely assume that nCellKey is 32-bits
83313 /* The last field of the index should be an integer - the ROWID.
83315 getVarint32NR((u8*)&m.z[szHdr-1], typeRowid);
83334 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
83369 assert( pC->eCurType==CURTYPE_BTREE );
83370 pCur = pC->uc.pCursor;
83394 assert( sqlite3_mutex_held(db->mutex) );
83395 db->nChange = nChange;
83396 db->nTotalChange += nChange;
83404 v->changeCntOn = 1;
83413 ** programs obsolete. Removing user-defined functions or collating
83427 for(p = db->pVdbe; p; p=p->pNext){
83428 p->expired = iCode+1;
83436 return v->db;
83443 return v->prepFlags;
83457 Mem *pMem = &v->aVar[iVar-1];
83458 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
83459 if( 0==(pMem->flags & MEM_Null) ){
83460 sqlite3_value *pRet = sqlite3ValueNew(v->db);
83473 ** to sqlite3_reoptimize() that re-preparing the statement may result
83478 assert( (v->db->flags & SQLITE_EnableQPSG)==0 );
83480 v->expmask |= 0x80000000;
83482 v->expmask |= ((u32)1 << (iVar-1));
83491 ** throw an error if it is given inputs that would make it non-deterministic.
83492 ** This routine is invoked by date/time functions that use non-deterministic
83498 if( pCtx->pVdbe==0 ) return 1;
83500 pOp = pCtx->pVdbe->aOp + pCtx->iOp;
83501 if( pOp->opcode==OP_PureFunc ){
83504 if( pOp->p5 & NC_IsCheck ){
83506 }else if( pOp->p5 & NC_GenCol ){
83511 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s",
83512 pCtx->pFunc->zName, zContext);
83513 sqlite3_result_error(pCtx, zMsg, -1);
83522 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
83527 if( pVtab->zErrMsg ){
83528 sqlite3 *db = p->db;
83529 sqlite3DbFree(db, p->zErrMsg);
83530 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
83531 sqlite3_free(pVtab->zErrMsg);
83532 pVtab->zErrMsg = 0;
83541 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord
83551 Mem *pMem = &p->aMem[i];
83552 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem);
83561 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call,
83567 Vdbe *v, /* Vdbe pre-update hook is invoked by */
83575 sqlite3 *db = v->db;
83578 const char *zTbl = pTab->zName;
83581 assert( db->pPreUpdate==0 );
83588 iKey2 = v->aMem[iReg].u.i;
83594 assert( pCsr->nField==pTab->nCol
83595 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1)
83604 preupdate.keyinfo.nKeyField = pTab->nCol;
83610 db->pPreUpdate = &preupdate;
83611 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2);
83612 db->pPreUpdate = 0;
83618 for(i=0; i<pCsr->nField; i++){
83648 ** Return TRUE (non-zero) of the statement supplied as an argument needs
83657 return p==0 || p->expired;
83667 if( p->db==0 ){
83691 assert( p->startTime>0 );
83692 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 );
83693 assert( db->init.busy==0 );
83694 assert( p->zSql!=0 );
83695 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
83696 iElapse = (iNow - p->startTime)*1000000;
83698 if( db->xProfile ){
83699 db->xProfile(db->pProfileArg, p->zSql, iElapse);
83702 if( db->mTrace & SQLITE_TRACE_PROFILE ){
83703 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse);
83705 p->startTime = 0;
83712 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
83714 # define checkProfileCallback(DB,P) /*no-op*/
83729 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
83730 ** pointer is a harmless no-op. */
83734 sqlite3 *db = v->db;
83736 sqlite3_mutex_enter(db->mutex);
83759 sqlite3 *db = v->db;
83760 sqlite3_mutex_enter(db->mutex);
83764 assert( (rc & (db->errMask))==rc );
83766 sqlite3_mutex_leave(db->mutex);
83779 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
83782 for(i=0; i<p->nVar; i++){
83783 sqlite3VdbeMemRelease(&p->aVar[i]);
83784 p->aVar[i].flags = MEM_Null;
83786 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
83787 if( p->expmask ){
83788 p->expired = 1;
83801 if( p->flags & (MEM_Blob|MEM_Str) ){
83803 assert( p->flags==MEM_Null && p->z==0 );
83806 p->flags |= MEM_Blob;
83807 return p->n ? p->z : 0;
83829 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
83833 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) ==
83836 && p->eSubtype=='p'
83837 && strcmp(p->u.zPType, zPType)==0
83839 return (void*)p->z;
83858 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
83859 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
83932 if( pVal->flags & MEM_Null ){
83934 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
83936 }else if( pVal->flags & MEM_Int ){
83938 }else if( pVal->flags & MEM_Str ){
83941 assert( eType == aType[pVal->flags&MEM_AffMask] );
83944 return aType[pVal->flags&MEM_AffMask];
83949 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
83954 return (pVal->flags&MEM_FromBind)!=0;
83966 pNew->flags &= ~MEM_Dyn;
83967 pNew->db = 0;
83968 if( pNew->flags&(MEM_Str|MEM_Blob) ){
83969 pNew->flags &= ~(MEM_Static|MEM_Dyn);
83970 pNew->flags |= MEM_Ephem;
83988 ** The following routines are used by user-defined functions to specify
84005 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
84032 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84041 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84050 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84051 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
84054 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84055 pCtx->isError = SQLITE_ERROR;
84056 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
84060 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84061 pCtx->isError = SQLITE_ERROR;
84062 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
84066 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84067 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
84070 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84071 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
84074 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84075 sqlite3VdbeMemSetNull(pCtx->pOut);
84083 Mem *pOut = pCtx->pOut;
84084 assert( sqlite3_mutex_held(pOut->db->mutex) );
84086 pOut->flags = MEM_Null;
84090 Mem *pOut = pCtx->pOut;
84091 assert( sqlite3_mutex_held(pOut->db->mutex) );
84092 pOut->eSubtype = eSubtype & 0xff;
84093 pOut->flags |= MEM_Subtype;
84101 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84111 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84127 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84136 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84145 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84150 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84151 sqlite3VdbeMemCopy(pCtx->pOut, pValue);
84154 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84155 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
84158 Mem *pOut = pCtx->pOut;
84159 assert( sqlite3_mutex_held(pOut->db->mutex) );
84160 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){
84163 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n);
84167 pCtx->isError = errCode ? errCode : -1;
84169 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode;
84171 if( pCtx->pOut->flags & MEM_Null ){
84172 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1,
84179 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84180 pCtx->isError = SQLITE_TOOBIG;
84181 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1,
84187 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84188 sqlite3VdbeMemSetNull(pCtx->pOut);
84189 pCtx->isError = SQLITE_NOMEM_BKPT;
84190 sqlite3OomFault(pCtx->pOut->db);
84196 ** test-control.
84199 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84200 if( pCtx->pOut->flags & MEM_Int ){
84201 pCtx->pOut->flags &= ~MEM_Int;
84202 pCtx->pOut->flags |= MEM_IntReal;
84216 for(i=0; i<db->nDb; i++){
84217 Btree *pBt = db->aDb[i].pBt;
84223 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){
84224 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry);
84247 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){
84252 ** This "automatic-reset" change is not technically an incompatibility,
84258 ** returns, and those were broken by the automatic-reset change. As a
84259 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
84265 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){
84276 db = p->db;
84277 if( db->mallocFailed ){
84278 p->rc = SQLITE_NOMEM;
84282 if( p->pc<0 && p->expired ){
84283 p->rc = SQLITE_SCHEMA;
84285 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
84287 ** error has occurred, then return the error code in p->rc to the
84294 if( p->pc<0 ){
84299 if( db->nVdbeActive==0 ){
84300 AtomicStore(&db->u1.isInterrupted, 0);
84303 assert( db->nVdbeWrite>0 || db->autoCommit==0
84304 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
84308 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0
84309 && !db->init.busy && p->zSql ){
84310 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
84312 assert( p->startTime==0 );
84316 db->nVdbeActive++;
84317 if( p->readOnly==0 ) db->nVdbeWrite++;
84318 if( p->bIsReader ) db->nVdbeRead++;
84319 p->pc = 0;
84322 p->rcApp = SQLITE_OK;
84325 if( p->explain ){
84330 db->nVdbeExec++;
84332 db->nVdbeExec--;
84341 if( rc==SQLITE_DONE && db->autoCommit ){
84342 assert( p->rc==SQLITE_OK );
84343 p->rc = doWalCallbacks(db);
84344 if( p->rc!=SQLITE_OK ){
84347 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){
84349 ** error has occurred, then return the error code in p->rc to the
84356 db->errCode = rc;
84357 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
84358 p->rc = SQLITE_NOMEM_BKPT;
84359 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc;
84364 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0
84368 return (rc&db->errMask);
84372 ** This is the top-level implementation of sqlite3_step(). Call
84385 db = v->db;
84386 sqlite3_mutex_enter(db->mutex);
84387 v->doingRerun = 0;
84390 int savedPc = v->pc;
84394 ** The error message from the SQL compiler has already been loaded
84395 ** into the database handle. This block copies the error message
84398 ** finalized or reset the parser error message is available via
84401 const char *zErr = (const char *)sqlite3_value_text(db->pErr);
84402 sqlite3DbFree(db, v->zErrMsg);
84403 if( !db->mallocFailed ){
84404 v->zErrMsg = sqlite3DbStrDup(db, zErr);
84405 v->rc = rc = sqlite3ApiExit(db, rc);
84407 v->zErrMsg = 0;
84408 v->rc = rc = SQLITE_NOMEM_BKPT;
84413 if( savedPc>=0 ) v->doingRerun = 1;
84414 assert( v->expired==0 );
84416 sqlite3_mutex_leave(db->mutex);
84426 assert( p && p->pFunc );
84427 return p->pFunc->pUserData;
84434 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
84441 assert( p && p->pOut );
84442 return p->pOut->db;
84456 ** performance by substituting a NULL result, or some other light-weight
84461 return sqlite3_value_nochange(p->pOut);
84474 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
84475 assert( p->pVdbe!=0 );
84478 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
84481 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
84489 ** its pMem->z element.
84492 Mem *pMem = p->pMem;
84493 assert( (pMem->flags & MEM_Agg)==0 );
84496 pMem->z = 0;
84499 pMem->flags = MEM_Agg;
84500 pMem->u.pDef = p->pFunc;
84501 if( pMem->z ){
84502 memset(pMem->z, 0, nByte);
84505 return (void*)pMem->z;
84514 assert( p && p->pFunc && p->pFunc->xFinalize );
84515 assert( sqlite3_mutex_held(p->pOut->db->mutex) );
84517 if( (p->pMem->flags & MEM_Agg)==0 ){
84520 return (void*)p->pMem->z;
84526 ** the user-function defined by pCtx.
84528 ** The left-most argument is 0.
84537 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84539 if( pCtx->pVdbe==0 ) return 0;
84541 assert( pCtx->pVdbe!=0 );
84543 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
84544 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
84545 return pAuxData->pAux;
84553 ** argument to the user-function defined by pCtx. Any previous value is
84556 ** The left-most argument is 0.
84569 Vdbe *pVdbe = pCtx->pVdbe;
84571 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
84578 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
84579 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
84584 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
84586 pAuxData->iAuxOp = pCtx->iOp;
84587 pAuxData->iAuxArg = iArg;
84588 pAuxData->pNextAux = pVdbe->pAuxData;
84589 pVdbe->pAuxData = pAuxData;
84590 if( pCtx->isError==0 ) pCtx->isError = -1;
84591 }else if( pAuxData->xDeleteAux ){
84592 pAuxData->xDeleteAux(pAuxData->pAux);
84595 pAuxData->pAux = pAux;
84596 pAuxData->xDeleteAux = xDelete;
84616 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize );
84617 return p->pMem->n;
84626 return pVm ? pVm->nResColumn : 0;
84635 if( pVm==0 || pVm->pResultSet==0 ) return 0;
84636 return pVm->nResColumn;
84645 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
84646 ** instead of an 8-byte one. This all works fine, except that when
84648 ** that a Mem structure is located on an 8-byte boundary. To prevent
84650 ** using gcc, we force nullMem to be 8-byte aligned using the magical
84688 assert( pVm->db );
84689 sqlite3_mutex_enter(pVm->db->mutex);
84690 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
84691 pOut = &pVm->pResultSet[i];
84693 sqlite3Error(pVm->db, SQLITE_RANGE);
84726 assert( p->db!=0 );
84727 assert( sqlite3_mutex_held(p->db->mutex) );
84728 p->rc = sqlite3ApiExit(p->db, p->rc);
84729 sqlite3_mutex_leave(p->db->mutex);
84779 if( pOut->flags&MEM_Static ){
84780 pOut->flags &= ~MEM_Static;
84781 pOut->flags |= MEM_Ephem;
84800 ** Convert the N-th element of pStmt->pColName[] into a string using
84833 db = p->db;
84838 sqlite3_mutex_enter(db->mutex);
84839 assert( db->mallocFailed==0 );
84842 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
84846 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
84851 if( db->mallocFailed ){
84855 sqlite3_mutex_leave(db->mutex);
84954 ** The error code stored in database p->db is overwritten with the return
84962 sqlite3_mutex_enter(p->db->mutex);
84963 if( p->iVdbeMagic!=VDBE_MAGIC_RUN || p->pc>=0 ){
84964 sqlite3Error(p->db, SQLITE_MISUSE);
84965 sqlite3_mutex_leave(p->db->mutex);
84967 "bind on a busy prepared statement: [%s]", p->zSql);
84970 if( i<1 || i>p->nVar ){
84971 sqlite3Error(p->db, SQLITE_RANGE);
84972 sqlite3_mutex_leave(p->db->mutex);
84975 i--;
84976 pVar = &p->aVar[i];
84978 pVar->flags = MEM_Null;
84979 p->db->errCode = SQLITE_OK;
84984 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host
84990 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 );
84991 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){
84992 p->expired = 1;
85015 pVar = &p->aVar[i-1];
85018 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
85021 sqlite3Error(p->db, rc);
85022 rc = sqlite3ApiExit(p->db, rc);
85025 sqlite3_mutex_leave(p->db->mutex);
85067 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
85068 sqlite3_mutex_leave(p->db->mutex);
85080 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
85081 sqlite3_mutex_leave(p->db->mutex);
85090 sqlite3_mutex_leave(p->db->mutex);
85105 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor);
85106 sqlite3_mutex_leave(p->db->mutex);
85152 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
85156 rc = sqlite3_bind_double(pStmt, i, pValue->u.r);
85160 if( pValue->flags & MEM_Zero ){
85161 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
85163 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
85168 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT,
85169 pValue->enc);
85184 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
85185 sqlite3_mutex_leave(p->db->mutex);
85192 sqlite3_mutex_enter(p->db->mutex);
85193 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){
85199 rc = sqlite3ApiExit(p->db, rc);
85200 sqlite3_mutex_leave(p->db->mutex);
85210 return p ? p->nVar : 0;
85217 ** The result is always UTF-8.
85222 return sqlite3VListNumToName(p->pVList, i);
85232 return sqlite3VListNameToNum(p->pVList, zName, nName);
85245 assert( pTo->db==pFrom->db );
85246 assert( pTo->nVar==pFrom->nVar );
85247 sqlite3_mutex_enter(pTo->db->mutex);
85248 for(i=0; i<pFrom->nVar; i++){
85249 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
85251 sqlite3_mutex_leave(pTo->db->mutex);
85271 if( pFrom->nVar!=pTo->nVar ){
85274 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 );
85275 if( pTo->expmask ){
85276 pTo->expired = 1;
85278 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 );
85279 if( pFrom->expmask ){
85280 pFrom->expired = 1;
85293 return pStmt ? ((Vdbe*)pStmt)->db : 0;
85301 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
85309 return pStmt ? ((Vdbe*)pStmt)->explain : 0;
85317 return v!=0 && v->iVdbeMagic==VDBE_MAGIC_RUN && v->pc>=0;
85334 sqlite3_mutex_enter(pDb->mutex);
85336 pNext = (sqlite3_stmt*)pDb->pVdbe;
85338 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
85340 sqlite3_mutex_leave(pDb->mutex);
85352 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter)))
85359 sqlite3 *db = pVdbe->db;
85360 sqlite3_mutex_enter(db->mutex);
85362 db->pnBytesFreed = (int*)&v;
85365 db->pnBytesFreed = 0;
85366 sqlite3_mutex_leave(db->mutex);
85368 v = pVdbe->aCounter[op];
85369 if( resetFlag ) pVdbe->aCounter[op] = 0;
85379 return p ? p->zSql : 0;
85399 sqlite3_mutex_enter(p->db->mutex);
85401 sqlite3_mutex_leave(p->db->mutex);
85414 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){
85415 sqlite3_mutex_enter(p->db->mutex);
85416 p->zNormSql = sqlite3Normalize(p, p->zSql);
85417 sqlite3_mutex_leave(p->db->mutex);
85419 return p->zNormSql;
85438 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1));
85445 ** This function is called from within a pre-update callback to retrieve
85449 PreUpdate *p = db->pPreUpdate;
85454 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */
85455 if( !p || p->op==SQLITE_INSERT ){
85459 if( p->pPk ){
85460 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
85462 if( iIdx>=p->pCsr->nField || iIdx<0 ){
85468 if( p->pUnpacked==0 ){
85472 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor);
85475 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec);
85477 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec);
85478 if( !p->pUnpacked ) rc = SQLITE_NOMEM;
85484 p->aRecord = aRec;
85487 pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
85488 if( iIdx==p->pTab->iPKey ){
85489 sqlite3VdbeMemSetInt64(pMem, p->iKey1);
85490 }else if( iIdx>=p->pUnpacked->nField ){
85492 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
85493 if( pMem->flags & (MEM_Int|MEM_IntReal) ){
85494 testcase( pMem->flags & MEM_Int );
85495 testcase( pMem->flags & MEM_IntReal );
85508 ** This function is called from within a pre-update callback to retrieve
85512 PreUpdate *p = db->pPreUpdate;
85513 return (p ? p->keyinfo.nKeyField : 0);
85519 ** This function is designed to be called from within a pre-update callback
85523 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a
85524 ** top-level trigger etc.).
85530 PreUpdate *p = db->pPreUpdate;
85531 return (p ? p->v->nFrame : 0);
85537 ** This function is called from within a pre-update callback to retrieve
85541 PreUpdate *p = db->pPreUpdate;
85545 if( !p || p->op==SQLITE_DELETE ){
85549 if( p->pPk && p->op!=SQLITE_UPDATE ){
85550 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx);
85552 if( iIdx>=p->pCsr->nField || iIdx<0 ){
85557 if( p->op==SQLITE_INSERT ){
85558 /* For an INSERT, memory cell p->iNewReg contains the serialized record
85560 UnpackedRecord *pUnpack = p->pNewUnpacked;
85562 Mem *pData = &p->v->aMem[p->iNewReg];
85565 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z);
85570 p->pNewUnpacked = pUnpack;
85572 pMem = &pUnpack->aMem[iIdx];
85573 if( iIdx==p->pTab->iPKey ){
85574 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
85575 }else if( iIdx>=pUnpack->nField ){
85579 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required
85584 assert( p->op==SQLITE_UPDATE );
85585 if( !p->aNew ){
85586 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField);
85587 if( !p->aNew ){
85592 assert( iIdx>=0 && iIdx<p->pCsr->nField );
85593 pMem = &p->aNew[iIdx];
85594 if( pMem->flags==0 ){
85595 if( iIdx==p->pTab->iPKey ){
85596 sqlite3VdbeMemSetInt64(pMem, p->iKey2);
85598 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]);
85623 if( idx<0 || idx>=p->nScan ) return 1;
85624 pScan = &p->aScan[idx];
85627 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop];
85631 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit];
85636 LogEst x = pScan->nEst;
85645 *(const char**)pOut = pScan->zName;
85649 if( pScan->addrExplain ){
85650 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z;
85657 if( pScan->addrExplain ){
85658 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1;
85660 *(int*)pOut = -1;
85676 memset(p->anExec, 0, p->nOp * sizeof(i64));
85697 ** The Vdbe parse-tree explainer is also found here.
85705 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
85730 ** This function returns a pointer to a nul-terminated string in memory
85734 ** then the returned string holds a copy of zRawSql with "-- " prepended
85740 ** with large (multi-megabyte) strings and blobs.
85750 ** parameter index is known, locate the value in p->aVar[]. Then render
85770 db = p->db;
85772 db->aLimit[SQLITE_LIMIT_LENGTH]);
85773 if( db->nVdbeExec>1 ){
85777 sqlite3_str_append(&out, "-- ", 3);
85778 assert( (zRawSql - zStart) > 0 );
85779 sqlite3_str_append(&out, zStart, (int)(zRawSql-zStart));
85781 }else if( p->nVar==0 ){
85810 assert( idx>0 && idx<=p->nVar );
85811 pVar = &p->aVar[idx-1];
85812 if( pVar->flags & MEM_Null ){
85814 }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
85815 sqlite3_str_appendf(&out, "%lld", pVar->u.i);
85816 }else if( pVar->flags & MEM_Real ){
85817 sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
85818 }else if( pVar->flags & MEM_Str ){
85825 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
85833 nOut = pVar->n;
85837 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
85840 sqlite3_str_appendf(&out, "'%.*q'", nOut, pVar->z);
85842 if( nOut<pVar->n ){
85843 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85849 }else if( pVar->flags & MEM_Zero ){
85850 sqlite3_str_appendf(&out, "zeroblob(%d)", pVar->u.nZero);
85853 assert( pVar->flags & MEM_Blob );
85855 nOut = pVar->n;
85860 sqlite3_str_appendf(&out, "%02x", pVar->z[i]&0xff);
85864 if( nOut<pVar->n ){
85865 sqlite3_str_appendf(&out, "/*+%d bytes*/", pVar->n-nOut);
85954 ** use this information to make sure that the zero-blob functionality
85961 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
85962 sqlite3_max_blobsize = p->n;
85972 # define HAS_UPDATE_HOOK(DB) ((DB)->xPreUpdateCallback||(DB)->xUpdateCallback)
85974 # define HAS_UPDATE_HOOK(DB) ((DB)->xUpdateCallback)
86026 ** M: 2 - two-way branch (I=0: fall-thru 1: jump )
86027 ** 3 - two-way + NULL (I=0: fall-thru 1: jump 2: NULL )
86028 ** 4 - OP_Jump (I=0: jump p1 1: jump p2 2: jump p3)
86030 ** In other words, if M is 2, then I is either 0 (for fall-through) or
86032 ** ordinary fall-through, I is 1 if the branch was taken, and I is 2
86043 ** always taken, the flags should be 0x05 since the fall-through and
86045 ** flags should be 0x06 since only the fall-through approach is allowed.
86048 ** interested in equal or not-equal. In other words, I==0 and I==2
86053 ** should be no-ops except for special builds used to measure test coverage.
86058 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
86062 assert( M<=4 ); /* 2: two-way branch, 3: three-way branch, 4: OP_Jump */
86074 ** iSrcLine - the line number of the VdbeCoverage() macro, with
86076 ** I - Mask of bits 0x07 indicating which cases are are
86078 ** fall-thru, 0x02 means taken, 0x04 means NULL. Any
86083 ** M - Type of jump. Same as M argument above
86108 if( ((P)->flags&MEM_Ephem)!=0 \
86112 #define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
86122 int iDb, /* Database the cursor belongs to, or -1 */
86140 ** the top of the register space. Cursor 1 is at Mem[p->nMem-1].
86141 ** Cursor 2 is at Mem[p->nMem-2]. And so forth.
86143 Mem *pMem = iCur>0 ? &p->aMem[p->nMem-iCur] : p->aMem;
86151 assert( iCur>=0 && iCur<p->nCursor );
86152 if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
86153 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
86154 p->apCsr[iCur] = 0;
86157 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
86159 pCx->eCurType = eCurType;
86160 pCx->iDb = iDb;
86161 pCx->nField = nField;
86162 pCx->aOffset = &pCx->aType[nField];
86164 pCx->uc.pCursor = (BtCursor*)
86165 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
86166 sqlite3BtreeCursorZero(pCx->uc.pCursor);
86184 return 0==sqlite3Atoi64(pRec->z, piValue, pRec->n, pRec->enc);
86204 u8 enc = pRec->enc;
86206 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
86207 rc = sqlite3AtoF(pRec->z, &rValue, pRec->n, enc);
86209 if( rc==1 && alsoAnInt(pRec, rValue, &pRec->u.i) ){
86210 pRec->flags |= MEM_Int;
86212 pRec->u.r = rValue;
86213 pRec->flags |= MEM_Real;
86216 /* TEXT->NUMERIC is many->one. Hence, it is important to invalidate the
86219 ** numeric value. Ticket [343634942dd54ab57b7024] 2018-01-31. */
86220 pRec->flags &= ~MEM_Str;
86230 ** floating-point representation if an integer representation
86240 ** No-op. pRec is unchanged.
86250 if( (pRec->flags & MEM_Int)==0 ){ /*OPTIMIZATION-IF-FALSE*/
86251 if( (pRec->flags & MEM_Real)==0 ){
86252 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
86263 if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
86264 if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
86265 testcase( pRec->flags & MEM_Int );
86266 testcase( pRec->flags & MEM_Real );
86267 testcase( pRec->flags & MEM_IntReal );
86271 pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
86306 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
86312 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
86313 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
86315 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);
86317 if( rc==0 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1 ){
86318 pMem->u.i = ix;
86323 }else if( rc==1 && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)==0 ){
86324 pMem->u.i = ix;
86334 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
86335 ** But it does set pMem->u.r and pMem->u.i appropriately.
86338 if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
86339 testcase( pMem->flags & MEM_Int );
86340 testcase( pMem->flags & MEM_Real );
86341 testcase( pMem->flags & MEM_IntReal );
86342 return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
86344 if( pMem->flags & (MEM_Str|MEM_Blob) ){
86345 testcase( pMem->flags & MEM_Str );
86346 testcase( pMem->flags & MEM_Blob );
86358 int f = pMem->flags;
86376 for(i=0; i<25 && i<pMem->n; i++){
86377 sqlite3_str_appendf(pStr, "%02X", ((int)pMem->z[i] & 0xFF));
86380 for(i=0; i<25 && i<pMem->n; i++){
86381 char z = pMem->z[i];
86386 sqlite3_str_appendf(pStr, "+%dz",pMem->u.nZero);
86403 sqlite3_str_appendf(pStr, " %c%d[", c, pMem->n);
86404 for(j=0; j<25 && j<pMem->n; j++){
86405 c = pMem->z[j];
86408 sqlite3_str_appendf(pStr, "]%s", encnames[pMem->enc]);
86418 if( p->flags & MEM_Undefined ){
86420 }else if( p->flags & MEM_Null ){
86421 printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
86422 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
86423 printf(" si:%lld", p->u.i);
86424 }else if( (p->flags & (MEM_IntReal))!=0 ){
86425 printf(" ir:%lld", p->u.i);
86426 }else if( p->flags & MEM_Int ){
86427 printf(" i:%lld", p->u.i);
86429 }else if( p->flags & MEM_Real ){
86430 printf(" r:%.17g", p->u.r);
86441 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
86446 if( p->pScopyFrom ){
86447 printf(" <== R[%d]", (int)(p->pScopyFrom - &p[-iReg]));
86461 for(i=1; i<v->nMem; i++) registerTrace(i, v->aMem+i);
86467 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
86477 ** high-performance timing routines.
86493 ** This file contains inline asm code for retrieving "high-performance"
86500 ** The following routine only works on pentium-class (or newer) processors.
86502 ** processor and returns that value. This can be used for high-res
86576 ** the number of non-transaction savepoints currently in the
86586 for(p=db->pSavepoint; p; p=p->pNext) n++;
86587 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
86593 ** Return the register of pOp->p2 after first preparing it to be
86598 pOut->flags = MEM_Int;
86603 assert( pOp->p2>0 );
86604 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
86605 pOut = &p->aMem[pOp->p2];
86607 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
86610 pOut->flags = MEM_Int;
86623 Op *aOp = p->aOp; /* Copy of p->aOp */
86632 sqlite3 *db = p->db; /* The database */
86640 Mem *aMem = p->aMem; /* Copy of p->aMem */
86650 assert( p->iVdbeMagic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
86653 if( db->xProgress ){
86654 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
86655 assert( 0 < db->nProgressOps );
86656 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
86661 if( p->rc==SQLITE_NOMEM ){
86666 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
86667 testcase( p->rc!=SQLITE_OK );
86668 p->rc = SQLITE_OK;
86669 assert( p->bIsReader || p->readOnly!=0 );
86670 p->iCurrentTime = 0;
86671 assert( p->explain==0 );
86672 p->pResultSet = 0;
86673 db->busyHandler.nBusy = 0;
86674 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
86678 if( p->pc==0
86679 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
86684 if( p->db->flags & SQLITE_VdbeListing ){
86686 for(i=0; i<p->nOp; i++){
86690 if( p->db->flags & SQLITE_VdbeEQP ){
86691 for(i=0; i<p->nOp; i++){
86699 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
86703 for(pOp=&aOp[p->pc]; 1; pOp++){
86708 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
86714 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
86720 if( db->flags & SQLITE_VdbeTrace ){
86721 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
86722 test_trace_breakpoint((int)(pOp - aOp),pOp,p);
86732 sqlite3_interrupt_count--;
86742 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
86744 assert( pOp->p1>0 );
86745 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
86746 assert( memIsValid(&aMem[pOp->p1]) );
86747 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
86748 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
86751 assert( pOp->p2>0 );
86752 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
86753 assert( memIsValid(&aMem[pOp->p2]) );
86754 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
86755 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
86758 assert( pOp->p3>0 );
86759 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86760 assert( memIsValid(&aMem[pOp->p3]) );
86761 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
86762 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
86765 assert( pOp->p2>0 );
86766 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
86767 memAboutToChange(p, &aMem[pOp->p2]);
86770 assert( pOp->p3>0 );
86771 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
86772 memAboutToChange(p, &aMem[pOp->p3]);
86780 switch( pOp->opcode ){
86787 ** the switch statement will break with convention and be flush-left. Another
86825 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
86835 if( pOp->p5 ){
86836 assert( pOp->p2 < (int)(pOp - aOp) );
86837 assert( pOp->p2 > 1 );
86838 pOp = &aOp[pOp->p2 - 2];
86845 pOp = &aOp[pOp->p2 - 1];
86856 ** faster according to "valgrind --tool=cachegrind" */
86858 if( AtomicLoad(&db->u1.isInterrupted) ) goto abort_due_to_interrupt;
86863 ** If the progress callback returns non-zero, exit the virtual machine with
86866 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
86867 assert( db->nProgressOps!=0 );
86868 nProgressLimit += db->nProgressOps;
86869 if( db->xProgress(db->pProgressArg) ){
86886 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86887 pIn1 = &aMem[pOp->p1];
86890 pIn1->flags = MEM_Int;
86891 pIn1->u.i = (int)(pOp-aOp);
86892 REGISTER_TRACE(pOp->p1, pIn1);
86897 pOp = &aOp[pOp->p2 - 1];
86907 pIn1 = &aMem[pOp->p1];
86908 assert( pIn1->flags==MEM_Int );
86909 pOp = &aOp[pIn1->u.i];
86910 pIn1->flags = MEM_Undefined;
86926 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
86927 assert( pOp->p2>=0 && pOp->p2<p->nOp );
86928 assert( pOp->p3>=0 && pOp->p3<p->nOp );
86929 pOut = &aMem[pOp->p1];
86931 pOut->u.i = pOp->p3 - 1;
86932 pOut->flags = MEM_Int;
86933 if( pOp->p2 ) goto jump_to_p2;
86947 pIn1 = &aMem[pOp->p1];
86948 assert( pIn1->flags==MEM_Int );
86949 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
86950 pCaller = &aOp[pIn1->u.i];
86951 assert( pCaller->opcode==OP_Yield );
86952 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
86953 pOp = &aOp[pCaller->p2 - 1];
86954 pIn1->flags = MEM_Undefined;
86973 pIn1 = &aMem[pOp->p1];
86975 pIn1->flags = MEM_Int;
86976 pcDest = (int)pIn1->u.i;
86977 pIn1->u.i = (int)(pOp - aOp);
86978 REGISTER_TRACE(pOp->p1, pIn1);
86988 ** value in register P3 is not NULL, then this routine is a no-op.
86992 pIn3 = &aMem[pOp->p3];
86994 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
86996 if( (pIn3->flags & MEM_Null)==0 ) break;
87014 ** If P4 is not null then it is an error message string.
87035 pcx = (int)(pOp - aOp);
87037 if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
87039 if( pOp->p1==SQLITE_OK && p->pFrame ){
87040 /* Halt the sub-program. Return control to the parent frame. */
87041 pFrame = p->pFrame;
87042 p->pFrame = pFrame->pParent;
87043 p->nFrame--;
87044 sqlite3VdbeSetChanges(db, p->nChange);
87046 if( pOp->p2==OE_Ignore ){
87047 /* Instruction pcx is the OP_Program that invoked the sub-program
87049 ** instruction is set to OE_Ignore, then the sub-program is throwing
87052 pcx = p->aOp[pcx].p2-1;
87054 aOp = p->aOp;
87055 aMem = p->aMem;
87059 p->rc = pOp->p1;
87060 p->errorAction = (u8)pOp->p2;
87061 p->pc = pcx;
87062 assert( pOp->p5<=4 );
87063 if( p->rc ){
87064 if( pOp->p5 ){
87067 testcase( pOp->p5==1 );
87068 testcase( pOp->p5==2 );
87069 testcase( pOp->p5==3 );
87070 testcase( pOp->p5==4 );
87071 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
87072 if( pOp->p4.z ){
87073 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
87076 sqlite3VdbeError(p, "%s", pOp->p4.z);
87078 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
87083 p->rc = SQLITE_BUSY;
87085 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
87086 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
87087 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
87095 ** The 32-bit integer value P1 is written into register P2.
87099 pOut->u.i = pOp->p1;
87106 ** P4 is a pointer to a 64-bit integer value.
87111 assert( pOp->p4.pI64!=0 );
87112 pOut->u.i = *pOp->p4.pI64;
87120 ** P4 is a pointer to a 64-bit floating point value.
87125 pOut->flags = MEM_Real;
87126 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
87127 pOut->u.r = *pOp->p4.pReal;
87135 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
87141 assert( pOp->p4.z!=0 );
87143 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
87147 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
87151 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
87153 pOut->szMalloc = 0;
87154 pOut->flags |= MEM_Static;
87155 if( pOp->p4type==P4_DYNAMIC ){
87156 sqlite3DbFree(db, pOp->p4.z);
87158 pOp->p4type = P4_DYNAMIC;
87159 pOp->p4.z = pOut->z;
87160 pOp->p1 = pOut->n;
87163 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
87166 pOp->opcode = OP_String;
87185 assert( pOp->p4.z!=0 );
87187 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
87188 pOut->z = pOp->p4.z;
87189 pOut->n = pOp->p1;
87190 pOut->enc = encoding;
87193 if( pOp->p3>0 ){
87194 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87195 pIn3 = &aMem[pOp->p3];
87196 assert( pIn3->flags & MEM_Int );
87197 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
87211 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
87219 cnt = pOp->p3-pOp->p2;
87220 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
87221 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
87222 pOut->n = 0;
87224 pOut->uTemp = 0;
87230 pOut->flags = nullFlag;
87231 pOut->n = 0;
87232 cnt--;
87246 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
87247 pOut = &aMem[pOp->p1];
87248 pOut->flags = (pOut->flags&~(MEM_Undefined|MEM_AffMask))|MEM_Null;
87259 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
87261 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
87262 pOut->enc = encoding;
87278 assert( pOp->p1>0 && pOp->p1<=p->nVar );
87279 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
87280 pVar = &p->aVar[pOp->p1 - 1];
87284 pOut = &aMem[pOp->p2];
87287 pOut->flags &= ~(MEM_Dyn|MEM_Ephem);
87288 pOut->flags |= MEM_Static|MEM_FromBind;
87296 ** Move the P3 values in register P1..P1+P3-1 over into
87297 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
87299 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
87307 n = pOp->p3;
87308 p1 = pOp->p1;
87309 p2 = pOp->p2;
87316 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
87317 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
87322 pIn1->pScopyFrom = 0;
87324 for(i=1; i<p->nMem; i++){
87335 }while( --n );
87350 n = pOp->p3;
87351 pIn1 = &aMem[pOp->p1];
87352 pOut = &aMem[pOp->p2];
87359 pOut->pScopyFrom = 0;
87361 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
87362 if( (n--)==0 ) break;
87383 pIn1 = &aMem[pOp->p1];
87384 pOut = &aMem[pOp->p2];
87388 pOut->pScopyFrom = pIn1;
87389 pOut->mScopyFlags = pIn1->flags;
87403 pIn1 = &aMem[pOp->p1];
87404 assert( (pIn1->flags & MEM_Int)!=0 );
87405 pOut = &aMem[pOp->p2];
87406 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
87419 ** non-zero. The P2 opcode must be 1.
87422 assert( pOp->p2==1 );
87433 ** The registers P1 through P1+P2-1 contain a single row of
87436 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
87442 assert( p->nResColumn==pOp->p2 );
87443 assert( pOp->p1>0 );
87444 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
87447 p->cacheCtr = (p->cacheCtr + 2)|1;
87450 ** and have an assigned type. The results are de-ephemeralized as
87453 pMem = p->pResultSet = &aMem[pOp->p1];
87454 for(i=0; i<pOp->p2; i++){
87460 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
87470 if( db->mallocFailed ) goto no_mem;
87472 if( db->mTrace & SQLITE_TRACE_ROW ){
87473 db->trace.xV2(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
87479 p->pc = (int)(pOp - aOp) + 1;
87502 pIn1 = &aMem[pOp->p1];
87503 pIn2 = &aMem[pOp->p2];
87504 pOut = &aMem[pOp->p3];
87507 flags1 = pIn1->flags;
87509 testcase( pIn2->flags & MEM_Null );
87510 if( (flags1 | pIn2->flags) & MEM_Null ){
87516 flags1 = pIn1->flags & ~MEM_Str;
87519 flags1 = pIn1->flags & ~MEM_Str;
87521 flags2 = pIn2->flags;
87524 flags2 = pIn2->flags & ~MEM_Str;
87527 flags2 = pIn2->flags & ~MEM_Str;
87529 nByte = pIn1->n + pIn2->n;
87530 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
87538 memcpy(pOut->z, pIn2->z, pIn2->n);
87539 assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
87540 pIn2->flags = flags2;
87542 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
87543 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
87544 pIn1->flags = flags1;
87545 pOut->z[nByte]=0;
87546 pOut->z[nByte+1] = 0;
87547 pOut->z[nByte+2] = 0;
87548 pOut->flags |= MEM_Term;
87549 pOut->n = (int)nByte;
87550 pOut->enc = encoding;
87571 ** Synopsis: r[P3]=r[P2]-r[P1]
87606 pIn1 = &aMem[pOp->p1];
87608 pIn2 = &aMem[pOp->p2];
87610 pOut = &aMem[pOp->p3];
87611 flags = pIn1->flags | pIn2->flags;
87613 iA = pIn1->u.i;
87614 iB = pIn2->u.i;
87615 switch( pOp->opcode ){
87621 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
87627 if( iA==-1 ) iA = 1;
87632 pOut->u.i = iB;
87640 switch( pOp->opcode ){
87642 case OP_Subtract: rB -= rA; break;
87654 if( iA==-1 ) iA = 1;
87660 pOut->u.i = rB;
87666 pOut->u.r = rB;
87681 ** be returned. This is used by the built-in min(), max() and nullif()
87690 ** publicly. Only built-in functions have access to this feature.
87693 assert( pOp->p4type==P4_COLLSEQ );
87694 if( pOp->p1 ){
87695 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
87703 ** Take the bit-wise AND of the values in register P1 and P2 and
87710 ** Take the bit-wise OR of the values in register P1 and P2 and
87739 pIn1 = &aMem[pOp->p1];
87740 pIn2 = &aMem[pOp->p2];
87741 pOut = &aMem[pOp->p3];
87742 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
87748 op = pOp->opcode;
87759 op = 2*OP_ShiftLeft + 1 - op;
87760 iB = iB>(-64) ? -iB : 64;
87764 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
87771 /* Sign-extend on a right shift of a negative number */
87772 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
87777 pOut->u.i = iA;
87791 pIn1 = &aMem[pOp->p1];
87794 pIn1->u.i += pOp->p2;
87806 pIn1 = &aMem[pOp->p1];
87807 if( (pIn1->flags & MEM_Int)==0 ){
87809 if( (pIn1->flags & MEM_Int)==0 ){
87811 if( pOp->p2==0 ){
87835 pIn1 = &aMem[pOp->p1];
87836 if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
87837 testcase( pIn1->flags & MEM_Int );
87838 testcase( pIn1->flags & MEM_IntReal );
87840 REGISTER_TRACE(pOp->p1, pIn1);
87863 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
87864 testcase( pOp->p2==SQLITE_AFF_TEXT );
87865 testcase( pOp->p2==SQLITE_AFF_BLOB );
87866 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
87867 testcase( pOp->p2==SQLITE_AFF_INTEGER );
87868 testcase( pOp->p2==SQLITE_AFF_REAL );
87869 pIn1 = &aMem[pOp->p1];
87873 rc = sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
87876 REGISTER_TRACE(pOp->p1, pIn1);
87888 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87938 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
87985 u16 flags1; /* Copy of initial value of pIn1->flags */
87986 u16 flags3; /* Copy of initial value of pIn3->flags */
87988 pIn1 = &aMem[pOp->p1];
87989 pIn3 = &aMem[pOp->p3];
87990 flags1 = pIn1->flags;
87991 flags3 = pIn3->flags;
87994 if( pOp->p5 & SQLITE_NULLEQ ){
88000 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 || CORRUPT_DB );
88001 testcase( (pOp->p5 & SQLITE_JUMPIFNULL)!=0 );
88007 res = ((flags3 & MEM_Null) ? -1 : +1); /* Operands are not equal */
88014 if( pOp->p5 & SQLITE_STOREP2 ){
88015 pOut = &aMem[pOp->p2];
88019 REGISTER_TRACE(pOp->p2, pOut);
88022 if( pOp->p5 & SQLITE_JUMPIFNULL ){
88030 affinity = pOp->p5 & SQLITE_AFF_MASK;
88035 testcase( flags3==pIn3->flags );
88036 flags3 = pIn3->flags;
88044 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
88045 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
88046 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
88052 testcase( pIn1->flags & MEM_Int );
88053 testcase( pIn1->flags & MEM_Real );
88054 testcase( pIn1->flags & MEM_IntReal );
88056 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
88057 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
88061 testcase( pIn3->flags & MEM_Int );
88062 testcase( pIn3->flags & MEM_Real );
88063 testcase( pIn3->flags & MEM_IntReal );
88065 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
88066 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
88069 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
88070 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
88083 res2 = aLTb[pOp->opcode - OP_Ne];
88086 res2 = aEQb[pOp->opcode - OP_Ne];
88089 res2 = aGTb[pOp->opcode - OP_Ne];
88093 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
88094 pIn3->flags = flags3;
88095 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
88096 pIn1->flags = flags1;
88098 if( pOp->p5 & SQLITE_STOREP2 ){
88099 pOut = &aMem[pOp->p2];
88101 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
88109 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
88111 testcase( res2==0 && pOp->opcode==OP_Eq );
88112 testcase( res2==1 && pOp->opcode==OP_Eq );
88113 testcase( res2==0 && pOp->opcode==OP_Ne );
88114 testcase( res2==1 && pOp->opcode==OP_Ne );
88115 if( (pOp->opcode==OP_Eq)==res2 ) break;
88119 pOut->u.i = res2;
88120 REGISTER_TRACE(pOp->p2, pOut);
88122 VdbeBranchTaken(res2!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
88146 /* Verify the preconditions of this opcode - that it follows an OP_Lt or
88150 for(iAddr = (int)(pOp - aOp) - 1; ALWAYS(iAddr>=0); iAddr--){
88176 assert( pOp->p4type==P4_INTARRAY );
88177 assert( pOp->p4.ai );
88184 ** Synopsis: r[P1@P3] <-> r[P2@P3]
88186 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
88187 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
88214 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
88218 assert( pOp[-1].opcode==OP_Permutation );
88219 assert( pOp[-1].p4type==P4_INTARRAY );
88220 aPermute = pOp[-1].p4.ai + 1;
88223 n = pOp->p3;
88224 pKeyInfo = pOp->p4.pKeyInfo;
88227 p1 = pOp->p1;
88228 p2 = pOp->p2;
88233 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
88234 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
88236 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
88237 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
88246 assert( i<pKeyInfo->nKeyField );
88247 pColl = pKeyInfo->aColl[i];
88248 bRev = (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC);
88251 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL)
88254 iCompare = -iCompare;
88256 if( bRev ) iCompare = -iCompare;
88271 VdbeBranchTaken(0,4); pOp = &aOp[pOp->p1 - 1];
88273 VdbeBranchTaken(1,4); pOp = &aOp[pOp->p2 - 1];
88275 VdbeBranchTaken(2,4); pOp = &aOp[pOp->p3 - 1];
88305 v1 = sqlite3VdbeBooleanValue(&aMem[pOp->p1], 2);
88306 v2 = sqlite3VdbeBooleanValue(&aMem[pOp->p2], 2);
88307 if( pOp->opcode==OP_And ){
88314 pOut = &aMem[pOp->p3];
88318 pOut->u.i = v1;
88345 assert( pOp->p4type==P4_INT32 );
88346 assert( pOp->p4.i==0 || pOp->p4.i==1 );
88347 assert( pOp->p3==0 || pOp->p3==1 );
88348 sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
88349 sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
88361 pIn1 = &aMem[pOp->p1];
88362 pOut = &aMem[pOp->p2];
88363 if( (pIn1->flags & MEM_Null)==0 ){
88375 ** ones-complement of the P1 value into register P2. If P1 holds
88379 pIn1 = &aMem[pOp->p1];
88380 pOut = &aMem[pOp->p2];
88382 if( (pIn1->flags & MEM_Null)==0 ){
88383 pOut->flags = MEM_Int;
88384 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
88392 ** encountered on each invocation of the byte-code program. Jump to P2
88395 ** Top-level programs determine first invocation by comparing the P1
88403 ** because the self-altering code trick does not work for recursive
88408 assert( p->aOp[0].opcode==OP_Init );
88409 if( p->pFrame ){
88410 iAddr = (int)(pOp - p->aOp);
88411 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
88415 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
88417 if( p->aOp[0].p1==pOp->p1 ){
88423 pOp->p1 = p->aOp[0].p1;
88430 ** is considered true if it is numeric and non-zero. If the value
88431 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
88435 c = sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3);
88445 ** in P1 is NULL then take the jump if and only if P3 is non-zero.
88449 c = !sqlite3VdbeBooleanValue(&aMem[pOp->p1], !pOp->p3);
88461 pIn1 = &aMem[pOp->p1];
88462 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
88463 if( (pIn1->flags & MEM_Null)!=0 ){
88475 pIn1 = &aMem[pOp->p1];
88476 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
88477 if( (pIn1->flags & MEM_Null)==0 ){
88492 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88493 assert( p->apCsr[pOp->p1]!=0 );
88494 if( p->apCsr[pOp->p1]->nullRow ){
88495 sqlite3VdbeMemSetNull(aMem + pOp->p3);
88515 ** -DSQLITE_ENABLE_OFFSET_SQL_FUNC option.
88519 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88520 pC = p->apCsr[pOp->p1];
88521 pOut = &p->aMem[pOp->p3];
88522 if( NEVER(pC==0) || pC->eCurType!=CURTYPE_BTREE ){
88525 sqlite3VdbeMemSetInt64(pOut, sqlite3BtreeOffset(pC->uc.pCursor));
88536 ** information about the format of the data.) Extract the P2-th column
88555 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
88563 u64 offset64; /* 64-bit offset */
88567 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
88568 pC = p->apCsr[pOp->p1];
88570 p2 = (u32)pOp->p2;
88572 /* If the cursor cache is stale (meaning it is not currently point at
88573 ** the correct row) then bring it up-to-date by doing the necessary
88574 ** B-Tree seek. */
88578 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
88579 pDest = &aMem[pOp->p3];
88582 assert( p2<(u32)pC->nField );
88583 aOffset = pC->aOffset;
88584 assert( pC->eCurType!=CURTYPE_VTAB );
88585 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
88586 assert( pC->eCurType!=CURTYPE_SORTER );
88588 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
88589 if( pC->nullRow ){
88590 if( pC->eCurType==CURTYPE_PSEUDO ){
88591 /* For the special case of as pseudo-cursor, the seekResult field
88593 assert( pC->seekResult>0 );
88594 pReg = &aMem[pC->seekResult];
88595 assert( pReg->flags & MEM_Blob );
88597 pC->payloadSize = pC->szRow = pReg->n;
88598 pC->aRow = (u8*)pReg->z;
88604 pCrsr = pC->uc.pCursor;
88605 assert( pC->eCurType==CURTYPE_BTREE );
88608 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
88609 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &pC->szRow);
88610 assert( pC->szRow<=pC->payloadSize );
88611 assert( pC->szRow<=65536 ); /* Maximum page size is 64KiB */
88612 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
88616 pC->cacheStatus = p->cacheCtr;
88617 pC->iHdrOffset = getVarint32(pC->aRow, aOffset[0]);
88618 pC->nHdrParsed = 0;
88621 if( pC->szRow<aOffset[0] ){ /*OPTIMIZATION-IF-FALSE*/
88622 /* pC->aRow does not have to hold the entire row, but it does at least
88623 ** need to cover the header of the record. If pC->aRow does not contain
88626 pC->aRow = 0;
88627 pC->szRow = 0;
88635 ** 3-byte type for each of the maximum of 32768 columns plus three
88638 if( aOffset[0] > 98307 || aOffset[0] > pC->payloadSize ){
88643 ** (ex: pC->nHdrParsed<=p2) in the next section, we achieve a
88655 zData = pC->aRow;
88656 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
88663 ** parsed and valid information is in aOffset[] and pC->aType[].
88665 if( pC->nHdrParsed<=p2 ){
88667 ** to extract additional fields up through the p2+1-th field
88669 if( pC->iHdrOffset<aOffset[0] ){
88671 if( pC->aRow==0 ){
88673 rc = sqlite3VdbeMemFromBtreeZeroOffset(pC->uc.pCursor,aOffset[0],&sMem);
88677 zData = pC->aRow;
88680 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
88682 i = pC->nHdrParsed;
88684 zHdr = zData + pC->iHdrOffset;
88688 if( (pC->aType[i] = t = zHdr[0])<0x80 ){
88693 pC->aType[i] = t;
88704 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
88705 || (offset64 > pC->payloadSize)
88711 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
88716 pC->nHdrParsed = i;
88717 pC->iHdrOffset = (u32)(zHdr - zData);
88718 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
88727 if( pC->nHdrParsed<=p2 ){
88728 if( pOp->p4type==P4_MEM ){
88729 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
88736 t = pC->aType[p2];
88739 /* Extract the content for the p2+1-th column. Control can only
88740 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
88743 assert( p2<pC->nHdrParsed );
88749 assert( t==pC->aType[p2] );
88750 if( pC->szRow>=aOffset[p2+1] ){
88752 ** page - where the content is not on an overflow page */
88753 zData = pC->aRow + aOffset[p2];
88758 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
88762 pDest->n = len = (t-12)/2;
88763 pDest->enc = encoding;
88764 if( pDest->szMalloc < len+2 ){
88765 pDest->flags = MEM_Null;
88768 pDest->z = pDest->zMalloc;
88770 memcpy(pDest->z, zData, len);
88771 pDest->z[len] = 0;
88772 pDest->z[len+1] = 0;
88773 pDest->flags = aFlag[t&1];
88776 pDest->enc = encoding;
88778 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
88779 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
88797 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
88799 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
88800 pDest->flags &= ~MEM_Ephem;
88806 REGISTER_TRACE(pOp->p3, pDest);
88811 pOp = &aOp[aOp[0].p3-1];
88824 ** P4 is a string that is P2 characters long. The N-th character of the
88825 ** string indicates the column affinity that should be used for the N-th
88831 zAffinity = pOp->p4.z;
88833 assert( pOp->p2>0 );
88834 assert( zAffinity[pOp->p2]==0 );
88835 pIn1 = &aMem[pOp->p1];
88836 while( 1 /*exit-by-break*/ ){
88837 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
88840 if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
88843 ** so that we keep the high-resolution integer value but know that
88845 testcase( pIn1->u.i==140737488355328LL );
88846 testcase( pIn1->u.i==140737488355327LL );
88847 testcase( pIn1->u.i==-140737488355328LL );
88848 testcase( pIn1->u.i==-140737488355329LL );
88849 if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
88850 pIn1->flags |= MEM_IntReal;
88851 pIn1->flags &= ~MEM_Int;
88853 pIn1->u.r = (double)pIn1->u.i;
88854 pIn1->flags |= MEM_Real;
88855 pIn1->flags &= ~MEM_Int;
88858 REGISTER_TRACE((int)(pIn1-aMem), pIn1);
88873 ** P4 may be a string that is P2 characters long. The N-th character of the
88874 ** string indicates the column affinity that should be used for the N-th
88883 ** compile-time option is enabled:
88886 ** of the right-most table that can be null-trimmed.
88890 ** accept no-change records with serial_type 10. This value is
88913 ** ------------------------------------------------------------------------
88914 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
88915 ** ------------------------------------------------------------------------
88922 ** hdr-size field is also a varint which is the offset from the beginning
88928 nField = pOp->p1;
88929 zAffinity = pOp->p4.z;
88930 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
88932 nField = pOp->p2;
88933 pLast = &pData0[nField-1];
88934 file_format = p->minWriteFileFormat;
88937 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
88938 pOut = &aMem[pOp->p3];
88948 if( zAffinity[0]==SQLITE_AFF_REAL && (pRec->flags & MEM_Int) ){
88949 pRec->flags |= MEM_IntReal;
88950 pRec->flags &= ~(MEM_Int);
88952 REGISTER_TRACE((int)(pRec-aMem), pRec);
88962 ** have a non-NULL default value. Also, the record must be left with
88964 ** index of the right-most column with a non-NULL default value */
88965 if( pOp->p5 ){
88966 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
88967 pLast--;
88968 nField--;
88975 ** the Mem.uTemp field of each term should hold the serial-type that will
88979 ** --------------- ---------------
88981 ** 1 1-byte signed integer
88982 ** 2 2-byte signed integer
88983 ** 3 3-byte signed integer
88984 ** 4 4-byte signed integer
88985 ** 5 6-byte signed integer
88986 ** 6 8-byte signed integer
89002 if( pRec->flags & MEM_Null ){
89003 if( pRec->flags & MEM_Zero ){
89007 ** Give such values a special internal-use-only serial-type of 10
89011 assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
89013 pRec->uTemp = 10;
89015 pRec->uTemp = 0;
89018 }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
89020 i64 i = pRec->u.i;
89022 testcase( pRec->flags & MEM_Int );
89023 testcase( pRec->flags & MEM_IntReal );
89037 pRec->uTemp = 8+(u32)uu;
89040 pRec->uTemp = 1;
89044 pRec->uTemp = 2;
89047 pRec->uTemp = 3;
89050 pRec->uTemp = 4;
89053 pRec->uTemp = 5;
89056 if( pRec->flags & MEM_IntReal ){
89058 ** as an integer, then we might as well make it an 8-byte floating
89060 pRec->u.r = (double)pRec->u.i;
89061 pRec->flags &= ~MEM_IntReal;
89062 pRec->flags |= MEM_Real;
89063 pRec->uTemp = 7;
89065 pRec->uTemp = 6;
89068 }else if( pRec->flags & MEM_Real ){
89071 pRec->uTemp = 7;
89073 assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
89074 assert( pRec->n>=0 );
89075 len = (u32)pRec->n;
89076 serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
89077 if( pRec->flags & MEM_Zero ){
89078 serial_type += pRec->u.nZero*2;
89081 len += pRec->u.nZero;
89083 nZero += pRec->u.nZero;
89088 pRec->uTemp = serial_type;
89091 pRec--;
89094 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
89112 ** the new record. The output register (pOp->p3) is not allowed to
89116 if( nByte+nZero<=pOut->szMalloc ){
89119 pOut->z = pOut->zMalloc;
89123 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
89130 pOut->n = (int)nByte;
89131 pOut->flags = MEM_Blob;
89133 pOut->u.nZero = nZero;
89134 pOut->flags |= MEM_Zero;
89137 zHdr = (u8 *)pOut->z;
89145 serial_type = pRec->uTemp;
89146 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
89149 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
89153 assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
89154 assert( nByte==(int)(zPayload - (u8*)pOut->z) );
89156 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
89157 REGISTER_TRACE(pOp->p3, pOut);
89168 ** every btree page of the table. But if P3 is non-zero, an estimate
89175 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
89176 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
89178 if( pOp->p3 ){
89186 pOut->u.i = nEntry;
89207 p1 = pOp->p1;
89208 zName = pOp->p4.z;
89213 assert( db->pSavepoint==0 || db->autoCommit==0 );
89215 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
89217 assert( p->bIsReader );
89220 if( db->nVdbeWrite>0 ){
89224 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
89233 ** that the db->aVTrans[] array is empty. */
89234 assert( db->autoCommit==0 || db->nVTrans==0 );
89236 db->nStatement+db->nSavepoint);
89243 pNew->zName = (char *)&pNew[1];
89244 memcpy(pNew->zName, zName, nName+1);
89248 if( db->autoCommit ){
89249 db->autoCommit = 0;
89250 db->isTransactionSavepoint = 1;
89252 db->nSavepoint++;
89256 pNew->pNext = db->pSavepoint;
89257 db->pSavepoint = pNew;
89258 pNew->nDeferredCons = db->nDeferredCons;
89259 pNew->nDeferredImmCons = db->nDeferredImmCons;
89269 pSavepoint = db->pSavepoint;
89270 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
89271 pSavepoint = pSavepoint->pNext
89278 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
89282 sqlite3VdbeError(p, "cannot release savepoint - "
89291 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
89296 db->autoCommit = 1;
89298 p->pc = (int)(pOp - aOp);
89299 db->autoCommit = 0;
89300 p->rc = rc = SQLITE_BUSY;
89303 rc = p->rc;
89305 db->autoCommit = 0;
89307 db->isTransactionSavepoint = 0;
89311 iSavepoint = db->nSavepoint - iSavepoint - 1;
89313 isSchemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0;
89314 for(ii=0; ii<db->nDb; ii++){
89315 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
89324 for(ii=0; ii<db->nDb; ii++){
89325 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
89333 db->mDbFlags |= DBFLAG_SchemaChange;
89340 while( db->pSavepoint!=pSavepoint ){
89341 pTmp = db->pSavepoint;
89342 db->pSavepoint = pTmp->pNext;
89344 db->nSavepoint--;
89352 assert( pSavepoint==db->pSavepoint );
89353 db->pSavepoint = pSavepoint->pNext;
89356 db->nSavepoint--;
89360 db->nDeferredCons = pSavepoint->nDeferredCons;
89361 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
89377 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
89388 desiredAutoCommit = pOp->p1;
89389 iRollback = pOp->p2;
89392 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
89393 assert( p->bIsReader );
89395 if( desiredAutoCommit!=db->autoCommit ){
89399 db->autoCommit = 1;
89400 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
89404 sqlite3VdbeError(p, "cannot commit transaction - "
89411 db->autoCommit = (u8)desiredAutoCommit;
89414 p->pc = (int)(pOp - aOp);
89415 db->autoCommit = (u8)(1-desiredAutoCommit);
89416 p->rc = rc = SQLITE_BUSY;
89420 if( p->rc==SQLITE_OK ){
89429 (iRollback)?"cannot rollback - no transaction is active":
89430 "cannot commit - no transaction is active"));
89442 ** If P2 is non-zero, then a write-transaction is started, or if a
89443 ** read-transaction is already active, it is upgraded to a write-transaction.
89444 ** If P2 is zero, then a read-transaction is started. If P2 is 2 or more
89452 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
89477 assert( p->bIsReader );
89478 assert( p->readOnly==0 || pOp->p2==0 );
89479 assert( pOp->p2>=0 && pOp->p2<=2 );
89480 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89481 assert( DbMaskTest(p->btreeMask, pOp->p1) );
89482 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
89486 pBt = db->aDb[pOp->p1].pBt;
89489 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2, &iMeta);
89494 p->pc = (int)(pOp - aOp);
89495 p->rc = rc;
89501 if( p->usesStmtJournal
89502 && pOp->p2
89503 && (db->autoCommit==0 || db->nVdbeRead>1)
89506 if( p->iStatement==0 ){
89507 assert( db->nStatement>=0 && db->nSavepoint>=0 );
89508 db->nStatement++;
89509 p->iStatement = db->nSavepoint + db->nStatement;
89512 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
89514 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
89520 p->nStmtDefCons = db->nDeferredCons;
89521 p->nStmtDefImmCons = db->nDeferredImmCons;
89524 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
89525 if( pOp->p5
89526 && (iMeta!=pOp->p3
89527 || db->aDb[pOp->p1].pSchema->iGeneration!=pOp->p4.i)
89530 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
89534 sqlite3DbFree(db, p->zErrMsg);
89535 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
89536 /* If the schema-cookie from the database file matches the cookie
89537 ** stored with the in-memory representation of the schema, do
89540 ** If virtual-tables are in use, this is not just an optimization.
89541 ** Often, v-tables store their data in other SQLite tables, which
89542 ** are queried from within xNext() and other v-table methods using
89543 ** prepared queries. If such a query is out-of-date, we do not want to
89545 ** v-table would have to be ready for the sqlite3_vtab structure itself
89547 ** a v-table method.
89549 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
89550 sqlite3ResetOneSchema(db, pOp->p1);
89552 p->expired = 1;
89567 ** There must be a read-lock on the database (either a transaction
89576 assert( p->bIsReader );
89577 iDb = pOp->p1;
89578 iCookie = pOp->p3;
89579 assert( pOp->p3<SQLITE_N_BTREE_META );
89580 assert( iDb>=0 && iDb<db->nDb );
89581 assert( db->aDb[iDb].pBt!=0 );
89582 assert( DbMaskTest(p->btreeMask, iDb) );
89584 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
89586 pOut->u.i = iMeta;
89601 ** schema version is set to P3-P5. The "PRAGMA schema_version=N" statement
89609 assert( pOp->p2<SQLITE_N_BTREE_META );
89610 assert( pOp->p1>=0 && pOp->p1<db->nDb );
89611 assert( DbMaskTest(p->btreeMask, pOp->p1) );
89612 assert( p->readOnly==0 );
89613 pDb = &db->aDb[pOp->p1];
89614 assert( pDb->pBt!=0 );
89615 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
89617 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
89618 if( pOp->p2==BTREE_SCHEMA_VERSION ){
89620 pDb->pSchema->schema_cookie = pOp->p3 - pOp->p5;
89621 db->mDbFlags |= DBFLAG_SchemaChange;
89622 }else if( pOp->p2==BTREE_FILE_FORMAT ){
89624 pDb->pSchema->file_format = pOp->p3;
89626 if( pOp->p1==1 ){
89630 p->expired = 0;
89639 ** Open a read-only cursor for the database table whose root page is
89656 ** object, then table being opened must be an [index b-tree] where the
89658 ** sequence of that index b-tree. Otherwise, if P4 is an integer
89659 ** value, then the table being opened must be a [table b-tree] with a
89669 ** b-tree and if it is this opcode becomes a no-op. In other words,
89691 ** OPFLAG_P2ISREG bit is set in P5 - see below).
89695 ** object, then table being opened must be an [index b-tree] where the
89697 ** sequence of that index b-tree. Otherwise, if P4 is an integer
89698 ** value, then the table being opened must be a [table b-tree] with a
89730 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
89731 assert( pOp->p4type==P4_KEYINFO );
89732 pCur = p->apCsr[pOp->p1];
89733 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
89734 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
89742 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
89743 assert( p->bIsReader );
89744 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
89745 || p->readOnly==0 );
89747 if( p->expired==1 ){
89754 p2 = (u32)pOp->p2;
89755 iDb = pOp->p3;
89756 assert( iDb>=0 && iDb<db->nDb );
89757 assert( DbMaskTest(p->btreeMask, iDb) );
89758 pDb = &db->aDb[iDb];
89759 pX = pDb->pBt;
89761 if( pOp->opcode==OP_OpenWrite ){
89763 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
89765 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
89766 p->minWriteFileFormat = pDb->pSchema->file_format;
89771 if( pOp->p5 & OPFLAG_P2ISREG ){
89773 assert( p2<=(u32)(p->nMem+1 - p->nCursor) );
89774 assert( pOp->opcode==OP_OpenWrite );
89777 assert( (pIn2->flags & MEM_Int)!=0 );
89779 p2 = (int)pIn2->u.i;
89786 if( pOp->p4type==P4_KEYINFO ){
89787 pKeyInfo = pOp->p4.pKeyInfo;
89788 assert( pKeyInfo->enc==ENC(db) );
89789 assert( pKeyInfo->db==db );
89790 nField = pKeyInfo->nAllField;
89791 }else if( pOp->p4type==P4_INT32 ){
89792 nField = pOp->p4.i;
89794 assert( pOp->p1>=0 );
89797 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
89799 pCur->nullRow = 1;
89800 pCur->isOrdered = 1;
89801 pCur->pgnoRoot = p2;
89803 pCur->wrFlag = wrFlag;
89805 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
89806 pCur->pKeyInfo = pKeyInfo;
89808 ** SQLite used to check if the root-page flags were sane at this point
89811 pCur->isTable = pOp->p4type!=P4_KEYINFO;
89816 testcase( pOp->p5 & OPFLAG_BULKCSR );
89817 testcase( pOp->p2 & OPFLAG_SEEKEQ );
89818 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
89819 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
89830 ** Duplicate ephemeral cursors are used for self-joins of materialized views.
89836 pOrig = p->apCsr[pOp->p2];
89838 assert( pOrig->isEphemeral ); /* Only ephemeral cursors can be duplicated */
89840 pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE);
89842 pCx->nullRow = 1;
89843 pCx->isEphemeral = 1;
89844 pCx->pKeyInfo = pOrig->pKeyInfo;
89845 pCx->isTable = pOrig->isTable;
89846 pCx->pgnoRoot = pOrig->pgnoRoot;
89847 pCx->isOrdered = pOrig->isOrdered;
89848 pCx->pBtx = pOrig->pBtx;
89849 pCx->hasBeenDuped = 1;
89850 pOrig->hasBeenDuped = 1;
89851 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89852 pCx->pKeyInfo, pCx->uc.pCursor);
89866 ** the main database is read-only. The ephemeral
89883 ** can be used as zero-length data for OP_Insert. This is an optimization
89905 assert( pOp->p1>=0 );
89906 assert( pOp->p2>=0 );
89907 if( pOp->p3>0 ){
89910 assert( pOp->p2==0 ); /* Only used when number of columns is zero */
89911 assert( pOp->opcode==OP_OpenEphemeral );
89912 assert( aMem[pOp->p3].flags & MEM_Null );
89913 aMem[pOp->p3].n = 0;
89914 aMem[pOp->p3].z = "";
89916 pCx = p->apCsr[pOp->p1];
89917 if( pCx && !pCx->hasBeenDuped ){
89921 assert( pCx->isEphemeral );
89922 pCx->seqCount = 0;
89923 pCx->cacheStatus = CACHE_STALE;
89924 rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
89926 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
89928 pCx->isEphemeral = 1;
89929 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
89930 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
89933 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1, 0);
89938 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
89940 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
89941 assert( pOp->p4type==P4_KEYINFO );
89942 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pCx->pgnoRoot,
89943 BTREE_BLOBKEY | pOp->p5);
89945 assert( pCx->pgnoRoot==SCHEMA_ROOT+1 );
89946 assert( pKeyInfo->db==db );
89947 assert( pKeyInfo->enc==ENC(db) );
89948 rc = sqlite3BtreeCursor(pCx->pBtx, pCx->pgnoRoot, BTREE_WRCSR,
89949 pKeyInfo, pCx->uc.pCursor);
89951 pCx->isTable = 0;
89953 pCx->pgnoRoot = SCHEMA_ROOT;
89954 rc = sqlite3BtreeCursor(pCx->pBtx, SCHEMA_ROOT, BTREE_WRCSR,
89955 0, pCx->uc.pCursor);
89956 pCx->isTable = 1;
89959 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
89961 sqlite3BtreeClose(pCx->pBtx);
89966 pCx->nullRow = 1;
89974 ** tables using an external merge-sort algorithm.
89976 ** If argument P3 is non-zero, then it indicates that the sorter may
89983 assert( pOp->p1>=0 );
89984 assert( pOp->p2>=0 );
89985 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
89987 pCx->pKeyInfo = pOp->p4.pKeyInfo;
89988 assert( pCx->pKeyInfo->db==db );
89989 assert( pCx->pKeyInfo->enc==ENC(db) );
89990 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
90004 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90005 pC = p->apCsr[pOp->p1];
90007 if( (pC->seqCount++)==0 ){
90021 ** A pseudo-table created by this opcode is used to hold a single
90024 ** is the only cursor opcode that works with a pseudo-table.
90027 ** the pseudo-table.
90032 assert( pOp->p1>=0 );
90033 assert( pOp->p3>=0 );
90034 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
90036 pCx->nullRow = 1;
90037 pCx->seekResult = pOp->p2;
90038 pCx->isTable = 1;
90039 /* Give this pseudo-cursor a fake BtCursor pointer so that pCx
90041 ** for pCx->eCurType==CURTYPE_BTREE inside of sqlite3VdbeCursorMoveto()
90043 pCx->uc.pCursor = sqlite3BtreeFakeValidCursor();
90044 assert( pOp->p5==0 );
90051 ** currently open, this instruction is a no-op.
90054 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90055 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
90056 p->apCsr[pOp->p1] = 0;
90065 ** table or index for cursor P1 are used. P4 is a 64-bit integer
90068 ** by the cursor. The high-order bit is set if any column after
90073 pC = p->apCsr[pOp->p1];
90074 assert( pC->eCurType==CURTYPE_BTREE );
90075 pC->maskUsed = *(u64*)pOp->p4.pI64;
90083 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
90110 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
90128 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
90146 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
90182 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90183 assert( pOp->p2!=0 );
90184 pC = p->apCsr[pOp->p1];
90186 assert( pC->eCurType==CURTYPE_BTREE );
90190 assert( pC->isOrdered );
90191 assert( pC->uc.pCursor!=0 );
90192 oc = pOp->opcode;
90194 pC->nullRow = 0;
90196 pC->seekOp = pOp->opcode;
90199 pC->deferredMoveto = 0;
90200 pC->cacheStatus = CACHE_STALE;
90201 if( pC->isTable ){
90204 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
90210 pIn3 = &aMem[pOp->p3];
90211 flags3 = pIn3->flags;
90216 newType = pIn3->flags; /* Record the type after applying numeric affinity */
90217 pIn3->flags = flags3; /* But convert the type back to its original */
90227 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
90237 ** (x > 4.9) -> (x >= 5)
90238 ** (x <= 4.9) -> (x < 5)
90240 if( pIn3->u.r<(double)iKey ){
90241 assert( OP_SeekGE==(OP_SeekGT-1) );
90242 assert( OP_SeekLT==(OP_SeekLE-1) );
90244 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
90249 else if( pIn3->u.r>(double)iKey ){
90256 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
90257 pC->movetoTarget = iKey; /* Used by OP_Delete */
90267 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
90269 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
90271 assert( pOp->opcode==OP_SeekGE || pOp[1].opcode==OP_IdxLT );
90272 assert( pOp->opcode==OP_SeekLE || pOp[1].opcode==OP_IdxGT );
90279 nField = pOp->p4.i;
90280 assert( pOp->p4type==P4_INT32 );
90282 r.pKeyInfo = pC->pKeyInfo;
90287 ** r.default_rc = -1;
90292 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
90293 assert( oc!=OP_SeekGT || r.default_rc==-1 );
90294 assert( oc!=OP_SeekLE || r.default_rc==-1 );
90298 r.aMem = &aMem[pOp->p3];
90303 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
90318 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
90334 rc = sqlite3BtreePrevious(pC->uc.pCursor, 0);
90347 res = sqlite3BtreeEof(pC->uc.pCursor);
90351 assert( pOp->p2>0 );
90364 ** Synopsis: Scan-ahead up to P1 rows
90376 ** This opcode helps to optimize IN operators on a multi-column index
90379 ** of the b-tree instead. A correct answer is obtained if this opcode
90380 ** is omitted or is a no-op.
90387 ** then this opcode is a no-op and control passes through into the OP_SeekGE.
90417 /* pOp->p2 points to the first instruction past the OP_IdxGT that
90419 assert( pOp->p2>=(int)(pOp-aOp)+2 );
90420 assert( aOp[pOp->p2-1].opcode==OP_IdxGT || aOp[pOp->p2-1].opcode==OP_IdxGE );
90421 testcase( aOp[pOp->p2-1].opcode==OP_IdxGE );
90422 assert( pOp[1].p1==aOp[pOp->p2-1].p1 );
90423 assert( pOp[1].p2==aOp[pOp->p2-1].p2 );
90424 assert( pOp[1].p3==aOp[pOp->p2-1].p3 );
90426 assert( pOp->p1>0 );
90427 pC = p->apCsr[pOp[1].p1];
90429 assert( pC->eCurType==CURTYPE_BTREE );
90430 assert( !pC->isTable );
90431 if( !sqlite3BtreeCursorIsValidNN(pC->uc.pCursor) ){
90433 if( db->flags&SQLITE_VdbeTrace ){
90434 printf("... cursor not valid - fall through\n");
90439 nStep = pOp->p1;
90441 r.pKeyInfo = pC->pKeyInfo;
90461 if( db->flags&SQLITE_VdbeTrace ){
90462 printf("... %d steps and then skip\n", pOp->p1 - nStep);
90471 if( db->flags&SQLITE_VdbeTrace ){
90472 printf("... %d steps and then success\n", pOp->p1 - nStep);
90481 if( db->flags&SQLITE_VdbeTrace ){
90482 printf("... fall through after %d steps\n", pOp->p1);
90488 nStep--;
90489 rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
90514 ** early, thus saving work. This is part of the IN-early-out optimization.
90516 ** P1 must be a valid b-tree cursor.
90520 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90521 pC = p->apCsr[pOp->p1];
90523 assert( pOp->p3>=pOp->p2 );
90524 if( pC->seekHit<pOp->p2 ){
90525 pC->seekHit = pOp->p2;
90526 }else if( pC->seekHit>pOp->p3 ){
90527 pC->seekHit = pOp->p3;
90538 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90539 VdbeBranchTaken(p->apCsr[pOp->p1]==0, 2);
90540 if( !p->apCsr[pOp->p1] ){
90601 ** This opcode is used in IN clause processing for a multi-column key.
90603 ** left-most element, and if there are no matches on the most recent
90622 ** record are not-NULL then a check is done to determine if any row in the
90638 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90639 pC = p->apCsr[pOp->p1];
90641 if( pC->seekHit>=pOp->p4.i ) break;
90658 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
90661 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90662 assert( pOp->p4type==P4_INT32 );
90663 pC = p->apCsr[pOp->p1];
90666 pC->seekOp = pOp->opcode;
90668 pIn3 = &aMem[pOp->p3];
90669 assert( pC->eCurType==CURTYPE_BTREE );
90670 assert( pC->uc.pCursor!=0 );
90671 assert( pC->isTable==0 );
90672 if( pOp->p4.i>0 ){
90673 r.pKeyInfo = pC->pKeyInfo;
90674 r.nField = (u16)pOp->p4.i;
90680 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
90686 assert( pIn3->flags & MEM_Blob );
90690 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
90692 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
90694 pIdxKey->default_rc = 0;
90696 if( pOp->opcode==OP_NoConflict ){
90700 for(ii=0; ii<pIdxKey->nField; ii++){
90701 if( pIdxKey->aMem[ii].flags & MEM_Null ){
90707 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
90712 pC->seekResult = res;
90714 pC->nullRow = 1-alreadyExists;
90715 pC->deferredMoveto = 0;
90716 pC->cacheStatus = CACHE_STALE;
90717 if( pOp->opcode==OP_Found ){
90723 if( pOp->opcode==OP_IfNoHope ) pC->seekHit = pOp->p4.i;
90744 ** (with arbitrary multi-value keys).
90763 ** P3 register to contain a non-integer value, in which case the jump is
90767 ** (with arbitrary multi-value keys).
90781 pIn3 = &aMem[pOp->p3];
90782 testcase( pIn3->flags & MEM_Int );
90783 testcase( pIn3->flags & MEM_IntReal );
90784 testcase( pIn3->flags & MEM_Real );
90785 testcase( (pIn3->flags & (MEM_Str|MEM_Int))==MEM_Str );
90786 if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
90787 /* If pIn3->u.i does not contain an integer, compute iKey as the
90801 pIn3 = &aMem[pOp->p3];
90802 assert( (pIn3->flags & MEM_Int)!=0 || pOp->opcode==OP_SeekRowid );
90803 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90804 iKey = pIn3->u.i;
90806 pC = p->apCsr[pOp->p1];
90809 if( pOp->opcode==OP_SeekRowid ) pC->seekOp = OP_SeekRowid;
90811 assert( pC->isTable );
90812 assert( pC->eCurType==CURTYPE_BTREE );
90813 pCrsr = pC->uc.pCursor;
90818 pC->movetoTarget = iKey; /* Used by OP_Delete */
90819 pC->nullRow = 0;
90820 pC->cacheStatus = CACHE_STALE;
90821 pC->deferredMoveto = 0;
90823 pC->seekResult = res;
90826 if( pOp->p2==0 ){
90845 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90846 assert( p->apCsr[pOp->p1]!=0 );
90847 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
90849 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
90882 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
90883 pC = p->apCsr[pOp->p1];
90885 assert( pC->isTable );
90886 assert( pC->eCurType==CURTYPE_BTREE );
90887 assert( pC->uc.pCursor!=0 );
90890 ** thing) is obtained in a two-step algorithm.
90902 assert( pC->isTable );
90914 if( !pC->useRandomRowid ){
90915 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
90920 v = 1; /* IMP: R-61914-48074 */
90922 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
90923 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
90925 pC->useRandomRowid = 1;
90927 v++; /* IMP: R-29538-34987 */
90933 if( pOp->p3 ){
90935 assert( pOp->p3>0 );
90936 if( p->pFrame ){
90937 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
90939 assert( pOp->p3<=pFrame->nMem );
90940 pMem = &pFrame->aMem[pOp->p3];
90943 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
90944 pMem = &aMem[pOp->p3];
90949 REGISTER_TRACE(pOp->p3, pMem);
90951 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
90952 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
90953 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
90956 if( v<pMem->u.i+1 ){
90957 v = pMem->u.i + 1;
90959 pMem->u.i = v;
90962 if( pC->useRandomRowid ){
90963 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
90967 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
90973 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
90979 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
90982 assert( v>0 ); /* EV: R-40812-03570 */
90984 pC->deferredMoveto = 0;
90985 pC->cacheStatus = CACHE_STALE;
90987 pOut->u.i = v;
91016 ** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
91019 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
91020 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
91033 const char *zDb; /* database name - used by the update hook */
91034 Table *pTab; /* Table structure - used by update and pre-update hooks */
91037 pData = &aMem[pOp->p2];
91038 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91040 pC = p->apCsr[pOp->p1];
91042 assert( pC->eCurType==CURTYPE_BTREE );
91043 assert( pC->deferredMoveto==0 );
91044 assert( pC->uc.pCursor!=0 );
91045 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
91046 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
91047 REGISTER_TRACE(pOp->p2, pData);
91050 pKey = &aMem[pOp->p3];
91051 assert( pKey->flags & MEM_Int );
91053 REGISTER_TRACE(pOp->p3, pKey);
91054 x.nKey = pKey->u.i;
91056 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
91057 assert( pC->iDb>=0 );
91058 zDb = db->aDb[pC->iDb].zDbSName;
91059 pTab = pOp->p4.pTab;
91060 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
91067 /* Invoke the pre-update hook, if any */
91069 if( db->xPreUpdateCallback && !(pOp->p5 & OPFLAG_ISUPDATE) ){
91070 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey,pOp->p2);
91072 if( db->xUpdateCallback==0 || pTab->aCol==0 ){
91073 /* Prevent post-update hook from running in cases when it should not */
91077 if( pOp->p5 & OPFLAG_ISNOOP ) break;
91080 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
91081 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
91082 assert( (pData->flags & (MEM_Blob|MEM_Str))!=0 || pData->n==0 );
91083 x.pData = pData->z;
91084 x.nData = pData->n;
91085 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
91086 if( pData->flags & MEM_Zero ){
91087 x.nZero = pData->u.nZero;
91092 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
91093 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
91096 pC->deferredMoveto = 0;
91097 pC->cacheStatus = CACHE_STALE;
91099 /* Invoke the update-hook if required. */
91102 assert( db->xUpdateCallback!=0 );
91103 assert( pTab->aCol!=0 );
91104 db->xUpdateCallback(db->pUpdateArg,
91105 (pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT,
91106 zDb, pTab->zName, x.nKey);
91114 ** of table - intkey or index. This opcode is used as part of copying
91127 assert( pOp[1].opcode==OP_Insert || pOp->p3==0 );
91128 assert( pOp[1].opcode==OP_IdxInsert || pOp->p3>0 );
91130 pDest = p->apCsr[pOp->p1];
91131 pSrc = p->apCsr[pOp->p2];
91132 iKey = pOp->p3 ? aMem[pOp->p3].u.i : 0;
91133 rc = sqlite3BtreeTransferRow(pDest->uc.pCursor, pSrc->uc.pCursor, iKey);
91145 ** the next Next instruction will be a no-op. As a result, in this case
91159 ** P1 must not be pseudo-table. It has to be a real table with
91163 ** the update or pre-update hook, or both, may be invoked. The P1 cursor must
91165 ** this case. Specifically, if one is configured, the pre-update hook is
91166 ** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
91179 opflags = pOp->p2;
91180 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91181 pC = p->apCsr[pOp->p1];
91183 assert( pC->eCurType==CURTYPE_BTREE );
91184 assert( pC->uc.pCursor!=0 );
91185 assert( pC->deferredMoveto==0 );
91189 if( pOp->p4type==P4_TABLE
91190 && HasRowid(pOp->p4.pTab)
91191 && pOp->p5==0
91192 && sqlite3BtreeCursorIsValidNN(pC->uc.pCursor)
91195 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
91197 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
91198 assert( CORRUPT_DB || pC->movetoTarget==iKey );
91202 /* If the update-hook or pre-update-hook will be invoked, set zDb to
91207 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
91208 assert( pC->iDb>=0 );
91209 assert( pOp->p4.pTab!=0 );
91210 zDb = db->aDb[pC->iDb].zDbSName;
91211 pTab = pOp->p4.pTab;
91212 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
91213 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
91221 /* Invoke the pre-update-hook if required. */
91222 if( db->xPreUpdateCallback && pOp->p4.pTab ){
91225 || (aMem[pOp->p3].flags & MEM_Int)
91229 zDb, pTab, pC->movetoTarget,
91230 pOp->p3
91237 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
91242 if( p->pFrame==0 ){
91243 if( pC->isEphemeral==0
91244 && (pOp->p5 & OPFLAG_AUXDELETE)==0
91245 && (pC->wrFlag & OPFLAG_FORDELETE)==0
91249 if( pOp->p2 & OPFLAG_NCHANGE ){
91250 nExtraDelete--;
91255 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
91256 pC->cacheStatus = CACHE_STALE;
91257 pC->seekResult = 0;
91260 /* Invoke the update-hook if required. */
91262 p->nChange++;
91263 if( db->xUpdateCallback && HasRowid(pTab) ){
91264 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
91265 pC->movetoTarget);
91266 assert( pC->iDb>=0 );
91280 sqlite3VdbeSetChanges(db, p->nChange);
91281 p->nChange = 0;
91305 pC = p->apCsr[pOp->p1];
91307 assert( pOp->p4type==P4_INT32 );
91308 pIn3 = &aMem[pOp->p3];
91309 nKeyCol = pOp->p4.i;
91325 ** a register that is the source for a pseudo-table cursor created using
91326 ** OpenPseudo. That pseudo-table cursor is the one that is identified by
91333 pOut = &aMem[pOp->p2];
91334 pC = p->apCsr[pOp->p1];
91337 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
91338 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91340 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
91357 ** of a real table, not a pseudo-table.
91361 ** register will be invalidated as soon as the cursor moves - including
91379 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91380 pC = p->apCsr[pOp->p1];
91382 assert( pC->eCurType==CURTYPE_BTREE );
91384 assert( pC->nullRow==0 );
91385 assert( pC->uc.pCursor!=0 );
91386 pCrsr = pC->uc.pCursor;
91396 assert( pC->deferredMoveto==0 );
91400 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
91406 if( !pOp->p3 ) Deephemeralize(pOut);
91408 REGISTER_TRACE(pOp->p2, pOut);
91429 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91430 pC = p->apCsr[pOp->p1];
91432 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
91433 if( pC->nullRow ){
91434 pOut->flags = MEM_Null;
91436 }else if( pC->deferredMoveto ){
91437 v = pC->movetoTarget;
91439 }else if( pC->eCurType==CURTYPE_VTAB ){
91440 assert( pC->uc.pVCur!=0 );
91441 pVtab = pC->uc.pVCur->pVtab;
91442 pModule = pVtab->pModule;
91443 assert( pModule->xRowid );
91444 rc = pModule->xRowid(pC->uc.pVCur, &v);
91449 assert( pC->eCurType==CURTYPE_BTREE );
91450 assert( pC->uc.pCursor!=0 );
91453 if( pC->nullRow ){
91454 pOut->flags = MEM_Null;
91457 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
91459 pOut->u.i = v;
91472 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91473 pC = p->apCsr[pOp->p1];
91475 pC->nullRow = 1;
91476 pC->cacheStatus = CACHE_STALE;
91477 if( pC->eCurType==CURTYPE_BTREE ){
91478 assert( pC->uc.pCursor!=0 );
91479 sqlite3BtreeClearCursor(pC->uc.pCursor);
91482 if( pC->seekOp==0 ) pC->seekOp = OP_NullRow;
91515 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91516 pC = p->apCsr[pOp->p1];
91518 assert( pC->eCurType==CURTYPE_BTREE );
91519 pCrsr = pC->uc.pCursor;
91523 pC->seekOp = pOp->opcode;
91525 if( pOp->opcode==OP_SeekEnd ){
91526 assert( pOp->p2==0 );
91527 pC->seekResult = -1;
91533 pC->nullRow = (u8)res;
91534 pC->deferredMoveto = 0;
91535 pC->cacheStatus = CACHE_STALE;
91537 if( pOp->p2>0 ){
91555 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91556 pC = p->apCsr[pOp->p1];
91558 pCrsr = pC->uc.pCursor;
91564 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
91597 sqlite3_search_count--;
91599 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
91620 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91621 assert( pOp->p5==0 );
91622 pC = p->apCsr[pOp->p1];
91624 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
91627 pC->seekOp = OP_Rewind;
91632 assert( pC->eCurType==CURTYPE_BTREE );
91633 pCrsr = pC->uc.pCursor;
91636 pC->deferredMoveto = 0;
91637 pC->cacheStatus = CACHE_STALE;
91640 pC->nullRow = (u8)res;
91641 assert( pOp->p2>0 && pOp->p2<p->nOp );
91658 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
91670 ** number P5-1 in the prepared statement is incremented.
91686 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
91698 ** number P5-1 in the prepared statement is incremented.
91710 pC = p->apCsr[pOp->p1];
91716 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91717 assert( pOp->p5<ArraySize(p->aCounter) );
91718 pC = p->apCsr[pOp->p1];
91720 assert( pC->deferredMoveto==0 );
91721 assert( pC->eCurType==CURTYPE_BTREE );
91722 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
91723 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
91727 assert( pOp->opcode!=OP_Next
91728 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
91729 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found
91730 || pC->seekOp==OP_NullRow|| pC->seekOp==OP_SeekRowid
91731 || pC->seekOp==OP_IfNoHope);
91732 assert( pOp->opcode!=OP_Prev
91733 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
91734 || pC->seekOp==OP_Last || pC->seekOp==OP_IfNoHope
91735 || pC->seekOp==OP_NullRow);
91737 rc = pOp->p4.xAdvance(pC->uc.pCursor, pOp->p3);
91739 pC->cacheStatus = CACHE_STALE;
91742 pC->nullRow = 0;
91743 p->aCounter[pOp->p5]++;
91751 pC->nullRow = 1;
91767 ** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
91787 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91788 pC = p->apCsr[pOp->p1];
91792 pIn2 = &aMem[pOp->p2];
91793 assert( (pIn2->flags & MEM_Blob) || (pOp->p5 & OPFLAG_PREFORMAT) );
91794 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
91795 assert( pC->eCurType==CURTYPE_BTREE );
91796 assert( pC->isTable==0 );
91799 x.nKey = pIn2->n;
91800 x.pKey = pIn2->z;
91801 x.aMem = aMem + pOp->p3;
91802 x.nMem = (u16)pOp->p4.i;
91803 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
91804 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION|OPFLAG_PREFORMAT)),
91805 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
91807 assert( pC->deferredMoveto==0 );
91808 pC->cacheStatus = CACHE_STALE;
91823 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91824 pC = p->apCsr[pOp->p1];
91828 pIn2 = &aMem[pOp->p2];
91829 assert( pIn2->flags & MEM_Blob );
91830 assert( pC->isTable==0 );
91858 assert( pOp->p3>0 );
91859 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
91860 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91861 pC = p->apCsr[pOp->p1];
91863 assert( pC->eCurType==CURTYPE_BTREE );
91865 pCrsr = pC->uc.pCursor;
91867 r.pKeyInfo = pC->pKeyInfo;
91868 r.nField = (u16)pOp->p3;
91870 r.aMem = &aMem[pOp->p2];
91876 }else if( pOp->p5 ){
91880 assert( pC->deferredMoveto==0 );
91881 pC->cacheStatus = CACHE_STALE;
91882 pC->seekResult = 0;
91899 ** is non-zero, then reading column a(i)-1 from cursor P3 is
91920 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91921 pC = p->apCsr[pOp->p1];
91923 assert( pC->eCurType==CURTYPE_BTREE );
91924 assert( pC->uc.pCursor!=0 );
91925 assert( pC->isTable==0 );
91926 assert( pC->deferredMoveto==0 );
91927 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
91938 if( !pC->nullRow ){
91940 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
91944 if( pOp->opcode==OP_DeferredSeek ){
91945 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
91946 pTabCur = p->apCsr[pOp->p3];
91948 assert( pTabCur->eCurType==CURTYPE_BTREE );
91949 assert( pTabCur->uc.pCursor!=0 );
91950 assert( pTabCur->isTable );
91951 pTabCur->nullRow = 0;
91952 pTabCur->movetoTarget = rowid;
91953 pTabCur->deferredMoveto = 1;
91954 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
91955 pTabCur->aAltMap = pOp->p4.ai;
91956 assert( !pC->isEphemeral );
91957 assert( !pTabCur->isEphemeral );
91958 pTabCur->pAltCursor = pC;
91961 pOut->u.i = rowid;
91964 assert( pOp->opcode==OP_IdxRowid );
91965 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
91974 ** already occurred, this instruction is a no-op.
91979 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
91980 pC = p->apCsr[pOp->p1];
91981 if( pC->deferredMoveto ){
92040 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92041 pC = p->apCsr[pOp->p1];
92043 assert( pC->isOrdered );
92044 assert( pC->eCurType==CURTYPE_BTREE );
92045 assert( pC->uc.pCursor!=0);
92046 assert( pC->deferredMoveto==0 );
92047 assert( pOp->p4type==P4_INT32 );
92048 r.pKeyInfo = pC->pKeyInfo;
92049 r.nField = (u16)pOp->p4.i;
92050 if( pOp->opcode<OP_IdxLT ){
92051 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
92052 r.default_rc = -1;
92054 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
92057 r.aMem = &aMem[pOp->p3];
92063 REGISTER_TRACE(pOp->p3+i, &aMem[pOp->p3+i]);
92074 assert( pC->eCurType==CURTYPE_BTREE );
92075 pCur = pC->uc.pCursor;
92093 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
92094 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
92095 res = -res;
92097 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
92118 ** value of the root page that moved - its value before the move occurred -
92129 ** and non-autovacuum modes.
92138 assert( p->readOnly==0 );
92139 assert( pOp->p1>1 );
92141 pOut->flags = MEM_Null;
92142 if( db->nVdbeRead > db->nVDestroy+1 ){
92144 p->errorAction = OE_Abort;
92147 iDb = pOp->p3;
92148 assert( DbMaskTest(p->btreeMask, iDb) );
92150 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
92151 pOut->flags = MEM_Int;
92152 pOut->u.i = iMoved;
92156 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
92176 ** If the P3 value is non-zero, then the table referred to must be an
92189 assert( p->readOnly==0 );
92190 assert( DbMaskTest(p->btreeMask, pOp->p2) );
92192 db->aDb[pOp->p2].pBt, (u32)pOp->p1, (pOp->p3 ? &nChange : 0)
92194 if( pOp->p3 ){
92195 p->nChange += nChange;
92196 if( pOp->p3>0 ){
92197 assert( memIsValid(&aMem[pOp->p3]) );
92198 memAboutToChange(p, &aMem[pOp->p3]);
92199 aMem[pOp->p3].u.i += nChange;
92217 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
92218 pC = p->apCsr[pOp->p1];
92221 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
92223 assert( pC->eCurType==CURTYPE_BTREE );
92224 assert( pC->isEphemeral );
92225 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
92234 ** Allocate a new b-tree in the main database file if P1==0 or in the
92238 ** The root page number of the new b-tree is stored in register P2.
92247 assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
92248 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92249 assert( DbMaskTest(p->btreeMask, pOp->p1) );
92250 assert( p->readOnly==0 );
92251 pDb = &db->aDb[pOp->p1];
92252 assert( pDb->pBt!=0 );
92253 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, pOp->p3);
92255 pOut->u.i = pgno;
92265 db->nSqlExec++;
92266 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
92267 db->nSqlExec--;
92279 ** then runs the new virtual machine. It is thus a re-entrant opcode.
92292 for(iDb=0; iDb<db->nDb; iDb++){
92293 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
92297 iDb = pOp->p1;
92298 assert( iDb>=0 && iDb<db->nDb );
92302 if( pOp->p4.z==0 ){
92303 sqlite3SchemaClear(db->aDb[iDb].pSchema);
92304 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
92305 rc = sqlite3InitOne(db, iDb, &p->zErrMsg, pOp->p5);
92306 db->mDbFlags |= DBFLAG_SchemaChange;
92307 p->expired = 0;
92314 initData.pzErrMsg = &p->zErrMsg;
92316 initData.mxPage = sqlite3BtreeLastPage(db->aDb[iDb].pBt);
92319 db->aDb[iDb].zDbSName, zSchema, pOp->p4.z);
92323 assert( db->init.busy==0 );
92324 db->init.busy = 1;
92327 assert( !db->mallocFailed );
92331 /* The OP_ParseSchema opcode with a non-NULL P4 argument should parse
92337 db->init.busy = 0;
92358 assert( pOp->p1>=0 && pOp->p1<db->nDb );
92359 rc = sqlite3AnalysisLoad(db, pOp->p1);
92367 ** Remove the internal (in-memory) data structures that describe
92375 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
92381 ** Remove the internal (in-memory) data structures that describe
92389 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
92395 ** Remove the internal (in-memory) data structures that describe
92403 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
92412 ** register P1 the text of an error message describing any problems.
92435 assert( p->bIsReader );
92436 nRoot = pOp->p2;
92437 aRoot = pOp->p4.ai;
92440 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
92441 pnErr = &aMem[pOp->p3];
92442 assert( (pnErr->flags & MEM_Int)!=0 );
92443 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
92444 pIn1 = &aMem[pOp->p1];
92445 assert( pOp->p5<db->nDb );
92446 assert( DbMaskTest(p->btreeMask, pOp->p5) );
92447 z = sqlite3BtreeIntegrityCheck(db, db->aDb[pOp->p5].pBt, &aRoot[1], nRoot,
92448 (int)pnErr->u.i+1, &nErr);
92455 pnErr->u.i -= nErr-1;
92456 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
92473 pIn1 = &aMem[pOp->p1];
92474 pIn2 = &aMem[pOp->p2];
92475 assert( (pIn2->flags & MEM_Int)!=0 );
92476 if( (pIn1->flags & MEM_Blob)==0 ){
92480 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn2->u.i);
92495 pIn1 = &aMem[pOp->p1];
92496 assert( (pIn1->flags & MEM_Blob)==0 || sqlite3VdbeMemIsRowSet(pIn1) );
92497 if( (pIn1->flags & MEM_Blob)==0
92498 || sqlite3RowSetNext((RowSet*)pIn1->z, &val)==0
92507 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
92515 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
92524 ** must have P4==0, the final set must have P4==-1, and for all other sets
92529 ** (b) when P4==-1 there is no need to insert the value, as it will
92539 pIn1 = &aMem[pOp->p1];
92540 pIn3 = &aMem[pOp->p3];
92541 iSet = pOp->p4.i;
92542 assert( pIn3->flags&MEM_Int );
92547 if( (pIn1->flags & MEM_Blob)==0 ){
92551 assert( pOp->p4type==P4_INT32 );
92552 assert( iSet==-1 || iSet>=0 );
92554 exists = sqlite3RowSetTest((RowSet*)pIn1->z, iSet, pIn3->u.i);
92559 sqlite3RowSetInsert((RowSet*)pIn1->z, pIn3->u.i);
92572 ** cell in an array of values used as arguments to the sub-program. P2
92573 ** contains the address to jump to if the sub-program throws an IGNORE
92576 ** memory required by the sub-vdbe at runtime.
92580 ** If P5 is non-zero, then recursive program invocation is enabled.
92583 int nMem; /* Number of memory registers for sub-program */
92584 int nByte; /* Bytes of runtime space required for sub-program */
92589 SubProgram *pProgram; /* Sub-program to execute */
92592 pProgram = pOp->p4.pProgram;
92593 pRt = &aMem[pOp->p3];
92594 assert( pProgram->nOp>0 );
92597 ** disabled for backwards compatibility (p5 is set if this sub-program
92607 if( pOp->p5 ){
92608 t = pProgram->token;
92609 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
92613 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
92623 if( (pRt->flags&MEM_Blob)==0 ){
92629 nMem = pProgram->nMem + pProgram->nCsr;
92631 if( pProgram->nCsr==0 ) nMem++;
92634 + pProgram->nCsr * sizeof(VdbeCursor*)
92635 + (pProgram->nOp + 7)/8;
92641 pRt->flags = MEM_Blob|MEM_Dyn;
92642 pRt->z = (char*)pFrame;
92643 pRt->n = nByte;
92644 pRt->xDel = sqlite3VdbeFrameMemDel;
92646 pFrame->v = p;
92647 pFrame->nChildMem = nMem;
92648 pFrame->nChildCsr = pProgram->nCsr;
92649 pFrame->pc = (int)(pOp - aOp);
92650 pFrame->aMem = p->aMem;
92651 pFrame->nMem = p->nMem;
92652 pFrame->apCsr = p->apCsr;
92653 pFrame->nCursor = p->nCursor;
92654 pFrame->aOp = p->aOp;
92655 pFrame->nOp = p->nOp;
92656 pFrame->token = pProgram->token;
92658 pFrame->anExec = p->anExec;
92661 pFrame->iFrameMagic = SQLITE_FRAME_MAGIC;
92664 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
92666 pMem->flags = MEM_Undefined;
92667 pMem->db = db;
92670 pFrame = (VdbeFrame*)pRt->z;
92671 assert( pRt->xDel==sqlite3VdbeFrameMemDel );
92672 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
92673 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
92674 assert( pProgram->nCsr==pFrame->nChildCsr );
92675 assert( (int)(pOp - aOp)==pFrame->pc );
92678 p->nFrame++;
92679 pFrame->pParent = p->pFrame;
92680 pFrame->lastRowid = db->lastRowid;
92681 pFrame->nChange = p->nChange;
92682 pFrame->nDbChange = p->db->nChange;
92683 assert( pFrame->pAuxData==0 );
92684 pFrame->pAuxData = p->pAuxData;
92685 p->pAuxData = 0;
92686 p->nChange = 0;
92687 p->pFrame = pFrame;
92688 p->aMem = aMem = VdbeFrameMem(pFrame);
92689 p->nMem = pFrame->nChildMem;
92690 p->nCursor = (u16)pFrame->nChildCsr;
92691 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
92692 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
92693 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
92694 p->aOp = aOp = pProgram->aOp;
92695 p->nOp = pProgram->nOp;
92697 p->anExec = 0;
92704 for(i=0; i<p->nMem; i++){
92705 aMem[i].pScopyFrom = 0; /* Prevent false-positive AboutToChange() errs */
92710 pOp = &aOp[-1];
92716 ** This opcode is only ever present in sub-programs called via the
92730 pFrame = p->pFrame;
92731 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
92743 ** If P1 is non-zero, the database constraint counter is incremented
92748 if( db->flags & SQLITE_DeferFKs ){
92749 db->nDeferredImmCons += pOp->p2;
92750 }else if( pOp->p1 ){
92751 db->nDeferredCons += pOp->p2;
92753 p->nFkConstraint += pOp->p2;
92761 ** This opcode tests if a foreign key constraint-counter is currently zero.
92765 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
92767 ** zero, the jump is taken if the statement constraint-counter is zero
92771 if( pOp->p1 ){
92772 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
92773 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
92775 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
92776 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
92788 ** within a sub-program). Set the value of register P1 to the maximum of
92796 if( p->pFrame ){
92797 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
92798 pIn1 = &pFrame->aMem[pOp->p1];
92800 pIn1 = &aMem[pOp->p1];
92804 pIn2 = &aMem[pOp->p2];
92806 if( pIn1->u.i<pIn2->u.i){
92807 pIn1->u.i = pIn2->u.i;
92814 ** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
92824 pIn1 = &aMem[pOp->p1];
92825 assert( pIn1->flags&MEM_Int );
92826 VdbeBranchTaken( pIn1->u.i>0, 2);
92827 if( pIn1->u.i>0 ){
92828 pIn1->u.i -= pOp->p3;
92835 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
92848 ** and r[P2] is set to -1.
92854 pIn1 = &aMem[pOp->p1];
92855 pIn3 = &aMem[pOp->p3];
92857 assert( pIn1->flags & MEM_Int );
92858 assert( pIn3->flags & MEM_Int );
92859 x = pIn1->u.i;
92860 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
92868 pOut->u.i = -1;
92870 pOut->u.i = x;
92876 ** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
92880 ** If it is non-zero (negative or positive) and then also jump to P2.
92884 pIn1 = &aMem[pOp->p1];
92885 assert( pIn1->flags&MEM_Int );
92886 VdbeBranchTaken(pIn1->u.i<0, 2);
92887 if( pIn1->u.i ){
92888 if( pIn1->u.i>0 ) pIn1->u.i--;
92895 ** Synopsis: if (--r[P1])==0 goto P2
92901 pIn1 = &aMem[pOp->p1];
92902 assert( pIn1->flags&MEM_Int );
92903 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
92904 VdbeBranchTaken(pIn1->u.i==0, 2);
92905 if( pIn1->u.i==0 ) goto jump_to_p2;
92954 assert( pOp->p4type==P4_FUNCDEF );
92955 n = pOp->p5;
92956 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
92957 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
92958 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
92960 (sizeof(pCtx[0]) + sizeof(Mem) - sizeof(sqlite3_value*)));
92962 pCtx->pMem = 0;
92963 pCtx->pOut = (Mem*)&(pCtx->argv[n]);
92964 sqlite3VdbeMemInit(pCtx->pOut, db, MEM_Null);
92965 pCtx->pFunc = pOp->p4.pFunc;
92966 pCtx->iOp = (int)(pOp - aOp);
92967 pCtx->pVdbe = p;
92968 pCtx->skipFlag = 0;
92969 pCtx->isError = 0;
92970 pCtx->argc = n;
92971 pOp->p4type = P4_FUNCCTX;
92972 pOp->p4.pCtx = pCtx;
92975 assert( pOp->p1==(pOp->opcode==OP_AggInverse) );
92977 pOp->opcode = OP_AggStep1;
92986 assert( pOp->p4type==P4_FUNCCTX );
92987 pCtx = pOp->p4.pCtx;
92988 pMem = &aMem[pOp->p3];
92991 if( pOp->p1 ){
92994 assert( pMem->uTemp==0x1122e0e3 );
92997 pMem->uTemp = 0x1122e0e3;
93005 if( pCtx->pMem != pMem ){
93006 pCtx->pMem = pMem;
93007 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
93011 for(i=0; i<pCtx->argc; i++){
93012 assert( memIsValid(pCtx->argv[i]) );
93013 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
93017 pMem->n++;
93018 assert( pCtx->pOut->flags==MEM_Null );
93019 assert( pCtx->isError==0 );
93020 assert( pCtx->skipFlag==0 );
93022 if( pOp->p1 ){
93023 (pCtx->pFunc->xInverse)(pCtx,pCtx->argc,pCtx->argv);
93026 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
93028 if( pCtx->isError ){
93029 if( pCtx->isError>0 ){
93030 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
93031 rc = pCtx->isError;
93033 if( pCtx->skipFlag ){
93034 assert( pOp[-1].opcode==OP_CollSeq );
93035 i = pOp[-1].p1;
93037 pCtx->skipFlag = 0;
93039 sqlite3VdbeMemRelease(pCtx->pOut);
93040 pCtx->pOut->flags = MEM_Null;
93041 pCtx->isError = 0;
93044 assert( pCtx->pOut->flags==MEM_Null );
93045 assert( pCtx->skipFlag==0 );
93078 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
93079 assert( pOp->p3==0 || pOp->opcode==OP_AggValue );
93080 pMem = &aMem[pOp->p1];
93081 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
93083 if( pOp->p3 ){
93084 memAboutToChange(p, &aMem[pOp->p3]);
93085 rc = sqlite3VdbeMemAggValue(pMem, &aMem[pOp->p3], pOp->p4.pFunc);
93086 pMem = &aMem[pOp->p3];
93090 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
93108 ** Checkpoint database P1. This is a no-op if P1 is not currently in
93115 ** mem[P3+2] are initialized to -1.
93122 assert( p->readOnly==0 );
93124 aRes[1] = aRes[2] = -1;
93125 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
93126 || pOp->p2==SQLITE_CHECKPOINT_FULL
93127 || pOp->p2==SQLITE_CHECKPOINT_RESTART
93128 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
93130 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
93136 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
93153 ** Write a string containing the final journal-mode to register P2.
93165 eNew = pOp->p3;
93174 assert( pOp->p1>=0 && pOp->p1<db->nDb );
93175 assert( p->readOnly==0 );
93177 pBt = db->aDb[pOp->p1].pBt;
93191 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
93199 if( !db->autoCommit || db->nVdbeRead>1 ){
93210 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
93238 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
93239 pOut->z = (char *)sqlite3JournalModename(eNew);
93240 pOut->n = sqlite3Strlen30(pOut->z);
93241 pOut->enc = SQLITE_UTF8;
93259 assert( p->readOnly==0 );
93260 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1,
93261 pOp->p2 ? &aMem[pOp->p2] : 0);
93277 assert( pOp->p1>=0 && pOp->p1<db->nDb );
93278 assert( DbMaskTest(p->btreeMask, pOp->p1) );
93279 assert( p->readOnly==0 );
93280 pBt = db->aDb[pOp->p1].pBt;
93299 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
93309 assert( pOp->p2==0 || pOp->p2==1 );
93310 if( !pOp->p1 ){
93311 sqlite3ExpirePreparedStatements(db, pOp->p2);
93313 p->expired = pOp->p2+1;
93325 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93326 pC = p->apCsr[pOp->p1];
93328 assert( pC->eCurType==CURTYPE_BTREE );
93329 sqlite3BtreeCursorPin(pC->uc.pCursor);
93340 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
93341 pC = p->apCsr[pOp->p1];
93343 assert( pC->eCurType==CURTYPE_BTREE );
93344 sqlite3BtreeCursorUnpin(pC->uc.pCursor);
93353 ** the shared-cache feature is enabled.
93359 ** P2 contains the root-page of the table to lock.
93362 ** used to generate an error message if the lock cannot be obtained.
93365 u8 isWriteLock = (u8)pOp->p3;
93366 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommit) ){
93367 int p1 = pOp->p1;
93368 assert( p1>=0 && p1<db->nDb );
93369 assert( DbMaskTest(p->btreeMask, p1) );
93371 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
93374 const char *z = pOp->p4.z;
93396 pVTab = pOp->p4.pVtab;
93398 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
93418 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
93419 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
93420 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
93423 assert( zTab || db->mallocFailed );
93425 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
93440 db->nVDestroy++;
93441 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
93442 db->nVDestroy--;
93443 assert( p->errorAction==OE_Abort && p->usesStmtJournal );
93462 assert( p->bIsReader );
93465 pVtab = pOp->p4.pVtab->pVtab;
93466 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
93470 pModule = pVtab->pModule;
93471 rc = pModule->xOpen(pVtab, &pVCur);
93476 pVCur->pVtab = pVtab;
93479 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
93481 pCur->uc.pVCur = pVCur;
93482 pVtab->nRef++;
93484 assert( db->mallocFailed );
93485 pModule->xClose(pVCur);
93525 pQuery = &aMem[pOp->p3];
93527 pCur = p->apCsr[pOp->p1];
93529 REGISTER_TRACE(pOp->p3, pQuery);
93530 assert( pCur->eCurType==CURTYPE_VTAB );
93531 pVCur = pCur->uc.pVCur;
93532 pVtab = pVCur->pVtab;
93533 pModule = pVtab->pModule;
93536 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
93537 nArg = (int)pArgc->u.i;
93538 iQuery = (int)pQuery->u.i;
93542 apArg = p->apArg;
93546 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
93549 res = pModule->xEof(pVCur);
93550 pCur->nullRow = 0;
93561 ** Store in register P3 the value of the P2-th column of
93562 ** the current row of the virtual-table of cursor P1.
93578 VdbeCursor *pCur = p->apCsr[pOp->p1];
93579 assert( pCur->eCurType==CURTYPE_VTAB );
93580 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
93581 pDest = &aMem[pOp->p3];
93583 if( pCur->nullRow ){
93587 pVtab = pCur->uc.pVCur->pVtab;
93588 pModule = pVtab->pModule;
93589 assert( pModule->xColumn );
93592 assert( pOp->p5==OPFLAG_NOCHNG || pOp->p5==0 );
93593 if( pOp->p5 & OPFLAG_NOCHNG ){
93595 pDest->flags = MEM_Null|MEM_Zero;
93596 pDest->u.nZero = 0;
93600 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
93607 REGISTER_TRACE(pOp->p3, pDest);
93632 pCur = p->apCsr[pOp->p1];
93633 assert( pCur->eCurType==CURTYPE_VTAB );
93634 if( pCur->nullRow ){
93637 pVtab = pCur->uc.pVCur->pVtab;
93638 pModule = pVtab->pModule;
93639 assert( pModule->xNext );
93647 rc = pModule->xNext(pCur->uc.pVCur);
93650 res = pModule->xEof(pCur->uc.pVCur);
93672 isLegacy = (db->flags & SQLITE_LegacyAlter);
93673 db->flags |= SQLITE_LegacyAlter;
93674 pVtab = pOp->p4.pVtab->pVtab;
93675 pName = &aMem[pOp->p1];
93676 assert( pVtab->pModule->xRename );
93678 assert( p->readOnly==0 );
93679 REGISTER_TRACE(pOp->p1, pName);
93680 assert( pName->flags & MEM_Str );
93681 testcase( pName->enc==SQLITE_UTF8 );
93682 testcase( pName->enc==SQLITE_UTF16BE );
93683 testcase( pName->enc==SQLITE_UTF16LE );
93686 rc = pVtab->pModule->xRename(pVtab, pName->z);
93687 if( isLegacy==0 ) db->flags &= ~(u64)SQLITE_LegacyAlter;
93689 p->expired = 0;
93702 ** invocation. The value in register (P3+P2-1) corresponds to the
93732 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
93733 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
93735 assert( p->readOnly==0 );
93736 if( db->mallocFailed ) goto no_mem;
93738 pVtab = pOp->p4.pVtab->pVtab;
93739 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
93743 pModule = pVtab->pModule;
93744 nArg = pOp->p2;
93745 assert( pOp->p4type==P4_VTAB );
93746 if( ALWAYS(pModule->xUpdate) ){
93747 u8 vtabOnConflict = db->vtabOnConflict;
93748 apArg = p->apArg;
93749 pX = &aMem[pOp->p3];
93756 db->vtabOnConflict = pOp->p5;
93757 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
93758 db->vtabOnConflict = vtabOnConflict;
93760 if( rc==SQLITE_OK && pOp->p1 ){
93761 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
93762 db->lastRowid = rowid;
93764 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
93765 if( pOp->p5==OE_Ignore ){
93768 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
93771 p->nChange++;
93786 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
93806 pBt = db->aDb[pOp->p1].pBt;
93808 if( pOp->p3 ){
93810 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
93812 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
93827 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
93846 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
93855 ** purely non-deterministic. Some built-in date/time functions can be
93856 ** either determinitic of non-deterministic, depending on their arguments.
93857 ** When those function are used in a non-deterministic way, they will check
93868 assert( pOp->p4type==P4_FUNCCTX );
93869 pCtx = pOp->p4.pCtx;
93875 pOut = &aMem[pOp->p3];
93876 if( pCtx->pOut != pOut ){
93877 pCtx->pVdbe = p;
93878 pCtx->pOut = pOut;
93879 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
93881 assert( pCtx->pVdbe==p );
93885 for(i=0; i<pCtx->argc; i++){
93886 assert( memIsValid(pCtx->argv[i]) );
93887 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
93891 assert( pCtx->isError==0 );
93892 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
93895 if( pCtx->isError ){
93896 if( pCtx->isError>0 ){
93898 rc = pCtx->isError;
93900 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
93901 pCtx->isError = 0;
93906 if( pOut->flags & (MEM_Str|MEM_Blob) ){
93911 REGISTER_TRACE(pOp->p3, pOut);
93930 ** the UTF-8 string contained in P4 is emitted on the trace callback.
93949 ** The "--" string is broken up to prevent false-positives with srcck1.c.
93952 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
93954 ** using the X argument when X begins with "--" and invoking
93957 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
93960 assert( pOp==p->aOp || pOp->opcode==OP_Trace );
93963 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
93964 && !p->doingRerun
93965 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
93968 if( db->mTrace & SQLITE_TRACE_LEGACY ){
93970 db->trace.xLegacy(db->pTraceArg, z);
93974 if( db->nVdbeExec>1 ){
93975 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
93976 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
93979 (void)db->trace.xV2(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
93983 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
93986 for(j=0; j<db->nDb; j++){
93987 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
93988 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
93993 if( (db->flags & SQLITE_SqlTrace)!=0
93994 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
93996 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
94000 assert( pOp->p2>0 );
94001 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
94002 if( pOp->opcode==OP_Trace ) break;
94003 for(i=1; i<p->nOp; i++){
94004 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
94006 pOp->p1 = 0;
94008 pOp->p1++;
94009 p->aCounter[SQLITE_STMTSTATUS_RUN]++;
94019 ** expression refer to columns in the b-tree to which cursor P1 is pointing.
94024 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
94025 assert( pOp->p4type==P4_EXPR );
94026 pC = p->apCsr[pOp->p1];
94028 assert( pC->eCurType==CURTYPE_BTREE );
94029 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
94030 pOp->p4.pExpr, aMem);
94087 assert( pOp->p1>0 );
94088 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
94089 pMem = &aMem[pOp->p1];
94090 constMask = pOp->p3;
94091 for(i=0; i<pOp->p2; i++, pMem++){
94093 pMem->pScopyFrom = 0;
94094 if( i<32 && pOp->p5 ) MemSetTypeFlag(pMem, MEM_Undefined);
94110 ** the same as a no-op. This opcodesnever appears in a real VM program.
94113 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
94120 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
94129 if( endTime>start ) pOrigOp->cycles += endTime - start;
94130 pOrigOp->cnt++;
94140 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
94143 if( db->flags & SQLITE_VdbeTrace ){
94144 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
94147 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
94150 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
94167 if( db->mallocFailed ){
94173 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
94176 p->rc = rc;
94180 (int)(pOp - aOp), p->zSql, p->zErrMsg);
94185 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
94193 while( nVmStep>=nProgressLimit && db->xProgress!=0 ){
94194 nProgressLimit += db->nProgressOps;
94195 if( db->xProgress(db->pProgressArg) ){
94202 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
94205 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
94229 assert( AtomicLoad(&db->u1.isInterrupted) );
94275 ** the b-tree cursor associated with blob handle p to point to row iRow.
94282 ** be set to point to a buffer containing an error message. It is the
94283 ** responsibility of the caller to free the error message buffer using
94286 ** If an error does occur, then the b-tree cursor is closed. All subsequent
94292 char *zErr = 0; /* Error message */
94293 Vdbe *v = (Vdbe *)p->pStmt;
94298 v->aMem[1].flags = MEM_Int;
94299 v->aMem[1].u.i = iRow;
94305 if( v->pc>4 ){
94306 v->pc = 4;
94307 assert( v->aOp[v->pc].opcode==OP_NotExists );
94310 rc = sqlite3_step(p->pStmt);
94313 VdbeCursor *pC = v->apCsr[0];
94314 u32 type = pC->nHdrParsed>p->iCol ? pC->aType[p->iCol] : 0;
94315 testcase( pC->nHdrParsed==p->iCol );
94316 testcase( pC->nHdrParsed==p->iCol+1 );
94318 zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
94322 sqlite3_finalize(p->pStmt);
94323 p->pStmt = 0;
94325 p->iOffset = pC->aType[p->iCol + pC->nField];
94326 p->nByte = sqlite3VdbeSerialTypeLen(type);
94327 p->pCsr = pC->uc.pCursor;
94328 sqlite3BtreeIncrblobCursor(p->pCsr);
94334 }else if( p->pStmt ){
94335 rc = sqlite3_finalize(p->pStmt);
94336 p->pStmt = 0;
94338 zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
94341 zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
94361 int wrFlag, /* True -> read/write access, false -> read-only */
94365 int iCol; /* Index of zColumn in row-record */
94385 sqlite3_mutex_enter(db->mutex);
94406 if( pTab && pTab->pSelect ){
94421 pBlob->pTab = pTab;
94422 pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;
94425 for(iCol=0; iCol<pTab->nCol; iCol++) {
94426 if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
94430 if( iCol==pTab->nCol ){
94445 if( db->flags&SQLITE_ForeignKeys ){
94451 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
94453 for(j=0; j<pFKey->nCol; j++){
94454 if( pFKey->aCol[j].iFrom==iCol ){
94461 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94463 for(j=0; j<pIdx->nKeyCol; j++){
94465 if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
94479 pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
94480 assert( pBlob->pStmt || db->mallocFailed );
94481 if( pBlob->pStmt ){
94485 ** of writing code to use the b-tree layer directly is that the
94490 ** Code external to the Vdbe then "borrows" the b-tree cursor and
94495 ** which closes the b-tree cursor and (possibly) commits the
94508 Vdbe *v = (Vdbe *)pBlob->pStmt;
94509 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
94513 pTab->pSchema->schema_cookie,
94514 pTab->pSchema->iGeneration);
94516 assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
94522 if( db->mallocFailed==0 ){
94529 aOp[0].p2 = pTab->tnum;
94531 sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
94533 if( db->mallocFailed==0 ){
94537 ** parameter of the other to pTab->tnum. */
94539 aOp[1].p2 = pTab->tnum;
94550 aOp[1].p4.i = pTab->nCol+1;
94551 aOp[3].p2 = pTab->nCol;
94560 pBlob->iCol = iCol;
94561 pBlob->db = db;
94563 if( db->mallocFailed ){
94570 if( rc==SQLITE_OK && db->mallocFailed==0 ){
94573 if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
94580 sqlite3_mutex_leave(db->mutex);
94594 sqlite3_stmt *pStmt = p->pStmt;
94595 db = p->db;
94596 sqlite3_mutex_enter(db->mutex);
94598 sqlite3_mutex_leave(db->mutex);
94622 db = p->db;
94623 sqlite3_mutex_enter(db->mutex);
94624 v = (Vdbe*)p->pStmt;
94626 if( n<0 || iOffset<0 || ((sqlite3_int64)iOffset+n)>p->nByte ){
94630 /* If there is no statement handle, then the blob-handle has
94636 ** returned, clean-up the statement handle.
94638 assert( db == v->db );
94639 sqlite3BtreeEnterCursor(p->pCsr);
94642 if( xCall==sqlite3BtreePutData && db->xPreUpdateCallback ){
94643 /* If a pre-update hook is registered and this is a write cursor,
94646 ** TODO: The preupdate-hook is passed SQLITE_DELETE, even though this
94653 ** using the incremental-blob API, this works. For the sessions module
94657 iKey = sqlite3BtreeIntegerKey(p->pCsr);
94659 v, v->apCsr[0], SQLITE_DELETE, p->zDb, p->pTab, iKey, -1
94664 rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
94665 sqlite3BtreeLeaveCursor(p->pCsr);
94668 p->pStmt = 0;
94670 v->rc = rc;
94675 sqlite3_mutex_leave(db->mutex);
94701 return (p && p->pStmt) ? p->nByte : 0;
94710 ** database handle error code and message set. If this happens, then all
94720 db = p->db;
94721 sqlite3_mutex_enter(db->mutex);
94723 if( p->pStmt==0 ){
94724 /* If there is no statement handle, then the blob-handle has
94739 assert( rc==SQLITE_OK || p->pStmt==0 );
94740 sqlite3_mutex_leave(db->mutex);
94749 ** 2011-07-09
94764 ** The VdbeSorter object implements a multi-threaded external merge sort
94768 ** Here is the (internal, non-API) interface between this module and the
94821 ** an in-memory merge sort. In this case, no temporary files are required
94828 ** A PMA created at this point is known as a "level-0 PMA". Higher levels
94829 ** of PMAs may be created by merging existing PMAs together - for example
94830 ** merging two or more level-0 PMAs together creates a level-1 PMA.
94834 ** page-cache of the main database. Specifically, the threshold is set to
94838 ** If the sorter is running in single-threaded mode, then all PMAs generated
94840 ** multi-threaded mode then up to (N+1) temporary files may be opened, where
94847 ** The sorter is running in multi-threaded mode if (a) the library was built
94848 ** with pre-processor symbol SQLITE_MAX_WORKER_THREADS set to a value greater
94857 ** sorter is running in single-threaded mode, then these PMAs are merged
94862 ** Or, if running in multi-threaded mode, then a background thread is
94872 ** Rewind() is called, then a hierarchy of incremental-merges is used.
94878 ** If running in multi-threaded mode and there are more than
94891 ** characteristics of the sorter in multi-threaded mode.
94898 ** Hard-coded maximum amount of data to accumulate in memory before flushing
94911 typedef struct SortSubtask SortSubtask; /* A sub-task in the sort process */
94913 typedef struct SorterList SorterList; /* In-memory list of records */
94926 ** An in-memory list of objects to be sorted.
94935 u8 *aMemory; /* If non-NULL, bulk memory to hold pList */
94958 ** comparing aReadr[2*i-N] and aReadr[2*i-N+1]. Whichever key is smaller, the
94972 ** aReadr[0] -> Banana
94973 ** aReadr[1] -> Feijoa
94974 ** aReadr[2] -> Elderberry
94975 ** aReadr[3] -> Currant
94976 ** aReadr[4] -> Grapefruit
94977 ** aReadr[5] -> Apple
94978 ** aReadr[6] -> Durian
94979 ** aReadr[7] -> EOF
94988 ** aReadr[5] -> Eggplant
94993 ** The value of PmaReader 6 - "Durian" - is now smaller than that of PmaReader
95016 ** single-threaded operation, there is exactly one instance of this object
95017 ** and for multi-threaded operation there are two or more instances.
95028 ** 1. When flushing the contents of memory to a level-0 PMA on disk, to
95045 VdbeSorter *pSorter; /* Sorter that owns this sub-task */
95050 SorterFile file; /* Temp file for level-0 PMAs */
95074 SorterList list; /* List of in-memory records */
95117 ** There are two types of IncrMerger object - single (bUseThread==0) and
95118 ** multi-threaded (bUseThread==1).
95120 ** A multi-threaded IncrMerger object uses two temporary files - aFile[0]
95135 ** A single-threaded IncrMerger does not open any temporary files of its
95137 ** at offset iStartOff of file pTask->file2. And instead of using a
95139 ** threaded IncrMerger the allocate part of pTask->file2 is "refilled" with
95157 ** size. But I/O is more efficient if it occurs in page-sized blocks where
95159 ** the PMA so that aligned, page-size blocks are written.
95162 int eFWErr; /* Non-zero if in an error state */
95176 ** by this module. If using a separate allocation for each in-memory record
95184 ** has finished passing records to the sorter, or when the in-memory buffer
95217 sqlite3_free(pReadr->aAlloc);
95218 sqlite3_free(pReadr->aBuffer);
95219 if( pReadr->aMap ) sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
95220 vdbeIncrFree(pReadr->pIncr);
95241 if( p->aMap ){
95242 *ppOut = &p->aMap[p->iReadOff];
95243 p->iReadOff += nByte;
95247 assert( p->aBuffer );
95250 ** p->nBuffer bytes of data from the file into it. Or, if there are less
95251 ** than p->nBuffer bytes remaining in the PMA, read all remaining data. */
95252 iBuf = p->iReadOff % p->nBuffer;
95258 if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
95259 nRead = p->nBuffer;
95261 nRead = (int)(p->iEof - p->iReadOff);
95266 rc = sqlite3OsRead(p->pFd, p->aBuffer, nRead, p->iReadOff);
95270 nAvail = p->nBuffer - iBuf;
95273 /* The requested data is available in the in-memory buffer. In this
95276 *ppOut = &p->aBuffer[iBuf];
95277 p->iReadOff += nByte;
95279 /* The requested data is not all available in the in-memory buffer.
95280 ** In this case, allocate space at p->aAlloc[] to copy the requested
95281 ** range into. Then return a copy of pointer p->aAlloc to the caller. */
95284 /* Extend the p->aAlloc[] allocation if required. */
95285 if( p->nAlloc<nByte ){
95287 sqlite3_int64 nNew = MAX(128, 2*(sqlite3_int64)p->nAlloc);
95289 aNew = sqlite3Realloc(p->aAlloc, nNew);
95291 p->nAlloc = nNew;
95292 p->aAlloc = aNew;
95296 ** p->aAlloc[]. */
95297 memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
95298 p->iReadOff += nAvail;
95299 nRem = nByte - nAvail;
95301 /* The following loop copies up to p->nBuffer bytes per iteration into
95302 ** the p->aAlloc[] buffer. */
95309 if( nRem>p->nBuffer ) nCopy = p->nBuffer;
95312 assert( aNext!=p->aAlloc );
95313 memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
95314 nRem -= nCopy;
95317 *ppOut = p->aAlloc;
95330 if( p->aMap ){
95331 p->iReadOff += sqlite3GetVarint(&p->aMap[p->iReadOff], pnOut);
95333 iBuf = p->iReadOff % p->nBuffer;
95334 if( iBuf && (p->nBuffer-iBuf)>=9 ){
95335 p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
95362 if( pFile->iEof<=(i64)(pTask->pSorter->db->nMaxSorterMmap) ){
95363 sqlite3_file *pFd = pFile->pFd;
95364 if( pFd->pMethods->iVersion>=3 ){
95365 rc = sqlite3OsFetch(pFd, 0, (int)pFile->iEof, (void**)pp);
95385 assert( pReadr->pIncr==0 || pReadr->pIncr->bEof==0 );
95388 if( pReadr->aMap ){
95389 sqlite3OsUnfetch(pReadr->pFd, 0, pReadr->aMap);
95390 pReadr->aMap = 0;
95392 pReadr->iReadOff = iOff;
95393 pReadr->iEof = pFile->iEof;
95394 pReadr->pFd = pFile->pFd;
95396 rc = vdbeSorterMapFile(pTask, pFile, &pReadr->aMap);
95397 if( rc==SQLITE_OK && pReadr->aMap==0 ){
95398 int pgsz = pTask->pSorter->pgsz;
95399 int iBuf = pReadr->iReadOff % pgsz;
95400 if( pReadr->aBuffer==0 ){
95401 pReadr->aBuffer = (u8*)sqlite3Malloc(pgsz);
95402 if( pReadr->aBuffer==0 ) rc = SQLITE_NOMEM_BKPT;
95403 pReadr->nBuffer = pgsz;
95406 int nRead = pgsz - iBuf;
95407 if( (pReadr->iReadOff + nRead) > pReadr->iEof ){
95408 nRead = (int)(pReadr->iEof - pReadr->iReadOff);
95411 pReadr->pFd, &pReadr->aBuffer[iBuf], nRead, pReadr->iReadOff
95429 if( pReadr->iReadOff>=pReadr->iEof ){
95430 IncrMerger *pIncr = pReadr->pIncr;
95434 if( rc==SQLITE_OK && pIncr->bEof==0 ){
95436 pIncr->pTask, pReadr, &pIncr->aFile[0], pIncr->iStartOff
95454 pReadr->nKey = (int)nRec;
95455 rc = vdbePmaReadBlob(pReadr, (int)nRec, &pReadr->aKey);
95464 ** starting at offset iStart and ending at offset iEof-1. This function
95480 assert( pFile->iEof>iStart );
95481 assert( pReadr->aAlloc==0 && pReadr->nAlloc==0 );
95482 assert( pReadr->aBuffer==0 );
95483 assert( pReadr->aMap==0 );
95489 pReadr->iEof = pReadr->iReadOff + nByte;
95506 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
95510 UnpackedRecord *r2 = pTask->pUnpacked;
95512 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
95520 ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences
95524 ** it is assumed that (pTask->pUnpacked) contains the unpacked version
95525 ** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked
95528 ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
95533 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
95537 UnpackedRecord *r2 = pTask->pUnpacked;
95539 sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2);
95552 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
95567 res = memcmp(v1, v2, (MIN(n1, n2) - 13)/2);
95569 res = n1 - n2;
95573 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
95579 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
95580 if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
95581 res = res * -1;
95594 int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */
95616 if( (res = v1[i] - v2[i])!=0 ){
95618 res = v1[0] & 0x80 ? -1 : +1;
95624 res = s1 - s2;
95629 res = -1;
95631 res = s1 - s2;
95636 if( *v1 & 0x80 ) res = -1;
95643 if( pTask->pSorter->pKeyInfo->nKeyField>1 ){
95648 }else if( pTask->pSorter->pKeyInfo->aSortFlags[0] ){
95649 assert( !(pTask->pSorter->pKeyInfo->aSortFlags[0]&KEYINFO_ORDER_BIGNULL) );
95650 res = res * -1;
95659 ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nKeyField)
95662 ** is non-zero and the sorter is able to guarantee a stable sort, nField
95670 ** The sorter can guarantee a stable sort when running in single-threaded
95671 ** mode, but not in multi-threaded mode.
95683 KeyInfo *pKeyInfo; /* Copy of pCsr->pKeyInfo with db==0 */
95684 int szKeyInfo; /* Size of pCsr->pKeyInfo in bytes */
95698 nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
95706 nWorker = SORTER_MAX_MERGE_COUNT-1;
95710 assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
95711 assert( pCsr->eCurType==CURTYPE_SORTER );
95712 szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-1)*sizeof(CollSeq*);
95716 pCsr->uc.pSorter = pSorter;
95720 Btree *pBt = db->aDb[0].pBt;
95721 pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
95722 memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
95723 pKeyInfo->db = 0;
95725 pKeyInfo->nKeyField = nField;
95728 pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(pBt);
95730 pSorter->nTask = nWorker + 1;
95731 pSorter->iPrev = (u8)(nWorker - 1);
95732 pSorter->bUseThreads = (pSorter->nTask>1);
95733 pSorter->db = db;
95734 for(i=0; i<pSorter->nTask; i++){
95735 SortSubtask *pTask = &pSorter->aTask[i];
95736 pTask->pSorter = pSorter;
95742 pSorter->mnPmaSize = szPma * pgsz;
95744 mxCache = db->aDb[0].pSchema->cache_size;
95746 /* A negative cache-size value C indicates that the cache is abs(C)
95748 mxCache = mxCache * -1024;
95753 pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);
95758 assert( pSorter->iMemory==0 );
95759 pSorter->nMemory = pgsz;
95760 pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
95761 if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
95765 if( pKeyInfo->nAllField<13
95766 && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
95767 && (pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL)==0
95769 pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
95784 pNext = p->u.pNext;
95794 sqlite3DbFree(db, pTask->pUnpacked);
95796 /* pTask->list.aMemory can only be non-zero if it was handed memory
95798 if( pTask->list.aMemory ){
95799 sqlite3_free(pTask->list.aMemory);
95803 assert( pTask->list.aMemory==0 );
95804 vdbeSorterRecordFree(0, pTask->list.pList);
95806 if( pTask->file.pFd ){
95807 sqlite3OsCloseFree(pTask->file.pFd);
95809 if( pTask->file2.pFd ){
95810 sqlite3OsCloseFree(pTask->file2.pFd);
95818 int iTask = (pTask - pTask->pSorter->aTask);
95819 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
95832 int iTask = (pTask - pTask->pSorter->aTask);
95833 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
95843 sqlite3OsCurrentTimeInt64(pTask->pSorter->db->pVfs, &t);
95856 ** Join thread pTask->thread.
95860 if( pTask->pThread ){
95862 int bDone = pTask->bDone;
95866 (void)sqlite3ThreadJoin(pTask->pThread, &pRet);
95869 assert( pTask->bDone==1 );
95870 pTask->bDone = 0;
95871 pTask->pThread = 0;
95884 assert( pTask->pThread==0 && pTask->bDone==0 );
95885 return sqlite3ThreadCreate(&pTask->pThread, xTask, pIn);
95890 ** level-0 PMAs.
95899 ** it is possible that thread pSorter->aTask[pSorter->nTask-1].pThread
95902 ** thread pSorter->aTask[pSorter->nTask-1].pThread first. */
95903 for(i=pSorter->nTask-1; i>=0; i--){
95904 SortSubtask *pTask = &pSorter->aTask[i];
95934 pNew->nTree = N;
95935 pNew->pTask = 0;
95936 pNew->aReadr = (PmaReader*)&pNew[1];
95937 pNew->aTree = (int*)&pNew->aReadr[N];
95948 for(i=0; i<pMerger->nTree; i++){
95949 vdbePmaReaderClear(&pMerger->aReadr[i]);
95962 if( pIncr->bUseThread ){
95963 vdbeSorterJoinThread(pIncr->pTask);
95964 if( pIncr->aFile[0].pFd ) sqlite3OsCloseFree(pIncr->aFile[0].pFd);
95965 if( pIncr->aFile[1].pFd ) sqlite3OsCloseFree(pIncr->aFile[1].pFd);
95968 vdbeMergeEngineFree(pIncr->pMerger);
95979 assert( pSorter->bUseThreads || pSorter->pReader==0 );
95981 if( pSorter->pReader ){
95982 vdbePmaReaderClear(pSorter->pReader);
95983 sqlite3DbFree(db, pSorter->pReader);
95984 pSorter->pReader = 0;
95987 vdbeMergeEngineFree(pSorter->pMerger);
95988 pSorter->pMerger = 0;
95989 for(i=0; i<pSorter->nTask; i++){
95990 SortSubtask *pTask = &pSorter->aTask[i];
95992 pTask->pSorter = pSorter;
95994 if( pSorter->list.aMemory==0 ){
95995 vdbeSorterRecordFree(0, pSorter->list.pList);
95997 pSorter->list.pList = 0;
95998 pSorter->list.szPMA = 0;
95999 pSorter->bUsePMA = 0;
96000 pSorter->iMemory = 0;
96001 pSorter->mxKeysize = 0;
96002 sqlite3DbFree(db, pSorter->pUnpacked);
96003 pSorter->pUnpacked = 0;
96011 assert( pCsr->eCurType==CURTYPE_SORTER );
96012 pSorter = pCsr->uc.pSorter;
96015 sqlite3_free(pSorter->list.aMemory);
96017 pCsr->uc.pSorter = 0;
96023 ** The first argument is a file-handle open on a temporary file. The file
96032 if( nByte<=(i64)(db->nMaxSorterMmap) && pFd->pMethods->iVersion>=3 ){
96046 ** Allocate space for a file-handle and open a temporary file. If successful,
96047 ** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
96057 rc = sqlite3OsOpenMalloc(db->pVfs, 0, ppFd,
96074 ** structure at pTask->pUnpacked. Return SQLITE_OK if successful (or
96078 if( pTask->pUnpacked==0 ){
96079 pTask->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pTask->pSorter->pKeyInfo);
96080 if( pTask->pUnpacked==0 ) return SQLITE_NOMEM_BKPT;
96081 pTask->pUnpacked->nField = pTask->pSorter->pKeyInfo->nKeyField;
96082 pTask->pUnpacked->errCode = 0;
96103 res = pTask->xCompare(
96104 pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal
96109 pp = &p1->u.pNext;
96110 p1 = p1->u.pNext;
96117 pp = &p2->u.pNext;
96118 p2 = p2->u.pNext;
96134 if( p->typeMask==SORTER_TYPE_INTEGER ){
96136 }else if( p->typeMask==SORTER_TYPE_TEXT ){
96143 ** Sort the linked list of records headed at pTask->pList. Return
96156 p = pList->pList;
96157 pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter);
96162 if( pList->aMemory ){
96163 if( (u8*)p==pList->aMemory ){
96166 assert( p->u.iNext<sqlite3MallocSize(pList->aMemory) );
96167 pNext = (SorterRecord*)&pList->aMemory[p->u.iNext];
96170 pNext = p->u.pNext;
96173 p->u.pNext = 0;
96187 pList->pList = p;
96189 assert( pTask->pUnpacked->errCode==SQLITE_OK
96190 || pTask->pUnpacked->errCode==SQLITE_NOMEM
96192 return pTask->pUnpacked->errCode;
96196 ** Initialize a PMA-writer object.
96205 p->aBuffer = (u8*)sqlite3Malloc(nBuf);
96206 if( !p->aBuffer ){
96207 p->eFWErr = SQLITE_NOMEM_BKPT;
96209 p->iBufEnd = p->iBufStart = (iStart % nBuf);
96210 p->iWriteOff = iStart - p->iBufStart;
96211 p->nBuffer = nBuf;
96212 p->pFd = pFd;
96222 while( nRem>0 && p->eFWErr==0 ){
96224 if( nCopy>(p->nBuffer - p->iBufEnd) ){
96225 nCopy = p->nBuffer - p->iBufEnd;
96228 memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
96229 p->iBufEnd += nCopy;
96230 if( p->iBufEnd==p->nBuffer ){
96231 p->eFWErr = sqlite3OsWrite(p->pFd,
96232 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
96233 p->iWriteOff + p->iBufStart
96235 p->iBufStart = p->iBufEnd = 0;
96236 p->iWriteOff += p->nBuffer;
96238 assert( p->iBufEnd<p->nBuffer );
96240 nRem -= nCopy;
96245 ** Flush any buffered data to disk and clean up the PMA-writer object.
96246 ** The results of using the PMA-writer after this call are undefined.
96255 if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
96256 p->eFWErr = sqlite3OsWrite(p->pFd,
96257 &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
96258 p->iWriteOff + p->iBufStart
96261 *piEof = (p->iWriteOff + p->iBufEnd);
96262 sqlite3_free(p->aBuffer);
96263 rc = p->eFWErr;
96280 ** Write the current contents of in-memory linked-list pList to a level-0
96281 ** PMA in the temp file belonging to sub-task pTask. Return SQLITE_OK if
96289 ** * One or more records packed end-to-end in order of ascending keys.
96294 sqlite3 *db = pTask->pSorter->db;
96299 /* Set iSz to the expected size of file pTask->file after writing the PMA.
96301 i64 iSz = pList->szPMA + sqlite3VarintLen(pList->szPMA) + pTask->file.iEof;
96306 assert( pList->szPMA>0 );
96309 if( pTask->file.pFd==0 ){
96310 rc = vdbeSorterOpenTempFile(db, 0, &pTask->file.pFd);
96311 assert( rc!=SQLITE_OK || pTask->file.pFd );
96312 assert( pTask->file.iEof==0 );
96313 assert( pTask->nPMA==0 );
96318 vdbeSorterExtendFile(db, pTask->file.pFd, pTask->file.iEof+pList->szPMA+9);
96330 vdbePmaWriterInit(pTask->file.pFd, &writer, pTask->pSorter->pgsz,
96331 pTask->file.iEof);
96332 pTask->nPMA++;
96333 vdbePmaWriteVarint(&writer, pList->szPMA);
96334 for(p=pList->pList; p; p=pNext){
96335 pNext = p->u.pNext;
96336 vdbePmaWriteVarint(&writer, p->nVal);
96337 vdbePmaWriteBlob(&writer, SRVAL(p), p->nVal);
96338 if( pList->aMemory==0 ) sqlite3_free(p);
96340 pList->pList = p;
96341 rc = vdbePmaWriterFinish(&writer, &pTask->file.iEof);
96345 assert( rc!=SQLITE_OK || pList->pList==0 );
96346 assert( rc!=SQLITE_OK || pTask->file.iEof==iSz );
96362 int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
96363 SortSubtask *pTask = pMerger->pTask;
96366 rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);
96377 pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
96378 pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];
96380 for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
96383 if( pReadr1->pFd==0 ){
96385 }else if( pReadr2->pFd==0 ){
96386 iRes = -1;
96388 iRes = pTask->xCompare(pTask, &bCached,
96389 pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
96395 ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
96400 ** was actually called above, then pTask->pUnpacked now contains
96409 pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
96410 pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
96413 if( pReadr1->pFd ) bCached = 0;
96414 pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
96415 pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
96418 *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
96421 return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
96426 ** The main routine for background threads that write level-0 PMAs.
96431 assert( pTask->bDone==0 );
96432 rc = vdbeSorterListToPMA(pTask, &pTask->list);
96433 pTask->bDone = 1;
96444 pSorter->bUsePMA = 1;
96445 return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
96450 int nWorker = (pSorter->nTask-1);
96454 pSorter->bUsePMA = 1;
96456 /* Select a sub-task to sort and flush the current list of in-memory
96457 ** records to disk. If the sorter is running in multi-threaded mode,
96458 ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
96459 ** the background thread from a sub-tasks previous turn is still running,
96460 ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
96461 ** fall back to using the final sub-task. The first (pSorter->nTask-1)
96462 ** sub-tasks are prefered as they use background threads - the final
96463 ** sub-task uses the main thread. */
96465 int iTest = (pSorter->iPrev + i + 1) % nWorker;
96466 pTask = &pSorter->aTask[iTest];
96467 if( pTask->bDone ){
96470 if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
96476 rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
96483 assert( pTask->pThread==0 && pTask->bDone==0 );
96484 assert( pTask->list.pList==0 );
96485 assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
96487 aMem = pTask->list.aMemory;
96489 pSorter->iPrev = (u8)(pTask - pSorter->aTask);
96490 pTask->list = pSorter->list;
96491 pSorter->list.pList = 0;
96492 pSorter->list.szPMA = 0;
96494 pSorter->list.aMemory = aMem;
96495 pSorter->nMemory = sqlite3MallocSize(aMem);
96496 }else if( pSorter->list.aMemory ){
96497 pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
96498 if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
96524 assert( pCsr->eCurType==CURTYPE_SORTER );
96525 pSorter = pCsr->uc.pSorter;
96526 getVarint32NR((const u8*)&pVal->z[1], t);
96528 pSorter->typeMask &= SORTER_TYPE_INTEGER;
96530 pSorter->typeMask &= SORTER_TYPE_TEXT;
96532 pSorter->typeMask = 0;
96540 ** If using the single large allocation mode (pSorter->aMemory!=0), then
96547 ** * The total memory allocated for the in-memory list is greater
96548 ** than (page-size * cache-size), or
96550 ** * The total memory allocated for the in-memory list is greater
96551 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
96553 nReq = pVal->n + sizeof(SorterRecord);
96554 nPMA = pVal->n + sqlite3VarintLen(pVal->n);
96555 if( pSorter->mxPmaSize ){
96556 if( pSorter->list.aMemory ){
96557 bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
96560 (pSorter->list.szPMA > pSorter->mxPmaSize)
96561 || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
96566 pSorter->list.szPMA = 0;
96567 pSorter->iMemory = 0;
96568 assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
96572 pSorter->list.szPMA += nPMA;
96573 if( nPMA>pSorter->mxKeysize ){
96574 pSorter->mxKeysize = nPMA;
96577 if( pSorter->list.aMemory ){
96578 int nMin = pSorter->iMemory + nReq;
96580 if( nMin>pSorter->nMemory ){
96582 sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
96583 int iListOff = -1;
96584 if( pSorter->list.pList ){
96585 iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
96588 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
96590 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
96593 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
96595 pSorter->list.aMemory = aNew;
96596 pSorter->nMemory = nNew;
96599 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
96600 pSorter->iMemory += ROUND8(nReq);
96601 if( pSorter->list.pList ){
96602 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
96609 pNew->u.pNext = pSorter->list.pList;
96612 memcpy(SRVAL(pNew), pVal->z, pVal->n);
96613 pNew->nVal = pVal->n;
96614 pSorter->list.pList = pNew;
96620 ** Read keys from pIncr->pMerger and populate pIncr->aFile[1]. The format
96622 ** except that the number-of-bytes varint is omitted from the start.
96627 i64 iStart = pIncr->iStartOff;
96628 SorterFile *pOut = &pIncr->aFile[1];
96629 SortSubtask *pTask = pIncr->pTask;
96630 MergeEngine *pMerger = pIncr->pMerger;
96632 assert( pIncr->bEof==0 );
96636 vdbePmaWriterInit(pOut->pFd, &writer, pTask->pSorter->pgsz, iStart);
96639 PmaReader *pReader = &pMerger->aReadr[ pMerger->aTree[1] ];
96640 int nKey = pReader->nKey;
96645 if( pReader->pFd==0 ) break;
96646 if( (iEof + nKey + sqlite3VarintLen(nKey))>(iStart + pIncr->mxSz) ) break;
96650 vdbePmaWriteBlob(&writer, pReader->aKey, nKey);
96651 assert( pIncr->pMerger->pTask==pTask );
96652 rc = vdbeMergeEngineStep(pIncr->pMerger, &dummy);
96655 rc2 = vdbePmaWriterFinish(&writer, &pOut->iEof);
96664 ** multi-threaded IncrMerger objects.
96669 pIncr->pTask->bDone = 1;
96678 assert( pIncr->bUseThread );
96679 return vdbeSorterCreateThread(pIncr->pTask, vdbeIncrPopulateThread, p);
96689 ** For single-threaded objects, this is accomplished by literally reading
96690 ** keys from pIncr->pMerger and repopulating aFile[0].
96692 ** For multi-threaded objects, all that is required is to wait until the
96704 if( pIncr->bUseThread ){
96705 rc = vdbeSorterJoinThread(pIncr->pTask);
96708 SorterFile f0 = pIncr->aFile[0];
96709 pIncr->aFile[0] = pIncr->aFile[1];
96710 pIncr->aFile[1] = f0;
96714 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
96715 pIncr->bEof = 1;
96724 pIncr->aFile[0] = pIncr->aFile[1];
96725 if( pIncr->aFile[0].iEof==pIncr->iStartOff ){
96726 pIncr->bEof = 1;
96748 pIncr->pMerger = pMerger;
96749 pIncr->pTask = pTask;
96750 pIncr->mxSz = MAX(pTask->pSorter->mxKeysize+9,pTask->pSorter->mxPmaSize/2);
96751 pTask->file2.iEof += pIncr->mxSz;
96761 ** Set the "use-threads" flag on object pIncr.
96764 pIncr->bUseThread = 1;
96765 pIncr->pTask->file2.iEof -= pIncr->mxSz;
96772 ** Recompute pMerger->aTree[iOut] by comparing the next keys on the
96778 int iOut /* Store the result in pMerger->aTree[iOut] */
96786 assert( iOut<pMerger->nTree && iOut>0 );
96788 if( iOut>=(pMerger->nTree/2) ){
96789 i1 = (iOut - pMerger->nTree/2) * 2;
96792 i1 = pMerger->aTree[iOut*2];
96793 i2 = pMerger->aTree[iOut*2+1];
96796 p1 = &pMerger->aReadr[i1];
96797 p2 = &pMerger->aReadr[i2];
96799 if( p1->pFd==0 ){
96801 }else if( p2->pFd==0 ){
96804 SortSubtask *pTask = pMerger->pTask;
96807 assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */
96808 res = pTask->xCompare(
96809 pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey
96818 pMerger->aTree[iOut] = iRes;
96825 ** Only INCRINIT_NORMAL is valid in single-threaded builds (when
96871 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
96875 assert( pMerger->pTask==0 );
96876 pMerger->pTask = pTask;
96878 nTree = pMerger->nTree;
96883 ** However, in the INCRINIT_ROOT case, if PmaReader aReadr[nTask-1] is
96886 ** on this PmaReader before any of the multi-threaded PmaReaders takes
96887 ** better advantage of multi-processor hardware. */
96888 rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]);
96890 rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL);
96895 for(i=pMerger->nTree-1; i>0; i--){
96898 return pTask->pUnpacked->errCode;
96903 ** incremental-reader (pReadr->pIncr!=0). This function serves to open
96905 ** object at (pReadr->pIncr).
96908 ** in the sub-tree headed by pReadr are also initialized. Data is then
96913 ** to be a multi-threaded PmaReader and this function is being called in a
96914 ** background thread. In this case all PmaReaders in the sub-tree are
96922 ** to block on thread (pTask->thread) before accessing aFile[1]. But, since
96923 ** this entire function is being run by thread (pTask->thread), that will
96927 ** that pReadr->pIncr is a multi-threaded IncrMerge objects, and that all
96928 ** child-trees have already been initialized using IncrInit(INCRINIT_TASK).
96936 IncrMerger *pIncr = pReadr->pIncr;
96937 SortSubtask *pTask = pIncr->pTask;
96938 sqlite3 *db = pTask->pSorter->db;
96940 /* eMode is always INCRINIT_NORMAL in single-threaded mode */
96943 rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode);
96945 /* Set up the required files for pIncr. A multi-theaded IncrMerge object
96946 ** requires two temp files to itself, whereas a single-threaded object
96947 ** only requires a region of pTask->file2. */
96949 int mxSz = pIncr->mxSz;
96951 if( pIncr->bUseThread ){
96952 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd);
96954 rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd);
96958 /*if( !pIncr->bUseThread )*/{
96959 if( pTask->file2.pFd==0 ){
96960 assert( pTask->file2.iEof>0 );
96961 rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd);
96962 pTask->file2.iEof = 0;
96965 pIncr->aFile[1].pFd = pTask->file2.pFd;
96966 pIncr->iStartOff = pTask->file2.iEof;
96967 pTask->file2.iEof += mxSz;
96973 if( rc==SQLITE_OK && pIncr->bUseThread ){
96975 ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object,
96977 ** pIncr->pTask->thread.
97006 pReader->pIncr->pTask->bDone = 1;
97012 ** If the PmaReader passed as the first argument is not an incremental-reader
97013 ** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes
97017 ** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1),
97023 IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */
97027 assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK );
97028 if( pIncr->bUseThread ){
97030 rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx);
97041 ** Allocate a new MergeEngine object to merge the contents of nPMA level-0
97042 ** PMAs from pTask->file. If no error occurs, set *ppOut to point to
97047 ** first PMA to read from pTask->file. Assuming no error occurs, it is
97055 i64 *piOffset, /* IN/OUT: Readr offset in pTask->file */
97056 MergeEngine **ppOut /* OUT: New merge-engine */
97068 PmaReader *pReadr = &pNew->aReadr[i];
97069 rc = vdbePmaReaderInit(pTask, &pTask->file, iOff, pReadr, &nDummy);
97070 iOff = pReadr->iEof;
97087 ** nPMA<=16 -> TreeDepth() == 0
97088 ** nPMA<=256 -> TreeDepth() == 1
97089 ** nPMA<=65536 -> TreeDepth() == 2
97102 ** pRoot is the root of an incremental merge-tree with depth nDepth (according
97130 PmaReader *pReadr = &p->aReadr[iIter];
97132 if( pReadr->pIncr==0 ){
97137 rc = vdbeIncrMergerNew(pTask, pNew, &pReadr->pIncr);
97141 p = pReadr->pIncr->pMerger;
97147 p->aReadr[iSeq % SORTER_MAX_MERGE_COUNT].pIncr = pIncr;
97156 ** that has already written two or more level-0 PMAs to one or more temp
97174 /* If the sorter uses more than one task, then create the top-level
97176 ** one PmaReader per sub-task. */
97177 assert( pSorter->bUseThreads || pSorter->nTask==1 );
97178 if( pSorter->nTask>1 ){
97179 pMain = vdbeMergeEngineNew(pSorter->nTask);
97184 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
97185 SortSubtask *pTask = &pSorter->aTask[iTask];
97186 assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
97187 if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
97189 int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
97192 if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
97193 rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
97199 for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
97200 MergeEngine *pMerger = 0; /* New level-0 PMA merger */
97201 int nReader; /* Number of level-0 PMAs to merge */
97203 nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
97214 rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
97239 ** (for multi-threaded sorters) so that it can be used to iterate through
97246 SortSubtask *pTask0 = &pSorter->aTask[0];
97249 sqlite3 *db = pTask0->pSorter->db;
97252 for(i=0; i<pSorter->nTask; i++){
97253 pSorter->aTask[i].xCompare = xCompare;
97260 assert( pSorter->bUseThreads==0 || pSorter->nTask>1 );
97261 if( pSorter->bUseThreads ){
97264 SortSubtask *pLast = &pSorter->aTask[pSorter->nTask-1];
97268 pSorter->pReader = pReadr;
97272 rc = vdbeIncrMergerNew(pLast, pMain, &pReadr->pIncr);
97274 vdbeIncrMergerSetThreads(pReadr->pIncr);
97275 for(iTask=0; iTask<(pSorter->nTask-1); iTask++){
97277 if( (pIncr = pMain->aReadr[iTask].pIncr) ){
97279 assert( pIncr->pTask!=pLast );
97282 for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
97287 ** b) If it is using task (nTask-1), it is configured to run
97288 ** in single-threaded mode. This is important, as the
97292 PmaReader *p = &pMain->aReadr[iTask];
97293 assert( p->pIncr==0 || (
97294 (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */
97295 && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */
97309 pSorter->pMerger = pMain;
97330 assert( pCsr->eCurType==CURTYPE_SORTER );
97331 pSorter = pCsr->uc.pSorter;
97336 ** from the in-memory list. */
97337 if( pSorter->bUsePMA==0 ){
97338 if( pSorter->list.pList ){
97340 rc = vdbeSorterSort(&pSorter->aTask[0], &pSorter->list);
97347 /* Write the current in-memory list to a PMA. When the VdbeSorterWrite()
97351 assert( pSorter->list.pList );
97361 assert( pSorter->pReader==0 );
97382 assert( pCsr->eCurType==CURTYPE_SORTER );
97383 pSorter = pCsr->uc.pSorter;
97384 assert( pSorter->bUsePMA || (pSorter->pReader==0 && pSorter->pMerger==0) );
97385 if( pSorter->bUsePMA ){
97386 assert( pSorter->pReader==0 || pSorter->pMerger==0 );
97387 assert( pSorter->bUseThreads==0 || pSorter->pReader );
97388 assert( pSorter->bUseThreads==1 || pSorter->pMerger );
97390 if( pSorter->bUseThreads ){
97391 rc = vdbePmaReaderNext(pSorter->pReader);
97392 if( rc==SQLITE_OK && pSorter->pReader->pFd==0 ) rc = SQLITE_DONE;
97395 /*if( !pSorter->bUseThreads )*/ {
97397 assert( pSorter->pMerger!=0 );
97398 assert( pSorter->pMerger->pTask==(&pSorter->aTask[0]) );
97399 rc = vdbeMergeEngineStep(pSorter->pMerger, &res);
97403 SorterRecord *pFree = pSorter->list.pList;
97404 pSorter->list.pList = pFree->u.pNext;
97405 pFree->u.pNext = 0;
97406 if( pSorter->list.aMemory==0 ) vdbeSorterRecordFree(db, pFree);
97407 rc = pSorter->list.pList ? SQLITE_OK : SQLITE_DONE;
97421 if( pSorter->bUsePMA ){
97424 if( pSorter->bUseThreads ){
97425 pReader = pSorter->pReader;
97428 /*if( !pSorter->bUseThreads )*/{
97429 pReader = &pSorter->pMerger->aReadr[pSorter->pMerger->aTree[1]];
97431 *pnKey = pReader->nKey;
97432 pKey = pReader->aKey;
97434 *pnKey = pSorter->list.pList->nVal;
97435 pKey = SRVAL(pSorter->list.pList);
97447 assert( pCsr->eCurType==CURTYPE_SORTER );
97448 pSorter = pCsr->uc.pSorter;
97453 pOut->n = nKey;
97455 memcpy(pOut->z, pKey, nKey);
97488 assert( pCsr->eCurType==CURTYPE_SORTER );
97489 pSorter = pCsr->uc.pSorter;
97490 r2 = pSorter->pUnpacked;
97491 pKeyInfo = pCsr->pKeyInfo;
97493 r2 = pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
97495 r2->nField = nKeyCol;
97497 assert( r2->nField==nKeyCol );
97502 if( r2->aMem[i].flags & MEM_Null ){
97503 *pRes = -1;
97508 *pRes = sqlite3VdbeRecordCompare(pVal->n, pVal->z, r2);
97515 ** 2020-03-23
97526 ** This file implements virtual-tables for examining the bytecode content
97533 /* An instance of the bytecode() table-valued function.
97537 sqlite3_vtab base; /* Base class - must be first */
97546 sqlite3_vtab_cursor base; /* Base class - must be first */
97561 ** Create a new bytecode() table-valued function.
97605 pNew->db = db;
97606 pNew->bTablesUsed = isTabUsed*2;
97629 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
97630 *ppCursor = &pCur->base;
97638 sqlite3_free(pCur->zP4);
97639 pCur->zP4 = 0;
97640 sqlite3VdbeMemRelease(&pCur->sub);
97641 sqlite3VdbeMemSetNull(&pCur->sub);
97642 if( pCur->needFinalize ){
97643 sqlite3_finalize(pCur->pStmt);
97645 pCur->pStmt = 0;
97646 pCur->needFinalize = 0;
97647 pCur->zType = 0;
97648 pCur->zSchema = 0;
97649 pCur->zName = 0;
97668 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
97670 if( pCur->zP4 ){
97671 sqlite3_free(pCur->zP4);
97672 pCur->zP4 = 0;
97674 if( pCur->zName ){
97675 pCur->zName = 0;
97676 pCur->zType = 0;
97677 pCur->zSchema = 0;
97680 (Vdbe*)pCur->pStmt,
97681 pCur->showSubprograms ? &pCur->sub : 0,
97682 pTab->bTablesUsed,
97683 &pCur->iRowid,
97684 &pCur->iAddr,
97685 &pCur->aOp);
97687 sqlite3VdbeMemSetNull(&pCur->sub);
97688 pCur->aOp = 0;
97699 return pCur->aOp==0;
97712 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
97713 Op *pOp = pCur->aOp + pCur->iAddr;
97714 if( pVTab->bTablesUsed ){
97718 if( i<=2 && pCur->zType==0 ){
97721 int iDb = pOp->p3;
97722 Pgno iRoot = (Pgno)pOp->p2;
97723 sqlite3 *db = pVTab->db;
97724 pSchema = db->aDb[iDb].pSchema;
97725 pCur->zSchema = db->aDb[iDb].zDbSName;
97726 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
97728 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
97729 pCur->zName = pTab->zName;
97730 pCur->zType = "table";
97734 if( pCur->zName==0 ){
97735 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
97737 if( pIdx->tnum==iRoot ){
97738 pCur->zName = pIdx->zName;
97739 pCur->zType = "index";
97749 sqlite3_result_int(ctx, pCur->iAddr);
97752 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
97753 -1, SQLITE_STATIC);
97756 sqlite3_result_int(ctx, pOp->p1);
97759 sqlite3_result_int(ctx, pOp->p2);
97762 sqlite3_result_int(ctx, pOp->p3);
97766 if( pCur->zP4==0 ){
97767 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
97770 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
97773 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
97774 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
97779 sqlite3_result_int(ctx, pOp->p5);
97782 Op *aOp = pCur->aOp;
97784 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
97785 if( pCur->iRowid==pCur->iAddr+1 ){
97788 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
97795 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
97798 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
97801 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
97804 sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
97816 *pRowid = pCur->iRowid;
97832 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
97836 pCur->iRowid = 0;
97837 pCur->iAddr = 0;
97838 pCur->showSubprograms = idxNum==0;
97845 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
97846 pCur->needFinalize = 1;
97849 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
97851 if( pCur->pStmt==0 ){
97852 pVTab->base.zErrMsg = sqlite3_mprintf(
97854 pVTab->bTablesUsed ? "tables_used" : "bytecode"
97876 int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
97877 pIdxInfo->estimatedCost = (double)100;
97878 pIdxInfo->estimatedRows = 100;
97879 pIdxInfo->idxNum = 0;
97880 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
97881 if( p->usable==0 ) continue;
97882 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
97884 pIdxInfo->aConstraintUsage[i].omit = 1;
97885 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
97887 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
97888 pIdxInfo->aConstraintUsage[i].omit = 1;
97889 pIdxInfo->idxNum = 1;
97953 ** This file contains code use to implement an in-memory rollback journal.
97954 ** The in-memory rollback journal is used to journal transactions for
97957 ** Update: The in-memory journal is also used to temporarily cache
97958 ** smaller journals that are not critical for power-loss recovery.
97976 ** The zChunk array is always at least 8 bytes in size - usually much more.
97993 #define fileChunkSize(nChunkSize) (sizeof(FileChunk) + ((nChunkSize)-8))
98005 ** This structure is a subclass of sqlite3_file. Each open memory-journal
98010 int nChunkSize; /* In-memory chunk-size */
98013 FileChunk *pFirst; /* Head of in-memory chunk-list */
98023 ** Read data from the in-memory journal file. This is the implementation
98038 if( (iAmt+iOfst)>p->endpoint.iOffset ){
98041 assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
98042 if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
98044 for(pChunk=p->pFirst;
98045 ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
98046 pChunk=pChunk->pNext
98048 iOff += p->nChunkSize;
98051 pChunk = p->readpoint.pChunk;
98055 iChunkOffset = (int)(iOfst%p->nChunkSize);
98057 int iSpace = p->nChunkSize - iChunkOffset;
98058 int nCopy = MIN(nRead, (p->nChunkSize - iChunkOffset));
98059 memcpy(zOut, (u8*)pChunk->zChunk + iChunkOffset, nCopy);
98061 nRead -= iSpace;
98063 } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
98064 p->readpoint.iOffset = pChunk ? iOfst+iAmt : 0;
98065 p->readpoint.pChunk = pChunk;
98077 pNext = pIter->pNext;
98096 for(pIter=copy.pFirst; pIter; pIter=pIter->pNext){
98098 nChunk = copy.endpoint.iOffset - iOff;
98100 rc = sqlite3OsWrite(pReal, (u8*)pIter->zChunk, nChunk, iOff);
98105 /* No error has occurred. Free the in-memory buffers. */
98111 ** the original before returning. This way, SQLite uses the in-memory
98112 ** journal data to roll back changes made to the internal page-cache
98136 if( p->nSpill>0 && (iAmt+iOfst)>p->nSpill ){
98146 /* An in-memory journal file should only ever be appended to. Random
98148 ** the in-memory journal is being used by a connection using the
98149 ** atomic-write optimization. In this case the first 28 bytes of the
98151 assert( iOfst==p->endpoint.iOffset || iOfst==0 );
98154 if( iOfst==0 && p->pFirst ){
98155 assert( p->nChunkSize>iAmt );
98156 memcpy((u8*)p->pFirst->zChunk, zBuf, iAmt);
98159 assert( iOfst>0 || p->pFirst==0 );
98163 FileChunk *pChunk = p->endpoint.pChunk;
98164 int iChunkOffset = (int)(p->endpoint.iOffset%p->nChunkSize);
98165 int iSpace = MIN(nWrite, p->nChunkSize - iChunkOffset);
98169 FileChunk *pNew = sqlite3_malloc(fileChunkSize(p->nChunkSize));
98173 pNew->pNext = 0;
98175 assert( p->pFirst );
98176 pChunk->pNext = pNew;
98178 assert( !p->pFirst );
98179 p->pFirst = pNew;
98181 p->endpoint.pChunk = pNew;
98184 memcpy((u8*)p->endpoint.pChunk->zChunk + iChunkOffset, zWrite, iSpace);
98186 nWrite -= iSpace;
98187 p->endpoint.iOffset += iSpace;
98196 ** Truncate the in-memory file.
98203 memjrnlFreeChunks(p->pFirst);
98204 p->pFirst = 0;
98206 i64 iOff = p->nChunkSize;
98207 for(pIter=p->pFirst; ALWAYS(pIter) && iOff<=size; pIter=pIter->pNext){
98208 iOff += p->nChunkSize;
98211 memjrnlFreeChunks(pIter->pNext);
98212 pIter->pNext = 0;
98216 p->endpoint.pChunk = pIter;
98217 p->endpoint.iOffset = size;
98218 p->readpoint.pChunk = 0;
98219 p->readpoint.iOffset = 0;
98228 memjrnlFreeChunks(p->pFirst);
98236 ** syncing an in-memory journal is a no-op.
98248 *pSize = (sqlite_int64) p->endpoint.iOffset;
98283 ** all content is always stored in main-memory. Finally, if nSpill is a
98284 ** positive value, then the journal file is initially created in-memory
98298 /* Zero the file-handle object. If nSpill was passed zero, initialize
98301 ** made on the journal file-handle. */
98308 p->nChunkSize = nSpill;
98310 p->nChunkSize = 8 + MEMJOURNAL_DFLT_FILECHUNKSIZE - sizeof(FileChunk);
98311 assert( MEMJOURNAL_DFLT_FILECHUNKSIZE==fileChunkSize(p->nChunkSize) );
98314 pJfd->pMethods = (const sqlite3_io_methods*)&MemJournalMethods;
98315 p->nSpill = nSpill;
98316 p->flags = flags;
98317 p->zJournal = zName;
98318 p->pVfs = pVfs;
98323 ** Open an in-memory journal file.
98326 sqlite3JournalOpen(0, 0, pJfd, 0, -1);
98333 ** in-memory-only journal file (i.e. is one that was opened with a +ve
98340 if( pJfd->pMethods==&MemJournalMethods && (
98342 p->nSpill>0
98347 NEVER(p->nSpill>0)
98350 || (p->flags & SQLITE_OPEN_MAIN_JOURNAL)
98360 ** The file-handle passed as the only argument is open on a journal file.
98365 return p->pMethods==&MemJournalMethods;
98370 ** pVfs to create the underlying on-disk files.
98373 return MAX(pVfs->szOsFile, (int)sizeof(MemJournal));
98404 for(pWin=pList; pWin; pWin=pWin->pNextWin){
98406 rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
98408 rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
98410 rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
98417 rc = sqlite3WalkExpr(pWalker, pWin->pStart);
98419 rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
98441 ** return from the top-level walk call.
98451 rc = pWalker->xExprCallback(pWalker, pExpr);
98454 assert( pExpr->x.pList==0 || pExpr->pRight==0 );
98455 if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
98456 if( pExpr->pRight ){
98458 pExpr = pExpr->pRight;
98462 if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
98464 if( pExpr->x.pList ){
98465 if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
98469 if( walkWindowList(pWalker, pExpr->y.pWin, 1) ) return WRC_Abort;
98490 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
98491 if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
98504 if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
98505 if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
98506 if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
98507 if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
98508 if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
98509 if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
98512 Parse *pParse = pWalker->pParse;
98516 int rc = walkWindowList(pWalker, p->pWinDefn, 0);
98536 pSrc = p->pSrc;
98538 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
98539 if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
98542 if( pItem->fg.isTabFunc
98543 && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
98555 ** on the compound select chain, p->pPrior.
98560 ** but only if both xSelectCallback and xSelectCallback2 are both non-NULL
98567 ** is a no-op returning WRC_Continue.
98572 if( pWalker->xSelectCallback==0 ) return WRC_Continue;
98574 rc = pWalker->xSelectCallback(pWalker, p);
98581 if( pWalker->xSelectCallback2 ){
98582 pWalker->xSelectCallback2(pWalker, p);
98584 p = p->pPrior;
98594 pWalker->walkerDepth++;
98599 pWalker->walkerDepth--;
98604 ** No-op routine for the parse-tree walker.
98618 ** No-op routine for the parse-tree walker for SELECT statements.
98658 ** is a helper function - a callback for the tree walker.
98663 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
98677 ** Turn the pExpr expression into an alias for the iCol-th column of the
98698 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
98702 Expr *pOrig; /* The iCol-th column of the result set */
98706 assert( iCol>=0 && iCol<pEList->nExpr );
98707 pOrig = pEList->a[iCol].pExpr;
98709 db = pParse->db;
98713 if( pExpr->op==TK_COLLATE ){
98714 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
98720 ** The pExpr->u.zToken might point into memory that will be freed by the
98727 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
98728 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
98729 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
98730 pExpr->flags |= EP_MemToken;
98733 if( pExpr->y.pWin!=0 ){
98734 pExpr->y.pWin->pOwner = pExpr;
98736 assert( db->mallocFailed );
98753 for(k=0; k<pUsing->nId; k++){
98754 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
98775 if( pItem->eEName!=ENAME_TAB ) return 0;
98776 zSpan = pItem->zEName;
98794 ** Return TRUE if the double-quoted string mis-feature should be supported.
98797 if( db->init.busy ) return 1; /* Always support for legacy schemas */
98798 if( pTopNC->ncFlags & NC_IsDDL ){
98800 if( sqlite3WritableSchema(db) && (db->flags & SQLITE_DqsDML)!=0 ){
98803 return (db->flags & SQLITE_DqsDDL)!=0;
98806 return (db->flags & SQLITE_DqsDML)!=0;
98811 ** The argument is guaranteed to be a non-NULL Expr node of type TK_COLUMN.
98818 n = pExpr->iColumn;
98819 pExTab = pExpr->y.pTab;
98821 if( (pExTab->tabFlags & TF_HasGenerated)!=0
98822 && (pExTab->aCol[n].colFlags & COLFLAG_GENERATED)!=0
98824 testcase( pExTab->nCol==BMS-1 );
98825 testcase( pExTab->nCol==BMS );
98826 return pExTab->nCol>=BMS ? ALLBITS : MASKBIT(pExTab->nCol)-1;
98828 testcase( n==BMS-1 );
98830 if( n>=BMS ) n = BMS-1;
98841 ** pExpr->iDb Set the index in db->aDb[] of the database X
98843 ** pExpr->iTable Set to the cursor number for the table obtained
98845 ** pExpr->y.pTab Points to the Table structure of X.Y (even if
98847 ** pExpr->iColumn Set to the column number within the table.
98848 ** pExpr->op Set to TK_COLUMN.
98849 ** pExpr->pLeft Any expression this points to is deleted
98850 ** pExpr->pRight Any expression this points to is deleted.
98859 ** If the name cannot be resolved unambiguously, leave an error message
98874 sqlite3 *db = pParse->db; /* The database connection */
98879 int eNewExprOp = TK_COLUMN; /* New value for pExpr->op on success */
98887 /* Initialize the node to no-match */
98888 pExpr->iTable = -1;
98893 ** resulting in an appropriate error message toward the end of this routine
98896 testcase( pNC->ncFlags & NC_PartIdx );
98897 testcase( pNC->ncFlags & NC_IsCheck );
98898 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
98905 for(i=0; i<db->nDb; i++){
98906 assert( db->aDb[i].zDbSName );
98907 if( sqlite3StrICmp(db->aDb[i].zDbSName,zDb)==0 ){
98908 pSchema = db->aDb[i].pSchema;
98912 if( i==db->nDb && sqlite3StrICmp("main", zDb)==0 ){
98915 pSchema = db->aDb[0].pSchema;
98916 zDb = db->aDb[0].zDbSName;
98921 /* Start at the inner-most context and move outward until a match is found */
98925 SrcList *pSrcList = pNC->pSrcList;
98928 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
98930 pTab = pItem->pTab;
98931 assert( pTab!=0 && pTab->zName!=0 );
98932 assert( pTab->nCol>0 );
98933 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
98935 pEList = pItem->pSelect->pEList;
98936 for(j=0; j<pEList->nExpr; j++){
98937 if( sqlite3MatchEName(&pEList->a[j], zCol, zTab, zDb) ){
98941 pExpr->iColumn = j;
98947 if( zDb && pTab->pSchema!=pSchema ){
98951 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
98956 if( IN_RENAME_OBJECT && pItem->zAlias ){
98957 sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->y.pTab);
98964 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
98965 if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
98967 ** is for the right-hand table of a NATURAL JOIN or is in a
98971 if( pItem->fg.jointype & JT_NATURAL ) continue;
98972 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
98976 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
98977 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
98983 pExpr->iTable = pMatch->iCursor;
98984 pExpr->y.pTab = pMatch->pTab;
98986 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
98987 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
98990 pSchema = pExpr->y.pTab->pSchema;
99003 if( pParse->pTriggerTab!=0 ){
99004 int op = pParse->eTriggerOp;
99006 if( pParse->bReturning ){
99007 if( (pNC->ncFlags & NC_UBaseReg)!=0
99008 && (zTab==0 || sqlite3StrICmp(zTab,pParse->pTriggerTab->zName)==0)
99010 pExpr->iTable = op!=TK_DELETE;
99011 pTab = pParse->pTriggerTab;
99014 pExpr->iTable = 1;
99015 pTab = pParse->pTriggerTab;
99017 pExpr->iTable = 0;
99018 pTab = pParse->pTriggerTab;
99023 if( (pNC->ncFlags & NC_UUpsert)!=0 && zTab!=0 ){
99024 Upsert *pUpsert = pNC->uNC.pUpsert;
99026 pTab = pUpsert->pUpsertSrc->a[0].pTab;
99027 pExpr->iTable = EXCLUDED_TABLE_NUMBER;
99035 pSchema = pTab->pSchema;
99037 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
99038 if( pCol->hName==hCol && sqlite3StrICmp(pCol->zName, zCol)==0 ){
99039 if( iCol==pTab->iPKey ){
99040 iCol = -1;
99045 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
99046 /* IMP: R-51414-32910 */
99047 iCol = -1;
99049 if( iCol<pTab->nCol ){
99053 if( pExpr->iTable==EXCLUDED_TABLE_NUMBER ){
99054 testcase( iCol==(-1) );
99056 pExpr->iColumn = iCol;
99057 pExpr->y.pTab = pTab;
99060 pExpr->iTable = pNC->uNC.pUpsert->regData +
99067 pExpr->y.pTab = pTab;
99068 if( pParse->bReturning ){
99070 pExpr->iTable = pNC->uNC.iBaseReg + (pTab->nCol+1)*pExpr->iTable +
99073 pExpr->iColumn = (i16)iCol;
99077 pExpr->affExpr = SQLITE_AFF_INTEGER;
99078 }else if( pExpr->iTable==0 ){
99081 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
99085 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
99101 && (pNC->ncFlags & (NC_IdxExpr|NC_GenCol))==0
99103 && VisibleRowid(pMatch->pTab)
99106 pExpr->iColumn = -1;
99107 pExpr->affExpr = SQLITE_AFF_INTEGER;
99112 ** might refer to an result-set alias. This happens, for example, when
99122 ** The ability to use an output result-set column in the WHERE, GROUP BY,
99128 if( (pNC->ncFlags & NC_UEList)!=0
99132 pEList = pNC->uNC.pEList;
99134 for(j=0; j<pEList->nExpr; j++){
99135 char *zAs = pEList->a[j].zEName;
99136 if( pEList->a[j].eEName==ENAME_NAME
99140 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
99141 assert( pExpr->x.pList==0 );
99142 assert( pExpr->x.pSelect==0 );
99143 pOrig = pEList->a[j].pExpr;
99144 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
99149 && ((pNC->ncFlags&NC_AllowWin)==0 || pNC!=pTopNC )
99174 pNC = pNC->pNext;
99181 ** supplied) and the value of Z is enclosed in double-quotes, then
99186 ** Because no reference was made to outer contexts, the pNC->nRef
99190 assert( pExpr->op==TK_ID );
99193 && (db->init.bDropColumn==0 || sqlite3StrICmp(zCol, db->init.azInit[0])!=0)
99195 /* If a double-quoted identifier does not match any known column name,
99199 ** to be compatible with MySQL 3.x, which used double-quotes for strings.
99209 ** 2021-03-15: ticket 1c24a659e6d7f3a1
99210 ** Do not do the ID-to-STRING conversion when doing the schema
99215 "double-quoted string literal: \"%w\"", zCol);
99217 sqlite3VdbeAddDblquoteStr(db, pParse->pVdbe, zCol);
99219 pExpr->op = TK_STRING;
99220 pExpr->y.pTab = 0;
99242 pParse->checkSchema = 1;
99243 pTopNC->nErr++;
99260 if( pExpr->iColumn>=0 && pMatch!=0 ){
99261 pMatch->colUsed |= sqlite3ExprColUsed(pExpr);
99267 sqlite3ExprDelete(db, pExpr->pLeft);
99268 pExpr->pLeft = 0;
99269 sqlite3ExprDelete(db, pExpr->pRight);
99270 pExpr->pRight = 0;
99272 pExpr->op = eNewExprOp;
99278 if( pParse->db->xAuth
99279 && (pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER)
99281 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
99288 pTopNC->nRef++;
99290 pTopNC = pTopNC->pNext;
99305 SrcItem *pItem = &pSrc->a[iSrc];
99306 Table *pTab = p->y.pTab = pItem->pTab;
99307 p->iTable = pItem->iCursor;
99308 if( p->y.pTab->iPKey==iCol ){
99309 p->iColumn = -1;
99311 p->iColumn = (ynVar)iCol;
99312 if( (pTab->tabFlags & TF_HasGenerated)!=0
99313 && (pTab->aCol[iCol].colFlags & COLFLAG_GENERATED)!=0
99315 testcase( pTab->nCol==63 );
99316 testcase( pTab->nCol==64 );
99317 pItem->colUsed = pTab->nCol>=64 ? ALLBITS : MASKBIT(pTab->nCol)-1;
99320 testcase( iCol==BMS-1 );
99321 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
99330 ** pNC->ncFlags values determined by validMask.
99333 ** Parse *pParse, // Leave error message here
99345 Parse *pParse, /* Leave error message here */
99351 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
99353 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
99356 else if( pNC->ncFlags & NC_GenCol ) zIn = "generated columns";
99359 if( pExpr ) pExpr->op = TK_NULL;
99363 if( ((N)->ncFlags & (X))!=0 ) notValidImpl(P,N,M,E);
99367 ** Return 1024 times this value. Or return -1 if p is not a floating point
99371 double r = -1.0;
99372 if( p->op!=TK_FLOAT ) return -1;
99373 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
99375 if( r>1.0 ) return -1;
99394 pNC = pWalker->u.pNC;
99396 pParse = pNC->pParse;
99397 assert( pParse==pWalker->pParse );
99400 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
99401 SrcList *pSrcList = pNC->pSrcList;
99403 for(i=0; i<pNC->pSrcList->nSrc; i++){
99404 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
99408 switch( pExpr->op ){
99416 SrcList *pSrcList = pNC->pSrcList;
99418 assert( pSrcList && pSrcList->nSrc>=1 );
99419 pItem = pSrcList->a;
99420 pExpr->op = TK_COLUMN;
99421 pExpr->y.pTab = pItem->pTab;
99422 pExpr->iTable = pItem->iCursor;
99423 pExpr->iColumn--;
99424 pExpr->affExpr = SQLITE_AFF_INTEGER;
99430 ** "expr IS NOT NULL" --> "TRUE"
99431 ** "expr IS NULL" --> "FALSE"
99436 ** If this optimization occurs, also restore the NameContext ref-counts
99447 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
99448 anRef[i] = p->nRef;
99450 sqlite3WalkExpr(pWalker, pExpr->pLeft);
99451 if( 0==sqlite3ExprCanBeNull(pExpr->pLeft) && !IN_RENAME_OBJECT ){
99452 if( pExpr->op==TK_NOTNULL ){
99453 pExpr->u.zToken = "true";
99456 pExpr->u.zToken = "false";
99459 pExpr->op = TK_TRUEFALSE;
99460 for(i=0, p=pNC; p && i<ArraySize(anRef); p=p->pNext, i++){
99461 p->nRef = anRef[i];
99463 sqlite3ExprDelete(pParse->db, pExpr->pLeft);
99464 pExpr->pLeft = 0;
99474 ** be one call to lookupName(). Then the compiler will in-line
99484 if( pExpr->op==TK_ID ){
99487 zColumn = pExpr->u.zToken;
99489 Expr *pLeft = pExpr->pLeft;
99490 testcase( pNC->ncFlags & NC_IdxExpr );
99491 testcase( pNC->ncFlags & NC_GenCol );
99494 pRight = pExpr->pRight;
99495 if( pRight->op==TK_ID ){
99498 assert( pRight->op==TK_DOT );
99499 zDb = pLeft->u.zToken;
99500 pLeft = pRight->pLeft;
99501 pRight = pRight->pRight;
99503 zTable = pLeft->u.zToken;
99504 zColumn = pRight->u.zToken;
99507 sqlite3RenameTokenRemap(pParse, (void*)&pExpr->y.pTab, (void*)pLeft);
99516 ExprList *pList = pExpr->x.pList; /* The argument list */
99517 int n = pList ? pList->nExpr : 0; /* Number of arguments */
99524 u8 enc = ENC(pParse->db); /* The database encoding */
99525 int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
99527 Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
99530 zId = pExpr->u.zToken;
99532 pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
99534 pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
99541 is_agg = pDef->xFinalize!=0;
99542 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
99545 pExpr->iTable = exprProbability(pList->a[1].pExpr);
99546 if( pExpr->iTable<0 ){
99550 pNC->nErr++;
99553 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
99555 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
99556 ** short-hand for likelihood(X,0.0625).
99557 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
99559 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
99562 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
99567 int auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0,pDef->zName,0);
99571 pDef->zName);
99572 pNC->nErr++;
99574 pExpr->op = TK_NULL;
99579 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
99586 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
99587 /* Clearly non-deterministic functions like random(), but also
99593 sqlite3ResolveNotValid(pParse, pNC, "non-deterministic functions",
99597 pExpr->op2 = pNC->ncFlags & NC_SelfRef;
99598 if( pNC->ncFlags & NC_FromDDL ) ExprSetProperty(pExpr, EP_FromDDL);
99600 if( (pDef->funcFlags & SQLITE_FUNC_INTERNAL)!=0
99601 && pParse->nested==0
99602 && (pParse->db->mDbFlags & DBFLAG_InternalFunc)==0
99604 /* Internal-use-only functions are disallowed unless the
99606 ** the SQLITE_TESTCTRL_INTERNAL_FUNCTIONS test-control has be
99611 if( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0
99620 assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
99621 || (pDef->xValue==0 && pDef->xInverse==0)
99622 || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
99624 if( pDef && pDef->xValue==0 && pWin ){
99628 pNC->nErr++;
99630 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
99631 || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
99632 || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
99635 if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
99641 pNC->nErr++;
99645 if( (is_agg && (pNC->ncFlags & NC_AllowAgg)==0) ){
99647 pNC->nErr++;
99651 else if( no_such_func && pParse->db->init.busy==0
99653 && pParse->explain==0
99657 pNC->nErr++;
99661 pNC->nErr++;
99666 "FILTER may not be used with non-aggregate %.*s()",
99669 pNC->nErr++;
99677 pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
99679 pNC->ncFlags &= ~NC_AllowAgg;
99692 Select *pSel = pNC->pWinSelect;
99693 assert( pWin==pExpr->y.pWin );
99695 sqlite3WindowUpdate(pParse, pSel ? pSel->pWinDefn : 0, pWin, pDef);
99696 if( pParse->db->mallocFailed ) break;
99698 sqlite3WalkExprList(pWalker, pWin->pPartition);
99699 sqlite3WalkExprList(pWalker, pWin->pOrderBy);
99700 sqlite3WalkExpr(pWalker, pWin->pFilter);
99702 pNC->ncFlags |= NC_HasWin;
99707 pExpr->op = TK_AGG_FUNCTION;
99708 pExpr->op2 = 0;
99711 sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
99714 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
99715 pExpr->op2++;
99716 pNC2 = pNC2->pNext;
99721 testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
99722 pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
99726 pNC->ncFlags |= savedAllowFlags;
99728 /* FIX ME: Compute pExpr->affinity based on the expected return
99735 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
99738 testcase( pExpr->op==TK_IN );
99740 int nRef = pNC->nRef;
99741 testcase( pNC->ncFlags & NC_IsCheck );
99742 testcase( pNC->ncFlags & NC_PartIdx );
99743 testcase( pNC->ncFlags & NC_IdxExpr );
99744 testcase( pNC->ncFlags & NC_GenCol );
99747 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
99748 assert( pNC->nRef>=nRef );
99749 if( nRef!=pNC->nRef ){
99751 pNC->ncFlags |= NC_VarSelect;
99757 testcase( pNC->ncFlags & NC_IsCheck );
99758 testcase( pNC->ncFlags & NC_PartIdx );
99759 testcase( pNC->ncFlags & NC_IdxExpr );
99760 testcase( pNC->ncFlags & NC_GenCol );
99767 Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
99771 if( ALWAYS(pRight) && (pRight->op==TK_ID || pRight->op==TK_TRUEFALSE) ){
99774 if( pRight->op==TK_TRUEFALSE ){
99775 pExpr->op2 = pExpr->op;
99776 pExpr->op = TK_TRUTH;
99790 if( pParse->db->mallocFailed ) break;
99791 assert( pExpr->pLeft!=0 );
99792 nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
99793 if( pExpr->op==TK_BETWEEN ){
99794 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
99796 nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
99799 assert( pExpr->pRight!=0 );
99800 nRight = sqlite3ExprVectorSize(pExpr->pRight);
99803 testcase( pExpr->op==TK_EQ );
99804 testcase( pExpr->op==TK_NE );
99805 testcase( pExpr->op==TK_LT );
99806 testcase( pExpr->op==TK_LE );
99807 testcase( pExpr->op==TK_GT );
99808 testcase( pExpr->op==TK_GE );
99809 testcase( pExpr->op==TK_IS );
99810 testcase( pExpr->op==TK_ISNOT );
99811 testcase( pExpr->op==TK_BETWEEN );
99817 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
99824 ** to the AS-name of one of the terms of the expression list. If it is,
99841 if( pE->op==TK_ID ){
99842 char *zCol = pE->u.zToken;
99843 for(i=0; i<pEList->nExpr; i++){
99844 if( pEList->a[i].eEName==ENAME_NAME
99845 && sqlite3_stricmp(pEList->a[i].zEName, zCol)==0
99863 ** Attempt to match pE against result set columns in the left-most
99865 ** as an indication to the caller that it should sort by the i-th column.
99866 ** The left-most column is 1. In other words, the value returned is the
99870 ** If there is no match, return 0. Return -1 if an error occurs.
99882 u8 savedSuppErr; /* Saved value of db->suppressErr */
99885 pEList = pSelect->pEList;
99891 nc.pSrcList = pSelect->pSrc;
99895 db = pParse->db;
99896 savedSuppErr = db->suppressErr;
99897 if( IN_RENAME_OBJECT==0 ) db->suppressErr = 1;
99899 db->suppressErr = savedSuppErr;
99903 ** in the result set. Return an 1-based index of the matching
99904 ** result-set entry.
99906 for(i=0; i<pEList->nExpr; i++){
99907 if( sqlite3ExprCompare(0, pEList->a[i].pExpr, pE, -1)<2 ){
99917 ** Generate an ORDER BY or GROUP BY term out-of-range error.
99922 int i, /* The index (1-based) of the term out of range */
99926 "%r %s BY term out of range - should be "
99939 ** beginning with the left-most SELECT and working toward the right.
99955 pOrderBy = pSelect->pOrderBy;
99957 db = pParse->db;
99958 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
99962 for(i=0; i<pOrderBy->nExpr; i++){
99963 pOrderBy->a[i].done = 0;
99965 pSelect->pNext = 0;
99966 while( pSelect->pPrior ){
99967 pSelect->pPrior->pNext = pSelect;
99968 pSelect = pSelect->pPrior;
99973 pEList = pSelect->pEList;
99975 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
99976 int iCol = -1;
99978 if( pItem->done ) continue;
99979 pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
99982 if( iCol<=0 || iCol>pEList->nExpr ){
99983 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
100007 if( !db->mallocFailed ){
100022 pNew->flags |= EP_IntValue;
100023 pNew->u.iValue = iCol;
100024 if( pItem->pExpr==pE ){
100025 pItem->pExpr = pNew;
100027 Expr *pParent = pItem->pExpr;
100028 assert( pParent->op==TK_COLLATE );
100029 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
100030 assert( pParent->pLeft==pE );
100031 pParent->pLeft = pNew;
100034 pItem->u.x.iOrderByCol = (u16)iCol;
100036 pItem->done = 1;
100041 pSelect = pSelect->pNext;
100043 for(i=0; i<pOrderBy->nExpr; i++){
100044 if( pOrderBy->a[i].done==0 ){
100060 ** If any errors are detected, add an error message to pParse and
100061 ** return non-zero. Return zero if no errors are seen.
100070 sqlite3 *db = pParse->db;
100074 if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
100075 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
100079 pEList = pSelect->pEList;
100081 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
100082 if( pItem->u.x.iOrderByCol ){
100083 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
100084 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
100087 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,0);
100100 Window *pWin = pExpr->y.pWin;
100111 if( pSelect->pWin ){
100129 ** If the order-by term is an integer I between 1 and N (where N is the
100131 ** in the resolution is a copy of the I-th result-set expression. If
100132 ** the order-by term is an identifier that corresponds to the AS-name of
100133 ** a result-set expression, then the term resolves to a copy of the
100134 ** result-set expression. Otherwise, the expression is resolved in
100135 ** the usual way - using sqlite3ResolveExprNames().
100138 ** an appropriate error message might be left in pParse. (OOM errors
100154 nResult = pSelect->pEList->nExpr;
100155 pParse = pNC->pParse;
100156 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
100157 Expr *pE = pItem->pExpr;
100161 iCol = resolveAsName(pParse, pSelect->pEList, pE2);
100163 /* If an AS-name match is found, mark this ORDER BY column as being
100164 ** a copy of the iCol-th result-set column. The subsequent call to
100166 ** copy of the iCol-th result-set expression. */
100167 pItem->u.x.iOrderByCol = (u16)iCol;
100174 ** order-by term to a copy of the result-set expression */
100179 pItem->u.x.iOrderByCol = (u16)iCol;
100184 pItem->u.x.iOrderByCol = 0;
100188 for(j=0; j<pSelect->pEList->nExpr; j++){
100189 if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
100194 pItem->u.x.iOrderByCol = j+1;
100212 Select *pLeftmost; /* Left-most of SELECT of a compound */
100217 if( p->selFlags & SF_Resolved ){
100220 pOuterNC = pWalker->u.pNC;
100221 pParse = pWalker->pParse;
100222 db = pParse->db;
100232 if( (p->selFlags & SF_Expanded)==0 ){
100234 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
100237 isCompound = p->pPrior!=0;
100241 assert( (p->selFlags & SF_Expanded)!=0 );
100242 assert( (p->selFlags & SF_Resolved)==0 );
100243 p->selFlags |= SF_Resolved;
100251 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
100257 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
100258 ** as if it were part of the sub-query, not the parent. This block
100259 ** moves the pOrderBy down to the sub-query. It will be moved back
100261 if( p->selFlags & SF_Converted ){
100262 Select *pSub = p->pSrc->a[0].pSelect;
100263 assert( p->pSrc->nSrc==1 && p->pOrderBy );
100264 assert( pSub->pPrior && pSub->pOrderBy==0 );
100265 pSub->pOrderBy = p->pOrderBy;
100266 p->pOrderBy = 0;
100271 for(i=0; i<p->pSrc->nSrc; i++){
100272 SrcItem *pItem = &p->pSrc->a[i];
100273 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_Resolved)==0 ){
100274 int nRef = pOuterNC ? pOuterNC->nRef : 0;
100275 const char *zSavedContext = pParse->zAuthContext;
100277 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
100278 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
100279 pParse->zAuthContext = zSavedContext;
100280 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
100283 ** expressions in the sub-select were resolved, the sub-select
100289 assert( pItem->fg.isCorrelated==0 && pOuterNC->nRef>=nRef );
100290 pItem->fg.isCorrelated = (pOuterNC->nRef>nRef);
100295 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
100296 ** resolve the result-set expression list.
100299 sNC.pSrcList = p->pSrc;
100303 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
100306 /* If there are no aggregate functions in the result-set, and no GROUP BY
100309 assert( (p->selFlags & SF_Aggregate)==0 );
100310 pGroupBy = p->pGroupBy;
100313 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
100320 if( p->pHaving && !pGroupBy ){
100325 /* Add the output column list to the name-context before parsing the
100331 ** re-evaluated for each reference to it.
100334 sNC.uNC.pEList = p->pEList;
100336 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
100337 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
100339 /* Resolve names in table-valued-function arguments */
100340 for(i=0; i<p->pSrc->nSrc; i++){
100341 SrcItem *pItem = &p->pSrc->a[i];
100342 if( pItem->fg.isTabFunc
100343 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
100356 ** the sub-query back to the parent query. At this point each term
100360 if( p->selFlags & SF_Converted ){
100361 Select *pSub = p->pSrc->a[0].pSelect;
100362 p->pOrderBy = pSub->pOrderBy;
100363 pSub->pOrderBy = 0;
100368 ** below, after all of the result-sets for all of the elements of
100371 ** If there is an ORDER BY clause on a term of a compound-select other
100372 ** than the right-most term, then that is a syntax error. But the error
100376 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
100377 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
100381 if( db->mallocFailed ){
100392 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
100395 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
100396 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
100407 for(pWin=p->pWinDefn; pWin; pWin=pWin->pNextWin){
100408 if( sqlite3ResolveExprListNames(&sNC, pWin->pOrderBy)
100409 || sqlite3ResolveExprListNames(&sNC, pWin->pPartition)
100419 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
100420 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
100426 p = p->pPrior;
100442 ** table columns and result-set columns. At the same time, do error
100450 ** the symbolic name assigned to an ATTACH-ed database.
100461 ** Expr.iColumn The column index in X.Y. -1 for the rowid.
100465 ** To resolve result-set references, look for expression nodes of the
100466 ** form Z (with no X and Y prefix) where the Z matches the right-hand
100467 ** size of an AS clause in the result-set of a SELECT. The Z expression
100468 ** is replaced by a copy of the left-hand side of the result-set expression.
100469 ** Table-name and function resolution occurs on the substituted expression
100485 ** An error message is left in pParse if anything is amiss. The number
100496 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100497 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100498 w.pParse = pNC->pParse;
100504 w.pParse->nHeight += pExpr->nHeight;
100505 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
100511 w.pParse->nHeight -= pExpr->nHeight;
100515 testcase( pNC->ncFlags & NC_HasAgg );
100516 testcase( pNC->ncFlags & NC_HasWin );
100517 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
100518 pNC->ncFlags |= savedHasAgg;
100519 return pNC->nErr>0 || w.pParse->nErr>0;
100535 w.pParse = pNC->pParse;
100540 savedHasAgg = pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100541 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100542 for(i=0; i<pList->nExpr; i++){
100543 Expr *pExpr = pList->a[i].pExpr;
100546 w.pParse->nHeight += pExpr->nHeight;
100547 if( sqlite3ExprCheckHeight(w.pParse, w.pParse->nHeight) ){
100553 w.pParse->nHeight -= pExpr->nHeight;
100557 testcase( pNC->ncFlags & NC_HasAgg );
100558 testcase( pNC->ncFlags & NC_HasWin );
100559 if( pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin) ){
100560 ExprSetProperty(pExpr, pNC->ncFlags & (NC_HasAgg|NC_HasWin) );
100561 savedHasAgg |= pNC->ncFlags & (NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100562 pNC->ncFlags &= ~(NC_HasAgg|NC_MinMaxAgg|NC_HasWin);
100564 if( pNC->nErr>0 || w.pParse->nErr>0 ) return WRC_Abort;
100566 pNC->ncFlags |= savedHasAgg;
100572 ** decendents of the SELECT, including compounds off of p->pPrior,
100603 ** ------------
100611 ** nodes of the expression is set to -1 and the Expr.iColumn value is
100614 ** Any errors cause an error message to be set in pParse.
100623 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
100624 NameContext sNC; /* Name context for pParse->pNewTable */
100634 sSrc.a[0].zName = pTab->zName;
100636 sSrc.a[0].iCursor = -1;
100637 if( pTab->pSchema!=pParse->db->aDb[1].pSchema ){
100638 /* Cause EP_FromDDL to be set on TK_FUNCTION nodes of non-TEMP
100677 assert( iCol<pTab->nCol );
100678 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
100685 ** or a sub-select with a column as the return value, then the
100700 assert( pExpr->op==TK_COLLATE
100701 || pExpr->op==TK_IF_NULL_ROW
100702 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_IF_NULL_ROW) );
100703 pExpr = pExpr->pLeft;
100706 op = pExpr->op;
100708 assert( pExpr->flags&EP_xIsSelect );
100709 assert( pExpr->x.pSelect!=0 );
100710 assert( pExpr->x.pSelect->pEList!=0 );
100711 assert( pExpr->x.pSelect->pEList->a[0].pExpr!=0 );
100712 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
100714 if( op==TK_REGISTER ) op = pExpr->op2;
100718 return sqlite3AffinityType(pExpr->u.zToken, 0);
100721 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
100722 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
100725 assert( pExpr->pLeft->flags&EP_xIsSelect );
100727 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
100731 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
100733 return pExpr->affExpr;
100741 ** If a memory allocation error occurs, that fact is recorded in pParse->db
100750 assert( pExpr!=0 || pParse->db->mallocFailed );
100752 if( pExpr->op==TK_VECTOR ){
100753 ExprList *pList = pExpr->x.pList;
100756 for(i=0; i<pList->nExpr; i++){
100757 pList->a[i].pExpr = sqlite3ExprAddCollateToken(pParse,pList->a[i].pExpr,
100761 }else if( pCollName->n>0 ){
100762 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
100764 pNew->pLeft = pExpr;
100765 pNew->flags |= EP_Collate|EP_Skip;
100783 assert( pExpr->op==TK_COLLATE );
100784 pExpr = pExpr->pLeft;
100798 assert( pExpr->x.pList->nExpr>0 );
100799 assert( pExpr->op==TK_FUNCTION );
100800 pExpr = pExpr->x.pList->a[0].pExpr;
100802 assert( pExpr->op==TK_COLLATE );
100803 pExpr = pExpr->pLeft;
100824 sqlite3 *db = pParse->db;
100828 int op = p->op;
100829 if( op==TK_REGISTER ) op = p->op2;
100831 && p->y.pTab!=0
100833 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
100835 int j = p->iColumn;
100837 const char *zColl = p->y.pTab->aCol[j].zColl;
100843 p = p->pLeft;
100847 p = p->x.pList->a[0].pExpr;
100851 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
100854 if( p->flags & EP_Collate ){
100855 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
100856 p = p->pLeft;
100858 Expr *pNext = p->pRight;
100860 assert( p->x.pList==0 || p->pRight==0 );
100861 if( p->x.pList!=0
100862 && !db->mallocFailed
100866 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
100867 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
100868 pNext = p->x.pList->a[i].pExpr;
100897 if( p==0 ) p = pParse->db->pDfltColl;
100908 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
100940 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
100941 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
100942 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
100943 assert( pExpr->pLeft );
100944 aff = sqlite3ExprAffinity(pExpr->pLeft);
100945 if( pExpr->pRight ){
100946 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
100948 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
101005 if( pLeft->flags & EP_Collate ){
101007 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
101028 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
101030 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
101051 if( pParse->nErr ) return 0;
101058 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
101060 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
101080 ** is a sub-select, return the number of columns in the sub-select. For
101084 u8 op = pExpr->op;
101085 if( op==TK_REGISTER ) op = pExpr->op2;
101087 return pExpr->x.pList->nExpr;
101089 return pExpr->x.pSelect->pEList->nExpr;
101096 ** Return a pointer to a subexpression of pVector that is the i-th
101106 ** just the expression for the i-th term of the result set, and may
101113 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
101114 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
101115 return pVector->x.pSelect->pEList->a[i].pExpr;
101117 return pVector->x.pList->a[i].pExpr;
101126 ** the iField-th column of the vector expression pVector.
101146 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
101150 if( pVector->op==TK_SELECT ){
101151 assert( pVector->flags & EP_xIsSelect );
101158 ** pLeft->iTable: First in an array of register holding result, or 0
101170 pRet->iColumn = iField;
101171 pRet->pLeft = pVector;
101173 assert( pRet==0 || pRet->iTable==0 );
101175 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
101176 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
101185 ** sub-select returns more than one column, the first in an array
101193 if( pExpr->op==TK_SELECT ){
101201 ** Argument pVector points to a vector expression - either a TK_VECTOR
101209 ** containing the results of the sub-select.
101226 u8 op = pVector->op;
101230 return pVector->iTable+iField;
101233 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
101236 *ppExpr = pVector->x.pList->a[iField].pExpr;
101247 ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
101248 ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
101249 ** otherwise: op==pExpr->op and p5==0
101258 Vdbe *v = pParse->pVdbe;
101259 Expr *pLeft = pExpr->pLeft;
101260 Expr *pRight = pExpr->pRight;
101270 if( pParse->nErr ) return;
101275 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
101276 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
101277 || pExpr->op==TK_LT || pExpr->op==TK_GT
101278 || pExpr->op==TK_LE || pExpr->op==TK_GE
101280 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
101281 || (pExpr->op==TK_ISNOT && op==TK_NE) );
101282 assert( p5==0 || pExpr->op!=op );
101283 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
101294 Expr *pL, *pR;
101298 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
101299 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5, isCommuted);
101308 if( i==nLeft-1 ){
101324 if( i==nLeft-2 ) opx = op;
101333 ** expression depth allowed. If it is not, leave an error message in
101338 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
101359 if( p->nHeight>*pnHeight ){
101360 *pnHeight = p->nHeight;
101367 for(i=0; i<p->nExpr; i++){
101368 heightOfExpr(p->a[i].pExpr, pnHeight);
101374 for(p=pSelect; p; p=p->pPrior){
101375 heightOfExpr(p->pWhere, pnHeight);
101376 heightOfExpr(p->pHaving, pnHeight);
101377 heightOfExpr(p->pLimit, pnHeight);
101378 heightOfExprList(p->pEList, pnHeight);
101379 heightOfExprList(p->pGroupBy, pnHeight);
101380 heightOfExprList(p->pOrderBy, pnHeight);
101396 heightOfExpr(p->pLeft, &nHeight);
101397 heightOfExpr(p->pRight, &nHeight);
101399 heightOfSelect(p->x.pSelect, &nHeight);
101400 }else if( p->x.pList ){
101401 heightOfExprList(p->x.pList, &nHeight);
101402 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
101404 p->nHeight = nHeight + 1;
101416 if( pParse->nErr ) return;
101418 sqlite3ExprCheckHeight(pParse, p->nHeight);
101436 if( pParse->nErr ) return;
101437 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
101438 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
101455 ** appear to be quoted. If the quotes were of the form "..." (double-quotes)
101459 ** can be translated into a 32-bit integer, then the token is not
101476 if( op!=TK_INTEGER || pToken->z==0
101477 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
101478 nExtra = pToken->n+1;
101485 pNew->op = (u8)op;
101486 pNew->iAgg = -1;
101489 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
101490 pNew->u.iValue = iValue;
101492 pNew->u.zToken = (char*)&pNew[1];
101493 assert( pToken->z!=0 || pToken->n==0 );
101494 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
101495 pNew->u.zToken[pToken->n] = 0;
101496 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
101502 pNew->nHeight = 1;
101509 ** Allocate a new expression node from a zero-terminated token that has
101536 assert( db->mallocFailed );
101541 pRoot->pRight = pRight;
101542 pRoot->flags |= EP_Propagate & pRight->flags;
101545 pRoot->pLeft = pLeft;
101546 pRoot->flags |= EP_Propagate & pLeft->flags;
101556 ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
101566 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
101569 p->op = op & 0xff;
101570 p->iAgg = -1;
101571 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
101572 sqlite3ExprCheckHeight(pParse, p->nHeight);
101574 sqlite3ExprDelete(pParse->db, pLeft);
101575 sqlite3ExprDelete(pParse->db, pRight);
101586 pExpr->x.pSelect = pSelect;
101590 assert( pParse->db->mallocFailed );
101591 sqlite3SelectDelete(pParse->db, pSelect);
101605 sqlite3 *db = pParse->db;
101632 sqlite3 *db = pParse->db;
101639 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
101642 pNew->x.pList = pList;
101654 ** SQLITE_FUNC_DIRECT - Only usable from top-level SQL
101656 ** SQLITE_FUNC_UNSAFE - Usable if TRUSTED_SCHEMA or from
101657 ** top-level SQL
101667 assert( (pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE))!=0 );
101669 if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
101670 || (pParse->db->flags & SQLITE_TrustedSchema)==0
101679 sqlite3ErrorMsg(pParse, "unsafe use of %s()", pDef->zName);
101701 sqlite3 *db = pParse->db;
101707 z = pExpr->u.zToken;
101714 x = (ynVar)(++pParse->nVar);
101722 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
101723 i = z[1]-'0'; /* The common case of ?N for a single digit N */
101726 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
101730 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
101731 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
101732 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
101734 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
101738 if( x>pParse->nVar ){
101739 pParse->nVar = (int)x;
101741 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
101749 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
101751 x = (ynVar)(++pParse->nVar);
101756 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
101759 pExpr->iColumn = x;
101760 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
101770 /* Sanity check: Assert that the IntValue is non-negative if it exists */
101771 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
101773 assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
101774 assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
101775 || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
101778 assert( p->pLeft==0 );
101779 assert( p->pRight==0 );
101780 assert( p->x.pSelect==0 );
101785 assert( p->x.pList==0 || p->pRight==0 );
101786 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
101787 if( p->pRight ){
101789 sqlite3ExprDeleteNN(db, p->pRight);
101792 sqlite3SelectDelete(db, p->x.pSelect);
101794 sqlite3ExprListDelete(db, p->x.pList);
101797 sqlite3WindowDelete(db, p->y.pWin);
101802 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
101820 ** pExpr to the pParse->pConstExpr list with a register number of 0.
101823 pParse->pConstExpr =
101824 sqlite3ExprListAppend(pParse, pParse->pConstExpr, pExpr);
101835 sqlite3ExprDeleteNN(pParse->db, p);
101861 ** The dupedExprStructSize() function returns two values OR-ed together:
101874 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
101889 if( 0==flags || p->op==TK_SELECT_COLUMN
101900 if( p->pLeft || p->x.pList ){
101903 assert( p->pRight==0 );
101917 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
101918 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
101941 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
101950 ** to store the copy of expression p, the copies of p->u.zToken
101951 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
101979 ** by the copy of the p->u.zToken string (if any).
101984 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
101985 nToken = sqlite3Strlen30(p->u.zToken) + 1;
101996 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
102001 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
102002 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
102003 pNew->flags |= staticFlag;
102009 /* Copy the p->u.zToken string, if any. */
102011 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
102012 memcpy(zToken, p->u.zToken, nToken);
102015 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
102016 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
102018 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
102020 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
102024 /* Fill in pNew->pLeft and pNew->pRight. */
102028 pNew->pLeft = p->pLeft ?
102029 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
102030 pNew->pRight = p->pRight ?
102031 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
102035 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
102044 if( pNew->op==TK_SELECT_COLUMN ){
102045 pNew->pLeft = p->pLeft;
102046 assert( p->iColumn==0 || p->pRight==0 );
102047 assert( p->pRight==0 || p->pRight==p->pLeft );
102049 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
102051 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
102061 ** and the db->mallocFailed flag set.
102067 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
102071 pRet->nCte = p->nCte;
102072 for(i=0; i<p->nCte; i++){
102073 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
102074 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
102075 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
102090 ** objects found there, assembling them onto the linked list at Select->pWin.
102093 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
102094 Select *pSelect = pWalker->u.pSelect;
102095 Window *pWin = pExpr->y.pWin;
102098 assert( pWin->ppThis==0 );
102104 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
102133 ** part of the in-memory representation of the database schema.
102148 pNew->nExpr = p->nExpr;
102149 pItem = pNew->a;
102150 pOldItem = p->a;
102151 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
102152 Expr *pOldExpr = pOldItem->pExpr;
102154 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
102156 && pOldExpr->op==TK_SELECT_COLUMN
102157 && (pNewExpr = pItem->pExpr)!=0
102159 assert( pNewExpr->iColumn==0 || i>0 );
102160 if( pNewExpr->iColumn==0 ){
102161 assert( pOldExpr->pLeft==pOldExpr->pRight );
102162 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
102165 assert( pItem[-1].pExpr!=0 );
102166 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
102167 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
102168 pNewExpr->pLeft = pPriorSelectCol;
102171 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
102172 pItem->sortFlags = pOldItem->sortFlags;
102173 pItem->eEName = pOldItem->eEName;
102174 pItem->done = 0;
102175 pItem->bNulls = pOldItem->bNulls;
102176 pItem->bSorterRef = pOldItem->bSorterRef;
102177 pItem->u = pOldItem->u;
102196 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
102199 pNew->nSrc = pNew->nAlloc = p->nSrc;
102200 for(i=0; i<p->nSrc; i++){
102201 SrcItem *pNewItem = &pNew->a[i];
102202 SrcItem *pOldItem = &p->a[i];
102204 pNewItem->pSchema = pOldItem->pSchema;
102205 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
102206 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
102207 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
102208 pNewItem->fg = pOldItem->fg;
102209 pNewItem->iCursor = pOldItem->iCursor;
102210 pNewItem->addrFillSub = pOldItem->addrFillSub;
102211 pNewItem->regReturn = pOldItem->regReturn;
102212 if( pNewItem->fg.isIndexedBy ){
102213 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
102215 pNewItem->u2 = pOldItem->u2;
102216 if( pNewItem->fg.isCte ){
102217 pNewItem->u2.pCteUse->nUse++;
102219 if( pNewItem->fg.isTabFunc ){
102220 pNewItem->u1.pFuncArg =
102221 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
102223 pTab = pNewItem->pTab = pOldItem->pTab;
102225 pTab->nTabRef++;
102227 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
102228 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
102229 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
102230 pNewItem->colUsed = pOldItem->colUsed;
102241 pNew->nId = p->nId;
102242 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
102243 if( pNew->a==0 ){
102247 /* Note that because the size of the allocation for p->a[] is not
102250 for(i=0; i<p->nId; i++){
102251 struct IdList_item *pNewItem = &pNew->a[i];
102252 struct IdList_item *pOldItem = &p->a[i];
102253 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
102254 pNewItem->idx = pOldItem->idx;
102265 for(p=pDup; p; p=p->pPrior){
102268 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
102269 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
102270 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
102271 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
102272 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
102273 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
102274 pNew->op = p->op;
102275 pNew->pNext = pNext;
102276 pNew->pPrior = 0;
102277 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
102278 pNew->iLimit = 0;
102279 pNew->iOffset = 0;
102280 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
102281 pNew->addrOpenEphm[0] = -1;
102282 pNew->addrOpenEphm[1] = -1;
102283 pNew->nSelectRow = p->nSelectRow;
102284 pNew->pWith = withDup(db, p->pWith);
102286 pNew->pWin = 0;
102287 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
102288 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
102290 pNew->selId = p->selId;
102292 pp = &pNew->pPrior;
102313 ** Reason: This routine assumes that the number of slots in pList->a[]
102318 ** NULL is returned. If non-NULL is returned, then it is guaranteed
102327 sqlite3 *db = pParse->db;
102334 pList->nExpr = 0;
102335 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
102338 sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
102344 pItem = &pList->a[pList->nExpr++];
102345 assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
102347 memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
102348 pItem->pExpr = pExpr;
102375 sqlite3 *db = pParse->db;
102378 int iFirst = pList ? pList->nExpr : 0;
102389 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
102391 pColumns->nId, n);
102395 for(i=0; i<pColumns->nId; i++){
102397 assert( pSubExpr!=0 || db->mallocFailed );
102398 assert( pSubExpr==0 || pSubExpr->iTable==0 );
102400 pSubExpr->iTable = pColumns->nId;
102403 assert( pList->nExpr==iFirst+i+1 );
102404 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
102405 pColumns->a[i].zName = 0;
102409 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
102410 Expr *pFirst = pList->a[iFirst].pExpr;
102412 assert( pFirst->op==TK_SELECT_COLUMN );
102416 pFirst->pRight = pExpr;
102421 pFirst->iTable = pColumns->nId;
102436 assert( p->nExpr>0 );
102448 pItem = &p->a[p->nExpr-1];
102449 assert( pItem->bNulls==0 );
102453 pItem->sortFlags = (u8)iSortOrder;
102456 pItem->bNulls = 1;
102458 pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
102468 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
102477 assert( pList!=0 || pParse->db->mallocFailed!=0 );
102478 assert( pParse->eParseMode!=PARSE_MODE_UNMAP || dequote==0 );
102481 assert( pList->nExpr>0 );
102482 pItem = &pList->a[pList->nExpr-1];
102483 assert( pItem->zEName==0 );
102484 assert( pItem->eEName==ENAME_NAME );
102485 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
102487 /* If dequote==0, then pName->z does not point to part of a DDL
102489 ** to the token-map. */
102490 sqlite3Dequote(pItem->zEName);
102492 sqlite3RenameTokenMap(pParse, (void*)pItem->zEName, pName);
102503 ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
102512 sqlite3 *db = pParse->db;
102513 assert( pList!=0 || db->mallocFailed!=0 );
102515 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
102516 assert( pList->nExpr>0 );
102517 if( pItem->zEName==0 ){
102518 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
102519 pItem->eEName = ENAME_SPAN;
102526 ** leave an error message in pParse.
102533 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
102534 testcase( pEList && pEList->nExpr==mx );
102535 testcase( pEList && pEList->nExpr==mx+1 );
102536 if( pEList && pEList->nExpr>mx ){
102545 int i = pList->nExpr;
102546 struct ExprList_item *pItem = pList->a;
102547 assert( pList->nExpr>0 );
102549 sqlite3ExprDelete(db, pItem->pExpr);
102550 sqlite3DbFree(db, pItem->zEName);
102552 }while( --i>0 );
102560 ** Return the bitwise-OR of all Expr.flags fields in the given
102567 for(i=0; i<pList->nExpr; i++){
102568 Expr *pExpr = pList->a[i].pExpr;
102570 m |= pExpr->flags;
102576 ** This is a SELECT-node callback for the expression walker that
102578 ** pWalker->eCode to zero and abort.
102584 pWalker->eCode = 0;
102605 ** then convert it into an TK_TRUEFALSE term. Return non-zero if
102610 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
102612 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
102614 pExpr->op = TK_TRUEFALSE;
102627 assert( pExpr->op==TK_TRUEFALSE );
102628 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
102629 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
102630 return pExpr->u.zToken[4]==0;
102648 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
102649 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
102650 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
102652 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
102654 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
102669 ** sqlite3ExprIsConstant() pWalker->eCode==1
102670 ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
102671 ** sqlite3ExprIsTableConstant() pWalker->eCode==3
102672 ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
102689 /* If pWalker->eCode is 2 then any term of the expression that comes from
102692 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
102693 pWalker->eCode = 0;
102697 switch( pExpr->op ){
102699 ** and either pWalker->eCode==4 or 5 or the function has the
102702 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
102705 if( pWalker->eCode==5 ) ExprSetProperty(pExpr, EP_FromDDL);
102708 pWalker->eCode = 0;
102721 testcase( pExpr->op==TK_ID );
102722 testcase( pExpr->op==TK_COLUMN );
102723 testcase( pExpr->op==TK_AGG_FUNCTION );
102724 testcase( pExpr->op==TK_AGG_COLUMN );
102725 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
102728 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
102735 testcase( pExpr->op==TK_REGISTER );
102736 testcase( pExpr->op==TK_IF_NULL_ROW );
102737 testcase( pExpr->op==TK_DOT );
102738 pWalker->eCode = 0;
102741 if( pWalker->eCode==5 ){
102745 pExpr->op = TK_NULL;
102746 }else if( pWalker->eCode==4 ){
102749 pWalker->eCode = 0;
102754 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
102755 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
102773 ** Walk an expression tree. Return non-zero if the expression is constant
102776 ** For the purposes of this function, a double-quoted string (ex: "abc")
102777 ** is considered a variable but a single-quoted string (ex: 'abc') is
102785 ** Walk an expression tree. Return non-zero if
102794 ** can be added to the pParse->pConstExpr list and evaluated once when
102802 ** Walk an expression tree. Return non-zero if the expression is constant
102804 ** expression must not refer to any non-deterministic function nor any
102816 ExprList *pGroupBy = pWalker->u.pGroupBy;
102821 for(i=0; i<pGroupBy->nExpr; i++){
102822 Expr *p = pGroupBy->a[i].pExpr;
102823 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
102824 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
102831 /* Check if pExpr is a sub-select. If so, consider it variable. */
102833 pWalker->eCode = 0;
102841 ** Walk the expression tree passed as the first argument. Return non-zero
102849 ** assumes that no other collating sequence will have a finer-grained
102872 ** in a CREATE TABLE statement. Return non-zero if the expression is
102873 ** acceptable for use as a DEFAULT. That is to say, return non-zero if
102887 ** For the purposes of this function, a double-quoted string (ex: "abc")
102888 ** is considered a variable but a single-quoted string (ex: 'abc') is
102916 ** to fit in a 32-bit integer, return 1 and put the value of the integer
102918 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
102924 /* If an expression is an integer literal that fits in a signed 32-bit
102926 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
102927 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
102929 if( p->flags & EP_IntValue ){
102930 *pValue = p->u.iValue;
102933 switch( p->op ){
102935 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
102940 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
102941 assert( v!=(-2147483647-1) );
102942 *pValue = -v;
102968 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
102969 p = p->pLeft;
102971 op = p->op;
102972 if( op==TK_REGISTER ) op = p->op2;
102981 p->y.pTab==0 || /* Reference to column of index on expression */
102982 (p->iColumn>=0
102983 && ALWAYS(p->y.pTab->aCol!=0) /* Defense against OOM problems */
102984 && p->y.pTab->aCol[p->iColumn].notNull==0);
103004 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
103005 if( p->op==TK_UMINUS ) unaryMinus = 1;
103006 p = p->pLeft;
103008 op = p->op;
103009 if( op==TK_REGISTER ) op = p->op2;
103024 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
103025 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
103034 ** Return TRUE if the given string is a row-id column name.
103059 p = pX->x.pSelect;
103060 if( p->pPrior ) return 0; /* Not a compound SELECT */
103061 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
103062 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
103063 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
103066 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
103067 if( p->pLimit ) return 0; /* Has no LIMIT clause */
103068 if( p->pWhere ) return 0; /* Has no WHERE clause */
103069 pSrc = p->pSrc;
103071 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
103072 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
103073 pTab = pSrc->a[0].pTab;
103075 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
103077 pEList = p->pEList;
103080 for(i=0; i<pEList->nExpr; i++){
103081 Expr *pRes = pEList->a[i].pExpr;
103082 if( pRes->op!=TK_COLUMN ) return 0;
103083 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
103091 ** Generate code that checks the left-most column of index table iCur to see if
103093 ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
103111 ** right-hand side. Return TRUE if that list is constant.
103117 pLHS = pIn->pLeft;
103118 pIn->pLeft = 0;
103120 pIn->pLeft = pLHS;
103130 ** The job of this routine is to find or create a b-tree object that can
103134 ** A cursor is opened on the b-tree object that is the RHS of the IN operator
103135 ** and pX->iTable is set to the index of that cursor.
103137 ** The returned value of this function indicates the b-tree type, as follows:
103139 ** IN_INDEX_ROWID - The cursor was opened on a database table.
103140 ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
103141 ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
103142 ** IN_INDEX_EPH - The cursor was opened on a specially created and
103144 ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
103147 ** An existing b-tree might be used if the RHS expression pX is a simple
103154 ** pX->iTable made to point to the ephemeral table instead of an
103163 ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
103164 ** through the set members) then the b-tree must not contain duplicates.
103166 ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
103169 ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
103172 ** index can be found with the specified <columns> as its left-most.
103176 ** routine might decide that creating an ephemeral b-tree for membership
103181 ** When the b-tree is being used for membership tests, the calling function
103190 ** the value in that register will be NULL if the b-tree contains one or more
103191 ** NULL values, and it will be some non-NULL value if the b-tree contains no
103216 int iTab = pParse->nTab++; /* Cursor of the RHS table */
103220 assert( pX->op==TK_IN );
103228 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
103230 ExprList *pEList = pX->x.pSelect->pEList;
103231 for(i=0; i<pEList->nExpr; i++){
103232 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
103234 if( i==pEList->nExpr ){
103242 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
103243 sqlite3 *db = pParse->db; /* Database connection */
103246 ExprList *pEList = p->pEList;
103247 int nExpr = pEList->nExpr;
103249 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
103250 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
103251 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
103252 pTab = p->pSrc->a[0].pTab;
103255 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103258 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
103261 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
103269 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
103281 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
103282 int iCol = pEList->a[i].pExpr->iColumn;
103304 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
103307 if( pIdx->nColumn<nExpr ) continue;
103308 if( pIdx->pPartIdxWhere!=0 ) continue;
103309 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
103311 testcase( pIdx->nColumn==BMS-2 );
103312 testcase( pIdx->nColumn==BMS-1 );
103313 if( pIdx->nColumn>=BMS-1 ) continue;
103315 if( pIdx->nKeyCol>nExpr
103316 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
103324 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
103325 Expr *pRhs = pEList->a[i].pExpr;
103329 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
103331 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
103332 assert( pIdx->azColl[j] );
103333 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
103345 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
103346 if( colUsed==(MASKBIT(nExpr)-1) ){
103350 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
103351 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
103353 VdbeComment((v, "%s", pIdx->zName));
103355 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
103359 i64 mask = (1<<nExpr)-1;
103363 *prRhsHasNull = ++pParse->nMem;
103385 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
103391 /* Could not find an existing table or index to use as the RHS b-tree.
103394 u32 savedNQueryLoop = pParse->nQueryLoop;
103398 pParse->nQueryLoop = 0;
103400 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
103402 assert( pX->op==TK_IN );
103407 pParse->nQueryLoop = savedNQueryLoop;
103412 n = sqlite3ExprVectorSize(pX->pLeft);
103423 ** function allocates and returns a nul-terminated string containing
103430 Expr *pLeft = pExpr->pLeft;
103432 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
103435 assert( pExpr->op==TK_IN );
103436 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
103443 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
103457 ** message of the form:
103459 ** "sub-select returns N columns - expected M"
103462 if( pParse->nErr==0 ){
103463 const char *zFmt = "sub-select returns %d columns - expected %d";
103471 ** it is not permitted. If pExpr is a sub-select vector, this routine
103472 ** loads the Parse object with a message of the form:
103474 ** "sub-select returns N columns - expected 1"
103482 if( pExpr->flags & EP_xIsSelect ){
103483 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
103497 ** x IN (4,5,11) -- IN operator with list on right-hand side
103498 ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
103502 ** table is computed, the cursor number is also stored in pExpr->iTable,
103526 v = pParse->pVdbe;
103532 ** * The right-hand side is a correlated subquery
103533 ** * The right-hand side is an expression list containing variables
103539 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
103548 pExpr->x.pSelect->selId));
103550 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
103551 pExpr->y.sub.iAddr);
103552 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
103560 pExpr->y.sub.regReturn = ++pParse->nMem;
103561 pExpr->y.sub.iAddr =
103562 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
103569 pLeft = pExpr->pLeft;
103575 pExpr->iTable = iTab;
103576 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
103579 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
103584 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
103592 Select *pSelect = pExpr->x.pSelect;
103593 ExprList *pEList = pSelect->pEList;
103596 addrOnce?"":"CORRELATED ", pSelect->selId
103600 if( ALWAYS(pEList->nExpr==nVal) ){
103605 pSelect->iLimit = 0;
103606 testcase( pSelect->selFlags & SF_Distinct );
103609 sqlite3DbFree(pParse->db, dest.zAffSdst);
103613 sqlite3DbFree(pParse->db, dest.zAffSdst);
103616 assert( pEList->nExpr>0 );
103620 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
103621 pParse, p, pEList->a[i].pExpr
103625 }else if( ALWAYS(pExpr->x.pList!=0) ){
103635 ExprList *pList = pExpr->x.pList;
103646 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
103652 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
103653 Expr *pE2 = pItem->pExpr;
103657 ** this code only executes once. Because for a non-constant
103680 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
103681 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
103691 ** (SELECT a FROM b) -- subquery
103692 ** EXISTS (SELECT a FROM b) -- EXISTS subquery
103696 ** Return the register that holds the result. For a multi-column SELECT,
103698 ** return value is the register of the left-most result column.
103710 Vdbe *v = pParse->pVdbe;
103712 testcase( pExpr->op==TK_EXISTS );
103713 testcase( pExpr->op==TK_SELECT );
103714 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
103716 pSel = pExpr->x.pSelect;
103721 ** * The right-hand side is a correlated subquery
103722 ** * The right-hand side is an expression list containing variables
103732 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
103733 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
103734 pExpr->y.sub.iAddr);
103735 return pExpr->iTable;
103740 pExpr->y.sub.regReturn = ++pParse->nMem;
103741 pExpr->y.sub.iAddr =
103742 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
103759 addrOnce?"":"CORRELATED ", pSel->selId));
103760 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
103761 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
103762 pParse->nMem += nReg;
103763 if( pExpr->op==TK_SELECT ){
103767 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
103774 if( pSel->pLimit ){
103775 /* The subquery already has a limit. If the pre-existing limit is X
103777 sqlite3 *db = pParse->db;
103780 pLimit->affExpr = SQLITE_AFF_NUMERIC;
103782 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
103784 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
103785 pSel->pLimit->pLeft = pLimit;
103787 /* If there is no pre-existing limit add a limit of 1 */
103788 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
103789 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
103791 pSel->iLimit = 0;
103795 pExpr->iTable = rReg = dest.iSDParm;
103801 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
103802 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
103813 ** sub-select on the RHS of the IN() operator has the same number of
103815 ** a sub-query, that the LHS is a vector of size 1.
103818 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
103819 if( (pIn->flags & EP_xIsSelect) ){
103820 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
103821 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
103825 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
103839 ** The left-hand side (LHS) is a scalar or vector expression. The
103840 ** right-hand side (RHS) is an array of zero or more scalar values, or a
103855 ** See the separate in-operator.md documentation file in the canonical
103879 int addrTop; /* Top of the step-6 loop */
103881 u8 okConstFactor = pParse->okConstFactor;
103884 pLeft = pExpr->pLeft;
103887 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
103889 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
103891 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
103897 v = pParse->pVdbe;
103905 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
103910 ** nVector-1. */
103930 assert( pParse->okConstFactor==okConstFactor );
103931 pParse->okConstFactor = 0;
103933 pParse->okConstFactor = okConstFactor;
103950 ** This is step (1) in the in-operator.md optimized algorithm.
103953 ExprList *pList = pExpr->x.pList;
103954 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
103964 for(ii=0; ii<pList->nExpr; ii++){
103965 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
103966 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
103970 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
103974 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
103975 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
103976 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
103977 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
104007 if( pParse->nErr ) goto sqlite3ExprCodeIN_finished;
104009 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
104016 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
104021 /* In this case, the RHS is the ROWID of table b-tree and so we also
104022 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
104040 /* Step 4. If the RHS is known to be non-NULL and we did not find
104100 sqlite3DbFree(pParse->db, aiMap);
104101 sqlite3DbFree(pParse->db, zAff);
104108 ** value described by z[0..n-1] into register iMem.
104110 ** The z[] string will probably not be zero-terminated. But the
104119 if( negateFlag ) value = -value;
104128 ** text z[0..n-1] into register iMem.
104130 ** Expr.u.zToken is always UTF8 and zero-terminated.
104133 Vdbe *v = pParse->pVdbe;
104134 if( pExpr->flags & EP_IntValue ){
104135 int i = pExpr->u.iValue;
104137 if( negFlag ) i = -i;
104142 const char *z = pExpr->u.zToken;
104147 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
104151 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
104159 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
104167 ** appropriate for the iIdxCol-th column of index pIdx.
104176 i16 iTabCol = pIdx->aiColumn[iIdxCol];
104178 assert( pIdx->aColExpr );
104179 assert( pIdx->aColExpr->nExpr>iIdxCol );
104180 pParse->iSelfTab = iTabCur + 1;
104181 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
104182 pParse->iSelfTab = 0;
104184 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
104200 Vdbe *v = pParse->pVdbe;
104202 assert( pParse->iSelfTab!=0 );
104203 if( pParse->iSelfTab>0 ){
104204 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
104208 sqlite3ExprCodeCopy(pParse, pCol->pDflt, regOut);
104209 if( pCol->affinity>=SQLITE_AFF_TEXT ){
104210 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
104217 ** Generate code to extract the value of the iCol-th column of a table.
104232 if( iCol<0 || iCol==pTab->iPKey ){
104241 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
104243 if( pCol->colFlags & COLFLAG_BUSY ){
104244 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pCol->zName);
104246 int savedSelfTab = pParse->iSelfTab;
104247 pCol->colFlags |= COLFLAG_BUSY;
104248 pParse->iSelfTab = iTabCur+1;
104250 pParse->iSelfTab = savedSelfTab;
104251 pCol->colFlags &= ~COLFLAG_BUSY;
104270 ** Generate code that will extract the iColumn-th column from
104284 assert( pParse->pVdbe!=0 );
104285 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
104287 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
104288 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
104294 ** Generate code to move content from registers iFrom...iFrom+nReg-1
104295 ** over to iTo..iTo+nReg-1.
104298 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
104309 p->op2 = p->op;
104310 p->op = TK_REGISTER;
104311 p->iTable = iReg;
104332 if( p->op==TK_SELECT ){
104340 iResult = pParse->nMem+1;
104341 pParse->nMem += nResult;
104343 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
104351 ** If the last opcode is a OP_Copy, then set the do-not-merge flag (p5)
104355 if( sqlite3VdbeGetOp(v, -1)->opcode==OP_Copy ){
104362 ** in-line rather than by using the usual callbacks.
104371 Vdbe *v = pParse->pVdbe;
104374 nFarg = pFarg->nExpr;
104375 assert( nFarg>0 ); /* All in-line functions have at least one argument */
104378 /* Attempt a direct implementation of the built-in COALESCE() and
104380 ** arguments past the first non-NULL argument.
104385 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
104389 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
104404 /* The UNLIKELY() function is a no-op. The result is the value
104408 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
104413 ** Test-only SQL functions that are only usable if enabled
104420 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
104429 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
104438 pA1 = pFarg->a[1].pExpr;
104439 if( pA1->op==TK_COLUMN ){
104441 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
104458 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
104460 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
104481 Vdbe *v = pParse->pVdbe; /* The VM under construction */
104484 int regFree1 = 0; /* If non-zero free this temporary register */
104485 int regFree2 = 0; /* If non-zero free this temporary register */
104490 assert( target>0 && target<=pParse->nMem );
104498 op = pExpr->op;
104502 AggInfo *pAggInfo = pExpr->pAggInfo;
104505 assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn );
104506 pCol = &pAggInfo->aCol[pExpr->iAgg];
104507 if( !pAggInfo->directMode ){
104508 assert( pCol->iMem>0 );
104509 return pCol->iMem;
104510 }else if( pAggInfo->useSortingIdx ){
104511 Table *pTab = pCol->pTab;
104512 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
104513 pCol->iSorterColumn, target);
104514 if( pCol->iColumn<0 ){
104515 VdbeComment((v,"%s.rowid",pTab->zName));
104517 VdbeComment((v,"%s.%s",pTab->zName,pTab->aCol[pCol->iColumn].zName));
104518 if( pTab->aCol[pCol->iColumn].affinity==SQLITE_AFF_REAL ){
104528 int iTab = pExpr->iTable;
104532 ** constraints, and that constant is coded by the pExpr->pLeft
104538 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
104539 if( pExpr->y.pTab ){
104540 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
104542 aff = pExpr->affExpr;
104549 &zAff[(aff-'B')*2], P4_STATIC);
104554 if( pParse->iSelfTab<0 ){
104558 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
104562 Table *pTab = pExpr->y.pTab;
104564 int iCol = pExpr->iColumn;
104567 assert( iCol<pTab->nCol );
104569 return -1-pParse->iSelfTab;
104571 pCol = pTab->aCol + iCol;
104573 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
104575 if( pCol->colFlags & COLFLAG_GENERATED ){
104576 if( pCol->colFlags & COLFLAG_BUSY ){
104578 pCol->zName);
104581 pCol->colFlags |= COLFLAG_BUSY;
104582 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
104585 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
104589 if( pCol->affinity==SQLITE_AFF_REAL ){
104599 iTab = pParse->iSelfTab - 1;
104602 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
104603 pExpr->iColumn, iTab, target,
104604 pExpr->op2);
104605 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
104621 codeReal(v, pExpr->u.zToken, 0, target);
104627 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
104645 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
104646 assert( pExpr->u.zToken[1]=='\'' );
104647 z = &pExpr->u.zToken[2];
104648 n = sqlite3Strlen30(z) - 1;
104657 assert( pExpr->u.zToken!=0 );
104658 assert( pExpr->u.zToken[0]!=0 );
104659 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
104660 if( pExpr->u.zToken[1]!=0 ){
104661 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
104662 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
104663 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
104669 return pExpr->iTable;
104674 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
104680 sqlite3AffinityType(pExpr->u.zToken, 0));
104688 /* fall-through */
104695 Expr *pLeft = pExpr->pLeft;
104700 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104701 codeCompare(pParse, pLeft, pExpr->pRight, op,
104738 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104739 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
104746 Expr *pLeft = pExpr->pLeft;
104748 if( pLeft->op==TK_INTEGER ){
104752 }else if( pLeft->op==TK_FLOAT ){
104754 codeReal(v, pLeft->u.zToken, 1, target);
104763 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
104773 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104781 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104783 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
104784 bNormal = pExpr->op2==TK_IS;
104796 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
104806 AggInfo *pInfo = pExpr->pAggInfo;
104808 || NEVER(pExpr->iAgg<0)
104809 || NEVER(pExpr->iAgg>=pInfo->nFunc)
104812 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
104814 return pInfo->aFunc[pExpr->iAgg].iMem;
104825 sqlite3 *db = pParse->db; /* The database connection */
104831 return pExpr->y.pWin->regResult;
104838 return sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
104842 pFarg = pExpr->x.pList;
104843 nFarg = pFarg ? pFarg->nExpr : 0;
104845 zId = pExpr->u.zToken;
104848 if( pDef==0 && pParse->explain ){
104852 if( pDef==0 || pDef->xFinalize!=0 ){
104856 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
104857 assert( (pDef->funcFlags & SQLITE_FUNC_UNSAFE)==0 );
104858 assert( (pDef->funcFlags & SQLITE_FUNC_DIRECT)==0 );
104860 SQLITE_PTR_TO_INT(pDef->pUserData), target);
104861 }else if( pDef->funcFlags & (SQLITE_FUNC_DIRECT|SQLITE_FUNC_UNSAFE) ){
104866 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
104870 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
104871 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
104876 r1 = pParse->nMem+1;
104877 pParse->nMem += nFarg;
104887 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
104890 assert( pFarg->a[0].pExpr!=0 );
104891 exprOp = pFarg->a[0].pExpr->op;
104895 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
104896 pFarg->a[0].pExpr->op2 =
104897 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
104920 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
104922 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
104925 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
104926 if( !pColl ) pColl = db->pDfltColl;
104930 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
104931 Expr *pArg = pFarg->a[0].pExpr;
104932 if( pArg->op==TK_COLUMN ){
104933 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
104941 pDef, pExpr->op2);
104958 if( pParse->db->mallocFailed ){
104960 }else if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
104969 if( pExpr->pLeft->iTable==0 ){
104970 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
104972 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
104973 if( pExpr->iTable!=0
104974 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
104977 pExpr->iTable, n);
104979 return pExpr->pLeft->iTable + pExpr->iColumn;
105002 ** X is stored in pExpr->pLeft.
105003 ** Y is stored in pExpr->pList->a[0].pExpr.
105004 ** Z is stored in pExpr->pList->a[1].pExpr.
105013 pExpr = pExpr->pLeft;
105014 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
105019 ** to a column in the new.* or old.* pseudo-tables available to
105021 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
105022 ** is set to the column of the pseudo-table to read, or to -1 to
105027 ** to reference another column of the old.* pseudo-table, where
105029 ** set to (n+1), where n is the number of columns in each pseudo-table.
105030 ** For a reference to any other column in the new.* pseudo-table, p1
105039 ** p1==0 -> old.rowid p1==3 -> new.rowid
105040 ** p1==1 -> old.a p1==4 -> new.a
105041 ** p1==2 -> old.b p1==5 -> new.b
105043 Table *pTab = pExpr->y.pTab;
105044 int iCol = pExpr->iColumn;
105045 int p1 = pExpr->iTable * (pTab->nCol+1) + 1
105048 assert( pExpr->iTable==0 || pExpr->iTable==1 );
105049 assert( iCol>=-1 && iCol<pTab->nCol );
105050 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
105051 assert( p1>=0 && p1<(pTab->nCol*2+2) );
105055 (pExpr->iTable ? "new" : "old"),
105056 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zName)
105063 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
105065 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
105078 ** that derive from the right-hand table of a LEFT JOIN. The
105079 ** Expr.iTable value is the table number for the right-hand table.
105085 u8 okConstFactor = pParse->okConstFactor;
105086 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
105089 ** really constant because they originate from the right-hand side
105091 pParse->okConstFactor = 0;
105092 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
105093 pParse->okConstFactor = okConstFactor;
105110 ** X (if it exists) is in pExpr->pLeft.
105111 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
105114 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
105131 sqlite3 *db = pParse->db;
105133 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
105134 assert(pExpr->x.pList->nExpr > 0);
105135 pEList = pExpr->x.pList;
105136 aListelem = pEList->a;
105137 nExpr = pEList->nExpr;
105139 if( (pX = pExpr->pLeft)!=0 ){
105141 if( db->mallocFailed ){
105145 testcase( pX->op==TK_COLUMN );
105153 ** The value in regFree1 might get SCopy-ed into the file result.
105158 for(i=0; i<nExpr-1; i=i+2){
105166 testcase( pTest->op==TK_COLUMN );
105168 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
105174 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
105185 assert( pExpr->affExpr==OE_Rollback
105186 || pExpr->affExpr==OE_Abort
105187 || pExpr->affExpr==OE_Fail
105188 || pExpr->affExpr==OE_Ignore
105190 if( !pParse->pTriggerTab && !pParse->nested ){
105192 "RAISE() may only be used within a trigger-program");
105195 if( pExpr->affExpr==OE_Abort ){
105199 if( pExpr->affExpr==OE_Ignore ){
105201 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
105205 pParse->pTriggerTab ? SQLITE_CONSTRAINT_TRIGGER : SQLITE_ERROR,
105206 pExpr->affExpr, pExpr->u.zToken, 0, 0);
105242 p = pParse->pConstExpr;
105246 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
105247 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
105248 return pItem->u.iConstExprReg;
105252 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
105254 Vdbe *v = pParse->pVdbe;
105258 pParse->okConstFactor = 0;
105259 if( !pParse->db->mallocFailed ){
105260 if( regDest<0 ) regDest = ++pParse->nMem;
105263 pParse->okConstFactor = 1;
105264 sqlite3ExprDelete(pParse->db, pExpr);
105269 struct ExprList_item *pItem = &p->a[p->nExpr-1];
105270 pItem->reusable = regDest<0;
105271 if( regDest<0 ) regDest = ++pParse->nMem;
105272 pItem->u.iConstExprReg = regDest;
105274 pParse->pConstExpr = p;
105297 && pExpr->op!=TK_REGISTER
105301 r2 = sqlite3ExprCodeRunJustOnce(pParse, pExpr, -1);
105324 assert( target>0 && target<=pParse->nMem );
105325 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
105326 if( pParse->pVdbe==0 ) return;
105335 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
105345 sqlite3 *db = pParse->db;
105347 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
105358 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
105370 ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
105395 Vdbe *v = pParse->pVdbe;
105398 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
105399 n = pList->nExpr;
105401 for(pItem=pList->a, i=0; i<n; i++, pItem++){
105402 Expr *pExpr = pItem->pExpr;
105404 if( pItem->bSorterRef ){
105405 i--;
105406 n--;
105409 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
105411 i--;
105412 n--;
105414 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
105425 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
105426 && pOp->p1+pOp->p3+1==inReg
105427 && pOp->p2+pOp->p3+1==target+i
105428 && pOp->p5==0 /* The do-not-merge flag must be clear */
105430 pOp->p3++;
105472 sqlite3 *db = pParse->db;
105479 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
105480 if( db->mallocFailed==0 ){
105486 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
105489 compRight.pRight = pExpr->x.pList->a[1].pExpr;
105499 pDel->flags |= EP_FromJoin;
105533 Vdbe *v = pParse->pVdbe;
105543 op = pExpr->op;
105553 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
105555 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
105559 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
105560 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
105566 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
105573 isNot = pExpr->op2==TK_ISNOT;
105574 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
105578 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
105581 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
105599 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
105601 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105602 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
105603 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
105623 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105650 /* No-op */
105675 Vdbe *v = pParse->pVdbe;
105686 /* The value of pExpr->op and op are related as follows:
105688 ** pExpr->op op
105689 ** --------- ----------
105699 ** For other values of pExpr->op, op is undefined and unused.
105704 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
105708 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
105709 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
105710 assert( pExpr->op!=TK_NE || op==OP_Eq );
105711 assert( pExpr->op!=TK_EQ || op==OP_Ne );
105712 assert( pExpr->op!=TK_LT || op==OP_Ge );
105713 assert( pExpr->op!=TK_LE || op==OP_Gt );
105714 assert( pExpr->op!=TK_GT || op==OP_Le );
105715 assert( pExpr->op!=TK_GE || op==OP_Lt );
105717 switch( pExpr->op ){
105723 }else if( pExpr->op==TK_AND ){
105725 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
105726 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
105730 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
105732 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
105739 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
105746 isNot = pExpr->op2==TK_ISNOT;
105747 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
105752 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
105757 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
105764 testcase( pExpr->op==TK_IS );
105765 testcase( pExpr->op==TK_ISNOT );
105766 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
105775 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
105777 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105778 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
105779 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
105797 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
105826 /* no-op */
105847 sqlite3 *db = pParse->db;
105849 if( db->mallocFailed==0 ){
105859 ** If pExpr is a simple SQL value - an integer, real, string, blob
105860 ** or NULL value - then the VDBE currently being prepared is configured
105861 ** to re-prepare each time a new value is bound to variable pVar.
105864 ** same as that currently bound to variable pVar, non-zero is returned.
105871 sqlite3_value *pL, *pR = 0;
105873 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
105874 if( pR ){
105875 iVar = pVar->iColumn;
105876 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
105877 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
105880 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
105882 res = 0==sqlite3MemCompare(pL, pR, 0);
105884 sqlite3ValueFree(pR);
105895 ** other than the top-level COLLATE operator.
105897 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
105909 ** this routine is used, it does not hurt to get an extra 2 - that
105914 ** pParse->pReprepare can be matched against literals in pB. The
105915 ** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
105925 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
105928 combinedFlags = pA->flags | pB->flags;
105930 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
105935 if( pA->op!=pB->op || pA->op==TK_RAISE ){
105936 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
105939 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
105944 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
105945 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
105946 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
105948 assert( pA->op==pB->op );
105953 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
105958 }else if( pA->op==TK_NULL ){
105960 }else if( pA->op==TK_COLLATE ){
105961 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
105962 }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
105966 if( (pA->flags & (EP_Distinct|EP_Commuted))
105967 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
105971 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
105972 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
105973 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
105974 if( pA->op!=TK_STRING
105975 && pA->op!=TK_TRUEFALSE
105978 if( pA->iColumn!=pB->iColumn ) return 2;
105979 if( pA->op2!=pB->op2 && pA->op==TK_TRUTH ) return 2;
105980 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
105993 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
105996 ** This routine might return non-zero for equivalent ExprLists. The
106002 ** always differs from a non-NULL pointer.
106008 if( pA->nExpr!=pB->nExpr ) return 1;
106009 for(i=0; i<pA->nExpr; i++){
106011 Expr *pExprA = pA->a[i].pExpr;
106012 Expr *pExprB = pB->a[i].pExpr;
106013 if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
106020 ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
106031 ** Return non-zero if Expr p can only be true if pNN is not NULL.
106033 ** Or if seenNot is true, return non-zero if Expr p can only be
106034 ** non-NULL if pNN is not NULL
106041 int seenNot /* Return true only if p can be any non-NULL value */
106046 return pNN->op!=TK_NULL;
106048 switch( p->op ){
106052 || (p->x.pList!=0 && p->x.pList->nExpr>0) );
106053 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
106056 ExprList *pList = p->x.pList;
106058 assert( pList->nExpr==2 );
106060 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
106061 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
106065 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
106085 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
106092 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
106096 if( p->op2!=TK_IS ) return 0;
106097 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
106101 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
106124 ** compared against literal values in pE2 and pParse->pVdbe->expmask is
106136 if( pE2->op==TK_OR
106137 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
106138 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
106142 if( pE2->op==TK_NOTNULL
106143 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
106152 ** If the expression node requires that the table at pWalker->iCur
106153 ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
106156 ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
106157 ** (never setting pWalker->eCode) is a harmless missed optimization.
106160 testcase( pExpr->op==TK_AGG_COLUMN );
106161 testcase( pExpr->op==TK_AGG_FUNCTION );
106163 switch( pExpr->op ){
106174 testcase( pExpr->op==TK_ISNOT );
106175 testcase( pExpr->op==TK_ISNULL );
106176 testcase( pExpr->op==TK_NOTNULL );
106177 testcase( pExpr->op==TK_IS );
106178 testcase( pExpr->op==TK_OR );
106179 testcase( pExpr->op==TK_VECTOR );
106180 testcase( pExpr->op==TK_CASE );
106181 testcase( pExpr->op==TK_IN );
106182 testcase( pExpr->op==TK_FUNCTION );
106183 testcase( pExpr->op==TK_TRUTH );
106186 if( pWalker->u.iCur==pExpr->iTable ){
106187 pWalker->eCode = 1;
106193 if( pWalker->eCode==0 ){
106194 sqlite3WalkExpr(pWalker, pExpr->pLeft);
106195 if( pWalker->eCode ){
106196 pWalker->eCode = 0;
106197 sqlite3WalkExpr(pWalker, pExpr->pRight);
106203 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
106204 assert( pWalker->eCode );
106218 Expr *pLeft = pExpr->pLeft;
106219 Expr *pRight = pExpr->pRight;
106220 testcase( pExpr->op==TK_EQ );
106221 testcase( pExpr->op==TK_NE );
106222 testcase( pExpr->op==TK_LT );
106223 testcase( pExpr->op==TK_LE );
106224 testcase( pExpr->op==TK_GT );
106225 testcase( pExpr->op==TK_GE );
106228 if( (pLeft->op==TK_COLUMN && ALWAYS(pLeft->y.pTab!=0)
106229 && IsVirtual(pLeft->y.pTab))
106230 || (pRight->op==TK_COLUMN && ALWAYS(pRight->y.pTab!=0)
106231 && IsVirtual(pRight->y.pTab))
106243 ** Return true (non-zero) if expression p can only be true if at least
106244 ** one column of table iTab is non-null. In other words, return true
106261 ** be non-NULL, then the LEFT JOIN can be safely converted into an
106268 if( p->op==TK_NOTNULL ){
106269 p = p->pLeft;
106271 while( p->op==TK_AND ){
106272 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
106273 p = p->pRight;
106299 ** pWalker->u.pIdxCover->iCur can be satisfied using the index
106300 ** pWalker->u.pIdxCover->pIdx.
106303 if( pExpr->op==TK_COLUMN
106304 && pExpr->iTable==pWalker->u.pIdxCover->iCur
106305 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
106307 pWalker->eCode = 1;
106360 struct SrcCount *p = pWalker->u.pSrcCount;
106361 if( p->iSrcInner==0x7FFFFFFF && ALWAYS(pSel->pSrc) && pSel->pSrc->nSrc ){
106362 pWalker->u.pSrcCount->iSrcInner = pSel->pSrc->a[0].iCursor;
106375 ** to window functions - sqlite3WindowRewrite() may now indirectly call
106376 ** FunctionUsesThisSrc() when creating a new sub-select. */
106377 if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
106379 struct SrcCount *p = pWalker->u.pSrcCount;
106380 SrcList *pSrc = p->pSrc;
106381 int nSrc = pSrc ? pSrc->nSrc : 0;
106383 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
106386 p->nThis++;
106387 }else if( pExpr->iTable<p->iSrcInner ){
106388 /* In a well-formed parse tree (no name resolution errors),
106391 p->nOther++;
106406 assert( pExpr->op==TK_AGG_FUNCTION );
106412 cnt.iSrcInner = (pSrcList&&pSrcList->nSrc)?pSrcList->a[0].iCursor:0x7FFFFFFF;
106415 sqlite3WalkExprList(&w, pExpr->x.pList);
106418 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
106432 ** The copy is stored on pParse->pConstExpr with a register number of 0.
106439 && pExpr->pAggInfo!=0
106441 AggInfo *pAggInfo = pExpr->pAggInfo;
106442 int iAgg = pExpr->iAgg;
106443 Parse *pParse = pWalker->pParse;
106444 sqlite3 *db = pParse->db;
106445 assert( pExpr->op==TK_AGG_COLUMN || pExpr->op==TK_AGG_FUNCTION );
106446 if( pExpr->op==TK_AGG_COLUMN ){
106447 assert( iAgg>=0 && iAgg<pAggInfo->nColumn );
106448 if( pAggInfo->aCol[iAgg].pCExpr==pExpr ){
106451 pAggInfo->aCol[iAgg].pCExpr = pExpr;
106456 assert( iAgg>=0 && iAgg<pAggInfo->nFunc );
106457 if( pAggInfo->aFunc[iAgg].pFExpr==pExpr ){
106460 pAggInfo->aFunc[iAgg].pFExpr = pExpr;
106475 pWalker->pParse = pParse;
106476 pWalker->xExprCallback = agginfoPersistExprCb;
106477 pWalker->xSelectCallback = sqlite3SelectWalkNoop;
106481 ** Add a new element to the pAggInfo->aCol[] array. Return the index of
106486 pInfo->aCol = sqlite3ArrayAllocate(
106488 pInfo->aCol,
106489 sizeof(pInfo->aCol[0]),
106490 &pInfo->nColumn,
106497 ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
106502 pInfo->aFunc = sqlite3ArrayAllocate(
106504 pInfo->aFunc,
106505 sizeof(pInfo->aFunc[0]),
106506 &pInfo->nFunc,
106519 NameContext *pNC = pWalker->u.pNC;
106520 Parse *pParse = pNC->pParse;
106521 SrcList *pSrcList = pNC->pSrcList;
106522 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
106524 assert( pNC->ncFlags & NC_UAggInfo );
106525 switch( pExpr->op ){
106528 testcase( pExpr->op==TK_AGG_COLUMN );
106529 testcase( pExpr->op==TK_COLUMN );
106533 SrcItem *pItem = pSrcList->a;
106534 for(i=0; i<pSrcList->nSrc; i++, pItem++){
106537 if( pExpr->iTable==pItem->iCursor ){
106541 ** Make an entry for the column in pAggInfo->aCol[] if there
106545 pCol = pAggInfo->aCol;
106546 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
106547 if( pCol->iTable==pExpr->iTable &&
106548 pCol->iColumn==pExpr->iColumn ){
106552 if( (k>=pAggInfo->nColumn)
106553 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
106555 pCol = &pAggInfo->aCol[k];
106556 pCol->pTab = pExpr->y.pTab;
106557 pCol->iTable = pExpr->iTable;
106558 pCol->iColumn = pExpr->iColumn;
106559 pCol->iMem = ++pParse->nMem;
106560 pCol->iSorterColumn = -1;
106561 pCol->pCExpr = pExpr;
106562 if( pAggInfo->pGroupBy ){
106564 ExprList *pGB = pAggInfo->pGroupBy;
106565 struct ExprList_item *pTerm = pGB->a;
106566 n = pGB->nExpr;
106568 Expr *pE = pTerm->pExpr;
106569 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
106570 pE->iColumn==pExpr->iColumn ){
106571 pCol->iSorterColumn = j;
106576 if( pCol->iSorterColumn<0 ){
106577 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
106580 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
106583 ** pAggInfo->aCol[] entry.
106586 pExpr->pAggInfo = pAggInfo;
106587 pExpr->op = TK_AGG_COLUMN;
106588 pExpr->iAgg = (i16)k;
106590 } /* endif pExpr->iTable==pItem->iCursor */
106596 if( (pNC->ncFlags & NC_InAggFunc)==0
106597 && pWalker->walkerDepth==pExpr->op2
106602 struct AggInfo_func *pItem = pAggInfo->aFunc;
106603 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
106604 if( sqlite3ExprCompare(0, pItem->pFExpr, pExpr, -1)==0 ){
106608 if( i>=pAggInfo->nFunc ){
106609 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
106611 u8 enc = ENC(pParse->db);
106612 i = addAggInfoFunc(pParse->db, pAggInfo);
106615 pItem = &pAggInfo->aFunc[i];
106616 pItem->pFExpr = pExpr;
106617 pItem->iMem = ++pParse->nMem;
106619 pItem->pFunc = sqlite3FindFunction(pParse->db,
106620 pExpr->u.zToken,
106621 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
106622 if( pExpr->flags & EP_Distinct ){
106623 pItem->iDistinct = pParse->nTab++;
106625 pItem->iDistinct = -1;
106629 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
106633 pExpr->iAgg = (i16)i;
106634 pExpr->pAggInfo = pAggInfo;
106646 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
106661 assert( pNC->pSrcList!=0 );
106675 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
106676 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
106685 if( pParse->nTempReg==0 ){
106686 return ++pParse->nMem;
106688 return pParse->aTempReg[--pParse->nTempReg];
106698 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
106699 pParse->aTempReg[pParse->nTempReg++] = iReg;
106710 i = pParse->iRangeReg;
106711 n = pParse->nRangeReg;
106713 pParse->iRangeReg += nReg;
106714 pParse->nRangeReg -= nReg;
106716 i = pParse->nMem+1;
106717 pParse->nMem += nReg;
106727 if( nReg>pParse->nRangeReg ){
106728 pParse->nRangeReg = nReg;
106729 pParse->iRangeReg = iReg;
106736 ** Always invoke this procedure after coding a subroutine or co-routine
106738 ** the sub/co-routine does not use registers in common with the code that
106739 ** invokes the sub/co-routine.
106742 pParse->nTempReg = 0;
106743 pParse->nRangeReg = 0;
106754 if( pParse->nRangeReg>0
106755 && pParse->iRangeReg+pParse->nRangeReg > iFirst
106756 && pParse->iRangeReg <= iLast
106760 for(i=0; i<pParse->nTempReg; i++){
106761 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
106796 ** If the table is a system table, this function leaves an error message
106797 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
106802 if( 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7)
106804 || ( (pTab->tabFlags & TF_Shadow)!=0
106805 && sqlite3ReadOnlyShadowTables(pParse->db)
106809 sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
106826 const char *zWhen, /* "when" part of error message */
106829 pParse->colNamesSet = 1;
106857 Vdbe *v = pParse->pVdbe;
106860 sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, iDb, 0, p5);
106861 if( iDb!=1 ) sqlite3VdbeAddParseSchemaOp(pParse->pVdbe, 1, 0, p5);
106877 char *zName = 0; /* NULL-terminated version of pName */
106878 sqlite3 *db = pParse->db; /* Database connection */
106879 int nTabName; /* Number of UTF-8 characters in zTabName */
106882 VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */
106883 u32 savedDbFlags; /* Saved value of db->mDbFlags */
106885 savedDbFlags = db->mDbFlags;
106886 if( NEVER(db->mallocFailed) ) goto exit_rename_table;
106887 assert( pSrc->nSrc==1 );
106888 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
106890 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
106892 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
106893 zDb = db->aDb[iDb].zDbSName;
106894 db->mDbFlags |= DBFLAG_PreferBuiltin;
106923 if( pTab->pSelect ){
106924 sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
106931 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
106942 if( pVTab->pVtab->pModule->xRename==0 ){
106958 /* figure out how many UTF-8 characters are in zName */
106959 zTabName = pTab->zName;
106960 nTabName = sqlite3Utf8CharLen(zTabName, -1);
106997 zDb, zName, pTab->zName);
107018 ** of any resources used by the v-table implementation (including other
107023 int i = ++pParse->nMem;
107035 db->mDbFlags = savedDbFlags;
107046 const char *zErr /* Error message text */
107059 ** The Table structure pParse->pNewTable was extended to include
107063 Table *pNew; /* Copy of pParse->pNewTable */
107068 char *zCol; /* Null-terminated column definition */
107075 db = pParse->db;
107076 if( pParse->nErr || db->mallocFailed ) return;
107077 pNew = pParse->pNewTable;
107081 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
107082 zDb = db->aDb[iDb].zDbSName;
107083 zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */
107084 pCol = &pNew->aCol[pNew->nCol-1];
107085 pDflt = pCol->pDflt;
107091 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
107101 if( pCol->colFlags & COLFLAG_PRIMKEY ){
107105 if( pNew->pIndex ){
107110 if( (pCol->colFlags & COLFLAG_GENERATED)==0 ){
107115 assert( pDflt==0 || pDflt->op==TK_SPAN );
107116 if( pDflt && pDflt->pLeft->op==TK_NULL ){
107119 if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
107121 "Cannot add a REFERENCES column with non-NULL default value");
107123 if( pCol->notNull && !pDflt ){
107138 assert( db->mallocFailed == 1 );
107143 "Cannot add a column with non-constant default");
107147 }else if( pCol->colFlags & COLFLAG_STORED ){
107153 zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
107155 char *zEnd = &zCol[pColDef->n-1];
107156 u32 savedDbFlags = db->mDbFlags;
107158 *zEnd-- = '\0';
107160 db->mDbFlags |= DBFLAG_PreferBuiltin;
107168 zDb, pNew->addColOffset, zCol, pNew->addColOffset,
107172 db->mDbFlags = savedDbFlags;
107184 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -2);
107196 ** This function is called by the parser after the table-name in
107197 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
107198 ** pSrc is the full-name of the table being altered.
107216 sqlite3 *db = pParse->db;
107219 assert( pParse->pNewTable==0 );
107221 if( db->mallocFailed ) goto exit_begin_add_column;
107222 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
107233 if( pTab->pSelect ){
107242 assert( pTab->addColOffset>0 );
107243 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107254 pParse->pNewTable = pNew;
107255 pNew->nTabRef = 1;
107256 pNew->nCol = pTab->nCol;
107257 assert( pNew->nCol>0 );
107258 nAlloc = (((pNew->nCol-1)/8)*8)+8;
107259 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
107260 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
107261 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
107262 if( !pNew->aCol || !pNew->zName ){
107263 assert( db->mallocFailed );
107266 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
107267 for(i=0; i<pNew->nCol; i++){
107268 Column *pCol = &pNew->aCol[i];
107269 pCol->zName = sqlite3DbStrDup(db, pCol->zName);
107270 pCol->hName = sqlite3StrIHash(pCol->zName);
107271 pCol->zColl = 0;
107272 pCol->pDflt = 0;
107274 pNew->pSchema = db->aDb[iDb].pSchema;
107275 pNew->addColOffset = pTab->addColOffset;
107276 pNew->nTabRef = 1;
107287 ** it loads an error message into pParse and returns non-zero.
107295 if( pTab->pSelect ){
107307 zType, pTab->zName
107324 SrcList *pSrc, /* Table being altered. pSrc->nSrc==1 */
107328 sqlite3 *db = pParse->db; /* Database connection */
107338 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
107346 iSchema = sqlite3SchemaToIndex(db, pTab->pSchema);
107348 zDb = db->aDb[iSchema].zDbSName;
107352 if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
107361 for(iCol=0; iCol<pTab->nCol; iCol++){
107362 if( 0==sqlite3StrICmp(pTab->aCol[iCol].zName, zOld) ) break;
107364 if( iCol==pTab->nCol ){
107376 assert( pNew->n>0 );
107377 bQuote = sqlite3Isquote(pNew->z[0]);
107385 zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
107386 pTab->zName
107393 zDb, pTab->zName, iCol, zNew, bQuote
107448 ** rename-token list.
107450 ** 2. Dereferences each pointer in the rename-token list.
107453 ** address-sanitizer or similar. If any of these pointers no longer
107454 ** point to valid objects, an exception is raised by the memory-checking
107468 if( pParse->nErr==0 && pParse->db->mallocFailed==0 ){
107471 for(p=pParse->pRename; p; p=p->pNext){
107472 if( p->p ){
107473 assert( p->p!=pPtr );
107474 i += *(u8*)(p->p);
107489 ** in pParse->pRename.
107497 assert( pPtr || pParse->db->mallocFailed );
107499 if( ALWAYS(pParse->eParseMode!=PARSE_MODE_UNMAP) ){
107500 pNew = sqlite3DbMallocZero(pParse->db, sizeof(RenameToken));
107502 pNew->p = pPtr;
107503 pNew->t = *pToken;
107504 pNew->pNext = pParse->pRename;
107505 pParse->pRename = pNew;
107520 for(p=pParse->pRename; p; p=p->pNext){
107521 if( p->p==pFrom ){
107522 p->p = pTo;
107532 Parse *pParse = pWalker->pParse;
107542 With *pWith = pSelect->pWith;
107545 for(i=0; i<pWith->nCte; i++){
107546 Select *p = pWith->a[i].pSelect;
107549 sNC.pParse = pWalker->pParse;
107552 sqlite3RenameExprlistUnmap(pWalker->pParse, pWith->a[i].pCols);
107566 for(ii=0; ii<pIdList->nId; ii++){
107567 sqlite3RenameTokenRemap(pParse, 0, (void*)pIdList->a[ii].zName);
107576 Parse *pParse = pWalker->pParse;
107578 if( pParse->nErr ) return WRC_Abort;
107579 if( NEVER(p->selFlags & SF_View) ) return WRC_Prune;
107580 if( ALWAYS(p->pEList) ){
107581 ExprList *pList = p->pEList;
107582 for(i=0; i<pList->nExpr; i++){
107583 if( pList->a[i].zEName && pList->a[i].eEName==ENAME_NAME ){
107584 sqlite3RenameTokenRemap(pParse, 0, (void*)pList->a[i].zEName);
107588 if( ALWAYS(p->pSrc) ){ /* Every Select as a SrcList, even if it is empty */
107589 SrcList *pSrc = p->pSrc;
107590 for(i=0; i<pSrc->nSrc; i++){
107591 sqlite3RenameTokenRemap(pParse, 0, (void*)pSrc->a[i].zName);
107592 if( sqlite3WalkExpr(pWalker, pSrc->a[i].pOn) ) return WRC_Abort;
107593 unmapColumnIdlistNames(pParse, pSrc->a[i].pUsing);
107605 u8 eMode = pParse->eParseMode;
107611 pParse->eParseMode = PARSE_MODE_UNMAP;
107613 pParse->eParseMode = eMode;
107617 ** Remove all nodes that are part of expression-list pEList from the
107628 for(i=0; i<pEList->nExpr; i++){
107629 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME) ){
107630 sqlite3RenameTokenRemap(pParse, 0, (void*)pEList->a[i].zEName);
107643 pNext = p->pNext;
107664 for(pp=&pParse->pRename; (*pp); pp=&(*pp)->pNext){
107665 if( (*pp)->p==pPtr ){
107668 *pp = pToken->pNext;
107669 pToken->pNext = pCtx->pList;
107670 pCtx->pList = pToken;
107671 pCtx->nList++;
107682 ** descend into sub-select statements.
107685 if( p->selFlags & SF_View ) return WRC_Prune;
107697 ** constructed in RenameCtx object at pWalker->u.pRename.
107700 RenameCtx *p = pWalker->u.pRename;
107701 if( pExpr->op==TK_TRIGGER
107702 && pExpr->iColumn==p->iCol
107703 && pWalker->pParse->pTriggerTab==p->pTab
107705 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
107706 }else if( pExpr->op==TK_COLUMN
107707 && pExpr->iColumn==p->iCol
107708 && p->pTab==pExpr->y.pTab
107710 renameTokenFind(pWalker->pParse, p, (void*)pExpr);
107725 RenameToken *pBest = pCtx->pList;
107729 for(pToken=pBest->pNext; pToken; pToken=pToken->pNext){
107730 if( pToken->t.z>pBest->t.z ) pBest = pToken;
107732 for(pp=&pCtx->pList; *pp!=pBest; pp=&(*pp)->pNext);
107733 *pp = pBest->pNext;
107740 ** object (either pParse->pNewTable, pNewIndex or pNewTrigger) as part of an
107741 ** ALTER TABLE RENAME COLUMN program. The error message emitted by the
107742 ** sub-routine is currently stored in pParse->zErrMsg. This function
107743 ** adds context to the error message and then stores it in pCtx.
107758 pParse->zErrMsg
107760 sqlite3_result_error(pCtx, zErr, -1);
107765 ** For each name in the the expression-list pEList (i.e. each
107766 ** pEList->a[i].zName) that matches the string in zOld, extract the
107767 ** corresponding rename-token from Parse object pParse and add it
107778 for(i=0; i<pEList->nExpr; i++){
107779 char *zName = pEList->a[i].zEName;
107780 if( ALWAYS(pEList->a[i].eEName==ENAME_NAME)
107791 ** For each name in the the id-list pIdList (i.e. each pIdList->a[i].zName)
107792 ** that matches the string in zOld, extract the corresponding rename-token
107803 for(i=0; i<pIdList->nId; i++){
107804 char *zName = pIdList->a[i].zName;
107828 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
107830 db->init.bDropColumn = 1;
107831 db->init.azInit = (char**)&zDropColumn;
107838 p->eParseMode = PARSE_MODE_RENAME;
107839 p->db = db;
107840 p->nQueryLoop = 1;
107842 assert( p->zErrMsg==0 );
107844 p->zErrMsg = zErr;
107845 if( db->mallocFailed ) rc = SQLITE_NOMEM;
107847 && p->pNewTable==0 && p->pNewIndex==0 && p->pNewTrigger==0
107858 for(pToken=p->pRename; pToken; pToken=pToken->pNext){
107859 assert( pToken->t.z>=zSql && &pToken->t.z[pToken->t.n]<=&zSql[nSql] );
107864 db->init.iDb = 0;
107865 db->init.bDropColumn = 0;
107909 /* At this point pRename->pList contains a list of RenameToken objects
107914 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
107918 while( pRename->pList ){
107924 if( sqlite3IsIdChar(*pBest->t.z) ){
107932 iOff = pBest->t.z - zSql;
107933 if( pBest->t.n!=nReplace ){
107934 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
107935 nOut - (iOff + pBest->t.n)
107937 nOut += nReplace - pBest->t.n;
107944 sqlite3_result_text(pCtx, zOut, -1, SQLITE_TRANSIENT);
107955 ** Resolve all symbols in the trigger at pParse->pNewTrigger, assuming
107958 ** message in the Parse object.
107961 sqlite3 *db = pParse->db;
107962 Trigger *pNew = pParse->pNewTrigger;
107969 assert( pNew->pTabSchema );
107970 pParse->pTriggerTab = sqlite3FindTable(db, pNew->table,
107971 db->aDb[sqlite3SchemaToIndex(db, pNew->pTabSchema)].zDbSName
107973 pParse->eTriggerOp = pNew->op;
107976 if( ALWAYS(pParse->pTriggerTab) ){
107977 rc = sqlite3ViewGetColumnNames(pParse, pParse->pTriggerTab);
107981 if( rc==SQLITE_OK && pNew->pWhen ){
107982 rc = sqlite3ResolveExprNames(&sNC, pNew->pWhen);
107985 for(pStep=pNew->step_list; rc==SQLITE_OK && pStep; pStep=pStep->pNext){
107986 if( pStep->pSelect ){
107987 sqlite3SelectPrep(pParse, pStep->pSelect, &sNC);
107988 if( pParse->nErr ) rc = pParse->rc;
107990 if( rc==SQLITE_OK && pStep->zTarget ){
107994 for(i=0; i<pSrc->nSrc && rc==SQLITE_OK; i++){
107995 SrcItem *p = &pSrc->a[i];
107996 p->iCursor = pParse->nTab++;
107997 if( p->pSelect ){
107998 sqlite3SelectPrep(pParse, p->pSelect, 0);
108001 assert( pStep->pFrom->a[i-1].pSelect );
108002 sqlite3SelectPrep(pParse, pStep->pFrom->a[i-1].pSelect, 0);
108004 p->pTab = sqlite3LocateTableItem(pParse, 0, p);
108005 if( p->pTab==0 ){
108008 p->pTab->nTabRef++;
108009 rc = sqlite3ViewGetColumnNames(pParse, p->pTab);
108014 if( rc==SQLITE_OK && pStep->pWhere ){
108015 rc = sqlite3ResolveExprNames(&sNC, pStep->pWhere);
108018 rc = sqlite3ResolveExprListNames(&sNC, pStep->pExprList);
108020 assert( !pStep->pUpsert || (!pStep->pWhere && !pStep->pExprList) );
108021 if( pStep->pUpsert && rc==SQLITE_OK ){
108022 Upsert *pUpsert = pStep->pUpsert;
108023 pUpsert->pUpsertSrc = pSrc;
108026 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
108028 ExprList *pUpsertSet = pUpsert->pUpsertSet;
108032 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertWhere);
108035 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
108057 sqlite3WalkExpr(pWalker, pTrigger->pWhen);
108060 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
108061 sqlite3WalkSelect(pWalker, pStep->pSelect);
108062 sqlite3WalkExpr(pWalker, pStep->pWhere);
108063 sqlite3WalkExprList(pWalker, pStep->pExprList);
108064 if( pStep->pUpsert ){
108065 Upsert *pUpsert = pStep->pUpsert;
108066 sqlite3WalkExprList(pWalker, pUpsert->pUpsertTarget);
108067 sqlite3WalkExprList(pWalker, pUpsert->pUpsertSet);
108068 sqlite3WalkExpr(pWalker, pUpsert->pUpsertWhere);
108069 sqlite3WalkExpr(pWalker, pUpsert->pUpsertTargetWhere);
108071 if( pStep->pFrom ){
108073 for(i=0; i<pStep->pFrom->nSrc; i++){
108074 sqlite3WalkSelect(pWalker, pStep->pFrom->a[i].pSelect);
108085 sqlite3 *db = pParse->db;
108087 if( pParse->pVdbe ){
108088 sqlite3VdbeFinalize(pParse->pVdbe);
108090 sqlite3DeleteTable(db, pParse->pNewTable);
108091 while( (pIdx = pParse->pNewIndex)!=0 ){
108092 pParse->pNewIndex = pIdx->pNext;
108095 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
108096 sqlite3DbFree(db, pParse->zErrMsg);
108097 renameTokenFree(db, pParse->pRename);
108113 ** 7. bQuote: Non-zero if the new column name should be quoted.
108117 ** The iCol-th column (left-most is 0) of table zTable is renamed from zCol
108146 sqlite3_xauth xAuth = db->xAuth;
108156 if( pTab==0 || iCol>=pTab->nCol ){
108160 zOld = pTab->aCol[iCol].zName;
108162 sCtx.iCol = ((iCol==pTab->iPKey) ? -1 : iCol);
108165 db->xAuth = 0;
108179 Select *pSelect = sParse.pNewTable->pSelect;
108181 pSelect->selFlags &= ~SF_View;
108184 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
108191 int bFKOnly = sqlite3_stricmp(zTable, sParse.pNewTable->zName);
108193 assert( sParse.pNewTable->pSelect==0 );
108197 &sParse, &sCtx, (void*)sParse.pNewTable->aCol[iCol].zName
108200 renameTokenFind(&sParse, &sCtx, (void*)&sParse.pNewTable->iPKey);
108202 sqlite3WalkExprList(&sWalker, sParse.pNewTable->pCheck);
108203 for(pIdx=sParse.pNewTable->pIndex; pIdx; pIdx=pIdx->pNext){
108204 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
108206 for(pIdx=sParse.pNewIndex; pIdx; pIdx=pIdx->pNext){
108207 sqlite3WalkExprList(&sWalker, pIdx->aColExpr);
108210 for(i=0; i<sParse.pNewTable->nCol; i++){
108211 sqlite3WalkExpr(&sWalker, sParse.pNewTable->aCol[i].pDflt);
108216 for(pFKey=sParse.pNewTable->pFKey; pFKey; pFKey=pFKey->pNextFrom){
108217 for(i=0; i<pFKey->nCol; i++){
108218 if( bFKOnly==0 && pFKey->aCol[i].iFrom==iCol ){
108219 renameTokenFind(&sParse, &sCtx, (void*)&pFKey->aCol[i]);
108221 if( 0==sqlite3_stricmp(pFKey->zTo, zTable)
108222 && 0==sqlite3_stricmp(pFKey->aCol[i].zCol, zOld)
108224 renameTokenFind(&sParse, &sCtx, (void*)pFKey->aCol[i].zCol);
108230 sqlite3WalkExprList(&sWalker, sParse.pNewIndex->aColExpr);
108231 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
108238 for(pStep=sParse.pNewTrigger->step_list; pStep; pStep=pStep->pNext){
108239 if( pStep->zTarget ){
108240 Table *pTarget = sqlite3LocateTable(&sParse, 0, pStep->zTarget, zDb);
108242 if( pStep->pUpsert ){
108243 ExprList *pUpsertSet = pStep->pUpsert->pUpsertSet;
108246 renameColumnIdlistNames(&sParse, &sCtx, pStep->pIdList, zOld);
108247 renameColumnElistNames(&sParse, &sCtx, pStep->pExprList, zOld);
108255 renameColumnIdlistNames(&sParse, &sCtx,sParse.pNewTrigger->pColumns,zOld);
108277 db->xAuth = xAuth;
108286 RenameCtx *p = pWalker->u.pRename;
108287 if( pExpr->op==TK_COLUMN && p->pTab==pExpr->y.pTab ){
108288 renameTokenFind(pWalker->pParse, p, (void*)&pExpr->y.pTab);
108298 RenameCtx *p = pWalker->u.pRename;
108299 SrcList *pSrc = pSelect->pSrc;
108300 if( pSelect->selFlags & SF_View ) return WRC_Prune;
108302 assert( pWalker->pParse->db->mallocFailed );
108305 for(i=0; i<pSrc->nSrc; i++){
108306 SrcItem *pItem = &pSrc->a[i];
108307 if( pItem->pTab==p->pTab ){
108308 renameTokenFind(pWalker->pParse, p, pItem->zName);
108334 ** -> 'CREATE TABLE t1(a REFERENCES t3)'
108357 sqlite3_xauth xAuth = db->xAuth;
108358 db->xAuth = 0;
108374 int isLegacy = (db->flags & SQLITE_LegacyAlter);
108378 if( pTab->pSelect ){
108380 Select *pSelect = pTab->pSelect;
108385 assert( pSelect->selFlags & SF_View );
108386 pSelect->selFlags &= ~SF_View;
108387 sqlite3SelectPrep(&sParse, pTab->pSelect, &sNC);
108391 sqlite3WalkSelect(&sWalker, pTab->pSelect);
108397 if( isLegacy==0 || (db->flags & SQLITE_ForeignKeys) ){
108399 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
108400 if( sqlite3_stricmp(pFKey->zTo, zOld)==0 ){
108401 renameTokenFind(&sParse, &sCtx, (void*)pFKey->zTo);
108410 if( sqlite3_stricmp(zOld, pTab->zName)==0 ){
108413 sqlite3WalkExprList(&sWalker, pTab->pCheck);
108415 renameTokenFind(&sParse, &sCtx, pTab->zName);
108421 renameTokenFind(&sParse, &sCtx, sParse.pNewIndex->zName);
108423 sqlite3WalkExpr(&sWalker, sParse.pNewIndex->pPartIdxWhere);
108431 if( 0==sqlite3_stricmp(sParse.pNewTrigger->table, zOld)
108432 && sCtx.pTab->pSchema==pTrigger->pTabSchema
108434 renameTokenFind(&sParse, &sCtx, sParse.pNewTrigger->table);
108441 for(pStep=pTrigger->step_list; pStep; pStep=pStep->pNext){
108442 if( pStep->zTarget && 0==sqlite3_stricmp(pStep->zTarget, zOld) ){
108443 renameTokenFind(&sParse, &sCtx, pStep->zTarget);
108467 db->xAuth = xAuth;
108486 ** 5: "when" part of error message.
108504 int isLegacy = (db->flags & SQLITE_LegacyAlter);
108509 sqlite3_xauth xAuth = db->xAuth;
108510 db->xAuth = 0;
108519 if( isLegacy==0 && sParse.pNewTable && sParse.pNewTable->pSelect ){
108523 sqlite3SelectPrep(&sParse, sParse.pNewTable->pSelect, &sNC);
108532 int i1 = sqlite3SchemaToIndex(db, sParse.pNewTrigger->pTabSchema);
108546 db->xAuth = xAuth;
108555 ** argv[0]: An integer - the index of the schema containing the table
108557 ** argv[2]: An integer - the index of the column to remove.
108571 const char *zDb = db->aDb[iSchema].zDbSName;
108580 sqlite3_xauth xAuth = db->xAuth;
108581 db->xAuth = 0;
108588 if( pTab==0 || pTab->nCol==1 || iCol>=pTab->nCol ){
108594 pCol = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol].zName);
108595 if( iCol<pTab->nCol-1 ){
108597 pEnd = renameTokenFind(&sParse, 0, (void*)pTab->aCol[iCol+1].zName);
108598 zEnd = (const char*)pEnd->t.z;
108600 zEnd = (const char*)&zSql[pTab->addColOffset];
108601 while( ALWAYS(pCol->t.z[0]!=0) && pCol->t.z[0]!=',' ) pCol->t.z--;
108604 zNew = sqlite3MPrintf(db, "%.*s%s", pCol->t.z-zSql, zSql, zEnd);
108605 sqlite3_result_text(context, zNew, -1, SQLITE_TRANSIENT);
108611 db->xAuth = xAuth;
108627 sqlite3 *db = pParse->db; /* Database handle */
108632 int iCol; /* Index of column zCol in pTab->aCol[] */
108635 assert( pParse->pNewTable==0 );
108637 if( NEVER(db->mallocFailed) ) goto exit_drop_column;
108638 pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
108649 assert( db->mallocFailed );
108660 if( pTab->aCol[iCol].colFlags & (COLFLAG_PRIMKEY|COLFLAG_UNIQUE) ){
108662 (pTab->aCol[iCol].colFlags&COLFLAG_PRIMKEY) ? "PRIMARY KEY" : "UNIQUE",
108669 if( pTab->nCol<=1 ){
108675 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108677 zDb = db->aDb[iDb].zDbSName;
108683 , zDb, iDb, iCol, pTab->zName
108691 if( pParse->nErr==0 && (pTab->aCol[iCol].colFlags & COLFLAG_VIRTUAL)==0 ){
108697 int nField = 0; /* Number of non-virtual columns after drop */
108700 iCur = pParse->nTab++;
108703 reg = ++pParse->nMem;
108704 pParse->nMem += pTab->nCol;
108710 for(i=0; i<pTab->nCol; i++){
108711 if( i!=iCol && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
108716 regOut = reg+1+iPos-(iPos>iColPos);
108724 regRec = reg + pTab->nCol;
108727 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iCur, regRec, reg+1, pPk->nKeyCol);
108742 ** Register built-in functions used to help implement ALTER TABLE
108758 ** 2005-07-08
108808 ** columns. The N-th integer (for N>1) is the average number of rows in
108809 ** the index which have the same value for the first N-1 columns. For
108810 ** a K-column index, there will be K+1 integers in the stat column. If
108834 ** inclusive are samples of the left-most key value in the index taken at
108841 ** For i between 0 and S-1. Conceptually, the index space is divided into
108868 ** of entries in the index whose left-most column exactly matches
108869 ** the left-most column of the sample. The second integer in nEq
108875 ** left-most column is less than the left-most column of the sample.
108876 ** The K-th integer in the nLt entry is the number of index entries
108891 ** looks at the left-most column of the index. The sqlite_stat3.sample
108892 ** column contains the actual value of the left-most column instead
108913 ** appropriate compile-time options are provided.
108942 sqlite3 *db = pParse->db;
108956 pDb = &db->aDb[iDb];
108965 if( (pStat = sqlite3FindTable(db, zTab, pDb->zDbSName))==0 ){
108968 ** side-effect of the CREATE TABLE statement is to leave the rootpage
108969 ** of the new table in register pParse->regRoot. This is important
108972 "CREATE TABLE %Q.%s(%s)", pDb->zDbSName, zTab, aTable[i].zCols
108974 aRoot[i] = (u32)pParse->regRoot;
108981 aRoot[i] = pStat->tnum;
108986 pDb->zDbSName, zTab, zWhereType, zWhere
108989 }else if( db->xPreUpdateCallback ){
108990 sqlite3NestedParse(pParse, "DELETE FROM %Q.%s", pDb->zDbSName, zTab);
109016 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
109041 int nLimit; /* Analysis row-scan limit */
109044 u8 nSkipAhead; /* Number of times of skip-ahead */
109049 u32 iPrn; /* Pseudo-random number used for sampling */
109064 if( p->nRowid ){
109065 sqlite3DbFree(db, p->u.aRowid);
109066 p->nRowid = 0;
109076 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
109077 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
109078 if( p->u.aRowid ){
109079 p->nRowid = n;
109080 memcpy(p->u.aRowid, pData, n);
109082 p->nRowid = 0;
109092 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
109093 p->nRowid = 0;
109094 p->u.iRowid = iRowid;
109104 pTo->isPSample = pFrom->isPSample;
109105 pTo->iCol = pFrom->iCol;
109106 pTo->iHash = pFrom->iHash;
109107 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
109108 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
109109 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
109110 if( pFrom->nRowid ){
109111 sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
109113 sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
109124 if( p->mxSample ){
109126 for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
109127 for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
109128 sampleClear(p->db, &p->current);
109131 sqlite3DbFree(p->db, p);
109140 ** L: A limit on the number of rows to scan, or 0 for no-limit
109199 p->db = db;
109200 p->nEst = sqlite3_value_int64(argv[2]);
109201 p->nRow = 0;
109202 p->nLimit = sqlite3_value_int64(argv[3]);
109203 p->nCol = nCol;
109204 p->nKeyCol = nKeyCol;
109205 p->nSkipAhead = 0;
109206 p->current.anDLt = (tRowcnt*)&p[1];
109207 p->current.anEq = &p->current.anDLt[nColUp];
109210 p->mxSample = p->nLimit==0 ? mxSample : 0;
109213 int i; /* Used to iterate through p->aSample[] */
109215 p->iGet = -1;
109216 p->nPSample = (tRowcnt)(p->nEst/(mxSample/3+1) + 1);
109217 p->current.anLt = &p->current.anEq[nColUp];
109218 p->iPrn = 0x689e962d*(u32)nCol ^ 0xd0944565*(u32)sqlite3_value_int(argv[2]);
109221 p->a = (struct StatSample*)&p->current.anLt[nColUp];
109222 p->aBest = &p->a[mxSample];
109223 pSpace = (u8*)(&p->a[mxSample+nCol]);
109225 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
109226 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
109227 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
109229 assert( (pSpace - (u8*)p)==n );
109232 p->aBest[i].iCol = i;
109257 ** pNew and pOld are both candidate non-periodic samples selected for
109258 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
109265 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
109272 int nCol = pAccum->nCol;
109274 assert( pNew->iCol==pOld->iCol );
109275 for(i=pNew->iCol+1; i<nCol; i++){
109276 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
109277 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
109279 if( pNew->iHash>pOld->iHash ) return 1;
109289 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
109296 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
109297 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
109299 assert( pOld->isPSample==0 && pNew->isPSample==0 );
109300 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
109304 if( pNew->iCol<pOld->iCol ) return 1;
109305 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
109311 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
109312 ** remove the least desirable sample from p->a[] to make room.
109324 if( nEqZero>p->nMaxEqZero ){
109325 p->nMaxEqZero = nEqZero;
109327 if( pNew->isPSample==0 ){
109329 assert( pNew->anEq[pNew->iCol]>0 );
109336 for(i=p->nSample-1; i>=0; i--){
109337 StatSample *pOld = &p->a[i];
109338 if( pOld->anEq[pNew->iCol]==0 ){
109339 if( pOld->isPSample ) return;
109340 assert( pOld->iCol>pNew->iCol );
109348 pUpgrade->iCol = pNew->iCol;
109349 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
109355 if( p->nSample>=p->mxSample ){
109356 StatSample *pMin = &p->a[p->iMin];
109357 tRowcnt *anEq = pMin->anEq;
109358 tRowcnt *anLt = pMin->anLt;
109359 tRowcnt *anDLt = pMin->anDLt;
109360 sampleClear(p->db, pMin);
109361 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
109362 pSample = &p->a[p->nSample-1];
109363 pSample->nRowid = 0;
109364 pSample->anEq = anEq;
109365 pSample->anDLt = anDLt;
109366 pSample->anLt = anLt;
109367 p->nSample = p->mxSample-1;
109370 /* The "rows less-than" for the rowid column must be greater than that
109371 ** for the last sample in the p->a[] array. Otherwise, the samples would
109373 assert( p->nSample==0
109374 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
109377 pSample = &p->a[p->nSample];
109379 p->nSample++;
109382 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
109385 if( p->nSample>=p->mxSample ){
109386 int iMin = -1;
109387 for(i=0; i<p->mxSample; i++){
109388 if( p->a[i].isPSample ) continue;
109389 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
109394 p->iMin = iMin;
109402 ** p->current contains a sample that reflects the previous row of the
109411 for(i=(p->nCol-2); i>=iChng; i--){
109412 StatSample *pBest = &p->aBest[i];
109413 pBest->anEq[i] = p->current.anEq[i];
109414 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
109420 ** p->nMaxEqZero or greater set to zero. */
109421 for(i=p->nSample-1; i>=0; i--){
109423 for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
109427 if( iChng<p->nMaxEqZero ){
109428 for(i=p->nSample-1; i>=0; i--){
109430 for(j=iChng; j<p->nCol; j++){
109431 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
109434 p->nMaxEqZero = iChng;
109444 ** C Index of left-most column to differ from previous row
109454 ** if it wants the byte-code to do special processing.
109471 assert( p->nCol>0 );
109472 assert( iChng<p->nCol );
109474 if( p->nRow==0 ){
109476 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
109480 if( p->mxSample ) samplePushPrevious(p, iChng);
109486 p->current.anEq[i]++;
109488 for(i=iChng; i<p->nCol; i++){
109489 p->current.anDLt[i]++;
109491 if( p->mxSample ) p->current.anLt[i] += p->current.anEq[i];
109493 p->current.anEq[i] = 1;
109497 p->nRow++;
109499 if( p->mxSample ){
109502 sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
109504 sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
109507 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
109509 nLt = p->current.anLt[p->nCol-1];
109511 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
109512 p->current.isPSample = 1;
109513 p->current.iCol = 0;
109514 sampleInsert(p, &p->current, p->nCol-1);
109515 p->current.isPSample = 0;
109519 for(i=0; i<(p->nCol-1); i++){
109520 p->current.iCol = i;
109521 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
109522 sampleCopy(p, &p->aBest[i], &p->current);
109527 if( p->nLimit && p->nRow>(tRowcnt)p->nLimit*(p->nSkipAhead+1) ){
109528 p->nSkipAhead++;
109529 sqlite3_result_int(context, p->current.anDLt[0]>0);
109567 ** a one-parameter function, stat_get(P), that always returns the
109584 assert( eCall==STAT_GET_STAT1 || p->mxSample );
109609 ** I = (K+D-1)/D
109614 char *zRet = sqlite3MallocZero( (p->nKeyCol+1)*25 );
109621 p->nSkipAhead ? (u64)p->nEst : (u64)p->nRow);
109623 for(i=0; i<p->nKeyCol; i++){
109624 u64 nDistinct = p->current.anDLt[i] + 1;
109625 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
109628 assert( p->current.anEq[i] );
109632 sqlite3_result_text(context, zRet, -1, sqlite3_free);
109636 if( p->iGet<0 ){
109638 p->iGet = 0;
109640 if( p->iGet<p->nSample ){
109641 StatSample *pS = p->a + p->iGet;
109642 if( pS->nRowid==0 ){
109643 sqlite3_result_int64(context, pS->u.iRowid);
109645 sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
109652 assert( p->iGet<p->nSample );
109654 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
109655 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
109657 aCnt = p->a[p->iGet].anDLt;
109658 p->iGet++;
109664 char *zRet = sqlite3MallocZero(p->nCol * 25);
109670 for(i=0; i<p->nCol; i++){
109675 z[-1] = '\0';
109676 sqlite3_result_text(context, zRet, -1, sqlite3_free);
109699 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Integer, iParam, regStat+1);
109712 ** of the k-th column of the pIdx index.
109720 assert( k>=0 && k<pIdx->nColumn );
109721 i = pIdx->aiColumn[k];
109723 VdbeComment((v,"%s.rowid",pIdx->zName));
109725 VdbeComment((v,"%s.expr(%d)",pIdx->zName, k));
109727 VdbeComment((v,"%s.%s", pIdx->zName, pIdx->pTable->aCol[i].zName));
109746 sqlite3 *db = pParse->db; /* Database handle */
109752 int jZeroRows = -1; /* Jump from here if number of rows is zero */
109769 pParse->nMem = MAX(pParse->nMem, iMem);
109774 if( pTab->tnum==0 ){
109778 if( sqlite3_strlike("sqlite\\_%", pTab->zName, '\\')==0 ){
109783 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
109787 if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
109788 db->aDb[iDb].zDbSName ) ){
109794 if( db->xPreUpdateCallback ){
109797 pStat1->zName = (char*)&pStat1[1];
109798 memcpy(pStat1->zName, "sqlite_stat1", 13);
109799 pStat1->nCol = 3;
109800 pStat1->iPKey = -1;
109801 sqlite3VdbeAddOp4(pParse->pVdbe, OP_Noop, 0, 0, 0,(char*)pStat1,P4_DYNBLOB);
109805 /* Establish a read-lock on the table at the shared-cache level.
109806 ** Open a read-only cursor on the table. Also allocate a cursor number
109809 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
109812 pParse->nTab = MAX(pParse->nTab, iTab);
109814 sqlite3VdbeLoadString(v, regTabname, pTab->zName);
109816 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
109824 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
109826 nCol = pIdx->nKeyCol;
109827 zIdxName = pTab->zName;
109828 nColTest = nCol - 1;
109830 nCol = pIdx->nColumn;
109831 zIdxName = pIdx->zName;
109832 nColTest = pIdx->uniqNotNull ? pIdx->nKeyCol-1 : nCol-1;
109837 VdbeComment((v, "Analysis for %s.%s", pTab->zName, zIdxName));
109840 ** Pseudo-code for loop that calls stat_push():
109875 pParse->nMem = MAX(pParse->nMem, regPrev+nColTest);
109877 /* Open a read-only cursor on the index being analyzed. */
109878 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
109879 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
109881 VdbeComment((v, "%s", pIdx->zName));
109892 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regRowid);
109906 sqlite3VdbeAddOp2(v, OP_Integer, db->nAnalysisLimit, regTemp2);
109939 if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
109940 /* For a single-column UNIQUE index, once we have found a non-NULL
109947 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
109967 sqlite3VdbeJumpHere(v, addrNextRow-1);
109990 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
109992 regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
109993 for(j=0; j<pPk->nKeyCol; j++){
109994 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
109995 assert( k>=0 && k<pIdx->nColumn );
109999 sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
110000 sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
110008 if( db->nAnalysisLimit ){
110030 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
110036 if( OptimizationEnabled(db, SQLITE_Stat4) && db->nAnalysisLimit==0 ){
110047 pParse->nMem = MAX(pParse->nMem, regCol+nCol);
110065 sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
110079 VdbeComment((v, "%s", pTab->zName));
110089 sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
110111 sqlite3 *db = pParse->db;
110112 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
110119 iStatCur = pParse->nTab;
110120 pParse->nTab += 3;
110122 iMem = pParse->nMem+1;
110123 iTab = pParse->nTab;
110125 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
110142 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
110143 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
110145 iStatCur = pParse->nTab;
110146 pParse->nTab += 3;
110148 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
110150 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
110152 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
110160 ** ANALYZE -- 1
110161 ** ANALYZE <database> -- 2
110162 ** ANALYZE ?<database>.?<tablename> -- 3
110169 sqlite3 *db = pParse->db;
110178 /* Read the database schema. If an error occurs, leave an error message
110180 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
110188 for(i=0; i<db->nDb; i++){
110192 }else if( pName2->n==0 && (iDb = sqlite3FindDb(db, pName1))>=0 ){
110199 zDb = pName2->n ? db->aDb[iDb].zDbSName : 0;
110203 analyzeTable(pParse, pIdx->pTable, pIdx);
110211 if( db->nSqlExec==0 && (v = sqlite3GetVdbe(pParse))!=0 ){
110227 ** The first argument points to a nul-terminated string containing a
110251 v = v*10 + c - '0';
110270 pIndex->bUnordered = 0;
110271 pIndex->noSkipScan = 0;
110274 pIndex->bUnordered = 1;
110275 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
110278 pIndex->szIdxRow = sqlite3LogEst(sz);
110280 pIndex->noSkipScan = 1;
110283 else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
110284 pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
110299 ** argv[2] = results of analysis - on integer for each column
110316 pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
110325 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
110331 int nCol = pIndex->nKeyCol+1;
110336 if( pIndex->aiRowEst==0 ){
110337 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
110338 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
110340 aiRowEst = pIndex->aiRowEst;
110342 pIndex->bUnordered = 0;
110343 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
110344 pIndex->hasStat1 = 1;
110345 if( pIndex->pPartIdxWhere==0 ){
110346 pTable->nRowLogEst = pIndex->aiRowLogEst[0];
110347 pTable->tabFlags |= TF_HasStat1;
110351 fakeIdx.szIdxRow = pTable->szTabRow;
110355 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
110356 pTable->szTabRow = fakeIdx.szIdxRow;
110357 pTable->tabFlags |= TF_HasStat1;
110369 if( pIdx->aSample ){
110371 for(j=0; j<pIdx->nSample; j++){
110372 IndexSample *p = &pIdx->aSample[j];
110373 sqlite3DbFree(db, p->p);
110375 sqlite3DbFree(db, pIdx->aSample);
110377 if( db && db->pnBytesFreed==0 ){
110378 pIdx->nSample = 0;
110379 pIdx->aSample = 0;
110389 ** Populate the pIdx->aAvgEq[] array based on the samples currently
110390 ** stored in pIdx->aSample[].
110394 IndexSample *aSample = pIdx->aSample;
110395 IndexSample *pFinal = &aSample[pIdx->nSample-1];
110398 if( pIdx->nSampleCol>1 ){
110403 nCol = pIdx->nSampleCol-1;
110404 pIdx->aAvgEq[nCol] = 1;
110407 int nSample = pIdx->nSample;
110415 if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
110416 nRow = pFinal->anLt[iCol];
110417 nDist100 = (i64)100 * pFinal->anDLt[iCol];
110418 nSample--;
110420 nRow = pIdx->aiRowEst[0];
110421 nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
110423 pIdx->nRowEst0 = nRow;
110430 if( i==(pIdx->nSample-1)
110439 avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
110442 pIdx->aAvgEq[iCol] = avgEq;
110486 IndexSample *pSample; /* A slot in pIdx->aSample[] */
110488 assert( db->lookaside.bDisable );
110493 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
110511 assert( pIdx==0 || pIdx->nSample==0 );
110513 assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
110514 if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
110515 nIdxCol = pIdx->nKeyCol;
110517 nIdxCol = pIdx->nColumn;
110519 pIdx->nSampleCol = nIdxCol;
110524 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
110525 if( pIdx->aSample==0 ){
110529 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
110530 pIdx->aAvgEq = pSpace; pSpace += nIdxCol;
110531 pIdx->pTable->tabFlags |= TF_HasStat4;
110533 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
110534 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
110535 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
110537 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
110546 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
110561 nCol = pIdx->nSampleCol;
110566 pSample = &pIdx->aSample[pIdx->nSample];
110567 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
110568 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
110569 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
110577 pSample->n = sqlite3_column_bytes(pStmt, 4);
110578 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
110579 if( pSample->p==0 ){
110583 if( pSample->n ){
110584 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
110586 pIdx->nSample++;
110600 assert( db->lookaside.bDisable );
110628 ** If an OOM error occurs, this function always sets db->mallocFailed.
110637 Schema *pSchema = db->aDb[iDb].pSchema;
110639 assert( iDb>=0 && iDb<db->nDb );
110640 assert( db->aDb[iDb].pBt!=0 );
110644 for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
110646 pTab->tabFlags &= ~TF_HasStat1;
110648 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
110650 pIdx->hasStat1 = 0;
110653 pIdx->aSample = 0;
110659 sInfo.zDatabase = db->aDb[iDb].zDbSName;
110673 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
110675 if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
110685 for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
110687 sqlite3_free(pIdx->aiRowEst);
110688 pIdx->aiRowEst = 0;
110741 if( pExpr->op!=TK_ID ){
110744 pExpr->op = TK_STRING;
110756 sqlite3StrICmp(db->aDb[iDb].zDbSName, zName)==0
110762 ** An SQL user-function registered to do the work of an ATTACH statement. The
110772 ** If the db->init.reopenMemdb flags is set, then instead of attaching a
110773 ** new database, close the database on db->init.iDb and reopen it as an
110801 # define REOPEN_AS_MEMDB(db) (db->init.reopenMemdb)
110808 ** from sqlite3_deserialize() to close database db->init.iDb and
110812 pNew = &db->aDb[db->init.iDb];
110813 if( pNew->pBt ) sqlite3BtreeClose(pNew->pBt);
110814 pNew->pBt = 0;
110815 pNew->pSchema = 0;
110816 rc = sqlite3BtreeOpen(pVfs, "x\0", db, &pNew->pBt, 0, SQLITE_OPEN_MAIN_DB);
110826 if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
110827 zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
110828 db->aLimit[SQLITE_LIMIT_ATTACHED]
110832 for(i=0; i<db->nDb; i++){
110840 /* Allocate the new entry in the db->aDb[] array and initialize the schema
110843 if( db->aDb==db->aDbStatic ){
110844 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
110846 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
110848 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
110851 db->aDb = aNew;
110852 pNew = &db->aDb[db->nDb];
110859 flags = db->openFlags;
110860 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
110863 sqlite3_result_error(context, zErr, -1);
110869 rc = sqlite3BtreeOpen(pVfs, zPath, db, &pNew->pBt, 0, flags);
110870 db->nDb++;
110871 pNew->zDbSName = sqlite3DbStrDup(db, zName);
110873 db->noSharedCache = 0;
110879 pNew->pSchema = sqlite3SchemaGet(db, pNew->pBt);
110880 if( !pNew->pSchema ){
110882 }else if( pNew->pSchema->file_format && pNew->pSchema->enc!=ENC(db) ){
110887 sqlite3BtreeEnter(pNew->pBt);
110888 pPager = sqlite3BtreePager(pNew->pBt);
110889 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
110890 sqlite3BtreeSecureDelete(pNew->pBt,
110891 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
110893 sqlite3BtreeSetPagerFlags(pNew->pBt,
110894 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
110896 sqlite3BtreeLeave(pNew->pBt);
110898 pNew->safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
110899 if( rc==SQLITE_OK && pNew->zDbSName==0 ){
110906 ** remove the entry from the db->aDb[] array. i.e. put everything back the
110911 db->init.iDb = 0;
110912 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
110923 if( newAuth<db->auth.authLevel ){
110930 int iDb = db->nDb - 1;
110932 if( db->aDb[iDb].pBt ){
110933 sqlite3BtreeClose(db->aDb[iDb].pBt);
110934 db->aDb[iDb].pBt = 0;
110935 db->aDb[iDb].pSchema = 0;
110938 db->nDb = iDb;
110955 sqlite3_result_error(context, zErrDyn, -1);
110962 ** An SQL user-function registered to do the work of an DETACH statement. The
110984 for(i=0; i<db->nDb; i++){
110985 pDb = &db->aDb[i];
110986 if( pDb->pBt==0 ) continue;
110990 if( i>=db->nDb ){
110998 if( sqlite3BtreeTxnState(pDb->pBt)!=SQLITE_TXN_NONE
110999 || sqlite3BtreeIsInBackup(pDb->pBt)
111007 assert( db->aDb[1].pSchema );
111008 pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
111011 if( pTrig->pTabSchema==pDb->pSchema ){
111012 pTrig->pTabSchema = pTrig->pSchema;
111017 sqlite3BtreeClose(pDb->pBt);
111018 pDb->pBt = 0;
111019 pDb->pSchema = 0;
111024 sqlite3_result_error(context, zErr, -1);
111043 sqlite3* db = pParse->db;
111046 if( pParse->nErr ) goto attach_end;
111061 if( pAuthArg->op==TK_STRING ){
111062 zAuthArg = pAuthArg->u.zToken;
111080 assert( v || db->mallocFailed );
111082 sqlite3VdbeAddFunctionCall(pParse, 0, regArgs+3-pFunc->nArg, regArgs+3,
111083 pFunc->nArg, pFunc, 0);
111142 DbFixer *pFix = p->u.pFix;
111143 if( !pFix->bTemp ) ExprSetProperty(pExpr, EP_FromDDL);
111144 if( pExpr->op==TK_VARIABLE ){
111145 if( pFix->pParse->db->init.busy ){
111146 pExpr->op = TK_NULL;
111148 sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
111159 DbFixer *pFix = p->u.pFix;
111162 sqlite3 *db = pFix->pParse->db;
111163 int iDb = sqlite3FindDbName(db, pFix->zDb);
111164 SrcList *pList = pSelect->pSrc;
111167 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
111168 if( pFix->bTemp==0 ){
111169 if( pItem->zDatabase && iDb!=sqlite3FindDbName(db, pItem->zDatabase) ){
111170 sqlite3ErrorMsg(pFix->pParse,
111172 pFix->zType, pFix->pName, pItem->zDatabase);
111175 sqlite3DbFree(db, pItem->zDatabase);
111176 pItem->zDatabase = 0;
111177 pItem->pSchema = pFix->pSchema;
111178 pItem->fg.fromDDL = 1;
111181 if( sqlite3WalkExpr(&pFix->w, pList->a[i].pOn) ) return WRC_Abort;
111184 if( pSelect->pWith ){
111185 for(i=0; i<pSelect->pWith->nCte; i++){
111186 if( sqlite3WalkSelect(p, pSelect->pWith->a[i].pSelect) ){
111205 sqlite3 *db = pParse->db;
111206 assert( db->nDb>iDb );
111207 pFix->pParse = pParse;
111208 pFix->zDb = db->aDb[iDb].zDbSName;
111209 pFix->pSchema = db->aDb[iDb].pSchema;
111210 pFix->zType = zType;
111211 pFix->pName = pName;
111212 pFix->bTemp = (iDb==1);
111213 pFix->w.pParse = pParse;
111214 pFix->w.xExprCallback = fixExprCb;
111215 pFix->w.xSelectCallback = fixSelectCb;
111216 pFix->w.xSelectCallback2 = 0;
111217 pFix->w.walkerDepth = 0;
111218 pFix->w.eCode = 0;
111219 pFix->w.u.pFix = pFix;
111232 ** to an object in a different database, an error message is added to
111233 ** pParse->zErrMsg and these routines return non-zero. If everything
111245 res = sqlite3WalkSelect(&pFix->w, &s);
111254 return sqlite3WalkSelect(&pFix->w, pSelect);
111260 return sqlite3WalkExpr(&pFix->w, pExpr);
111270 if( sqlite3WalkSelect(&pFix->w, pStep->pSelect)
111271 || sqlite3WalkExpr(&pFix->w, pStep->pWhere)
111272 || sqlite3WalkExprList(&pFix->w, pStep->pExprList)
111273 || sqlite3FixSrcList(pFix, pStep->pFrom)
111278 if( pStep->pUpsert ){
111279 Upsert *pUp = pStep->pUpsert;
111280 if( sqlite3WalkExprList(&pFix->w, pUp->pUpsertTarget)
111281 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertTargetWhere)
111282 || sqlite3WalkExprList(&pFix->w, pUp->pUpsertSet)
111283 || sqlite3WalkExpr(&pFix->w, pUp->pUpsertWhere)
111289 pStep = pStep->pNext;
111312 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
111359 ** means that the SQL statement will never-run - the sqlite3_exec() call
111375 sqlite3_mutex_enter(db->mutex);
111376 db->xAuth = (sqlite3_xauth)xAuth;
111377 db->pAuthArg = pArg;
111378 if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
111379 sqlite3_mutex_leave(db->mutex);
111384 ** Write an error message into pParse->zErrMsg that explains that the
111385 ** user-supplied authorization function returned an illegal value.
111389 pParse->rc = SQLITE_ERROR;
111407 sqlite3 *db = pParse->db; /* Database handle */
111408 char *zDb = db->aDb[iDb].zDbSName; /* Schema name of attached database */
111411 if( db->init.busy ) return SQLITE_OK;
111412 rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext
111414 ,db->auth.zAuthUser
111419 if( db->nDb>2 || iDb!=0 ) z = sqlite3_mprintf("%s.%z", zDb, z);
111421 pParse->rc = SQLITE_AUTH;
111445 int iSrc; /* Index in pTabList->a[] of table being read */
111449 assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
111451 assert( pParse->db->xAuth!=0 );
111452 iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
111459 if( pExpr->op==TK_TRIGGER ){
111460 pTab = pParse->pTriggerTab;
111463 for(iSrc=0; iSrc<pTabList->nSrc; iSrc++){
111464 if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
111465 pTab = pTabList->a[iSrc].pTab;
111470 iCol = pExpr->iColumn;
111474 assert( iCol<pTab->nCol );
111475 zCol = pTab->aCol[iCol].zName;
111476 }else if( pTab->iPKey>=0 ){
111477 assert( pTab->iPKey<pTab->nCol );
111478 zCol = pTab->aCol[pTab->iPKey].zName;
111482 assert( iDb>=0 && iDb<pParse->db->nDb );
111483 if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
111484 pExpr->op = TK_NULL;
111491 ** is returned, then the error count and error message in pParse are
111501 sqlite3 *db = pParse->db;
111507 assert( !IN_RENAME_OBJECT || db->xAuth==0 );
111508 if( db->xAuth==0 || db->init.busy || IN_SPECIAL_PARSE ){
111512 /* EVIDENCE-OF: R-43249-19882 The third through sixth parameters to the
111513 ** callback are either NULL pointers or zero-terminated strings that
111521 testcase( pParse->zAuthContext==0 );
111523 rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext
111525 ,db->auth.zAuthUser
111530 pParse->rc = SQLITE_AUTH;
111541 ** popped. Or if pParse==0, this routine is a no-op.
111549 pContext->pParse = pParse;
111550 pContext->zAuthContext = pParse->zAuthContext;
111551 pParse->zAuthContext = zContext;
111559 if( pContext->pParse ){
111560 pContext->pParse->zAuthContext = pContext->zAuthContext;
111561 pContext->pParse = 0;
111608 ** Record the fact that we want to lock a table at run-time.
111631 if( !sqlite3BtreeSharable(pParse->db->aDb[iDb].pBt) ) return;
111633 for(i=0; i<pToplevel->nTableLock; i++){
111634 p = &pToplevel->aTableLock[i];
111635 if( p->iDb==iDb && p->iTab==iTab ){
111636 p->isWriteLock = (p->isWriteLock || isWriteLock);
111641 nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
111642 pToplevel->aTableLock =
111643 sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
111644 if( pToplevel->aTableLock ){
111645 p = &pToplevel->aTableLock[pToplevel->nTableLock++];
111646 p->iDb = iDb;
111647 p->iTab = iTab;
111648 p->isWriteLock = isWriteLock;
111649 p->zLockName = zName;
111651 pToplevel->nTableLock = 0;
111652 sqlite3OomFault(pToplevel->db);
111662 Vdbe *pVdbe = pParse->pVdbe;
111665 for(i=0; i<pParse->nTableLock; i++){
111666 TableLock *p = &pParse->aTableLock[i];
111667 int p1 = p->iDb;
111668 sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
111669 p->zLockName, P4_STATIC);
111677 ** Return TRUE if the given yDbMask object is empty - if it contains no
111703 assert( pParse->pToplevel==0 );
111704 db = pParse->db;
111705 if( pParse->nested ) return;
111706 if( db->mallocFailed || pParse->nErr ){
111707 if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR;
111714 v = pParse->pVdbe;
111716 if( db->init.busy ){
111717 pParse->rc = SQLITE_DONE;
111721 if( v==0 ) pParse->rc = SQLITE_ERROR;
111723 assert( !pParse->isMultiWrite
111724 || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
111726 if( pParse->bReturning ){
111727 Returning *pReturning = pParse->u1.pReturning;
111733 sqlite3VdbeAddOp1(v, OP_Rewind, pReturning->iRetCur);
111735 reg = pReturning->iRetReg;
111736 for(i=0; i<pReturning->nRetCol; i++){
111737 sqlite3VdbeAddOp3(v, OP_Column, pReturning->iRetCur, i, reg+i);
111740 sqlite3VdbeAddOp2(v, OP_Next, pReturning->iRetCur, addrRewind+1);
111747 if( pParse->nTableLock>0 && db->init.busy==0 ){
111749 if( db->auth.authLevel<UAUTH_User ){
111751 pParse->rc = SQLITE_AUTH_USER;
111763 if( db->mallocFailed==0
111764 && (DbMaskNonZero(pParse->cookieMask) || pParse->pConstExpr)
111767 assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
111769 for(iDb=0; iDb<db->nDb; iDb++){
111771 if( DbMaskTest(pParse->cookieMask, iDb)==0 ) continue;
111773 pSchema = db->aDb[iDb].pSchema;
111777 DbMaskTest(pParse->writeMask,iDb), /* P2 */
111778 pSchema->schema_cookie, /* P3 */
111779 pSchema->iGeneration /* P4 */
111781 if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
111783 "usesStmtJournal=%d", pParse->mayAbort && pParse->isMultiWrite));
111786 for(i=0; i<pParse->nVtabLock; i++){
111787 char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
111790 pParse->nVtabLock = 0;
111794 ** obtain the required table-locks. This is a no-op unless the
111795 ** shared-cache feature is enabled.
111810 if( pParse->pConstExpr ){
111811 ExprList *pEL = pParse->pConstExpr;
111812 pParse->okConstFactor = 0;
111813 for(i=0; i<pEL->nExpr; i++){
111814 int iReg = pEL->a[i].u.iConstExprReg;
111816 sqlite3ExprCode(pParse, pEL->a[i].pExpr, iReg);
111821 if( pParse->bReturning ){
111822 Returning *pRet = pParse->u1.pReturning;
111823 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pRet->iRetCur, pRet->nRetCol);
111833 if( v && pParse->nErr==0 && !db->mallocFailed ){
111836 assert( pParse->pAinc==0 || pParse->nTab>0 );
111838 pParse->rc = SQLITE_DONE;
111840 pParse->rc = SQLITE_ERROR;
111860 sqlite3 *db = pParse->db;
111863 if( pParse->nErr ) return;
111864 assert( pParse->nested<10 ); /* Nesting should only be of limited depth */
111872 if( !db->mallocFailed ) pParse->rc = SQLITE_TOOBIG;
111873 pParse->nErr++;
111876 pParse->nested++;
111883 pParse->nested--;
111897 ** Locate the in-memory structure that describes a particular database
111917 if( db->auth.authLevel<UAUTH_Admin && sqlite3UserAuthTable(zName)!=0 ){
111922 for(i=0; i<db->nDb; i++){
111923 if( sqlite3StrICmp(zDatabase, db->aDb[i].zDbSName)==0 ) break;
111925 if( i>=db->nDb ){
111934 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
111941 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
111946 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash,
111953 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash, zName);
111956 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, zName);
111959 for(i=2; i<db->nDb; i++){
111961 p = sqlite3HashFind(&db->aDb[i].pSchema->tblHash, zName);
111966 p = sqlite3HashFind(&db->aDb[0].pSchema->tblHash, DFLT_SCHEMA_TABLE);
111968 p = sqlite3HashFind(&db->aDb[1].pSchema->tblHash,
111977 ** Locate the in-memory structure that describes a particular database
111980 ** error message in pParse->zErrMsg.
111983 ** routine leaves an error message in pParse->zErrMsg where
111993 sqlite3 *db = pParse->db;
111995 /* Read the database schema. If an error occurs, leave an error message
111997 if( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0
112009 if( pParse->disableVtab==0 ){
112010 Module *pMod = (Module*)sqlite3HashFind(&db->aModule, zName);
112015 return pMod->pEpoTab;
112020 pParse->checkSchema = 1;
112021 }else if( IsVirtual(p) && pParse->disableVtab ){
112042 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
112043 ** non-NULL if it is part of a view or trigger program definition. See
112052 assert( p->pSchema==0 || p->zDatabase==0 );
112053 if( p->pSchema ){
112054 int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
112055 zDb = pParse->db->aDb[iDb].zDbSName;
112057 zDb = p->zDatabase;
112059 return sqlite3LocateTable(pParse, flags, p->zName, zDb);
112063 ** Locate the in-memory structure that describes
112079 for(i=OMIT_TEMPDB; i<db->nDb; i++){
112081 Schema *pSchema = db->aDb[j].pSchema;
112085 p = sqlite3HashFind(&pSchema->idxHash, zName);
112098 sqlite3ExprDelete(db, p->pPartIdxWhere);
112099 sqlite3ExprListDelete(db, p->aColExpr);
112100 sqlite3DbFree(db, p->zColAff);
112101 if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
112103 sqlite3_free(p->aiRowEst);
112119 pHash = &db->aDb[iDb].pSchema->idxHash;
112122 if( pIndex->pTable->pIndex==pIndex ){
112123 pIndex->pTable->pIndex = pIndex->pNext;
112128 p = pIndex->pTable->pIndex;
112129 while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
112130 if( ALWAYS(p && p->pNext==pIndex) ){
112131 p->pNext = pIndex->pNext;
112136 db->mDbFlags |= DBFLAG_SchemaChange;
112140 ** Look through the list of open database files in db->aDb[] and if
112142 ** db->aDb[] structure to a smaller size, if possible.
112149 for(i=j=2; i<db->nDb; i++){
112150 struct Db *pDb = &db->aDb[i];
112151 if( pDb->pBt==0 ){
112152 sqlite3DbFree(db, pDb->zDbSName);
112153 pDb->zDbSName = 0;
112157 db->aDb[j] = db->aDb[i];
112161 db->nDb = j;
112162 if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
112163 memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
112164 sqlite3DbFree(db, db->aDb);
112165 db->aDb = db->aDbStatic;
112171 ** TEMP schema. The reset is deferred if db->nSchemaLock is not zero.
112176 assert( iDb<db->nDb );
112182 db->mDbFlags &= ~DBFLAG_SchemaKnownOk;
112185 if( db->nSchemaLock==0 ){
112186 for(i=0; i<db->nDb; i++){
112188 sqlite3SchemaClear(db->aDb[i].pSchema);
112201 for(i=0; i<db->nDb; i++){
112202 Db *pDb = &db->aDb[i];
112203 if( pDb->pSchema ){
112204 if( db->nSchemaLock==0 ){
112205 sqlite3SchemaClear(pDb->pSchema);
112211 db->mDbFlags &= ~(DBFLAG_SchemaChange|DBFLAG_SchemaKnownOk);
112214 if( db->nSchemaLock==0 ){
112223 db->mDbFlags &= ~DBFLAG_SchemaChange;
112234 if( (pCol = pTable->aCol)!=0 ){
112235 for(i=0; i<pTable->nCol; i++, pCol++){
112236 assert( pCol->zName==0 || pCol->hName==sqlite3StrIHash(pCol->zName) );
112237 sqlite3DbFree(db, pCol->zName);
112238 sqlite3ExprDelete(db, pCol->pDflt);
112239 sqlite3DbFree(db, pCol->zColl);
112241 sqlite3DbFree(db, pTable->aCol);
112257 ** db parameter can be used with db->pnBytesFreed to measure the memory
112272 if( db && !db->mallocFailed && (pTable->tabFlags & TF_Ephemeral)==0 ){
112278 for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
112279 pNext = pIndex->pNext;
112280 assert( pIndex->pSchema==pTable->pSchema
112281 || (IsVirtual(pTable) && pIndex->idxType!=SQLITE_IDXTYPE_APPDEF) );
112282 if( (db==0 || db->pnBytesFreed==0) && !IsVirtual(pTable) ){
112283 char *zName = pIndex->zName;
112285 &pIndex->pSchema->idxHash, zName, 0
112287 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
112299 sqlite3DbFree(db, pTable->zName);
112300 sqlite3DbFree(db, pTable->zColAff);
112301 sqlite3SelectDelete(db, pTable->pSelect);
112302 sqlite3ExprListDelete(db, pTable->pCheck);
112314 if( ((!db || db->pnBytesFreed==0) && (--pTable->nTabRef)>0) ) return;
112328 assert( iDb>=0 && iDb<db->nDb );
112331 testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */
112332 pDb = &db->aDb[iDb];
112333 p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, 0);
112335 db->mDbFlags |= DBFLAG_SchemaChange;
112354 zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
112370 if( p->nTab==0 ){
112371 p->nTab = 1;
112376 ** Parameter zName points to a nul-terminated buffer containing the name
112378 ** function returns the index of the named database in db->aDb[], or
112379 ** -1 if the named db cannot be found.
112382 int i = -1; /* Database number */
112385 for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
112386 if( 0==sqlite3_stricmp(pDb->zDbSName, zName) ) break;
112398 ** index of the named database in db->aDb[], or -1 if the named db
112433 sqlite3 *db = pParse->db;
112436 if( pName2->n>0 ){
112437 if( db->init.busy ) {
112439 return -1;
112445 return -1;
112448 assert( db->init.iDb==0 || db->init.busy || IN_RENAME_OBJECT
112449 || (db->mDbFlags & DBFLAG_Vacuum)!=0);
112450 iDb = db->init.iDb;
112460 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==0 );
112461 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
112463 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
112465 testcase( (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==
112467 return (db->flags&(SQLITE_WriteSchema|SQLITE_Defensive))==SQLITE_WriteSchema;
112471 ** This routine is used to check if the UTF-8 string zName is a legal
112487 sqlite3 *db = pParse->db;
112489 || db->init.imposterTable
112495 if( db->init.busy ){
112496 if( sqlite3_stricmp(zType, db->init.azInit[0])
112497 || sqlite3_stricmp(zName, db->init.azInit[1])
112498 || sqlite3_stricmp(zTblName, db->init.azInit[2])
112504 if( (pParse->nested==0 && 0==sqlite3StrNICmp(zName, "sqlite_", 7))
112521 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
112528 ** find the (first) offset of that column in index pIdx. Or return -1
112533 for(i=0; i<pIdx->nColumn; i++){
112534 if( iCol==pIdx->aiColumn[i] ) return i;
112536 return -1;
112549 ** If SQLITE_OMIT_GENERATED_COLUMNS, this routine is a no-op macro.
112552 if( pTab->tabFlags & TF_HasVirtual ){
112555 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) iCol++;
112567 ** the N-th virtual column (zero-based) then the storage number is
112568 ** the number of non-virtual columns in the table plus N.
112584 ** -- 0 1 2 3 4 5 6 7 8
112595 ** this routine is a no-op macro. If the pTab does not have any virtual
112596 ** columns, then this routine is no-op that always return iCol. If iCol
112602 assert( iCol<pTab->nCol );
112603 if( (pTab->tabFlags & TF_HasVirtual)==0 || iCol<0 ) return iCol;
112605 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) n++;
112607 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ){
112609 return pTab->nNVCol + i - n;
112627 ** The new table record is initialized and put in pParse->pNewTable.
112644 sqlite3 *db = pParse->db;
112649 if( db->init.busy && db->init.newTnum==1 ){
112651 iDb = db->init.iDb;
112658 if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
112670 pParse->sNameToken = *pName;
112675 if( db->init.iDb==1 ) isTemp = 1;
112686 char *zDb = db->aDb[iDb].zDbSName;
112698 ** index or table name in the same database. Issue an error message if
112705 char *zDb = db->aDb[iDb].zDbSName;
112714 assert( !db->init.busy || CORRUPT_DB );
112727 assert( db->mallocFailed );
112728 pParse->rc = SQLITE_NOMEM_BKPT;
112729 pParse->nErr++;
112732 pTable->zName = zName;
112733 pTable->iPKey = -1;
112734 pTable->pSchema = db->aDb[iDb].pSchema;
112735 pTable->nTabRef = 1;
112737 pTable->nRowLogEst = sqlite3LogEst(SQLITE_DEFAULT_ROWEST);
112739 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
112741 assert( pParse->pNewTable==0 );
112742 pParse->pNewTable = pTable;
112749 if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
112751 pTable->pSchema->pSeqTab = pTable;
112763 if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
112780 reg1 = pParse->regRowid = ++pParse->nMem;
112781 reg2 = pParse->regRoot = ++pParse->nMem;
112782 reg3 = ++pParse->nMem;
112786 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
112792 /* This just creates a place-holder record in the sqlite_schema table.
112796 ** The rowid for the new entry is left in register pParse->regRowid.
112797 ** The root page number of the new table is left in reg pParse->regRoot.
112807 assert( !pParse->bReturning );
112808 pParse->u1.addrCrTab =
112819 /* Normal (non-error) return. */
112833 if( sqlite3_strnicmp(pCol->zName, "__hidden__", 10)==0 ){
112834 pCol->colFlags |= COLFLAG_HIDDEN;
112835 if( pTab ) pTab->tabFlags |= TF_HasHidden;
112836 }else if( pTab && pCol!=pTab->aCol && (pCol[-1].colFlags & COLFLAG_HIDDEN) ){
112837 pTab->tabFlags |= TF_OOOHidden;
112845 ** with any application-generated triggers.
112854 pHash = &(db->aDb[1].pSchema->trigHash);
112856 sqlite3ExprListDelete(db, pRet->pReturnEL);
112879 sqlite3 *db = pParse->db;
112880 if( pParse->pNewTrigger ){
112883 assert( pParse->bReturning==0 );
112885 pParse->bReturning = 1;
112891 pParse->u1.pReturning = pRet;
112892 pRet->pParse = pParse;
112893 pRet->pReturnEL = pList;
112896 testcase( pParse->earlyCleanup );
112897 if( db->mallocFailed ) return;
112898 pRet->retTrig.zName = RETURNING_TRIGGER_NAME;
112899 pRet->retTrig.op = TK_RETURNING;
112900 pRet->retTrig.tr_tm = TRIGGER_AFTER;
112901 pRet->retTrig.bReturning = 1;
112902 pRet->retTrig.pSchema = db->aDb[1].pSchema;
112903 pRet->retTrig.step_list = &pRet->retTStep;
112904 pRet->retTStep.op = TK_RETURNING;
112905 pRet->retTStep.pTrig = &pRet->retTrig;
112906 pRet->retTStep.pExprList = pList;
112907 pHash = &(db->aDb[1].pSchema->trigHash);
112908 assert( sqlite3HashFind(pHash, RETURNING_TRIGGER_NAME)==0 || pParse->nErr );
112909 if( sqlite3HashInsert(pHash, RETURNING_TRIGGER_NAME, &pRet->retTrig)
112910 ==&pRet->retTrig ){
112929 sqlite3 *db = pParse->db;
112932 if( (p = pParse->pNewTable)==0 ) return;
112933 if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
112934 sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
112937 z = sqlite3DbMallocRaw(db, pName->n + pType->n + 2);
112940 memcpy(z, pName->z, pName->n);
112941 z[pName->n] = 0;
112944 for(i=0; i<p->nCol; i++){
112945 if( p->aCol[i].hName==hName && sqlite3StrICmp(z, p->aCol[i].zName)==0 ){
112951 if( (p->nCol & 0x7)==0 ){
112953 aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
112958 p->aCol = aNew;
112960 pCol = &p->aCol[p->nCol];
112961 memset(pCol, 0, sizeof(p->aCol[0]));
112962 pCol->zName = z;
112963 pCol->hName = hName;
112966 if( pType->n==0 ){
112969 pCol->affinity = SQLITE_AFF_BLOB;
112970 pCol->szEst = 1;
112973 pCol->colFlags |= COLFLAG_SORTERREF;
112978 memcpy(zType, pType->z, pType->n);
112979 zType[pType->n] = 0;
112981 pCol->affinity = sqlite3AffinityType(zType, pCol);
112982 pCol->colFlags |= COLFLAG_HASTYPE;
112984 p->nCol++;
112985 p->nNVCol++;
112986 pParse->constraintName.n = 0;
112998 p = pParse->pNewTable;
112999 if( p==0 || NEVER(p->nCol<1) ) return;
113000 pCol = &p->aCol[p->nCol-1];
113001 pCol->notNull = (u8)onError;
113002 p->tabFlags |= TF_HasNotNull;
113006 if( pCol->colFlags & COLFLAG_UNIQUE ){
113008 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
113009 assert( pIdx->nKeyCol==1 && pIdx->onError!=OE_None );
113010 if( pIdx->aiColumn[0]==p->nCol-1 ){
113011 pIdx->uniqNotNull = 1;
113021 ** This routine does a case-independent search of zType for the
113029 ** --------------------------------
113087 /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
113094 v = 16; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
113099 pCol->colFlags |= COLFLAG_SORTERREF;
113104 pCol->szEst = v;
113127 sqlite3 *db = pParse->db;
113128 p = pParse->pNewTable;
113130 int isInit = db->init.busy && db->init.iDb!=1;
113131 pCol = &(p->aCol[p->nCol-1]);
113134 pCol->zName);
113136 }else if( pCol->colFlags & COLFLAG_GENERATED ){
113137 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
113138 testcase( pCol->colFlags & COLFLAG_STORED );
113146 sqlite3ExprDelete(db, pCol->pDflt);
113152 pCol->pDflt = sqlite3ExprDup(db, &x, EXPRDUP_REDUCE);
113179 if( p->op==TK_STRING ){
113180 p->op = TK_ID;
113181 }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
113182 p->pLeft->op = TK_ID;
113190 pCol->colFlags |= COLFLAG_PRIMKEY;
113192 if( pCol->colFlags & COLFLAG_GENERATED ){
113193 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
113194 testcase( pCol->colFlags & COLFLAG_STORED );
113213 ** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is
113226 Table *pTab = pParse->pNewTable;
113228 int iCol = -1, i;
113231 if( pTab->tabFlags & TF_HasPrimaryKey ){
113233 "table \"%s\" has more than one primary key", pTab->zName);
113236 pTab->tabFlags |= TF_HasPrimaryKey;
113238 iCol = pTab->nCol - 1;
113239 pCol = &pTab->aCol[iCol];
113243 nTerm = pList->nExpr;
113245 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[i].pExpr);
113248 if( pCExpr->op==TK_ID ){
113249 const char *zCName = pCExpr->u.zToken;
113250 for(iCol=0; iCol<pTab->nCol; iCol++){
113251 if( sqlite3StrICmp(zCName, pTab->aCol[iCol].zName)==0 ){
113252 pCol = &pTab->aCol[iCol];
113266 Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
113267 sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
113269 pTab->iPKey = iCol;
113270 pTab->keyConf = (u8)onError;
113272 pTab->tabFlags |= autoInc*TF_Autoincrement;
113273 if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
113287 sqlite3ExprListDelete(pParse->db, pList);
113301 Table *pTab = pParse->pNewTable;
113302 sqlite3 *db = pParse->db;
113304 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
113306 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
113307 if( pParse->constraintName.n ){
113308 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
113312 while( sqlite3Isspace(zEnd[-1]) ){ zEnd--; }
113314 t.n = (int)(zEnd - t.z);
113315 sqlite3ExprListSetName(pParse, pTab->pCheck, &t, 1);
113320 sqlite3ExprDelete(pParse->db, pCheckExpr);
113334 if( (p = pParse->pNewTable)==0 || IN_RENAME_OBJECT ) return;
113335 i = p->nCol-1;
113336 db = pParse->db;
113342 sqlite3DbFree(db, p->aCol[i].zColl);
113343 p->aCol[i].zColl = zColl;
113349 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
113350 assert( pIdx->nKeyCol==1 );
113351 if( pIdx->aiColumn[0]==i ){
113352 pIdx->azColl[0] = p->aCol[i].zColl;
113366 Table *pTab = pParse->pNewTable;
113372 pCol = &(pTab->aCol[pTab->nCol-1]);
113377 if( pCol->pDflt ) goto generated_error;
113379 if( pType->n==7 && sqlite3StrNICmp("virtual",pType->z,7)==0 ){
113380 /* no-op */
113381 }else if( pType->n==6 && sqlite3StrNICmp("stored",pType->z,6)==0 ){
113387 if( eType==COLFLAG_VIRTUAL ) pTab->nNVCol--;
113388 pCol->colFlags |= eType;
113391 pTab->tabFlags |= eType;
113392 if( pCol->colFlags & COLFLAG_PRIMKEY ){
113393 makeColumnPartOfPrimaryKey(pParse, pCol); /* For the error message */
113395 pCol->pDflt = pExpr;
113401 pCol->zName);
113403 sqlite3ExprDelete(pParse->db, pExpr);
113406 ** SQLITE_OMIT_GENERATED_COLUMNS compile-time option is used. */
113408 sqlite3ExprDelete(pParse->db, pExpr);
113422 ** This plan is not completely bullet-proof. It is possible for
113428 ** IMPLEMENTATION-OF: R-34230-56049 SQLite automatically increments
113429 ** the schema-version whenever the schema changes.
113432 sqlite3 *db = pParse->db;
113433 Vdbe *v = pParse->pVdbe;
113436 (int)(1+(unsigned)db->aDb[iDb].pSchema->schema_cookie));
113459 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
113463 ** If the string zSignedIdent consists entirely of alpha-numeric
113466 ** it is quoted using double-quotes.
113502 for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
113503 n += identLength(pCol->zName) + 5;
113505 n += identLength(p->zName);
113515 n += 35 + 6*p->nCol;
113523 identPut(zStmt, &k, p->zName);
113525 for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
113536 sqlite3_snprintf(n-k, &zStmt[k], zSep);
113539 identPut(zStmt, &k, pCol->zName);
113540 assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
113541 assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
113542 testcase( pCol->affinity==SQLITE_AFF_BLOB );
113543 testcase( pCol->affinity==SQLITE_AFF_TEXT );
113544 testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
113545 testcase( pCol->affinity==SQLITE_AFF_INTEGER );
113546 testcase( pCol->affinity==SQLITE_AFF_REAL );
113548 zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
113550 assert( pCol->affinity==SQLITE_AFF_BLOB
113551 || pCol->affinity==sqlite3AffinityType(zType, 0) );
113556 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
113567 if( pIdx->nColumn>=N ) return SQLITE_OK;
113568 assert( pIdx->isResized==0 );
113572 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
113573 pIdx->azColl = (const char**)zExtra;
113575 memcpy(zExtra, pIdx->aiRowLogEst, sizeof(LogEst)*(pIdx->nKeyCol+1));
113576 pIdx->aiRowLogEst = (LogEst*)zExtra;
113578 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
113579 pIdx->aiColumn = (i16*)zExtra;
113581 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
113582 pIdx->aSortOrder = (u8*)zExtra;
113583 pIdx->nColumn = N;
113584 pIdx->isResized = 1;
113595 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
113596 wTable += pTabCol->szEst;
113598 if( pTab->iPKey<0 ) wTable++;
113599 pTab->szTabRow = sqlite3LogEst(wTable*4);
113608 const Column *aCol = pIdx->pTable->aCol;
113609 for(i=0; i<pIdx->nColumn; i++){
113610 i16 x = pIdx->aiColumn[i];
113611 assert( x<pIdx->pTable->nCol );
113612 wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
113614 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
113622 while( nCol-- > 0 ){
113633 ** match the iCol-th entry of pPk. pPk is always a WITHOUT ROWID
113646 assert( nKey<=pIdx->nColumn );
113647 assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
113648 assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
113649 assert( pPk->pTable->tabFlags & TF_WithoutRowid );
113650 assert( pPk->pTable==pIdx->pTable );
113652 j = pPk->aiColumn[iCol];
113655 assert( pIdx->aiColumn[i]>=0 || j>=0 );
113656 if( pIdx->aiColumn[i]==j
113657 && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
113669 ** high-order bit of colNotIdxed is always 1. All unindexed columns
113672 ** 2019-10-24: For the purpose of this computation, virtual columns are
113680 ** The colNotIdxed mask is AND-ed with the SrcList.a[].colUsed mask
113686 Table *pTab = pIdx->pTable;
113687 for(j=pIdx->nColumn-1; j>=0; j--){
113688 int x = pIdx->aiColumn[j];
113689 if( x>=0 && (pTab->aCol[x].colFlags & COLFLAG_VIRTUAL)==0 ){
113690 testcase( x==BMS-1 );
113691 testcase( x==BMS-2 );
113692 if( x<BMS-1 ) m |= MASKBIT(x);
113695 pIdx->colNotIdxed = ~m;
113696 assert( (pIdx->colNotIdxed>>63)==1 );
113729 sqlite3 *db = pParse->db;
113730 Vdbe *v = pParse->pVdbe;
113734 if( !db->init.imposterTable ){
113735 for(i=0; i<pTab->nCol; i++){
113736 if( (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){
113737 pTab->aCol[i].notNull = OE_Abort;
113740 pTab->tabFlags |= TF_HasNotNull;
113746 assert( !pParse->bReturning );
113747 if( pParse->u1.addrCrTab ){
113749 sqlite3VdbeChangeP3(v, pParse->u1.addrCrTab, BTREE_BLOBKEY);
113755 if( pTab->iPKey>=0 ){
113758 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
113763 sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
113765 pList->a[0].sortFlags = pParse->iPkSortOrder;
113766 assert( pParse->pNewTable==pTab );
113767 pTab->iPKey = -1;
113768 sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
113770 if( db->mallocFailed || pParse->nErr ) return;
113772 assert( pPk->nKeyCol==1 );
113782 for(i=j=1; i<pPk->nKeyCol; i++){
113784 pPk->nColumn--;
113786 testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
113787 pPk->azColl[j] = pPk->azColl[i];
113788 pPk->aSortOrder[j] = pPk->aSortOrder[i];
113789 pPk->aiColumn[j++] = pPk->aiColumn[i];
113792 pPk->nKeyCol = j;
113795 pPk->isCovering = 1;
113796 if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
113797 nPk = pPk->nColumn = pPk->nKeyCol;
113803 if( v && pPk->tnum>0 ){
113804 assert( db->init.busy==0 );
113805 sqlite3VdbeChangeOpcode(v, (int)pPk->tnum, OP_Goto);
113809 pPk->tnum = pTab->tnum;
113811 /* Update the in-memory representation of all UNIQUE indices by converting
113814 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
113818 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
113819 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
113825 pIdx->nColumn = pIdx->nKeyCol;
113828 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
113829 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
113830 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
113831 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
113832 pIdx->aiColumn[j] = pPk->aiColumn[i];
113833 pIdx->azColl[j] = pPk->azColl[i];
113834 if( pPk->aSortOrder[i] ){
113836 pIdx->bAscKeyBug = 1;
113841 assert( pIdx->nColumn>=pIdx->nKeyCol+n );
113842 assert( pIdx->nColumn>=j );
113848 for(i=0; i<pTab->nCol; i++){
113849 if( !hasColumn(pPk->aiColumn, nPk, i)
113850 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
113853 for(i=0, j=nPk; i<pTab->nCol; i++){
113854 if( !hasColumn(pPk->aiColumn, j, i)
113855 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
113857 assert( j<pPk->nColumn );
113858 pPk->aiColumn[j] = i;
113859 pPk->azColl[j] = sqlite3StrBINARY;
113863 assert( pPk->nColumn==j );
113864 assert( pTab->nNVCol<=j );
113879 nName = sqlite3Strlen30(pTab->zName);
113880 if( sqlite3_strnicmp(zName, pTab->zName, nName)!=0 ) return 0;
113882 pMod = (Module*)sqlite3HashFind(&db->aModule, pTab->azModuleArg[0]);
113884 if( pMod->pModule->iVersion<3 ) return 0;
113885 if( pMod->pModule->xShadowName==0 ) return 0;
113886 return pMod->pModule->xShadowName(zName+nName+1);
113935 #define markExprListImmutable(X) /* no-op */
113948 ** this is a temporary table or db->init.busy==1. When db->init.busy==1
113967 sqlite3 *db = pParse->db; /* The database connection */
113974 assert( !db->mallocFailed );
113975 p = pParse->pNewTable;
113978 if( pSelect==0 && sqlite3ShadowTableName(db, p->zName) ){
113979 p->tabFlags |= TF_Shadow;
113982 /* If the db->init.busy is 1 it means we are reading the SQL off the
113985 ** for the table from the db->init.newTnum field. (The page number
113989 ** table itself. So mark it read-only.
113991 if( db->init.busy ){
113996 p->tnum = db->init.newTnum;
113997 if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
114000 assert( (p->tabFlags & TF_HasPrimaryKey)==0
114001 || p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
114002 assert( (p->tabFlags & TF_HasPrimaryKey)!=0
114003 || (p->iPKey<0 && sqlite3PrimaryKeyIndex(p)==0) );
114007 if( (p->tabFlags & TF_Autoincrement) ){
114012 if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
114013 sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
114016 p->tabFlags |= TF_WithoutRowid | TF_NoVisibleRowid;
114019 iDb = sqlite3SchemaToIndex(db, p->pSchema);
114024 if( p->pCheck ){
114025 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
114026 if( pParse->nErr ){
114029 sqlite3ExprListDelete(db, p->pCheck);
114030 p->pCheck = 0;
114032 markExprListImmutable(p->pCheck);
114037 if( p->tabFlags & TF_HasGenerated ){
114039 testcase( p->tabFlags & TF_HasVirtual );
114040 testcase( p->tabFlags & TF_HasStored );
114041 for(ii=0; ii<p->nCol; ii++){
114042 u32 colFlags = p->aCol[ii].colFlags;
114044 Expr *pX = p->aCol[ii].pDflt;
114055 p->aCol[ii].pDflt = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
114062 sqlite3ErrorMsg(pParse, "must have at least one non-generated column");
114070 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
114080 if( !db->init.busy ){
114095 if( p->pSelect==0 ){
114108 ** statement to populate the new table. The root-page number for the
114109 ** new table is in register pParse->regRoot.
114115 ** A shared-cache write-lock is not required to write to the new table,
114116 ** as a schema-lock must have already been obtained to create it. Since
114117 ** a schema-lock excludes all other database users, the write-lock would
114122 int regYield; /* Register holding co-routine entry-point */
114123 int addrTop; /* Top of the co-routine */
114129 regYield = ++pParse->nMem;
114130 regRec = ++pParse->nMem;
114131 regRowid = ++pParse->nMem;
114132 assert(pParse->nTab==1);
114134 sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
114136 pParse->nTab = 2;
114139 if( pParse->nErr ) return;
114142 assert( p->aCol==0 );
114143 p->nCol = p->nNVCol = pSelTab->nCol;
114144 p->aCol = pSelTab->aCol;
114145 pSelTab->nCol = 0;
114146 pSelTab->aCol = 0;
114150 if( pParse->nErr ) return;
114152 sqlite3VdbeJumpHere(v, addrTop - 1);
114168 Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
114169 n = (int)(pEnd2->z - pParse->sNameToken.z);
114170 if( pEnd2->z[0]!=';' ) n += pEnd2->n;
114172 "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
114184 db->aDb[iDb].zDbSName,
114186 p->zName,
114187 p->zName,
114188 pParse->regRoot,
114190 pParse->regRowid
114199 if( (p->tabFlags & TF_Autoincrement)!=0 ){
114200 Db *pDb = &db->aDb[iDb];
114202 if( pDb->pSchema->pSeqTab==0 ){
114205 pDb->zDbSName
114213 sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName),0);
114216 /* Add the table to the in-memory representation of the database.
114218 if( db->init.busy ){
114220 Schema *pSchema = p->pSchema;
114222 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
114228 pParse->pNewTable = 0;
114229 db->mDbFlags |= DBFLAG_SchemaChange;
114233 if( !pSelect && !p->pSelect ){
114235 if( pCons->z==0 ){
114238 p->addColOffset = 13 + (int)(pCons->z - pParse->sNameToken.z);
114264 sqlite3 *db = pParse->db;
114266 if( pParse->nVar>0 ){
114271 p = pParse->pNewTable;
114272 if( p==0 || pParse->nErr ) goto create_view_fail;
114274 iDb = sqlite3SchemaToIndex(db, p->pSchema);
114280 ** allocated rather than point to the input string - which means that
114283 pSelect->selFlags |= SF_View;
114285 p->pSelect = pSelect;
114288 p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
114290 p->pCheck = sqlite3ExprListDup(db, pCNames, EXPRDUP_REDUCE);
114291 if( db->mallocFailed ) goto create_view_fail;
114296 sEnd = pParse->sLastToken;
114302 n = (int)(sEnd.z - pBegin->z);
114304 z = pBegin->z;
114305 while( sqlite3Isspace(z[n-1]) ){ n--; }
114306 sEnd.z = &z[n-1];
114326 ** of errors. If an error is seen leave an error message in pParse->zErrMsg.
114333 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
114344 db->nSchemaLock++;
114346 db->nSchemaLock--;
114357 if( pTable->nCol>0 ) return 0;
114374 if( pTable->nCol<0 ){
114375 sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
114378 assert( pTable->nCol>=0 );
114387 assert( pTable->pSelect );
114388 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
114390 u8 eParseMode = pParse->eParseMode;
114391 pParse->eParseMode = PARSE_MODE_NORMAL;
114392 n = pParse->nTab;
114393 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
114394 pTable->nCol = -1;
114397 xAuth = db->xAuth;
114398 db->xAuth = 0;
114400 db->xAuth = xAuth;
114404 pParse->nTab = n;
114406 pTable->nCol = 0;
114408 }else if( pTable->pCheck ){
114411 ** arglist which is stored in pTable->pCheck. The pCheck field
114415 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
114416 &pTable->nCol, &pTable->aCol);
114417 if( db->mallocFailed==0
114418 && pParse->nErr==0
114419 && pTable->nCol==pSel->pEList->nExpr
114428 assert( pTable->aCol==0 );
114429 pTable->nCol = pSelTab->nCol;
114430 pTable->aCol = pSelTab->aCol;
114431 pTable->tabFlags |= (pSelTab->tabFlags & COLFLAG_NOINSERT);
114432 pSelTab->nCol = 0;
114433 pSelTab->aCol = 0;
114434 assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
114436 pTable->nNVCol = pTable->nCol;
114440 pParse->eParseMode = eParseMode;
114444 pTable->pSchema->schemaFlags |= DB_UnresetViews;
114445 if( db->mallocFailed ){
114447 pTable->aCol = 0;
114448 pTable->nCol = 0;
114463 for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
114465 if( pTab->pSelect ){
114467 pTab->aCol = 0;
114468 pTab->nCol = 0;
114480 ** root-page of a table or index in database iDb has changed from iFrom
114501 pDb = &db->aDb[iDb];
114502 pHash = &pDb->pSchema->tblHash;
114505 if( pTab->tnum==iFrom ){
114506 pTab->tnum = iTo;
114509 pHash = &pDb->pSchema->idxHash;
114512 if( pIdx->tnum==iFrom ){
114513 pIdx->tnum = iTo;
114520 ** Write code to erase the table with root-page iTable from database iDb.
114522 ** if a root-page of another table is moved by the btree-layer whilst
114523 ** erasing iTable (this can happen with an auto-vacuum database).
114533 ** is non-zero, then it is the root page number of a table moved to
114544 pParse->db->aDb[iDb].zDbSName, iTable, r1, r1);
114552 ** in case a root-page belonging to another table is moved by the btree layer
114553 ** is also added (this can happen with an auto-vacuum database).
114556 /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
114558 ** table and index root-pages in order, starting with the numerically
114559 ** largest root-page number. This guarantees that none of the root-pages
114567 ** and root page 5 happened to be the largest root-page number in the
114570 ** a free-list page.
114572 Pgno iTab = pTab->tnum;
114582 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114583 Pgno iIdx = pIdx->tnum;
114584 assert( pIdx->pSchema==pTab->pSchema );
114592 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
114593 assert( iDb>=0 && iDb<pParse->db->nDb );
114611 const char *zDbName = pParse->db->aDb[iDb].zDbSName;
114615 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
114629 sqlite3 *db = pParse->db;
114631 Db *pDb = &db->aDb[iDb];
114649 assert( pTrigger->pSchema==pTab->pSchema ||
114650 pTrigger->pSchema==db->aDb[1].pSchema );
114652 pTrigger = pTrigger->pNext;
114659 ** move as a result of the drop (can happen in auto-vacuum mode).
114661 if( pTab->tabFlags & TF_Autoincrement ){
114664 pDb->zDbSName, pTab->zName
114679 pDb->zDbSName, pTab->zName);
114688 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
114691 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
114697 ** Return TRUE if shadow tables should be read-only in the current
114702 if( (db->flags & SQLITE_Defensive)!=0
114703 && db->pVtabCtx==0
114704 && db->nVdbeExec==0
114716 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
114717 if( sqlite3StrNICmp(pTab->zName+7, "stat", 4)==0 ) return 0;
114718 if( sqlite3StrNICmp(pTab->zName+7, "parameters", 10)==0 ) return 0;
114721 if( (pTab->tabFlags & TF_Shadow)!=0 && sqlite3ReadOnlyShadowTables(db) ){
114734 sqlite3 *db = pParse->db;
114737 if( db->mallocFailed ){
114740 assert( pParse->nErr==0 );
114741 assert( pName->nSrc==1 );
114743 if( noErr ) db->suppressErr++;
114745 pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
114746 if( noErr ) db->suppressErr--;
114749 if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
114752 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114753 assert( iDb>=0 && iDb<db->nDb );
114765 const char *zDb = db->aDb[iDb].zDbSName;
114779 zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
114788 if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
114791 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
114797 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
114805 if( isView && pTab->pSelect==0 ){
114806 sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
114809 if( !isView && pTab->pSelect ){
114810 sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
114822 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
114843 ** under construction in the pParse->pNewTable field.
114855 sqlite3 *db = pParse->db;
114859 Table *p = pParse->pNewTable;
114868 int iCol = p->nCol-1;
114870 if( pToCol && pToCol->nExpr!=1 ){
114873 p->aCol[iCol].zName, pTo);
114877 }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
114883 nCol = pFromCol->nExpr;
114885 nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
114887 for(i=0; i<pToCol->nExpr; i++){
114888 nByte += sqlite3Strlen30(pToCol->a[i].zEName) + 1;
114895 pFKey->pFrom = p;
114896 pFKey->pNextFrom = p->pFKey;
114897 z = (char*)&pFKey->aCol[nCol];
114898 pFKey->zTo = z;
114902 memcpy(z, pTo->z, pTo->n);
114903 z[pTo->n] = 0;
114905 z += pTo->n+1;
114906 pFKey->nCol = nCol;
114908 pFKey->aCol[0].iFrom = p->nCol-1;
114912 for(j=0; j<p->nCol; j++){
114913 if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zEName)==0 ){
114914 pFKey->aCol[i].iFrom = j;
114918 if( j>=p->nCol ){
114921 pFromCol->a[i].zEName);
114925 sqlite3RenameTokenRemap(pParse, &pFKey->aCol[i], pFromCol->a[i].zEName);
114931 int n = sqlite3Strlen30(pToCol->a[i].zEName);
114932 pFKey->aCol[i].zCol = z;
114934 sqlite3RenameTokenRemap(pParse, z, pToCol->a[i].zEName);
114936 memcpy(z, pToCol->a[i].zEName, n);
114941 pFKey->isDeferred = 0;
114942 pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */
114943 pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */
114945 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
114946 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
114947 pFKey->zTo, (void *)pFKey
114954 assert( pNextTo->pPrevTo==0 );
114955 pFKey->pNextTo = pNextTo;
114956 pNextTo->pPrevTo = pFKey;
114961 p->pFKey = pFKey;
114982 if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
114983 assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
114984 pFKey->isDeferred = (u8)isDeferred;
114997 ** the root page number of the index is taken from pIndex->tnum.
115000 Table *pTab = pIndex->pTable; /* The table that is indexed */
115001 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
115002 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
115011 sqlite3 *db = pParse->db; /* The database connection */
115012 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
115015 if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
115016 db->aDb[iDb].zDbSName ) ){
115021 /* Require a write-lock on the table to perform this operation */
115022 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
115029 tnum = pIndex->tnum;
115032 assert( pKey!=0 || db->mallocFailed || pParse->nErr );
115035 iSorter = pParse->nTab++;
115036 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, pIndex->nKeyCol, (char*)
115062 pIndex->nKeyCol); VdbeCoverage(v);
115077 if( !pIndex->bAscKeyBug ){
115102 ** of 8-byte aligned space after the Index object and return a
115122 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
115123 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
115124 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
115125 p->aSortOrder = (u8*)pExtra;
115126 p->nColumn = nCol;
115127 p->nKeyCol = nCol - 1;
115136 ** pParse and return non-zero. Otherwise, return zero.
115141 for(i=0; i<pList->nExpr; i++){
115142 if( pList->a[i].bNulls ){
115143 u8 sf = pList->a[i].sortFlags;
115158 ** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable
115159 ** as the table to be indexed. pParse->pNewTable is a table that is
115163 ** is a primary key or unique-constraint on the most recent column added
115170 SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
115186 sqlite3 *db = pParse->db;
115196 if( db->mallocFailed || pParse->nErr>0 ){
115214 /* Use the two-part index name to determine the database
115221 assert( pName && pName->z );
115228 if( !db->init.busy ){
115230 if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
115242 pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
115243 assert( db->mallocFailed==0 || pTab==0 );
115245 if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
115247 "cannot create a TEMP index on non-TEMP table \"%s\"",
115248 pTab->zName);
115255 pTab = pParse->pNewTable;
115257 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115259 pDb = &db->aDb[iDb];
115262 assert( pParse->nErr==0 );
115263 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
115264 && db->init.busy==0
115267 && sqlite3UserAuthTable(pTab->zName)==0
115270 sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
115274 if( pTab->pSelect ){
115302 assert( pName->z!=0 );
115303 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
115307 if( !db->init.busy ){
115313 if( sqlite3FindIndex(db, zName, pDb->zDbSName)!=0 ){
115317 assert( !db->init.busy );
115326 for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
115327 zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
115344 const char *zDb = pDb->zDbSName;
115350 if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
115362 Column *pCol = &pTab->aCol[pTab->nCol-1];
115363 pCol->colFlags |= COLFLAG_UNIQUE;
115364 sqlite3TokenInit(&prevCol, pCol->zName);
115368 assert( pList->nExpr==1 );
115372 if( pParse->nErr ) goto exit_create_index;
115378 for(i=0; i<pList->nExpr; i++){
115379 Expr *pExpr = pList->a[i].pExpr;
115381 if( pExpr->op==TK_COLLATE ){
115382 nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
115390 nExtraCol = pPk ? pPk->nKeyCol : 1;
115391 assert( pList->nExpr + nExtraCol <= 32767 /* Fits in i16 */ );
115392 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
115394 if( db->mallocFailed ){
115397 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
115398 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
115399 pIndex->zName = zExtra;
115401 memcpy(pIndex->zName, zName, nName+1);
115402 pIndex->pTable = pTab;
115403 pIndex->onError = (u8)onError;
115404 pIndex->uniqNotNull = onError!=OE_None;
115405 pIndex->idxType = idxType;
115406 pIndex->pSchema = db->aDb[iDb].pSchema;
115407 pIndex->nKeyCol = pList->nExpr;
115410 pIndex->pPartIdxWhere = pPIWhere;
115417 if( pDb->pSchema->file_format>=4 ){
115418 sortOrderMask = -1; /* Honor DESC */
115426 ** populate pIndex->aColExpr and store XN_EXPR (-2) in aiColumn[].
115432 pListItem = pList->a;
115434 pIndex->aColExpr = pList;
115437 for(i=0; i<pIndex->nKeyCol; i++, pListItem++){
115438 Expr *pCExpr; /* The i-th index expression */
115439 int requestedSortOrder; /* ASC or DESC on the i-th expression */
115442 sqlite3StringToId(pListItem->pExpr);
115443 sqlite3ResolveSelfReference(pParse, pTab, NC_IdxExpr, pListItem->pExpr, 0);
115444 if( pParse->nErr ) goto exit_create_index;
115445 pCExpr = sqlite3ExprSkipCollate(pListItem->pExpr);
115446 if( pCExpr->op!=TK_COLUMN ){
115447 if( pTab==pParse->pNewTable ){
115452 if( pIndex->aColExpr==0 ){
115453 pIndex->aColExpr = pList;
115457 pIndex->aiColumn[i] = XN_EXPR;
115458 pIndex->uniqNotNull = 0;
115460 j = pCExpr->iColumn;
115463 j = pTab->iPKey;
115465 if( pTab->aCol[j].notNull==0 ){
115466 pIndex->uniqNotNull = 0;
115468 if( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL ){
115469 pIndex->bHasVCol = 1;
115472 pIndex->aiColumn[i] = (i16)j;
115475 if( pListItem->pExpr->op==TK_COLLATE ){
115477 zColl = pListItem->pExpr->u.zToken;
115483 nExtra -= nColl;
115485 zColl = pTab->aCol[j].zColl;
115488 if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
115491 pIndex->azColl[i] = zColl;
115492 requestedSortOrder = pListItem->sortFlags & sortOrderMask;
115493 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
115501 for(j=0; j<pPk->nKeyCol; j++){
115502 int x = pPk->aiColumn[j];
115504 if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
115505 pIndex->nColumn--;
115507 testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
115508 pIndex->aiColumn[i] = x;
115509 pIndex->azColl[i] = pPk->azColl[j];
115510 pIndex->aSortOrder[i] = pPk->aSortOrder[j];
115514 assert( i==pIndex->nColumn );
115516 pIndex->aiColumn[i] = XN_ROWID;
115517 pIndex->azColl[i] = sqlite3StrBINARY;
115520 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
115525 || pTab->iPKey<0 || sqlite3TableColumnToIndex(pIndex, pTab->iPKey)>=0 );
115527 if( pTblName!=0 && pIndex->nColumn>=pTab->nCol ){
115528 pIndex->isCovering = 1;
115529 for(j=0; j<pTab->nCol; j++){
115530 if( j==pTab->iPKey ) continue;
115532 pIndex->isCovering = 0;
115537 if( pTab==pParse->pNewTable ){
115560 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
115563 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
115566 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
115567 for(k=0; k<pIdx->nKeyCol; k++){
115570 assert( pIdx->aiColumn[k]>=0 );
115571 if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
115572 z1 = pIdx->azColl[k];
115573 z2 = pIndex->azColl[k];
115576 if( k==pIdx->nKeyCol ){
115577 if( pIdx->onError!=pIndex->onError ){
115585 if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
115589 if( pIdx->onError==OE_Default ){
115590 pIdx->onError = pIndex->onError;
115593 if( idxType==SQLITE_IDXTYPE_PRIMARYKEY ) pIdx->idxType = idxType;
115595 pIndex->pNext = pParse->pNewIndex;
115596 pParse->pNewIndex = pIndex;
115607 ** in-memory database structures.
115609 assert( pParse->nErr==0 );
115610 if( db->init.busy ){
115613 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
115615 pIndex->tnum = db->init.newTnum;
115618 pParse->rc = SQLITE_CORRUPT_BKPT;
115622 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
115623 pIndex->zName, pIndex);
115629 db->mDbFlags |= DBFLAG_SchemaChange;
115648 int iMem = ++pParse->nMem;
115661 pIndex->tnum = (Pgno)sqlite3VdbeAddOp0(v, OP_Noop);
115669 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
115670 if( pName->z[n-1]==';' ) n--;
115673 onError==OE_None ? "" : " UNIQUE", n, pName->z);
115684 db->aDb[iDb].zDbSName,
115685 pIndex->zName,
115686 pTab->zName,
115693 ** to invalidate all pre-compiled statements.
115699 sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), 0);
115703 sqlite3VdbeJumpHere(v, (int)pIndex->tnum);
115706 if( db->init.busy || pTblName==0 ){
115707 pIndex->pNext = pTab->pIndex;
115708 pTab->pIndex = pIndex;
115712 assert( pParse->pNewIndex==0 );
115713 pParse->pNewIndex = pIndex;
115725 Index **ppFrom = &pTab->pIndex;
115727 for(ppFrom=&pTab->pIndex; (pThis = *ppFrom)!=0; ppFrom=&pThis->pNext){
115729 if( pThis->onError!=OE_Replace ) continue;
115730 while( (pNext = pThis->pNext)!=0 && pNext->onError!=OE_Replace ){
115732 pThis->pNext = pNext->pNext;
115733 pNext->pNext = pThis;
115734 ppFrom = &pNext->pNext;
115742 for(pThis = pTab->pIndex; pThis; pThis=pThis->pNext){
115743 assert( pThis->onError!=OE_Replace
115744 || pThis->pNext==0
115745 || pThis->pNext->onError==OE_Replace );
115756 ** Fill the Index.aiRowEst[] array with default information - information
115766 ** aiRowEst[N]<=aiRowEst[N-1]
115776 LogEst *a = pIdx->aiRowLogEst;
115778 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
115782 assert( !pIdx->hasStat1 );
115788 ** 2020-05-27: If some of the stat data is coming from the sqlite_stat1
115794 x = pIdx->pTable->nRowLogEst;
115797 pIdx->pTable->nRowLogEst = x = 99;
115799 if( pIdx->pPartIdxWhere!=0 ) x -= 10; assert( 10==sqlite3LogEst(2) );
115805 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
115810 if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
115820 sqlite3 *db = pParse->db;
115823 assert( pParse->nErr==0 ); /* Never called with prior errors */
115824 if( db->mallocFailed ){
115827 assert( pName->nSrc==1 );
115831 pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
115836 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
115838 pParse->checkSchema = 1;
115841 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
115846 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
115850 Table *pTab = pIndex->pTable;
115851 const char *zDb = db->aDb[iDb].zDbSName;
115857 if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
115869 db->aDb[iDb].zDbSName, pIndex->zName
115871 sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
115873 destroyRootPage(pParse, pIndex->tnum, iDb);
115874 sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
115887 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
115895 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
115907 if( (n & (n-1))==0 ){
115911 *pIdx = -1;
115929 sqlite3 *db = pParse->db;
115935 pList->a = sqlite3ArrayAllocate(
115937 pList->a,
115938 sizeof(pList->a[0]),
115939 &pList->nId,
115946 pList->a[i].zName = sqlite3NameFromToken(db, pToken);
115947 if( IN_RENAME_OBJECT && pList->a[i].zName ){
115948 sqlite3RenameTokenMap(pParse, (void*)pList->a[i].zName, pToken);
115959 for(i=0; i<pList->nId; i++){
115960 sqlite3DbFree(db, pList->a[i].zName);
115962 sqlite3DbFree(db, pList->a);
115967 ** Return the index in pList of the identifier named zId. Return -1
115972 if( pList==0 ) return -1;
115973 for(i=0; i<pList->nId; i++){
115974 if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
115976 return -1;
115985 ** appropriate for small and memory-limited applications.
116008 ** the original SrcList unchanged, return NULL, and leave an error message
116014 int nExtra, /* Number of new slots to add to pSrc->a[] */
116015 int iStart /* Index in pSrc->a[] of first new slot */
116023 assert( iStart<=pSrc->nSrc );
116026 if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
116028 sqlite3_int64 nAlloc = 2*(sqlite3_int64)pSrc->nSrc+nExtra;
116029 sqlite3 *db = pParse->db;
116031 if( pSrc->nSrc+nExtra>=SQLITE_MAX_SRCLIST ){
116038 sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
116040 assert( db->mallocFailed );
116044 pSrc->nAlloc = nAlloc;
116049 for(i=pSrc->nSrc-1; i>=iStart; i--){
116050 pSrc->a[i+nExtra] = pSrc->a[i];
116052 pSrc->nSrc += nExtra;
116055 memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
116057 pSrc->a[i].iCursor = -1;
116110 assert( pParse->db!=0 );
116111 db = pParse->db;
116113 pList = sqlite3DbMallocRawNN(pParse->db, sizeof(SrcList) );
116115 pList->nAlloc = 1;
116116 pList->nSrc = 1;
116117 memset(&pList->a[0], 0, sizeof(pList->a[0]));
116118 pList->a[0].iCursor = -1;
116120 SrcList *pNew = sqlite3SrcListEnlarge(pParse, pList, 1, pList->nSrc);
116128 pItem = &pList->a[pList->nSrc-1];
116129 if( pDatabase && pDatabase->z==0 ){
116133 pItem->zName = sqlite3NameFromToken(db, pDatabase);
116134 pItem->zDatabase = sqlite3NameFromToken(db, pTable);
116136 pItem->zName = sqlite3NameFromToken(db, pTable);
116137 pItem->zDatabase = 0;
116148 assert(pList || pParse->db->mallocFailed );
116150 for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
116151 if( pItem->iCursor>=0 ) continue;
116152 pItem->iCursor = pParse->nTab++;
116153 if( pItem->pSelect ){
116154 sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
116167 for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
116168 if( pItem->zDatabase ) sqlite3DbFreeNN(db, pItem->zDatabase);
116169 sqlite3DbFree(db, pItem->zName);
116170 if( pItem->zAlias ) sqlite3DbFreeNN(db, pItem->zAlias);
116171 if( pItem->fg.isIndexedBy ) sqlite3DbFree(db, pItem->u1.zIndexedBy);
116172 if( pItem->fg.isTabFunc ) sqlite3ExprListDelete(db, pItem->u1.pFuncArg);
116173 sqlite3DeleteTable(db, pItem->pTab);
116174 if( pItem->pSelect ) sqlite3SelectDelete(db, pItem->pSelect);
116175 if( pItem->pOn ) sqlite3ExprDelete(db, pItem->pOn);
116176 if( pItem->pUsing ) sqlite3IdListDelete(db, pItem->pUsing);
116187 ** pDatabase is NULL if the database name qualifier is missing - the
116202 Token *pAlias, /* The right-hand side of the AS subexpression */
116208 sqlite3 *db = pParse->db;
116219 assert( p->nSrc>0 );
116220 pItem = &p->a[p->nSrc-1];
116222 assert( pItem->zName==0 || pDatabase!=0 );
116223 if( IN_RENAME_OBJECT && pItem->zName ){
116224 Token *pToken = (ALWAYS(pDatabase) && pDatabase->z) ? pDatabase : pTable;
116225 sqlite3RenameTokenMap(pParse, pItem->zName, pToken);
116228 if( pAlias->n ){
116229 pItem->zAlias = sqlite3NameFromToken(db, pAlias);
116231 pItem->pSelect = pSubquery;
116232 pItem->pOn = pOn;
116233 pItem->pUsing = pUsing;
116246 ** element of the source-list passed as the second argument.
116250 if( p && pIndexedBy->n>0 ){
116252 assert( p->nSrc>0 );
116253 pItem = &p->a[p->nSrc-1];
116254 assert( pItem->fg.notIndexed==0 );
116255 assert( pItem->fg.isIndexedBy==0 );
116256 assert( pItem->fg.isTabFunc==0 );
116257 if( pIndexedBy->n==1 && !pIndexedBy->z ){
116260 pItem->fg.notIndexed = 1;
116262 pItem->u1.zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
116263 pItem->fg.isIndexedBy = 1;
116274 assert( p1 && p1->nSrc==1 );
116276 SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
116278 sqlite3SrcListDelete(pParse->db, p2);
116281 memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
116282 sqlite3DbFree(pParse->db, p2);
116290 ** table-valued-function.
116294 SrcItem *pItem = &p->a[p->nSrc-1];
116295 assert( pItem->fg.notIndexed==0 );
116296 assert( pItem->fg.isIndexedBy==0 );
116297 assert( pItem->fg.isTabFunc==0 );
116298 pItem->u1.pFuncArg = pList;
116299 pItem->fg.isTabFunc = 1;
116301 sqlite3ExprListDelete(pParse->db, pList);
116317 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
116323 for(i=p->nSrc-1; i>0; i--){
116324 p->a[i].fg.jointype = p->a[i-1].fg.jointype;
116326 p->a[0].fg.jointype = 0;
116339 db = pParse->db;
116347 for(i=0; i<db->nDb; i++){
116349 Btree *pBt = db->aDb[i].pBt;
116374 assert( pParse->db!=0 );
116392 char *zName = sqlite3NameFromToken(pParse->db, pName);
116400 sqlite3DbFree(pParse->db, zName);
116412 sqlite3 *db = pParse->db;
116413 if( db->aDb[1].pBt==0 && !pParse->explain ){
116423 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
116427 pParse->rc = rc;
116430 db->aDb[1].pBt = pBt;
116431 assert( db->aDb[1].pSchema );
116432 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, 0, 0) ){
116443 ** will occur at the end of the top-level VDBE and will be generated
116447 assert( iDb>=0 && iDb<pToplevel->db->nDb );
116448 assert( pToplevel->db->aDb[iDb].pBt!=0 || iDb==1 );
116450 assert( sqlite3SchemaMutexHeld(pToplevel->db, iDb, 0) );
116451 if( DbMaskTest(pToplevel->cookieMask, iDb)==0 ){
116452 DbMaskSet(pToplevel->cookieMask, iDb);
116468 sqlite3 *db = pParse->db;
116470 for(i=0; i<db->nDb; i++){
116471 Db *pDb = &db->aDb[i];
116472 if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zDbSName)) ){
116494 DbMaskSet(pToplevel->writeMask, iDb);
116495 pToplevel->isMultiWrite |= setStatement;
116507 pToplevel->isMultiWrite = 1;
116528 pToplevel->mayAbort = 1;
116540 char *p4, /* Error message */
116545 assert( pParse->pVdbe!=0 );
116547 assert( (errCode&0xff)==SQLITE_CONSTRAINT || pParse->nested );
116566 Table *pTab = pIdx->pTable;
116568 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0,
116569 pParse->db->aLimit[SQLITE_LIMIT_LENGTH]);
116570 if( pIdx->aColExpr ){
116571 sqlite3_str_appendf(&errMsg, "index '%q'", pIdx->zName);
116573 for(j=0; j<pIdx->nKeyCol; j++){
116575 assert( pIdx->aiColumn[j]>=0 );
116576 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
116578 sqlite3_str_appendall(&errMsg, pTab->zName);
116592 ** Code an OP_Halt due to non-unique rowid.
116597 Table *pTab /* The table with the non-unique rowid */
116601 if( pTab->iPKey>=0 ){
116602 zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
116603 pTab->aCol[pTab->iPKey].zName);
116606 zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
116621 for(i=0; i<pIndex->nColumn; i++){
116622 const char *z = pIndex->azColl[i];
116623 assert( z!=0 || pIndex->aiColumn[i]<0 );
116624 if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
116641 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
116643 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
116645 sqlite3RefillIndex(pParse, pIndex, -1);
116661 sqlite3 *db = pParse->db; /* The database connection */
116666 for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
116668 for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){
116679 ** REINDEX -- 1
116680 ** REINDEX <collation> -- 2
116681 ** REINDEX ?<database>.?<tablename> -- 3
116682 ** REINDEX ?<database>.?<indexname> -- 4
116697 sqlite3 *db = pParse->db; /* The database connection */
116700 /* Read the database schema. If an error occurs, leave an error message
116709 }else if( NEVER(pName2==0) || pName2->z==0 ){
116711 assert( pName1->z );
116712 zColl = sqlite3NameFromToken(pParse->db, pName1);
116726 zDb = db->aDb[iDb].zDbSName;
116737 sqlite3RefillIndex(pParse, pIndex, -1);
116752 int nCol = pIdx->nColumn;
116753 int nKey = pIdx->nKeyCol;
116755 if( pParse->nErr ) return 0;
116756 if( pIdx->uniqNotNull ){
116757 pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
116759 pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
116764 const char *zColl = pIdx->azColl[i];
116765 pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
116767 pKey->aSortFlags[i] = pIdx->aSortOrder[i];
116768 assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
116770 if( pParse->nErr ){
116771 assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
116772 if( pIdx->bNoQuery==0 ){
116777 ** the missing index using the collation-needed callback. For
116780 pIdx->bNoQuery = 1;
116781 pParse->rc = SQLITE_ERROR_RETRY;
116796 Token *pName, /* Name of the common-table */
116802 sqlite3 *db = pParse->db;
116805 assert( pNew!=0 || db->mallocFailed );
116807 if( db->mallocFailed ){
116811 pNew->pSelect = pQuery;
116812 pNew->pCols = pArglist;
116813 pNew->zName = sqlite3NameFromToken(pParse->db, pName);
116814 pNew->eM10d = eM10d;
116825 sqlite3ExprListDelete(db, pCte->pCols);
116826 sqlite3SelectDelete(db, pCte->pSelect);
116827 sqlite3DbFree(db, pCte->zName);
116850 sqlite3 *db = pParse->db;
116860 zName = pCte->zName;
116863 for(i=0; i<pWith->nCte; i++){
116864 if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
116871 sqlite3_int64 nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
116876 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
116878 if( db->mallocFailed ){
116882 pNew->a[pNew->nCte++] = *pCte;
116895 for(i=0; i<pWith->nCte; i++){
116896 cteClear(db, &pWith->a[i]);
116928 assert( !db->xCollNeeded || !db->xCollNeeded16 );
116929 if( db->xCollNeeded ){
116932 db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
116936 if( db->xCollNeeded16 ){
116939 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
116942 db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
116953 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
116958 char *z = pColl->zName;
116963 if( pColl2->xCmp!=0 ){
116965 pColl->xDel = 0; /* Do not copy the destructor */
116984 if( pColl && pColl->xCmp==0 ){
116985 const char *zName = pColl->zName;
116986 sqlite3 *db = pParse->db;
117005 ** preferred for UTF-8, the second UTF-16le, and the third UTF-16be.
117017 pColl = sqlite3HashFind(&db->aCollSeq, zName);
117031 pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, pColl);
117049 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
117058 ** if necessary and generates an error message if the collating sequence
117074 if( pColl ) pColl += enc-1;
117076 pColl = db->pDfltColl;
117087 db->enc = enc;
117088 /* EVIDENCE-OF: R-08308-17224 The default collating function for all
117091 db->pDfltColl = sqlite3FindCollSeq(db, enc, sqlite3StrBINARY, 0);
117104 ** sequence can be found. If no collation is found, leave an error message.
117115 sqlite3 *db = pParse->db;
117121 if( !p || !p->xCmp ){
117128 if( p && !p->xCmp && synthCollSeq(db, p) ){
117131 assert( !p || p->xCmp );
117134 pParse->rc = SQLITE_ERROR_MISSING_COLLSEQ;
117150 ** another error occurs, NULL is returned and an error message written into
117155 ** and generates an error message.
117160 sqlite3 *db = pParse->db;
117162 u8 initbusy = db->init.busy;
117166 if( !initbusy && (!pColl || !pColl->xCmp) ){
117179 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
117180 ** is also -1. In other words, we are searching for a function that
117183 ** If nArg is -2 that means that we are searching for any function
117193 ** 4: UTF8/16 conversion required - argument count matches exactly
117194 ** 5: UTF16 byte order conversion required - argument count matches exactly
117197 ** If nArg==(-2) then any function with a non-null xSFunc is
117199 ** a non-match.
117204 int nArg, /* Desired number of arguments. (-1)==any */
117208 assert( p->nArg>=-1 );
117211 if( p->nArg!=nArg ){
117212 if( nArg==(-2) ) return (p->xSFunc==0) ? 0 : FUNC_PERFECT_MATCH;
117213 if( p->nArg>=0 ) return 0;
117218 if( p->nArg==nArg ){
117225 if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
117227 }else if( (enc & p->funcFlags & 2)!=0 ){
117243 for(p=sqlite3BuiltinFunctions.a[h]; p; p=p->u.pHash){
117244 if( sqlite3StrICmp(p->zName, zFunc)==0 ){
117267 assert( pOther!=&aDef[i] && pOther->pNext!=&aDef[i] );
117268 aDef[i].pNext = pOther->pNext;
117269 pOther->pNext = &aDef[i];
117282 ** indicating whether the function prefers UTF-16 over UTF-8. Return a
117290 ** If nArg is -2, then the first valid function found is returned. A
117291 ** function is valid if xSFunc is non-zero. The nArg==(-2)
117293 ** of arguments. If nArg is -2, then createFlag must be 0.
117301 const char *zName, /* Name of the function. zero-terminated */
117302 int nArg, /* Number of arguments. -1 means any number */
117312 assert( nArg>=(-2) );
117313 assert( nArg>=(-1) || createFlag==0 );
117316 /* First search for a match amongst the application-defined functions.
117318 p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName);
117325 p = p->pNext;
117328 /* If no match is found, search the built-in functions.
117330 ** If the DBFLAG_PreferBuiltin flag is set, then search the built-in
117331 ** functions even if a prior app-defined function was found. And give
117332 ** priority to built-in functions.
117337 ** new function. But the FuncDefs for built-in functions are read-only.
117338 ** So we must not search for built-ins when creating a new function.
117340 if( !createFlag && (pBest==0 || (db->mDbFlags & DBFLAG_PreferBuiltin)!=0) ){
117350 p = p->pNext;
117362 pBest->zName = (const char*)&pBest[1];
117363 pBest->nArg = (u16)nArg;
117364 pBest->funcFlags = enc;
117366 for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
117367 pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
117373 pBest->pNext = pOther;
117377 if( pBest && (pBest->xSFunc || createFlag) ){
117397 temp1 = pSchema->tblHash;
117398 temp2 = pSchema->trigHash;
117399 sqlite3HashInit(&pSchema->trigHash);
117400 sqlite3HashClear(&pSchema->idxHash);
117405 sqlite3HashInit(&pSchema->tblHash);
117411 sqlite3HashClear(&pSchema->fkeyHash);
117412 pSchema->pSeqTab = 0;
117413 if( pSchema->schemaFlags & DB_SchemaLoaded ){
117414 pSchema->iGeneration++;
117416 pSchema->schemaFlags &= ~(DB_SchemaLoaded|DB_ResetWanted);
117432 }else if ( 0==p->file_format ){
117433 sqlite3HashInit(&p->tblHash);
117434 sqlite3HashInit(&p->idxHash);
117435 sqlite3HashInit(&p->trigHash);
117436 sqlite3HashInit(&p->fkeyHash);
117437 p->enc = SQLITE_UTF8;
117465 ** return a pointer. Set an error message and return NULL if the table
117470 ** pSrc->a[0].pTab Pointer to the Table object
117471 ** pSrc->a[0].pIndex Pointer to the INDEXED BY index, if there is one
117475 SrcItem *pItem = pSrc->a;
117477 assert( pItem && pSrc->nSrc>=1 );
117479 sqlite3DeleteTable(pParse->db, pItem->pTab);
117480 pItem->pTab = pTab;
117482 pTab->nTabRef++;
117483 if( pItem->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pItem) ){
117490 /* Return true if table pTab is read-only.
117492 ** A table is read-only if any of the following are true:
117503 ** is for a top-level SQL statement.
117508 return sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0;
117510 if( (pTab->tabFlags & (TF_Readonly|TF_Shadow))==0 ) return 0;
117511 db = pParse->db;
117512 if( (pTab->tabFlags & TF_Readonly)!=0 ){
117513 return sqlite3WritableSchema(db)==0 && pParse->nested==0;
117515 assert( pTab->tabFlags & TF_Shadow );
117521 ** writable, generate an error message and return 1. If it is
117526 sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
117530 if( !viewOk && pTab->pSelect ){
117531 sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
117556 sqlite3 *db = pParse->db;
117557 int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
117561 assert( pFrom->nSrc==1 );
117562 pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
117563 pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
117564 assert( pFrom->a[0].pOn==0 );
117565 assert( pFrom->a[0].pUsing==0 );
117586 SrcList *pSrc, /* the FROM clause -- which tables to scan */
117592 sqlite3 *db = pParse->db;
117604 sqlite3ExprDelete(pParse->db, pWhere);
117605 sqlite3ExprListDelete(pParse->db, pOrderBy);
117625 pTab = pSrc->a[0].pTab;
117633 if( pPk->nKeyCol==1 ){
117634 const char *zName = pTab->aCol[pPk->aiColumn[0]].zName;
117639 for(i=0; i<pPk->nKeyCol; i++){
117640 Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
117645 pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
117652 pSrc->a[0].pTab = 0;
117654 pSrc->a[0].pTab = pTab;
117655 if( pSrc->a[0].fg.isIndexedBy ){
117656 pSrc->a[0].u2.pIBIndex = 0;
117657 pSrc->a[0].fg.isIndexedBy = 0;
117658 sqlite3DbFree(db, pSrc->a[0].u1.zIndexedBy);
117659 }else if( pSrc->a[0].fg.isCte ){
117660 pSrc->a[0].u2.pCteUse->nUse++;
117727 db = pParse->db;
117728 if( pParse->nErr || db->mallocFailed ){
117731 assert( pTabList->nSrc==1 );
117747 isView = pTab->pSelect!=0;
117777 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
117778 assert( iDb<db->nDb );
117779 rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0,
117780 db->aDb[iDb].zDbSName);
117789 assert( pTabList->nSrc==1 );
117790 iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
117791 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
117792 pParse->nTab++;
117798 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
117807 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
117836 if( (db->flags & SQLITE_CountRows)!=0
117837 && !pParse->nested
117838 && !pParse->pTriggerTab
117839 && !pParse->bReturning
117841 memCnt = ++pParse->nMem;
117852 ** IMPLEMENTATION-OF: R-17228-37124 If the action code is SQLITE_DELETE and
117862 && db->xPreUpdateCallback==0
117866 sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
117868 sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt ? memCnt : -1,
117869 pTab->zName, P4_STATIC);
117871 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
117872 assert( pIdx->pSchema==pTab->pSchema );
117873 sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
117885 iRowSet = ++pParse->nMem;
117892 nPk = pPk->nKeyCol;
117893 iPk = pParse->nMem+1;
117894 pParse->nMem += nPk;
117895 iEphCur = pParse->nTab++;
117904 ** ONEPASS_OFF: Two-pass approach - use a FIFO for rowids/PK values.
117905 ** ONEPASS_SINGLE: One-pass approach - at most one row deleted.
117906 ** ONEPASS_MULTI: One-pass approach - any number of rows may be deleted.
117926 assert( pPk->aiColumn[i]>=0 );
117928 pPk->aiColumn[i], iPk+i);
117932 iKey = ++pParse->nMem;
117933 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, -1, iKey);
117937 /* For ONEPASS, no need to store the rowid/primary-key. There is only
117948 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
117949 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
117955 iKey = ++pParse->nMem;
117958 sqlite3IndexAffinityStr(pParse->db, pPk), nPk);
117988 /* Set up a loop over the rowids/primary-keys that were found in the
117989 ** where-clause loop above.
117993 if( !IsVirtual(pTab) && aToOpen[iDataCur-iTabCur] ){
117994 assert( pPk!=0 || pTab->pSelect!=0 );
118022 pParse->isMultiWrite = 0;
118030 int count = (pParse->nested==0); /* True to count changes */
118035 /* End of the loop over all rowids/primary-keys. */
118046 } /* End non-truncate path */
118052 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
118100 ** cursor number iIdxCur+i for the i-th index.
118136 u8 count, /* If non-zero, increment the row change counter */
118141 Vdbe *v = pParse->pVdbe; /* Vdbe */
118175 iOld = pParse->nMem+1;
118176 pParse->nMem += (1 + pTab->nCol);
118178 /* Populate the OLD.* pseudo-table register array. These values will be
118181 for(iCol=0; iCol<pTab->nCol; iCol++){
118209 iIdxNoSeek = -1;
118222 ** If variable 'count' is non-zero, then this OP_Delete instruction should
118223 ** invoke the update-hook. The pre-update-hook, on the other hand should
118225 ** the update-hook is not invoked for rows removed by REPLACE, but the
118226 ** pre-update-hook is.
118228 if( pTab->pSelect==0 ){
118232 if( pParse->nested==0 || 0==sqlite3_stricmp(pTab->zName, "sqlite_stat1") ){
118274 ** cursor number iIdxCur+i for the i-th index. (The pTab->pIndex
118275 ** index is the 0-th index.)
118289 int r1 = -1; /* Register holding an index key */
118296 v = pParse->pVdbe;
118298 for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
118303 VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
118307 pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
118355 Vdbe *v = pParse->pVdbe;
118361 if( pIdx->pPartIdxWhere ){
118363 pParse->iSelfTab = iDataCur + 1;
118364 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
118366 pParse->iSelfTab = 0;
118367 pPrior = 0; /* Ticket a9efb42811fa41ee 2019-11-02;
118373 nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
118375 if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
118378 && pPrior->aiColumn[j]==pIdx->aiColumn[j]
118379 && pPrior->aiColumn[j]!=XN_EXPR
118401 ** If a prior call to sqlite3GenerateIndexKey() generated a jump-over label
118407 sqlite3VdbeResolveLabel(pParse->pVdbe, iLabel);
118424 ** This file contains the C-language implementations for many of the SQL
118441 assert( context->pVdbe!=0 );
118442 pOp = &context->pVdbe->aOp[context->iOp-1];
118443 assert( pOp->opcode==OP_CollSeq );
118444 assert( pOp->p4type==P4_COLLSEQ );
118445 return pOp->p4.pColl;
118453 assert( context->isError<=0 );
118454 context->isError = -1;
118455 context->skipFlag = 1;
118459 ** Implementation of the non-aggregate min() and max() functions
118472 mask = sqlite3_user_data(context)==0 ? 0 : -1;
118475 assert( mask==-1 || mask==0 );
118497 int i = sqlite3_value_type(argv[0]) - 1;
118505 /* EVIDENCE-OF: R-01470-60482 The sqlite3_value_type(V) interface returns
118509 sqlite3_result_text(context, azType[i], -1, SQLITE_STATIC);
118542 sqlite3_result_int(context, (int)(z-z0));
118555 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
118566 /* IMP: R-31676-45509 If X is the integer -9223372036854775808
118568 ** equivalent positive 64-bit two complement value. */
118569 sqlite3_result_error(context, "integer overflow", -1);
118572 iVal = -iVal;
118578 /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
118585 ** IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
118589 if( rVal<0 ) rVal = -rVal;
118656 nHaystack--;
118687 x.nArg = argc-1;
118690 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
118703 ** p1 is 1-indexed. So substr(x,1,1) returns the first character
118704 ** of x. If x is text, then we actually count UTF-8 characters.
118748 ** as substr(X,1,N) - it returns the first N characters of X. This
118749 ** is essentially a back-out of the bug-fix in check-in [5fc125d362df4b8]
118750 ** from 2009-02-02 for compatibility of applications that exploited the
118757 p2 = -p2;
118761 p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
118771 p1--;
118773 p2--;
118776 p1 -= p2;
118786 p1--;
118788 for(z2=z; *z2 && p2; p2--){
118791 sqlite3_result_text64(context, (char*)z, z2-z, SQLITE_TRANSIENT,
118795 p2 = len-p1;
118819 /* If Y==0 and X will fit in a 64-bit int,
118823 if( r<-4503599627370496.0 || r>+4503599627370496.0 ){
118826 r = (double)((sqlite_int64)(r+(r<0?-0.5:+0.5)));
118851 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
118852 testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
118853 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
118915 #define noopFunc versionFunc /* Substitute function - never called */
118930 ** (or -9223372036854775808) since when you do abs() of that
118935 ** therefore be no less than -9223372036854775807.
118937 r = -(r & LARGEST_INT64);
118977 /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
118986 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
119011 /* IMP: R-52756-41993 This function is a wrapper around the
119017 ** A structure defining how to do GLOB-style comparisons.
119040 /* The correct SQL-92 behavior is for the LIKE operator to ignore
119055 ** Compare two UTF-8 strings for equality where the first string is
119075 ** range of characters can be specified using '-'. Example:
119076 ** "[a-z]" matches any single lower-case letter. To match a '-', make
119099 u32 matchOne = pInfo->matchOne; /* "?" or "_" */
119100 u32 matchAll = pInfo->matchAll; /* "*" or "%" */
119101 u8 noCase = pInfo->noCase; /* True if uppercase==lowercase */
119118 if( pInfo->matchSet==0 ){
119124 assert( matchOther<0x80 ); /* '[' is a single-byte character */
119126 int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
119139 ** For a case-insensitive search, set variable cx to be the same as
119172 if( pInfo->matchSet==0 ){
119192 if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
119223 ** non-zero if there is no match.
119230 ** The sqlite3_strlike() interface. Return 0 on a match and non-zero for
119231 ** a miss - like strcmp().
119249 ** the build-in LIKE operator. The first argument to the function is the
119287 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
119288 testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
119289 if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
119290 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
119294 /* The escape character string must consist of a single UTF-8 character.
119299 if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
119301 "ESCAPE expression must be a single character", -1);
119305 if( escape==pInfo->matchAll || escape==pInfo->matchOne ){
119308 if( escape==pInfo->matchAll ) pInfo->matchAll = 0;
119309 if( escape==pInfo->matchOne ) pInfo->matchOne = 0;
119312 escape = pInfo->matchSet;
119352 /* IMP: R-48699-48617 This function is an SQL wrapper around the
119353 ** sqlite3_libversion() C-interface. */
119354 sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
119368 /* IMP: R-24470-31136 This function is an SQL wrapper around the
119370 sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
119376 ** its side-effects.
119402 /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
119426 /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
119430 sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
119434 /* Array for converting from half-bytes (nybbles) into ASCII hex
119446 ** single-quote escapes.
119461 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
119484 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
119521 ** The unicode() function. Return the integer unicode code-point value
119573 sqlite3_result_text64(context, (char*)z, zOut-z, sqlite3_free, SQLITE_UTF8);
119606 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
119619 rc = sqlite3_result_zeroblob64(context, n); /* IMP: R-00293-64994 */
119658 || sqlite3_context_db_handle(context)->mallocFailed );
119678 loopLimit = nStr - nPattern;
119685 nOut += nRep - nPattern;
119686 testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
119687 testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
119688 if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
119694 if( (cntExpand&(cntExpand-1))==0 ){
119699 zOut = sqlite3Realloc(zOut, (int)nOut + (nOut - nStr - 1));
119709 i += nPattern-1;
119712 assert( j+nStr-i+1<=nOut );
119713 memcpy(&zOut[j], &zStr[i], nStr-i);
119714 j += nStr - i;
119768 aLen[nChar] = (u8)(z - azChar[nChar]);
119783 nIn -= len;
119791 if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
119794 nIn -= len;
119809 ** when the SQLITE_ENABLE_UNKNOWN_FUNCTION compile-time option is used.
119810 ** When the "sqlite3" command-line shell is built using this functionality,
119812 ** involving application-defined functions to be examined in a generic
119820 /* no-op */
119825 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
119826 ** is only available if the SQLITE_SOUNDEX compile-time option is used
119833 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
119878 /* IMP: R-64894-50321 The string "?000" is returned if the argument
119887 ** A function that loads a shared-library extension then returns NULL.
119898 if( (db->flags & SQLITE_LoadExtFunc)==0 ){
119899 sqlite3_result_error(context, "not authorized", -1);
119909 sqlite3_result_error(context, zErrMsg, -1);
119926 u8 approx; /* True if non-integer value was input to the sum */
119947 p->cnt++;
119950 p->rSum += v;
119951 if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
119952 p->approx = p->overflow = 1;
119955 p->rSum += sqlite3_value_double(argv[0]);
119956 p->approx = 1;
119968 /* p is always non-NULL because sumStep() will have been called first
119971 assert( p->cnt>0 );
119972 p->cnt--;
119973 assert( type==SQLITE_INTEGER || p->approx );
119974 if( type==SQLITE_INTEGER && p->approx==0 ){
119976 p->rSum -= v;
119977 p->iSum -= v;
119979 p->rSum -= sqlite3_value_double(argv[0]);
119989 if( p && p->cnt>0 ){
119990 if( p->overflow ){
119991 sqlite3_result_error(context,"integer overflow",-1);
119992 }else if( p->approx ){
119993 sqlite3_result_double(context, p->rSum);
119995 sqlite3_result_int64(context, p->iSum);
120002 if( p && p->cnt>0 ){
120003 sqlite3_result_double(context, p->rSum/(double)p->cnt);
120010 sqlite3_result_double(context, p ? p->rSum : (double)0);
120032 p->n++;
120039 ** expressed as a 32-bit integer. */
120040 assert( argc==1 || p==0 || p->n>0x7fffffff || p->bInverse
120041 || p->n==sqlite3_aggregate_count(context) );
120047 sqlite3_result_int64(context, p ? p->n : 0);
120053 /* p is always non-NULL since countStep() will have been called first */
120055 p->n--;
120057 p->bInverse = 1;
120081 if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
120082 }else if( pBest->flags ){
120089 ** sqlite3_user_data() function returns (void *)-1. For min() it
120102 pBest->db = sqlite3_context_db_handle(context);
120110 if( pRes->flags ){
120145 int firstTerm = pAccum->mxAlloc==0;
120146 pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
120173 /* pAccum is always non-NULL since groupConcatStep() will have always
120182 if( n>=(int)pAccum->nChar ){
120183 pAccum->nChar = 0;
120185 pAccum->nChar -= n;
120186 memmove(pAccum->zText, &pAccum->zText[n], pAccum->nChar);
120188 if( pAccum->nChar==0 ) pAccum->mxAlloc = 0;
120198 if( pAccum->accError==SQLITE_TOOBIG ){
120200 }else if( pAccum->accError==SQLITE_NOMEM ){
120203 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
120213 if( pAccum->accError==SQLITE_TOOBIG ){
120215 }else if( pAccum->accError==SQLITE_NOMEM ){
120219 sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
120228 ** This routine does per-connection function registration. Most
120229 ** of the built-in functions above are part of the global function set.
120241 ** Re-register the built-in LIKE functions. The caseSensitive
120257 sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
120258 sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
120266 ** LIKE-style function then return FALSE.
120282 assert( pExpr->op==TK_FUNCTION );
120283 if( !pExpr->x.pList ){
120287 nExpr = pExpr->x.pList->nExpr;
120288 pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
120292 if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
120300 memcpy(aWc, pDef->pUserData, 3);
120308 Expr *pEscape = pExpr->x.pList->a[2].pExpr;
120310 if( pEscape->op!=TK_STRING ) return 0;
120311 zEscape = pEscape->u.zToken;
120318 *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
120334 /* Extra math functions that require linking with -lm
120380 ** ln(X) - natural logarithm
120381 ** log(X) - log X base 10
120382 ** log10(X) - log X base 10
120383 ** log(B,X) - log X base B
120439 ** Implementation of 1-argument SQL math functions:
120441 ** exp(X) - Compute e to the X-th power
120461 ** Implementation of 2-argument SQL math functions:
120463 ** power(X,Y) - Compute X to the Y-th power
120486 ** Implementation of 2-argument SQL math functions:
120488 ** power(X,Y) - Compute X to the Y-th power
120516 sqlite3_result_int(context, x<0.0 ? -1 : x>0.0 ? +1 : 0);
120521 ** to the global function hash table. This occurs at start-time (as
120532 ** FuncDef.pHash elements at start-time. The elements of this array
120533 ** are read-only after initialization is complete.
120573 FUNCTION(min, -1, 0, 1, minmaxFunc ),
120577 FUNCTION(max, -1, 1, 1, minmaxFunc ),
120584 FUNCTION(printf, -1, 0, 0, printfFunc ),
120586 FUNCTION(char, -1, 0, 0, charFunc ),
120633 FUNCTION(unknown, -1, 0, 0, unknownFunc ),
120674 INLINE_FUNC(coalesce, -1, INLINEFUNC_coalesce, 0 ),
120684 #if 0 /* Enable to print out how the built-in functions are hashed */
120689 printf("FUNC-HASH %02d:", i);
120690 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash){
120691 int n = sqlite3Strlen30(p->zName);
120692 int h = p->zName[0] + n;
120693 printf(" %s(%d)", p->zName, h);
120723 ** --------------------------
120805 ** ---------------
120807 ** Before coding an UPDATE or DELETE row operation, the code-generator
120812 ** accessed). No information is required by the code-generator before
120816 ** sqlite3FkRequired() - Test to see if FK processing is required.
120817 ** sqlite3FkOldmask() - Query for the set of required old.* columns.
120821 ** --------------------------------------
120823 ** sqlite3FkCheck() - Check for foreign key violations.
120824 ** sqlite3FkActions() - Code triggers for ON UPDATE/ON DELETE actions.
120825 ** sqlite3FkDelete() - Delete an FKey structure.
120830 ** -----------------------
120860 ** constraint to the parent table column stored in the left-most column
120862 ** child table column that corresponds to the second left-most column of
120881 ** then non-zero is returned, and a "foreign key mismatch" error loaded
120882 ** into pParse. If an OOM error occurs, non-zero is returned and the
120883 ** pParse->db->mallocFailed flag is set.
120894 int nCol = pFKey->nCol; /* Number of columns in parent key */
120895 char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */
120902 /* If this is a non-composite (single column) foreign key, check if it
120908 ** Non-composite foreign keys do not require the aiCol array.
120918 if( pParent->iPKey>=0 ){
120920 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
120924 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
120929 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
120930 if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) && pIdx->pPartIdxWhere==0 ){
120942 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
120947 /* If zKey is non-NULL, then this foreign key was declared to
120953 i16 iCol = pIdx->aiColumn[i]; /* Index of column in parent tbl */
120962 zDfltColl = pParent->aCol[iCol].zColl;
120964 if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
120966 zIdxCol = pParent->aCol[iCol].zName;
120968 if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
120969 if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
120981 if( !pParse->disableTriggers ){
120983 "foreign key mismatch - \"%w\" referencing \"%w\"",
120984 pFKey->pFrom->zName, pFKey->zTo);
120986 sqlite3DbFree(pParse->db, aiCol);
120998 ** affected - once to "delete" the old row, and then again to "insert" the
121008 ** --------------------------------------------------------------------------
121033 int iCur = pParse->nTab - 1; /* Cursor number to use */
121037 (!pFKey->isDeferred
121038 && !(pParse->db->flags & SQLITE_DeferFKs)
121039 && !pParse->pToplevel
121040 && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
121050 sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
121053 for(i=0; i<pFKey->nCol; i++){
121054 int iReg = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i]) + regData + 1;
121071 sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[0])+1+regData, regTemp);
121076 ** to increment the constraint-counter (i.e. this is an INSERT operation),
121078 ** increment the constraint-counter. */
121079 if( pTab==pFKey->pFrom && nIncr==1 ){
121087 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
121091 int nCol = pFKey->nCol;
121095 sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
121099 sqlite3TableColumnToStorage(pFKey->pFrom, aiCol[i])+1+regData,
121104 ** to increment the constraint-counter (i.e. this is an INSERT operation),
121106 ** increment the constraint-counter.
121108 ** If any of the parent-key values are NULL, then the row cannot match
121110 ** of the parent-key values are NULL (at this point it is known that
121113 if( pTab==pFKey->pFrom && nIncr==1 ){
121116 int iChild = sqlite3TableColumnToStorage(pFKey->pFrom,aiCol[i])
121119 iParent += sqlite3TableColumnToStorage(pIdx->pTable,
121120 pIdx->aiColumn[i]);
121121 assert( pIdx->aiColumn[i]>=0 );
121122 assert( aiCol[i]!=pTab->iPKey );
121123 if( pIdx->aiColumn[i]==pTab->iPKey ){
121134 sqlite3IndexAffinityStr(pParse->db,pIdx), nCol);
121142 if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
121143 && !pParse->pToplevel
121144 && !pParse->isMultiWrite
121154 if( nIncr>0 && pFKey->isDeferred==0 ){
121157 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
121182 sqlite3 *db = pParse->db;
121186 if( iCol>=0 && iCol!=pTab->iPKey ){
121187 pCol = &pTab->aCol[iCol];
121188 pExpr->iTable = regBase + sqlite3TableColumnToStorage(pTab,iCol) + 1;
121189 pExpr->affExpr = pCol->affinity;
121190 zColl = pCol->zColl;
121191 if( zColl==0 ) zColl = db->pDfltColl->zName;
121194 pExpr->iTable = regBase;
121195 pExpr->affExpr = SQLITE_AFF_INTEGER;
121213 pExpr->y.pTab = pTab;
121214 pExpr->iTable = iCursor;
121215 pExpr->iColumn = iCol;
121224 ** code for an SQL UPDATE operation, this function may be called twice -
121227 ** Parameter nIncr is passed -1 when inserting a row (as this may decrease
121236 ** --------------------------------------------------------------------------
121262 sqlite3 *db = pParse->db; /* Database handle */
121270 assert( pIdx==0 || pIdx->pTable==pTab );
121271 assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
121272 assert( pIdx!=0 || pFKey->nCol==1 );
121276 iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
121282 ** <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
121288 for(i=0; i<pFKey->nCol; i++){
121295 iCol = pIdx ? pIdx->aiColumn[i] : -1;
121297 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
121299 zCol = pFKey->pFrom->aCol[iCol].zName;
121319 if( pTab==pFKey->pFrom && nIncr>0 ){
121324 pLeft = exprTableRegister(pParse, pTab, regData, -1);
121325 pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
121330 for(i=0; i<pIdx->nKeyCol; i++){
121331 i16 iCol = pIdx->aiColumn[i];
121334 pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName);
121352 if( pParse->nErr==0 ){
121354 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
121382 return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName);
121388 ** and all of its sub-components.
121390 ** The Trigger structure or any of its sub-components may be allocated from
121395 TriggerStep *pStep = p->step_list;
121396 sqlite3ExprDelete(dbMem, pStep->pWhere);
121397 sqlite3ExprListDelete(dbMem, pStep->pExprList);
121398 sqlite3SelectDelete(dbMem, pStep->pSelect);
121399 sqlite3ExprDelete(dbMem, p->pWhen);
121422 sqlite3 *db = pParse->db;
121423 if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) ){
121428 assert( pTab->pSelect==0 ); /* Not a view */
121436 for(p=pTab->pFKey; p; p=p->pNextFrom){
121437 if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
121444 pParse->disableTriggers = 1;
121446 pParse->disableTriggers = 0;
121457 if( (db->flags & SQLITE_DeferFKs)==0 ){
121478 ** is set to -1). If the rowid column is modified by the UPDATE statement
121479 ** the bChngRowid argument is non-zero.
121491 for(i=0; i<p->nCol; i++){
121492 int iChildKey = p->aCol[i].iFrom;
121494 if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
121505 ** is set to -1). If the rowid column is modified by the UPDATE statement
121506 ** the bChngRowid argument is non-zero.
121518 for(i=0; i<p->nCol; i++){
121519 char *zKey = p->aCol[i].zCol;
121521 for(iKey=0; iKey<pTab->nCol; iKey++){
121522 if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
121523 Column *pCol = &pTab->aCol[iKey];
121525 if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
121526 }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
121542 if( pTop->pTriggerPrg ){
121543 Trigger *p = pTop->pTriggerPrg->pTrigger;
121544 if( (p==pFKey->apTrigger[0] && pFKey->aAction[0]==OE_SetNull)
121545 || (p==pFKey->apTrigger[1] && pFKey->aAction[1]==OE_SetNull)
121559 ** first register in an array of (pTab->nCol+1) registers containing the
121565 ** first register of an array of (pTab->nCol+1) registers containing the new
121581 sqlite3 *db = pParse->db; /* Database handle */
121585 int isIgnoreErrors = pParse->disableTriggers;
121587 /* Exactly one of regOld and regNew should be non-zero. */
121590 /* If foreign-keys are disabled, this function is a no-op. */
121591 if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
121593 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
121594 zDb = db->aDb[iDb].zDbSName;
121598 for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
121608 && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
121618 if( pParse->disableTriggers ){
121619 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
121621 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
121625 if( !isIgnoreErrors || db->mallocFailed ) return;
121632 ** FK counter for each row of the current table with non-NULL keys.
121635 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
121636 for(i=0; i<pFKey->nCol; i++){
121638 iFromCol = pFKey->aCol[i].iFrom;
121639 iReg = sqlite3TableColumnToStorage(pFKey->pFrom,iFromCol) + regOld+1;
121642 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
121646 assert( pFKey->nCol==1 || (aiFree && pIdx) );
121651 iCol = pFKey->aCol[0].iFrom;
121654 for(i=0; i<pFKey->nCol; i++){
121655 if( aiCol[i]==pTab->iPKey ){
121656 aiCol[i] = -1;
121658 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
121663 if( db->xAuth ){
121665 char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
121666 rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
121672 /* Take a shared-cache advisory read-lock on the parent table. Allocate
121675 sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
121676 pParse->nTab++;
121682 fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1, bIgnore);
121701 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
121710 if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
121711 && !pParse->pToplevel && !pParse->isMultiWrite
121720 if( !isIgnoreErrors || db->mallocFailed ) return;
121723 assert( aiCol || pFKey->nCol==1 );
121729 SrcItem *pItem = pSrc->a;
121730 pItem->pTab = pFKey->pFrom;
121731 pItem->zName = pFKey->pFrom->zName;
121732 pItem->pTab->nTabRef++;
121733 pItem->iCursor = pParse->nTab++;
121736 fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
121739 int eAction = pFKey->aAction[aChange!=0];
121744 ** So do not set the "may-abort" flag in this case.
121747 ** may-abort flag will eventually be set on this statement anyway
121758 if( !pFKey->isDeferred && eAction!=OE_Cascade && eAction!=OE_SetNull ){
121762 pItem->zName = 0;
121780 if( pParse->db->flags&SQLITE_ForeignKeys ){
121783 for(p=pTab->pFKey; p; p=p->pNextFrom){
121784 for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
121786 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
121790 for(i=0; i<pIdx->nKeyCol; i++){
121791 assert( pIdx->aiColumn[i]>=0 );
121792 mask |= COLUMN_MASK(pIdx->aiColumn[i]);
121807 ** entry in the aChange[] array is set to -1. If the column is modified,
121812 ** non-zero. If there is no foreign key related processing, this function
121828 int *aChange, /* Non-NULL for UPDATE operations */
121833 if( pParse->db->flags&SQLITE_ForeignKeys ){
121838 bHaveFK = (sqlite3FkReferences(pTab) || pTab->pFKey);
121845 for(p=pTab->pFKey; p; p=p->pNextFrom){
121847 if( 0==sqlite3_stricmp(pTab->zName, p->zTo) ) eRet = 2;
121853 for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
121855 if( p->aAction[1]!=OE_None ) return 2;
121866 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
121875 ** sub-system, code for them is created by fkScanChildren()).
121897 ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */
121899 sqlite3 *db = pParse->db; /* Database handle */
121904 action = pFKey->aAction[iAction];
121905 if( action==OE_Restrict && (db->flags & SQLITE_DeferFKs) ){
121908 pTrigger = pFKey->apTrigger[iAction];
121914 int *aiCol = 0; /* child table cols -> parent key cols */
121923 assert( aiCol || pFKey->nCol==1 );
121925 for(i=0; i<pFKey->nCol; i++){
121933 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
121935 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
121936 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
121938 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
121939 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
121977 Column *pCol = pFKey->pFrom->aCol + iFromCol;
121979 if( pCol->colFlags & COLFLAG_GENERATED ){
121980 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
121981 testcase( pCol->colFlags & COLFLAG_STORED );
121984 pDflt = pCol->pDflt;
122000 zFrom = pFKey->pFrom->zName;
122011 pRaise->affExpr = OE_Abort;
122028 nFrom + 1 /* Space for pStep->zTarget */
122031 pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
122032 pStep->zTarget = (char *)&pStep[1];
122033 memcpy((char *)pStep->zTarget, zFrom, nFrom);
122035 pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
122036 pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
122037 pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
122040 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
122044 /* Re-enable the lookaside buffer, if it was disabled earlier. */
122051 if( db->mallocFailed==1 ){
122060 pStep->op = TK_SELECT;
122064 pStep->op = TK_DELETE;
122069 pStep->op = TK_UPDATE;
122071 pStep->pTrig = pTrigger;
122072 pTrigger->pSchema = pTab->pSchema;
122073 pTrigger->pTabSchema = pTab->pSchema;
122074 pFKey->apTrigger[iAction] = pTrigger;
122075 pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
122088 ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */
122093 /* If foreign-key support is enabled, iterate through all FKs that
122096 ** trigger sub-program. */
122097 if( pParse->db->flags&SQLITE_ForeignKeys ){
122099 for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
122119 FKey *pNext; /* Copy of pFKey->pNextFrom */
122122 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
122123 for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
122126 if( !db || db->pnBytesFreed==0 ){
122127 if( pFKey->pPrevTo ){
122128 pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
122130 void *p = (void *)pFKey->pNextTo;
122131 const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
122132 sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, p);
122134 if( pFKey->pNextTo ){
122135 pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
122139 /* EV: R-30323-21917 Each foreign key constraint in SQLite is
122142 assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
122146 fkTriggerDelete(db, pFKey->apTrigger[0]);
122147 fkTriggerDelete(db, pFKey->apTrigger[1]);
122150 pNext = pFKey->pNextFrom;
122192 assert( pParse->pVdbe!=0 );
122193 v = pParse->pVdbe;
122195 sqlite3TableLock(pParse, iDb, pTab->tnum,
122196 (opcode==OP_OpenWrite)?1:0, pTab->zName);
122198 sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nNVCol);
122199 VdbeComment((v, "%s", pTab->zName));
122203 assert( pPk->tnum==pTab->tnum );
122204 sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
122206 VdbeComment((v, "%s", pTab->zName));
122216 ** ------------------------------
122231 if( !pIdx->zColAff ){
122241 Table *pTab = pIdx->pTable;
122242 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
122243 if( !pIdx->zColAff ){
122247 for(n=0; n<pIdx->nColumn; n++){
122248 i16 x = pIdx->aiColumn[n];
122251 aff = pTab->aCol[x].affinity;
122256 assert( pIdx->aColExpr!=0 );
122257 aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
122261 pIdx->zColAff[n] = aff;
122263 pIdx->zColAff[n] = 0;
122266 return pIdx->zColAff;
122282 ** ------------------------------
122291 char *zColAff = pTab->zColAff;
122294 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
122300 for(i=j=0; i<pTab->nCol; i++){
122301 assert( pTab->aCol[i].affinity!=0 );
122302 if( (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ){
122303 zColAff[j++] = pTab->aCol[i].affinity;
122307 zColAff[j--] = 0;
122309 pTab->zColAff = zColAff;
122317 sqlite3VdbeChangeP4(v, -1, zColAff, i);
122323 ** Return non-zero if the table pTab in database iDb or any of its indices
122333 VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
122339 if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
122341 Pgno tnum = pOp->p2;
122342 if( tnum==pTab->tnum ){
122345 for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
122346 if( tnum==pIndex->tnum ){
122352 if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
122353 assert( pOp->p4.pVtab!=0 );
122354 assert( pOp->p4type==P4_VTAB );
122366 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 ){
122367 assert( pExpr->iColumn < pWalker->u.pTab->nCol );
122368 pWalker->eCode |= pWalker->u.pTab->aCol[pExpr->iColumn].colFlags;
122392 assert( pTab->tabFlags & TF_HasGenerated );
122393 testcase( pTab->tabFlags & TF_HasVirtual );
122394 testcase( pTab->tabFlags & TF_HasStored );
122399 sqlite3TableAffinity(pParse->pVdbe, pTab, iRegStore);
122400 if( (pTab->tabFlags & TF_HasStored)!=0
122401 && (pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1))->opcode==OP_Affinity
122404 ** columns. '@' is the no-op affinity and those columns have not
122407 char *zP4 = pOp->p4.z;
122409 assert( pOp->p4type==P4_DYNAMIC );
122411 if( pTab->aCol[ii].colFlags & COLFLAG_VIRTUAL ){
122414 if( pTab->aCol[ii].colFlags & COLFLAG_STORED ){
122422 ** this is a two-pass algorithm. On the first pass, mark all generated
122425 for(i=0; i<pTab->nCol; i++){
122426 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
122427 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
122428 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
122429 pTab->aCol[i].colFlags |= COLFLAG_NOTAVAIL;
122438 /* On the second pass, compute the value of each NOT-AVAILABLE column.
122443 pParse->iSelfTab = -iRegStore;
122447 for(i=0; i<pTab->nCol; i++){
122448 Column *pCol = pTab->aCol + i;
122449 if( (pCol->colFlags & COLFLAG_NOTAVAIL)!=0 ){
122451 pCol->colFlags |= COLFLAG_BUSY;
122453 sqlite3WalkExpr(&w, pCol->pDflt);
122454 pCol->colFlags &= ~COLFLAG_BUSY;
122460 assert( pCol->colFlags & COLFLAG_GENERATED );
122463 pCol->colFlags &= ~COLFLAG_NOTAVAIL;
122468 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pRedo->zName);
122470 pParse->iSelfTab = 0;
122505 assert( pParse->db->aDb[iDb].pSchema!=0 );
122506 if( (pTab->tabFlags & TF_Autoincrement)!=0
122507 && (pParse->db->mDbFlags & DBFLAG_Vacuum)==0
122511 Table *pSeqTab = pParse->db->aDb[iDb].pSchema->pSeqTab;
122515 ** Ticket d8dc2b3a58cd5dc2918a1d4acb 2018-05-23 */
122519 || pSeqTab->nCol!=2
122521 pParse->nErr++;
122522 pParse->rc = SQLITE_CORRUPT_SEQUENCE;
122526 pInfo = pToplevel->pAinc;
122527 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
122529 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
122531 testcase( pParse->earlyCleanup );
122532 if( pParse->db->mallocFailed ) return 0;
122533 pInfo->pNext = pToplevel->pAinc;
122534 pToplevel->pAinc = pInfo;
122535 pInfo->pTab = pTab;
122536 pInfo->iDb = iDb;
122537 pToplevel->nMem++; /* Register to hold name of table */
122538 pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
122539 pToplevel->nMem +=2; /* Rowid in sqlite_sequence + orig max val */
122541 memId = pInfo->regCtr;
122552 sqlite3 *db = pParse->db; /* The database connection */
122555 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
122557 /* This routine is never called during trigger-generation. It is
122558 ** only called from the top-level */
122559 assert( pParse->pTriggerTab==0 );
122563 for(p = pParse->pAinc; p; p = p->pNext){
122580 pDb = &db->aDb[p->iDb];
122581 memId = p->regCtr;
122582 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
122583 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
122584 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
122590 aOp[3].p1 = memId-1;
122599 if( pParse->nTab==0 ) pParse->nTab = 1;
122613 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
122626 Vdbe *v = pParse->pVdbe;
122627 sqlite3 *db = pParse->db;
122630 for(p = pParse->pAinc; p; p = p->pNext){
122640 Db *pDb = &db->aDb[p->iDb];
122642 int memId = p->regCtr;
122645 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
122648 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
122653 aOp[2].p1 = memId-1;
122662 if( pParse->pAinc ) autoIncrementEnd(pParse);
122667 ** above are all no-ops
122691 ** then a list of all (non-hidden) columns for the table is substituted.
122696 ** first two forms shown above. A VALUES clause is really just short-hand
122702 ** insert with data coming from a single-row VALUES clause, the code executes
122703 ** once straight down through. Pseudo-code follows (we call this
122715 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
122739 ** X <- A
122747 ** end-coroutine X
122760 ** X <- A
122768 ** end co-routine R
122797 int ipkColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
122826 db = pParse->db;
122827 if( pParse->nErr || db->mallocFailed ){
122836 if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
122837 pList = pSelect->pEList;
122838 pSelect->pEList = 0;
122845 assert( pTabList->nSrc==1 );
122850 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
122851 assert( iDb<db->nDb );
122852 if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0,
122853 db->aDb[iDb].zDbSName) ){
122863 isView = pTab->pSelect!=0;
122876 ** ViewGetColumnNames() is a no-op if pTab is not a view.
122882 /* Cannot insert into a read-only table.
122892 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
122920 regRowid = regIns = pParse->nMem+1;
122921 pParse->nMem += pTab->nCol + 1;
122924 pParse->nMem++;
122937 ** PRIMARY KEY in the original table is pTab->iPKey.) After this
122938 ** loop, if ipkColumn==(-1), that means that integer primary key
122947 bIdListInOrder = (pTab->tabFlags & (TF_OOOHidden|TF_HasStored))==0;
122949 for(i=0; i<pColumn->nId; i++){
122950 pColumn->a[i].idx = -1;
122952 for(i=0; i<pColumn->nId; i++){
122953 for(j=0; j<pTab->nCol; j++){
122954 if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
122955 pColumn->a[i].idx = j;
122957 if( j==pTab->iPKey ){
122961 if( pTab->aCol[j].colFlags & (COLFLAG_STORED|COLFLAG_VIRTUAL) ){
122964 pTab->aCol[j].zName);
122971 if( j>=pTab->nCol ){
122972 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
122977 pTabList, 0, pColumn->a[i].zName);
122978 pParse->checkSchema = 1;
122986 ** is coming from a SELECT statement, then generate a co-routine that
122988 ** co-routine is the common header to the 3rd and 4th templates.
122991 /* Data is coming from a SELECT or from a multi-row VALUES clause.
122992 ** Generate a co-routine to run the SELECT. */
122993 int regYield; /* Register holding co-routine entry-point */
122994 int addrTop; /* Top of the co-routine */
122997 regYield = ++pParse->nMem;
123002 dest.nSdst = pTab->nCol;
123005 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
123007 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
123008 assert( pSelect->pEList );
123009 nColumn = pSelect->pEList->nExpr;
123039 srcTab = pParse->nTab++;
123054 ** single-row VALUES clause
123059 srcTab = -1;
123062 nColumn = pList->nExpr;
123076 ipkColumn = pTab->iPKey;
123078 if( ipkColumn>=0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
123079 testcase( pTab->tabFlags & TF_HasVirtual );
123080 testcase( pTab->tabFlags & TF_HasStored );
123081 for(i=ipkColumn-1; i>=0; i--){
123082 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
123083 testcase( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL );
123084 testcase( pTab->aCol[i].colFlags & COLFLAG_STORED );
123085 ipkColumn--;
123097 if( (pTab->tabFlags & (TF_HasGenerated|TF_HasHidden))!=0 ){
123098 for(i=0; i<pTab->nCol; i++){
123099 if( pTab->aCol[i].colFlags & COLFLAG_NOINSERT ) nHidden++;
123102 if( nColumn!=(pTab->nCol-nHidden) ){
123105 pTabList, 0, pTab->nCol-nHidden, nColumn);
123109 if( pColumn!=0 && nColumn!=pColumn->nId ){
123110 sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
123116 if( (db->flags & SQLITE_CountRows)!=0
123117 && !pParse->nested
123118 && !pParse->pTriggerTab
123119 && !pParse->bReturning
123121 regRowCount = ++pParse->nMem;
123128 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
123134 for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
123136 aRegIdx[i] = ++pParse->nMem;
123137 pParse->nMem += pIdx->nColumn;
123139 aRegIdx[i] = ++pParse->nMem; /* Register to store the table record */
123146 pTab->zName);
123149 if( pTab->pSelect ){
123153 if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
123156 pTabList->a[0].iCursor = iDataCur;
123159 pNx->pUpsertSrc = pTabList;
123160 pNx->regData = regData;
123161 pNx->iDataCur = iDataCur;
123162 pNx->iIdxCur = iIdxCur;
123163 if( pNx->pUpsertTarget ){
123168 pNx = pNx->pNextUpsert;
123196 sqlite3VdbeReleaseRegisters(pParse, regData, pTab->nCol, 0, 0);
123200 /* tag-20191021-001: If the INTEGER PRIMARY KEY is being generated by the
123202 ** the value does not get overwritten by a NULL at tag-20191021-002. */
123216 for(i=0; i<pTab->nCol; i++, iRegStore++){
123220 if( i==pTab->iPKey ){
123221 /* tag-20191021-002: References to the INTEGER PRIMARY KEY are filled
123224 ** NULL - we cannot optimize further by skipping the column completely */
123228 if( ((colFlags = pTab->aCol[i].colFlags) & COLFLAG_NOINSERT)!=0 ){
123234 iRegStore--;
123238 ** triggers, the slots used for stored columns will be OP_Copy-ed
123248 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
123253 for(j=0; j<pColumn->nId && pColumn->a[j].idx!=i; j++){}
123254 if( j>=pColumn->nId ){
123257 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
123263 sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
123266 k = i - nHidden;
123276 sqlite3ExprCode(pParse, pList->a[k].pExpr, iRegStore);
123285 int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
123291 ** not happened yet) so we substitute a rowid of -1
123294 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
123302 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
123305 sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
123311 assert( pTab->nNVCol>0 );
123312 sqlite3VdbeAddOp3(v, OP_Copy, regRowid+1, regCols+1, pTab->nNVCol-1);
123319 if( pTab->tabFlags & TF_HasGenerated ){
123320 testcase( pTab->tabFlags & TF_HasVirtual );
123321 testcase( pTab->tabFlags & TF_HasStored );
123337 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
123339 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
123352 /* Rowid already initialized at tag-20191021-001 */
123354 Expr *pIpk = pList->a[ipkColumn].pExpr;
123355 if( pIpk->op==TK_NULL && !IsVirtual(pTab) ){
123359 sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
123390 if( pTab->tabFlags & TF_HasGenerated ){
123402 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
123439 pTab, regData-2-pTab->nCol, onError, endOfLoop);
123456 if( sqlite3VdbeGetOp(v, addrCont-1)->opcode==OP_ReleaseReg ){
123457 assert( sqlite3VdbeGetOp(v, addrCont)->opcode==OP_Yield );
123471 if( pParse->nested==0 && pParse->pTriggerTab==0 ){
123509 ** Meanings of bits in of pWalker->eCode for
123516 * Set bit 0x01 of pWalker->eCode if pWalker->eCode to 0 and if this
123521 if( pExpr->op==TK_COLUMN ){
123522 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
123523 if( pExpr->iColumn>=0 ){
123524 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
123525 pWalker->eCode |= CKCNSTRNT_COLUMN;
123528 pWalker->eCode |= CKCNSTRNT_ROWID;
123535 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
123544 ** 2018-09-15: pExpr might also be an expression for an index-on-expressions.
123545 ** The operation of this routine is the same - return true if an only if
123573 ** the indexes of a table in the order provided in the Table->pIndex list.
123574 ** However, sometimes (rarely - when there is an upsert) it wants to visit
123608 assert( pIter->i==0 );
123609 if( pIter->eType ){
123610 *pIx = pIter->u.ax.aIdx[0].ix;
123611 return pIter->u.ax.aIdx[0].p;
123614 return pIter->u.lx.pIdx;
123620 if( pIter->eType ){
123621 int i = ++pIter->i;
123622 if( i>=pIter->u.ax.nIdx ){
123626 *pIx = pIter->u.ax.aIdx[i].ix;
123627 return pIter->u.ax.aIdx[i].p;
123630 pIter->u.lx.pIdx = pIter->u.lx.pIdx->pNext;
123631 return pIter->u.lx.pIdx;
123641 ** pTab->nCol+1 registers in this range. The first register (the one
123668 ** at pTab->pIndex.
123670 ** (2019-05-07) The generated code also creates a new record for the
123672 ** register identified by aRegIdx[nIdx] - in other words in the first
123683 ** for the first index in the pTab->pIndex list. Cursors for other indices
123684 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
123692 ** --------------- ---------- ----------------------------------------
123722 ** Or if overrideError==OE_Default, then the pParse->onError parameter
123723 ** is used. Or if pParse->onError==OE_Default then the onError value
123734 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
123768 db = pParse->db;
123769 v = pParse->pVdbe;
123771 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
123772 nCol = pTab->nCol;
123783 nPkField = pPk->nKeyCol;
123792 if( pTab->tabFlags & TF_HasNotNull ){
123799 Column *pCol = &pTab->aCol[i]; /* The column to check for NOT NULL */
123800 int isGenerated; /* non-zero if column is generated */
123801 onError = pCol->notNull;
123803 if( i==pTab->iPKey ){
123806 isGenerated = pCol->colFlags & COLFLAG_GENERATED;
123822 || pCol->pDflt==0 /* REPLACE is ABORT if no DEFAULT value */
123824 testcase( pCol->colFlags & COLFLAG_VIRTUAL );
123825 testcase( pCol->colFlags & COLFLAG_STORED );
123826 testcase( pCol->colFlags & COLFLAG_GENERATED );
123842 assert( (pCol->colFlags & COLFLAG_GENERATED)==0 );
123844 sqlite3ExprCodeCopy(pParse, pCol->pDflt, iReg);
123853 char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
123854 pCol->zName);
123879 if( nSeenReplace>0 && (pTab->tabFlags & TF_HasGenerated)!=0 ){
123887 } /* end of 2-pass loop */
123888 } /* end if( has-not-null-constraints ) */
123893 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
123894 ExprList *pCheck = pTab->pCheck;
123895 pParse->iSelfTab = -(regNewData+1);
123897 for(i=0; i<pCheck->nExpr; i++){
123900 Expr *pExpr = pCheck->a[i].pExpr;
123915 if( !db->mallocFailed ){
123922 char *zName = pCheck->a[i].zEName;
123923 assert( zName!=0 || pParse->db->mallocFailed );
123924 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-26383-51744 */
123931 pParse->iSelfTab = 0;
123948 ** 2018-08-14: Ticket https://www.sqlite.org/src/info/908f001483982c43
123966 sIdxIter.u.lx.pIdx = pTab->pIndex;
123968 if( pUpsert->pUpsertTarget==0 ){
123969 /* There is just on ON CONFLICT clause and it has no constraint-target */
123970 assert( pUpsert->pNextUpsert==0 );
123971 if( pUpsert->isDoUpdate==0 ){
123972 /* A single ON CONFLICT DO NOTHING clause, without a constraint-target.
123980 }else if( pTab->pIndex!=0 ){
123988 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
123997 pUpsert->pToFree = sIdxIter.u.ax.aIdx;
123998 for(i=0, pTerm=pUpsert; pTerm; pTerm=pTerm->pNextUpsert){
123999 if( pTerm->pUpsertTarget==0 ) break;
124000 if( pTerm->pUpsertIdx==0 ) continue; /* Skip ON CONFLICT for the IPK */
124002 pIdx = pTab->pIndex;
124003 while( ALWAYS(pIdx!=0) && pIdx!=pTerm->pUpsertIdx ){
124004 pIdx = pIdx->pNext;
124013 for(jj=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, jj++){
124043 if( (db->flags & (SQLITE_RecTriggers|SQLITE_ForeignKeys))==0 ){
124049 if( db->flags&SQLITE_RecTriggers ){
124059 regTrigCnt = ++pParse->nMem;
124074 onError = pTab->keyConf;
124085 if( pUpsertClause->isDoUpdate==0 ){
124106 && pTab->pIndex /* There exist other constraints */
124144 ** recursive-triggers flag is set, call GenerateRowDelete() to
124146 ** the triggers and remove both the table and index b-tree entries.
124148 ** Otherwise, if there are no triggers or the recursive-triggers
124150 ** GenerateRowIndexDelete(). This removes the index b-tree entries
124151 ** only. The table b-tree entry will be replaced by the new entry
124168 regNewData, 1, 0, OE_Replace, 1, -1);
124174 /* This OP_Delete opcode fires the pre-update-hook only. It does
124175 ** not modify the b-tree. It is more efficient to let the coming
124181 if( pTab->pIndex ){
124183 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
124206 sqlite3VdbeJumpHere(v, ipkTop-1);
124239 VdbeNoopComment((v, "prep index %s", pIdx->zName));
124244 if( pIdx->pPartIdxWhere ){
124246 pParse->iSelfTab = -(regNewData+1);
124247 sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
124249 pParse->iSelfTab = 0;
124256 for(i=0; i<pIdx->nColumn; i++){
124257 int iField = pIdx->aiColumn[i];
124260 pParse->iSelfTab = -(regNewData+1);
124261 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[i].pExpr, regIdx+i);
124262 pParse->iSelfTab = 0;
124263 VdbeComment((v, "%s column %d", pIdx->zName, i));
124264 }else if( iField==XN_ROWID || iField==pTab->iPKey ){
124272 VdbeComment((v, "%s", pTab->aCol[iField].zName));
124275 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
124276 VdbeComment((v, "for %s", pIdx->zName));
124278 if( pIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
124279 sqlite3SetMakeRecordP5(v, pIdx->pTable);
124282 sqlite3VdbeReleaseRegisters(pParse, regIdx, pIdx->nColumn, 0, 0);
124294 onError = pIdx->onError;
124307 if( pUpsertClause->isDoUpdate==0 ){
124322 ** must be explicitly deleted in order to ensure any pre-update hook
124325 if( (ix==0 && pIdx->pNext==0) /* Condition 3 */
124328 && ( 0==(db->flags&SQLITE_RecTriggers) || /* Condition 4 */
124330 && ( 0==(db->flags&SQLITE_ForeignKeys) || /* Condition 5 */
124331 (0==pTab->pFKey && 0==sqlite3FkReferences(pTab)))
124342 regIdx, pIdx->nKeyCol); VdbeCoverage(v);
124350 ** is different from old-rowid */
124359 ** store it in registers regR..regR+nPk-1 */
124361 for(i=0; i<pPk->nKeyCol; i++){
124362 assert( pPk->aiColumn[i]>=0 );
124363 x = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
124365 VdbeComment((v, "%s.%s", pTab->zName,
124366 pTab->aCol[pPk->aiColumn[i]].zName));
124377 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
124381 for(i=0; i<pPk->nKeyCol; i++){
124382 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
124383 x = pPk->aiColumn[i];
124385 if( i==(pPk->nKeyCol-1) ){
124429 nConflictCk = sqlite3VdbeCurrentAddr(v) - addrConflictCk;
124457 if( pIdx->pPartIdxWhere ){
124460 sqlite3VdbeAddOp2(v, OP_IsNull, regIdx-1, lblRecheckOk);
124464 ** the constraint-ok jump destination to be the address of
124485 nConflictCk--;
124539 sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nNVCol, regRec);
124553 ** to be the number of columns in table pTab that must not be NULL-trimmed.
124555 ** Or if no columns of pTab may be NULL-trimmed, leave P5 at zero.
124561 ** version 2 and later (SQLite version 3.1.4, 2005-02-20). */
124562 if( pTab->pSchema->file_format<2 ) return;
124564 for(i=pTab->nCol-1; i>0; i--){
124565 if( pTab->aCol[i].pDflt!=0 ) break;
124566 if( pTab->aCol[i].colFlags & COLFLAG_PRIMKEY ) break;
124575 ** PK index. This function adds code to invoke the pre-update hook,
124585 Vdbe *v = pParse->pVdbe;
124588 assert( 0==(pParse->db->mDbFlags & DBFLAG_Vacuum) || CORRUPT_DB );
124628 v = pParse->pVdbe;
124630 assert( pTab->pSelect==0 ); /* This table is not a VIEW */
124631 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
124633 assert( pIdx->onError!=OE_Replace
124634 || pIdx->pNext==0
124635 || pIdx->pNext->onError==OE_Replace );
124637 if( pIdx->pPartIdxWhere ){
124643 assert( pParse->nested==0 );
124652 pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
124656 if( pParse->nested ){
124669 if( !pParse->nested ){
124685 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
124691 ** pTab->pIndex list.
124693 ** If pTab is a virtual table, then this routine is a no-op and the
124715 /* This routine is a no-op for virtual tables. Leave the output
124720 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
124721 v = pParse->pVdbe;
124723 if( iBase<0 ) iBase = pParse->nTab;
124729 sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
124732 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
124734 assert( pIdx->pSchema==pTab->pSchema );
124740 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
124743 VdbeComment((v, "%s", pIdx->zName));
124746 if( iBase>pParse->nTab ) pParse->nTab = iBase;
124755 ** purposes only - to make sure the transfer optimization really
124777 assert( pDest->pTable!=pSrc->pTable );
124778 if( pDest->nKeyCol!=pSrc->nKeyCol || pDest->nColumn!=pSrc->nColumn ){
124781 if( pDest->onError!=pSrc->onError ){
124784 for(i=0; i<pSrc->nKeyCol; i++){
124785 if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
124788 if( pSrc->aiColumn[i]==XN_EXPR ){
124789 assert( pSrc->aColExpr!=0 && pDest->aColExpr!=0 );
124790 if( sqlite3ExprCompare(0, pSrc->aColExpr->a[i].pExpr,
124791 pDest->aColExpr->a[i].pExpr, -1)!=0 ){
124795 if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
124798 if( sqlite3_stricmp(pSrc->azColl[i],pDest->azColl[i])!=0 ){
124802 if( sqlite3ExprCompare(0, pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
124820 ** There are lots of rules for determining compatibility - see comments
124825 ** is empty - a factor that can only be determined at run-time. In that
124842 sqlite3 *db = pParse->db;
124846 SrcItem *pItem; /* An element of pSelect->pSrc */
124861 if( pParse->pWith || pSelect->pWith ){
124876 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
124879 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
124880 if( pSelect->pSrc->nSrc!=1 ){
124883 if( pSelect->pSrc->a[0].pSelect ){
124886 if( pSelect->pWhere ){
124889 if( pSelect->pOrderBy ){
124894 if( pSelect->pGroupBy ){
124897 if( pSelect->pLimit ){
124900 if( pSelect->pPrior ){
124903 if( pSelect->selFlags & SF_Distinct ){
124906 pEList = pSelect->pEList;
124908 if( pEList->nExpr!=1 ){
124911 assert( pEList->a[0].pExpr );
124912 if( pEList->a[0].pExpr->op!=TK_ASTERISK ){
124920 pItem = pSelect->pSrc->a;
124925 if( pSrc->tnum==pDest->tnum && pSrc->pSchema==pDest->pSchema ){
124937 if( pSrc->pSelect ){
124940 if( pDest->nCol!=pSrc->nCol ){
124943 if( pDest->iPKey!=pSrc->iPKey ){
124946 for(i=0; i<pDest->nCol; i++){
124947 Column *pDestCol = &pDest->aCol[i];
124948 Column *pSrcCol = &pSrc->aCol[i];
124950 if( (db->mDbFlags & DBFLAG_Vacuum)==0
124951 && (pDestCol->colFlags | pSrcCol->colFlags) & COLFLAG_HIDDEN
124973 if( (pDestCol->colFlags & COLFLAG_GENERATED) !=
124974 (pSrcCol->colFlags & COLFLAG_GENERATED) ){
124975 return 0; /* Both columns have the same generated-column type */
124981 if( (pDestCol->colFlags & COLFLAG_GENERATED)!=0 ){
124982 if( sqlite3ExprCompare(0, pSrcCol->pDflt, pDestCol->pDflt, -1)!=0 ){
124983 testcase( pDestCol->colFlags & COLFLAG_VIRTUAL );
124984 testcase( pDestCol->colFlags & COLFLAG_STORED );
124989 if( pDestCol->affinity!=pSrcCol->affinity ){
124992 if( sqlite3_stricmp(pDestCol->zColl, pSrcCol->zColl)!=0 ){
124995 if( pDestCol->notNull && !pSrcCol->notNull ){
124999 if( (pDestCol->colFlags & COLFLAG_GENERATED)==0 && i>0 ){
125000 assert( pDestCol->pDflt==0 || pDestCol->pDflt->op==TK_SPAN );
125001 assert( pSrcCol->pDflt==0 || pSrcCol->pDflt->op==TK_SPAN );
125002 if( (pDestCol->pDflt==0)!=(pSrcCol->pDflt==0)
125003 || (pDestCol->pDflt && strcmp(pDestCol->pDflt->u.zToken,
125004 pSrcCol->pDflt->u.zToken)!=0)
125010 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
125014 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
125020 if( pSrcIdx->tnum==pDestIdx->tnum && pSrc->pSchema==pDest->pSchema
125025 return 0; /* Corrupt schema - two indexes on the same btree */
125029 if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
125041 if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
125045 if( (db->flags & SQLITE_CountRows)!=0 ){
125056 iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema);
125059 iSrc = pParse->nTab++;
125060 iDest = pParse->nTab++;
125067 if( (db->mDbFlags & DBFLAG_Vacuum)==0 && (
125068 (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
125097 if( pDest->iPKey>=0 ){
125099 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
125107 }else if( pDest->pIndex==0 && !(db->mDbFlags & DBFLAG_VacuumInto) ){
125111 assert( (pDest->tabFlags & TF_Autoincrement)==0 );
125114 if( db->mDbFlags & DBFLAG_Vacuum ){
125121 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
125130 if( (db->mDbFlags & DBFLAG_Vacuum)==0 ){
125131 sqlite3VdbeChangeP4(v, -1, (char*)pDest, P4_TABLE);
125139 sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
125140 sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
125142 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
125144 for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
125148 sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
125150 VdbeComment((v, "%s", pSrcIdx->zName));
125151 sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
125154 VdbeComment((v, "%s", pDestIdx->zName));
125156 if( db->mDbFlags & DBFLAG_Vacuum ){
125161 ** order. In this case, instead of seeking within the b-tree as part
125163 ** OP_IdxInsert to seek to the point within the b-tree where each key
125171 for(i=0; i<pSrcIdx->nColumn; i++){
125172 const char *zColl = pSrcIdx->azColl[i];
125175 if( i==pSrcIdx->nColumn ){
125180 }else if( !HasRowid(pSrc) && pDestIdx->idxType==SQLITE_IDXTYPE_PRIMARYKEY ){
125185 if( (db->mDbFlags & DBFLAG_Vacuum)==0
125237 ** codes. Also write an error message into memory obtained from
125238 ** malloc() and make *pzErrMsg point to that message.
125261 sqlite3_mutex_enter(db->mutex);
125268 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
125274 /* this happens for a comment or white-space */
125287 && db->flags&SQLITE_NullCallback)) ){
125314 /* EVIDENCE-OF: R-38229-40159 If the callback function to
125315 ** sqlite3_exec() returns non-zero, then sqlite3_exec() will
125353 assert( (rc&db->errMask)==rc );
125354 sqlite3_mutex_leave(db->mutex);
125736 ** (part of the main SQLite library - not an extension) so that
125743 #define sqlite3_aggregate_context sqlite3_api->aggregate_context
125745 #define sqlite3_aggregate_count sqlite3_api->aggregate_count
125747 #define sqlite3_bind_blob sqlite3_api->bind_blob
125748 #define sqlite3_bind_double sqlite3_api->bind_double
125749 #define sqlite3_bind_int sqlite3_api->bind_int
125750 #define sqlite3_bind_int64 sqlite3_api->bind_int64
125751 #define sqlite3_bind_null sqlite3_api->bind_null
125752 #define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
125753 #define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
125754 #define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
125755 #define sqlite3_bind_text sqlite3_api->bind_text
125756 #define sqlite3_bind_text16 sqlite3_api->bind_text16
125757 #define sqlite3_bind_value sqlite3_api->bind_value
125758 #define sqlite3_busy_handler sqlite3_api->busy_handler
125759 #define sqlite3_busy_timeout sqlite3_api->busy_timeout
125760 #define sqlite3_changes sqlite3_api->changes
125761 #define sqlite3_close sqlite3_api->close
125762 #define sqlite3_collation_needed sqlite3_api->collation_needed
125763 #define sqlite3_collation_needed16 sqlite3_api->collation_needed16
125764 #define sqlite3_column_blob sqlite3_api->column_blob
125765 #define sqlite3_column_bytes sqlite3_api->column_bytes
125766 #define sqlite3_column_bytes16 sqlite3_api->column_bytes16
125767 #define sqlite3_column_count sqlite3_api->column_count
125768 #define sqlite3_column_database_name sqlite3_api->column_database_name
125769 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
125770 #define sqlite3_column_decltype sqlite3_api->column_decltype
125771 #define sqlite3_column_decltype16 sqlite3_api->column_decltype16
125772 #define sqlite3_column_double sqlite3_api->column_double
125773 #define sqlite3_column_int sqlite3_api->column_int
125774 #define sqlite3_column_int64 sqlite3_api->column_int64
125775 #define sqlite3_column_name sqlite3_api->column_name
125776 #define sqlite3_column_name16 sqlite3_api->column_name16
125777 #define sqlite3_column_origin_name sqlite3_api->column_origin_name
125778 #define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
125779 #define sqlite3_column_table_name sqlite3_api->column_table_name
125780 #define sqlite3_column_table_name16 sqlite3_api->column_table_name16
125781 #define sqlite3_column_text sqlite3_api->column_text
125782 #define sqlite3_column_text16 sqlite3_api->column_text16
125783 #define sqlite3_column_type sqlite3_api->column_type
125784 #define sqlite3_column_value sqlite3_api->column_value
125785 #define sqlite3_commit_hook sqlite3_api->commit_hook
125786 #define sqlite3_complete sqlite3_api->complete
125787 #define sqlite3_complete16 sqlite3_api->complete16
125788 #define sqlite3_create_collation sqlite3_api->create_collation
125789 #define sqlite3_create_collation16 sqlite3_api->create_collation16
125790 #define sqlite3_create_function sqlite3_api->create_function
125791 #define sqlite3_create_function16 sqlite3_api->create_function16
125792 #define sqlite3_create_module sqlite3_api->create_module
125793 #define sqlite3_create_module_v2 sqlite3_api->create_module_v2
125794 #define sqlite3_data_count sqlite3_api->data_count
125795 #define sqlite3_db_handle sqlite3_api->db_handle
125796 #define sqlite3_declare_vtab sqlite3_api->declare_vtab
125797 #define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
125798 #define sqlite3_errcode sqlite3_api->errcode
125799 #define sqlite3_errmsg sqlite3_api->errmsg
125800 #define sqlite3_errmsg16 sqlite3_api->errmsg16
125801 #define sqlite3_exec sqlite3_api->exec
125803 #define sqlite3_expired sqlite3_api->expired
125805 #define sqlite3_finalize sqlite3_api->finalize
125806 #define sqlite3_free sqlite3_api->free
125807 #define sqlite3_free_table sqlite3_api->free_table
125808 #define sqlite3_get_autocommit sqlite3_api->get_autocommit
125809 #define sqlite3_get_auxdata sqlite3_api->get_auxdata
125810 #define sqlite3_get_table sqlite3_api->get_table
125812 #define sqlite3_global_recover sqlite3_api->global_recover
125814 #define sqlite3_interrupt sqlite3_api->interruptx
125815 #define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
125816 #define sqlite3_libversion sqlite3_api->libversion
125817 #define sqlite3_libversion_number sqlite3_api->libversion_number
125818 #define sqlite3_malloc sqlite3_api->malloc
125819 #define sqlite3_mprintf sqlite3_api->mprintf
125820 #define sqlite3_open sqlite3_api->open
125821 #define sqlite3_open16 sqlite3_api->open16
125822 #define sqlite3_prepare sqlite3_api->prepare
125823 #define sqlite3_prepare16 sqlite3_api->prepare16
125824 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
125825 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
125826 #define sqlite3_profile sqlite3_api->profile
125827 #define sqlite3_progress_handler sqlite3_api->progress_handler
125828 #define sqlite3_realloc sqlite3_api->realloc
125829 #define sqlite3_reset sqlite3_api->reset
125830 #define sqlite3_result_blob sqlite3_api->result_blob
125831 #define sqlite3_result_double sqlite3_api->result_double
125832 #define sqlite3_result_error sqlite3_api->result_error
125833 #define sqlite3_result_error16 sqlite3_api->result_error16
125834 #define sqlite3_result_int sqlite3_api->result_int
125835 #define sqlite3_result_int64 sqlite3_api->result_int64
125836 #define sqlite3_result_null sqlite3_api->result_null
125837 #define sqlite3_result_text sqlite3_api->result_text
125838 #define sqlite3_result_text16 sqlite3_api->result_text16
125839 #define sqlite3_result_text16be sqlite3_api->result_text16be
125840 #define sqlite3_result_text16le sqlite3_api->result_text16le
125841 #define sqlite3_result_value sqlite3_api->result_value
125842 #define sqlite3_rollback_hook sqlite3_api->rollback_hook
125843 #define sqlite3_set_authorizer sqlite3_api->set_authorizer
125844 #define sqlite3_set_auxdata sqlite3_api->set_auxdata
125845 #define sqlite3_snprintf sqlite3_api->xsnprintf
125846 #define sqlite3_step sqlite3_api->step
125847 #define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
125848 #define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
125849 #define sqlite3_total_changes sqlite3_api->total_changes
125850 #define sqlite3_trace sqlite3_api->trace
125852 #define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
125854 #define sqlite3_update_hook sqlite3_api->update_hook
125855 #define sqlite3_user_data sqlite3_api->user_data
125856 #define sqlite3_value_blob sqlite3_api->value_blob
125857 #define sqlite3_value_bytes sqlite3_api->value_bytes
125858 #define sqlite3_value_bytes16 sqlite3_api->value_bytes16
125859 #define sqlite3_value_double sqlite3_api->value_double
125860 #define sqlite3_value_int sqlite3_api->value_int
125861 #define sqlite3_value_int64 sqlite3_api->value_int64
125862 #define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
125863 #define sqlite3_value_text sqlite3_api->value_text
125864 #define sqlite3_value_text16 sqlite3_api->value_text16
125865 #define sqlite3_value_text16be sqlite3_api->value_text16be
125866 #define sqlite3_value_text16le sqlite3_api->value_text16le
125867 #define sqlite3_value_type sqlite3_api->value_type
125868 #define sqlite3_vmprintf sqlite3_api->vmprintf
125869 #define sqlite3_vsnprintf sqlite3_api->xvsnprintf
125870 #define sqlite3_overload_function sqlite3_api->overload_function
125871 #define sqlite3_prepare_v2 sqlite3_api->prepare_v2
125872 #define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
125873 #define sqlite3_clear_bindings sqlite3_api->clear_bindings
125874 #define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
125875 #define sqlite3_blob_bytes sqlite3_api->blob_bytes
125876 #define sqlite3_blob_close sqlite3_api->blob_close
125877 #define sqlite3_blob_open sqlite3_api->blob_open
125878 #define sqlite3_blob_read sqlite3_api->blob_read
125879 #define sqlite3_blob_write sqlite3_api->blob_write
125880 #define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
125881 #define sqlite3_file_control sqlite3_api->file_control
125882 #define sqlite3_memory_highwater sqlite3_api->memory_highwater
125883 #define sqlite3_memory_used sqlite3_api->memory_used
125884 #define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
125885 #define sqlite3_mutex_enter sqlite3_api->mutex_enter
125886 #define sqlite3_mutex_free sqlite3_api->mutex_free
125887 #define sqlite3_mutex_leave sqlite3_api->mutex_leave
125888 #define sqlite3_mutex_try sqlite3_api->mutex_try
125889 #define sqlite3_open_v2 sqlite3_api->open_v2
125890 #define sqlite3_release_memory sqlite3_api->release_memory
125891 #define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
125892 #define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
125893 #define sqlite3_sleep sqlite3_api->sleep
125894 #define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
125895 #define sqlite3_vfs_find sqlite3_api->vfs_find
125896 #define sqlite3_vfs_register sqlite3_api->vfs_register
125897 #define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
125898 #define sqlite3_threadsafe sqlite3_api->xthreadsafe
125899 #define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
125900 #define sqlite3_result_error_code sqlite3_api->result_error_code
125901 #define sqlite3_test_control sqlite3_api->test_control
125902 #define sqlite3_randomness sqlite3_api->randomness
125903 #define sqlite3_context_db_handle sqlite3_api->context_db_handle
125904 #define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
125905 #define sqlite3_limit sqlite3_api->limit
125906 #define sqlite3_next_stmt sqlite3_api->next_stmt
125907 #define sqlite3_sql sqlite3_api->sql
125908 #define sqlite3_status sqlite3_api->status
125909 #define sqlite3_backup_finish sqlite3_api->backup_finish
125910 #define sqlite3_backup_init sqlite3_api->backup_init
125911 #define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
125912 #define sqlite3_backup_remaining sqlite3_api->backup_remaining
125913 #define sqlite3_backup_step sqlite3_api->backup_step
125914 #define sqlite3_compileoption_get sqlite3_api->compileoption_get
125915 #define sqlite3_compileoption_used sqlite3_api->compileoption_used
125916 #define sqlite3_create_function_v2 sqlite3_api->create_function_v2
125917 #define sqlite3_db_config sqlite3_api->db_config
125918 #define sqlite3_db_mutex sqlite3_api->db_mutex
125919 #define sqlite3_db_status sqlite3_api->db_status
125920 #define sqlite3_extended_errcode sqlite3_api->extended_errcode
125921 #define sqlite3_log sqlite3_api->log
125922 #define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
125923 #define sqlite3_sourceid sqlite3_api->sourceid
125924 #define sqlite3_stmt_status sqlite3_api->stmt_status
125925 #define sqlite3_strnicmp sqlite3_api->strnicmp
125926 #define sqlite3_unlock_notify sqlite3_api->unlock_notify
125927 #define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
125928 #define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
125929 #define sqlite3_wal_hook sqlite3_api->wal_hook
125930 #define sqlite3_blob_reopen sqlite3_api->blob_reopen
125931 #define sqlite3_vtab_config sqlite3_api->vtab_config
125932 #define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
125934 #define sqlite3_close_v2 sqlite3_api->close_v2
125935 #define sqlite3_db_filename sqlite3_api->db_filename
125936 #define sqlite3_db_readonly sqlite3_api->db_readonly
125937 #define sqlite3_db_release_memory sqlite3_api->db_release_memory
125938 #define sqlite3_errstr sqlite3_api->errstr
125939 #define sqlite3_stmt_busy sqlite3_api->stmt_busy
125940 #define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
125941 #define sqlite3_stricmp sqlite3_api->stricmp
125942 #define sqlite3_uri_boolean sqlite3_api->uri_boolean
125943 #define sqlite3_uri_int64 sqlite3_api->uri_int64
125944 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
125945 #define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
125946 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
125948 #define sqlite3_auto_extension sqlite3_api->auto_extension
125949 #define sqlite3_bind_blob64 sqlite3_api->bind_blob64
125950 #define sqlite3_bind_text64 sqlite3_api->bind_text64
125951 #define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
125952 #define sqlite3_load_extension sqlite3_api->load_extension
125953 #define sqlite3_malloc64 sqlite3_api->malloc64
125954 #define sqlite3_msize sqlite3_api->msize
125955 #define sqlite3_realloc64 sqlite3_api->realloc64
125956 #define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
125957 #define sqlite3_result_blob64 sqlite3_api->result_blob64
125958 #define sqlite3_result_text64 sqlite3_api->result_text64
125959 #define sqlite3_strglob sqlite3_api->strglob
125961 #define sqlite3_value_dup sqlite3_api->value_dup
125962 #define sqlite3_value_free sqlite3_api->value_free
125963 #define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
125964 #define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
125966 #define sqlite3_value_subtype sqlite3_api->value_subtype
125967 #define sqlite3_result_subtype sqlite3_api->result_subtype
125969 #define sqlite3_status64 sqlite3_api->status64
125970 #define sqlite3_strlike sqlite3_api->strlike
125971 #define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
125973 #define sqlite3_system_errno sqlite3_api->system_errno
125975 #define sqlite3_trace_v2 sqlite3_api->trace_v2
125976 #define sqlite3_expanded_sql sqlite3_api->expanded_sql
125978 #define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
125980 #define sqlite3_prepare_v3 sqlite3_api->prepare_v3
125981 #define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
125982 #define sqlite3_bind_pointer sqlite3_api->bind_pointer
125983 #define sqlite3_result_pointer sqlite3_api->result_pointer
125984 #define sqlite3_value_pointer sqlite3_api->value_pointer
125986 #define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
125987 #define sqlite3_value_nochange sqlite3_api->value_nochange
125988 #define sqlite3_vtab_collation sqlite3_api->vtab_collation
125990 #define sqlite3_keyword_count sqlite3_api->keyword_count
125991 #define sqlite3_keyword_name sqlite3_api->keyword_name
125992 #define sqlite3_keyword_check sqlite3_api->keyword_check
125993 #define sqlite3_str_new sqlite3_api->str_new
125994 #define sqlite3_str_finish sqlite3_api->str_finish
125995 #define sqlite3_str_appendf sqlite3_api->str_appendf
125996 #define sqlite3_str_vappendf sqlite3_api->str_vappendf
125997 #define sqlite3_str_append sqlite3_api->str_append
125998 #define sqlite3_str_appendall sqlite3_api->str_appendall
125999 #define sqlite3_str_appendchar sqlite3_api->str_appendchar
126000 #define sqlite3_str_reset sqlite3_api->str_reset
126001 #define sqlite3_str_errcode sqlite3_api->str_errcode
126002 #define sqlite3_str_length sqlite3_api->str_length
126003 #define sqlite3_str_value sqlite3_api->str_value
126005 #define sqlite3_create_window_function sqlite3_api->create_window_function
126007 #define sqlite3_normalized_sql sqlite3_api->normalized_sql
126009 #define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
126010 #define sqlite3_value_frombind sqlite3_api->value_frombind
126012 #define sqlite3_drop_modules sqlite3_api->drop_modules
126014 #define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
126015 #define sqlite3_uri_key sqlite3_api->uri_key
126016 #define sqlite3_filename_database sqlite3_api->filename_database
126017 #define sqlite3_filename_journal sqlite3_api->filename_journal
126018 #define sqlite3_filename_wal sqlite3_api->filename_wal
126020 #define sqlite3_create_filename sqlite3_api->create_filename
126021 #define sqlite3_free_filename sqlite3_api->free_filename
126022 #define sqlite3_database_file_object sqlite3_api->database_file_object
126024 #define sqlite3_txn_state sqlite3_api->txn_state
126037 # define SQLITE_EXTENSION_INIT1 /*no-op*/
126039 # define SQLITE_EXTENSION_INIT3 /*no-op*/
126528 ** error message text. The calling function should free this memory
126535 char **pzErrMsg /* Put error message here if not 0 */
126537 sqlite3_vfs *pVfs = db->pVfs;
126569 if( (db->flags & SQLITE_LoadExtension)==0 ){
126593 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
126620 for(iFile=ncFile-1; iFile>=0 && !DirSep(zFile[iFile]); iFile--){}
126639 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
126658 /* Append the new shared library handle to the db->aExtension array. */
126659 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
126663 if( db->nExtension>0 ){
126664 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
126666 sqlite3DbFree(db, db->aExtension);
126667 db->aExtension = aHandle;
126669 db->aExtension[db->nExtension++] = handle;
126676 char **pzErrMsg /* Put error message here if not 0 */
126679 sqlite3_mutex_enter(db->mutex);
126682 sqlite3_mutex_leave(db->mutex);
126692 assert( sqlite3_mutex_held(db->mutex) );
126693 for(i=0; i<db->nExtension; i++){
126694 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
126696 sqlite3DbFree(db, db->aExtension);
126704 sqlite3_mutex_enter(db->mutex);
126706 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
126708 db->flags &= ~(u64)(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
126710 sqlite3_mutex_leave(db->mutex);
126731 ** we have to locate the state vector at run-time. In the more common
126791 ** routine is a no-op.
126806 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
126808 wsdAutoext.nExt--;
126973 #define PragFlg_ReadOnly 0x08 /* Read-only HEADER_VALUE */
126977 #define PragFlg_SchemaReq 0x80 /* Schema required - "main" is default */
126979 /* Names of columns for pragmas that return multi-column result
126980 ** or that return single-column results where the name of the
127041 /* Definitions of all built-in pragmas */
127627 ** Interpret the given string as an auto-vacuum mode value.
127645 ** backed temporary databases, 2 for the Red-Black tree in memory database
127646 ** and 0 to use the compile-time default.
127650 return z[0] - '0';
127667 sqlite3 *db = pParse->db;
127668 if( db->aDb[1].pBt!=0 ){
127669 if( !db->autoCommit
127670 || sqlite3BtreeTxnState(db->aDb[1].pBt)!=SQLITE_TXN_NONE
127676 sqlite3BtreeClose(db->aDb[1].pBt);
127677 db->aDb[1].pBt = 0;
127692 sqlite3 *db = pParse->db;
127693 if( db->temp_store==ts ) return SQLITE_OK;
127697 db->temp_store = (u8)ts;
127709 u8 n = pPragma->nPragCName;
127712 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, pPragma->zName, SQLITE_STATIC);
127715 for(i=0, j=pPragma->iPragCName; i<n; i++, j++){
127749 if( db->autoCommit ){
127750 Db *pDb = db->aDb;
127751 int n = db->nDb;
127757 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
127758 while( (n--) > 0 ){
127759 if( pDb->pBt ){
127760 sqlite3BtreeSetPagerFlags(pDb->pBt,
127761 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
127768 # define setAllPagerFlags(X) /* no-op */
127773 ** Return a human-readable name for a constraint resolution action.
127794 ** journal-mode name.
127821 upr = ArraySize(aPragmaName)-1;
127827 upr = mid - 1;
127842 int isBuiltin, /* True if this is a built-in function */
127845 for(; p; p=p->pNext){
127861 if( p->xSFunc==0 ) continue;
127862 if( (p->funcFlags & SQLITE_FUNC_INTERNAL)!=0
127867 if( p->xValue!=0 ){
127869 }else if( p->xFinalize!=0 ){
127875 p->zName, isBuiltin,
127876 zType, azEnc[p->funcFlags&SQLITE_FUNC_ENCMASK],
127877 p->nArg,
127878 (p->funcFlags & mask) ^ SQLITE_INNOCUOUS
127887 ** Generate code to output a single-column result row with a value of the
127920 int minusFlag /* True if a '-' sign preceded <value> */
127922 char *zLeft = 0; /* Nul-terminated UTF-8 string <id> */
127923 char *zRight = 0; /* Nul-terminated UTF-8 string <value>, or NULL */
127929 sqlite3 *db = pParse->db; /* The database connection */
127936 pParse->nMem = 2;
127942 pDb = &db->aDb[iDb];
127954 zRight = sqlite3MPrintf(db, "-%T", pValue);
127960 zDb = pId2->n>0 ? pDb->zDbSName : 0;
127965 /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
127967 ** handled the pragma and generate a no-op prepared statement.
127969 ** IMPLEMENTATION-OF: R-12238-55120 Whenever a PRAGMA statement is parsed,
127974 ** IMPLEMENTATION-OF: R-29875-31678 The argument to the SQLITE_FCNTL_PRAGMA
127984 db->busyHandler.nBusy = 0;
127998 pParse->nErr++;
127999 pParse->rc = rc;
128008 if( (pPragma->mPragFlg & PragFlg_NeedSchema)!=0 ){
128013 if( (pPragma->mPragFlg & PragFlg_NoColumns)==0
128014 && ((pPragma->mPragFlg & PragFlg_NoColumns1)==0 || zRight==0)
128020 switch( pPragma->ePragTyp ){
128055 pParse->nMem += 2;
128067 pDb->pSchema->cache_size = size;
128068 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
128085 Btree *pBt = pDb->pBt;
128091 /* Malloc may fail when setting the page-size, as there is an internal
128094 db->nextPagesize = sqlite3Atoi(zRight);
128095 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,0,0) ){
128111 Btree *pBt = pDb->pBt;
128112 int b = -1;
128121 if( pId2->n==0 && b>=0 ){
128123 for(ii=0; ii<db->nDb; ii++){
128124 sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
128153 iReg = ++pParse->nMem;
128177 if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
128180 ** the locking-mode of the main database).
128182 eMode = db->dfltLockMode;
128185 if( pId2->n==0 ){
128187 ** of the PRAGMA command. In this case the locking-mode must be
128195 assert(pDb==&db->aDb[0]);
128196 for(ii=2; ii<db->nDb; ii++){
128197 pPager = sqlite3BtreePager(db->aDb[ii].pBt);
128200 db->dfltLockMode = (u8)eMode;
128202 pPager = sqlite3BtreePager(pDb->pBt);
128239 if( eMode==PAGER_JOURNALMODE_OFF && (db->flags & SQLITE_Defensive)!=0 ){
128240 /* Do not allow journal-mode "OFF" in defensive since the database
128245 if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
128248 pId2->n = 1;
128250 for(ii=db->nDb-1; ii>=0; ii--){
128251 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
128267 Pager *pPager = sqlite3BtreePager(pDb->pBt);
128268 i64 iLimit = -2;
128271 if( iLimit<-1 ) iLimit = -1;
128284 ** Get or set the value of the database 'auto-vacuum' parameter.
128289 Btree *pBt = pDb->pBt;
128296 db->nextAutovac = (u8)eAuto;
128298 ** incr-vacuum flags. This is required in case this connection
128300 ** as an auto-vacuum capable db.
128307 ** that this really is an auto-vacuum capable database.
128326 aOp[4].p3 = eAuto - 1;
128349 sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
128365 ** number of pages is adjusted so that the cache uses -N kibibytes
128371 returnSingleInt(v, pDb->pSchema->cache_size);
128374 pDb->pSchema->cache_size = size;
128375 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
128392 ** number of pages is adjusted so that the cache uses -N kibibytes
128406 (db->flags & SQLITE_CacheSpill)==0 ? 0 :
128407 sqlite3BtreeSetSpillSize(pDb->pBt,0));
128411 sqlite3BtreeSetSpillSize(pDb->pBt, size);
128414 db->flags |= SQLITE_CacheSpill;
128416 db->flags &= ~(u64)SQLITE_CacheSpill;
128445 if( pId2->n==0 ) db->szMmap = sz;
128446 for(ii=db->nDb-1; ii>=0; ii--){
128447 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
128448 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
128452 sz = -1;
128461 pParse->nErr++;
128462 pParse->rc = rc;
128475 ** Note that it is possible for the library compile-time options to
128480 returnSingleInt(v, db->temp_store);
128504 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
128511 || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
128512 || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
128548 rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
128577 Pager *pPager = sqlite3BtreePager(pDb->pBt);
128584 Pager *pPager = sqlite3BtreePager(pDb->pBt);
128614 returnSingleInt(v, pDb->safety_level-1);
128616 if( !db->autoCommit ){
128622 pDb->safety_level = iLevel;
128623 pDb->bSyncSet = 1;
128635 returnSingleInt(v, (db->flags & pPragma->iArg)!=0 );
128637 u64 mask = pPragma->iArg; /* Mask of bits to set or clear. */
128638 if( db->autoCommit==0 ){
128640 ** in auto-commit mode. */
128644 if( db->auth.authLevel==UAUTH_User ){
128645 /* Do not allow non-admin users to modify the schema arbitrarily */
128651 db->flags |= mask;
128653 db->flags &= ~mask;
128654 if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
128657 /* Many of the flag-pragmas modify the code generated by the SQL
128680 ** pk: Non-zero for PK fields.
128691 pParse->nMem = 7;
128693 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
128695 if( pCol->colFlags & COLFLAG_NOINSERT ){
128696 if( pPragma->iArg==0 ){
128700 if( pCol->colFlags & COLFLAG_VIRTUAL ){
128702 }else if( pCol->colFlags & COLFLAG_STORED ){
128704 }else{ assert( pCol->colFlags & COLFLAG_HIDDEN );
128708 if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
128713 for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){}
128715 assert( pCol->pDflt==0 || pCol->pDflt->op==TK_SPAN || isHidden>=2 );
128716 sqlite3VdbeMultiLoad(v, 1, pPragma->iArg ? "issisii" : "issisi",
128717 i-nHidden,
128718 pCol->zName,
128720 pCol->notNull ? 1 : 0,
128721 pCol->pDflt && isHidden<2 ? pCol->pDflt->u.zToken : 0,
128733 pParse->nMem = 5;
128735 for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
128738 pTab->zName,
128740 pTab->szTabRow,
128741 pTab->nRowLogEst,
128742 pTab->tabFlags);
128743 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
128745 pIdx->zName,
128746 pIdx->szIdxRow,
128747 pIdx->aiRowLogEst[0],
128748 pIdx->hasStat1);
128770 int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
128773 if( pPragma->iArg ){
128775 mx = pIdx->nColumn;
128776 pParse->nMem = 6;
128779 mx = pIdx->nKeyCol;
128780 pParse->nMem = 3;
128782 pTab = pIdx->pTable;
128784 assert( pParse->nMem<=pPragma->nPragCName );
128786 i16 cnum = pIdx->aiColumn[i];
128788 cnum<0 ? 0 : pTab->aCol[cnum].zName);
128789 if( pPragma->iArg ){
128791 pIdx->aSortOrder[i],
128792 pIdx->azColl[i],
128793 i<pIdx->nKeyCol);
128795 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, pParse->nMem);
128807 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128808 pParse->nMem = 5;
128810 for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
128814 pIdx->zName,
128816 azOrigin[pIdx->idxType],
128817 pIdx->pPartIdxWhere!=0);
128825 pParse->nMem = 3;
128826 for(i=0; i<db->nDb; i++){
128827 if( db->aDb[i].pBt==0 ) continue;
128828 assert( db->aDb[i].zDbSName!=0 );
128831 db->aDb[i].zDbSName,
128832 sqlite3BtreeGetFilename(db->aDb[i].pBt));
128840 pParse->nMem = 2;
128841 for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
128843 sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
128853 int showInternFunc = (db->mDbFlags & DBFLAG_InternalFunc)!=0;
128854 pParse->nMem = 6;
128856 for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){
128860 for(j=sqliteHashFirst(&db->aFunc); j; j=sqliteHashNext(j)){
128870 pParse->nMem = 1;
128871 for(j=sqliteHashFirst(&db->aModule); j; j=sqliteHashNext(j)){
128873 sqlite3VdbeMultiLoad(v, 1, "s", pMod->zName);
128896 pFK = pTab->pFKey;
128898 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128900 pParse->nMem = 8;
128904 for(j=0; j<pFK->nCol; j++){
128908 pFK->zTo,
128909 pTab->aCol[pFK->aCol[j].iFrom].zName,
128910 pFK->aCol[j].zCol,
128911 actionName(pFK->aAction[1]), /* ON UPDATE */
128912 actionName(pFK->aAction[0]), /* ON DELETE */
128916 pFK = pFK->pNextFrom;
128942 regResult = pParse->nMem+1;
128943 pParse->nMem += 4;
128944 regKey = ++pParse->nMem;
128945 regRow = ++pParse->nMem;
128946 k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
128955 if( pTab==0 || pTab->pFKey==0 ) continue;
128956 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
128957 zDb = db->aDb[iDb].zDbSName;
128959 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
128960 if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
128962 sqlite3VdbeLoadString(v, regResult, pTab->zName);
128963 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
128964 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
128967 sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
128973 sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
128981 assert( pParse->nErr>0 || pFK==0 );
128983 if( pParse->nTab<i ) pParse->nTab = i;
128985 for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
128986 pParent = sqlite3FindTable(db, pFK->zTo, zDb);
128991 assert( x==0 || db->mallocFailed );
128999 for(j=0; j<pFK->nCol; j++){
129000 int iCol = aiCols ? aiCols[j] : pFK->aCol[j].iFrom;
129008 sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
129009 sqlite3IndexAffinityStr(db,pIdx), pFK->nCol);
129016 assert( pFK->nCol==1 || db->mallocFailed );
129025 sqlite3VdbeMultiLoad(v, regResult+2, "siX", pFK->zTo, i-1);
129064 ** without the overhead of cross-checking indexes. Quick_check
129092 ** to -1 here, to indicate that the VDBE should verify the integrity
129095 assert( iDb==0 || pId2->z );
129096 if( pId2->z==0 ) iDb = -1;
129099 pParse->nMem = 6;
129110 iDb>=0 ? db->aDb[iDb].zDbSName : 0);
129113 sqlite3VdbeAddOp2(v, OP_Integer, mxErr-1, 1); /* reg[1] holds errors left */
129116 for(i=0; i<db->nDb; i++){
129128 /* Do an integrity check of the B-Tree
129134 pTbls = &db->aDb[i].pSchema->tblHash;
129141 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){ cnt++; }
129154 if( HasRowid(pTab) ) aRoot[++cnt] = pTab->tnum;
129155 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
129156 aRoot[++cnt] = pIdx->tnum;
129162 pParse->nMem = MAX( pParse->nMem, 8+mxIdx );
129165 /* Do the b-tree integrity checks */
129170 sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zDbSName),
129184 int r1 = -1;
129186 if( pTab->tnum<1 ) continue; /* Skip VIEWs or VIRTUAL TABLEs */
129192 ** reg[8+i] counts the number of entries in the i-th index
129195 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
129198 assert( pParse->nMem>=8+j );
129204 sqlite3VdbeAddOp3(v, OP_Column, iDataCur, pTab->nNVCol-1,3);
129208 for(j=0; j<pTab->nCol; j++){
129211 if( j==pTab->iPKey ) continue;
129212 if( pTab->aCol[j].notNull==0 ) continue;
129214 if( sqlite3VdbeGetOp(v,-1)->opcode==OP_Column ){
129218 zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
129219 pTab->aCol[j].zName);
129225 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
129226 ExprList *pCheck = sqlite3ExprListDup(db, pTab->pCheck, 0);
129227 if( db->mallocFailed==0 ){
129232 pParse->iSelfTab = iDataCur + 1;
129233 for(k=pCheck->nExpr-1; k>0; k--){
129234 sqlite3ExprIfFalse(pParse, pCheck->a[k].pExpr, addrCkFault, 0);
129236 sqlite3ExprIfTrue(pParse, pCheck->a[0].pExpr, addrCkOk,
129239 pParse->iSelfTab = 0;
129241 pTab->zName);
129250 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
129260 pIdx->nColumn); VdbeCoverage(v);
129265 jmp5 = sqlite3VdbeLoadString(v, 4, pIdx->zName);
129276 for(kk=0; kk<pIdx->nKeyCol; kk++){
129277 int iCol = pIdx->aiColumn[kk];
129278 assert( iCol!=XN_ROWID && iCol<pTab->nCol );
129279 if( iCol>=0 && pTab->aCol[iCol].notNull ) continue;
129287 pIdx->nKeyCol); VdbeCoverage(v);
129288 sqlite3VdbeLoadString(v, 3, "non-unique entry in index ");
129297 sqlite3VdbeJumpHere(v, loopTop-1);
129300 for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
129305 sqlite3VdbeLoadString(v, 4, pIdx->zName);
129328 aOp[0].p2 = 1-mxErr;
129334 sqlite3VdbeChangeP3(v, 0, sqlite3VdbeCurrentAddr(v)-2);
129343 ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
129348 ** The second form of this pragma is a no-op if the main database file
129369 { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */
129370 { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */
129371 { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */
129374 { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */
129384 returnSingleText(v, encnames[ENC(pParse->db)].zName);
129391 if( (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
129392 for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
129393 if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
129394 u8 enc = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
129400 if( !pEnc->zName ){
129425 ** the value of the schema-version and user-version, respectively. Both
129426 ** the schema-version and the user-version are 32-bit signed integers
129429 ** The schema-cookie is usually only manipulated internally by SQLite. It
129436 ** the schema-version is potentially dangerous and may lead to program
129439 ** The user-version is not used internally by SQLite. It may be used by
129443 int iCookie = pPragma->iArg; /* Which cookie to read or write */
129445 if( zRight && (pPragma->mPragFlg & PragFlg_ReadOnly)==0 ){
129484 ** Return the names of all compile-time options used in this build,
129490 pParse->nMem = 1;
129507 int iBt = (pId2->z?iDb:SQLITE_MAX_DB);
129518 pParse->nMem = 3;
129537 db->xWalCallback==sqlite3WalDefaultHook ?
129538 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
129546 ** IMPLEMENTATION-OF: R-23445-46109 This pragma causes the database
129588 ** ever added that should be off by default, those off-by-default
129598 ** (2) The query planner used sqlite_stat1-style statistics for one or
129626 iTabCur = pParse->nTab++;
129627 for(iDbLast = zDb?iDb:db->nDb-1; iDb<=iDbLast; iDb++){
129630 pSchema = db->aDb[iDb].pSchema;
129631 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
129637 if( (pTab->tabFlags & TF_StatsUsed)==0 ) continue;
129640 szThreshold = pTab->nRowLogEst + 46; assert( sqlite3LogEst(25)==46 );
129641 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
129642 if( !pIdx->hasStat1 ){
129654 db->aDb[iDb].zDbSName, pTab->zName);
129678 assert( pPragma->ePragTyp==PragTyp_BUSY_TIMEOUT );
129682 returnSingleInt(v, db->busyTimeout);
129690 ** IMPLEMENTATION-OF: R-26343-45930 This pragma invokes the
129692 ** specified and is a non-negative integer.
129693 ** IMPLEMENTATION-OF: R-64451-07163 The soft_heap_limit pragma always
129695 ** sqlite3_soft_heap_limit64(-1) C-language function.
129702 returnSingleInt(v, sqlite3_soft_heap_limit64(-1));
129713 ** sqlite3_hard_heap_limit64() C-language API can raise or deactivate
129720 sqlite3_int64 iPrior = sqlite3_hard_heap_limit64(-1);
129723 returnSingleInt(v, sqlite3_hard_heap_limit64(-1));
129742 returnSingleInt(v, sqlite3_limit(db, SQLITE_LIMIT_WORKER_THREADS, -1));
129759 db->nAnalysisLimit = (int)(N&0x7fffffff);
129761 returnSingleInt(v, db->nAnalysisLimit);
129774 pParse->nMem = 2;
129775 for(i=0; i<db->nDb; i++){
129779 if( db->aDb[i].zDbSName==0 ) continue;
129780 pBt = db->aDb[i].pBt;
129783 }else if( sqlite3_file_control(db, i ? db->aDb[i].zDbSName : 0,
129787 sqlite3VdbeMultiLoad(v, 1, "ss", db->aDb[i].zDbSName, zState);
129795 if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
129804 /* The following block is a no-op unless SQLITE_DEBUG is defined. Its only
129809 if( (pPragma->mPragFlg & PragFlg_NoColumns1) && zRight ){
129860 for(i=0, j=pPragma->iPragCName; i<pPragma->nPragCName; i++, j++){
129865 sqlite3_str_appendf(&acc, "(\"%s\"", pPragma->zName);
129869 if( pPragma->mPragFlg & PragFlg_Result1 ){
129873 if( pPragma->mPragFlg & (PragFlg_SchemaOpt|PragFlg_SchemaReq) ){
129879 assert( strlen(zBuf) < sizeof(zBuf)-1 );
129887 pTab->pName = pPragma;
129888 pTab->db = db;
129889 pTab->iHidden = i;
129890 pTab->nHidden = j;
129922 pIdxInfo->estimatedCost = (double)1;
129923 if( pTab->nHidden==0 ){ return SQLITE_OK; }
129924 pConstraint = pIdxInfo->aConstraint;
129927 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
129928 if( pConstraint->usable==0 ) continue;
129929 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
129930 if( pConstraint->iColumn < pTab->iHidden ) continue;
129931 j = pConstraint->iColumn - pTab->iHidden;
129936 pIdxInfo->estimatedCost = (double)2147483647;
129937 pIdxInfo->estimatedRows = 2147483647;
129940 j = seen[0]-1;
129941 pIdxInfo->aConstraintUsage[j].argvIndex = 1;
129942 pIdxInfo->aConstraintUsage[j].omit = 1;
129944 pIdxInfo->estimatedCost = (double)20;
129945 pIdxInfo->estimatedRows = 20;
129946 j = seen[1]-1;
129947 pIdxInfo->aConstraintUsage[j].argvIndex = 2;
129948 pIdxInfo->aConstraintUsage[j].omit = 1;
129958 pCsr->base.pVtab = pVtab;
129959 *ppCursor = &pCsr->base;
129966 sqlite3_finalize(pCsr->pPragma);
129967 pCsr->pPragma = 0;
129968 for(i=0; i<ArraySize(pCsr->azArg); i++){
129969 sqlite3_free(pCsr->azArg[i]);
129970 pCsr->azArg[i] = 0;
129988 pCsr->iRowid++;
129989 assert( pCsr->pPragma );
129990 if( SQLITE_ROW!=sqlite3_step(pCsr->pPragma) ){
129991 rc = sqlite3_finalize(pCsr->pPragma);
129992 pCsr->pPragma = 0;
130007 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
130016 j = (pTab->pName->mPragFlg & PragFlg_Result1)!=0 ? 0 : 1;
130019 assert( j<ArraySize(pCsr->azArg) );
130020 assert( pCsr->azArg[j]==0 );
130022 pCsr->azArg[j] = sqlite3_mprintf("%s", zText);
130023 if( pCsr->azArg[j]==0 ){
130028 sqlite3StrAccumInit(&acc, 0, 0, 0, pTab->db->aLimit[SQLITE_LIMIT_SQL_LENGTH]);
130030 if( pCsr->azArg[1] ){
130031 sqlite3_str_appendf(&acc, "%Q.", pCsr->azArg[1]);
130033 sqlite3_str_appendall(&acc, pTab->pName->zName);
130034 if( pCsr->azArg[0] ){
130035 sqlite3_str_appendf(&acc, "=%Q", pCsr->azArg[0]);
130039 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pPragma, 0);
130042 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
130053 return (pCsr->pPragma==0);
130065 PragmaVtab *pTab = (PragmaVtab*)(pVtabCursor->pVtab);
130066 if( i<pTab->iHidden ){
130067 sqlite3_result_value(ctx, sqlite3_column_value(pCsr->pPragma, i));
130069 sqlite3_result_text(ctx, pCsr->azArg[i-pTab->iHidden],-1,SQLITE_TRANSIENT);
130079 *p = pCsr->iRowid;
130086 0, /* xCreate - create a table */
130087 pragmaVtabConnect, /* xConnect - connect to an existing table */
130088 pragmaVtabBestIndex, /* xBestIndex - Determine search strategy */
130089 pragmaVtabDisconnect, /* xDisconnect - Disconnect from a table */
130090 0, /* xDestroy - Drop a table */
130091 pragmaVtabOpen, /* xOpen - open a cursor */
130092 pragmaVtabClose, /* xClose - close a cursor */
130093 pragmaVtabFilter, /* xFilter - configure scan constraints */
130094 pragmaVtabNext, /* xNext - advance a cursor */
130096 pragmaVtabColumn, /* xColumn - read data */
130097 pragmaVtabRowid, /* xRowid - read data */
130098 0, /* xUpdate - write data */
130099 0, /* xBegin - begin transaction */
130100 0, /* xSync - sync transaction */
130101 0, /* xCommit - commit transaction */
130102 0, /* xRollback - rollback transaction */
130103 0, /* xFindFunction - function overloading */
130104 0, /* xRename - rename the table */
130121 if( (pName->mPragFlg & (PragFlg_Result0|PragFlg_Result1))==0 ) return 0;
130122 assert( sqlite3HashFind(&db->aModule, zName)==0 );
130150 ** Fill the InitData structure with an error message that indicates
130158 sqlite3 *db = pData->db;
130159 if( db->mallocFailed ){
130160 pData->rc = SQLITE_NOMEM_BKPT;
130161 }else if( pData->pzErrMsg[0]!=0 ){
130162 /* A error message has already been generated. Do not overwrite it */
130163 }else if( pData->mInitFlags & (INITFLAG_AlterRename|INITFLAG_AlterDrop) ){
130164 *pData->pzErrMsg = sqlite3MPrintf(db,
130166 (pData->mInitFlags & INITFLAG_AlterRename) ? "rename" : "drop column",
130169 pData->rc = SQLITE_ERROR;
130170 }else if( db->flags & SQLITE_WriteSchema ){
130171 pData->rc = SQLITE_CORRUPT_BKPT;
130176 if( zExtra && zExtra[0] ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
130177 *pData->pzErrMsg = z;
130178 pData->rc = SQLITE_CORRUPT_BKPT;
130189 for(p=pIndex->pTable->pIndex; p; p=p->pNext){
130190 if( p->tnum==pIndex->tnum && p!=pIndex ) return 1;
130198 const char *zSql, /* UTF-8 encoded SQL statement. */
130223 sqlite3 *db = pData->db;
130224 int iDb = pData->iDb;
130228 assert( sqlite3_mutex_held(db->mutex) );
130229 db->mDbFlags |= DBFLAG_EncodingFixed;
130230 pData->nInitRow++;
130231 if( db->mallocFailed ){
130236 assert( iDb>=0 && iDb<db->nDb );
130244 ** But because db->init.busy is set to 1, no VDBE code is generated
130254 u8 saved_iDb = db->init.iDb;
130258 assert( db->init.busy );
130259 db->init.iDb = iDb;
130260 if( sqlite3GetUInt32(argv[3], &db->init.newTnum)==0
130261 || (db->init.newTnum>pData->mxPage && pData->mxPage>0)
130267 db->init.orphanTrigger = 0;
130268 db->init.azInit = argv;
130270 TESTONLY(rcp = ) sqlite3Prepare(db, argv[4], -1, 0, 0, &pStmt, 0);
130271 rc = db->errCode;
130273 db->init.iDb = saved_iDb;
130274 /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
130276 if( db->init.orphanTrigger ){
130279 if( rc > pData->rc ) pData->rc = rc;
130298 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
130302 if( sqlite3GetUInt32(argv[3],&pIndex->tnum)==0
130303 || pIndex->tnum<2
130304 || pIndex->tnum>pData->mxPage
130335 int mask = ((db->mDbFlags & DBFLAG_EncodingFixed) | ~DBFLAG_EncodingFixed);
130337 assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
130338 assert( iDb>=0 && iDb<db->nDb );
130339 assert( db->aDb[iDb].pSchema );
130340 assert( sqlite3_mutex_held(db->mutex) );
130341 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
130343 db->init.busy = 1;
130345 /* Construct the in-memory representation schema tables (sqlite_schema or
130349 ** the schema table as read-only. */
130365 db->mDbFlags &= mask;
130373 pDb = &db->aDb[iDb];
130374 if( pDb->pBt==0 ){
130381 /* If there is not already a read-only (or read-write) transaction opened
130382 ** on the b-tree database, open one now. If a transaction is opened, it
130384 sqlite3BtreeEnter(pDb->pBt);
130385 if( sqlite3BtreeTxnState(pDb->pBt)==SQLITE_TXN_NONE ){
130386 rc = sqlite3BtreeBeginTrans(pDb->pBt, 0, 0);
130401 ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
130412 sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
130414 if( (db->flags & SQLITE_ResetDatabase)!=0 ){
130417 pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
130419 /* If opening a non-empty database, check the text encoding. For the
130424 if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */
130425 if( iDb==0 && (db->mDbFlags & DBFLAG_EncodingFixed)==0 ){
130429 encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
130437 if( (meta[BTREE_TEXT_ENCODING-1] & 3)!=ENC(db) ){
130445 pDb->pSchema->enc = ENC(db);
130447 if( pDb->pSchema->cache_size==0 ){
130449 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
130451 pDb->pSchema->cache_size = size;
130453 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
130455 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
130461 ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults
130464 pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
130465 if( pDb->pSchema->file_format==0 ){
130466 pDb->pSchema->file_format = 1;
130468 if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
130479 if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
130480 db->flags &= ~(u64)SQLITE_LegacyFileFmt;
130485 assert( db->init.busy );
130486 initData.mxPage = sqlite3BtreeLastPage(pDb->pBt);
130491 db->aDb[iDb].zDbSName, zSchemaTabName);
130495 xAuth = db->xAuth;
130496 db->xAuth = 0;
130500 db->xAuth = xAuth;
130511 if( db->mallocFailed ){
130515 if( rc==SQLITE_OK || (db->flags&SQLITE_NoSchemaError)){
130534 sqlite3BtreeCommit(pDb->pBt);
130536 sqlite3BtreeLeave(pDb->pBt);
130545 db->init.busy = 0;
130550 ** Initialize all database files - the main database file, the file
130553 ** error occurs, write an error message into *pzErrMsg.
130560 int commit_internal = !(db->mDbFlags&DBFLAG_SchemaChange);
130562 assert( sqlite3_mutex_held(db->mutex) );
130563 assert( sqlite3BtreeHoldsMutex(db->aDb[0].pBt) );
130564 assert( db->init.busy==0 );
130566 assert( db->nDb>0 );
130573 for(i=db->nDb-1; i>0; i--){
130574 assert( i==1 || sqlite3BtreeHoldsMutex(db->aDb[i].pBt) );
130587 ** This routine is a no-op if the database schema is already initialized.
130592 sqlite3 *db = pParse->db;
130593 assert( sqlite3_mutex_held(db->mutex) );
130594 if( !db->init.busy ){
130595 rc = sqlite3Init(db, &pParse->zErrMsg);
130597 pParse->rc = rc;
130598 pParse->nErr++;
130599 }else if( db->noSharedCache ){
130600 db->mDbFlags |= DBFLAG_SchemaKnownOk;
130609 ** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies
130610 ** make no changes to pParse->rc.
130613 sqlite3 *db = pParse->db;
130618 assert( pParse->checkSchema );
130619 assert( sqlite3_mutex_held(db->mutex) );
130620 for(iDb=0; iDb<db->nDb; iDb++){
130622 Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */
130625 /* If there is not already a read-only (or read-write) transaction opened
130626 ** on the b-tree database, open one now. If a transaction is opened, it
130627 ** will be closed immediately after reading the meta-value. */
130638 ** value stored as part of the in-memory schema representation,
130642 if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
130644 pParse->rc = SQLITE_SCHEMA;
130656 ** which database file in db->aDb[] the schema refers to.
130662 int i = -32768;
130664 /* If pSchema is NULL, then return -32768. This happens when code in
130666 ** created by a sub-select). In this case the return value of this
130669 ** We return -32768 instead of the more usual -1 simply because using
130670 ** -32768 as the incorrect index into db->aDb[] is much
130671 ** more likely to cause a segfault than -1 (of course there are assert()
130673 ** -32768 will still fit into a 16-bit signed integer.
130675 assert( sqlite3_mutex_held(db->mutex) );
130678 assert( i<db->nDb );
130679 if( db->aDb[i].pSchema==pSchema ){
130683 assert( i>=0 && i<db->nDb );
130692 sqlite3 *db = pParse->db;
130693 while( pParse->pCleanup ){
130694 ParseCleanup *pCleanup = pParse->pCleanup;
130695 pParse->pCleanup = pCleanup->pNext;
130696 pCleanup->xCleanup(db, pCleanup->pPtr);
130699 sqlite3DbFree(db, pParse->aLabel);
130700 if( pParse->pConstExpr ){
130701 sqlite3ExprListDelete(db, pParse->pConstExpr);
130704 assert( db->lookaside.bDisable >= pParse->disableLookaside );
130705 db->lookaside.bDisable -= pParse->disableLookaside;
130706 db->lookaside.sz = db->lookaside.bDisable ? 0 : db->lookaside.szTrue;
130708 pParse->disableLookaside = 0;
130719 ** common cleanups, we save a single NULL-pointer comparison in
130724 ** pParse->earlyCleanup flag is set in that case. Calling code show verify
130726 ** use-after-free errors following an OOM. The preferred way to do this is
130729 ** testcase( pParse->earlyCleanup );
130744 ParseCleanup *pCleanup = sqlite3DbMallocRaw(pParse->db, sizeof(*pCleanup));
130746 pCleanup->pNext = pParse->pCleanup;
130747 pParse->pCleanup = pCleanup;
130748 pCleanup->pPtr = pPtr;
130749 pCleanup->xCleanup = xCleanup;
130751 xCleanup(pParse->db, pPtr);
130754 pParse->earlyCleanup = 1;
130761 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
130765 const char *zSql, /* UTF-8 encoded SQL statement. */
130772 char *zErrMsg = 0; /* Error message */
130781 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
130782 assert( sqlite3_mutex_held(db->mutex) );
130784 /* For a long-term use prepared statement avoid the use of
130795 ** some other database connection is holding a write-lock, which in
130816 if( !db->noSharedCache ){
130817 for(i=0; i<db->nDb; i++) {
130818 Btree *pBt = db->aDb[i].pBt;
130823 const char *zDb = db->aDb[i].zDbSName;
130825 testcase( db->flags & SQLITE_ReadUncommit );
130835 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
130837 int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
130848 sParse.zTail = &zSql[sParse.zTail-zSqlCopy];
130862 if( db->init.busy==0 ){
130863 sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
130865 if( db->mallocFailed ){
130894 sParse.pTriggerPrg = pT->pNext;
130905 const char *zSql, /* UTF-8 encoded SQL statement. */
130922 sqlite3_mutex_enter(db->mutex);
130931 || (rc==SQLITE_SCHEMA && (sqlite3ResetOneSchema(db,-1), cnt++)==0) );
130934 assert( (rc&db->errMask)==rc );
130935 db->busyHandler.nBusy = 0;
130936 sqlite3_mutex_leave(db->mutex);
130956 assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
130960 assert( sqlite3_mutex_held(db->mutex) );
130962 rc = sqlite3LockAndPrepare(db, zSql, -1, prepFlags, p, &pNew, 0);
130990 const char *zSql, /* UTF-8 encoded SQL statement. */
131002 const char *zSql, /* UTF-8 encoded SQL statement. */
131008 /* EVIDENCE-OF: R-37923-12173 The sqlite3_prepare_v2() interface works
131020 const char *zSql, /* UTF-8 encoded SQL statement. */
131027 /* EVIDENCE-OF: R-56861-42673 sqlite3_prepare_v3() differs from
131044 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
131048 const void *zSql, /* UTF-16 encoded SQL statement. */
131054 /* This function currently works by first transforming the UTF-16
131055 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
131075 sqlite3_mutex_enter(db->mutex);
131078 rc = sqlite3LockAndPrepare(db, zSql8, -1, prepFlags, 0, ppStmt, &zTail8);
131083 ** equivalent pointer into the UTF-16 string by counting the unicode
131085 ** the same number of characters into the UTF-16 string.
131087 int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
131092 sqlite3_mutex_leave(db->mutex);
131106 const void *zSql, /* UTF-16 encoded SQL statement. */
131118 const void *zSql, /* UTF-16 encoded SQL statement. */
131130 const void *zSql, /* UTF-16 encoded SQL statement. */
131181 ** The aDefer[] array is used by the sorter-references optimization. For
131191 ** When the sorter-reference optimization is used, there is one entry in the
131200 int regReturn; /* Register holding block-output return address */
131201 int labelBkOut; /* Start label for the block-output subroutine */
131227 Select *pPrior = p->pPrior;
131228 sqlite3ExprListDelete(db, p->pEList);
131229 sqlite3SrcListDelete(db, p->pSrc);
131230 sqlite3ExprDelete(db, p->pWhere);
131231 sqlite3ExprListDelete(db, p->pGroupBy);
131232 sqlite3ExprDelete(db, p->pHaving);
131233 sqlite3ExprListDelete(db, p->pOrderBy);
131234 sqlite3ExprDelete(db, p->pLimit);
131235 if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
131237 if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
131238 sqlite3WindowListDelete(db, p->pWinDefn);
131240 while( p->pWin ){
131241 assert( p->pWin->ppThis==&p->pWin );
131242 sqlite3WindowUnlinkFromSelect(p->pWin);
131255 pDest->eDest = (u8)eDest;
131256 pDest->iSDParm = iParm;
131257 pDest->iSDParm2 = 0;
131258 pDest->zAffSdst = 0;
131259 pDest->iSdst = 0;
131260 pDest->nSdst = 0;
131271 SrcList *pSrc, /* the FROM clause -- which tables to scan */
131281 pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
131283 assert( pParse->db->mallocFailed );
131288 sqlite3Expr(pParse->db,TK_ASTERISK,0));
131290 pNew->pEList = pEList;
131291 pNew->op = TK_SELECT;
131292 pNew->selFlags = selFlags;
131293 pNew->iLimit = 0;
131294 pNew->iOffset = 0;
131295 pNew->selId = ++pParse->nSelect;
131296 pNew->addrOpenEphm[0] = -1;
131297 pNew->addrOpenEphm[1] = -1;
131298 pNew->nSelectRow = 0;
131299 if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc));
131300 pNew->pSrc = pSrc;
131301 pNew->pWhere = pWhere;
131302 pNew->pGroupBy = pGroupBy;
131303 pNew->pHaving = pHaving;
131304 pNew->pOrderBy = pOrderBy;
131305 pNew->pPrior = 0;
131306 pNew->pNext = 0;
131307 pNew->pLimit = pLimit;
131308 pNew->pWith = 0;
131310 pNew->pWin = 0;
131311 pNew->pWinDefn = 0;
131313 if( pParse->db->mallocFailed ) {
131314 clearSelect(pParse->db, pNew, pNew!=&standin);
131317 assert( pNew->pSrc!=0 || pParse->nErr>0 );
131331 ** Return a pointer to the right-most SELECT statement in a compound.
131334 while( p->pNext ) p = p->pNext;
131381 if( p->n==aKeyword[j].nChar
131382 && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
131413 ** Return the index of a column in a table. Return -1 if the column
131420 for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){
131421 if( pCol->hName==h && sqlite3StrICmp(pCol->zName, zCol)==0 ) return i;
131423 return -1;
131437 int N, /* Number of tables in pSrc->a[] to search */
131439 int *piTab, /* Write index of pSrc->a[] here */
131440 int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
131448 iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol);
131450 && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0)
131483 sqlite3 *db = pParse->db;
131489 assert( pSrc->nSrc>iRight );
131490 assert( pSrc->a[iLeft].pTab );
131491 assert( pSrc->a[iRight].pTab );
131501 pEq->iRightJoinTable = pE2->iTable;
131537 p->iRightJoinTable = iTable;
131538 if( p->op==TK_FUNCTION && p->x.pList ){
131540 for(i=0; i<p->x.pList->nExpr; i++){
131541 sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable);
131544 sqlite3SetJoinExpr(p->pLeft, iTable);
131545 p = p->pRight;
131558 && (iTable<0 || p->iRightJoinTable==iTable) ){
131561 if( p->op==TK_COLUMN && p->iTable==iTable ){
131564 if( p->op==TK_FUNCTION && p->x.pList ){
131566 for(i=0; i<p->x.pList->nExpr; i++){
131567 unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
131570 unsetJoinExpr(p->pLeft, iTable);
131571 p = p->pRight;
131581 ** The left most table is the first entry in Select.pSrc. The right-most
131595 pSrc = p->pSrc;
131596 pLeft = &pSrc->a[0];
131598 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
131599 Table *pRightTab = pRight->pTab;
131602 if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue;
131603 isOuter = (pRight->fg.jointype & JT_OUTER)!=0;
131608 if( pRight->fg.jointype & JT_NATURAL ){
131609 if( pRight->pOn || pRight->pUsing ){
131614 for(j=0; j<pRightTab->nCol; j++){
131619 if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue;
131620 zName = pRightTab->aCol[j].zName;
131623 isOuter, &p->pWhere);
131630 if( pRight->pOn && pRight->pUsing ){
131639 if( pRight->pOn ){
131640 if( isOuter ) sqlite3SetJoinExpr(pRight->pOn, pRight->iCursor);
131641 p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
131642 pRight->pOn = 0;
131652 if( pRight->pUsing ){
131653 IdList *pList = pRight->pUsing;
131654 for(j=0; j<pList->nId; j++){
131660 zName = pList->a[j].zName;
131665 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
131670 isOuter, &p->pWhere);
131700 sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult,
131701 0, pInfo->ecelFlags);
131703 if( pInfo->pExtra ){
131704 sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0);
131705 sqlite3ExprListDelete(pParse->db, pInfo->pExtra);
131723 int nOBSat = pSort->nOBSat;
131724 Vdbe *v = pParse->pVdbe;
131725 int regOut = ++pParse->nMem;
131726 if( pSort->pDeferredRowLoad ){
131727 innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad);
131729 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut);
131735 ** through regData+nData-1 onto the sorter.
131746 Vdbe *v = pParse->pVdbe; /* Stmt under construction */
131747 int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0);
131748 int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */
131752 int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */
131776 regBase = regData - nPrefixReg;
131778 regBase = pParse->nMem + 1;
131779 pParse->nMem += nBase;
131781 assert( pSelect->iOffset==0 || pSelect->iLimit!=0 );
131782 iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit;
131783 pSort->labelDone = sqlite3VdbeMakeLabel(pParse);
131784 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData,
131787 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
131801 regPrevKey = pParse->nMem+1;
131802 pParse->nMem += pSort->nOBSat;
131803 nKey = nExpr - pSort->nOBSat + bSeq;
131807 addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor);
131810 sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
131811 pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
131812 if( pParse->db->mallocFailed ) return;
131813 pOp->p2 = nKey + nData;
131814 pKI = pOp->p4.pKeyInfo;
131815 memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
131816 sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
131817 testcase( pKI->nAllField > pKI->nKeyField+2 );
131818 pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
131819 pKI->nAllField-pKI->nKeyField-1);
131823 pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
131824 pSort->regReturn = ++pParse->nMem;
131825 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
131826 sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor);
131828 sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone);
131832 sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat);
131846 ** jump to the next iteration of the loop. If the pSort->labelOBLopt
131851 int iCsr = pSort->iECursor;
131856 iCsr, 0, regBase+nOBSat, nExpr-nOBSat);
131863 if( pSort->sortFlags & SORTFLAG_UseSorter ){
131868 sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord,
131869 regBase+nOBSat, nBase-nOBSat);
131872 pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v));
131909 v = pParse->pVdbe;
131920 ** This function is called as part of inner-loop generation for a SELECT
131922 ** determines the expressions, if any, that the sorter-reference
131923 ** optimization should be used for. The sorter-reference optimization
131936 ** for which the sorter-reference optimization should be enabled.
131937 ** Additionally, the pSort->aDefer[] array is populated with entries
131952 for(i=0; i<pEList->nExpr; i++){
131953 struct ExprList_item *pItem = &pEList->a[i];
131954 if( pItem->u.x.iOrderByCol==0 ){
131955 Expr *pExpr = pItem->pExpr;
131956 Table *pTab = pExpr->y.pTab;
131957 if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
131958 && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
131962 if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
131965 if( nDefer==ArraySize(pSort->aDefer) ){
131973 nKey = pPk->nKeyCol;
131978 pNew->iTable = pExpr->iTable;
131979 pNew->y.pTab = pExpr->y.pTab;
131980 pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
131984 pSort->aDefer[nDefer].pTab = pExpr->y.pTab;
131985 pSort->aDefer[nDefer].iCsr = pExpr->iTable;
131986 pSort->aDefer[nDefer].nKey = nKey;
131990 pItem->bSorterRef = 1;
131994 pSort->nDefer = (u8)nDefer;
132003 ** If srcTab is negative, then the p->pEList expressions
132005 ** zero or more, then data is pulled from srcTab and p->pEList is used only
132011 int srcTab, /* Pull data from this table if non-negative */
132018 Vdbe *v = pParse->pVdbe;
132021 int eDest = pDest->eDest; /* How to dispose of results */
132022 int iParm = pDest->iSDParm; /* First argument to disposal method */
132030 ** values for any expressions that are also part of the sort-key are omitted
132036 assert( p->pEList!=0 );
132037 hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
132038 if( pSort && pSort->pOrderBy==0 ) pSort = 0;
132041 codeOffset(v, p->iOffset, iContinue);
132046 nResultCol = p->pEList->nExpr;
132048 if( pDest->iSdst==0 ){
132050 nPrefixReg = pSort->pOrderBy->nExpr;
132051 if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++;
132052 pParse->nMem += nPrefixReg;
132054 pDest->iSdst = pParse->nMem+1;
132055 pParse->nMem += nResultCol;
132056 }else if( pDest->iSdst+nResultCol > pParse->nMem ){
132058 ** on the right-hand side of an INSERT contains more result columns than
132062 pParse->nMem += nResultCol;
132064 pDest->nSdst = nResultCol;
132065 regOrig = regResult = pDest->iSdst;
132069 VdbeComment((v, "%s", p->pEList->a[i].zEName));
132086 /* For each expression in p->pEList that is a copy of an expression in
132087 ** the ORDER BY clause (pSort->pOrderBy), set the associated
132089 ** expression within the sort-key that pushOntoSorter() will generate.
132090 ** This allows the p->pEList field to be omitted from the sorted record,
132094 for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){
132096 if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){
132097 p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat;
132101 selectExprDefer(pParse, pSort, p->pEList, &pExtra);
132102 if( pExtra && pParse->db->mallocFailed==0 ){
132108 VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
132109 pOp->p2 += (pExtra->nExpr - pSort->nDefer);
132110 pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer);
132111 pParse->nMem += pExtra->nExpr;
132117 pEList = p->pEList;
132118 for(i=0; i<pEList->nExpr; i++){
132119 if( pEList->a[i].u.x.iOrderByCol>0
132121 || pEList->a[i].bSorterRef
132124 nResultCol--;
132143 if( pExtra ) nResultCol += pExtra->nExpr;
132145 if( p->iLimit
132151 pSort->pDeferredRowLoad = &sRowLoadInfo;
132163 switch( pDistinct->eTnctType ){
132170 regPrev = pParse->nMem+1;
132171 pParse->nMem += nResultCol;
132179 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
132180 pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
132181 pOp->opcode = OP_Null;
132182 pOp->p1 = 1;
132183 pOp->p2 = regPrev;
132188 CollSeq *pColl = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
132189 if( i<nResultCol-1 ){
132196 sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
132199 assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed );
132200 sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
132205 sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
132210 assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
132211 codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
132217 codeOffset(v, p->iOffset, iContinue);
132290 int i2 = pDest->iSDParm2;
132299 regResult+(i2<0), nResultCol-(i2<0), r1);
132324 assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol );
132326 r1, pDest->zAffSdst, nResultCol);
132348 assert( nResultCol<=pDest->nSdst );
132352 assert( nResultCol==pDest->nSdst );
132360 case SRT_Coroutine: /* Send data to a co-routine */
132368 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
132377 ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an
132378 ** index with pSO->nExpr+2 columns. Build a key using pSO for the first
132379 ** pSO->nExpr columns, then make sure all keys are unique by adding a
132388 pSO = pDest->pOrderBy;
132390 nKey = pSO->nExpr;
132409 regResult + pSO->a[i].u.x.iOrderByCol - 1,
132427 ** user-defined functions that have side effects. We do not care
132441 if( pSort==0 && p->iLimit ){
132442 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
132451 int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
132454 p->aSortFlags = (u8*)&p->aColl[N+X];
132455 p->nKeyField = (u16)N;
132456 p->nAllField = (u16)(N+X);
132457 p->enc = ENC(db);
132458 p->db = db;
132459 p->nRef = 1;
132472 assert( p->nRef>0 );
132473 p->nRef--;
132474 if( p->nRef==0 ) sqlite3DbFreeNN(p->db, p);
132483 assert( p->nRef>0 );
132484 p->nRef++;
132496 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
132522 sqlite3 *db = pParse->db;
132525 nExpr = pList->nExpr;
132526 pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
132529 for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
132530 pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
132531 pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
132554 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
132557 ** "USE TEMP B-TREE FOR xxx"
132563 ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage));
132567 ** Assign expression b to lvalue a. A second, no-op, version of this macro
132576 /* No-op versions of the explainXXX() functions and macros. */
132583 ** If the inner loop was generated using a non-null pOrderBy argument,
132595 Vdbe *v = pParse->pVdbe; /* The prepared statement */
132596 int addrBreak = pSort->labelDone; /* Jump here to exit loop */
132601 ExprList *pOrderBy = pSort->pOrderBy;
132602 int eDest = pDest->eDest;
132603 int iParm = pDest->iSDParm;
132612 struct ExprList_item *aOutEx = p->pEList->a;
132615 if( pSort->labelBkOut ){
132616 sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut);
132618 sqlite3VdbeResolveLabel(v, pSort->labelBkOut);
132622 /* Open any cursors needed for sorter-reference expressions */
132623 for(i=0; i<pSort->nDefer; i++){
132624 Table *pTab = pSort->aDefer[i].pTab;
132625 int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
132626 sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead);
132627 nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey);
132631 iTab = pSort->iECursor;
132634 regRow = pDest->iSdst;
132644 nKey = pOrderBy->nExpr - pSort->nOBSat;
132645 if( pSort->sortFlags & SORTFLAG_UseSorter ){
132646 int regSortOut = ++pParse->nMem;
132647 iSortTab = pParse->nTab++;
132648 if( pSort->labelBkOut ){
132656 codeOffset(v, p->iOffset, addrContinue);
132661 codeOffset(v, p->iOffset, addrContinue);
132665 for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){
132672 if( pSort->nDefer ){
132676 for(i=0; i<pSort->nDefer; i++){
132677 int iCsr = pSort->aDefer[i].iCsr;
132678 Table *pTab = pSort->aDefer[i].pTab;
132679 int nKey = pSort->aDefer[i].nKey;
132689 assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey );
132702 for(i=nColumn-1; i>=0; i--){
132711 iRead = aOutEx[i].u.x.iOrderByCol-1;
132713 iRead = iCol--;
132730 assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) );
132732 pDest->zAffSdst, nColumn);
132742 int i2 = pDest->iSDParm2;
132744 sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1);
132757 sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
132759 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
132775 if( pSort->sortFlags & SORTFLAG_UseSorter ){
132780 if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn);
132795 ** result-set expression in all of the following SELECT statements is
132806 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
132833 assert( pNC->pSrcList!=0 );
132834 switch( pExpr->op ){
132842 int iCol = pExpr->iColumn; /* Index of column in pTab */
132844 SrcList *pTabList = pNC->pSrcList;
132845 for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
132846 if( j<pTabList->nSrc ){
132847 pTab = pTabList->a[j].pTab;
132848 pS = pTabList->a[j].pSelect;
132850 pNC = pNC->pNext;
132865 ** sub-select. In this case, set the column type to NULL, even
132875 assert( pTab && pExpr->y.pTab==pTab );
132877 /* The "table" is actually a sub-select or a view in the FROM clause
132879 ** data for the result-set column of the sub-select.
132881 if( iCol>=0 && iCol<pS->pEList->nExpr ){
132883 ** rowid of the sub-select or view. This expression is legal (see
132884 ** test case misc2.2.2) - it always evaluates to NULL.
132887 Expr *p = pS->pEList->a[iCol].pExpr;
132888 sNC.pSrcList = pS->pSrc;
132890 sNC.pParse = pNC->pParse;
132897 if( iCol<0 ) iCol = pTab->iPKey;
132898 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
132903 zOrigCol = pTab->aCol[iCol].zName;
132904 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
132906 zOrigTab = pTab->zName;
132907 if( pNC->pParse && pTab->pSchema ){
132908 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
132909 zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName;
132912 assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) );
132916 zType = sqlite3ColumnType(&pTab->aCol[iCol],0);
132924 /* The expression is a sub-select. Return the declaration type and
132929 Select *pS = pExpr->x.pSelect;
132930 Expr *p = pS->pEList->a[0].pExpr;
132932 sNC.pSrcList = pS->pSrc;
132934 sNC.pParse = pNC->pParse;
132962 Vdbe *v = pParse->pVdbe;
132968 for(i=0; i<pEList->nExpr; i++){
132969 Expr *p = pEList->a[i].pExpr;
132977 /* The vdbe must make its own copy of the column-type and other
133027 Vdbe *v = pParse->pVdbe;
133032 sqlite3 *db = pParse->db;
133038 if( pParse->explain ){
133043 if( pParse->colNamesSet ) return;
133044 /* Column names are determined by the left-most term of a compound select */
133045 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
133047 pTabList = pSelect->pSrc;
133048 pEList = pSelect->pEList;
133051 pParse->colNamesSet = 1;
133052 fullName = (db->flags & SQLITE_FullColNames)!=0;
133053 srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
133054 sqlite3VdbeSetNumCols(v, pEList->nExpr);
133055 for(i=0; i<pEList->nExpr; i++){
133056 Expr *p = pEList->a[i].pExpr;
133059 assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */
133060 assert( p->op!=TK_COLUMN || p->y.pTab!=0 ); /* Covering idx not yet coded */
133061 if( pEList->a[i].zEName && pEList->a[i].eEName==ENAME_NAME ){
133063 char *zName = pEList->a[i].zEName;
133065 }else if( srcName && p->op==TK_COLUMN ){
133067 int iCol = p->iColumn;
133068 pTab = p->y.pTab;
133070 if( iCol<0 ) iCol = pTab->iPKey;
133071 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
133075 zCol = pTab->aCol[iCol].zName;
133079 zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
133085 const char *z = pEList->a[i].zEName;
133121 sqlite3 *db = pParse->db; /* Database connection */
133133 nCol = pEList->nExpr;
133145 for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
133148 if( (zName = pEList->a[i].zEName)!=0 && pEList->a[i].eEName==ENAME_NAME ){
133151 Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
133152 while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){
133153 pColExpr = pColExpr->pRight;
133156 if( pColExpr->op==TK_COLUMN && (pTab = pColExpr->y.pTab)!=0 ){
133158 int iCol = pColExpr->iColumn;
133159 if( iCol<0 ) iCol = pTab->iPKey;
133160 zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
133161 }else if( pColExpr->op==TK_ID ){
133163 zName = pColExpr->u.zToken;
133166 zName = pEList->a[i].zEName;
133182 for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){}
133188 pCol->zName = zName;
133189 pCol->hName = sqlite3StrIHash(zName);
133196 if( db->mallocFailed ){
133225 sqlite3 *db = pParse->db;
133234 assert( (pSelect->selFlags & SF_Resolved)!=0 );
133235 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
133236 if( db->mallocFailed ) return;
133238 sNC.pSrcList = pSelect->pSrc;
133239 a = pSelect->pEList->a;
133240 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
133243 pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT);
133246 /* pCol->szEst = ... // Column size est for SELECT tables never used */
133247 pCol->affinity = sqlite3ExprAffinity(p);
133250 n = sqlite3Strlen30(pCol->zName);
133251 pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
133252 if( pCol->zName ){
133253 memcpy(&pCol->zName[n+1], zType, m+1);
133254 pCol->colFlags |= COLFLAG_HASTYPE;
133257 if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
133259 if( pColl && pCol->zColl==0 ){
133260 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
133263 pTab->szTabRow = 1; /* Any non-zero value works */
133272 sqlite3 *db = pParse->db;
133275 savedFlags = db->flags;
133276 db->flags &= ~(u64)SQLITE_FullColNames;
133277 db->flags |= SQLITE_ShortColNames;
133279 db->flags = savedFlags;
133280 if( pParse->nErr ) return 0;
133281 while( pSelect->pPrior ) pSelect = pSelect->pPrior;
133286 pTab->nTabRef = 1;
133287 pTab->zName = 0;
133288 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
133289 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
133291 pTab->iPKey = -1;
133292 if( db->mallocFailed ){
133301 ** If an error occurs, return NULL and leave a message in pParse.
133304 if( pParse->pVdbe ){
133305 return pParse->pVdbe;
133307 if( pParse->pToplevel==0
133308 && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
133310 pParse->okConstFactor = 1;
133318 ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions
133326 ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit
133334 ** Only if pLimit->pLeft!=0 do the limit registers get
133344 Expr *pLimit = p->pLimit;
133346 if( p->iLimit ) return;
133349 ** "LIMIT -1" always shows all rows. There is some
133355 assert( pLimit->op==TK_LIMIT );
133356 assert( pLimit->pLeft!=0 );
133357 p->iLimit = iLimit = ++pParse->nMem;
133360 if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
133365 }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
133366 p->nSelectRow = sqlite3LogEst((u64)n);
133367 p->selFlags |= SF_FixedLimit;
133370 sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
133375 if( pLimit->pRight ){
133376 p->iOffset = iOffset = ++pParse->nMem;
133377 pParse->nMem++; /* Allocate an extra register for limit+offset */
133378 sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
133389 ** Return the appropriate collating sequence for the iCol-th column of
133390 ** the result set for the compound-select statement "p". Return NULL if
133394 ** left-most term of the select that has a collating sequence.
133398 if( p->pPrior ){
133399 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
133404 /* iCol must be less than p->pEList->nExpr. Otherwise an error would
133407 if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){
133408 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
133423 ExprList *pOrderBy = p->pOrderBy;
133424 int nOrderBy = p->pOrderBy->nExpr;
133425 sqlite3 *db = pParse->db;
133430 struct ExprList_item *pItem = &pOrderBy->a[i];
133431 Expr *pTerm = pItem->pExpr;
133434 if( pTerm->flags & EP_Collate ){
133437 pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
133438 if( pColl==0 ) pColl = db->pDfltColl;
133439 pOrderBy->a[i].pExpr =
133440 sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
133443 pRet->aColl[i] = pColl;
133444 pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
133456 ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
133458 ** p->pPrior p
133461 ** There is exactly one reference to the recursive-table in the FROM clause
133462 ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag.
133464 ** The setup-query runs once to generate an initial set of rows that go
133468 ** recursive-table for a recursive-query run. The output of the recursive-query
133493 SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */
133494 int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */
133495 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
133496 Select *pSetup = p->pPrior; /* The setup query */
133497 Select *pFirstRec; /* Left-most recursive term */
133513 if( p->pWin ){
133524 p->nSelectRow = 320; /* 4 billion rows */
133526 pLimit = p->pLimit;
133527 regLimit = p->iLimit;
133528 regOffset = p->iOffset;
133529 p->pLimit = 0;
133530 p->iLimit = p->iOffset = 0;
133531 pOrderBy = p->pOrderBy;
133534 for(i=0; ALWAYS(i<pSrc->nSrc); i++){
133535 if( pSrc->a[i].fg.isRecursive ){
133536 iCurrent = pSrc->a[i].iCursor;
133544 iQueue = pParse->nTab++;
133545 if( p->op==TK_UNION ){
133547 iDistinct = pParse->nTab++;
133554 regCurrent = ++pParse->nMem;
133558 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
133566 p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
133567 p->selFlags |= SF_UsesEphemeral;
133571 p->pOrderBy = 0;
133577 ** iDistinct table. pFirstRec is left pointing to the left-most
133581 for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){
133582 if( pFirstRec->selFlags & SF_Aggregate ){
133586 pFirstRec->op = TK_ALL;
133587 if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break;
133590 /* Store the results of the setup-query in Queue. */
133591 pSetup = pFirstRec->pPrior;
133592 pSetup->pNext = 0;
133595 pSetup->pNext = p;
133604 sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
133622 ** the value for the recursive-table. Store the results in the Queue.
133624 pFirstRec->pPrior = 0;
133627 assert( pFirstRec->pPrior==0 );
133628 pFirstRec->pPrior = pSetup;
133635 sqlite3ExprListDelete(pParse->db, p->pOrderBy);
133636 p->pOrderBy = pOrderBy;
133637 p->pLimit = pLimit;
133645 Select *p, /* The right-most of SELECTs to be coded */
133650 ** Handle the special case of a compound-select that originates from a
133663 ** Since the limit is exactly 1, we only need to evalutes the left-most VALUES.
133667 Select *p, /* The right-most of SELECTs to be coded */
133672 int bShowAll = p->pLimit==0;
133673 assert( p->selFlags & SF_MultiValue );
133675 assert( p->selFlags & SF_Values );
133676 assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
133677 assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
133679 if( p->pWin ) return -1;
133681 if( p->pPrior==0 ) break;
133682 assert( p->pPrior->pNext==p );
133683 p = p->pPrior;
133689 selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1);
133691 p->nSelectRow = nRow;
133692 p = p->pNext;
133703 while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; }
133712 ** "p" points to the right-most of the two queries. the query on the
133713 ** left is p->pPrior. The left query could also be a compound query
133719 ** Example 1: Consider a three-way compound SQL statement.
133727 ** `-----> SELECT b FROM t2
133729 ** `------> SELECT a FROM t1
133733 ** pPrior will be the t2 query. p->op will be TK_UNION in this case.
133740 Select *p, /* The right-most of SELECTs to be coded */
133751 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
133753 assert( p && p->pPrior ); /* Calling function guarantees this much */
133754 assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
133755 assert( p->selFlags & SF_Compound );
133756 db = pParse->db;
133757 pPrior = p->pPrior;
133759 assert( pPrior->pOrderBy==0 );
133760 assert( pPrior->pLimit==0 );
133768 assert( p->pEList );
133769 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
133773 /* Special handling for a compound-select that originates as a VALUES clause.
133775 if( p->selFlags & SF_MultiValue ){
133784 assert( p->pEList && pPrior->pEList );
133785 assert( p->pEList->nExpr==pPrior->pEList->nExpr );
133788 if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){
133795 if( p->pOrderBy ){
133800 if( pPrior->pPrior==0 ){
133802 ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY"));
133808 switch( p->op ){
133812 assert( !pPrior->pLimit );
133813 pPrior->iLimit = p->iLimit;
133814 pPrior->iOffset = p->iOffset;
133815 pPrior->pLimit = p->pLimit;
133817 pPrior->pLimit = 0;
133821 p->pPrior = 0;
133822 p->iLimit = pPrior->iLimit;
133823 p->iOffset = pPrior->iOffset;
133824 if( p->iLimit ){
133825 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
133827 if( p->iOffset ){
133829 p->iLimit, p->iOffset+1, p->iOffset);
133835 pDelete = p->pPrior;
133836 p->pPrior = pPrior;
133837 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
133838 if( p->pLimit
133839 && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit)
133840 && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit)
133842 p->nSelectRow = sqlite3LogEst((u64)nLimit);
133854 Expr *pLimit; /* Saved values of p->nLimit */
133858 testcase( p->op==TK_EXCEPT );
133859 testcase( p->op==TK_UNION );
133865 assert( p->pLimit==0 ); /* Not allowed on leftward elements */
133871 unionTab = pParse->nTab++;
133872 assert( p->pOrderBy==0 );
133874 assert( p->addrOpenEphm[0] == -1 );
133875 p->addrOpenEphm[0] = addr;
133876 findRightmost(p)->selFlags |= SF_UsesEphemeral;
133877 assert( p->pEList );
133883 assert( !pPrior->pOrderBy );
133892 if( p->op==TK_EXCEPT ){
133895 assert( p->op==TK_UNION );
133898 p->pPrior = 0;
133899 pLimit = p->pLimit;
133900 p->pLimit = 0;
133902 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
133903 sqlite3SelectOpName(p->op)));
133906 assert( p->pOrderBy==0 );
133907 pDelete = p->pPrior;
133908 p->pPrior = pPrior;
133909 p->pOrderBy = 0;
133910 if( p->op==TK_UNION ){
133911 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
133913 sqlite3ExprDelete(db, p->pLimit);
133914 p->pLimit = pLimit;
133915 p->iLimit = 0;
133916 p->iOffset = 0;
133922 assert( p->pEList || db->mallocFailed );
133923 if( dest.eDest!=priorOp && db->mallocFailed==0 ){
133939 default: assert( p->op==TK_INTERSECT ); {
133951 tab1 = pParse->nTab++;
133952 tab2 = pParse->nTab++;
133953 assert( p->pOrderBy==0 );
133956 assert( p->addrOpenEphm[0] == -1 );
133957 p->addrOpenEphm[0] = addr;
133958 findRightmost(p)->selFlags |= SF_UsesEphemeral;
133959 assert( p->pEList );
133972 assert( p->addrOpenEphm[1] == -1 );
133973 p->addrOpenEphm[1] = addr;
133974 p->pPrior = 0;
133975 pLimit = p->pLimit;
133976 p->pLimit = 0;
133978 ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE",
133979 sqlite3SelectOpName(p->op)));
133982 pDelete = p->pPrior;
133983 p->pPrior = pPrior;
133984 if( p->nSelectRow>pPrior->nSelectRow ){
133985 p->nSelectRow = pPrior->nSelectRow;
133987 sqlite3ExprDelete(db, p->pLimit);
133988 p->pLimit = pLimit;
133994 assert( p->pEList );
134016 if( p->pNext==0 ){
134021 if( pParse->nErr ) goto multi_select_end;
134027 ** This section is run by the right-most SELECT statement only.
134028 ** SELECT statements to the left always skip this part. The right-most
134032 if( p->selFlags & SF_UsesEphemeral ){
134036 CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */
134039 assert( p->pNext==0 );
134040 nCol = p->pEList->nExpr;
134046 for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
134049 *apColl = db->pDfltColl;
134053 for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
134055 int addr = pLoop->addrOpenEphm[i];
134059 assert( pLoop->addrOpenEphm[1]<0 );
134065 pLoop->addrOpenEphm[i] = -1;
134072 pDest->iSdst = dest.iSdst;
134073 pDest->nSdst = dest.nSdst;
134080 ** Error message for when two or more terms of a compound select have different
134084 if( p->selFlags & SF_Values ){
134089 sqlite3SelectOpName(p->op));
134097 ** The data to be output is contained in pIn->iSdst. There are
134098 ** pIn->nSdst columns to be output. pDest is where the output should
134110 ** If the LIMIT found in p->iLimit is reached, jump immediately to
134123 Vdbe *v = pParse->pVdbe;
134135 addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
134139 sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
134142 if( pParse->db->mallocFailed ) return 0;
134146 codeOffset(v, p->iOffset, iContinue);
134148 assert( pDest->eDest!=SRT_Exists );
134149 assert( pDest->eDest!=SRT_Table );
134150 switch( pDest->eDest ){
134156 sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
134157 sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
134158 sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
134170 testcase( pIn->nSdst>1 );
134172 sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst,
134173 r1, pDest->zAffSdst, pIn->nSdst);
134174 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1,
134175 pIn->iSdst, pIn->nSdst);
134183 ** if it is the RHS of a row-value IN operator.
134186 testcase( pIn->nSdst>1 );
134187 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
134194 ** starting at pDest->iSdst. Then the co-routine yields.
134197 if( pDest->iSdst==0 ){
134198 pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
134199 pDest->nSdst = pIn->nSdst;
134201 sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst);
134202 sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
134215 assert( pDest->eDest==SRT_Output );
134216 sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
134223 if( p->iLimit ){
134224 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v);
134245 ** co-routines. Then run the co-routines in parallel and merge the results
134272 ** ------------- ----------------- -------------- -----------------
134323 Select *p, /* The right-most of SELECTs to be coded */
134331 int regAddrA; /* Address register for select-A coroutine */
134332 int regAddrB; /* Address register for select-B coroutine */
134333 int addrSelectA; /* Address of the select-A coroutine */
134334 int addrSelectB; /* Address of the select-B coroutine */
134335 int regOutA; /* Address register for the output-A subroutine */
134336 int regOutB; /* Address register for the output-B subroutine */
134337 int addrOutA; /* Address of the output-A subroutine */
134338 int addrOutB = 0; /* Address of the output-B subroutine */
134339 int addrEofA; /* Address of the select-A-exhausted subroutine */
134341 int addrEofB; /* Address of the select-B-exhausted subroutine */
134345 int regLimitA; /* Limit register for select-A */
134346 int regLimitB; /* Limit register for select-A */
134348 int savedLimit; /* Saved value of p->iLimit */
134349 int savedOffset; /* Saved value of p->iOffset */
134361 assert( p->pOrderBy!=0 );
134363 db = pParse->db;
134364 v = pParse->pVdbe;
134372 op = p->op;
134373 pPrior = p->pPrior;
134374 assert( pPrior->pOrderBy==0 );
134375 pOrderBy = p->pOrderBy;
134377 nOrderBy = pOrderBy->nExpr;
134384 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
134386 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
134387 assert( pItem->u.x.iOrderByCol>0 );
134388 if( pItem->u.x.iOrderByCol==i ) break;
134393 pNew->flags |= EP_IntValue;
134394 pNew->u.iValue = i;
134395 p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
134396 if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
134412 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
134413 assert( pItem->u.x.iOrderByCol>0 );
134414 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
134415 aPermute[i] = pItem->u.x.iOrderByCol - 1;
134424 p->pOrderBy = pOrderBy;
134425 pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
134434 int nExpr = p->pEList->nExpr;
134435 assert( nOrderBy>=nExpr || db->mallocFailed );
134436 regPrev = pParse->nMem+1;
134437 pParse->nMem += nExpr+1;
134443 pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
134444 pKeyDup->aSortFlags[i] = 0;
134451 p->pPrior = 0;
134452 pPrior->pNext = 0;
134453 sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
134454 if( pPrior->pPrior==0 ){
134455 sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
134460 if( p->iLimit && op==TK_ALL ){
134461 regLimitA = ++pParse->nMem;
134462 regLimitB = ++pParse->nMem;
134463 sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
134469 sqlite3ExprDelete(db, p->pLimit);
134470 p->pLimit = 0;
134472 regAddrA = ++pParse->nMem;
134473 regAddrB = ++pParse->nMem;
134474 regOutA = ++pParse->nMem;
134475 regOutB = ++pParse->nMem;
134479 ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op)));
134482 ** left of the compound operator - the "A" select.
134487 pPrior->iLimit = regLimitA;
134494 ** the right - the "B" select
134499 savedLimit = p->iLimit;
134500 savedOffset = p->iOffset;
134501 p->iLimit = regLimitB;
134502 p->iOffset = 0;
134505 p->iLimit = savedLimit;
134506 p->iOffset = savedOffset;
134534 VdbeNoopComment((v, "eof-A subroutine"));
134539 p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
134547 if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
134549 VdbeNoopComment((v, "eof-B subroutine"));
134557 VdbeNoopComment((v, "A-lt-B subroutine"));
134570 VdbeNoopComment((v, "A-eq-B subroutine"));
134578 VdbeNoopComment((v, "A-gt-B subroutine"));
134607 if( p->pPrior ){
134608 sqlite3SelectDelete(db, p->pPrior);
134610 p->pPrior = pPrior;
134611 pPrior->pNext = p;
134616 return pParse->nErr!=0;
134642 ** a column in table number iTable with a copy of the iColumn-th
134659 && pExpr->iRightJoinTable==pSubst->iTable
134661 pExpr->iRightJoinTable = pSubst->iNewTable;
134663 if( pExpr->op==TK_COLUMN
134664 && pExpr->iTable==pSubst->iTable
134667 if( pExpr->iColumn<0 ){
134668 pExpr->op = TK_NULL;
134671 Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
134673 assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
134674 assert( pExpr->pRight==0 );
134676 sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
134678 sqlite3 *db = pSubst->pParse->db;
134679 if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
134683 ifNullRow.iTable = pSubst->iNewTable;
134689 if( pNew && pSubst->isLeftJoin ){
134693 sqlite3SetJoinExpr(pNew, pExpr->iRightJoinTable);
134699 ** just as it did when it was a column of a view or sub-query. */
134701 if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
134702 CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
134703 pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr,
134704 (pColl ? pColl->zName : "BINARY")
134712 if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
134713 pExpr->iTable = pSubst->iNewTable;
134715 pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
134716 pExpr->pRight = substExpr(pSubst, pExpr->pRight);
134718 substSelect(pSubst, pExpr->x.pSelect, 1);
134720 substExprList(pSubst, pExpr->x.pList);
134724 Window *pWin = pExpr->y.pWin;
134725 pWin->pFilter = substExpr(pSubst, pWin->pFilter);
134726 substExprList(pSubst, pWin->pPartition);
134727 substExprList(pSubst, pWin->pOrderBy);
134739 for(i=0; i<pList->nExpr; i++){
134740 pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr);
134746 int doPrior /* Do substitutes on p->pPrior too */
134753 substExprList(pSubst, p->pEList);
134754 substExprList(pSubst, p->pGroupBy);
134755 substExprList(pSubst, p->pOrderBy);
134756 p->pHaving = substExpr(pSubst, p->pHaving);
134757 p->pWhere = substExpr(pSubst, p->pWhere);
134758 pSrc = p->pSrc;
134760 for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
134761 substSelect(pSubst, pItem->pSelect, 1);
134762 if( pItem->fg.isTabFunc ){
134763 substExprList(pSubst, pItem->u1.pFuncArg);
134766 }while( doPrior && (p = p->pPrior)!=0 );
134776 ** pSrcItem->colUsed mask.
134780 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
134781 pItem = pWalker->u.pSrcItem;
134782 if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue;
134783 if( pExpr->iColumn<0 ) return WRC_Continue;
134784 pItem->colUsed |= sqlite3ExprColUsed(pExpr);
134792 if( NEVER(pSrcItem->pTab==0) ) return;
134797 pSrcItem->colUsed = 0;
134813 ** If pSrc contains any sub-selects, call this routine recursively
134814 ** on the FROM clause of each such sub-select, with iExcept set to -1.
134824 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
134827 pItem->iCursor = aCsrMap[pItem->iCursor] = pParse->nTab++;
134828 for(p=pItem->pSelect; p; p=p->pPrior){
134829 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1);
134840 int *aCsrMap = pWalker->u.aiCol;
134841 int op = pExpr->op;
134842 if( (op==TK_COLUMN || op==TK_IF_NULL_ROW) && aCsrMap[pExpr->iTable] ){
134843 pExpr->iTable = aCsrMap[pExpr->iTable];
134845 if( ExprHasProperty(pExpr, EP_FromJoin) && aCsrMap[pExpr->iRightJoinTable] ){
134846 pExpr->iRightJoinTable = aCsrMap[pExpr->iRightJoinTable];
134854 ** cursor in the FROM clause of any FROM clause sub-selects, recursively.
134876 srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept);
134921 ** other than the one FROM-clause subquery that is a candidate
134923 ** from 2015-02-09.)
134935 ** sub-queries that were excluded from this optimization. Restriction
134950 ** (**) Restriction (10) was removed from the code on 2005-02-05 but we
134951 ** accidently carried the comment forward until 2014-09-15. Original
134983 ** The parent and sub-query may contain WHERE clauses. Subject to
134990 ** Also, each component of the sub-query must return the same number
134993 ** such (illegal) sub-query is flattened. The caller will detect the
134994 ** syntax error and return a detailed message.
134996 ** (18) If the sub-query is a compound select, then all terms of the
135003 ** (20) If the sub-query is a compound select, then it must not use
135014 ** (23) If the outer query is a recursive CTE, then the sub-query may not be
135020 ** The subquery may not be an aggregate that uses the built-in min() or
135031 ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
135034 ** If flattening is not attempted, this routine is a no-op and returns 0.
135043 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
135046 const char *zSavedAuthContext = pParse->zAuthContext;
135049 Select *pSub1; /* Pointer to the rightmost select in sub-query */
135053 int iNewParent = -1;/* Replacement table for iParent */
135058 sqlite3 *db = pParse->db;
135065 assert( p->pPrior==0 );
135067 pSrc = p->pSrc;
135068 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
135069 pSubitem = &pSrc->a[iFrom];
135070 iParent = pSubitem->iCursor;
135071 pSub = pSubitem->pSelect;
135075 if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */
135078 pSubSrc = pSub->pSrc;
135082 ** because they could be computed at compile-time. But when LIMIT and OFFSET
135085 if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
135086 if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */
135087 if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
135090 if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
135091 if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */
135092 if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
135095 if( p->pOrderBy && pSub->pOrderBy ){
135098 if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */
135099 if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */
135100 if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
135103 if( pSub->selFlags & (SF_Recursive) ){
135122 ** aggregates are processed - there is no mechanism to determine if
135123 ** the LEFT JOIN table should be all-NULL.
135127 if( (pSubitem->fg.jointype & JT_OUTER)!=0 ){
135129 if( pSubSrc->nSrc>1 /* (3a) */
135131 || IsVirtual(pSubSrc->a[0].pTab) /* (3c) */
135132 || (p->selFlags & SF_Distinct)!=0 /* (3d) */
135139 /* Setting isLeftJoin to -1 causes OP_IfNullRow opcodes to be generated for
135141 ** though they are not necessary. This will stress-test the OP_IfNullRow
135143 isLeftJoin = -1;
135147 /* Restriction (17): If the sub-query is a compound SELECT, then it must
135152 if( pSub->pPrior ){
135153 if( pSub->pOrderBy ){
135156 if( isAgg || (p->selFlags & SF_Distinct)!=0 || isLeftJoin>0 ){
135159 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
135160 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
135161 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
135162 assert( pSub->pSrc!=0 );
135163 assert( (pSub->selFlags & SF_Recursive)==0 );
135164 assert( pSub->pEList->nExpr==pSub1->pEList->nExpr );
135165 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */
135166 || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */
135167 || pSub1->pSrc->nSrc<1 /* (17c) */
135169 || pSub1->pWin /* (17e) */
135174 testcase( pSub1->pSrc->nSrc>1 );
135178 if( p->pOrderBy ){
135180 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
135181 if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
135186 if( (p->selFlags & SF_Recursive) ) return 0;
135188 if( pSrc->nSrc>1 ){
135189 if( pParse->nSelect>500 ) return 0;
135190 aCsrMap = sqlite3DbMallocZero(db, pParse->nTab*sizeof(int));
135196 pSub->selId, pSub, iFrom));
135199 pParse->zAuthContext = pSubitem->zName;
135202 pParse->zAuthContext = zSavedAuthContext;
135205 pSub1 = pSubitem->pSelect;
135206 sqlite3DbFree(db, pSubitem->zDatabase);
135207 sqlite3DbFree(db, pSubitem->zName);
135208 sqlite3DbFree(db, pSubitem->zAlias);
135209 pSubitem->zDatabase = 0;
135210 pSubitem->zName = 0;
135211 pSubitem->zAlias = 0;
135212 pSubitem->pSelect = 0;
135213 assert( pSubitem->pOn==0 );
135215 /* If the sub-query is a compound SELECT statement, then (by restrictions
135219 ** SELECT <expr-list> FROM (<sub-query>) <where-clause>
135222 ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
135223 ** OFFSET clauses and joins them to the left-hand-side of the original
135225 ** select statements in the compound sub-query.
135246 ** We call this the "compound-subquery flattening".
135248 for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
135250 ExprList *pOrderBy = p->pOrderBy;
135251 Expr *pLimit = p->pLimit;
135252 Select *pPrior = p->pPrior;
135253 Table *pItemTab = pSubitem->pTab;
135254 pSubitem->pTab = 0;
135255 p->pOrderBy = 0;
135256 p->pPrior = 0;
135257 p->pLimit = 0;
135259 p->pLimit = pLimit;
135260 p->pOrderBy = pOrderBy;
135261 p->op = TK_ALL;
135262 pSubitem->pTab = pItemTab;
135264 p->pPrior = pPrior;
135266 pNew->selId = ++pParse->nSelect;
135267 if( aCsrMap && db->mallocFailed==0 ){
135270 pNew->pPrior = pPrior;
135271 if( pPrior ) pPrior->pNext = pNew;
135272 pNew->pNext = p;
135273 p->pPrior = pNew;
135274 SELECTTRACE(2,pParse,p,("compound-subquery flattener"
135275 " creates %u as peer\n",pNew->selId));
135277 assert( pSubitem->pSelect==0 );
135280 if( db->mallocFailed ){
135281 pSubitem->pSelect = pSub1;
135290 ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
135292 if( ALWAYS(pSubitem->pTab!=0) ){
135293 Table *pTabToDel = pSubitem->pTab;
135294 if( pTabToDel->nTabRef==1 ){
135299 testcase( pToplevel->earlyCleanup );
135301 pTabToDel->nTabRef--;
135303 pSubitem->pTab = 0;
135306 /* The following loop runs once for each term in a compound-subquery
135308 ** of flattening - a flattening other than a compound-subquery flattening -
135320 for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
135324 pSubSrc = pSub->pSrc; /* FROM clause of subquery */
135325 nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */
135326 pSrc = pParent->pSrc; /* FROM clause of the outer query */
135329 jointype = pSubitem->fg.jointype; /* First time through the loop */
135348 pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1);
135350 pParent->pSrc = pSrc;
135357 sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
135358 assert( pSrc->a[i+iFrom].fg.isTabFunc==0 );
135359 pSrc->a[i+iFrom] = pSubSrc->a[i];
135360 iNewParent = pSubSrc->a[i].iCursor;
135361 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
135363 pSrc->a[iFrom].fg.jointype = jointype;
135377 if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){
135378 /* At this point, any non-zero iOrderByCol values indicate that the
135385 ** function attempts to flatten a compound sub-query into pParent
135386 ** (the only way this can happen is if the compound sub-query is
135387 ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */
135388 ExprList *pOrderBy = pSub->pOrderBy;
135389 for(i=0; i<pOrderBy->nExpr; i++){
135390 pOrderBy->a[i].u.x.iOrderByCol = 0;
135392 assert( pParent->pOrderBy==0 );
135393 pParent->pOrderBy = pOrderBy;
135394 pSub->pOrderBy = 0;
135396 pWhere = pSub->pWhere;
135397 pSub->pWhere = 0;
135402 if( pParent->pWhere ){
135403 pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere);
135405 pParent->pWhere = pWhere;
135408 if( db->mallocFailed==0 ){
135414 x.pEList = pSub->pEList;
135420 pParent->selFlags |= pSub->selFlags & SF_Compound;
135421 assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */
135429 if( pSub->pLimit ){
135430 pParent->pLimit = pSub->pLimit;
135431 pSub->pLimit = 0;
135437 recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]);
135486 assert( pColumn->op==TK_COLUMN );
135491 if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){
135495 /* 2018-10-25 ticket [cf5ed20f]
135497 for(i=0; i<pConst->nConst; i++){
135498 const Expr *pE2 = pConst->apExpr[i*2];
135499 assert( pE2->op==TK_COLUMN );
135500 if( pE2->iTable==pColumn->iTable
135501 && pE2->iColumn==pColumn->iColumn
135507 pConst->nConst++;
135508 pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr,
135509 pConst->nConst*2*sizeof(Expr*));
135510 if( pConst->apExpr==0 ){
135511 pConst->nConst = 0;
135513 pConst->apExpr[pConst->nConst*2-2] = pColumn;
135514 pConst->apExpr[pConst->nConst*2-1] = pValue;
135521 ** is part of the AND-connected terms of the expression. For each term
135528 if( pExpr->op==TK_AND ){
135529 findConstInWhere(pConst, pExpr->pRight);
135530 findConstInWhere(pConst, pExpr->pLeft);
135533 if( pExpr->op!=TK_EQ ) return;
135534 pRight = pExpr->pRight;
135535 pLeft = pExpr->pLeft;
135538 if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){
135541 if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){
135549 ** columns named in pWalker->u.pConst, then overwrite it with its
135555 if( pExpr->op!=TK_COLUMN ) return WRC_Continue;
135561 pConst = pWalker->u.pConst;
135562 for(i=0; i<pConst->nConst; i++){
135563 Expr *pColumn = pConst->apExpr[i*2];
135565 if( pColumn->iTable!=pExpr->iTable ) continue;
135566 if( pColumn->iColumn!=pExpr->iColumn ) continue;
135568 pConst->nChng++;
135571 assert( pExpr->pLeft==0 );
135572 pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0);
135579 ** The WHERE-clause constant propagation optimization.
135582 ** CONSTANT=COLUMN that are top-level AND-connected terms that are not
135626 findConstInWhere(&x, p->pWhere);
135635 sqlite3WalkExpr(&w, p->pWhere);
135636 sqlite3DbFree(x.pParse->db, x.apExpr);
135647 ** push WHERE clause expression pExpr down to FROM clause sub-query
135654 ** BY clause of all window function used by the sub-query. It is safe
135660 ** * the sub-query uses only one distinct window frame, and
135664 assert( pSubq->pWin->pPartition );
135665 assert( (pSubq->selFlags & SF_MultiPart)==0 );
135666 assert( pSubq->pPrior==0 );
135667 return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition);
135677 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
135681 ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
135689 ** (1) (** This restriction was removed on 2017-09-29. We used to
135707 ** of a LEFT JOIN where iCursor is not the right-hand table of that
135726 ** (6b) The inner query is a compound and uses window-functions.
135730 ** all window-functions used by the sub-query. It is safe to
135732 ** window over which any window-function is calculated.
135738 ** Return 0 if no changes are made and non-zero if one or more WHERE clause
135751 if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0;
135754 if( pSubq->pPrior ){
135756 for(pSel=pSubq; pSel; pSel=pSel->pPrior){
135757 if( pSel->pWin ) return 0; /* restriction (6b) */
135760 if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0;
135771 for(pX=pSubq; pX; pX=pX->pPrior){
135772 assert( (pX->selFlags & (SF_Recursive))==0 );
135777 if( pSubq->pLimit!=0 ){
135780 while( pWhere->op==TK_AND ){
135781 nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
135783 pWhere = pWhere->pLeft;
135787 || pWhere->iRightJoinTable!=iCursor)
135791 if( ExprHasProperty(pWhere,EP_FromJoin) && pWhere->iRightJoinTable!=iCursor ){
135796 pSubq->selFlags |= SF_PushDown;
135799 pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
135800 unsetJoinExpr(pNew, -1);
135805 x.pEList = pSubq->pEList;
135808 if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){
135809 /* Restriction 6c has prevented push-down in this case */
135810 sqlite3ExprDelete(pParse->db, pNew);
135811 nChng--;
135815 if( pSubq->selFlags & SF_Aggregate ){
135816 pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
135818 pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
135820 pSubq = pSubq->pPrior;
135845 ExprList *pEList = pFunc->x.pList; /* Arguments to agg function */
135851 assert( pFunc->op==TK_AGG_FUNCTION );
135854 || pEList->nExpr!=1
135860 zFunc = pFunc->u.zToken;
135863 if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){
135873 assert( pOrderBy!=0 || db->mallocFailed );
135874 if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
135880 ** The second argument is the associated aggregate-info object. This
135885 ** where table is a database table, not a sub-select or view. If the query
135893 assert( !p->pGroupBy );
135895 if( p->pWhere || p->pEList->nExpr!=1
135896 || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
135900 pTab = p->pSrc->a[0].pTab;
135901 pExpr = p->pEList->a[0].pExpr;
135902 assert( pTab && !pTab->pSelect && pExpr );
135905 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
135906 if( NEVER(pAggInfo->nFunc==0) ) return 0;
135907 if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
135914 ** If the source-list item passed as an argument was augmented with an
135918 ** pFrom->pIndex and return SQLITE_OK.
135921 Table *pTab = pFrom->pTab;
135922 char *zIndexedBy = pFrom->u1.zIndexedBy;
135925 assert( pFrom->fg.isIndexedBy!=0 );
135927 for(pIdx=pTab->pIndex;
135928 pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy);
135929 pIdx=pIdx->pNext
135933 pParse->checkSchema = 1;
135936 pFrom->u2.pIBIndex = pIdx;
135971 if( p->pPrior==0 ) return WRC_Continue;
135972 if( p->pOrderBy==0 ) return WRC_Continue;
135973 for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
135975 a = p->pOrderBy->a;
135977 /* If iOrderByCol is already non-zero, then it has already been matched
135979 ** SELECT is rewritten for window-functions processing and then passed
135984 for(i=p->pOrderBy->nExpr-1; i>=0; i--){
135985 if( a[i].pExpr->flags & EP_Collate ) break;
135991 pParse = pWalker->pParse;
135992 db = pParse->db;
135999 p->pSrc = pNewSrc;
136000 p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0));
136001 p->op = TK_SELECT;
136002 p->pWhere = 0;
136003 pNew->pGroupBy = 0;
136004 pNew->pHaving = 0;
136005 pNew->pOrderBy = 0;
136006 p->pPrior = 0;
136007 p->pNext = 0;
136008 p->pWith = 0;
136010 p->pWinDefn = 0;
136012 p->selFlags &= ~SF_Compound;
136013 assert( (p->selFlags & SF_Converted)==0 );
136014 p->selFlags |= SF_Converted;
136015 assert( pNew->pPrior!=0 );
136016 pNew->pPrior->pNext = pNew;
136017 pNew->pLimit = 0;
136022 ** Check to see if the FROM clause term pFrom has table-valued function
136023 ** arguments. If it does, leave an error message in pParse and return
136024 ** non-zero, since pFrom is not allowed to be a table-valued function.
136027 if( pFrom->fg.isTabFunc ){
136028 sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName);
136038 ** FROM clause element pItem is really a common-table-expression (CTE)
136042 ** If a non-NULL value is returned, set *ppContext to point to the With
136050 const char *zName = pItem->zName;
136052 assert( pItem->zDatabase==0 );
136054 for(p=pWith; p; p=p->pOuter){
136056 for(i=0; i<p->nCte; i++){
136057 if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
136059 return &p->a[i];
136067 ** with the inner-most WITH clause being at the top of the stack.
136078 assert( pParse->pWith!=pWith );
136079 pWith->pOuter = pParse->pWith;
136080 pParse->pWith = pWith;
136085 testcase( pParse->earlyCleanup );
136093 ** pParse->pWith linked list). And if currently processing a CTE
136097 ** If pFrom matches a CTE according to either of these two above, pFrom->pTab
136112 assert( pFrom->pTab==0 );
136113 if( pParse->pWith==0 ){
136117 if( pFrom->zDatabase!=0 ){
136122 pCte = searchWith(pParse->pWith, pFrom, &pWith);
136124 sqlite3 *db = pParse->db;
136128 Select *pLeft; /* Left-most SELECT statement */
136129 Select *pRecTerm; /* Left-most recursive term */
136131 With *pSavedWith; /* Initial value of pParse->pWith */
136132 int iRecTab = -1; /* Cursor for recursive table */
136135 /* If pCte->zCteErr is non-NULL at this point, then this is an illegal
136137 ** early. If pCte->zCteErr is NULL, then this is not a recursive reference.
136139 if( pCte->zCteErr ){
136140 sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName);
136145 assert( pFrom->pTab==0 );
136148 pCteUse = pCte->pUse;
136150 pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0]));
136157 pCteUse->eM10d = pCte->eM10d;
136159 pFrom->pTab = pTab;
136160 pTab->nTabRef = 1;
136161 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
136162 pTab->iPKey = -1;
136163 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
136164 pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid;
136165 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
136166 if( db->mallocFailed ) return 2;
136167 assert( pFrom->pSelect );
136168 pFrom->fg.isCte = 1;
136169 pFrom->u2.pCteUse = pCteUse;
136170 pCteUse->nUse++;
136171 if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){
136172 pCteUse->eM10d = M10d_Yes;
136176 pRecTerm = pSel = pFrom->pSelect;
136177 bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
136178 while( bMayRecursive && pRecTerm->op==pSel->op ){
136180 SrcList *pSrc = pRecTerm->pSrc;
136181 assert( pRecTerm->pPrior!=0 );
136182 for(i=0; i<pSrc->nSrc; i++){
136183 SrcItem *pItem = &pSrc->a[i];
136184 if( pItem->zDatabase==0
136185 && pItem->zName!=0
136186 && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
136188 pItem->pTab = pTab;
136189 pTab->nTabRef++;
136190 pItem->fg.isRecursive = 1;
136191 if( pRecTerm->selFlags & SF_Recursive ){
136193 "multiple references to recursive table: %s", pCte->zName
136197 pRecTerm->selFlags |= SF_Recursive;
136198 if( iRecTab<0 ) iRecTab = pParse->nTab++;
136199 pItem->iCursor = iRecTab;
136202 if( (pRecTerm->selFlags & SF_Recursive)==0 ) break;
136203 pRecTerm = pRecTerm->pPrior;
136206 pCte->zCteErr = "circular reference: %s";
136207 pSavedWith = pParse->pWith;
136208 pParse->pWith = pWith;
136209 if( pSel->selFlags & SF_Recursive ){
136212 assert( (pRecTerm->selFlags & SF_Recursive)==0 );
136213 assert( pRecTerm->pNext!=0 );
136214 assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 );
136215 assert( pRecTerm->pWith==0 );
136216 pRecTerm->pWith = pSel->pWith;
136218 pRecTerm->pWith = 0;
136220 pParse->pWith = pSavedWith;
136225 pParse->pWith = pSavedWith;
136229 pParse->pWith = pWith;
136231 for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
136232 pEList = pLeft->pEList;
136233 if( pCte->pCols ){
136234 if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){
136236 pCte->zName, pEList->nExpr, pCte->pCols->nExpr
136238 pParse->pWith = pSavedWith;
136241 pEList = pCte->pCols;
136244 sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
136246 if( pSel->selFlags & SF_Recursive ){
136247 pCte->zCteErr = "multiple recursive references: %s";
136249 pCte->zCteErr = "recursive reference in a subquery: %s";
136253 pCte->zCteErr = 0;
136254 pParse->pWith = pSavedWith;
136271 Parse *pParse = pWalker->pParse;
136272 if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){
136273 With *pWith = findRightmost(p)->pWith;
136275 assert( pParse->pWith==pWith || pParse->nErr );
136276 pParse->pWith = pWith->pOuter;
136286 ** sub-query in the FROM clause of a SELECT statement. This function
136292 Select *pSel = pFrom->pSelect;
136296 pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table));
136298 pTab->nTabRef = 1;
136299 if( pFrom->zAlias ){
136300 pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias);
136302 pTab->zName = sqlite3MPrintf(pParse->db, "subquery_%u", pSel->selId);
136304 while( pSel->pPrior ){ pSel = pSel->pPrior; }
136305 sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
136306 pTab->iPKey = -1;
136307 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
136308 pTab->tabFlags |= TF_Ephemeral;
136310 return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
136320 ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
136322 ** fill pTabList->a[].pSelect with a copy of the SELECT statement
136338 Parse *pParse = pWalker->pParse;
136343 sqlite3 *db = pParse->db;
136345 u16 selFlags = p->selFlags;
136348 p->selFlags |= SF_Expanded;
136349 if( db->mallocFailed ){
136352 assert( p->pSrc!=0 );
136356 if( pWalker->eCode ){
136358 p->selId = ++pParse->nSelect;
136360 pTabList = p->pSrc;
136361 pEList = p->pEList;
136362 sqlite3WithPush(pParse, p->pWith, 0);
136373 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
136375 assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 );
136376 if( pFrom->pTab ) continue;
136377 assert( pFrom->fg.isRecursive==0 );
136378 if( pFrom->zName==0 ){
136380 Select *pSel = pFrom->pSelect;
136381 /* A sub-query in the FROM clause of a SELECT */
136383 assert( pFrom->pTab==0 );
136390 pTab = pFrom->pTab;
136395 assert( pFrom->pTab==0 );
136396 pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
136398 if( pTab->nTabRef>=0xffff ){
136400 pTab->zName);
136401 pFrom->pTab = 0;
136404 pTab->nTabRef++;
136409 if( IsVirtual(pTab) || pTab->pSelect ){
136411 u8 eCodeOrig = pWalker->eCode;
136413 assert( pFrom->pSelect==0 );
136414 if( pTab->pSelect
136415 && (db->flags & SQLITE_EnableView)==0
136416 && pTab->pSchema!=db->aDb[1].pSchema
136419 pTab->zName);
136423 && pFrom->fg.fromDDL
136424 && ALWAYS(pTab->pVTable!=0)
136425 && pTab->pVTable->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0)
136428 pTab->zName);
136431 pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
136432 nCol = pTab->nCol;
136433 pTab->nCol = -1;
136434 pWalker->eCode = 1; /* Turn on Select.selId renumbering */
136435 sqlite3WalkSelect(pWalker, pFrom->pSelect);
136436 pWalker->eCode = eCodeOrig;
136437 pTab->nCol = nCol;
136443 if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){
136450 if( pParse->nErr || db->mallocFailed || sqliteProcessJoin(pParse, p) ){
136465 for(k=0; k<pEList->nExpr; k++){
136466 pE = pEList->a[k].pExpr;
136467 if( pE->op==TK_ASTERISK ) break;
136468 assert( pE->op!=TK_DOT || pE->pRight!=0 );
136469 assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
136470 if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
136471 elistFlags |= pE->flags;
136473 if( k<pEList->nExpr ){
136479 struct ExprList_item *a = pEList->a;
136481 int flags = pParse->db->flags;
136485 for(k=0; k<pEList->nExpr; k++){
136487 elistFlags |= pE->flags;
136488 pRight = pE->pRight;
136489 assert( pE->op!=TK_DOT || pRight!=0 );
136490 if( pE->op!=TK_ASTERISK
136491 && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
136497 pNew->a[pNew->nExpr-1].zEName = a[k].zEName;
136498 pNew->a[pNew->nExpr-1].eEName = a[k].eEName;
136507 if( pE->op==TK_DOT ){
136508 assert( pE->pLeft!=0 );
136509 assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
136510 zTName = pE->pLeft->u.zToken;
136512 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
136513 Table *pTab = pFrom->pTab;
136514 Select *pSub = pFrom->pSelect;
136515 char *zTabName = pFrom->zAlias;
136519 zTabName = pTab->zName;
136521 if( db->mallocFailed ) break;
136522 if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
136527 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
136528 zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*";
136530 for(j=0; j<pTab->nCol; j++){
136531 char *zName = pTab->aCol[j].zName;
136538 && sqlite3MatchEName(&pSub->pEList->a[j], 0, zTName, 0)==0
136544 ** result-set list unless the SELECT has the SF_IncludeHidden
136547 if( (p->selFlags & SF_IncludeHidden)==0
136548 && IsHiddenColumn(&pTab->aCol[j])
136555 if( (pFrom->fg.jointype & JT_NATURAL)!=0
136562 if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
136571 if( longNames || pTabList->nSrc>1 ){
136589 if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){
136590 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
136591 sqlite3DbFree(db, pX->zEName);
136593 pX->zEName = sqlite3DbStrDup(db, pSub->pEList->a[j].zEName);
136594 testcase( pX->zEName==0 );
136596 pX->zEName = sqlite3MPrintf(db, "%s.%s.%s",
136598 testcase( pX->zEName==0 );
136600 pX->eEName = ENAME_TAB;
136615 p->pEList = pNew;
136617 if( p->pEList ){
136618 if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
136623 p->selFlags |= SF_ComplexResult;
136648 ** If anything goes wrong, an error message is written into pParse.
136649 ** The calling function can detect the problem by looking at pParse->nErr
136650 ** and/or pParse->db->mallocFailed.
136656 if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){
136673 ** For each FROM-clause subquery, add Column.zType and Column.zColl
136688 assert( p->selFlags & SF_Resolved );
136689 if( p->selFlags & SF_HasTypeInfo ) return;
136690 p->selFlags |= SF_HasTypeInfo;
136691 pParse = pWalker->pParse;
136692 pTabList = p->pSrc;
136693 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
136694 Table *pTab = pFrom->pTab;
136696 if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
136697 /* A sub-query in the FROM clause of a SELECT */
136698 Select *pSel = pFrom->pSelect;
136700 while( pSel->pPrior ) pSel = pSel->pPrior;
136712 ** the Table structures of all FROM-clause subqueries in a
136733 ** * VDBE Cursor numbers are assigned to all FROM-clause terms.
136734 ** * Ephemeral Table objects are created for all FROM-clause subqueries.
136746 assert( p!=0 || pParse->db->mallocFailed );
136747 if( pParse->db->mallocFailed ) return;
136748 if( p->selFlags & SF_HasTypeInfo ) return;
136750 if( pParse->nErr || pParse->db->mallocFailed ) return;
136752 if( pParse->nErr || pParse->db->mallocFailed ) return;
136765 Vdbe *v = pParse->pVdbe;
136768 int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
136770 if( pParse->nErr || pParse->db->mallocFailed ) return;
136774 assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
136775 for(i=0; i<pAggInfo->nColumn; i++){
136776 assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
136777 && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
136779 for(i=0; i<pAggInfo->nFunc; i++){
136780 assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
136781 && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
136784 sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
136785 for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
136786 if( pFunc->iDistinct>=0 ){
136787 Expr *pE = pFunc->pFExpr;
136789 if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
136792 pFunc->iDistinct = -1;
136794 KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
136795 sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
136807 Vdbe *v = pParse->pVdbe;
136810 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
136811 ExprList *pList = pF->pFExpr->x.pList;
136812 assert( !ExprHasProperty(pF->pFExpr, EP_xIsSelect) );
136813 sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
136814 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
136823 ** If regAcc is non-zero and there are no min() or max() aggregates
136824 ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator
136829 Vdbe *v = pParse->pVdbe;
136836 pAggInfo->directMode = 1;
136837 for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
136841 ExprList *pList = pF->pFExpr->x.pList;
136842 assert( !ExprHasProperty(pF->pFExpr, EP_xIsSelect) );
136843 assert( !IsWindowFunc(pF->pFExpr) );
136844 if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){
136845 Expr *pFilter = pF->pFExpr->y.pWin->pFilter;
136846 if( pAggInfo->nAccumulator
136847 && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
136853 if( regHit==0 ) regHit = ++pParse->nMem;
136867 nArg = pList->nExpr;
136874 if( pF->iDistinct>=0 ){
136880 codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
136882 if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
136886 assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */
136887 for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
136888 pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
136891 pColl = pParse->db->pDfltColl;
136893 if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
136896 sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, pF->iMem);
136897 sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
136904 if( regHit==0 && pAggInfo->nAccumulator ){
136910 for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
136911 sqlite3ExprCode(pParse, pC->pCExpr, pC->iMem);
136914 pAggInfo->directMode = 0;
136930 if( pParse->explain==2 ){
136933 pTab->zName,
136935 bCover ? pIdx->zName : ""
136950 ** sub-expression matches the criteria for being moved to the WHERE
136951 ** clause. If so, add it to the WHERE clause and replace the sub-expression
136955 if( pExpr->op!=TK_AND ){
136956 Select *pS = pWalker->u.pSelect;
136957 if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy)
136960 sqlite3 *db = pWalker->pParse->db;
136963 Expr *pWhere = pS->pWhere;
136965 pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
136966 pS->pWhere = pNew;
136967 pWalker->eCode = 1;
136996 sqlite3WalkExpr(&sWalker, p->pHaving);
137006 ** Check to see if the pThis entry of pTabList is a self-join of a prior view.
137011 SrcList *pTabList, /* Search for self-joins in this FROM clause */
137015 assert( pThis->pSelect!=0 );
137016 if( pThis->pSelect->selFlags & SF_PushDown ) return 0;
137017 for(pItem = pTabList->a; pItem<pThis; pItem++){
137019 if( pItem->pSelect==0 ) continue;
137020 if( pItem->fg.viaCoroutine ) continue;
137021 if( pItem->zName==0 ) continue;
137022 assert( pItem->pTab!=0 );
137023 assert( pThis->pTab!=0 );
137024 if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue;
137025 if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
137026 pS1 = pItem->pSelect;
137027 if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){
137032 if( pItem->pSelect->selFlags & SF_PushDown ){
137046 sqlite3DbFree(db, p->aCol);
137047 sqlite3DbFree(db, p->aFunc);
137076 if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */
137077 if( p->pEList->nExpr!=1 ) return 0; /* Single result column */
137078 if( p->pWhere ) return 0;
137079 if( p->pGroupBy ) return 0;
137080 pExpr = p->pEList->a[0].pExpr;
137081 if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */
137082 if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */
137083 if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */
137084 if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */
137085 pSub = p->pSrc->a[0].pSelect;
137087 if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */
137089 if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */
137090 if( pSub->pWhere ) return 0; /* No WHERE clause */
137091 if( pSub->pLimit ) return 0; /* No LIMIT clause */
137092 if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */
137093 pSub = pSub->pPrior; /* Repeat over compound */
137098 db = pParse->db;
137101 pSub = p->pSrc->a[0].pSelect;
137102 p->pSrc->a[0].pSelect = 0;
137103 sqlite3SrcListDelete(db, p->pSrc);
137104 p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc));
137107 pPrior = pSub->pPrior;
137108 pSub->pPrior = 0;
137109 pSub->pNext = 0;
137110 pSub->selFlags |= SF_Aggregate;
137111 pSub->selFlags &= ~SF_Compound;
137112 pSub->nSelectRow = 0;
137113 sqlite3ExprListDelete(db, pSub->pEList);
137115 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
137125 p->pEList->a[0].pExpr = pExpr;
137126 p->selFlags &= ~SF_Aggregate;
137130 SELECTTRACE(0x400,pParse,p,("After count-of-view optimization:\n"));
137145 ** encountered, then an appropriate error message is left in
137146 ** pParse->zErrMsg.
137174 db = pParse->db;
137176 if( p==0 || db->mallocFailed || pParse->nErr ){
137181 SELECTTRACE(1,pParse,p, ("begin processing:\n", pParse->addrExplain));
137187 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo );
137188 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo );
137189 assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue );
137190 assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue );
137192 assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
137193 pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard ||
137194 pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo );
137196 if( p->pOrderBy ){
137200 sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY");
137205 p->pOrderBy);
137206 testcase( pParse->earlyCleanup );
137207 p->pOrderBy = 0;
137209 p->selFlags &= ~SF_Distinct;
137210 p->selFlags |= SF_NoopOrderBy;
137213 if( pParse->nErr || db->mallocFailed ){
137216 assert( p->pEList!=0 );
137226 ** In this case, it is an error if the target object (pSrc->a[0]) name
137227 ** or alias is duplicated within FROM clause (pSrc->a[1..n]). */
137228 if( p->selFlags & SF_UpdateFrom ){
137229 SrcItem *p0 = &p->pSrc->a[0];
137230 for(i=1; i<p->pSrc->nSrc; i++){
137231 SrcItem *p1 = &p->pSrc->a[i];
137232 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
137235 p0->zAlias ? p0->zAlias : p0->pTab->zName
137242 if( pDest->eDest==SRT_Output ){
137249 assert( db->mallocFailed || pParse->nErr>0 );
137253 if( p->pWin && (sqlite3SelectTrace & 0x108)!=0 ){
137259 pTabList = p->pSrc;
137260 isAgg = (p->selFlags & SF_Aggregate)!=0;
137262 sSort.pOrderBy = p->pOrderBy;
137268 for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
137269 SrcItem *pItem = &pTabList->a[i];
137270 Select *pSub = pItem->pSelect;
137271 Table *pTab = pItem->pTab;
137281 if( (pItem->fg.jointype & JT_LEFT)!=0
137282 && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor)
137286 ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
137287 pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
137288 unsetJoinExpr(p->pWhere, pItem->iCursor);
137296 if( pTab->nCol!=pSub->pEList->nExpr ){
137298 pTab->nCol, pTab->zName, pSub->pEList->nExpr);
137306 ** will be implemented as a co-routine and there is no advantage to
137309 if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
137310 assert( pSub->pGroupBy==0 );
137315 ** it will be implemented as a co-routine, then do not flatten. This
137330 if( pSub->pOrderBy!=0
137332 && (p->selFlags & SF_ComplexResult)!=0
137333 && (pTabList->nSrc==1
137334 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
137340 if( pParse->nErr ) goto select_end;
137342 i = -1;
137344 pTabList = p->pSrc;
137345 if( db->mallocFailed ) goto select_end;
137347 sSort.pOrderBy = p->pOrderBy;
137356 if( p->pPrior ){
137359 SELECTTRACE(0x1,pParse,p,("end compound-select processing\n"));
137364 if( p->pNext==0 ) ExplainQueryPlanPop(pParse);
137369 /* Do the WHERE-clause constant propagation optimization if this is
137370 ** a join. No need to speed time on this operation for non-join queries
137374 if( pTabList->nSrc>1
137392 if( db->mallocFailed ) goto select_end;
137393 pEList = p->pEList;
137394 pTabList = p->pSrc;
137400 ** (2) Generate code for all sub-queries
137402 for(i=0; i<pTabList->nSrc; i++){
137403 SrcItem *pItem = &pTabList->a[i];
137416 ** SELECT count(*) FROM t1; -- SQLITE_READ t1.""
137417 ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2.""
137424 ** assume the column name is non-NULL and segfault. The use of an empty
137427 if( pItem->colUsed==0 && pItem->zName!=0 ){
137428 sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
137432 /* Generate code for all sub-queries in the FROM clause
137434 pSub = pItem->pSelect;
137443 ** Update 2019-07-24:
137449 testcase( pItem->addrFillSub!=0 );
137454 ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
137458 pParse->nHeight += sqlite3SelectExprHeight(p);
137460 /* Make copies of constant WHERE-clause terms in the outer query down
137464 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes)
137465 && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor,
137466 (pItem->fg.jointype & JT_OUTER)!=0)
137471 ("After WHERE-clause push-down into subquery %d:\n", pSub->selId));
137475 assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 );
137477 SELECTTRACE(0x100,pParse,p,("Push-down not possible\n"));
137480 zSavedAuthContext = pParse->zAuthContext;
137481 pParse->zAuthContext = pItem->zName;
137485 ** The subquery is implemented as a co-routine if:
137490 ** TODO: Are there other reasons beside (1) and (2) to use a co-routine
137494 && (pTabList->nSrc==1
137495 || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0) /* (1) */
137496 && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */
137498 /* Implement a co-routine that will return a single row of the result
137503 pItem->regReturn = ++pParse->nMem;
137504 sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
137505 VdbeComment((v, "%s", pItem->pTab->zName));
137506 pItem->addrFillSub = addrTop;
137507 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
137508 ExplainQueryPlan((pParse, 1, "CO-ROUTINE %u", pSub->selId));
137510 pItem->pTab->nRowLogEst = pSub->nSelectRow;
137511 pItem->fg.viaCoroutine = 1;
137512 pItem->regResult = dest.iSdst;
137513 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
137514 sqlite3VdbeJumpHere(v, addrTop-1);
137516 }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){
137519 ** the make the pItem->iCursor be a copy of the ephemerial table that
137521 CteUse *pCteUse = pItem->u2.pCteUse;
137522 sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e);
137523 if( pItem->iCursor!=pCteUse->iCur ){
137524 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur);
137526 pSub->nSelectRow = pCteUse->nRowEst;
137530 if( pPrior->addrFillSub ){
137531 sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub);
137533 sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor);
137534 pSub->nSelectRow = pPrior->pSelect->nSelectRow;
137543 testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
137544 pItem->regReturn = ++pParse->nMem;
137545 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
137546 pItem->addrFillSub = topAddr+1;
137547 if( pItem->fg.isCorrelated==0 ){
137552 VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
137554 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
137556 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
137557 ExplainQueryPlan((pParse, 1, "MATERIALIZE %u", pSub->selId));
137559 pItem->pTab->nRowLogEst = pSub->nSelectRow;
137561 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
137562 VdbeComment((v, "end %s", pItem->pTab->zName));
137565 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
137566 CteUse *pCteUse = pItem->u2.pCteUse;
137567 pCteUse->addrM9e = pItem->addrFillSub;
137568 pCteUse->regRtn = pItem->regReturn;
137569 pCteUse->iCur = pItem->iCursor;
137570 pCteUse->nRowEst = pSub->nSelectRow;
137573 if( db->mallocFailed ) goto select_end;
137574 pParse->nHeight -= sqlite3SelectExprHeight(p);
137575 pParse->zAuthContext = zSavedAuthContext;
137581 pEList = p->pEList;
137582 pWhere = p->pWhere;
137583 pGroupBy = p->pGroupBy;
137584 pHaving = p->pHaving;
137585 sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
137589 SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
137595 ** if the select-list is the same as the ORDER BY list, then this query
137604 ** The second form is preferred as a single index (or temp-table) may be
137606 ** written the query must use a temp-table for at least one of the ORDER
137607 ** BY and DISTINCT, and an index or separate temp-table for the other.
137609 if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
137610 && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
137612 && p->pWin==0
137615 p->selFlags &= ~SF_Distinct;
137616 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
137617 p->selFlags |= SF_Aggregate;
137618 /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
137633 ** being unused if the data can be extracted in pre-sorted order.
137642 pParse, sSort.pOrderBy, 0, pEList->nExpr);
137643 sSort.iECursor = pParse->nTab++;
137646 sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0,
137650 sSort.addrSortIndex = -1;
137655 if( pDest->eDest==SRT_EphemTab ){
137656 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
137662 if( (p->selFlags & SF_FixedLimit)==0 ){
137663 p->nSelectRow = 320; /* 4 billion rows */
137666 if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
137673 if( p->selFlags & SF_Distinct ){
137674 sDistinct.tabTnct = pParse->nTab++;
137677 (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0),
137688 | (p->selFlags & SF_FixedLimit);
137690 Window *pWin = p->pWin; /* Main window object (or NULL) */
137701 p->pEList, wctrlFlags, p->nSelectRow);
137703 if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
137704 p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
137712 if( sSort.nOBSat==sSort.pOrderBy->nExpr ){
137726 assert( p->pEList==pEList );
137732 int regGosub = ++pParse->nMem;
137738 VdbeNoopComment((v, "inner-loop subroutine"));
137740 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak);
137743 VdbeComment((v, "end inner-loop subroutine"));
137749 selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest,
137781 for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
137782 pItem->u.x.iAlias = 0;
137784 for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
137785 pItem->u.x.iAlias = 0;
137788 if( p->nSelectRow>66 ) p->nSelectRow = 66;
137793 ** in the correct order. It also may not - the GROUP BY might use a
137798 if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){
137801 ** ASC or DESC order - only that each group is returned contiguously.
137805 for(ii=0; ii<pGroupBy->nExpr; ii++){
137806 u8 sortFlags = sSort.pOrderBy->a[ii].sortFlags & KEYINFO_ORDER_DESC;
137807 pGroupBy->a[ii].sortFlags = sortFlags;
137809 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
137815 p->nSelectRow = 0;
137829 testcase( pParse->earlyCleanup );
137831 if( db->mallocFailed ){
137834 pAggInfo->selId = p->selId;
137840 pAggInfo->mnReg = pParse->nMem+1;
137841 pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0;
137842 pAggInfo->pGroupBy = pGroupBy;
137847 assert( pWhere==p->pWhere );
137848 assert( pHaving==p->pHaving );
137849 assert( pGroupBy==p->pGroupBy );
137851 pWhere = p->pWhere;
137855 pAggInfo->nAccumulator = pAggInfo->nColumn;
137856 if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){
137857 minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy);
137861 for(i=0; i<pAggInfo->nFunc; i++){
137862 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
137865 sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
137869 sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
137874 pAggInfo->mxReg = pParse->nMem;
137875 if( db->mallocFailed ) goto select_end;
137885 for(ii=0; ii<pAggInfo->nColumn; ii++){
137886 sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
137887 ii, pAggInfo->aCol[ii].iMem);
137888 sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0);
137890 for(ii=0; ii<pAggInfo->nFunc; ii++){
137891 sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
137892 ii, pAggInfo->aFunc[ii].iMem);
137893 sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0);
137904 int addr1; /* A-vs-B comparision jump */
137918 pAggInfo->sortingIdx = pParse->nTab++;
137920 0, pAggInfo->nColumn);
137922 pAggInfo->sortingIdx, pAggInfo->nSortingColumn,
137927 iUseFlag = ++pParse->nMem;
137928 iAbortFlag = ++pParse->nMem;
137929 regOutputRow = ++pParse->nMem;
137931 regReset = ++pParse->nMem;
137933 iAMem = pParse->nMem + 1;
137934 pParse->nMem += pGroupBy->nExpr;
137935 iBMem = pParse->nMem + 1;
137936 pParse->nMem += pGroupBy->nExpr;
137939 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
137953 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
137971 (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
137975 nGroupBy = pGroupBy->nExpr;
137978 for(i=0; i<pAggInfo->nColumn; i++){
137979 if( pAggInfo->aCol[i].iSorterColumn>=j ){
137987 for(i=0; i<pAggInfo->nColumn; i++){
137988 struct AggInfo_col *pCol = &pAggInfo->aCol[i];
137989 if( pCol->iSorterColumn>=j ){
137992 pCol->pTab, pCol->iTable, pCol->iColumn, r1);
137998 sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord);
138003 pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++;
138006 sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd);
138008 pAggInfo->useSortingIdx = 1;
138015 ** This is an optimization - the correct answer should result regardless.
138032 sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx,
138035 for(j=0; j<pGroupBy->nExpr; j++){
138039 pAggInfo->directMode = 1;
138040 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
138043 sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
138057 sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
138076 sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop);
138095 ** is less than or equal to zero, the subroutine is a no-op. If
138112 selectInnerLoop(pParse, p, -1, &sSort,
138118 /* Generate a subroutine that will reset the group-by accumulator
138143 const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
138144 const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */
138148 Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */
138151 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
138155 ** (2011-04-15) Do not do a full scan of an unordered index.
138157 ** (2013-10-03) Do not count the entries in a partial index.
138163 if( !p->pSrc->a[0].fg.notIndexed ){
138164 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
138165 if( pIdx->bUnordered==0
138166 && pIdx->szIdxRow<pTab->szTabRow
138167 && pIdx->pPartIdxWhere==0
138168 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
138175 iRoot = pBest->tnum;
138179 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
138182 sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
138184 sqlite3VdbeAddOp2(v, OP_Count, iCsr, pAggInfo->aFunc[0].iMem);
138199 if( pAggInfo->nAccumulator ){
138200 for(i=0; i<pAggInfo->nFunc; i++){
138201 if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){
138204 if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){
138208 if( i==pAggInfo->nFunc ){
138209 regAcc = ++pParse->nMem;
138218 assert( p->pGroupBy==0 );
138227 assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
138248 selectInnerLoop(pParse, p, -1, 0, 0,
138265 assert( p->pEList==pEList );
138266 generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
138275 rc = (pParse->nErr>0);
138283 if( pAggInfo && !db->mallocFailed ){
138284 for(i=0; i<pAggInfo->nColumn; i++){
138285 Expr *pExpr = pAggInfo->aCol[i].pCExpr;
138287 assert( pExpr->pAggInfo==pAggInfo );
138288 assert( pExpr->iAgg==i );
138290 for(i=0; i<pAggInfo->nFunc; i++){
138291 Expr *pExpr = pAggInfo->aFunc[i].pFExpr;
138293 assert( pExpr->pAggInfo==pAggInfo );
138294 assert( pExpr->iAgg==i );
138339 char *zErrMsg; /* Error message text, if an error occurs */
138354 int need; /* Slots needed in p->azResult[] */
138358 /* Make sure there is enough space in p->azResult to hold everything
138361 if( p->nRow==0 && argv!=0 ){
138366 if( p->nData + need > p->nAlloc ){
138368 p->nAlloc = p->nAlloc*2 + need;
138369 azNew = sqlite3Realloc( p->azResult, sizeof(char*)*p->nAlloc );
138371 p->azResult = azNew;
138377 if( p->nRow==0 ){
138378 p->nColumn = nCol;
138382 p->azResult[p->nData++] = z;
138384 }else if( (int)p->nColumn!=nCol ){
138385 sqlite3_free(p->zErrMsg);
138386 p->zErrMsg = sqlite3_mprintf(
138389 p->rc = SQLITE_ERROR;
138405 p->azResult[p->nData++] = z;
138407 p->nRow++;
138412 p->rc = SQLITE_NOMEM_BKPT;
138452 db->errCode = SQLITE_NOMEM;
138468 db->errCode = res.rc; /* Assume 32-bit assignment is atomic */
138481 db->errCode = SQLITE_NOMEM;
138500 azResult--;
138533 pTriggerStep = pTriggerStep->pNext;
138535 sqlite3ExprDelete(db, pTmp->pWhere);
138536 sqlite3ExprListDelete(db, pTmp->pExprList);
138537 sqlite3SelectDelete(db, pTmp->pSelect);
138538 sqlite3IdListDelete(db, pTmp->pIdList);
138539 sqlite3UpsertDelete(db, pTmp->pUpsert);
138540 sqlite3SrcListDelete(db, pTmp->pFrom);
138541 sqlite3DbFree(db, pTmp->zSpan);
138552 ** are already attached to pTab->pTrigger. But there might be additional
138554 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
138559 ** pTab as well as the triggers lised in pTab->pTrigger.
138566 if( pParse->disableTriggers ){
138569 pTmpSchema = pParse->db->aDb[1].pSchema;
138570 p = sqliteHashFirst(&pTmpSchema->trigHash);
138572 return pTab->pTrigger;
138574 pList = pTab->pTrigger;
138575 if( pTmpSchema!=pTab->pSchema ){
138578 if( pTrig->pTabSchema==pTab->pSchema
138579 && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
138581 pTrig->pNext = pList;
138583 }else if( pTrig->op==TK_RETURNING ){
138584 assert( pParse->bReturning );
138585 assert( &(pParse->u1.pReturning->retTrig) == pTrig );
138586 pTrig->table = pTab->zName;
138587 pTrig->pTabSchema = pTab->pSchema;
138588 pTrig->pNext = pList;
138601 ** in pParse->pNewTrigger. After the trigger actions have been parsed, the
138620 sqlite3 *db = pParse->db; /* The database connection */
138625 assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */
138631 if( pName2->n>0 ){
138644 if( !pTableName || db->mallocFailed ){
138648 /* A long-standing parser bug is that this syntax was allowed:
138656 if( db->init.busy && iDb!=1 ){
138657 sqlite3DbFree(db, pTableName->a[0].zDatabase);
138658 pTableName->a[0].zDatabase = 0;
138667 if( db->init.busy==0 && pName2->n==0 && pTab
138668 && pTab->pSchema==db->aDb[1].pSchema ){
138673 if( db->mallocFailed ) goto trigger_cleanup;
138674 assert( pTableName->nSrc==1 );
138693 assert( db->mallocFailed );
138696 if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
138701 if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
138705 assert( !db->init.busy );
138713 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
138721 if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
138726 if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
138734 int iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
138736 const char *zDb = db->aDb[iTabDb].zDbSName;
138737 const char *zDbTrig = isTemp ? db->aDb[1].zDbSName : zDb;
138739 if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
138760 pTrigger->zName = zName;
138762 pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
138763 pTrigger->pSchema = db->aDb[iDb].pSchema;
138764 pTrigger->pTabSchema = pTab->pSchema;
138765 pTrigger->op = (u8)op;
138766 pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
138768 sqlite3RenameTokenRemap(pParse, pTrigger->table, pTableName->a[0].zName);
138769 pTrigger->pWhen = pWhen;
138772 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
138774 pTrigger->pColumns = pColumns;
138776 assert( pParse->pNewTrigger==0 );
138777 pParse->pNewTrigger = pTrigger;
138784 if( !pParse->pNewTrigger ){
138787 assert( pParse->pNewTrigger==pTrigger );
138792 if( db->init.iDb==1 ){
138795 ** dropped too. But if a TEMP trigger is created on a non-TEMP table
138799 ** "orphaned trigger" - a trigger whose associated table is missing.
138801 ** 2020-11-05 see also https://sqlite.org/forum/forumpost/157dc791df
138803 db->init.orphanTrigger = 1;
138817 Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */
138819 sqlite3 *db = pParse->db; /* The database */
138824 pParse->pNewTrigger = 0;
138825 if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
138826 zName = pTrig->zName;
138827 iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
138828 pTrig->step_list = pStepList;
138830 pStepList->pTrig = pTrig;
138831 pStepList = pStepList->pNext;
138833 sqlite3TokenInit(&nameToken, pTrig->zName);
138835 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
138836 || sqlite3FixExpr(&sFix, pTrig->pWhen)
138843 assert( !db->init.busy );
138844 pParse->pNewTrigger = pTrig;
138852 if( !db->init.busy ){
138860 z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
138865 db->aDb[iDb].zDbSName, zName,
138866 pTrig->table, z);
138873 if( db->init.busy ){
138875 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
138881 }else if( pLink->pSchema==pLink->pTabSchema ){
138883 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
138885 pLink->pNext = pTab->pTrigger;
138886 pTab->pTrigger = pLink;
138892 assert( IN_RENAME_OBJECT || !pParse->pNewTrigger );
138925 pTriggerStep->op = TK_SELECT;
138926 pTriggerStep->pSelect = pSelect;
138927 pTriggerStep->orconf = OE_Default;
138928 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
138936 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
138945 sqlite3 *db = pParse->db;
138948 pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1);
138951 memcpy(z, pName->z, pName->n);
138953 pTriggerStep->zTarget = z;
138954 pTriggerStep->op = op;
138955 pTriggerStep->zSpan = triggerSpanDup(db, zStart, zEnd);
138957 sqlite3RenameTokenMap(pParse, pTriggerStep->zTarget, pName);
138980 sqlite3 *db = pParse->db;
138983 assert(pSelect != 0 || db->mallocFailed);
138988 pTriggerStep->pSelect = pSelect;
138991 pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
138993 pTriggerStep->pIdList = pColumn;
138994 pTriggerStep->pUpsert = pUpsert;
138995 pTriggerStep->orconf = orconf;
138997 sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget);
139025 sqlite3 *db = pParse->db;
139031 pTriggerStep->pExprList = pEList;
139032 pTriggerStep->pWhere = pWhere;
139033 pTriggerStep->pFrom = pFrom;
139038 pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
139039 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
139040 pTriggerStep->pFrom = sqlite3SrcListDup(db, pFrom, EXPRDUP_REDUCE);
139042 pTriggerStep->orconf = orconf;
139062 sqlite3 *db = pParse->db;
139068 pTriggerStep->pWhere = pWhere;
139071 pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
139073 pTriggerStep->orconf = OE_Default;
139083 if( pTrigger==0 || pTrigger->bReturning ) return;
139084 sqlite3DeleteTriggerStep(db, pTrigger->step_list);
139085 sqlite3DbFree(db, pTrigger->zName);
139086 sqlite3DbFree(db, pTrigger->table);
139087 sqlite3ExprDelete(db, pTrigger->pWhen);
139088 sqlite3IdListDelete(db, pTrigger->pColumns);
139105 sqlite3 *db = pParse->db;
139107 if( db->mallocFailed ) goto drop_trigger_cleanup;
139112 assert( pName->nSrc==1 );
139113 zDb = pName->a[0].zDatabase;
139114 zName = pName->a[0].zName;
139116 for(i=OMIT_TEMPDB; i<db->nDb; i++){
139120 pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName);
139129 pParse->checkSchema = 1;
139143 return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table);
139153 sqlite3 *db = pParse->db;
139156 iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
139157 assert( iDb>=0 && iDb<db->nDb );
139159 assert( (pTable && pTable->pSchema==pTrigger->pSchema) || iDb==1 );
139163 const char *zDb = db->aDb[iDb].zDbSName;
139166 if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
139178 db->aDb[iDb].zDbSName, pTrigger->zName
139181 sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
139193 pHash = &(db->aDb[iDb].pSchema->trigHash);
139196 if( pTrigger->pSchema==pTrigger->pTabSchema ){
139200 for(pp=&pTab->pTrigger; *pp; pp=&((*pp)->pNext)){
139202 *pp = (*pp)->pNext;
139209 db->mDbFlags |= DBFLAG_SchemaChange;
139225 for(e=0; e<pEList->nExpr; e++){
139226 if( sqlite3IdListIndex(pIdList, pEList->a[e].zEName)>=0 ) return 1;
139250 || (pList->bReturning && pList->pNext==0) );
139253 if( (pParse->db->flags & SQLITE_EnableTrigger)==0
139254 && pTab->pTrigger!=0
139259 if( pList==pTab->pTrigger ){
139263 while( ALWAYS(p->pNext) && p->pNext!=pTab->pTrigger ) p = p->pNext;
139264 p->pNext = 0;
139268 if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
139269 mask |= p->tr_tm;
139270 }else if( p->op==TK_RETURNING ){
139274 p->op = op;
139281 p->tr_tm = TRIGGER_BEFORE;
139283 p->tr_tm = TRIGGER_AFTER;
139285 mask |= p->tr_tm;
139286 }else if( p->bReturning && p->op==TK_INSERT && op==TK_UPDATE
139289 mask |= p->tr_tm;
139291 p = p->pNext;
139302 ** Convert the pStep->zTarget string into a SrcList and return a pointer
139315 sqlite3 *db = pParse->db;
139317 char *zName = sqlite3DbStrDup(db, pStep->zTarget);
139319 assert( pSrc==0 || pSrc->nSrc==1 );
139322 Schema *pSchema = pStep->pTrig->pSchema;
139323 pSrc->a[0].zName = zName;
139324 if( pSchema!=db->aDb[1].pSchema ){
139325 pSrc->a[0].pSchema = pSchema;
139327 if( pStep->pFrom ){
139328 SrcList *pDup = sqlite3SrcListDup(db, pStep->pFrom, 0);
139347 if( pTerm->op==TK_ASTERISK ) return 1;
139348 if( pTerm->op!=TK_DOT ) return 0;
139349 assert( pTerm->pRight!=0 );
139350 assert( pTerm->pLeft!=0 );
139351 if( pTerm->pRight->op!=TK_ASTERISK ) return 0;
139368 sqlite3 *db = pParse->db;
139371 for(i=0; i<pList->nExpr; i++){
139372 Expr *pOldExpr = pList->a[i].pExpr;
139376 for(jj=0; jj<pTab->nCol; jj++){
139378 if( IsHiddenColumn(pTab->aCol+jj) ) continue;
139379 pNewExpr = sqlite3Expr(db, TK_ID, pTab->aCol[jj].zName);
139381 if( !db->mallocFailed ){
139382 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
139383 pItem->zEName = sqlite3DbStrDup(db, pTab->aCol[jj].zName);
139384 pItem->eEName = ENAME_NAME;
139390 if( !db->mallocFailed && ALWAYS(pList->a[i].zEName!=0) ){
139391 struct ExprList_item *pItem = &pNew->a[pNew->nExpr-1];
139392 pItem->zEName = sqlite3DbStrDup(db, pList->a[i].zEName);
139393 pItem->eEName = pList->a[i].eEName;
139397 if( !db->mallocFailed ){
139398 Vdbe *v = pParse->pVdbe;
139400 sqlite3VdbeSetNumCols(v, pNew->nExpr);
139401 for(i=0; i<pNew->nExpr; i++){
139402 sqlite3VdbeSetColName(v, i, COLNAME_NAME, pNew->a[i].zEName,
139412 ** is generated in-line.
139420 Vdbe *v = pParse->pVdbe;
139425 assert( pParse->bReturning );
139426 pReturning = pParse->u1.pReturning;
139427 assert( pTrigger == &(pReturning->retTrig) );
139428 pNew = sqlite3ExpandReturning(pParse, pReturning->pReturnEL, pTab);
139432 if( pReturning->nRetCol==0 ){
139433 pReturning->nRetCol = pNew->nExpr;
139434 pReturning->iRetCur = pParse->nTab++;
139439 pParse->eTriggerOp = pTrigger->op;
139440 pParse->pTriggerTab = pTab;
139443 int nCol = pNew->nExpr;
139444 int reg = pParse->nMem+1;
139445 pParse->nMem += nCol+2;
139446 pReturning->iRetReg = reg;
139448 sqlite3ExprCodeFactorable(pParse, pNew->a[i].pExpr, reg+i);
139451 sqlite3VdbeAddOp2(v, OP_NewRowid, pReturning->iRetCur, reg+i+1);
139452 sqlite3VdbeAddOp3(v, OP_Insert, pReturning->iRetCur, reg+i, reg+i+1);
139454 sqlite3ExprListDelete(pParse->db, pNew);
139455 pParse->eTriggerOp = 0;
139456 pParse->pTriggerTab = 0;
139472 Vdbe *v = pParse->pVdbe;
139473 sqlite3 *db = pParse->db;
139475 assert( pParse->pTriggerTab && pParse->pToplevel );
139478 for(pStep=pStepList; pStep; pStep=pStep->pNext){
139489 ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy
139490 ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy
139492 pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
139493 assert( pParse->okConstFactor==0 );
139496 if( pStep->zSpan ){
139498 sqlite3MPrintf(db, "-- %s", pStep->zSpan),
139503 switch( pStep->op ){
139507 sqlite3ExprListDup(db, pStep->pExprList, 0),
139508 sqlite3ExprDup(db, pStep->pWhere, 0),
139509 pParse->eOrconf, 0, 0, 0
139517 sqlite3SelectDup(db, pStep->pSelect, 0),
139518 sqlite3IdListDup(db, pStep->pIdList),
139519 pParse->eOrconf,
139520 sqlite3UpsertDup(db, pStep->pUpsert)
139528 sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
139533 default: assert( pStep->op==TK_SELECT ); {
139535 Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
139566 ** Parse context structure pFrom has just been used to create a sub-vdbe
139571 assert( pFrom->zErrMsg==0 || pFrom->nErr );
139572 assert( pTo->zErrMsg==0 || pTo->nErr );
139573 if( pTo->nErr==0 ){
139574 pTo->zErrMsg = pFrom->zErrMsg;
139575 pTo->nErr = pFrom->nErr;
139576 pTo->rc = pFrom->rc;
139578 sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
139583 ** Create and populate a new TriggerPrg object with a sub-program
139593 sqlite3 *db = pParse->db; /* Database handle */
139597 NameContext sNC; /* Name context for sub-vdbe */
139598 SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */
139599 Parse *pSubParse; /* Parse context for sub-vdbe */
139602 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
139603 assert( pTop->pVdbe );
139607 ** list of the top-level Parse object sooner rather than later. */
139610 pPrg->pNext = pTop->pTriggerPrg;
139611 pTop->pTriggerPrg = pPrg;
139612 pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
139614 sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
139615 pPrg->pTrigger = pTrigger;
139616 pPrg->orconf = orconf;
139617 pPrg->aColmask[0] = 0xffffffff;
139618 pPrg->aColmask[1] = 0xffffffff;
139621 ** trigger sub-program. */
139626 pSubParse->db = db;
139627 pSubParse->pTriggerTab = pTab;
139628 pSubParse->pToplevel = pTop;
139629 pSubParse->zAuthContext = pTrigger->zName;
139630 pSubParse->eTriggerOp = pTrigger->op;
139631 pSubParse->nQueryLoop = pParse->nQueryLoop;
139632 pSubParse->disableVtab = pParse->disableVtab;
139637 pTrigger->zName, onErrorText(orconf),
139638 (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
139639 (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
139640 (pTrigger->op==TK_INSERT ? "INSERT" : ""),
139641 (pTrigger->op==TK_DELETE ? "DELETE" : ""),
139642 pTab->zName
139645 if( pTrigger->zName ){
139646 sqlite3VdbeChangeP4(v, -1,
139647 sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
139653 ** (or NULL) the sub-vdbe is immediately halted by jumping to the
139655 if( pTrigger->pWhen ){
139656 pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
139658 && db->mallocFailed==0
139666 /* Code the trigger program into the sub-vdbe. */
139667 codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
139669 /* Insert an OP_Halt at the end of the sub-program. */
139674 VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
139677 if( db->mallocFailed==0 && pParse->nErr==0 ){
139678 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
139680 pProgram->nMem = pSubParse->nMem;
139681 pProgram->nCsr = pSubParse->nTab;
139682 pProgram->token = (void *)pTrigger;
139683 pPrg->aColmask[0] = pSubParse->oldmask;
139684 pPrg->aColmask[1] = pSubParse->newmask;
139688 assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
139696 ** Return a pointer to a TriggerPrg object containing the sub-program for
139710 assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
139716 for(pPrg=pRoot->pTriggerPrg;
139717 pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
139718 pPrg=pPrg->pNext
139746 assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
139749 ** is a pointer to the sub-vdbe containing the trigger program. */
139751 int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
139753 sqlite3VdbeAddOp4(v, OP_Program, reg, ignoreJump, ++pParse->nMem,
139754 (const char *)pPrg->pProgram, P4_SUBPROGRAM);
139756 (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
139758 /* Set the P5 operand of the OP_Program instruction to non-zero if
139760 ** invocation is disallowed if (a) the sub-program is really a trigger,
139775 ** operation on pTab, this function is a no-op.
139780 ** (a copy of pTab->nCol), then registers are populated as follows:
139783 ** ------------------------------------------------------
139785 ** reg+1 OLD.* value of left-most column of pTab
139787 ** reg+N OLD.* value of right-most column of pTab
139789 ** reg+N+2 NEW.* value of left-most column of pTab
139791 ** reg+N+N+1 NEW.* value of right-most column of pTab
139824 for(p=pTrigger; p; p=p->pNext){
139829 assert( p->pSchema!=0 );
139830 assert( p->pTabSchema!=0 );
139831 assert( p->pSchema==p->pTabSchema
139832 || p->pSchema==pParse->db->aDb[1].pSchema );
139839 if( (p->op==op || (p->bReturning && p->op==TK_INSERT && op==TK_UPDATE))
139840 && p->tr_tm==tr_tm
139841 && checkColumnOverlap(p->pColumns, pChanges)
139843 if( !p->bReturning ){
139853 ** Triggers may access values stored in the old.* or new.* pseudo-table.
139854 ** This function returns a 32-bit bitmask indicating which columns of the
139859 ** Bit 0 of the returned mask is set if the left-most column of the
139891 for(p=pTrigger; p; p=p->pNext){
139892 if( p->op==op
139893 && (tr_tm&p->tr_tm)
139894 && checkColumnOverlap(p->pColumns,pChanges)
139896 if( p->bReturning ){
139902 mask |= pPrg->aColmask[isNew];
139947 ** i-th column of table pTab. This routine sets the P4 parameter of the
139953 ** command. If the latter, then the row-records in the table btree on disk
139956 ** If the former, then all row-records are guaranteed to include a value
139970 ** If column as REAL affinity and the table is an ordinary b-tree table
139977 if( !pTab->pSelect ){
139980 Column *pCol = &pTab->aCol[i];
139981 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
139982 assert( i<pTab->nCol );
139983 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
139984 pCol->affinity, &pValue);
139990 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL && !IsVirtual(pTab) ){
139999 ** and false if not. This is an optimization. False-positives are a
140000 ** performance degradation, but false-negatives can result in a corrupt
140003 ** aXRef[j] will be non-negative if column j of the original table is
140013 i16 iIdxCol = pIdx->aiColumn[iCol];
140019 assert( pIdx->aColExpr!=0 );
140020 assert( pIdx->aColExpr->a[iCol].pExpr!=0 );
140021 return sqlite3ExprReferencesUpdatedColumn(pIdx->aColExpr->a[iCol].pExpr,
140029 ** to be unchanged. This is an optimization. False-positives are a
140030 ** performance degradation, but false-negatives can result in a corrupt
140033 ** aXRef[j] will be non-negative if column j of the original table is
140042 if( pIdx->pPartIdxWhere==0 ) return 0;
140043 return sqlite3ExprReferencesUpdatedColumn(pIdx->pPartIdxWhere,
140051 ** table in the source-list (pSrc->a[0]).
140055 if( pRet ) pRet->iColumn = iCol+1;
140063 ** SELECT <other-columns>, pChanges FROM pTabList WHERE pWhere
140071 ** SELECT <other-columns>, pChanges FROM pTabList
140073 ** GROUP BY <other-columns>
140079 ** the <other-columns> in the query above are is determined by the type
140080 ** of table pTabList->a[0].pTab.
140083 ** PRIMARY KEY. In this case <other-columns> are the primary key columns
140087 ** If the table is actually a view, then <other-columns> are all columns of
140091 ** If the table is a virtual or ordinary intkey table, then <other-columns>
140094 ** rowid value in <other-columns> is used as the integer key, and the
140114 sqlite3 *db = pParse->db;
140115 Table *pTab = pTabList->a[0].pTab;
140135 assert( pTabList->nSrc>1 );
140137 pSrc->a[0].iCursor = -1;
140138 pSrc->a[0].pTab->nTabRef--;
140139 pSrc->a[0].pTab = 0;
140142 for(i=0; i<pPk->nKeyCol; i++){
140143 Expr *pNew = exprRowColumn(pParse, pPk->aiColumn[i]);
140152 }else if( pTab->pSelect ){
140153 for(i=0; i<pTab->nCol; i++){
140167 for(i=0; i<pChanges->nExpr; i++){
140169 sqlite3ExprDup(db, pChanges->a[i].pExpr, 0)
140177 dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
140215 int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the
140216 ** an expression for the i-th column of the table.
140217 ** aXRef[i]==-1 if the i-th column is not changed. */
140223 int iRowidExpr = -1; /* Index of "rowid=" (or IPK) assignment in pChanges */
140225 NameContext sNC; /* The name-context to resolve expressions in */
140247 int nChangeFrom = 0; /* If there is a FROM, pChanges->nExpr, else 0 */
140259 db = pParse->db;
140260 if( pParse->nErr || db->mallocFailed ){
140268 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
140275 isView = pTab->pSelect!=0;
140288 ** in the change-list. Otherwise, set it to 0. There cannot be a FROM
140291 nChangeFrom = (pTabList->nSrc>1) ? pChanges->nExpr : 0;
140316 iBaseCur = iDataCur = pParse->nTab++;
140319 testcase( pPk!=0 && pPk!=pTab->pIndex );
140320 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
140322 iDataCur = pParse->nTab;
140324 pParse->nTab++;
140328 iDataCur = pUpsert->iDataCur;
140329 iIdxCur = pUpsert->iIdxCur;
140330 pParse->nTab = iBaseCur;
140332 pTabList->a[0].iCursor = iDataCur;
140337 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
140339 aRegIdx = aXRef+pTab->nCol;
140343 for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
140345 /* Initialize the name-context */
140363 for(i=0; i<pChanges->nExpr; i++){
140366 if( nChangeFrom==0 && sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
140369 for(j=0; j<pTab->nCol; j++){
140370 if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zEName)==0 ){
140371 if( j==pTab->iPKey ){
140373 pRowidExpr = pChanges->a[i].pExpr;
140375 }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
140379 else if( pTab->aCol[j].colFlags & COLFLAG_GENERATED ){
140380 testcase( pTab->aCol[j].colFlags & COLFLAG_VIRTUAL );
140381 testcase( pTab->aCol[j].colFlags & COLFLAG_STORED );
140384 pTab->aCol[j].zName);
140392 if( j>=pTab->nCol ){
140393 if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zEName) ){
140394 j = -1;
140396 pRowidExpr = pChanges->a[i].pExpr;
140399 sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zEName);
140400 pParse->checkSchema = 1;
140407 rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
140408 j<0 ? "ROWID" : pTab->aCol[j].zName,
140409 db->aDb[iDb].zDbSName);
140413 aXRef[j] = -1;
140427 ** is non-negative, so the value of aXRef[] for generated columns can be
140428 ** set to any non-negative number. We use 99999 so that the value is
140431 if( pTab->tabFlags & TF_HasGenerated ){
140433 testcase( pTab->tabFlags & TF_HasVirtual );
140434 testcase( pTab->tabFlags & TF_HasStored );
140437 for(i=0; i<pTab->nCol; i++){
140439 if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 ) continue;
140440 if( sqlite3ExprReferencesUpdatedColumn(pTab->aCol[i].pDflt,
140455 pTabList->a[0].colUsed = IsVirtual(pTab) ? ALLBITS : 0;
140464 for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
140469 reg = ++pParse->nMem;
140470 pParse->nMem += pIdx->nColumn;
140473 for(i=0; i<pIdx->nKeyCol; i++){
140475 reg = ++pParse->nMem;
140476 pParse->nMem += pIdx->nColumn;
140477 if( onError==OE_Default && pIdx->onError==OE_Replace ){
140487 aRegIdx[nAllIdx] = ++pParse->nMem; /* Register storing the table record */
140494 if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
140503 ** two-pass update algorithm. */
140504 assert( aRegIdx[nAllIdx]==pParse->nMem );
140506 regOldRowid = regNewRowid = ++pParse->nMem;
140508 regOld = pParse->nMem + 1;
140509 pParse->nMem += pTab->nCol;
140512 regNewRowid = ++pParse->nMem;
140514 regNew = pParse->nMem + 1;
140515 pParse->nMem += pTab->nCol;
140520 sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
140557 if( (db->flags&SQLITE_CountRows)!=0
140558 && !pParse->pTriggerTab
140559 && !pParse->nested
140560 && !pParse->bReturning
140563 regRowCount = ++pParse->nMem;
140569 iEph = pParse->nTab++;
140573 nPk = pPk ? pPk->nKeyCol : 0;
140574 iPk = pParse->nMem+1;
140575 pParse->nMem += nPk;
140576 pParse->nMem += nChangeFrom;
140577 regKey = ++pParse->nMem;
140579 int nEphCol = nPk + nChangeFrom + (isView ? pTab->nCol : 0);
140580 iEph = pParse->nTab++;
140581 if( pPk ) sqlite3VdbeAddOp3(v, OP_Null, 0, iPk, iPk+nPk-1);
140586 pKeyInfo->nAllField = nEphCol;
140620 ** Do not consider a single-pass strategy for a multi-row update if
140624 ** or index, causing a single-pass approach to malfunction. */
140626 if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){
140632 /* A one-pass strategy that might update more than one row may not
140648 if( iCur>=0 && iCur!=iDataCur && aToOpen[iCur-iBaseCur] ){
140658 ** mode, write the rowid into the FIFO. In either of the one-pass modes,
140662 aRegIdx[nAllIdx] = ++pParse->nMem;
140674 assert( pPk->aiColumn[i]>=0 );
140676 pPk->aiColumn[i], iPk+i);
140700 if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
140701 if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
140704 if( eOnePass==ONEPASS_MULTI && (nIdx-(aiCurOnePass[1]>=0))>0 ){
140777 /* Compute the old pre-UPDATE content of the row being changed, if that
140784 for(i=0; i<pTab->nCol; i++){
140785 u32 colFlags = pTab->aCol[i].colFlags;
140818 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
140819 if( i==pTab->iPKey ){
140821 }else if( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)!=0 ){
140822 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
140827 int nOff = (isView ? pTab->nCol : nPk);
140831 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, k);
140849 if( pTab->tabFlags & TF_HasGenerated ){
140850 testcase( pTab->tabFlags & TF_HasVirtual );
140851 testcase( pTab->tabFlags & TF_HasStored );
140865 /* The row-trigger may have deleted the row being updated. In this
140867 ** required. This behavior - what happens when the row being updated
140868 ** is deleted or renamed by a BEFORE trigger - is left undefined in the
140879 /* After-BEFORE-trigger-reload-loop:
140885 ** BEFORE trigger runs. See test case trigger1-18.0 (added 2018-04-26)
140888 for(i=0, k=regNew; i<pTab->nCol; i++, k++){
140889 if( pTab->aCol[i].colFlags & COLFLAG_GENERATED ){
140890 if( pTab->aCol[i].colFlags & COLFLAG_VIRTUAL ) k--;
140891 }else if( aXRef[i]<0 && i!=pTab->iPKey ){
140896 if( pTab->tabFlags & TF_HasGenerated ){
140897 testcase( pTab->tabFlags & TF_HasVirtual );
140898 testcase( pTab->tabFlags & TF_HasStored );
140930 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx, -1);
140943 ** to invoke the pre-update hook.
140946 ** pre-update hook. If the caller invokes preupdate_new(), the returned
140960 if( !pParse->nested ){
141001 /* Nothing to do at end-of-loop for a single-pass */
141015 if( pParse->nested==0 && pParse->pTriggerTab==0 && pUpsert==0 ){
141055 ** There are two possible strategies - the default and the special
141083 Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */
141086 sqlite3 *db = pParse->db; /* Database connection */
141089 int nArg = 2 + pTab->nCol; /* Number of arguments to VUpdate */
141093 int iCsr = pSrc->a[0].iCursor; /* Cursor used for virtual table scan */
141102 ephemTab = pParse->nTab++;
141104 regArg = pParse->nMem + 1;
141105 pParse->nMem += nArg;
141106 if( pSrc->nSrc>1 ){
141120 assert( pPk->nKeyCol==1 );
141121 iPk = pPk->aiColumn[0];
141123 pRow = sqlite3ExprDup(db, pChanges->a[aXRef[iPk]].pExpr, 0);
141130 for(i=0; i<pTab->nCol; i++){
141133 sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0)
141144 regRec = ++pParse->nMem;
141145 regRowid = ++pParse->nMem;
141152 for(i=0; i<pTab->nCol; i++){
141153 assert( (pTab->aCol[i].colFlags & COLFLAG_GENERATED)==0 );
141155 sqlite3ExprCode(pParse, pChanges->a[aXRef[i]].pExpr, regArg+2+i);
141173 assert( pPk->nKeyCol==1 );
141174 iPk = pPk->aiColumn[0];
141185 /* If using the onepass strategy, no-op out the OP_OpenEphemeral coded
141196 ** accept no-change records with serial_type 10 */
141207 if( pSrc->nSrc==1 ){
141240 ** 2018-04-12
141261 Upsert *pNext = p->pNextUpsert;
141262 sqlite3ExprListDelete(db, p->pUpsertTarget);
141263 sqlite3ExprDelete(db, p->pUpsertTargetWhere);
141264 sqlite3ExprListDelete(db, p->pUpsertSet);
141265 sqlite3ExprDelete(db, p->pUpsertWhere);
141266 sqlite3DbFree(db, p->pToFree);
141282 sqlite3ExprListDup(db, p->pUpsertTarget, 0),
141283 sqlite3ExprDup(db, p->pUpsertTargetWhere, 0),
141284 sqlite3ExprListDup(db, p->pUpsertSet, 0),
141285 sqlite3ExprDup(db, p->pUpsertWhere, 0),
141286 sqlite3UpsertDup(db, p->pNextUpsert)
141311 pNew->pUpsertTarget = pTarget;
141312 pNew->pUpsertTargetWhere = pTargetWhere;
141313 pNew->pUpsertSet = pSet;
141314 pNew->pUpsertWhere = pWhere;
141315 pNew->isDoUpdate = pSet!=0;
141316 pNew->pNextUpsert = pNext;
141323 ** symbols in the conflict-target.
141337 ExprList *pTarget; /* The conflict-target clause */
141338 Expr *pTerm; /* One term of the conflict-target clause */
141343 assert( pTabList->nSrc==1 );
141344 assert( pTabList->a[0].pTab!=0 );
141346 assert( pUpsert->pUpsertTarget!=0 );
141348 /* Resolve all symbolic names in the conflict-target clause, which
141349 ** includes both the list of columns and the optional partial-index
141355 for(; pUpsert && pUpsert->pUpsertTarget;
141356 pUpsert=pUpsert->pNextUpsert, nClause++){
141357 rc = sqlite3ResolveExprListNames(&sNC, pUpsert->pUpsertTarget);
141359 rc = sqlite3ResolveExprNames(&sNC, pUpsert->pUpsertTargetWhere);
141363 pTab = pTabList->a[0].pTab;
141364 pTarget = pUpsert->pUpsertTarget;
141365 iCursor = pTabList->a[0].iCursor;
141367 && pTarget->nExpr==1
141368 && (pTerm = pTarget->a[0].pExpr)->op==TK_COLUMN
141369 && pTerm->iColumn==XN_ROWID
141371 /* The conflict-target is the rowid of the primary table */
141372 assert( pUpsert->pUpsertIdx==0 );
141380 ** prior to comparing against the conflict-target expression.
141386 sCol[1].iTable = pTabList->a[0].iCursor;
141389 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
141392 if( pTarget->nExpr!=pIdx->nKeyCol ) continue;
141393 if( pIdx->pPartIdxWhere ){
141394 if( pUpsert->pUpsertTargetWhere==0 ) continue;
141395 if( sqlite3ExprCompare(pParse, pUpsert->pUpsertTargetWhere,
141396 pIdx->pPartIdxWhere, iCursor)!=0 ){
141400 nn = pIdx->nKeyCol;
141403 sCol[0].u.zToken = (char*)pIdx->azColl[ii];
141404 if( pIdx->aiColumn[ii]==XN_EXPR ){
141405 assert( pIdx->aColExpr!=0 );
141406 assert( pIdx->aColExpr->nExpr>ii );
141407 pExpr = pIdx->aColExpr->a[ii].pExpr;
141408 if( pExpr->op!=TK_COLLATE ){
141414 sCol[1].iColumn = pIdx->aiColumn[ii];
141418 if( sqlite3ExprCompare(pParse,pTarget->a[jj].pExpr,pExpr,iCursor)<2 ){
141432 pUpsert->pUpsertIdx = pIdx;
141435 if( pUpsert->pUpsertIdx==0 ){
141437 if( nClause==0 && pUpsert->pNextUpsert==0 ){
141458 pNext = pUpsert->pNextUpsert;
141460 if( pNext->pUpsertTarget==0 ) return 1;
141461 if( pNext->pUpsertIdx==0 ) return 1;
141474 && pUpsert->pUpsertTarget!=0
141475 && pUpsert->pUpsertIdx!=pIdx
141477 pUpsert = pUpsert->pNextUpsert;
141486 ** In this case parameter iCur is a cursor open on the table b-tree that
141492 Parse *pParse, /* The parsing and code-generating context */
141498 Vdbe *v = pParse->pVdbe;
141499 sqlite3 *db = pParse->db;
141507 iDataCur = pUpsert->iDataCur;
141519 int nPk = pPk->nKeyCol;
141520 int iPk = pParse->nMem+1;
141521 pParse->nMem += nPk;
141524 assert( pPk->aiColumn[i]>=0 );
141525 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[i]);
141527 VdbeComment((v, "%s.%s", pIdx->zName,
141528 pTab->aCol[pPk->aiColumn[i]].zName));
141539 /* pUpsert does not own pTop->pUpsertSrc - the outer INSERT statement does.
141541 pSrc = sqlite3SrcListDup(db, pTop->pUpsertSrc, 0);
141543 for(i=0; i<pTab->nCol; i++){
141544 if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
141545 sqlite3VdbeAddOp1(v, OP_RealAffinity, pTop->regData+i);
141548 sqlite3Update(pParse, pSrc, sqlite3ExprListDup(db,pUpsert->pUpsertSet,0),
141549 sqlite3ExprDup(db,pUpsert->pUpsertWhere,0), OE_Abort, 0, 0, pUpsert);
141593 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
141665 if( pParse->nErr ) goto build_vacuum_end;
141674 ** to VACUUM are silently ignored. This is a back-out of a bug fix that
141675 ** occurred on 2016-08-19 (https://www.sqlite.org/src/info/083f9e6270).
141678 iDb = sqlite3FindDb(pParse->db, pNm);
141685 iIntoReg = ++pParse->nMem;
141692 sqlite3ExprDelete(pParse->db, pInto);
141700 char **pzErrMsg, /* Write error message here */
141708 u32 saved_mDbFlags; /* Saved value of db->mDbFlags */
141709 u64 saved_flags; /* Saved value of db->flags */
141710 int saved_nChange; /* Saved value of db->nChange */
141711 int saved_nTotalChange; /* Saved value of db->nTotalChange */
141712 u32 saved_openFlags; /* Saved value of db->openFlags */
141721 if( !db->autoCommit ){
141723 return SQLITE_ERROR; /* IMP: R-12218-18073 */
141725 if( db->nVdbeActive>1 ){
141726 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
141727 return SQLITE_ERROR; /* IMP: R-15610-35227 */
141729 saved_openFlags = db->openFlags;
141732 sqlite3SetString(pzErrMsg, db, "non-text filename");
141736 db->openFlags &= ~SQLITE_OPEN_READONLY;
141737 db->openFlags |= SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE;
141743 ** restored before returning. Then set the writable-schema flag, and
141745 saved_flags = db->flags;
141746 saved_mDbFlags = db->mDbFlags;
141747 saved_nChange = db->nChange;
141748 saved_nTotalChange = db->nTotalChange;
141749 saved_mTrace = db->mTrace;
141750 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
141751 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
141752 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder
141754 db->mTrace = 0;
141756 zDbMain = db->aDb[iDb].zDbSName;
141757 pMain = db->aDb[iDb].pBt;
141766 ** An optimisation would be to use a non-journaled pager.
141774 nDb = db->nDb;
141776 db->openFlags = saved_openFlags;
141778 assert( (db->nDb-1)==nDb );
141779 pDb = &db->aDb[nDb];
141780 assert( strcmp(pDb->zDbSName,"vacuum_db")==0 );
141781 pTemp = pDb->pBt;
141785 if( id->pMethods!=0 && (sqlite3OsFileSize(id, &sz)!=SQLITE_OK || sz>0) ){
141790 db->mDbFlags |= DBFLAG_VacuumInto;
141794 sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size);
141800 ** to ensure that we do not try to change the page-size on a WAL database.
141810 db->nextPagesize = 0;
141814 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
141815 || NEVER(db->mallocFailed)
141822 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
141829 db->init.iDb = nDb; /* force new CREATE statements into vacuum_db */
141843 db->init.iDb = 0;
141856 assert( (db->mDbFlags & DBFLAG_Vacuum)!=0 );
141857 db->mDbFlags &= ~DBFLAG_Vacuum;
141876 ** both transactions are closed by this block - the main database
141929 /* Restore the original value of db->flags */
141930 db->init.iDb = 0;
141931 db->mDbFlags = saved_mDbFlags;
141932 db->flags = saved_flags;
141933 db->nChange = saved_nChange;
141934 db->nTotalChange = saved_nTotalChange;
141935 db->mTrace = saved_mTrace;
141936 sqlite3BtreeSetPageSize(pMain, -1, 0, 1);
141945 db->autoCommit = 1;
141948 sqlite3BtreeClose(pDb->pBt);
141949 pDb->pBt = 0;
141950 pDb->pSchema = 0;
141953 /* This both clears the schemas and reduces the size of the db->aDb[]
142024 pMod->zName = zCopy;
142025 pMod->pModule = pModule;
142026 pMod->pAux = pAux;
142027 pMod->xDestroy = xDestroy;
142028 pMod->pEpoTab = 0;
142029 pMod->nRefModule = 1;
142031 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
142059 sqlite3_mutex_enter(db->mutex);
142063 sqlite3_mutex_leave(db->mutex);
142069 ** External API function used to create a new virtual-table module.
142084 ** External API function used to create a new virtual-table module.
142100 ** External API to drop all virtual-table modules, except those named
142108 for(pThis=sqliteHashFirst(&db->aModule); pThis; pThis=pNext){
142113 for(ii=0; azNames[ii]!=0 && strcmp(azNames[ii],pMod->zName)!=0; ii++){}
142116 createModule(db, pMod->zName, 0, 0, 0);
142126 assert( pMod->nRefModule>0 );
142127 pMod->nRefModule--;
142128 if( pMod->nRefModule==0 ){
142129 if( pMod->xDestroy ){
142130 pMod->xDestroy(pMod->pAux);
142132 assert( pMod->pEpoTab==0 );
142146 pVTab->nRef++;
142151 ** pTab is a pointer to a Table structure representing a virtual-table.
142153 ** this virtual-table, if one has been created, or NULL otherwise.
142158 for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
142163 ** Decrement the ref-count on a virtual table object. When the ref-count
142167 sqlite3 *db = pVTab->db;
142170 assert( pVTab->nRef>0 );
142171 assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
142173 pVTab->nRef--;
142174 if( pVTab->nRef==0 ){
142175 sqlite3_vtab *p = pVTab->pVtab;
142176 sqlite3VtabModuleUnref(pVTab->db, pVTab->pMod);
142178 p->pModule->xDisconnect(p);
142186 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
142189 ** connection db is left in the p->pVTable list.
142193 VTable *pVTable = p->pVTable;
142194 p->pVTable = 0;
142200 ** database connection that may have an entry in the p->pVTable list.
142202 assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
142205 sqlite3 *db2 = pVTable->db;
142206 VTable *pNext = pVTable->pNext;
142210 p->pVTable = pRet;
142211 pRet->pNext = 0;
142213 pVTable->pNext = db2->pDisconnect;
142214 db2->pDisconnect = pVTable;
142226 ** list in p->pVTab. It also decrements the VTable ref count. This is
142229 ** be being used by other shared-cache connections).
142236 assert( sqlite3_mutex_held(db->mutex) );
142238 for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
142239 if( (*ppVTab)->db==db ){
142241 *ppVTab = pVTab->pNext;
142253 ** shared b-tree databases opened using connection db are held by the
142263 ** or, if the virtual table is stored in a non-sharable database, then
142267 ** by multiple threads. It is thread-safe.
142270 VTable *p = db->pDisconnect;
142273 assert( sqlite3_mutex_held(db->mutex) );
142276 db->pDisconnect = 0;
142279 VTable *pNext = p->pNext;
142287 ** Clear any and all virtual-table information from the Table record.
142291 ** Since it is a virtual-table, the Table structure contains a pointer
142301 if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
142302 if( p->azModuleArg ){
142304 for(i=0; i<p->nModuleArg; i++){
142305 if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
142307 sqlite3DbFree(db, p->azModuleArg);
142312 ** Add a new module argument to pTable->azModuleArg[].
142313 ** The string is not copied - the pointer is stored. The
142318 sqlite3_int64 nBytes = sizeof(char *)*(2+pTable->nModuleArg);
142320 sqlite3 *db = pParse->db;
142321 if( pTable->nModuleArg+3>=db->aLimit[SQLITE_LIMIT_COLUMN] ){
142322 sqlite3ErrorMsg(pParse, "too many columns on %s", pTable->zName);
142324 azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
142328 int i = pTable->nModuleArg++;
142331 pTable->azModuleArg = azModuleArg;
142351 pTable = pParse->pNewTable;
142353 assert( 0==pTable->pIndex );
142355 db = pParse->db;
142357 assert( pTable->nModuleArg==0 );
142360 addModuleArgument(pParse, pTable, sqlite3DbStrDup(db, pTable->zName));
142361 assert( (pParse->sNameToken.z==pName2->z && pName2->z!=0)
142362 || (pParse->sNameToken.z==pName1->z && pName2->z==0)
142364 pParse->sNameToken.n = (int)(
142365 &pModuleName->z[pModuleName->n] - pParse->sNameToken.z
142374 if( pTable->azModuleArg ){
142375 int iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
142377 sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
142378 pTable->azModuleArg[0], pParse->db->aDb[iDb].zDbSName);
142385 ** in pParse->zArg[] and appends it to the list of arguments on the
142386 ** virtual table currently under construction in pParse->pTable.
142389 if( pParse->sArg.z && pParse->pNewTable ){
142390 const char *z = (const char*)pParse->sArg.z;
142391 int n = pParse->sArg.n;
142392 sqlite3 *db = pParse->db;
142393 addModuleArgument(pParse, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
142402 Table *pTab = pParse->pNewTable; /* The table being constructed */
142403 sqlite3 *db = pParse->db; /* The database connection */
142407 pParse->sArg.z = 0;
142408 if( pTab->nModuleArg<1 ) return;
142416 if( !db->init.busy ){
142427 pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
142429 zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
142435 ** The VM register number pParse->regRowid holds the rowid of an
142439 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142444 db->aDb[iDb].zDbSName,
142445 pTab->zName,
142446 pTab->zName,
142448 pParse->regRowid
142454 zWhere = sqlite3MPrintf(db, "name=%Q AND sql=%Q", pTab->zName, zStmt);
142458 iReg = ++pParse->nMem;
142459 sqlite3VdbeLoadString(v, iReg, pTab->zName);
142463 /* If we are rereading the sqlite_schema table create the in-memory
142470 Schema *pSchema = pTab->pSchema;
142471 const char *zName = pTab->zName;
142473 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
142479 pParse->pNewTable = 0;
142489 pParse->sArg.z = 0;
142490 pParse->sArg.n = 0;
142498 Token *pArg = &pParse->sArg;
142499 if( pArg->z==0 ){
142500 pArg->z = p->z;
142501 pArg->n = p->n;
142503 assert(pArg->z <= p->z);
142504 pArg->n = (int)(&p->z[p->n] - pArg->z);
142523 const char *const*azArg = (const char *const*)pTab->azModuleArg;
142524 int nArg = pTab->nModuleArg;
142530 /* Check that the virtual-table is not already being initialized */
142531 for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){
142532 if( pCtx->pTab==pTab ){
142534 "vtable constructor called recursively: %s", pTab->zName
142540 zModuleName = sqlite3DbStrDup(db, pTab->zName);
142551 pVTable->db = db;
142552 pVTable->pMod = pMod;
142553 pVTable->eVtabRisk = SQLITE_VTABRISK_Normal;
142555 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
142556 pTab->azModuleArg[1] = db->aDb[iDb].zDbSName;
142559 assert( &db->pVtabCtx );
142563 sCtx.pPrior = db->pVtabCtx;
142565 db->pVtabCtx = &sCtx;
142566 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
142567 db->pVtabCtx = sCtx.pPrior;
142579 }else if( ALWAYS(pVTable->pVtab) ){
142582 memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0]));
142583 pVTable->pVtab->pModule = pMod->pModule;
142584 pMod->nRefModule++;
142585 pVTable->nRef = 1;
142588 *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
142595 ** into the linked list headed by pTab->pVTable. Then loop through the
142599 pVTable->pNext = pTab->pVTable;
142600 pTab->pVTable = pVTable;
142602 for(iCol=0; iCol<pTab->nCol; iCol++){
142603 char *zType = sqlite3ColumnType(&pTab->aCol[iCol], "");
142609 && (i==0 || zType[i-1]==' ')
142622 assert(zType[i-1]==' ');
142623 zType[i-1] = '\0';
142625 pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
142626 pTab->tabFlags |= TF_HasHidden;
142629 pTab->tabFlags |= oooHidden;
142644 ** This call is a no-op if table pTab is not a virtual table.
142647 sqlite3 *db = pParse->db;
142658 zMod = pTab->azModuleArg[0];
142659 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
142662 const char *zModule = pTab->azModuleArg[0];
142667 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
142670 pParse->rc = rc;
142678 ** Grow the db->aVTrans[] array so that there is room for at least one
142679 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
142685 if( (db->nVTrans%ARRAY_INCR)==0 ){
142688 ((sqlite3_int64)db->nVTrans + ARRAY_INCR);
142689 aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
142693 memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
142694 db->aVTrans = aVTrans;
142706 db->aVTrans[db->nVTrans++] = pVTab;
142724 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
142725 assert( pTab && IsVirtual(pTab) && !pTab->pVTable );
142728 zMod = pTab->azModuleArg[0];
142729 pMod = (Module*)sqlite3HashFind(&db->aModule, zMod);
142735 if( pMod==0 || pMod->pModule->xCreate==0 || pMod->pModule->xDestroy==0 ){
142739 rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
142771 sqlite3_mutex_enter(db->mutex);
142772 pCtx = db->pVtabCtx;
142773 if( !pCtx || pCtx->bDeclared ){
142775 sqlite3_mutex_leave(db->mutex);
142778 pTab = pCtx->pTab;
142787 && !db->mallocFailed
142788 && !sParse.pNewTable->pSelect
142791 if( !pTab->aCol ){
142794 pTab->aCol = pNew->aCol;
142795 pTab->nNVCol = pTab->nCol = pNew->nCol;
142796 pTab->tabFlags |= pNew->tabFlags & (TF_WithoutRowid|TF_NoVisibleRowid);
142797 pNew->nCol = 0;
142798 pNew->aCol = 0;
142799 assert( pTab->pIndex==0 );
142802 && pCtx->pVTable->pMod->pModule->xUpdate!=0
142803 && sqlite3PrimaryKeyIndex(pNew)->nKeyCol!=1
142805 /* WITHOUT ROWID virtual tables must either be read-only (xUpdate==0)
142806 ** or else must have a single-column PRIMARY KEY */
142809 pIdx = pNew->pIndex;
142811 assert( pIdx->pNext==0 );
142812 pTab->pIndex = pIdx;
142813 pNew->pIndex = 0;
142814 pIdx->pTable = pTab;
142817 pCtx->bDeclared = 1;
142833 sqlite3_mutex_leave(db->mutex);
142842 ** This call is a no-op if zTab is not a virtual table.
142848 pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zDbSName);
142849 if( pTab!=0 && ALWAYS(pTab->pVTable!=0) ){
142852 for(p=pTab->pVTable; p; p=p->pNext){
142853 assert( p->pVtab );
142854 if( p->pVtab->nRef>0 ){
142859 xDestroy = p->pMod->pModule->xDestroy;
142860 if( xDestroy==0 ) xDestroy = p->pMod->pModule->xDisconnect;
142862 pTab->nTabRef++;
142863 rc = xDestroy(p->pVtab);
142866 assert( pTab->pVTable==p && p->pNext==0 );
142867 p->pVtab = 0;
142868 pTab->pVTable = 0;
142887 if( db->aVTrans ){
142888 VTable **aVTrans = db->aVTrans;
142889 db->aVTrans = 0;
142890 for(i=0; i<db->nVTrans; i++){
142892 sqlite3_vtab *p = pVTab->pVtab;
142895 x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
142898 pVTab->iSavepoint = 0;
142902 db->nVTrans = 0;
142911 ** If an error message is available, leave it in p->zErrMsg.
142916 VTable **aVTrans = db->aVTrans;
142918 db->aVTrans = 0;
142919 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
142921 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
142922 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
142927 db->aVTrans = aVTrans;
142961 /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
142972 pModule = pVTab->pVtab->pModule;
142974 if( pModule->xBegin ){
142978 for(i=0; i<db->nVTrans; i++){
142979 if( db->aVTrans[i]==pVTab ){
142988 rc = pModule->xBegin(pVTab->pVtab);
142990 int iSvpt = db->nStatement + db->nSavepoint;
142992 if( iSvpt && pModule->xSavepoint ){
142993 pVTab->iSavepoint = iSvpt;
142994 rc = pModule->xSavepoint(pVTab->pVtab, iSvpt-1);
143021 assert( iSavepoint>=-1 );
143022 if( db->aVTrans ){
143024 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
143025 VTable *pVTab = db->aVTrans[i];
143026 const sqlite3_module *pMod = pVTab->pMod->pModule;
143027 if( pVTab->pVtab && pMod->iVersion>=2 ){
143032 xMethod = pMod->xSavepoint;
143033 pVTab->iSavepoint = iSavepoint+1;
143036 xMethod = pMod->xRollbackTo;
143039 xMethod = pMod->xRelease;
143042 if( xMethod && pVTab->iSavepoint>iSavepoint ){
143043 rc = xMethod(pVTab->pVtab, iSavepoint);
143081 if( pExpr->op!=TK_COLUMN ) return pDef;
143082 pTab = pExpr->y.pTab;
143085 pVtab = sqlite3GetVTable(db, pTab)->pVtab;
143087 assert( pVtab->pModule!=0 );
143088 pMod = (sqlite3_module *)pVtab->pModule;
143089 if( pMod->xFindFunction==0 ) return pDef;
143095 ** with an all lower-case function name. Continue in this tradition to
143101 for(i=0; pDef->zName[i]; i++){
143102 unsigned char x = (unsigned char)pDef->zName[i];
143107 rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
143115 + sqlite3Strlen30(pDef->zName) + 1);
143120 pNew->zName = (const char*)&pNew[1];
143121 memcpy((char*)&pNew[1], pDef->zName, sqlite3Strlen30(pDef->zName)+1);
143122 pNew->xSFunc = xSFunc;
143123 pNew->pUserData = pArg;
143124 pNew->funcFlags |= SQLITE_FUNC_EPHEM;
143129 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
143132 ** is a no-op.
143140 for(i=0; i<pToplevel->nVtabLock; i++){
143141 if( pTab==pToplevel->apVtabLock[i] ) return;
143143 n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
143144 apVtabLock = sqlite3Realloc(pToplevel->apVtabLock, n);
143146 pToplevel->apVtabLock = apVtabLock;
143147 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
143149 sqlite3OomFault(pToplevel->db);
143156 ** exist. Return non-zero if the eponymous virtual table instance exists
143162 ** instances always exist. They cannot be DROP-ed.
143168 const sqlite3_module *pModule = pMod->pModule;
143172 sqlite3 *db = pParse->db;
143173 if( pMod->pEpoTab ) return 1;
143174 if( pModule->xCreate!=0 && pModule->xCreate!=pModule->xConnect ) return 0;
143177 pTab->zName = sqlite3DbStrDup(db, pMod->zName);
143178 if( pTab->zName==0 ){
143182 pMod->pEpoTab = pTab;
143183 pTab->nTabRef = 1;
143184 pTab->pSchema = db->aDb[0].pSchema;
143185 assert( pTab->nModuleArg==0 );
143186 pTab->iPKey = -1;
143187 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
143189 addModuleArgument(pParse, pTab, sqlite3DbStrDup(db, pTab->zName));
143190 rc = vtabCallConstructor(db, pTab, pMod, pModule->xConnect, &zErr);
143205 Table *pTab = pMod->pEpoTab;
143210 pTab->tabFlags |= TF_Ephemeral;
143212 pMod->pEpoTab = 0;
143232 assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
143233 return (int)aMap[db->vtabOnConflict-1];
143249 sqlite3_mutex_enter(db->mutex);
143250 p = db->pVtabCtx;
143254 assert( p->pTab==0 || IsVirtual(p->pTab) );
143258 p->pVTable->bConstraint = (u8)va_arg(ap, int);
143262 p->pVTable->eVtabRisk = SQLITE_VTABRISK_Low;
143266 p->pVTable->eVtabRisk = SQLITE_VTABRISK_High;
143278 sqlite3_mutex_leave(db->mutex);
143287 ** 2015-06-06
143300 ** This file was split off from where.c on 2015-06-06 in order to reduce the
143309 ** 2013-11-12
143356 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
143364 int addrSkip; /* Jump here for next iteration of skip-scan */
143368 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
143369 int addrBignull; /* Jump here for next part of big-null scan */
143377 union { /* Information that depends on pWLoop->wsFlags */
143383 int iBase; /* Base register of multi-key index record */
143387 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
143401 ** prevent a query solution - which is an error) and many terms of the
143403 ** potential way of implementing that FROM-clause term, together with
143419 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
143460 ** correspond to the subquery(s) of OR-clause processing. Only the
143476 ** vector, not a scalar, and because dependencies are many-to-one, not
143477 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
143493 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
143506 ** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
143537 ** spread out over the non-negative integers. For example, the cursor
143556 int iParent; /* Disable pWC->a[iParent] when this term disabled */
143566 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
143579 #define TERM_OR_OK 0x0040 /* Used during OR-clause processing */
143585 #define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
143606 int k; /* Resume scanning at this->pWC->a[this->k] */
143608 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
143667 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
143668 ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
143675 ** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
143676 ** 57->5, 73->4. Or one of 719 other combinations might be used. It
143690 #define initMaskSet(P) (P)->n=0
143721 ** routine. The limit is high enough that is should not impact real-world
143770 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
143779 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
143793 ** Private interfaces - callable only by other where.c routines.
143837 int iLevel, /* Which level of pWInfo->a[] should be coded */
143859 ** OR-ed combination of these values can be used when searching for
143871 #define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
143872 #define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
143873 #define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
143874 #define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
143878 #define WO_OR 0x0200 /* Two or more OR-connected terms */
143879 #define WO_AND 0x0400 /* Two or more AND-connected terms */
143884 #define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
143899 #define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
143907 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
143912 #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
143922 ** Return the name of the i-th column of the pIdx index.
143925 i = pIdx->aiColumn[i];
143928 return pIdx->pTable->aCol[i].zName;
143943 int iTerm, /* Zero-based index of first term. */
143944 int bAnd, /* Non-zero to append " AND " */
143984 Index *pIndex = pLoop->u.btree.pIndex;
143985 u16 nEq = pLoop->u.btree.nEq;
143986 u16 nSkip = pLoop->nSkip;
143989 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
143998 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
143999 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
144002 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
144003 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
144009 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
144011 ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode
144025 if( sqlite3ParseToplevel(pParse)->explain==2 )
144028 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
144029 Vdbe *v = pParse->pVdbe; /* VM being constructed */
144030 sqlite3 *db = pParse->db; /* Database handle */
144038 pLoop = pLevel->pWLoop;
144039 flags = pLoop->wsFlags;
144043 || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
144048 if( pItem->pSelect ){
144049 sqlite3_str_appendf(&str, " SUBQUERY %u", pItem->pSelect->selId);
144051 sqlite3_str_appendf(&str, " TABLE %s", pItem->zName);
144054 if( pItem->zAlias ){
144055 sqlite3_str_appendf(&str, " AS %s", pItem->zAlias);
144061 assert( pLoop->u.btree.pIndex!=0 );
144062 pIdx = pLoop->u.btree.pIndex;
144064 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
144079 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
144100 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
144104 if( pLoop->nOut>=10 ){
144106 sqlite3LogEstToInt(pLoop->nOut));
144114 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
144137 WhereLoop *pLoop = pLvl->pWLoop;
144138 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){
144139 zObj = pLoop->u.btree.pIndex->zName;
144141 zObj = pSrclist->a[pLvl->iFrom].zName;
144144 v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
144190 ** LIKE-optimization loop, when scanning BLOBs instead of strings.
144195 while( (pTerm->wtFlags & TERM_CODED)==0
144196 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
144197 && (pLevel->notReady & pTerm->prereqAll)==0
144199 if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
144200 pTerm->wtFlags |= TERM_LIKECOND;
144202 pTerm->wtFlags |= TERM_CODED;
144204 if( pTerm->iParent<0 ) break;
144205 pTerm = &pTerm->pWC->a[pTerm->iParent];
144207 pTerm->nChild--;
144208 if( pTerm->nChild!=0 ) break;
144218 ** are no-ops) at the beginning and end of zAff are ignored. If all entries
144225 Vdbe *v = pParse->pVdbe;
144227 assert( pParse->db->mallocFailed );
144237 n--;
144241 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
144242 n--;
144313 sqlite3 *db = pParse->db;
144316 if( db->mallocFailed==0 ){
144317 ExprList *pOrigRhs = pNew->x.pSelect->pEList; /* Original unmodified RHS */
144318 ExprList *pOrigLhs = pNew->pLeft->x.pList; /* Original unmodified LHS */
144324 for(i=iEq; i<pLoop->nLTerm; i++){
144325 if( pLoop->aLTerm[i]->pExpr==pX ){
144326 int iField = pLoop->aLTerm[i]->u.x.iField - 1;
144327 if( pOrigRhs->a[iField].pExpr==0 ) continue; /* Duplicate PK column */
144328 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
144329 pOrigRhs->a[iField].pExpr = 0;
144330 assert( pOrigLhs->a[iField].pExpr!=0 );
144331 pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
144332 pOrigLhs->a[iField].pExpr = 0;
144337 pNew->pLeft->x.pList = pLhs;
144338 pNew->x.pSelect->pEList = pRhs;
144339 if( pLhs && pLhs->nExpr==1 ){
144343 Expr *p = pLhs->a[0].pExpr;
144344 pLhs->a[0].pExpr = 0;
144345 sqlite3ExprDelete(db, pNew->pLeft);
144346 pNew->pLeft = p;
144348 pSelect = pNew->x.pSelect;
144349 if( pSelect->pOrderBy ){
144351 ** iOrderByCol variables. These are set to non-zero when an
144353 ** result-set. Since the result-set of the SELECT statement may
144357 ExprList *pOrderBy = pSelect->pOrderBy;
144358 for(i=0; i<pOrderBy->nExpr; i++){
144359 pOrderBy->a[i].u.x.iOrderByCol = 0;
144386 ** straight-line code. For constraints of the form X IN (...)
144394 int bRev, /* True for reverse-order IN operations */
144397 Expr *pX = pTerm->pExpr;
144398 Vdbe *v = pParse->pVdbe;
144401 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
144403 if( pX->op==TK_EQ || pX->op==TK_IS ){
144404 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
144405 }else if( pX->op==TK_ISNULL ){
144413 WhereLoop *pLoop = pLevel->pWLoop;
144418 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
144419 && pLoop->u.btree.pIndex!=0
144420 && pLoop->u.btree.pIndex->aSortOrder[iEq]
144426 assert( pX->op==TK_IN );
144430 if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
144435 for(i=iEq;i<pLoop->nLTerm; i++){
144436 assert( pLoop->aLTerm[i]!=0 );
144437 if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
144441 if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
144444 sqlite3 *db = pParse->db;
144447 if( !db->mallocFailed ){
144448 aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
144450 pTerm->pExpr->iTable = iTab;
144453 pX = pTerm->pExpr;
144463 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
144465 pLoop->wsFlags |= WHERE_IN_ABLE;
144466 if( pLevel->u.in.nIn==0 ){
144467 pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
144469 if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){
144470 pLoop->wsFlags |= WHERE_IN_EARLYOUT;
144473 i = pLevel->u.in.nIn;
144474 pLevel->u.in.nIn += nEq;
144475 pLevel->u.in.aInLoop =
144476 sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
144477 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
144478 pIn = pLevel->u.in.aInLoop;
144482 for(i=iEq;i<pLoop->nLTerm; i++){
144483 if( pLoop->aLTerm[i]->pExpr==pX ){
144484 int iOut = iReg + i - iEq;
144486 pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut);
144489 pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut);
144493 pIn->iCur = iTab;
144494 pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
144496 pIn->iBase = iReg - i;
144497 pIn->nPrefix = i;
144499 pIn->nPrefix = 0;
144502 pIn->eEndLoopOp = OP_Noop;
144508 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
144509 && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 );
144511 && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0
144513 sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq);
144516 pLevel->u.in.nIn = 0;
144518 sqlite3DbFree(pParse->db, aiMap);
144538 ** of nEq including 0. If nEq==0, this routine is nearly a no-op.
144539 ** The only thing it does is allocate the pLevel->iMem memory cell and
144578 u16 nSkip; /* Number of left-most columns to skip */
144579 Vdbe *v = pParse->pVdbe; /* The vm under construction */
144589 pLoop = pLevel->pWLoop;
144590 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
144591 nEq = pLoop->u.btree.nEq;
144592 nSkip = pLoop->nSkip;
144593 pIdx = pLoop->u.btree.pIndex;
144598 regBase = pParse->nMem + 1;
144599 nReg = pLoop->u.btree.nEq + nExtraReg;
144600 pParse->nMem += nReg;
144602 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
144603 assert( zAff!=0 || pParse->db->mallocFailed );
144606 int iIdxCur = pLevel->iIdxCur;
144610 VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
144612 pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
144619 testcase( pIdx->aiColumn[j]==XN_EXPR );
144629 pTerm = pLoop->aLTerm[j];
144633 testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
144634 testcase( pTerm->wtFlags & TERM_VIRTUAL );
144644 if( pTerm->eOperator & WO_IN ){
144645 if( pTerm->pExpr->flags & EP_xIsSelect ){
144652 }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
144653 Expr *pRight = pTerm->pExpr->pRight;
144654 if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
144655 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
144688 ** becomes a no-op.
144695 if( pTerm->wtFlags & TERM_LIKEOPT ){
144697 assert( pLevel->iLikeRepCntr>0 );
144698 pOp = sqlite3VdbeGetOp(v, -1);
144700 assert( pOp->opcode==OP_String8
144701 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
144702 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
144703 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
144726 ** accessed through the index. If it cannot, then set pWalker->eCode to 1.
144729 struct CCurHint *pHint = pWalker->u.pCCurHint;
144730 assert( pHint->pIdx!=0 );
144731 if( pExpr->op==TK_COLUMN
144732 && pExpr->iTable==pHint->iTabCur
144733 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
144735 pWalker->eCode = 1;
144742 ** should be included in the cursor-hint for a table that is on the rhs
144743 ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
144756 if( pExpr->op==TK_IS
144757 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
144758 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
144760 pWalker->eCode = 1;
144761 }else if( pExpr->op==TK_FUNCTION ){
144764 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
144765 pWalker->eCode = 1;
144792 struct CCurHint *pHint = pWalker->u.pCCurHint;
144793 if( pExpr->op==TK_COLUMN ){
144794 if( pExpr->iTable!=pHint->iTabCur ){
144795 int reg = ++pWalker->pParse->nMem; /* Register for column value */
144796 sqlite3ExprCode(pWalker->pParse, pExpr, reg);
144797 pExpr->op = TK_REGISTER;
144798 pExpr->iTable = reg;
144799 }else if( pHint->pIdx!=0 ){
144800 pExpr->iTable = pHint->iIdxCur;
144801 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
144802 assert( pExpr->iColumn>=0 );
144804 }else if( pExpr->op==TK_AGG_FUNCTION ){
144806 ** be a correlated sub-query, and expression pExpr is an aggregate from
144824 WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */
144826 Parse *pParse = pWInfo->pParse;
144827 sqlite3 *db = pParse->db;
144828 Vdbe *v = pParse->pVdbe;
144830 WhereLoop *pLoop = pLevel->pWLoop;
144839 iCur = pLevel->iTabCur;
144840 assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor );
144842 sHint.iIdxCur = pLevel->iIdxCur;
144843 sHint.pIdx = pLoop->u.btree.pIndex;
144847 pWC = &pWInfo->sWC;
144848 for(i=0; i<pWC->nTerm; i++){
144849 pTerm = &pWC->a[i];
144850 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
144851 if( pTerm->prereqAll & pLevel->notReady ) continue;
144855 ** from the cursor-hint.
144875 if( pTabItem->fg.jointype & JT_LEFT ){
144876 Expr *pExpr = pTerm->pExpr;
144878 || pExpr->iRightJoinTable!=pTabItem->iCursor
144882 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
144886 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
144889 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
144892 if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){
144893 for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){}
144894 if( j<pLoop->nLTerm ) continue;
144897 /* No subqueries or non-deterministic functions allowed */
144898 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
144905 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
144910 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
144921 # define codeCursorHint(A,B,C,D) /* No-op */
144925 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
144934 ** However, if the scan currently being coded is a branch of an OR-loop and
144945 int iCur, /* Cursor for IPK b-tree */
144948 Parse *pParse = pWInfo->pParse; /* Parse context */
144949 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
144952 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
144954 pWInfo->bDeferredSeek = 1;
144956 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
144957 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
144960 Table *pTab = pIdx->pTable;
144961 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
144963 ai[0] = pTab->nCol;
144964 for(i=0; i<pIdx->nColumn-1; i++){
144966 assert( pIdx->aiColumn[i]<pTab->nCol );
144967 x1 = pIdx->aiColumn[i];
144972 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
144990 if( (p->flags & EP_xIsSelect) ){
144991 Vdbe *v = pParse->pVdbe;
144993 assert( p->op==TK_SELECT );
144995 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
145000 ExprList *pList = p->x.pList;
145001 assert( nReg<=pList->nExpr );
145003 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
145031 pNew = sqlite3DbMallocRaw(pTrans->db, sizeof(*pNew));
145033 pNew->pNext = pTrans->pWInfo->pExprMods;
145034 pTrans->pWInfo->pExprMods = pNew;
145035 pNew->pExpr = pExpr;
145036 memcpy(&pNew->orig, pExpr, sizeof(*pExpr));
145046 IdxExprTrans *pX = p->u.pIdxTrans;
145047 if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
145049 pExpr->affExpr = sqlite3ExprAffinity(pExpr);
145050 pExpr->op = TK_COLUMN;
145051 pExpr->iTable = pX->iIdxCur;
145052 pExpr->iColumn = pX->iIdxCol;
145053 pExpr->y.pTab = 0;
145068 if( pExpr->op==TK_COLUMN ){
145069 IdxExprTrans *pX = p->u.pIdxTrans;
145070 if( pExpr->iTable==pX->iTabCur && pExpr->iColumn==pX->iTabCol ){
145071 assert( pExpr->y.pTab!=0 );
145073 pExpr->affExpr = sqlite3TableColumnAffinity(pExpr->y.pTab,pExpr->iColumn);
145074 pExpr->iTable = pX->iIdxCur;
145075 pExpr->iColumn = pX->iIdxCol;
145076 pExpr->y.pTab = 0;
145088 ** 2019-10-24: Updated to also translate references to a VIRTUAL column in
145103 aColExpr = pIdx->aColExpr;
145104 if( aColExpr==0 && !pIdx->bHasVCol ){
145109 pTab = pIdx->pTable;
145115 x.db = pWInfo->pParse->db;
145116 for(iIdxCol=0; iIdxCol<pIdx->nColumn; iIdxCol++){
145117 i16 iRef = pIdx->aiColumn[iIdxCol];
145119 assert( aColExpr->a[iIdxCol].pExpr!=0 );
145120 x.pIdxExpr = aColExpr->a[iIdxCol].pExpr;
145125 && (pTab->aCol[iRef].colFlags & COLFLAG_VIRTUAL)!=0
145126 && (pTab->aCol[iRef].zColl==0
145127 || sqlite3StrICmp(pTab->aCol[iRef].zColl, sqlite3StrBINARY)==0)
145131 ** out of the index is an optimization only - the main table is always
145134 ** the column is non-standard */
145142 sqlite3WalkExpr(&w, pWInfo->pWhere);
145143 sqlite3WalkExprList(&w, pWInfo->pOrderBy);
145144 sqlite3WalkExprList(&w, pWInfo->pResultSet);
145162 while( pTruth->op==TK_AND ){
145163 whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC);
145164 pTruth = pTruth->pRight;
145166 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
145168 if( pTerm->wtFlags & TERM_CODED ) continue;
145169 pExpr = pTerm->pExpr;
145171 pTerm->wtFlags |= TERM_CODED;
145177 ** Generate code for the start of the iLevel-th loop in the WHERE clause
145184 int iLevel, /* Which level of pWInfo->a[] should be coded */
145205 pWC = &pWInfo->sWC;
145206 db = pParse->db;
145207 pLoop = pLevel->pWLoop;
145208 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
145209 iCur = pTabItem->iCursor;
145210 pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
145211 bRev = (pWInfo->revMask>>iLevel)&1;
145212 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
145216 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
145222 sqlite3TreeViewExpr(0, pWInfo->pWhere, 0);
145224 sqlite3DebugPrintf("All WHERE-clause terms before coding:\n");
145239 addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse);
145240 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
145246 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)
145247 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
145249 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
145250 pLevel->iLeftJoin = ++pParse->nMem;
145251 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
145252 VdbeComment((v, "init LEFT JOIN no-match flag"));
145257 for(j=iLevel; j>0 && pWInfo->a[j].iLeftJoin==0; j--){}
145258 addrHalt = pWInfo->a[j].addrBrk;
145260 /* Special case of a FROM clause subquery implemented as a co-routine */
145261 if( pTabItem->fg.viaCoroutine ){
145262 int regYield = pTabItem->regReturn;
145263 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
145264 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
145266 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
145267 pLevel->op = OP_Goto;
145271 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
145272 /* Case 1: The table is a virtual-table. Use the VFilter and VNext
145277 int nConstraint = pLoop->nLTerm;
145281 addrNotFound = pLevel->addrBrk;
145284 pTerm = pLoop->aLTerm[j];
145286 if( pTerm->eOperator & WO_IN ){
145288 addrNotFound = pLevel->addrNxt;
145290 Expr *pRight = pTerm->pExpr->pRight;
145294 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
145297 pLoop->u.vtab.idxStr,
145298 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
145300 pLoop->u.vtab.needFree = 0;
145302 ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */
145303 if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0;
145304 pLevel->p1 = iCur;
145305 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
145306 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
145307 iIn = pLevel->u.in.nIn;
145308 for(j=nConstraint-1; j>=0; j--){
145309 pTerm = pLoop->aLTerm[j];
145310 if( (pTerm->eOperator & WO_IN)!=0 ) iIn--;
145311 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
145313 }else if( (pTerm->eOperator & WO_IN)!=0
145314 && sqlite3ExprVectorSize(pTerm->pExpr->pLeft)==1
145324 assert( pLevel->u.in.aInLoop!=0 || db->mallocFailed );
145325 if( !db->mallocFailed ){
145326 assert( iIn>=0 && iIn<pLevel->u.in.nIn );
145327 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
145328 assert( pOp->opcode==OP_Column || pOp->opcode==OP_Rowid );
145329 assert( pOp->opcode!=OP_Column || pOp->p3==iReg+j+2 );
145330 assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
145331 testcase( pOp->opcode==OP_Rowid );
145332 sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
145338 assert( pCompare!=0 || db->mallocFailed );
145340 pCompare->pLeft = pTerm->pExpr->pLeft;
145341 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
145343 pRight->iTable = iReg+j+2;
145345 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
145348 pCompare->pLeft = 0;
145353 assert( iIn==0 || db->mallocFailed );
145356 ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems
145364 if( (pLoop->wsFlags & WHERE_IPK)!=0
145365 && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
145372 assert( pLoop->u.btree.nEq==1 );
145373 pTerm = pLoop->aLTerm[0];
145375 assert( pTerm->pExpr!=0 );
145376 testcase( pTerm->wtFlags & TERM_VIRTUAL );
145377 iReleaseReg = ++pParse->nMem;
145380 addrNxt = pLevel->addrNxt;
145383 pLevel->op = OP_Noop;
145384 if( (pTerm->prereqAll & pLevel->notReady)==0 ){
145385 pTerm->wtFlags |= TERM_CODED;
145387 }else if( (pLoop->wsFlags & WHERE_IPK)!=0
145388 && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
145399 if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
145400 if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
145426 assert( (pStart->wtFlags & TERM_VNULL)==0 );
145427 testcase( pStart->wtFlags & TERM_VIRTUAL );
145428 pX = pStart->pExpr;
145430 testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
145431 if( sqlite3ExprIsVector(pX->pRight) ){
145433 codeExprOrVector(pParse, pX->pRight, r1, 1);
145434 testcase( pX->op==TK_GT );
145435 testcase( pX->op==TK_GE );
145436 testcase( pX->op==TK_LT );
145437 testcase( pX->op==TK_LE );
145438 op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
145439 assert( pX->op!=TK_GT || op==OP_SeekGE );
145440 assert( pX->op!=TK_GE || op==OP_SeekGE );
145441 assert( pX->op!=TK_LT || op==OP_SeekLE );
145442 assert( pX->op!=TK_LE || op==OP_SeekLE );
145444 r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
145446 op = aMoveOp[(pX->op - TK_GT)];
145450 VdbeCoverageIf(v, pX->op==TK_GT);
145451 VdbeCoverageIf(v, pX->op==TK_LE);
145452 VdbeCoverageIf(v, pX->op==TK_LT);
145453 VdbeCoverageIf(v, pX->op==TK_GE);
145462 pX = pEnd->pExpr;
145464 assert( (pEnd->wtFlags & TERM_VNULL)==0 );
145465 testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
145466 testcase( pEnd->wtFlags & TERM_VIRTUAL );
145467 memEndValue = ++pParse->nMem;
145468 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
145469 if( 0==sqlite3ExprIsVector(pX->pRight)
145470 && (pX->op==TK_LT || pX->op==TK_GT)
145476 if( 0==sqlite3ExprIsVector(pX->pRight) ){
145481 pLevel->op = bRev ? OP_Prev : OP_Next;
145482 pLevel->p1 = iCur;
145483 pLevel->p2 = start;
145484 assert( pLevel->p5==0 );
145486 iRowidReg = ++pParse->nMem;
145495 }else if( pLoop->wsFlags & WHERE_INDEXED ){
145500 ** left-most columns of the index. It may also contain
145503 ** the right-most column can be an inequality - the rest must
145543 u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */
145544 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
145545 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
145561 int regBignull = 0; /* big-null flag register */
145564 pIdx = pLoop->u.btree.pIndex;
145565 iIdxCur = pLevel->iIdxCur;
145566 assert( nEq>=pLoop->nSkip );
145572 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
145573 pRangeStart = pLoop->aLTerm[j++];
145574 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
145576 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
145577 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
145579 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
145580 pRangeEnd = pLoop->aLTerm[j++];
145581 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
145583 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
145585 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
145586 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
145587 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
145589 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
145593 testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
145595 pLevel->iLikeRepCntr <<=1;
145596 pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC);
145600 j = pIdx->aiColumn[nEq];
145601 if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){
145606 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
145609 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
145612 ** it is not. For an ASC sort, the non-NULL entries are scanned first.
145615 if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0
145616 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0
145620 testcase( pLoop->nSkip>0 );
145623 pLevel->regBignull = regBignull = ++pParse->nMem;
145624 if( pLevel->iLeftJoin ){
145627 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
145634 if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
145635 || (bRev && pIdx->nKeyCol==nEq)
145642 if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){
145658 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
145660 testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
145661 testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
145662 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
145663 testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
145664 startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
145665 endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
145671 Expr *pRight = pRangeStart->pExpr->pRight;
145674 if( (pRangeStart->wtFlags & TERM_VNULL)==0
145684 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
145701 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
145702 if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){
145703 /* The skip-scan logic inside the call to codeAllEqualityConstraints()
145709 VdbeComment((v, "NULL-scan pass ctr"));
145714 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
145725 (pIdx->aiRowLogEst[0]+9)/10);
145745 nConstraint-startEq);
145760 Expr *pRight = pRangeEnd->pExpr->pRight;
145763 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
145773 assert( pParse->db->mallocFailed );
145776 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
145794 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
145799 /* Except, skip the end-of-range check while doing the NULL-scan */
145801 VdbeComment((v, "If NULL-scan 2nd pass"));
145813 /* During a NULL-scan, check to see if we have reached the end of
145819 VdbeComment((v, "If NULL-scan 1st pass"));
145830 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
145835 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
145836 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0;
145839 }else if( HasRowid(pIdx->pTable) ){
145842 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
145843 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
145844 for(j=0; j<pPk->nKeyCol; j++){
145845 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
145849 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
145852 if( pLevel->iLeftJoin==0 ){
145865 ** Also, do not do this when processing one index an a multi-index
145870 if( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
145878 ** 2019-11-02 ticket 623eff57e76d45f6: This optimization does not work
145881 if( pIdx->pPartIdxWhere ){
145882 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
145885 testcase( pIdx->pPartIdxWhere );
145887 ** The OR-optimization doesn't work for the right hand table of
145889 assert( (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0 );
145893 if( pLoop->wsFlags & WHERE_ONEROW ){
145894 pLevel->op = OP_Noop;
145896 pLevel->op = OP_Prev;
145898 pLevel->op = OP_Next;
145900 pLevel->p1 = iIdxCur;
145901 pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
145902 if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
145903 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
145905 assert( pLevel->p5==0 );
145911 if( pLoop->wsFlags & WHERE_MULTI_OR ){
145950 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
145955 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
145956 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
145958 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
145960 int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */
145968 Table *pTab = pTabItem->pTab;
145970 pTerm = pLoop->aLTerm[0];
145972 assert( pTerm->eOperator & WO_OR );
145973 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
145974 pOrWc = &pTerm->u.pOrInfo->wc;
145975 pLevel->op = OP_Return;
145976 pLevel->p1 = regReturn;
145982 if( pWInfo->nLevel>1 ){
145985 nNotReady = pWInfo->nLevel - iLevel - 1;
145987 sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
145989 pOrTab->nAlloc = (u8)(nNotReady + 1);
145990 pOrTab->nSrc = pOrTab->nAlloc;
145991 memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
145992 origSrc = pWInfo->pTabList->a;
145994 memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
145997 pOrTab = pWInfo->pTabList;
146008 ** correct response for the end-of-loop code (the OP_Return) is to
146012 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
146014 regRowset = ++pParse->nMem;
146018 regRowset = pParse->nTab++;
146019 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
146022 regRowid = ++pParse->nMem;
146032 ** the "interesting" terms of z - terms that did not originate in the
146040 if( pWC->nTerm>1 ){
146042 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
146043 Expr *pExpr = pWC->a[iTerm].pExpr;
146044 if( &pWC->a[iTerm] == pTerm ) continue;
146045 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
146046 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
146047 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
146048 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
146049 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
146057 ** prevents sqlite3PExpr() from implementing AND short-circuit
146065 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
146067 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
146068 for(ii=0; ii<pOrWc->nTerm; ii++){
146069 WhereTerm *pOrTerm = &pOrWc->a[ii];
146070 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
146071 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
146072 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
146078 pAndExpr->pLeft = pOrExpr;
146083 WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
146086 assert( pSubWInfo || pParse->nErr || db->mallocFailed );
146090 pParse, pOrTab, &pSubWInfo->a[0], 0
146092 sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
146094 /* This is the sub-WHERE clause body. First skip over
146095 ** duplicate rows from prior sub-WHERE clauses, and record the
146097 ** row will be skipped in subsequent sub-WHERE clauses.
146099 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
146100 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
146102 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
146108 int nPk = pPk->nKeyCol;
146115 int iCol = pPk->aiColumn[iPk];
146127 ** the temp table. And if iSet is -1, assume that there is no
146150 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
146153 /* The pSubWInfo->untestedTerms flag means that this OR term
146158 if( pSubWInfo->untestedTerms ) untestedTerms = 1;
146160 /* If all of the OR-connected terms are optimized using the same
146166 ** uses an index, and this is either the first OR-connected term
146172 pSubLoop = pSubWInfo->a[0].pWLoop;
146173 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
146174 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
146175 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
146176 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
146178 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
146179 pCov = pSubLoop->u.btree.pIndex;
146184 pWInfo->bDeferredSeek = 1;
146194 pLevel->u.pCovidx = pCov;
146195 if( pCov ) pLevel->iIdxCur = iCovCur;
146197 pAndExpr->pLeft = 0;
146201 sqlite3VdbeGoto(v, pLevel->addrBrk);
146204 if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
146216 if( pTabItem->fg.isRecursive ){
146218 ** a pseudo-cursor. No need to Rewind or Next such cursors. */
146219 pLevel->op = OP_Noop;
146222 pLevel->op = aStep[bRev];
146223 pLevel->p1 = iCur;
146224 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrHalt);
146227 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
146232 pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
146244 ** sub-queries.
146252 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
146255 testcase( pTerm->wtFlags & TERM_VIRTUAL );
146256 testcase( pTerm->wtFlags & TERM_CODED );
146257 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
146258 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
146259 testcase( pWInfo->untestedTerms==0
146260 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 );
146261 pWInfo->untestedTerms = 1;
146264 pE = pTerm->pExpr;
146266 if( (pTabItem->fg.jointype&JT_LEFT) && !ExprHasProperty(pE,EP_FromJoin) ){
146270 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
146274 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
146279 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
146288 u32 x = pLevel->iLikeRepCntr;
146299 pWC->nTerm-j, pTerm, iLoop));
146303 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
146308 pTerm->wtFlags |= TERM_CODED;
146321 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
146324 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
146325 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
146326 if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
146327 if( pTerm->leftCursor!=iCur ) continue;
146328 if( pTabItem->fg.jointype & JT_LEFT ) continue;
146329 pE = pTerm->pExpr;
146333 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
146337 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
146338 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
146341 if( pAlt->wtFlags & (TERM_CODED) ) continue;
146342 if( (pAlt->eOperator & WO_IN)
146343 && (pAlt->pExpr->flags & EP_xIsSelect)
146344 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
146348 testcase( pAlt->eOperator & WO_EQ );
146349 testcase( pAlt->eOperator & WO_IS );
146350 testcase( pAlt->eOperator & WO_IN );
146352 sEAlt = *pAlt->pExpr;
146353 sEAlt.pLeft = pE->pLeft;
146360 if( pLevel->iLeftJoin ){
146361 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
146362 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
146364 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
146365 testcase( pTerm->wtFlags & TERM_VIRTUAL );
146366 testcase( pTerm->wtFlags & TERM_CODED );
146367 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
146368 if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
146369 assert( pWInfo->untestedTerms );
146372 assert( pTerm->pExpr );
146373 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
146374 pTerm->wtFlags |= TERM_CODED;
146380 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
146386 iLevel, (u64)pLevel->notReady);
146389 return pLevel->notReady;
146395 ** 2015-06-08
146422 sqlite3WhereClauseClear(&p->wc);
146430 sqlite3WhereClauseClear(&p->wc);
146437 ** The index in pWC->a[] of the new WhereTerm is returned on success.
146440 ** the db->mallocFailed flag so that higher-level functions can detect it.
146442 ** This routine will increase the size of the pWC->a[] array as necessary.
146451 ** the pWC->a[] array.
146457 if( pWC->nTerm>=pWC->nSlot ){
146458 WhereTerm *pOld = pWC->a;
146459 sqlite3 *db = pWC->pWInfo->pParse->db;
146460 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
146461 if( pWC->a==0 ){
146465 pWC->a = pOld;
146468 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
146469 if( pOld!=pWC->aStatic ){
146472 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
146474 pTerm = &pWC->a[idx = pWC->nTerm++];
146476 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
146478 pTerm->truthProb = 1;
146480 pTerm->pExpr = sqlite3ExprSkipCollateAndLikely(p);
146481 pTerm->wtFlags = wtFlags;
146482 pTerm->pWC = pWC;
146483 pTerm->iParent = -1;
146484 memset(&pTerm->eOperator, 0,
146485 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
146507 if( pExpr->pLeft->op==TK_VECTOR
146508 || pExpr->pRight->op==TK_VECTOR
146509 || sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight) !=
146510 sqlite3BinaryCompareCollSeq(pParse, pExpr->pRight, pExpr->pLeft)
146512 pExpr->flags ^= EP_Commuted;
146514 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
146515 if( pExpr->op>=TK_GT ){
146520 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
146521 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
146539 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
146540 c = (u16)(WO_EQ<<(op-TK_EQ));
146578 int cnt; /* Number of non-wildcard prefix characters */
146580 sqlite3 *db = pParse->db; /* Database connection */
146591 pList = pExpr->x.pList;
146592 pLeft = pList->a[1].pExpr;
146594 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
146595 op = pRight->op;
146596 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
146597 Vdbe *pReprepare = pParse->pReprepare;
146598 int iCol = pRight->iColumn;
146603 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
146604 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
146606 z = (u8*)pRight->u.zToken;
146618 ** with a wildcard and if (2) the non-wildcard prefix does not end with
146625 if( cnt!=0 && 255!=(u8)z[cnt-1] && (cnt>1 || z[0]!=wc[3]) ){
146635 char *zNew = pPrefix->u.zToken;
146651 ** 2018-09-10 https://sqlite.org/src/info/c94369cae9b561b1
146652 ** 2019-05-02 https://sqlite.org/src/info/b043a54c3de54b28
146653 ** 2019-06-10 https://sqlite.org/src/info/fd76310a5e843e07
146654 ** 2019-06-14 https://sqlite.org/src/info/ce8717f0885af975
146655 ** 2019-09-03 https://sqlite.org/src/info/0f0428096f17252a
146657 if( pLeft->op!=TK_COLUMN
146659 || IsVirtual(pLeft->y.pTab) /* Value might be numeric */
146665 if( iTo==1 && zNew[0]=='-' ){
146668 zNew[iTo-1]++;
146670 zNew[iTo-1]--;
146685 Vdbe *v = pParse->pVdbe;
146686 sqlite3VdbeSetVarmask(v, pRight->iColumn);
146687 if( *pisComplete && pRight->u.zToken[1] ){
146696 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
146718 ** ----------------------- ---------------------------------
146746 if( pExpr->op==TK_FUNCTION ){
146760 pList = pExpr->x.pList;
146761 if( pList==0 || pList->nExpr!=2 ){
146765 /* Built-in operators MATCH, GLOB, LIKE, and REGEXP attach to a
146767 ** the left-hand side operand in their in-fix form.
146772 pCol = pList->a[1].pExpr;
146773 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
146776 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
146778 *ppRight = pList->a[0].pExpr;
146791 ** Historically, xFindFunction expected to see lower-case function
146795 pCol = pList->a[0].pExpr;
146796 testcase( pCol->op==TK_COLUMN && pCol->y.pTab==0 );
146802 pVtab = sqlite3GetVTable(db, pCol->y.pTab)->pVtab;
146804 assert( pVtab->pModule!=0 );
146805 pMod = (sqlite3_module *)pVtab->pModule;
146806 if( pMod->xFindFunction!=0 ){
146807 i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
146810 *ppRight = pList->a[1].pExpr;
146816 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
146818 Expr *pLeft = pExpr->pLeft;
146819 Expr *pRight = pExpr->pRight;
146820 testcase( pLeft->op==TK_COLUMN && pLeft->y.pTab==0 );
146824 testcase( pRight && pRight->op==TK_COLUMN && pRight->y.pTab==0 );
146831 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
146832 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
146833 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
146846 pDerived->flags |= pBase->flags & EP_FromJoin;
146847 pDerived->iRightJoinTable = pBase->iRightJoinTable;
146855 pWC->a[iChild].iParent = iParent;
146856 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
146857 pWC->a[iParent].nChild++;
146861 ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
146866 if( pTerm->eOperator!=WO_AND ){
146869 if( N<pTerm->u.pAndInfo->wc.nTerm ){
146870 return &pTerm->u.pAndInfo->wc.a[N];
146877 ** two subterms are in disjunction - they are OR-ed together.
146887 ** x<y OR x=y --> x<=y
146888 ** x=y OR x=y --> x=y
146889 ** x<=y OR x<y --> x<=y
146893 ** x<y OR x>y --> x!=y
146901 u16 eOp = pOne->eOperator | pTwo->eOperator;
146907 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
146908 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
146911 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
146912 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
146913 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
146914 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
146916 if( (eOp & (eOp-1))!=0 ){
146924 db = pWC->pWInfo->pParse->db;
146925 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
146927 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
146928 pNew->op = op;
146935 ** Analyze a term that consists of two or more OR-connected
146948 ** The term being analyzed must have two or more of OR-connected subterms.
146949 ** A single subterm might be a set of AND-connected sub-subterms.
146987 ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
147024 int idxTerm /* Index of the OR-term to be analyzed */
147026 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
147027 Parse *pParse = pWInfo->pParse; /* Parser context */
147028 sqlite3 *db = pParse->db; /* Database connection */
147029 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
147030 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
147033 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
147043 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
147044 assert( pExpr->op==TK_OR );
147045 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
147047 pTerm->wtFlags |= TERM_ORINFO;
147048 pOrWc = &pOrInfo->wc;
147049 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
147053 if( db->mallocFailed ) return;
147054 assert( pOrWc->nTerm>=2 );
147061 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
147062 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
147064 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
147072 pOrTerm->u.pAndInfo = pAndInfo;
147073 pOrTerm->wtFlags |= TERM_ANDINFO;
147074 pOrTerm->eOperator = WO_AND;
147075 pAndWC = &pAndInfo->wc;
147076 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
147077 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
147078 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
147080 pAndWC->pOuter = pWC;
147081 if( !db->mallocFailed ){
147082 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
147083 assert( pAndTerm->pExpr );
147084 if( allowedOp(pAndTerm->pExpr->op)
147085 || pAndTerm->eOperator==WO_AUX
147087 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
147093 }else if( pOrTerm->wtFlags & TERM_COPIED ){
147098 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
147099 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
147100 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
147101 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
147104 if( (pOrTerm->eOperator & WO_EQ)==0 ){
147116 pOrInfo->indexable = indexable;
147118 pTerm->eOperator = WO_OR;
147119 pWC->hasOr = 1;
147121 pTerm->eOperator = WO_OR;
147124 /* For a two-way OR, attempt to implementation case 2.
147126 if( indexable && pOrWc->nTerm==2 ){
147129 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
147132 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
147143 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
147146 ** something other than == on a column in the single table. The 1-bit
147150 ** sure the same column is used on all terms. The 2-bit case is when
147161 int iColumn = -1; /* Column index on lhs of IN operator */
147162 int iCursor = -1; /* Table cursor common to all terms */
147173 pOrTerm = pOrWc->a;
147174 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
147175 assert( pOrTerm->eOperator & WO_EQ );
147176 pOrTerm->wtFlags &= ~TERM_OR_OK;
147177 if( pOrTerm->leftCursor==iCursor ){
147178 /* This is the 2-bit case and we are on the second iteration and
147183 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
147184 pOrTerm->leftCursor))==0 ){
147189 testcase( pOrTerm->wtFlags & TERM_COPIED );
147190 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
147191 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
147194 iColumn = pOrTerm->u.x.leftColumn;
147195 iCursor = pOrTerm->leftCursor;
147196 pLeft = pOrTerm->pExpr->pLeft;
147204 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
147212 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
147213 assert( pOrTerm->eOperator & WO_EQ );
147214 if( pOrTerm->leftCursor!=iCursor ){
147215 pOrTerm->wtFlags &= ~TERM_OR_OK;
147216 }else if( pOrTerm->u.x.leftColumn!=iColumn || (iColumn==XN_EXPR
147217 && sqlite3ExprCompare(pParse, pOrTerm->pExpr->pLeft, pLeft, -1)
147222 /* If the right-hand side is also a column, then the affinities
147226 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
147227 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
147231 pOrTerm->wtFlags |= TERM_OR_OK;
147247 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
147248 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
147249 assert( pOrTerm->eOperator & WO_EQ );
147250 assert( pOrTerm->leftCursor==iCursor );
147251 assert( pOrTerm->u.x.leftColumn==iColumn );
147252 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
147253 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
147254 pLeft = pOrTerm->pExpr->pLeft;
147263 pNew->x.pList = pList;
147267 /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
147295 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
147296 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
147298 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
147299 aff2 = sqlite3ExprAffinity(pExpr->pRight);
147307 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
147318 SrcList *pSrc = pS->pSrc;
147319 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
147320 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
147321 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
147322 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
147323 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
147326 for(i=0; i<pSrc->nSrc; i++){
147327 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
147328 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
147329 if( pSrc->a[i].fg.isTabFunc ){
147330 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
147334 pS = pS->pPrior;
147345 ** of the column that is indexed, or XN_EXPR (-2) if an expression is being
147362 iCur = pFrom->a[i].iCursor;
147363 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
147364 if( pIdx->aColExpr==0 ) continue;
147365 for(i=0; i<pIdx->nKeyCol; i++){
147366 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
147367 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
147389 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
147390 pExpr = pExpr->x.pList->a[0].pExpr;
147393 if( pExpr->op==TK_COLUMN ){
147394 aiCurCol[0] = pExpr->iTable;
147395 aiCurCol[1] = pExpr->iColumn;
147399 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
147407 if( pExpr->op==TK_COLUMN ){
147408 SrcList *pSrc = p->u.pSrcList;
147409 int iCsr = pExpr->iTable;
147411 for(ii=0; ii<pSrc->nSrc; ii++){
147412 if( pSrc->a[ii].iCursor==iCsr ){
147413 return p->eCode ? WRC_Abort : WRC_Continue;
147416 return p->eCode ? WRC_Continue : WRC_Abort;
147432 ** a sub-select.
147434 ** If there is no sub-select in pExpr, then return true if pExpr
147441 ** -------- ------ ------------------------------------------------
147443 ** bUses==1 true pExpr contains either a sub-select or a
147446 ** bUses==1 false pExpr contains no sub-selects and all TK_COLUMN
147449 ** bUses==0 true pExpr contains either a sub-select or a TK_COLUMN
147452 ** bUses==0 false pExpr contains no sub-selects and all TK_COLUMN
147482 ** This function returns non-zero if the expression tree does not meet
147488 switch( pExpr->op ){
147490 p->ppParent = ppExpr;
147491 if( exprExistsToInIter(p, &pExpr->pLeft) ) return 1;
147492 p->ppParent = ppExpr;
147493 if( exprExistsToInIter(p, &pExpr->pRight) ) return 1;
147496 int bLeft = exprUsesSrclist(p->pSrc, pExpr->pLeft, 0);
147497 int bRight = exprUsesSrclist(p->pSrc, pExpr->pRight, 0);
147499 if( (bLeft && bRight) || p->pInLhs ) return 1;
147500 p->pInLhs = bLeft ? pExpr->pLeft : pExpr->pRight;
147501 if( exprUsesSrclist(p->pSrc, p->pInLhs, 1) ) return 1;
147502 p->pEq = pExpr;
147503 p->ppAnd = p->ppParent;
147508 if( exprUsesSrclist(p->pSrc, pExpr, 0) ){
147519 ** terms equivalent to user-supplied EXIST(...) clauses. It splits the WHERE
147535 ** If non-NULL is returned and ppEq is non-NULL, *ppEq is set to point
147536 ** to the == expression node before returning. If pppAnd is non-NULL and
147548 ctx.pSrc = pSel->pSrc;
147549 if( exprExistsToInIter(&ctx, &pSel->pWhere) ){
147573 ** 1. The sub-select must not be an aggregate or use window functions,
147575 ** 2. The sub-select must not be a compound SELECT,
147591 Parse *pParse = pWC->pWInfo->pParse;
147592 WhereTerm *pTerm = &pWC->a[idxTerm];
147593 Expr *pExpr = pTerm->pExpr;
147594 Select *pSel = pExpr->x.pSelect;
147601 sqlite3 *db = pParse->db;
147603 assert( pExpr->op==TK_EXISTS );
147604 assert( (pExpr->flags & EP_VarSelect) && (pExpr->flags & EP_xIsSelect) );
147606 if( pSel->selFlags & SF_Aggregate ) return;
147608 if( pSel->pWin ) return;
147610 if( pSel->pPrior ) return;
147611 if( pSel->pWhere==0 ) return;
147612 if( pSel->pLimit ) return;
147616 if( db->mallocFailed ){
147620 pSel = pDup->x.pSelect;
147621 sqlite3ExprListDelete(db, pSel->pEList);
147622 pSel->pEList = 0;
147626 assert( pEq==pSel->pWhere || ppAnd );
147627 if( pInLhs==pEq->pLeft ){
147628 pRet = pEq->pRight;
147631 pInLhs = sqlite3ExprAddCollateString(pParse, pInLhs, p?p->zName:"BINARY");
147632 pRet = pEq->pLeft;
147635 assert( pDup->pLeft==0 );
147636 pDup->op = TK_IN;
147637 pDup->pLeft = pInLhs;
147638 pDup->flags &= ~EP_VarSelect;
147639 if( pRet->op==TK_VECTOR ){
147640 pSel->pEList = pRet->x.pList;
147641 pRet->x.pList = 0;
147644 pSel->pEList = sqlite3ExprListAppend(pParse, 0, pRet);
147646 pEq->pLeft = 0;
147647 pEq->pRight = 0;
147650 Expr *pOther = (pAnd->pLeft==pEq) ? pAnd->pRight : pAnd->pLeft;
147651 pAnd->pLeft = pAnd->pRight = 0;
147655 assert( pSel->pWhere==pEq );
147656 pSel->pWhere = 0;
147671 pWC->a[idxTerm].wtFlags |= TERM_COPIED;
147697 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
147701 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
147707 int op; /* Top-level operator. pExpr->op */
147708 Parse *pParse = pWInfo->pParse; /* Parsing context */
147709 sqlite3 *db = pParse->db; /* Database connection */
147713 if( db->mallocFailed ){
147716 pTerm = &pWC->a[idxTerm];
147717 pMaskSet = &pWInfo->sMaskSet;
147718 pExpr = pTerm->pExpr;
147719 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
147720 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
147721 op = pExpr->op;
147723 assert( pExpr->pRight==0 );
147726 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
147728 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
147731 pTerm->prereqRight = 0;
147733 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
147735 pMaskSet->bVarSelect = 0;
147737 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
147739 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
147741 extraRight = x-1; /* ON clause terms may not be used with an index
147748 pTerm->prereqAll = prereqAll;
147749 pTerm->leftCursor = -1;
147750 pTerm->iParent = -1;
147751 pTerm->eOperator = 0;
147754 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
147755 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
147756 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
147758 if( pTerm->u.x.iField>0 ){
147760 assert( pLeft->op==TK_VECTOR );
147761 pLeft = pLeft->x.pList->a[pTerm->u.x.iField-1].pExpr;
147765 pTerm->leftCursor = aiCurCol[0];
147766 pTerm->u.x.leftColumn = aiCurCol[1];
147767 pTerm->eOperator = operatorMask(op) & opMask;
147769 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
147771 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
147775 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
147776 assert( pTerm->u.x.iField==0 );
147777 if( pTerm->leftCursor>=0 ){
147780 if( db->mallocFailed ){
147786 pNew = &pWC->a[idxNew];
147788 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
147789 pTerm = &pWC->a[idxTerm];
147790 pTerm->wtFlags |= TERM_COPIED;
147793 pTerm->eOperator |= WO_EQUIV;
147800 pNew->wtFlags |= exprCommute(pParse, pDup);
147801 pNew->leftCursor = aiCurCol[0];
147802 pNew->u.x.leftColumn = aiCurCol[1];
147804 pNew->prereqRight = prereqLeft | extraRight;
147805 pNew->prereqAll = prereqAll;
147806 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
147808 pExpr->op = TK_TRUEFALSE;
147809 pExpr->u.zToken = "false";
147811 pTerm->prereqAll = 0;
147812 pTerm->eOperator = 0;
147832 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
147833 ExprList *pList = pExpr->x.pList;
147837 assert( pList->nExpr==2 );
147842 sqlite3ExprDup(db, pExpr->pLeft, 0),
147843 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
147848 pTerm = &pWC->a[idxTerm];
147858 else if( pExpr->op==TK_OR ){
147859 assert( pWC->op==TK_AND );
147861 pTerm = &pWC->a[idxTerm];
147865 else if( pExpr->op==TK_EXISTS ){
147867 if( (pExpr->flags & EP_VarSelect)!=0
147880 else if( pExpr->op==TK_NOTNULL ){
147881 if( pExpr->pLeft->op==TK_COLUMN
147882 && pExpr->pLeft->iColumn>=0
147886 Expr *pLeft = pExpr->pLeft;
147897 pNewTerm = &pWC->a[idxNew];
147898 pNewTerm->prereqRight = 0;
147899 pNewTerm->leftCursor = pLeft->iTable;
147900 pNewTerm->u.x.leftColumn = pLeft->iColumn;
147901 pNewTerm->eOperator = WO_GT;
147903 pTerm = &pWC->a[idxTerm];
147904 pTerm->wtFlags |= TERM_COPIED;
147905 pNewTerm->prereqAll = pTerm->prereqAll;
147921 ** for LIKE) then the lower-bound is made all uppercase and the upper-
147925 else if( pExpr->op==TK_FUNCTION
147926 && pWC->op==TK_AND
147930 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
147938 pLeft = pExpr->x.pList->a[1].pExpr;
147941 /* Convert the lower bound to upper-case and the upper bound to
147942 ** lower-case (upper-case is less than lower-case in ASCII) so that
147945 if( noCase && !pParse->db->mallocFailed ){
147948 pTerm->wtFlags |= TERM_LIKE;
147949 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
147950 pStr1->u.zToken[i] = sqlite3Toupper(c);
147951 pStr2->u.zToken[i] = sqlite3Tolower(c);
147955 if( !db->mallocFailed ){
147957 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
147966 if( c=='A'-1 ) isComplete = 0;
147988 pTerm = &pWC->a[idxTerm];
147996 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
147997 ** new terms for each component comparison - "a = ?" and "b = ?". The
148002 ** is not a sub-select. */
148003 if( (pExpr->op==TK_EQ || pExpr->op==TK_IS)
148004 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
148005 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
148006 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
148007 || (pExpr->pRight->flags & EP_xIsSelect)==0)
148008 && pWC->op==TK_AND
148014 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
148015 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
148017 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
148022 pTerm = &pWC->a[idxTerm];
148023 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
148024 pTerm->eOperator = 0;
148027 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
148036 else if( pExpr->op==TK_IN
148037 && pTerm->u.x.iField==0
148038 && pExpr->pLeft->op==TK_VECTOR
148039 && pExpr->x.pSelect->pPrior==0
148041 && pExpr->x.pSelect->pWin==0
148043 && pWC->op==TK_AND
148046 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
148049 pWC->a[idxNew].u.x.iField = i+1;
148065 else if( pWC->op==TK_AND ){
148068 while( res-- > 0 ){
148081 pNewExpr->iRightJoinTable = pExpr->iRightJoinTable;
148085 pNewTerm = &pWC->a[idxNew];
148086 pNewTerm->prereqRight = prereqExpr;
148087 pNewTerm->leftCursor = pLeft->iTable;
148088 pNewTerm->u.x.leftColumn = pLeft->iColumn;
148089 pNewTerm->eOperator = WO_AUX;
148090 pNewTerm->eMatchOp = eOp2;
148092 pTerm = &pWC->a[idxTerm];
148093 pTerm->wtFlags |= TERM_COPIED;
148094 pNewTerm->prereqAll = pTerm->prereqAll;
148104 testcase( pTerm!=&pWC->a[idxTerm] );
148105 pTerm = &pWC->a[idxTerm];
148106 pTerm->prereqRight |= extraRight;
148133 pWC->op = op;
148136 if( pE2->op!=op ){
148139 sqlite3WhereSplit(pWC, pE2->pLeft, op);
148140 sqlite3WhereSplit(pWC, pE2->pRight, op);
148151 pWC->pWInfo = pWInfo;
148152 pWC->hasOr = 0;
148153 pWC->pOuter = 0;
148154 pWC->nTerm = 0;
148155 pWC->nSlot = ArraySize(pWC->aStatic);
148156 pWC->a = pWC->aStatic;
148167 sqlite3 *db = pWC->pWInfo->pParse->db;
148168 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
148169 if( a->wtFlags & TERM_DYNAMIC ){
148170 sqlite3ExprDelete(db, a->pExpr);
148172 if( a->wtFlags & TERM_ORINFO ){
148173 whereOrInfoDelete(db, a->u.pOrInfo);
148174 }else if( a->wtFlags & TERM_ANDINFO ){
148175 whereAndInfoDelete(db, a->u.pAndInfo);
148178 if( pWC->a!=pWC->aStatic ){
148179 sqlite3DbFree(db, pWC->a);
148191 if( p->op==TK_COLUMN && !ExprHasProperty(p, EP_FixedCol) ){
148192 return sqlite3WhereGetMask(pMaskSet, p->iTable);
148194 assert( p->op!=TK_IF_NULL_ROW );
148197 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
148198 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
148199 if( p->pRight ){
148200 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
148201 assert( p->x.pList==0 );
148203 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
148204 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
148205 }else if( p->x.pList ){
148206 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
148209 if( (p->op==TK_FUNCTION || p->op==TK_AGG_FUNCTION) && p->y.pWin ){
148210 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pPartition);
148211 mask |= sqlite3WhereExprListUsage(pMaskSet, p->y.pWin->pOrderBy);
148212 mask |= sqlite3WhereExprUsage(pMaskSet, p->y.pWin->pFilter);
148224 for(i=0; i<pList->nExpr; i++){
148225 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
148245 for(i=pWC->nTerm-1; i>=0; i--){
148251 ** For table-valued-functions, transform the function arguments into
148267 if( pItem->fg.isTabFunc==0 ) return;
148268 pTab = pItem->pTab;
148270 pArgs = pItem->u1.pFuncArg;
148272 for(j=k=0; j<pArgs->nExpr; j++){
148274 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
148275 if( k>=pTab->nCol ){
148276 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
148277 pTab->zName, j);
148280 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
148282 pColRef->iTable = pItem->iCursor;
148283 pColRef->iColumn = k++;
148284 pColRef->y.pTab = pTab;
148286 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
148288 if( pItem->fg.jointype & JT_LEFT ){
148289 sqlite3SetJoinExpr(pTerm, pItem->iCursor);
148340 return pWInfo->nRowOut;
148348 return pWInfo->eDistinct;
148360 return pWInfo->nOBSat;
148364 ** In the ORDER BY LIMIT optimization, if the inner-most loop is known
148366 ** inner-most loop did not fit within the sorter, then we can skip all
148369 ** loop - the loop immediately outside the inner-most.
148376 ** be the continuation for the second-inner-most loop. If the ORDER BY
148378 ** be the continuation for the inner-most loop.
148381 ** inner-most loop, in the sense that a correct answer will result.
148388 if( !pWInfo->bOrderedInnerLoop ){
148390 ** continuation of the inner-most loop. */
148391 return pWInfo->iContinue;
148393 pInner = &pWInfo->a[pWInfo->nLevel-1];
148394 assert( pInner->addrNxt!=0 );
148395 return pInner->addrNxt;
148400 ** the aggregate-step call to min() or max(), check to see if any
148412 if( !pWInfo->bOrderedInnerLoop ) return;
148413 if( pWInfo->nOBSat==0 ) return;
148414 for(i=pWInfo->nLevel-1; i>=0; i--){
148415 pInner = &pWInfo->a[i];
148416 if( (pInner->pWLoop->wsFlags & WHERE_COLUMN_IN)!=0 ){
148417 sqlite3VdbeGoto(v, pInner->addrNxt);
148421 sqlite3VdbeGoto(v, pWInfo->iBreak);
148429 assert( pWInfo->iContinue!=0 );
148430 return pWInfo->iContinue;
148438 return pWInfo->iBreak;
148445 ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass
148452 ** Either value may be -1, indicating that cursor is not used.
148455 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
148459 memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
148461 if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){
148463 pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI",
148467 return pWInfo->eOnePass;
148475 return pWInfo->bDeferredSeek;
148482 pDest->n = pSrc->n;
148483 memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
148496 LogEst rRun, /* Run-cost of the new entry */
148501 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
148502 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
148505 if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
148509 if( pSet->n<N_OR_COST ){
148510 p = &pSet->a[pSet->n++];
148511 p->nOut = nOut;
148513 p = pSet->a;
148514 for(i=1; i<pSet->n; i++){
148515 if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
148517 if( p->rRun<=rRun ) return 0;
148520 p->prereq = prereq;
148521 p->rRun = rRun;
148522 if( p->nOut>nOut ) p->nOut = nOut;
148532 assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
148533 for(i=0; i<pMaskSet->n; i++){
148534 if( pMaskSet->ix[i]==iCursor ){
148546 ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[]
148550 assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
148551 pMaskSet->ix[pMaskSet->n++] = iCursor;
148555 ** If the right-hand branch of the expression is a TK_COLUMN, then return
148556 ** a pointer to the right-hand branch. Otherwise, return NULL.
148559 p = sqlite3ExprSkipCollateAndLikely(p->pRight);
148560 if( ALWAYS(p!=0) && p->op==TK_COLUMN ) return p;
148571 i16 iColumn; /* The column on the LHS of the term. -1 for IPK */
148573 WhereClause *pWC; /* Shorthand for pScan->pWC */
148575 int k = pScan->k; /* Where to start scanning */
148577 assert( pScan->iEquiv<=pScan->nEquiv );
148578 pWC = pScan->pWC;
148580 iColumn = pScan->aiColumn[pScan->iEquiv-1];
148581 iCur = pScan->aiCur[pScan->iEquiv-1];
148584 for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
148585 if( pTerm->leftCursor==iCur
148586 && pTerm->u.x.leftColumn==iColumn
148588 || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
148589 pScan->pIdxExpr,iCur)==0)
148590 && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
148592 if( (pTerm->eOperator & WO_EQUIV)!=0
148593 && pScan->nEquiv<ArraySize(pScan->aiCur)
148594 && (pX = whereRightSubexprIsColumn(pTerm->pExpr))!=0
148597 for(j=0; j<pScan->nEquiv; j++){
148598 if( pScan->aiCur[j]==pX->iTable
148599 && pScan->aiColumn[j]==pX->iColumn ){
148603 if( j==pScan->nEquiv ){
148604 pScan->aiCur[j] = pX->iTable;
148605 pScan->aiColumn[j] = pX->iColumn;
148606 pScan->nEquiv++;
148609 if( (pTerm->eOperator & pScan->opMask)!=0 ){
148611 if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
148613 Parse *pParse = pWC->pWInfo->pParse;
148614 pX = pTerm->pExpr;
148615 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
148618 assert(pX->pLeft);
148620 if( pColl==0 ) pColl = pParse->db->pDfltColl;
148621 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
148625 if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
148626 && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
148627 && pX->iTable==pScan->aiCur[0]
148628 && pX->iColumn==pScan->aiColumn[0]
148630 testcase( pTerm->eOperator & WO_IS );
148633 pScan->pWC = pWC;
148634 pScan->k = k+1;
148639 pWC = pWC->pOuter;
148642 if( pScan->iEquiv>=pScan->nEquiv ) break;
148643 pWC = pScan->pOrigWC;
148645 pScan->iEquiv++;
148652 ** It is factored out into a separate tail-recursion subroutine so that
148653 ** the normal whereScanInit() routine, which is a high-runner, does not
148657 pScan->idxaff = sqlite3ExprAffinity(pScan->pIdxExpr);
148688 pScan->pOrigWC = pWC;
148689 pScan->pWC = pWC;
148690 pScan->pIdxExpr = 0;
148691 pScan->idxaff = 0;
148692 pScan->zCollName = 0;
148693 pScan->opMask = opMask;
148694 pScan->k = 0;
148695 pScan->aiCur[0] = iCur;
148696 pScan->nEquiv = 1;
148697 pScan->iEquiv = 1;
148700 iColumn = pIdx->aiColumn[j];
148702 pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr;
148703 pScan->zCollName = pIdx->azColl[j];
148704 pScan->aiColumn[0] = XN_EXPR;
148706 }else if( iColumn==pIdx->pTable->iPKey ){
148709 pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
148710 pScan->zCollName = pIdx->azColl[j];
148715 pScan->aiColumn[0] = iColumn;
148726 ** Search for terms matching the iColumn-th column of pIdx
148727 ** rather than the iColumn-th column of table iCur.
148731 ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The
148738 ** then try for the one with no dependencies on <expr> - in other words where
148741 ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS
148759 if( (p->prereqRight & notReady)==0 ){
148760 if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
148761 testcase( p->eOperator & WO_IS );
148772 ** This function searches pList for an entry that matches the iCol-th column
148775 ** If such an expression is found, its index in pList->a[] is returned. If
148776 ** no expression is found, -1 is returned.
148786 const char *zColl = pIdx->azColl[iCol];
148788 for(i=0; i<pList->nExpr; i++){
148789 Expr *p = sqlite3ExprSkipCollateAndLikely(pList->a[i].pExpr);
148791 && p->op==TK_COLUMN
148792 && p->iColumn==pIdx->aiColumn[iCol]
148793 && p->iTable==iBase
148795 CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr);
148796 if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
148802 return -1;
148806 ** Return TRUE if the iCol-th column of index pIdx is NOT NULL
148811 assert( iCol>=0 && iCol<pIdx->nColumn );
148812 j = pIdx->aiColumn[iCol];
148814 return pIdx->pTable->aCol[j].notNull;
148815 }else if( j==(-1) ){
148818 assert( j==(-2) );
148825 ** Return true if the DISTINCT expression-list passed as the third argument
148829 ** DISTINCT list are collectively unique and individually non-null.
148842 /* If there is more than one table or sub-select in the FROM clause of
148845 if( pTabList->nSrc!=1 ) return 0;
148846 iBase = pTabList->a[0].iCursor;
148847 pTab = pTabList->a[0].pTab;
148850 ** true. Note: The (p->iTable==iBase) part of this test may be false if the
148851 ** current SELECT is a correlated sub-query.
148853 for(i=0; i<pDistinct->nExpr; i++){
148854 Expr *p = sqlite3ExprSkipCollateAndLikely(pDistinct->a[i].pExpr);
148856 if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
148867 ** comparison and select-list expressions must match those of the index.
148872 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
148874 for(i=0; i<pIdx->nKeyCol; i++){
148880 if( i==pIdx->nKeyCol ){
148894 return N<=10 ? 0 : sqlite3LogEst(N) - 33;
148901 ** opcodes into OP_Copy when the table is being accessed via co-routine
148914 int iAutoidxCur /* If non-zero, cursor of autoindex being generated */
148916 Vdbe *v = pParse->pVdbe;
148919 if( pParse->db->mallocFailed ) return;
148921 if( pOp->p1!=iTabCur ) continue;
148922 if( pOp->opcode==OP_Column ){
148923 pOp->opcode = OP_Copy;
148924 pOp->p1 = pOp->p2 + iRegister;
148925 pOp->p2 = pOp->p3;
148926 pOp->p3 = 0;
148927 }else if( pOp->opcode==OP_Rowid ){
148929 pOp->opcode = OP_Sequence;
148930 pOp->p1 = iAutoidxCur;
148932 pOp->opcode = OP_Null;
148933 pOp->p1 = 0;
148934 pOp->p3 = 0;
148944 ** are no-ops.
148950 for(i=0; i<p->nConstraint; i++){
148953 p->aConstraint[i].iColumn,
148954 p->aConstraint[i].iTermOffset,
148955 p->aConstraint[i].op,
148956 p->aConstraint[i].usable);
148958 for(i=0; i<p->nOrderBy; i++){
148961 p->aOrderBy[i].iColumn,
148962 p->aOrderBy[i].desc);
148968 for(i=0; i<p->nConstraint; i++){
148971 p->aConstraintUsage[i].argvIndex,
148972 p->aConstraintUsage[i].omit);
148974 sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum);
148975 sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr);
148976 sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed);
148977 sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost);
148978 sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows);
148997 if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
148998 if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0;
148999 if( (pSrc->fg.jointype & JT_LEFT)
149000 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
149001 && (pTerm->eOperator & WO_IS)
149008 if( (pTerm->prereqRight & notReady)!=0 ) return 0;
149009 if( pTerm->u.x.leftColumn<0 ) return 0;
149010 aff = pSrc->pTab->aCol[pTerm->u.x.leftColumn].affinity;
149011 if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
149012 testcase( pTerm->pExpr->op==TK_IS );
149033 WhereTerm *pWCEnd; /* End of pWC->a[] */
149042 int mxBitCol; /* Maximum column in pSrc->colUsed */
149057 v = pParse->pVdbe;
149064 pTable = pSrc->pTab;
149065 pWCEnd = &pWC->a[pWC->nTerm];
149066 pLoop = pLevel->pWLoop;
149068 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
149069 Expr *pExpr = pTerm->pExpr;
149070 assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */
149071 || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */
149072 || pLoop->prereq!=0 ); /* table of a LEFT JOIN */
149073 if( pLoop->prereq==0
149074 && (pTerm->wtFlags & TERM_VIRTUAL)==0
149076 && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
149078 sqlite3ExprDup(pParse->db, pExpr, 0));
149081 int iCol = pTerm->u.x.leftColumn;
149082 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
149084 testcase( iCol==BMS-1 );
149087 "automatic index on %s(%s)", pTable->zName,
149088 pTable->aCol[iCol].zName);
149092 if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
149095 pLoop->aLTerm[nKeyCol++] = pTerm;
149101 pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
149102 pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
149113 extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
149114 mxBitCol = MIN(BMS-1,pTable->nCol);
149115 testcase( pTable->nCol==BMS-1 );
149116 testcase( pTable->nCol==BMS-2 );
149120 if( pSrc->colUsed & MASKBIT(BMS-1) ){
149121 nKeyCol += pTable->nCol - BMS + 1;
149125 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
149127 pLoop->u.btree.pIndex = pIdx;
149128 pIdx->zName = "auto-index";
149129 pIdx->pTable = pTable;
149132 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
149134 int iCol = pTerm->u.x.leftColumn;
149135 Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
149136 testcase( iCol==BMS-1 );
149139 Expr *pX = pTerm->pExpr;
149141 pIdx->aiColumn[n] = pTerm->u.x.leftColumn;
149143 assert( pColl!=0 || pParse->nErr>0 ); /* TH3 collate01.800 */
149144 pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
149149 assert( (u32)n==pLoop->u.btree.nEq );
149155 pIdx->aiColumn[n] = i;
149156 pIdx->azColl[n] = sqlite3StrBINARY;
149160 if( pSrc->colUsed & MASKBIT(BMS-1) ){
149161 for(i=BMS-1; i<pTable->nCol; i++){
149162 pIdx->aiColumn[n] = i;
149163 pIdx->azColl[n] = sqlite3StrBINARY;
149168 pIdx->aiColumn[n] = XN_ROWID;
149169 pIdx->azColl[n] = sqlite3StrBINARY;
149172 assert( pLevel->iIdxCur>=0 );
149173 pLevel->iIdxCur = pParse->nTab++;
149174 sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
149176 VdbeComment((v, "for %s", pTable->zName));
149179 pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom];
149180 if( pTabItem->fg.viaCoroutine ){
149181 int regYield = pTabItem->regReturn;
149183 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
149186 VdbeComment((v, "next row of %s", pTabItem->pTab->zName));
149188 addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
149193 pLoop->wsFlags |= WHERE_PARTIALIDX;
149197 pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0
149199 sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
149202 if( pTabItem->fg.viaCoroutine ){
149204 testcase( pParse->db->mallocFailed );
149205 assert( pLevel->iIdxCur>0 );
149206 translateColumnToCopy(pParse, addrTop, pLevel->iTabCur,
149207 pTabItem->regResult, pLevel->iIdxCur);
149209 pTabItem->fg.viaCoroutine = 0;
149211 sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
149221 sqlite3ExprDelete(pParse->db, pPartial);
149252 for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
149253 if( pTerm->leftCursor != pSrc->iCursor ) continue;
149254 if( pTerm->prereqRight & mUnusable ) continue;
149255 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
149256 testcase( pTerm->eOperator & WO_IN );
149257 testcase( pTerm->eOperator & WO_ISNULL );
149258 testcase( pTerm->eOperator & WO_IS );
149259 testcase( pTerm->eOperator & WO_ALL );
149260 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
149261 if( pTerm->wtFlags & TERM_VNULL ) continue;
149262 assert( pTerm->u.x.leftColumn>=(-1) );
149272 int n = pOrderBy->nExpr;
149274 Expr *pExpr = pOrderBy->a[i].pExpr;
149275 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
149276 if( pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL ) break;
149285 pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
149296 pIdxInfo->nOrderBy = nOrderBy;
149297 pIdxInfo->aConstraint = pIdxCons;
149298 pIdxInfo->aOrderBy = pIdxOrderBy;
149299 pIdxInfo->aConstraintUsage = pUsage;
149300 pHidden->pWC = pWC;
149301 pHidden->pParse = pParse;
149302 for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
149304 if( pTerm->leftCursor != pSrc->iCursor ) continue;
149305 if( pTerm->prereqRight & mUnusable ) continue;
149306 assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
149307 testcase( pTerm->eOperator & WO_IN );
149308 testcase( pTerm->eOperator & WO_IS );
149309 testcase( pTerm->eOperator & WO_ISNULL );
149310 testcase( pTerm->eOperator & WO_ALL );
149311 if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue;
149312 if( pTerm->wtFlags & TERM_VNULL ) continue;
149314 /* tag-20191211-002: WHERE-clause constraints are not useful to the
149315 ** right-hand table of a LEFT JOIN. See tag-20191211-001 for the
149317 if( (pSrc->fg.jointype & JT_LEFT)!=0
149318 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
149322 assert( pTerm->u.x.leftColumn>=(-1) );
149323 pIdxCons[j].iColumn = pTerm->u.x.leftColumn;
149325 op = pTerm->eOperator & WO_ALL;
149328 pIdxCons[j].op = pTerm->eMatchOp;
149345 assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
149348 && sqlite3ExprIsVector(pTerm->pExpr->pRight)
149359 pIdxInfo->nConstraint = j;
149361 Expr *pExpr = pOrderBy->a[i].pExpr;
149362 pIdxOrderBy[i].iColumn = pExpr->iColumn;
149363 pIdxOrderBy[i].desc = pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC;
149376 ** If an error occurs, pParse is populated with an error message and an
149383 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
149387 sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
149391 rc = pVtab->pModule->xBestIndex(pVtab, p);
149396 sqlite3OomFault(pParse->db);
149397 }else if( !pVtab->zErrMsg ){
149400 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
149403 sqlite3_free(pVtab->zErrMsg);
149404 pVtab->zErrMsg = 0;
149419 ** into the aSample[] array - it is an index into a virtual set of samples
149430 IndexSample *aSample = pIdx->aSample;
149444 assert( pIdx->nSample>0 );
149445 assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol );
149491 nField = pRec->nField;
149493 iSample = pIdx->nSample * nField;
149505 if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break;
149511 pRec->nField = n;
149514 iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1];
149517 iLower = aSample[iSamp].anLt[n-1];
149519 res = -1;
149522 iCol = n-1;
149531 if( pParse->db->mallocFailed==0 ){
149534 assert( i<pIdx->nSample );
149535 assert( iCol==nField-1 );
149536 pRec->nField = nField;
149538 || pParse->db->mallocFailed
149541 /* Unless i==pIdx->nSample, indicating that pRec is larger than
149544 assert( i<=pIdx->nSample && i>=0 );
149545 pRec->nField = iCol+1;
149546 assert( i==pIdx->nSample
149548 || pParse->db->mallocFailed );
149553 ** If (i>0), then pRec must also be greater than sample (i-1). */
149555 pRec->nField = iCol;
149557 || pParse->db->mallocFailed );
149560 pRec->nField = nField;
149561 assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
149562 || pParse->db->mallocFailed );
149570 assert( iCol==nField-1 );
149575 ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec
149578 if( i>=pIdx->nSample ){
149579 iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
149587 iGap = iUpper - iLower;
149595 aStat[1] = pIdx->aAvgEq[nField-1];
149598 /* Restore the pRec->nField value before returning. */
149599 pRec->nField = nField;
149618 if( pTerm->truthProb<=0 ){
149619 nRet += pTerm->truthProb;
149620 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
149621 nRet -= 20; assert( 20==sqlite3LogEst(4) );
149633 assert( iCol>=0 && iCol<pIdx->nColumn );
149634 if( !pIdx->zColAff ){
149637 assert( pIdx->zColAff[iCol]!=0 );
149638 return pIdx->zColAff[iCol];
149646 ** range-scan on a skip-scan index. For example:
149651 ** Value pLoop->nOut is currently set to the estimated number of rows
149662 ** N is the total number of samples, the pLoop->nOut value is adjusted
149665 ** nOut = nOut * ( min(U - L, 1) / N )
149686 Index *p = pLoop->u.btree.pIndex;
149687 int nEq = pLoop->u.btree.nEq;
149688 sqlite3 *db = pParse->db;
149689 int nLower = -1;
149690 int nUpper = p->nSample+1;
149699 pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
149701 rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
149705 rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
149706 nUpper = p2 ? 0 : p->nSample;
149712 for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
149713 rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
149723 nDiff = (nUpper - nLower);
149732 int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
149733 pLoop->nOut -= nAdjust;
149735 WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n",
149736 nLower, nUpper, nAdjust*-1, pLoop->nOut));
149766 ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index
149774 ** left-most column of the index). Or, if the query is:
149799 int nOut = pLoop->nOut;
149803 Index *p = pLoop->u.btree.pIndex;
149804 int nEq = pLoop->u.btree.nEq;
149806 if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
149807 && OptimizationEnabled(pParse->db, SQLITE_Stat4)
149809 if( nEq==pBuilder->nRecValid ){
149810 UnpackedRecord *pRec = pBuilder->pRec;
149812 int nBtm = pLoop->u.btree.nBtm;
149813 int nTop = pLoop->u.btree.nTop;
149818 ** key-prefix formed by the nEq values matched against the nEq left-most
149832 ** The number of rows between the two bounds is then just iUpper-iLower.
149836 int iLwrIdx = -2; /* aSample[] for the lower bound */
149837 int iUprIdx = -1; /* aSample[] for the upper bound */
149840 testcase( pRec->nField!=pBuilder->nRecValid );
149841 pRec->nField = pBuilder->nRecValid;
149846 iUpper = p->nRowEst0;
149848 /* Note: this call could be optimized away - since the same values must
149855 assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
149856 assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
149857 assert( p->aSortOrder!=0 );
149858 if( p->aSortOrder[nEq] ){
149867 Expr *pExpr = pLower->pExpr->pRight;
149874 iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
149876 nOut--;
149884 Expr *pExpr = pUpper->pExpr->pRight;
149891 iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
149893 nOut--;
149898 pBuilder->pRec = pRec;
149901 nNew = sqlite3LogEst(iUpper - iLower);
149906 if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) );
149927 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
149932 ** has an application-defined likelihood(), assume the range is
149933 ** reduced by an additional 75%. This means that, by default, an open-ended
149937 if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
149938 nNew -= 20;
149941 nOut -= (pLower!=0) + (pUpper!=0);
149945 if( pLoop->nOut>nOut ){
149947 pLoop->nOut, nOut));
149950 pLoop->nOut = (LogEst)nOut;
149958 ** the histogram data. This only works when x is the left-most
149965 ** non-zero.
149978 Index *p = pBuilder->pNew->u.btree.pIndex;
149979 int nEq = pBuilder->pNew->u.btree.nEq;
149980 UnpackedRecord *pRec = pBuilder->pRec;
149986 assert( nEq<=p->nColumn );
149987 assert( p->aSample!=0 );
149988 assert( p->nSample>0 );
149989 assert( pBuilder->nRecValid<nEq );
149993 if( pBuilder->nRecValid<(nEq-1) ){
149999 if( nEq>=p->nColumn ){
150004 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk);
150005 pBuilder->pRec = pRec;
150008 pBuilder->nRecValid = nEq;
150012 p->zName, nEq-1, (int)a[1]));
150022 ** an IN constraint where the right-hand side of the IN operator
150029 ** non-zero.
150042 Index *p = pBuilder->pNew->u.btree.pIndex;
150043 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
150044 int nRecValid = pBuilder->nRecValid;
150050 assert( p->aSample!=0 );
150051 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
150053 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
150055 pBuilder->nRecValid = nRecValid;
150063 assert( pBuilder->nRecValid==nRecValid );
150075 sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm);
150080 if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
150081 if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E';
150082 if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
150083 if( pTerm->wtFlags & TERM_CODED ) zType[3] = 'C';
150084 if( pTerm->eOperator & WO_SINGLE ){
150086 pTerm->leftCursor, pTerm->u.x.leftColumn);
150087 }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){
150089 pTerm->u.pOrInfo->indexable);
150091 sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor);
150094 "TERM-%-3d %p %s %-12s op=%03x wtFlags=%04x",
150095 iTerm, pTerm, zType, zLeft, pTerm->eOperator, pTerm->wtFlags);
150099 sqlite3DebugPrintf(" prob=%-3d prereq=%llx,%llx",
150100 pTerm->truthProb, (u64)pTerm->prereqAll, (u64)pTerm->prereqRight);
150102 if( pTerm->u.x.iField ){
150103 sqlite3DebugPrintf(" iField=%d", pTerm->u.x.iField);
150105 if( pTerm->iParent>=0 ){
150106 sqlite3DebugPrintf(" iParent=%d", pTerm->iParent);
150109 sqlite3TreeViewExpr(0, pTerm->pExpr, 0);
150120 for(i=0; i<pWC->nTerm; i++){
150121 sqlite3WhereTermPrint(&pWC->a[i], i);
150131 WhereInfo *pWInfo = pWC->pWInfo;
150132 int nb = 1+(pWInfo->pTabList->nSrc+3)/4;
150133 SrcItem *pItem = pWInfo->pTabList->a + p->iTab;
150134 Table *pTab = pItem->pTab;
150135 Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1;
150136 sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
150137 p->iTab, nb, p->maskSelf, nb, p->prereq & mAll);
150139 pItem->zAlias ? pItem->zAlias : pTab->zName);
150140 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
150142 if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
150144 int i = sqlite3Strlen30(zName) - 1;
150145 while( zName[i]!='_' ) i--;
150148 sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
150154 if( p->u.vtab.idxStr ){
150156 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
150158 z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
150160 sqlite3DebugPrintf(" %-19s", z);
150163 if( p->wsFlags & WHERE_SKIPSCAN ){
150164 sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip);
150166 sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm);
150168 sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
150169 if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){
150171 for(i=0; i<p->nLTerm; i++){
150172 sqlite3WhereTermPrint(p->aLTerm[i], i);
150183 p->aLTerm = p->aLTermSpace;
150184 p->nLTerm = 0;
150185 p->nLSlot = ArraySize(p->aLTermSpace);
150186 p->wsFlags = 0;
150193 if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
150194 if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
150195 sqlite3_free(p->u.vtab.idxStr);
150196 p->u.vtab.needFree = 0;
150197 p->u.vtab.idxStr = 0;
150198 }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
150199 sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
150200 sqlite3DbFreeNN(db, p->u.btree.pIndex);
150201 p->u.btree.pIndex = 0;
150210 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
150216 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
150220 if( p->nLSlot>=n ) return SQLITE_OK;
150222 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
150224 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
150225 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm);
150226 p->aLTerm = paNew;
150227 p->nLSlot = n;
150236 if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
150237 memset(&pTo->u, 0, sizeof(pTo->u));
150241 memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
150242 if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
150243 pFrom->u.vtab.needFree = 0;
150244 }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
150245 pFrom->u.btree.pIndex = 0;
150264 for(i=0; i<pWInfo->nLevel; i++){
150265 WhereLevel *pLevel = &pWInfo->a[i];
150266 if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
150267 sqlite3DbFree(db, pLevel->u.in.aInLoop);
150270 sqlite3WhereClauseClear(&pWInfo->sWC);
150271 while( pWInfo->pLoops ){
150272 WhereLoop *p = pWInfo->pLoops;
150273 pWInfo->pLoops = p->pNextLoop;
150276 assert( pWInfo->pExprMods==0 );
150293 ** was added because if X uses skip-scan less than Y it still might
150296 ** than a non-covering index even if it is a proper subset.
150303 if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
150306 if( pY->nSkip > pX->nSkip ) return 0;
150307 if( pX->rRun >= pY->rRun ){
150308 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
150309 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
150311 for(i=pX->nLTerm-1; i>=0; i--){
150312 if( pX->aLTerm[i]==0 ) continue;
150313 for(j=pY->nLTerm-1; j>=0; j--){
150314 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
150318 if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
150319 && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
150340 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
150341 for(; p; p=p->pNextLoop){
150342 if( p->iTab!=pTemplate->iTab ) continue;
150343 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
150348 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1));
150349 pTemplate->rRun = p->rRun;
150350 pTemplate->nOut = p->nOut - 1;
150355 pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1));
150356 pTemplate->rRun = p->rRun;
150357 pTemplate->nOut = p->nOut + 1;
150381 for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
150382 if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
150391 assert( p->rSetup==0 || pTemplate->rSetup==0
150392 || p->rSetup==pTemplate->rSetup );
150396 ** rSetup. Call this SETUP-INVARIANT */
150397 assert( p->rSetup>=pTemplate->rSetup );
150399 /* Any loop using an appliation-defined index (or PRIMARY KEY or
150401 ** than an automatic index. Unless it is a skip-scan. */
150402 if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
150403 && (pTemplate->nSkip)==0
150404 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
150405 && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
150406 && (p->prereq & pTemplate->prereq)==pTemplate->prereq
150416 if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
150417 && p->rSetup<=pTemplate->rSetup /* (2a) */
150418 && p->rRun<=pTemplate->rRun /* (2b) */
150419 && p->nOut<=pTemplate->nOut /* (2c) */
150429 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
150430 && p->rRun>=pTemplate->rRun /* (2a) */
150431 && p->nOut>=pTemplate->nOut /* (2b) */
150433 assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
150449 ** If pBuilder->pOrSet is not NULL then we care about only the
150451 ** information is gathered in the pBuilder->pOrSet object. This special
150454 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
150466 WhereInfo *pWInfo = pBuilder->pWInfo;
150467 sqlite3 *db = pWInfo->pParse->db;
150471 if( pBuilder->iPlanLimit==0 ){
150473 if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0;
150476 pBuilder->iPlanLimit--;
150478 whereLoopAdjustCost(pWInfo->pLoops, pTemplate);
150480 /* If pBuilder->pOrSet is defined, then only keep track of the costs
150483 if( pBuilder->pOrSet!=0 ){
150484 if( pTemplate->nLTerm ){
150486 u16 n = pBuilder->pOrSet->n;
150489 whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
150490 pTemplate->nOut);
150493 sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n);
150494 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
150503 ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
150511 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
150527 sqlite3WhereLoopPrint(p, pBuilder->pWC);
150532 sqlite3WhereLoopPrint(pTemplate, pBuilder->pWC);
150540 p->pNextLoop = 0;
150545 WhereLoop **ppTail = &p->pNextLoop;
150552 *ppTail = pToDel->pNextLoop;
150556 sqlite3WhereLoopPrint(pToDel, pBuilder->pWC);
150563 if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
150564 Index *pIndex = p->u.btree.pIndex;
150565 if( pIndex && pIndex->idxType==SQLITE_IDXTYPE_IPK ){
150566 p->u.btree.pIndex = 0;
150585 ** TODO --> Perhaps this is something that could be improved by better
150589 ** value corresponds to -1 in LogEst notation, so this means decrement
150596 ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the
150597 ** "x" column is boolean or else -1 or 0 or 1 is a common default value
150607 Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
150609 LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */
150611 assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
150612 for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
150614 if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
150615 if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
150616 if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
150617 for(j=pLoop->nLTerm-1; j>=0; j--){
150618 pX = pLoop->aLTerm[j];
150621 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
150624 if( pTerm->truthProb<=0 ){
150627 pLoop->nOut += pTerm->truthProb;
150631 pLoop->nOut--;
150632 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0
150633 && (pTerm->wtFlags & TERM_HIGHTRUTH)==0 /* tag-20200224-1 */
150635 Expr *pRight = pTerm->pExpr->pRight;
150637 testcase( pTerm->pExpr->op==TK_IS );
150638 if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
150644 pTerm->wtFlags |= TERM_HEURTRUTH;
150651 if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce;
150678 int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft);
150681 nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
150688 Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
150689 Expr *pRhs = pTerm->pExpr->pRight;
150690 if( pRhs->flags & EP_xIsSelect ){
150691 pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
150693 pRhs = pRhs->x.pList->a[i].pExpr;
150700 if( pLhs->op!=TK_COLUMN
150701 || pLhs->iTable!=iCur
150702 || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
150703 || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
150708 testcase( pLhs->iColumn==XN_ROWID );
150710 idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
150715 if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
150722 ** compiled with -DSQLITE_ENABLE_COSTMULT
150731 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
150734 ** When this function is called, pBuilder->pNew->nOut contains the
150739 ** If pProbe->idxType==SQLITE_IDXTYPE_IPK, that means pIndex is
150748 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
150749 Parse *pParse = pWInfo->pParse; /* Parsing context */
150750 sqlite3 *db = pParse->db; /* Database connection malloc context */
150755 Bitmask saved_prereq; /* Original value of pNew->prereq */
150756 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
150757 u16 saved_nEq; /* Original value of pNew->u.btree.nEq */
150758 u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */
150759 u16 saved_nTop; /* Original value of pNew->u.btree.nTop */
150760 u16 saved_nSkip; /* Original value of pNew->nSkip */
150761 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
150762 LogEst saved_nOut; /* Original value of pNew->nOut */
150768 pNew = pBuilder->pNew;
150769 if( db->mallocFailed ) return SQLITE_NOMEM_BKPT;
150771 pProbe->pTable->zName,pProbe->zName,
150772 pNew->u.btree.nEq, pNew->nSkip, pNew->rRun));
150774 assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
150775 assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
150776 if( pNew->wsFlags & WHERE_BTM_LIMIT ){
150779 assert( pNew->u.btree.nBtm==0 );
150782 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
150784 assert( pNew->u.btree.nEq<pProbe->nColumn );
150786 saved_nEq = pNew->u.btree.nEq;
150787 saved_nBtm = pNew->u.btree.nBtm;
150788 saved_nTop = pNew->u.btree.nTop;
150789 saved_nSkip = pNew->nSkip;
150790 saved_nLTerm = pNew->nLTerm;
150791 saved_wsFlags = pNew->wsFlags;
150792 saved_prereq = pNew->prereq;
150793 saved_nOut = pNew->nOut;
150794 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq,
150796 pNew->rSetup = 0;
150797 rSize = pProbe->aiRowLogEst[0];
150800 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
150805 int nRecValid = pBuilder->nRecValid;
150807 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
150812 if( pTerm->prereqRight & pNew->maskSelf ) continue;
150816 if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue;
150818 /* tag-20191211-001: Do not allow constraints from the WHERE clause to
150820 ** ON clause are allowed. See tag-20191211-002 for the vtab equivalent. */
150821 if( (pSrc->fg.jointype & JT_LEFT)!=0
150822 && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
150827 if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
150828 pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
150830 pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
150832 pNew->wsFlags = saved_wsFlags;
150833 pNew->u.btree.nEq = saved_nEq;
150834 pNew->u.btree.nBtm = saved_nBtm;
150835 pNew->u.btree.nTop = saved_nTop;
150836 pNew->nLTerm = saved_nLTerm;
150837 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
150838 pNew->aLTerm[pNew->nLTerm++] = pTerm;
150839 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
150842 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
150843 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
150844 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
150848 Expr *pExpr = pTerm->pExpr;
150859 for(i=0; i<pNew->nLTerm-1; i++){
150860 if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
150862 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
150864 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
150866 if( pProbe->hasStat1 && rLogSize>=10 ){
150873 ** the left-most index column, M==N.
150883 ** with the index, as using an index has better worst-case behavior.
150888 M = pProbe->aiRowLogEst[saved_nEq];
150894 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
150895 pNew->wsFlags |= WHERE_IN_SEEKSCAN;
150899 saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize));
150902 pNew->wsFlags |= WHERE_COLUMN_IN;
150904 int iCol = pProbe->aiColumn[saved_nEq];
150905 pNew->wsFlags |= WHERE_COLUMN_EQ;
150906 assert( saved_nEq==pNew->u.btree.nEq );
150908 || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
150910 if( iCol==XN_ROWID || pProbe->uniqNotNull
150911 || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
150913 pNew->wsFlags |= WHERE_ONEROW;
150915 pNew->wsFlags |= WHERE_UNQ_WANTED;
150919 pNew->wsFlags |= WHERE_COLUMN_NULL;
150923 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
150924 pNew->u.btree.nBtm = whereRangeVectorLen(
150925 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
150929 if( pTerm->wtFlags & TERM_LIKEOPT ){
150933 assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm );
150934 assert( pTop->wtFlags & TERM_LIKEOPT );
150935 assert( pTop->eOperator==WO_LT );
150936 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
150937 pNew->aLTerm[pNew->nLTerm++] = pTop;
150938 pNew->wsFlags |= WHERE_TOP_LIMIT;
150939 pNew->u.btree.nTop = 1;
150945 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
150946 pNew->u.btree.nTop = whereRangeVectorLen(
150947 pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm
150950 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
150951 pNew->aLTerm[pNew->nLTerm-2] : 0;
150954 /* At this point pNew->nOut is set to the number of rows expected to
150958 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
150959 assert( pNew->nOut==saved_nOut );
150960 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
150965 int nEq = ++pNew->u.btree.nEq;
150968 assert( pNew->nOut==saved_nOut );
150969 if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
150972 pNew->nOut += pTerm->truthProb;
150973 pNew->nOut -= nIn;
150978 && pProbe->nSample
150979 && pNew->u.btree.nEq<=pProbe->nSampleCol
150980 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
150983 Expr *pExpr = pTerm->pExpr;
150988 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
150990 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
150995 pNew->nOut = sqlite3LogEst(nOut);
150999 ** See tag-202002240-1 */
151000 && pNew->nOut+10 > pProbe->aiRowLogEst[0]
151009 pTerm->wtFlags |= TERM_HIGHTRUTH;
151010 if( pTerm->wtFlags & TERM_HEURTRUTH ){
151014 pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
151017 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
151018 pNew->nOut -= nIn;
151024 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
151029 pNew->nOut += 10;
151036 ** it to pNew->rRun, which is currently set to the cost of the index
151037 ** seek only. Then, if this is a non-covering index, add the cost of
151039 assert( pSrc->pTab->szTabRow>0 );
151040 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
151041 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
151042 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
151043 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
151045 ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);
151047 nOutUnadjusted = pNew->nOut;
151048 pNew->rRun += nInMul + nIn;
151049 pNew->nOut += nInMul + nIn;
151050 whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
151053 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
151054 pNew->nOut = saved_nOut;
151056 pNew->nOut = nOutUnadjusted;
151059 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
151060 && pNew->u.btree.nEq<pProbe->nColumn
151064 pNew->nOut = saved_nOut;
151066 pBuilder->nRecValid = nRecValid;
151069 pNew->prereq = saved_prereq;
151070 pNew->u.btree.nEq = saved_nEq;
151071 pNew->u.btree.nBtm = saved_nBtm;
151072 pNew->u.btree.nTop = saved_nTop;
151073 pNew->nSkip = saved_nSkip;
151074 pNew->wsFlags = saved_wsFlags;
151075 pNew->nOut = saved_nOut;
151076 pNew->nLTerm = saved_nLTerm;
151078 /* Consider using a skip-scan if there are no WHERE clause constraints
151079 ** available for the left-most terms of the index, and if the average
151080 ** number of repeats in the left-most terms is at least 18.
151090 && saved_nEq+1<pProbe->nKeyCol
151091 && saved_nEq==pNew->nLTerm
151092 && pProbe->noSkipScan==0
151093 && pProbe->hasStat1!=0
151095 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
151096 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
151099 pNew->u.btree.nEq++;
151100 pNew->nSkip++;
151101 pNew->aLTerm[pNew->nLTerm++] = 0;
151102 pNew->wsFlags |= WHERE_SKIPSCAN;
151103 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
151104 pNew->nOut -= nIter;
151105 /* TUNING: Because uncertainties in the estimates for skip-scan queries,
151106 ** add a 1.375 fudge factor to make skip-scan slightly less likely. */
151109 pNew->nOut = saved_nOut;
151110 pNew->u.btree.nEq = saved_nEq;
151111 pNew->nSkip = saved_nSkip;
151112 pNew->wsFlags = saved_wsFlags;
151116 pProbe->pTable->zName, pProbe->zName, saved_nEq, rc));
151137 if( pIndex->bUnordered ) return 0;
151138 if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
151139 for(ii=0; ii<pOB->nExpr; ii++){
151140 Expr *pExpr = sqlite3ExprSkipCollateAndLikely(pOB->a[ii].pExpr);
151142 if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
151143 if( pExpr->iColumn<0 ) return 1;
151144 for(jj=0; jj<pIndex->nKeyCol; jj++){
151145 if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
151147 }else if( (aColExpr = pIndex->aColExpr)!=0 ){
151148 for(jj=0; jj<pIndex->nKeyCol; jj++){
151149 if( pIndex->aiColumn[jj]!=XN_EXPR ) continue;
151150 if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
151170 Parse *pParse = pWC->pWInfo->pParse;
151171 while( pWhere->op==TK_AND ){
151172 if( !whereUsablePartialIndex(iTab,isLeft,pWC,pWhere->pLeft) ) return 0;
151173 pWhere = pWhere->pRight;
151175 if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
151176 for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
151178 pExpr = pTerm->pExpr;
151179 if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
151191 ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be
151192 ** a b-tree table, not a virtual table.
151194 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
151199 ** cost = nRow * 3.0 // full-table scan
151201 ** cost = nRow * (K+3.0) // scan of non-covering index
151208 ** the index b-tree:
151211 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
151215 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
151220 ** log(nRow) factor is omitted from a non-covering index scan in order to
151221 ** bias the scoring in favor of using an index, since the worst-case
151222 ** performance of using an index is far better than the worst-case performance
151233 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
151245 pNew = pBuilder->pNew;
151246 pWInfo = pBuilder->pWInfo;
151247 pTabList = pWInfo->pTabList;
151248 pSrc = pTabList->a + pNew->iTab;
151249 pTab = pSrc->pTab;
151250 pWC = pBuilder->pWC;
151251 assert( !IsVirtual(pSrc->pTab) );
151253 if( pSrc->fg.isIndexedBy ){
151255 pProbe = pSrc->u2.pIBIndex;
151257 pProbe = pTab->pIndex;
151271 sPk.szIdxRow = pTab->szTabRow;
151273 aiRowEstPk[0] = pTab->nRowLogEst;
151275 pFirst = pSrc->pTab->pIndex;
151276 if( pSrc->fg.notIndexed==0 ){
151283 rSize = pTab->nRowLogEst;
151288 if( !pBuilder->pOrSet /* Not part of an OR optimization */
151289 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
151290 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
151291 && !pSrc->fg.isIndexedBy /* Has no INDEXED BY clause */
151292 && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
151294 && !pSrc->fg.isCorrelated /* Not a correlated subquery */
151295 && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
151297 /* Generate auto-index WhereLoops */
151299 WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
151300 for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
151301 if( pTerm->prereqRight & pNew->maskSelf ) continue;
151303 pNew->u.btree.nEq = 1;
151304 pNew->nSkip = 0;
151305 pNew->u.btree.pIndex = 0;
151306 pNew->nLTerm = 1;
151307 pNew->aLTerm[0] = pTerm;
151308 /* TUNING: One-time cost for computing the automatic index is
151311 ** tables or 0.5 (LogEst=-10) for views and subqueries. The value
151316 pNew->rSetup = rLogSize + rSize;
151317 if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
151318 pNew->rSetup += 28;
151320 pNew->rSetup -= 10;
151322 ApplyCostMultiplier(pNew->rSetup, pTab->costMult);
151323 if( pNew->rSetup<0 ) pNew->rSetup = 0;
151328 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
151329 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
151330 pNew->wsFlags = WHERE_AUTO_INDEX;
151331 pNew->prereq = mPrereq | pTerm->prereqRight;
151341 pProbe=(pSrc->fg.isIndexedBy ? 0 : pProbe->pNext), iSortIdx++
151343 int isLeft = (pSrc->fg.jointype & JT_OUTER)!=0;
151344 if( pProbe->pPartIdxWhere!=0
151345 && !whereUsablePartialIndex(pSrc->iCursor, isLeft, pWC,
151346 pProbe->pPartIdxWhere)
151348 testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */
151351 if( pProbe->bNoQuery ) continue;
151352 rSize = pProbe->aiRowLogEst[0];
151353 pNew->u.btree.nEq = 0;
151354 pNew->u.btree.nBtm = 0;
151355 pNew->u.btree.nTop = 0;
151356 pNew->nSkip = 0;
151357 pNew->nLTerm = 0;
151358 pNew->iSortIdx = 0;
151359 pNew->rSetup = 0;
151360 pNew->prereq = mPrereq;
151361 pNew->nOut = rSize;
151362 pNew->u.btree.pIndex = pProbe;
151363 b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
151366 assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
151367 if( pProbe->idxType==SQLITE_IDXTYPE_IPK ){
151369 pNew->wsFlags = WHERE_IPK;
151372 pNew->iSortIdx = b ? iSortIdx : 0;
151375 ** since index lookups have better worst-case performance if our
151386 pNew->rRun = rSize + 16 - 2*((pTab->tabFlags & TF_HasStat4)!=0);
151388 pNew->rRun = rSize + 16;
151390 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
151393 pNew->nOut = rSize;
151397 if( pProbe->isCovering ){
151398 pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
151401 m = pSrc->colUsed & pProbe->colNotIdxed;
151402 pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
151408 || pProbe->pPartIdxWhere!=0
151409 || pSrc->fg.isIndexedBy
151411 && pProbe->bUnordered==0
151412 && (pProbe->szIdxRow<pTab->szTabRow)
151413 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
151415 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
151418 pNew->iSortIdx = b ? iSortIdx : 0;
151423 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
151425 /* If this is a non-covering index scan, add in the cost of
151432 int iCur = pSrc->iCursor;
151433 WhereClause *pWC2 = &pWInfo->sWC;
151434 for(ii=0; ii<pWC2->nTerm; ii++){
151435 WhereTerm *pTerm = &pWC2->a[ii];
151436 if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
151441 if( pTerm->truthProb<=0 ){
151442 nLookup += pTerm->truthProb;
151444 nLookup--;
151445 if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19;
151449 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup);
151451 ApplyCostMultiplier(pNew->rRun, pTab->costMult);
151454 pNew->nOut = rSize;
151459 pBuilder->bldFlags1 = 0;
151461 if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
151462 /* If a non-unique index is used, or if a prefix of the key for
151463 ** unique index is used (making the index functionally non-unique)
151466 pTab->tabFlags |= TF_StatsUsed;
151469 sqlite3Stat4ProbeFree(pBuilder->pRec);
151470 pBuilder->nRecValid = 0;
151471 pBuilder->pRec = 0;
151481 ** be used by the virtual table identified by pBuilder->pNew->iTab. This
151508 WhereClause *pWC = pBuilder->pWC;
151510 struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage;
151514 WhereLoop *pNew = pBuilder->pNew;
151515 Parse *pParse = pBuilder->pWInfo->pParse;
151516 SrcItem *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab];
151517 int nConstraint = pIdxInfo->nConstraint;
151521 pNew->prereq = mPrereq;
151525 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
151527 WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset];
151528 pIdxCons->usable = 0;
151529 if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
151530 && (pTerm->eOperator & mExclude)==0
151532 pIdxCons->usable = 1;
151538 assert( pIdxInfo->needToFreeIdxStr==0 );
151539 pIdxInfo->idxStr = 0;
151540 pIdxInfo->idxNum = 0;
151541 pIdxInfo->orderByConsumed = 0;
151542 pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
151543 pIdxInfo->estimatedRows = 25;
151544 pIdxInfo->idxFlags = 0;
151545 pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed;
151548 rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo);
151555 WHERETRACE(0xffff, (" ^^^^--- non-viable plan rejected!\n"));
151561 mxTerm = -1;
151562 assert( pNew->nLSlot>=nConstraint );
151563 for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
151564 pNew->u.vtab.omitMask = 0;
151565 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
151568 if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
151570 int j = pIdxCons->iTermOffset;
151573 || j>=pWC->nTerm
151574 || pNew->aLTerm[iTerm]!=0
151575 || pIdxCons->usable==0
151577 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
151578 testcase( pIdxInfo->needToFreeIdxStr );
151581 testcase( iTerm==nConstraint-1 );
151583 testcase( j==pWC->nTerm-1 );
151584 pTerm = &pWC->a[j];
151585 pNew->prereq |= pTerm->prereqRight;
151586 assert( iTerm<pNew->nLSlot );
151587 pNew->aLTerm[iTerm] = pTerm;
151594 pNew->u.vtab.omitMask |= 1<<iTerm;
151599 if( (pTerm->eOperator & WO_IN)!=0 ){
151605 pIdxInfo->orderByConsumed = 0;
151606 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
151612 pNew->nLTerm = mxTerm+1;
151614 if( pNew->aLTerm[i]==0 ){
151615 /* The non-zero argvIdx values must be contiguous. Raise an
151617 sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName);
151618 testcase( pIdxInfo->needToFreeIdxStr );
151622 assert( pNew->nLTerm<=pNew->nLSlot );
151623 pNew->u.vtab.idxNum = pIdxInfo->idxNum;
151624 pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
151625 pIdxInfo->needToFreeIdxStr = 0;
151626 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
151627 pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
151628 pIdxInfo->nOrderBy : 0);
151629 pNew->rSetup = 0;
151630 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
151631 pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
151635 if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
151636 pNew->wsFlags |= WHERE_ONEROW;
151638 pNew->wsFlags &= ~WHERE_ONEROW;
151641 if( pNew->u.vtab.needFree ){
151642 sqlite3_free(pNew->u.vtab.idxStr);
151643 pNew->u.vtab.needFree = 0;
151647 (sqlite3_uint64)(pNew->prereq & ~mPrereq)));
151662 if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
151664 int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
151665 Expr *pX = pHidden->pWC->a[iTerm].pExpr;
151666 if( pX->pLeft ){
151667 pC = sqlite3ExprCompareCollSeq(pHidden->pParse, pX);
151669 zRet = (pC ? pC->zName : sqlite3StrBINARY);
151676 ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table.
151697 ** mUnusable should always be configured as "not-usable" for xBestIndex.
151717 pWInfo = pBuilder->pWInfo;
151718 pParse = pWInfo->pParse;
151719 pWC = pBuilder->pWC;
151720 pNew = pBuilder->pNew;
151721 pSrc = &pWInfo->pTabList->a[pNew->iTab];
151722 assert( IsVirtual(pSrc->pTab) );
151723 p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy,
151726 pNew->rSetup = 0;
151727 pNew->wsFlags = WHERE_VIRTUALTABLE;
151728 pNew->nLTerm = 0;
151729 pNew->u.vtab.needFree = 0;
151730 nConstraint = p->nConstraint;
151731 if( whereLoopResize(pParse->db, pNew, nConstraint) ){
151732 sqlite3DbFree(pParse->db, p);
151737 WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName));
151746 if( rc==SQLITE_OK && ((mBest = (pNew->prereq & ~mPrereq))!=0 || bIn) ){
151759 mBestNoIn = pNew->prereq & ~mPrereq;
151774 pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq
151785 if( pNew->prereq==mPrereq ){
151811 if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr);
151812 sqlite3DbFreeNN(pParse->db, p);
151813 WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc));
151827 WhereInfo *pWInfo = pBuilder->pWInfo;
151838 pWC = pBuilder->pWC;
151839 pWCEnd = pWC->a + pWC->nTerm;
151840 pNew = pBuilder->pNew;
151842 pItem = pWInfo->pTabList->a + pNew->iTab;
151843 iCur = pItem->iCursor;
151845 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
151846 if( (pTerm->eOperator & WO_OR)!=0
151847 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
151849 WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
151850 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
151859 WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm));
151860 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
151861 if( (pOrTerm->eOperator & WO_AND)!=0 ){
151862 sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
151863 }else if( pOrTerm->leftCursor==iCur ){
151864 tempWC.pWInfo = pWC->pWInfo;
151875 WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n",
151876 (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm));
151882 if( IsVirtual(pItem->pTab) ){
151913 pNew->nLTerm = 1;
151914 pNew->aLTerm[0] = pTerm;
151915 pNew->wsFlags = WHERE_MULTI_OR;
151916 pNew->rSetup = 0;
151917 pNew->iSortIdx = 0;
151918 memset(&pNew->u, 0, sizeof(pNew->u));
151921 ** of all sub-scans required by the OR-scan. However, due to rounding
151922 ** errors, it may be that the cost of the OR-scan is equal to its
151923 ** most expensive sub-scan. Add the smallest possible penalty
151930 ** the planner may elect to "OR" together a full-table scan and an
151932 pNew->rRun = sSum.a[i].rRun + 1;
151933 pNew->nOut = sSum.a[i].nOut;
151934 pNew->prereq = sSum.a[i].prereq;
151937 WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm));
151947 WhereInfo *pWInfo = pBuilder->pWInfo;
151951 SrcList *pTabList = pWInfo->pTabList;
151953 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
151954 sqlite3 *db = pWInfo->pParse->db;
151959 pNew = pBuilder->pNew;
151961 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
151962 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
151964 pNew->iTab = iTab;
151965 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
151966 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
151967 if( (pItem->fg.jointype & (JT_LEFT|JT_CROSS))!=0 ){
151969 ** right-hand-side of a LEFT or CROSS JOIN. */
151975 if( IsVirtual(pItem->pTab) ){
151978 if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
151979 mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
151988 if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
151991 mPrior |= pNew->maskSelf;
151992 if( rc || db->mallocFailed ){
152022 ** pOrderBy terms must be matched in strict left-to-right order.
152029 u16 nLoop, /* Number of entries in pPath->aLoop[] */
152030 WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */
152036 u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */
152052 sqlite3 *db = pWInfo->pParse->db; /* Database connection */
152055 Bitmask orderDistinctMask; /* Mask of all well-ordered loops */
152059 ** We say the WhereLoop is "one-row" if it generates no more than one
152060 ** row of output. A WhereLoop is one-row if all of the following are true:
152063 ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
152064 ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
152066 ** We say the WhereLoop is "order-distinct" if the set of columns from
152068 ** row of the WhereLoop. Every one-row WhereLoop is automatically
152069 ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause
152070 ** is not order-distinct. To be order-distinct is not quite the same as being
152072 ** are NULL and NULL values are equivalent for the purpose of order-distinct.
152073 ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
152077 ** automatically order-distinct.
152083 nOrderBy = pOrderBy->nExpr;
152084 testcase( nOrderBy==BMS-1 );
152085 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
152087 obDone = MASKBIT(nOrderBy)-1;
152095 if( iLoop>0 ) ready |= pLoop->maskSelf;
152097 pLoop = pPath->aLoop[iLoop];
152102 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
152103 if( pLoop->u.vtab.isOrdered && (wctrlFlags & WHERE_DISTINCTBY)==0 ){
152108 pLoop->u.btree.nDistinctCol = 0;
152110 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
152119 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
152121 if( pOBExpr->op!=TK_COLUMN ) continue;
152122 if( pOBExpr->iTable!=iCur ) continue;
152123 pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
152126 if( pTerm->eOperator==WO_IN ){
152132 for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){}
152133 if( j>=pLoop->nLTerm ) continue;
152135 if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
152136 Parse *pParse = pWInfo->pParse;
152137 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[i].pExpr);
152138 CollSeq *pColl2 = sqlite3ExprCompareCollSeq(pParse, pTerm->pExpr);
152140 if( pColl2==0 || sqlite3StrICmp(pColl1->zName, pColl2->zName) ){
152143 testcase( pTerm->pExpr->op==TK_IS );
152148 if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
152149 if( pLoop->wsFlags & WHERE_IPK ){
152153 }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
152156 nKeyCol = pIndex->nKeyCol;
152157 nColumn = pIndex->nColumn;
152158 assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
152159 assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
152160 || !HasRowid(pIndex->pTable));
152162 && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
152173 assert( j>=pLoop->u.btree.nEq
152174 || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip)
152176 if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
152177 u16 eOp = pLoop->aLTerm[j]->eOperator;
152182 ** the loop need to be marked as not order-distinct because it can
152201 ** j<pLoop->u.btree.nEq constraint above. Any equality other
152204 Expr *pX = pLoop->aLTerm[j]->pExpr;
152205 for(i=j+1; i<pLoop->u.btree.nEq; i++){
152206 if( pLoop->aLTerm[i]->pExpr==pX ){
152207 assert( (pLoop->aLTerm[i]->eOperator & WO_IN) );
152216 ** (revIdx) for the j-th column of the index.
152219 iColumn = pIndex->aiColumn[j];
152220 revIdx = pIndex->aSortOrder[j] & KEYINFO_ORDER_DESC;
152221 if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID;
152228 ** WhereLoop is not well-ordered
152232 && j>=pLoop->u.btree.nEq
152233 && pIndex->pTable->aCol[iColumn].notNull==0
152238 /* Find the ORDER BY term that corresponds to the j-th column
152244 pOBExpr = sqlite3ExprSkipCollateAndLikely(pOrderBy->a[i].pExpr);
152250 if( pOBExpr->op!=TK_COLUMN ) continue;
152251 if( pOBExpr->iTable!=iCur ) continue;
152252 if( pOBExpr->iColumn!=iColumn ) continue;
152254 Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr;
152260 pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
152261 if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
152264 pLoop->u.btree.nDistinctCol = j+1;
152273 if( (rev ^ revIdx)!=(pOrderBy->a[i].sortFlags&KEYINFO_ORDER_DESC) ){
152277 rev = revIdx ^ (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_DESC);
152282 if( isMatch && (pOrderBy->a[i].sortFlags & KEYINFO_ORDER_BIGNULL) ){
152283 if( j==pLoop->u.btree.nEq ){
152284 pLoop->wsFlags |= WHERE_BIGNULL_SORT;
152308 } /* end-if not one-row */
152312 orderDistinctMask |= pLoop->maskSelf;
152317 p = pOrderBy->a[i].pExpr;
152318 mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
152325 } /* End the loop over all WhereLoops from outer-most down to inner-most */
152328 for(i=nOrderBy-1; i>0; i--){
152329 Bitmask m = MASKBIT(i) - 1;
152334 return -1;
152341 ** BY clause - and so any order that groups rows as required satisfies the
152358 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
152359 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
152362 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
152363 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
152364 return pWInfo->sorted;
152372 for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
152373 if( pLast ) zName[i++] = pLast->cId;
152395 ** Or, if the order-by clause has X terms but only the last Y
152396 ** terms are out of order, then block-sorting will reduce the
152406 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
152414 if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
152415 nRow = pWInfo->iLimit;
152416 }else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
152419 if( nRow>10 ) nRow -= 10; assert( 10==sqlite3LogEst(2) );
152426 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
152428 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
152459 pParse = pWInfo->pParse;
152460 db = pParse->db;
152461 nLoop = pWInfo->nLevel;
152463 ** For 2-way joins, the 5 best paths are followed.
152466 assert( nLoop<=pWInfo->pTabList->nSrc );
152467 WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst));
152474 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
152477 nOrderBy = pWInfo->pOrderBy->nExpr;
152489 for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
152490 pFrom->aLoop = pX;
152495 ** is either zero - meaning it has not yet been initialized - or the
152510 aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
152518 ** -1, indicating that the result set may or may not be ordered,
152520 aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
152529 for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
152533 i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */
152535 Bitmask revMask = 0; /* Mask of rev-order loops for (..) */
152537 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
152538 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
152539 if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
152550 rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
152551 rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted);
152552 nOut = pFrom->nRow + pWLoop->nOut;
152553 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
152556 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
152559 revMask = pFrom->revLoop;
152574 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
152575 aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy,
152579 rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */
152583 ** mxChoice best-so-far paths.
152585 ** First look for an existing path among best-so-far paths
152589 ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent
152590 ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range
152591 ** of legal values for isOrdered, -1..64.
152594 if( pTo->maskLoop==maskNew
152595 && ((pTo->isOrdered^isOrdered)&0x80)==0
152597 testcase( jj==nTo-1 );
152602 /* None of the existing best-so-far paths match the candidate. */
152607 ** paths currently in the best-so-far buffer. So discard
152611 sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
152619 ** needs to be added to the set of best-so-far paths. */
152630 sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
152636 /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
152642 ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
152644 if( pTo->rCost<rCost
152645 || (pTo->rCost==rCost
152646 && (pTo->nRow<nOut
152647 || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
152654 "Skip %s cost=%-3d,%3d,%3d order=%c",
152657 sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
152658 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
152659 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
152663 testcase( pTo->rCost==rCost );
152666 testcase( pTo->rCost==rCost+1 );
152672 "Update %s cost=%-3d,%3d,%3d order=%c",
152675 sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
152676 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
152677 pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
152682 pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
152683 pTo->revLoop = revMask;
152684 pTo->nRow = nOut;
152685 pTo->rCost = rCost;
152686 pTo->rUnsorted = rUnsorted;
152687 pTo->isOrdered = isOrdered;
152688 memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
152689 pTo->aLoop[iLoop] = pWLoop;
152695 if( pTo->rCost>mxCost
152696 || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
152698 mxCost = pTo->rCost;
152699 mxUnsorted = pTo->rUnsorted;
152709 sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
152711 sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
152712 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
152713 pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?');
152714 if( pTo->isOrdered>0 ){
152715 sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
152739 if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
152741 assert( pWInfo->nLevel==nLoop );
152744 WhereLevel *pLevel = pWInfo->a + iLoop;
152745 pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
152746 pLevel->iFrom = pWLoop->iTab;
152747 pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
152749 if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
152750 && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
152751 && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
152755 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
152756 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
152757 if( rc==pWInfo->pResultSet->nExpr ){
152758 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
152761 pWInfo->bOrderedInnerLoop = 0;
152762 if( pWInfo->pOrderBy ){
152763 if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
152764 if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
152765 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
152768 pWInfo->nOBSat = pFrom->isOrdered;
152769 pWInfo->revMask = pFrom->revLoop;
152770 if( pWInfo->nOBSat<=0 ){
152771 pWInfo->nOBSat = 0;
152773 u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags;
152778 int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom,
152779 WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m);
152782 if( rc==pWInfo->pOrderBy->nExpr ){
152783 pWInfo->bOrderedInnerLoop = 1;
152784 pWInfo->revMask = m;
152789 && pWInfo->nOBSat==1
152790 && (pWInfo->wctrlFlags & (WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX))!=0
152792 pWInfo->bOrderedInnerLoop = 1;
152795 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
152796 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
152799 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
152800 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask
152802 assert( pWInfo->sorted==0 );
152803 if( nOrder==pWInfo->pOrderBy->nExpr ){
152804 pWInfo->sorted = 1;
152805 pWInfo->revMask = revMask;
152811 pWInfo->nRowOut = pFrom->nRow;
152822 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
152825 ** Return non-zero on success, if this query can be handled by this
152826 ** no-frills query planner. Return zero if this query needs the
152827 ** general-purpose query planner.
152840 pWInfo = pBuilder->pWInfo;
152841 if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0;
152842 assert( pWInfo->pTabList->nSrc>=1 );
152843 pItem = pWInfo->pTabList->a;
152844 pTab = pItem->pTab;
152846 if( pItem->fg.isIndexedBy ) return 0;
152847 iCur = pItem->iCursor;
152848 pWC = &pWInfo->sWC;
152849 pLoop = pBuilder->pNew;
152850 pLoop->wsFlags = 0;
152851 pLoop->nSkip = 0;
152852 pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
152854 testcase( pTerm->eOperator & WO_IS );
152855 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
152856 pLoop->aLTerm[0] = pTerm;
152857 pLoop->nLTerm = 1;
152858 pLoop->u.btree.nEq = 1;
152860 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
152862 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
152864 assert( pLoop->aLTermSpace==pLoop->aLTerm );
152866 || pIdx->pPartIdxWhere!=0
152867 || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
152869 opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
152870 for(j=0; j<pIdx->nKeyCol; j++){
152873 testcase( pTerm->eOperator & WO_IS );
152874 pLoop->aLTerm[j] = pTerm;
152876 if( j!=pIdx->nKeyCol ) continue;
152877 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
152878 if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
152879 pLoop->wsFlags |= WHERE_IDX_ONLY;
152881 pLoop->nLTerm = j;
152882 pLoop->u.btree.nEq = j;
152883 pLoop->u.btree.pIndex = pIdx;
152885 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
152889 if( pLoop->wsFlags ){
152890 pLoop->nOut = (LogEst)1;
152891 pWInfo->a[0].pWLoop = pLoop;
152892 assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] );
152893 pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */
152894 pWInfo->a[0].iTabCur = iCur;
152895 pWInfo->nRowOut = 1;
152896 if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr;
152897 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
152898 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
152901 pLoop->cId = '0';
152912 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
152913 pWalker->eCode = 0;
152920 ** Return true if the expression contains no non-deterministic SQL
152921 ** functions. Do not consider non-deterministic SQL functions that are
152922 ** part of sub-select statements.
152945 for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
152946 p->cId = zLabel[i%(sizeof(zLabel)-1)];
152975 ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
152979 ** end |-- by sqlite3WhereEnd()
152986 ** scanning through all values on the right-hand side of the IN.
152989 ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
152999 ** entire tables. Thus a three-way join is an O(N^3) operation. But if
153009 ** inner loops (or around the "..." if the test occurs within the inner-
153057 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
153061 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
153078 db = pParse->db;
153082 testcase( pOrderBy && pOrderBy->nExpr==BMS-1 );
153083 if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0;
153095 testcase( pTabList->nSrc==BMS );
153096 if( pTabList->nSrc>BMS ){
153106 nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
153111 ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
153112 ** field (type Bitmask) it must be aligned on an 8-byte boundary on
153115 nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
153117 if( db->mallocFailed ){
153122 pWInfo->pParse = pParse;
153123 pWInfo->pTabList = pTabList;
153124 pWInfo->pOrderBy = pOrderBy;
153125 pWInfo->pWhere = pWhere;
153126 pWInfo->pResultSet = pResultSet;
153127 pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
153128 pWInfo->nLevel = nTabList;
153129 pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(pParse);
153130 pWInfo->wctrlFlags = wctrlFlags;
153131 pWInfo->iLimit = iAuxArg;
153132 pWInfo->savedNQueryLoop = pParse->nQueryLoop;
153133 memset(&pWInfo->nOBSat, 0,
153134 offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat));
153135 memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel));
153136 assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */
153137 pMaskSet = &pWInfo->sMaskSet;
153139 sWLB.pWC = &pWInfo->sWC;
153144 sWLB.pNew->cId = '*';
153151 sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
153152 sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
153157 if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr;
153159 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
153165 ** The N-th term of the FROM clause is assigned a bitmask of 1<<N.
153168 ** a table T, then X-1 is the bitmask for all other tables to the left of T.
153172 ** Note that bitmasks are created for all pTabList->nSrc tables in
153174 ** equal to pTabList->nSrc but might be shortened to 1 if the
153179 createMask(pMaskSet, pTabList->a[ii].iCursor);
153180 sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC);
153181 }while( (++ii)<pTabList->nSrc );
153185 for(ii=0; ii<pTabList->nSrc; ii++){
153186 Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
153195 sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
153196 if( db->mallocFailed ) goto whereBeginError;
153202 ** Do not do this if the expression contains non-deterministic functions
153203 ** that are not within a sub-select. This is not strictly required, but
153206 ** FROM ... WHERE random()>0; -- eval random() once per row
153207 ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall
153209 for(ii=0; ii<sWLB.pWC->nTerm; ii++){
153210 WhereTerm *pT = &sWLB.pWC->a[ii];
153211 if( pT->wtFlags & TERM_VIRTUAL ) continue;
153212 if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
153213 sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL);
153214 pT->wtFlags |= TERM_CODED;
153219 if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
153221 pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
153224 pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
153225 pWInfo->pOrderBy = pResultSet;
153249 sqlite3DebugPrintf("---- WHERE clause at start of analysis:\n");
153269 while( pWInfo->pLoops ){
153270 WhereLoop *p = pWInfo->pLoops;
153271 pWInfo->pLoops = p->pNextLoop;
153281 if( db->mallocFailed ) goto whereBeginError;
153282 if( pWInfo->pOrderBy ){
153283 wherePathSolver(pWInfo, pWInfo->nRowOut+1);
153284 if( db->mallocFailed ) goto whereBeginError;
153287 if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
153288 pWInfo->revMask = ALLBITS;
153290 if( pParse->nErr || db->mallocFailed ){
153295 sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
153296 if( pWInfo->nOBSat>0 ){
153297 sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
153299 switch( pWInfo->eDistinct ){
153314 for(ii=0; ii<pWInfo->nLevel; ii++){
153315 sqlite3WhereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
153350 if( pWInfo->nLevel>=2
153359 for(i=pWInfo->nLevel-1; i>=1; i--){
153362 pLoop = pWInfo->a[i].pWLoop;
153363 pItem = &pWInfo->pTabList->a[pLoop->iTab];
153364 if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
153366 && (pLoop->wsFlags & WHERE_ONEROW)==0
153370 if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
153371 pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
153372 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
153373 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
153374 if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
153375 || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
153382 WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
153383 notReady &= ~pLoop->maskSelf;
153384 for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
153385 if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
153386 pTerm->wtFlags |= TERM_CODED;
153389 if( i!=pWInfo->nLevel-1 ){
153390 int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
153391 memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
153393 pWInfo->nLevel--;
153394 nTabList--;
153399 sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
153404 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
153407 ** to use a one-pass algorithm, determine if this is appropriate.
153409 ** A one-pass approach can be used if the caller has requested one
153413 ** * the caller has indicated that a one-pass approach can be used
153422 ** use a one-pass approach, and this is not set accurately for scans
153425 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
153427 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
153429 assert( !(wsFlags & WHERE_VIRTUALTABLE) || IsVirtual(pTabList->a[0].pTab) );
153432 && !IsVirtual(pTabList->a[0].pTab)
153435 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
153436 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
153440 pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY);
153448 for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
153453 pTabItem = &pTabList->a[pLevel->iFrom];
153454 pTab = pTabItem->pTab;
153455 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
153456 pLoop = pLevel->pWLoop;
153457 if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
153461 if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
153463 int iCur = pTabItem->iCursor;
153469 if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
153472 if( pWInfo->eOnePass!=ONEPASS_OFF ){
153474 pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
153476 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
153477 assert( pTabItem->iCursor==pLevel->iTabCur );
153478 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 );
153479 testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS );
153480 if( pWInfo->eOnePass==ONEPASS_OFF
153481 && pTab->nCol<BMS
153482 && (pTab->tabFlags & (TF_HasGenerated|TF_WithoutRowid))==0
153487 Bitmask b = pTabItem->colUsed;
153490 sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32);
153491 assert( n<=pTab->nCol );
153494 if( pLoop->u.btree.pIndex!=0 ){
153502 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
153503 (const u8*)&pTabItem->colUsed, P4_INT64);
153506 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
153508 if( pLoop->wsFlags & WHERE_INDEXED ){
153509 Index *pIx = pLoop->u.btree.pIndex;
153513 assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
153517 /* This is one term of an OR-optimization using the PRIMARY KEY of a
153519 iIndexCur = pLevel->iTabCur;
153521 }else if( pWInfo->eOnePass!=ONEPASS_OFF ){
153522 Index *pJ = pTabItem->pTab->pIndex;
153527 pJ = pJ->pNext;
153530 pWInfo->aiCurOnePass[1] = iIndexCur;
153535 iIndexCur = pParse->nTab++;
153537 pLevel->iIdxCur = iIndexCur;
153538 assert( pIx->pSchema==pTab->pSchema );
153541 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
153543 if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
153544 && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
153545 && (pLoop->wsFlags & WHERE_BIGNULL_SORT)==0
153546 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0
153547 && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
153548 && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
153552 VdbeComment((v, "%s", pIx->zName));
153557 for(ii=0; ii<pIx->nColumn; ii++){
153558 jj = pIx->aiColumn[ii];
153561 if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue;
153572 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
153573 if( db->mallocFailed ) goto whereBeginError;
153582 pLevel = &pWInfo->a[ii];
153583 wsFlags = pLevel->pWLoop->wsFlags;
153585 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
153586 constructAutomaticIndex(pParse, &pWInfo->sWC,
153587 &pTabList->a[pLevel->iFrom], notReady, pLevel);
153588 if( db->mallocFailed ) goto whereBeginError;
153594 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
153596 pWInfo->iContinue = pLevel->addrCont;
153603 VdbeModuleComment((v, "Begin WHERE-core"));
153604 pWInfo->iEndWhere = sqlite3VdbeCurrentAddr(v);
153610 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
153623 # define OpcodeRewriteTrace(D,K,P) /* no-op */
153631 if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return;
153641 Parse *pParse = pWInfo->pParse;
153642 Vdbe *v = pParse->pVdbe;
153646 SrcList *pTabList = pWInfo->pTabList;
153647 sqlite3 *db = pParse->db;
153652 VdbeModuleComment((v, "End WHERE-core"));
153653 for(i=pWInfo->nLevel-1; i>=0; i--){
153655 pLevel = &pWInfo->a[i];
153656 pLoop = pLevel->pWLoop;
153657 if( pLevel->op!=OP_Noop ){
153662 if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
153663 && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
153664 && (pLoop->wsFlags & WHERE_INDEXED)!=0
153665 && (pIdx = pLoop->u.btree.pIndex)->hasStat1
153666 && (n = pLoop->u.btree.nDistinctCol)>0
153667 && pIdx->aiRowLogEst[n]>=36
153669 int r1 = pParse->nMem+1;
153672 sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
153674 pParse->nMem += n+1;
153675 op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
153676 addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n);
153679 sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
153683 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
153684 sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
153685 sqlite3VdbeChangeP5(v, pLevel->p5);
153687 VdbeCoverageIf(v, pLevel->op==OP_Next);
153688 VdbeCoverageIf(v, pLevel->op==OP_Prev);
153689 VdbeCoverageIf(v, pLevel->op==OP_VNext);
153690 if( pLevel->regBignull ){
153691 sqlite3VdbeResolveLabel(v, pLevel->addrBignull);
153692 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, pLevel->regBignull, pLevel->p2-1);
153699 sqlite3VdbeResolveLabel(v, pLevel->addrCont);
153701 if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
153704 sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
153705 for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
153706 sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
153707 if( pIn->eEndLoopOp!=OP_Noop ){
153708 if( pIn->nPrefix ){
153710 (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
153711 && (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0;
153712 if( pLevel->iLeftJoin ){
153713 /* For LEFT JOIN queries, cursor pIn->iCur may not have been
153718 ** return the null-row. So, if the cursor is not open yet,
153721 sqlite3VdbeAddOp2(v, OP_IfNotOpen, pIn->iCur,
153726 sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur,
153728 pIn->iBase, pIn->nPrefix);
153732 sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
153734 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev);
153735 VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next);
153737 sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
153740 sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
153741 if( pLevel->addrSkip ){
153742 sqlite3VdbeGoto(v, pLevel->addrSkip);
153743 VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
153744 sqlite3VdbeJumpHere(v, pLevel->addrSkip);
153745 sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
153748 if( pLevel->addrLikeRep ){
153749 sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1),
153750 pLevel->addrLikeRep);
153754 if( pLevel->iLeftJoin ){
153755 int ws = pLoop->wsFlags;
153756 addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
153759 assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
153760 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
153763 || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
153765 sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
153767 if( pLevel->op==OP_Return ){
153768 sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
153770 sqlite3VdbeGoto(v, pLevel->addrFirst);
153774 VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
153775 pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
153781 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
153783 assert( pWInfo->nLevel<=pTabList->nSrc );
153784 for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
153788 SrcItem *pTabItem = &pTabList->a[pLevel->iFrom];
153789 Table *pTab = pTabItem->pTab;
153791 pLoop = pLevel->pWLoop;
153793 /* For a co-routine, change all OP_Column references to the table of
153794 ** the co-routine into OP_Copy of result contained in a register.
153797 if( pTabItem->fg.viaCoroutine ){
153798 testcase( pParse->db->mallocFailed );
153799 translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur,
153800 pTabItem->regResult, 0);
153810 if( (pTab->tabFlags & TF_Ephemeral)==0
153811 && pTab->pSelect==0
153812 && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
153814 int ws = pLoop->wsFlags;
153815 if( pWInfo->eOnePass==ONEPASS_OFF && (ws & WHERE_IDX_ONLY)==0 ){
153816 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
153820 && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
153822 sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
153838 if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
153839 pIdx = pLoop->u.btree.pIndex;
153840 }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
153841 pIdx = pLevel->u.pCovidx;
153844 && !db->mallocFailed
153846 if( pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable) ){
153849 last = pWInfo->iEndWhere;
153851 k = pLevel->addrBody + 1;
153853 if( db->flags & SQLITE_VdbeAddopTrace ){
153854 printf("TRANSLATE opcodes in range %d..%d\n", k, last-1);
153857 pOp = sqlite3VdbeGetOp(v, k - 1);
153858 assert( pOp->opcode!=OP_Column || pOp->p1!=pLevel->iTabCur );
153859 assert( pOp->opcode!=OP_Rowid || pOp->p1!=pLevel->iTabCur );
153860 assert( pOp->opcode!=OP_IfNullRow || pOp->p1!=pLevel->iTabCur );
153863 pLastOp = pOp + (last - k);
153864 assert( pOp<pLastOp || (pParse->nErr>0 && pOp==pLastOp) );
153866 if( pOp->p1!=pLevel->iTabCur ){
153867 /* no-op */
153868 }else if( pOp->opcode==OP_Column
153870 || pOp->opcode==OP_Offset
153873 int x = pOp->p2;
153874 assert( pIdx->pTable==pTab );
153877 x = pPk->aiColumn[x];
153885 pOp->p2 = x;
153886 pOp->p1 = pLevel->iIdxCur;
153889 assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0
153890 || pWInfo->eOnePass );
153891 }else if( pOp->opcode==OP_Rowid ){
153892 pOp->p1 = pLevel->iIdxCur;
153893 pOp->opcode = OP_IdxRowid;
153895 }else if( pOp->opcode==OP_IfNullRow ){
153896 pOp->p1 = pLevel->iIdxCur;
153904 if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n");
153910 while( pWInfo->pExprMods ){
153911 WhereExprMod *p = pWInfo->pExprMods;
153912 pWInfo->pExprMods = p->pNext;
153913 memcpy(p->pExpr, &p->orig, sizeof(p->orig));
153919 pParse->nQueryLoop = pWInfo->savedNQueryLoop;
153969 ** the sub-query.
153974 ** select-list and ORDER BY expressions in the parent query are
153975 ** selected by the sub-query. For the purposes of the transformation,
154007 ** sub-query, which is always implemented as a co-routine. It then calls
154012 ** by the sub-query a sub-routine (OP_Gosub) coded by select.c is invoked.
154013 ** When the sub-routine is invoked:
154015 ** * The results of all window-functions for the row are stored
154027 ** BUILT-IN WINDOW FUNCTIONS
154029 ** This implementation features the following built-in window functions:
154043 ** These are the same built-in window functions supported by Postgres.
154046 ** be implemented using an API, built-in window functions are much more
154049 ** As such, some built-in window functions use the same API as aggregate
154060 ** As well as some of the built-in window functions, aggregate window
154067 ** Implementation of built-in window function row_number(). Assumes that the
154098 ** Implementation of built-in window function dense_rank(). Assumes that
154110 if( p ) p->nStep = 1;
154118 if( p->nStep ){
154119 p->nValue++;
154120 p->nStep = 0;
154122 sqlite3_result_int64(pCtx, p->nValue);
154127 ** Implementation of built-in window function nth_value(). This
154128 ** implementation is used in "slow mode" only - when the EXCLUDE clause
154159 p->nStep++;
154160 if( iVal==p->nStep ){
154161 p->pValue = sqlite3_value_dup(apArg[0]);
154162 if( !p->pValue ){
154173 pCtx, "second argument to nth_value must be a positive integer", -1
154179 if( p && p->pValue ){
154180 sqlite3_result_value(pCtx, p->pValue);
154181 sqlite3_value_free(p->pValue);
154182 p->pValue = 0;
154195 if( p && p->pValue==0 ){
154196 p->pValue = sqlite3_value_dup(apArg[0]);
154197 if( !p->pValue ){
154207 if( p && p->pValue ){
154208 sqlite3_result_value(pCtx, p->pValue);
154209 sqlite3_value_free(p->pValue);
154210 p->pValue = 0;
154217 ** Implementation of built-in window function rank(). Assumes that
154230 p->nStep++;
154231 if( p->nValue==0 ){
154232 p->nValue = p->nStep;
154242 sqlite3_result_int64(pCtx, p->nValue);
154243 p->nValue = 0;
154248 ** Implementation of built-in window function percent_rank(). Assumes that
154263 p->nTotal++;
154275 p->nStep++;
154281 p->nValue = p->nStep;
154282 if( p->nTotal>1 ){
154283 double r = (double)p->nValue / (double)(p->nTotal-1);
154293 ** Implementation of built-in window function cume_dist(). Assumes that
154308 p->nTotal++;
154320 p->nStep++;
154326 double r = (double)(p->nStep) / (double)(p->nTotal);
154356 if( p->nTotal==0 ){
154357 p->nParam = sqlite3_value_int64(apArg[0]);
154358 if( p->nParam<=0 ){
154360 pCtx, "argument of ntile must be a positive integer", -1
154364 p->nTotal++;
154376 p->iRow++;
154381 if( p && p->nParam>0 ){
154382 int nSize = (p->nTotal / p->nParam);
154384 sqlite3_result_int64(pCtx, p->iRow+1);
154386 i64 nLarge = p->nTotal - p->nParam*nSize;
154388 i64 iRow = p->iRow;
154390 assert( (nLarge*(nSize+1) + (p->nParam-nLarge)*nSize)==p->nTotal );
154395 sqlite3_result_int64(pCtx, 1 + nLarge + (iRow-iSmall)/nSize);
154422 sqlite3_value_free(p->pVal);
154423 p->pVal = sqlite3_value_dup(apArg[0]);
154424 if( p->pVal==0 ){
154427 p->nVal++;
154441 p->nVal--;
154442 if( p->nVal==0 ){
154443 sqlite3_value_free(p->pVal);
154444 p->pVal = 0;
154451 if( p && p->pVal ){
154452 sqlite3_result_value(pCtx, p->pVal);
154458 if( p && p->pVal ){
154459 sqlite3_result_value(pCtx, p->pVal);
154460 sqlite3_value_free(p->pVal);
154461 p->pVal = 0;
154466 ** Static names for the built-in window function names. These static
154471 ** if( pFuncDef->zName==row_valueName ){ ... }
154486 ** No-op implementations of xStep() and xFinalize(). Used as place-holders
154487 ** for built-in window functions that never call those interfaces.
154504 static void noopValueFunc(sqlite3_context *p){ UNUSED_PARAMETER(p); /*no-op*/ }
154515 ** no-op routines for their methods */
154533 ** Register those built-in window functions that are not also aggregates.
154558 for(p=pList; p; p=p->pNextWin){
154559 if( sqlite3StrICmp(p->zName, zName)==0 ) break;
154580 ** * If the function is a built-in window function that requires the
154581 ** window to be coerced (see "BUILT-IN WINDOW FUNCTIONS" at the top
154590 if( pWin->zName && pWin->eFrmType==0 ){
154591 Window *p = windowFind(pParse, pList, pWin->zName);
154593 pWin->pPartition = sqlite3ExprListDup(pParse->db, p->pPartition, 0);
154594 pWin->pOrderBy = sqlite3ExprListDup(pParse->db, p->pOrderBy, 0);
154595 pWin->pStart = sqlite3ExprDup(pParse->db, p->pStart, 0);
154596 pWin->pEnd = sqlite3ExprDup(pParse->db, p->pEnd, 0);
154597 pWin->eStart = p->eStart;
154598 pWin->eEnd = p->eEnd;
154599 pWin->eFrmType = p->eFrmType;
154600 pWin->eExclude = p->eExclude;
154604 if( (pWin->eFrmType==TK_RANGE)
154605 && (pWin->pStart || pWin->pEnd)
154606 && (pWin->pOrderBy==0 || pWin->pOrderBy->nExpr!=1)
154612 if( pFunc->funcFlags & SQLITE_FUNC_WINDOW ){
154613 sqlite3 *db = pParse->db;
154614 if( pWin->pFilter ){
154636 if( pFunc->zName==aUp[i].zFunc ){
154637 sqlite3ExprDelete(db, pWin->pStart);
154638 sqlite3ExprDelete(db, pWin->pEnd);
154639 pWin->pEnd = pWin->pStart = 0;
154640 pWin->eFrmType = aUp[i].eFrmType;
154641 pWin->eStart = aUp[i].eStart;
154642 pWin->eEnd = aUp[i].eEnd;
154643 pWin->eExclude = 0;
154644 if( pWin->eStart==TK_FOLLOWING ){
154645 pWin->pStart = sqlite3Expr(db, TK_INTEGER, "1");
154652 pWin->pFunc = pFunc;
154665 Select *pSubSelect; /* Current sub-select, if any */
154670 ** this function appends to the output expression-list and updates
154674 struct WindowRewrite *p = pWalker->u.pRewrite;
154675 Parse *pParse = pWalker->pParse;
154677 assert( p->pWin!=0 );
154679 /* If this function is being called from within a scalar sub-select
154683 ** to the scalar sub-select. */
154684 if( p->pSubSelect ){
154685 if( pExpr->op!=TK_COLUMN ){
154688 int nSrc = p->pSrc->nSrc;
154691 if( pExpr->iTable==p->pSrc->a[i].iCursor ) break;
154697 switch( pExpr->op ){
154704 for(pWin=p->pWin; pWin; pWin=pWin->pNextWin){
154705 if( pExpr->y.pWin==pWin ){
154706 assert( pWin->pOwner==pExpr );
154715 int iCol = -1;
154716 if( p->pSub ){
154718 for(i=0; i<p->pSub->nExpr; i++){
154719 if( 0==sqlite3ExprCompare(0, p->pSub->a[i].pExpr, pExpr, -1) ){
154726 Expr *pDup = sqlite3ExprDup(pParse->db, pExpr, 0);
154727 if( pDup && pDup->op==TK_AGG_FUNCTION ) pDup->op = TK_FUNCTION;
154728 p->pSub = sqlite3ExprListAppend(pParse, p->pSub, pDup);
154730 if( p->pSub ){
154731 int f = pExpr->flags & EP_Collate;
154734 sqlite3ExprDelete(pParse->db, pExpr);
154738 pExpr->op = TK_COLUMN;
154739 pExpr->iColumn = (iCol<0 ? p->pSub->nExpr-1: iCol);
154740 pExpr->iTable = p->pWin->iEphCsr;
154741 pExpr->y.pTab = p->pTab;
154742 pExpr->flags = f;
154744 if( pParse->db->mallocFailed ) return WRC_Abort;
154748 default: /* no-op */
154755 struct WindowRewrite *p = pWalker->u.pRewrite;
154756 Select *pSave = p->pSubSelect;
154760 p->pSubSelect = pSelect;
154762 p->pSubSelect = pSave;
154769 ** Iterate through each expression in expression-list pEList. For each:
154776 ** Append the node to output expression-list (*ppSub). And replace it
154777 ** with a TK_COLUMN that reads the (N-1)th element of table
154778 ** pWin->iEphCsr, where N is the number of elements in (*ppSub) after
154787 ExprList **ppSub /* IN/OUT: Sub-select expression-list */
154812 ** Append a copy of each expression in expression-list pAppend to
154823 int nInit = pList ? pList->nExpr : 0;
154824 for(i=0; i<pAppend->nExpr; i++){
154825 Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
154830 for(pSub=pDup; ExprHasProperty(pSub, EP_Skip); pSub=pSub->pLeft){
154834 pSub->op = TK_NULL;
154835 pSub->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
154836 pSub->u.zToken = 0;
154840 if( pList ) pList->a[nInit+i].sortFlags = pAppend->a[i].sortFlags;
154849 ** then we have to increase the Expr->op2 values of those nodes
154855 if( pExpr->op==TK_AGG_FUNCTION
154856 && pExpr->op2>=pWalker->walkerDepth
154858 pExpr->op2++;
154865 ** any SQL window functions, this function is a no-op. Otherwise, it
154872 if( p->pWin && p->pPrior==0 && (p->selFlags & SF_WinRewrite)==0 ){
154874 sqlite3 *db = pParse->db;
154876 SrcList *pSrc = p->pSrc;
154877 Expr *pWhere = p->pWhere;
154878 ExprList *pGroupBy = p->pGroupBy;
154879 Expr *pHaving = p->pHaving;
154882 ExprList *pSublist = 0; /* Expression list for sub-query */
154883 Window *pMWin = p->pWin; /* Main window object */
154888 u32 selFlags = p->selFlags;
154897 p->pSrc = 0;
154898 p->pWhere = 0;
154899 p->pGroupBy = 0;
154900 p->pHaving = 0;
154901 p->selFlags &= ~SF_Aggregate;
154902 p->selFlags |= SF_WinRewrite;
154904 /* Create the ORDER BY clause for the sub-select. This is the concatenation
154907 pSort = exprListAppendList(pParse, 0, pMWin->pPartition, 1);
154908 pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
154909 if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
154910 int nSave = pSort->nExpr;
154911 pSort->nExpr = p->pOrderBy->nExpr;
154912 if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
154913 sqlite3ExprListDelete(db, p->pOrderBy);
154914 p->pOrderBy = 0;
154916 pSort->nExpr = nSave;
154922 pMWin->iEphCsr = pParse->nTab++;
154923 pParse->nTab += 3;
154925 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, pTab, &pSublist);
154926 selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, pTab, &pSublist);
154927 pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
154930 ** sub-select expression list. They are required to figure out where
154932 pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
154933 pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
154936 ** sub-select expression list. Also allocate two registers for each
154937 ** window function - one for the accumulator, another for interim
154939 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
154940 ExprList *pArgs = pWin->pOwner->x.pList;
154941 if( pWin->pFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
154943 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
154944 pWin->bExprArgs = 1;
154946 pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
154949 if( pWin->pFilter ){
154950 Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
154953 pWin->regAccum = ++pParse->nMem;
154954 pWin->regResult = ++pParse->nMem;
154955 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
154974 ("New window-function subquery in FROM clause of (%u/%p)\n",
154975 p->selId, p));
154976 p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
154977 if( p->pSrc ){
154979 p->pSrc->a[0].pSelect = pSub;
154980 sqlite3SrcListAssignCursors(pParse, p->pSrc);
154981 pSub->selFlags |= SF_Expanded;
154983 pSub->selFlags |= (selFlags & SF_Aggregate);
154986 ** pParse->nErr will be set, so if SQLITE_NOMEM is set, we will get
154987 ** the correct error message regardless. */
154991 pTab->tabFlags |= TF_Ephemeral;
154992 p->pSrc->a[0].pTab = pTab;
155003 if( db->mallocFailed ) rc = SQLITE_NOMEM;
155008 if( pParse->nErr==0 ){
155009 assert( pParse->db->mallocFailed );
155010 sqlite3ErrorToParser(pParse->db, SQLITE_NOMEM);
155021 if( p->ppThis ){
155022 *p->ppThis = p->pNextWin;
155023 if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
155024 p->ppThis = 0;
155034 sqlite3ExprDelete(db, p->pFilter);
155035 sqlite3ExprListDelete(db, p->pPartition);
155036 sqlite3ExprListDelete(db, p->pOrderBy);
155037 sqlite3ExprDelete(db, p->pEnd);
155038 sqlite3ExprDelete(db, p->pStart);
155039 sqlite3DbFree(db, p->zName);
155040 sqlite3DbFree(db, p->zBase);
155050 Window *pNext = p->pNextWin;
155058 ** value should be a non-negative integer. If the value is not a
155059 ** constant, change it to NULL. The fact that it is then a non-negative
155066 sqlite3ExprDelete(pParse->db, pExpr);
155067 pExpr = sqlite3ExprAlloc(pParse->db, TK_NULL, 0, 0);
155122 pWin = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
155124 pWin->eFrmType = eType;
155125 pWin->eStart = eStart;
155126 pWin->eEnd = eEnd;
155127 if( eExclude==0 && OptimizationDisabled(pParse->db, SQLITE_WindowFunc) ){
155130 pWin->eExclude = eExclude;
155131 pWin->bImplicitFrame = bImplicitFrame;
155132 pWin->pEnd = sqlite3WindowOffsetExpr(pParse, pEnd);
155133 pWin->pStart = sqlite3WindowOffsetExpr(pParse, pStart);
155137 sqlite3ExprDelete(pParse->db, pEnd);
155138 sqlite3ExprDelete(pParse->db, pStart);
155144 ** pWin. Also, if parameter pBase is not NULL, set pWin->zBase to the
155145 ** equivalent nul-terminated string.
155155 pWin->pPartition = pPartition;
155156 pWin->pOrderBy = pOrderBy;
155158 pWin->zBase = sqlite3DbStrNDup(pParse->db, pBase->z, pBase->n);
155161 sqlite3ExprListDelete(pParse->db, pPartition);
155162 sqlite3ExprListDelete(pParse->db, pOrderBy);
155170 ** stored in the linked list starting at pWin->pNextWin. This function
155175 if( pWin->zBase ){
155176 sqlite3 *db = pParse->db;
155177 Window *pExist = windowFind(pParse, pList, pWin->zBase);
155181 if( pWin->pPartition ){
155183 }else if( pExist->pOrderBy && pWin->pOrderBy ){
155185 }else if( pExist->bImplicitFrame==0 ){
155190 "cannot override %s of window: %s", zErr, pWin->zBase
155193 pWin->pPartition = sqlite3ExprListDup(db, pExist->pPartition, 0);
155194 if( pExist->pOrderBy ){
155195 assert( pWin->pOrderBy==0 );
155196 pWin->pOrderBy = sqlite3ExprListDup(db, pExist->pOrderBy, 0);
155198 sqlite3DbFree(db, pWin->zBase);
155199 pWin->zBase = 0;
155210 assert( p->op==TK_FUNCTION );
155212 p->y.pWin = pWin;
155214 pWin->pOwner = p;
155215 if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
155221 sqlite3WindowDelete(pParse->db, pWin);
155226 ** Possibly link window pWin into the list at pSel->pWin (window functions
155233 if( 0==pSel->pWin || 0==sqlite3WindowCompare(0, pSel->pWin, pWin, 0) ){
155234 pWin->pNextWin = pSel->pWin;
155235 if( pSel->pWin ){
155236 pSel->pWin->ppThis = &pWin->pNextWin;
155238 pSel->pWin = pWin;
155239 pWin->ppThis = &pSel->pWin;
155241 if( sqlite3ExprListCompare(pWin->pPartition, pSel->pWin->pPartition,-1) ){
155242 pSel->selFlags |= SF_MultiPart;
155256 if( p1->eFrmType!=p2->eFrmType ) return 1;
155257 if( p1->eStart!=p2->eStart ) return 1;
155258 if( p1->eEnd!=p2->eEnd ) return 1;
155259 if( p1->eExclude!=p2->eExclude ) return 1;
155260 if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
155261 if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
155262 if( (res = sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1)) ){
155265 if( (res = sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1)) ){
155269 if( (res = sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1)) ){
155279 ** to begin iterating through the sub-query results. It is used to allocate
155283 int nEphExpr = pSelect->pSrc->a[0].pSelect->pEList->nExpr;
155284 Window *pMWin = pSelect->pWin;
155288 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pMWin->iEphCsr, nEphExpr);
155289 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+1, pMWin->iEphCsr);
155290 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+2, pMWin->iEphCsr);
155291 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->iEphCsr+3, pMWin->iEphCsr);
155295 if( pMWin->pPartition ){
155296 int nExpr = pMWin->pPartition->nExpr;
155297 pMWin->regPart = pParse->nMem+1;
155298 pParse->nMem += nExpr;
155299 sqlite3VdbeAddOp3(v, OP_Null, 0, pMWin->regPart, pMWin->regPart+nExpr-1);
155302 pMWin->regOne = ++pParse->nMem;
155303 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regOne);
155305 if( pMWin->eExclude ){
155306 pMWin->regStartRowid = ++pParse->nMem;
155307 pMWin->regEndRowid = ++pParse->nMem;
155308 pMWin->csrApp = pParse->nTab++;
155309 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
155310 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
155311 sqlite3VdbeAddOp2(v, OP_OpenDup, pMWin->csrApp, pMWin->iEphCsr);
155315 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155316 FuncDef *p = pWin->pFunc;
155317 if( (p->funcFlags & SQLITE_FUNC_MINMAX) && pWin->eStart!=TK_UNBOUNDED ){
155325 ExprList *pList = pWin->pOwner->x.pList;
155327 pWin->csrApp = pParse->nTab++;
155328 pWin->regApp = pParse->nMem+1;
155329 pParse->nMem += 3;
155330 if( pKeyInfo && pWin->pFunc->zName[1]=='i' ){
155331 assert( pKeyInfo->aSortFlags[0]==0 );
155332 pKeyInfo->aSortFlags[0] = KEYINFO_ORDER_DESC;
155334 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pWin->csrApp, 2);
155336 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
155338 else if( p->zName==nth_valueName || p->zName==first_valueName ){
155339 /* Allocate two registers at pWin->regApp. These will be used to
155341 pWin->regApp = pParse->nMem+1;
155342 pWin->csrApp = pParse->nTab++;
155343 pParse->nMem += 2;
155344 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
155346 else if( p->zName==leadName || p->zName==lagName ){
155347 pWin->csrApp = pParse->nTab++;
155348 sqlite3VdbeAddOp2(v, OP_OpenDup, pWin->csrApp, pMWin->iEphCsr);
155363 ** code to check that the value is a non-negative integer and throws an
155368 "frame starting offset must be a non-negative integer",
155369 "frame ending offset must be a non-negative integer",
155371 "frame starting offset must be a non-negative number",
155372 "frame ending offset must be a non-negative number",
155410 ** Return the number of arguments passed to the window-function associated
155414 ExprList *pList = pWin->pOwner->x.pList;
155415 return (pList ? pList->nExpr : 0);
155438 ** in an array of accumulator registers - one for each window function
155445 ** order to reduce memory requirements - it would always be safe just
155460 ** Consider a window-frame similar to the following:
155480 ** Depending on the window-frame in question, all three cursors may not
155507 Window *pMWin = p->pMWin;
155508 ExprList *pOrderBy = pMWin->pOrderBy;
155510 Vdbe *v = sqlite3GetVdbe(p->pParse);
155511 ExprList *pPart = pMWin->pPartition;
155512 int iColOff = pMWin->nBufferCol + (pPart ? pPart->nExpr : 0);
155514 for(i=0; i<pOrderBy->nExpr; i++){
155522 ** xInverse (if bInverse is non-zero) for each window function in the
155523 ** linked list starting at pMWin. Or, for built-in window functions
155534 ** already populated with all columns from the current row of the sub-query.
155536 ** If argument regPartSize is non-zero, then it is a register containing the
155546 Parse *pParse = p->pParse;
155549 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155550 FuncDef *pFunc = pWin->pFunc;
155552 int nArg = pWin->bExprArgs ? 0 : windowArgCount(pWin);
155555 assert( bInverse==0 || pWin->eStart!=TK_UNBOUNDED );
155562 if( i!=1 || pFunc->zName!=nth_valueName ){
155563 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+i, reg+i);
155565 sqlite3VdbeAddOp3(v, OP_Column, pMWin->iEphCsr, pWin->iArgCol+i, reg+i);
155570 if( pMWin->regStartRowid==0
155571 && (pFunc->funcFlags & SQLITE_FUNC_MINMAX)
155572 && (pWin->eStart!=TK_UNBOUNDED)
155577 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1, 1);
155578 sqlite3VdbeAddOp2(v, OP_SCopy, regArg, pWin->regApp);
155579 sqlite3VdbeAddOp3(v, OP_MakeRecord, pWin->regApp, 2, pWin->regApp+2);
155580 sqlite3VdbeAddOp2(v, OP_IdxInsert, pWin->csrApp, pWin->regApp+2);
155582 sqlite3VdbeAddOp4Int(v, OP_SeekGE, pWin->csrApp, 0, regArg, 1);
155584 sqlite3VdbeAddOp1(v, OP_Delete, pWin->csrApp);
155585 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
155588 }else if( pWin->regApp ){
155589 assert( pFunc->zName==nth_valueName
155590 || pFunc->zName==first_valueName
155593 sqlite3VdbeAddOp2(v, OP_AddImm, pWin->regApp+1-bInverse, 1);
155594 }else if( pFunc->xSFunc!=noopStepFunc ){
155596 if( pWin->pFilter ){
155598 assert( pWin->bExprArgs || !nArg ||nArg==pWin->pOwner->x.pList->nExpr );
155599 assert( pWin->bExprArgs || nArg ||pWin->pOwner->x.pList==0 );
155601 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol+nArg,regTmp);
155607 if( pWin->bExprArgs ){
155611 nArg = pWin->pOwner->x.pList->nExpr;
155613 sqlite3ExprCodeExprList(pParse, pWin->pOwner->x.pList, regArg, 0, 0);
155615 pEnd = sqlite3VdbeGetOp(v, -1);
155617 if( pOp->opcode==OP_Column && pOp->p1==pWin->iEphCsr ){
155618 pOp->p1 = csr;
155622 if( pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
155625 pColl = sqlite3ExprNNCollSeq(pParse, pWin->pOwner->x.pList->a[0].pExpr);
155629 bInverse, regArg, pWin->regAccum);
155632 if( pWin->bExprArgs ){
155650 ** pMWin. Or, for built-in window-functions that do not use the standard
155654 Parse *pParse = p->pParse;
155655 Window *pMWin = p->pMWin;
155659 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155660 if( pMWin->regStartRowid==0
155661 && (pWin->pFunc->funcFlags & SQLITE_FUNC_MINMAX)
155662 && (pWin->eStart!=TK_UNBOUNDED)
155664 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
155665 sqlite3VdbeAddOp1(v, OP_Last, pWin->csrApp);
155667 sqlite3VdbeAddOp3(v, OP_Column, pWin->csrApp, 0, pWin->regResult);
155668 sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
155669 }else if( pWin->regApp ){
155670 assert( pMWin->regStartRowid==0 );
155674 sqlite3VdbeAddOp2(v, OP_AggFinal, pWin->regAccum, nArg);
155675 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
155676 sqlite3VdbeAddOp2(v, OP_Copy, pWin->regAccum, pWin->regResult);
155677 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
155679 sqlite3VdbeAddOp3(v, OP_AggValue,pWin->regAccum,nArg,pWin->regResult);
155680 sqlite3VdbeAppendP4(v, pWin->pFunc, P4_FUNCDEF);
155688 ** p->pMWin list by doing a full scan of the current window frame. Store the
155694 Parse *pParse = p->pParse;
155695 Window *pMWin = p->pMWin;
155696 Vdbe *v = p->pVdbe;
155712 csr = pMWin->csrApp;
155713 nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
155725 sqlite3VdbeAddOp2(v, OP_Rowid, pMWin->iEphCsr, regCRowid);
155726 windowReadPeerValues(p, pMWin->iEphCsr, regCPeer);
155728 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155729 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
155732 sqlite3VdbeAddOp3(v, OP_SeekGE, csr, lblBrk, pMWin->regStartRowid);
155736 sqlite3VdbeAddOp3(v, OP_Gt, pMWin->regEndRowid, lblBrk, regRowid);
155739 if( pMWin->eExclude==TK_CURRENT ){
155742 }else if( pMWin->eExclude!=TK_NO ){
155747 if( pMWin->pOrderBy ){
155748 pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pMWin->pOrderBy, 0, 0);
155750 if( pMWin->eExclude==TK_TIES ){
155767 windowAggStep(p, pMWin, csr, 0, p->regArg);
155772 sqlite3VdbeJumpHere(v, addrNext-1);
155786 ** Invoke the sub-routine at regGosub (generated by code in select.c) to
155790 ** for per-row processing is only generated for the following built-in window
155799 Window *pMWin = p->pMWin;
155800 Vdbe *v = p->pVdbe;
155802 if( pMWin->regStartRowid ){
155805 Parse *pParse = p->pParse;
155808 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155809 FuncDef *pFunc = pWin->pFunc;
155810 if( pFunc->zName==nth_valueName
155811 || pFunc->zName==first_valueName
155813 int csr = pWin->csrApp;
155816 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
155818 if( pFunc->zName==nth_valueName ){
155819 sqlite3VdbeAddOp3(v, OP_Column,pMWin->iEphCsr,pWin->iArgCol+1,tmpReg);
155824 sqlite3VdbeAddOp3(v, OP_Add, tmpReg, pWin->regApp, tmpReg);
155825 sqlite3VdbeAddOp3(v, OP_Gt, pWin->regApp+1, lbl, tmpReg);
155829 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
155833 else if( pFunc->zName==leadName || pFunc->zName==lagName ){
155834 int nArg = pWin->pOwner->x.pList->nExpr;
155835 int csr = pWin->csrApp;
155838 int iEph = pMWin->iEphCsr;
155841 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regResult);
155843 sqlite3VdbeAddOp3(v, OP_Column, iEph,pWin->iArgCol+2,pWin->regResult);
155847 int val = (pFunc->zName==leadName ? 1 : -1);
155850 int op = (pFunc->zName==leadName ? OP_Add : OP_Subtract);
155852 sqlite3VdbeAddOp3(v, OP_Column, iEph, pWin->iArgCol+1, tmpReg2);
155859 sqlite3VdbeAddOp3(v, OP_Column, csr, pWin->iArgCol, pWin->regResult);
155865 sqlite3VdbeAddOp2(v, OP_Gosub, p->regGosub, p->addrGosub);
155871 ** any equivalent initialization required by any built-in window functions
155879 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155880 FuncDef *pFunc = pWin->pFunc;
155881 assert( pWin->regAccum );
155882 sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);
155884 if( pMWin->regStartRowid==0 ){
155885 if( pFunc->zName==nth_valueName || pFunc->zName==first_valueName ){
155886 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp);
155887 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
155890 if( (pFunc->funcFlags & SQLITE_FUNC_MINMAX) && pWin->csrApp ){
155891 assert( pWin->eStart!=TK_UNBOUNDED );
155892 sqlite3VdbeAddOp1(v, OP_ResetSorter, pWin->csrApp);
155893 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWin->regApp+1);
155897 regArg = pParse->nMem+1;
155898 pParse->nMem += nArg;
155908 if( pMWin->regStartRowid ) return 1;
155909 for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
155910 FuncDef *pFunc = pWin->pFunc;
155911 if( (pFunc->zName==nth_valueName)
155912 || (pFunc->zName==first_valueName)
155913 || (pFunc->zName==leadName)
155914 || (pFunc->zName==lagName)
155924 ** pOrderBy->nExpr. This function generates code to compare the two
155941 int nVal = pOrderBy->nExpr;
155949 sqlite3VdbeAddOp3(v, OP_Copy, regNew, regOld, nVal-1);
155964 ** operator in the above pseudo-code is replaced with ">" or "<=", respectively.
155966 ** If the sort-order for the ORDER BY term in the window is DESC, then the
155968 ** subtracted. And the comparison operator is inverted to - ">=" becomes "<=",
155972 ** if( csr1.peerVal - regVal <= csr2.peerVal ) goto lbl;
155982 int regVal, /* Register containing non-negative number */
155986 Parse *pParse = p->pParse;
155988 ExprList *pOrderBy = p->pMWin->pOrderBy; /* ORDER BY clause for window */
155991 int regString = ++pParse->nMem; /* Reg. for constant value '' */
155997 assert( pOrderBy && pOrderBy->nExpr==1 );
155998 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_DESC ){
156007 /* Read the peer-value from each cursor into a register */
156012 reg1, (arith==OP_Add ? "+" : "-"), regVal,
156016 /* Register reg1 currently contains csr1.peerVal (the peer-value from csr1).
156019 ** then leave reg1 as it is. In pseudo-code, this is implemented as:
156022 ** reg1 = reg1 +/- regVal
156025 ** Since all strings and blobs are greater-than-or-equal-to an empty string,
156054 if( pOrderBy->a[0].sortFlags & KEYINFO_ORDER_BIGNULL ){
156069 default: assert( op==OP_Lt ); /* no-op */ break;
156077 sqlite3VdbeChangeP2(v, -1, sqlite3VdbeCurrentAddr(v)+1);
156085 pColl = sqlite3ExprNNCollSeq(pParse, pOrderBy->a[0].pExpr);
156113 Parse *pParse = p->pParse;
156114 Window *pMWin = p->pMWin;
156116 Vdbe *v = p->pVdbe;
156118 int bPeer = (pMWin->eFrmType!=TK_ROWS);
156123 /* Special case - WINDOW_AGGINVERSE is always a no-op if the frame
156125 if( op==WINDOW_AGGINVERSE && pMWin->eStart==TK_UNBOUNDED ){
156131 if( pMWin->eFrmType==TK_RANGE ){
156135 if( pMWin->eStart==TK_FOLLOWING ){
156137 p, OP_Le, p->current.csr, regCountdown, p->start.csr, lblDone
156141 p, OP_Ge, p->start.csr, regCountdown, p->current.csr, lblDone
156146 p, OP_Gt, p->end.csr, regCountdown, p->current.csr, lblDone
156155 if( op==WINDOW_RETURN_ROW && pMWin->regStartRowid==0 ){
156164 if( pMWin->eStart==pMWin->eEnd && regCountdown
156165 && pMWin->eFrmType==TK_RANGE && op==WINDOW_AGGINVERSE
156169 sqlite3VdbeAddOp2(v, OP_Rowid, p->start.csr, regRowid1);
156170 sqlite3VdbeAddOp2(v, OP_Rowid, p->end.csr, regRowid2);
156175 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING );
156180 csr = p->current.csr;
156181 reg = p->current.reg;
156186 csr = p->start.csr;
156187 reg = p->start.reg;
156188 if( pMWin->regStartRowid ){
156189 assert( pMWin->regEndRowid );
156190 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regStartRowid, 1);
156192 windowAggStep(p, pMWin, csr, 1, p->regArg);
156198 csr = p->end.csr;
156199 reg = p->end.reg;
156200 if( pMWin->regStartRowid ){
156201 assert( pMWin->regEndRowid );
156202 sqlite3VdbeAddOp2(v, OP_AddImm, pMWin->regEndRowid, 1);
156204 windowAggStep(p, pMWin, csr, 0, p->regArg);
156209 if( op==p->eDelete ){
156227 int nReg = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
156230 windowIfNewPeer(pParse, pMWin->pOrderBy, regTmp, reg, addrContinue);
156252 pNew->zName = sqlite3DbStrDup(db, p->zName);
156253 pNew->zBase = sqlite3DbStrDup(db, p->zBase);
156254 pNew->pFilter = sqlite3ExprDup(db, p->pFilter, 0);
156255 pNew->pFunc = p->pFunc;
156256 pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
156257 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
156258 pNew->eFrmType = p->eFrmType;
156259 pNew->eEnd = p->eEnd;
156260 pNew->eStart = p->eStart;
156261 pNew->eExclude = p->eExclude;
156262 pNew->regResult = p->regResult;
156263 pNew->regAccum = p->regAccum;
156264 pNew->iArgCol = p->iArgCol;
156265 pNew->iEphCsr = p->iEphCsr;
156266 pNew->bExprArgs = p->bExprArgs;
156267 pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
156268 pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
156269 pNew->pOwner = pOwner;
156270 pNew->bImplicitFrame = p->bImplicitFrame;
156285 for(pWin=p; pWin; pWin=pWin->pNextWin){
156288 pp = &((*pp)->pNextWin);
156304 sqlite3 *db = pParse->db;
156306 sqlite3ValueFromExpr(db, pExpr, db->enc, SQLITE_AFF_NUMERIC, &pVal);
156318 ** and invoke the sub-routine at instruction addrGosub once for each row.
156348 ** if( (regEnd--)<=0 ){
156350 ** if( (regStart--)<=0 ){
156361 ** if( (regStart--)<=0 ){
156367 ** The pseudo-code above uses the following shorthand:
156382 ** differently from the above - "BETWEEN <expr> PRECEDING AND <expr> PRECEDING"
156401 ** if( (regEnd--)<=0 ){
156405 ** if( (regStart--)<=0 ){
156411 ** if( (regEnd--)<=0 ){
156427 ** regStart = regEnd - <expr1>
156430 ** if( (regEnd--)<=0 ){
156433 ** if( (regStart--)<=0 ){
156441 ** if( (regEnd--)<=0 ){
156445 ** if( (regStart--)<=0 ){
156457 ** This is optimized of course - branches that will never be taken and
156478 ** if( (regStart--)<=0 ){
156494 ** To handle this case, the pseudo-code programs depicted above are modified
156514 ** of the outer loop - the one started by sqlite3WhereBegin().
156656 Window *pMWin = p->pWin;
156657 ExprList *pOrderBy = pMWin->pOrderBy;
156660 int csrInput = p->pSrc->a[0].iCursor; /* Cursor of sub-select */
156661 int nInput = p->pSrc->a[0].pTab->nCol; /* Number of cols returned by sub */
156673 WindowCodeArg s; /* Context object for sub-routines */
156678 assert( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_CURRENT
156679 || pMWin->eStart==TK_FOLLOWING || pMWin->eStart==TK_UNBOUNDED
156681 assert( pMWin->eEnd==TK_FOLLOWING || pMWin->eEnd==TK_CURRENT
156682 || pMWin->eEnd==TK_UNBOUNDED || pMWin->eEnd==TK_PRECEDING
156684 assert( pMWin->eExclude==0 || pMWin->eExclude==TK_CURRENT
156685 || pMWin->eExclude==TK_GROUP || pMWin->eExclude==TK_TIES
156686 || pMWin->eExclude==TK_NO
156698 s.current.csr = pMWin->iEphCsr;
156704 ** are four options - they may never be deleted (eDelete==0), they may
156709 switch( pMWin->eStart ){
156711 if( pMWin->eFrmType!=TK_RANGE
156712 && windowExprGtZero(pParse, pMWin->pStart)
156719 if( pMWin->eEnd==TK_PRECEDING ){
156720 if( pMWin->eFrmType!=TK_RANGE
156721 && windowExprGtZero(pParse, pMWin->pEnd)
156735 /* Allocate registers for the array of values from the sub-query, the
156738 regNew = pParse->nMem+1;
156739 pParse->nMem += nInput;
156740 regRecord = ++pParse->nMem;
156741 regRowid = ++pParse->nMem;
156746 if( pMWin->eStart==TK_PRECEDING || pMWin->eStart==TK_FOLLOWING ){
156747 regStart = ++pParse->nMem;
156749 if( pMWin->eEnd==TK_PRECEDING || pMWin->eEnd==TK_FOLLOWING ){
156750 regEnd = ++pParse->nMem;
156756 if( pMWin->eFrmType!=TK_ROWS ){
156757 int nPeer = (pOrderBy ? pOrderBy->nExpr : 0);
156758 regNewPeer = regNew + pMWin->nBufferCol;
156759 if( pMWin->pPartition ) regNewPeer += pMWin->pPartition->nExpr;
156760 regPeer = pParse->nMem+1; pParse->nMem += nPeer;
156761 s.start.reg = pParse->nMem+1; pParse->nMem += nPeer;
156762 s.current.reg = pParse->nMem+1; pParse->nMem += nPeer;
156763 s.end.reg = pParse->nMem+1; pParse->nMem += nPeer;
156766 /* Load the column values for the row returned by the sub-select
156779 if( pMWin->pPartition ){
156781 ExprList *pPart = pMWin->pPartition;
156782 int nPart = pPart->nExpr;
156783 int regNewPart = regNew + pMWin->nBufferCol;
156786 regFlushPart = ++pParse->nMem;
156787 addr = sqlite3VdbeAddOp3(v, OP_Compare, regNewPart, pMWin->regPart, nPart);
156793 sqlite3VdbeAddOp3(v, OP_Copy, regNewPart, pMWin->regPart, nPart-1);
156799 addrNe = sqlite3VdbeAddOp3(v, OP_Ne, pMWin->regOne, 0, regRowid);
156806 sqlite3ExprCode(pParse, pMWin->pStart, regStart);
156807 windowCheckValue(pParse, regStart, 0 + (pMWin->eFrmType==TK_RANGE?3:0));
156810 sqlite3ExprCode(pParse, pMWin->pEnd, regEnd);
156811 windowCheckValue(pParse, regEnd, 1 + (pMWin->eFrmType==TK_RANGE?3:0));
156814 if( pMWin->eFrmType!=TK_RANGE && pMWin->eStart==pMWin->eEnd && regStart ){
156815 int op = ((pMWin->eStart==TK_FOLLOWING) ? OP_Ge : OP_Le);
156827 if( pMWin->eStart==TK_FOLLOWING && pMWin->eFrmType!=TK_RANGE && regEnd ){
156828 assert( pMWin->eEnd==TK_FOLLOWING );
156832 if( pMWin->eStart!=TK_UNBOUNDED ){
156841 sqlite3VdbeAddOp3(v, OP_Copy, regNewPeer, regPeer, pOrderBy->nExpr-1);
156842 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.start.reg, pOrderBy->nExpr-1);
156843 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.current.reg, pOrderBy->nExpr-1);
156844 sqlite3VdbeAddOp3(v, OP_Copy, regPeer, s.end.reg, pOrderBy->nExpr-1);
156855 if( pMWin->eStart==TK_FOLLOWING ){
156857 if( pMWin->eEnd!=TK_UNBOUNDED ){
156858 if( pMWin->eFrmType==TK_RANGE ){
156872 if( pMWin->eEnd==TK_PRECEDING ){
156873 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
156881 if( pMWin->eEnd!=TK_UNBOUNDED ){
156882 if( pMWin->eFrmType==TK_RANGE ){
156912 if( pMWin->pPartition ){
156919 if( pMWin->eEnd==TK_PRECEDING ){
156920 int bRPS = (pMWin->eStart==TK_PRECEDING && pMWin->eFrmType==TK_RANGE);
156924 }else if( pMWin->eStart==TK_FOLLOWING ){
156930 if( pMWin->eFrmType==TK_RANGE ){
156935 if( pMWin->eEnd==TK_UNBOUNDED ){
156940 assert( pMWin->eEnd==TK_FOLLOWING );
156965 if( pMWin->pPartition ){
156966 if( pMWin->regStartRowid ){
156967 sqlite3VdbeAddOp2(v, OP_Integer, 1, pMWin->regStartRowid);
156968 sqlite3VdbeAddOp2(v, OP_Integer, 0, pMWin->regEndRowid);
156982 ** 2001-09-15
156996 ** That input file is processed by Lemon to generate a C-language
156998 ** this comment as part of the translated C-code. Edits should be made
157005 ** Disable all error recovery processing in the parser push-down
157058 sqlite3 *db = pParse->db;
157059 pParse->disableLookaside++;
157066 ** Issue an error message if an ORDER BY or LIMIT clause occurs on an
157079 sqlite3ExprListDelete(pParse->db, pOrderBy);
157080 sqlite3ExprDelete(pParse->db, pLimit);
157086 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
157092 if( p->pPrior ){
157096 pLoop->pNext = pNext;
157097 pLoop->selFlags |= SF_Compound;
157099 pLoop = pLoop->pPrior;
157102 if( pLoop->pOrderBy || pLoop->pLimit ){
157104 pLoop->pOrderBy!=0 ? "ORDER BY" : "LIMIT",
157105 sqlite3SelectOpName(pNext->op));
157109 if( (p->selFlags & SF_MultiValue)==0 &&
157110 (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 &&
157123 pSelect->pWith = pWith;
157126 sqlite3WithDelete(pParse->db, pWith);
157137 Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
157140 p->op = (u8)op;
157141 p->affExpr = 0;
157142 p->flags = EP_Leaf;
157144 p->iAgg = -1;
157145 p->pLeft = p->pRight = 0;
157146 p->x.pList = 0;
157147 p->pAggInfo = 0;
157148 p->y.pTab = 0;
157149 p->op2 = 0;
157150 p->iTable = 0;
157151 p->iColumn = 0;
157152 p->u.zToken = (char*)&p[1];
157153 memcpy(p->u.zToken, t.z, t.n);
157154 p->u.zToken[t.n] = 0;
157155 if( sqlite3Isquote(p->u.zToken[0]) ){
157159 p->nHeight = 1;
157172 sqlite3 *db = pParse->db;
157173 if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
157174 pA->op = (u8)op;
157175 sqlite3ExprDelete(db, pA->pRight);
157176 pA->pRight = 0;
157194 && pParse->db->init.busy==0
157197 pIdToken->n, pIdToken->z);
157398 ** that represent terminal and non-terminal symbols.
157404 ** (also known as: "terminal symbols") have fall-back
157408 ** YYACTIONTYPE is the data type used for "action codes" - numbers
157413 ** value associated with a terminal or non-terminal
157416 ** Each non-terminal can have a different minor type.
157438 ** YY_MIN_SHIFTREDUCE Minimum value for shift-reduce actions
157439 ** YY_MAX_SHIFTREDUCE Maximum value for shift-reduce actions
157442 ** YY_NO_ACTION The yy_action[] code for no-op
157486 #define sqlite3ParserCTX_FETCH Parse *pParse=yypParser->pParse;
157487 #define sqlite3ParserCTX_STORE yypParser->pParse=pParse;
157504 /* Define the yytestcase() macro to be a no-op if is not already defined
157529 ** and YY_MAX_SHIFTREDUCE reduce by rule N-YY_MIN_SHIFTREDUCE.
157538 ** N between YY_MIN_REDUCE Reduce by rule N-YY_MIN_REDUCE
157551 ** a terminal symbol. If the lookahead is a non-terminal (as occurs after
157563 ** shifting non-terminals after a reduce.
158058 #define YY_REDUCE_MIN (-265)
158061 /* 0 */ 111, 168, 386, 761, -176, -174, -191, -189, -181, -178,
158062 /* 10 */ 176, 263, 44, -207, -204, -265, -139, -114, 158, 504,
158064 /* 30 */ 70, 714, -187, 127, 774, 713, 767, 769, 970, 1019,
158065 /* 40 */ 1021, -255, -255, -255, -255, -255, -255, -255, -255, -255,
158066 /* 50 */ -255, -255, -255, -255, -255, -255, -255, -255, -255, -255,
158067 /* 60 */ -255, -255, -255, -255, -255, -255, -255, -255, -255, -255,
158068 /* 70 */ -255, -255, -255, -255, -255, -255, -255, -255, 394, 542,
158073 /* 120 */ 1298, 1300, 1329, 1331, 1343, 1358, 1360, -255, -255, -255,
158074 /* 130 */ -255, -255, -255, -255, -255, 196, -255, 387, -177, 507,
158075 /* 140 */ 1002, -219, 557, -93, -167, 638, -121, 284, 500, 284,
158076 /* 150 */ 500, 247, 651, 865, -255, -255, -255, -255, -85, -85,
158077 /* 160 */ -85, 237, 171, 602, 846, 885, -212, -203, 217, 380,
158078 /* 170 */ 380, -23, 161, 653, 712, 773, 943, 990, 1040, 563,
158079 /* 180 */ 833, 971, 1005, 1042, 1092, 1078, 1043, 1144, 1184, -186,
158081 /* 200 */ 1087, 1094, 336, 545, 772, 1201, 1117, 1207, -179, -137,
158082 /* 210 */ -112, -13, 18, 112, 197, 418, 495, 508, 777, 809,
158162 /********** End of lemon-generated parsing tables *****************************/
158383 YYACTIONTYPE stateno; /* The state-number, or reduce action in SHIFTREDUCE */
158386 YYMINORTYPE minor; /* The user-supplied minor token value. This
158396 int yyhwm; /* High-water mark of the stack */
158424 ** and a prompt to preface each trace message. Tracing is turned off
159186 newSize = p->yystksz*2 + 100;
159187 idx = p->yytos ? (int)(p->yytos - p->yystack) : 0;
159188 if( p->yystack==&p->yystk0 ){
159190 if( pNew ) pNew[0] = p->yystk0;
159192 pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
159195 p->yystack = pNew;
159196 p->yytos = &p->yystack[idx];
159200 yyTracePrompt, p->yystksz, newSize);
159203 p->yystksz = newSize;
159224 yypParser->yyhwm = 0;
159227 yypParser->yytos = NULL;
159228 yypParser->yystack = NULL;
159229 yypParser->yystksz = 0;
159231 yypParser->yystack = &yypParser->yystk0;
159232 yypParser->yystksz = 1;
159236 yypParser->yyerrcnt = -1;
159238 yypParser->yytos = yypParser->yystack;
159239 yypParser->yystack[0].stateno = 0;
159240 yypParser->yystack[0].major = 0;
159242 yypParser->yystackEnd = &yypParser->yystack[YYSTACKDEPTH-1];
159287 ** terminal or non-terminal is destroyed. This can happen
159302 sqlite3SelectDelete(pParse->db, (yypminor->yy307));
159318 sqlite3ExprDelete(pParse->db, (yypminor->yy602));
159335 sqlite3ExprListDelete(pParse->db, (yypminor->yy338));
159344 sqlite3SrcListDelete(pParse->db, (yypminor->yy291));
159349 sqlite3WithDelete(pParse->db, (yypminor->yy195));
159355 sqlite3WindowListDelete(pParse->db, (yypminor->yy19));
159362 sqlite3IdListDelete(pParse->db, (yypminor->yy288));
159371 sqlite3WindowDelete(pParse->db, (yypminor->yy19));
159377 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy483));
159382 sqlite3IdListDelete(pParse->db, (yypminor->yy50).b);
159389 sqlite3ExprDelete(pParse->db, (yypminor->yy113).pExpr);
159405 assert( pParser->yytos!=0 );
159406 assert( pParser->yytos > pParser->yystack );
159407 yytos = pParser->yytos--;
159412 yyTokenName[yytos->major]);
159415 yy_destructor(pParser, yytos->major, &yytos->minor);
159423 while( pParser->yytos>pParser->yystack ) yy_pop_parser_stack(pParser);
159425 if( pParser->yystack!=&pParser->yystk0 ) free(pParser->yystack);
159456 return pParser->yyhwm;
159462 ** is in state X and has a lookahead token Y. In a well-tested
159499 ** look-ahead token iLookAhead.
159502 YYCODETYPE iLookAhead, /* The look-ahead token */
159540 int j = i - iLookAhead + YYWILDCARD;
159563 ** Find the appropriate action for a parser given the non-terminal
159564 ** look-ahead token iLookAhead.
159568 YYCODETYPE iLookAhead /* The look-ahead token */
159603 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
159622 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
159626 yyTracePrompt, zTag, yyTokenName[yypParser->yytos->major],
159627 yyNewState - YY_MIN_REDUCE);
159645 yypParser->yytos++;
159647 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
159648 yypParser->yyhwm++;
159649 assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
159653 if( yypParser->yytos>yypParser->yystackEnd ){
159654 yypParser->yytos--;
159659 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
159661 yypParser->yytos--;
159668 yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;
159670 yytos = yypParser->yytos;
159671 yytos->stateno = yyNewState;
159672 yytos->major = yyMajor;
159673 yytos->minor.yy0 = yyMinor;
159677 /* For rule J, yyRuleInfoLhs[J] contains the symbol on the left-hand side
160081 ** of symbols on the right-hand side of that rule. */
160083 -1, /* (0) explain ::= EXPLAIN */
160084 -3, /* (1) explain ::= EXPLAIN QUERY PLAN */
160085 -1, /* (2) cmdx ::= cmd */
160086 -3, /* (3) cmd ::= BEGIN transtype trans_opt */
160088 -1, /* (5) transtype ::= DEFERRED */
160089 -1, /* (6) transtype ::= IMMEDIATE */
160090 -1, /* (7) transtype ::= EXCLUSIVE */
160091 -2, /* (8) cmd ::= COMMIT|END trans_opt */
160092 -2, /* (9) cmd ::= ROLLBACK trans_opt */
160093 -2, /* (10) cmd ::= SAVEPOINT nm */
160094 -3, /* (11) cmd ::= RELEASE savepoint_opt nm */
160095 -5, /* (12) cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
160096 -6, /* (13) create_table ::= createkw temp TABLE ifnotexists nm dbnm */
160097 -1, /* (14) createkw ::= CREATE */
160099 -3, /* (16) ifnotexists ::= IF NOT EXISTS */
160100 -1, /* (17) temp ::= TEMP */
160102 -5, /* (19) create_table_args ::= LP columnlist conslist_opt RP table_options */
160103 -2, /* (20) create_table_args ::= AS select */
160105 -2, /* (22) table_options ::= WITHOUT nm */
160106 -2, /* (23) columnname ::= nm typetoken */
160108 -4, /* (25) typetoken ::= typename LP signed RP */
160109 -6, /* (26) typetoken ::= typename LP signed COMMA signed RP */
160110 -2, /* (27) typename ::= typename ID|STRING */
160113 -2, /* (30) ccons ::= CONSTRAINT nm */
160114 -3, /* (31) ccons ::= DEFAULT scantok term */
160115 -4, /* (32) ccons ::= DEFAULT LP expr RP */
160116 -4, /* (33) ccons ::= DEFAULT PLUS scantok term */
160117 -4, /* (34) ccons ::= DEFAULT MINUS scantok term */
160118 -3, /* (35) ccons ::= DEFAULT scantok ID|INDEXED */
160119 -3, /* (36) ccons ::= NOT NULL onconf */
160120 -5, /* (37) ccons ::= PRIMARY KEY sortorder onconf autoinc */
160121 -2, /* (38) ccons ::= UNIQUE onconf */
160122 -4, /* (39) ccons ::= CHECK LP expr RP */
160123 -4, /* (40) ccons ::= REFERENCES nm eidlist_opt refargs */
160124 -1, /* (41) ccons ::= defer_subclause */
160125 -2, /* (42) ccons ::= COLLATE ID|STRING */
160126 -3, /* (43) generated ::= LP expr RP */
160127 -4, /* (44) generated ::= LP expr RP ID */
160129 -1, /* (46) autoinc ::= AUTOINCR */
160131 -2, /* (48) refargs ::= refargs refarg */
160132 -2, /* (49) refarg ::= MATCH nm */
160133 -3, /* (50) refarg ::= ON INSERT refact */
160134 -3, /* (51) refarg ::= ON DELETE refact */
160135 -3, /* (52) refarg ::= ON UPDATE refact */
160136 -2, /* (53) refact ::= SET NULL */
160137 -2, /* (54) refact ::= SET DEFAULT */
160138 -1, /* (55) refact ::= CASCADE */
160139 -1, /* (56) refact ::= RESTRICT */
160140 -2, /* (57) refact ::= NO ACTION */
160141 -3, /* (58) defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */
160142 -2, /* (59) defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
160144 -2, /* (61) init_deferred_pred_opt ::= INITIALLY DEFERRED */
160145 -2, /* (62) init_deferred_pred_opt ::= INITIALLY IMMEDIATE */
160147 -1, /* (64) tconscomma ::= COMMA */
160148 -2, /* (65) tcons ::= CONSTRAINT nm */
160149 -7, /* (66) tcons ::= PRIMARY KEY LP sortlist autoinc RP onconf */
160150 -5, /* (67) tcons ::= UNIQUE LP sortlist RP onconf */
160151 -5, /* (68) tcons ::= CHECK LP expr RP onconf */
160152-10, /* (69) tcons ::= FOREIGN KEY LP eidlist RP REFERENCES nm eidlist_opt refargs defer_subclaus…
160155 -3, /* (72) onconf ::= ON CONFLICT resolvetype */
160157 -2, /* (74) orconf ::= OR resolvetype */
160158 -1, /* (75) resolvetype ::= IGNORE */
160159 -1, /* (76) resolvetype ::= REPLACE */
160160 -4, /* (77) cmd ::= DROP TABLE ifexists fullname */
160161 -2, /* (78) ifexists ::= IF EXISTS */
160163 -9, /* (80) cmd ::= createkw temp VIEW ifnotexists nm dbnm eidlist_opt AS select */
160164 -4, /* (81) cmd ::= DROP VIEW ifexists fullname */
160165 -1, /* (82) cmd ::= select */
160166 -3, /* (83) select ::= WITH wqlist selectnowith */
160167 -4, /* (84) select ::= WITH RECURSIVE wqlist selectnowith */
160168 -1, /* (85) select ::= selectnowith */
160169 -3, /* (86) selectnowith ::= selectnowith multiselect_op oneselect */
160170 -1, /* (87) multiselect_op ::= UNION */
160171 -2, /* (88) multiselect_op ::= UNION ALL */
160172 -1, /* (89) multiselect_op ::= EXCEPT|INTERSECT */
160173-9, /* (90) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderb…
160174-10, /* (91) oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt windo…
160175 -4, /* (92) values ::= VALUES LP nexprlist RP */
160176 -5, /* (93) values ::= values COMMA LP nexprlist RP */
160177 -1, /* (94) distinct ::= DISTINCT */
160178 -1, /* (95) distinct ::= ALL */
160181 -5, /* (98) selcollist ::= sclp scanpt expr scanpt as */
160182 -3, /* (99) selcollist ::= sclp scanpt STAR */
160183 -5, /* (100) selcollist ::= sclp scanpt nm DOT STAR */
160184 -2, /* (101) as ::= AS nm */
160187 -2, /* (104) from ::= FROM seltablist */
160188 -2, /* (105) stl_prefix ::= seltablist joinop */
160190 -7, /* (107) seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
160191 -9, /* (108) seltablist ::= stl_prefix nm dbnm LP exprlist RP as on_opt using_opt */
160192 -7, /* (109) seltablist ::= stl_prefix LP select RP as on_opt using_opt */
160193 -7, /* (110) seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
160195 -2, /* (112) dbnm ::= DOT nm */
160196 -1, /* (113) fullname ::= nm */
160197 -3, /* (114) fullname ::= nm DOT nm */
160198 -1, /* (115) xfullname ::= nm */
160199 -3, /* (116) xfullname ::= nm DOT nm */
160200 -5, /* (117) xfullname ::= nm DOT nm AS nm */
160201 -3, /* (118) xfullname ::= nm AS nm */
160202 -1, /* (119) joinop ::= COMMA|JOIN */
160203 -2, /* (120) joinop ::= JOIN_KW JOIN */
160204 -3, /* (121) joinop ::= JOIN_KW nm JOIN */
160205 -4, /* (122) joinop ::= JOIN_KW nm nm JOIN */
160206 -2, /* (123) on_opt ::= ON expr */
160209 -3, /* (126) indexed_opt ::= INDEXED BY nm */
160210 -2, /* (127) indexed_opt ::= NOT INDEXED */
160211 -4, /* (128) using_opt ::= USING LP idlist RP */
160214 -3, /* (131) orderby_opt ::= ORDER BY sortlist */
160215 -5, /* (132) sortlist ::= sortlist COMMA expr sortorder nulls */
160216 -3, /* (133) sortlist ::= expr sortorder nulls */
160217 -1, /* (134) sortorder ::= ASC */
160218 -1, /* (135) sortorder ::= DESC */
160220 -2, /* (137) nulls ::= NULLS FIRST */
160221 -2, /* (138) nulls ::= NULLS LAST */
160224 -3, /* (141) groupby_opt ::= GROUP BY nexprlist */
160226 -2, /* (143) having_opt ::= HAVING expr */
160228 -2, /* (145) limit_opt ::= LIMIT expr */
160229 -4, /* (146) limit_opt ::= LIMIT expr OFFSET expr */
160230 -4, /* (147) limit_opt ::= LIMIT expr COMMA expr */
160231 -6, /* (148) cmd ::= with DELETE FROM xfullname indexed_opt where_opt_ret */
160233 -2, /* (150) where_opt ::= WHERE expr */
160235 -2, /* (152) where_opt_ret ::= WHERE expr */
160236 -2, /* (153) where_opt_ret ::= RETURNING selcollist */
160237 -4, /* (154) where_opt_ret ::= WHERE expr RETURNING selcollist */
160238 -9, /* (155) cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
160239 -5, /* (156) setlist ::= setlist COMMA nm EQ expr */
160240 -7, /* (157) setlist ::= setlist COMMA LP idlist RP EQ expr */
160241 -3, /* (158) setlist ::= nm EQ expr */
160242 -5, /* (159) setlist ::= LP idlist RP EQ expr */
160243 -7, /* (160) cmd ::= with insert_cmd INTO xfullname idlist_opt select upsert */
160244 -8, /* (161) cmd ::= with insert_cmd INTO xfullname idlist_opt DEFAULT VALUES returning */
160246 -2, /* (163) upsert ::= RETURNING selcollist */
160247-12, /* (164) upsert ::= ON CONFLICT LP sortlist RP where_opt DO UPDATE SET setlist where_opt ups…
160248 -9, /* (165) upsert ::= ON CONFLICT LP sortlist RP where_opt DO NOTHING upsert */
160249 -5, /* (166) upsert ::= ON CONFLICT DO NOTHING returning */
160250 -8, /* (167) upsert ::= ON CONFLICT DO UPDATE SET setlist where_opt returning */
160251 -2, /* (168) returning ::= RETURNING selcollist */
160252 -2, /* (169) insert_cmd ::= INSERT orconf */
160253 -1, /* (170) insert_cmd ::= REPLACE */
160255 -3, /* (172) idlist_opt ::= LP idlist RP */
160256 -3, /* (173) idlist ::= idlist COMMA nm */
160257 -1, /* (174) idlist ::= nm */
160258 -3, /* (175) expr ::= LP expr RP */
160259 -1, /* (176) expr ::= ID|INDEXED */
160260 -1, /* (177) expr ::= JOIN_KW */
160261 -3, /* (178) expr ::= nm DOT nm */
160262 -5, /* (179) expr ::= nm DOT nm DOT nm */
160263 -1, /* (180) term ::= NULL|FLOAT|BLOB */
160264 -1, /* (181) term ::= STRING */
160265 -1, /* (182) term ::= INTEGER */
160266 -1, /* (183) expr ::= VARIABLE */
160267 -3, /* (184) expr ::= expr COLLATE ID|STRING */
160268 -6, /* (185) expr ::= CAST LP expr AS typetoken RP */
160269 -5, /* (186) expr ::= ID|INDEXED LP distinct exprlist RP */
160270 -4, /* (187) expr ::= ID|INDEXED LP STAR RP */
160271 -6, /* (188) expr ::= ID|INDEXED LP distinct exprlist RP filter_over */
160272 -5, /* (189) expr ::= ID|INDEXED LP STAR RP filter_over */
160273 -1, /* (190) term ::= CTIME_KW */
160274 -5, /* (191) expr ::= LP nexprlist COMMA expr RP */
160275 -3, /* (192) expr ::= expr AND expr */
160276 -3, /* (193) expr ::= expr OR expr */
160277 -3, /* (194) expr ::= expr LT|GT|GE|LE expr */
160278 -3, /* (195) expr ::= expr EQ|NE expr */
160279 -3, /* (196) expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */
160280 -3, /* (197) expr ::= expr PLUS|MINUS expr */
160281 -3, /* (198) expr ::= expr STAR|SLASH|REM expr */
160282 -3, /* (199) expr ::= expr CONCAT expr */
160283 -2, /* (200) likeop ::= NOT LIKE_KW|MATCH */
160284 -3, /* (201) expr ::= expr likeop expr */
160285 -5, /* (202) expr ::= expr likeop expr ESCAPE expr */
160286 -2, /* (203) expr ::= expr ISNULL|NOTNULL */
160287 -3, /* (204) expr ::= expr NOT NULL */
160288 -3, /* (205) expr ::= expr IS expr */
160289 -4, /* (206) expr ::= expr IS NOT expr */
160290 -2, /* (207) expr ::= NOT expr */
160291 -2, /* (208) expr ::= BITNOT expr */
160292 -2, /* (209) expr ::= PLUS|MINUS expr */
160293 -1, /* (210) between_op ::= BETWEEN */
160294 -2, /* (211) between_op ::= NOT BETWEEN */
160295 -5, /* (212) expr ::= expr between_op expr AND expr */
160296 -1, /* (213) in_op ::= IN */
160297 -2, /* (214) in_op ::= NOT IN */
160298 -5, /* (215) expr ::= expr in_op LP exprlist RP */
160299 -3, /* (216) expr ::= LP select RP */
160300 -5, /* (217) expr ::= expr in_op LP select RP */
160301 -5, /* (218) expr ::= expr in_op nm dbnm paren_exprlist */
160302 -4, /* (219) expr ::= EXISTS LP select RP */
160303 -5, /* (220) expr ::= CASE case_operand case_exprlist case_else END */
160304 -5, /* (221) case_exprlist ::= case_exprlist WHEN expr THEN expr */
160305 -4, /* (222) case_exprlist ::= WHEN expr THEN expr */
160306 -2, /* (223) case_else ::= ELSE expr */
160308 -1, /* (225) case_operand ::= expr */
160311 -3, /* (228) nexprlist ::= nexprlist COMMA expr */
160312 -1, /* (229) nexprlist ::= expr */
160314 -3, /* (231) paren_exprlist ::= LP exprlist RP */
160315-12, /* (232) cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP sortlist RP where_op…
160316 -1, /* (233) uniqueflag ::= UNIQUE */
160319 -3, /* (236) eidlist_opt ::= LP eidlist RP */
160320 -5, /* (237) eidlist ::= eidlist COMMA nm collate sortorder */
160321 -3, /* (238) eidlist ::= nm collate sortorder */
160323 -2, /* (240) collate ::= COLLATE ID|STRING */
160324 -4, /* (241) cmd ::= DROP INDEX ifexists fullname */
160325 -2, /* (242) cmd ::= VACUUM vinto */
160326 -3, /* (243) cmd ::= VACUUM nm vinto */
160327 -2, /* (244) vinto ::= INTO expr */
160329 -3, /* (246) cmd ::= PRAGMA nm dbnm */
160330 -5, /* (247) cmd ::= PRAGMA nm dbnm EQ nmnum */
160331 -6, /* (248) cmd ::= PRAGMA nm dbnm LP nmnum RP */
160332 -5, /* (249) cmd ::= PRAGMA nm dbnm EQ minus_num */
160333 -6, /* (250) cmd ::= PRAGMA nm dbnm LP minus_num RP */
160334 -2, /* (251) plus_num ::= PLUS INTEGER|FLOAT */
160335 -2, /* (252) minus_num ::= MINUS INTEGER|FLOAT */
160336 -5, /* (253) cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
160337-11, /* (254) trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON ful…
160338 -1, /* (255) trigger_time ::= BEFORE|AFTER */
160339 -2, /* (256) trigger_time ::= INSTEAD OF */
160341 -1, /* (258) trigger_event ::= DELETE|INSERT */
160342 -1, /* (259) trigger_event ::= UPDATE */
160343 -3, /* (260) trigger_event ::= UPDATE OF idlist */
160345 -2, /* (262) when_clause ::= WHEN expr */
160346 -3, /* (263) trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
160347 -2, /* (264) trigger_cmd_list ::= trigger_cmd SEMI */
160348 -3, /* (265) trnm ::= nm DOT nm */
160349 -3, /* (266) tridxby ::= INDEXED BY nm */
160350 -2, /* (267) tridxby ::= NOT INDEXED */
160351 -9, /* (268) trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist from where_opt scanpt */
160352 -8, /* (269) trigger_cmd ::= scanpt insert_cmd INTO trnm idlist_opt select upsert scanpt */
160353 -6, /* (270) trigger_cmd ::= DELETE FROM trnm tridxby where_opt scanpt */
160354 -3, /* (271) trigger_cmd ::= scanpt select scanpt */
160355 -4, /* (272) expr ::= RAISE LP IGNORE RP */
160356 -6, /* (273) expr ::= RAISE LP raisetype COMMA nm RP */
160357 -1, /* (274) raisetype ::= ROLLBACK */
160358 -1, /* (275) raisetype ::= ABORT */
160359 -1, /* (276) raisetype ::= FAIL */
160360 -4, /* (277) cmd ::= DROP TRIGGER ifexists fullname */
160361 -6, /* (278) cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
160362 -3, /* (279) cmd ::= DETACH database_kw_opt expr */
160364 -2, /* (281) key_opt ::= KEY expr */
160365 -1, /* (282) cmd ::= REINDEX */
160366 -3, /* (283) cmd ::= REINDEX nm dbnm */
160367 -1, /* (284) cmd ::= ANALYZE */
160368 -3, /* (285) cmd ::= ANALYZE nm dbnm */
160369 -6, /* (286) cmd ::= ALTER TABLE fullname RENAME TO nm */
160370 -7, /* (287) cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt columnname carglist */
160371 -6, /* (288) cmd ::= ALTER TABLE fullname DROP kwcolumn_opt nm */
160372 -1, /* (289) add_column_fullname ::= fullname */
160373 -8, /* (290) cmd ::= ALTER TABLE fullname RENAME kwcolumn_opt nm TO nm */
160374 -1, /* (291) cmd ::= create_vtab */
160375 -4, /* (292) cmd ::= create_vtab LP vtabarglist RP */
160376 -8, /* (293) create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
160378 -1, /* (295) vtabargtoken ::= ANY */
160379 -3, /* (296) vtabargtoken ::= lp anylist RP */
160380 -1, /* (297) lp ::= LP */
160381 -2, /* (298) with ::= WITH wqlist */
160382 -3, /* (299) with ::= WITH RECURSIVE wqlist */
160383 -1, /* (300) wqas ::= AS */
160384 -2, /* (301) wqas ::= AS MATERIALIZED */
160385 -3, /* (302) wqas ::= AS NOT MATERIALIZED */
160386 -6, /* (303) wqitem ::= nm eidlist_opt wqas LP select RP */
160387 -1, /* (304) wqlist ::= wqitem */
160388 -3, /* (305) wqlist ::= wqlist COMMA wqitem */
160389 -1, /* (306) windowdefn_list ::= windowdefn */
160390 -3, /* (307) windowdefn_list ::= windowdefn_list COMMA windowdefn */
160391 -5, /* (308) windowdefn ::= nm AS LP window RP */
160392 -5, /* (309) window ::= PARTITION BY nexprlist orderby_opt frame_opt */
160393 -6, /* (310) window ::= nm PARTITION BY nexprlist orderby_opt frame_opt */
160394 -4, /* (311) window ::= ORDER BY sortlist frame_opt */
160395 -5, /* (312) window ::= nm ORDER BY sortlist frame_opt */
160396 -1, /* (313) window ::= frame_opt */
160397 -2, /* (314) window ::= nm frame_opt */
160399 -3, /* (316) frame_opt ::= range_or_rows frame_bound_s frame_exclude_opt */
160400-6, /* (317) frame_opt ::= range_or_rows BETWEEN frame_bound_s AND frame_bound_e frame_exclude_op…
160401 -1, /* (318) range_or_rows ::= RANGE|ROWS|GROUPS */
160402 -1, /* (319) frame_bound_s ::= frame_bound */
160403 -2, /* (320) frame_bound_s ::= UNBOUNDED PRECEDING */
160404 -1, /* (321) frame_bound_e ::= frame_bound */
160405 -2, /* (322) frame_bound_e ::= UNBOUNDED FOLLOWING */
160406 -2, /* (323) frame_bound ::= expr PRECEDING|FOLLOWING */
160407 -2, /* (324) frame_bound ::= CURRENT ROW */
160409 -2, /* (326) frame_exclude_opt ::= EXCLUDE frame_exclude */
160410 -2, /* (327) frame_exclude ::= NO OTHERS */
160411 -2, /* (328) frame_exclude ::= CURRENT ROW */
160412 -1, /* (329) frame_exclude ::= GROUP|TIES */
160413 -2, /* (330) window_clause ::= WINDOW windowdefn_list */
160414 -2, /* (331) filter_over ::= filter_clause over_clause */
160415 -1, /* (332) filter_over ::= over_clause */
160416 -1, /* (333) filter_over ::= filter_clause */
160417 -4, /* (334) over_clause ::= OVER LP window RP */
160418 -2, /* (335) over_clause ::= OVER nm */
160419 -5, /* (336) filter_clause ::= FILTER LP WHERE expr RP */
160420 -1, /* (337) input ::= cmdlist */
160421 -2, /* (338) cmdlist ::= cmdlist ecmd */
160422 -1, /* (339) cmdlist ::= ecmd */
160423 -1, /* (340) ecmd ::= SEMI */
160424 -2, /* (341) ecmd ::= cmdx SEMI */
160425 -3, /* (342) ecmd ::= explain cmdx SEMI */
160427 -1, /* (344) trans_opt ::= TRANSACTION */
160428 -2, /* (345) trans_opt ::= TRANSACTION nm */
160429 -1, /* (346) savepoint_opt ::= SAVEPOINT */
160431 -2, /* (348) cmd ::= create_table create_table_args */
160432 -4, /* (349) columnlist ::= columnlist COMMA columnname carglist */
160433 -2, /* (350) columnlist ::= columnname carglist */
160434 -1, /* (351) nm ::= ID|INDEXED */
160435 -1, /* (352) nm ::= STRING */
160436 -1, /* (353) nm ::= JOIN_KW */
160437 -1, /* (354) typetoken ::= typename */
160438 -1, /* (355) typename ::= ID|STRING */
160439 -1, /* (356) signed ::= plus_num */
160440 -1, /* (357) signed ::= minus_num */
160441 -2, /* (358) carglist ::= carglist ccons */
160443 -2, /* (360) ccons ::= NULL onconf */
160444 -4, /* (361) ccons ::= GENERATED ALWAYS AS generated */
160445 -2, /* (362) ccons ::= AS generated */
160446 -2, /* (363) conslist_opt ::= COMMA conslist */
160447 -3, /* (364) conslist ::= conslist tconscomma tcons */
160448 -1, /* (365) conslist ::= tcons */
160450 -1, /* (367) defer_subclause_opt ::= defer_subclause */
160451 -1, /* (368) resolvetype ::= raisetype */
160452 -1, /* (369) selectnowith ::= oneselect */
160453 -1, /* (370) oneselect ::= values */
160454 -2, /* (371) sclp ::= selcollist COMMA */
160455 -1, /* (372) as ::= ID|STRING */
160457 -1, /* (374) expr ::= term */
160458 -1, /* (375) likeop ::= LIKE_KW|MATCH */
160459 -1, /* (376) exprlist ::= nexprlist */
160460 -1, /* (377) nmnum ::= plus_num */
160461 -1, /* (378) nmnum ::= nm */
160462 -1, /* (379) nmnum ::= ON */
160463 -1, /* (380) nmnum ::= DELETE */
160464 -1, /* (381) nmnum ::= DEFAULT */
160465 -1, /* (382) plus_num ::= INTEGER|FLOAT */
160467 -3, /* (384) foreach_clause ::= FOR EACH ROW */
160468 -1, /* (385) trnm ::= nm */
160470 -1, /* (387) database_kw_opt ::= DATABASE */
160473 -1, /* (390) kwcolumn_opt ::= COLUMNKW */
160474 -1, /* (391) vtabarglist ::= vtabarg */
160475 -3, /* (392) vtabarglist ::= vtabarglist COMMA vtabarg */
160476 -2, /* (393) vtabarg ::= vtabarg vtabargtoken */
160478 -4, /* (395) anylist ::= anylist LP anylist RP */
160479 -2, /* (396) anylist ::= anylist ANY */
160492 ** only called from one place, optimizing compilers will in-line it, which
160509 yymsp = yypParser->yytos;
160523 { pParse->explain = 1; }
160526 { pParse->explain = 2; }
160532 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy60);}
160541 {yymsp[0].minor.yy60 = yymsp[0].major; /*A-overwrites-X*/}
160545 {sqlite3EndTransaction(pParse,yymsp[-1].major);}
160564 …sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy60,0,0,yymsp[-
160582 {yymsp[-2].minor.yy60 = 1;}
160590 sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy60,0);
160596 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy307);
160602 yymsp[-1].minor.yy60 = TF_WithoutRowid | TF_NoVisibleRowid;
160604 yymsp[-1].minor.yy60 = 0;
160610 {sqlite3AddColumn(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
160619 …yymsp[-3].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
160624 …yymsp[-5].minor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
160628 {yymsp[-1].minor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);}
160644 {pParse->constraintName = yymsp[0].minor.yy0;}
160647 …te3AddDefaultValue(pParse,yymsp[0].minor.yy602,yymsp[-1].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
160650 {sqlite3AddDefaultValue(pParse,yymsp[-1].minor.yy602,yymsp[-2].minor.yy0.z+1,yymsp[0].minor.yy0.z);}
160653 …te3AddDefaultValue(pParse,yymsp[0].minor.yy602,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[
160658 …sqlite3AddDefaultValue(pParse,p,yymsp[-2].minor.yy0.z,&yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n…
160666 testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
160675 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy60,yymsp[0].minor.yy60,yymsp[-2].minor.yy60);}
160682 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy602,yymsp[-2].minor.yy0.z,yymsp[0].minor.yy0.z)…
160685 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy338,yymsp[0].minor.yy60);}
160694 {sqlite3AddGenerated(pParse,yymsp[-1].minor.yy602,0);}
160697 {sqlite3AddGenerated(pParse,yymsp[-2].minor.yy602,&yymsp[0].minor.yy0);}
160700 { yymsp[1].minor.yy60 = OE_None*0x0101; /* EV: R-19803-45884 */}
160703 { yymsp[-1].minor.yy60 = (yymsp[-1].minor.yy60 & ~yymsp[0].minor.yy615.mask) | yymsp[0].minor.yy615…
160706 { yymsp[-1].minor.yy615.value = 0; yymsp[-1].minor.yy615.mask = 0x000000; }
160709 { yymsp[-2].minor.yy615.value = 0; yymsp[-2].minor.yy615.mask = 0x000000; }
160712 { yymsp[-2].minor.yy615.value = yymsp[0].minor.yy60; yymsp[-2].minor.yy615.mask = 0x0000ff; }
160715 { yymsp[-2].minor.yy615.value = yymsp[0].minor.yy60<<8; yymsp[-2].minor.yy615.mask = 0x00ff00; }
160718 { yymsp[-1].minor.yy60 = OE_SetNull; /* EV: R-33326-45252 */}
160721 { yymsp[-1].minor.yy60 = OE_SetDflt; /* EV: R-33326-45252 */}
160724 { yymsp[0].minor.yy60 = OE_Cascade; /* EV: R-33326-45252 */}
160727 { yymsp[0].minor.yy60 = OE_Restrict; /* EV: R-33326-45252 */}
160730 { yymsp[-1].minor.yy60 = OE_None; /* EV: R-33326-45252 */}
160733 {yymsp[-2].minor.yy60 = 0;}
160738 {yymsp[-1].minor.yy60 = yymsp[0].minor.yy60;}
160745 {yymsp[-1].minor.yy60 = 1;}
160748 {yymsp[-1].minor.yy60 = 0;}
160751 {pParse->constraintName.n = 0;}
160754 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy338,yymsp[0].minor.yy60,yymsp[-2].minor.yy60,0);}
160757 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy338,yymsp[0].minor.yy60,0,0,0,0,
160761 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy602,yymsp[-3].minor.yy0.z,yymsp[-1].minor.yy0.z…
160765 …sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy338, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy338…
160774 {yymsp[-2].minor.yy60 = yymsp[0].minor.yy60;}
160785 sqlite3DropTable(pParse, yymsp[0].minor.yy291, 0, yymsp[-1].minor.yy60);
160790 …e, &yymsp[-8].minor.yy0, &yymsp[-4].minor.yy0, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy338, yymsp[…
160795 sqlite3DropTable(pParse, yymsp[0].minor.yy291, 1, yymsp[-1].minor.yy60);
160802 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy307);
160806 {yymsp[-2].minor.yy307 = attachWithToSelect(pParse,yymsp[0].minor.yy307,yymsp[-1].minor.yy195);}
160809 {yymsp[-3].minor.yy307 = attachWithToSelect(pParse,yymsp[0].minor.yy307,yymsp[-1].minor.yy195);}
160817 yymsp[0].minor.yy307 = p; /*A-overwrites-X*/
160823 Select *pLhs = yymsp[-2].minor.yy307;
160824 if( pRhs && pRhs->pPrior ){
160833 pRhs->op = (u8)yymsp[-1].minor.yy60;
160834 pRhs->pPrior = pLhs;
160835 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
160836 pRhs->selFlags &= ~SF_MultiValue;
160837 if( yymsp[-1].minor.yy60!=TK_ALL ) pParse->hasCompound = 1;
160839 sqlite3SelectDelete(pParse->db, pLhs);
160841 yymsp[-2].minor.yy307 = pRhs;
160846 {yymsp[0].minor.yy60 = yymsp[0].major; /*A-overwrites-OP*/}
160849 {yymsp[-1].minor.yy60 = TK_ALL;}
160853-8].minor.yy307 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy338,yymsp[-5].minor.yy291,yymsp[-4].mi…
160858-9].minor.yy307 = sqlite3SelectNew(pParse,yymsp[-7].minor.yy338,yymsp[-6].minor.yy291,yymsp[-5].mi…
160859 if( yymsp[-9].minor.yy307 ){
160860 yymsp[-9].minor.yy307->pWinDefn = yymsp[-2].minor.yy19;
160862 sqlite3WindowListDelete(pParse->db, yymsp[-2].minor.yy19);
160868 yymsp[-3].minor.yy307 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy338,0,0,0,0,0,SF_Values,0);
160873 Select *pRight, *pLeft = yymsp[-4].minor.yy307;
160874 pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy338,0,0,0,0,0,SF_Values|SF_MultiValue,0);
160875 if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
160877 pRight->op = TK_ALL;
160878 pRight->pPrior = pLeft;
160879 yymsp[-4].minor.yy307 = pRight;
160881 yymsp[-4].minor.yy307 = pLeft;
160901 …yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy338, yymsp[-2].minor.yy602…
160902 …if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy338, &yymsp[0].minor…
160903 sqlite3ExprListSetSpan(pParse,yymsp[-4].minor.yy338,yymsp[-3].minor.yy528,yymsp[-1].minor.yy528);
160908 Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
160909 yymsp[-2].minor.yy338 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy338, p);
160915 Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
160917 yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy338, pDot);
160924 {yymsp[-1].minor.yy0 = yymsp[0].minor.yy0;}
160932 yymsp[-1].minor.yy291 = yymsp[0].minor.yy291;
160933 sqlite3SrcListShiftJoinType(yymsp[-1].minor.yy291);
160938 …if( ALWAYS(yymsp[-1].minor.yy291 && yymsp[-1].minor.yy291->nSrc>0) ) yymsp[-1].minor.yy291->a[yyms…
160943 …p[-6].minor.yy291 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy291,&yymsp[-5].minor.yy0…
160944 sqlite3SrcListIndexedBy(pParse, yymsp[-6].minor.yy291, &yymsp[-2].minor.yy0);
160949 …p[-8].minor.yy291 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-8].minor.yy291,&yymsp[-7].minor.yy0…
160950 sqlite3SrcListFuncArgs(pParse, yymsp[-8].minor.yy291, yymsp[-4].minor.yy338);
160955 …yymsp[-6].minor.yy291 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy291,0,0,&yymsp[-2].m…
160960 …if( yymsp[-6].minor.yy291==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy602==0 && yymsp[0].m…
160961 yymsp[-6].minor.yy291 = yymsp[-4].minor.yy291;
160962 }else if( yymsp[-4].minor.yy291->nSrc==1 ){
160963 …yymsp[-6].minor.yy291 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy291,0,0,&yymsp[-2].m…
160964 if( yymsp[-6].minor.yy291 ){
160965 SrcItem *pNew = &yymsp[-6].minor.yy291->a[yymsp[-6].minor.yy291->nSrc-1];
160966 SrcItem *pOld = yymsp[-4].minor.yy291->a;
160967 pNew->zName = pOld->zName;
160968 pNew->zDatabase = pOld->zDatabase;
160969 pNew->pSelect = pOld->pSelect;
160970 if( pOld->fg.isTabFunc ){
160971 pNew->u1.pFuncArg = pOld->u1.pFuncArg;
160972 pOld->u1.pFuncArg = 0;
160973 pOld->fg.isTabFunc = 0;
160974 pNew->fg.isTabFunc = 1;
160976 pOld->zName = pOld->zDatabase = 0;
160977 pOld->pSelect = 0;
160979 sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy291);
160982 sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy291);
160983 pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy291,0,0,0,0,SF_NestedFrom,0);
160984 …yymsp[-6].minor.yy291 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy291,0,0,&yymsp[-2].m…
160995 …if( IN_RENAME_OBJECT && yylhsminor.yy291 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy291->a[0].zN…
161001 yylhsminor.yy291 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
161002 …if( IN_RENAME_OBJECT && yylhsminor.yy291 ) sqlite3RenameTokenMap(pParse, yylhsminor.yy291->a[0].zN…
161004 yymsp[-2].minor.yy291 = yylhsminor.yy291;
161007 {yymsp[0].minor.yy291 = sqlite3SrcListAppend(pParse,0,&yymsp[0].minor.yy0,0); /*A-overwrites-X*/}
161010 {yymsp[-2].minor.yy291 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); /…
161014 …yymsp[-4].minor.yy291 = sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,&yymsp[-2].minor.yy0); …
161015 …if( yymsp[-4].minor.yy291 ) yymsp[-4].minor.yy291->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
161020 yymsp[-2].minor.yy291 = sqlite3SrcListAppend(pParse,0,&yymsp[-2].minor.yy0,0); /*A-overwrites-X*/
161021 …if( yymsp[-2].minor.yy291 ) yymsp[-2].minor.yy291->a[0].zAlias = sqlite3NameFromToken(pParse->db, …
161028 {yymsp[-1].minor.yy60 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); /*X-overwrites-A*/}
161031 {yymsp[-2].minor.yy60 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); /*X-ov…
161034 {yymsp[-3].minor.yy60 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1]…
161042 {yymsp[-1].minor.yy602 = yymsp[0].minor.yy602;}
161055 {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
161058 {yymsp[-1].minor.yy0.z=0; yymsp[-1].minor.yy0.n=1;}
161061 {yymsp[-3].minor.yy288 = yymsp[-1].minor.yy288;}
161069 {yymsp[-2].minor.yy338 = yymsp[0].minor.yy338;}
161073 yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy338,yymsp[-2].minor.yy602);
161074 sqlite3ExprListSetSortOrder(yymsp[-4].minor.yy338,yymsp[-1].minor.yy60,yymsp[0].minor.yy60);
161079 yymsp[-2].minor.yy338 = sqlite3ExprListAppend(pParse,0,yymsp[-2].minor.yy602); /*A-overwrites-Y*/
161080 sqlite3ExprListSetSortOrder(yymsp[-2].minor.yy338,yymsp[-1].minor.yy60,yymsp[0].minor.yy60);
161094 {yymsp[-1].minor.yy60 = SQLITE_SO_ASC;}
161097 {yymsp[-1].minor.yy60 = SQLITE_SO_DESC;}
161100 {yymsp[-1].minor.yy602 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy602,0);}
161103 {yymsp[-3].minor.yy602 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy602,yymsp[0].minor.yy602);}
161106 {yymsp[-3].minor.yy602 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy602,yymsp[-2].minor.yy602);}
161110 sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy291, &yymsp[-1].minor.yy0);
161111 sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy291,yymsp[0].minor.yy602,0,0);
161115 {sqlite3AddReturning(pParse,yymsp[0].minor.yy338); yymsp[-1].minor.yy602 = 0;}
161118 {sqlite3AddReturning(pParse,yymsp[0].minor.yy338); yymsp[-3].minor.yy602 = yymsp[-2].minor.yy602;}
161122 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy291, &yymsp[-4].minor.yy0);
161123 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy338,"set list");
161124 …yymsp[-5].minor.yy291 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy291, yymsp[-1].minor.yy…
161125 …sqlite3Update(pParse,yymsp[-5].minor.yy291,yymsp[-2].minor.yy338,yymsp[0].minor.yy602,yymsp[-6].mi…
161130 …yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy338, yymsp[0].minor.yy602);
161131 sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy338, &yymsp[-2].minor.yy0, 1);
161136 …yymsp[-6].minor.yy338 = sqlite3ExprListAppendVector(pParse, yymsp[-6].minor.yy338, yymsp[-3].minor…
161142 sqlite3ExprListSetName(pParse, yylhsminor.yy338, &yymsp[-2].minor.yy0, 1);
161144 yymsp[-2].minor.yy338 = yylhsminor.yy338;
161148 …yymsp[-4].minor.yy338 = sqlite3ExprListAppendVector(pParse, 0, yymsp[-3].minor.yy288, yymsp[0].min…
161153 …sqlite3Insert(pParse, yymsp[-3].minor.yy291, yymsp[-1].minor.yy307, yymsp[-2].minor.yy288, yymsp[-
161158 sqlite3Insert(pParse, yymsp[-4].minor.yy291, 0, yymsp[-3].minor.yy288, yymsp[-6].minor.yy60, 0);
161165 { yymsp[-1].minor.yy178 = 0; sqlite3AddReturning(pParse,yymsp[0].minor.yy338); }
161168 { yymsp[-11].minor.yy178 = sqlite3UpsertNew(pParse->db,yymsp[-8].minor.yy338,yymsp[-6].minor.yy602,…
161171 { yymsp[-8].minor.yy178 = sqlite3UpsertNew(pParse->db,yymsp[-5].minor.yy338,yymsp[-3].minor.yy602,0…
161174 { yymsp[-4].minor.yy178 = sqlite3UpsertNew(pParse->db,0,0,0,0,0); }
161177 { yymsp[-7].minor.yy178 = sqlite3UpsertNew(pParse->db,0,0,yymsp[-2].minor.yy338,yymsp[-1].minor.yy6…
161183 {yymsp[-2].minor.yy288 = yymsp[-1].minor.yy288;}
161186 {yymsp[-2].minor.yy288 = sqlite3IdListAppend(pParse,yymsp[-2].minor.yy288,&yymsp[0].minor.yy0);}
161189 {yymsp[0].minor.yy288 = sqlite3IdListAppend(pParse,0,&yymsp[0].minor.yy0); /*A-overwrites-Y*/}
161192 {yymsp[-2].minor.yy602 = yymsp[-1].minor.yy602;}
161196 {yymsp[0].minor.yy602=tokenExpr(pParse,TK_ID,yymsp[0].minor.yy0); /*A-overwrites-X*/}
161200 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
161201 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
161204 sqlite3RenameTokenMap(pParse, (void*)temp1, &yymsp[-2].minor.yy0);
161208 yymsp[-2].minor.yy602 = yylhsminor.yy602;
161212 Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-4].minor.yy0, 1);
161213 Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[-2].minor.yy0, 1);
161214 Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &yymsp[0].minor.yy0, 1);
161218 sqlite3RenameTokenMap(pParse, (void*)temp2, &yymsp[-2].minor.yy0);
161222 yymsp[-4].minor.yy602 = yylhsminor.yy602;
161226 {yymsp[0].minor.yy602=tokenExpr(pParse,yymsp[0].major,yymsp[0].minor.yy0); /*A-overwrites-X*/}
161230 yylhsminor.yy602 = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &yymsp[0].minor.yy0, 1);
161243 ** in the virtual machine. #N is the N-th register. */
161244 Token t = yymsp[0].minor.yy0; /*A-overwrites-X*/
161246 if( pParse->nested==0 ){
161251 if( yymsp[0].minor.yy602 ) sqlite3GetInt32(&t.z[1], &yymsp[0].minor.yy602->iTable);
161258 …yymsp[-2].minor.yy602 = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy602, &yymsp[0].minor.…
161263 yymsp[-5].minor.yy602 = sqlite3ExprAlloc(pParse->db, TK_CAST, &yymsp[-1].minor.yy0, 1);
161264 sqlite3ExprAttachSubtrees(pParse->db, yymsp[-5].minor.yy602, yymsp[-3].minor.yy602, 0);
161269 …yylhsminor.yy602 = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy338, &yymsp[-4].minor.yy0, yymsp[
161271 yymsp[-4].minor.yy602 = yylhsminor.yy602;
161275 yylhsminor.yy602 = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0, 0);
161277 yymsp[-3].minor.yy602 = yylhsminor.yy602;
161281 …yylhsminor.yy602 = sqlite3ExprFunction(pParse, yymsp[-2].minor.yy338, &yymsp[-5].minor.yy0, yymsp[
161284 yymsp[-5].minor.yy602 = yylhsminor.yy602;
161288 yylhsminor.yy602 = sqlite3ExprFunction(pParse, 0, &yymsp[-4].minor.yy0, 0);
161291 yymsp[-4].minor.yy602 = yylhsminor.yy602;
161301 ExprList *pList = sqlite3ExprListAppend(pParse, yymsp[-3].minor.yy338, yymsp[-1].minor.yy602);
161302 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
161303 if( yymsp[-4].minor.yy602 ){
161304 yymsp[-4].minor.yy602->x.pList = pList;
161305 if( ALWAYS(pList->nExpr) ){
161306 yymsp[-4].minor.yy602->flags |= pList->a[0].pExpr->flags & EP_Propagate;
161309 sqlite3ExprListDelete(pParse->db, pList);
161314 {yymsp[-2].minor.yy602=sqlite3ExprAnd(pParse,yymsp[-2].minor.yy602,yymsp[0].minor.yy602);}
161323 {yymsp[-2].minor.yy602=sqlite3PExpr(pParse,yymsp[-1].major,yymsp[-2].minor.yy602,yymsp[0].minor.yy6…
161326 {yymsp[-1].minor.yy0=yymsp[0].minor.yy0; yymsp[-1].minor.yy0.n|=0x80000000; /*yymsp[-1].minor.yy0-o…
161331 int bNot = yymsp[-1].minor.yy0.n & 0x80000000;
161332 yymsp[-1].minor.yy0.n &= 0x7fffffff;
161334 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy602);
161335 yymsp[-2].minor.yy602 = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy0, 0);
161336 if( bNot ) yymsp[-2].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-2].minor.yy602, 0);
161337 if( yymsp[-2].minor.yy602 ) yymsp[-2].minor.yy602->flags |= EP_InfixFunc;
161343 int bNot = yymsp[-3].minor.yy0.n & 0x80000000;
161344 yymsp[-3].minor.yy0.n &= 0x7fffffff;
161345 pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy602);
161346 pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy602);
161348 yymsp[-4].minor.yy602 = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy0, 0);
161349 if( bNot ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy602, 0);
161350 if( yymsp[-4].minor.yy602 ) yymsp[-4].minor.yy602->flags |= EP_InfixFunc;
161354 {yymsp[-1].minor.yy602 = sqlite3PExpr(pParse,yymsp[0].major,yymsp[-1].minor.yy602,0);}
161357 {yymsp[-2].minor.yy602 = sqlite3PExpr(pParse,TK_NOTNULL,yymsp[-2].minor.yy602,0);}
161361 yymsp[-2].minor.yy602 = sqlite3PExpr(pParse,TK_IS,yymsp[-2].minor.yy602,yymsp[0].minor.yy602);
161362 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy602, yymsp[-2].minor.yy602, TK_ISNULL);
161367 yymsp[-3].minor.yy602 = sqlite3PExpr(pParse,TK_ISNOT,yymsp[-3].minor.yy602,yymsp[0].minor.yy602);
161368 binaryToUnaryIfNull(pParse, yymsp[0].minor.yy602, yymsp[-3].minor.yy602, TK_NOTNULL);
161373 {yymsp[-1].minor.yy602 = sqlite3PExpr(pParse, yymsp[-1].major, yymsp[0].minor.yy602, 0);/*A-overwri…
161377 …yymsp[-1].minor.yy602 = sqlite3PExpr(pParse, yymsp[-1].major==TK_PLUS ? TK_UPLUS : TK_UMINUS, yyms…
161378 /*A-overwrites-B*/
161387 ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy602);
161389 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy602, 0);
161390 if( yymsp[-4].minor.yy602 ){
161391 yymsp[-4].minor.yy602->x.pList = pList;
161393 sqlite3ExprListDelete(pParse->db, pList);
161395 …if( yymsp[-3].minor.yy60 ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy…
161400 if( yymsp[-1].minor.yy338==0 ){
161409 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy602);
161410 yymsp[-4].minor.yy602 = sqlite3Expr(pParse->db, TK_INTEGER, yymsp[-3].minor.yy60 ? "1" : "0");
161411 …}else if( yymsp[-1].minor.yy338->nExpr==1 && sqlite3ExprIsConstant(yymsp[-1].minor.yy338->a[0].pEx…
161412 Expr *pRHS = yymsp[-1].minor.yy338->a[0].pExpr;
161413 yymsp[-1].minor.yy338->a[0].pExpr = 0;
161414 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy338);
161416 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_EQ, yymsp[-4].minor.yy602, pRHS);
161417 …if( yymsp[-3].minor.yy60 ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy…
161419 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy602, 0);
161420 if( yymsp[-4].minor.yy602 ){
161421 yymsp[-4].minor.yy602->x.pList = yymsp[-1].minor.yy338;
161422 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy602);
161424 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy338);
161426 …if( yymsp[-3].minor.yy60 ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy…
161432 yymsp[-2].minor.yy602 = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
161433 sqlite3PExprAddSelect(pParse, yymsp[-2].minor.yy602, yymsp[-1].minor.yy307);
161438 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy602, 0);
161439 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy602, yymsp[-1].minor.yy307);
161440 …if( yymsp[-3].minor.yy60 ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy…
161445 SrcList *pSrc = sqlite3SrcListAppend(pParse, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
161448 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy602, 0);
161449 sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy602, pSelect);
161450 …if( yymsp[-3].minor.yy60 ) yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_NOT, yymsp[-4].minor.yy…
161456 p = yymsp[-3].minor.yy602 = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
161457 sqlite3PExprAddSelect(pParse, p, yymsp[-1].minor.yy307);
161462 yymsp[-4].minor.yy602 = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy602, 0);
161463 if( yymsp[-4].minor.yy602 ){
161464 …yymsp[-4].minor.yy602->x.pList = yymsp[-1].minor.yy602 ? sqlite3ExprListAppend(pParse,yymsp[-2].mi…
161465 sqlite3ExprSetHeightAndFlags(pParse, yymsp[-4].minor.yy602);
161467 sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy338);
161468 sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy602);
161474 …yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy338, yymsp[-2].minor.yy602);
161475 yymsp[-4].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy338, yymsp[0].minor.yy602);
161480 yymsp[-3].minor.yy338 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy602);
161481 yymsp[-3].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy338, yymsp[0].minor.yy602);
161485 {yymsp[0].minor.yy602 = yymsp[0].minor.yy602; /*A-overwrites-X*/}
161488 {yymsp[-2].minor.yy338 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy338,yymsp[0].minor.yy602);}
161491 {yymsp[0].minor.yy338 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy602); /*A-overwrites-Y*/}
161495 {yymsp[-2].minor.yy338 = yymsp[-1].minor.yy338;}
161499 sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
161500 …sqlite3SrcListAppend(pParse,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy338, yymsp[-10].minor.yy6…
161501 …&yymsp[-11].minor.yy0, yymsp[0].minor.yy602, SQLITE_SO_ASC, yymsp[-8].minor.yy60, SQLITE_IDXTYPE_A…
161502 if( IN_RENAME_OBJECT && pParse->pNewIndex ){
161503 sqlite3RenameTokenMap(pParse, pParse->pNewIndex->zName, &yymsp[-4].minor.yy0);
161516 …yymsp[-4].minor.yy338 = parserAddExprIdListTerm(pParse, yymsp[-4].minor.yy338, &yymsp[-2].minor.yy…
161521 …yymsp[-2].minor.yy338 = parserAddExprIdListTerm(pParse, 0, &yymsp[-2].minor.yy0, yymsp[-1].minor.y…
161525 {sqlite3DropIndex(pParse, yymsp[0].minor.yy291, yymsp[-1].minor.yy60);}
161531 {sqlite3Vacuum(pParse,&yymsp[-1].minor.yy0,yymsp[0].minor.yy602);}
161534 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
161537 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
161540 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
161543 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
161546 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
161551 all.z = yymsp[-3].minor.yy0.z;
161552 all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
161553 sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy483, &all);
161558-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy60, yymsp[-4].minor.yy50.a, yymsp[-4].minor…
161559 …yymsp[-10].minor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); /*A-ove…
161563 { yymsp[0].minor.yy60 = yymsp[0].major; /*A-overwrites-X*/ }
161566 { yymsp[-1].minor.yy60 = TK_INSTEAD;}
161573 {yymsp[0].minor.yy50.a = yymsp[0].major; /*A-overwrites-X*/ yymsp[0].minor.yy50.b = 0;}
161576 {yymsp[-2].minor.yy50.a = TK_UPDATE; yymsp[-2].minor.yy50.b = yymsp[0].minor.yy288;}
161584 { yymsp[-1].minor.yy602 = yymsp[0].minor.yy602; }
161588 assert( yymsp[-2].minor.yy483!=0 );
161589 yymsp[-2].minor.yy483->pLast->pNext = yymsp[-1].minor.yy483;
161590 yymsp[-2].minor.yy483->pLast = yymsp[-1].minor.yy483;
161595 assert( yymsp[-1].minor.yy483!=0 );
161596 yymsp[-1].minor.yy483->pLast = yymsp[-1].minor.yy483;
161601 yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;
161622 …Step(pParse, &yymsp[-6].minor.yy0, yymsp[-2].minor.yy291, yymsp[-3].minor.yy338, yymsp[-1].minor.y…
161623 yymsp[-8].minor.yy483 = yylhsminor.yy483;
161627-4].minor.yy0,yymsp[-3].minor.yy288,yymsp[-2].minor.yy307,yymsp[-6].minor.yy60,yymsp[-1].minor.yy1…
161629 yymsp[-7].minor.yy483 = yylhsminor.yy483;
161632 …minor.yy483 = sqlite3TriggerDeleteStep(pParse, &yymsp[-3].minor.yy0, yymsp[-1].minor.yy602, yymsp[
161633 yymsp[-5].minor.yy483 = yylhsminor.yy483;
161636 …gerSelectStep(pParse->db, yymsp[-1].minor.yy307, yymsp[-2].minor.yy528, yymsp[0].minor.yy528); /*y…
161637 yymsp[-2].minor.yy483 = yylhsminor.yy483;
161641 yymsp[-3].minor.yy602 = sqlite3PExpr(pParse, TK_RAISE, 0, 0);
161642 if( yymsp[-3].minor.yy602 ){
161643 yymsp[-3].minor.yy602->affExpr = OE_Ignore;
161649 yymsp[-5].minor.yy602 = sqlite3ExprAlloc(pParse->db, TK_RAISE, &yymsp[-1].minor.yy0, 1);
161650 if( yymsp[-5].minor.yy602 ) {
161651 yymsp[-5].minor.yy602->affExpr = (char)yymsp[-3].minor.yy60;
161663 sqlite3DropTrigger(pParse,yymsp[0].minor.yy291,yymsp[-1].minor.yy60);
161668 sqlite3Attach(pParse, yymsp[-3].minor.yy602, yymsp[-1].minor.yy602, yymsp[0].minor.yy602);
161680 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
161686 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
161690 sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy291,&yymsp[0].minor.yy0);
161695 yymsp[-1].minor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-1].minor.yy0.z) + pParse->sLastToken.n;
161696 sqlite3AlterFinishAddColumn(pParse, &yymsp[-1].minor.yy0);
161701 sqlite3AlterDropColumn(pParse, yymsp[-3].minor.yy291, &yymsp[0].minor.yy0);
161712 …sqlite3AlterRenameColumn(pParse, yymsp[-5].minor.yy291, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0);
161723 …sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yym…
161742 {yymsp[-1].minor.yy570 = M10d_Yes;}
161745 {yymsp[-2].minor.yy570 = M10d_No;}
161749 …sp[-5].minor.yy607 = sqlite3CteNew(pParse, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy338, yymsp[-1].…
161754 yymsp[0].minor.yy195 = sqlite3WithAdd(pParse, 0, yymsp[0].minor.yy607); /*A-overwrites-X*/
161759 yymsp[-2].minor.yy195 = sqlite3WithAdd(pParse, yymsp[-2].minor.yy195, yymsp[0].minor.yy607);
161769 sqlite3WindowChain(pParse, yymsp[0].minor.yy19, yymsp[-2].minor.yy19);
161770 yymsp[0].minor.yy19->pNextWin = yymsp[-2].minor.yy19;
161773 yymsp[-2].minor.yy19 = yylhsminor.yy19;
161777 if( ALWAYS(yymsp[-1].minor.yy19) ){
161778 …yymsp[-1].minor.yy19->zName = sqlite3DbStrNDup(pParse->db, yymsp[-4].minor.yy0.z, yymsp[-4].minor.…
161780 yylhsminor.yy19 = yymsp[-1].minor.yy19;
161782 yymsp[-4].minor.yy19 = yylhsminor.yy19;
161786 …yymsp[-4].minor.yy19 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy19, yymsp[-2].minor.yy338, y…
161791 …e3WindowAssemble(pParse, yymsp[0].minor.yy19, yymsp[-2].minor.yy338, yymsp[-1].minor.yy338, &yymsp…
161793 yymsp[-5].minor.yy19 = yylhsminor.yy19;
161797 …yymsp[-3].minor.yy19 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy19, 0, yymsp[-1].minor.yy338…
161802 …9 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy19, 0, yymsp[-1].minor.yy338, &yymsp[-4].minor.…
161804 yymsp[-4].minor.yy19 = yylhsminor.yy19;
161815 yylhsminor.yy19 = sqlite3WindowAssemble(pParse, yymsp[0].minor.yy19, 0, 0, &yymsp[-1].minor.yy0);
161817 yymsp[-1].minor.yy19 = yylhsminor.yy19;
161826 …ylhsminor.yy19 = sqlite3WindowAlloc(pParse, yymsp[-2].minor.yy60, yymsp[-1].minor.yy113.eType, yym…
161828 yymsp[-2].minor.yy19 = yylhsminor.yy19;
161832 …wAlloc(pParse, yymsp[-5].minor.yy60, yymsp[-3].minor.yy113.eType, yymsp[-3].minor.yy113.pExpr, yym…
161834 yymsp[-5].minor.yy19 = yylhsminor.yy19;
161844 {yylhsminor.yy113.eType = yymsp[-1].major; yylhsminor.yy113.pExpr = 0;}
161845 yymsp[-1].minor.yy113 = yylhsminor.yy113;
161848 {yylhsminor.yy113.eType = yymsp[0].major; yylhsminor.yy113.pExpr = yymsp[-1].minor.yy602;}
161849 yymsp[-1].minor.yy113 = yylhsminor.yy113;
161855 {yymsp[-1].minor.yy570 = yymsp[0].minor.yy570;}
161859 {yymsp[-1].minor.yy570 = yymsp[-1].major; /*A-overwrites-X*/}
161862 {yymsp[0].minor.yy570 = yymsp[0].major; /*A-overwrites-X*/}
161865 { yymsp[-1].minor.yy19 = yymsp[0].minor.yy19; }
161869 yymsp[0].minor.yy19->pFilter = yymsp[-1].minor.yy602;
161872 yymsp[-1].minor.yy19 = yylhsminor.yy19;
161876 yylhsminor.yy19 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
161878 yylhsminor.yy19->eFrmType = TK_FILTER;
161879 yylhsminor.yy19->pFilter = yymsp[0].minor.yy602;
161881 sqlite3ExprDelete(pParse->db, yymsp[0].minor.yy602);
161888 yymsp[-3].minor.yy19 = yymsp[-1].minor.yy19;
161889 assert( yymsp[-3].minor.yy19!=0 );
161894 yymsp[-1].minor.yy19 = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
161895 if( yymsp[-1].minor.yy19 ){
161896 …yymsp[-1].minor.yy19->zName = sqlite3DbStrNDup(pParse->db, yymsp[0].minor.yy0.z, yymsp[0].minor.yy…
161901 { yymsp[-4].minor.yy602 = yymsp[-1].minor.yy602; }
161981 yypParser->yytos = yymsp;
161982 yymsp->stateno = (YYACTIONTYPE)yyact;
161983 yymsp->major = (YYCODETYPE)yygoto;
162002 while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
162050 yypParser->yyerrcnt = -1;
162052 assert( yypParser->yytos==yypParser->yystack );
162074 ** <li> An option argument of a grammar-specified type.
162098 assert( yypParser->yytos!=0 );
162103 yyact = yypParser->yytos->stateno;
162111 yyTracePrompt,yyTokenName[yymajor],yyact-YY_MIN_REDUCE);
162117 assert( yypParser->yytos>=yypParser->yystack );
162118 assert( yyact==yypParser->yytos->stateno );
162121 unsigned int yyruleno = yyact - YY_MIN_REDUCE; /* Reduce by this rule */
162131 yypParser->yytos[yysize].stateno);
162145 if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
162146 yypParser->yyhwm++;
162147 assert( yypParser->yyhwm ==
162148 (int)(yypParser->yytos - yypParser->yystack));
162152 if( yypParser->yytos>=yypParser->yystackEnd ){
162157 if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz-1] ){
162169 yypParser->yyerrcnt--;
162173 yypParser->yytos--;
162207 if( yypParser->yyerrcnt<0 ){
162210 yymx = yypParser->yytos->major;
162221 while( yypParser->yytos >= yypParser->yystack
162223 yypParser->yytos->stateno,
162228 if( yypParser->yytos < yypParser->yystack || yymajor==0 ){
162232 yypParser->yyerrcnt = -1;
162239 yypParser->yyerrcnt = 3;
162242 yyact = yypParser->yytos->stateno;
162257 ** * Report an error message, and throw away the input token.
162264 if( yypParser->yyerrcnt<=0 ){
162267 yypParser->yyerrcnt = 3;
162272 yypParser->yyerrcnt = -1;
162284 for(i=&yypParser->yystack[1]; i<=yypParser->yytos; i++){
162285 fprintf(yyTraceFILE,"%c%s", cDiv, yyTokenName[i->major]);
162324 ** individual tokens and sends those tokens one-by-one over to the
162349 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
162354 #define CC_SLASH 16 /* '/'. / or c-style comment */
162412 ** lower-case ASCII equivalent. On ASCII machines, this is just
162413 ** an upper-to-lower case map. On EBCDIC machines we also need
162466 ** might be implemented more directly using a hand-written hash table.
162524 /* aKWHash[i] is the hash value for the i-th keyword */
162538 ** then the i-th keyword has no more hash collisions. Otherwise,
162539 ** the next keyword with the same hash is aKWHash[i]-1. */
162554 /* aKWLen[i] is the length (in bytes) of the i-th keyword */
162570 ** the text for the i-th keyword. */
162585 /* aKWCode[i] is the parser symbol code for the i-th keyword */
162747 /* Check to see if z[0..n-1] is a keyword. If it is, write the
162754 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n*1) % 127;
162755 for(i=((int)aKWHash[i])-1; i>=0; i=((int)aKWNext[i])-1){
162949 ** For ASCII, any character with the high-order bit set is
162950 ** allowed in an identifier. For 7-bit characters,
162980 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
163022 ** impossible to call a window-function without a FILTER clause.
163069 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
163083 if( z[1]=='-' ){
163085 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
163118 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
163244 || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
163350 ** then an and attempt is made to write an error message into
163352 ** error message.
163356 void *pEngine; /* The LEMON-generated LALR(1) parser */
163359 int lastTokenParsed = -1; /* type of the previous token */
163360 sqlite3 *db = pParse->db; /* The database connection */
163363 yyParser sEngine; /* Space to hold the Lemon-generated Parser object */
163365 VVA_ONLY( u8 startedWithOom = db->mallocFailed );
163368 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
163369 if( db->nVdbeActive==0 ){
163370 AtomicStore(&db->u1.isInterrupted, 0);
163372 pParse->rc = SQLITE_OK;
163373 pParse->zTail = zSql;
163376 if( db->flags & SQLITE_ParserTrace ){
163393 assert( pParse->pNewTable==0 );
163394 assert( pParse->pNewTrigger==0 );
163395 assert( pParse->nVar==0 );
163396 assert( pParse->pVList==0 );
163397 pParse->pParentParse = db->pParse;
163398 db->pParse = pParse;
163401 mxSqlLen -= n;
163403 pParse->rc = SQLITE_TOOBIG;
163415 if( AtomicLoad(&db->u1.isInterrupted) ){
163416 pParse->rc = SQLITE_INTERRUPT;
163450 pParse->sLastToken.z = zSql;
163451 pParse->sLastToken.n = n;
163452 sqlite3Parser(pEngine, tokenType, pParse->sLastToken);
163455 assert( db->mallocFailed==0 || pParse->rc!=SQLITE_OK || startedWithOom );
163456 if( pParse->rc!=SQLITE_OK ) break;
163471 if( db->mallocFailed ){
163472 pParse->rc = SQLITE_NOMEM_BKPT;
163474 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
163475 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
163478 if( pParse->zErrMsg ){
163479 *pzErrMsg = pParse->zErrMsg;
163480 sqlite3_log(pParse->rc, "%s in \"%s\"",
163481 *pzErrMsg, pParse->zTail);
163482 pParse->zErrMsg = 0;
163485 pParse->zTail = zSql;
163486 if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
163487 sqlite3VdbeDelete(pParse->pVdbe);
163488 pParse->pVdbe = 0;
163491 if( pParse->nested==0 ){
163492 sqlite3DbFree(db, pParse->aTableLock);
163493 pParse->aTableLock = 0;
163494 pParse->nTableLock = 0;
163498 sqlite3_free(pParse->apVtabLock);
163502 /* If the pParse->declareVtab flag is set, do not delete any table
163503 ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
163506 sqlite3DeleteTable(db, pParse->pNewTable);
163509 sqlite3DeleteTrigger(db, pParse->pNewTrigger);
163511 sqlite3DbFree(db, pParse->pVList);
163512 db->pParse = pParse->pParentParse;
163513 pParse->pParentParse = 0;
163514 assert( nErr==0 || pParse->rc!=SQLITE_OK );
163525 if( pStr->nChar && sqlite3IsIdChar(pStr->zText[pStr->nChar-1]) ){
163531 ** Compute a normalization of the SQL given by zSql[0..nSql-1]. Return
163543 int prevType = 0; /* Previous non-whitespace token */
163551 tokenType = -1;
163555 for(i=0; zSql[i] && pStr->accError==0; i+=n){
163583 iStartIN = pStr->nChar;
163591 assert( pStr->nChar>=(u32)iStartIN );
163592 pStr->nChar = iStartIN+1;
163596 nParen--;
163602 j = pStr->nChar;
163626 while( j<pStr->nChar ){
163627 pStr->zText[j] = sqlite3Tolower(pStr->zText[j]);
163638 j = pStr->nChar;
163640 while( j<pStr->nChar ){
163641 pStr->zText[j] = sqlite3Toupper(pStr->zText[j]);
163685 #define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
163714 ** (0) INVALID We have not yet seen a non-whitespace character.
163812 case '/': { /* C-style comments */
163824 case '-': { /* SQL-style comments from "--" to end of line */
163825 if( zSql[1]!='-' ){
163834 case '[': { /* Microsoft-style identifiers in [...] */
163841 case '`': /* Grave-accent quoted symbols used by MySQL */
163842 case '"': /* single- and double-quoted strings */
163903 zSql += nId-1;
163920 ** above, except that the parameter is required to be UTF-16 encoded, not
163921 ** UTF-8.
163933 sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
164071 ** This is an extension initializer that is a no-op and always
164072 ** succeeds, except that it fails if the fault-simulation is set
164103 ** built-in extensions.
164143 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
164149 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
164154 /* IMPLEMENTATION-OF: R-25063-23286 The sqlite3_sourceid() function returns a
164162 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
164167 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
164169 ** the SQLITE_THREADSAFE compile-time option being set to 0.
164175 ** this variable being set to non-zero will cause OSTRACE macros to emit
164222 ** This routine is a no-op except on its very first call for the process,
164238 ** * Calls to this routine from Y must block until the outer-most
164259 ** combination, the work-around is to set the correct pointer
164260 ** size at compile-time using -DSQLITE_PTRSIZE=n compile-time option */
164264 ** to sqlite3_initialize() should be a no-op. But the initialization
164287 ** malloc subsystem - this implies that the allocation of a static
164324 ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
164371 sqlite3GlobalConfig.nRefInitMutex--;
164388 u64 x = (((u64)1)<<63)-1;
164399 ** compile-time option.
164417 ** when this routine is invoked, then this routine is a harmless no-op.
164466 ** the SQLite library at run-time.
164487 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-54466-46756 */
164489 /* EVIDENCE-OF: R-02748-19096 This option sets the threading mode to
164490 ** Single-thread. */
164496 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-20520-54086 */
164498 /* EVIDENCE-OF: R-14374-42468 This option sets the threading mode to
164499 ** Multi-thread. */
164505 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-59593-21810 */
164507 /* EVIDENCE-OF: R-41220-51800 This option sets the threading mode to
164514 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-63666-48755 */
164521 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 /* IMP: R-14450-37597 */
164530 /* EVIDENCE-OF: R-55594-21030 The SQLITE_CONFIG_MALLOC option takes a
164533 ** low-level memory allocation routines to be used in place of the memory
164539 /* EVIDENCE-OF: R-51213-46414 The SQLITE_CONFIG_GETMALLOC option takes a
164548 /* EVIDENCE-OF: R-61275-35157 The SQLITE_CONFIG_MEMSTATUS option takes
164559 /* EVIDENCE-OF: R-18761-36601 There are three arguments to
164560 ** SQLITE_CONFIG_PAGECACHE: A pointer to 8-byte aligned memory (pMem),
164569 /* EVIDENCE-OF: R-39100-27317 The SQLITE_CONFIG_PCACHE_HDRSZ option takes
164581 /* no-op */
164591 /* EVIDENCE-OF: R-63325-48378 The SQLITE_CONFIG_PCACHE2 option takes a
164599 /* EVIDENCE-OF: R-22035-46182 The SQLITE_CONFIG_GETPCACHE2 option takes a
164610 /* EVIDENCE-OF: R-06626-12911 The SQLITE_CONFIG_HEAP option is only
164615 /* EVIDENCE-OF: R-19854-42126 There are three arguments to
164616 ** SQLITE_CONFIG_HEAP: An 8-byte aligned pointer to the memory, the
164631 /* EVIDENCE-OF: R-49920-60189 If the first pointer (the memory pointer)
164641 /* EVIDENCE-OF: R-61006-08918 If the memory pointer is not NULL then the
164676 /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
164677 ** can be changed at start-time using the
164682 /* EVIDENCE-OF: R-25451-61125 The SQLITE_CONFIG_URI option takes a single
164683 ** argument of type int. If non-zero, then URI handling is globally
164691 /* EVIDENCE-OF: R-36592-02772 The SQLITE_CONFIG_COVERING_INDEX_SCAN
164709 /* EVIDENCE-OF: R-58063-38258 SQLITE_CONFIG_MMAP_SIZE takes two 64-bit
164715 /* EVIDENCE-OF: R-53367-43190 If either argument to this option is
164716 ** negative, then that argument is changed to its compile-time default.
164718 ** EVIDENCE-OF: R-34993-45031 The maximum allowed mmap size will be
164720 ** compile-time maximum mmap size set by the SQLITE_MAX_MMAP_SIZE
164721 ** compile-time option.
164733 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC) /* IMP: R-04780-55815 */
164735 /* EVIDENCE-OF: R-34926-03360 SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit
164795 int nBig; /* Number of full-size slots */
164796 int nSm; /* Number smaller LOOKASIDE_SMALL-byte slots */
164805 if( db->lookaside.bMalloced ){
164806 sqlite3_free(db->lookaside.pStart);
164811 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
164819 pStart = sqlite3Malloc( szAlloc ); /* IMP: R-61949-35727 */
164828 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
164831 nSm = (szAlloc - sz*nBig)/LOOKASIDE_SMALL;
164840 db->lookaside.pStart = pStart;
164841 db->lookaside.pInit = 0;
164842 db->lookaside.pFree = 0;
164843 db->lookaside.sz = (u16)sz;
164844 db->lookaside.szTrue = (u16)sz;
164851 p->pNext = db->lookaside.pInit;
164852 db->lookaside.pInit = p;
164856 db->lookaside.pSmallInit = 0;
164857 db->lookaside.pSmallFree = 0;
164858 db->lookaside.pMiddle = p;
164860 p->pNext = db->lookaside.pSmallInit;
164861 db->lookaside.pSmallInit = p;
164866 db->lookaside.pEnd = p;
164867 db->lookaside.bDisable = 0;
164868 db->lookaside.bMalloced = pBuf==0 ?1:0;
164869 db->lookaside.nSlot = nBig+nSm;
164871 db->lookaside.pStart = db;
164873 db->lookaside.pSmallInit = 0;
164874 db->lookaside.pSmallFree = 0;
164875 db->lookaside.pMiddle = db;
164877 db->lookaside.pEnd = db;
164878 db->lookaside.bDisable = 1;
164879 db->lookaside.sz = 0;
164880 db->lookaside.bMalloced = 0;
164881 db->lookaside.nSlot = 0;
164898 return db->mutex;
164911 sqlite3_mutex_enter(db->mutex);
164913 for(i=0; i<db->nDb; i++){
164914 Btree *pBt = db->aDb[i].pBt;
164921 sqlite3_mutex_leave(db->mutex);
164926 ** Flush any dirty pages in the pager-cache for any attached database
164937 sqlite3_mutex_enter(db->mutex);
164939 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
164940 Btree *pBt = db->aDb[i].pBt;
164951 sqlite3_mutex_leave(db->mutex);
164964 /* IMP: R-06824-28531 */
164965 /* IMP: R-36257-52125 */
164966 db->aDb[0].zDbSName = va_arg(ap,char*);
164971 void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
164972 int sz = va_arg(ap, int); /* IMP: R-47871-25994 */
164973 int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */
165001 rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
165006 u64 oldFlags = db->flags;
165008 db->flags |= aFlagOp[i].mask;
165010 db->flags &= ~(u64)aFlagOp[i].mask;
165012 if( oldFlags!=db->flags ){
165016 *pRes = (db->flags & aFlagOp[i].mask)!=0;
165041 /* EVIDENCE-OF: R-65033-28449 The built-in BINARY collation compares
165047 rc = nKey1 - nKey2;
165063 while( nKey1 && pK1[nKey1-1]==' ' ) nKey1--;
165064 while( nKey2 && pK2[nKey2-1]==' ' ) nKey2--;
165069 ** Return true if CollSeq is the default built-in BINARY.
165072 assert( p==0 || p->xCmp!=binCollFunc || strcmp(p->zName,"BINARY")==0 );
165073 return p==0 || p->xCmp==binCollFunc;
165077 ** Another built-in collating sequence: NOCASE.
165083 ** At the moment there is only a UTF-8 implementation.
165094 r = nKey1-nKey2;
165109 return db->lastRowid;
165122 sqlite3_mutex_enter(db->mutex);
165123 db->lastRowid = iRowid;
165124 sqlite3_mutex_leave(db->mutex);
165137 return db->nChange;
165150 return db->nTotalChange;
165156 ** at the b-tree/pager level.
165159 while( db->pSavepoint ){
165160 Savepoint *pTmp = db->pSavepoint;
165161 db->pSavepoint = pTmp->pNext;
165164 db->nSavepoint = 0;
165165 db->nStatement = 0;
165166 db->isTransactionSavepoint = 0;
165176 FuncDestructor *pDestructor = p->u.pDestructor;
165178 pDestructor->nRef--;
165179 if( pDestructor->nRef==0 ){
165180 pDestructor->xDestroy(pDestructor->pUserData);
165195 for(i=0; i<db->nDb; i++){
165196 Schema *pSchema = db->aDb[i].pSchema;
165198 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
165204 for(p=sqliteHashFirst(&db->aModule); p; p=sqliteHashNext(p)){
165206 if( pMod->pEpoTab ){
165207 sqlite3VtabDisconnect(db, pMod->pEpoTab);
165223 assert( sqlite3_mutex_held(db->mutex) );
165224 if( db->pVdbe ) return 1;
165225 for(j=0; j<db->nDb; j++){
165226 Btree *pBt = db->aDb[j].pBt;
165237 /* EVIDENCE-OF: R-63257-11740 Calling sqlite3_close() or
165238 ** sqlite3_close_v2() with a NULL pointer argument is a harmless no-op. */
165244 sqlite3_mutex_enter(db->mutex);
165245 if( db->mTrace & SQLITE_TRACE_CLOSE ){
165246 db->trace.xV2(SQLITE_TRACE_CLOSE, db->pTraceArg, db, 0);
165254 ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
165256 ** SQL statements below, as the v-table implementation may be storing
165267 sqlite3_mutex_leave(db->mutex);
165280 db->magic = SQLITE_MAGIC_ZOMBIE;
165291 int iTxn = -1;
165295 return -1;
165298 sqlite3_mutex_enter(db->mutex);
165301 if( iDb<0 ) nDb--;
165304 nDb = db->nDb-1;
165307 Btree *pBt = db->aDb[iDb].pBt;
165311 sqlite3_mutex_leave(db->mutex);
165344 if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
165345 sqlite3_mutex_leave(db->mutex);
165357 ** they are reset. And that the required b-tree mutex is held to make
165365 for(j=0; j<db->nDb; j++){
165366 struct Db *pDb = &db->aDb[j];
165367 if( pDb->pBt ){
165368 sqlite3BtreeClose(pDb->pBt);
165369 pDb->pBt = 0;
165371 pDb->pSchema = 0;
165376 if( db->aDb[1].pSchema ){
165377 sqlite3SchemaClear(db->aDb[1].pSchema);
165383 assert( db->nDb<=2 );
165384 assert( db->aDb==db->aDbStatic );
165387 ** locks and does not require any further unlock-notify callbacks.
165391 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
165396 pNext = p->pNext;
165401 sqlite3HashClear(&db->aFunc);
165402 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
165412 sqlite3HashClear(&db->aCollSeq);
165414 for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
165419 sqlite3HashClear(&db->aModule);
165423 sqlite3ValueFree(db->pErr);
165426 sqlite3_free(db->auth.zAuthUser);
165427 sqlite3_free(db->auth.zAuthPW);
165430 db->magic = SQLITE_MAGIC_ERROR;
165432 /* The temp-database schema is allocated differently from the other schema
165438 sqlite3DbFree(db, db->aDb[1].pSchema);
165439 sqlite3_mutex_leave(db->mutex);
165440 db->magic = SQLITE_MAGIC_CLOSED;
165441 sqlite3_mutex_free(db->mutex);
165443 if( db->lookaside.bMalloced ){
165444 sqlite3_free(db->lookaside.pStart);
165451 ** any write cursors are invalidated ("tripped" - as in "tripping a circuit
165460 assert( sqlite3_mutex_held(db->mutex) );
165463 /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
165465 ** modified the database schema. If the b-tree mutexes are not taken
165466 ** here, then another shared-cache connection might sneak in between
165470 schemaChange = (db->mDbFlags & DBFLAG_SchemaChange)!=0 && db->init.busy==0;
165472 for(i=0; i<db->nDb; i++){
165473 Btree *p = db->aDb[i].pBt;
165491 db->nDeferredCons = 0;
165492 db->nDeferredImmCons = 0;
165493 db->flags &= ~(u64)SQLITE_DeferFKs;
165495 /* If one has been configured, invoke the rollback-hook callback */
165496 if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
165497 db->xRollbackCallback(db->pRollbackArg);
165650 /* SQLITE_NOTICE */ "notification message",
165651 /* SQLITE_WARNING */ "warning message",
165684 ** Return non-zero to retry the lock. Return zero to stop trying
165700 int tmout = db->busyTimeout;
165708 delay = delays[NDELAY-1];
165709 prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
165712 delay = tmout - prior;
165715 sqlite3OsSleep(db->pVfs, delay*1000);
165721 int tmout = ((sqlite3 *)ptr)->busyTimeout;
165725 sqlite3OsSleep(db->pVfs, 1000000);
165736 ** If this routine returns non-zero, the lock is retried. If it
165741 if( p->xBusyHandler==0 || p->nBusy<0 ) return 0;
165742 rc = p->xBusyHandler(p->pBusyArg, p->nBusy);
165744 p->nBusy = -1;
165746 p->nBusy++;
165763 sqlite3_mutex_enter(db->mutex);
165764 db->busyHandler.xBusyHandler = xBusy;
165765 db->busyHandler.pBusyArg = pArg;
165766 db->busyHandler.nBusy = 0;
165767 db->busyTimeout = 0;
165768 sqlite3_mutex_leave(db->mutex);
165790 sqlite3_mutex_enter(db->mutex);
165792 db->xProgress = xProgress;
165793 db->nProgressOps = (unsigned)nOps;
165794 db->pProgressArg = pArg;
165796 db->xProgress = 0;
165797 db->nProgressOps = 0;
165798 db->pProgressArg = 0;
165800 sqlite3_mutex_leave(db->mutex);
165816 db->busyTimeout = ms;
165828 if( !sqlite3SafetyCheckOk(db) && (db==0 || db->magic!=SQLITE_MAGIC_ZOMBIE) ){
165833 AtomicStore(&db->u1.isInterrupted, 1);
165860 assert( sqlite3_mutex_held(db->mutex) );
165866 || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
165919 if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==(u32)enc && p->nArg==nArg ){
165920 if( db->nVdbeActive ){
165922 "unable to delete/modify user-function due to active statements");
165923 assert( !db->mallocFailed );
165931 assert(p || db->mallocFailed);
165941 pDestructor->nRef++;
165943 p->u.pDestructor = pDestructor;
165944 p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
165945 testcase( p->funcFlags & SQLITE_DETERMINISTIC );
165946 testcase( p->funcFlags & SQLITE_DIRECTONLY );
165947 p->xSFunc = xSFunc ? xSFunc : xStep;
165948 p->xFinalize = xFinal;
165949 p->xValue = xValue;
165950 p->xInverse = xInverse;
165951 p->pUserData = pUserData;
165952 p->nArg = (u16)nArg;
165957 ** Worker function used by utf-8 APIs that create new functions:
165984 sqlite3_mutex_enter(db->mutex);
165992 pArg->nRef = 0;
165993 pArg->xDestroy = xDestroy;
165994 pArg->pUserData = p;
165999 if( pArg && pArg->nRef==0 ){
166007 sqlite3_mutex_leave(db->mutex);
166074 sqlite3_mutex_enter(db->mutex);
166075 assert( !db->mallocFailed );
166076 zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
166080 sqlite3_mutex_leave(db->mutex);
166088 ** fails with an error message stating that the function is used in the
166104 sqlite3_result_error(context, zErr, -1);
166112 ** this routine is a no-op. If the function does not exist, then create
166113 ** a new one that always throws a run-time error.
166129 if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
166133 sqlite3_mutex_enter(db->mutex);
166135 sqlite3_mutex_leave(db->mutex);
166148 ** A NULL trace function means that no tracing is executes. A non-NULL
166162 sqlite3_mutex_enter(db->mutex);
166163 pOld = db->pTraceArg;
166164 db->mTrace = xTrace ? SQLITE_TRACE_LEGACY : 0;
166165 db->trace.xLegacy = xTrace;
166166 db->pTraceArg = pArg;
166167 sqlite3_mutex_leave(db->mutex);
166172 /* Register a trace callback using the version-2 interface.
166185 sqlite3_mutex_enter(db->mutex);
166188 db->mTrace = mTrace;
166189 db->trace.xV2 = xTrace;
166190 db->pTraceArg = pArg;
166191 sqlite3_mutex_leave(db->mutex);
166200 ** A NULL profile function means that no profiling is executes. A non-NULL
166217 sqlite3_mutex_enter(db->mutex);
166218 pOld = db->pProfileArg;
166219 db->xProfile = xProfile;
166220 db->pProfileArg = pArg;
166221 db->mTrace &= SQLITE_TRACE_NONLEGACY_MASK;
166222 if( db->xProfile ) db->mTrace |= SQLITE_TRACE_XPROFILE;
166223 sqlite3_mutex_leave(db->mutex);
166231 ** If the invoked function returns non-zero, then the commit becomes a
166247 sqlite3_mutex_enter(db->mutex);
166248 pOld = db->pCommitArg;
166249 db->xCommitCallback = xCallback;
166250 db->pCommitArg = pArg;
166251 sqlite3_mutex_leave(db->mutex);
166272 sqlite3_mutex_enter(db->mutex);
166273 pRet = db->pUpdateArg;
166274 db->xUpdateCallback = xCallback;
166275 db->pUpdateArg = pArg;
166276 sqlite3_mutex_leave(db->mutex);
166297 sqlite3_mutex_enter(db->mutex);
166298 pRet = db->pRollbackArg;
166299 db->xRollbackCallback = xCallback;
166300 db->pRollbackArg = pArg;
166301 sqlite3_mutex_leave(db->mutex);
166317 sqlite3_mutex_enter(db->mutex);
166318 pRet = db->pPreUpdateArg;
166319 db->xPreUpdateCallback = xCallback;
166320 db->pPreUpdateArg = pArg;
166321 sqlite3_mutex_leave(db->mutex);
166378 ** into the write-ahead-log by this database connection.
166393 sqlite3_mutex_enter(db->mutex);
166394 pRet = db->pWalArg;
166395 db->xWalCallback = xCallback;
166396 db->pWalArg = pArg;
166397 sqlite3_mutex_leave(db->mutex);
166424 /* Initialize the output variables to -1 in case an error occurs. */
166425 if( pnLog ) *pnLog = -1;
166426 if( pnCkpt ) *pnCkpt = -1;
166433 /* EVIDENCE-OF: R-03996-12088 The M parameter must be a valid checkpoint
166438 sqlite3_mutex_enter(db->mutex);
166448 db->busyHandler.nBusy = 0;
166456 if( db->nVdbeActive==0 ){
166457 AtomicStore(&db->u1.isInterrupted, 0);
166460 sqlite3_mutex_leave(db->mutex);
166468 ** to contains a zero-length string, all attached databases are
166472 /* EVIDENCE-OF: R-41613-20553 The sqlite3_wal_checkpoint(D,X) is equivalent to
166479 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
166488 ** associated with the specific b-tree being checkpointed is taken by
166492 ** checkpointed. If an error is encountered it is returned immediately -
166503 assert( sqlite3_mutex_held(db->mutex) );
166504 assert( !pnLog || *pnLog==-1 );
166505 assert( !pnCkpt || *pnCkpt==-1 );
166509 for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
166511 rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
166526 ** This function returns true if main-memory should be used instead of
166528 ** The value returned depends on the value of db->temp_store (runtime
166533 ** SQLITE_TEMP_STORE db->temp_store Location of temporary database
166534 ** ----------------- -------------- ------------------------------
166546 return ( db->temp_store==2 );
166549 return ( db->temp_store!=1 );
166562 ** Return UTF-8 encoded English language explanation of the most recent
166573 sqlite3_mutex_enter(db->mutex);
166574 if( db->mallocFailed ){
166577 testcase( db->pErr==0 );
166578 z = db->errCode ? (char*)sqlite3_value_text(db->pErr) : 0;
166579 assert( !db->mallocFailed );
166581 z = sqlite3ErrStr(db->errCode);
166584 sqlite3_mutex_leave(db->mutex);
166590 ** Return UTF-16 encoded English language explanation of the most recent
166610 sqlite3_mutex_enter(db->mutex);
166611 if( db->mallocFailed ){
166614 z = sqlite3_value_text16(db->pErr);
166616 sqlite3ErrorWithMsg(db, db->errCode, sqlite3ErrStr(db->errCode));
166617 z = sqlite3_value_text16(db->pErr);
166620 ** above. If this is the case, then the db->mallocFailed flag needs to
166622 ** sqlite3ApiExit(), to avoid setting the database handle error message.
166626 sqlite3_mutex_leave(db->mutex);
166639 if( !db || db->mallocFailed ){
166642 return db->errCode & db->errMask;
166648 if( !db || db->mallocFailed ){
166651 return db->errCode;
166654 return db ? db->iSysErrno : 0;
166681 assert( sqlite3_mutex_held(db->mutex) );
166699 ** are no active VMs, invalidate any pre-compiled statements.
166702 if( pColl && pColl->xCmp ){
166703 if( db->nVdbeActive ){
166713 ** Also, collation destructor - CollSeq.xDel() - function may need
166716 if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
166717 CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName);
166721 if( p->enc==pColl->enc ){
166722 if( p->xDel ){
166723 p->xDel(p->pUser);
166725 p->xCmp = 0;
166733 pColl->xCmp = xCompare;
166734 pColl->pUser = pCtx;
166735 pColl->xDel = xDel;
166736 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
166757 SQLITE_MAX_VARIABLE_NUMBER, /* IMP: R-38091-32352 */
166802 ** If an invalid limit index is supplied, report -1.
166816 return -1;
166820 /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
166821 ** there is a hard upper bound set at compile-time by a C preprocessor
166838 assert( SQLITE_LIMIT_WORKER_THREADS==(SQLITE_N_LIMIT-1) );
166842 return -1;
166844 oldLimit = db->aLimit[limitId];
166845 if( newLimit>=0 ){ /* IMP: R-52476-28732 */
166847 newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */
166849 db->aLimit[limitId] = newLimit;
166851 return oldLimit; /* IMP: R-53341-35419 */
166855 ** This function is used to parse both URIs and non-URI filenames passed by the
166861 ** query parameter. The second argument contains the URI (or non-URI filename)
166877 ** message. It is the responsibility of the caller to eventually release
166882 const char *zUri, /* Nul-terminated URI to parse */
166886 char **pzErrMsg /* OUT: Error message (if rc!=SQLITE_OK) */
166897 if( ((flags & SQLITE_OPEN_URI) /* IMP: R-48725-32206 */
166898 || sqlite3GlobalConfig.bOpenUri) /* IMP: R-51689-46548 */
166899 && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
166908 ** method that there may be extra parameters following the file-name. */
166915 memset(zFile, 0, 4); /* 4-byte of 0x00 is the start of DB name marker */
166938 iIn-7, &zUri[7]);
166951 ** 0: Parsing file-name.
166989 if( zFile[iOut-1]==0 ){
166991 while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
167007 memset(zFile+iOut, 0, 4); /* end-of-options + empty journal filenames */
167133 ** is UTF-8 encoded.
167136 const char *zFilename, /* Database filename UTF-8 encoded */
167145 char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
167204 db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
167205 if( db->mutex==0 ){
167211 sqlite3MutexWarnOnContention(db->mutex);
167214 sqlite3_mutex_enter(db->mutex);
167215 db->errMask = 0xff;
167216 db->nDb = 2;
167217 db->magic = SQLITE_MAGIC_BUSY;
167218 db->aDb = db->aDbStatic;
167219 db->lookaside.bDisable = 1;
167220 db->lookaside.sz = 0;
167222 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
167223 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
167224 db->aLimit[SQLITE_LIMIT_WORKER_THREADS] = SQLITE_DEFAULT_WORKER_THREADS;
167225 db->autoCommit = 1;
167226 db->nextAutovac = -1;
167227 db->szMmap = sqlite3GlobalConfig.szMmap;
167228 db->nextPagesize = 0;
167229 db->nMaxSorterMmap = 0x7FFFFFFF;
167230 db->flags |= SQLITE_ShortColNames
167237 /* The SQLITE_DQS compile-time option determines the default settings
167241 ** ---------- ----------------------- -----------------------
167248 ** Legacy behavior is 3 (double-quoted string literals are allowed anywhere)
167250 ** -DSQLITE_DQS=0 (best) or -DSQLITE_DQS=1 (second choice) if possible.
167299 sqlite3HashInit(&db->aCollSeq);
167301 sqlite3HashInit(&db->aModule);
167304 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
167305 ** and UTF-16, so add a version for each to avoid any unnecessary
167308 ** EVIDENCE-OF: R-52786-44878 SQLite defines three built-in collating
167316 if( db->mallocFailed ){
167323 ** Throw an error if any non-sense combination is used. If we
167332 db->openFlags = flags;
167340 rc = SQLITE_MISUSE_BKPT; /* IMP: R-18321-05872 */
167342 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
167352 rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
167361 sqlite3BtreeEnter(db->aDb[0].pBt);
167362 db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
167363 if( !db->mallocFailed ){
167366 sqlite3BtreeLeave(db->aDb[0].pBt);
167367 db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
167372 db->aDb[0].zDbSName = "main";
167373 db->aDb[0].safety_level = SQLITE_DEFAULT_SYNCHRONOUS+1;
167374 db->aDb[1].zDbSName = "temp";
167375 db->aDb[1].safety_level = PAGER_SYNCHRONOUS_OFF;
167377 db->magic = SQLITE_MAGIC_OPEN;
167378 if( db->mallocFailed ){
167382 /* Register all built-in functions, but do not attempt to read the
167391 /* Load compiled-in extensions */
167396 /* Load automatic extensions - extensions that have been registered
167408 /* Testing use only!!! The -DSQLITE_ENABLE_INTERNAL_FUNCTIONS=1 compile-time
167411 db->mDbFlags |= DBFLAG_InternalFunc;
167414 /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
167415 ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
167419 db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
167420 sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
167426 /* Enable the lookaside-malloc subsystem */
167434 assert( db->mutex!=0 || isThreadsafe==0
167436 sqlite3_mutex_leave(db->mutex);
167444 db->magic = SQLITE_MAGIC_SICK;
167470 const char *filename, /* Database filename (UTF-8) */
167486 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
167500 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
167547 sqlite3_mutex_enter(db->mutex);
167548 assert( !db->mallocFailed );
167551 sqlite3_mutex_leave(db->mutex);
167572 sqlite3_mutex_enter(db->mutex);
167573 assert( !db->mallocFailed );
167574 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
167580 sqlite3_mutex_leave(db->mutex);
167597 sqlite3_mutex_enter(db->mutex);
167598 db->xCollNeeded = xCollNeeded;
167599 db->xCollNeeded16 = 0;
167600 db->pCollNeededArg = pCollNeededArg;
167601 sqlite3_mutex_leave(db->mutex);
167618 sqlite3_mutex_enter(db->mutex);
167619 db->xCollNeeded = 0;
167620 db->xCollNeeded16 = xCollNeeded16;
167621 db->pCollNeededArg = pCollNeededArg;
167622 sqlite3_mutex_leave(db->mutex);
167650 return db->autoCommit;
167662 ** a low-level error is first detected.
167702 ** This is a convenience routine that makes sure that all thread-specific
167705 ** SQLite no longer uses thread-specific data so this routine is now a
167706 ** no-op. It is retained for historical compatibility.
167725 int *pAutoinc /* OUTPUT: True if column is auto-increment */
167746 sqlite3_mutex_enter(db->mutex);
167755 if( !pTab || pTab->pSelect ){
167764 for(iCol=0; iCol<pTab->nCol; iCol++){
167765 pCol = &pTab->aCol[iCol];
167766 if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
167770 if( iCol==pTab->nCol ){
167772 iCol = pTab->iPKey;
167773 pCol = iCol>=0 ? &pTab->aCol[iCol] : 0;
167793 zCollSeq = pCol->zColl;
167794 notnull = pCol->notNull!=0;
167795 primarykey = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
167796 autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
167827 sqlite3_mutex_leave(db->mutex);
167854 sqlite3_mutex_enter(db->mutex);
167855 db->errMask = onoff ? 0xffffffff : 0xff;
167856 sqlite3_mutex_leave(db->mutex);
167870 sqlite3_mutex_enter(db->mutex);
167900 int nSave = db->busyHandler.nBusy;
167902 db->busyHandler.nBusy = nSave;
167906 sqlite3_mutex_leave(db->mutex);
167942 ** Control the seed for the pseudo-random number generator (PRNG) that
167957 ** This test-control also resets the PRNG so that the new seed will
167965 assert( db==0 || db->aDb[0].pSchema!=0 );
167966 if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
167977 ** is an array of integers that defines the test. Return -1 on a
167978 ** memory allocation error, 0 on success, or non-zero for an error.
168052 ** This action provides a run-time test to see whether or not
168053 ** assert() was enabled at compile-time. If X is true and assert()
168062 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
168071 ** This action provides a run-time test to see how the ALWAYS and
168072 ** NEVER macros were defined at compile-time.
168077 ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
168079 ** hard-coded to true or else it asserts if its argument is false.
168080 ** The first behavior (hard-coded to true) is the case if
168085 ** The run-time test procedure might look something like this:
168088 ** // ALWAYS() and NEVER() are no-op pass-through macros
168104 ** The integer returned reveals the byte-order of the computer on which
168107 ** 1 big-endian, determined at run-time
168108 ** 10 little-endian, determined at run-time
168109 ** 432101 big-endian, determined at compile-time
168110 ** 123410 little-endian, determined at compile-time
168128 db->dbOptFlags = va_arg(ap, u32);
168134 ** If parameter onoff is non-zero, subsequent calls to localtime()
168149 db->mDbFlags ^= DBFLAG_InternalFunc;
168155 ** Set or clear a flag that indicates that the database file is always well-
168159 ** that demonstrat invariants on well-formed database files.
168172 ** 2020-07-22: Disabling EXTRA_SCHEMA_CHECKS also disables the
168211 db->nMaxSorterMmap = va_arg(ap, int);
168230 ** or off. "tnum" is the root page of the b-tree to which the imposter
168243 sqlite3_mutex_enter(db->mutex);
168244 db->init.iDb = sqlite3FindDbName(db, va_arg(ap,const char*));
168245 db->init.busy = db->init.imposterTable = va_arg(ap,int);
168246 db->init.newTnum = va_arg(ap,int);
168247 if( db->init.busy==0 && db->init.newTnum>0 ){
168250 sqlite3_mutex_leave(db->mutex);
168258 ** -DYYCOVERAGE) writes a report onto "out" that shows all
168272 ** This test-control causes the most recent sqlite3_result_int64() value
168276 ** test-control enables us to write an intreal() SQL function that can
168291 ** This test-control queries the seek-counter on the "main" database
168292 ** file. The seek-counter is written into *pnSeek and is then reset.
168293 ** The seek-count is only available if compiled with SQLITE_DEBUG.
168298 *pn = sqlite3BtreeSeekCount(db->aDb->pBt);
168338 while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
168339 zName--;
168390 assert( (sqlite3_int64)(p - pResult)==nByte );
168402 sqlite3_free(p - 4);
168430 while( zFilename[0] && (N--)>0 ){
168447 ** Return a 64-bit integer value for a query parameter.
168468 ** passing free() a pointer that was not obtained from malloc() - it is
168499 return iDb<0 ? 0 : db->aDb[iDb].pBt;
168519 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
168527 return -1;
168531 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
168552 sqlite3_mutex_enter(db->mutex);
168554 if( db->autoCommit==0 ){
168557 Btree *pBt = db->aDb[iDb].pBt;
168567 sqlite3_mutex_leave(db->mutex);
168573 ** Open a read-transaction on the snapshot idendified by pSnapshot.
168588 sqlite3_mutex_enter(db->mutex);
168589 if( db->autoCommit==0 ){
168593 Btree *pBt = db->aDb[iDb].pBt;
168598 if( db->nVdbeActive==0 ){
168622 sqlite3_mutex_leave(db->mutex);
168642 sqlite3_mutex_enter(db->mutex);
168645 Btree *pBt = db->aDb[iDb].pBt;
168654 sqlite3_mutex_leave(db->mutex);
168669 ** Given the name of a compile-time option, return true if that option
168705 ** Return the N-th compile-time option string. If N is out of range,
168767 ** 1) Each entry in the list has a non-NULL value for either
168779 for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
168784 assert( p->pUnlockConnection || p->pBlockingConnection );
168787 for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
168788 if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
168789 assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
168790 assert( db==0 || p->pUnlockConnection!=db );
168791 assert( db==0 || p->pBlockingConnection!=db );
168801 ** db is not currently a part of the list, this function is a no-op.
168806 for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
168808 *pp = (*pp)->pNextBlocked;
168823 *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
168824 pp=&(*pp)->pNextBlocked
168826 db->pNextBlocked = *pp;
168848 ** Register an unlock-notify callback.
168853 ** cache. pOther is found by looking at db->pBlockingConnection.
168875 sqlite3_mutex_enter(db->mutex);
168880 db->pBlockingConnection = 0;
168881 db->pUnlockConnection = 0;
168882 db->xUnlockNotify = 0;
168883 db->pUnlockArg = 0;
168884 }else if( 0==db->pBlockingConnection ){
168893 for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
168897 db->pUnlockConnection = db->pBlockingConnection;
168898 db->xUnlockNotify = xNotify;
168899 db->pUnlockArg = pArg;
168906 assert( !db->mallocFailed );
168908 sqlite3_mutex_leave(db->mutex);
168920 if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
168923 db->pBlockingConnection = pBlocker;
168939 ** set to db, then invoke the configured unlock-notify callback and
168947 void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
168957 /* This loop runs once for each entry in the blocked-connections list. */
168958 for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
168962 if( p->pBlockingConnection==db ){
168963 p->pBlockingConnection = 0;
168967 if( p->pUnlockConnection==db ){
168968 assert( p->xUnlockNotify );
168969 if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
168988 ** be passed to the unlock-notify callback is larger than the
168995 ** closed and the unlock-notify callbacks on blocked connections
168997 ** indefinitely for an unlock-notify callback that will never
169000 ** Instead, invoke the unlock-notify callback with the context
169003 ** requiring any dynamic allocation. This is sub-optimal because
169017 aArg[nArg++] = p->pUnlockArg;
169018 xUnlockNotify = p->xUnlockNotify;
169019 p->pUnlockConnection = 0;
169020 p->xUnlockNotify = 0;
169021 p->pUnlockArg = 0;
169025 if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
169027 *pp = p->pNextBlocked;
169028 p->pNextBlocked = 0;
169030 pp = &p->pNextBlocked;
169068 ** This is an SQLite module implementing full-text search.
169081 /* The full-text index is stored in a series of b+tree (-like)
169090 ** The basic unit of encoding is a variable-length integer called a
169091 ** varint. We encode variable-length integers in little-endian order
169098 ** 7 bits - A
169099 ** 14 bits - BA
169100 ** 21 bits - BBA
169104 ** the encoding is not the same. SQLite varints are big-endian
169106 ** little-endian and can be up to 10 bytes in length (in theory).
169116 ** A doclist (document list) holds a docid-sorted list of hits for a
169122 ** FTS3 used to optionally store character offsets using a compile-time
169155 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
169158 ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
169160 ** (72-2) and then terminates with the 0 at K.
169162 ** A "position-list" is the list of positions for multiple columns for
169163 ** a single docid. A "column-list" is the set of positions for a single
169164 ** column. Hence, a position-list consists of one or more column-lists,
169165 ** a document record consists of a docid followed by a position-list and
169169 ** array of varint-encoded docids.
169184 ** (further terms are delta-encoded)
169230 ** (further terms are delta-encoded)
169260 ** The segment directory in table %_segdir stores meta-information for
169272 ** The meta-information in the segment directory is:
169273 ** level - segment level (see below)
169274 ** idx - index within level
169275 ** - (level,idx uniquely identify a segment)
169276 ** start_block - first leaf node
169277 ** leaves_end_block - last leaf node
169278 ** end_block - last block (including interior nodes)
169279 ** root - contents of root node
169308 ** a tiny bit slower (perhaps due to more overhead in merge-time
169324 ** costs), and infrequent and non-existent terms still seem to be fast
169327 ** TODO(shess) That said, it would be nice to have a better query-side
169335 ** Since we're using a segmented structure, with no docid-oriented
169400 ** Defines the interface to tokenizers used by fulltext-search. There
169458 ** CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
169505 ** normalized version of the token (i.e. after any case-folding and/or
169519 ** nul-terminated. This should either be fixed, or pInput/nBytes
169571 ** This is the header file for the generic hash-table implementation
169573 ** hash table implementation for the full-text indexing module.
169584 ** The internals of this structure are intended to be opaque -- client
169604 ** structure. All elements are stored on a single doubly-linked list.
169619 ** (including the null-terminator, if any). Case
169660 #define fts3HashFirst(H) ((H)->first)
169661 #define fts3HashNext(E) ((E)->next)
169662 #define fts3HashData(E) ((E)->data)
169663 #define fts3HashKey(E) ((E)->pKey)
169664 #define fts3HashKeysize(E) ((E)->nKey)
169669 #define fts3HashCount(H) ((H)->count)
169708 ** a collision when building an amalgamation with built-in FTS3.
169729 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
169731 ** as one or more b+-trees in the %_segments and %_segdir tables.
169733 ** It is possible to determine which index a b+-tree belongs to based on the
169735 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
169741 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
169748 ** make it a no-op.
169755 ** Terminator values for position-lists and column-lists.
169757 #define POS_COLUMN (1) /* Column-list terminator */
169758 #define POS_END (0) /* Position-list terminator */
169798 typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */
169799 typedef short int i16; /* 2-byte (or larger) signed integer */
169800 typedef unsigned int u32; /* 4-byte unsigned integer */
169801 typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */
169802 typedef sqlite3_int64 i64; /* 8-byte signed integer */
169828 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
169899 ** time must share a common language-id (see the FTS4 langid= feature).
169902 ** A single FTS4 table may have multiple full-text indexes. For each index
169936 ** by special insert command 'test-no-incr-doclist'. */
169946 # define MergeCount(P) ((P)->nMergeCount)
169969 char *aDoclist; /* List of docids for full-text queries */
169988 ** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index
170002 #define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */
170005 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
170007 ** above. The upper 16-bits contain a combination of the following
170008 ** bits, used to describe extra constraints on full-text searches.
170028 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
170041 Fts3MultiSegReader *pSegcsr; /* Segment-reader for this token */
170087 Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */
170155 #define FTS3_SEGCURSOR_PENDING -1
170156 #define FTS3_SEGCURSOR_ALL -2
170185 int nAdvance; /* How many seg-readers to advance */
170318 ** assert() conditions in the fts3 code are activated - conditions that are
170324 ** Write a 64-bit variable-length integer to memory starting at p[0].
170335 q[-1] &= 0x7f; /* turn off high bit in final byte */
170336 assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
170337 return (int) (q - (unsigned char *)p);
170366 return (int)(p - pStart);
170370 ** Read a 64-bit variable-length integer from memory starting at p[0].
170379 ** Read a 64-bit variable-length integer from memory starting at p[0] and
170380 ** not extending past pEnd[-1].
170401 return (int)(p - pStart);
170406 ** a non-negative 32-bit integer before it is returned.
170442 ** Convert an SQL-style quoted string into a normal string by removing
170443 ** the quote characters. The conversion is done in-place. If the
170445 ** is a no-op.
170463 /* If the first byte was a '[', then the close-quote character is a ']' */
170492 ** varint that is part of a doclist (or position-list, or any other list
170509 ** clear on character p[-1]. */
170510 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
170525 assert( p->nPendingData==0 );
170526 assert( p->pSegments==0 );
170529 sqlite3_finalize(p->pSeekStmt);
170530 for(i=0; i<SizeofArray(p->aStmt); i++){
170531 sqlite3_finalize(p->aStmt[i]);
170533 sqlite3_free(p->zSegmentsTbl);
170534 sqlite3_free(p->zReadExprlist);
170535 sqlite3_free(p->zWriteExprlist);
170536 sqlite3_free(p->zContentTbl);
170537 sqlite3_free(p->zLanguageid);
170540 p->pTokenizer->pModule->xDestroy(p->pTokenizer);
170547 ** Write an error message into *pzErr
170562 ** If *pRc is initially non-zero then this routine is a no-op.
170590 const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
170591 sqlite3 *db = p->db; /* Database handle */
170600 zDb, p->zName,
170601 zDb, p->zName,
170602 zDb, p->zName,
170603 zDb, p->zName,
170604 (p->zContentTbl ? "--" : ""), zDb,p->zName
170620 ** If *pRc is non-zero when this function is called, it is a no-op.
170632 zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
170633 sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
170636 zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
170637 for(i=1; zCols && i<p->nColumn; i++){
170638 zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
170644 zCols, p->zName, zLanguageid
170649 rc = sqlite3_declare_vtab(p->db, zSql);
170662 fts3DbExec(pRc, p->db,
170665 p->zDb, p->zName
170667 if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
170675 ** If the p->bHasDocsize boolean is true (indicating that this is an
170682 sqlite3 *db = p->db; /* The database connection */
170684 if( p->zContentTbl==0 ){
170685 const char *zLanguageid = p->zLanguageid;
170690 for(i=0; zContentCols && i<p->nColumn; i++){
170691 char *z = p->azColumn[i];
170702 p->zDb, p->zName, zContentCols
170710 p->zDb, p->zName
170722 p->zDb, p->zName
170724 if( p->bHasDocsize ){
170727 p->zDb, p->zName
170730 assert( p->bHasStat==p->bFts4 );
170731 if( p->bHasStat ){
170738 ** Store the current database page-size in bytes in p->nPgsz.
170740 ** If *pRc is non-zero when this function is called, it is a no-op.
170750 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
170754 rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
170757 p->nPgsz = sqlite3_column_int(pStmt, 0);
170760 p->nPgsz = 1024;
170764 assert( p->nPgsz>0 || rc!=SQLITE_OK );
170791 *pnKey = (int)(zCsr-z);
170827 ** Return a copy of input string zInput enclosed in double-quotes (") and
170830 ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
170865 ** table has the three user-defined columns "a", "b", and "c", the following
170873 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
170884 if( p->zContentTbl==0 ){
170891 for(i=0; i<p->nColumn; i++){
170892 fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
170894 if( p->zLanguageid ){
170900 for(i=0; i<p->nColumn; i++){
170901 fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
170903 if( p->zLanguageid ){
170904 fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
170908 p->zDb,
170909 (p->zContentTbl ? p->zContentTbl : p->zName),
170910 (p->zContentTbl ? "" : "_content")
170918 ** user-defined text column).
170923 ** user-defined text columns, the following string is returned:
170930 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
170947 for(i=0; i<p->nColumn; i++){
170950 if( p->zLanguageid ){
170958 ** Buffer z contains a positive integer value encoded as utf-8 text.
170966 iVal = iVal*10 + (z[i] - '0');
170967 if( iVal>0x7FFFFFFF ) return -1;
170974 ** This function interprets the string at (*pp) as a non-negative integer
171050 nIndex--;
171051 i--;
171076 ** of each columns name, including the nul-terminator.
171095 char **pzErr /* OUT: error message */
171105 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
171120 ** nul-terminator byte. */
171158 ** argv[0] -> module name ("fts3" or "fts4")
171159 ** argv[1] -> database name
171160 ** argv[2] -> table name
171161 ** argv[...] -> "column name" and other module argument fields.
171170 char **pzErr /* Write any error message here */
171209 nByte = sizeof(const char *) * (argc-2);
171254 { "matchinfo", 9 }, /* 0 -> MATCHINFO */
171255 { "prefix", 6 }, /* 1 -> PREFIX */
171256 { "compress", 8 }, /* 2 -> COMPRESS */
171257 { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
171258 { "order", 5 }, /* 4 -> ORDER */
171259 { "content", 7 }, /* 5 -> CONTENT */
171260 { "languageid", 10 }, /* 6 -> LANGUAGEID */
171261 { "notindexed", 10 } /* 7 -> NOTINDEXED */
171270 if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
171371 nCol--;
171414 p->db = db;
171415 p->nColumn = nCol;
171416 p->nPendingData = 0;
171417 p->azColumn = (char **)&p[1];
171418 p->pTokenizer = pTokenizer;
171419 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
171420 p->bHasDocsize = (isFts4 && bNoDocsize==0);
171421 p->bHasStat = (u8)isFts4;
171422 p->bFts4 = (u8)isFts4;
171423 p->bDescIdx = (u8)bDescIdx;
171424 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
171425 p->zContentTbl = zContent;
171426 p->zLanguageid = zLanguageid;
171429 TESTONLY( p->inTransaction = -1 );
171430 TESTONLY( p->mxSavepoint = -1 );
171432 p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
171433 memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
171434 p->nIndex = nIndex;
171436 fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
171438 p->abNotindexed = (u8 *)&p->aIndex[nIndex];
171441 zCsr = (char *)&p->abNotindexed[nCol];
171442 p->zName = zCsr;
171445 p->zDb = zCsr;
171459 p->azColumn[iCol] = zCsr;
171466 int n = (int)strlen(p->azColumn[iCol]);
171470 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
171472 p->abNotindexed[iCol] = 1;
171490 p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
171491 p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
171505 p->bHasStat = 2;
171508 /* Figure out the page-size for the database. This is required in order to
171511 p->nNodeSize = p->nPgsz-35;
171514 p->nMergeCount = FTS3_MERGE_COUNT;
171534 pTokenizer->pModule->xDestroy(pTokenizer);
171537 assert( p->pSegments==0 );
171538 *ppVTab = &p->base;
171553 char **pzErr /* OUT: sqlite3_malloc'd error message */
171563 char **pzErr /* OUT: sqlite3_malloc'd error message */
171569 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
171571 ** support estimatedRows. In that case this function is a no-op.
171576 pIdxInfo->estimatedRows = nRow;
171582 ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
171584 ** support index-info flags. In that case this function is a no-op.
171589 pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
171599 ** 2. Full-text search using a MATCH operator on a non-docid column.
171605 int iCons = -1; /* Index of constraint to use */
171607 int iLangidCons = -1; /* Index of langid=x constraint, if present */
171608 int iDocidGe = -1; /* Index of docid>=x constraint, if present */
171609 int iDocidLe = -1; /* Index of docid<=x constraint, if present */
171612 if( p->bLock ){
171620 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
171621 pInfo->estimatedCost = 5000000;
171622 for(i=0; i<pInfo->nConstraint; i++){
171624 struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
171625 if( pCons->usable==0 ){
171626 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
171632 pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
171633 pInfo->estimatedCost = 1e50;
171640 bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
171643 if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
171644 pInfo->idxNum = FTS3_DOCID_SEARCH;
171645 pInfo->estimatedCost = 1.0;
171649 /* A MATCH constraint. Use a full-text search.
171658 if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
171659 && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
171661 pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
171662 pInfo->estimatedCost = 2.0;
171667 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
171668 && pCons->iColumn==p->nColumn + 2
171674 switch( pCons->op ){
171689 if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
171693 pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
171694 pInfo->aConstraintUsage[iCons].omit = 1;
171697 pInfo->idxNum |= FTS3_HAVE_LANGID;
171698 pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
171701 pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
171702 pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
171705 pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
171706 pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
171712 if( pInfo->nOrderBy==1 ){
171713 struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
171714 if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
171715 if( pOrder->desc ){
171716 pInfo->idxStr = "DESC";
171718 pInfo->idxStr = "ASC";
171720 pInfo->orderByConsumed = 1;
171724 assert( p->pSegments==0 );
171749 ** Finalize the statement handle at pCsr->pStmt.
171756 if( pCsr->bSeekStmt ){
171757 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
171758 if( p->pSeekStmt==0 ){
171759 p->pSeekStmt = pCsr->pStmt;
171760 sqlite3_reset(pCsr->pStmt);
171761 pCsr->pStmt = 0;
171763 pCsr->bSeekStmt = 0;
171765 sqlite3_finalize(pCsr->pStmt);
171775 sqlite3_free(pCsr->aDoclist);
171776 sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
171777 sqlite3Fts3ExprFree(pCsr->pExpr);
171778 memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
171787 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
171789 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
171795 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
171800 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
171805 if( pCsr->pStmt==0 ){
171806 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
171808 if( p->pSeekStmt ){
171809 pCsr->pStmt = p->pSeekStmt;
171810 p->pSeekStmt = 0;
171812 zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
171814 p->bLock++;
171816 p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
171818 p->bLock--;
171821 if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
171827 ** Position the pCsr->pStmt statement so that it is on the row
171833 if( pCsr->isRequireSeek ){
171836 Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
171837 pTab->bLock++;
171838 sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
171839 pCsr->isRequireSeek = 0;
171840 if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
171841 pTab->bLock--;
171844 pTab->bLock--;
171845 rc = sqlite3_reset(pCsr->pStmt);
171846 if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
171848 ** table is missing a row that is present in the full-text index.
171851 pCsr->isEof = 1;
171865 ** a b-tree for a term or term prefix. The node data is passed to this
171870 ** of the child node that heads the sub-tree that may contain the term.
171872 ** If piLast is not NULL, then *piLast is set to the right-most child node
171873 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
171896 ** interior node. Then load the blockid of the left-child of the b-tree
171932 if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
171953 ** to the term from the interior node, then all terms on the sub-tree
171985 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
171986 ** contains a term. This function searches the sub-tree headed by the zNode
171991 ** left-most leaf node in the tree that may contain the specified term.
171993 ** right-most leaf node that may contain a term for which the specified
172055 ** This function is used to create delta-encoded serialized lists of FTS3
172063 assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
172064 *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
172070 ** start of a position-list. After it returns, *ppPoslist points to the
172071 ** first byte after the position-list.
172089 ** of some other, multi-byte, value.
172091 ** The following while-loop moves pEnd to point to the first byte that is not
172094 ** last byte in the position-list.
172103 int n = (int)(pEnd - *ppPoslist);
172114 ** start of a column-list. After it returns, *ppPoslist points to the
172115 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
172117 ** A column-list is list of delta-encoded positions for a single column
172120 ** The column-list is terminated either by a POS_COLUMN varint (1) or
172122 ** the POS_COLUMN or POS_END that terminates the column-list.
172124 ** If pp is not NULL, then the contents of the column-list are copied
172133 /* A column-list is terminated by either a 0x01 or 0x00 byte that is
172134 ** not part of a multi-byte varint.
172141 int n = (int)(pEnd - *ppPoslist);
172151 ** Value used to signify the end of an position-list. This must be
172153 ** position-list, even a position list that has been corrupted.
172158 ** This function is used to help parse position-lists. When this function is
172159 ** called, *pp may point to the start of the next varint in the position-list
172160 ** being parsed, or it may point to 1 byte past the end of the position-list
172164 ** If *pp points past the end of the current position-list, set *pi to
172171 ** in the position-list. Because positions are delta-encoded, the value
172176 char **pp, /* IN/OUT: Pointer into position-list buffer */
172177 sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
172183 *pi -= 2;
172251 /* At this point, both p1 and p2 point to the start of column-lists
172253 ** A column-list is a list of non-negative delta-encoded varints, each
172267 iPrev -= 2;
172295 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
172310 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
172355 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
172356 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
172365 fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
172371 fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
172374 fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
172397 ** column-number in the position list.
172425 ** Merge two position-lists as required by the NEAR operator. The argument
172431 ** Position list *pp1 corresponds to the left-hand side of the NEAR
172436 ** The output position list - written to *pp - is a copy of *pp2 with those
172509 *pVal = (i64)((u64)*pVal - iVal);
172524 ** If *pbFirst is non-zero when this function is called, then the value
172525 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
172526 ** (if bDescIdx is non-zero).
172541 iWrite = (u64)iVal - (u64)*piPrev;
172544 iWrite = (u64)*piPrev - (u64)iVal;
172556 ** arguments are 64-bit docid values. If the value of the stack variable
172557 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
172558 ** Otherwise, (i2-i1).
172563 /* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
172564 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
172571 ** passed a non-zero value.
172605 ** current and previous docid (a positive number - since the list is in
172630 aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
172655 assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
172662 assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
172663 memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
172666 *pnOut = (int)(p-aOut);
172672 ** the output contains a copy of each position from the right-hand input
172673 ** doclist for which there is a position in the left-hand input doclist
172678 ** order, it should be passed a non-zero value.
172680 ** The right-hand input doclist is overwritten by this function.
172736 *pnRight = (int)(p - aOut);
172813 for(i=0; i<SizeofArray(pTS->aaOutput); i++){
172814 if( pTS->aaOutput[i] ){
172816 aOut = pTS->aaOutput[i];
172817 nOut = pTS->anOutput[i];
172818 pTS->aaOutput[i] = 0;
172823 int rc = fts3DoclistOrMerge(p->bDescIdx,
172824 pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
172831 sqlite3_free(pTS->aaOutput[i]);
172833 pTS->aaOutput[i] = 0;
172840 pTS->aaOutput[0] = aOut;
172841 pTS->anOutput[0] = nOut;
172853 ** doclists to merge, the merging is done pair-wise using the TermSelect
172865 if( pTS->aaOutput[0]==0 ){
172875 ** not true for order=DESC. For example, a doclist containing (1, -1)
172876 ** may be smaller than (-1), as in the first example the -1 may be stored
172877 ** as a single-byte delta, whereas in the second it must be stored as a
172882 pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1);
172883 pTS->anOutput[0] = nDoclist;
172884 if( pTS->aaOutput[0] ){
172885 memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
172886 memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
172895 for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
172896 if( pTS->aaOutput[iOut]==0 ){
172898 pTS->aaOutput[iOut] = aMerge;
172899 pTS->anOutput[iOut] = nMerge;
172905 int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
172906 pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
172914 sqlite3_free(pTS->aaOutput[iOut]);
172915 pTS->aaOutput[iOut] = 0;
172919 if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
172920 pTS->aaOutput[iOut] = aMerge;
172921 pTS->anOutput[iOut] = nMerge;
172930 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
172936 if( (pCsr->nSegment%16)==0 ){
172938 sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
172939 apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
172944 pCsr->apSegment = apNew;
172946 pCsr->apSegment[pCsr->nSegment++] = pNew;
172951 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
172960 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
172972 /* If iLevel is less than 0 and this is not a scan, include a seg-reader
172973 ** for the pending-terms. If this is a scan, then this call must be being
172978 if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
173010 rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
173028 ** Set up a cursor object for iterating through a full-text index or a
173033 int iLangid, /* Language-id to search */
173034 int iIndex, /* Index to search (from 0 to p->nIndex-1) */
173042 assert( iIndex>=0 && iIndex<p->nIndex );
173093 Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
173102 Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
173105 for(i=1; bFound==0 && i<p->nIndex; i++){
173106 if( p->aIndex[i].nPrefix==nTerm ){
173108 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
173111 pSegcsr->bLookup = 1;
173115 for(i=1; bFound==0 && i<p->nIndex; i++){
173116 if( p->aIndex[i].nPrefix==nTerm+1 ){
173118 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
173123 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
173131 rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
173134 pSegcsr->bLookup = !isPrefix;
173157 int iColumn, /* Column to query (or -ve for all columns) */
173162 Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
173163 TermSelect tsc; /* Object for pair-wise doclist merging */
173166 pSegcsr = pTok->pSegcsr;
173170 | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
173171 | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
173172 | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
173174 filter.zTerm = pTok->z;
173175 filter.nTerm = pTok->n;
173181 rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
173198 pTok->pSegcsr = 0;
173207 ** contains a position-list following each docid. Otherwise, it is assumed
173229 ** the next row that matches. For a full-table scan, this will be
173234 ** even if we reach end-of-file. The fts3EofMethod() will be called
173240 if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
173241 Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
173242 pTab->bLock++;
173243 if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
173244 pCsr->isEof = 1;
173245 rc = sqlite3_reset(pCsr->pStmt);
173247 pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
173250 pTab->bLock--;
173254 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
173260 ** converted to a 64-bit signed integer. Otherwise, return a copy of
173285 ** column on the left-hand side of the MATCH operator is column
173286 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
173299 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
173311 if( p->bLock ){
173316 assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
173317 assert( p->pSegments==0 );
173331 pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
173332 pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
173335 pCsr->bDesc = (idxStr[0]=='D');
173337 pCsr->bDesc = p->bDescIdx;
173339 pCsr->eSearch = (i16)eSearch;
173342 int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
173349 pCsr->iLangid = 0;
173350 if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
173352 assert( p->base.zErrMsg==0 );
173353 rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
173354 p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
173355 &p->base.zErrMsg
173364 pCsr->pNextId = pCsr->aDoclist;
173365 pCsr->iPrevId = 0;
173368 /* Compile a SELECT statement for this cursor. For a full-table-scan, the
173370 ** full-text query or docid lookup, the statement retrieves a single
173377 p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
173378 (pCsr->bDesc ? "DESC" : "ASC")
173382 p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
173386 p->bLock++;
173388 p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
173390 p->bLock--;
173398 rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
173412 if( pCsr->isEof ){
173414 pCsr->isEof = 1;
173416 return pCsr->isEof;
173427 *pRowid = pCsr->iPrevId;
173437 ** (iCol < p->nColumn) -> The value of the iCol'th user column.
173438 ** (iCol == p->nColumn) -> Magic column with the same name as the table.
173439 ** (iCol == p->nColumn+1) -> Docid column
173440 ** (iCol == p->nColumn+2) -> Langid column
173449 Fts3Table *p = (Fts3Table *)pCursor->pVtab;
173452 assert( iCol>=0 && iCol<=p->nColumn+2 );
173454 switch( iCol-p->nColumn ){
173456 /* The special 'table-name' column */
173462 sqlite3_result_int64(pCtx, pCsr->iPrevId);
173466 if( pCsr->pExpr ){
173467 sqlite3_result_int64(pCtx, pCsr->iLangid);
173469 }else if( p->zLanguageid==0 ){
173473 iCol = p->nColumn;
173478 /* A user column. Or, if this is a full-table scan, possibly the
173479 ** language-id column. Seek the cursor. */
173481 if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
173482 sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
173487 assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
173506 ** Implementation of xSync() method. Flush the contents of the pending-terms
173507 ** hash-table to the database.
173511 /* Following an incremental-merge operation, assuming that the input
173516 ** if the height of the input segment b-trees is N, and input segments
173518 ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
173519 ** small - often between 0 and 2. So the overhead of the incremental
173527 ** as it would also be required by a crisis-merge that used the same input
173530 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
173534 i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
173538 && p->nLeafAdd>(nMinMerge/16)
173539 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
173542 int A; /* Incr-merge parameter A */
173546 A = p->nLeafAdd * mxLevel;
173548 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
173551 sqlite3_set_last_insert_rowid(p->db, iLastRowid);
173557 ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
173563 if( p->bHasStat==2 ){
173564 char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
173566 int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
173568 p->bHasStat = (res==SQLITE_OK);
173582 assert( p->pSegments==0 );
173583 assert( p->nPendingData==0 );
173584 assert( p->inTransaction!=1 );
173585 TESTONLY( p->inTransaction = 1 );
173586 TESTONLY( p->mxSavepoint = -1; );
173587 p->nLeafAdd = 0;
173592 ** Implementation of xCommit() method. This is a no-op. The contents of
173593 ** the pending-terms hash-table have already been flushed into the database
173599 assert( p->nPendingData==0 );
173600 assert( p->inTransaction!=0 );
173601 assert( p->pSegments==0 );
173602 TESTONLY( p->inTransaction = 0 );
173603 TESTONLY( p->mxSavepoint = -1; );
173608 ** Implementation of xRollback(). Discard the contents of the pending-terms
173609 ** hash-table. Any changes made to the database are reverted by SQLite.
173614 assert( p->inTransaction!=0 );
173615 TESTONLY( p->inTransaction = 0 );
173616 TESTONLY( p->mxSavepoint = -1; );
173622 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
173627 char *p = &(*ppPoslist)[-2];
173631 while( p>pStart && (c=*p--)==0 );
173637 c = *p--;
173666 ** message is written to context pContext and SQLITE_ERROR returned. The
173667 ** string passed via zFunc is used as part of the error message.
173681 sqlite3_result_error(pContext, zErr, -1);
173700 int iCol = -1;
173704 ** the non-overloaded version would have been called instead of this one).
173710 "wrong number of arguments to function snippet()", -1);
173729 sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
173777 p = (Fts3Table *)pCursor->base.pVtab;
173784 sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
173787 sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
173859 sqlite3 *db = p->db; /* Database connection */
173869 ** flushes the pending terms table. But leave the (no-op) call to
173872 assert( p->nPendingData==0 );
173877 if( p->zContentTbl==0 ){
173880 p->zDb, p->zName, zName
173884 if( p->bHasDocsize ){
173887 p->zDb, p->zName, zName
173890 if( p->bHasStat ){
173893 p->zDb, p->zName, zName
173898 p->zDb, p->zName, zName
173902 p->zDb, p->zName, zName
173910 ** Flush the contents of the pending-terms table to disk.
173915 assert( ((Fts3Table *)pVtab)->inTransaction );
173916 assert( ((Fts3Table *)pVtab)->mxSavepoint <= iSavepoint );
173917 TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
173918 if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
173927 ** This is a no-op.
173933 assert( p->inTransaction );
173934 assert( p->mxSavepoint >= iSavepoint );
173935 TESTONLY( p->mxSavepoint = iSavepoint-1 );
173947 assert( p->inTransaction );
173948 TESTONLY( p->mxSavepoint = iSavepoint );
174007 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
174060 /* Allocate and initialize the hash-table used to store tokenizers. */
174068 /* Load the built-in tokenizers into the hash table */
174090 /* Create the virtual table wrapper around the hash-table and overload
174096 && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
174131 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
174133 ** within the union of all segments of a b-tree. Hence the name.
174136 ** segment b-tree (if the term is not a prefix or it is a prefix for which
174137 ** there exists prefix b-tree of the right length) then it may be traversed
174138 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
174149 if( pExpr->eType==FTSQUERY_PHRASE ){
174151 int nToken = pExpr->pPhrase->nToken;
174154 Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
174156 pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
174163 assert( pExpr->pPhrase->iDoclistToken==0 );
174164 pExpr->pPhrase->iDoclistToken = -1;
174166 *pnOr += (pExpr->eType==FTSQUERY_OR);
174167 fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
174168 fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
174175 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
174191 assert( iToken!=p->iDoclistToken );
174194 sqlite3_free(p->doclist.aAll);
174195 p->doclist.aAll = 0;
174196 p->doclist.nAll = 0;
174199 else if( p->iDoclistToken<0 ){
174200 p->doclist.aAll = pList;
174201 p->doclist.nAll = nList;
174204 else if( p->doclist.aAll==0 ){
174215 if( p->iDoclistToken<iToken ){
174216 pLeft = p->doclist.aAll;
174217 nLeft = p->doclist.nAll;
174220 nDiff = iToken - p->iDoclistToken;
174222 pRight = p->doclist.aAll;
174223 nRight = p->doclist.nAll;
174226 nDiff = p->iDoclistToken - iToken;
174230 pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
174233 p->doclist.aAll = pRight;
174234 p->doclist.nAll = nRight;
174237 if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
174242 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
174251 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174255 for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
174256 Fts3PhraseToken *pToken = &p->aToken[iToken];
174257 assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
174259 if( pToken->pSegcsr ){
174262 rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
174267 assert( pToken->pSegcsr==0 );
174288 int iPrev = -1; /* Token number of previous deferred token */
174290 assert( pPhrase->doclist.bFreeList==0 );
174292 for(iToken=0; iToken<pPhrase->nToken; iToken++){
174293 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
174294 Fts3DeferredToken *pDeferred = pToken->pDeferred;
174304 pPhrase->doclist.pList = 0;
174305 pPhrase->doclist.nList = 0;
174318 fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
174321 nPoslist = (int)(aOut - aPoslist);
174324 pPhrase->doclist.pList = 0;
174325 pPhrase->doclist.nList = 0;
174334 int nMaxUndeferred = pPhrase->iDoclistToken;
174336 pPhrase->doclist.pList = aPoslist;
174337 pPhrase->doclist.nList = nPoslist;
174338 pPhrase->doclist.iDocid = pCsr->iPrevId;
174339 pPhrase->doclist.bFreeList = 1;
174348 p2 = pPhrase->doclist.pList;
174349 nDistance = nMaxUndeferred - iPrev;
174351 p1 = pPhrase->doclist.pList;
174353 nDistance = iPrev - nMaxUndeferred;
174362 pPhrase->doclist.pList = aOut;
174364 pPhrase->doclist.bFreeList = 1;
174365 pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
174368 pPhrase->doclist.pList = 0;
174369 pPhrase->doclist.nList = 0;
174386 ** This function is called for each Fts3Phrase in a full-text query
174398 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174406 ** tokens or prefix tokens that cannot use a prefix-index. */
174409 && pCsr->bDesc==pTab->bDescIdx
174410 && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
174412 && pTab->bNoIncrDoclist==0
174415 for(i=0; bIncrOk==1 && i<p->nToken; i++){
174416 Fts3PhraseToken *pToken = &p->aToken[i];
174417 if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
174420 if( pToken->pSegcsr ) bHaveIncr = 1;
174425 int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
174426 for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
174427 Fts3PhraseToken *pToken = &p->aToken[i];
174428 Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
174430 rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
174433 p->bIncr = 1;
174437 p->bIncr = 0;
174440 assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
174448 ** pending-terms lists when writing to databases with "order=desc".
174461 u8 *pbEof /* OUT: End-of-file flag */
174484 iMul = (bDescIdx ? -1 : 1);
174487 *pnList = (int)(pEnd - pNext);
174491 int iMul = (bDescIdx ? -1 : 1);
174494 *piDocid -= (iMul * iDelta);
174501 *pnList = (int)(pSave - p);
174516 u8 *pbEof /* OUT: End-of-file flag */
174536 *piDocid += ((bDescIdx ? -1 : 1) * iVar);
174544 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
174555 if( pDL->pNextDocid ){
174556 pIter = pDL->pNextDocid;
174557 assert( pDL->aAll!=0 || pIter==0 );
174559 pIter = pDL->aAll;
174562 if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
174568 if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
174569 pDL->iDocid += iDelta;
174571 pDL->iDocid -= iDelta;
174573 pDL->pList = pIter;
174575 pDL->nList = (int)(pIter - pDL->pList);
174577 /* pIter now points just past the 0x00 that terminates the position-
174578 ** list for document pDL->iDocid. However, if this position-list was
174581 ** with this case by advancing pIter past the zero-padding added by
174585 pDL->pNextDocid = pIter;
174586 assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
174604 ** multi-token phrase. Advance it to the next matching document in the
174620 if( pPhrase->iDoclistToken==iToken ){
174621 assert( p->bIgnore==0 );
174622 assert( pPhrase->aToken[iToken].pSegcsr==0 );
174623 fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
174624 p->pList = pPhrase->doclist.pList;
174625 p->nList = pPhrase->doclist.nList;
174626 p->iDocid = pPhrase->doclist.iDocid;
174628 Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
174629 assert( pToken->pDeferred==0 );
174630 assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
174631 if( pToken->pSegcsr ){
174632 assert( p->bIgnore==0 );
174634 pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
174636 if( p->pList==0 ) *pbEof = 1;
174638 p->bIgnore = 1;
174669 Fts3Doclist *pDL = &p->doclist;
174670 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174675 assert( p->bIncr==1 );
174677 if( p->nToken==1 ){
174678 rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
174679 &pDL->iDocid, &pDL->pList, &pDL->nList
174681 if( pDL->pList==0 ) bEof = 1;
174683 int bDescDoclist = pCsr->bDesc;
174687 assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
174688 assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
174696 for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
174703 assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
174707 for(i=0; i<p->nToken; i++){
174722 int nByte = a[p->nToken-1].nList;
174725 memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
174728 for(i=0; i<(p->nToken-1); i++){
174731 char *pR = aDoclist;
174733 int nDist = p->nToken-1-i;
174734 int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
174736 nList = (int)(pOut - aDoclist);
174739 if( i==(p->nToken-1) ){
174740 pDL->iDocid = iMax;
174741 pDL->pList = aDoclist;
174742 pDL->nList = nList;
174743 pDL->bFreeList = 1;
174770 Fts3Doclist *pDL = &p->doclist;
174771 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174773 if( p->bIncr ){
174775 }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
174776 sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
174777 &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
174779 pDL->pList = pDL->pNextDocid;
174789 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
174797 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
174809 if( pExpr->eType==FTSQUERY_PHRASE ){
174810 int nToken = pExpr->pPhrase->nToken;
174814 if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
174816 pExpr->bDeferred = (i==nToken);
174818 *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
174820 fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
174821 fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
174822 pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
174852 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
174865 if( pExpr->eType==FTSQUERY_PHRASE ){
174866 Fts3Phrase *pPhrase = pExpr->pPhrase;
174868 for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
174870 pTC->pPhrase = pPhrase;
174871 pTC->iToken = i;
174872 pTC->pRoot = pRoot;
174873 pTC->pToken = &pPhrase->aToken[i];
174874 pTC->iCol = pPhrase->iColumn;
174875 *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
174877 }else if( pExpr->eType!=FTSQUERY_NOT ){
174878 assert( pExpr->eType==FTSQUERY_OR
174879 || pExpr->eType==FTSQUERY_AND
174880 || pExpr->eType==FTSQUERY_NEAR
174882 assert( pExpr->pLeft && pExpr->pRight );
174883 if( pExpr->eType==FTSQUERY_OR ){
174884 pRoot = pExpr->pLeft;
174888 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
174889 if( pExpr->eType==FTSQUERY_OR ){
174890 pRoot = pExpr->pRight;
174894 fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
174912 if( pCsr->nRowAvg==0 ){
174924 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
174947 pCsr->nDoc = nDoc;
174948 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
174949 assert( pCsr->nRowAvg>0 );
174953 *pnPage = pCsr->nRowAvg;
174977 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
174992 if( pTab->zContentTbl ){
175014 ** to retrieve the entire doclist for the token from the full-text index.
175020 ** b. Part of a multi-token phrase.
175032 ** (nMinEst + 4^nOther - 1) / (4^nOther)
175041 && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
175048 if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
175053 Fts3PhraseToken *pToken = pTC->pToken;
175054 rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
175055 fts3SegReaderCursorFree(pToken->pSegcsr);
175056 pToken->pSegcsr = 0;
175059 ** for-loop. Except, limit the value to 2^24 to prevent it from
175060 ** overflowing the 32-bit integer it is stored in. */
175063 if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
175065 ** part of a multi-token phrase. Either way, the entire doclist will
175067 Fts3PhraseToken *pToken = pTC->pToken;
175070 rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
175074 pTab, pTC->pPhrase, pTC->iToken,pList,nList
175080 pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
175086 pTC->pToken = 0;
175094 ** the full-text query currently stored in pCsr->pExpr. To iterate through
175100 ** if( pCsr->bEof ) break;
175101 ** ... return row pCsr->iPrevId to the caller ...
175105 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175111 fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
175115 if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
175131 fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
175132 nToken = (int)(pTC-aTC);
175133 nOr = (int)(ppOr-apOr);
175147 fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
175155 if( pPhrase->doclist.bFreeList ){
175156 sqlite3_free(pPhrase->doclist.pList);
175158 pPhrase->doclist.pList = 0;
175159 pPhrase->doclist.nList = 0;
175160 pPhrase->doclist.bFreeList = 0;
175179 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
175192 int nParam1 = nNear + pPhrase->nToken;
175199 assert( pPhrase->doclist.pList );
175201 p2 = pOut = pPhrase->doclist.pList;
175206 nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
175207 assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 );
175208 if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
175209 assert( pPhrase->doclist.pList[nNew]=='\0' );
175210 memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
175211 pPhrase->doclist.nList = nNew;
175213 *paPoslist = pPhrase->doclist.pList;
175214 *pnToken = pPhrase->nToken;
175221 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
175225 ** or descending if it is non-zero.
175230 ** Fts3Expr.bEof (non-zero if EOF - there is no next row)
175237 ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
175238 ** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
175245 ** the db. In this case the position-list is not populated at all.
175248 ** more non-deferred tokens, then the expression is advanced to the
175249 ** next possible match, considering only non-deferred tokens. In other
175267 int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
175268 assert( pExpr->bEof==0 );
175269 pExpr->bStart = 1;
175271 switch( pExpr->eType ){
175274 Fts3Expr *pLeft = pExpr->pLeft;
175275 Fts3Expr *pRight = pExpr->pRight;
175276 assert( !pLeft->bDeferred || !pRight->bDeferred );
175278 if( pLeft->bDeferred ){
175282 pExpr->iDocid = pRight->iDocid;
175283 pExpr->bEof = pRight->bEof;
175284 }else if( pRight->bDeferred ){
175288 pExpr->iDocid = pLeft->iDocid;
175289 pExpr->bEof = pLeft->bEof;
175294 while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
175295 sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
175303 pExpr->iDocid = pLeft->iDocid;
175304 pExpr->bEof = (pLeft->bEof || pRight->bEof);
175305 if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
175306 assert( pRight->eType==FTSQUERY_PHRASE );
175307 if( pRight->pPhrase->doclist.aAll ){
175308 Fts3Doclist *pDl = &pRight->pPhrase->doclist;
175309 while( *pRc==SQLITE_OK && pRight->bEof==0 ){
175310 memset(pDl->pList, 0, pDl->nList);
175314 if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
175315 Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
175316 while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
175317 memset(pDl->pList, 0, pDl->nList);
175321 pRight->bEof = pLeft->bEof = 1;
175328 Fts3Expr *pLeft = pExpr->pLeft;
175329 Fts3Expr *pRight = pExpr->pRight;
175330 sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
175332 assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
175333 assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
175335 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
175337 }else if( pLeft->bEof || iCmp>0 ){
175344 pExpr->bEof = (pLeft->bEof && pRight->bEof);
175345 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
175346 if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
175347 pExpr->iDocid = pLeft->iDocid;
175349 pExpr->iDocid = pRight->iDocid;
175356 Fts3Expr *pLeft = pExpr->pLeft;
175357 Fts3Expr *pRight = pExpr->pRight;
175359 if( pRight->bStart==0 ){
175361 assert( *pRc!=SQLITE_OK || pRight->bStart );
175365 if( pLeft->bEof==0 ){
175367 && !pRight->bEof
175368 && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
175373 pExpr->iDocid = pLeft->iDocid;
175374 pExpr->bEof = pLeft->bEof;
175379 Fts3Phrase *pPhrase = pExpr->pPhrase;
175381 *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
175382 pExpr->iDocid = pPhrase->doclist.iDocid;
175395 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
175415 ** +--NEAR--+ <-- root of NEAR query
175417 ** +--NEAR--+ "z"
175419 ** +--NEAR--+ "y"
175423 ** The right-hand child of a NEAR node is always a phrase. The
175424 ** left-hand child may be either a phrase or a NEAR node. There are
175425 ** no exceptions to this - it's the way the parser in fts3_expr.c works.
175428 && pExpr->eType==FTSQUERY_NEAR
175429 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
175436 for(p=pExpr; p->pLeft; p=p->pLeft){
175437 assert( p->pRight->pPhrase->doclist.nList>0 );
175438 nTmp += p->pRight->pPhrase->doclist.nList;
175440 nTmp += p->pPhrase->doclist.nList;
175446 char *aPoslist = p->pPhrase->doclist.pList;
175447 int nToken = p->pPhrase->nToken;
175449 for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
175450 Fts3Phrase *pPhrase = p->pRight->pPhrase;
175451 int nNear = p->nNear;
175455 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
175456 nToken = pExpr->pRight->pPhrase->nToken;
175457 for(p=pExpr->pLeft; p && res; p=p->pLeft){
175460 assert( p->pParent && p->pParent->pLeft==p );
175461 nNear = p->pParent->nNear;
175463 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
175477 ** Assuming no error occurs or has occurred, It returns non-zero if the
175481 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
175493 switch( pExpr->eType ){
175497 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
175498 && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
175515 ** (as "b" is part of a non-matching NEAR clause).
175518 && pExpr->eType==FTSQUERY_NEAR
175519 && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
175522 for(p=pExpr; p->pPhrase==0; p=p->pLeft){
175523 if( p->pRight->iDocid==pCsr->iPrevId ){
175524 fts3EvalInvalidatePoslist(p->pRight->pPhrase);
175527 if( p->iDocid==pCsr->iPrevId ){
175528 fts3EvalInvalidatePoslist(p->pPhrase);
175535 int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
175536 int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
175543 fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
175544 && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
175550 if( pCsr->pDeferred
175551 && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
175553 Fts3Phrase *pPhrase = pExpr->pPhrase;
175554 assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
175555 if( pExpr->bDeferred ){
175559 bHit = (pPhrase->doclist.pList!=0);
175560 pExpr->iDocid = pCsr->iPrevId;
175565 pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
175566 && pExpr->pPhrase->doclist.nList>0
175578 ** iterating through the results of a full-text query. At this point the
175611 if( pCsr->pDeferred ){
175617 bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
175619 /* Free the position-lists accumulated for each deferred token above. */
175632 Fts3Expr *pExpr = pCsr->pExpr;
175633 assert( pCsr->isEof==0 );
175635 pCsr->isEof = 1;
175638 if( pCsr->isRequireSeek==0 ){
175639 sqlite3_reset(pCsr->pStmt);
175641 assert( sqlite3_data_count(pCsr->pStmt)==0 );
175643 pCsr->isEof = pExpr->bEof;
175644 pCsr->isRequireSeek = 1;
175645 pCsr->isMatchinfoNeeded = 1;
175646 pCsr->iPrevId = pExpr->iDocid;
175647 }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
175653 (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
175654 || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
175656 pCsr->isEof = 1;
175668 ** a no-op. If an error occurs within this function, *pRc is set to an
175677 Fts3Phrase *pPhrase = pExpr->pPhrase;
175681 if( pPhrase->bIncr ){
175683 for(i=0; i<pPhrase->nToken; i++){
175684 Fts3PhraseToken *pToken = &pPhrase->aToken[i];
175685 assert( pToken->pDeferred==0 );
175686 if( pToken->pSegcsr ){
175687 sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
175692 pPhrase->doclist.pNextDocid = 0;
175693 pPhrase->doclist.iDocid = 0;
175694 pPhrase->pOrPoslist = 0;
175697 pExpr->iDocid = 0;
175698 pExpr->bEof = 0;
175699 pExpr->bStart = 0;
175701 fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
175702 fts3EvalRestart(pCsr, pExpr->pRight, pRc);
175710 ** the values in Fts3Expr.aMI[] according to the position-list currently
175711 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
175716 Fts3Phrase *pPhrase = pExpr->pPhrase;
175717 if( pPhrase && pPhrase->doclist.pList ){
175719 char *p = pPhrase->doclist.pList;
175732 pExpr->aMI[iCol*3 + 1] += iCnt;
175733 pExpr->aMI[iCol*3 + 2] += (iCnt>0);
175740 fts3EvalUpdateCounts(pExpr->pLeft, nCol);
175741 fts3EvalUpdateCounts(pExpr->pRight, nCol);
175762 assert( pExpr->eType==FTSQUERY_PHRASE );
175763 if( pExpr->aMI==0 ){
175764 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175768 sqlite3_int64 iPrevId = pCsr->iPrevId;
175774 while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
175775 pRoot = pRoot->pParent;
175777 iDocid = pRoot->iDocid;
175778 bEof = pRoot->bEof;
175779 assert( pRoot->bStart );
175782 for(p=pRoot; p; p=p->pLeft){
175783 Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
175784 assert( pE->aMI==0 );
175785 pE->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
175786 if( !pE->aMI ) return SQLITE_NOMEM;
175787 memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
175792 while( pCsr->isEof==0 && rc==SQLITE_OK ){
175796 if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
175797 assert( sqlite3_data_count(pCsr->pStmt)==0 );
175801 pCsr->isEof = pRoot->bEof;
175802 pCsr->isRequireSeek = 1;
175803 pCsr->isMatchinfoNeeded = 1;
175804 pCsr->iPrevId = pRoot->iDocid;
175805 }while( pCsr->isEof==0
175806 && pRoot->eType==FTSQUERY_NEAR
175810 if( rc==SQLITE_OK && pCsr->isEof==0 ){
175811 fts3EvalUpdateCounts(pRoot, pTab->nColumn);
175815 pCsr->isEof = 0;
175816 pCsr->iPrevId = iPrevId;
175819 pRoot->bEof = bEof;
175825 ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
175830 assert_fts3_nc( pRoot->bEof==0 );
175831 if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB;
175832 }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
175861 ** * If a phrase contains some deferred tokens (and some non-deferred
175863 ** the non-deferred tokens instead of actual phrase occurrences.
175873 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175877 if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
175878 assert( pCsr->nDoc>0 );
175879 for(iCol=0; iCol<pTab->nColumn; iCol++){
175880 aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
175881 aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
175886 assert( pExpr->aMI );
175887 for(iCol=0; iCol<pTab->nColumn; iCol++){
175888 aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
175889 aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
175902 ** a position-list indicating the occurrences of the phrase in column iCol
175908 ** if the requested column contains "a b X c d X X" and the position-list
175922 Fts3Phrase *pPhrase = pExpr->pPhrase;
175923 Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
175931 assert( iCol>=0 && iCol<pTab->nColumn );
175932 if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
175936 iDocid = pExpr->iDocid;
175937 pIter = pPhrase->doclist.pList;
175938 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
175940 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
175949 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
175953 for(p=pExpr->pParent; p; p=p->pParent){
175954 if( p->eType==FTSQUERY_OR ) bOr = 1;
175955 if( p->eType==FTSQUERY_NEAR ) pNear = p;
175956 if( p->bEof ) bTreeEof = 1;
175963 if( pPhrase->bIncr ){
175964 int bEofSave = pNear->bEof;
175966 while( rc==SQLITE_OK && !pNear->bEof ){
175968 if( bEofSave==0 && pNear->iDocid==iDocid ) break;
175970 assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
175973 while( rc==SQLITE_OK && !pNear->bEof ){
175980 for(p=pNear; p; p=p->pLeft){
175984 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
175985 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
175986 assert( pTest->eType==FTSQUERY_PHRASE );
175987 pPh = pTest->pPhrase;
175989 pIter = pPh->pOrPoslist;
175990 iDocid = pPh->iOrDocid;
175991 if( pCsr->bDesc==bDescDoclist ){
175992 bEof = !pPh->doclist.nAll ||
175993 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
175994 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
175996 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
176001 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
176002 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
176005 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
176010 pPh->pOrPoslist = pIter;
176011 pPh->iOrDocid = iDocid;
176012 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
176016 pIter = pPhrase->pOrPoslist;
176047 ** * the contents of pPhrase->doclist, and
176053 sqlite3_free(pPhrase->doclist.aAll);
176055 memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
176056 for(i=0; i<pPhrase->nToken; i++){
176057 fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
176058 pPhrase->aToken[i].pSegcsr = 0;
176126 int nStop; /* Byte-length of string zStop */
176156 char **pzErr /* OUT: sqlite3_malloc'd error message */
176170 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
176171 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
176198 p->pFts3Tab = (Fts3Table *)&p[1];
176199 p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
176200 p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
176201 p->pFts3Tab->db = db;
176202 p->pFts3Tab->nIndex = 1;
176204 memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
176205 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
176206 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
176223 Fts3Table *pFts3 = p->pFts3Tab;
176227 for(i=0; i<SizeofArray(pFts3->aStmt); i++){
176228 sqlite3_finalize(pFts3->aStmt[i]);
176230 sqlite3_free(pFts3->zSegmentsTbl);
176240 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
176247 int iEq = -1;
176248 int iGe = -1;
176249 int iLe = -1;
176250 int iLangid = -1;
176256 if( pInfo->nOrderBy==1
176257 && pInfo->aOrderBy[0].iColumn==0
176258 && pInfo->aOrderBy[0].desc==0
176260 pInfo->orderByConsumed = 1;
176265 for(i=0; i<pInfo->nConstraint; i++){
176266 if( pInfo->aConstraint[i].usable ){
176267 int op = pInfo->aConstraint[i].op;
176268 int iCol = pInfo->aConstraint[i].iColumn;
176284 pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
176285 pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
176286 pInfo->estimatedCost = 5;
176288 pInfo->idxNum = 0;
176289 pInfo->estimatedCost = 20000;
176291 pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
176292 pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
176293 pInfo->estimatedCost /= 2;
176296 pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
176297 pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
176298 pInfo->estimatedCost /= 2;
176302 pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
176303 pInfo->estimatedCost--;
176310 ** xOpen - Open a cursor.
176326 ** xClose - Close a cursor.
176329 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
176333 sqlite3Fts3SegReaderFinish(&pCsr->csr);
176334 sqlite3_free((void *)pCsr->filter.zTerm);
176335 sqlite3_free(pCsr->zStop);
176336 sqlite3_free(pCsr->aStat);
176342 if( nSize>pCsr->nStat ){
176344 aNew = (struct Fts3auxColstats *)sqlite3_realloc64(pCsr->aStat,
176348 memset(&aNew[pCsr->nStat], 0,
176349 sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
176351 pCsr->aStat = aNew;
176352 pCsr->nStat = nSize;
176358 ** xNext - Advance the cursor to the next row, if any.
176362 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
176366 pCsr->iRowid++;
176368 for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
176369 if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
176372 rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
176375 int nDoclist = pCsr->csr.nDoclist;
176376 char *aDoclist = pCsr->csr.aDoclist;
176381 if( pCsr->zStop ){
176382 int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
176383 int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
176384 if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
176385 pCsr->isEof = 1;
176391 memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
176401 pCsr->aStat[0].nDoc++;
176417 pCsr->aStat[1].nDoc++;
176428 pCsr->aStat[iCol+1].nOcc++;
176429 pCsr->aStat[0].nOcc++;
176437 pCsr->aStat[iCol+1].nDoc++;
176443 pCsr->iCol = 0;
176446 pCsr->isEof = 1;
176452 ** xFilter - Initialize a cursor to point at the start of its data.
176462 Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
176467 int iEq = -1; /* Index of term=? value in apVal */
176468 int iGe = -1; /* Index of term>=? value in apVal */
176469 int iLe = -1; /* Index of term<=? value in apVal */
176470 int iLangid = -1; /* Index of languageid=? value in apVal */
176498 testcase(pCsr->filter.zTerm);
176499 sqlite3Fts3SegReaderFinish(&pCsr->csr);
176500 sqlite3_free((void *)pCsr->filter.zTerm);
176501 sqlite3_free(pCsr->aStat);
176502 memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
176504 pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
176505 if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
176509 assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
176511 pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
176512 if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
176513 pCsr->filter.nTerm = (int)strlen(pCsr->filter.zTerm);
176518 pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
176519 if( pCsr->zStop==0 ) return SQLITE_NOMEM;
176520 pCsr->nStop = (int)strlen(pCsr->zStop);
176533 pCsr->iLangid = iLangVal;
176536 pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
176539 rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
176547 ** xEof - Return true if the cursor is at EOF, or false otherwise.
176551 return pCsr->isEof;
176555 ** xColumn - Return a column value.
176564 assert( p->isEof==0 );
176567 sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
176571 if( p->iCol ){
176572 sqlite3_result_int(pCtx, p->iCol-1);
176574 sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
176579 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
176583 sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
176588 sqlite3_result_int(pCtx, p->iLangid);
176596 ** xRowid - Return the current rowid for the cursor.
176603 *pRowid = pCsr->iRowid;
176661 ** (the right-hand argument to the MATCH operator). Because the supported
176663 ** hand-coded.
176678 ** c) The old syntax supports the "-" token qualifier. This is not
176688 ** non-zero the new syntax is activated. This is so both syntaxes can
176700 ** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
176735 ** FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
176736 ** FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
176744 int bFts4; /* True to allow FTS4-only syntax */
176747 int isNot; /* True if getNextNode() sees a unary - */
176748 sqlite3_context *pCtx; /* Write error message here */
176785 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
176789 rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
176792 pCsr->pTokenizer = pTokenizer;
176793 if( pModule->iVersion>=1 ){
176794 rc = pModule->xLanguageid(pCsr, iLangid);
176796 pModule->xClose(pCsr);
176830 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
176831 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
176844 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
176850 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
176857 pRet->eType = FTSQUERY_PHRASE;
176858 pRet->pPhrase = (Fts3Phrase *)&pRet[1];
176859 pRet->pPhrase->nToken = 1;
176860 pRet->pPhrase->iColumn = iCol;
176861 pRet->pPhrase->aToken[0].n = nToken;
176862 pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
176863 memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
176866 pRet->pPhrase->aToken[0].isPrefix = 1;
176872 && iStart>0 && z[iStart-1]=='-'
176874 pParse->isNot = 1;
176875 iStart--;
176876 }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
176877 pRet->pPhrase->aToken[0].bFirst = 1;
176878 iStart--;
176890 pModule->xClose(pCursor);
176899 ** Enlarge a memory allocation. If an out-of-memory allocation occurs,
176927 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
176928 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
176958 pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
176964 rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
176975 pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
176981 pToken->n = nByte;
176982 pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
176983 pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
176988 pModule->xClose(pCursor);
176998 memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
176999 p->eType = FTSQUERY_PHRASE;
177000 p->pPhrase = (Fts3Phrase *)&p[1];
177001 p->pPhrase->iColumn = pParse->iDefaultCol;
177002 p->pPhrase->nToken = nToken;
177004 zBuf = (char *)&p->pPhrase->aToken[nToken];
177012 for(jj=0; jj<p->pPhrase->nToken; jj++){
177013 p->pPhrase->aToken[jj].z = zBuf;
177014 zBuf += p->pPhrase->aToken[jj].n;
177024 pModule->xClose(pCursor);
177038 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
177066 pParse->isNot = 0;
177072 nInput--;
177083 if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
177087 if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
177089 int nKey = pKey->n;
177093 if( pKey->eType==FTSQUERY_NEAR ){
177112 pRet->eType = pKey->eType;
177113 pRet->nNear = nNear;
177115 *pnConsumed = (int)((zInput - z) + nKey);
177132 *pnConsumed = (int)((zInput - z) + ii + 1);
177136 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
177142 pParse->nNest++;
177144 if( pParse->nNest>1000 ) return SQLITE_ERROR;
177146 if( pParse->nNest>SQLITE_MAX_EXPR_DEPTH ) return SQLITE_ERROR;
177148 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
177149 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
177152 pParse->nNest--;
177153 *pnConsumed = (int)((zInput - z) + 1);
177170 iCol = pParse->iDefaultCol;
177172 for(ii=0; ii<pParse->nCol; ii++){
177173 const char *zStr = pParse->azCol[ii];
177179 iColLen = (int)((zInput - z) + nStr + 1);
177183 rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
177208 assert( p->eType!=FTSQUERY_PHRASE );
177210 return p->eType;
177211 }else if( p->eType==FTSQUERY_NEAR ){
177213 }else if( p->eType==FTSQUERY_OR ){
177216 assert( p->eType==FTSQUERY_AND );
177234 while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
177235 pSplit = pSplit->pParent;
177238 if( pSplit->pParent ){
177239 assert( pSplit->pParent->pRight==pSplit );
177240 pSplit->pParent->pRight = pNew;
177241 pNew->pParent = pSplit->pParent;
177245 pNew->pLeft = pSplit;
177246 pSplit->pParent = pNew;
177252 ** closing bracket - ')' - is encountered.
177284 && p->eType==FTSQUERY_PHRASE && pParse->isNot
177293 pNot->eType = FTSQUERY_NOT;
177294 pNot->pRight = p;
177295 p->pParent = pNot;
177297 pNot->pLeft = pNotBranch;
177298 pNotBranch->pParent = pNot;
177303 int eType = p->eType;
177304 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
177327 pAnd->eType = FTSQUERY_AND;
177342 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
177343 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
177352 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
177353 pPrev->pRight = p;
177354 p->pParent = pPrev;
177368 nIn -= nByte;
177383 while( pIter->pLeft ){
177384 pIter = pIter->pLeft;
177386 pIter->pLeft = pRet;
177387 pRet->pParent = pIter;
177392 *pnConsumed = n - nIn;
177414 rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
177416 rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
177429 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
177438 int eType = pRoot->eType; /* Type of node in this tree */
177458 /* Set $p to point to the left-most leaf in the tree of eType nodes. */
177459 for(p=pRoot; p->eType==eType; p=p->pLeft){
177460 assert( p->pParent==0 || p->pParent->pLeft==p );
177461 assert( p->pLeft && p->pRight );
177467 Fts3Expr *pParent = p->pParent; /* Current parent of p */
177469 assert( pParent==0 || pParent->pLeft==p );
177470 p->pParent = 0;
177472 pParent->pLeft = 0;
177476 rc = fts3ExprBalance(&p, nMaxDepth-1);
177485 pFree->pLeft = apLeaf[iLvl];
177486 pFree->pRight = p;
177487 pFree->pLeft->pParent = pFree;
177488 pFree->pRight->pParent = pFree;
177491 pFree = pFree->pParent;
177492 p->pParent = 0;
177506 for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
177509 assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
177510 pParent->pRight->pParent = pParent->pParent;
177511 if( pParent->pParent ){
177512 pParent->pParent->pLeft = pParent->pRight;
177515 pRoot = pParent->pRight;
177520 pParent->pParent = pFree;
177530 p->pParent = 0;
177533 pFree->pRight = p;
177534 pFree->pLeft = apLeaf[i];
177535 pFree->pLeft->pParent = pFree;
177536 pFree->pRight->pParent = pFree;
177539 pFree = pFree->pParent;
177540 p->pParent = 0;
177554 pFree = pDel->pParent;
177563 Fts3Expr *pLeft = pRoot->pLeft;
177564 Fts3Expr *pRight = pRoot->pRight;
177566 pRoot->pLeft = 0;
177567 pRoot->pRight = 0;
177568 pLeft->pParent = 0;
177569 pRight->pParent = 0;
177571 rc = fts3ExprBalance(&pLeft, nMaxDepth-1);
177573 rc = fts3ExprBalance(&pRight, nMaxDepth-1);
177581 pRoot->pLeft = pLeft;
177582 pLeft->pParent = pRoot;
177583 pRoot->pRight = pRight;
177584 pRight->pParent = pRoot;
177612 int bFts4, /* True to allow FTS4-only syntax */
177653 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
177657 ** nul-terminated string and the length is determined using strlen().
177663 ** Column names must be nul-terminated strings.
177666 ** that appears on the left-hand-side of the MATCH operator (the default
177668 ** specified as part of the query string), or -1 if tokens may by default
177675 int bFts4, /* True to allow FTS4-only syntax */
177680 char **pzErr /* OUT: Error message (sqlite3_malloc) */
177716 assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
177717 sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
177718 sqlite3_free(p->aMI);
177731 assert( pDel==0 || pDel->pParent==0 );
177732 for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
177733 assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
177736 Fts3Expr *pParent = p->pParent;
177738 if( pParent && p==pParent->pLeft && pParent->pRight ){
177739 p = pParent->pRight;
177740 while( p && (p->pLeft || p->pRight) ){
177741 assert( p==p->pParent->pRight || p==p->pParent->pLeft );
177742 p = (p->pLeft ? p->pLeft : p->pRight);
177773 switch( pExpr->eType ){
177775 Fts3Phrase *pPhrase = pExpr->pPhrase;
177778 "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
177779 for(i=0; zBuf && i<pPhrase->nToken; i++){
177781 pPhrase->aToken[i].n, pPhrase->aToken[i].z,
177782 (pPhrase->aToken[i].isPrefix?"+":"")
177789 zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
177803 if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
177806 if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
177847 "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
177858 sqlite3_result_error(context, zErr, -1);
177866 nCol = argc-2;
177891 sqlite3_result_error(context, "Error parsing expression", -1);
177895 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
177903 rc = pTokenizer->pModule->xDestroy(pTokenizer);
177929 db, "fts3_exprtest", -1, SQLITE_UTF8, (void*)pHash, fts3ExprTest, 0, 0
177933 -1, SQLITE_UTF8, (void*)pHash, fts3ExprTestRebalance, 0, 0
177955 ** This is the implementation of generic hash-tables used in SQLite.
177957 ** implementation for the full-text indexing module.
178005 pNew->keyClass = keyClass;
178006 pNew->copyKey = copyKey;
178007 pNew->first = 0;
178008 pNew->count = 0;
178009 pNew->htsize = 0;
178010 pNew->ht = 0;
178021 elem = pH->first;
178022 pH->first = 0;
178023 fts3HashFree(pH->ht);
178024 pH->ht = 0;
178025 pH->htsize = 0;
178027 Fts3HashElem *next_elem = elem->next;
178028 if( pH->copyKey && elem->pKey ){
178029 fts3HashFree(elem->pKey);
178034 pH->count = 0;
178046 nKey--;
178061 while( nKey-- > 0 ){
178115 pHead = pEntry->chain;
178117 pNew->next = pHead;
178118 pNew->prev = pHead->prev;
178119 if( pHead->prev ){ pHead->prev->next = pNew; }
178120 else { pH->first = pNew; }
178121 pHead->prev = pNew;
178123 pNew->next = pH->first;
178124 if( pH->first ){ pH->first->prev = pNew; }
178125 pNew->prev = 0;
178126 pH->first = pNew;
178128 pEntry->count++;
178129 pEntry->chain = pNew;
178137 ** Return non-zero if a memory allocation error occurs.
178144 assert( (new_size & (new_size-1))==0 );
178147 fts3HashFree(pH->ht);
178148 pH->ht = new_ht;
178149 pH->htsize = new_size;
178150 xHash = ftsHashFunction(pH->keyClass);
178151 for(elem=pH->first, pH->first=0; elem; elem = next_elem){
178152 int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
178153 next_elem = elem->next;
178173 if( pH->ht ){
178174 struct _fts3ht *pEntry = &pH->ht[h];
178175 elem = pEntry->chain;
178176 count = pEntry->count;
178177 xCompare = ftsCompareFunction(pH->keyClass);
178178 while( count-- && elem ){
178179 if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
178182 elem = elem->next;
178197 if( elem->prev ){
178198 elem->prev->next = elem->next;
178200 pH->first = elem->next;
178202 if( elem->next ){
178203 elem->next->prev = elem->prev;
178205 pEntry = &pH->ht[h];
178206 if( pEntry->chain==elem ){
178207 pEntry->chain = elem->next;
178209 pEntry->count--;
178210 if( pEntry->count<=0 ){
178211 pEntry->chain = 0;
178213 if( pH->copyKey && elem->pKey ){
178214 fts3HashFree(elem->pKey);
178217 pH->count--;
178218 if( pH->count<=0 ){
178219 assert( pH->first==0 );
178220 assert( pH->count==0 );
178233 if( pH==0 || pH->ht==0 ) return 0;
178234 xHash = ftsHashFunction(pH->keyClass);
178237 assert( (pH->htsize & (pH->htsize-1))==0 );
178238 return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
178250 return pElem ? pElem->data : 0;
178281 xHash = ftsHashFunction(pH->keyClass);
178284 assert( (pH->htsize & (pH->htsize-1))==0 );
178285 h = hraw & (pH->htsize-1);
178288 void *old_data = elem->data;
178292 elem->data = data;
178297 if( (pH->htsize==0 && fts3Rehash(pH,8))
178298 || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
178300 pH->count = 0;
178303 assert( pH->htsize>0 );
178306 if( pH->copyKey && pKey!=0 ){
178307 new_elem->pKey = fts3HashMalloc( nKey );
178308 if( new_elem->pKey==0 ){
178312 memcpy((void*)new_elem->pKey, pKey, nKey);
178314 new_elem->pKey = (void*)pKey;
178316 new_elem->nKey = nKey;
178317 pH->count++;
178318 assert( pH->htsize>0 );
178319 assert( (pH->htsize & (pH->htsize-1))==0 );
178320 h = hraw & (pH->htsize-1);
178321 fts3HashInsertElement(pH, &pH->ht[h], new_elem);
178322 new_elem->data = data;
178341 ** Implementation of the full-text-search tokenizer that implements
178400 *ppTokenizer = &t->base;
178414 ** string to be tokenized is zInput[0..nInput-1]. A cursor
178430 c->zInput = zInput;
178432 c->nInput = 0;
178434 c->nInput = (int)strlen(zInput);
178436 c->nInput = nInput;
178438 c->iOffset = 0; /* start tokenizing at the beginning */
178439 c->iToken = 0;
178440 c->zToken = NULL; /* no space allocated, yet. */
178441 c->nAllocated = 0;
178443 *ppCursor = &c->base;
178453 sqlite3_free(c->zToken);
178484 j = cType[x-'a'];
178493 j = cType[x-'a'];
178494 if( j<2 ) return 1-j;
178506 ** vowel-consonant pairs followed by an optional vowel. "m" is the
178510 ** Return true if the m-value for z is 1 or more. In other words,
178553 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
178572 ** are consonant-vowel-consonent and where the final consonant
178609 *(--z) = *(zTo++);
178618 ** US-ASCII case folding. If the input word is too long (more
178629 zOut[i] = c - 'A' + 'a';
178637 for(j=mx, i=nIn-mx; i<nIn; i++, j++){
178648 ** Stem the input word zIn[0..nIn-1]. Store the output in zOut.
178652 ** Any upper-case characters in the US-ASCII character set ([A-Z])
178653 ** are converted to lower case. Upper-case UTF characters are
178660 ** are taken from each end. US-ASCII case folding still applies.
178663 ** in [a-zA-Z] then no stemming is attempted and this routine just
178664 ** copies the input into the input into the output with US-ASCII
178674 if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
178680 for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
178683 zReverse[j] = c + 'a' - 'A';
178687 /* The use of a character not in [a-zA-Z] means that we fallback
178693 memset(&zReverse[sizeof(zReverse)-5], 0, 5);
178723 *(--z) = 'e';
178899 zOut[--i] = *(z++);
178917 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
178932 const char *z = c->zInput;
178934 while( c->iOffset<c->nInput ){
178938 while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
178939 c->iOffset++;
178942 /* Count non-delimiter characters. */
178943 iStartOffset = c->iOffset;
178944 while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
178945 c->iOffset++;
178948 if( c->iOffset>iStartOffset ){
178949 int n = c->iOffset-iStartOffset;
178950 if( n>c->nAllocated ){
178952 c->nAllocated = n+20;
178953 pNew = sqlite3_realloc(c->zToken, c->nAllocated);
178955 c->zToken = pNew;
178957 porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
178958 *pzToken = c->zToken;
178960 *piEndOffset = c->iOffset;
178961 *piPosition = c->iToken++;
178969 ** The set of routines that implement the porter-stemmer tokenizer
179007 ** This is part of an SQLite module implementing full-text search.
179027 ** Return true if the two-argument version of fts3_tokenizer()
179034 sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER,-1,&isEnabled);
179042 ** SELECT <function-name>(<key-name>);
179043 ** SELECT <function-name>(<key-name>, <pointer>);
179045 ** where <function-name> is the name passed as the second argument
179050 ** to the string <key-name>. If <pointer> is not specified, then
179051 ** the string <key-name> must already exist in the has table. Otherwise,
179056 ** to string <key-name> (after the hash-table is updated, if applicable).
179080 sqlite3_result_error(context, "argument type mismatch", -1);
179086 sqlite3_result_error(context, "out of memory", -1);
179089 sqlite3_result_error(context, "fts3tokenize disabled", -1);
179098 sqlite3_result_error(context, zErr, -1);
179155 *pn = (int)(z2-z1);
179163 char **pzErr /* OUT: Set to malloced error message */
179169 char *zEnd; /* Pointer to nul-term of zCopy */
179206 rc = m->xCreate(iArg, aArg, ppTok);
179211 (*ppTok)->pModule = m;
179235 ** SELECT <function-name>(<key-name>, ..., <input-string>);
179237 ** where <function-name> is the name passed as the second argument
179242 ** list. For each token in the <input-string>, three elements are
179245 ** substring of <input-string> associated with the token. For example,
179246 ** using the built-in "simple" tokenizer:
179284 sqlite3_result_error(context, "insufficient arguments", -1);
179290 nInput = sqlite3_value_bytes(argv[argc-1]);
179291 zInput = (const char *)sqlite3_value_text(argv[argc-1]);
179298 sqlite3_result_error(context, zErr2, -1);
179306 for(i=1; i<argc-1; i++){
179307 azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
179310 if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
179314 pTokenizer->pModule = p;
179320 while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
179324 nToken = iEnd-iStart;
179328 if( SQLITE_OK!=p->xClose(pCsr) ){
179332 if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
179339 sqlite3_result_error(context, zErr, -1);
179341 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
179356 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
179361 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
179380 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
179385 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
179405 ** can be used as designed by the C-code in the queryTokenizer and
179449 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
179499 rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
179530 ** Implementation of the "simple" full-text-search tokenizer.
179569 return c<0x80 && t->delim[c];
179589 ** else we need to reindex. One solution would be a meta-table to
179597 /* We explicitly don't support UTF-8 delimiters for now. */
179602 t->delim[ch] = 1;
179605 /* Mark non-alphanumeric ASCII characters as delimiters */
179608 t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
179612 *ppTokenizer = &t->base;
179626 ** string to be tokenized is pInput[0..nBytes-1]. A cursor
179642 c->pInput = pInput;
179644 c->nBytes = 0;
179646 c->nBytes = (int)strlen(pInput);
179648 c->nBytes = nBytes;
179650 c->iOffset = 0; /* start tokenizing at the beginning */
179651 c->iToken = 0;
179652 c->pToken = NULL; /* no space allocated, yet. */
179653 c->nTokenAllocated = 0;
179655 *ppCursor = &c->base;
179665 sqlite3_free(c->pToken);
179683 simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
179684 unsigned char *p = (unsigned char *)c->pInput;
179686 while( c->iOffset<c->nBytes ){
179690 while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
179691 c->iOffset++;
179694 /* Count non-delimiter characters. */
179695 iStartOffset = c->iOffset;
179696 while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
179697 c->iOffset++;
179700 if( c->iOffset>iStartOffset ){
179701 int i, n = c->iOffset-iStartOffset;
179702 if( n>c->nTokenAllocated ){
179704 c->nTokenAllocated = n+20;
179705 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
179707 c->pToken = pNew;
179710 /* TODO(shess) This needs expansion to handle UTF-8
179711 ** case-insensitivity.
179714 c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
179716 *ppToken = c->pToken;
179719 *piEndOffset = c->iOffset;
179720 *piPosition = c->iToken++;
179771 ** <tokenizer-name>, <arg-1>, ...
179851 ** The second argument, argv[], is an array of pointers to nul-terminated
179918 char **pzErr /* OUT: sqlite3_malloc'd error message */
179930 nDequote = argc-3;
179947 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
179959 pTab->pMod = pMod;
179960 pTab->pTok = pTok;
179961 *ppVtab = &pTab->base;
179964 pMod->xDestroy(pTok);
179980 pTab->pMod->xDestroy(pTab->pTok);
179986 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
179995 for(i=0; i<pInfo->nConstraint; i++){
179996 if( pInfo->aConstraint[i].usable
179997 && pInfo->aConstraint[i].iColumn==0
179998 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
180000 pInfo->idxNum = 1;
180001 pInfo->aConstraintUsage[i].argvIndex = 1;
180002 pInfo->aConstraintUsage[i].omit = 1;
180003 pInfo->estimatedCost = 1;
180008 pInfo->idxNum = 0;
180009 assert( pInfo->estimatedCost>1000000.0 );
180015 ** xOpen - Open a cursor.
180036 if( pCsr->pCsr ){
180037 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
180038 pTab->pMod->xClose(pCsr->pCsr);
180039 pCsr->pCsr = 0;
180041 sqlite3_free(pCsr->zInput);
180042 pCsr->zInput = 0;
180043 pCsr->zToken = 0;
180044 pCsr->nToken = 0;
180045 pCsr->iStart = 0;
180046 pCsr->iEnd = 0;
180047 pCsr->iPos = 0;
180048 pCsr->iRowid = 0;
180052 ** xClose - Close a cursor.
180063 ** xNext - Advance the cursor to the next row, if any.
180067 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
180070 pCsr->iRowid++;
180071 rc = pTab->pMod->xNext(pCsr->pCsr,
180072 &pCsr->zToken, &pCsr->nToken,
180073 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
180085 ** xFilter - Initialize a cursor to point at the start of its data.
180096 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
180104 pCsr->zInput = sqlite3_malloc64(nByte+1);
180105 if( pCsr->zInput==0 ){
180108 if( nByte>0 ) memcpy(pCsr->zInput, zByte, nByte);
180109 pCsr->zInput[nByte] = 0;
180110 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
180112 pCsr->pCsr->pTokenizer = pTab->pTok;
180122 ** xEof - Return true if the cursor is at EOF, or false otherwise.
180126 return (pCsr->zToken==0);
180130 ** xColumn - Return a column value.
180142 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
180145 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
180148 sqlite3_result_int(pCtx, pCsr->iStart);
180151 sqlite3_result_int(pCtx, pCsr->iEnd);
180155 sqlite3_result_int(pCtx, pCsr->iPos);
180162 ** xRowid - Return the current rowid for the cursor.
180169 *pRowid = (sqlite3_int64)pCsr->iRowid;
180228 ** tables. It also contains code to merge FTS3 b-tree segments. Some
180229 ** of the sub-routines used to merge segments are also used by the query
180244 ** When full-text index nodes are loaded from disk, the buffer that they
180246 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
180256 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
180262 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
180264 ** The code is written so that the hard lower-limit for each of these values
180298 sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
180335 ** a contiguous set of segment b-tree leaf nodes. Although the details of
180338 ** terms when querying the full-text index. See functions:
180353 u8 rootOnly; /* True for a root-only reader */
180382 int nOffsetList; /* For descending pending seg-readers only */
180386 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
180387 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
180390 ** An instance of this structure is used to create a segment b-tree in the
180414 ** the interior part of the segment b+-tree structures (everything except
180423 ** or the pending-terms table being flushed), leaves are written into the
180426 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
180432 SegmentNode *pRight; /* Pointer to right-sibling */
180433 SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */
180556 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
180601 /* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
180603 /* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
180609 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
180612 pStmt = p->aStmt[eStmt];
180617 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
180620 zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
180622 zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
180627 rc = sqlite3_prepare_v3(p->db, zSql, -1, f, &pStmt, NULL);
180630 p->aStmt[eStmt] = pStmt;
180727 ** shared-cache table-lock on the %_segdir table. This is required before
180742 if( p->nPendingData==0 ){
180756 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
180763 ** level within the index) are encoded in 64-bit integer values stored
180765 ** separate component values into the single 64-bit integer value that
180768 ** Specifically, each language-id/index combination is allocated 1024
180769 ** 64-bit integer level values ("absolute levels"). The main terms index
180770 ** for language-id 0 is allocate values 0-1023. The first prefix index
180771 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
180775 ** absolute levels that corresponds to language-id iLangid and index
180781 int iIndex, /* Index in p->aIndex[] */
180786 assert( p->nIndex>0 );
180787 assert( iIndex>=0 && iIndex<p->nIndex );
180789 iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
180813 int iIndex, /* Index for p->aIndex[] */
180822 assert( iIndex>=0 && iIndex<p->nIndex );
180830 getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
180869 p->nSpace = 100;
180870 p->aData = (char *)&p[1];
180871 p->nData = 0;
180873 else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
180874 int nNew = p->nSpace * 2;
180881 p->nSpace = nNew;
180882 p->aData = (char *)&p[1];
180886 p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
180887 p->aData[p->nData] = '\0';
180893 ** Add a docid/column/position entry to a PendingList structure. Non-zero
180911 assert( !p || p->iLastDocid<=iDocid );
180913 if( !p || p->iLastDocid!=iDocid ){
180914 u64 iDelta = (u64)iDocid - (u64)(p ? p->iLastDocid : 0);
180916 assert( p->nData<p->nSpace );
180917 assert( p->aData[p->nData]==0 );
180918 p->nData++;
180923 p->iLastCol = -1;
180924 p->iLastPos = 0;
180925 p->iLastDocid = iDocid;
180927 if( iCol>0 && p->iLastCol!=iCol ){
180933 p->iLastCol = iCol;
180934 p->iLastPos = 0;
180937 assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
180938 rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
180940 p->iLastPos = iPos;
180961 ** Add an entry to one of the pending-terms hash tables.
180976 p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
180978 if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
180989 p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
180995 ** Tokenize the nul-terminated string zText and add all tokens to the
180996 ** pending-terms hash-table. The docid used is that currently stored in
180997 ** p->iPrevDocid, and the column is specified by argument iCol.
181017 sqlite3_tokenizer *pTokenizer = p->pTokenizer;
181018 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
181033 rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
181038 xNext = pModule->xNext;
181045 /* Positions cannot be negative; we use -1 as a terminator internally.
181046 ** Tokens must have a non-zero length.
181055 p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
181060 for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
181061 struct Fts3Index *pIndex = &p->aIndex[i];
181062 if( nToken<pIndex->nPrefix ) continue;
181064 p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
181069 pModule->xClose(pCsr);
181076 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
181080 Fts3Table *p, /* Full-text table handle */
181089 ** forced-flush would provide better performance. I suspect that if
181094 if( iDocid<p->iPrevDocid
181095 || (iDocid==p->iPrevDocid && p->bPrevDelete==0)
181096 || p->iPrevLangid!=iLangid
181097 || p->nPendingData>p->nMaxPendingData
181102 p->iPrevDocid = iDocid;
181103 p->iPrevLangid = iLangid;
181104 p->bPrevDelete = bDelete;
181109 ** Discard the contents of the pending-terms hash tables.
181113 for(i=0; i<p->nIndex; i++){
181115 Fts3Hash *pHash = &p->aIndex[i].hPending;
181122 p->nPendingData = 0;
181140 for(i=2; i<p->nColumn+2; i++){
181141 int iCol = i-2;
181142 if( p->abNotindexed[iCol]==0 ){
181148 aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
181161 ** apVal[2] Left-most user-defined column
181163 ** apVal[p->nColumn+1] Right-most user-defined column
181164 ** apVal[p->nColumn+2] Hidden column with same name as table
181165 ** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid)
181166 ** apVal[p->nColumn+4] Hidden languageid column
181169 Fts3Table *p, /* Full-text table */
181176 if( p->zContentTbl ){
181177 sqlite3_value *pRowid = apVal[p->nColumn+3];
181197 if( rc==SQLITE_OK && p->zLanguageid ){
181199 pContentInsert, p->nColumn+2,
181200 sqlite3_value_int(apVal[p->nColumn+4])
181212 ** In FTS3, this is an error. It is an error to specify non-NULL values
181215 if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
181222 rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
181232 *piDocid = sqlite3_last_insert_rowid(p->db);
181245 /* Discard the contents of the pending-terms hash table. */
181250 assert( p->zContentTbl==0 || bContent==0 );
181254 if( p->bHasDocsize ){
181257 if( p->bHasStat ){
181268 if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
181275 ** full-text index.
181296 for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
181297 int iCol = i-1;
181298 if( p->abNotindexed[iCol]==0 ){
181300 rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
181301 aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
181342 int iIndex, /* Index for p->aIndex */
181351 assert( p->nIndex>=1 );
181397 ** paBlob is non-NULL, then it is the responsibility of the caller to
181419 /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
181422 if( p->pSegments ){
181423 rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
181425 if( 0==p->zSegmentsTbl ){
181426 p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
181427 if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
181430 p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
181435 int nByte = sqlite3_blob_bytes(p->pSegments);
181446 rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
181463 ** Close the blob handle at p->pSegments, if it is open. See comments above
181467 sqlite3_blob_close(p->pSegments);
181468 p->pSegments = 0;
181475 nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
181477 pReader->pBlob,
181478 &pReader->aNode[pReader->nPopulate],
181480 pReader->nPopulate
181484 pReader->nPopulate += nRead;
181485 memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
181486 if( pReader->nPopulate==pReader->nNode ){
181487 sqlite3_blob_close(pReader->pBlob);
181488 pReader->pBlob = 0;
181489 pReader->nPopulate = 0;
181497 assert( !pReader->pBlob
181498 || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
181500 while( pReader->pBlob && rc==SQLITE_OK
181501 && (pFrom - pReader->aNode + nByte)>pReader->nPopulate
181513 sqlite3_free(pSeg->aNode);
181514 sqlite3_blob_close(pSeg->pBlob);
181515 pSeg->pBlob = 0;
181517 pSeg->aNode = 0;
181530 int rc; /* Return code of various sub-routines */
181535 if( !pReader->aDoclist ){
181536 pNext = pReader->aNode;
181538 pNext = &pReader->aDoclist[pReader->nDoclist];
181541 if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
181544 Fts3HashElem *pElem = *(pReader->ppNextElem);
181545 sqlite3_free(pReader->aNode);
181546 pReader->aNode = 0;
181550 int nCopy = pList->nData+1;
181551 pReader->zTerm = (char *)fts3HashKey(pElem);
181552 pReader->nTerm = fts3HashKeysize(pElem);
181555 memcpy(aCopy, pList->aData, nCopy);
181556 pReader->nNode = pReader->nDoclist = nCopy;
181557 pReader->aNode = pReader->aDoclist = aCopy;
181558 pReader->ppNextElem++;
181559 assert( pReader->aNode );
181569 assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock || CORRUPT_DB );
181571 if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
181576 p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
181577 (bIncr ? &pReader->nPopulate : 0)
181580 assert( pReader->pBlob==0 );
181581 if( bIncr && pReader->nPopulate<pReader->nNode ){
181582 pReader->pBlob = p->pSegments;
181583 p->pSegments = 0;
181585 pNext = pReader->aNode;
181598 || (&pReader->aNode[pReader->nNode] - pNext)<nSuffix
181599 || nPrefix>pReader->nTerm
181606 ** overflow - hence the (i64) casts. */
181607 if( (i64)nPrefix+nSuffix>(i64)pReader->nTermAlloc ){
181609 char *zNew = sqlite3_realloc64(pReader->zTerm, nNew);
181613 pReader->zTerm = zNew;
181614 pReader->nTermAlloc = nNew;
181620 memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
181621 pReader->nTerm = nPrefix+nSuffix;
181623 pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
181624 pReader->aDoclist = pNext;
181625 pReader->pOffsetList = 0;
181628 ** b-tree node. And that the final byte of the doclist is 0x00. If either
181631 if( pReader->nDoclist > pReader->nNode-(pReader->aDoclist-pReader->aNode)
181632 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
181633 || pReader->nDoclist==0
181646 assert( pReader->aDoclist );
181647 assert( !pReader->pOffsetList );
181648 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
181650 pReader->iDocid = 0;
181651 pReader->nOffsetList = 0;
181653 pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
181654 &pReader->iDocid, &pReader->nOffsetList, &bEof
181657 rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
181659 int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
181660 pReader->pOffsetList = &pReader->aDoclist[n];
181671 ** *ppOffsetList is set to point to the first column-offset list
181673 ** *pnOffsetList is set to the length of the set of column-offset
181674 ** lists, not including the nul-terminator byte. For example:
181679 char **ppOffsetList, /* OUT: Pointer to current position-list */
181683 char *p = pReader->pOffsetList;
181688 if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
181689 /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
181690 ** Pending-terms doclists are always built up in ascending order, so
181694 *ppOffsetList = pReader->pOffsetList;
181695 *pnOffsetList = pReader->nOffsetList - 1;
181698 pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
181699 &pReader->nOffsetList, &bEof
181702 pReader->pOffsetList = 0;
181704 pReader->pOffsetList = p;
181707 char *pEnd = &pReader->aDoclist[pReader->nDoclist];
181718 ** the populated part of pReader->aNode[].
181723 if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
181730 ** size of the previous offset-list.
181733 *ppOffsetList = pReader->pOffsetList;
181734 *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
181746 pReader->pOffsetList = 0;
181751 pReader->pOffsetList = p + sqlite3Fts3GetVarintU(p, &iDelta);
181752 if( pTab->bDescIdx ){
181753 pReader->iDocid = (i64)((u64)pReader->iDocid - iDelta);
181755 pReader->iDocid = (i64)((u64)pReader->iDocid + iDelta);
181770 Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
181774 int pgsz = p->nPgsz;
181776 assert( p->bFts4 );
181779 for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
181780 Fts3SegReader *pReader = pMsr->apSegment[ii];
181785 for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
181806 sqlite3_free(pReader->zTerm);
181809 sqlite3_free(pReader->aNode);
181811 sqlite3_blob_close(pReader->pBlob);
181847 pReader->iIdx = iAge;
181848 pReader->bLookup = bLookup!=0;
181849 pReader->iStartBlock = iStartLeaf;
181850 pReader->iLeafEndBlock = iEndLeaf;
181851 pReader->iEndBlock = iEndBlock;
181855 pReader->aNode = (char *)&pReader[1];
181856 pReader->rootOnly = 1;